1/*
2 *  Copyright (c) 2015 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 "test/codec_factory.h"
14#include "test/decode_test_driver.h"
15#include "test/md5_helper.h"
16#include "test/util.h"
17#include "test/webm_video_source.h"
18
19namespace {
20
21const char kVp9TestFile[] = "vp90-2-08-tile_1x8_frame_parallel.webm";
22const char kVp9Md5File[] = "vp90-2-08-tile_1x8_frame_parallel.webm.md5";
23
24// Class for testing shutting off the loop filter.
25class SkipLoopFilterTest {
26 public:
27  SkipLoopFilterTest()
28      : video_(NULL),
29        decoder_(NULL),
30        md5_file_(NULL) {}
31
32  ~SkipLoopFilterTest() {
33    if (md5_file_ != NULL)
34      fclose(md5_file_);
35    delete decoder_;
36    delete video_;
37  }
38
39  // If |threads| > 0 then set the decoder with that number of threads.
40  void Init(int num_threads) {
41    expected_md5_[0] = '\0';
42    junk_[0] = '\0';
43    video_ = new libvpx_test::WebMVideoSource(kVp9TestFile);
44    ASSERT_TRUE(video_ != NULL);
45    video_->Init();
46    video_->Begin();
47
48    vpx_codec_dec_cfg_t cfg = vpx_codec_dec_cfg_t();
49    if (num_threads > 0)
50      cfg.threads = num_threads;
51    decoder_ = new libvpx_test::VP9Decoder(cfg, 0);
52    ASSERT_TRUE(decoder_ != NULL);
53
54    OpenMd5File(kVp9Md5File);
55  }
56
57  // Set the VP9 skipLoopFilter control value.
58  void SetSkipLoopFilter(int value, vpx_codec_err_t expected_value) {
59    decoder_->Control(VP9_SET_SKIP_LOOP_FILTER, value, expected_value);
60  }
61
62  vpx_codec_err_t DecodeOneFrame() {
63    const vpx_codec_err_t res =
64        decoder_->DecodeFrame(video_->cxdata(), video_->frame_size());
65    if (res == VPX_CODEC_OK) {
66      ReadMd5();
67      video_->Next();
68    }
69    return res;
70  }
71
72  vpx_codec_err_t DecodeRemainingFrames() {
73    for (; video_->cxdata() != NULL; video_->Next()) {
74      const vpx_codec_err_t res =
75          decoder_->DecodeFrame(video_->cxdata(), video_->frame_size());
76      if (res != VPX_CODEC_OK)
77        return res;
78      ReadMd5();
79    }
80    return VPX_CODEC_OK;
81  }
82
83  // Checks if MD5 matches or doesn't.
84  void CheckMd5(bool matches) {
85    libvpx_test::DxDataIterator dec_iter = decoder_->GetDxData();
86    const vpx_image_t *img = dec_iter.Next();
87    CheckMd5Vpx(*img, matches);
88  }
89
90 private:
91  // TODO(fgalligan): Move the MD5 testing code into another class.
92  void OpenMd5File(const std::string &md5_file_name) {
93    md5_file_ = libvpx_test::OpenTestDataFile(md5_file_name);
94    ASSERT_TRUE(md5_file_ != NULL) << "MD5 file open failed. Filename: "
95        << md5_file_name;
96  }
97
98  // Reads the next line of the MD5 file.
99  void ReadMd5() {
100    ASSERT_TRUE(md5_file_ != NULL);
101    const int res = fscanf(md5_file_, "%s  %s", expected_md5_, junk_);
102    ASSERT_NE(EOF, res) << "Read md5 data failed";
103    expected_md5_[32] = '\0';
104  }
105
106  // Checks if the last read MD5 matches |img| or doesn't.
107  void CheckMd5Vpx(const vpx_image_t &img, bool matches) {
108    ::libvpx_test::MD5 md5_res;
109    md5_res.Add(&img);
110    const char *const actual_md5 = md5_res.Get();
111
112    // Check MD5.
113    if (matches)
114      ASSERT_STREQ(expected_md5_, actual_md5) << "MD5 checksums don't match";
115    else
116      ASSERT_STRNE(expected_md5_, actual_md5) << "MD5 checksums match";
117  }
118
119  libvpx_test::WebMVideoSource *video_;
120  libvpx_test::VP9Decoder *decoder_;
121  FILE *md5_file_;
122  char expected_md5_[33];
123  char junk_[128];
124};
125
126TEST(SkipLoopFilterTest, ShutOffLoopFilter) {
127  const int non_zero_value = 1;
128  const int num_threads = 0;
129  SkipLoopFilterTest skip_loop_filter;
130  skip_loop_filter.Init(num_threads);
131  skip_loop_filter.SetSkipLoopFilter(non_zero_value, VPX_CODEC_OK);
132  ASSERT_EQ(VPX_CODEC_OK, skip_loop_filter.DecodeRemainingFrames());
133  skip_loop_filter.CheckMd5(false);
134}
135
136TEST(SkipLoopFilterTest, ShutOffLoopFilterSingleThread) {
137  const int non_zero_value = 1;
138  const int num_threads = 1;
139  SkipLoopFilterTest skip_loop_filter;
140  skip_loop_filter.Init(num_threads);
141  skip_loop_filter.SetSkipLoopFilter(non_zero_value, VPX_CODEC_OK);
142  ASSERT_EQ(VPX_CODEC_OK, skip_loop_filter.DecodeRemainingFrames());
143  skip_loop_filter.CheckMd5(false);
144}
145
146TEST(SkipLoopFilterTest, ShutOffLoopFilter8Threads) {
147  const int non_zero_value = 1;
148  const int num_threads = 8;
149  SkipLoopFilterTest skip_loop_filter;
150  skip_loop_filter.Init(num_threads);
151  skip_loop_filter.SetSkipLoopFilter(non_zero_value, VPX_CODEC_OK);
152  ASSERT_EQ(VPX_CODEC_OK, skip_loop_filter.DecodeRemainingFrames());
153  skip_loop_filter.CheckMd5(false);
154}
155
156TEST(SkipLoopFilterTest, WithLoopFilter) {
157  const int non_zero_value = 1;
158  const int num_threads = 0;
159  SkipLoopFilterTest skip_loop_filter;
160  skip_loop_filter.Init(num_threads);
161  skip_loop_filter.SetSkipLoopFilter(non_zero_value, VPX_CODEC_OK);
162  skip_loop_filter.SetSkipLoopFilter(0, VPX_CODEC_OK);
163  ASSERT_EQ(VPX_CODEC_OK, skip_loop_filter.DecodeRemainingFrames());
164  skip_loop_filter.CheckMd5(true);
165}
166
167TEST(SkipLoopFilterTest, ToggleLoopFilter) {
168  const int num_threads = 0;
169  SkipLoopFilterTest skip_loop_filter;
170  skip_loop_filter.Init(num_threads);
171
172  for (int i = 0; i < 10; ++i) {
173    skip_loop_filter.SetSkipLoopFilter(i % 2, VPX_CODEC_OK);
174    ASSERT_EQ(VPX_CODEC_OK, skip_loop_filter.DecodeOneFrame());
175  }
176  ASSERT_EQ(VPX_CODEC_OK, skip_loop_filter.DecodeRemainingFrames());
177  skip_loop_filter.CheckMd5(false);
178}
179
180}  // namespace
181