blob: 99bc6dad223bdaa58fcfcb56da6d451cf0ab0ad4 [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
kthelgason194f40a2016-09-14 02:14:58 -070013#include <algorithm>
14#include <cmath>
15
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000016namespace webrtc {
17
Peter Boström926dfcd2016-04-14 14:48:10 +020018namespace {
pboscbac40d2016-04-13 02:51:02 -070019// Threshold constant used until first downscale (to permit fast rampup).
20static const int kMeasureSecondsFastUpscale = 2;
21static const int kMeasureSecondsUpscale = 5;
Peter Boström2c8a2962016-04-18 12:58:02 +020022static const int kMeasureSecondsDownscale = 5;
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000023static const int kFramedropPercentThreshold = 60;
Peter Boström926dfcd2016-04-14 14:48:10 +020024// Min width/height to downscale to, set to not go below QVGA, but with some
25// margin to permit "almost-QVGA" resolutions, such as QCIF.
26static const int kMinDownscaleDimension = 140;
Peter Boströmb9e77092016-04-18 22:45:55 +020027// Initial resolutions corresponding to a bitrate. Aa bit above their actual
28// values to permit near-VGA and near-QVGA resolutions to use the same
29// mechanism.
30static const int kVgaBitrateThresholdKbps = 500;
31static const int kVgaNumPixels = 700 * 500; // 640x480
32static const int kQvgaBitrateThresholdKbps = 250;
33static const int kQvgaNumPixels = 400 * 300; // 320x240
Peter Boström926dfcd2016-04-14 14:48:10 +020034} // namespace
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000035
pbos1f534522016-05-13 11:05:35 -070036// QP thresholds are chosen to be high enough to be hit in practice when quality
37// is good, but also low enough to not cause a flip-flop behavior (e.g. going up
38// in resolution shouldn't give so bad quality that we should go back down).
39
40const int QualityScaler::kLowVp8QpThreshold = 29;
41const int QualityScaler::kBadVp8QpThreshold = 95;
42
tkchin6c687e72016-08-15 16:37:56 -070043#if defined(WEBRTC_IOS)
tkchin6a450102016-08-08 15:27:36 -070044const int QualityScaler::kLowH264QpThreshold = 32;
45const int QualityScaler::kBadH264QpThreshold = 42;
tkchin6c687e72016-08-15 16:37:56 -070046#else
glaznev36a06a92016-08-24 12:09:16 -070047const int QualityScaler::kLowH264QpThreshold = 24;
48const int QualityScaler::kBadH264QpThreshold = 37;
tkchin6c687e72016-08-15 16:37:56 -070049#endif
pbos1f534522016-05-13 11:05:35 -070050
kthelgason194f40a2016-09-14 02:14:58 -070051// Default values. Should immediately get set to something more sensible.
52QualityScaler::QualityScaler()
53 : average_qp_(kMeasureSecondsUpscale * 30),
54 framedrop_percent_(kMeasureSecondsUpscale * 30),
55 low_qp_threshold_(-1) {}
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000056
Peter Boström17417702015-09-25 17:03:26 +020057void QualityScaler::Init(int low_qp_threshold,
58 int high_qp_threshold,
Alex Glazneva9d08922016-02-19 15:24:06 -080059 int initial_bitrate_kbps,
60 int width,
pboscbac40d2016-04-13 02:51:02 -070061 int height,
62 int fps) {
jackychen98d8cf52015-05-21 11:12:02 -070063 low_qp_threshold_ = low_qp_threshold;
Peter Boström17417702015-09-25 17:03:26 +020064 high_qp_threshold_ = high_qp_threshold;
Peter Boström01bcbd02016-03-22 21:44:29 +010065 downscale_shift_ = 0;
kthelgason194f40a2016-09-14 02:14:58 -070066
67 fast_rampup_ = true;
68
69 ClearSamples();
70 ReportFramerate(fps);
71
Peter Boström01bcbd02016-03-22 21:44:29 +010072 const int init_width = width;
73 const int init_height = height;
Peter Boströmb9e77092016-04-18 22:45:55 +020074 if (initial_bitrate_kbps > 0) {
75 int init_num_pixels = width * height;
76 if (initial_bitrate_kbps < kVgaBitrateThresholdKbps)
77 init_num_pixels = kVgaNumPixels;
78 if (initial_bitrate_kbps < kQvgaBitrateThresholdKbps)
79 init_num_pixels = kQvgaNumPixels;
80 while (width * height > init_num_pixels) {
Alex Glazneva9d08922016-02-19 15:24:06 -080081 ++downscale_shift_;
82 width /= 2;
83 height /= 2;
84 }
85 }
Peter Boström01bcbd02016-03-22 21:44:29 +010086 UpdateTargetResolution(init_width, init_height);
pboscbac40d2016-04-13 02:51:02 -070087 ReportFramerate(fps);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000088}
89
jackychen98d8cf52015-05-21 11:12:02 -070090// Report framerate(fps) to estimate # of samples.
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000091void QualityScaler::ReportFramerate(int framerate) {
kthelgason194f40a2016-09-14 02:14:58 -070092 // Use a faster window for upscaling initially.
93 // This enables faster initial rampups without risking strong up-down
94 // behavior later.
95 num_samples_upscale_ = framerate * (fast_rampup_ ? kMeasureSecondsFastUpscale
96 : kMeasureSecondsUpscale);
97 num_samples_downscale_ = framerate * kMeasureSecondsDownscale;
98
99 average_qp_ =
100 MovingAverage(std::max(num_samples_upscale_, num_samples_downscale_));
101 framedrop_percent_ =
102 MovingAverage(std::max(num_samples_upscale_, num_samples_downscale_));
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000103}
104
jackychen98d8cf52015-05-21 11:12:02 -0700105void QualityScaler::ReportQP(int qp) {
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000106 framedrop_percent_.AddSample(0);
kthelgason194f40a2016-09-14 02:14:58 -0700107 average_qp_.AddSample(qp);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000108}
109
110void QualityScaler::ReportDroppedFrame() {
111 framedrop_percent_.AddSample(100);
112}
113
Niels Möller718a7632016-06-13 13:06:01 +0200114void QualityScaler::OnEncodeFrame(int width, int height) {
jackychen61b4d512015-04-21 15:30:11 -0700115 // Should be set through InitEncode -> Should be set by now.
Niels Möller718a7632016-06-13 13:06:01 +0200116 RTC_DCHECK_GE(low_qp_threshold_, 0);
kthelgason194f40a2016-09-14 02:14:58 -0700117 if (target_res_.width != width || target_res_.height != height) {
118 UpdateTargetResolution(width, height);
jackychen61b4d512015-04-21 15:30:11 -0700119 }
kthelgason194f40a2016-09-14 02:14:58 -0700120
121 // Check if we should scale down due to high frame drop.
122 const auto drop_rate = framedrop_percent_.GetAverage(num_samples_downscale_);
123 if (drop_rate && *drop_rate >= kFramedropPercentThreshold) {
124 ScaleDown();
125 return;
126 }
127
128 // Check if we should scale up or down based on QP.
129 const auto avg_qp_down = average_qp_.GetAverage(num_samples_downscale_);
130 if (avg_qp_down && *avg_qp_down > high_qp_threshold_) {
131 ScaleDown();
132 return;
133 }
134 const auto avg_qp_up = average_qp_.GetAverage(num_samples_upscale_);
135 if (avg_qp_up && *avg_qp_up <= low_qp_threshold_) {
136 // QP has been low. We want to try a higher resolution.
137 ScaleUp();
138 return;
139 }
140}
141
142void QualityScaler::ScaleUp() {
143 downscale_shift_ = std::max(0, downscale_shift_ - 1);
144 ClearSamples();
145}
146
147void QualityScaler::ScaleDown() {
148 downscale_shift_ = std::min(maximum_shift_, downscale_shift_ + 1);
149 ClearSamples();
150 // If we've scaled down, wait longer before scaling up again.
151 if (fast_rampup_) {
152 fast_rampup_ = false;
153 num_samples_upscale_ = (num_samples_upscale_ / kMeasureSecondsFastUpscale) *
154 kMeasureSecondsUpscale;
155 }
jackychen6e2ce6e2015-07-13 16:26:33 -0700156}
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000157
jackychen6e2ce6e2015-07-13 16:26:33 -0700158QualityScaler::Resolution QualityScaler::GetScaledResolution() const {
kthelgason194f40a2016-09-14 02:14:58 -0700159 const int frame_width = target_res_.width >> downscale_shift_;
160 const int frame_height = target_res_.height >> downscale_shift_;
161 return Resolution{frame_width, frame_height};
jackychen6e2ce6e2015-07-13 16:26:33 -0700162}
163
Niels Möller718a7632016-06-13 13:06:01 +0200164rtc::scoped_refptr<VideoFrameBuffer> QualityScaler::GetScaledBuffer(
165 const rtc::scoped_refptr<VideoFrameBuffer>& frame) {
jackychen6e2ce6e2015-07-13 16:26:33 -0700166 Resolution res = GetScaledResolution();
kthelgason194f40a2016-09-14 02:14:58 -0700167 const int src_width = frame->width();
168 const int src_height = frame->height();
Niels Möller718a7632016-06-13 13:06:01 +0200169
170 if (res.width == src_width && res.height == src_height)
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000171 return frame;
Niels Möller718a7632016-06-13 13:06:01 +0200172 rtc::scoped_refptr<I420Buffer> scaled_buffer =
173 pool_.CreateBuffer(res.width, res.height);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000174
Niels Möller718a7632016-06-13 13:06:01 +0200175 scaled_buffer->ScaleFrom(frame);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000176
Niels Möller718a7632016-06-13 13:06:01 +0200177 return scaled_buffer;
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000178}
179
kthelgason194f40a2016-09-14 02:14:58 -0700180void QualityScaler::UpdateTargetResolution(int width, int height) {
181 if (width < kMinDownscaleDimension || height < kMinDownscaleDimension) {
182 maximum_shift_ = 0;
183 } else {
184 maximum_shift_ = static_cast<int>(
185 std::log2(std::min(width, height) / kMinDownscaleDimension));
Peter Boström919288f2016-05-12 02:17:35 +0200186 }
kthelgason194f40a2016-09-14 02:14:58 -0700187 target_res_ = Resolution{width, height};
Peter Boström01bcbd02016-03-22 21:44:29 +0100188}
189
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000190void QualityScaler::ClearSamples() {
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000191 framedrop_percent_.Reset();
kthelgason194f40a2016-09-14 02:14:58 -0700192 average_qp_.Reset();
pboscbac40d2016-04-13 02:51:02 -0700193}
194
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000195
196} // namespace webrtc