encode_test_driver.h 7.54 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
John Koleszar's avatar
John Koleszar committed
3
 *
4 5 6 7 8 9
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
John Koleszar's avatar
John Koleszar committed
10 11 12
 */
#ifndef TEST_ENCODE_TEST_DRIVER_H_
#define TEST_ENCODE_TEST_DRIVER_H_
13

John Koleszar's avatar
John Koleszar committed
14 15
#include <string>
#include <vector>
16

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

19 20 21
#include "./aom_config.h"
#if CONFIG_AV1_ENCODER
#include "aom/aomcx.h"
Alex Converse's avatar
Alex Converse committed
22
#endif
23
#include "aom/aom_encoder.h"
John Koleszar's avatar
John Koleszar committed
24

25
namespace libaom_test {
John Koleszar's avatar
John Koleszar committed
26

27
class CodecFactory;
John Koleszar's avatar
John Koleszar committed
28 29
class VideoSource;

30 31
enum TestMode { kRealTime, kOnePassGood, kTwoPassGood };
#define ALL_TEST_MODES                                                     \
32
  ::testing::Values(::libaom_test::kRealTime, ::libaom_test::kOnePassGood, \
33 34 35 36
                    ::libaom_test::kTwoPassGood)

#define ONE_PASS_TEST_MODES \
  ::testing::Values(::libaom_test::kRealTime, ::libaom_test::kOnePassGood)
37

38
#define TWO_PASS_TEST_MODES ::testing::Values(::libaom_test::kTwoPassGood)
John Koleszar's avatar
John Koleszar committed
39

40
// Provides an object to handle the libaom get_cx_data() iteration pattern
John Koleszar's avatar
John Koleszar committed
41 42
class CxDataIterator {
 public:
43
  explicit CxDataIterator(aom_codec_ctx_t *encoder)
44
      : encoder_(encoder), iter_(NULL) {}
John Koleszar's avatar
John Koleszar committed
45

46 47
  const aom_codec_cx_pkt_t *Next() {
    return aom_codec_get_cx_data(encoder_, &iter_);
John Koleszar's avatar
John Koleszar committed
48 49 50
  }

 private:
51 52
  aom_codec_ctx_t *encoder_;
  aom_codec_iter_t iter_;
John Koleszar's avatar
John Koleszar committed
53 54
};

55
// Implements an in-memory store for libaom twopass statistics
John Koleszar's avatar
John Koleszar committed
56 57
class TwopassStatsStore {
 public:
58
  void Append(const aom_codec_cx_pkt_t &pkt) {
John Koleszar's avatar
John Koleszar committed
59 60 61 62
    buffer_.append(reinterpret_cast<char *>(pkt.data.twopass_stats.buf),
                   pkt.data.twopass_stats.sz);
  }

63 64
  aom_fixed_buf_t buf() {
    const aom_fixed_buf_t buf = { &buffer_[0], buffer_.size() };
John Koleszar's avatar
John Koleszar committed
65 66 67
    return buf;
  }

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

John Koleszar's avatar
John Koleszar committed
70
 protected:
clang-format's avatar
clang-format committed
71
  std::string buffer_;
John Koleszar's avatar
John Koleszar committed
72 73 74 75 76 77 78 79 80
};

// 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:
Sean DuBois's avatar
Sean DuBois committed
81 82 83
  Encoder(aom_codec_enc_cfg_t cfg, const uint32_t init_flags,
          TwopassStatsStore *stats)
      : cfg_(cfg), init_flags_(init_flags), stats_(stats) {
John Koleszar's avatar
John Koleszar committed
84 85 86
    memset(&encoder_, 0, sizeof(encoder_));
  }

87
  virtual ~Encoder() { aom_codec_destroy(&encoder_); }
John Koleszar's avatar
John Koleszar committed
88

clang-format's avatar
clang-format committed
89
  CxDataIterator GetCxData() { return CxDataIterator(&encoder_); }
John Koleszar's avatar
John Koleszar committed
90

91 92
  void InitEncoder(VideoSource *video);

93 94
  const aom_image_t *GetPreviewFrame() {
    return aom_codec_get_preview_frame(&encoder_);
95
  }
96 97
  // This is a thin wrapper around aom_codec_encode(), so refer to
  // aom_encoder.h for its semantics.
98
  void EncodeFrame(VideoSource *video, const unsigned long frame_flags);
John Koleszar's avatar
John Koleszar committed
99 100

  // Convenience wrapper for EncodeFrame()
clang-format's avatar
clang-format committed
101
  void EncodeFrame(VideoSource *video) { EncodeFrame(video, 0); }
John Koleszar's avatar
John Koleszar committed
102

103
  void Control(int ctrl_id, int arg) {
104 105
    const aom_codec_err_t res = aom_codec_control_(&encoder_, ctrl_id, arg);
    ASSERT_EQ(AOM_CODEC_OK, res) << EncoderError();
106 107
  }

108
  void Control(int ctrl_id, int *arg) {
109 110
    const aom_codec_err_t res = aom_codec_control_(&encoder_, ctrl_id, arg);
    ASSERT_EQ(AOM_CODEC_OK, res) << EncoderError();
111 112
  }

113 114 115
  void Control(int ctrl_id, struct aom_scaling_mode *arg) {
    const aom_codec_err_t res = aom_codec_control_(&encoder_, ctrl_id, arg);
    ASSERT_EQ(AOM_CODEC_OK, res) << EncoderError();
116 117
  }

118 119 120 121
#if CONFIG_AV1_ENCODER
  void Control(int ctrl_id, aom_active_map_t *arg) {
    const aom_codec_err_t res = aom_codec_control_(&encoder_, ctrl_id, arg);
    ASSERT_EQ(AOM_CODEC_OK, res) << EncoderError();
Alex Converse's avatar
Alex Converse committed
122 123 124
  }
#endif

125 126 127
  void Config(const aom_codec_enc_cfg_t *cfg) {
    const aom_codec_err_t res = aom_codec_enc_config_set(&encoder_, cfg);
    ASSERT_EQ(AOM_CODEC_OK, res) << EncoderError();
128 129 130
    cfg_ = *cfg;
  }

John Koleszar's avatar
John Koleszar committed
131
 protected:
132
  virtual aom_codec_iface_t *CodecInterface() const = 0;
133

John Koleszar's avatar
John Koleszar committed
134
  const char *EncoderError() {
135 136
    const char *detail = aom_codec_error_detail(&encoder_);
    return detail ? detail : aom_codec_error(&encoder_);
John Koleszar's avatar
John Koleszar committed
137 138 139
  }

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

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

146 147
  aom_codec_ctx_t encoder_;
  aom_codec_enc_cfg_t cfg_;
clang-format's avatar
clang-format committed
148 149
  unsigned long init_flags_;
  TwopassStatsStore *stats_;
John Koleszar's avatar
John Koleszar committed
150 151 152 153 154 155 156 157 158 159 160
};

// 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:
161 162
  explicit EncoderTest(const CodecFactory *codec)
      : codec_(codec), abort_(false), init_flags_(0), frame_flags_(0),
Thomas Daede's avatar
Thomas Daede committed
163
        last_pts_(0), mode_(kRealTime) {
Frank Galligan's avatar
Frank Galligan committed
164 165 166
    // Default to 1 thread.
    cfg_.g_threads = 1;
  }
John Koleszar's avatar
John Koleszar committed
167 168 169 170

  virtual ~EncoderTest() {}

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

Sean DuBois's avatar
Sean DuBois committed
173
  // Map the TestMode enum to the passes_ variables.
John Koleszar's avatar
John Koleszar committed
174 175
  void SetMode(TestMode mode);

176 177 178 179 180
  // Set encoder flag.
  void set_init_flags(unsigned long flag) {  // NOLINT(runtime/int)
    init_flags_ = flag;
  }

181
  // Main loop
John Koleszar's avatar
John Koleszar committed
182 183 184
  virtual void RunLoop(VideoSource *video);

  // Hook to be called at the beginning of a pass.
185
  virtual void BeginPassHook(unsigned int /*pass*/) {}
John Koleszar's avatar
John Koleszar committed
186 187 188 189 190

  // 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
191 192 193
  virtual void PreEncodeFrameHook(VideoSource * /*video*/) {}
  virtual void PreEncodeFrameHook(VideoSource * /*video*/,
                                  Encoder * /*encoder*/) {}
John Koleszar's avatar
John Koleszar committed
194 195

  // Hook to be called on every compressed data packet.
196
  virtual void FramePktHook(const aom_codec_cx_pkt_t * /*pkt*/) {}
John Koleszar's avatar
John Koleszar committed
197

198
  // Hook to be called on every PSNR packet.
199
  virtual void PSNRPktHook(const aom_codec_cx_pkt_t * /*pkt*/) {}
200

John Koleszar's avatar
John Koleszar committed
201
  // Hook to determine whether the encode loop should continue.
202 203 204
  virtual bool Continue() const {
    return !(::testing::Test::HasFatalFailure() || abort_);
  }
John Koleszar's avatar
John Koleszar committed
205

clang-format's avatar
clang-format committed
206
  const CodecFactory *codec_;
207 208 209 210
  // Hook to determine whether to decode frame after encoding
  virtual bool DoDecode() const { return 1; }

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

213
  // Hook to be called on every decompressed frame.
214 215
  virtual void DecompressedFrameHook(const aom_image_t & /*img*/,
                                     aom_codec_pts_t /*pts*/) {}
216

217
  // Hook to be called to handle decode result. Return true to continue.
218
  virtual bool HandleDecodeResult(const aom_codec_err_t res_dec,
219
                                  Decoder *decoder) {
220 221
    EXPECT_EQ(AOM_CODEC_OK, res_dec) << decoder->DecodeError();
    return AOM_CODEC_OK == res_dec;
222 223
  }

John Koleszar's avatar
John Koleszar committed
224
  // Hook that can modify the encoder's output data
225 226
  virtual const aom_codec_cx_pkt_t *MutateEncoderOutputHook(
      const aom_codec_cx_pkt_t *pkt) {
John Koleszar's avatar
John Koleszar committed
227 228 229
    return pkt;
  }

clang-format's avatar
clang-format committed
230
  bool abort_;
231
  aom_codec_enc_cfg_t cfg_;
clang-format's avatar
clang-format committed
232 233 234 235
  unsigned int passes_;
  TwopassStatsStore stats_;
  unsigned long init_flags_;
  unsigned long frame_flags_;
236
  aom_codec_pts_t last_pts_;
Thomas Daede's avatar
Thomas Daede committed
237
  TestMode mode_;
John Koleszar's avatar
John Koleszar committed
238 239
};

240
}  // namespace libaom_test
John Koleszar's avatar
John Koleszar committed
241 242

#endif  // TEST_ENCODE_TEST_DRIVER_H_