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