michael@0: /* michael@0: * Copyright (c) 2010 The WebM project authors. All Rights Reserved. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license michael@0: * that can be found in the LICENSE file in the root of the source michael@0: * tree. An additional intellectual property rights grant can be found michael@0: * in the file PATENTS. All contributing project authors may michael@0: * be found in the AUTHORS file in the root of the source tree. michael@0: */ michael@0: michael@0: michael@0: #include "vp8/common/blockd.h" michael@0: #include "onyxd_int.h" michael@0: #include "vpx_mem/vpx_mem.h" michael@0: #include "vpx_ports/mem.h" michael@0: #include "detokenize.h" michael@0: michael@0: void vp8_reset_mb_tokens_context(MACROBLOCKD *x) michael@0: { michael@0: ENTROPY_CONTEXT *a_ctx = ((ENTROPY_CONTEXT *)x->above_context); michael@0: ENTROPY_CONTEXT *l_ctx = ((ENTROPY_CONTEXT *)x->left_context); michael@0: michael@0: vpx_memset(a_ctx, 0, sizeof(ENTROPY_CONTEXT_PLANES)-1); michael@0: vpx_memset(l_ctx, 0, sizeof(ENTROPY_CONTEXT_PLANES)-1); michael@0: michael@0: /* Clear entropy contexts for Y2 blocks */ michael@0: if (!x->mode_info_context->mbmi.is_4x4) michael@0: { michael@0: a_ctx[8] = l_ctx[8] = 0; michael@0: } michael@0: } michael@0: michael@0: /* michael@0: ------------------------------------------------------------------------------ michael@0: Residual decoding (Paragraph 13.2 / 13.3) michael@0: */ michael@0: static const uint8_t kBands[16 + 1] = { michael@0: 0, 1, 2, 3, 6, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7, michael@0: 0 /* extra entry as sentinel */ michael@0: }; michael@0: michael@0: static const uint8_t kCat3[] = { 173, 148, 140, 0 }; michael@0: static const uint8_t kCat4[] = { 176, 155, 140, 135, 0 }; michael@0: static const uint8_t kCat5[] = { 180, 157, 141, 134, 130, 0 }; michael@0: static const uint8_t kCat6[] = michael@0: { 254, 254, 243, 230, 196, 177, 153, 140, 133, 130, 129, 0 }; michael@0: static const uint8_t* const kCat3456[] = { kCat3, kCat4, kCat5, kCat6 }; michael@0: static const uint8_t kZigzag[16] = { michael@0: 0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15 michael@0: }; michael@0: michael@0: #define VP8GetBit vp8dx_decode_bool michael@0: #define NUM_PROBAS 11 michael@0: #define NUM_CTX 3 michael@0: michael@0: /* for const-casting */ michael@0: typedef const uint8_t (*ProbaArray)[NUM_CTX][NUM_PROBAS]; michael@0: michael@0: static int GetSigned(BOOL_DECODER *br, int value_to_sign) michael@0: { michael@0: int split = (br->range + 1) >> 1; michael@0: VP8_BD_VALUE bigsplit = (VP8_BD_VALUE)split << (VP8_BD_VALUE_SIZE - 8); michael@0: int v; michael@0: michael@0: if(br->count < 0) michael@0: vp8dx_bool_decoder_fill(br); michael@0: michael@0: if ( br->value < bigsplit ) michael@0: { michael@0: br->range = split; michael@0: v= value_to_sign; michael@0: } michael@0: else michael@0: { michael@0: br->range = br->range-split; michael@0: br->value = br->value-bigsplit; michael@0: v = -value_to_sign; michael@0: } michael@0: br->range +=br->range; michael@0: br->value +=br->value; michael@0: br->count--; michael@0: michael@0: return v; michael@0: } michael@0: /* michael@0: Returns the position of the last non-zero coeff plus one michael@0: (and 0 if there's no coeff at all) michael@0: */ michael@0: static int GetCoeffs(BOOL_DECODER *br, ProbaArray prob, michael@0: int ctx, int n, int16_t* out) michael@0: { michael@0: const uint8_t* p = prob[n][ctx]; michael@0: if (!VP8GetBit(br, p[0])) michael@0: { /* first EOB is more a 'CBP' bit. */ michael@0: return 0; michael@0: } michael@0: while (1) michael@0: { michael@0: ++n; michael@0: if (!VP8GetBit(br, p[1])) michael@0: { michael@0: p = prob[kBands[n]][0]; michael@0: } michael@0: else michael@0: { /* non zero coeff */ michael@0: int v, j; michael@0: if (!VP8GetBit(br, p[2])) michael@0: { michael@0: p = prob[kBands[n]][1]; michael@0: v = 1; michael@0: } michael@0: else michael@0: { michael@0: if (!VP8GetBit(br, p[3])) michael@0: { michael@0: if (!VP8GetBit(br, p[4])) michael@0: { michael@0: v = 2; michael@0: } michael@0: else michael@0: { michael@0: v = 3 + VP8GetBit(br, p[5]); michael@0: } michael@0: } michael@0: else michael@0: { michael@0: if (!VP8GetBit(br, p[6])) michael@0: { michael@0: if (!VP8GetBit(br, p[7])) michael@0: { michael@0: v = 5 + VP8GetBit(br, 159); michael@0: } else michael@0: { michael@0: v = 7 + 2 * VP8GetBit(br, 165); michael@0: v += VP8GetBit(br, 145); michael@0: } michael@0: } michael@0: else michael@0: { michael@0: const uint8_t* tab; michael@0: const int bit1 = VP8GetBit(br, p[8]); michael@0: const int bit0 = VP8GetBit(br, p[9 + bit1]); michael@0: const int cat = 2 * bit1 + bit0; michael@0: v = 0; michael@0: for (tab = kCat3456[cat]; *tab; ++tab) michael@0: { michael@0: v += v + VP8GetBit(br, *tab); michael@0: } michael@0: v += 3 + (8 << cat); michael@0: } michael@0: } michael@0: p = prob[kBands[n]][2]; michael@0: } michael@0: j = kZigzag[n - 1]; michael@0: michael@0: out[j] = GetSigned(br, v); michael@0: michael@0: if (n == 16 || !VP8GetBit(br, p[0])) michael@0: { /* EOB */ michael@0: return n; michael@0: } michael@0: } michael@0: if (n == 16) michael@0: { michael@0: return 16; michael@0: } michael@0: } michael@0: } michael@0: michael@0: int vp8_decode_mb_tokens(VP8D_COMP *dx, MACROBLOCKD *x) michael@0: { michael@0: BOOL_DECODER *bc = x->current_bc; michael@0: const FRAME_CONTEXT * const fc = &dx->common.fc; michael@0: char *eobs = x->eobs; michael@0: michael@0: int i; michael@0: int nonzeros; michael@0: int eobtotal = 0; michael@0: michael@0: short *qcoeff_ptr; michael@0: ProbaArray coef_probs; michael@0: ENTROPY_CONTEXT *a_ctx = ((ENTROPY_CONTEXT *)x->above_context); michael@0: ENTROPY_CONTEXT *l_ctx = ((ENTROPY_CONTEXT *)x->left_context); michael@0: ENTROPY_CONTEXT *a; michael@0: ENTROPY_CONTEXT *l; michael@0: int skip_dc = 0; michael@0: michael@0: qcoeff_ptr = &x->qcoeff[0]; michael@0: michael@0: if (!x->mode_info_context->mbmi.is_4x4) michael@0: { michael@0: a = a_ctx + 8; michael@0: l = l_ctx + 8; michael@0: michael@0: coef_probs = fc->coef_probs [1]; michael@0: michael@0: nonzeros = GetCoeffs(bc, coef_probs, (*a + *l), 0, qcoeff_ptr + 24 * 16); michael@0: *a = *l = (nonzeros > 0); michael@0: michael@0: eobs[24] = nonzeros; michael@0: eobtotal += nonzeros - 16; michael@0: michael@0: coef_probs = fc->coef_probs [0]; michael@0: skip_dc = 1; michael@0: } michael@0: else michael@0: { michael@0: coef_probs = fc->coef_probs [3]; michael@0: skip_dc = 0; michael@0: } michael@0: michael@0: for (i = 0; i < 16; ++i) michael@0: { michael@0: a = a_ctx + (i&3); michael@0: l = l_ctx + ((i&0xc)>>2); michael@0: michael@0: nonzeros = GetCoeffs(bc, coef_probs, (*a + *l), skip_dc, qcoeff_ptr); michael@0: *a = *l = (nonzeros > 0); michael@0: michael@0: nonzeros += skip_dc; michael@0: eobs[i] = nonzeros; michael@0: eobtotal += nonzeros; michael@0: qcoeff_ptr += 16; michael@0: } michael@0: michael@0: coef_probs = fc->coef_probs [2]; michael@0: michael@0: a_ctx += 4; michael@0: l_ctx += 4; michael@0: for (i = 16; i < 24; ++i) michael@0: { michael@0: a = a_ctx + ((i > 19)<<1) + (i&1); michael@0: l = l_ctx + ((i > 19)<<1) + ((i&3)>1); michael@0: michael@0: nonzeros = GetCoeffs(bc, coef_probs, (*a + *l), 0, qcoeff_ptr); michael@0: *a = *l = (nonzeros > 0); michael@0: michael@0: eobs[i] = nonzeros; michael@0: eobtotal += nonzeros; michael@0: qcoeff_ptr += 16; michael@0: } michael@0: michael@0: return eobtotal; michael@0: } michael@0: