blob: 7a2a9c02615f6d548388d95a70af2f7a6ad74936 [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),
27 min_width_(kDefaultMinDownscaleDimension),
28 min_height_(kDefaultMinDownscaleDimension) {
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000029}
30
jackychen98d8cf52015-05-21 11:12:02 -070031void QualityScaler::Init(int low_qp_threshold) {
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000032 ClearSamples();
jackychen98d8cf52015-05-21 11:12:02 -070033 low_qp_threshold_ = low_qp_threshold;
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000034}
35
jackychen61b4d512015-04-21 15:30:11 -070036void QualityScaler::SetMinResolution(int min_width, int min_height) {
37 min_width_ = min_width;
38 min_height_ = min_height;
39}
40
jackychen98d8cf52015-05-21 11:12:02 -070041// Report framerate(fps) to estimate # of samples.
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000042void QualityScaler::ReportFramerate(int framerate) {
43 num_samples_ = static_cast<size_t>(
44 kMeasureSeconds * (framerate < kMinFps ? kMinFps : framerate));
45}
46
jackychen98d8cf52015-05-21 11:12:02 -070047void QualityScaler::ReportQP(int qp) {
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000048 framedrop_percent_.AddSample(0);
jackychen98d8cf52015-05-21 11:12:02 -070049 average_qp_.AddSample(qp);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000050}
51
52void QualityScaler::ReportDroppedFrame() {
53 framedrop_percent_.AddSample(100);
54}
55
56QualityScaler::Resolution QualityScaler::GetScaledResolution(
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070057 const VideoFrame& frame) {
jackychen61b4d512015-04-21 15:30:11 -070058 // Should be set through InitEncode -> Should be set by now.
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000059 assert(low_qp_threshold_ >= 0);
60 assert(num_samples_ > 0);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000061
62 Resolution res;
63 res.width = frame.width();
64 res.height = frame.height();
65
jackychen61b4d512015-04-21 15:30:11 -070066 // Update scale factor.
jackychen5af6d472015-05-21 14:11:36 -070067 int avg_drop = 0;
68 int avg_qp = 0;
jackychen61b4d512015-04-21 15:30:11 -070069 if (framedrop_percent_.GetAverage(num_samples_, &avg_drop) &&
70 avg_drop >= kFramedropPercentThreshold) {
71 AdjustScale(false);
jackychen98d8cf52015-05-21 11:12:02 -070072 } else if (average_qp_.GetAverage(num_samples_, &avg_qp) &&
73 avg_qp <= low_qp_threshold_) {
jackychen61b4d512015-04-21 15:30:11 -070074 AdjustScale(true);
75 }
76
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000077 assert(downscale_shift_ >= 0);
78 for (int shift = downscale_shift_;
Peter Boström6a688f52015-06-22 08:02:58 +020079 shift > 0 && (res.width >> 1 >= min_width_) &&
80 (res.height >> 1 >= min_height_);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000081 --shift) {
82 res.width >>= 1;
83 res.height >>= 1;
84 }
85
86 return res;
87}
88
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070089const VideoFrame& QualityScaler::GetScaledFrame(const VideoFrame& frame) {
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000090 Resolution res = GetScaledResolution(frame);
91 if (res.width == frame.width())
92 return frame;
93
94 scaler_.Set(frame.width(),
95 frame.height(),
96 res.width,
97 res.height,
98 kI420,
99 kI420,
100 kScaleBox);
101 if (scaler_.Scale(frame, &scaled_frame_) != 0)
102 return frame;
103
104 scaled_frame_.set_ntp_time_ms(frame.ntp_time_ms());
105 scaled_frame_.set_timestamp(frame.timestamp());
106 scaled_frame_.set_render_time_ms(frame.render_time_ms());
107
108 return scaled_frame_;
109}
110
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000111void QualityScaler::ClearSamples() {
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000112 framedrop_percent_.Reset();
jackychen98d8cf52015-05-21 11:12:02 -0700113 average_qp_.Reset();
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000114}
115
116void QualityScaler::AdjustScale(bool up) {
117 downscale_shift_ += up ? -1 : 1;
118 if (downscale_shift_ < 0)
119 downscale_shift_ = 0;
120 ClearSamples();
121}
122
123} // namespace webrtc