blob: f7d4651511297ae986f235297c7bc1af746ebd99 [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 */
jackychen61b4d512015-04-21 15:30:11 -070010#include "webrtc/modules/video_coding/utility/include/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),
29 min_height_(kDefaultMinDownscaleDimension) {
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000030}
31
jackychen6e2ce6e2015-07-13 16:26:33 -070032void QualityScaler::Init(int low_qp_threshold, bool use_framerate_reduction) {
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000033 ClearSamples();
jackychen98d8cf52015-05-21 11:12:02 -070034 low_qp_threshold_ = low_qp_threshold;
jackychen6e2ce6e2015-07-13 16:26:33 -070035 use_framerate_reduction_ = use_framerate_reduction;
36 target_framerate_ = -1;
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000037}
38
jackychen61b4d512015-04-21 15:30:11 -070039void QualityScaler::SetMinResolution(int min_width, int min_height) {
40 min_width_ = min_width;
41 min_height_ = min_height;
42}
43
jackychen98d8cf52015-05-21 11:12:02 -070044// Report framerate(fps) to estimate # of samples.
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000045void QualityScaler::ReportFramerate(int framerate) {
46 num_samples_ = static_cast<size_t>(
47 kMeasureSeconds * (framerate < kMinFps ? kMinFps : framerate));
jackychen6e2ce6e2015-07-13 16:26:33 -070048 framerate_ = framerate;
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000049}
50
jackychen98d8cf52015-05-21 11:12:02 -070051void QualityScaler::ReportQP(int qp) {
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000052 framedrop_percent_.AddSample(0);
jackychen98d8cf52015-05-21 11:12:02 -070053 average_qp_.AddSample(qp);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000054}
55
56void QualityScaler::ReportDroppedFrame() {
57 framedrop_percent_.AddSample(100);
58}
59
jackychen6e2ce6e2015-07-13 16:26:33 -070060void QualityScaler::OnEncodeFrame(const VideoFrame& frame) {
jackychen61b4d512015-04-21 15:30:11 -070061 // Should be set through InitEncode -> Should be set by now.
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000062 assert(low_qp_threshold_ >= 0);
63 assert(num_samples_ > 0);
jackychen6e2ce6e2015-07-13 16:26:33 -070064 res_.width = frame.width();
65 res_.height = frame.height();
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000066
jackychen61b4d512015-04-21 15:30:11 -070067 // Update scale factor.
jackychen5af6d472015-05-21 14:11:36 -070068 int avg_drop = 0;
69 int avg_qp = 0;
jackychen6e2ce6e2015-07-13 16:26:33 -070070
71 // When encoder consistently overshoots, framerate reduction and spatial
72 // resizing will be triggered to get a smoother video.
jackychen61b4d512015-04-21 15:30:11 -070073 if (framedrop_percent_.GetAverage(num_samples_, &avg_drop) &&
74 avg_drop >= kFramedropPercentThreshold) {
jackychen6e2ce6e2015-07-13 16:26:33 -070075 // Reducing frame rate before spatial resolution change.
76 // Reduce frame rate only when it is above a certain number.
77 // Only one reduction is allowed for now.
78 // TODO(jackychen): Allow more than one framerate reduction.
79 if (use_framerate_reduction_ && !framerate_down_ && framerate_ >= 20) {
80 target_framerate_ = framerate_ / 2;
81 framerate_down_ = true;
82 // If frame rate has been updated, clear the buffer. We don't want
83 // spatial resolution to change right after frame rate change.
84 ClearSamples();
85 } else {
86 AdjustScale(false);
87 }
jackychen98d8cf52015-05-21 11:12:02 -070088 } else if (average_qp_.GetAverage(num_samples_, &avg_qp) &&
89 avg_qp <= low_qp_threshold_) {
jackychen6e2ce6e2015-07-13 16:26:33 -070090 if (use_framerate_reduction_ && framerate_down_) {
91 target_framerate_ = -1;
92 framerate_down_ = false;
93 ClearSamples();
94 } else {
95 AdjustScale(true);
96 }
jackychen61b4d512015-04-21 15:30:11 -070097 }
98
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000099 assert(downscale_shift_ >= 0);
100 for (int shift = downscale_shift_;
jackychen6e2ce6e2015-07-13 16:26:33 -0700101 shift > 0 && (res_.width / 2 >= min_width_) &&
102 (res_.height / 2 >= min_height_);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000103 --shift) {
jackychen6e2ce6e2015-07-13 16:26:33 -0700104 res_.width /= 2;
105 res_.height /= 2;
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000106 }
jackychen6e2ce6e2015-07-13 16:26:33 -0700107}
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000108
jackychen6e2ce6e2015-07-13 16:26:33 -0700109QualityScaler::Resolution QualityScaler::GetScaledResolution() const {
110 return res_;
111}
112
113int QualityScaler::GetTargetFramerate() const {
114 return target_framerate_;
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000115}
116
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700117const VideoFrame& QualityScaler::GetScaledFrame(const VideoFrame& frame) {
jackychen6e2ce6e2015-07-13 16:26:33 -0700118 Resolution res = GetScaledResolution();
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000119 if (res.width == frame.width())
120 return frame;
121
122 scaler_.Set(frame.width(),
123 frame.height(),
124 res.width,
125 res.height,
126 kI420,
127 kI420,
128 kScaleBox);
129 if (scaler_.Scale(frame, &scaled_frame_) != 0)
130 return frame;
131
132 scaled_frame_.set_ntp_time_ms(frame.ntp_time_ms());
133 scaled_frame_.set_timestamp(frame.timestamp());
134 scaled_frame_.set_render_time_ms(frame.render_time_ms());
135
136 return scaled_frame_;
137}
138
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000139void QualityScaler::ClearSamples() {
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000140 framedrop_percent_.Reset();
jackychen98d8cf52015-05-21 11:12:02 -0700141 average_qp_.Reset();
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000142}
143
144void QualityScaler::AdjustScale(bool up) {
145 downscale_shift_ += up ? -1 : 1;
146 if (downscale_shift_ < 0)
147 downscale_shift_ = 0;
148 ClearSamples();
149}
150
151} // namespace webrtc