philipel | 4e70216 | 2020-11-27 16:56:37 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2020 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 | #ifndef MODULES_VIDEO_CODING_RTP_VP9_REF_FINDER_H_ |
| 12 | #define MODULES_VIDEO_CODING_RTP_VP9_REF_FINDER_H_ |
| 13 | |
| 14 | #include <deque> |
| 15 | #include <map> |
| 16 | #include <memory> |
| 17 | #include <set> |
| 18 | |
| 19 | #include "absl/container/inlined_vector.h" |
Tony Herre | be9b576 | 2023-02-03 11:29:04 | [diff] [blame] | 20 | #include "modules/rtp_rtcp/source/frame_object.h" |
philipel | 4e70216 | 2020-11-27 16:56:37 | [diff] [blame] | 21 | #include "modules/video_coding/rtp_frame_reference_finder.h" |
Evan Shrubsole | 097fc34 | 2023-01-09 10:21:43 | [diff] [blame] | 22 | #include "rtc_base/numerics/sequence_number_unwrapper.h" |
philipel | 4e70216 | 2020-11-27 16:56:37 | [diff] [blame] | 23 | |
| 24 | namespace webrtc { |
philipel | 4e70216 | 2020-11-27 16:56:37 | [diff] [blame] | 25 | |
| 26 | class RtpVp9RefFinder { |
| 27 | public: |
| 28 | RtpVp9RefFinder() = default; |
| 29 | |
| 30 | RtpFrameReferenceFinder::ReturnVector ManageFrame( |
philipel | ca18809 | 2021-03-23 11:00:49 | [diff] [blame] | 31 | std::unique_ptr<RtpFrameObject> frame); |
philipel | 4e70216 | 2020-11-27 16:56:37 | [diff] [blame] | 32 | void ClearTo(uint16_t seq_num); |
| 33 | |
| 34 | private: |
| 35 | static constexpr int kFrameIdLength = 1 << 15; |
| 36 | static constexpr int kMaxGofSaved = 50; |
| 37 | static constexpr int kMaxLayerInfo = 50; |
| 38 | static constexpr int kMaxNotYetReceivedFrames = 100; |
| 39 | static constexpr int kMaxStashedFrames = 100; |
| 40 | static constexpr int kMaxTemporalLayers = 5; |
| 41 | |
| 42 | enum FrameDecision { kStash, kHandOff, kDrop }; |
| 43 | |
| 44 | struct GofInfo { |
philipel | 0d17535 | 2021-08-27 16:29:19 | [diff] [blame] | 45 | GofInfo(GofInfoVP9* gof, uint16_t last_picture_id) |
| 46 | : gof(gof), last_picture_id(last_picture_id) {} |
philipel | 4e70216 | 2020-11-27 16:56:37 | [diff] [blame] | 47 | GofInfoVP9* gof; |
| 48 | uint16_t last_picture_id; |
| 49 | }; |
| 50 | |
philipel | 773205d | 2022-03-07 17:12:26 | [diff] [blame] | 51 | struct UnwrappedTl0Frame { |
| 52 | int64_t unwrapped_tl0; |
| 53 | std::unique_ptr<RtpFrameObject> frame; |
| 54 | }; |
| 55 | |
| 56 | FrameDecision ManageFrameFlexible(RtpFrameObject* frame, |
| 57 | const RTPVideoHeaderVP9& vp9_header); |
| 58 | FrameDecision ManageFrameGof(RtpFrameObject* frame, |
| 59 | const RTPVideoHeaderVP9& vp9_header, |
| 60 | int64_t unwrapped_tl0); |
philipel | 4e70216 | 2020-11-27 16:56:37 | [diff] [blame] | 61 | void RetryStashedFrames(RtpFrameReferenceFinder::ReturnVector& res); |
| 62 | |
| 63 | bool MissingRequiredFrameVp9(uint16_t picture_id, const GofInfo& info); |
| 64 | |
| 65 | void FrameReceivedVp9(uint16_t picture_id, GofInfo* info); |
| 66 | bool UpSwitchInIntervalVp9(uint16_t picture_id, |
| 67 | uint8_t temporal_idx, |
| 68 | uint16_t pid_ref); |
| 69 | |
philipel | ca18809 | 2021-03-23 11:00:49 | [diff] [blame] | 70 | void FlattenFrameIdAndRefs(RtpFrameObject* frame, bool inter_layer_predicted); |
philipel | 4e70216 | 2020-11-27 16:56:37 | [diff] [blame] | 71 | |
philipel | 4e70216 | 2020-11-27 16:56:37 | [diff] [blame] | 72 | // Frames that have been fully received but didn't have all the information |
| 73 | // needed to determine their references. |
philipel | 773205d | 2022-03-07 17:12:26 | [diff] [blame] | 74 | std::deque<UnwrappedTl0Frame> stashed_frames_; |
philipel | 4e70216 | 2020-11-27 16:56:37 | [diff] [blame] | 75 | |
| 76 | // Where the current scalability structure is in the |
Artem Titov | dcd7fc7 | 2021-08-09 11:02:57 | [diff] [blame] | 77 | // `scalability_structures_` array. |
philipel | 4e70216 | 2020-11-27 16:56:37 | [diff] [blame] | 78 | uint8_t current_ss_idx_ = 0; |
| 79 | |
| 80 | // Holds received scalability structures. |
| 81 | std::array<GofInfoVP9, kMaxGofSaved> scalability_structures_; |
| 82 | |
| 83 | // Holds the the Gof information for a given unwrapped TL0 picture index. |
| 84 | std::map<int64_t, GofInfo> gof_info_; |
| 85 | |
| 86 | // Keep track of which picture id and which temporal layer that had the |
| 87 | // up switch flag set. |
| 88 | std::map<uint16_t, uint8_t, DescendingSeqNumComp<uint16_t, kFrameIdLength>> |
| 89 | up_switch_; |
| 90 | |
| 91 | // For every temporal layer, keep a set of which frames that are missing. |
| 92 | std::array<std::set<uint16_t, DescendingSeqNumComp<uint16_t, kFrameIdLength>>, |
| 93 | kMaxTemporalLayers> |
| 94 | missing_frames_for_layer_; |
| 95 | |
| 96 | // Unwrapper used to unwrap VP8/VP9 streams which have their picture id |
| 97 | // specified. |
| 98 | SeqNumUnwrapper<uint16_t, kFrameIdLength> unwrapper_; |
| 99 | |
| 100 | SeqNumUnwrapper<uint8_t> tl0_unwrapper_; |
| 101 | }; |
| 102 | |
philipel | 4e70216 | 2020-11-27 16:56:37 | [diff] [blame] | 103 | } // namespace webrtc |
| 104 | |
| 105 | #endif // MODULES_VIDEO_CODING_RTP_VP9_REF_FINDER_H_ |