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.
14 #include "third_party/googletest/src/include/gtest/gtest.h"
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"
23 namespace libvpx_test {
24 void Encoder::InitEncoder(VideoSource *video) {
26 const vpx_image_t *img = video->img();
28 if (video->img() && !encoder_.priv) {
31 cfg_.g_timebase = video->timebase();
32 cfg_.rc_twopass_stats_in = stats_->buf();
34 res = vpx_codec_enc_init(&encoder_, CodecInterface(), &cfg_, init_flags_);
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);
62 // Handle twopass stats
63 CxDataIterator iter = GetCxData();
65 while (const vpx_codec_cx_pkt_t *pkt = iter.Next()) {
66 if (pkt->kind != VPX_CODEC_STATS_PKT) continue;
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(res = vpx_codec_encode(&encoder_, img, video.pts(),
87 video.duration(), frame_flags,
89 ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
92 void Encoder::Flush() {
93 const vpx_codec_err_t res =
94 vpx_codec_encode(&encoder_, nullptr, 0, 0, 0, deadline_);
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) {
109 case kRealTime: deadline_ = VPX_DL_REALTIME; break;
112 case kTwoPassGood: deadline_ = VPX_DL_GOOD_QUALITY; break;
115 case kTwoPassBest: deadline_ = VPX_DL_BEST_QUALITY; break;
117 default: ASSERT_TRUE(false) << "Unexpected mode " << mode;
120 if (mode == kTwoPassGood || mode == kTwoPassBest) {
126 // The function should return "true" most of the time, therefore no early
127 // break-out is implemented within the match checking process.
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);
132 if (!match) return false;
134 const unsigned int width_y = img1->d_w;
135 const unsigned int height_y = img1->d_h;
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],
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],
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],
160 void EncoderTest::MismatchHook(const vpx_image_t * /*img1*/,
161 const vpx_image_t * /*img2*/) {
162 ASSERT_TRUE(0) << "Encode/Decode mismatch found";
165 void EncoderTest::RunLoop(VideoSource *video) {
166 vpx_codec_dec_cfg_t dec_cfg = vpx_codec_dec_cfg_t();
170 ASSERT_TRUE(passes_ == 1 || passes_ == 2);
171 for (unsigned int pass = 0; pass < passes_; pass++) {
175 cfg_.g_pass = VPX_RC_ONE_PASS;
176 } else if (pass == 0) {
177 cfg_.g_pass = VPX_RC_FIRST_PASS;
179 cfg_.g_pass = VPX_RC_LAST_PASS;
183 std::unique_ptr<Encoder> encoder(
184 codec_->CreateEncoder(cfg_, deadline_, init_flags_, &stats_));
185 ASSERT_NE(encoder.get(), nullptr);
187 ASSERT_NO_FATAL_FAILURE(video->Begin());
188 encoder->InitEncoder(video);
189 ASSERT_FALSE(::testing::Test::HasFatalFailure());
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;
197 std::unique_ptr<Decoder> decoder(
198 codec_->CreateDecoder(dec_cfg, dec_init_flags));
200 for (again = true; again; video->Next()) {
201 again = (video->img() != nullptr);
203 PreEncodeFrameHook(video);
204 PreEncodeFrameHook(video, encoder.get());
205 encoder->EncodeFrame(video, frame_flags_);
207 PostEncodeFrameHook(encoder.get());
209 CxDataIterator iter = encoder->GetCxData();
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);
217 case VPX_CODEC_CX_FRAME_PKT:
219 if (decoder != nullptr && 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);
224 if (!HandleDecodeResult(res_dec, *video, decoder.get())) break;
228 ASSERT_GE(pkt->data.frame.pts, last_pts_);
229 last_pts_ = pkt->data.frame.pts;
233 case VPX_CODEC_PSNR_PKT: PSNRPktHook(pkt); break;
235 case VPX_CODEC_STATS_PKT: StatsPktHook(pkt); break;
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(nullptr, 0);
244 if (!HandleDecodeResult(res_dec, *video, decoder.get())) break;
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);
257 if (img_dec) DecompressedFrameHook(*img_dec, video->pts());
259 if (!Continue()) break;
264 if (!Continue()) break;
268 } // namespace libvpx_test