blob: 217ec047d9c731a6afdd37682a14a4521f57698a [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
philipel1a4746a2018-07-09 15:52:29 +020016#include "absl/types/variant.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "modules/video_coding/frame_object.h"
18#include "modules/video_coding/packet_buffer.h"
19#include "rtc_base/checks.h"
20#include "rtc_base/logging.h"
Karl Wiberg80ba3332018-02-05 10:33:35 +010021#include "rtc_base/system/fallthrough.h"
philipel02447bc2016-05-13 06:01:03 -070022
23namespace webrtc {
24namespace video_coding {
25
26RtpFrameReferenceFinder::RtpFrameReferenceFinder(
27 OnCompleteFrameCallback* frame_callback)
28 : last_picture_id_(-1),
29 last_unwrap_(-1),
30 current_ss_idx_(0),
philipel463d3012016-09-09 03:32:44 -070031 cleared_to_seq_num_(-1),
philipel02447bc2016-05-13 06:01:03 -070032 frame_callback_(frame_callback) {}
33
34void RtpFrameReferenceFinder::ManageFrame(
35 std::unique_ptr<RtpFrameObject> frame) {
36 rtc::CritScope lock(&crit_);
philipel463d3012016-09-09 03:32:44 -070037
38 // If we have cleared past this frame, drop it.
39 if (cleared_to_seq_num_ != -1 &&
40 AheadOf<uint16_t>(cleared_to_seq_num_, frame->first_seq_num())) {
41 return;
42 }
43
philipelafcf7f52017-04-26 08:17:35 -070044 FrameDecision decision = ManageFrameInternal(frame.get());
45
46 switch (decision) {
47 case kStash:
48 if (stashed_frames_.size() > kMaxStashedFrames)
49 stashed_frames_.pop_back();
50 stashed_frames_.push_front(std::move(frame));
51 break;
52 case kHandOff:
53 frame_callback_->OnCompleteFrame(std::move(frame));
54 RetryStashedFrames();
55 break;
56 case kDrop:
57 break;
58 }
59}
60
61void RtpFrameReferenceFinder::RetryStashedFrames() {
62 bool complete_frame = false;
63 do {
64 complete_frame = false;
65 for (auto frame_it = stashed_frames_.begin();
66 frame_it != stashed_frames_.end();) {
67 FrameDecision decision = ManageFrameInternal(frame_it->get());
68
69 switch (decision) {
70 case kStash:
71 ++frame_it;
72 break;
73 case kHandOff:
74 complete_frame = true;
75 frame_callback_->OnCompleteFrame(std::move(*frame_it));
Karl Wiberg80ba3332018-02-05 10:33:35 +010076 RTC_FALLTHROUGH();
philipelafcf7f52017-04-26 08:17:35 -070077 case kDrop:
78 frame_it = stashed_frames_.erase(frame_it);
79 }
80 }
81 } while (complete_frame);
82}
83
84RtpFrameReferenceFinder::FrameDecision
85RtpFrameReferenceFinder::ManageFrameInternal(RtpFrameObject* frame) {
philipel02447bc2016-05-13 06:01:03 -070086 switch (frame->codec_type()) {
philipel02447bc2016-05-13 06:01:03 -070087 case kVideoCodecVP8:
philipelafcf7f52017-04-26 08:17:35 -070088 return ManageFrameVp8(frame);
philipel02447bc2016-05-13 06:01:03 -070089 case kVideoCodecVP9:
philipelafcf7f52017-04-26 08:17:35 -070090 return ManageFrameVp9(frame);
Kári Tristan Helgason84ccb2d2018-08-16 14:35:26 +020091 default:
philipelafcf7f52017-04-26 08:17:35 -070092 return ManageFrameGeneric(frame, kNoPictureId);
philipel02447bc2016-05-13 06:01:03 -070093 }
94}
95
philipel9b2ce6b2016-07-05 05:04:46 -070096void RtpFrameReferenceFinder::PaddingReceived(uint16_t seq_num) {
97 rtc::CritScope lock(&crit_);
98 auto clean_padding_to =
99 stashed_padding_.lower_bound(seq_num - kMaxPaddingAge);
100 stashed_padding_.erase(stashed_padding_.begin(), clean_padding_to);
101 stashed_padding_.insert(seq_num);
102 UpdateLastPictureIdWithPadding(seq_num);
103 RetryStashedFrames();
104}
105
philipel463d3012016-09-09 03:32:44 -0700106void RtpFrameReferenceFinder::ClearTo(uint16_t seq_num) {
107 rtc::CritScope lock(&crit_);
108 cleared_to_seq_num_ = seq_num;
109
110 auto it = stashed_frames_.begin();
111 while (it != stashed_frames_.end()) {
112 if (AheadOf<uint16_t>(cleared_to_seq_num_, (*it)->first_seq_num())) {
113 it = stashed_frames_.erase(it);
114 } else {
115 ++it;
116 }
117 }
118}
119
philipel9b2ce6b2016-07-05 05:04:46 -0700120void RtpFrameReferenceFinder::UpdateLastPictureIdWithPadding(uint16_t seq_num) {
121 auto gop_seq_num_it = last_seq_num_gop_.upper_bound(seq_num);
122
123 // If this padding packet "belongs" to a group of pictures that we don't track
124 // anymore, do nothing.
125 if (gop_seq_num_it == last_seq_num_gop_.begin())
126 return;
127 --gop_seq_num_it;
128
129 // Calculate the next contiuous sequence number and search for it in
130 // the padding packets we have stashed.
131 uint16_t next_seq_num_with_padding = gop_seq_num_it->second.second + 1;
132 auto padding_seq_num_it =
133 stashed_padding_.lower_bound(next_seq_num_with_padding);
134
135 // While there still are padding packets and those padding packets are
136 // continuous, then advance the "last-picture-id-with-padding" and remove
137 // the stashed padding packet.
138 while (padding_seq_num_it != stashed_padding_.end() &&
139 *padding_seq_num_it == next_seq_num_with_padding) {
140 gop_seq_num_it->second.second = next_seq_num_with_padding;
141 ++next_seq_num_with_padding;
142 padding_seq_num_it = stashed_padding_.erase(padding_seq_num_it);
143 }
philipel41bb7922017-02-20 07:53:23 -0800144
145 // In the case where the stream has been continuous without any new keyframes
146 // for a while there is a risk that new frames will appear to be older than
147 // the keyframe they belong to due to wrapping sequence number. In order
148 // to prevent this we advance the picture id of the keyframe every so often.
149 if (ForwardDiff(gop_seq_num_it->first, seq_num) > 10000) {
150 RTC_DCHECK_EQ(1ul, last_seq_num_gop_.size());
151 last_seq_num_gop_[seq_num] = gop_seq_num_it->second;
152 last_seq_num_gop_.erase(gop_seq_num_it);
153 }
philipel9b2ce6b2016-07-05 05:04:46 -0700154}
155
philipelafcf7f52017-04-26 08:17:35 -0700156RtpFrameReferenceFinder::FrameDecision
157RtpFrameReferenceFinder::ManageFrameGeneric(RtpFrameObject* frame,
158 int picture_id) {
philipel647998c2016-06-03 09:40:16 -0700159 // If |picture_id| is specified then we use that to set the frame references,
160 // otherwise we use sequence number.
161 if (picture_id != kNoPictureId) {
162 if (last_unwrap_ == -1)
163 last_unwrap_ = picture_id;
164
philipel0fa82a62018-03-19 15:34:53 +0100165 frame->id.picture_id = unwrapper_.Unwrap(picture_id);
philipel647998c2016-06-03 09:40:16 -0700166 frame->num_references = frame->frame_type() == kVideoFrameKey ? 0 : 1;
philipel0fa82a62018-03-19 15:34:53 +0100167 frame->references[0] = frame->id.picture_id - 1;
philipelafcf7f52017-04-26 08:17:35 -0700168 return kHandOff;
philipel647998c2016-06-03 09:40:16 -0700169 }
170
philipel9b2ce6b2016-07-05 05:04:46 -0700171 if (frame->frame_type() == kVideoFrameKey) {
172 last_seq_num_gop_.insert(std::make_pair(
173 frame->last_seq_num(),
174 std::make_pair(frame->last_seq_num(), frame->last_seq_num())));
175 }
philipel02447bc2016-05-13 06:01:03 -0700176
177 // We have received a frame but not yet a keyframe, stash this frame.
philipelafcf7f52017-04-26 08:17:35 -0700178 if (last_seq_num_gop_.empty())
179 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700180
181 // Clean up info for old keyframes but make sure to keep info
182 // for the last keyframe.
183 auto clean_to = last_seq_num_gop_.lower_bound(frame->last_seq_num() - 100);
philipel41bb7922017-02-20 07:53:23 -0800184 for (auto it = last_seq_num_gop_.begin();
185 it != clean_to && last_seq_num_gop_.size() > 1;) {
186 it = last_seq_num_gop_.erase(it);
187 }
philipel02447bc2016-05-13 06:01:03 -0700188
189 // Find the last sequence number of the last frame for the keyframe
190 // that this frame indirectly references.
191 auto seq_num_it = last_seq_num_gop_.upper_bound(frame->last_seq_num());
philipel9b2ce6b2016-07-05 05:04:46 -0700192 if (seq_num_it == last_seq_num_gop_.begin()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100193 RTC_LOG(LS_WARNING) << "Generic frame with packet range ["
194 << frame->first_seq_num() << ", "
195 << frame->last_seq_num()
196 << "] has no GoP, dropping frame.";
philipelafcf7f52017-04-26 08:17:35 -0700197 return kDrop;
philipel9b2ce6b2016-07-05 05:04:46 -0700198 }
philipel02447bc2016-05-13 06:01:03 -0700199 seq_num_it--;
200
201 // Make sure the packet sequence numbers are continuous, otherwise stash
202 // this frame.
philipel9b2ce6b2016-07-05 05:04:46 -0700203 uint16_t last_picture_id_gop = seq_num_it->second.first;
204 uint16_t last_picture_id_with_padding_gop = seq_num_it->second.second;
philipel02447bc2016-05-13 06:01:03 -0700205 if (frame->frame_type() == kVideoFrameDelta) {
philipel9b2ce6b2016-07-05 05:04:46 -0700206 uint16_t prev_seq_num = frame->first_seq_num() - 1;
philipelafcf7f52017-04-26 08:17:35 -0700207
208 if (prev_seq_num != last_picture_id_with_padding_gop)
209 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700210 }
211
212 RTC_DCHECK(AheadOrAt(frame->last_seq_num(), seq_num_it->first));
213
214 // Since keyframes can cause reordering we can't simply assign the
215 // picture id according to some incrementing counter.
philipel0fa82a62018-03-19 15:34:53 +0100216 frame->id.picture_id = frame->last_seq_num();
philipel02447bc2016-05-13 06:01:03 -0700217 frame->num_references = frame->frame_type() == kVideoFrameDelta;
philipeld4fac692017-09-04 07:03:46 -0700218 frame->references[0] = generic_unwrapper_.Unwrap(last_picture_id_gop);
philipel0fa82a62018-03-19 15:34:53 +0100219 if (AheadOf<uint16_t>(frame->id.picture_id, last_picture_id_gop)) {
220 seq_num_it->second.first = frame->id.picture_id;
221 seq_num_it->second.second = frame->id.picture_id;
philipel9b2ce6b2016-07-05 05:04:46 -0700222 }
philipel02447bc2016-05-13 06:01:03 -0700223
philipel0fa82a62018-03-19 15:34:53 +0100224 last_picture_id_ = frame->id.picture_id;
225 UpdateLastPictureIdWithPadding(frame->id.picture_id);
226 frame->id.picture_id = generic_unwrapper_.Unwrap(frame->id.picture_id);
philipelafcf7f52017-04-26 08:17:35 -0700227 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700228}
229
philipelafcf7f52017-04-26 08:17:35 -0700230RtpFrameReferenceFinder::FrameDecision RtpFrameReferenceFinder::ManageFrameVp8(
231 RtpFrameObject* frame) {
Danil Chapovalov0040b662018-06-18 10:48:16 +0200232 absl::optional<RTPVideoTypeHeader> rtp_codec_header = frame->GetCodecHeader();
philipeld4fac692017-09-04 07:03:46 -0700233 if (!rtp_codec_header) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100234 RTC_LOG(LS_WARNING)
235 << "Failed to get codec header from frame, dropping frame.";
philipelafcf7f52017-04-26 08:17:35 -0700236 return kDrop;
philipeld4fac692017-09-04 07:03:46 -0700237 }
philipel02447bc2016-05-13 06:01:03 -0700238
philipel1a4746a2018-07-09 15:52:29 +0200239 const RTPVideoHeaderVP8& codec_header =
240 absl::get<RTPVideoHeaderVP8>(*rtp_codec_header);
philipel02447bc2016-05-13 06:01:03 -0700241
242 if (codec_header.pictureId == kNoPictureId ||
243 codec_header.temporalIdx == kNoTemporalIdx ||
244 codec_header.tl0PicIdx == kNoTl0PicIdx) {
philipelafcf7f52017-04-26 08:17:35 -0700245 return ManageFrameGeneric(std::move(frame), codec_header.pictureId);
philipel02447bc2016-05-13 06:01:03 -0700246 }
247
philipel0fa82a62018-03-19 15:34:53 +0100248 frame->id.picture_id = codec_header.pictureId % kPicIdLength;
philipel02447bc2016-05-13 06:01:03 -0700249
250 if (last_unwrap_ == -1)
251 last_unwrap_ = codec_header.pictureId;
252
253 if (last_picture_id_ == -1)
philipel0fa82a62018-03-19 15:34:53 +0100254 last_picture_id_ = frame->id.picture_id;
philipel02447bc2016-05-13 06:01:03 -0700255
256 // Find if there has been a gap in fully received frames and save the picture
257 // id of those frames in |not_yet_received_frames_|.
philipel0fa82a62018-03-19 15:34:53 +0100258 if (AheadOf<uint16_t, kPicIdLength>(frame->id.picture_id, last_picture_id_)) {
philipel9bd1d662017-07-14 04:52:01 -0700259 do {
philipel02447bc2016-05-13 06:01:03 -0700260 last_picture_id_ = Add<kPicIdLength>(last_picture_id_, 1);
philipel9bd1d662017-07-14 04:52:01 -0700261 not_yet_received_frames_.insert(last_picture_id_);
philipel0fa82a62018-03-19 15:34:53 +0100262 } while (last_picture_id_ != frame->id.picture_id);
philipel02447bc2016-05-13 06:01:03 -0700263 }
264
philipel57ec6852018-07-03 18:09:32 +0200265 int64_t unwrapped_tl0 = tl0_unwrapper_.Unwrap(codec_header.tl0PicIdx);
266
philipel02447bc2016-05-13 06:01:03 -0700267 // Clean up info for base layers that are too old.
philipel57ec6852018-07-03 18:09:32 +0200268 int64_t old_tl0_pic_idx = unwrapped_tl0 - kMaxLayerInfo;
philipel02447bc2016-05-13 06:01:03 -0700269 auto clean_layer_info_to = layer_info_.lower_bound(old_tl0_pic_idx);
270 layer_info_.erase(layer_info_.begin(), clean_layer_info_to);
271
272 // Clean up info about not yet received frames that are too old.
273 uint16_t old_picture_id =
philipel0fa82a62018-03-19 15:34:53 +0100274 Subtract<kPicIdLength>(frame->id.picture_id, kMaxNotYetReceivedFrames);
philipel02447bc2016-05-13 06:01:03 -0700275 auto clean_frames_to = not_yet_received_frames_.lower_bound(old_picture_id);
276 not_yet_received_frames_.erase(not_yet_received_frames_.begin(),
277 clean_frames_to);
278
279 if (frame->frame_type() == kVideoFrameKey) {
280 frame->num_references = 0;
philipel57ec6852018-07-03 18:09:32 +0200281 layer_info_[unwrapped_tl0].fill(-1);
282 UpdateLayerInfoVp8(frame, unwrapped_tl0, codec_header.temporalIdx);
philipelafcf7f52017-04-26 08:17:35 -0700283 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700284 }
285
philipel57ec6852018-07-03 18:09:32 +0200286 auto layer_info_it = layer_info_.find(
287 codec_header.temporalIdx == 0 ? unwrapped_tl0 - 1 : unwrapped_tl0);
philipel02447bc2016-05-13 06:01:03 -0700288
289 // If we don't have the base layer frame yet, stash this frame.
philipelafcf7f52017-04-26 08:17:35 -0700290 if (layer_info_it == layer_info_.end())
291 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700292
293 // A non keyframe base layer frame has been received, copy the layer info
294 // from the previous base layer frame and set a reference to the previous
295 // base layer frame.
296 if (codec_header.temporalIdx == 0) {
297 layer_info_it =
philipel57ec6852018-07-03 18:09:32 +0200298 layer_info_.emplace(unwrapped_tl0, layer_info_it->second).first;
philipel02447bc2016-05-13 06:01:03 -0700299 frame->num_references = 1;
300 frame->references[0] = layer_info_it->second[0];
philipel57ec6852018-07-03 18:09:32 +0200301 UpdateLayerInfoVp8(frame, unwrapped_tl0, codec_header.temporalIdx);
philipelafcf7f52017-04-26 08:17:35 -0700302 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700303 }
304
305 // Layer sync frame, this frame only references its base layer frame.
306 if (codec_header.layerSync) {
307 frame->num_references = 1;
308 frame->references[0] = layer_info_it->second[0];
309
philipel57ec6852018-07-03 18:09:32 +0200310 UpdateLayerInfoVp8(frame, unwrapped_tl0, codec_header.temporalIdx);
philipelafcf7f52017-04-26 08:17:35 -0700311 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700312 }
313
314 // Find all references for this frame.
315 frame->num_references = 0;
316 for (uint8_t layer = 0; layer <= codec_header.temporalIdx; ++layer) {
philipeld268d6f2016-09-15 13:43:13 +0200317 // If we have not yet received a previous frame on this temporal layer,
318 // stash this frame.
philipelafcf7f52017-04-26 08:17:35 -0700319 if (layer_info_it->second[layer] == -1)
320 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700321
philipel86b92e02016-10-24 07:11:53 -0700322 // If the last frame on this layer is ahead of this frame it means that
323 // a layer sync frame has been received after this frame for the same
324 // base layer frame, drop this frame.
325 if (AheadOf<uint16_t, kPicIdLength>(layer_info_it->second[layer],
philipel0fa82a62018-03-19 15:34:53 +0100326 frame->id.picture_id)) {
philipelafcf7f52017-04-26 08:17:35 -0700327 return kDrop;
philipel86b92e02016-10-24 07:11:53 -0700328 }
329
philipel02447bc2016-05-13 06:01:03 -0700330 // If we have not yet received a frame between this frame and the referenced
331 // frame then we have to wait for that frame to be completed first.
332 auto not_received_frame_it =
333 not_yet_received_frames_.upper_bound(layer_info_it->second[layer]);
334 if (not_received_frame_it != not_yet_received_frames_.end() &&
philipel0fa82a62018-03-19 15:34:53 +0100335 AheadOf<uint16_t, kPicIdLength>(frame->id.picture_id,
philipel02447bc2016-05-13 06:01:03 -0700336 *not_received_frame_it)) {
philipelafcf7f52017-04-26 08:17:35 -0700337 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700338 }
339
philipel0fa82a62018-03-19 15:34:53 +0100340 if (!(AheadOf<uint16_t, kPicIdLength>(frame->id.picture_id,
philipel57f19cc2017-03-07 03:54:05 -0800341 layer_info_it->second[layer]))) {
philipel0fa82a62018-03-19 15:34:53 +0100342 RTC_LOG(LS_WARNING) << "Frame with picture id " << frame->id.picture_id
Mirko Bonadei675513b2017-11-09 11:09:25 +0100343 << " and packet range [" << frame->first_seq_num()
344 << ", " << frame->last_seq_num()
345 << "] already received, "
346 << " dropping frame.";
philipelafcf7f52017-04-26 08:17:35 -0700347 return kDrop;
philipel57f19cc2017-03-07 03:54:05 -0800348 }
349
philipel02447bc2016-05-13 06:01:03 -0700350 ++frame->num_references;
351 frame->references[layer] = layer_info_it->second[layer];
352 }
353
philipel57ec6852018-07-03 18:09:32 +0200354 UpdateLayerInfoVp8(frame, unwrapped_tl0, codec_header.temporalIdx);
philipelafcf7f52017-04-26 08:17:35 -0700355 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700356}
357
philipel57ec6852018-07-03 18:09:32 +0200358void RtpFrameReferenceFinder::UpdateLayerInfoVp8(RtpFrameObject* frame,
359 int64_t unwrapped_tl0,
360 uint8_t temporal_idx) {
361 auto layer_info_it = layer_info_.find(unwrapped_tl0);
philipel02447bc2016-05-13 06:01:03 -0700362
363 // Update this layer info and newer.
364 while (layer_info_it != layer_info_.end()) {
philipel57ec6852018-07-03 18:09:32 +0200365 if (layer_info_it->second[temporal_idx] != -1 &&
366 AheadOf<uint16_t, kPicIdLength>(layer_info_it->second[temporal_idx],
philipel0fa82a62018-03-19 15:34:53 +0100367 frame->id.picture_id)) {
philipel02447bc2016-05-13 06:01:03 -0700368 // The frame was not newer, then no subsequent layer info have to be
369 // update.
370 break;
371 }
372
philipel57ec6852018-07-03 18:09:32 +0200373 layer_info_it->second[temporal_idx] = frame->id.picture_id;
374 ++unwrapped_tl0;
375 layer_info_it = layer_info_.find(unwrapped_tl0);
philipel02447bc2016-05-13 06:01:03 -0700376 }
philipel0fa82a62018-03-19 15:34:53 +0100377 not_yet_received_frames_.erase(frame->id.picture_id);
philipel02447bc2016-05-13 06:01:03 -0700378
philipelafcf7f52017-04-26 08:17:35 -0700379 UnwrapPictureIds(frame);
philipel02447bc2016-05-13 06:01:03 -0700380}
381
philipelafcf7f52017-04-26 08:17:35 -0700382RtpFrameReferenceFinder::FrameDecision RtpFrameReferenceFinder::ManageFrameVp9(
383 RtpFrameObject* frame) {
Danil Chapovalov0040b662018-06-18 10:48:16 +0200384 absl::optional<RTPVideoTypeHeader> rtp_codec_header = frame->GetCodecHeader();
philipeld4fac692017-09-04 07:03:46 -0700385 if (!rtp_codec_header) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100386 RTC_LOG(LS_WARNING)
387 << "Failed to get codec header from frame, dropping frame.";
philipel4c140092017-08-31 08:31:45 -0700388 return kDrop;
philipeld4fac692017-09-04 07:03:46 -0700389 }
philipel4c140092017-08-31 08:31:45 -0700390
philipel1a4746a2018-07-09 15:52:29 +0200391 const RTPVideoHeaderVP9& codec_header =
392 absl::get<RTPVideoHeaderVP9>(*rtp_codec_header);
philipel02447bc2016-05-13 06:01:03 -0700393
philipel647998c2016-06-03 09:40:16 -0700394 if (codec_header.picture_id == kNoPictureId ||
philipel57ec6852018-07-03 18:09:32 +0200395 codec_header.temporal_idx == kNoTemporalIdx ||
396 codec_header.tl0_pic_idx == kNoTl0PicIdx) {
philipelafcf7f52017-04-26 08:17:35 -0700397 return ManageFrameGeneric(std::move(frame), codec_header.picture_id);
philipel02447bc2016-05-13 06:01:03 -0700398 }
399
philipel0fa82a62018-03-19 15:34:53 +0100400 frame->id.spatial_layer = codec_header.spatial_idx;
philipel02447bc2016-05-13 06:01:03 -0700401 frame->inter_layer_predicted = codec_header.inter_layer_predicted;
philipel0fa82a62018-03-19 15:34:53 +0100402 frame->id.picture_id = codec_header.picture_id % kPicIdLength;
philipel02447bc2016-05-13 06:01:03 -0700403
404 if (last_unwrap_ == -1)
405 last_unwrap_ = codec_header.picture_id;
406
407 if (last_picture_id_ == -1)
philipel0fa82a62018-03-19 15:34:53 +0100408 last_picture_id_ = frame->id.picture_id;
philipel02447bc2016-05-13 06:01:03 -0700409
410 if (codec_header.flexible_mode) {
411 frame->num_references = codec_header.num_ref_pics;
412 for (size_t i = 0; i < frame->num_references; ++i) {
philipel0fa82a62018-03-19 15:34:53 +0100413 frame->references[i] = Subtract<kPicIdLength>(frame->id.picture_id,
414 codec_header.pid_diff[i]);
philipel02447bc2016-05-13 06:01:03 -0700415 }
416
philipelafcf7f52017-04-26 08:17:35 -0700417 UnwrapPictureIds(frame);
418 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700419 }
420
philipel15643602018-05-03 16:14:13 +0200421 GofInfo* info;
philipel57ec6852018-07-03 18:09:32 +0200422 int64_t unwrapped_tl0 = tl0_unwrapper_.Unwrap(codec_header.tl0_pic_idx);
philipel02447bc2016-05-13 06:01:03 -0700423 if (codec_header.ss_data_available) {
philipel02447bc2016-05-13 06:01:03 -0700424 if (codec_header.temporal_idx != 0) {
philipel15643602018-05-03 16:14:13 +0200425 RTC_LOG(LS_WARNING) << "Received scalability structure on a non base "
426 "layer frame. Scalability structure ignored.";
philipel02447bc2016-05-13 06:01:03 -0700427 } else {
428 current_ss_idx_ = Add<kMaxGofSaved>(current_ss_idx_, 1);
Natalie Silvanovich3ea3e302018-05-16 11:03:12 -0700429 if (codec_header.gof.num_frames_in_gof == 0 ||
430 codec_header.gof.num_frames_in_gof > kMaxVp9FramesInGof) {
431 return kDrop;
432 }
433
philipel02447bc2016-05-13 06:01:03 -0700434 scalability_structures_[current_ss_idx_] = codec_header.gof;
philipel0fa82a62018-03-19 15:34:53 +0100435 scalability_structures_[current_ss_idx_].pid_start = frame->id.picture_id;
philipel57ec6852018-07-03 18:09:32 +0200436 gof_info_.emplace(unwrapped_tl0,
philipel15643602018-05-03 16:14:13 +0200437 GofInfo(&scalability_structures_[current_ss_idx_],
438 frame->id.picture_id));
philipel02447bc2016-05-13 06:01:03 -0700439 }
philipel15643602018-05-03 16:14:13 +0200440
philipel57ec6852018-07-03 18:09:32 +0200441 const auto gof_info_it = gof_info_.find(unwrapped_tl0);
philipel15643602018-05-03 16:14:13 +0200442 if (gof_info_it == gof_info_.end())
443 return kStash;
444
445 info = &gof_info_it->second;
446
447 if (frame->frame_type() == kVideoFrameKey) {
448 frame->num_references = 0;
449 FrameReceivedVp9(frame->id.picture_id, info);
450 UnwrapPictureIds(frame);
451 return kHandOff;
452 }
453 } else {
454 if (frame->frame_type() == kVideoFrameKey) {
455 RTC_LOG(LS_WARNING) << "Received keyframe without scalability structure";
456 return kDrop;
457 }
458
philipel57ec6852018-07-03 18:09:32 +0200459 auto gof_info_it = gof_info_.find(
460 (codec_header.temporal_idx == 0) ? unwrapped_tl0 - 1 : unwrapped_tl0);
philipel15643602018-05-03 16:14:13 +0200461
462 // Gof info for this frame is not available yet, stash this frame.
463 if (gof_info_it == gof_info_.end())
464 return kStash;
465
466 if (codec_header.temporal_idx == 0) {
philipel57ec6852018-07-03 18:09:32 +0200467 gof_info_it = gof_info_
468 .emplace(unwrapped_tl0, GofInfo(gof_info_it->second.gof,
469 frame->id.picture_id))
470 .first;
philipel15643602018-05-03 16:14:13 +0200471 }
472
473 info = &gof_info_it->second;
philipel02447bc2016-05-13 06:01:03 -0700474 }
475
476 // Clean up info for base layers that are too old.
philipel57ec6852018-07-03 18:09:32 +0200477 int64_t old_tl0_pic_idx = unwrapped_tl0 - kMaxGofSaved;
philipel02447bc2016-05-13 06:01:03 -0700478 auto clean_gof_info_to = gof_info_.lower_bound(old_tl0_pic_idx);
479 gof_info_.erase(gof_info_.begin(), clean_gof_info_to);
480
philipel0fa82a62018-03-19 15:34:53 +0100481 FrameReceivedVp9(frame->id.picture_id, info);
philipel02447bc2016-05-13 06:01:03 -0700482
483 // Make sure we don't miss any frame that could potentially have the
484 // up switch flag set.
philipel0fa82a62018-03-19 15:34:53 +0100485 if (MissingRequiredFrameVp9(frame->id.picture_id, *info))
philipelafcf7f52017-04-26 08:17:35 -0700486 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700487
philipel15643602018-05-03 16:14:13 +0200488 if (codec_header.temporal_up_switch)
489 up_switch_.emplace(frame->id.picture_id, codec_header.temporal_idx);
philipel02447bc2016-05-13 06:01:03 -0700490
491 // Clean out old info about up switch frames.
philipel0fa82a62018-03-19 15:34:53 +0100492 uint16_t old_picture_id = Subtract<kPicIdLength>(frame->id.picture_id, 50);
philipel02447bc2016-05-13 06:01:03 -0700493 auto up_switch_erase_to = up_switch_.lower_bound(old_picture_id);
494 up_switch_.erase(up_switch_.begin(), up_switch_erase_to);
495
philipelc9b27d52016-07-15 06:50:27 -0700496 size_t diff = ForwardDiff<uint16_t, kPicIdLength>(info->gof->pid_start,
philipel0fa82a62018-03-19 15:34:53 +0100497 frame->id.picture_id);
philipelc9b27d52016-07-15 06:50:27 -0700498 size_t gof_idx = diff % info->gof->num_frames_in_gof;
philipel02447bc2016-05-13 06:01:03 -0700499
500 // Populate references according to the scalability structure.
philipelc9b27d52016-07-15 06:50:27 -0700501 frame->num_references = info->gof->num_ref_pics[gof_idx];
philipel02447bc2016-05-13 06:01:03 -0700502 for (size_t i = 0; i < frame->num_references; ++i) {
philipelc9b27d52016-07-15 06:50:27 -0700503 frame->references[i] = Subtract<kPicIdLength>(
philipel0fa82a62018-03-19 15:34:53 +0100504 frame->id.picture_id, info->gof->pid_diff[gof_idx][i]);
philipel02447bc2016-05-13 06:01:03 -0700505
506 // If this is a reference to a frame earlier than the last up switch point,
507 // then ignore this reference.
philipel0fa82a62018-03-19 15:34:53 +0100508 if (UpSwitchInIntervalVp9(frame->id.picture_id, codec_header.temporal_idx,
philipel02447bc2016-05-13 06:01:03 -0700509 frame->references[i])) {
510 --frame->num_references;
511 }
512 }
513
philipelafcf7f52017-04-26 08:17:35 -0700514 UnwrapPictureIds(frame);
515 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700516}
517
518bool RtpFrameReferenceFinder::MissingRequiredFrameVp9(uint16_t picture_id,
philipelc9b27d52016-07-15 06:50:27 -0700519 const GofInfo& info) {
520 size_t diff =
521 ForwardDiff<uint16_t, kPicIdLength>(info.gof->pid_start, picture_id);
522 size_t gof_idx = diff % info.gof->num_frames_in_gof;
523 size_t temporal_idx = info.gof->temporal_idx[gof_idx];
philipel02447bc2016-05-13 06:01:03 -0700524
philipela157e082018-05-02 15:19:01 +0200525 if (temporal_idx >= kMaxTemporalLayers) {
526 RTC_LOG(LS_WARNING) << "At most " << kMaxTemporalLayers << " temporal "
527 << "layers are supported.";
528 return true;
529 }
530
philipel02447bc2016-05-13 06:01:03 -0700531 // For every reference this frame has, check if there is a frame missing in
532 // the interval (|ref_pid|, |picture_id|) in any of the lower temporal
533 // layers. If so, we are missing a required frame.
philipelc9b27d52016-07-15 06:50:27 -0700534 uint8_t num_references = info.gof->num_ref_pics[gof_idx];
philipel02447bc2016-05-13 06:01:03 -0700535 for (size_t i = 0; i < num_references; ++i) {
536 uint16_t ref_pid =
philipelc9b27d52016-07-15 06:50:27 -0700537 Subtract<kPicIdLength>(picture_id, info.gof->pid_diff[gof_idx][i]);
philipel02447bc2016-05-13 06:01:03 -0700538 for (size_t l = 0; l < temporal_idx; ++l) {
539 auto missing_frame_it = missing_frames_for_layer_[l].lower_bound(ref_pid);
540 if (missing_frame_it != missing_frames_for_layer_[l].end() &&
541 AheadOf<uint16_t, kPicIdLength>(picture_id, *missing_frame_it)) {
542 return true;
543 }
544 }
545 }
546 return false;
547}
548
549void RtpFrameReferenceFinder::FrameReceivedVp9(uint16_t picture_id,
philipelc9b27d52016-07-15 06:50:27 -0700550 GofInfo* info) {
551 int last_picture_id = info->last_picture_id;
philipel459f4e32018-03-02 10:55:12 +0100552 size_t gof_size = std::min(info->gof->num_frames_in_gof, kMaxVp9FramesInGof);
philipel02447bc2016-05-13 06:01:03 -0700553
554 // If there is a gap, find which temporal layer the missing frames
555 // belong to and add the frame as missing for that temporal layer.
556 // Otherwise, remove this frame from the set of missing frames.
philipelc9b27d52016-07-15 06:50:27 -0700557 if (AheadOf<uint16_t, kPicIdLength>(picture_id, last_picture_id)) {
558 size_t diff = ForwardDiff<uint16_t, kPicIdLength>(info->gof->pid_start,
559 last_picture_id);
philipel459f4e32018-03-02 10:55:12 +0100560 size_t gof_idx = diff % gof_size;
philipel02447bc2016-05-13 06:01:03 -0700561
philipelc9b27d52016-07-15 06:50:27 -0700562 last_picture_id = Add<kPicIdLength>(last_picture_id, 1);
563 while (last_picture_id != picture_id) {
Yves Gerey665174f2018-06-19 15:03:05 +0200564 gof_idx = (gof_idx + 1) % gof_size;
philipel459f4e32018-03-02 10:55:12 +0100565 RTC_CHECK(gof_idx < kMaxVp9FramesInGof);
566
philipelc9b27d52016-07-15 06:50:27 -0700567 size_t temporal_idx = info->gof->temporal_idx[gof_idx];
philipel459f4e32018-03-02 10:55:12 +0100568 if (temporal_idx >= kMaxTemporalLayers) {
569 RTC_LOG(LS_WARNING) << "At most " << kMaxTemporalLayers << " temporal "
570 << "layers are supported.";
571 return;
572 }
573
philipelc9b27d52016-07-15 06:50:27 -0700574 missing_frames_for_layer_[temporal_idx].insert(last_picture_id);
575 last_picture_id = Add<kPicIdLength>(last_picture_id, 1);
philipel02447bc2016-05-13 06:01:03 -0700576 }
philipel459f4e32018-03-02 10:55:12 +0100577
philipelc9b27d52016-07-15 06:50:27 -0700578 info->last_picture_id = last_picture_id;
philipel02447bc2016-05-13 06:01:03 -0700579 } else {
580 size_t diff =
philipelc9b27d52016-07-15 06:50:27 -0700581 ForwardDiff<uint16_t, kPicIdLength>(info->gof->pid_start, picture_id);
philipel459f4e32018-03-02 10:55:12 +0100582 size_t gof_idx = diff % gof_size;
583 RTC_CHECK(gof_idx < kMaxVp9FramesInGof);
584
philipelc9b27d52016-07-15 06:50:27 -0700585 size_t temporal_idx = info->gof->temporal_idx[gof_idx];
philipel459f4e32018-03-02 10:55:12 +0100586 if (temporal_idx >= kMaxTemporalLayers) {
587 RTC_LOG(LS_WARNING) << "At most " << kMaxTemporalLayers << " temporal "
588 << "layers are supported.";
589 return;
590 }
591
philipel02447bc2016-05-13 06:01:03 -0700592 missing_frames_for_layer_[temporal_idx].erase(picture_id);
593 }
594}
595
596bool RtpFrameReferenceFinder::UpSwitchInIntervalVp9(uint16_t picture_id,
597 uint8_t temporal_idx,
598 uint16_t pid_ref) {
599 for (auto up_switch_it = up_switch_.upper_bound(pid_ref);
600 up_switch_it != up_switch_.end() &&
601 AheadOf<uint16_t, kPicIdLength>(picture_id, up_switch_it->first);
602 ++up_switch_it) {
603 if (up_switch_it->second < temporal_idx)
604 return true;
605 }
606
607 return false;
608}
609
philipelafcf7f52017-04-26 08:17:35 -0700610void RtpFrameReferenceFinder::UnwrapPictureIds(RtpFrameObject* frame) {
philipel02447bc2016-05-13 06:01:03 -0700611 for (size_t i = 0; i < frame->num_references; ++i)
philipeld4fac692017-09-04 07:03:46 -0700612 frame->references[i] = unwrapper_.Unwrap(frame->references[i]);
philipel0fa82a62018-03-19 15:34:53 +0100613 frame->id.picture_id = unwrapper_.Unwrap(frame->id.picture_id);
philipel02447bc2016-05-13 06:01:03 -0700614}
615
philipel02447bc2016-05-13 06:01:03 -0700616} // namespace video_coding
617} // namespace webrtc