encode_test_driver.h 8.4 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

John Koleszar's avatar
John Koleszar committed
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"
John Koleszar's avatar
John Koleszar committed
24

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

27
class CodecFactory;
John Koleszar's avatar
John Koleszar committed
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)
John Koleszar's avatar
John Koleszar committed
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)
John Koleszar's avatar
John Koleszar committed
48

Yaowu Xu's avatar
Yaowu Xu committed
49
// Provides an object to handle the libaom get_cx_data() iteration pattern
John Koleszar's avatar
John Koleszar committed
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) {}
John Koleszar's avatar
John Koleszar committed
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_);
John Koleszar's avatar
John Koleszar committed
57 58 59
  }

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

Yaowu Xu's avatar
Yaowu Xu committed
64
// Implements an in-memory store for libaom twopass statistics
John Koleszar's avatar
John Koleszar committed
65 66
class TwopassStatsStore {
 public:
Adrian Grange's avatar
Adrian Grange committed
67
  void Append(const aom_codec_cx_pkt_t &pkt) {
John Koleszar's avatar
John Koleszar committed
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() };
John Koleszar's avatar
John Koleszar committed
74 75 76
    return buf;
  }

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

John Koleszar's avatar
John Koleszar committed
79
 protected:
clang-format's avatar
clang-format committed
80
  std::string buffer_;
John Koleszar's avatar
John Koleszar committed
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) {
John Koleszar's avatar
John Koleszar committed
93 94 95
    memset(&encoder_, 0, sizeof(encoder_));
  }

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

clang-format's avatar
clang-format committed
98
  CxDataIterator GetCxData() { return CxDataIterator(&encoder_); }
John Koleszar's avatar
John Koleszar committed
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);
John Koleszar's avatar
John Koleszar committed
108 109

  // Convenience wrapper for EncodeFrame()
clang-format's avatar
clang-format committed
110
  void EncodeFrame(VideoSource *video) { EncodeFrame(video, 0); }
John Koleszar's avatar
John Koleszar committed
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
  }

Adrian Grange's avatar
Adrian Grange committed
132 133
  void Control(int ctrl_id, struct aom_svc_parameters *arg) {
    const aom_codec_err_t res = aom_codec_control_(&encoder_, ctrl_id, arg);
Adrian Grange's avatar
Adrian Grange committed
134
    ASSERT_EQ(AOM_CODEC_OK, res) << EncoderError();
135
  }
136
#if CONFIG_AV1_ENCODER
Adrian Grange's avatar
Adrian Grange committed
137 138
  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
139
    ASSERT_EQ(AOM_CODEC_OK, res) << EncoderError();
Alex Converse's avatar
Alex Converse committed
140 141 142
  }
#endif

Adrian Grange's avatar
Adrian Grange committed
143 144
  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
145
    ASSERT_EQ(AOM_CODEC_OK, res) << EncoderError();
146 147 148
    cfg_ = *cfg;
  }

clang-format's avatar
clang-format committed
149
  void set_deadline(unsigned long deadline) { deadline_ = deadline; }
John Koleszar's avatar
John Koleszar committed
150 151

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

John Koleszar's avatar
John Koleszar committed
154
  const char *EncoderError() {
Adrian Grange's avatar
Adrian Grange committed
155 156
    const char *detail = aom_codec_error_detail(&encoder_);
    return detail ? detail : aom_codec_error(&encoder_);
John Koleszar's avatar
John Koleszar committed
157 158 159
  }

  // Encode an image
160 161
  void EncodeFrameInternal(const VideoSource &video,
                           const unsigned long frame_flags);
John Koleszar's avatar
John Koleszar committed
162 163 164 165

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

Adrian Grange's avatar
Adrian Grange committed
166 167
  aom_codec_ctx_t encoder_;
  aom_codec_enc_cfg_t cfg_;
clang-format's avatar
clang-format committed
168 169 170
  unsigned long deadline_;
  unsigned long init_flags_;
  TwopassStatsStore *stats_;
John Koleszar's avatar
John Koleszar committed
171 172 173 174 175 176 177 178 179 180 181
};

// 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:
182 183
  explicit EncoderTest(const CodecFactory *codec)
      : codec_(codec), abort_(false), init_flags_(0), frame_flags_(0),
Frank Galligan's avatar
Frank Galligan committed
184 185 186 187
        last_pts_(0) {
    // Default to 1 thread.
    cfg_.g_threads = 1;
  }
John Koleszar's avatar
John Koleszar committed
188 189 190 191

  virtual ~EncoderTest() {}

  // Initialize the cfg_ member with the default configuration.
192
  void InitializeConfig();
John Koleszar's avatar
John Koleszar committed
193 194 195 196

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

197 198 199 200 201
  // Set encoder flag.
  void set_init_flags(unsigned long flag) {  // NOLINT(runtime/int)
    init_flags_ = flag;
  }

202
  // Main loop
John Koleszar's avatar
John Koleszar committed
203 204 205
  virtual void RunLoop(VideoSource *video);

  // Hook to be called at the beginning of a pass.
206
  virtual void BeginPassHook(unsigned int /*pass*/) {}
John Koleszar's avatar
John Koleszar committed
207 208 209 210 211

  // 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
212 213 214
  virtual void PreEncodeFrameHook(VideoSource * /*video*/) {}
  virtual void PreEncodeFrameHook(VideoSource * /*video*/,
                                  Encoder * /*encoder*/) {}
John Koleszar's avatar
John Koleszar committed
215 216

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

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

John Koleszar's avatar
John Koleszar committed
222
  // Hook to determine whether the encode loop should continue.
223 224 225
  virtual bool Continue() const {
    return !(::testing::Test::HasFatalFailure() || abort_);
  }
John Koleszar's avatar
John Koleszar committed
226

clang-format's avatar
clang-format committed
227
  const CodecFactory *codec_;
228 229 230 231
  // 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
232
  virtual void MismatchHook(const aom_image_t *img1, const aom_image_t *img2);
233

234
  // Hook to be called on every decompressed frame.
Adrian Grange's avatar
Adrian Grange committed
235 236
  virtual void DecompressedFrameHook(const aom_image_t & /*img*/,
                                     aom_codec_pts_t /*pts*/) {}
237

238
  // Hook to be called to handle decode result. Return true to continue.
Adrian Grange's avatar
Adrian Grange committed
239
  virtual bool HandleDecodeResult(const aom_codec_err_t res_dec,
clang-format's avatar
clang-format committed
240
                                  const VideoSource & /*video*/,
241
                                  Decoder *decoder) {
Adrian Grange's avatar
Adrian Grange committed
242 243
    EXPECT_EQ(AOM_CODEC_OK, res_dec) << decoder->DecodeError();
    return AOM_CODEC_OK == res_dec;
244 245
  }

John Koleszar's avatar
John Koleszar committed
246
  // Hook that can modify the encoder's output data
Adrian Grange's avatar
Adrian Grange committed
247 248
  virtual const aom_codec_cx_pkt_t *MutateEncoderOutputHook(
      const aom_codec_cx_pkt_t *pkt) {
John Koleszar's avatar
John Koleszar committed
249 250 251
    return pkt;
  }

clang-format's avatar
clang-format committed
252
  bool abort_;
Adrian Grange's avatar
Adrian Grange committed
253 254
  aom_codec_enc_cfg_t cfg_;
  aom_codec_dec_cfg_t dec_cfg_;
clang-format's avatar
clang-format committed
255 256 257 258 259
  unsigned int passes_;
  unsigned long deadline_;
  TwopassStatsStore stats_;
  unsigned long init_flags_;
  unsigned long frame_flags_;
Adrian Grange's avatar
Adrian Grange committed
260
  aom_codec_pts_t last_pts_;
John Koleszar's avatar
John Koleszar committed
261 262
};

Yaowu Xu's avatar
Yaowu Xu committed
263
}  // namespace libaom_test
John Koleszar's avatar
John Koleszar committed
264 265

#endif  // TEST_ENCODE_TEST_DRIVER_H_