philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/video_coding/rtp_frame_reference_finder.h" |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 12 | |
| 13 | #include <algorithm> |
| 14 | #include <limits> |
| 15 | |
philipel | 1a4746a | 2018-07-09 15:52:29 +0200 | [diff] [blame] | 16 | #include "absl/types/variant.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #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 Wiberg | 80ba333 | 2018-02-05 10:33:35 +0100 | [diff] [blame] | 21 | #include "rtc_base/system/fallthrough.h" |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 22 | |
| 23 | namespace webrtc { |
| 24 | namespace video_coding { |
| 25 | |
| 26 | RtpFrameReferenceFinder::RtpFrameReferenceFinder( |
| 27 | OnCompleteFrameCallback* frame_callback) |
| 28 | : last_picture_id_(-1), |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 29 | current_ss_idx_(0), |
philipel | 463d301 | 2016-09-09 03:32:44 -0700 | [diff] [blame] | 30 | cleared_to_seq_num_(-1), |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 31 | frame_callback_(frame_callback) {} |
| 32 | |
Mirko Bonadei | 8fdcac3 | 2018-08-28 16:30:18 +0200 | [diff] [blame] | 33 | RtpFrameReferenceFinder::~RtpFrameReferenceFinder() = default; |
| 34 | |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 35 | void RtpFrameReferenceFinder::ManageFrame( |
| 36 | std::unique_ptr<RtpFrameObject> frame) { |
| 37 | rtc::CritScope lock(&crit_); |
philipel | 463d301 | 2016-09-09 03:32:44 -0700 | [diff] [blame] | 38 | |
| 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 | |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 45 | 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 | |
| 62 | void 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 Wiberg | 80ba333 | 2018-02-05 10:33:35 +0100 | [diff] [blame] | 77 | RTC_FALLTHROUGH(); |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 78 | case kDrop: |
| 79 | frame_it = stashed_frames_.erase(frame_it); |
| 80 | } |
| 81 | } |
| 82 | } while (complete_frame); |
| 83 | } |
| 84 | |
| 85 | RtpFrameReferenceFinder::FrameDecision |
| 86 | RtpFrameReferenceFinder::ManageFrameInternal(RtpFrameObject* frame) { |
philipel | 2837edc | 2018-10-02 13:55:47 +0200 | [diff] [blame] | 87 | absl::optional<RtpGenericFrameDescriptor> generic_descriptor = |
| 88 | frame->GetGenericFrameDescriptor(); |
| 89 | if (generic_descriptor) { |
| 90 | return ManageFrameGeneric(frame, *generic_descriptor); |
philipel | dabfcae | 2018-09-25 12:54:37 +0200 | [diff] [blame] | 91 | } |
| 92 | |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 93 | switch (frame->codec_type()) { |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 94 | case kVideoCodecVP8: |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 95 | return ManageFrameVp8(frame); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 96 | case kVideoCodecVP9: |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 97 | return ManageFrameVp9(frame); |
Sami Kalliomäki | 9882495 | 2018-08-28 14:39:21 +0200 | [diff] [blame] | 98 | default: { |
| 99 | // Use 15 first bits of frame ID as picture ID if available. |
philipel | 2837edc | 2018-10-02 13:55:47 +0200 | [diff] [blame] | 100 | absl::optional<RTPVideoHeader> video_header = frame->GetRtpVideoHeader(); |
philipel | dabfcae | 2018-09-25 12:54:37 +0200 | [diff] [blame] | 101 | 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äki | 9882495 | 2018-08-28 14:39:21 +0200 | [diff] [blame] | 106 | } |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
philipel | 9b2ce6b | 2016-07-05 05:04:46 -0700 | [diff] [blame] | 110 | void 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 | |
philipel | 463d301 | 2016-09-09 03:32:44 -0700 | [diff] [blame] | 120 | void 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 | |
philipel | 9b2ce6b | 2016-07-05 05:04:46 -0700 | [diff] [blame] | 134 | void 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 | } |
philipel | 41bb792 | 2017-02-20 07:53:23 -0800 | [diff] [blame] | 158 | |
| 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 | } |
philipel | 9b2ce6b | 2016-07-05 05:04:46 -0700 | [diff] [blame] | 168 | } |
| 169 | |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 170 | RtpFrameReferenceFinder::FrameDecision |
philipel | dabfcae | 2018-09-25 12:54:37 +0200 | [diff] [blame] | 171 | RtpFrameReferenceFinder::ManageFrameGeneric( |
| 172 | RtpFrameObject* frame, |
philipel | 2837edc | 2018-10-02 13:55:47 +0200 | [diff] [blame] | 173 | 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()) { |
philipel | dabfcae | 2018-09-25 12:54:37 +0200 | [diff] [blame] | 180 | RTC_LOG(LS_WARNING) << "Too many dependencies in generic descriptor."; |
| 181 | return kDrop; |
| 182 | } |
| 183 | |
philipel | 2837edc | 2018-10-02 13:55:47 +0200 | [diff] [blame] | 184 | frame->num_references = diffs.size(); |
| 185 | for (size_t i = 0; i < diffs.size(); ++i) |
| 186 | frame->references[i] = frame_id - diffs[i]; |
philipel | dabfcae | 2018-09-25 12:54:37 +0200 | [diff] [blame] | 187 | |
| 188 | return kHandOff; |
| 189 | } |
| 190 | |
| 191 | RtpFrameReferenceFinder::FrameDecision |
| 192 | RtpFrameReferenceFinder::ManageFramePidOrSeqNum(RtpFrameObject* frame, |
| 193 | int picture_id) { |
philipel | 647998c | 2016-06-03 09:40:16 -0700 | [diff] [blame] | 194 | // 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) { |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 197 | frame->id.picture_id = unwrapper_.Unwrap(picture_id); |
Niels Möller | 8f7ce22 | 2019-03-21 15:43:58 +0100 | [diff] [blame] | 198 | frame->num_references = |
| 199 | frame->frame_type() == VideoFrameType::kVideoFrameKey ? 0 : 1; |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 200 | frame->references[0] = frame->id.picture_id - 1; |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 201 | return kHandOff; |
philipel | 647998c | 2016-06-03 09:40:16 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Niels Möller | 8f7ce22 | 2019-03-21 15:43:58 +0100 | [diff] [blame] | 204 | if (frame->frame_type() == VideoFrameType::kVideoFrameKey) { |
philipel | 9b2ce6b | 2016-07-05 05:04:46 -0700 | [diff] [blame] | 205 | last_seq_num_gop_.insert(std::make_pair( |
| 206 | frame->last_seq_num(), |
| 207 | std::make_pair(frame->last_seq_num(), frame->last_seq_num()))); |
| 208 | } |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 209 | |
| 210 | // We have received a frame but not yet a keyframe, stash this frame. |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 211 | if (last_seq_num_gop_.empty()) |
| 212 | return kStash; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 213 | |
| 214 | // Clean up info for old keyframes but make sure to keep info |
| 215 | // for the last keyframe. |
| 216 | auto clean_to = last_seq_num_gop_.lower_bound(frame->last_seq_num() - 100); |
philipel | 41bb792 | 2017-02-20 07:53:23 -0800 | [diff] [blame] | 217 | for (auto it = last_seq_num_gop_.begin(); |
| 218 | it != clean_to && last_seq_num_gop_.size() > 1;) { |
| 219 | it = last_seq_num_gop_.erase(it); |
| 220 | } |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 221 | |
| 222 | // Find the last sequence number of the last frame for the keyframe |
| 223 | // that this frame indirectly references. |
| 224 | auto seq_num_it = last_seq_num_gop_.upper_bound(frame->last_seq_num()); |
philipel | 9b2ce6b | 2016-07-05 05:04:46 -0700 | [diff] [blame] | 225 | if (seq_num_it == last_seq_num_gop_.begin()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 226 | RTC_LOG(LS_WARNING) << "Generic frame with packet range [" |
| 227 | << frame->first_seq_num() << ", " |
| 228 | << frame->last_seq_num() |
| 229 | << "] has no GoP, dropping frame."; |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 230 | return kDrop; |
philipel | 9b2ce6b | 2016-07-05 05:04:46 -0700 | [diff] [blame] | 231 | } |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 232 | seq_num_it--; |
| 233 | |
| 234 | // Make sure the packet sequence numbers are continuous, otherwise stash |
| 235 | // this frame. |
philipel | 9b2ce6b | 2016-07-05 05:04:46 -0700 | [diff] [blame] | 236 | uint16_t last_picture_id_gop = seq_num_it->second.first; |
| 237 | uint16_t last_picture_id_with_padding_gop = seq_num_it->second.second; |
Niels Möller | 8f7ce22 | 2019-03-21 15:43:58 +0100 | [diff] [blame] | 238 | if (frame->frame_type() == VideoFrameType::kVideoFrameDelta) { |
philipel | 9b2ce6b | 2016-07-05 05:04:46 -0700 | [diff] [blame] | 239 | uint16_t prev_seq_num = frame->first_seq_num() - 1; |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 240 | |
| 241 | if (prev_seq_num != last_picture_id_with_padding_gop) |
| 242 | return kStash; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | RTC_DCHECK(AheadOrAt(frame->last_seq_num(), seq_num_it->first)); |
| 246 | |
| 247 | // Since keyframes can cause reordering we can't simply assign the |
| 248 | // picture id according to some incrementing counter. |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 249 | frame->id.picture_id = frame->last_seq_num(); |
Niels Möller | 8f7ce22 | 2019-03-21 15:43:58 +0100 | [diff] [blame] | 250 | frame->num_references = |
| 251 | frame->frame_type() == VideoFrameType::kVideoFrameDelta; |
philipel | dabfcae | 2018-09-25 12:54:37 +0200 | [diff] [blame] | 252 | frame->references[0] = rtp_seq_num_unwrapper_.Unwrap(last_picture_id_gop); |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 253 | if (AheadOf<uint16_t>(frame->id.picture_id, last_picture_id_gop)) { |
| 254 | seq_num_it->second.first = frame->id.picture_id; |
| 255 | seq_num_it->second.second = frame->id.picture_id; |
philipel | 9b2ce6b | 2016-07-05 05:04:46 -0700 | [diff] [blame] | 256 | } |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 257 | |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 258 | last_picture_id_ = frame->id.picture_id; |
| 259 | UpdateLastPictureIdWithPadding(frame->id.picture_id); |
philipel | dabfcae | 2018-09-25 12:54:37 +0200 | [diff] [blame] | 260 | frame->id.picture_id = rtp_seq_num_unwrapper_.Unwrap(frame->id.picture_id); |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 261 | return kHandOff; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 262 | } |
| 263 | |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 264 | RtpFrameReferenceFinder::FrameDecision RtpFrameReferenceFinder::ManageFrameVp8( |
| 265 | RtpFrameObject* frame) { |
philipel | 5470f40 | 2018-09-07 13:38:53 +0200 | [diff] [blame] | 266 | absl::optional<RTPVideoHeader> video_header = frame->GetRtpVideoHeader(); |
Sami Kalliomäki | 9882495 | 2018-08-28 14:39:21 +0200 | [diff] [blame] | 267 | if (!video_header) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 268 | RTC_LOG(LS_WARNING) |
| 269 | << "Failed to get codec header from frame, dropping frame."; |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 270 | return kDrop; |
philipel | d4fac69 | 2017-09-04 07:03:46 -0700 | [diff] [blame] | 271 | } |
Sami Kalliomäki | 9882495 | 2018-08-28 14:39:21 +0200 | [diff] [blame] | 272 | RTPVideoTypeHeader rtp_codec_header = video_header->video_type_header; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 273 | |
philipel | 1a4746a | 2018-07-09 15:52:29 +0200 | [diff] [blame] | 274 | const RTPVideoHeaderVP8& codec_header = |
Sami Kalliomäki | 9882495 | 2018-08-28 14:39:21 +0200 | [diff] [blame] | 275 | absl::get<RTPVideoHeaderVP8>(rtp_codec_header); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 276 | |
| 277 | if (codec_header.pictureId == kNoPictureId || |
| 278 | codec_header.temporalIdx == kNoTemporalIdx || |
| 279 | codec_header.tl0PicIdx == kNoTl0PicIdx) { |
Mirko Bonadei | 05cf6be | 2019-01-31 21:38:12 +0100 | [diff] [blame] | 280 | return ManageFramePidOrSeqNum(frame, codec_header.pictureId); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 281 | } |
| 282 | |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 283 | frame->id.picture_id = codec_header.pictureId % kPicIdLength; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 284 | |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 285 | if (last_picture_id_ == -1) |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 286 | last_picture_id_ = frame->id.picture_id; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 287 | |
| 288 | // Find if there has been a gap in fully received frames and save the picture |
| 289 | // id of those frames in |not_yet_received_frames_|. |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 290 | if (AheadOf<uint16_t, kPicIdLength>(frame->id.picture_id, last_picture_id_)) { |
philipel | 9bd1d66 | 2017-07-14 04:52:01 -0700 | [diff] [blame] | 291 | do { |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 292 | last_picture_id_ = Add<kPicIdLength>(last_picture_id_, 1); |
philipel | 9bd1d66 | 2017-07-14 04:52:01 -0700 | [diff] [blame] | 293 | not_yet_received_frames_.insert(last_picture_id_); |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 294 | } while (last_picture_id_ != frame->id.picture_id); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 295 | } |
| 296 | |
philipel | 57ec685 | 2018-07-03 18:09:32 +0200 | [diff] [blame] | 297 | int64_t unwrapped_tl0 = tl0_unwrapper_.Unwrap(codec_header.tl0PicIdx); |
| 298 | |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 299 | // Clean up info for base layers that are too old. |
philipel | 57ec685 | 2018-07-03 18:09:32 +0200 | [diff] [blame] | 300 | int64_t old_tl0_pic_idx = unwrapped_tl0 - kMaxLayerInfo; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 301 | auto clean_layer_info_to = layer_info_.lower_bound(old_tl0_pic_idx); |
| 302 | layer_info_.erase(layer_info_.begin(), clean_layer_info_to); |
| 303 | |
| 304 | // Clean up info about not yet received frames that are too old. |
| 305 | uint16_t old_picture_id = |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 306 | Subtract<kPicIdLength>(frame->id.picture_id, kMaxNotYetReceivedFrames); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 307 | auto clean_frames_to = not_yet_received_frames_.lower_bound(old_picture_id); |
| 308 | not_yet_received_frames_.erase(not_yet_received_frames_.begin(), |
| 309 | clean_frames_to); |
| 310 | |
Niels Möller | 8f7ce22 | 2019-03-21 15:43:58 +0100 | [diff] [blame] | 311 | if (frame->frame_type() == VideoFrameType::kVideoFrameKey) { |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 312 | frame->num_references = 0; |
philipel | 57ec685 | 2018-07-03 18:09:32 +0200 | [diff] [blame] | 313 | layer_info_[unwrapped_tl0].fill(-1); |
| 314 | UpdateLayerInfoVp8(frame, unwrapped_tl0, codec_header.temporalIdx); |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 315 | return kHandOff; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 316 | } |
| 317 | |
philipel | 57ec685 | 2018-07-03 18:09:32 +0200 | [diff] [blame] | 318 | auto layer_info_it = layer_info_.find( |
| 319 | codec_header.temporalIdx == 0 ? unwrapped_tl0 - 1 : unwrapped_tl0); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 320 | |
| 321 | // If we don't have the base layer frame yet, stash this frame. |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 322 | if (layer_info_it == layer_info_.end()) |
| 323 | return kStash; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 324 | |
| 325 | // A non keyframe base layer frame has been received, copy the layer info |
| 326 | // from the previous base layer frame and set a reference to the previous |
| 327 | // base layer frame. |
| 328 | if (codec_header.temporalIdx == 0) { |
| 329 | layer_info_it = |
philipel | 57ec685 | 2018-07-03 18:09:32 +0200 | [diff] [blame] | 330 | layer_info_.emplace(unwrapped_tl0, layer_info_it->second).first; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 331 | frame->num_references = 1; |
| 332 | frame->references[0] = layer_info_it->second[0]; |
philipel | 57ec685 | 2018-07-03 18:09:32 +0200 | [diff] [blame] | 333 | UpdateLayerInfoVp8(frame, unwrapped_tl0, codec_header.temporalIdx); |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 334 | return kHandOff; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | // Layer sync frame, this frame only references its base layer frame. |
| 338 | if (codec_header.layerSync) { |
| 339 | frame->num_references = 1; |
| 340 | frame->references[0] = layer_info_it->second[0]; |
| 341 | |
philipel | 57ec685 | 2018-07-03 18:09:32 +0200 | [diff] [blame] | 342 | UpdateLayerInfoVp8(frame, unwrapped_tl0, codec_header.temporalIdx); |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 343 | return kHandOff; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | // Find all references for this frame. |
| 347 | frame->num_references = 0; |
| 348 | for (uint8_t layer = 0; layer <= codec_header.temporalIdx; ++layer) { |
philipel | d268d6f | 2016-09-15 13:43:13 +0200 | [diff] [blame] | 349 | // If we have not yet received a previous frame on this temporal layer, |
| 350 | // stash this frame. |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 351 | if (layer_info_it->second[layer] == -1) |
| 352 | return kStash; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 353 | |
philipel | 86b92e0 | 2016-10-24 07:11:53 -0700 | [diff] [blame] | 354 | // If the last frame on this layer is ahead of this frame it means that |
| 355 | // a layer sync frame has been received after this frame for the same |
| 356 | // base layer frame, drop this frame. |
| 357 | if (AheadOf<uint16_t, kPicIdLength>(layer_info_it->second[layer], |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 358 | frame->id.picture_id)) { |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 359 | return kDrop; |
philipel | 86b92e0 | 2016-10-24 07:11:53 -0700 | [diff] [blame] | 360 | } |
| 361 | |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 362 | // If we have not yet received a frame between this frame and the referenced |
| 363 | // frame then we have to wait for that frame to be completed first. |
| 364 | auto not_received_frame_it = |
| 365 | not_yet_received_frames_.upper_bound(layer_info_it->second[layer]); |
| 366 | if (not_received_frame_it != not_yet_received_frames_.end() && |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 367 | AheadOf<uint16_t, kPicIdLength>(frame->id.picture_id, |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 368 | *not_received_frame_it)) { |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 369 | return kStash; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 370 | } |
| 371 | |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 372 | if (!(AheadOf<uint16_t, kPicIdLength>(frame->id.picture_id, |
philipel | 57f19cc | 2017-03-07 03:54:05 -0800 | [diff] [blame] | 373 | layer_info_it->second[layer]))) { |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 374 | RTC_LOG(LS_WARNING) << "Frame with picture id " << frame->id.picture_id |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 375 | << " and packet range [" << frame->first_seq_num() |
| 376 | << ", " << frame->last_seq_num() |
| 377 | << "] already received, " |
| 378 | << " dropping frame."; |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 379 | return kDrop; |
philipel | 57f19cc | 2017-03-07 03:54:05 -0800 | [diff] [blame] | 380 | } |
| 381 | |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 382 | ++frame->num_references; |
| 383 | frame->references[layer] = layer_info_it->second[layer]; |
| 384 | } |
| 385 | |
philipel | 57ec685 | 2018-07-03 18:09:32 +0200 | [diff] [blame] | 386 | UpdateLayerInfoVp8(frame, unwrapped_tl0, codec_header.temporalIdx); |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 387 | return kHandOff; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 388 | } |
| 389 | |
philipel | 57ec685 | 2018-07-03 18:09:32 +0200 | [diff] [blame] | 390 | void RtpFrameReferenceFinder::UpdateLayerInfoVp8(RtpFrameObject* frame, |
| 391 | int64_t unwrapped_tl0, |
| 392 | uint8_t temporal_idx) { |
| 393 | auto layer_info_it = layer_info_.find(unwrapped_tl0); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 394 | |
| 395 | // Update this layer info and newer. |
| 396 | while (layer_info_it != layer_info_.end()) { |
philipel | 57ec685 | 2018-07-03 18:09:32 +0200 | [diff] [blame] | 397 | if (layer_info_it->second[temporal_idx] != -1 && |
| 398 | AheadOf<uint16_t, kPicIdLength>(layer_info_it->second[temporal_idx], |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 399 | frame->id.picture_id)) { |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 400 | // The frame was not newer, then no subsequent layer info have to be |
| 401 | // update. |
| 402 | break; |
| 403 | } |
| 404 | |
philipel | 57ec685 | 2018-07-03 18:09:32 +0200 | [diff] [blame] | 405 | layer_info_it->second[temporal_idx] = frame->id.picture_id; |
| 406 | ++unwrapped_tl0; |
| 407 | layer_info_it = layer_info_.find(unwrapped_tl0); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 408 | } |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 409 | not_yet_received_frames_.erase(frame->id.picture_id); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 410 | |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 411 | UnwrapPictureIds(frame); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 412 | } |
| 413 | |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 414 | RtpFrameReferenceFinder::FrameDecision RtpFrameReferenceFinder::ManageFrameVp9( |
| 415 | RtpFrameObject* frame) { |
philipel | 5470f40 | 2018-09-07 13:38:53 +0200 | [diff] [blame] | 416 | absl::optional<RTPVideoHeader> video_header = frame->GetRtpVideoHeader(); |
Sami Kalliomäki | 9882495 | 2018-08-28 14:39:21 +0200 | [diff] [blame] | 417 | if (!video_header) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 418 | RTC_LOG(LS_WARNING) |
| 419 | << "Failed to get codec header from frame, dropping frame."; |
philipel | 4c14009 | 2017-08-31 08:31:45 -0700 | [diff] [blame] | 420 | return kDrop; |
philipel | d4fac69 | 2017-09-04 07:03:46 -0700 | [diff] [blame] | 421 | } |
Sami Kalliomäki | 9882495 | 2018-08-28 14:39:21 +0200 | [diff] [blame] | 422 | RTPVideoTypeHeader rtp_codec_header = video_header->video_type_header; |
philipel | 4c14009 | 2017-08-31 08:31:45 -0700 | [diff] [blame] | 423 | |
philipel | 1a4746a | 2018-07-09 15:52:29 +0200 | [diff] [blame] | 424 | const RTPVideoHeaderVP9& codec_header = |
Sami Kalliomäki | 9882495 | 2018-08-28 14:39:21 +0200 | [diff] [blame] | 425 | absl::get<RTPVideoHeaderVP9>(rtp_codec_header); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 426 | |
philipel | 647998c | 2016-06-03 09:40:16 -0700 | [diff] [blame] | 427 | if (codec_header.picture_id == kNoPictureId || |
Sergey Silkin | d34a188 | 2018-08-20 16:46:05 +0200 | [diff] [blame] | 428 | codec_header.temporal_idx == kNoTemporalIdx) { |
Mirko Bonadei | 05cf6be | 2019-01-31 21:38:12 +0100 | [diff] [blame] | 429 | return ManageFramePidOrSeqNum(frame, codec_header.picture_id); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 430 | } |
| 431 | |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 432 | frame->id.spatial_layer = codec_header.spatial_idx; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 433 | frame->inter_layer_predicted = codec_header.inter_layer_predicted; |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 434 | frame->id.picture_id = codec_header.picture_id % kPicIdLength; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 435 | |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 436 | if (last_picture_id_ == -1) |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 437 | last_picture_id_ = frame->id.picture_id; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 438 | |
| 439 | if (codec_header.flexible_mode) { |
| 440 | frame->num_references = codec_header.num_ref_pics; |
| 441 | for (size_t i = 0; i < frame->num_references; ++i) { |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 442 | frame->references[i] = Subtract<kPicIdLength>(frame->id.picture_id, |
| 443 | codec_header.pid_diff[i]); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 444 | } |
| 445 | |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 446 | UnwrapPictureIds(frame); |
| 447 | return kHandOff; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 448 | } |
| 449 | |
Sergey Silkin | d34a188 | 2018-08-20 16:46:05 +0200 | [diff] [blame] | 450 | if (codec_header.tl0_pic_idx == kNoTl0PicIdx) { |
| 451 | RTC_LOG(LS_WARNING) << "TL0PICIDX is expected to be present in " |
| 452 | "non-flexible mode."; |
| 453 | return kDrop; |
| 454 | } |
| 455 | |
philipel | 1564360 | 2018-05-03 16:14:13 +0200 | [diff] [blame] | 456 | GofInfo* info; |
philipel | 57ec685 | 2018-07-03 18:09:32 +0200 | [diff] [blame] | 457 | int64_t unwrapped_tl0 = tl0_unwrapper_.Unwrap(codec_header.tl0_pic_idx); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 458 | if (codec_header.ss_data_available) { |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 459 | if (codec_header.temporal_idx != 0) { |
philipel | 1564360 | 2018-05-03 16:14:13 +0200 | [diff] [blame] | 460 | RTC_LOG(LS_WARNING) << "Received scalability structure on a non base " |
| 461 | "layer frame. Scalability structure ignored."; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 462 | } else { |
Sergey Silkin | 2f864fb | 2018-09-07 11:49:38 +0200 | [diff] [blame] | 463 | if (codec_header.gof.num_frames_in_gof > kMaxVp9FramesInGof) { |
Natalie Silvanovich | 3ea3e30 | 2018-05-16 11:03:12 -0700 | [diff] [blame] | 464 | return kDrop; |
| 465 | } |
| 466 | |
Sergey Silkin | 2f864fb | 2018-09-07 11:49:38 +0200 | [diff] [blame] | 467 | GofInfoVP9 gof = codec_header.gof; |
| 468 | if (gof.num_frames_in_gof == 0) { |
| 469 | RTC_LOG(LS_WARNING) << "Number of frames in GOF is zero. Assume " |
| 470 | "that stream has only one temporal layer."; |
| 471 | gof.SetGofInfoVP9(kTemporalStructureMode1); |
| 472 | } |
| 473 | |
| 474 | current_ss_idx_ = Add<kMaxGofSaved>(current_ss_idx_, 1); |
| 475 | scalability_structures_[current_ss_idx_] = gof; |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 476 | scalability_structures_[current_ss_idx_].pid_start = frame->id.picture_id; |
philipel | 57ec685 | 2018-07-03 18:09:32 +0200 | [diff] [blame] | 477 | gof_info_.emplace(unwrapped_tl0, |
philipel | 1564360 | 2018-05-03 16:14:13 +0200 | [diff] [blame] | 478 | GofInfo(&scalability_structures_[current_ss_idx_], |
| 479 | frame->id.picture_id)); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 480 | } |
philipel | 1564360 | 2018-05-03 16:14:13 +0200 | [diff] [blame] | 481 | |
philipel | 57ec685 | 2018-07-03 18:09:32 +0200 | [diff] [blame] | 482 | const auto gof_info_it = gof_info_.find(unwrapped_tl0); |
philipel | 1564360 | 2018-05-03 16:14:13 +0200 | [diff] [blame] | 483 | if (gof_info_it == gof_info_.end()) |
| 484 | return kStash; |
| 485 | |
| 486 | info = &gof_info_it->second; |
| 487 | |
Niels Möller | 8f7ce22 | 2019-03-21 15:43:58 +0100 | [diff] [blame] | 488 | if (frame->frame_type() == VideoFrameType::kVideoFrameKey) { |
philipel | 1564360 | 2018-05-03 16:14:13 +0200 | [diff] [blame] | 489 | frame->num_references = 0; |
| 490 | FrameReceivedVp9(frame->id.picture_id, info); |
| 491 | UnwrapPictureIds(frame); |
| 492 | return kHandOff; |
| 493 | } |
Niels Möller | 8f7ce22 | 2019-03-21 15:43:58 +0100 | [diff] [blame] | 494 | } else if (frame->frame_type() == VideoFrameType::kVideoFrameKey) { |
Ilya Nikolaevskiy | 5546aef | 2018-12-04 15:54:52 +0100 | [diff] [blame] | 495 | if (frame->id.spatial_layer == 0) { |
philipel | 1564360 | 2018-05-03 16:14:13 +0200 | [diff] [blame] | 496 | RTC_LOG(LS_WARNING) << "Received keyframe without scalability structure"; |
| 497 | return kDrop; |
| 498 | } |
Ilya Nikolaevskiy | 5546aef | 2018-12-04 15:54:52 +0100 | [diff] [blame] | 499 | const auto gof_info_it = gof_info_.find(unwrapped_tl0); |
| 500 | if (gof_info_it == gof_info_.end()) |
| 501 | return kStash; |
philipel | 1564360 | 2018-05-03 16:14:13 +0200 | [diff] [blame] | 502 | |
Ilya Nikolaevskiy | 5546aef | 2018-12-04 15:54:52 +0100 | [diff] [blame] | 503 | info = &gof_info_it->second; |
| 504 | |
Niels Möller | 8f7ce22 | 2019-03-21 15:43:58 +0100 | [diff] [blame] | 505 | if (frame->frame_type() == VideoFrameType::kVideoFrameKey) { |
Ilya Nikolaevskiy | 5546aef | 2018-12-04 15:54:52 +0100 | [diff] [blame] | 506 | frame->num_references = 0; |
| 507 | FrameReceivedVp9(frame->id.picture_id, info); |
| 508 | UnwrapPictureIds(frame); |
| 509 | return kHandOff; |
| 510 | } |
| 511 | } else { |
philipel | 57ec685 | 2018-07-03 18:09:32 +0200 | [diff] [blame] | 512 | auto gof_info_it = gof_info_.find( |
| 513 | (codec_header.temporal_idx == 0) ? unwrapped_tl0 - 1 : unwrapped_tl0); |
philipel | 1564360 | 2018-05-03 16:14:13 +0200 | [diff] [blame] | 514 | |
| 515 | // Gof info for this frame is not available yet, stash this frame. |
| 516 | if (gof_info_it == gof_info_.end()) |
| 517 | return kStash; |
| 518 | |
| 519 | if (codec_header.temporal_idx == 0) { |
philipel | 57ec685 | 2018-07-03 18:09:32 +0200 | [diff] [blame] | 520 | gof_info_it = gof_info_ |
| 521 | .emplace(unwrapped_tl0, GofInfo(gof_info_it->second.gof, |
| 522 | frame->id.picture_id)) |
| 523 | .first; |
philipel | 1564360 | 2018-05-03 16:14:13 +0200 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | info = &gof_info_it->second; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | // Clean up info for base layers that are too old. |
philipel | 57ec685 | 2018-07-03 18:09:32 +0200 | [diff] [blame] | 530 | int64_t old_tl0_pic_idx = unwrapped_tl0 - kMaxGofSaved; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 531 | auto clean_gof_info_to = gof_info_.lower_bound(old_tl0_pic_idx); |
| 532 | gof_info_.erase(gof_info_.begin(), clean_gof_info_to); |
| 533 | |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 534 | FrameReceivedVp9(frame->id.picture_id, info); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 535 | |
| 536 | // Make sure we don't miss any frame that could potentially have the |
| 537 | // up switch flag set. |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 538 | if (MissingRequiredFrameVp9(frame->id.picture_id, *info)) |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 539 | return kStash; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 540 | |
philipel | 1564360 | 2018-05-03 16:14:13 +0200 | [diff] [blame] | 541 | if (codec_header.temporal_up_switch) |
| 542 | up_switch_.emplace(frame->id.picture_id, codec_header.temporal_idx); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 543 | |
| 544 | // Clean out old info about up switch frames. |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 545 | uint16_t old_picture_id = Subtract<kPicIdLength>(frame->id.picture_id, 50); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 546 | auto up_switch_erase_to = up_switch_.lower_bound(old_picture_id); |
| 547 | up_switch_.erase(up_switch_.begin(), up_switch_erase_to); |
| 548 | |
philipel | c9b27d5 | 2016-07-15 06:50:27 -0700 | [diff] [blame] | 549 | size_t diff = ForwardDiff<uint16_t, kPicIdLength>(info->gof->pid_start, |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 550 | frame->id.picture_id); |
philipel | c9b27d5 | 2016-07-15 06:50:27 -0700 | [diff] [blame] | 551 | size_t gof_idx = diff % info->gof->num_frames_in_gof; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 552 | |
| 553 | // Populate references according to the scalability structure. |
philipel | c9b27d5 | 2016-07-15 06:50:27 -0700 | [diff] [blame] | 554 | frame->num_references = info->gof->num_ref_pics[gof_idx]; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 555 | for (size_t i = 0; i < frame->num_references; ++i) { |
philipel | c9b27d5 | 2016-07-15 06:50:27 -0700 | [diff] [blame] | 556 | frame->references[i] = Subtract<kPicIdLength>( |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 557 | frame->id.picture_id, info->gof->pid_diff[gof_idx][i]); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 558 | |
| 559 | // If this is a reference to a frame earlier than the last up switch point, |
| 560 | // then ignore this reference. |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 561 | if (UpSwitchInIntervalVp9(frame->id.picture_id, codec_header.temporal_idx, |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 562 | frame->references[i])) { |
| 563 | --frame->num_references; |
| 564 | } |
| 565 | } |
| 566 | |
Ilya Nikolaevskiy | 2ec0c65 | 2019-01-18 11:56:48 +0100 | [diff] [blame] | 567 | // Override GOF references. |
| 568 | if (!codec_header.inter_pic_predicted) { |
| 569 | frame->num_references = 0; |
| 570 | } |
| 571 | |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 572 | UnwrapPictureIds(frame); |
| 573 | return kHandOff; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | bool RtpFrameReferenceFinder::MissingRequiredFrameVp9(uint16_t picture_id, |
philipel | c9b27d5 | 2016-07-15 06:50:27 -0700 | [diff] [blame] | 577 | const GofInfo& info) { |
| 578 | size_t diff = |
| 579 | ForwardDiff<uint16_t, kPicIdLength>(info.gof->pid_start, picture_id); |
| 580 | size_t gof_idx = diff % info.gof->num_frames_in_gof; |
| 581 | size_t temporal_idx = info.gof->temporal_idx[gof_idx]; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 582 | |
philipel | a157e08 | 2018-05-02 15:19:01 +0200 | [diff] [blame] | 583 | if (temporal_idx >= kMaxTemporalLayers) { |
| 584 | RTC_LOG(LS_WARNING) << "At most " << kMaxTemporalLayers << " temporal " |
| 585 | << "layers are supported."; |
| 586 | return true; |
| 587 | } |
| 588 | |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 589 | // For every reference this frame has, check if there is a frame missing in |
| 590 | // the interval (|ref_pid|, |picture_id|) in any of the lower temporal |
| 591 | // layers. If so, we are missing a required frame. |
philipel | c9b27d5 | 2016-07-15 06:50:27 -0700 | [diff] [blame] | 592 | uint8_t num_references = info.gof->num_ref_pics[gof_idx]; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 593 | for (size_t i = 0; i < num_references; ++i) { |
| 594 | uint16_t ref_pid = |
philipel | c9b27d5 | 2016-07-15 06:50:27 -0700 | [diff] [blame] | 595 | Subtract<kPicIdLength>(picture_id, info.gof->pid_diff[gof_idx][i]); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 596 | for (size_t l = 0; l < temporal_idx; ++l) { |
| 597 | auto missing_frame_it = missing_frames_for_layer_[l].lower_bound(ref_pid); |
| 598 | if (missing_frame_it != missing_frames_for_layer_[l].end() && |
| 599 | AheadOf<uint16_t, kPicIdLength>(picture_id, *missing_frame_it)) { |
| 600 | return true; |
| 601 | } |
| 602 | } |
| 603 | } |
| 604 | return false; |
| 605 | } |
| 606 | |
| 607 | void RtpFrameReferenceFinder::FrameReceivedVp9(uint16_t picture_id, |
philipel | c9b27d5 | 2016-07-15 06:50:27 -0700 | [diff] [blame] | 608 | GofInfo* info) { |
| 609 | int last_picture_id = info->last_picture_id; |
philipel | 459f4e3 | 2018-03-02 10:55:12 +0100 | [diff] [blame] | 610 | size_t gof_size = std::min(info->gof->num_frames_in_gof, kMaxVp9FramesInGof); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 611 | |
| 612 | // If there is a gap, find which temporal layer the missing frames |
| 613 | // belong to and add the frame as missing for that temporal layer. |
| 614 | // Otherwise, remove this frame from the set of missing frames. |
philipel | c9b27d5 | 2016-07-15 06:50:27 -0700 | [diff] [blame] | 615 | if (AheadOf<uint16_t, kPicIdLength>(picture_id, last_picture_id)) { |
| 616 | size_t diff = ForwardDiff<uint16_t, kPicIdLength>(info->gof->pid_start, |
| 617 | last_picture_id); |
philipel | 459f4e3 | 2018-03-02 10:55:12 +0100 | [diff] [blame] | 618 | size_t gof_idx = diff % gof_size; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 619 | |
philipel | c9b27d5 | 2016-07-15 06:50:27 -0700 | [diff] [blame] | 620 | last_picture_id = Add<kPicIdLength>(last_picture_id, 1); |
| 621 | while (last_picture_id != picture_id) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 622 | gof_idx = (gof_idx + 1) % gof_size; |
philipel | 459f4e3 | 2018-03-02 10:55:12 +0100 | [diff] [blame] | 623 | RTC_CHECK(gof_idx < kMaxVp9FramesInGof); |
| 624 | |
philipel | c9b27d5 | 2016-07-15 06:50:27 -0700 | [diff] [blame] | 625 | size_t temporal_idx = info->gof->temporal_idx[gof_idx]; |
philipel | 459f4e3 | 2018-03-02 10:55:12 +0100 | [diff] [blame] | 626 | if (temporal_idx >= kMaxTemporalLayers) { |
| 627 | RTC_LOG(LS_WARNING) << "At most " << kMaxTemporalLayers << " temporal " |
| 628 | << "layers are supported."; |
| 629 | return; |
| 630 | } |
| 631 | |
philipel | c9b27d5 | 2016-07-15 06:50:27 -0700 | [diff] [blame] | 632 | missing_frames_for_layer_[temporal_idx].insert(last_picture_id); |
| 633 | last_picture_id = Add<kPicIdLength>(last_picture_id, 1); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 634 | } |
philipel | 459f4e3 | 2018-03-02 10:55:12 +0100 | [diff] [blame] | 635 | |
philipel | c9b27d5 | 2016-07-15 06:50:27 -0700 | [diff] [blame] | 636 | info->last_picture_id = last_picture_id; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 637 | } else { |
| 638 | size_t diff = |
philipel | c9b27d5 | 2016-07-15 06:50:27 -0700 | [diff] [blame] | 639 | ForwardDiff<uint16_t, kPicIdLength>(info->gof->pid_start, picture_id); |
philipel | 459f4e3 | 2018-03-02 10:55:12 +0100 | [diff] [blame] | 640 | size_t gof_idx = diff % gof_size; |
| 641 | RTC_CHECK(gof_idx < kMaxVp9FramesInGof); |
| 642 | |
philipel | c9b27d5 | 2016-07-15 06:50:27 -0700 | [diff] [blame] | 643 | size_t temporal_idx = info->gof->temporal_idx[gof_idx]; |
philipel | 459f4e3 | 2018-03-02 10:55:12 +0100 | [diff] [blame] | 644 | if (temporal_idx >= kMaxTemporalLayers) { |
| 645 | RTC_LOG(LS_WARNING) << "At most " << kMaxTemporalLayers << " temporal " |
| 646 | << "layers are supported."; |
| 647 | return; |
| 648 | } |
| 649 | |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 650 | missing_frames_for_layer_[temporal_idx].erase(picture_id); |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | bool RtpFrameReferenceFinder::UpSwitchInIntervalVp9(uint16_t picture_id, |
| 655 | uint8_t temporal_idx, |
| 656 | uint16_t pid_ref) { |
| 657 | for (auto up_switch_it = up_switch_.upper_bound(pid_ref); |
| 658 | up_switch_it != up_switch_.end() && |
| 659 | AheadOf<uint16_t, kPicIdLength>(picture_id, up_switch_it->first); |
| 660 | ++up_switch_it) { |
| 661 | if (up_switch_it->second < temporal_idx) |
| 662 | return true; |
| 663 | } |
| 664 | |
| 665 | return false; |
| 666 | } |
| 667 | |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 668 | void RtpFrameReferenceFinder::UnwrapPictureIds(RtpFrameObject* frame) { |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 669 | for (size_t i = 0; i < frame->num_references; ++i) |
philipel | d4fac69 | 2017-09-04 07:03:46 -0700 | [diff] [blame] | 670 | frame->references[i] = unwrapper_.Unwrap(frame->references[i]); |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 671 | frame->id.picture_id = unwrapper_.Unwrap(frame->id.picture_id); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 672 | } |
| 673 | |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 674 | } // namespace video_coding |
| 675 | } // namespace webrtc |