blob: af474d5ce88b4d822646e431e1bf6e8bfa07b84a [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.
philipel2837edc2018-10-02 13:55:47 +0200102 absl::optional<RTPVideoHeader> video_header = frame->GetRtpVideoHeader();
philipeldabfcae2018-09-25 12:54:37 +0200103 int picture_id = kNoPictureId;
104 if (video_header && video_header->generic)
105 picture_id = video_header->generic->frame_id & 0x7fff;
106
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) {
philipel5470f402018-09-07 13:38:53 +0200268 absl::optional<RTPVideoHeader> video_header = frame->GetRtpVideoHeader();
Sami Kalliomäki98824952018-08-28 14:39:21 +0200269 if (!video_header) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100270 RTC_LOG(LS_WARNING)
271 << "Failed to get codec header from frame, dropping frame.";
philipelafcf7f52017-04-26 08:17:35 -0700272 return kDrop;
philipeld4fac692017-09-04 07:03:46 -0700273 }
Sami Kalliomäki98824952018-08-28 14:39:21 +0200274 RTPVideoTypeHeader rtp_codec_header = video_header->video_type_header;
philipel02447bc2016-05-13 06:01:03 -0700275
philipel1a4746a2018-07-09 15:52:29 +0200276 const RTPVideoHeaderVP8& codec_header =
Sami Kalliomäki98824952018-08-28 14:39:21 +0200277 absl::get<RTPVideoHeaderVP8>(rtp_codec_header);
philipel02447bc2016-05-13 06:01:03 -0700278
279 if (codec_header.pictureId == kNoPictureId ||
280 codec_header.temporalIdx == kNoTemporalIdx ||
281 codec_header.tl0PicIdx == kNoTl0PicIdx) {
Mirko Bonadei05cf6be2019-01-31 21:38:12 +0100282 return ManageFramePidOrSeqNum(frame, codec_header.pictureId);
philipel02447bc2016-05-13 06:01:03 -0700283 }
284
philipel0fa82a62018-03-19 15:34:53 +0100285 frame->id.picture_id = codec_header.pictureId % kPicIdLength;
philipel02447bc2016-05-13 06:01:03 -0700286
philipel02447bc2016-05-13 06:01:03 -0700287 if (last_picture_id_ == -1)
philipel0fa82a62018-03-19 15:34:53 +0100288 last_picture_id_ = frame->id.picture_id;
philipel02447bc2016-05-13 06:01:03 -0700289
290 // Find if there has been a gap in fully received frames and save the picture
291 // id of those frames in |not_yet_received_frames_|.
philipel0fa82a62018-03-19 15:34:53 +0100292 if (AheadOf<uint16_t, kPicIdLength>(frame->id.picture_id, last_picture_id_)) {
philipel9bd1d662017-07-14 04:52:01 -0700293 do {
philipel02447bc2016-05-13 06:01:03 -0700294 last_picture_id_ = Add<kPicIdLength>(last_picture_id_, 1);
philipel9bd1d662017-07-14 04:52:01 -0700295 not_yet_received_frames_.insert(last_picture_id_);
philipel0fa82a62018-03-19 15:34:53 +0100296 } while (last_picture_id_ != frame->id.picture_id);
philipel02447bc2016-05-13 06:01:03 -0700297 }
298
philipel57ec6852018-07-03 18:09:32 +0200299 int64_t unwrapped_tl0 = tl0_unwrapper_.Unwrap(codec_header.tl0PicIdx);
300
philipel02447bc2016-05-13 06:01:03 -0700301 // Clean up info for base layers that are too old.
philipel57ec6852018-07-03 18:09:32 +0200302 int64_t old_tl0_pic_idx = unwrapped_tl0 - kMaxLayerInfo;
philipel02447bc2016-05-13 06:01:03 -0700303 auto clean_layer_info_to = layer_info_.lower_bound(old_tl0_pic_idx);
304 layer_info_.erase(layer_info_.begin(), clean_layer_info_to);
305
306 // Clean up info about not yet received frames that are too old.
307 uint16_t old_picture_id =
philipel0fa82a62018-03-19 15:34:53 +0100308 Subtract<kPicIdLength>(frame->id.picture_id, kMaxNotYetReceivedFrames);
philipel02447bc2016-05-13 06:01:03 -0700309 auto clean_frames_to = not_yet_received_frames_.lower_bound(old_picture_id);
310 not_yet_received_frames_.erase(not_yet_received_frames_.begin(),
311 clean_frames_to);
312
Niels Möller8f7ce222019-03-21 15:43:58 +0100313 if (frame->frame_type() == VideoFrameType::kVideoFrameKey) {
philipel02447bc2016-05-13 06:01:03 -0700314 frame->num_references = 0;
philipel57ec6852018-07-03 18:09:32 +0200315 layer_info_[unwrapped_tl0].fill(-1);
316 UpdateLayerInfoVp8(frame, unwrapped_tl0, codec_header.temporalIdx);
philipelafcf7f52017-04-26 08:17:35 -0700317 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700318 }
319
philipel57ec6852018-07-03 18:09:32 +0200320 auto layer_info_it = layer_info_.find(
321 codec_header.temporalIdx == 0 ? unwrapped_tl0 - 1 : unwrapped_tl0);
philipel02447bc2016-05-13 06:01:03 -0700322
323 // If we don't have the base layer frame yet, stash this frame.
philipelafcf7f52017-04-26 08:17:35 -0700324 if (layer_info_it == layer_info_.end())
325 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700326
327 // A non keyframe base layer frame has been received, copy the layer info
328 // from the previous base layer frame and set a reference to the previous
329 // base layer frame.
330 if (codec_header.temporalIdx == 0) {
331 layer_info_it =
philipel57ec6852018-07-03 18:09:32 +0200332 layer_info_.emplace(unwrapped_tl0, layer_info_it->second).first;
philipel02447bc2016-05-13 06:01:03 -0700333 frame->num_references = 1;
334 frame->references[0] = layer_info_it->second[0];
philipel57ec6852018-07-03 18:09:32 +0200335 UpdateLayerInfoVp8(frame, unwrapped_tl0, codec_header.temporalIdx);
philipelafcf7f52017-04-26 08:17:35 -0700336 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700337 }
338
339 // Layer sync frame, this frame only references its base layer frame.
340 if (codec_header.layerSync) {
341 frame->num_references = 1;
342 frame->references[0] = layer_info_it->second[0];
343
philipel57ec6852018-07-03 18:09:32 +0200344 UpdateLayerInfoVp8(frame, unwrapped_tl0, codec_header.temporalIdx);
philipelafcf7f52017-04-26 08:17:35 -0700345 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700346 }
347
348 // Find all references for this frame.
349 frame->num_references = 0;
350 for (uint8_t layer = 0; layer <= codec_header.temporalIdx; ++layer) {
philipeld268d6f2016-09-15 13:43:13 +0200351 // If we have not yet received a previous frame on this temporal layer,
352 // stash this frame.
philipelafcf7f52017-04-26 08:17:35 -0700353 if (layer_info_it->second[layer] == -1)
354 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700355
philipel86b92e02016-10-24 07:11:53 -0700356 // If the last frame on this layer is ahead of this frame it means that
357 // a layer sync frame has been received after this frame for the same
358 // base layer frame, drop this frame.
359 if (AheadOf<uint16_t, kPicIdLength>(layer_info_it->second[layer],
philipel0fa82a62018-03-19 15:34:53 +0100360 frame->id.picture_id)) {
philipelafcf7f52017-04-26 08:17:35 -0700361 return kDrop;
philipel86b92e02016-10-24 07:11:53 -0700362 }
363
philipel02447bc2016-05-13 06:01:03 -0700364 // If we have not yet received a frame between this frame and the referenced
365 // frame then we have to wait for that frame to be completed first.
366 auto not_received_frame_it =
367 not_yet_received_frames_.upper_bound(layer_info_it->second[layer]);
368 if (not_received_frame_it != not_yet_received_frames_.end() &&
philipel0fa82a62018-03-19 15:34:53 +0100369 AheadOf<uint16_t, kPicIdLength>(frame->id.picture_id,
philipel02447bc2016-05-13 06:01:03 -0700370 *not_received_frame_it)) {
philipelafcf7f52017-04-26 08:17:35 -0700371 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700372 }
373
philipel0fa82a62018-03-19 15:34:53 +0100374 if (!(AheadOf<uint16_t, kPicIdLength>(frame->id.picture_id,
philipel57f19cc2017-03-07 03:54:05 -0800375 layer_info_it->second[layer]))) {
philipel0fa82a62018-03-19 15:34:53 +0100376 RTC_LOG(LS_WARNING) << "Frame with picture id " << frame->id.picture_id
Mirko Bonadei675513b2017-11-09 11:09:25 +0100377 << " and packet range [" << frame->first_seq_num()
378 << ", " << frame->last_seq_num()
379 << "] already received, "
380 << " dropping frame.";
philipelafcf7f52017-04-26 08:17:35 -0700381 return kDrop;
philipel57f19cc2017-03-07 03:54:05 -0800382 }
383
philipel02447bc2016-05-13 06:01:03 -0700384 ++frame->num_references;
385 frame->references[layer] = layer_info_it->second[layer];
386 }
387
philipel57ec6852018-07-03 18:09:32 +0200388 UpdateLayerInfoVp8(frame, unwrapped_tl0, codec_header.temporalIdx);
philipelafcf7f52017-04-26 08:17:35 -0700389 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700390}
391
philipel57ec6852018-07-03 18:09:32 +0200392void RtpFrameReferenceFinder::UpdateLayerInfoVp8(RtpFrameObject* frame,
393 int64_t unwrapped_tl0,
394 uint8_t temporal_idx) {
395 auto layer_info_it = layer_info_.find(unwrapped_tl0);
philipel02447bc2016-05-13 06:01:03 -0700396
397 // Update this layer info and newer.
398 while (layer_info_it != layer_info_.end()) {
philipel57ec6852018-07-03 18:09:32 +0200399 if (layer_info_it->second[temporal_idx] != -1 &&
400 AheadOf<uint16_t, kPicIdLength>(layer_info_it->second[temporal_idx],
philipel0fa82a62018-03-19 15:34:53 +0100401 frame->id.picture_id)) {
philipel02447bc2016-05-13 06:01:03 -0700402 // The frame was not newer, then no subsequent layer info have to be
403 // update.
404 break;
405 }
406
philipel57ec6852018-07-03 18:09:32 +0200407 layer_info_it->second[temporal_idx] = frame->id.picture_id;
408 ++unwrapped_tl0;
409 layer_info_it = layer_info_.find(unwrapped_tl0);
philipel02447bc2016-05-13 06:01:03 -0700410 }
philipel0fa82a62018-03-19 15:34:53 +0100411 not_yet_received_frames_.erase(frame->id.picture_id);
philipel02447bc2016-05-13 06:01:03 -0700412
philipelafcf7f52017-04-26 08:17:35 -0700413 UnwrapPictureIds(frame);
philipel02447bc2016-05-13 06:01:03 -0700414}
415
philipelafcf7f52017-04-26 08:17:35 -0700416RtpFrameReferenceFinder::FrameDecision RtpFrameReferenceFinder::ManageFrameVp9(
417 RtpFrameObject* frame) {
philipel5470f402018-09-07 13:38:53 +0200418 absl::optional<RTPVideoHeader> video_header = frame->GetRtpVideoHeader();
Sami Kalliomäki98824952018-08-28 14:39:21 +0200419 if (!video_header) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100420 RTC_LOG(LS_WARNING)
421 << "Failed to get codec header from frame, dropping frame.";
philipel4c140092017-08-31 08:31:45 -0700422 return kDrop;
philipeld4fac692017-09-04 07:03:46 -0700423 }
Sami Kalliomäki98824952018-08-28 14:39:21 +0200424 RTPVideoTypeHeader rtp_codec_header = video_header->video_type_header;
philipel4c140092017-08-31 08:31:45 -0700425
philipel1a4746a2018-07-09 15:52:29 +0200426 const RTPVideoHeaderVP9& codec_header =
Sami Kalliomäki98824952018-08-28 14:39:21 +0200427 absl::get<RTPVideoHeaderVP9>(rtp_codec_header);
philipel02447bc2016-05-13 06:01:03 -0700428
philipel647998c2016-06-03 09:40:16 -0700429 if (codec_header.picture_id == kNoPictureId ||
Sergey Silkind34a1882018-08-20 16:46:05 +0200430 codec_header.temporal_idx == kNoTemporalIdx) {
Mirko Bonadei05cf6be2019-01-31 21:38:12 +0100431 return ManageFramePidOrSeqNum(frame, codec_header.picture_id);
philipel02447bc2016-05-13 06:01:03 -0700432 }
433
philipel0fa82a62018-03-19 15:34:53 +0100434 frame->id.spatial_layer = codec_header.spatial_idx;
philipel02447bc2016-05-13 06:01:03 -0700435 frame->inter_layer_predicted = codec_header.inter_layer_predicted;
philipel0fa82a62018-03-19 15:34:53 +0100436 frame->id.picture_id = codec_header.picture_id % kPicIdLength;
philipel02447bc2016-05-13 06:01:03 -0700437
philipel02447bc2016-05-13 06:01:03 -0700438 if (last_picture_id_ == -1)
philipel0fa82a62018-03-19 15:34:53 +0100439 last_picture_id_ = frame->id.picture_id;
philipel02447bc2016-05-13 06:01:03 -0700440
441 if (codec_header.flexible_mode) {
442 frame->num_references = codec_header.num_ref_pics;
443 for (size_t i = 0; i < frame->num_references; ++i) {
philipel0fa82a62018-03-19 15:34:53 +0100444 frame->references[i] = Subtract<kPicIdLength>(frame->id.picture_id,
445 codec_header.pid_diff[i]);
philipel02447bc2016-05-13 06:01:03 -0700446 }
447
philipelafcf7f52017-04-26 08:17:35 -0700448 UnwrapPictureIds(frame);
449 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700450 }
451
Sergey Silkind34a1882018-08-20 16:46:05 +0200452 if (codec_header.tl0_pic_idx == kNoTl0PicIdx) {
453 RTC_LOG(LS_WARNING) << "TL0PICIDX is expected to be present in "
454 "non-flexible mode.";
455 return kDrop;
456 }
457
philipel15643602018-05-03 16:14:13 +0200458 GofInfo* info;
philipel57ec6852018-07-03 18:09:32 +0200459 int64_t unwrapped_tl0 = tl0_unwrapper_.Unwrap(codec_header.tl0_pic_idx);
philipel02447bc2016-05-13 06:01:03 -0700460 if (codec_header.ss_data_available) {
philipel02447bc2016-05-13 06:01:03 -0700461 if (codec_header.temporal_idx != 0) {
philipel15643602018-05-03 16:14:13 +0200462 RTC_LOG(LS_WARNING) << "Received scalability structure on a non base "
463 "layer frame. Scalability structure ignored.";
philipel02447bc2016-05-13 06:01:03 -0700464 } else {
Sergey Silkin2f864fb2018-09-07 11:49:38 +0200465 if (codec_header.gof.num_frames_in_gof > kMaxVp9FramesInGof) {
Natalie Silvanovich3ea3e302018-05-16 11:03:12 -0700466 return kDrop;
467 }
468
Sergey Silkin2f864fb2018-09-07 11:49:38 +0200469 GofInfoVP9 gof = codec_header.gof;
470 if (gof.num_frames_in_gof == 0) {
471 RTC_LOG(LS_WARNING) << "Number of frames in GOF is zero. Assume "
472 "that stream has only one temporal layer.";
473 gof.SetGofInfoVP9(kTemporalStructureMode1);
474 }
475
476 current_ss_idx_ = Add<kMaxGofSaved>(current_ss_idx_, 1);
477 scalability_structures_[current_ss_idx_] = gof;
philipel0fa82a62018-03-19 15:34:53 +0100478 scalability_structures_[current_ss_idx_].pid_start = frame->id.picture_id;
philipel57ec6852018-07-03 18:09:32 +0200479 gof_info_.emplace(unwrapped_tl0,
philipel15643602018-05-03 16:14:13 +0200480 GofInfo(&scalability_structures_[current_ss_idx_],
481 frame->id.picture_id));
philipel02447bc2016-05-13 06:01:03 -0700482 }
philipel15643602018-05-03 16:14:13 +0200483
philipel57ec6852018-07-03 18:09:32 +0200484 const auto gof_info_it = gof_info_.find(unwrapped_tl0);
philipel15643602018-05-03 16:14:13 +0200485 if (gof_info_it == gof_info_.end())
486 return kStash;
487
488 info = &gof_info_it->second;
489
Niels Möller8f7ce222019-03-21 15:43:58 +0100490 if (frame->frame_type() == VideoFrameType::kVideoFrameKey) {
philipel15643602018-05-03 16:14:13 +0200491 frame->num_references = 0;
492 FrameReceivedVp9(frame->id.picture_id, info);
493 UnwrapPictureIds(frame);
494 return kHandOff;
495 }
Niels Möller8f7ce222019-03-21 15:43:58 +0100496 } else if (frame->frame_type() == VideoFrameType::kVideoFrameKey) {
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100497 if (frame->id.spatial_layer == 0) {
philipel15643602018-05-03 16:14:13 +0200498 RTC_LOG(LS_WARNING) << "Received keyframe without scalability structure";
499 return kDrop;
500 }
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100501 const auto gof_info_it = gof_info_.find(unwrapped_tl0);
502 if (gof_info_it == gof_info_.end())
503 return kStash;
philipel15643602018-05-03 16:14:13 +0200504
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100505 info = &gof_info_it->second;
506
Niels Möller8f7ce222019-03-21 15:43:58 +0100507 if (frame->frame_type() == VideoFrameType::kVideoFrameKey) {
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100508 frame->num_references = 0;
509 FrameReceivedVp9(frame->id.picture_id, info);
510 UnwrapPictureIds(frame);
511 return kHandOff;
512 }
513 } else {
philipel57ec6852018-07-03 18:09:32 +0200514 auto gof_info_it = gof_info_.find(
515 (codec_header.temporal_idx == 0) ? unwrapped_tl0 - 1 : unwrapped_tl0);
philipel15643602018-05-03 16:14:13 +0200516
517 // Gof info for this frame is not available yet, stash this frame.
518 if (gof_info_it == gof_info_.end())
519 return kStash;
520
521 if (codec_header.temporal_idx == 0) {
philipel57ec6852018-07-03 18:09:32 +0200522 gof_info_it = gof_info_
523 .emplace(unwrapped_tl0, GofInfo(gof_info_it->second.gof,
524 frame->id.picture_id))
525 .first;
philipel15643602018-05-03 16:14:13 +0200526 }
527
528 info = &gof_info_it->second;
philipel02447bc2016-05-13 06:01:03 -0700529 }
530
531 // Clean up info for base layers that are too old.
philipel57ec6852018-07-03 18:09:32 +0200532 int64_t old_tl0_pic_idx = unwrapped_tl0 - kMaxGofSaved;
philipel02447bc2016-05-13 06:01:03 -0700533 auto clean_gof_info_to = gof_info_.lower_bound(old_tl0_pic_idx);
534 gof_info_.erase(gof_info_.begin(), clean_gof_info_to);
535
philipel0fa82a62018-03-19 15:34:53 +0100536 FrameReceivedVp9(frame->id.picture_id, info);
philipel02447bc2016-05-13 06:01:03 -0700537
538 // Make sure we don't miss any frame that could potentially have the
539 // up switch flag set.
philipel0fa82a62018-03-19 15:34:53 +0100540 if (MissingRequiredFrameVp9(frame->id.picture_id, *info))
philipelafcf7f52017-04-26 08:17:35 -0700541 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700542
philipel15643602018-05-03 16:14:13 +0200543 if (codec_header.temporal_up_switch)
544 up_switch_.emplace(frame->id.picture_id, codec_header.temporal_idx);
philipel02447bc2016-05-13 06:01:03 -0700545
546 // Clean out old info about up switch frames.
philipel0fa82a62018-03-19 15:34:53 +0100547 uint16_t old_picture_id = Subtract<kPicIdLength>(frame->id.picture_id, 50);
philipel02447bc2016-05-13 06:01:03 -0700548 auto up_switch_erase_to = up_switch_.lower_bound(old_picture_id);
549 up_switch_.erase(up_switch_.begin(), up_switch_erase_to);
550
philipelc9b27d52016-07-15 06:50:27 -0700551 size_t diff = ForwardDiff<uint16_t, kPicIdLength>(info->gof->pid_start,
philipel0fa82a62018-03-19 15:34:53 +0100552 frame->id.picture_id);
philipelc9b27d52016-07-15 06:50:27 -0700553 size_t gof_idx = diff % info->gof->num_frames_in_gof;
philipel02447bc2016-05-13 06:01:03 -0700554
555 // Populate references according to the scalability structure.
philipelc9b27d52016-07-15 06:50:27 -0700556 frame->num_references = info->gof->num_ref_pics[gof_idx];
philipel02447bc2016-05-13 06:01:03 -0700557 for (size_t i = 0; i < frame->num_references; ++i) {
philipelc9b27d52016-07-15 06:50:27 -0700558 frame->references[i] = Subtract<kPicIdLength>(
philipel0fa82a62018-03-19 15:34:53 +0100559 frame->id.picture_id, info->gof->pid_diff[gof_idx][i]);
philipel02447bc2016-05-13 06:01:03 -0700560
561 // If this is a reference to a frame earlier than the last up switch point,
562 // then ignore this reference.
philipel0fa82a62018-03-19 15:34:53 +0100563 if (UpSwitchInIntervalVp9(frame->id.picture_id, codec_header.temporal_idx,
philipel02447bc2016-05-13 06:01:03 -0700564 frame->references[i])) {
565 --frame->num_references;
566 }
567 }
568
Ilya Nikolaevskiy2ec0c652019-01-18 11:56:48 +0100569 // Override GOF references.
570 if (!codec_header.inter_pic_predicted) {
571 frame->num_references = 0;
572 }
573
philipelafcf7f52017-04-26 08:17:35 -0700574 UnwrapPictureIds(frame);
575 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700576}
577
578bool RtpFrameReferenceFinder::MissingRequiredFrameVp9(uint16_t picture_id,
philipelc9b27d52016-07-15 06:50:27 -0700579 const GofInfo& info) {
580 size_t diff =
581 ForwardDiff<uint16_t, kPicIdLength>(info.gof->pid_start, picture_id);
582 size_t gof_idx = diff % info.gof->num_frames_in_gof;
583 size_t temporal_idx = info.gof->temporal_idx[gof_idx];
philipel02447bc2016-05-13 06:01:03 -0700584
philipela157e082018-05-02 15:19:01 +0200585 if (temporal_idx >= kMaxTemporalLayers) {
586 RTC_LOG(LS_WARNING) << "At most " << kMaxTemporalLayers << " temporal "
587 << "layers are supported.";
588 return true;
589 }
590
philipel02447bc2016-05-13 06:01:03 -0700591 // For every reference this frame has, check if there is a frame missing in
592 // the interval (|ref_pid|, |picture_id|) in any of the lower temporal
593 // layers. If so, we are missing a required frame.
philipelc9b27d52016-07-15 06:50:27 -0700594 uint8_t num_references = info.gof->num_ref_pics[gof_idx];
philipel02447bc2016-05-13 06:01:03 -0700595 for (size_t i = 0; i < num_references; ++i) {
596 uint16_t ref_pid =
philipelc9b27d52016-07-15 06:50:27 -0700597 Subtract<kPicIdLength>(picture_id, info.gof->pid_diff[gof_idx][i]);
philipel02447bc2016-05-13 06:01:03 -0700598 for (size_t l = 0; l < temporal_idx; ++l) {
599 auto missing_frame_it = missing_frames_for_layer_[l].lower_bound(ref_pid);
600 if (missing_frame_it != missing_frames_for_layer_[l].end() &&
601 AheadOf<uint16_t, kPicIdLength>(picture_id, *missing_frame_it)) {
602 return true;
603 }
604 }
605 }
606 return false;
607}
608
609void RtpFrameReferenceFinder::FrameReceivedVp9(uint16_t picture_id,
philipelc9b27d52016-07-15 06:50:27 -0700610 GofInfo* info) {
611 int last_picture_id = info->last_picture_id;
philipel459f4e32018-03-02 10:55:12 +0100612 size_t gof_size = std::min(info->gof->num_frames_in_gof, kMaxVp9FramesInGof);
philipel02447bc2016-05-13 06:01:03 -0700613
614 // If there is a gap, find which temporal layer the missing frames
615 // belong to and add the frame as missing for that temporal layer.
616 // Otherwise, remove this frame from the set of missing frames.
philipelc9b27d52016-07-15 06:50:27 -0700617 if (AheadOf<uint16_t, kPicIdLength>(picture_id, last_picture_id)) {
618 size_t diff = ForwardDiff<uint16_t, kPicIdLength>(info->gof->pid_start,
619 last_picture_id);
philipel459f4e32018-03-02 10:55:12 +0100620 size_t gof_idx = diff % gof_size;
philipel02447bc2016-05-13 06:01:03 -0700621
philipelc9b27d52016-07-15 06:50:27 -0700622 last_picture_id = Add<kPicIdLength>(last_picture_id, 1);
623 while (last_picture_id != picture_id) {
Yves Gerey665174f2018-06-19 15:03:05 +0200624 gof_idx = (gof_idx + 1) % gof_size;
philipel459f4e32018-03-02 10:55:12 +0100625 RTC_CHECK(gof_idx < kMaxVp9FramesInGof);
626
philipelc9b27d52016-07-15 06:50:27 -0700627 size_t temporal_idx = info->gof->temporal_idx[gof_idx];
philipel459f4e32018-03-02 10:55:12 +0100628 if (temporal_idx >= kMaxTemporalLayers) {
629 RTC_LOG(LS_WARNING) << "At most " << kMaxTemporalLayers << " temporal "
630 << "layers are supported.";
631 return;
632 }
633
philipelc9b27d52016-07-15 06:50:27 -0700634 missing_frames_for_layer_[temporal_idx].insert(last_picture_id);
635 last_picture_id = Add<kPicIdLength>(last_picture_id, 1);
philipel02447bc2016-05-13 06:01:03 -0700636 }
philipel459f4e32018-03-02 10:55:12 +0100637
philipelc9b27d52016-07-15 06:50:27 -0700638 info->last_picture_id = last_picture_id;
philipel02447bc2016-05-13 06:01:03 -0700639 } else {
640 size_t diff =
philipelc9b27d52016-07-15 06:50:27 -0700641 ForwardDiff<uint16_t, kPicIdLength>(info->gof->pid_start, picture_id);
philipel459f4e32018-03-02 10:55:12 +0100642 size_t gof_idx = diff % gof_size;
643 RTC_CHECK(gof_idx < kMaxVp9FramesInGof);
644
philipelc9b27d52016-07-15 06:50:27 -0700645 size_t temporal_idx = info->gof->temporal_idx[gof_idx];
philipel459f4e32018-03-02 10:55:12 +0100646 if (temporal_idx >= kMaxTemporalLayers) {
647 RTC_LOG(LS_WARNING) << "At most " << kMaxTemporalLayers << " temporal "
648 << "layers are supported.";
649 return;
650 }
651
philipel02447bc2016-05-13 06:01:03 -0700652 missing_frames_for_layer_[temporal_idx].erase(picture_id);
653 }
654}
655
656bool RtpFrameReferenceFinder::UpSwitchInIntervalVp9(uint16_t picture_id,
657 uint8_t temporal_idx,
658 uint16_t pid_ref) {
659 for (auto up_switch_it = up_switch_.upper_bound(pid_ref);
660 up_switch_it != up_switch_.end() &&
661 AheadOf<uint16_t, kPicIdLength>(picture_id, up_switch_it->first);
662 ++up_switch_it) {
663 if (up_switch_it->second < temporal_idx)
664 return true;
665 }
666
667 return false;
668}
669
philipelafcf7f52017-04-26 08:17:35 -0700670void RtpFrameReferenceFinder::UnwrapPictureIds(RtpFrameObject* frame) {
philipel02447bc2016-05-13 06:01:03 -0700671 for (size_t i = 0; i < frame->num_references; ++i)
philipeld4fac692017-09-04 07:03:46 -0700672 frame->references[i] = unwrapper_.Unwrap(frame->references[i]);
philipel0fa82a62018-03-19 15:34:53 +0100673 frame->id.picture_id = unwrapper_.Unwrap(frame->id.picture_id);
philipel02447bc2016-05-13 06:01:03 -0700674}
675
Johnny Leebc7f41b2019-05-01 14:41:32 -0400676RtpFrameReferenceFinder::FrameDecision RtpFrameReferenceFinder::ManageFrameH264(
677 RtpFrameObject* frame) {
678 absl::optional<FrameMarking> rtp_frame_marking = frame->GetFrameMarking();
679 if (!rtp_frame_marking) {
680 return ManageFramePidOrSeqNum(std::move(frame), kNoPictureId);
681 }
682
683 uint8_t tid = rtp_frame_marking->temporal_id;
684 bool blSync = rtp_frame_marking->base_layer_sync;
685
686 if (tid == kNoTemporalIdx)
687 return ManageFramePidOrSeqNum(std::move(frame), kNoPictureId);
688
689 frame->id.picture_id = frame->last_seq_num();
690
691 if (frame->frame_type() == VideoFrameType::kVideoFrameKey) {
692 // For H264, use last_seq_num_gop_ to simply store last picture id
693 // as a pair of unpadded and padded sequence numbers.
694 if (last_seq_num_gop_.empty()) {
695 last_seq_num_gop_.insert(std::make_pair(
696 0, std::make_pair(frame->id.picture_id, frame->id.picture_id)));
697 }
698 }
699
700 // Stash if we have no keyframe yet.
701 if (last_seq_num_gop_.empty())
702 return kStash;
703
704 // Check for gap in sequence numbers. Store in |not_yet_received_seq_num_|.
705 if (frame->frame_type() == VideoFrameType::kVideoFrameDelta) {
706 uint16_t last_pic_id_padded = last_seq_num_gop_.begin()->second.second;
707 if (AheadOf<uint16_t>(frame->id.picture_id, last_pic_id_padded)) {
708 do {
709 last_pic_id_padded = last_pic_id_padded + 1;
710 not_yet_received_seq_num_.insert(last_pic_id_padded);
711 } while (last_pic_id_padded != frame->id.picture_id);
712 }
713 }
714
715 int64_t unwrapped_tl0 = tl0_unwrapper_.Unwrap(rtp_frame_marking->tl0_pic_idx);
716
717 // Clean up info for base layers that are too old.
718 int64_t old_tl0_pic_idx = unwrapped_tl0 - kMaxLayerInfo;
719 auto clean_layer_info_to = layer_info_.lower_bound(old_tl0_pic_idx);
720 layer_info_.erase(layer_info_.begin(), clean_layer_info_to);
721
722 // Clean up info about not yet received frames that are too old.
723 uint16_t old_picture_id = frame->id.picture_id - kMaxNotYetReceivedFrames * 2;
724 auto clean_frames_to = not_yet_received_seq_num_.lower_bound(old_picture_id);
725 not_yet_received_seq_num_.erase(not_yet_received_seq_num_.begin(),
726 clean_frames_to);
727
728 if (frame->frame_type() == VideoFrameType::kVideoFrameKey) {
729 frame->num_references = 0;
730 layer_info_[unwrapped_tl0].fill(-1);
731 UpdateDataH264(frame, unwrapped_tl0, tid);
732 return kHandOff;
733 }
734
735 auto layer_info_it = layer_info_.find(
736 tid == 0 ? unwrapped_tl0 - 1 : unwrapped_tl0);
737
738 // Stash if we have no base layer frame yet.
739 if (layer_info_it == layer_info_.end())
740 return kStash;
741
742 // Base layer frame. Copy layer info from previous base layer frame.
743 if (tid == 0) {
744 layer_info_it = layer_info_.insert(
745 std::make_pair(unwrapped_tl0, layer_info_it->second)).first;
746 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;
855 for (uint16_t n = frame->first_seq_num();
856 AheadOrAt(last_seq_num_padded, n); ++n) {
857 not_yet_received_seq_num_.erase(n);
858 }
859}
860
philipel02447bc2016-05-13 06:01:03 -0700861} // namespace video_coding
862} // namespace webrtc