blob: 12517885a43b4314a91468ff6e2a463c1bc334a9 [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);
Sami Kalliomäki98824952018-08-28 14:39:21 +020091 default: {
92 // Use 15 first bits of frame ID as picture ID if available.
93 absl::optional<RTPVideoHeader> video_header = frame->GetCodecHeader();
94 absl::optional<RTPVideoHeader::GenericDescriptorInfo> generic_info =
95 video_header ? video_header->generic : absl::nullopt;
96 return ManageFrameGeneric(
97 frame, generic_info ? generic_info->frame_id & 0x7fff : kNoPictureId);
98 }
philipel02447bc2016-05-13 06:01:03 -070099 }
100}
101
philipel9b2ce6b2016-07-05 05:04:46 -0700102void RtpFrameReferenceFinder::PaddingReceived(uint16_t seq_num) {
103 rtc::CritScope lock(&crit_);
104 auto clean_padding_to =
105 stashed_padding_.lower_bound(seq_num - kMaxPaddingAge);
106 stashed_padding_.erase(stashed_padding_.begin(), clean_padding_to);
107 stashed_padding_.insert(seq_num);
108 UpdateLastPictureIdWithPadding(seq_num);
109 RetryStashedFrames();
110}
111
philipel463d3012016-09-09 03:32:44 -0700112void RtpFrameReferenceFinder::ClearTo(uint16_t seq_num) {
113 rtc::CritScope lock(&crit_);
114 cleared_to_seq_num_ = seq_num;
115
116 auto it = stashed_frames_.begin();
117 while (it != stashed_frames_.end()) {
118 if (AheadOf<uint16_t>(cleared_to_seq_num_, (*it)->first_seq_num())) {
119 it = stashed_frames_.erase(it);
120 } else {
121 ++it;
122 }
123 }
124}
125
philipel9b2ce6b2016-07-05 05:04:46 -0700126void RtpFrameReferenceFinder::UpdateLastPictureIdWithPadding(uint16_t seq_num) {
127 auto gop_seq_num_it = last_seq_num_gop_.upper_bound(seq_num);
128
129 // If this padding packet "belongs" to a group of pictures that we don't track
130 // anymore, do nothing.
131 if (gop_seq_num_it == last_seq_num_gop_.begin())
132 return;
133 --gop_seq_num_it;
134
135 // Calculate the next contiuous sequence number and search for it in
136 // the padding packets we have stashed.
137 uint16_t next_seq_num_with_padding = gop_seq_num_it->second.second + 1;
138 auto padding_seq_num_it =
139 stashed_padding_.lower_bound(next_seq_num_with_padding);
140
141 // While there still are padding packets and those padding packets are
142 // continuous, then advance the "last-picture-id-with-padding" and remove
143 // the stashed padding packet.
144 while (padding_seq_num_it != stashed_padding_.end() &&
145 *padding_seq_num_it == next_seq_num_with_padding) {
146 gop_seq_num_it->second.second = next_seq_num_with_padding;
147 ++next_seq_num_with_padding;
148 padding_seq_num_it = stashed_padding_.erase(padding_seq_num_it);
149 }
philipel41bb7922017-02-20 07:53:23 -0800150
151 // In the case where the stream has been continuous without any new keyframes
152 // for a while there is a risk that new frames will appear to be older than
153 // the keyframe they belong to due to wrapping sequence number. In order
154 // to prevent this we advance the picture id of the keyframe every so often.
155 if (ForwardDiff(gop_seq_num_it->first, seq_num) > 10000) {
156 RTC_DCHECK_EQ(1ul, last_seq_num_gop_.size());
157 last_seq_num_gop_[seq_num] = gop_seq_num_it->second;
158 last_seq_num_gop_.erase(gop_seq_num_it);
159 }
philipel9b2ce6b2016-07-05 05:04:46 -0700160}
161
philipelafcf7f52017-04-26 08:17:35 -0700162RtpFrameReferenceFinder::FrameDecision
163RtpFrameReferenceFinder::ManageFrameGeneric(RtpFrameObject* frame,
164 int picture_id) {
philipel647998c2016-06-03 09:40:16 -0700165 // If |picture_id| is specified then we use that to set the frame references,
166 // otherwise we use sequence number.
167 if (picture_id != kNoPictureId) {
168 if (last_unwrap_ == -1)
169 last_unwrap_ = picture_id;
170
philipel0fa82a62018-03-19 15:34:53 +0100171 frame->id.picture_id = unwrapper_.Unwrap(picture_id);
philipel647998c2016-06-03 09:40:16 -0700172 frame->num_references = frame->frame_type() == kVideoFrameKey ? 0 : 1;
philipel0fa82a62018-03-19 15:34:53 +0100173 frame->references[0] = frame->id.picture_id - 1;
philipelafcf7f52017-04-26 08:17:35 -0700174 return kHandOff;
philipel647998c2016-06-03 09:40:16 -0700175 }
176
philipel9b2ce6b2016-07-05 05:04:46 -0700177 if (frame->frame_type() == kVideoFrameKey) {
178 last_seq_num_gop_.insert(std::make_pair(
179 frame->last_seq_num(),
180 std::make_pair(frame->last_seq_num(), frame->last_seq_num())));
181 }
philipel02447bc2016-05-13 06:01:03 -0700182
183 // We have received a frame but not yet a keyframe, stash this frame.
philipelafcf7f52017-04-26 08:17:35 -0700184 if (last_seq_num_gop_.empty())
185 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700186
187 // Clean up info for old keyframes but make sure to keep info
188 // for the last keyframe.
189 auto clean_to = last_seq_num_gop_.lower_bound(frame->last_seq_num() - 100);
philipel41bb7922017-02-20 07:53:23 -0800190 for (auto it = last_seq_num_gop_.begin();
191 it != clean_to && last_seq_num_gop_.size() > 1;) {
192 it = last_seq_num_gop_.erase(it);
193 }
philipel02447bc2016-05-13 06:01:03 -0700194
195 // Find the last sequence number of the last frame for the keyframe
196 // that this frame indirectly references.
197 auto seq_num_it = last_seq_num_gop_.upper_bound(frame->last_seq_num());
philipel9b2ce6b2016-07-05 05:04:46 -0700198 if (seq_num_it == last_seq_num_gop_.begin()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100199 RTC_LOG(LS_WARNING) << "Generic frame with packet range ["
200 << frame->first_seq_num() << ", "
201 << frame->last_seq_num()
202 << "] has no GoP, dropping frame.";
philipelafcf7f52017-04-26 08:17:35 -0700203 return kDrop;
philipel9b2ce6b2016-07-05 05:04:46 -0700204 }
philipel02447bc2016-05-13 06:01:03 -0700205 seq_num_it--;
206
207 // Make sure the packet sequence numbers are continuous, otherwise stash
208 // this frame.
philipel9b2ce6b2016-07-05 05:04:46 -0700209 uint16_t last_picture_id_gop = seq_num_it->second.first;
210 uint16_t last_picture_id_with_padding_gop = seq_num_it->second.second;
philipel02447bc2016-05-13 06:01:03 -0700211 if (frame->frame_type() == kVideoFrameDelta) {
philipel9b2ce6b2016-07-05 05:04:46 -0700212 uint16_t prev_seq_num = frame->first_seq_num() - 1;
philipelafcf7f52017-04-26 08:17:35 -0700213
214 if (prev_seq_num != last_picture_id_with_padding_gop)
215 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700216 }
217
218 RTC_DCHECK(AheadOrAt(frame->last_seq_num(), seq_num_it->first));
219
220 // Since keyframes can cause reordering we can't simply assign the
221 // picture id according to some incrementing counter.
philipel0fa82a62018-03-19 15:34:53 +0100222 frame->id.picture_id = frame->last_seq_num();
philipel02447bc2016-05-13 06:01:03 -0700223 frame->num_references = frame->frame_type() == kVideoFrameDelta;
philipeld4fac692017-09-04 07:03:46 -0700224 frame->references[0] = generic_unwrapper_.Unwrap(last_picture_id_gop);
philipel0fa82a62018-03-19 15:34:53 +0100225 if (AheadOf<uint16_t>(frame->id.picture_id, last_picture_id_gop)) {
226 seq_num_it->second.first = frame->id.picture_id;
227 seq_num_it->second.second = frame->id.picture_id;
philipel9b2ce6b2016-07-05 05:04:46 -0700228 }
philipel02447bc2016-05-13 06:01:03 -0700229
philipel0fa82a62018-03-19 15:34:53 +0100230 last_picture_id_ = frame->id.picture_id;
231 UpdateLastPictureIdWithPadding(frame->id.picture_id);
232 frame->id.picture_id = generic_unwrapper_.Unwrap(frame->id.picture_id);
philipelafcf7f52017-04-26 08:17:35 -0700233 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700234}
235
philipelafcf7f52017-04-26 08:17:35 -0700236RtpFrameReferenceFinder::FrameDecision RtpFrameReferenceFinder::ManageFrameVp8(
237 RtpFrameObject* frame) {
Sami Kalliomäki98824952018-08-28 14:39:21 +0200238 absl::optional<RTPVideoHeader> video_header = frame->GetCodecHeader();
239 if (!video_header) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100240 RTC_LOG(LS_WARNING)
241 << "Failed to get codec header from frame, dropping frame.";
philipelafcf7f52017-04-26 08:17:35 -0700242 return kDrop;
philipeld4fac692017-09-04 07:03:46 -0700243 }
Sami Kalliomäki98824952018-08-28 14:39:21 +0200244 RTPVideoTypeHeader rtp_codec_header = video_header->video_type_header;
philipel02447bc2016-05-13 06:01:03 -0700245
philipel1a4746a2018-07-09 15:52:29 +0200246 const RTPVideoHeaderVP8& codec_header =
Sami Kalliomäki98824952018-08-28 14:39:21 +0200247 absl::get<RTPVideoHeaderVP8>(rtp_codec_header);
philipel02447bc2016-05-13 06:01:03 -0700248
249 if (codec_header.pictureId == kNoPictureId ||
250 codec_header.temporalIdx == kNoTemporalIdx ||
251 codec_header.tl0PicIdx == kNoTl0PicIdx) {
philipelafcf7f52017-04-26 08:17:35 -0700252 return ManageFrameGeneric(std::move(frame), codec_header.pictureId);
philipel02447bc2016-05-13 06:01:03 -0700253 }
254
philipel0fa82a62018-03-19 15:34:53 +0100255 frame->id.picture_id = codec_header.pictureId % kPicIdLength;
philipel02447bc2016-05-13 06:01:03 -0700256
257 if (last_unwrap_ == -1)
258 last_unwrap_ = codec_header.pictureId;
259
260 if (last_picture_id_ == -1)
philipel0fa82a62018-03-19 15:34:53 +0100261 last_picture_id_ = frame->id.picture_id;
philipel02447bc2016-05-13 06:01:03 -0700262
263 // Find if there has been a gap in fully received frames and save the picture
264 // id of those frames in |not_yet_received_frames_|.
philipel0fa82a62018-03-19 15:34:53 +0100265 if (AheadOf<uint16_t, kPicIdLength>(frame->id.picture_id, last_picture_id_)) {
philipel9bd1d662017-07-14 04:52:01 -0700266 do {
philipel02447bc2016-05-13 06:01:03 -0700267 last_picture_id_ = Add<kPicIdLength>(last_picture_id_, 1);
philipel9bd1d662017-07-14 04:52:01 -0700268 not_yet_received_frames_.insert(last_picture_id_);
philipel0fa82a62018-03-19 15:34:53 +0100269 } while (last_picture_id_ != frame->id.picture_id);
philipel02447bc2016-05-13 06:01:03 -0700270 }
271
philipel57ec6852018-07-03 18:09:32 +0200272 int64_t unwrapped_tl0 = tl0_unwrapper_.Unwrap(codec_header.tl0PicIdx);
273
philipel02447bc2016-05-13 06:01:03 -0700274 // Clean up info for base layers that are too old.
philipel57ec6852018-07-03 18:09:32 +0200275 int64_t old_tl0_pic_idx = unwrapped_tl0 - kMaxLayerInfo;
philipel02447bc2016-05-13 06:01:03 -0700276 auto clean_layer_info_to = layer_info_.lower_bound(old_tl0_pic_idx);
277 layer_info_.erase(layer_info_.begin(), clean_layer_info_to);
278
279 // Clean up info about not yet received frames that are too old.
280 uint16_t old_picture_id =
philipel0fa82a62018-03-19 15:34:53 +0100281 Subtract<kPicIdLength>(frame->id.picture_id, kMaxNotYetReceivedFrames);
philipel02447bc2016-05-13 06:01:03 -0700282 auto clean_frames_to = not_yet_received_frames_.lower_bound(old_picture_id);
283 not_yet_received_frames_.erase(not_yet_received_frames_.begin(),
284 clean_frames_to);
285
286 if (frame->frame_type() == kVideoFrameKey) {
287 frame->num_references = 0;
philipel57ec6852018-07-03 18:09:32 +0200288 layer_info_[unwrapped_tl0].fill(-1);
289 UpdateLayerInfoVp8(frame, unwrapped_tl0, codec_header.temporalIdx);
philipelafcf7f52017-04-26 08:17:35 -0700290 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700291 }
292
philipel57ec6852018-07-03 18:09:32 +0200293 auto layer_info_it = layer_info_.find(
294 codec_header.temporalIdx == 0 ? unwrapped_tl0 - 1 : unwrapped_tl0);
philipel02447bc2016-05-13 06:01:03 -0700295
296 // If we don't have the base layer frame yet, stash this frame.
philipelafcf7f52017-04-26 08:17:35 -0700297 if (layer_info_it == layer_info_.end())
298 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700299
300 // A non keyframe base layer frame has been received, copy the layer info
301 // from the previous base layer frame and set a reference to the previous
302 // base layer frame.
303 if (codec_header.temporalIdx == 0) {
304 layer_info_it =
philipel57ec6852018-07-03 18:09:32 +0200305 layer_info_.emplace(unwrapped_tl0, layer_info_it->second).first;
philipel02447bc2016-05-13 06:01:03 -0700306 frame->num_references = 1;
307 frame->references[0] = layer_info_it->second[0];
philipel57ec6852018-07-03 18:09:32 +0200308 UpdateLayerInfoVp8(frame, unwrapped_tl0, codec_header.temporalIdx);
philipelafcf7f52017-04-26 08:17:35 -0700309 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700310 }
311
312 // Layer sync frame, this frame only references its base layer frame.
313 if (codec_header.layerSync) {
314 frame->num_references = 1;
315 frame->references[0] = layer_info_it->second[0];
316
philipel57ec6852018-07-03 18:09:32 +0200317 UpdateLayerInfoVp8(frame, unwrapped_tl0, codec_header.temporalIdx);
philipelafcf7f52017-04-26 08:17:35 -0700318 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700319 }
320
321 // Find all references for this frame.
322 frame->num_references = 0;
323 for (uint8_t layer = 0; layer <= codec_header.temporalIdx; ++layer) {
philipeld268d6f2016-09-15 13:43:13 +0200324 // If we have not yet received a previous frame on this temporal layer,
325 // stash this frame.
philipelafcf7f52017-04-26 08:17:35 -0700326 if (layer_info_it->second[layer] == -1)
327 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700328
philipel86b92e02016-10-24 07:11:53 -0700329 // If the last frame on this layer is ahead of this frame it means that
330 // a layer sync frame has been received after this frame for the same
331 // base layer frame, drop this frame.
332 if (AheadOf<uint16_t, kPicIdLength>(layer_info_it->second[layer],
philipel0fa82a62018-03-19 15:34:53 +0100333 frame->id.picture_id)) {
philipelafcf7f52017-04-26 08:17:35 -0700334 return kDrop;
philipel86b92e02016-10-24 07:11:53 -0700335 }
336
philipel02447bc2016-05-13 06:01:03 -0700337 // If we have not yet received a frame between this frame and the referenced
338 // frame then we have to wait for that frame to be completed first.
339 auto not_received_frame_it =
340 not_yet_received_frames_.upper_bound(layer_info_it->second[layer]);
341 if (not_received_frame_it != not_yet_received_frames_.end() &&
philipel0fa82a62018-03-19 15:34:53 +0100342 AheadOf<uint16_t, kPicIdLength>(frame->id.picture_id,
philipel02447bc2016-05-13 06:01:03 -0700343 *not_received_frame_it)) {
philipelafcf7f52017-04-26 08:17:35 -0700344 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700345 }
346
philipel0fa82a62018-03-19 15:34:53 +0100347 if (!(AheadOf<uint16_t, kPicIdLength>(frame->id.picture_id,
philipel57f19cc2017-03-07 03:54:05 -0800348 layer_info_it->second[layer]))) {
philipel0fa82a62018-03-19 15:34:53 +0100349 RTC_LOG(LS_WARNING) << "Frame with picture id " << frame->id.picture_id
Mirko Bonadei675513b2017-11-09 11:09:25 +0100350 << " and packet range [" << frame->first_seq_num()
351 << ", " << frame->last_seq_num()
352 << "] already received, "
353 << " dropping frame.";
philipelafcf7f52017-04-26 08:17:35 -0700354 return kDrop;
philipel57f19cc2017-03-07 03:54:05 -0800355 }
356
philipel02447bc2016-05-13 06:01:03 -0700357 ++frame->num_references;
358 frame->references[layer] = layer_info_it->second[layer];
359 }
360
philipel57ec6852018-07-03 18:09:32 +0200361 UpdateLayerInfoVp8(frame, unwrapped_tl0, codec_header.temporalIdx);
philipelafcf7f52017-04-26 08:17:35 -0700362 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700363}
364
philipel57ec6852018-07-03 18:09:32 +0200365void RtpFrameReferenceFinder::UpdateLayerInfoVp8(RtpFrameObject* frame,
366 int64_t unwrapped_tl0,
367 uint8_t temporal_idx) {
368 auto layer_info_it = layer_info_.find(unwrapped_tl0);
philipel02447bc2016-05-13 06:01:03 -0700369
370 // Update this layer info and newer.
371 while (layer_info_it != layer_info_.end()) {
philipel57ec6852018-07-03 18:09:32 +0200372 if (layer_info_it->second[temporal_idx] != -1 &&
373 AheadOf<uint16_t, kPicIdLength>(layer_info_it->second[temporal_idx],
philipel0fa82a62018-03-19 15:34:53 +0100374 frame->id.picture_id)) {
philipel02447bc2016-05-13 06:01:03 -0700375 // The frame was not newer, then no subsequent layer info have to be
376 // update.
377 break;
378 }
379
philipel57ec6852018-07-03 18:09:32 +0200380 layer_info_it->second[temporal_idx] = frame->id.picture_id;
381 ++unwrapped_tl0;
382 layer_info_it = layer_info_.find(unwrapped_tl0);
philipel02447bc2016-05-13 06:01:03 -0700383 }
philipel0fa82a62018-03-19 15:34:53 +0100384 not_yet_received_frames_.erase(frame->id.picture_id);
philipel02447bc2016-05-13 06:01:03 -0700385
philipelafcf7f52017-04-26 08:17:35 -0700386 UnwrapPictureIds(frame);
philipel02447bc2016-05-13 06:01:03 -0700387}
388
philipelafcf7f52017-04-26 08:17:35 -0700389RtpFrameReferenceFinder::FrameDecision RtpFrameReferenceFinder::ManageFrameVp9(
390 RtpFrameObject* frame) {
Sami Kalliomäki98824952018-08-28 14:39:21 +0200391 absl::optional<RTPVideoHeader> video_header = frame->GetCodecHeader();
392 if (!video_header) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100393 RTC_LOG(LS_WARNING)
394 << "Failed to get codec header from frame, dropping frame.";
philipel4c140092017-08-31 08:31:45 -0700395 return kDrop;
philipeld4fac692017-09-04 07:03:46 -0700396 }
Sami Kalliomäki98824952018-08-28 14:39:21 +0200397 RTPVideoTypeHeader rtp_codec_header = video_header->video_type_header;
philipel4c140092017-08-31 08:31:45 -0700398
philipel1a4746a2018-07-09 15:52:29 +0200399 const RTPVideoHeaderVP9& codec_header =
Sami Kalliomäki98824952018-08-28 14:39:21 +0200400 absl::get<RTPVideoHeaderVP9>(rtp_codec_header);
philipel02447bc2016-05-13 06:01:03 -0700401
philipel647998c2016-06-03 09:40:16 -0700402 if (codec_header.picture_id == kNoPictureId ||
Sergey Silkind34a1882018-08-20 16:46:05 +0200403 codec_header.temporal_idx == kNoTemporalIdx) {
philipelafcf7f52017-04-26 08:17:35 -0700404 return ManageFrameGeneric(std::move(frame), codec_header.picture_id);
philipel02447bc2016-05-13 06:01:03 -0700405 }
406
philipel0fa82a62018-03-19 15:34:53 +0100407 frame->id.spatial_layer = codec_header.spatial_idx;
philipel02447bc2016-05-13 06:01:03 -0700408 frame->inter_layer_predicted = codec_header.inter_layer_predicted;
philipel0fa82a62018-03-19 15:34:53 +0100409 frame->id.picture_id = codec_header.picture_id % kPicIdLength;
philipel02447bc2016-05-13 06:01:03 -0700410
411 if (last_unwrap_ == -1)
412 last_unwrap_ = codec_header.picture_id;
413
414 if (last_picture_id_ == -1)
philipel0fa82a62018-03-19 15:34:53 +0100415 last_picture_id_ = frame->id.picture_id;
philipel02447bc2016-05-13 06:01:03 -0700416
417 if (codec_header.flexible_mode) {
418 frame->num_references = codec_header.num_ref_pics;
419 for (size_t i = 0; i < frame->num_references; ++i) {
philipel0fa82a62018-03-19 15:34:53 +0100420 frame->references[i] = Subtract<kPicIdLength>(frame->id.picture_id,
421 codec_header.pid_diff[i]);
philipel02447bc2016-05-13 06:01:03 -0700422 }
423
philipelafcf7f52017-04-26 08:17:35 -0700424 UnwrapPictureIds(frame);
425 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700426 }
427
Sergey Silkind34a1882018-08-20 16:46:05 +0200428 if (codec_header.tl0_pic_idx == kNoTl0PicIdx) {
429 RTC_LOG(LS_WARNING) << "TL0PICIDX is expected to be present in "
430 "non-flexible mode.";
431 return kDrop;
432 }
433
philipel15643602018-05-03 16:14:13 +0200434 GofInfo* info;
philipel57ec6852018-07-03 18:09:32 +0200435 int64_t unwrapped_tl0 = tl0_unwrapper_.Unwrap(codec_header.tl0_pic_idx);
philipel02447bc2016-05-13 06:01:03 -0700436 if (codec_header.ss_data_available) {
philipel02447bc2016-05-13 06:01:03 -0700437 if (codec_header.temporal_idx != 0) {
philipel15643602018-05-03 16:14:13 +0200438 RTC_LOG(LS_WARNING) << "Received scalability structure on a non base "
439 "layer frame. Scalability structure ignored.";
philipel02447bc2016-05-13 06:01:03 -0700440 } else {
441 current_ss_idx_ = Add<kMaxGofSaved>(current_ss_idx_, 1);
Natalie Silvanovich3ea3e302018-05-16 11:03:12 -0700442 if (codec_header.gof.num_frames_in_gof == 0 ||
443 codec_header.gof.num_frames_in_gof > kMaxVp9FramesInGof) {
444 return kDrop;
445 }
446
philipel02447bc2016-05-13 06:01:03 -0700447 scalability_structures_[current_ss_idx_] = codec_header.gof;
philipel0fa82a62018-03-19 15:34:53 +0100448 scalability_structures_[current_ss_idx_].pid_start = frame->id.picture_id;
philipel57ec6852018-07-03 18:09:32 +0200449 gof_info_.emplace(unwrapped_tl0,
philipel15643602018-05-03 16:14:13 +0200450 GofInfo(&scalability_structures_[current_ss_idx_],
451 frame->id.picture_id));
philipel02447bc2016-05-13 06:01:03 -0700452 }
philipel15643602018-05-03 16:14:13 +0200453
philipel57ec6852018-07-03 18:09:32 +0200454 const auto gof_info_it = gof_info_.find(unwrapped_tl0);
philipel15643602018-05-03 16:14:13 +0200455 if (gof_info_it == gof_info_.end())
456 return kStash;
457
458 info = &gof_info_it->second;
459
460 if (frame->frame_type() == kVideoFrameKey) {
461 frame->num_references = 0;
462 FrameReceivedVp9(frame->id.picture_id, info);
463 UnwrapPictureIds(frame);
464 return kHandOff;
465 }
466 } else {
467 if (frame->frame_type() == kVideoFrameKey) {
468 RTC_LOG(LS_WARNING) << "Received keyframe without scalability structure";
469 return kDrop;
470 }
471
philipel57ec6852018-07-03 18:09:32 +0200472 auto gof_info_it = gof_info_.find(
473 (codec_header.temporal_idx == 0) ? unwrapped_tl0 - 1 : unwrapped_tl0);
philipel15643602018-05-03 16:14:13 +0200474
475 // Gof info for this frame is not available yet, stash this frame.
476 if (gof_info_it == gof_info_.end())
477 return kStash;
478
479 if (codec_header.temporal_idx == 0) {
philipel57ec6852018-07-03 18:09:32 +0200480 gof_info_it = gof_info_
481 .emplace(unwrapped_tl0, GofInfo(gof_info_it->second.gof,
482 frame->id.picture_id))
483 .first;
philipel15643602018-05-03 16:14:13 +0200484 }
485
486 info = &gof_info_it->second;
philipel02447bc2016-05-13 06:01:03 -0700487 }
488
489 // Clean up info for base layers that are too old.
philipel57ec6852018-07-03 18:09:32 +0200490 int64_t old_tl0_pic_idx = unwrapped_tl0 - kMaxGofSaved;
philipel02447bc2016-05-13 06:01:03 -0700491 auto clean_gof_info_to = gof_info_.lower_bound(old_tl0_pic_idx);
492 gof_info_.erase(gof_info_.begin(), clean_gof_info_to);
493
philipel0fa82a62018-03-19 15:34:53 +0100494 FrameReceivedVp9(frame->id.picture_id, info);
philipel02447bc2016-05-13 06:01:03 -0700495
496 // Make sure we don't miss any frame that could potentially have the
497 // up switch flag set.
philipel0fa82a62018-03-19 15:34:53 +0100498 if (MissingRequiredFrameVp9(frame->id.picture_id, *info))
philipelafcf7f52017-04-26 08:17:35 -0700499 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700500
philipel15643602018-05-03 16:14:13 +0200501 if (codec_header.temporal_up_switch)
502 up_switch_.emplace(frame->id.picture_id, codec_header.temporal_idx);
philipel02447bc2016-05-13 06:01:03 -0700503
504 // Clean out old info about up switch frames.
philipel0fa82a62018-03-19 15:34:53 +0100505 uint16_t old_picture_id = Subtract<kPicIdLength>(frame->id.picture_id, 50);
philipel02447bc2016-05-13 06:01:03 -0700506 auto up_switch_erase_to = up_switch_.lower_bound(old_picture_id);
507 up_switch_.erase(up_switch_.begin(), up_switch_erase_to);
508
philipelc9b27d52016-07-15 06:50:27 -0700509 size_t diff = ForwardDiff<uint16_t, kPicIdLength>(info->gof->pid_start,
philipel0fa82a62018-03-19 15:34:53 +0100510 frame->id.picture_id);
philipelc9b27d52016-07-15 06:50:27 -0700511 size_t gof_idx = diff % info->gof->num_frames_in_gof;
philipel02447bc2016-05-13 06:01:03 -0700512
513 // Populate references according to the scalability structure.
philipelc9b27d52016-07-15 06:50:27 -0700514 frame->num_references = info->gof->num_ref_pics[gof_idx];
philipel02447bc2016-05-13 06:01:03 -0700515 for (size_t i = 0; i < frame->num_references; ++i) {
philipelc9b27d52016-07-15 06:50:27 -0700516 frame->references[i] = Subtract<kPicIdLength>(
philipel0fa82a62018-03-19 15:34:53 +0100517 frame->id.picture_id, info->gof->pid_diff[gof_idx][i]);
philipel02447bc2016-05-13 06:01:03 -0700518
519 // If this is a reference to a frame earlier than the last up switch point,
520 // then ignore this reference.
philipel0fa82a62018-03-19 15:34:53 +0100521 if (UpSwitchInIntervalVp9(frame->id.picture_id, codec_header.temporal_idx,
philipel02447bc2016-05-13 06:01:03 -0700522 frame->references[i])) {
523 --frame->num_references;
524 }
525 }
526
philipelafcf7f52017-04-26 08:17:35 -0700527 UnwrapPictureIds(frame);
528 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700529}
530
531bool RtpFrameReferenceFinder::MissingRequiredFrameVp9(uint16_t picture_id,
philipelc9b27d52016-07-15 06:50:27 -0700532 const GofInfo& info) {
533 size_t diff =
534 ForwardDiff<uint16_t, kPicIdLength>(info.gof->pid_start, picture_id);
535 size_t gof_idx = diff % info.gof->num_frames_in_gof;
536 size_t temporal_idx = info.gof->temporal_idx[gof_idx];
philipel02447bc2016-05-13 06:01:03 -0700537
philipela157e082018-05-02 15:19:01 +0200538 if (temporal_idx >= kMaxTemporalLayers) {
539 RTC_LOG(LS_WARNING) << "At most " << kMaxTemporalLayers << " temporal "
540 << "layers are supported.";
541 return true;
542 }
543
philipel02447bc2016-05-13 06:01:03 -0700544 // For every reference this frame has, check if there is a frame missing in
545 // the interval (|ref_pid|, |picture_id|) in any of the lower temporal
546 // layers. If so, we are missing a required frame.
philipelc9b27d52016-07-15 06:50:27 -0700547 uint8_t num_references = info.gof->num_ref_pics[gof_idx];
philipel02447bc2016-05-13 06:01:03 -0700548 for (size_t i = 0; i < num_references; ++i) {
549 uint16_t ref_pid =
philipelc9b27d52016-07-15 06:50:27 -0700550 Subtract<kPicIdLength>(picture_id, info.gof->pid_diff[gof_idx][i]);
philipel02447bc2016-05-13 06:01:03 -0700551 for (size_t l = 0; l < temporal_idx; ++l) {
552 auto missing_frame_it = missing_frames_for_layer_[l].lower_bound(ref_pid);
553 if (missing_frame_it != missing_frames_for_layer_[l].end() &&
554 AheadOf<uint16_t, kPicIdLength>(picture_id, *missing_frame_it)) {
555 return true;
556 }
557 }
558 }
559 return false;
560}
561
562void RtpFrameReferenceFinder::FrameReceivedVp9(uint16_t picture_id,
philipelc9b27d52016-07-15 06:50:27 -0700563 GofInfo* info) {
564 int last_picture_id = info->last_picture_id;
philipel459f4e32018-03-02 10:55:12 +0100565 size_t gof_size = std::min(info->gof->num_frames_in_gof, kMaxVp9FramesInGof);
philipel02447bc2016-05-13 06:01:03 -0700566
567 // If there is a gap, find which temporal layer the missing frames
568 // belong to and add the frame as missing for that temporal layer.
569 // Otherwise, remove this frame from the set of missing frames.
philipelc9b27d52016-07-15 06:50:27 -0700570 if (AheadOf<uint16_t, kPicIdLength>(picture_id, last_picture_id)) {
571 size_t diff = ForwardDiff<uint16_t, kPicIdLength>(info->gof->pid_start,
572 last_picture_id);
philipel459f4e32018-03-02 10:55:12 +0100573 size_t gof_idx = diff % gof_size;
philipel02447bc2016-05-13 06:01:03 -0700574
philipelc9b27d52016-07-15 06:50:27 -0700575 last_picture_id = Add<kPicIdLength>(last_picture_id, 1);
576 while (last_picture_id != picture_id) {
Yves Gerey665174f2018-06-19 15:03:05 +0200577 gof_idx = (gof_idx + 1) % gof_size;
philipel459f4e32018-03-02 10:55:12 +0100578 RTC_CHECK(gof_idx < kMaxVp9FramesInGof);
579
philipelc9b27d52016-07-15 06:50:27 -0700580 size_t temporal_idx = info->gof->temporal_idx[gof_idx];
philipel459f4e32018-03-02 10:55:12 +0100581 if (temporal_idx >= kMaxTemporalLayers) {
582 RTC_LOG(LS_WARNING) << "At most " << kMaxTemporalLayers << " temporal "
583 << "layers are supported.";
584 return;
585 }
586
philipelc9b27d52016-07-15 06:50:27 -0700587 missing_frames_for_layer_[temporal_idx].insert(last_picture_id);
588 last_picture_id = Add<kPicIdLength>(last_picture_id, 1);
philipel02447bc2016-05-13 06:01:03 -0700589 }
philipel459f4e32018-03-02 10:55:12 +0100590
philipelc9b27d52016-07-15 06:50:27 -0700591 info->last_picture_id = last_picture_id;
philipel02447bc2016-05-13 06:01:03 -0700592 } else {
593 size_t diff =
philipelc9b27d52016-07-15 06:50:27 -0700594 ForwardDiff<uint16_t, kPicIdLength>(info->gof->pid_start, picture_id);
philipel459f4e32018-03-02 10:55:12 +0100595 size_t gof_idx = diff % gof_size;
596 RTC_CHECK(gof_idx < kMaxVp9FramesInGof);
597
philipelc9b27d52016-07-15 06:50:27 -0700598 size_t temporal_idx = info->gof->temporal_idx[gof_idx];
philipel459f4e32018-03-02 10:55:12 +0100599 if (temporal_idx >= kMaxTemporalLayers) {
600 RTC_LOG(LS_WARNING) << "At most " << kMaxTemporalLayers << " temporal "
601 << "layers are supported.";
602 return;
603 }
604
philipel02447bc2016-05-13 06:01:03 -0700605 missing_frames_for_layer_[temporal_idx].erase(picture_id);
606 }
607}
608
609bool RtpFrameReferenceFinder::UpSwitchInIntervalVp9(uint16_t picture_id,
610 uint8_t temporal_idx,
611 uint16_t pid_ref) {
612 for (auto up_switch_it = up_switch_.upper_bound(pid_ref);
613 up_switch_it != up_switch_.end() &&
614 AheadOf<uint16_t, kPicIdLength>(picture_id, up_switch_it->first);
615 ++up_switch_it) {
616 if (up_switch_it->second < temporal_idx)
617 return true;
618 }
619
620 return false;
621}
622
philipelafcf7f52017-04-26 08:17:35 -0700623void RtpFrameReferenceFinder::UnwrapPictureIds(RtpFrameObject* frame) {
philipel02447bc2016-05-13 06:01:03 -0700624 for (size_t i = 0; i < frame->num_references; ++i)
philipeld4fac692017-09-04 07:03:46 -0700625 frame->references[i] = unwrapper_.Unwrap(frame->references[i]);
philipel0fa82a62018-03-19 15:34:53 +0100626 frame->id.picture_id = unwrapper_.Unwrap(frame->id.picture_id);
philipel02447bc2016-05-13 06:01:03 -0700627}
628
philipel02447bc2016-05-13 06:01:03 -0700629} // namespace video_coding
630} // namespace webrtc