blob: bb60ee036e381dc9762e3b9a030acd257582eed5 [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 */
kjellander@webrtc.orgb7ce9642015-11-18 23:04:10 +010010#include "webrtc/modules/video_coding/utility/quality_scaler.h"
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000011
12namespace webrtc {
13
Peter Boström926dfcd2016-04-14 14:48:10 +020014namespace {
pboscbac40d2016-04-13 02:51:02 -070015static const int kMinFps = 5;
pboscbac40d2016-04-13 02:51:02 -070016// Threshold constant used until first downscale (to permit fast rampup).
17static const int kMeasureSecondsFastUpscale = 2;
18static const int kMeasureSecondsUpscale = 5;
Peter Boström2c8a2962016-04-18 12:58:02 +020019static const int kMeasureSecondsDownscale = 5;
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000020static const int kFramedropPercentThreshold = 60;
Peter Boström926dfcd2016-04-14 14:48:10 +020021// Min width/height to downscale to, set to not go below QVGA, but with some
22// margin to permit "almost-QVGA" resolutions, such as QCIF.
23static const int kMinDownscaleDimension = 140;
Peter Boströmb9e77092016-04-18 22:45:55 +020024// Initial resolutions corresponding to a bitrate. Aa bit above their actual
25// values to permit near-VGA and near-QVGA resolutions to use the same
26// mechanism.
27static const int kVgaBitrateThresholdKbps = 500;
28static const int kVgaNumPixels = 700 * 500; // 640x480
29static const int kQvgaBitrateThresholdKbps = 250;
30static const int kQvgaNumPixels = 400 * 300; // 320x240
Peter Boström926dfcd2016-04-14 14:48:10 +020031} // namespace
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000032
pbos1f534522016-05-13 11:05:35 -070033// QP thresholds are chosen to be high enough to be hit in practice when quality
34// is good, but also low enough to not cause a flip-flop behavior (e.g. going up
35// in resolution shouldn't give so bad quality that we should go back down).
36
37const int QualityScaler::kLowVp8QpThreshold = 29;
38const int QualityScaler::kBadVp8QpThreshold = 95;
39
40const int QualityScaler::kLowH264QpThreshold = 22;
41const int QualityScaler::kBadH264QpThreshold = 35;
42
Peter Boström3c6eac22016-04-26 13:37:10 +020043QualityScaler::QualityScaler() : low_qp_threshold_(-1) {}
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000044
Peter Boström17417702015-09-25 17:03:26 +020045void QualityScaler::Init(int low_qp_threshold,
46 int high_qp_threshold,
Alex Glazneva9d08922016-02-19 15:24:06 -080047 int initial_bitrate_kbps,
48 int width,
pboscbac40d2016-04-13 02:51:02 -070049 int height,
50 int fps) {
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000051 ClearSamples();
jackychen98d8cf52015-05-21 11:12:02 -070052 low_qp_threshold_ = low_qp_threshold;
Peter Boström17417702015-09-25 17:03:26 +020053 high_qp_threshold_ = high_qp_threshold;
Peter Boström01bcbd02016-03-22 21:44:29 +010054 downscale_shift_ = 0;
pboscbac40d2016-04-13 02:51:02 -070055 // Use a faster window for upscaling initially (but be more graceful later).
56 // This enables faster initial rampups without risking strong up-down
57 // behavior later.
58 measure_seconds_upscale_ = kMeasureSecondsFastUpscale;
Peter Boström01bcbd02016-03-22 21:44:29 +010059 const int init_width = width;
60 const int init_height = height;
Peter Boströmb9e77092016-04-18 22:45:55 +020061 if (initial_bitrate_kbps > 0) {
62 int init_num_pixels = width * height;
63 if (initial_bitrate_kbps < kVgaBitrateThresholdKbps)
64 init_num_pixels = kVgaNumPixels;
65 if (initial_bitrate_kbps < kQvgaBitrateThresholdKbps)
66 init_num_pixels = kQvgaNumPixels;
67 while (width * height > init_num_pixels) {
Alex Glazneva9d08922016-02-19 15:24:06 -080068 ++downscale_shift_;
69 width /= 2;
70 height /= 2;
71 }
72 }
Peter Boström919288f2016-05-12 02:17:35 +020073
74 // Zero out width/height so they can be checked against inside
75 // UpdateTargetResolution.
76 res_.width = res_.height = 0;
Peter Boström01bcbd02016-03-22 21:44:29 +010077 UpdateTargetResolution(init_width, init_height);
pboscbac40d2016-04-13 02:51:02 -070078 ReportFramerate(fps);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000079}
80
jackychen98d8cf52015-05-21 11:12:02 -070081// Report framerate(fps) to estimate # of samples.
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000082void QualityScaler::ReportFramerate(int framerate) {
jackychen6e2ce6e2015-07-13 16:26:33 -070083 framerate_ = framerate;
pboscbac40d2016-04-13 02:51:02 -070084 UpdateSampleCounts();
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000085}
86
jackychen98d8cf52015-05-21 11:12:02 -070087void QualityScaler::ReportQP(int qp) {
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000088 framedrop_percent_.AddSample(0);
pboscbac40d2016-04-13 02:51:02 -070089 average_qp_downscale_.AddSample(qp);
90 average_qp_upscale_.AddSample(qp);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000091}
92
93void QualityScaler::ReportDroppedFrame() {
94 framedrop_percent_.AddSample(100);
95}
96
jackychen6e2ce6e2015-07-13 16:26:33 -070097void QualityScaler::OnEncodeFrame(const VideoFrame& frame) {
jackychen61b4d512015-04-21 15:30:11 -070098 // Should be set through InitEncode -> Should be set by now.
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000099 assert(low_qp_threshold_ >= 0);
pboscbac40d2016-04-13 02:51:02 -0700100 assert(num_samples_upscale_ > 0);
101 assert(num_samples_downscale_ > 0);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000102
jackychen61b4d512015-04-21 15:30:11 -0700103 // Update scale factor.
jackychen5af6d472015-05-21 14:11:36 -0700104 int avg_drop = 0;
105 int avg_qp = 0;
jackychen6e2ce6e2015-07-13 16:26:33 -0700106
pboscbac40d2016-04-13 02:51:02 -0700107 if ((framedrop_percent_.GetAverage(num_samples_downscale_, &avg_drop) &&
Peter Boström17417702015-09-25 17:03:26 +0200108 avg_drop >= kFramedropPercentThreshold) ||
pboscbac40d2016-04-13 02:51:02 -0700109 (average_qp_downscale_.GetAverage(num_samples_downscale_, &avg_qp) &&
Peter Boström17417702015-09-25 17:03:26 +0200110 avg_qp > high_qp_threshold_)) {
Peter Boström3c6eac22016-04-26 13:37:10 +0200111 AdjustScale(false);
pboscbac40d2016-04-13 02:51:02 -0700112 } else if (average_qp_upscale_.GetAverage(num_samples_upscale_, &avg_qp) &&
philipel5908c712015-12-21 08:23:20 -0800113 avg_qp <= low_qp_threshold_) {
Peter Boström3c6eac22016-04-26 13:37:10 +0200114 AdjustScale(true);
jackychen61b4d512015-04-21 15:30:11 -0700115 }
Peter Boström01bcbd02016-03-22 21:44:29 +0100116 UpdateTargetResolution(frame.width(), frame.height());
jackychen6e2ce6e2015-07-13 16:26:33 -0700117}
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000118
jackychen6e2ce6e2015-07-13 16:26:33 -0700119QualityScaler::Resolution QualityScaler::GetScaledResolution() const {
120 return res_;
121}
122
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700123const VideoFrame& QualityScaler::GetScaledFrame(const VideoFrame& frame) {
jackychen6e2ce6e2015-07-13 16:26:33 -0700124 Resolution res = GetScaledResolution();
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000125 if (res.width == frame.width())
126 return frame;
127
philipel5908c712015-12-21 08:23:20 -0800128 scaler_.Set(frame.width(), frame.height(), res.width, res.height, kI420,
129 kI420, kScaleBox);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000130 if (scaler_.Scale(frame, &scaled_frame_) != 0)
131 return frame;
132
Perba7dc722016-04-19 15:01:23 +0200133 // TODO(perkj): Refactor the scaler to not own |scaled_frame|. VideoFrame are
134 // just thin wrappers so instead the scaler should return a
135 // rtc::scoped_refptr<VideoFrameBuffer> and a new VideoFrame be created with
136 // the meta data from |frame|. That way we would not have to set all these
137 // meta data.
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000138 scaled_frame_.set_ntp_time_ms(frame.ntp_time_ms());
139 scaled_frame_.set_timestamp(frame.timestamp());
140 scaled_frame_.set_render_time_ms(frame.render_time_ms());
Perba7dc722016-04-19 15:01:23 +0200141 scaled_frame_.set_rotation(frame.rotation());
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000142
143 return scaled_frame_;
144}
145
Peter Boström01bcbd02016-03-22 21:44:29 +0100146void QualityScaler::UpdateTargetResolution(int frame_width, int frame_height) {
147 assert(downscale_shift_ >= 0);
Peter Boström919288f2016-05-12 02:17:35 +0200148 int shifts_performed = 0;
149 for (int shift = downscale_shift_;
150 shift > 0 && (frame_width / 2 >= kMinDownscaleDimension) &&
151 (frame_height / 2 >= kMinDownscaleDimension);
152 --shift, ++shifts_performed) {
153 frame_width /= 2;
154 frame_height /= 2;
155 }
156 // Clamp to number of shifts actually performed to not be stuck trying to
157 // scale way beyond QVGA.
158 downscale_shift_ = shifts_performed;
159 if (res_.width == frame_width && res_.height == frame_height) {
160 // No reset done/needed, using same resolution.
161 return;
162 }
Peter Boström01bcbd02016-03-22 21:44:29 +0100163 res_.width = frame_width;
164 res_.height = frame_height;
Peter Boström919288f2016-05-12 02:17:35 +0200165 ClearSamples();
Peter Boström01bcbd02016-03-22 21:44:29 +0100166}
167
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000168void QualityScaler::ClearSamples() {
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000169 framedrop_percent_.Reset();
pboscbac40d2016-04-13 02:51:02 -0700170 average_qp_downscale_.Reset();
171 average_qp_upscale_.Reset();
172}
173
174void QualityScaler::UpdateSampleCounts() {
175 num_samples_downscale_ = static_cast<size_t>(
176 kMeasureSecondsDownscale * (framerate_ < kMinFps ? kMinFps : framerate_));
177 num_samples_upscale_ = static_cast<size_t>(
178 measure_seconds_upscale_ * (framerate_ < kMinFps ? kMinFps : framerate_));
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000179}
180
181void QualityScaler::AdjustScale(bool up) {
182 downscale_shift_ += up ? -1 : 1;
183 if (downscale_shift_ < 0)
184 downscale_shift_ = 0;
pboscbac40d2016-04-13 02:51:02 -0700185 if (!up) {
Peter Boström919288f2016-05-12 02:17:35 +0200186 // First downscale hit, start using a slower threshold for going up.
pboscbac40d2016-04-13 02:51:02 -0700187 measure_seconds_upscale_ = kMeasureSecondsUpscale;
188 UpdateSampleCounts();
189 }
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000190}
191
192} // namespace webrtc