blob: a46e8502cb310e459c77a97f0fd0b4ab6264b86e [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
14static const int kMinFps = 10;
15static const int kMeasureSeconds = 5;
16static const int kFramedropPercentThreshold = 60;
Alex Glazneva9d08922016-02-19 15:24:06 -080017static const int kHdResolutionThreshold = 700 * 500;
18static const int kHdBitrateThresholdKbps = 500;
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000019
Peter Boström6a688f52015-06-22 08:02:58 +020020const int QualityScaler::kDefaultLowQpDenominator = 3;
21// Note that this is the same for width and height to permit 120x90 in both
22// portrait and landscape mode.
23const int QualityScaler::kDefaultMinDownscaleDimension = 90;
24
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000025QualityScaler::QualityScaler()
Peter Boström6a688f52015-06-22 08:02:58 +020026 : num_samples_(0),
27 low_qp_threshold_(-1),
28 downscale_shift_(0),
jackychen6e2ce6e2015-07-13 16:26:33 -070029 framerate_down_(false),
Peter Boström6a688f52015-06-22 08:02:58 +020030 min_width_(kDefaultMinDownscaleDimension),
philipel5908c712015-12-21 08:23:20 -080031 min_height_(kDefaultMinDownscaleDimension) {}
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,
38 int height) {
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000039 ClearSamples();
jackychen98d8cf52015-05-21 11:12:02 -070040 low_qp_threshold_ = low_qp_threshold;
Peter Boström17417702015-09-25 17:03:26 +020041 high_qp_threshold_ = high_qp_threshold;
jackychen6e2ce6e2015-07-13 16:26:33 -070042 use_framerate_reduction_ = use_framerate_reduction;
Alex Glazneva9d08922016-02-19 15:24:06 -080043 // TODO(glaznev): Investigate using thresholds for other resolutions
44 // or threshold tables.
45 if (initial_bitrate_kbps > 0 &&
46 initial_bitrate_kbps < kHdBitrateThresholdKbps) {
47 // Start scaling to roughly VGA.
48 while (width * height > kHdResolutionThreshold) {
49 ++downscale_shift_;
50 width /= 2;
51 height /= 2;
52 }
53 }
54 res_.width = width;
55 res_.height = height;
jackychen6e2ce6e2015-07-13 16:26:33 -070056 target_framerate_ = -1;
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000057}
58
jackychen61b4d512015-04-21 15:30:11 -070059void QualityScaler::SetMinResolution(int min_width, int min_height) {
60 min_width_ = min_width;
61 min_height_ = min_height;
62}
63
jackychen98d8cf52015-05-21 11:12:02 -070064// Report framerate(fps) to estimate # of samples.
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000065void QualityScaler::ReportFramerate(int framerate) {
66 num_samples_ = static_cast<size_t>(
67 kMeasureSeconds * (framerate < kMinFps ? kMinFps : framerate));
jackychen6e2ce6e2015-07-13 16:26:33 -070068 framerate_ = framerate;
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);
jackychen98d8cf52015-05-21 11:12:02 -070073 average_qp_.AddSample(qp);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000074}
75
76void QualityScaler::ReportDroppedFrame() {
77 framedrop_percent_.AddSample(100);
78}
79
jackychen6e2ce6e2015-07-13 16:26:33 -070080void QualityScaler::OnEncodeFrame(const VideoFrame& frame) {
jackychen61b4d512015-04-21 15:30:11 -070081 // Should be set through InitEncode -> Should be set by now.
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000082 assert(low_qp_threshold_ >= 0);
83 assert(num_samples_ > 0);
jackychen6e2ce6e2015-07-13 16:26:33 -070084 res_.width = frame.width();
85 res_.height = frame.height();
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.
Peter Boström17417702015-09-25 17:03:26 +020093 if ((framedrop_percent_.GetAverage(num_samples_, &avg_drop) &&
94 avg_drop >= kFramedropPercentThreshold) ||
95 (average_qp_.GetAverage(num_samples_, &avg_qp) &&
96 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 }
jackychen98d8cf52015-05-21 11:12:02 -0700110 } else if (average_qp_.GetAverage(num_samples_, &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 }
120
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000121 assert(downscale_shift_ >= 0);
122 for (int shift = downscale_shift_;
jackychen6e2ce6e2015-07-13 16:26:33 -0700123 shift > 0 && (res_.width / 2 >= min_width_) &&
philipel5908c712015-12-21 08:23:20 -0800124 (res_.height / 2 >= min_height_);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000125 --shift) {
jackychen6e2ce6e2015-07-13 16:26:33 -0700126 res_.width /= 2;
127 res_.height /= 2;
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000128 }
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
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000156void QualityScaler::ClearSamples() {
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000157 framedrop_percent_.Reset();
jackychen98d8cf52015-05-21 11:12:02 -0700158 average_qp_.Reset();
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000159}
160
161void QualityScaler::AdjustScale(bool up) {
162 downscale_shift_ += up ? -1 : 1;
163 if (downscale_shift_ < 0)
164 downscale_shift_ = 0;
165 ClearSamples();
166}
167
168} // namespace webrtc