blob: 0aaeaec79fcbc614036a2ea263914a5ace2b6fad [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:
Taylor Brandstetter12044482018-01-27 00:45:14 +0000101 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);
philipel1610f942017-12-12 13:58:31 +0100294 UpdateLayerInfoVp8(frame, codec_header);
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];
philipel1610f942017-12-12 13:58:31 +0100316 UpdateLayerInfoVp8(frame, codec_header);
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
philipel1610f942017-12-12 13:58:31 +0100325 UpdateLayerInfoVp8(frame, codec_header);
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
philipel1610f942017-12-12 13:58:31 +0100369 UpdateLayerInfoVp8(frame, codec_header);
philipelafcf7f52017-04-26 08:17:35 -0700370 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700371}
372
philipel1610f942017-12-12 13:58:31 +0100373void RtpFrameReferenceFinder::UpdateLayerInfoVp8(
374 RtpFrameObject* frame,
375 const RTPVideoHeaderVP8& codec_header) {
philipel02447bc2016-05-13 06:01:03 -0700376 uint8_t tl0_pic_idx = codec_header.tl0PicIdx;
377 uint8_t temporal_index = codec_header.temporalIdx;
378 auto layer_info_it = layer_info_.find(tl0_pic_idx);
379
380 // Update this layer info and newer.
381 while (layer_info_it != layer_info_.end()) {
382 if (layer_info_it->second[temporal_index] != -1 &&
383 AheadOf<uint16_t, kPicIdLength>(layer_info_it->second[temporal_index],
384 frame->picture_id)) {
385 // The frame was not newer, then no subsequent layer info have to be
386 // update.
387 break;
388 }
389
390 layer_info_it->second[codec_header.temporalIdx] = frame->picture_id;
391 ++tl0_pic_idx;
392 layer_info_it = layer_info_.find(tl0_pic_idx);
393 }
394 not_yet_received_frames_.erase(frame->picture_id);
395
philipelafcf7f52017-04-26 08:17:35 -0700396 UnwrapPictureIds(frame);
philipel02447bc2016-05-13 06:01:03 -0700397}
398
philipelafcf7f52017-04-26 08:17:35 -0700399RtpFrameReferenceFinder::FrameDecision RtpFrameReferenceFinder::ManageFrameVp9(
400 RtpFrameObject* frame) {
philipel88488282016-11-03 08:56:54 -0700401 rtc::Optional<RTPVideoTypeHeader> rtp_codec_header = frame->GetCodecHeader();
philipeld4fac692017-09-04 07:03:46 -0700402 if (!rtp_codec_header) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100403 RTC_LOG(LS_WARNING)
404 << "Failed to get codec header from frame, dropping frame.";
philipel4c140092017-08-31 08:31:45 -0700405 return kDrop;
philipeld4fac692017-09-04 07:03:46 -0700406 }
philipel4c140092017-08-31 08:31:45 -0700407
philipel02447bc2016-05-13 06:01:03 -0700408 const RTPVideoHeaderVP9& codec_header = rtp_codec_header->VP9;
409
philipel647998c2016-06-03 09:40:16 -0700410 if (codec_header.picture_id == kNoPictureId ||
411 codec_header.temporal_idx == kNoTemporalIdx) {
philipelafcf7f52017-04-26 08:17:35 -0700412 return ManageFrameGeneric(std::move(frame), codec_header.picture_id);
philipel02447bc2016-05-13 06:01:03 -0700413 }
414
415 frame->spatial_layer = codec_header.spatial_idx;
416 frame->inter_layer_predicted = codec_header.inter_layer_predicted;
417 frame->picture_id = codec_header.picture_id % kPicIdLength;
418
419 if (last_unwrap_ == -1)
420 last_unwrap_ = codec_header.picture_id;
421
422 if (last_picture_id_ == -1)
423 last_picture_id_ = frame->picture_id;
424
425 if (codec_header.flexible_mode) {
426 frame->num_references = codec_header.num_ref_pics;
427 for (size_t i = 0; i < frame->num_references; ++i) {
428 frame->references[i] =
philipela81403f2017-09-27 18:03:50 +0200429 Subtract<kPicIdLength>(frame->picture_id, codec_header.pid_diff[i]);
philipel02447bc2016-05-13 06:01:03 -0700430 }
431
philipelafcf7f52017-04-26 08:17:35 -0700432 UnwrapPictureIds(frame);
433 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700434 }
435
436 if (codec_header.ss_data_available) {
437 // Scalability structures can only be sent with tl0 frames.
438 if (codec_header.temporal_idx != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100439 RTC_LOG(LS_WARNING)
440 << "Received scalability structure on a non base layer"
441 " frame. Scalability structure ignored.";
philipel02447bc2016-05-13 06:01:03 -0700442 } else {
443 current_ss_idx_ = Add<kMaxGofSaved>(current_ss_idx_, 1);
444 scalability_structures_[current_ss_idx_] = codec_header.gof;
445 scalability_structures_[current_ss_idx_].pid_start = frame->picture_id;
446
philipelc9b27d52016-07-15 06:50:27 -0700447 GofInfo info(&scalability_structures_[current_ss_idx_],
448 frame->picture_id);
449 gof_info_.insert(std::make_pair(codec_header.tl0_pic_idx, info));
philipel02447bc2016-05-13 06:01:03 -0700450 }
451 }
452
453 // Clean up info for base layers that are too old.
454 uint8_t old_tl0_pic_idx = codec_header.tl0_pic_idx - kMaxGofSaved;
455 auto clean_gof_info_to = gof_info_.lower_bound(old_tl0_pic_idx);
456 gof_info_.erase(gof_info_.begin(), clean_gof_info_to);
457
458 if (frame->frame_type() == kVideoFrameKey) {
459 // When using GOF all keyframes must include the scalability structure.
460 if (!codec_header.ss_data_available)
Mirko Bonadei675513b2017-11-09 11:09:25 +0100461 RTC_LOG(LS_WARNING) << "Received keyframe without scalability structure";
philipel02447bc2016-05-13 06:01:03 -0700462
463 frame->num_references = 0;
philipelc9b27d52016-07-15 06:50:27 -0700464 GofInfo info = gof_info_.find(codec_header.tl0_pic_idx)->second;
465 FrameReceivedVp9(frame->picture_id, &info);
philipelafcf7f52017-04-26 08:17:35 -0700466 UnwrapPictureIds(frame);
467 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700468 }
469
470 auto gof_info_it = gof_info_.find(
471 (codec_header.temporal_idx == 0 && !codec_header.ss_data_available)
472 ? codec_header.tl0_pic_idx - 1
473 : codec_header.tl0_pic_idx);
474
475 // Gof info for this frame is not available yet, stash this frame.
philipelafcf7f52017-04-26 08:17:35 -0700476 if (gof_info_it == gof_info_.end())
477 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700478
philipelc9b27d52016-07-15 06:50:27 -0700479 GofInfo* info = &gof_info_it->second;
480 FrameReceivedVp9(frame->picture_id, info);
philipel02447bc2016-05-13 06:01:03 -0700481
482 // Make sure we don't miss any frame that could potentially have the
483 // up switch flag set.
philipelafcf7f52017-04-26 08:17:35 -0700484 if (MissingRequiredFrameVp9(frame->picture_id, *info))
485 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700486
487 if (codec_header.temporal_up_switch) {
488 auto pid_tidx =
489 std::make_pair(frame->picture_id, codec_header.temporal_idx);
490 up_switch_.insert(pid_tidx);
491 }
492
493 // If this is a base layer frame that contains a scalability structure
494 // then gof info has already been inserted earlier, so we only want to
495 // insert if we haven't done so already.
496 if (codec_header.temporal_idx == 0 && !codec_header.ss_data_available) {
philipelc9b27d52016-07-15 06:50:27 -0700497 GofInfo new_info(info->gof, frame->picture_id);
498 gof_info_.insert(std::make_pair(codec_header.tl0_pic_idx, new_info));
philipel02447bc2016-05-13 06:01:03 -0700499 }
500
501 // Clean out old info about up switch frames.
philipelc9b27d52016-07-15 06:50:27 -0700502 uint16_t old_picture_id = Subtract<kPicIdLength>(frame->picture_id, 50);
philipel02447bc2016-05-13 06:01:03 -0700503 auto up_switch_erase_to = up_switch_.lower_bound(old_picture_id);
504 up_switch_.erase(up_switch_.begin(), up_switch_erase_to);
505
philipelc9b27d52016-07-15 06:50:27 -0700506 size_t diff = ForwardDiff<uint16_t, kPicIdLength>(info->gof->pid_start,
507 frame->picture_id);
508 size_t gof_idx = diff % info->gof->num_frames_in_gof;
philipel02447bc2016-05-13 06:01:03 -0700509
510 // Populate references according to the scalability structure.
philipelc9b27d52016-07-15 06:50:27 -0700511 frame->num_references = info->gof->num_ref_pics[gof_idx];
philipel02447bc2016-05-13 06:01:03 -0700512 for (size_t i = 0; i < frame->num_references; ++i) {
philipelc9b27d52016-07-15 06:50:27 -0700513 frame->references[i] = Subtract<kPicIdLength>(
514 frame->picture_id, info->gof->pid_diff[gof_idx][i]);
philipel02447bc2016-05-13 06:01:03 -0700515
516 // If this is a reference to a frame earlier than the last up switch point,
517 // then ignore this reference.
518 if (UpSwitchInIntervalVp9(frame->picture_id, codec_header.temporal_idx,
519 frame->references[i])) {
520 --frame->num_references;
521 }
522 }
523
philipelafcf7f52017-04-26 08:17:35 -0700524 UnwrapPictureIds(frame);
525 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700526}
527
528bool RtpFrameReferenceFinder::MissingRequiredFrameVp9(uint16_t picture_id,
philipelc9b27d52016-07-15 06:50:27 -0700529 const GofInfo& info) {
530 size_t diff =
531 ForwardDiff<uint16_t, kPicIdLength>(info.gof->pid_start, picture_id);
532 size_t gof_idx = diff % info.gof->num_frames_in_gof;
533 size_t temporal_idx = info.gof->temporal_idx[gof_idx];
philipel02447bc2016-05-13 06:01:03 -0700534
535 // For every reference this frame has, check if there is a frame missing in
536 // the interval (|ref_pid|, |picture_id|) in any of the lower temporal
537 // layers. If so, we are missing a required frame.
philipelc9b27d52016-07-15 06:50:27 -0700538 uint8_t num_references = info.gof->num_ref_pics[gof_idx];
philipel02447bc2016-05-13 06:01:03 -0700539 for (size_t i = 0; i < num_references; ++i) {
540 uint16_t ref_pid =
philipelc9b27d52016-07-15 06:50:27 -0700541 Subtract<kPicIdLength>(picture_id, info.gof->pid_diff[gof_idx][i]);
philipel02447bc2016-05-13 06:01:03 -0700542 for (size_t l = 0; l < temporal_idx; ++l) {
543 auto missing_frame_it = missing_frames_for_layer_[l].lower_bound(ref_pid);
544 if (missing_frame_it != missing_frames_for_layer_[l].end() &&
545 AheadOf<uint16_t, kPicIdLength>(picture_id, *missing_frame_it)) {
546 return true;
547 }
548 }
549 }
550 return false;
551}
552
553void RtpFrameReferenceFinder::FrameReceivedVp9(uint16_t picture_id,
philipelc9b27d52016-07-15 06:50:27 -0700554 GofInfo* info) {
555 int last_picture_id = info->last_picture_id;
philipel02447bc2016-05-13 06:01:03 -0700556
557 // If there is a gap, find which temporal layer the missing frames
558 // belong to and add the frame as missing for that temporal layer.
559 // Otherwise, remove this frame from the set of missing frames.
philipelc9b27d52016-07-15 06:50:27 -0700560 if (AheadOf<uint16_t, kPicIdLength>(picture_id, last_picture_id)) {
561 size_t diff = ForwardDiff<uint16_t, kPicIdLength>(info->gof->pid_start,
562 last_picture_id);
563 size_t gof_idx = diff % info->gof->num_frames_in_gof;
philipel02447bc2016-05-13 06:01:03 -0700564
philipelc9b27d52016-07-15 06:50:27 -0700565 last_picture_id = Add<kPicIdLength>(last_picture_id, 1);
566 while (last_picture_id != picture_id) {
philipel02447bc2016-05-13 06:01:03 -0700567 ++gof_idx;
philipelc9b27d52016-07-15 06:50:27 -0700568 RTC_DCHECK_NE(0ul, gof_idx % info->gof->num_frames_in_gof);
569 size_t temporal_idx = info->gof->temporal_idx[gof_idx];
570 missing_frames_for_layer_[temporal_idx].insert(last_picture_id);
571 last_picture_id = Add<kPicIdLength>(last_picture_id, 1);
philipel02447bc2016-05-13 06:01:03 -0700572 }
philipelc9b27d52016-07-15 06:50:27 -0700573 info->last_picture_id = last_picture_id;
philipel02447bc2016-05-13 06:01:03 -0700574 } else {
575 size_t diff =
philipelc9b27d52016-07-15 06:50:27 -0700576 ForwardDiff<uint16_t, kPicIdLength>(info->gof->pid_start, picture_id);
577 size_t gof_idx = diff % info->gof->num_frames_in_gof;
578 size_t temporal_idx = info->gof->temporal_idx[gof_idx];
philipel02447bc2016-05-13 06:01:03 -0700579 missing_frames_for_layer_[temporal_idx].erase(picture_id);
580 }
581}
582
583bool RtpFrameReferenceFinder::UpSwitchInIntervalVp9(uint16_t picture_id,
584 uint8_t temporal_idx,
585 uint16_t pid_ref) {
586 for (auto up_switch_it = up_switch_.upper_bound(pid_ref);
587 up_switch_it != up_switch_.end() &&
588 AheadOf<uint16_t, kPicIdLength>(picture_id, up_switch_it->first);
589 ++up_switch_it) {
590 if (up_switch_it->second < temporal_idx)
591 return true;
592 }
593
594 return false;
595}
596
philipelafcf7f52017-04-26 08:17:35 -0700597void RtpFrameReferenceFinder::UnwrapPictureIds(RtpFrameObject* frame) {
philipel02447bc2016-05-13 06:01:03 -0700598 for (size_t i = 0; i < frame->num_references; ++i)
philipeld4fac692017-09-04 07:03:46 -0700599 frame->references[i] = unwrapper_.Unwrap(frame->references[i]);
600 frame->picture_id = unwrapper_.Unwrap(frame->picture_id);
philipel02447bc2016-05-13 06:01:03 -0700601}
602
philipel02447bc2016-05-13 06:01:03 -0700603} // namespace video_coding
604} // namespace webrtc