blob: 55cb274209565687ccf146477f6ff8928a85c6fd [file] [log] [blame]
pbos@webrtc.orga0d78272014-09-12 11:51:47 +00001/*
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öller718a7632016-06-13 13:06:01 +020010
kjellander@webrtc.orgb7ce9642015-11-18 23:04:10 +010011#include "webrtc/modules/video_coding/utility/quality_scaler.h"
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000012
Kári Tristan Helgason5a20ed32016-09-15 10:56:19 +020013#include <math.h>
14
kthelgason194f40a2016-09-14 02:14:58 -070015#include <algorithm>
Kári Tristan Helgason5a20ed32016-09-15 10:56:19 +020016
kthelgason478681e2016-09-28 08:17:43 -070017#include "webrtc/base/checks.h"
18
Kári Tristan Helgason5a20ed32016-09-15 10:56:19 +020019// 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
kthelgason194f40a2016-09-14 02:14:58 -070024
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000025namespace webrtc {
26
Peter Boström926dfcd2016-04-14 14:48:10 +020027namespace {
pboscbac40d2016-04-13 02:51:02 -070028// Threshold constant used until first downscale (to permit fast rampup).
29static const int kMeasureSecondsFastUpscale = 2;
30static const int kMeasureSecondsUpscale = 5;
Peter Boström2c8a2962016-04-18 12:58:02 +020031static const int kMeasureSecondsDownscale = 5;
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000032static const int kFramedropPercentThreshold = 60;
Peter Boström926dfcd2016-04-14 14:48:10 +020033// 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.
35static const int kMinDownscaleDimension = 140;
Peter Boströmb9e77092016-04-18 22:45:55 +020036// 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.
39static const int kVgaBitrateThresholdKbps = 500;
40static const int kVgaNumPixels = 700 * 500; // 640x480
41static const int kQvgaBitrateThresholdKbps = 250;
42static const int kQvgaNumPixels = 400 * 300; // 320x240
kthelgason478681e2016-09-28 08:17:43 -070043
44// QP scaling threshold defaults:
45static const int kLowH264QpThreshold = 24;
46static 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].
49static const int kLowVp8QpThreshold = 29;
50static const int kHighVp8QpThreshold = 95;
Peter Boström926dfcd2016-04-14 14:48:10 +020051} // namespace
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000052
kthelgason194f40a2016-09-14 02:14:58 -070053// Default values. Should immediately get set to something more sensible.
54QualityScaler::QualityScaler()
55 : average_qp_(kMeasureSecondsUpscale * 30),
56 framedrop_percent_(kMeasureSecondsUpscale * 30),
57 low_qp_threshold_(-1) {}
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000058
kthelgason478681e2016-09-28 08:17:43 -070059void 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öm17417702015-09-25 17:03:26 +020080void QualityScaler::Init(int low_qp_threshold,
81 int high_qp_threshold,
Alex Glazneva9d08922016-02-19 15:24:06 -080082 int initial_bitrate_kbps,
83 int width,
pboscbac40d2016-04-13 02:51:02 -070084 int height,
85 int fps) {
kthelgason478681e2016-09-28 08:17:43 -070086 ClearSamples();
jackychen98d8cf52015-05-21 11:12:02 -070087 low_qp_threshold_ = low_qp_threshold;
Peter Boström17417702015-09-25 17:03:26 +020088 high_qp_threshold_ = high_qp_threshold;
Peter Boström01bcbd02016-03-22 21:44:29 +010089 downscale_shift_ = 0;
kthelgason194f40a2016-09-14 02:14:58 -070090 fast_rampup_ = true;
kthelgason194f40a2016-09-14 02:14:58 -070091 ReportFramerate(fps);
92
Peter Boström01bcbd02016-03-22 21:44:29 +010093 const int init_width = width;
94 const int init_height = height;
Peter Boströmb9e77092016-04-18 22:45:55 +020095 if (initial_bitrate_kbps > 0) {
96 int init_num_pixels = width * height;
97 if (initial_bitrate_kbps < kVgaBitrateThresholdKbps)
98 init_num_pixels = kVgaNumPixels;
99 if (initial_bitrate_kbps < kQvgaBitrateThresholdKbps)
100 init_num_pixels = kQvgaNumPixels;
101 while (width * height > init_num_pixels) {
Alex Glazneva9d08922016-02-19 15:24:06 -0800102 ++downscale_shift_;
103 width /= 2;
104 height /= 2;
105 }
106 }
Peter Boström01bcbd02016-03-22 21:44:29 +0100107 UpdateTargetResolution(init_width, init_height);
pboscbac40d2016-04-13 02:51:02 -0700108 ReportFramerate(fps);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000109}
110
jackychen98d8cf52015-05-21 11:12:02 -0700111// Report framerate(fps) to estimate # of samples.
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000112void QualityScaler::ReportFramerate(int framerate) {
kthelgason194f40a2016-09-14 02:14:58 -0700113 // Use a faster window for upscaling initially.
114 // This enables faster initial rampups without risking strong up-down
115 // behavior later.
116 num_samples_upscale_ = framerate * (fast_rampup_ ? kMeasureSecondsFastUpscale
117 : kMeasureSecondsUpscale);
118 num_samples_downscale_ = framerate * kMeasureSecondsDownscale;
119
120 average_qp_ =
121 MovingAverage(std::max(num_samples_upscale_, num_samples_downscale_));
122 framedrop_percent_ =
123 MovingAverage(std::max(num_samples_upscale_, num_samples_downscale_));
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000124}
125
jackychen98d8cf52015-05-21 11:12:02 -0700126void QualityScaler::ReportQP(int qp) {
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000127 framedrop_percent_.AddSample(0);
kthelgason194f40a2016-09-14 02:14:58 -0700128 average_qp_.AddSample(qp);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000129}
130
131void QualityScaler::ReportDroppedFrame() {
132 framedrop_percent_.AddSample(100);
133}
134
Niels Möller718a7632016-06-13 13:06:01 +0200135void QualityScaler::OnEncodeFrame(int width, int height) {
jackychen61b4d512015-04-21 15:30:11 -0700136 // Should be set through InitEncode -> Should be set by now.
Niels Möller718a7632016-06-13 13:06:01 +0200137 RTC_DCHECK_GE(low_qp_threshold_, 0);
kthelgason194f40a2016-09-14 02:14:58 -0700138 if (target_res_.width != width || target_res_.height != height) {
139 UpdateTargetResolution(width, height);
jackychen61b4d512015-04-21 15:30:11 -0700140 }
kthelgason194f40a2016-09-14 02:14:58 -0700141
142 // Check if we should scale down due to high frame drop.
143 const auto drop_rate = framedrop_percent_.GetAverage(num_samples_downscale_);
144 if (drop_rate && *drop_rate >= kFramedropPercentThreshold) {
145 ScaleDown();
146 return;
147 }
148
149 // Check if we should scale up or down based on QP.
150 const auto avg_qp_down = average_qp_.GetAverage(num_samples_downscale_);
151 if (avg_qp_down && *avg_qp_down > high_qp_threshold_) {
152 ScaleDown();
153 return;
154 }
155 const auto avg_qp_up = average_qp_.GetAverage(num_samples_upscale_);
156 if (avg_qp_up && *avg_qp_up <= low_qp_threshold_) {
157 // QP has been low. We want to try a higher resolution.
158 ScaleUp();
159 return;
160 }
161}
162
163void QualityScaler::ScaleUp() {
164 downscale_shift_ = std::max(0, downscale_shift_ - 1);
165 ClearSamples();
166}
167
168void QualityScaler::ScaleDown() {
169 downscale_shift_ = std::min(maximum_shift_, downscale_shift_ + 1);
170 ClearSamples();
171 // If we've scaled down, wait longer before scaling up again.
172 if (fast_rampup_) {
173 fast_rampup_ = false;
174 num_samples_upscale_ = (num_samples_upscale_ / kMeasureSecondsFastUpscale) *
175 kMeasureSecondsUpscale;
176 }
jackychen6e2ce6e2015-07-13 16:26:33 -0700177}
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000178
jackychen6e2ce6e2015-07-13 16:26:33 -0700179QualityScaler::Resolution QualityScaler::GetScaledResolution() const {
kthelgason194f40a2016-09-14 02:14:58 -0700180 const int frame_width = target_res_.width >> downscale_shift_;
181 const int frame_height = target_res_.height >> downscale_shift_;
182 return Resolution{frame_width, frame_height};
jackychen6e2ce6e2015-07-13 16:26:33 -0700183}
184
Niels Möller718a7632016-06-13 13:06:01 +0200185rtc::scoped_refptr<VideoFrameBuffer> QualityScaler::GetScaledBuffer(
186 const rtc::scoped_refptr<VideoFrameBuffer>& frame) {
jackychen6e2ce6e2015-07-13 16:26:33 -0700187 Resolution res = GetScaledResolution();
kthelgason194f40a2016-09-14 02:14:58 -0700188 const int src_width = frame->width();
189 const int src_height = frame->height();
Niels Möller718a7632016-06-13 13:06:01 +0200190
191 if (res.width == src_width && res.height == src_height)
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000192 return frame;
Niels Möller718a7632016-06-13 13:06:01 +0200193 rtc::scoped_refptr<I420Buffer> scaled_buffer =
194 pool_.CreateBuffer(res.width, res.height);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000195
Niels Möller718a7632016-06-13 13:06:01 +0200196 scaled_buffer->ScaleFrom(frame);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000197
Niels Möller718a7632016-06-13 13:06:01 +0200198 return scaled_buffer;
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000199}
200
kthelgason194f40a2016-09-14 02:14:58 -0700201void QualityScaler::UpdateTargetResolution(int width, int height) {
202 if (width < kMinDownscaleDimension || height < kMinDownscaleDimension) {
203 maximum_shift_ = 0;
204 } else {
205 maximum_shift_ = static_cast<int>(
Kári Tristan Helgason5a20ed32016-09-15 10:56:19 +0200206 log2(std::min(width, height) / kMinDownscaleDimension));
Peter Boström919288f2016-05-12 02:17:35 +0200207 }
kthelgason194f40a2016-09-14 02:14:58 -0700208 target_res_ = Resolution{width, height};
Peter Boström01bcbd02016-03-22 21:44:29 +0100209}
210
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000211void QualityScaler::ClearSamples() {
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000212 framedrop_percent_.Reset();
kthelgason194f40a2016-09-14 02:14:58 -0700213 average_qp_.Reset();
pboscbac40d2016-04-13 02:51:02 -0700214}
215
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000216
217} // namespace webrtc