John Koleszar | 2fb29ff | 2012-05-23 12:55:27 -0700 | [diff] [blame] | 1 | /* |
| 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 Koleszar | 706cafe | 2013-01-18 11:51:12 -0800 | [diff] [blame] | 12 | #include "third_party/googletest/src/include/gtest/gtest.h" |
| 13 | #include "test/codec_factory.h" |
John Koleszar | 2fb29ff | 2012-05-23 12:55:27 -0700 | [diff] [blame] | 14 | #include "test/encode_test_driver.h" |
John Koleszar | 88f99f4 | 2013-02-06 12:44:20 -0800 | [diff] [blame] | 15 | #include "test/i420_video_source.h" |
John Koleszar | 2fb29ff | 2012-05-23 12:55:27 -0700 | [diff] [blame] | 16 | #include "test/video_source.h" |
John Koleszar | 706cafe | 2013-01-18 11:51:12 -0800 | [diff] [blame] | 17 | #include "test/util.h" |
John Koleszar | 2fb29ff | 2012-05-23 12:55:27 -0700 | [diff] [blame] | 18 | |
Adrian Grange | 88c8ff2 | 2013-09-12 09:35:04 -0700 | [diff] [blame] | 19 | // Enable(1) or Disable(0) writing of the compressed bitstream. |
| 20 | #define WRITE_COMPRESSED_STREAM 0 |
| 21 | |
John Koleszar | 2fb29ff | 2012-05-23 12:55:27 -0700 | [diff] [blame] | 22 | namespace { |
| 23 | |
Adrian Grange | 88c8ff2 | 2013-09-12 09:35:04 -0700 | [diff] [blame] | 24 | #if WRITE_COMPRESSED_STREAM |
| 25 | static void mem_put_le16(char *const mem, const unsigned int val) { |
| 26 | mem[0] = val; |
| 27 | mem[1] = val >> 8; |
| 28 | } |
| 29 | |
| 30 | static 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 | |
| 37 | static 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 | |
| 58 | static 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 | |
| 64 | static 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 Koleszar | 2fb29ff | 2012-05-23 12:55:27 -0700 | [diff] [blame] | 81 | const unsigned int kInitialWidth = 320; |
| 82 | const unsigned int kInitialHeight = 240; |
| 83 | |
jackychen | 9ac42bc | 2015-09-15 14:17:04 -0700 | [diff] [blame] | 84 | struct 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 | |
Marco | 3cbc26f | 2016-02-10 11:39:04 -0800 | [diff] [blame] | 93 | void 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 Koleszar | 2fb29ff | 2012-05-23 12:55:27 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | class ResizingVideoSource : public ::libvpx_test::DummyVideoSource { |
| 249 | public: |
| 250 | ResizingVideoSource() { |
| 251 | SetSize(kInitialWidth, kInitialHeight); |
Marco | 3cbc26f | 2016-02-10 11:39:04 -0800 | [diff] [blame] | 252 | limit_ = 350; |
John Koleszar | 2fb29ff | 2012-05-23 12:55:27 -0700 | [diff] [blame] | 253 | } |
Marco | 3cbc26f | 2016-02-10 11:39:04 -0800 | [diff] [blame] | 254 | int flag_codec_; |
Adrian Grange | 88c8ff2 | 2013-09-12 09:35:04 -0700 | [diff] [blame] | 255 | virtual ~ResizingVideoSource() {} |
| 256 | |
John Koleszar | 2fb29ff | 2012-05-23 12:55:27 -0700 | [diff] [blame] | 257 | protected: |
| 258 | virtual void Next() { |
| 259 | ++frame_; |
Marco | 3cbc26f | 2016-02-10 11:39:04 -0800 | [diff] [blame] | 260 | unsigned int width; |
| 261 | unsigned int height; |
| 262 | ScaleForFrameNumber(frame_, kInitialWidth, kInitialHeight, &width, &height, |
| 263 | flag_codec_); |
| 264 | SetSize(width, height); |
John Koleszar | 2fb29ff | 2012-05-23 12:55:27 -0700 | [diff] [blame] | 265 | FillFrame(); |
| 266 | } |
| 267 | }; |
| 268 | |
| 269 | class ResizeTest : public ::libvpx_test::EncoderTest, |
John Koleszar | 706cafe | 2013-01-18 11:51:12 -0800 | [diff] [blame] | 270 | public ::libvpx_test::CodecTestWithParam<libvpx_test::TestMode> { |
John Koleszar | 2fb29ff | 2012-05-23 12:55:27 -0700 | [diff] [blame] | 271 | protected: |
John Koleszar | 706cafe | 2013-01-18 11:51:12 -0800 | [diff] [blame] | 272 | ResizeTest() : EncoderTest(GET_PARAM(0)) {} |
| 273 | |
Adrian Grange | 88c8ff2 | 2013-09-12 09:35:04 -0700 | [diff] [blame] | 274 | virtual ~ResizeTest() {} |
| 275 | |
John Koleszar | 2fb29ff | 2012-05-23 12:55:27 -0700 | [diff] [blame] | 276 | virtual void SetUp() { |
| 277 | InitializeConfig(); |
John Koleszar | 706cafe | 2013-01-18 11:51:12 -0800 | [diff] [blame] | 278 | SetMode(GET_PARAM(1)); |
John Koleszar | 2fb29ff | 2012-05-23 12:55:27 -0700 | [diff] [blame] | 279 | } |
| 280 | |
John Koleszar | 88f99f4 | 2013-02-06 12:44:20 -0800 | [diff] [blame] | 281 | 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 Koleszar | 2fb29ff | 2012-05-23 12:55:27 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | std::vector< FrameInfo > frame_info_list_; |
| 287 | }; |
| 288 | |
| 289 | TEST_P(ResizeTest, TestExternalResizeWorks) { |
| 290 | ResizingVideoSource video; |
Marco | 3cbc26f | 2016-02-10 11:39:04 -0800 | [diff] [blame] | 291 | video.flag_codec_ = 0; |
Alex Converse | 910ca85 | 2015-01-12 16:26:05 -0800 | [diff] [blame] | 292 | cfg_.g_lag_in_frames = 0; |
John Koleszar | 2fb29ff | 2012-05-23 12:55:27 -0700 | [diff] [blame] | 293 | ASSERT_NO_FATAL_FAILURE(RunLoop(&video)); |
| 294 | |
James Zern | bb06138 | 2014-01-31 20:11:55 -0800 | [diff] [blame] | 295 | for (std::vector<FrameInfo>::const_iterator info = frame_info_list_.begin(); |
John Koleszar | 2fb29ff | 2012-05-23 12:55:27 -0700 | [diff] [blame] | 296 | info != frame_info_list_.end(); ++info) { |
James Zern | 17b8993 | 2014-01-31 20:10:28 -0800 | [diff] [blame] | 297 | const unsigned int frame = static_cast<unsigned>(info->pts); |
Marco | 3cbc26f | 2016-02-10 11:39:04 -0800 | [diff] [blame] | 298 | unsigned int expected_w; |
| 299 | unsigned int expected_h; |
| 300 | ScaleForFrameNumber(frame, kInitialWidth, kInitialHeight, |
| 301 | &expected_w, &expected_h, 0); |
John Koleszar | 2fb29ff | 2012-05-23 12:55:27 -0700 | [diff] [blame] | 302 | EXPECT_EQ(expected_w, info->w) |
Alex Converse | 910ca85 | 2015-01-12 16:26:05 -0800 | [diff] [blame] | 303 | << "Frame " << frame << " had unexpected width"; |
John Koleszar | 2fb29ff | 2012-05-23 12:55:27 -0700 | [diff] [blame] | 304 | EXPECT_EQ(expected_h, info->h) |
Alex Converse | 910ca85 | 2015-01-12 16:26:05 -0800 | [diff] [blame] | 305 | << "Frame " << frame << " had unexpected height"; |
John Koleszar | 2fb29ff | 2012-05-23 12:55:27 -0700 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | |
Adrian Grange | 88c8ff2 | 2013-09-12 09:35:04 -0700 | [diff] [blame] | 309 | const unsigned int kStepDownFrame = 3; |
| 310 | const unsigned int kStepUpFrame = 6; |
| 311 | |
John Koleszar | 88f99f4 | 2013-02-06 12:44:20 -0800 | [diff] [blame] | 312 | class ResizeInternalTest : public ResizeTest { |
| 313 | protected: |
Adrian Grange | 88c8ff2 | 2013-09-12 09:35:04 -0700 | [diff] [blame] | 314 | #if WRITE_COMPRESSED_STREAM |
| 315 | ResizeInternalTest() |
| 316 | : ResizeTest(), |
| 317 | frame0_psnr_(0.0), |
| 318 | outfile_(NULL), |
| 319 | out_frames_(0) {} |
| 320 | #else |
John Koleszar | b683eec | 2013-02-21 10:38:27 -0800 | [diff] [blame] | 321 | ResizeInternalTest() : ResizeTest(), frame0_psnr_(0.0) {} |
Adrian Grange | 88c8ff2 | 2013-09-12 09:35:04 -0700 | [diff] [blame] | 322 | #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 Koleszar | 88f99f4 | 2013-02-06 12:44:20 -0800 | [diff] [blame] | 342 | |
| 343 | virtual void PreEncodeFrameHook(libvpx_test::VideoSource *video, |
| 344 | libvpx_test::Encoder *encoder) { |
jackychen | 55f092d | 2015-09-21 09:37:46 -0700 | [diff] [blame] | 345 | 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 Koleszar | b683eec | 2013-02-21 10:38:27 -0800 | [diff] [blame] | 366 | } |
John Koleszar | 88f99f4 | 2013-02-06 12:44:20 -0800 | [diff] [blame] | 367 | } |
John Koleszar | b683eec | 2013-02-21 10:38:27 -0800 | [diff] [blame] | 368 | |
| 369 | virtual void PSNRPktHook(const vpx_codec_cx_pkt_t *pkt) { |
| 370 | if (!frame0_psnr_) |
| 371 | frame0_psnr_ = pkt->data.psnr.psnr[0]; |
Deb Mukherjee | 3cd37df | 2014-01-17 12:56:36 -0800 | [diff] [blame] | 372 | EXPECT_NEAR(pkt->data.psnr.psnr[0], frame0_psnr_, 2.0); |
John Koleszar | b683eec | 2013-02-21 10:38:27 -0800 | [diff] [blame] | 373 | } |
| 374 | |
Adrian Grange | 88c8ff2 | 2013-09-12 09:35:04 -0700 | [diff] [blame] | 375 | #if WRITE_COMPRESSED_STREAM |
James Zern | dbe6917 | 2014-08-22 12:11:42 -0700 | [diff] [blame] | 376 | virtual void FramePktHook(const vpx_codec_cx_pkt_t *pkt) { |
Adrian Grange | 88c8ff2 | 2013-09-12 09:35:04 -0700 | [diff] [blame] | 377 | ++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 Grange | 88c8ff2 | 2013-09-12 09:35:04 -0700 | [diff] [blame] | 386 | } |
James Zern | dbe6917 | 2014-08-22 12:11:42 -0700 | [diff] [blame] | 387 | #endif |
Adrian Grange | 88c8ff2 | 2013-09-12 09:35:04 -0700 | [diff] [blame] | 388 | |
John Koleszar | b683eec | 2013-02-21 10:38:27 -0800 | [diff] [blame] | 389 | double frame0_psnr_; |
jackychen | 55f092d | 2015-09-21 09:37:46 -0700 | [diff] [blame] | 390 | bool change_config_; |
Adrian Grange | 88c8ff2 | 2013-09-12 09:35:04 -0700 | [diff] [blame] | 391 | #if WRITE_COMPRESSED_STREAM |
| 392 | FILE *outfile_; |
| 393 | unsigned int out_frames_; |
| 394 | #endif |
John Koleszar | 88f99f4 | 2013-02-06 12:44:20 -0800 | [diff] [blame] | 395 | }; |
| 396 | |
| 397 | TEST_P(ResizeInternalTest, TestInternalResizeWorks) { |
| 398 | ::libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288, |
John Koleszar | b683eec | 2013-02-21 10:38:27 -0800 | [diff] [blame] | 399 | 30, 1, 0, 10); |
| 400 | init_flags_ = VPX_CODEC_USE_PSNR; |
jackychen | 55f092d | 2015-09-21 09:37:46 -0700 | [diff] [blame] | 401 | change_config_ = false; |
Adrian Grange | 93ffd37 | 2013-09-10 12:02:37 -0700 | [diff] [blame] | 402 | |
Adrian Grange | 88c8ff2 | 2013-09-12 09:35:04 -0700 | [diff] [blame] | 403 | // q picked such that initial keyframe on this clip is ~30dB PSNR |
| 404 | cfg_.rc_min_quantizer = cfg_.rc_max_quantizer = 48; |
| 405 | |
Adrian Grange | 93ffd37 | 2013-09-10 12:02:37 -0700 | [diff] [blame] | 406 | // 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 Koleszar | 88f99f4 | 2013-02-06 12:44:20 -0800 | [diff] [blame] | 411 | ASSERT_NO_FATAL_FAILURE(RunLoop(&video)); |
| 412 | |
James Zern | bb06138 | 2014-01-31 20:11:55 -0800 | [diff] [blame] | 413 | for (std::vector<FrameInfo>::const_iterator info = frame_info_list_.begin(); |
John Koleszar | 88f99f4 | 2013-02-06 12:44:20 -0800 | [diff] [blame] | 414 | info != frame_info_list_.end(); ++info) { |
| 415 | const vpx_codec_pts_t pts = info->pts; |
Adrian Grange | 88c8ff2 | 2013-09-12 09:35:04 -0700 | [diff] [blame] | 416 | if (pts >= kStepDownFrame && pts < kStepUpFrame) { |
John Koleszar | 88f99f4 | 2013-02-06 12:44:20 -0800 | [diff] [blame] | 417 | 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 | |
jackychen | 55f092d | 2015-09-21 09:37:46 -0700 | [diff] [blame] | 426 | TEST_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 | |
jackychen | 55c8843 | 2015-11-09 14:58:14 -0800 | [diff] [blame] | 435 | class ResizeRealtimeTest : public ::libvpx_test::EncoderTest, |
jackychen | 9ac42bc | 2015-09-15 14:17:04 -0700 | [diff] [blame] | 436 | public ::libvpx_test::CodecTestWith2Params<libvpx_test::TestMode, int> { |
| 437 | protected: |
jackychen | 55c8843 | 2015-11-09 14:58:14 -0800 | [diff] [blame] | 438 | ResizeRealtimeTest() : EncoderTest(GET_PARAM(0)) {} |
| 439 | virtual ~ResizeRealtimeTest() {} |
jackychen | 9ac42bc | 2015-09-15 14:17:04 -0700 | [diff] [blame] | 440 | |
| 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 | } |
jackychen | ca8f8fd | 2015-09-15 15:47:11 -0700 | [diff] [blame] | 447 | |
| 448 | if (change_bitrate_ && video->frame() == 120) { |
| 449 | change_bitrate_ = false; |
| 450 | cfg_.rc_target_bitrate = 500; |
| 451 | encoder->Config(&cfg_); |
| 452 | } |
jackychen | 9ac42bc | 2015-09-15 14:17:04 -0700 | [diff] [blame] | 453 | } |
| 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() { |
jackychen | 9ac42bc | 2015-09-15 14:17:04 -0700 | [diff] [blame] | 467 | 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_; |
jackychen | ca8f8fd | 2015-09-15 15:47:11 -0700 | [diff] [blame] | 490 | bool change_bitrate_; |
jackychen | 9ac42bc | 2015-09-15 14:17:04 -0700 | [diff] [blame] | 491 | }; |
| 492 | |
jackychen | 55c8843 | 2015-11-09 14:58:14 -0800 | [diff] [blame] | 493 | TEST_P(ResizeRealtimeTest, TestExternalResizeWorks) { |
| 494 | ResizingVideoSource video; |
Marco | 3cbc26f | 2016-02-10 11:39:04 -0800 | [diff] [blame] | 495 | video.flag_codec_ = 1; |
jackychen | 55c8843 | 2015-11-09 14:58:14 -0800 | [diff] [blame] | 496 | DefaultConfig(); |
Marco | 34d12d1 | 2016-02-10 16:59:09 -0800 | [diff] [blame] | 497 | // Disable internal resize for this test. |
| 498 | cfg_.rc_resize_allowed = 0; |
jackychen | 55c8843 | 2015-11-09 14:58:14 -0800 | [diff] [blame] | 499 | 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); |
Marco | 3cbc26f | 2016-02-10 11:39:04 -0800 | [diff] [blame] | 505 | unsigned int expected_w; |
| 506 | unsigned int expected_h; |
| 507 | ScaleForFrameNumber(frame, kInitialWidth, kInitialHeight, |
| 508 | &expected_w, &expected_h, 1); |
jackychen | 55c8843 | 2015-11-09 14:58:14 -0800 | [diff] [blame] | 509 | 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 | |
jackychen | 9ac42bc | 2015-09-15 14:17:04 -0700 | [diff] [blame] | 516 | // 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. |
jackychen | 55c8843 | 2015-11-09 14:58:14 -0800 | [diff] [blame] | 519 | TEST_P(ResizeRealtimeTest, TestInternalResizeDown) { |
jackychen | 9ac42bc | 2015-09-15 14:17:04 -0700 | [diff] [blame] | 520 | ::libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288, |
| 521 | 30, 1, 0, 299); |
| 522 | DefaultConfig(); |
jackychen | 55c8843 | 2015-11-09 14:58:14 -0800 | [diff] [blame] | 523 | cfg_.g_w = 352; |
| 524 | cfg_.g_h = 288; |
jackychen | ca8f8fd | 2015-09-15 15:47:11 -0700 | [diff] [blame] | 525 | change_bitrate_ = false; |
jackychen | 9ac42bc | 2015-09-15 14:17:04 -0700 | [diff] [blame] | 526 | 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 | |
jackychen | ca8f8fd | 2015-09-15 15:47:11 -0700 | [diff] [blame] | 547 | // 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. |
jackychen | 55c8843 | 2015-11-09 14:58:14 -0800 | [diff] [blame] | 550 | TEST_P(ResizeRealtimeTest, TestInternalResizeDownUpChangeBitRate) { |
jackychen | ca8f8fd | 2015-09-15 15:47:11 -0700 | [diff] [blame] | 551 | ::libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288, |
jackychen | 0465aa4 | 2015-11-09 14:04:58 -0800 | [diff] [blame] | 552 | 30, 1, 0, 359); |
jackychen | ca8f8fd | 2015-09-15 15:47:11 -0700 | [diff] [blame] | 553 | DefaultConfig(); |
jackychen | 55c8843 | 2015-11-09 14:58:14 -0800 | [diff] [blame] | 554 | cfg_.g_w = 352; |
| 555 | cfg_.g_h = 288; |
jackychen | ca8f8fd | 2015-09-15 15:47:11 -0700 | [diff] [blame] | 556 | change_bitrate_ = true; |
| 557 | // Disable dropped frames. |
| 558 | cfg_.rc_dropframe_thresh = 0; |
| 559 | // Starting bitrate low. |
jackychen | 204cde5 | 2015-11-13 16:02:43 -0800 | [diff] [blame] | 560 | cfg_.rc_target_bitrate = 80; |
jackychen | ca8f8fd | 2015-09-15 15:47:11 -0700 | [diff] [blame] | 561 | 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 | |
jackychen | 204cde5 | 2015-11-13 16:02:43 -0800 | [diff] [blame] | 584 | // Verify that we get 2 resize events in this test. |
| 585 | ASSERT_EQ(resize_count, 2) << "Resizing should occur twice."; |
jackychen | ca8f8fd | 2015-09-15 15:47:11 -0700 | [diff] [blame] | 586 | } |
| 587 | |
Alex Converse | 797a255 | 2015-01-15 13:56:55 -0800 | [diff] [blame] | 588 | vpx_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 | |
| 596 | class 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 | |
| 668 | class 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 | |
| 685 | TEST_P(ResizeCspTest, TestResizeCspWorks) { |
| 686 | ResizingCspVideoSource video; |
James Zern | 40f177a | 2015-02-26 20:31:59 -0800 | [diff] [blame] | 687 | init_flags_ = VPX_CODEC_USE_PSNR; |
Alex Converse | 797a255 | 2015-01-15 13:56:55 -0800 | [diff] [blame] | 688 | 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 Koleszar | 706cafe | 2013-01-18 11:51:12 -0800 | [diff] [blame] | 693 | VP8_INSTANTIATE_TEST_CASE(ResizeTest, ONE_PASS_TEST_MODES); |
Alex Converse | 910ca85 | 2015-01-12 16:26:05 -0800 | [diff] [blame] | 694 | VP9_INSTANTIATE_TEST_CASE(ResizeTest, |
| 695 | ::testing::Values(::libvpx_test::kRealTime)); |
John Koleszar | 88f99f4 | 2013-02-06 12:44:20 -0800 | [diff] [blame] | 696 | VP9_INSTANTIATE_TEST_CASE(ResizeInternalTest, |
| 697 | ::testing::Values(::libvpx_test::kOnePassBest)); |
jackychen | 55c8843 | 2015-11-09 14:58:14 -0800 | [diff] [blame] | 698 | VP9_INSTANTIATE_TEST_CASE(ResizeRealtimeTest, |
jackychen | 9ac42bc | 2015-09-15 14:17:04 -0700 | [diff] [blame] | 699 | ::testing::Values(::libvpx_test::kRealTime), |
| 700 | ::testing::Range(5, 9)); |
Alex Converse | 797a255 | 2015-01-15 13:56:55 -0800 | [diff] [blame] | 701 | VP9_INSTANTIATE_TEST_CASE(ResizeCspTest, |
| 702 | ::testing::Values(::libvpx_test::kRealTime)); |
John Koleszar | 2fb29ff | 2012-05-23 12:55:27 -0700 | [diff] [blame] | 703 | } // namespace |