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