decode_test_driver.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. }
  50. } else {
  51. /* The Vp9 implementation of PeekStream returns an error only if the
  52. * data passed to it isn't a valid Vp9 chunk. */
  53. ASSERT_EQ(VPX_CODEC_OK, res_peek)
  54. << "Peek return failed: " << vpx_codec_err_to_string(res_peek);
  55. }
  56. }
  57. void DecoderTest::RunLoop(CompressedVideoSource *video,
  58. const vpx_codec_dec_cfg_t &dec_cfg) {
  59. Decoder *const decoder = codec_->CreateDecoder(dec_cfg, flags_);
  60. ASSERT_TRUE(decoder != NULL);
  61. bool end_of_file = false;
  62. // Decode frames.
  63. for (video->Begin(); !::testing::Test::HasFailure() && !end_of_file;
  64. video->Next()) {
  65. PreDecodeFrameHook(*video, decoder);
  66. vpx_codec_stream_info_t stream_info;
  67. stream_info.sz = sizeof(stream_info);
  68. if (video->cxdata() != NULL) {
  69. const vpx_codec_err_t res_peek = decoder->PeekStream(
  70. video->cxdata(), video->frame_size(), &stream_info);
  71. HandlePeekResult(decoder, video, res_peek);
  72. ASSERT_FALSE(::testing::Test::HasFailure());
  73. vpx_codec_err_t res_dec =
  74. decoder->DecodeFrame(video->cxdata(), video->frame_size());
  75. if (!HandleDecodeResult(res_dec, *video, decoder)) break;
  76. } else {
  77. // Signal end of the file to the decoder.
  78. const vpx_codec_err_t res_dec = decoder->DecodeFrame(NULL, 0);
  79. ASSERT_EQ(VPX_CODEC_OK, res_dec) << decoder->DecodeError();
  80. end_of_file = true;
  81. }
  82. DxDataIterator dec_iter = decoder->GetDxData();
  83. const vpx_image_t *img = NULL;
  84. // Get decompressed data
  85. while (!::testing::Test::HasFailure() && (img = dec_iter.Next())) {
  86. DecompressedFrameHook(*img, video->frame_number());
  87. }
  88. }
  89. delete decoder;
  90. }
  91. void DecoderTest::RunLoop(CompressedVideoSource *video) {
  92. vpx_codec_dec_cfg_t dec_cfg = vpx_codec_dec_cfg_t();
  93. RunLoop(video, dec_cfg);
  94. }
  95. void DecoderTest::set_cfg(const vpx_codec_dec_cfg_t &dec_cfg) {
  96. memcpy(&cfg_, &dec_cfg, sizeof(cfg_));
  97. }
  98. void DecoderTest::set_flags(const vpx_codec_flags_t flags) { flags_ = flags; }
  99. } // namespace libvpx_test