decode_test_driver.h 5.44 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 29 30 31 32 33 34 35 36 37 38

  const vpx_image_t *Next() {
    return vpx_codec_get_frame(decoder_, &iter_);
  }

 private:
  vpx_codec_ctx_t  *decoder_;
  vpx_codec_iter_t  iter_;
};

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

  Decoder(vpx_codec_dec_cfg_t cfg, const vpx_codec_flags_t flag,
          unsigned long deadline)  // NOLINT
      : cfg_(cfg), flags_(flag), deadline_(deadline), init_done_(false) {
51 52 53
    memset(&decoder_, 0, sizeof(decoder_));
  }

54
  virtual ~Decoder() {
55 56 57
    vpx_codec_destroy(&decoder_);
  }

58 59 60
  vpx_codec_err_t PeekStream(const uint8_t *cxdata, size_t size,
                             vpx_codec_stream_info_t *stream_info);

61
  vpx_codec_err_t DecodeFrame(const uint8_t *cxdata, size_t size);
62

63 64 65
  vpx_codec_err_t DecodeFrame(const uint8_t *cxdata, size_t size,
                              void *user_priv);

66 67 68 69 70 71 72 73 74
  DxDataIterator GetDxData() {
    return DxDataIterator(&decoder_);
  }

  void set_deadline(unsigned long deadline) {
    deadline_ = deadline;
  }

  void Control(int ctrl_id, int arg) {
75 76 77 78
    Control(ctrl_id, arg, VPX_CODEC_OK);
  }

  void Control(int ctrl_id, const void *arg) {
79
    InitOnce();
80 81 82 83
    const vpx_codec_err_t res = vpx_codec_control_(&decoder_, ctrl_id, arg);
    ASSERT_EQ(VPX_CODEC_OK, res) << DecodeError();
  }

84
  void Control(int ctrl_id, int arg, vpx_codec_err_t expected_value) {
85
    InitOnce();
86
    const vpx_codec_err_t res = vpx_codec_control_(&decoder_, ctrl_id, arg);
87
    ASSERT_EQ(expected_value, res) << DecodeError();
88
  }
89 90

  const char* DecodeError() {
91 92 93 94
    const char *detail = vpx_codec_error_detail(&decoder_);
    return detail ? detail : vpx_codec_error(&decoder_);
  }

95 96 97 98 99 100 101 102 103
  // 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();
    return vpx_codec_set_frame_buffer_functions(
        &decoder_, cb_get, cb_release, user_priv);
  }

104
  const char* GetDecoderName() const {
105 106 107
    return vpx_codec_iface_name(CodecInterface());
  }

108 109
  bool IsVP8() const;

110 111 112 113
  vpx_codec_ctx_t * GetDecoder() {
    return &decoder_;
  }

114
 protected:
115
  virtual vpx_codec_iface_t* CodecInterface() const = 0;
116 117 118 119 120

  void InitOnce() {
    if (!init_done_) {
      const vpx_codec_err_t res = vpx_codec_dec_init(&decoder_,
                                                     CodecInterface(),
121
                                                     &cfg_, flags_);
122 123 124
      ASSERT_EQ(VPX_CODEC_OK, res) << DecodeError();
      init_done_ = true;
    }
125 126
  }

127 128
  vpx_codec_ctx_t     decoder_;
  vpx_codec_dec_cfg_t cfg_;
129
  vpx_codec_flags_t   flags_;
130
  unsigned int        deadline_;
131
  bool                init_done_;
132 133
};

134 135 136
// Common test functionality for all Decoder tests.
class DecoderTest {
 public:
137
  // Main decoding loop
138
  virtual void RunLoop(CompressedVideoSource *video);
139 140
  virtual void RunLoop(CompressedVideoSource *video,
                       const vpx_codec_dec_cfg_t &dec_cfg);
141

142 143 144
  virtual void set_cfg(const vpx_codec_dec_cfg_t &dec_cfg);
  virtual void set_flags(const vpx_codec_flags_t flags);

145
  // Hook to be called before decompressing every frame.
146 147
  virtual void PreDecodeFrameHook(const CompressedVideoSource& /*video*/,
                                  Decoder* /*decoder*/) {}
148

149 150
  // Hook to be called to handle decode result. Return true to continue.
  virtual bool HandleDecodeResult(const vpx_codec_err_t res_dec,
151
                                  const CompressedVideoSource& /*video*/,
152 153 154 155 156
                                  Decoder *decoder) {
    EXPECT_EQ(VPX_CODEC_OK, res_dec) << decoder->DecodeError();
    return VPX_CODEC_OK == res_dec;
  }

157
  // Hook to be called on every decompressed frame.
158 159
  virtual void DecompressedFrameHook(const vpx_image_t& /*img*/,
                                     const unsigned int /*frame_number*/) {}
160

161 162 163 164 165
  // Hook to be called on peek result
  virtual void HandlePeekResult(Decoder* const decoder,
                                CompressedVideoSource *video,
                                const vpx_codec_err_t res_peek);

166
 protected:
167 168 169 170
  explicit DecoderTest(const CodecFactory *codec)
      : codec_(codec),
        cfg_(),
        flags_(0) {}
171 172

  virtual ~DecoderTest() {}
173 174

  const CodecFactory *codec_;
175 176
  vpx_codec_dec_cfg_t cfg_;
  vpx_codec_flags_t   flags_;
177 178
};

179
}  // namespace libvpx_test
180 181

#endif  // TEST_DECODE_TEST_DRIVER_H_