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 | |
philipel | 4e70216 | 2020-11-27 17:56:37 +0100 | [diff] [blame] | 13 | #include <utility> |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 14 | |
philipel | 4e70216 | 2020-11-27 17:56:37 +0100 | [diff] [blame] | 15 | #include "absl/types/variant.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "modules/video_coding/frame_object.h" |
philipel | 4e70216 | 2020-11-27 17:56:37 +0100 | [diff] [blame] | 17 | #include "modules/video_coding/rtp_frame_id_only_ref_finder.h" |
| 18 | #include "modules/video_coding/rtp_generic_ref_finder.h" |
| 19 | #include "modules/video_coding/rtp_seq_num_only_ref_finder.h" |
| 20 | #include "modules/video_coding/rtp_vp8_ref_finder.h" |
| 21 | #include "modules/video_coding/rtp_vp9_ref_finder.h" |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 22 | |
| 23 | namespace webrtc { |
| 24 | namespace video_coding { |
philipel | 4e70216 | 2020-11-27 17:56:37 +0100 | [diff] [blame] | 25 | namespace internal { |
| 26 | class RtpFrameReferenceFinderImpl { |
| 27 | public: |
| 28 | RtpFrameReferenceFinderImpl() = default; |
| 29 | |
| 30 | RtpFrameReferenceFinder::ReturnVector ManageFrame( |
| 31 | std::unique_ptr<RtpFrameObject> frame); |
| 32 | RtpFrameReferenceFinder::ReturnVector PaddingReceived(uint16_t seq_num); |
| 33 | void ClearTo(uint16_t seq_num); |
| 34 | |
| 35 | private: |
| 36 | using RefFinder = absl::variant<absl::monostate, |
| 37 | RtpGenericFrameRefFinder, |
| 38 | RtpFrameIdOnlyRefFinder, |
| 39 | RtpSeqNumOnlyRefFinder, |
| 40 | RtpVp8RefFinder, |
| 41 | RtpVp9RefFinder>; |
| 42 | |
| 43 | template <typename T> |
| 44 | T& GetRefFinderAs(); |
| 45 | RefFinder ref_finder_; |
| 46 | }; |
| 47 | |
| 48 | RtpFrameReferenceFinder::ReturnVector RtpFrameReferenceFinderImpl::ManageFrame( |
| 49 | std::unique_ptr<RtpFrameObject> frame) { |
| 50 | const RTPVideoHeader& video_header = frame->GetRtpVideoHeader(); |
| 51 | |
| 52 | if (video_header.generic.has_value()) { |
| 53 | return GetRefFinderAs<RtpGenericFrameRefFinder>().ManageFrame( |
| 54 | std::move(frame), *video_header.generic); |
| 55 | } |
| 56 | |
| 57 | switch (frame->codec_type()) { |
| 58 | case kVideoCodecVP8: { |
| 59 | const RTPVideoHeaderVP8& vp8_header = |
| 60 | absl::get<RTPVideoHeaderVP8>(video_header.video_type_header); |
| 61 | |
| 62 | if (vp8_header.temporalIdx == kNoTemporalIdx || |
| 63 | vp8_header.tl0PicIdx == kNoTl0PicIdx) { |
| 64 | if (vp8_header.pictureId == kNoPictureId) { |
| 65 | return GetRefFinderAs<RtpSeqNumOnlyRefFinder>().ManageFrame( |
| 66 | std::move(frame)); |
| 67 | } |
| 68 | |
| 69 | return GetRefFinderAs<RtpFrameIdOnlyRefFinder>().ManageFrame( |
| 70 | std::move(frame), vp8_header.pictureId); |
| 71 | } |
| 72 | |
| 73 | return GetRefFinderAs<RtpVp8RefFinder>().ManageFrame(std::move(frame)); |
| 74 | } |
| 75 | case kVideoCodecVP9: { |
| 76 | const RTPVideoHeaderVP9& vp9_header = |
| 77 | absl::get<RTPVideoHeaderVP9>(video_header.video_type_header); |
| 78 | |
| 79 | if (vp9_header.temporal_idx == kNoTemporalIdx) { |
| 80 | if (vp9_header.picture_id == kNoPictureId) { |
| 81 | return GetRefFinderAs<RtpSeqNumOnlyRefFinder>().ManageFrame( |
| 82 | std::move(frame)); |
| 83 | } |
| 84 | |
| 85 | return GetRefFinderAs<RtpFrameIdOnlyRefFinder>().ManageFrame( |
| 86 | std::move(frame), vp9_header.picture_id); |
| 87 | } |
| 88 | |
| 89 | return GetRefFinderAs<RtpVp9RefFinder>().ManageFrame(std::move(frame)); |
| 90 | } |
philipel | 4e70216 | 2020-11-27 17:56:37 +0100 | [diff] [blame] | 91 | case kVideoCodecGeneric: { |
| 92 | if (auto* generic_header = absl::get_if<RTPVideoHeaderLegacyGeneric>( |
| 93 | &video_header.video_type_header)) { |
| 94 | return GetRefFinderAs<RtpFrameIdOnlyRefFinder>().ManageFrame( |
| 95 | std::move(frame), generic_header->picture_id); |
| 96 | } |
| 97 | |
| 98 | return GetRefFinderAs<RtpSeqNumOnlyRefFinder>().ManageFrame( |
| 99 | std::move(frame)); |
| 100 | } |
| 101 | default: { |
philipel | 39f09b4 | 2020-12-01 11:47:12 +0100 | [diff] [blame] | 102 | return GetRefFinderAs<RtpSeqNumOnlyRefFinder>().ManageFrame( |
| 103 | std::move(frame)); |
philipel | 4e70216 | 2020-11-27 17:56:37 +0100 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | RtpFrameReferenceFinder::ReturnVector |
| 109 | RtpFrameReferenceFinderImpl::PaddingReceived(uint16_t seq_num) { |
| 110 | if (auto* ref_finder = absl::get_if<RtpSeqNumOnlyRefFinder>(&ref_finder_)) { |
| 111 | return ref_finder->PaddingReceived(seq_num); |
| 112 | } |
| 113 | return {}; |
| 114 | } |
| 115 | |
| 116 | void RtpFrameReferenceFinderImpl::ClearTo(uint16_t seq_num) { |
| 117 | struct ClearToVisitor { |
| 118 | void operator()(absl::monostate& ref_finder) {} |
| 119 | void operator()(RtpGenericFrameRefFinder& ref_finder) {} |
| 120 | void operator()(RtpFrameIdOnlyRefFinder& ref_finder) {} |
| 121 | void operator()(RtpSeqNumOnlyRefFinder& ref_finder) { |
| 122 | ref_finder.ClearTo(seq_num); |
| 123 | } |
| 124 | void operator()(RtpVp8RefFinder& ref_finder) { |
| 125 | ref_finder.ClearTo(seq_num); |
| 126 | } |
| 127 | void operator()(RtpVp9RefFinder& ref_finder) { |
| 128 | ref_finder.ClearTo(seq_num); |
| 129 | } |
| 130 | uint16_t seq_num; |
| 131 | }; |
| 132 | |
| 133 | absl::visit(ClearToVisitor{seq_num}, ref_finder_); |
| 134 | } |
| 135 | |
| 136 | template <typename T> |
| 137 | T& RtpFrameReferenceFinderImpl::GetRefFinderAs() { |
| 138 | if (auto* ref_finder = absl::get_if<T>(&ref_finder_)) { |
| 139 | return *ref_finder; |
| 140 | } |
| 141 | return ref_finder_.emplace<T>(); |
| 142 | } |
| 143 | |
| 144 | } // namespace internal |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 145 | |
| 146 | RtpFrameReferenceFinder::RtpFrameReferenceFinder( |
| 147 | OnCompleteFrameCallback* frame_callback) |
philipel | 7acc4a4 | 2019-09-26 11:25:52 +0200 | [diff] [blame] | 148 | : RtpFrameReferenceFinder(frame_callback, 0) {} |
| 149 | |
| 150 | RtpFrameReferenceFinder::RtpFrameReferenceFinder( |
| 151 | OnCompleteFrameCallback* frame_callback, |
| 152 | int64_t picture_id_offset) |
philipel | 4e70216 | 2020-11-27 17:56:37 +0100 | [diff] [blame] | 153 | : picture_id_offset_(picture_id_offset), |
philipel | 7acc4a4 | 2019-09-26 11:25:52 +0200 | [diff] [blame] | 154 | frame_callback_(frame_callback), |
philipel | 4e70216 | 2020-11-27 17:56:37 +0100 | [diff] [blame] | 155 | impl_(std::make_unique<internal::RtpFrameReferenceFinderImpl>()) {} |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 156 | |
Mirko Bonadei | 8fdcac3 | 2018-08-28 16:30:18 +0200 | [diff] [blame] | 157 | RtpFrameReferenceFinder::~RtpFrameReferenceFinder() = default; |
| 158 | |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 159 | void RtpFrameReferenceFinder::ManageFrame( |
| 160 | std::unique_ptr<RtpFrameObject> frame) { |
philipel | 463d301 | 2016-09-09 03:32:44 -0700 | [diff] [blame] | 161 | // If we have cleared past this frame, drop it. |
| 162 | if (cleared_to_seq_num_ != -1 && |
| 163 | AheadOf<uint16_t>(cleared_to_seq_num_, frame->first_seq_num())) { |
| 164 | return; |
| 165 | } |
philipel | 4e70216 | 2020-11-27 17:56:37 +0100 | [diff] [blame] | 166 | HandOffFrames(impl_->ManageFrame(std::move(frame))); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 167 | } |
| 168 | |
philipel | 9b2ce6b | 2016-07-05 05:04:46 -0700 | [diff] [blame] | 169 | void RtpFrameReferenceFinder::PaddingReceived(uint16_t seq_num) { |
philipel | 4e70216 | 2020-11-27 17:56:37 +0100 | [diff] [blame] | 170 | HandOffFrames(impl_->PaddingReceived(seq_num)); |
philipel | 9b2ce6b | 2016-07-05 05:04:46 -0700 | [diff] [blame] | 171 | } |
| 172 | |
philipel | 463d301 | 2016-09-09 03:32:44 -0700 | [diff] [blame] | 173 | void RtpFrameReferenceFinder::ClearTo(uint16_t seq_num) { |
philipel | 463d301 | 2016-09-09 03:32:44 -0700 | [diff] [blame] | 174 | cleared_to_seq_num_ = seq_num; |
philipel | 4e70216 | 2020-11-27 17:56:37 +0100 | [diff] [blame] | 175 | impl_->ClearTo(seq_num); |
philipel | 463d301 | 2016-09-09 03:32:44 -0700 | [diff] [blame] | 176 | } |
| 177 | |
philipel | 4e70216 | 2020-11-27 17:56:37 +0100 | [diff] [blame] | 178 | void RtpFrameReferenceFinder::HandOffFrames(ReturnVector frames) { |
| 179 | for (auto& frame : frames) { |
philipel | 9aa9b8d | 2021-02-15 13:31:29 +0100 | [diff] [blame^] | 180 | frame->SetId(frame->Id() + picture_id_offset_); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 181 | for (size_t i = 0; i < frame->num_references; ++i) { |
philipel | 4e70216 | 2020-11-27 17:56:37 +0100 | [diff] [blame] | 182 | frame->references[i] += picture_id_offset_; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 183 | } |
| 184 | |
philipel | 4e70216 | 2020-11-27 17:56:37 +0100 | [diff] [blame] | 185 | frame_callback_->OnCompleteFrame(std::move(frame)); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 186 | } |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 187 | } |
| 188 | |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 189 | } // namespace video_coding |
| 190 | } // namespace webrtc |