pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
Niels Möller | 718a763 | 2016-06-13 13:06:01 +0200 | [diff] [blame] | 10 | |
kjellander@webrtc.org | b7ce964 | 2015-11-18 23:04:10 +0100 | [diff] [blame] | 11 | #include "webrtc/modules/video_coding/utility/quality_scaler.h" |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 12 | |
Kári Tristan Helgason | 5a20ed3 | 2016-09-15 10:56:19 +0200 | [diff] [blame] | 13 | #include <math.h> |
| 14 | |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 15 | #include <algorithm> |
Kári Tristan Helgason | 5a20ed3 | 2016-09-15 10:56:19 +0200 | [diff] [blame] | 16 | |
kthelgason | 478681e | 2016-09-28 08:17:43 -0700 | [diff] [blame] | 17 | #include "webrtc/base/checks.h" |
| 18 | |
Kári Tristan Helgason | 5a20ed3 | 2016-09-15 10:56:19 +0200 | [diff] [blame] | 19 | // TODO(kthelgason): Some versions of Android have issues with log2. |
| 20 | // See https://code.google.com/p/android/issues/detail?id=212634 for details |
| 21 | #if defined(WEBRTC_ANDROID) |
| 22 | #define log2(x) (log(x) / log(2)) |
| 23 | #endif |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 24 | |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 25 | namespace webrtc { |
| 26 | |
Peter Boström | 926dfcd | 2016-04-14 14:48:10 +0200 | [diff] [blame] | 27 | namespace { |
pbos | cbac40d | 2016-04-13 02:51:02 -0700 | [diff] [blame] | 28 | // Threshold constant used until first downscale (to permit fast rampup). |
| 29 | static const int kMeasureSecondsFastUpscale = 2; |
| 30 | static const int kMeasureSecondsUpscale = 5; |
Peter Boström | 2c8a296 | 2016-04-18 12:58:02 +0200 | [diff] [blame] | 31 | static const int kMeasureSecondsDownscale = 5; |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 32 | static const int kFramedropPercentThreshold = 60; |
Peter Boström | 926dfcd | 2016-04-14 14:48:10 +0200 | [diff] [blame] | 33 | // Min width/height to downscale to, set to not go below QVGA, but with some |
| 34 | // margin to permit "almost-QVGA" resolutions, such as QCIF. |
| 35 | static const int kMinDownscaleDimension = 140; |
Peter Boström | b9e7709 | 2016-04-18 22:45:55 +0200 | [diff] [blame] | 36 | // Initial resolutions corresponding to a bitrate. Aa bit above their actual |
| 37 | // values to permit near-VGA and near-QVGA resolutions to use the same |
| 38 | // mechanism. |
| 39 | static const int kVgaBitrateThresholdKbps = 500; |
| 40 | static const int kVgaNumPixels = 700 * 500; // 640x480 |
| 41 | static const int kQvgaBitrateThresholdKbps = 250; |
| 42 | static const int kQvgaNumPixels = 400 * 300; // 320x240 |
kthelgason | 478681e | 2016-09-28 08:17:43 -0700 | [diff] [blame] | 43 | |
| 44 | // QP scaling threshold defaults: |
| 45 | static const int kLowH264QpThreshold = 24; |
| 46 | static const int kHighH264QpThreshold = 37; |
| 47 | // QP is obtained from VP8-bitstream for HW, so the QP corresponds to the |
| 48 | // bitstream range of [0, 127] and not the user-level range of [0,63]. |
| 49 | static const int kLowVp8QpThreshold = 29; |
| 50 | static const int kHighVp8QpThreshold = 95; |
Peter Boström | 926dfcd | 2016-04-14 14:48:10 +0200 | [diff] [blame] | 51 | } // namespace |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 52 | |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 53 | // Default values. Should immediately get set to something more sensible. |
| 54 | QualityScaler::QualityScaler() |
| 55 | : average_qp_(kMeasureSecondsUpscale * 30), |
| 56 | framedrop_percent_(kMeasureSecondsUpscale * 30), |
| 57 | low_qp_threshold_(-1) {} |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 58 | |
kthelgason | 478681e | 2016-09-28 08:17:43 -0700 | [diff] [blame] | 59 | void QualityScaler::Init(VideoCodecType codec_type, |
| 60 | int initial_bitrate_kbps, |
| 61 | int width, |
| 62 | int height, |
| 63 | int fps) { |
| 64 | int low = -1, high = -1; |
| 65 | switch (codec_type) { |
| 66 | case kVideoCodecH264: |
| 67 | low = kLowH264QpThreshold; |
| 68 | high = kHighH264QpThreshold; |
| 69 | break; |
| 70 | case kVideoCodecVP8: |
| 71 | low = kLowVp8QpThreshold; |
| 72 | high = kHighVp8QpThreshold; |
| 73 | break; |
| 74 | default: |
| 75 | RTC_NOTREACHED() << "Invalid codec type for QualityScaler."; |
| 76 | } |
| 77 | Init(low, high, initial_bitrate_kbps, width, height, fps); |
| 78 | } |
| 79 | |
Peter Boström | 1741770 | 2015-09-25 17:03:26 +0200 | [diff] [blame] | 80 | void QualityScaler::Init(int low_qp_threshold, |
| 81 | int high_qp_threshold, |
Alex Glaznev | a9d0892 | 2016-02-19 15:24:06 -0800 | [diff] [blame] | 82 | int initial_bitrate_kbps, |
| 83 | int width, |
pbos | cbac40d | 2016-04-13 02:51:02 -0700 | [diff] [blame] | 84 | int height, |
| 85 | int fps) { |
kthelgason | 478681e | 2016-09-28 08:17:43 -0700 | [diff] [blame] | 86 | ClearSamples(); |
jackychen | 98d8cf5 | 2015-05-21 11:12:02 -0700 | [diff] [blame] | 87 | low_qp_threshold_ = low_qp_threshold; |
Peter Boström | 1741770 | 2015-09-25 17:03:26 +0200 | [diff] [blame] | 88 | high_qp_threshold_ = high_qp_threshold; |
Peter Boström | 01bcbd0 | 2016-03-22 21:44:29 +0100 | [diff] [blame] | 89 | downscale_shift_ = 0; |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 90 | fast_rampup_ = true; |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 91 | |
Peter Boström | 01bcbd0 | 2016-03-22 21:44:29 +0100 | [diff] [blame] | 92 | const int init_width = width; |
| 93 | const int init_height = height; |
Peter Boström | b9e7709 | 2016-04-18 22:45:55 +0200 | [diff] [blame] | 94 | if (initial_bitrate_kbps > 0) { |
| 95 | int init_num_pixels = width * height; |
| 96 | if (initial_bitrate_kbps < kVgaBitrateThresholdKbps) |
| 97 | init_num_pixels = kVgaNumPixels; |
| 98 | if (initial_bitrate_kbps < kQvgaBitrateThresholdKbps) |
| 99 | init_num_pixels = kQvgaNumPixels; |
| 100 | while (width * height > init_num_pixels) { |
Alex Glaznev | a9d0892 | 2016-02-19 15:24:06 -0800 | [diff] [blame] | 101 | ++downscale_shift_; |
| 102 | width /= 2; |
| 103 | height /= 2; |
| 104 | } |
| 105 | } |
Peter Boström | 01bcbd0 | 2016-03-22 21:44:29 +0100 | [diff] [blame] | 106 | UpdateTargetResolution(init_width, init_height); |
pbos | cbac40d | 2016-04-13 02:51:02 -0700 | [diff] [blame] | 107 | ReportFramerate(fps); |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 108 | } |
| 109 | |
jackychen | 98d8cf5 | 2015-05-21 11:12:02 -0700 | [diff] [blame] | 110 | // Report framerate(fps) to estimate # of samples. |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 111 | void QualityScaler::ReportFramerate(int framerate) { |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 112 | // Use a faster window for upscaling initially. |
| 113 | // This enables faster initial rampups without risking strong up-down |
| 114 | // behavior later. |
| 115 | num_samples_upscale_ = framerate * (fast_rampup_ ? kMeasureSecondsFastUpscale |
| 116 | : kMeasureSecondsUpscale); |
| 117 | num_samples_downscale_ = framerate * kMeasureSecondsDownscale; |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 118 | } |
| 119 | |
jackychen | 98d8cf5 | 2015-05-21 11:12:02 -0700 | [diff] [blame] | 120 | void QualityScaler::ReportQP(int qp) { |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 121 | framedrop_percent_.AddSample(0); |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 122 | average_qp_.AddSample(qp); |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | void QualityScaler::ReportDroppedFrame() { |
| 126 | framedrop_percent_.AddSample(100); |
| 127 | } |
| 128 | |
Niels Möller | 718a763 | 2016-06-13 13:06:01 +0200 | [diff] [blame] | 129 | void QualityScaler::OnEncodeFrame(int width, int height) { |
jackychen | 61b4d51 | 2015-04-21 15:30:11 -0700 | [diff] [blame] | 130 | // Should be set through InitEncode -> Should be set by now. |
Niels Möller | 718a763 | 2016-06-13 13:06:01 +0200 | [diff] [blame] | 131 | RTC_DCHECK_GE(low_qp_threshold_, 0); |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 132 | if (target_res_.width != width || target_res_.height != height) { |
| 133 | UpdateTargetResolution(width, height); |
jackychen | 61b4d51 | 2015-04-21 15:30:11 -0700 | [diff] [blame] | 134 | } |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 135 | |
| 136 | // Check if we should scale down due to high frame drop. |
| 137 | const auto drop_rate = framedrop_percent_.GetAverage(num_samples_downscale_); |
| 138 | if (drop_rate && *drop_rate >= kFramedropPercentThreshold) { |
| 139 | ScaleDown(); |
| 140 | return; |
| 141 | } |
| 142 | |
| 143 | // Check if we should scale up or down based on QP. |
| 144 | const auto avg_qp_down = average_qp_.GetAverage(num_samples_downscale_); |
| 145 | if (avg_qp_down && *avg_qp_down > high_qp_threshold_) { |
| 146 | ScaleDown(); |
| 147 | return; |
| 148 | } |
| 149 | const auto avg_qp_up = average_qp_.GetAverage(num_samples_upscale_); |
| 150 | if (avg_qp_up && *avg_qp_up <= low_qp_threshold_) { |
| 151 | // QP has been low. We want to try a higher resolution. |
| 152 | ScaleUp(); |
| 153 | return; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | void QualityScaler::ScaleUp() { |
| 158 | downscale_shift_ = std::max(0, downscale_shift_ - 1); |
| 159 | ClearSamples(); |
| 160 | } |
| 161 | |
| 162 | void QualityScaler::ScaleDown() { |
| 163 | downscale_shift_ = std::min(maximum_shift_, downscale_shift_ + 1); |
| 164 | ClearSamples(); |
| 165 | // If we've scaled down, wait longer before scaling up again. |
| 166 | if (fast_rampup_) { |
| 167 | fast_rampup_ = false; |
| 168 | num_samples_upscale_ = (num_samples_upscale_ / kMeasureSecondsFastUpscale) * |
| 169 | kMeasureSecondsUpscale; |
| 170 | } |
jackychen | 6e2ce6e | 2015-07-13 16:26:33 -0700 | [diff] [blame] | 171 | } |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 172 | |
jackychen | 6e2ce6e | 2015-07-13 16:26:33 -0700 | [diff] [blame] | 173 | QualityScaler::Resolution QualityScaler::GetScaledResolution() const { |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 174 | const int frame_width = target_res_.width >> downscale_shift_; |
| 175 | const int frame_height = target_res_.height >> downscale_shift_; |
| 176 | return Resolution{frame_width, frame_height}; |
jackychen | 6e2ce6e | 2015-07-13 16:26:33 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Niels Möller | 718a763 | 2016-06-13 13:06:01 +0200 | [diff] [blame] | 179 | rtc::scoped_refptr<VideoFrameBuffer> QualityScaler::GetScaledBuffer( |
| 180 | const rtc::scoped_refptr<VideoFrameBuffer>& frame) { |
jackychen | 6e2ce6e | 2015-07-13 16:26:33 -0700 | [diff] [blame] | 181 | Resolution res = GetScaledResolution(); |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 182 | const int src_width = frame->width(); |
| 183 | const int src_height = frame->height(); |
Niels Möller | 718a763 | 2016-06-13 13:06:01 +0200 | [diff] [blame] | 184 | |
| 185 | if (res.width == src_width && res.height == src_height) |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 186 | return frame; |
Niels Möller | 718a763 | 2016-06-13 13:06:01 +0200 | [diff] [blame] | 187 | rtc::scoped_refptr<I420Buffer> scaled_buffer = |
| 188 | pool_.CreateBuffer(res.width, res.height); |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 189 | |
nisse | e3fe4a7 | 2016-11-10 08:44:38 -0800 | [diff] [blame] | 190 | scaled_buffer->ScaleFrom(*frame); |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 191 | |
Niels Möller | 718a763 | 2016-06-13 13:06:01 +0200 | [diff] [blame] | 192 | return scaled_buffer; |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 193 | } |
| 194 | |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 195 | void QualityScaler::UpdateTargetResolution(int width, int height) { |
| 196 | if (width < kMinDownscaleDimension || height < kMinDownscaleDimension) { |
| 197 | maximum_shift_ = 0; |
| 198 | } else { |
| 199 | maximum_shift_ = static_cast<int>( |
Kári Tristan Helgason | 5a20ed3 | 2016-09-15 10:56:19 +0200 | [diff] [blame] | 200 | log2(std::min(width, height) / kMinDownscaleDimension)); |
Peter Boström | 919288f | 2016-05-12 02:17:35 +0200 | [diff] [blame] | 201 | } |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 202 | target_res_ = Resolution{width, height}; |
Peter Boström | 01bcbd0 | 2016-03-22 21:44:29 +0100 | [diff] [blame] | 203 | } |
| 204 | |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 205 | void QualityScaler::ClearSamples() { |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 206 | framedrop_percent_.Reset(); |
kthelgason | 194f40a | 2016-09-14 02:14:58 -0700 | [diff] [blame] | 207 | average_qp_.Reset(); |
pbos | cbac40d | 2016-04-13 02:51:02 -0700 | [diff] [blame] | 208 | } |
| 209 | |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 210 | |
| 211 | } // namespace webrtc |