encode_test_driver.h 8.14 KB
Newer Older
1
/*
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
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.
10 11 12
 */
#ifndef TEST_ENCODE_TEST_DRIVER_H_
#define TEST_ENCODE_TEST_DRIVER_H_
13

14 15
#include <string>
#include <vector>
16

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

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

Yaowu Xu's avatar
Yaowu Xu committed
25
namespace libaom_test {
26

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

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

clang-format's avatar
clang-format committed
42
#define ONE_PASS_TEST_MODES                                                \
Yaowu Xu's avatar
Yaowu Xu committed
43 44
  ::testing::Values(::libaom_test::kRealTime, ::libaom_test::kOnePassGood, \
                    ::libaom_test::kOnePassBest)
45

clang-format's avatar
clang-format committed
46
#define TWO_PASS_TEST_MODES \
Yaowu Xu's avatar
Yaowu Xu committed
47
  ::testing::Values(::libaom_test::kTwoPassGood, ::libaom_test::kTwoPassBest)
48

Yaowu Xu's avatar
Yaowu Xu committed
49
// Provides an object to handle the libaom get_cx_data() iteration pattern
50 51
class CxDataIterator {
 public:
Adrian Grange's avatar
Adrian Grange committed
52
  explicit CxDataIterator(aom_codec_ctx_t *encoder)
53
      : encoder_(encoder), iter_(NULL) {}
54

Adrian Grange's avatar
Adrian Grange committed
55 56
  const aom_codec_cx_pkt_t *Next() {
    return aom_codec_get_cx_data(encoder_, &iter_);
57 58 59
  }

 private:
Adrian Grange's avatar
Adrian Grange committed
60 61
  aom_codec_ctx_t *encoder_;
  aom_codec_iter_t iter_;
62 63
};

Yaowu Xu's avatar
Yaowu Xu committed
64
// Implements an in-memory store for libaom twopass statistics
65 66
class TwopassStatsStore {
 public:
Adrian Grange's avatar
Adrian Grange committed
67
  void Append(const aom_codec_cx_pkt_t &pkt) {
68 69 70 71
    buffer_.append(reinterpret_cast<char *>(pkt.data.twopass_stats.buf),
                   pkt.data.twopass_stats.sz);
  }

Adrian Grange's avatar
Adrian Grange committed
72 73
  aom_fixed_buf_t buf() {
    const aom_fixed_buf_t buf = { &buffer_[0], buffer_.size() };
74 75 76
    return buf;
  }

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

79
 protected:
clang-format's avatar
clang-format committed
80
  std::string buffer_;
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:
Adrian Grange's avatar
Adrian Grange committed
90
  Encoder(aom_codec_enc_cfg_t cfg, unsigned long deadline,
91
          const unsigned long init_flags, TwopassStatsStore *stats)
92
      : cfg_(cfg), deadline_(deadline), init_flags_(init_flags), stats_(stats) {
93 94 95
    memset(&encoder_, 0, sizeof(encoder_));
  }

Adrian Grange's avatar
Adrian Grange committed
96
  virtual ~Encoder() { aom_codec_destroy(&encoder_); }
97

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

100 101
  void InitEncoder(VideoSource *video);

Adrian Grange's avatar
Adrian Grange committed
102 103
  const aom_image_t *GetPreviewFrame() {
    return aom_codec_get_preview_frame(&encoder_);
104
  }
Adrian Grange's avatar
Adrian Grange committed
105 106
  // This is a thin wrapper around aom_codec_encode(), so refer to
  // aom_encoder.h for its semantics.
107
  void EncodeFrame(VideoSource *video, const unsigned long frame_flags);
108 109

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

112
  void Control(int ctrl_id, int arg) {
Adrian Grange's avatar
Adrian Grange committed
113
    const aom_codec_err_t res = aom_codec_control_(&encoder_, ctrl_id, arg);
Adrian Grange's avatar
Adrian Grange committed
114
    ASSERT_EQ(AOM_CODEC_OK, res) << EncoderError();
115 116
  }

117
  void Control(int ctrl_id, int *arg) {
Adrian Grange's avatar
Adrian Grange committed
118
    const aom_codec_err_t res = aom_codec_control_(&encoder_, ctrl_id, arg);
Adrian Grange's avatar
Adrian Grange committed
119
    ASSERT_EQ(AOM_CODEC_OK, res) << EncoderError();
120 121
  }

Adrian Grange's avatar
Adrian Grange committed
122 123
  void Control(int ctrl_id, struct aom_scaling_mode *arg) {
    const aom_codec_err_t res = aom_codec_control_(&encoder_, ctrl_id, arg);
Adrian Grange's avatar
Adrian Grange committed
124
    ASSERT_EQ(AOM_CODEC_OK, res) << EncoderError();
125 126
  }

Adrian Grange's avatar
Adrian Grange committed
127 128
  void Control(int ctrl_id, struct aom_svc_layer_id *arg) {
    const aom_codec_err_t res = aom_codec_control_(&encoder_, ctrl_id, arg);
Adrian Grange's avatar
Adrian Grange committed
129
    ASSERT_EQ(AOM_CODEC_OK, res) << EncoderError();
130 131
  }

132
#if CONFIG_AV1_ENCODER
Adrian Grange's avatar
Adrian Grange committed
133 134
  void Control(int ctrl_id, aom_active_map_t *arg) {
    const aom_codec_err_t res = aom_codec_control_(&encoder_, ctrl_id, arg);
Adrian Grange's avatar
Adrian Grange committed
135
    ASSERT_EQ(AOM_CODEC_OK, res) << EncoderError();
Alex Converse's avatar
Alex Converse committed
136 137 138
  }
#endif

Adrian Grange's avatar
Adrian Grange committed
139 140
  void Config(const aom_codec_enc_cfg_t *cfg) {
    const aom_codec_err_t res = aom_codec_enc_config_set(&encoder_, cfg);
Adrian Grange's avatar
Adrian Grange committed
141
    ASSERT_EQ(AOM_CODEC_OK, res) << EncoderError();
142 143 144
    cfg_ = *cfg;
  }

clang-format's avatar
clang-format committed
145
  void set_deadline(unsigned long deadline) { deadline_ = deadline; }
146 147

 protected:
Adrian Grange's avatar
Adrian Grange committed
148
  virtual aom_codec_iface_t *CodecInterface() const = 0;
149

150
  const char *EncoderError() {
Adrian Grange's avatar
Adrian Grange committed
151 152
    const char *detail = aom_codec_error_detail(&encoder_);
    return detail ? detail : aom_codec_error(&encoder_);
153 154 155
  }

  // Encode an image
156 157
  void EncodeFrameInternal(const VideoSource &video,
                           const unsigned long frame_flags);
158 159 160 161

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

Adrian Grange's avatar
Adrian Grange committed
162 163
  aom_codec_ctx_t encoder_;
  aom_codec_enc_cfg_t cfg_;
clang-format's avatar
clang-format committed
164 165 166
  unsigned long deadline_;
  unsigned long init_flags_;
  TwopassStatsStore *stats_;
167 168 169 170 171 172 173 174 175 176 177
};

// 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:
178 179
  explicit EncoderTest(const CodecFactory *codec)
      : codec_(codec), abort_(false), init_flags_(0), frame_flags_(0),
180 181 182 183
        last_pts_(0) {
    // Default to 1 thread.
    cfg_.g_threads = 1;
  }
184 185 186 187

  virtual ~EncoderTest() {}

  // Initialize the cfg_ member with the default configuration.
188
  void InitializeConfig();
189 190 191 192

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

193 194 195 196 197
  // Set encoder flag.
  void set_init_flags(unsigned long flag) {  // NOLINT(runtime/int)
    init_flags_ = flag;
  }

198
  // Main loop
199 200 201
  virtual void RunLoop(VideoSource *video);

  // Hook to be called at the beginning of a pass.
202
  virtual void BeginPassHook(unsigned int /*pass*/) {}
203 204 205 206 207

  // 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
208 209 210
  virtual void PreEncodeFrameHook(VideoSource * /*video*/) {}
  virtual void PreEncodeFrameHook(VideoSource * /*video*/,
                                  Encoder * /*encoder*/) {}
211 212

  // Hook to be called on every compressed data packet.
Adrian Grange's avatar
Adrian Grange committed
213
  virtual void FramePktHook(const aom_codec_cx_pkt_t * /*pkt*/) {}
214

215
  // Hook to be called on every PSNR packet.
Adrian Grange's avatar
Adrian Grange committed
216
  virtual void PSNRPktHook(const aom_codec_cx_pkt_t * /*pkt*/) {}
217

218
  // Hook to determine whether the encode loop should continue.
219 220 221
  virtual bool Continue() const {
    return !(::testing::Test::HasFatalFailure() || abort_);
  }
222

clang-format's avatar
clang-format committed
223
  const CodecFactory *codec_;
224 225 226 227
  // Hook to determine whether to decode frame after encoding
  virtual bool DoDecode() const { return 1; }

  // Hook to handle encode/decode mismatch
Adrian Grange's avatar
Adrian Grange committed
228
  virtual void MismatchHook(const aom_image_t *img1, const aom_image_t *img2);
229

230
  // Hook to be called on every decompressed frame.
Adrian Grange's avatar
Adrian Grange committed
231 232
  virtual void DecompressedFrameHook(const aom_image_t & /*img*/,
                                     aom_codec_pts_t /*pts*/) {}
233

234
  // Hook to be called to handle decode result. Return true to continue.
Adrian Grange's avatar
Adrian Grange committed
235
  virtual bool HandleDecodeResult(const aom_codec_err_t res_dec,
236
                                  Decoder *decoder) {
Adrian Grange's avatar
Adrian Grange committed
237 238
    EXPECT_EQ(AOM_CODEC_OK, res_dec) << decoder->DecodeError();
    return AOM_CODEC_OK == res_dec;
239 240
  }

John Koleszar's avatar
John Koleszar committed
241
  // Hook that can modify the encoder's output data
Adrian Grange's avatar
Adrian Grange committed
242 243
  virtual const aom_codec_cx_pkt_t *MutateEncoderOutputHook(
      const aom_codec_cx_pkt_t *pkt) {
John Koleszar's avatar
John Koleszar committed
244 245 246
    return pkt;
  }

clang-format's avatar
clang-format committed
247
  bool abort_;
Adrian Grange's avatar
Adrian Grange committed
248 249
  aom_codec_enc_cfg_t cfg_;
  aom_codec_dec_cfg_t dec_cfg_;
clang-format's avatar
clang-format committed
250 251 252 253 254
  unsigned int passes_;
  unsigned long deadline_;
  TwopassStatsStore stats_;
  unsigned long init_flags_;
  unsigned long frame_flags_;
Adrian Grange's avatar
Adrian Grange committed
255
  aom_codec_pts_t last_pts_;
256 257
};

Yaowu Xu's avatar
Yaowu Xu committed
258
}  // namespace libaom_test
259 260

#endif  // TEST_ENCODE_TEST_DRIVER_H_