blob: a2f32c2a0c4ae829c52a0542676f9ea51cbc925b [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:
101 case kVideoCodecGeneric:
philipelafcf7f52017-04-26 08:17:35 -0700102 return ManageFrameGeneric(frame, kNoPictureId);
philipel02447bc2016-05-13 06:01:03 -0700103 }
philipelafcf7f52017-04-26 08:17:35 -0700104
105 // If not all code paths return a value it makes the win compiler sad.
106 RTC_NOTREACHED();
107 return kDrop;
philipel02447bc2016-05-13 06:01:03 -0700108}
109
philipel9b2ce6b2016-07-05 05:04:46 -0700110void RtpFrameReferenceFinder::PaddingReceived(uint16_t seq_num) {
111 rtc::CritScope lock(&crit_);
112 auto clean_padding_to =
113 stashed_padding_.lower_bound(seq_num - kMaxPaddingAge);
114 stashed_padding_.erase(stashed_padding_.begin(), clean_padding_to);
115 stashed_padding_.insert(seq_num);
116 UpdateLastPictureIdWithPadding(seq_num);
117 RetryStashedFrames();
118}
119
philipel463d3012016-09-09 03:32:44 -0700120void RtpFrameReferenceFinder::ClearTo(uint16_t seq_num) {
121 rtc::CritScope lock(&crit_);
122 cleared_to_seq_num_ = seq_num;
123
124 auto it = stashed_frames_.begin();
125 while (it != stashed_frames_.end()) {
126 if (AheadOf<uint16_t>(cleared_to_seq_num_, (*it)->first_seq_num())) {
127 it = stashed_frames_.erase(it);
128 } else {
129 ++it;
130 }
131 }
132}
133
philipel9b2ce6b2016-07-05 05:04:46 -0700134void RtpFrameReferenceFinder::UpdateLastPictureIdWithPadding(uint16_t seq_num) {
135 auto gop_seq_num_it = last_seq_num_gop_.upper_bound(seq_num);
136
137 // If this padding packet "belongs" to a group of pictures that we don't track
138 // anymore, do nothing.
139 if (gop_seq_num_it == last_seq_num_gop_.begin())
140 return;
141 --gop_seq_num_it;
142
143 // Calculate the next contiuous sequence number and search for it in
144 // the padding packets we have stashed.
145 uint16_t next_seq_num_with_padding = gop_seq_num_it->second.second + 1;
146 auto padding_seq_num_it =
147 stashed_padding_.lower_bound(next_seq_num_with_padding);
148
149 // While there still are padding packets and those padding packets are
150 // continuous, then advance the "last-picture-id-with-padding" and remove
151 // the stashed padding packet.
152 while (padding_seq_num_it != stashed_padding_.end() &&
153 *padding_seq_num_it == next_seq_num_with_padding) {
154 gop_seq_num_it->second.second = next_seq_num_with_padding;
155 ++next_seq_num_with_padding;
156 padding_seq_num_it = stashed_padding_.erase(padding_seq_num_it);
157 }
philipel41bb7922017-02-20 07:53:23 -0800158
159 // In the case where the stream has been continuous without any new keyframes
160 // for a while there is a risk that new frames will appear to be older than
161 // the keyframe they belong to due to wrapping sequence number. In order
162 // to prevent this we advance the picture id of the keyframe every so often.
163 if (ForwardDiff(gop_seq_num_it->first, seq_num) > 10000) {
164 RTC_DCHECK_EQ(1ul, last_seq_num_gop_.size());
165 last_seq_num_gop_[seq_num] = gop_seq_num_it->second;
166 last_seq_num_gop_.erase(gop_seq_num_it);
167 }
philipel9b2ce6b2016-07-05 05:04:46 -0700168}
169
philipelafcf7f52017-04-26 08:17:35 -0700170RtpFrameReferenceFinder::FrameDecision
171RtpFrameReferenceFinder::ManageFrameGeneric(RtpFrameObject* frame,
172 int picture_id) {
philipel647998c2016-06-03 09:40:16 -0700173 // If |picture_id| is specified then we use that to set the frame references,
174 // otherwise we use sequence number.
175 if (picture_id != kNoPictureId) {
176 if (last_unwrap_ == -1)
177 last_unwrap_ = picture_id;
178
philipeld4fac692017-09-04 07:03:46 -0700179 frame->picture_id = unwrapper_.Unwrap(picture_id);
philipel647998c2016-06-03 09:40:16 -0700180 frame->num_references = frame->frame_type() == kVideoFrameKey ? 0 : 1;
181 frame->references[0] = frame->picture_id - 1;
philipelafcf7f52017-04-26 08:17:35 -0700182 return kHandOff;
philipel647998c2016-06-03 09:40:16 -0700183 }
184
philipel9b2ce6b2016-07-05 05:04:46 -0700185 if (frame->frame_type() == kVideoFrameKey) {
186 last_seq_num_gop_.insert(std::make_pair(
187 frame->last_seq_num(),
188 std::make_pair(frame->last_seq_num(), frame->last_seq_num())));
189 }
philipel02447bc2016-05-13 06:01:03 -0700190
191 // We have received a frame but not yet a keyframe, stash this frame.
philipelafcf7f52017-04-26 08:17:35 -0700192 if (last_seq_num_gop_.empty())
193 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700194
195 // Clean up info for old keyframes but make sure to keep info
196 // for the last keyframe.
197 auto clean_to = last_seq_num_gop_.lower_bound(frame->last_seq_num() - 100);
philipel41bb7922017-02-20 07:53:23 -0800198 for (auto it = last_seq_num_gop_.begin();
199 it != clean_to && last_seq_num_gop_.size() > 1;) {
200 it = last_seq_num_gop_.erase(it);
201 }
philipel02447bc2016-05-13 06:01:03 -0700202
203 // Find the last sequence number of the last frame for the keyframe
204 // that this frame indirectly references.
205 auto seq_num_it = last_seq_num_gop_.upper_bound(frame->last_seq_num());
philipel9b2ce6b2016-07-05 05:04:46 -0700206 if (seq_num_it == last_seq_num_gop_.begin()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100207 RTC_LOG(LS_WARNING) << "Generic frame with packet range ["
208 << frame->first_seq_num() << ", "
209 << frame->last_seq_num()
210 << "] has no GoP, dropping frame.";
philipelafcf7f52017-04-26 08:17:35 -0700211 return kDrop;
philipel9b2ce6b2016-07-05 05:04:46 -0700212 }
philipel02447bc2016-05-13 06:01:03 -0700213 seq_num_it--;
214
215 // Make sure the packet sequence numbers are continuous, otherwise stash
216 // this frame.
philipel9b2ce6b2016-07-05 05:04:46 -0700217 uint16_t last_picture_id_gop = seq_num_it->second.first;
218 uint16_t last_picture_id_with_padding_gop = seq_num_it->second.second;
philipel02447bc2016-05-13 06:01:03 -0700219 if (frame->frame_type() == kVideoFrameDelta) {
philipel9b2ce6b2016-07-05 05:04:46 -0700220 uint16_t prev_seq_num = frame->first_seq_num() - 1;
philipelafcf7f52017-04-26 08:17:35 -0700221
222 if (prev_seq_num != last_picture_id_with_padding_gop)
223 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700224 }
225
226 RTC_DCHECK(AheadOrAt(frame->last_seq_num(), seq_num_it->first));
227
228 // Since keyframes can cause reordering we can't simply assign the
229 // picture id according to some incrementing counter.
230 frame->picture_id = frame->last_seq_num();
231 frame->num_references = frame->frame_type() == kVideoFrameDelta;
philipeld4fac692017-09-04 07:03:46 -0700232 frame->references[0] = generic_unwrapper_.Unwrap(last_picture_id_gop);
233 if (AheadOf<uint16_t>(frame->picture_id, last_picture_id_gop)) {
philipel9b2ce6b2016-07-05 05:04:46 -0700234 seq_num_it->second.first = frame->picture_id;
235 seq_num_it->second.second = frame->picture_id;
236 }
philipel02447bc2016-05-13 06:01:03 -0700237
238 last_picture_id_ = frame->picture_id;
philipel9b2ce6b2016-07-05 05:04:46 -0700239 UpdateLastPictureIdWithPadding(frame->picture_id);
philipeld4fac692017-09-04 07:03:46 -0700240 frame->picture_id = generic_unwrapper_.Unwrap(frame->picture_id);
philipelafcf7f52017-04-26 08:17:35 -0700241 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700242}
243
philipelafcf7f52017-04-26 08:17:35 -0700244RtpFrameReferenceFinder::FrameDecision RtpFrameReferenceFinder::ManageFrameVp8(
245 RtpFrameObject* frame) {
philipel88488282016-11-03 08:56:54 -0700246 rtc::Optional<RTPVideoTypeHeader> rtp_codec_header = frame->GetCodecHeader();
philipeld4fac692017-09-04 07:03:46 -0700247 if (!rtp_codec_header) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100248 RTC_LOG(LS_WARNING)
249 << "Failed to get codec header from frame, dropping frame.";
philipelafcf7f52017-04-26 08:17:35 -0700250 return kDrop;
philipeld4fac692017-09-04 07:03:46 -0700251 }
philipel02447bc2016-05-13 06:01:03 -0700252
253 const RTPVideoHeaderVP8& codec_header = rtp_codec_header->VP8;
254
255 if (codec_header.pictureId == kNoPictureId ||
256 codec_header.temporalIdx == kNoTemporalIdx ||
257 codec_header.tl0PicIdx == kNoTl0PicIdx) {
philipelafcf7f52017-04-26 08:17:35 -0700258 return ManageFrameGeneric(std::move(frame), codec_header.pictureId);
philipel02447bc2016-05-13 06:01:03 -0700259 }
260
261 frame->picture_id = codec_header.pictureId % kPicIdLength;
262
263 if (last_unwrap_ == -1)
264 last_unwrap_ = codec_header.pictureId;
265
266 if (last_picture_id_ == -1)
267 last_picture_id_ = frame->picture_id;
268
269 // Find if there has been a gap in fully received frames and save the picture
270 // id of those frames in |not_yet_received_frames_|.
271 if (AheadOf<uint16_t, kPicIdLength>(frame->picture_id, last_picture_id_)) {
philipel9bd1d662017-07-14 04:52:01 -0700272 do {
philipel02447bc2016-05-13 06:01:03 -0700273 last_picture_id_ = Add<kPicIdLength>(last_picture_id_, 1);
philipel9bd1d662017-07-14 04:52:01 -0700274 not_yet_received_frames_.insert(last_picture_id_);
275 } while (last_picture_id_ != frame->picture_id);
philipel02447bc2016-05-13 06:01:03 -0700276 }
277
278 // Clean up info for base layers that are too old.
279 uint8_t old_tl0_pic_idx = codec_header.tl0PicIdx - kMaxLayerInfo;
280 auto clean_layer_info_to = layer_info_.lower_bound(old_tl0_pic_idx);
281 layer_info_.erase(layer_info_.begin(), clean_layer_info_to);
282
283 // Clean up info about not yet received frames that are too old.
284 uint16_t old_picture_id =
285 Subtract<kPicIdLength>(frame->picture_id, kMaxNotYetReceivedFrames);
286 auto clean_frames_to = not_yet_received_frames_.lower_bound(old_picture_id);
287 not_yet_received_frames_.erase(not_yet_received_frames_.begin(),
288 clean_frames_to);
289
290 if (frame->frame_type() == kVideoFrameKey) {
291 frame->num_references = 0;
292 layer_info_[codec_header.tl0PicIdx].fill(-1);
philipeld4fac692017-09-04 07:03:46 -0700293 UpdateLayerInfoVp8(frame);
philipelafcf7f52017-04-26 08:17:35 -0700294 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700295 }
296
297 auto layer_info_it = layer_info_.find(codec_header.temporalIdx == 0
298 ? codec_header.tl0PicIdx - 1
299 : codec_header.tl0PicIdx);
300
301 // If we don't have the base layer frame yet, stash this frame.
philipelafcf7f52017-04-26 08:17:35 -0700302 if (layer_info_it == layer_info_.end())
303 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700304
305 // A non keyframe base layer frame has been received, copy the layer info
306 // from the previous base layer frame and set a reference to the previous
307 // base layer frame.
308 if (codec_header.temporalIdx == 0) {
309 layer_info_it =
310 layer_info_
311 .insert(make_pair(codec_header.tl0PicIdx, layer_info_it->second))
312 .first;
313 frame->num_references = 1;
314 frame->references[0] = layer_info_it->second[0];
philipeld4fac692017-09-04 07:03:46 -0700315 UpdateLayerInfoVp8(frame);
philipelafcf7f52017-04-26 08:17:35 -0700316 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700317 }
318
319 // Layer sync frame, this frame only references its base layer frame.
320 if (codec_header.layerSync) {
321 frame->num_references = 1;
322 frame->references[0] = layer_info_it->second[0];
323
philipeld4fac692017-09-04 07:03:46 -0700324 UpdateLayerInfoVp8(frame);
philipelafcf7f52017-04-26 08:17:35 -0700325 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700326 }
327
328 // Find all references for this frame.
329 frame->num_references = 0;
330 for (uint8_t layer = 0; layer <= codec_header.temporalIdx; ++layer) {
philipeld268d6f2016-09-15 13:43:13 +0200331 // If we have not yet received a previous frame on this temporal layer,
332 // stash this frame.
philipelafcf7f52017-04-26 08:17:35 -0700333 if (layer_info_it->second[layer] == -1)
334 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700335
philipel86b92e02016-10-24 07:11:53 -0700336 // If the last frame on this layer is ahead of this frame it means that
337 // a layer sync frame has been received after this frame for the same
338 // base layer frame, drop this frame.
339 if (AheadOf<uint16_t, kPicIdLength>(layer_info_it->second[layer],
340 frame->picture_id)) {
philipelafcf7f52017-04-26 08:17:35 -0700341 return kDrop;
philipel86b92e02016-10-24 07:11:53 -0700342 }
343
philipel02447bc2016-05-13 06:01:03 -0700344 // If we have not yet received a frame between this frame and the referenced
345 // frame then we have to wait for that frame to be completed first.
346 auto not_received_frame_it =
347 not_yet_received_frames_.upper_bound(layer_info_it->second[layer]);
348 if (not_received_frame_it != not_yet_received_frames_.end() &&
349 AheadOf<uint16_t, kPicIdLength>(frame->picture_id,
350 *not_received_frame_it)) {
philipelafcf7f52017-04-26 08:17:35 -0700351 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700352 }
353
philipel57f19cc2017-03-07 03:54:05 -0800354 if (!(AheadOf<uint16_t, kPicIdLength>(frame->picture_id,
355 layer_info_it->second[layer]))) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100356 RTC_LOG(LS_WARNING) << "Frame with picture id " << frame->picture_id
357 << " and packet range [" << frame->first_seq_num()
358 << ", " << frame->last_seq_num()
359 << "] already received, "
360 << " dropping frame.";
philipelafcf7f52017-04-26 08:17:35 -0700361 return kDrop;
philipel57f19cc2017-03-07 03:54:05 -0800362 }
363
philipel02447bc2016-05-13 06:01:03 -0700364 ++frame->num_references;
365 frame->references[layer] = layer_info_it->second[layer];
366 }
367
philipeld4fac692017-09-04 07:03:46 -0700368 UpdateLayerInfoVp8(frame);
philipelafcf7f52017-04-26 08:17:35 -0700369 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700370}
371
philipeld4fac692017-09-04 07:03:46 -0700372void RtpFrameReferenceFinder::UpdateLayerInfoVp8(RtpFrameObject* frame) {
373 rtc::Optional<RTPVideoTypeHeader> rtp_codec_header = frame->GetCodecHeader();
374 RTC_DCHECK(rtp_codec_header);
375 const RTPVideoHeaderVP8& codec_header = rtp_codec_header->VP8;
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