blob: 0f63edca7d126d520fff5fd28eb39a8adf8ca66c [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 */
Niels Möller718a7632016-06-13 13:06:01 +020010
kjellander@webrtc.orgb7ce9642015-11-18 23:04:10 +010011#include "webrtc/modules/video_coding/utility/quality_scaler.h"
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000012
Kári Tristan Helgason5a20ed32016-09-15 10:56:19 +020013#include <math.h>
14
kthelgason194f40a2016-09-14 02:14:58 -070015#include <algorithm>
Kári Tristan Helgason5a20ed32016-09-15 10:56:19 +020016
kthelgason478681e2016-09-28 08:17:43 -070017#include "webrtc/base/checks.h"
18
Kári Tristan Helgason5a20ed32016-09-15 10:56:19 +020019// TODO(kthelgason): Some versions of Android have issues with log2.
20// See https://code.google.com/p/android/issues/detail?id=212634 for details
21#if defined(WEBRTC_ANDROID)
22#define log2(x) (log(x) / log(2))
23#endif
kthelgason194f40a2016-09-14 02:14:58 -070024
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000025namespace webrtc {
26
Peter Boström926dfcd2016-04-14 14:48:10 +020027namespace {
pboscbac40d2016-04-13 02:51:02 -070028// Threshold constant used until first downscale (to permit fast rampup).
29static const int kMeasureSecondsFastUpscale = 2;
30static const int kMeasureSecondsUpscale = 5;
Peter Boström2c8a2962016-04-18 12:58:02 +020031static const int kMeasureSecondsDownscale = 5;
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000032static const int kFramedropPercentThreshold = 60;
Peter Boström926dfcd2016-04-14 14:48:10 +020033// Min width/height to downscale to, set to not go below QVGA, but with some
34// margin to permit "almost-QVGA" resolutions, such as QCIF.
35static const int kMinDownscaleDimension = 140;
Peter Boströmb9e77092016-04-18 22:45:55 +020036// Initial resolutions corresponding to a bitrate. Aa bit above their actual
37// values to permit near-VGA and near-QVGA resolutions to use the same
38// mechanism.
39static const int kVgaBitrateThresholdKbps = 500;
40static const int kVgaNumPixels = 700 * 500; // 640x480
41static const int kQvgaBitrateThresholdKbps = 250;
42static const int kQvgaNumPixels = 400 * 300; // 320x240
kthelgason478681e2016-09-28 08:17:43 -070043
44// QP scaling threshold defaults:
45static const int kLowH264QpThreshold = 24;
46static const int kHighH264QpThreshold = 37;
47// QP is obtained from VP8-bitstream for HW, so the QP corresponds to the
48// bitstream range of [0, 127] and not the user-level range of [0,63].
49static const int kLowVp8QpThreshold = 29;
50static const int kHighVp8QpThreshold = 95;
Peter Boström926dfcd2016-04-14 14:48:10 +020051} // namespace
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000052
kthelgason194f40a2016-09-14 02:14:58 -070053// Default values. Should immediately get set to something more sensible.
54QualityScaler::QualityScaler()
55 : average_qp_(kMeasureSecondsUpscale * 30),
56 framedrop_percent_(kMeasureSecondsUpscale * 30),
57 low_qp_threshold_(-1) {}
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000058
kthelgason478681e2016-09-28 08:17:43 -070059void QualityScaler::Init(VideoCodecType codec_type,
60 int initial_bitrate_kbps,
61 int width,
62 int height,
63 int fps) {
64 int low = -1, high = -1;
65 switch (codec_type) {
66 case kVideoCodecH264:
67 low = kLowH264QpThreshold;
68 high = kHighH264QpThreshold;
69 break;
70 case kVideoCodecVP8:
71 low = kLowVp8QpThreshold;
72 high = kHighVp8QpThreshold;
73 break;
74 default:
75 RTC_NOTREACHED() << "Invalid codec type for QualityScaler.";
76 }
77 Init(low, high, initial_bitrate_kbps, width, height, fps);
78}
79
Peter Boström17417702015-09-25 17:03:26 +020080void QualityScaler::Init(int low_qp_threshold,
81 int high_qp_threshold,
Alex Glazneva9d08922016-02-19 15:24:06 -080082 int initial_bitrate_kbps,
83 int width,
pboscbac40d2016-04-13 02:51:02 -070084 int height,
85 int fps) {
kthelgason478681e2016-09-28 08:17:43 -070086 ClearSamples();
jackychen98d8cf52015-05-21 11:12:02 -070087 low_qp_threshold_ = low_qp_threshold;
Peter Boström17417702015-09-25 17:03:26 +020088 high_qp_threshold_ = high_qp_threshold;
Peter Boström01bcbd02016-03-22 21:44:29 +010089 downscale_shift_ = 0;
kthelgason194f40a2016-09-14 02:14:58 -070090 fast_rampup_ = true;
kthelgason194f40a2016-09-14 02:14:58 -070091
Peter Boström01bcbd02016-03-22 21:44:29 +010092 const int init_width = width;
93 const int init_height = height;
Peter Boströmb9e77092016-04-18 22:45:55 +020094 if (initial_bitrate_kbps > 0) {
95 int init_num_pixels = width * height;
96 if (initial_bitrate_kbps < kVgaBitrateThresholdKbps)
97 init_num_pixels = kVgaNumPixels;
98 if (initial_bitrate_kbps < kQvgaBitrateThresholdKbps)
99 init_num_pixels = kQvgaNumPixels;
100 while (width * height > init_num_pixels) {
Alex Glazneva9d08922016-02-19 15:24:06 -0800101 ++downscale_shift_;
102 width /= 2;
103 height /= 2;
104 }
105 }
Peter Boström01bcbd02016-03-22 21:44:29 +0100106 UpdateTargetResolution(init_width, init_height);
pboscbac40d2016-04-13 02:51:02 -0700107 ReportFramerate(fps);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000108}
109
jackychen98d8cf52015-05-21 11:12:02 -0700110// Report framerate(fps) to estimate # of samples.
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000111void QualityScaler::ReportFramerate(int framerate) {
kthelgason194f40a2016-09-14 02:14:58 -0700112 // Use a faster window for upscaling initially.
113 // This enables faster initial rampups without risking strong up-down
114 // behavior later.
115 num_samples_upscale_ = framerate * (fast_rampup_ ? kMeasureSecondsFastUpscale
116 : kMeasureSecondsUpscale);
117 num_samples_downscale_ = framerate * kMeasureSecondsDownscale;
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000118}
119
jackychen98d8cf52015-05-21 11:12:02 -0700120void QualityScaler::ReportQP(int qp) {
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000121 framedrop_percent_.AddSample(0);
kthelgason194f40a2016-09-14 02:14:58 -0700122 average_qp_.AddSample(qp);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000123}
124
125void QualityScaler::ReportDroppedFrame() {
126 framedrop_percent_.AddSample(100);
127}
128
Niels Möller718a7632016-06-13 13:06:01 +0200129void QualityScaler::OnEncodeFrame(int width, int height) {
jackychen61b4d512015-04-21 15:30:11 -0700130 // Should be set through InitEncode -> Should be set by now.
Niels Möller718a7632016-06-13 13:06:01 +0200131 RTC_DCHECK_GE(low_qp_threshold_, 0);
kthelgason194f40a2016-09-14 02:14:58 -0700132 if (target_res_.width != width || target_res_.height != height) {
133 UpdateTargetResolution(width, height);
jackychen61b4d512015-04-21 15:30:11 -0700134 }
kthelgason194f40a2016-09-14 02:14:58 -0700135
136 // Check if we should scale down due to high frame drop.
137 const auto drop_rate = framedrop_percent_.GetAverage(num_samples_downscale_);
138 if (drop_rate && *drop_rate >= kFramedropPercentThreshold) {
139 ScaleDown();
140 return;
141 }
142
143 // Check if we should scale up or down based on QP.
144 const auto avg_qp_down = average_qp_.GetAverage(num_samples_downscale_);
145 if (avg_qp_down && *avg_qp_down > high_qp_threshold_) {
146 ScaleDown();
147 return;
148 }
149 const auto avg_qp_up = average_qp_.GetAverage(num_samples_upscale_);
150 if (avg_qp_up && *avg_qp_up <= low_qp_threshold_) {
151 // QP has been low. We want to try a higher resolution.
152 ScaleUp();
153 return;
154 }
155}
156
157void QualityScaler::ScaleUp() {
158 downscale_shift_ = std::max(0, downscale_shift_ - 1);
159 ClearSamples();
160}
161
162void QualityScaler::ScaleDown() {
163 downscale_shift_ = std::min(maximum_shift_, downscale_shift_ + 1);
164 ClearSamples();
165 // If we've scaled down, wait longer before scaling up again.
166 if (fast_rampup_) {
167 fast_rampup_ = false;
168 num_samples_upscale_ = (num_samples_upscale_ / kMeasureSecondsFastUpscale) *
169 kMeasureSecondsUpscale;
170 }
jackychen6e2ce6e2015-07-13 16:26:33 -0700171}
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000172
jackychen6e2ce6e2015-07-13 16:26:33 -0700173QualityScaler::Resolution QualityScaler::GetScaledResolution() const {
kthelgason194f40a2016-09-14 02:14:58 -0700174 const int frame_width = target_res_.width >> downscale_shift_;
175 const int frame_height = target_res_.height >> downscale_shift_;
176 return Resolution{frame_width, frame_height};
jackychen6e2ce6e2015-07-13 16:26:33 -0700177}
178
Niels Möller718a7632016-06-13 13:06:01 +0200179rtc::scoped_refptr<VideoFrameBuffer> QualityScaler::GetScaledBuffer(
180 const rtc::scoped_refptr<VideoFrameBuffer>& frame) {
jackychen6e2ce6e2015-07-13 16:26:33 -0700181 Resolution res = GetScaledResolution();
kthelgason194f40a2016-09-14 02:14:58 -0700182 const int src_width = frame->width();
183 const int src_height = frame->height();
Niels Möller718a7632016-06-13 13:06:01 +0200184
185 if (res.width == src_width && res.height == src_height)
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000186 return frame;
Niels Möller718a7632016-06-13 13:06:01 +0200187 rtc::scoped_refptr<I420Buffer> scaled_buffer =
188 pool_.CreateBuffer(res.width, res.height);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000189
nissee3fe4a72016-11-10 08:44:38 -0800190 scaled_buffer->ScaleFrom(*frame);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000191
Niels Möller718a7632016-06-13 13:06:01 +0200192 return scaled_buffer;
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000193}
194
kthelgason194f40a2016-09-14 02:14:58 -0700195void QualityScaler::UpdateTargetResolution(int width, int height) {
196 if (width < kMinDownscaleDimension || height < kMinDownscaleDimension) {
197 maximum_shift_ = 0;
198 } else {
199 maximum_shift_ = static_cast<int>(
Kári Tristan Helgason5a20ed32016-09-15 10:56:19 +0200200 log2(std::min(width, height) / kMinDownscaleDimension));
Peter Boström919288f2016-05-12 02:17:35 +0200201 }
kthelgason194f40a2016-09-14 02:14:58 -0700202 target_res_ = Resolution{width, height};
Peter Boström01bcbd02016-03-22 21:44:29 +0100203}
204
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000205void QualityScaler::ClearSamples() {
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000206 framedrop_percent_.Reset();
kthelgason194f40a2016-09-14 02:14:58 -0700207 average_qp_.Reset();
pboscbac40d2016-04-13 02:51:02 -0700208}
209
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000210
211} // namespace webrtc