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) |
philipel | 7acc4a4 | 2019-09-26 11:25:52 +0200 | [diff] [blame] | 28 | : RtpFrameReferenceFinder(frame_callback, 0) {} |
| 29 | |
| 30 | RtpFrameReferenceFinder::RtpFrameReferenceFinder( |
| 31 | OnCompleteFrameCallback* frame_callback, |
| 32 | int64_t picture_id_offset) |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 33 | : last_picture_id_(-1), |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 34 | current_ss_idx_(0), |
philipel | 463d301 | 2016-09-09 03:32:44 -0700 | [diff] [blame] | 35 | cleared_to_seq_num_(-1), |
philipel | 7acc4a4 | 2019-09-26 11:25:52 +0200 | [diff] [blame] | 36 | frame_callback_(frame_callback), |
| 37 | picture_id_offset_(picture_id_offset) {} |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 38 | |
Mirko Bonadei | 8fdcac3 | 2018-08-28 16:30:18 +0200 | [diff] [blame] | 39 | RtpFrameReferenceFinder::~RtpFrameReferenceFinder() = default; |
| 40 | |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 41 | void RtpFrameReferenceFinder::ManageFrame( |
| 42 | std::unique_ptr<RtpFrameObject> frame) { |
philipel | 463d301 | 2016-09-09 03:32:44 -0700 | [diff] [blame] | 43 | // If we have cleared past this frame, drop it. |
| 44 | if (cleared_to_seq_num_ != -1 && |
| 45 | AheadOf<uint16_t>(cleared_to_seq_num_, frame->first_seq_num())) { |
| 46 | return; |
| 47 | } |
| 48 | |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 49 | FrameDecision decision = ManageFrameInternal(frame.get()); |
| 50 | |
| 51 | switch (decision) { |
| 52 | case kStash: |
| 53 | if (stashed_frames_.size() > kMaxStashedFrames) |
| 54 | stashed_frames_.pop_back(); |
| 55 | stashed_frames_.push_front(std::move(frame)); |
| 56 | break; |
| 57 | case kHandOff: |
philipel | 7acc4a4 | 2019-09-26 11:25:52 +0200 | [diff] [blame] | 58 | HandOffFrame(std::move(frame)); |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 59 | RetryStashedFrames(); |
| 60 | break; |
| 61 | case kDrop: |
| 62 | break; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | void RtpFrameReferenceFinder::RetryStashedFrames() { |
| 67 | bool complete_frame = false; |
| 68 | do { |
| 69 | complete_frame = false; |
| 70 | for (auto frame_it = stashed_frames_.begin(); |
| 71 | frame_it != stashed_frames_.end();) { |
| 72 | FrameDecision decision = ManageFrameInternal(frame_it->get()); |
| 73 | |
| 74 | switch (decision) { |
| 75 | case kStash: |
| 76 | ++frame_it; |
| 77 | break; |
| 78 | case kHandOff: |
| 79 | complete_frame = true; |
philipel | 7acc4a4 | 2019-09-26 11:25:52 +0200 | [diff] [blame] | 80 | HandOffFrame(std::move(*frame_it)); |
Karl Wiberg | 80ba333 | 2018-02-05 10:33:35 +0100 | [diff] [blame] | 81 | RTC_FALLTHROUGH(); |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 82 | case kDrop: |
| 83 | frame_it = stashed_frames_.erase(frame_it); |
| 84 | } |
| 85 | } |
| 86 | } while (complete_frame); |
| 87 | } |
| 88 | |
philipel | 7acc4a4 | 2019-09-26 11:25:52 +0200 | [diff] [blame] | 89 | void RtpFrameReferenceFinder::HandOffFrame( |
| 90 | std::unique_ptr<RtpFrameObject> frame) { |
| 91 | frame->id.picture_id += picture_id_offset_; |
| 92 | for (size_t i = 0; i < frame->num_references; ++i) { |
| 93 | frame->references[i] += picture_id_offset_; |
| 94 | } |
| 95 | |
| 96 | frame_callback_->OnCompleteFrame(std::move(frame)); |
| 97 | } |
| 98 | |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 99 | RtpFrameReferenceFinder::FrameDecision |
| 100 | RtpFrameReferenceFinder::ManageFrameInternal(RtpFrameObject* frame) { |
philipel | 2837edc | 2018-10-02 13:55:47 +0200 | [diff] [blame] | 101 | absl::optional<RtpGenericFrameDescriptor> generic_descriptor = |
| 102 | frame->GetGenericFrameDescriptor(); |
| 103 | if (generic_descriptor) { |
| 104 | return ManageFrameGeneric(frame, *generic_descriptor); |
philipel | dabfcae | 2018-09-25 12:54:37 +0200 | [diff] [blame] | 105 | } |
| 106 | |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 107 | switch (frame->codec_type()) { |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 108 | case kVideoCodecVP8: |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 109 | return ManageFrameVp8(frame); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 110 | case kVideoCodecVP9: |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 111 | return ManageFrameVp9(frame); |
Johnny Lee | bc7f41b | 2019-05-01 14:41:32 -0400 | [diff] [blame] | 112 | case kVideoCodecH264: |
| 113 | return ManageFrameH264(frame); |
Sami Kalliomäki | 9882495 | 2018-08-28 14:39:21 +0200 | [diff] [blame] | 114 | default: { |
| 115 | // Use 15 first bits of frame ID as picture ID if available. |
Johannes Kron | a370556 | 2019-08-26 16:37:11 +0200 | [diff] [blame] | 116 | const RTPVideoHeader& video_header = frame->GetRtpVideoHeader(); |
philipel | dabfcae | 2018-09-25 12:54:37 +0200 | [diff] [blame] | 117 | int picture_id = kNoPictureId; |
Johannes Kron | a370556 | 2019-08-26 16:37:11 +0200 | [diff] [blame] | 118 | if (video_header.generic) |
| 119 | picture_id = video_header.generic->frame_id & 0x7fff; |
philipel | dabfcae | 2018-09-25 12:54:37 +0200 | [diff] [blame] | 120 | |
| 121 | return ManageFramePidOrSeqNum(frame, picture_id); |
Sami Kalliomäki | 9882495 | 2018-08-28 14:39:21 +0200 | [diff] [blame] | 122 | } |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
philipel | 9b2ce6b | 2016-07-05 05:04:46 -0700 | [diff] [blame] | 126 | void RtpFrameReferenceFinder::PaddingReceived(uint16_t seq_num) { |
philipel | 9b2ce6b | 2016-07-05 05:04:46 -0700 | [diff] [blame] | 127 | auto clean_padding_to = |
| 128 | stashed_padding_.lower_bound(seq_num - kMaxPaddingAge); |
| 129 | stashed_padding_.erase(stashed_padding_.begin(), clean_padding_to); |
| 130 | stashed_padding_.insert(seq_num); |
| 131 | UpdateLastPictureIdWithPadding(seq_num); |
| 132 | RetryStashedFrames(); |
| 133 | } |
| 134 | |
philipel | 463d301 | 2016-09-09 03:32:44 -0700 | [diff] [blame] | 135 | void RtpFrameReferenceFinder::ClearTo(uint16_t seq_num) { |
philipel | 463d301 | 2016-09-09 03:32:44 -0700 | [diff] [blame] | 136 | cleared_to_seq_num_ = seq_num; |
| 137 | |
| 138 | auto it = stashed_frames_.begin(); |
| 139 | while (it != stashed_frames_.end()) { |
| 140 | if (AheadOf<uint16_t>(cleared_to_seq_num_, (*it)->first_seq_num())) { |
| 141 | it = stashed_frames_.erase(it); |
| 142 | } else { |
| 143 | ++it; |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
philipel | 9b2ce6b | 2016-07-05 05:04:46 -0700 | [diff] [blame] | 148 | void RtpFrameReferenceFinder::UpdateLastPictureIdWithPadding(uint16_t seq_num) { |
| 149 | auto gop_seq_num_it = last_seq_num_gop_.upper_bound(seq_num); |
| 150 | |
| 151 | // If this padding packet "belongs" to a group of pictures that we don't track |
| 152 | // anymore, do nothing. |
| 153 | if (gop_seq_num_it == last_seq_num_gop_.begin()) |
| 154 | return; |
| 155 | --gop_seq_num_it; |
| 156 | |
| 157 | // Calculate the next contiuous sequence number and search for it in |
| 158 | // the padding packets we have stashed. |
| 159 | uint16_t next_seq_num_with_padding = gop_seq_num_it->second.second + 1; |
| 160 | auto padding_seq_num_it = |
| 161 | stashed_padding_.lower_bound(next_seq_num_with_padding); |
| 162 | |
| 163 | // While there still are padding packets and those padding packets are |
| 164 | // continuous, then advance the "last-picture-id-with-padding" and remove |
| 165 | // the stashed padding packet. |
| 166 | while (padding_seq_num_it != stashed_padding_.end() && |
| 167 | *padding_seq_num_it == next_seq_num_with_padding) { |
| 168 | gop_seq_num_it->second.second = next_seq_num_with_padding; |
| 169 | ++next_seq_num_with_padding; |
| 170 | padding_seq_num_it = stashed_padding_.erase(padding_seq_num_it); |
| 171 | } |
philipel | 41bb792 | 2017-02-20 07:53:23 -0800 | [diff] [blame] | 172 | |
| 173 | // In the case where the stream has been continuous without any new keyframes |
| 174 | // for a while there is a risk that new frames will appear to be older than |
| 175 | // the keyframe they belong to due to wrapping sequence number. In order |
| 176 | // to prevent this we advance the picture id of the keyframe every so often. |
| 177 | if (ForwardDiff(gop_seq_num_it->first, seq_num) > 10000) { |
| 178 | RTC_DCHECK_EQ(1ul, last_seq_num_gop_.size()); |
| 179 | last_seq_num_gop_[seq_num] = gop_seq_num_it->second; |
| 180 | last_seq_num_gop_.erase(gop_seq_num_it); |
| 181 | } |
philipel | 9b2ce6b | 2016-07-05 05:04:46 -0700 | [diff] [blame] | 182 | } |
| 183 | |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 184 | RtpFrameReferenceFinder::FrameDecision |
philipel | dabfcae | 2018-09-25 12:54:37 +0200 | [diff] [blame] | 185 | RtpFrameReferenceFinder::ManageFrameGeneric( |
| 186 | RtpFrameObject* frame, |
philipel | 2837edc | 2018-10-02 13:55:47 +0200 | [diff] [blame] | 187 | const RtpGenericFrameDescriptor& descriptor) { |
| 188 | int64_t frame_id = generic_frame_id_unwrapper_.Unwrap(descriptor.FrameId()); |
| 189 | frame->id.picture_id = frame_id; |
| 190 | frame->id.spatial_layer = descriptor.SpatialLayer(); |
| 191 | |
| 192 | rtc::ArrayView<const uint16_t> diffs = descriptor.FrameDependenciesDiffs(); |
| 193 | if (EncodedFrame::kMaxFrameReferences < diffs.size()) { |
philipel | dabfcae | 2018-09-25 12:54:37 +0200 | [diff] [blame] | 194 | RTC_LOG(LS_WARNING) << "Too many dependencies in generic descriptor."; |
| 195 | return kDrop; |
| 196 | } |
| 197 | |
philipel | 2837edc | 2018-10-02 13:55:47 +0200 | [diff] [blame] | 198 | frame->num_references = diffs.size(); |
| 199 | for (size_t i = 0; i < diffs.size(); ++i) |
| 200 | frame->references[i] = frame_id - diffs[i]; |
philipel | dabfcae | 2018-09-25 12:54:37 +0200 | [diff] [blame] | 201 | |
| 202 | return kHandOff; |
| 203 | } |
| 204 | |
| 205 | RtpFrameReferenceFinder::FrameDecision |
| 206 | RtpFrameReferenceFinder::ManageFramePidOrSeqNum(RtpFrameObject* frame, |
| 207 | int picture_id) { |
philipel | 647998c | 2016-06-03 09:40:16 -0700 | [diff] [blame] | 208 | // If |picture_id| is specified then we use that to set the frame references, |
| 209 | // otherwise we use sequence number. |
| 210 | if (picture_id != kNoPictureId) { |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 211 | frame->id.picture_id = unwrapper_.Unwrap(picture_id); |
Niels Möller | 8f7ce22 | 2019-03-21 15:43:58 +0100 | [diff] [blame] | 212 | frame->num_references = |
| 213 | frame->frame_type() == VideoFrameType::kVideoFrameKey ? 0 : 1; |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 214 | frame->references[0] = frame->id.picture_id - 1; |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 215 | return kHandOff; |
philipel | 647998c | 2016-06-03 09:40:16 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Niels Möller | 8f7ce22 | 2019-03-21 15:43:58 +0100 | [diff] [blame] | 218 | if (frame->frame_type() == VideoFrameType::kVideoFrameKey) { |
philipel | 9b2ce6b | 2016-07-05 05:04:46 -0700 | [diff] [blame] | 219 | last_seq_num_gop_.insert(std::make_pair( |
| 220 | frame->last_seq_num(), |
| 221 | std::make_pair(frame->last_seq_num(), frame->last_seq_num()))); |
| 222 | } |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 223 | |
| 224 | // We have received a frame but not yet a keyframe, stash this frame. |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 225 | if (last_seq_num_gop_.empty()) |
| 226 | return kStash; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 227 | |
| 228 | // Clean up info for old keyframes but make sure to keep info |
| 229 | // for the last keyframe. |
| 230 | 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] | 231 | for (auto it = last_seq_num_gop_.begin(); |
| 232 | it != clean_to && last_seq_num_gop_.size() > 1;) { |
| 233 | it = last_seq_num_gop_.erase(it); |
| 234 | } |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 235 | |
| 236 | // Find the last sequence number of the last frame for the keyframe |
| 237 | // that this frame indirectly references. |
| 238 | 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] | 239 | if (seq_num_it == last_seq_num_gop_.begin()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 240 | RTC_LOG(LS_WARNING) << "Generic frame with packet range [" |
| 241 | << frame->first_seq_num() << ", " |
| 242 | << frame->last_seq_num() |
| 243 | << "] has no GoP, dropping frame."; |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 244 | return kDrop; |
philipel | 9b2ce6b | 2016-07-05 05:04:46 -0700 | [diff] [blame] | 245 | } |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 246 | seq_num_it--; |
| 247 | |
| 248 | // Make sure the packet sequence numbers are continuous, otherwise stash |
| 249 | // this frame. |
philipel | 9b2ce6b | 2016-07-05 05:04:46 -0700 | [diff] [blame] | 250 | uint16_t last_picture_id_gop = seq_num_it->second.first; |
| 251 | 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] | 252 | if (frame->frame_type() == VideoFrameType::kVideoFrameDelta) { |
philipel | 9b2ce6b | 2016-07-05 05:04:46 -0700 | [diff] [blame] | 253 | uint16_t prev_seq_num = frame->first_seq_num() - 1; |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 254 | |
| 255 | if (prev_seq_num != last_picture_id_with_padding_gop) |
| 256 | return kStash; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | RTC_DCHECK(AheadOrAt(frame->last_seq_num(), seq_num_it->first)); |
| 260 | |
| 261 | // Since keyframes can cause reordering we can't simply assign the |
| 262 | // picture id according to some incrementing counter. |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 263 | frame->id.picture_id = frame->last_seq_num(); |
Niels Möller | 8f7ce22 | 2019-03-21 15:43:58 +0100 | [diff] [blame] | 264 | frame->num_references = |
| 265 | frame->frame_type() == VideoFrameType::kVideoFrameDelta; |
philipel | dabfcae | 2018-09-25 12:54:37 +0200 | [diff] [blame] | 266 | frame->references[0] = rtp_seq_num_unwrapper_.Unwrap(last_picture_id_gop); |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 267 | if (AheadOf<uint16_t>(frame->id.picture_id, last_picture_id_gop)) { |
| 268 | seq_num_it->second.first = frame->id.picture_id; |
| 269 | seq_num_it->second.second = frame->id.picture_id; |
philipel | 9b2ce6b | 2016-07-05 05:04:46 -0700 | [diff] [blame] | 270 | } |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 271 | |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 272 | last_picture_id_ = frame->id.picture_id; |
| 273 | UpdateLastPictureIdWithPadding(frame->id.picture_id); |
philipel | dabfcae | 2018-09-25 12:54:37 +0200 | [diff] [blame] | 274 | frame->id.picture_id = rtp_seq_num_unwrapper_.Unwrap(frame->id.picture_id); |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 275 | return kHandOff; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 276 | } |
| 277 | |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 278 | RtpFrameReferenceFinder::FrameDecision RtpFrameReferenceFinder::ManageFrameVp8( |
| 279 | RtpFrameObject* frame) { |
Johannes Kron | a370556 | 2019-08-26 16:37:11 +0200 | [diff] [blame] | 280 | const RTPVideoHeader& video_header = frame->GetRtpVideoHeader(); |
| 281 | RTPVideoTypeHeader rtp_codec_header = video_header.video_type_header; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 282 | |
philipel | 1a4746a | 2018-07-09 15:52:29 +0200 | [diff] [blame] | 283 | const RTPVideoHeaderVP8& codec_header = |
Sami Kalliomäki | 9882495 | 2018-08-28 14:39:21 +0200 | [diff] [blame] | 284 | absl::get<RTPVideoHeaderVP8>(rtp_codec_header); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 285 | |
| 286 | if (codec_header.pictureId == kNoPictureId || |
| 287 | codec_header.temporalIdx == kNoTemporalIdx || |
| 288 | codec_header.tl0PicIdx == kNoTl0PicIdx) { |
Mirko Bonadei | 05cf6be | 2019-01-31 21:38:12 +0100 | [diff] [blame] | 289 | return ManageFramePidOrSeqNum(frame, codec_header.pictureId); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 290 | } |
| 291 | |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 292 | frame->id.picture_id = codec_header.pictureId % kPicIdLength; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 293 | |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 294 | if (last_picture_id_ == -1) |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 295 | last_picture_id_ = frame->id.picture_id; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 296 | |
| 297 | // Find if there has been a gap in fully received frames and save the picture |
| 298 | // id of those frames in |not_yet_received_frames_|. |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 299 | if (AheadOf<uint16_t, kPicIdLength>(frame->id.picture_id, last_picture_id_)) { |
philipel | 9bd1d66 | 2017-07-14 04:52:01 -0700 | [diff] [blame] | 300 | do { |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 301 | last_picture_id_ = Add<kPicIdLength>(last_picture_id_, 1); |
philipel | 9bd1d66 | 2017-07-14 04:52:01 -0700 | [diff] [blame] | 302 | not_yet_received_frames_.insert(last_picture_id_); |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 303 | } while (last_picture_id_ != frame->id.picture_id); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 304 | } |
| 305 | |
philipel | 57ec685 | 2018-07-03 18:09:32 +0200 | [diff] [blame] | 306 | int64_t unwrapped_tl0 = tl0_unwrapper_.Unwrap(codec_header.tl0PicIdx); |
| 307 | |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 308 | // Clean up info for base layers that are too old. |
philipel | 57ec685 | 2018-07-03 18:09:32 +0200 | [diff] [blame] | 309 | int64_t old_tl0_pic_idx = unwrapped_tl0 - kMaxLayerInfo; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 310 | auto clean_layer_info_to = layer_info_.lower_bound(old_tl0_pic_idx); |
| 311 | layer_info_.erase(layer_info_.begin(), clean_layer_info_to); |
| 312 | |
| 313 | // Clean up info about not yet received frames that are too old. |
| 314 | uint16_t old_picture_id = |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 315 | Subtract<kPicIdLength>(frame->id.picture_id, kMaxNotYetReceivedFrames); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 316 | auto clean_frames_to = not_yet_received_frames_.lower_bound(old_picture_id); |
| 317 | not_yet_received_frames_.erase(not_yet_received_frames_.begin(), |
| 318 | clean_frames_to); |
| 319 | |
Niels Möller | 8f7ce22 | 2019-03-21 15:43:58 +0100 | [diff] [blame] | 320 | if (frame->frame_type() == VideoFrameType::kVideoFrameKey) { |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 321 | frame->num_references = 0; |
philipel | 57ec685 | 2018-07-03 18:09:32 +0200 | [diff] [blame] | 322 | layer_info_[unwrapped_tl0].fill(-1); |
| 323 | UpdateLayerInfoVp8(frame, unwrapped_tl0, codec_header.temporalIdx); |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 324 | return kHandOff; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 325 | } |
| 326 | |
philipel | 57ec685 | 2018-07-03 18:09:32 +0200 | [diff] [blame] | 327 | auto layer_info_it = layer_info_.find( |
| 328 | codec_header.temporalIdx == 0 ? unwrapped_tl0 - 1 : unwrapped_tl0); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 329 | |
| 330 | // If we don't have the base layer frame yet, stash this frame. |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 331 | if (layer_info_it == layer_info_.end()) |
| 332 | return kStash; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 333 | |
| 334 | // A non keyframe base layer frame has been received, copy the layer info |
| 335 | // from the previous base layer frame and set a reference to the previous |
| 336 | // base layer frame. |
| 337 | if (codec_header.temporalIdx == 0) { |
| 338 | layer_info_it = |
philipel | 57ec685 | 2018-07-03 18:09:32 +0200 | [diff] [blame] | 339 | layer_info_.emplace(unwrapped_tl0, layer_info_it->second).first; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 340 | frame->num_references = 1; |
| 341 | frame->references[0] = layer_info_it->second[0]; |
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 | // Layer sync frame, this frame only references its base layer frame. |
| 347 | if (codec_header.layerSync) { |
| 348 | frame->num_references = 1; |
| 349 | frame->references[0] = layer_info_it->second[0]; |
| 350 | |
philipel | 57ec685 | 2018-07-03 18:09:32 +0200 | [diff] [blame] | 351 | UpdateLayerInfoVp8(frame, unwrapped_tl0, codec_header.temporalIdx); |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 352 | return kHandOff; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | // Find all references for this frame. |
| 356 | frame->num_references = 0; |
| 357 | for (uint8_t layer = 0; layer <= codec_header.temporalIdx; ++layer) { |
philipel | d268d6f | 2016-09-15 13:43:13 +0200 | [diff] [blame] | 358 | // If we have not yet received a previous frame on this temporal layer, |
| 359 | // stash this frame. |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 360 | if (layer_info_it->second[layer] == -1) |
| 361 | return kStash; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 362 | |
philipel | 86b92e0 | 2016-10-24 07:11:53 -0700 | [diff] [blame] | 363 | // If the last frame on this layer is ahead of this frame it means that |
| 364 | // a layer sync frame has been received after this frame for the same |
| 365 | // base layer frame, drop this frame. |
| 366 | if (AheadOf<uint16_t, kPicIdLength>(layer_info_it->second[layer], |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 367 | frame->id.picture_id)) { |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 368 | return kDrop; |
philipel | 86b92e0 | 2016-10-24 07:11:53 -0700 | [diff] [blame] | 369 | } |
| 370 | |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 371 | // If we have not yet received a frame between this frame and the referenced |
| 372 | // frame then we have to wait for that frame to be completed first. |
| 373 | auto not_received_frame_it = |
| 374 | not_yet_received_frames_.upper_bound(layer_info_it->second[layer]); |
| 375 | if (not_received_frame_it != not_yet_received_frames_.end() && |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 376 | AheadOf<uint16_t, kPicIdLength>(frame->id.picture_id, |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 377 | *not_received_frame_it)) { |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 378 | return kStash; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 379 | } |
| 380 | |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 381 | if (!(AheadOf<uint16_t, kPicIdLength>(frame->id.picture_id, |
philipel | 57f19cc | 2017-03-07 03:54:05 -0800 | [diff] [blame] | 382 | layer_info_it->second[layer]))) { |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 383 | RTC_LOG(LS_WARNING) << "Frame with picture id " << frame->id.picture_id |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 384 | << " and packet range [" << frame->first_seq_num() |
| 385 | << ", " << frame->last_seq_num() |
| 386 | << "] already received, " |
| 387 | << " dropping frame."; |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 388 | return kDrop; |
philipel | 57f19cc | 2017-03-07 03:54:05 -0800 | [diff] [blame] | 389 | } |
| 390 | |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 391 | ++frame->num_references; |
| 392 | frame->references[layer] = layer_info_it->second[layer]; |
| 393 | } |
| 394 | |
philipel | 57ec685 | 2018-07-03 18:09:32 +0200 | [diff] [blame] | 395 | UpdateLayerInfoVp8(frame, unwrapped_tl0, codec_header.temporalIdx); |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 396 | return kHandOff; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 397 | } |
| 398 | |
philipel | 57ec685 | 2018-07-03 18:09:32 +0200 | [diff] [blame] | 399 | void RtpFrameReferenceFinder::UpdateLayerInfoVp8(RtpFrameObject* frame, |
| 400 | int64_t unwrapped_tl0, |
| 401 | uint8_t temporal_idx) { |
| 402 | auto layer_info_it = layer_info_.find(unwrapped_tl0); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 403 | |
| 404 | // Update this layer info and newer. |
| 405 | while (layer_info_it != layer_info_.end()) { |
philipel | 57ec685 | 2018-07-03 18:09:32 +0200 | [diff] [blame] | 406 | if (layer_info_it->second[temporal_idx] != -1 && |
| 407 | AheadOf<uint16_t, kPicIdLength>(layer_info_it->second[temporal_idx], |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 408 | frame->id.picture_id)) { |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 409 | // The frame was not newer, then no subsequent layer info have to be |
| 410 | // update. |
| 411 | break; |
| 412 | } |
| 413 | |
philipel | 57ec685 | 2018-07-03 18:09:32 +0200 | [diff] [blame] | 414 | layer_info_it->second[temporal_idx] = frame->id.picture_id; |
| 415 | ++unwrapped_tl0; |
| 416 | layer_info_it = layer_info_.find(unwrapped_tl0); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 417 | } |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 418 | not_yet_received_frames_.erase(frame->id.picture_id); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 419 | |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 420 | UnwrapPictureIds(frame); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 421 | } |
| 422 | |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 423 | RtpFrameReferenceFinder::FrameDecision RtpFrameReferenceFinder::ManageFrameVp9( |
| 424 | RtpFrameObject* frame) { |
Johannes Kron | a370556 | 2019-08-26 16:37:11 +0200 | [diff] [blame] | 425 | const RTPVideoHeader& video_header = frame->GetRtpVideoHeader(); |
| 426 | RTPVideoTypeHeader rtp_codec_header = video_header.video_type_header; |
philipel | 4c14009 | 2017-08-31 08:31:45 -0700 | [diff] [blame] | 427 | |
philipel | 1a4746a | 2018-07-09 15:52:29 +0200 | [diff] [blame] | 428 | const RTPVideoHeaderVP9& codec_header = |
Sami Kalliomäki | 9882495 | 2018-08-28 14:39:21 +0200 | [diff] [blame] | 429 | absl::get<RTPVideoHeaderVP9>(rtp_codec_header); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 430 | |
philipel | 647998c | 2016-06-03 09:40:16 -0700 | [diff] [blame] | 431 | if (codec_header.picture_id == kNoPictureId || |
Sergey Silkin | d34a188 | 2018-08-20 16:46:05 +0200 | [diff] [blame] | 432 | codec_header.temporal_idx == kNoTemporalIdx) { |
Mirko Bonadei | 05cf6be | 2019-01-31 21:38:12 +0100 | [diff] [blame] | 433 | return ManageFramePidOrSeqNum(frame, codec_header.picture_id); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 434 | } |
| 435 | |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 436 | frame->id.spatial_layer = codec_header.spatial_idx; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 437 | frame->inter_layer_predicted = codec_header.inter_layer_predicted; |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 438 | frame->id.picture_id = codec_header.picture_id % kPicIdLength; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 439 | |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 440 | if (last_picture_id_ == -1) |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 441 | last_picture_id_ = frame->id.picture_id; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 442 | |
| 443 | if (codec_header.flexible_mode) { |
| 444 | frame->num_references = codec_header.num_ref_pics; |
| 445 | for (size_t i = 0; i < frame->num_references; ++i) { |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 446 | frame->references[i] = Subtract<kPicIdLength>(frame->id.picture_id, |
| 447 | codec_header.pid_diff[i]); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 448 | } |
| 449 | |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 450 | UnwrapPictureIds(frame); |
| 451 | return kHandOff; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 452 | } |
| 453 | |
Sergey Silkin | d34a188 | 2018-08-20 16:46:05 +0200 | [diff] [blame] | 454 | if (codec_header.tl0_pic_idx == kNoTl0PicIdx) { |
| 455 | RTC_LOG(LS_WARNING) << "TL0PICIDX is expected to be present in " |
| 456 | "non-flexible mode."; |
| 457 | return kDrop; |
| 458 | } |
| 459 | |
philipel | 1564360 | 2018-05-03 16:14:13 +0200 | [diff] [blame] | 460 | GofInfo* info; |
philipel | 57ec685 | 2018-07-03 18:09:32 +0200 | [diff] [blame] | 461 | int64_t unwrapped_tl0 = tl0_unwrapper_.Unwrap(codec_header.tl0_pic_idx); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 462 | if (codec_header.ss_data_available) { |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 463 | if (codec_header.temporal_idx != 0) { |
philipel | 1564360 | 2018-05-03 16:14:13 +0200 | [diff] [blame] | 464 | RTC_LOG(LS_WARNING) << "Received scalability structure on a non base " |
| 465 | "layer frame. Scalability structure ignored."; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 466 | } else { |
Sergey Silkin | 2f864fb | 2018-09-07 11:49:38 +0200 | [diff] [blame] | 467 | if (codec_header.gof.num_frames_in_gof > kMaxVp9FramesInGof) { |
Natalie Silvanovich | 3ea3e30 | 2018-05-16 11:03:12 -0700 | [diff] [blame] | 468 | return kDrop; |
| 469 | } |
| 470 | |
Sergey Silkin | 2f864fb | 2018-09-07 11:49:38 +0200 | [diff] [blame] | 471 | GofInfoVP9 gof = codec_header.gof; |
| 472 | if (gof.num_frames_in_gof == 0) { |
| 473 | RTC_LOG(LS_WARNING) << "Number of frames in GOF is zero. Assume " |
| 474 | "that stream has only one temporal layer."; |
| 475 | gof.SetGofInfoVP9(kTemporalStructureMode1); |
| 476 | } |
| 477 | |
| 478 | current_ss_idx_ = Add<kMaxGofSaved>(current_ss_idx_, 1); |
| 479 | scalability_structures_[current_ss_idx_] = gof; |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 480 | scalability_structures_[current_ss_idx_].pid_start = frame->id.picture_id; |
philipel | 57ec685 | 2018-07-03 18:09:32 +0200 | [diff] [blame] | 481 | gof_info_.emplace(unwrapped_tl0, |
philipel | 1564360 | 2018-05-03 16:14:13 +0200 | [diff] [blame] | 482 | GofInfo(&scalability_structures_[current_ss_idx_], |
| 483 | frame->id.picture_id)); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 484 | } |
philipel | 1564360 | 2018-05-03 16:14:13 +0200 | [diff] [blame] | 485 | |
philipel | 57ec685 | 2018-07-03 18:09:32 +0200 | [diff] [blame] | 486 | const auto gof_info_it = gof_info_.find(unwrapped_tl0); |
philipel | 1564360 | 2018-05-03 16:14:13 +0200 | [diff] [blame] | 487 | if (gof_info_it == gof_info_.end()) |
| 488 | return kStash; |
| 489 | |
| 490 | info = &gof_info_it->second; |
| 491 | |
Niels Möller | 8f7ce22 | 2019-03-21 15:43:58 +0100 | [diff] [blame] | 492 | if (frame->frame_type() == VideoFrameType::kVideoFrameKey) { |
philipel | 1564360 | 2018-05-03 16:14:13 +0200 | [diff] [blame] | 493 | frame->num_references = 0; |
| 494 | FrameReceivedVp9(frame->id.picture_id, info); |
| 495 | UnwrapPictureIds(frame); |
| 496 | return kHandOff; |
| 497 | } |
Niels Möller | 8f7ce22 | 2019-03-21 15:43:58 +0100 | [diff] [blame] | 498 | } else if (frame->frame_type() == VideoFrameType::kVideoFrameKey) { |
Ilya Nikolaevskiy | 5546aef | 2018-12-04 15:54:52 +0100 | [diff] [blame] | 499 | if (frame->id.spatial_layer == 0) { |
philipel | 1564360 | 2018-05-03 16:14:13 +0200 | [diff] [blame] | 500 | RTC_LOG(LS_WARNING) << "Received keyframe without scalability structure"; |
| 501 | return kDrop; |
| 502 | } |
Ilya Nikolaevskiy | 5546aef | 2018-12-04 15:54:52 +0100 | [diff] [blame] | 503 | const auto gof_info_it = gof_info_.find(unwrapped_tl0); |
| 504 | if (gof_info_it == gof_info_.end()) |
| 505 | return kStash; |
philipel | 1564360 | 2018-05-03 16:14:13 +0200 | [diff] [blame] | 506 | |
Ilya Nikolaevskiy | 5546aef | 2018-12-04 15:54:52 +0100 | [diff] [blame] | 507 | info = &gof_info_it->second; |
| 508 | |
Niels Möller | 8f7ce22 | 2019-03-21 15:43:58 +0100 | [diff] [blame] | 509 | if (frame->frame_type() == VideoFrameType::kVideoFrameKey) { |
Ilya Nikolaevskiy | 5546aef | 2018-12-04 15:54:52 +0100 | [diff] [blame] | 510 | frame->num_references = 0; |
| 511 | FrameReceivedVp9(frame->id.picture_id, info); |
| 512 | UnwrapPictureIds(frame); |
| 513 | return kHandOff; |
| 514 | } |
| 515 | } else { |
philipel | 57ec685 | 2018-07-03 18:09:32 +0200 | [diff] [blame] | 516 | auto gof_info_it = gof_info_.find( |
| 517 | (codec_header.temporal_idx == 0) ? unwrapped_tl0 - 1 : unwrapped_tl0); |
philipel | 1564360 | 2018-05-03 16:14:13 +0200 | [diff] [blame] | 518 | |
| 519 | // Gof info for this frame is not available yet, stash this frame. |
| 520 | if (gof_info_it == gof_info_.end()) |
| 521 | return kStash; |
| 522 | |
| 523 | if (codec_header.temporal_idx == 0) { |
philipel | 57ec685 | 2018-07-03 18:09:32 +0200 | [diff] [blame] | 524 | gof_info_it = gof_info_ |
| 525 | .emplace(unwrapped_tl0, GofInfo(gof_info_it->second.gof, |
| 526 | frame->id.picture_id)) |
| 527 | .first; |
philipel | 1564360 | 2018-05-03 16:14:13 +0200 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | info = &gof_info_it->second; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | // Clean up info for base layers that are too old. |
philipel | 57ec685 | 2018-07-03 18:09:32 +0200 | [diff] [blame] | 534 | int64_t old_tl0_pic_idx = unwrapped_tl0 - kMaxGofSaved; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 535 | auto clean_gof_info_to = gof_info_.lower_bound(old_tl0_pic_idx); |
| 536 | gof_info_.erase(gof_info_.begin(), clean_gof_info_to); |
| 537 | |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 538 | FrameReceivedVp9(frame->id.picture_id, info); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 539 | |
| 540 | // Make sure we don't miss any frame that could potentially have the |
| 541 | // up switch flag set. |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 542 | if (MissingRequiredFrameVp9(frame->id.picture_id, *info)) |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 543 | return kStash; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 544 | |
philipel | 1564360 | 2018-05-03 16:14:13 +0200 | [diff] [blame] | 545 | if (codec_header.temporal_up_switch) |
| 546 | up_switch_.emplace(frame->id.picture_id, codec_header.temporal_idx); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 547 | |
| 548 | // Clean out old info about up switch frames. |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 549 | uint16_t old_picture_id = Subtract<kPicIdLength>(frame->id.picture_id, 50); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 550 | auto up_switch_erase_to = up_switch_.lower_bound(old_picture_id); |
| 551 | up_switch_.erase(up_switch_.begin(), up_switch_erase_to); |
| 552 | |
philipel | c9b27d5 | 2016-07-15 06:50:27 -0700 | [diff] [blame] | 553 | size_t diff = ForwardDiff<uint16_t, kPicIdLength>(info->gof->pid_start, |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 554 | frame->id.picture_id); |
philipel | c9b27d5 | 2016-07-15 06:50:27 -0700 | [diff] [blame] | 555 | size_t gof_idx = diff % info->gof->num_frames_in_gof; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 556 | |
| 557 | // Populate references according to the scalability structure. |
philipel | c9b27d5 | 2016-07-15 06:50:27 -0700 | [diff] [blame] | 558 | frame->num_references = info->gof->num_ref_pics[gof_idx]; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 559 | for (size_t i = 0; i < frame->num_references; ++i) { |
philipel | c9b27d5 | 2016-07-15 06:50:27 -0700 | [diff] [blame] | 560 | frame->references[i] = Subtract<kPicIdLength>( |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 561 | frame->id.picture_id, info->gof->pid_diff[gof_idx][i]); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 562 | |
| 563 | // If this is a reference to a frame earlier than the last up switch point, |
| 564 | // then ignore this reference. |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 565 | if (UpSwitchInIntervalVp9(frame->id.picture_id, codec_header.temporal_idx, |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 566 | frame->references[i])) { |
| 567 | --frame->num_references; |
| 568 | } |
| 569 | } |
| 570 | |
Ilya Nikolaevskiy | 2ec0c65 | 2019-01-18 11:56:48 +0100 | [diff] [blame] | 571 | // Override GOF references. |
| 572 | if (!codec_header.inter_pic_predicted) { |
| 573 | frame->num_references = 0; |
| 574 | } |
| 575 | |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 576 | UnwrapPictureIds(frame); |
| 577 | return kHandOff; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | bool RtpFrameReferenceFinder::MissingRequiredFrameVp9(uint16_t picture_id, |
philipel | c9b27d5 | 2016-07-15 06:50:27 -0700 | [diff] [blame] | 581 | const GofInfo& info) { |
| 582 | size_t diff = |
| 583 | ForwardDiff<uint16_t, kPicIdLength>(info.gof->pid_start, picture_id); |
| 584 | size_t gof_idx = diff % info.gof->num_frames_in_gof; |
| 585 | size_t temporal_idx = info.gof->temporal_idx[gof_idx]; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 586 | |
philipel | a157e08 | 2018-05-02 15:19:01 +0200 | [diff] [blame] | 587 | if (temporal_idx >= kMaxTemporalLayers) { |
| 588 | RTC_LOG(LS_WARNING) << "At most " << kMaxTemporalLayers << " temporal " |
| 589 | << "layers are supported."; |
| 590 | return true; |
| 591 | } |
| 592 | |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 593 | // For every reference this frame has, check if there is a frame missing in |
| 594 | // the interval (|ref_pid|, |picture_id|) in any of the lower temporal |
| 595 | // layers. If so, we are missing a required frame. |
philipel | c9b27d5 | 2016-07-15 06:50:27 -0700 | [diff] [blame] | 596 | uint8_t num_references = info.gof->num_ref_pics[gof_idx]; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 597 | for (size_t i = 0; i < num_references; ++i) { |
| 598 | uint16_t ref_pid = |
philipel | c9b27d5 | 2016-07-15 06:50:27 -0700 | [diff] [blame] | 599 | Subtract<kPicIdLength>(picture_id, info.gof->pid_diff[gof_idx][i]); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 600 | for (size_t l = 0; l < temporal_idx; ++l) { |
| 601 | auto missing_frame_it = missing_frames_for_layer_[l].lower_bound(ref_pid); |
| 602 | if (missing_frame_it != missing_frames_for_layer_[l].end() && |
| 603 | AheadOf<uint16_t, kPicIdLength>(picture_id, *missing_frame_it)) { |
| 604 | return true; |
| 605 | } |
| 606 | } |
| 607 | } |
| 608 | return false; |
| 609 | } |
| 610 | |
| 611 | void RtpFrameReferenceFinder::FrameReceivedVp9(uint16_t picture_id, |
philipel | c9b27d5 | 2016-07-15 06:50:27 -0700 | [diff] [blame] | 612 | GofInfo* info) { |
| 613 | int last_picture_id = info->last_picture_id; |
philipel | 459f4e3 | 2018-03-02 10:55:12 +0100 | [diff] [blame] | 614 | size_t gof_size = std::min(info->gof->num_frames_in_gof, kMaxVp9FramesInGof); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 615 | |
| 616 | // If there is a gap, find which temporal layer the missing frames |
| 617 | // belong to and add the frame as missing for that temporal layer. |
| 618 | // Otherwise, remove this frame from the set of missing frames. |
philipel | c9b27d5 | 2016-07-15 06:50:27 -0700 | [diff] [blame] | 619 | if (AheadOf<uint16_t, kPicIdLength>(picture_id, last_picture_id)) { |
| 620 | size_t diff = ForwardDiff<uint16_t, kPicIdLength>(info->gof->pid_start, |
| 621 | last_picture_id); |
philipel | 459f4e3 | 2018-03-02 10:55:12 +0100 | [diff] [blame] | 622 | size_t gof_idx = diff % gof_size; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 623 | |
philipel | c9b27d5 | 2016-07-15 06:50:27 -0700 | [diff] [blame] | 624 | last_picture_id = Add<kPicIdLength>(last_picture_id, 1); |
| 625 | while (last_picture_id != picture_id) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 626 | gof_idx = (gof_idx + 1) % gof_size; |
philipel | 459f4e3 | 2018-03-02 10:55:12 +0100 | [diff] [blame] | 627 | RTC_CHECK(gof_idx < kMaxVp9FramesInGof); |
| 628 | |
philipel | c9b27d5 | 2016-07-15 06:50:27 -0700 | [diff] [blame] | 629 | size_t temporal_idx = info->gof->temporal_idx[gof_idx]; |
philipel | 459f4e3 | 2018-03-02 10:55:12 +0100 | [diff] [blame] | 630 | if (temporal_idx >= kMaxTemporalLayers) { |
| 631 | RTC_LOG(LS_WARNING) << "At most " << kMaxTemporalLayers << " temporal " |
| 632 | << "layers are supported."; |
| 633 | return; |
| 634 | } |
| 635 | |
philipel | c9b27d5 | 2016-07-15 06:50:27 -0700 | [diff] [blame] | 636 | missing_frames_for_layer_[temporal_idx].insert(last_picture_id); |
| 637 | last_picture_id = Add<kPicIdLength>(last_picture_id, 1); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 638 | } |
philipel | 459f4e3 | 2018-03-02 10:55:12 +0100 | [diff] [blame] | 639 | |
philipel | c9b27d5 | 2016-07-15 06:50:27 -0700 | [diff] [blame] | 640 | info->last_picture_id = last_picture_id; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 641 | } else { |
| 642 | size_t diff = |
philipel | c9b27d5 | 2016-07-15 06:50:27 -0700 | [diff] [blame] | 643 | ForwardDiff<uint16_t, kPicIdLength>(info->gof->pid_start, picture_id); |
philipel | 459f4e3 | 2018-03-02 10:55:12 +0100 | [diff] [blame] | 644 | size_t gof_idx = diff % gof_size; |
| 645 | RTC_CHECK(gof_idx < kMaxVp9FramesInGof); |
| 646 | |
philipel | c9b27d5 | 2016-07-15 06:50:27 -0700 | [diff] [blame] | 647 | size_t temporal_idx = info->gof->temporal_idx[gof_idx]; |
philipel | 459f4e3 | 2018-03-02 10:55:12 +0100 | [diff] [blame] | 648 | if (temporal_idx >= kMaxTemporalLayers) { |
| 649 | RTC_LOG(LS_WARNING) << "At most " << kMaxTemporalLayers << " temporal " |
| 650 | << "layers are supported."; |
| 651 | return; |
| 652 | } |
| 653 | |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 654 | missing_frames_for_layer_[temporal_idx].erase(picture_id); |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | bool RtpFrameReferenceFinder::UpSwitchInIntervalVp9(uint16_t picture_id, |
| 659 | uint8_t temporal_idx, |
| 660 | uint16_t pid_ref) { |
| 661 | for (auto up_switch_it = up_switch_.upper_bound(pid_ref); |
| 662 | up_switch_it != up_switch_.end() && |
| 663 | AheadOf<uint16_t, kPicIdLength>(picture_id, up_switch_it->first); |
| 664 | ++up_switch_it) { |
| 665 | if (up_switch_it->second < temporal_idx) |
| 666 | return true; |
| 667 | } |
| 668 | |
| 669 | return false; |
| 670 | } |
| 671 | |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 672 | void RtpFrameReferenceFinder::UnwrapPictureIds(RtpFrameObject* frame) { |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 673 | for (size_t i = 0; i < frame->num_references; ++i) |
philipel | d4fac69 | 2017-09-04 07:03:46 -0700 | [diff] [blame] | 674 | frame->references[i] = unwrapper_.Unwrap(frame->references[i]); |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 675 | frame->id.picture_id = unwrapper_.Unwrap(frame->id.picture_id); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 676 | } |
| 677 | |
Johnny Lee | bc7f41b | 2019-05-01 14:41:32 -0400 | [diff] [blame] | 678 | RtpFrameReferenceFinder::FrameDecision RtpFrameReferenceFinder::ManageFrameH264( |
| 679 | RtpFrameObject* frame) { |
Johannes Kron | a370556 | 2019-08-26 16:37:11 +0200 | [diff] [blame] | 680 | const FrameMarking& rtp_frame_marking = frame->GetFrameMarking(); |
Johnny Lee | bc7f41b | 2019-05-01 14:41:32 -0400 | [diff] [blame] | 681 | |
Johannes Kron | a370556 | 2019-08-26 16:37:11 +0200 | [diff] [blame] | 682 | uint8_t tid = rtp_frame_marking.temporal_id; |
| 683 | bool blSync = rtp_frame_marking.base_layer_sync; |
Johnny Lee | bc7f41b | 2019-05-01 14:41:32 -0400 | [diff] [blame] | 684 | |
| 685 | if (tid == kNoTemporalIdx) |
| 686 | return ManageFramePidOrSeqNum(std::move(frame), kNoPictureId); |
| 687 | |
| 688 | frame->id.picture_id = frame->last_seq_num(); |
| 689 | |
| 690 | if (frame->frame_type() == VideoFrameType::kVideoFrameKey) { |
| 691 | // For H264, use last_seq_num_gop_ to simply store last picture id |
| 692 | // as a pair of unpadded and padded sequence numbers. |
| 693 | if (last_seq_num_gop_.empty()) { |
| 694 | last_seq_num_gop_.insert(std::make_pair( |
| 695 | 0, std::make_pair(frame->id.picture_id, frame->id.picture_id))); |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | // Stash if we have no keyframe yet. |
| 700 | if (last_seq_num_gop_.empty()) |
| 701 | return kStash; |
| 702 | |
| 703 | // Check for gap in sequence numbers. Store in |not_yet_received_seq_num_|. |
| 704 | if (frame->frame_type() == VideoFrameType::kVideoFrameDelta) { |
| 705 | uint16_t last_pic_id_padded = last_seq_num_gop_.begin()->second.second; |
| 706 | if (AheadOf<uint16_t>(frame->id.picture_id, last_pic_id_padded)) { |
| 707 | do { |
| 708 | last_pic_id_padded = last_pic_id_padded + 1; |
| 709 | not_yet_received_seq_num_.insert(last_pic_id_padded); |
| 710 | } while (last_pic_id_padded != frame->id.picture_id); |
| 711 | } |
| 712 | } |
| 713 | |
Johannes Kron | a370556 | 2019-08-26 16:37:11 +0200 | [diff] [blame] | 714 | int64_t unwrapped_tl0 = tl0_unwrapper_.Unwrap(rtp_frame_marking.tl0_pic_idx); |
Johnny Lee | bc7f41b | 2019-05-01 14:41:32 -0400 | [diff] [blame] | 715 | |
| 716 | // Clean up info for base layers that are too old. |
| 717 | int64_t old_tl0_pic_idx = unwrapped_tl0 - kMaxLayerInfo; |
| 718 | auto clean_layer_info_to = layer_info_.lower_bound(old_tl0_pic_idx); |
| 719 | layer_info_.erase(layer_info_.begin(), clean_layer_info_to); |
| 720 | |
| 721 | // Clean up info about not yet received frames that are too old. |
| 722 | uint16_t old_picture_id = frame->id.picture_id - kMaxNotYetReceivedFrames * 2; |
| 723 | auto clean_frames_to = not_yet_received_seq_num_.lower_bound(old_picture_id); |
| 724 | not_yet_received_seq_num_.erase(not_yet_received_seq_num_.begin(), |
| 725 | clean_frames_to); |
| 726 | |
| 727 | if (frame->frame_type() == VideoFrameType::kVideoFrameKey) { |
| 728 | frame->num_references = 0; |
| 729 | layer_info_[unwrapped_tl0].fill(-1); |
| 730 | UpdateDataH264(frame, unwrapped_tl0, tid); |
| 731 | return kHandOff; |
| 732 | } |
| 733 | |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 734 | auto layer_info_it = |
| 735 | layer_info_.find(tid == 0 ? unwrapped_tl0 - 1 : unwrapped_tl0); |
Johnny Lee | bc7f41b | 2019-05-01 14:41:32 -0400 | [diff] [blame] | 736 | |
| 737 | // Stash if we have no base layer frame yet. |
| 738 | if (layer_info_it == layer_info_.end()) |
| 739 | return kStash; |
| 740 | |
| 741 | // Base layer frame. Copy layer info from previous base layer frame. |
| 742 | if (tid == 0) { |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 743 | layer_info_it = |
| 744 | layer_info_.insert(std::make_pair(unwrapped_tl0, layer_info_it->second)) |
| 745 | .first; |
Johnny Lee | bc7f41b | 2019-05-01 14:41:32 -0400 | [diff] [blame] | 746 | frame->num_references = 1; |
| 747 | frame->references[0] = layer_info_it->second[0]; |
| 748 | UpdateDataH264(frame, unwrapped_tl0, tid); |
| 749 | return kHandOff; |
| 750 | } |
| 751 | |
| 752 | // This frame only references its base layer frame. |
| 753 | if (blSync) { |
| 754 | frame->num_references = 1; |
| 755 | frame->references[0] = layer_info_it->second[0]; |
| 756 | UpdateDataH264(frame, unwrapped_tl0, tid); |
| 757 | return kHandOff; |
| 758 | } |
| 759 | |
| 760 | // Find all references for general frame. |
| 761 | frame->num_references = 0; |
| 762 | for (uint8_t layer = 0; layer <= tid; ++layer) { |
| 763 | // Stash if we have not yet received frames on this temporal layer. |
| 764 | if (layer_info_it->second[layer] == -1) |
| 765 | return kStash; |
| 766 | |
| 767 | // Drop if the last frame on this layer is ahead of this frame. A layer sync |
| 768 | // frame was received after this frame for the same base layer frame. |
| 769 | uint16_t last_frame_in_layer = layer_info_it->second[layer]; |
| 770 | if (AheadOf<uint16_t>(last_frame_in_layer, frame->id.picture_id)) |
| 771 | return kDrop; |
| 772 | |
| 773 | // Stash and wait for missing frame between this frame and the reference |
| 774 | auto not_received_seq_num_it = |
| 775 | not_yet_received_seq_num_.upper_bound(last_frame_in_layer); |
| 776 | if (not_received_seq_num_it != not_yet_received_seq_num_.end() && |
| 777 | AheadOf<uint16_t>(frame->id.picture_id, *not_received_seq_num_it)) { |
| 778 | return kStash; |
| 779 | } |
| 780 | |
| 781 | if (!(AheadOf<uint16_t>(frame->id.picture_id, last_frame_in_layer))) { |
| 782 | RTC_LOG(LS_WARNING) << "Frame with picture id " << frame->id.picture_id |
| 783 | << " and packet range [" << frame->first_seq_num() |
| 784 | << ", " << frame->last_seq_num() |
| 785 | << "] already received, " |
| 786 | << " dropping frame."; |
| 787 | return kDrop; |
| 788 | } |
| 789 | |
| 790 | ++frame->num_references; |
| 791 | frame->references[layer] = last_frame_in_layer; |
| 792 | } |
| 793 | |
| 794 | UpdateDataH264(frame, unwrapped_tl0, tid); |
| 795 | return kHandOff; |
| 796 | } |
| 797 | |
| 798 | void RtpFrameReferenceFinder::UpdateLastPictureIdWithPaddingH264() { |
| 799 | auto seq_num_it = last_seq_num_gop_.begin(); |
| 800 | |
| 801 | // Check if next sequence number is in a stashed padding packet. |
| 802 | uint16_t next_padded_seq_num = seq_num_it->second.second + 1; |
| 803 | auto padding_seq_num_it = stashed_padding_.lower_bound(next_padded_seq_num); |
| 804 | |
| 805 | // Check for more consecutive padding packets to increment |
| 806 | // the "last-picture-id-with-padding" and remove the stashed packets. |
| 807 | while (padding_seq_num_it != stashed_padding_.end() && |
| 808 | *padding_seq_num_it == next_padded_seq_num) { |
| 809 | seq_num_it->second.second = next_padded_seq_num; |
| 810 | ++next_padded_seq_num; |
| 811 | padding_seq_num_it = stashed_padding_.erase(padding_seq_num_it); |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | void RtpFrameReferenceFinder::UpdateLayerInfoH264(RtpFrameObject* frame, |
| 816 | int64_t unwrapped_tl0, |
| 817 | uint8_t temporal_idx) { |
| 818 | auto layer_info_it = layer_info_.find(unwrapped_tl0); |
| 819 | |
| 820 | // Update this layer info and newer. |
| 821 | while (layer_info_it != layer_info_.end()) { |
| 822 | if (layer_info_it->second[temporal_idx] != -1 && |
| 823 | AheadOf<uint16_t>(layer_info_it->second[temporal_idx], |
| 824 | frame->id.picture_id)) { |
| 825 | // Not a newer frame. No subsequent layer info needs update. |
| 826 | break; |
| 827 | } |
| 828 | |
| 829 | layer_info_it->second[temporal_idx] = frame->id.picture_id; |
| 830 | ++unwrapped_tl0; |
| 831 | layer_info_it = layer_info_.find(unwrapped_tl0); |
| 832 | } |
| 833 | |
| 834 | for (size_t i = 0; i < frame->num_references; ++i) |
| 835 | frame->references[i] = rtp_seq_num_unwrapper_.Unwrap(frame->references[i]); |
| 836 | frame->id.picture_id = rtp_seq_num_unwrapper_.Unwrap(frame->id.picture_id); |
| 837 | } |
| 838 | |
| 839 | void RtpFrameReferenceFinder::UpdateDataH264(RtpFrameObject* frame, |
| 840 | int64_t unwrapped_tl0, |
| 841 | uint8_t temporal_idx) { |
| 842 | // Update last_seq_num_gop_ entry for last picture id. |
| 843 | auto seq_num_it = last_seq_num_gop_.begin(); |
| 844 | uint16_t last_pic_id = seq_num_it->second.first; |
| 845 | if (AheadOf<uint16_t>(frame->id.picture_id, last_pic_id)) { |
| 846 | seq_num_it->second.first = frame->id.picture_id; |
| 847 | seq_num_it->second.second = frame->id.picture_id; |
| 848 | } |
| 849 | UpdateLastPictureIdWithPaddingH264(); |
| 850 | |
| 851 | UpdateLayerInfoH264(frame, unwrapped_tl0, temporal_idx); |
| 852 | |
| 853 | // Remove any current packets from |not_yet_received_seq_num_|. |
| 854 | uint16_t last_seq_num_padded = seq_num_it->second.second; |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 855 | for (uint16_t n = frame->first_seq_num(); AheadOrAt(last_seq_num_padded, n); |
| 856 | ++n) { |
Johnny Lee | bc7f41b | 2019-05-01 14:41:32 -0400 | [diff] [blame] | 857 | not_yet_received_seq_num_.erase(n); |
| 858 | } |
| 859 | } |
| 860 | |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 861 | } // namespace video_coding |
| 862 | } // namespace webrtc |