xref: /aosp_15_r20/external/libvpx/test/byte_alignment_test.cc (revision fb1b10ab9aebc7c7068eedab379b749d7e3900be)
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 
11 #include <string>
12 
13 #include "./vpx_config.h"
14 #include "test/codec_factory.h"
15 #include "test/decode_test_driver.h"
16 #include "test/md5_helper.h"
17 #include "test/util.h"
18 #if CONFIG_WEBM_IO
19 #include "test/webm_video_source.h"
20 #endif
21 
22 namespace {
23 
24 #if CONFIG_WEBM_IO
25 
26 const int kLegacyByteAlignment = 0;
27 const int kLegacyYPlaneByteAlignment = 32;
28 const int kNumPlanesToCheck = 3;
29 const char kVP9TestFile[] = "vp90-2-02-size-lf-1920x1080.webm";
30 const char kVP9Md5File[] = "vp90-2-02-size-lf-1920x1080.webm.md5";
31 
32 struct ByteAlignmentTestParam {
33   int byte_alignment;
34   vpx_codec_err_t expected_value;
35   bool decode_remaining;
36 };
37 
38 const ByteAlignmentTestParam kBaTestParams[] = {
39   { kLegacyByteAlignment, VPX_CODEC_OK, true },
40   { 32, VPX_CODEC_OK, true },
41   { 64, VPX_CODEC_OK, true },
42   { 128, VPX_CODEC_OK, true },
43   { 256, VPX_CODEC_OK, true },
44   { 512, VPX_CODEC_OK, true },
45   { 1024, VPX_CODEC_OK, true },
46   { 1, VPX_CODEC_INVALID_PARAM, false },
47   { -2, VPX_CODEC_INVALID_PARAM, false },
48   { 4, VPX_CODEC_INVALID_PARAM, false },
49   { 16, VPX_CODEC_INVALID_PARAM, false },
50   { 255, VPX_CODEC_INVALID_PARAM, false },
51   { 2048, VPX_CODEC_INVALID_PARAM, false },
52 };
53 
54 // Class for testing byte alignment of reference buffers.
55 class ByteAlignmentTest
56     : public ::testing::TestWithParam<ByteAlignmentTestParam> {
57  protected:
ByteAlignmentTest()58   ByteAlignmentTest()
59       : video_(nullptr), decoder_(nullptr), md5_file_(nullptr) {}
60 
SetUp()61   void SetUp() override {
62     video_ = new libvpx_test::WebMVideoSource(kVP9TestFile);
63     ASSERT_NE(video_, nullptr);
64     video_->Init();
65     video_->Begin();
66 
67     const vpx_codec_dec_cfg_t cfg = vpx_codec_dec_cfg_t();
68     decoder_ = new libvpx_test::VP9Decoder(cfg, 0);
69     ASSERT_NE(decoder_, nullptr);
70 
71     OpenMd5File(kVP9Md5File);
72   }
73 
TearDown()74   void TearDown() override {
75     if (md5_file_ != nullptr) fclose(md5_file_);
76 
77     delete decoder_;
78     delete video_;
79   }
80 
SetByteAlignment(int byte_alignment,vpx_codec_err_t expected_value)81   void SetByteAlignment(int byte_alignment, vpx_codec_err_t expected_value) {
82     decoder_->Control(VP9_SET_BYTE_ALIGNMENT, byte_alignment, expected_value);
83   }
84 
DecodeOneFrame(int byte_alignment_to_check)85   vpx_codec_err_t DecodeOneFrame(int byte_alignment_to_check) {
86     const vpx_codec_err_t res =
87         decoder_->DecodeFrame(video_->cxdata(), video_->frame_size());
88     CheckDecodedFrames(byte_alignment_to_check);
89     if (res == VPX_CODEC_OK) video_->Next();
90     return res;
91   }
92 
DecodeRemainingFrames(int byte_alignment_to_check)93   vpx_codec_err_t DecodeRemainingFrames(int byte_alignment_to_check) {
94     for (; video_->cxdata() != nullptr; video_->Next()) {
95       const vpx_codec_err_t res =
96           decoder_->DecodeFrame(video_->cxdata(), video_->frame_size());
97       if (res != VPX_CODEC_OK) return res;
98       CheckDecodedFrames(byte_alignment_to_check);
99     }
100     return VPX_CODEC_OK;
101   }
102 
103  private:
104   // Check if |data| is aligned to |byte_alignment_to_check|.
105   // |byte_alignment_to_check| must be a power of 2.
CheckByteAlignment(const uint8_t * data,int byte_alignment_to_check)106   void CheckByteAlignment(const uint8_t *data, int byte_alignment_to_check) {
107     ASSERT_EQ(0u, reinterpret_cast<size_t>(data) % byte_alignment_to_check);
108   }
109 
110   // Iterate through the planes of the decoded frames and check for
111   // alignment based off |byte_alignment_to_check|.
CheckDecodedFrames(int byte_alignment_to_check)112   void CheckDecodedFrames(int byte_alignment_to_check) {
113     libvpx_test::DxDataIterator dec_iter = decoder_->GetDxData();
114     const vpx_image_t *img;
115 
116     // Get decompressed data
117     while ((img = dec_iter.Next()) != nullptr) {
118       if (byte_alignment_to_check == kLegacyByteAlignment) {
119         CheckByteAlignment(img->planes[0], kLegacyYPlaneByteAlignment);
120       } else {
121         for (int i = 0; i < kNumPlanesToCheck; ++i) {
122           CheckByteAlignment(img->planes[i], byte_alignment_to_check);
123         }
124       }
125       CheckMd5(*img);
126     }
127   }
128 
129   // TODO(fgalligan): Move the MD5 testing code into another class.
OpenMd5File(const std::string & md5_file_name_)130   void OpenMd5File(const std::string &md5_file_name_) {
131     md5_file_ = libvpx_test::OpenTestDataFile(md5_file_name_);
132     ASSERT_NE(md5_file_, nullptr)
133         << "MD5 file open failed. Filename: " << md5_file_name_;
134   }
135 
CheckMd5(const vpx_image_t & img)136   void CheckMd5(const vpx_image_t &img) {
137     ASSERT_NE(md5_file_, nullptr);
138     char expected_md5[33];
139     char junk[128];
140 
141     // Read correct md5 checksums.
142     const int res = fscanf(md5_file_, "%s  %s", expected_md5, junk);
143     ASSERT_NE(EOF, res) << "Read md5 data failed";
144     expected_md5[32] = '\0';
145 
146     ::libvpx_test::MD5 md5_res;
147     md5_res.Add(&img);
148     const char *const actual_md5 = md5_res.Get();
149 
150     // Check md5 match.
151     ASSERT_STREQ(expected_md5, actual_md5) << "MD5 checksums don't match";
152   }
153 
154   libvpx_test::WebMVideoSource *video_;
155   libvpx_test::VP9Decoder *decoder_;
156   FILE *md5_file_;
157 };
158 
TEST_F(ByteAlignmentTest,SwitchByteAlignment)159 TEST_F(ByteAlignmentTest, SwitchByteAlignment) {
160   const int num_elements = 14;
161   const int byte_alignments[] = { 0, 32,   64, 128, 256, 512, 1024,
162                                   0, 1024, 32, 512, 64,  256, 128 };
163 
164   for (int i = 0; i < num_elements; ++i) {
165     SetByteAlignment(byte_alignments[i], VPX_CODEC_OK);
166     ASSERT_EQ(VPX_CODEC_OK, DecodeOneFrame(byte_alignments[i]));
167   }
168   SetByteAlignment(byte_alignments[0], VPX_CODEC_OK);
169   ASSERT_EQ(VPX_CODEC_OK, DecodeRemainingFrames(byte_alignments[0]));
170 }
171 
TEST_P(ByteAlignmentTest,TestAlignment)172 TEST_P(ByteAlignmentTest, TestAlignment) {
173   const ByteAlignmentTestParam t = GetParam();
174   SetByteAlignment(t.byte_alignment, t.expected_value);
175   if (t.decode_remaining) {
176     ASSERT_EQ(VPX_CODEC_OK, DecodeRemainingFrames(t.byte_alignment));
177   }
178 }
179 
180 INSTANTIATE_TEST_SUITE_P(Alignments, ByteAlignmentTest,
181                          ::testing::ValuesIn(kBaTestParams));
182 
183 #endif  // CONFIG_WEBM_IO
184 
185 }  // namespace
186