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 "./vpx_config.h"
16#include "test/codec_factory.h"
17#include "test/decode_test_driver.h"
18#include "test/ivf_video_source.h"
19#include "test/md5_helper.h"
20#include "test/test_vectors.h"
21#include "test/util.h"
22#if CONFIG_WEBM_IO
23#include "test/webm_video_source.h"
24#endif
25#include "vpx_mem/vpx_mem.h"
26
27namespace {
28
29class TestVectorTest : public ::libvpx_test::DecoderTest,
30    public ::libvpx_test::CodecTestWithParam<const char*> {
31 protected:
32  TestVectorTest() : DecoderTest(GET_PARAM(0)), md5_file_(NULL) {}
33
34  virtual ~TestVectorTest() {
35    if (md5_file_)
36      fclose(md5_file_);
37  }
38
39  void OpenMD5File(const std::string& md5_file_name_) {
40    md5_file_ = libvpx_test::OpenTestDataFile(md5_file_name_);
41    ASSERT_TRUE(md5_file_ != NULL) << "Md5 file open failed. Filename: "
42        << md5_file_name_;
43  }
44
45  virtual void DecompressedFrameHook(const vpx_image_t& img,
46                                     const unsigned int frame_number) {
47    ASSERT_TRUE(md5_file_ != NULL);
48    char expected_md5[33];
49    char junk[128];
50
51    // Read correct md5 checksums.
52    const int res = fscanf(md5_file_, "%s  %s", expected_md5, junk);
53    ASSERT_NE(res, EOF) << "Read md5 data failed";
54    expected_md5[32] = '\0';
55
56    ::libvpx_test::MD5 md5_res;
57    md5_res.Add(&img);
58    const char *actual_md5 = md5_res.Get();
59
60    // Check md5 match.
61    ASSERT_STREQ(expected_md5, actual_md5)
62        << "Md5 checksums don't match: frame number = " << frame_number;
63  }
64
65 private:
66  FILE *md5_file_;
67};
68
69// This test runs through the whole set of test vectors, and decodes them.
70// The md5 checksums are computed for each frame in the video file. If md5
71// checksums match the correct md5 data, then the test is passed. Otherwise,
72// the test failed.
73TEST_P(TestVectorTest, MD5Match) {
74  const std::string filename = GET_PARAM(1);
75  libvpx_test::CompressedVideoSource *video = NULL;
76
77  // Open compressed video file.
78  if (filename.substr(filename.length() - 3, 3) == "ivf") {
79    video = new libvpx_test::IVFVideoSource(filename);
80  } else if (filename.substr(filename.length() - 4, 4) == "webm") {
81#if CONFIG_WEBM_IO
82    video = new libvpx_test::WebMVideoSource(filename);
83#else
84    fprintf(stderr, "WebM IO is disabled, skipping test vector %s\n",
85            filename.c_str());
86    return;
87#endif
88  }
89  video->Init();
90
91  // Construct md5 file name.
92  const std::string md5_filename = filename + ".md5";
93  OpenMD5File(md5_filename);
94
95  // Decode frame, and check the md5 matching.
96  ASSERT_NO_FATAL_FAILURE(RunLoop(video));
97  delete video;
98}
99
100VP8_INSTANTIATE_TEST_CASE(TestVectorTest,
101                          ::testing::ValuesIn(libvpx_test::kVP8TestVectors,
102                                              libvpx_test::kVP8TestVectors +
103                                              libvpx_test::kNumVP8TestVectors));
104VP9_INSTANTIATE_TEST_CASE(TestVectorTest,
105                          ::testing::ValuesIn(libvpx_test::kVP9TestVectors,
106                                              libvpx_test::kVP9TestVectors +
107                                              libvpx_test::kNumVP9TestVectors));
108
109}  // namespace
110