svc_test.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (c) 2018 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 "test/svc_test.h"
  11. namespace svc_test {
  12. void OnePassCbrSvc::SetSvcConfig(const int num_spatial_layer,
  13. const int num_temporal_layer) {
  14. SetConfig(num_temporal_layer);
  15. cfg_.ss_number_layers = num_spatial_layer;
  16. cfg_.ts_number_layers = num_temporal_layer;
  17. if (num_spatial_layer == 1) {
  18. svc_params_.scaling_factor_num[0] = 288;
  19. svc_params_.scaling_factor_den[0] = 288;
  20. } else if (num_spatial_layer == 2) {
  21. svc_params_.scaling_factor_num[0] = 144;
  22. svc_params_.scaling_factor_den[0] = 288;
  23. svc_params_.scaling_factor_num[1] = 288;
  24. svc_params_.scaling_factor_den[1] = 288;
  25. } else if (num_spatial_layer == 3) {
  26. svc_params_.scaling_factor_num[0] = 72;
  27. svc_params_.scaling_factor_den[0] = 288;
  28. svc_params_.scaling_factor_num[1] = 144;
  29. svc_params_.scaling_factor_den[1] = 288;
  30. svc_params_.scaling_factor_num[2] = 288;
  31. svc_params_.scaling_factor_den[2] = 288;
  32. }
  33. number_spatial_layers_ = cfg_.ss_number_layers;
  34. number_temporal_layers_ = cfg_.ts_number_layers;
  35. }
  36. void OnePassCbrSvc::PreEncodeFrameHookSetup(::libvpx_test::VideoSource *video,
  37. ::libvpx_test::Encoder *encoder) {
  38. if (video->frame() == 0) {
  39. for (int i = 0; i < VPX_MAX_LAYERS; ++i) {
  40. svc_params_.max_quantizers[i] = 63;
  41. svc_params_.min_quantizers[i] = 0;
  42. }
  43. svc_params_.speed_per_layer[0] = base_speed_setting_;
  44. for (int i = 1; i < VPX_SS_MAX_LAYERS; ++i) {
  45. svc_params_.speed_per_layer[i] = speed_setting_;
  46. }
  47. encoder->Control(VP9E_SET_SVC, 1);
  48. encoder->Control(VP9E_SET_SVC_PARAMETERS, &svc_params_);
  49. encoder->Control(VP8E_SET_CPUUSED, speed_setting_);
  50. encoder->Control(VP9E_SET_AQ_MODE, 3);
  51. encoder->Control(VP8E_SET_MAX_INTRA_BITRATE_PCT, 300);
  52. encoder->Control(VP9E_SET_TILE_COLUMNS, get_msb(cfg_.g_threads));
  53. encoder->Control(VP9E_SET_ROW_MT, 1);
  54. encoder->Control(VP8E_SET_STATIC_THRESHOLD, 1);
  55. }
  56. superframe_count_++;
  57. temporal_layer_id_ = 0;
  58. if (number_temporal_layers_ == 2) {
  59. temporal_layer_id_ = (superframe_count_ % 2 != 0);
  60. } else if (number_temporal_layers_ == 3) {
  61. if (superframe_count_ % 2 != 0) temporal_layer_id_ = 2;
  62. if (superframe_count_ > 1) {
  63. if ((superframe_count_ - 2) % 4 == 0) temporal_layer_id_ = 1;
  64. }
  65. }
  66. frame_flags_ = 0;
  67. }
  68. void OnePassCbrSvc::PostEncodeFrameHook(::libvpx_test::Encoder *encoder) {
  69. vpx_svc_layer_id_t layer_id;
  70. encoder->Control(VP9E_GET_SVC_LAYER_ID, &layer_id);
  71. temporal_layer_id_ = layer_id.temporal_layer_id;
  72. for (int sl = 0; sl < number_spatial_layers_; ++sl) {
  73. for (int tl = temporal_layer_id_; tl < number_temporal_layers_; ++tl) {
  74. const int layer = sl * number_temporal_layers_ + tl;
  75. bits_in_buffer_model_[layer] +=
  76. static_cast<int64_t>(layer_target_avg_bandwidth_[layer]);
  77. }
  78. }
  79. }
  80. void OnePassCbrSvc::AssignLayerBitrates() {
  81. int sl, spatial_layer_target;
  82. int spatial_layers = cfg_.ss_number_layers;
  83. int temporal_layers = cfg_.ts_number_layers;
  84. float total = 0;
  85. float alloc_ratio[VPX_MAX_LAYERS] = { 0 };
  86. float framerate = 30.0;
  87. for (sl = 0; sl < spatial_layers; ++sl) {
  88. if (svc_params_.scaling_factor_den[sl] > 0) {
  89. alloc_ratio[sl] =
  90. static_cast<float>((svc_params_.scaling_factor_num[sl] * 1.0 /
  91. svc_params_.scaling_factor_den[sl]));
  92. total += alloc_ratio[sl];
  93. }
  94. }
  95. for (sl = 0; sl < spatial_layers; ++sl) {
  96. cfg_.ss_target_bitrate[sl] = spatial_layer_target =
  97. static_cast<unsigned int>(cfg_.rc_target_bitrate * alloc_ratio[sl] /
  98. total);
  99. const int index = sl * temporal_layers;
  100. if (cfg_.temporal_layering_mode == 3) {
  101. cfg_.layer_target_bitrate[index] = spatial_layer_target >> 1;
  102. cfg_.layer_target_bitrate[index + 1] =
  103. (spatial_layer_target >> 1) + (spatial_layer_target >> 2);
  104. cfg_.layer_target_bitrate[index + 2] = spatial_layer_target;
  105. } else if (cfg_.temporal_layering_mode == 2) {
  106. cfg_.layer_target_bitrate[index] = spatial_layer_target * 2 / 3;
  107. cfg_.layer_target_bitrate[index + 1] = spatial_layer_target;
  108. } else if (cfg_.temporal_layering_mode <= 1) {
  109. cfg_.layer_target_bitrate[index] = spatial_layer_target;
  110. }
  111. }
  112. for (sl = 0; sl < spatial_layers; ++sl) {
  113. for (int tl = 0; tl < temporal_layers; ++tl) {
  114. const int layer = sl * temporal_layers + tl;
  115. float layer_framerate = framerate;
  116. if (temporal_layers == 2 && tl == 0) layer_framerate = framerate / 2;
  117. if (temporal_layers == 3 && tl == 0) layer_framerate = framerate / 4;
  118. if (temporal_layers == 3 && tl == 1) layer_framerate = framerate / 2;
  119. layer_target_avg_bandwidth_[layer] = static_cast<int>(
  120. cfg_.layer_target_bitrate[layer] * 1000.0 / layer_framerate);
  121. bits_in_buffer_model_[layer] =
  122. cfg_.layer_target_bitrate[layer] * cfg_.rc_buf_initial_sz;
  123. }
  124. }
  125. }
  126. } // namespace svc_test