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