blob: 501fcbc81a860977cd72cb31a5c622b60edb21a0 [file] [log] [blame]
philipel02447bc2016-05-13 06:01:03 -07001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_VIDEO_CODING_RTP_FRAME_REFERENCE_FINDER_H_
12#define MODULES_VIDEO_CODING_RTP_FRAME_REFERENCE_FINDER_H_
philipel02447bc2016-05-13 06:01:03 -070013
14#include <array>
Yves Gerey665174f2018-06-19 15:03:05 +020015#include <deque>
philipel02447bc2016-05-13 06:01:03 -070016#include <map>
kwibergfd8be342016-05-14 19:44:11 -070017#include <memory>
philipel02447bc2016-05-13 06:01:03 -070018#include <set>
19#include <utility>
20
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "modules/include/module_common_types.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_base/criticalsection.h"
Bjorn Tereliusa194e582017-10-25 13:07:09 +020023#include "rtc_base/numerics/sequence_number_util.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "rtc_base/thread_annotations.h"
philipel02447bc2016-05-13 06:01:03 -070025
26namespace webrtc {
27namespace video_coding {
28
philipele7c891f2018-02-22 14:35:06 +010029class EncodedFrame;
philipel02447bc2016-05-13 06:01:03 -070030class RtpFrameObject;
philipel17deeb42016-08-11 15:09:26 +020031
32// A complete frame is a frame which has received all its packets and all its
33// references are known.
34class OnCompleteFrameCallback {
35 public:
36 virtual ~OnCompleteFrameCallback() {}
philipele7c891f2018-02-22 14:35:06 +010037 virtual void OnCompleteFrame(std::unique_ptr<EncodedFrame> frame) = 0;
philipel17deeb42016-08-11 15:09:26 +020038};
philipel02447bc2016-05-13 06:01:03 -070039
40class RtpFrameReferenceFinder {
41 public:
42 explicit RtpFrameReferenceFinder(OnCompleteFrameCallback* frame_callback);
Mirko Bonadei8fdcac32018-08-28 16:30:18 +020043 ~RtpFrameReferenceFinder();
philipel463d3012016-09-09 03:32:44 -070044
45 // Manage this frame until:
46 // - We have all information needed to determine its references, after
47 // which |frame_callback_| is called with the completed frame, or
philipelafcf7f52017-04-26 08:17:35 -070048 // - We have too many stashed frames (determined by |kMaxStashedFrames|)
philipel463d3012016-09-09 03:32:44 -070049 // so we drop this frame, or
50 // - It gets cleared by ClearTo, which also means we drop it.
philipel02447bc2016-05-13 06:01:03 -070051 void ManageFrame(std::unique_ptr<RtpFrameObject> frame);
philipel463d3012016-09-09 03:32:44 -070052
53 // Notifies that padding has been received, which the reference finder
54 // might need to calculate the references of a frame.
philipel9b2ce6b2016-07-05 05:04:46 -070055 void PaddingReceived(uint16_t seq_num);
philipel02447bc2016-05-13 06:01:03 -070056
philipel463d3012016-09-09 03:32:44 -070057 // Clear all stashed frames that include packets older than |seq_num|.
58 void ClearTo(uint16_t seq_num);
59
philipel02447bc2016-05-13 06:01:03 -070060 private:
philipelfd5a20f2016-11-15 00:57:57 -080061 static const uint16_t kPicIdLength = 1 << 15;
philipel02447bc2016-05-13 06:01:03 -070062 static const uint8_t kMaxTemporalLayers = 5;
philipelfd5a20f2016-11-15 00:57:57 -080063 static const int kMaxLayerInfo = 50;
Sergey Silkin52233a32018-07-31 14:30:54 +020064 static const int kMaxStashedFrames = 100;
philipelfd5a20f2016-11-15 00:57:57 -080065 static const int kMaxNotYetReceivedFrames = 100;
66 static const int kMaxGofSaved = 50;
philipel9b2ce6b2016-07-05 05:04:46 -070067 static const int kMaxPaddingAge = 100;
philipel02447bc2016-05-13 06:01:03 -070068
philipelafcf7f52017-04-26 08:17:35 -070069 enum FrameDecision { kStash, kHandOff, kDrop };
philipelc9b27d52016-07-15 06:50:27 -070070
71 struct GofInfo {
72 GofInfo(GofInfoVP9* gof, uint16_t last_picture_id)
73 : gof(gof), last_picture_id(last_picture_id) {}
74 GofInfoVP9* gof;
75 uint16_t last_picture_id;
76 };
77
philipel02447bc2016-05-13 06:01:03 -070078 rtc::CriticalSection crit_;
79
philipel9b2ce6b2016-07-05 05:04:46 -070080 // Find the relevant group of pictures and update its "last-picture-id-with
81 // padding" sequence number.
82 void UpdateLastPictureIdWithPadding(uint16_t seq_num)
danilchap56359be2017-09-07 07:53:45 -070083 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipel9b2ce6b2016-07-05 05:04:46 -070084
philipelafcf7f52017-04-26 08:17:35 -070085 // Retry stashed frames until no more complete frames are found.
danilchap56359be2017-09-07 07:53:45 -070086 void RetryStashedFrames() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipel02447bc2016-05-13 06:01:03 -070087
philipelafcf7f52017-04-26 08:17:35 -070088 FrameDecision ManageFrameInternal(RtpFrameObject* frame)
danilchap56359be2017-09-07 07:53:45 -070089 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelafcf7f52017-04-26 08:17:35 -070090
philipel647998c2016-06-03 09:40:16 -070091 // Find references for generic frames. If |picture_id| is unspecified
92 // then packet sequence numbers will be used to determine the references
93 // of the frames.
philipelafcf7f52017-04-26 08:17:35 -070094 FrameDecision ManageFrameGeneric(RtpFrameObject* frame, int picture_id)
danilchap56359be2017-09-07 07:53:45 -070095 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipel02447bc2016-05-13 06:01:03 -070096
philipelafcf7f52017-04-26 08:17:35 -070097 // Find references for Vp8 frames
98 FrameDecision ManageFrameVp8(RtpFrameObject* frame)
danilchap56359be2017-09-07 07:53:45 -070099 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelafcf7f52017-04-26 08:17:35 -0700100
101 // Updates necessary layer info state used to determine frame references for
102 // Vp8.
philipel1610f942017-12-12 13:58:31 +0100103 void UpdateLayerInfoVp8(RtpFrameObject* frame,
philipel57ec6852018-07-03 18:09:32 +0200104 int64_t unwrapped_tl0,
105 uint8_t temporal_idx)
danilchap56359be2017-09-07 07:53:45 -0700106 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipel02447bc2016-05-13 06:01:03 -0700107
108 // Find references for Vp9 frames
philipelafcf7f52017-04-26 08:17:35 -0700109 FrameDecision ManageFrameVp9(RtpFrameObject* frame)
danilchap56359be2017-09-07 07:53:45 -0700110 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipel02447bc2016-05-13 06:01:03 -0700111
112 // Check if we are missing a frame necessary to determine the references
113 // for this frame.
philipelc9b27d52016-07-15 06:50:27 -0700114 bool MissingRequiredFrameVp9(uint16_t picture_id, const GofInfo& info)
danilchap56359be2017-09-07 07:53:45 -0700115 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipel02447bc2016-05-13 06:01:03 -0700116
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.
philipelc9b27d52016-07-15 06:50:27 -0700120 void FrameReceivedVp9(uint16_t picture_id, GofInfo* info)
danilchap56359be2017-09-07 07:53:45 -0700121 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipel02447bc2016-05-13 06:01:03 -0700122
123 // Check if there is a frame with the up-switch flag set in the interval
124 // (|pid_ref|, |picture_id|) with temporal layer smaller than |temporal_idx|.
125 bool UpSwitchInIntervalVp9(uint16_t picture_id,
126 uint8_t temporal_idx,
danilchap56359be2017-09-07 07:53:45 -0700127 uint16_t pid_ref)
128 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipel02447bc2016-05-13 06:01:03 -0700129
philipelafcf7f52017-04-26 08:17:35 -0700130 // Unwrap |frame|s picture id and its references to 16 bits.
danilchap56359be2017-09-07 07:53:45 -0700131 void UnwrapPictureIds(RtpFrameObject* frame)
132 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipel02447bc2016-05-13 06:01:03 -0700133
philipel9b2ce6b2016-07-05 05:04:46 -0700134 // For every group of pictures, hold two sequence numbers. The first being
135 // the sequence number of the last packet of the last completed frame, and
136 // the second being the sequence number of the last packet of the last
137 // completed frame advanced by any potential continuous packets of padding.
138 std::map<uint16_t,
139 std::pair<uint16_t, uint16_t>,
140 DescendingSeqNumComp<uint16_t>>
danilchap56359be2017-09-07 07:53:45 -0700141 last_seq_num_gop_ RTC_GUARDED_BY(crit_);
philipel02447bc2016-05-13 06:01:03 -0700142
143 // Save the last picture id in order to detect when there is a gap in frames
144 // that have not yet been fully received.
danilchap56359be2017-09-07 07:53:45 -0700145 int last_picture_id_ RTC_GUARDED_BY(crit_);
philipel02447bc2016-05-13 06:01:03 -0700146
philipel9b2ce6b2016-07-05 05:04:46 -0700147 // Padding packets that have been received but that are not yet continuous
148 // with any group of pictures.
149 std::set<uint16_t, DescendingSeqNumComp<uint16_t>> stashed_padding_
danilchap56359be2017-09-07 07:53:45 -0700150 RTC_GUARDED_BY(crit_);
philipel9b2ce6b2016-07-05 05:04:46 -0700151
philipel02447bc2016-05-13 06:01:03 -0700152 // Frames earlier than the last received frame that have not yet been
153 // fully received.
154 std::set<uint16_t, DescendingSeqNumComp<uint16_t, kPicIdLength>>
danilchap56359be2017-09-07 07:53:45 -0700155 not_yet_received_frames_ RTC_GUARDED_BY(crit_);
philipel02447bc2016-05-13 06:01:03 -0700156
157 // Frames that have been fully received but didn't have all the information
158 // needed to determine their references.
danilchap56359be2017-09-07 07:53:45 -0700159 std::deque<std::unique_ptr<RtpFrameObject>> stashed_frames_
160 RTC_GUARDED_BY(crit_);
philipel02447bc2016-05-13 06:01:03 -0700161
162 // Holds the information about the last completed frame for a given temporal
philipel57ec6852018-07-03 18:09:32 +0200163 // layer given an unwrapped Tl0 picture index.
164 std::map<int64_t, std::array<int16_t, kMaxTemporalLayers>> layer_info_
165 RTC_GUARDED_BY(crit_);
philipel02447bc2016-05-13 06:01:03 -0700166
167 // Where the current scalability structure is in the
168 // |scalability_structures_| array.
169 uint8_t current_ss_idx_;
170
171 // Holds received scalability structures.
172 std::array<GofInfoVP9, kMaxGofSaved> scalability_structures_
danilchap56359be2017-09-07 07:53:45 -0700173 RTC_GUARDED_BY(crit_);
philipel02447bc2016-05-13 06:01:03 -0700174
philipel57ec6852018-07-03 18:09:32 +0200175 // Holds the the Gof information for a given unwrapped TL0 picture index.
176 std::map<int64_t, GofInfo> gof_info_ RTC_GUARDED_BY(crit_);
philipel02447bc2016-05-13 06:01:03 -0700177
178 // Keep track of which picture id and which temporal layer that had the
179 // up switch flag set.
philipelc9b27d52016-07-15 06:50:27 -0700180 std::map<uint16_t, uint8_t, DescendingSeqNumComp<uint16_t, kPicIdLength>>
danilchap56359be2017-09-07 07:53:45 -0700181 up_switch_ RTC_GUARDED_BY(crit_);
philipel02447bc2016-05-13 06:01:03 -0700182
183 // For every temporal layer, keep a set of which frames that are missing.
184 std::array<std::set<uint16_t, DescendingSeqNumComp<uint16_t, kPicIdLength>>,
185 kMaxTemporalLayers>
danilchap56359be2017-09-07 07:53:45 -0700186 missing_frames_for_layer_ RTC_GUARDED_BY(crit_);
philipel02447bc2016-05-13 06:01:03 -0700187
philipel463d3012016-09-09 03:32:44 -0700188 // How far frames have been cleared by sequence number. A frame will be
189 // cleared if it contains a packet with a sequence number older than
190 // |cleared_to_seq_num_|.
danilchap56359be2017-09-07 07:53:45 -0700191 int cleared_to_seq_num_ RTC_GUARDED_BY(crit_);
philipel463d3012016-09-09 03:32:44 -0700192
philipel02447bc2016-05-13 06:01:03 -0700193 OnCompleteFrameCallback* frame_callback_;
philipeld4fac692017-09-04 07:03:46 -0700194
195 // Unwrapper used to unwrap generic RTP streams. In a generic stream we derive
196 // a picture id from the packet sequence number.
danilchap56359be2017-09-07 07:53:45 -0700197 SeqNumUnwrapper<uint16_t> generic_unwrapper_ RTC_GUARDED_BY(crit_);
philipeld4fac692017-09-04 07:03:46 -0700198
199 // Unwrapper used to unwrap VP8/VP9 streams which have their picture id
200 // specified.
danilchap56359be2017-09-07 07:53:45 -0700201 SeqNumUnwrapper<uint16_t, kPicIdLength> unwrapper_ RTC_GUARDED_BY(crit_);
philipel57ec6852018-07-03 18:09:32 +0200202
203 SeqNumUnwrapper<uint8_t> tl0_unwrapper_ RTC_GUARDED_BY(crit_);
philipel02447bc2016-05-13 06:01:03 -0700204};
205
206} // namespace video_coding
207} // namespace webrtc
208
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200209#endif // MODULES_VIDEO_CODING_RTP_FRAME_REFERENCE_FINDER_H_