blob: 4932c70ad42be2f32f2fb60c830f3beb5b7f54fa [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)
philipel7acc4a42019-09-26 11:25:52 +020028 : RtpFrameReferenceFinder(frame_callback, 0) {}
29
30RtpFrameReferenceFinder::RtpFrameReferenceFinder(
31 OnCompleteFrameCallback* frame_callback,
32 int64_t picture_id_offset)
philipel02447bc2016-05-13 06:01:03 -070033 : last_picture_id_(-1),
philipel02447bc2016-05-13 06:01:03 -070034 current_ss_idx_(0),
philipel463d3012016-09-09 03:32:44 -070035 cleared_to_seq_num_(-1),
philipel7acc4a42019-09-26 11:25:52 +020036 frame_callback_(frame_callback),
37 picture_id_offset_(picture_id_offset) {}
philipel02447bc2016-05-13 06:01:03 -070038
Mirko Bonadei8fdcac32018-08-28 16:30:18 +020039RtpFrameReferenceFinder::~RtpFrameReferenceFinder() = default;
40
philipel02447bc2016-05-13 06:01:03 -070041void RtpFrameReferenceFinder::ManageFrame(
42 std::unique_ptr<RtpFrameObject> frame) {
philipel463d3012016-09-09 03:32:44 -070043 // If we have cleared past this frame, drop it.
44 if (cleared_to_seq_num_ != -1 &&
45 AheadOf<uint16_t>(cleared_to_seq_num_, frame->first_seq_num())) {
46 return;
47 }
48
philipelafcf7f52017-04-26 08:17:35 -070049 FrameDecision decision = ManageFrameInternal(frame.get());
50
51 switch (decision) {
52 case kStash:
53 if (stashed_frames_.size() > kMaxStashedFrames)
54 stashed_frames_.pop_back();
55 stashed_frames_.push_front(std::move(frame));
56 break;
57 case kHandOff:
philipel7acc4a42019-09-26 11:25:52 +020058 HandOffFrame(std::move(frame));
philipelafcf7f52017-04-26 08:17:35 -070059 RetryStashedFrames();
60 break;
61 case kDrop:
62 break;
63 }
64}
65
66void RtpFrameReferenceFinder::RetryStashedFrames() {
67 bool complete_frame = false;
68 do {
69 complete_frame = false;
70 for (auto frame_it = stashed_frames_.begin();
71 frame_it != stashed_frames_.end();) {
72 FrameDecision decision = ManageFrameInternal(frame_it->get());
73
74 switch (decision) {
75 case kStash:
76 ++frame_it;
77 break;
78 case kHandOff:
79 complete_frame = true;
philipel7acc4a42019-09-26 11:25:52 +020080 HandOffFrame(std::move(*frame_it));
Karl Wiberg80ba3332018-02-05 10:33:35 +010081 RTC_FALLTHROUGH();
philipelafcf7f52017-04-26 08:17:35 -070082 case kDrop:
83 frame_it = stashed_frames_.erase(frame_it);
84 }
85 }
86 } while (complete_frame);
87}
88
philipel7acc4a42019-09-26 11:25:52 +020089void RtpFrameReferenceFinder::HandOffFrame(
90 std::unique_ptr<RtpFrameObject> frame) {
91 frame->id.picture_id += picture_id_offset_;
92 for (size_t i = 0; i < frame->num_references; ++i) {
93 frame->references[i] += picture_id_offset_;
94 }
95
96 frame_callback_->OnCompleteFrame(std::move(frame));
97}
98
philipelafcf7f52017-04-26 08:17:35 -070099RtpFrameReferenceFinder::FrameDecision
100RtpFrameReferenceFinder::ManageFrameInternal(RtpFrameObject* frame) {
philipel2837edc2018-10-02 13:55:47 +0200101 absl::optional<RtpGenericFrameDescriptor> generic_descriptor =
102 frame->GetGenericFrameDescriptor();
103 if (generic_descriptor) {
104 return ManageFrameGeneric(frame, *generic_descriptor);
philipeldabfcae2018-09-25 12:54:37 +0200105 }
106
philipel02447bc2016-05-13 06:01:03 -0700107 switch (frame->codec_type()) {
philipel02447bc2016-05-13 06:01:03 -0700108 case kVideoCodecVP8:
philipelafcf7f52017-04-26 08:17:35 -0700109 return ManageFrameVp8(frame);
philipel02447bc2016-05-13 06:01:03 -0700110 case kVideoCodecVP9:
philipelafcf7f52017-04-26 08:17:35 -0700111 return ManageFrameVp9(frame);
Johnny Leebc7f41b2019-05-01 14:41:32 -0400112 case kVideoCodecH264:
113 return ManageFrameH264(frame);
Sami Kalliomäki98824952018-08-28 14:39:21 +0200114 default: {
115 // Use 15 first bits of frame ID as picture ID if available.
Johannes Krona3705562019-08-26 16:37:11 +0200116 const RTPVideoHeader& video_header = frame->GetRtpVideoHeader();
philipeldabfcae2018-09-25 12:54:37 +0200117 int picture_id = kNoPictureId;
Johannes Krona3705562019-08-26 16:37:11 +0200118 if (video_header.generic)
119 picture_id = video_header.generic->frame_id & 0x7fff;
philipeldabfcae2018-09-25 12:54:37 +0200120
121 return ManageFramePidOrSeqNum(frame, picture_id);
Sami Kalliomäki98824952018-08-28 14:39:21 +0200122 }
philipel02447bc2016-05-13 06:01:03 -0700123 }
124}
125
philipel9b2ce6b2016-07-05 05:04:46 -0700126void RtpFrameReferenceFinder::PaddingReceived(uint16_t seq_num) {
philipel9b2ce6b2016-07-05 05:04:46 -0700127 auto clean_padding_to =
128 stashed_padding_.lower_bound(seq_num - kMaxPaddingAge);
129 stashed_padding_.erase(stashed_padding_.begin(), clean_padding_to);
130 stashed_padding_.insert(seq_num);
131 UpdateLastPictureIdWithPadding(seq_num);
132 RetryStashedFrames();
133}
134
philipel463d3012016-09-09 03:32:44 -0700135void RtpFrameReferenceFinder::ClearTo(uint16_t seq_num) {
philipel463d3012016-09-09 03:32:44 -0700136 cleared_to_seq_num_ = seq_num;
137
138 auto it = stashed_frames_.begin();
139 while (it != stashed_frames_.end()) {
140 if (AheadOf<uint16_t>(cleared_to_seq_num_, (*it)->first_seq_num())) {
141 it = stashed_frames_.erase(it);
142 } else {
143 ++it;
144 }
145 }
146}
147
philipel9b2ce6b2016-07-05 05:04:46 -0700148void RtpFrameReferenceFinder::UpdateLastPictureIdWithPadding(uint16_t seq_num) {
149 auto gop_seq_num_it = last_seq_num_gop_.upper_bound(seq_num);
150
151 // If this padding packet "belongs" to a group of pictures that we don't track
152 // anymore, do nothing.
153 if (gop_seq_num_it == last_seq_num_gop_.begin())
154 return;
155 --gop_seq_num_it;
156
157 // Calculate the next contiuous sequence number and search for it in
158 // the padding packets we have stashed.
159 uint16_t next_seq_num_with_padding = gop_seq_num_it->second.second + 1;
160 auto padding_seq_num_it =
161 stashed_padding_.lower_bound(next_seq_num_with_padding);
162
163 // While there still are padding packets and those padding packets are
164 // continuous, then advance the "last-picture-id-with-padding" and remove
165 // the stashed padding packet.
166 while (padding_seq_num_it != stashed_padding_.end() &&
167 *padding_seq_num_it == next_seq_num_with_padding) {
168 gop_seq_num_it->second.second = next_seq_num_with_padding;
169 ++next_seq_num_with_padding;
170 padding_seq_num_it = stashed_padding_.erase(padding_seq_num_it);
171 }
philipel41bb7922017-02-20 07:53:23 -0800172
173 // In the case where the stream has been continuous without any new keyframes
174 // for a while there is a risk that new frames will appear to be older than
175 // the keyframe they belong to due to wrapping sequence number. In order
176 // to prevent this we advance the picture id of the keyframe every so often.
177 if (ForwardDiff(gop_seq_num_it->first, seq_num) > 10000) {
178 RTC_DCHECK_EQ(1ul, last_seq_num_gop_.size());
179 last_seq_num_gop_[seq_num] = gop_seq_num_it->second;
180 last_seq_num_gop_.erase(gop_seq_num_it);
181 }
philipel9b2ce6b2016-07-05 05:04:46 -0700182}
183
philipelafcf7f52017-04-26 08:17:35 -0700184RtpFrameReferenceFinder::FrameDecision
philipeldabfcae2018-09-25 12:54:37 +0200185RtpFrameReferenceFinder::ManageFrameGeneric(
186 RtpFrameObject* frame,
philipel2837edc2018-10-02 13:55:47 +0200187 const RtpGenericFrameDescriptor& descriptor) {
188 int64_t frame_id = generic_frame_id_unwrapper_.Unwrap(descriptor.FrameId());
189 frame->id.picture_id = frame_id;
190 frame->id.spatial_layer = descriptor.SpatialLayer();
191
192 rtc::ArrayView<const uint16_t> diffs = descriptor.FrameDependenciesDiffs();
193 if (EncodedFrame::kMaxFrameReferences < diffs.size()) {
philipeldabfcae2018-09-25 12:54:37 +0200194 RTC_LOG(LS_WARNING) << "Too many dependencies in generic descriptor.";
195 return kDrop;
196 }
197
philipel2837edc2018-10-02 13:55:47 +0200198 frame->num_references = diffs.size();
199 for (size_t i = 0; i < diffs.size(); ++i)
200 frame->references[i] = frame_id - diffs[i];
philipeldabfcae2018-09-25 12:54:37 +0200201
202 return kHandOff;
203}
204
205RtpFrameReferenceFinder::FrameDecision
206RtpFrameReferenceFinder::ManageFramePidOrSeqNum(RtpFrameObject* frame,
207 int picture_id) {
philipel647998c2016-06-03 09:40:16 -0700208 // If |picture_id| is specified then we use that to set the frame references,
209 // otherwise we use sequence number.
210 if (picture_id != kNoPictureId) {
philipel0fa82a62018-03-19 15:34:53 +0100211 frame->id.picture_id = unwrapper_.Unwrap(picture_id);
Niels Möller8f7ce222019-03-21 15:43:58 +0100212 frame->num_references =
213 frame->frame_type() == VideoFrameType::kVideoFrameKey ? 0 : 1;
philipel0fa82a62018-03-19 15:34:53 +0100214 frame->references[0] = frame->id.picture_id - 1;
philipelafcf7f52017-04-26 08:17:35 -0700215 return kHandOff;
philipel647998c2016-06-03 09:40:16 -0700216 }
217
Niels Möller8f7ce222019-03-21 15:43:58 +0100218 if (frame->frame_type() == VideoFrameType::kVideoFrameKey) {
philipel9b2ce6b2016-07-05 05:04:46 -0700219 last_seq_num_gop_.insert(std::make_pair(
220 frame->last_seq_num(),
221 std::make_pair(frame->last_seq_num(), frame->last_seq_num())));
222 }
philipel02447bc2016-05-13 06:01:03 -0700223
224 // We have received a frame but not yet a keyframe, stash this frame.
philipelafcf7f52017-04-26 08:17:35 -0700225 if (last_seq_num_gop_.empty())
226 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700227
228 // Clean up info for old keyframes but make sure to keep info
229 // for the last keyframe.
230 auto clean_to = last_seq_num_gop_.lower_bound(frame->last_seq_num() - 100);
philipel41bb7922017-02-20 07:53:23 -0800231 for (auto it = last_seq_num_gop_.begin();
232 it != clean_to && last_seq_num_gop_.size() > 1;) {
233 it = last_seq_num_gop_.erase(it);
234 }
philipel02447bc2016-05-13 06:01:03 -0700235
236 // Find the last sequence number of the last frame for the keyframe
237 // that this frame indirectly references.
238 auto seq_num_it = last_seq_num_gop_.upper_bound(frame->last_seq_num());
philipel9b2ce6b2016-07-05 05:04:46 -0700239 if (seq_num_it == last_seq_num_gop_.begin()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100240 RTC_LOG(LS_WARNING) << "Generic frame with packet range ["
241 << frame->first_seq_num() << ", "
242 << frame->last_seq_num()
243 << "] has no GoP, dropping frame.";
philipelafcf7f52017-04-26 08:17:35 -0700244 return kDrop;
philipel9b2ce6b2016-07-05 05:04:46 -0700245 }
philipel02447bc2016-05-13 06:01:03 -0700246 seq_num_it--;
247
248 // Make sure the packet sequence numbers are continuous, otherwise stash
249 // this frame.
philipel9b2ce6b2016-07-05 05:04:46 -0700250 uint16_t last_picture_id_gop = seq_num_it->second.first;
251 uint16_t last_picture_id_with_padding_gop = seq_num_it->second.second;
Niels Möller8f7ce222019-03-21 15:43:58 +0100252 if (frame->frame_type() == VideoFrameType::kVideoFrameDelta) {
philipel9b2ce6b2016-07-05 05:04:46 -0700253 uint16_t prev_seq_num = frame->first_seq_num() - 1;
philipelafcf7f52017-04-26 08:17:35 -0700254
255 if (prev_seq_num != last_picture_id_with_padding_gop)
256 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700257 }
258
259 RTC_DCHECK(AheadOrAt(frame->last_seq_num(), seq_num_it->first));
260
261 // Since keyframes can cause reordering we can't simply assign the
262 // picture id according to some incrementing counter.
philipel0fa82a62018-03-19 15:34:53 +0100263 frame->id.picture_id = frame->last_seq_num();
Niels Möller8f7ce222019-03-21 15:43:58 +0100264 frame->num_references =
265 frame->frame_type() == VideoFrameType::kVideoFrameDelta;
philipeldabfcae2018-09-25 12:54:37 +0200266 frame->references[0] = rtp_seq_num_unwrapper_.Unwrap(last_picture_id_gop);
philipel0fa82a62018-03-19 15:34:53 +0100267 if (AheadOf<uint16_t>(frame->id.picture_id, last_picture_id_gop)) {
268 seq_num_it->second.first = frame->id.picture_id;
269 seq_num_it->second.second = frame->id.picture_id;
philipel9b2ce6b2016-07-05 05:04:46 -0700270 }
philipel02447bc2016-05-13 06:01:03 -0700271
philipel0fa82a62018-03-19 15:34:53 +0100272 last_picture_id_ = frame->id.picture_id;
273 UpdateLastPictureIdWithPadding(frame->id.picture_id);
philipeldabfcae2018-09-25 12:54:37 +0200274 frame->id.picture_id = rtp_seq_num_unwrapper_.Unwrap(frame->id.picture_id);
philipelafcf7f52017-04-26 08:17:35 -0700275 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700276}
277
philipelafcf7f52017-04-26 08:17:35 -0700278RtpFrameReferenceFinder::FrameDecision RtpFrameReferenceFinder::ManageFrameVp8(
279 RtpFrameObject* frame) {
Johannes Krona3705562019-08-26 16:37:11 +0200280 const RTPVideoHeader& video_header = frame->GetRtpVideoHeader();
281 RTPVideoTypeHeader rtp_codec_header = video_header.video_type_header;
philipel02447bc2016-05-13 06:01:03 -0700282
philipel1a4746a2018-07-09 15:52:29 +0200283 const RTPVideoHeaderVP8& codec_header =
Sami Kalliomäki98824952018-08-28 14:39:21 +0200284 absl::get<RTPVideoHeaderVP8>(rtp_codec_header);
philipel02447bc2016-05-13 06:01:03 -0700285
286 if (codec_header.pictureId == kNoPictureId ||
287 codec_header.temporalIdx == kNoTemporalIdx ||
288 codec_header.tl0PicIdx == kNoTl0PicIdx) {
Mirko Bonadei05cf6be2019-01-31 21:38:12 +0100289 return ManageFramePidOrSeqNum(frame, codec_header.pictureId);
philipel02447bc2016-05-13 06:01:03 -0700290 }
291
philipel0fa82a62018-03-19 15:34:53 +0100292 frame->id.picture_id = codec_header.pictureId % kPicIdLength;
philipel02447bc2016-05-13 06:01:03 -0700293
philipel02447bc2016-05-13 06:01:03 -0700294 if (last_picture_id_ == -1)
philipel0fa82a62018-03-19 15:34:53 +0100295 last_picture_id_ = frame->id.picture_id;
philipel02447bc2016-05-13 06:01:03 -0700296
297 // Find if there has been a gap in fully received frames and save the picture
298 // id of those frames in |not_yet_received_frames_|.
philipel0fa82a62018-03-19 15:34:53 +0100299 if (AheadOf<uint16_t, kPicIdLength>(frame->id.picture_id, last_picture_id_)) {
philipel9bd1d662017-07-14 04:52:01 -0700300 do {
philipel02447bc2016-05-13 06:01:03 -0700301 last_picture_id_ = Add<kPicIdLength>(last_picture_id_, 1);
philipel9bd1d662017-07-14 04:52:01 -0700302 not_yet_received_frames_.insert(last_picture_id_);
philipel0fa82a62018-03-19 15:34:53 +0100303 } while (last_picture_id_ != frame->id.picture_id);
philipel02447bc2016-05-13 06:01:03 -0700304 }
305
philipel57ec6852018-07-03 18:09:32 +0200306 int64_t unwrapped_tl0 = tl0_unwrapper_.Unwrap(codec_header.tl0PicIdx);
307
philipel02447bc2016-05-13 06:01:03 -0700308 // Clean up info for base layers that are too old.
philipel57ec6852018-07-03 18:09:32 +0200309 int64_t old_tl0_pic_idx = unwrapped_tl0 - kMaxLayerInfo;
philipel02447bc2016-05-13 06:01:03 -0700310 auto clean_layer_info_to = layer_info_.lower_bound(old_tl0_pic_idx);
311 layer_info_.erase(layer_info_.begin(), clean_layer_info_to);
312
313 // Clean up info about not yet received frames that are too old.
314 uint16_t old_picture_id =
philipel0fa82a62018-03-19 15:34:53 +0100315 Subtract<kPicIdLength>(frame->id.picture_id, kMaxNotYetReceivedFrames);
philipel02447bc2016-05-13 06:01:03 -0700316 auto clean_frames_to = not_yet_received_frames_.lower_bound(old_picture_id);
317 not_yet_received_frames_.erase(not_yet_received_frames_.begin(),
318 clean_frames_to);
319
Niels Möller8f7ce222019-03-21 15:43:58 +0100320 if (frame->frame_type() == VideoFrameType::kVideoFrameKey) {
philipel02447bc2016-05-13 06:01:03 -0700321 frame->num_references = 0;
philipel57ec6852018-07-03 18:09:32 +0200322 layer_info_[unwrapped_tl0].fill(-1);
323 UpdateLayerInfoVp8(frame, unwrapped_tl0, codec_header.temporalIdx);
philipelafcf7f52017-04-26 08:17:35 -0700324 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700325 }
326
philipel57ec6852018-07-03 18:09:32 +0200327 auto layer_info_it = layer_info_.find(
328 codec_header.temporalIdx == 0 ? unwrapped_tl0 - 1 : unwrapped_tl0);
philipel02447bc2016-05-13 06:01:03 -0700329
330 // If we don't have the base layer frame yet, stash this frame.
philipelafcf7f52017-04-26 08:17:35 -0700331 if (layer_info_it == layer_info_.end())
332 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700333
334 // A non keyframe base layer frame has been received, copy the layer info
335 // from the previous base layer frame and set a reference to the previous
336 // base layer frame.
337 if (codec_header.temporalIdx == 0) {
338 layer_info_it =
philipel57ec6852018-07-03 18:09:32 +0200339 layer_info_.emplace(unwrapped_tl0, layer_info_it->second).first;
philipel02447bc2016-05-13 06:01:03 -0700340 frame->num_references = 1;
341 frame->references[0] = layer_info_it->second[0];
philipel57ec6852018-07-03 18:09:32 +0200342 UpdateLayerInfoVp8(frame, unwrapped_tl0, codec_header.temporalIdx);
philipelafcf7f52017-04-26 08:17:35 -0700343 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700344 }
345
346 // Layer sync frame, this frame only references its base layer frame.
347 if (codec_header.layerSync) {
348 frame->num_references = 1;
349 frame->references[0] = layer_info_it->second[0];
350
philipel57ec6852018-07-03 18:09:32 +0200351 UpdateLayerInfoVp8(frame, unwrapped_tl0, codec_header.temporalIdx);
philipelafcf7f52017-04-26 08:17:35 -0700352 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700353 }
354
355 // Find all references for this frame.
356 frame->num_references = 0;
357 for (uint8_t layer = 0; layer <= codec_header.temporalIdx; ++layer) {
philipeld268d6f2016-09-15 13:43:13 +0200358 // If we have not yet received a previous frame on this temporal layer,
359 // stash this frame.
philipelafcf7f52017-04-26 08:17:35 -0700360 if (layer_info_it->second[layer] == -1)
361 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700362
philipel86b92e02016-10-24 07:11:53 -0700363 // If the last frame on this layer is ahead of this frame it means that
364 // a layer sync frame has been received after this frame for the same
365 // base layer frame, drop this frame.
366 if (AheadOf<uint16_t, kPicIdLength>(layer_info_it->second[layer],
philipel0fa82a62018-03-19 15:34:53 +0100367 frame->id.picture_id)) {
philipelafcf7f52017-04-26 08:17:35 -0700368 return kDrop;
philipel86b92e02016-10-24 07:11:53 -0700369 }
370
philipel02447bc2016-05-13 06:01:03 -0700371 // If we have not yet received a frame between this frame and the referenced
372 // frame then we have to wait for that frame to be completed first.
373 auto not_received_frame_it =
374 not_yet_received_frames_.upper_bound(layer_info_it->second[layer]);
375 if (not_received_frame_it != not_yet_received_frames_.end() &&
philipel0fa82a62018-03-19 15:34:53 +0100376 AheadOf<uint16_t, kPicIdLength>(frame->id.picture_id,
philipel02447bc2016-05-13 06:01:03 -0700377 *not_received_frame_it)) {
philipelafcf7f52017-04-26 08:17:35 -0700378 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700379 }
380
philipel0fa82a62018-03-19 15:34:53 +0100381 if (!(AheadOf<uint16_t, kPicIdLength>(frame->id.picture_id,
philipel57f19cc2017-03-07 03:54:05 -0800382 layer_info_it->second[layer]))) {
philipel0fa82a62018-03-19 15:34:53 +0100383 RTC_LOG(LS_WARNING) << "Frame with picture id " << frame->id.picture_id
Mirko Bonadei675513b2017-11-09 11:09:25 +0100384 << " and packet range [" << frame->first_seq_num()
385 << ", " << frame->last_seq_num()
386 << "] already received, "
387 << " dropping frame.";
philipelafcf7f52017-04-26 08:17:35 -0700388 return kDrop;
philipel57f19cc2017-03-07 03:54:05 -0800389 }
390
philipel02447bc2016-05-13 06:01:03 -0700391 ++frame->num_references;
392 frame->references[layer] = layer_info_it->second[layer];
393 }
394
philipel57ec6852018-07-03 18:09:32 +0200395 UpdateLayerInfoVp8(frame, unwrapped_tl0, codec_header.temporalIdx);
philipelafcf7f52017-04-26 08:17:35 -0700396 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700397}
398
philipel57ec6852018-07-03 18:09:32 +0200399void RtpFrameReferenceFinder::UpdateLayerInfoVp8(RtpFrameObject* frame,
400 int64_t unwrapped_tl0,
401 uint8_t temporal_idx) {
402 auto layer_info_it = layer_info_.find(unwrapped_tl0);
philipel02447bc2016-05-13 06:01:03 -0700403
404 // Update this layer info and newer.
405 while (layer_info_it != layer_info_.end()) {
philipel57ec6852018-07-03 18:09:32 +0200406 if (layer_info_it->second[temporal_idx] != -1 &&
407 AheadOf<uint16_t, kPicIdLength>(layer_info_it->second[temporal_idx],
philipel0fa82a62018-03-19 15:34:53 +0100408 frame->id.picture_id)) {
philipel02447bc2016-05-13 06:01:03 -0700409 // The frame was not newer, then no subsequent layer info have to be
410 // update.
411 break;
412 }
413
philipel57ec6852018-07-03 18:09:32 +0200414 layer_info_it->second[temporal_idx] = frame->id.picture_id;
415 ++unwrapped_tl0;
416 layer_info_it = layer_info_.find(unwrapped_tl0);
philipel02447bc2016-05-13 06:01:03 -0700417 }
philipel0fa82a62018-03-19 15:34:53 +0100418 not_yet_received_frames_.erase(frame->id.picture_id);
philipel02447bc2016-05-13 06:01:03 -0700419
philipelafcf7f52017-04-26 08:17:35 -0700420 UnwrapPictureIds(frame);
philipel02447bc2016-05-13 06:01:03 -0700421}
422
philipelafcf7f52017-04-26 08:17:35 -0700423RtpFrameReferenceFinder::FrameDecision RtpFrameReferenceFinder::ManageFrameVp9(
424 RtpFrameObject* frame) {
Johannes Krona3705562019-08-26 16:37:11 +0200425 const RTPVideoHeader& video_header = frame->GetRtpVideoHeader();
426 RTPVideoTypeHeader rtp_codec_header = video_header.video_type_header;
philipel4c140092017-08-31 08:31:45 -0700427
philipel1a4746a2018-07-09 15:52:29 +0200428 const RTPVideoHeaderVP9& codec_header =
Sami Kalliomäki98824952018-08-28 14:39:21 +0200429 absl::get<RTPVideoHeaderVP9>(rtp_codec_header);
philipel02447bc2016-05-13 06:01:03 -0700430
philipel647998c2016-06-03 09:40:16 -0700431 if (codec_header.picture_id == kNoPictureId ||
Sergey Silkind34a1882018-08-20 16:46:05 +0200432 codec_header.temporal_idx == kNoTemporalIdx) {
Mirko Bonadei05cf6be2019-01-31 21:38:12 +0100433 return ManageFramePidOrSeqNum(frame, codec_header.picture_id);
philipel02447bc2016-05-13 06:01:03 -0700434 }
435
philipel0fa82a62018-03-19 15:34:53 +0100436 frame->id.spatial_layer = codec_header.spatial_idx;
philipel02447bc2016-05-13 06:01:03 -0700437 frame->inter_layer_predicted = codec_header.inter_layer_predicted;
philipel0fa82a62018-03-19 15:34:53 +0100438 frame->id.picture_id = codec_header.picture_id % kPicIdLength;
philipel02447bc2016-05-13 06:01:03 -0700439
philipel02447bc2016-05-13 06:01:03 -0700440 if (last_picture_id_ == -1)
philipel0fa82a62018-03-19 15:34:53 +0100441 last_picture_id_ = frame->id.picture_id;
philipel02447bc2016-05-13 06:01:03 -0700442
443 if (codec_header.flexible_mode) {
444 frame->num_references = codec_header.num_ref_pics;
445 for (size_t i = 0; i < frame->num_references; ++i) {
philipel0fa82a62018-03-19 15:34:53 +0100446 frame->references[i] = Subtract<kPicIdLength>(frame->id.picture_id,
447 codec_header.pid_diff[i]);
philipel02447bc2016-05-13 06:01:03 -0700448 }
449
philipelafcf7f52017-04-26 08:17:35 -0700450 UnwrapPictureIds(frame);
451 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700452 }
453
Sergey Silkind34a1882018-08-20 16:46:05 +0200454 if (codec_header.tl0_pic_idx == kNoTl0PicIdx) {
455 RTC_LOG(LS_WARNING) << "TL0PICIDX is expected to be present in "
456 "non-flexible mode.";
457 return kDrop;
458 }
459
philipel15643602018-05-03 16:14:13 +0200460 GofInfo* info;
philipel57ec6852018-07-03 18:09:32 +0200461 int64_t unwrapped_tl0 = tl0_unwrapper_.Unwrap(codec_header.tl0_pic_idx);
philipel02447bc2016-05-13 06:01:03 -0700462 if (codec_header.ss_data_available) {
philipel02447bc2016-05-13 06:01:03 -0700463 if (codec_header.temporal_idx != 0) {
philipel15643602018-05-03 16:14:13 +0200464 RTC_LOG(LS_WARNING) << "Received scalability structure on a non base "
465 "layer frame. Scalability structure ignored.";
philipel02447bc2016-05-13 06:01:03 -0700466 } else {
Sergey Silkin2f864fb2018-09-07 11:49:38 +0200467 if (codec_header.gof.num_frames_in_gof > kMaxVp9FramesInGof) {
Natalie Silvanovich3ea3e302018-05-16 11:03:12 -0700468 return kDrop;
469 }
470
Sergey Silkin2f864fb2018-09-07 11:49:38 +0200471 GofInfoVP9 gof = codec_header.gof;
472 if (gof.num_frames_in_gof == 0) {
473 RTC_LOG(LS_WARNING) << "Number of frames in GOF is zero. Assume "
474 "that stream has only one temporal layer.";
475 gof.SetGofInfoVP9(kTemporalStructureMode1);
476 }
477
478 current_ss_idx_ = Add<kMaxGofSaved>(current_ss_idx_, 1);
479 scalability_structures_[current_ss_idx_] = gof;
philipel0fa82a62018-03-19 15:34:53 +0100480 scalability_structures_[current_ss_idx_].pid_start = frame->id.picture_id;
philipel57ec6852018-07-03 18:09:32 +0200481 gof_info_.emplace(unwrapped_tl0,
philipel15643602018-05-03 16:14:13 +0200482 GofInfo(&scalability_structures_[current_ss_idx_],
483 frame->id.picture_id));
philipel02447bc2016-05-13 06:01:03 -0700484 }
philipel15643602018-05-03 16:14:13 +0200485
philipel57ec6852018-07-03 18:09:32 +0200486 const auto gof_info_it = gof_info_.find(unwrapped_tl0);
philipel15643602018-05-03 16:14:13 +0200487 if (gof_info_it == gof_info_.end())
488 return kStash;
489
490 info = &gof_info_it->second;
491
Niels Möller8f7ce222019-03-21 15:43:58 +0100492 if (frame->frame_type() == VideoFrameType::kVideoFrameKey) {
philipel15643602018-05-03 16:14:13 +0200493 frame->num_references = 0;
494 FrameReceivedVp9(frame->id.picture_id, info);
495 UnwrapPictureIds(frame);
496 return kHandOff;
497 }
Niels Möller8f7ce222019-03-21 15:43:58 +0100498 } else if (frame->frame_type() == VideoFrameType::kVideoFrameKey) {
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100499 if (frame->id.spatial_layer == 0) {
philipel15643602018-05-03 16:14:13 +0200500 RTC_LOG(LS_WARNING) << "Received keyframe without scalability structure";
501 return kDrop;
502 }
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100503 const auto gof_info_it = gof_info_.find(unwrapped_tl0);
504 if (gof_info_it == gof_info_.end())
505 return kStash;
philipel15643602018-05-03 16:14:13 +0200506
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100507 info = &gof_info_it->second;
508
Niels Möller8f7ce222019-03-21 15:43:58 +0100509 if (frame->frame_type() == VideoFrameType::kVideoFrameKey) {
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100510 frame->num_references = 0;
511 FrameReceivedVp9(frame->id.picture_id, info);
512 UnwrapPictureIds(frame);
513 return kHandOff;
514 }
515 } else {
philipel57ec6852018-07-03 18:09:32 +0200516 auto gof_info_it = gof_info_.find(
517 (codec_header.temporal_idx == 0) ? unwrapped_tl0 - 1 : unwrapped_tl0);
philipel15643602018-05-03 16:14:13 +0200518
519 // Gof info for this frame is not available yet, stash this frame.
520 if (gof_info_it == gof_info_.end())
521 return kStash;
522
523 if (codec_header.temporal_idx == 0) {
philipel57ec6852018-07-03 18:09:32 +0200524 gof_info_it = gof_info_
525 .emplace(unwrapped_tl0, GofInfo(gof_info_it->second.gof,
526 frame->id.picture_id))
527 .first;
philipel15643602018-05-03 16:14:13 +0200528 }
529
530 info = &gof_info_it->second;
philipel02447bc2016-05-13 06:01:03 -0700531 }
532
533 // Clean up info for base layers that are too old.
philipel57ec6852018-07-03 18:09:32 +0200534 int64_t old_tl0_pic_idx = unwrapped_tl0 - kMaxGofSaved;
philipel02447bc2016-05-13 06:01:03 -0700535 auto clean_gof_info_to = gof_info_.lower_bound(old_tl0_pic_idx);
536 gof_info_.erase(gof_info_.begin(), clean_gof_info_to);
537
philipel0fa82a62018-03-19 15:34:53 +0100538 FrameReceivedVp9(frame->id.picture_id, info);
philipel02447bc2016-05-13 06:01:03 -0700539
540 // Make sure we don't miss any frame that could potentially have the
541 // up switch flag set.
philipel0fa82a62018-03-19 15:34:53 +0100542 if (MissingRequiredFrameVp9(frame->id.picture_id, *info))
philipelafcf7f52017-04-26 08:17:35 -0700543 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700544
philipel15643602018-05-03 16:14:13 +0200545 if (codec_header.temporal_up_switch)
546 up_switch_.emplace(frame->id.picture_id, codec_header.temporal_idx);
philipel02447bc2016-05-13 06:01:03 -0700547
548 // Clean out old info about up switch frames.
philipel0fa82a62018-03-19 15:34:53 +0100549 uint16_t old_picture_id = Subtract<kPicIdLength>(frame->id.picture_id, 50);
philipel02447bc2016-05-13 06:01:03 -0700550 auto up_switch_erase_to = up_switch_.lower_bound(old_picture_id);
551 up_switch_.erase(up_switch_.begin(), up_switch_erase_to);
552
philipelc9b27d52016-07-15 06:50:27 -0700553 size_t diff = ForwardDiff<uint16_t, kPicIdLength>(info->gof->pid_start,
philipel0fa82a62018-03-19 15:34:53 +0100554 frame->id.picture_id);
philipelc9b27d52016-07-15 06:50:27 -0700555 size_t gof_idx = diff % info->gof->num_frames_in_gof;
philipel02447bc2016-05-13 06:01:03 -0700556
557 // Populate references according to the scalability structure.
philipelc9b27d52016-07-15 06:50:27 -0700558 frame->num_references = info->gof->num_ref_pics[gof_idx];
philipel02447bc2016-05-13 06:01:03 -0700559 for (size_t i = 0; i < frame->num_references; ++i) {
philipelc9b27d52016-07-15 06:50:27 -0700560 frame->references[i] = Subtract<kPicIdLength>(
philipel0fa82a62018-03-19 15:34:53 +0100561 frame->id.picture_id, info->gof->pid_diff[gof_idx][i]);
philipel02447bc2016-05-13 06:01:03 -0700562
563 // If this is a reference to a frame earlier than the last up switch point,
564 // then ignore this reference.
philipel0fa82a62018-03-19 15:34:53 +0100565 if (UpSwitchInIntervalVp9(frame->id.picture_id, codec_header.temporal_idx,
philipel02447bc2016-05-13 06:01:03 -0700566 frame->references[i])) {
567 --frame->num_references;
568 }
569 }
570
Ilya Nikolaevskiy2ec0c652019-01-18 11:56:48 +0100571 // Override GOF references.
572 if (!codec_header.inter_pic_predicted) {
573 frame->num_references = 0;
574 }
575
philipelafcf7f52017-04-26 08:17:35 -0700576 UnwrapPictureIds(frame);
577 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700578}
579
580bool RtpFrameReferenceFinder::MissingRequiredFrameVp9(uint16_t picture_id,
philipelc9b27d52016-07-15 06:50:27 -0700581 const GofInfo& info) {
582 size_t diff =
583 ForwardDiff<uint16_t, kPicIdLength>(info.gof->pid_start, picture_id);
584 size_t gof_idx = diff % info.gof->num_frames_in_gof;
585 size_t temporal_idx = info.gof->temporal_idx[gof_idx];
philipel02447bc2016-05-13 06:01:03 -0700586
philipela157e082018-05-02 15:19:01 +0200587 if (temporal_idx >= kMaxTemporalLayers) {
588 RTC_LOG(LS_WARNING) << "At most " << kMaxTemporalLayers << " temporal "
589 << "layers are supported.";
590 return true;
591 }
592
philipel02447bc2016-05-13 06:01:03 -0700593 // For every reference this frame has, check if there is a frame missing in
594 // the interval (|ref_pid|, |picture_id|) in any of the lower temporal
595 // layers. If so, we are missing a required frame.
philipelc9b27d52016-07-15 06:50:27 -0700596 uint8_t num_references = info.gof->num_ref_pics[gof_idx];
philipel02447bc2016-05-13 06:01:03 -0700597 for (size_t i = 0; i < num_references; ++i) {
598 uint16_t ref_pid =
philipelc9b27d52016-07-15 06:50:27 -0700599 Subtract<kPicIdLength>(picture_id, info.gof->pid_diff[gof_idx][i]);
philipel02447bc2016-05-13 06:01:03 -0700600 for (size_t l = 0; l < temporal_idx; ++l) {
601 auto missing_frame_it = missing_frames_for_layer_[l].lower_bound(ref_pid);
602 if (missing_frame_it != missing_frames_for_layer_[l].end() &&
603 AheadOf<uint16_t, kPicIdLength>(picture_id, *missing_frame_it)) {
604 return true;
605 }
606 }
607 }
608 return false;
609}
610
611void RtpFrameReferenceFinder::FrameReceivedVp9(uint16_t picture_id,
philipelc9b27d52016-07-15 06:50:27 -0700612 GofInfo* info) {
613 int last_picture_id = info->last_picture_id;
philipel459f4e32018-03-02 10:55:12 +0100614 size_t gof_size = std::min(info->gof->num_frames_in_gof, kMaxVp9FramesInGof);
philipel02447bc2016-05-13 06:01:03 -0700615
616 // If there is a gap, find which temporal layer the missing frames
617 // belong to and add the frame as missing for that temporal layer.
618 // Otherwise, remove this frame from the set of missing frames.
philipelc9b27d52016-07-15 06:50:27 -0700619 if (AheadOf<uint16_t, kPicIdLength>(picture_id, last_picture_id)) {
620 size_t diff = ForwardDiff<uint16_t, kPicIdLength>(info->gof->pid_start,
621 last_picture_id);
philipel459f4e32018-03-02 10:55:12 +0100622 size_t gof_idx = diff % gof_size;
philipel02447bc2016-05-13 06:01:03 -0700623
philipelc9b27d52016-07-15 06:50:27 -0700624 last_picture_id = Add<kPicIdLength>(last_picture_id, 1);
625 while (last_picture_id != picture_id) {
Yves Gerey665174f2018-06-19 15:03:05 +0200626 gof_idx = (gof_idx + 1) % gof_size;
philipel459f4e32018-03-02 10:55:12 +0100627 RTC_CHECK(gof_idx < kMaxVp9FramesInGof);
628
philipelc9b27d52016-07-15 06:50:27 -0700629 size_t temporal_idx = info->gof->temporal_idx[gof_idx];
philipel459f4e32018-03-02 10:55:12 +0100630 if (temporal_idx >= kMaxTemporalLayers) {
631 RTC_LOG(LS_WARNING) << "At most " << kMaxTemporalLayers << " temporal "
632 << "layers are supported.";
633 return;
634 }
635
philipelc9b27d52016-07-15 06:50:27 -0700636 missing_frames_for_layer_[temporal_idx].insert(last_picture_id);
637 last_picture_id = Add<kPicIdLength>(last_picture_id, 1);
philipel02447bc2016-05-13 06:01:03 -0700638 }
philipel459f4e32018-03-02 10:55:12 +0100639
philipelc9b27d52016-07-15 06:50:27 -0700640 info->last_picture_id = last_picture_id;
philipel02447bc2016-05-13 06:01:03 -0700641 } else {
642 size_t diff =
philipelc9b27d52016-07-15 06:50:27 -0700643 ForwardDiff<uint16_t, kPicIdLength>(info->gof->pid_start, picture_id);
philipel459f4e32018-03-02 10:55:12 +0100644 size_t gof_idx = diff % gof_size;
645 RTC_CHECK(gof_idx < kMaxVp9FramesInGof);
646
philipelc9b27d52016-07-15 06:50:27 -0700647 size_t temporal_idx = info->gof->temporal_idx[gof_idx];
philipel459f4e32018-03-02 10:55:12 +0100648 if (temporal_idx >= kMaxTemporalLayers) {
649 RTC_LOG(LS_WARNING) << "At most " << kMaxTemporalLayers << " temporal "
650 << "layers are supported.";
651 return;
652 }
653
philipel02447bc2016-05-13 06:01:03 -0700654 missing_frames_for_layer_[temporal_idx].erase(picture_id);
655 }
656}
657
658bool RtpFrameReferenceFinder::UpSwitchInIntervalVp9(uint16_t picture_id,
659 uint8_t temporal_idx,
660 uint16_t pid_ref) {
661 for (auto up_switch_it = up_switch_.upper_bound(pid_ref);
662 up_switch_it != up_switch_.end() &&
663 AheadOf<uint16_t, kPicIdLength>(picture_id, up_switch_it->first);
664 ++up_switch_it) {
665 if (up_switch_it->second < temporal_idx)
666 return true;
667 }
668
669 return false;
670}
671
philipelafcf7f52017-04-26 08:17:35 -0700672void RtpFrameReferenceFinder::UnwrapPictureIds(RtpFrameObject* frame) {
philipel02447bc2016-05-13 06:01:03 -0700673 for (size_t i = 0; i < frame->num_references; ++i)
philipeld4fac692017-09-04 07:03:46 -0700674 frame->references[i] = unwrapper_.Unwrap(frame->references[i]);
philipel0fa82a62018-03-19 15:34:53 +0100675 frame->id.picture_id = unwrapper_.Unwrap(frame->id.picture_id);
philipel02447bc2016-05-13 06:01:03 -0700676}
677
Johnny Leebc7f41b2019-05-01 14:41:32 -0400678RtpFrameReferenceFinder::FrameDecision RtpFrameReferenceFinder::ManageFrameH264(
679 RtpFrameObject* frame) {
Johannes Krona3705562019-08-26 16:37:11 +0200680 const FrameMarking& rtp_frame_marking = frame->GetFrameMarking();
Johnny Leebc7f41b2019-05-01 14:41:32 -0400681
Johannes Krona3705562019-08-26 16:37:11 +0200682 uint8_t tid = rtp_frame_marking.temporal_id;
683 bool blSync = rtp_frame_marking.base_layer_sync;
Johnny Leebc7f41b2019-05-01 14:41:32 -0400684
685 if (tid == kNoTemporalIdx)
686 return ManageFramePidOrSeqNum(std::move(frame), kNoPictureId);
687
688 frame->id.picture_id = frame->last_seq_num();
689
690 if (frame->frame_type() == VideoFrameType::kVideoFrameKey) {
691 // For H264, use last_seq_num_gop_ to simply store last picture id
692 // as a pair of unpadded and padded sequence numbers.
693 if (last_seq_num_gop_.empty()) {
694 last_seq_num_gop_.insert(std::make_pair(
695 0, std::make_pair(frame->id.picture_id, frame->id.picture_id)));
696 }
697 }
698
699 // Stash if we have no keyframe yet.
700 if (last_seq_num_gop_.empty())
701 return kStash;
702
703 // Check for gap in sequence numbers. Store in |not_yet_received_seq_num_|.
704 if (frame->frame_type() == VideoFrameType::kVideoFrameDelta) {
705 uint16_t last_pic_id_padded = last_seq_num_gop_.begin()->second.second;
706 if (AheadOf<uint16_t>(frame->id.picture_id, last_pic_id_padded)) {
707 do {
708 last_pic_id_padded = last_pic_id_padded + 1;
709 not_yet_received_seq_num_.insert(last_pic_id_padded);
710 } while (last_pic_id_padded != frame->id.picture_id);
711 }
712 }
713
Johannes Krona3705562019-08-26 16:37:11 +0200714 int64_t unwrapped_tl0 = tl0_unwrapper_.Unwrap(rtp_frame_marking.tl0_pic_idx);
Johnny Leebc7f41b2019-05-01 14:41:32 -0400715
716 // Clean up info for base layers that are too old.
717 int64_t old_tl0_pic_idx = unwrapped_tl0 - kMaxLayerInfo;
718 auto clean_layer_info_to = layer_info_.lower_bound(old_tl0_pic_idx);
719 layer_info_.erase(layer_info_.begin(), clean_layer_info_to);
720
721 // Clean up info about not yet received frames that are too old.
722 uint16_t old_picture_id = frame->id.picture_id - kMaxNotYetReceivedFrames * 2;
723 auto clean_frames_to = not_yet_received_seq_num_.lower_bound(old_picture_id);
724 not_yet_received_seq_num_.erase(not_yet_received_seq_num_.begin(),
725 clean_frames_to);
726
727 if (frame->frame_type() == VideoFrameType::kVideoFrameKey) {
728 frame->num_references = 0;
729 layer_info_[unwrapped_tl0].fill(-1);
730 UpdateDataH264(frame, unwrapped_tl0, tid);
731 return kHandOff;
732 }
733
Jonas Olssona4d87372019-07-05 19:08:33 +0200734 auto layer_info_it =
735 layer_info_.find(tid == 0 ? unwrapped_tl0 - 1 : unwrapped_tl0);
Johnny Leebc7f41b2019-05-01 14:41:32 -0400736
737 // Stash if we have no base layer frame yet.
738 if (layer_info_it == layer_info_.end())
739 return kStash;
740
741 // Base layer frame. Copy layer info from previous base layer frame.
742 if (tid == 0) {
Jonas Olssona4d87372019-07-05 19:08:33 +0200743 layer_info_it =
744 layer_info_.insert(std::make_pair(unwrapped_tl0, layer_info_it->second))
745 .first;
Johnny Leebc7f41b2019-05-01 14:41:32 -0400746 frame->num_references = 1;
747 frame->references[0] = layer_info_it->second[0];
748 UpdateDataH264(frame, unwrapped_tl0, tid);
749 return kHandOff;
750 }
751
752 // This frame only references its base layer frame.
753 if (blSync) {
754 frame->num_references = 1;
755 frame->references[0] = layer_info_it->second[0];
756 UpdateDataH264(frame, unwrapped_tl0, tid);
757 return kHandOff;
758 }
759
760 // Find all references for general frame.
761 frame->num_references = 0;
762 for (uint8_t layer = 0; layer <= tid; ++layer) {
763 // Stash if we have not yet received frames on this temporal layer.
764 if (layer_info_it->second[layer] == -1)
765 return kStash;
766
767 // Drop if the last frame on this layer is ahead of this frame. A layer sync
768 // frame was received after this frame for the same base layer frame.
769 uint16_t last_frame_in_layer = layer_info_it->second[layer];
770 if (AheadOf<uint16_t>(last_frame_in_layer, frame->id.picture_id))
771 return kDrop;
772
773 // Stash and wait for missing frame between this frame and the reference
774 auto not_received_seq_num_it =
775 not_yet_received_seq_num_.upper_bound(last_frame_in_layer);
776 if (not_received_seq_num_it != not_yet_received_seq_num_.end() &&
777 AheadOf<uint16_t>(frame->id.picture_id, *not_received_seq_num_it)) {
778 return kStash;
779 }
780
781 if (!(AheadOf<uint16_t>(frame->id.picture_id, last_frame_in_layer))) {
782 RTC_LOG(LS_WARNING) << "Frame with picture id " << frame->id.picture_id
783 << " and packet range [" << frame->first_seq_num()
784 << ", " << frame->last_seq_num()
785 << "] already received, "
786 << " dropping frame.";
787 return kDrop;
788 }
789
790 ++frame->num_references;
791 frame->references[layer] = last_frame_in_layer;
792 }
793
794 UpdateDataH264(frame, unwrapped_tl0, tid);
795 return kHandOff;
796}
797
798void RtpFrameReferenceFinder::UpdateLastPictureIdWithPaddingH264() {
799 auto seq_num_it = last_seq_num_gop_.begin();
800
801 // Check if next sequence number is in a stashed padding packet.
802 uint16_t next_padded_seq_num = seq_num_it->second.second + 1;
803 auto padding_seq_num_it = stashed_padding_.lower_bound(next_padded_seq_num);
804
805 // Check for more consecutive padding packets to increment
806 // the "last-picture-id-with-padding" and remove the stashed packets.
807 while (padding_seq_num_it != stashed_padding_.end() &&
808 *padding_seq_num_it == next_padded_seq_num) {
809 seq_num_it->second.second = next_padded_seq_num;
810 ++next_padded_seq_num;
811 padding_seq_num_it = stashed_padding_.erase(padding_seq_num_it);
812 }
813}
814
815void RtpFrameReferenceFinder::UpdateLayerInfoH264(RtpFrameObject* frame,
816 int64_t unwrapped_tl0,
817 uint8_t temporal_idx) {
818 auto layer_info_it = layer_info_.find(unwrapped_tl0);
819
820 // Update this layer info and newer.
821 while (layer_info_it != layer_info_.end()) {
822 if (layer_info_it->second[temporal_idx] != -1 &&
823 AheadOf<uint16_t>(layer_info_it->second[temporal_idx],
824 frame->id.picture_id)) {
825 // Not a newer frame. No subsequent layer info needs update.
826 break;
827 }
828
829 layer_info_it->second[temporal_idx] = frame->id.picture_id;
830 ++unwrapped_tl0;
831 layer_info_it = layer_info_.find(unwrapped_tl0);
832 }
833
834 for (size_t i = 0; i < frame->num_references; ++i)
835 frame->references[i] = rtp_seq_num_unwrapper_.Unwrap(frame->references[i]);
836 frame->id.picture_id = rtp_seq_num_unwrapper_.Unwrap(frame->id.picture_id);
837}
838
839void RtpFrameReferenceFinder::UpdateDataH264(RtpFrameObject* frame,
840 int64_t unwrapped_tl0,
841 uint8_t temporal_idx) {
842 // Update last_seq_num_gop_ entry for last picture id.
843 auto seq_num_it = last_seq_num_gop_.begin();
844 uint16_t last_pic_id = seq_num_it->second.first;
845 if (AheadOf<uint16_t>(frame->id.picture_id, last_pic_id)) {
846 seq_num_it->second.first = frame->id.picture_id;
847 seq_num_it->second.second = frame->id.picture_id;
848 }
849 UpdateLastPictureIdWithPaddingH264();
850
851 UpdateLayerInfoH264(frame, unwrapped_tl0, temporal_idx);
852
853 // Remove any current packets from |not_yet_received_seq_num_|.
854 uint16_t last_seq_num_padded = seq_num_it->second.second;
Jonas Olssona4d87372019-07-05 19:08:33 +0200855 for (uint16_t n = frame->first_seq_num(); AheadOrAt(last_seq_num_padded, n);
856 ++n) {
Johnny Leebc7f41b2019-05-01 14:41:32 -0400857 not_yet_received_seq_num_.erase(n);
858 }
859}
860
philipel02447bc2016-05-13 06:01:03 -0700861} // namespace video_coding
862} // namespace webrtc