1 /*
2 * Copyright (c) 2013 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 <cstdio>
12 #include <cstdlib>
13 #include <memory>
14 #include <set>
15 #include <string>
16 #include <tuple>
17
18 #include "gtest/gtest.h"
19 #include "../tools_common.h"
20 #include "./vpx_config.h"
21 #include "test/codec_factory.h"
22 #include "test/decode_test_driver.h"
23 #include "test/ivf_video_source.h"
24 #include "test/md5_helper.h"
25 #include "test/test_vectors.h"
26 #include "test/util.h"
27 #if CONFIG_WEBM_IO
28 #include "test/webm_video_source.h"
29 #endif
30 #include "vpx_mem/vpx_mem.h"
31
32 namespace {
33
34 const int kThreads = 0;
35 const int kMtMode = 1;
36 const int kFileName = 2;
37
38 typedef std::tuple<int, int, const char *> DecodeParam;
39
40 class TestVectorTest : public ::libvpx_test::DecoderTest,
41 public ::libvpx_test::CodecTestWithParam<DecodeParam> {
42 protected:
TestVectorTest()43 TestVectorTest() : DecoderTest(GET_PARAM(0)), md5_file_(nullptr) {
44 #if CONFIG_VP9_DECODER
45 resize_clips_.insert(::libvpx_test::kVP9TestVectorsResize,
46 ::libvpx_test::kVP9TestVectorsResize +
47 ::libvpx_test::kNumVP9TestVectorsResize);
48 #endif
49 }
50
~TestVectorTest()51 ~TestVectorTest() override {
52 if (md5_file_) fclose(md5_file_);
53 }
54
OpenMD5File(const std::string & md5_file_name_)55 void OpenMD5File(const std::string &md5_file_name_) {
56 md5_file_ = libvpx_test::OpenTestDataFile(md5_file_name_);
57 ASSERT_NE(md5_file_, nullptr)
58 << "Md5 file open failed. Filename: " << md5_file_name_;
59 }
60
61 #if CONFIG_VP9_DECODER
PreDecodeFrameHook(const libvpx_test::CompressedVideoSource & video,libvpx_test::Decoder * decoder)62 void PreDecodeFrameHook(const libvpx_test::CompressedVideoSource &video,
63 libvpx_test::Decoder *decoder) override {
64 if (video.frame_number() == 0 && mt_mode_ >= 0) {
65 if (mt_mode_ == 1) {
66 decoder->Control(VP9D_SET_LOOP_FILTER_OPT, 1);
67 decoder->Control(VP9D_SET_ROW_MT, 0);
68 } else if (mt_mode_ == 2) {
69 decoder->Control(VP9D_SET_LOOP_FILTER_OPT, 0);
70 decoder->Control(VP9D_SET_ROW_MT, 1);
71 } else {
72 decoder->Control(VP9D_SET_LOOP_FILTER_OPT, 0);
73 decoder->Control(VP9D_SET_ROW_MT, 0);
74 }
75 }
76 }
77 #endif
78
DecompressedFrameHook(const vpx_image_t & img,const unsigned int frame_number)79 void DecompressedFrameHook(const vpx_image_t &img,
80 const unsigned int frame_number) override {
81 ASSERT_NE(md5_file_, nullptr);
82 char expected_md5[33];
83 char junk[128];
84
85 // Read correct md5 checksums.
86 const int res = fscanf(md5_file_, "%s %s", expected_md5, junk);
87 ASSERT_NE(res, EOF) << "Read md5 data failed";
88 expected_md5[32] = '\0';
89
90 ::libvpx_test::MD5 md5_res;
91 md5_res.Add(&img);
92 const char *actual_md5 = md5_res.Get();
93
94 // Check md5 match.
95 ASSERT_STREQ(expected_md5, actual_md5)
96 << "Md5 checksums don't match: frame number = " << frame_number;
97 }
98
99 #if CONFIG_VP9_DECODER
100 std::set<std::string> resize_clips_;
101 #endif
102 int mt_mode_;
103
104 private:
105 FILE *md5_file_;
106 };
107
108 // This test runs through the whole set of test vectors, and decodes them.
109 // The md5 checksums are computed for each frame in the video file. If md5
110 // checksums match the correct md5 data, then the test is passed. Otherwise,
111 // the test failed.
TEST_P(TestVectorTest,MD5Match)112 TEST_P(TestVectorTest, MD5Match) {
113 const DecodeParam input = GET_PARAM(1);
114 const std::string filename = std::get<kFileName>(input);
115 vpx_codec_flags_t flags = 0;
116 vpx_codec_dec_cfg_t cfg = vpx_codec_dec_cfg_t();
117 char str[256];
118
119 cfg.threads = std::get<kThreads>(input);
120 mt_mode_ = std::get<kMtMode>(input);
121 snprintf(str, sizeof(str) / sizeof(str[0]) - 1,
122 "file: %s threads: %d MT mode: %d", filename.c_str(), cfg.threads,
123 mt_mode_);
124 SCOPED_TRACE(str);
125
126 // Open compressed video file.
127 std::unique_ptr<libvpx_test::CompressedVideoSource> video;
128 if (filename.substr(filename.length() - 3, 3) == "ivf") {
129 video.reset(new libvpx_test::IVFVideoSource(filename));
130 } else if (filename.substr(filename.length() - 4, 4) == "webm") {
131 #if CONFIG_WEBM_IO
132 video.reset(new libvpx_test::WebMVideoSource(filename));
133 #else
134 fprintf(stderr, "WebM IO is disabled, skipping test vector %s\n",
135 filename.c_str());
136 return;
137 #endif
138 }
139 ASSERT_NE(video.get(), nullptr);
140 video->Init();
141
142 // Construct md5 file name.
143 const std::string md5_filename = filename + ".md5";
144 OpenMD5File(md5_filename);
145
146 // Set decode config and flags.
147 set_cfg(cfg);
148 set_flags(flags);
149
150 // Decode frame, and check the md5 matching.
151 ASSERT_NO_FATAL_FAILURE(RunLoop(video.get(), cfg));
152 }
153
154 #if CONFIG_VP8_DECODER
155 VP8_INSTANTIATE_TEST_SUITE(
156 TestVectorTest,
157 ::testing::Combine(
158 ::testing::Values(1), // Single thread.
159 ::testing::Values(-1), // LPF opt and Row MT is not applicable
160 ::testing::ValuesIn(libvpx_test::kVP8TestVectors,
161 libvpx_test::kVP8TestVectors +
162 libvpx_test::kNumVP8TestVectors)));
163
164 // Test VP8 decode in with different numbers of threads.
165 INSTANTIATE_TEST_SUITE_P(
166 VP8MultiThreaded, TestVectorTest,
167 ::testing::Combine(
168 ::testing::Values(
169 static_cast<const libvpx_test::CodecFactory *>(&libvpx_test::kVP8)),
170 ::testing::Combine(
171 ::testing::Range(2, 9), // With 2 ~ 8 threads.
172 ::testing::Values(-1), // LPF opt and Row MT is not applicable
173 ::testing::ValuesIn(libvpx_test::kVP8TestVectors,
174 libvpx_test::kVP8TestVectors +
175 libvpx_test::kNumVP8TestVectors))));
176
177 #endif // CONFIG_VP8_DECODER
178
179 #if CONFIG_VP9_DECODER
180 VP9_INSTANTIATE_TEST_SUITE(
181 TestVectorTest,
182 ::testing::Combine(
183 ::testing::Values(1), // Single thread.
184 ::testing::Values(-1), // LPF opt and Row MT is not applicable
185 ::testing::ValuesIn(libvpx_test::kVP9TestVectors,
186 libvpx_test::kVP9TestVectors +
187 libvpx_test::kNumVP9TestVectors)));
188
189 INSTANTIATE_TEST_SUITE_P(
190 VP9MultiThreaded, TestVectorTest,
191 ::testing::Combine(
192 ::testing::Values(
193 static_cast<const libvpx_test::CodecFactory *>(&libvpx_test::kVP9)),
194 ::testing::Combine(
195 ::testing::Range(2, 9), // With 2 ~ 8 threads.
196 ::testing::Range(0, 3), // With multi threads modes 0 ~ 2
197 // 0: LPF opt and Row MT disabled
198 // 1: LPF opt enabled
199 // 2: Row MT enabled
200 ::testing::ValuesIn(libvpx_test::kVP9TestVectors,
201 libvpx_test::kVP9TestVectors +
202 libvpx_test::kNumVP9TestVectors))));
203 #endif
204 } // namespace
205