1 /* 2 * Copyright (c) 2016, 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_ENCODER_RATECTRL_H_ 13 #define AOM_AV1_ENCODER_RATECTRL_H_ 14 15 #include "aom/aom_codec.h" 16 #include "aom/aom_integer.h" 17 18 #include "av1/common/blockd.h" 19 20 #ifdef __cplusplus 21 extern "C" { 22 #endif 23 24 // Bits Per MB at different Q (Multiplied by 512) 25 #define BPER_MB_NORMBITS 9 26 27 #define CUSTOMIZED_GF 1 28 29 #if CONFIG_FIX_GF_LENGTH 30 #define FIXED_GF_LENGTH 16 31 #define MAX_PYRAMID_LVL 4 32 // We allow a frame to have at most two left/right descendants before changing 33 // them into to a subtree, i.e., we allow the following structure: 34 /* OUT_OF_ORDER_FRAME 35 / / \ \ 36 (two left children) F F F F (two right children) */ 37 // Therefore the max gf size supported by 4 layer structure is 38 // 1 (KEY/OVERLAY) + 1 + 2 + 4 + 16 (two children on both side of their parent) 39 #define MAX_PYRAMID_SIZE 24 40 #define USE_SYMM_MULTI_LAYER 1 41 #define REDUCE_LAST_ALT_BOOST 1 42 #define REDUCE_LAST_GF_LENGTH 1 43 #define MULTI_LVL_BOOST_VBR_CQ 1 44 #else 45 #define USE_SYMM_MULTI_LAYER 0 46 #define REDUCE_LAST_ALT_BOOST 0 47 #define REDUCE_LAST_GF_LENGTH 0 48 #define MULTI_LVL_BOOST_VBR_CQ 0 49 #endif 50 51 #if USE_SYMM_MULTI_LAYER 52 #define USE_MANUAL_GF4_STRUCT 0 53 #endif 54 55 #define MIN_GF_INTERVAL 4 56 #define MAX_GF_INTERVAL 16 57 #define FIXED_GF_INTERVAL 8 // Used in some testing modes only 58 59 typedef enum { 60 INTER_NORMAL = 0, 61 INTER_LOW = 1, 62 INTER_HIGH = 2, 63 GF_ARF_LOW = 3, 64 GF_ARF_STD = 4, 65 KF_STD = 5, 66 RATE_FACTOR_LEVELS = 6 67 } RATE_FACTOR_LEVEL; 68 69 static const double rate_factor_deltas[RATE_FACTOR_LEVELS] = { 70 1.00, // INTER_NORMAL 71 0.80, // INTER_LOW 72 1.50, // INTER_HIGH 73 1.25, // GF_ARF_LOW 74 2.00, // GF_ARF_STD 75 2.00, // KF_STD 76 }; 77 78 typedef struct { 79 int resize_width; 80 int resize_height; 81 uint8_t superres_denom; 82 } size_params_type; 83 84 typedef struct { 85 // Rate targetting variables 86 int base_frame_target; // A baseline frame target before adjustment 87 // for previous under or over shoot. 88 int this_frame_target; // Actual frame target after rc adjustment. 89 int projected_frame_size; 90 int sb64_target_rate; 91 int last_q[FRAME_TYPES]; // Separate values for Intra/Inter 92 int last_boosted_qindex; // Last boosted GF/KF/ARF q 93 int last_kf_qindex; // Q index of the last key frame coded. 94 95 int gfu_boost; 96 int last_boost; 97 int kf_boost; 98 99 double rate_correction_factors[RATE_FACTOR_LEVELS]; 100 101 int frames_since_golden; 102 int frames_till_gf_update_due; 103 int min_gf_interval; 104 int max_gf_interval; 105 int static_scene_max_gf_interval; 106 int baseline_gf_interval; 107 int constrained_gf_group; 108 int frames_to_key; 109 int frames_since_key; 110 int this_key_frame_forced; 111 int next_key_frame_forced; 112 int source_alt_ref_pending; 113 int source_alt_ref_active; 114 int is_src_frame_alt_ref; 115 int sframe_due; 116 117 // Length of the bi-predictive frame group interval 118 int bipred_group_interval; 119 120 // NOTE: Different types of frames may have different bits allocated 121 // accordingly, aiming to achieve the overall optimal RD performance. 122 int is_bwd_ref_frame; 123 int is_last_bipred_frame; 124 int is_bipred_frame; 125 int is_src_frame_ext_arf; 126 127 int avg_frame_bandwidth; // Average frame size target for clip 128 int min_frame_bandwidth; // Minimum allocation used for any frame 129 int max_frame_bandwidth; // Maximum burst rate allowed for a frame. 130 131 int ni_av_qi; 132 int ni_tot_qi; 133 int ni_frames; 134 int avg_frame_qindex[FRAME_TYPES]; 135 double tot_q; 136 double avg_q; 137 138 int64_t buffer_level; 139 int64_t bits_off_target; 140 int64_t vbr_bits_off_target; 141 int64_t vbr_bits_off_target_fast; 142 143 int decimation_factor; 144 int decimation_count; 145 146 int rolling_target_bits; 147 int rolling_actual_bits; 148 149 int long_rolling_target_bits; 150 int long_rolling_actual_bits; 151 152 int rate_error_estimate; 153 154 int64_t total_actual_bits; 155 int64_t total_target_bits; 156 int64_t total_target_vs_actual; 157 158 int worst_quality; 159 int best_quality; 160 161 int64_t starting_buffer_level; 162 int64_t optimal_buffer_level; 163 int64_t maximum_buffer_size; 164 165 // rate control history for last frame(1) and the frame before(2). 166 // -1: undershot 167 // 1: overshoot 168 // 0: not initialized. 169 int rc_1_frame; 170 int rc_2_frame; 171 int q_1_frame; 172 int q_2_frame; 173 174 // Auto frame-scaling variables. 175 int rf_level_maxq[RATE_FACTOR_LEVELS]; 176 float arf_boost_factor; 177 // Q index used for ALT frame 178 int arf_q; 179 } RATE_CONTROL; 180 181 struct AV1_COMP; 182 struct AV1EncoderConfig; 183 184 void av1_rc_init(const struct AV1EncoderConfig *oxcf, int pass, 185 RATE_CONTROL *rc); 186 187 int av1_estimate_bits_at_q(FRAME_TYPE frame_kind, int q, int mbs, 188 double correction_factor, aom_bit_depth_t bit_depth); 189 190 double av1_convert_qindex_to_q(int qindex, aom_bit_depth_t bit_depth); 191 192 void av1_rc_init_minq_luts(void); 193 194 int av1_rc_get_default_min_gf_interval(int width, int height, double framerate); 195 // Note av1_rc_get_default_max_gf_interval() requires the min_gf_interval to 196 // be passed in to ensure that the max_gf_interval returned is at least as bis 197 // as that. 198 int av1_rc_get_default_max_gf_interval(double framerate, int min_frame_rate); 199 200 // Generally at the high level, the following flow is expected 201 // to be enforced for rate control: 202 // First call per frame, one of: 203 // av1_rc_get_one_pass_vbr_params() 204 // av1_rc_get_one_pass_cbr_params() 205 // av1_rc_get_first_pass_params() 206 // av1_rc_get_second_pass_params() 207 // depending on the usage to set the rate control encode parameters desired. 208 // 209 // Then, call encode_frame_to_data_rate() to perform the 210 // actual encode. This function will in turn call encode_frame() 211 // one or more times, followed by one of: 212 // av1_rc_postencode_update() 213 // av1_rc_postencode_update_drop_frame() 214 // 215 // The majority of rate control parameters are only expected 216 // to be set in the av1_rc_get_..._params() functions and 217 // updated during the av1_rc_postencode_update...() functions. 218 // The only exceptions are av1_rc_drop_frame() and 219 // av1_rc_update_rate_correction_factors() functions. 220 221 // Functions to set parameters for encoding before the actual 222 // encode_frame_to_data_rate() function. 223 void av1_rc_get_one_pass_vbr_params(struct AV1_COMP *cpi); 224 void av1_rc_get_one_pass_cbr_params(struct AV1_COMP *cpi); 225 226 // Post encode update of the rate control parameters based 227 // on bytes used 228 void av1_rc_postencode_update(struct AV1_COMP *cpi, uint64_t bytes_used); 229 // Post encode update of the rate control parameters for dropped frames 230 void av1_rc_postencode_update_drop_frame(struct AV1_COMP *cpi); 231 232 // Updates rate correction factors 233 // Changes only the rate correction factors in the rate control structure. 234 void av1_rc_update_rate_correction_factors(struct AV1_COMP *cpi, int width, 235 int height); 236 237 // Decide if we should drop this frame: For 1-pass CBR. 238 // Changes only the decimation count in the rate control structure 239 int av1_rc_drop_frame(struct AV1_COMP *cpi); 240 241 // Computes frame size bounds. 242 void av1_rc_compute_frame_size_bounds(const struct AV1_COMP *cpi, 243 int this_frame_target, 244 int *frame_under_shoot_limit, 245 int *frame_over_shoot_limit); 246 247 // Picks q and q bounds given the target for bits 248 int av1_rc_pick_q_and_bounds(struct AV1_COMP *cpi, int width, int height, 249 int *bottom_index, int *top_index); 250 251 // Estimates q to achieve a target bits per frame 252 int av1_rc_regulate_q(const struct AV1_COMP *cpi, int target_bits_per_frame, 253 int active_best_quality, int active_worst_quality, 254 int width, int height); 255 256 // Estimates bits per mb for a given qindex and correction factor. 257 int av1_rc_bits_per_mb(FRAME_TYPE frame_type, int qindex, 258 double correction_factor, aom_bit_depth_t bit_depth); 259 260 // Clamping utilities for bitrate targets for iframes and pframes. 261 int av1_rc_clamp_iframe_target_size(const struct AV1_COMP *const cpi, 262 int target); 263 int av1_rc_clamp_pframe_target_size(const struct AV1_COMP *const cpi, 264 int target); 265 // Utility to set frame_target into the RATE_CONTROL structure 266 // This function is called only from the av1_rc_get_..._params() functions. 267 void av1_rc_set_frame_target(struct AV1_COMP *cpi, int target); 268 269 // Computes a q delta (in "q index" terms) to get from a starting q value 270 // to a target q value 271 int av1_compute_qdelta(const RATE_CONTROL *rc, double qstart, double qtarget, 272 aom_bit_depth_t bit_depth); 273 274 // Computes a q delta (in "q index" terms) to get from a starting q value 275 // to a value that should equate to the given rate ratio. 276 int av1_compute_qdelta_by_rate(const RATE_CONTROL *rc, FRAME_TYPE frame_type, 277 int qindex, double rate_target_ratio, 278 aom_bit_depth_t bit_depth); 279 280 int av1_frame_type_qdelta(const struct AV1_COMP *cpi, int rf_level, int q); 281 282 void av1_rc_update_framerate(struct AV1_COMP *cpi, int width, int height); 283 284 void av1_rc_set_gf_interval_range(const struct AV1_COMP *const cpi, 285 RATE_CONTROL *const rc); 286 287 void av1_set_target_rate(struct AV1_COMP *cpi, int width, int height); 288 289 int av1_resize_one_pass_cbr(struct AV1_COMP *cpi); 290 291 #ifdef __cplusplus 292 } // extern "C" 293 #endif 294 295 #endif // AOM_AV1_ENCODER_RATECTRL_H_ 296