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 <limits>
19 #include <map>
20 #include <memory>
21 #include <set>
22 #include <utility>
23 #include <vector>
24 
25 #include "absl/types/optional.h"
26 #include "api/video_codecs/vp8_frame_config.h"
27 #include "api/video_codecs/vp8_temporal_layers.h"
28 #include "modules/video_coding/codecs/vp8/include/temporal_layers_checker.h"
29 #include "modules/video_coding/include/video_codec_interface.h"
30 
31 namespace webrtc {
32 
33 class DefaultTemporalLayers final : public Vp8FrameBufferController {
34  public:
35   explicit DefaultTemporalLayers(int number_of_temporal_layers);
36   ~DefaultTemporalLayers() override;
37 
38   void SetQpLimits(size_t stream_index, int min_qp, int max_qp) override;
39 
40   size_t StreamCount() const override;
41 
42   bool SupportsEncoderFrameDropping(size_t stream_index) const override;
43 
44   // Returns the recommended VP8 encode flags needed. May refresh the decoder
45   // and/or update the reference buffers.
46   Vp8FrameConfig NextFrameConfig(size_t stream_index,
47                                  uint32_t timestamp) override;
48 
49   // New target bitrate, per temporal layer.
50   void OnRatesUpdated(size_t stream_index,
51                       const std::vector<uint32_t>& bitrates_bps,
52                       int framerate_fps) override;
53 
54   Vp8EncoderConfig UpdateConfiguration(size_t stream_index) override;
55 
56   void OnEncodeDone(size_t stream_index,
57                     uint32_t rtp_timestamp,
58                     size_t size_bytes,
59                     bool is_keyframe,
60                     int qp,
61                     CodecSpecificInfo* info) override;
62 
63   void OnFrameDropped(size_t stream_index, uint32_t rtp_timestamp) override;
64 
65   void OnPacketLossRateUpdate(float packet_loss_rate) override;
66 
67   void OnRttUpdate(int64_t rtt_ms) override;
68 
69   void OnLossNotification(
70       const VideoEncoder::LossNotification& loss_notification) override;
71 
72  private:
73   struct DependencyInfo {
74     DependencyInfo() = default;
DependencyInfoDependencyInfo75     DependencyInfo(absl::string_view indication_symbols,
76                    Vp8FrameConfig frame_config)
77         : decode_target_indications(
78               webrtc_impl::StringToDecodeTargetIndications(indication_symbols)),
79           frame_config(frame_config) {}
80 
81     absl::InlinedVector<DecodeTargetIndication, 10> decode_target_indications;
82     Vp8FrameConfig frame_config;
83   };
84 
85   static std::vector<DependencyInfo> GetDependencyInfo(size_t num_layers);
86   bool IsSyncFrame(const Vp8FrameConfig& config) const;
87   void ValidateReferences(Vp8FrameConfig::BufferFlags* flags,
88                           Vp8FrameConfig::Vp8BufferReference ref) const;
89   void UpdateSearchOrder(Vp8FrameConfig* config);
90 
91   const size_t num_layers_;
92   const std::vector<unsigned int> temporal_ids_;
93   const std::vector<DependencyInfo> temporal_pattern_;
94   // Set of buffers that are never updated except by keyframes.
95   std::set<Vp8FrameConfig::Vp8BufferReference> kf_buffers_;
96   FrameDependencyStructure GetTemplateStructure(int num_layers) const;
97 
98   uint8_t pattern_idx_;
99   // Updated cumulative bitrates, per temporal layer.
100   absl::optional<std::vector<uint32_t>> new_bitrates_bps_;
101 
102   struct PendingFrame {
103     PendingFrame();
104     PendingFrame(bool expired,
105                  uint8_t updated_buffers_mask,
106                  const DependencyInfo& dependency_info);
107     // Flag indicating if this frame has expired, ie it belongs to a previous
108     // iteration of the temporal pattern.
109     bool expired = false;
110     // Bitmask of Vp8BufferReference flags, indicating which buffers this frame
111     // updates.
112     uint8_t updated_buffer_mask = 0;
113     // The frame config returned by NextFrameConfig() for this frame.
114     DependencyInfo dependency_info;
115   };
116   // Map from rtp timestamp to pending frame status. Reset on pattern loop.
117   std::map<uint32_t, PendingFrame> pending_frames_;
118 
119   // One counter per Vp8BufferReference, indicating number of frames since last
120   // refresh. For non-base-layer frames (ie golden, altref buffers), this is
121   // reset when the pattern loops.
122   std::map<Vp8FrameConfig::Vp8BufferReference, size_t>
123       frames_since_buffer_refresh_;
124 
125   // Optional utility used to verify reference validity.
126   std::unique_ptr<TemporalLayersChecker> checker_;
127 };
128 
129 class DefaultTemporalLayersChecker : public TemporalLayersChecker {
130  public:
131   explicit DefaultTemporalLayersChecker(int number_of_temporal_layers);
132   ~DefaultTemporalLayersChecker() override;
133 
134   bool CheckTemporalConfig(bool frame_is_keyframe,
135                            const Vp8FrameConfig& frame_config) override;
136 
137  private:
138   struct BufferState {
BufferStateBufferState139     BufferState()
140         : is_updated_this_cycle(false), is_keyframe(true), pattern_idx(0) {}
141 
142     bool is_updated_this_cycle;
143     bool is_keyframe;
144     uint8_t pattern_idx;
145   };
146   const size_t num_layers_;
147   std::vector<unsigned int> temporal_ids_;
148   const std::vector<std::set<uint8_t>> temporal_dependencies_;
149   BufferState last_;
150   BufferState arf_;
151   BufferState golden_;
152   uint8_t pattern_idx_;
153 };
154 
155 }  // namespace webrtc
156 #endif  // MODULES_VIDEO_CODING_CODECS_VP8_DEFAULT_TEMPORAL_LAYERS_H_
157