blob: 1ccfa3b1ed1c178394799b543755c60a323c2f25 [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 {
25namespace video_coding {
26
27class RtpVp9RefFinder {
28 public:
29 RtpVp9RefFinder() = default;
30
31 RtpFrameReferenceFinder::ReturnVector ManageFrame(
32 std::unique_ptr<RtpFrameObject> frame);
33 void ClearTo(uint16_t seq_num);
34
35 private:
36 static constexpr int kFrameIdLength = 1 << 15;
37 static constexpr int kMaxGofSaved = 50;
38 static constexpr int kMaxLayerInfo = 50;
39 static constexpr int kMaxNotYetReceivedFrames = 100;
40 static constexpr int kMaxStashedFrames = 100;
41 static constexpr int kMaxTemporalLayers = 5;
42
43 enum FrameDecision { kStash, kHandOff, kDrop };
44
45 struct GofInfo {
46 GofInfo(GofInfoVP9* gof, uint16_t last_picture_id)
47 : gof(gof), last_picture_id(last_picture_id) {}
48 GofInfoVP9* gof;
49 uint16_t last_picture_id;
50 };
51
52 FrameDecision ManageFrameInternal(RtpFrameObject* frame);
53 void RetryStashedFrames(RtpFrameReferenceFinder::ReturnVector& res);
54
55 bool MissingRequiredFrameVp9(uint16_t picture_id, const GofInfo& info);
56
57 void FrameReceivedVp9(uint16_t picture_id, GofInfo* info);
58 bool UpSwitchInIntervalVp9(uint16_t picture_id,
59 uint8_t temporal_idx,
60 uint16_t pid_ref);
61
philipel0cb73262020-12-08 17:36:53 +010062 void FlattenFrameIdAndRefs(RtpFrameObject* frame, bool inter_layer_predicted);
philipel4e702162020-11-27 17:56:37 +010063
64 // Save the last picture id in order to detect when there is a gap in frames
65 // that have not yet been fully received.
66 int last_picture_id_ = -1;
67
68 // Frames that have been fully received but didn't have all the information
69 // needed to determine their references.
70 std::deque<std::unique_ptr<RtpFrameObject>> stashed_frames_;
71
72 // Where the current scalability structure is in the
73 // |scalability_structures_| array.
74 uint8_t current_ss_idx_ = 0;
75
76 // Holds received scalability structures.
77 std::array<GofInfoVP9, kMaxGofSaved> scalability_structures_;
78
79 // Holds the the Gof information for a given unwrapped TL0 picture index.
80 std::map<int64_t, GofInfo> gof_info_;
81
82 // Keep track of which picture id and which temporal layer that had the
83 // up switch flag set.
84 std::map<uint16_t, uint8_t, DescendingSeqNumComp<uint16_t, kFrameIdLength>>
85 up_switch_;
86
87 // For every temporal layer, keep a set of which frames that are missing.
88 std::array<std::set<uint16_t, DescendingSeqNumComp<uint16_t, kFrameIdLength>>,
89 kMaxTemporalLayers>
90 missing_frames_for_layer_;
91
92 // Unwrapper used to unwrap VP8/VP9 streams which have their picture id
93 // specified.
94 SeqNumUnwrapper<uint16_t, kFrameIdLength> unwrapper_;
95
96 SeqNumUnwrapper<uint8_t> tl0_unwrapper_;
97};
98
99} // namespace video_coding
100} // namespace webrtc
101
102#endif // MODULES_VIDEO_CODING_RTP_VP9_REF_FINDER_H_