decode_api_test.cc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Copyright (c) 2014 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 "./vpx_config.h"
  12. #include "test/ivf_video_source.h"
  13. #include "vpx/vp8dx.h"
  14. #include "vpx/vpx_decoder.h"
  15. namespace {
  16. #define NELEMENTS(x) static_cast<int>(sizeof(x) / sizeof(x[0]))
  17. TEST(DecodeAPI, InvalidParams) {
  18. static const vpx_codec_iface_t *kCodecs[] = {
  19. #if CONFIG_VP8_DECODER
  20. &vpx_codec_vp8_dx_algo,
  21. #endif
  22. #if CONFIG_VP9_DECODER
  23. &vpx_codec_vp9_dx_algo,
  24. #endif
  25. };
  26. uint8_t buf[1] = { 0 };
  27. vpx_codec_ctx_t dec;
  28. EXPECT_EQ(VPX_CODEC_INVALID_PARAM, vpx_codec_dec_init(NULL, NULL, NULL, 0));
  29. EXPECT_EQ(VPX_CODEC_INVALID_PARAM, vpx_codec_dec_init(&dec, NULL, NULL, 0));
  30. EXPECT_EQ(VPX_CODEC_INVALID_PARAM, vpx_codec_decode(NULL, NULL, 0, NULL, 0));
  31. EXPECT_EQ(VPX_CODEC_INVALID_PARAM, vpx_codec_decode(NULL, buf, 0, NULL, 0));
  32. EXPECT_EQ(VPX_CODEC_INVALID_PARAM,
  33. vpx_codec_decode(NULL, buf, NELEMENTS(buf), NULL, 0));
  34. EXPECT_EQ(VPX_CODEC_INVALID_PARAM,
  35. vpx_codec_decode(NULL, NULL, NELEMENTS(buf), NULL, 0));
  36. EXPECT_EQ(VPX_CODEC_INVALID_PARAM, vpx_codec_destroy(NULL));
  37. EXPECT_TRUE(vpx_codec_error(NULL) != NULL);
  38. for (int i = 0; i < NELEMENTS(kCodecs); ++i) {
  39. EXPECT_EQ(VPX_CODEC_INVALID_PARAM,
  40. vpx_codec_dec_init(NULL, kCodecs[i], NULL, 0));
  41. EXPECT_EQ(VPX_CODEC_OK, vpx_codec_dec_init(&dec, kCodecs[i], NULL, 0));
  42. EXPECT_EQ(VPX_CODEC_UNSUP_BITSTREAM,
  43. vpx_codec_decode(&dec, buf, NELEMENTS(buf), NULL, 0));
  44. EXPECT_EQ(VPX_CODEC_INVALID_PARAM,
  45. vpx_codec_decode(&dec, NULL, NELEMENTS(buf), NULL, 0));
  46. EXPECT_EQ(VPX_CODEC_INVALID_PARAM, vpx_codec_decode(&dec, buf, 0, NULL, 0));
  47. EXPECT_EQ(VPX_CODEC_OK, vpx_codec_destroy(&dec));
  48. }
  49. }
  50. #if CONFIG_VP8_DECODER
  51. TEST(DecodeAPI, OptionalParams) {
  52. vpx_codec_ctx_t dec;
  53. #if CONFIG_ERROR_CONCEALMENT
  54. EXPECT_EQ(VPX_CODEC_OK, vpx_codec_dec_init(&dec, &vpx_codec_vp8_dx_algo, NULL,
  55. VPX_CODEC_USE_ERROR_CONCEALMENT));
  56. #else
  57. EXPECT_EQ(VPX_CODEC_INCAPABLE,
  58. vpx_codec_dec_init(&dec, &vpx_codec_vp8_dx_algo, NULL,
  59. VPX_CODEC_USE_ERROR_CONCEALMENT));
  60. #endif // CONFIG_ERROR_CONCEALMENT
  61. }
  62. #endif // CONFIG_VP8_DECODER
  63. #if CONFIG_VP9_DECODER
  64. // Test VP9 codec controls after a decode error to ensure the code doesn't
  65. // misbehave.
  66. void TestVp9Controls(vpx_codec_ctx_t *dec) {
  67. static const int kControls[] = { VP8D_GET_LAST_REF_UPDATES,
  68. VP8D_GET_FRAME_CORRUPTED,
  69. VP9D_GET_DISPLAY_SIZE, VP9D_GET_FRAME_SIZE };
  70. int val[2];
  71. for (int i = 0; i < NELEMENTS(kControls); ++i) {
  72. const vpx_codec_err_t res = vpx_codec_control_(dec, kControls[i], val);
  73. switch (kControls[i]) {
  74. case VP8D_GET_FRAME_CORRUPTED:
  75. EXPECT_EQ(VPX_CODEC_ERROR, res) << kControls[i];
  76. break;
  77. default: EXPECT_EQ(VPX_CODEC_OK, res) << kControls[i]; break;
  78. }
  79. EXPECT_EQ(VPX_CODEC_INVALID_PARAM,
  80. vpx_codec_control_(dec, kControls[i], NULL));
  81. }
  82. vp9_ref_frame_t ref;
  83. ref.idx = 0;
  84. EXPECT_EQ(VPX_CODEC_ERROR, vpx_codec_control(dec, VP9_GET_REFERENCE, &ref));
  85. EXPECT_EQ(VPX_CODEC_INVALID_PARAM,
  86. vpx_codec_control(dec, VP9_GET_REFERENCE, NULL));
  87. vpx_ref_frame_t ref_copy;
  88. const int width = 352;
  89. const int height = 288;
  90. ASSERT_TRUE(
  91. vpx_img_alloc(&ref_copy.img, VPX_IMG_FMT_I420, width, height, 1) != NULL);
  92. ref_copy.frame_type = VP8_LAST_FRAME;
  93. EXPECT_EQ(VPX_CODEC_ERROR,
  94. vpx_codec_control(dec, VP8_COPY_REFERENCE, &ref_copy));
  95. EXPECT_EQ(VPX_CODEC_INVALID_PARAM,
  96. vpx_codec_control(dec, VP8_COPY_REFERENCE, NULL));
  97. vpx_img_free(&ref_copy.img);
  98. }
  99. TEST(DecodeAPI, Vp9InvalidDecode) {
  100. const vpx_codec_iface_t *const codec = &vpx_codec_vp9_dx_algo;
  101. const char filename[] =
  102. "invalid-vp90-2-00-quantizer-00.webm.ivf.s5861_r01-05_b6-.v2.ivf";
  103. libvpx_test::IVFVideoSource video(filename);
  104. video.Init();
  105. video.Begin();
  106. ASSERT_TRUE(!HasFailure());
  107. vpx_codec_ctx_t dec;
  108. EXPECT_EQ(VPX_CODEC_OK, vpx_codec_dec_init(&dec, codec, NULL, 0));
  109. const uint32_t frame_size = static_cast<uint32_t>(video.frame_size());
  110. #if CONFIG_VP9_HIGHBITDEPTH
  111. EXPECT_EQ(VPX_CODEC_MEM_ERROR,
  112. vpx_codec_decode(&dec, video.cxdata(), frame_size, NULL, 0));
  113. #else
  114. EXPECT_EQ(VPX_CODEC_UNSUP_BITSTREAM,
  115. vpx_codec_decode(&dec, video.cxdata(), frame_size, NULL, 0));
  116. #endif
  117. vpx_codec_iter_t iter = NULL;
  118. EXPECT_EQ(NULL, vpx_codec_get_frame(&dec, &iter));
  119. TestVp9Controls(&dec);
  120. EXPECT_EQ(VPX_CODEC_OK, vpx_codec_destroy(&dec));
  121. }
  122. void TestPeekInfo(const uint8_t *const data, uint32_t data_sz,
  123. uint32_t peek_size) {
  124. const vpx_codec_iface_t *const codec = &vpx_codec_vp9_dx_algo;
  125. // Verify behavior of vpx_codec_decode. vpx_codec_decode doesn't even get
  126. // to decoder_peek_si_internal on frames of size < 8.
  127. if (data_sz >= 8) {
  128. vpx_codec_ctx_t dec;
  129. EXPECT_EQ(VPX_CODEC_OK, vpx_codec_dec_init(&dec, codec, NULL, 0));
  130. EXPECT_EQ((data_sz < peek_size) ? VPX_CODEC_UNSUP_BITSTREAM
  131. : VPX_CODEC_CORRUPT_FRAME,
  132. vpx_codec_decode(&dec, data, data_sz, NULL, 0));
  133. vpx_codec_iter_t iter = NULL;
  134. EXPECT_EQ(NULL, vpx_codec_get_frame(&dec, &iter));
  135. EXPECT_EQ(VPX_CODEC_OK, vpx_codec_destroy(&dec));
  136. }
  137. // Verify behavior of vpx_codec_peek_stream_info.
  138. vpx_codec_stream_info_t si;
  139. si.sz = sizeof(si);
  140. EXPECT_EQ((data_sz < peek_size) ? VPX_CODEC_UNSUP_BITSTREAM : VPX_CODEC_OK,
  141. vpx_codec_peek_stream_info(codec, data, data_sz, &si));
  142. }
  143. TEST(DecodeAPI, Vp9PeekStreamInfo) {
  144. // The first 9 bytes are valid and the rest of the bytes are made up. Until
  145. // size 10, this should return VPX_CODEC_UNSUP_BITSTREAM and after that it
  146. // should return VPX_CODEC_CORRUPT_FRAME.
  147. const uint8_t data[32] = {
  148. 0x85, 0xa4, 0xc1, 0xa1, 0x38, 0x81, 0xa3, 0x49, 0x83, 0xff, 0xff,
  149. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  150. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  151. };
  152. for (uint32_t data_sz = 1; data_sz <= 32; ++data_sz) {
  153. TestPeekInfo(data, data_sz, 10);
  154. }
  155. }
  156. TEST(DecodeAPI, Vp9PeekStreamInfoTruncated) {
  157. // This profile 1 header requires 10.25 bytes, ensure
  158. // vpx_codec_peek_stream_info doesn't over read.
  159. const uint8_t profile1_data[10] = { 0xa4, 0xe9, 0x30, 0x68, 0x53,
  160. 0xe9, 0x30, 0x68, 0x53, 0x04 };
  161. for (uint32_t data_sz = 1; data_sz <= 10; ++data_sz) {
  162. TestPeekInfo(profile1_data, data_sz, 11);
  163. }
  164. }
  165. #endif // CONFIG_VP9_DECODER
  166. TEST(DecodeAPI, HighBitDepthCapability) {
  167. // VP8 should not claim VP9 HBD as a capability.
  168. #if CONFIG_VP8_DECODER
  169. const vpx_codec_caps_t vp8_caps = vpx_codec_get_caps(&vpx_codec_vp8_dx_algo);
  170. EXPECT_EQ(vp8_caps & VPX_CODEC_CAP_HIGHBITDEPTH, 0);
  171. #endif
  172. #if CONFIG_VP9_DECODER
  173. const vpx_codec_caps_t vp9_caps = vpx_codec_get_caps(&vpx_codec_vp9_dx_algo);
  174. #if CONFIG_VP9_HIGHBITDEPTH
  175. EXPECT_EQ(vp9_caps & VPX_CODEC_CAP_HIGHBITDEPTH, VPX_CODEC_CAP_HIGHBITDEPTH);
  176. #else
  177. EXPECT_EQ(vp9_caps & VPX_CODEC_CAP_HIGHBITDEPTH, 0);
  178. #endif
  179. #endif
  180. }
  181. } // namespace