blob: f6fce17215480640e328c43d5c8519d34913c3d9 [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),
philipel02447bc2016-05-13 06:01:03 -070029 current_ss_idx_(0),
philipel463d3012016-09-09 03:32:44 -070030 cleared_to_seq_num_(-1),
philipel02447bc2016-05-13 06:01:03 -070031 frame_callback_(frame_callback) {}
32
Mirko Bonadei8fdcac32018-08-28 16:30:18 +020033RtpFrameReferenceFinder::~RtpFrameReferenceFinder() = default;
34
philipel02447bc2016-05-13 06:01:03 -070035void RtpFrameReferenceFinder::ManageFrame(
36 std::unique_ptr<RtpFrameObject> frame) {
37 rtc::CritScope lock(&crit_);
philipel463d3012016-09-09 03:32:44 -070038
39 // If we have cleared past this frame, drop it.
40 if (cleared_to_seq_num_ != -1 &&
41 AheadOf<uint16_t>(cleared_to_seq_num_, frame->first_seq_num())) {
42 return;
43 }
44
philipelafcf7f52017-04-26 08:17:35 -070045 FrameDecision decision = ManageFrameInternal(frame.get());
46
47 switch (decision) {
48 case kStash:
49 if (stashed_frames_.size() > kMaxStashedFrames)
50 stashed_frames_.pop_back();
51 stashed_frames_.push_front(std::move(frame));
52 break;
53 case kHandOff:
54 frame_callback_->OnCompleteFrame(std::move(frame));
55 RetryStashedFrames();
56 break;
57 case kDrop:
58 break;
59 }
60}
61
62void RtpFrameReferenceFinder::RetryStashedFrames() {
63 bool complete_frame = false;
64 do {
65 complete_frame = false;
66 for (auto frame_it = stashed_frames_.begin();
67 frame_it != stashed_frames_.end();) {
68 FrameDecision decision = ManageFrameInternal(frame_it->get());
69
70 switch (decision) {
71 case kStash:
72 ++frame_it;
73 break;
74 case kHandOff:
75 complete_frame = true;
76 frame_callback_->OnCompleteFrame(std::move(*frame_it));
Karl Wiberg80ba3332018-02-05 10:33:35 +010077 RTC_FALLTHROUGH();
philipelafcf7f52017-04-26 08:17:35 -070078 case kDrop:
79 frame_it = stashed_frames_.erase(frame_it);
80 }
81 }
82 } while (complete_frame);
83}
84
85RtpFrameReferenceFinder::FrameDecision
86RtpFrameReferenceFinder::ManageFrameInternal(RtpFrameObject* frame) {
philipel2837edc2018-10-02 13:55:47 +020087 absl::optional<RtpGenericFrameDescriptor> generic_descriptor =
88 frame->GetGenericFrameDescriptor();
89 if (generic_descriptor) {
90 return ManageFrameGeneric(frame, *generic_descriptor);
philipeldabfcae2018-09-25 12:54:37 +020091 }
92
philipel02447bc2016-05-13 06:01:03 -070093 switch (frame->codec_type()) {
philipel02447bc2016-05-13 06:01:03 -070094 case kVideoCodecVP8:
philipelafcf7f52017-04-26 08:17:35 -070095 return ManageFrameVp8(frame);
philipel02447bc2016-05-13 06:01:03 -070096 case kVideoCodecVP9:
philipelafcf7f52017-04-26 08:17:35 -070097 return ManageFrameVp9(frame);
Sami Kalliomäki98824952018-08-28 14:39:21 +020098 default: {
99 // Use 15 first bits of frame ID as picture ID if available.
philipel2837edc2018-10-02 13:55:47 +0200100 absl::optional<RTPVideoHeader> video_header = frame->GetRtpVideoHeader();
philipeldabfcae2018-09-25 12:54:37 +0200101 int picture_id = kNoPictureId;
102 if (video_header && video_header->generic)
103 picture_id = video_header->generic->frame_id & 0x7fff;
104
105 return ManageFramePidOrSeqNum(frame, picture_id);
Sami Kalliomäki98824952018-08-28 14:39:21 +0200106 }
philipel02447bc2016-05-13 06:01:03 -0700107 }
108}
109
philipel9b2ce6b2016-07-05 05:04:46 -0700110void RtpFrameReferenceFinder::PaddingReceived(uint16_t seq_num) {
111 rtc::CritScope lock(&crit_);
112 auto clean_padding_to =
113 stashed_padding_.lower_bound(seq_num - kMaxPaddingAge);
114 stashed_padding_.erase(stashed_padding_.begin(), clean_padding_to);
115 stashed_padding_.insert(seq_num);
116 UpdateLastPictureIdWithPadding(seq_num);
117 RetryStashedFrames();
118}
119
philipel463d3012016-09-09 03:32:44 -0700120void RtpFrameReferenceFinder::ClearTo(uint16_t seq_num) {
121 rtc::CritScope lock(&crit_);
122 cleared_to_seq_num_ = seq_num;
123
124 auto it = stashed_frames_.begin();
125 while (it != stashed_frames_.end()) {
126 if (AheadOf<uint16_t>(cleared_to_seq_num_, (*it)->first_seq_num())) {
127 it = stashed_frames_.erase(it);
128 } else {
129 ++it;
130 }
131 }
132}
133
philipel9b2ce6b2016-07-05 05:04:46 -0700134void RtpFrameReferenceFinder::UpdateLastPictureIdWithPadding(uint16_t seq_num) {
135 auto gop_seq_num_it = last_seq_num_gop_.upper_bound(seq_num);
136
137 // If this padding packet "belongs" to a group of pictures that we don't track
138 // anymore, do nothing.
139 if (gop_seq_num_it == last_seq_num_gop_.begin())
140 return;
141 --gop_seq_num_it;
142
143 // Calculate the next contiuous sequence number and search for it in
144 // the padding packets we have stashed.
145 uint16_t next_seq_num_with_padding = gop_seq_num_it->second.second + 1;
146 auto padding_seq_num_it =
147 stashed_padding_.lower_bound(next_seq_num_with_padding);
148
149 // While there still are padding packets and those padding packets are
150 // continuous, then advance the "last-picture-id-with-padding" and remove
151 // the stashed padding packet.
152 while (padding_seq_num_it != stashed_padding_.end() &&
153 *padding_seq_num_it == next_seq_num_with_padding) {
154 gop_seq_num_it->second.second = next_seq_num_with_padding;
155 ++next_seq_num_with_padding;
156 padding_seq_num_it = stashed_padding_.erase(padding_seq_num_it);
157 }
philipel41bb7922017-02-20 07:53:23 -0800158
159 // In the case where the stream has been continuous without any new keyframes
160 // for a while there is a risk that new frames will appear to be older than
161 // the keyframe they belong to due to wrapping sequence number. In order
162 // to prevent this we advance the picture id of the keyframe every so often.
163 if (ForwardDiff(gop_seq_num_it->first, seq_num) > 10000) {
164 RTC_DCHECK_EQ(1ul, last_seq_num_gop_.size());
165 last_seq_num_gop_[seq_num] = gop_seq_num_it->second;
166 last_seq_num_gop_.erase(gop_seq_num_it);
167 }
philipel9b2ce6b2016-07-05 05:04:46 -0700168}
169
philipelafcf7f52017-04-26 08:17:35 -0700170RtpFrameReferenceFinder::FrameDecision
philipeldabfcae2018-09-25 12:54:37 +0200171RtpFrameReferenceFinder::ManageFrameGeneric(
172 RtpFrameObject* frame,
philipel2837edc2018-10-02 13:55:47 +0200173 const RtpGenericFrameDescriptor& descriptor) {
174 int64_t frame_id = generic_frame_id_unwrapper_.Unwrap(descriptor.FrameId());
175 frame->id.picture_id = frame_id;
176 frame->id.spatial_layer = descriptor.SpatialLayer();
177
178 rtc::ArrayView<const uint16_t> diffs = descriptor.FrameDependenciesDiffs();
179 if (EncodedFrame::kMaxFrameReferences < diffs.size()) {
philipeldabfcae2018-09-25 12:54:37 +0200180 RTC_LOG(LS_WARNING) << "Too many dependencies in generic descriptor.";
181 return kDrop;
182 }
183
philipel2837edc2018-10-02 13:55:47 +0200184 frame->num_references = diffs.size();
185 for (size_t i = 0; i < diffs.size(); ++i)
186 frame->references[i] = frame_id - diffs[i];
philipeldabfcae2018-09-25 12:54:37 +0200187
188 return kHandOff;
189}
190
191RtpFrameReferenceFinder::FrameDecision
192RtpFrameReferenceFinder::ManageFramePidOrSeqNum(RtpFrameObject* frame,
193 int picture_id) {
philipel647998c2016-06-03 09:40:16 -0700194 // If |picture_id| is specified then we use that to set the frame references,
195 // otherwise we use sequence number.
196 if (picture_id != kNoPictureId) {
philipel0fa82a62018-03-19 15:34:53 +0100197 frame->id.picture_id = unwrapper_.Unwrap(picture_id);
philipel647998c2016-06-03 09:40:16 -0700198 frame->num_references = frame->frame_type() == kVideoFrameKey ? 0 : 1;
philipel0fa82a62018-03-19 15:34:53 +0100199 frame->references[0] = frame->id.picture_id - 1;
philipelafcf7f52017-04-26 08:17:35 -0700200 return kHandOff;
philipel647998c2016-06-03 09:40:16 -0700201 }
202
philipel9b2ce6b2016-07-05 05:04:46 -0700203 if (frame->frame_type() == kVideoFrameKey) {
204 last_seq_num_gop_.insert(std::make_pair(
205 frame->last_seq_num(),
206 std::make_pair(frame->last_seq_num(), frame->last_seq_num())));
207 }
philipel02447bc2016-05-13 06:01:03 -0700208
209 // We have received a frame but not yet a keyframe, stash this frame.
philipelafcf7f52017-04-26 08:17:35 -0700210 if (last_seq_num_gop_.empty())
211 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700212
213 // Clean up info for old keyframes but make sure to keep info
214 // for the last keyframe.
215 auto clean_to = last_seq_num_gop_.lower_bound(frame->last_seq_num() - 100);
philipel41bb7922017-02-20 07:53:23 -0800216 for (auto it = last_seq_num_gop_.begin();
217 it != clean_to && last_seq_num_gop_.size() > 1;) {
218 it = last_seq_num_gop_.erase(it);
219 }
philipel02447bc2016-05-13 06:01:03 -0700220
221 // Find the last sequence number of the last frame for the keyframe
222 // that this frame indirectly references.
223 auto seq_num_it = last_seq_num_gop_.upper_bound(frame->last_seq_num());
philipel9b2ce6b2016-07-05 05:04:46 -0700224 if (seq_num_it == last_seq_num_gop_.begin()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100225 RTC_LOG(LS_WARNING) << "Generic frame with packet range ["
226 << frame->first_seq_num() << ", "
227 << frame->last_seq_num()
228 << "] has no GoP, dropping frame.";
philipelafcf7f52017-04-26 08:17:35 -0700229 return kDrop;
philipel9b2ce6b2016-07-05 05:04:46 -0700230 }
philipel02447bc2016-05-13 06:01:03 -0700231 seq_num_it--;
232
233 // Make sure the packet sequence numbers are continuous, otherwise stash
234 // this frame.
philipel9b2ce6b2016-07-05 05:04:46 -0700235 uint16_t last_picture_id_gop = seq_num_it->second.first;
236 uint16_t last_picture_id_with_padding_gop = seq_num_it->second.second;
philipel02447bc2016-05-13 06:01:03 -0700237 if (frame->frame_type() == kVideoFrameDelta) {
philipel9b2ce6b2016-07-05 05:04:46 -0700238 uint16_t prev_seq_num = frame->first_seq_num() - 1;
philipelafcf7f52017-04-26 08:17:35 -0700239
240 if (prev_seq_num != last_picture_id_with_padding_gop)
241 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700242 }
243
244 RTC_DCHECK(AheadOrAt(frame->last_seq_num(), seq_num_it->first));
245
246 // Since keyframes can cause reordering we can't simply assign the
247 // picture id according to some incrementing counter.
philipel0fa82a62018-03-19 15:34:53 +0100248 frame->id.picture_id = frame->last_seq_num();
philipel02447bc2016-05-13 06:01:03 -0700249 frame->num_references = frame->frame_type() == kVideoFrameDelta;
philipeldabfcae2018-09-25 12:54:37 +0200250 frame->references[0] = rtp_seq_num_unwrapper_.Unwrap(last_picture_id_gop);
philipel0fa82a62018-03-19 15:34:53 +0100251 if (AheadOf<uint16_t>(frame->id.picture_id, last_picture_id_gop)) {
252 seq_num_it->second.first = frame->id.picture_id;
253 seq_num_it->second.second = frame->id.picture_id;
philipel9b2ce6b2016-07-05 05:04:46 -0700254 }
philipel02447bc2016-05-13 06:01:03 -0700255
philipel0fa82a62018-03-19 15:34:53 +0100256 last_picture_id_ = frame->id.picture_id;
257 UpdateLastPictureIdWithPadding(frame->id.picture_id);
philipeldabfcae2018-09-25 12:54:37 +0200258 frame->id.picture_id = rtp_seq_num_unwrapper_.Unwrap(frame->id.picture_id);
philipelafcf7f52017-04-26 08:17:35 -0700259 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700260}
261
philipelafcf7f52017-04-26 08:17:35 -0700262RtpFrameReferenceFinder::FrameDecision RtpFrameReferenceFinder::ManageFrameVp8(
263 RtpFrameObject* frame) {
philipel5470f402018-09-07 13:38:53 +0200264 absl::optional<RTPVideoHeader> video_header = frame->GetRtpVideoHeader();
Sami Kalliomäki98824952018-08-28 14:39:21 +0200265 if (!video_header) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100266 RTC_LOG(LS_WARNING)
267 << "Failed to get codec header from frame, dropping frame.";
philipelafcf7f52017-04-26 08:17:35 -0700268 return kDrop;
philipeld4fac692017-09-04 07:03:46 -0700269 }
Sami Kalliomäki98824952018-08-28 14:39:21 +0200270 RTPVideoTypeHeader rtp_codec_header = video_header->video_type_header;
philipel02447bc2016-05-13 06:01:03 -0700271
philipel1a4746a2018-07-09 15:52:29 +0200272 const RTPVideoHeaderVP8& codec_header =
Sami Kalliomäki98824952018-08-28 14:39:21 +0200273 absl::get<RTPVideoHeaderVP8>(rtp_codec_header);
philipel02447bc2016-05-13 06:01:03 -0700274
275 if (codec_header.pictureId == kNoPictureId ||
276 codec_header.temporalIdx == kNoTemporalIdx ||
277 codec_header.tl0PicIdx == kNoTl0PicIdx) {
philipeldabfcae2018-09-25 12:54:37 +0200278 return ManageFramePidOrSeqNum(std::move(frame), codec_header.pictureId);
philipel02447bc2016-05-13 06:01:03 -0700279 }
280
philipel0fa82a62018-03-19 15:34:53 +0100281 frame->id.picture_id = codec_header.pictureId % kPicIdLength;
philipel02447bc2016-05-13 06:01:03 -0700282
philipel02447bc2016-05-13 06:01:03 -0700283 if (last_picture_id_ == -1)
philipel0fa82a62018-03-19 15:34:53 +0100284 last_picture_id_ = frame->id.picture_id;
philipel02447bc2016-05-13 06:01:03 -0700285
286 // Find if there has been a gap in fully received frames and save the picture
287 // id of those frames in |not_yet_received_frames_|.
philipel0fa82a62018-03-19 15:34:53 +0100288 if (AheadOf<uint16_t, kPicIdLength>(frame->id.picture_id, last_picture_id_)) {
philipel9bd1d662017-07-14 04:52:01 -0700289 do {
philipel02447bc2016-05-13 06:01:03 -0700290 last_picture_id_ = Add<kPicIdLength>(last_picture_id_, 1);
philipel9bd1d662017-07-14 04:52:01 -0700291 not_yet_received_frames_.insert(last_picture_id_);
philipel0fa82a62018-03-19 15:34:53 +0100292 } while (last_picture_id_ != frame->id.picture_id);
philipel02447bc2016-05-13 06:01:03 -0700293 }
294
philipel57ec6852018-07-03 18:09:32 +0200295 int64_t unwrapped_tl0 = tl0_unwrapper_.Unwrap(codec_header.tl0PicIdx);
296
philipel02447bc2016-05-13 06:01:03 -0700297 // Clean up info for base layers that are too old.
philipel57ec6852018-07-03 18:09:32 +0200298 int64_t old_tl0_pic_idx = unwrapped_tl0 - kMaxLayerInfo;
philipel02447bc2016-05-13 06:01:03 -0700299 auto clean_layer_info_to = layer_info_.lower_bound(old_tl0_pic_idx);
300 layer_info_.erase(layer_info_.begin(), clean_layer_info_to);
301
302 // Clean up info about not yet received frames that are too old.
303 uint16_t old_picture_id =
philipel0fa82a62018-03-19 15:34:53 +0100304 Subtract<kPicIdLength>(frame->id.picture_id, kMaxNotYetReceivedFrames);
philipel02447bc2016-05-13 06:01:03 -0700305 auto clean_frames_to = not_yet_received_frames_.lower_bound(old_picture_id);
306 not_yet_received_frames_.erase(not_yet_received_frames_.begin(),
307 clean_frames_to);
308
309 if (frame->frame_type() == kVideoFrameKey) {
310 frame->num_references = 0;
philipel57ec6852018-07-03 18:09:32 +0200311 layer_info_[unwrapped_tl0].fill(-1);
312 UpdateLayerInfoVp8(frame, unwrapped_tl0, codec_header.temporalIdx);
philipelafcf7f52017-04-26 08:17:35 -0700313 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700314 }
315
philipel57ec6852018-07-03 18:09:32 +0200316 auto layer_info_it = layer_info_.find(
317 codec_header.temporalIdx == 0 ? unwrapped_tl0 - 1 : unwrapped_tl0);
philipel02447bc2016-05-13 06:01:03 -0700318
319 // If we don't have the base layer frame yet, stash this frame.
philipelafcf7f52017-04-26 08:17:35 -0700320 if (layer_info_it == layer_info_.end())
321 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700322
323 // A non keyframe base layer frame has been received, copy the layer info
324 // from the previous base layer frame and set a reference to the previous
325 // base layer frame.
326 if (codec_header.temporalIdx == 0) {
327 layer_info_it =
philipel57ec6852018-07-03 18:09:32 +0200328 layer_info_.emplace(unwrapped_tl0, layer_info_it->second).first;
philipel02447bc2016-05-13 06:01:03 -0700329 frame->num_references = 1;
330 frame->references[0] = layer_info_it->second[0];
philipel57ec6852018-07-03 18:09:32 +0200331 UpdateLayerInfoVp8(frame, unwrapped_tl0, codec_header.temporalIdx);
philipelafcf7f52017-04-26 08:17:35 -0700332 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700333 }
334
335 // Layer sync frame, this frame only references its base layer frame.
336 if (codec_header.layerSync) {
337 frame->num_references = 1;
338 frame->references[0] = layer_info_it->second[0];
339
philipel57ec6852018-07-03 18:09:32 +0200340 UpdateLayerInfoVp8(frame, unwrapped_tl0, codec_header.temporalIdx);
philipelafcf7f52017-04-26 08:17:35 -0700341 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700342 }
343
344 // Find all references for this frame.
345 frame->num_references = 0;
346 for (uint8_t layer = 0; layer <= codec_header.temporalIdx; ++layer) {
philipeld268d6f2016-09-15 13:43:13 +0200347 // If we have not yet received a previous frame on this temporal layer,
348 // stash this frame.
philipelafcf7f52017-04-26 08:17:35 -0700349 if (layer_info_it->second[layer] == -1)
350 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700351
philipel86b92e02016-10-24 07:11:53 -0700352 // If the last frame on this layer is ahead of this frame it means that
353 // a layer sync frame has been received after this frame for the same
354 // base layer frame, drop this frame.
355 if (AheadOf<uint16_t, kPicIdLength>(layer_info_it->second[layer],
philipel0fa82a62018-03-19 15:34:53 +0100356 frame->id.picture_id)) {
philipelafcf7f52017-04-26 08:17:35 -0700357 return kDrop;
philipel86b92e02016-10-24 07:11:53 -0700358 }
359
philipel02447bc2016-05-13 06:01:03 -0700360 // If we have not yet received a frame between this frame and the referenced
361 // frame then we have to wait for that frame to be completed first.
362 auto not_received_frame_it =
363 not_yet_received_frames_.upper_bound(layer_info_it->second[layer]);
364 if (not_received_frame_it != not_yet_received_frames_.end() &&
philipel0fa82a62018-03-19 15:34:53 +0100365 AheadOf<uint16_t, kPicIdLength>(frame->id.picture_id,
philipel02447bc2016-05-13 06:01:03 -0700366 *not_received_frame_it)) {
philipelafcf7f52017-04-26 08:17:35 -0700367 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700368 }
369
philipel0fa82a62018-03-19 15:34:53 +0100370 if (!(AheadOf<uint16_t, kPicIdLength>(frame->id.picture_id,
philipel57f19cc2017-03-07 03:54:05 -0800371 layer_info_it->second[layer]))) {
philipel0fa82a62018-03-19 15:34:53 +0100372 RTC_LOG(LS_WARNING) << "Frame with picture id " << frame->id.picture_id
Mirko Bonadei675513b2017-11-09 11:09:25 +0100373 << " and packet range [" << frame->first_seq_num()
374 << ", " << frame->last_seq_num()
375 << "] already received, "
376 << " dropping frame.";
philipelafcf7f52017-04-26 08:17:35 -0700377 return kDrop;
philipel57f19cc2017-03-07 03:54:05 -0800378 }
379
philipel02447bc2016-05-13 06:01:03 -0700380 ++frame->num_references;
381 frame->references[layer] = layer_info_it->second[layer];
382 }
383
philipel57ec6852018-07-03 18:09:32 +0200384 UpdateLayerInfoVp8(frame, unwrapped_tl0, codec_header.temporalIdx);
philipelafcf7f52017-04-26 08:17:35 -0700385 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700386}
387
philipel57ec6852018-07-03 18:09:32 +0200388void RtpFrameReferenceFinder::UpdateLayerInfoVp8(RtpFrameObject* frame,
389 int64_t unwrapped_tl0,
390 uint8_t temporal_idx) {
391 auto layer_info_it = layer_info_.find(unwrapped_tl0);
philipel02447bc2016-05-13 06:01:03 -0700392
393 // Update this layer info and newer.
394 while (layer_info_it != layer_info_.end()) {
philipel57ec6852018-07-03 18:09:32 +0200395 if (layer_info_it->second[temporal_idx] != -1 &&
396 AheadOf<uint16_t, kPicIdLength>(layer_info_it->second[temporal_idx],
philipel0fa82a62018-03-19 15:34:53 +0100397 frame->id.picture_id)) {
philipel02447bc2016-05-13 06:01:03 -0700398 // The frame was not newer, then no subsequent layer info have to be
399 // update.
400 break;
401 }
402
philipel57ec6852018-07-03 18:09:32 +0200403 layer_info_it->second[temporal_idx] = frame->id.picture_id;
404 ++unwrapped_tl0;
405 layer_info_it = layer_info_.find(unwrapped_tl0);
philipel02447bc2016-05-13 06:01:03 -0700406 }
philipel0fa82a62018-03-19 15:34:53 +0100407 not_yet_received_frames_.erase(frame->id.picture_id);
philipel02447bc2016-05-13 06:01:03 -0700408
philipelafcf7f52017-04-26 08:17:35 -0700409 UnwrapPictureIds(frame);
philipel02447bc2016-05-13 06:01:03 -0700410}
411
philipelafcf7f52017-04-26 08:17:35 -0700412RtpFrameReferenceFinder::FrameDecision RtpFrameReferenceFinder::ManageFrameVp9(
413 RtpFrameObject* frame) {
philipel5470f402018-09-07 13:38:53 +0200414 absl::optional<RTPVideoHeader> video_header = frame->GetRtpVideoHeader();
Sami Kalliomäki98824952018-08-28 14:39:21 +0200415 if (!video_header) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100416 RTC_LOG(LS_WARNING)
417 << "Failed to get codec header from frame, dropping frame.";
philipel4c140092017-08-31 08:31:45 -0700418 return kDrop;
philipeld4fac692017-09-04 07:03:46 -0700419 }
Sami Kalliomäki98824952018-08-28 14:39:21 +0200420 RTPVideoTypeHeader rtp_codec_header = video_header->video_type_header;
philipel4c140092017-08-31 08:31:45 -0700421
philipel1a4746a2018-07-09 15:52:29 +0200422 const RTPVideoHeaderVP9& codec_header =
Sami Kalliomäki98824952018-08-28 14:39:21 +0200423 absl::get<RTPVideoHeaderVP9>(rtp_codec_header);
philipel02447bc2016-05-13 06:01:03 -0700424
philipel647998c2016-06-03 09:40:16 -0700425 if (codec_header.picture_id == kNoPictureId ||
Sergey Silkind34a1882018-08-20 16:46:05 +0200426 codec_header.temporal_idx == kNoTemporalIdx) {
philipeldabfcae2018-09-25 12:54:37 +0200427 return ManageFramePidOrSeqNum(std::move(frame), codec_header.picture_id);
philipel02447bc2016-05-13 06:01:03 -0700428 }
429
philipel0fa82a62018-03-19 15:34:53 +0100430 frame->id.spatial_layer = codec_header.spatial_idx;
philipel02447bc2016-05-13 06:01:03 -0700431 frame->inter_layer_predicted = codec_header.inter_layer_predicted;
philipel0fa82a62018-03-19 15:34:53 +0100432 frame->id.picture_id = codec_header.picture_id % kPicIdLength;
philipel02447bc2016-05-13 06:01:03 -0700433
philipel02447bc2016-05-13 06:01:03 -0700434 if (last_picture_id_ == -1)
philipel0fa82a62018-03-19 15:34:53 +0100435 last_picture_id_ = frame->id.picture_id;
philipel02447bc2016-05-13 06:01:03 -0700436
437 if (codec_header.flexible_mode) {
438 frame->num_references = codec_header.num_ref_pics;
439 for (size_t i = 0; i < frame->num_references; ++i) {
philipel0fa82a62018-03-19 15:34:53 +0100440 frame->references[i] = Subtract<kPicIdLength>(frame->id.picture_id,
441 codec_header.pid_diff[i]);
philipel02447bc2016-05-13 06:01:03 -0700442 }
443
philipelafcf7f52017-04-26 08:17:35 -0700444 UnwrapPictureIds(frame);
445 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700446 }
447
Sergey Silkind34a1882018-08-20 16:46:05 +0200448 if (codec_header.tl0_pic_idx == kNoTl0PicIdx) {
449 RTC_LOG(LS_WARNING) << "TL0PICIDX is expected to be present in "
450 "non-flexible mode.";
451 return kDrop;
452 }
453
philipel15643602018-05-03 16:14:13 +0200454 GofInfo* info;
philipel57ec6852018-07-03 18:09:32 +0200455 int64_t unwrapped_tl0 = tl0_unwrapper_.Unwrap(codec_header.tl0_pic_idx);
philipel02447bc2016-05-13 06:01:03 -0700456 if (codec_header.ss_data_available) {
philipel02447bc2016-05-13 06:01:03 -0700457 if (codec_header.temporal_idx != 0) {
philipel15643602018-05-03 16:14:13 +0200458 RTC_LOG(LS_WARNING) << "Received scalability structure on a non base "
459 "layer frame. Scalability structure ignored.";
philipel02447bc2016-05-13 06:01:03 -0700460 } else {
Sergey Silkin2f864fb2018-09-07 11:49:38 +0200461 if (codec_header.gof.num_frames_in_gof > kMaxVp9FramesInGof) {
Natalie Silvanovich3ea3e302018-05-16 11:03:12 -0700462 return kDrop;
463 }
464
Sergey Silkin2f864fb2018-09-07 11:49:38 +0200465 GofInfoVP9 gof = codec_header.gof;
466 if (gof.num_frames_in_gof == 0) {
467 RTC_LOG(LS_WARNING) << "Number of frames in GOF is zero. Assume "
468 "that stream has only one temporal layer.";
469 gof.SetGofInfoVP9(kTemporalStructureMode1);
470 }
471
472 current_ss_idx_ = Add<kMaxGofSaved>(current_ss_idx_, 1);
473 scalability_structures_[current_ss_idx_] = gof;
philipel0fa82a62018-03-19 15:34:53 +0100474 scalability_structures_[current_ss_idx_].pid_start = frame->id.picture_id;
philipel57ec6852018-07-03 18:09:32 +0200475 gof_info_.emplace(unwrapped_tl0,
philipel15643602018-05-03 16:14:13 +0200476 GofInfo(&scalability_structures_[current_ss_idx_],
477 frame->id.picture_id));
philipel02447bc2016-05-13 06:01:03 -0700478 }
philipel15643602018-05-03 16:14:13 +0200479
philipel57ec6852018-07-03 18:09:32 +0200480 const auto gof_info_it = gof_info_.find(unwrapped_tl0);
philipel15643602018-05-03 16:14:13 +0200481 if (gof_info_it == gof_info_.end())
482 return kStash;
483
484 info = &gof_info_it->second;
485
486 if (frame->frame_type() == kVideoFrameKey) {
487 frame->num_references = 0;
488 FrameReceivedVp9(frame->id.picture_id, info);
489 UnwrapPictureIds(frame);
490 return kHandOff;
491 }
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100492 } else if (frame->frame_type() == kVideoFrameKey) {
493 if (frame->id.spatial_layer == 0) {
philipel15643602018-05-03 16:14:13 +0200494 RTC_LOG(LS_WARNING) << "Received keyframe without scalability structure";
495 return kDrop;
496 }
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100497 const auto gof_info_it = gof_info_.find(unwrapped_tl0);
498 if (gof_info_it == gof_info_.end())
499 return kStash;
philipel15643602018-05-03 16:14:13 +0200500
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100501 info = &gof_info_it->second;
502
503 if (frame->frame_type() == kVideoFrameKey) {
504 frame->num_references = 0;
505 FrameReceivedVp9(frame->id.picture_id, info);
506 UnwrapPictureIds(frame);
507 return kHandOff;
508 }
509 } else {
philipel57ec6852018-07-03 18:09:32 +0200510 auto gof_info_it = gof_info_.find(
511 (codec_header.temporal_idx == 0) ? unwrapped_tl0 - 1 : unwrapped_tl0);
philipel15643602018-05-03 16:14:13 +0200512
513 // Gof info for this frame is not available yet, stash this frame.
514 if (gof_info_it == gof_info_.end())
515 return kStash;
516
517 if (codec_header.temporal_idx == 0) {
philipel57ec6852018-07-03 18:09:32 +0200518 gof_info_it = gof_info_
519 .emplace(unwrapped_tl0, GofInfo(gof_info_it->second.gof,
520 frame->id.picture_id))
521 .first;
philipel15643602018-05-03 16:14:13 +0200522 }
523
524 info = &gof_info_it->second;
philipel02447bc2016-05-13 06:01:03 -0700525 }
526
527 // Clean up info for base layers that are too old.
philipel57ec6852018-07-03 18:09:32 +0200528 int64_t old_tl0_pic_idx = unwrapped_tl0 - kMaxGofSaved;
philipel02447bc2016-05-13 06:01:03 -0700529 auto clean_gof_info_to = gof_info_.lower_bound(old_tl0_pic_idx);
530 gof_info_.erase(gof_info_.begin(), clean_gof_info_to);
531
philipel0fa82a62018-03-19 15:34:53 +0100532 FrameReceivedVp9(frame->id.picture_id, info);
philipel02447bc2016-05-13 06:01:03 -0700533
534 // Make sure we don't miss any frame that could potentially have the
535 // up switch flag set.
philipel0fa82a62018-03-19 15:34:53 +0100536 if (MissingRequiredFrameVp9(frame->id.picture_id, *info))
philipelafcf7f52017-04-26 08:17:35 -0700537 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700538
philipel15643602018-05-03 16:14:13 +0200539 if (codec_header.temporal_up_switch)
540 up_switch_.emplace(frame->id.picture_id, codec_header.temporal_idx);
philipel02447bc2016-05-13 06:01:03 -0700541
542 // Clean out old info about up switch frames.
philipel0fa82a62018-03-19 15:34:53 +0100543 uint16_t old_picture_id = Subtract<kPicIdLength>(frame->id.picture_id, 50);
philipel02447bc2016-05-13 06:01:03 -0700544 auto up_switch_erase_to = up_switch_.lower_bound(old_picture_id);
545 up_switch_.erase(up_switch_.begin(), up_switch_erase_to);
546
philipelc9b27d52016-07-15 06:50:27 -0700547 size_t diff = ForwardDiff<uint16_t, kPicIdLength>(info->gof->pid_start,
philipel0fa82a62018-03-19 15:34:53 +0100548 frame->id.picture_id);
philipelc9b27d52016-07-15 06:50:27 -0700549 size_t gof_idx = diff % info->gof->num_frames_in_gof;
philipel02447bc2016-05-13 06:01:03 -0700550
551 // Populate references according to the scalability structure.
philipelc9b27d52016-07-15 06:50:27 -0700552 frame->num_references = info->gof->num_ref_pics[gof_idx];
philipel02447bc2016-05-13 06:01:03 -0700553 for (size_t i = 0; i < frame->num_references; ++i) {
philipelc9b27d52016-07-15 06:50:27 -0700554 frame->references[i] = Subtract<kPicIdLength>(
philipel0fa82a62018-03-19 15:34:53 +0100555 frame->id.picture_id, info->gof->pid_diff[gof_idx][i]);
philipel02447bc2016-05-13 06:01:03 -0700556
557 // If this is a reference to a frame earlier than the last up switch point,
558 // then ignore this reference.
philipel0fa82a62018-03-19 15:34:53 +0100559 if (UpSwitchInIntervalVp9(frame->id.picture_id, codec_header.temporal_idx,
philipel02447bc2016-05-13 06:01:03 -0700560 frame->references[i])) {
561 --frame->num_references;
562 }
563 }
564
philipelafcf7f52017-04-26 08:17:35 -0700565 UnwrapPictureIds(frame);
566 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700567}
568
569bool RtpFrameReferenceFinder::MissingRequiredFrameVp9(uint16_t picture_id,
philipelc9b27d52016-07-15 06:50:27 -0700570 const GofInfo& info) {
571 size_t diff =
572 ForwardDiff<uint16_t, kPicIdLength>(info.gof->pid_start, picture_id);
573 size_t gof_idx = diff % info.gof->num_frames_in_gof;
574 size_t temporal_idx = info.gof->temporal_idx[gof_idx];
philipel02447bc2016-05-13 06:01:03 -0700575
philipela157e082018-05-02 15:19:01 +0200576 if (temporal_idx >= kMaxTemporalLayers) {
577 RTC_LOG(LS_WARNING) << "At most " << kMaxTemporalLayers << " temporal "
578 << "layers are supported.";
579 return true;
580 }
581
philipel02447bc2016-05-13 06:01:03 -0700582 // For every reference this frame has, check if there is a frame missing in
583 // the interval (|ref_pid|, |picture_id|) in any of the lower temporal
584 // layers. If so, we are missing a required frame.
philipelc9b27d52016-07-15 06:50:27 -0700585 uint8_t num_references = info.gof->num_ref_pics[gof_idx];
philipel02447bc2016-05-13 06:01:03 -0700586 for (size_t i = 0; i < num_references; ++i) {
587 uint16_t ref_pid =
philipelc9b27d52016-07-15 06:50:27 -0700588 Subtract<kPicIdLength>(picture_id, info.gof->pid_diff[gof_idx][i]);
philipel02447bc2016-05-13 06:01:03 -0700589 for (size_t l = 0; l < temporal_idx; ++l) {
590 auto missing_frame_it = missing_frames_for_layer_[l].lower_bound(ref_pid);
591 if (missing_frame_it != missing_frames_for_layer_[l].end() &&
592 AheadOf<uint16_t, kPicIdLength>(picture_id, *missing_frame_it)) {
593 return true;
594 }
595 }
596 }
597 return false;
598}
599
600void RtpFrameReferenceFinder::FrameReceivedVp9(uint16_t picture_id,
philipelc9b27d52016-07-15 06:50:27 -0700601 GofInfo* info) {
602 int last_picture_id = info->last_picture_id;
philipel459f4e32018-03-02 10:55:12 +0100603 size_t gof_size = std::min(info->gof->num_frames_in_gof, kMaxVp9FramesInGof);
philipel02447bc2016-05-13 06:01:03 -0700604
605 // If there is a gap, find which temporal layer the missing frames
606 // belong to and add the frame as missing for that temporal layer.
607 // Otherwise, remove this frame from the set of missing frames.
philipelc9b27d52016-07-15 06:50:27 -0700608 if (AheadOf<uint16_t, kPicIdLength>(picture_id, last_picture_id)) {
609 size_t diff = ForwardDiff<uint16_t, kPicIdLength>(info->gof->pid_start,
610 last_picture_id);
philipel459f4e32018-03-02 10:55:12 +0100611 size_t gof_idx = diff % gof_size;
philipel02447bc2016-05-13 06:01:03 -0700612
philipelc9b27d52016-07-15 06:50:27 -0700613 last_picture_id = Add<kPicIdLength>(last_picture_id, 1);
614 while (last_picture_id != picture_id) {
Yves Gerey665174f2018-06-19 15:03:05 +0200615 gof_idx = (gof_idx + 1) % gof_size;
philipel459f4e32018-03-02 10:55:12 +0100616 RTC_CHECK(gof_idx < kMaxVp9FramesInGof);
617
philipelc9b27d52016-07-15 06:50:27 -0700618 size_t temporal_idx = info->gof->temporal_idx[gof_idx];
philipel459f4e32018-03-02 10:55:12 +0100619 if (temporal_idx >= kMaxTemporalLayers) {
620 RTC_LOG(LS_WARNING) << "At most " << kMaxTemporalLayers << " temporal "
621 << "layers are supported.";
622 return;
623 }
624
philipelc9b27d52016-07-15 06:50:27 -0700625 missing_frames_for_layer_[temporal_idx].insert(last_picture_id);
626 last_picture_id = Add<kPicIdLength>(last_picture_id, 1);
philipel02447bc2016-05-13 06:01:03 -0700627 }
philipel459f4e32018-03-02 10:55:12 +0100628
philipelc9b27d52016-07-15 06:50:27 -0700629 info->last_picture_id = last_picture_id;
philipel02447bc2016-05-13 06:01:03 -0700630 } else {
631 size_t diff =
philipelc9b27d52016-07-15 06:50:27 -0700632 ForwardDiff<uint16_t, kPicIdLength>(info->gof->pid_start, picture_id);
philipel459f4e32018-03-02 10:55:12 +0100633 size_t gof_idx = diff % gof_size;
634 RTC_CHECK(gof_idx < kMaxVp9FramesInGof);
635
philipelc9b27d52016-07-15 06:50:27 -0700636 size_t temporal_idx = info->gof->temporal_idx[gof_idx];
philipel459f4e32018-03-02 10:55:12 +0100637 if (temporal_idx >= kMaxTemporalLayers) {
638 RTC_LOG(LS_WARNING) << "At most " << kMaxTemporalLayers << " temporal "
639 << "layers are supported.";
640 return;
641 }
642
philipel02447bc2016-05-13 06:01:03 -0700643 missing_frames_for_layer_[temporal_idx].erase(picture_id);
644 }
645}
646
647bool RtpFrameReferenceFinder::UpSwitchInIntervalVp9(uint16_t picture_id,
648 uint8_t temporal_idx,
649 uint16_t pid_ref) {
650 for (auto up_switch_it = up_switch_.upper_bound(pid_ref);
651 up_switch_it != up_switch_.end() &&
652 AheadOf<uint16_t, kPicIdLength>(picture_id, up_switch_it->first);
653 ++up_switch_it) {
654 if (up_switch_it->second < temporal_idx)
655 return true;
656 }
657
658 return false;
659}
660
philipelafcf7f52017-04-26 08:17:35 -0700661void RtpFrameReferenceFinder::UnwrapPictureIds(RtpFrameObject* frame) {
philipel02447bc2016-05-13 06:01:03 -0700662 for (size_t i = 0; i < frame->num_references; ++i)
philipeld4fac692017-09-04 07:03:46 -0700663 frame->references[i] = unwrapper_.Unwrap(frame->references[i]);
philipel0fa82a62018-03-19 15:34:53 +0100664 frame->id.picture_id = unwrapper_.Unwrap(frame->id.picture_id);
philipel02447bc2016-05-13 06:01:03 -0700665}
666
philipel02447bc2016-05-13 06:01:03 -0700667} // namespace video_coding
668} // namespace webrtc