vp9_ratectrl.h 9.9 KB

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