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