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

Side by Side Diff: webrtc/modules/video_coding/codecs/vp8/screenshare_layers.cc

Issue 2488833004: Reland of Issue 2434073003: Extract bitrate allocation ... (Closed)
Patch Set: Rebase 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 /* Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. 1 /* Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
2 * 2 *
3 * Use of this source code is governed by a BSD-style license 3 * Use of this source code is governed by a BSD-style license
4 * that can be found in the LICENSE file in the root of the source 4 * that can be found in the LICENSE file in the root of the source
5 * tree. An additional intellectual property rights grant can be found 5 * tree. An additional intellectual property rights grant can be found
6 * in the file PATENTS. All contributing project authors may 6 * in the file PATENTS. All contributing project authors may
7 * be found in the AUTHORS file in the root of the source tree. 7 * be found in the AUTHORS file in the root of the source tree.
8 */ 8 */
9 9
10 #include "webrtc/modules/video_coding/codecs/vp8/screenshare_layers.h" 10 #include "webrtc/modules/video_coding/codecs/vp8/screenshare_layers.h"
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
58 number_of_temporal_layers_(num_temporal_layers), 58 number_of_temporal_layers_(num_temporal_layers),
59 last_base_layer_sync_(false), 59 last_base_layer_sync_(false),
60 tl0_pic_idx_(initial_tl0_pic_idx), 60 tl0_pic_idx_(initial_tl0_pic_idx),
61 active_layer_(-1), 61 active_layer_(-1),
62 last_timestamp_(-1), 62 last_timestamp_(-1),
63 last_sync_timestamp_(-1), 63 last_sync_timestamp_(-1),
64 last_emitted_tl0_timestamp_(-1), 64 last_emitted_tl0_timestamp_(-1),
65 min_qp_(-1), 65 min_qp_(-1),
66 max_qp_(-1), 66 max_qp_(-1),
67 max_debt_bytes_(0), 67 max_debt_bytes_(0),
68 frame_rate_(-1) { 68 framerate_(-1),
69 bitrate_updated_(false) {
69 RTC_CHECK_GT(num_temporal_layers, 0); 70 RTC_CHECK_GT(num_temporal_layers, 0);
70 RTC_CHECK_LE(num_temporal_layers, 2); 71 RTC_CHECK_LE(num_temporal_layers, 2);
71 } 72 }
72 73
73 ScreenshareLayers::~ScreenshareLayers() { 74 ScreenshareLayers::~ScreenshareLayers() {
74 UpdateHistograms(); 75 UpdateHistograms();
75 } 76 }
76 77
77 int ScreenshareLayers::CurrentLayerId() const { 78 int ScreenshareLayers::CurrentLayerId() const {
78 // Codec does not use temporal layers for screenshare. 79 // Codec does not use temporal layers for screenshare.
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 flags = -1; 130 flags = -1;
130 ++stats_.num_dropped_frames_; 131 ++stats_.num_dropped_frames_;
131 break; 132 break;
132 default: 133 default:
133 flags = -1; 134 flags = -1;
134 RTC_NOTREACHED(); 135 RTC_NOTREACHED();
135 } 136 }
136 137
137 int64_t ts_diff; 138 int64_t ts_diff;
138 if (last_timestamp_ == -1) { 139 if (last_timestamp_ == -1) {
139 ts_diff = kOneSecond90Khz / (frame_rate_ <= 0 ? 5 : frame_rate_); 140 ts_diff = kOneSecond90Khz / (framerate_ <= 0 ? 5 : framerate_);
140 } else { 141 } else {
141 ts_diff = unwrapped_timestamp - last_timestamp_; 142 ts_diff = unwrapped_timestamp - last_timestamp_;
142 } 143 }
143 // Make sure both frame droppers leak out bits. 144 // Make sure both frame droppers leak out bits.
144 layers_[0].UpdateDebt(ts_diff / 90); 145 layers_[0].UpdateDebt(ts_diff / 90);
145 layers_[1].UpdateDebt(ts_diff / 90); 146 layers_[1].UpdateDebt(ts_diff / 90);
146 last_timestamp_ = timestamp; 147 last_timestamp_ = timestamp;
147 return flags; 148 return flags;
148 } 149 }
149 150
150 bool ScreenshareLayers::ConfigureBitrates(int bitrate_kbps, 151 std::vector<uint32_t> ScreenshareLayers::OnRatesUpdated(int bitrate_kbps,
151 int max_bitrate_kbps, 152 int max_bitrate_kbps,
152 int framerate, 153 int framerate) {
153 vpx_codec_enc_cfg_t* cfg) {
154 layers_[0].target_rate_kbps_ = bitrate_kbps; 154 layers_[0].target_rate_kbps_ = bitrate_kbps;
155 layers_[1].target_rate_kbps_ = max_bitrate_kbps; 155 layers_[1].target_rate_kbps_ = max_bitrate_kbps;
156 framerate_ = framerate;
157 bitrate_updated_ = true;
156 158
157 int target_bitrate_kbps = bitrate_kbps; 159 std::vector<uint32_t> allocation;
158 160 allocation.push_back(bitrate_kbps);
159 if (cfg != nullptr) { 161 if (max_bitrate_kbps > bitrate_kbps)
160 if (number_of_temporal_layers_ > 1) { 162 allocation.push_back(max_bitrate_kbps - bitrate_kbps);
161 // Calculate a codec target bitrate. This may be higher than TL0, gaining 163 return allocation;
162 // quality at the expense of frame rate at TL0. Constraints:
163 // - TL0 frame rate no less than framerate / kMaxTL0FpsReduction.
164 // - Target rate * kAcceptableTargetOvershoot should not exceed TL1 rate.
165 target_bitrate_kbps =
166 std::min(bitrate_kbps * kMaxTL0FpsReduction,
167 max_bitrate_kbps / kAcceptableTargetOvershoot);
168
169 cfg->rc_target_bitrate = std::max(bitrate_kbps, target_bitrate_kbps);
170 }
171
172 // Don't reconfigure qp limits during quality boost frames.
173 if (active_layer_ == -1 ||
174 layers_[active_layer_].state != TemporalLayer::State::kQualityBoost) {
175 min_qp_ = cfg->rc_min_quantizer;
176 max_qp_ = cfg->rc_max_quantizer;
177 // After a dropped frame, a frame with max qp will be encoded and the
178 // quality will then ramp up from there. To boost the speed of recovery,
179 // encode the next frame with lower max qp. TL0 is the most important to
180 // improve since the errors in this layer will propagate to TL1.
181 // Currently, reduce max qp by 20% for TL0 and 15% for TL1.
182 layers_[0].enhanced_max_qp = min_qp_ + (((max_qp_ - min_qp_) * 80) / 100);
183 layers_[1].enhanced_max_qp = min_qp_ + (((max_qp_ - min_qp_) * 85) / 100);
184 }
185 }
186
187 int avg_frame_size = (target_bitrate_kbps * 1000) / (8 * framerate);
188 max_debt_bytes_ = 4 * avg_frame_size;
189
190 return true;
191 } 164 }
192 165
193 void ScreenshareLayers::FrameEncoded(unsigned int size, 166 void ScreenshareLayers::FrameEncoded(unsigned int size,
194 uint32_t timestamp, 167 uint32_t timestamp,
195 int qp) { 168 int qp) {
196 if (number_of_temporal_layers_ == 1) 169 if (number_of_temporal_layers_ == 1)
197 return; 170 return;
198 171
199 RTC_DCHECK_NE(-1, active_layer_); 172 RTC_DCHECK_NE(-1, active_layer_);
200 if (size == 0) { 173 if (size == 0) {
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
272 return false; 245 return false;
273 } 246 }
274 // Issue a sync frame if difference in quality between TL0 and TL1 isn't too 247 // Issue a sync frame if difference in quality between TL0 and TL1 isn't too
275 // large. 248 // large.
276 if (layers_[0].last_qp - layers_[1].last_qp < kQpDeltaThresholdForSync) 249 if (layers_[0].last_qp - layers_[1].last_qp < kQpDeltaThresholdForSync)
277 return true; 250 return true;
278 return false; 251 return false;
279 } 252 }
280 253
281 bool ScreenshareLayers::UpdateConfiguration(vpx_codec_enc_cfg_t* cfg) { 254 bool ScreenshareLayers::UpdateConfiguration(vpx_codec_enc_cfg_t* cfg) {
255 bool cfg_updated = false;
256 if (bitrate_updated_) {
257 uint32_t target_bitrate_kbps = layers_[0].target_rate_kbps_;
258
259 if (number_of_temporal_layers_ > 1) {
260 // Calculate a codec target bitrate. This may be higher than TL0, gaining
261 // quality at the expense of frame rate at TL0. Constraints:
262 // - TL0 frame rate no less than framerate / kMaxTL0FpsReduction.
263 // - Target rate * kAcceptableTargetOvershoot should not exceed TL1 rate.
264 target_bitrate_kbps =
265 std::min(layers_[0].target_rate_kbps_ * kMaxTL0FpsReduction,
266 layers_[1].target_rate_kbps_ / kAcceptableTargetOvershoot);
267
268 cfg->rc_target_bitrate =
269 std::max(layers_[0].target_rate_kbps_, target_bitrate_kbps);
270 }
271
272 // Don't reconfigure qp limits during quality boost frames.
273 if (active_layer_ == -1 ||
274 layers_[active_layer_].state != TemporalLayer::State::kQualityBoost) {
275 min_qp_ = cfg->rc_min_quantizer;
276 max_qp_ = cfg->rc_max_quantizer;
277 // After a dropped frame, a frame with max qp will be encoded and the
278 // quality will then ramp up from there. To boost the speed of recovery,
279 // encode the next frame with lower max qp. TL0 is the most important to
280 // improve since the errors in this layer will propagate to TL1.
281 // Currently, reduce max qp by 20% for TL0 and 15% for TL1.
282 layers_[0].enhanced_max_qp = min_qp_ + (((max_qp_ - min_qp_) * 80) / 100);
283 layers_[1].enhanced_max_qp = min_qp_ + (((max_qp_ - min_qp_) * 85) / 100);
284 }
285
286 if (framerate_ > 0) {
287 int avg_frame_size = (target_bitrate_kbps * 1000) / (8 * framerate_);
288 max_debt_bytes_ = 4 * avg_frame_size;
289 }
290
291 bitrate_updated_ = false;
292 cfg_updated = true;
293 }
294
295 // Don't try to update boosts state if not active yet.
296 if (active_layer_ == -1)
297 return cfg_updated;
298
282 if (max_qp_ == -1 || number_of_temporal_layers_ <= 1) 299 if (max_qp_ == -1 || number_of_temporal_layers_ <= 1)
283 return false; 300 return cfg_updated;
284 RTC_DCHECK_NE(-1, active_layer_);
285 301
286 // If layer is in the quality boost state (following a dropped frame), update 302 // If layer is in the quality boost state (following a dropped frame), update
287 // the configuration with the adjusted (lower) qp and set the state back to 303 // the configuration with the adjusted (lower) qp and set the state back to
288 // normal. 304 // normal.
289 unsigned int adjusted_max_qp; 305 unsigned int adjusted_max_qp;
290 if (layers_[active_layer_].state == TemporalLayer::State::kQualityBoost && 306 if (layers_[active_layer_].state == TemporalLayer::State::kQualityBoost &&
291 layers_[active_layer_].enhanced_max_qp != -1) { 307 layers_[active_layer_].enhanced_max_qp != -1) {
292 adjusted_max_qp = layers_[active_layer_].enhanced_max_qp; 308 adjusted_max_qp = layers_[active_layer_].enhanced_max_qp;
293 layers_[active_layer_].state = TemporalLayer::State::kNormal; 309 layers_[active_layer_].state = TemporalLayer::State::kNormal;
294 } else { 310 } else {
295 if (max_qp_ == -1) 311 if (max_qp_ == -1)
296 return false; 312 return cfg_updated;
297 adjusted_max_qp = max_qp_; // Set the normal max qp. 313 adjusted_max_qp = max_qp_; // Set the normal max qp.
298 } 314 }
299 315
300 if (adjusted_max_qp == cfg->rc_max_quantizer) 316 if (adjusted_max_qp == cfg->rc_max_quantizer)
301 return false; 317 return cfg_updated;
302 318
303 cfg->rc_max_quantizer = adjusted_max_qp; 319 cfg->rc_max_quantizer = adjusted_max_qp;
304 return true; 320 cfg_updated = true;
321 return cfg_updated;
305 } 322 }
306 323
307 void ScreenshareLayers::TemporalLayer::UpdateDebt(int64_t delta_ms) { 324 void ScreenshareLayers::TemporalLayer::UpdateDebt(int64_t delta_ms) {
308 uint32_t debt_reduction_bytes = target_rate_kbps_ * delta_ms / 8; 325 uint32_t debt_reduction_bytes = target_rate_kbps_ * delta_ms / 8;
309 if (debt_reduction_bytes >= debt_bytes_) { 326 if (debt_reduction_bytes >= debt_bytes_) {
310 debt_bytes_ = 0; 327 debt_bytes_ = 0;
311 } else { 328 } else {
312 debt_bytes_ -= debt_reduction_bytes; 329 debt_bytes_ -= debt_reduction_bytes;
313 } 330 }
314 } 331 }
(...skipping 30 matching lines...) Expand all
345 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.Screenshare.Layer1.Qp", 362 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.Screenshare.Layer1.Qp",
346 stats_.tl1_qp_sum_ / stats_.num_tl1_frames_); 363 stats_.tl1_qp_sum_ / stats_.num_tl1_frames_);
347 RTC_HISTOGRAM_COUNTS_10000( 364 RTC_HISTOGRAM_COUNTS_10000(
348 "WebRTC.Video.Screenshare.Layer1.TargetBitrate", 365 "WebRTC.Video.Screenshare.Layer1.TargetBitrate",
349 stats_.tl1_target_bitrate_sum_ / stats_.num_tl1_frames_); 366 stats_.tl1_target_bitrate_sum_ / stats_.num_tl1_frames_);
350 } 367 }
351 } 368 }
352 } 369 }
353 370
354 } // namespace webrtc 371 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698