blob: f1fa3bf8f35b109a26867755678620edf5966a6f [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 {
16
17EncoderOvershootDetector::EncoderOvershootDetector(int64_t window_size_ms)
18 : window_size_ms_(window_size_ms),
19 time_last_update_ms_(-1),
20 sum_utilization_factors_(0.0),
21 target_bitrate_(DataRate::Zero()),
22 target_framerate_fps_(0),
23 buffer_level_bits_(0) {}
24
25EncoderOvershootDetector::~EncoderOvershootDetector() = default;
26
27void EncoderOvershootDetector::SetTargetRate(DataRate target_bitrate,
28 double target_framerate_fps,
29 int64_t time_ms) {
30 // First leak bits according to the previous target rate.
31 if (target_bitrate_ != DataRate::Zero()) {
32 LeakBits(time_ms);
33 } else if (target_bitrate != DataRate::Zero()) {
34 // Stream was just enabled, reset state.
35 time_last_update_ms_ = time_ms;
36 utilization_factors_.clear();
37 sum_utilization_factors_ = 0.0;
38 buffer_level_bits_ = 0;
39 }
40
41 target_bitrate_ = target_bitrate;
42 target_framerate_fps_ = target_framerate_fps;
43}
44
45void EncoderOvershootDetector::OnEncodedFrame(size_t bytes, int64_t time_ms) {
46 // Leak bits from the virtual pacer buffer, according to the current target
47 // bitrate.
48 LeakBits(time_ms);
49
50 // Ideal size of a frame given the current rates.
51 const int64_t ideal_frame_size = IdealFrameSizeBits();
52 if (ideal_frame_size == 0) {
53 // Frame without updated bitrate and/or framerate, ignore it.
54 return;
55 }
56
57 // Add new frame to the buffer level. If doing so exceeds the ideal buffer
58 // size, penalize this frame but cap overshoot to current buffer level rather
59 // than size of this frame. This is done so that a single large frame is not
60 // penalized if the encoder afterwards compensates by dropping frames and/or
61 // reducing frame size. If however a large frame is followed by more data,
62 // we cannot pace that next frame out within one frame space.
63 const int64_t bitsum = (bytes * 8) + buffer_level_bits_;
64 int64_t overshoot_bits = 0;
65 if (bitsum > ideal_frame_size) {
66 overshoot_bits = std::min(buffer_level_bits_, bitsum - ideal_frame_size);
67 }
68
69 // Add entry for the (over) utilization for this frame. Factor is capped
70 // at 1.0 so that we don't risk overshooting on sudden changes.
71 double frame_utilization_factor;
72 if (utilization_factors_.empty()) {
73 // First frame, cannot estimate overshoot based on previous one so
74 // for this particular frame, just like as size vs optimal size.
75 frame_utilization_factor =
76 std::max(1.0, static_cast<double>(bytes) * 8 / ideal_frame_size);
77 } else {
78 frame_utilization_factor =
79 1.0 + (static_cast<double>(overshoot_bits) / ideal_frame_size);
80 }
81 utilization_factors_.emplace_back(frame_utilization_factor, time_ms);
82 sum_utilization_factors_ += frame_utilization_factor;
83
84 // Remove the overshot bits from the virtual buffer so we don't penalize
85 // those bits multiple times.
86 buffer_level_bits_ -= overshoot_bits;
87 buffer_level_bits_ += bytes * 8;
88}
89
90absl::optional<double> EncoderOvershootDetector::GetUtilizationFactor(
91 int64_t time_ms) {
92 // Cull old data points.
93 const int64_t cutoff_time_ms = time_ms - window_size_ms_;
94 while (!utilization_factors_.empty() &&
95 utilization_factors_.front().update_time_ms < cutoff_time_ms) {
96 // Make sure sum is never allowed to become negative due rounding errors.
97 sum_utilization_factors_ =
98 std::max(0.0, sum_utilization_factors_ -
99 utilization_factors_.front().utilization_factor);
100 utilization_factors_.pop_front();
101 }
102
103 // No data points within window, return.
104 if (utilization_factors_.empty()) {
105 return absl::nullopt;
106 }
107
108 // TODO(sprang): Consider changing from arithmetic mean to some other
109 // function such as 90th percentile.
110 return sum_utilization_factors_ / utilization_factors_.size();
111}
112
113void EncoderOvershootDetector::Reset() {
114 time_last_update_ms_ = -1;
115 utilization_factors_.clear();
116 target_bitrate_ = DataRate::Zero();
117 sum_utilization_factors_ = 0.0;
118 target_framerate_fps_ = 0.0;
119 buffer_level_bits_ = 0;
120}
121
122int64_t EncoderOvershootDetector::IdealFrameSizeBits() const {
123 if (target_framerate_fps_ <= 0 || target_bitrate_ == DataRate::Zero()) {
124 return 0;
125 }
126
127 // Current ideal frame size, based on the current target bitrate.
128 return static_cast<int64_t>(
129 (target_bitrate_.bps() + target_framerate_fps_ / 2) /
130 target_framerate_fps_);
131}
132
133void EncoderOvershootDetector::LeakBits(int64_t time_ms) {
134 if (time_last_update_ms_ != -1 && target_bitrate_ > DataRate::Zero()) {
135 int64_t time_delta_ms = time_ms - time_last_update_ms_;
136 // Leak bits according to the current target bitrate.
137 int64_t leaked_bits = std::min(
138 buffer_level_bits_, (target_bitrate_.bps() * time_delta_ms) / 1000);
139 buffer_level_bits_ -= leaked_bits;
140 }
141 time_last_update_ms_ = time_ms;
142}
143
144} // namespace webrtc