2 * Copyright (c) 2015 The WebM project authors. All Rights Reserved.
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.
13 #include "third_party/googletest/src/include/gtest/gtest.h"
15 #include "test/codec_factory.h"
16 #include "test/encode_test_driver.h"
17 #include "test/util.h"
18 #include "test/y4m_video_source.h"
19 #include "test/yuv_video_source.h"
20 #include "vp9/encoder/vp9_ratectrl.h"
24 const unsigned int kFrames = 100;
25 const int kBitrate = 500;
27 #define ARF_NOT_SEEN 1000001
28 #define ARF_SEEN_ONCE 1000000
34 unsigned int framerate_num;
35 unsigned int framerate_den;
36 unsigned int input_bit_depth;
38 vpx_bit_depth_t bit_depth;
43 libvpx_test::TestMode mode;
47 const TestVideoParam kTestVectors[] = {
48 // artificially increase framerate to trigger default check
49 { "hantro_collage_w352h288.yuv", 352, 288, 5000, 1, 8, VPX_IMG_FMT_I420,
51 { "hantro_collage_w352h288.yuv", 352, 288, 30, 1, 8, VPX_IMG_FMT_I420,
53 { "rush_hour_444.y4m", 352, 288, 30, 1, 8, VPX_IMG_FMT_I444, VPX_BITS_8, 1 },
54 #if CONFIG_VP9_HIGHBITDEPTH
55 // Add list of profile 2/3 test videos here ...
56 #endif // CONFIG_VP9_HIGHBITDEPTH
59 const TestEncodeParam kEncodeVectors[] = {
60 { ::libvpx_test::kOnePassGood, 2 }, { ::libvpx_test::kOnePassGood, 5 },
61 { ::libvpx_test::kTwoPassGood, 1 }, { ::libvpx_test::kTwoPassGood, 2 },
62 { ::libvpx_test::kTwoPassGood, 5 }, { ::libvpx_test::kRealTime, 5 },
65 const int kMinArfVectors[] = {
66 // NOTE: 0 refers to the default built-in logic in:
67 // vp9_rc_get_default_min_gf_interval(...)
71 int is_extension_y4m(const char *filename) {
72 const char *dot = strrchr(filename, '.');
73 if (!dot || dot == filename) {
76 return !strcmp(dot, ".y4m");
81 : public ::libvpx_test::EncoderTest,
82 public ::libvpx_test::CodecTestWith3Params<TestVideoParam,
83 TestEncodeParam, int> {
86 : EncoderTest(GET_PARAM(0)), test_video_param_(GET_PARAM(1)),
87 test_encode_param_(GET_PARAM(2)), min_arf_requested_(GET_PARAM(3)) {}
89 virtual ~ArfFreqTest() {}
91 virtual void SetUp() {
93 SetMode(test_encode_param_.mode);
94 if (test_encode_param_.mode != ::libvpx_test::kRealTime) {
95 cfg_.g_lag_in_frames = 25;
96 cfg_.rc_end_usage = VPX_VBR;
98 cfg_.g_lag_in_frames = 0;
99 cfg_.rc_end_usage = VPX_CBR;
100 cfg_.rc_buf_sz = 1000;
101 cfg_.rc_buf_initial_sz = 500;
102 cfg_.rc_buf_optimal_sz = 600;
104 dec_cfg_.threads = 4;
107 virtual void BeginPassHook(unsigned int) {
108 min_run_ = ARF_NOT_SEEN;
109 run_of_visible_frames_ = 0;
112 int GetNumFramesInPkt(const vpx_codec_cx_pkt_t *pkt) {
113 const uint8_t *buffer = reinterpret_cast<uint8_t *>(pkt->data.frame.buf);
114 const uint8_t marker = buffer[pkt->data.frame.sz - 1];
115 const int mag = ((marker >> 3) & 3) + 1;
116 int frames = (marker & 0x7) + 1;
117 const unsigned int index_sz = 2 + mag * frames;
118 // Check for superframe or not.
119 // Assume superframe has only one visible frame, the rest being
120 // invisible. If superframe index is not found, then there is only
122 if (!((marker & 0xe0) == 0xc0 && pkt->data.frame.sz >= index_sz &&
123 buffer[pkt->data.frame.sz - index_sz] == marker)) {
129 virtual void FramePktHook(const vpx_codec_cx_pkt_t *pkt) {
130 if (pkt->kind != VPX_CODEC_CX_FRAME_PKT) return;
131 const int frames = GetNumFramesInPkt(pkt);
133 run_of_visible_frames_++;
134 } else if (frames == 2) {
135 if (min_run_ == ARF_NOT_SEEN) {
136 min_run_ = ARF_SEEN_ONCE;
137 } else if (min_run_ == ARF_SEEN_ONCE ||
138 run_of_visible_frames_ < min_run_) {
139 min_run_ = run_of_visible_frames_;
141 run_of_visible_frames_ = 1;
144 run_of_visible_frames_ = 1;
148 virtual void PreEncodeFrameHook(::libvpx_test::VideoSource *video,
149 ::libvpx_test::Encoder *encoder) {
150 if (video->frame() == 0) {
151 encoder->Control(VP9E_SET_FRAME_PARALLEL_DECODING, 1);
152 encoder->Control(VP9E_SET_TILE_COLUMNS, 4);
153 encoder->Control(VP8E_SET_CPUUSED, test_encode_param_.cpu_used);
154 encoder->Control(VP9E_SET_MIN_GF_INTERVAL, min_arf_requested_);
155 if (test_encode_param_.mode != ::libvpx_test::kRealTime) {
156 encoder->Control(VP8E_SET_ENABLEAUTOALTREF, 1);
157 encoder->Control(VP8E_SET_ARNR_MAXFRAMES, 7);
158 encoder->Control(VP8E_SET_ARNR_STRENGTH, 5);
159 encoder->Control(VP8E_SET_ARNR_TYPE, 3);
164 int GetMinVisibleRun() const { return min_run_; }
166 int GetMinArfDistanceRequested() const {
167 if (min_arf_requested_) {
168 return min_arf_requested_;
170 return vp9_rc_get_default_min_gf_interval(
171 test_video_param_.width, test_video_param_.height,
172 (double)test_video_param_.framerate_num /
173 test_video_param_.framerate_den);
177 TestVideoParam test_video_param_;
178 TestEncodeParam test_encode_param_;
181 int min_arf_requested_;
183 int run_of_visible_frames_;
186 TEST_P(ArfFreqTest, MinArfFreqTest) {
187 cfg_.rc_target_bitrate = kBitrate;
188 cfg_.g_error_resilient = 0;
189 cfg_.g_profile = test_video_param_.profile;
190 cfg_.g_input_bit_depth = test_video_param_.input_bit_depth;
191 cfg_.g_bit_depth = test_video_param_.bit_depth;
192 init_flags_ = VPX_CODEC_USE_PSNR;
193 if (cfg_.g_bit_depth > 8) init_flags_ |= VPX_CODEC_USE_HIGHBITDEPTH;
195 std::unique_ptr<libvpx_test::VideoSource> video;
196 if (is_extension_y4m(test_video_param_.filename)) {
197 video.reset(new libvpx_test::Y4mVideoSource(test_video_param_.filename, 0,
200 video.reset(new libvpx_test::YUVVideoSource(
201 test_video_param_.filename, test_video_param_.fmt,
202 test_video_param_.width, test_video_param_.height,
203 test_video_param_.framerate_num, test_video_param_.framerate_den, 0,
207 ASSERT_NO_FATAL_FAILURE(RunLoop(video.get()));
208 const int min_run = GetMinVisibleRun();
209 const int min_arf_dist_requested = GetMinArfDistanceRequested();
210 if (min_run != ARF_NOT_SEEN && min_run != ARF_SEEN_ONCE) {
211 const int min_arf_dist = min_run + 1;
212 EXPECT_GE(min_arf_dist, min_arf_dist_requested);
216 VP9_INSTANTIATE_TEST_CASE(ArfFreqTest, ::testing::ValuesIn(kTestVectors),
217 ::testing::ValuesIn(kEncodeVectors),
218 ::testing::ValuesIn(kMinArfVectors));