1/*
2 *  Copyright (c) 2014 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 <vector>
15#include "third_party/googletest/src/include/gtest/gtest.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/util.h"
21#if CONFIG_WEBM_IO
22#include "test/webm_video_source.h"
23#endif
24#include "vpx_mem/vpx_mem.h"
25
26namespace {
27
28struct DecodeParam {
29  int threads;
30  const char *filename;
31};
32
33std::ostream &operator<<(std::ostream &os, const DecodeParam &dp) {
34  return os << "threads: " << dp.threads << " file: " << dp.filename;
35}
36
37class InvalidFileTest
38    : public ::libvpx_test::DecoderTest,
39      public ::libvpx_test::CodecTestWithParam<DecodeParam> {
40 protected:
41  InvalidFileTest() : DecoderTest(GET_PARAM(0)), res_file_(NULL) {}
42
43  virtual ~InvalidFileTest() {
44    if (res_file_ != NULL)
45      fclose(res_file_);
46  }
47
48  void OpenResFile(const std::string &res_file_name_) {
49    res_file_ = libvpx_test::OpenTestDataFile(res_file_name_);
50    ASSERT_TRUE(res_file_ != NULL) << "Result file open failed. Filename: "
51        << res_file_name_;
52  }
53
54  virtual bool HandleDecodeResult(
55      const vpx_codec_err_t res_dec,
56      const libvpx_test::CompressedVideoSource &video,
57      libvpx_test::Decoder *decoder) {
58    EXPECT_TRUE(res_file_ != NULL);
59    int expected_res_dec;
60
61    // Read integer result.
62    const int res = fscanf(res_file_, "%d", &expected_res_dec);
63    EXPECT_NE(res, EOF) << "Read result data failed";
64
65    // Check results match.
66    EXPECT_EQ(expected_res_dec, res_dec)
67        << "Results don't match: frame number = " << video.frame_number()
68        << ". (" << decoder->DecodeError() << ")";
69
70    return !HasFailure();
71  }
72
73  void RunTest() {
74    const DecodeParam input = GET_PARAM(1);
75    libvpx_test::CompressedVideoSource *video = NULL;
76    vpx_codec_dec_cfg_t cfg = vpx_codec_dec_cfg_t();
77    cfg.threads = input.threads;
78    const std::string filename = input.filename;
79
80    // Open compressed video file.
81    if (filename.substr(filename.length() - 3, 3) == "ivf") {
82      video = new libvpx_test::IVFVideoSource(filename);
83    } else if (filename.substr(filename.length() - 4, 4) == "webm") {
84#if CONFIG_WEBM_IO
85      video = new libvpx_test::WebMVideoSource(filename);
86#else
87      fprintf(stderr, "WebM IO is disabled, skipping test vector %s\n",
88              filename.c_str());
89      return;
90#endif
91    }
92    video->Init();
93
94    // Construct result file name. The file holds a list of expected integer
95    // results, one for each decoded frame.  Any result that doesn't match
96    // the files list will cause a test failure.
97    const std::string res_filename = filename + ".res";
98    OpenResFile(res_filename);
99
100    // Decode frame, and check the md5 matching.
101    ASSERT_NO_FATAL_FAILURE(RunLoop(video, cfg));
102    delete video;
103  }
104
105 private:
106  FILE *res_file_;
107};
108
109TEST_P(InvalidFileTest, ReturnCode) {
110  RunTest();
111}
112
113const DecodeParam kVP9InvalidFileTests[] = {
114  {1, "invalid-vp90-02-v2.webm"},
115  {1, "invalid-vp90-2-00-quantizer-00.webm.ivf.s5861_r01-05_b6-.v2.ivf"},
116  {1, "invalid-vp90-03-v3.webm"},
117  {1, "invalid-vp90-2-00-quantizer-11.webm.ivf.s52984_r01-05_b6-.ivf"},
118  {1, "invalid-vp90-2-00-quantizer-11.webm.ivf.s52984_r01-05_b6-z.ivf"},
119  {1, "invalid-vp90-2-12-droppable_1.ivf.s3676_r01-05_b6-.ivf"},
120  {1, "invalid-vp90-2-05-resize.ivf.s59293_r01-05_b6-.ivf"},
121  {1, "invalid-vp90-2-09-subpixel-00.ivf.s20492_r01-05_b6-.v2.ivf"},
122  {1, "invalid-vp91-2-mixedrefcsp-444to420.ivf"},
123  {1, "invalid-vp90-2-12-droppable_1.ivf.s73804_r01-05_b6-.ivf"},
124};
125
126VP9_INSTANTIATE_TEST_CASE(InvalidFileTest,
127                          ::testing::ValuesIn(kVP9InvalidFileTests));
128
129// This class will include test vectors that are expected to fail
130// peek. However they are still expected to have no fatal failures.
131class InvalidFileInvalidPeekTest : public InvalidFileTest {
132 protected:
133  InvalidFileInvalidPeekTest() : InvalidFileTest() {}
134  virtual void HandlePeekResult(libvpx_test::Decoder *const /*decoder*/,
135                                libvpx_test::CompressedVideoSource* /*video*/,
136                                const vpx_codec_err_t /*res_peek*/) {}
137};
138
139TEST_P(InvalidFileInvalidPeekTest, ReturnCode) {
140  RunTest();
141}
142
143const DecodeParam kVP9InvalidFileInvalidPeekTests[] = {
144  {1, "invalid-vp90-01-v2.webm"},
145};
146
147VP9_INSTANTIATE_TEST_CASE(InvalidFileInvalidPeekTest,
148                          ::testing::ValuesIn(kVP9InvalidFileInvalidPeekTests));
149
150const DecodeParam kMultiThreadedVP9InvalidFileTests[] = {
151  {4, "invalid-vp90-2-08-tile_1x4_frame_parallel_all_key.webm"},
152  {4, "invalid-"
153      "vp90-2-08-tile_1x2_frame_parallel.webm.ivf.s47039_r01-05_b6-.ivf"},
154  {2, "invalid-vp90-2-09-aq2.webm.ivf.s3984_r01-05_b6-.v2.ivf"},
155  {4, "invalid-vp90-2-09-subpixel-00.ivf.s19552_r01-05_b6-.v2.ivf"},
156};
157
158INSTANTIATE_TEST_CASE_P(
159    VP9MultiThreaded, InvalidFileTest,
160    ::testing::Combine(
161        ::testing::Values(
162            static_cast<const libvpx_test::CodecFactory*>(&libvpx_test::kVP9)),
163        ::testing::ValuesIn(kMultiThreadedVP9InvalidFileTests)));
164}  // namespace
165