superframe_test.cc 3.7 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 *  Copyright (c) 2012 The WebM project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */
#include <climits>
#include "third_party/googletest/src/include/gtest/gtest.h"
#include "test/codec_factory.h"
#include "test/encode_test_driver.h"
#include "test/i420_video_source.h"
#include "test/util.h"

namespace {

19 20 21 22 23
const int kTestMode = 0;
const int kSuperframeSyntax = 1;

typedef std::tr1::tuple<libvpx_test::TestMode,int> SuperframeTestParam;

John Koleszar's avatar
John Koleszar committed
24
class SuperframeTest : public ::libvpx_test::EncoderTest,
25
    public ::libvpx_test::CodecTestWithParam<SuperframeTestParam> {
John Koleszar's avatar
John Koleszar committed
26 27 28
 protected:
  SuperframeTest() : EncoderTest(GET_PARAM(0)), modified_buf_(NULL),
      last_sf_pts_(0) {}
29
  virtual ~SuperframeTest() {}
John Koleszar's avatar
John Koleszar committed
30 31 32

  virtual void SetUp() {
    InitializeConfig();
33 34 35 36
    const SuperframeTestParam input = GET_PARAM(1);
    const libvpx_test::TestMode mode = std::tr1::get<kTestMode>(input);
    const int syntax = std::tr1::get<kSuperframeSyntax>(input);
    SetMode(mode);
John Koleszar's avatar
John Koleszar committed
37 38
    sf_count_ = 0;
    sf_count_max_ = INT_MAX;
39
    is_vp10_style_superframe_ = syntax;
John Koleszar's avatar
John Koleszar committed
40 41 42
  }

  virtual void TearDown() {
43
    delete[] modified_buf_;
John Koleszar's avatar
John Koleszar committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
  }

  virtual void PreEncodeFrameHook(libvpx_test::VideoSource *video,
                                  libvpx_test::Encoder *encoder) {
    if (video->frame() == 1) {
      encoder->Control(VP8E_SET_ENABLEAUTOALTREF, 1);
    }
  }

  virtual const vpx_codec_cx_pkt_t * MutateEncoderOutputHook(
      const vpx_codec_cx_pkt_t *pkt) {
    if (pkt->kind != VPX_CODEC_CX_FRAME_PKT)
      return pkt;

    const uint8_t *buffer = reinterpret_cast<uint8_t*>(pkt->data.frame.buf);
    const uint8_t marker = buffer[pkt->data.frame.sz - 1];
    const int frames = (marker & 0x7) + 1;
    const int mag = ((marker >> 3) & 3) + 1;
62 63
    const unsigned int index_sz =
        2 + mag * (frames - is_vp10_style_superframe_);
64
    if ((marker & 0xe0) == 0xc0 &&
John Koleszar's avatar
John Koleszar committed
65 66 67 68
        pkt->data.frame.sz >= index_sz &&
        buffer[pkt->data.frame.sz - index_sz] == marker) {
      // frame is a superframe. strip off the index.
      if (modified_buf_)
69
        delete[] modified_buf_;
John Koleszar's avatar
John Koleszar committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
      modified_buf_ = new uint8_t[pkt->data.frame.sz - index_sz];
      memcpy(modified_buf_, pkt->data.frame.buf,
             pkt->data.frame.sz - index_sz);
      modified_pkt_ = *pkt;
      modified_pkt_.data.frame.buf = modified_buf_;
      modified_pkt_.data.frame.sz -= index_sz;

      sf_count_++;
      last_sf_pts_ = pkt->data.frame.pts;
      return &modified_pkt_;
    }

    // Make sure we do a few frames after the last SF
    abort_ |= sf_count_ > sf_count_max_ &&
              pkt->data.frame.pts - last_sf_pts_ >= 5;
    return pkt;
  }

88
  int is_vp10_style_superframe_;
John Koleszar's avatar
John Koleszar committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
  int sf_count_;
  int sf_count_max_;
  vpx_codec_cx_pkt_t modified_pkt_;
  uint8_t *modified_buf_;
  vpx_codec_pts_t last_sf_pts_;
};

TEST_P(SuperframeTest, TestSuperframeIndexIsOptional) {
  sf_count_max_ = 0;  // early exit on successful test.
  cfg_.g_lag_in_frames = 25;

  ::libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
                                       30, 1, 0, 40);
  ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
  EXPECT_EQ(sf_count_, 1);
}

106 107 108
VP9_INSTANTIATE_TEST_CASE(SuperframeTest, ::testing::Combine(
    ::testing::Values(::libvpx_test::kTwoPassGood),
    ::testing::Values(0)));
109

110 111 112
VP10_INSTANTIATE_TEST_CASE(SuperframeTest, ::testing::Combine(
    ::testing::Values(::libvpx_test::kTwoPassGood),
    ::testing::Values(CONFIG_MISC_FIXES)));
John Koleszar's avatar
John Koleszar committed
113
}  // namespace