rtp_frame_reference_finder.h 8.4 KB

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