xref: /aosp_15_r20/external/libaom/av1/ratectrl_rtc.h (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1 /*
2  * Copyright (c) 2021, Alliance for Open Media. All rights reserved.
3  *
4  * This source code is subject to the terms of the BSD 2 Clause License and
5  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6  * was not distributed with this source code in the LICENSE file, you can
7  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8  * Media Patent License 1.0 was not distributed with this source code in the
9  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10  */
11 
12 #ifndef AOM_AV1_RATECTRL_RTC_H_
13 #define AOM_AV1_RATECTRL_RTC_H_
14 
15 #include <cstdint>
16 #include <memory>
17 
18 struct AV1_COMP;
19 
20 namespace aom {
21 
22 // These constants come from AV1 spec.
23 static constexpr size_t kAV1MaxLayers = 32;
24 static constexpr size_t kAV1MaxTemporalLayers = 8;
25 static constexpr size_t kAV1MaxSpatialLayers = 4;
26 
27 enum FrameType { kKeyFrame, kInterFrame };
28 
29 struct AV1RateControlRtcConfig {
30  public:
31   AV1RateControlRtcConfig();
32 
33   int width;
34   int height;
35   // Flag indicating if the content is screen or not.
36   bool is_screen = false;
37   // 0-63
38   int max_quantizer;
39   int min_quantizer;
40   int64_t target_bandwidth;
41   int64_t buf_initial_sz;
42   int64_t buf_optimal_sz;
43   int64_t buf_sz;
44   int undershoot_pct;
45   int overshoot_pct;
46   int max_intra_bitrate_pct;
47   int max_inter_bitrate_pct;
48   int frame_drop_thresh;
49   int max_consec_drop_ms;
50   double framerate;
51   int layer_target_bitrate[kAV1MaxLayers];
52   int ts_rate_decimator[kAV1MaxTemporalLayers];
53   int aq_mode;
54   // Number of spatial layers
55   int ss_number_layers;
56   // Number of temporal layers
57   int ts_number_layers;
58   int max_quantizers[kAV1MaxLayers];
59   int min_quantizers[kAV1MaxLayers];
60   int scaling_factor_num[kAV1MaxSpatialLayers];
61   int scaling_factor_den[kAV1MaxSpatialLayers];
62 };
63 
64 struct AV1FrameParamsRTC {
65   FrameType frame_type;
66   int spatial_layer_id;
67   int temporal_layer_id;
68 };
69 
70 struct AV1LoopfilterLevel {
71   int filter_level[2];
72   int filter_level_u;
73   int filter_level_v;
74 };
75 
76 struct AV1CdefInfo {
77   int cdef_strength_y;
78   int cdef_strength_uv;
79   int damping;
80 };
81 
82 struct AV1SegmentationData {
83   const uint8_t *segmentation_map;
84   size_t segmentation_map_size;
85   const int *delta_q;
86   size_t delta_q_size;
87 };
88 
89 enum class FrameDropDecision {
90   kOk,    // Frame is encoded.
91   kDrop,  // Frame is dropped.
92 };
93 
94 class AV1RateControlRTC {
95  public:
96   static std::unique_ptr<AV1RateControlRTC> Create(
97       const AV1RateControlRtcConfig &cfg);
98   ~AV1RateControlRTC();
99 
100   bool UpdateRateControl(const AV1RateControlRtcConfig &rc_cfg);
101   // GetQP() needs to be called after ComputeQP() to get the latest QP
102   int GetQP() const;
103   // GetLoopfilterLevel() needs to be called after ComputeQP()
104   AV1LoopfilterLevel GetLoopfilterLevel() const;
105   // GetCdefInfo() needs to be called after ComputeQP()
106   AV1CdefInfo GetCdefInfo() const;
107   // Returns the segmentation map used for cyclic refresh, based on 4x4 blocks.
108   bool GetSegmentationData(AV1SegmentationData *segmentation_data) const;
109   // ComputeQP returns the QP if the frame is not dropped (kOk return),
110   // otherwise it returns kDrop and subsequent GetQP and PostEncodeUpdate
111   // are not to be called (av1_rc_postencode_update_drop_frame is already
112   // called via ComputeQP if drop is decided).
113   FrameDropDecision ComputeQP(const AV1FrameParamsRTC &frame_params);
114   // Feedback to rate control with the size of current encoded frame
115   void PostEncodeUpdate(uint64_t encoded_frame_size);
116 
117  private:
118   AV1RateControlRTC() = default;
119   bool InitRateControl(const AV1RateControlRtcConfig &cfg);
120   AV1_COMP *cpi_;
121   int initial_width_;
122   int initial_height_;
123 };
124 
125 }  // namespace aom
126 
127 #endif  // AOM_AV1_RATECTRL_RTC_H_
128