]> granicus.if.org Git - libvpx/blob - test/resize_test.cc
Merge "build/make/configure.sh: Fix armv7 builds in Xcode7."
[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 / 2;
98   if (frame < 30)
99     return val * 2 / 3;
100   if (frame < 40)
101     return val / 4;
102   if (frame < 50)
103     return val * 7 / 8;
104   return val;
105 }
106
107 class ResizingVideoSource : public ::libvpx_test::DummyVideoSource {
108  public:
109   ResizingVideoSource() {
110     SetSize(kInitialWidth, kInitialHeight);
111     limit_ = 60;
112   }
113
114   virtual ~ResizingVideoSource() {}
115
116  protected:
117   virtual void Next() {
118     ++frame_;
119     SetSize(ScaleForFrameNumber(frame_, kInitialWidth),
120             ScaleForFrameNumber(frame_, kInitialHeight));
121     FillFrame();
122   }
123 };
124
125 class ResizeTest : public ::libvpx_test::EncoderTest,
126   public ::libvpx_test::CodecTestWithParam<libvpx_test::TestMode> {
127  protected:
128   ResizeTest() : EncoderTest(GET_PARAM(0)) {}
129
130   virtual ~ResizeTest() {}
131
132   virtual void SetUp() {
133     InitializeConfig();
134     SetMode(GET_PARAM(1));
135   }
136
137   virtual void DecompressedFrameHook(const vpx_image_t &img,
138                                      vpx_codec_pts_t pts) {
139     frame_info_list_.push_back(FrameInfo(pts, img.d_w, img.d_h));
140   }
141
142   std::vector< FrameInfo > frame_info_list_;
143 };
144
145 TEST_P(ResizeTest, TestExternalResizeWorks) {
146   ResizingVideoSource video;
147   cfg_.g_lag_in_frames = 0;
148   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
149
150   for (std::vector<FrameInfo>::const_iterator info = frame_info_list_.begin();
151        info != frame_info_list_.end(); ++info) {
152     const unsigned int frame = static_cast<unsigned>(info->pts);
153     const unsigned int expected_w = ScaleForFrameNumber(frame, kInitialWidth);
154     const unsigned int expected_h = ScaleForFrameNumber(frame, kInitialHeight);
155
156     EXPECT_EQ(expected_w, info->w)
157         << "Frame " << frame << " had unexpected width";
158     EXPECT_EQ(expected_h, info->h)
159         << "Frame " << frame << " had unexpected height";
160   }
161 }
162
163 const unsigned int kStepDownFrame = 3;
164 const unsigned int kStepUpFrame = 6;
165
166 class ResizeInternalTest : public ResizeTest {
167  protected:
168 #if WRITE_COMPRESSED_STREAM
169   ResizeInternalTest()
170       : ResizeTest(),
171         frame0_psnr_(0.0),
172         outfile_(NULL),
173         out_frames_(0) {}
174 #else
175   ResizeInternalTest() : ResizeTest(), frame0_psnr_(0.0) {}
176 #endif
177
178   virtual ~ResizeInternalTest() {}
179
180   virtual void BeginPassHook(unsigned int /*pass*/) {
181 #if WRITE_COMPRESSED_STREAM
182     outfile_ = fopen("vp90-2-05-resize.ivf", "wb");
183 #endif
184   }
185
186   virtual void EndPassHook() {
187 #if WRITE_COMPRESSED_STREAM
188     if (outfile_) {
189       if (!fseek(outfile_, 0, SEEK_SET))
190         write_ivf_file_header(&cfg_, out_frames_, outfile_);
191       fclose(outfile_);
192       outfile_ = NULL;
193     }
194 #endif
195   }
196
197   virtual void PreEncodeFrameHook(libvpx_test::VideoSource *video,
198                                   libvpx_test::Encoder *encoder) {
199     if (video->frame() == kStepDownFrame) {
200       struct vpx_scaling_mode mode = {VP8E_FOURFIVE, VP8E_THREEFIVE};
201       encoder->Control(VP8E_SET_SCALEMODE, &mode);
202     }
203     if (video->frame() == kStepUpFrame) {
204       struct vpx_scaling_mode mode = {VP8E_NORMAL, VP8E_NORMAL};
205       encoder->Control(VP8E_SET_SCALEMODE, &mode);
206     }
207   }
208
209   virtual void PSNRPktHook(const vpx_codec_cx_pkt_t *pkt) {
210     if (!frame0_psnr_)
211       frame0_psnr_ = pkt->data.psnr.psnr[0];
212     EXPECT_NEAR(pkt->data.psnr.psnr[0], frame0_psnr_, 2.0);
213   }
214
215 #if WRITE_COMPRESSED_STREAM
216   virtual void FramePktHook(const vpx_codec_cx_pkt_t *pkt) {
217     ++out_frames_;
218
219     // Write initial file header if first frame.
220     if (pkt->data.frame.pts == 0)
221       write_ivf_file_header(&cfg_, 0, outfile_);
222
223     // Write frame header and data.
224     write_ivf_frame_header(pkt, outfile_);
225     (void)fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz, outfile_);
226   }
227 #endif
228
229   double frame0_psnr_;
230 #if WRITE_COMPRESSED_STREAM
231   FILE *outfile_;
232   unsigned int out_frames_;
233 #endif
234 };
235
236 TEST_P(ResizeInternalTest, TestInternalResizeWorks) {
237   ::libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
238                                        30, 1, 0, 10);
239   init_flags_ = VPX_CODEC_USE_PSNR;
240
241   // q picked such that initial keyframe on this clip is ~30dB PSNR
242   cfg_.rc_min_quantizer = cfg_.rc_max_quantizer = 48;
243
244   // If the number of frames being encoded is smaller than g_lag_in_frames
245   // the encoded frame is unavailable using the current API. Comparing
246   // frames to detect mismatch would then not be possible. Set
247   // g_lag_in_frames = 0 to get around this.
248   cfg_.g_lag_in_frames = 0;
249   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
250
251   for (std::vector<FrameInfo>::const_iterator info = frame_info_list_.begin();
252        info != frame_info_list_.end(); ++info) {
253     const vpx_codec_pts_t pts = info->pts;
254     if (pts >= kStepDownFrame && pts < kStepUpFrame) {
255       ASSERT_EQ(282U, info->w) << "Frame " << pts << " had unexpected width";
256       ASSERT_EQ(173U, info->h) << "Frame " << pts << " had unexpected height";
257     } else {
258       EXPECT_EQ(352U, info->w) << "Frame " << pts << " had unexpected width";
259       EXPECT_EQ(288U, info->h) << "Frame " << pts << " had unexpected height";
260     }
261   }
262 }
263
264 class ResizeInternalRealtimeTest : public ::libvpx_test::EncoderTest,
265   public ::libvpx_test::CodecTestWith2Params<libvpx_test::TestMode, int> {
266  protected:
267   ResizeInternalRealtimeTest() : EncoderTest(GET_PARAM(0)) {}
268   virtual ~ResizeInternalRealtimeTest() {}
269
270   virtual void PreEncodeFrameHook(libvpx_test::VideoSource *video,
271                                   libvpx_test::Encoder *encoder) {
272     if (video->frame() == 0) {
273       encoder->Control(VP9E_SET_AQ_MODE, 3);
274       encoder->Control(VP8E_SET_CPUUSED, set_cpu_used_);
275     }
276
277     if (change_bitrate_ && video->frame() == 120) {
278       change_bitrate_ = false;
279       cfg_.rc_target_bitrate = 500;
280       encoder->Config(&cfg_);
281     }
282   }
283
284   virtual void SetUp() {
285     InitializeConfig();
286     SetMode(GET_PARAM(1));
287     set_cpu_used_ = GET_PARAM(2);
288   }
289
290   virtual void DecompressedFrameHook(const vpx_image_t &img,
291                                      vpx_codec_pts_t pts) {
292     frame_info_list_.push_back(FrameInfo(pts, img.d_w, img.d_h));
293   }
294
295   void DefaultConfig() {
296     cfg_.g_w = 352;
297     cfg_.g_h = 288;
298     cfg_.rc_buf_initial_sz = 500;
299     cfg_.rc_buf_optimal_sz = 600;
300     cfg_.rc_buf_sz = 1000;
301     cfg_.rc_min_quantizer = 2;
302     cfg_.rc_max_quantizer = 56;
303     cfg_.rc_undershoot_pct = 50;
304     cfg_.rc_overshoot_pct = 50;
305     cfg_.rc_end_usage = VPX_CBR;
306     cfg_.kf_mode = VPX_KF_AUTO;
307     cfg_.g_lag_in_frames = 0;
308     cfg_.kf_min_dist = cfg_.kf_max_dist = 3000;
309     // Enable dropped frames.
310     cfg_.rc_dropframe_thresh = 1;
311     // Enable error_resilience mode.
312     cfg_.g_error_resilient  = 1;
313     // Enable dynamic resizing.
314     cfg_.rc_resize_allowed = 1;
315     // Run at low bitrate.
316     cfg_.rc_target_bitrate = 200;
317   }
318
319   std::vector< FrameInfo > frame_info_list_;
320   int set_cpu_used_;
321   bool change_bitrate_;
322 };
323
324 // Verify the dynamic resizer behavior for real time, 1 pass CBR mode.
325 // Run at low bitrate, with resize_allowed = 1, and verify that we get
326 // one resize down event.
327 TEST_P(ResizeInternalRealtimeTest, TestInternalResizeDown) {
328   ::libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
329                                        30, 1, 0, 299);
330   DefaultConfig();
331   change_bitrate_ = false;
332   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
333
334   unsigned int last_w = cfg_.g_w;
335   unsigned int last_h = cfg_.g_h;
336   int resize_count = 0;
337   for (std::vector<FrameInfo>::const_iterator info = frame_info_list_.begin();
338        info != frame_info_list_.end(); ++info) {
339     if (info->w != last_w || info->h != last_h) {
340       // Verify that resize down occurs.
341       ASSERT_LT(info->w, last_w);
342       ASSERT_LT(info->h, last_h);
343       last_w = info->w;
344       last_h = info->h;
345       resize_count++;
346     }
347   }
348
349   // Verify that we get 1 resize down event in this test.
350   ASSERT_EQ(1, resize_count) << "Resizing should occur.";
351 }
352
353 // Verify the dynamic resizer behavior for real time, 1 pass CBR mode.
354 // Start at low target bitrate, raise the bitrate in the middle of the clip,
355 // scaling-up should occur after bitrate changed.
356 TEST_P(ResizeInternalRealtimeTest, TestInternalResizeDownUpChangeBitRate) {
357   ::libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
358                                        30, 1, 0, 299);
359   DefaultConfig();
360   change_bitrate_ = true;
361   // Disable dropped frames.
362   cfg_.rc_dropframe_thresh = 0;
363   // Starting bitrate low.
364   cfg_.rc_target_bitrate = 100;
365   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
366
367   unsigned int last_w = cfg_.g_w;
368   unsigned int last_h = cfg_.g_h;
369   int resize_count = 0;
370   for (std::vector<FrameInfo>::const_iterator info = frame_info_list_.begin();
371        info != frame_info_list_.end(); ++info) {
372     if (info->w != last_w || info->h != last_h) {
373       resize_count++;
374       if (resize_count == 1) {
375         // Verify that resize down occurs.
376         ASSERT_LT(info->w, last_w);
377         ASSERT_LT(info->h, last_h);
378       } else if (resize_count == 2) {
379         // Verify that resize up occurs.
380         ASSERT_GT(info->w, last_w);
381         ASSERT_GT(info->h, last_h);
382       }
383       last_w = info->w;
384       last_h = info->h;
385     }
386   }
387
388   // Verify that we get 2 resize events in this test.
389   ASSERT_EQ(2, resize_count) << "Resizing should occur twice.";
390 }
391
392 vpx_img_fmt_t CspForFrameNumber(int frame) {
393   if (frame < 10)
394     return VPX_IMG_FMT_I420;
395   if (frame < 20)
396     return VPX_IMG_FMT_I444;
397   return VPX_IMG_FMT_I420;
398 }
399
400 class ResizeCspTest : public ResizeTest {
401  protected:
402 #if WRITE_COMPRESSED_STREAM
403   ResizeCspTest()
404       : ResizeTest(),
405         frame0_psnr_(0.0),
406         outfile_(NULL),
407         out_frames_(0) {}
408 #else
409   ResizeCspTest() : ResizeTest(), frame0_psnr_(0.0) {}
410 #endif
411
412   virtual ~ResizeCspTest() {}
413
414   virtual void BeginPassHook(unsigned int /*pass*/) {
415 #if WRITE_COMPRESSED_STREAM
416     outfile_ = fopen("vp91-2-05-cspchape.ivf", "wb");
417 #endif
418   }
419
420   virtual void EndPassHook() {
421 #if WRITE_COMPRESSED_STREAM
422     if (outfile_) {
423       if (!fseek(outfile_, 0, SEEK_SET))
424         write_ivf_file_header(&cfg_, out_frames_, outfile_);
425       fclose(outfile_);
426       outfile_ = NULL;
427     }
428 #endif
429   }
430
431   virtual void PreEncodeFrameHook(libvpx_test::VideoSource *video,
432                                   libvpx_test::Encoder *encoder) {
433     if (CspForFrameNumber(video->frame()) != VPX_IMG_FMT_I420 &&
434         cfg_.g_profile != 1) {
435       cfg_.g_profile = 1;
436       encoder->Config(&cfg_);
437     }
438     if (CspForFrameNumber(video->frame()) == VPX_IMG_FMT_I420 &&
439         cfg_.g_profile != 0) {
440       cfg_.g_profile = 0;
441       encoder->Config(&cfg_);
442     }
443   }
444
445   virtual void PSNRPktHook(const vpx_codec_cx_pkt_t *pkt) {
446     if (!frame0_psnr_)
447       frame0_psnr_ = pkt->data.psnr.psnr[0];
448     EXPECT_NEAR(pkt->data.psnr.psnr[0], frame0_psnr_, 2.0);
449   }
450
451 #if WRITE_COMPRESSED_STREAM
452   virtual void FramePktHook(const vpx_codec_cx_pkt_t *pkt) {
453     ++out_frames_;
454
455     // Write initial file header if first frame.
456     if (pkt->data.frame.pts == 0)
457       write_ivf_file_header(&cfg_, 0, outfile_);
458
459     // Write frame header and data.
460     write_ivf_frame_header(pkt, outfile_);
461     (void)fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz, outfile_);
462   }
463 #endif
464
465   double frame0_psnr_;
466 #if WRITE_COMPRESSED_STREAM
467   FILE *outfile_;
468   unsigned int out_frames_;
469 #endif
470 };
471
472 class ResizingCspVideoSource : public ::libvpx_test::DummyVideoSource {
473  public:
474   ResizingCspVideoSource() {
475     SetSize(kInitialWidth, kInitialHeight);
476     limit_ = 30;
477   }
478
479   virtual ~ResizingCspVideoSource() {}
480
481  protected:
482   virtual void Next() {
483     ++frame_;
484     SetImageFormat(CspForFrameNumber(frame_));
485     FillFrame();
486   }
487 };
488
489 TEST_P(ResizeCspTest, TestResizeCspWorks) {
490   ResizingCspVideoSource video;
491   init_flags_ = VPX_CODEC_USE_PSNR;
492   cfg_.rc_min_quantizer = cfg_.rc_max_quantizer = 48;
493   cfg_.g_lag_in_frames = 0;
494   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
495 }
496
497 VP8_INSTANTIATE_TEST_CASE(ResizeTest, ONE_PASS_TEST_MODES);
498 VP9_INSTANTIATE_TEST_CASE(ResizeTest,
499                           ::testing::Values(::libvpx_test::kRealTime));
500 VP9_INSTANTIATE_TEST_CASE(ResizeInternalTest,
501                           ::testing::Values(::libvpx_test::kOnePassBest));
502 VP9_INSTANTIATE_TEST_CASE(ResizeInternalRealtimeTest,
503                           ::testing::Values(::libvpx_test::kRealTime),
504                           ::testing::Range(5, 9));
505 VP9_INSTANTIATE_TEST_CASE(ResizeCspTest,
506                           ::testing::Values(::libvpx_test::kRealTime));
507 }  // namespace