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] = |
| 138 config_->layer_target_bitrate[i] = static_cast<int>( |
| 139 static_cast<int64_t>(config_->rc_target_bitrate) * |
| 140 codec_.spatialLayers[i].target_bitrate_bps / 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] = static_cast<float>( |
138 config_->ss_target_bitrate[i] * 2 / 3; | 153 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 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
348 return 2; | 378 return 2; |
349 } else { | 379 } else { |
350 // 1 thread less than VGA. | 380 // 1 thread less than VGA. |
351 return 1; | 381 return 1; |
352 } | 382 } |
353 } | 383 } |
354 | 384 |
355 int VP9EncoderImpl::InitAndSetControlSettings(const VideoCodec* inst) { | 385 int VP9EncoderImpl::InitAndSetControlSettings(const VideoCodec* inst) { |
356 config_->ss_number_layers = num_spatial_layers_; | 386 config_->ss_number_layers = num_spatial_layers_; |
357 | 387 |
358 int scaling_factor_num = 256; | 388 if (ExplicitlyConfiguredSpatialLayers()) { |
359 for (int i = num_spatial_layers_ - 1; i >= 0; --i) { | 389 for (int i = 0; i < num_spatial_layers_; ++i) { |
360 svc_internal_.svc_params.max_quantizers[i] = config_->rc_max_quantizer; | 390 const auto &layer = codec_.spatialLayers[i]; |
361 svc_internal_.svc_params.min_quantizers[i] = config_->rc_min_quantizer; | 391 svc_internal_.svc_params.max_quantizers[i] = config_->rc_max_quantizer; |
362 // 1:2 scaling in each dimension. | 392 svc_internal_.svc_params.min_quantizers[i] = config_->rc_min_quantizer; |
363 svc_internal_.svc_params.scaling_factor_num[i] = scaling_factor_num; | 393 svc_internal_.svc_params.scaling_factor_num[i] = layer.scaling_factor_num; |
364 svc_internal_.svc_params.scaling_factor_den[i] = 256; | 394 svc_internal_.svc_params.scaling_factor_den[i] = layer.scaling_factor_den; |
365 scaling_factor_num /= 2; | 395 } |
| 396 } else { |
| 397 int scaling_factor_num = 256; |
| 398 for (int i = num_spatial_layers_ - 1; i >= 0; --i) { |
| 399 svc_internal_.svc_params.max_quantizers[i] = config_->rc_max_quantizer; |
| 400 svc_internal_.svc_params.min_quantizers[i] = config_->rc_min_quantizer; |
| 401 // 1:2 scaling in each dimension. |
| 402 svc_internal_.svc_params.scaling_factor_num[i] = scaling_factor_num; |
| 403 svc_internal_.svc_params.scaling_factor_den[i] = 256; |
| 404 scaling_factor_num /= 2; |
| 405 } |
366 } | 406 } |
367 | 407 |
368 if (!SetSvcRates()) { | 408 if (!SetSvcRates()) { |
369 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; | 409 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
370 } | 410 } |
371 | 411 |
372 if (vpx_codec_enc_init(encoder_, vpx_codec_vp9_cx(), config_, 0)) { | 412 if (vpx_codec_enc_init(encoder_, vpx_codec_vp9_cx(), config_, 0)) { |
373 return WEBRTC_VIDEO_CODEC_UNINITIALIZED; | 413 return WEBRTC_VIDEO_CODEC_UNINITIALIZED; |
374 } | 414 } |
375 vpx_codec_control(encoder_, VP8E_SET_CPUUSED, cpu_speed_); | 415 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; | 821 decoder_ = NULL; |
782 } | 822 } |
783 // Releases buffers from the pool. Any buffers not in use are deleted. Buffers | 823 // Releases buffers from the pool. Any buffers not in use are deleted. Buffers |
784 // still referenced externally are deleted once fully released, not returning | 824 // still referenced externally are deleted once fully released, not returning |
785 // to the pool. | 825 // to the pool. |
786 frame_buffer_pool_.ClearPool(); | 826 frame_buffer_pool_.ClearPool(); |
787 inited_ = false; | 827 inited_ = false; |
788 return WEBRTC_VIDEO_CODEC_OK; | 828 return WEBRTC_VIDEO_CODEC_OK; |
789 } | 829 } |
790 } // namespace webrtc | 830 } // namespace webrtc |
OLD | NEW |