]> granicus.if.org Git - libvpx/blob - test/byte_alignment_test.cc
Revert "Revert "Add support for setting byte alignment.""
[libvpx] / test / byte_alignment_test.cc
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 const int kLegacyByteAlignment = 0;
25 const int kLegacyYPlaneByteAlignment = 32;
26 const int kNumPlanesToCheck = 3;
27 const char kVP9TestFile[] = "vp90-2-02-size-lf-1920x1080.webm";
28 const char kVP9Md5File[] = "vp90-2-02-size-lf-1920x1080.webm.md5";
29
30 #if CONFIG_WEBM_IO
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:
58   ByteAlignmentTest()
59       : video_(NULL),
60         decoder_(NULL),
61         md5_file_(NULL) {}
62
63   virtual void SetUp() {
64     video_ = new libvpx_test::WebMVideoSource(kVP9TestFile);
65     ASSERT_TRUE(video_ != NULL);
66     video_->Init();
67     video_->Begin();
68
69     const vpx_codec_dec_cfg_t cfg = vpx_codec_dec_cfg_t();
70     decoder_ = new libvpx_test::VP9Decoder(cfg, 0);
71     ASSERT_TRUE(decoder_ != NULL);
72
73     OpenMd5File(kVP9Md5File);
74   }
75
76   virtual void TearDown() {
77     if (md5_file_ != NULL)
78       fclose(md5_file_);
79
80     delete decoder_;
81     delete video_;
82   }
83
84   void SetByteAlignment(int byte_alignment, vpx_codec_err_t expected_value) {
85     decoder_->Control(VP9_SET_BYTE_ALIGNMENT, byte_alignment, expected_value);
86   }
87
88   vpx_codec_err_t DecodeOneFrame(int byte_alignment_to_check) {
89     const vpx_codec_err_t res =
90         decoder_->DecodeFrame(video_->cxdata(), video_->frame_size());
91     CheckDecodedFrames(byte_alignment_to_check);
92     if (res == VPX_CODEC_OK)
93       video_->Next();
94     return res;
95   }
96
97   vpx_codec_err_t DecodeRemainingFrames(int byte_alignment_to_check) {
98     for (; video_->cxdata() != NULL; video_->Next()) {
99       const vpx_codec_err_t res =
100           decoder_->DecodeFrame(video_->cxdata(), video_->frame_size());
101       if (res != VPX_CODEC_OK)
102         return res;
103       CheckDecodedFrames(byte_alignment_to_check);
104     }
105     return VPX_CODEC_OK;
106   }
107
108  private:
109   // Check if |data| is aligned to |byte_alignment_to_check|.
110   // |byte_alignment_to_check| must be a power of 2.
111   void CheckByteAlignment(const uint8_t *data, int byte_alignment_to_check) {
112     ASSERT_EQ(0u, reinterpret_cast<size_t>(data) % byte_alignment_to_check);
113   }
114
115   // Iterate through the planes of the decoded frames and check for
116   // alignment based off |byte_alignment_to_check|.
117   void CheckDecodedFrames(int byte_alignment_to_check) {
118     libvpx_test::DxDataIterator dec_iter = decoder_->GetDxData();
119     const vpx_image_t *img;
120
121     // Get decompressed data
122     while ((img = dec_iter.Next()) != NULL) {
123       if (byte_alignment_to_check == kLegacyByteAlignment) {
124         CheckByteAlignment(img->planes[0], kLegacyYPlaneByteAlignment);
125       } else {
126         for (int i = 0; i < kNumPlanesToCheck; ++i) {
127           CheckByteAlignment(img->planes[i], byte_alignment_to_check);
128         }
129       }
130       CheckMd5(*img);
131     }
132   }
133
134   // TODO(fgalligan): Move the MD5 testing code into another class.
135   void OpenMd5File(const std::string &md5_file_name_) {
136     md5_file_ = libvpx_test::OpenTestDataFile(md5_file_name_);
137     ASSERT_TRUE(md5_file_ != NULL) << "MD5 file open failed. Filename: "
138         << md5_file_name_;
139   }
140
141   void CheckMd5(const vpx_image_t &img) {
142     ASSERT_TRUE(md5_file_ != NULL);
143     char expected_md5[33];
144     char junk[128];
145
146     // Read correct md5 checksums.
147     const int res = fscanf(md5_file_, "%s  %s", expected_md5, junk);
148     ASSERT_NE(EOF, res) << "Read md5 data failed";
149     expected_md5[32] = '\0';
150
151     ::libvpx_test::MD5 md5_res;
152     md5_res.Add(&img);
153     const char *const actual_md5 = md5_res.Get();
154
155     // Check md5 match.
156     ASSERT_STREQ(expected_md5, actual_md5) << "MD5 checksums don't match";
157   }
158
159   libvpx_test::WebMVideoSource *video_;
160   libvpx_test::VP9Decoder *decoder_;
161   FILE *md5_file_;
162 };
163
164 TEST_F(ByteAlignmentTest, SwitchByteAlignment) {
165   const int num_elements = 14;
166   const int byte_alignments[] = { 0, 32, 64, 128, 256, 512, 1024,
167                                   0, 1024, 32, 512, 64, 256, 128 };
168
169   for (int i = 0; i < num_elements; ++i) {
170     SetByteAlignment(byte_alignments[i], VPX_CODEC_OK);
171     ASSERT_EQ(VPX_CODEC_OK, DecodeOneFrame(byte_alignments[i]));
172   }
173   SetByteAlignment(byte_alignments[0], VPX_CODEC_OK);
174   ASSERT_EQ(VPX_CODEC_OK, DecodeRemainingFrames(byte_alignments[0]));
175 }
176
177 TEST_P(ByteAlignmentTest, TestAlignment) {
178   const ByteAlignmentTestParam t = GetParam();
179   SetByteAlignment(t.byte_alignment, t.expected_value);
180   if (t.decode_remaining)
181     ASSERT_EQ(VPX_CODEC_OK, DecodeRemainingFrames(t.byte_alignment));
182 }
183
184 INSTANTIATE_TEST_CASE_P(Alignments, ByteAlignmentTest,
185                         ::testing::ValuesIn(kBaTestParams));
186
187 #endif  // CONFIG_WEBM_IO
188
189 }  // namespace