1 /*
2 * Copyright (c) 2019 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 "video/encoder_overshoot_detector.h"
12
13 #include <algorithm>
14
15 namespace webrtc {
16 namespace {
17 // The buffer level for media-rate utilization is allowed to go below zero,
18 // down to
19 // -(|kMaxMediaUnderrunFrames| / |target_framerate_fps_|) * |target_bitrate_|.
20 static constexpr double kMaxMediaUnderrunFrames = 5.0;
21 } // namespace
22
EncoderOvershootDetector(int64_t window_size_ms)23 EncoderOvershootDetector::EncoderOvershootDetector(int64_t window_size_ms)
24 : window_size_ms_(window_size_ms),
25 time_last_update_ms_(-1),
26 sum_network_utilization_factors_(0.0),
27 sum_media_utilization_factors_(0.0),
28 target_bitrate_(DataRate::Zero()),
29 target_framerate_fps_(0),
30 network_buffer_level_bits_(0),
31 media_buffer_level_bits_(0) {}
32
33 EncoderOvershootDetector::~EncoderOvershootDetector() = default;
34
SetTargetRate(DataRate target_bitrate,double target_framerate_fps,int64_t time_ms)35 void EncoderOvershootDetector::SetTargetRate(DataRate target_bitrate,
36 double target_framerate_fps,
37 int64_t time_ms) {
38 // First leak bits according to the previous target rate.
39 if (target_bitrate_ != DataRate::Zero()) {
40 LeakBits(time_ms);
41 } else if (target_bitrate != DataRate::Zero()) {
42 // Stream was just enabled, reset state.
43 time_last_update_ms_ = time_ms;
44 utilization_factors_.clear();
45 sum_network_utilization_factors_ = 0.0;
46 sum_media_utilization_factors_ = 0.0;
47 network_buffer_level_bits_ = 0;
48 media_buffer_level_bits_ = 0;
49 }
50
51 target_bitrate_ = target_bitrate;
52 target_framerate_fps_ = target_framerate_fps;
53 }
54
OnEncodedFrame(size_t bytes,int64_t time_ms)55 void EncoderOvershootDetector::OnEncodedFrame(size_t bytes, int64_t time_ms) {
56 // Leak bits from the virtual pacer buffer, according to the current target
57 // bitrate.
58 LeakBits(time_ms);
59
60 // Ideal size of a frame given the current rates.
61 const int64_t ideal_frame_size_bits = IdealFrameSizeBits();
62 if (ideal_frame_size_bits == 0) {
63 // Frame without updated bitrate and/or framerate, ignore it.
64 return;
65 }
66
67 const double network_utilization_factor = HandleEncodedFrame(
68 bytes * 8, ideal_frame_size_bits, time_ms, &network_buffer_level_bits_);
69 const double media_utilization_factor = HandleEncodedFrame(
70 bytes * 8, ideal_frame_size_bits, time_ms, &media_buffer_level_bits_);
71
72 sum_network_utilization_factors_ += network_utilization_factor;
73 sum_media_utilization_factors_ += media_utilization_factor;
74
75 utilization_factors_.emplace_back(network_utilization_factor,
76 media_utilization_factor, time_ms);
77 }
78
HandleEncodedFrame(size_t frame_size_bits,int64_t ideal_frame_size_bits,int64_t time_ms,int64_t * buffer_level_bits) const79 double EncoderOvershootDetector::HandleEncodedFrame(
80 size_t frame_size_bits,
81 int64_t ideal_frame_size_bits,
82 int64_t time_ms,
83 int64_t* buffer_level_bits) const {
84 // Add new frame to the buffer level. If doing so exceeds the ideal buffer
85 // size, penalize this frame but cap overshoot to current buffer level rather
86 // than size of this frame. This is done so that a single large frame is not
87 // penalized if the encoder afterwards compensates by dropping frames and/or
88 // reducing frame size. If however a large frame is followed by more data,
89 // we cannot pace that next frame out within one frame space.
90 const int64_t bitsum = frame_size_bits + *buffer_level_bits;
91 int64_t overshoot_bits = 0;
92 if (bitsum > ideal_frame_size_bits) {
93 overshoot_bits =
94 std::min(*buffer_level_bits, bitsum - ideal_frame_size_bits);
95 }
96
97 // Add entry for the (over) utilization for this frame. Factor is capped
98 // at 1.0 so that we don't risk overshooting on sudden changes.
99 double utilization_factor;
100 if (utilization_factors_.empty()) {
101 // First frame, cannot estimate overshoot based on previous one so
102 // for this particular frame, just like as size vs optimal size.
103 utilization_factor = std::max(
104 1.0, static_cast<double>(frame_size_bits) / ideal_frame_size_bits);
105 } else {
106 utilization_factor =
107 1.0 + (static_cast<double>(overshoot_bits) / ideal_frame_size_bits);
108 }
109
110 // Remove the overshot bits from the virtual buffer so we don't penalize
111 // those bits multiple times.
112 *buffer_level_bits -= overshoot_bits;
113 *buffer_level_bits += frame_size_bits;
114
115 return utilization_factor;
116 }
117
118 absl::optional<double>
GetNetworkRateUtilizationFactor(int64_t time_ms)119 EncoderOvershootDetector::GetNetworkRateUtilizationFactor(int64_t time_ms) {
120 CullOldUpdates(time_ms);
121
122 // No data points within window, return.
123 if (utilization_factors_.empty()) {
124 return absl::nullopt;
125 }
126
127 // TODO(sprang): Consider changing from arithmetic mean to some other
128 // function such as 90th percentile.
129 return sum_network_utilization_factors_ / utilization_factors_.size();
130 }
131
GetMediaRateUtilizationFactor(int64_t time_ms)132 absl::optional<double> EncoderOvershootDetector::GetMediaRateUtilizationFactor(
133 int64_t time_ms) {
134 CullOldUpdates(time_ms);
135
136 // No data points within window, return.
137 if (utilization_factors_.empty()) {
138 return absl::nullopt;
139 }
140
141 return sum_media_utilization_factors_ / utilization_factors_.size();
142 }
143
Reset()144 void EncoderOvershootDetector::Reset() {
145 time_last_update_ms_ = -1;
146 utilization_factors_.clear();
147 target_bitrate_ = DataRate::Zero();
148 sum_network_utilization_factors_ = 0.0;
149 sum_media_utilization_factors_ = 0.0;
150 target_framerate_fps_ = 0.0;
151 network_buffer_level_bits_ = 0;
152 media_buffer_level_bits_ = 0;
153 }
154
IdealFrameSizeBits() const155 int64_t EncoderOvershootDetector::IdealFrameSizeBits() const {
156 if (target_framerate_fps_ <= 0 || target_bitrate_ == DataRate::Zero()) {
157 return 0;
158 }
159
160 // Current ideal frame size, based on the current target bitrate.
161 return static_cast<int64_t>(
162 (target_bitrate_.bps() + target_framerate_fps_ / 2) /
163 target_framerate_fps_);
164 }
165
LeakBits(int64_t time_ms)166 void EncoderOvershootDetector::LeakBits(int64_t time_ms) {
167 if (time_last_update_ms_ != -1 && target_bitrate_ > DataRate::Zero()) {
168 int64_t time_delta_ms = time_ms - time_last_update_ms_;
169 // Leak bits according to the current target bitrate.
170 const int64_t leaked_bits = (target_bitrate_.bps() * time_delta_ms) / 1000;
171
172 // Network buffer may not go below zero.
173 network_buffer_level_bits_ =
174 std::max<int64_t>(0, network_buffer_level_bits_ - leaked_bits);
175
176 // Media buffer my go down to minus |kMaxMediaUnderrunFrames| frames worth
177 // of data.
178 const double max_underrun_seconds =
179 std::min(kMaxMediaUnderrunFrames, target_framerate_fps_) /
180 target_framerate_fps_;
181 media_buffer_level_bits_ = std::max<int64_t>(
182 -max_underrun_seconds * target_bitrate_.bps<int64_t>(),
183 media_buffer_level_bits_ - leaked_bits);
184 }
185 time_last_update_ms_ = time_ms;
186 }
187
CullOldUpdates(int64_t time_ms)188 void EncoderOvershootDetector::CullOldUpdates(int64_t time_ms) {
189 // Cull old data points.
190 const int64_t cutoff_time_ms = time_ms - window_size_ms_;
191 while (!utilization_factors_.empty() &&
192 utilization_factors_.front().update_time_ms < cutoff_time_ms) {
193 // Make sure sum is never allowed to become negative due rounding errors.
194 sum_network_utilization_factors_ = std::max(
195 0.0, sum_network_utilization_factors_ -
196 utilization_factors_.front().network_utilization_factor);
197 sum_media_utilization_factors_ = std::max(
198 0.0, sum_media_utilization_factors_ -
199 utilization_factors_.front().media_utilization_factor);
200 utilization_factors_.pop_front();
201 }
202 }
203
204 } // namespace webrtc
205