• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef VPX_TEST_ENCODE_TEST_DRIVER_H_
11 #define VPX_TEST_ENCODE_TEST_DRIVER_H_
12 
13 #include <string>
14 #include <vector>
15 
16 #include "third_party/googletest/src/include/gtest/gtest.h"
17 
18 #include "./vpx_config.h"
19 #if CONFIG_VP8_ENCODER || CONFIG_VP9_ENCODER
20 #include "vpx/vp8cx.h"
21 #endif
22 #include "vpx/vpx_tpl.h"
23 
24 namespace libvpx_test {
25 
26 class CodecFactory;
27 class VideoSource;
28 
29 enum TestMode {
30   kRealTime,
31   kOnePassGood,
32   kOnePassBest,
33   kTwoPassGood,
34   kTwoPassBest
35 };
36 #define ALL_TEST_MODES                                                        \
37   ::testing::Values(::libvpx_test::kRealTime, ::libvpx_test::kOnePassGood,    \
38                     ::libvpx_test::kOnePassBest, ::libvpx_test::kTwoPassGood, \
39                     ::libvpx_test::kTwoPassBest)
40 
41 #define ONE_PASS_TEST_MODES                                                \
42   ::testing::Values(::libvpx_test::kRealTime, ::libvpx_test::kOnePassGood, \
43                     ::libvpx_test::kOnePassBest)
44 
45 #define TWO_PASS_TEST_MODES \
46   ::testing::Values(::libvpx_test::kTwoPassGood, ::libvpx_test::kTwoPassBest)
47 
48 // Provides an object to handle the libvpx get_cx_data() iteration pattern
49 class CxDataIterator {
50  public:
CxDataIterator(vpx_codec_ctx_t * encoder)51   explicit CxDataIterator(vpx_codec_ctx_t *encoder)
52       : encoder_(encoder), iter_(nullptr) {}
53 
Next()54   const vpx_codec_cx_pkt_t *Next() {
55     return vpx_codec_get_cx_data(encoder_, &iter_);
56   }
57 
58  private:
59   vpx_codec_ctx_t *encoder_;
60   vpx_codec_iter_t iter_;
61 };
62 
63 // Implements an in-memory store for libvpx twopass statistics
64 class TwopassStatsStore {
65  public:
Append(const vpx_codec_cx_pkt_t & pkt)66   void Append(const vpx_codec_cx_pkt_t &pkt) {
67     buffer_.append(reinterpret_cast<char *>(pkt.data.twopass_stats.buf),
68                    pkt.data.twopass_stats.sz);
69   }
70 
buf()71   vpx_fixed_buf_t buf() {
72     const vpx_fixed_buf_t buf = { &buffer_[0], buffer_.size() };
73     return buf;
74   }
75 
Reset()76   void Reset() { buffer_.clear(); }
77 
78  protected:
79   std::string buffer_;
80 };
81 
82 // Provides a simplified interface to manage one video encoding pass, given
83 // a configuration and video source.
84 //
85 // TODO(jkoleszar): The exact services it provides and the appropriate
86 // level of abstraction will be fleshed out as more tests are written.
87 class Encoder {
88  public:
Encoder(vpx_codec_enc_cfg_t cfg,unsigned long deadline,const unsigned long init_flags,TwopassStatsStore * stats)89   Encoder(vpx_codec_enc_cfg_t cfg, unsigned long deadline,
90           const unsigned long init_flags, TwopassStatsStore *stats)
91       : cfg_(cfg), deadline_(deadline), init_flags_(init_flags), stats_(stats) {
92     memset(&encoder_, 0, sizeof(encoder_));
93   }
94 
~Encoder()95   virtual ~Encoder() { vpx_codec_destroy(&encoder_); }
96 
GetCxData()97   CxDataIterator GetCxData() { return CxDataIterator(&encoder_); }
98 
99   void InitEncoder(VideoSource *video);
100 
GetPreviewFrame()101   const vpx_image_t *GetPreviewFrame() {
102     return vpx_codec_get_preview_frame(&encoder_);
103   }
104   // This is a thin wrapper around vpx_codec_encode(), so refer to
105   // vpx_encoder.h for its semantics.
106   void EncodeFrame(VideoSource *video, vpx_enc_frame_flags_t frame_flags);
107 
108   // Convenience wrapper for EncodeFrame()
EncodeFrame(VideoSource * video)109   void EncodeFrame(VideoSource *video) { EncodeFrame(video, 0); }
110 
Control(int ctrl_id,int arg)111   void Control(int ctrl_id, int arg) {
112     const vpx_codec_err_t res = vpx_codec_control_(&encoder_, ctrl_id, arg);
113     ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
114   }
115 
Control(int ctrl_id,int * arg)116   void Control(int ctrl_id, int *arg) {
117     const vpx_codec_err_t res = vpx_codec_control_(&encoder_, ctrl_id, arg);
118     ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
119   }
120 
Control(int ctrl_id,struct vpx_scaling_mode * arg)121   void Control(int ctrl_id, struct vpx_scaling_mode *arg) {
122     const vpx_codec_err_t res = vpx_codec_control_(&encoder_, ctrl_id, arg);
123     ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
124   }
125 
Control(int ctrl_id,struct vpx_svc_layer_id * arg)126   void Control(int ctrl_id, struct vpx_svc_layer_id *arg) {
127     const vpx_codec_err_t res = vpx_codec_control_(&encoder_, ctrl_id, arg);
128     ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
129   }
130 
Control(int ctrl_id,struct vpx_svc_ref_frame_config * arg)131   void Control(int ctrl_id, struct vpx_svc_ref_frame_config *arg) {
132     const vpx_codec_err_t res = vpx_codec_control_(&encoder_, ctrl_id, arg);
133     ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
134   }
135 
Control(int ctrl_id,struct vpx_svc_parameters * arg)136   void Control(int ctrl_id, struct vpx_svc_parameters *arg) {
137     const vpx_codec_err_t res = vpx_codec_control_(&encoder_, ctrl_id, arg);
138     ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
139   }
140 
Control(int ctrl_id,struct vpx_svc_frame_drop * arg)141   void Control(int ctrl_id, struct vpx_svc_frame_drop *arg) {
142     const vpx_codec_err_t res = vpx_codec_control_(&encoder_, ctrl_id, arg);
143     ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
144   }
145 
Control(int ctrl_id,struct vpx_svc_spatial_layer_sync * arg)146   void Control(int ctrl_id, struct vpx_svc_spatial_layer_sync *arg) {
147     const vpx_codec_err_t res = vpx_codec_control_(&encoder_, ctrl_id, arg);
148     ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
149   }
150 
151 #if CONFIG_VP9_ENCODER
Control(int ctrl_id,vpx_rc_funcs_t * arg)152   void Control(int ctrl_id, vpx_rc_funcs_t *arg) {
153     const vpx_codec_err_t res = vpx_codec_control_(&encoder_, ctrl_id, arg);
154     ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
155   }
156 
Control(int ctrl_id,VpxTplGopStats * arg)157   void Control(int ctrl_id, VpxTplGopStats *arg) {
158     const vpx_codec_err_t res = vpx_codec_control_(&encoder_, ctrl_id, arg);
159     ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
160   }
161 #endif  // CONFIG_VP9_ENCODER
162 
163 #if CONFIG_VP8_ENCODER || CONFIG_VP9_ENCODER
Control(int ctrl_id,vpx_active_map_t * arg)164   void Control(int ctrl_id, vpx_active_map_t *arg) {
165     const vpx_codec_err_t res = vpx_codec_control_(&encoder_, ctrl_id, arg);
166     ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
167   }
168 
Control(int ctrl_id,vpx_roi_map_t * arg)169   void Control(int ctrl_id, vpx_roi_map_t *arg) {
170     const vpx_codec_err_t res = vpx_codec_control_(&encoder_, ctrl_id, arg);
171     ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
172   }
173 #endif
Config(const vpx_codec_enc_cfg_t * cfg)174   void Config(const vpx_codec_enc_cfg_t *cfg) {
175     const vpx_codec_err_t res = vpx_codec_enc_config_set(&encoder_, cfg);
176     ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
177     cfg_ = *cfg;
178   }
179 
set_deadline(unsigned long deadline)180   void set_deadline(unsigned long deadline) { deadline_ = deadline; }
181 
182  protected:
183   virtual vpx_codec_iface_t *CodecInterface() const = 0;
184 
EncoderError()185   const char *EncoderError() {
186     const char *detail = vpx_codec_error_detail(&encoder_);
187     return detail ? detail : vpx_codec_error(&encoder_);
188   }
189 
190   // Encode an image
191   void EncodeFrameInternal(const VideoSource &video,
192                            vpx_enc_frame_flags_t frame_flags);
193 
194   // Flush the encoder on EOS
195   void Flush();
196 
197   vpx_codec_ctx_t encoder_;
198   vpx_codec_enc_cfg_t cfg_;
199   unsigned long deadline_;
200   unsigned long init_flags_;
201   TwopassStatsStore *stats_;
202 };
203 
204 // Common test functionality for all Encoder tests.
205 //
206 // This class is a mixin which provides the main loop common to all
207 // encoder tests. It provides hooks which can be overridden by subclasses
208 // to implement each test's specific behavior, while centralizing the bulk
209 // of the boilerplate. Note that it doesn't inherit the gtest testing
210 // classes directly, so that tests can be parameterized differently.
211 class EncoderTest {
212  protected:
EncoderTest(const CodecFactory * codec)213   explicit EncoderTest(const CodecFactory *codec)
214       : codec_(codec), abort_(false), init_flags_(0), frame_flags_(0) {
215     // Default to 1 thread.
216     cfg_.g_threads = 1;
217   }
218 
~EncoderTest()219   virtual ~EncoderTest() {}
220 
221   // Initialize the cfg_ member with the default configuration.
222   void InitializeConfig();
223 
224   // Map the TestMode enum to the deadline_ and passes_ variables.
225   void SetMode(TestMode mode);
226 
227   // Set encoder flag.
set_init_flags(unsigned long flag)228   void set_init_flags(unsigned long flag) {  // NOLINT(runtime/int)
229     init_flags_ = flag;
230   }
231 
232   // Main loop
233   virtual void RunLoop(VideoSource *video);
234 
235   // Hook to be called at the beginning of a pass.
BeginPassHook(unsigned int)236   virtual void BeginPassHook(unsigned int /*pass*/) {}
237 
238   // Hook to be called at the end of a pass.
EndPassHook()239   virtual void EndPassHook() {}
240 
241   // Hook to be called before encoding a frame.
PreEncodeFrameHook(VideoSource *)242   virtual void PreEncodeFrameHook(VideoSource * /*video*/) {}
PreEncodeFrameHook(VideoSource *,Encoder *)243   virtual void PreEncodeFrameHook(VideoSource * /*video*/,
244                                   Encoder * /*encoder*/) {}
245 
PreDecodeFrameHook(VideoSource *,Decoder *)246   virtual void PreDecodeFrameHook(VideoSource * /*video*/,
247                                   Decoder * /*decoder*/) {}
248 
PostEncodeFrameHook(Encoder *)249   virtual void PostEncodeFrameHook(Encoder * /*encoder*/) {}
250 
251   // Hook to be called on every compressed data packet.
FramePktHook(const vpx_codec_cx_pkt_t *)252   virtual void FramePktHook(const vpx_codec_cx_pkt_t * /*pkt*/) {}
253 
254   // Hook to be called on every PSNR packet.
PSNRPktHook(const vpx_codec_cx_pkt_t *)255   virtual void PSNRPktHook(const vpx_codec_cx_pkt_t * /*pkt*/) {}
256 
257   // Hook to be called on every first pass stats packet.
StatsPktHook(const vpx_codec_cx_pkt_t *)258   virtual void StatsPktHook(const vpx_codec_cx_pkt_t * /*pkt*/) {}
259 
260   // Hook to determine whether the encode loop should continue.
Continue()261   virtual bool Continue() const {
262     return !(::testing::Test::HasFatalFailure() || abort_);
263   }
264 
265   const CodecFactory *codec_;
266   // Hook to determine whether to decode frame after encoding
DoDecode()267   virtual bool DoDecode() const { return true; }
268 
269   // Hook to handle encode/decode mismatch
270   virtual void MismatchHook(const vpx_image_t *img1, const vpx_image_t *img2);
271 
272   // Hook to be called on every decompressed frame.
DecompressedFrameHook(const vpx_image_t &,vpx_codec_pts_t)273   virtual void DecompressedFrameHook(const vpx_image_t & /*img*/,
274                                      vpx_codec_pts_t /*pts*/) {}
275 
276   // Hook to be called to handle decode result. Return true to continue.
HandleDecodeResult(const vpx_codec_err_t res_dec,const VideoSource &,Decoder * decoder)277   virtual bool HandleDecodeResult(const vpx_codec_err_t res_dec,
278                                   const VideoSource & /*video*/,
279                                   Decoder *decoder) {
280     EXPECT_EQ(VPX_CODEC_OK, res_dec) << decoder->DecodeError();
281     return VPX_CODEC_OK == res_dec;
282   }
283 
284   // Hook that can modify the encoder's output data
MutateEncoderOutputHook(const vpx_codec_cx_pkt_t * pkt)285   virtual const vpx_codec_cx_pkt_t *MutateEncoderOutputHook(
286       const vpx_codec_cx_pkt_t *pkt) {
287     return pkt;
288   }
289 
290   bool abort_;
291   vpx_codec_enc_cfg_t cfg_;
292   vpx_codec_dec_cfg_t dec_cfg_;
293   unsigned int passes_;
294   unsigned long deadline_;
295   TwopassStatsStore stats_;
296   unsigned long init_flags_;
297   vpx_enc_frame_flags_t frame_flags_;
298 };
299 
300 }  // namespace libvpx_test
301 
302 #endif  // VPX_TEST_ENCODE_TEST_DRIVER_H_
303