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