blob: 0e1f1d4248bd7e97a66542da765e12b18a3c5464 [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;
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
28QualityScaler::QualityScaler()
Peter Boström926dfcd2016-04-14 14:48:10 +020029 : low_qp_threshold_(-1), framerate_down_(false) {}
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000030
Peter Boström17417702015-09-25 17:03:26 +020031void QualityScaler::Init(int low_qp_threshold,
32 int high_qp_threshold,
Alex Glazneva9d08922016-02-19 15:24:06 -080033 bool use_framerate_reduction,
34 int initial_bitrate_kbps,
35 int width,
pboscbac40d2016-04-13 02:51:02 -070036 int height,
37 int fps) {
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000038 ClearSamples();
jackychen98d8cf52015-05-21 11:12:02 -070039 low_qp_threshold_ = low_qp_threshold;
Peter Boström17417702015-09-25 17:03:26 +020040 high_qp_threshold_ = high_qp_threshold;
jackychen6e2ce6e2015-07-13 16:26:33 -070041 use_framerate_reduction_ = use_framerate_reduction;
Peter Boström01bcbd02016-03-22 21:44:29 +010042 downscale_shift_ = 0;
pboscbac40d2016-04-13 02:51:02 -070043 // Use a faster window for upscaling initially (but be more graceful later).
44 // This enables faster initial rampups without risking strong up-down
45 // behavior later.
46 measure_seconds_upscale_ = kMeasureSecondsFastUpscale;
Peter Boström01bcbd02016-03-22 21:44:29 +010047 const int init_width = width;
48 const int init_height = height;
Alex Glazneva9d08922016-02-19 15:24:06 -080049 // TODO(glaznev): Investigate using thresholds for other resolutions
50 // or threshold tables.
51 if (initial_bitrate_kbps > 0 &&
52 initial_bitrate_kbps < kHdBitrateThresholdKbps) {
53 // Start scaling to roughly VGA.
54 while (width * height > kHdResolutionThreshold) {
55 ++downscale_shift_;
56 width /= 2;
57 height /= 2;
58 }
59 }
Peter Boström01bcbd02016-03-22 21:44:29 +010060 UpdateTargetResolution(init_width, init_height);
pboscbac40d2016-04-13 02:51:02 -070061 ReportFramerate(fps);
jackychen6e2ce6e2015-07-13 16:26:33 -070062 target_framerate_ = -1;
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000063}
64
jackychen98d8cf52015-05-21 11:12:02 -070065// Report framerate(fps) to estimate # of samples.
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000066void QualityScaler::ReportFramerate(int framerate) {
jackychen6e2ce6e2015-07-13 16:26:33 -070067 framerate_ = framerate;
pboscbac40d2016-04-13 02:51:02 -070068 UpdateSampleCounts();
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000069}
70
jackychen98d8cf52015-05-21 11:12:02 -070071void QualityScaler::ReportQP(int qp) {
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000072 framedrop_percent_.AddSample(0);
pboscbac40d2016-04-13 02:51:02 -070073 average_qp_downscale_.AddSample(qp);
74 average_qp_upscale_.AddSample(qp);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000075}
76
77void QualityScaler::ReportDroppedFrame() {
78 framedrop_percent_.AddSample(100);
79}
80
jackychen6e2ce6e2015-07-13 16:26:33 -070081void QualityScaler::OnEncodeFrame(const VideoFrame& frame) {
jackychen61b4d512015-04-21 15:30:11 -070082 // Should be set through InitEncode -> Should be set by now.
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000083 assert(low_qp_threshold_ >= 0);
pboscbac40d2016-04-13 02:51:02 -070084 assert(num_samples_upscale_ > 0);
85 assert(num_samples_downscale_ > 0);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000086
jackychen61b4d512015-04-21 15:30:11 -070087 // Update scale factor.
jackychen5af6d472015-05-21 14:11:36 -070088 int avg_drop = 0;
89 int avg_qp = 0;
jackychen6e2ce6e2015-07-13 16:26:33 -070090
91 // When encoder consistently overshoots, framerate reduction and spatial
92 // resizing will be triggered to get a smoother video.
pboscbac40d2016-04-13 02:51:02 -070093 if ((framedrop_percent_.GetAverage(num_samples_downscale_, &avg_drop) &&
Peter Boström17417702015-09-25 17:03:26 +020094 avg_drop >= kFramedropPercentThreshold) ||
pboscbac40d2016-04-13 02:51:02 -070095 (average_qp_downscale_.GetAverage(num_samples_downscale_, &avg_qp) &&
Peter Boström17417702015-09-25 17:03:26 +020096 avg_qp > high_qp_threshold_)) {
jackychen6e2ce6e2015-07-13 16:26:33 -070097 // Reducing frame rate before spatial resolution change.
98 // Reduce frame rate only when it is above a certain number.
99 // Only one reduction is allowed for now.
100 // TODO(jackychen): Allow more than one framerate reduction.
101 if (use_framerate_reduction_ && !framerate_down_ && framerate_ >= 20) {
102 target_framerate_ = framerate_ / 2;
103 framerate_down_ = true;
104 // If frame rate has been updated, clear the buffer. We don't want
105 // spatial resolution to change right after frame rate change.
106 ClearSamples();
107 } else {
108 AdjustScale(false);
109 }
pboscbac40d2016-04-13 02:51:02 -0700110 } else if (average_qp_upscale_.GetAverage(num_samples_upscale_, &avg_qp) &&
philipel5908c712015-12-21 08:23:20 -0800111 avg_qp <= low_qp_threshold_) {
jackychen6e2ce6e2015-07-13 16:26:33 -0700112 if (use_framerate_reduction_ && framerate_down_) {
113 target_framerate_ = -1;
114 framerate_down_ = false;
115 ClearSamples();
116 } else {
117 AdjustScale(true);
118 }
jackychen61b4d512015-04-21 15:30:11 -0700119 }
Peter Boström01bcbd02016-03-22 21:44:29 +0100120 UpdateTargetResolution(frame.width(), frame.height());
jackychen6e2ce6e2015-07-13 16:26:33 -0700121}
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000122
jackychen6e2ce6e2015-07-13 16:26:33 -0700123QualityScaler::Resolution QualityScaler::GetScaledResolution() const {
124 return res_;
125}
126
127int QualityScaler::GetTargetFramerate() const {
128 return target_framerate_;
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000129}
130
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700131const VideoFrame& QualityScaler::GetScaledFrame(const VideoFrame& frame) {
jackychen6e2ce6e2015-07-13 16:26:33 -0700132 Resolution res = GetScaledResolution();
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000133 if (res.width == frame.width())
134 return frame;
135
philipel5908c712015-12-21 08:23:20 -0800136 scaler_.Set(frame.width(), frame.height(), res.width, res.height, kI420,
137 kI420, kScaleBox);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000138 if (scaler_.Scale(frame, &scaled_frame_) != 0)
139 return frame;
140
141 scaled_frame_.set_ntp_time_ms(frame.ntp_time_ms());
142 scaled_frame_.set_timestamp(frame.timestamp());
143 scaled_frame_.set_render_time_ms(frame.render_time_ms());
144
145 return scaled_frame_;
146}
147
Peter Boström01bcbd02016-03-22 21:44:29 +0100148void QualityScaler::UpdateTargetResolution(int frame_width, int frame_height) {
149 assert(downscale_shift_ >= 0);
150 res_.width = frame_width;
151 res_.height = frame_height;
152 for (int shift = downscale_shift_;
Peter Boström926dfcd2016-04-14 14:48:10 +0200153 shift > 0 && (res_.width / 2 >= kMinDownscaleDimension) &&
154 (res_.height / 2 >= kMinDownscaleDimension);
Peter Boström01bcbd02016-03-22 21:44:29 +0100155 --shift) {
156 res_.width /= 2;
157 res_.height /= 2;
158 }
159}
160
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000161void QualityScaler::ClearSamples() {
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000162 framedrop_percent_.Reset();
pboscbac40d2016-04-13 02:51:02 -0700163 average_qp_downscale_.Reset();
164 average_qp_upscale_.Reset();
165}
166
167void QualityScaler::UpdateSampleCounts() {
168 num_samples_downscale_ = static_cast<size_t>(
169 kMeasureSecondsDownscale * (framerate_ < kMinFps ? kMinFps : framerate_));
170 num_samples_upscale_ = static_cast<size_t>(
171 measure_seconds_upscale_ * (framerate_ < kMinFps ? kMinFps : framerate_));
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000172}
173
174void QualityScaler::AdjustScale(bool up) {
175 downscale_shift_ += up ? -1 : 1;
176 if (downscale_shift_ < 0)
177 downscale_shift_ = 0;
pboscbac40d2016-04-13 02:51:02 -0700178 if (!up) {
179 // Hit first downscale, start using a slower threshold for going up.
180 measure_seconds_upscale_ = kMeasureSecondsUpscale;
181 UpdateSampleCounts();
182 }
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000183 ClearSamples();
184}
185
186} // namespace webrtc