blob: 918f5da026fe04e9439274cb761ae27bc49fb187 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
2 * Copyright (c) 2011 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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/video_coding/utility/frame_dropper.h"
stefan@webrtc.orgeb917922013-02-18 14:40:18 +000012
isheriff7620be82016-03-06 23:22:33 -080013#include <algorithm>
14
philipel5908c712015-12-21 08:23:20 -080015namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000016
isheriff7620be82016-03-06 23:22:33 -080017namespace {
18
19const float kDefaultFrameSizeAlpha = 0.9f;
20const float kDefaultKeyFrameRatioAlpha = 0.99f;
21// 1 key frame every 10th second in 30 fps.
22const float kDefaultKeyFrameRatioValue = 1 / 300.0f;
23
stefan@webrtc.org84cd8e32013-03-07 13:12:32 +000024const float kDefaultDropRatioAlpha = 0.9f;
isheriff7620be82016-03-06 23:22:33 -080025const float kDefaultDropRatioValue = 0.96f;
26// Maximum duration over which frames are continuously dropped.
27const float kDefaultMaxDropDurationSecs = 4.0f;
28
29// Default target bitrate.
30// TODO(isheriff): Should this be higher to avoid dropping too many packets when
31// the bandwidth is unknown at the start ?
32const float kDefaultTargetBitrateKbps = 300.0f;
33const float kDefaultIncomingFrameRate = 30;
34const float kLeakyBucketSizeSeconds = 0.5f;
35
36// A delta frame that is bigger than |kLargeDeltaFactor| times the average
37// delta frame is a large frame that is spread out for accumulation.
38const int kLargeDeltaFactor = 3;
39
40// Cap on the frame size accumulator to prevent excessive drops.
41const float kAccumulatorCapBufferSizeSecs = 3.0f;
42} // namespace
stefan@webrtc.org84cd8e32013-03-07 13:12:32 +000043
stefan@webrtc.orgeb917922013-02-18 14:40:18 +000044FrameDropper::FrameDropper()
isheriff7620be82016-03-06 23:22:33 -080045 : key_frame_ratio_(kDefaultKeyFrameRatioAlpha),
46 delta_frame_size_avg_kbits_(kDefaultFrameSizeAlpha),
47 drop_ratio_(kDefaultDropRatioAlpha, kDefaultDropRatioValue),
48 enabled_(true),
49 max_drop_duration_secs_(kDefaultMaxDropDurationSecs) {
philipel5908c712015-12-21 08:23:20 -080050 Reset();
stefan@webrtc.org84cd8e32013-03-07 13:12:32 +000051}
52
philipel5908c712015-12-21 08:23:20 -080053void FrameDropper::Reset() {
isheriff7620be82016-03-06 23:22:33 -080054 key_frame_ratio_.Reset(kDefaultKeyFrameRatioAlpha);
55 key_frame_ratio_.Apply(1.0f, kDefaultKeyFrameRatioValue);
56 delta_frame_size_avg_kbits_.Reset(kDefaultFrameSizeAlpha);
57
58 accumulator_ = 0.0f;
59 accumulator_max_ = kDefaultTargetBitrateKbps / 2;
60 target_bitrate_ = kDefaultTargetBitrateKbps;
61 incoming_frame_rate_ = kDefaultIncomingFrameRate;
62
63 large_frame_accumulation_count_ = 0;
isheriff4d7bc242016-04-21 16:37:20 -070064 large_frame_accumulation_chunk_size_ = 0;
isheriff7620be82016-03-06 23:22:33 -080065 large_frame_accumulation_spread_ = 0.5 * kDefaultIncomingFrameRate;
66
67 drop_next_ = false;
68 drop_ratio_.Reset(0.9f);
69 drop_ratio_.Apply(0.0f, 0.0f);
70 drop_count_ = 0;
71 was_below_max_ = true;
philipel5908c712015-12-21 08:23:20 -080072}
73
74void FrameDropper::Enable(bool enable) {
isheriff7620be82016-03-06 23:22:33 -080075 enabled_ = enable;
philipel5908c712015-12-21 08:23:20 -080076}
77
isheriff7620be82016-03-06 23:22:33 -080078void FrameDropper::Fill(size_t framesize_bytes, bool delta_frame) {
79 if (!enabled_) {
philipel5908c712015-12-21 08:23:20 -080080 return;
81 }
isheriff7620be82016-03-06 23:22:33 -080082 float framesize_kbits = 8.0f * static_cast<float>(framesize_bytes) / 1000.0f;
83 if (!delta_frame) {
84 key_frame_ratio_.Apply(1.0, 1.0);
85 // Do not spread if we are already doing it (or we risk dropping bits that
Åsa Perssonb52a4d92017-11-08 11:33:37 +010086 // need accumulation). Given we compute the key frame ratio and spread
87 // based on that, this should not normally happen.
isheriff7620be82016-03-06 23:22:33 -080088 if (large_frame_accumulation_count_ == 0) {
89 if (key_frame_ratio_.filtered() > 1e-5 &&
90 1 / key_frame_ratio_.filtered() < large_frame_accumulation_spread_) {
91 large_frame_accumulation_count_ =
92 static_cast<int32_t>(1 / key_frame_ratio_.filtered() + 0.5);
93 } else {
94 large_frame_accumulation_count_ =
95 static_cast<int32_t>(large_frame_accumulation_spread_ + 0.5);
96 }
97 large_frame_accumulation_chunk_size_ =
98 framesize_kbits / large_frame_accumulation_count_;
99 framesize_kbits = 0;
philipel5908c712015-12-21 08:23:20 -0800100 }
101 } else {
isheriff7620be82016-03-06 23:22:33 -0800102 // Identify if it is an unusually large delta frame and spread accumulation
103 // if that is the case.
104 if (delta_frame_size_avg_kbits_.filtered() != -1 &&
105 (framesize_kbits >
106 kLargeDeltaFactor * delta_frame_size_avg_kbits_.filtered()) &&
107 large_frame_accumulation_count_ == 0) {
108 large_frame_accumulation_count_ =
109 static_cast<int32_t>(large_frame_accumulation_spread_ + 0.5);
110 large_frame_accumulation_chunk_size_ =
111 framesize_kbits / large_frame_accumulation_count_;
112 framesize_kbits = 0;
113 } else {
114 delta_frame_size_avg_kbits_.Apply(1, framesize_kbits);
115 }
116 key_frame_ratio_.Apply(1.0, 0.0);
philipel5908c712015-12-21 08:23:20 -0800117 }
118 // Change the level of the accumulator (bucket)
isheriff7620be82016-03-06 23:22:33 -0800119 accumulator_ += framesize_kbits;
philipel5908c712015-12-21 08:23:20 -0800120 CapAccumulator();
121}
122
isheriff7620be82016-03-06 23:22:33 -0800123void FrameDropper::Leak(uint32_t input_framerate) {
124 if (!enabled_) {
philipel5908c712015-12-21 08:23:20 -0800125 return;
126 }
isheriff7620be82016-03-06 23:22:33 -0800127 if (input_framerate < 1) {
philipel5908c712015-12-21 08:23:20 -0800128 return;
129 }
isheriff7620be82016-03-06 23:22:33 -0800130 if (target_bitrate_ < 0.0f) {
philipel5908c712015-12-21 08:23:20 -0800131 return;
132 }
isheriff7620be82016-03-06 23:22:33 -0800133 // Add lower bound for large frame accumulation spread.
134 large_frame_accumulation_spread_ = std::max(0.5 * input_framerate, 5.0);
135 // Expected bits per frame based on current input frame rate.
136 float expected_bits_per_frame = target_bitrate_ / input_framerate;
137 if (large_frame_accumulation_count_ > 0) {
138 expected_bits_per_frame -= large_frame_accumulation_chunk_size_;
139 --large_frame_accumulation_count_;
philipel5908c712015-12-21 08:23:20 -0800140 }
isheriff7620be82016-03-06 23:22:33 -0800141 accumulator_ -= expected_bits_per_frame;
142 if (accumulator_ < 0.0f) {
143 accumulator_ = 0.0f;
philipel5908c712015-12-21 08:23:20 -0800144 }
145 UpdateRatio();
niklase@google.com470e71d2011-07-07 08:21:25 +0000146}
147
philipel5908c712015-12-21 08:23:20 -0800148void FrameDropper::UpdateRatio() {
isheriff7620be82016-03-06 23:22:33 -0800149 if (accumulator_ > 1.3f * accumulator_max_) {
Åsa Perssonb52a4d92017-11-08 11:33:37 +0100150 // Too far above accumulator max, react faster.
isheriff7620be82016-03-06 23:22:33 -0800151 drop_ratio_.UpdateBase(0.8f);
philipel5908c712015-12-21 08:23:20 -0800152 } else {
Åsa Perssonb52a4d92017-11-08 11:33:37 +0100153 // Go back to normal reaction.
isheriff7620be82016-03-06 23:22:33 -0800154 drop_ratio_.UpdateBase(0.9f);
philipel5908c712015-12-21 08:23:20 -0800155 }
isheriff7620be82016-03-06 23:22:33 -0800156 if (accumulator_ > accumulator_max_) {
Åsa Perssonb52a4d92017-11-08 11:33:37 +0100157 // We are above accumulator max, and should ideally drop a frame. Increase
158 // the drop_ratio_ and drop the frame later.
isheriff7620be82016-03-06 23:22:33 -0800159 if (was_below_max_) {
160 drop_next_ = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000161 }
isheriff7620be82016-03-06 23:22:33 -0800162 drop_ratio_.Apply(1.0f, 1.0f);
163 drop_ratio_.UpdateBase(0.9f);
philipel5908c712015-12-21 08:23:20 -0800164 } else {
isheriff7620be82016-03-06 23:22:33 -0800165 drop_ratio_.Apply(1.0f, 0.0f);
philipel5908c712015-12-21 08:23:20 -0800166 }
isheriff7620be82016-03-06 23:22:33 -0800167 was_below_max_ = accumulator_ < accumulator_max_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000168}
169
philipel5908c712015-12-21 08:23:20 -0800170// This function signals when to drop frames to the caller. It makes use of the
Åsa Perssonb52a4d92017-11-08 11:33:37 +0100171// drop_ratio_ to smooth out the drops over time.
philipel5908c712015-12-21 08:23:20 -0800172bool FrameDropper::DropFrame() {
isheriff7620be82016-03-06 23:22:33 -0800173 if (!enabled_) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000174 return false;
philipel5908c712015-12-21 08:23:20 -0800175 }
isheriff7620be82016-03-06 23:22:33 -0800176 if (drop_next_) {
177 drop_next_ = false;
178 drop_count_ = 0;
philipel5908c712015-12-21 08:23:20 -0800179 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000180
isheriff7620be82016-03-06 23:22:33 -0800181 if (drop_ratio_.filtered() >= 0.5f) { // Drops per keep
Åsa Perssonb52a4d92017-11-08 11:33:37 +0100182 // Limit is the number of frames we should drop between each kept frame
philipel5908c712015-12-21 08:23:20 -0800183 // to keep our drop ratio. limit is positive in this case.
isheriff7620be82016-03-06 23:22:33 -0800184 float denom = 1.0f - drop_ratio_.filtered();
philipel5908c712015-12-21 08:23:20 -0800185 if (denom < 1e-5) {
186 denom = 1e-5f;
187 }
188 int32_t limit = static_cast<int32_t>(1.0f / denom - 1.0f + 0.5f);
189 // Put a bound on the max amount of dropped frames between each kept
190 // frame, in terms of frame rate and window size (secs).
isheriff7620be82016-03-06 23:22:33 -0800191 int max_limit =
192 static_cast<int>(incoming_frame_rate_ * max_drop_duration_secs_);
philipel5908c712015-12-21 08:23:20 -0800193 if (limit > max_limit) {
194 limit = max_limit;
195 }
isheriff7620be82016-03-06 23:22:33 -0800196 if (drop_count_ < 0) {
197 // Reset the drop_count_ since it was negative and should be positive.
198 drop_count_ = -drop_count_;
philipel5908c712015-12-21 08:23:20 -0800199 }
isheriff7620be82016-03-06 23:22:33 -0800200 if (drop_count_ < limit) {
philipel5908c712015-12-21 08:23:20 -0800201 // As long we are below the limit we should drop frames.
isheriff7620be82016-03-06 23:22:33 -0800202 drop_count_++;
philipel5908c712015-12-21 08:23:20 -0800203 return true;
204 } else {
isheriff7620be82016-03-06 23:22:33 -0800205 // Only when we reset drop_count_ a frame should be kept.
206 drop_count_ = 0;
philipel5908c712015-12-21 08:23:20 -0800207 return false;
208 }
isheriff7620be82016-03-06 23:22:33 -0800209 } else if (drop_ratio_.filtered() > 0.0f &&
210 drop_ratio_.filtered() < 0.5f) { // Keeps per drop
Åsa Perssonb52a4d92017-11-08 11:33:37 +0100211 // Limit is the number of frames we should keep between each drop
philipel5908c712015-12-21 08:23:20 -0800212 // in order to keep the drop ratio. limit is negative in this case,
isheriff7620be82016-03-06 23:22:33 -0800213 // and the drop_count_ is also negative.
214 float denom = drop_ratio_.filtered();
philipel5908c712015-12-21 08:23:20 -0800215 if (denom < 1e-5) {
216 denom = 1e-5f;
217 }
218 int32_t limit = -static_cast<int32_t>(1.0f / denom - 1.0f + 0.5f);
isheriff7620be82016-03-06 23:22:33 -0800219 if (drop_count_ > 0) {
220 // Reset the drop_count_ since we have a positive
221 // drop_count_, and it should be negative.
222 drop_count_ = -drop_count_;
philipel5908c712015-12-21 08:23:20 -0800223 }
isheriff7620be82016-03-06 23:22:33 -0800224 if (drop_count_ > limit) {
225 if (drop_count_ == 0) {
226 // Drop frames when we reset drop_count_.
227 drop_count_--;
philipel5908c712015-12-21 08:23:20 -0800228 return true;
229 } else {
230 // Keep frames as long as we haven't reached limit.
isheriff7620be82016-03-06 23:22:33 -0800231 drop_count_--;
philipel5908c712015-12-21 08:23:20 -0800232 return false;
233 }
234 } else {
isheriff7620be82016-03-06 23:22:33 -0800235 drop_count_ = 0;
philipel5908c712015-12-21 08:23:20 -0800236 return false;
237 }
238 }
isheriff7620be82016-03-06 23:22:33 -0800239 drop_count_ = 0;
philipel5908c712015-12-21 08:23:20 -0800240 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000241}
242
isheriff7620be82016-03-06 23:22:33 -0800243void FrameDropper::SetRates(float bitrate, float incoming_frame_rate) {
philipel5908c712015-12-21 08:23:20 -0800244 // Bit rate of -1 means infinite bandwidth.
isheriff7620be82016-03-06 23:22:33 -0800245 accumulator_max_ = bitrate * kLeakyBucketSizeSeconds;
246 if (target_bitrate_ > 0.0f && bitrate < target_bitrate_ &&
247 accumulator_ > accumulator_max_) {
philipel5908c712015-12-21 08:23:20 -0800248 // Rescale the accumulator level if the accumulator max decreases
isheriff7620be82016-03-06 23:22:33 -0800249 accumulator_ = bitrate / target_bitrate_ * accumulator_;
philipel5908c712015-12-21 08:23:20 -0800250 }
isheriff7620be82016-03-06 23:22:33 -0800251 target_bitrate_ = bitrate;
philipel5908c712015-12-21 08:23:20 -0800252 CapAccumulator();
isheriff7620be82016-03-06 23:22:33 -0800253 incoming_frame_rate_ = incoming_frame_rate;
niklase@google.com470e71d2011-07-07 08:21:25 +0000254}
255
marpan@webrtc.org1dd8d4b2012-10-09 20:43:56 +0000256// Put a cap on the accumulator, i.e., don't let it grow beyond some level.
257// This is a temporary fix for screencasting where very large frames from
258// encoder will cause very slow response (too many frame drops).
isheriff7620be82016-03-06 23:22:33 -0800259// TODO(isheriff): Remove this now that large delta frames are also spread out ?
stefan@webrtc.orgeb917922013-02-18 14:40:18 +0000260void FrameDropper::CapAccumulator() {
isheriff7620be82016-03-06 23:22:33 -0800261 float max_accumulator = target_bitrate_ * kAccumulatorCapBufferSizeSecs;
262 if (accumulator_ > max_accumulator) {
263 accumulator_ = max_accumulator;
marpan@webrtc.org1dd8d4b2012-10-09 20:43:56 +0000264 }
265}
philipel5908c712015-12-21 08:23:20 -0800266} // namespace webrtc