blob: a5e8c22bb093fe6bd776d4c6983f9eee3c2a8e77 [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
33QualityScaler::QualityScaler()
Peter Boström926dfcd2016-04-14 14:48:10 +020034 : low_qp_threshold_(-1), framerate_down_(false) {}
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000035
Peter Boström17417702015-09-25 17:03:26 +020036void QualityScaler::Init(int low_qp_threshold,
37 int high_qp_threshold,
Alex Glazneva9d08922016-02-19 15:24:06 -080038 bool use_framerate_reduction,
39 int initial_bitrate_kbps,
40 int width,
pboscbac40d2016-04-13 02:51:02 -070041 int height,
42 int fps) {
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000043 ClearSamples();
jackychen98d8cf52015-05-21 11:12:02 -070044 low_qp_threshold_ = low_qp_threshold;
Peter Boström17417702015-09-25 17:03:26 +020045 high_qp_threshold_ = high_qp_threshold;
jackychen6e2ce6e2015-07-13 16:26:33 -070046 use_framerate_reduction_ = use_framerate_reduction;
Peter Boström01bcbd02016-03-22 21:44:29 +010047 downscale_shift_ = 0;
pboscbac40d2016-04-13 02:51:02 -070048 // Use a faster window for upscaling initially (but be more graceful later).
49 // This enables faster initial rampups without risking strong up-down
50 // behavior later.
51 measure_seconds_upscale_ = kMeasureSecondsFastUpscale;
Peter Boström01bcbd02016-03-22 21:44:29 +010052 const int init_width = width;
53 const int init_height = height;
Peter Boströmb9e77092016-04-18 22:45:55 +020054 if (initial_bitrate_kbps > 0) {
55 int init_num_pixels = width * height;
56 if (initial_bitrate_kbps < kVgaBitrateThresholdKbps)
57 init_num_pixels = kVgaNumPixels;
58 if (initial_bitrate_kbps < kQvgaBitrateThresholdKbps)
59 init_num_pixels = kQvgaNumPixels;
60 while (width * height > init_num_pixels) {
Alex Glazneva9d08922016-02-19 15:24:06 -080061 ++downscale_shift_;
62 width /= 2;
63 height /= 2;
64 }
65 }
Peter Boström01bcbd02016-03-22 21:44:29 +010066 UpdateTargetResolution(init_width, init_height);
pboscbac40d2016-04-13 02:51:02 -070067 ReportFramerate(fps);
jackychen6e2ce6e2015-07-13 16:26:33 -070068 target_framerate_ = -1;
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000069}
70
jackychen98d8cf52015-05-21 11:12:02 -070071// Report framerate(fps) to estimate # of samples.
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000072void QualityScaler::ReportFramerate(int framerate) {
jackychen6e2ce6e2015-07-13 16:26:33 -070073 framerate_ = framerate;
pboscbac40d2016-04-13 02:51:02 -070074 UpdateSampleCounts();
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000075}
76
jackychen98d8cf52015-05-21 11:12:02 -070077void QualityScaler::ReportQP(int qp) {
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000078 framedrop_percent_.AddSample(0);
pboscbac40d2016-04-13 02:51:02 -070079 average_qp_downscale_.AddSample(qp);
80 average_qp_upscale_.AddSample(qp);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000081}
82
83void QualityScaler::ReportDroppedFrame() {
84 framedrop_percent_.AddSample(100);
85}
86
jackychen6e2ce6e2015-07-13 16:26:33 -070087void QualityScaler::OnEncodeFrame(const VideoFrame& frame) {
jackychen61b4d512015-04-21 15:30:11 -070088 // Should be set through InitEncode -> Should be set by now.
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000089 assert(low_qp_threshold_ >= 0);
pboscbac40d2016-04-13 02:51:02 -070090 assert(num_samples_upscale_ > 0);
91 assert(num_samples_downscale_ > 0);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000092
jackychen61b4d512015-04-21 15:30:11 -070093 // Update scale factor.
jackychen5af6d472015-05-21 14:11:36 -070094 int avg_drop = 0;
95 int avg_qp = 0;
jackychen6e2ce6e2015-07-13 16:26:33 -070096
97 // When encoder consistently overshoots, framerate reduction and spatial
98 // resizing will be triggered to get a smoother video.
pboscbac40d2016-04-13 02:51:02 -070099 if ((framedrop_percent_.GetAverage(num_samples_downscale_, &avg_drop) &&
Peter Boström17417702015-09-25 17:03:26 +0200100 avg_drop >= kFramedropPercentThreshold) ||
pboscbac40d2016-04-13 02:51:02 -0700101 (average_qp_downscale_.GetAverage(num_samples_downscale_, &avg_qp) &&
Peter Boström17417702015-09-25 17:03:26 +0200102 avg_qp > high_qp_threshold_)) {
jackychen6e2ce6e2015-07-13 16:26:33 -0700103 // Reducing frame rate before spatial resolution change.
104 // Reduce frame rate only when it is above a certain number.
105 // Only one reduction is allowed for now.
106 // TODO(jackychen): Allow more than one framerate reduction.
107 if (use_framerate_reduction_ && !framerate_down_ && framerate_ >= 20) {
108 target_framerate_ = framerate_ / 2;
109 framerate_down_ = true;
110 // If frame rate has been updated, clear the buffer. We don't want
111 // spatial resolution to change right after frame rate change.
112 ClearSamples();
113 } else {
114 AdjustScale(false);
115 }
pboscbac40d2016-04-13 02:51:02 -0700116 } else if (average_qp_upscale_.GetAverage(num_samples_upscale_, &avg_qp) &&
philipel5908c712015-12-21 08:23:20 -0800117 avg_qp <= low_qp_threshold_) {
jackychen6e2ce6e2015-07-13 16:26:33 -0700118 if (use_framerate_reduction_ && framerate_down_) {
119 target_framerate_ = -1;
120 framerate_down_ = false;
121 ClearSamples();
122 } else {
123 AdjustScale(true);
124 }
jackychen61b4d512015-04-21 15:30:11 -0700125 }
Peter Boström01bcbd02016-03-22 21:44:29 +0100126 UpdateTargetResolution(frame.width(), frame.height());
jackychen6e2ce6e2015-07-13 16:26:33 -0700127}
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000128
jackychen6e2ce6e2015-07-13 16:26:33 -0700129QualityScaler::Resolution QualityScaler::GetScaledResolution() const {
130 return res_;
131}
132
133int QualityScaler::GetTargetFramerate() const {
134 return target_framerate_;
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000135}
136
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700137const VideoFrame& QualityScaler::GetScaledFrame(const VideoFrame& frame) {
jackychen6e2ce6e2015-07-13 16:26:33 -0700138 Resolution res = GetScaledResolution();
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000139 if (res.width == frame.width())
140 return frame;
141
philipel5908c712015-12-21 08:23:20 -0800142 scaler_.Set(frame.width(), frame.height(), res.width, res.height, kI420,
143 kI420, kScaleBox);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000144 if (scaler_.Scale(frame, &scaled_frame_) != 0)
145 return frame;
146
147 scaled_frame_.set_ntp_time_ms(frame.ntp_time_ms());
148 scaled_frame_.set_timestamp(frame.timestamp());
149 scaled_frame_.set_render_time_ms(frame.render_time_ms());
150
151 return scaled_frame_;
152}
153
Peter Boström01bcbd02016-03-22 21:44:29 +0100154void QualityScaler::UpdateTargetResolution(int frame_width, int frame_height) {
155 assert(downscale_shift_ >= 0);
156 res_.width = frame_width;
157 res_.height = frame_height;
158 for (int shift = downscale_shift_;
Peter Boström926dfcd2016-04-14 14:48:10 +0200159 shift > 0 && (res_.width / 2 >= kMinDownscaleDimension) &&
160 (res_.height / 2 >= kMinDownscaleDimension);
Peter Boström01bcbd02016-03-22 21:44:29 +0100161 --shift) {
162 res_.width /= 2;
163 res_.height /= 2;
164 }
165}
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) {
185 // Hit first downscale, start using a slower threshold for going up.
186 measure_seconds_upscale_ = kMeasureSecondsUpscale;
187 UpdateSampleCounts();
188 }
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000189 ClearSamples();
190}
191
192} // namespace webrtc