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