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_COMMON_VP9_ENTROPY_H_ 12 #define VP9_COMMON_VP9_ENTROPY_H_ 13 14 #include "vpx/vpx_integer.h" 15 #include "vp9/common/vp9_treecoder.h" 16 #include "vp9/common/vp9_blockd.h" 17 #include "vp9/common/vp9_common.h" 18 19 /* Coefficient token alphabet */ 20 21 #define ZERO_TOKEN 0 /* 0 Extra Bits 0+0 */ 22 #define ONE_TOKEN 1 /* 1 Extra Bits 0+1 */ 23 #define TWO_TOKEN 2 /* 2 Extra Bits 0+1 */ 24 #define THREE_TOKEN 3 /* 3 Extra Bits 0+1 */ 25 #define FOUR_TOKEN 4 /* 4 Extra Bits 0+1 */ 26 #define DCT_VAL_CATEGORY1 5 /* 5-6 Extra Bits 1+1 */ 27 #define DCT_VAL_CATEGORY2 6 /* 7-10 Extra Bits 2+1 */ 28 #define DCT_VAL_CATEGORY3 7 /* 11-18 Extra Bits 3+1 */ 29 #define DCT_VAL_CATEGORY4 8 /* 19-34 Extra Bits 4+1 */ 30 #define DCT_VAL_CATEGORY5 9 /* 35-66 Extra Bits 5+1 */ 31 #define DCT_VAL_CATEGORY6 10 /* 67+ Extra Bits 14+1 */ 32 #define DCT_EOB_TOKEN 11 /* EOB Extra Bits 0+0 */ 33 #define MAX_ENTROPY_TOKENS 12 34 #define ENTROPY_NODES 11 35 #define EOSB_TOKEN 127 /* Not signalled, encoder only */ 36 37 #define INTER_MODE_CONTEXTS 7 38 39 extern const vp9_tree_index vp9_coef_tree[]; 40 41 #define DCT_EOB_MODEL_TOKEN 3 /* EOB Extra Bits 0+0 */ 42 extern const vp9_tree_index vp9_coefmodel_tree[]; 43 44 extern struct vp9_token vp9_coef_encodings[MAX_ENTROPY_TOKENS]; 45 46 typedef struct { 47 vp9_tree_p tree; 48 const vp9_prob *prob; 49 int len; 50 int base_val; 51 } vp9_extra_bit; 52 53 extern const vp9_extra_bit vp9_extra_bits[12]; /* indexed by token value */ 54 55 #define MAX_PROB 255 56 #define DCT_MAX_VALUE 16384 57 58 /* Coefficients are predicted via a 3-dimensional probability table. */ 59 60 /* Outside dimension. 0 = Y with DC, 1 = UV */ 61 #define BLOCK_TYPES 2 62 #define REF_TYPES 2 // intra=0, inter=1 63 64 /* Middle dimension reflects the coefficient position within the transform. */ 65 #define COEF_BANDS 6 66 67 /* Inside dimension is measure of nearby complexity, that reflects the energy 68 of nearby coefficients are nonzero. For the first coefficient (DC, unless 69 block type is 0), we look at the (already encoded) blocks above and to the 70 left of the current block. The context index is then the number (0,1,or 2) 71 of these blocks having nonzero coefficients. 72 After decoding a coefficient, the measure is determined by the size of the 73 most recently decoded coefficient. 74 Note that the intuitive meaning of this measure changes as coefficients 75 are decoded, e.g., prior to the first token, a zero means that my neighbors 76 are empty while, after the first token, because of the use of end-of-block, 77 a zero means we just decoded a zero and hence guarantees that a non-zero 78 coefficient will appear later in this block. However, this shift 79 in meaning is perfectly OK because our context depends also on the 80 coefficient band (and since zigzag positions 0, 1, and 2 are in 81 distinct bands). */ 82 83 #define PREV_COEF_CONTEXTS 6 84 85 // #define ENTROPY_STATS 86 87 typedef unsigned int vp9_coeff_count[REF_TYPES][COEF_BANDS][PREV_COEF_CONTEXTS] 88 [MAX_ENTROPY_TOKENS]; 89 typedef unsigned int vp9_coeff_stats[REF_TYPES][COEF_BANDS][PREV_COEF_CONTEXTS] 90 [ENTROPY_NODES][2]; 91 typedef vp9_prob vp9_coeff_probs[REF_TYPES][COEF_BANDS][PREV_COEF_CONTEXTS] 92 [ENTROPY_NODES]; 93 94 #define SUBEXP_PARAM 4 /* Subexponential code parameter */ 95 #define MODULUS_PARAM 13 /* Modulus parameter */ 96 97 struct VP9Common; 98 void vp9_default_coef_probs(struct VP9Common *cm); 99 extern DECLARE_ALIGNED(16, const int16_t, vp9_default_scan_4x4[16]); 100 101 extern DECLARE_ALIGNED(16, const int16_t, vp9_col_scan_4x4[16]); 102 extern DECLARE_ALIGNED(16, const int16_t, vp9_row_scan_4x4[16]); 103 104 extern DECLARE_ALIGNED(16, const int16_t, vp9_default_scan_8x8[64]); 105 106 extern DECLARE_ALIGNED(16, const int16_t, vp9_col_scan_8x8[64]); 107 extern DECLARE_ALIGNED(16, const int16_t, vp9_row_scan_8x8[64]); 108 109 extern DECLARE_ALIGNED(16, const int16_t, vp9_default_scan_16x16[256]); 110 111 extern DECLARE_ALIGNED(16, const int16_t, vp9_col_scan_16x16[256]); 112 extern DECLARE_ALIGNED(16, const int16_t, vp9_row_scan_16x16[256]); 113 114 extern DECLARE_ALIGNED(16, const int16_t, vp9_default_scan_32x32[1024]); 115 116 extern DECLARE_ALIGNED(16, int16_t, vp9_default_iscan_4x4[16]); 117 118 extern DECLARE_ALIGNED(16, int16_t, vp9_col_iscan_4x4[16]); 119 extern DECLARE_ALIGNED(16, int16_t, vp9_row_iscan_4x4[16]); 120 121 extern DECLARE_ALIGNED(16, int16_t, vp9_default_iscan_8x8[64]); 122 123 extern DECLARE_ALIGNED(16, int16_t, vp9_col_iscan_8x8[64]); 124 extern DECLARE_ALIGNED(16, int16_t, vp9_row_iscan_8x8[64]); 125 126 extern DECLARE_ALIGNED(16, int16_t, vp9_default_iscan_16x16[256]); 127 128 extern DECLARE_ALIGNED(16, int16_t, vp9_col_iscan_16x16[256]); 129 extern DECLARE_ALIGNED(16, int16_t, vp9_row_iscan_16x16[256]); 130 131 extern DECLARE_ALIGNED(16, int16_t, vp9_default_iscan_32x32[1024]); 132 133 #define MAX_NEIGHBORS 2 134 135 extern DECLARE_ALIGNED(16, int16_t, 136 vp9_default_scan_4x4_neighbors[17 * MAX_NEIGHBORS]); 137 extern DECLARE_ALIGNED(16, int16_t, 138 vp9_col_scan_4x4_neighbors[17 * MAX_NEIGHBORS]); 139 extern DECLARE_ALIGNED(16, int16_t, 140 vp9_row_scan_4x4_neighbors[17 * MAX_NEIGHBORS]); 141 extern DECLARE_ALIGNED(16, int16_t, 142 vp9_col_scan_8x8_neighbors[65 * MAX_NEIGHBORS]); 143 extern DECLARE_ALIGNED(16, int16_t, 144 vp9_row_scan_8x8_neighbors[65 * MAX_NEIGHBORS]); 145 extern DECLARE_ALIGNED(16, int16_t, 146 vp9_default_scan_8x8_neighbors[65 * MAX_NEIGHBORS]); 147 extern DECLARE_ALIGNED(16, int16_t, 148 vp9_col_scan_16x16_neighbors[257 * MAX_NEIGHBORS]); 149 extern DECLARE_ALIGNED(16, int16_t, 150 vp9_row_scan_16x16_neighbors[257 * MAX_NEIGHBORS]); 151 extern DECLARE_ALIGNED(16, int16_t, 152 vp9_default_scan_16x16_neighbors[257 * MAX_NEIGHBORS]); 153 extern DECLARE_ALIGNED(16, int16_t, 154 vp9_default_scan_32x32_neighbors[1025 * MAX_NEIGHBORS]); 155 156 void vp9_coef_tree_initialize(void); 157 void vp9_adapt_coef_probs(struct VP9Common *cm); 158 159 static INLINE void reset_skip_context(MACROBLOCKD *xd, BLOCK_SIZE bsize) { 160 int i; 161 for (i = 0; i < MAX_MB_PLANE; i++) { 162 struct macroblockd_plane *const pd = &xd->plane[i]; 163 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd); 164 vpx_memset(pd->above_context, 0, sizeof(ENTROPY_CONTEXT) * 165 num_4x4_blocks_wide_lookup[plane_bsize]); 166 vpx_memset(pd->left_context, 0, sizeof(ENTROPY_CONTEXT) * 167 num_4x4_blocks_high_lookup[plane_bsize]); 168 } 169 } 170 171 // This is the index in the scan order beyond which all coefficients for 172 // 8x8 transform and above are in the top band. 173 // For 4x4 blocks the index is less but to keep things common the lookup 174 // table for 4x4 is padded out to this index. 175 #define MAXBAND_INDEX 21 176 177 extern const uint8_t vp9_coefband_trans_8x8plus[MAXBAND_INDEX + 1]; 178 extern const uint8_t vp9_coefband_trans_4x4[MAXBAND_INDEX + 1]; 179 180 181 static int get_coef_band(const uint8_t * band_translate, int coef_index) { 182 return (coef_index > MAXBAND_INDEX) 183 ? (COEF_BANDS-1) : band_translate[coef_index]; 184 } 185 186 static INLINE int get_coef_context(const int16_t *neighbors, 187 uint8_t *token_cache, 188 int c) { 189 return (1 + token_cache[neighbors[MAX_NEIGHBORS * c + 0]] + 190 token_cache[neighbors[MAX_NEIGHBORS * c + 1]]) >> 1; 191 } 192 193 const int16_t *vp9_get_coef_neighbors_handle(const int16_t *scan); 194 195 196 // 128 lists of probabilities are stored for the following ONE node probs: 197 // 1, 3, 5, 7, ..., 253, 255 198 // In between probabilities are interpolated linearly 199 200 #define COEFPROB_MODELS 128 201 202 #define UNCONSTRAINED_NODES 3 203 204 #define PIVOT_NODE 2 // which node is pivot 205 206 typedef vp9_prob vp9_coeff_probs_model[REF_TYPES][COEF_BANDS] 207 [PREV_COEF_CONTEXTS] 208 [UNCONSTRAINED_NODES]; 209 210 typedef unsigned int vp9_coeff_count_model[REF_TYPES][COEF_BANDS] 211 [PREV_COEF_CONTEXTS] 212 [UNCONSTRAINED_NODES + 1]; 213 typedef unsigned int vp9_coeff_stats_model[REF_TYPES][COEF_BANDS] 214 [PREV_COEF_CONTEXTS] 215 [UNCONSTRAINED_NODES][2]; 216 217 void vp9_model_to_full_probs(const vp9_prob *model, vp9_prob *full); 218 219 static INLINE const int16_t* get_scan_4x4(TX_TYPE tx_type) { 220 switch (tx_type) { 221 case ADST_DCT: 222 return vp9_row_scan_4x4; 223 case DCT_ADST: 224 return vp9_col_scan_4x4; 225 default: 226 return vp9_default_scan_4x4; 227 } 228 } 229 230 static INLINE void get_scan_nb_4x4(TX_TYPE tx_type, 231 const int16_t **scan, const int16_t **nb) { 232 switch (tx_type) { 233 case ADST_DCT: 234 *scan = vp9_row_scan_4x4; 235 *nb = vp9_row_scan_4x4_neighbors; 236 break; 237 case DCT_ADST: 238 *scan = vp9_col_scan_4x4; 239 *nb = vp9_col_scan_4x4_neighbors; 240 break; 241 default: 242 *scan = vp9_default_scan_4x4; 243 *nb = vp9_default_scan_4x4_neighbors; 244 break; 245 } 246 } 247 248 static INLINE const int16_t* get_iscan_4x4(TX_TYPE tx_type) { 249 switch (tx_type) { 250 case ADST_DCT: 251 return vp9_row_iscan_4x4; 252 case DCT_ADST: 253 return vp9_col_iscan_4x4; 254 default: 255 return vp9_default_iscan_4x4; 256 } 257 } 258 259 static INLINE const int16_t* get_scan_8x8(TX_TYPE tx_type) { 260 switch (tx_type) { 261 case ADST_DCT: 262 return vp9_row_scan_8x8; 263 case DCT_ADST: 264 return vp9_col_scan_8x8; 265 default: 266 return vp9_default_scan_8x8; 267 } 268 } 269 270 static INLINE void get_scan_nb_8x8(TX_TYPE tx_type, 271 const int16_t **scan, const int16_t **nb) { 272 switch (tx_type) { 273 case ADST_DCT: 274 *scan = vp9_row_scan_8x8; 275 *nb = vp9_row_scan_8x8_neighbors; 276 break; 277 case DCT_ADST: 278 *scan = vp9_col_scan_8x8; 279 *nb = vp9_col_scan_8x8_neighbors; 280 break; 281 default: 282 *scan = vp9_default_scan_8x8; 283 *nb = vp9_default_scan_8x8_neighbors; 284 break; 285 } 286 } 287 288 static INLINE const int16_t* get_iscan_8x8(TX_TYPE tx_type) { 289 switch (tx_type) { 290 case ADST_DCT: 291 return vp9_row_iscan_8x8; 292 case DCT_ADST: 293 return vp9_col_iscan_8x8; 294 default: 295 return vp9_default_iscan_8x8; 296 } 297 } 298 299 static INLINE const int16_t* get_scan_16x16(TX_TYPE tx_type) { 300 switch (tx_type) { 301 case ADST_DCT: 302 return vp9_row_scan_16x16; 303 case DCT_ADST: 304 return vp9_col_scan_16x16; 305 default: 306 return vp9_default_scan_16x16; 307 } 308 } 309 310 static INLINE void get_scan_nb_16x16(TX_TYPE tx_type, 311 const int16_t **scan, const int16_t **nb) { 312 switch (tx_type) { 313 case ADST_DCT: 314 *scan = vp9_row_scan_16x16; 315 *nb = vp9_row_scan_16x16_neighbors; 316 break; 317 case DCT_ADST: 318 *scan = vp9_col_scan_16x16; 319 *nb = vp9_col_scan_16x16_neighbors; 320 break; 321 default: 322 *scan = vp9_default_scan_16x16; 323 *nb = vp9_default_scan_16x16_neighbors; 324 break; 325 } 326 } 327 328 static INLINE const int16_t* get_iscan_16x16(TX_TYPE tx_type) { 329 switch (tx_type) { 330 case ADST_DCT: 331 return vp9_row_iscan_16x16; 332 case DCT_ADST: 333 return vp9_col_iscan_16x16; 334 default: 335 return vp9_default_iscan_16x16; 336 } 337 } 338 339 static int get_entropy_context(const MACROBLOCKD *xd, TX_SIZE tx_size, 340 PLANE_TYPE type, int block_idx, 341 ENTROPY_CONTEXT *A, ENTROPY_CONTEXT *L, 342 const int16_t **scan, 343 const uint8_t **band_translate) { 344 ENTROPY_CONTEXT above_ec = 0, left_ec = 0; 345 346 switch (tx_size) { 347 case TX_4X4: 348 *scan = get_scan_4x4(get_tx_type_4x4(type, xd, block_idx)); 349 *band_translate = vp9_coefband_trans_4x4; 350 above_ec = A[0] != 0; 351 left_ec = L[0] != 0; 352 break; 353 case TX_8X8: 354 *scan = get_scan_8x8(get_tx_type_8x8(type, xd)); 355 *band_translate = vp9_coefband_trans_8x8plus; 356 above_ec = !!*(uint16_t *)A; 357 left_ec = !!*(uint16_t *)L; 358 break; 359 case TX_16X16: 360 *scan = get_scan_16x16(get_tx_type_16x16(type, xd)); 361 *band_translate = vp9_coefband_trans_8x8plus; 362 above_ec = !!*(uint32_t *)A; 363 left_ec = !!*(uint32_t *)L; 364 break; 365 case TX_32X32: 366 *scan = vp9_default_scan_32x32; 367 *band_translate = vp9_coefband_trans_8x8plus; 368 above_ec = !!*(uint64_t *)A; 369 left_ec = !!*(uint64_t *)L; 370 break; 371 default: 372 assert(!"Invalid transform size."); 373 } 374 375 return combine_entropy_contexts(above_ec, left_ec); 376 } 377 378 enum { VP9_COEF_UPDATE_PROB = 252 }; 379 380 #endif // VP9_COMMON_VP9_ENTROPY_H_ 381