2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
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.
11 #include "webrtc/modules/video_coding/utility/include/frame_dropper.h"
13 #include "webrtc/system_wrappers/interface/trace.h"
18 const float kDefaultKeyFrameSizeAvgKBits = 0.9f;
19 const float kDefaultKeyFrameRatio = 0.99f;
20 const float kDefaultDropRatioAlpha = 0.9f;
21 const float kDefaultDropRatioMax = 0.96f;
22 const float kDefaultMaxTimeToDropFrames = 4.0f; // In seconds.
24 FrameDropper::FrameDropper()
26 _keyFrameSizeAvgKbits(kDefaultKeyFrameSizeAvgKBits),
27 _keyFrameRatio(kDefaultKeyFrameRatio),
28 _dropRatio(kDefaultDropRatioAlpha, kDefaultDropRatioMax),
30 _max_time_drops(kDefaultMaxTimeToDropFrames)
35 FrameDropper::FrameDropper(float max_time_drops)
37 _keyFrameSizeAvgKbits(kDefaultKeyFrameSizeAvgKBits),
38 _keyFrameRatio(kDefaultKeyFrameRatio),
39 _dropRatio(kDefaultDropRatioAlpha, kDefaultDropRatioMax),
41 _max_time_drops(max_time_drops)
49 _keyFrameRatio.Reset(0.99f);
50 _keyFrameRatio.Apply(1.0f, 1.0f/300.0f); // 1 key frame every 10th second in 30 fps
51 _keyFrameSizeAvgKbits.Reset(0.9f);
54 _accumulatorMax = 150.0f; // assume 300 kb/s and 0.5 s window
55 _targetBitRate = 300.0f;
56 _incoming_frame_rate = 30;
57 _keyFrameSpreadFrames = 0.5f * _incoming_frame_rate;
59 _dropRatio.Reset(0.9f);
60 _dropRatio.Apply(0.0f, 0.0f); // Initialize to 0
64 _fastMode = false; // start with normal (non-aggressive) mode
65 // Cap for the encoder buffer level/accumulator, in secs.
66 _cap_buffer_size = 3.0f;
67 // Cap on maximum amount of dropped frames between kept frames, in secs.
68 _max_time_drops = 4.0f;
72 FrameDropper::Enable(bool enable)
78 FrameDropper::Fill(uint32_t frameSizeBytes, bool deltaFrame)
84 float frameSizeKbits = 8.0f * static_cast<float>(frameSizeBytes) / 1000.0f;
85 if (!deltaFrame && !_fastMode) // fast mode does not treat key-frames any different
87 _keyFrameSizeAvgKbits.Apply(1, frameSizeKbits);
88 _keyFrameRatio.Apply(1.0, 1.0);
89 if (frameSizeKbits > _keyFrameSizeAvgKbits.Value())
91 // Remove the average key frame size since we
92 // compensate for key frames when adding delta
94 frameSizeKbits -= _keyFrameSizeAvgKbits.Value();
98 // Shouldn't be negative, so zero is the lower bound.
101 if (_keyFrameRatio.Value() > 1e-5 && 1 / _keyFrameRatio.Value() < _keyFrameSpreadFrames)
103 // We are sending key frames more often than our upper bound for
104 // how much we allow the key frame compensation to be spread
105 // out in time. Therefor we must use the key frame ratio rather
106 // than keyFrameSpreadFrames.
107 _keyFrameCount = static_cast<int32_t>(1 / _keyFrameRatio.Value() + 0.5);
111 // Compensate for the key frame the following frames
112 _keyFrameCount = static_cast<int32_t>(_keyFrameSpreadFrames + 0.5);
117 // Decrease the keyFrameRatio
118 _keyFrameRatio.Apply(1.0, 0.0);
120 // Change the level of the accumulator (bucket)
121 _accumulator += frameSizeKbits;
126 FrameDropper::Leak(uint32_t inputFrameRate)
132 if (inputFrameRate < 1)
136 if (_targetBitRate < 0.0f)
140 _keyFrameSpreadFrames = 0.5f * inputFrameRate;
141 // T is the expected bits per frame (target). If all frames were the same size,
142 // we would get T bits per frame. Notice that T is also weighted to be able to
143 // force a lower frame rate if wanted.
144 float T = _targetBitRate / inputFrameRate;
145 if (_keyFrameCount > 0)
147 // Perform the key frame compensation
148 if (_keyFrameRatio.Value() > 0 && 1 / _keyFrameRatio.Value() < _keyFrameSpreadFrames)
150 T -= _keyFrameSizeAvgKbits.Value() * _keyFrameRatio.Value();
154 T -= _keyFrameSizeAvgKbits.Value() / _keyFrameSpreadFrames;
159 if (_accumulator < 0.0f)
167 FrameDropper::UpdateNack(uint32_t nackBytes)
173 _accumulator += static_cast<float>(nackBytes) * 8.0f / 1000.0f;
177 FrameDropper::FillBucket(float inKbits, float outKbits)
179 _accumulator += (inKbits - outKbits);
183 FrameDropper::UpdateRatio()
185 if (_accumulator > 1.3f * _accumulatorMax)
187 // Too far above accumulator max, react faster
188 _dropRatio.UpdateBase(0.8f);
192 // Go back to normal reaction
193 _dropRatio.UpdateBase(0.9f);
195 if (_accumulator > _accumulatorMax)
197 // We are above accumulator max, and should ideally
198 // drop a frame. Increase the dropRatio and drop
206 // always drop in aggressive mode
210 _dropRatio.Apply(1.0f, 1.0f);
211 _dropRatio.UpdateBase(0.9f);
215 _dropRatio.Apply(1.0f, 0.0f);
217 _wasBelowMax = _accumulator < _accumulatorMax;
220 // This function signals when to drop frames to the caller. It makes use of the dropRatio
221 // to smooth out the drops over time.
223 FrameDropper::DropFrame()
235 if (_dropRatio.Value() >= 0.5f) // Drops per keep
237 // limit is the number of frames we should drop between each kept frame
238 // to keep our drop ratio. limit is positive in this case.
239 float denom = 1.0f - _dropRatio.Value();
244 int32_t limit = static_cast<int32_t>(1.0f / denom - 1.0f + 0.5f);
245 // Put a bound on the max amount of dropped frames between each kept
246 // frame, in terms of frame rate and window size (secs).
247 int max_limit = static_cast<int>(_incoming_frame_rate *
249 if (limit > max_limit) {
254 // Reset the _dropCount since it was negative and should be positive.
255 if (_dropRatio.Value() > 0.4f)
257 _dropCount = -_dropCount;
264 if (_dropCount < limit)
266 // As long we are below the limit we should drop frames.
272 // Only when we reset _dropCount a frame should be kept.
277 else if (_dropRatio.Value() > 0.0f && _dropRatio.Value() < 0.5f) // Keeps per drop
279 // limit is the number of frames we should keep between each drop
280 // in order to keep the drop ratio. limit is negative in this case,
281 // and the _dropCount is also negative.
282 float denom = _dropRatio.Value();
287 int32_t limit = -static_cast<int32_t>(1.0f / denom - 1.0f + 0.5f);
290 // Reset the _dropCount since we have a positive
291 // _dropCount, and it should be negative.
292 if (_dropRatio.Value() < 0.6f)
294 _dropCount = -_dropCount;
301 if (_dropCount > limit)
305 // Drop frames when we reset _dropCount.
311 // Keep frames as long as we haven't reached limit.
325 // A simpler version, unfiltered and quicker
326 //bool dropNext = _dropNext;
332 FrameDropper::SetRates(float bitRate, float incoming_frame_rate)
334 // Bit rate of -1 means infinite bandwidth.
335 _accumulatorMax = bitRate * _windowSize; // bitRate * windowSize (in seconds)
336 if (_targetBitRate > 0.0f && bitRate < _targetBitRate && _accumulator > _accumulatorMax)
338 // Rescale the accumulator level if the accumulator max decreases
339 _accumulator = bitRate / _targetBitRate * _accumulator;
341 _targetBitRate = bitRate;
343 _incoming_frame_rate = incoming_frame_rate;
347 FrameDropper::ActualFrameRate(uint32_t inputFrameRate) const
351 return static_cast<float>(inputFrameRate);
353 return inputFrameRate * (1.0f - _dropRatio.Value());
356 // Put a cap on the accumulator, i.e., don't let it grow beyond some level.
357 // This is a temporary fix for screencasting where very large frames from
358 // encoder will cause very slow response (too many frame drops).
359 void FrameDropper::CapAccumulator() {
360 float max_accumulator = _targetBitRate * _cap_buffer_size;
361 if (_accumulator > max_accumulator) {
362 _accumulator = max_accumulator;