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) {
147 class ResizingVideoSource : public ::libvpx_test::DummyVideoSource {
149 ResizingVideoSource() {
150 SetSize(kInitialWidth, kInitialHeight);
154 virtual ~ResizingVideoSource() {}
157 virtual void Next() {
159 SetSize(ScaleForFrameNumber(frame_, kInitialWidth),
160 ScaleForFrameNumber(frame_, kInitialHeight));
165 class ResizeTest : public ::libvpx_test::EncoderTest,
166 public ::libvpx_test::CodecTestWithParam<libvpx_test::TestMode> {
168 ResizeTest() : EncoderTest(GET_PARAM(0)) {}
170 virtual ~ResizeTest() {}
172 virtual void SetUp() {
174 SetMode(GET_PARAM(1));
177 virtual void DecompressedFrameHook(const vpx_image_t &img,
178 vpx_codec_pts_t pts) {
179 frame_info_list_.push_back(FrameInfo(pts, img.d_w, img.d_h));
182 std::vector< FrameInfo > frame_info_list_;
185 TEST_P(ResizeTest, TestExternalResizeWorks) {
186 ResizingVideoSource video;
187 cfg_.g_lag_in_frames = 0;
188 ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
190 for (std::vector<FrameInfo>::const_iterator info = frame_info_list_.begin();
191 info != frame_info_list_.end(); ++info) {
192 const unsigned int frame = static_cast<unsigned>(info->pts);
193 const unsigned int expected_w = ScaleForFrameNumber(frame, kInitialWidth);
194 const unsigned int expected_h = ScaleForFrameNumber(frame, kInitialHeight);
196 EXPECT_EQ(expected_w, info->w)
197 << "Frame " << frame << " had unexpected width";
198 EXPECT_EQ(expected_h, info->h)
199 << "Frame " << frame << " had unexpected height";
203 const unsigned int kStepDownFrame = 3;
204 const unsigned int kStepUpFrame = 6;
206 class ResizeInternalTest : public ResizeTest {
208 #if WRITE_COMPRESSED_STREAM
215 ResizeInternalTest() : ResizeTest(), frame0_psnr_(0.0) {}
218 virtual ~ResizeInternalTest() {}
220 virtual void BeginPassHook(unsigned int /*pass*/) {
221 #if WRITE_COMPRESSED_STREAM
222 outfile_ = fopen("vp90-2-05-resize.ivf", "wb");
226 virtual void EndPassHook() {
227 #if WRITE_COMPRESSED_STREAM
229 if (!fseek(outfile_, 0, SEEK_SET))
230 write_ivf_file_header(&cfg_, out_frames_, outfile_);
237 virtual void PreEncodeFrameHook(libvpx_test::VideoSource *video,
238 libvpx_test::Encoder *encoder) {
239 if (change_config_) {
241 if (video->frame() == 0) {
242 struct vpx_scaling_mode mode = {VP8E_ONETWO, VP8E_ONETWO};
243 encoder->Control(VP8E_SET_SCALEMODE, &mode);
245 if (video->frame() == 1) {
246 struct vpx_scaling_mode mode = {VP8E_NORMAL, VP8E_NORMAL};
247 encoder->Control(VP8E_SET_SCALEMODE, &mode);
248 cfg_.rc_min_quantizer = cfg_.rc_max_quantizer = new_q;
249 encoder->Config(&cfg_);
252 if (video->frame() == kStepDownFrame) {
253 struct vpx_scaling_mode mode = {VP8E_FOURFIVE, VP8E_THREEFIVE};
254 encoder->Control(VP8E_SET_SCALEMODE, &mode);
256 if (video->frame() == kStepUpFrame) {
257 struct vpx_scaling_mode mode = {VP8E_NORMAL, VP8E_NORMAL};
258 encoder->Control(VP8E_SET_SCALEMODE, &mode);
263 virtual void PSNRPktHook(const vpx_codec_cx_pkt_t *pkt) {
265 frame0_psnr_ = pkt->data.psnr.psnr[0];
266 EXPECT_NEAR(pkt->data.psnr.psnr[0], frame0_psnr_, 2.0);
269 #if WRITE_COMPRESSED_STREAM
270 virtual void FramePktHook(const vpx_codec_cx_pkt_t *pkt) {
273 // Write initial file header if first frame.
274 if (pkt->data.frame.pts == 0)
275 write_ivf_file_header(&cfg_, 0, outfile_);
277 // Write frame header and data.
278 write_ivf_frame_header(pkt, outfile_);
279 (void)fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz, outfile_);
285 #if WRITE_COMPRESSED_STREAM
287 unsigned int out_frames_;
291 TEST_P(ResizeInternalTest, TestInternalResizeWorks) {
292 ::libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
294 init_flags_ = VPX_CODEC_USE_PSNR;
295 change_config_ = false;
297 // q picked such that initial keyframe on this clip is ~30dB PSNR
298 cfg_.rc_min_quantizer = cfg_.rc_max_quantizer = 48;
300 // If the number of frames being encoded is smaller than g_lag_in_frames
301 // the encoded frame is unavailable using the current API. Comparing
302 // frames to detect mismatch would then not be possible. Set
303 // g_lag_in_frames = 0 to get around this.
304 cfg_.g_lag_in_frames = 0;
305 ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
307 for (std::vector<FrameInfo>::const_iterator info = frame_info_list_.begin();
308 info != frame_info_list_.end(); ++info) {
309 const vpx_codec_pts_t pts = info->pts;
310 if (pts >= kStepDownFrame && pts < kStepUpFrame) {
311 ASSERT_EQ(282U, info->w) << "Frame " << pts << " had unexpected width";
312 ASSERT_EQ(173U, info->h) << "Frame " << pts << " had unexpected height";
314 EXPECT_EQ(352U, info->w) << "Frame " << pts << " had unexpected width";
315 EXPECT_EQ(288U, info->h) << "Frame " << pts << " had unexpected height";
320 TEST_P(ResizeInternalTest, TestInternalResizeChangeConfig) {
321 ::libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
325 change_config_ = true;
326 ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
329 class ResizeRealtimeTest : public ::libvpx_test::EncoderTest,
330 public ::libvpx_test::CodecTestWith2Params<libvpx_test::TestMode, int> {
332 ResizeRealtimeTest() : EncoderTest(GET_PARAM(0)) {}
333 virtual ~ResizeRealtimeTest() {}
335 virtual void PreEncodeFrameHook(libvpx_test::VideoSource *video,
336 libvpx_test::Encoder *encoder) {
337 if (video->frame() == 0) {
338 encoder->Control(VP9E_SET_AQ_MODE, 3);
339 encoder->Control(VP8E_SET_CPUUSED, set_cpu_used_);
342 if (change_bitrate_ && video->frame() == 120) {
343 change_bitrate_ = false;
344 cfg_.rc_target_bitrate = 500;
345 encoder->Config(&cfg_);
349 virtual void SetUp() {
351 SetMode(GET_PARAM(1));
352 set_cpu_used_ = GET_PARAM(2);
355 virtual void DecompressedFrameHook(const vpx_image_t &img,
356 vpx_codec_pts_t pts) {
357 frame_info_list_.push_back(FrameInfo(pts, img.d_w, img.d_h));
360 void DefaultConfig() {
361 cfg_.rc_buf_initial_sz = 500;
362 cfg_.rc_buf_optimal_sz = 600;
363 cfg_.rc_buf_sz = 1000;
364 cfg_.rc_min_quantizer = 2;
365 cfg_.rc_max_quantizer = 56;
366 cfg_.rc_undershoot_pct = 50;
367 cfg_.rc_overshoot_pct = 50;
368 cfg_.rc_end_usage = VPX_CBR;
369 cfg_.kf_mode = VPX_KF_AUTO;
370 cfg_.g_lag_in_frames = 0;
371 cfg_.kf_min_dist = cfg_.kf_max_dist = 3000;
372 // Enable dropped frames.
373 cfg_.rc_dropframe_thresh = 1;
374 // Enable error_resilience mode.
375 cfg_.g_error_resilient = 1;
376 // Enable dynamic resizing.
377 cfg_.rc_resize_allowed = 1;
378 // Run at low bitrate.
379 cfg_.rc_target_bitrate = 200;
382 std::vector< FrameInfo > frame_info_list_;
384 bool change_bitrate_;
387 TEST_P(ResizeRealtimeTest, TestExternalResizeWorks) {
388 ResizingVideoSource video;
390 // Disable internal resize for this test.
391 cfg_.rc_resize_allowed = 0;
392 change_bitrate_ = false;
393 ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
395 for (std::vector<FrameInfo>::const_iterator info = frame_info_list_.begin();
396 info != frame_info_list_.end(); ++info) {
397 const unsigned int frame = static_cast<unsigned>(info->pts);
398 const unsigned int expected_w = ScaleForFrameNumber(frame, kInitialWidth);
399 const unsigned int expected_h = ScaleForFrameNumber(frame, kInitialHeight);
401 EXPECT_EQ(expected_w, info->w)
402 << "Frame " << frame << " had unexpected width";
403 EXPECT_EQ(expected_h, info->h)
404 << "Frame " << frame << " had unexpected height";
408 // Verify the dynamic resizer behavior for real time, 1 pass CBR mode.
409 // Run at low bitrate, with resize_allowed = 1, and verify that we get
410 // one resize down event.
411 TEST_P(ResizeRealtimeTest, TestInternalResizeDown) {
412 ::libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
417 change_bitrate_ = false;
418 ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
420 unsigned int last_w = cfg_.g_w;
421 unsigned int last_h = cfg_.g_h;
422 int resize_count = 0;
423 for (std::vector<FrameInfo>::const_iterator info = frame_info_list_.begin();
424 info != frame_info_list_.end(); ++info) {
425 if (info->w != last_w || info->h != last_h) {
426 // Verify that resize down occurs.
427 ASSERT_LT(info->w, last_w);
428 ASSERT_LT(info->h, last_h);
435 // Verify that we get 1 resize down event in this test.
436 ASSERT_EQ(1, resize_count) << "Resizing should occur.";
439 // Verify the dynamic resizer behavior for real time, 1 pass CBR mode.
440 // Start at low target bitrate, raise the bitrate in the middle of the clip,
441 // scaling-up should occur after bitrate changed.
442 TEST_P(ResizeRealtimeTest, TestInternalResizeDownUpChangeBitRate) {
443 ::libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
448 change_bitrate_ = true;
449 // Disable dropped frames.
450 cfg_.rc_dropframe_thresh = 0;
451 // Starting bitrate low.
452 cfg_.rc_target_bitrate = 80;
453 ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
455 unsigned int last_w = cfg_.g_w;
456 unsigned int last_h = cfg_.g_h;
457 int resize_count = 0;
458 for (std::vector<FrameInfo>::const_iterator info = frame_info_list_.begin();
459 info != frame_info_list_.end(); ++info) {
460 if (info->w != last_w || info->h != last_h) {
462 if (resize_count == 1) {
463 // Verify that resize down occurs.
464 ASSERT_LT(info->w, last_w);
465 ASSERT_LT(info->h, last_h);
466 } else if (resize_count == 2) {
467 // Verify that resize up occurs.
468 ASSERT_GT(info->w, last_w);
469 ASSERT_GT(info->h, last_h);
476 // Verify that we get 2 resize events in this test.
477 ASSERT_EQ(resize_count, 2) << "Resizing should occur twice.";
480 vpx_img_fmt_t CspForFrameNumber(int frame) {
482 return VPX_IMG_FMT_I420;
484 return VPX_IMG_FMT_I444;
485 return VPX_IMG_FMT_I420;
488 class ResizeCspTest : public ResizeTest {
490 #if WRITE_COMPRESSED_STREAM
497 ResizeCspTest() : ResizeTest(), frame0_psnr_(0.0) {}
500 virtual ~ResizeCspTest() {}
502 virtual void BeginPassHook(unsigned int /*pass*/) {
503 #if WRITE_COMPRESSED_STREAM
504 outfile_ = fopen("vp91-2-05-cspchape.ivf", "wb");
508 virtual void EndPassHook() {
509 #if WRITE_COMPRESSED_STREAM
511 if (!fseek(outfile_, 0, SEEK_SET))
512 write_ivf_file_header(&cfg_, out_frames_, outfile_);
519 virtual void PreEncodeFrameHook(libvpx_test::VideoSource *video,
520 libvpx_test::Encoder *encoder) {
521 if (CspForFrameNumber(video->frame()) != VPX_IMG_FMT_I420 &&
522 cfg_.g_profile != 1) {
524 encoder->Config(&cfg_);
526 if (CspForFrameNumber(video->frame()) == VPX_IMG_FMT_I420 &&
527 cfg_.g_profile != 0) {
529 encoder->Config(&cfg_);
533 virtual void PSNRPktHook(const vpx_codec_cx_pkt_t *pkt) {
535 frame0_psnr_ = pkt->data.psnr.psnr[0];
536 EXPECT_NEAR(pkt->data.psnr.psnr[0], frame0_psnr_, 2.0);
539 #if WRITE_COMPRESSED_STREAM
540 virtual void FramePktHook(const vpx_codec_cx_pkt_t *pkt) {
543 // Write initial file header if first frame.
544 if (pkt->data.frame.pts == 0)
545 write_ivf_file_header(&cfg_, 0, outfile_);
547 // Write frame header and data.
548 write_ivf_frame_header(pkt, outfile_);
549 (void)fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz, outfile_);
554 #if WRITE_COMPRESSED_STREAM
556 unsigned int out_frames_;
560 class ResizingCspVideoSource : public ::libvpx_test::DummyVideoSource {
562 ResizingCspVideoSource() {
563 SetSize(kInitialWidth, kInitialHeight);
567 virtual ~ResizingCspVideoSource() {}
570 virtual void Next() {
572 SetImageFormat(CspForFrameNumber(frame_));
577 TEST_P(ResizeCspTest, TestResizeCspWorks) {
578 ResizingCspVideoSource video;
579 init_flags_ = VPX_CODEC_USE_PSNR;
580 cfg_.rc_min_quantizer = cfg_.rc_max_quantizer = 48;
581 cfg_.g_lag_in_frames = 0;
582 ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
585 VP8_INSTANTIATE_TEST_CASE(ResizeTest, ONE_PASS_TEST_MODES);
586 VP9_INSTANTIATE_TEST_CASE(ResizeTest,
587 ::testing::Values(::libvpx_test::kRealTime));
588 VP9_INSTANTIATE_TEST_CASE(ResizeInternalTest,
589 ::testing::Values(::libvpx_test::kOnePassBest));
590 VP9_INSTANTIATE_TEST_CASE(ResizeRealtimeTest,
591 ::testing::Values(::libvpx_test::kRealTime),
592 ::testing::Range(5, 9));
593 VP9_INSTANTIATE_TEST_CASE(ResizeCspTest,
594 ::testing::Values(::libvpx_test::kRealTime));