blob: 1f8519ec2bba6e36f0b15d127c7a6468741e7b68 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "modules/video_coding/frame_object.h"
17#include "modules/video_coding/packet_buffer.h"
18#include "rtc_base/checks.h"
19#include "rtc_base/logging.h"
philipel02447bc2016-05-13 06:01:03 -070020
21namespace webrtc {
22namespace video_coding {
23
24RtpFrameReferenceFinder::RtpFrameReferenceFinder(
25 OnCompleteFrameCallback* frame_callback)
26 : last_picture_id_(-1),
27 last_unwrap_(-1),
28 current_ss_idx_(0),
philipel463d3012016-09-09 03:32:44 -070029 cleared_to_seq_num_(-1),
philipel02447bc2016-05-13 06:01:03 -070030 frame_callback_(frame_callback) {}
31
32void RtpFrameReferenceFinder::ManageFrame(
33 std::unique_ptr<RtpFrameObject> frame) {
34 rtc::CritScope lock(&crit_);
philipel463d3012016-09-09 03:32:44 -070035
36 // If we have cleared past this frame, drop it.
37 if (cleared_to_seq_num_ != -1 &&
38 AheadOf<uint16_t>(cleared_to_seq_num_, frame->first_seq_num())) {
39 return;
40 }
41
philipelafcf7f52017-04-26 08:17:35 -070042 FrameDecision decision = ManageFrameInternal(frame.get());
43
44 switch (decision) {
45 case kStash:
46 if (stashed_frames_.size() > kMaxStashedFrames)
47 stashed_frames_.pop_back();
48 stashed_frames_.push_front(std::move(frame));
49 break;
50 case kHandOff:
51 frame_callback_->OnCompleteFrame(std::move(frame));
52 RetryStashedFrames();
53 break;
54 case kDrop:
55 break;
56 }
57}
58
59void RtpFrameReferenceFinder::RetryStashedFrames() {
60 bool complete_frame = false;
61 do {
62 complete_frame = false;
63 for (auto frame_it = stashed_frames_.begin();
64 frame_it != stashed_frames_.end();) {
65 FrameDecision decision = ManageFrameInternal(frame_it->get());
66
67 switch (decision) {
68 case kStash:
69 ++frame_it;
70 break;
71 case kHandOff:
72 complete_frame = true;
73 frame_callback_->OnCompleteFrame(std::move(*frame_it));
kjellanderbdf30722017-09-08 11:00:21 -070074 FALLTHROUGH();
philipelafcf7f52017-04-26 08:17:35 -070075 case kDrop:
76 frame_it = stashed_frames_.erase(frame_it);
77 }
78 }
79 } while (complete_frame);
80}
81
82RtpFrameReferenceFinder::FrameDecision
83RtpFrameReferenceFinder::ManageFrameInternal(RtpFrameObject* frame) {
philipel02447bc2016-05-13 06:01:03 -070084 switch (frame->codec_type()) {
brandtr87d7d772016-11-07 03:03:41 -080085 case kVideoCodecFlexfec:
philipel02447bc2016-05-13 06:01:03 -070086 case kVideoCodecULPFEC:
87 case kVideoCodecRED:
philipel02447bc2016-05-13 06:01:03 -070088 RTC_NOTREACHED();
89 break;
90 case kVideoCodecVP8:
philipelafcf7f52017-04-26 08:17:35 -070091 return ManageFrameVp8(frame);
philipel02447bc2016-05-13 06:01:03 -070092 case kVideoCodecVP9:
philipelafcf7f52017-04-26 08:17:35 -070093 return ManageFrameVp9(frame);
philipel266f0a42016-11-28 08:49:07 -080094 // Since the EndToEndTests use kVicdeoCodecUnknow we treat it the same as
95 // kVideoCodecGeneric.
96 // TODO(philipel): Take a look at the EndToEndTests and see if maybe they
97 // should be changed to use kVideoCodecGeneric instead.
98 case kVideoCodecUnknown:
philipel02447bc2016-05-13 06:01:03 -070099 case kVideoCodecH264:
100 case kVideoCodecI420:
Emircan Uysaler90612a62017-11-28 09:45:25 -0800101 case kVideoCodecStereo:
philipel02447bc2016-05-13 06:01:03 -0700102 case kVideoCodecGeneric:
philipelafcf7f52017-04-26 08:17:35 -0700103 return ManageFrameGeneric(frame, kNoPictureId);
philipel02447bc2016-05-13 06:01:03 -0700104 }
philipelafcf7f52017-04-26 08:17:35 -0700105
106 // If not all code paths return a value it makes the win compiler sad.
107 RTC_NOTREACHED();
108 return kDrop;
philipel02447bc2016-05-13 06:01:03 -0700109}
110
philipel9b2ce6b2016-07-05 05:04:46 -0700111void RtpFrameReferenceFinder::PaddingReceived(uint16_t seq_num) {
112 rtc::CritScope lock(&crit_);
113 auto clean_padding_to =
114 stashed_padding_.lower_bound(seq_num - kMaxPaddingAge);
115 stashed_padding_.erase(stashed_padding_.begin(), clean_padding_to);
116 stashed_padding_.insert(seq_num);
117 UpdateLastPictureIdWithPadding(seq_num);
118 RetryStashedFrames();
119}
120
philipel463d3012016-09-09 03:32:44 -0700121void RtpFrameReferenceFinder::ClearTo(uint16_t seq_num) {
122 rtc::CritScope lock(&crit_);
123 cleared_to_seq_num_ = seq_num;
124
125 auto it = stashed_frames_.begin();
126 while (it != stashed_frames_.end()) {
127 if (AheadOf<uint16_t>(cleared_to_seq_num_, (*it)->first_seq_num())) {
128 it = stashed_frames_.erase(it);
129 } else {
130 ++it;
131 }
132 }
133}
134
philipel9b2ce6b2016-07-05 05:04:46 -0700135void RtpFrameReferenceFinder::UpdateLastPictureIdWithPadding(uint16_t seq_num) {
136 auto gop_seq_num_it = last_seq_num_gop_.upper_bound(seq_num);
137
138 // If this padding packet "belongs" to a group of pictures that we don't track
139 // anymore, do nothing.
140 if (gop_seq_num_it == last_seq_num_gop_.begin())
141 return;
142 --gop_seq_num_it;
143
144 // Calculate the next contiuous sequence number and search for it in
145 // the padding packets we have stashed.
146 uint16_t next_seq_num_with_padding = gop_seq_num_it->second.second + 1;
147 auto padding_seq_num_it =
148 stashed_padding_.lower_bound(next_seq_num_with_padding);
149
150 // While there still are padding packets and those padding packets are
151 // continuous, then advance the "last-picture-id-with-padding" and remove
152 // the stashed padding packet.
153 while (padding_seq_num_it != stashed_padding_.end() &&
154 *padding_seq_num_it == next_seq_num_with_padding) {
155 gop_seq_num_it->second.second = next_seq_num_with_padding;
156 ++next_seq_num_with_padding;
157 padding_seq_num_it = stashed_padding_.erase(padding_seq_num_it);
158 }
philipel41bb7922017-02-20 07:53:23 -0800159
160 // In the case where the stream has been continuous without any new keyframes
161 // for a while there is a risk that new frames will appear to be older than
162 // the keyframe they belong to due to wrapping sequence number. In order
163 // to prevent this we advance the picture id of the keyframe every so often.
164 if (ForwardDiff(gop_seq_num_it->first, seq_num) > 10000) {
165 RTC_DCHECK_EQ(1ul, last_seq_num_gop_.size());
166 last_seq_num_gop_[seq_num] = gop_seq_num_it->second;
167 last_seq_num_gop_.erase(gop_seq_num_it);
168 }
philipel9b2ce6b2016-07-05 05:04:46 -0700169}
170
philipelafcf7f52017-04-26 08:17:35 -0700171RtpFrameReferenceFinder::FrameDecision
172RtpFrameReferenceFinder::ManageFrameGeneric(RtpFrameObject* frame,
173 int picture_id) {
philipel647998c2016-06-03 09:40:16 -0700174 // If |picture_id| is specified then we use that to set the frame references,
175 // otherwise we use sequence number.
176 if (picture_id != kNoPictureId) {
177 if (last_unwrap_ == -1)
178 last_unwrap_ = picture_id;
179
philipeld4fac692017-09-04 07:03:46 -0700180 frame->picture_id = unwrapper_.Unwrap(picture_id);
philipel647998c2016-06-03 09:40:16 -0700181 frame->num_references = frame->frame_type() == kVideoFrameKey ? 0 : 1;
182 frame->references[0] = frame->picture_id - 1;
philipelafcf7f52017-04-26 08:17:35 -0700183 return kHandOff;
philipel647998c2016-06-03 09:40:16 -0700184 }
185
philipel9b2ce6b2016-07-05 05:04:46 -0700186 if (frame->frame_type() == kVideoFrameKey) {
187 last_seq_num_gop_.insert(std::make_pair(
188 frame->last_seq_num(),
189 std::make_pair(frame->last_seq_num(), frame->last_seq_num())));
190 }
philipel02447bc2016-05-13 06:01:03 -0700191
192 // We have received a frame but not yet a keyframe, stash this frame.
philipelafcf7f52017-04-26 08:17:35 -0700193 if (last_seq_num_gop_.empty())
194 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700195
196 // Clean up info for old keyframes but make sure to keep info
197 // for the last keyframe.
198 auto clean_to = last_seq_num_gop_.lower_bound(frame->last_seq_num() - 100);
philipel41bb7922017-02-20 07:53:23 -0800199 for (auto it = last_seq_num_gop_.begin();
200 it != clean_to && last_seq_num_gop_.size() > 1;) {
201 it = last_seq_num_gop_.erase(it);
202 }
philipel02447bc2016-05-13 06:01:03 -0700203
204 // Find the last sequence number of the last frame for the keyframe
205 // that this frame indirectly references.
206 auto seq_num_it = last_seq_num_gop_.upper_bound(frame->last_seq_num());
philipel9b2ce6b2016-07-05 05:04:46 -0700207 if (seq_num_it == last_seq_num_gop_.begin()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100208 RTC_LOG(LS_WARNING) << "Generic frame with packet range ["
209 << frame->first_seq_num() << ", "
210 << frame->last_seq_num()
211 << "] has no GoP, dropping frame.";
philipelafcf7f52017-04-26 08:17:35 -0700212 return kDrop;
philipel9b2ce6b2016-07-05 05:04:46 -0700213 }
philipel02447bc2016-05-13 06:01:03 -0700214 seq_num_it--;
215
216 // Make sure the packet sequence numbers are continuous, otherwise stash
217 // this frame.
philipel9b2ce6b2016-07-05 05:04:46 -0700218 uint16_t last_picture_id_gop = seq_num_it->second.first;
219 uint16_t last_picture_id_with_padding_gop = seq_num_it->second.second;
philipel02447bc2016-05-13 06:01:03 -0700220 if (frame->frame_type() == kVideoFrameDelta) {
philipel9b2ce6b2016-07-05 05:04:46 -0700221 uint16_t prev_seq_num = frame->first_seq_num() - 1;
philipelafcf7f52017-04-26 08:17:35 -0700222
223 if (prev_seq_num != last_picture_id_with_padding_gop)
224 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700225 }
226
227 RTC_DCHECK(AheadOrAt(frame->last_seq_num(), seq_num_it->first));
228
229 // Since keyframes can cause reordering we can't simply assign the
230 // picture id according to some incrementing counter.
231 frame->picture_id = frame->last_seq_num();
232 frame->num_references = frame->frame_type() == kVideoFrameDelta;
philipeld4fac692017-09-04 07:03:46 -0700233 frame->references[0] = generic_unwrapper_.Unwrap(last_picture_id_gop);
234 if (AheadOf<uint16_t>(frame->picture_id, last_picture_id_gop)) {
philipel9b2ce6b2016-07-05 05:04:46 -0700235 seq_num_it->second.first = frame->picture_id;
236 seq_num_it->second.second = frame->picture_id;
237 }
philipel02447bc2016-05-13 06:01:03 -0700238
239 last_picture_id_ = frame->picture_id;
philipel9b2ce6b2016-07-05 05:04:46 -0700240 UpdateLastPictureIdWithPadding(frame->picture_id);
philipeld4fac692017-09-04 07:03:46 -0700241 frame->picture_id = generic_unwrapper_.Unwrap(frame->picture_id);
philipelafcf7f52017-04-26 08:17:35 -0700242 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700243}
244
philipelafcf7f52017-04-26 08:17:35 -0700245RtpFrameReferenceFinder::FrameDecision RtpFrameReferenceFinder::ManageFrameVp8(
246 RtpFrameObject* frame) {
philipel88488282016-11-03 08:56:54 -0700247 rtc::Optional<RTPVideoTypeHeader> rtp_codec_header = frame->GetCodecHeader();
philipeld4fac692017-09-04 07:03:46 -0700248 if (!rtp_codec_header) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100249 RTC_LOG(LS_WARNING)
250 << "Failed to get codec header from frame, dropping frame.";
philipelafcf7f52017-04-26 08:17:35 -0700251 return kDrop;
philipeld4fac692017-09-04 07:03:46 -0700252 }
philipel02447bc2016-05-13 06:01:03 -0700253
254 const RTPVideoHeaderVP8& codec_header = rtp_codec_header->VP8;
255
256 if (codec_header.pictureId == kNoPictureId ||
257 codec_header.temporalIdx == kNoTemporalIdx ||
258 codec_header.tl0PicIdx == kNoTl0PicIdx) {
philipelafcf7f52017-04-26 08:17:35 -0700259 return ManageFrameGeneric(std::move(frame), codec_header.pictureId);
philipel02447bc2016-05-13 06:01:03 -0700260 }
261
262 frame->picture_id = codec_header.pictureId % kPicIdLength;
263
264 if (last_unwrap_ == -1)
265 last_unwrap_ = codec_header.pictureId;
266
267 if (last_picture_id_ == -1)
268 last_picture_id_ = frame->picture_id;
269
270 // Find if there has been a gap in fully received frames and save the picture
271 // id of those frames in |not_yet_received_frames_|.
272 if (AheadOf<uint16_t, kPicIdLength>(frame->picture_id, last_picture_id_)) {
philipel9bd1d662017-07-14 04:52:01 -0700273 do {
philipel02447bc2016-05-13 06:01:03 -0700274 last_picture_id_ = Add<kPicIdLength>(last_picture_id_, 1);
philipel9bd1d662017-07-14 04:52:01 -0700275 not_yet_received_frames_.insert(last_picture_id_);
276 } while (last_picture_id_ != frame->picture_id);
philipel02447bc2016-05-13 06:01:03 -0700277 }
278
279 // Clean up info for base layers that are too old.
280 uint8_t old_tl0_pic_idx = codec_header.tl0PicIdx - kMaxLayerInfo;
281 auto clean_layer_info_to = layer_info_.lower_bound(old_tl0_pic_idx);
282 layer_info_.erase(layer_info_.begin(), clean_layer_info_to);
283
284 // Clean up info about not yet received frames that are too old.
285 uint16_t old_picture_id =
286 Subtract<kPicIdLength>(frame->picture_id, kMaxNotYetReceivedFrames);
287 auto clean_frames_to = not_yet_received_frames_.lower_bound(old_picture_id);
288 not_yet_received_frames_.erase(not_yet_received_frames_.begin(),
289 clean_frames_to);
290
291 if (frame->frame_type() == kVideoFrameKey) {
292 frame->num_references = 0;
293 layer_info_[codec_header.tl0PicIdx].fill(-1);
philipeld4fac692017-09-04 07:03:46 -0700294 UpdateLayerInfoVp8(frame);
philipelafcf7f52017-04-26 08:17:35 -0700295 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700296 }
297
298 auto layer_info_it = layer_info_.find(codec_header.temporalIdx == 0
299 ? codec_header.tl0PicIdx - 1
300 : codec_header.tl0PicIdx);
301
302 // If we don't have the base layer frame yet, stash this frame.
philipelafcf7f52017-04-26 08:17:35 -0700303 if (layer_info_it == layer_info_.end())
304 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700305
306 // A non keyframe base layer frame has been received, copy the layer info
307 // from the previous base layer frame and set a reference to the previous
308 // base layer frame.
309 if (codec_header.temporalIdx == 0) {
310 layer_info_it =
311 layer_info_
312 .insert(make_pair(codec_header.tl0PicIdx, layer_info_it->second))
313 .first;
314 frame->num_references = 1;
315 frame->references[0] = layer_info_it->second[0];
philipeld4fac692017-09-04 07:03:46 -0700316 UpdateLayerInfoVp8(frame);
philipelafcf7f52017-04-26 08:17:35 -0700317 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700318 }
319
320 // Layer sync frame, this frame only references its base layer frame.
321 if (codec_header.layerSync) {
322 frame->num_references = 1;
323 frame->references[0] = layer_info_it->second[0];
324
philipeld4fac692017-09-04 07:03:46 -0700325 UpdateLayerInfoVp8(frame);
philipelafcf7f52017-04-26 08:17:35 -0700326 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700327 }
328
329 // Find all references for this frame.
330 frame->num_references = 0;
331 for (uint8_t layer = 0; layer <= codec_header.temporalIdx; ++layer) {
philipeld268d6f2016-09-15 13:43:13 +0200332 // If we have not yet received a previous frame on this temporal layer,
333 // stash this frame.
philipelafcf7f52017-04-26 08:17:35 -0700334 if (layer_info_it->second[layer] == -1)
335 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700336
philipel86b92e02016-10-24 07:11:53 -0700337 // If the last frame on this layer is ahead of this frame it means that
338 // a layer sync frame has been received after this frame for the same
339 // base layer frame, drop this frame.
340 if (AheadOf<uint16_t, kPicIdLength>(layer_info_it->second[layer],
341 frame->picture_id)) {
philipelafcf7f52017-04-26 08:17:35 -0700342 return kDrop;
philipel86b92e02016-10-24 07:11:53 -0700343 }
344
philipel02447bc2016-05-13 06:01:03 -0700345 // If we have not yet received a frame between this frame and the referenced
346 // frame then we have to wait for that frame to be completed first.
347 auto not_received_frame_it =
348 not_yet_received_frames_.upper_bound(layer_info_it->second[layer]);
349 if (not_received_frame_it != not_yet_received_frames_.end() &&
350 AheadOf<uint16_t, kPicIdLength>(frame->picture_id,
351 *not_received_frame_it)) {
philipelafcf7f52017-04-26 08:17:35 -0700352 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700353 }
354
philipel57f19cc2017-03-07 03:54:05 -0800355 if (!(AheadOf<uint16_t, kPicIdLength>(frame->picture_id,
356 layer_info_it->second[layer]))) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100357 RTC_LOG(LS_WARNING) << "Frame with picture id " << frame->picture_id
358 << " and packet range [" << frame->first_seq_num()
359 << ", " << frame->last_seq_num()
360 << "] already received, "
361 << " dropping frame.";
philipelafcf7f52017-04-26 08:17:35 -0700362 return kDrop;
philipel57f19cc2017-03-07 03:54:05 -0800363 }
364
philipel02447bc2016-05-13 06:01:03 -0700365 ++frame->num_references;
366 frame->references[layer] = layer_info_it->second[layer];
367 }
368
philipeld4fac692017-09-04 07:03:46 -0700369 UpdateLayerInfoVp8(frame);
philipelafcf7f52017-04-26 08:17:35 -0700370 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700371}
372
philipeld4fac692017-09-04 07:03:46 -0700373void RtpFrameReferenceFinder::UpdateLayerInfoVp8(RtpFrameObject* frame) {
374 rtc::Optional<RTPVideoTypeHeader> rtp_codec_header = frame->GetCodecHeader();
375 RTC_DCHECK(rtp_codec_header);
376 const RTPVideoHeaderVP8& codec_header = rtp_codec_header->VP8;
philipel02447bc2016-05-13 06:01:03 -0700377 uint8_t tl0_pic_idx = codec_header.tl0PicIdx;
378 uint8_t temporal_index = codec_header.temporalIdx;
379 auto layer_info_it = layer_info_.find(tl0_pic_idx);
380
381 // Update this layer info and newer.
382 while (layer_info_it != layer_info_.end()) {
383 if (layer_info_it->second[temporal_index] != -1 &&
384 AheadOf<uint16_t, kPicIdLength>(layer_info_it->second[temporal_index],
385 frame->picture_id)) {
386 // The frame was not newer, then no subsequent layer info have to be
387 // update.
388 break;
389 }
390
391 layer_info_it->second[codec_header.temporalIdx] = frame->picture_id;
392 ++tl0_pic_idx;
393 layer_info_it = layer_info_.find(tl0_pic_idx);
394 }
395 not_yet_received_frames_.erase(frame->picture_id);
396
philipelafcf7f52017-04-26 08:17:35 -0700397 UnwrapPictureIds(frame);
philipel02447bc2016-05-13 06:01:03 -0700398}
399
philipelafcf7f52017-04-26 08:17:35 -0700400RtpFrameReferenceFinder::FrameDecision RtpFrameReferenceFinder::ManageFrameVp9(
401 RtpFrameObject* frame) {
philipel88488282016-11-03 08:56:54 -0700402 rtc::Optional<RTPVideoTypeHeader> rtp_codec_header = frame->GetCodecHeader();
philipeld4fac692017-09-04 07:03:46 -0700403 if (!rtp_codec_header) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100404 RTC_LOG(LS_WARNING)
405 << "Failed to get codec header from frame, dropping frame.";
philipel4c140092017-08-31 08:31:45 -0700406 return kDrop;
philipeld4fac692017-09-04 07:03:46 -0700407 }
philipel4c140092017-08-31 08:31:45 -0700408
philipel02447bc2016-05-13 06:01:03 -0700409 const RTPVideoHeaderVP9& codec_header = rtp_codec_header->VP9;
410
philipel647998c2016-06-03 09:40:16 -0700411 if (codec_header.picture_id == kNoPictureId ||
412 codec_header.temporal_idx == kNoTemporalIdx) {
philipelafcf7f52017-04-26 08:17:35 -0700413 return ManageFrameGeneric(std::move(frame), codec_header.picture_id);
philipel02447bc2016-05-13 06:01:03 -0700414 }
415
416 frame->spatial_layer = codec_header.spatial_idx;
417 frame->inter_layer_predicted = codec_header.inter_layer_predicted;
418 frame->picture_id = codec_header.picture_id % kPicIdLength;
419
420 if (last_unwrap_ == -1)
421 last_unwrap_ = codec_header.picture_id;
422
423 if (last_picture_id_ == -1)
424 last_picture_id_ = frame->picture_id;
425
426 if (codec_header.flexible_mode) {
427 frame->num_references = codec_header.num_ref_pics;
428 for (size_t i = 0; i < frame->num_references; ++i) {
429 frame->references[i] =
philipela81403f2017-09-27 18:03:50 +0200430 Subtract<kPicIdLength>(frame->picture_id, codec_header.pid_diff[i]);
philipel02447bc2016-05-13 06:01:03 -0700431 }
432
philipelafcf7f52017-04-26 08:17:35 -0700433 UnwrapPictureIds(frame);
434 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700435 }
436
437 if (codec_header.ss_data_available) {
438 // Scalability structures can only be sent with tl0 frames.
439 if (codec_header.temporal_idx != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100440 RTC_LOG(LS_WARNING)
441 << "Received scalability structure on a non base layer"
442 " frame. Scalability structure ignored.";
philipel02447bc2016-05-13 06:01:03 -0700443 } else {
444 current_ss_idx_ = Add<kMaxGofSaved>(current_ss_idx_, 1);
445 scalability_structures_[current_ss_idx_] = codec_header.gof;
446 scalability_structures_[current_ss_idx_].pid_start = frame->picture_id;
447
philipelc9b27d52016-07-15 06:50:27 -0700448 GofInfo info(&scalability_structures_[current_ss_idx_],
449 frame->picture_id);
450 gof_info_.insert(std::make_pair(codec_header.tl0_pic_idx, info));
philipel02447bc2016-05-13 06:01:03 -0700451 }
452 }
453
454 // Clean up info for base layers that are too old.
455 uint8_t old_tl0_pic_idx = codec_header.tl0_pic_idx - kMaxGofSaved;
456 auto clean_gof_info_to = gof_info_.lower_bound(old_tl0_pic_idx);
457 gof_info_.erase(gof_info_.begin(), clean_gof_info_to);
458
459 if (frame->frame_type() == kVideoFrameKey) {
460 // When using GOF all keyframes must include the scalability structure.
461 if (!codec_header.ss_data_available)
Mirko Bonadei675513b2017-11-09 11:09:25 +0100462 RTC_LOG(LS_WARNING) << "Received keyframe without scalability structure";
philipel02447bc2016-05-13 06:01:03 -0700463
464 frame->num_references = 0;
philipelc9b27d52016-07-15 06:50:27 -0700465 GofInfo info = gof_info_.find(codec_header.tl0_pic_idx)->second;
466 FrameReceivedVp9(frame->picture_id, &info);
philipelafcf7f52017-04-26 08:17:35 -0700467 UnwrapPictureIds(frame);
468 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700469 }
470
471 auto gof_info_it = gof_info_.find(
472 (codec_header.temporal_idx == 0 && !codec_header.ss_data_available)
473 ? codec_header.tl0_pic_idx - 1
474 : codec_header.tl0_pic_idx);
475
476 // Gof info for this frame is not available yet, stash this frame.
philipelafcf7f52017-04-26 08:17:35 -0700477 if (gof_info_it == gof_info_.end())
478 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700479
philipelc9b27d52016-07-15 06:50:27 -0700480 GofInfo* info = &gof_info_it->second;
481 FrameReceivedVp9(frame->picture_id, info);
philipel02447bc2016-05-13 06:01:03 -0700482
483 // Make sure we don't miss any frame that could potentially have the
484 // up switch flag set.
philipelafcf7f52017-04-26 08:17:35 -0700485 if (MissingRequiredFrameVp9(frame->picture_id, *info))
486 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700487
488 if (codec_header.temporal_up_switch) {
489 auto pid_tidx =
490 std::make_pair(frame->picture_id, codec_header.temporal_idx);
491 up_switch_.insert(pid_tidx);
492 }
493
494 // If this is a base layer frame that contains a scalability structure
495 // then gof info has already been inserted earlier, so we only want to
496 // insert if we haven't done so already.
497 if (codec_header.temporal_idx == 0 && !codec_header.ss_data_available) {
philipelc9b27d52016-07-15 06:50:27 -0700498 GofInfo new_info(info->gof, frame->picture_id);
499 gof_info_.insert(std::make_pair(codec_header.tl0_pic_idx, new_info));
philipel02447bc2016-05-13 06:01:03 -0700500 }
501
502 // Clean out old info about up switch frames.
philipelc9b27d52016-07-15 06:50:27 -0700503 uint16_t old_picture_id = Subtract<kPicIdLength>(frame->picture_id, 50);
philipel02447bc2016-05-13 06:01:03 -0700504 auto up_switch_erase_to = up_switch_.lower_bound(old_picture_id);
505 up_switch_.erase(up_switch_.begin(), up_switch_erase_to);
506
philipelc9b27d52016-07-15 06:50:27 -0700507 size_t diff = ForwardDiff<uint16_t, kPicIdLength>(info->gof->pid_start,
508 frame->picture_id);
509 size_t gof_idx = diff % info->gof->num_frames_in_gof;
philipel02447bc2016-05-13 06:01:03 -0700510
511 // Populate references according to the scalability structure.
philipelc9b27d52016-07-15 06:50:27 -0700512 frame->num_references = info->gof->num_ref_pics[gof_idx];
philipel02447bc2016-05-13 06:01:03 -0700513 for (size_t i = 0; i < frame->num_references; ++i) {
philipelc9b27d52016-07-15 06:50:27 -0700514 frame->references[i] = Subtract<kPicIdLength>(
515 frame->picture_id, info->gof->pid_diff[gof_idx][i]);
philipel02447bc2016-05-13 06:01:03 -0700516
517 // If this is a reference to a frame earlier than the last up switch point,
518 // then ignore this reference.
519 if (UpSwitchInIntervalVp9(frame->picture_id, codec_header.temporal_idx,
520 frame->references[i])) {
521 --frame->num_references;
522 }
523 }
524
philipelafcf7f52017-04-26 08:17:35 -0700525 UnwrapPictureIds(frame);
526 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700527}
528
529bool RtpFrameReferenceFinder::MissingRequiredFrameVp9(uint16_t picture_id,
philipelc9b27d52016-07-15 06:50:27 -0700530 const GofInfo& info) {
531 size_t diff =
532 ForwardDiff<uint16_t, kPicIdLength>(info.gof->pid_start, picture_id);
533 size_t gof_idx = diff % info.gof->num_frames_in_gof;
534 size_t temporal_idx = info.gof->temporal_idx[gof_idx];
philipel02447bc2016-05-13 06:01:03 -0700535
536 // For every reference this frame has, check if there is a frame missing in
537 // the interval (|ref_pid|, |picture_id|) in any of the lower temporal
538 // layers. If so, we are missing a required frame.
philipelc9b27d52016-07-15 06:50:27 -0700539 uint8_t num_references = info.gof->num_ref_pics[gof_idx];
philipel02447bc2016-05-13 06:01:03 -0700540 for (size_t i = 0; i < num_references; ++i) {
541 uint16_t ref_pid =
philipelc9b27d52016-07-15 06:50:27 -0700542 Subtract<kPicIdLength>(picture_id, info.gof->pid_diff[gof_idx][i]);
philipel02447bc2016-05-13 06:01:03 -0700543 for (size_t l = 0; l < temporal_idx; ++l) {
544 auto missing_frame_it = missing_frames_for_layer_[l].lower_bound(ref_pid);
545 if (missing_frame_it != missing_frames_for_layer_[l].end() &&
546 AheadOf<uint16_t, kPicIdLength>(picture_id, *missing_frame_it)) {
547 return true;
548 }
549 }
550 }
551 return false;
552}
553
554void RtpFrameReferenceFinder::FrameReceivedVp9(uint16_t picture_id,
philipelc9b27d52016-07-15 06:50:27 -0700555 GofInfo* info) {
556 int last_picture_id = info->last_picture_id;
philipel02447bc2016-05-13 06:01:03 -0700557
558 // If there is a gap, find which temporal layer the missing frames
559 // belong to and add the frame as missing for that temporal layer.
560 // Otherwise, remove this frame from the set of missing frames.
philipelc9b27d52016-07-15 06:50:27 -0700561 if (AheadOf<uint16_t, kPicIdLength>(picture_id, last_picture_id)) {
562 size_t diff = ForwardDiff<uint16_t, kPicIdLength>(info->gof->pid_start,
563 last_picture_id);
564 size_t gof_idx = diff % info->gof->num_frames_in_gof;
philipel02447bc2016-05-13 06:01:03 -0700565
philipelc9b27d52016-07-15 06:50:27 -0700566 last_picture_id = Add<kPicIdLength>(last_picture_id, 1);
567 while (last_picture_id != picture_id) {
philipel02447bc2016-05-13 06:01:03 -0700568 ++gof_idx;
philipelc9b27d52016-07-15 06:50:27 -0700569 RTC_DCHECK_NE(0ul, gof_idx % info->gof->num_frames_in_gof);
570 size_t temporal_idx = info->gof->temporal_idx[gof_idx];
571 missing_frames_for_layer_[temporal_idx].insert(last_picture_id);
572 last_picture_id = Add<kPicIdLength>(last_picture_id, 1);
philipel02447bc2016-05-13 06:01:03 -0700573 }
philipelc9b27d52016-07-15 06:50:27 -0700574 info->last_picture_id = last_picture_id;
philipel02447bc2016-05-13 06:01:03 -0700575 } else {
576 size_t diff =
philipelc9b27d52016-07-15 06:50:27 -0700577 ForwardDiff<uint16_t, kPicIdLength>(info->gof->pid_start, picture_id);
578 size_t gof_idx = diff % info->gof->num_frames_in_gof;
579 size_t temporal_idx = info->gof->temporal_idx[gof_idx];
philipel02447bc2016-05-13 06:01:03 -0700580 missing_frames_for_layer_[temporal_idx].erase(picture_id);
581 }
582}
583
584bool RtpFrameReferenceFinder::UpSwitchInIntervalVp9(uint16_t picture_id,
585 uint8_t temporal_idx,
586 uint16_t pid_ref) {
587 for (auto up_switch_it = up_switch_.upper_bound(pid_ref);
588 up_switch_it != up_switch_.end() &&
589 AheadOf<uint16_t, kPicIdLength>(picture_id, up_switch_it->first);
590 ++up_switch_it) {
591 if (up_switch_it->second < temporal_idx)
592 return true;
593 }
594
595 return false;
596}
597
philipelafcf7f52017-04-26 08:17:35 -0700598void RtpFrameReferenceFinder::UnwrapPictureIds(RtpFrameObject* frame) {
philipel02447bc2016-05-13 06:01:03 -0700599 for (size_t i = 0; i < frame->num_references; ++i)
philipeld4fac692017-09-04 07:03:46 -0700600 frame->references[i] = unwrapper_.Unwrap(frame->references[i]);
601 frame->picture_id = unwrapper_.Unwrap(frame->picture_id);
philipel02447bc2016-05-13 06:01:03 -0700602}
603
philipel02447bc2016-05-13 06:01:03 -0700604} // namespace video_coding
605} // namespace webrtc