encode_test_driver.h 8.72 KB
Newer Older
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 14
#include <string>
#include <vector>
15

16
#include "third_party/googletest/src/include/gtest/gtest.h"
17 18

#include "./vpx_config.h"
James Zern's avatar
James Zern committed
19
#if CONFIG_VP8_ENCODER || CONFIG_VP9_ENCODER
Alex Converse's avatar
Alex Converse committed
20 21
#include "vpx/vp8cx.h"
#endif
22
#include "vpx/vpx_encoder.h"
23 24 25

namespace libvpx_test {

26
class CodecFactory;
27 28 29 30 31 32 33 34 35
class VideoSource;

enum TestMode {
  kRealTime,
  kOnePassGood,
  kOnePassBest,
  kTwoPassGood,
  kTwoPassBest
};
clang-format's avatar
clang-format committed
36 37 38 39
#define ALL_TEST_MODES                                                        \
  ::testing::Values(::libvpx_test::kRealTime, ::libvpx_test::kOnePassGood,    \
                    ::libvpx_test::kOnePassBest, ::libvpx_test::kTwoPassGood, \
                    ::libvpx_test::kTwoPassBest)
40

clang-format's avatar
clang-format committed
41 42 43
#define ONE_PASS_TEST_MODES                                                \
  ::testing::Values(::libvpx_test::kRealTime, ::libvpx_test::kOnePassGood, \
                    ::libvpx_test::kOnePassBest)
44

clang-format's avatar
clang-format committed
45 46
#define TWO_PASS_TEST_MODES \
  ::testing::Values(::libvpx_test::kTwoPassGood, ::libvpx_test::kTwoPassBest)
47 48 49 50 51

// Provides an object to handle the libvpx get_cx_data() iteration pattern
class CxDataIterator {
 public:
  explicit CxDataIterator(vpx_codec_ctx_t *encoder)
52
      : encoder_(encoder), iter_(NULL) {}
53 54 55 56 57 58

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

 private:
clang-format's avatar
clang-format committed
59 60
  vpx_codec_ctx_t *encoder_;
  vpx_codec_iter_t iter_;
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
};

// 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;
  }

clang-format's avatar
clang-format committed
76
  void Reset() { buffer_.clear(); }
77

78
 protected:
clang-format's avatar
clang-format committed
79
  std::string buffer_;
80 81 82 83 84 85 86 87 88 89
};

// 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) {
92 93 94
    memset(&encoder_, 0, sizeof(encoder_));
  }

clang-format's avatar
clang-format committed
95
  virtual ~Encoder() { vpx_codec_destroy(&encoder_); }
96

clang-format's avatar
clang-format committed
97
  CxDataIterator GetCxData() { return CxDataIterator(&encoder_); }
98

99 100
  void InitEncoder(VideoSource *video);

101 102 103
  const vpx_image_t *GetPreviewFrame() {
    return vpx_codec_get_preview_frame(&encoder_);
  }
104 105
  // This is a thin wrapper around vpx_codec_encode(), so refer to
  // vpx_encoder.h for its semantics.
106
  void EncodeFrame(VideoSource *video, const unsigned long frame_flags);
107 108

  // Convenience wrapper for EncodeFrame()
clang-format's avatar
clang-format committed
109
  void EncodeFrame(VideoSource *video) { EncodeFrame(video, 0); }
110

111 112 113 114 115
  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();
  }

116 117 118 119 120
  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();
  }

121 122 123 124 125
  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();
  }

126 127 128 129 130
  void Control(int ctrl_id, struct vpx_svc_layer_id *arg) {
    const vpx_codec_err_t res = vpx_codec_control_(&encoder_, ctrl_id, arg);
    ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
  }

131 132 133 134
  void Control(int ctrl_id, struct vpx_svc_parameters *arg) {
    const vpx_codec_err_t res = vpx_codec_control_(&encoder_, ctrl_id, arg);
    ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
  }
James Zern's avatar
James Zern committed
135
#if CONFIG_VP8_ENCODER || CONFIG_VP9_ENCODER
Alex Converse's avatar
Alex Converse committed
136 137 138 139 140 141
  void Control(int ctrl_id, vpx_active_map_t *arg) {
    const vpx_codec_err_t res = vpx_codec_control_(&encoder_, ctrl_id, arg);
    ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
  }
#endif

142 143 144 145 146 147 148
#if CONFIG_VP8_ENCODER
  void Control(int ctrl_id, vpx_roi_map_t *arg) {
    const vpx_codec_err_t res = vpx_codec_control_(&encoder_, ctrl_id, arg);
    ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
  }
#endif

149 150 151 152 153 154
  void Config(const vpx_codec_enc_cfg_t *cfg) {
    const vpx_codec_err_t res = vpx_codec_enc_config_set(&encoder_, cfg);
    ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
    cfg_ = *cfg;
  }

clang-format's avatar
clang-format committed
155
  void set_deadline(unsigned long deadline) { deadline_ = deadline; }
156 157

 protected:
clang-format's avatar
clang-format committed
158
  virtual vpx_codec_iface_t *CodecInterface() const = 0;
159

160 161 162 163 164 165
  const char *EncoderError() {
    const char *detail = vpx_codec_error_detail(&encoder_);
    return detail ? detail : vpx_codec_error(&encoder_);
  }

  // Encode an image
166 167
  void EncodeFrameInternal(const VideoSource &video,
                           const unsigned long frame_flags);
168 169 170 171

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

clang-format's avatar
clang-format committed
172 173 174 175 176
  vpx_codec_ctx_t encoder_;
  vpx_codec_enc_cfg_t cfg_;
  unsigned long deadline_;
  unsigned long init_flags_;
  TwopassStatsStore *stats_;
177 178 179 180 181 182 183 184 185 186 187
};

// 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:
188 189
  explicit EncoderTest(const CodecFactory *codec)
      : codec_(codec), abort_(false), init_flags_(0), frame_flags_(0),
190 191 192 193
        last_pts_(0) {
    // Default to 1 thread.
    cfg_.g_threads = 1;
  }
194 195 196 197

  virtual ~EncoderTest() {}

  // Initialize the cfg_ member with the default configuration.
198
  void InitializeConfig();
199 200 201 202

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

203 204 205 206 207
  // Set encoder flag.
  void set_init_flags(unsigned long flag) {  // NOLINT(runtime/int)
    init_flags_ = flag;
  }

208
  // Main loop
209 210 211
  virtual void RunLoop(VideoSource *video);

  // Hook to be called at the beginning of a pass.
212
  virtual void BeginPassHook(unsigned int /*pass*/) {}
213 214 215 216 217

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

  // Hook to be called before encoding a frame.
clang-format's avatar
clang-format committed
218 219 220
  virtual void PreEncodeFrameHook(VideoSource * /*video*/) {}
  virtual void PreEncodeFrameHook(VideoSource * /*video*/,
                                  Encoder * /*encoder*/) {}
221

222 223
  virtual void PostEncodeFrameHook(Encoder * /*encoder*/) {}

224
  // Hook to be called on every compressed data packet.
clang-format's avatar
clang-format committed
225
  virtual void FramePktHook(const vpx_codec_cx_pkt_t * /*pkt*/) {}
226

227
  // Hook to be called on every PSNR packet.
clang-format's avatar
clang-format committed
228
  virtual void PSNRPktHook(const vpx_codec_cx_pkt_t * /*pkt*/) {}
229

230 231 232
  // Hook to be called on every first pass stats packet.
  virtual void StatsPktHook(const vpx_codec_cx_pkt_t * /*pkt*/) {}

233
  // Hook to determine whether the encode loop should continue.
234 235 236
  virtual bool Continue() const {
    return !(::testing::Test::HasFatalFailure() || abort_);
  }
237

clang-format's avatar
clang-format committed
238
  const CodecFactory *codec_;
239 240 241 242
  // Hook to determine whether to decode frame after encoding
  virtual bool DoDecode() const { return 1; }

  // Hook to handle encode/decode mismatch
clang-format's avatar
clang-format committed
243
  virtual void MismatchHook(const vpx_image_t *img1, const vpx_image_t *img2);
244

245
  // Hook to be called on every decompressed frame.
clang-format's avatar
clang-format committed
246
  virtual void DecompressedFrameHook(const vpx_image_t & /*img*/,
247
                                     vpx_codec_pts_t /*pts*/) {}
248

249 250
  // Hook to be called to handle decode result. Return true to continue.
  virtual bool HandleDecodeResult(const vpx_codec_err_t res_dec,
clang-format's avatar
clang-format committed
251
                                  const VideoSource & /*video*/,
252 253 254 255 256
                                  Decoder *decoder) {
    EXPECT_EQ(VPX_CODEC_OK, res_dec) << decoder->DecodeError();
    return VPX_CODEC_OK == res_dec;
  }

John Koleszar's avatar
John Koleszar committed
257
  // Hook that can modify the encoder's output data
258
  virtual const vpx_codec_cx_pkt_t *MutateEncoderOutputHook(
John Koleszar's avatar
John Koleszar committed
259 260 261 262
      const vpx_codec_cx_pkt_t *pkt) {
    return pkt;
  }

clang-format's avatar
clang-format committed
263 264 265 266 267 268 269 270 271
  bool abort_;
  vpx_codec_enc_cfg_t cfg_;
  vpx_codec_dec_cfg_t dec_cfg_;
  unsigned int passes_;
  unsigned long deadline_;
  TwopassStatsStore stats_;
  unsigned long init_flags_;
  unsigned long frame_flags_;
  vpx_codec_pts_t last_pts_;
272 273 274 275 276
};

}  // namespace libvpx_test

#endif  // TEST_ENCODE_TEST_DRIVER_H_