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
11 #include <memory>
12 #include <string>
13
14 #include "third_party/googletest/src/include/gtest/gtest.h"
15
16 #include "./vpx_config.h"
17 #include "test/codec_factory.h"
18 #include "test/decode_test_driver.h"
19 #include "test/encode_test_driver.h"
20 #include "test/register_state_check.h"
21 #include "test/video_source.h"
22
23 namespace libvpx_test {
InitEncoder(VideoSource * video)24 void Encoder::InitEncoder(VideoSource *video) {
25 vpx_codec_err_t res;
26 const vpx_image_t *img = video->img();
27
28 if (video->img() && !encoder_.priv) {
29 cfg_.g_w = img->d_w;
30 cfg_.g_h = img->d_h;
31 cfg_.g_timebase = video->timebase();
32 cfg_.rc_twopass_stats_in = stats_->buf();
33
34 res = vpx_codec_enc_init(&encoder_, CodecInterface(), &cfg_, init_flags_);
35 ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
36
37 #if CONFIG_VP9_ENCODER
38 if (CodecInterface() == &vpx_codec_vp9_cx_algo) {
39 // Default to 1 tile column for VP9.
40 const int log2_tile_columns = 0;
41 res = vpx_codec_control_(&encoder_, VP9E_SET_TILE_COLUMNS,
42 log2_tile_columns);
43 ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
44 } else
45 #endif
46 {
47 #if CONFIG_VP8_ENCODER
48 ASSERT_EQ(&vpx_codec_vp8_cx_algo, CodecInterface())
49 << "Unknown Codec Interface";
50 #endif
51 }
52 }
53 }
54
EncodeFrame(VideoSource * video,const unsigned long frame_flags)55 void Encoder::EncodeFrame(VideoSource *video, const unsigned long frame_flags) {
56 if (video->img()) {
57 EncodeFrameInternal(*video, frame_flags);
58 } else {
59 Flush();
60 }
61
62 // Handle twopass stats
63 CxDataIterator iter = GetCxData();
64
65 while (const vpx_codec_cx_pkt_t *pkt = iter.Next()) {
66 if (pkt->kind != VPX_CODEC_STATS_PKT) continue;
67
68 stats_->Append(*pkt);
69 }
70 }
71
EncodeFrameInternal(const VideoSource & video,const unsigned long frame_flags)72 void Encoder::EncodeFrameInternal(const VideoSource &video,
73 const unsigned long frame_flags) {
74 vpx_codec_err_t res;
75 const vpx_image_t *img = video.img();
76
77 // Handle frame resizing
78 if (cfg_.g_w != img->d_w || cfg_.g_h != img->d_h) {
79 cfg_.g_w = img->d_w;
80 cfg_.g_h = img->d_h;
81 res = vpx_codec_enc_config_set(&encoder_, &cfg_);
82 ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
83 }
84
85 // Encode the frame
86 API_REGISTER_STATE_CHECK(res = vpx_codec_encode(&encoder_, img, video.pts(),
87 video.duration(), frame_flags,
88 deadline_));
89 ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
90 }
91
Flush()92 void Encoder::Flush() {
93 const vpx_codec_err_t res =
94 vpx_codec_encode(&encoder_, NULL, 0, 0, 0, deadline_);
95 if (!encoder_.priv)
96 ASSERT_EQ(VPX_CODEC_ERROR, res) << EncoderError();
97 else
98 ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
99 }
100
InitializeConfig()101 void EncoderTest::InitializeConfig() {
102 const vpx_codec_err_t res = codec_->DefaultEncoderConfig(&cfg_, 0);
103 dec_cfg_ = vpx_codec_dec_cfg_t();
104 ASSERT_EQ(VPX_CODEC_OK, res);
105 }
106
SetMode(TestMode mode)107 void EncoderTest::SetMode(TestMode mode) {
108 switch (mode) {
109 case kRealTime: deadline_ = VPX_DL_REALTIME; break;
110
111 case kOnePassGood:
112 case kTwoPassGood: deadline_ = VPX_DL_GOOD_QUALITY; break;
113
114 case kOnePassBest:
115 case kTwoPassBest: deadline_ = VPX_DL_BEST_QUALITY; break;
116
117 default: ASSERT_TRUE(false) << "Unexpected mode " << mode;
118 }
119
120 if (mode == kTwoPassGood || mode == kTwoPassBest) {
121 passes_ = 2;
122 } else {
123 passes_ = 1;
124 }
125 }
126 // The function should return "true" most of the time, therefore no early
127 // break-out is implemented within the match checking process.
compare_img(const vpx_image_t * img1,const vpx_image_t * img2)128 static bool compare_img(const vpx_image_t *img1, const vpx_image_t *img2) {
129 bool match = (img1->fmt == img2->fmt) && (img1->cs == img2->cs) &&
130 (img1->d_w == img2->d_w) && (img1->d_h == img2->d_h);
131
132 if (!match) return false;
133
134 const unsigned int width_y = img1->d_w;
135 const unsigned int height_y = img1->d_h;
136 unsigned int i;
137 for (i = 0; i < height_y; ++i) {
138 match = (memcmp(img1->planes[VPX_PLANE_Y] + i * img1->stride[VPX_PLANE_Y],
139 img2->planes[VPX_PLANE_Y] + i * img2->stride[VPX_PLANE_Y],
140 width_y) == 0) &&
141 match;
142 }
143 const unsigned int width_uv = (img1->d_w + 1) >> 1;
144 const unsigned int height_uv = (img1->d_h + 1) >> 1;
145 for (i = 0; i < height_uv; ++i) {
146 match = (memcmp(img1->planes[VPX_PLANE_U] + i * img1->stride[VPX_PLANE_U],
147 img2->planes[VPX_PLANE_U] + i * img2->stride[VPX_PLANE_U],
148 width_uv) == 0) &&
149 match;
150 }
151 for (i = 0; i < height_uv; ++i) {
152 match = (memcmp(img1->planes[VPX_PLANE_V] + i * img1->stride[VPX_PLANE_V],
153 img2->planes[VPX_PLANE_V] + i * img2->stride[VPX_PLANE_V],
154 width_uv) == 0) &&
155 match;
156 }
157 return match;
158 }
159
MismatchHook(const vpx_image_t *,const vpx_image_t *)160 void EncoderTest::MismatchHook(const vpx_image_t * /*img1*/,
161 const vpx_image_t * /*img2*/) {
162 ASSERT_TRUE(0) << "Encode/Decode mismatch found";
163 }
164
RunLoop(VideoSource * video)165 void EncoderTest::RunLoop(VideoSource *video) {
166 vpx_codec_dec_cfg_t dec_cfg = vpx_codec_dec_cfg_t();
167
168 stats_.Reset();
169
170 ASSERT_TRUE(passes_ == 1 || passes_ == 2);
171 for (unsigned int pass = 0; pass < passes_; pass++) {
172 last_pts_ = 0;
173
174 if (passes_ == 1) {
175 cfg_.g_pass = VPX_RC_ONE_PASS;
176 } else if (pass == 0) {
177 cfg_.g_pass = VPX_RC_FIRST_PASS;
178 } else {
179 cfg_.g_pass = VPX_RC_LAST_PASS;
180 }
181
182 BeginPassHook(pass);
183 std::unique_ptr<Encoder> encoder(
184 codec_->CreateEncoder(cfg_, deadline_, init_flags_, &stats_));
185 ASSERT_TRUE(encoder.get() != NULL);
186
187 ASSERT_NO_FATAL_FAILURE(video->Begin());
188 encoder->InitEncoder(video);
189 ASSERT_FALSE(::testing::Test::HasFatalFailure());
190
191 unsigned long dec_init_flags = 0; // NOLINT
192 // Use fragment decoder if encoder outputs partitions.
193 // NOTE: fragment decoder and partition encoder are only supported by VP8.
194 if (init_flags_ & VPX_CODEC_USE_OUTPUT_PARTITION) {
195 dec_init_flags |= VPX_CODEC_USE_INPUT_FRAGMENTS;
196 }
197 std::unique_ptr<Decoder> decoder(
198 codec_->CreateDecoder(dec_cfg, dec_init_flags));
199 bool again;
200 for (again = true; again; video->Next()) {
201 again = (video->img() != NULL);
202
203 PreEncodeFrameHook(video);
204 PreEncodeFrameHook(video, encoder.get());
205 encoder->EncodeFrame(video, frame_flags_);
206
207 PostEncodeFrameHook(encoder.get());
208
209 CxDataIterator iter = encoder->GetCxData();
210
211 bool has_cxdata = false;
212 bool has_dxdata = false;
213 while (const vpx_codec_cx_pkt_t *pkt = iter.Next()) {
214 pkt = MutateEncoderOutputHook(pkt);
215 again = true;
216 switch (pkt->kind) {
217 case VPX_CODEC_CX_FRAME_PKT:
218 has_cxdata = true;
219 if (decoder.get() != NULL && DoDecode()) {
220 PreDecodeFrameHook(video, decoder.get());
221 vpx_codec_err_t res_dec = decoder->DecodeFrame(
222 (const uint8_t *)pkt->data.frame.buf, pkt->data.frame.sz);
223
224 if (!HandleDecodeResult(res_dec, *video, decoder.get())) break;
225
226 has_dxdata = true;
227 }
228 ASSERT_GE(pkt->data.frame.pts, last_pts_);
229 last_pts_ = pkt->data.frame.pts;
230 FramePktHook(pkt);
231 break;
232
233 case VPX_CODEC_PSNR_PKT: PSNRPktHook(pkt); break;
234
235 case VPX_CODEC_STATS_PKT: StatsPktHook(pkt); break;
236
237 default: break;
238 }
239 }
240
241 // Flush the decoder when there are no more fragments.
242 if ((init_flags_ & VPX_CODEC_USE_OUTPUT_PARTITION) && has_dxdata) {
243 const vpx_codec_err_t res_dec = decoder->DecodeFrame(NULL, 0);
244 if (!HandleDecodeResult(res_dec, *video, decoder.get())) break;
245 }
246
247 if (has_dxdata && has_cxdata) {
248 const vpx_image_t *img_enc = encoder->GetPreviewFrame();
249 DxDataIterator dec_iter = decoder->GetDxData();
250 const vpx_image_t *img_dec = dec_iter.Next();
251 if (img_enc && img_dec) {
252 const bool res = compare_img(img_enc, img_dec);
253 if (!res) { // Mismatch
254 MismatchHook(img_enc, img_dec);
255 }
256 }
257 if (img_dec) DecompressedFrameHook(*img_dec, video->pts());
258 }
259 if (!Continue()) break;
260 }
261
262 EndPassHook();
263
264 if (!Continue()) break;
265 }
266 }
267
268 } // namespace libvpx_test
269