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 TEST_ENCODE_TEST_DRIVER_H_
11 #define TEST_ENCODE_TEST_DRIVER_H_
14 #include "third_party/googletest/src/include/gtest/gtest.h"
15 #include "vpx/vpx_encoder.h"
16 #include "vpx/vp8cx.h"
18 namespace libvpx_test {
29 #define ALL_TEST_MODES ::testing::Values(::libvpx_test::kRealTime, \
30 ::libvpx_test::kOnePassGood, \
31 ::libvpx_test::kOnePassBest, \
32 ::libvpx_test::kTwoPassGood, \
33 ::libvpx_test::kTwoPassBest)
36 // Provides an object to handle the libvpx get_cx_data() iteration pattern
37 class CxDataIterator {
39 explicit CxDataIterator(vpx_codec_ctx_t *encoder)
40 : encoder_(encoder), iter_(NULL) {}
42 const vpx_codec_cx_pkt_t *Next() {
43 return vpx_codec_get_cx_data(encoder_, &iter_);
47 vpx_codec_ctx_t *encoder_;
48 vpx_codec_iter_t iter_;
52 // Implements an in-memory store for libvpx twopass statistics
53 class TwopassStatsStore {
55 void Append(const vpx_codec_cx_pkt_t &pkt) {
56 buffer_.append(reinterpret_cast<char *>(pkt.data.twopass_stats.buf),
57 pkt.data.twopass_stats.sz);
60 vpx_fixed_buf_t buf() {
61 const vpx_fixed_buf_t buf = { &buffer_[0], buffer_.size() };
70 // Provides a simplified interface to manage one video encoding pass, given
71 // a configuration and video source.
73 // TODO(jkoleszar): The exact services it provides and the appropriate
74 // level of abstraction will be fleshed out as more tests are written.
77 Encoder(vpx_codec_enc_cfg_t cfg, unsigned long deadline,
78 TwopassStatsStore *stats)
79 : cfg_(cfg), deadline_(deadline), stats_(stats) {
80 memset(&encoder_, 0, sizeof(encoder_));
84 vpx_codec_destroy(&encoder_);
87 CxDataIterator GetCxData() {
88 return CxDataIterator(&encoder_);
91 // This is a thin wrapper around vpx_codec_encode(), so refer to
92 // vpx_encoder.h for its semantics.
93 void EncodeFrame(VideoSource *video, unsigned long flags);
95 // Convenience wrapper for EncodeFrame()
96 void EncodeFrame(VideoSource *video) {
97 EncodeFrame(video, 0);
100 void set_deadline(unsigned long deadline) {
101 deadline_ = deadline;
105 const char *EncoderError() {
106 const char *detail = vpx_codec_error_detail(&encoder_);
107 return detail ? detail : vpx_codec_error(&encoder_);
111 void EncodeFrameInternal(const VideoSource &video, unsigned long flags);
113 // Flush the encoder on EOS
116 vpx_codec_ctx_t encoder_;
117 vpx_codec_enc_cfg_t cfg_;
118 unsigned long deadline_;
119 TwopassStatsStore *stats_;
123 // Common test functionality for all Encoder tests.
125 // This class is a mixin which provides the main loop common to all
126 // encoder tests. It provides hooks which can be overridden by subclasses
127 // to implement each test's specific behavior, while centralizing the bulk
128 // of the boilerplate. Note that it doesn't inherit the gtest testing
129 // classes directly, so that tests can be parameterized differently.
132 EncoderTest() : abort_(false), flags_(0) {}
134 virtual ~EncoderTest() {}
136 // Initialize the cfg_ member with the default configuration.
137 void InitializeConfig() {
138 const vpx_codec_err_t res = vpx_codec_enc_config_default(
139 &vpx_codec_vp8_cx_algo, &cfg_, 0);
140 ASSERT_EQ(VPX_CODEC_OK, res);
143 // Map the TestMode enum to the deadline_ and passes_ variables.
144 void SetMode(TestMode mode);
147 virtual void RunLoop(VideoSource *video);
149 // Hook to be called at the beginning of a pass.
150 virtual void BeginPassHook(unsigned int pass) {}
152 // Hook to be called at the end of a pass.
153 virtual void EndPassHook() {}
155 // Hook to be called before encoding a frame.
156 virtual void PreEncodeFrameHook(VideoSource *video) {}
158 // Hook to be called on every compressed data packet.
159 virtual void FramePktHook(const vpx_codec_cx_pkt_t *pkt) {}
161 // Hook to determine whether the encode loop should continue.
162 virtual bool Continue() const { return !abort_; }
165 vpx_codec_enc_cfg_t cfg_;
166 unsigned int passes_;
167 unsigned long deadline_;
168 TwopassStatsStore stats_;
169 unsigned long flags_;
172 } // namespace libvpx_test
174 // Macros to be used with ::testing::Combine
175 #define PARAMS(...) ::testing::TestWithParam< std::tr1::tuple< __VA_ARGS__ > >
176 #define GET_PARAM(k) std::tr1::get< k >(GetParam())
178 #endif // TEST_ENCODE_TEST_DRIVER_H_