blob: f05f8d3e48a5d988d685a48e46ef797c22a49279 [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"
Karl Wiberg80ba3332018-02-05 10:33:35 +010020#include "rtc_base/system/fallthrough.h"
philipel02447bc2016-05-13 06:01:03 -070021
22namespace webrtc {
23namespace video_coding {
24
25RtpFrameReferenceFinder::RtpFrameReferenceFinder(
26 OnCompleteFrameCallback* frame_callback)
27 : last_picture_id_(-1),
28 last_unwrap_(-1),
29 current_ss_idx_(0),
philipel463d3012016-09-09 03:32:44 -070030 cleared_to_seq_num_(-1),
philipel02447bc2016-05-13 06:01:03 -070031 frame_callback_(frame_callback) {}
32
33void RtpFrameReferenceFinder::ManageFrame(
34 std::unique_ptr<RtpFrameObject> frame) {
35 rtc::CritScope lock(&crit_);
philipel463d3012016-09-09 03:32:44 -070036
37 // If we have cleared past this frame, drop it.
38 if (cleared_to_seq_num_ != -1 &&
39 AheadOf<uint16_t>(cleared_to_seq_num_, frame->first_seq_num())) {
40 return;
41 }
42
philipelafcf7f52017-04-26 08:17:35 -070043 FrameDecision decision = ManageFrameInternal(frame.get());
44
45 switch (decision) {
46 case kStash:
47 if (stashed_frames_.size() > kMaxStashedFrames)
48 stashed_frames_.pop_back();
49 stashed_frames_.push_front(std::move(frame));
50 break;
51 case kHandOff:
52 frame_callback_->OnCompleteFrame(std::move(frame));
53 RetryStashedFrames();
54 break;
55 case kDrop:
56 break;
57 }
58}
59
60void RtpFrameReferenceFinder::RetryStashedFrames() {
61 bool complete_frame = false;
62 do {
63 complete_frame = false;
64 for (auto frame_it = stashed_frames_.begin();
65 frame_it != stashed_frames_.end();) {
66 FrameDecision decision = ManageFrameInternal(frame_it->get());
67
68 switch (decision) {
69 case kStash:
70 ++frame_it;
71 break;
72 case kHandOff:
73 complete_frame = true;
74 frame_callback_->OnCompleteFrame(std::move(*frame_it));
Karl Wiberg80ba3332018-02-05 10:33:35 +010075 RTC_FALLTHROUGH();
philipelafcf7f52017-04-26 08:17:35 -070076 case kDrop:
77 frame_it = stashed_frames_.erase(frame_it);
78 }
79 }
80 } while (complete_frame);
81}
82
83RtpFrameReferenceFinder::FrameDecision
84RtpFrameReferenceFinder::ManageFrameInternal(RtpFrameObject* frame) {
philipel02447bc2016-05-13 06:01:03 -070085 switch (frame->codec_type()) {
brandtr87d7d772016-11-07 03:03:41 -080086 case kVideoCodecFlexfec:
philipel02447bc2016-05-13 06:01:03 -070087 case kVideoCodecULPFEC:
88 case kVideoCodecRED:
philipel02447bc2016-05-13 06:01:03 -070089 RTC_NOTREACHED();
90 break;
91 case kVideoCodecVP8:
philipelafcf7f52017-04-26 08:17:35 -070092 return ManageFrameVp8(frame);
philipel02447bc2016-05-13 06:01:03 -070093 case kVideoCodecVP9:
philipelafcf7f52017-04-26 08:17:35 -070094 return ManageFrameVp9(frame);
philipel266f0a42016-11-28 08:49:07 -080095 // Since the EndToEndTests use kVicdeoCodecUnknow we treat it the same as
96 // kVideoCodecGeneric.
97 // TODO(philipel): Take a look at the EndToEndTests and see if maybe they
98 // should be changed to use kVideoCodecGeneric instead.
99 case kVideoCodecUnknown:
philipel02447bc2016-05-13 06:01:03 -0700100 case kVideoCodecH264:
101 case kVideoCodecI420:
Emircan Uysalerd7ae3c32018-01-25 13:01:09 -0800102 case kVideoCodecMultiplex:
philipel02447bc2016-05-13 06:01:03 -0700103 case kVideoCodecGeneric:
philipelafcf7f52017-04-26 08:17:35 -0700104 return ManageFrameGeneric(frame, kNoPictureId);
philipel02447bc2016-05-13 06:01:03 -0700105 }
philipelafcf7f52017-04-26 08:17:35 -0700106
107 // If not all code paths return a value it makes the win compiler sad.
108 RTC_NOTREACHED();
109 return kDrop;
philipel02447bc2016-05-13 06:01:03 -0700110}
111
philipel9b2ce6b2016-07-05 05:04:46 -0700112void RtpFrameReferenceFinder::PaddingReceived(uint16_t seq_num) {
113 rtc::CritScope lock(&crit_);
114 auto clean_padding_to =
115 stashed_padding_.lower_bound(seq_num - kMaxPaddingAge);
116 stashed_padding_.erase(stashed_padding_.begin(), clean_padding_to);
117 stashed_padding_.insert(seq_num);
118 UpdateLastPictureIdWithPadding(seq_num);
119 RetryStashedFrames();
120}
121
philipel463d3012016-09-09 03:32:44 -0700122void RtpFrameReferenceFinder::ClearTo(uint16_t seq_num) {
123 rtc::CritScope lock(&crit_);
124 cleared_to_seq_num_ = seq_num;
125
126 auto it = stashed_frames_.begin();
127 while (it != stashed_frames_.end()) {
128 if (AheadOf<uint16_t>(cleared_to_seq_num_, (*it)->first_seq_num())) {
129 it = stashed_frames_.erase(it);
130 } else {
131 ++it;
132 }
133 }
134}
135
philipel9b2ce6b2016-07-05 05:04:46 -0700136void RtpFrameReferenceFinder::UpdateLastPictureIdWithPadding(uint16_t seq_num) {
137 auto gop_seq_num_it = last_seq_num_gop_.upper_bound(seq_num);
138
139 // If this padding packet "belongs" to a group of pictures that we don't track
140 // anymore, do nothing.
141 if (gop_seq_num_it == last_seq_num_gop_.begin())
142 return;
143 --gop_seq_num_it;
144
145 // Calculate the next contiuous sequence number and search for it in
146 // the padding packets we have stashed.
147 uint16_t next_seq_num_with_padding = gop_seq_num_it->second.second + 1;
148 auto padding_seq_num_it =
149 stashed_padding_.lower_bound(next_seq_num_with_padding);
150
151 // While there still are padding packets and those padding packets are
152 // continuous, then advance the "last-picture-id-with-padding" and remove
153 // the stashed padding packet.
154 while (padding_seq_num_it != stashed_padding_.end() &&
155 *padding_seq_num_it == next_seq_num_with_padding) {
156 gop_seq_num_it->second.second = next_seq_num_with_padding;
157 ++next_seq_num_with_padding;
158 padding_seq_num_it = stashed_padding_.erase(padding_seq_num_it);
159 }
philipel41bb7922017-02-20 07:53:23 -0800160
161 // In the case where the stream has been continuous without any new keyframes
162 // for a while there is a risk that new frames will appear to be older than
163 // the keyframe they belong to due to wrapping sequence number. In order
164 // to prevent this we advance the picture id of the keyframe every so often.
165 if (ForwardDiff(gop_seq_num_it->first, seq_num) > 10000) {
166 RTC_DCHECK_EQ(1ul, last_seq_num_gop_.size());
167 last_seq_num_gop_[seq_num] = gop_seq_num_it->second;
168 last_seq_num_gop_.erase(gop_seq_num_it);
169 }
philipel9b2ce6b2016-07-05 05:04:46 -0700170}
171
philipelafcf7f52017-04-26 08:17:35 -0700172RtpFrameReferenceFinder::FrameDecision
173RtpFrameReferenceFinder::ManageFrameGeneric(RtpFrameObject* frame,
174 int picture_id) {
philipel647998c2016-06-03 09:40:16 -0700175 // If |picture_id| is specified then we use that to set the frame references,
176 // otherwise we use sequence number.
177 if (picture_id != kNoPictureId) {
178 if (last_unwrap_ == -1)
179 last_unwrap_ = picture_id;
180
philipel0fa82a62018-03-19 15:34:53 +0100181 frame->id.picture_id = unwrapper_.Unwrap(picture_id);
philipel647998c2016-06-03 09:40:16 -0700182 frame->num_references = frame->frame_type() == kVideoFrameKey ? 0 : 1;
philipel0fa82a62018-03-19 15:34:53 +0100183 frame->references[0] = frame->id.picture_id - 1;
philipelafcf7f52017-04-26 08:17:35 -0700184 return kHandOff;
philipel647998c2016-06-03 09:40:16 -0700185 }
186
philipel9b2ce6b2016-07-05 05:04:46 -0700187 if (frame->frame_type() == kVideoFrameKey) {
188 last_seq_num_gop_.insert(std::make_pair(
189 frame->last_seq_num(),
190 std::make_pair(frame->last_seq_num(), frame->last_seq_num())));
191 }
philipel02447bc2016-05-13 06:01:03 -0700192
193 // We have received a frame but not yet a keyframe, stash this frame.
philipelafcf7f52017-04-26 08:17:35 -0700194 if (last_seq_num_gop_.empty())
195 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700196
197 // Clean up info for old keyframes but make sure to keep info
198 // for the last keyframe.
199 auto clean_to = last_seq_num_gop_.lower_bound(frame->last_seq_num() - 100);
philipel41bb7922017-02-20 07:53:23 -0800200 for (auto it = last_seq_num_gop_.begin();
201 it != clean_to && last_seq_num_gop_.size() > 1;) {
202 it = last_seq_num_gop_.erase(it);
203 }
philipel02447bc2016-05-13 06:01:03 -0700204
205 // Find the last sequence number of the last frame for the keyframe
206 // that this frame indirectly references.
207 auto seq_num_it = last_seq_num_gop_.upper_bound(frame->last_seq_num());
philipel9b2ce6b2016-07-05 05:04:46 -0700208 if (seq_num_it == last_seq_num_gop_.begin()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100209 RTC_LOG(LS_WARNING) << "Generic frame with packet range ["
210 << frame->first_seq_num() << ", "
211 << frame->last_seq_num()
212 << "] has no GoP, dropping frame.";
philipelafcf7f52017-04-26 08:17:35 -0700213 return kDrop;
philipel9b2ce6b2016-07-05 05:04:46 -0700214 }
philipel02447bc2016-05-13 06:01:03 -0700215 seq_num_it--;
216
217 // Make sure the packet sequence numbers are continuous, otherwise stash
218 // this frame.
philipel9b2ce6b2016-07-05 05:04:46 -0700219 uint16_t last_picture_id_gop = seq_num_it->second.first;
220 uint16_t last_picture_id_with_padding_gop = seq_num_it->second.second;
philipel02447bc2016-05-13 06:01:03 -0700221 if (frame->frame_type() == kVideoFrameDelta) {
philipel9b2ce6b2016-07-05 05:04:46 -0700222 uint16_t prev_seq_num = frame->first_seq_num() - 1;
philipelafcf7f52017-04-26 08:17:35 -0700223
224 if (prev_seq_num != last_picture_id_with_padding_gop)
225 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700226 }
227
228 RTC_DCHECK(AheadOrAt(frame->last_seq_num(), seq_num_it->first));
229
230 // Since keyframes can cause reordering we can't simply assign the
231 // picture id according to some incrementing counter.
philipel0fa82a62018-03-19 15:34:53 +0100232 frame->id.picture_id = frame->last_seq_num();
philipel02447bc2016-05-13 06:01:03 -0700233 frame->num_references = frame->frame_type() == kVideoFrameDelta;
philipeld4fac692017-09-04 07:03:46 -0700234 frame->references[0] = generic_unwrapper_.Unwrap(last_picture_id_gop);
philipel0fa82a62018-03-19 15:34:53 +0100235 if (AheadOf<uint16_t>(frame->id.picture_id, last_picture_id_gop)) {
236 seq_num_it->second.first = frame->id.picture_id;
237 seq_num_it->second.second = frame->id.picture_id;
philipel9b2ce6b2016-07-05 05:04:46 -0700238 }
philipel02447bc2016-05-13 06:01:03 -0700239
philipel0fa82a62018-03-19 15:34:53 +0100240 last_picture_id_ = frame->id.picture_id;
241 UpdateLastPictureIdWithPadding(frame->id.picture_id);
242 frame->id.picture_id = generic_unwrapper_.Unwrap(frame->id.picture_id);
philipelafcf7f52017-04-26 08:17:35 -0700243 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700244}
245
philipelafcf7f52017-04-26 08:17:35 -0700246RtpFrameReferenceFinder::FrameDecision RtpFrameReferenceFinder::ManageFrameVp8(
247 RtpFrameObject* frame) {
philipel88488282016-11-03 08:56:54 -0700248 rtc::Optional<RTPVideoTypeHeader> rtp_codec_header = frame->GetCodecHeader();
philipeld4fac692017-09-04 07:03:46 -0700249 if (!rtp_codec_header) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100250 RTC_LOG(LS_WARNING)
251 << "Failed to get codec header from frame, dropping frame.";
philipelafcf7f52017-04-26 08:17:35 -0700252 return kDrop;
philipeld4fac692017-09-04 07:03:46 -0700253 }
philipel02447bc2016-05-13 06:01:03 -0700254
255 const RTPVideoHeaderVP8& codec_header = rtp_codec_header->VP8;
256
257 if (codec_header.pictureId == kNoPictureId ||
258 codec_header.temporalIdx == kNoTemporalIdx ||
259 codec_header.tl0PicIdx == kNoTl0PicIdx) {
philipelafcf7f52017-04-26 08:17:35 -0700260 return ManageFrameGeneric(std::move(frame), codec_header.pictureId);
philipel02447bc2016-05-13 06:01:03 -0700261 }
262
philipel0fa82a62018-03-19 15:34:53 +0100263 frame->id.picture_id = codec_header.pictureId % kPicIdLength;
philipel02447bc2016-05-13 06:01:03 -0700264
265 if (last_unwrap_ == -1)
266 last_unwrap_ = codec_header.pictureId;
267
268 if (last_picture_id_ == -1)
philipel0fa82a62018-03-19 15:34:53 +0100269 last_picture_id_ = frame->id.picture_id;
philipel02447bc2016-05-13 06:01:03 -0700270
271 // Find if there has been a gap in fully received frames and save the picture
272 // id of those frames in |not_yet_received_frames_|.
philipel0fa82a62018-03-19 15:34:53 +0100273 if (AheadOf<uint16_t, kPicIdLength>(frame->id.picture_id, last_picture_id_)) {
philipel9bd1d662017-07-14 04:52:01 -0700274 do {
philipel02447bc2016-05-13 06:01:03 -0700275 last_picture_id_ = Add<kPicIdLength>(last_picture_id_, 1);
philipel9bd1d662017-07-14 04:52:01 -0700276 not_yet_received_frames_.insert(last_picture_id_);
philipel0fa82a62018-03-19 15:34:53 +0100277 } while (last_picture_id_ != frame->id.picture_id);
philipel02447bc2016-05-13 06:01:03 -0700278 }
279
280 // Clean up info for base layers that are too old.
281 uint8_t old_tl0_pic_idx = codec_header.tl0PicIdx - kMaxLayerInfo;
282 auto clean_layer_info_to = layer_info_.lower_bound(old_tl0_pic_idx);
283 layer_info_.erase(layer_info_.begin(), clean_layer_info_to);
284
285 // Clean up info about not yet received frames that are too old.
286 uint16_t old_picture_id =
philipel0fa82a62018-03-19 15:34:53 +0100287 Subtract<kPicIdLength>(frame->id.picture_id, kMaxNotYetReceivedFrames);
philipel02447bc2016-05-13 06:01:03 -0700288 auto clean_frames_to = not_yet_received_frames_.lower_bound(old_picture_id);
289 not_yet_received_frames_.erase(not_yet_received_frames_.begin(),
290 clean_frames_to);
291
292 if (frame->frame_type() == kVideoFrameKey) {
293 frame->num_references = 0;
294 layer_info_[codec_header.tl0PicIdx].fill(-1);
philipel1610f942017-12-12 13:58:31 +0100295 UpdateLayerInfoVp8(frame, codec_header);
philipelafcf7f52017-04-26 08:17:35 -0700296 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700297 }
298
299 auto layer_info_it = layer_info_.find(codec_header.temporalIdx == 0
300 ? codec_header.tl0PicIdx - 1
301 : codec_header.tl0PicIdx);
302
303 // If we don't have the base layer frame yet, stash this frame.
philipelafcf7f52017-04-26 08:17:35 -0700304 if (layer_info_it == layer_info_.end())
305 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700306
307 // A non keyframe base layer frame has been received, copy the layer info
308 // from the previous base layer frame and set a reference to the previous
309 // base layer frame.
310 if (codec_header.temporalIdx == 0) {
311 layer_info_it =
philipel15643602018-05-03 16:14:13 +0200312 layer_info_.emplace(codec_header.tl0PicIdx, layer_info_it->second)
philipel02447bc2016-05-13 06:01:03 -0700313 .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],
philipel0fa82a62018-03-19 15:34:53 +0100341 frame->id.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() &&
philipel0fa82a62018-03-19 15:34:53 +0100350 AheadOf<uint16_t, kPicIdLength>(frame->id.picture_id,
philipel02447bc2016-05-13 06:01:03 -0700351 *not_received_frame_it)) {
philipelafcf7f52017-04-26 08:17:35 -0700352 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700353 }
354
philipel0fa82a62018-03-19 15:34:53 +0100355 if (!(AheadOf<uint16_t, kPicIdLength>(frame->id.picture_id,
philipel57f19cc2017-03-07 03:54:05 -0800356 layer_info_it->second[layer]))) {
philipel0fa82a62018-03-19 15:34:53 +0100357 RTC_LOG(LS_WARNING) << "Frame with picture id " << frame->id.picture_id
Mirko Bonadei675513b2017-11-09 11:09:25 +0100358 << " 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],
philipel0fa82a62018-03-19 15:34:53 +0100384 frame->id.picture_id)) {
philipel02447bc2016-05-13 06:01:03 -0700385 // The frame was not newer, then no subsequent layer info have to be
386 // update.
387 break;
388 }
389
philipel0fa82a62018-03-19 15:34:53 +0100390 layer_info_it->second[codec_header.temporalIdx] = frame->id.picture_id;
philipel02447bc2016-05-13 06:01:03 -0700391 ++tl0_pic_idx;
392 layer_info_it = layer_info_.find(tl0_pic_idx);
393 }
philipel0fa82a62018-03-19 15:34:53 +0100394 not_yet_received_frames_.erase(frame->id.picture_id);
philipel02447bc2016-05-13 06:01:03 -0700395
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
philipel0fa82a62018-03-19 15:34:53 +0100415 frame->id.spatial_layer = codec_header.spatial_idx;
philipel02447bc2016-05-13 06:01:03 -0700416 frame->inter_layer_predicted = codec_header.inter_layer_predicted;
philipel0fa82a62018-03-19 15:34:53 +0100417 frame->id.picture_id = codec_header.picture_id % kPicIdLength;
philipel02447bc2016-05-13 06:01:03 -0700418
419 if (last_unwrap_ == -1)
420 last_unwrap_ = codec_header.picture_id;
421
422 if (last_picture_id_ == -1)
philipel0fa82a62018-03-19 15:34:53 +0100423 last_picture_id_ = frame->id.picture_id;
philipel02447bc2016-05-13 06:01:03 -0700424
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) {
philipel0fa82a62018-03-19 15:34:53 +0100428 frame->references[i] = Subtract<kPicIdLength>(frame->id.picture_id,
429 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
philipel15643602018-05-03 16:14:13 +0200436 GofInfo* info;
philipel02447bc2016-05-13 06:01:03 -0700437 if (codec_header.ss_data_available) {
philipel02447bc2016-05-13 06:01:03 -0700438 if (codec_header.temporal_idx != 0) {
philipel15643602018-05-03 16:14:13 +0200439 RTC_LOG(LS_WARNING) << "Received scalability structure on a non base "
440 "layer frame. Scalability structure ignored.";
philipel02447bc2016-05-13 06:01:03 -0700441 } else {
442 current_ss_idx_ = Add<kMaxGofSaved>(current_ss_idx_, 1);
443 scalability_structures_[current_ss_idx_] = codec_header.gof;
philipel0fa82a62018-03-19 15:34:53 +0100444 scalability_structures_[current_ss_idx_].pid_start = frame->id.picture_id;
philipel15643602018-05-03 16:14:13 +0200445 gof_info_.emplace(codec_header.tl0_pic_idx,
446 GofInfo(&scalability_structures_[current_ss_idx_],
447 frame->id.picture_id));
philipel02447bc2016-05-13 06:01:03 -0700448 }
philipel15643602018-05-03 16:14:13 +0200449
450 const auto gof_info_it = gof_info_.find(codec_header.tl0_pic_idx);
451 if (gof_info_it == gof_info_.end())
452 return kStash;
453
454 info = &gof_info_it->second;
455
456 if (frame->frame_type() == kVideoFrameKey) {
457 frame->num_references = 0;
458 FrameReceivedVp9(frame->id.picture_id, info);
459 UnwrapPictureIds(frame);
460 return kHandOff;
461 }
462 } else {
463 if (frame->frame_type() == kVideoFrameKey) {
464 RTC_LOG(LS_WARNING) << "Received keyframe without scalability structure";
465 return kDrop;
466 }
467
468 auto gof_info_it = gof_info_.find((codec_header.temporal_idx == 0)
469 ? codec_header.tl0_pic_idx - 1
470 : codec_header.tl0_pic_idx);
471
472 // Gof info for this frame is not available yet, stash this frame.
473 if (gof_info_it == gof_info_.end())
474 return kStash;
475
476 if (codec_header.temporal_idx == 0) {
477 gof_info_it =
478 gof_info_
479 .emplace(codec_header.tl0_pic_idx,
480 GofInfo(gof_info_it->second.gof, frame->id.picture_id))
481 .first;
482 }
483
484 info = &gof_info_it->second;
philipel02447bc2016-05-13 06:01:03 -0700485 }
486
487 // Clean up info for base layers that are too old.
488 uint8_t old_tl0_pic_idx = codec_header.tl0_pic_idx - kMaxGofSaved;
489 auto clean_gof_info_to = gof_info_.lower_bound(old_tl0_pic_idx);
490 gof_info_.erase(gof_info_.begin(), clean_gof_info_to);
491
philipel0fa82a62018-03-19 15:34:53 +0100492 FrameReceivedVp9(frame->id.picture_id, info);
philipel02447bc2016-05-13 06:01:03 -0700493
494 // Make sure we don't miss any frame that could potentially have the
495 // up switch flag set.
philipel0fa82a62018-03-19 15:34:53 +0100496 if (MissingRequiredFrameVp9(frame->id.picture_id, *info))
philipelafcf7f52017-04-26 08:17:35 -0700497 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700498
philipel15643602018-05-03 16:14:13 +0200499 if (codec_header.temporal_up_switch)
500 up_switch_.emplace(frame->id.picture_id, codec_header.temporal_idx);
philipel02447bc2016-05-13 06:01:03 -0700501
502 // Clean out old info about up switch frames.
philipel0fa82a62018-03-19 15:34:53 +0100503 uint16_t old_picture_id = Subtract<kPicIdLength>(frame->id.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,
philipel0fa82a62018-03-19 15:34:53 +0100508 frame->id.picture_id);
philipelc9b27d52016-07-15 06:50:27 -0700509 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>(
philipel0fa82a62018-03-19 15:34:53 +0100515 frame->id.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.
philipel0fa82a62018-03-19 15:34:53 +0100519 if (UpSwitchInIntervalVp9(frame->id.picture_id, codec_header.temporal_idx,
philipel02447bc2016-05-13 06:01:03 -0700520 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
philipela157e082018-05-02 15:19:01 +0200536 if (temporal_idx >= kMaxTemporalLayers) {
537 RTC_LOG(LS_WARNING) << "At most " << kMaxTemporalLayers << " temporal "
538 << "layers are supported.";
539 return true;
540 }
541
philipel02447bc2016-05-13 06:01:03 -0700542 // For every reference this frame has, check if there is a frame missing in
543 // the interval (|ref_pid|, |picture_id|) in any of the lower temporal
544 // layers. If so, we are missing a required frame.
philipelc9b27d52016-07-15 06:50:27 -0700545 uint8_t num_references = info.gof->num_ref_pics[gof_idx];
philipel02447bc2016-05-13 06:01:03 -0700546 for (size_t i = 0; i < num_references; ++i) {
547 uint16_t ref_pid =
philipelc9b27d52016-07-15 06:50:27 -0700548 Subtract<kPicIdLength>(picture_id, info.gof->pid_diff[gof_idx][i]);
philipel02447bc2016-05-13 06:01:03 -0700549 for (size_t l = 0; l < temporal_idx; ++l) {
550 auto missing_frame_it = missing_frames_for_layer_[l].lower_bound(ref_pid);
551 if (missing_frame_it != missing_frames_for_layer_[l].end() &&
552 AheadOf<uint16_t, kPicIdLength>(picture_id, *missing_frame_it)) {
553 return true;
554 }
555 }
556 }
557 return false;
558}
559
560void RtpFrameReferenceFinder::FrameReceivedVp9(uint16_t picture_id,
philipelc9b27d52016-07-15 06:50:27 -0700561 GofInfo* info) {
562 int last_picture_id = info->last_picture_id;
philipel459f4e32018-03-02 10:55:12 +0100563 size_t gof_size = std::min(info->gof->num_frames_in_gof, kMaxVp9FramesInGof);
philipel02447bc2016-05-13 06:01:03 -0700564
565 // If there is a gap, find which temporal layer the missing frames
566 // belong to and add the frame as missing for that temporal layer.
567 // Otherwise, remove this frame from the set of missing frames.
philipelc9b27d52016-07-15 06:50:27 -0700568 if (AheadOf<uint16_t, kPicIdLength>(picture_id, last_picture_id)) {
569 size_t diff = ForwardDiff<uint16_t, kPicIdLength>(info->gof->pid_start,
570 last_picture_id);
philipel459f4e32018-03-02 10:55:12 +0100571 size_t gof_idx = diff % gof_size;
philipel02447bc2016-05-13 06:01:03 -0700572
philipelc9b27d52016-07-15 06:50:27 -0700573 last_picture_id = Add<kPicIdLength>(last_picture_id, 1);
574 while (last_picture_id != picture_id) {
philipel459f4e32018-03-02 10:55:12 +0100575 gof_idx = (gof_idx + 1) % gof_size;
576 RTC_CHECK(gof_idx < kMaxVp9FramesInGof);
577
philipelc9b27d52016-07-15 06:50:27 -0700578 size_t temporal_idx = info->gof->temporal_idx[gof_idx];
philipel459f4e32018-03-02 10:55:12 +0100579 if (temporal_idx >= kMaxTemporalLayers) {
580 RTC_LOG(LS_WARNING) << "At most " << kMaxTemporalLayers << " temporal "
581 << "layers are supported.";
582 return;
583 }
584
philipelc9b27d52016-07-15 06:50:27 -0700585 missing_frames_for_layer_[temporal_idx].insert(last_picture_id);
586 last_picture_id = Add<kPicIdLength>(last_picture_id, 1);
philipel02447bc2016-05-13 06:01:03 -0700587 }
philipel459f4e32018-03-02 10:55:12 +0100588
philipelc9b27d52016-07-15 06:50:27 -0700589 info->last_picture_id = last_picture_id;
philipel02447bc2016-05-13 06:01:03 -0700590 } else {
591 size_t diff =
philipelc9b27d52016-07-15 06:50:27 -0700592 ForwardDiff<uint16_t, kPicIdLength>(info->gof->pid_start, picture_id);
philipel459f4e32018-03-02 10:55:12 +0100593 size_t gof_idx = diff % gof_size;
594 RTC_CHECK(gof_idx < kMaxVp9FramesInGof);
595
philipelc9b27d52016-07-15 06:50:27 -0700596 size_t temporal_idx = info->gof->temporal_idx[gof_idx];
philipel459f4e32018-03-02 10:55:12 +0100597 if (temporal_idx >= kMaxTemporalLayers) {
598 RTC_LOG(LS_WARNING) << "At most " << kMaxTemporalLayers << " temporal "
599 << "layers are supported.";
600 return;
601 }
602
philipel02447bc2016-05-13 06:01:03 -0700603 missing_frames_for_layer_[temporal_idx].erase(picture_id);
604 }
605}
606
607bool RtpFrameReferenceFinder::UpSwitchInIntervalVp9(uint16_t picture_id,
608 uint8_t temporal_idx,
609 uint16_t pid_ref) {
610 for (auto up_switch_it = up_switch_.upper_bound(pid_ref);
611 up_switch_it != up_switch_.end() &&
612 AheadOf<uint16_t, kPicIdLength>(picture_id, up_switch_it->first);
613 ++up_switch_it) {
614 if (up_switch_it->second < temporal_idx)
615 return true;
616 }
617
618 return false;
619}
620
philipelafcf7f52017-04-26 08:17:35 -0700621void RtpFrameReferenceFinder::UnwrapPictureIds(RtpFrameObject* frame) {
philipel02447bc2016-05-13 06:01:03 -0700622 for (size_t i = 0; i < frame->num_references; ++i)
philipeld4fac692017-09-04 07:03:46 -0700623 frame->references[i] = unwrapper_.Unwrap(frame->references[i]);
philipel0fa82a62018-03-19 15:34:53 +0100624 frame->id.picture_id = unwrapper_.Unwrap(frame->id.picture_id);
philipel02447bc2016-05-13 06:01:03 -0700625}
626
philipel02447bc2016-05-13 06:01:03 -0700627} // namespace video_coding
628} // namespace webrtc