Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame^] | 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 | #ifndef VP10_ENCODER_RATECTRL_H_ |
| 12 | #define VP10_ENCODER_RATECTRL_H_ |
| 13 | |
| 14 | #include "aom/vpx_codec.h" |
| 15 | #include "aom/vpx_integer.h" |
| 16 | |
| 17 | #include "av1/common/blockd.h" |
| 18 | |
| 19 | #ifdef __cplusplus |
| 20 | extern "C" { |
| 21 | #endif |
| 22 | |
| 23 | // Bits Per MB at different Q (Multiplied by 512) |
| 24 | #define BPER_MB_NORMBITS 9 |
| 25 | |
| 26 | #define MIN_GF_INTERVAL 4 |
| 27 | #define MAX_GF_INTERVAL 16 |
| 28 | #define FIXED_GF_INTERVAL 8 // Used in some testing modes only |
| 29 | |
| 30 | #if CONFIG_EXT_REFS |
| 31 | typedef enum { |
| 32 | INTER_NORMAL = 0, |
| 33 | INTER_LOW = 1, |
| 34 | INTER_HIGH = 2, |
| 35 | GF_ARF_LOW = 3, |
| 36 | GF_ARF_STD = 4, |
| 37 | KF_STD = 5, |
| 38 | RATE_FACTOR_LEVELS = 6 |
| 39 | } RATE_FACTOR_LEVEL; |
| 40 | #else |
| 41 | typedef enum { |
| 42 | INTER_NORMAL = 0, |
| 43 | INTER_HIGH = 1, |
| 44 | GF_ARF_LOW = 2, |
| 45 | GF_ARF_STD = 3, |
| 46 | KF_STD = 4, |
| 47 | RATE_FACTOR_LEVELS = 5 |
| 48 | } RATE_FACTOR_LEVEL; |
| 49 | #endif // CONFIG_EXT_REFS |
| 50 | |
| 51 | // Internal frame scaling level. |
| 52 | typedef enum { |
| 53 | UNSCALED = 0, // Frame is unscaled. |
| 54 | SCALE_STEP1 = 1, // First-level down-scaling. |
| 55 | FRAME_SCALE_STEPS |
| 56 | } FRAME_SCALE_LEVEL; |
| 57 | |
| 58 | // Frame dimensions multiplier wrt the native frame size, in 1/16ths, |
| 59 | // specified for the scale-up case. |
| 60 | // e.g. 24 => 16/24 = 2/3 of native size. The restriction to 1/16th is |
| 61 | // intended to match the capabilities of the normative scaling filters, |
| 62 | // giving precedence to the up-scaling accuracy. |
| 63 | static const int frame_scale_factor[FRAME_SCALE_STEPS] = { 16, 24 }; |
| 64 | |
| 65 | // Multiplier of the target rate to be used as threshold for triggering scaling. |
| 66 | static const double rate_thresh_mult[FRAME_SCALE_STEPS] = { 1.0, 2.0 }; |
| 67 | |
| 68 | // Scale dependent Rate Correction Factor multipliers. Compensates for the |
| 69 | // greater number of bits per pixel generated in down-scaled frames. |
| 70 | static const double rcf_mult[FRAME_SCALE_STEPS] = { 1.0, 2.0 }; |
| 71 | |
| 72 | typedef struct { |
| 73 | // Rate targetting variables |
| 74 | int base_frame_target; // A baseline frame target before adjustment |
| 75 | // for previous under or over shoot. |
| 76 | int this_frame_target; // Actual frame target after rc adjustment. |
| 77 | int projected_frame_size; |
| 78 | int sb64_target_rate; |
| 79 | int last_q[FRAME_TYPES]; // Separate values for Intra/Inter |
| 80 | int last_boosted_qindex; // Last boosted GF/KF/ARF q |
| 81 | int last_kf_qindex; // Q index of the last key frame coded. |
| 82 | |
| 83 | int gfu_boost; |
| 84 | int last_boost; |
| 85 | int kf_boost; |
| 86 | |
| 87 | double rate_correction_factors[RATE_FACTOR_LEVELS]; |
| 88 | |
| 89 | int frames_since_golden; |
| 90 | int frames_till_gf_update_due; |
| 91 | int min_gf_interval; |
| 92 | int max_gf_interval; |
| 93 | int static_scene_max_gf_interval; |
| 94 | int baseline_gf_interval; |
| 95 | int constrained_gf_group; |
| 96 | int frames_to_key; |
| 97 | int frames_since_key; |
| 98 | int this_key_frame_forced; |
| 99 | int next_key_frame_forced; |
| 100 | int source_alt_ref_pending; |
| 101 | int source_alt_ref_active; |
| 102 | int is_src_frame_alt_ref; |
| 103 | |
| 104 | #if CONFIG_EXT_REFS |
| 105 | // Length of the bi-predictive frame group interval |
| 106 | int bipred_group_interval; |
| 107 | |
| 108 | // NOTE: Different types of frames may have different bits allocated |
| 109 | // accordingly, aiming to achieve the overall optimal RD performance. |
| 110 | int is_bwd_ref_frame; |
| 111 | int is_last_bipred_frame; |
| 112 | int is_bipred_frame; |
| 113 | int is_src_frame_ext_arf; |
| 114 | #endif // CONFIG_EXT_REFS |
| 115 | |
| 116 | int avg_frame_bandwidth; // Average frame size target for clip |
| 117 | int min_frame_bandwidth; // Minimum allocation used for any frame |
| 118 | int max_frame_bandwidth; // Maximum burst rate allowed for a frame. |
| 119 | |
| 120 | int ni_av_qi; |
| 121 | int ni_tot_qi; |
| 122 | int ni_frames; |
| 123 | int avg_frame_qindex[FRAME_TYPES]; |
| 124 | double tot_q; |
| 125 | double avg_q; |
| 126 | |
| 127 | int64_t buffer_level; |
| 128 | int64_t bits_off_target; |
| 129 | int64_t vbr_bits_off_target; |
| 130 | int64_t vbr_bits_off_target_fast; |
| 131 | |
| 132 | int decimation_factor; |
| 133 | int decimation_count; |
| 134 | |
| 135 | int rolling_target_bits; |
| 136 | int rolling_actual_bits; |
| 137 | |
| 138 | int long_rolling_target_bits; |
| 139 | int long_rolling_actual_bits; |
| 140 | |
| 141 | int rate_error_estimate; |
| 142 | |
| 143 | int64_t total_actual_bits; |
| 144 | int64_t total_target_bits; |
| 145 | int64_t total_target_vs_actual; |
| 146 | |
| 147 | int worst_quality; |
| 148 | int best_quality; |
| 149 | |
| 150 | int64_t starting_buffer_level; |
| 151 | int64_t optimal_buffer_level; |
| 152 | int64_t maximum_buffer_size; |
| 153 | |
| 154 | // rate control history for last frame(1) and the frame before(2). |
| 155 | // -1: undershot |
| 156 | // 1: overshoot |
| 157 | // 0: not initialized. |
| 158 | int rc_1_frame; |
| 159 | int rc_2_frame; |
| 160 | int q_1_frame; |
| 161 | int q_2_frame; |
| 162 | |
| 163 | // Auto frame-scaling variables. |
| 164 | FRAME_SCALE_LEVEL frame_size_selector; |
| 165 | FRAME_SCALE_LEVEL next_frame_size_selector; |
| 166 | int frame_width[FRAME_SCALE_STEPS]; |
| 167 | int frame_height[FRAME_SCALE_STEPS]; |
| 168 | int rf_level_maxq[RATE_FACTOR_LEVELS]; |
| 169 | } RATE_CONTROL; |
| 170 | |
| 171 | struct VP10_COMP; |
| 172 | struct VP10EncoderConfig; |
| 173 | |
| 174 | void vp10_rc_init(const struct VP10EncoderConfig *oxcf, int pass, |
| 175 | RATE_CONTROL *rc); |
| 176 | |
| 177 | int vp10_estimate_bits_at_q(FRAME_TYPE frame_kind, int q, int mbs, |
| 178 | double correction_factor, |
| 179 | vpx_bit_depth_t bit_depth); |
| 180 | |
| 181 | double vp10_convert_qindex_to_q(int qindex, vpx_bit_depth_t bit_depth); |
| 182 | |
| 183 | void vp10_rc_init_minq_luts(void); |
| 184 | |
| 185 | int vp10_rc_get_default_min_gf_interval(int width, int height, |
| 186 | double framerate); |
| 187 | // Note vp10_rc_get_default_max_gf_interval() requires the min_gf_interval to |
| 188 | // be passed in to ensure that the max_gf_interval returned is at least as bis |
| 189 | // as that. |
| 190 | int vp10_rc_get_default_max_gf_interval(double framerate, int min_frame_rate); |
| 191 | |
| 192 | // Generally at the high level, the following flow is expected |
| 193 | // to be enforced for rate control: |
| 194 | // First call per frame, one of: |
| 195 | // vp10_rc_get_one_pass_vbr_params() |
| 196 | // vp10_rc_get_one_pass_cbr_params() |
| 197 | // vp10_rc_get_first_pass_params() |
| 198 | // vp10_rc_get_second_pass_params() |
| 199 | // depending on the usage to set the rate control encode parameters desired. |
| 200 | // |
| 201 | // Then, call encode_frame_to_data_rate() to perform the |
| 202 | // actual encode. This function will in turn call encode_frame() |
| 203 | // one or more times, followed by one of: |
| 204 | // vp10_rc_postencode_update() |
| 205 | // vp10_rc_postencode_update_drop_frame() |
| 206 | // |
| 207 | // The majority of rate control parameters are only expected |
| 208 | // to be set in the vp10_rc_get_..._params() functions and |
| 209 | // updated during the vp10_rc_postencode_update...() functions. |
| 210 | // The only exceptions are vp10_rc_drop_frame() and |
| 211 | // vp10_rc_update_rate_correction_factors() functions. |
| 212 | |
| 213 | // Functions to set parameters for encoding before the actual |
| 214 | // encode_frame_to_data_rate() function. |
| 215 | void vp10_rc_get_one_pass_vbr_params(struct VP10_COMP *cpi); |
| 216 | void vp10_rc_get_one_pass_cbr_params(struct VP10_COMP *cpi); |
| 217 | |
| 218 | // Post encode update of the rate control parameters based |
| 219 | // on bytes used |
| 220 | void vp10_rc_postencode_update(struct VP10_COMP *cpi, uint64_t bytes_used); |
| 221 | // Post encode update of the rate control parameters for dropped frames |
| 222 | void vp10_rc_postencode_update_drop_frame(struct VP10_COMP *cpi); |
| 223 | |
| 224 | // Updates rate correction factors |
| 225 | // Changes only the rate correction factors in the rate control structure. |
| 226 | void vp10_rc_update_rate_correction_factors(struct VP10_COMP *cpi); |
| 227 | |
| 228 | // Decide if we should drop this frame: For 1-pass CBR. |
| 229 | // Changes only the decimation count in the rate control structure |
| 230 | int vp10_rc_drop_frame(struct VP10_COMP *cpi); |
| 231 | |
| 232 | // Computes frame size bounds. |
| 233 | void vp10_rc_compute_frame_size_bounds(const struct VP10_COMP *cpi, |
| 234 | int this_frame_target, |
| 235 | int *frame_under_shoot_limit, |
| 236 | int *frame_over_shoot_limit); |
| 237 | |
| 238 | // Picks q and q bounds given the target for bits |
| 239 | int vp10_rc_pick_q_and_bounds(const struct VP10_COMP *cpi, int *bottom_index, |
| 240 | int *top_index); |
| 241 | |
| 242 | // Estimates q to achieve a target bits per frame |
| 243 | int vp10_rc_regulate_q(const struct VP10_COMP *cpi, int target_bits_per_frame, |
| 244 | int active_best_quality, int active_worst_quality); |
| 245 | |
| 246 | // Estimates bits per mb for a given qindex and correction factor. |
| 247 | int vp10_rc_bits_per_mb(FRAME_TYPE frame_type, int qindex, |
| 248 | double correction_factor, vpx_bit_depth_t bit_depth); |
| 249 | |
| 250 | // Clamping utilities for bitrate targets for iframes and pframes. |
| 251 | int vp10_rc_clamp_iframe_target_size(const struct VP10_COMP *const cpi, |
| 252 | int target); |
| 253 | int vp10_rc_clamp_pframe_target_size(const struct VP10_COMP *const cpi, |
| 254 | int target); |
| 255 | // Utility to set frame_target into the RATE_CONTROL structure |
| 256 | // This function is called only from the vp10_rc_get_..._params() functions. |
| 257 | void vp10_rc_set_frame_target(struct VP10_COMP *cpi, int target); |
| 258 | |
| 259 | // Computes a q delta (in "q index" terms) to get from a starting q value |
| 260 | // to a target q value |
| 261 | int vp10_compute_qdelta(const RATE_CONTROL *rc, double qstart, double qtarget, |
| 262 | vpx_bit_depth_t bit_depth); |
| 263 | |
| 264 | // Computes a q delta (in "q index" terms) to get from a starting q value |
| 265 | // to a value that should equate to the given rate ratio. |
| 266 | int vp10_compute_qdelta_by_rate(const RATE_CONTROL *rc, FRAME_TYPE frame_type, |
| 267 | int qindex, double rate_target_ratio, |
| 268 | vpx_bit_depth_t bit_depth); |
| 269 | |
| 270 | int vp10_frame_type_qdelta(const struct VP10_COMP *cpi, int rf_level, int q); |
| 271 | |
| 272 | void vp10_rc_update_framerate(struct VP10_COMP *cpi); |
| 273 | |
| 274 | void vp10_rc_set_gf_interval_range(const struct VP10_COMP *const cpi, |
| 275 | RATE_CONTROL *const rc); |
| 276 | |
| 277 | void vp10_set_target_rate(struct VP10_COMP *cpi); |
| 278 | |
| 279 | int vp10_resize_one_pass_cbr(struct VP10_COMP *cpi); |
| 280 | |
| 281 | #ifdef __cplusplus |
| 282 | } // extern "C" |
| 283 | #endif |
| 284 | |
| 285 | #endif // VP10_ENCODER_RATECTRL_H_ |