blob: e167e15c52cc60355fbef3dbb13e6d6a89145fac [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
11#include "webrtc/modules/video_coding/rtp_frame_reference_finder.h"
12
13#include <algorithm>
14#include <limits>
15
philipel02447bc2016-05-13 06:01:03 -070016#include "webrtc/modules/video_coding/frame_object.h"
17#include "webrtc/modules/video_coding/packet_buffer.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020018#include "webrtc/rtc_base/checks.h"
19#include "webrtc/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()) {
207 LOG(LS_WARNING) << "Generic frame with packet range ["
208 << frame->first_seq_num() << ", " << frame->last_seq_num()
philipel41bb7922017-02-20 07:53:23 -0800209 << "] has no GoP, dropping frame.";
philipelafcf7f52017-04-26 08:17:35 -0700210 return kDrop;
philipel9b2ce6b2016-07-05 05:04:46 -0700211 }
philipel02447bc2016-05-13 06:01:03 -0700212 seq_num_it--;
213
214 // Make sure the packet sequence numbers are continuous, otherwise stash
215 // this frame.
philipel9b2ce6b2016-07-05 05:04:46 -0700216 uint16_t last_picture_id_gop = seq_num_it->second.first;
217 uint16_t last_picture_id_with_padding_gop = seq_num_it->second.second;
philipel02447bc2016-05-13 06:01:03 -0700218 if (frame->frame_type() == kVideoFrameDelta) {
philipel9b2ce6b2016-07-05 05:04:46 -0700219 uint16_t prev_seq_num = frame->first_seq_num() - 1;
philipelafcf7f52017-04-26 08:17:35 -0700220
221 if (prev_seq_num != last_picture_id_with_padding_gop)
222 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700223 }
224
225 RTC_DCHECK(AheadOrAt(frame->last_seq_num(), seq_num_it->first));
226
227 // Since keyframes can cause reordering we can't simply assign the
228 // picture id according to some incrementing counter.
229 frame->picture_id = frame->last_seq_num();
230 frame->num_references = frame->frame_type() == kVideoFrameDelta;
philipeld4fac692017-09-04 07:03:46 -0700231 frame->references[0] = generic_unwrapper_.Unwrap(last_picture_id_gop);
232 if (AheadOf<uint16_t>(frame->picture_id, last_picture_id_gop)) {
philipel9b2ce6b2016-07-05 05:04:46 -0700233 seq_num_it->second.first = frame->picture_id;
234 seq_num_it->second.second = frame->picture_id;
235 }
philipel02447bc2016-05-13 06:01:03 -0700236
237 last_picture_id_ = frame->picture_id;
philipel9b2ce6b2016-07-05 05:04:46 -0700238 UpdateLastPictureIdWithPadding(frame->picture_id);
philipeld4fac692017-09-04 07:03:46 -0700239 frame->picture_id = generic_unwrapper_.Unwrap(frame->picture_id);
philipelafcf7f52017-04-26 08:17:35 -0700240 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700241}
242
philipelafcf7f52017-04-26 08:17:35 -0700243RtpFrameReferenceFinder::FrameDecision RtpFrameReferenceFinder::ManageFrameVp8(
244 RtpFrameObject* frame) {
philipel88488282016-11-03 08:56:54 -0700245 rtc::Optional<RTPVideoTypeHeader> rtp_codec_header = frame->GetCodecHeader();
philipeld4fac692017-09-04 07:03:46 -0700246 if (!rtp_codec_header) {
247 LOG(LS_WARNING) << "Failed to get codec header from frame, dropping frame.";
philipelafcf7f52017-04-26 08:17:35 -0700248 return kDrop;
philipeld4fac692017-09-04 07:03:46 -0700249 }
philipel02447bc2016-05-13 06:01:03 -0700250
251 const RTPVideoHeaderVP8& codec_header = rtp_codec_header->VP8;
252
253 if (codec_header.pictureId == kNoPictureId ||
254 codec_header.temporalIdx == kNoTemporalIdx ||
255 codec_header.tl0PicIdx == kNoTl0PicIdx) {
philipelafcf7f52017-04-26 08:17:35 -0700256 return ManageFrameGeneric(std::move(frame), codec_header.pictureId);
philipel02447bc2016-05-13 06:01:03 -0700257 }
258
259 frame->picture_id = codec_header.pictureId % kPicIdLength;
260
261 if (last_unwrap_ == -1)
262 last_unwrap_ = codec_header.pictureId;
263
264 if (last_picture_id_ == -1)
265 last_picture_id_ = frame->picture_id;
266
267 // Find if there has been a gap in fully received frames and save the picture
268 // id of those frames in |not_yet_received_frames_|.
269 if (AheadOf<uint16_t, kPicIdLength>(frame->picture_id, last_picture_id_)) {
philipel9bd1d662017-07-14 04:52:01 -0700270 do {
philipel02447bc2016-05-13 06:01:03 -0700271 last_picture_id_ = Add<kPicIdLength>(last_picture_id_, 1);
philipel9bd1d662017-07-14 04:52:01 -0700272 not_yet_received_frames_.insert(last_picture_id_);
273 } while (last_picture_id_ != frame->picture_id);
philipel02447bc2016-05-13 06:01:03 -0700274 }
275
276 // Clean up info for base layers that are too old.
277 uint8_t old_tl0_pic_idx = codec_header.tl0PicIdx - kMaxLayerInfo;
278 auto clean_layer_info_to = layer_info_.lower_bound(old_tl0_pic_idx);
279 layer_info_.erase(layer_info_.begin(), clean_layer_info_to);
280
281 // Clean up info about not yet received frames that are too old.
282 uint16_t old_picture_id =
283 Subtract<kPicIdLength>(frame->picture_id, kMaxNotYetReceivedFrames);
284 auto clean_frames_to = not_yet_received_frames_.lower_bound(old_picture_id);
285 not_yet_received_frames_.erase(not_yet_received_frames_.begin(),
286 clean_frames_to);
287
288 if (frame->frame_type() == kVideoFrameKey) {
289 frame->num_references = 0;
290 layer_info_[codec_header.tl0PicIdx].fill(-1);
philipeld4fac692017-09-04 07:03:46 -0700291 UpdateLayerInfoVp8(frame);
philipelafcf7f52017-04-26 08:17:35 -0700292 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700293 }
294
295 auto layer_info_it = layer_info_.find(codec_header.temporalIdx == 0
296 ? codec_header.tl0PicIdx - 1
297 : codec_header.tl0PicIdx);
298
299 // If we don't have the base layer frame yet, stash this frame.
philipelafcf7f52017-04-26 08:17:35 -0700300 if (layer_info_it == layer_info_.end())
301 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700302
303 // A non keyframe base layer frame has been received, copy the layer info
304 // from the previous base layer frame and set a reference to the previous
305 // base layer frame.
306 if (codec_header.temporalIdx == 0) {
307 layer_info_it =
308 layer_info_
309 .insert(make_pair(codec_header.tl0PicIdx, layer_info_it->second))
310 .first;
311 frame->num_references = 1;
312 frame->references[0] = layer_info_it->second[0];
philipeld4fac692017-09-04 07:03:46 -0700313 UpdateLayerInfoVp8(frame);
philipelafcf7f52017-04-26 08:17:35 -0700314 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700315 }
316
317 // Layer sync frame, this frame only references its base layer frame.
318 if (codec_header.layerSync) {
319 frame->num_references = 1;
320 frame->references[0] = layer_info_it->second[0];
321
philipeld4fac692017-09-04 07:03:46 -0700322 UpdateLayerInfoVp8(frame);
philipelafcf7f52017-04-26 08:17:35 -0700323 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700324 }
325
326 // Find all references for this frame.
327 frame->num_references = 0;
328 for (uint8_t layer = 0; layer <= codec_header.temporalIdx; ++layer) {
philipeld268d6f2016-09-15 13:43:13 +0200329 // If we have not yet received a previous frame on this temporal layer,
330 // stash this frame.
philipelafcf7f52017-04-26 08:17:35 -0700331 if (layer_info_it->second[layer] == -1)
332 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700333
philipel86b92e02016-10-24 07:11:53 -0700334 // If the last frame on this layer is ahead of this frame it means that
335 // a layer sync frame has been received after this frame for the same
336 // base layer frame, drop this frame.
337 if (AheadOf<uint16_t, kPicIdLength>(layer_info_it->second[layer],
338 frame->picture_id)) {
philipelafcf7f52017-04-26 08:17:35 -0700339 return kDrop;
philipel86b92e02016-10-24 07:11:53 -0700340 }
341
philipel02447bc2016-05-13 06:01:03 -0700342 // If we have not yet received a frame between this frame and the referenced
343 // frame then we have to wait for that frame to be completed first.
344 auto not_received_frame_it =
345 not_yet_received_frames_.upper_bound(layer_info_it->second[layer]);
346 if (not_received_frame_it != not_yet_received_frames_.end() &&
347 AheadOf<uint16_t, kPicIdLength>(frame->picture_id,
348 *not_received_frame_it)) {
philipelafcf7f52017-04-26 08:17:35 -0700349 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700350 }
351
philipel57f19cc2017-03-07 03:54:05 -0800352 if (!(AheadOf<uint16_t, kPicIdLength>(frame->picture_id,
353 layer_info_it->second[layer]))) {
354 LOG(LS_WARNING) << "Frame with picture id " << frame->picture_id
355 << " and packet range [" << frame->first_seq_num() << ", "
356 << frame->last_seq_num() << "] already received, "
357 << " dropping frame.";
philipelafcf7f52017-04-26 08:17:35 -0700358 return kDrop;
philipel57f19cc2017-03-07 03:54:05 -0800359 }
360
philipel02447bc2016-05-13 06:01:03 -0700361 ++frame->num_references;
362 frame->references[layer] = layer_info_it->second[layer];
363 }
364
philipeld4fac692017-09-04 07:03:46 -0700365 UpdateLayerInfoVp8(frame);
philipelafcf7f52017-04-26 08:17:35 -0700366 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700367}
368
philipeld4fac692017-09-04 07:03:46 -0700369void RtpFrameReferenceFinder::UpdateLayerInfoVp8(RtpFrameObject* frame) {
370 rtc::Optional<RTPVideoTypeHeader> rtp_codec_header = frame->GetCodecHeader();
371 RTC_DCHECK(rtp_codec_header);
372 const RTPVideoHeaderVP8& codec_header = rtp_codec_header->VP8;
philipel02447bc2016-05-13 06:01:03 -0700373 uint8_t tl0_pic_idx = codec_header.tl0PicIdx;
374 uint8_t temporal_index = codec_header.temporalIdx;
375 auto layer_info_it = layer_info_.find(tl0_pic_idx);
376
377 // Update this layer info and newer.
378 while (layer_info_it != layer_info_.end()) {
379 if (layer_info_it->second[temporal_index] != -1 &&
380 AheadOf<uint16_t, kPicIdLength>(layer_info_it->second[temporal_index],
381 frame->picture_id)) {
382 // The frame was not newer, then no subsequent layer info have to be
383 // update.
384 break;
385 }
386
387 layer_info_it->second[codec_header.temporalIdx] = frame->picture_id;
388 ++tl0_pic_idx;
389 layer_info_it = layer_info_.find(tl0_pic_idx);
390 }
391 not_yet_received_frames_.erase(frame->picture_id);
392
philipelafcf7f52017-04-26 08:17:35 -0700393 UnwrapPictureIds(frame);
philipel02447bc2016-05-13 06:01:03 -0700394}
395
philipelafcf7f52017-04-26 08:17:35 -0700396RtpFrameReferenceFinder::FrameDecision RtpFrameReferenceFinder::ManageFrameVp9(
397 RtpFrameObject* frame) {
philipel88488282016-11-03 08:56:54 -0700398 rtc::Optional<RTPVideoTypeHeader> rtp_codec_header = frame->GetCodecHeader();
philipeld4fac692017-09-04 07:03:46 -0700399 if (!rtp_codec_header) {
400 LOG(LS_WARNING) << "Failed to get codec header from frame, dropping frame.";
philipel4c140092017-08-31 08:31:45 -0700401 return kDrop;
philipeld4fac692017-09-04 07:03:46 -0700402 }
philipel4c140092017-08-31 08:31:45 -0700403
philipel02447bc2016-05-13 06:01:03 -0700404 const RTPVideoHeaderVP9& codec_header = rtp_codec_header->VP9;
405
philipel647998c2016-06-03 09:40:16 -0700406 if (codec_header.picture_id == kNoPictureId ||
407 codec_header.temporal_idx == kNoTemporalIdx) {
philipelafcf7f52017-04-26 08:17:35 -0700408 return ManageFrameGeneric(std::move(frame), codec_header.picture_id);
philipel02447bc2016-05-13 06:01:03 -0700409 }
410
411 frame->spatial_layer = codec_header.spatial_idx;
412 frame->inter_layer_predicted = codec_header.inter_layer_predicted;
413 frame->picture_id = codec_header.picture_id % kPicIdLength;
414
415 if (last_unwrap_ == -1)
416 last_unwrap_ = codec_header.picture_id;
417
418 if (last_picture_id_ == -1)
419 last_picture_id_ = frame->picture_id;
420
421 if (codec_header.flexible_mode) {
422 frame->num_references = codec_header.num_ref_pics;
423 for (size_t i = 0; i < frame->num_references; ++i) {
424 frame->references[i] =
425 Subtract<1 << 16>(frame->picture_id, codec_header.pid_diff[i]);
426 }
427
philipelafcf7f52017-04-26 08:17:35 -0700428 UnwrapPictureIds(frame);
429 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700430 }
431
432 if (codec_header.ss_data_available) {
433 // Scalability structures can only be sent with tl0 frames.
434 if (codec_header.temporal_idx != 0) {
435 LOG(LS_WARNING) << "Received scalability structure on a non base layer"
436 " frame. Scalability structure ignored.";
437 } else {
438 current_ss_idx_ = Add<kMaxGofSaved>(current_ss_idx_, 1);
439 scalability_structures_[current_ss_idx_] = codec_header.gof;
440 scalability_structures_[current_ss_idx_].pid_start = frame->picture_id;
441
philipelc9b27d52016-07-15 06:50:27 -0700442 GofInfo info(&scalability_structures_[current_ss_idx_],
443 frame->picture_id);
444 gof_info_.insert(std::make_pair(codec_header.tl0_pic_idx, info));
philipel02447bc2016-05-13 06:01:03 -0700445 }
446 }
447
448 // Clean up info for base layers that are too old.
449 uint8_t old_tl0_pic_idx = codec_header.tl0_pic_idx - kMaxGofSaved;
450 auto clean_gof_info_to = gof_info_.lower_bound(old_tl0_pic_idx);
451 gof_info_.erase(gof_info_.begin(), clean_gof_info_to);
452
453 if (frame->frame_type() == kVideoFrameKey) {
454 // When using GOF all keyframes must include the scalability structure.
455 if (!codec_header.ss_data_available)
456 LOG(LS_WARNING) << "Received keyframe without scalability structure";
457
458 frame->num_references = 0;
philipelc9b27d52016-07-15 06:50:27 -0700459 GofInfo info = gof_info_.find(codec_header.tl0_pic_idx)->second;
460 FrameReceivedVp9(frame->picture_id, &info);
philipelafcf7f52017-04-26 08:17:35 -0700461 UnwrapPictureIds(frame);
462 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700463 }
464
465 auto gof_info_it = gof_info_.find(
466 (codec_header.temporal_idx == 0 && !codec_header.ss_data_available)
467 ? codec_header.tl0_pic_idx - 1
468 : codec_header.tl0_pic_idx);
469
470 // Gof info for this frame is not available yet, stash this frame.
philipelafcf7f52017-04-26 08:17:35 -0700471 if (gof_info_it == gof_info_.end())
472 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700473
philipelc9b27d52016-07-15 06:50:27 -0700474 GofInfo* info = &gof_info_it->second;
475 FrameReceivedVp9(frame->picture_id, info);
philipel02447bc2016-05-13 06:01:03 -0700476
477 // Make sure we don't miss any frame that could potentially have the
478 // up switch flag set.
philipelafcf7f52017-04-26 08:17:35 -0700479 if (MissingRequiredFrameVp9(frame->picture_id, *info))
480 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700481
482 if (codec_header.temporal_up_switch) {
483 auto pid_tidx =
484 std::make_pair(frame->picture_id, codec_header.temporal_idx);
485 up_switch_.insert(pid_tidx);
486 }
487
488 // If this is a base layer frame that contains a scalability structure
489 // then gof info has already been inserted earlier, so we only want to
490 // insert if we haven't done so already.
491 if (codec_header.temporal_idx == 0 && !codec_header.ss_data_available) {
philipelc9b27d52016-07-15 06:50:27 -0700492 GofInfo new_info(info->gof, frame->picture_id);
493 gof_info_.insert(std::make_pair(codec_header.tl0_pic_idx, new_info));
philipel02447bc2016-05-13 06:01:03 -0700494 }
495
496 // Clean out old info about up switch frames.
philipelc9b27d52016-07-15 06:50:27 -0700497 uint16_t old_picture_id = Subtract<kPicIdLength>(frame->picture_id, 50);
philipel02447bc2016-05-13 06:01:03 -0700498 auto up_switch_erase_to = up_switch_.lower_bound(old_picture_id);
499 up_switch_.erase(up_switch_.begin(), up_switch_erase_to);
500
philipelc9b27d52016-07-15 06:50:27 -0700501 size_t diff = ForwardDiff<uint16_t, kPicIdLength>(info->gof->pid_start,
502 frame->picture_id);
503 size_t gof_idx = diff % info->gof->num_frames_in_gof;
philipel02447bc2016-05-13 06:01:03 -0700504
505 // Populate references according to the scalability structure.
philipelc9b27d52016-07-15 06:50:27 -0700506 frame->num_references = info->gof->num_ref_pics[gof_idx];
philipel02447bc2016-05-13 06:01:03 -0700507 for (size_t i = 0; i < frame->num_references; ++i) {
philipelc9b27d52016-07-15 06:50:27 -0700508 frame->references[i] = Subtract<kPicIdLength>(
509 frame->picture_id, info->gof->pid_diff[gof_idx][i]);
philipel02447bc2016-05-13 06:01:03 -0700510
511 // If this is a reference to a frame earlier than the last up switch point,
512 // then ignore this reference.
513 if (UpSwitchInIntervalVp9(frame->picture_id, codec_header.temporal_idx,
514 frame->references[i])) {
515 --frame->num_references;
516 }
517 }
518
philipelafcf7f52017-04-26 08:17:35 -0700519 UnwrapPictureIds(frame);
520 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700521}
522
523bool RtpFrameReferenceFinder::MissingRequiredFrameVp9(uint16_t picture_id,
philipelc9b27d52016-07-15 06:50:27 -0700524 const GofInfo& info) {
525 size_t diff =
526 ForwardDiff<uint16_t, kPicIdLength>(info.gof->pid_start, picture_id);
527 size_t gof_idx = diff % info.gof->num_frames_in_gof;
528 size_t temporal_idx = info.gof->temporal_idx[gof_idx];
philipel02447bc2016-05-13 06:01:03 -0700529
530 // For every reference this frame has, check if there is a frame missing in
531 // the interval (|ref_pid|, |picture_id|) in any of the lower temporal
532 // layers. If so, we are missing a required frame.
philipelc9b27d52016-07-15 06:50:27 -0700533 uint8_t num_references = info.gof->num_ref_pics[gof_idx];
philipel02447bc2016-05-13 06:01:03 -0700534 for (size_t i = 0; i < num_references; ++i) {
535 uint16_t ref_pid =
philipelc9b27d52016-07-15 06:50:27 -0700536 Subtract<kPicIdLength>(picture_id, info.gof->pid_diff[gof_idx][i]);
philipel02447bc2016-05-13 06:01:03 -0700537 for (size_t l = 0; l < temporal_idx; ++l) {
538 auto missing_frame_it = missing_frames_for_layer_[l].lower_bound(ref_pid);
539 if (missing_frame_it != missing_frames_for_layer_[l].end() &&
540 AheadOf<uint16_t, kPicIdLength>(picture_id, *missing_frame_it)) {
541 return true;
542 }
543 }
544 }
545 return false;
546}
547
548void RtpFrameReferenceFinder::FrameReceivedVp9(uint16_t picture_id,
philipelc9b27d52016-07-15 06:50:27 -0700549 GofInfo* info) {
550 int last_picture_id = info->last_picture_id;
philipel02447bc2016-05-13 06:01:03 -0700551
552 // If there is a gap, find which temporal layer the missing frames
553 // belong to and add the frame as missing for that temporal layer.
554 // Otherwise, remove this frame from the set of missing frames.
philipelc9b27d52016-07-15 06:50:27 -0700555 if (AheadOf<uint16_t, kPicIdLength>(picture_id, last_picture_id)) {
556 size_t diff = ForwardDiff<uint16_t, kPicIdLength>(info->gof->pid_start,
557 last_picture_id);
558 size_t gof_idx = diff % info->gof->num_frames_in_gof;
philipel02447bc2016-05-13 06:01:03 -0700559
philipelc9b27d52016-07-15 06:50:27 -0700560 last_picture_id = Add<kPicIdLength>(last_picture_id, 1);
561 while (last_picture_id != picture_id) {
philipel02447bc2016-05-13 06:01:03 -0700562 ++gof_idx;
philipelc9b27d52016-07-15 06:50:27 -0700563 RTC_DCHECK_NE(0ul, gof_idx % info->gof->num_frames_in_gof);
564 size_t temporal_idx = info->gof->temporal_idx[gof_idx];
565 missing_frames_for_layer_[temporal_idx].insert(last_picture_id);
566 last_picture_id = Add<kPicIdLength>(last_picture_id, 1);
philipel02447bc2016-05-13 06:01:03 -0700567 }
philipelc9b27d52016-07-15 06:50:27 -0700568 info->last_picture_id = last_picture_id;
philipel02447bc2016-05-13 06:01:03 -0700569 } else {
570 size_t diff =
philipelc9b27d52016-07-15 06:50:27 -0700571 ForwardDiff<uint16_t, kPicIdLength>(info->gof->pid_start, picture_id);
572 size_t gof_idx = diff % info->gof->num_frames_in_gof;
573 size_t temporal_idx = info->gof->temporal_idx[gof_idx];
philipel02447bc2016-05-13 06:01:03 -0700574 missing_frames_for_layer_[temporal_idx].erase(picture_id);
575 }
576}
577
578bool RtpFrameReferenceFinder::UpSwitchInIntervalVp9(uint16_t picture_id,
579 uint8_t temporal_idx,
580 uint16_t pid_ref) {
581 for (auto up_switch_it = up_switch_.upper_bound(pid_ref);
582 up_switch_it != up_switch_.end() &&
583 AheadOf<uint16_t, kPicIdLength>(picture_id, up_switch_it->first);
584 ++up_switch_it) {
585 if (up_switch_it->second < temporal_idx)
586 return true;
587 }
588
589 return false;
590}
591
philipelafcf7f52017-04-26 08:17:35 -0700592void RtpFrameReferenceFinder::UnwrapPictureIds(RtpFrameObject* frame) {
philipel02447bc2016-05-13 06:01:03 -0700593 for (size_t i = 0; i < frame->num_references; ++i)
philipeld4fac692017-09-04 07:03:46 -0700594 frame->references[i] = unwrapper_.Unwrap(frame->references[i]);
595 frame->picture_id = unwrapper_.Unwrap(frame->picture_id);
philipel02447bc2016-05-13 06:01:03 -0700596}
597
philipel02447bc2016-05-13 06:01:03 -0700598} // namespace video_coding
599} // namespace webrtc