Merge changes I38e93fe2,I6d6a0fb6,I51155833,If4c3e5d4,Ia2f40ef2
[profile/ivi/libvpx.git] / 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 #include <string>
13 #include <vector>
14 #include "third_party/googletest/src/include/gtest/gtest.h"
15 #include "vpx/vpx_encoder.h"
16 #include "vpx/vp8cx.h"
17
18 namespace libvpx_test {
19
20 class VideoSource;
21
22 enum TestMode {
23   kRealTime,
24   kOnePassGood,
25   kOnePassBest,
26   kTwoPassGood,
27   kTwoPassBest
28 };
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)
34
35
36 // Provides an object to handle the libvpx get_cx_data() iteration pattern
37 class CxDataIterator {
38  public:
39   explicit CxDataIterator(vpx_codec_ctx_t *encoder)
40     : encoder_(encoder), iter_(NULL) {}
41
42   const vpx_codec_cx_pkt_t *Next() {
43     return vpx_codec_get_cx_data(encoder_, &iter_);
44   }
45
46  private:
47   vpx_codec_ctx_t  *encoder_;
48   vpx_codec_iter_t  iter_;
49 };
50
51
52 // Implements an in-memory store for libvpx twopass statistics
53 class TwopassStatsStore {
54  public:
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);
58   }
59
60   vpx_fixed_buf_t buf() {
61     const vpx_fixed_buf_t buf = { &buffer_[0], buffer_.size() };
62     return buf;
63   }
64
65  protected:
66   std::string  buffer_;
67 };
68
69
70 // Provides a simplified interface to manage one video encoding pass, given
71 // a configuration and video source.
72 //
73 // TODO(jkoleszar): The exact services it provides and the appropriate
74 // level of abstraction will be fleshed out as more tests are written.
75 class Encoder {
76  public:
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_));
81   }
82
83   ~Encoder() {
84     vpx_codec_destroy(&encoder_);
85   }
86
87   CxDataIterator GetCxData() {
88     return CxDataIterator(&encoder_);
89   }
90
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);
94
95   // Convenience wrapper for EncodeFrame()
96   void EncodeFrame(VideoSource *video) {
97     EncodeFrame(video, 0);
98   }
99
100   void set_deadline(unsigned long deadline) {
101     deadline_ = deadline;
102   }
103
104  protected:
105   const char *EncoderError() {
106     const char *detail = vpx_codec_error_detail(&encoder_);
107     return detail ? detail : vpx_codec_error(&encoder_);
108   }
109
110   // Encode an image
111   void EncodeFrameInternal(const VideoSource &video, unsigned long flags);
112
113   // Flush the encoder on EOS
114   void Flush();
115
116   vpx_codec_ctx_t      encoder_;
117   vpx_codec_enc_cfg_t  cfg_;
118   unsigned long        deadline_;
119   TwopassStatsStore   *stats_;
120 };
121
122
123 // Common test functionality for all Encoder tests.
124 //
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.
130 class EncoderTest {
131  protected:
132   EncoderTest() : abort_(false), flags_(0) {}
133
134   virtual ~EncoderTest() {}
135
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);
141   }
142
143   // Map the TestMode enum to the deadline_ and passes_ variables.
144   void SetMode(TestMode mode);
145
146   // Main loop.
147   virtual void RunLoop(VideoSource *video);
148
149   // Hook to be called at the beginning of a pass.
150   virtual void BeginPassHook(unsigned int pass) {}
151
152   // Hook to be called at the end of a pass.
153   virtual void EndPassHook() {}
154
155   // Hook to be called before encoding a frame.
156   virtual void PreEncodeFrameHook(VideoSource *video) {}
157
158   // Hook to be called on every compressed data packet.
159   virtual void FramePktHook(const vpx_codec_cx_pkt_t *pkt) {}
160
161   // Hook to determine whether the encode loop should continue.
162   virtual bool Continue() const { return !abort_; }
163
164   bool                 abort_;
165   vpx_codec_enc_cfg_t  cfg_;
166   unsigned int         passes_;
167   unsigned long        deadline_;
168   TwopassStatsStore    stats_;
169   unsigned long        flags_;
170 };
171
172 }  // namespace libvpx_test
173
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())
177
178 #endif  // TEST_ENCODE_TEST_DRIVER_H_