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 | */ |
James Zern | 0616fa6 | 2016-04-26 19:56:44 -0700 | [diff] [blame] | 10 | #include <stdio.h> |
| 11 | |
John Koleszar | 2fb29ff | 2012-05-23 12:55:27 -0700 | [diff] [blame] | 12 | #include <climits> |
| 13 | #include <vector> |
John Koleszar | 706cafe | 2013-01-18 11:51:12 -0800 | [diff] [blame] | 14 | #include "third_party/googletest/src/include/gtest/gtest.h" |
| 15 | #include "test/codec_factory.h" |
John Koleszar | 2fb29ff | 2012-05-23 12:55:27 -0700 | [diff] [blame] | 16 | #include "test/encode_test_driver.h" |
John Koleszar | 88f99f4 | 2013-02-06 12:44:20 -0800 | [diff] [blame] | 17 | #include "test/i420_video_source.h" |
John Koleszar | 2fb29ff | 2012-05-23 12:55:27 -0700 | [diff] [blame] | 18 | #include "test/video_source.h" |
John Koleszar | 706cafe | 2013-01-18 11:51:12 -0800 | [diff] [blame] | 19 | #include "test/util.h" |
John Koleszar | 2fb29ff | 2012-05-23 12:55:27 -0700 | [diff] [blame] | 20 | |
Adrian Grange | 88c8ff2 | 2013-09-12 09:35:04 -0700 | [diff] [blame] | 21 | // Enable(1) or Disable(0) writing of the compressed bitstream. |
| 22 | #define WRITE_COMPRESSED_STREAM 0 |
| 23 | |
John Koleszar | 2fb29ff | 2012-05-23 12:55:27 -0700 | [diff] [blame] | 24 | namespace { |
| 25 | |
Adrian Grange | 88c8ff2 | 2013-09-12 09:35:04 -0700 | [diff] [blame] | 26 | #if WRITE_COMPRESSED_STREAM |
| 27 | static void mem_put_le16(char *const mem, const unsigned int val) { |
| 28 | mem[0] = val; |
| 29 | mem[1] = val >> 8; |
| 30 | } |
| 31 | |
| 32 | static void mem_put_le32(char *const mem, const unsigned int val) { |
| 33 | mem[0] = val; |
| 34 | mem[1] = val >> 8; |
| 35 | mem[2] = val >> 16; |
| 36 | mem[3] = val >> 24; |
| 37 | } |
| 38 | |
| 39 | static void write_ivf_file_header(const vpx_codec_enc_cfg_t *const cfg, |
| 40 | int frame_cnt, FILE *const outfile) { |
| 41 | char header[32]; |
| 42 | |
| 43 | header[0] = 'D'; |
| 44 | header[1] = 'K'; |
| 45 | header[2] = 'I'; |
| 46 | header[3] = 'F'; |
| 47 | mem_put_le16(header + 4, 0); /* version */ |
| 48 | mem_put_le16(header + 6, 32); /* headersize */ |
| 49 | mem_put_le32(header + 8, 0x30395056); /* fourcc (vp9) */ |
| 50 | mem_put_le16(header + 12, cfg->g_w); /* width */ |
| 51 | mem_put_le16(header + 14, cfg->g_h); /* height */ |
| 52 | mem_put_le32(header + 16, cfg->g_timebase.den); /* rate */ |
| 53 | mem_put_le32(header + 20, cfg->g_timebase.num); /* scale */ |
| 54 | mem_put_le32(header + 24, frame_cnt); /* length */ |
| 55 | mem_put_le32(header + 28, 0); /* unused */ |
| 56 | |
| 57 | (void)fwrite(header, 1, 32, outfile); |
| 58 | } |
| 59 | |
| 60 | static void write_ivf_frame_size(FILE *const outfile, const size_t size) { |
| 61 | char header[4]; |
| 62 | mem_put_le32(header, static_cast<unsigned int>(size)); |
| 63 | (void)fwrite(header, 1, 4, outfile); |
| 64 | } |
| 65 | |
| 66 | static void write_ivf_frame_header(const vpx_codec_cx_pkt_t *const pkt, |
| 67 | FILE *const outfile) { |
| 68 | char header[12]; |
| 69 | vpx_codec_pts_t pts; |
| 70 | |
| 71 | if (pkt->kind != VPX_CODEC_CX_FRAME_PKT) |
| 72 | return; |
| 73 | |
| 74 | pts = pkt->data.frame.pts; |
| 75 | mem_put_le32(header, static_cast<unsigned int>(pkt->data.frame.sz)); |
| 76 | mem_put_le32(header + 4, pts & 0xFFFFFFFF); |
| 77 | mem_put_le32(header + 8, pts >> 32); |
| 78 | |
| 79 | (void)fwrite(header, 1, 12, outfile); |
| 80 | } |
| 81 | #endif // WRITE_COMPRESSED_STREAM |
| 82 | |
John Koleszar | 2fb29ff | 2012-05-23 12:55:27 -0700 | [diff] [blame] | 83 | const unsigned int kInitialWidth = 320; |
| 84 | const unsigned int kInitialHeight = 240; |
| 85 | |
jackychen | 9ac42bc | 2015-09-15 14:17:04 -0700 | [diff] [blame] | 86 | struct FrameInfo { |
| 87 | FrameInfo(vpx_codec_pts_t _pts, unsigned int _w, unsigned int _h) |
| 88 | : pts(_pts), w(_w), h(_h) {} |
| 89 | |
| 90 | vpx_codec_pts_t pts; |
| 91 | unsigned int w; |
| 92 | unsigned int h; |
| 93 | }; |
| 94 | |
Marco | 3cbc26f | 2016-02-10 11:39:04 -0800 | [diff] [blame] | 95 | void ScaleForFrameNumber(unsigned int frame, |
| 96 | unsigned int initial_w, |
| 97 | unsigned int initial_h, |
| 98 | unsigned int *w, |
| 99 | unsigned int *h, |
| 100 | int flag_codec) { |
| 101 | if (frame < 10) { |
| 102 | *w = initial_w; |
| 103 | *h = initial_h; |
| 104 | return; |
| 105 | } |
| 106 | if (frame < 20) { |
| 107 | *w = initial_w * 3 / 4; |
| 108 | *h = initial_h * 3 / 4; |
| 109 | return; |
| 110 | } |
| 111 | if (frame < 30) { |
| 112 | *w = initial_w / 2; |
| 113 | *h = initial_h / 2; |
| 114 | return; |
| 115 | } |
| 116 | if (frame < 40) { |
| 117 | *w = initial_w; |
| 118 | *h = initial_h; |
| 119 | return; |
| 120 | } |
| 121 | if (frame < 50) { |
| 122 | *w = initial_w * 3 / 4; |
| 123 | *h = initial_h * 3 / 4; |
| 124 | return; |
| 125 | } |
| 126 | if (frame < 60) { |
| 127 | *w = initial_w / 2; |
| 128 | *h = initial_h / 2; |
| 129 | return; |
| 130 | } |
| 131 | if (frame < 70) { |
| 132 | *w = initial_w; |
| 133 | *h = initial_h; |
| 134 | return; |
| 135 | } |
| 136 | if (frame < 80) { |
| 137 | *w = initial_w * 3 / 4; |
| 138 | *h = initial_h * 3 / 4; |
| 139 | return; |
| 140 | } |
| 141 | if (frame < 90) { |
| 142 | *w = initial_w / 2; |
| 143 | *h = initial_h / 2; |
| 144 | return; |
| 145 | } |
| 146 | if (frame < 100) { |
| 147 | *w = initial_w * 3 / 4; |
| 148 | *h = initial_h * 3 / 4; |
| 149 | return; |
| 150 | } |
| 151 | if (frame < 110) { |
| 152 | *w = initial_w; |
| 153 | *h = initial_h; |
| 154 | return; |
| 155 | } |
| 156 | if (frame < 120) { |
| 157 | *w = initial_w * 3 / 4; |
| 158 | *h = initial_h * 3 / 4; |
| 159 | return; |
| 160 | } |
| 161 | if (frame < 130) { |
| 162 | *w = initial_w / 2; |
| 163 | *h = initial_h / 2; |
| 164 | return; |
| 165 | } |
| 166 | if (frame < 140) { |
| 167 | *w = initial_w * 3 / 4; |
| 168 | *h = initial_h * 3 / 4; |
| 169 | return; |
| 170 | } |
| 171 | if (frame < 150) { |
| 172 | *w = initial_w; |
| 173 | *h = initial_h; |
| 174 | return; |
| 175 | } |
| 176 | if (frame < 160) { |
| 177 | *w = initial_w * 3 / 4; |
| 178 | *h = initial_h * 3 / 4; |
| 179 | return; |
| 180 | } |
| 181 | if (frame < 170) { |
| 182 | *w = initial_w / 2; |
| 183 | *h = initial_h / 2; |
| 184 | return; |
| 185 | } |
| 186 | if (frame < 180) { |
| 187 | *w = initial_w * 3 / 4; |
| 188 | *h = initial_h * 3 / 4; |
| 189 | return; |
| 190 | } |
| 191 | if (frame < 190) { |
| 192 | *w = initial_w; |
| 193 | *h = initial_h; |
| 194 | return; |
| 195 | } |
| 196 | if (frame < 200) { |
| 197 | *w = initial_w * 3 / 4; |
| 198 | *h = initial_h * 3 / 4; |
| 199 | return; |
| 200 | } |
| 201 | if (frame < 210) { |
| 202 | *w = initial_w / 2; |
| 203 | *h = initial_h / 2; |
| 204 | return; |
| 205 | } |
| 206 | if (frame < 220) { |
| 207 | *w = initial_w * 3 / 4; |
| 208 | *h = initial_h * 3 / 4; |
| 209 | return; |
| 210 | } |
| 211 | if (frame < 230) { |
| 212 | *w = initial_w; |
| 213 | *h = initial_h; |
| 214 | return; |
| 215 | } |
| 216 | if (frame < 240) { |
| 217 | *w = initial_w * 3 / 4; |
| 218 | *h = initial_h * 3 / 4; |
| 219 | return; |
| 220 | } |
| 221 | if (frame < 250) { |
| 222 | *w = initial_w / 2; |
| 223 | *h = initial_h / 2; |
| 224 | return; |
| 225 | } |
| 226 | if (frame < 260) { |
| 227 | *w = initial_w; |
| 228 | *h = initial_h; |
| 229 | return; |
| 230 | } |
| 231 | // Go down very low. |
| 232 | if (frame < 270) { |
| 233 | *w = initial_w / 4; |
| 234 | *h = initial_h / 4; |
| 235 | return; |
| 236 | } |
| 237 | if (flag_codec == 1) { |
| 238 | // Cases that only works for VP9. |
| 239 | // For VP9: Swap width and height of original. |
| 240 | if (frame < 320) { |
| 241 | *w = initial_h; |
| 242 | *h = initial_w; |
| 243 | return; |
| 244 | } |
| 245 | } |
| 246 | *w = initial_w; |
| 247 | *h = initial_h; |
John Koleszar | 2fb29ff | 2012-05-23 12:55:27 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | class ResizingVideoSource : public ::libvpx_test::DummyVideoSource { |
| 251 | public: |
| 252 | ResizingVideoSource() { |
| 253 | SetSize(kInitialWidth, kInitialHeight); |
Marco | 3cbc26f | 2016-02-10 11:39:04 -0800 | [diff] [blame] | 254 | limit_ = 350; |
John Koleszar | 2fb29ff | 2012-05-23 12:55:27 -0700 | [diff] [blame] | 255 | } |
Marco | 3cbc26f | 2016-02-10 11:39:04 -0800 | [diff] [blame] | 256 | int flag_codec_; |
Adrian Grange | 88c8ff2 | 2013-09-12 09:35:04 -0700 | [diff] [blame] | 257 | virtual ~ResizingVideoSource() {} |
| 258 | |
John Koleszar | 2fb29ff | 2012-05-23 12:55:27 -0700 | [diff] [blame] | 259 | protected: |
| 260 | virtual void Next() { |
| 261 | ++frame_; |
Marco | 3cbc26f | 2016-02-10 11:39:04 -0800 | [diff] [blame] | 262 | unsigned int width; |
| 263 | unsigned int height; |
| 264 | ScaleForFrameNumber(frame_, kInitialWidth, kInitialHeight, &width, &height, |
| 265 | flag_codec_); |
| 266 | SetSize(width, height); |
John Koleszar | 2fb29ff | 2012-05-23 12:55:27 -0700 | [diff] [blame] | 267 | FillFrame(); |
| 268 | } |
| 269 | }; |
| 270 | |
| 271 | class ResizeTest : public ::libvpx_test::EncoderTest, |
John Koleszar | 706cafe | 2013-01-18 11:51:12 -0800 | [diff] [blame] | 272 | public ::libvpx_test::CodecTestWithParam<libvpx_test::TestMode> { |
John Koleszar | 2fb29ff | 2012-05-23 12:55:27 -0700 | [diff] [blame] | 273 | protected: |
John Koleszar | 706cafe | 2013-01-18 11:51:12 -0800 | [diff] [blame] | 274 | ResizeTest() : EncoderTest(GET_PARAM(0)) {} |
| 275 | |
Adrian Grange | 88c8ff2 | 2013-09-12 09:35:04 -0700 | [diff] [blame] | 276 | virtual ~ResizeTest() {} |
| 277 | |
John Koleszar | 2fb29ff | 2012-05-23 12:55:27 -0700 | [diff] [blame] | 278 | virtual void SetUp() { |
| 279 | InitializeConfig(); |
John Koleszar | 706cafe | 2013-01-18 11:51:12 -0800 | [diff] [blame] | 280 | SetMode(GET_PARAM(1)); |
John Koleszar | 2fb29ff | 2012-05-23 12:55:27 -0700 | [diff] [blame] | 281 | } |
| 282 | |
John Koleszar | 88f99f4 | 2013-02-06 12:44:20 -0800 | [diff] [blame] | 283 | virtual void DecompressedFrameHook(const vpx_image_t &img, |
| 284 | vpx_codec_pts_t pts) { |
| 285 | 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] | 286 | } |
| 287 | |
| 288 | std::vector< FrameInfo > frame_info_list_; |
| 289 | }; |
| 290 | |
| 291 | TEST_P(ResizeTest, TestExternalResizeWorks) { |
| 292 | ResizingVideoSource video; |
Marco | 3cbc26f | 2016-02-10 11:39:04 -0800 | [diff] [blame] | 293 | video.flag_codec_ = 0; |
Alex Converse | 910ca85 | 2015-01-12 16:26:05 -0800 | [diff] [blame] | 294 | cfg_.g_lag_in_frames = 0; |
John Koleszar | 2fb29ff | 2012-05-23 12:55:27 -0700 | [diff] [blame] | 295 | ASSERT_NO_FATAL_FAILURE(RunLoop(&video)); |
| 296 | |
James Zern | bb06138 | 2014-01-31 20:11:55 -0800 | [diff] [blame] | 297 | for (std::vector<FrameInfo>::const_iterator info = frame_info_list_.begin(); |
John Koleszar | 2fb29ff | 2012-05-23 12:55:27 -0700 | [diff] [blame] | 298 | info != frame_info_list_.end(); ++info) { |
James Zern | 17b8993 | 2014-01-31 20:10:28 -0800 | [diff] [blame] | 299 | const unsigned int frame = static_cast<unsigned>(info->pts); |
Marco | 3cbc26f | 2016-02-10 11:39:04 -0800 | [diff] [blame] | 300 | unsigned int expected_w; |
| 301 | unsigned int expected_h; |
| 302 | ScaleForFrameNumber(frame, kInitialWidth, kInitialHeight, |
| 303 | &expected_w, &expected_h, 0); |
John Koleszar | 2fb29ff | 2012-05-23 12:55:27 -0700 | [diff] [blame] | 304 | EXPECT_EQ(expected_w, info->w) |
Alex Converse | 910ca85 | 2015-01-12 16:26:05 -0800 | [diff] [blame] | 305 | << "Frame " << frame << " had unexpected width"; |
John Koleszar | 2fb29ff | 2012-05-23 12:55:27 -0700 | [diff] [blame] | 306 | EXPECT_EQ(expected_h, info->h) |
Alex Converse | 910ca85 | 2015-01-12 16:26:05 -0800 | [diff] [blame] | 307 | << "Frame " << frame << " had unexpected height"; |
John Koleszar | 2fb29ff | 2012-05-23 12:55:27 -0700 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | |
Adrian Grange | 88c8ff2 | 2013-09-12 09:35:04 -0700 | [diff] [blame] | 311 | const unsigned int kStepDownFrame = 3; |
| 312 | const unsigned int kStepUpFrame = 6; |
| 313 | |
John Koleszar | 88f99f4 | 2013-02-06 12:44:20 -0800 | [diff] [blame] | 314 | class ResizeInternalTest : public ResizeTest { |
| 315 | protected: |
Adrian Grange | 88c8ff2 | 2013-09-12 09:35:04 -0700 | [diff] [blame] | 316 | #if WRITE_COMPRESSED_STREAM |
| 317 | ResizeInternalTest() |
| 318 | : ResizeTest(), |
| 319 | frame0_psnr_(0.0), |
| 320 | outfile_(NULL), |
| 321 | out_frames_(0) {} |
| 322 | #else |
John Koleszar | b683eec | 2013-02-21 10:38:27 -0800 | [diff] [blame] | 323 | ResizeInternalTest() : ResizeTest(), frame0_psnr_(0.0) {} |
Adrian Grange | 88c8ff2 | 2013-09-12 09:35:04 -0700 | [diff] [blame] | 324 | #endif |
| 325 | |
| 326 | virtual ~ResizeInternalTest() {} |
| 327 | |
| 328 | virtual void BeginPassHook(unsigned int /*pass*/) { |
| 329 | #if WRITE_COMPRESSED_STREAM |
| 330 | outfile_ = fopen("vp90-2-05-resize.ivf", "wb"); |
| 331 | #endif |
| 332 | } |
| 333 | |
| 334 | virtual void EndPassHook() { |
| 335 | #if WRITE_COMPRESSED_STREAM |
| 336 | if (outfile_) { |
| 337 | if (!fseek(outfile_, 0, SEEK_SET)) |
| 338 | write_ivf_file_header(&cfg_, out_frames_, outfile_); |
| 339 | fclose(outfile_); |
| 340 | outfile_ = NULL; |
| 341 | } |
| 342 | #endif |
| 343 | } |
John Koleszar | 88f99f4 | 2013-02-06 12:44:20 -0800 | [diff] [blame] | 344 | |
| 345 | virtual void PreEncodeFrameHook(libvpx_test::VideoSource *video, |
| 346 | libvpx_test::Encoder *encoder) { |
jackychen | 55f092d | 2015-09-21 09:37:46 -0700 | [diff] [blame] | 347 | if (change_config_) { |
| 348 | int new_q = 60; |
| 349 | if (video->frame() == 0) { |
| 350 | struct vpx_scaling_mode mode = {VP8E_ONETWO, VP8E_ONETWO}; |
| 351 | encoder->Control(VP8E_SET_SCALEMODE, &mode); |
| 352 | } |
| 353 | if (video->frame() == 1) { |
| 354 | struct vpx_scaling_mode mode = {VP8E_NORMAL, VP8E_NORMAL}; |
| 355 | encoder->Control(VP8E_SET_SCALEMODE, &mode); |
| 356 | cfg_.rc_min_quantizer = cfg_.rc_max_quantizer = new_q; |
| 357 | encoder->Config(&cfg_); |
| 358 | } |
| 359 | } else { |
| 360 | if (video->frame() == kStepDownFrame) { |
| 361 | struct vpx_scaling_mode mode = {VP8E_FOURFIVE, VP8E_THREEFIVE}; |
| 362 | encoder->Control(VP8E_SET_SCALEMODE, &mode); |
| 363 | } |
| 364 | if (video->frame() == kStepUpFrame) { |
| 365 | struct vpx_scaling_mode mode = {VP8E_NORMAL, VP8E_NORMAL}; |
| 366 | encoder->Control(VP8E_SET_SCALEMODE, &mode); |
| 367 | } |
John Koleszar | b683eec | 2013-02-21 10:38:27 -0800 | [diff] [blame] | 368 | } |
John Koleszar | 88f99f4 | 2013-02-06 12:44:20 -0800 | [diff] [blame] | 369 | } |
John Koleszar | b683eec | 2013-02-21 10:38:27 -0800 | [diff] [blame] | 370 | |
| 371 | virtual void PSNRPktHook(const vpx_codec_cx_pkt_t *pkt) { |
James Zern | 60be793 | 2016-07-22 13:11:07 -0700 | [diff] [blame] | 372 | if (frame0_psnr_ == 0.) |
John Koleszar | b683eec | 2013-02-21 10:38:27 -0800 | [diff] [blame] | 373 | frame0_psnr_ = pkt->data.psnr.psnr[0]; |
Deb Mukherjee | 3cd37df | 2014-01-17 12:56:36 -0800 | [diff] [blame] | 374 | EXPECT_NEAR(pkt->data.psnr.psnr[0], frame0_psnr_, 2.0); |
John Koleszar | b683eec | 2013-02-21 10:38:27 -0800 | [diff] [blame] | 375 | } |
| 376 | |
Adrian Grange | 88c8ff2 | 2013-09-12 09:35:04 -0700 | [diff] [blame] | 377 | #if WRITE_COMPRESSED_STREAM |
James Zern | dbe6917 | 2014-08-22 12:11:42 -0700 | [diff] [blame] | 378 | virtual void FramePktHook(const vpx_codec_cx_pkt_t *pkt) { |
Adrian Grange | 88c8ff2 | 2013-09-12 09:35:04 -0700 | [diff] [blame] | 379 | ++out_frames_; |
| 380 | |
| 381 | // Write initial file header if first frame. |
| 382 | if (pkt->data.frame.pts == 0) |
| 383 | write_ivf_file_header(&cfg_, 0, outfile_); |
| 384 | |
| 385 | // Write frame header and data. |
| 386 | write_ivf_frame_header(pkt, outfile_); |
| 387 | (void)fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz, outfile_); |
Adrian Grange | 88c8ff2 | 2013-09-12 09:35:04 -0700 | [diff] [blame] | 388 | } |
James Zern | dbe6917 | 2014-08-22 12:11:42 -0700 | [diff] [blame] | 389 | #endif |
Adrian Grange | 88c8ff2 | 2013-09-12 09:35:04 -0700 | [diff] [blame] | 390 | |
John Koleszar | b683eec | 2013-02-21 10:38:27 -0800 | [diff] [blame] | 391 | double frame0_psnr_; |
jackychen | 55f092d | 2015-09-21 09:37:46 -0700 | [diff] [blame] | 392 | bool change_config_; |
Adrian Grange | 88c8ff2 | 2013-09-12 09:35:04 -0700 | [diff] [blame] | 393 | #if WRITE_COMPRESSED_STREAM |
| 394 | FILE *outfile_; |
| 395 | unsigned int out_frames_; |
| 396 | #endif |
John Koleszar | 88f99f4 | 2013-02-06 12:44:20 -0800 | [diff] [blame] | 397 | }; |
| 398 | |
| 399 | TEST_P(ResizeInternalTest, TestInternalResizeWorks) { |
| 400 | ::libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288, |
John Koleszar | b683eec | 2013-02-21 10:38:27 -0800 | [diff] [blame] | 401 | 30, 1, 0, 10); |
| 402 | init_flags_ = VPX_CODEC_USE_PSNR; |
jackychen | 55f092d | 2015-09-21 09:37:46 -0700 | [diff] [blame] | 403 | change_config_ = false; |
Adrian Grange | 93ffd37 | 2013-09-10 12:02:37 -0700 | [diff] [blame] | 404 | |
Adrian Grange | 88c8ff2 | 2013-09-12 09:35:04 -0700 | [diff] [blame] | 405 | // q picked such that initial keyframe on this clip is ~30dB PSNR |
| 406 | cfg_.rc_min_quantizer = cfg_.rc_max_quantizer = 48; |
| 407 | |
Adrian Grange | 93ffd37 | 2013-09-10 12:02:37 -0700 | [diff] [blame] | 408 | // If the number of frames being encoded is smaller than g_lag_in_frames |
| 409 | // the encoded frame is unavailable using the current API. Comparing |
| 410 | // frames to detect mismatch would then not be possible. Set |
| 411 | // g_lag_in_frames = 0 to get around this. |
| 412 | cfg_.g_lag_in_frames = 0; |
John Koleszar | 88f99f4 | 2013-02-06 12:44:20 -0800 | [diff] [blame] | 413 | ASSERT_NO_FATAL_FAILURE(RunLoop(&video)); |
| 414 | |
James Zern | bb06138 | 2014-01-31 20:11:55 -0800 | [diff] [blame] | 415 | for (std::vector<FrameInfo>::const_iterator info = frame_info_list_.begin(); |
John Koleszar | 88f99f4 | 2013-02-06 12:44:20 -0800 | [diff] [blame] | 416 | info != frame_info_list_.end(); ++info) { |
| 417 | const vpx_codec_pts_t pts = info->pts; |
Adrian Grange | 88c8ff2 | 2013-09-12 09:35:04 -0700 | [diff] [blame] | 418 | if (pts >= kStepDownFrame && pts < kStepUpFrame) { |
John Koleszar | 88f99f4 | 2013-02-06 12:44:20 -0800 | [diff] [blame] | 419 | ASSERT_EQ(282U, info->w) << "Frame " << pts << " had unexpected width"; |
| 420 | ASSERT_EQ(173U, info->h) << "Frame " << pts << " had unexpected height"; |
| 421 | } else { |
| 422 | EXPECT_EQ(352U, info->w) << "Frame " << pts << " had unexpected width"; |
| 423 | EXPECT_EQ(288U, info->h) << "Frame " << pts << " had unexpected height"; |
| 424 | } |
| 425 | } |
| 426 | } |
| 427 | |
jackychen | 55f092d | 2015-09-21 09:37:46 -0700 | [diff] [blame] | 428 | TEST_P(ResizeInternalTest, TestInternalResizeChangeConfig) { |
| 429 | ::libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288, |
| 430 | 30, 1, 0, 10); |
| 431 | cfg_.g_w = 352; |
| 432 | cfg_.g_h = 288; |
| 433 | change_config_ = true; |
| 434 | ASSERT_NO_FATAL_FAILURE(RunLoop(&video)); |
| 435 | } |
| 436 | |
jackychen | 55c8843 | 2015-11-09 14:58:14 -0800 | [diff] [blame] | 437 | class ResizeRealtimeTest : public ::libvpx_test::EncoderTest, |
jackychen | 9ac42bc | 2015-09-15 14:17:04 -0700 | [diff] [blame] | 438 | public ::libvpx_test::CodecTestWith2Params<libvpx_test::TestMode, int> { |
| 439 | protected: |
jackychen | 55c8843 | 2015-11-09 14:58:14 -0800 | [diff] [blame] | 440 | ResizeRealtimeTest() : EncoderTest(GET_PARAM(0)) {} |
| 441 | virtual ~ResizeRealtimeTest() {} |
jackychen | 9ac42bc | 2015-09-15 14:17:04 -0700 | [diff] [blame] | 442 | |
| 443 | virtual void PreEncodeFrameHook(libvpx_test::VideoSource *video, |
| 444 | libvpx_test::Encoder *encoder) { |
| 445 | if (video->frame() == 0) { |
| 446 | encoder->Control(VP9E_SET_AQ_MODE, 3); |
| 447 | encoder->Control(VP8E_SET_CPUUSED, set_cpu_used_); |
| 448 | } |
jackychen | ca8f8fd | 2015-09-15 15:47:11 -0700 | [diff] [blame] | 449 | |
| 450 | if (change_bitrate_ && video->frame() == 120) { |
| 451 | change_bitrate_ = false; |
| 452 | cfg_.rc_target_bitrate = 500; |
| 453 | encoder->Config(&cfg_); |
| 454 | } |
jackychen | 9ac42bc | 2015-09-15 14:17:04 -0700 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | virtual void SetUp() { |
| 458 | InitializeConfig(); |
| 459 | SetMode(GET_PARAM(1)); |
| 460 | set_cpu_used_ = GET_PARAM(2); |
| 461 | } |
| 462 | |
| 463 | virtual void DecompressedFrameHook(const vpx_image_t &img, |
| 464 | vpx_codec_pts_t pts) { |
| 465 | frame_info_list_.push_back(FrameInfo(pts, img.d_w, img.d_h)); |
| 466 | } |
| 467 | |
Marco | b9cb955 | 2016-03-07 16:58:06 -0800 | [diff] [blame] | 468 | virtual void MismatchHook(const vpx_image_t *img1, |
| 469 | const vpx_image_t *img2) { |
| 470 | double mismatch_psnr = compute_psnr(img1, img2); |
| 471 | mismatch_psnr_ += mismatch_psnr; |
| 472 | ++mismatch_nframes_; |
| 473 | } |
| 474 | |
| 475 | unsigned int GetMismatchFrames() { |
| 476 | return mismatch_nframes_; |
| 477 | } |
| 478 | |
jackychen | 9ac42bc | 2015-09-15 14:17:04 -0700 | [diff] [blame] | 479 | void DefaultConfig() { |
jackychen | 9ac42bc | 2015-09-15 14:17:04 -0700 | [diff] [blame] | 480 | cfg_.rc_buf_initial_sz = 500; |
| 481 | cfg_.rc_buf_optimal_sz = 600; |
| 482 | cfg_.rc_buf_sz = 1000; |
| 483 | cfg_.rc_min_quantizer = 2; |
| 484 | cfg_.rc_max_quantizer = 56; |
| 485 | cfg_.rc_undershoot_pct = 50; |
| 486 | cfg_.rc_overshoot_pct = 50; |
| 487 | cfg_.rc_end_usage = VPX_CBR; |
| 488 | cfg_.kf_mode = VPX_KF_AUTO; |
| 489 | cfg_.g_lag_in_frames = 0; |
| 490 | cfg_.kf_min_dist = cfg_.kf_max_dist = 3000; |
| 491 | // Enable dropped frames. |
| 492 | cfg_.rc_dropframe_thresh = 1; |
| 493 | // Enable error_resilience mode. |
| 494 | cfg_.g_error_resilient = 1; |
| 495 | // Enable dynamic resizing. |
| 496 | cfg_.rc_resize_allowed = 1; |
| 497 | // Run at low bitrate. |
| 498 | cfg_.rc_target_bitrate = 200; |
| 499 | } |
| 500 | |
| 501 | std::vector< FrameInfo > frame_info_list_; |
| 502 | int set_cpu_used_; |
jackychen | ca8f8fd | 2015-09-15 15:47:11 -0700 | [diff] [blame] | 503 | bool change_bitrate_; |
Marco | b9cb955 | 2016-03-07 16:58:06 -0800 | [diff] [blame] | 504 | double mismatch_psnr_; |
| 505 | int mismatch_nframes_; |
jackychen | 9ac42bc | 2015-09-15 14:17:04 -0700 | [diff] [blame] | 506 | }; |
| 507 | |
jackychen | 55c8843 | 2015-11-09 14:58:14 -0800 | [diff] [blame] | 508 | TEST_P(ResizeRealtimeTest, TestExternalResizeWorks) { |
| 509 | ResizingVideoSource video; |
Marco | 3cbc26f | 2016-02-10 11:39:04 -0800 | [diff] [blame] | 510 | video.flag_codec_ = 1; |
jackychen | 55c8843 | 2015-11-09 14:58:14 -0800 | [diff] [blame] | 511 | DefaultConfig(); |
Marco | 34d12d1 | 2016-02-10 16:59:09 -0800 | [diff] [blame] | 512 | // Disable internal resize for this test. |
| 513 | cfg_.rc_resize_allowed = 0; |
jackychen | 55c8843 | 2015-11-09 14:58:14 -0800 | [diff] [blame] | 514 | change_bitrate_ = false; |
Marco | b9cb955 | 2016-03-07 16:58:06 -0800 | [diff] [blame] | 515 | mismatch_psnr_ = 0.0; |
| 516 | mismatch_nframes_ = 0; |
jackychen | 55c8843 | 2015-11-09 14:58:14 -0800 | [diff] [blame] | 517 | ASSERT_NO_FATAL_FAILURE(RunLoop(&video)); |
| 518 | |
| 519 | for (std::vector<FrameInfo>::const_iterator info = frame_info_list_.begin(); |
| 520 | info != frame_info_list_.end(); ++info) { |
| 521 | const unsigned int frame = static_cast<unsigned>(info->pts); |
Marco | 3cbc26f | 2016-02-10 11:39:04 -0800 | [diff] [blame] | 522 | unsigned int expected_w; |
| 523 | unsigned int expected_h; |
| 524 | ScaleForFrameNumber(frame, kInitialWidth, kInitialHeight, |
| 525 | &expected_w, &expected_h, 1); |
jackychen | 55c8843 | 2015-11-09 14:58:14 -0800 | [diff] [blame] | 526 | EXPECT_EQ(expected_w, info->w) |
| 527 | << "Frame " << frame << " had unexpected width"; |
| 528 | EXPECT_EQ(expected_h, info->h) |
| 529 | << "Frame " << frame << " had unexpected height"; |
Marco | b9cb955 | 2016-03-07 16:58:06 -0800 | [diff] [blame] | 530 | EXPECT_EQ(static_cast<unsigned int>(0), GetMismatchFrames()); |
jackychen | 55c8843 | 2015-11-09 14:58:14 -0800 | [diff] [blame] | 531 | } |
| 532 | } |
| 533 | |
jackychen | 9ac42bc | 2015-09-15 14:17:04 -0700 | [diff] [blame] | 534 | // Verify the dynamic resizer behavior for real time, 1 pass CBR mode. |
| 535 | // Run at low bitrate, with resize_allowed = 1, and verify that we get |
| 536 | // one resize down event. |
jackychen | 55c8843 | 2015-11-09 14:58:14 -0800 | [diff] [blame] | 537 | TEST_P(ResizeRealtimeTest, TestInternalResizeDown) { |
jackychen | 9ac42bc | 2015-09-15 14:17:04 -0700 | [diff] [blame] | 538 | ::libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288, |
| 539 | 30, 1, 0, 299); |
| 540 | DefaultConfig(); |
jackychen | 55c8843 | 2015-11-09 14:58:14 -0800 | [diff] [blame] | 541 | cfg_.g_w = 352; |
| 542 | cfg_.g_h = 288; |
jackychen | ca8f8fd | 2015-09-15 15:47:11 -0700 | [diff] [blame] | 543 | change_bitrate_ = false; |
Marco | b9cb955 | 2016-03-07 16:58:06 -0800 | [diff] [blame] | 544 | mismatch_psnr_ = 0.0; |
| 545 | mismatch_nframes_ = 0; |
jackychen | 9ac42bc | 2015-09-15 14:17:04 -0700 | [diff] [blame] | 546 | ASSERT_NO_FATAL_FAILURE(RunLoop(&video)); |
| 547 | |
| 548 | unsigned int last_w = cfg_.g_w; |
| 549 | unsigned int last_h = cfg_.g_h; |
| 550 | int resize_count = 0; |
| 551 | for (std::vector<FrameInfo>::const_iterator info = frame_info_list_.begin(); |
| 552 | info != frame_info_list_.end(); ++info) { |
| 553 | if (info->w != last_w || info->h != last_h) { |
| 554 | // Verify that resize down occurs. |
| 555 | ASSERT_LT(info->w, last_w); |
| 556 | ASSERT_LT(info->h, last_h); |
| 557 | last_w = info->w; |
| 558 | last_h = info->h; |
| 559 | resize_count++; |
| 560 | } |
| 561 | } |
| 562 | |
James Zern | 0616fa6 | 2016-04-26 19:56:44 -0700 | [diff] [blame] | 563 | #if CONFIG_VP9_DECODER |
jackychen | 9ac42bc | 2015-09-15 14:17:04 -0700 | [diff] [blame] | 564 | // Verify that we get 1 resize down event in this test. |
| 565 | ASSERT_EQ(1, resize_count) << "Resizing should occur."; |
Marco | b9cb955 | 2016-03-07 16:58:06 -0800 | [diff] [blame] | 566 | EXPECT_EQ(static_cast<unsigned int>(0), GetMismatchFrames()); |
James Zern | 0616fa6 | 2016-04-26 19:56:44 -0700 | [diff] [blame] | 567 | #else |
| 568 | printf("Warning: VP9 decoder unavailable, unable to check resize count!\n"); |
| 569 | #endif |
jackychen | 9ac42bc | 2015-09-15 14:17:04 -0700 | [diff] [blame] | 570 | } |
| 571 | |
jackychen | ca8f8fd | 2015-09-15 15:47:11 -0700 | [diff] [blame] | 572 | // Verify the dynamic resizer behavior for real time, 1 pass CBR mode. |
| 573 | // Start at low target bitrate, raise the bitrate in the middle of the clip, |
| 574 | // scaling-up should occur after bitrate changed. |
jackychen | 55c8843 | 2015-11-09 14:58:14 -0800 | [diff] [blame] | 575 | TEST_P(ResizeRealtimeTest, TestInternalResizeDownUpChangeBitRate) { |
jackychen | ca8f8fd | 2015-09-15 15:47:11 -0700 | [diff] [blame] | 576 | ::libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288, |
jackychen | 0465aa4 | 2015-11-09 14:04:58 -0800 | [diff] [blame] | 577 | 30, 1, 0, 359); |
jackychen | ca8f8fd | 2015-09-15 15:47:11 -0700 | [diff] [blame] | 578 | DefaultConfig(); |
jackychen | 55c8843 | 2015-11-09 14:58:14 -0800 | [diff] [blame] | 579 | cfg_.g_w = 352; |
| 580 | cfg_.g_h = 288; |
jackychen | ca8f8fd | 2015-09-15 15:47:11 -0700 | [diff] [blame] | 581 | change_bitrate_ = true; |
Marco | b9cb955 | 2016-03-07 16:58:06 -0800 | [diff] [blame] | 582 | mismatch_psnr_ = 0.0; |
| 583 | mismatch_nframes_ = 0; |
jackychen | ca8f8fd | 2015-09-15 15:47:11 -0700 | [diff] [blame] | 584 | // Disable dropped frames. |
| 585 | cfg_.rc_dropframe_thresh = 0; |
| 586 | // Starting bitrate low. |
jackychen | 204cde5 | 2015-11-13 16:02:43 -0800 | [diff] [blame] | 587 | cfg_.rc_target_bitrate = 80; |
jackychen | ca8f8fd | 2015-09-15 15:47:11 -0700 | [diff] [blame] | 588 | ASSERT_NO_FATAL_FAILURE(RunLoop(&video)); |
| 589 | |
| 590 | unsigned int last_w = cfg_.g_w; |
| 591 | unsigned int last_h = cfg_.g_h; |
| 592 | int resize_count = 0; |
| 593 | for (std::vector<FrameInfo>::const_iterator info = frame_info_list_.begin(); |
| 594 | info != frame_info_list_.end(); ++info) { |
| 595 | if (info->w != last_w || info->h != last_h) { |
| 596 | resize_count++; |
| 597 | if (resize_count == 1) { |
| 598 | // Verify that resize down occurs. |
| 599 | ASSERT_LT(info->w, last_w); |
| 600 | ASSERT_LT(info->h, last_h); |
| 601 | } else if (resize_count == 2) { |
| 602 | // Verify that resize up occurs. |
| 603 | ASSERT_GT(info->w, last_w); |
| 604 | ASSERT_GT(info->h, last_h); |
| 605 | } |
| 606 | last_w = info->w; |
| 607 | last_h = info->h; |
| 608 | } |
| 609 | } |
| 610 | |
James Zern | 0616fa6 | 2016-04-26 19:56:44 -0700 | [diff] [blame] | 611 | #if CONFIG_VP9_DECODER |
jackychen | 204cde5 | 2015-11-13 16:02:43 -0800 | [diff] [blame] | 612 | // Verify that we get 2 resize events in this test. |
| 613 | ASSERT_EQ(resize_count, 2) << "Resizing should occur twice."; |
Marco | b9cb955 | 2016-03-07 16:58:06 -0800 | [diff] [blame] | 614 | EXPECT_EQ(static_cast<unsigned int>(0), GetMismatchFrames()); |
James Zern | 0616fa6 | 2016-04-26 19:56:44 -0700 | [diff] [blame] | 615 | #else |
| 616 | printf("Warning: VP9 decoder unavailable, unable to check resize count!\n"); |
| 617 | #endif |
jackychen | ca8f8fd | 2015-09-15 15:47:11 -0700 | [diff] [blame] | 618 | } |
| 619 | |
Alex Converse | 797a255 | 2015-01-15 13:56:55 -0800 | [diff] [blame] | 620 | vpx_img_fmt_t CspForFrameNumber(int frame) { |
| 621 | if (frame < 10) |
| 622 | return VPX_IMG_FMT_I420; |
| 623 | if (frame < 20) |
| 624 | return VPX_IMG_FMT_I444; |
| 625 | return VPX_IMG_FMT_I420; |
| 626 | } |
| 627 | |
| 628 | class ResizeCspTest : public ResizeTest { |
| 629 | protected: |
| 630 | #if WRITE_COMPRESSED_STREAM |
| 631 | ResizeCspTest() |
| 632 | : ResizeTest(), |
| 633 | frame0_psnr_(0.0), |
| 634 | outfile_(NULL), |
| 635 | out_frames_(0) {} |
| 636 | #else |
| 637 | ResizeCspTest() : ResizeTest(), frame0_psnr_(0.0) {} |
| 638 | #endif |
| 639 | |
| 640 | virtual ~ResizeCspTest() {} |
| 641 | |
| 642 | virtual void BeginPassHook(unsigned int /*pass*/) { |
| 643 | #if WRITE_COMPRESSED_STREAM |
| 644 | outfile_ = fopen("vp91-2-05-cspchape.ivf", "wb"); |
| 645 | #endif |
| 646 | } |
| 647 | |
| 648 | virtual void EndPassHook() { |
| 649 | #if WRITE_COMPRESSED_STREAM |
| 650 | if (outfile_) { |
| 651 | if (!fseek(outfile_, 0, SEEK_SET)) |
| 652 | write_ivf_file_header(&cfg_, out_frames_, outfile_); |
| 653 | fclose(outfile_); |
| 654 | outfile_ = NULL; |
| 655 | } |
| 656 | #endif |
| 657 | } |
| 658 | |
| 659 | virtual void PreEncodeFrameHook(libvpx_test::VideoSource *video, |
| 660 | libvpx_test::Encoder *encoder) { |
| 661 | if (CspForFrameNumber(video->frame()) != VPX_IMG_FMT_I420 && |
| 662 | cfg_.g_profile != 1) { |
| 663 | cfg_.g_profile = 1; |
| 664 | encoder->Config(&cfg_); |
| 665 | } |
| 666 | if (CspForFrameNumber(video->frame()) == VPX_IMG_FMT_I420 && |
| 667 | cfg_.g_profile != 0) { |
| 668 | cfg_.g_profile = 0; |
| 669 | encoder->Config(&cfg_); |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | virtual void PSNRPktHook(const vpx_codec_cx_pkt_t *pkt) { |
James Zern | 60be793 | 2016-07-22 13:11:07 -0700 | [diff] [blame] | 674 | if (frame0_psnr_ == 0.) |
Alex Converse | 797a255 | 2015-01-15 13:56:55 -0800 | [diff] [blame] | 675 | frame0_psnr_ = pkt->data.psnr.psnr[0]; |
| 676 | EXPECT_NEAR(pkt->data.psnr.psnr[0], frame0_psnr_, 2.0); |
| 677 | } |
| 678 | |
| 679 | #if WRITE_COMPRESSED_STREAM |
| 680 | virtual void FramePktHook(const vpx_codec_cx_pkt_t *pkt) { |
| 681 | ++out_frames_; |
| 682 | |
| 683 | // Write initial file header if first frame. |
| 684 | if (pkt->data.frame.pts == 0) |
| 685 | write_ivf_file_header(&cfg_, 0, outfile_); |
| 686 | |
| 687 | // Write frame header and data. |
| 688 | write_ivf_frame_header(pkt, outfile_); |
| 689 | (void)fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz, outfile_); |
| 690 | } |
| 691 | #endif |
| 692 | |
| 693 | double frame0_psnr_; |
| 694 | #if WRITE_COMPRESSED_STREAM |
| 695 | FILE *outfile_; |
| 696 | unsigned int out_frames_; |
| 697 | #endif |
| 698 | }; |
| 699 | |
| 700 | class ResizingCspVideoSource : public ::libvpx_test::DummyVideoSource { |
| 701 | public: |
| 702 | ResizingCspVideoSource() { |
| 703 | SetSize(kInitialWidth, kInitialHeight); |
| 704 | limit_ = 30; |
| 705 | } |
| 706 | |
| 707 | virtual ~ResizingCspVideoSource() {} |
| 708 | |
| 709 | protected: |
| 710 | virtual void Next() { |
| 711 | ++frame_; |
| 712 | SetImageFormat(CspForFrameNumber(frame_)); |
| 713 | FillFrame(); |
| 714 | } |
| 715 | }; |
| 716 | |
| 717 | TEST_P(ResizeCspTest, TestResizeCspWorks) { |
| 718 | ResizingCspVideoSource video; |
James Zern | 40f177a | 2015-02-26 20:31:59 -0800 | [diff] [blame] | 719 | init_flags_ = VPX_CODEC_USE_PSNR; |
Alex Converse | 797a255 | 2015-01-15 13:56:55 -0800 | [diff] [blame] | 720 | cfg_.rc_min_quantizer = cfg_.rc_max_quantizer = 48; |
| 721 | cfg_.g_lag_in_frames = 0; |
| 722 | ASSERT_NO_FATAL_FAILURE(RunLoop(&video)); |
| 723 | } |
| 724 | |
Yaowu Xu | 3fa28d5 | 2016-07-27 15:52:42 -0700 | [diff] [blame^] | 725 | VP10_INSTANTIATE_TEST_CASE(ResizeTest, |
Alex Converse | 910ca85 | 2015-01-12 16:26:05 -0800 | [diff] [blame] | 726 | ::testing::Values(::libvpx_test::kRealTime)); |
Yaowu Xu | 3fa28d5 | 2016-07-27 15:52:42 -0700 | [diff] [blame^] | 727 | VP10_INSTANTIATE_TEST_CASE(ResizeInternalTest, |
John Koleszar | 88f99f4 | 2013-02-06 12:44:20 -0800 | [diff] [blame] | 728 | ::testing::Values(::libvpx_test::kOnePassBest)); |
Yaowu Xu | 3fa28d5 | 2016-07-27 15:52:42 -0700 | [diff] [blame^] | 729 | VP10_INSTANTIATE_TEST_CASE(ResizeRealtimeTest, |
jackychen | 9ac42bc | 2015-09-15 14:17:04 -0700 | [diff] [blame] | 730 | ::testing::Values(::libvpx_test::kRealTime), |
| 731 | ::testing::Range(5, 9)); |
Yaowu Xu | 3fa28d5 | 2016-07-27 15:52:42 -0700 | [diff] [blame^] | 732 | VP10_INSTANTIATE_TEST_CASE(ResizeCspTest, |
Alex Converse | 797a255 | 2015-01-15 13:56:55 -0800 | [diff] [blame] | 733 | ::testing::Values(::libvpx_test::kRealTime)); |
John Koleszar | 2fb29ff | 2012-05-23 12:55:27 -0700 | [diff] [blame] | 734 | } // namespace |