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/blockd.h" 21 #include "av1/common/onyxc_int.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 // Threshold used to define if a KF group is static (e.g. a slide show). 31 // Essentially, this means that no frame in the group has more than 1% of MBs 32 // that are not marked as coded with 0,0 motion in the first pass. 33 #define STATIC_KF_GROUP_THRESH 99 34 #define STATIC_KF_GROUP_FLOAT_THRESH 0.99 35 36 // The maximum duration of a GF group that is static (e.g. a slide show). 37 #define MAX_STATIC_GF_GROUP_LENGTH 250 38 39 // Minimum and maximum height for the new pyramid structure. 40 // (Old structure supports height = 1, but does NOT support height = 4). 41 #define MIN_PYRAMID_LVL 0 42 #define MAX_PYRAMID_LVL 4 43 44 #define MIN_GF_INTERVAL 4 45 #define MAX_GF_INTERVAL 16 46 #define FIXED_GF_INTERVAL 8 // Used in some testing modes only 47 48 typedef struct { 49 int resize_width; 50 int resize_height; 51 uint8_t superres_denom; 52 } size_params_type; 53 54 enum { 55 INTER_NORMAL, 56 GF_ARF_LOW, 57 GF_ARF_STD, 58 KF_STD, 59 RATE_FACTOR_LEVELS 60 } UENUM1BYTE(RATE_FACTOR_LEVEL); 61 62 typedef struct { 63 // Rate targetting variables 64 int base_frame_target; // A baseline frame target before adjustment 65 // for previous under or over shoot. 66 int this_frame_target; // Actual frame target after rc adjustment. 67 int projected_frame_size; 68 int sb64_target_rate; 69 int last_q[FRAME_TYPES]; // Separate values for Intra/Inter 70 int last_boosted_qindex; // Last boosted GF/KF/ARF q 71 int last_kf_qindex; // Q index of the last key frame coded. 72 73 int gfu_boost; 74 int kf_boost; 75 76 double rate_correction_factors[RATE_FACTOR_LEVELS]; 77 78 int frames_since_golden; 79 int frames_till_gf_update_due; 80 int min_gf_interval; 81 int max_gf_interval; 82 int static_scene_max_gf_interval; 83 int baseline_gf_interval; 84 int constrained_gf_group; 85 int frames_to_key; 86 int frames_since_key; 87 int this_key_frame_forced; 88 int next_key_frame_forced; 89 int source_alt_ref_pending; 90 int source_alt_ref_active; 91 int is_src_frame_alt_ref; 92 int is_src_frame_internal_arf; 93 int sframe_due; 94 95 int avg_frame_bandwidth; // Average frame size target for clip 96 int min_frame_bandwidth; // Minimum allocation used for any frame 97 int max_frame_bandwidth; // Maximum burst rate allowed for a frame. 98 99 int ni_av_qi; 100 int ni_tot_qi; 101 int ni_frames; 102 int avg_frame_qindex[FRAME_TYPES]; 103 double tot_q; 104 double avg_q; 105 106 int64_t buffer_level; 107 int64_t bits_off_target; 108 int64_t vbr_bits_off_target; 109 int64_t vbr_bits_off_target_fast; 110 111 int decimation_factor; 112 int decimation_count; 113 114 int rolling_target_bits; 115 int rolling_actual_bits; 116 117 int long_rolling_target_bits; 118 int long_rolling_actual_bits; 119 120 int rate_error_estimate; 121 122 int64_t total_actual_bits; 123 int64_t total_target_bits; 124 int64_t total_target_vs_actual; 125 126 int worst_quality; 127 int best_quality; 128 129 int64_t starting_buffer_level; 130 int64_t optimal_buffer_level; 131 int64_t maximum_buffer_size; 132 133 // rate control history for last frame(1) and the frame before(2). 134 // -1: undershot 135 // 1: overshoot 136 // 0: not initialized. 137 int rc_1_frame; 138 int rc_2_frame; 139 int q_1_frame; 140 int q_2_frame; 141 142 float_t arf_boost_factor; 143 // Q index used for ALT frame 144 int arf_q; 145 } RATE_CONTROL; 146 147 struct AV1_COMP; 148 struct AV1EncoderConfig; 149 150 void av1_rc_init(const struct AV1EncoderConfig *oxcf, int pass, 151 RATE_CONTROL *rc); 152 153 int av1_estimate_bits_at_q(FRAME_TYPE frame_kind, int q, int mbs, 154 double correction_factor, aom_bit_depth_t bit_depth); 155 156 double av1_convert_qindex_to_q(int qindex, aom_bit_depth_t bit_depth); 157 158 void av1_rc_init_minq_luts(void); 159 160 int av1_rc_get_default_min_gf_interval(int width, int height, double framerate); 161 // Note av1_rc_get_default_max_gf_interval() requires the min_gf_interval to 162 // be passed in to ensure that the max_gf_interval returned is at least as bis 163 // as that. 164 int av1_rc_get_default_max_gf_interval(double framerate, int min_gf_interval); 165 166 // Generally at the high level, the following flow is expected 167 // to be enforced for rate control: 168 // First call per frame, one of: 169 // av1_rc_get_one_pass_vbr_params() 170 // av1_rc_get_one_pass_cbr_params() 171 // av1_rc_get_first_pass_params() 172 // av1_rc_get_second_pass_params() 173 // depending on the usage to set the rate control encode parameters desired. 174 // 175 // Then, call encode_frame_to_data_rate() to perform the 176 // actual encode. This function will in turn call encode_frame() 177 // one or more times, followed by one of: 178 // av1_rc_postencode_update() 179 // av1_rc_postencode_update_drop_frame() 180 // 181 // The majority of rate control parameters are only expected 182 // to be set in the av1_rc_get_..._params() functions and 183 // updated during the av1_rc_postencode_update...() functions. 184 // The only exceptions are av1_rc_drop_frame() and 185 // av1_rc_update_rate_correction_factors() functions. 186 187 // Functions to set parameters for encoding before the actual 188 // encode_frame_to_data_rate() function. 189 struct EncodeFrameParams; 190 void av1_rc_get_one_pass_vbr_params( 191 struct AV1_COMP *cpi, uint8_t *const frame_update_type, 192 struct EncodeFrameParams *const frame_params, unsigned int frame_flags); 193 void av1_rc_get_one_pass_cbr_params( 194 struct AV1_COMP *cpi, uint8_t *const frame_update_type, 195 struct EncodeFrameParams *const frame_params, unsigned int frame_flags); 196 197 // Post encode update of the rate control parameters based 198 // on bytes used 199 void av1_rc_postencode_update(struct AV1_COMP *cpi, uint64_t bytes_used); 200 // Post encode update of the rate control parameters for dropped frames 201 void av1_rc_postencode_update_drop_frame(struct AV1_COMP *cpi); 202 203 // Updates rate correction factors 204 // Changes only the rate correction factors in the rate control structure. 205 void av1_rc_update_rate_correction_factors(struct AV1_COMP *cpi, int width, 206 int height); 207 208 // Decide if we should drop this frame: For 1-pass CBR. 209 // Changes only the decimation count in the rate control structure 210 int av1_rc_drop_frame(struct AV1_COMP *cpi); 211 212 // Computes frame size bounds. 213 void av1_rc_compute_frame_size_bounds(const struct AV1_COMP *cpi, 214 int this_frame_target, 215 int *frame_under_shoot_limit, 216 int *frame_over_shoot_limit); 217 218 // Picks q and q bounds given the target for bits 219 int av1_rc_pick_q_and_bounds(struct AV1_COMP *cpi, int width, int height, 220 int *bottom_index, int *top_index); 221 222 // Estimates q to achieve a target bits per frame 223 int av1_rc_regulate_q(const struct AV1_COMP *cpi, int target_bits_per_frame, 224 int active_best_quality, int active_worst_quality, 225 int width, int height); 226 227 // Estimates bits per mb for a given qindex and correction factor. 228 int av1_rc_bits_per_mb(FRAME_TYPE frame_type, int qindex, 229 double correction_factor, aom_bit_depth_t bit_depth); 230 231 // Clamping utilities for bitrate targets for iframes and pframes. 232 int av1_rc_clamp_iframe_target_size(const struct AV1_COMP *const cpi, 233 int target); 234 int av1_rc_clamp_pframe_target_size(const struct AV1_COMP *const cpi, 235 int target, uint8_t frame_update_type); 236 237 // Find q_index corresponding to desired_q, within [best_qindex, worst_qindex]. 238 // To be precise, 'q_index' is the smallest integer, for which the corresponding 239 // q >= desired_q. 240 // If no such q index is found, returns 'worst_qindex'. 241 int av1_find_qindex(double desired_q, aom_bit_depth_t bit_depth, 242 int best_qindex, int worst_qindex); 243 244 // Computes a q delta (in "q index" terms) to get from a starting q value 245 // to a target q value 246 int av1_compute_qdelta(const RATE_CONTROL *rc, double qstart, double qtarget, 247 aom_bit_depth_t bit_depth); 248 249 // Computes a q delta (in "q index" terms) to get from a starting q value 250 // to a value that should equate to the given rate ratio. 251 int av1_compute_qdelta_by_rate(const RATE_CONTROL *rc, FRAME_TYPE frame_type, 252 int qindex, double rate_target_ratio, 253 aom_bit_depth_t bit_depth); 254 255 int av1_frame_type_qdelta(const struct AV1_COMP *cpi, int q); 256 257 void av1_rc_update_framerate(struct AV1_COMP *cpi, int width, int height); 258 259 void av1_rc_set_gf_interval_range(const struct AV1_COMP *const cpi, 260 RATE_CONTROL *const rc); 261 262 void av1_set_target_rate(struct AV1_COMP *cpi, int width, int height); 263 264 int av1_resize_one_pass_cbr(struct AV1_COMP *cpi); 265 266 #ifdef __cplusplus 267 } // extern "C" 268 #endif 269 270 #endif // AOM_AV1_ENCODER_RATECTRL_H_ 271