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