2 * Copyright (c) 2012 The WebM project authors. All Rights Reserved.
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.
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"
19 // Enable(1) or Disable(0) writing of the compressed bitstream.
20 #define WRITE_COMPRESSED_STREAM 0
24 #if WRITE_COMPRESSED_STREAM
25 static void mem_put_le16(char *const mem, const unsigned int val) {
30 static void mem_put_le32(char *const mem, const unsigned int val) {
37 static void write_ivf_file_header(const vpx_codec_enc_cfg_t *const cfg,
38 int frame_cnt, FILE *const outfile) {
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 */
55 (void)fwrite(header, 1, 32, outfile);
58 static void write_ivf_frame_size(FILE *const outfile, const size_t size) {
60 mem_put_le32(header, static_cast<unsigned int>(size));
61 (void)fwrite(header, 1, 4, outfile);
64 static void write_ivf_frame_header(const vpx_codec_cx_pkt_t *const pkt,
65 FILE *const outfile) {
69 if (pkt->kind != VPX_CODEC_CX_FRAME_PKT)
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);
77 (void)fwrite(header, 1, 12, outfile);
79 #endif // WRITE_COMPRESSED_STREAM
81 const unsigned int kInitialWidth = 320;
82 const unsigned int kInitialHeight = 240;
84 unsigned int ScaleForFrameNumber(unsigned int frame, unsigned int val) {
98 class ResizingVideoSource : public ::libvpx_test::DummyVideoSource {
100 ResizingVideoSource() {
101 SetSize(kInitialWidth, kInitialHeight);
105 virtual ~ResizingVideoSource() {}
108 virtual void Next() {
110 SetSize(ScaleForFrameNumber(frame_, kInitialWidth),
111 ScaleForFrameNumber(frame_, kInitialHeight));
116 class ResizeTest : public ::libvpx_test::EncoderTest,
117 public ::libvpx_test::CodecTestWithParam<libvpx_test::TestMode> {
119 ResizeTest() : EncoderTest(GET_PARAM(0)) {}
121 virtual ~ResizeTest() {}
124 FrameInfo(vpx_codec_pts_t _pts, unsigned int _w, unsigned int _h)
125 : pts(_pts), w(_w), h(_h) {}
132 virtual void SetUp() {
134 SetMode(GET_PARAM(1));
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));
142 std::vector< FrameInfo > frame_info_list_;
145 TEST_P(ResizeTest, TestExternalResizeWorks) {
146 ResizingVideoSource video;
147 ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
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);
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";
162 const unsigned int kStepDownFrame = 3;
163 const unsigned int kStepUpFrame = 6;
165 class ResizeInternalTest : public ResizeTest {
167 #if WRITE_COMPRESSED_STREAM
174 ResizeInternalTest() : ResizeTest(), frame0_psnr_(0.0) {}
177 virtual ~ResizeInternalTest() {}
179 virtual void BeginPassHook(unsigned int /*pass*/) {
180 #if WRITE_COMPRESSED_STREAM
181 outfile_ = fopen("vp90-2-05-resize.ivf", "wb");
185 virtual void EndPassHook() {
186 #if WRITE_COMPRESSED_STREAM
188 if (!fseek(outfile_, 0, SEEK_SET))
189 write_ivf_file_header(&cfg_, out_frames_, outfile_);
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);
202 if (video->frame() == kStepUpFrame) {
203 struct vpx_scaling_mode mode = {VP8E_NORMAL, VP8E_NORMAL};
204 encoder->Control(VP8E_SET_SCALEMODE, &mode);
208 virtual void PSNRPktHook(const vpx_codec_cx_pkt_t *pkt) {
210 frame0_psnr_ = pkt->data.psnr.psnr[0];
211 EXPECT_NEAR(pkt->data.psnr.psnr[0], frame0_psnr_, 2.0);
214 virtual void FramePktHook(const vpx_codec_cx_pkt_t *pkt) {
215 #if WRITE_COMPRESSED_STREAM
218 // Write initial file header if first frame.
219 if (pkt->data.frame.pts == 0)
220 write_ivf_file_header(&cfg_, 0, outfile_);
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_);
229 #if WRITE_COMPRESSED_STREAM
231 unsigned int out_frames_;
235 TEST_P(ResizeInternalTest, TestInternalResizeWorks) {
236 ::libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
238 init_flags_ = VPX_CODEC_USE_PSNR;
240 // q picked such that initial keyframe on this clip is ~30dB PSNR
241 cfg_.rc_min_quantizer = cfg_.rc_max_quantizer = 48;
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));
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";
257 EXPECT_EQ(352U, info->w) << "Frame " << pts << " had unexpected width";
258 EXPECT_EQ(288U, info->h) << "Frame " << pts << " had unexpected height";
263 VP8_INSTANTIATE_TEST_CASE(ResizeTest, ONE_PASS_TEST_MODES);
264 VP9_INSTANTIATE_TEST_CASE(ResizeInternalTest,
265 ::testing::Values(::libvpx_test::kOnePassBest));