decode_test_driver.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. #include "third_party/googletest/src/include/gtest/gtest.h"
  11. #include "test/codec_factory.h"
  12. #include "test/decode_test_driver.h"
  13. #include "test/register_state_check.h"
  14. #include "test/video_source.h"
  15. namespace libvpx_test {
  16. const char kVP8Name[] = "WebM Project VP8";
  17. vpx_codec_err_t Decoder::PeekStream(const uint8_t *cxdata, size_t size,
  18. vpx_codec_stream_info_t *stream_info) {
  19. return vpx_codec_peek_stream_info(
  20. CodecInterface(), cxdata, static_cast<unsigned int>(size), stream_info);
  21. }
  22. vpx_codec_err_t Decoder::DecodeFrame(const uint8_t *cxdata, size_t size) {
  23. return DecodeFrame(cxdata, size, NULL);
  24. }
  25. vpx_codec_err_t Decoder::DecodeFrame(const uint8_t *cxdata, size_t size,
  26. void *user_priv) {
  27. vpx_codec_err_t res_dec;
  28. InitOnce();
  29. API_REGISTER_STATE_CHECK(
  30. res_dec = vpx_codec_decode(
  31. &decoder_, cxdata, static_cast<unsigned int>(size), user_priv, 0));
  32. return res_dec;
  33. }
  34. bool Decoder::IsVP8() const {
  35. const char *codec_name = GetDecoderName();
  36. return strncmp(kVP8Name, codec_name, sizeof(kVP8Name) - 1) == 0;
  37. }
  38. void DecoderTest::HandlePeekResult(Decoder *const decoder,
  39. CompressedVideoSource *video,
  40. const vpx_codec_err_t res_peek) {
  41. const bool is_vp8 = decoder->IsVP8();
  42. if (is_vp8) {
  43. /* Vp8's implementation of PeekStream returns an error if the frame you
  44. * pass it is not a keyframe, so we only expect VPX_CODEC_OK on the first
  45. * frame, which must be a keyframe. */
  46. if (video->frame_number() == 0)
  47. ASSERT_EQ(VPX_CODEC_OK, res_peek)
  48. << "Peek return failed: " << vpx_codec_err_to_string(res_peek);
  49. } else {
  50. /* The Vp9 implementation of PeekStream returns an error only if the
  51. * data passed to it isn't a valid Vp9 chunk. */
  52. ASSERT_EQ(VPX_CODEC_OK, res_peek)
  53. << "Peek return failed: " << vpx_codec_err_to_string(res_peek);
  54. }
  55. }
  56. void DecoderTest::RunLoop(CompressedVideoSource *video,
  57. const vpx_codec_dec_cfg_t &dec_cfg) {
  58. Decoder *const decoder = codec_->CreateDecoder(dec_cfg, flags_);
  59. ASSERT_TRUE(decoder != NULL);
  60. bool end_of_file = false;
  61. // Decode frames.
  62. for (video->Begin(); !::testing::Test::HasFailure() && !end_of_file;
  63. video->Next()) {
  64. PreDecodeFrameHook(*video, decoder);
  65. vpx_codec_stream_info_t stream_info;
  66. stream_info.sz = sizeof(stream_info);
  67. if (video->cxdata() != NULL) {
  68. const vpx_codec_err_t res_peek = decoder->PeekStream(
  69. video->cxdata(), video->frame_size(), &stream_info);
  70. HandlePeekResult(decoder, video, res_peek);
  71. ASSERT_FALSE(::testing::Test::HasFailure());
  72. vpx_codec_err_t res_dec =
  73. decoder->DecodeFrame(video->cxdata(), video->frame_size());
  74. if (!HandleDecodeResult(res_dec, *video, decoder)) break;
  75. } else {
  76. // Signal end of the file to the decoder.
  77. const vpx_codec_err_t res_dec = decoder->DecodeFrame(NULL, 0);
  78. ASSERT_EQ(VPX_CODEC_OK, res_dec) << decoder->DecodeError();
  79. end_of_file = true;
  80. }
  81. DxDataIterator dec_iter = decoder->GetDxData();
  82. const vpx_image_t *img = NULL;
  83. // Get decompressed data
  84. while ((img = dec_iter.Next())) {
  85. DecompressedFrameHook(*img, video->frame_number());
  86. }
  87. }
  88. delete decoder;
  89. }
  90. void DecoderTest::RunLoop(CompressedVideoSource *video) {
  91. vpx_codec_dec_cfg_t dec_cfg = vpx_codec_dec_cfg_t();
  92. RunLoop(video, dec_cfg);
  93. }
  94. void DecoderTest::set_cfg(const vpx_codec_dec_cfg_t &dec_cfg) {
  95. memcpy(&cfg_, &dec_cfg, sizeof(cfg_));
  96. }
  97. void DecoderTest::set_flags(const vpx_codec_flags_t flags) { flags_ = flags; }
  98. } // namespace libvpx_test