1 /* 2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 12 #ifndef VP9_ENCODER_VP9_RATECTRL_H_ 13 #define VP9_ENCODER_VP9_RATECTRL_H_ 14 15 #include "vpx/vpx_codec.h" 16 #include "vpx/vpx_integer.h" 17 18 #include "vp9/common/vp9_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 MIN_GF_INTERVAL 4 28 #define MAX_GF_INTERVAL 16 29 30 typedef enum { 31 INTER_NORMAL = 0, 32 INTER_HIGH = 1, 33 GF_ARF_LOW = 2, 34 GF_ARF_STD = 3, 35 KF_STD = 4, 36 RATE_FACTOR_LEVELS = 5 37 } RATE_FACTOR_LEVEL; 38 39 // Internal frame scaling level. 40 typedef enum { 41 UNSCALED = 0, // Frame is unscaled. 42 SCALE_STEP1 = 1, // First-level down-scaling. 43 FRAME_SCALE_STEPS 44 } FRAME_SCALE_LEVEL; 45 46 // Frame dimensions multiplier wrt the native frame size, in 1/16ths, 47 // specified for the scale-up case. 48 // e.g. 24 => 16/24 = 2/3 of native size. The restriction to 1/16th is 49 // intended to match the capabilities of the normative scaling filters, 50 // giving precedence to the up-scaling accuracy. 51 static const int frame_scale_factor[FRAME_SCALE_STEPS] = {16, 24}; 52 53 // Multiplier of the target rate to be used as threshold for triggering scaling. 54 static const double rate_thresh_mult[FRAME_SCALE_STEPS] = {1.0, 2.0}; 55 56 // Scale dependent Rate Correction Factor multipliers. Compensates for the 57 // greater number of bits per pixel generated in down-scaled frames. 58 static const double rcf_mult[FRAME_SCALE_STEPS] = {1.0, 2.0}; 59 60 typedef struct { 61 // Rate targetting variables 62 int base_frame_target; // A baseline frame target before adjustment 63 // for previous under or over shoot. 64 int this_frame_target; // Actual frame target after rc adjustment. 65 int projected_frame_size; 66 int sb64_target_rate; 67 int last_q[FRAME_TYPES]; // Separate values for Intra/Inter 68 int last_boosted_qindex; // Last boosted GF/KF/ARF q 69 int last_kf_qindex; // Q index of the last key frame coded. 70 71 int gfu_boost; 72 int last_boost; 73 int kf_boost; 74 75 double rate_correction_factors[RATE_FACTOR_LEVELS]; 76 77 int frames_since_golden; 78 int frames_till_gf_update_due; 79 int min_gf_interval; 80 int max_gf_interval; 81 int static_scene_max_gf_interval; 82 int baseline_gf_interval; 83 int constrained_gf_group; 84 int frames_to_key; 85 int frames_since_key; 86 int this_key_frame_forced; 87 int next_key_frame_forced; 88 int source_alt_ref_pending; 89 int source_alt_ref_active; 90 int is_src_frame_alt_ref; 91 92 int avg_frame_bandwidth; // Average frame size target for clip 93 int min_frame_bandwidth; // Minimum allocation used for any frame 94 int max_frame_bandwidth; // Maximum burst rate allowed for a frame. 95 96 int ni_av_qi; 97 int ni_tot_qi; 98 int ni_frames; 99 int avg_frame_qindex[FRAME_TYPES]; 100 double tot_q; 101 double avg_q; 102 103 int64_t buffer_level; 104 int64_t bits_off_target; 105 int64_t vbr_bits_off_target; 106 int64_t vbr_bits_off_target_fast; 107 108 int decimation_factor; 109 int decimation_count; 110 111 int rolling_target_bits; 112 int rolling_actual_bits; 113 114 int long_rolling_target_bits; 115 int long_rolling_actual_bits; 116 117 int rate_error_estimate; 118 119 int64_t total_actual_bits; 120 int64_t total_target_bits; 121 int64_t total_target_vs_actual; 122 123 int worst_quality; 124 int best_quality; 125 126 int64_t starting_buffer_level; 127 int64_t optimal_buffer_level; 128 int64_t maximum_buffer_size; 129 130 // rate control history for last frame(1) and the frame before(2). 131 // -1: undershot 132 // 1: overshoot 133 // 0: not initialized. 134 int rc_1_frame; 135 int rc_2_frame; 136 int q_1_frame; 137 int q_2_frame; 138 139 // Auto frame-scaling variables. 140 FRAME_SCALE_LEVEL frame_size_selector; 141 FRAME_SCALE_LEVEL next_frame_size_selector; 142 int frame_width[FRAME_SCALE_STEPS]; 143 int frame_height[FRAME_SCALE_STEPS]; 144 int rf_level_maxq[RATE_FACTOR_LEVELS]; 145 146 uint64_t avg_source_sad; 147 int high_source_sad; 148 } RATE_CONTROL; 149 150 struct VP9_COMP; 151 struct VP9EncoderConfig; 152 153 void vp9_rc_init(const struct VP9EncoderConfig *oxcf, int pass, 154 RATE_CONTROL *rc); 155 156 int vp9_estimate_bits_at_q(FRAME_TYPE frame_kind, int q, int mbs, 157 double correction_factor, 158 vpx_bit_depth_t bit_depth); 159 160 double vp9_convert_qindex_to_q(int qindex, vpx_bit_depth_t bit_depth); 161 162 void vp9_rc_init_minq_luts(void); 163 164 int vp9_rc_get_default_min_gf_interval(int width, int height, double framerate); 165 // Note vp9_rc_get_default_max_gf_interval() requires the min_gf_interval to 166 // be passed in to ensure that the max_gf_interval returned is at least as bis 167 // as that. 168 int vp9_rc_get_default_max_gf_interval(double framerate, int min_frame_rate); 169 170 // Generally at the high level, the following flow is expected 171 // to be enforced for rate control: 172 // First call per frame, one of: 173 // vp9_rc_get_one_pass_vbr_params() 174 // vp9_rc_get_one_pass_cbr_params() 175 // vp9_rc_get_svc_params() 176 // vp9_rc_get_first_pass_params() 177 // vp9_rc_get_second_pass_params() 178 // depending on the usage to set the rate control encode parameters desired. 179 // 180 // Then, call encode_frame_to_data_rate() to perform the 181 // actual encode. This function will in turn call encode_frame() 182 // one or more times, followed by one of: 183 // vp9_rc_postencode_update() 184 // vp9_rc_postencode_update_drop_frame() 185 // 186 // The majority of rate control parameters are only expected 187 // to be set in the vp9_rc_get_..._params() functions and 188 // updated during the vp9_rc_postencode_update...() functions. 189 // The only exceptions are vp9_rc_drop_frame() and 190 // vp9_rc_update_rate_correction_factors() functions. 191 192 // Functions to set parameters for encoding before the actual 193 // encode_frame_to_data_rate() function. 194 void vp9_rc_get_one_pass_vbr_params(struct VP9_COMP *cpi); 195 void vp9_rc_get_one_pass_cbr_params(struct VP9_COMP *cpi); 196 void vp9_rc_get_svc_params(struct VP9_COMP *cpi); 197 198 // Post encode update of the rate control parameters based 199 // on bytes used 200 void vp9_rc_postencode_update(struct VP9_COMP *cpi, uint64_t bytes_used); 201 // Post encode update of the rate control parameters for dropped frames 202 void vp9_rc_postencode_update_drop_frame(struct VP9_COMP *cpi); 203 204 // Updates rate correction factors 205 // Changes only the rate correction factors in the rate control structure. 206 void vp9_rc_update_rate_correction_factors(struct VP9_COMP *cpi); 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 vp9_rc_drop_frame(struct VP9_COMP *cpi); 211 212 // Computes frame size bounds. 213 void vp9_rc_compute_frame_size_bounds(const struct VP9_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 vp9_rc_pick_q_and_bounds(const struct VP9_COMP *cpi, 220 int *bottom_index, 221 int *top_index); 222 223 // Estimates q to achieve a target bits per frame 224 int vp9_rc_regulate_q(const struct VP9_COMP *cpi, int target_bits_per_frame, 225 int active_best_quality, int active_worst_quality); 226 227 // Estimates bits per mb for a given qindex and correction factor. 228 int vp9_rc_bits_per_mb(FRAME_TYPE frame_type, int qindex, 229 double correction_factor, vpx_bit_depth_t bit_depth); 230 231 // Clamping utilities for bitrate targets for iframes and pframes. 232 int vp9_rc_clamp_iframe_target_size(const struct VP9_COMP *const cpi, 233 int target); 234 int vp9_rc_clamp_pframe_target_size(const struct VP9_COMP *const cpi, 235 int target); 236 // Utility to set frame_target into the RATE_CONTROL structure 237 // This function is called only from the vp9_rc_get_..._params() functions. 238 void vp9_rc_set_frame_target(struct VP9_COMP *cpi, int target); 239 240 // Computes a q delta (in "q index" terms) to get from a starting q value 241 // to a target q value 242 int vp9_compute_qdelta(const RATE_CONTROL *rc, double qstart, double qtarget, 243 vpx_bit_depth_t bit_depth); 244 245 // Computes a q delta (in "q index" terms) to get from a starting q value 246 // to a value that should equate to the given rate ratio. 247 int vp9_compute_qdelta_by_rate(const RATE_CONTROL *rc, FRAME_TYPE frame_type, 248 int qindex, double rate_target_ratio, 249 vpx_bit_depth_t bit_depth); 250 251 int vp9_frame_type_qdelta(const struct VP9_COMP *cpi, int rf_level, int q); 252 253 void vp9_rc_update_framerate(struct VP9_COMP *cpi); 254 255 void vp9_rc_set_gf_interval_range(const struct VP9_COMP *const cpi, 256 RATE_CONTROL *const rc); 257 258 void vp9_set_target_rate(struct VP9_COMP *cpi); 259 260 int vp9_resize_one_pass_cbr(struct VP9_COMP *cpi); 261 262 void vp9_avg_source_sad(struct VP9_COMP *cpi); 263 264 int vp9_encodedframe_overshoot(struct VP9_COMP *cpi, int frame_size, int *q); 265 266 #ifdef __cplusplus 267 } // extern "C" 268 #endif 269 270 #endif // VP9_ENCODER_VP9_RATECTRL_H_ 271