blob: 7714b29d92cbc9624fffb5b23d77dbf0fadcb04e [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
Mirko Bonadei8fdcac32018-08-28 16:30:18 +020034RtpFrameReferenceFinder::~RtpFrameReferenceFinder() = default;
35
philipel02447bc2016-05-13 06:01:03 -070036void RtpFrameReferenceFinder::ManageFrame(
37 std::unique_ptr<RtpFrameObject> frame) {
38 rtc::CritScope lock(&crit_);
philipel463d3012016-09-09 03:32:44 -070039
40 // If we have cleared past this frame, drop it.
41 if (cleared_to_seq_num_ != -1 &&
42 AheadOf<uint16_t>(cleared_to_seq_num_, frame->first_seq_num())) {
43 return;
44 }
45
philipelafcf7f52017-04-26 08:17:35 -070046 FrameDecision decision = ManageFrameInternal(frame.get());
47
48 switch (decision) {
49 case kStash:
50 if (stashed_frames_.size() > kMaxStashedFrames)
51 stashed_frames_.pop_back();
52 stashed_frames_.push_front(std::move(frame));
53 break;
54 case kHandOff:
55 frame_callback_->OnCompleteFrame(std::move(frame));
56 RetryStashedFrames();
57 break;
58 case kDrop:
59 break;
60 }
61}
62
63void RtpFrameReferenceFinder::RetryStashedFrames() {
64 bool complete_frame = false;
65 do {
66 complete_frame = false;
67 for (auto frame_it = stashed_frames_.begin();
68 frame_it != stashed_frames_.end();) {
69 FrameDecision decision = ManageFrameInternal(frame_it->get());
70
71 switch (decision) {
72 case kStash:
73 ++frame_it;
74 break;
75 case kHandOff:
76 complete_frame = true;
77 frame_callback_->OnCompleteFrame(std::move(*frame_it));
Karl Wiberg80ba3332018-02-05 10:33:35 +010078 RTC_FALLTHROUGH();
philipelafcf7f52017-04-26 08:17:35 -070079 case kDrop:
80 frame_it = stashed_frames_.erase(frame_it);
81 }
82 }
83 } while (complete_frame);
84}
85
86RtpFrameReferenceFinder::FrameDecision
87RtpFrameReferenceFinder::ManageFrameInternal(RtpFrameObject* frame) {
philipel02447bc2016-05-13 06:01:03 -070088 switch (frame->codec_type()) {
philipel02447bc2016-05-13 06:01:03 -070089 case kVideoCodecVP8:
philipelafcf7f52017-04-26 08:17:35 -070090 return ManageFrameVp8(frame);
philipel02447bc2016-05-13 06:01:03 -070091 case kVideoCodecVP9:
philipelafcf7f52017-04-26 08:17:35 -070092 return ManageFrameVp9(frame);
Sami Kalliomäki98824952018-08-28 14:39:21 +020093 default: {
94 // Use 15 first bits of frame ID as picture ID if available.
95 absl::optional<RTPVideoHeader> video_header = frame->GetCodecHeader();
96 absl::optional<RTPVideoHeader::GenericDescriptorInfo> generic_info =
97 video_header ? video_header->generic : absl::nullopt;
98 return ManageFrameGeneric(
99 frame, generic_info ? generic_info->frame_id & 0x7fff : kNoPictureId);
100 }
philipel02447bc2016-05-13 06:01:03 -0700101 }
102}
103
philipel9b2ce6b2016-07-05 05:04:46 -0700104void RtpFrameReferenceFinder::PaddingReceived(uint16_t seq_num) {
105 rtc::CritScope lock(&crit_);
106 auto clean_padding_to =
107 stashed_padding_.lower_bound(seq_num - kMaxPaddingAge);
108 stashed_padding_.erase(stashed_padding_.begin(), clean_padding_to);
109 stashed_padding_.insert(seq_num);
110 UpdateLastPictureIdWithPadding(seq_num);
111 RetryStashedFrames();
112}
113
philipel463d3012016-09-09 03:32:44 -0700114void RtpFrameReferenceFinder::ClearTo(uint16_t seq_num) {
115 rtc::CritScope lock(&crit_);
116 cleared_to_seq_num_ = seq_num;
117
118 auto it = stashed_frames_.begin();
119 while (it != stashed_frames_.end()) {
120 if (AheadOf<uint16_t>(cleared_to_seq_num_, (*it)->first_seq_num())) {
121 it = stashed_frames_.erase(it);
122 } else {
123 ++it;
124 }
125 }
126}
127
philipel9b2ce6b2016-07-05 05:04:46 -0700128void RtpFrameReferenceFinder::UpdateLastPictureIdWithPadding(uint16_t seq_num) {
129 auto gop_seq_num_it = last_seq_num_gop_.upper_bound(seq_num);
130
131 // If this padding packet "belongs" to a group of pictures that we don't track
132 // anymore, do nothing.
133 if (gop_seq_num_it == last_seq_num_gop_.begin())
134 return;
135 --gop_seq_num_it;
136
137 // Calculate the next contiuous sequence number and search for it in
138 // the padding packets we have stashed.
139 uint16_t next_seq_num_with_padding = gop_seq_num_it->second.second + 1;
140 auto padding_seq_num_it =
141 stashed_padding_.lower_bound(next_seq_num_with_padding);
142
143 // While there still are padding packets and those padding packets are
144 // continuous, then advance the "last-picture-id-with-padding" and remove
145 // the stashed padding packet.
146 while (padding_seq_num_it != stashed_padding_.end() &&
147 *padding_seq_num_it == next_seq_num_with_padding) {
148 gop_seq_num_it->second.second = next_seq_num_with_padding;
149 ++next_seq_num_with_padding;
150 padding_seq_num_it = stashed_padding_.erase(padding_seq_num_it);
151 }
philipel41bb7922017-02-20 07:53:23 -0800152
153 // In the case where the stream has been continuous without any new keyframes
154 // for a while there is a risk that new frames will appear to be older than
155 // the keyframe they belong to due to wrapping sequence number. In order
156 // to prevent this we advance the picture id of the keyframe every so often.
157 if (ForwardDiff(gop_seq_num_it->first, seq_num) > 10000) {
158 RTC_DCHECK_EQ(1ul, last_seq_num_gop_.size());
159 last_seq_num_gop_[seq_num] = gop_seq_num_it->second;
160 last_seq_num_gop_.erase(gop_seq_num_it);
161 }
philipel9b2ce6b2016-07-05 05:04:46 -0700162}
163
philipelafcf7f52017-04-26 08:17:35 -0700164RtpFrameReferenceFinder::FrameDecision
165RtpFrameReferenceFinder::ManageFrameGeneric(RtpFrameObject* frame,
166 int picture_id) {
philipel647998c2016-06-03 09:40:16 -0700167 // If |picture_id| is specified then we use that to set the frame references,
168 // otherwise we use sequence number.
169 if (picture_id != kNoPictureId) {
170 if (last_unwrap_ == -1)
171 last_unwrap_ = picture_id;
172
philipel0fa82a62018-03-19 15:34:53 +0100173 frame->id.picture_id = unwrapper_.Unwrap(picture_id);
philipel647998c2016-06-03 09:40:16 -0700174 frame->num_references = frame->frame_type() == kVideoFrameKey ? 0 : 1;
philipel0fa82a62018-03-19 15:34:53 +0100175 frame->references[0] = frame->id.picture_id - 1;
philipelafcf7f52017-04-26 08:17:35 -0700176 return kHandOff;
philipel647998c2016-06-03 09:40:16 -0700177 }
178
philipel9b2ce6b2016-07-05 05:04:46 -0700179 if (frame->frame_type() == kVideoFrameKey) {
180 last_seq_num_gop_.insert(std::make_pair(
181 frame->last_seq_num(),
182 std::make_pair(frame->last_seq_num(), frame->last_seq_num())));
183 }
philipel02447bc2016-05-13 06:01:03 -0700184
185 // We have received a frame but not yet a keyframe, stash this frame.
philipelafcf7f52017-04-26 08:17:35 -0700186 if (last_seq_num_gop_.empty())
187 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700188
189 // Clean up info for old keyframes but make sure to keep info
190 // for the last keyframe.
191 auto clean_to = last_seq_num_gop_.lower_bound(frame->last_seq_num() - 100);
philipel41bb7922017-02-20 07:53:23 -0800192 for (auto it = last_seq_num_gop_.begin();
193 it != clean_to && last_seq_num_gop_.size() > 1;) {
194 it = last_seq_num_gop_.erase(it);
195 }
philipel02447bc2016-05-13 06:01:03 -0700196
197 // Find the last sequence number of the last frame for the keyframe
198 // that this frame indirectly references.
199 auto seq_num_it = last_seq_num_gop_.upper_bound(frame->last_seq_num());
philipel9b2ce6b2016-07-05 05:04:46 -0700200 if (seq_num_it == last_seq_num_gop_.begin()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100201 RTC_LOG(LS_WARNING) << "Generic frame with packet range ["
202 << frame->first_seq_num() << ", "
203 << frame->last_seq_num()
204 << "] has no GoP, dropping frame.";
philipelafcf7f52017-04-26 08:17:35 -0700205 return kDrop;
philipel9b2ce6b2016-07-05 05:04:46 -0700206 }
philipel02447bc2016-05-13 06:01:03 -0700207 seq_num_it--;
208
209 // Make sure the packet sequence numbers are continuous, otherwise stash
210 // this frame.
philipel9b2ce6b2016-07-05 05:04:46 -0700211 uint16_t last_picture_id_gop = seq_num_it->second.first;
212 uint16_t last_picture_id_with_padding_gop = seq_num_it->second.second;
philipel02447bc2016-05-13 06:01:03 -0700213 if (frame->frame_type() == kVideoFrameDelta) {
philipel9b2ce6b2016-07-05 05:04:46 -0700214 uint16_t prev_seq_num = frame->first_seq_num() - 1;
philipelafcf7f52017-04-26 08:17:35 -0700215
216 if (prev_seq_num != last_picture_id_with_padding_gop)
217 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700218 }
219
220 RTC_DCHECK(AheadOrAt(frame->last_seq_num(), seq_num_it->first));
221
222 // Since keyframes can cause reordering we can't simply assign the
223 // picture id according to some incrementing counter.
philipel0fa82a62018-03-19 15:34:53 +0100224 frame->id.picture_id = frame->last_seq_num();
philipel02447bc2016-05-13 06:01:03 -0700225 frame->num_references = frame->frame_type() == kVideoFrameDelta;
philipeld4fac692017-09-04 07:03:46 -0700226 frame->references[0] = generic_unwrapper_.Unwrap(last_picture_id_gop);
philipel0fa82a62018-03-19 15:34:53 +0100227 if (AheadOf<uint16_t>(frame->id.picture_id, last_picture_id_gop)) {
228 seq_num_it->second.first = frame->id.picture_id;
229 seq_num_it->second.second = frame->id.picture_id;
philipel9b2ce6b2016-07-05 05:04:46 -0700230 }
philipel02447bc2016-05-13 06:01:03 -0700231
philipel0fa82a62018-03-19 15:34:53 +0100232 last_picture_id_ = frame->id.picture_id;
233 UpdateLastPictureIdWithPadding(frame->id.picture_id);
234 frame->id.picture_id = generic_unwrapper_.Unwrap(frame->id.picture_id);
philipelafcf7f52017-04-26 08:17:35 -0700235 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700236}
237
philipelafcf7f52017-04-26 08:17:35 -0700238RtpFrameReferenceFinder::FrameDecision RtpFrameReferenceFinder::ManageFrameVp8(
239 RtpFrameObject* frame) {
Sami Kalliomäki98824952018-08-28 14:39:21 +0200240 absl::optional<RTPVideoHeader> video_header = frame->GetCodecHeader();
241 if (!video_header) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100242 RTC_LOG(LS_WARNING)
243 << "Failed to get codec header from frame, dropping frame.";
philipelafcf7f52017-04-26 08:17:35 -0700244 return kDrop;
philipeld4fac692017-09-04 07:03:46 -0700245 }
Sami Kalliomäki98824952018-08-28 14:39:21 +0200246 RTPVideoTypeHeader rtp_codec_header = video_header->video_type_header;
philipel02447bc2016-05-13 06:01:03 -0700247
philipel1a4746a2018-07-09 15:52:29 +0200248 const RTPVideoHeaderVP8& codec_header =
Sami Kalliomäki98824952018-08-28 14:39:21 +0200249 absl::get<RTPVideoHeaderVP8>(rtp_codec_header);
philipel02447bc2016-05-13 06:01:03 -0700250
251 if (codec_header.pictureId == kNoPictureId ||
252 codec_header.temporalIdx == kNoTemporalIdx ||
253 codec_header.tl0PicIdx == kNoTl0PicIdx) {
philipelafcf7f52017-04-26 08:17:35 -0700254 return ManageFrameGeneric(std::move(frame), codec_header.pictureId);
philipel02447bc2016-05-13 06:01:03 -0700255 }
256
philipel0fa82a62018-03-19 15:34:53 +0100257 frame->id.picture_id = codec_header.pictureId % kPicIdLength;
philipel02447bc2016-05-13 06:01:03 -0700258
259 if (last_unwrap_ == -1)
260 last_unwrap_ = codec_header.pictureId;
261
262 if (last_picture_id_ == -1)
philipel0fa82a62018-03-19 15:34:53 +0100263 last_picture_id_ = frame->id.picture_id;
philipel02447bc2016-05-13 06:01:03 -0700264
265 // Find if there has been a gap in fully received frames and save the picture
266 // id of those frames in |not_yet_received_frames_|.
philipel0fa82a62018-03-19 15:34:53 +0100267 if (AheadOf<uint16_t, kPicIdLength>(frame->id.picture_id, last_picture_id_)) {
philipel9bd1d662017-07-14 04:52:01 -0700268 do {
philipel02447bc2016-05-13 06:01:03 -0700269 last_picture_id_ = Add<kPicIdLength>(last_picture_id_, 1);
philipel9bd1d662017-07-14 04:52:01 -0700270 not_yet_received_frames_.insert(last_picture_id_);
philipel0fa82a62018-03-19 15:34:53 +0100271 } while (last_picture_id_ != frame->id.picture_id);
philipel02447bc2016-05-13 06:01:03 -0700272 }
273
philipel57ec6852018-07-03 18:09:32 +0200274 int64_t unwrapped_tl0 = tl0_unwrapper_.Unwrap(codec_header.tl0PicIdx);
275
philipel02447bc2016-05-13 06:01:03 -0700276 // Clean up info for base layers that are too old.
philipel57ec6852018-07-03 18:09:32 +0200277 int64_t old_tl0_pic_idx = unwrapped_tl0 - kMaxLayerInfo;
philipel02447bc2016-05-13 06:01:03 -0700278 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 =
philipel0fa82a62018-03-19 15:34:53 +0100283 Subtract<kPicIdLength>(frame->id.picture_id, kMaxNotYetReceivedFrames);
philipel02447bc2016-05-13 06:01:03 -0700284 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;
philipel57ec6852018-07-03 18:09:32 +0200290 layer_info_[unwrapped_tl0].fill(-1);
291 UpdateLayerInfoVp8(frame, unwrapped_tl0, codec_header.temporalIdx);
philipelafcf7f52017-04-26 08:17:35 -0700292 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700293 }
294
philipel57ec6852018-07-03 18:09:32 +0200295 auto layer_info_it = layer_info_.find(
296 codec_header.temporalIdx == 0 ? unwrapped_tl0 - 1 : unwrapped_tl0);
philipel02447bc2016-05-13 06:01:03 -0700297
298 // If we don't have the base layer frame yet, stash this frame.
philipelafcf7f52017-04-26 08:17:35 -0700299 if (layer_info_it == layer_info_.end())
300 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700301
302 // A non keyframe base layer frame has been received, copy the layer info
303 // from the previous base layer frame and set a reference to the previous
304 // base layer frame.
305 if (codec_header.temporalIdx == 0) {
306 layer_info_it =
philipel57ec6852018-07-03 18:09:32 +0200307 layer_info_.emplace(unwrapped_tl0, layer_info_it->second).first;
philipel02447bc2016-05-13 06:01:03 -0700308 frame->num_references = 1;
309 frame->references[0] = layer_info_it->second[0];
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 // Layer sync frame, this frame only references its base layer frame.
315 if (codec_header.layerSync) {
316 frame->num_references = 1;
317 frame->references[0] = layer_info_it->second[0];
318
philipel57ec6852018-07-03 18:09:32 +0200319 UpdateLayerInfoVp8(frame, unwrapped_tl0, codec_header.temporalIdx);
philipelafcf7f52017-04-26 08:17:35 -0700320 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700321 }
322
323 // Find all references for this frame.
324 frame->num_references = 0;
325 for (uint8_t layer = 0; layer <= codec_header.temporalIdx; ++layer) {
philipeld268d6f2016-09-15 13:43:13 +0200326 // If we have not yet received a previous frame on this temporal layer,
327 // stash this frame.
philipelafcf7f52017-04-26 08:17:35 -0700328 if (layer_info_it->second[layer] == -1)
329 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700330
philipel86b92e02016-10-24 07:11:53 -0700331 // If the last frame on this layer is ahead of this frame it means that
332 // a layer sync frame has been received after this frame for the same
333 // base layer frame, drop this frame.
334 if (AheadOf<uint16_t, kPicIdLength>(layer_info_it->second[layer],
philipel0fa82a62018-03-19 15:34:53 +0100335 frame->id.picture_id)) {
philipelafcf7f52017-04-26 08:17:35 -0700336 return kDrop;
philipel86b92e02016-10-24 07:11:53 -0700337 }
338
philipel02447bc2016-05-13 06:01:03 -0700339 // If we have not yet received a frame between this frame and the referenced
340 // frame then we have to wait for that frame to be completed first.
341 auto not_received_frame_it =
342 not_yet_received_frames_.upper_bound(layer_info_it->second[layer]);
343 if (not_received_frame_it != not_yet_received_frames_.end() &&
philipel0fa82a62018-03-19 15:34:53 +0100344 AheadOf<uint16_t, kPicIdLength>(frame->id.picture_id,
philipel02447bc2016-05-13 06:01:03 -0700345 *not_received_frame_it)) {
philipelafcf7f52017-04-26 08:17:35 -0700346 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700347 }
348
philipel0fa82a62018-03-19 15:34:53 +0100349 if (!(AheadOf<uint16_t, kPicIdLength>(frame->id.picture_id,
philipel57f19cc2017-03-07 03:54:05 -0800350 layer_info_it->second[layer]))) {
philipel0fa82a62018-03-19 15:34:53 +0100351 RTC_LOG(LS_WARNING) << "Frame with picture id " << frame->id.picture_id
Mirko Bonadei675513b2017-11-09 11:09:25 +0100352 << " and packet range [" << frame->first_seq_num()
353 << ", " << frame->last_seq_num()
354 << "] already received, "
355 << " dropping frame.";
philipelafcf7f52017-04-26 08:17:35 -0700356 return kDrop;
philipel57f19cc2017-03-07 03:54:05 -0800357 }
358
philipel02447bc2016-05-13 06:01:03 -0700359 ++frame->num_references;
360 frame->references[layer] = layer_info_it->second[layer];
361 }
362
philipel57ec6852018-07-03 18:09:32 +0200363 UpdateLayerInfoVp8(frame, unwrapped_tl0, codec_header.temporalIdx);
philipelafcf7f52017-04-26 08:17:35 -0700364 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700365}
366
philipel57ec6852018-07-03 18:09:32 +0200367void RtpFrameReferenceFinder::UpdateLayerInfoVp8(RtpFrameObject* frame,
368 int64_t unwrapped_tl0,
369 uint8_t temporal_idx) {
370 auto layer_info_it = layer_info_.find(unwrapped_tl0);
philipel02447bc2016-05-13 06:01:03 -0700371
372 // Update this layer info and newer.
373 while (layer_info_it != layer_info_.end()) {
philipel57ec6852018-07-03 18:09:32 +0200374 if (layer_info_it->second[temporal_idx] != -1 &&
375 AheadOf<uint16_t, kPicIdLength>(layer_info_it->second[temporal_idx],
philipel0fa82a62018-03-19 15:34:53 +0100376 frame->id.picture_id)) {
philipel02447bc2016-05-13 06:01:03 -0700377 // The frame was not newer, then no subsequent layer info have to be
378 // update.
379 break;
380 }
381
philipel57ec6852018-07-03 18:09:32 +0200382 layer_info_it->second[temporal_idx] = frame->id.picture_id;
383 ++unwrapped_tl0;
384 layer_info_it = layer_info_.find(unwrapped_tl0);
philipel02447bc2016-05-13 06:01:03 -0700385 }
philipel0fa82a62018-03-19 15:34:53 +0100386 not_yet_received_frames_.erase(frame->id.picture_id);
philipel02447bc2016-05-13 06:01:03 -0700387
philipelafcf7f52017-04-26 08:17:35 -0700388 UnwrapPictureIds(frame);
philipel02447bc2016-05-13 06:01:03 -0700389}
390
philipelafcf7f52017-04-26 08:17:35 -0700391RtpFrameReferenceFinder::FrameDecision RtpFrameReferenceFinder::ManageFrameVp9(
392 RtpFrameObject* frame) {
Sami Kalliomäki98824952018-08-28 14:39:21 +0200393 absl::optional<RTPVideoHeader> video_header = frame->GetCodecHeader();
394 if (!video_header) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100395 RTC_LOG(LS_WARNING)
396 << "Failed to get codec header from frame, dropping frame.";
philipel4c140092017-08-31 08:31:45 -0700397 return kDrop;
philipeld4fac692017-09-04 07:03:46 -0700398 }
Sami Kalliomäki98824952018-08-28 14:39:21 +0200399 RTPVideoTypeHeader rtp_codec_header = video_header->video_type_header;
philipel4c140092017-08-31 08:31:45 -0700400
philipel1a4746a2018-07-09 15:52:29 +0200401 const RTPVideoHeaderVP9& codec_header =
Sami Kalliomäki98824952018-08-28 14:39:21 +0200402 absl::get<RTPVideoHeaderVP9>(rtp_codec_header);
philipel02447bc2016-05-13 06:01:03 -0700403
philipel647998c2016-06-03 09:40:16 -0700404 if (codec_header.picture_id == kNoPictureId ||
Sergey Silkind34a1882018-08-20 16:46:05 +0200405 codec_header.temporal_idx == kNoTemporalIdx) {
philipelafcf7f52017-04-26 08:17:35 -0700406 return ManageFrameGeneric(std::move(frame), codec_header.picture_id);
philipel02447bc2016-05-13 06:01:03 -0700407 }
408
philipel0fa82a62018-03-19 15:34:53 +0100409 frame->id.spatial_layer = codec_header.spatial_idx;
philipel02447bc2016-05-13 06:01:03 -0700410 frame->inter_layer_predicted = codec_header.inter_layer_predicted;
philipel0fa82a62018-03-19 15:34:53 +0100411 frame->id.picture_id = codec_header.picture_id % kPicIdLength;
philipel02447bc2016-05-13 06:01:03 -0700412
413 if (last_unwrap_ == -1)
414 last_unwrap_ = codec_header.picture_id;
415
416 if (last_picture_id_ == -1)
philipel0fa82a62018-03-19 15:34:53 +0100417 last_picture_id_ = frame->id.picture_id;
philipel02447bc2016-05-13 06:01:03 -0700418
419 if (codec_header.flexible_mode) {
420 frame->num_references = codec_header.num_ref_pics;
421 for (size_t i = 0; i < frame->num_references; ++i) {
philipel0fa82a62018-03-19 15:34:53 +0100422 frame->references[i] = Subtract<kPicIdLength>(frame->id.picture_id,
423 codec_header.pid_diff[i]);
philipel02447bc2016-05-13 06:01:03 -0700424 }
425
philipelafcf7f52017-04-26 08:17:35 -0700426 UnwrapPictureIds(frame);
427 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700428 }
429
Sergey Silkind34a1882018-08-20 16:46:05 +0200430 if (codec_header.tl0_pic_idx == kNoTl0PicIdx) {
431 RTC_LOG(LS_WARNING) << "TL0PICIDX is expected to be present in "
432 "non-flexible mode.";
433 return kDrop;
434 }
435
philipel15643602018-05-03 16:14:13 +0200436 GofInfo* info;
philipel57ec6852018-07-03 18:09:32 +0200437 int64_t unwrapped_tl0 = tl0_unwrapper_.Unwrap(codec_header.tl0_pic_idx);
philipel02447bc2016-05-13 06:01:03 -0700438 if (codec_header.ss_data_available) {
philipel02447bc2016-05-13 06:01:03 -0700439 if (codec_header.temporal_idx != 0) {
philipel15643602018-05-03 16:14:13 +0200440 RTC_LOG(LS_WARNING) << "Received scalability structure on a non base "
441 "layer frame. Scalability structure ignored.";
philipel02447bc2016-05-13 06:01:03 -0700442 } else {
443 current_ss_idx_ = Add<kMaxGofSaved>(current_ss_idx_, 1);
Natalie Silvanovich3ea3e302018-05-16 11:03:12 -0700444 if (codec_header.gof.num_frames_in_gof == 0 ||
445 codec_header.gof.num_frames_in_gof > kMaxVp9FramesInGof) {
446 return kDrop;
447 }
448
philipel02447bc2016-05-13 06:01:03 -0700449 scalability_structures_[current_ss_idx_] = codec_header.gof;
philipel0fa82a62018-03-19 15:34:53 +0100450 scalability_structures_[current_ss_idx_].pid_start = frame->id.picture_id;
philipel57ec6852018-07-03 18:09:32 +0200451 gof_info_.emplace(unwrapped_tl0,
philipel15643602018-05-03 16:14:13 +0200452 GofInfo(&scalability_structures_[current_ss_idx_],
453 frame->id.picture_id));
philipel02447bc2016-05-13 06:01:03 -0700454 }
philipel15643602018-05-03 16:14:13 +0200455
philipel57ec6852018-07-03 18:09:32 +0200456 const auto gof_info_it = gof_info_.find(unwrapped_tl0);
philipel15643602018-05-03 16:14:13 +0200457 if (gof_info_it == gof_info_.end())
458 return kStash;
459
460 info = &gof_info_it->second;
461
462 if (frame->frame_type() == kVideoFrameKey) {
463 frame->num_references = 0;
464 FrameReceivedVp9(frame->id.picture_id, info);
465 UnwrapPictureIds(frame);
466 return kHandOff;
467 }
468 } else {
469 if (frame->frame_type() == kVideoFrameKey) {
470 RTC_LOG(LS_WARNING) << "Received keyframe without scalability structure";
471 return kDrop;
472 }
473
philipel57ec6852018-07-03 18:09:32 +0200474 auto gof_info_it = gof_info_.find(
475 (codec_header.temporal_idx == 0) ? unwrapped_tl0 - 1 : unwrapped_tl0);
philipel15643602018-05-03 16:14:13 +0200476
477 // Gof info for this frame is not available yet, stash this frame.
478 if (gof_info_it == gof_info_.end())
479 return kStash;
480
481 if (codec_header.temporal_idx == 0) {
philipel57ec6852018-07-03 18:09:32 +0200482 gof_info_it = gof_info_
483 .emplace(unwrapped_tl0, GofInfo(gof_info_it->second.gof,
484 frame->id.picture_id))
485 .first;
philipel15643602018-05-03 16:14:13 +0200486 }
487
488 info = &gof_info_it->second;
philipel02447bc2016-05-13 06:01:03 -0700489 }
490
491 // Clean up info for base layers that are too old.
philipel57ec6852018-07-03 18:09:32 +0200492 int64_t old_tl0_pic_idx = unwrapped_tl0 - kMaxGofSaved;
philipel02447bc2016-05-13 06:01:03 -0700493 auto clean_gof_info_to = gof_info_.lower_bound(old_tl0_pic_idx);
494 gof_info_.erase(gof_info_.begin(), clean_gof_info_to);
495
philipel0fa82a62018-03-19 15:34:53 +0100496 FrameReceivedVp9(frame->id.picture_id, info);
philipel02447bc2016-05-13 06:01:03 -0700497
498 // Make sure we don't miss any frame that could potentially have the
499 // up switch flag set.
philipel0fa82a62018-03-19 15:34:53 +0100500 if (MissingRequiredFrameVp9(frame->id.picture_id, *info))
philipelafcf7f52017-04-26 08:17:35 -0700501 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700502
philipel15643602018-05-03 16:14:13 +0200503 if (codec_header.temporal_up_switch)
504 up_switch_.emplace(frame->id.picture_id, codec_header.temporal_idx);
philipel02447bc2016-05-13 06:01:03 -0700505
506 // Clean out old info about up switch frames.
philipel0fa82a62018-03-19 15:34:53 +0100507 uint16_t old_picture_id = Subtract<kPicIdLength>(frame->id.picture_id, 50);
philipel02447bc2016-05-13 06:01:03 -0700508 auto up_switch_erase_to = up_switch_.lower_bound(old_picture_id);
509 up_switch_.erase(up_switch_.begin(), up_switch_erase_to);
510
philipelc9b27d52016-07-15 06:50:27 -0700511 size_t diff = ForwardDiff<uint16_t, kPicIdLength>(info->gof->pid_start,
philipel0fa82a62018-03-19 15:34:53 +0100512 frame->id.picture_id);
philipelc9b27d52016-07-15 06:50:27 -0700513 size_t gof_idx = diff % info->gof->num_frames_in_gof;
philipel02447bc2016-05-13 06:01:03 -0700514
515 // Populate references according to the scalability structure.
philipelc9b27d52016-07-15 06:50:27 -0700516 frame->num_references = info->gof->num_ref_pics[gof_idx];
philipel02447bc2016-05-13 06:01:03 -0700517 for (size_t i = 0; i < frame->num_references; ++i) {
philipelc9b27d52016-07-15 06:50:27 -0700518 frame->references[i] = Subtract<kPicIdLength>(
philipel0fa82a62018-03-19 15:34:53 +0100519 frame->id.picture_id, info->gof->pid_diff[gof_idx][i]);
philipel02447bc2016-05-13 06:01:03 -0700520
521 // If this is a reference to a frame earlier than the last up switch point,
522 // then ignore this reference.
philipel0fa82a62018-03-19 15:34:53 +0100523 if (UpSwitchInIntervalVp9(frame->id.picture_id, codec_header.temporal_idx,
philipel02447bc2016-05-13 06:01:03 -0700524 frame->references[i])) {
525 --frame->num_references;
526 }
527 }
528
philipelafcf7f52017-04-26 08:17:35 -0700529 UnwrapPictureIds(frame);
530 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700531}
532
533bool RtpFrameReferenceFinder::MissingRequiredFrameVp9(uint16_t picture_id,
philipelc9b27d52016-07-15 06:50:27 -0700534 const GofInfo& info) {
535 size_t diff =
536 ForwardDiff<uint16_t, kPicIdLength>(info.gof->pid_start, picture_id);
537 size_t gof_idx = diff % info.gof->num_frames_in_gof;
538 size_t temporal_idx = info.gof->temporal_idx[gof_idx];
philipel02447bc2016-05-13 06:01:03 -0700539
philipela157e082018-05-02 15:19:01 +0200540 if (temporal_idx >= kMaxTemporalLayers) {
541 RTC_LOG(LS_WARNING) << "At most " << kMaxTemporalLayers << " temporal "
542 << "layers are supported.";
543 return true;
544 }
545
philipel02447bc2016-05-13 06:01:03 -0700546 // For every reference this frame has, check if there is a frame missing in
547 // the interval (|ref_pid|, |picture_id|) in any of the lower temporal
548 // layers. If so, we are missing a required frame.
philipelc9b27d52016-07-15 06:50:27 -0700549 uint8_t num_references = info.gof->num_ref_pics[gof_idx];
philipel02447bc2016-05-13 06:01:03 -0700550 for (size_t i = 0; i < num_references; ++i) {
551 uint16_t ref_pid =
philipelc9b27d52016-07-15 06:50:27 -0700552 Subtract<kPicIdLength>(picture_id, info.gof->pid_diff[gof_idx][i]);
philipel02447bc2016-05-13 06:01:03 -0700553 for (size_t l = 0; l < temporal_idx; ++l) {
554 auto missing_frame_it = missing_frames_for_layer_[l].lower_bound(ref_pid);
555 if (missing_frame_it != missing_frames_for_layer_[l].end() &&
556 AheadOf<uint16_t, kPicIdLength>(picture_id, *missing_frame_it)) {
557 return true;
558 }
559 }
560 }
561 return false;
562}
563
564void RtpFrameReferenceFinder::FrameReceivedVp9(uint16_t picture_id,
philipelc9b27d52016-07-15 06:50:27 -0700565 GofInfo* info) {
566 int last_picture_id = info->last_picture_id;
philipel459f4e32018-03-02 10:55:12 +0100567 size_t gof_size = std::min(info->gof->num_frames_in_gof, kMaxVp9FramesInGof);
philipel02447bc2016-05-13 06:01:03 -0700568
569 // If there is a gap, find which temporal layer the missing frames
570 // belong to and add the frame as missing for that temporal layer.
571 // Otherwise, remove this frame from the set of missing frames.
philipelc9b27d52016-07-15 06:50:27 -0700572 if (AheadOf<uint16_t, kPicIdLength>(picture_id, last_picture_id)) {
573 size_t diff = ForwardDiff<uint16_t, kPicIdLength>(info->gof->pid_start,
574 last_picture_id);
philipel459f4e32018-03-02 10:55:12 +0100575 size_t gof_idx = diff % gof_size;
philipel02447bc2016-05-13 06:01:03 -0700576
philipelc9b27d52016-07-15 06:50:27 -0700577 last_picture_id = Add<kPicIdLength>(last_picture_id, 1);
578 while (last_picture_id != picture_id) {
Yves Gerey665174f2018-06-19 15:03:05 +0200579 gof_idx = (gof_idx + 1) % gof_size;
philipel459f4e32018-03-02 10:55:12 +0100580 RTC_CHECK(gof_idx < kMaxVp9FramesInGof);
581
philipelc9b27d52016-07-15 06:50:27 -0700582 size_t temporal_idx = info->gof->temporal_idx[gof_idx];
philipel459f4e32018-03-02 10:55:12 +0100583 if (temporal_idx >= kMaxTemporalLayers) {
584 RTC_LOG(LS_WARNING) << "At most " << kMaxTemporalLayers << " temporal "
585 << "layers are supported.";
586 return;
587 }
588
philipelc9b27d52016-07-15 06:50:27 -0700589 missing_frames_for_layer_[temporal_idx].insert(last_picture_id);
590 last_picture_id = Add<kPicIdLength>(last_picture_id, 1);
philipel02447bc2016-05-13 06:01:03 -0700591 }
philipel459f4e32018-03-02 10:55:12 +0100592
philipelc9b27d52016-07-15 06:50:27 -0700593 info->last_picture_id = last_picture_id;
philipel02447bc2016-05-13 06:01:03 -0700594 } else {
595 size_t diff =
philipelc9b27d52016-07-15 06:50:27 -0700596 ForwardDiff<uint16_t, kPicIdLength>(info->gof->pid_start, picture_id);
philipel459f4e32018-03-02 10:55:12 +0100597 size_t gof_idx = diff % gof_size;
598 RTC_CHECK(gof_idx < kMaxVp9FramesInGof);
599
philipelc9b27d52016-07-15 06:50:27 -0700600 size_t temporal_idx = info->gof->temporal_idx[gof_idx];
philipel459f4e32018-03-02 10:55:12 +0100601 if (temporal_idx >= kMaxTemporalLayers) {
602 RTC_LOG(LS_WARNING) << "At most " << kMaxTemporalLayers << " temporal "
603 << "layers are supported.";
604 return;
605 }
606
philipel02447bc2016-05-13 06:01:03 -0700607 missing_frames_for_layer_[temporal_idx].erase(picture_id);
608 }
609}
610
611bool RtpFrameReferenceFinder::UpSwitchInIntervalVp9(uint16_t picture_id,
612 uint8_t temporal_idx,
613 uint16_t pid_ref) {
614 for (auto up_switch_it = up_switch_.upper_bound(pid_ref);
615 up_switch_it != up_switch_.end() &&
616 AheadOf<uint16_t, kPicIdLength>(picture_id, up_switch_it->first);
617 ++up_switch_it) {
618 if (up_switch_it->second < temporal_idx)
619 return true;
620 }
621
622 return false;
623}
624
philipelafcf7f52017-04-26 08:17:35 -0700625void RtpFrameReferenceFinder::UnwrapPictureIds(RtpFrameObject* frame) {
philipel02447bc2016-05-13 06:01:03 -0700626 for (size_t i = 0; i < frame->num_references; ++i)
philipeld4fac692017-09-04 07:03:46 -0700627 frame->references[i] = unwrapper_.Unwrap(frame->references[i]);
philipel0fa82a62018-03-19 15:34:53 +0100628 frame->id.picture_id = unwrapper_.Unwrap(frame->id.picture_id);
philipel02447bc2016-05-13 06:01:03 -0700629}
630
philipel02447bc2016-05-13 06:01:03 -0700631} // namespace video_coding
632} // namespace webrtc