blob: c509e843642f2237e94fe1727749479a9523faa7 [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
13namespace webrtc {
14
Peter Boström926dfcd2016-04-14 14:48:10 +020015namespace {
pboscbac40d2016-04-13 02:51:02 -070016static const int kMinFps = 5;
pboscbac40d2016-04-13 02:51:02 -070017// Threshold constant used until first downscale (to permit fast rampup).
18static const int kMeasureSecondsFastUpscale = 2;
19static const int kMeasureSecondsUpscale = 5;
Peter Boström2c8a2962016-04-18 12:58:02 +020020static const int kMeasureSecondsDownscale = 5;
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000021static const int kFramedropPercentThreshold = 60;
Peter Boström926dfcd2016-04-14 14:48:10 +020022// 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.
24static const int kMinDownscaleDimension = 140;
Peter Boströmb9e77092016-04-18 22:45:55 +020025// 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// mechanism.
28static const int kVgaBitrateThresholdKbps = 500;
29static const int kVgaNumPixels = 700 * 500; // 640x480
30static const int kQvgaBitrateThresholdKbps = 250;
31static const int kQvgaNumPixels = 400 * 300; // 320x240
Peter Boström926dfcd2016-04-14 14:48:10 +020032} // namespace
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000033
pbos1f534522016-05-13 11:05:35 -070034// QP thresholds are chosen to be high enough to be hit in practice when quality
35// is good, but also low enough to not cause a flip-flop behavior (e.g. going up
36// in resolution shouldn't give so bad quality that we should go back down).
37
38const int QualityScaler::kLowVp8QpThreshold = 29;
39const int QualityScaler::kBadVp8QpThreshold = 95;
40
tkchin6c687e72016-08-15 16:37:56 -070041#if defined(WEBRTC_IOS)
tkchin6a450102016-08-08 15:27:36 -070042const int QualityScaler::kLowH264QpThreshold = 32;
43const int QualityScaler::kBadH264QpThreshold = 42;
tkchin6c687e72016-08-15 16:37:56 -070044#else
glaznev36a06a92016-08-24 12:09:16 -070045const int QualityScaler::kLowH264QpThreshold = 24;
46const int QualityScaler::kBadH264QpThreshold = 37;
tkchin6c687e72016-08-15 16:37:56 -070047#endif
pbos1f534522016-05-13 11:05:35 -070048
Peter Boström3c6eac22016-04-26 13:37:10 +020049QualityScaler::QualityScaler() : low_qp_threshold_(-1) {}
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000050
Peter Boström17417702015-09-25 17:03:26 +020051void QualityScaler::Init(int low_qp_threshold,
52 int high_qp_threshold,
Alex Glazneva9d08922016-02-19 15:24:06 -080053 int initial_bitrate_kbps,
54 int width,
pboscbac40d2016-04-13 02:51:02 -070055 int height,
56 int fps) {
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000057 ClearSamples();
jackychen98d8cf52015-05-21 11:12:02 -070058 low_qp_threshold_ = low_qp_threshold;
Peter Boström17417702015-09-25 17:03:26 +020059 high_qp_threshold_ = high_qp_threshold;
Peter Boström01bcbd02016-03-22 21:44:29 +010060 downscale_shift_ = 0;
pboscbac40d2016-04-13 02:51:02 -070061 // Use a faster window for upscaling initially (but be more graceful later).
62 // This enables faster initial rampups without risking strong up-down
63 // behavior later.
64 measure_seconds_upscale_ = kMeasureSecondsFastUpscale;
Peter Boström01bcbd02016-03-22 21:44:29 +010065 const int init_width = width;
66 const int init_height = height;
Peter Boströmb9e77092016-04-18 22:45:55 +020067 if (initial_bitrate_kbps > 0) {
68 int init_num_pixels = width * height;
69 if (initial_bitrate_kbps < kVgaBitrateThresholdKbps)
70 init_num_pixels = kVgaNumPixels;
71 if (initial_bitrate_kbps < kQvgaBitrateThresholdKbps)
72 init_num_pixels = kQvgaNumPixels;
73 while (width * height > init_num_pixels) {
Alex Glazneva9d08922016-02-19 15:24:06 -080074 ++downscale_shift_;
75 width /= 2;
76 height /= 2;
77 }
78 }
Peter Boström919288f2016-05-12 02:17:35 +020079
80 // Zero out width/height so they can be checked against inside
81 // UpdateTargetResolution.
82 res_.width = res_.height = 0;
Peter Boström01bcbd02016-03-22 21:44:29 +010083 UpdateTargetResolution(init_width, init_height);
pboscbac40d2016-04-13 02:51:02 -070084 ReportFramerate(fps);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000085}
86
jackychen98d8cf52015-05-21 11:12:02 -070087// Report framerate(fps) to estimate # of samples.
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000088void QualityScaler::ReportFramerate(int framerate) {
jackychen6e2ce6e2015-07-13 16:26:33 -070089 framerate_ = framerate;
pboscbac40d2016-04-13 02:51:02 -070090 UpdateSampleCounts();
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000091}
92
jackychen98d8cf52015-05-21 11:12:02 -070093void QualityScaler::ReportQP(int qp) {
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000094 framedrop_percent_.AddSample(0);
pboscbac40d2016-04-13 02:51:02 -070095 average_qp_downscale_.AddSample(qp);
96 average_qp_upscale_.AddSample(qp);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000097}
98
99void QualityScaler::ReportDroppedFrame() {
100 framedrop_percent_.AddSample(100);
101}
102
Niels Möller718a7632016-06-13 13:06:01 +0200103void QualityScaler::OnEncodeFrame(int width, int height) {
jackychen61b4d512015-04-21 15:30:11 -0700104 // Should be set through InitEncode -> Should be set by now.
Niels Möller718a7632016-06-13 13:06:01 +0200105 RTC_DCHECK_GE(low_qp_threshold_, 0);
106 RTC_DCHECK_GT(num_samples_upscale_, 0u);
107 RTC_DCHECK_GT(num_samples_downscale_, 0u);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000108
jackychen61b4d512015-04-21 15:30:11 -0700109 // Update scale factor.
jackychen5af6d472015-05-21 14:11:36 -0700110 int avg_drop = 0;
111 int avg_qp = 0;
jackychen6e2ce6e2015-07-13 16:26:33 -0700112
pboscbac40d2016-04-13 02:51:02 -0700113 if ((framedrop_percent_.GetAverage(num_samples_downscale_, &avg_drop) &&
Peter Boström17417702015-09-25 17:03:26 +0200114 avg_drop >= kFramedropPercentThreshold) ||
pboscbac40d2016-04-13 02:51:02 -0700115 (average_qp_downscale_.GetAverage(num_samples_downscale_, &avg_qp) &&
Peter Boström17417702015-09-25 17:03:26 +0200116 avg_qp > high_qp_threshold_)) {
Peter Boström3c6eac22016-04-26 13:37:10 +0200117 AdjustScale(false);
pboscbac40d2016-04-13 02:51:02 -0700118 } else if (average_qp_upscale_.GetAverage(num_samples_upscale_, &avg_qp) &&
philipel5908c712015-12-21 08:23:20 -0800119 avg_qp <= low_qp_threshold_) {
Peter Boström3c6eac22016-04-26 13:37:10 +0200120 AdjustScale(true);
jackychen61b4d512015-04-21 15:30:11 -0700121 }
Niels Möller718a7632016-06-13 13:06:01 +0200122 UpdateTargetResolution(width, height);
jackychen6e2ce6e2015-07-13 16:26:33 -0700123}
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000124
jackychen6e2ce6e2015-07-13 16:26:33 -0700125QualityScaler::Resolution QualityScaler::GetScaledResolution() const {
126 return res_;
127}
128
Niels Möller718a7632016-06-13 13:06:01 +0200129rtc::scoped_refptr<VideoFrameBuffer> QualityScaler::GetScaledBuffer(
130 const rtc::scoped_refptr<VideoFrameBuffer>& frame) {
jackychen6e2ce6e2015-07-13 16:26:33 -0700131 Resolution res = GetScaledResolution();
Niels Möller718a7632016-06-13 13:06:01 +0200132 int src_width = frame->width();
133 int src_height = frame->height();
134
135 if (res.width == src_width && res.height == src_height)
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000136 return frame;
Niels Möller718a7632016-06-13 13:06:01 +0200137 rtc::scoped_refptr<I420Buffer> scaled_buffer =
138 pool_.CreateBuffer(res.width, res.height);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000139
Niels Möller718a7632016-06-13 13:06:01 +0200140 scaled_buffer->ScaleFrom(frame);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000141
Niels Möller718a7632016-06-13 13:06:01 +0200142 return scaled_buffer;
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000143}
144
Peter Boström01bcbd02016-03-22 21:44:29 +0100145void QualityScaler::UpdateTargetResolution(int frame_width, int frame_height) {
Niels Möller718a7632016-06-13 13:06:01 +0200146 RTC_DCHECK_GE(downscale_shift_, 0);
Peter Boström919288f2016-05-12 02:17:35 +0200147 int shifts_performed = 0;
148 for (int shift = downscale_shift_;
149 shift > 0 && (frame_width / 2 >= kMinDownscaleDimension) &&
150 (frame_height / 2 >= kMinDownscaleDimension);
151 --shift, ++shifts_performed) {
152 frame_width /= 2;
153 frame_height /= 2;
154 }
155 // Clamp to number of shifts actually performed to not be stuck trying to
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 }
Peter Boström01bcbd02016-03-22 21:44:29 +0100162 res_.width = frame_width;
163 res_.height = frame_height;
Peter Boström919288f2016-05-12 02:17:35 +0200164 ClearSamples();
Peter Boström01bcbd02016-03-22 21:44:29 +0100165}
166
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000167void QualityScaler::ClearSamples() {
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000168 framedrop_percent_.Reset();
pboscbac40d2016-04-13 02:51:02 -0700169 average_qp_downscale_.Reset();
170 average_qp_upscale_.Reset();
171}
172
173void 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_));
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000178}
179
180void QualityScaler::AdjustScale(bool up) {
181 downscale_shift_ += up ? -1 : 1;
182 if (downscale_shift_ < 0)
183 downscale_shift_ = 0;
pboscbac40d2016-04-13 02:51:02 -0700184 if (!up) {
Peter Boström919288f2016-05-12 02:17:35 +0200185 // First downscale hit, start using a slower threshold for going up.
pboscbac40d2016-04-13 02:51:02 -0700186 measure_seconds_upscale_ = kMeasureSecondsUpscale;
187 UpdateSampleCounts();
188 }
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000189}
190
191} // namespace webrtc