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::EncodeFrame(VideoSource *video, const unsigned long frame_flags) {
22 EncodeFrameInternal(*video, frame_flags);
26 // Handle twopass stats
27 CxDataIterator iter = GetCxData();
29 while (const vpx_codec_cx_pkt_t *pkt = iter.Next()) {
30 if (pkt->kind != VPX_CODEC_STATS_PKT)
37 void Encoder::EncodeFrameInternal(const VideoSource &video,
38 const unsigned long frame_flags) {
40 const vpx_image_t *img = video.img();
42 // Handle first frame initialization
46 cfg_.g_timebase = video.timebase();
47 cfg_.rc_twopass_stats_in = stats_->buf();
48 res = vpx_codec_enc_init(&encoder_, CodecInterface(), &cfg_,
50 ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
53 // Handle frame resizing
54 if (cfg_.g_w != img->d_w || cfg_.g_h != img->d_h) {
57 res = vpx_codec_enc_config_set(&encoder_, &cfg_);
58 ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
62 API_REGISTER_STATE_CHECK(
63 res = vpx_codec_encode(&encoder_,
64 video.img(), video.pts(), video.duration(),
65 frame_flags, deadline_));
66 ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
69 void Encoder::Flush() {
70 const vpx_codec_err_t res = vpx_codec_encode(&encoder_, NULL, 0, 0, 0,
73 ASSERT_EQ(VPX_CODEC_ERROR, res) << EncoderError();
75 ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
78 void EncoderTest::InitializeConfig() {
79 const vpx_codec_err_t res = codec_->DefaultEncoderConfig(&cfg_, 0);
80 ASSERT_EQ(VPX_CODEC_OK, res);
83 void EncoderTest::SetMode(TestMode mode) {
86 deadline_ = VPX_DL_REALTIME;
91 deadline_ = VPX_DL_GOOD_QUALITY;
96 deadline_ = VPX_DL_BEST_QUALITY;
100 ASSERT_TRUE(false) << "Unexpected mode " << mode;
103 if (mode == kTwoPassGood || mode == kTwoPassBest)
108 // The function should return "true" most of the time, therefore no early
109 // break-out is implemented within the match checking process.
110 static bool compare_img(const vpx_image_t *img1,
111 const vpx_image_t *img2) {
112 bool match = (img1->fmt == img2->fmt) &&
113 (img1->d_w == img2->d_w) &&
114 (img1->d_h == img2->d_h);
116 const unsigned int width_y = img1->d_w;
117 const unsigned int height_y = img1->d_h;
119 for (i = 0; i < height_y; ++i)
120 match = (memcmp(img1->planes[VPX_PLANE_Y] + i * img1->stride[VPX_PLANE_Y],
121 img2->planes[VPX_PLANE_Y] + i * img2->stride[VPX_PLANE_Y],
122 width_y) == 0) && match;
123 const unsigned int width_uv = (img1->d_w + 1) >> 1;
124 const unsigned int height_uv = (img1->d_h + 1) >> 1;
125 for (i = 0; i < height_uv; ++i)
126 match = (memcmp(img1->planes[VPX_PLANE_U] + i * img1->stride[VPX_PLANE_U],
127 img2->planes[VPX_PLANE_U] + i * img2->stride[VPX_PLANE_U],
128 width_uv) == 0) && match;
129 for (i = 0; i < height_uv; ++i)
130 match = (memcmp(img1->planes[VPX_PLANE_V] + i * img1->stride[VPX_PLANE_V],
131 img2->planes[VPX_PLANE_V] + i * img2->stride[VPX_PLANE_V],
132 width_uv) == 0) && match;
136 void EncoderTest::MismatchHook(const vpx_image_t* /*img1*/,
137 const vpx_image_t* /*img2*/) {
138 ASSERT_TRUE(0) << "Encode/Decode mismatch found";
141 void EncoderTest::RunLoop(VideoSource *video) {
142 vpx_codec_dec_cfg_t dec_cfg = vpx_codec_dec_cfg_t();
146 ASSERT_TRUE(passes_ == 1 || passes_ == 2);
147 for (unsigned int pass = 0; pass < passes_; pass++) {
151 cfg_.g_pass = VPX_RC_ONE_PASS;
153 cfg_.g_pass = VPX_RC_FIRST_PASS;
155 cfg_.g_pass = VPX_RC_LAST_PASS;
158 Encoder* const encoder = codec_->CreateEncoder(cfg_, deadline_, init_flags_,
160 ASSERT_TRUE(encoder != NULL);
161 Decoder* const decoder = codec_->CreateDecoder(dec_cfg, 0);
163 for (again = true, video->Begin(); again; video->Next()) {
164 again = (video->img() != NULL);
166 PreEncodeFrameHook(video);
167 PreEncodeFrameHook(video, encoder);
168 encoder->EncodeFrame(video, frame_flags_);
170 CxDataIterator iter = encoder->GetCxData();
172 bool has_cxdata = false;
173 bool has_dxdata = false;
174 while (const vpx_codec_cx_pkt_t *pkt = iter.Next()) {
175 pkt = MutateEncoderOutputHook(pkt);
178 case VPX_CODEC_CX_FRAME_PKT:
180 if (decoder && DoDecode()) {
181 vpx_codec_err_t res_dec = decoder->DecodeFrame(
182 (const uint8_t*)pkt->data.frame.buf, pkt->data.frame.sz);
184 if (!HandleDecodeResult(res_dec, *video, decoder))
189 ASSERT_GE(pkt->data.frame.pts, last_pts_);
190 last_pts_ = pkt->data.frame.pts;
194 case VPX_CODEC_PSNR_PKT:
203 if (has_dxdata && has_cxdata) {
204 const vpx_image_t *img_enc = encoder->GetPreviewFrame();
205 DxDataIterator dec_iter = decoder->GetDxData();
206 const vpx_image_t *img_dec = dec_iter.Next();
207 if (img_enc && img_dec) {
208 const bool res = compare_img(img_enc, img_dec);
209 if (!res) { // Mismatch
210 MismatchHook(img_enc, img_dec);
214 DecompressedFrameHook(*img_dec, video->pts());
231 } // namespace libvpx_test