byte_alignment_test.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright (c) 2014 The WebM project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include <string>
  11. #include "./vpx_config.h"
  12. #include "test/codec_factory.h"
  13. #include "test/decode_test_driver.h"
  14. #include "test/md5_helper.h"
  15. #include "test/util.h"
  16. #if CONFIG_WEBM_IO
  17. #include "test/webm_video_source.h"
  18. #endif
  19. namespace {
  20. #if CONFIG_WEBM_IO
  21. const int kLegacyByteAlignment = 0;
  22. const int kLegacyYPlaneByteAlignment = 32;
  23. const int kNumPlanesToCheck = 3;
  24. const char kVP9TestFile[] = "vp90-2-02-size-lf-1920x1080.webm";
  25. const char kVP9Md5File[] = "vp90-2-02-size-lf-1920x1080.webm.md5";
  26. struct ByteAlignmentTestParam {
  27. int byte_alignment;
  28. vpx_codec_err_t expected_value;
  29. bool decode_remaining;
  30. };
  31. const ByteAlignmentTestParam kBaTestParams[] = {
  32. { kLegacyByteAlignment, VPX_CODEC_OK, true },
  33. { 32, VPX_CODEC_OK, true },
  34. { 64, VPX_CODEC_OK, true },
  35. { 128, VPX_CODEC_OK, true },
  36. { 256, VPX_CODEC_OK, true },
  37. { 512, VPX_CODEC_OK, true },
  38. { 1024, VPX_CODEC_OK, true },
  39. { 1, VPX_CODEC_INVALID_PARAM, false },
  40. { -2, VPX_CODEC_INVALID_PARAM, false },
  41. { 4, VPX_CODEC_INVALID_PARAM, false },
  42. { 16, VPX_CODEC_INVALID_PARAM, false },
  43. { 255, VPX_CODEC_INVALID_PARAM, false },
  44. { 2048, VPX_CODEC_INVALID_PARAM, false },
  45. };
  46. // Class for testing byte alignment of reference buffers.
  47. class ByteAlignmentTest
  48. : public ::testing::TestWithParam<ByteAlignmentTestParam> {
  49. protected:
  50. ByteAlignmentTest() : video_(NULL), decoder_(NULL), md5_file_(NULL) {}
  51. virtual void SetUp() {
  52. video_ = new libvpx_test::WebMVideoSource(kVP9TestFile);
  53. ASSERT_TRUE(video_ != NULL);
  54. video_->Init();
  55. video_->Begin();
  56. const vpx_codec_dec_cfg_t cfg = vpx_codec_dec_cfg_t();
  57. decoder_ = new libvpx_test::VP9Decoder(cfg, 0);
  58. ASSERT_TRUE(decoder_ != NULL);
  59. OpenMd5File(kVP9Md5File);
  60. }
  61. virtual void TearDown() {
  62. if (md5_file_ != NULL) fclose(md5_file_);
  63. delete decoder_;
  64. delete video_;
  65. }
  66. void SetByteAlignment(int byte_alignment, vpx_codec_err_t expected_value) {
  67. decoder_->Control(VP9_SET_BYTE_ALIGNMENT, byte_alignment, expected_value);
  68. }
  69. vpx_codec_err_t DecodeOneFrame(int byte_alignment_to_check) {
  70. const vpx_codec_err_t res =
  71. decoder_->DecodeFrame(video_->cxdata(), video_->frame_size());
  72. CheckDecodedFrames(byte_alignment_to_check);
  73. if (res == VPX_CODEC_OK) video_->Next();
  74. return res;
  75. }
  76. vpx_codec_err_t DecodeRemainingFrames(int byte_alignment_to_check) {
  77. for (; video_->cxdata() != NULL; video_->Next()) {
  78. const vpx_codec_err_t res =
  79. decoder_->DecodeFrame(video_->cxdata(), video_->frame_size());
  80. if (res != VPX_CODEC_OK) return res;
  81. CheckDecodedFrames(byte_alignment_to_check);
  82. }
  83. return VPX_CODEC_OK;
  84. }
  85. private:
  86. // Check if |data| is aligned to |byte_alignment_to_check|.
  87. // |byte_alignment_to_check| must be a power of 2.
  88. void CheckByteAlignment(const uint8_t *data, int byte_alignment_to_check) {
  89. ASSERT_EQ(0u, reinterpret_cast<size_t>(data) % byte_alignment_to_check);
  90. }
  91. // Iterate through the planes of the decoded frames and check for
  92. // alignment based off |byte_alignment_to_check|.
  93. void CheckDecodedFrames(int byte_alignment_to_check) {
  94. libvpx_test::DxDataIterator dec_iter = decoder_->GetDxData();
  95. const vpx_image_t *img;
  96. // Get decompressed data
  97. while ((img = dec_iter.Next()) != NULL) {
  98. if (byte_alignment_to_check == kLegacyByteAlignment) {
  99. CheckByteAlignment(img->planes[0], kLegacyYPlaneByteAlignment);
  100. } else {
  101. for (int i = 0; i < kNumPlanesToCheck; ++i) {
  102. CheckByteAlignment(img->planes[i], byte_alignment_to_check);
  103. }
  104. }
  105. CheckMd5(*img);
  106. }
  107. }
  108. // TODO(fgalligan): Move the MD5 testing code into another class.
  109. void OpenMd5File(const std::string &md5_file_name_) {
  110. md5_file_ = libvpx_test::OpenTestDataFile(md5_file_name_);
  111. ASSERT_TRUE(md5_file_ != NULL)
  112. << "MD5 file open failed. Filename: " << md5_file_name_;
  113. }
  114. void CheckMd5(const vpx_image_t &img) {
  115. ASSERT_TRUE(md5_file_ != NULL);
  116. char expected_md5[33];
  117. char junk[128];
  118. // Read correct md5 checksums.
  119. const int res = fscanf(md5_file_, "%s %s", expected_md5, junk);
  120. ASSERT_NE(EOF, res) << "Read md5 data failed";
  121. expected_md5[32] = '\0';
  122. ::libvpx_test::MD5 md5_res;
  123. md5_res.Add(&img);
  124. const char *const actual_md5 = md5_res.Get();
  125. // Check md5 match.
  126. ASSERT_STREQ(expected_md5, actual_md5) << "MD5 checksums don't match";
  127. }
  128. libvpx_test::WebMVideoSource *video_;
  129. libvpx_test::VP9Decoder *decoder_;
  130. FILE *md5_file_;
  131. };
  132. TEST_F(ByteAlignmentTest, SwitchByteAlignment) {
  133. const int num_elements = 14;
  134. const int byte_alignments[] = { 0, 32, 64, 128, 256, 512, 1024,
  135. 0, 1024, 32, 512, 64, 256, 128 };
  136. for (int i = 0; i < num_elements; ++i) {
  137. SetByteAlignment(byte_alignments[i], VPX_CODEC_OK);
  138. ASSERT_EQ(VPX_CODEC_OK, DecodeOneFrame(byte_alignments[i]));
  139. }
  140. SetByteAlignment(byte_alignments[0], VPX_CODEC_OK);
  141. ASSERT_EQ(VPX_CODEC_OK, DecodeRemainingFrames(byte_alignments[0]));
  142. }
  143. TEST_P(ByteAlignmentTest, TestAlignment) {
  144. const ByteAlignmentTestParam t = GetParam();
  145. SetByteAlignment(t.byte_alignment, t.expected_value);
  146. if (t.decode_remaining) {
  147. ASSERT_EQ(VPX_CODEC_OK, DecodeRemainingFrames(t.byte_alignment));
  148. }
  149. }
  150. INSTANTIATE_TEST_CASE_P(Alignments, ByteAlignmentTest,
  151. ::testing::ValuesIn(kBaTestParams));
  152. #endif // CONFIG_WEBM_IO
  153. } // namespace