blob: 0f792183f5806a2efa482b2a0e036158bbf75951 [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#include "modules/video_coding/rtp_frame_reference_finder.h"
philipel02447bc2016-05-13 06:01:03 -070012
13#include <algorithm>
14#include <limits>
15
philipel1a4746a2018-07-09 15:52:29 +020016#include "absl/types/variant.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "modules/video_coding/frame_object.h"
18#include "modules/video_coding/packet_buffer.h"
19#include "rtc_base/checks.h"
20#include "rtc_base/logging.h"
Karl Wiberg80ba3332018-02-05 10:33:35 +010021#include "rtc_base/system/fallthrough.h"
philipel02447bc2016-05-13 06:01:03 -070022
23namespace webrtc {
24namespace video_coding {
25
26RtpFrameReferenceFinder::RtpFrameReferenceFinder(
27 OnCompleteFrameCallback* frame_callback)
28 : last_picture_id_(-1),
philipel02447bc2016-05-13 06:01:03 -070029 current_ss_idx_(0),
philipel463d3012016-09-09 03:32:44 -070030 cleared_to_seq_num_(-1),
philipel02447bc2016-05-13 06:01:03 -070031 frame_callback_(frame_callback) {}
32
Mirko Bonadei8fdcac32018-08-28 16:30:18 +020033RtpFrameReferenceFinder::~RtpFrameReferenceFinder() = default;
34
philipel02447bc2016-05-13 06:01:03 -070035void RtpFrameReferenceFinder::ManageFrame(
36 std::unique_ptr<RtpFrameObject> frame) {
37 rtc::CritScope lock(&crit_);
philipel463d3012016-09-09 03:32:44 -070038
39 // If we have cleared past this frame, drop it.
40 if (cleared_to_seq_num_ != -1 &&
41 AheadOf<uint16_t>(cleared_to_seq_num_, frame->first_seq_num())) {
42 return;
43 }
44
philipelafcf7f52017-04-26 08:17:35 -070045 FrameDecision decision = ManageFrameInternal(frame.get());
46
47 switch (decision) {
48 case kStash:
49 if (stashed_frames_.size() > kMaxStashedFrames)
50 stashed_frames_.pop_back();
51 stashed_frames_.push_front(std::move(frame));
52 break;
53 case kHandOff:
54 frame_callback_->OnCompleteFrame(std::move(frame));
55 RetryStashedFrames();
56 break;
57 case kDrop:
58 break;
59 }
60}
61
62void RtpFrameReferenceFinder::RetryStashedFrames() {
63 bool complete_frame = false;
64 do {
65 complete_frame = false;
66 for (auto frame_it = stashed_frames_.begin();
67 frame_it != stashed_frames_.end();) {
68 FrameDecision decision = ManageFrameInternal(frame_it->get());
69
70 switch (decision) {
71 case kStash:
72 ++frame_it;
73 break;
74 case kHandOff:
75 complete_frame = true;
76 frame_callback_->OnCompleteFrame(std::move(*frame_it));
Karl Wiberg80ba3332018-02-05 10:33:35 +010077 RTC_FALLTHROUGH();
philipelafcf7f52017-04-26 08:17:35 -070078 case kDrop:
79 frame_it = stashed_frames_.erase(frame_it);
80 }
81 }
82 } while (complete_frame);
83}
84
85RtpFrameReferenceFinder::FrameDecision
86RtpFrameReferenceFinder::ManageFrameInternal(RtpFrameObject* frame) {
philipel2837edc2018-10-02 13:55:47 +020087 absl::optional<RtpGenericFrameDescriptor> generic_descriptor =
88 frame->GetGenericFrameDescriptor();
89 if (generic_descriptor) {
90 return ManageFrameGeneric(frame, *generic_descriptor);
philipeldabfcae2018-09-25 12:54:37 +020091 }
92
philipel02447bc2016-05-13 06:01:03 -070093 switch (frame->codec_type()) {
philipel02447bc2016-05-13 06:01:03 -070094 case kVideoCodecVP8:
philipelafcf7f52017-04-26 08:17:35 -070095 return ManageFrameVp8(frame);
philipel02447bc2016-05-13 06:01:03 -070096 case kVideoCodecVP9:
philipelafcf7f52017-04-26 08:17:35 -070097 return ManageFrameVp9(frame);
Johnny Leebc7f41b2019-05-01 14:41:32 -040098 case kVideoCodecH264:
99 return ManageFrameH264(frame);
Sami Kalliomäki98824952018-08-28 14:39:21 +0200100 default: {
101 // Use 15 first bits of frame ID as picture ID if available.
Johannes Krona3705562019-08-26 16:37:11 +0200102 const RTPVideoHeader& video_header = frame->GetRtpVideoHeader();
philipeldabfcae2018-09-25 12:54:37 +0200103 int picture_id = kNoPictureId;
Johannes Krona3705562019-08-26 16:37:11 +0200104 if (video_header.generic)
105 picture_id = video_header.generic->frame_id & 0x7fff;
philipeldabfcae2018-09-25 12:54:37 +0200106
107 return ManageFramePidOrSeqNum(frame, picture_id);
Sami Kalliomäki98824952018-08-28 14:39:21 +0200108 }
philipel02447bc2016-05-13 06:01:03 -0700109 }
110}
111
philipel9b2ce6b2016-07-05 05:04:46 -0700112void RtpFrameReferenceFinder::PaddingReceived(uint16_t seq_num) {
113 rtc::CritScope lock(&crit_);
114 auto clean_padding_to =
115 stashed_padding_.lower_bound(seq_num - kMaxPaddingAge);
116 stashed_padding_.erase(stashed_padding_.begin(), clean_padding_to);
117 stashed_padding_.insert(seq_num);
118 UpdateLastPictureIdWithPadding(seq_num);
119 RetryStashedFrames();
120}
121
philipel463d3012016-09-09 03:32:44 -0700122void RtpFrameReferenceFinder::ClearTo(uint16_t seq_num) {
123 rtc::CritScope lock(&crit_);
124 cleared_to_seq_num_ = seq_num;
125
126 auto it = stashed_frames_.begin();
127 while (it != stashed_frames_.end()) {
128 if (AheadOf<uint16_t>(cleared_to_seq_num_, (*it)->first_seq_num())) {
129 it = stashed_frames_.erase(it);
130 } else {
131 ++it;
132 }
133 }
134}
135
philipel9b2ce6b2016-07-05 05:04:46 -0700136void RtpFrameReferenceFinder::UpdateLastPictureIdWithPadding(uint16_t seq_num) {
137 auto gop_seq_num_it = last_seq_num_gop_.upper_bound(seq_num);
138
139 // If this padding packet "belongs" to a group of pictures that we don't track
140 // anymore, do nothing.
141 if (gop_seq_num_it == last_seq_num_gop_.begin())
142 return;
143 --gop_seq_num_it;
144
145 // Calculate the next contiuous sequence number and search for it in
146 // the padding packets we have stashed.
147 uint16_t next_seq_num_with_padding = gop_seq_num_it->second.second + 1;
148 auto padding_seq_num_it =
149 stashed_padding_.lower_bound(next_seq_num_with_padding);
150
151 // While there still are padding packets and those padding packets are
152 // continuous, then advance the "last-picture-id-with-padding" and remove
153 // the stashed padding packet.
154 while (padding_seq_num_it != stashed_padding_.end() &&
155 *padding_seq_num_it == next_seq_num_with_padding) {
156 gop_seq_num_it->second.second = next_seq_num_with_padding;
157 ++next_seq_num_with_padding;
158 padding_seq_num_it = stashed_padding_.erase(padding_seq_num_it);
159 }
philipel41bb7922017-02-20 07:53:23 -0800160
161 // In the case where the stream has been continuous without any new keyframes
162 // for a while there is a risk that new frames will appear to be older than
163 // the keyframe they belong to due to wrapping sequence number. In order
164 // to prevent this we advance the picture id of the keyframe every so often.
165 if (ForwardDiff(gop_seq_num_it->first, seq_num) > 10000) {
166 RTC_DCHECK_EQ(1ul, last_seq_num_gop_.size());
167 last_seq_num_gop_[seq_num] = gop_seq_num_it->second;
168 last_seq_num_gop_.erase(gop_seq_num_it);
169 }
philipel9b2ce6b2016-07-05 05:04:46 -0700170}
171
philipelafcf7f52017-04-26 08:17:35 -0700172RtpFrameReferenceFinder::FrameDecision
philipeldabfcae2018-09-25 12:54:37 +0200173RtpFrameReferenceFinder::ManageFrameGeneric(
174 RtpFrameObject* frame,
philipel2837edc2018-10-02 13:55:47 +0200175 const RtpGenericFrameDescriptor& descriptor) {
176 int64_t frame_id = generic_frame_id_unwrapper_.Unwrap(descriptor.FrameId());
177 frame->id.picture_id = frame_id;
178 frame->id.spatial_layer = descriptor.SpatialLayer();
179
180 rtc::ArrayView<const uint16_t> diffs = descriptor.FrameDependenciesDiffs();
181 if (EncodedFrame::kMaxFrameReferences < diffs.size()) {
philipeldabfcae2018-09-25 12:54:37 +0200182 RTC_LOG(LS_WARNING) << "Too many dependencies in generic descriptor.";
183 return kDrop;
184 }
185
philipel2837edc2018-10-02 13:55:47 +0200186 frame->num_references = diffs.size();
187 for (size_t i = 0; i < diffs.size(); ++i)
188 frame->references[i] = frame_id - diffs[i];
philipeldabfcae2018-09-25 12:54:37 +0200189
190 return kHandOff;
191}
192
193RtpFrameReferenceFinder::FrameDecision
194RtpFrameReferenceFinder::ManageFramePidOrSeqNum(RtpFrameObject* frame,
195 int picture_id) {
philipel647998c2016-06-03 09:40:16 -0700196 // If |picture_id| is specified then we use that to set the frame references,
197 // otherwise we use sequence number.
198 if (picture_id != kNoPictureId) {
philipel0fa82a62018-03-19 15:34:53 +0100199 frame->id.picture_id = unwrapper_.Unwrap(picture_id);
Niels Möller8f7ce222019-03-21 15:43:58 +0100200 frame->num_references =
201 frame->frame_type() == VideoFrameType::kVideoFrameKey ? 0 : 1;
philipel0fa82a62018-03-19 15:34:53 +0100202 frame->references[0] = frame->id.picture_id - 1;
philipelafcf7f52017-04-26 08:17:35 -0700203 return kHandOff;
philipel647998c2016-06-03 09:40:16 -0700204 }
205
Niels Möller8f7ce222019-03-21 15:43:58 +0100206 if (frame->frame_type() == VideoFrameType::kVideoFrameKey) {
philipel9b2ce6b2016-07-05 05:04:46 -0700207 last_seq_num_gop_.insert(std::make_pair(
208 frame->last_seq_num(),
209 std::make_pair(frame->last_seq_num(), frame->last_seq_num())));
210 }
philipel02447bc2016-05-13 06:01:03 -0700211
212 // We have received a frame but not yet a keyframe, stash this frame.
philipelafcf7f52017-04-26 08:17:35 -0700213 if (last_seq_num_gop_.empty())
214 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700215
216 // Clean up info for old keyframes but make sure to keep info
217 // for the last keyframe.
218 auto clean_to = last_seq_num_gop_.lower_bound(frame->last_seq_num() - 100);
philipel41bb7922017-02-20 07:53:23 -0800219 for (auto it = last_seq_num_gop_.begin();
220 it != clean_to && last_seq_num_gop_.size() > 1;) {
221 it = last_seq_num_gop_.erase(it);
222 }
philipel02447bc2016-05-13 06:01:03 -0700223
224 // Find the last sequence number of the last frame for the keyframe
225 // that this frame indirectly references.
226 auto seq_num_it = last_seq_num_gop_.upper_bound(frame->last_seq_num());
philipel9b2ce6b2016-07-05 05:04:46 -0700227 if (seq_num_it == last_seq_num_gop_.begin()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100228 RTC_LOG(LS_WARNING) << "Generic frame with packet range ["
229 << frame->first_seq_num() << ", "
230 << frame->last_seq_num()
231 << "] has no GoP, dropping frame.";
philipelafcf7f52017-04-26 08:17:35 -0700232 return kDrop;
philipel9b2ce6b2016-07-05 05:04:46 -0700233 }
philipel02447bc2016-05-13 06:01:03 -0700234 seq_num_it--;
235
236 // Make sure the packet sequence numbers are continuous, otherwise stash
237 // this frame.
philipel9b2ce6b2016-07-05 05:04:46 -0700238 uint16_t last_picture_id_gop = seq_num_it->second.first;
239 uint16_t last_picture_id_with_padding_gop = seq_num_it->second.second;
Niels Möller8f7ce222019-03-21 15:43:58 +0100240 if (frame->frame_type() == VideoFrameType::kVideoFrameDelta) {
philipel9b2ce6b2016-07-05 05:04:46 -0700241 uint16_t prev_seq_num = frame->first_seq_num() - 1;
philipelafcf7f52017-04-26 08:17:35 -0700242
243 if (prev_seq_num != last_picture_id_with_padding_gop)
244 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700245 }
246
247 RTC_DCHECK(AheadOrAt(frame->last_seq_num(), seq_num_it->first));
248
249 // Since keyframes can cause reordering we can't simply assign the
250 // picture id according to some incrementing counter.
philipel0fa82a62018-03-19 15:34:53 +0100251 frame->id.picture_id = frame->last_seq_num();
Niels Möller8f7ce222019-03-21 15:43:58 +0100252 frame->num_references =
253 frame->frame_type() == VideoFrameType::kVideoFrameDelta;
philipeldabfcae2018-09-25 12:54:37 +0200254 frame->references[0] = rtp_seq_num_unwrapper_.Unwrap(last_picture_id_gop);
philipel0fa82a62018-03-19 15:34:53 +0100255 if (AheadOf<uint16_t>(frame->id.picture_id, last_picture_id_gop)) {
256 seq_num_it->second.first = frame->id.picture_id;
257 seq_num_it->second.second = frame->id.picture_id;
philipel9b2ce6b2016-07-05 05:04:46 -0700258 }
philipel02447bc2016-05-13 06:01:03 -0700259
philipel0fa82a62018-03-19 15:34:53 +0100260 last_picture_id_ = frame->id.picture_id;
261 UpdateLastPictureIdWithPadding(frame->id.picture_id);
philipeldabfcae2018-09-25 12:54:37 +0200262 frame->id.picture_id = rtp_seq_num_unwrapper_.Unwrap(frame->id.picture_id);
philipelafcf7f52017-04-26 08:17:35 -0700263 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700264}
265
philipelafcf7f52017-04-26 08:17:35 -0700266RtpFrameReferenceFinder::FrameDecision RtpFrameReferenceFinder::ManageFrameVp8(
267 RtpFrameObject* frame) {
Johannes Krona3705562019-08-26 16:37:11 +0200268 const RTPVideoHeader& video_header = frame->GetRtpVideoHeader();
269 RTPVideoTypeHeader rtp_codec_header = video_header.video_type_header;
philipel02447bc2016-05-13 06:01:03 -0700270
philipel1a4746a2018-07-09 15:52:29 +0200271 const RTPVideoHeaderVP8& codec_header =
Sami Kalliomäki98824952018-08-28 14:39:21 +0200272 absl::get<RTPVideoHeaderVP8>(rtp_codec_header);
philipel02447bc2016-05-13 06:01:03 -0700273
274 if (codec_header.pictureId == kNoPictureId ||
275 codec_header.temporalIdx == kNoTemporalIdx ||
276 codec_header.tl0PicIdx == kNoTl0PicIdx) {
Mirko Bonadei05cf6be2019-01-31 21:38:12 +0100277 return ManageFramePidOrSeqNum(frame, codec_header.pictureId);
philipel02447bc2016-05-13 06:01:03 -0700278 }
279
philipel0fa82a62018-03-19 15:34:53 +0100280 frame->id.picture_id = codec_header.pictureId % kPicIdLength;
philipel02447bc2016-05-13 06:01:03 -0700281
philipel02447bc2016-05-13 06:01:03 -0700282 if (last_picture_id_ == -1)
philipel0fa82a62018-03-19 15:34:53 +0100283 last_picture_id_ = frame->id.picture_id;
philipel02447bc2016-05-13 06:01:03 -0700284
285 // Find if there has been a gap in fully received frames and save the picture
286 // id of those frames in |not_yet_received_frames_|.
philipel0fa82a62018-03-19 15:34:53 +0100287 if (AheadOf<uint16_t, kPicIdLength>(frame->id.picture_id, last_picture_id_)) {
philipel9bd1d662017-07-14 04:52:01 -0700288 do {
philipel02447bc2016-05-13 06:01:03 -0700289 last_picture_id_ = Add<kPicIdLength>(last_picture_id_, 1);
philipel9bd1d662017-07-14 04:52:01 -0700290 not_yet_received_frames_.insert(last_picture_id_);
philipel0fa82a62018-03-19 15:34:53 +0100291 } while (last_picture_id_ != frame->id.picture_id);
philipel02447bc2016-05-13 06:01:03 -0700292 }
293
philipel57ec6852018-07-03 18:09:32 +0200294 int64_t unwrapped_tl0 = tl0_unwrapper_.Unwrap(codec_header.tl0PicIdx);
295
philipel02447bc2016-05-13 06:01:03 -0700296 // Clean up info for base layers that are too old.
philipel57ec6852018-07-03 18:09:32 +0200297 int64_t old_tl0_pic_idx = unwrapped_tl0 - kMaxLayerInfo;
philipel02447bc2016-05-13 06:01:03 -0700298 auto clean_layer_info_to = layer_info_.lower_bound(old_tl0_pic_idx);
299 layer_info_.erase(layer_info_.begin(), clean_layer_info_to);
300
301 // Clean up info about not yet received frames that are too old.
302 uint16_t old_picture_id =
philipel0fa82a62018-03-19 15:34:53 +0100303 Subtract<kPicIdLength>(frame->id.picture_id, kMaxNotYetReceivedFrames);
philipel02447bc2016-05-13 06:01:03 -0700304 auto clean_frames_to = not_yet_received_frames_.lower_bound(old_picture_id);
305 not_yet_received_frames_.erase(not_yet_received_frames_.begin(),
306 clean_frames_to);
307
Niels Möller8f7ce222019-03-21 15:43:58 +0100308 if (frame->frame_type() == VideoFrameType::kVideoFrameKey) {
philipel02447bc2016-05-13 06:01:03 -0700309 frame->num_references = 0;
philipel57ec6852018-07-03 18:09:32 +0200310 layer_info_[unwrapped_tl0].fill(-1);
311 UpdateLayerInfoVp8(frame, unwrapped_tl0, codec_header.temporalIdx);
philipelafcf7f52017-04-26 08:17:35 -0700312 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700313 }
314
philipel57ec6852018-07-03 18:09:32 +0200315 auto layer_info_it = layer_info_.find(
316 codec_header.temporalIdx == 0 ? unwrapped_tl0 - 1 : unwrapped_tl0);
philipel02447bc2016-05-13 06:01:03 -0700317
318 // If we don't have the base layer frame yet, stash this frame.
philipelafcf7f52017-04-26 08:17:35 -0700319 if (layer_info_it == layer_info_.end())
320 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700321
322 // A non keyframe base layer frame has been received, copy the layer info
323 // from the previous base layer frame and set a reference to the previous
324 // base layer frame.
325 if (codec_header.temporalIdx == 0) {
326 layer_info_it =
philipel57ec6852018-07-03 18:09:32 +0200327 layer_info_.emplace(unwrapped_tl0, layer_info_it->second).first;
philipel02447bc2016-05-13 06:01:03 -0700328 frame->num_references = 1;
329 frame->references[0] = layer_info_it->second[0];
philipel57ec6852018-07-03 18:09:32 +0200330 UpdateLayerInfoVp8(frame, unwrapped_tl0, codec_header.temporalIdx);
philipelafcf7f52017-04-26 08:17:35 -0700331 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700332 }
333
334 // Layer sync frame, this frame only references its base layer frame.
335 if (codec_header.layerSync) {
336 frame->num_references = 1;
337 frame->references[0] = layer_info_it->second[0];
338
philipel57ec6852018-07-03 18:09:32 +0200339 UpdateLayerInfoVp8(frame, unwrapped_tl0, codec_header.temporalIdx);
philipelafcf7f52017-04-26 08:17:35 -0700340 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700341 }
342
343 // Find all references for this frame.
344 frame->num_references = 0;
345 for (uint8_t layer = 0; layer <= codec_header.temporalIdx; ++layer) {
philipeld268d6f2016-09-15 13:43:13 +0200346 // If we have not yet received a previous frame on this temporal layer,
347 // stash this frame.
philipelafcf7f52017-04-26 08:17:35 -0700348 if (layer_info_it->second[layer] == -1)
349 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700350
philipel86b92e02016-10-24 07:11:53 -0700351 // If the last frame on this layer is ahead of this frame it means that
352 // a layer sync frame has been received after this frame for the same
353 // base layer frame, drop this frame.
354 if (AheadOf<uint16_t, kPicIdLength>(layer_info_it->second[layer],
philipel0fa82a62018-03-19 15:34:53 +0100355 frame->id.picture_id)) {
philipelafcf7f52017-04-26 08:17:35 -0700356 return kDrop;
philipel86b92e02016-10-24 07:11:53 -0700357 }
358
philipel02447bc2016-05-13 06:01:03 -0700359 // If we have not yet received a frame between this frame and the referenced
360 // frame then we have to wait for that frame to be completed first.
361 auto not_received_frame_it =
362 not_yet_received_frames_.upper_bound(layer_info_it->second[layer]);
363 if (not_received_frame_it != not_yet_received_frames_.end() &&
philipel0fa82a62018-03-19 15:34:53 +0100364 AheadOf<uint16_t, kPicIdLength>(frame->id.picture_id,
philipel02447bc2016-05-13 06:01:03 -0700365 *not_received_frame_it)) {
philipelafcf7f52017-04-26 08:17:35 -0700366 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700367 }
368
philipel0fa82a62018-03-19 15:34:53 +0100369 if (!(AheadOf<uint16_t, kPicIdLength>(frame->id.picture_id,
philipel57f19cc2017-03-07 03:54:05 -0800370 layer_info_it->second[layer]))) {
philipel0fa82a62018-03-19 15:34:53 +0100371 RTC_LOG(LS_WARNING) << "Frame with picture id " << frame->id.picture_id
Mirko Bonadei675513b2017-11-09 11:09:25 +0100372 << " and packet range [" << frame->first_seq_num()
373 << ", " << frame->last_seq_num()
374 << "] already received, "
375 << " dropping frame.";
philipelafcf7f52017-04-26 08:17:35 -0700376 return kDrop;
philipel57f19cc2017-03-07 03:54:05 -0800377 }
378
philipel02447bc2016-05-13 06:01:03 -0700379 ++frame->num_references;
380 frame->references[layer] = layer_info_it->second[layer];
381 }
382
philipel57ec6852018-07-03 18:09:32 +0200383 UpdateLayerInfoVp8(frame, unwrapped_tl0, codec_header.temporalIdx);
philipelafcf7f52017-04-26 08:17:35 -0700384 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700385}
386
philipel57ec6852018-07-03 18:09:32 +0200387void RtpFrameReferenceFinder::UpdateLayerInfoVp8(RtpFrameObject* frame,
388 int64_t unwrapped_tl0,
389 uint8_t temporal_idx) {
390 auto layer_info_it = layer_info_.find(unwrapped_tl0);
philipel02447bc2016-05-13 06:01:03 -0700391
392 // Update this layer info and newer.
393 while (layer_info_it != layer_info_.end()) {
philipel57ec6852018-07-03 18:09:32 +0200394 if (layer_info_it->second[temporal_idx] != -1 &&
395 AheadOf<uint16_t, kPicIdLength>(layer_info_it->second[temporal_idx],
philipel0fa82a62018-03-19 15:34:53 +0100396 frame->id.picture_id)) {
philipel02447bc2016-05-13 06:01:03 -0700397 // The frame was not newer, then no subsequent layer info have to be
398 // update.
399 break;
400 }
401
philipel57ec6852018-07-03 18:09:32 +0200402 layer_info_it->second[temporal_idx] = frame->id.picture_id;
403 ++unwrapped_tl0;
404 layer_info_it = layer_info_.find(unwrapped_tl0);
philipel02447bc2016-05-13 06:01:03 -0700405 }
philipel0fa82a62018-03-19 15:34:53 +0100406 not_yet_received_frames_.erase(frame->id.picture_id);
philipel02447bc2016-05-13 06:01:03 -0700407
philipelafcf7f52017-04-26 08:17:35 -0700408 UnwrapPictureIds(frame);
philipel02447bc2016-05-13 06:01:03 -0700409}
410
philipelafcf7f52017-04-26 08:17:35 -0700411RtpFrameReferenceFinder::FrameDecision RtpFrameReferenceFinder::ManageFrameVp9(
412 RtpFrameObject* frame) {
Johannes Krona3705562019-08-26 16:37:11 +0200413 const RTPVideoHeader& video_header = frame->GetRtpVideoHeader();
414 RTPVideoTypeHeader rtp_codec_header = video_header.video_type_header;
philipel4c140092017-08-31 08:31:45 -0700415
philipel1a4746a2018-07-09 15:52:29 +0200416 const RTPVideoHeaderVP9& codec_header =
Sami Kalliomäki98824952018-08-28 14:39:21 +0200417 absl::get<RTPVideoHeaderVP9>(rtp_codec_header);
philipel02447bc2016-05-13 06:01:03 -0700418
philipel647998c2016-06-03 09:40:16 -0700419 if (codec_header.picture_id == kNoPictureId ||
Sergey Silkind34a1882018-08-20 16:46:05 +0200420 codec_header.temporal_idx == kNoTemporalIdx) {
Mirko Bonadei05cf6be2019-01-31 21:38:12 +0100421 return ManageFramePidOrSeqNum(frame, codec_header.picture_id);
philipel02447bc2016-05-13 06:01:03 -0700422 }
423
philipel0fa82a62018-03-19 15:34:53 +0100424 frame->id.spatial_layer = codec_header.spatial_idx;
philipel02447bc2016-05-13 06:01:03 -0700425 frame->inter_layer_predicted = codec_header.inter_layer_predicted;
philipel0fa82a62018-03-19 15:34:53 +0100426 frame->id.picture_id = codec_header.picture_id % kPicIdLength;
philipel02447bc2016-05-13 06:01:03 -0700427
philipel02447bc2016-05-13 06:01:03 -0700428 if (last_picture_id_ == -1)
philipel0fa82a62018-03-19 15:34:53 +0100429 last_picture_id_ = frame->id.picture_id;
philipel02447bc2016-05-13 06:01:03 -0700430
431 if (codec_header.flexible_mode) {
432 frame->num_references = codec_header.num_ref_pics;
433 for (size_t i = 0; i < frame->num_references; ++i) {
philipel0fa82a62018-03-19 15:34:53 +0100434 frame->references[i] = Subtract<kPicIdLength>(frame->id.picture_id,
435 codec_header.pid_diff[i]);
philipel02447bc2016-05-13 06:01:03 -0700436 }
437
philipelafcf7f52017-04-26 08:17:35 -0700438 UnwrapPictureIds(frame);
439 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700440 }
441
Sergey Silkind34a1882018-08-20 16:46:05 +0200442 if (codec_header.tl0_pic_idx == kNoTl0PicIdx) {
443 RTC_LOG(LS_WARNING) << "TL0PICIDX is expected to be present in "
444 "non-flexible mode.";
445 return kDrop;
446 }
447
philipel15643602018-05-03 16:14:13 +0200448 GofInfo* info;
philipel57ec6852018-07-03 18:09:32 +0200449 int64_t unwrapped_tl0 = tl0_unwrapper_.Unwrap(codec_header.tl0_pic_idx);
philipel02447bc2016-05-13 06:01:03 -0700450 if (codec_header.ss_data_available) {
philipel02447bc2016-05-13 06:01:03 -0700451 if (codec_header.temporal_idx != 0) {
philipel15643602018-05-03 16:14:13 +0200452 RTC_LOG(LS_WARNING) << "Received scalability structure on a non base "
453 "layer frame. Scalability structure ignored.";
philipel02447bc2016-05-13 06:01:03 -0700454 } else {
Sergey Silkin2f864fb2018-09-07 11:49:38 +0200455 if (codec_header.gof.num_frames_in_gof > kMaxVp9FramesInGof) {
Natalie Silvanovich3ea3e302018-05-16 11:03:12 -0700456 return kDrop;
457 }
458
Sergey Silkin2f864fb2018-09-07 11:49:38 +0200459 GofInfoVP9 gof = codec_header.gof;
460 if (gof.num_frames_in_gof == 0) {
461 RTC_LOG(LS_WARNING) << "Number of frames in GOF is zero. Assume "
462 "that stream has only one temporal layer.";
463 gof.SetGofInfoVP9(kTemporalStructureMode1);
464 }
465
466 current_ss_idx_ = Add<kMaxGofSaved>(current_ss_idx_, 1);
467 scalability_structures_[current_ss_idx_] = gof;
philipel0fa82a62018-03-19 15:34:53 +0100468 scalability_structures_[current_ss_idx_].pid_start = frame->id.picture_id;
philipel57ec6852018-07-03 18:09:32 +0200469 gof_info_.emplace(unwrapped_tl0,
philipel15643602018-05-03 16:14:13 +0200470 GofInfo(&scalability_structures_[current_ss_idx_],
471 frame->id.picture_id));
philipel02447bc2016-05-13 06:01:03 -0700472 }
philipel15643602018-05-03 16:14:13 +0200473
philipel57ec6852018-07-03 18:09:32 +0200474 const auto gof_info_it = gof_info_.find(unwrapped_tl0);
philipel15643602018-05-03 16:14:13 +0200475 if (gof_info_it == gof_info_.end())
476 return kStash;
477
478 info = &gof_info_it->second;
479
Niels Möller8f7ce222019-03-21 15:43:58 +0100480 if (frame->frame_type() == VideoFrameType::kVideoFrameKey) {
philipel15643602018-05-03 16:14:13 +0200481 frame->num_references = 0;
482 FrameReceivedVp9(frame->id.picture_id, info);
483 UnwrapPictureIds(frame);
484 return kHandOff;
485 }
Niels Möller8f7ce222019-03-21 15:43:58 +0100486 } else if (frame->frame_type() == VideoFrameType::kVideoFrameKey) {
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100487 if (frame->id.spatial_layer == 0) {
philipel15643602018-05-03 16:14:13 +0200488 RTC_LOG(LS_WARNING) << "Received keyframe without scalability structure";
489 return kDrop;
490 }
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100491 const auto gof_info_it = gof_info_.find(unwrapped_tl0);
492 if (gof_info_it == gof_info_.end())
493 return kStash;
philipel15643602018-05-03 16:14:13 +0200494
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100495 info = &gof_info_it->second;
496
Niels Möller8f7ce222019-03-21 15:43:58 +0100497 if (frame->frame_type() == VideoFrameType::kVideoFrameKey) {
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100498 frame->num_references = 0;
499 FrameReceivedVp9(frame->id.picture_id, info);
500 UnwrapPictureIds(frame);
501 return kHandOff;
502 }
503 } else {
philipel57ec6852018-07-03 18:09:32 +0200504 auto gof_info_it = gof_info_.find(
505 (codec_header.temporal_idx == 0) ? unwrapped_tl0 - 1 : unwrapped_tl0);
philipel15643602018-05-03 16:14:13 +0200506
507 // Gof info for this frame is not available yet, stash this frame.
508 if (gof_info_it == gof_info_.end())
509 return kStash;
510
511 if (codec_header.temporal_idx == 0) {
philipel57ec6852018-07-03 18:09:32 +0200512 gof_info_it = gof_info_
513 .emplace(unwrapped_tl0, GofInfo(gof_info_it->second.gof,
514 frame->id.picture_id))
515 .first;
philipel15643602018-05-03 16:14:13 +0200516 }
517
518 info = &gof_info_it->second;
philipel02447bc2016-05-13 06:01:03 -0700519 }
520
521 // Clean up info for base layers that are too old.
philipel57ec6852018-07-03 18:09:32 +0200522 int64_t old_tl0_pic_idx = unwrapped_tl0 - kMaxGofSaved;
philipel02447bc2016-05-13 06:01:03 -0700523 auto clean_gof_info_to = gof_info_.lower_bound(old_tl0_pic_idx);
524 gof_info_.erase(gof_info_.begin(), clean_gof_info_to);
525
philipel0fa82a62018-03-19 15:34:53 +0100526 FrameReceivedVp9(frame->id.picture_id, info);
philipel02447bc2016-05-13 06:01:03 -0700527
528 // Make sure we don't miss any frame that could potentially have the
529 // up switch flag set.
philipel0fa82a62018-03-19 15:34:53 +0100530 if (MissingRequiredFrameVp9(frame->id.picture_id, *info))
philipelafcf7f52017-04-26 08:17:35 -0700531 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700532
philipel15643602018-05-03 16:14:13 +0200533 if (codec_header.temporal_up_switch)
534 up_switch_.emplace(frame->id.picture_id, codec_header.temporal_idx);
philipel02447bc2016-05-13 06:01:03 -0700535
536 // Clean out old info about up switch frames.
philipel0fa82a62018-03-19 15:34:53 +0100537 uint16_t old_picture_id = Subtract<kPicIdLength>(frame->id.picture_id, 50);
philipel02447bc2016-05-13 06:01:03 -0700538 auto up_switch_erase_to = up_switch_.lower_bound(old_picture_id);
539 up_switch_.erase(up_switch_.begin(), up_switch_erase_to);
540
philipelc9b27d52016-07-15 06:50:27 -0700541 size_t diff = ForwardDiff<uint16_t, kPicIdLength>(info->gof->pid_start,
philipel0fa82a62018-03-19 15:34:53 +0100542 frame->id.picture_id);
philipelc9b27d52016-07-15 06:50:27 -0700543 size_t gof_idx = diff % info->gof->num_frames_in_gof;
philipel02447bc2016-05-13 06:01:03 -0700544
545 // Populate references according to the scalability structure.
philipelc9b27d52016-07-15 06:50:27 -0700546 frame->num_references = info->gof->num_ref_pics[gof_idx];
philipel02447bc2016-05-13 06:01:03 -0700547 for (size_t i = 0; i < frame->num_references; ++i) {
philipelc9b27d52016-07-15 06:50:27 -0700548 frame->references[i] = Subtract<kPicIdLength>(
philipel0fa82a62018-03-19 15:34:53 +0100549 frame->id.picture_id, info->gof->pid_diff[gof_idx][i]);
philipel02447bc2016-05-13 06:01:03 -0700550
551 // If this is a reference to a frame earlier than the last up switch point,
552 // then ignore this reference.
philipel0fa82a62018-03-19 15:34:53 +0100553 if (UpSwitchInIntervalVp9(frame->id.picture_id, codec_header.temporal_idx,
philipel02447bc2016-05-13 06:01:03 -0700554 frame->references[i])) {
555 --frame->num_references;
556 }
557 }
558
Ilya Nikolaevskiy2ec0c652019-01-18 11:56:48 +0100559 // Override GOF references.
560 if (!codec_header.inter_pic_predicted) {
561 frame->num_references = 0;
562 }
563
philipelafcf7f52017-04-26 08:17:35 -0700564 UnwrapPictureIds(frame);
565 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700566}
567
568bool RtpFrameReferenceFinder::MissingRequiredFrameVp9(uint16_t picture_id,
philipelc9b27d52016-07-15 06:50:27 -0700569 const GofInfo& info) {
570 size_t diff =
571 ForwardDiff<uint16_t, kPicIdLength>(info.gof->pid_start, picture_id);
572 size_t gof_idx = diff % info.gof->num_frames_in_gof;
573 size_t temporal_idx = info.gof->temporal_idx[gof_idx];
philipel02447bc2016-05-13 06:01:03 -0700574
philipela157e082018-05-02 15:19:01 +0200575 if (temporal_idx >= kMaxTemporalLayers) {
576 RTC_LOG(LS_WARNING) << "At most " << kMaxTemporalLayers << " temporal "
577 << "layers are supported.";
578 return true;
579 }
580
philipel02447bc2016-05-13 06:01:03 -0700581 // For every reference this frame has, check if there is a frame missing in
582 // the interval (|ref_pid|, |picture_id|) in any of the lower temporal
583 // layers. If so, we are missing a required frame.
philipelc9b27d52016-07-15 06:50:27 -0700584 uint8_t num_references = info.gof->num_ref_pics[gof_idx];
philipel02447bc2016-05-13 06:01:03 -0700585 for (size_t i = 0; i < num_references; ++i) {
586 uint16_t ref_pid =
philipelc9b27d52016-07-15 06:50:27 -0700587 Subtract<kPicIdLength>(picture_id, info.gof->pid_diff[gof_idx][i]);
philipel02447bc2016-05-13 06:01:03 -0700588 for (size_t l = 0; l < temporal_idx; ++l) {
589 auto missing_frame_it = missing_frames_for_layer_[l].lower_bound(ref_pid);
590 if (missing_frame_it != missing_frames_for_layer_[l].end() &&
591 AheadOf<uint16_t, kPicIdLength>(picture_id, *missing_frame_it)) {
592 return true;
593 }
594 }
595 }
596 return false;
597}
598
599void RtpFrameReferenceFinder::FrameReceivedVp9(uint16_t picture_id,
philipelc9b27d52016-07-15 06:50:27 -0700600 GofInfo* info) {
601 int last_picture_id = info->last_picture_id;
philipel459f4e32018-03-02 10:55:12 +0100602 size_t gof_size = std::min(info->gof->num_frames_in_gof, kMaxVp9FramesInGof);
philipel02447bc2016-05-13 06:01:03 -0700603
604 // If there is a gap, find which temporal layer the missing frames
605 // belong to and add the frame as missing for that temporal layer.
606 // Otherwise, remove this frame from the set of missing frames.
philipelc9b27d52016-07-15 06:50:27 -0700607 if (AheadOf<uint16_t, kPicIdLength>(picture_id, last_picture_id)) {
608 size_t diff = ForwardDiff<uint16_t, kPicIdLength>(info->gof->pid_start,
609 last_picture_id);
philipel459f4e32018-03-02 10:55:12 +0100610 size_t gof_idx = diff % gof_size;
philipel02447bc2016-05-13 06:01:03 -0700611
philipelc9b27d52016-07-15 06:50:27 -0700612 last_picture_id = Add<kPicIdLength>(last_picture_id, 1);
613 while (last_picture_id != picture_id) {
Yves Gerey665174f2018-06-19 15:03:05 +0200614 gof_idx = (gof_idx + 1) % gof_size;
philipel459f4e32018-03-02 10:55:12 +0100615 RTC_CHECK(gof_idx < kMaxVp9FramesInGof);
616
philipelc9b27d52016-07-15 06:50:27 -0700617 size_t temporal_idx = info->gof->temporal_idx[gof_idx];
philipel459f4e32018-03-02 10:55:12 +0100618 if (temporal_idx >= kMaxTemporalLayers) {
619 RTC_LOG(LS_WARNING) << "At most " << kMaxTemporalLayers << " temporal "
620 << "layers are supported.";
621 return;
622 }
623
philipelc9b27d52016-07-15 06:50:27 -0700624 missing_frames_for_layer_[temporal_idx].insert(last_picture_id);
625 last_picture_id = Add<kPicIdLength>(last_picture_id, 1);
philipel02447bc2016-05-13 06:01:03 -0700626 }
philipel459f4e32018-03-02 10:55:12 +0100627
philipelc9b27d52016-07-15 06:50:27 -0700628 info->last_picture_id = last_picture_id;
philipel02447bc2016-05-13 06:01:03 -0700629 } else {
630 size_t diff =
philipelc9b27d52016-07-15 06:50:27 -0700631 ForwardDiff<uint16_t, kPicIdLength>(info->gof->pid_start, picture_id);
philipel459f4e32018-03-02 10:55:12 +0100632 size_t gof_idx = diff % gof_size;
633 RTC_CHECK(gof_idx < kMaxVp9FramesInGof);
634
philipelc9b27d52016-07-15 06:50:27 -0700635 size_t temporal_idx = info->gof->temporal_idx[gof_idx];
philipel459f4e32018-03-02 10:55:12 +0100636 if (temporal_idx >= kMaxTemporalLayers) {
637 RTC_LOG(LS_WARNING) << "At most " << kMaxTemporalLayers << " temporal "
638 << "layers are supported.";
639 return;
640 }
641
philipel02447bc2016-05-13 06:01:03 -0700642 missing_frames_for_layer_[temporal_idx].erase(picture_id);
643 }
644}
645
646bool RtpFrameReferenceFinder::UpSwitchInIntervalVp9(uint16_t picture_id,
647 uint8_t temporal_idx,
648 uint16_t pid_ref) {
649 for (auto up_switch_it = up_switch_.upper_bound(pid_ref);
650 up_switch_it != up_switch_.end() &&
651 AheadOf<uint16_t, kPicIdLength>(picture_id, up_switch_it->first);
652 ++up_switch_it) {
653 if (up_switch_it->second < temporal_idx)
654 return true;
655 }
656
657 return false;
658}
659
philipelafcf7f52017-04-26 08:17:35 -0700660void RtpFrameReferenceFinder::UnwrapPictureIds(RtpFrameObject* frame) {
philipel02447bc2016-05-13 06:01:03 -0700661 for (size_t i = 0; i < frame->num_references; ++i)
philipeld4fac692017-09-04 07:03:46 -0700662 frame->references[i] = unwrapper_.Unwrap(frame->references[i]);
philipel0fa82a62018-03-19 15:34:53 +0100663 frame->id.picture_id = unwrapper_.Unwrap(frame->id.picture_id);
philipel02447bc2016-05-13 06:01:03 -0700664}
665
Johnny Leebc7f41b2019-05-01 14:41:32 -0400666RtpFrameReferenceFinder::FrameDecision RtpFrameReferenceFinder::ManageFrameH264(
667 RtpFrameObject* frame) {
Johannes Krona3705562019-08-26 16:37:11 +0200668 const FrameMarking& rtp_frame_marking = frame->GetFrameMarking();
Johnny Leebc7f41b2019-05-01 14:41:32 -0400669
Johannes Krona3705562019-08-26 16:37:11 +0200670 uint8_t tid = rtp_frame_marking.temporal_id;
671 bool blSync = rtp_frame_marking.base_layer_sync;
Johnny Leebc7f41b2019-05-01 14:41:32 -0400672
673 if (tid == kNoTemporalIdx)
674 return ManageFramePidOrSeqNum(std::move(frame), kNoPictureId);
675
676 frame->id.picture_id = frame->last_seq_num();
677
678 if (frame->frame_type() == VideoFrameType::kVideoFrameKey) {
679 // For H264, use last_seq_num_gop_ to simply store last picture id
680 // as a pair of unpadded and padded sequence numbers.
681 if (last_seq_num_gop_.empty()) {
682 last_seq_num_gop_.insert(std::make_pair(
683 0, std::make_pair(frame->id.picture_id, frame->id.picture_id)));
684 }
685 }
686
687 // Stash if we have no keyframe yet.
688 if (last_seq_num_gop_.empty())
689 return kStash;
690
691 // Check for gap in sequence numbers. Store in |not_yet_received_seq_num_|.
692 if (frame->frame_type() == VideoFrameType::kVideoFrameDelta) {
693 uint16_t last_pic_id_padded = last_seq_num_gop_.begin()->second.second;
694 if (AheadOf<uint16_t>(frame->id.picture_id, last_pic_id_padded)) {
695 do {
696 last_pic_id_padded = last_pic_id_padded + 1;
697 not_yet_received_seq_num_.insert(last_pic_id_padded);
698 } while (last_pic_id_padded != frame->id.picture_id);
699 }
700 }
701
Johannes Krona3705562019-08-26 16:37:11 +0200702 int64_t unwrapped_tl0 = tl0_unwrapper_.Unwrap(rtp_frame_marking.tl0_pic_idx);
Johnny Leebc7f41b2019-05-01 14:41:32 -0400703
704 // Clean up info for base layers that are too old.
705 int64_t old_tl0_pic_idx = unwrapped_tl0 - kMaxLayerInfo;
706 auto clean_layer_info_to = layer_info_.lower_bound(old_tl0_pic_idx);
707 layer_info_.erase(layer_info_.begin(), clean_layer_info_to);
708
709 // Clean up info about not yet received frames that are too old.
710 uint16_t old_picture_id = frame->id.picture_id - kMaxNotYetReceivedFrames * 2;
711 auto clean_frames_to = not_yet_received_seq_num_.lower_bound(old_picture_id);
712 not_yet_received_seq_num_.erase(not_yet_received_seq_num_.begin(),
713 clean_frames_to);
714
715 if (frame->frame_type() == VideoFrameType::kVideoFrameKey) {
716 frame->num_references = 0;
717 layer_info_[unwrapped_tl0].fill(-1);
718 UpdateDataH264(frame, unwrapped_tl0, tid);
719 return kHandOff;
720 }
721
Jonas Olssona4d87372019-07-05 19:08:33 +0200722 auto layer_info_it =
723 layer_info_.find(tid == 0 ? unwrapped_tl0 - 1 : unwrapped_tl0);
Johnny Leebc7f41b2019-05-01 14:41:32 -0400724
725 // Stash if we have no base layer frame yet.
726 if (layer_info_it == layer_info_.end())
727 return kStash;
728
729 // Base layer frame. Copy layer info from previous base layer frame.
730 if (tid == 0) {
Jonas Olssona4d87372019-07-05 19:08:33 +0200731 layer_info_it =
732 layer_info_.insert(std::make_pair(unwrapped_tl0, layer_info_it->second))
733 .first;
Johnny Leebc7f41b2019-05-01 14:41:32 -0400734 frame->num_references = 1;
735 frame->references[0] = layer_info_it->second[0];
736 UpdateDataH264(frame, unwrapped_tl0, tid);
737 return kHandOff;
738 }
739
740 // This frame only references its base layer frame.
741 if (blSync) {
742 frame->num_references = 1;
743 frame->references[0] = layer_info_it->second[0];
744 UpdateDataH264(frame, unwrapped_tl0, tid);
745 return kHandOff;
746 }
747
748 // Find all references for general frame.
749 frame->num_references = 0;
750 for (uint8_t layer = 0; layer <= tid; ++layer) {
751 // Stash if we have not yet received frames on this temporal layer.
752 if (layer_info_it->second[layer] == -1)
753 return kStash;
754
755 // Drop if the last frame on this layer is ahead of this frame. A layer sync
756 // frame was received after this frame for the same base layer frame.
757 uint16_t last_frame_in_layer = layer_info_it->second[layer];
758 if (AheadOf<uint16_t>(last_frame_in_layer, frame->id.picture_id))
759 return kDrop;
760
761 // Stash and wait for missing frame between this frame and the reference
762 auto not_received_seq_num_it =
763 not_yet_received_seq_num_.upper_bound(last_frame_in_layer);
764 if (not_received_seq_num_it != not_yet_received_seq_num_.end() &&
765 AheadOf<uint16_t>(frame->id.picture_id, *not_received_seq_num_it)) {
766 return kStash;
767 }
768
769 if (!(AheadOf<uint16_t>(frame->id.picture_id, last_frame_in_layer))) {
770 RTC_LOG(LS_WARNING) << "Frame with picture id " << frame->id.picture_id
771 << " and packet range [" << frame->first_seq_num()
772 << ", " << frame->last_seq_num()
773 << "] already received, "
774 << " dropping frame.";
775 return kDrop;
776 }
777
778 ++frame->num_references;
779 frame->references[layer] = last_frame_in_layer;
780 }
781
782 UpdateDataH264(frame, unwrapped_tl0, tid);
783 return kHandOff;
784}
785
786void RtpFrameReferenceFinder::UpdateLastPictureIdWithPaddingH264() {
787 auto seq_num_it = last_seq_num_gop_.begin();
788
789 // Check if next sequence number is in a stashed padding packet.
790 uint16_t next_padded_seq_num = seq_num_it->second.second + 1;
791 auto padding_seq_num_it = stashed_padding_.lower_bound(next_padded_seq_num);
792
793 // Check for more consecutive padding packets to increment
794 // the "last-picture-id-with-padding" and remove the stashed packets.
795 while (padding_seq_num_it != stashed_padding_.end() &&
796 *padding_seq_num_it == next_padded_seq_num) {
797 seq_num_it->second.second = next_padded_seq_num;
798 ++next_padded_seq_num;
799 padding_seq_num_it = stashed_padding_.erase(padding_seq_num_it);
800 }
801}
802
803void RtpFrameReferenceFinder::UpdateLayerInfoH264(RtpFrameObject* frame,
804 int64_t unwrapped_tl0,
805 uint8_t temporal_idx) {
806 auto layer_info_it = layer_info_.find(unwrapped_tl0);
807
808 // Update this layer info and newer.
809 while (layer_info_it != layer_info_.end()) {
810 if (layer_info_it->second[temporal_idx] != -1 &&
811 AheadOf<uint16_t>(layer_info_it->second[temporal_idx],
812 frame->id.picture_id)) {
813 // Not a newer frame. No subsequent layer info needs update.
814 break;
815 }
816
817 layer_info_it->second[temporal_idx] = frame->id.picture_id;
818 ++unwrapped_tl0;
819 layer_info_it = layer_info_.find(unwrapped_tl0);
820 }
821
822 for (size_t i = 0; i < frame->num_references; ++i)
823 frame->references[i] = rtp_seq_num_unwrapper_.Unwrap(frame->references[i]);
824 frame->id.picture_id = rtp_seq_num_unwrapper_.Unwrap(frame->id.picture_id);
825}
826
827void RtpFrameReferenceFinder::UpdateDataH264(RtpFrameObject* frame,
828 int64_t unwrapped_tl0,
829 uint8_t temporal_idx) {
830 // Update last_seq_num_gop_ entry for last picture id.
831 auto seq_num_it = last_seq_num_gop_.begin();
832 uint16_t last_pic_id = seq_num_it->second.first;
833 if (AheadOf<uint16_t>(frame->id.picture_id, last_pic_id)) {
834 seq_num_it->second.first = frame->id.picture_id;
835 seq_num_it->second.second = frame->id.picture_id;
836 }
837 UpdateLastPictureIdWithPaddingH264();
838
839 UpdateLayerInfoH264(frame, unwrapped_tl0, temporal_idx);
840
841 // Remove any current packets from |not_yet_received_seq_num_|.
842 uint16_t last_seq_num_padded = seq_num_it->second.second;
Jonas Olssona4d87372019-07-05 19:08:33 +0200843 for (uint16_t n = frame->first_seq_num(); AheadOrAt(last_seq_num_padded, n);
844 ++n) {
Johnny Leebc7f41b2019-05-01 14:41:32 -0400845 not_yet_received_seq_num_.erase(n);
846 }
847}
848
philipel02447bc2016-05-13 06:01:03 -0700849} // namespace video_coding
850} // namespace webrtc