philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 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_RTP_FRAME_REFERENCE_FINDER_H_ |
| 12 | #define MODULES_VIDEO_CODING_RTP_FRAME_REFERENCE_FINDER_H_ |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 13 | |
| 14 | #include <array> |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 15 | #include <deque> |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 16 | #include <map> |
kwiberg | fd8be34 | 2016-05-14 19:44:11 -0700 | [diff] [blame] | 17 | #include <memory> |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 18 | #include <set> |
| 19 | #include <utility> |
| 20 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "modules/include/module_common_types.h" |
philipel | 2837edc | 2018-10-02 13:55:47 +0200 | [diff] [blame] | 22 | #include "modules/rtp_rtcp/source/rtp_generic_frame_descriptor.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 23 | #include "rtc_base/critical_section.h" |
Bjorn Terelius | a194e58 | 2017-10-25 13:07:09 +0200 | [diff] [blame] | 24 | #include "rtc_base/numerics/sequence_number_util.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 25 | #include "rtc_base/thread_annotations.h" |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 26 | |
| 27 | namespace webrtc { |
| 28 | namespace video_coding { |
| 29 | |
philipel | e7c891f | 2018-02-22 14:35:06 +0100 | [diff] [blame] | 30 | class EncodedFrame; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 31 | class RtpFrameObject; |
philipel | 17deeb4 | 2016-08-11 15:09:26 +0200 | [diff] [blame] | 32 | |
| 33 | // A complete frame is a frame which has received all its packets and all its |
| 34 | // references are known. |
| 35 | class OnCompleteFrameCallback { |
| 36 | public: |
| 37 | virtual ~OnCompleteFrameCallback() {} |
philipel | e7c891f | 2018-02-22 14:35:06 +0100 | [diff] [blame] | 38 | virtual void OnCompleteFrame(std::unique_ptr<EncodedFrame> frame) = 0; |
philipel | 17deeb4 | 2016-08-11 15:09:26 +0200 | [diff] [blame] | 39 | }; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 40 | |
| 41 | class RtpFrameReferenceFinder { |
| 42 | public: |
| 43 | explicit RtpFrameReferenceFinder(OnCompleteFrameCallback* frame_callback); |
Mirko Bonadei | 8fdcac3 | 2018-08-28 16:30:18 +0200 | [diff] [blame] | 44 | ~RtpFrameReferenceFinder(); |
philipel | 463d301 | 2016-09-09 03:32:44 -0700 | [diff] [blame] | 45 | |
| 46 | // Manage this frame until: |
| 47 | // - We have all information needed to determine its references, after |
| 48 | // which |frame_callback_| is called with the completed frame, or |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 49 | // - We have too many stashed frames (determined by |kMaxStashedFrames|) |
philipel | 463d301 | 2016-09-09 03:32:44 -0700 | [diff] [blame] | 50 | // so we drop this frame, or |
| 51 | // - It gets cleared by ClearTo, which also means we drop it. |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 52 | void ManageFrame(std::unique_ptr<RtpFrameObject> frame); |
philipel | 463d301 | 2016-09-09 03:32:44 -0700 | [diff] [blame] | 53 | |
| 54 | // Notifies that padding has been received, which the reference finder |
| 55 | // might need to calculate the references of a frame. |
philipel | 9b2ce6b | 2016-07-05 05:04:46 -0700 | [diff] [blame] | 56 | void PaddingReceived(uint16_t seq_num); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 57 | |
philipel | 463d301 | 2016-09-09 03:32:44 -0700 | [diff] [blame] | 58 | // Clear all stashed frames that include packets older than |seq_num|. |
| 59 | void ClearTo(uint16_t seq_num); |
| 60 | |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 61 | private: |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 62 | static const uint16_t kPicIdLength = 1 << 15; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 63 | static const uint8_t kMaxTemporalLayers = 5; |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 64 | static const int kMaxLayerInfo = 50; |
Sergey Silkin | 52233a3 | 2018-07-31 14:30:54 +0200 | [diff] [blame] | 65 | static const int kMaxStashedFrames = 100; |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 66 | static const int kMaxNotYetReceivedFrames = 100; |
| 67 | static const int kMaxGofSaved = 50; |
philipel | 9b2ce6b | 2016-07-05 05:04:46 -0700 | [diff] [blame] | 68 | static const int kMaxPaddingAge = 100; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 69 | |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 70 | enum FrameDecision { kStash, kHandOff, kDrop }; |
philipel | c9b27d5 | 2016-07-15 06:50:27 -0700 | [diff] [blame] | 71 | |
| 72 | struct GofInfo { |
| 73 | GofInfo(GofInfoVP9* gof, uint16_t last_picture_id) |
| 74 | : gof(gof), last_picture_id(last_picture_id) {} |
| 75 | GofInfoVP9* gof; |
| 76 | uint16_t last_picture_id; |
| 77 | }; |
| 78 | |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 79 | rtc::CriticalSection crit_; |
| 80 | |
philipel | 9b2ce6b | 2016-07-05 05:04:46 -0700 | [diff] [blame] | 81 | // Find the relevant group of pictures and update its "last-picture-id-with |
| 82 | // padding" sequence number. |
| 83 | void UpdateLastPictureIdWithPadding(uint16_t seq_num) |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 84 | RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
philipel | 9b2ce6b | 2016-07-05 05:04:46 -0700 | [diff] [blame] | 85 | |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 86 | // Retry stashed frames until no more complete frames are found. |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 87 | void RetryStashedFrames() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 88 | |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 89 | FrameDecision ManageFrameInternal(RtpFrameObject* frame) |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 90 | RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 91 | |
philipel | 2837edc | 2018-10-02 13:55:47 +0200 | [diff] [blame] | 92 | FrameDecision ManageFrameGeneric(RtpFrameObject* frame, |
| 93 | const RtpGenericFrameDescriptor& descriptor) |
philipel | dabfcae | 2018-09-25 12:54:37 +0200 | [diff] [blame] | 94 | RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 95 | |
| 96 | // Find references for frames with no or very limited information in the |
| 97 | // descriptor. If |picture_id| is unspecified then packet sequence numbers |
| 98 | // will be used to determine the references of the frames. |
| 99 | FrameDecision ManageFramePidOrSeqNum(RtpFrameObject* frame, int picture_id) |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 100 | RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 101 | |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 102 | // Find references for Vp8 frames |
| 103 | FrameDecision ManageFrameVp8(RtpFrameObject* frame) |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 104 | RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 105 | |
| 106 | // Updates necessary layer info state used to determine frame references for |
| 107 | // Vp8. |
philipel | 1610f94 | 2017-12-12 13:58:31 +0100 | [diff] [blame] | 108 | void UpdateLayerInfoVp8(RtpFrameObject* frame, |
philipel | 57ec685 | 2018-07-03 18:09:32 +0200 | [diff] [blame] | 109 | int64_t unwrapped_tl0, |
| 110 | uint8_t temporal_idx) |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 111 | RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 112 | |
| 113 | // Find references for Vp9 frames |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 114 | FrameDecision ManageFrameVp9(RtpFrameObject* frame) |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 115 | RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 116 | |
| 117 | // Check if we are missing a frame necessary to determine the references |
| 118 | // for this frame. |
philipel | c9b27d5 | 2016-07-15 06:50:27 -0700 | [diff] [blame] | 119 | bool MissingRequiredFrameVp9(uint16_t picture_id, const GofInfo& info) |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 120 | RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 121 | |
| 122 | // Updates which frames that have been received. If there is a gap, |
| 123 | // missing frames will be added to |missing_frames_for_layer_| or |
| 124 | // if this is an already missing frame then it will be removed. |
philipel | c9b27d5 | 2016-07-15 06:50:27 -0700 | [diff] [blame] | 125 | void FrameReceivedVp9(uint16_t picture_id, GofInfo* info) |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 126 | RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 127 | |
| 128 | // Check if there is a frame with the up-switch flag set in the interval |
| 129 | // (|pid_ref|, |picture_id|) with temporal layer smaller than |temporal_idx|. |
| 130 | bool UpSwitchInIntervalVp9(uint16_t picture_id, |
| 131 | uint8_t temporal_idx, |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 132 | uint16_t pid_ref) |
| 133 | RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 134 | |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 135 | // Unwrap |frame|s picture id and its references to 16 bits. |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 136 | void UnwrapPictureIds(RtpFrameObject* frame) |
| 137 | RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 138 | |
philipel | 9b2ce6b | 2016-07-05 05:04:46 -0700 | [diff] [blame] | 139 | // For every group of pictures, hold two sequence numbers. The first being |
| 140 | // the sequence number of the last packet of the last completed frame, and |
| 141 | // the second being the sequence number of the last packet of the last |
| 142 | // completed frame advanced by any potential continuous packets of padding. |
| 143 | std::map<uint16_t, |
| 144 | std::pair<uint16_t, uint16_t>, |
| 145 | DescendingSeqNumComp<uint16_t>> |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 146 | last_seq_num_gop_ RTC_GUARDED_BY(crit_); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 147 | |
| 148 | // Save the last picture id in order to detect when there is a gap in frames |
| 149 | // that have not yet been fully received. |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 150 | int last_picture_id_ RTC_GUARDED_BY(crit_); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 151 | |
philipel | 9b2ce6b | 2016-07-05 05:04:46 -0700 | [diff] [blame] | 152 | // Padding packets that have been received but that are not yet continuous |
| 153 | // with any group of pictures. |
| 154 | std::set<uint16_t, DescendingSeqNumComp<uint16_t>> stashed_padding_ |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 155 | RTC_GUARDED_BY(crit_); |
philipel | 9b2ce6b | 2016-07-05 05:04:46 -0700 | [diff] [blame] | 156 | |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 157 | // Frames earlier than the last received frame that have not yet been |
| 158 | // fully received. |
| 159 | std::set<uint16_t, DescendingSeqNumComp<uint16_t, kPicIdLength>> |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 160 | not_yet_received_frames_ RTC_GUARDED_BY(crit_); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 161 | |
| 162 | // Frames that have been fully received but didn't have all the information |
| 163 | // needed to determine their references. |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 164 | std::deque<std::unique_ptr<RtpFrameObject>> stashed_frames_ |
| 165 | RTC_GUARDED_BY(crit_); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 166 | |
| 167 | // Holds the information about the last completed frame for a given temporal |
philipel | 57ec685 | 2018-07-03 18:09:32 +0200 | [diff] [blame] | 168 | // layer given an unwrapped Tl0 picture index. |
| 169 | std::map<int64_t, std::array<int16_t, kMaxTemporalLayers>> layer_info_ |
| 170 | RTC_GUARDED_BY(crit_); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 171 | |
| 172 | // Where the current scalability structure is in the |
| 173 | // |scalability_structures_| array. |
| 174 | uint8_t current_ss_idx_; |
| 175 | |
| 176 | // Holds received scalability structures. |
| 177 | std::array<GofInfoVP9, kMaxGofSaved> scalability_structures_ |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 178 | RTC_GUARDED_BY(crit_); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 179 | |
philipel | 57ec685 | 2018-07-03 18:09:32 +0200 | [diff] [blame] | 180 | // Holds the the Gof information for a given unwrapped TL0 picture index. |
| 181 | std::map<int64_t, GofInfo> gof_info_ RTC_GUARDED_BY(crit_); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 182 | |
| 183 | // Keep track of which picture id and which temporal layer that had the |
| 184 | // up switch flag set. |
philipel | c9b27d5 | 2016-07-15 06:50:27 -0700 | [diff] [blame] | 185 | std::map<uint16_t, uint8_t, DescendingSeqNumComp<uint16_t, kPicIdLength>> |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 186 | up_switch_ RTC_GUARDED_BY(crit_); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 187 | |
| 188 | // For every temporal layer, keep a set of which frames that are missing. |
| 189 | std::array<std::set<uint16_t, DescendingSeqNumComp<uint16_t, kPicIdLength>>, |
| 190 | kMaxTemporalLayers> |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 191 | missing_frames_for_layer_ RTC_GUARDED_BY(crit_); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 192 | |
philipel | 463d301 | 2016-09-09 03:32:44 -0700 | [diff] [blame] | 193 | // How far frames have been cleared by sequence number. A frame will be |
| 194 | // cleared if it contains a packet with a sequence number older than |
| 195 | // |cleared_to_seq_num_|. |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 196 | int cleared_to_seq_num_ RTC_GUARDED_BY(crit_); |
philipel | 463d301 | 2016-09-09 03:32:44 -0700 | [diff] [blame] | 197 | |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 198 | OnCompleteFrameCallback* frame_callback_; |
philipel | d4fac69 | 2017-09-04 07:03:46 -0700 | [diff] [blame] | 199 | |
philipel | dabfcae | 2018-09-25 12:54:37 +0200 | [diff] [blame] | 200 | SeqNumUnwrapper<uint16_t> generic_frame_id_unwrapper_ RTC_GUARDED_BY(crit_); |
| 201 | |
philipel | d4fac69 | 2017-09-04 07:03:46 -0700 | [diff] [blame] | 202 | // Unwrapper used to unwrap generic RTP streams. In a generic stream we derive |
| 203 | // a picture id from the packet sequence number. |
philipel | dabfcae | 2018-09-25 12:54:37 +0200 | [diff] [blame] | 204 | SeqNumUnwrapper<uint16_t> rtp_seq_num_unwrapper_ RTC_GUARDED_BY(crit_); |
philipel | d4fac69 | 2017-09-04 07:03:46 -0700 | [diff] [blame] | 205 | |
| 206 | // Unwrapper used to unwrap VP8/VP9 streams which have their picture id |
| 207 | // specified. |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 208 | SeqNumUnwrapper<uint16_t, kPicIdLength> unwrapper_ RTC_GUARDED_BY(crit_); |
philipel | 57ec685 | 2018-07-03 18:09:32 +0200 | [diff] [blame] | 209 | |
| 210 | SeqNumUnwrapper<uint8_t> tl0_unwrapper_ RTC_GUARDED_BY(crit_); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 211 | }; |
| 212 | |
| 213 | } // namespace video_coding |
| 214 | } // namespace webrtc |
| 215 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 216 | #endif // MODULES_VIDEO_CODING_RTP_FRAME_REFERENCE_FINDER_H_ |