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.
13 #include "third_party/googletest/src/include/gtest/gtest.h"
15 #include "./vpx_config.h"
16 #include "test/codec_factory.h"
17 #include "test/decode_test_driver.h"
18 #include "test/encode_test_driver.h"
19 #include "test/register_state_check.h"
20 #include "test/video_source.h"
22 namespace libvpx_test {
23 void Encoder::InitEncoder(VideoSource *video) {
25 const vpx_image_t *img = video->img();
27 if (video->img() && !encoder_.priv) {
30 cfg_.g_timebase = video->timebase();
31 cfg_.rc_twopass_stats_in = stats_->buf();
33 res = vpx_codec_enc_init(&encoder_, CodecInterface(), &cfg_,
35 ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
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,
43 ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
47 #if CONFIG_VP8_ENCODER
48 ASSERT_EQ(&vpx_codec_vp8_cx_algo, CodecInterface())
49 << "Unknown Codec Interface";
55 void Encoder::EncodeFrame(VideoSource *video, const unsigned long frame_flags) {
57 EncodeFrameInternal(*video, frame_flags);
61 // Handle twopass stats
62 CxDataIterator iter = GetCxData();
64 while (const vpx_codec_cx_pkt_t *pkt = iter.Next()) {
65 if (pkt->kind != VPX_CODEC_STATS_PKT)
72 void Encoder::EncodeFrameInternal(const VideoSource &video,
73 const unsigned long frame_flags) {
75 const vpx_image_t *img = video.img();
77 // Handle frame resizing
78 if (cfg_.g_w != img->d_w || cfg_.g_h != img->d_h) {
81 res = vpx_codec_enc_config_set(&encoder_, &cfg_);
82 ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
86 API_REGISTER_STATE_CHECK(
87 res = vpx_codec_encode(&encoder_, img, video.pts(), video.duration(),
88 frame_flags, deadline_));
89 ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
92 void Encoder::Flush() {
93 const vpx_codec_err_t res = vpx_codec_encode(&encoder_, NULL, 0, 0, 0,
96 ASSERT_EQ(VPX_CODEC_ERROR, res) << EncoderError();
98 ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
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);
107 void EncoderTest::SetMode(TestMode mode) {
110 deadline_ = VPX_DL_REALTIME;
115 deadline_ = VPX_DL_GOOD_QUALITY;
120 deadline_ = VPX_DL_BEST_QUALITY;
124 ASSERT_TRUE(false) << "Unexpected mode " << mode;
127 if (mode == kTwoPassGood || mode == kTwoPassBest)
132 // The function should return "true" most of the time, therefore no early
133 // break-out is implemented within the match checking process.
134 static bool compare_img(const vpx_image_t *img1,
135 const vpx_image_t *img2) {
136 bool match = (img1->fmt == img2->fmt) &&
137 (img1->cs == img2->cs) &&
138 (img1->d_w == img2->d_w) &&
139 (img1->d_h == img2->d_h);
141 const unsigned int width_y = img1->d_w;
142 const unsigned int height_y = img1->d_h;
144 for (i = 0; i < height_y; ++i)
145 match = (memcmp(img1->planes[VPX_PLANE_Y] + i * img1->stride[VPX_PLANE_Y],
146 img2->planes[VPX_PLANE_Y] + i * img2->stride[VPX_PLANE_Y],
147 width_y) == 0) && match;
148 const unsigned int width_uv = (img1->d_w + 1) >> 1;
149 const unsigned int height_uv = (img1->d_h + 1) >> 1;
150 for (i = 0; i < height_uv; ++i)
151 match = (memcmp(img1->planes[VPX_PLANE_U] + i * img1->stride[VPX_PLANE_U],
152 img2->planes[VPX_PLANE_U] + i * img2->stride[VPX_PLANE_U],
153 width_uv) == 0) && match;
154 for (i = 0; i < height_uv; ++i)
155 match = (memcmp(img1->planes[VPX_PLANE_V] + i * img1->stride[VPX_PLANE_V],
156 img2->planes[VPX_PLANE_V] + i * img2->stride[VPX_PLANE_V],
157 width_uv) == 0) && match;
161 void EncoderTest::MismatchHook(const vpx_image_t* /*img1*/,
162 const vpx_image_t* /*img2*/) {
163 ASSERT_TRUE(0) << "Encode/Decode mismatch found";
166 void EncoderTest::RunLoop(VideoSource *video) {
167 vpx_codec_dec_cfg_t dec_cfg = vpx_codec_dec_cfg_t();
171 ASSERT_TRUE(passes_ == 1 || passes_ == 2);
172 for (unsigned int pass = 0; pass < passes_; pass++) {
176 cfg_.g_pass = VPX_RC_ONE_PASS;
178 cfg_.g_pass = VPX_RC_FIRST_PASS;
180 cfg_.g_pass = VPX_RC_LAST_PASS;
183 Encoder* const encoder = codec_->CreateEncoder(cfg_, deadline_, init_flags_,
185 ASSERT_TRUE(encoder != NULL);
188 encoder->InitEncoder(video);
190 unsigned long dec_init_flags = 0; // NOLINT
191 // Use fragment decoder if encoder outputs partitions.
192 // NOTE: fragment decoder and partition encoder are only supported by VP8.
193 if (init_flags_ & VPX_CODEC_USE_OUTPUT_PARTITION)
194 dec_init_flags |= VPX_CODEC_USE_INPUT_FRAGMENTS;
195 Decoder* const decoder = codec_->CreateDecoder(dec_cfg, dec_init_flags, 0);
197 for (again = true; again; video->Next()) {
198 again = (video->img() != NULL);
200 PreEncodeFrameHook(video);
201 PreEncodeFrameHook(video, encoder);
202 encoder->EncodeFrame(video, frame_flags_);
204 CxDataIterator iter = encoder->GetCxData();
206 bool has_cxdata = false;
207 bool has_dxdata = false;
208 while (const vpx_codec_cx_pkt_t *pkt = iter.Next()) {
209 pkt = MutateEncoderOutputHook(pkt);
212 case VPX_CODEC_CX_FRAME_PKT:
214 if (decoder && DoDecode()) {
215 vpx_codec_err_t res_dec = decoder->DecodeFrame(
216 (const uint8_t*)pkt->data.frame.buf, pkt->data.frame.sz);
218 if (!HandleDecodeResult(res_dec, *video, decoder))
223 ASSERT_GE(pkt->data.frame.pts, last_pts_);
224 last_pts_ = pkt->data.frame.pts;
228 case VPX_CODEC_PSNR_PKT:
237 // Flush the decoder when there are no more fragments.
238 if ((init_flags_ & VPX_CODEC_USE_OUTPUT_PARTITION) && has_dxdata) {
239 const vpx_codec_err_t res_dec = decoder->DecodeFrame(NULL, 0);
240 if (!HandleDecodeResult(res_dec, *video, decoder))
244 if (has_dxdata && has_cxdata) {
245 const vpx_image_t *img_enc = encoder->GetPreviewFrame();
246 DxDataIterator dec_iter = decoder->GetDxData();
247 const vpx_image_t *img_dec = dec_iter.Next();
248 if (img_enc && img_dec) {
249 const bool res = compare_img(img_enc, img_dec);
250 if (!res) { // Mismatch
251 MismatchHook(img_enc, img_dec);
255 DecompressedFrameHook(*img_dec, video->pts());
272 } // namespace libvpx_test