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