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.
11 #include "third_party/googletest/src/include/gtest/gtest.h"
13 #include "test/codec_factory.h"
14 #include "test/encode_test_driver.h"
15 #include "test/util.h"
16 #include "test/y4m_video_source.h"
17 #include "test/yuv_video_source.h"
18 #include "vp9/encoder/vp9_ratectrl.h"
22 const unsigned int kFrames = 100;
23 const int kBitrate = 500;
25 #define ARF_NOT_SEEN 1000001
26 #define ARF_SEEN_ONCE 1000000
32 unsigned int framerate_num;
33 unsigned int framerate_den;
34 unsigned int input_bit_depth;
36 vpx_bit_depth_t bit_depth;
41 libvpx_test::TestMode mode;
45 const TestVideoParam kTestVectors[] = {
46 // artificially increase framerate to trigger default check
47 {"hantro_collage_w352h288.yuv", 352, 288, 5000, 1,
48 8, VPX_IMG_FMT_I420, VPX_BITS_8, 0},
49 {"hantro_collage_w352h288.yuv", 352, 288, 30, 1,
50 8, VPX_IMG_FMT_I420, VPX_BITS_8, 0},
51 {"rush_hour_444.y4m", 352, 288, 30, 1,
52 8, VPX_IMG_FMT_I444, VPX_BITS_8, 1},
53 #if CONFIG_VP9_HIGHBITDEPTH
54 // Add list of profile 2/3 test videos here ...
55 #endif // CONFIG_VP9_HIGHBITDEPTH
58 const TestEncodeParam kEncodeVectors[] = {
59 {::libvpx_test::kOnePassGood, 2},
60 {::libvpx_test::kOnePassGood, 5},
61 {::libvpx_test::kTwoPassGood, 1},
62 {::libvpx_test::kTwoPassGood, 2},
63 {::libvpx_test::kTwoPassGood, 5},
64 {::libvpx_test::kRealTime, 5},
67 const int kMinArfVectors[] = {
68 // NOTE: 0 refers to the default built-in logic in:
69 // vp9_rc_get_default_min_gf_interval(...)
73 int is_extension_y4m(const char *filename) {
74 const char *dot = strrchr(filename, '.');
75 if (!dot || dot == filename)
78 return !strcmp(dot, ".y4m");
82 : public ::libvpx_test::EncoderTest,
83 public ::libvpx_test::CodecTestWith3Params<TestVideoParam, \
84 TestEncodeParam, int> {
87 : EncoderTest(GET_PARAM(0)),
88 test_video_param_(GET_PARAM(1)),
89 test_encode_param_(GET_PARAM(2)),
90 min_arf_requested_(GET_PARAM(3)) {
93 virtual ~ArfFreqTest() {}
95 virtual void SetUp() {
97 SetMode(test_encode_param_.mode);
98 if (test_encode_param_.mode != ::libvpx_test::kRealTime) {
99 cfg_.g_lag_in_frames = 25;
100 cfg_.rc_end_usage = VPX_VBR;
102 cfg_.g_lag_in_frames = 0;
103 cfg_.rc_end_usage = VPX_CBR;
104 cfg_.rc_buf_sz = 1000;
105 cfg_.rc_buf_initial_sz = 500;
106 cfg_.rc_buf_optimal_sz = 600;
108 dec_cfg_.threads = 4;
111 virtual void BeginPassHook(unsigned int) {
112 min_run_ = ARF_NOT_SEEN;
113 run_of_visible_frames_ = 0;
116 int GetNumFramesInPkt(const vpx_codec_cx_pkt_t *pkt) {
117 const uint8_t *buffer = reinterpret_cast<uint8_t*>(pkt->data.frame.buf);
118 const uint8_t marker = buffer[pkt->data.frame.sz - 1];
119 const int mag = ((marker >> 3) & 3) + 1;
120 int frames = (marker & 0x7) + 1;
121 const unsigned int index_sz = 2 + mag * frames;
122 // Check for superframe or not.
123 // Assume superframe has only one visible frame, the rest being
124 // invisible. If superframe index is not found, then there is only
126 if (!((marker & 0xe0) == 0xc0 &&
127 pkt->data.frame.sz >= index_sz &&
128 buffer[pkt->data.frame.sz - index_sz] == marker)) {
134 virtual void FramePktHook(const vpx_codec_cx_pkt_t *pkt) {
135 if (pkt->kind != VPX_CODEC_CX_FRAME_PKT)
137 const int frames = GetNumFramesInPkt(pkt);
139 run_of_visible_frames_++;
140 } else if (frames == 2) {
141 if (min_run_ == ARF_NOT_SEEN) {
142 min_run_ = ARF_SEEN_ONCE;
143 } else if (min_run_ == ARF_SEEN_ONCE ||
144 run_of_visible_frames_ < min_run_) {
145 min_run_ = run_of_visible_frames_;
147 run_of_visible_frames_ = 1;
150 run_of_visible_frames_ = 1;
154 virtual void PreEncodeFrameHook(::libvpx_test::VideoSource *video,
155 ::libvpx_test::Encoder *encoder) {
156 if (video->frame() == 0) {
157 encoder->Control(VP9E_SET_FRAME_PARALLEL_DECODING, 1);
158 encoder->Control(VP9E_SET_TILE_COLUMNS, 4);
159 encoder->Control(VP8E_SET_CPUUSED, test_encode_param_.cpu_used);
160 encoder->Control(VP9E_SET_MIN_GF_INTERVAL, min_arf_requested_);
161 if (test_encode_param_.mode != ::libvpx_test::kRealTime) {
162 encoder->Control(VP8E_SET_ENABLEAUTOALTREF, 1);
163 encoder->Control(VP8E_SET_ARNR_MAXFRAMES, 7);
164 encoder->Control(VP8E_SET_ARNR_STRENGTH, 5);
165 encoder->Control(VP8E_SET_ARNR_TYPE, 3);
170 int GetMinVisibleRun() const {
174 int GetMinArfDistanceRequested() const {
175 if (min_arf_requested_)
176 return min_arf_requested_;
178 return vp9_rc_get_default_min_gf_interval(
179 test_video_param_.width, test_video_param_.height,
180 (double)test_video_param_.framerate_num /
181 test_video_param_.framerate_den);
184 TestVideoParam test_video_param_;
185 TestEncodeParam test_encode_param_;
188 int min_arf_requested_;
190 int run_of_visible_frames_;
193 TEST_P(ArfFreqTest, MinArfFreqTest) {
194 cfg_.rc_target_bitrate = kBitrate;
195 cfg_.g_error_resilient = 0;
196 cfg_.g_profile = test_video_param_.profile;
197 cfg_.g_input_bit_depth = test_video_param_.input_bit_depth;
198 cfg_.g_bit_depth = test_video_param_.bit_depth;
199 init_flags_ = VPX_CODEC_USE_PSNR;
200 if (cfg_.g_bit_depth > 8)
201 init_flags_ |= VPX_CODEC_USE_HIGHBITDEPTH;
203 libvpx_test::VideoSource *video;
204 if (is_extension_y4m(test_video_param_.filename)) {
205 video = new libvpx_test::Y4mVideoSource(test_video_param_.filename,
208 video = new libvpx_test::YUVVideoSource(test_video_param_.filename,
209 test_video_param_.fmt,
210 test_video_param_.width,
211 test_video_param_.height,
212 test_video_param_.framerate_num,
213 test_video_param_.framerate_den,
217 ASSERT_NO_FATAL_FAILURE(RunLoop(video));
218 const int min_run = GetMinVisibleRun();
219 const int min_arf_dist_requested = GetMinArfDistanceRequested();
220 if (min_run != ARF_NOT_SEEN && min_run != ARF_SEEN_ONCE) {
221 const int min_arf_dist = min_run + 1;
222 EXPECT_GE(min_arf_dist, min_arf_dist_requested);
227 VP9_INSTANTIATE_TEST_CASE(
229 ::testing::ValuesIn(kTestVectors),
230 ::testing::ValuesIn(kEncodeVectors),
231 ::testing::ValuesIn(kMinArfVectors));
233 VP10_INSTANTIATE_TEST_CASE(
235 ::testing::ValuesIn(kTestVectors),
236 ::testing::ValuesIn(kEncodeVectors),
237 ::testing::ValuesIn(kMinArfVectors));