blob: 30a5776b2d05b6d92495e67dfb75743972809854 [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
Peter Boström3c6eac22016-04-26 13:37:10 +020033QualityScaler::QualityScaler() : low_qp_threshold_(-1) {}
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000034
Peter Boström17417702015-09-25 17:03:26 +020035void QualityScaler::Init(int low_qp_threshold,
36 int high_qp_threshold,
Alex Glazneva9d08922016-02-19 15:24:06 -080037 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;
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;
Peter Boströmb9e77092016-04-18 22:45:55 +020051 if (initial_bitrate_kbps > 0) {
52 int init_num_pixels = width * height;
53 if (initial_bitrate_kbps < kVgaBitrateThresholdKbps)
54 init_num_pixels = kVgaNumPixels;
55 if (initial_bitrate_kbps < kQvgaBitrateThresholdKbps)
56 init_num_pixels = kQvgaNumPixels;
57 while (width * height > init_num_pixels) {
Alex Glazneva9d08922016-02-19 15:24:06 -080058 ++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);
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
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_)) {
Peter Boström3c6eac22016-04-26 13:37:10 +020097 AdjustScale(false);
pboscbac40d2016-04-13 02:51:02 -070098 } else if (average_qp_upscale_.GetAverage(num_samples_upscale_, &avg_qp) &&
philipel5908c712015-12-21 08:23:20 -080099 avg_qp <= low_qp_threshold_) {
Peter Boström3c6eac22016-04-26 13:37:10 +0200100 AdjustScale(true);
jackychen61b4d512015-04-21 15:30:11 -0700101 }
Peter Boström01bcbd02016-03-22 21:44:29 +0100102 UpdateTargetResolution(frame.width(), frame.height());
jackychen6e2ce6e2015-07-13 16:26:33 -0700103}
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000104
jackychen6e2ce6e2015-07-13 16:26:33 -0700105QualityScaler::Resolution QualityScaler::GetScaledResolution() const {
106 return res_;
107}
108
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700109const VideoFrame& QualityScaler::GetScaledFrame(const VideoFrame& frame) {
jackychen6e2ce6e2015-07-13 16:26:33 -0700110 Resolution res = GetScaledResolution();
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000111 if (res.width == frame.width())
112 return frame;
113
philipel5908c712015-12-21 08:23:20 -0800114 scaler_.Set(frame.width(), frame.height(), res.width, res.height, kI420,
115 kI420, kScaleBox);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000116 if (scaler_.Scale(frame, &scaled_frame_) != 0)
117 return frame;
118
Perba7dc722016-04-19 15:01:23 +0200119 // TODO(perkj): Refactor the scaler to not own |scaled_frame|. VideoFrame are
120 // just thin wrappers so instead the scaler should return a
121 // rtc::scoped_refptr<VideoFrameBuffer> and a new VideoFrame be created with
122 // the meta data from |frame|. That way we would not have to set all these
123 // meta data.
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000124 scaled_frame_.set_ntp_time_ms(frame.ntp_time_ms());
125 scaled_frame_.set_timestamp(frame.timestamp());
126 scaled_frame_.set_render_time_ms(frame.render_time_ms());
Perba7dc722016-04-19 15:01:23 +0200127 scaled_frame_.set_rotation(frame.rotation());
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000128
129 return scaled_frame_;
130}
131
Peter Boström01bcbd02016-03-22 21:44:29 +0100132void QualityScaler::UpdateTargetResolution(int frame_width, int frame_height) {
133 assert(downscale_shift_ >= 0);
134 res_.width = frame_width;
135 res_.height = frame_height;
136 for (int shift = downscale_shift_;
Peter Boström926dfcd2016-04-14 14:48:10 +0200137 shift > 0 && (res_.width / 2 >= kMinDownscaleDimension) &&
138 (res_.height / 2 >= kMinDownscaleDimension);
Peter Boström01bcbd02016-03-22 21:44:29 +0100139 --shift) {
140 res_.width /= 2;
141 res_.height /= 2;
142 }
143}
144
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000145void QualityScaler::ClearSamples() {
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000146 framedrop_percent_.Reset();
pboscbac40d2016-04-13 02:51:02 -0700147 average_qp_downscale_.Reset();
148 average_qp_upscale_.Reset();
149}
150
151void QualityScaler::UpdateSampleCounts() {
152 num_samples_downscale_ = static_cast<size_t>(
153 kMeasureSecondsDownscale * (framerate_ < kMinFps ? kMinFps : framerate_));
154 num_samples_upscale_ = static_cast<size_t>(
155 measure_seconds_upscale_ * (framerate_ < kMinFps ? kMinFps : framerate_));
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000156}
157
158void QualityScaler::AdjustScale(bool up) {
159 downscale_shift_ += up ? -1 : 1;
160 if (downscale_shift_ < 0)
161 downscale_shift_ = 0;
pboscbac40d2016-04-13 02:51:02 -0700162 if (!up) {
163 // Hit first downscale, start using a slower threshold for going up.
164 measure_seconds_upscale_ = kMeasureSecondsUpscale;
165 UpdateSampleCounts();
166 }
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000167 ClearSamples();
168}
169
170} // namespace webrtc