]> granicus.if.org Git - libvpx/blob - test/encode_test_driver.cc
Merge "endian_inl.h: fix mips32 android build"
[libvpx] / test / encode_test_driver.cc
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 <string>
12
13 #include "third_party/googletest/src/include/gtest/gtest.h"
14
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"
21
22 namespace libvpx_test {
23 void Encoder::InitEncoder(VideoSource *video) {
24   vpx_codec_err_t res;
25   const vpx_image_t *img = video->img();
26
27   if (video->img() && !encoder_.priv) {
28     cfg_.g_w = img->d_w;
29     cfg_.g_h = img->d_h;
30     cfg_.g_timebase = video->timebase();
31     cfg_.rc_twopass_stats_in = stats_->buf();
32
33     res = vpx_codec_enc_init(&encoder_, CodecInterface(), &cfg_,
34                              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
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   // Handle twopass stats
62   CxDataIterator iter = GetCxData();
63
64   while (const vpx_codec_cx_pkt_t *pkt = iter.Next()) {
65     if (pkt->kind != VPX_CODEC_STATS_PKT)
66       continue;
67
68     stats_->Append(*pkt);
69   }
70 }
71
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(
87       res = vpx_codec_encode(&encoder_, img, video.pts(), video.duration(),
88                              frame_flags, deadline_));
89   ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
90 }
91
92 void Encoder::Flush() {
93   const vpx_codec_err_t res = vpx_codec_encode(&encoder_, NULL, 0, 0, 0,
94                                                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
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
107 void EncoderTest::SetMode(TestMode mode) {
108   switch (mode) {
109     case kRealTime:
110       deadline_ = VPX_DL_REALTIME;
111       break;
112
113     case kOnePassGood:
114     case kTwoPassGood:
115       deadline_ = VPX_DL_GOOD_QUALITY;
116       break;
117
118     case kOnePassBest:
119     case kTwoPassBest:
120       deadline_ = VPX_DL_BEST_QUALITY;
121       break;
122
123     default:
124       ASSERT_TRUE(false) << "Unexpected mode " << mode;
125   }
126
127   if (mode == kTwoPassGood || mode == kTwoPassBest)
128     passes_ = 2;
129   else
130     passes_ = 1;
131 }
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);
140
141   const unsigned int width_y  = img1->d_w;
142   const unsigned int height_y = img1->d_h;
143   unsigned int i;
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;
158   return match;
159 }
160
161 void EncoderTest::MismatchHook(const vpx_image_t* /*img1*/,
162                                const vpx_image_t* /*img2*/) {
163   ASSERT_TRUE(0) << "Encode/Decode mismatch found";
164 }
165
166 void EncoderTest::RunLoop(VideoSource *video) {
167   vpx_codec_dec_cfg_t dec_cfg = vpx_codec_dec_cfg_t();
168
169   stats_.Reset();
170
171   ASSERT_TRUE(passes_ == 1 || passes_ == 2);
172   for (unsigned int pass = 0; pass < passes_; pass++) {
173     last_pts_ = 0;
174
175     if (passes_ == 1)
176       cfg_.g_pass = VPX_RC_ONE_PASS;
177     else if (pass == 0)
178       cfg_.g_pass = VPX_RC_FIRST_PASS;
179     else
180       cfg_.g_pass = VPX_RC_LAST_PASS;
181
182     BeginPassHook(pass);
183     Encoder* const encoder = codec_->CreateEncoder(cfg_, deadline_, init_flags_,
184                                                    &stats_);
185     ASSERT_TRUE(encoder != NULL);
186
187     video->Begin();
188     encoder->InitEncoder(video);
189
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);
196     bool again;
197     for (again = true; again; video->Next()) {
198       again = (video->img() != NULL);
199
200       PreEncodeFrameHook(video);
201       PreEncodeFrameHook(video, encoder);
202       encoder->EncodeFrame(video, frame_flags_);
203
204       CxDataIterator iter = encoder->GetCxData();
205
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);
210         again = true;
211         switch (pkt->kind) {
212           case VPX_CODEC_CX_FRAME_PKT:
213             has_cxdata = true;
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);
217
218               if (!HandleDecodeResult(res_dec, *video, decoder))
219                 break;
220
221               has_dxdata = true;
222             }
223             ASSERT_GE(pkt->data.frame.pts, last_pts_);
224             last_pts_ = pkt->data.frame.pts;
225             FramePktHook(pkt);
226             break;
227
228           case VPX_CODEC_PSNR_PKT:
229             PSNRPktHook(pkt);
230             break;
231
232           default:
233             break;
234         }
235       }
236
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))
241           break;
242       }
243
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);
252           }
253         }
254         if (img_dec)
255           DecompressedFrameHook(*img_dec, video->pts());
256       }
257       if (!Continue())
258         break;
259     }
260
261     EndPassHook();
262
263     if (decoder)
264       delete decoder;
265     delete encoder;
266
267     if (!Continue())
268       break;
269   }
270 }
271
272 }  // namespace libvpx_test