Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / libvpx / source / 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 unsigned int ScaleForFrameNumber(unsigned int frame, unsigned int val) {
85   if (frame < 10)
86     return val;
87   if (frame < 20)
88     return val / 2;
89   if (frame < 30)
90     return val * 2 / 3;
91   if (frame < 40)
92     return val / 4;
93   if (frame < 50)
94     return val * 7 / 8;
95   return val;
96 }
97
98 class ResizingVideoSource : public ::libvpx_test::DummyVideoSource {
99  public:
100   ResizingVideoSource() {
101     SetSize(kInitialWidth, kInitialHeight);
102     limit_ = 60;
103   }
104
105   virtual ~ResizingVideoSource() {}
106
107  protected:
108   virtual void Next() {
109     ++frame_;
110     SetSize(ScaleForFrameNumber(frame_, kInitialWidth),
111             ScaleForFrameNumber(frame_, kInitialHeight));
112     FillFrame();
113   }
114 };
115
116 class ResizeTest : public ::libvpx_test::EncoderTest,
117   public ::libvpx_test::CodecTestWithParam<libvpx_test::TestMode> {
118  protected:
119   ResizeTest() : EncoderTest(GET_PARAM(0)) {}
120
121   virtual ~ResizeTest() {}
122
123   struct FrameInfo {
124     FrameInfo(vpx_codec_pts_t _pts, unsigned int _w, unsigned int _h)
125         : pts(_pts), w(_w), h(_h) {}
126
127     vpx_codec_pts_t pts;
128     unsigned int w;
129     unsigned int h;
130   };
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   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
148
149   for (std::vector<FrameInfo>::const_iterator info = frame_info_list_.begin();
150        info != frame_info_list_.end(); ++info) {
151     const unsigned int frame = static_cast<unsigned>(info->pts);
152     const unsigned int expected_w = ScaleForFrameNumber(frame, kInitialWidth);
153     const unsigned int expected_h = ScaleForFrameNumber(frame, kInitialHeight);
154
155     EXPECT_EQ(expected_w, info->w)
156         << "Frame " << frame << "had unexpected width";
157     EXPECT_EQ(expected_h, info->h)
158         << "Frame " << frame << "had unexpected height";
159   }
160 }
161
162 const unsigned int kStepDownFrame = 3;
163 const unsigned int kStepUpFrame = 6;
164
165 class ResizeInternalTest : public ResizeTest {
166  protected:
167 #if WRITE_COMPRESSED_STREAM
168   ResizeInternalTest()
169       : ResizeTest(),
170         frame0_psnr_(0.0),
171         outfile_(NULL),
172         out_frames_(0) {}
173 #else
174   ResizeInternalTest() : ResizeTest(), frame0_psnr_(0.0) {}
175 #endif
176
177   virtual ~ResizeInternalTest() {}
178
179   virtual void BeginPassHook(unsigned int /*pass*/) {
180 #if WRITE_COMPRESSED_STREAM
181     outfile_ = fopen("vp90-2-05-resize.ivf", "wb");
182 #endif
183   }
184
185   virtual void EndPassHook() {
186 #if WRITE_COMPRESSED_STREAM
187     if (outfile_) {
188       if (!fseek(outfile_, 0, SEEK_SET))
189         write_ivf_file_header(&cfg_, out_frames_, outfile_);
190       fclose(outfile_);
191       outfile_ = NULL;
192     }
193 #endif
194   }
195
196   virtual void PreEncodeFrameHook(libvpx_test::VideoSource *video,
197                                   libvpx_test::Encoder *encoder) {
198     if (video->frame() == kStepDownFrame) {
199       struct vpx_scaling_mode mode = {VP8E_FOURFIVE, VP8E_THREEFIVE};
200       encoder->Control(VP8E_SET_SCALEMODE, &mode);
201     }
202     if (video->frame() == kStepUpFrame) {
203       struct vpx_scaling_mode mode = {VP8E_NORMAL, VP8E_NORMAL};
204       encoder->Control(VP8E_SET_SCALEMODE, &mode);
205     }
206   }
207
208   virtual void PSNRPktHook(const vpx_codec_cx_pkt_t *pkt) {
209     if (!frame0_psnr_)
210       frame0_psnr_ = pkt->data.psnr.psnr[0];
211     EXPECT_NEAR(pkt->data.psnr.psnr[0], frame0_psnr_, 2.0);
212   }
213
214   virtual void FramePktHook(const vpx_codec_cx_pkt_t *pkt) {
215 #if WRITE_COMPRESSED_STREAM
216     ++out_frames_;
217
218     // Write initial file header if first frame.
219     if (pkt->data.frame.pts == 0)
220       write_ivf_file_header(&cfg_, 0, outfile_);
221
222     // Write frame header and data.
223     write_ivf_frame_header(pkt, outfile_);
224     (void)fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz, outfile_);
225 #endif
226   }
227
228   double frame0_psnr_;
229 #if WRITE_COMPRESSED_STREAM
230   FILE *outfile_;
231   unsigned int out_frames_;
232 #endif
233 };
234
235 TEST_P(ResizeInternalTest, TestInternalResizeWorks) {
236   ::libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
237                                        30, 1, 0, 10);
238   init_flags_ = VPX_CODEC_USE_PSNR;
239
240   // q picked such that initial keyframe on this clip is ~30dB PSNR
241   cfg_.rc_min_quantizer = cfg_.rc_max_quantizer = 48;
242
243   // If the number of frames being encoded is smaller than g_lag_in_frames
244   // the encoded frame is unavailable using the current API. Comparing
245   // frames to detect mismatch would then not be possible. Set
246   // g_lag_in_frames = 0 to get around this.
247   cfg_.g_lag_in_frames = 0;
248   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
249
250   for (std::vector<FrameInfo>::const_iterator info = frame_info_list_.begin();
251        info != frame_info_list_.end(); ++info) {
252     const vpx_codec_pts_t pts = info->pts;
253     if (pts >= kStepDownFrame && pts < kStepUpFrame) {
254       ASSERT_EQ(282U, info->w) << "Frame " << pts << " had unexpected width";
255       ASSERT_EQ(173U, info->h) << "Frame " << pts << " had unexpected height";
256     } else {
257       EXPECT_EQ(352U, info->w) << "Frame " << pts << " had unexpected width";
258       EXPECT_EQ(288U, info->h) << "Frame " << pts << " had unexpected height";
259     }
260   }
261 }
262
263 VP8_INSTANTIATE_TEST_CASE(ResizeTest, ONE_PASS_TEST_MODES);
264 VP9_INSTANTIATE_TEST_CASE(ResizeInternalTest,
265                           ::testing::Values(::libvpx_test::kOnePassBest));
266 }  // namespace