2 * Copyright (c) 2012 The WebM project authors. All Rights Reserved.
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.
10 #ifndef VPX_TEST_ENCODE_TEST_DRIVER_H_
11 #define VPX_TEST_ENCODE_TEST_DRIVER_H_
16 #include "third_party/googletest/src/include/gtest/gtest.h"
18 #include "./vpx_config.h"
19 #if CONFIG_VP8_ENCODER || CONFIG_VP9_ENCODER
20 #include "vpx/vp8cx.h"
22 #include "vpx/vpx_encoder.h"
24 namespace libvpx_test {
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)
41 #define ONE_PASS_TEST_MODES \
42 ::testing::Values(::libvpx_test::kRealTime, ::libvpx_test::kOnePassGood, \
43 ::libvpx_test::kOnePassBest)
45 #define TWO_PASS_TEST_MODES \
46 ::testing::Values(::libvpx_test::kTwoPassGood, ::libvpx_test::kTwoPassBest)
48 // Provides an object to handle the libvpx get_cx_data() iteration pattern
49 class CxDataIterator {
51 explicit CxDataIterator(vpx_codec_ctx_t *encoder)
52 : encoder_(encoder), iter_(NULL) {}
54 const vpx_codec_cx_pkt_t *Next() {
55 return vpx_codec_get_cx_data(encoder_, &iter_);
59 vpx_codec_ctx_t *encoder_;
60 vpx_codec_iter_t iter_;
63 // Implements an in-memory store for libvpx twopass statistics
64 class TwopassStatsStore {
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);
71 vpx_fixed_buf_t buf() {
72 const vpx_fixed_buf_t buf = { &buffer_[0], buffer_.size() };
76 void Reset() { buffer_.clear(); }
82 // Provides a simplified interface to manage one video encoding pass, given
83 // a configuration and video source.
85 // TODO(jkoleszar): The exact services it provides and the appropriate
86 // level of abstraction will be fleshed out as more tests are written.
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_));
95 virtual ~Encoder() { vpx_codec_destroy(&encoder_); }
97 CxDataIterator GetCxData() { return CxDataIterator(&encoder_); }
99 void InitEncoder(VideoSource *video);
101 const vpx_image_t *GetPreviewFrame() {
102 return vpx_codec_get_preview_frame(&encoder_);
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, const unsigned long frame_flags);
108 // Convenience wrapper for EncodeFrame()
109 void EncodeFrame(VideoSource *video) { EncodeFrame(video, 0); }
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();
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();
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();
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();
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();
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();
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();
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();
151 #if CONFIG_VP9_ENCODER
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();
156 #endif // CONFIG_VP9_ENCODER
158 #if CONFIG_VP8_ENCODER || CONFIG_VP9_ENCODER
159 void Control(int ctrl_id, vpx_active_map_t *arg) {
160 const vpx_codec_err_t res = vpx_codec_control_(&encoder_, ctrl_id, arg);
161 ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
164 void Control(int ctrl_id, vpx_roi_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();
169 void Config(const vpx_codec_enc_cfg_t *cfg) {
170 const vpx_codec_err_t res = vpx_codec_enc_config_set(&encoder_, cfg);
171 ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
175 void set_deadline(unsigned long deadline) { deadline_ = deadline; }
178 virtual vpx_codec_iface_t *CodecInterface() const = 0;
180 const char *EncoderError() {
181 const char *detail = vpx_codec_error_detail(&encoder_);
182 return detail ? detail : vpx_codec_error(&encoder_);
186 void EncodeFrameInternal(const VideoSource &video,
187 const unsigned long frame_flags);
189 // Flush the encoder on EOS
192 vpx_codec_ctx_t encoder_;
193 vpx_codec_enc_cfg_t cfg_;
194 unsigned long deadline_;
195 unsigned long init_flags_;
196 TwopassStatsStore *stats_;
199 // Common test functionality for all Encoder tests.
201 // This class is a mixin which provides the main loop common to all
202 // encoder tests. It provides hooks which can be overridden by subclasses
203 // to implement each test's specific behavior, while centralizing the bulk
204 // of the boilerplate. Note that it doesn't inherit the gtest testing
205 // classes directly, so that tests can be parameterized differently.
208 explicit EncoderTest(const CodecFactory *codec)
209 : codec_(codec), abort_(false), init_flags_(0), frame_flags_(0),
211 // Default to 1 thread.
215 virtual ~EncoderTest() {}
217 // Initialize the cfg_ member with the default configuration.
218 void InitializeConfig();
220 // Map the TestMode enum to the deadline_ and passes_ variables.
221 void SetMode(TestMode mode);
224 void set_init_flags(unsigned long flag) { // NOLINT(runtime/int)
229 virtual void RunLoop(VideoSource *video);
231 // Hook to be called at the beginning of a pass.
232 virtual void BeginPassHook(unsigned int /*pass*/) {}
234 // Hook to be called at the end of a pass.
235 virtual void EndPassHook() {}
237 // Hook to be called before encoding a frame.
238 virtual void PreEncodeFrameHook(VideoSource * /*video*/) {}
239 virtual void PreEncodeFrameHook(VideoSource * /*video*/,
240 Encoder * /*encoder*/) {}
242 virtual void PreDecodeFrameHook(VideoSource * /*video*/,
243 Decoder * /*decoder*/) {}
245 virtual void PostEncodeFrameHook(Encoder * /*encoder*/) {}
247 // Hook to be called on every compressed data packet.
248 virtual void FramePktHook(const vpx_codec_cx_pkt_t * /*pkt*/) {}
250 // Hook to be called on every PSNR packet.
251 virtual void PSNRPktHook(const vpx_codec_cx_pkt_t * /*pkt*/) {}
253 // Hook to be called on every first pass stats packet.
254 virtual void StatsPktHook(const vpx_codec_cx_pkt_t * /*pkt*/) {}
256 // Hook to determine whether the encode loop should continue.
257 virtual bool Continue() const {
258 return !(::testing::Test::HasFatalFailure() || abort_);
261 const CodecFactory *codec_;
262 // Hook to determine whether to decode frame after encoding
263 virtual bool DoDecode() const { return 1; }
265 // Hook to handle encode/decode mismatch
266 virtual void MismatchHook(const vpx_image_t *img1, const vpx_image_t *img2);
268 // Hook to be called on every decompressed frame.
269 virtual void DecompressedFrameHook(const vpx_image_t & /*img*/,
270 vpx_codec_pts_t /*pts*/) {}
272 // Hook to be called to handle decode result. Return true to continue.
273 virtual bool HandleDecodeResult(const vpx_codec_err_t res_dec,
274 const VideoSource & /*video*/,
276 EXPECT_EQ(VPX_CODEC_OK, res_dec) << decoder->DecodeError();
277 return VPX_CODEC_OK == res_dec;
280 // Hook that can modify the encoder's output data
281 virtual const vpx_codec_cx_pkt_t *MutateEncoderOutputHook(
282 const vpx_codec_cx_pkt_t *pkt) {
287 vpx_codec_enc_cfg_t cfg_;
288 vpx_codec_dec_cfg_t dec_cfg_;
289 unsigned int passes_;
290 unsigned long deadline_;
291 TwopassStatsStore stats_;
292 unsigned long init_flags_;
293 unsigned long frame_flags_;
294 vpx_codec_pts_t last_pts_;
297 } // namespace libvpx_test
299 #endif // VPX_TEST_ENCODE_TEST_DRIVER_H_