blob: 73d641055f966ac5c2c51d7a0e4602abd74f343e [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
kjellander@webrtc.orgb7ce9642015-11-18 23:04:10 +010011#include "webrtc/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
Edward Lemurc20978e2017-07-06 19:44:34 +020015#include "webrtc/rtc_base/logging.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000016
philipel5908c712015-12-21 08:23:20 -080017namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000018
isheriff7620be82016-03-06 23:22:33 -080019namespace {
20
21const float kDefaultFrameSizeAlpha = 0.9f;
22const float kDefaultKeyFrameRatioAlpha = 0.99f;
23// 1 key frame every 10th second in 30 fps.
24const float kDefaultKeyFrameRatioValue = 1 / 300.0f;
25
stefan@webrtc.org84cd8e32013-03-07 13:12:32 +000026const float kDefaultDropRatioAlpha = 0.9f;
isheriff7620be82016-03-06 23:22:33 -080027const float kDefaultDropRatioValue = 0.96f;
28// Maximum duration over which frames are continuously dropped.
29const float kDefaultMaxDropDurationSecs = 4.0f;
30
31// Default target bitrate.
32// TODO(isheriff): Should this be higher to avoid dropping too many packets when
33// the bandwidth is unknown at the start ?
34const float kDefaultTargetBitrateKbps = 300.0f;
35const float kDefaultIncomingFrameRate = 30;
36const float kLeakyBucketSizeSeconds = 0.5f;
37
38// A delta frame that is bigger than |kLargeDeltaFactor| times the average
39// delta frame is a large frame that is spread out for accumulation.
40const int kLargeDeltaFactor = 3;
41
42// Cap on the frame size accumulator to prevent excessive drops.
43const float kAccumulatorCapBufferSizeSecs = 3.0f;
44} // namespace
stefan@webrtc.org84cd8e32013-03-07 13:12:32 +000045
stefan@webrtc.orgeb917922013-02-18 14:40:18 +000046FrameDropper::FrameDropper()
isheriff7620be82016-03-06 23:22:33 -080047 : key_frame_ratio_(kDefaultKeyFrameRatioAlpha),
48 delta_frame_size_avg_kbits_(kDefaultFrameSizeAlpha),
49 drop_ratio_(kDefaultDropRatioAlpha, kDefaultDropRatioValue),
50 enabled_(true),
51 max_drop_duration_secs_(kDefaultMaxDropDurationSecs) {
philipel5908c712015-12-21 08:23:20 -080052 Reset();
stefan@webrtc.org84cd8e32013-03-07 13:12:32 +000053}
54
isheriff7620be82016-03-06 23:22:33 -080055FrameDropper::FrameDropper(float max_drop_duration_secs)
56 : key_frame_ratio_(kDefaultKeyFrameRatioAlpha),
57 delta_frame_size_avg_kbits_(kDefaultFrameSizeAlpha),
58 drop_ratio_(kDefaultDropRatioAlpha, kDefaultDropRatioValue),
59 enabled_(true),
60 max_drop_duration_secs_(max_drop_duration_secs) {
philipel5908c712015-12-21 08:23:20 -080061 Reset();
niklase@google.com470e71d2011-07-07 08:21:25 +000062}
63
philipel5908c712015-12-21 08:23:20 -080064void FrameDropper::Reset() {
isheriff7620be82016-03-06 23:22:33 -080065 key_frame_ratio_.Reset(kDefaultKeyFrameRatioAlpha);
66 key_frame_ratio_.Apply(1.0f, kDefaultKeyFrameRatioValue);
67 delta_frame_size_avg_kbits_.Reset(kDefaultFrameSizeAlpha);
68
69 accumulator_ = 0.0f;
70 accumulator_max_ = kDefaultTargetBitrateKbps / 2;
71 target_bitrate_ = kDefaultTargetBitrateKbps;
72 incoming_frame_rate_ = kDefaultIncomingFrameRate;
73
74 large_frame_accumulation_count_ = 0;
isheriff4d7bc242016-04-21 16:37:20 -070075 large_frame_accumulation_chunk_size_ = 0;
isheriff7620be82016-03-06 23:22:33 -080076 large_frame_accumulation_spread_ = 0.5 * kDefaultIncomingFrameRate;
77
78 drop_next_ = false;
79 drop_ratio_.Reset(0.9f);
80 drop_ratio_.Apply(0.0f, 0.0f);
81 drop_count_ = 0;
82 was_below_max_ = true;
philipel5908c712015-12-21 08:23:20 -080083}
84
85void FrameDropper::Enable(bool enable) {
isheriff7620be82016-03-06 23:22:33 -080086 enabled_ = enable;
philipel5908c712015-12-21 08:23:20 -080087}
88
isheriff7620be82016-03-06 23:22:33 -080089void FrameDropper::Fill(size_t framesize_bytes, bool delta_frame) {
90 if (!enabled_) {
philipel5908c712015-12-21 08:23:20 -080091 return;
92 }
isheriff7620be82016-03-06 23:22:33 -080093 float framesize_kbits = 8.0f * static_cast<float>(framesize_bytes) / 1000.0f;
94 if (!delta_frame) {
95 key_frame_ratio_.Apply(1.0, 1.0);
96 // Do not spread if we are already doing it (or we risk dropping bits that
97 // need accumulation). Given we compute the key
98 // frame ratio and spread based on that, this should not normally happen.
99 if (large_frame_accumulation_count_ == 0) {
100 if (key_frame_ratio_.filtered() > 1e-5 &&
101 1 / key_frame_ratio_.filtered() < large_frame_accumulation_spread_) {
102 large_frame_accumulation_count_ =
103 static_cast<int32_t>(1 / key_frame_ratio_.filtered() + 0.5);
104 } else {
105 large_frame_accumulation_count_ =
106 static_cast<int32_t>(large_frame_accumulation_spread_ + 0.5);
107 }
108 large_frame_accumulation_chunk_size_ =
109 framesize_kbits / large_frame_accumulation_count_;
110 framesize_kbits = 0;
philipel5908c712015-12-21 08:23:20 -0800111 }
112 } else {
isheriff7620be82016-03-06 23:22:33 -0800113 // Identify if it is an unusually large delta frame and spread accumulation
114 // if that is the case.
115 if (delta_frame_size_avg_kbits_.filtered() != -1 &&
116 (framesize_kbits >
117 kLargeDeltaFactor * delta_frame_size_avg_kbits_.filtered()) &&
118 large_frame_accumulation_count_ == 0) {
119 large_frame_accumulation_count_ =
120 static_cast<int32_t>(large_frame_accumulation_spread_ + 0.5);
121 large_frame_accumulation_chunk_size_ =
122 framesize_kbits / large_frame_accumulation_count_;
123 framesize_kbits = 0;
124 } else {
125 delta_frame_size_avg_kbits_.Apply(1, framesize_kbits);
126 }
127 key_frame_ratio_.Apply(1.0, 0.0);
philipel5908c712015-12-21 08:23:20 -0800128 }
129 // Change the level of the accumulator (bucket)
isheriff7620be82016-03-06 23:22:33 -0800130 accumulator_ += framesize_kbits;
philipel5908c712015-12-21 08:23:20 -0800131 CapAccumulator();
132}
133
isheriff7620be82016-03-06 23:22:33 -0800134void FrameDropper::Leak(uint32_t input_framerate) {
135 if (!enabled_) {
philipel5908c712015-12-21 08:23:20 -0800136 return;
137 }
isheriff7620be82016-03-06 23:22:33 -0800138 if (input_framerate < 1) {
philipel5908c712015-12-21 08:23:20 -0800139 return;
140 }
isheriff7620be82016-03-06 23:22:33 -0800141 if (target_bitrate_ < 0.0f) {
philipel5908c712015-12-21 08:23:20 -0800142 return;
143 }
isheriff7620be82016-03-06 23:22:33 -0800144 // Add lower bound for large frame accumulation spread.
145 large_frame_accumulation_spread_ = std::max(0.5 * input_framerate, 5.0);
146 // Expected bits per frame based on current input frame rate.
147 float expected_bits_per_frame = target_bitrate_ / input_framerate;
148 if (large_frame_accumulation_count_ > 0) {
149 expected_bits_per_frame -= large_frame_accumulation_chunk_size_;
150 --large_frame_accumulation_count_;
philipel5908c712015-12-21 08:23:20 -0800151 }
isheriff7620be82016-03-06 23:22:33 -0800152 accumulator_ -= expected_bits_per_frame;
153 if (accumulator_ < 0.0f) {
154 accumulator_ = 0.0f;
philipel5908c712015-12-21 08:23:20 -0800155 }
156 UpdateRatio();
niklase@google.com470e71d2011-07-07 08:21:25 +0000157}
158
philipel5908c712015-12-21 08:23:20 -0800159void FrameDropper::UpdateRatio() {
isheriff7620be82016-03-06 23:22:33 -0800160 if (accumulator_ > 1.3f * accumulator_max_) {
philipel5908c712015-12-21 08:23:20 -0800161 // Too far above accumulator max, react faster
isheriff7620be82016-03-06 23:22:33 -0800162 drop_ratio_.UpdateBase(0.8f);
philipel5908c712015-12-21 08:23:20 -0800163 } else {
164 // Go back to normal reaction
isheriff7620be82016-03-06 23:22:33 -0800165 drop_ratio_.UpdateBase(0.9f);
philipel5908c712015-12-21 08:23:20 -0800166 }
isheriff7620be82016-03-06 23:22:33 -0800167 if (accumulator_ > accumulator_max_) {
philipel5908c712015-12-21 08:23:20 -0800168 // We are above accumulator max, and should ideally
169 // drop a frame. Increase the dropRatio and drop
170 // the frame later.
isheriff7620be82016-03-06 23:22:33 -0800171 if (was_below_max_) {
172 drop_next_ = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000173 }
isheriff7620be82016-03-06 23:22:33 -0800174 drop_ratio_.Apply(1.0f, 1.0f);
175 drop_ratio_.UpdateBase(0.9f);
philipel5908c712015-12-21 08:23:20 -0800176 } else {
isheriff7620be82016-03-06 23:22:33 -0800177 drop_ratio_.Apply(1.0f, 0.0f);
philipel5908c712015-12-21 08:23:20 -0800178 }
isheriff7620be82016-03-06 23:22:33 -0800179 was_below_max_ = accumulator_ < accumulator_max_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000180}
181
philipel5908c712015-12-21 08:23:20 -0800182// This function signals when to drop frames to the caller. It makes use of the
183// dropRatio
niklase@google.com470e71d2011-07-07 08:21:25 +0000184// to smooth out the drops over time.
philipel5908c712015-12-21 08:23:20 -0800185bool FrameDropper::DropFrame() {
isheriff7620be82016-03-06 23:22:33 -0800186 if (!enabled_) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000187 return false;
philipel5908c712015-12-21 08:23:20 -0800188 }
isheriff7620be82016-03-06 23:22:33 -0800189 if (drop_next_) {
190 drop_next_ = false;
191 drop_count_ = 0;
philipel5908c712015-12-21 08:23:20 -0800192 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000193
isheriff7620be82016-03-06 23:22:33 -0800194 if (drop_ratio_.filtered() >= 0.5f) { // Drops per keep
philipel5908c712015-12-21 08:23:20 -0800195 // limit is the number of frames we should drop between each kept frame
196 // to keep our drop ratio. limit is positive in this case.
isheriff7620be82016-03-06 23:22:33 -0800197 float denom = 1.0f - drop_ratio_.filtered();
philipel5908c712015-12-21 08:23:20 -0800198 if (denom < 1e-5) {
199 denom = 1e-5f;
200 }
201 int32_t limit = static_cast<int32_t>(1.0f / denom - 1.0f + 0.5f);
202 // Put a bound on the max amount of dropped frames between each kept
203 // frame, in terms of frame rate and window size (secs).
isheriff7620be82016-03-06 23:22:33 -0800204 int max_limit =
205 static_cast<int>(incoming_frame_rate_ * max_drop_duration_secs_);
philipel5908c712015-12-21 08:23:20 -0800206 if (limit > max_limit) {
207 limit = max_limit;
208 }
isheriff7620be82016-03-06 23:22:33 -0800209 if (drop_count_ < 0) {
210 // Reset the drop_count_ since it was negative and should be positive.
211 drop_count_ = -drop_count_;
philipel5908c712015-12-21 08:23:20 -0800212 }
isheriff7620be82016-03-06 23:22:33 -0800213 if (drop_count_ < limit) {
philipel5908c712015-12-21 08:23:20 -0800214 // As long we are below the limit we should drop frames.
isheriff7620be82016-03-06 23:22:33 -0800215 drop_count_++;
philipel5908c712015-12-21 08:23:20 -0800216 return true;
217 } else {
isheriff7620be82016-03-06 23:22:33 -0800218 // Only when we reset drop_count_ a frame should be kept.
219 drop_count_ = 0;
philipel5908c712015-12-21 08:23:20 -0800220 return false;
221 }
isheriff7620be82016-03-06 23:22:33 -0800222 } else if (drop_ratio_.filtered() > 0.0f &&
223 drop_ratio_.filtered() < 0.5f) { // Keeps per drop
philipel5908c712015-12-21 08:23:20 -0800224 // limit is the number of frames we should keep between each drop
225 // in order to keep the drop ratio. limit is negative in this case,
isheriff7620be82016-03-06 23:22:33 -0800226 // and the drop_count_ is also negative.
227 float denom = drop_ratio_.filtered();
philipel5908c712015-12-21 08:23:20 -0800228 if (denom < 1e-5) {
229 denom = 1e-5f;
230 }
231 int32_t limit = -static_cast<int32_t>(1.0f / denom - 1.0f + 0.5f);
isheriff7620be82016-03-06 23:22:33 -0800232 if (drop_count_ > 0) {
233 // Reset the drop_count_ since we have a positive
234 // drop_count_, and it should be negative.
235 drop_count_ = -drop_count_;
philipel5908c712015-12-21 08:23:20 -0800236 }
isheriff7620be82016-03-06 23:22:33 -0800237 if (drop_count_ > limit) {
238 if (drop_count_ == 0) {
239 // Drop frames when we reset drop_count_.
240 drop_count_--;
philipel5908c712015-12-21 08:23:20 -0800241 return true;
242 } else {
243 // Keep frames as long as we haven't reached limit.
isheriff7620be82016-03-06 23:22:33 -0800244 drop_count_--;
philipel5908c712015-12-21 08:23:20 -0800245 return false;
246 }
247 } else {
isheriff7620be82016-03-06 23:22:33 -0800248 drop_count_ = 0;
philipel5908c712015-12-21 08:23:20 -0800249 return false;
250 }
251 }
isheriff7620be82016-03-06 23:22:33 -0800252 drop_count_ = 0;
philipel5908c712015-12-21 08:23:20 -0800253 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000254}
255
isheriff7620be82016-03-06 23:22:33 -0800256void FrameDropper::SetRates(float bitrate, float incoming_frame_rate) {
philipel5908c712015-12-21 08:23:20 -0800257 // Bit rate of -1 means infinite bandwidth.
isheriff7620be82016-03-06 23:22:33 -0800258 accumulator_max_ = bitrate * kLeakyBucketSizeSeconds;
259 if (target_bitrate_ > 0.0f && bitrate < target_bitrate_ &&
260 accumulator_ > accumulator_max_) {
philipel5908c712015-12-21 08:23:20 -0800261 // Rescale the accumulator level if the accumulator max decreases
isheriff7620be82016-03-06 23:22:33 -0800262 accumulator_ = bitrate / target_bitrate_ * accumulator_;
philipel5908c712015-12-21 08:23:20 -0800263 }
isheriff7620be82016-03-06 23:22:33 -0800264 target_bitrate_ = bitrate;
philipel5908c712015-12-21 08:23:20 -0800265 CapAccumulator();
isheriff7620be82016-03-06 23:22:33 -0800266 incoming_frame_rate_ = incoming_frame_rate;
niklase@google.com470e71d2011-07-07 08:21:25 +0000267}
268
marpan@webrtc.org1dd8d4b2012-10-09 20:43:56 +0000269// Put a cap on the accumulator, i.e., don't let it grow beyond some level.
270// This is a temporary fix for screencasting where very large frames from
271// encoder will cause very slow response (too many frame drops).
isheriff7620be82016-03-06 23:22:33 -0800272// TODO(isheriff): Remove this now that large delta frames are also spread out ?
stefan@webrtc.orgeb917922013-02-18 14:40:18 +0000273void FrameDropper::CapAccumulator() {
isheriff7620be82016-03-06 23:22:33 -0800274 float max_accumulator = target_bitrate_ * kAccumulatorCapBufferSizeSecs;
275 if (accumulator_ > max_accumulator) {
276 accumulator_ = max_accumulator;
marpan@webrtc.org1dd8d4b2012-10-09 20:43:56 +0000277 }
278}
philipel5908c712015-12-21 08:23:20 -0800279} // namespace webrtc