blob: 76bf9f5b0399ed0d7558eed398f6894797490672 [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;
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000017
Peter Boström6a688f52015-06-22 08:02:58 +020018const int QualityScaler::kDefaultLowQpDenominator = 3;
19// Note that this is the same for width and height to permit 120x90 in both
20// portrait and landscape mode.
21const int QualityScaler::kDefaultMinDownscaleDimension = 90;
22
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000023QualityScaler::QualityScaler()
Peter Boström6a688f52015-06-22 08:02:58 +020024 : num_samples_(0),
25 low_qp_threshold_(-1),
26 downscale_shift_(0),
jackychen6e2ce6e2015-07-13 16:26:33 -070027 framerate_down_(false),
Peter Boström6a688f52015-06-22 08:02:58 +020028 min_width_(kDefaultMinDownscaleDimension),
philipel5908c712015-12-21 08:23:20 -080029 min_height_(kDefaultMinDownscaleDimension) {}
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,
33 bool use_framerate_reduction) {
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000034 ClearSamples();
jackychen98d8cf52015-05-21 11:12:02 -070035 low_qp_threshold_ = low_qp_threshold;
Peter Boström17417702015-09-25 17:03:26 +020036 high_qp_threshold_ = high_qp_threshold;
jackychen6e2ce6e2015-07-13 16:26:33 -070037 use_framerate_reduction_ = use_framerate_reduction;
38 target_framerate_ = -1;
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000039}
40
jackychen61b4d512015-04-21 15:30:11 -070041void QualityScaler::SetMinResolution(int min_width, int min_height) {
42 min_width_ = min_width;
43 min_height_ = min_height;
44}
45
jackychen98d8cf52015-05-21 11:12:02 -070046// Report framerate(fps) to estimate # of samples.
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000047void QualityScaler::ReportFramerate(int framerate) {
48 num_samples_ = static_cast<size_t>(
49 kMeasureSeconds * (framerate < kMinFps ? kMinFps : framerate));
jackychen6e2ce6e2015-07-13 16:26:33 -070050 framerate_ = framerate;
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000051}
52
jackychen98d8cf52015-05-21 11:12:02 -070053void QualityScaler::ReportQP(int qp) {
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000054 framedrop_percent_.AddSample(0);
jackychen98d8cf52015-05-21 11:12:02 -070055 average_qp_.AddSample(qp);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000056}
57
58void QualityScaler::ReportDroppedFrame() {
59 framedrop_percent_.AddSample(100);
60}
61
jackychen6e2ce6e2015-07-13 16:26:33 -070062void QualityScaler::OnEncodeFrame(const VideoFrame& frame) {
jackychen61b4d512015-04-21 15:30:11 -070063 // Should be set through InitEncode -> Should be set by now.
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000064 assert(low_qp_threshold_ >= 0);
65 assert(num_samples_ > 0);
jackychen6e2ce6e2015-07-13 16:26:33 -070066 res_.width = frame.width();
67 res_.height = frame.height();
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000068
jackychen61b4d512015-04-21 15:30:11 -070069 // Update scale factor.
jackychen5af6d472015-05-21 14:11:36 -070070 int avg_drop = 0;
71 int avg_qp = 0;
jackychen6e2ce6e2015-07-13 16:26:33 -070072
73 // When encoder consistently overshoots, framerate reduction and spatial
74 // resizing will be triggered to get a smoother video.
Peter Boström17417702015-09-25 17:03:26 +020075 if ((framedrop_percent_.GetAverage(num_samples_, &avg_drop) &&
76 avg_drop >= kFramedropPercentThreshold) ||
77 (average_qp_.GetAverage(num_samples_, &avg_qp) &&
78 avg_qp > high_qp_threshold_)) {
jackychen6e2ce6e2015-07-13 16:26:33 -070079 // Reducing frame rate before spatial resolution change.
80 // Reduce frame rate only when it is above a certain number.
81 // Only one reduction is allowed for now.
82 // TODO(jackychen): Allow more than one framerate reduction.
83 if (use_framerate_reduction_ && !framerate_down_ && framerate_ >= 20) {
84 target_framerate_ = framerate_ / 2;
85 framerate_down_ = true;
86 // If frame rate has been updated, clear the buffer. We don't want
87 // spatial resolution to change right after frame rate change.
88 ClearSamples();
89 } else {
90 AdjustScale(false);
91 }
jackychen98d8cf52015-05-21 11:12:02 -070092 } else if (average_qp_.GetAverage(num_samples_, &avg_qp) &&
philipel5908c712015-12-21 08:23:20 -080093 avg_qp <= low_qp_threshold_) {
jackychen6e2ce6e2015-07-13 16:26:33 -070094 if (use_framerate_reduction_ && framerate_down_) {
95 target_framerate_ = -1;
96 framerate_down_ = false;
97 ClearSamples();
98 } else {
99 AdjustScale(true);
100 }
jackychen61b4d512015-04-21 15:30:11 -0700101 }
102
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000103 assert(downscale_shift_ >= 0);
104 for (int shift = downscale_shift_;
jackychen6e2ce6e2015-07-13 16:26:33 -0700105 shift > 0 && (res_.width / 2 >= min_width_) &&
philipel5908c712015-12-21 08:23:20 -0800106 (res_.height / 2 >= min_height_);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000107 --shift) {
jackychen6e2ce6e2015-07-13 16:26:33 -0700108 res_.width /= 2;
109 res_.height /= 2;
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000110 }
jackychen6e2ce6e2015-07-13 16:26:33 -0700111}
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000112
jackychen6e2ce6e2015-07-13 16:26:33 -0700113QualityScaler::Resolution QualityScaler::GetScaledResolution() const {
114 return res_;
115}
116
117int QualityScaler::GetTargetFramerate() const {
118 return target_framerate_;
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000119}
120
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700121const VideoFrame& QualityScaler::GetScaledFrame(const VideoFrame& frame) {
jackychen6e2ce6e2015-07-13 16:26:33 -0700122 Resolution res = GetScaledResolution();
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000123 if (res.width == frame.width())
124 return frame;
125
philipel5908c712015-12-21 08:23:20 -0800126 scaler_.Set(frame.width(), frame.height(), res.width, res.height, kI420,
127 kI420, kScaleBox);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000128 if (scaler_.Scale(frame, &scaled_frame_) != 0)
129 return frame;
130
131 scaled_frame_.set_ntp_time_ms(frame.ntp_time_ms());
132 scaled_frame_.set_timestamp(frame.timestamp());
133 scaled_frame_.set_render_time_ms(frame.render_time_ms());
134
135 return scaled_frame_;
136}
137
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000138void QualityScaler::ClearSamples() {
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000139 framedrop_percent_.Reset();
jackychen98d8cf52015-05-21 11:12:02 -0700140 average_qp_.Reset();
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000141}
142
143void QualityScaler::AdjustScale(bool up) {
144 downscale_shift_ += up ? -1 : 1;
145 if (downscale_shift_ < 0)
146 downscale_shift_ = 0;
147 ClearSamples();
148}
149
150} // namespace webrtc