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 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 input_image_(NULL), | 76 input_image_(NULL), |
77 tl0_pic_idx_(0), | 77 tl0_pic_idx_(0), |
78 frames_since_kf_(0), | 78 frames_since_kf_(0), |
79 num_temporal_layers_(0), | 79 num_temporal_layers_(0), |
80 num_spatial_layers_(0), | 80 num_spatial_layers_(0), |
81 is_flexible_mode_(false), | 81 is_flexible_mode_(false), |
82 frames_encoded_(0), | 82 frames_encoded_(0), |
83 // Use two spatial when screensharing with flexible mode. | 83 // Use two spatial when screensharing with flexible mode. |
84 spatial_layer_(new ScreenshareLayersVP9(2)) { | 84 spatial_layer_(new ScreenshareLayersVP9(2)) { |
85 memset(&codec_, 0, sizeof(codec_)); | 85 memset(&codec_, 0, sizeof(codec_)); |
86 memset(&svc_internal_.svc_params, 0, sizeof(vpx_svc_extra_cfg_t)); | 86 memset(&svc_params_, 0, sizeof(vpx_svc_extra_cfg_t)); |
87 uint32_t seed = rtc::Time32(); | 87 uint32_t seed = rtc::Time32(); |
88 srand(seed); | 88 srand(seed); |
89 } | 89 } |
90 | 90 |
91 VP9EncoderImpl::~VP9EncoderImpl() { | 91 VP9EncoderImpl::~VP9EncoderImpl() { |
92 Release(); | 92 Release(); |
93 } | 93 } |
94 | 94 |
95 int VP9EncoderImpl::Release() { | 95 int VP9EncoderImpl::Release() { |
96 if (encoded_image_._buffer != NULL) { | 96 if (encoded_image_._buffer != NULL) { |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 config_->ss_target_bitrate[i] = config_->layer_target_bitrate[i] = | 141 config_->ss_target_bitrate[i] = config_->layer_target_bitrate[i] = |
142 static_cast<int>(static_cast<int64_t>(config_->rc_target_bitrate) * | 142 static_cast<int>(static_cast<int64_t>(config_->rc_target_bitrate) * |
143 codec_.spatialLayers[i].target_bitrate_bps / | 143 codec_.spatialLayers[i].target_bitrate_bps / |
144 total_bitrate_bps); | 144 total_bitrate_bps); |
145 } | 145 } |
146 } else { | 146 } else { |
147 float rate_ratio[VPX_MAX_LAYERS] = {0}; | 147 float rate_ratio[VPX_MAX_LAYERS] = {0}; |
148 float total = 0; | 148 float total = 0; |
149 | 149 |
150 for (i = 0; i < num_spatial_layers_; ++i) { | 150 for (i = 0; i < num_spatial_layers_; ++i) { |
151 if (svc_internal_.svc_params.scaling_factor_num[i] <= 0 || | 151 if (svc_params_.scaling_factor_num[i] <= 0 || |
152 svc_internal_.svc_params.scaling_factor_den[i] <= 0) { | 152 svc_params_.scaling_factor_den[i] <= 0) { |
153 LOG(LS_ERROR) << "Scaling factors not specified!"; | 153 LOG(LS_ERROR) << "Scaling factors not specified!"; |
154 return false; | 154 return false; |
155 } | 155 } |
156 rate_ratio[i] = | 156 rate_ratio[i] = |
157 static_cast<float>(svc_internal_.svc_params.scaling_factor_num[i]) / | 157 static_cast<float>(svc_params_.scaling_factor_num[i]) / |
158 svc_internal_.svc_params.scaling_factor_den[i]; | 158 svc_params_.scaling_factor_den[i]; |
159 total += rate_ratio[i]; | 159 total += rate_ratio[i]; |
160 } | 160 } |
161 | 161 |
162 for (i = 0; i < num_spatial_layers_; ++i) { | 162 for (i = 0; i < num_spatial_layers_; ++i) { |
163 config_->ss_target_bitrate[i] = static_cast<unsigned int>( | 163 config_->ss_target_bitrate[i] = static_cast<unsigned int>( |
164 config_->rc_target_bitrate * rate_ratio[i] / total); | 164 config_->rc_target_bitrate * rate_ratio[i] / total); |
165 if (num_temporal_layers_ == 1) { | 165 if (num_temporal_layers_ == 1) { |
166 config_->layer_target_bitrate[i] = config_->ss_target_bitrate[i]; | 166 config_->layer_target_bitrate[i] = config_->ss_target_bitrate[i]; |
167 } else if (num_temporal_layers_ == 2) { | 167 } else if (num_temporal_layers_ == 2) { |
168 config_->layer_target_bitrate[i * num_temporal_layers_] = | 168 config_->layer_target_bitrate[i * num_temporal_layers_] = |
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
389 } else { | 389 } else { |
390 // 1 thread less than VGA. | 390 // 1 thread less than VGA. |
391 return 1; | 391 return 1; |
392 } | 392 } |
393 } | 393 } |
394 | 394 |
395 int VP9EncoderImpl::InitAndSetControlSettings(const VideoCodec* inst) { | 395 int VP9EncoderImpl::InitAndSetControlSettings(const VideoCodec* inst) { |
396 // Set QP-min/max per spatial and temporal layer. | 396 // Set QP-min/max per spatial and temporal layer. |
397 int tot_num_layers = num_spatial_layers_ * num_temporal_layers_; | 397 int tot_num_layers = num_spatial_layers_ * num_temporal_layers_; |
398 for (int i = 0; i < tot_num_layers; ++i) { | 398 for (int i = 0; i < tot_num_layers; ++i) { |
399 svc_internal_.svc_params.max_quantizers[i] = config_->rc_max_quantizer; | 399 svc_params_.max_quantizers[i] = config_->rc_max_quantizer; |
400 svc_internal_.svc_params.min_quantizers[i] = config_->rc_min_quantizer; | 400 svc_params_.min_quantizers[i] = config_->rc_min_quantizer; |
401 } | 401 } |
402 config_->ss_number_layers = num_spatial_layers_; | 402 config_->ss_number_layers = num_spatial_layers_; |
403 if (ExplicitlyConfiguredSpatialLayers()) { | 403 if (ExplicitlyConfiguredSpatialLayers()) { |
404 for (int i = 0; i < num_spatial_layers_; ++i) { | 404 for (int i = 0; i < num_spatial_layers_; ++i) { |
405 const auto& layer = codec_.spatialLayers[i]; | 405 const auto& layer = codec_.spatialLayers[i]; |
406 svc_internal_.svc_params.scaling_factor_num[i] = layer.scaling_factor_num; | 406 svc_params_.scaling_factor_num[i] = layer.scaling_factor_num; |
407 svc_internal_.svc_params.scaling_factor_den[i] = layer.scaling_factor_den; | 407 svc_params_.scaling_factor_den[i] = layer.scaling_factor_den; |
408 } | 408 } |
409 } else { | 409 } else { |
410 int scaling_factor_num = 256; | 410 int scaling_factor_num = 256; |
411 for (int i = num_spatial_layers_ - 1; i >= 0; --i) { | 411 for (int i = num_spatial_layers_ - 1; i >= 0; --i) { |
412 // 1:2 scaling in each dimension. | 412 // 1:2 scaling in each dimension. |
413 svc_internal_.svc_params.scaling_factor_num[i] = scaling_factor_num; | 413 svc_params_.scaling_factor_num[i] = scaling_factor_num; |
414 svc_internal_.svc_params.scaling_factor_den[i] = 256; | 414 svc_params_.scaling_factor_den[i] = 256; |
415 if (codec_.mode != kScreensharing) | 415 if (codec_.mode != kScreensharing) |
416 scaling_factor_num /= 2; | 416 scaling_factor_num /= 2; |
417 } | 417 } |
418 } | 418 } |
419 | 419 |
420 if (!SetSvcRates()) { | 420 if (!SetSvcRates()) { |
421 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; | 421 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
422 } | 422 } |
423 | 423 |
424 if (vpx_codec_enc_init(encoder_, vpx_codec_vp9_cx(), config_, 0)) { | 424 if (vpx_codec_enc_init(encoder_, vpx_codec_vp9_cx(), config_, 0)) { |
425 return WEBRTC_VIDEO_CODEC_UNINITIALIZED; | 425 return WEBRTC_VIDEO_CODEC_UNINITIALIZED; |
426 } | 426 } |
427 vpx_codec_control(encoder_, VP8E_SET_CPUUSED, cpu_speed_); | 427 vpx_codec_control(encoder_, VP8E_SET_CPUUSED, cpu_speed_); |
428 vpx_codec_control(encoder_, VP8E_SET_MAX_INTRA_BITRATE_PCT, | 428 vpx_codec_control(encoder_, VP8E_SET_MAX_INTRA_BITRATE_PCT, |
429 rc_max_intra_target_); | 429 rc_max_intra_target_); |
430 vpx_codec_control(encoder_, VP9E_SET_AQ_MODE, | 430 vpx_codec_control(encoder_, VP9E_SET_AQ_MODE, |
431 inst->VP9().adaptiveQpMode ? 3 : 0); | 431 inst->VP9().adaptiveQpMode ? 3 : 0); |
432 | 432 |
433 vpx_codec_control( | 433 vpx_codec_control( |
434 encoder_, VP9E_SET_SVC, | 434 encoder_, VP9E_SET_SVC, |
435 (num_temporal_layers_ > 1 || num_spatial_layers_ > 1) ? 1 : 0); | 435 (num_temporal_layers_ > 1 || num_spatial_layers_ > 1) ? 1 : 0); |
436 if (num_temporal_layers_ > 1 || num_spatial_layers_ > 1) { | 436 if (num_temporal_layers_ > 1 || num_spatial_layers_ > 1) { |
437 vpx_codec_control(encoder_, VP9E_SET_SVC_PARAMETERS, | 437 vpx_codec_control(encoder_, VP9E_SET_SVC_PARAMETERS, |
438 &svc_internal_.svc_params); | 438 &svc_params_); |
439 } | 439 } |
440 // Register callback for getting each spatial layer. | 440 // Register callback for getting each spatial layer. |
441 vpx_codec_priv_output_cx_pkt_cb_pair_t cbp = { | 441 vpx_codec_priv_output_cx_pkt_cb_pair_t cbp = { |
442 VP9EncoderImpl::EncoderOutputCodedPacketCallback, | 442 VP9EncoderImpl::EncoderOutputCodedPacketCallback, |
443 reinterpret_cast<void*>(this)}; | 443 reinterpret_cast<void*>(this)}; |
444 vpx_codec_control(encoder_, VP9E_REGISTER_CX_CALLBACK, | 444 vpx_codec_control(encoder_, VP9E_REGISTER_CX_CALLBACK, |
445 reinterpret_cast<void*>(&cbp)); | 445 reinterpret_cast<void*>(&cbp)); |
446 | 446 |
447 // Control function to set the number of column tiles in encoding a frame, in | 447 // Control function to set the number of column tiles in encoding a frame, in |
448 // log2 unit: e.g., 0 = 1 tile column, 1 = 2 tile columns, 2 = 4 tile columns. | 448 // log2 unit: e.g., 0 = 1 tile column, 1 = 2 tile columns, 2 = 4 tile columns. |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
641 } else { | 641 } else { |
642 vp9_info->gof_idx = | 642 vp9_info->gof_idx = |
643 static_cast<uint8_t>(frames_since_kf_ % gof_.num_frames_in_gof); | 643 static_cast<uint8_t>(frames_since_kf_ % gof_.num_frames_in_gof); |
644 vp9_info->temporal_up_switch = gof_.temporal_up_switch[vp9_info->gof_idx]; | 644 vp9_info->temporal_up_switch = gof_.temporal_up_switch[vp9_info->gof_idx]; |
645 } | 645 } |
646 | 646 |
647 if (vp9_info->ss_data_available) { | 647 if (vp9_info->ss_data_available) { |
648 vp9_info->spatial_layer_resolution_present = true; | 648 vp9_info->spatial_layer_resolution_present = true; |
649 for (size_t i = 0; i < vp9_info->num_spatial_layers; ++i) { | 649 for (size_t i = 0; i < vp9_info->num_spatial_layers; ++i) { |
650 vp9_info->width[i] = codec_.width * | 650 vp9_info->width[i] = codec_.width * |
651 svc_internal_.svc_params.scaling_factor_num[i] / | 651 svc_params_.scaling_factor_num[i] / |
652 svc_internal_.svc_params.scaling_factor_den[i]; | 652 svc_params_.scaling_factor_den[i]; |
653 vp9_info->height[i] = codec_.height * | 653 vp9_info->height[i] = codec_.height * |
654 svc_internal_.svc_params.scaling_factor_num[i] / | 654 svc_params_.scaling_factor_num[i] / |
655 svc_internal_.svc_params.scaling_factor_den[i]; | 655 svc_params_.scaling_factor_den[i]; |
656 } | 656 } |
657 if (!vp9_info->flexible_mode) { | 657 if (!vp9_info->flexible_mode) { |
658 vp9_info->gof.CopyGofInfoVP9(gof_); | 658 vp9_info->gof.CopyGofInfoVP9(gof_); |
659 } | 659 } |
660 } | 660 } |
661 } | 661 } |
662 | 662 |
663 int VP9EncoderImpl::GetEncodedLayerFrame(const vpx_codec_cx_pkt* pkt) { | 663 int VP9EncoderImpl::GetEncodedLayerFrame(const vpx_codec_cx_pkt* pkt) { |
664 RTC_DCHECK_EQ(pkt->kind, VPX_CODEC_CX_FRAME_PKT); | 664 RTC_DCHECK_EQ(pkt->kind, VPX_CODEC_CX_FRAME_PKT); |
665 | 665 |
(...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
997 frame_buffer_pool_.ClearPool(); | 997 frame_buffer_pool_.ClearPool(); |
998 inited_ = false; | 998 inited_ = false; |
999 return WEBRTC_VIDEO_CODEC_OK; | 999 return WEBRTC_VIDEO_CODEC_OK; |
1000 } | 1000 } |
1001 | 1001 |
1002 const char* VP9DecoderImpl::ImplementationName() const { | 1002 const char* VP9DecoderImpl::ImplementationName() const { |
1003 return "libvpx"; | 1003 return "libvpx"; |
1004 } | 1004 } |
1005 | 1005 |
1006 } // namespace webrtc | 1006 } // namespace webrtc |
OLD | NEW |