Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(337)

Side by Side Diff: webrtc/modules/video_coding/codecs/vp8/vp8_impl.cc

Issue 2398963003: Move usage of QualityScaler to ViEEncoder. (Closed)
Patch Set: Report dropped frames to ViEEncoder Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2012 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 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
116 inited_(false), 116 inited_(false),
117 timestamp_(0), 117 timestamp_(0),
118 feedback_mode_(false), 118 feedback_mode_(false),
119 qp_max_(56), // Setting for max quantizer. 119 qp_max_(56), // Setting for max quantizer.
120 cpu_speed_default_(-6), 120 cpu_speed_default_(-6),
121 number_of_cores_(0), 121 number_of_cores_(0),
122 rc_max_intra_target_(0), 122 rc_max_intra_target_(0),
123 token_partitions_(VP8_ONE_TOKENPARTITION), 123 token_partitions_(VP8_ONE_TOKENPARTITION),
124 down_scale_requested_(false), 124 down_scale_requested_(false),
125 down_scale_bitrate_(0), 125 down_scale_bitrate_(0),
126 key_frame_request_(kMaxSimulcastStreams, false), 126 key_frame_request_(kMaxSimulcastStreams, false) {
127 quality_scaler_enabled_(false) {
128 uint32_t seed = rtc::Time32(); 127 uint32_t seed = rtc::Time32();
129 srand(seed); 128 srand(seed);
130 129
131 picture_id_.reserve(kMaxSimulcastStreams); 130 picture_id_.reserve(kMaxSimulcastStreams);
132 last_key_frame_picture_id_.reserve(kMaxSimulcastStreams); 131 last_key_frame_picture_id_.reserve(kMaxSimulcastStreams);
133 temporal_layers_.reserve(kMaxSimulcastStreams); 132 temporal_layers_.reserve(kMaxSimulcastStreams);
134 raw_images_.reserve(kMaxSimulcastStreams); 133 raw_images_.reserve(kMaxSimulcastStreams);
135 encoded_images_.reserve(kMaxSimulcastStreams); 134 encoded_images_.reserve(kMaxSimulcastStreams);
136 send_stream_.reserve(kMaxSimulcastStreams); 135 send_stream_.reserve(kMaxSimulcastStreams);
137 cpu_speed_.assign(kMaxSimulcastStreams, -6); // Set default to -6. 136 cpu_speed_.assign(kMaxSimulcastStreams, -6); // Set default to -6.
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
246 if (send_stream || encoders_.size() > 1) 245 if (send_stream || encoders_.size() > 1)
247 SetStreamState(send_stream, stream_idx); 246 SetStreamState(send_stream, stream_idx);
248 247
249 configurations_[i].rc_target_bitrate = target_bitrate_kbps; 248 configurations_[i].rc_target_bitrate = target_bitrate_kbps;
250 temporal_layers_[stream_idx]->UpdateConfiguration(&configurations_[i]); 249 temporal_layers_[stream_idx]->UpdateConfiguration(&configurations_[i]);
251 250
252 if (vpx_codec_enc_config_set(&encoders_[i], &configurations_[i])) { 251 if (vpx_codec_enc_config_set(&encoders_[i], &configurations_[i])) {
253 return WEBRTC_VIDEO_CODEC_ERROR; 252 return WEBRTC_VIDEO_CODEC_ERROR;
254 } 253 }
255 } 254 }
256 quality_scaler_.ReportFramerate(new_framerate);
257 return WEBRTC_VIDEO_CODEC_OK; 255 return WEBRTC_VIDEO_CODEC_OK;
258 } 256 }
259 257
260 void VP8EncoderImpl::OnDroppedFrame() {
261 if (quality_scaler_enabled_)
262 quality_scaler_.ReportDroppedFrame();
263 }
264
265 const char* VP8EncoderImpl::ImplementationName() const { 258 const char* VP8EncoderImpl::ImplementationName() const {
266 return "libvpx"; 259 return "libvpx";
267 } 260 }
268 261
269 void VP8EncoderImpl::SetStreamState(bool send_stream, 262 void VP8EncoderImpl::SetStreamState(bool send_stream,
270 int stream_idx) { 263 int stream_idx) {
271 if (send_stream && !send_stream_[stream_idx]) { 264 if (send_stream && !send_stream_[stream_idx]) {
272 // Need a key frame if we have not sent this stream before. 265 // Need a key frame if we have not sent this stream before.
273 key_frame_request_[stream_idx] = true; 266 key_frame_request_[stream_idx] = true;
274 } 267 }
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after
527 inst->simulcastStream[stream_idx].width, 520 inst->simulcastStream[stream_idx].width,
528 inst->simulcastStream[stream_idx].height, kVp832ByteAlign); 521 inst->simulcastStream[stream_idx].height, kVp832ByteAlign);
529 SetStreamState(stream_bitrates[stream_idx] > 0, stream_idx); 522 SetStreamState(stream_bitrates[stream_idx] > 0, stream_idx);
530 configurations_[i].rc_target_bitrate = stream_bitrates[stream_idx]; 523 configurations_[i].rc_target_bitrate = stream_bitrates[stream_idx];
531 temporal_layers_[stream_idx]->OnRatesUpdated( 524 temporal_layers_[stream_idx]->OnRatesUpdated(
532 stream_bitrates[stream_idx], inst->maxBitrate, inst->maxFramerate); 525 stream_bitrates[stream_idx], inst->maxBitrate, inst->maxFramerate);
533 temporal_layers_[stream_idx]->UpdateConfiguration(&configurations_[i]); 526 temporal_layers_[stream_idx]->UpdateConfiguration(&configurations_[i]);
534 } 527 }
535 528
536 rps_.Init(); 529 rps_.Init();
537 quality_scaler_.Init(codec_.codecType, codec_.startBitrate, codec_.width,
538 codec_.height, codec_.maxFramerate);
539
540 // Only apply scaling to improve for single-layer streams. The scaling metrics
541 // use frame drops as a signal and is only applicable when we drop frames.
542 quality_scaler_enabled_ = encoders_.size() == 1 &&
543 configurations_[0].rc_dropframe_thresh > 0 &&
544 codec_.VP8()->automaticResizeOn;
545
546 return InitAndSetControlSettings(); 530 return InitAndSetControlSettings();
547 } 531 }
548 532
549 int VP8EncoderImpl::SetCpuSpeed(int width, int height) { 533 int VP8EncoderImpl::SetCpuSpeed(int width, int height) {
550 #if defined(WEBRTC_ARCH_ARM) || defined(WEBRTC_ARCH_ARM64) || defined(ANDROID) 534 #if defined(WEBRTC_ARCH_ARM) || defined(WEBRTC_ARCH_ARM64) || defined(ANDROID)
551 // On mobile platform, use a lower speed setting for lower resolutions for 535 // On mobile platform, use a lower speed setting for lower resolutions for
552 // CPUs with 4 or more cores. 536 // CPUs with 4 or more cores.
553 RTC_DCHECK_GT(number_of_cores_, 0); 537 RTC_DCHECK_GT(number_of_cores_, 0);
554 if (number_of_cores_ <= 3) 538 if (number_of_cores_ <= 3)
555 return -12; 539 return -12;
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
668 uint32_t targetPct = optimalBuffersize * scalePar * codec_.maxFramerate / 10; 652 uint32_t targetPct = optimalBuffersize * scalePar * codec_.maxFramerate / 10;
669 653
670 // Don't go below 3 times the per frame bandwidth. 654 // Don't go below 3 times the per frame bandwidth.
671 const uint32_t minIntraTh = 300; 655 const uint32_t minIntraTh = 300;
672 return (targetPct < minIntraTh) ? minIntraTh : targetPct; 656 return (targetPct < minIntraTh) ? minIntraTh : targetPct;
673 } 657 }
674 658
675 int VP8EncoderImpl::Encode(const VideoFrame& frame, 659 int VP8EncoderImpl::Encode(const VideoFrame& frame,
676 const CodecSpecificInfo* codec_specific_info, 660 const CodecSpecificInfo* codec_specific_info,
677 const std::vector<FrameType>* frame_types) { 661 const std::vector<FrameType>* frame_types) {
662 RTC_DCHECK_EQ(frame.width(), codec_.width);
663 RTC_DCHECK_EQ(frame.height(), codec_.height);
664
678 if (!inited_) 665 if (!inited_)
679 return WEBRTC_VIDEO_CODEC_UNINITIALIZED; 666 return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
680 if (frame.IsZeroSize()) 667 if (frame.IsZeroSize())
681 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; 668 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
682 if (encoded_complete_callback_ == NULL) 669 if (encoded_complete_callback_ == NULL)
683 return WEBRTC_VIDEO_CODEC_UNINITIALIZED; 670 return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
684 671
685 rtc::scoped_refptr<VideoFrameBuffer> input_image = frame.video_frame_buffer(); 672 rtc::scoped_refptr<VideoFrameBuffer> input_image = frame.video_frame_buffer();
686
687 if (quality_scaler_enabled_) {
688 quality_scaler_.OnEncodeFrame(frame.width(), frame.height());
689 input_image = quality_scaler_.GetScaledBuffer(input_image);
690
691 if (input_image->width() != codec_.width ||
692 input_image->height() != codec_.height) {
693 int ret =
694 UpdateCodecFrameSize(input_image->width(), input_image->height());
695 if (ret < 0)
696 return ret;
697 }
698 }
699
700 // Since we are extracting raw pointers from |input_image| to 673 // Since we are extracting raw pointers from |input_image| to
701 // |raw_images_[0]|, the resolution of these frames must match. Note that 674 // |raw_images_[0]|, the resolution of these frames must match. Note that
702 // |input_image| might be scaled from |frame|. In that case, the resolution of 675 // |input_image| might be scaled from |frame|. In that case, the resolution of
703 // |raw_images_[0]| should have been updated in UpdateCodecFrameSize. 676 // |raw_images_[0]| should have been updated in UpdateCodecFrameSize.
704 RTC_DCHECK_EQ(input_image->width(), static_cast<int>(raw_images_[0].d_w)); 677 RTC_DCHECK_EQ(input_image->width(), static_cast<int>(raw_images_[0].d_w));
705 RTC_DCHECK_EQ(input_image->height(), static_cast<int>(raw_images_[0].d_h)); 678 RTC_DCHECK_EQ(input_image->height(), static_cast<int>(raw_images_[0].d_h));
706 679
707 // Image in vpx_image_t format. 680 // Image in vpx_image_t format.
708 // Input image is const. VP8's raw image is not defined as const. 681 // Input image is const. VP8's raw image is not defined as const.
709 raw_images_[0].planes[VPX_PLANE_Y] = 682 raw_images_[0].planes[VPX_PLANE_Y] =
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after
986 encoded_images_[encoder_idx]._length, 959 encoded_images_[encoder_idx]._length,
987 encoded_images_[encoder_idx]._timeStamp, qp); 960 encoded_images_[encoder_idx]._timeStamp, qp);
988 if (send_stream_[stream_idx]) { 961 if (send_stream_[stream_idx]) {
989 if (encoded_images_[encoder_idx]._length > 0) { 962 if (encoded_images_[encoder_idx]._length > 0) {
990 TRACE_COUNTER_ID1("webrtc", "EncodedFrameSize", encoder_idx, 963 TRACE_COUNTER_ID1("webrtc", "EncodedFrameSize", encoder_idx,
991 encoded_images_[encoder_idx]._length); 964 encoded_images_[encoder_idx]._length);
992 encoded_images_[encoder_idx]._encodedHeight = 965 encoded_images_[encoder_idx]._encodedHeight =
993 codec_.simulcastStream[stream_idx].height; 966 codec_.simulcastStream[stream_idx].height;
994 encoded_images_[encoder_idx]._encodedWidth = 967 encoded_images_[encoder_idx]._encodedWidth =
995 codec_.simulcastStream[stream_idx].width; 968 codec_.simulcastStream[stream_idx].width;
996 encoded_images_[encoder_idx]
997 .adapt_reason_.quality_resolution_downscales =
998 quality_scaler_enabled_ ? quality_scaler_.downscale_shift() : -1;
999 // Report once per frame (lowest stream always sent). 969 // Report once per frame (lowest stream always sent).
1000 encoded_images_[encoder_idx].adapt_reason_.bw_resolutions_disabled = 970 encoded_images_[encoder_idx].adapt_reason_.bw_resolutions_disabled =
1001 (stream_idx == 0) ? bw_resolutions_disabled : -1; 971 (stream_idx == 0) ? bw_resolutions_disabled : -1;
1002 int qp_128 = -1; 972 int qp_128 = -1;
1003 vpx_codec_control(&encoders_[encoder_idx], VP8E_GET_LAST_QUANTIZER, 973 vpx_codec_control(&encoders_[encoder_idx], VP8E_GET_LAST_QUANTIZER,
1004 &qp_128); 974 &qp_128);
1005 encoded_images_[encoder_idx].qp_ = qp_128; 975 encoded_images_[encoder_idx].qp_ = qp_128;
1006 encoded_complete_callback_->OnEncodedImage(encoded_images_[encoder_idx], 976 encoded_complete_callback_->OnEncodedImage(encoded_images_[encoder_idx],
1007 &codec_specific, &frag_info); 977 &codec_specific, &frag_info);
1008 } else if (codec_.mode == kScreensharing) { 978 } else if (codec_.mode == kScreensharing) {
1009 result = WEBRTC_VIDEO_CODEC_TARGET_BITRATE_OVERSHOOT; 979 result = WEBRTC_VIDEO_CODEC_TARGET_BITRATE_OVERSHOOT;
1010 } 980 }
1011 } 981 }
1012 } 982 }
1013 if (encoders_.size() == 1 && send_stream_[0]) {
1014 if (encoded_images_[0]._length > 0) {
1015 int qp_128;
1016 vpx_codec_control(&encoders_[0], VP8E_GET_LAST_QUANTIZER, &qp_128);
1017 quality_scaler_.ReportQP(qp_128);
1018 } else {
1019 quality_scaler_.ReportDroppedFrame();
1020 }
1021 }
1022 return result; 983 return result;
1023 } 984 }
1024 985
986 VideoEncoder::ScalingSettings VP8EncoderImpl::GetScalingSettings() const {
987 const bool enable_scaling = encoders_.size() == 1 &&
988 configurations_[0].rc_dropframe_thresh > 0 &&
989 codec_.VP8().automaticResizeOn;
990 return VideoEncoder::ScalingSettings(enable_scaling);
991 }
992
1025 int VP8EncoderImpl::SetChannelParameters(uint32_t packetLoss, int64_t rtt) { 993 int VP8EncoderImpl::SetChannelParameters(uint32_t packetLoss, int64_t rtt) {
1026 rps_.SetRtt(rtt); 994 rps_.SetRtt(rtt);
1027 return WEBRTC_VIDEO_CODEC_OK; 995 return WEBRTC_VIDEO_CODEC_OK;
1028 } 996 }
1029 997
1030 int VP8EncoderImpl::RegisterEncodeCompleteCallback( 998 int VP8EncoderImpl::RegisterEncodeCompleteCallback(
1031 EncodedImageCallback* callback) { 999 EncodedImageCallback* callback) {
1032 encoded_complete_callback_ = callback; 1000 encoded_complete_callback_ = callback;
1033 return WEBRTC_VIDEO_CODEC_OK; 1001 return WEBRTC_VIDEO_CODEC_OK;
1034 } 1002 }
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after
1312 return -1; 1280 return -1;
1313 } 1281 }
1314 if (vpx_codec_control(copy->decoder_, VP8_SET_REFERENCE, ref_frame_) != 1282 if (vpx_codec_control(copy->decoder_, VP8_SET_REFERENCE, ref_frame_) !=
1315 VPX_CODEC_OK) { 1283 VPX_CODEC_OK) {
1316 return -1; 1284 return -1;
1317 } 1285 }
1318 return 0; 1286 return 0;
1319 } 1287 }
1320 1288
1321 } // namespace webrtc 1289 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698