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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "modules/video_coding/frame_object.h" |
| 17 | #include "modules/video_coding/packet_buffer.h" |
| 18 | #include "rtc_base/checks.h" |
| 19 | #include "rtc_base/logging.h" |
Karl Wiberg | 80ba333 | 2018-02-05 10:33:35 +0100 | [diff] [blame] | 20 | #include "rtc_base/system/fallthrough.h" |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 21 | |
| 22 | namespace webrtc { |
| 23 | namespace video_coding { |
| 24 | |
| 25 | RtpFrameReferenceFinder::RtpFrameReferenceFinder( |
| 26 | OnCompleteFrameCallback* frame_callback) |
| 27 | : last_picture_id_(-1), |
| 28 | last_unwrap_(-1), |
| 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 | |
| 33 | void RtpFrameReferenceFinder::ManageFrame( |
| 34 | std::unique_ptr<RtpFrameObject> frame) { |
| 35 | rtc::CritScope lock(&crit_); |
philipel | 463d301 | 2016-09-09 03:32:44 -0700 | [diff] [blame] | 36 | |
| 37 | // If we have cleared past this frame, drop it. |
| 38 | if (cleared_to_seq_num_ != -1 && |
| 39 | AheadOf<uint16_t>(cleared_to_seq_num_, frame->first_seq_num())) { |
| 40 | return; |
| 41 | } |
| 42 | |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 43 | FrameDecision decision = ManageFrameInternal(frame.get()); |
| 44 | |
| 45 | switch (decision) { |
| 46 | case kStash: |
| 47 | if (stashed_frames_.size() > kMaxStashedFrames) |
| 48 | stashed_frames_.pop_back(); |
| 49 | stashed_frames_.push_front(std::move(frame)); |
| 50 | break; |
| 51 | case kHandOff: |
| 52 | frame_callback_->OnCompleteFrame(std::move(frame)); |
| 53 | RetryStashedFrames(); |
| 54 | break; |
| 55 | case kDrop: |
| 56 | break; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | void RtpFrameReferenceFinder::RetryStashedFrames() { |
| 61 | bool complete_frame = false; |
| 62 | do { |
| 63 | complete_frame = false; |
| 64 | for (auto frame_it = stashed_frames_.begin(); |
| 65 | frame_it != stashed_frames_.end();) { |
| 66 | FrameDecision decision = ManageFrameInternal(frame_it->get()); |
| 67 | |
| 68 | switch (decision) { |
| 69 | case kStash: |
| 70 | ++frame_it; |
| 71 | break; |
| 72 | case kHandOff: |
| 73 | complete_frame = true; |
| 74 | frame_callback_->OnCompleteFrame(std::move(*frame_it)); |
Karl Wiberg | 80ba333 | 2018-02-05 10:33:35 +0100 | [diff] [blame] | 75 | RTC_FALLTHROUGH(); |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 76 | case kDrop: |
| 77 | frame_it = stashed_frames_.erase(frame_it); |
| 78 | } |
| 79 | } |
| 80 | } while (complete_frame); |
| 81 | } |
| 82 | |
| 83 | RtpFrameReferenceFinder::FrameDecision |
| 84 | RtpFrameReferenceFinder::ManageFrameInternal(RtpFrameObject* frame) { |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 85 | switch (frame->codec_type()) { |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 86 | case kVideoCodecVP8: |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 87 | return ManageFrameVp8(frame); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 88 | case kVideoCodecVP9: |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 89 | return ManageFrameVp9(frame); |
philipel | 266f0a4 | 2016-11-28 08:49:07 -0800 | [diff] [blame] | 90 | // Since the EndToEndTests use kVicdeoCodecUnknow we treat it the same as |
| 91 | // kVideoCodecGeneric. |
| 92 | // TODO(philipel): Take a look at the EndToEndTests and see if maybe they |
| 93 | // should be changed to use kVideoCodecGeneric instead. |
| 94 | case kVideoCodecUnknown: |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 95 | case kVideoCodecH264: |
| 96 | case kVideoCodecI420: |
Emircan Uysaler | d7ae3c3 | 2018-01-25 13:01:09 -0800 | [diff] [blame] | 97 | case kVideoCodecMultiplex: |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 98 | case kVideoCodecGeneric: |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 99 | return ManageFrameGeneric(frame, kNoPictureId); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 100 | } |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 101 | |
| 102 | // If not all code paths return a value it makes the win compiler sad. |
| 103 | RTC_NOTREACHED(); |
| 104 | return kDrop; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 105 | } |
| 106 | |
philipel | 9b2ce6b | 2016-07-05 05:04:46 -0700 | [diff] [blame] | 107 | void RtpFrameReferenceFinder::PaddingReceived(uint16_t seq_num) { |
| 108 | rtc::CritScope lock(&crit_); |
| 109 | auto clean_padding_to = |
| 110 | stashed_padding_.lower_bound(seq_num - kMaxPaddingAge); |
| 111 | stashed_padding_.erase(stashed_padding_.begin(), clean_padding_to); |
| 112 | stashed_padding_.insert(seq_num); |
| 113 | UpdateLastPictureIdWithPadding(seq_num); |
| 114 | RetryStashedFrames(); |
| 115 | } |
| 116 | |
philipel | 463d301 | 2016-09-09 03:32:44 -0700 | [diff] [blame] | 117 | void RtpFrameReferenceFinder::ClearTo(uint16_t seq_num) { |
| 118 | rtc::CritScope lock(&crit_); |
| 119 | cleared_to_seq_num_ = seq_num; |
| 120 | |
| 121 | auto it = stashed_frames_.begin(); |
| 122 | while (it != stashed_frames_.end()) { |
| 123 | if (AheadOf<uint16_t>(cleared_to_seq_num_, (*it)->first_seq_num())) { |
| 124 | it = stashed_frames_.erase(it); |
| 125 | } else { |
| 126 | ++it; |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
philipel | 9b2ce6b | 2016-07-05 05:04:46 -0700 | [diff] [blame] | 131 | void RtpFrameReferenceFinder::UpdateLastPictureIdWithPadding(uint16_t seq_num) { |
| 132 | auto gop_seq_num_it = last_seq_num_gop_.upper_bound(seq_num); |
| 133 | |
| 134 | // If this padding packet "belongs" to a group of pictures that we don't track |
| 135 | // anymore, do nothing. |
| 136 | if (gop_seq_num_it == last_seq_num_gop_.begin()) |
| 137 | return; |
| 138 | --gop_seq_num_it; |
| 139 | |
| 140 | // Calculate the next contiuous sequence number and search for it in |
| 141 | // the padding packets we have stashed. |
| 142 | uint16_t next_seq_num_with_padding = gop_seq_num_it->second.second + 1; |
| 143 | auto padding_seq_num_it = |
| 144 | stashed_padding_.lower_bound(next_seq_num_with_padding); |
| 145 | |
| 146 | // While there still are padding packets and those padding packets are |
| 147 | // continuous, then advance the "last-picture-id-with-padding" and remove |
| 148 | // the stashed padding packet. |
| 149 | while (padding_seq_num_it != stashed_padding_.end() && |
| 150 | *padding_seq_num_it == next_seq_num_with_padding) { |
| 151 | gop_seq_num_it->second.second = next_seq_num_with_padding; |
| 152 | ++next_seq_num_with_padding; |
| 153 | padding_seq_num_it = stashed_padding_.erase(padding_seq_num_it); |
| 154 | } |
philipel | 41bb792 | 2017-02-20 07:53:23 -0800 | [diff] [blame] | 155 | |
| 156 | // In the case where the stream has been continuous without any new keyframes |
| 157 | // for a while there is a risk that new frames will appear to be older than |
| 158 | // the keyframe they belong to due to wrapping sequence number. In order |
| 159 | // to prevent this we advance the picture id of the keyframe every so often. |
| 160 | if (ForwardDiff(gop_seq_num_it->first, seq_num) > 10000) { |
| 161 | RTC_DCHECK_EQ(1ul, last_seq_num_gop_.size()); |
| 162 | last_seq_num_gop_[seq_num] = gop_seq_num_it->second; |
| 163 | last_seq_num_gop_.erase(gop_seq_num_it); |
| 164 | } |
philipel | 9b2ce6b | 2016-07-05 05:04:46 -0700 | [diff] [blame] | 165 | } |
| 166 | |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 167 | RtpFrameReferenceFinder::FrameDecision |
| 168 | RtpFrameReferenceFinder::ManageFrameGeneric(RtpFrameObject* frame, |
| 169 | int picture_id) { |
philipel | 647998c | 2016-06-03 09:40:16 -0700 | [diff] [blame] | 170 | // If |picture_id| is specified then we use that to set the frame references, |
| 171 | // otherwise we use sequence number. |
| 172 | if (picture_id != kNoPictureId) { |
| 173 | if (last_unwrap_ == -1) |
| 174 | last_unwrap_ = picture_id; |
| 175 | |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 176 | frame->id.picture_id = unwrapper_.Unwrap(picture_id); |
philipel | 647998c | 2016-06-03 09:40:16 -0700 | [diff] [blame] | 177 | frame->num_references = frame->frame_type() == kVideoFrameKey ? 0 : 1; |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 178 | frame->references[0] = frame->id.picture_id - 1; |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 179 | return kHandOff; |
philipel | 647998c | 2016-06-03 09:40:16 -0700 | [diff] [blame] | 180 | } |
| 181 | |
philipel | 9b2ce6b | 2016-07-05 05:04:46 -0700 | [diff] [blame] | 182 | if (frame->frame_type() == kVideoFrameKey) { |
| 183 | last_seq_num_gop_.insert(std::make_pair( |
| 184 | frame->last_seq_num(), |
| 185 | std::make_pair(frame->last_seq_num(), frame->last_seq_num()))); |
| 186 | } |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 187 | |
| 188 | // We have received a frame but not yet a keyframe, stash this frame. |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 189 | if (last_seq_num_gop_.empty()) |
| 190 | return kStash; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 191 | |
| 192 | // Clean up info for old keyframes but make sure to keep info |
| 193 | // for the last keyframe. |
| 194 | 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] | 195 | for (auto it = last_seq_num_gop_.begin(); |
| 196 | it != clean_to && last_seq_num_gop_.size() > 1;) { |
| 197 | it = last_seq_num_gop_.erase(it); |
| 198 | } |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 199 | |
| 200 | // Find the last sequence number of the last frame for the keyframe |
| 201 | // that this frame indirectly references. |
| 202 | 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] | 203 | if (seq_num_it == last_seq_num_gop_.begin()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 204 | RTC_LOG(LS_WARNING) << "Generic frame with packet range [" |
| 205 | << frame->first_seq_num() << ", " |
| 206 | << frame->last_seq_num() |
| 207 | << "] has no GoP, dropping frame."; |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 208 | return kDrop; |
philipel | 9b2ce6b | 2016-07-05 05:04:46 -0700 | [diff] [blame] | 209 | } |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 210 | seq_num_it--; |
| 211 | |
| 212 | // Make sure the packet sequence numbers are continuous, otherwise stash |
| 213 | // this frame. |
philipel | 9b2ce6b | 2016-07-05 05:04:46 -0700 | [diff] [blame] | 214 | uint16_t last_picture_id_gop = seq_num_it->second.first; |
| 215 | uint16_t last_picture_id_with_padding_gop = seq_num_it->second.second; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 216 | if (frame->frame_type() == kVideoFrameDelta) { |
philipel | 9b2ce6b | 2016-07-05 05:04:46 -0700 | [diff] [blame] | 217 | uint16_t prev_seq_num = frame->first_seq_num() - 1; |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 218 | |
| 219 | if (prev_seq_num != last_picture_id_with_padding_gop) |
| 220 | return kStash; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | RTC_DCHECK(AheadOrAt(frame->last_seq_num(), seq_num_it->first)); |
| 224 | |
| 225 | // Since keyframes can cause reordering we can't simply assign the |
| 226 | // picture id according to some incrementing counter. |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 227 | frame->id.picture_id = frame->last_seq_num(); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 228 | frame->num_references = frame->frame_type() == kVideoFrameDelta; |
philipel | d4fac69 | 2017-09-04 07:03:46 -0700 | [diff] [blame] | 229 | frame->references[0] = generic_unwrapper_.Unwrap(last_picture_id_gop); |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 230 | if (AheadOf<uint16_t>(frame->id.picture_id, last_picture_id_gop)) { |
| 231 | seq_num_it->second.first = frame->id.picture_id; |
| 232 | seq_num_it->second.second = frame->id.picture_id; |
philipel | 9b2ce6b | 2016-07-05 05:04:46 -0700 | [diff] [blame] | 233 | } |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 234 | |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 235 | last_picture_id_ = frame->id.picture_id; |
| 236 | UpdateLastPictureIdWithPadding(frame->id.picture_id); |
| 237 | frame->id.picture_id = generic_unwrapper_.Unwrap(frame->id.picture_id); |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 238 | return kHandOff; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 239 | } |
| 240 | |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 241 | RtpFrameReferenceFinder::FrameDecision RtpFrameReferenceFinder::ManageFrameVp8( |
| 242 | RtpFrameObject* frame) { |
Danil Chapovalov | 0040b66 | 2018-06-18 10:48:16 +0200 | [diff] [blame^] | 243 | absl::optional<RTPVideoTypeHeader> rtp_codec_header = frame->GetCodecHeader(); |
philipel | d4fac69 | 2017-09-04 07:03:46 -0700 | [diff] [blame] | 244 | if (!rtp_codec_header) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 245 | RTC_LOG(LS_WARNING) |
| 246 | << "Failed to get codec header from frame, dropping frame."; |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 247 | return kDrop; |
philipel | d4fac69 | 2017-09-04 07:03:46 -0700 | [diff] [blame] | 248 | } |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 249 | |
| 250 | const RTPVideoHeaderVP8& codec_header = rtp_codec_header->VP8; |
| 251 | |
| 252 | if (codec_header.pictureId == kNoPictureId || |
| 253 | codec_header.temporalIdx == kNoTemporalIdx || |
| 254 | codec_header.tl0PicIdx == kNoTl0PicIdx) { |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 255 | return ManageFrameGeneric(std::move(frame), codec_header.pictureId); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 256 | } |
| 257 | |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 258 | frame->id.picture_id = codec_header.pictureId % kPicIdLength; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 259 | |
| 260 | if (last_unwrap_ == -1) |
| 261 | last_unwrap_ = codec_header.pictureId; |
| 262 | |
| 263 | if (last_picture_id_ == -1) |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 264 | last_picture_id_ = frame->id.picture_id; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 265 | |
| 266 | // Find if there has been a gap in fully received frames and save the picture |
| 267 | // id of those frames in |not_yet_received_frames_|. |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 268 | if (AheadOf<uint16_t, kPicIdLength>(frame->id.picture_id, last_picture_id_)) { |
philipel | 9bd1d66 | 2017-07-14 04:52:01 -0700 | [diff] [blame] | 269 | do { |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 270 | last_picture_id_ = Add<kPicIdLength>(last_picture_id_, 1); |
philipel | 9bd1d66 | 2017-07-14 04:52:01 -0700 | [diff] [blame] | 271 | not_yet_received_frames_.insert(last_picture_id_); |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 272 | } while (last_picture_id_ != frame->id.picture_id); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | // Clean up info for base layers that are too old. |
| 276 | uint8_t old_tl0_pic_idx = codec_header.tl0PicIdx - kMaxLayerInfo; |
| 277 | auto clean_layer_info_to = layer_info_.lower_bound(old_tl0_pic_idx); |
| 278 | layer_info_.erase(layer_info_.begin(), clean_layer_info_to); |
| 279 | |
| 280 | // Clean up info about not yet received frames that are too old. |
| 281 | uint16_t old_picture_id = |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 282 | Subtract<kPicIdLength>(frame->id.picture_id, kMaxNotYetReceivedFrames); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 283 | auto clean_frames_to = not_yet_received_frames_.lower_bound(old_picture_id); |
| 284 | not_yet_received_frames_.erase(not_yet_received_frames_.begin(), |
| 285 | clean_frames_to); |
| 286 | |
| 287 | if (frame->frame_type() == kVideoFrameKey) { |
| 288 | frame->num_references = 0; |
| 289 | layer_info_[codec_header.tl0PicIdx].fill(-1); |
philipel | 1610f94 | 2017-12-12 13:58:31 +0100 | [diff] [blame] | 290 | UpdateLayerInfoVp8(frame, codec_header); |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 291 | return kHandOff; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | auto layer_info_it = layer_info_.find(codec_header.temporalIdx == 0 |
| 295 | ? codec_header.tl0PicIdx - 1 |
| 296 | : codec_header.tl0PicIdx); |
| 297 | |
| 298 | // If we don't have the base layer frame yet, stash this frame. |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 299 | if (layer_info_it == layer_info_.end()) |
| 300 | return kStash; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 301 | |
| 302 | // A non keyframe base layer frame has been received, copy the layer info |
| 303 | // from the previous base layer frame and set a reference to the previous |
| 304 | // base layer frame. |
| 305 | if (codec_header.temporalIdx == 0) { |
| 306 | layer_info_it = |
philipel | 1564360 | 2018-05-03 16:14:13 +0200 | [diff] [blame] | 307 | layer_info_.emplace(codec_header.tl0PicIdx, layer_info_it->second) |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 308 | .first; |
| 309 | frame->num_references = 1; |
| 310 | frame->references[0] = layer_info_it->second[0]; |
philipel | 1610f94 | 2017-12-12 13:58:31 +0100 | [diff] [blame] | 311 | UpdateLayerInfoVp8(frame, codec_header); |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 312 | return kHandOff; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | // Layer sync frame, this frame only references its base layer frame. |
| 316 | if (codec_header.layerSync) { |
| 317 | frame->num_references = 1; |
| 318 | frame->references[0] = layer_info_it->second[0]; |
| 319 | |
philipel | 1610f94 | 2017-12-12 13:58:31 +0100 | [diff] [blame] | 320 | UpdateLayerInfoVp8(frame, codec_header); |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 321 | return kHandOff; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | // Find all references for this frame. |
| 325 | frame->num_references = 0; |
| 326 | for (uint8_t layer = 0; layer <= codec_header.temporalIdx; ++layer) { |
philipel | d268d6f | 2016-09-15 13:43:13 +0200 | [diff] [blame] | 327 | // If we have not yet received a previous frame on this temporal layer, |
| 328 | // stash this frame. |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 329 | if (layer_info_it->second[layer] == -1) |
| 330 | return kStash; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 331 | |
philipel | 86b92e0 | 2016-10-24 07:11:53 -0700 | [diff] [blame] | 332 | // If the last frame on this layer is ahead of this frame it means that |
| 333 | // a layer sync frame has been received after this frame for the same |
| 334 | // base layer frame, drop this frame. |
| 335 | if (AheadOf<uint16_t, kPicIdLength>(layer_info_it->second[layer], |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 336 | frame->id.picture_id)) { |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 337 | return kDrop; |
philipel | 86b92e0 | 2016-10-24 07:11:53 -0700 | [diff] [blame] | 338 | } |
| 339 | |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 340 | // If we have not yet received a frame between this frame and the referenced |
| 341 | // frame then we have to wait for that frame to be completed first. |
| 342 | auto not_received_frame_it = |
| 343 | not_yet_received_frames_.upper_bound(layer_info_it->second[layer]); |
| 344 | if (not_received_frame_it != not_yet_received_frames_.end() && |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 345 | AheadOf<uint16_t, kPicIdLength>(frame->id.picture_id, |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 346 | *not_received_frame_it)) { |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 347 | return kStash; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 348 | } |
| 349 | |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 350 | if (!(AheadOf<uint16_t, kPicIdLength>(frame->id.picture_id, |
philipel | 57f19cc | 2017-03-07 03:54:05 -0800 | [diff] [blame] | 351 | layer_info_it->second[layer]))) { |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 352 | RTC_LOG(LS_WARNING) << "Frame with picture id " << frame->id.picture_id |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 353 | << " and packet range [" << frame->first_seq_num() |
| 354 | << ", " << frame->last_seq_num() |
| 355 | << "] already received, " |
| 356 | << " dropping frame."; |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 357 | return kDrop; |
philipel | 57f19cc | 2017-03-07 03:54:05 -0800 | [diff] [blame] | 358 | } |
| 359 | |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 360 | ++frame->num_references; |
| 361 | frame->references[layer] = layer_info_it->second[layer]; |
| 362 | } |
| 363 | |
philipel | 1610f94 | 2017-12-12 13:58:31 +0100 | [diff] [blame] | 364 | UpdateLayerInfoVp8(frame, codec_header); |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 365 | return kHandOff; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 366 | } |
| 367 | |
philipel | 1610f94 | 2017-12-12 13:58:31 +0100 | [diff] [blame] | 368 | void RtpFrameReferenceFinder::UpdateLayerInfoVp8( |
| 369 | RtpFrameObject* frame, |
| 370 | const RTPVideoHeaderVP8& codec_header) { |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 371 | uint8_t tl0_pic_idx = codec_header.tl0PicIdx; |
| 372 | uint8_t temporal_index = codec_header.temporalIdx; |
| 373 | auto layer_info_it = layer_info_.find(tl0_pic_idx); |
| 374 | |
| 375 | // Update this layer info and newer. |
| 376 | while (layer_info_it != layer_info_.end()) { |
| 377 | if (layer_info_it->second[temporal_index] != -1 && |
| 378 | AheadOf<uint16_t, kPicIdLength>(layer_info_it->second[temporal_index], |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 379 | frame->id.picture_id)) { |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 380 | // The frame was not newer, then no subsequent layer info have to be |
| 381 | // update. |
| 382 | break; |
| 383 | } |
| 384 | |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 385 | layer_info_it->second[codec_header.temporalIdx] = frame->id.picture_id; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 386 | ++tl0_pic_idx; |
| 387 | layer_info_it = layer_info_.find(tl0_pic_idx); |
| 388 | } |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 389 | not_yet_received_frames_.erase(frame->id.picture_id); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 390 | |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 391 | UnwrapPictureIds(frame); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 392 | } |
| 393 | |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 394 | RtpFrameReferenceFinder::FrameDecision RtpFrameReferenceFinder::ManageFrameVp9( |
| 395 | RtpFrameObject* frame) { |
Danil Chapovalov | 0040b66 | 2018-06-18 10:48:16 +0200 | [diff] [blame^] | 396 | absl::optional<RTPVideoTypeHeader> rtp_codec_header = frame->GetCodecHeader(); |
philipel | d4fac69 | 2017-09-04 07:03:46 -0700 | [diff] [blame] | 397 | if (!rtp_codec_header) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 398 | RTC_LOG(LS_WARNING) |
| 399 | << "Failed to get codec header from frame, dropping frame."; |
philipel | 4c14009 | 2017-08-31 08:31:45 -0700 | [diff] [blame] | 400 | return kDrop; |
philipel | d4fac69 | 2017-09-04 07:03:46 -0700 | [diff] [blame] | 401 | } |
philipel | 4c14009 | 2017-08-31 08:31:45 -0700 | [diff] [blame] | 402 | |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 403 | const RTPVideoHeaderVP9& codec_header = rtp_codec_header->VP9; |
| 404 | |
philipel | 647998c | 2016-06-03 09:40:16 -0700 | [diff] [blame] | 405 | if (codec_header.picture_id == kNoPictureId || |
| 406 | codec_header.temporal_idx == kNoTemporalIdx) { |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 407 | return ManageFrameGeneric(std::move(frame), codec_header.picture_id); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 408 | } |
| 409 | |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 410 | frame->id.spatial_layer = codec_header.spatial_idx; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 411 | frame->inter_layer_predicted = codec_header.inter_layer_predicted; |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 412 | frame->id.picture_id = codec_header.picture_id % kPicIdLength; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 413 | |
| 414 | if (last_unwrap_ == -1) |
| 415 | last_unwrap_ = codec_header.picture_id; |
| 416 | |
| 417 | if (last_picture_id_ == -1) |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 418 | last_picture_id_ = frame->id.picture_id; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 419 | |
| 420 | if (codec_header.flexible_mode) { |
| 421 | frame->num_references = codec_header.num_ref_pics; |
| 422 | for (size_t i = 0; i < frame->num_references; ++i) { |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 423 | frame->references[i] = Subtract<kPicIdLength>(frame->id.picture_id, |
| 424 | codec_header.pid_diff[i]); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 425 | } |
| 426 | |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 427 | UnwrapPictureIds(frame); |
| 428 | return kHandOff; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 429 | } |
| 430 | |
philipel | 1564360 | 2018-05-03 16:14:13 +0200 | [diff] [blame] | 431 | GofInfo* info; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 432 | if (codec_header.ss_data_available) { |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 433 | if (codec_header.temporal_idx != 0) { |
philipel | 1564360 | 2018-05-03 16:14:13 +0200 | [diff] [blame] | 434 | RTC_LOG(LS_WARNING) << "Received scalability structure on a non base " |
| 435 | "layer frame. Scalability structure ignored."; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 436 | } else { |
| 437 | current_ss_idx_ = Add<kMaxGofSaved>(current_ss_idx_, 1); |
Natalie Silvanovich | 3ea3e30 | 2018-05-16 11:03:12 -0700 | [diff] [blame] | 438 | if (codec_header.gof.num_frames_in_gof == 0 || |
| 439 | codec_header.gof.num_frames_in_gof > kMaxVp9FramesInGof) { |
| 440 | return kDrop; |
| 441 | } |
| 442 | |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 443 | scalability_structures_[current_ss_idx_] = codec_header.gof; |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 444 | scalability_structures_[current_ss_idx_].pid_start = frame->id.picture_id; |
philipel | 1564360 | 2018-05-03 16:14:13 +0200 | [diff] [blame] | 445 | gof_info_.emplace(codec_header.tl0_pic_idx, |
| 446 | GofInfo(&scalability_structures_[current_ss_idx_], |
| 447 | frame->id.picture_id)); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 448 | } |
philipel | 1564360 | 2018-05-03 16:14:13 +0200 | [diff] [blame] | 449 | |
| 450 | const auto gof_info_it = gof_info_.find(codec_header.tl0_pic_idx); |
| 451 | if (gof_info_it == gof_info_.end()) |
| 452 | return kStash; |
| 453 | |
| 454 | info = &gof_info_it->second; |
| 455 | |
| 456 | if (frame->frame_type() == kVideoFrameKey) { |
| 457 | frame->num_references = 0; |
| 458 | FrameReceivedVp9(frame->id.picture_id, info); |
| 459 | UnwrapPictureIds(frame); |
| 460 | return kHandOff; |
| 461 | } |
| 462 | } else { |
| 463 | if (frame->frame_type() == kVideoFrameKey) { |
| 464 | RTC_LOG(LS_WARNING) << "Received keyframe without scalability structure"; |
| 465 | return kDrop; |
| 466 | } |
| 467 | |
| 468 | auto gof_info_it = gof_info_.find((codec_header.temporal_idx == 0) |
| 469 | ? codec_header.tl0_pic_idx - 1 |
| 470 | : codec_header.tl0_pic_idx); |
| 471 | |
| 472 | // Gof info for this frame is not available yet, stash this frame. |
| 473 | if (gof_info_it == gof_info_.end()) |
| 474 | return kStash; |
| 475 | |
| 476 | if (codec_header.temporal_idx == 0) { |
| 477 | gof_info_it = |
| 478 | gof_info_ |
| 479 | .emplace(codec_header.tl0_pic_idx, |
| 480 | GofInfo(gof_info_it->second.gof, frame->id.picture_id)) |
| 481 | .first; |
| 482 | } |
| 483 | |
| 484 | info = &gof_info_it->second; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | // Clean up info for base layers that are too old. |
| 488 | uint8_t old_tl0_pic_idx = codec_header.tl0_pic_idx - kMaxGofSaved; |
| 489 | auto clean_gof_info_to = gof_info_.lower_bound(old_tl0_pic_idx); |
| 490 | gof_info_.erase(gof_info_.begin(), clean_gof_info_to); |
| 491 | |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 492 | FrameReceivedVp9(frame->id.picture_id, info); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 493 | |
| 494 | // Make sure we don't miss any frame that could potentially have the |
| 495 | // up switch flag set. |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 496 | if (MissingRequiredFrameVp9(frame->id.picture_id, *info)) |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 497 | return kStash; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 498 | |
philipel | 1564360 | 2018-05-03 16:14:13 +0200 | [diff] [blame] | 499 | if (codec_header.temporal_up_switch) |
| 500 | up_switch_.emplace(frame->id.picture_id, codec_header.temporal_idx); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 501 | |
| 502 | // Clean out old info about up switch frames. |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 503 | uint16_t old_picture_id = Subtract<kPicIdLength>(frame->id.picture_id, 50); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 504 | auto up_switch_erase_to = up_switch_.lower_bound(old_picture_id); |
| 505 | up_switch_.erase(up_switch_.begin(), up_switch_erase_to); |
| 506 | |
philipel | c9b27d5 | 2016-07-15 06:50:27 -0700 | [diff] [blame] | 507 | size_t diff = ForwardDiff<uint16_t, kPicIdLength>(info->gof->pid_start, |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 508 | frame->id.picture_id); |
philipel | c9b27d5 | 2016-07-15 06:50:27 -0700 | [diff] [blame] | 509 | size_t gof_idx = diff % info->gof->num_frames_in_gof; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 510 | |
| 511 | // Populate references according to the scalability structure. |
philipel | c9b27d5 | 2016-07-15 06:50:27 -0700 | [diff] [blame] | 512 | frame->num_references = info->gof->num_ref_pics[gof_idx]; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 513 | for (size_t i = 0; i < frame->num_references; ++i) { |
philipel | c9b27d5 | 2016-07-15 06:50:27 -0700 | [diff] [blame] | 514 | frame->references[i] = Subtract<kPicIdLength>( |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 515 | frame->id.picture_id, info->gof->pid_diff[gof_idx][i]); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 516 | |
| 517 | // If this is a reference to a frame earlier than the last up switch point, |
| 518 | // then ignore this reference. |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 519 | if (UpSwitchInIntervalVp9(frame->id.picture_id, codec_header.temporal_idx, |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 520 | frame->references[i])) { |
| 521 | --frame->num_references; |
| 522 | } |
| 523 | } |
| 524 | |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 525 | UnwrapPictureIds(frame); |
| 526 | return kHandOff; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | bool RtpFrameReferenceFinder::MissingRequiredFrameVp9(uint16_t picture_id, |
philipel | c9b27d5 | 2016-07-15 06:50:27 -0700 | [diff] [blame] | 530 | const GofInfo& info) { |
| 531 | size_t diff = |
| 532 | ForwardDiff<uint16_t, kPicIdLength>(info.gof->pid_start, picture_id); |
| 533 | size_t gof_idx = diff % info.gof->num_frames_in_gof; |
| 534 | size_t temporal_idx = info.gof->temporal_idx[gof_idx]; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 535 | |
philipel | a157e08 | 2018-05-02 15:19:01 +0200 | [diff] [blame] | 536 | if (temporal_idx >= kMaxTemporalLayers) { |
| 537 | RTC_LOG(LS_WARNING) << "At most " << kMaxTemporalLayers << " temporal " |
| 538 | << "layers are supported."; |
| 539 | return true; |
| 540 | } |
| 541 | |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 542 | // For every reference this frame has, check if there is a frame missing in |
| 543 | // the interval (|ref_pid|, |picture_id|) in any of the lower temporal |
| 544 | // layers. If so, we are missing a required frame. |
philipel | c9b27d5 | 2016-07-15 06:50:27 -0700 | [diff] [blame] | 545 | uint8_t num_references = info.gof->num_ref_pics[gof_idx]; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 546 | for (size_t i = 0; i < num_references; ++i) { |
| 547 | uint16_t ref_pid = |
philipel | c9b27d5 | 2016-07-15 06:50:27 -0700 | [diff] [blame] | 548 | Subtract<kPicIdLength>(picture_id, info.gof->pid_diff[gof_idx][i]); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 549 | for (size_t l = 0; l < temporal_idx; ++l) { |
| 550 | auto missing_frame_it = missing_frames_for_layer_[l].lower_bound(ref_pid); |
| 551 | if (missing_frame_it != missing_frames_for_layer_[l].end() && |
| 552 | AheadOf<uint16_t, kPicIdLength>(picture_id, *missing_frame_it)) { |
| 553 | return true; |
| 554 | } |
| 555 | } |
| 556 | } |
| 557 | return false; |
| 558 | } |
| 559 | |
| 560 | void RtpFrameReferenceFinder::FrameReceivedVp9(uint16_t picture_id, |
philipel | c9b27d5 | 2016-07-15 06:50:27 -0700 | [diff] [blame] | 561 | GofInfo* info) { |
| 562 | int last_picture_id = info->last_picture_id; |
philipel | 459f4e3 | 2018-03-02 10:55:12 +0100 | [diff] [blame] | 563 | size_t gof_size = std::min(info->gof->num_frames_in_gof, kMaxVp9FramesInGof); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 564 | |
| 565 | // If there is a gap, find which temporal layer the missing frames |
| 566 | // belong to and add the frame as missing for that temporal layer. |
| 567 | // Otherwise, remove this frame from the set of missing frames. |
philipel | c9b27d5 | 2016-07-15 06:50:27 -0700 | [diff] [blame] | 568 | if (AheadOf<uint16_t, kPicIdLength>(picture_id, last_picture_id)) { |
| 569 | size_t diff = ForwardDiff<uint16_t, kPicIdLength>(info->gof->pid_start, |
| 570 | last_picture_id); |
philipel | 459f4e3 | 2018-03-02 10:55:12 +0100 | [diff] [blame] | 571 | size_t gof_idx = diff % gof_size; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 572 | |
philipel | c9b27d5 | 2016-07-15 06:50:27 -0700 | [diff] [blame] | 573 | last_picture_id = Add<kPicIdLength>(last_picture_id, 1); |
| 574 | while (last_picture_id != picture_id) { |
philipel | 459f4e3 | 2018-03-02 10:55:12 +0100 | [diff] [blame] | 575 | gof_idx = (gof_idx + 1) % gof_size; |
| 576 | RTC_CHECK(gof_idx < kMaxVp9FramesInGof); |
| 577 | |
philipel | c9b27d5 | 2016-07-15 06:50:27 -0700 | [diff] [blame] | 578 | size_t temporal_idx = info->gof->temporal_idx[gof_idx]; |
philipel | 459f4e3 | 2018-03-02 10:55:12 +0100 | [diff] [blame] | 579 | if (temporal_idx >= kMaxTemporalLayers) { |
| 580 | RTC_LOG(LS_WARNING) << "At most " << kMaxTemporalLayers << " temporal " |
| 581 | << "layers are supported."; |
| 582 | return; |
| 583 | } |
| 584 | |
philipel | c9b27d5 | 2016-07-15 06:50:27 -0700 | [diff] [blame] | 585 | missing_frames_for_layer_[temporal_idx].insert(last_picture_id); |
| 586 | last_picture_id = Add<kPicIdLength>(last_picture_id, 1); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 587 | } |
philipel | 459f4e3 | 2018-03-02 10:55:12 +0100 | [diff] [blame] | 588 | |
philipel | c9b27d5 | 2016-07-15 06:50:27 -0700 | [diff] [blame] | 589 | info->last_picture_id = last_picture_id; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 590 | } else { |
| 591 | size_t diff = |
philipel | c9b27d5 | 2016-07-15 06:50:27 -0700 | [diff] [blame] | 592 | ForwardDiff<uint16_t, kPicIdLength>(info->gof->pid_start, picture_id); |
philipel | 459f4e3 | 2018-03-02 10:55:12 +0100 | [diff] [blame] | 593 | size_t gof_idx = diff % gof_size; |
| 594 | RTC_CHECK(gof_idx < kMaxVp9FramesInGof); |
| 595 | |
philipel | c9b27d5 | 2016-07-15 06:50:27 -0700 | [diff] [blame] | 596 | size_t temporal_idx = info->gof->temporal_idx[gof_idx]; |
philipel | 459f4e3 | 2018-03-02 10:55:12 +0100 | [diff] [blame] | 597 | if (temporal_idx >= kMaxTemporalLayers) { |
| 598 | RTC_LOG(LS_WARNING) << "At most " << kMaxTemporalLayers << " temporal " |
| 599 | << "layers are supported."; |
| 600 | return; |
| 601 | } |
| 602 | |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 603 | missing_frames_for_layer_[temporal_idx].erase(picture_id); |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | bool RtpFrameReferenceFinder::UpSwitchInIntervalVp9(uint16_t picture_id, |
| 608 | uint8_t temporal_idx, |
| 609 | uint16_t pid_ref) { |
| 610 | for (auto up_switch_it = up_switch_.upper_bound(pid_ref); |
| 611 | up_switch_it != up_switch_.end() && |
| 612 | AheadOf<uint16_t, kPicIdLength>(picture_id, up_switch_it->first); |
| 613 | ++up_switch_it) { |
| 614 | if (up_switch_it->second < temporal_idx) |
| 615 | return true; |
| 616 | } |
| 617 | |
| 618 | return false; |
| 619 | } |
| 620 | |
philipel | afcf7f5 | 2017-04-26 08:17:35 -0700 | [diff] [blame] | 621 | void RtpFrameReferenceFinder::UnwrapPictureIds(RtpFrameObject* frame) { |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 622 | for (size_t i = 0; i < frame->num_references; ++i) |
philipel | d4fac69 | 2017-09-04 07:03:46 -0700 | [diff] [blame] | 623 | frame->references[i] = unwrapper_.Unwrap(frame->references[i]); |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 624 | frame->id.picture_id = unwrapper_.Unwrap(frame->id.picture_id); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 625 | } |
| 626 | |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 627 | } // namespace video_coding |
| 628 | } // namespace webrtc |