blob: c6a6ba2ea526c2a3a24969430be2dfe3c015d5a3 [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;
16static const int kMeasureSecondsDownscale = 3;
17// Threshold constant used until first downscale (to permit fast rampup).
18static const int kMeasureSecondsFastUpscale = 2;
19static const int kMeasureSecondsUpscale = 5;
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000020static const int kFramedropPercentThreshold = 60;
Alex Glazneva9d08922016-02-19 15:24:06 -080021static const int kHdResolutionThreshold = 700 * 500;
22static const int kHdBitrateThresholdKbps = 500;
Peter Boström926dfcd2016-04-14 14:48:10 +020023// Min width/height to downscale to, set to not go below QVGA, but with some
24// margin to permit "almost-QVGA" resolutions, such as QCIF.
25static const int kMinDownscaleDimension = 140;
26} // namespace
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000027
Peter Boström6a688f52015-06-22 08:02:58 +020028const int QualityScaler::kDefaultLowQpDenominator = 3;
Peter Boström6a688f52015-06-22 08:02:58 +020029
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000030QualityScaler::QualityScaler()
Peter Boström926dfcd2016-04-14 14:48:10 +020031 : low_qp_threshold_(-1), framerate_down_(false) {}
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000032
Peter Boström17417702015-09-25 17:03:26 +020033void QualityScaler::Init(int low_qp_threshold,
34 int high_qp_threshold,
Alex Glazneva9d08922016-02-19 15:24:06 -080035 bool use_framerate_reduction,
36 int initial_bitrate_kbps,
37 int width,
pboscbac40d2016-04-13 02:51:02 -070038 int height,
39 int fps) {
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000040 ClearSamples();
jackychen98d8cf52015-05-21 11:12:02 -070041 low_qp_threshold_ = low_qp_threshold;
Peter Boström17417702015-09-25 17:03:26 +020042 high_qp_threshold_ = high_qp_threshold;
jackychen6e2ce6e2015-07-13 16:26:33 -070043 use_framerate_reduction_ = use_framerate_reduction;
Peter Boström01bcbd02016-03-22 21:44:29 +010044 downscale_shift_ = 0;
pboscbac40d2016-04-13 02:51:02 -070045 // Use a faster window for upscaling initially (but be more graceful later).
46 // This enables faster initial rampups without risking strong up-down
47 // behavior later.
48 measure_seconds_upscale_ = kMeasureSecondsFastUpscale;
Peter Boström01bcbd02016-03-22 21:44:29 +010049 const int init_width = width;
50 const int init_height = height;
Alex Glazneva9d08922016-02-19 15:24:06 -080051 // TODO(glaznev): Investigate using thresholds for other resolutions
52 // or threshold tables.
53 if (initial_bitrate_kbps > 0 &&
54 initial_bitrate_kbps < kHdBitrateThresholdKbps) {
55 // Start scaling to roughly VGA.
56 while (width * height > kHdResolutionThreshold) {
57 ++downscale_shift_;
58 width /= 2;
59 height /= 2;
60 }
61 }
Peter Boström01bcbd02016-03-22 21:44:29 +010062 UpdateTargetResolution(init_width, init_height);
pboscbac40d2016-04-13 02:51:02 -070063 ReportFramerate(fps);
jackychen6e2ce6e2015-07-13 16:26:33 -070064 target_framerate_ = -1;
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000065}
66
jackychen98d8cf52015-05-21 11:12:02 -070067// Report framerate(fps) to estimate # of samples.
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000068void QualityScaler::ReportFramerate(int framerate) {
jackychen6e2ce6e2015-07-13 16:26:33 -070069 framerate_ = framerate;
pboscbac40d2016-04-13 02:51:02 -070070 UpdateSampleCounts();
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000071}
72
jackychen98d8cf52015-05-21 11:12:02 -070073void QualityScaler::ReportQP(int qp) {
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000074 framedrop_percent_.AddSample(0);
pboscbac40d2016-04-13 02:51:02 -070075 average_qp_downscale_.AddSample(qp);
76 average_qp_upscale_.AddSample(qp);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000077}
78
79void QualityScaler::ReportDroppedFrame() {
80 framedrop_percent_.AddSample(100);
81}
82
jackychen6e2ce6e2015-07-13 16:26:33 -070083void QualityScaler::OnEncodeFrame(const VideoFrame& frame) {
jackychen61b4d512015-04-21 15:30:11 -070084 // Should be set through InitEncode -> Should be set by now.
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000085 assert(low_qp_threshold_ >= 0);
pboscbac40d2016-04-13 02:51:02 -070086 assert(num_samples_upscale_ > 0);
87 assert(num_samples_downscale_ > 0);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000088
jackychen61b4d512015-04-21 15:30:11 -070089 // Update scale factor.
jackychen5af6d472015-05-21 14:11:36 -070090 int avg_drop = 0;
91 int avg_qp = 0;
jackychen6e2ce6e2015-07-13 16:26:33 -070092
93 // When encoder consistently overshoots, framerate reduction and spatial
94 // resizing will be triggered to get a smoother video.
pboscbac40d2016-04-13 02:51:02 -070095 if ((framedrop_percent_.GetAverage(num_samples_downscale_, &avg_drop) &&
Peter Boström17417702015-09-25 17:03:26 +020096 avg_drop >= kFramedropPercentThreshold) ||
pboscbac40d2016-04-13 02:51:02 -070097 (average_qp_downscale_.GetAverage(num_samples_downscale_, &avg_qp) &&
Peter Boström17417702015-09-25 17:03:26 +020098 avg_qp > high_qp_threshold_)) {
jackychen6e2ce6e2015-07-13 16:26:33 -070099 // Reducing frame rate before spatial resolution change.
100 // Reduce frame rate only when it is above a certain number.
101 // Only one reduction is allowed for now.
102 // TODO(jackychen): Allow more than one framerate reduction.
103 if (use_framerate_reduction_ && !framerate_down_ && framerate_ >= 20) {
104 target_framerate_ = framerate_ / 2;
105 framerate_down_ = true;
106 // If frame rate has been updated, clear the buffer. We don't want
107 // spatial resolution to change right after frame rate change.
108 ClearSamples();
109 } else {
110 AdjustScale(false);
111 }
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_) {
jackychen6e2ce6e2015-07-13 16:26:33 -0700114 if (use_framerate_reduction_ && framerate_down_) {
115 target_framerate_ = -1;
116 framerate_down_ = false;
117 ClearSamples();
118 } else {
119 AdjustScale(true);
120 }
jackychen61b4d512015-04-21 15:30:11 -0700121 }
Peter Boström01bcbd02016-03-22 21:44:29 +0100122 UpdateTargetResolution(frame.width(), frame.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
129int QualityScaler::GetTargetFramerate() const {
130 return target_framerate_;
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000131}
132
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700133const VideoFrame& QualityScaler::GetScaledFrame(const VideoFrame& frame) {
jackychen6e2ce6e2015-07-13 16:26:33 -0700134 Resolution res = GetScaledResolution();
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000135 if (res.width == frame.width())
136 return frame;
137
philipel5908c712015-12-21 08:23:20 -0800138 scaler_.Set(frame.width(), frame.height(), res.width, res.height, kI420,
139 kI420, kScaleBox);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000140 if (scaler_.Scale(frame, &scaled_frame_) != 0)
141 return frame;
142
143 scaled_frame_.set_ntp_time_ms(frame.ntp_time_ms());
144 scaled_frame_.set_timestamp(frame.timestamp());
145 scaled_frame_.set_render_time_ms(frame.render_time_ms());
146
147 return scaled_frame_;
148}
149
Peter Boström01bcbd02016-03-22 21:44:29 +0100150void QualityScaler::UpdateTargetResolution(int frame_width, int frame_height) {
151 assert(downscale_shift_ >= 0);
152 res_.width = frame_width;
153 res_.height = frame_height;
154 for (int shift = downscale_shift_;
Peter Boström926dfcd2016-04-14 14:48:10 +0200155 shift > 0 && (res_.width / 2 >= kMinDownscaleDimension) &&
156 (res_.height / 2 >= kMinDownscaleDimension);
Peter Boström01bcbd02016-03-22 21:44:29 +0100157 --shift) {
158 res_.width /= 2;
159 res_.height /= 2;
160 }
161}
162
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000163void QualityScaler::ClearSamples() {
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000164 framedrop_percent_.Reset();
pboscbac40d2016-04-13 02:51:02 -0700165 average_qp_downscale_.Reset();
166 average_qp_upscale_.Reset();
167}
168
169void QualityScaler::UpdateSampleCounts() {
170 num_samples_downscale_ = static_cast<size_t>(
171 kMeasureSecondsDownscale * (framerate_ < kMinFps ? kMinFps : framerate_));
172 num_samples_upscale_ = static_cast<size_t>(
173 measure_seconds_upscale_ * (framerate_ < kMinFps ? kMinFps : framerate_));
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000174}
175
176void QualityScaler::AdjustScale(bool up) {
177 downscale_shift_ += up ? -1 : 1;
178 if (downscale_shift_ < 0)
179 downscale_shift_ = 0;
pboscbac40d2016-04-13 02:51:02 -0700180 if (!up) {
181 // Hit first downscale, start using a slower threshold for going up.
182 measure_seconds_upscale_ = kMeasureSecondsUpscale;
183 UpdateSampleCounts();
184 }
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000185 ClearSamples();
186}
187
188} // namespace webrtc