]> granicus.if.org Git - libvpx/blob - test/test_vector_test.cc
Merge "endian_inl.h: fix mips32 android build"
[libvpx] / test / test_vector_test.cc
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 <string>
14 #include "third_party/googletest/src/include/gtest/gtest.h"
15 #include "../tools_common.h"
16 #include "./vpx_config.h"
17 #include "test/codec_factory.h"
18 #include "test/decode_test_driver.h"
19 #include "test/ivf_video_source.h"
20 #include "test/md5_helper.h"
21 #include "test/test_vectors.h"
22 #include "test/util.h"
23 #if CONFIG_WEBM_IO
24 #include "test/webm_video_source.h"
25 #endif
26 #include "vpx_mem/vpx_mem.h"
27
28 namespace {
29
30 enum DecodeMode {
31   kSerialMode,
32   kFrameParallelMode
33 };
34
35 const int kDecodeMode = 0;
36 const int kThreads = 1;
37 const int kFileName = 2;
38
39 typedef std::tr1::tuple<int, int, const char*> DecodeParam;
40
41 class TestVectorTest : public ::libvpx_test::DecoderTest,
42     public ::libvpx_test::CodecTestWithParam<DecodeParam> {
43  protected:
44   TestVectorTest()
45       : DecoderTest(GET_PARAM(0)),
46         md5_file_(NULL) {
47   }
48
49   virtual ~TestVectorTest() {
50     if (md5_file_)
51       fclose(md5_file_);
52   }
53
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
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  private:
81   FILE *md5_file_;
82 };
83
84 // This test runs through the whole set of test vectors, and decodes them.
85 // The md5 checksums are computed for each frame in the video file. If md5
86 // checksums match the correct md5 data, then the test is passed. Otherwise,
87 // the test failed.
88 TEST_P(TestVectorTest, MD5Match) {
89   const DecodeParam input = GET_PARAM(1);
90   const std::string filename = std::tr1::get<kFileName>(input);
91   const int threads = std::tr1::get<kThreads>(input);
92   const int mode = std::tr1::get<kDecodeMode>(input);
93   libvpx_test::CompressedVideoSource *video = NULL;
94   vpx_codec_flags_t flags = 0;
95   vpx_codec_dec_cfg_t cfg = {0};
96   char str[256];
97
98   if (mode == kFrameParallelMode) {
99     flags |= VPX_CODEC_USE_FRAME_THREADING;
100   }
101
102   cfg.threads = threads;
103
104   snprintf(str, sizeof(str) / sizeof(str[0]) - 1,
105            "file: %s  mode: %s threads: %d",
106            filename.c_str(), mode == 0 ? "Serial" : "Parallel", threads);
107   SCOPED_TRACE(str);
108
109   // Open compressed video file.
110   if (filename.substr(filename.length() - 3, 3) == "ivf") {
111     video = new libvpx_test::IVFVideoSource(filename);
112   } else if (filename.substr(filename.length() - 4, 4) == "webm") {
113 #if CONFIG_WEBM_IO
114     video = new libvpx_test::WebMVideoSource(filename);
115 #else
116     fprintf(stderr, "WebM IO is disabled, skipping test vector %s\n",
117             filename.c_str());
118     return;
119 #endif
120   }
121   video->Init();
122
123   // Construct md5 file name.
124   const std::string md5_filename = filename + ".md5";
125   OpenMD5File(md5_filename);
126
127   // Set decode config and flags.
128   set_cfg(cfg);
129   set_flags(flags);
130
131   // Decode frame, and check the md5 matching.
132   ASSERT_NO_FATAL_FAILURE(RunLoop(video, cfg));
133   delete video;
134 }
135
136 // Test VP8 decode in serial mode with single thread.
137 // NOTE: VP8 only support serial mode.
138 #if CONFIG_VP8_DECODER
139 VP8_INSTANTIATE_TEST_CASE(
140     TestVectorTest,
141     ::testing::Combine(
142         ::testing::Values(0),  // Serial Mode.
143         ::testing::Values(1),  // Single thread.
144         ::testing::ValuesIn(libvpx_test::kVP8TestVectors,
145                             libvpx_test::kVP8TestVectors +
146                                 libvpx_test::kNumVP8TestVectors)));
147 #endif  // CONFIG_VP8_DECODER
148
149 // Test VP9 decode in serial mode with single thread.
150 #if CONFIG_VP9_DECODER
151 VP9_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::kVP9TestVectors,
157                             libvpx_test::kVP9TestVectors +
158                                 libvpx_test::kNumVP9TestVectors)));
159
160 // Test VP9 decode in frame parallel mode with different number of threads.
161 INSTANTIATE_TEST_CASE_P(
162     VP9MultiThreadedFrameParallel, TestVectorTest,
163     ::testing::Combine(
164         ::testing::Values(
165             static_cast<const libvpx_test::CodecFactory *>(&libvpx_test::kVP9)),
166         ::testing::Combine(
167             ::testing::Values(1),        // Frame Parallel mode.
168             ::testing::Range(2, 9),      // With 2 ~ 8 threads.
169             ::testing::ValuesIn(libvpx_test::kVP9TestVectors,
170                                 libvpx_test::kVP9TestVectors +
171                                     libvpx_test::kNumVP9TestVectors))));
172 #endif
173 }  // namespace