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