blob: dc912b6f42ba6cb265880dc8441ce1ac144441e6 [file] [log] [blame]
philipel4e702162020-11-27 17:56:37 +01001/*
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"
20#include "modules/video_coding/frame_object.h"
21#include "modules/video_coding/rtp_frame_reference_finder.h"
22#include "rtc_base/numerics/sequence_number_util.h"
23
24namespace webrtc {
philipel4e702162020-11-27 17:56:37 +010025
26class RtpVp9RefFinder {
27 public:
28 RtpVp9RefFinder() = default;
29
30 RtpFrameReferenceFinder::ReturnVector ManageFrame(
philipelca188092021-03-23 12:00:49 +010031 std::unique_ptr<RtpFrameObject> frame);
philipel4e702162020-11-27 17:56:37 +010032 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 {
philipeldbab1be2021-07-19 18:43:34 +020045 GofInfo(GofInfoVP9* gof, uint16_t last_picture_id, uint16_t last_seq_num)
46 : gof(gof),
47 last_picture_id(last_picture_id),
48 last_seq_num(last_seq_num) {}
philipel4e702162020-11-27 17:56:37 +010049 GofInfoVP9* gof;
50 uint16_t last_picture_id;
philipeldbab1be2021-07-19 18:43:34 +020051 uint16_t last_seq_num;
philipel4e702162020-11-27 17:56:37 +010052 };
53
philipelca188092021-03-23 12:00:49 +010054 FrameDecision ManageFrameInternal(RtpFrameObject* frame);
philipel4e702162020-11-27 17:56:37 +010055 void RetryStashedFrames(RtpFrameReferenceFinder::ReturnVector& res);
56
57 bool MissingRequiredFrameVp9(uint16_t picture_id, const GofInfo& info);
58
59 void FrameReceivedVp9(uint16_t picture_id, GofInfo* info);
60 bool UpSwitchInIntervalVp9(uint16_t picture_id,
61 uint8_t temporal_idx,
62 uint16_t pid_ref);
63
philipelca188092021-03-23 12:00:49 +010064 void FlattenFrameIdAndRefs(RtpFrameObject* frame, bool inter_layer_predicted);
philipel4e702162020-11-27 17:56:37 +010065
66 // Save the last picture id in order to detect when there is a gap in frames
67 // that have not yet been fully received.
68 int last_picture_id_ = -1;
69
70 // Frames that have been fully received but didn't have all the information
71 // needed to determine their references.
philipelca188092021-03-23 12:00:49 +010072 std::deque<std::unique_ptr<RtpFrameObject>> stashed_frames_;
philipel4e702162020-11-27 17:56:37 +010073
74 // Where the current scalability structure is in the
75 // |scalability_structures_| array.
76 uint8_t current_ss_idx_ = 0;
77
78 // Holds received scalability structures.
79 std::array<GofInfoVP9, kMaxGofSaved> scalability_structures_;
80
81 // Holds the the Gof information for a given unwrapped TL0 picture index.
82 std::map<int64_t, GofInfo> gof_info_;
83
84 // Keep track of which picture id and which temporal layer that had the
85 // up switch flag set.
86 std::map<uint16_t, uint8_t, DescendingSeqNumComp<uint16_t, kFrameIdLength>>
87 up_switch_;
88
89 // For every temporal layer, keep a set of which frames that are missing.
90 std::array<std::set<uint16_t, DescendingSeqNumComp<uint16_t, kFrameIdLength>>,
91 kMaxTemporalLayers>
92 missing_frames_for_layer_;
93
94 // Unwrapper used to unwrap VP8/VP9 streams which have their picture id
95 // specified.
96 SeqNumUnwrapper<uint16_t, kFrameIdLength> unwrapper_;
97
98 SeqNumUnwrapper<uint8_t> tl0_unwrapper_;
99};
100
philipel4e702162020-11-27 17:56:37 +0100101} // namespace webrtc
102
103#endif // MODULES_VIDEO_CODING_RTP_VP9_REF_FINDER_H_