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 <memory>
14 #include <string>
15 #include <vector>
16 #include "third_party/googletest/src/include/gtest/gtest.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/util.h"
22 #if CONFIG_WEBM_IO
23 #include "test/webm_video_source.h"
24 #endif
25 #include "vpx_mem/vpx_mem.h"
26 
27 namespace {
28 
29 struct DecodeParam {
30   int threads;
31   const char *filename;
32 };
33 
operator <<(std::ostream & os,const DecodeParam & dp)34 std::ostream &operator<<(std::ostream &os, const DecodeParam &dp) {
35   return os << "threads: " << dp.threads << " file: " << dp.filename;
36 }
37 
38 class InvalidFileTest : public ::libvpx_test::DecoderTest,
39                         public ::libvpx_test::CodecTestWithParam<DecodeParam> {
40  protected:
InvalidFileTest()41   InvalidFileTest() : DecoderTest(GET_PARAM(0)), res_file_(NULL) {}
42 
~InvalidFileTest()43   virtual ~InvalidFileTest() {
44     if (res_file_ != NULL) fclose(res_file_);
45   }
46 
OpenResFile(const std::string & res_file_name_)47   void OpenResFile(const std::string &res_file_name_) {
48     res_file_ = libvpx_test::OpenTestDataFile(res_file_name_);
49     ASSERT_TRUE(res_file_ != NULL)
50         << "Result file open failed. Filename: " << res_file_name_;
51   }
52 
HandleDecodeResult(const vpx_codec_err_t res_dec,const libvpx_test::CompressedVideoSource & video,libvpx_test::Decoder * decoder)53   virtual bool HandleDecodeResult(
54       const vpx_codec_err_t res_dec,
55       const libvpx_test::CompressedVideoSource &video,
56       libvpx_test::Decoder *decoder) {
57     EXPECT_TRUE(res_file_ != NULL);
58     int expected_res_dec;
59 
60     // Read integer result.
61     const int res = fscanf(res_file_, "%d", &expected_res_dec);
62     EXPECT_NE(res, EOF) << "Read result data failed";
63 
64     // Check results match.
65     const DecodeParam input = GET_PARAM(1);
66     if (input.threads > 1) {
67       // The serial decode check is too strict for tile-threaded decoding as
68       // there is no guarantee on the decode order nor which specific error
69       // will take precedence. Currently a tile-level error is not forwarded so
70       // the frame will simply be marked corrupt.
71       EXPECT_TRUE(res_dec == expected_res_dec ||
72                   res_dec == VPX_CODEC_CORRUPT_FRAME)
73           << "Results don't match: frame number = " << video.frame_number()
74           << ". (" << decoder->DecodeError()
75           << "). Expected: " << expected_res_dec << " or "
76           << VPX_CODEC_CORRUPT_FRAME;
77     } else {
78       EXPECT_EQ(expected_res_dec, res_dec)
79           << "Results don't match: frame number = " << video.frame_number()
80           << ". (" << decoder->DecodeError() << ")";
81     }
82 
83     return !HasFailure();
84   }
85 
RunTest()86   void RunTest() {
87     const DecodeParam input = GET_PARAM(1);
88     vpx_codec_dec_cfg_t cfg = vpx_codec_dec_cfg_t();
89     cfg.threads = input.threads;
90     const std::string filename = input.filename;
91 
92     // Open compressed video file.
93     std::unique_ptr<libvpx_test::CompressedVideoSource> video;
94     if (filename.substr(filename.length() - 3, 3) == "ivf") {
95       video.reset(new libvpx_test::IVFVideoSource(filename));
96     } else if (filename.substr(filename.length() - 4, 4) == "webm") {
97 #if CONFIG_WEBM_IO
98       video.reset(new libvpx_test::WebMVideoSource(filename));
99 #else
100       fprintf(stderr, "WebM IO is disabled, skipping test vector %s\n",
101               filename.c_str());
102       return;
103 #endif
104     }
105     ASSERT_TRUE(video.get() != NULL);
106     video->Init();
107 
108     // Construct result file name. The file holds a list of expected integer
109     // results, one for each decoded frame.  Any result that doesn't match
110     // the files list will cause a test failure.
111     const std::string res_filename = filename + ".res";
112     OpenResFile(res_filename);
113 
114     // Decode frame, and check the md5 matching.
115     ASSERT_NO_FATAL_FAILURE(RunLoop(video.get(), cfg));
116   }
117 
118  private:
119   FILE *res_file_;
120 };
121 
TEST_P(InvalidFileTest,ReturnCode)122 TEST_P(InvalidFileTest, ReturnCode) { RunTest(); }
123 
124 #if CONFIG_VP8_DECODER
125 const DecodeParam kVP8InvalidFileTests[] = {
126   { 1, "invalid-bug-1443.ivf" },
127   { 1, "invalid-token-partition.ivf" },
128   { 1, "invalid-vp80-00-comprehensive-s17661_r01-05_b6-.ivf" },
129 };
130 
131 VP8_INSTANTIATE_TEST_CASE(InvalidFileTest,
132                           ::testing::ValuesIn(kVP8InvalidFileTests));
133 #endif  // CONFIG_VP8_DECODER
134 
135 #if CONFIG_VP9_DECODER
136 const DecodeParam kVP9InvalidFileTests[] = {
137   { 1, "invalid-vp90-02-v2.webm" },
138 #if CONFIG_VP9_HIGHBITDEPTH
139   { 1, "invalid-vp90-2-00-quantizer-00.webm.ivf.s5861_r01-05_b6-.v2.ivf" },
140   { 1,
141     "invalid-vp90-2-21-resize_inter_320x180_5_3-4.webm.ivf.s45551_r01-05_b6-."
142     "ivf" },
143 #endif
144   { 1, "invalid-vp90-03-v3.webm" },
145   { 1, "invalid-vp90-2-00-quantizer-11.webm.ivf.s52984_r01-05_b6-.ivf" },
146   { 1, "invalid-vp90-2-00-quantizer-11.webm.ivf.s52984_r01-05_b6-z.ivf" },
147 // This file will cause a large allocation which is expected to fail in 32-bit
148 // environments. Test x86 for coverage purposes as the allocation failure will
149 // be in platform agnostic code.
150 #if VPX_ARCH_X86
151   { 1, "invalid-vp90-2-00-quantizer-63.ivf.kf_65527x61446.ivf" },
152 #endif
153   { 1, "invalid-vp90-2-12-droppable_1.ivf.s3676_r01-05_b6-.ivf" },
154   { 1, "invalid-vp90-2-05-resize.ivf.s59293_r01-05_b6-.ivf" },
155   { 1, "invalid-vp90-2-09-subpixel-00.ivf.s20492_r01-05_b6-.v2.ivf" },
156   { 1, "invalid-vp91-2-mixedrefcsp-444to420.ivf" },
157   { 1, "invalid-vp90-2-12-droppable_1.ivf.s73804_r01-05_b6-.ivf" },
158   { 1, "invalid-vp90-2-03-size-224x196.webm.ivf.s44156_r01-05_b6-.ivf" },
159   { 1, "invalid-vp90-2-03-size-202x210.webm.ivf.s113306_r01-05_b6-.ivf" },
160   { 1,
161     "invalid-vp90-2-10-show-existing-frame.webm.ivf.s180315_r01-05_b6-.ivf" },
162   { 1, "invalid-crbug-667044.webm" },
163 };
164 
165 VP9_INSTANTIATE_TEST_CASE(InvalidFileTest,
166                           ::testing::ValuesIn(kVP9InvalidFileTests));
167 #endif  // CONFIG_VP9_DECODER
168 
169 // This class will include test vectors that are expected to fail
170 // peek. However they are still expected to have no fatal failures.
171 class InvalidFileInvalidPeekTest : public InvalidFileTest {
172  protected:
InvalidFileInvalidPeekTest()173   InvalidFileInvalidPeekTest() : InvalidFileTest() {}
HandlePeekResult(libvpx_test::Decoder * const,libvpx_test::CompressedVideoSource *,const vpx_codec_err_t)174   virtual void HandlePeekResult(libvpx_test::Decoder *const /*decoder*/,
175                                 libvpx_test::CompressedVideoSource * /*video*/,
176                                 const vpx_codec_err_t /*res_peek*/) {}
177 };
178 
TEST_P(InvalidFileInvalidPeekTest,ReturnCode)179 TEST_P(InvalidFileInvalidPeekTest, ReturnCode) { RunTest(); }
180 
181 #if CONFIG_VP8_DECODER
182 const DecodeParam kVP8InvalidPeekTests[] = {
183   { 1, "invalid-vp80-00-comprehensive-018.ivf.2kf_0x6.ivf" },
184 };
185 
186 VP8_INSTANTIATE_TEST_CASE(InvalidFileInvalidPeekTest,
187                           ::testing::ValuesIn(kVP8InvalidPeekTests));
188 #endif  // CONFIG_VP8_DECODER
189 
190 #if CONFIG_VP9_DECODER
191 const DecodeParam kVP9InvalidFileInvalidPeekTests[] = {
192   { 1, "invalid-vp90-01-v3.webm" },
193 };
194 
195 VP9_INSTANTIATE_TEST_CASE(InvalidFileInvalidPeekTest,
196                           ::testing::ValuesIn(kVP9InvalidFileInvalidPeekTests));
197 
198 const DecodeParam kMultiThreadedVP9InvalidFileTests[] = {
199   { 4, "invalid-vp90-2-08-tile_1x4_frame_parallel_all_key.webm" },
200   { 4,
201     "invalid-"
202     "vp90-2-08-tile_1x2_frame_parallel.webm.ivf.s47039_r01-05_b6-.ivf" },
203   { 4,
204     "invalid-vp90-2-08-tile_1x8_frame_parallel.webm.ivf.s288_r01-05_b6-.ivf" },
205   { 2, "invalid-vp90-2-09-aq2.webm.ivf.s3984_r01-05_b6-.v2.ivf" },
206   { 4, "invalid-vp90-2-09-subpixel-00.ivf.s19552_r01-05_b6-.v2.ivf" },
207   { 2, "invalid-crbug-629481.webm" },
208   { 3, "invalid-crbug-1558.ivf" },
209   { 4, "invalid-crbug-1562.ivf" },
210 };
211 
212 INSTANTIATE_TEST_CASE_P(
213     VP9MultiThreaded, InvalidFileTest,
214     ::testing::Combine(
215         ::testing::Values(
216             static_cast<const libvpx_test::CodecFactory *>(&libvpx_test::kVP9)),
217         ::testing::ValuesIn(kMultiThreadedVP9InvalidFileTests)));
218 #endif  // CONFIG_VP9_DECODER
219 }  // namespace
220