1 /*
2  *  Copyright (c) 2012 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 #include <climits>
11 #include <tuple>
12 
13 #include "third_party/googletest/src/include/gtest/gtest.h"
14 #include "test/codec_factory.h"
15 #include "test/encode_test_driver.h"
16 #include "test/i420_video_source.h"
17 #include "test/util.h"
18 
19 namespace {
20 
21 const int kTestMode = 0;
22 
23 typedef std::tuple<libvpx_test::TestMode, int> SuperframeTestParam;
24 
25 class SuperframeTest
26     : public ::libvpx_test::EncoderTest,
27       public ::libvpx_test::CodecTestWithParam<SuperframeTestParam> {
28  protected:
SuperframeTest()29   SuperframeTest()
30       : EncoderTest(GET_PARAM(0)), modified_buf_(NULL), last_sf_pts_(0) {}
~SuperframeTest()31   virtual ~SuperframeTest() {}
32 
SetUp()33   virtual void SetUp() {
34     InitializeConfig();
35     const SuperframeTestParam input = GET_PARAM(1);
36     const libvpx_test::TestMode mode = std::get<kTestMode>(input);
37     SetMode(mode);
38     sf_count_ = 0;
39     sf_count_max_ = INT_MAX;
40   }
41 
TearDown()42   virtual void TearDown() { delete[] modified_buf_; }
43 
PreEncodeFrameHook(libvpx_test::VideoSource * video,libvpx_test::Encoder * encoder)44   virtual void PreEncodeFrameHook(libvpx_test::VideoSource *video,
45                                   libvpx_test::Encoder *encoder) {
46     if (video->frame() == 0) {
47       encoder->Control(VP8E_SET_ENABLEAUTOALTREF, 1);
48     }
49   }
50 
MutateEncoderOutputHook(const vpx_codec_cx_pkt_t * pkt)51   virtual const vpx_codec_cx_pkt_t *MutateEncoderOutputHook(
52       const vpx_codec_cx_pkt_t *pkt) {
53     if (pkt->kind != VPX_CODEC_CX_FRAME_PKT) return pkt;
54 
55     const uint8_t *buffer = reinterpret_cast<uint8_t *>(pkt->data.frame.buf);
56     const uint8_t marker = buffer[pkt->data.frame.sz - 1];
57     const int frames = (marker & 0x7) + 1;
58     const int mag = ((marker >> 3) & 3) + 1;
59     const unsigned int index_sz = 2 + mag * frames;
60     if ((marker & 0xe0) == 0xc0 && pkt->data.frame.sz >= index_sz &&
61         buffer[pkt->data.frame.sz - index_sz] == marker) {
62       // frame is a superframe. strip off the index.
63       if (modified_buf_) delete[] modified_buf_;
64       modified_buf_ = new uint8_t[pkt->data.frame.sz - index_sz];
65       memcpy(modified_buf_, pkt->data.frame.buf, pkt->data.frame.sz - index_sz);
66       modified_pkt_ = *pkt;
67       modified_pkt_.data.frame.buf = modified_buf_;
68       modified_pkt_.data.frame.sz -= index_sz;
69 
70       sf_count_++;
71       last_sf_pts_ = pkt->data.frame.pts;
72       return &modified_pkt_;
73     }
74 
75     // Make sure we do a few frames after the last SF
76     abort_ |=
77         sf_count_ > sf_count_max_ && pkt->data.frame.pts - last_sf_pts_ >= 5;
78     return pkt;
79   }
80 
81   int sf_count_;
82   int sf_count_max_;
83   vpx_codec_cx_pkt_t modified_pkt_;
84   uint8_t *modified_buf_;
85   vpx_codec_pts_t last_sf_pts_;
86 };
87 
TEST_P(SuperframeTest,TestSuperframeIndexIsOptional)88 TEST_P(SuperframeTest, TestSuperframeIndexIsOptional) {
89   sf_count_max_ = 0;  // early exit on successful test.
90   cfg_.g_lag_in_frames = 25;
91 
92   ::libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
93                                        30, 1, 0, 40);
94   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
95   EXPECT_EQ(sf_count_, 1);
96 }
97 
98 VP9_INSTANTIATE_TEST_CASE(
99     SuperframeTest,
100     ::testing::Combine(::testing::Values(::libvpx_test::kTwoPassGood),
101                        ::testing::Values(0)));
102 }  // namespace
103