blob: 541d0d431fb413944ca42ffdc03f3a2b2b7f4766 [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
41const int QualityScaler::kLowH264QpThreshold = 22;
42const int QualityScaler::kBadH264QpThreshold = 35;
43
Peter Boström3c6eac22016-04-26 13:37:10 +020044QualityScaler::QualityScaler() : low_qp_threshold_(-1) {}
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000045
Peter Boström17417702015-09-25 17:03:26 +020046void QualityScaler::Init(int low_qp_threshold,
47 int high_qp_threshold,
Alex Glazneva9d08922016-02-19 15:24:06 -080048 int initial_bitrate_kbps,
49 int width,
pboscbac40d2016-04-13 02:51:02 -070050 int height,
51 int fps) {
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000052 ClearSamples();
jackychen98d8cf52015-05-21 11:12:02 -070053 low_qp_threshold_ = low_qp_threshold;
Peter Boström17417702015-09-25 17:03:26 +020054 high_qp_threshold_ = high_qp_threshold;
Peter Boström01bcbd02016-03-22 21:44:29 +010055 downscale_shift_ = 0;
pboscbac40d2016-04-13 02:51:02 -070056 // Use a faster window for upscaling initially (but be more graceful later).
57 // This enables faster initial rampups without risking strong up-down
58 // behavior later.
59 measure_seconds_upscale_ = kMeasureSecondsFastUpscale;
Peter Boström01bcbd02016-03-22 21:44:29 +010060 const int init_width = width;
61 const int init_height = height;
Peter Boströmb9e77092016-04-18 22:45:55 +020062 if (initial_bitrate_kbps > 0) {
63 int init_num_pixels = width * height;
64 if (initial_bitrate_kbps < kVgaBitrateThresholdKbps)
65 init_num_pixels = kVgaNumPixels;
66 if (initial_bitrate_kbps < kQvgaBitrateThresholdKbps)
67 init_num_pixels = kQvgaNumPixels;
68 while (width * height > init_num_pixels) {
Alex Glazneva9d08922016-02-19 15:24:06 -080069 ++downscale_shift_;
70 width /= 2;
71 height /= 2;
72 }
73 }
Peter Boström919288f2016-05-12 02:17:35 +020074
75 // Zero out width/height so they can be checked against inside
76 // UpdateTargetResolution.
77 res_.width = res_.height = 0;
Peter Boström01bcbd02016-03-22 21:44:29 +010078 UpdateTargetResolution(init_width, init_height);
pboscbac40d2016-04-13 02:51:02 -070079 ReportFramerate(fps);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000080}
81
jackychen98d8cf52015-05-21 11:12:02 -070082// Report framerate(fps) to estimate # of samples.
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000083void QualityScaler::ReportFramerate(int framerate) {
jackychen6e2ce6e2015-07-13 16:26:33 -070084 framerate_ = framerate;
pboscbac40d2016-04-13 02:51:02 -070085 UpdateSampleCounts();
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000086}
87
jackychen98d8cf52015-05-21 11:12:02 -070088void QualityScaler::ReportQP(int qp) {
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000089 framedrop_percent_.AddSample(0);
pboscbac40d2016-04-13 02:51:02 -070090 average_qp_downscale_.AddSample(qp);
91 average_qp_upscale_.AddSample(qp);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000092}
93
94void QualityScaler::ReportDroppedFrame() {
95 framedrop_percent_.AddSample(100);
96}
97
Niels Möller718a7632016-06-13 13:06:01 +020098void QualityScaler::OnEncodeFrame(int width, int height) {
jackychen61b4d512015-04-21 15:30:11 -070099 // Should be set through InitEncode -> Should be set by now.
Niels Möller718a7632016-06-13 13:06:01 +0200100 RTC_DCHECK_GE(low_qp_threshold_, 0);
101 RTC_DCHECK_GT(num_samples_upscale_, 0u);
102 RTC_DCHECK_GT(num_samples_downscale_, 0u);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000103
jackychen61b4d512015-04-21 15:30:11 -0700104 // Update scale factor.
jackychen5af6d472015-05-21 14:11:36 -0700105 int avg_drop = 0;
106 int avg_qp = 0;
jackychen6e2ce6e2015-07-13 16:26:33 -0700107
pboscbac40d2016-04-13 02:51:02 -0700108 if ((framedrop_percent_.GetAverage(num_samples_downscale_, &avg_drop) &&
Peter Boström17417702015-09-25 17:03:26 +0200109 avg_drop >= kFramedropPercentThreshold) ||
pboscbac40d2016-04-13 02:51:02 -0700110 (average_qp_downscale_.GetAverage(num_samples_downscale_, &avg_qp) &&
Peter Boström17417702015-09-25 17:03:26 +0200111 avg_qp > high_qp_threshold_)) {
Peter Boström3c6eac22016-04-26 13:37:10 +0200112 AdjustScale(false);
pboscbac40d2016-04-13 02:51:02 -0700113 } else if (average_qp_upscale_.GetAverage(num_samples_upscale_, &avg_qp) &&
philipel5908c712015-12-21 08:23:20 -0800114 avg_qp <= low_qp_threshold_) {
Peter Boström3c6eac22016-04-26 13:37:10 +0200115 AdjustScale(true);
jackychen61b4d512015-04-21 15:30:11 -0700116 }
Niels Möller718a7632016-06-13 13:06:01 +0200117 UpdateTargetResolution(width, height);
jackychen6e2ce6e2015-07-13 16:26:33 -0700118}
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000119
jackychen6e2ce6e2015-07-13 16:26:33 -0700120QualityScaler::Resolution QualityScaler::GetScaledResolution() const {
121 return res_;
122}
123
Niels Möller718a7632016-06-13 13:06:01 +0200124rtc::scoped_refptr<VideoFrameBuffer> QualityScaler::GetScaledBuffer(
125 const rtc::scoped_refptr<VideoFrameBuffer>& frame) {
jackychen6e2ce6e2015-07-13 16:26:33 -0700126 Resolution res = GetScaledResolution();
Niels Möller718a7632016-06-13 13:06:01 +0200127 int src_width = frame->width();
128 int src_height = frame->height();
129
130 if (res.width == src_width && res.height == src_height)
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000131 return frame;
Niels Möller718a7632016-06-13 13:06:01 +0200132 rtc::scoped_refptr<I420Buffer> scaled_buffer =
133 pool_.CreateBuffer(res.width, res.height);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000134
Niels Möller718a7632016-06-13 13:06:01 +0200135 scaled_buffer->ScaleFrom(frame);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000136
Niels Möller718a7632016-06-13 13:06:01 +0200137 return scaled_buffer;
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000138}
139
Peter Boström01bcbd02016-03-22 21:44:29 +0100140void QualityScaler::UpdateTargetResolution(int frame_width, int frame_height) {
Niels Möller718a7632016-06-13 13:06:01 +0200141 RTC_DCHECK_GE(downscale_shift_, 0);
Peter Boström919288f2016-05-12 02:17:35 +0200142 int shifts_performed = 0;
143 for (int shift = downscale_shift_;
144 shift > 0 && (frame_width / 2 >= kMinDownscaleDimension) &&
145 (frame_height / 2 >= kMinDownscaleDimension);
146 --shift, ++shifts_performed) {
147 frame_width /= 2;
148 frame_height /= 2;
149 }
150 // Clamp to number of shifts actually performed to not be stuck trying to
151 // scale way beyond QVGA.
152 downscale_shift_ = shifts_performed;
153 if (res_.width == frame_width && res_.height == frame_height) {
154 // No reset done/needed, using same resolution.
155 return;
156 }
Peter Boström01bcbd02016-03-22 21:44:29 +0100157 res_.width = frame_width;
158 res_.height = frame_height;
Peter Boström919288f2016-05-12 02:17:35 +0200159 ClearSamples();
Peter Boström01bcbd02016-03-22 21:44:29 +0100160}
161
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000162void QualityScaler::ClearSamples() {
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000163 framedrop_percent_.Reset();
pboscbac40d2016-04-13 02:51:02 -0700164 average_qp_downscale_.Reset();
165 average_qp_upscale_.Reset();
166}
167
168void QualityScaler::UpdateSampleCounts() {
169 num_samples_downscale_ = static_cast<size_t>(
170 kMeasureSecondsDownscale * (framerate_ < kMinFps ? kMinFps : framerate_));
171 num_samples_upscale_ = static_cast<size_t>(
172 measure_seconds_upscale_ * (framerate_ < kMinFps ? kMinFps : framerate_));
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000173}
174
175void QualityScaler::AdjustScale(bool up) {
176 downscale_shift_ += up ? -1 : 1;
177 if (downscale_shift_ < 0)
178 downscale_shift_ = 0;
pboscbac40d2016-04-13 02:51:02 -0700179 if (!up) {
Peter Boström919288f2016-05-12 02:17:35 +0200180 // First downscale hit, start using a slower threshold for going up.
pboscbac40d2016-04-13 02:51:02 -0700181 measure_seconds_upscale_ = kMeasureSecondsUpscale;
182 UpdateSampleCounts();
183 }
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000184}
185
186} // namespace webrtc