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 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 config_ = NULL; | 105 config_ = NULL; |
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::ExplicitlyConfiguredSpatialLayers() const { |
| 116 // We check target_bitrate_bps of the 0th layer to see if the spatial layers |
| 117 // (i.e. bitrates) were explicitly configured. |
| 118 return num_spatial_layers_ > 1 && |
| 119 codec_.spatialLayers[0].target_bitrate_bps > 0; |
| 120 } |
| 121 |
115 bool VP9EncoderImpl::SetSvcRates() { | 122 bool VP9EncoderImpl::SetSvcRates() { |
116 float rate_ratio[VPX_MAX_LAYERS] = {0}; | |
117 float total = 0; | |
118 uint8_t i = 0; | 123 uint8_t i = 0; |
119 | 124 |
120 for (i = 0; i < num_spatial_layers_; ++i) { | 125 if (ExplicitlyConfiguredSpatialLayers()) { |
121 if (svc_internal_.svc_params.scaling_factor_num[i] <= 0 || | 126 if (num_temporal_layers_ > 1) { |
122 svc_internal_.svc_params.scaling_factor_den[i] <= 0) { | 127 LOG(LS_ERROR) << "Multiple temporal layers when manually specifying " |
| 128 "spatial layers not implemented yet!"; |
123 return false; | 129 return false; |
124 } | 130 } |
125 rate_ratio[i] = static_cast<float>( | 131 int total_bitrate_bps = 0; |
126 svc_internal_.svc_params.scaling_factor_num[i]) / | 132 for (i = 0; i < num_spatial_layers_; ++i) |
127 svc_internal_.svc_params.scaling_factor_den[i]; | 133 total_bitrate_bps += codec_.spatialLayers[i].target_bitrate_bps; |
128 total += rate_ratio[i]; | 134 // If total bitrate differs now from what has been specified at the |
129 } | 135 // beginning, update the bitrates in the same ratio as before. |
| 136 for (i = 0; i < num_spatial_layers_; ++i) { |
| 137 config_->ss_target_bitrate[i] = config_->layer_target_bitrate[i] = |
| 138 static_cast<int>(static_cast<int64_t>(config_->rc_target_bitrate) * |
| 139 codec_.spatialLayers[i].target_bitrate_bps / |
| 140 total_bitrate_bps); |
| 141 } |
| 142 } else { |
| 143 float rate_ratio[VPX_MAX_LAYERS] = {0}; |
| 144 float total = 0; |
130 | 145 |
131 for (i = 0; i < num_spatial_layers_; ++i) { | 146 for (i = 0; i < num_spatial_layers_; ++i) { |
132 config_->ss_target_bitrate[i] = static_cast<unsigned int>( | 147 if (svc_internal_.svc_params.scaling_factor_num[i] <= 0 || |
133 config_->rc_target_bitrate * rate_ratio[i] / total); | 148 svc_internal_.svc_params.scaling_factor_den[i] <= 0) { |
134 if (num_temporal_layers_ == 1) { | 149 LOG(LS_ERROR) << "Scaling factors not specified!"; |
135 config_->layer_target_bitrate[i] = config_->ss_target_bitrate[i]; | 150 return false; |
136 } else if (num_temporal_layers_ == 2) { | 151 } |
137 config_->layer_target_bitrate[i * num_temporal_layers_] = | 152 rate_ratio[i] = |
138 config_->ss_target_bitrate[i] * 2 / 3; | 153 static_cast<float>(svc_internal_.svc_params.scaling_factor_num[i]) / |
139 config_->layer_target_bitrate[i * num_temporal_layers_ + 1] = | 154 svc_internal_.svc_params.scaling_factor_den[i]; |
140 config_->ss_target_bitrate[i]; | 155 total += rate_ratio[i]; |
141 } else if (num_temporal_layers_ == 3) { | 156 } |
142 config_->layer_target_bitrate[i * num_temporal_layers_] = | 157 |
143 config_->ss_target_bitrate[i] / 2; | 158 for (i = 0; i < num_spatial_layers_; ++i) { |
144 config_->layer_target_bitrate[i * num_temporal_layers_ + 1] = | 159 config_->ss_target_bitrate[i] = static_cast<unsigned int>( |
145 config_->layer_target_bitrate[i * num_temporal_layers_] + | 160 config_->rc_target_bitrate * rate_ratio[i] / total); |
146 (config_->ss_target_bitrate[i] / 4); | 161 if (num_temporal_layers_ == 1) { |
147 config_->layer_target_bitrate[i * num_temporal_layers_ + 2] = | 162 config_->layer_target_bitrate[i] = config_->ss_target_bitrate[i]; |
148 config_->ss_target_bitrate[i]; | 163 } else if (num_temporal_layers_ == 2) { |
149 } else { | 164 config_->layer_target_bitrate[i * num_temporal_layers_] = |
150 return false; | 165 config_->ss_target_bitrate[i] * 2 / 3; |
| 166 config_->layer_target_bitrate[i * num_temporal_layers_ + 1] = |
| 167 config_->ss_target_bitrate[i]; |
| 168 } else if (num_temporal_layers_ == 3) { |
| 169 config_->layer_target_bitrate[i * num_temporal_layers_] = |
| 170 config_->ss_target_bitrate[i] / 2; |
| 171 config_->layer_target_bitrate[i * num_temporal_layers_ + 1] = |
| 172 config_->layer_target_bitrate[i * num_temporal_layers_] + |
| 173 (config_->ss_target_bitrate[i] / 4); |
| 174 config_->layer_target_bitrate[i * num_temporal_layers_ + 2] = |
| 175 config_->ss_target_bitrate[i]; |
| 176 } else { |
| 177 LOG(LS_ERROR) << "Unsupported number of temporal layers: " |
| 178 << num_temporal_layers_; |
| 179 return false; |
| 180 } |
151 } | 181 } |
152 } | 182 } |
153 | 183 |
154 // For now, temporal layers only supported when having one spatial layer. | 184 // For now, temporal layers only supported when having one spatial layer. |
155 if (num_spatial_layers_ == 1) { | 185 if (num_spatial_layers_ == 1) { |
156 for (i = 0; i < num_temporal_layers_; ++i) { | 186 for (i = 0; i < num_temporal_layers_; ++i) { |
157 config_->ts_target_bitrate[i] = config_->layer_target_bitrate[i]; | 187 config_->ts_target_bitrate[i] = config_->layer_target_bitrate[i]; |
158 } | 188 } |
159 } | 189 } |
160 | 190 |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
342 return 2; | 372 return 2; |
343 } else { | 373 } else { |
344 // 1 thread less than VGA. | 374 // 1 thread less than VGA. |
345 return 1; | 375 return 1; |
346 } | 376 } |
347 } | 377 } |
348 | 378 |
349 int VP9EncoderImpl::InitAndSetControlSettings(const VideoCodec* inst) { | 379 int VP9EncoderImpl::InitAndSetControlSettings(const VideoCodec* inst) { |
350 config_->ss_number_layers = num_spatial_layers_; | 380 config_->ss_number_layers = num_spatial_layers_; |
351 | 381 |
352 int scaling_factor_num = 256; | 382 if (ExplicitlyConfiguredSpatialLayers()) { |
353 for (int i = num_spatial_layers_ - 1; i >= 0; --i) { | 383 for (int i = 0; i < num_spatial_layers_; ++i) { |
354 svc_internal_.svc_params.max_quantizers[i] = config_->rc_max_quantizer; | 384 const auto& layer = codec_.spatialLayers[i]; |
355 svc_internal_.svc_params.min_quantizers[i] = config_->rc_min_quantizer; | 385 svc_internal_.svc_params.max_quantizers[i] = config_->rc_max_quantizer; |
356 // 1:2 scaling in each dimension. | 386 svc_internal_.svc_params.min_quantizers[i] = config_->rc_min_quantizer; |
357 svc_internal_.svc_params.scaling_factor_num[i] = scaling_factor_num; | 387 svc_internal_.svc_params.scaling_factor_num[i] = layer.scaling_factor_num; |
358 svc_internal_.svc_params.scaling_factor_den[i] = 256; | 388 svc_internal_.svc_params.scaling_factor_den[i] = layer.scaling_factor_den; |
359 scaling_factor_num /= 2; | 389 } |
| 390 } else { |
| 391 int scaling_factor_num = 256; |
| 392 for (int i = num_spatial_layers_ - 1; i >= 0; --i) { |
| 393 svc_internal_.svc_params.max_quantizers[i] = config_->rc_max_quantizer; |
| 394 svc_internal_.svc_params.min_quantizers[i] = config_->rc_min_quantizer; |
| 395 // 1:2 scaling in each dimension. |
| 396 svc_internal_.svc_params.scaling_factor_num[i] = scaling_factor_num; |
| 397 svc_internal_.svc_params.scaling_factor_den[i] = 256; |
| 398 scaling_factor_num /= 2; |
| 399 } |
360 } | 400 } |
361 | 401 |
362 if (!SetSvcRates()) { | 402 if (!SetSvcRates()) { |
363 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; | 403 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
364 } | 404 } |
365 | 405 |
366 if (vpx_codec_enc_init(encoder_, vpx_codec_vp9_cx(), config_, 0)) { | 406 if (vpx_codec_enc_init(encoder_, vpx_codec_vp9_cx(), config_, 0)) { |
367 return WEBRTC_VIDEO_CODEC_UNINITIALIZED; | 407 return WEBRTC_VIDEO_CODEC_UNINITIALIZED; |
368 } | 408 } |
369 vpx_codec_control(encoder_, VP8E_SET_CPUUSED, cpu_speed_); | 409 vpx_codec_control(encoder_, VP8E_SET_CPUUSED, cpu_speed_); |
(...skipping 405 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
775 decoder_ = NULL; | 815 decoder_ = NULL; |
776 } | 816 } |
777 // Releases buffers from the pool. Any buffers not in use are deleted. Buffers | 817 // Releases buffers from the pool. Any buffers not in use are deleted. Buffers |
778 // still referenced externally are deleted once fully released, not returning | 818 // still referenced externally are deleted once fully released, not returning |
779 // to the pool. | 819 // to the pool. |
780 frame_buffer_pool_.ClearPool(); | 820 frame_buffer_pool_.ClearPool(); |
781 inited_ = false; | 821 inited_ = false; |
782 return WEBRTC_VIDEO_CODEC_OK; | 822 return WEBRTC_VIDEO_CODEC_OK; |
783 } | 823 } |
784 } // namespace webrtc | 824 } // namespace webrtc |
OLD | NEW |