vp9_ratectrl.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. // Bits Per MB at different Q (Multiplied by 512)
  19. #define BPER_MB_NORMBITS 9
  20. typedef enum {
  21. INTER_NORMAL = 0,
  22. INTER_HIGH = 1,
  23. GF_ARF_LOW = 2,
  24. GF_ARF_STD = 3,
  25. KF_STD = 4,
  26. RATE_FACTOR_LEVELS = 5
  27. } RATE_FACTOR_LEVEL;
  28. // Internal frame scaling level.
  29. typedef enum {
  30. UNSCALED = 0, // Frame is unscaled.
  31. SCALE_STEP1 = 1, // First-level down-scaling.
  32. FRAME_SCALE_STEPS
  33. } FRAME_SCALE_LEVEL;
  34. // Frame dimensions multiplier wrt the native frame size, in 1/16ths,
  35. // specified for the scale-up case.
  36. // e.g. 24 => 16/24 = 2/3 of native size. The restriction to 1/16th is
  37. // intended to match the capabilities of the normative scaling filters,
  38. // giving precedence to the up-scaling accuracy.
  39. static const int frame_scale_factor[FRAME_SCALE_STEPS] = {16, 24};
  40. // Multiplier of the target rate to be used as threshold for triggering scaling.
  41. static const double rate_thresh_mult[FRAME_SCALE_STEPS] = {1.0, 2.0};
  42. // Scale dependent Rate Correction Factor multipliers. Compensates for the
  43. // greater number of bits per pixel generated in down-scaled frames.
  44. static const double rcf_mult[FRAME_SCALE_STEPS] = {1.0, 2.0};
  45. typedef struct {
  46. // Rate targetting variables
  47. int base_frame_target; // A baseline frame target before adjustment
  48. // for previous under or over shoot.
  49. int this_frame_target; // Actual frame target after rc adjustment.
  50. int projected_frame_size;
  51. int sb64_target_rate;
  52. int last_q[FRAME_TYPES]; // Separate values for Intra/Inter
  53. int last_boosted_qindex; // Last boosted GF/KF/ARF q
  54. int last_kf_qindex; // Q index of the last key frame coded.
  55. int gfu_boost;
  56. int last_boost;
  57. int kf_boost;
  58. double rate_correction_factors[RATE_FACTOR_LEVELS];
  59. int frames_since_golden;
  60. int frames_till_gf_update_due;
  61. int min_gf_interval;
  62. int max_gf_interval;
  63. int static_scene_max_gf_interval;
  64. int baseline_gf_interval;
  65. int constrained_gf_group;
  66. int frames_to_key;
  67. int frames_since_key;
  68. int this_key_frame_forced;
  69. int next_key_frame_forced;
  70. int source_alt_ref_pending;
  71. int source_alt_ref_active;
  72. int is_src_frame_alt_ref;
  73. int avg_frame_bandwidth; // Average frame size target for clip
  74. int min_frame_bandwidth; // Minimum allocation used for any frame
  75. int max_frame_bandwidth; // Maximum burst rate allowed for a frame.
  76. int ni_av_qi;
  77. int ni_tot_qi;
  78. int ni_frames;
  79. int avg_frame_qindex[FRAME_TYPES];
  80. double tot_q;
  81. double avg_q;
  82. int64_t buffer_level;
  83. int64_t bits_off_target;
  84. int64_t vbr_bits_off_target;
  85. int64_t vbr_bits_off_target_fast;
  86. int decimation_factor;
  87. int decimation_count;
  88. int rolling_target_bits;
  89. int rolling_actual_bits;
  90. int long_rolling_target_bits;
  91. int long_rolling_actual_bits;
  92. int rate_error_estimate;
  93. int64_t total_actual_bits;
  94. int64_t total_target_bits;
  95. int64_t total_target_vs_actual;
  96. int worst_quality;
  97. int best_quality;
  98. int64_t starting_buffer_level;
  99. int64_t optimal_buffer_level;
  100. int64_t maximum_buffer_size;
  101. // rate control history for last frame(1) and the frame before(2).
  102. // -1: undershot
  103. // 1: overshoot
  104. // 0: not initialized.
  105. int rc_1_frame;
  106. int rc_2_frame;
  107. int q_1_frame;
  108. int q_2_frame;
  109. // Auto frame-scaling variables.
  110. FRAME_SCALE_LEVEL frame_size_selector;
  111. FRAME_SCALE_LEVEL next_frame_size_selector;
  112. int frame_width[FRAME_SCALE_STEPS];
  113. int frame_height[FRAME_SCALE_STEPS];
  114. int rf_level_maxq[RATE_FACTOR_LEVELS];
  115. } RATE_CONTROL;
  116. struct VP9_COMP;
  117. struct VP9EncoderConfig;
  118. void vp9_rc_init(const struct VP9EncoderConfig *oxcf, int pass,
  119. RATE_CONTROL *rc);
  120. int vp9_estimate_bits_at_q(FRAME_TYPE frame_kind, int q, int mbs,
  121. double correction_factor,
  122. vpx_bit_depth_t bit_depth);
  123. double vp9_convert_qindex_to_q(int qindex, vpx_bit_depth_t bit_depth);
  124. void vp9_rc_init_minq_luts(void);
  125. // Generally at the high level, the following flow is expected
  126. // to be enforced for rate control:
  127. // First call per frame, one of:
  128. // vp9_rc_get_one_pass_vbr_params()
  129. // vp9_rc_get_one_pass_cbr_params()
  130. // vp9_rc_get_svc_params()
  131. // vp9_rc_get_first_pass_params()
  132. // vp9_rc_get_second_pass_params()
  133. // depending on the usage to set the rate control encode parameters desired.
  134. //
  135. // Then, call encode_frame_to_data_rate() to perform the
  136. // actual encode. This function will in turn call encode_frame()
  137. // one or more times, followed by one of:
  138. // vp9_rc_postencode_update()
  139. // vp9_rc_postencode_update_drop_frame()
  140. //
  141. // The majority of rate control parameters are only expected
  142. // to be set in the vp9_rc_get_..._params() functions and
  143. // updated during the vp9_rc_postencode_update...() functions.
  144. // The only exceptions are vp9_rc_drop_frame() and
  145. // vp9_rc_update_rate_correction_factors() functions.
  146. // Functions to set parameters for encoding before the actual
  147. // encode_frame_to_data_rate() function.
  148. void vp9_rc_get_one_pass_vbr_params(struct VP9_COMP *cpi);
  149. void vp9_rc_get_one_pass_cbr_params(struct VP9_COMP *cpi);
  150. void vp9_rc_get_svc_params(struct VP9_COMP *cpi);
  151. // Post encode update of the rate control parameters based
  152. // on bytes used
  153. void vp9_rc_postencode_update(struct VP9_COMP *cpi, uint64_t bytes_used);
  154. // Post encode update of the rate control parameters for dropped frames
  155. void vp9_rc_postencode_update_drop_frame(struct VP9_COMP *cpi);
  156. // Updates rate correction factors
  157. // Changes only the rate correction factors in the rate control structure.
  158. void vp9_rc_update_rate_correction_factors(struct VP9_COMP *cpi);
  159. // Decide if we should drop this frame: For 1-pass CBR.
  160. // Changes only the decimation count in the rate control structure
  161. int vp9_rc_drop_frame(struct VP9_COMP *cpi);
  162. // Computes frame size bounds.
  163. void vp9_rc_compute_frame_size_bounds(const struct VP9_COMP *cpi,
  164. int this_frame_target,
  165. int *frame_under_shoot_limit,
  166. int *frame_over_shoot_limit);
  167. // Picks q and q bounds given the target for bits
  168. int vp9_rc_pick_q_and_bounds(const struct VP9_COMP *cpi,
  169. int *bottom_index,
  170. int *top_index);
  171. // Estimates q to achieve a target bits per frame
  172. int vp9_rc_regulate_q(const struct VP9_COMP *cpi, int target_bits_per_frame,
  173. int active_best_quality, int active_worst_quality);
  174. // Estimates bits per mb for a given qindex and correction factor.
  175. int vp9_rc_bits_per_mb(FRAME_TYPE frame_type, int qindex,
  176. double correction_factor, vpx_bit_depth_t bit_depth);
  177. // Clamping utilities for bitrate targets for iframes and pframes.
  178. int vp9_rc_clamp_iframe_target_size(const struct VP9_COMP *const cpi,
  179. int target);
  180. int vp9_rc_clamp_pframe_target_size(const struct VP9_COMP *const cpi,
  181. int target);
  182. // Utility to set frame_target into the RATE_CONTROL structure
  183. // This function is called only from the vp9_rc_get_..._params() functions.
  184. void vp9_rc_set_frame_target(struct VP9_COMP *cpi, int target);
  185. // Computes a q delta (in "q index" terms) to get from a starting q value
  186. // to a target q value
  187. int vp9_compute_qdelta(const RATE_CONTROL *rc, double qstart, double qtarget,
  188. vpx_bit_depth_t bit_depth);
  189. // Computes a q delta (in "q index" terms) to get from a starting q value
  190. // to a value that should equate to the given rate ratio.
  191. int vp9_compute_qdelta_by_rate(const RATE_CONTROL *rc, FRAME_TYPE frame_type,
  192. int qindex, double rate_target_ratio,
  193. vpx_bit_depth_t bit_depth);
  194. int vp9_frame_type_qdelta(const struct VP9_COMP *cpi, int rf_level, int q);
  195. void vp9_rc_update_framerate(struct VP9_COMP *cpi);
  196. void vp9_rc_set_gf_interval_range(const struct VP9_COMP *const cpi,
  197. RATE_CONTROL *const rc);
  198. void vp9_set_target_rate(struct VP9_COMP *cpi);
  199. #ifdef __cplusplus
  200. } // extern "C"
  201. #endif
  202. #endif // VP9_ENCODER_VP9_RATECTRL_H_