decode_test_driver.h 5.11 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*
 *  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_DECODE_TEST_DRIVER_H_
#define TEST_DECODE_TEST_DRIVER_H_
13
#include <cstring>
14
#include "third_party/googletest/src/include/gtest/gtest.h"
Yaowu Xu's avatar
Yaowu Xu committed
15
#include "./vpx_config.h"
16
#include "vpx/vpx_decoder.h"
17

18 19
namespace libvpx_test {

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

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

clang-format's avatar
clang-format committed
29
  const vpx_image_t *Next() { return vpx_codec_get_frame(decoder_, &iter_); }
30 31

 private:
clang-format's avatar
clang-format committed
32 33
  vpx_codec_ctx_t *decoder_;
  vpx_codec_iter_t iter_;
34 35 36
};

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

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

clang-format's avatar
clang-format committed
51
  virtual ~Decoder() { vpx_codec_destroy(&decoder_); }
52

53 54 55
  vpx_codec_err_t PeekStream(const uint8_t *cxdata, size_t size,
                             vpx_codec_stream_info_t *stream_info);

56
  vpx_codec_err_t DecodeFrame(const uint8_t *cxdata, size_t size);
57

58 59 60
  vpx_codec_err_t DecodeFrame(const uint8_t *cxdata, size_t size,
                              void *user_priv);

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

clang-format's avatar
clang-format committed
63
  void Control(int ctrl_id, int arg) { Control(ctrl_id, arg, VPX_CODEC_OK); }
64 65

  void Control(int ctrl_id, const void *arg) {
66
    InitOnce();
67 68 69 70
    const vpx_codec_err_t res = vpx_codec_control_(&decoder_, ctrl_id, arg);
    ASSERT_EQ(VPX_CODEC_OK, res) << DecodeError();
  }

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

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

82 83 84 85 86
  // Passes the external frame buffer information to libvpx.
  vpx_codec_err_t SetFrameBufferFunctions(
      vpx_get_frame_buffer_cb_fn_t cb_get,
      vpx_release_frame_buffer_cb_fn_t cb_release, void *user_priv) {
    InitOnce();
clang-format's avatar
clang-format committed
87 88
    return vpx_codec_set_frame_buffer_functions(&decoder_, cb_get, cb_release,
                                                user_priv);
89 90
  }

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

95 96
  bool IsVP8() const;

clang-format's avatar
clang-format committed
97
  vpx_codec_ctx_t *GetDecoder() { return &decoder_; }
98

99
 protected:
clang-format's avatar
clang-format committed
100
  virtual vpx_codec_iface_t *CodecInterface() const = 0;
101 102 103

  void InitOnce() {
    if (!init_done_) {
clang-format's avatar
clang-format committed
104 105
      const vpx_codec_err_t res =
          vpx_codec_dec_init(&decoder_, CodecInterface(), &cfg_, flags_);
106 107 108
      ASSERT_EQ(VPX_CODEC_OK, res) << DecodeError();
      init_done_ = true;
    }
109 110
  }

clang-format's avatar
clang-format committed
111
  vpx_codec_ctx_t decoder_;
112
  vpx_codec_dec_cfg_t cfg_;
clang-format's avatar
clang-format committed
113 114
  vpx_codec_flags_t flags_;
  bool init_done_;
115 116
};

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

125 126 127
  virtual void set_cfg(const vpx_codec_dec_cfg_t &dec_cfg);
  virtual void set_flags(const vpx_codec_flags_t flags);

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

132 133
  // 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
134
                                  const CompressedVideoSource & /*video*/,
135 136 137 138 139
                                  Decoder *decoder) {
    EXPECT_EQ(VPX_CODEC_OK, res_dec) << decoder->DecodeError();
    return VPX_CODEC_OK == res_dec;
  }

140
  // Hook to be called on every decompressed frame.
clang-format's avatar
clang-format committed
141
  virtual void DecompressedFrameHook(const vpx_image_t & /*img*/,
142
                                     const unsigned int /*frame_number*/) {}
143

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

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

  virtual ~DecoderTest() {}
154 155

  const CodecFactory *codec_;
156
  vpx_codec_dec_cfg_t cfg_;
clang-format's avatar
clang-format committed
157
  vpx_codec_flags_t flags_;
158 159
};

160
}  // namespace libvpx_test
161 162

#endif  // TEST_DECODE_TEST_DRIVER_H_