blob: 017730899b6b525f5682952720684a5538a0ac6b [file] [log] [blame]
John Koleszar2fb29ff2012-05-23 12:55:27 -07001/*
2 * Copyright (c) 2012 The WebM project authors. All Rights Reserved.
3 *
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.
9 */
10#include <climits>
11#include <vector>
John Koleszar706cafe2013-01-18 11:51:12 -080012#include "third_party/googletest/src/include/gtest/gtest.h"
13#include "test/codec_factory.h"
John Koleszar2fb29ff2012-05-23 12:55:27 -070014#include "test/encode_test_driver.h"
John Koleszar88f99f42013-02-06 12:44:20 -080015#include "test/i420_video_source.h"
John Koleszar2fb29ff2012-05-23 12:55:27 -070016#include "test/video_source.h"
John Koleszar706cafe2013-01-18 11:51:12 -080017#include "test/util.h"
John Koleszar2fb29ff2012-05-23 12:55:27 -070018
Adrian Grange88c8ff22013-09-12 09:35:04 -070019// Enable(1) or Disable(0) writing of the compressed bitstream.
20#define WRITE_COMPRESSED_STREAM 0
21
John Koleszar2fb29ff2012-05-23 12:55:27 -070022namespace {
23
Adrian Grange88c8ff22013-09-12 09:35:04 -070024#if WRITE_COMPRESSED_STREAM
25static void mem_put_le16(char *const mem, const unsigned int val) {
26 mem[0] = val;
27 mem[1] = val >> 8;
28}
29
30static void mem_put_le32(char *const mem, const unsigned int val) {
31 mem[0] = val;
32 mem[1] = val >> 8;
33 mem[2] = val >> 16;
34 mem[3] = val >> 24;
35}
36
37static void write_ivf_file_header(const vpx_codec_enc_cfg_t *const cfg,
38 int frame_cnt, FILE *const outfile) {
39 char header[32];
40
41 header[0] = 'D';
42 header[1] = 'K';
43 header[2] = 'I';
44 header[3] = 'F';
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 */
54
55 (void)fwrite(header, 1, 32, outfile);
56}
57
58static void write_ivf_frame_size(FILE *const outfile, const size_t size) {
59 char header[4];
60 mem_put_le32(header, static_cast<unsigned int>(size));
61 (void)fwrite(header, 1, 4, outfile);
62}
63
64static void write_ivf_frame_header(const vpx_codec_cx_pkt_t *const pkt,
65 FILE *const outfile) {
66 char header[12];
67 vpx_codec_pts_t pts;
68
69 if (pkt->kind != VPX_CODEC_CX_FRAME_PKT)
70 return;
71
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);
76
77 (void)fwrite(header, 1, 12, outfile);
78}
79#endif // WRITE_COMPRESSED_STREAM
80
John Koleszar2fb29ff2012-05-23 12:55:27 -070081const unsigned int kInitialWidth = 320;
82const unsigned int kInitialHeight = 240;
83
jackychen9ac42bc2015-09-15 14:17:04 -070084struct FrameInfo {
85 FrameInfo(vpx_codec_pts_t _pts, unsigned int _w, unsigned int _h)
86 : pts(_pts), w(_w), h(_h) {}
87
88 vpx_codec_pts_t pts;
89 unsigned int w;
90 unsigned int h;
91};
92
Marco3cbc26f2016-02-10 11:39:04 -080093void ScaleForFrameNumber(unsigned int frame,
94 unsigned int initial_w,
95 unsigned int initial_h,
96 unsigned int *w,
97 unsigned int *h,
98 int flag_codec) {
99 if (frame < 10) {
100 *w = initial_w;
101 *h = initial_h;
102 return;
103 }
104 if (frame < 20) {
105 *w = initial_w * 3 / 4;
106 *h = initial_h * 3 / 4;
107 return;
108 }
109 if (frame < 30) {
110 *w = initial_w / 2;
111 *h = initial_h / 2;
112 return;
113 }
114 if (frame < 40) {
115 *w = initial_w;
116 *h = initial_h;
117 return;
118 }
119 if (frame < 50) {
120 *w = initial_w * 3 / 4;
121 *h = initial_h * 3 / 4;
122 return;
123 }
124 if (frame < 60) {
125 *w = initial_w / 2;
126 *h = initial_h / 2;
127 return;
128 }
129 if (frame < 70) {
130 *w = initial_w;
131 *h = initial_h;
132 return;
133 }
134 if (frame < 80) {
135 *w = initial_w * 3 / 4;
136 *h = initial_h * 3 / 4;
137 return;
138 }
139 if (frame < 90) {
140 *w = initial_w / 2;
141 *h = initial_h / 2;
142 return;
143 }
144 if (frame < 100) {
145 *w = initial_w * 3 / 4;
146 *h = initial_h * 3 / 4;
147 return;
148 }
149 if (frame < 110) {
150 *w = initial_w;
151 *h = initial_h;
152 return;
153 }
154 if (frame < 120) {
155 *w = initial_w * 3 / 4;
156 *h = initial_h * 3 / 4;
157 return;
158 }
159 if (frame < 130) {
160 *w = initial_w / 2;
161 *h = initial_h / 2;
162 return;
163 }
164 if (frame < 140) {
165 *w = initial_w * 3 / 4;
166 *h = initial_h * 3 / 4;
167 return;
168 }
169 if (frame < 150) {
170 *w = initial_w;
171 *h = initial_h;
172 return;
173 }
174 if (frame < 160) {
175 *w = initial_w * 3 / 4;
176 *h = initial_h * 3 / 4;
177 return;
178 }
179 if (frame < 170) {
180 *w = initial_w / 2;
181 *h = initial_h / 2;
182 return;
183 }
184 if (frame < 180) {
185 *w = initial_w * 3 / 4;
186 *h = initial_h * 3 / 4;
187 return;
188 }
189 if (frame < 190) {
190 *w = initial_w;
191 *h = initial_h;
192 return;
193 }
194 if (frame < 200) {
195 *w = initial_w * 3 / 4;
196 *h = initial_h * 3 / 4;
197 return;
198 }
199 if (frame < 210) {
200 *w = initial_w / 2;
201 *h = initial_h / 2;
202 return;
203 }
204 if (frame < 220) {
205 *w = initial_w * 3 / 4;
206 *h = initial_h * 3 / 4;
207 return;
208 }
209 if (frame < 230) {
210 *w = initial_w;
211 *h = initial_h;
212 return;
213 }
214 if (frame < 240) {
215 *w = initial_w * 3 / 4;
216 *h = initial_h * 3 / 4;
217 return;
218 }
219 if (frame < 250) {
220 *w = initial_w / 2;
221 *h = initial_h / 2;
222 return;
223 }
224 if (frame < 260) {
225 *w = initial_w;
226 *h = initial_h;
227 return;
228 }
229 // Go down very low.
230 if (frame < 270) {
231 *w = initial_w / 4;
232 *h = initial_h / 4;
233 return;
234 }
235 if (flag_codec == 1) {
236 // Cases that only works for VP9.
237 // For VP9: Swap width and height of original.
238 if (frame < 320) {
239 *w = initial_h;
240 *h = initial_w;
241 return;
242 }
243 }
244 *w = initial_w;
245 *h = initial_h;
John Koleszar2fb29ff2012-05-23 12:55:27 -0700246}
247
248class ResizingVideoSource : public ::libvpx_test::DummyVideoSource {
249 public:
250 ResizingVideoSource() {
251 SetSize(kInitialWidth, kInitialHeight);
Marco3cbc26f2016-02-10 11:39:04 -0800252 limit_ = 350;
John Koleszar2fb29ff2012-05-23 12:55:27 -0700253 }
Marco3cbc26f2016-02-10 11:39:04 -0800254 int flag_codec_;
Adrian Grange88c8ff22013-09-12 09:35:04 -0700255 virtual ~ResizingVideoSource() {}
256
John Koleszar2fb29ff2012-05-23 12:55:27 -0700257 protected:
258 virtual void Next() {
259 ++frame_;
Marco3cbc26f2016-02-10 11:39:04 -0800260 unsigned int width;
261 unsigned int height;
262 ScaleForFrameNumber(frame_, kInitialWidth, kInitialHeight, &width, &height,
263 flag_codec_);
264 SetSize(width, height);
John Koleszar2fb29ff2012-05-23 12:55:27 -0700265 FillFrame();
266 }
267};
268
269class ResizeTest : public ::libvpx_test::EncoderTest,
John Koleszar706cafe2013-01-18 11:51:12 -0800270 public ::libvpx_test::CodecTestWithParam<libvpx_test::TestMode> {
John Koleszar2fb29ff2012-05-23 12:55:27 -0700271 protected:
John Koleszar706cafe2013-01-18 11:51:12 -0800272 ResizeTest() : EncoderTest(GET_PARAM(0)) {}
273
Adrian Grange88c8ff22013-09-12 09:35:04 -0700274 virtual ~ResizeTest() {}
275
John Koleszar2fb29ff2012-05-23 12:55:27 -0700276 virtual void SetUp() {
277 InitializeConfig();
John Koleszar706cafe2013-01-18 11:51:12 -0800278 SetMode(GET_PARAM(1));
John Koleszar2fb29ff2012-05-23 12:55:27 -0700279 }
280
John Koleszar88f99f42013-02-06 12:44:20 -0800281 virtual void DecompressedFrameHook(const vpx_image_t &img,
282 vpx_codec_pts_t pts) {
283 frame_info_list_.push_back(FrameInfo(pts, img.d_w, img.d_h));
John Koleszar2fb29ff2012-05-23 12:55:27 -0700284 }
285
286 std::vector< FrameInfo > frame_info_list_;
287};
288
289TEST_P(ResizeTest, TestExternalResizeWorks) {
290 ResizingVideoSource video;
Marco3cbc26f2016-02-10 11:39:04 -0800291 video.flag_codec_ = 0;
Alex Converse910ca852015-01-12 16:26:05 -0800292 cfg_.g_lag_in_frames = 0;
John Koleszar2fb29ff2012-05-23 12:55:27 -0700293 ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
294
James Zernbb061382014-01-31 20:11:55 -0800295 for (std::vector<FrameInfo>::const_iterator info = frame_info_list_.begin();
John Koleszar2fb29ff2012-05-23 12:55:27 -0700296 info != frame_info_list_.end(); ++info) {
James Zern17b89932014-01-31 20:10:28 -0800297 const unsigned int frame = static_cast<unsigned>(info->pts);
Marco3cbc26f2016-02-10 11:39:04 -0800298 unsigned int expected_w;
299 unsigned int expected_h;
300 ScaleForFrameNumber(frame, kInitialWidth, kInitialHeight,
301 &expected_w, &expected_h, 0);
John Koleszar2fb29ff2012-05-23 12:55:27 -0700302 EXPECT_EQ(expected_w, info->w)
Alex Converse910ca852015-01-12 16:26:05 -0800303 << "Frame " << frame << " had unexpected width";
John Koleszar2fb29ff2012-05-23 12:55:27 -0700304 EXPECT_EQ(expected_h, info->h)
Alex Converse910ca852015-01-12 16:26:05 -0800305 << "Frame " << frame << " had unexpected height";
John Koleszar2fb29ff2012-05-23 12:55:27 -0700306 }
307}
308
Adrian Grange88c8ff22013-09-12 09:35:04 -0700309const unsigned int kStepDownFrame = 3;
310const unsigned int kStepUpFrame = 6;
311
John Koleszar88f99f42013-02-06 12:44:20 -0800312class ResizeInternalTest : public ResizeTest {
313 protected:
Adrian Grange88c8ff22013-09-12 09:35:04 -0700314#if WRITE_COMPRESSED_STREAM
315 ResizeInternalTest()
316 : ResizeTest(),
317 frame0_psnr_(0.0),
318 outfile_(NULL),
319 out_frames_(0) {}
320#else
John Koleszarb683eec2013-02-21 10:38:27 -0800321 ResizeInternalTest() : ResizeTest(), frame0_psnr_(0.0) {}
Adrian Grange88c8ff22013-09-12 09:35:04 -0700322#endif
323
324 virtual ~ResizeInternalTest() {}
325
326 virtual void BeginPassHook(unsigned int /*pass*/) {
327#if WRITE_COMPRESSED_STREAM
328 outfile_ = fopen("vp90-2-05-resize.ivf", "wb");
329#endif
330 }
331
332 virtual void EndPassHook() {
333#if WRITE_COMPRESSED_STREAM
334 if (outfile_) {
335 if (!fseek(outfile_, 0, SEEK_SET))
336 write_ivf_file_header(&cfg_, out_frames_, outfile_);
337 fclose(outfile_);
338 outfile_ = NULL;
339 }
340#endif
341 }
John Koleszar88f99f42013-02-06 12:44:20 -0800342
343 virtual void PreEncodeFrameHook(libvpx_test::VideoSource *video,
344 libvpx_test::Encoder *encoder) {
jackychen55f092d2015-09-21 09:37:46 -0700345 if (change_config_) {
346 int new_q = 60;
347 if (video->frame() == 0) {
348 struct vpx_scaling_mode mode = {VP8E_ONETWO, VP8E_ONETWO};
349 encoder->Control(VP8E_SET_SCALEMODE, &mode);
350 }
351 if (video->frame() == 1) {
352 struct vpx_scaling_mode mode = {VP8E_NORMAL, VP8E_NORMAL};
353 encoder->Control(VP8E_SET_SCALEMODE, &mode);
354 cfg_.rc_min_quantizer = cfg_.rc_max_quantizer = new_q;
355 encoder->Config(&cfg_);
356 }
357 } else {
358 if (video->frame() == kStepDownFrame) {
359 struct vpx_scaling_mode mode = {VP8E_FOURFIVE, VP8E_THREEFIVE};
360 encoder->Control(VP8E_SET_SCALEMODE, &mode);
361 }
362 if (video->frame() == kStepUpFrame) {
363 struct vpx_scaling_mode mode = {VP8E_NORMAL, VP8E_NORMAL};
364 encoder->Control(VP8E_SET_SCALEMODE, &mode);
365 }
John Koleszarb683eec2013-02-21 10:38:27 -0800366 }
John Koleszar88f99f42013-02-06 12:44:20 -0800367 }
John Koleszarb683eec2013-02-21 10:38:27 -0800368
369 virtual void PSNRPktHook(const vpx_codec_cx_pkt_t *pkt) {
370 if (!frame0_psnr_)
371 frame0_psnr_ = pkt->data.psnr.psnr[0];
Deb Mukherjee3cd37df2014-01-17 12:56:36 -0800372 EXPECT_NEAR(pkt->data.psnr.psnr[0], frame0_psnr_, 2.0);
John Koleszarb683eec2013-02-21 10:38:27 -0800373 }
374
Adrian Grange88c8ff22013-09-12 09:35:04 -0700375#if WRITE_COMPRESSED_STREAM
James Zerndbe69172014-08-22 12:11:42 -0700376 virtual void FramePktHook(const vpx_codec_cx_pkt_t *pkt) {
Adrian Grange88c8ff22013-09-12 09:35:04 -0700377 ++out_frames_;
378
379 // Write initial file header if first frame.
380 if (pkt->data.frame.pts == 0)
381 write_ivf_file_header(&cfg_, 0, outfile_);
382
383 // Write frame header and data.
384 write_ivf_frame_header(pkt, outfile_);
385 (void)fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz, outfile_);
Adrian Grange88c8ff22013-09-12 09:35:04 -0700386 }
James Zerndbe69172014-08-22 12:11:42 -0700387#endif
Adrian Grange88c8ff22013-09-12 09:35:04 -0700388
John Koleszarb683eec2013-02-21 10:38:27 -0800389 double frame0_psnr_;
jackychen55f092d2015-09-21 09:37:46 -0700390 bool change_config_;
Adrian Grange88c8ff22013-09-12 09:35:04 -0700391#if WRITE_COMPRESSED_STREAM
392 FILE *outfile_;
393 unsigned int out_frames_;
394#endif
John Koleszar88f99f42013-02-06 12:44:20 -0800395};
396
397TEST_P(ResizeInternalTest, TestInternalResizeWorks) {
398 ::libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
John Koleszarb683eec2013-02-21 10:38:27 -0800399 30, 1, 0, 10);
400 init_flags_ = VPX_CODEC_USE_PSNR;
jackychen55f092d2015-09-21 09:37:46 -0700401 change_config_ = false;
Adrian Grange93ffd372013-09-10 12:02:37 -0700402
Adrian Grange88c8ff22013-09-12 09:35:04 -0700403 // q picked such that initial keyframe on this clip is ~30dB PSNR
404 cfg_.rc_min_quantizer = cfg_.rc_max_quantizer = 48;
405
Adrian Grange93ffd372013-09-10 12:02:37 -0700406 // If the number of frames being encoded is smaller than g_lag_in_frames
407 // the encoded frame is unavailable using the current API. Comparing
408 // frames to detect mismatch would then not be possible. Set
409 // g_lag_in_frames = 0 to get around this.
410 cfg_.g_lag_in_frames = 0;
John Koleszar88f99f42013-02-06 12:44:20 -0800411 ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
412
James Zernbb061382014-01-31 20:11:55 -0800413 for (std::vector<FrameInfo>::const_iterator info = frame_info_list_.begin();
John Koleszar88f99f42013-02-06 12:44:20 -0800414 info != frame_info_list_.end(); ++info) {
415 const vpx_codec_pts_t pts = info->pts;
Adrian Grange88c8ff22013-09-12 09:35:04 -0700416 if (pts >= kStepDownFrame && pts < kStepUpFrame) {
John Koleszar88f99f42013-02-06 12:44:20 -0800417 ASSERT_EQ(282U, info->w) << "Frame " << pts << " had unexpected width";
418 ASSERT_EQ(173U, info->h) << "Frame " << pts << " had unexpected height";
419 } else {
420 EXPECT_EQ(352U, info->w) << "Frame " << pts << " had unexpected width";
421 EXPECT_EQ(288U, info->h) << "Frame " << pts << " had unexpected height";
422 }
423 }
424}
425
jackychen55f092d2015-09-21 09:37:46 -0700426TEST_P(ResizeInternalTest, TestInternalResizeChangeConfig) {
427 ::libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
428 30, 1, 0, 10);
429 cfg_.g_w = 352;
430 cfg_.g_h = 288;
431 change_config_ = true;
432 ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
433}
434
jackychen55c88432015-11-09 14:58:14 -0800435class ResizeRealtimeTest : public ::libvpx_test::EncoderTest,
jackychen9ac42bc2015-09-15 14:17:04 -0700436 public ::libvpx_test::CodecTestWith2Params<libvpx_test::TestMode, int> {
437 protected:
jackychen55c88432015-11-09 14:58:14 -0800438 ResizeRealtimeTest() : EncoderTest(GET_PARAM(0)) {}
439 virtual ~ResizeRealtimeTest() {}
jackychen9ac42bc2015-09-15 14:17:04 -0700440
441 virtual void PreEncodeFrameHook(libvpx_test::VideoSource *video,
442 libvpx_test::Encoder *encoder) {
443 if (video->frame() == 0) {
444 encoder->Control(VP9E_SET_AQ_MODE, 3);
445 encoder->Control(VP8E_SET_CPUUSED, set_cpu_used_);
446 }
jackychenca8f8fd2015-09-15 15:47:11 -0700447
448 if (change_bitrate_ && video->frame() == 120) {
449 change_bitrate_ = false;
450 cfg_.rc_target_bitrate = 500;
451 encoder->Config(&cfg_);
452 }
jackychen9ac42bc2015-09-15 14:17:04 -0700453 }
454
455 virtual void SetUp() {
456 InitializeConfig();
457 SetMode(GET_PARAM(1));
458 set_cpu_used_ = GET_PARAM(2);
459 }
460
461 virtual void DecompressedFrameHook(const vpx_image_t &img,
462 vpx_codec_pts_t pts) {
463 frame_info_list_.push_back(FrameInfo(pts, img.d_w, img.d_h));
464 }
465
466 void DefaultConfig() {
jackychen9ac42bc2015-09-15 14:17:04 -0700467 cfg_.rc_buf_initial_sz = 500;
468 cfg_.rc_buf_optimal_sz = 600;
469 cfg_.rc_buf_sz = 1000;
470 cfg_.rc_min_quantizer = 2;
471 cfg_.rc_max_quantizer = 56;
472 cfg_.rc_undershoot_pct = 50;
473 cfg_.rc_overshoot_pct = 50;
474 cfg_.rc_end_usage = VPX_CBR;
475 cfg_.kf_mode = VPX_KF_AUTO;
476 cfg_.g_lag_in_frames = 0;
477 cfg_.kf_min_dist = cfg_.kf_max_dist = 3000;
478 // Enable dropped frames.
479 cfg_.rc_dropframe_thresh = 1;
480 // Enable error_resilience mode.
481 cfg_.g_error_resilient = 1;
482 // Enable dynamic resizing.
483 cfg_.rc_resize_allowed = 1;
484 // Run at low bitrate.
485 cfg_.rc_target_bitrate = 200;
486 }
487
488 std::vector< FrameInfo > frame_info_list_;
489 int set_cpu_used_;
jackychenca8f8fd2015-09-15 15:47:11 -0700490 bool change_bitrate_;
jackychen9ac42bc2015-09-15 14:17:04 -0700491};
492
jackychen55c88432015-11-09 14:58:14 -0800493TEST_P(ResizeRealtimeTest, TestExternalResizeWorks) {
494 ResizingVideoSource video;
Marco3cbc26f2016-02-10 11:39:04 -0800495 video.flag_codec_ = 1;
jackychen55c88432015-11-09 14:58:14 -0800496 DefaultConfig();
Marco34d12d12016-02-10 16:59:09 -0800497 // Disable internal resize for this test.
498 cfg_.rc_resize_allowed = 0;
jackychen55c88432015-11-09 14:58:14 -0800499 change_bitrate_ = false;
500 ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
501
502 for (std::vector<FrameInfo>::const_iterator info = frame_info_list_.begin();
503 info != frame_info_list_.end(); ++info) {
504 const unsigned int frame = static_cast<unsigned>(info->pts);
Marco3cbc26f2016-02-10 11:39:04 -0800505 unsigned int expected_w;
506 unsigned int expected_h;
507 ScaleForFrameNumber(frame, kInitialWidth, kInitialHeight,
508 &expected_w, &expected_h, 1);
jackychen55c88432015-11-09 14:58:14 -0800509 EXPECT_EQ(expected_w, info->w)
510 << "Frame " << frame << " had unexpected width";
511 EXPECT_EQ(expected_h, info->h)
512 << "Frame " << frame << " had unexpected height";
513 }
514}
515
jackychen9ac42bc2015-09-15 14:17:04 -0700516// Verify the dynamic resizer behavior for real time, 1 pass CBR mode.
517// Run at low bitrate, with resize_allowed = 1, and verify that we get
518// one resize down event.
jackychen55c88432015-11-09 14:58:14 -0800519TEST_P(ResizeRealtimeTest, TestInternalResizeDown) {
jackychen9ac42bc2015-09-15 14:17:04 -0700520 ::libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
521 30, 1, 0, 299);
522 DefaultConfig();
jackychen55c88432015-11-09 14:58:14 -0800523 cfg_.g_w = 352;
524 cfg_.g_h = 288;
jackychenca8f8fd2015-09-15 15:47:11 -0700525 change_bitrate_ = false;
jackychen9ac42bc2015-09-15 14:17:04 -0700526 ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
527
528 unsigned int last_w = cfg_.g_w;
529 unsigned int last_h = cfg_.g_h;
530 int resize_count = 0;
531 for (std::vector<FrameInfo>::const_iterator info = frame_info_list_.begin();
532 info != frame_info_list_.end(); ++info) {
533 if (info->w != last_w || info->h != last_h) {
534 // Verify that resize down occurs.
535 ASSERT_LT(info->w, last_w);
536 ASSERT_LT(info->h, last_h);
537 last_w = info->w;
538 last_h = info->h;
539 resize_count++;
540 }
541 }
542
543 // Verify that we get 1 resize down event in this test.
544 ASSERT_EQ(1, resize_count) << "Resizing should occur.";
545}
546
jackychenca8f8fd2015-09-15 15:47:11 -0700547// Verify the dynamic resizer behavior for real time, 1 pass CBR mode.
548// Start at low target bitrate, raise the bitrate in the middle of the clip,
549// scaling-up should occur after bitrate changed.
jackychen55c88432015-11-09 14:58:14 -0800550TEST_P(ResizeRealtimeTest, TestInternalResizeDownUpChangeBitRate) {
jackychenca8f8fd2015-09-15 15:47:11 -0700551 ::libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
jackychen0465aa42015-11-09 14:04:58 -0800552 30, 1, 0, 359);
jackychenca8f8fd2015-09-15 15:47:11 -0700553 DefaultConfig();
jackychen55c88432015-11-09 14:58:14 -0800554 cfg_.g_w = 352;
555 cfg_.g_h = 288;
jackychenca8f8fd2015-09-15 15:47:11 -0700556 change_bitrate_ = true;
557 // Disable dropped frames.
558 cfg_.rc_dropframe_thresh = 0;
559 // Starting bitrate low.
jackychen204cde52015-11-13 16:02:43 -0800560 cfg_.rc_target_bitrate = 80;
jackychenca8f8fd2015-09-15 15:47:11 -0700561 ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
562
563 unsigned int last_w = cfg_.g_w;
564 unsigned int last_h = cfg_.g_h;
565 int resize_count = 0;
566 for (std::vector<FrameInfo>::const_iterator info = frame_info_list_.begin();
567 info != frame_info_list_.end(); ++info) {
568 if (info->w != last_w || info->h != last_h) {
569 resize_count++;
570 if (resize_count == 1) {
571 // Verify that resize down occurs.
572 ASSERT_LT(info->w, last_w);
573 ASSERT_LT(info->h, last_h);
574 } else if (resize_count == 2) {
575 // Verify that resize up occurs.
576 ASSERT_GT(info->w, last_w);
577 ASSERT_GT(info->h, last_h);
578 }
579 last_w = info->w;
580 last_h = info->h;
581 }
582 }
583
jackychen204cde52015-11-13 16:02:43 -0800584 // Verify that we get 2 resize events in this test.
585 ASSERT_EQ(resize_count, 2) << "Resizing should occur twice.";
jackychenca8f8fd2015-09-15 15:47:11 -0700586}
587
Alex Converse797a2552015-01-15 13:56:55 -0800588vpx_img_fmt_t CspForFrameNumber(int frame) {
589 if (frame < 10)
590 return VPX_IMG_FMT_I420;
591 if (frame < 20)
592 return VPX_IMG_FMT_I444;
593 return VPX_IMG_FMT_I420;
594}
595
596class ResizeCspTest : public ResizeTest {
597 protected:
598#if WRITE_COMPRESSED_STREAM
599 ResizeCspTest()
600 : ResizeTest(),
601 frame0_psnr_(0.0),
602 outfile_(NULL),
603 out_frames_(0) {}
604#else
605 ResizeCspTest() : ResizeTest(), frame0_psnr_(0.0) {}
606#endif
607
608 virtual ~ResizeCspTest() {}
609
610 virtual void BeginPassHook(unsigned int /*pass*/) {
611#if WRITE_COMPRESSED_STREAM
612 outfile_ = fopen("vp91-2-05-cspchape.ivf", "wb");
613#endif
614 }
615
616 virtual void EndPassHook() {
617#if WRITE_COMPRESSED_STREAM
618 if (outfile_) {
619 if (!fseek(outfile_, 0, SEEK_SET))
620 write_ivf_file_header(&cfg_, out_frames_, outfile_);
621 fclose(outfile_);
622 outfile_ = NULL;
623 }
624#endif
625 }
626
627 virtual void PreEncodeFrameHook(libvpx_test::VideoSource *video,
628 libvpx_test::Encoder *encoder) {
629 if (CspForFrameNumber(video->frame()) != VPX_IMG_FMT_I420 &&
630 cfg_.g_profile != 1) {
631 cfg_.g_profile = 1;
632 encoder->Config(&cfg_);
633 }
634 if (CspForFrameNumber(video->frame()) == VPX_IMG_FMT_I420 &&
635 cfg_.g_profile != 0) {
636 cfg_.g_profile = 0;
637 encoder->Config(&cfg_);
638 }
639 }
640
641 virtual void PSNRPktHook(const vpx_codec_cx_pkt_t *pkt) {
642 if (!frame0_psnr_)
643 frame0_psnr_ = pkt->data.psnr.psnr[0];
644 EXPECT_NEAR(pkt->data.psnr.psnr[0], frame0_psnr_, 2.0);
645 }
646
647#if WRITE_COMPRESSED_STREAM
648 virtual void FramePktHook(const vpx_codec_cx_pkt_t *pkt) {
649 ++out_frames_;
650
651 // Write initial file header if first frame.
652 if (pkt->data.frame.pts == 0)
653 write_ivf_file_header(&cfg_, 0, outfile_);
654
655 // Write frame header and data.
656 write_ivf_frame_header(pkt, outfile_);
657 (void)fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz, outfile_);
658 }
659#endif
660
661 double frame0_psnr_;
662#if WRITE_COMPRESSED_STREAM
663 FILE *outfile_;
664 unsigned int out_frames_;
665#endif
666};
667
668class ResizingCspVideoSource : public ::libvpx_test::DummyVideoSource {
669 public:
670 ResizingCspVideoSource() {
671 SetSize(kInitialWidth, kInitialHeight);
672 limit_ = 30;
673 }
674
675 virtual ~ResizingCspVideoSource() {}
676
677 protected:
678 virtual void Next() {
679 ++frame_;
680 SetImageFormat(CspForFrameNumber(frame_));
681 FillFrame();
682 }
683};
684
685TEST_P(ResizeCspTest, TestResizeCspWorks) {
686 ResizingCspVideoSource video;
James Zern40f177a2015-02-26 20:31:59 -0800687 init_flags_ = VPX_CODEC_USE_PSNR;
Alex Converse797a2552015-01-15 13:56:55 -0800688 cfg_.rc_min_quantizer = cfg_.rc_max_quantizer = 48;
689 cfg_.g_lag_in_frames = 0;
690 ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
691}
692
John Koleszar706cafe2013-01-18 11:51:12 -0800693VP8_INSTANTIATE_TEST_CASE(ResizeTest, ONE_PASS_TEST_MODES);
Alex Converse910ca852015-01-12 16:26:05 -0800694VP9_INSTANTIATE_TEST_CASE(ResizeTest,
695 ::testing::Values(::libvpx_test::kRealTime));
John Koleszar88f99f42013-02-06 12:44:20 -0800696VP9_INSTANTIATE_TEST_CASE(ResizeInternalTest,
697 ::testing::Values(::libvpx_test::kOnePassBest));
jackychen55c88432015-11-09 14:58:14 -0800698VP9_INSTANTIATE_TEST_CASE(ResizeRealtimeTest,
jackychen9ac42bc2015-09-15 14:17:04 -0700699 ::testing::Values(::libvpx_test::kRealTime),
700 ::testing::Range(5, 9));
Alex Converse797a2552015-01-15 13:56:55 -0800701VP9_INSTANTIATE_TEST_CASE(ResizeCspTest,
702 ::testing::Values(::libvpx_test::kRealTime));
John Koleszar2fb29ff2012-05-23 12:55:27 -0700703} // namespace