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.
11 #include "./vpx_config.h"
12 #include "test/codec_factory.h"
13 #include "test/encode_test_driver.h"
14 #include "test/decode_test_driver.h"
15 #include "test/register_state_check.h"
16 #include "test/video_source.h"
17 #include "third_party/googletest/src/include/gtest/gtest.h"
19 namespace libvpx_test {
20 void Encoder::InitEncoder(VideoSource *video) {
22 const vpx_image_t *img = video->img();
24 if (video->img() && !encoder_.priv) {
27 cfg_.g_timebase = video->timebase();
28 cfg_.rc_twopass_stats_in = stats_->buf();
29 res = vpx_codec_enc_init(&encoder_, CodecInterface(), &cfg_,
31 ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
35 void Encoder::EncodeFrame(VideoSource *video, const unsigned long frame_flags) {
37 EncodeFrameInternal(*video, frame_flags);
41 // Handle twopass stats
42 CxDataIterator iter = GetCxData();
44 while (const vpx_codec_cx_pkt_t *pkt = iter.Next()) {
45 if (pkt->kind != VPX_CODEC_STATS_PKT)
52 void Encoder::EncodeFrameInternal(const VideoSource &video,
53 const unsigned long frame_flags) {
55 const vpx_image_t *img = video.img();
57 // Handle frame resizing
58 if (cfg_.g_w != img->d_w || cfg_.g_h != img->d_h) {
61 res = vpx_codec_enc_config_set(&encoder_, &cfg_);
62 ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
66 API_REGISTER_STATE_CHECK(
67 res = vpx_codec_encode(&encoder_, img, video.pts(), video.duration(),
68 frame_flags, deadline_));
69 ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
72 void Encoder::Flush() {
73 const vpx_codec_err_t res = vpx_codec_encode(&encoder_, NULL, 0, 0, 0,
76 ASSERT_EQ(VPX_CODEC_ERROR, res) << EncoderError();
78 ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
81 void EncoderTest::InitializeConfig() {
82 const vpx_codec_err_t res = codec_->DefaultEncoderConfig(&cfg_, 0);
83 dec_cfg_ = vpx_codec_dec_cfg_t();
84 ASSERT_EQ(VPX_CODEC_OK, res);
87 void EncoderTest::SetMode(TestMode mode) {
90 deadline_ = VPX_DL_REALTIME;
95 deadline_ = VPX_DL_GOOD_QUALITY;
100 deadline_ = VPX_DL_BEST_QUALITY;
104 ASSERT_TRUE(false) << "Unexpected mode " << mode;
107 if (mode == kTwoPassGood || mode == kTwoPassBest)
112 // The function should return "true" most of the time, therefore no early
113 // break-out is implemented within the match checking process.
114 static bool compare_img(const vpx_image_t *img1,
115 const vpx_image_t *img2) {
116 bool match = (img1->fmt == img2->fmt) &&
117 (img1->cs == img2->cs) &&
118 (img1->d_w == img2->d_w) &&
119 (img1->d_h == img2->d_h);
121 const unsigned int width_y = img1->d_w;
122 const unsigned int height_y = img1->d_h;
124 for (i = 0; i < height_y; ++i)
125 match = (memcmp(img1->planes[VPX_PLANE_Y] + i * img1->stride[VPX_PLANE_Y],
126 img2->planes[VPX_PLANE_Y] + i * img2->stride[VPX_PLANE_Y],
127 width_y) == 0) && match;
128 const unsigned int width_uv = (img1->d_w + 1) >> 1;
129 const unsigned int height_uv = (img1->d_h + 1) >> 1;
130 for (i = 0; i < height_uv; ++i)
131 match = (memcmp(img1->planes[VPX_PLANE_U] + i * img1->stride[VPX_PLANE_U],
132 img2->planes[VPX_PLANE_U] + i * img2->stride[VPX_PLANE_U],
133 width_uv) == 0) && match;
134 for (i = 0; i < height_uv; ++i)
135 match = (memcmp(img1->planes[VPX_PLANE_V] + i * img1->stride[VPX_PLANE_V],
136 img2->planes[VPX_PLANE_V] + i * img2->stride[VPX_PLANE_V],
137 width_uv) == 0) && match;
141 void EncoderTest::MismatchHook(const vpx_image_t* /*img1*/,
142 const vpx_image_t* /*img2*/) {
143 ASSERT_TRUE(0) << "Encode/Decode mismatch found";
146 void EncoderTest::RunLoop(VideoSource *video) {
147 vpx_codec_dec_cfg_t dec_cfg = vpx_codec_dec_cfg_t();
151 ASSERT_TRUE(passes_ == 1 || passes_ == 2);
152 for (unsigned int pass = 0; pass < passes_; pass++) {
156 cfg_.g_pass = VPX_RC_ONE_PASS;
158 cfg_.g_pass = VPX_RC_FIRST_PASS;
160 cfg_.g_pass = VPX_RC_LAST_PASS;
163 Encoder* const encoder = codec_->CreateEncoder(cfg_, deadline_, init_flags_,
165 ASSERT_TRUE(encoder != NULL);
168 encoder->InitEncoder(video);
170 unsigned long dec_init_flags = 0; // NOLINT
171 // Use fragment decoder if encoder outputs partitions.
172 // NOTE: fragment decoder and partition encoder are only supported by VP8.
173 if (init_flags_ & VPX_CODEC_USE_OUTPUT_PARTITION)
174 dec_init_flags |= VPX_CODEC_USE_INPUT_FRAGMENTS;
175 Decoder* const decoder = codec_->CreateDecoder(dec_cfg, dec_init_flags, 0);
177 for (again = true; again; video->Next()) {
178 again = (video->img() != NULL);
180 PreEncodeFrameHook(video);
181 PreEncodeFrameHook(video, encoder);
182 encoder->EncodeFrame(video, frame_flags_);
184 CxDataIterator iter = encoder->GetCxData();
186 bool has_cxdata = false;
187 bool has_dxdata = false;
188 while (const vpx_codec_cx_pkt_t *pkt = iter.Next()) {
189 pkt = MutateEncoderOutputHook(pkt);
192 case VPX_CODEC_CX_FRAME_PKT:
194 if (decoder && DoDecode()) {
195 vpx_codec_err_t res_dec = decoder->DecodeFrame(
196 (const uint8_t*)pkt->data.frame.buf, pkt->data.frame.sz);
198 if (!HandleDecodeResult(res_dec, *video, decoder))
203 ASSERT_GE(pkt->data.frame.pts, last_pts_);
204 last_pts_ = pkt->data.frame.pts;
208 case VPX_CODEC_PSNR_PKT:
217 // Flush the decoder when there are no more fragments.
218 if ((init_flags_ & VPX_CODEC_USE_OUTPUT_PARTITION) && has_dxdata) {
219 const vpx_codec_err_t res_dec = decoder->DecodeFrame(NULL, 0);
220 if (!HandleDecodeResult(res_dec, *video, decoder))
224 if (has_dxdata && has_cxdata) {
225 const vpx_image_t *img_enc = encoder->GetPreviewFrame();
226 DxDataIterator dec_iter = decoder->GetDxData();
227 const vpx_image_t *img_dec = dec_iter.Next();
228 if (img_enc && img_dec) {
229 const bool res = compare_img(img_enc, img_dec);
230 if (!res) { // Mismatch
231 MismatchHook(img_enc, img_dec);
235 DecompressedFrameHook(*img_dec, video->pts());
252 } // namespace libvpx_test