• 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 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:
CxDataIterator(vpx_codec_ctx_t * encoder)52   explicit CxDataIterator(vpx_codec_ctx_t *encoder)
53       : encoder_(encoder), iter_(NULL) {}
54 
Next()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:
Append(const vpx_codec_cx_pkt_t & pkt)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 
buf()72   vpx_fixed_buf_t buf() {
73     const vpx_fixed_buf_t buf = { &buffer_[0], buffer_.size() };
74     return buf;
75   }
76 
Reset()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:
Encoder(vpx_codec_enc_cfg_t cfg,unsigned long deadline,const unsigned long init_flags,TwopassStatsStore * stats)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 
~Encoder()99   virtual ~Encoder() {
100     vpx_codec_destroy(&encoder_);
101   }
102 
GetCxData()103   CxDataIterator GetCxData() {
104     return CxDataIterator(&encoder_);
105   }
106 
GetPreviewFrame()107   const vpx_image_t *GetPreviewFrame() {
108     return vpx_codec_get_preview_frame(&encoder_);
109   }
110   // This is a thin wrapper around vpx_codec_encode(), so refer to
111   // vpx_encoder.h for its semantics.
112   void EncodeFrame(VideoSource *video, const unsigned long frame_flags);
113 
114   // Convenience wrapper for EncodeFrame()
EncodeFrame(VideoSource * video)115   void EncodeFrame(VideoSource *video) {
116     EncodeFrame(video, 0);
117   }
118 
Control(int ctrl_id,int arg)119   void Control(int ctrl_id, int arg) {
120     const vpx_codec_err_t res = vpx_codec_control_(&encoder_, ctrl_id, arg);
121     ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
122   }
123 
Control(int ctrl_id,struct vpx_scaling_mode * arg)124   void Control(int ctrl_id, struct vpx_scaling_mode *arg) {
125     const vpx_codec_err_t res = vpx_codec_control_(&encoder_, ctrl_id, arg);
126     ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
127   }
128 
Control(int ctrl_id,struct vpx_svc_layer_id * arg)129   void Control(int ctrl_id, struct vpx_svc_layer_id *arg) {
130     const vpx_codec_err_t res = vpx_codec_control_(&encoder_, ctrl_id, arg);
131     ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
132   }
133 
134 #if CONFIG_VP8_ENCODER || CONFIG_VP9_ENCODER
Control(int ctrl_id,vpx_active_map_t * arg)135   void Control(int ctrl_id, vpx_active_map_t *arg) {
136     const vpx_codec_err_t res = vpx_codec_control_(&encoder_, ctrl_id, arg);
137     ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
138   }
139 #endif
140 
set_deadline(unsigned long deadline)141   void set_deadline(unsigned long deadline) {
142     deadline_ = deadline;
143   }
144 
145  protected:
146   virtual vpx_codec_iface_t* CodecInterface() const = 0;
147 
EncoderError()148   const char *EncoderError() {
149     const char *detail = vpx_codec_error_detail(&encoder_);
150     return detail ? detail : vpx_codec_error(&encoder_);
151   }
152 
153   // Encode an image
154   void EncodeFrameInternal(const VideoSource &video,
155                            const unsigned long frame_flags);
156 
157   // Flush the encoder on EOS
158   void Flush();
159 
160   vpx_codec_ctx_t      encoder_;
161   vpx_codec_enc_cfg_t  cfg_;
162   unsigned long        deadline_;
163   unsigned long        init_flags_;
164   TwopassStatsStore   *stats_;
165 };
166 
167 // Common test functionality for all Encoder tests.
168 //
169 // This class is a mixin which provides the main loop common to all
170 // encoder tests. It provides hooks which can be overridden by subclasses
171 // to implement each test's specific behavior, while centralizing the bulk
172 // of the boilerplate. Note that it doesn't inherit the gtest testing
173 // classes directly, so that tests can be parameterized differently.
174 class EncoderTest {
175  protected:
EncoderTest(const CodecFactory * codec)176   explicit EncoderTest(const CodecFactory *codec)
177       : codec_(codec), abort_(false), init_flags_(0), frame_flags_(0),
178         last_pts_(0) {}
179 
~EncoderTest()180   virtual ~EncoderTest() {}
181 
182   // Initialize the cfg_ member with the default configuration.
183   void InitializeConfig();
184 
185   // Map the TestMode enum to the deadline_ and passes_ variables.
186   void SetMode(TestMode mode);
187 
188   // Main loop
189   virtual void RunLoop(VideoSource *video);
190 
191   // Hook to be called at the beginning of a pass.
BeginPassHook(unsigned int pass)192   virtual void BeginPassHook(unsigned int pass) {}
193 
194   // Hook to be called at the end of a pass.
EndPassHook()195   virtual void EndPassHook() {}
196 
197   // Hook to be called before encoding a frame.
PreEncodeFrameHook(VideoSource * video)198   virtual void PreEncodeFrameHook(VideoSource *video) {}
PreEncodeFrameHook(VideoSource * video,Encoder * encoder)199   virtual void PreEncodeFrameHook(VideoSource *video, Encoder *encoder) {}
200 
201   // Hook to be called on every compressed data packet.
FramePktHook(const vpx_codec_cx_pkt_t * pkt)202   virtual void FramePktHook(const vpx_codec_cx_pkt_t *pkt) {}
203 
204   // Hook to be called on every PSNR packet.
PSNRPktHook(const vpx_codec_cx_pkt_t * pkt)205   virtual void PSNRPktHook(const vpx_codec_cx_pkt_t *pkt) {}
206 
207   // Hook to determine whether the encode loop should continue.
Continue()208   virtual bool Continue() const {
209     return !(::testing::Test::HasFatalFailure() || abort_);
210   }
211 
212   const CodecFactory   *codec_;
213   // Hook to determine whether to decode frame after encoding
DoDecode()214   virtual bool DoDecode() const { return 1; }
215 
216   // Hook to handle encode/decode mismatch
217   virtual void MismatchHook(const vpx_image_t *img1,
218                             const vpx_image_t *img2);
219 
220   // Hook to be called on every decompressed frame.
DecompressedFrameHook(const vpx_image_t & img,vpx_codec_pts_t pts)221   virtual void DecompressedFrameHook(const vpx_image_t& img,
222                                      vpx_codec_pts_t pts) {}
223 
224   // Hook that can modify the encoder's output data
MutateEncoderOutputHook(const vpx_codec_cx_pkt_t * pkt)225   virtual const vpx_codec_cx_pkt_t * MutateEncoderOutputHook(
226       const vpx_codec_cx_pkt_t *pkt) {
227     return pkt;
228   }
229 
230   bool                 abort_;
231   vpx_codec_enc_cfg_t  cfg_;
232   unsigned int         passes_;
233   unsigned long        deadline_;
234   TwopassStatsStore    stats_;
235   unsigned long        init_flags_;
236   unsigned long        frame_flags_;
237   vpx_codec_pts_t      last_pts_;
238 };
239 
240 }  // namespace libvpx_test
241 
242 #endif  // TEST_ENCODE_TEST_DRIVER_H_
243