encode_test_driver.h 6.83 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
2
3
4
5
6
7
8
9
10
11
/*
 *  Copyright (c) 2012 The WebM project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */
#ifndef TEST_ENCODE_TEST_DRIVER_H_
#define TEST_ENCODE_TEST_DRIVER_H_
12
13

#include "./vpx_config.h"
John Koleszar's avatar
John Koleszar committed
14
15
16
17
18
19
20
#include <string>
#include <vector>
#include "third_party/googletest/src/include/gtest/gtest.h"
#include "vpx/vpx_encoder.h"

namespace libvpx_test {

21
class CodecFactory;
John Koleszar's avatar
John Koleszar committed
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class VideoSource;

enum TestMode {
  kRealTime,
  kOnePassGood,
  kOnePassBest,
  kTwoPassGood,
  kTwoPassBest
};
#define ALL_TEST_MODES ::testing::Values(::libvpx_test::kRealTime, \
                                         ::libvpx_test::kOnePassGood, \
                                         ::libvpx_test::kOnePassBest, \
                                         ::libvpx_test::kTwoPassGood, \
                                         ::libvpx_test::kTwoPassBest)

John Koleszar's avatar
John Koleszar committed
37
38
39
40
#define ONE_PASS_TEST_MODES ::testing::Values(::libvpx_test::kRealTime, \
                                              ::libvpx_test::kOnePassGood, \
                                              ::libvpx_test::kOnePassBest)

41
42
43
#define TWO_PASS_TEST_MODES ::testing::Values(::libvpx_test::kTwoPassGood, \
                                              ::libvpx_test::kTwoPassBest)

John Koleszar's avatar
John Koleszar committed
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

// Provides an object to handle the libvpx get_cx_data() iteration pattern
class CxDataIterator {
 public:
  explicit CxDataIterator(vpx_codec_ctx_t *encoder)
    : encoder_(encoder), iter_(NULL) {}

  const vpx_codec_cx_pkt_t *Next() {
    return vpx_codec_get_cx_data(encoder_, &iter_);
  }

 private:
  vpx_codec_ctx_t  *encoder_;
  vpx_codec_iter_t  iter_;
};

// Implements an in-memory store for libvpx twopass statistics
class TwopassStatsStore {
 public:
  void Append(const vpx_codec_cx_pkt_t &pkt) {
    buffer_.append(reinterpret_cast<char *>(pkt.data.twopass_stats.buf),
                   pkt.data.twopass_stats.sz);
  }

  vpx_fixed_buf_t buf() {
    const vpx_fixed_buf_t buf = { &buffer_[0], buffer_.size() };
    return buf;
  }

73
74
75
76
  void Reset() {
    buffer_.clear();
  }

John Koleszar's avatar
John Koleszar committed
77
78
79
80
81
82
83
84
85
86
87
88
89
 protected:
  std::string  buffer_;
};


// Provides a simplified interface to manage one video encoding pass, given
// a configuration and video source.
//
// TODO(jkoleszar): The exact services it provides and the appropriate
// level of abstraction will be fleshed out as more tests are written.
class Encoder {
 public:
  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) {
John Koleszar's avatar
John Koleszar committed
92
93
94
95
96
97
98
99
100
101
102
    memset(&encoder_, 0, sizeof(encoder_));
  }

  ~Encoder() {
    vpx_codec_destroy(&encoder_);
  }

  CxDataIterator GetCxData() {
    return CxDataIterator(&encoder_);
  }

103
104
105
  const vpx_image_t *GetPreviewFrame() {
    return vpx_codec_get_preview_frame(&encoder_);
  }
John Koleszar's avatar
John Koleszar committed
106
107
  // This is a thin wrapper around vpx_codec_encode(), so refer to
  // vpx_encoder.h for its semantics.
108
  void EncodeFrame(VideoSource *video, const unsigned long frame_flags);
John Koleszar's avatar
John Koleszar committed
109
110
111
112
113
114

  // Convenience wrapper for EncodeFrame()
  void EncodeFrame(VideoSource *video) {
    EncodeFrame(video, 0);
  }

115
116
117
118
119
  void Control(int ctrl_id, int arg) {
    const vpx_codec_err_t res = vpx_codec_control_(&encoder_, ctrl_id, arg);
    ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
  }

120
121
122
123
124
  void Control(int ctrl_id, struct vpx_scaling_mode *arg) {
    const vpx_codec_err_t res = vpx_codec_control_(&encoder_, ctrl_id, arg);
    ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
  }

John Koleszar's avatar
John Koleszar committed
125
126
127
128
129
  void set_deadline(unsigned long deadline) {
    deadline_ = deadline;
  }

 protected:
130
131
  virtual const vpx_codec_iface_t* CodecInterface() const = 0;

John Koleszar's avatar
John Koleszar committed
132
133
134
135
136
137
  const char *EncoderError() {
    const char *detail = vpx_codec_error_detail(&encoder_);
    return detail ? detail : vpx_codec_error(&encoder_);
  }

  // Encode an image
138
139
  void EncodeFrameInternal(const VideoSource &video,
                           const unsigned long frame_flags);
John Koleszar's avatar
John Koleszar committed
140
141
142
143
144
145
146

  // Flush the encoder on EOS
  void Flush();

  vpx_codec_ctx_t      encoder_;
  vpx_codec_enc_cfg_t  cfg_;
  unsigned long        deadline_;
147
  unsigned long        init_flags_;
John Koleszar's avatar
John Koleszar committed
148
149
150
151
152
153
154
155
156
157
158
159
  TwopassStatsStore   *stats_;
};

// Common test functionality for all Encoder tests.
//
// This class is a mixin which provides the main loop common to all
// encoder tests. It provides hooks which can be overridden by subclasses
// to implement each test's specific behavior, while centralizing the bulk
// of the boilerplate. Note that it doesn't inherit the gtest testing
// classes directly, so that tests can be parameterized differently.
class EncoderTest {
 protected:
160
161
162
  explicit EncoderTest(const CodecFactory *codec)
      : codec_(codec), abort_(false), init_flags_(0), frame_flags_(0),
        last_pts_(0) {}
John Koleszar's avatar
John Koleszar committed
163
164
165
166

  virtual ~EncoderTest() {}

  // Initialize the cfg_ member with the default configuration.
167
  void InitializeConfig();
John Koleszar's avatar
John Koleszar committed
168
169
170
171

  // Map the TestMode enum to the deadline_ and passes_ variables.
  void SetMode(TestMode mode);

172
  // Main loop
John Koleszar's avatar
John Koleszar committed
173
174
175
176
177
178
179
180
181
182
  virtual void RunLoop(VideoSource *video);

  // Hook to be called at the beginning of a pass.
  virtual void BeginPassHook(unsigned int pass) {}

  // Hook to be called at the end of a pass.
  virtual void EndPassHook() {}

  // Hook to be called before encoding a frame.
  virtual void PreEncodeFrameHook(VideoSource *video) {}
183
  virtual void PreEncodeFrameHook(VideoSource *video, Encoder *encoder) {}
John Koleszar's avatar
John Koleszar committed
184
185
186
187

  // Hook to be called on every compressed data packet.
  virtual void FramePktHook(const vpx_codec_cx_pkt_t *pkt) {}

188
189
190
  // Hook to be called on every PSNR packet.
  virtual void PSNRPktHook(const vpx_codec_cx_pkt_t *pkt) {}

John Koleszar's avatar
John Koleszar committed
191
192
193
  // Hook to determine whether the encode loop should continue.
  virtual bool Continue() const { return !abort_; }

194
  const CodecFactory   *codec_;
195
196
197
198
199
200
201
  // Hook to determine whether to decode frame after encoding
  virtual bool DoDecode() const { return 1; }

  // Hook to handle encode/decode mismatch
  virtual void MismatchHook(const vpx_image_t *img1,
                            const vpx_image_t *img2);

202
203
204
205
  // Hook to be called on every decompressed frame.
  virtual void DecompressedFrameHook(const vpx_image_t& img,
                                     vpx_codec_pts_t pts) {}

John Koleszar's avatar
John Koleszar committed
206
207
208
209
210
211
  // Hook that can modify the encoder's output data
  virtual const vpx_codec_cx_pkt_t * MutateEncoderOutputHook(
      const vpx_codec_cx_pkt_t *pkt) {
    return pkt;
  }

John Koleszar's avatar
John Koleszar committed
212
213
214
215
216
  bool                 abort_;
  vpx_codec_enc_cfg_t  cfg_;
  unsigned int         passes_;
  unsigned long        deadline_;
  TwopassStatsStore    stats_;
217
218
  unsigned long        init_flags_;
  unsigned long        frame_flags_;
219
  vpx_codec_pts_t      last_pts_;
John Koleszar's avatar
John Koleszar committed
220
221
222
223
224
};

}  // namespace libvpx_test

#endif  // TEST_ENCODE_TEST_DRIVER_H_