encode_api_test.cc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright (c) 2016 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 "vpx/vp8cx.h"
  13. #include "vpx/vpx_encoder.h"
  14. namespace {
  15. #define NELEMENTS(x) static_cast<int>(sizeof(x) / sizeof(x[0]))
  16. TEST(EncodeAPI, InvalidParams) {
  17. static const vpx_codec_iface_t *kCodecs[] = {
  18. #if CONFIG_VP8_ENCODER
  19. &vpx_codec_vp8_cx_algo,
  20. #endif
  21. #if CONFIG_VP9_ENCODER
  22. &vpx_codec_vp9_cx_algo,
  23. #endif
  24. };
  25. uint8_t buf[1] = { 0 };
  26. vpx_image_t img;
  27. vpx_codec_ctx_t enc;
  28. vpx_codec_enc_cfg_t cfg;
  29. EXPECT_EQ(&img, vpx_img_wrap(&img, VPX_IMG_FMT_I420, 1, 1, 1, buf));
  30. EXPECT_EQ(VPX_CODEC_INVALID_PARAM, vpx_codec_enc_init(NULL, NULL, NULL, 0));
  31. EXPECT_EQ(VPX_CODEC_INVALID_PARAM, vpx_codec_enc_init(&enc, NULL, NULL, 0));
  32. EXPECT_EQ(VPX_CODEC_INVALID_PARAM, vpx_codec_encode(NULL, NULL, 0, 0, 0, 0));
  33. EXPECT_EQ(VPX_CODEC_INVALID_PARAM, vpx_codec_encode(NULL, &img, 0, 0, 0, 0));
  34. EXPECT_EQ(VPX_CODEC_INVALID_PARAM, vpx_codec_destroy(NULL));
  35. EXPECT_EQ(VPX_CODEC_INVALID_PARAM,
  36. vpx_codec_enc_config_default(NULL, NULL, 0));
  37. EXPECT_EQ(VPX_CODEC_INVALID_PARAM,
  38. vpx_codec_enc_config_default(NULL, &cfg, 0));
  39. EXPECT_TRUE(vpx_codec_error(NULL) != NULL);
  40. for (int i = 0; i < NELEMENTS(kCodecs); ++i) {
  41. SCOPED_TRACE(vpx_codec_iface_name(kCodecs[i]));
  42. EXPECT_EQ(VPX_CODEC_INVALID_PARAM,
  43. vpx_codec_enc_init(NULL, kCodecs[i], NULL, 0));
  44. EXPECT_EQ(VPX_CODEC_INVALID_PARAM,
  45. vpx_codec_enc_init(&enc, kCodecs[i], NULL, 0));
  46. EXPECT_EQ(VPX_CODEC_INVALID_PARAM,
  47. vpx_codec_enc_config_default(kCodecs[i], &cfg, 1));
  48. EXPECT_EQ(VPX_CODEC_OK, vpx_codec_enc_config_default(kCodecs[i], &cfg, 0));
  49. EXPECT_EQ(VPX_CODEC_OK, vpx_codec_enc_init(&enc, kCodecs[i], &cfg, 0));
  50. EXPECT_EQ(VPX_CODEC_OK, vpx_codec_encode(&enc, NULL, 0, 0, 0, 0));
  51. EXPECT_EQ(VPX_CODEC_OK, vpx_codec_destroy(&enc));
  52. }
  53. }
  54. TEST(EncodeAPI, HighBitDepthCapability) {
  55. // VP8 should not claim VP9 HBD as a capability.
  56. #if CONFIG_VP8_ENCODER
  57. const vpx_codec_caps_t vp8_caps = vpx_codec_get_caps(&vpx_codec_vp8_cx_algo);
  58. EXPECT_EQ(vp8_caps & VPX_CODEC_CAP_HIGHBITDEPTH, 0);
  59. #endif
  60. #if CONFIG_VP9_ENCODER
  61. const vpx_codec_caps_t vp9_caps = vpx_codec_get_caps(&vpx_codec_vp9_cx_algo);
  62. #if CONFIG_VP9_HIGHBITDEPTH
  63. EXPECT_EQ(vp9_caps & VPX_CODEC_CAP_HIGHBITDEPTH, VPX_CODEC_CAP_HIGHBITDEPTH);
  64. #else
  65. EXPECT_EQ(vp9_caps & VPX_CODEC_CAP_HIGHBITDEPTH, 0);
  66. #endif
  67. #endif
  68. }
  69. #if CONFIG_VP8_ENCODER
  70. TEST(EncodeAPI, ImageSizeSetting) {
  71. const int width = 711;
  72. const int height = 360;
  73. const int bps = 12;
  74. vpx_image_t img;
  75. vpx_codec_ctx_t enc;
  76. vpx_codec_enc_cfg_t cfg;
  77. uint8_t *img_buf = reinterpret_cast<uint8_t *>(
  78. calloc(width * height * bps / 8, sizeof(*img_buf)));
  79. vpx_codec_enc_config_default(vpx_codec_vp8_cx(), &cfg, 0);
  80. cfg.g_w = width;
  81. cfg.g_h = height;
  82. vpx_img_wrap(&img, VPX_IMG_FMT_I420, width, height, 1, img_buf);
  83. vpx_codec_enc_init(&enc, vpx_codec_vp8_cx(), &cfg, 0);
  84. EXPECT_EQ(VPX_CODEC_OK, vpx_codec_encode(&enc, &img, 0, 1, 0, 0));
  85. free(img_buf);
  86. vpx_codec_destroy(&enc);
  87. }
  88. #endif
  89. // Set up 2 spatial streams with 2 temporal layers per stream, and generate
  90. // invalid configuration by setting the temporal layer rate allocation
  91. // (ts_target_bitrate[]) to 0 for both layers. This should fail independent of
  92. // CONFIG_MULTI_RES_ENCODING.
  93. TEST(EncodeAPI, MultiResEncode) {
  94. static const vpx_codec_iface_t *kCodecs[] = {
  95. #if CONFIG_VP8_ENCODER
  96. &vpx_codec_vp8_cx_algo,
  97. #endif
  98. #if CONFIG_VP9_ENCODER
  99. &vpx_codec_vp9_cx_algo,
  100. #endif
  101. };
  102. const int width = 1280;
  103. const int height = 720;
  104. const int width_down = width / 2;
  105. const int height_down = height / 2;
  106. const int target_bitrate = 1000;
  107. const int framerate = 30;
  108. for (int c = 0; c < NELEMENTS(kCodecs); ++c) {
  109. const vpx_codec_iface_t *const iface = kCodecs[c];
  110. vpx_codec_ctx_t enc[2];
  111. vpx_codec_enc_cfg_t cfg[2];
  112. vpx_rational_t dsf[2] = { { 2, 1 }, { 2, 1 } };
  113. memset(enc, 0, sizeof(enc));
  114. for (int i = 0; i < 2; i++) {
  115. vpx_codec_enc_config_default(iface, &cfg[i], 0);
  116. }
  117. /* Highest-resolution encoder settings */
  118. cfg[0].g_w = width;
  119. cfg[0].g_h = height;
  120. cfg[0].rc_dropframe_thresh = 0;
  121. cfg[0].rc_end_usage = VPX_CBR;
  122. cfg[0].rc_resize_allowed = 0;
  123. cfg[0].rc_min_quantizer = 2;
  124. cfg[0].rc_max_quantizer = 56;
  125. cfg[0].rc_undershoot_pct = 100;
  126. cfg[0].rc_overshoot_pct = 15;
  127. cfg[0].rc_buf_initial_sz = 500;
  128. cfg[0].rc_buf_optimal_sz = 600;
  129. cfg[0].rc_buf_sz = 1000;
  130. cfg[0].g_error_resilient = 1; /* Enable error resilient mode */
  131. cfg[0].g_lag_in_frames = 0;
  132. cfg[0].kf_mode = VPX_KF_AUTO;
  133. cfg[0].kf_min_dist = 3000;
  134. cfg[0].kf_max_dist = 3000;
  135. cfg[0].rc_target_bitrate = target_bitrate; /* Set target bitrate */
  136. cfg[0].g_timebase.num = 1; /* Set fps */
  137. cfg[0].g_timebase.den = framerate;
  138. memcpy(&cfg[1], &cfg[0], sizeof(cfg[0]));
  139. cfg[1].rc_target_bitrate = 500;
  140. cfg[1].g_w = width_down;
  141. cfg[1].g_h = height_down;
  142. for (int i = 0; i < 2; i++) {
  143. cfg[i].ts_number_layers = 2;
  144. cfg[i].ts_periodicity = 2;
  145. cfg[i].ts_rate_decimator[0] = 2;
  146. cfg[i].ts_rate_decimator[1] = 1;
  147. cfg[i].ts_layer_id[0] = 0;
  148. cfg[i].ts_layer_id[1] = 1;
  149. // Invalid parameters.
  150. cfg[i].ts_target_bitrate[0] = 0;
  151. cfg[i].ts_target_bitrate[1] = 0;
  152. }
  153. // VP9 should report incapable, VP8 invalid for all configurations.
  154. const char kVP9Name[] = "WebM Project VP9";
  155. const bool is_vp9 = strncmp(kVP9Name, vpx_codec_iface_name(iface),
  156. sizeof(kVP9Name) - 1) == 0;
  157. EXPECT_EQ(is_vp9 ? VPX_CODEC_INCAPABLE : VPX_CODEC_INVALID_PARAM,
  158. vpx_codec_enc_init_multi(&enc[0], iface, &cfg[0], 2, 0, &dsf[0]));
  159. for (int i = 0; i < 2; i++) {
  160. vpx_codec_destroy(&enc[i]);
  161. }
  162. }
  163. }
  164. } // namespace