resize_test.cc 20 KB

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