decode_test_driver.h 5.18 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 13
 */

#ifndef TEST_DECODE_TEST_DRIVER_H_
#define TEST_DECODE_TEST_DRIVER_H_
14
#include <cstring>
15
#include "third_party/googletest/src/googletest/include/gtest/gtest.h"
16 17
#include "./aom_config.h"
#include "aom/aom_decoder.h"
18

19
namespace libaom_test {
20

21
class CodecFactory;
22
class CompressedVideoSource;
23 24 25 26

// Provides an object to handle decoding output
class DxDataIterator {
 public:
27
  explicit DxDataIterator(aom_codec_ctx_t *decoder)
28
      : decoder_(decoder), iter_(NULL) {}
29

30
  const aom_image_t *Next() { return aom_codec_get_frame(decoder_, &iter_); }
31 32

 private:
33 34
  aom_codec_ctx_t *decoder_;
  aom_codec_iter_t iter_;
35 36 37
};

// Provides a simplified interface to manage one video decoding.
Yaowu Xu's avatar
Yaowu Xu committed
38 39
// Similar to Encoder class, the exact services should be added
// as more tests are added.
40 41
class Decoder {
 public:
42 43
  explicit Decoder(aom_codec_dec_cfg_t cfg)
      : cfg_(cfg), flags_(0), init_done_(false) {
44 45 46
    memset(&decoder_, 0, sizeof(decoder_));
  }

47 48
  Decoder(aom_codec_dec_cfg_t cfg, const aom_codec_flags_t flag)
      : cfg_(cfg), flags_(flag), init_done_(false) {
49 50 51
    memset(&decoder_, 0, sizeof(decoder_));
  }

52
  virtual ~Decoder() { aom_codec_destroy(&decoder_); }
53

54 55
  aom_codec_err_t PeekStream(const uint8_t *cxdata, size_t size,
                             aom_codec_stream_info_t *stream_info);
56

57
  aom_codec_err_t DecodeFrame(const uint8_t *cxdata, size_t size);
58

59
  aom_codec_err_t DecodeFrame(const uint8_t *cxdata, size_t size,
60 61
                              void *user_priv);

clang-format's avatar
clang-format committed
62
  DxDataIterator GetDxData() { return DxDataIterator(&decoder_); }
63

64
  void Control(int ctrl_id, int arg) { Control(ctrl_id, arg, AOM_CODEC_OK); }
65 66

  void Control(int ctrl_id, const void *arg) {
67
    InitOnce();
68 69
    const aom_codec_err_t res = aom_codec_control_(&decoder_, ctrl_id, arg);
    ASSERT_EQ(AOM_CODEC_OK, res) << DecodeError();
70 71
  }

72
  void Control(int ctrl_id, int arg, aom_codec_err_t expected_value) {
73
    InitOnce();
74
    const aom_codec_err_t res = aom_codec_control_(&decoder_, ctrl_id, arg);
75
    ASSERT_EQ(expected_value, res) << DecodeError();
76
  }
77

clang-format's avatar
clang-format committed
78
  const char *DecodeError() {
79 80
    const char *detail = aom_codec_error_detail(&decoder_);
    return detail ? detail : aom_codec_error(&decoder_);
81 82
  }

83
  // Passes the external frame buffer information to libaom.
84 85 86
  aom_codec_err_t SetFrameBufferFunctions(
      aom_get_frame_buffer_cb_fn_t cb_get,
      aom_release_frame_buffer_cb_fn_t cb_release, void *user_priv) {
87
    InitOnce();
88
    return aom_codec_set_frame_buffer_functions(&decoder_, cb_get, cb_release,
clang-format's avatar
clang-format committed
89
                                                user_priv);
90 91
  }

clang-format's avatar
clang-format committed
92
  const char *GetDecoderName() const {
93
    return aom_codec_iface_name(CodecInterface());
94 95
  }

96 97
  bool IsVP8() const;

98
  bool IsAV1() const;
99

100
  aom_codec_ctx_t *GetDecoder() { return &decoder_; }
101

102
 protected:
103
  virtual aom_codec_iface_t *CodecInterface() const = 0;
104 105 106

  void InitOnce() {
    if (!init_done_) {
107 108 109
      const aom_codec_err_t res =
          aom_codec_dec_init(&decoder_, CodecInterface(), &cfg_, flags_);
      ASSERT_EQ(AOM_CODEC_OK, res) << DecodeError();
110 111
      init_done_ = true;
    }
112 113
  }

114 115 116
  aom_codec_ctx_t decoder_;
  aom_codec_dec_cfg_t cfg_;
  aom_codec_flags_t flags_;
clang-format's avatar
clang-format committed
117
  bool init_done_;
118 119
};

120 121 122
// Common test functionality for all Decoder tests.
class DecoderTest {
 public:
123
  // Main decoding loop
124
  virtual void RunLoop(CompressedVideoSource *video);
125
  virtual void RunLoop(CompressedVideoSource *video,
126
                       const aom_codec_dec_cfg_t &dec_cfg);
127

128 129
  virtual void set_cfg(const aom_codec_dec_cfg_t &dec_cfg);
  virtual void set_flags(const aom_codec_flags_t flags);
130

131
  // Hook to be called before decompressing every frame.
clang-format's avatar
clang-format committed
132 133
  virtual void PreDecodeFrameHook(const CompressedVideoSource & /*video*/,
                                  Decoder * /*decoder*/) {}
134

135
  // Hook to be called to handle decode result. Return true to continue.
136
  virtual bool HandleDecodeResult(const aom_codec_err_t res_dec,
137
                                  Decoder *decoder) {
138 139
    EXPECT_EQ(AOM_CODEC_OK, res_dec) << decoder->DecodeError();
    return AOM_CODEC_OK == res_dec;
140 141
  }

142
  // Hook to be called on every decompressed frame.
143
  virtual void DecompressedFrameHook(const aom_image_t & /*img*/,
144
                                     const unsigned int /*frame_number*/) {}
145

146
  // Hook to be called on peek result
clang-format's avatar
clang-format committed
147
  virtual void HandlePeekResult(Decoder *const decoder,
148
                                CompressedVideoSource *video,
149
                                const aom_codec_err_t res_peek);
150

151
 protected:
152
  explicit DecoderTest(const CodecFactory *codec)
clang-format's avatar
clang-format committed
153
      : codec_(codec), cfg_(), flags_(0) {}
154 155

  virtual ~DecoderTest() {}
156 157

  const CodecFactory *codec_;
158 159
  aom_codec_dec_cfg_t cfg_;
  aom_codec_flags_t flags_;
160 161
};

162
}  // namespace libaom_test
163 164

#endif  // TEST_DECODE_TEST_DRIVER_H_