niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
| 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.org | b7ce964 | 2015-11-18 23:04:10 +0100 | [diff] [blame] | 11 | #ifndef WEBRTC_MODULES_VIDEO_CODING_UTILITY_FRAME_DROPPER_H_ |
| 12 | #define WEBRTC_MODULES_VIDEO_CODING_UTILITY_FRAME_DROPPER_H_ |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 13 | |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 14 | #include <cstddef> |
| 15 | |
terelius | bc5d921 | 2017-01-13 09:14:33 -0800 | [diff] [blame] | 16 | #include "webrtc/base/numerics/exp_filter.h" |
stefan@webrtc.org | eb91792 | 2013-02-18 14:40:18 +0000 | [diff] [blame] | 17 | #include "webrtc/typedefs.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 18 | |
kjellander@webrtc.org | b7ce964 | 2015-11-18 23:04:10 +0100 | [diff] [blame] | 19 | namespace webrtc { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 20 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 21 | // The Frame Dropper implements a variant of the leaky bucket algorithm |
| 22 | // for keeping track of when to drop frames to avoid bit rate |
| 23 | // over use when the encoder can't keep its bit rate. |
kjellander@webrtc.org | b7ce964 | 2015-11-18 23:04:10 +0100 | [diff] [blame] | 24 | class FrameDropper { |
| 25 | public: |
philipel | 5908c71 | 2015-12-21 08:23:20 -0800 | [diff] [blame] | 26 | FrameDropper(); |
| 27 | explicit FrameDropper(float max_time_drops); |
| 28 | virtual ~FrameDropper() {} |
stefan@webrtc.org | eb91792 | 2013-02-18 14:40:18 +0000 | [diff] [blame] | 29 | |
philipel | 5908c71 | 2015-12-21 08:23:20 -0800 | [diff] [blame] | 30 | // Resets the FrameDropper to its initial state. |
| 31 | // This means that the frameRateWeight is set to its |
| 32 | // default value as well. |
| 33 | virtual void Reset(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 34 | |
philipel | 5908c71 | 2015-12-21 08:23:20 -0800 | [diff] [blame] | 35 | virtual void Enable(bool enable); |
| 36 | // Answers the question if it's time to drop a frame |
| 37 | // if we want to reach a given frame rate. Must be |
| 38 | // called for every frame. |
| 39 | // |
| 40 | // Return value : True if we should drop the current frame |
| 41 | virtual bool DropFrame(); |
| 42 | // Updates the FrameDropper with the size of the latest encoded |
| 43 | // frame. The FrameDropper calculates a new drop ratio (can be |
| 44 | // seen as the probability to drop a frame) and updates its |
| 45 | // internal statistics. |
| 46 | // |
| 47 | // Input: |
| 48 | // - frameSizeBytes : The size of the latest frame |
| 49 | // returned from the encoder. |
| 50 | // - deltaFrame : True if the encoder returned |
| 51 | // a key frame. |
| 52 | virtual void Fill(size_t frameSizeBytes, bool deltaFrame); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 53 | |
philipel | 5908c71 | 2015-12-21 08:23:20 -0800 | [diff] [blame] | 54 | virtual void Leak(uint32_t inputFrameRate); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 55 | |
philipel | 5908c71 | 2015-12-21 08:23:20 -0800 | [diff] [blame] | 56 | // Sets the target bit rate and the frame rate produced by |
| 57 | // the camera. |
| 58 | // |
| 59 | // Input: |
| 60 | // - bitRate : The target bit rate |
| 61 | virtual void SetRates(float bitRate, float incoming_frame_rate); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 62 | |
kjellander@webrtc.org | b7ce964 | 2015-11-18 23:04:10 +0100 | [diff] [blame] | 63 | private: |
philipel | 5908c71 | 2015-12-21 08:23:20 -0800 | [diff] [blame] | 64 | void UpdateRatio(); |
| 65 | void CapAccumulator(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 66 | |
isheriff | 7620be8 | 2016-03-06 23:22:33 -0800 | [diff] [blame] | 67 | rtc::ExpFilter key_frame_ratio_; |
| 68 | rtc::ExpFilter delta_frame_size_avg_kbits_; |
| 69 | |
| 70 | // Key frames and large delta frames are not immediately accumulated in the |
| 71 | // bucket since they can immediately overflow the bucket leading to large |
| 72 | // drops on the following packets that may be much smaller. Instead these |
| 73 | // large frames are accumulated over several frames when the bucket leaks. |
| 74 | |
| 75 | // |large_frame_accumulation_spread_| represents the number of frames over |
| 76 | // which a large frame is accumulated. |
| 77 | float large_frame_accumulation_spread_; |
| 78 | // |large_frame_accumulation_count_| represents the number of frames left |
| 79 | // to finish accumulating a large frame. |
| 80 | int large_frame_accumulation_count_; |
| 81 | // |large_frame_accumulation_chunk_size_| represents the size of a single |
| 82 | // chunk for large frame accumulation. |
| 83 | float large_frame_accumulation_chunk_size_; |
| 84 | |
| 85 | float accumulator_; |
| 86 | float accumulator_max_; |
| 87 | float target_bitrate_; |
| 88 | bool drop_next_; |
| 89 | rtc::ExpFilter drop_ratio_; |
| 90 | int drop_count_; |
| 91 | float incoming_frame_rate_; |
| 92 | bool was_below_max_; |
| 93 | bool enabled_; |
| 94 | const float max_drop_duration_secs_; |
| 95 | }; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 96 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 97 | } // namespace webrtc |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 98 | |
kjellander@webrtc.org | b7ce964 | 2015-11-18 23:04:10 +0100 | [diff] [blame] | 99 | #endif // WEBRTC_MODULES_VIDEO_CODING_UTILITY_FRAME_DROPPER_H_ |