blob: 95da4f327e64f66e0fbe8f2e657480742befaadc [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>
15#include <map>
kwibergfd8be342016-05-14 19:44:11 -070016#include <memory>
philipel463d3012016-09-09 03:32:44 -070017#include <deque>
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
philipel17deeb42016-08-11 15:09:26 +020029class FrameObject;
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() {}
37 virtual void OnCompleteFrame(std::unique_ptr<FrameObject> frame) = 0;
38};
philipel02447bc2016-05-13 06:01:03 -070039
40class RtpFrameReferenceFinder {
41 public:
42 explicit RtpFrameReferenceFinder(OnCompleteFrameCallback* frame_callback);
philipel463d3012016-09-09 03:32:44 -070043
44 // Manage this frame until:
45 // - We have all information needed to determine its references, after
46 // which |frame_callback_| is called with the completed frame, or
philipelafcf7f52017-04-26 08:17:35 -070047 // - We have too many stashed frames (determined by |kMaxStashedFrames|)
philipel463d3012016-09-09 03:32:44 -070048 // so we drop this frame, or
49 // - It gets cleared by ClearTo, which also means we drop it.
philipel02447bc2016-05-13 06:01:03 -070050 void ManageFrame(std::unique_ptr<RtpFrameObject> frame);
philipel463d3012016-09-09 03:32:44 -070051
52 // Notifies that padding has been received, which the reference finder
53 // might need to calculate the references of a frame.
philipel9b2ce6b2016-07-05 05:04:46 -070054 void PaddingReceived(uint16_t seq_num);
philipel02447bc2016-05-13 06:01:03 -070055
philipel463d3012016-09-09 03:32:44 -070056 // Clear all stashed frames that include packets older than |seq_num|.
57 void ClearTo(uint16_t seq_num);
58
philipel02447bc2016-05-13 06:01:03 -070059 private:
philipelfd5a20f2016-11-15 00:57:57 -080060 static const uint16_t kPicIdLength = 1 << 15;
philipel02447bc2016-05-13 06:01:03 -070061 static const uint8_t kMaxTemporalLayers = 5;
philipelfd5a20f2016-11-15 00:57:57 -080062 static const int kMaxLayerInfo = 50;
63 static const int kMaxStashedFrames = 50;
64 static const int kMaxNotYetReceivedFrames = 100;
65 static const int kMaxGofSaved = 50;
philipel9b2ce6b2016-07-05 05:04:46 -070066 static const int kMaxPaddingAge = 100;
philipel02447bc2016-05-13 06:01:03 -070067
philipelafcf7f52017-04-26 08:17:35 -070068 enum FrameDecision { kStash, kHandOff, kDrop };
philipelc9b27d52016-07-15 06:50:27 -070069
70 struct GofInfo {
71 GofInfo(GofInfoVP9* gof, uint16_t last_picture_id)
72 : gof(gof), last_picture_id(last_picture_id) {}
73 GofInfoVP9* gof;
74 uint16_t last_picture_id;
75 };
76
philipel02447bc2016-05-13 06:01:03 -070077 rtc::CriticalSection crit_;
78
philipel9b2ce6b2016-07-05 05:04:46 -070079 // Find the relevant group of pictures and update its "last-picture-id-with
80 // padding" sequence number.
81 void UpdateLastPictureIdWithPadding(uint16_t seq_num)
danilchap56359be2017-09-07 07:53:45 -070082 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipel9b2ce6b2016-07-05 05:04:46 -070083
philipelafcf7f52017-04-26 08:17:35 -070084 // Retry stashed frames until no more complete frames are found.
danilchap56359be2017-09-07 07:53:45 -070085 void RetryStashedFrames() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipel02447bc2016-05-13 06:01:03 -070086
philipelafcf7f52017-04-26 08:17:35 -070087 FrameDecision ManageFrameInternal(RtpFrameObject* frame)
danilchap56359be2017-09-07 07:53:45 -070088 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelafcf7f52017-04-26 08:17:35 -070089
philipel647998c2016-06-03 09:40:16 -070090 // Find references for generic frames. If |picture_id| is unspecified
91 // then packet sequence numbers will be used to determine the references
92 // of the frames.
philipelafcf7f52017-04-26 08:17:35 -070093 FrameDecision ManageFrameGeneric(RtpFrameObject* frame, int picture_id)
danilchap56359be2017-09-07 07:53:45 -070094 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipel02447bc2016-05-13 06:01:03 -070095
philipelafcf7f52017-04-26 08:17:35 -070096 // Find references for Vp8 frames
97 FrameDecision ManageFrameVp8(RtpFrameObject* frame)
danilchap56359be2017-09-07 07:53:45 -070098 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelafcf7f52017-04-26 08:17:35 -070099
100 // Updates necessary layer info state used to determine frame references for
101 // Vp8.
philipel1610f942017-12-12 13:58:31 +0100102 void UpdateLayerInfoVp8(RtpFrameObject* frame,
103 const RTPVideoHeaderVP8& codec_header)
danilchap56359be2017-09-07 07:53:45 -0700104 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipel02447bc2016-05-13 06:01:03 -0700105
106 // Find references for Vp9 frames
philipelafcf7f52017-04-26 08:17:35 -0700107 FrameDecision ManageFrameVp9(RtpFrameObject* frame)
danilchap56359be2017-09-07 07:53:45 -0700108 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipel02447bc2016-05-13 06:01:03 -0700109
110 // Check if we are missing a frame necessary to determine the references
111 // for this frame.
philipelc9b27d52016-07-15 06:50:27 -0700112 bool MissingRequiredFrameVp9(uint16_t picture_id, const GofInfo& info)
danilchap56359be2017-09-07 07:53:45 -0700113 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipel02447bc2016-05-13 06:01:03 -0700114
115 // Updates which frames that have been received. If there is a gap,
116 // missing frames will be added to |missing_frames_for_layer_| or
117 // if this is an already missing frame then it will be removed.
philipelc9b27d52016-07-15 06:50:27 -0700118 void FrameReceivedVp9(uint16_t picture_id, GofInfo* info)
danilchap56359be2017-09-07 07:53:45 -0700119 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipel02447bc2016-05-13 06:01:03 -0700120
121 // Check if there is a frame with the up-switch flag set in the interval
122 // (|pid_ref|, |picture_id|) with temporal layer smaller than |temporal_idx|.
123 bool UpSwitchInIntervalVp9(uint16_t picture_id,
124 uint8_t temporal_idx,
danilchap56359be2017-09-07 07:53:45 -0700125 uint16_t pid_ref)
126 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipel02447bc2016-05-13 06:01:03 -0700127
philipelafcf7f52017-04-26 08:17:35 -0700128 // Unwrap |frame|s picture id and its references to 16 bits.
danilchap56359be2017-09-07 07:53:45 -0700129 void UnwrapPictureIds(RtpFrameObject* frame)
130 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipel02447bc2016-05-13 06:01:03 -0700131
philipelfd5a20f2016-11-15 00:57:57 -0800132 // Returns true if the frame is old and should be dropped.
133 // TODO(philipel): Remove when VP9 PID/TL0 does not jump mid-stream (should be
134 // around M59).
135 bool Vp9PidTl0Fix(const RtpFrameObject& frame,
136 int16_t* picture_id,
danilchap56359be2017-09-07 07:53:45 -0700137 int16_t* tl0_pic_idx) RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelfd5a20f2016-11-15 00:57:57 -0800138
139 // TODO(philipel): Remove when VP9 PID/TL0 does not jump mid-stream (should be
140 // around M59).
141 bool DetectVp9PicIdJump(int fixed_pid,
142 int fixed_tl0,
143 uint32_t timestamp) const
danilchap56359be2017-09-07 07:53:45 -0700144 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipelfd5a20f2016-11-15 00:57:57 -0800145
146 // TODO(philipel): Remove when VP9 PID/TL0 does not jump mid-stream (should be
147 // around M59).
148 bool DetectVp9Tl0PicIdxJump(int fixed_tl0, uint32_t timestamp) const
danilchap56359be2017-09-07 07:53:45 -0700149 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
philipel9b2ce6b2016-07-05 05:04:46 -0700150
151 // For every group of pictures, hold two sequence numbers. The first being
152 // the sequence number of the last packet of the last completed frame, and
153 // the second being the sequence number of the last packet of the last
154 // completed frame advanced by any potential continuous packets of padding.
155 std::map<uint16_t,
156 std::pair<uint16_t, uint16_t>,
157 DescendingSeqNumComp<uint16_t>>
danilchap56359be2017-09-07 07:53:45 -0700158 last_seq_num_gop_ RTC_GUARDED_BY(crit_);
philipel02447bc2016-05-13 06:01:03 -0700159
160 // Save the last picture id in order to detect when there is a gap in frames
161 // that have not yet been fully received.
danilchap56359be2017-09-07 07:53:45 -0700162 int last_picture_id_ RTC_GUARDED_BY(crit_);
philipel02447bc2016-05-13 06:01:03 -0700163
philipel9b2ce6b2016-07-05 05:04:46 -0700164 // Padding packets that have been received but that are not yet continuous
165 // with any group of pictures.
166 std::set<uint16_t, DescendingSeqNumComp<uint16_t>> stashed_padding_
danilchap56359be2017-09-07 07:53:45 -0700167 RTC_GUARDED_BY(crit_);
philipel9b2ce6b2016-07-05 05:04:46 -0700168
philipel02447bc2016-05-13 06:01:03 -0700169 // The last unwrapped picture id. Used to unwrap the picture id from a length
170 // of |kPicIdLength| to 16 bits.
danilchap56359be2017-09-07 07:53:45 -0700171 int last_unwrap_ RTC_GUARDED_BY(crit_);
philipel02447bc2016-05-13 06:01:03 -0700172
173 // Frames earlier than the last received frame that have not yet been
174 // fully received.
175 std::set<uint16_t, DescendingSeqNumComp<uint16_t, kPicIdLength>>
danilchap56359be2017-09-07 07:53:45 -0700176 not_yet_received_frames_ RTC_GUARDED_BY(crit_);
philipel02447bc2016-05-13 06:01:03 -0700177
178 // Frames that have been fully received but didn't have all the information
179 // needed to determine their references.
danilchap56359be2017-09-07 07:53:45 -0700180 std::deque<std::unique_ptr<RtpFrameObject>> stashed_frames_
181 RTC_GUARDED_BY(crit_);
philipel02447bc2016-05-13 06:01:03 -0700182
183 // Holds the information about the last completed frame for a given temporal
184 // layer given a Tl0 picture index.
185 std::map<uint8_t,
186 std::array<int16_t, kMaxTemporalLayers>,
187 DescendingSeqNumComp<uint8_t>>
danilchap56359be2017-09-07 07:53:45 -0700188 layer_info_ RTC_GUARDED_BY(crit_);
philipel02447bc2016-05-13 06:01:03 -0700189
190 // Where the current scalability structure is in the
191 // |scalability_structures_| array.
192 uint8_t current_ss_idx_;
193
194 // Holds received scalability structures.
195 std::array<GofInfoVP9, kMaxGofSaved> scalability_structures_
danilchap56359be2017-09-07 07:53:45 -0700196 RTC_GUARDED_BY(crit_);
philipel02447bc2016-05-13 06:01:03 -0700197
philipelc9b27d52016-07-15 06:50:27 -0700198 // Holds the the Gof information for a given TL0 picture index.
199 std::map<uint8_t, GofInfo, DescendingSeqNumComp<uint8_t>> gof_info_
danilchap56359be2017-09-07 07:53:45 -0700200 RTC_GUARDED_BY(crit_);
philipel02447bc2016-05-13 06:01:03 -0700201
202 // Keep track of which picture id and which temporal layer that had the
203 // up switch flag set.
philipelc9b27d52016-07-15 06:50:27 -0700204 std::map<uint16_t, uint8_t, DescendingSeqNumComp<uint16_t, kPicIdLength>>
danilchap56359be2017-09-07 07:53:45 -0700205 up_switch_ RTC_GUARDED_BY(crit_);
philipel02447bc2016-05-13 06:01:03 -0700206
207 // For every temporal layer, keep a set of which frames that are missing.
208 std::array<std::set<uint16_t, DescendingSeqNumComp<uint16_t, kPicIdLength>>,
209 kMaxTemporalLayers>
danilchap56359be2017-09-07 07:53:45 -0700210 missing_frames_for_layer_ RTC_GUARDED_BY(crit_);
philipel02447bc2016-05-13 06:01:03 -0700211
philipel463d3012016-09-09 03:32:44 -0700212 // How far frames have been cleared by sequence number. A frame will be
213 // cleared if it contains a packet with a sequence number older than
214 // |cleared_to_seq_num_|.
danilchap56359be2017-09-07 07:53:45 -0700215 int cleared_to_seq_num_ RTC_GUARDED_BY(crit_);
philipel463d3012016-09-09 03:32:44 -0700216
philipel02447bc2016-05-13 06:01:03 -0700217 OnCompleteFrameCallback* frame_callback_;
philipeld4fac692017-09-04 07:03:46 -0700218
219 // Unwrapper used to unwrap generic RTP streams. In a generic stream we derive
220 // a picture id from the packet sequence number.
danilchap56359be2017-09-07 07:53:45 -0700221 SeqNumUnwrapper<uint16_t> generic_unwrapper_ RTC_GUARDED_BY(crit_);
philipeld4fac692017-09-04 07:03:46 -0700222
223 // Unwrapper used to unwrap VP8/VP9 streams which have their picture id
224 // specified.
danilchap56359be2017-09-07 07:53:45 -0700225 SeqNumUnwrapper<uint16_t, kPicIdLength> unwrapper_ RTC_GUARDED_BY(crit_);
philipel02447bc2016-05-13 06:01:03 -0700226};
227
228} // namespace video_coding
229} // namespace webrtc
230
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200231#endif // MODULES_VIDEO_CODING_RTP_FRAME_REFERENCE_FINDER_H_