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