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