2 * Copyright (c) 2012 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.
12 #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/i420_video_source.h"
16 #include "test/video_source.h"
17 #include "test/util.h"
19 // Enable(1) or Disable(0) writing of the compressed bitstream.
20 #define WRITE_COMPRESSED_STREAM 0
24 #if WRITE_COMPRESSED_STREAM
25 static void mem_put_le16(char *const mem, const unsigned int val) {
30 static void mem_put_le32(char *const mem, const unsigned int val) {
37 static void write_ivf_file_header(const vpx_codec_enc_cfg_t *const cfg,
38 int frame_cnt, FILE *const outfile) {
45 mem_put_le16(header + 4, 0); /* version */
46 mem_put_le16(header + 6, 32); /* headersize */
47 mem_put_le32(header + 8, 0x30395056); /* fourcc (vp9) */
48 mem_put_le16(header + 12, cfg->g_w); /* width */
49 mem_put_le16(header + 14, cfg->g_h); /* height */
50 mem_put_le32(header + 16, cfg->g_timebase.den); /* rate */
51 mem_put_le32(header + 20, cfg->g_timebase.num); /* scale */
52 mem_put_le32(header + 24, frame_cnt); /* length */
53 mem_put_le32(header + 28, 0); /* unused */
55 (void)fwrite(header, 1, 32, outfile);
58 static void write_ivf_frame_size(FILE *const outfile, const size_t size) {
60 mem_put_le32(header, static_cast<unsigned int>(size));
61 (void)fwrite(header, 1, 4, outfile);
64 static void write_ivf_frame_header(const vpx_codec_cx_pkt_t *const pkt,
65 FILE *const outfile) {
69 if (pkt->kind != VPX_CODEC_CX_FRAME_PKT)
72 pts = pkt->data.frame.pts;
73 mem_put_le32(header, static_cast<unsigned int>(pkt->data.frame.sz));
74 mem_put_le32(header + 4, pts & 0xFFFFFFFF);
75 mem_put_le32(header + 8, pts >> 32);
77 (void)fwrite(header, 1, 12, outfile);
79 #endif // WRITE_COMPRESSED_STREAM
81 const unsigned int kInitialWidth = 320;
82 const unsigned int kInitialHeight = 240;
85 FrameInfo(vpx_codec_pts_t _pts, unsigned int _w, unsigned int _h)
86 : pts(_pts), w(_w), h(_h) {}
93 unsigned int ScaleForFrameNumber(unsigned int frame, unsigned int val) {
107 class ResizingVideoSource : public ::libvpx_test::DummyVideoSource {
109 ResizingVideoSource() {
110 SetSize(kInitialWidth, kInitialHeight);
114 virtual ~ResizingVideoSource() {}
117 virtual void Next() {
119 SetSize(ScaleForFrameNumber(frame_, kInitialWidth),
120 ScaleForFrameNumber(frame_, kInitialHeight));
125 class ResizeTest : public ::libvpx_test::EncoderTest,
126 public ::libvpx_test::CodecTestWithParam<libvpx_test::TestMode> {
128 ResizeTest() : EncoderTest(GET_PARAM(0)) {}
130 virtual ~ResizeTest() {}
132 virtual void SetUp() {
134 SetMode(GET_PARAM(1));
137 virtual void DecompressedFrameHook(const vpx_image_t &img,
138 vpx_codec_pts_t pts) {
139 frame_info_list_.push_back(FrameInfo(pts, img.d_w, img.d_h));
142 std::vector< FrameInfo > frame_info_list_;
145 TEST_P(ResizeTest, TestExternalResizeWorks) {
146 ResizingVideoSource video;
147 cfg_.g_lag_in_frames = 0;
148 ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
150 for (std::vector<FrameInfo>::const_iterator info = frame_info_list_.begin();
151 info != frame_info_list_.end(); ++info) {
152 const unsigned int frame = static_cast<unsigned>(info->pts);
153 const unsigned int expected_w = ScaleForFrameNumber(frame, kInitialWidth);
154 const unsigned int expected_h = ScaleForFrameNumber(frame, kInitialHeight);
156 EXPECT_EQ(expected_w, info->w)
157 << "Frame " << frame << " had unexpected width";
158 EXPECT_EQ(expected_h, info->h)
159 << "Frame " << frame << " had unexpected height";
163 const unsigned int kStepDownFrame = 3;
164 const unsigned int kStepUpFrame = 6;
166 class ResizeInternalTest : public ResizeTest {
168 #if WRITE_COMPRESSED_STREAM
175 ResizeInternalTest() : ResizeTest(), frame0_psnr_(0.0) {}
178 virtual ~ResizeInternalTest() {}
180 virtual void BeginPassHook(unsigned int /*pass*/) {
181 #if WRITE_COMPRESSED_STREAM
182 outfile_ = fopen("vp90-2-05-resize.ivf", "wb");
186 virtual void EndPassHook() {
187 #if WRITE_COMPRESSED_STREAM
189 if (!fseek(outfile_, 0, SEEK_SET))
190 write_ivf_file_header(&cfg_, out_frames_, outfile_);
197 virtual void PreEncodeFrameHook(libvpx_test::VideoSource *video,
198 libvpx_test::Encoder *encoder) {
199 if (video->frame() == kStepDownFrame) {
200 struct vpx_scaling_mode mode = {VP8E_FOURFIVE, VP8E_THREEFIVE};
201 encoder->Control(VP8E_SET_SCALEMODE, &mode);
203 if (video->frame() == kStepUpFrame) {
204 struct vpx_scaling_mode mode = {VP8E_NORMAL, VP8E_NORMAL};
205 encoder->Control(VP8E_SET_SCALEMODE, &mode);
209 virtual void PSNRPktHook(const vpx_codec_cx_pkt_t *pkt) {
211 frame0_psnr_ = pkt->data.psnr.psnr[0];
212 EXPECT_NEAR(pkt->data.psnr.psnr[0], frame0_psnr_, 2.0);
215 #if WRITE_COMPRESSED_STREAM
216 virtual void FramePktHook(const vpx_codec_cx_pkt_t *pkt) {
219 // Write initial file header if first frame.
220 if (pkt->data.frame.pts == 0)
221 write_ivf_file_header(&cfg_, 0, outfile_);
223 // Write frame header and data.
224 write_ivf_frame_header(pkt, outfile_);
225 (void)fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz, outfile_);
230 #if WRITE_COMPRESSED_STREAM
232 unsigned int out_frames_;
236 TEST_P(ResizeInternalTest, TestInternalResizeWorks) {
237 ::libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
239 init_flags_ = VPX_CODEC_USE_PSNR;
241 // q picked such that initial keyframe on this clip is ~30dB PSNR
242 cfg_.rc_min_quantizer = cfg_.rc_max_quantizer = 48;
244 // If the number of frames being encoded is smaller than g_lag_in_frames
245 // the encoded frame is unavailable using the current API. Comparing
246 // frames to detect mismatch would then not be possible. Set
247 // g_lag_in_frames = 0 to get around this.
248 cfg_.g_lag_in_frames = 0;
249 ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
251 for (std::vector<FrameInfo>::const_iterator info = frame_info_list_.begin();
252 info != frame_info_list_.end(); ++info) {
253 const vpx_codec_pts_t pts = info->pts;
254 if (pts >= kStepDownFrame && pts < kStepUpFrame) {
255 ASSERT_EQ(282U, info->w) << "Frame " << pts << " had unexpected width";
256 ASSERT_EQ(173U, info->h) << "Frame " << pts << " had unexpected height";
258 EXPECT_EQ(352U, info->w) << "Frame " << pts << " had unexpected width";
259 EXPECT_EQ(288U, info->h) << "Frame " << pts << " had unexpected height";
264 class ResizeInternalRealtimeTest : public ::libvpx_test::EncoderTest,
265 public ::libvpx_test::CodecTestWith2Params<libvpx_test::TestMode, int> {
267 ResizeInternalRealtimeTest() : EncoderTest(GET_PARAM(0)) {}
268 virtual ~ResizeInternalRealtimeTest() {}
270 virtual void PreEncodeFrameHook(libvpx_test::VideoSource *video,
271 libvpx_test::Encoder *encoder) {
272 if (video->frame() == 0) {
273 encoder->Control(VP9E_SET_AQ_MODE, 3);
274 encoder->Control(VP8E_SET_CPUUSED, set_cpu_used_);
277 if (change_bitrate_ && video->frame() == 120) {
278 change_bitrate_ = false;
279 cfg_.rc_target_bitrate = 500;
280 encoder->Config(&cfg_);
284 virtual void SetUp() {
286 SetMode(GET_PARAM(1));
287 set_cpu_used_ = GET_PARAM(2);
290 virtual void DecompressedFrameHook(const vpx_image_t &img,
291 vpx_codec_pts_t pts) {
292 frame_info_list_.push_back(FrameInfo(pts, img.d_w, img.d_h));
295 void DefaultConfig() {
298 cfg_.rc_buf_initial_sz = 500;
299 cfg_.rc_buf_optimal_sz = 600;
300 cfg_.rc_buf_sz = 1000;
301 cfg_.rc_min_quantizer = 2;
302 cfg_.rc_max_quantizer = 56;
303 cfg_.rc_undershoot_pct = 50;
304 cfg_.rc_overshoot_pct = 50;
305 cfg_.rc_end_usage = VPX_CBR;
306 cfg_.kf_mode = VPX_KF_AUTO;
307 cfg_.g_lag_in_frames = 0;
308 cfg_.kf_min_dist = cfg_.kf_max_dist = 3000;
309 // Enable dropped frames.
310 cfg_.rc_dropframe_thresh = 1;
311 // Enable error_resilience mode.
312 cfg_.g_error_resilient = 1;
313 // Enable dynamic resizing.
314 cfg_.rc_resize_allowed = 1;
315 // Run at low bitrate.
316 cfg_.rc_target_bitrate = 200;
319 std::vector< FrameInfo > frame_info_list_;
321 bool change_bitrate_;
324 // Verify the dynamic resizer behavior for real time, 1 pass CBR mode.
325 // Run at low bitrate, with resize_allowed = 1, and verify that we get
326 // one resize down event.
327 TEST_P(ResizeInternalRealtimeTest, TestInternalResizeDown) {
328 ::libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
331 change_bitrate_ = false;
332 ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
334 unsigned int last_w = cfg_.g_w;
335 unsigned int last_h = cfg_.g_h;
336 int resize_count = 0;
337 for (std::vector<FrameInfo>::const_iterator info = frame_info_list_.begin();
338 info != frame_info_list_.end(); ++info) {
339 if (info->w != last_w || info->h != last_h) {
340 // Verify that resize down occurs.
341 ASSERT_LT(info->w, last_w);
342 ASSERT_LT(info->h, last_h);
349 // Verify that we get 1 resize down event in this test.
350 ASSERT_EQ(1, resize_count) << "Resizing should occur.";
353 // Verify the dynamic resizer behavior for real time, 1 pass CBR mode.
354 // Start at low target bitrate, raise the bitrate in the middle of the clip,
355 // scaling-up should occur after bitrate changed.
356 TEST_P(ResizeInternalRealtimeTest, TestInternalResizeDownUpChangeBitRate) {
357 ::libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
360 change_bitrate_ = true;
361 // Disable dropped frames.
362 cfg_.rc_dropframe_thresh = 0;
363 // Starting bitrate low.
364 cfg_.rc_target_bitrate = 100;
365 ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
367 unsigned int last_w = cfg_.g_w;
368 unsigned int last_h = cfg_.g_h;
369 int resize_count = 0;
370 for (std::vector<FrameInfo>::const_iterator info = frame_info_list_.begin();
371 info != frame_info_list_.end(); ++info) {
372 if (info->w != last_w || info->h != last_h) {
374 if (resize_count == 1) {
375 // Verify that resize down occurs.
376 ASSERT_LT(info->w, last_w);
377 ASSERT_LT(info->h, last_h);
378 } else if (resize_count == 2) {
379 // Verify that resize up occurs.
380 ASSERT_GT(info->w, last_w);
381 ASSERT_GT(info->h, last_h);
388 // Verify that we get 2 resize events in this test.
389 ASSERT_EQ(2, resize_count) << "Resizing should occur twice.";
392 vpx_img_fmt_t CspForFrameNumber(int frame) {
394 return VPX_IMG_FMT_I420;
396 return VPX_IMG_FMT_I444;
397 return VPX_IMG_FMT_I420;
400 class ResizeCspTest : public ResizeTest {
402 #if WRITE_COMPRESSED_STREAM
409 ResizeCspTest() : ResizeTest(), frame0_psnr_(0.0) {}
412 virtual ~ResizeCspTest() {}
414 virtual void BeginPassHook(unsigned int /*pass*/) {
415 #if WRITE_COMPRESSED_STREAM
416 outfile_ = fopen("vp91-2-05-cspchape.ivf", "wb");
420 virtual void EndPassHook() {
421 #if WRITE_COMPRESSED_STREAM
423 if (!fseek(outfile_, 0, SEEK_SET))
424 write_ivf_file_header(&cfg_, out_frames_, outfile_);
431 virtual void PreEncodeFrameHook(libvpx_test::VideoSource *video,
432 libvpx_test::Encoder *encoder) {
433 if (CspForFrameNumber(video->frame()) != VPX_IMG_FMT_I420 &&
434 cfg_.g_profile != 1) {
436 encoder->Config(&cfg_);
438 if (CspForFrameNumber(video->frame()) == VPX_IMG_FMT_I420 &&
439 cfg_.g_profile != 0) {
441 encoder->Config(&cfg_);
445 virtual void PSNRPktHook(const vpx_codec_cx_pkt_t *pkt) {
447 frame0_psnr_ = pkt->data.psnr.psnr[0];
448 EXPECT_NEAR(pkt->data.psnr.psnr[0], frame0_psnr_, 2.0);
451 #if WRITE_COMPRESSED_STREAM
452 virtual void FramePktHook(const vpx_codec_cx_pkt_t *pkt) {
455 // Write initial file header if first frame.
456 if (pkt->data.frame.pts == 0)
457 write_ivf_file_header(&cfg_, 0, outfile_);
459 // Write frame header and data.
460 write_ivf_frame_header(pkt, outfile_);
461 (void)fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz, outfile_);
466 #if WRITE_COMPRESSED_STREAM
468 unsigned int out_frames_;
472 class ResizingCspVideoSource : public ::libvpx_test::DummyVideoSource {
474 ResizingCspVideoSource() {
475 SetSize(kInitialWidth, kInitialHeight);
479 virtual ~ResizingCspVideoSource() {}
482 virtual void Next() {
484 SetImageFormat(CspForFrameNumber(frame_));
489 TEST_P(ResizeCspTest, TestResizeCspWorks) {
490 ResizingCspVideoSource video;
491 init_flags_ = VPX_CODEC_USE_PSNR;
492 cfg_.rc_min_quantizer = cfg_.rc_max_quantizer = 48;
493 cfg_.g_lag_in_frames = 0;
494 ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
497 VP8_INSTANTIATE_TEST_CASE(ResizeTest, ONE_PASS_TEST_MODES);
498 VP9_INSTANTIATE_TEST_CASE(ResizeTest,
499 ::testing::Values(::libvpx_test::kRealTime));
500 VP9_INSTANTIATE_TEST_CASE(ResizeInternalTest,
501 ::testing::Values(::libvpx_test::kOnePassBest));
502 VP9_INSTANTIATE_TEST_CASE(ResizeInternalRealtimeTest,
503 ::testing::Values(::libvpx_test::kRealTime),
504 ::testing::Range(5, 9));
505 VP9_INSTANTIATE_TEST_CASE(ResizeCspTest,
506 ::testing::Values(::libvpx_test::kRealTime));