Chromium Code Reviews| OLD | NEW | 
|---|---|
| 1 /* | 1 /* | 
| 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. | 
| 3 * | 3 * | 
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found | 
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may | 
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. | 
| 9 * | 9 * | 
| 10 */ | 10 */ | 
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 106 } | 106 } | 
| 107 if (raw_ != NULL) { | 107 if (raw_ != NULL) { | 
| 108 vpx_img_free(raw_); | 108 vpx_img_free(raw_); | 
| 109 raw_ = NULL; | 109 raw_ = NULL; | 
| 110 } | 110 } | 
| 111 inited_ = false; | 111 inited_ = false; | 
| 112 return WEBRTC_VIDEO_CODEC_OK; | 112 return WEBRTC_VIDEO_CODEC_OK; | 
| 113 } | 113 } | 
| 114 | 114 | 
| 115 bool VP9EncoderImpl::SetSvcRates() { | 115 bool VP9EncoderImpl::SetSvcRates() { | 
| 116 float rate_ratio[VPX_MAX_LAYERS] = {0}; | |
| 117 float total = 0; | |
| 118 uint8_t i = 0; | 116 uint8_t i = 0; | 
| 119 | 117 | 
| 120 for (i = 0; i < num_spatial_layers_; ++i) { | 118 // Check target_bitrate_bps to see if spatial layers were configured | 
| 121 if (svc_internal_.svc_params.scaling_factor_num[i] <= 0 || | 119 // manually. | 
| 122 svc_internal_.svc_params.scaling_factor_den[i] <= 0) { | 120 if (num_spatial_layers_ > 1 && | 
| 121 codec_.spatialLayers[0].target_bitrate_bps > 0) { | |
| 122 if (num_temporal_layers_ > 1) { | |
| 123 LOG(LS_ERROR) << "When specifying spatial layers manually, " | |
| 124 "multiple temporal layers are not supported!"; | |
| 123 return false; | 125 return false; | 
| 124 } | 126 } | 
| 125 rate_ratio[i] = static_cast<float>( | 127 int total_bitrate_bps = 0; | 
| 126 svc_internal_.svc_params.scaling_factor_num[i]) / | 128 for (i = 0; i < num_spatial_layers_; ++i) | 
| 127 svc_internal_.svc_params.scaling_factor_den[i]; | 129 total_bitrate_bps += codec_.spatialLayers[i].target_bitrate_bps; | 
| 128 total += rate_ratio[i]; | 130 // If total bitrate differs now from what has been specified at the | 
| 129 } | 131 // beginning, update the bitrates in the same ratio as before. | 
| 132 for (i = 0; i < num_spatial_layers_; ++i) { | |
| 133 config_->ss_target_bitrate[i] = | |
| 134 config_->layer_target_bitrate[i] = static_cast<int>( | |
| 135 static_cast<int64_t>(config_->rc_target_bitrate) * | |
| 136 codec_.spatialLayers[i].target_bitrate_bps / | |
| 137 total_bitrate_bps / 1000); | |
| 138 } | |
| 139 } else { | |
| 140 float rate_ratio[VPX_MAX_LAYERS] = {0}; | |
| 141 float total = 0; | |
| 130 | 142 | 
| 131 for (i = 0; i < num_spatial_layers_; ++i) { | 143 for (i = 0; i < num_spatial_layers_; ++i) { | 
| 132 config_->ss_target_bitrate[i] = static_cast<unsigned int>( | 144 if (svc_internal_.svc_params.scaling_factor_num[i] <= 0 || | 
| 133 config_->rc_target_bitrate * rate_ratio[i] / total); | 145 svc_internal_.svc_params.scaling_factor_den[i] <= 0) { | 
| 134 if (num_temporal_layers_ == 1) { | 146 LOG(LS_ERROR) << "Scaling factors not specified!"; | 
| 135 config_->layer_target_bitrate[i] = config_->ss_target_bitrate[i]; | 147 return false; | 
| 136 } else if (num_temporal_layers_ == 2) { | 148 } | 
| 137 config_->layer_target_bitrate[i * num_temporal_layers_] = | 149 rate_ratio[i] = static_cast<float>( | 
| 138 config_->ss_target_bitrate[i] * 2 / 3; | 150 svc_internal_.svc_params.scaling_factor_num[i]) / | 
| 139 config_->layer_target_bitrate[i * num_temporal_layers_ + 1] = | 151 svc_internal_.svc_params.scaling_factor_den[i]; | 
| 140 config_->ss_target_bitrate[i]; | 152 total += rate_ratio[i]; | 
| 141 } else if (num_temporal_layers_ == 3) { | 153 } | 
| 142 config_->layer_target_bitrate[i * num_temporal_layers_] = | 154 | 
| 143 config_->ss_target_bitrate[i] / 2; | 155 for (i = 0; i < num_spatial_layers_; ++i) { | 
| 144 config_->layer_target_bitrate[i * num_temporal_layers_ + 1] = | 156 config_->ss_target_bitrate[i] = static_cast<unsigned int>( | 
| 145 config_->layer_target_bitrate[i * num_temporal_layers_] + | 157 config_->rc_target_bitrate * rate_ratio[i] / total); | 
| 146 (config_->ss_target_bitrate[i] / 4); | 158 if (num_temporal_layers_ == 1) { | 
| 147 config_->layer_target_bitrate[i * num_temporal_layers_ + 2] = | 159 config_->layer_target_bitrate[i] = config_->ss_target_bitrate[i]; | 
| 148 config_->ss_target_bitrate[i]; | 160 } else if (num_temporal_layers_ == 2) { | 
| 149 } else { | 161 config_->layer_target_bitrate[i * num_temporal_layers_] = | 
| 150 return false; | 162 config_->ss_target_bitrate[i] * 2 / 3; | 
| 163 config_->layer_target_bitrate[i * num_temporal_layers_ + 1] = | |
| 164 config_->ss_target_bitrate[i]; | |
| 165 } else if (num_temporal_layers_ == 3) { | |
| 166 config_->layer_target_bitrate[i * num_temporal_layers_] = | |
| 167 config_->ss_target_bitrate[i] / 2; | |
| 168 config_->layer_target_bitrate[i * num_temporal_layers_ + 1] = | |
| 169 config_->layer_target_bitrate[i * num_temporal_layers_] + | |
| 170 (config_->ss_target_bitrate[i] / 4); | |
| 171 config_->layer_target_bitrate[i * num_temporal_layers_ + 2] = | |
| 172 config_->ss_target_bitrate[i]; | |
| 173 } else { | |
| 174 LOG(LS_ERROR) << "Unsupported number of temporal layers!"; | |
| 
 
sprang_webrtc
2015/09/29 14:19:33
LOG(LS_ERROR) << "Unsupported number of temporal l
 
ivica
2015/09/29 15:14:51
Done.
 
 | |
| 175 return false; | |
| 176 } | |
| 
 
sprang_webrtc
2015/09/29 14:19:33
Why not switch?
 
ivica
2015/09/29 15:14:51
This is not my code, should I change it?
 
 | |
| 151 } | 177 } | 
| 152 } | 178 } | 
| 153 | 179 | 
| 154 // For now, temporal layers only supported when having one spatial layer. | 180 // For now, temporal layers only supported when having one spatial layer. | 
| 155 if (num_spatial_layers_ == 1) { | 181 if (num_spatial_layers_ == 1) { | 
| 156 for (i = 0; i < num_temporal_layers_; ++i) { | 182 for (i = 0; i < num_temporal_layers_; ++i) { | 
| 157 config_->ts_target_bitrate[i] = config_->layer_target_bitrate[i]; | 183 config_->ts_target_bitrate[i] = config_->layer_target_bitrate[i]; | 
| 158 } | 184 } | 
| 159 } | 185 } | 
| 160 | 186 | 
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 348 return 2; | 374 return 2; | 
| 349 } else { | 375 } else { | 
| 350 // 1 thread less than VGA. | 376 // 1 thread less than VGA. | 
| 351 return 1; | 377 return 1; | 
| 352 } | 378 } | 
| 353 } | 379 } | 
| 354 | 380 | 
| 355 int VP9EncoderImpl::InitAndSetControlSettings(const VideoCodec* inst) { | 381 int VP9EncoderImpl::InitAndSetControlSettings(const VideoCodec* inst) { | 
| 356 config_->ss_number_layers = num_spatial_layers_; | 382 config_->ss_number_layers = num_spatial_layers_; | 
| 357 | 383 | 
| 358 int scaling_factor_num = 256; | 384 // Check target_bitrate_bps to see whether spatial layers were configured | 
| 359 for (int i = num_spatial_layers_ - 1; i >= 0; --i) { | 385 // manually. | 
| 360 svc_internal_.svc_params.max_quantizers[i] = config_->rc_max_quantizer; | 386 if (num_spatial_layers_ > 1 && | 
| 361 svc_internal_.svc_params.min_quantizers[i] = config_->rc_min_quantizer; | 387 codec_.spatialLayers[0].target_bitrate_bps > 0) { | 
| 362 // 1:2 scaling in each dimension. | 388 for (int i = 0; i < num_spatial_layers_; ++i) { | 
| 363 svc_internal_.svc_params.scaling_factor_num[i] = scaling_factor_num; | 389 const auto &layer = codec_.spatialLayers[i]; | 
| 364 svc_internal_.svc_params.scaling_factor_den[i] = 256; | 390 svc_internal_.svc_params.max_quantizers[i] = config_->rc_max_quantizer; | 
| 365 scaling_factor_num /= 2; | 391 svc_internal_.svc_params.min_quantizers[i] = config_->rc_min_quantizer; | 
| 392 svc_internal_.svc_params.scaling_factor_num[i] = layer.scaling_factor_num; | |
| 393 svc_internal_.svc_params.scaling_factor_den[i] = layer.scaling_factor_den; | |
| 394 } | |
| 395 } else { | |
| 396 int scaling_factor_num = 256; | |
| 397 for (int i = num_spatial_layers_ - 1; i >= 0; --i) { | |
| 398 svc_internal_.svc_params.max_quantizers[i] = config_->rc_max_quantizer; | |
| 399 svc_internal_.svc_params.min_quantizers[i] = config_->rc_min_quantizer; | |
| 400 // 1:2 scaling in each dimension. | |
| 401 svc_internal_.svc_params.scaling_factor_num[i] = scaling_factor_num; | |
| 402 svc_internal_.svc_params.scaling_factor_den[i] = 256; | |
| 403 scaling_factor_num /= 2; | |
| 404 } | |
| 366 } | 405 } | 
| 367 | 406 | 
| 368 if (!SetSvcRates()) { | 407 if (!SetSvcRates()) { | 
| 369 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; | 408 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; | 
| 370 } | 409 } | 
| 371 | 410 | 
| 372 if (vpx_codec_enc_init(encoder_, vpx_codec_vp9_cx(), config_, 0)) { | 411 if (vpx_codec_enc_init(encoder_, vpx_codec_vp9_cx(), config_, 0)) { | 
| 373 return WEBRTC_VIDEO_CODEC_UNINITIALIZED; | 412 return WEBRTC_VIDEO_CODEC_UNINITIALIZED; | 
| 374 } | 413 } | 
| 375 vpx_codec_control(encoder_, VP8E_SET_CPUUSED, cpu_speed_); | 414 vpx_codec_control(encoder_, VP8E_SET_CPUUSED, cpu_speed_); | 
| (...skipping 405 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 781 decoder_ = NULL; | 820 decoder_ = NULL; | 
| 782 } | 821 } | 
| 783 // Releases buffers from the pool. Any buffers not in use are deleted. Buffers | 822 // Releases buffers from the pool. Any buffers not in use are deleted. Buffers | 
| 784 // still referenced externally are deleted once fully released, not returning | 823 // still referenced externally are deleted once fully released, not returning | 
| 785 // to the pool. | 824 // to the pool. | 
| 786 frame_buffer_pool_.ClearPool(); | 825 frame_buffer_pool_.ClearPool(); | 
| 787 inited_ = false; | 826 inited_ = false; | 
| 788 return WEBRTC_VIDEO_CODEC_OK; | 827 return WEBRTC_VIDEO_CODEC_OK; | 
| 789 } | 828 } | 
| 790 } // namespace webrtc | 829 } // namespace webrtc | 
| OLD | NEW |