Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(434)

Side by Side Diff: webrtc/modules/video_coding/utility/quality_scaler.cc

Issue 2398963003: Move usage of QualityScaler to ViEEncoder. (Closed)
Patch Set: Fix flaky test Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include "webrtc/modules/video_coding/utility/quality_scaler.h" 11 #include "webrtc/modules/video_coding/utility/quality_scaler.h"
12 12
13 #include <math.h> 13 #include <math.h>
14 14
15 #include <algorithm> 15 #include <algorithm>
16 #include <memory>
16 17
17 #include "webrtc/base/checks.h" 18 #include "webrtc/base/checks.h"
19 #include "webrtc/base/logging.h"
20 #include "webrtc/base/task_queue.h"
18 21
19 // TODO(kthelgason): Some versions of Android have issues with log2. 22 // TODO(kthelgason): Some versions of Android have issues with log2.
20 // See https://code.google.com/p/android/issues/detail?id=212634 for details 23 // See https://code.google.com/p/android/issues/detail?id=212634 for details
21 #if defined(WEBRTC_ANDROID) 24 #if defined(WEBRTC_ANDROID)
22 #define log2(x) (log(x) / log(2)) 25 #define log2(x) (log(x) / log(2))
23 #endif 26 #endif
24 27
25 namespace webrtc { 28 namespace webrtc {
26 29
27 namespace { 30 namespace {
28 // Threshold constant used until first downscale (to permit fast rampup). 31 // Threshold constant used until first downscale (to permit fast rampup).
29 static const int kMeasureSecondsFastUpscale = 2; 32 static const int kMeasureMs = 2000;
30 static const int kMeasureSecondsUpscale = 5; 33 static const float kSamplePeriodScaleFactor = 2.5;
31 static const int kMeasureSecondsDownscale = 5;
32 static const int kFramedropPercentThreshold = 60; 34 static const int kFramedropPercentThreshold = 60;
33 // 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.
35 static const int kMinDownscaleDimension = 140;
36 // 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.
39 static const int kVgaBitrateThresholdKbps = 500;
40 static const int kVgaNumPixels = 700 * 500; // 640x480
41 static const int kQvgaBitrateThresholdKbps = 250;
42 static const int kQvgaNumPixels = 400 * 300; // 320x240
43
44 // QP scaling threshold defaults: 35 // QP scaling threshold defaults:
45 static const int kLowH264QpThreshold = 24; 36 static const int kLowH264QpThreshold = 24;
46 static const int kHighH264QpThreshold = 37; 37 static const int kHighH264QpThreshold = 37;
47 // QP is obtained from VP8-bitstream for HW, so the QP corresponds to the 38 // 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]. 39 // bitstream range of [0, 127] and not the user-level range of [0,63].
49 static const int kLowVp8QpThreshold = 29; 40 static const int kLowVp8QpThreshold = 29;
50 static const int kHighVp8QpThreshold = 95; 41 static const int kHighVp8QpThreshold = 95;
51 } // namespace 42 const auto scale_reason_ = ScalingObserverInterface::ScaleReason::kQuality;
magjed_webrtc 2016/11/10 13:07:00 Not allowed to use auto for non-local variables
kthelgason 2016/11/10 15:38:27 Done.
52 43
53 // Default values. Should immediately get set to something more sensible. 44 static QualityScaler::QPThresholds CodecTypeToDefaultThresholds(
54 QualityScaler::QualityScaler() 45 VideoCodecType codec_type) {
55 : average_qp_(kMeasureSecondsUpscale * 30), 46 int low = -1;
56 framedrop_percent_(kMeasureSecondsUpscale * 30), 47 int high = -1;
57 low_qp_threshold_(-1) {}
58
59 void 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) { 48 switch (codec_type) {
66 case kVideoCodecH264: 49 case kVideoCodecH264:
67 low = kLowH264QpThreshold; 50 low = kLowH264QpThreshold;
68 high = kHighH264QpThreshold; 51 high = kHighH264QpThreshold;
69 break; 52 break;
70 case kVideoCodecVP8: 53 case kVideoCodecVP8:
71 low = kLowVp8QpThreshold; 54 low = kLowVp8QpThreshold;
72 high = kHighVp8QpThreshold; 55 high = kHighVp8QpThreshold;
73 break; 56 break;
74 default: 57 default:
75 RTC_NOTREACHED() << "Invalid codec type for QualityScaler."; 58 RTC_NOTREACHED() << "Invalid codec type for QualityScaler.";
76 } 59 }
77 Init(low, high, initial_bitrate_kbps, width, height, fps); 60 return QualityScaler::QPThresholds(low, high);
61 }
62 } // namespace
63
64 class QualityScaler::CheckQPTask : public rtc::QueuedTask {
65 public:
66 explicit CheckQPTask(QualityScaler* scaler) : scaler_(scaler) {
67 LOG(LS_INFO) << "Created CheckQPTask. Scheduling on queue...";
68 const auto q = rtc::TaskQueue::Current();
69 q->PostDelayedTask(std::unique_ptr<rtc::QueuedTask>(this),
70 scaler_->GetSamplingPeriodMs());
71 }
72 void Stop() {
73 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_);
74 LOG(LS_INFO) << "Stopping QP Check task.";
75 stop_ = true;
76 }
77
78 private:
79 bool Run() override {
80 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_);
81 if (stop_)
82 return true; // TaskQueue will free this task.
83 scaler_->CheckQP();
84 rtc::TaskQueue::Current()->PostDelayedTask(
85 std::unique_ptr<rtc::QueuedTask>(this), scaler_->GetSamplingPeriodMs());
86 return false; // Retain the task in order to reuse it.
87 }
88
89 QualityScaler* const scaler_;
90 bool stop_ = false;
91 rtc::SequencedTaskChecker task_checker_;
92 };
93
94 QualityScaler::QualityScaler(ScalingObserverInterface* observer,
95 VideoCodecType codec_type)
96 : QualityScaler(observer, CodecTypeToDefaultThresholds(codec_type)) {}
97
98 // Protected ctor, for testing purposes only.
99 QualityScaler::QualityScaler(ScalingObserverInterface* observer,
100 QPThresholds thresholds,
101 int64_t sampling_period)
102 : QualityScaler(observer, thresholds) {
103 check_qp_task_->Stop();
magjed_webrtc 2016/11/10 13:07:00 I would like to avoid the 'check_qp_task_->Stop()'
kthelgason 2016/11/10 15:38:27 Thanks, good point. Done.
104 sampling_period_ms_ = sampling_period;
105 check_qp_task_ = new CheckQPTask(this);
78 } 106 }
79 107
80 void QualityScaler::Init(int low_qp_threshold, 108 QualityScaler::QualityScaler(ScalingObserverInterface* observer,
81 int high_qp_threshold, 109 QPThresholds thresholds)
82 int initial_bitrate_kbps, 110 : check_qp_task_(nullptr),
83 int width, 111 observer_(observer),
84 int height, 112 sampling_period_ms_(kMeasureMs),
85 int fps) { 113 fast_rampup_(true),
86 ClearSamples(); 114 // Arbitrarily choose size based on 30 fps for 5 seconds.
87 low_qp_threshold_ = low_qp_threshold; 115 average_qp_(5 * 30),
88 high_qp_threshold_ = high_qp_threshold; 116 framedrop_percent_(5 * 30),
89 downscale_shift_ = 0; 117 thresholds_(thresholds) {
90 fast_rampup_ = true; 118 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_);
91 119 RTC_DCHECK(observer_ != nullptr);
92 const int init_width = width; 120 check_qp_task_ = new CheckQPTask(this);
93 const int init_height = height;
94 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) {
101 ++downscale_shift_;
102 width /= 2;
103 height /= 2;
104 }
105 }
106 UpdateTargetResolution(init_width, init_height);
107 ReportFramerate(fps);
108 } 121 }
109 122
110 // Report framerate(fps) to estimate # of samples. 123 QualityScaler::~QualityScaler() {
111 void QualityScaler::ReportFramerate(int framerate) { 124 check_qp_task_->Stop();
112 // Use a faster window for upscaling initially. 125 check_qp_task_ = nullptr;
magjed_webrtc 2016/11/10 13:07:00 no need to set it to nullptr in the dtor.
kthelgason 2016/11/10 15:38:27 Done.
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;
118 } 126 }
119 127
120 void QualityScaler::ReportQP(int qp) { 128 int64_t QualityScaler::GetSamplingPeriodMs() const {
129 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_);
130 return sampling_period_ms_;
magjed_webrtc 2016/11/10 13:07:00 Can you make |sampling_period_ms_| const and retur
kthelgason 2016/11/10 15:38:27 Done.
131 }
132
133 void QualityScaler::ReportDroppedFrame() const {
134 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_);
135 framedrop_percent_.AddSample(100);
136 }
137
138 void QualityScaler::ReportQP(int qp) const {
139 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_);
121 framedrop_percent_.AddSample(0); 140 framedrop_percent_.AddSample(0);
122 average_qp_.AddSample(qp); 141 average_qp_.AddSample(qp);
123 } 142 }
124 143
125 void QualityScaler::ReportDroppedFrame() { 144 void QualityScaler::CheckQP() {
126 framedrop_percent_.AddSample(100); 145 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_);
127 }
128
129 void QualityScaler::OnEncodeFrame(int width, int height) {
130 // Should be set through InitEncode -> Should be set by now. 146 // Should be set through InitEncode -> Should be set by now.
131 RTC_DCHECK_GE(low_qp_threshold_, 0); 147 RTC_DCHECK_GE(thresholds_.low, 0);
132 if (target_res_.width != width || target_res_.height != height) { 148 LOG(LS_INFO) << "Checking if average QP exceeds threshold";
133 UpdateTargetResolution(width, height);
134 }
135
136 // Check if we should scale down due to high frame drop. 149 // Check if we should scale down due to high frame drop.
137 const auto drop_rate = framedrop_percent_.GetAverage(num_samples_downscale_); 150 const auto drop_rate = framedrop_percent_.GetAverage();
magjed_webrtc 2016/11/10 13:07:00 Don't use auto here.
kthelgason 2016/11/10 15:38:27 Done.
138 if (drop_rate && *drop_rate >= kFramedropPercentThreshold) { 151 if (drop_rate && *drop_rate >= kFramedropPercentThreshold) {
139 ScaleDown(); 152 ReportQPHigh();
140 return; 153 return;
141 } 154 }
142 155
143 // Check if we should scale up or down based on QP. 156 // Check if we should scale up or down based on QP.
144 const auto avg_qp_down = average_qp_.GetAverage(num_samples_downscale_); 157 const auto avg_qp = average_qp_.GetAverage();
145 if (avg_qp_down && *avg_qp_down > high_qp_threshold_) { 158 if (avg_qp && *avg_qp > thresholds_.high) {
146 ScaleDown(); 159 ReportQPHigh();
147 return; 160 return;
148 } 161 }
149 const auto avg_qp_up = average_qp_.GetAverage(num_samples_upscale_); 162 if (avg_qp && *avg_qp <= thresholds_.low) {
150 if (avg_qp_up && *avg_qp_up <= low_qp_threshold_) {
151 // QP has been low. We want to try a higher resolution. 163 // QP has been low. We want to try a higher resolution.
152 ScaleUp(); 164 ReportQPLow();
153 return; 165 return;
154 } 166 }
155 } 167 }
156 168
157 void QualityScaler::ScaleUp() { 169 void QualityScaler::ReportQPLow() {
158 downscale_shift_ = std::max(0, downscale_shift_ - 1); 170 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_);
171 LOG(LS_INFO) << "QP has been low, asking for higher resolution.";
159 ClearSamples(); 172 ClearSamples();
173 observer_->ScaleUp(scale_reason_);
160 } 174 }
161 175
162 void QualityScaler::ScaleDown() { 176 void QualityScaler::ReportQPHigh() {
163 downscale_shift_ = std::min(maximum_shift_, downscale_shift_ + 1); 177 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_);
178 LOG(LS_INFO) << "QP has been high , asking for lower resolution.";
164 ClearSamples(); 179 ClearSamples();
180 observer_->ScaleDown(scale_reason_);
165 // If we've scaled down, wait longer before scaling up again. 181 // If we've scaled down, wait longer before scaling up again.
166 if (fast_rampup_) { 182 if (fast_rampup_) {
167 fast_rampup_ = false; 183 fast_rampup_ = false;
168 num_samples_upscale_ = (num_samples_upscale_ / kMeasureSecondsFastUpscale) * 184 sampling_period_ms_ *= kSamplePeriodScaleFactor;
169 kMeasureSecondsUpscale;
170 } 185 }
171 } 186 }
172 187
173 QualityScaler::Resolution QualityScaler::GetScaledResolution() const {
174 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};
177 }
178
179 rtc::scoped_refptr<VideoFrameBuffer> QualityScaler::GetScaledBuffer(
180 const rtc::scoped_refptr<VideoFrameBuffer>& frame) {
181 Resolution res = GetScaledResolution();
182 const int src_width = frame->width();
183 const int src_height = frame->height();
184
185 if (res.width == src_width && res.height == src_height)
186 return frame;
187 rtc::scoped_refptr<I420Buffer> scaled_buffer =
188 pool_.CreateBuffer(res.width, res.height);
189
190 scaled_buffer->ScaleFrom(frame);
191
192 return scaled_buffer;
193 }
194
195 void QualityScaler::UpdateTargetResolution(int width, int height) {
196 if (width < kMinDownscaleDimension || height < kMinDownscaleDimension) {
197 maximum_shift_ = 0;
198 } else {
199 maximum_shift_ = static_cast<int>(
200 log2(std::min(width, height) / kMinDownscaleDimension));
201 }
202 target_res_ = Resolution{width, height};
203 }
204
205 void QualityScaler::ClearSamples() { 188 void QualityScaler::ClearSamples() {
189 RTC_DCHECK_CALLED_SEQUENTIALLY(&task_checker_);
206 framedrop_percent_.Reset(); 190 framedrop_percent_.Reset();
207 average_qp_.Reset(); 191 average_qp_.Reset();
208 } 192 }
209
210
211 } // namespace webrtc 193 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698