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 12 #include "vp8/common/blockd.h" 13 #include "onyxd_int.h" 14 #include "vpx_mem/vpx_mem.h" 15 #include "vpx_ports/mem.h" 16 #include "detokenize.h" 17 18 void vp8_reset_mb_tokens_context(MACROBLOCKD *x) 19 { 20 ENTROPY_CONTEXT *a_ctx = ((ENTROPY_CONTEXT *)x->above_context); 21 ENTROPY_CONTEXT *l_ctx = ((ENTROPY_CONTEXT *)x->left_context); 22 23 vpx_memset(a_ctx, 0, sizeof(ENTROPY_CONTEXT_PLANES)-1); 24 vpx_memset(l_ctx, 0, sizeof(ENTROPY_CONTEXT_PLANES)-1); 25 26 /* Clear entropy contexts for Y2 blocks */ 27 if (!x->mode_info_context->mbmi.is_4x4) 28 { 29 a_ctx[8] = l_ctx[8] = 0; 30 } 31 } 32 33 /* 34 ------------------------------------------------------------------------------ 35 Residual decoding (Paragraph 13.2 / 13.3) 36 */ 37 static const uint8_t kBands[16 + 1] = { 38 0, 1, 2, 3, 6, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7, 39 0 /* extra entry as sentinel */ 40 }; 41 42 static const uint8_t kCat3[] = { 173, 148, 140, 0 }; 43 static const uint8_t kCat4[] = { 176, 155, 140, 135, 0 }; 44 static const uint8_t kCat5[] = { 180, 157, 141, 134, 130, 0 }; 45 static const uint8_t kCat6[] = 46 { 254, 254, 243, 230, 196, 177, 153, 140, 133, 130, 129, 0 }; 47 static const uint8_t* const kCat3456[] = { kCat3, kCat4, kCat5, kCat6 }; 48 static const uint8_t kZigzag[16] = { 49 0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15 50 }; 51 52 #define VP8GetBit vp8dx_decode_bool 53 #define NUM_PROBAS 11 54 #define NUM_CTX 3 55 56 /* for const-casting */ 57 typedef const uint8_t (*ProbaArray)[NUM_CTX][NUM_PROBAS]; 58 59 static int GetSigned(BOOL_DECODER *br, int value_to_sign) 60 { 61 int split = (br->range + 1) >> 1; 62 VP8_BD_VALUE bigsplit = (VP8_BD_VALUE)split << (VP8_BD_VALUE_SIZE - 8); 63 int v; 64 65 if(br->count < 0) 66 vp8dx_bool_decoder_fill(br); 67 68 if ( br->value < bigsplit ) 69 { 70 br->range = split; 71 v= value_to_sign; 72 } 73 else 74 { 75 br->range = br->range-split; 76 br->value = br->value-bigsplit; 77 v = -value_to_sign; 78 } 79 br->range +=br->range; 80 br->value +=br->value; 81 br->count--; 82 83 return v; 84 } 85 /* 86 Returns the position of the last non-zero coeff plus one 87 (and 0 if there's no coeff at all) 88 */ 89 static int GetCoeffs(BOOL_DECODER *br, ProbaArray prob, 90 int ctx, int n, int16_t* out) 91 { 92 const uint8_t* p = prob[n][ctx]; 93 if (!VP8GetBit(br, p[0])) 94 { /* first EOB is more a 'CBP' bit. */ 95 return 0; 96 } 97 while (1) 98 { 99 ++n; 100 if (!VP8GetBit(br, p[1])) 101 { 102 p = prob[kBands[n]][0]; 103 } 104 else 105 { /* non zero coeff */ 106 int v, j; 107 if (!VP8GetBit(br, p[2])) 108 { 109 p = prob[kBands[n]][1]; 110 v = 1; 111 } 112 else 113 { 114 if (!VP8GetBit(br, p[3])) 115 { 116 if (!VP8GetBit(br, p[4])) 117 { 118 v = 2; 119 } 120 else 121 { 122 v = 3 + VP8GetBit(br, p[5]); 123 } 124 } 125 else 126 { 127 if (!VP8GetBit(br, p[6])) 128 { 129 if (!VP8GetBit(br, p[7])) 130 { 131 v = 5 + VP8GetBit(br, 159); 132 } else 133 { 134 v = 7 + 2 * VP8GetBit(br, 165); 135 v += VP8GetBit(br, 145); 136 } 137 } 138 else 139 { 140 const uint8_t* tab; 141 const int bit1 = VP8GetBit(br, p[8]); 142 const int bit0 = VP8GetBit(br, p[9 + bit1]); 143 const int cat = 2 * bit1 + bit0; 144 v = 0; 145 for (tab = kCat3456[cat]; *tab; ++tab) 146 { 147 v += v + VP8GetBit(br, *tab); 148 } 149 v += 3 + (8 << cat); 150 } 151 } 152 p = prob[kBands[n]][2]; 153 } 154 j = kZigzag[n - 1]; 155 156 out[j] = GetSigned(br, v); 157 158 if (n == 16 || !VP8GetBit(br, p[0])) 159 { /* EOB */ 160 return n; 161 } 162 } 163 if (n == 16) 164 { 165 return 16; 166 } 167 } 168 } 169 170 int vp8_decode_mb_tokens(VP8D_COMP *dx, MACROBLOCKD *x) 171 { 172 BOOL_DECODER *bc = x->current_bc; 173 const FRAME_CONTEXT * const fc = &dx->common.fc; 174 char *eobs = x->eobs; 175 176 int i; 177 int nonzeros; 178 int eobtotal = 0; 179 180 short *qcoeff_ptr; 181 ProbaArray coef_probs; 182 ENTROPY_CONTEXT *a_ctx = ((ENTROPY_CONTEXT *)x->above_context); 183 ENTROPY_CONTEXT *l_ctx = ((ENTROPY_CONTEXT *)x->left_context); 184 ENTROPY_CONTEXT *a; 185 ENTROPY_CONTEXT *l; 186 int skip_dc = 0; 187 188 qcoeff_ptr = &x->qcoeff[0]; 189 190 if (!x->mode_info_context->mbmi.is_4x4) 191 { 192 a = a_ctx + 8; 193 l = l_ctx + 8; 194 195 coef_probs = fc->coef_probs [1]; 196 197 nonzeros = GetCoeffs(bc, coef_probs, (*a + *l), 0, qcoeff_ptr + 24 * 16); 198 *a = *l = (nonzeros > 0); 199 200 eobs[24] = nonzeros; 201 eobtotal += nonzeros - 16; 202 203 coef_probs = fc->coef_probs [0]; 204 skip_dc = 1; 205 } 206 else 207 { 208 coef_probs = fc->coef_probs [3]; 209 skip_dc = 0; 210 } 211 212 for (i = 0; i < 16; ++i) 213 { 214 a = a_ctx + (i&3); 215 l = l_ctx + ((i&0xc)>>2); 216 217 nonzeros = GetCoeffs(bc, coef_probs, (*a + *l), skip_dc, qcoeff_ptr); 218 *a = *l = (nonzeros > 0); 219 220 nonzeros += skip_dc; 221 eobs[i] = nonzeros; 222 eobtotal += nonzeros; 223 qcoeff_ptr += 16; 224 } 225 226 coef_probs = fc->coef_probs [2]; 227 228 a_ctx += 4; 229 l_ctx += 4; 230 for (i = 16; i < 24; ++i) 231 { 232 a = a_ctx + ((i > 19)<<1) + (i&1); 233 l = l_ctx + ((i > 19)<<1) + ((i&3)>1); 234 235 nonzeros = GetCoeffs(bc, coef_probs, (*a + *l), 0, qcoeff_ptr); 236 *a = *l = (nonzeros > 0); 237 238 eobs[i] = nonzeros; 239 eobtotal += nonzeros; 240 qcoeff_ptr += 16; 241 } 242 243 return eobtotal; 244 } 245 246