vp9_rd.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 VPX_VP9_ENCODER_VP9_RD_H_
  11. #define VPX_VP9_ENCODER_VP9_RD_H_
  12. #include <limits.h>
  13. #include "vp9/common/vp9_blockd.h"
  14. #include "vp9/encoder/vp9_block.h"
  15. #include "vp9/encoder/vp9_context_tree.h"
  16. #include "vp9/encoder/vp9_cost.h"
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. #define RDDIV_BITS 7
  21. #define RD_EPB_SHIFT 6
  22. #define RDCOST(RM, DM, R, D) \
  23. ROUND_POWER_OF_TWO(((int64_t)(R)) * (RM), VP9_PROB_COST_SHIFT) + ((D) << (DM))
  24. #define RDCOST_NEG_R(RM, DM, R, D) \
  25. ((D) << (DM)) - ROUND_POWER_OF_TWO(((int64_t)(R)) * (RM), VP9_PROB_COST_SHIFT)
  26. #define RDCOST_NEG_D(RM, DM, R, D) \
  27. ROUND_POWER_OF_TWO(((int64_t)(R)) * (RM), VP9_PROB_COST_SHIFT) - ((D) << (DM))
  28. #define QIDX_SKIP_THRESH 115
  29. #define MV_COST_WEIGHT 108
  30. #define MV_COST_WEIGHT_SUB 120
  31. #define INVALID_MV 0x80008000
  32. #define MAX_MODES 30
  33. #define MAX_REFS 6
  34. #define RD_THRESH_INIT_FACT 32
  35. #define RD_THRESH_MAX_FACT 64
  36. #define RD_THRESH_INC 1
  37. #define VP9_DIST_SCALE_LOG2 4
  38. #define VP9_DIST_SCALE (1 << VP9_DIST_SCALE_LOG2)
  39. // This enumerator type needs to be kept aligned with the mode order in
  40. // const MODE_DEFINITION vp9_mode_order[MAX_MODES] used in the rd code.
  41. typedef enum {
  42. THR_NEARESTMV,
  43. THR_NEARESTA,
  44. THR_NEARESTG,
  45. THR_DC,
  46. THR_NEWMV,
  47. THR_NEWA,
  48. THR_NEWG,
  49. THR_NEARMV,
  50. THR_NEARA,
  51. THR_NEARG,
  52. THR_ZEROMV,
  53. THR_ZEROG,
  54. THR_ZEROA,
  55. THR_COMP_NEARESTLA,
  56. THR_COMP_NEARESTGA,
  57. THR_TM,
  58. THR_COMP_NEARLA,
  59. THR_COMP_NEWLA,
  60. THR_COMP_NEARGA,
  61. THR_COMP_NEWGA,
  62. THR_COMP_ZEROLA,
  63. THR_COMP_ZEROGA,
  64. THR_H_PRED,
  65. THR_V_PRED,
  66. THR_D135_PRED,
  67. THR_D207_PRED,
  68. THR_D153_PRED,
  69. THR_D63_PRED,
  70. THR_D117_PRED,
  71. THR_D45_PRED,
  72. } THR_MODES;
  73. typedef enum {
  74. THR_LAST,
  75. THR_GOLD,
  76. THR_ALTR,
  77. THR_COMP_LA,
  78. THR_COMP_GA,
  79. THR_INTRA,
  80. } THR_MODES_SUB8X8;
  81. typedef struct RD_OPT {
  82. // Thresh_mult is used to set a threshold for the rd score. A higher value
  83. // means that we will accept the best mode so far more often. This number
  84. // is used in combination with the current block size, and thresh_freq_fact to
  85. // pick a threshold.
  86. int thresh_mult[MAX_MODES];
  87. int thresh_mult_sub8x8[MAX_REFS];
  88. int threshes[MAX_SEGMENTS][BLOCK_SIZES][MAX_MODES];
  89. int64_t prediction_type_threshes[MAX_REF_FRAMES][REFERENCE_MODES];
  90. int64_t filter_threshes[MAX_REF_FRAMES][SWITCHABLE_FILTER_CONTEXTS];
  91. #if CONFIG_CONSISTENT_RECODE
  92. int64_t prediction_type_threshes_prev[MAX_REF_FRAMES][REFERENCE_MODES];
  93. int64_t filter_threshes_prev[MAX_REF_FRAMES][SWITCHABLE_FILTER_CONTEXTS];
  94. #endif
  95. int RDMULT;
  96. int RDDIV;
  97. double r0;
  98. } RD_OPT;
  99. typedef struct RD_COST {
  100. int rate;
  101. int64_t dist;
  102. int64_t rdcost;
  103. } RD_COST;
  104. // Reset the rate distortion cost values to maximum (invalid) value.
  105. void vp9_rd_cost_reset(RD_COST *rd_cost);
  106. // Initialize the rate distortion cost values to zero.
  107. void vp9_rd_cost_init(RD_COST *rd_cost);
  108. // It supports negative rate and dist, which is different from RDCOST().
  109. int64_t vp9_calculate_rd_cost(int mult, int div, int rate, int64_t dist);
  110. // Update the cost value based on its rate and distortion.
  111. void vp9_rd_cost_update(int mult, int div, RD_COST *rd_cost);
  112. struct TileInfo;
  113. struct TileDataEnc;
  114. struct VP9_COMP;
  115. struct macroblock;
  116. int vp9_compute_rd_mult_based_on_qindex(const struct VP9_COMP *cpi, int qindex);
  117. int vp9_compute_rd_mult(const struct VP9_COMP *cpi, int qindex);
  118. int vp9_get_adaptive_rdmult(const struct VP9_COMP *cpi, double beta);
  119. void vp9_initialize_rd_consts(struct VP9_COMP *cpi);
  120. void vp9_initialize_me_consts(struct VP9_COMP *cpi, MACROBLOCK *x, int qindex);
  121. void vp9_model_rd_from_var_lapndz(unsigned int var, unsigned int n_log2,
  122. unsigned int qstep, int *rate, int64_t *dist);
  123. void vp9_model_rd_from_var_lapndz_vec(unsigned int var[MAX_MB_PLANE],
  124. unsigned int n_log2[MAX_MB_PLANE],
  125. unsigned int qstep[MAX_MB_PLANE],
  126. int64_t *rate_sum, int64_t *dist_sum);
  127. int vp9_get_switchable_rate(const struct VP9_COMP *cpi,
  128. const MACROBLOCKD *const xd);
  129. int vp9_raster_block_offset(BLOCK_SIZE plane_bsize, int raster_block,
  130. int stride);
  131. int16_t *vp9_raster_block_offset_int16(BLOCK_SIZE plane_bsize, int raster_block,
  132. int16_t *base);
  133. YV12_BUFFER_CONFIG *vp9_get_scaled_ref_frame(const struct VP9_COMP *cpi,
  134. int ref_frame);
  135. void vp9_init_me_luts(void);
  136. void vp9_get_entropy_contexts(BLOCK_SIZE bsize, TX_SIZE tx_size,
  137. const struct macroblockd_plane *pd,
  138. ENTROPY_CONTEXT t_above[16],
  139. ENTROPY_CONTEXT t_left[16]);
  140. void vp9_set_rd_speed_thresholds(struct VP9_COMP *cpi);
  141. void vp9_set_rd_speed_thresholds_sub8x8(struct VP9_COMP *cpi);
  142. void vp9_update_rd_thresh_fact(int (*factor_buf)[MAX_MODES], int rd_thresh,
  143. int bsize, int best_mode_index);
  144. static INLINE int rd_less_than_thresh(int64_t best_rd, int thresh,
  145. const int *const thresh_fact) {
  146. return best_rd < ((int64_t)thresh * (*thresh_fact) >> 5) || thresh == INT_MAX;
  147. }
  148. static INLINE void set_error_per_bit(MACROBLOCK *x, int rdmult) {
  149. x->errorperbit = rdmult >> RD_EPB_SHIFT;
  150. x->errorperbit += (x->errorperbit == 0);
  151. }
  152. void vp9_mv_pred(struct VP9_COMP *cpi, MACROBLOCK *x, uint8_t *ref_y_buffer,
  153. int ref_y_stride, int ref_frame, BLOCK_SIZE block_size);
  154. void vp9_setup_pred_block(const MACROBLOCKD *xd,
  155. struct buf_2d dst[MAX_MB_PLANE],
  156. const YV12_BUFFER_CONFIG *src, int mi_row, int mi_col,
  157. const struct scale_factors *scale,
  158. const struct scale_factors *scale_uv);
  159. int vp9_get_intra_cost_penalty(const struct VP9_COMP *const cpi,
  160. BLOCK_SIZE bsize, int qindex, int qdelta);
  161. unsigned int vp9_get_sby_variance(struct VP9_COMP *cpi,
  162. const struct buf_2d *ref, BLOCK_SIZE bs);
  163. unsigned int vp9_get_sby_perpixel_variance(struct VP9_COMP *cpi,
  164. const struct buf_2d *ref,
  165. BLOCK_SIZE bs);
  166. #if CONFIG_VP9_HIGHBITDEPTH
  167. unsigned int vp9_high_get_sby_variance(struct VP9_COMP *cpi,
  168. const struct buf_2d *ref, BLOCK_SIZE bs,
  169. int bd);
  170. unsigned int vp9_high_get_sby_perpixel_variance(struct VP9_COMP *cpi,
  171. const struct buf_2d *ref,
  172. BLOCK_SIZE bs, int bd);
  173. #endif
  174. void vp9_build_inter_mode_cost(struct VP9_COMP *cpi);
  175. #ifdef __cplusplus
  176. } // extern "C"
  177. #endif
  178. #endif // VPX_VP9_ENCODER_VP9_RD_H_