]> granicus.if.org Git - libvpx/blob - test/encode_test_driver.cc
Enable decoder to pass through color space info
[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 "./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"
18
19 namespace libvpx_test {
20 void Encoder::InitEncoder(VideoSource *video) {
21   vpx_codec_err_t res;
22   const vpx_image_t *img = video->img();
23
24   if (video->img() && !encoder_.priv) {
25     cfg_.g_w = img->d_w;
26     cfg_.g_h = img->d_h;
27     cfg_.g_timebase = video->timebase();
28     cfg_.rc_twopass_stats_in = stats_->buf();
29     res = vpx_codec_enc_init(&encoder_, CodecInterface(), &cfg_,
30                              init_flags_);
31     ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
32   }
33 }
34
35 void Encoder::EncodeFrame(VideoSource *video, const unsigned long frame_flags) {
36   if (video->img())
37     EncodeFrameInternal(*video, frame_flags);
38   else
39     Flush();
40
41   // Handle twopass stats
42   CxDataIterator iter = GetCxData();
43
44   while (const vpx_codec_cx_pkt_t *pkt = iter.Next()) {
45     if (pkt->kind != VPX_CODEC_STATS_PKT)
46       continue;
47
48     stats_->Append(*pkt);
49   }
50 }
51
52 void Encoder::EncodeFrameInternal(const VideoSource &video,
53                                   const unsigned long frame_flags) {
54   vpx_codec_err_t res;
55   const vpx_image_t *img = video.img();
56
57   // Handle frame resizing
58   if (cfg_.g_w != img->d_w || cfg_.g_h != img->d_h) {
59     cfg_.g_w = img->d_w;
60     cfg_.g_h = img->d_h;
61     res = vpx_codec_enc_config_set(&encoder_, &cfg_);
62     ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
63   }
64
65   // Encode the frame
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();
70 }
71
72 void Encoder::Flush() {
73   const vpx_codec_err_t res = vpx_codec_encode(&encoder_, NULL, 0, 0, 0,
74                                                deadline_);
75   if (!encoder_.priv)
76     ASSERT_EQ(VPX_CODEC_ERROR, res) << EncoderError();
77   else
78     ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
79 }
80
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);
85 }
86
87 void EncoderTest::SetMode(TestMode mode) {
88   switch (mode) {
89     case kRealTime:
90       deadline_ = VPX_DL_REALTIME;
91       break;
92
93     case kOnePassGood:
94     case kTwoPassGood:
95       deadline_ = VPX_DL_GOOD_QUALITY;
96       break;
97
98     case kOnePassBest:
99     case kTwoPassBest:
100       deadline_ = VPX_DL_BEST_QUALITY;
101       break;
102
103     default:
104       ASSERT_TRUE(false) << "Unexpected mode " << mode;
105   }
106
107   if (mode == kTwoPassGood || mode == kTwoPassBest)
108     passes_ = 2;
109   else
110     passes_ = 1;
111 }
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);
120
121   const unsigned int width_y  = img1->d_w;
122   const unsigned int height_y = img1->d_h;
123   unsigned int i;
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;
138   return match;
139 }
140
141 void EncoderTest::MismatchHook(const vpx_image_t* /*img1*/,
142                                const vpx_image_t* /*img2*/) {
143   ASSERT_TRUE(0) << "Encode/Decode mismatch found";
144 }
145
146 void EncoderTest::RunLoop(VideoSource *video) {
147   vpx_codec_dec_cfg_t dec_cfg = vpx_codec_dec_cfg_t();
148
149   stats_.Reset();
150
151   ASSERT_TRUE(passes_ == 1 || passes_ == 2);
152   for (unsigned int pass = 0; pass < passes_; pass++) {
153     last_pts_ = 0;
154
155     if (passes_ == 1)
156       cfg_.g_pass = VPX_RC_ONE_PASS;
157     else if (pass == 0)
158       cfg_.g_pass = VPX_RC_FIRST_PASS;
159     else
160       cfg_.g_pass = VPX_RC_LAST_PASS;
161
162     BeginPassHook(pass);
163     Encoder* const encoder = codec_->CreateEncoder(cfg_, deadline_, init_flags_,
164                                                    &stats_);
165     ASSERT_TRUE(encoder != NULL);
166
167     video->Begin();
168     encoder->InitEncoder(video);
169
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);
176     bool again;
177     for (again = true; again; video->Next()) {
178       again = (video->img() != NULL);
179
180       PreEncodeFrameHook(video);
181       PreEncodeFrameHook(video, encoder);
182       encoder->EncodeFrame(video, frame_flags_);
183
184       CxDataIterator iter = encoder->GetCxData();
185
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);
190         again = true;
191         switch (pkt->kind) {
192           case VPX_CODEC_CX_FRAME_PKT:
193             has_cxdata = true;
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);
197
198               if (!HandleDecodeResult(res_dec, *video, decoder))
199                 break;
200
201               has_dxdata = true;
202             }
203             ASSERT_GE(pkt->data.frame.pts, last_pts_);
204             last_pts_ = pkt->data.frame.pts;
205             FramePktHook(pkt);
206             break;
207
208           case VPX_CODEC_PSNR_PKT:
209             PSNRPktHook(pkt);
210             break;
211
212           default:
213             break;
214         }
215       }
216
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))
221           break;
222       }
223
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);
232           }
233         }
234         if (img_dec)
235           DecompressedFrameHook(*img_dec, video->pts());
236       }
237       if (!Continue())
238         break;
239     }
240
241     EndPassHook();
242
243     if (decoder)
244       delete decoder;
245     delete encoder;
246
247     if (!Continue())
248       break;
249   }
250 }
251
252 }  // namespace libvpx_test