1ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang/* 2ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang * Copyright (c) 2010 The WebM project authors. All Rights Reserved. 3ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang * 4ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang * Use of this source code is governed by a BSD-style license 5ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang * that can be found in the LICENSE file in the root of the source 6ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang * tree. An additional intellectual property rights grant can be found 7ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang * in the file PATENTS. All contributing project authors may 8ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang * be found in the AUTHORS file in the root of the source tree. 9ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang */ 10ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 11ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 12ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang#include <math.h> 13ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang#include <stdio.h> 14ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang#include <string.h> 15ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang#include <assert.h> 16ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang#include "vp9/encoder/vp9_onyx_int.h" 17ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang#include "vp9/encoder/vp9_tokenize.h" 18ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang#include "vpx_mem/vpx_mem.h" 19ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 20ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang#include "vp9/common/vp9_pred_common.h" 21ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang#include "vp9/common/vp9_seg_common.h" 22ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang#include "vp9/common/vp9_entropy.h" 23ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 24ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang/* Global event counters used for accumulating statistics across several 25ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang compressions, then generating vp9_context.c = initial stats. */ 26ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 27ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang#ifdef ENTROPY_STATS 28f3bed9137f66ef693bd406e43b17e9a1114f1e14hkuangvp9_coeff_accum context_counters[TX_SIZES][BLOCK_TYPES]; 29f3bed9137f66ef693bd406e43b17e9a1114f1e14hkuangextern vp9_coeff_stats tree_update_hist[TX_SIZES][BLOCK_TYPES]; 30ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang#endif /* ENTROPY_STATS */ 31ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 32ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuangDECLARE_ALIGNED(16, extern const uint8_t, 33ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang vp9_pt_energy_class[MAX_ENTROPY_TOKENS]); 34ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 35ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuangstatic TOKENVALUE dct_value_tokens[DCT_MAX_VALUE * 2]; 36ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuangconst TOKENVALUE *vp9_dct_value_tokens_ptr; 37ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuangstatic int dct_value_cost[DCT_MAX_VALUE * 2]; 38ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuangconst int *vp9_dct_value_cost_ptr; 39ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 40ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuangstatic void fill_value_tokens() { 41ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 42ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang TOKENVALUE *const t = dct_value_tokens + DCT_MAX_VALUE; 43f3bed9137f66ef693bd406e43b17e9a1114f1e14hkuang const vp9_extra_bit *const e = vp9_extra_bits; 44ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 45ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang int i = -DCT_MAX_VALUE; 46ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang int sign = 1; 47ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 48ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang do { 49ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang if (!i) 50ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang sign = 0; 51ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 52ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang { 53ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang const int a = sign ? -i : i; 54ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang int eb = sign; 55ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 56ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang if (a > 4) { 57ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang int j = 4; 58ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 59ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang while (++j < 11 && e[j].base_val <= a) {} 60ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 61ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang t[i].token = --j; 62ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang eb |= (a - e[j].base_val) << 1; 63ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang } else 64ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang t[i].token = a; 65ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 66ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang t[i].extra = eb; 67ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang } 68ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 69ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang // initialize the cost for extra bits for all possible coefficient value. 70ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang { 71ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang int cost = 0; 72f3bed9137f66ef693bd406e43b17e9a1114f1e14hkuang const vp9_extra_bit *p = vp9_extra_bits + t[i].token; 73ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 74ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang if (p->base_val) { 75ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang const int extra = t[i].extra; 76ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang const int length = p->len; 77ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 78ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang if (length) 79ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang cost += treed_cost(p->tree, p->prob, extra >> 1, length); 80ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 81ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang cost += vp9_cost_bit(vp9_prob_half, extra & 1); /* sign */ 82ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang dct_value_cost[i + DCT_MAX_VALUE] = cost; 83ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang } 84ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 85ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang } 86ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 87ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang } while (++i < DCT_MAX_VALUE); 88ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 89ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang vp9_dct_value_tokens_ptr = dct_value_tokens + DCT_MAX_VALUE; 90ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang vp9_dct_value_cost_ptr = dct_value_cost + DCT_MAX_VALUE; 91ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang} 92ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 93ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuangstruct tokenize_b_args { 94ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang VP9_COMP *cpi; 95ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang MACROBLOCKD *xd; 96ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang TOKENEXTRA **tp; 97ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang TX_SIZE tx_size; 98ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang}; 99ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 1001184aebb761cbeac9124c37189a80a1a58f04b6bhkuangstatic void set_entropy_context_b(int plane, int block, BLOCK_SIZE plane_bsize, 1011184aebb761cbeac9124c37189a80a1a58f04b6bhkuang TX_SIZE tx_size, void *arg) { 102f3bed9137f66ef693bd406e43b17e9a1114f1e14hkuang struct tokenize_b_args* const args = arg; 1031184aebb761cbeac9124c37189a80a1a58f04b6bhkuang MACROBLOCKD *const xd = args->xd; 1041184aebb761cbeac9124c37189a80a1a58f04b6bhkuang struct macroblockd_plane *pd = &xd->plane[plane]; 1051184aebb761cbeac9124c37189a80a1a58f04b6bhkuang int aoff, loff; 1061184aebb761cbeac9124c37189a80a1a58f04b6bhkuang txfrm_block_to_raster_xy(plane_bsize, tx_size, block, &aoff, &loff); 1071184aebb761cbeac9124c37189a80a1a58f04b6bhkuang set_contexts(xd, pd, plane_bsize, tx_size, pd->eobs[block] > 0, aoff, loff); 108f3bed9137f66ef693bd406e43b17e9a1114f1e14hkuang} 109f3bed9137f66ef693bd406e43b17e9a1114f1e14hkuang 1101184aebb761cbeac9124c37189a80a1a58f04b6bhkuangstatic void tokenize_b(int plane, int block, BLOCK_SIZE plane_bsize, 1111184aebb761cbeac9124c37189a80a1a58f04b6bhkuang TX_SIZE tx_size, void *arg) { 112ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang struct tokenize_b_args* const args = arg; 113ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang VP9_COMP *cpi = args->cpi; 114ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang MACROBLOCKD *xd = args->xd; 115ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang TOKENEXTRA **tp = args->tp; 1161184aebb761cbeac9124c37189a80a1a58f04b6bhkuang struct macroblockd_plane *pd = &xd->plane[plane]; 1171184aebb761cbeac9124c37189a80a1a58f04b6bhkuang MB_MODE_INFO *mbmi = &xd->this_mi->mbmi; 118ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang int pt; /* near block/prev token context index */ 119ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang int c = 0, rc = 0; 120ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang TOKENEXTRA *t = *tp; /* store tokens starting here */ 1211184aebb761cbeac9124c37189a80a1a58f04b6bhkuang const int eob = pd->eobs[block]; 1221184aebb761cbeac9124c37189a80a1a58f04b6bhkuang const PLANE_TYPE type = pd->plane_type; 1231184aebb761cbeac9124c37189a80a1a58f04b6bhkuang const int16_t *qcoeff_ptr = BLOCK_OFFSET(pd->qcoeff, block); 1241184aebb761cbeac9124c37189a80a1a58f04b6bhkuang 125ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang const int segment_id = mbmi->segment_id; 12691037db265ecdd914a26e056cf69207b4f50924ehkuang const int16_t *scan, *nb; 1271184aebb761cbeac9124c37189a80a1a58f04b6bhkuang vp9_coeff_count *const counts = cpi->coef_counts[tx_size]; 1281184aebb761cbeac9124c37189a80a1a58f04b6bhkuang vp9_coeff_probs_model *const coef_probs = cpi->common.fc.coef_probs[tx_size]; 129f3bed9137f66ef693bd406e43b17e9a1114f1e14hkuang const int ref = is_inter_block(mbmi); 130ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang uint8_t token_cache[1024]; 13191037db265ecdd914a26e056cf69207b4f50924ehkuang const uint8_t *band_translate; 1321184aebb761cbeac9124c37189a80a1a58f04b6bhkuang ENTROPY_CONTEXT *A, *L; 1331184aebb761cbeac9124c37189a80a1a58f04b6bhkuang const int seg_eob = get_tx_eob(&cpi->common.seg, segment_id, tx_size); 1341184aebb761cbeac9124c37189a80a1a58f04b6bhkuang int aoff, loff; 1351184aebb761cbeac9124c37189a80a1a58f04b6bhkuang txfrm_block_to_raster_xy(plane_bsize, tx_size, block, &aoff, &loff); 136ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 1371184aebb761cbeac9124c37189a80a1a58f04b6bhkuang A = pd->above_context + aoff; 1381184aebb761cbeac9124c37189a80a1a58f04b6bhkuang L = pd->left_context + loff; 139ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 1401184aebb761cbeac9124c37189a80a1a58f04b6bhkuang assert((!type && !plane) || (type && plane)); 141ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 1421184aebb761cbeac9124c37189a80a1a58f04b6bhkuang pt = get_entropy_context(xd, tx_size, type, block, A, L, 1431184aebb761cbeac9124c37189a80a1a58f04b6bhkuang &scan, &band_translate); 1441184aebb761cbeac9124c37189a80a1a58f04b6bhkuang nb = vp9_get_coef_neighbors_handle(scan); 145ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang c = 0; 146ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang do { 147ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang const int band = get_coef_band(band_translate, c); 148ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang int token; 149ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang int v = 0; 150ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang rc = scan[c]; 151ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang if (c) 15291037db265ecdd914a26e056cf69207b4f50924ehkuang pt = get_coef_context(nb, token_cache, c); 153ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang if (c < eob) { 154ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang v = qcoeff_ptr[rc]; 155ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang assert(-DCT_MAX_VALUE <= v && v < DCT_MAX_VALUE); 156ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 157ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang t->extra = vp9_dct_value_tokens_ptr[v].extra; 158ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang token = vp9_dct_value_tokens_ptr[v].token; 159ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang } else { 160ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang token = DCT_EOB_TOKEN; 161ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang } 162ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 163ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang t->token = token; 164ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang t->context_tree = coef_probs[type][ref][band][pt]; 165ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang t->skip_eob_node = (c > 0) && (token_cache[scan[c - 1]] == 0); 166ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 167ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang assert(vp9_coef_encodings[t->token].len - t->skip_eob_node > 0); 168ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 169f3bed9137f66ef693bd406e43b17e9a1114f1e14hkuang ++counts[type][ref][band][pt][token]; 170f3bed9137f66ef693bd406e43b17e9a1114f1e14hkuang if (!t->skip_eob_node) 171f3bed9137f66ef693bd406e43b17e9a1114f1e14hkuang ++cpi->common.counts.eob_branch[tx_size][type][ref][band][pt]; 172f3bed9137f66ef693bd406e43b17e9a1114f1e14hkuang 173f3bed9137f66ef693bd406e43b17e9a1114f1e14hkuang token_cache[rc] = vp9_pt_energy_class[token]; 174ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang ++t; 175ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang } while (c < eob && ++c < seg_eob); 176ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 177ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang *tp = t; 1781184aebb761cbeac9124c37189a80a1a58f04b6bhkuang 1791184aebb761cbeac9124c37189a80a1a58f04b6bhkuang set_contexts(xd, pd, plane_bsize, tx_size, c > 0, aoff, loff); 180ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang} 181ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 182ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuangstruct is_skippable_args { 183ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang MACROBLOCKD *xd; 184ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang int *skippable; 185ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang}; 1861184aebb761cbeac9124c37189a80a1a58f04b6bhkuang 187ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuangstatic void is_skippable(int plane, int block, 1881184aebb761cbeac9124c37189a80a1a58f04b6bhkuang BLOCK_SIZE plane_bsize, TX_SIZE tx_size, 1891184aebb761cbeac9124c37189a80a1a58f04b6bhkuang void *argv) { 190ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang struct is_skippable_args *args = argv; 191ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang args->skippable[0] &= (!args->xd->plane[plane].eobs[block]); 192ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang} 193ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 1941184aebb761cbeac9124c37189a80a1a58f04b6bhkuangint vp9_sb_is_skippable(MACROBLOCKD *xd, BLOCK_SIZE bsize) { 195ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang int result = 1; 196ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang struct is_skippable_args args = {xd, &result}; 197ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang foreach_transformed_block(xd, bsize, is_skippable, &args); 198ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang return result; 199ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang} 200ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 2011184aebb761cbeac9124c37189a80a1a58f04b6bhkuangint vp9_is_skippable_in_plane(MACROBLOCKD *xd, BLOCK_SIZE bsize, 2021184aebb761cbeac9124c37189a80a1a58f04b6bhkuang int plane) { 203ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang int result = 1; 204ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang struct is_skippable_args args = {xd, &result}; 2051184aebb761cbeac9124c37189a80a1a58f04b6bhkuang foreach_transformed_block_in_plane(xd, bsize, plane, is_skippable, &args); 206ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang return result; 207ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang} 208ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 20991037db265ecdd914a26e056cf69207b4f50924ehkuangvoid vp9_tokenize_sb(VP9_COMP *cpi, TOKENEXTRA **t, int dry_run, 2101184aebb761cbeac9124c37189a80a1a58f04b6bhkuang BLOCK_SIZE bsize) { 21191037db265ecdd914a26e056cf69207b4f50924ehkuang VP9_COMMON *const cm = &cpi->common; 21291037db265ecdd914a26e056cf69207b4f50924ehkuang MACROBLOCKD *const xd = &cpi->mb.e_mbd; 2131184aebb761cbeac9124c37189a80a1a58f04b6bhkuang MB_MODE_INFO *const mbmi = &xd->this_mi->mbmi; 214ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang TOKENEXTRA *t_backup = *t; 21591037db265ecdd914a26e056cf69207b4f50924ehkuang const int mb_skip_context = vp9_get_pred_context_mbskip(xd); 2161184aebb761cbeac9124c37189a80a1a58f04b6bhkuang const int skip_inc = !vp9_segfeature_active(&cm->seg, mbmi->segment_id, 21791037db265ecdd914a26e056cf69207b4f50924ehkuang SEG_LVL_SKIP); 2181184aebb761cbeac9124c37189a80a1a58f04b6bhkuang struct tokenize_b_args arg = {cpi, xd, t, mbmi->tx_size}; 219ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 2201184aebb761cbeac9124c37189a80a1a58f04b6bhkuang mbmi->skip_coeff = vp9_sb_is_skippable(xd, bsize); 2211184aebb761cbeac9124c37189a80a1a58f04b6bhkuang if (mbmi->skip_coeff) { 222ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang if (!dry_run) 22391037db265ecdd914a26e056cf69207b4f50924ehkuang cm->counts.mbskip[mb_skip_context][1] += skip_inc; 2241184aebb761cbeac9124c37189a80a1a58f04b6bhkuang reset_skip_context(xd, bsize); 225ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang if (dry_run) 226ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang *t = t_backup; 227ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang return; 228ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang } 229ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 230f3bed9137f66ef693bd406e43b17e9a1114f1e14hkuang if (!dry_run) { 23191037db265ecdd914a26e056cf69207b4f50924ehkuang cm->counts.mbskip[mb_skip_context][0] += skip_inc; 232f3bed9137f66ef693bd406e43b17e9a1114f1e14hkuang foreach_transformed_block(xd, bsize, tokenize_b, &arg); 233f3bed9137f66ef693bd406e43b17e9a1114f1e14hkuang } else { 234f3bed9137f66ef693bd406e43b17e9a1114f1e14hkuang foreach_transformed_block(xd, bsize, set_entropy_context_b, &arg); 235ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang *t = t_backup; 236f3bed9137f66ef693bd406e43b17e9a1114f1e14hkuang } 237ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang} 238ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 239ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang#ifdef ENTROPY_STATS 240ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuangvoid init_context_counters(void) { 241ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang FILE *f = fopen("context.bin", "rb"); 242ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang if (!f) { 243ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang vp9_zero(context_counters); 244ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang } else { 245ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang fread(context_counters, sizeof(context_counters), 1, f); 246ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang fclose(f); 247ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang } 248ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 249ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang f = fopen("treeupdate.bin", "rb"); 250ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang if (!f) { 251ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang vpx_memset(tree_update_hist, 0, sizeof(tree_update_hist)); 252ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang } else { 253ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang fread(tree_update_hist, sizeof(tree_update_hist), 1, f); 254ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang fclose(f); 255ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang } 256ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang} 257ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 258ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuangstatic void print_counter(FILE *f, vp9_coeff_accum *context_counters, 259ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang int block_types, const char *header) { 260ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang int type, ref, band, pt, t; 261ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 262ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang fprintf(f, "static const vp9_coeff_count %s = {\n", header); 263ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 264ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang#define Comma(X) (X ? "," : "") 265ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang type = 0; 266ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang do { 267ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang ref = 0; 268ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang fprintf(f, "%s\n { /* block Type %d */", Comma(type), type); 269ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang do { 270ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang fprintf(f, "%s\n { /* %s */", Comma(type), ref ? "Inter" : "Intra"); 271ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang band = 0; 272ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang do { 273ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang fprintf(f, "%s\n { /* Coeff Band %d */", Comma(band), band); 274ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang pt = 0; 275ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang do { 276ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang fprintf(f, "%s\n {", Comma(pt)); 277ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 278ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang t = 0; 279ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang do { 280ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang const int64_t x = context_counters[type][ref][band][pt][t]; 281ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang const int y = (int) x; 282ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 283ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang assert(x == (int64_t) y); /* no overflow handling yet */ 284ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang fprintf(f, "%s %d", Comma(t), y); 285ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang } while (++t < 1 + MAX_ENTROPY_TOKENS); 286ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang fprintf(f, "}"); 287ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang } while (++pt < PREV_COEF_CONTEXTS); 288ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang fprintf(f, "\n }"); 289ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang } while (++band < COEF_BANDS); 290ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang fprintf(f, "\n }"); 291ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang } while (++ref < REF_TYPES); 292ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang fprintf(f, "\n }"); 293ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang } while (++type < block_types); 294ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang fprintf(f, "\n};\n"); 295ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang} 296ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 297ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuangstatic void print_probs(FILE *f, vp9_coeff_accum *context_counters, 298ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang int block_types, const char *header) { 299ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang int type, ref, band, pt, t; 300ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 301ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang fprintf(f, "static const vp9_coeff_probs %s = {", header); 302ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 303ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang type = 0; 304ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang#define Newline(x, spaces) (x ? " " : "\n" spaces) 305ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang do { 306ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang fprintf(f, "%s%s{ /* block Type %d */", 307ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang Comma(type), Newline(type, " "), type); 308ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang ref = 0; 309ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang do { 310ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang fprintf(f, "%s%s{ /* %s */", 311ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang Comma(band), Newline(band, " "), ref ? "Inter" : "Intra"); 312ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang band = 0; 313ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang do { 314ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang fprintf(f, "%s%s{ /* Coeff Band %d */", 315ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang Comma(band), Newline(band, " "), band); 316ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang pt = 0; 317ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang do { 318ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang unsigned int branch_ct[ENTROPY_NODES][2]; 319ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang unsigned int coef_counts[MAX_ENTROPY_TOKENS + 1]; 320ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang vp9_prob coef_probs[ENTROPY_NODES]; 321ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 322ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang if (pt >= 3 && band == 0) 323ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang break; 324ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang for (t = 0; t < MAX_ENTROPY_TOKENS + 1; ++t) 325ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang coef_counts[t] = context_counters[type][ref][band][pt][t]; 326ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang vp9_tree_probs_from_distribution(vp9_coef_tree, coef_probs, 327ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang branch_ct, coef_counts, 0); 328ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang branch_ct[0][1] = coef_counts[MAX_ENTROPY_TOKENS] - branch_ct[0][0]; 329ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang coef_probs[0] = get_binary_prob(branch_ct[0][0], branch_ct[0][1]); 330ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang fprintf(f, "%s\n {", Comma(pt)); 331ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 332ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang t = 0; 333ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang do { 334ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang fprintf(f, "%s %3d", Comma(t), coef_probs[t]); 335ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang } while (++t < ENTROPY_NODES); 336ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 337ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang fprintf(f, " }"); 338ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang } while (++pt < PREV_COEF_CONTEXTS); 339ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang fprintf(f, "\n }"); 340ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang } while (++band < COEF_BANDS); 341ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang fprintf(f, "\n }"); 342ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang } while (++ref < REF_TYPES); 343ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang fprintf(f, "\n }"); 344ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang } while (++type < block_types); 345ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang fprintf(f, "\n};\n"); 346ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang} 347ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 348ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuangvoid print_context_counters() { 349ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang FILE *f = fopen("vp9_context.c", "w"); 350ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 351ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang fprintf(f, "#include \"vp9_entropy.h\"\n"); 352ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang fprintf(f, "\n/* *** GENERATED FILE: DO NOT EDIT *** */\n\n"); 353ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 354ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang /* print counts */ 355ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang print_counter(f, context_counters[TX_4X4], BLOCK_TYPES, 356ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang "vp9_default_coef_counts_4x4[BLOCK_TYPES]"); 357ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang print_counter(f, context_counters[TX_8X8], BLOCK_TYPES, 358ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang "vp9_default_coef_counts_8x8[BLOCK_TYPES]"); 359ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang print_counter(f, context_counters[TX_16X16], BLOCK_TYPES, 360ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang "vp9_default_coef_counts_16x16[BLOCK_TYPES]"); 361ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang print_counter(f, context_counters[TX_32X32], BLOCK_TYPES, 362ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang "vp9_default_coef_counts_32x32[BLOCK_TYPES]"); 363ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 364ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang /* print coefficient probabilities */ 365ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang print_probs(f, context_counters[TX_4X4], BLOCK_TYPES, 366ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang "default_coef_probs_4x4[BLOCK_TYPES]"); 367ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang print_probs(f, context_counters[TX_8X8], BLOCK_TYPES, 368ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang "default_coef_probs_8x8[BLOCK_TYPES]"); 369ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang print_probs(f, context_counters[TX_16X16], BLOCK_TYPES, 370ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang "default_coef_probs_16x16[BLOCK_TYPES]"); 371ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang print_probs(f, context_counters[TX_32X32], BLOCK_TYPES, 372ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang "default_coef_probs_32x32[BLOCK_TYPES]"); 373ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 374ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang fclose(f); 375ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 376ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang f = fopen("context.bin", "wb"); 377ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang fwrite(context_counters, sizeof(context_counters), 1, f); 378ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang fclose(f); 379ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang} 380ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang#endif 381ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang 382ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuangvoid vp9_tokenize_initialize() { 383ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang fill_value_tokens(); 384ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang} 385