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