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 michael@0: #include michael@0: #include michael@0: #include michael@0: #include "vp9/encoder/vp9_onyx_int.h" michael@0: #include "vp9/encoder/vp9_tokenize.h" michael@0: #include "vpx_mem/vpx_mem.h" michael@0: michael@0: #include "vp9/common/vp9_pred_common.h" michael@0: #include "vp9/common/vp9_seg_common.h" michael@0: #include "vp9/common/vp9_entropy.h" michael@0: michael@0: static TOKENVALUE dct_value_tokens[DCT_MAX_VALUE * 2]; michael@0: const TOKENVALUE *vp9_dct_value_tokens_ptr; michael@0: static int dct_value_cost[DCT_MAX_VALUE * 2]; michael@0: const int *vp9_dct_value_cost_ptr; michael@0: michael@0: static void fill_value_tokens() { michael@0: TOKENVALUE *const t = dct_value_tokens + DCT_MAX_VALUE; michael@0: const vp9_extra_bit *const e = vp9_extra_bits; michael@0: michael@0: int i = -DCT_MAX_VALUE; michael@0: int sign = 1; michael@0: michael@0: do { michael@0: if (!i) michael@0: sign = 0; michael@0: michael@0: { michael@0: const int a = sign ? -i : i; michael@0: int eb = sign; michael@0: michael@0: if (a > 4) { michael@0: int j = 4; michael@0: michael@0: while (++j < 11 && e[j].base_val <= a) {} michael@0: michael@0: t[i].token = --j; michael@0: eb |= (a - e[j].base_val) << 1; michael@0: } else { michael@0: t[i].token = a; michael@0: } michael@0: t[i].extra = eb; michael@0: } michael@0: michael@0: // initialize the cost for extra bits for all possible coefficient value. michael@0: { michael@0: int cost = 0; michael@0: const vp9_extra_bit *p = &vp9_extra_bits[t[i].token]; michael@0: michael@0: if (p->base_val) { michael@0: const int extra = t[i].extra; michael@0: const int length = p->len; michael@0: michael@0: if (length) michael@0: cost += treed_cost(p->tree, p->prob, extra >> 1, length); michael@0: michael@0: cost += vp9_cost_bit(vp9_prob_half, extra & 1); /* sign */ michael@0: dct_value_cost[i + DCT_MAX_VALUE] = cost; michael@0: } michael@0: } michael@0: } while (++i < DCT_MAX_VALUE); michael@0: michael@0: vp9_dct_value_tokens_ptr = dct_value_tokens + DCT_MAX_VALUE; michael@0: vp9_dct_value_cost_ptr = dct_value_cost + DCT_MAX_VALUE; michael@0: } michael@0: michael@0: struct tokenize_b_args { michael@0: VP9_COMP *cpi; michael@0: MACROBLOCKD *xd; michael@0: TOKENEXTRA **tp; michael@0: TX_SIZE tx_size; michael@0: uint8_t *token_cache; michael@0: }; michael@0: michael@0: static void set_entropy_context_b(int plane, int block, BLOCK_SIZE plane_bsize, michael@0: TX_SIZE tx_size, void *arg) { michael@0: struct tokenize_b_args* const args = arg; michael@0: MACROBLOCKD *const xd = args->xd; michael@0: struct macroblockd_plane *pd = &xd->plane[plane]; michael@0: int aoff, loff; michael@0: txfrm_block_to_raster_xy(plane_bsize, tx_size, block, &aoff, &loff); michael@0: set_contexts(xd, pd, plane_bsize, tx_size, pd->eobs[block] > 0, aoff, loff); michael@0: } michael@0: michael@0: static void tokenize_b(int plane, int block, BLOCK_SIZE plane_bsize, michael@0: TX_SIZE tx_size, void *arg) { michael@0: struct tokenize_b_args* const args = arg; michael@0: VP9_COMP *cpi = args->cpi; michael@0: MACROBLOCKD *xd = args->xd; michael@0: TOKENEXTRA **tp = args->tp; michael@0: uint8_t *token_cache = args->token_cache; michael@0: struct macroblockd_plane *pd = &xd->plane[plane]; michael@0: MB_MODE_INFO *mbmi = &xd->mi_8x8[0]->mbmi; michael@0: int pt; /* near block/prev token context index */ michael@0: int c = 0, rc = 0; michael@0: TOKENEXTRA *t = *tp; /* store tokens starting here */ michael@0: const int eob = pd->eobs[block]; michael@0: const PLANE_TYPE type = pd->plane_type; michael@0: const int16_t *qcoeff_ptr = BLOCK_OFFSET(pd->qcoeff, block); michael@0: michael@0: const int segment_id = mbmi->segment_id; michael@0: const int16_t *scan, *nb; michael@0: vp9_coeff_count *const counts = cpi->coef_counts[tx_size]; michael@0: vp9_coeff_probs_model *const coef_probs = cpi->common.fc.coef_probs[tx_size]; michael@0: const int ref = is_inter_block(mbmi); michael@0: const uint8_t *const band_translate = get_band_translate(tx_size); michael@0: const int seg_eob = get_tx_eob(&cpi->common.seg, segment_id, tx_size); michael@0: int aoff, loff; michael@0: txfrm_block_to_raster_xy(plane_bsize, tx_size, block, &aoff, &loff); michael@0: michael@0: assert((!type && !plane) || (type && plane)); michael@0: michael@0: pt = get_entropy_context(tx_size, pd->above_context + aoff, michael@0: pd->left_context + loff); michael@0: get_scan(xd, tx_size, type, block, &scan, &nb); michael@0: c = 0; michael@0: do { michael@0: const int band = band_translate[c]; michael@0: int token; michael@0: int v = 0; michael@0: rc = scan[c]; michael@0: if (c) michael@0: pt = get_coef_context(nb, token_cache, c); michael@0: if (c < eob) { michael@0: v = qcoeff_ptr[rc]; michael@0: assert(-DCT_MAX_VALUE <= v && v < DCT_MAX_VALUE); michael@0: michael@0: t->extra = vp9_dct_value_tokens_ptr[v].extra; michael@0: token = vp9_dct_value_tokens_ptr[v].token; michael@0: } else { michael@0: token = DCT_EOB_TOKEN; michael@0: } michael@0: michael@0: t->token = token; michael@0: t->context_tree = coef_probs[type][ref][band][pt]; michael@0: t->skip_eob_node = (c > 0) && (token_cache[scan[c - 1]] == 0); michael@0: michael@0: assert(vp9_coef_encodings[t->token].len - t->skip_eob_node > 0); michael@0: michael@0: ++counts[type][ref][band][pt][token]; michael@0: if (!t->skip_eob_node) michael@0: ++cpi->common.counts.eob_branch[tx_size][type][ref][band][pt]; michael@0: michael@0: token_cache[rc] = vp9_pt_energy_class[token]; michael@0: ++t; michael@0: } while (c < eob && ++c < seg_eob); michael@0: michael@0: *tp = t; michael@0: michael@0: set_contexts(xd, pd, plane_bsize, tx_size, c > 0, aoff, loff); michael@0: } michael@0: michael@0: struct is_skippable_args { michael@0: MACROBLOCKD *xd; michael@0: int *skippable; michael@0: }; michael@0: michael@0: static void is_skippable(int plane, int block, michael@0: BLOCK_SIZE plane_bsize, TX_SIZE tx_size, michael@0: void *argv) { michael@0: struct is_skippable_args *args = argv; michael@0: args->skippable[0] &= (!args->xd->plane[plane].eobs[block]); michael@0: } michael@0: michael@0: int vp9_sb_is_skippable(MACROBLOCKD *xd, BLOCK_SIZE bsize) { michael@0: int result = 1; michael@0: struct is_skippable_args args = {xd, &result}; michael@0: foreach_transformed_block(xd, bsize, is_skippable, &args); michael@0: return result; michael@0: } michael@0: michael@0: int vp9_is_skippable_in_plane(MACROBLOCKD *xd, BLOCK_SIZE bsize, michael@0: int plane) { michael@0: int result = 1; michael@0: struct is_skippable_args args = {xd, &result}; michael@0: foreach_transformed_block_in_plane(xd, bsize, plane, is_skippable, &args); michael@0: return result; michael@0: } michael@0: michael@0: void vp9_tokenize_sb(VP9_COMP *cpi, TOKENEXTRA **t, int dry_run, michael@0: BLOCK_SIZE bsize) { michael@0: VP9_COMMON *const cm = &cpi->common; michael@0: MACROBLOCKD *const xd = &cpi->mb.e_mbd; michael@0: MB_MODE_INFO *const mbmi = &xd->mi_8x8[0]->mbmi; michael@0: TOKENEXTRA *t_backup = *t; michael@0: const int mb_skip_context = vp9_get_pred_context_mbskip(xd); michael@0: const int skip_inc = !vp9_segfeature_active(&cm->seg, mbmi->segment_id, michael@0: SEG_LVL_SKIP); michael@0: struct tokenize_b_args arg = {cpi, xd, t, mbmi->tx_size, cpi->mb.token_cache}; michael@0: michael@0: mbmi->skip_coeff = vp9_sb_is_skippable(xd, bsize); michael@0: if (mbmi->skip_coeff) { michael@0: if (!dry_run) michael@0: cm->counts.mbskip[mb_skip_context][1] += skip_inc; michael@0: reset_skip_context(xd, bsize); michael@0: if (dry_run) michael@0: *t = t_backup; michael@0: return; michael@0: } michael@0: michael@0: if (!dry_run) { michael@0: cm->counts.mbskip[mb_skip_context][0] += skip_inc; michael@0: foreach_transformed_block(xd, bsize, tokenize_b, &arg); michael@0: } else { michael@0: foreach_transformed_block(xd, bsize, set_entropy_context_b, &arg); michael@0: *t = t_backup; michael@0: } michael@0: } michael@0: michael@0: void vp9_tokenize_initialize() { michael@0: fill_value_tokens(); michael@0: }