blob: 38b582b99499bf716671e9455b3e4760ae1c8a86 [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;
17static const int kLowQpThresholdDenominator = 3;
jackychen61b4d512015-04-21 15:30:11 -070018static const double kFramesizeFlucThreshold = 0.11;
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000019
20QualityScaler::QualityScaler()
jackychen61b4d512015-04-21 15:30:11 -070021 : num_samples_(0), low_qp_threshold_(-1), downscale_shift_(0),
22 min_width_(0), min_height_(0) {
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000023}
24
25void QualityScaler::Init(int max_qp) {
26 ClearSamples();
jackychen61b4d512015-04-21 15:30:11 -070027 low_qp_threshold_ = max_qp / kLowQpThresholdDenominator;
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000028}
29
jackychen61b4d512015-04-21 15:30:11 -070030void QualityScaler::SetMinResolution(int min_width, int min_height) {
31 min_width_ = min_width;
32 min_height_ = min_height;
33}
34
35// TODO(jackychen): target_framesize should be calculated from average bitrate
36// in the measured period of time.
37// Report framerate(fps) and target_bitrate(kbit/s) to estimate # of samples
38// and get target_framesize_.
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000039void QualityScaler::ReportFramerate(int framerate) {
40 num_samples_ = static_cast<size_t>(
41 kMeasureSeconds * (framerate < kMinFps ? kMinFps : framerate));
42}
43
jackychen61b4d512015-04-21 15:30:11 -070044void QualityScaler::ReportNormalizedQP(int qp) {
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000045 framedrop_percent_.AddSample(0);
jackychen61b4d512015-04-21 15:30:11 -070046 frame_quality_.AddSample(static_cast<double>(qp) / low_qp_threshold_);
47}
48
49void QualityScaler::ReportNormalizedFrameSizeFluctuation(
50 double framesize_deviation) {
51 framedrop_percent_.AddSample(0);
52 frame_quality_.AddSample(framesize_deviation / kFramesizeFlucThreshold);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000053}
54
55void QualityScaler::ReportDroppedFrame() {
56 framedrop_percent_.AddSample(100);
57}
58
59QualityScaler::Resolution QualityScaler::GetScaledResolution(
60 const I420VideoFrame& 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);
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000064
65 Resolution res;
66 res.width = frame.width();
67 res.height = frame.height();
68
jackychen61b4d512015-04-21 15:30:11 -070069 // Update scale factor.
70 int avg_drop;
71 double avg_quality;
72 if (framedrop_percent_.GetAverage(num_samples_, &avg_drop) &&
73 avg_drop >= kFramedropPercentThreshold) {
74 AdjustScale(false);
75 } else if (frame_quality_.GetAverage(num_samples_, &avg_quality) &&
76 avg_quality <= 1.0) {
77 AdjustScale(true);
78 }
79
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000080 assert(downscale_shift_ >= 0);
81 for (int shift = downscale_shift_;
82 shift > 0 && res.width > 1 && res.height > 1;
83 --shift) {
84 res.width >>= 1;
85 res.height >>= 1;
86 }
87
jackychen61b4d512015-04-21 15:30:11 -070088 // Set this limitation for VP8 HW encoder to avoid crash.
89 if (min_width_ > 0 && res.width * res.height < min_width_ * min_height_) {
90 res.width = min_width_;
91 res.height = min_height_;
92 }
93
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000094 return res;
95}
96
97const I420VideoFrame& QualityScaler::GetScaledFrame(
98 const I420VideoFrame& frame) {
99 Resolution res = GetScaledResolution(frame);
100 if (res.width == frame.width())
101 return frame;
102
103 scaler_.Set(frame.width(),
104 frame.height(),
105 res.width,
106 res.height,
107 kI420,
108 kI420,
109 kScaleBox);
110 if (scaler_.Scale(frame, &scaled_frame_) != 0)
111 return frame;
112
113 scaled_frame_.set_ntp_time_ms(frame.ntp_time_ms());
114 scaled_frame_.set_timestamp(frame.timestamp());
115 scaled_frame_.set_render_time_ms(frame.render_time_ms());
116
117 return scaled_frame_;
118}
119
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000120void QualityScaler::ClearSamples() {
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000121 framedrop_percent_.Reset();
jackychen61b4d512015-04-21 15:30:11 -0700122 frame_quality_.Reset();
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000123}
124
125void QualityScaler::AdjustScale(bool up) {
126 downscale_shift_ += up ? -1 : 1;
127 if (downscale_shift_ < 0)
128 downscale_shift_ = 0;
129 ClearSamples();
130}
131
132} // namespace webrtc