resize_test.cc 20.3 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
John Koleszar's avatar
John Koleszar committed
3
 *
4 5 6 7 8 9
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Johann's avatar
Johann committed
10
 */
11

John Koleszar's avatar
John Koleszar committed
12 13
#include <climits>
#include <vector>
14
#include "third_party/googletest/src/googletest/include/gtest/gtest.h"
15
#include "test/codec_factory.h"
John Koleszar's avatar
John Koleszar committed
16
#include "test/encode_test_driver.h"
17
#include "test/i420_video_source.h"
John Koleszar's avatar
John Koleszar committed
18
#include "test/video_source.h"
19
#include "test/util.h"
John Koleszar's avatar
John Koleszar committed
20

21 22 23
// Enable(1) or Disable(0) writing of the compressed bitstream.
#define WRITE_COMPRESSED_STREAM 0

John Koleszar's avatar
John Koleszar committed
24 25
namespace {

26
#if WRITE_COMPRESSED_STREAM
Yaowu Xu's avatar
Yaowu Xu committed
27
static void mem_put_le16(char *const mem, unsigned int val) {
28 29 30 31
  mem[0] = val;
  mem[1] = val >> 8;
}

Yaowu Xu's avatar
Yaowu Xu committed
32
static void mem_put_le32(char *const mem, unsigned int val) {
33 34 35 36 37 38
  mem[0] = val;
  mem[1] = val >> 8;
  mem[2] = val >> 16;
  mem[3] = val >> 24;
}

39
static void write_ivf_file_header(const aom_codec_enc_cfg_t *const cfg,
40 41 42 43 44 45 46
                                  int frame_cnt, FILE *const outfile) {
  char header[32];

  header[0] = 'D';
  header[1] = 'K';
  header[2] = 'I';
  header[3] = 'F';
clang-format's avatar
clang-format committed
47 48
  mem_put_le16(header + 4, 0);                    /* version */
  mem_put_le16(header + 6, 32);                   /* headersize */
49
  mem_put_le32(header + 8, 0x30395056);           /* fourcc (av1) */
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
  mem_put_le16(header + 12, cfg->g_w);            /* width */
  mem_put_le16(header + 14, cfg->g_h);            /* height */
  mem_put_le32(header + 16, cfg->g_timebase.den); /* rate */
  mem_put_le32(header + 20, cfg->g_timebase.num); /* scale */
  mem_put_le32(header + 24, frame_cnt);           /* length */
  mem_put_le32(header + 28, 0);                   /* unused */

  (void)fwrite(header, 1, 32, outfile);
}

static void write_ivf_frame_size(FILE *const outfile, const size_t size) {
  char header[4];
  mem_put_le32(header, static_cast<unsigned int>(size));
  (void)fwrite(header, 1, 4, outfile);
}

66
static void write_ivf_frame_header(const aom_codec_cx_pkt_t *const pkt,
67 68
                                   FILE *const outfile) {
  char header[12];
69
  aom_codec_pts_t pts;
70

71
  if (pkt->kind != AOM_CODEC_CX_FRAME_PKT) return;
72 73 74 75 76 77 78 79 80 81

  pts = pkt->data.frame.pts;
  mem_put_le32(header, static_cast<unsigned int>(pkt->data.frame.sz));
  mem_put_le32(header + 4, pts & 0xFFFFFFFF);
  mem_put_le32(header + 8, pts >> 32);

  (void)fwrite(header, 1, 12, outfile);
}
#endif  // WRITE_COMPRESSED_STREAM

John Koleszar's avatar
John Koleszar committed
82 83 84
const unsigned int kInitialWidth = 320;
const unsigned int kInitialHeight = 240;

jackychen's avatar
jackychen committed
85
struct FrameInfo {
86
  FrameInfo(aom_codec_pts_t _pts, unsigned int _w, unsigned int _h)
jackychen's avatar
jackychen committed
87 88
      : pts(_pts), w(_w), h(_h) {}

89
  aom_codec_pts_t pts;
jackychen's avatar
jackychen committed
90 91 92 93
  unsigned int w;
  unsigned int h;
};

clang-format's avatar
clang-format committed
94 95 96
void ScaleForFrameNumber(unsigned int frame, unsigned int initial_w,
                         unsigned int initial_h, unsigned int *w,
                         unsigned int *h, int flag_codec) {
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
  if (frame < 10) {
    *w = initial_w;
    *h = initial_h;
    return;
  }
  if (frame < 20) {
    *w = initial_w * 3 / 4;
    *h = initial_h * 3 / 4;
    return;
  }
  if (frame < 30) {
    *w = initial_w / 2;
    *h = initial_h / 2;
    return;
  }
  if (frame < 40) {
    *w = initial_w;
    *h = initial_h;
    return;
  }
  if (frame < 50) {
    *w = initial_w * 3 / 4;
    *h = initial_h * 3 / 4;
    return;
  }
  if (frame < 60) {
    *w = initial_w / 2;
    *h = initial_h / 2;
    return;
  }
  if (frame < 70) {
    *w = initial_w;
    *h = initial_h;
    return;
  }
  if (frame < 80) {
    *w = initial_w * 3 / 4;
    *h = initial_h * 3 / 4;
    return;
  }
  if (frame < 90) {
    *w = initial_w / 2;
    *h = initial_h / 2;
    return;
  }
  if (frame < 100) {
    *w = initial_w * 3 / 4;
    *h = initial_h * 3 / 4;
    return;
  }
  if (frame < 110) {
    *w = initial_w;
    *h = initial_h;
    return;
  }
  if (frame < 120) {
    *w = initial_w * 3 / 4;
    *h = initial_h * 3 / 4;
    return;
  }
  if (frame < 130) {
    *w = initial_w / 2;
    *h = initial_h / 2;
    return;
  }
  if (frame < 140) {
    *w = initial_w * 3 / 4;
    *h = initial_h * 3 / 4;
    return;
  }
  if (frame < 150) {
    *w = initial_w;
    *h = initial_h;
    return;
  }
  if (frame < 160) {
    *w = initial_w * 3 / 4;
    *h = initial_h * 3 / 4;
    return;
  }
  if (frame < 170) {
    *w = initial_w / 2;
    *h = initial_h / 2;
    return;
  }
  if (frame < 180) {
    *w = initial_w * 3 / 4;
    *h = initial_h * 3 / 4;
    return;
  }
  if (frame < 190) {
    *w = initial_w;
    *h = initial_h;
    return;
  }
  if (frame < 200) {
    *w = initial_w * 3 / 4;
    *h = initial_h * 3 / 4;
    return;
  }
  if (frame < 210) {
    *w = initial_w / 2;
    *h = initial_h / 2;
    return;
  }
  if (frame < 220) {
    *w = initial_w * 3 / 4;
    *h = initial_h * 3 / 4;
    return;
  }
  if (frame < 230) {
    *w = initial_w;
    *h = initial_h;
    return;
  }
  if (frame < 240) {
    *w = initial_w * 3 / 4;
    *h = initial_h * 3 / 4;
    return;
  }
  if (frame < 250) {
clang-format's avatar
clang-format committed
218
    *w = initial_w / 2;
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
    *h = initial_h / 2;
    return;
  }
  if (frame < 260) {
    *w = initial_w;
    *h = initial_h;
    return;
  }
  // Go down very low.
  if (frame < 270) {
    *w = initial_w / 4;
    *h = initial_h / 4;
    return;
  }
  if (flag_codec == 1) {
234 235
    // Cases that only works for AV1.
    // For AV1: Swap width and height of original.
236 237 238 239 240 241 242 243
    if (frame < 320) {
      *w = initial_h;
      *h = initial_w;
      return;
    }
  }
  *w = initial_w;
  *h = initial_h;
John Koleszar's avatar
John Koleszar committed
244 245
}

246
class ResizingVideoSource : public ::libaom_test::DummyVideoSource {
John Koleszar's avatar
John Koleszar committed
247 248 249
 public:
  ResizingVideoSource() {
    SetSize(kInitialWidth, kInitialHeight);
250
    limit_ = 350;
John Koleszar's avatar
John Koleszar committed
251
  }
252
  int flag_codec_;
253 254
  virtual ~ResizingVideoSource() {}

John Koleszar's avatar
John Koleszar committed
255 256 257
 protected:
  virtual void Next() {
    ++frame_;
258 259 260 261 262
    unsigned int width;
    unsigned int height;
    ScaleForFrameNumber(frame_, kInitialWidth, kInitialHeight, &width, &height,
                        flag_codec_);
    SetSize(width, height);
John Koleszar's avatar
John Koleszar committed
263 264 265 266
    FillFrame();
  }
};

clang-format's avatar
clang-format committed
267
class ResizeTest
268 269
    : public ::libaom_test::CodecTestWithParam<libaom_test::TestMode>,
      public ::libaom_test::EncoderTest {
John Koleszar's avatar
John Koleszar committed
270
 protected:
271 272
  ResizeTest() : EncoderTest(GET_PARAM(0)) {}

273 274
  virtual ~ResizeTest() {}

John Koleszar's avatar
John Koleszar committed
275 276
  virtual void SetUp() {
    InitializeConfig();
277
    SetMode(GET_PARAM(1));
John Koleszar's avatar
John Koleszar committed
278 279
  }

280 281
  virtual void DecompressedFrameHook(const aom_image_t &img,
                                     aom_codec_pts_t pts) {
282
    frame_info_list_.push_back(FrameInfo(pts, img.d_w, img.d_h));
John Koleszar's avatar
John Koleszar committed
283 284
  }

clang-format's avatar
clang-format committed
285
  std::vector<FrameInfo> frame_info_list_;
John Koleszar's avatar
John Koleszar committed
286 287 288 289
};

TEST_P(ResizeTest, TestExternalResizeWorks) {
  ResizingVideoSource video;
290
  video.flag_codec_ = 0;
291
  cfg_.g_lag_in_frames = 0;
John Koleszar's avatar
John Koleszar committed
292 293
  ASSERT_NO_FATAL_FAILURE(RunLoop(&video));

294
  for (std::vector<FrameInfo>::const_iterator info = frame_info_list_.begin();
John Koleszar's avatar
John Koleszar committed
295
       info != frame_info_list_.end(); ++info) {
James Zern's avatar
James Zern committed
296
    const unsigned int frame = static_cast<unsigned>(info->pts);
297 298
    unsigned int expected_w;
    unsigned int expected_h;
clang-format's avatar
clang-format committed
299 300
    ScaleForFrameNumber(frame, kInitialWidth, kInitialHeight, &expected_w,
                        &expected_h, 0);
clang-format's avatar
clang-format committed
301 302 303 304
    EXPECT_EQ(expected_w, info->w)
        << "Frame " << frame << " had unexpected width";
    EXPECT_EQ(expected_h, info->h)
        << "Frame " << frame << " had unexpected height";
John Koleszar's avatar
John Koleszar committed
305 306 307
  }
}

308 309 310
const unsigned int kStepDownFrame = 3;
const unsigned int kStepUpFrame = 6;

311
class ResizeInternalTestLarge : public ResizeTest {
312
 protected:
313
#if WRITE_COMPRESSED_STREAM
314
  ResizeInternalTestLarge()
clang-format's avatar
clang-format committed
315
      : ResizeTest(), frame0_psnr_(0.0), outfile_(NULL), out_frames_(0) {}
316
#else
317
  ResizeInternalTestLarge() : ResizeTest(), frame0_psnr_(0.0) {}
318 319
#endif

320
  virtual ~ResizeInternalTestLarge() {}
321 322 323

  virtual void BeginPassHook(unsigned int /*pass*/) {
#if WRITE_COMPRESSED_STREAM
324
    outfile_ = fopen("av10-2-05-resize.ivf", "wb");
325 326 327 328 329 330 331 332 333 334 335 336 337
#endif
  }

  virtual void EndPassHook() {
#if WRITE_COMPRESSED_STREAM
    if (outfile_) {
      if (!fseek(outfile_, 0, SEEK_SET))
        write_ivf_file_header(&cfg_, out_frames_, outfile_);
      fclose(outfile_);
      outfile_ = NULL;
    }
#endif
  }
338

339 340
  virtual void PreEncodeFrameHook(libaom_test::VideoSource *video,
                                  libaom_test::Encoder *encoder) {
341 342 343
    if (change_config_) {
      int new_q = 60;
      if (video->frame() == 0) {
344 345
        struct aom_scaling_mode mode = { AOME_ONETWO, AOME_ONETWO };
        encoder->Control(AOME_SET_SCALEMODE, &mode);
346 347
      }
      if (video->frame() == 1) {
348 349
        struct aom_scaling_mode mode = { AOME_NORMAL, AOME_NORMAL };
        encoder->Control(AOME_SET_SCALEMODE, &mode);
350 351 352 353
        cfg_.rc_min_quantizer = cfg_.rc_max_quantizer = new_q;
        encoder->Config(&cfg_);
      }
    } else {
354
      if (video->frame() >= kStepDownFrame && video->frame() < kStepUpFrame) {
355 356
        struct aom_scaling_mode mode = { AOME_FOURFIVE, AOME_THREEFIVE };
        encoder->Control(AOME_SET_SCALEMODE, &mode);
357
      }
358
      if (video->frame() >= kStepUpFrame) {
359 360
        struct aom_scaling_mode mode = { AOME_NORMAL, AOME_NORMAL };
        encoder->Control(AOME_SET_SCALEMODE, &mode);
361
      }
362
    }
363
  }
364

365
  virtual void PSNRPktHook(const aom_codec_cx_pkt_t *pkt) {
clang-format's avatar
clang-format committed
366
    if (frame0_psnr_ == 0.) frame0_psnr_ = pkt->data.psnr.psnr[0];
367
    EXPECT_NEAR(pkt->data.psnr.psnr[0], frame0_psnr_, 2.5);
368 369
  }

370
#if WRITE_COMPRESSED_STREAM
371
  virtual void FramePktHook(const aom_codec_cx_pkt_t *pkt) {
372 373 374
    ++out_frames_;

    // Write initial file header if first frame.
clang-format's avatar
clang-format committed
375
    if (pkt->data.frame.pts == 0) write_ivf_file_header(&cfg_, 0, outfile_);
376 377 378 379 380

    // Write frame header and data.
    write_ivf_frame_header(pkt, outfile_);
    (void)fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz, outfile_);
  }
381
#endif
382

383
  double frame0_psnr_;
384
  bool change_config_;
385 386 387 388
#if WRITE_COMPRESSED_STREAM
  FILE *outfile_;
  unsigned int out_frames_;
#endif
389 390
};

391
TEST_P(ResizeInternalTestLarge, TestInternalResizeWorks) {
392
  ::libaom_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
393
                                       30, 1, 0, 10);
394
  init_flags_ = AOM_CODEC_USE_PSNR;
395
  change_config_ = false;
396

397 398 399
  // q picked such that initial keyframe on this clip is ~30dB PSNR
  cfg_.rc_min_quantizer = cfg_.rc_max_quantizer = 48;

400 401 402 403 404
  // If the number of frames being encoded is smaller than g_lag_in_frames
  // the encoded frame is unavailable using the current API. Comparing
  // frames to detect mismatch would then not be possible. Set
  // g_lag_in_frames = 0 to get around this.
  cfg_.g_lag_in_frames = 0;
405 406
  ASSERT_NO_FATAL_FAILURE(RunLoop(&video));

407 408 409
  for (std::vector<FrameInfo>::const_iterator info = frame_info_list_.begin();
       info != frame_info_list_.end(); ++info) {
  }
410
  for (std::vector<FrameInfo>::const_iterator info = frame_info_list_.begin();
411
       info != frame_info_list_.end(); ++info) {
412
    const aom_codec_pts_t pts = info->pts;
413
    if (pts >= kStepDownFrame && pts < kStepUpFrame) {
414 415 416 417 418 419 420 421 422
      ASSERT_EQ(282U, info->w) << "Frame " << pts << " had unexpected width";
      ASSERT_EQ(173U, info->h) << "Frame " << pts << " had unexpected height";
    } else {
      EXPECT_EQ(352U, info->w) << "Frame " << pts << " had unexpected width";
      EXPECT_EQ(288U, info->h) << "Frame " << pts << " had unexpected height";
    }
  }
}

423
TEST_P(ResizeInternalTestLarge, TestInternalResizeChangeConfig) {
424
  ::libaom_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
425 426 427 428 429 430 431
                                       30, 1, 0, 10);
  cfg_.g_w = 352;
  cfg_.g_h = 288;
  change_config_ = true;
  ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
}

clang-format's avatar
clang-format committed
432
class ResizeRealtimeTest
433 434
    : public ::libaom_test::CodecTestWith2Params<libaom_test::TestMode, int>,
      public ::libaom_test::EncoderTest {
jackychen's avatar
jackychen committed
435
 protected:
436 437
  ResizeRealtimeTest() : EncoderTest(GET_PARAM(0)) {}
  virtual ~ResizeRealtimeTest() {}
jackychen's avatar
jackychen committed
438

439 440
  virtual void PreEncodeFrameHook(libaom_test::VideoSource *video,
                                  libaom_test::Encoder *encoder) {
jackychen's avatar
jackychen committed
441
    if (video->frame() == 0) {
442 443
      encoder->Control(AV1E_SET_AQ_MODE, 3);
      encoder->Control(AOME_SET_CPUUSED, set_cpu_used_);
jackychen's avatar
jackychen committed
444
    }
445 446 447 448 449 450

    if (change_bitrate_ && video->frame() == 120) {
      change_bitrate_ = false;
      cfg_.rc_target_bitrate = 500;
      encoder->Config(&cfg_);
    }
jackychen's avatar
jackychen committed
451 452 453 454 455 456 457 458
  }

  virtual void SetUp() {
    InitializeConfig();
    SetMode(GET_PARAM(1));
    set_cpu_used_ = GET_PARAM(2);
  }

459 460
  virtual void DecompressedFrameHook(const aom_image_t &img,
                                     aom_codec_pts_t pts) {
jackychen's avatar
jackychen committed
461 462 463
    frame_info_list_.push_back(FrameInfo(pts, img.d_w, img.d_h));
  }

464
  virtual void MismatchHook(const aom_image_t *img1, const aom_image_t *img2) {
465 466 467 468 469
    double mismatch_psnr = compute_psnr(img1, img2);
    mismatch_psnr_ += mismatch_psnr;
    ++mismatch_nframes_;
  }

clang-format's avatar
clang-format committed
470
  unsigned int GetMismatchFrames() { return mismatch_nframes_; }
471

jackychen's avatar
jackychen committed
472 473 474 475 476 477 478 479
  void DefaultConfig() {
    cfg_.rc_buf_initial_sz = 500;
    cfg_.rc_buf_optimal_sz = 600;
    cfg_.rc_buf_sz = 1000;
    cfg_.rc_min_quantizer = 2;
    cfg_.rc_max_quantizer = 56;
    cfg_.rc_undershoot_pct = 50;
    cfg_.rc_overshoot_pct = 50;
480 481
    cfg_.rc_end_usage = AOM_CBR;
    cfg_.kf_mode = AOM_KF_AUTO;
jackychen's avatar
jackychen committed
482 483 484 485
    cfg_.g_lag_in_frames = 0;
    cfg_.kf_min_dist = cfg_.kf_max_dist = 3000;
    // Enable dropped frames.
    cfg_.rc_dropframe_thresh = 1;
486 487
    // Disable error_resilience mode.
    cfg_.g_error_resilient = 0;
jackychen's avatar
jackychen committed
488 489 490 491
    // Run at low bitrate.
    cfg_.rc_target_bitrate = 200;
  }

clang-format's avatar
clang-format committed
492
  std::vector<FrameInfo> frame_info_list_;
jackychen's avatar
jackychen committed
493
  int set_cpu_used_;
494
  bool change_bitrate_;
495 496
  double mismatch_psnr_;
  int mismatch_nframes_;
jackychen's avatar
jackychen committed
497 498
};

499
TEST_P(ResizeRealtimeTest, TestExternalResizeWorks) {
500
  ResizingVideoSource video;
501
  video.flag_codec_ = 1;
502 503
  DefaultConfig();
  change_bitrate_ = false;
504 505
  mismatch_psnr_ = 0.0;
  mismatch_nframes_ = 0;
506 507 508 509 510
  ASSERT_NO_FATAL_FAILURE(RunLoop(&video));

  for (std::vector<FrameInfo>::const_iterator info = frame_info_list_.begin();
       info != frame_info_list_.end(); ++info) {
    const unsigned int frame = static_cast<unsigned>(info->pts);
511 512
    unsigned int expected_w;
    unsigned int expected_h;
clang-format's avatar
clang-format committed
513 514
    ScaleForFrameNumber(frame, kInitialWidth, kInitialHeight, &expected_w,
                        &expected_h, 1);
clang-format's avatar
clang-format committed
515 516 517 518
    EXPECT_EQ(expected_w, info->w)
        << "Frame " << frame << " had unexpected width";
    EXPECT_EQ(expected_h, info->h)
        << "Frame " << frame << " had unexpected height";
519
    EXPECT_EQ(static_cast<unsigned int>(0), GetMismatchFrames());
520 521 522
  }
}

jackychen's avatar
jackychen committed
523 524 525
// Verify the dynamic resizer behavior for real time, 1 pass CBR mode.
// Run at low bitrate, with resize_allowed = 1, and verify that we get
// one resize down event.
Urvang Joshi's avatar
Urvang Joshi committed
526
TEST_P(ResizeRealtimeTest, DISABLED_TestInternalResizeDown) {
527
  ::libaom_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
jackychen's avatar
jackychen committed
528 529
                                       30, 1, 0, 299);
  DefaultConfig();
530 531
  cfg_.g_w = 352;
  cfg_.g_h = 288;
532
  change_bitrate_ = false;
533 534
  mismatch_psnr_ = 0.0;
  mismatch_nframes_ = 0;
jackychen's avatar
jackychen committed
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
  ASSERT_NO_FATAL_FAILURE(RunLoop(&video));

  unsigned int last_w = cfg_.g_w;
  unsigned int last_h = cfg_.g_h;
  int resize_count = 0;
  for (std::vector<FrameInfo>::const_iterator info = frame_info_list_.begin();
       info != frame_info_list_.end(); ++info) {
    if (info->w != last_w || info->h != last_h) {
      // Verify that resize down occurs.
      ASSERT_LT(info->w, last_w);
      ASSERT_LT(info->h, last_h);
      last_w = info->w;
      last_h = info->h;
      resize_count++;
    }
  }

552
#if CONFIG_AV1_DECODER
jackychen's avatar
jackychen committed
553 554
  // Verify that we get 1 resize down event in this test.
  ASSERT_EQ(1, resize_count) << "Resizing should occur.";
555
  EXPECT_EQ(static_cast<unsigned int>(0), GetMismatchFrames());
556
#else
557
  printf("Warning: AV1 decoder unavailable, unable to check resize count!\n");
558
#endif
jackychen's avatar
jackychen committed
559 560
}

561 562 563
// Verify the dynamic resizer behavior for real time, 1 pass CBR mode.
// Start at low target bitrate, raise the bitrate in the middle of the clip,
// scaling-up should occur after bitrate changed.
Urvang Joshi's avatar
Urvang Joshi committed
564
TEST_P(ResizeRealtimeTest, DISABLED_TestInternalResizeDownUpChangeBitRate) {
565
  ::libaom_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
566
                                       30, 1, 0, 359);
567
  DefaultConfig();
568 569
  cfg_.g_w = 352;
  cfg_.g_h = 288;
570
  change_bitrate_ = true;
571 572
  mismatch_psnr_ = 0.0;
  mismatch_nframes_ = 0;
573 574 575
  // Disable dropped frames.
  cfg_.rc_dropframe_thresh = 0;
  // Starting bitrate low.
576
  cfg_.rc_target_bitrate = 80;
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
  ASSERT_NO_FATAL_FAILURE(RunLoop(&video));

  unsigned int last_w = cfg_.g_w;
  unsigned int last_h = cfg_.g_h;
  int resize_count = 0;
  for (std::vector<FrameInfo>::const_iterator info = frame_info_list_.begin();
       info != frame_info_list_.end(); ++info) {
    if (info->w != last_w || info->h != last_h) {
      resize_count++;
      if (resize_count == 1) {
        // Verify that resize down occurs.
        ASSERT_LT(info->w, last_w);
        ASSERT_LT(info->h, last_h);
      } else if (resize_count == 2) {
        // Verify that resize up occurs.
        ASSERT_GT(info->w, last_w);
        ASSERT_GT(info->h, last_h);
      }
      last_w = info->w;
      last_h = info->h;
    }
  }

600
#if CONFIG_AV1_DECODER
601 602
  // Verify that we get 2 resize events in this test.
  ASSERT_EQ(resize_count, 2) << "Resizing should occur twice.";
603
  EXPECT_EQ(static_cast<unsigned int>(0), GetMismatchFrames());
604
#else
605
  printf("Warning: AV1 decoder unavailable, unable to check resize count!\n");
606
#endif
607 608
}

609 610 611 612
aom_img_fmt_t CspForFrameNumber(int frame) {
  if (frame < 10) return AOM_IMG_FMT_I420;
  if (frame < 20) return AOM_IMG_FMT_I444;
  return AOM_IMG_FMT_I420;
613 614 615 616 617 618
}

class ResizeCspTest : public ResizeTest {
 protected:
#if WRITE_COMPRESSED_STREAM
  ResizeCspTest()
clang-format's avatar
clang-format committed
619
      : ResizeTest(), frame0_psnr_(0.0), outfile_(NULL), out_frames_(0) {}
620 621 622 623 624 625 626 627
#else
  ResizeCspTest() : ResizeTest(), frame0_psnr_(0.0) {}
#endif

  virtual ~ResizeCspTest() {}

  virtual void BeginPassHook(unsigned int /*pass*/) {
#if WRITE_COMPRESSED_STREAM
628
    outfile_ = fopen("av11-2-05-cspchape.ivf", "wb");
629 630 631 632 633 634 635 636 637 638 639 640 641 642
#endif
  }

  virtual void EndPassHook() {
#if WRITE_COMPRESSED_STREAM
    if (outfile_) {
      if (!fseek(outfile_, 0, SEEK_SET))
        write_ivf_file_header(&cfg_, out_frames_, outfile_);
      fclose(outfile_);
      outfile_ = NULL;
    }
#endif
  }

643 644
  virtual void PreEncodeFrameHook(libaom_test::VideoSource *video,
                                  libaom_test::Encoder *encoder) {
645
    if (CspForFrameNumber(video->frame()) != AOM_IMG_FMT_I420 &&
646 647 648 649
        cfg_.g_profile != 1) {
      cfg_.g_profile = 1;
      encoder->Config(&cfg_);
    }
650
    if (CspForFrameNumber(video->frame()) == AOM_IMG_FMT_I420 &&
651 652 653 654 655 656
        cfg_.g_profile != 0) {
      cfg_.g_profile = 0;
      encoder->Config(&cfg_);
    }
  }

657
  virtual void PSNRPktHook(const aom_codec_cx_pkt_t *pkt) {
clang-format's avatar
clang-format committed
658
    if (frame0_psnr_ == 0.) frame0_psnr_ = pkt->data.psnr.psnr[0];
659 660 661 662
    EXPECT_NEAR(pkt->data.psnr.psnr[0], frame0_psnr_, 2.0);
  }

#if WRITE_COMPRESSED_STREAM
663
  virtual void FramePktHook(const aom_codec_cx_pkt_t *pkt) {
664 665 666
    ++out_frames_;

    // Write initial file header if first frame.
clang-format's avatar
clang-format committed
667
    if (pkt->data.frame.pts == 0) write_ivf_file_header(&cfg_, 0, outfile_);
668 669 670 671 672 673 674 675 676 677 678 679 680 681

    // Write frame header and data.
    write_ivf_frame_header(pkt, outfile_);
    (void)fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz, outfile_);
  }
#endif

  double frame0_psnr_;
#if WRITE_COMPRESSED_STREAM
  FILE *outfile_;
  unsigned int out_frames_;
#endif
};

682
class ResizingCspVideoSource : public ::libaom_test::DummyVideoSource {
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
 public:
  ResizingCspVideoSource() {
    SetSize(kInitialWidth, kInitialHeight);
    limit_ = 30;
  }

  virtual ~ResizingCspVideoSource() {}

 protected:
  virtual void Next() {
    ++frame_;
    SetImageFormat(CspForFrameNumber(frame_));
    FillFrame();
  }
};

699
#if (defined(DISABLE_TRELLISQ_SEARCH) && DISABLE_TRELLISQ_SEARCH)
700 701
TEST_P(ResizeCspTest, DISABLED_TestResizeCspWorks) {
#else
702
TEST_P(ResizeCspTest, TestResizeCspWorks) {
703
#endif
704
  ResizingCspVideoSource video;
705
  init_flags_ = AOM_CODEC_USE_PSNR;
706 707 708 709 710
  cfg_.rc_min_quantizer = cfg_.rc_max_quantizer = 48;
  cfg_.g_lag_in_frames = 0;
  ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
}

711 712
AV1_INSTANTIATE_TEST_CASE(ResizeTest,
                          ::testing::Values(::libaom_test::kRealTime));
713
AV1_INSTANTIATE_TEST_CASE(ResizeInternalTestLarge,
Urvang Joshi's avatar
Urvang Joshi committed
714
                          ::testing::Values(::libaom_test::kOnePassGood));
715 716 717 718 719
AV1_INSTANTIATE_TEST_CASE(ResizeRealtimeTest,
                          ::testing::Values(::libaom_test::kRealTime),
                          ::testing::Range(5, 9));
AV1_INSTANTIATE_TEST_CASE(ResizeCspTest,
                          ::testing::Values(::libaom_test::kRealTime));
John Koleszar's avatar
John Koleszar committed
720
}  // namespace