resize_test.cc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  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 FramePktHook(const vpx_codec_cx_pkt_t *pkt) {
  258. ASSERT_NE(static_cast<int>(pkt->data.frame.width[0]), 0);
  259. ASSERT_NE(static_cast<int>(pkt->data.frame.height[0]), 0);
  260. encode_frame_width_.push_back(pkt->data.frame.width[0]);
  261. encode_frame_height_.push_back(pkt->data.frame.height[0]);
  262. }
  263. unsigned int GetFrameWidth(size_t idx) const {
  264. return encode_frame_width_[idx];
  265. }
  266. unsigned int GetFrameHeight(size_t idx) const {
  267. return encode_frame_height_[idx];
  268. }
  269. virtual void DecompressedFrameHook(const vpx_image_t &img,
  270. vpx_codec_pts_t pts) {
  271. frame_info_list_.push_back(FrameInfo(pts, img.d_w, img.d_h));
  272. }
  273. std::vector<FrameInfo> frame_info_list_;
  274. std::vector<unsigned int> encode_frame_width_;
  275. std::vector<unsigned int> encode_frame_height_;
  276. };
  277. TEST_P(ResizeTest, TestExternalResizeWorks) {
  278. ResizingVideoSource video;
  279. video.flag_codec_ = 0;
  280. cfg_.g_lag_in_frames = 0;
  281. ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
  282. for (std::vector<FrameInfo>::const_iterator info = frame_info_list_.begin();
  283. info != frame_info_list_.end(); ++info) {
  284. const unsigned int frame = static_cast<unsigned>(info->pts);
  285. unsigned int expected_w;
  286. unsigned int expected_h;
  287. const size_t idx = info - frame_info_list_.begin();
  288. ASSERT_EQ(info->w, GetFrameWidth(idx));
  289. ASSERT_EQ(info->h, GetFrameHeight(idx));
  290. ScaleForFrameNumber(frame, kInitialWidth, kInitialHeight, &expected_w,
  291. &expected_h, 0);
  292. EXPECT_EQ(expected_w, info->w)
  293. << "Frame " << frame << " had unexpected width";
  294. EXPECT_EQ(expected_h, info->h)
  295. << "Frame " << frame << " had unexpected height";
  296. }
  297. }
  298. const unsigned int kStepDownFrame = 3;
  299. const unsigned int kStepUpFrame = 6;
  300. class ResizeInternalTest : public ResizeTest {
  301. protected:
  302. #if WRITE_COMPRESSED_STREAM
  303. ResizeInternalTest()
  304. : ResizeTest(), frame0_psnr_(0.0), outfile_(NULL), out_frames_(0) {}
  305. #else
  306. ResizeInternalTest() : ResizeTest(), frame0_psnr_(0.0) {}
  307. #endif
  308. virtual ~ResizeInternalTest() {}
  309. virtual void BeginPassHook(unsigned int /*pass*/) {
  310. #if WRITE_COMPRESSED_STREAM
  311. outfile_ = fopen("vp90-2-05-resize.ivf", "wb");
  312. #endif
  313. }
  314. virtual void EndPassHook() {
  315. #if WRITE_COMPRESSED_STREAM
  316. if (outfile_) {
  317. if (!fseek(outfile_, 0, SEEK_SET))
  318. write_ivf_file_header(&cfg_, out_frames_, outfile_);
  319. fclose(outfile_);
  320. outfile_ = NULL;
  321. }
  322. #endif
  323. }
  324. virtual void PreEncodeFrameHook(libvpx_test::VideoSource *video,
  325. libvpx_test::Encoder *encoder) {
  326. if (change_config_) {
  327. int new_q = 60;
  328. if (video->frame() == 0) {
  329. struct vpx_scaling_mode mode = { VP8E_ONETWO, VP8E_ONETWO };
  330. encoder->Control(VP8E_SET_SCALEMODE, &mode);
  331. }
  332. if (video->frame() == 1) {
  333. struct vpx_scaling_mode mode = { VP8E_NORMAL, VP8E_NORMAL };
  334. encoder->Control(VP8E_SET_SCALEMODE, &mode);
  335. cfg_.rc_min_quantizer = cfg_.rc_max_quantizer = new_q;
  336. encoder->Config(&cfg_);
  337. }
  338. } else {
  339. if (video->frame() == kStepDownFrame) {
  340. struct vpx_scaling_mode mode = { VP8E_FOURFIVE, VP8E_THREEFIVE };
  341. encoder->Control(VP8E_SET_SCALEMODE, &mode);
  342. }
  343. if (video->frame() == kStepUpFrame) {
  344. struct vpx_scaling_mode mode = { VP8E_NORMAL, VP8E_NORMAL };
  345. encoder->Control(VP8E_SET_SCALEMODE, &mode);
  346. }
  347. }
  348. }
  349. virtual void PSNRPktHook(const vpx_codec_cx_pkt_t *pkt) {
  350. if (frame0_psnr_ == 0.) frame0_psnr_ = pkt->data.psnr.psnr[0];
  351. EXPECT_NEAR(pkt->data.psnr.psnr[0], frame0_psnr_, 2.0);
  352. }
  353. #if WRITE_COMPRESSED_STREAM
  354. virtual void FramePktHook(const vpx_codec_cx_pkt_t *pkt) {
  355. ++out_frames_;
  356. // Write initial file header if first frame.
  357. if (pkt->data.frame.pts == 0) write_ivf_file_header(&cfg_, 0, outfile_);
  358. // Write frame header and data.
  359. write_ivf_frame_header(pkt, outfile_);
  360. (void)fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz, outfile_);
  361. }
  362. #endif
  363. double frame0_psnr_;
  364. bool change_config_;
  365. #if WRITE_COMPRESSED_STREAM
  366. FILE *outfile_;
  367. unsigned int out_frames_;
  368. #endif
  369. };
  370. TEST_P(ResizeInternalTest, TestInternalResizeWorks) {
  371. ::libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
  372. 30, 1, 0, 10);
  373. init_flags_ = VPX_CODEC_USE_PSNR;
  374. change_config_ = false;
  375. // q picked such that initial keyframe on this clip is ~30dB PSNR
  376. cfg_.rc_min_quantizer = cfg_.rc_max_quantizer = 48;
  377. // If the number of frames being encoded is smaller than g_lag_in_frames
  378. // the encoded frame is unavailable using the current API. Comparing
  379. // frames to detect mismatch would then not be possible. Set
  380. // g_lag_in_frames = 0 to get around this.
  381. cfg_.g_lag_in_frames = 0;
  382. ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
  383. for (std::vector<FrameInfo>::const_iterator info = frame_info_list_.begin();
  384. info != frame_info_list_.end(); ++info) {
  385. const vpx_codec_pts_t pts = info->pts;
  386. if (pts >= kStepDownFrame && pts < kStepUpFrame) {
  387. ASSERT_EQ(282U, info->w) << "Frame " << pts << " had unexpected width";
  388. ASSERT_EQ(173U, info->h) << "Frame " << pts << " had unexpected height";
  389. } else {
  390. EXPECT_EQ(352U, info->w) << "Frame " << pts << " had unexpected width";
  391. EXPECT_EQ(288U, info->h) << "Frame " << pts << " had unexpected height";
  392. }
  393. }
  394. }
  395. TEST_P(ResizeInternalTest, TestInternalResizeChangeConfig) {
  396. ::libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
  397. 30, 1, 0, 10);
  398. cfg_.g_w = 352;
  399. cfg_.g_h = 288;
  400. change_config_ = true;
  401. ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
  402. }
  403. class ResizeRealtimeTest
  404. : public ::libvpx_test::EncoderTest,
  405. public ::libvpx_test::CodecTestWith2Params<libvpx_test::TestMode, int> {
  406. protected:
  407. ResizeRealtimeTest() : EncoderTest(GET_PARAM(0)) {}
  408. virtual ~ResizeRealtimeTest() {}
  409. virtual void PreEncodeFrameHook(libvpx_test::VideoSource *video,
  410. libvpx_test::Encoder *encoder) {
  411. if (video->frame() == 0) {
  412. encoder->Control(VP9E_SET_AQ_MODE, 3);
  413. encoder->Control(VP8E_SET_CPUUSED, set_cpu_used_);
  414. }
  415. if (change_bitrate_ && video->frame() == 120) {
  416. change_bitrate_ = false;
  417. cfg_.rc_target_bitrate = 500;
  418. encoder->Config(&cfg_);
  419. }
  420. }
  421. virtual void SetUp() {
  422. InitializeConfig();
  423. SetMode(GET_PARAM(1));
  424. set_cpu_used_ = GET_PARAM(2);
  425. }
  426. virtual void DecompressedFrameHook(const vpx_image_t &img,
  427. vpx_codec_pts_t pts) {
  428. frame_info_list_.push_back(FrameInfo(pts, img.d_w, img.d_h));
  429. }
  430. virtual void MismatchHook(const vpx_image_t *img1, const vpx_image_t *img2) {
  431. double mismatch_psnr = compute_psnr(img1, img2);
  432. mismatch_psnr_ += mismatch_psnr;
  433. ++mismatch_nframes_;
  434. }
  435. virtual void FramePktHook(const vpx_codec_cx_pkt_t *pkt) {
  436. ASSERT_NE(static_cast<int>(pkt->data.frame.width[0]), 0);
  437. ASSERT_NE(static_cast<int>(pkt->data.frame.height[0]), 0);
  438. encode_frame_width_.push_back(pkt->data.frame.width[0]);
  439. encode_frame_height_.push_back(pkt->data.frame.height[0]);
  440. }
  441. unsigned int GetMismatchFrames() { return mismatch_nframes_; }
  442. unsigned int GetFrameWidth(size_t idx) const {
  443. return encode_frame_width_[idx];
  444. }
  445. unsigned int GetFrameHeight(size_t idx) const {
  446. return encode_frame_height_[idx];
  447. }
  448. void DefaultConfig() {
  449. cfg_.rc_buf_initial_sz = 500;
  450. cfg_.rc_buf_optimal_sz = 600;
  451. cfg_.rc_buf_sz = 1000;
  452. cfg_.rc_min_quantizer = 2;
  453. cfg_.rc_max_quantizer = 56;
  454. cfg_.rc_undershoot_pct = 50;
  455. cfg_.rc_overshoot_pct = 50;
  456. cfg_.rc_end_usage = VPX_CBR;
  457. cfg_.kf_mode = VPX_KF_AUTO;
  458. cfg_.g_lag_in_frames = 0;
  459. cfg_.kf_min_dist = cfg_.kf_max_dist = 3000;
  460. // Enable dropped frames.
  461. cfg_.rc_dropframe_thresh = 1;
  462. // Enable error_resilience mode.
  463. cfg_.g_error_resilient = 1;
  464. // Enable dynamic resizing.
  465. cfg_.rc_resize_allowed = 1;
  466. // Run at low bitrate.
  467. cfg_.rc_target_bitrate = 200;
  468. }
  469. std::vector<FrameInfo> frame_info_list_;
  470. int set_cpu_used_;
  471. bool change_bitrate_;
  472. double mismatch_psnr_;
  473. int mismatch_nframes_;
  474. std::vector<unsigned int> encode_frame_width_;
  475. std::vector<unsigned int> encode_frame_height_;
  476. };
  477. TEST_P(ResizeRealtimeTest, TestExternalResizeWorks) {
  478. ResizingVideoSource video;
  479. video.flag_codec_ = 1;
  480. DefaultConfig();
  481. // Disable internal resize for this test.
  482. cfg_.rc_resize_allowed = 0;
  483. change_bitrate_ = false;
  484. mismatch_psnr_ = 0.0;
  485. mismatch_nframes_ = 0;
  486. ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
  487. for (std::vector<FrameInfo>::const_iterator info = frame_info_list_.begin();
  488. info != frame_info_list_.end(); ++info) {
  489. const unsigned int frame = static_cast<unsigned>(info->pts);
  490. unsigned int expected_w;
  491. unsigned int expected_h;
  492. ScaleForFrameNumber(frame, kInitialWidth, kInitialHeight, &expected_w,
  493. &expected_h, 1);
  494. EXPECT_EQ(expected_w, info->w)
  495. << "Frame " << frame << " had unexpected width";
  496. EXPECT_EQ(expected_h, info->h)
  497. << "Frame " << frame << " had unexpected height";
  498. EXPECT_EQ(static_cast<unsigned int>(0), GetMismatchFrames());
  499. }
  500. }
  501. // Verify the dynamic resizer behavior for real time, 1 pass CBR mode.
  502. // Run at low bitrate, with resize_allowed = 1, and verify that we get
  503. // one resize down event.
  504. TEST_P(ResizeRealtimeTest, TestInternalResizeDown) {
  505. ::libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
  506. 30, 1, 0, 299);
  507. DefaultConfig();
  508. cfg_.g_w = 352;
  509. cfg_.g_h = 288;
  510. change_bitrate_ = false;
  511. mismatch_psnr_ = 0.0;
  512. mismatch_nframes_ = 0;
  513. ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
  514. unsigned int last_w = cfg_.g_w;
  515. unsigned int last_h = cfg_.g_h;
  516. int resize_count = 0;
  517. for (std::vector<FrameInfo>::const_iterator info = frame_info_list_.begin();
  518. info != frame_info_list_.end(); ++info) {
  519. if (info->w != last_w || info->h != last_h) {
  520. // Verify that resize down occurs.
  521. ASSERT_LT(info->w, last_w);
  522. ASSERT_LT(info->h, last_h);
  523. last_w = info->w;
  524. last_h = info->h;
  525. resize_count++;
  526. }
  527. }
  528. #if CONFIG_VP9_DECODER
  529. // Verify that we get 1 resize down event in this test.
  530. ASSERT_EQ(1, resize_count) << "Resizing should occur.";
  531. EXPECT_EQ(static_cast<unsigned int>(0), GetMismatchFrames());
  532. #else
  533. printf("Warning: VP9 decoder unavailable, unable to check resize count!\n");
  534. #endif
  535. }
  536. // Verify the dynamic resizer behavior for real time, 1 pass CBR mode.
  537. // Start at low target bitrate, raise the bitrate in the middle of the clip,
  538. // scaling-up should occur after bitrate changed.
  539. TEST_P(ResizeRealtimeTest, TestInternalResizeDownUpChangeBitRate) {
  540. ::libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
  541. 30, 1, 0, 359);
  542. DefaultConfig();
  543. cfg_.g_w = 352;
  544. cfg_.g_h = 288;
  545. change_bitrate_ = true;
  546. mismatch_psnr_ = 0.0;
  547. mismatch_nframes_ = 0;
  548. // Disable dropped frames.
  549. cfg_.rc_dropframe_thresh = 0;
  550. // Starting bitrate low.
  551. cfg_.rc_target_bitrate = 80;
  552. ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
  553. unsigned int last_w = cfg_.g_w;
  554. unsigned int last_h = cfg_.g_h;
  555. int resize_count = 0;
  556. for (std::vector<FrameInfo>::const_iterator info = frame_info_list_.begin();
  557. info != frame_info_list_.end(); ++info) {
  558. const size_t idx = info - frame_info_list_.begin();
  559. ASSERT_EQ(info->w, GetFrameWidth(idx));
  560. ASSERT_EQ(info->h, GetFrameHeight(idx));
  561. if (info->w != last_w || info->h != last_h) {
  562. resize_count++;
  563. if (resize_count == 1) {
  564. // Verify that resize down occurs.
  565. ASSERT_LT(info->w, last_w);
  566. ASSERT_LT(info->h, last_h);
  567. } else if (resize_count == 2) {
  568. // Verify that resize up occurs.
  569. ASSERT_GT(info->w, last_w);
  570. ASSERT_GT(info->h, last_h);
  571. }
  572. last_w = info->w;
  573. last_h = info->h;
  574. }
  575. }
  576. #if CONFIG_VP9_DECODER
  577. // Verify that we get 2 resize events in this test.
  578. ASSERT_EQ(resize_count, 2) << "Resizing should occur twice.";
  579. EXPECT_EQ(static_cast<unsigned int>(0), GetMismatchFrames());
  580. #else
  581. printf("Warning: VP9 decoder unavailable, unable to check resize count!\n");
  582. #endif
  583. }
  584. vpx_img_fmt_t CspForFrameNumber(int frame) {
  585. if (frame < 10) return VPX_IMG_FMT_I420;
  586. if (frame < 20) return VPX_IMG_FMT_I444;
  587. return VPX_IMG_FMT_I420;
  588. }
  589. class ResizeCspTest : public ResizeTest {
  590. protected:
  591. #if WRITE_COMPRESSED_STREAM
  592. ResizeCspTest()
  593. : ResizeTest(), frame0_psnr_(0.0), outfile_(NULL), out_frames_(0) {}
  594. #else
  595. ResizeCspTest() : ResizeTest(), frame0_psnr_(0.0) {}
  596. #endif
  597. virtual ~ResizeCspTest() {}
  598. virtual void BeginPassHook(unsigned int /*pass*/) {
  599. #if WRITE_COMPRESSED_STREAM
  600. outfile_ = fopen("vp91-2-05-cspchape.ivf", "wb");
  601. #endif
  602. }
  603. virtual void EndPassHook() {
  604. #if WRITE_COMPRESSED_STREAM
  605. if (outfile_) {
  606. if (!fseek(outfile_, 0, SEEK_SET))
  607. write_ivf_file_header(&cfg_, out_frames_, outfile_);
  608. fclose(outfile_);
  609. outfile_ = NULL;
  610. }
  611. #endif
  612. }
  613. virtual void PreEncodeFrameHook(libvpx_test::VideoSource *video,
  614. libvpx_test::Encoder *encoder) {
  615. if (CspForFrameNumber(video->frame()) != VPX_IMG_FMT_I420 &&
  616. cfg_.g_profile != 1) {
  617. cfg_.g_profile = 1;
  618. encoder->Config(&cfg_);
  619. }
  620. if (CspForFrameNumber(video->frame()) == VPX_IMG_FMT_I420 &&
  621. cfg_.g_profile != 0) {
  622. cfg_.g_profile = 0;
  623. encoder->Config(&cfg_);
  624. }
  625. }
  626. virtual void PSNRPktHook(const vpx_codec_cx_pkt_t *pkt) {
  627. if (frame0_psnr_ == 0.) frame0_psnr_ = pkt->data.psnr.psnr[0];
  628. EXPECT_NEAR(pkt->data.psnr.psnr[0], frame0_psnr_, 2.0);
  629. }
  630. #if WRITE_COMPRESSED_STREAM
  631. virtual void FramePktHook(const vpx_codec_cx_pkt_t *pkt) {
  632. ++out_frames_;
  633. // Write initial file header if first frame.
  634. if (pkt->data.frame.pts == 0) write_ivf_file_header(&cfg_, 0, outfile_);
  635. // Write frame header and data.
  636. write_ivf_frame_header(pkt, outfile_);
  637. (void)fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz, outfile_);
  638. }
  639. #endif
  640. double frame0_psnr_;
  641. #if WRITE_COMPRESSED_STREAM
  642. FILE *outfile_;
  643. unsigned int out_frames_;
  644. #endif
  645. };
  646. class ResizingCspVideoSource : public ::libvpx_test::DummyVideoSource {
  647. public:
  648. ResizingCspVideoSource() {
  649. SetSize(kInitialWidth, kInitialHeight);
  650. limit_ = 30;
  651. }
  652. virtual ~ResizingCspVideoSource() {}
  653. protected:
  654. virtual void Next() {
  655. ++frame_;
  656. SetImageFormat(CspForFrameNumber(frame_));
  657. FillFrame();
  658. }
  659. };
  660. TEST_P(ResizeCspTest, TestResizeCspWorks) {
  661. ResizingCspVideoSource video;
  662. init_flags_ = VPX_CODEC_USE_PSNR;
  663. cfg_.rc_min_quantizer = cfg_.rc_max_quantizer = 48;
  664. cfg_.g_lag_in_frames = 0;
  665. ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
  666. }
  667. VP8_INSTANTIATE_TEST_CASE(ResizeTest, ONE_PASS_TEST_MODES);
  668. VP9_INSTANTIATE_TEST_CASE(ResizeTest,
  669. ::testing::Values(::libvpx_test::kRealTime));
  670. VP9_INSTANTIATE_TEST_CASE(ResizeInternalTest,
  671. ::testing::Values(::libvpx_test::kOnePassBest));
  672. VP9_INSTANTIATE_TEST_CASE(ResizeRealtimeTest,
  673. ::testing::Values(::libvpx_test::kRealTime),
  674. ::testing::Range(5, 9));
  675. VP9_INSTANTIATE_TEST_CASE(ResizeCspTest,
  676. ::testing::Values(::libvpx_test::kRealTime));
  677. } // namespace