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

Side by Side Diff: webrtc/modules/video_coding/bitrate_adjuster.cc

Issue 1660963002: Bitrate controller for VideoToolbox encoder. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Fix win compile errors Created 4 years, 10 months 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
(Empty)
1 /*
2 * Copyright 2016 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 */
10
11 #include "webrtc/modules/video_coding/include/bitrate_adjuster.h"
12
13 #include <cmath>
14
15 #include "webrtc/base/checks.h"
16 #include "webrtc/base/logging.h"
17 #include "webrtc/system_wrappers/include/clock.h"
18
19 namespace webrtc {
20
21 // Update bitrate at most once every second.
22 const uint32_t BitrateAdjuster::kBitrateUpdateIntervalMs = 1000;
23
24 // Update bitrate at most once every 30 frames.
25 const uint32_t BitrateAdjuster::kBitrateUpdateFrameInterval = 30;
26
27 // 10 percent of original.
28 const float BitrateAdjuster::kBitrateTolerancePct = .1f;
29
30 const float BitrateAdjuster::kBytesPerMsToBitsPerSecond = 8 * 1000;
31
32 BitrateAdjuster::BitrateAdjuster(Clock* clock,
33 float min_adjusted_bitrate_pct,
34 float max_adjusted_bitrate_pct)
35 : clock_(clock),
36 min_adjusted_bitrate_pct_(min_adjusted_bitrate_pct),
37 max_adjusted_bitrate_pct_(max_adjusted_bitrate_pct),
38 bitrate_tracker_(1.5 * kBitrateUpdateIntervalMs,
39 kBytesPerMsToBitsPerSecond) {
40 Reset();
41 }
42
43 void BitrateAdjuster::SetTargetBitrateBps(uint32_t bitrate_bps) {
44 rtc::CritScope cs(&crit_);
45 // If the change in target bitrate is large, update the adjusted bitrate
46 // immediately since it's likely we have gained or lost a sizeable amount of
47 // bandwidth and we'll want to respond quickly.
48 // If the change in target bitrate fits within the existing tolerance of
49 // encoder output, wait for the next adjustment time to preserve
50 // existing penalties and not forcibly reset the adjusted bitrate to target.
51 // However, if we received many small deltas within an update time
52 // window and one of them exceeds the tolerance when compared to the last
53 // target we updated against, treat it as a large change in target bitrate.
54 if (!IsWithinTolerance(bitrate_bps, target_bitrate_bps_) ||
55 !IsWithinTolerance(bitrate_bps, last_adjusted_target_bitrate_bps_)) {
56 adjusted_bitrate_bps_ = bitrate_bps;
57 last_adjusted_target_bitrate_bps_ = bitrate_bps;
58 }
59 target_bitrate_bps_ = bitrate_bps;
60 }
61
62 uint32_t BitrateAdjuster::GetTargetBitrateBps() const {
63 rtc::CritScope cs(&crit_);
64 return target_bitrate_bps_;
65 }
66
67 uint32_t BitrateAdjuster::GetAdjustedBitrateBps() const {
68 rtc::CritScope cs(&crit_);
69 return adjusted_bitrate_bps_;
70 }
71
72 uint32_t BitrateAdjuster::GetEstimatedBitrateBps() {
73 rtc::CritScope cs(&crit_);
74 return bitrate_tracker_.Rate(clock_->TimeInMilliseconds());
75 }
76
77 void BitrateAdjuster::Update(size_t frame_size) {
78 rtc::CritScope cs(&crit_);
79 uint32_t current_time_ms = clock_->TimeInMilliseconds();
80 bitrate_tracker_.Update(frame_size, current_time_ms);
81 UpdateBitrate(current_time_ms);
82 }
83
84 bool BitrateAdjuster::IsWithinTolerance(uint32_t bitrate_bps,
85 uint32_t target_bitrate_bps) {
86 if (target_bitrate_bps == 0) {
87 return false;
88 }
89 float delta = std::abs(static_cast<float>(bitrate_bps) -
90 static_cast<float>(target_bitrate_bps));
91 float delta_pct = delta / target_bitrate_bps;
92 return delta_pct < kBitrateTolerancePct;
93 }
94
95 uint32_t BitrateAdjuster::GetMinAdjustedBitrateBps() const {
96 return min_adjusted_bitrate_pct_ * target_bitrate_bps_;
97 }
98
99 uint32_t BitrateAdjuster::GetMaxAdjustedBitrateBps() const {
100 return max_adjusted_bitrate_pct_ * target_bitrate_bps_;
101 }
102
103 // Only safe to call this after Update calls have stopped
104 void BitrateAdjuster::Reset() {
105 rtc::CritScope cs(&crit_);
106 target_bitrate_bps_ = 0;
107 adjusted_bitrate_bps_ = 0;
108 last_adjusted_target_bitrate_bps_ = 0;
109 last_bitrate_update_time_ms_ = 0;
110 frames_since_last_update_ = 0;
111 bitrate_tracker_.Reset();
112 }
113
114 void BitrateAdjuster::UpdateBitrate(uint32_t current_time_ms) {
115 uint32_t time_since_last_update_ms =
116 current_time_ms - last_bitrate_update_time_ms_;
117 // Don't attempt to update bitrate unless enough time and frames have passed.
118 ++frames_since_last_update_;
119 if (time_since_last_update_ms < kBitrateUpdateIntervalMs ||
120 frames_since_last_update_ < kBitrateUpdateFrameInterval) {
121 return;
122 }
123 float estimated_bitrate_bps = bitrate_tracker_.Rate(current_time_ms);
124 float target_bitrate_bps = target_bitrate_bps_;
125 float error = target_bitrate_bps - estimated_bitrate_bps;
126
127 // Adjust if we've overshot by any amount or if we've undershot too much.
128 if (estimated_bitrate_bps > target_bitrate_bps ||
129 error > kBitrateTolerancePct * target_bitrate_bps) {
130 // Adjust the bitrate by a fraction of the error.
131 float adjustment = .5 * error;
132 float adjusted_bitrate_bps = target_bitrate_bps + adjustment;
133
134 // Clamp the adjustment.
135 float min_bitrate_bps = GetMinAdjustedBitrateBps();
136 float max_bitrate_bps = GetMaxAdjustedBitrateBps();
137 adjusted_bitrate_bps = std::max(adjusted_bitrate_bps, min_bitrate_bps);
138 adjusted_bitrate_bps = std::min(adjusted_bitrate_bps, max_bitrate_bps);
139
140 // Set the adjustment if it's not already set.
141 float last_adjusted_bitrate_bps = adjusted_bitrate_bps_;
142 if (adjusted_bitrate_bps != last_adjusted_bitrate_bps) {
143 LOG(LS_VERBOSE) << "Adjusting encoder bitrate:"
144 << "\n target_bitrate:"
145 << static_cast<uint32_t>(target_bitrate_bps)
146 << "\n estimated_bitrate:"
147 << static_cast<uint32_t>(estimated_bitrate_bps)
148 << "\n last_adjusted_bitrate:"
149 << static_cast<uint32_t>(last_adjusted_bitrate_bps)
150 << "\n adjusted_bitrate:"
151 << static_cast<uint32_t>(adjusted_bitrate_bps);
152 adjusted_bitrate_bps_ = adjusted_bitrate_bps;
153 }
154 }
155 last_bitrate_update_time_ms_ = current_time_ms;
156 frames_since_last_update_ = 0;
157 last_adjusted_target_bitrate_bps_ = target_bitrate_bps_;
158 }
159
160 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/modules/video_coding/BUILD.gn ('k') | webrtc/modules/video_coding/bitrate_adjuster_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698