blob: c6e566973101e01f1ec868f8810e614d7a3480e7 [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
pboscbac40d2016-04-13 02:51:02 -070014static const int kMinFps = 5;
15static const int kMeasureSecondsDownscale = 3;
16// Threshold constant used until first downscale (to permit fast rampup).
17static const int kMeasureSecondsFastUpscale = 2;
18static const int kMeasureSecondsUpscale = 5;
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000019static const int kFramedropPercentThreshold = 60;
Alex Glazneva9d08922016-02-19 15:24:06 -080020static const int kHdResolutionThreshold = 700 * 500;
21static const int kHdBitrateThresholdKbps = 500;
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000022
Peter Boström6a688f52015-06-22 08:02:58 +020023const int QualityScaler::kDefaultLowQpDenominator = 3;
24// Note that this is the same for width and height to permit 120x90 in both
25// portrait and landscape mode.
26const int QualityScaler::kDefaultMinDownscaleDimension = 90;
27
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000028QualityScaler::QualityScaler()
pboscbac40d2016-04-13 02:51:02 -070029 : low_qp_threshold_(-1),
jackychen6e2ce6e2015-07-13 16:26:33 -070030 framerate_down_(false),
Peter Boström6a688f52015-06-22 08:02:58 +020031 min_width_(kDefaultMinDownscaleDimension),
philipel5908c712015-12-21 08:23:20 -080032 min_height_(kDefaultMinDownscaleDimension) {}
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000033
Peter Boström17417702015-09-25 17:03:26 +020034void QualityScaler::Init(int low_qp_threshold,
35 int high_qp_threshold,
Alex Glazneva9d08922016-02-19 15:24:06 -080036 bool use_framerate_reduction,
37 int initial_bitrate_kbps,
38 int width,
pboscbac40d2016-04-13 02:51:02 -070039 int height,
40 int fps) {
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000041 ClearSamples();
jackychen98d8cf52015-05-21 11:12:02 -070042 low_qp_threshold_ = low_qp_threshold;
Peter Boström17417702015-09-25 17:03:26 +020043 high_qp_threshold_ = high_qp_threshold;
jackychen6e2ce6e2015-07-13 16:26:33 -070044 use_framerate_reduction_ = use_framerate_reduction;
Peter Boström01bcbd02016-03-22 21:44:29 +010045 downscale_shift_ = 0;
pboscbac40d2016-04-13 02:51:02 -070046 // Use a faster window for upscaling initially (but be more graceful later).
47 // This enables faster initial rampups without risking strong up-down
48 // behavior later.
49 measure_seconds_upscale_ = kMeasureSecondsFastUpscale;
Peter Boström01bcbd02016-03-22 21:44:29 +010050 const int init_width = width;
51 const int init_height = height;
Alex Glazneva9d08922016-02-19 15:24:06 -080052 // TODO(glaznev): Investigate using thresholds for other resolutions
53 // or threshold tables.
54 if (initial_bitrate_kbps > 0 &&
55 initial_bitrate_kbps < kHdBitrateThresholdKbps) {
56 // Start scaling to roughly VGA.
57 while (width * height > kHdResolutionThreshold) {
58 ++downscale_shift_;
59 width /= 2;
60 height /= 2;
61 }
62 }
Peter Boström01bcbd02016-03-22 21:44:29 +010063 UpdateTargetResolution(init_width, init_height);
pboscbac40d2016-04-13 02:51:02 -070064 ReportFramerate(fps);
jackychen6e2ce6e2015-07-13 16:26:33 -070065 target_framerate_ = -1;
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000066}
67
jackychen61b4d512015-04-21 15:30:11 -070068void QualityScaler::SetMinResolution(int min_width, int min_height) {
69 min_width_ = min_width;
70 min_height_ = min_height;
71}
72
jackychen98d8cf52015-05-21 11:12:02 -070073// Report framerate(fps) to estimate # of samples.
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000074void QualityScaler::ReportFramerate(int framerate) {
jackychen6e2ce6e2015-07-13 16:26:33 -070075 framerate_ = framerate;
pboscbac40d2016-04-13 02:51:02 -070076 UpdateSampleCounts();
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000077}
78
jackychen98d8cf52015-05-21 11:12:02 -070079void QualityScaler::ReportQP(int qp) {
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000080 framedrop_percent_.AddSample(0);
pboscbac40d2016-04-13 02:51:02 -070081 average_qp_downscale_.AddSample(qp);
82 average_qp_upscale_.AddSample(qp);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000083}
84
85void QualityScaler::ReportDroppedFrame() {
86 framedrop_percent_.AddSample(100);
87}
88
jackychen6e2ce6e2015-07-13 16:26:33 -070089void QualityScaler::OnEncodeFrame(const VideoFrame& frame) {
jackychen61b4d512015-04-21 15:30:11 -070090 // Should be set through InitEncode -> Should be set by now.
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000091 assert(low_qp_threshold_ >= 0);
pboscbac40d2016-04-13 02:51:02 -070092 assert(num_samples_upscale_ > 0);
93 assert(num_samples_downscale_ > 0);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000094
jackychen61b4d512015-04-21 15:30:11 -070095 // Update scale factor.
jackychen5af6d472015-05-21 14:11:36 -070096 int avg_drop = 0;
97 int avg_qp = 0;
jackychen6e2ce6e2015-07-13 16:26:33 -070098
99 // When encoder consistently overshoots, framerate reduction and spatial
100 // resizing will be triggered to get a smoother video.
pboscbac40d2016-04-13 02:51:02 -0700101 if ((framedrop_percent_.GetAverage(num_samples_downscale_, &avg_drop) &&
Peter Boström17417702015-09-25 17:03:26 +0200102 avg_drop >= kFramedropPercentThreshold) ||
pboscbac40d2016-04-13 02:51:02 -0700103 (average_qp_downscale_.GetAverage(num_samples_downscale_, &avg_qp) &&
Peter Boström17417702015-09-25 17:03:26 +0200104 avg_qp > high_qp_threshold_)) {
jackychen6e2ce6e2015-07-13 16:26:33 -0700105 // Reducing frame rate before spatial resolution change.
106 // Reduce frame rate only when it is above a certain number.
107 // Only one reduction is allowed for now.
108 // TODO(jackychen): Allow more than one framerate reduction.
109 if (use_framerate_reduction_ && !framerate_down_ && framerate_ >= 20) {
110 target_framerate_ = framerate_ / 2;
111 framerate_down_ = true;
112 // If frame rate has been updated, clear the buffer. We don't want
113 // spatial resolution to change right after frame rate change.
114 ClearSamples();
115 } else {
116 AdjustScale(false);
117 }
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_) {
jackychen6e2ce6e2015-07-13 16:26:33 -0700120 if (use_framerate_reduction_ && framerate_down_) {
121 target_framerate_ = -1;
122 framerate_down_ = false;
123 ClearSamples();
124 } else {
125 AdjustScale(true);
126 }
jackychen61b4d512015-04-21 15:30:11 -0700127 }
Peter Boström01bcbd02016-03-22 21:44:29 +0100128 UpdateTargetResolution(frame.width(), frame.height());
jackychen6e2ce6e2015-07-13 16:26:33 -0700129}
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000130
jackychen6e2ce6e2015-07-13 16:26:33 -0700131QualityScaler::Resolution QualityScaler::GetScaledResolution() const {
132 return res_;
133}
134
135int QualityScaler::GetTargetFramerate() const {
136 return target_framerate_;
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000137}
138
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700139const VideoFrame& QualityScaler::GetScaledFrame(const VideoFrame& frame) {
jackychen6e2ce6e2015-07-13 16:26:33 -0700140 Resolution res = GetScaledResolution();
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000141 if (res.width == frame.width())
142 return frame;
143
philipel5908c712015-12-21 08:23:20 -0800144 scaler_.Set(frame.width(), frame.height(), res.width, res.height, kI420,
145 kI420, kScaleBox);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000146 if (scaler_.Scale(frame, &scaled_frame_) != 0)
147 return frame;
148
149 scaled_frame_.set_ntp_time_ms(frame.ntp_time_ms());
150 scaled_frame_.set_timestamp(frame.timestamp());
151 scaled_frame_.set_render_time_ms(frame.render_time_ms());
152
153 return scaled_frame_;
154}
155
Peter Boström01bcbd02016-03-22 21:44:29 +0100156void QualityScaler::UpdateTargetResolution(int frame_width, int frame_height) {
157 assert(downscale_shift_ >= 0);
158 res_.width = frame_width;
159 res_.height = frame_height;
160 for (int shift = downscale_shift_;
161 shift > 0 && (res_.width / 2 >= min_width_) &&
162 (res_.height / 2 >= min_height_);
163 --shift) {
164 res_.width /= 2;
165 res_.height /= 2;
166 }
167}
168
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000169void QualityScaler::ClearSamples() {
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000170 framedrop_percent_.Reset();
pboscbac40d2016-04-13 02:51:02 -0700171 average_qp_downscale_.Reset();
172 average_qp_upscale_.Reset();
173}
174
175void QualityScaler::UpdateSampleCounts() {
176 num_samples_downscale_ = static_cast<size_t>(
177 kMeasureSecondsDownscale * (framerate_ < kMinFps ? kMinFps : framerate_));
178 num_samples_upscale_ = static_cast<size_t>(
179 measure_seconds_upscale_ * (framerate_ < kMinFps ? kMinFps : framerate_));
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000180}
181
182void QualityScaler::AdjustScale(bool up) {
183 downscale_shift_ += up ? -1 : 1;
184 if (downscale_shift_ < 0)
185 downscale_shift_ = 0;
pboscbac40d2016-04-13 02:51:02 -0700186 if (!up) {
187 // Hit first downscale, start using a slower threshold for going up.
188 measure_seconds_upscale_ = kMeasureSecondsUpscale;
189 UpdateSampleCounts();
190 }
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000191 ClearSamples();
192}
193
194} // namespace webrtc