decode_test_driver.h 4.41 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
      : cfg_(cfg), deadline_(deadline), init_done_(false) {
45
46
47
    memset(&decoder_, 0, sizeof(decoder_));
  }

48
  virtual ~Decoder() {
49
50
51
    vpx_codec_destroy(&decoder_);
  }

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

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

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

60
61
62
63
64
65
66
67
68
  DxDataIterator GetDxData() {
    return DxDataIterator(&decoder_);
  }

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

  void Control(int ctrl_id, int arg) {
69
    InitOnce();
70
71
72
73
    const vpx_codec_err_t res = vpx_codec_control_(&decoder_, ctrl_id, arg);
    ASSERT_EQ(VPX_CODEC_OK, res) << DecodeError();
  }

74
  void Control(int ctrl_id, const void *arg) {
75
    InitOnce();
76
77
78
    const vpx_codec_err_t res = vpx_codec_control_(&decoder_, ctrl_id, arg);
    ASSERT_EQ(VPX_CODEC_OK, res) << DecodeError();
  }
79
80

  const char* DecodeError() {
81
82
83
84
    const char *detail = vpx_codec_error_detail(&decoder_);
    return detail ? detail : vpx_codec_error(&decoder_);
  }

85
86
87
88
89
90
91
92
93
  // 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);
  }

94
95
96
97
  const char* GetDecoderName() {
    return vpx_codec_iface_name(CodecInterface());
  }

98
 protected:
99
  virtual vpx_codec_iface_t* CodecInterface() const = 0;
100
101
102
103
104
105
106
107
108

  void InitOnce() {
    if (!init_done_) {
      const vpx_codec_err_t res = vpx_codec_dec_init(&decoder_,
                                                     CodecInterface(),
                                                     &cfg_, 0);
      ASSERT_EQ(VPX_CODEC_OK, res) << DecodeError();
      init_done_ = true;
    }
109
110
  }

111
112
113
  vpx_codec_ctx_t     decoder_;
  vpx_codec_dec_cfg_t cfg_;
  unsigned int        deadline_;
114
  bool                init_done_;
115
116
};

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

123
124
125
126
  // Hook to be called before decompressing every frame.
  virtual void PreDecodeFrameHook(const CompressedVideoSource& video,
                                  Decoder *decoder) {}

127
128
129
130
131
132
133
134
  // Hook to be called to handle decode result. Return true to continue.
  virtual bool HandleDecodeResult(const vpx_codec_err_t res_dec,
                                  const CompressedVideoSource& /* video */,
                                  Decoder *decoder) {
    EXPECT_EQ(VPX_CODEC_OK, res_dec) << decoder->DecodeError();
    return VPX_CODEC_OK == res_dec;
  }

135
136
137
138
139
  // Hook to be called on every decompressed frame.
  virtual void DecompressedFrameHook(const vpx_image_t& img,
                                     const unsigned int frame_number) {}

 protected:
140
  explicit DecoderTest(const CodecFactory *codec) : codec_(codec) {}
141
142

  virtual ~DecoderTest() {}
143
144

  const CodecFactory *codec_;
145
146
};

147
}  // namespace libvpx_test
148
149

#endif  // TEST_DECODE_TEST_DRIVER_H_