| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. | 2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #ifndef VP9_COMMON_VP9_ENTROPY_H_ | 11 #ifndef VP9_COMMON_VP9_ENTROPY_H_ |
| 12 #define VP9_COMMON_VP9_ENTROPY_H_ | 12 #define VP9_COMMON_VP9_ENTROPY_H_ |
| 13 | 13 |
| 14 #include "vpx/vpx_integer.h" | 14 #include "vpx/vpx_integer.h" |
| 15 | 15 |
| 16 #include "vp9/common/vp9_blockd.h" | 16 #include "vp9/common/vp9_blockd.h" |
| 17 #include "vp9/common/vp9_common.h" | 17 #include "vp9/common/vp9_common.h" |
| 18 #include "vp9/common/vp9_scan.h" | 18 #include "vp9/common/vp9_scan.h" |
| 19 #include "vp9/common/vp9_treecoder.h" | 19 #include "vp9/common/vp9_treecoder.h" |
| 20 #include "vp9/common/vp9_entropymode.h" |
| 20 | 21 |
| 21 #define DIFF_UPDATE_PROB 252 | 22 #define DIFF_UPDATE_PROB 252 |
| 22 | 23 |
| 23 /* Coefficient token alphabet */ | 24 // Coefficient token alphabet |
| 25 #define ZERO_TOKEN 0 // 0 Extra Bits 0+0 |
| 26 #define ONE_TOKEN 1 // 1 Extra Bits 0+1 |
| 27 #define TWO_TOKEN 2 // 2 Extra Bits 0+1 |
| 28 #define THREE_TOKEN 3 // 3 Extra Bits 0+1 |
| 29 #define FOUR_TOKEN 4 // 4 Extra Bits 0+1 |
| 30 #define CATEGORY1_TOKEN 5 // 5-6 Extra Bits 1+1 |
| 31 #define CATEGORY2_TOKEN 6 // 7-10 Extra Bits 2+1 |
| 32 #define CATEGORY3_TOKEN 7 // 11-18 Extra Bits 3+1 |
| 33 #define CATEGORY4_TOKEN 8 // 19-34 Extra Bits 4+1 |
| 34 #define CATEGORY5_TOKEN 9 // 35-66 Extra Bits 5+1 |
| 35 #define CATEGORY6_TOKEN 10 // 67+ Extra Bits 14+1 |
| 36 #define EOB_TOKEN 11 // EOB Extra Bits 0+0 |
| 24 | 37 |
| 25 #define ZERO_TOKEN 0 /* 0 Extra Bits 0+0 */ | 38 #define ENTROPY_TOKENS 12 |
| 26 #define ONE_TOKEN 1 /* 1 Extra Bits 0+1 */ | |
| 27 #define TWO_TOKEN 2 /* 2 Extra Bits 0+1 */ | |
| 28 #define THREE_TOKEN 3 /* 3 Extra Bits 0+1 */ | |
| 29 #define FOUR_TOKEN 4 /* 4 Extra Bits 0+1 */ | |
| 30 #define DCT_VAL_CATEGORY1 5 /* 5-6 Extra Bits 1+1 */ | |
| 31 #define DCT_VAL_CATEGORY2 6 /* 7-10 Extra Bits 2+1 */ | |
| 32 #define DCT_VAL_CATEGORY3 7 /* 11-18 Extra Bits 3+1 */ | |
| 33 #define DCT_VAL_CATEGORY4 8 /* 19-34 Extra Bits 4+1 */ | |
| 34 #define DCT_VAL_CATEGORY5 9 /* 35-66 Extra Bits 5+1 */ | |
| 35 #define DCT_VAL_CATEGORY6 10 /* 67+ Extra Bits 14+1 */ | |
| 36 #define DCT_EOB_TOKEN 11 /* EOB Extra Bits 0+0 */ | |
| 37 #define MAX_ENTROPY_TOKENS 12 | |
| 38 #define ENTROPY_NODES 11 | |
| 39 #define EOSB_TOKEN 127 /* Not signalled, encoder only */ | |
| 40 | 39 |
| 41 #define INTER_MODE_CONTEXTS 7 | 40 #define ENTROPY_NODES 11 |
| 42 | 41 |
| 43 extern DECLARE_ALIGNED(16, const uint8_t, | 42 extern DECLARE_ALIGNED(16, const uint8_t, vp9_pt_energy_class[ENTROPY_TOKENS]); |
| 44 vp9_pt_energy_class[MAX_ENTROPY_TOKENS]); | |
| 45 | 43 |
| 46 extern const vp9_tree_index vp9_coef_tree[TREE_SIZE(MAX_ENTROPY_TOKENS)]; | 44 #define EOB_MODEL_TOKEN 3 |
| 47 | |
| 48 #define DCT_EOB_MODEL_TOKEN 3 /* EOB Extra Bits 0+0 */ | |
| 49 extern const vp9_tree_index vp9_coefmodel_tree[]; | 45 extern const vp9_tree_index vp9_coefmodel_tree[]; |
| 50 | 46 |
| 51 extern struct vp9_token vp9_coef_encodings[MAX_ENTROPY_TOKENS]; | |
| 52 | |
| 53 typedef struct { | 47 typedef struct { |
| 54 vp9_tree_index *tree; | 48 const vp9_tree_index *tree; |
| 55 const vp9_prob *prob; | 49 const vp9_prob *prob; |
| 56 int len; | 50 int len; |
| 57 int base_val; | 51 int base_val; |
| 58 } vp9_extra_bit; | 52 } vp9_extra_bit; |
| 59 | 53 |
| 60 // indexed by token value | 54 // indexed by token value |
| 61 extern const vp9_extra_bit vp9_extra_bits[MAX_ENTROPY_TOKENS]; | 55 extern const vp9_extra_bit vp9_extra_bits[ENTROPY_TOKENS]; |
| 62 | 56 |
| 63 #define MAX_PROB 255 | 57 #define MAX_PROB 255 |
| 64 #define DCT_MAX_VALUE 16384 | 58 #define DCT_MAX_VALUE 16384 |
| 65 | 59 |
| 66 /* Coefficients are predicted via a 3-dimensional probability table. */ | 60 /* Coefficients are predicted via a 3-dimensional probability table. */ |
| 67 | 61 |
| 68 /* Outside dimension. 0 = Y with DC, 1 = UV */ | |
| 69 #define BLOCK_TYPES 2 | |
| 70 #define REF_TYPES 2 // intra=0, inter=1 | 62 #define REF_TYPES 2 // intra=0, inter=1 |
| 71 | 63 |
| 72 /* Middle dimension reflects the coefficient position within the transform. */ | 64 /* Middle dimension reflects the coefficient position within the transform. */ |
| 73 #define COEF_BANDS 6 | 65 #define COEF_BANDS 6 |
| 74 | 66 |
| 75 /* Inside dimension is measure of nearby complexity, that reflects the energy | 67 /* Inside dimension is measure of nearby complexity, that reflects the energy |
| 76 of nearby coefficients are nonzero. For the first coefficient (DC, unless | 68 of nearby coefficients are nonzero. For the first coefficient (DC, unless |
| 77 block type is 0), we look at the (already encoded) blocks above and to the | 69 block type is 0), we look at the (already encoded) blocks above and to the |
| 78 left of the current block. The context index is then the number (0,1,or 2) | 70 left of the current block. The context index is then the number (0,1,or 2) |
| 79 of these blocks having nonzero coefficients. | 71 of these blocks having nonzero coefficients. |
| 80 After decoding a coefficient, the measure is determined by the size of the | 72 After decoding a coefficient, the measure is determined by the size of the |
| 81 most recently decoded coefficient. | 73 most recently decoded coefficient. |
| 82 Note that the intuitive meaning of this measure changes as coefficients | 74 Note that the intuitive meaning of this measure changes as coefficients |
| 83 are decoded, e.g., prior to the first token, a zero means that my neighbors | 75 are decoded, e.g., prior to the first token, a zero means that my neighbors |
| 84 are empty while, after the first token, because of the use of end-of-block, | 76 are empty while, after the first token, because of the use of end-of-block, |
| 85 a zero means we just decoded a zero and hence guarantees that a non-zero | 77 a zero means we just decoded a zero and hence guarantees that a non-zero |
| 86 coefficient will appear later in this block. However, this shift | 78 coefficient will appear later in this block. However, this shift |
| 87 in meaning is perfectly OK because our context depends also on the | 79 in meaning is perfectly OK because our context depends also on the |
| 88 coefficient band (and since zigzag positions 0, 1, and 2 are in | 80 coefficient band (and since zigzag positions 0, 1, and 2 are in |
| 89 distinct bands). */ | 81 distinct bands). */ |
| 90 | 82 |
| 91 #define PREV_COEF_CONTEXTS 6 | 83 #define COEFF_CONTEXTS 6 |
| 84 #define BAND_COEFF_CONTEXTS(band) ((band) == 0 ? 3 : COEFF_CONTEXTS) |
| 92 | 85 |
| 93 // #define ENTROPY_STATS | 86 // #define ENTROPY_STATS |
| 94 | 87 |
| 95 typedef unsigned int vp9_coeff_count[REF_TYPES][COEF_BANDS][PREV_COEF_CONTEXTS] | 88 typedef unsigned int vp9_coeff_count[REF_TYPES][COEF_BANDS][COEFF_CONTEXTS] |
| 96 [MAX_ENTROPY_TOKENS]; | 89 [ENTROPY_TOKENS]; |
| 97 typedef unsigned int vp9_coeff_stats[REF_TYPES][COEF_BANDS][PREV_COEF_CONTEXTS] | 90 typedef unsigned int vp9_coeff_stats[REF_TYPES][COEF_BANDS][COEFF_CONTEXTS] |
| 98 [ENTROPY_NODES][2]; | 91 [ENTROPY_NODES][2]; |
| 99 | 92 |
| 100 #define SUBEXP_PARAM 4 /* Subexponential code parameter */ | 93 #define SUBEXP_PARAM 4 /* Subexponential code parameter */ |
| 101 #define MODULUS_PARAM 13 /* Modulus parameter */ | 94 #define MODULUS_PARAM 13 /* Modulus parameter */ |
| 102 | 95 |
| 103 struct VP9Common; | 96 struct VP9Common; |
| 104 void vp9_default_coef_probs(struct VP9Common *cm); | 97 void vp9_default_coef_probs(struct VP9Common *cm); |
| 105 | |
| 106 void vp9_coef_tree_initialize(); | |
| 107 void vp9_adapt_coef_probs(struct VP9Common *cm); | 98 void vp9_adapt_coef_probs(struct VP9Common *cm); |
| 108 | 99 |
| 109 static INLINE void reset_skip_context(MACROBLOCKD *xd, BLOCK_SIZE bsize) { | 100 static INLINE void reset_skip_context(MACROBLOCKD *xd, BLOCK_SIZE bsize) { |
| 110 int i; | 101 int i; |
| 111 for (i = 0; i < MAX_MB_PLANE; i++) { | 102 for (i = 0; i < MAX_MB_PLANE; i++) { |
| 112 struct macroblockd_plane *const pd = &xd->plane[i]; | 103 struct macroblockd_plane *const pd = &xd->plane[i]; |
| 113 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd); | 104 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd); |
| 114 vpx_memset(pd->above_context, 0, sizeof(ENTROPY_CONTEXT) * | 105 vpx_memset(pd->above_context, 0, sizeof(ENTROPY_CONTEXT) * |
| 115 num_4x4_blocks_wide_lookup[plane_bsize]); | 106 num_4x4_blocks_wide_lookup[plane_bsize]); |
| 116 vpx_memset(pd->left_context, 0, sizeof(ENTROPY_CONTEXT) * | 107 vpx_memset(pd->left_context, 0, sizeof(ENTROPY_CONTEXT) * |
| 117 num_4x4_blocks_high_lookup[plane_bsize]); | 108 num_4x4_blocks_high_lookup[plane_bsize]); |
| 118 } | 109 } |
| 119 } | 110 } |
| 120 | 111 |
| 121 // This is the index in the scan order beyond which all coefficients for | 112 // This is the index in the scan order beyond which all coefficients for |
| 122 // 8x8 transform and above are in the top band. | 113 // 8x8 transform and above are in the top band. |
| 123 // For 4x4 blocks the index is less but to keep things common the lookup | 114 // This macro is currently unused but may be used by certain implementations |
| 124 // table for 4x4 is padded out to this index. | |
| 125 #define MAXBAND_INDEX 21 | 115 #define MAXBAND_INDEX 21 |
| 126 | 116 |
| 127 extern const uint8_t vp9_coefband_trans_8x8plus[MAXBAND_INDEX + 1]; | 117 extern const uint8_t vp9_coefband_trans_8x8plus[1024]; |
| 128 extern const uint8_t vp9_coefband_trans_4x4[MAXBAND_INDEX + 1]; | 118 extern const uint8_t vp9_coefband_trans_4x4[16]; |
| 129 | 119 |
| 130 | 120 static const uint8_t *get_band_translate(TX_SIZE tx_size) { |
| 131 static int get_coef_band(const uint8_t * band_translate, int coef_index) { | 121 return tx_size == TX_4X4 ? vp9_coefband_trans_4x4 |
| 132 return (coef_index > MAXBAND_INDEX) | 122 : vp9_coefband_trans_8x8plus; |
| 133 ? (COEF_BANDS-1) : band_translate[coef_index]; | |
| 134 } | 123 } |
| 135 | 124 |
| 136 // 128 lists of probabilities are stored for the following ONE node probs: | 125 // 128 lists of probabilities are stored for the following ONE node probs: |
| 137 // 1, 3, 5, 7, ..., 253, 255 | 126 // 1, 3, 5, 7, ..., 253, 255 |
| 138 // In between probabilities are interpolated linearly | 127 // In between probabilities are interpolated linearly |
| 139 | 128 |
| 140 #define COEFPROB_MODELS 128 | 129 #define COEFF_PROB_MODELS 256 |
| 141 | 130 |
| 142 #define UNCONSTRAINED_NODES 3 | 131 #define UNCONSTRAINED_NODES 3 |
| 143 | 132 |
| 144 #define PIVOT_NODE 2 // which node is pivot | 133 #define PIVOT_NODE 2 // which node is pivot |
| 145 | 134 |
| 135 #define MODEL_NODES (ENTROPY_NODES - UNCONSTRAINED_NODES) |
| 136 extern const vp9_prob vp9_pareto8_full[COEFF_PROB_MODELS][MODEL_NODES]; |
| 137 |
| 146 typedef vp9_prob vp9_coeff_probs_model[REF_TYPES][COEF_BANDS] | 138 typedef vp9_prob vp9_coeff_probs_model[REF_TYPES][COEF_BANDS] |
| 147 [PREV_COEF_CONTEXTS] | 139 [COEFF_CONTEXTS][UNCONSTRAINED_NODES]; |
| 148 [UNCONSTRAINED_NODES]; | |
| 149 | 140 |
| 150 typedef unsigned int vp9_coeff_count_model[REF_TYPES][COEF_BANDS] | 141 typedef unsigned int vp9_coeff_count_model[REF_TYPES][COEF_BANDS] |
| 151 [PREV_COEF_CONTEXTS] | 142 [COEFF_CONTEXTS] |
| 152 [UNCONSTRAINED_NODES + 1]; | 143 [UNCONSTRAINED_NODES + 1]; |
| 153 | 144 |
| 154 void vp9_model_to_full_probs(const vp9_prob *model, vp9_prob *full); | 145 void vp9_model_to_full_probs(const vp9_prob *model, vp9_prob *full); |
| 155 | 146 |
| 156 static int get_entropy_context(TX_SIZE tx_size, | 147 static int get_entropy_context(TX_SIZE tx_size, const ENTROPY_CONTEXT *a, |
| 157 ENTROPY_CONTEXT *a, ENTROPY_CONTEXT *l) { | 148 const ENTROPY_CONTEXT *l) { |
| 158 ENTROPY_CONTEXT above_ec = 0, left_ec = 0; | 149 ENTROPY_CONTEXT above_ec = 0, left_ec = 0; |
| 159 | 150 |
| 160 switch (tx_size) { | 151 switch (tx_size) { |
| 161 case TX_4X4: | 152 case TX_4X4: |
| 162 above_ec = a[0] != 0; | 153 above_ec = a[0] != 0; |
| 163 left_ec = l[0] != 0; | 154 left_ec = l[0] != 0; |
| 164 break; | 155 break; |
| 165 case TX_8X8: | 156 case TX_8X8: |
| 166 above_ec = !!*(uint16_t *)a; | 157 above_ec = !!*(const uint16_t *)a; |
| 167 left_ec = !!*(uint16_t *)l; | 158 left_ec = !!*(const uint16_t *)l; |
| 168 break; | 159 break; |
| 169 case TX_16X16: | 160 case TX_16X16: |
| 170 above_ec = !!*(uint32_t *)a; | 161 above_ec = !!*(const uint32_t *)a; |
| 171 left_ec = !!*(uint32_t *)l; | 162 left_ec = !!*(const uint32_t *)l; |
| 172 break; | 163 break; |
| 173 case TX_32X32: | 164 case TX_32X32: |
| 174 above_ec = !!*(uint64_t *)a; | 165 above_ec = !!*(const uint64_t *)a; |
| 175 left_ec = !!*(uint64_t *)l; | 166 left_ec = !!*(const uint64_t *)l; |
| 176 break; | 167 break; |
| 177 default: | 168 default: |
| 178 assert(!"Invalid transform size."); | 169 assert(0 && "Invalid transform size."); |
| 179 } | 170 } |
| 180 | 171 |
| 181 return combine_entropy_contexts(above_ec, left_ec); | 172 return combine_entropy_contexts(above_ec, left_ec); |
| 182 } | 173 } |
| 183 | 174 |
| 184 static const uint8_t *get_band_translate(TX_SIZE tx_size) { | 175 static const scan_order *get_scan(const MACROBLOCKD *xd, TX_SIZE tx_size, |
| 185 return tx_size == TX_4X4 ? vp9_coefband_trans_4x4 | 176 PLANE_TYPE type, int block_idx) { |
| 186 : vp9_coefband_trans_8x8plus; | 177 const MODE_INFO *const mi = xd->mi_8x8[0]; |
| 187 } | 178 const MB_MODE_INFO *const mbmi = &mi->mbmi; |
| 188 | 179 |
| 189 static void get_scan(const MACROBLOCKD *xd, TX_SIZE tx_size, | 180 if (is_inter_block(mbmi) || type != PLANE_TYPE_Y || xd->lossless) { |
| 190 PLANE_TYPE type, int block_idx, | 181 return &vp9_default_scan_orders[tx_size]; |
| 191 const int16_t **scan, const int16_t **scan_nb) { | 182 } else { |
| 192 switch (tx_size) { | 183 const MB_PREDICTION_MODE mode = |
| 193 case TX_4X4: | 184 mbmi->sb_type < BLOCK_8X8 ? mi->bmi[block_idx].as_mode : mbmi->mode; |
| 194 get_scan_nb_4x4(get_tx_type_4x4(type, xd, block_idx), scan, scan_nb); | 185 return &vp9_scan_orders[tx_size][mode2txfm_map[mode]]; |
| 195 break; | |
| 196 case TX_8X8: | |
| 197 get_scan_nb_8x8(get_tx_type_8x8(type, xd), scan, scan_nb); | |
| 198 break; | |
| 199 case TX_16X16: | |
| 200 get_scan_nb_16x16(get_tx_type_16x16(type, xd), scan, scan_nb); | |
| 201 break; | |
| 202 case TX_32X32: | |
| 203 *scan = vp9_default_scan_32x32; | |
| 204 *scan_nb = vp9_default_scan_32x32_neighbors; | |
| 205 break; | |
| 206 default: | |
| 207 assert(!"Invalid transform size."); | |
| 208 } | 186 } |
| 209 } | 187 } |
| 210 | 188 |
| 211 #endif // VP9_COMMON_VP9_ENTROPY_H_ | 189 #endif // VP9_COMMON_VP9_ENTROPY_H_ |
| OLD | NEW |