xref: /aosp_15_r20/external/webrtc/modules/video_coding/codecs/vp8/default_temporal_layers.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /* Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
2  *
3  *  Use of this source code is governed by a BSD-style license
4  *  that can be found in the LICENSE file in the root of the source
5  *  tree. An additional intellectual property rights grant can be found
6  *  in the file PATENTS.  All contributing project authors may
7  *  be found in the AUTHORS file in the root of the source tree.
8  */
9 /*
10  * This file defines classes for doing temporal layers with VP8.
11  */
12 #ifndef MODULES_VIDEO_CODING_CODECS_VP8_DEFAULT_TEMPORAL_LAYERS_H_
13 #define MODULES_VIDEO_CODING_CODECS_VP8_DEFAULT_TEMPORAL_LAYERS_H_
14 
15 #include <stddef.h>
16 #include <stdint.h>
17 
18 #include <bitset>
19 #include <deque>
20 #include <limits>
21 #include <memory>
22 #include <set>
23 #include <utility>
24 #include <vector>
25 
26 #include "absl/types/optional.h"
27 #include "api/video_codecs/vp8_frame_config.h"
28 #include "api/video_codecs/vp8_temporal_layers.h"
29 #include "modules/video_coding/codecs/vp8/include/temporal_layers_checker.h"
30 #include "modules/video_coding/include/video_codec_interface.h"
31 
32 namespace webrtc {
33 
34 class DefaultTemporalLayers final : public Vp8FrameBufferController {
35  public:
36   explicit DefaultTemporalLayers(int number_of_temporal_layers);
37   ~DefaultTemporalLayers() override;
38 
39   void SetQpLimits(size_t stream_index, int min_qp, int max_qp) override;
40 
41   size_t StreamCount() const override;
42 
43   bool SupportsEncoderFrameDropping(size_t stream_index) const override;
44 
45   // Returns the recommended VP8 encode flags needed. May refresh the decoder
46   // and/or update the reference buffers.
47   Vp8FrameConfig NextFrameConfig(size_t stream_index,
48                                  uint32_t timestamp) override;
49 
50   // New target bitrate, per temporal layer.
51   void OnRatesUpdated(size_t stream_index,
52                       const std::vector<uint32_t>& bitrates_bps,
53                       int framerate_fps) override;
54 
55   Vp8EncoderConfig UpdateConfiguration(size_t stream_index) override;
56 
57   // Callbacks methods on frame completion. OnEncodeDone() or OnFrameDropped()
58   // should be called once for each NextFrameConfig() call (using the RTP
59   // timestamp as ID), and the calls MUST be in the same order.
60   void OnEncodeDone(size_t stream_index,
61                     uint32_t rtp_timestamp,
62                     size_t size_bytes,
63                     bool is_keyframe,
64                     int qp,
65                     CodecSpecificInfo* info) override;
66   void OnFrameDropped(size_t stream_index, uint32_t rtp_timestamp) override;
67 
68   void OnPacketLossRateUpdate(float packet_loss_rate) override;
69 
70   void OnRttUpdate(int64_t rtt_ms) override;
71 
72   void OnLossNotification(
73       const VideoEncoder::LossNotification& loss_notification) override;
74 
75  private:
76   static constexpr size_t kNumReferenceBuffers = 3;  // Last, golden, altref.
77   struct DependencyInfo {
78     DependencyInfo() = default;
DependencyInfoDependencyInfo79     DependencyInfo(absl::string_view indication_symbols,
80                    Vp8FrameConfig frame_config)
81         : decode_target_indications(
82               webrtc_impl::StringToDecodeTargetIndications(indication_symbols)),
83           frame_config(frame_config) {}
84 
85     absl::InlinedVector<DecodeTargetIndication, 10> decode_target_indications;
86     Vp8FrameConfig frame_config;
87   };
88   struct PendingFrame {
89     PendingFrame();
90     PendingFrame(uint32_t timestamp,
91                  bool expired,
92                  uint8_t updated_buffers_mask,
93                  const DependencyInfo& dependency_info);
94     uint32_t timestamp = 0;
95     // Flag indicating if this frame has expired, ie it belongs to a previous
96     // iteration of the temporal pattern.
97     bool expired = false;
98     // Bitmask of Vp8BufferReference flags, indicating which buffers this frame
99     // updates.
100     uint8_t updated_buffer_mask = 0;
101     // The frame config returned by NextFrameConfig() for this frame.
102     DependencyInfo dependency_info;
103   };
104 
105   static std::vector<DependencyInfo> GetDependencyInfo(size_t num_layers);
106   static std::bitset<kNumReferenceBuffers> DetermineStaticBuffers(
107       const std::vector<DependencyInfo>& temporal_pattern);
108   bool IsSyncFrame(const Vp8FrameConfig& config) const;
109   void ValidateReferences(Vp8FrameConfig::BufferFlags* flags,
110                           Vp8FrameConfig::Vp8BufferReference ref) const;
111   void UpdateSearchOrder(Vp8FrameConfig* config);
112   size_t NumFramesSinceBufferRefresh(
113       Vp8FrameConfig::Vp8BufferReference ref) const;
114   void ResetNumFramesSinceBufferRefresh(Vp8FrameConfig::Vp8BufferReference ref);
115   void CullPendingFramesBefore(uint32_t timestamp);
116 
117   const size_t num_layers_;
118   const std::vector<unsigned int> temporal_ids_;
119   const std::vector<DependencyInfo> temporal_pattern_;
120   // Per reference buffer flag indicating if it is static, meaning it is only
121   // updated by key-frames.
122   const std::bitset<kNumReferenceBuffers> is_static_buffer_;
123   FrameDependencyStructure GetTemplateStructure(int num_layers) const;
124 
125   uint8_t pattern_idx_;
126   // Updated cumulative bitrates, per temporal layer.
127   absl::optional<std::vector<uint32_t>> new_bitrates_bps_;
128 
129   // Status for each pending frame, in
130   std::deque<PendingFrame> pending_frames_;
131 
132   // One counter per reference buffer, indicating number of frames since last
133   // refresh. For non-base-layer frames (ie golden, altref buffers), this is
134   // reset when the pattern loops.
135   std::array<size_t, kNumReferenceBuffers> frames_since_buffer_refresh_;
136 
137   // Optional utility used to verify reference validity.
138   std::unique_ptr<TemporalLayersChecker> checker_;
139 };
140 
141 class DefaultTemporalLayersChecker : public TemporalLayersChecker {
142  public:
143   explicit DefaultTemporalLayersChecker(int number_of_temporal_layers);
144   ~DefaultTemporalLayersChecker() override;
145 
146   bool CheckTemporalConfig(bool frame_is_keyframe,
147                            const Vp8FrameConfig& frame_config) override;
148 
149  private:
150   struct BufferState {
BufferStateBufferState151     BufferState()
152         : is_updated_this_cycle(false), is_keyframe(true), pattern_idx(0) {}
153 
154     bool is_updated_this_cycle;
155     bool is_keyframe;
156     uint8_t pattern_idx;
157   };
158   const size_t num_layers_;
159   std::vector<unsigned int> temporal_ids_;
160   const std::vector<std::set<uint8_t>> temporal_dependencies_;
161   BufferState last_;
162   BufferState arf_;
163   BufferState golden_;
164   uint8_t pattern_idx_;
165 };
166 
167 }  // namespace webrtc
168 #endif  // MODULES_VIDEO_CODING_CODECS_VP8_DEFAULT_TEMPORAL_LAYERS_H_
169