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 11 #ifndef MODULES_VIDEO_CODING_RTP_FRAME_REFERENCE_FINDER_H_ 12 #define MODULES_VIDEO_CODING_RTP_FRAME_REFERENCE_FINDER_H_ 13 14 #include <array> 15 #include <deque> 16 #include <map> 17 #include <memory> 18 #include <set> 19 #include <utility> 20 21 #include "modules/include/module_common_types_public.h" 22 #include "modules/rtp_rtcp/source/rtp_video_header.h" 23 #include "modules/video_coding/codecs/vp9/include/vp9_globals.h" 24 #include "rtc_base/numerics/sequence_number_util.h" 25 #include "rtc_base/thread_annotations.h" 26 27 namespace webrtc { 28 namespace video_coding { 29 30 class EncodedFrame; 31 class RtpFrameObject; 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: ~OnCompleteFrameCallback()37 virtual ~OnCompleteFrameCallback() {} 38 virtual void OnCompleteFrame(std::unique_ptr<EncodedFrame> frame) = 0; 39 }; 40 41 class RtpFrameReferenceFinder { 42 public: 43 explicit RtpFrameReferenceFinder(OnCompleteFrameCallback* frame_callback); 44 explicit RtpFrameReferenceFinder(OnCompleteFrameCallback* frame_callback, 45 int64_t picture_id_offset); 46 ~RtpFrameReferenceFinder(); 47 48 // Manage this frame until: 49 // - We have all information needed to determine its references, after 50 // which |frame_callback_| is called with the completed frame, or 51 // - We have too many stashed frames (determined by |kMaxStashedFrames|) 52 // so we drop this frame, or 53 // - It gets cleared by ClearTo, which also means we drop it. 54 void ManageFrame(std::unique_ptr<RtpFrameObject> frame); 55 56 // Notifies that padding has been received, which the reference finder 57 // might need to calculate the references of a frame. 58 void PaddingReceived(uint16_t seq_num); 59 60 // Clear all stashed frames that include packets older than |seq_num|. 61 void ClearTo(uint16_t seq_num); 62 63 private: 64 static const uint16_t kPicIdLength = 1 << 15; 65 static const uint8_t kMaxTemporalLayers = 5; 66 static const int kMaxLayerInfo = 50; 67 static const int kMaxStashedFrames = 100; 68 static const int kMaxNotYetReceivedFrames = 100; 69 static const int kMaxGofSaved = 50; 70 static const int kMaxPaddingAge = 100; 71 72 enum FrameDecision { kStash, kHandOff, kDrop }; 73 74 struct GofInfo { GofInfoGofInfo75 GofInfo(GofInfoVP9* gof, uint16_t last_picture_id) 76 : gof(gof), last_picture_id(last_picture_id) {} 77 GofInfoVP9* gof; 78 uint16_t last_picture_id; 79 }; 80 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); 84 85 // Retry stashed frames until no more complete frames are found. 86 void RetryStashedFrames(); 87 88 void HandOffFrame(std::unique_ptr<RtpFrameObject> frame); 89 90 FrameDecision ManageFrameInternal(RtpFrameObject* frame); 91 92 FrameDecision ManageFrameGeneric( 93 RtpFrameObject* frame, 94 const RTPVideoHeader::GenericDescriptorInfo& descriptor); 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); 100 101 // Find references for Vp8 frames 102 FrameDecision ManageFrameVp8(RtpFrameObject* frame); 103 104 // Updates necessary layer info state used to determine frame references for 105 // Vp8. 106 void UpdateLayerInfoVp8(RtpFrameObject* frame, 107 int64_t unwrapped_tl0, 108 uint8_t temporal_idx); 109 110 // Find references for Vp9 frames 111 FrameDecision ManageFrameVp9(RtpFrameObject* frame); 112 113 // Check if we are missing a frame necessary to determine the references 114 // for this frame. 115 bool MissingRequiredFrameVp9(uint16_t picture_id, const GofInfo& info); 116 117 // Updates which frames that have been received. If there is a gap, 118 // missing frames will be added to |missing_frames_for_layer_| or 119 // if this is an already missing frame then it will be removed. 120 void FrameReceivedVp9(uint16_t picture_id, GofInfo* info); 121 122 // Check if there is a frame with the up-switch flag set in the interval 123 // (|pid_ref|, |picture_id|) with temporal layer smaller than |temporal_idx|. 124 bool UpSwitchInIntervalVp9(uint16_t picture_id, 125 uint8_t temporal_idx, 126 uint16_t pid_ref); 127 128 // Unwrap |frame|s picture id and its references to 16 bits. 129 void UnwrapPictureIds(RtpFrameObject* frame); 130 131 // Find references for H264 frames 132 FrameDecision ManageFrameH264(RtpFrameObject* frame); 133 134 // Update "last-picture-id-with-padding" sequence number for H264. 135 void UpdateLastPictureIdWithPaddingH264(); 136 137 // Update H264 layer info state used to determine frame references. 138 void UpdateLayerInfoH264(RtpFrameObject* frame, 139 int64_t unwrapped_tl0, 140 uint8_t temporal_idx); 141 142 // Update H264 state for decodeable frames. 143 void UpdateDataH264(RtpFrameObject* frame, 144 int64_t unwrapped_tl0, 145 uint8_t temporal_idx); 146 147 // For every group of pictures, hold two sequence numbers. The first being 148 // the sequence number of the last packet of the last completed frame, and 149 // the second being the sequence number of the last packet of the last 150 // completed frame advanced by any potential continuous packets of padding. 151 std::map<uint16_t, 152 std::pair<uint16_t, uint16_t>, 153 DescendingSeqNumComp<uint16_t>> 154 last_seq_num_gop_; 155 156 // Save the last picture id in order to detect when there is a gap in frames 157 // that have not yet been fully received. 158 int last_picture_id_; 159 160 // Padding packets that have been received but that are not yet continuous 161 // with any group of pictures. 162 std::set<uint16_t, DescendingSeqNumComp<uint16_t>> stashed_padding_; 163 164 // Frames earlier than the last received frame that have not yet been 165 // fully received. 166 std::set<uint16_t, DescendingSeqNumComp<uint16_t, kPicIdLength>> 167 not_yet_received_frames_; 168 169 // Sequence numbers of frames earlier than the last received frame that 170 // have not yet been fully received. 171 std::set<uint16_t, DescendingSeqNumComp<uint16_t>> not_yet_received_seq_num_; 172 173 // Frames that have been fully received but didn't have all the information 174 // needed to determine their references. 175 std::deque<std::unique_ptr<RtpFrameObject>> stashed_frames_; 176 177 // Holds the information about the last completed frame for a given temporal 178 // layer given an unwrapped Tl0 picture index. 179 std::map<int64_t, std::array<int64_t, kMaxTemporalLayers>> layer_info_; 180 181 // Where the current scalability structure is in the 182 // |scalability_structures_| array. 183 uint8_t current_ss_idx_; 184 185 // Holds received scalability structures. 186 std::array<GofInfoVP9, kMaxGofSaved> scalability_structures_; 187 188 // Holds the the Gof information for a given unwrapped TL0 picture index. 189 std::map<int64_t, GofInfo> gof_info_; 190 191 // Keep track of which picture id and which temporal layer that had the 192 // up switch flag set. 193 std::map<uint16_t, uint8_t, DescendingSeqNumComp<uint16_t, kPicIdLength>> 194 up_switch_; 195 196 // For every temporal layer, keep a set of which frames that are missing. 197 std::array<std::set<uint16_t, DescendingSeqNumComp<uint16_t, kPicIdLength>>, 198 kMaxTemporalLayers> 199 missing_frames_for_layer_; 200 201 // How far frames have been cleared by sequence number. A frame will be 202 // cleared if it contains a packet with a sequence number older than 203 // |cleared_to_seq_num_|. 204 int cleared_to_seq_num_; 205 206 OnCompleteFrameCallback* frame_callback_; 207 208 // Unwrapper used to unwrap generic RTP streams. In a generic stream we derive 209 // a picture id from the packet sequence number. 210 SeqNumUnwrapper<uint16_t> rtp_seq_num_unwrapper_; 211 212 // Unwrapper used to unwrap VP8/VP9 streams which have their picture id 213 // specified. 214 SeqNumUnwrapper<uint16_t, kPicIdLength> unwrapper_; 215 216 SeqNumUnwrapper<uint8_t> tl0_unwrapper_; 217 218 const int64_t picture_id_offset_; 219 }; 220 221 } // namespace video_coding 222 } // namespace webrtc 223 224 #endif // MODULES_VIDEO_CODING_RTP_FRAME_REFERENCE_FINDER_H_ 225