1 /*
2 * Copyright (c) 2018 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 <tuple>
12
13 #include "gtest/gtest.h"
14
15 #include "test/codec_factory.h"
16 #include "test/encode_test_driver.h"
17 #include "test/util.h"
18 #include "test/i420_video_source.h"
19 #include "vpx_config.h"
20 #include "vpx_mem/vpx_mem.h"
21
22 namespace {
23
24 class DecodeCorruptedFrameTest
25 : public ::libvpx_test::EncoderTest,
26 public ::testing::TestWithParam<
27 std::tuple<const libvpx_test::CodecFactory *> > {
28 public:
DecodeCorruptedFrameTest()29 DecodeCorruptedFrameTest() : EncoderTest(GET_PARAM(0)) {}
30
31 protected:
32 ~DecodeCorruptedFrameTest() override = default;
33
SetUp()34 void SetUp() override {
35 InitializeConfig();
36 SetMode(::libvpx_test::kRealTime);
37 cfg_.g_lag_in_frames = 0;
38 cfg_.rc_end_usage = VPX_CBR;
39 cfg_.rc_buf_sz = 1000;
40 cfg_.rc_buf_initial_sz = 500;
41 cfg_.rc_buf_optimal_sz = 600;
42
43 // Set small key frame distance such that we insert more key frames.
44 cfg_.kf_max_dist = 3;
45 dec_cfg_.threads = 1;
46 }
47
PreEncodeFrameHook(::libvpx_test::VideoSource * video,::libvpx_test::Encoder * encoder)48 void PreEncodeFrameHook(::libvpx_test::VideoSource *video,
49 ::libvpx_test::Encoder *encoder) override {
50 if (video->frame() == 0) encoder->Control(VP8E_SET_CPUUSED, 7);
51 }
52
MismatchHook(const vpx_image_t *,const vpx_image_t *)53 void MismatchHook(const vpx_image_t * /*img1*/,
54 const vpx_image_t * /*img2*/) override {}
55
MutateEncoderOutputHook(const vpx_codec_cx_pkt_t * pkt)56 const vpx_codec_cx_pkt_t *MutateEncoderOutputHook(
57 const vpx_codec_cx_pkt_t *pkt) override {
58 // Don't edit frame packet on key frame.
59 if (pkt->data.frame.flags & VPX_FRAME_IS_KEY) return pkt;
60 if (pkt->kind != VPX_CODEC_CX_FRAME_PKT) return pkt;
61
62 memcpy(&modified_pkt_, pkt, sizeof(*pkt));
63
64 // Halve the size so it's corrupted to decoder.
65 modified_pkt_.data.frame.sz = modified_pkt_.data.frame.sz / 2;
66
67 return &modified_pkt_;
68 }
69
HandleDecodeResult(const vpx_codec_err_t res_dec,const libvpx_test::VideoSource &,libvpx_test::Decoder * decoder)70 bool HandleDecodeResult(const vpx_codec_err_t res_dec,
71 const libvpx_test::VideoSource & /*video*/,
72 libvpx_test::Decoder *decoder) override {
73 EXPECT_NE(res_dec, VPX_CODEC_MEM_ERROR) << decoder->DecodeError();
74 return VPX_CODEC_MEM_ERROR != res_dec;
75 }
76
77 vpx_codec_cx_pkt_t modified_pkt_;
78 };
79
TEST_P(DecodeCorruptedFrameTest,DecodeCorruptedFrame)80 TEST_P(DecodeCorruptedFrameTest, DecodeCorruptedFrame) {
81 cfg_.rc_target_bitrate = 200;
82 cfg_.g_error_resilient = 0;
83
84 ::libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
85 30, 1, 0, 300);
86
87 ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
88 }
89
90 #if CONFIG_VP9
91 INSTANTIATE_TEST_SUITE_P(
92 VP9, DecodeCorruptedFrameTest,
93 ::testing::Values(
94 static_cast<const libvpx_test::CodecFactory *>(&libvpx_test::kVP9)));
95 #endif // CONFIG_VP9
96
97 #if CONFIG_VP8
98 INSTANTIATE_TEST_SUITE_P(
99 VP8, DecodeCorruptedFrameTest,
100 ::testing::Values(
101 static_cast<const libvpx_test::CodecFactory *>(&libvpx_test::kVP8)));
102 #endif // CONFIG_VP8
103
104 } // namespace
105