blob: da2b1a86c8754661a4fa78cd781062a6e3e3133d [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);
philipel266f0a42016-11-28 08:49:07 -080091 // Since the EndToEndTests use kVicdeoCodecUnknow we treat it the same as
92 // kVideoCodecGeneric.
93 // TODO(philipel): Take a look at the EndToEndTests and see if maybe they
94 // should be changed to use kVideoCodecGeneric instead.
95 case kVideoCodecUnknown:
philipel02447bc2016-05-13 06:01:03 -070096 case kVideoCodecH264:
97 case kVideoCodecI420:
Emircan Uysalerd7ae3c32018-01-25 13:01:09 -080098 case kVideoCodecMultiplex:
philipel02447bc2016-05-13 06:01:03 -070099 case kVideoCodecGeneric:
philipelafcf7f52017-04-26 08:17:35 -0700100 return ManageFrameGeneric(frame, kNoPictureId);
philipel02447bc2016-05-13 06:01:03 -0700101 }
philipelafcf7f52017-04-26 08:17:35 -0700102
103 // If not all code paths return a value it makes the win compiler sad.
104 RTC_NOTREACHED();
105 return kDrop;
philipel02447bc2016-05-13 06:01:03 -0700106}
107
philipel9b2ce6b2016-07-05 05:04:46 -0700108void RtpFrameReferenceFinder::PaddingReceived(uint16_t seq_num) {
109 rtc::CritScope lock(&crit_);
110 auto clean_padding_to =
111 stashed_padding_.lower_bound(seq_num - kMaxPaddingAge);
112 stashed_padding_.erase(stashed_padding_.begin(), clean_padding_to);
113 stashed_padding_.insert(seq_num);
114 UpdateLastPictureIdWithPadding(seq_num);
115 RetryStashedFrames();
116}
117
philipel463d3012016-09-09 03:32:44 -0700118void RtpFrameReferenceFinder::ClearTo(uint16_t seq_num) {
119 rtc::CritScope lock(&crit_);
120 cleared_to_seq_num_ = seq_num;
121
122 auto it = stashed_frames_.begin();
123 while (it != stashed_frames_.end()) {
124 if (AheadOf<uint16_t>(cleared_to_seq_num_, (*it)->first_seq_num())) {
125 it = stashed_frames_.erase(it);
126 } else {
127 ++it;
128 }
129 }
130}
131
philipel9b2ce6b2016-07-05 05:04:46 -0700132void RtpFrameReferenceFinder::UpdateLastPictureIdWithPadding(uint16_t seq_num) {
133 auto gop_seq_num_it = last_seq_num_gop_.upper_bound(seq_num);
134
135 // If this padding packet "belongs" to a group of pictures that we don't track
136 // anymore, do nothing.
137 if (gop_seq_num_it == last_seq_num_gop_.begin())
138 return;
139 --gop_seq_num_it;
140
141 // Calculate the next contiuous sequence number and search for it in
142 // the padding packets we have stashed.
143 uint16_t next_seq_num_with_padding = gop_seq_num_it->second.second + 1;
144 auto padding_seq_num_it =
145 stashed_padding_.lower_bound(next_seq_num_with_padding);
146
147 // While there still are padding packets and those padding packets are
148 // continuous, then advance the "last-picture-id-with-padding" and remove
149 // the stashed padding packet.
150 while (padding_seq_num_it != stashed_padding_.end() &&
151 *padding_seq_num_it == next_seq_num_with_padding) {
152 gop_seq_num_it->second.second = next_seq_num_with_padding;
153 ++next_seq_num_with_padding;
154 padding_seq_num_it = stashed_padding_.erase(padding_seq_num_it);
155 }
philipel41bb7922017-02-20 07:53:23 -0800156
157 // In the case where the stream has been continuous without any new keyframes
158 // for a while there is a risk that new frames will appear to be older than
159 // the keyframe they belong to due to wrapping sequence number. In order
160 // to prevent this we advance the picture id of the keyframe every so often.
161 if (ForwardDiff(gop_seq_num_it->first, seq_num) > 10000) {
162 RTC_DCHECK_EQ(1ul, last_seq_num_gop_.size());
163 last_seq_num_gop_[seq_num] = gop_seq_num_it->second;
164 last_seq_num_gop_.erase(gop_seq_num_it);
165 }
philipel9b2ce6b2016-07-05 05:04:46 -0700166}
167
philipelafcf7f52017-04-26 08:17:35 -0700168RtpFrameReferenceFinder::FrameDecision
169RtpFrameReferenceFinder::ManageFrameGeneric(RtpFrameObject* frame,
170 int picture_id) {
philipel647998c2016-06-03 09:40:16 -0700171 // If |picture_id| is specified then we use that to set the frame references,
172 // otherwise we use sequence number.
173 if (picture_id != kNoPictureId) {
174 if (last_unwrap_ == -1)
175 last_unwrap_ = picture_id;
176
philipel0fa82a62018-03-19 15:34:53 +0100177 frame->id.picture_id = unwrapper_.Unwrap(picture_id);
philipel647998c2016-06-03 09:40:16 -0700178 frame->num_references = frame->frame_type() == kVideoFrameKey ? 0 : 1;
philipel0fa82a62018-03-19 15:34:53 +0100179 frame->references[0] = frame->id.picture_id - 1;
philipelafcf7f52017-04-26 08:17:35 -0700180 return kHandOff;
philipel647998c2016-06-03 09:40:16 -0700181 }
182
philipel9b2ce6b2016-07-05 05:04:46 -0700183 if (frame->frame_type() == kVideoFrameKey) {
184 last_seq_num_gop_.insert(std::make_pair(
185 frame->last_seq_num(),
186 std::make_pair(frame->last_seq_num(), frame->last_seq_num())));
187 }
philipel02447bc2016-05-13 06:01:03 -0700188
189 // We have received a frame but not yet a keyframe, stash this frame.
philipelafcf7f52017-04-26 08:17:35 -0700190 if (last_seq_num_gop_.empty())
191 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700192
193 // Clean up info for old keyframes but make sure to keep info
194 // for the last keyframe.
195 auto clean_to = last_seq_num_gop_.lower_bound(frame->last_seq_num() - 100);
philipel41bb7922017-02-20 07:53:23 -0800196 for (auto it = last_seq_num_gop_.begin();
197 it != clean_to && last_seq_num_gop_.size() > 1;) {
198 it = last_seq_num_gop_.erase(it);
199 }
philipel02447bc2016-05-13 06:01:03 -0700200
201 // Find the last sequence number of the last frame for the keyframe
202 // that this frame indirectly references.
203 auto seq_num_it = last_seq_num_gop_.upper_bound(frame->last_seq_num());
philipel9b2ce6b2016-07-05 05:04:46 -0700204 if (seq_num_it == last_seq_num_gop_.begin()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100205 RTC_LOG(LS_WARNING) << "Generic frame with packet range ["
206 << frame->first_seq_num() << ", "
207 << frame->last_seq_num()
208 << "] has no GoP, dropping frame.";
philipelafcf7f52017-04-26 08:17:35 -0700209 return kDrop;
philipel9b2ce6b2016-07-05 05:04:46 -0700210 }
philipel02447bc2016-05-13 06:01:03 -0700211 seq_num_it--;
212
213 // Make sure the packet sequence numbers are continuous, otherwise stash
214 // this frame.
philipel9b2ce6b2016-07-05 05:04:46 -0700215 uint16_t last_picture_id_gop = seq_num_it->second.first;
216 uint16_t last_picture_id_with_padding_gop = seq_num_it->second.second;
philipel02447bc2016-05-13 06:01:03 -0700217 if (frame->frame_type() == kVideoFrameDelta) {
philipel9b2ce6b2016-07-05 05:04:46 -0700218 uint16_t prev_seq_num = frame->first_seq_num() - 1;
philipelafcf7f52017-04-26 08:17:35 -0700219
220 if (prev_seq_num != last_picture_id_with_padding_gop)
221 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700222 }
223
224 RTC_DCHECK(AheadOrAt(frame->last_seq_num(), seq_num_it->first));
225
226 // Since keyframes can cause reordering we can't simply assign the
227 // picture id according to some incrementing counter.
philipel0fa82a62018-03-19 15:34:53 +0100228 frame->id.picture_id = frame->last_seq_num();
philipel02447bc2016-05-13 06:01:03 -0700229 frame->num_references = frame->frame_type() == kVideoFrameDelta;
philipeld4fac692017-09-04 07:03:46 -0700230 frame->references[0] = generic_unwrapper_.Unwrap(last_picture_id_gop);
philipel0fa82a62018-03-19 15:34:53 +0100231 if (AheadOf<uint16_t>(frame->id.picture_id, last_picture_id_gop)) {
232 seq_num_it->second.first = frame->id.picture_id;
233 seq_num_it->second.second = frame->id.picture_id;
philipel9b2ce6b2016-07-05 05:04:46 -0700234 }
philipel02447bc2016-05-13 06:01:03 -0700235
philipel0fa82a62018-03-19 15:34:53 +0100236 last_picture_id_ = frame->id.picture_id;
237 UpdateLastPictureIdWithPadding(frame->id.picture_id);
238 frame->id.picture_id = generic_unwrapper_.Unwrap(frame->id.picture_id);
philipelafcf7f52017-04-26 08:17:35 -0700239 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700240}
241
philipelafcf7f52017-04-26 08:17:35 -0700242RtpFrameReferenceFinder::FrameDecision RtpFrameReferenceFinder::ManageFrameVp8(
243 RtpFrameObject* frame) {
Danil Chapovalov0040b662018-06-18 10:48:16 +0200244 absl::optional<RTPVideoTypeHeader> rtp_codec_header = frame->GetCodecHeader();
philipeld4fac692017-09-04 07:03:46 -0700245 if (!rtp_codec_header) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100246 RTC_LOG(LS_WARNING)
247 << "Failed to get codec header from frame, dropping frame.";
philipelafcf7f52017-04-26 08:17:35 -0700248 return kDrop;
philipeld4fac692017-09-04 07:03:46 -0700249 }
philipel02447bc2016-05-13 06:01:03 -0700250
philipel1a4746a2018-07-09 15:52:29 +0200251 const RTPVideoHeaderVP8& codec_header =
252 absl::get<RTPVideoHeaderVP8>(*rtp_codec_header);
philipel02447bc2016-05-13 06:01:03 -0700253
254 if (codec_header.pictureId == kNoPictureId ||
255 codec_header.temporalIdx == kNoTemporalIdx ||
256 codec_header.tl0PicIdx == kNoTl0PicIdx) {
philipelafcf7f52017-04-26 08:17:35 -0700257 return ManageFrameGeneric(std::move(frame), codec_header.pictureId);
philipel02447bc2016-05-13 06:01:03 -0700258 }
259
philipel0fa82a62018-03-19 15:34:53 +0100260 frame->id.picture_id = codec_header.pictureId % kPicIdLength;
philipel02447bc2016-05-13 06:01:03 -0700261
262 if (last_unwrap_ == -1)
263 last_unwrap_ = codec_header.pictureId;
264
265 if (last_picture_id_ == -1)
philipel0fa82a62018-03-19 15:34:53 +0100266 last_picture_id_ = frame->id.picture_id;
philipel02447bc2016-05-13 06:01:03 -0700267
268 // Find if there has been a gap in fully received frames and save the picture
269 // id of those frames in |not_yet_received_frames_|.
philipel0fa82a62018-03-19 15:34:53 +0100270 if (AheadOf<uint16_t, kPicIdLength>(frame->id.picture_id, last_picture_id_)) {
philipel9bd1d662017-07-14 04:52:01 -0700271 do {
philipel02447bc2016-05-13 06:01:03 -0700272 last_picture_id_ = Add<kPicIdLength>(last_picture_id_, 1);
philipel9bd1d662017-07-14 04:52:01 -0700273 not_yet_received_frames_.insert(last_picture_id_);
philipel0fa82a62018-03-19 15:34:53 +0100274 } while (last_picture_id_ != frame->id.picture_id);
philipel02447bc2016-05-13 06:01:03 -0700275 }
276
philipel57ec6852018-07-03 18:09:32 +0200277 int64_t unwrapped_tl0 = tl0_unwrapper_.Unwrap(codec_header.tl0PicIdx);
278
philipel02447bc2016-05-13 06:01:03 -0700279 // Clean up info for base layers that are too old.
philipel57ec6852018-07-03 18:09:32 +0200280 int64_t old_tl0_pic_idx = unwrapped_tl0 - kMaxLayerInfo;
philipel02447bc2016-05-13 06:01:03 -0700281 auto clean_layer_info_to = layer_info_.lower_bound(old_tl0_pic_idx);
282 layer_info_.erase(layer_info_.begin(), clean_layer_info_to);
283
284 // Clean up info about not yet received frames that are too old.
285 uint16_t old_picture_id =
philipel0fa82a62018-03-19 15:34:53 +0100286 Subtract<kPicIdLength>(frame->id.picture_id, kMaxNotYetReceivedFrames);
philipel02447bc2016-05-13 06:01:03 -0700287 auto clean_frames_to = not_yet_received_frames_.lower_bound(old_picture_id);
288 not_yet_received_frames_.erase(not_yet_received_frames_.begin(),
289 clean_frames_to);
290
291 if (frame->frame_type() == kVideoFrameKey) {
292 frame->num_references = 0;
philipel57ec6852018-07-03 18:09:32 +0200293 layer_info_[unwrapped_tl0].fill(-1);
294 UpdateLayerInfoVp8(frame, unwrapped_tl0, codec_header.temporalIdx);
philipelafcf7f52017-04-26 08:17:35 -0700295 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700296 }
297
philipel57ec6852018-07-03 18:09:32 +0200298 auto layer_info_it = layer_info_.find(
299 codec_header.temporalIdx == 0 ? unwrapped_tl0 - 1 : unwrapped_tl0);
philipel02447bc2016-05-13 06:01:03 -0700300
301 // If we don't have the base layer frame yet, stash this frame.
philipelafcf7f52017-04-26 08:17:35 -0700302 if (layer_info_it == layer_info_.end())
303 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700304
305 // A non keyframe base layer frame has been received, copy the layer info
306 // from the previous base layer frame and set a reference to the previous
307 // base layer frame.
308 if (codec_header.temporalIdx == 0) {
309 layer_info_it =
philipel57ec6852018-07-03 18:09:32 +0200310 layer_info_.emplace(unwrapped_tl0, layer_info_it->second).first;
philipel02447bc2016-05-13 06:01:03 -0700311 frame->num_references = 1;
312 frame->references[0] = layer_info_it->second[0];
philipel57ec6852018-07-03 18:09:32 +0200313 UpdateLayerInfoVp8(frame, unwrapped_tl0, codec_header.temporalIdx);
philipelafcf7f52017-04-26 08:17:35 -0700314 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700315 }
316
317 // Layer sync frame, this frame only references its base layer frame.
318 if (codec_header.layerSync) {
319 frame->num_references = 1;
320 frame->references[0] = layer_info_it->second[0];
321
philipel57ec6852018-07-03 18:09:32 +0200322 UpdateLayerInfoVp8(frame, unwrapped_tl0, codec_header.temporalIdx);
philipelafcf7f52017-04-26 08:17:35 -0700323 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700324 }
325
326 // Find all references for this frame.
327 frame->num_references = 0;
328 for (uint8_t layer = 0; layer <= codec_header.temporalIdx; ++layer) {
philipeld268d6f2016-09-15 13:43:13 +0200329 // If we have not yet received a previous frame on this temporal layer,
330 // stash this frame.
philipelafcf7f52017-04-26 08:17:35 -0700331 if (layer_info_it->second[layer] == -1)
332 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700333
philipel86b92e02016-10-24 07:11:53 -0700334 // If the last frame on this layer is ahead of this frame it means that
335 // a layer sync frame has been received after this frame for the same
336 // base layer frame, drop this frame.
337 if (AheadOf<uint16_t, kPicIdLength>(layer_info_it->second[layer],
philipel0fa82a62018-03-19 15:34:53 +0100338 frame->id.picture_id)) {
philipelafcf7f52017-04-26 08:17:35 -0700339 return kDrop;
philipel86b92e02016-10-24 07:11:53 -0700340 }
341
philipel02447bc2016-05-13 06:01:03 -0700342 // If we have not yet received a frame between this frame and the referenced
343 // frame then we have to wait for that frame to be completed first.
344 auto not_received_frame_it =
345 not_yet_received_frames_.upper_bound(layer_info_it->second[layer]);
346 if (not_received_frame_it != not_yet_received_frames_.end() &&
philipel0fa82a62018-03-19 15:34:53 +0100347 AheadOf<uint16_t, kPicIdLength>(frame->id.picture_id,
philipel02447bc2016-05-13 06:01:03 -0700348 *not_received_frame_it)) {
philipelafcf7f52017-04-26 08:17:35 -0700349 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700350 }
351
philipel0fa82a62018-03-19 15:34:53 +0100352 if (!(AheadOf<uint16_t, kPicIdLength>(frame->id.picture_id,
philipel57f19cc2017-03-07 03:54:05 -0800353 layer_info_it->second[layer]))) {
philipel0fa82a62018-03-19 15:34:53 +0100354 RTC_LOG(LS_WARNING) << "Frame with picture id " << frame->id.picture_id
Mirko Bonadei675513b2017-11-09 11:09:25 +0100355 << " and packet range [" << frame->first_seq_num()
356 << ", " << frame->last_seq_num()
357 << "] already received, "
358 << " dropping frame.";
philipelafcf7f52017-04-26 08:17:35 -0700359 return kDrop;
philipel57f19cc2017-03-07 03:54:05 -0800360 }
361
philipel02447bc2016-05-13 06:01:03 -0700362 ++frame->num_references;
363 frame->references[layer] = layer_info_it->second[layer];
364 }
365
philipel57ec6852018-07-03 18:09:32 +0200366 UpdateLayerInfoVp8(frame, unwrapped_tl0, codec_header.temporalIdx);
philipelafcf7f52017-04-26 08:17:35 -0700367 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700368}
369
philipel57ec6852018-07-03 18:09:32 +0200370void RtpFrameReferenceFinder::UpdateLayerInfoVp8(RtpFrameObject* frame,
371 int64_t unwrapped_tl0,
372 uint8_t temporal_idx) {
373 auto layer_info_it = layer_info_.find(unwrapped_tl0);
philipel02447bc2016-05-13 06:01:03 -0700374
375 // Update this layer info and newer.
376 while (layer_info_it != layer_info_.end()) {
philipel57ec6852018-07-03 18:09:32 +0200377 if (layer_info_it->second[temporal_idx] != -1 &&
378 AheadOf<uint16_t, kPicIdLength>(layer_info_it->second[temporal_idx],
philipel0fa82a62018-03-19 15:34:53 +0100379 frame->id.picture_id)) {
philipel02447bc2016-05-13 06:01:03 -0700380 // The frame was not newer, then no subsequent layer info have to be
381 // update.
382 break;
383 }
384
philipel57ec6852018-07-03 18:09:32 +0200385 layer_info_it->second[temporal_idx] = frame->id.picture_id;
386 ++unwrapped_tl0;
387 layer_info_it = layer_info_.find(unwrapped_tl0);
philipel02447bc2016-05-13 06:01:03 -0700388 }
philipel0fa82a62018-03-19 15:34:53 +0100389 not_yet_received_frames_.erase(frame->id.picture_id);
philipel02447bc2016-05-13 06:01:03 -0700390
philipelafcf7f52017-04-26 08:17:35 -0700391 UnwrapPictureIds(frame);
philipel02447bc2016-05-13 06:01:03 -0700392}
393
philipelafcf7f52017-04-26 08:17:35 -0700394RtpFrameReferenceFinder::FrameDecision RtpFrameReferenceFinder::ManageFrameVp9(
395 RtpFrameObject* frame) {
Danil Chapovalov0040b662018-06-18 10:48:16 +0200396 absl::optional<RTPVideoTypeHeader> rtp_codec_header = frame->GetCodecHeader();
philipeld4fac692017-09-04 07:03:46 -0700397 if (!rtp_codec_header) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100398 RTC_LOG(LS_WARNING)
399 << "Failed to get codec header from frame, dropping frame.";
philipel4c140092017-08-31 08:31:45 -0700400 return kDrop;
philipeld4fac692017-09-04 07:03:46 -0700401 }
philipel4c140092017-08-31 08:31:45 -0700402
philipel1a4746a2018-07-09 15:52:29 +0200403 const RTPVideoHeaderVP9& codec_header =
404 absl::get<RTPVideoHeaderVP9>(*rtp_codec_header);
philipel02447bc2016-05-13 06:01:03 -0700405
philipel647998c2016-06-03 09:40:16 -0700406 if (codec_header.picture_id == kNoPictureId ||
philipel57ec6852018-07-03 18:09:32 +0200407 codec_header.temporal_idx == kNoTemporalIdx ||
408 codec_header.tl0_pic_idx == kNoTl0PicIdx) {
philipelafcf7f52017-04-26 08:17:35 -0700409 return ManageFrameGeneric(std::move(frame), codec_header.picture_id);
philipel02447bc2016-05-13 06:01:03 -0700410 }
411
philipel0fa82a62018-03-19 15:34:53 +0100412 frame->id.spatial_layer = codec_header.spatial_idx;
philipel02447bc2016-05-13 06:01:03 -0700413 frame->inter_layer_predicted = codec_header.inter_layer_predicted;
philipel0fa82a62018-03-19 15:34:53 +0100414 frame->id.picture_id = codec_header.picture_id % kPicIdLength;
philipel02447bc2016-05-13 06:01:03 -0700415
416 if (last_unwrap_ == -1)
417 last_unwrap_ = codec_header.picture_id;
418
419 if (last_picture_id_ == -1)
philipel0fa82a62018-03-19 15:34:53 +0100420 last_picture_id_ = frame->id.picture_id;
philipel02447bc2016-05-13 06:01:03 -0700421
422 if (codec_header.flexible_mode) {
423 frame->num_references = codec_header.num_ref_pics;
424 for (size_t i = 0; i < frame->num_references; ++i) {
philipel0fa82a62018-03-19 15:34:53 +0100425 frame->references[i] = Subtract<kPicIdLength>(frame->id.picture_id,
426 codec_header.pid_diff[i]);
philipel02447bc2016-05-13 06:01:03 -0700427 }
428
philipelafcf7f52017-04-26 08:17:35 -0700429 UnwrapPictureIds(frame);
430 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700431 }
432
philipel15643602018-05-03 16:14:13 +0200433 GofInfo* info;
philipel57ec6852018-07-03 18:09:32 +0200434 int64_t unwrapped_tl0 = tl0_unwrapper_.Unwrap(codec_header.tl0_pic_idx);
philipel02447bc2016-05-13 06:01:03 -0700435 if (codec_header.ss_data_available) {
philipel02447bc2016-05-13 06:01:03 -0700436 if (codec_header.temporal_idx != 0) {
philipel15643602018-05-03 16:14:13 +0200437 RTC_LOG(LS_WARNING) << "Received scalability structure on a non base "
438 "layer frame. Scalability structure ignored.";
philipel02447bc2016-05-13 06:01:03 -0700439 } else {
440 current_ss_idx_ = Add<kMaxGofSaved>(current_ss_idx_, 1);
Natalie Silvanovich3ea3e302018-05-16 11:03:12 -0700441 if (codec_header.gof.num_frames_in_gof == 0 ||
442 codec_header.gof.num_frames_in_gof > kMaxVp9FramesInGof) {
443 return kDrop;
444 }
445
philipel02447bc2016-05-13 06:01:03 -0700446 scalability_structures_[current_ss_idx_] = codec_header.gof;
philipel0fa82a62018-03-19 15:34:53 +0100447 scalability_structures_[current_ss_idx_].pid_start = frame->id.picture_id;
philipel57ec6852018-07-03 18:09:32 +0200448 gof_info_.emplace(unwrapped_tl0,
philipel15643602018-05-03 16:14:13 +0200449 GofInfo(&scalability_structures_[current_ss_idx_],
450 frame->id.picture_id));
philipel02447bc2016-05-13 06:01:03 -0700451 }
philipel15643602018-05-03 16:14:13 +0200452
philipel57ec6852018-07-03 18:09:32 +0200453 const auto gof_info_it = gof_info_.find(unwrapped_tl0);
philipel15643602018-05-03 16:14:13 +0200454 if (gof_info_it == gof_info_.end())
455 return kStash;
456
457 info = &gof_info_it->second;
458
459 if (frame->frame_type() == kVideoFrameKey) {
460 frame->num_references = 0;
461 FrameReceivedVp9(frame->id.picture_id, info);
462 UnwrapPictureIds(frame);
463 return kHandOff;
464 }
465 } else {
466 if (frame->frame_type() == kVideoFrameKey) {
467 RTC_LOG(LS_WARNING) << "Received keyframe without scalability structure";
468 return kDrop;
469 }
470
philipel57ec6852018-07-03 18:09:32 +0200471 auto gof_info_it = gof_info_.find(
472 (codec_header.temporal_idx == 0) ? unwrapped_tl0 - 1 : unwrapped_tl0);
philipel15643602018-05-03 16:14:13 +0200473
474 // Gof info for this frame is not available yet, stash this frame.
475 if (gof_info_it == gof_info_.end())
476 return kStash;
477
478 if (codec_header.temporal_idx == 0) {
philipel57ec6852018-07-03 18:09:32 +0200479 gof_info_it = gof_info_
480 .emplace(unwrapped_tl0, GofInfo(gof_info_it->second.gof,
481 frame->id.picture_id))
482 .first;
philipel15643602018-05-03 16:14:13 +0200483 }
484
485 info = &gof_info_it->second;
philipel02447bc2016-05-13 06:01:03 -0700486 }
487
488 // Clean up info for base layers that are too old.
philipel57ec6852018-07-03 18:09:32 +0200489 int64_t old_tl0_pic_idx = unwrapped_tl0 - kMaxGofSaved;
philipel02447bc2016-05-13 06:01:03 -0700490 auto clean_gof_info_to = gof_info_.lower_bound(old_tl0_pic_idx);
491 gof_info_.erase(gof_info_.begin(), clean_gof_info_to);
492
philipel0fa82a62018-03-19 15:34:53 +0100493 FrameReceivedVp9(frame->id.picture_id, info);
philipel02447bc2016-05-13 06:01:03 -0700494
495 // Make sure we don't miss any frame that could potentially have the
496 // up switch flag set.
philipel0fa82a62018-03-19 15:34:53 +0100497 if (MissingRequiredFrameVp9(frame->id.picture_id, *info))
philipelafcf7f52017-04-26 08:17:35 -0700498 return kStash;
philipel02447bc2016-05-13 06:01:03 -0700499
philipel15643602018-05-03 16:14:13 +0200500 if (codec_header.temporal_up_switch)
501 up_switch_.emplace(frame->id.picture_id, codec_header.temporal_idx);
philipel02447bc2016-05-13 06:01:03 -0700502
503 // Clean out old info about up switch frames.
philipel0fa82a62018-03-19 15:34:53 +0100504 uint16_t old_picture_id = Subtract<kPicIdLength>(frame->id.picture_id, 50);
philipel02447bc2016-05-13 06:01:03 -0700505 auto up_switch_erase_to = up_switch_.lower_bound(old_picture_id);
506 up_switch_.erase(up_switch_.begin(), up_switch_erase_to);
507
philipelc9b27d52016-07-15 06:50:27 -0700508 size_t diff = ForwardDiff<uint16_t, kPicIdLength>(info->gof->pid_start,
philipel0fa82a62018-03-19 15:34:53 +0100509 frame->id.picture_id);
philipelc9b27d52016-07-15 06:50:27 -0700510 size_t gof_idx = diff % info->gof->num_frames_in_gof;
philipel02447bc2016-05-13 06:01:03 -0700511
512 // Populate references according to the scalability structure.
philipelc9b27d52016-07-15 06:50:27 -0700513 frame->num_references = info->gof->num_ref_pics[gof_idx];
philipel02447bc2016-05-13 06:01:03 -0700514 for (size_t i = 0; i < frame->num_references; ++i) {
philipelc9b27d52016-07-15 06:50:27 -0700515 frame->references[i] = Subtract<kPicIdLength>(
philipel0fa82a62018-03-19 15:34:53 +0100516 frame->id.picture_id, info->gof->pid_diff[gof_idx][i]);
philipel02447bc2016-05-13 06:01:03 -0700517
518 // If this is a reference to a frame earlier than the last up switch point,
519 // then ignore this reference.
philipel0fa82a62018-03-19 15:34:53 +0100520 if (UpSwitchInIntervalVp9(frame->id.picture_id, codec_header.temporal_idx,
philipel02447bc2016-05-13 06:01:03 -0700521 frame->references[i])) {
522 --frame->num_references;
523 }
524 }
525
philipelafcf7f52017-04-26 08:17:35 -0700526 UnwrapPictureIds(frame);
527 return kHandOff;
philipel02447bc2016-05-13 06:01:03 -0700528}
529
530bool RtpFrameReferenceFinder::MissingRequiredFrameVp9(uint16_t picture_id,
philipelc9b27d52016-07-15 06:50:27 -0700531 const GofInfo& info) {
532 size_t diff =
533 ForwardDiff<uint16_t, kPicIdLength>(info.gof->pid_start, picture_id);
534 size_t gof_idx = diff % info.gof->num_frames_in_gof;
535 size_t temporal_idx = info.gof->temporal_idx[gof_idx];
philipel02447bc2016-05-13 06:01:03 -0700536
philipela157e082018-05-02 15:19:01 +0200537 if (temporal_idx >= kMaxTemporalLayers) {
538 RTC_LOG(LS_WARNING) << "At most " << kMaxTemporalLayers << " temporal "
539 << "layers are supported.";
540 return true;
541 }
542
philipel02447bc2016-05-13 06:01:03 -0700543 // For every reference this frame has, check if there is a frame missing in
544 // the interval (|ref_pid|, |picture_id|) in any of the lower temporal
545 // layers. If so, we are missing a required frame.
philipelc9b27d52016-07-15 06:50:27 -0700546 uint8_t num_references = info.gof->num_ref_pics[gof_idx];
philipel02447bc2016-05-13 06:01:03 -0700547 for (size_t i = 0; i < num_references; ++i) {
548 uint16_t ref_pid =
philipelc9b27d52016-07-15 06:50:27 -0700549 Subtract<kPicIdLength>(picture_id, info.gof->pid_diff[gof_idx][i]);
philipel02447bc2016-05-13 06:01:03 -0700550 for (size_t l = 0; l < temporal_idx; ++l) {
551 auto missing_frame_it = missing_frames_for_layer_[l].lower_bound(ref_pid);
552 if (missing_frame_it != missing_frames_for_layer_[l].end() &&
553 AheadOf<uint16_t, kPicIdLength>(picture_id, *missing_frame_it)) {
554 return true;
555 }
556 }
557 }
558 return false;
559}
560
561void RtpFrameReferenceFinder::FrameReceivedVp9(uint16_t picture_id,
philipelc9b27d52016-07-15 06:50:27 -0700562 GofInfo* info) {
563 int last_picture_id = info->last_picture_id;
philipel459f4e32018-03-02 10:55:12 +0100564 size_t gof_size = std::min(info->gof->num_frames_in_gof, kMaxVp9FramesInGof);
philipel02447bc2016-05-13 06:01:03 -0700565
566 // If there is a gap, find which temporal layer the missing frames
567 // belong to and add the frame as missing for that temporal layer.
568 // Otherwise, remove this frame from the set of missing frames.
philipelc9b27d52016-07-15 06:50:27 -0700569 if (AheadOf<uint16_t, kPicIdLength>(picture_id, last_picture_id)) {
570 size_t diff = ForwardDiff<uint16_t, kPicIdLength>(info->gof->pid_start,
571 last_picture_id);
philipel459f4e32018-03-02 10:55:12 +0100572 size_t gof_idx = diff % gof_size;
philipel02447bc2016-05-13 06:01:03 -0700573
philipelc9b27d52016-07-15 06:50:27 -0700574 last_picture_id = Add<kPicIdLength>(last_picture_id, 1);
575 while (last_picture_id != picture_id) {
Yves Gerey665174f2018-06-19 15:03:05 +0200576 gof_idx = (gof_idx + 1) % gof_size;
philipel459f4e32018-03-02 10:55:12 +0100577 RTC_CHECK(gof_idx < kMaxVp9FramesInGof);
578
philipelc9b27d52016-07-15 06:50:27 -0700579 size_t temporal_idx = info->gof->temporal_idx[gof_idx];
philipel459f4e32018-03-02 10:55:12 +0100580 if (temporal_idx >= kMaxTemporalLayers) {
581 RTC_LOG(LS_WARNING) << "At most " << kMaxTemporalLayers << " temporal "
582 << "layers are supported.";
583 return;
584 }
585
philipelc9b27d52016-07-15 06:50:27 -0700586 missing_frames_for_layer_[temporal_idx].insert(last_picture_id);
587 last_picture_id = Add<kPicIdLength>(last_picture_id, 1);
philipel02447bc2016-05-13 06:01:03 -0700588 }
philipel459f4e32018-03-02 10:55:12 +0100589
philipelc9b27d52016-07-15 06:50:27 -0700590 info->last_picture_id = last_picture_id;
philipel02447bc2016-05-13 06:01:03 -0700591 } else {
592 size_t diff =
philipelc9b27d52016-07-15 06:50:27 -0700593 ForwardDiff<uint16_t, kPicIdLength>(info->gof->pid_start, picture_id);
philipel459f4e32018-03-02 10:55:12 +0100594 size_t gof_idx = diff % gof_size;
595 RTC_CHECK(gof_idx < kMaxVp9FramesInGof);
596
philipelc9b27d52016-07-15 06:50:27 -0700597 size_t temporal_idx = info->gof->temporal_idx[gof_idx];
philipel459f4e32018-03-02 10:55:12 +0100598 if (temporal_idx >= kMaxTemporalLayers) {
599 RTC_LOG(LS_WARNING) << "At most " << kMaxTemporalLayers << " temporal "
600 << "layers are supported.";
601 return;
602 }
603
philipel02447bc2016-05-13 06:01:03 -0700604 missing_frames_for_layer_[temporal_idx].erase(picture_id);
605 }
606}
607
608bool RtpFrameReferenceFinder::UpSwitchInIntervalVp9(uint16_t picture_id,
609 uint8_t temporal_idx,
610 uint16_t pid_ref) {
611 for (auto up_switch_it = up_switch_.upper_bound(pid_ref);
612 up_switch_it != up_switch_.end() &&
613 AheadOf<uint16_t, kPicIdLength>(picture_id, up_switch_it->first);
614 ++up_switch_it) {
615 if (up_switch_it->second < temporal_idx)
616 return true;
617 }
618
619 return false;
620}
621
philipelafcf7f52017-04-26 08:17:35 -0700622void RtpFrameReferenceFinder::UnwrapPictureIds(RtpFrameObject* frame) {
philipel02447bc2016-05-13 06:01:03 -0700623 for (size_t i = 0; i < frame->num_references; ++i)
philipeld4fac692017-09-04 07:03:46 -0700624 frame->references[i] = unwrapper_.Unwrap(frame->references[i]);
philipel0fa82a62018-03-19 15:34:53 +0100625 frame->id.picture_id = unwrapper_.Unwrap(frame->id.picture_id);
philipel02447bc2016-05-13 06:01:03 -0700626}
627
philipel02447bc2016-05-13 06:01:03 -0700628} // namespace video_coding
629} // namespace webrtc