decode_test_driver.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (c) 2012 The WebM project authors. All Rights Reserved.
  3. *
  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.
  9. */
  10. #ifndef VPX_TEST_DECODE_TEST_DRIVER_H_
  11. #define VPX_TEST_DECODE_TEST_DRIVER_H_
  12. #include <cstring>
  13. #include "third_party/googletest/src/include/gtest/gtest.h"
  14. #include "./vpx_config.h"
  15. #include "vpx/vpx_decoder.h"
  16. namespace libvpx_test {
  17. class CodecFactory;
  18. class CompressedVideoSource;
  19. // Provides an object to handle decoding output
  20. class DxDataIterator {
  21. public:
  22. explicit DxDataIterator(vpx_codec_ctx_t *decoder)
  23. : decoder_(decoder), iter_(NULL) {}
  24. const vpx_image_t *Next() { return vpx_codec_get_frame(decoder_, &iter_); }
  25. private:
  26. vpx_codec_ctx_t *decoder_;
  27. vpx_codec_iter_t iter_;
  28. };
  29. // Provides a simplified interface to manage one video decoding.
  30. // Similar to Encoder class, the exact services should be added
  31. // as more tests are added.
  32. class Decoder {
  33. public:
  34. explicit Decoder(vpx_codec_dec_cfg_t cfg)
  35. : cfg_(cfg), flags_(0), init_done_(false) {
  36. memset(&decoder_, 0, sizeof(decoder_));
  37. }
  38. Decoder(vpx_codec_dec_cfg_t cfg, const vpx_codec_flags_t flag)
  39. : cfg_(cfg), flags_(flag), init_done_(false) {
  40. memset(&decoder_, 0, sizeof(decoder_));
  41. }
  42. virtual ~Decoder() { vpx_codec_destroy(&decoder_); }
  43. vpx_codec_err_t PeekStream(const uint8_t *cxdata, size_t size,
  44. vpx_codec_stream_info_t *stream_info);
  45. vpx_codec_err_t DecodeFrame(const uint8_t *cxdata, size_t size);
  46. vpx_codec_err_t DecodeFrame(const uint8_t *cxdata, size_t size,
  47. void *user_priv);
  48. DxDataIterator GetDxData() { return DxDataIterator(&decoder_); }
  49. void Control(int ctrl_id, int arg) { Control(ctrl_id, arg, VPX_CODEC_OK); }
  50. void Control(int ctrl_id, const void *arg) {
  51. InitOnce();
  52. const vpx_codec_err_t res = vpx_codec_control_(&decoder_, ctrl_id, arg);
  53. ASSERT_EQ(VPX_CODEC_OK, res) << DecodeError();
  54. }
  55. void Control(int ctrl_id, int arg, vpx_codec_err_t expected_value) {
  56. InitOnce();
  57. const vpx_codec_err_t res = vpx_codec_control_(&decoder_, ctrl_id, arg);
  58. ASSERT_EQ(expected_value, res) << DecodeError();
  59. }
  60. const char *DecodeError() {
  61. const char *detail = vpx_codec_error_detail(&decoder_);
  62. return detail ? detail : vpx_codec_error(&decoder_);
  63. }
  64. // Passes the external frame buffer information to libvpx.
  65. vpx_codec_err_t SetFrameBufferFunctions(
  66. vpx_get_frame_buffer_cb_fn_t cb_get,
  67. vpx_release_frame_buffer_cb_fn_t cb_release, void *user_priv) {
  68. InitOnce();
  69. return vpx_codec_set_frame_buffer_functions(&decoder_, cb_get, cb_release,
  70. user_priv);
  71. }
  72. const char *GetDecoderName() const {
  73. return vpx_codec_iface_name(CodecInterface());
  74. }
  75. bool IsVP8() const;
  76. vpx_codec_ctx_t *GetDecoder() { return &decoder_; }
  77. protected:
  78. virtual vpx_codec_iface_t *CodecInterface() const = 0;
  79. void InitOnce() {
  80. if (!init_done_) {
  81. const vpx_codec_err_t res =
  82. vpx_codec_dec_init(&decoder_, CodecInterface(), &cfg_, flags_);
  83. ASSERT_EQ(VPX_CODEC_OK, res) << DecodeError();
  84. init_done_ = true;
  85. }
  86. }
  87. vpx_codec_ctx_t decoder_;
  88. vpx_codec_dec_cfg_t cfg_;
  89. vpx_codec_flags_t flags_;
  90. bool init_done_;
  91. };
  92. // Common test functionality for all Decoder tests.
  93. class DecoderTest {
  94. public:
  95. // Main decoding loop
  96. virtual void RunLoop(CompressedVideoSource *video);
  97. virtual void RunLoop(CompressedVideoSource *video,
  98. const vpx_codec_dec_cfg_t &dec_cfg);
  99. virtual void set_cfg(const vpx_codec_dec_cfg_t &dec_cfg);
  100. virtual void set_flags(const vpx_codec_flags_t flags);
  101. // Hook to be called before decompressing every frame.
  102. virtual void PreDecodeFrameHook(const CompressedVideoSource & /*video*/,
  103. Decoder * /*decoder*/) {}
  104. // Hook to be called to handle decode result. Return true to continue.
  105. virtual bool HandleDecodeResult(const vpx_codec_err_t res_dec,
  106. const CompressedVideoSource & /*video*/,
  107. Decoder *decoder) {
  108. EXPECT_EQ(VPX_CODEC_OK, res_dec) << decoder->DecodeError();
  109. return VPX_CODEC_OK == res_dec;
  110. }
  111. // Hook to be called on every decompressed frame.
  112. virtual void DecompressedFrameHook(const vpx_image_t & /*img*/,
  113. const unsigned int /*frame_number*/) {}
  114. // Hook to be called on peek result
  115. virtual void HandlePeekResult(Decoder *const decoder,
  116. CompressedVideoSource *video,
  117. const vpx_codec_err_t res_peek);
  118. protected:
  119. explicit DecoderTest(const CodecFactory *codec)
  120. : codec_(codec), cfg_(), flags_(0) {}
  121. virtual ~DecoderTest() {}
  122. const CodecFactory *codec_;
  123. vpx_codec_dec_cfg_t cfg_;
  124. vpx_codec_flags_t flags_;
  125. };
  126. } // namespace libvpx_test
  127. #endif // VPX_TEST_DECODE_TEST_DRIVER_H_