blob: 0b9926e18c89b09d0002be1763fb0a1c26561d19 [file] [log] [blame]
Erik Språng7ca375c2019-02-06 16:20:17 +01001/*
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
15namespace webrtc {
Erik Språng6c072ef2019-04-01 12:57:28 +020016namespace {
17// The buffer level for media-rate utilization is allowed to go below zero,
18// down to
19// -(|kMaxMediaUnderrunFrames| / |target_framerate_fps_|) * |target_bitrate_|.
20static constexpr double kMaxMediaUnderrunFrames = 5.0;
21} // namespace
Erik Språng7ca375c2019-02-06 16:20:17 +010022
23EncoderOvershootDetector::EncoderOvershootDetector(int64_t window_size_ms)
24 : window_size_ms_(window_size_ms),
25 time_last_update_ms_(-1),
Erik Språng6c072ef2019-04-01 12:57:28 +020026 sum_network_utilization_factors_(0.0),
27 sum_media_utilization_factors_(0.0),
Erik Språng7ca375c2019-02-06 16:20:17 +010028 target_bitrate_(DataRate::Zero()),
29 target_framerate_fps_(0),
Erik Språng6c072ef2019-04-01 12:57:28 +020030 network_buffer_level_bits_(0),
31 media_buffer_level_bits_(0) {}
Erik Språng7ca375c2019-02-06 16:20:17 +010032
33EncoderOvershootDetector::~EncoderOvershootDetector() = default;
34
35void 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();
Erik Språng6c072ef2019-04-01 12:57:28 +020045 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;
Erik Språng7ca375c2019-02-06 16:20:17 +010049 }
50
51 target_bitrate_ = target_bitrate;
52 target_framerate_fps_ = target_framerate_fps;
53}
54
55void 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.
Erik Språng6c072ef2019-04-01 12:57:28 +020061 const int64_t ideal_frame_size_bits = IdealFrameSizeBits();
62 if (ideal_frame_size_bits == 0) {
Erik Språng7ca375c2019-02-06 16:20:17 +010063 // Frame without updated bitrate and/or framerate, ignore it.
64 return;
65 }
66
Erik Språng6c072ef2019-04-01 12:57:28 +020067 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
79double 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 {
Erik Språng7ca375c2019-02-06 16:20:17 +010084 // 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.
Erik Språng6c072ef2019-04-01 12:57:28 +020090 const int64_t bitsum = frame_size_bits + *buffer_level_bits;
Erik Språng7ca375c2019-02-06 16:20:17 +010091 int64_t overshoot_bits = 0;
Erik Språng6c072ef2019-04-01 12:57:28 +020092 if (bitsum > ideal_frame_size_bits) {
93 overshoot_bits =
94 std::min(*buffer_level_bits, bitsum - ideal_frame_size_bits);
Erik Språng7ca375c2019-02-06 16:20:17 +010095 }
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.
Erik Språng6c072ef2019-04-01 12:57:28 +020099 double utilization_factor;
Erik Språng7ca375c2019-02-06 16:20:17 +0100100 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.
Erik Språng6c072ef2019-04-01 12:57:28 +0200103 utilization_factor = std::max(
104 1.0, static_cast<double>(frame_size_bits) / ideal_frame_size_bits);
Erik Språng7ca375c2019-02-06 16:20:17 +0100105 } else {
Erik Språng6c072ef2019-04-01 12:57:28 +0200106 utilization_factor =
107 1.0 + (static_cast<double>(overshoot_bits) / ideal_frame_size_bits);
Erik Språng7ca375c2019-02-06 16:20:17 +0100108 }
Erik Språng7ca375c2019-02-06 16:20:17 +0100109
110 // Remove the overshot bits from the virtual buffer so we don't penalize
111 // those bits multiple times.
Erik Språng6c072ef2019-04-01 12:57:28 +0200112 *buffer_level_bits -= overshoot_bits;
113 *buffer_level_bits += frame_size_bits;
114
115 return utilization_factor;
Erik Språng7ca375c2019-02-06 16:20:17 +0100116}
117
Erik Språng6c072ef2019-04-01 12:57:28 +0200118absl::optional<double>
119EncoderOvershootDetector::GetNetworkRateUtilizationFactor(int64_t time_ms) {
120 CullOldUpdates(time_ms);
Erik Språng7ca375c2019-02-06 16:20:17 +0100121
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.
Erik Språng6c072ef2019-04-01 12:57:28 +0200129 return sum_network_utilization_factors_ / utilization_factors_.size();
130}
131
132absl::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();
Erik Språng7ca375c2019-02-06 16:20:17 +0100142}
143
144void EncoderOvershootDetector::Reset() {
145 time_last_update_ms_ = -1;
146 utilization_factors_.clear();
147 target_bitrate_ = DataRate::Zero();
Erik Språng6c072ef2019-04-01 12:57:28 +0200148 sum_network_utilization_factors_ = 0.0;
149 sum_media_utilization_factors_ = 0.0;
Erik Språng7ca375c2019-02-06 16:20:17 +0100150 target_framerate_fps_ = 0.0;
Erik Språng6c072ef2019-04-01 12:57:28 +0200151 network_buffer_level_bits_ = 0;
152 media_buffer_level_bits_ = 0;
Erik Språng7ca375c2019-02-06 16:20:17 +0100153}
154
155int64_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
166void 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.
Erik Språng6c072ef2019-04-01 12:57:28 +0200170 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);
Erik Språng7ca375c2019-02-06 16:20:17 +0100184 }
185 time_last_update_ms_ = time_ms;
186}
187
Erik Språng6c072ef2019-04-01 12:57:28 +0200188void 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
Erik Språng7ca375c2019-02-06 16:20:17 +0100204} // namespace webrtc