2 * Copyright (c) 2012 The WebM project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
11 #ifndef TEST_DECODE_TEST_DRIVER_H_
12 #define TEST_DECODE_TEST_DRIVER_H_
14 #include "third_party/googletest/src/include/gtest/gtest.h"
15 #include "./vpx_config.h"
16 #include "vpx/vpx_decoder.h"
18 namespace libvpx_test {
21 class CompressedVideoSource;
23 // Provides an object to handle decoding output
24 class DxDataIterator {
26 explicit DxDataIterator(vpx_codec_ctx_t *decoder)
27 : decoder_(decoder), iter_(NULL) {}
29 const vpx_image_t *Next() { return vpx_codec_get_frame(decoder_, &iter_); }
32 vpx_codec_ctx_t *decoder_;
33 vpx_codec_iter_t iter_;
36 // Provides a simplified interface to manage one video decoding.
37 // Similar to Encoder class, the exact services should be added
38 // as more tests are added.
41 Decoder(vpx_codec_dec_cfg_t cfg, unsigned long deadline)
42 : cfg_(cfg), flags_(0), deadline_(deadline), init_done_(false) {
43 memset(&decoder_, 0, sizeof(decoder_));
46 Decoder(vpx_codec_dec_cfg_t cfg, const vpx_codec_flags_t flag,
47 unsigned long deadline) // NOLINT
52 memset(&decoder_, 0, sizeof(decoder_));
55 virtual ~Decoder() { vpx_codec_destroy(&decoder_); }
57 vpx_codec_err_t PeekStream(const uint8_t *cxdata, size_t size,
58 vpx_codec_stream_info_t *stream_info);
60 vpx_codec_err_t DecodeFrame(const uint8_t *cxdata, size_t size);
62 vpx_codec_err_t DecodeFrame(const uint8_t *cxdata, size_t size,
65 DxDataIterator GetDxData() { return DxDataIterator(&decoder_); }
67 void set_deadline(unsigned long deadline) { deadline_ = deadline; }
69 void Control(int ctrl_id, int arg) { Control(ctrl_id, arg, VPX_CODEC_OK); }
71 void Control(int ctrl_id, const void *arg) {
73 const vpx_codec_err_t res = vpx_codec_control_(&decoder_, ctrl_id, arg);
74 ASSERT_EQ(VPX_CODEC_OK, res) << DecodeError();
77 void Control(int ctrl_id, int arg, vpx_codec_err_t expected_value) {
79 const vpx_codec_err_t res = vpx_codec_control_(&decoder_, ctrl_id, arg);
80 ASSERT_EQ(expected_value, res) << DecodeError();
83 const char *DecodeError() {
84 const char *detail = vpx_codec_error_detail(&decoder_);
85 return detail ? detail : vpx_codec_error(&decoder_);
88 // Passes the external frame buffer information to libvpx.
89 vpx_codec_err_t SetFrameBufferFunctions(
90 vpx_get_frame_buffer_cb_fn_t cb_get,
91 vpx_release_frame_buffer_cb_fn_t cb_release, void *user_priv) {
93 return vpx_codec_set_frame_buffer_functions(&decoder_, cb_get, cb_release,
97 const char *GetDecoderName() const {
98 return vpx_codec_iface_name(CodecInterface());
103 vpx_codec_ctx_t *GetDecoder() { return &decoder_; }
106 virtual vpx_codec_iface_t *CodecInterface() const = 0;
110 const vpx_codec_err_t res =
111 vpx_codec_dec_init(&decoder_, CodecInterface(), &cfg_, flags_);
112 ASSERT_EQ(VPX_CODEC_OK, res) << DecodeError();
117 vpx_codec_ctx_t decoder_;
118 vpx_codec_dec_cfg_t cfg_;
119 vpx_codec_flags_t flags_;
120 unsigned int deadline_;
124 // Common test functionality for all Decoder tests.
127 // Main decoding loop
128 virtual void RunLoop(CompressedVideoSource *video);
129 virtual void RunLoop(CompressedVideoSource *video,
130 const vpx_codec_dec_cfg_t &dec_cfg);
132 virtual void set_cfg(const vpx_codec_dec_cfg_t &dec_cfg);
133 virtual void set_flags(const vpx_codec_flags_t flags);
135 // Hook to be called before decompressing every frame.
136 virtual void PreDecodeFrameHook(const CompressedVideoSource & /*video*/,
137 Decoder * /*decoder*/) {}
139 // Hook to be called to handle decode result. Return true to continue.
140 virtual bool HandleDecodeResult(const vpx_codec_err_t res_dec,
141 const CompressedVideoSource & /*video*/,
143 EXPECT_EQ(VPX_CODEC_OK, res_dec) << decoder->DecodeError();
144 return VPX_CODEC_OK == res_dec;
147 // Hook to be called on every decompressed frame.
148 virtual void DecompressedFrameHook(const vpx_image_t & /*img*/,
149 const unsigned int /*frame_number*/) {}
151 // Hook to be called on peek result
152 virtual void HandlePeekResult(Decoder *const decoder,
153 CompressedVideoSource *video,
154 const vpx_codec_err_t res_peek);
157 explicit DecoderTest(const CodecFactory *codec)
158 : codec_(codec), cfg_(), flags_(0) {}
160 virtual ~DecoderTest() {}
162 const CodecFactory *codec_;
163 vpx_codec_dec_cfg_t cfg_;
164 vpx_codec_flags_t flags_;
167 } // namespace libvpx_test
169 #endif // TEST_DECODE_TEST_DRIVER_H_