blob: 7ef7c575ba4b699210fca7c4e40f0b557a54d440 [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
17// TODO(kthelgason): Some versions of Android have issues with log2.
18// See https://code.google.com/p/android/issues/detail?id=212634 for details
19#if defined(WEBRTC_ANDROID)
20#define log2(x) (log(x) / log(2))
21#endif
kthelgason194f40a2016-09-14 02:14:58 -070022
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000023namespace webrtc {
24
Peter Boström926dfcd2016-04-14 14:48:10 +020025namespace {
pboscbac40d2016-04-13 02:51:02 -070026// Threshold constant used until first downscale (to permit fast rampup).
27static const int kMeasureSecondsFastUpscale = 2;
28static const int kMeasureSecondsUpscale = 5;
Peter Boström2c8a2962016-04-18 12:58:02 +020029static const int kMeasureSecondsDownscale = 5;
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000030static const int kFramedropPercentThreshold = 60;
Peter Boström926dfcd2016-04-14 14:48:10 +020031// Min width/height to downscale to, set to not go below QVGA, but with some
32// margin to permit "almost-QVGA" resolutions, such as QCIF.
33static const int kMinDownscaleDimension = 140;
Peter Boströmb9e77092016-04-18 22:45:55 +020034// Initial resolutions corresponding to a bitrate. Aa bit above their actual
35// values to permit near-VGA and near-QVGA resolutions to use the same
36// mechanism.
37static const int kVgaBitrateThresholdKbps = 500;
38static const int kVgaNumPixels = 700 * 500; // 640x480
39static const int kQvgaBitrateThresholdKbps = 250;
40static const int kQvgaNumPixels = 400 * 300; // 320x240
Peter Boström926dfcd2016-04-14 14:48:10 +020041} // namespace
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000042
pbos1f534522016-05-13 11:05:35 -070043// QP thresholds are chosen to be high enough to be hit in practice when quality
44// is good, but also low enough to not cause a flip-flop behavior (e.g. going up
45// in resolution shouldn't give so bad quality that we should go back down).
46
47const int QualityScaler::kLowVp8QpThreshold = 29;
48const int QualityScaler::kBadVp8QpThreshold = 95;
49
tkchin6c687e72016-08-15 16:37:56 -070050#if defined(WEBRTC_IOS)
tkchin6a450102016-08-08 15:27:36 -070051const int QualityScaler::kLowH264QpThreshold = 32;
52const int QualityScaler::kBadH264QpThreshold = 42;
tkchin6c687e72016-08-15 16:37:56 -070053#else
glaznev36a06a92016-08-24 12:09:16 -070054const int QualityScaler::kLowH264QpThreshold = 24;
55const int QualityScaler::kBadH264QpThreshold = 37;
tkchin6c687e72016-08-15 16:37:56 -070056#endif
pbos1f534522016-05-13 11:05:35 -070057
kthelgason194f40a2016-09-14 02:14:58 -070058// Default values. Should immediately get set to something more sensible.
59QualityScaler::QualityScaler()
60 : average_qp_(kMeasureSecondsUpscale * 30),
61 framedrop_percent_(kMeasureSecondsUpscale * 30),
62 low_qp_threshold_(-1) {}
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000063
Peter Boström17417702015-09-25 17:03:26 +020064void QualityScaler::Init(int low_qp_threshold,
65 int high_qp_threshold,
Alex Glazneva9d08922016-02-19 15:24:06 -080066 int initial_bitrate_kbps,
67 int width,
pboscbac40d2016-04-13 02:51:02 -070068 int height,
69 int fps) {
jackychen98d8cf52015-05-21 11:12:02 -070070 low_qp_threshold_ = low_qp_threshold;
Peter Boström17417702015-09-25 17:03:26 +020071 high_qp_threshold_ = high_qp_threshold;
Peter Boström01bcbd02016-03-22 21:44:29 +010072 downscale_shift_ = 0;
kthelgason194f40a2016-09-14 02:14:58 -070073
74 fast_rampup_ = true;
75
76 ClearSamples();
77 ReportFramerate(fps);
78
Peter Boström01bcbd02016-03-22 21:44:29 +010079 const int init_width = width;
80 const int init_height = height;
Peter Boströmb9e77092016-04-18 22:45:55 +020081 if (initial_bitrate_kbps > 0) {
82 int init_num_pixels = width * height;
83 if (initial_bitrate_kbps < kVgaBitrateThresholdKbps)
84 init_num_pixels = kVgaNumPixels;
85 if (initial_bitrate_kbps < kQvgaBitrateThresholdKbps)
86 init_num_pixels = kQvgaNumPixels;
87 while (width * height > init_num_pixels) {
Alex Glazneva9d08922016-02-19 15:24:06 -080088 ++downscale_shift_;
89 width /= 2;
90 height /= 2;
91 }
92 }
Peter Boström01bcbd02016-03-22 21:44:29 +010093 UpdateTargetResolution(init_width, init_height);
pboscbac40d2016-04-13 02:51:02 -070094 ReportFramerate(fps);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000095}
96
jackychen98d8cf52015-05-21 11:12:02 -070097// Report framerate(fps) to estimate # of samples.
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000098void QualityScaler::ReportFramerate(int framerate) {
kthelgason194f40a2016-09-14 02:14:58 -070099 // Use a faster window for upscaling initially.
100 // This enables faster initial rampups without risking strong up-down
101 // behavior later.
102 num_samples_upscale_ = framerate * (fast_rampup_ ? kMeasureSecondsFastUpscale
103 : kMeasureSecondsUpscale);
104 num_samples_downscale_ = framerate * kMeasureSecondsDownscale;
105
106 average_qp_ =
107 MovingAverage(std::max(num_samples_upscale_, num_samples_downscale_));
108 framedrop_percent_ =
109 MovingAverage(std::max(num_samples_upscale_, num_samples_downscale_));
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000110}
111
jackychen98d8cf52015-05-21 11:12:02 -0700112void QualityScaler::ReportQP(int qp) {
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000113 framedrop_percent_.AddSample(0);
kthelgason194f40a2016-09-14 02:14:58 -0700114 average_qp_.AddSample(qp);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000115}
116
117void QualityScaler::ReportDroppedFrame() {
118 framedrop_percent_.AddSample(100);
119}
120
Niels Möller718a7632016-06-13 13:06:01 +0200121void QualityScaler::OnEncodeFrame(int width, int height) {
jackychen61b4d512015-04-21 15:30:11 -0700122 // Should be set through InitEncode -> Should be set by now.
Niels Möller718a7632016-06-13 13:06:01 +0200123 RTC_DCHECK_GE(low_qp_threshold_, 0);
kthelgason194f40a2016-09-14 02:14:58 -0700124 if (target_res_.width != width || target_res_.height != height) {
125 UpdateTargetResolution(width, height);
jackychen61b4d512015-04-21 15:30:11 -0700126 }
kthelgason194f40a2016-09-14 02:14:58 -0700127
128 // Check if we should scale down due to high frame drop.
129 const auto drop_rate = framedrop_percent_.GetAverage(num_samples_downscale_);
130 if (drop_rate && *drop_rate >= kFramedropPercentThreshold) {
131 ScaleDown();
132 return;
133 }
134
135 // Check if we should scale up or down based on QP.
136 const auto avg_qp_down = average_qp_.GetAverage(num_samples_downscale_);
137 if (avg_qp_down && *avg_qp_down > high_qp_threshold_) {
138 ScaleDown();
139 return;
140 }
141 const auto avg_qp_up = average_qp_.GetAverage(num_samples_upscale_);
142 if (avg_qp_up && *avg_qp_up <= low_qp_threshold_) {
143 // QP has been low. We want to try a higher resolution.
144 ScaleUp();
145 return;
146 }
147}
148
149void QualityScaler::ScaleUp() {
150 downscale_shift_ = std::max(0, downscale_shift_ - 1);
151 ClearSamples();
152}
153
154void QualityScaler::ScaleDown() {
155 downscale_shift_ = std::min(maximum_shift_, downscale_shift_ + 1);
156 ClearSamples();
157 // If we've scaled down, wait longer before scaling up again.
158 if (fast_rampup_) {
159 fast_rampup_ = false;
160 num_samples_upscale_ = (num_samples_upscale_ / kMeasureSecondsFastUpscale) *
161 kMeasureSecondsUpscale;
162 }
jackychen6e2ce6e2015-07-13 16:26:33 -0700163}
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000164
jackychen6e2ce6e2015-07-13 16:26:33 -0700165QualityScaler::Resolution QualityScaler::GetScaledResolution() const {
kthelgason194f40a2016-09-14 02:14:58 -0700166 const int frame_width = target_res_.width >> downscale_shift_;
167 const int frame_height = target_res_.height >> downscale_shift_;
168 return Resolution{frame_width, frame_height};
jackychen6e2ce6e2015-07-13 16:26:33 -0700169}
170
Niels Möller718a7632016-06-13 13:06:01 +0200171rtc::scoped_refptr<VideoFrameBuffer> QualityScaler::GetScaledBuffer(
172 const rtc::scoped_refptr<VideoFrameBuffer>& frame) {
jackychen6e2ce6e2015-07-13 16:26:33 -0700173 Resolution res = GetScaledResolution();
kthelgason194f40a2016-09-14 02:14:58 -0700174 const int src_width = frame->width();
175 const int src_height = frame->height();
Niels Möller718a7632016-06-13 13:06:01 +0200176
177 if (res.width == src_width && res.height == src_height)
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000178 return frame;
Niels Möller718a7632016-06-13 13:06:01 +0200179 rtc::scoped_refptr<I420Buffer> scaled_buffer =
180 pool_.CreateBuffer(res.width, res.height);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000181
Niels Möller718a7632016-06-13 13:06:01 +0200182 scaled_buffer->ScaleFrom(frame);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000183
Niels Möller718a7632016-06-13 13:06:01 +0200184 return scaled_buffer;
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000185}
186
kthelgason194f40a2016-09-14 02:14:58 -0700187void QualityScaler::UpdateTargetResolution(int width, int height) {
188 if (width < kMinDownscaleDimension || height < kMinDownscaleDimension) {
189 maximum_shift_ = 0;
190 } else {
191 maximum_shift_ = static_cast<int>(
Kári Tristan Helgason5a20ed32016-09-15 10:56:19 +0200192 log2(std::min(width, height) / kMinDownscaleDimension));
Peter Boström919288f2016-05-12 02:17:35 +0200193 }
kthelgason194f40a2016-09-14 02:14:58 -0700194 target_res_ = Resolution{width, height};
Peter Boström01bcbd02016-03-22 21:44:29 +0100195}
196
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000197void QualityScaler::ClearSamples() {
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000198 framedrop_percent_.Reset();
kthelgason194f40a2016-09-14 02:14:58 -0700199 average_qp_.Reset();
pboscbac40d2016-04-13 02:51:02 -0700200}
201
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000202
203} // namespace webrtc