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 |
| 11 #include "webrtc/modules/video_coding/utility/quality_scaler.h" | 11 #include "webrtc/modules/video_coding/utility/quality_scaler.h" |
| 12 | 12 |
| 13 #include <algorithm> | |
| 14 | |
| 13 namespace webrtc { | 15 namespace webrtc { |
| 14 | 16 |
| 15 namespace { | 17 namespace { |
| 16 static const int kMinFps = 5; | |
| 17 // Threshold constant used until first downscale (to permit fast rampup). | 18 // Threshold constant used until first downscale (to permit fast rampup). |
| 18 static const int kMeasureSecondsFastUpscale = 2; | 19 static const int kMeasureSecondsFastUpscale = 2; |
| 19 static const int kMeasureSecondsUpscale = 5; | 20 static const int kMeasureSecondsUpscale = 5; |
| 20 static const int kMeasureSecondsDownscale = 5; | 21 static const int kMeasureSecondsDownscale = 5; |
| 21 static const int kFramedropPercentThreshold = 60; | 22 static const int kFramedropPercentThreshold = 60; |
| 22 // Min width/height to downscale to, set to not go below QVGA, but with some | 23 // Min width/height to downscale to, set to not go below QVGA, but with some |
| 23 // margin to permit "almost-QVGA" resolutions, such as QCIF. | 24 // margin to permit "almost-QVGA" resolutions, such as QCIF. |
| 24 static const int kMinDownscaleDimension = 140; | 25 static const int kMinDownscaleDimension = 140; |
| 25 // Initial resolutions corresponding to a bitrate. Aa bit above their actual | 26 // Initial resolutions corresponding to a bitrate. Aa bit above their actual |
| 26 // values to permit near-VGA and near-QVGA resolutions to use the same | 27 // values to permit near-VGA and near-QVGA resolutions to use the same |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 47 #endif | 48 #endif |
| 48 | 49 |
| 49 QualityScaler::QualityScaler() : low_qp_threshold_(-1) {} | 50 QualityScaler::QualityScaler() : low_qp_threshold_(-1) {} |
| 50 | 51 |
| 51 void QualityScaler::Init(int low_qp_threshold, | 52 void QualityScaler::Init(int low_qp_threshold, |
| 52 int high_qp_threshold, | 53 int high_qp_threshold, |
| 53 int initial_bitrate_kbps, | 54 int initial_bitrate_kbps, |
| 54 int width, | 55 int width, |
| 55 int height, | 56 int height, |
| 56 int fps) { | 57 int fps) { |
| 57 ClearSamples(); | |
| 58 low_qp_threshold_ = low_qp_threshold; | 58 low_qp_threshold_ = low_qp_threshold; |
| 59 high_qp_threshold_ = high_qp_threshold; | 59 high_qp_threshold_ = high_qp_threshold; |
| 60 downscale_shift_ = 0; | 60 downscale_shift_ = 0; |
| 61 // Use a faster window for upscaling initially (but be more graceful later). | 61 |
| 62 // This enables faster initial rampups without risking strong up-down | 62 fast_rampup_ = true; |
| 63 // behavior later. | 63 |
| 64 measure_seconds_upscale_ = kMeasureSecondsFastUpscale; | 64 ClearSamples(); |
| 65 ReportFramerate(fps); | |
| 66 | |
| 65 const int init_width = width; | 67 const int init_width = width; |
| 66 const int init_height = height; | 68 const int init_height = height; |
| 67 if (initial_bitrate_kbps > 0) { | 69 if (initial_bitrate_kbps > 0) { |
| 68 int init_num_pixels = width * height; | 70 int init_num_pixels = width * height; |
| 69 if (initial_bitrate_kbps < kVgaBitrateThresholdKbps) | 71 if (initial_bitrate_kbps < kVgaBitrateThresholdKbps) |
| 70 init_num_pixels = kVgaNumPixels; | 72 init_num_pixels = kVgaNumPixels; |
| 71 if (initial_bitrate_kbps < kQvgaBitrateThresholdKbps) | 73 if (initial_bitrate_kbps < kQvgaBitrateThresholdKbps) |
| 72 init_num_pixels = kQvgaNumPixels; | 74 init_num_pixels = kQvgaNumPixels; |
| 73 while (width * height > init_num_pixels) { | 75 while (width * height > init_num_pixels) { |
| 74 ++downscale_shift_; | 76 ++downscale_shift_; |
| 75 width /= 2; | 77 width /= 2; |
| 76 height /= 2; | 78 height /= 2; |
| 77 } | 79 } |
| 78 } | 80 } |
| 79 | |
| 80 // Zero out width/height so they can be checked against inside | |
| 81 // UpdateTargetResolution. | |
| 82 res_.width = res_.height = 0; | |
| 83 UpdateTargetResolution(init_width, init_height); | 81 UpdateTargetResolution(init_width, init_height); |
| 84 ReportFramerate(fps); | 82 ReportFramerate(fps); |
| 85 } | 83 } |
| 86 | 84 |
| 87 // Report framerate(fps) to estimate # of samples. | 85 // Report framerate(fps) to estimate # of samples. |
| 88 void QualityScaler::ReportFramerate(int framerate) { | 86 void QualityScaler::ReportFramerate(int framerate) { |
| 89 framerate_ = framerate; | 87 // Use a faster window for upscaling initially. |
| 90 UpdateSampleCounts(); | 88 // This enables faster initial rampups without risking strong up-down |
| 89 // behavior later. | |
| 90 if (fast_rampup_) | |
|
magjed_webrtc
2016/09/05 15:01:52
I would prefer to write this as:
num_samples_upsca
| |
| 91 num_samples_upscale_ = framerate * kMeasureSecondsFastUpscale; | |
| 92 else | |
| 93 num_samples_upscale_ = framerate * kMeasureSecondsUpscale; | |
| 94 num_samples_downscale_ = framerate * kMeasureSecondsDownscale; | |
| 91 } | 95 } |
| 92 | 96 |
| 93 void QualityScaler::ReportQP(int qp) { | 97 void QualityScaler::ReportQP(int qp) { |
| 94 framedrop_percent_.AddSample(0); | 98 framedrop_percent_.AddSample(0); |
| 95 average_qp_downscale_.AddSample(qp); | 99 average_qp_.AddSample(qp); |
| 96 average_qp_upscale_.AddSample(qp); | |
| 97 } | 100 } |
| 98 | 101 |
| 99 void QualityScaler::ReportDroppedFrame() { | 102 void QualityScaler::ReportDroppedFrame() { |
| 100 framedrop_percent_.AddSample(100); | 103 framedrop_percent_.AddSample(100); |
| 101 } | 104 } |
| 102 | 105 |
| 103 void QualityScaler::OnEncodeFrame(int width, int height) { | 106 void QualityScaler::OnEncodeFrame(int width, int height) { |
| 104 // Should be set through InitEncode -> Should be set by now. | 107 // Should be set through InitEncode -> Should be set by now. |
| 105 RTC_DCHECK_GE(low_qp_threshold_, 0); | 108 RTC_DCHECK_GE(low_qp_threshold_, 0); |
| 106 RTC_DCHECK_GT(num_samples_upscale_, 0u); | 109 if (res_.width != width || res_.height != height) { |
| 107 RTC_DCHECK_GT(num_samples_downscale_, 0u); | 110 UpdateTargetResolution(width, height); |
| 111 } | |
| 108 | 112 |
| 109 // Update scale factor. | 113 if ((framedrop_percent_.size() >= num_samples_downscale_ && |
| 110 int avg_drop = 0; | 114 (framedrop_percent_.GetAverage(num_samples_downscale_) >= |
| 111 int avg_qp = 0; | 115 kFramedropPercentThreshold)) || |
| 112 | 116 (average_qp_.size() >= num_samples_downscale_ && |
| 113 if ((framedrop_percent_.GetAverage(num_samples_downscale_, &avg_drop) && | 117 (average_qp_.GetAverage(num_samples_downscale_) > high_qp_threshold_))) { |
| 114 avg_drop >= kFramedropPercentThreshold) || | 118 // too much framedrop or too high QP. We want to scale down. |
|
magjed_webrtc
2016/09/05 15:01:52
nit: Start with capital letter in comments.
| |
| 115 (average_qp_downscale_.GetAverage(num_samples_downscale_, &avg_qp) && | 119 downscale_shift_ = std::min(maximum_shift_, downscale_shift_ + 1); |
| 116 avg_qp > high_qp_threshold_)) { | 120 if (fast_rampup_) { |
| 117 AdjustScale(false); | 121 fast_rampup_ = false; |
| 118 } else if (average_qp_upscale_.GetAverage(num_samples_upscale_, &avg_qp) && | 122 num_samples_upscale_ = |
| 119 avg_qp <= low_qp_threshold_) { | 123 (num_samples_upscale_ / kMeasureSecondsFastUpscale) |
| 120 AdjustScale(true); | 124 * kMeasureSecondsUpscale; |
| 125 } | |
| 126 ClearSamples(); | |
| 127 } else if (average_qp_.size() >= num_samples_upscale_ && | |
| 128 average_qp_.GetAverage(num_samples_upscale_) <= low_qp_threshold_) { | |
| 129 // QP has been high. We want to try a higher resolution. | |
| 130 downscale_shift_ = std::max(0, downscale_shift_ - 1); | |
| 131 ClearSamples(); | |
| 121 } | 132 } |
| 122 UpdateTargetResolution(width, height); | |
| 123 } | 133 } |
| 124 | 134 |
| 125 QualityScaler::Resolution QualityScaler::GetScaledResolution() const { | 135 QualityScaler::Resolution QualityScaler::GetScaledResolution() const { |
| 126 return res_; | 136 int frame_width = res_.width >> downscale_shift_; |
|
magjed_webrtc
2016/09/05 15:01:52
Use const (or inline these).
| |
| 137 int frame_height = res_.height >> downscale_shift_; | |
| 138 return Resolution{frame_width, frame_height}; | |
| 127 } | 139 } |
| 128 | 140 |
| 129 rtc::scoped_refptr<VideoFrameBuffer> QualityScaler::GetScaledBuffer( | 141 rtc::scoped_refptr<VideoFrameBuffer> QualityScaler::GetScaledBuffer( |
| 130 const rtc::scoped_refptr<VideoFrameBuffer>& frame) { | 142 const rtc::scoped_refptr<VideoFrameBuffer>& frame) { |
| 131 Resolution res = GetScaledResolution(); | 143 Resolution res = GetScaledResolution(); |
| 132 int src_width = frame->width(); | 144 int src_width = frame->width(); |
| 133 int src_height = frame->height(); | 145 int src_height = frame->height(); |
| 134 | 146 |
| 135 if (res.width == src_width && res.height == src_height) | 147 if (res.width == src_width && res.height == src_height) |
| 136 return frame; | 148 return frame; |
| 137 rtc::scoped_refptr<I420Buffer> scaled_buffer = | 149 rtc::scoped_refptr<I420Buffer> scaled_buffer = |
| 138 pool_.CreateBuffer(res.width, res.height); | 150 pool_.CreateBuffer(res.width, res.height); |
| 139 | 151 |
| 140 scaled_buffer->ScaleFrom(frame); | 152 scaled_buffer->ScaleFrom(frame); |
| 141 | 153 |
| 142 return scaled_buffer; | 154 return scaled_buffer; |
| 143 } | 155 } |
| 144 | 156 |
| 145 void QualityScaler::UpdateTargetResolution(int frame_width, int frame_height) { | 157 void QualityScaler::UpdateTargetResolution(int width, int height) { |
| 146 RTC_DCHECK_GE(downscale_shift_, 0); | 158 if (width < kMinDownscaleDimension || height < kMinDownscaleDimension) { |
| 147 int shifts_performed = 0; | 159 maximum_shift_ = 0; |
| 148 for (int shift = downscale_shift_; | 160 } else { |
| 149 shift > 0 && (frame_width / 2 >= kMinDownscaleDimension) && | 161 maximum_shift_ = static_cast<int>( |
| 150 (frame_height / 2 >= kMinDownscaleDimension); | 162 std::log2(std::min(width, height)/kMinDownscaleDimension)); |
|
magjed_webrtc
2016/09/05 15:01:52
nit: formatting. Use git cl format.
| |
| 151 --shift, ++shifts_performed) { | |
| 152 frame_width /= 2; | |
| 153 frame_height /= 2; | |
| 154 } | 163 } |
| 155 // Clamp to number of shifts actually performed to not be stuck trying to | 164 res_ = Resolution{width, height}; |
|
magjed_webrtc
2016/09/05 15:01:52
Maybe this variable should be called target_resolu
| |
| 156 // scale way beyond QVGA. | |
| 157 downscale_shift_ = shifts_performed; | |
| 158 if (res_.width == frame_width && res_.height == frame_height) { | |
| 159 // No reset done/needed, using same resolution. | |
| 160 return; | |
| 161 } | |
| 162 res_.width = frame_width; | |
| 163 res_.height = frame_height; | |
| 164 ClearSamples(); | |
| 165 } | 165 } |
| 166 | 166 |
| 167 void QualityScaler::ClearSamples() { | 167 void QualityScaler::ClearSamples() { |
| 168 framedrop_percent_.Reset(); | 168 framedrop_percent_.Reset(); |
| 169 average_qp_downscale_.Reset(); | 169 average_qp_.Reset(); |
| 170 average_qp_upscale_.Reset(); | |
| 171 } | 170 } |
| 172 | 171 |
| 173 void QualityScaler::UpdateSampleCounts() { | |
| 174 num_samples_downscale_ = static_cast<size_t>( | |
| 175 kMeasureSecondsDownscale * (framerate_ < kMinFps ? kMinFps : framerate_)); | |
| 176 num_samples_upscale_ = static_cast<size_t>( | |
| 177 measure_seconds_upscale_ * (framerate_ < kMinFps ? kMinFps : framerate_)); | |
| 178 } | |
| 179 | |
| 180 void QualityScaler::AdjustScale(bool up) { | |
| 181 downscale_shift_ += up ? -1 : 1; | |
| 182 if (downscale_shift_ < 0) | |
| 183 downscale_shift_ = 0; | |
| 184 if (!up) { | |
| 185 // First downscale hit, start using a slower threshold for going up. | |
| 186 measure_seconds_upscale_ = kMeasureSecondsUpscale; | |
| 187 UpdateSampleCounts(); | |
| 188 } | |
| 189 } | |
| 190 | 172 |
| 191 } // namespace webrtc | 173 } // namespace webrtc |
| OLD | NEW |