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 #ifdef __cplusplus 16 #include <cstddef> 17 #include <cstdint> 18 #include <memory> 19 #else 20 #include <stdbool.h> 21 #include <stddef.h> 22 #include <stdint.h> 23 #endif // __cplusplus 24 25 struct AV1_COMP; 26 27 typedef struct AomAV1LoopfilterLevel { 28 int filter_level[2]; 29 int filter_level_u; 30 int filter_level_v; 31 } AomAV1LoopfilterLevel; 32 33 typedef struct AomAV1CdefInfo { 34 int cdef_strength_y; 35 int cdef_strength_uv; 36 int damping; 37 } AomAV1CdefInfo; 38 39 typedef struct AomAV1SegmentationData { 40 const uint8_t *segmentation_map; 41 size_t segmentation_map_size; 42 const int *delta_q; 43 size_t delta_q_size; 44 } AomAV1SegmentationData; 45 46 typedef enum AomFrameType { kAomKeyFrame, kAomInterFrame } AomFrameType; 47 48 typedef struct AomAV1FrameParamsRTC { 49 AomFrameType frame_type; 50 int spatial_layer_id; 51 int temporal_layer_id; 52 } AomAV1FrameParamsRTC; 53 54 typedef enum AomFrameDropDecision { 55 kAomFrameDropDecisionOk, // Frame is encoded. 56 kAomFrameDropDecisionDrop, // Frame is dropped. 57 } AomFrameDropDecision; 58 59 // These constants come from AV1 spec. 60 enum { 61 kAomAV1MaxLayers = 32, 62 kAomAV1MaxTemporalLayers = 8, 63 kAomAV1MaxSpatialLayers = 4, 64 }; 65 66 typedef struct AomAV1RateControlRtcConfig { 67 #ifdef __cplusplus 68 AomAV1RateControlRtcConfig(); 69 #endif 70 71 int width; 72 int height; 73 // Flag indicating if the content is screen or not. 74 bool is_screen; 75 // 0-63 76 int max_quantizer; 77 int min_quantizer; 78 int64_t target_bandwidth; 79 int64_t buf_initial_sz; 80 int64_t buf_optimal_sz; 81 int64_t buf_sz; 82 int undershoot_pct; 83 int overshoot_pct; 84 int max_intra_bitrate_pct; 85 int max_inter_bitrate_pct; 86 int frame_drop_thresh; 87 int max_consec_drop_ms; 88 double framerate; 89 int layer_target_bitrate[kAomAV1MaxLayers]; 90 int ts_rate_decimator[kAomAV1MaxTemporalLayers]; 91 int aq_mode; 92 // Number of spatial layers 93 int ss_number_layers; 94 // Number of temporal layers 95 int ts_number_layers; 96 int max_quantizers[kAomAV1MaxLayers]; 97 int min_quantizers[kAomAV1MaxLayers]; 98 int scaling_factor_num[kAomAV1MaxSpatialLayers]; 99 int scaling_factor_den[kAomAV1MaxSpatialLayers]; 100 } AomAV1RateControlRtcConfig; 101 102 struct AomAV1RateControlRTC; 103 typedef struct AomAV1RateControlRTC AomAV1RateControlRTC; 104 105 #ifdef __cplusplus 106 namespace aom { 107 108 using AV1LoopfilterLevel = AomAV1LoopfilterLevel; 109 using AV1CdefInfo = AomAV1CdefInfo; 110 using AV1SegmentationData = AomAV1SegmentationData; 111 using AV1FrameParamsRTC = AomAV1FrameParamsRTC; 112 using AV1RateControlRtcConfig = AomAV1RateControlRtcConfig; 113 114 using FrameType = AomFrameType; 115 constexpr FrameType kKeyFrame = kAomKeyFrame; 116 constexpr FrameType kInterFrame = kAomInterFrame; 117 118 using FrameDropDecision = AomFrameDropDecision; 119 constexpr FrameDropDecision kFrameDropDecisionOk = kAomFrameDropDecisionOk; 120 constexpr FrameDropDecision kFrameDropDecisionDrop = kAomFrameDropDecisionDrop; 121 122 class AV1RateControlRTC { 123 public: 124 static std::unique_ptr<AV1RateControlRTC> Create( 125 const AV1RateControlRtcConfig &cfg); 126 ~AV1RateControlRTC(); 127 128 bool UpdateRateControl(const AV1RateControlRtcConfig &rc_cfg); 129 // GetQP() needs to be called after ComputeQP() to get the latest QP 130 int GetQP() const; 131 // GetLoopfilterLevel() needs to be called after ComputeQP() 132 AV1LoopfilterLevel GetLoopfilterLevel() const; 133 // GetCdefInfo() needs to be called after ComputeQP() 134 AV1CdefInfo GetCdefInfo() const; 135 // Returns the segmentation map used for cyclic refresh, based on 4x4 blocks. 136 bool GetSegmentationData(AV1SegmentationData *segmentation_data) const; 137 // ComputeQP returns the QP if the frame is not dropped (kOk return), 138 // otherwise it returns kDrop and subsequent GetQP and PostEncodeUpdate 139 // are not to be called (av1_rc_postencode_update_drop_frame is already 140 // called via ComputeQP if drop is decided). 141 FrameDropDecision ComputeQP(const AV1FrameParamsRTC &frame_params); 142 // Feedback to rate control with the size of current encoded frame 143 void PostEncodeUpdate(uint64_t encoded_frame_size); 144 145 private: 146 AV1RateControlRTC() = default; 147 bool InitRateControl(const AV1RateControlRtcConfig &cfg); 148 AV1_COMP *cpi_; 149 int initial_width_; 150 int initial_height_; 151 }; 152 } // namespace aom 153 #endif // __cplusplus 154 155 #ifdef __cplusplus 156 extern "C" { 157 #endif 158 AomAV1RateControlRTC *av1_ratecontrol_rtc_create( 159 const AomAV1RateControlRtcConfig *rc_cfg); 160 void av1_ratecontrol_rtc_destroy(AomAV1RateControlRTC *controller); 161 bool av1_ratecontrol_rtc_update(AomAV1RateControlRTC *controller, 162 const AomAV1RateControlRtcConfig *rc_cfg); 163 int av1_ratecontrol_rtc_get_qp(const AomAV1RateControlRTC *controller); 164 165 AomAV1LoopfilterLevel av1_ratecontrol_rtc_get_loop_filter_level( 166 const AomAV1RateControlRTC *controller); 167 AomFrameDropDecision av1_ratecontrol_rtc_compute_qp( 168 AomAV1RateControlRTC *controller, const AomAV1FrameParamsRTC *frame_params); 169 170 void av1_ratecontrol_rtc_post_encode_update(AomAV1RateControlRTC *controller, 171 uint64_t encoded_frame_size); 172 173 bool av1_ratecontrol_rtc_get_segmentation( 174 const AomAV1RateControlRTC *controller, 175 AomAV1SegmentationData *segmentation_data); 176 177 AomAV1CdefInfo av1_ratecontrol_rtc_get_cdef_info( 178 const AomAV1RateControlRTC *controller); 179 180 void av1_ratecontrol_rtc_init_ratecontrol_config( 181 AomAV1RateControlRtcConfig *config); 182 #ifdef __cplusplus 183 } // extern "C" 184 #endif 185 186 #endif // AOM_AV1_RATECTRL_RTC_H_ 187