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 "aom_ports/mem.h" 19 20 #include "av1/common/av1_common_int.h" 21 #include "av1/common/blockd.h" 22 23 #ifdef __cplusplus 24 extern "C" { 25 #endif 26 27 // Bits Per MB at different Q (Multiplied by 512) 28 #define BPER_MB_NORMBITS 9 29 30 // Use this macro to turn on/off use of alt-refs in one-pass mode. 31 #define USE_ALTREF_FOR_ONE_PASS 1 32 33 // Threshold used to define if a KF group is static (e.g. a slide show). 34 // Essentially, this means that no frame in the group has more than 1% of MBs 35 // that are not marked as coded with 0,0 motion in the first pass. 36 #define STATIC_KF_GROUP_THRESH 99 37 #define STATIC_KF_GROUP_FLOAT_THRESH 0.99 38 39 // The maximum duration of a GF group that is static (e.g. a slide show). 40 #define MAX_STATIC_GF_GROUP_LENGTH 250 41 42 // Minimum and maximum height for the new pyramid structure. 43 // (Old structure supports height = 1, but does NOT support height = 4). 44 #define MIN_PYRAMID_LVL 0 45 #define MAX_PYRAMID_LVL 4 46 47 #define MIN_GF_INTERVAL 4 48 #define MAX_GF_INTERVAL 32 49 #define FIXED_GF_INTERVAL 8 // Used in some testing modes only 50 #define MAX_GF_LENGTH_LAP 16 51 52 #define MAX_NUM_GF_INTERVALS 15 53 54 #define MAX_ARF_LAYERS 6 55 // #define STRICT_RC 56 57 typedef struct { 58 int resize_width; 59 int resize_height; 60 uint8_t superres_denom; 61 } size_params_type; 62 63 enum { 64 INTER_NORMAL, 65 GF_ARF_LOW, 66 GF_ARF_STD, 67 KF_STD, 68 RATE_FACTOR_LEVELS 69 } UENUM1BYTE(RATE_FACTOR_LEVEL); 70 71 enum { 72 KF_UPDATE, 73 LF_UPDATE, 74 GF_UPDATE, 75 ARF_UPDATE, 76 OVERLAY_UPDATE, 77 INTNL_OVERLAY_UPDATE, // Internal Overlay Frame 78 INTNL_ARF_UPDATE, // Internal Altref Frame 79 FRAME_UPDATE_TYPES 80 } UENUM1BYTE(FRAME_UPDATE_TYPE); 81 82 typedef struct { 83 // Rate targetting variables 84 int base_frame_target; // A baseline frame target before adjustment 85 // for previous under or over shoot. 86 int this_frame_target; // Actual frame target after rc adjustment. 87 88 // gop bit budget 89 int64_t gf_group_bits; 90 91 int projected_frame_size; 92 int sb64_target_rate; 93 int last_q[FRAME_TYPES]; // Separate values for Intra/Inter 94 int last_boosted_qindex; // Last boosted GF/KF/ARF q 95 int last_kf_qindex; // Q index of the last key frame coded. 96 97 int gfu_boost; 98 int kf_boost; 99 100 double rate_correction_factors[RATE_FACTOR_LEVELS]; 101 102 int frames_since_golden; 103 int frames_till_gf_update_due; 104 105 // number of determined gf group length left 106 int intervals_till_gf_calculate_due; 107 // stores gf group length intervals 108 int gf_intervals[MAX_NUM_GF_INTERVALS]; 109 // the current index in gf_intervals 110 int cur_gf_index; 111 112 int min_gf_interval; 113 int max_gf_interval; 114 int static_scene_max_gf_interval; 115 int baseline_gf_interval; 116 int constrained_gf_group; 117 int frames_to_key; 118 int frames_since_key; 119 int this_key_frame_forced; 120 int next_key_frame_forced; 121 int source_alt_ref_pending; 122 int source_alt_ref_active; 123 int is_src_frame_alt_ref; 124 int sframe_due; 125 126 int avg_frame_bandwidth; // Average frame size target for clip 127 int min_frame_bandwidth; // Minimum allocation used for any frame 128 int max_frame_bandwidth; // Maximum burst rate allowed for a frame. 129 int prev_avg_frame_bandwidth; 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 float_t arf_boost_factor; 175 // Q index used for ALT frame 176 int arf_q; 177 int active_worst_quality; 178 int active_best_quality[MAX_ARF_LAYERS + 1]; 179 int base_layer_qp; 180 181 // Total number of stats used only for kf_boost calculation. 182 int num_stats_used_for_kf_boost; 183 // Total number of stats used only for gfu_boost calculation. 184 int num_stats_used_for_gfu_boost; 185 // Total number of stats required by gfu_boost calculation. 186 int num_stats_required_for_gfu_boost; 187 int next_is_fwd_key; 188 int enable_scenecut_detection; 189 } RATE_CONTROL; 190 191 struct AV1_COMP; 192 struct AV1EncoderConfig; 193 194 void av1_rc_init(const struct AV1EncoderConfig *oxcf, int pass, 195 RATE_CONTROL *rc); 196 197 int av1_estimate_bits_at_q(FRAME_TYPE frame_kind, int q, int mbs, 198 double correction_factor, aom_bit_depth_t bit_depth); 199 200 double av1_convert_qindex_to_q(int qindex, aom_bit_depth_t bit_depth); 201 202 void av1_rc_init_minq_luts(void); 203 204 int av1_rc_get_default_min_gf_interval(int width, int height, double framerate); 205 // Note av1_rc_get_default_max_gf_interval() requires the min_gf_interval to 206 // be passed in to ensure that the max_gf_interval returned is at least as bis 207 // as that. 208 int av1_rc_get_default_max_gf_interval(double framerate, int min_gf_interval); 209 210 // Generally at the high level, the following flow is expected 211 // to be enforced for rate control: 212 // First call per frame, one of: 213 // av1_rc_get_first_pass_params() 214 // av1_rc_get_second_pass_params() 215 // depending on the usage to set the rate control encode parameters desired. 216 // 217 // Then, call encode_frame_to_data_rate() to perform the 218 // actual encode. This function will in turn call encode_frame() 219 // one or more times, followed by one of: 220 // av1_rc_postencode_update() 221 // av1_rc_postencode_update_drop_frame() 222 // 223 // The majority of rate control parameters are only expected 224 // to be set in the av1_rc_get_..._params() functions and 225 // updated during the av1_rc_postencode_update...() functions. 226 // The only exceptions are av1_rc_drop_frame() and 227 // av1_rc_update_rate_correction_factors() functions. 228 229 // Functions to set parameters for encoding before the actual 230 // encode_frame_to_data_rate() function. 231 struct EncodeFrameParams; 232 233 // Post encode update of the rate control parameters based 234 // on bytes used 235 void av1_rc_postencode_update(struct AV1_COMP *cpi, uint64_t bytes_used); 236 // Post encode update of the rate control parameters for dropped frames 237 void av1_rc_postencode_update_drop_frame(struct AV1_COMP *cpi); 238 239 // Updates rate correction factors 240 // Changes only the rate correction factors in the rate control structure. 241 void av1_rc_update_rate_correction_factors(struct AV1_COMP *cpi, int width, 242 int height); 243 244 // Decide if we should drop this frame: For 1-pass CBR. 245 // Changes only the decimation count in the rate control structure 246 int av1_rc_drop_frame(struct AV1_COMP *cpi); 247 248 // Computes frame size bounds. 249 void av1_rc_compute_frame_size_bounds(const struct AV1_COMP *cpi, 250 int this_frame_target, 251 int *frame_under_shoot_limit, 252 int *frame_over_shoot_limit); 253 254 // Picks q and q bounds given the target for bits 255 int av1_rc_pick_q_and_bounds(const struct AV1_COMP *cpi, RATE_CONTROL *rc, 256 int width, int height, int gf_index, 257 int *bottom_index, int *top_index); 258 259 // Estimates q to achieve a target bits per frame 260 int av1_rc_regulate_q(const struct AV1_COMP *cpi, int target_bits_per_frame, 261 int active_best_quality, int active_worst_quality, 262 int width, int height); 263 264 // Estimates bits per mb for a given qindex and correction factor. 265 int av1_rc_bits_per_mb(FRAME_TYPE frame_type, int qindex, 266 double correction_factor, aom_bit_depth_t bit_depth); 267 268 // Clamping utilities for bitrate targets for iframes and pframes. 269 int av1_rc_clamp_iframe_target_size(const struct AV1_COMP *const cpi, 270 int target); 271 int av1_rc_clamp_pframe_target_size(const struct AV1_COMP *const cpi, 272 int target, uint8_t frame_update_type); 273 274 // Find q_index corresponding to desired_q, within [best_qindex, worst_qindex]. 275 // To be precise, 'q_index' is the smallest integer, for which the corresponding 276 // q >= desired_q. 277 // If no such q index is found, returns 'worst_qindex'. 278 int av1_find_qindex(double desired_q, aom_bit_depth_t bit_depth, 279 int best_qindex, int worst_qindex); 280 281 // Computes a q delta (in "q index" terms) to get from a starting q value 282 // to a target q value 283 int av1_compute_qdelta(const RATE_CONTROL *rc, double qstart, double qtarget, 284 aom_bit_depth_t bit_depth); 285 286 // Computes a q delta (in "q index" terms) to get from a starting q value 287 // to a value that should equate to the given rate ratio. 288 int av1_compute_qdelta_by_rate(const RATE_CONTROL *rc, FRAME_TYPE frame_type, 289 int qindex, double rate_target_ratio, 290 aom_bit_depth_t bit_depth); 291 292 int av1_frame_type_qdelta(const struct AV1_COMP *cpi, int q); 293 294 void av1_rc_update_framerate(struct AV1_COMP *cpi, int width, int height); 295 296 void av1_rc_set_gf_interval_range(const struct AV1_COMP *const cpi, 297 RATE_CONTROL *const rc); 298 299 void av1_set_target_rate(struct AV1_COMP *cpi, int width, int height); 300 301 int av1_resize_one_pass_cbr(struct AV1_COMP *cpi); 302 303 void av1_rc_set_frame_target(struct AV1_COMP *cpi, int target, int width, 304 int height); 305 306 int av1_calc_pframe_target_size_one_pass_vbr( 307 const struct AV1_COMP *const cpi, FRAME_UPDATE_TYPE frame_update_type); 308 309 int av1_calc_iframe_target_size_one_pass_vbr(const struct AV1_COMP *const cpi); 310 311 int av1_calc_pframe_target_size_one_pass_cbr( 312 const struct AV1_COMP *cpi, FRAME_UPDATE_TYPE frame_update_type); 313 314 int av1_calc_iframe_target_size_one_pass_cbr(const struct AV1_COMP *cpi); 315 316 void av1_get_one_pass_rt_params(struct AV1_COMP *cpi, 317 struct EncodeFrameParams *const frame_params, 318 unsigned int frame_flags); 319 320 #ifdef __cplusplus 321 } // extern "C" 322 #endif 323 324 #endif // AOM_AV1_ENCODER_RATECTRL_H_ 325