bitstream.c 89.74 KiB
/*
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */
#include "vp8/common/header.h"
#include "encodemv.h"
#include "vp8/common/entropymode.h"
#include "vp8/common/findnearmv.h"
#include "mcomp.h"
#include "vp8/common/systemdependent.h"
#include <assert.h>
#include <stdio.h>
#include <limits.h>
#include "vp8/common/pragmas.h"
#include "vpx/vpx_encoder.h"
#include "vpx_mem/vpx_mem.h"
#include "bitstream.h"
#include "segmentation.h"
#include "vp8/common/seg_common.h"
#include "vp8/common/pred_common.h"
#include "vp8/common/entropy.h"
#include "vp8/encoder/encodemv.h"
#if CONFIG_NEWBESTREFMV
#include "vp8/common/mvref_common.h"
#endif
#if defined(SECTIONBITS_OUTPUT)
unsigned __int64 Sectionbits[500];
#endif
//int final_packing = 0;
#ifdef ENTROPY_STATS
int intra_mode_stats [VP8_BINTRAMODES] [VP8_BINTRAMODES] [VP8_BINTRAMODES];
unsigned int tree_update_hist [BLOCK_TYPES]
                              [COEF_BANDS]
                              [PREV_COEF_CONTEXTS]
                              [ENTROPY_NODES][2];
#if CONFIG_HYBRIDTRANSFORM
unsigned int hybrid_tree_update_hist [BLOCK_TYPES]
                                     [COEF_BANDS]
                                     [PREV_COEF_CONTEXTS]
                                     [ENTROPY_NODES][2];
#endif
unsigned int tree_update_hist_8x8 [BLOCK_TYPES_8X8]
                                  [COEF_BANDS]
                                  [PREV_COEF_CONTEXTS]
                                  [ENTROPY_NODES] [2];
#if CONFIG_HYBRIDTRANSFORM8X8
unsigned int hybrid_tree_update_hist_8x8 [BLOCK_TYPES_8X8]
                                         [COEF_BANDS]
                                         [PREV_COEF_CONTEXTS]
                                         [ENTROPY_NODES] [2];
#endif
#if CONFIG_TX16X16
unsigned int tree_update_hist_16x16 [BLOCK_TYPES_16X16]
                                    [COEF_BANDS]
                                    [PREV_COEF_CONTEXTS]
                                    [ENTROPY_NODES] [2];
#if CONFIG_HYBRIDTRANSFORM16X16
unsigned int hybrid_tree_update_hist_16x16 [BLOCK_TYPES_16X16]
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
[COEF_BANDS] [PREV_COEF_CONTEXTS] [ENTROPY_NODES] [2]; #endif #endif extern unsigned int active_section; #endif #ifdef MODE_STATS int count_mb_seg[4] = { 0, 0, 0, 0 }; #endif #define vp8_cost_upd ((int)(vp8_cost_one(upd) - vp8_cost_zero(upd)) >> 8) #define vp8_cost_upd256 ((int)(vp8_cost_one(upd) - vp8_cost_zero(upd))) #define SEARCH_NEWP static int update_bits[255]; static void compute_update_table() { int i; for (i = 0; i < 255; i++) update_bits[i] = vp8_count_term_subexp(i, SUBEXP_PARAM, 255); } static int split_index(int i, int n, int modulus) { int max1 = (n - 1 - modulus / 2) / modulus + 1; if (i % modulus == modulus / 2) i = i / modulus; else i = max1 + i - (i + modulus - modulus / 2) / modulus; return i; } static int remap_prob(int v, int m) { const int n = 256; const int modulus = MODULUS_PARAM; int i; if ((m << 1) <= n) i = recenter_nonneg(v, m) - 1; else i = recenter_nonneg(n - 1 - v, n - 1 - m) - 1; i = split_index(i, n - 1, modulus); return i; } static void write_prob_diff_update(vp8_writer *const w, vp8_prob newp, vp8_prob oldp) { int delp = remap_prob(newp, oldp); vp8_encode_term_subexp(w, delp, SUBEXP_PARAM, 255); } static int prob_diff_update_cost(vp8_prob newp, vp8_prob oldp) { int delp = remap_prob(newp, oldp); return update_bits[delp] * 256; } #if CONFIG_NEW_MVREF // Estimate the cost of each coding the vector using each reference candidate unsigned int pick_best_mv_ref( MACROBLOCK *x, int_mv target_mv, int_mv * mv_ref_list, int_mv * best_ref ) { int i; int best_index = 0; int cost, cost2; int index_cost[MAX_MV_REFS]; MACROBLOCKD *xd = &x->e_mbd; /*unsigned int distance, distance2;
141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
distance = mv_distance(&target_mv, &mv_ref_list[0]); for (i = 1; i < MAX_MV_REFS; ++i ) { distance2 = mv_distance(&target_mv, &mv_ref_list[i]); if (distance2 < distance) { distance = distance2; best_index = i; } }*/ // For now estimate the cost of selecting a given ref index // as index * 1 bits (but here 1 bit is scaled to 256) for (i = 0; i < MAX_MV_REFS; ++i ) { index_cost[i] = i << 8; } index_cost[0] = vp8_cost_zero(205); index_cost[1] = vp8_cost_zero(40); index_cost[2] = vp8_cost_zero(8); index_cost[3] = vp8_cost_zero(2); cost = index_cost[0] + vp8_mv_bit_cost(&target_mv, &mv_ref_list[0], XMVCOST, 96, xd->allow_high_precision_mv); //for (i = 1; i < MAX_MV_REFS; ++i ) { for (i = 1; i < 4; ++i ) { cost2 = index_cost[i] + vp8_mv_bit_cost(&target_mv, &mv_ref_list[i], XMVCOST, 96, xd->allow_high_precision_mv); if (cost2 < cost) { cost = cost2; best_index = i; } } (*best_ref).as_int = mv_ref_list[best_index].as_int; return best_index; } #endif static void update_mode( vp8_writer *const w, int n, vp8_token tok [/* n */], vp8_tree tree, vp8_prob Pnew [/* n-1 */], vp8_prob Pcur [/* n-1 */], unsigned int bct [/* n-1 */] [2], const unsigned int num_events[/* n */] ) { unsigned int new_b = 0, old_b = 0; int i = 0; vp8_tree_probs_from_distribution( n--, tok, tree, Pnew, bct, num_events, 256, 1 ); do { new_b += vp8_cost_branch(bct[i], Pnew[i]);
211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
old_b += vp8_cost_branch(bct[i], Pcur[i]); } while (++i < n); if (new_b + (n << 8) < old_b) { int i = 0; vp8_write_bit(w, 1); do { const vp8_prob p = Pnew[i]; vp8_write_literal(w, Pcur[i] = p ? p : 1, 8); } while (++i < n); } else vp8_write_bit(w, 0); } static void update_mbintra_mode_probs(VP8_COMP *cpi) { VP8_COMMON *const cm = & cpi->common; vp8_writer *const w = & cpi->bc; { vp8_prob Pnew [VP8_YMODES - 1]; unsigned int bct [VP8_YMODES - 1] [2]; update_mode( w, VP8_YMODES, vp8_ymode_encodings, vp8_ymode_tree, Pnew, cm->fc.ymode_prob, bct, (unsigned int *)cpi->ymode_count ); } } void update_skip_probs(VP8_COMP *cpi) { VP8_COMMON *const pc = & cpi->common; int prob_skip_false[3] = {0, 0, 0}; int k; for (k = 0; k < MBSKIP_CONTEXTS; ++k) { if ((cpi->skip_false_count[k] + cpi->skip_true_count[k])) { prob_skip_false[k] = cpi->skip_false_count[k] * 256 / (cpi->skip_false_count[k] + cpi->skip_true_count[k]); if (prob_skip_false[k] <= 1) prob_skip_false[k] = 1; if (prob_skip_false[k] > 255) prob_skip_false[k] = 255; } else prob_skip_false[k] = 128; pc->mbskip_pred_probs[k] = prob_skip_false[k]; } } #if CONFIG_SWITCHABLE_INTERP void update_switchable_interp_probs(VP8_COMP *cpi) { VP8_COMMON *const pc = & cpi->common; vp8_writer *const w = & cpi->bc; unsigned int branch_ct[32][2]; int i, j; for (j = 0; j <= VP8_SWITCHABLE_FILTERS; ++j) { //for (j = 0; j <= 0; ++j) { /* if (!cpi->dummy_packing) #if VP8_SWITCHABLE_FILTERS == 3 printf("HELLO %d %d %d\n", cpi->switchable_interp_count[j][0], cpi->switchable_interp_count[j][1], cpi->switchable_interp_count[j][2]); #else
281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
printf("HELLO %d %d\n", cpi->switchable_interp_count[j][0], cpi->switchable_interp_count[j][1]); #endif */ vp8_tree_probs_from_distribution( VP8_SWITCHABLE_FILTERS, vp8_switchable_interp_encodings, vp8_switchable_interp_tree, pc->fc.switchable_interp_prob[j], branch_ct, cpi->switchable_interp_count[j], 256, 1 ); for (i = 0; i < VP8_SWITCHABLE_FILTERS - 1; ++i) { if (pc->fc.switchable_interp_prob[j][i] < 1) pc->fc.switchable_interp_prob[j][i] = 1; vp8_write_literal(w, pc->fc.switchable_interp_prob[j][i], 8); /* if (!cpi->dummy_packing) #if VP8_SWITCHABLE_FILTERS == 3 printf("Probs %d %d [%d]\n", pc->fc.switchable_interp_prob[j][0], pc->fc.switchable_interp_prob[j][1], pc->frame_type); #else printf("Probs %d [%d]\n", pc->fc.switchable_interp_prob[j][0], pc->frame_type); #endif */ } } /* if (!cpi->dummy_packing) #if VP8_SWITCHABLE_FILTERS == 3 printf("Probs %d %d [%d]\n", pc->fc.switchable_interp_prob[0], pc->fc.switchable_interp_prob[1], pc->frame_type); #else printf("Probs %d [%d]\n", pc->fc.switchable_interp_prob[0], pc->frame_type); #endif */ } #endif // This function updates the reference frame prediction stats static void update_refpred_stats(VP8_COMP *cpi) { VP8_COMMON *const cm = & cpi->common; int i; int tot_count; vp8_prob new_pred_probs[PREDICTION_PROBS]; int old_cost, new_cost; // Set the prediction probability structures to defaults if (cm->frame_type == KEY_FRAME) { // Set the prediction probabilities to defaults cm->ref_pred_probs[0] = 120; cm->ref_pred_probs[1] = 80; cm->ref_pred_probs[2] = 40; vpx_memset(cpi->ref_pred_probs_update, 0, sizeof(cpi->ref_pred_probs_update)); } else { // From the prediction counts set the probabilities for each context for (i = 0; i < PREDICTION_PROBS; i++) { tot_count = cpi->ref_pred_count[i][0] + cpi->ref_pred_count[i][1]; if (tot_count) { new_pred_probs[i] = (cpi->ref_pred_count[i][0] * 255 + (tot_count >> 1)) / tot_count; // Clamp to minimum allowed value new_pred_probs[i] += !new_pred_probs[i]; } else new_pred_probs[i] = 128; // Decide whether or not to update the reference frame probs.
351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
// Returned costs are in 1/256 bit units. old_cost = (cpi->ref_pred_count[i][0] * vp8_cost_zero(cm->ref_pred_probs[i])) + (cpi->ref_pred_count[i][1] * vp8_cost_one(cm->ref_pred_probs[i])); new_cost = (cpi->ref_pred_count[i][0] * vp8_cost_zero(new_pred_probs[i])) + (cpi->ref_pred_count[i][1] * vp8_cost_one(new_pred_probs[i])); // Cost saving must be >= 8 bits (2048 in these units) if ((old_cost - new_cost) >= 2048) { cpi->ref_pred_probs_update[i] = 1; cm->ref_pred_probs[i] = new_pred_probs[i]; } else cpi->ref_pred_probs_update[i] = 0; } } } static void write_ymode(vp8_writer *bc, int m, const vp8_prob *p) { vp8_write_token(bc, vp8_ymode_tree, p, vp8_ymode_encodings + m); } static void kfwrite_ymode(vp8_writer *bc, int m, const vp8_prob *p) { vp8_write_token(bc, vp8_kf_ymode_tree, p, vp8_kf_ymode_encodings + m); } #if CONFIG_SUPERBLOCKS static void sb_kfwrite_ymode(vp8_writer *bc, int m, const vp8_prob *p) { vp8_write_token(bc, vp8_uv_mode_tree, p, vp8_sb_kf_ymode_encodings + m); } #endif static void write_i8x8_mode(vp8_writer *bc, int m, const vp8_prob *p) { vp8_write_token(bc, vp8_i8x8_mode_tree, p, vp8_i8x8_mode_encodings + m); } static void write_uv_mode(vp8_writer *bc, int m, const vp8_prob *p) { vp8_write_token(bc, vp8_uv_mode_tree, p, vp8_uv_mode_encodings + m); } static void write_bmode(vp8_writer *bc, int m, const vp8_prob *p) { vp8_write_token(bc, vp8_bmode_tree, p, vp8_bmode_encodings + m); } static void write_split(vp8_writer *bc, int x, const vp8_prob *p) { vp8_write_token( bc, vp8_mbsplit_tree, p, vp8_mbsplit_encodings + x ); } static int prob_update_savings(const unsigned int *ct, const vp8_prob oldp, const vp8_prob newp, const vp8_prob upd) { const int old_b = vp8_cost_branch256(ct, oldp); const int new_b = vp8_cost_branch256(ct, newp); const int update_b = 2048 + vp8_cost_upd256; return (old_b - new_b - update_b); } static int prob_diff_update_savings(const unsigned int *ct, const vp8_prob oldp, const vp8_prob newp, const vp8_prob upd) { const int old_b = vp8_cost_branch256(ct, oldp); const int new_b = vp8_cost_branch256(ct, newp); const int update_b = (newp == oldp ? 0 : prob_diff_update_cost(newp, oldp) + vp8_cost_upd256); return (old_b - new_b - update_b);
421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
} static int prob_diff_update_savings_search(const unsigned int *ct, const vp8_prob oldp, vp8_prob *bestp, const vp8_prob upd) { const int old_b = vp8_cost_branch256(ct, oldp); int new_b, update_b, savings, bestsavings, step; vp8_prob newp, bestnewp; bestsavings = 0; bestnewp = oldp; step = (*bestp > oldp ? -1 : 1); for (newp = *bestp; newp != oldp; newp += step) { new_b = vp8_cost_branch256(ct, newp); update_b = prob_diff_update_cost(newp, oldp) + vp8_cost_upd256; savings = old_b - new_b - update_b; if (savings > bestsavings) { bestsavings = savings; bestnewp = newp; } } *bestp = bestnewp; return bestsavings; } static void pack_tokens_c(vp8_writer *w, const TOKENEXTRA *p, int xcount) { const TOKENEXTRA *const stop = p + xcount; unsigned int split; unsigned int shift; int count = w->count; unsigned int range = w->range; unsigned int lowvalue = w->lowvalue; while (p < stop) { const int t = p->Token; vp8_token *const a = vp8_coef_encodings + t; const vp8_extra_bit_struct *const b = vp8_extra_bits + t; int i = 0; const unsigned char *pp = p->context_tree; int v = a->value; int n = a->Len; /* skip one or two nodes */ if (p->skip_eob_node) { n -= p->skip_eob_node; i = 2 * p->skip_eob_node; } do { const int bb = (v >> --n) & 1; split = 1 + (((range - 1) * pp[i >> 1]) >> 8); i = vp8_coef_tree[i + bb]; if (bb) { lowvalue += split; range = range - split; } else { range = split; } shift = vp8_norm[range]; range <<= shift; count += shift; if (count >= 0) { int offset = shift - count; if ((lowvalue << (offset - 1)) & 0x80000000) { int x = w->pos - 1;
491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
while (x >= 0 && w->buffer[x] == 0xff) { w->buffer[x] = (unsigned char)0; x--; } w->buffer[x] += 1; } w->buffer[w->pos++] = (lowvalue >> (24 - offset)); lowvalue <<= offset; shift = count; lowvalue &= 0xffffff; count -= 8; } lowvalue <<= shift; } while (n); if (b->base_val) { const int e = p->Extra, L = b->Len; if (L) { const unsigned char *pp = b->prob; int v = e >> 1; int n = L; /* number of bits in v, assumed nonzero */ int i = 0; do { const int bb = (v >> --n) & 1; split = 1 + (((range - 1) * pp[i >> 1]) >> 8); i = b->tree[i + bb]; if (bb) { lowvalue += split; range = range - split; } else { range = split; } shift = vp8_norm[range]; range <<= shift; count += shift; if (count >= 0) { int offset = shift - count; if ((lowvalue << (offset - 1)) & 0x80000000) { int x = w->pos - 1; while (x >= 0 && w->buffer[x] == 0xff) { w->buffer[x] = (unsigned char)0; x--; } w->buffer[x] += 1; } w->buffer[w->pos++] = (lowvalue >> (24 - offset)); lowvalue <<= offset; shift = count; lowvalue &= 0xffffff; count -= 8; } lowvalue <<= shift; } while (n); }
561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
{ split = (range + 1) >> 1; if (e & 1) { lowvalue += split; range = range - split; } else { range = split; } range <<= 1; if ((lowvalue & 0x80000000)) { int x = w->pos - 1; while (x >= 0 && w->buffer[x] == 0xff) { w->buffer[x] = (unsigned char)0; x--; } w->buffer[x] += 1; } lowvalue <<= 1; if (!++count) { count = -8; w->buffer[w->pos++] = (lowvalue >> 24); lowvalue &= 0xffffff; } } } ++p; } w->count = count; w->lowvalue = lowvalue; w->range = range; } static void write_partition_size(unsigned char *cx_data, int size) { signed char csize; csize = size & 0xff; *cx_data = csize; csize = (size >> 8) & 0xff; *(cx_data + 1) = csize; csize = (size >> 16) & 0xff; *(cx_data + 2) = csize; } static void write_mv_ref ( vp8_writer *w, MB_PREDICTION_MODE m, const vp8_prob *p ) { #if CONFIG_DEBUG assert(NEARESTMV <= m && m <= SPLITMV); #endif vp8_write_token(w, vp8_mv_ref_tree, p, vp8_mv_ref_encoding_array - NEARESTMV + m); } #if CONFIG_SUPERBLOCKS
631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
static void write_sb_mv_ref(vp8_writer *w, MB_PREDICTION_MODE m, const vp8_prob *p) { #if CONFIG_DEBUG assert(NEARESTMV <= m && m < SPLITMV); #endif vp8_write_token(w, vp8_sb_mv_ref_tree, p, vp8_sb_mv_ref_encoding_array - NEARESTMV + m); } #endif static void write_sub_mv_ref ( vp8_writer *w, B_PREDICTION_MODE m, const vp8_prob *p ) { #if CONFIG_DEBUG assert(LEFT4X4 <= m && m <= NEW4X4); #endif vp8_write_token(w, vp8_sub_mv_ref_tree, p, vp8_sub_mv_ref_encoding_array - LEFT4X4 + m); } #if CONFIG_NEWMVENTROPY static void write_nmv (vp8_writer *w, const MV *mv, const int_mv *ref, const nmv_context *nmvc, int usehp) { MV e; e.row = mv->row - ref->as_mv.row; e.col = mv->col - ref->as_mv.col; vp8_encode_nmv(w, &e, &ref->as_mv, nmvc); vp8_encode_nmv_fp(w, &e, &ref->as_mv, nmvc, usehp); } #else static void write_mv ( vp8_writer *w, const MV *mv, const int_mv *ref, const MV_CONTEXT *mvc ) { MV e; e.row = mv->row - ref->as_mv.row; e.col = mv->col - ref->as_mv.col; vp8_encode_motion_vector(w, &e, mvc); } static void write_mv_hp ( vp8_writer *w, const MV *mv, const int_mv *ref, const MV_CONTEXT_HP *mvc ) { MV e; e.row = mv->row - ref->as_mv.row; e.col = mv->col - ref->as_mv.col; vp8_encode_motion_vector_hp(w, &e, mvc); } #endif /* CONFIG_NEWMVENTROPY */ // This function writes the current macro block's segnment id to the bitstream // It should only be called if a segment map update is indicated. static void write_mb_segid(vp8_writer *w, const MB_MODE_INFO *mi, const MACROBLOCKD *xd) { // Encode the MB segment id. if (xd->segmentation_enabled && xd->update_mb_segmentation_map) { switch (mi->segment_id) { case 0: vp8_write(w, 0, xd->mb_segment_tree_probs[0]); vp8_write(w, 0, xd->mb_segment_tree_probs[1]); break; case 1: vp8_write(w, 0, xd->mb_segment_tree_probs[0]); vp8_write(w, 1, xd->mb_segment_tree_probs[1]);
701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
break; case 2: vp8_write(w, 1, xd->mb_segment_tree_probs[0]); vp8_write(w, 0, xd->mb_segment_tree_probs[2]); break; case 3: vp8_write(w, 1, xd->mb_segment_tree_probs[0]); vp8_write(w, 1, xd->mb_segment_tree_probs[2]); break; // TRAP.. This should not happen default: vp8_write(w, 0, xd->mb_segment_tree_probs[0]); vp8_write(w, 0, xd->mb_segment_tree_probs[1]); break; } } } // This function encodes the reference frame static void encode_ref_frame(vp8_writer *const w, VP8_COMMON *const cm, MACROBLOCKD *xd, int segment_id, MV_REFERENCE_FRAME rf) { int seg_ref_active; int seg_ref_count = 0; seg_ref_active = segfeature_active(xd, segment_id, SEG_LVL_REF_FRAME); if (seg_ref_active) { seg_ref_count = check_segref(xd, segment_id, INTRA_FRAME) + check_segref(xd, segment_id, LAST_FRAME) + check_segref(xd, segment_id, GOLDEN_FRAME) + check_segref(xd, segment_id, ALTREF_FRAME); } // If segment level coding of this signal is disabled... // or the segment allows multiple reference frame options if (!seg_ref_active || (seg_ref_count > 1)) { // Values used in prediction model coding unsigned char prediction_flag; vp8_prob pred_prob; MV_REFERENCE_FRAME pred_rf; // Get the context probability the prediction flag pred_prob = get_pred_prob(cm, xd, PRED_REF); // Get the predicted value. pred_rf = get_pred_ref(cm, xd); // Did the chosen reference frame match its predicted value. prediction_flag = (xd->mode_info_context->mbmi.ref_frame == pred_rf); set_pred_flag(xd, PRED_REF, prediction_flag); vp8_write(w, prediction_flag, pred_prob); // If not predicted correctly then code value explicitly if (!prediction_flag) { vp8_prob mod_refprobs[PREDICTION_PROBS]; vpx_memcpy(mod_refprobs, cm->mod_refprobs[pred_rf], sizeof(mod_refprobs)); // If segment coding enabled blank out options that cant occur by // setting the branch probability to 0. if (seg_ref_active) { mod_refprobs[INTRA_FRAME] *=
771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
check_segref(xd, segment_id, INTRA_FRAME); mod_refprobs[LAST_FRAME] *= check_segref(xd, segment_id, LAST_FRAME); mod_refprobs[GOLDEN_FRAME] *= (check_segref(xd, segment_id, GOLDEN_FRAME) * check_segref(xd, segment_id, ALTREF_FRAME)); } if (mod_refprobs[0]) { vp8_write(w, (rf != INTRA_FRAME), mod_refprobs[0]); } // Inter coded if (rf != INTRA_FRAME) { if (mod_refprobs[1]) { vp8_write(w, (rf != LAST_FRAME), mod_refprobs[1]); } if (rf != LAST_FRAME) { if (mod_refprobs[2]) { vp8_write(w, (rf != GOLDEN_FRAME), mod_refprobs[2]); } } } } } // if using the prediction mdoel we have nothing further to do because // the reference frame is fully coded by the segment } // Update the probabilities used to encode reference frame data static void update_ref_probs(VP8_COMP *const cpi) { VP8_COMMON *const cm = & cpi->common; const int *const rfct = cpi->count_mb_ref_frame_usage; const int rf_intra = rfct[INTRA_FRAME]; const int rf_inter = rfct[LAST_FRAME] + rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME]; cm->prob_intra_coded = (rf_intra + rf_inter) ? rf_intra * 255 / (rf_intra + rf_inter) : 1; if (!cm->prob_intra_coded) cm->prob_intra_coded = 1; cm->prob_last_coded = rf_inter ? (rfct[LAST_FRAME] * 255) / rf_inter : 128; if (!cm->prob_last_coded) cm->prob_last_coded = 1; cm->prob_gf_coded = (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME]) ? (rfct[GOLDEN_FRAME] * 255) / (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME]) : 128; if (!cm->prob_gf_coded) cm->prob_gf_coded = 1; // Compute a modified set of probabilities to use when prediction of the // reference frame fails compute_mod_refprobs(cm); } static void pack_inter_mode_mvs(VP8_COMP *const cpi) { int i; VP8_COMMON *const pc = & cpi->common; vp8_writer *const w = & cpi->bc; #if CONFIG_NEWMVENTROPY const nmv_context *nmvc = &pc->fc.nmvc; #else
841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
const MV_CONTEXT *mvc = pc->fc.mvc; const MV_CONTEXT_HP *mvc_hp = pc->fc.mvc_hp; #endif MACROBLOCK *x = &cpi->mb; MACROBLOCKD *xd = &cpi->mb.e_mbd; MODE_INFO *m; MODE_INFO *prev_m; const int mis = pc->mode_info_stride; int mb_row, mb_col; int row, col; // Values used in prediction model coding vp8_prob pred_prob; unsigned char prediction_flag; int row_delta[4] = { 0, +1, 0, -1}; int col_delta[4] = { +1, -1, +1, +1}; //final_packing = !cpi->dummy_packing; cpi->mb.partition_info = cpi->mb.pi; // Update the probabilities used to encode reference frame data update_ref_probs(cpi); #ifdef ENTROPY_STATS active_section = 1; #endif if (pc->mb_no_coeff_skip) { int k; update_skip_probs(cpi); for (k = 0; k < MBSKIP_CONTEXTS; ++k) vp8_write_literal(w, pc->mbskip_pred_probs[k], 8); } #if CONFIG_PRED_FILTER // Write the prediction filter mode used for this frame vp8_write_literal(w, pc->pred_filter_mode, 2); // Write prediction filter on/off probability if signaling at MB level if (pc->pred_filter_mode == 2) vp8_write_literal(w, pc->prob_pred_filter_off, 8); // printf("pred_filter_mode:%d prob_pred_filter_off:%d\n", // pc->pred_filter_mode, pc->prob_pred_filter_off); #endif #if CONFIG_SWITCHABLE_INTERP if (pc->mcomp_filter_type == SWITCHABLE) update_switchable_interp_probs(cpi); #endif vp8_write_literal(w, pc->prob_intra_coded, 8); vp8_write_literal(w, pc->prob_last_coded, 8); vp8_write_literal(w, pc->prob_gf_coded, 8); if (cpi->common.comp_pred_mode == HYBRID_PREDICTION) { vp8_write(w, 1, 128); vp8_write(w, 1, 128); for (i = 0; i < COMP_PRED_CONTEXTS; i++) { if (cpi->single_pred_count[i] + cpi->comp_pred_count[i]) { pc->prob_comppred[i] = cpi->single_pred_count[i] * 255 / (cpi->single_pred_count[i] + cpi->comp_pred_count[i]); if (pc->prob_comppred[i] < 1) pc->prob_comppred[i] = 1; } else { pc->prob_comppred[i] = 128; }
911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
vp8_write_literal(w, pc->prob_comppred[i], 8); } } else if (cpi->common.comp_pred_mode == SINGLE_PREDICTION_ONLY) { vp8_write(w, 0, 128); } else { /* compound prediction only */ vp8_write(w, 1, 128); vp8_write(w, 0, 128); } update_mbintra_mode_probs(cpi); #if CONFIG_NEWMVENTROPY vp8_write_nmvprobs(cpi, xd->allow_high_precision_mv); #else if (xd->allow_high_precision_mv) vp8_write_mvprobs_hp(cpi); else vp8_write_mvprobs(cpi); #endif mb_row = 0; for (row = 0; row < pc->mb_rows; row += 2) { m = pc->mi + row * mis; prev_m = pc->prev_mi + row * mis; mb_col = 0; for (col = 0; col < pc->mb_cols; col += 2) { int i; // Process the 4 MBs in the order: // top-left, top-right, bottom-left, bottom-right #if CONFIG_SUPERBLOCKS vp8_write(w, m->mbmi.encoded_as_sb, pc->sb_coded); #endif for (i = 0; i < 4; i++) { MB_MODE_INFO *mi; MV_REFERENCE_FRAME rf; MB_PREDICTION_MODE mode; int segment_id; int dy = row_delta[i]; int dx = col_delta[i]; int offset_extended = dy * mis + dx; if ((mb_row >= pc->mb_rows) || (mb_col >= pc->mb_cols)) { // MB lies outside frame, move on mb_row += dy; mb_col += dx; m += offset_extended; prev_m += offset_extended; cpi->mb.partition_info += offset_extended; continue; } mi = & m->mbmi; rf = mi->ref_frame; mode = mi->mode; segment_id = mi->segment_id; // Distance of Mb to the various image edges. // These specified to 8th pel as they are always compared to MV // values that are in 1/8th pel units xd->mb_to_left_edge = -((mb_col * 16) << 3); xd->mb_to_right_edge = ((pc->mb_cols - 1 - mb_col) * 16) << 3; xd->mb_to_top_edge = -((mb_row * 16)) << 3; xd->mb_to_bottom_edge = ((pc->mb_rows - 1 - mb_row) * 16) << 3; // Make sure the MacroBlockD mode info pointer is set correctly xd->mode_info_context = m; xd->prev_mode_info_context = prev_m;
981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
#ifdef ENTROPY_STATS active_section = 9; #endif if (cpi->mb.e_mbd.update_mb_segmentation_map) { // Is temporal coding of the segment map enabled if (pc->temporal_update) { prediction_flag = get_pred_flag(xd, PRED_SEG_ID); pred_prob = get_pred_prob(pc, xd, PRED_SEG_ID); // Code the segment id prediction flag for this mb vp8_write(w, prediction_flag, pred_prob); // If the mb segment id wasn't predicted code explicitly if (!prediction_flag) write_mb_segid(w, mi, &cpi->mb.e_mbd); } else { // Normal unpredicted coding write_mb_segid(w, mi, &cpi->mb.e_mbd); } } if (pc->mb_no_coeff_skip && (!segfeature_active(xd, segment_id, SEG_LVL_EOB) || (get_segdata(xd, segment_id, SEG_LVL_EOB) != 0))) { int skip_coeff = mi->mb_skip_coeff; #if CONFIG_SUPERBLOCKS if (mi->encoded_as_sb) { skip_coeff &= m[1].mbmi.mb_skip_coeff; skip_coeff &= m[mis].mbmi.mb_skip_coeff; skip_coeff &= m[mis + 1].mbmi.mb_skip_coeff; } #endif vp8_encode_bool(w, skip_coeff, get_pred_prob(pc, xd, PRED_MBSKIP)); } // Encode the reference frame. encode_ref_frame(w, pc, xd, segment_id, rf); if (rf == INTRA_FRAME) { #ifdef ENTROPY_STATS active_section = 6; #endif // TODO(rbultje) write using SB tree structure if (!segfeature_active(xd, segment_id, SEG_LVL_MODE)) { write_ymode(w, mode, pc->fc.ymode_prob); } if (mode == B_PRED) { int j = 0; #if CONFIG_COMP_INTRA_PRED int uses_second = m->bmi[0].as_mode.second != (B_PREDICTION_MODE)(B_DC_PRED - 1); vp8_write(w, uses_second, 128); #endif do { #if CONFIG_COMP_INTRA_PRED B_PREDICTION_MODE mode2 = m->bmi[j].as_mode.second; #endif write_bmode(w, m->bmi[j].as_mode.first, pc->fc.bmode_prob); #if CONFIG_COMP_INTRA_PRED if (uses_second) { write_bmode(w, mode2, pc->fc.bmode_prob); }
1051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
#endif } while (++j < 16); } if (mode == I8X8_PRED) { write_i8x8_mode(w, m->bmi[0].as_mode.first, pc->fc.i8x8_mode_prob); write_i8x8_mode(w, m->bmi[2].as_mode.first, pc->fc.i8x8_mode_prob); write_i8x8_mode(w, m->bmi[8].as_mode.first, pc->fc.i8x8_mode_prob); write_i8x8_mode(w, m->bmi[10].as_mode.first, pc->fc.i8x8_mode_prob); } else { write_uv_mode(w, mi->uv_mode, pc->fc.uv_mode_prob[mode]); } } else { int_mv best_mv, best_second_mv; int ct[4]; vp8_prob mv_ref_p [VP8_MVREFS - 1]; { int_mv n1, n2; vp8_find_near_mvs(xd, m, prev_m, &n1, &n2, &best_mv, ct, rf, cpi->common.ref_frame_sign_bias); #if CONFIG_NEWBESTREFMV best_mv.as_int = mi->ref_mv.as_int; #endif vp8_mv_ref_probs(&cpi->common, mv_ref_p, ct); #ifdef ENTROPY_STATS accum_mv_refs(mode, ct); #endif } #ifdef ENTROPY_STATS active_section = 3; #endif // Is the segment coding of mode enabled if (!segfeature_active(xd, segment_id, SEG_LVL_MODE)) { #if CONFIG_SUPERBLOCKS if (mi->encoded_as_sb) { write_sb_mv_ref(w, mode, mv_ref_p); } else #endif { write_mv_ref(w, mode, mv_ref_p); } vp8_accum_mv_refs(&cpi->common, mode, ct); } #if CONFIG_PRED_FILTER // Is the prediction filter enabled if (mode >= NEARESTMV && mode < SPLITMV) { if (cpi->common.pred_filter_mode == 2) vp8_write(w, mi->pred_filter_enabled, pc->prob_pred_filter_off); else assert(mi->pred_filter_enabled == cpi->common.pred_filter_mode); } #endif #if CONFIG_SWITCHABLE_INTERP if (mode >= NEARESTMV && mode <= SPLITMV) { if (cpi->common.mcomp_filter_type == SWITCHABLE) { vp8_write_token(w, vp8_switchable_interp_tree,
1121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190
get_pred_probs(&cpi->common, xd, PRED_SWITCHABLE_INTERP), vp8_switchable_interp_encodings + vp8_switchable_interp_map[mi->interp_filter]); //if (!cpi->dummy_packing) printf("Reading: %d\n", mi->interp_filter); } else { assert (mi->interp_filter == cpi->common.mcomp_filter_type); } } #endif if (mi->second_ref_frame && (mode == NEWMV || mode == SPLITMV)) { int_mv n1, n2; vp8_find_near_mvs(xd, m, prev_m, &n1, &n2, &best_second_mv, ct, mi->second_ref_frame, cpi->common.ref_frame_sign_bias); #if CONFIG_NEWBESTREFMV best_second_mv.as_int = mi->second_ref_mv.as_int; #endif } // does the feature use compound prediction or not // (if not specified at the frame/segment level) if (cpi->common.comp_pred_mode == HYBRID_PREDICTION) { vp8_write(w, mi->second_ref_frame != INTRA_FRAME, get_pred_prob(pc, xd, PRED_COMP)); } { switch (mode) { /* new, split require MVs */ case NEWMV: #ifdef ENTROPY_STATS active_section = 5; #endif #if 0 //CONFIG_NEW_MVREF { unsigned int best_index; /*find_mv_refs(xd, m, prev_m, m->mbmi.ref_frame, mi->ref_mvs[rf], cpi->common.ref_frame_sign_bias );*/ best_index = pick_best_mv_ref(x, mi->mv[0], mi->ref_mvs[rf], &best_mv); cpi->best_ref_index_counts[best_index]++; } #endif #if CONFIG_NEWMVENTROPY write_nmv(w, &mi->mv[0].as_mv, &best_mv, (const nmv_context*) nmvc, xd->allow_high_precision_mv); #else if (xd->allow_high_precision_mv) { write_mv_hp(w, &mi->mv[0].as_mv, &best_mv, mvc_hp); } else { write_mv(w, &mi->mv[0].as_mv, &best_mv, mvc); } #endif if (mi->second_ref_frame) { #if 0 //CONFIG_NEW_MVREF unsigned int best_index; /*find_mv_refs(xd, m, prev_m, m->mbmi.second_ref_frame,
1191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260
mi->ref_mvs[mi->second_ref_frame], cpi->common.ref_frame_sign_bias );*/ best_index = pick_best_mv_ref(x, mi->mv[1], mi->ref_mvs[mi->second_ref_frame], &best_second_mv); cpi->best_ref_index_counts[best_index]++; #endif #if CONFIG_NEWMVENTROPY write_nmv(w, &mi->mv[1].as_mv, &best_second_mv, (const nmv_context*) nmvc, xd->allow_high_precision_mv); #else if (xd->allow_high_precision_mv) { write_mv_hp(w, &mi->mv[1].as_mv, &best_second_mv, mvc_hp); } else { write_mv(w, &mi->mv[1].as_mv, &best_second_mv, mvc); } #endif } break; case SPLITMV: { int j = 0; #ifdef MODE_STATS ++count_mb_seg [mi->partitioning]; #endif write_split(w, mi->partitioning, cpi->common.fc.mbsplit_prob); cpi->mbsplit_count[mi->partitioning]++; do { B_PREDICTION_MODE blockmode; int_mv blockmv; const int *const L = vp8_mbsplits [mi->partitioning]; int k = -1; /* first block in subset j */ int mv_contz; int_mv leftmv, abovemv; blockmode = cpi->mb.partition_info->bmi[j].mode; blockmv = cpi->mb.partition_info->bmi[j].mv; #if CONFIG_DEBUG while (j != L[++k]) if (k >= 16) assert(0); #else while (j != L[++k]); #endif leftmv.as_int = left_block_mv(m, k); abovemv.as_int = above_block_mv(m, k, mis); mv_contz = vp8_mv_cont(&leftmv, &abovemv); write_sub_mv_ref(w, blockmode, cpi->common.fc.sub_mv_ref_prob [mv_contz]); cpi->sub_mv_ref_count[mv_contz][blockmode - LEFT4X4]++; if (blockmode == NEW4X4) { #ifdef ENTROPY_STATS active_section = 11; #endif #if CONFIG_NEWMVENTROPY write_nmv(w, &blockmv.as_mv, &best_mv, (const nmv_context*) nmvc, xd->allow_high_precision_mv); #else if (xd->allow_high_precision_mv) { write_mv_hp(w, &blockmv.as_mv, &best_mv, (const MV_CONTEXT_HP *) mvc_hp); } else {
1261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330
write_mv(w, &blockmv.as_mv, &best_mv, (const MV_CONTEXT *) mvc); } #endif if (mi->second_ref_frame) { #if CONFIG_NEWMVENTROPY write_nmv(w, &cpi->mb.partition_info->bmi[j].second_mv.as_mv, &best_second_mv, (const nmv_context*) nmvc, xd->allow_high_precision_mv); #else if (xd->allow_high_precision_mv) { write_mv_hp(w, &cpi->mb.partition_info->bmi[j].second_mv.as_mv, &best_second_mv, (const MV_CONTEXT_HP *) mvc_hp); } else { write_mv(w, &cpi->mb.partition_info->bmi[j].second_mv.as_mv, &best_second_mv, (const MV_CONTEXT *) mvc); } #endif } } } while (++j < cpi->mb.partition_info->count); } break; default: break; } } } #if CONFIG_SUPERBLOCKS if (m->mbmi.encoded_as_sb) { assert(!i); mb_col += 2; m += 2; cpi->mb.partition_info += 2; prev_m += 2; break; } #endif // Next MB mb_row += dy; mb_col += dx; m += offset_extended; prev_m += offset_extended; cpi->mb.partition_info += offset_extended; #if CONFIG_DEBUG assert((prev_m - cpi->common.prev_mip) == (m - cpi->common.mip)); assert((prev_m - cpi->common.prev_mi) == (m - cpi->common.mi)); #endif } } // Next SB mb_row += 2; m += mis + (1 - (pc->mb_cols & 0x1)); prev_m += mis + (1 - (pc->mb_cols & 0x1)); cpi->mb.partition_info += mis + (1 - (pc->mb_cols & 0x1)); } } static void write_kfmodes(VP8_COMP *cpi) { vp8_writer *const bc = & cpi->bc; VP8_COMMON *const c = & cpi->common; const int mis = c->mode_info_stride; MACROBLOCKD *xd = &cpi->mb.e_mbd;
1331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400
MODE_INFO *m; int i; int row, col; int mb_row, mb_col; int row_delta[4] = { 0, +1, 0, -1}; int col_delta[4] = { +1, -1, +1, +1}; if (c->mb_no_coeff_skip) { update_skip_probs(cpi); for (i = 0; i < MBSKIP_CONTEXTS; ++i) vp8_write_literal(bc, c->mbskip_pred_probs[i], 8); } if (!c->kf_ymode_probs_update) { vp8_write_literal(bc, c->kf_ymode_probs_index, 3); } mb_row = 0; for (row = 0; row < c->mb_rows; row += 2) { m = c->mi + row * mis; mb_col = 0; for (col = 0; col < c->mb_cols; col += 2) { #if CONFIG_SUPERBLOCKS vp8_write(bc, m->mbmi.encoded_as_sb, c->sb_coded); #endif // Process the 4 MBs in the order: // top-left, top-right, bottom-left, bottom-right for (i = 0; i < 4; i++) { int ym; int segment_id; int dy = row_delta[i]; int dx = col_delta[i]; int offset_extended = dy * mis + dx; if ((mb_row >= c->mb_rows) || (mb_col >= c->mb_cols)) { // MB lies outside frame, move on mb_row += dy; mb_col += dx; m += offset_extended; continue; } // Make sure the MacroBlockD mode info pointer is set correctly xd->mode_info_context = m; ym = m->mbmi.mode; segment_id = m->mbmi.segment_id; if (cpi->mb.e_mbd.update_mb_segmentation_map) { write_mb_segid(bc, &m->mbmi, &cpi->mb.e_mbd); } if (c->mb_no_coeff_skip && (!segfeature_active(xd, segment_id, SEG_LVL_EOB) || (get_segdata(xd, segment_id, SEG_LVL_EOB) != 0))) { int skip_coeff = m->mbmi.mb_skip_coeff; #if CONFIG_SUPERBLOCKS if (m->mbmi.encoded_as_sb) { skip_coeff &= m[1].mbmi.mb_skip_coeff; skip_coeff &= m[mis].mbmi.mb_skip_coeff; skip_coeff &= m[mis + 1].mbmi.mb_skip_coeff; } #endif vp8_encode_bool(bc, skip_coeff, get_pred_prob(c, xd, PRED_MBSKIP)); } #if CONFIG_SUPERBLOCKS if (m->mbmi.encoded_as_sb) { sb_kfwrite_ymode(bc, ym,
1401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470
c->sb_kf_ymode_prob[c->kf_ymode_probs_index]); } else #endif { kfwrite_ymode(bc, ym, c->kf_ymode_prob[c->kf_ymode_probs_index]); } if (ym == B_PRED) { const int mis = c->mode_info_stride; int i = 0; #if CONFIG_COMP_INTRA_PRED int uses_second = m->bmi[0].as_mode.second != (B_PREDICTION_MODE)(B_DC_PRED - 1); vp8_write(bc, uses_second, 128); #endif do { const B_PREDICTION_MODE A = above_block_mode(m, i, mis); const B_PREDICTION_MODE L = left_block_mode(m, i); const int bm = m->bmi[i].as_mode.first; #if CONFIG_COMP_INTRA_PRED const int bm2 = m->bmi[i].as_mode.second; #endif #ifdef ENTROPY_STATS ++intra_mode_stats [A] [L] [bm]; #endif write_bmode(bc, bm, c->kf_bmode_prob [A] [L]); // printf(" mode: %d\n", bm); #if CONFIG_COMP_INTRA_PRED if (uses_second) { write_bmode(bc, bm2, c->kf_bmode_prob [A] [L]); } #endif } while (++i < 16); } if (ym == I8X8_PRED) { write_i8x8_mode(bc, m->bmi[0].as_mode.first, c->fc.i8x8_mode_prob); // printf(" mode: %d\n", m->bmi[0].as_mode.first); fflush(stdout); write_i8x8_mode(bc, m->bmi[2].as_mode.first, c->fc.i8x8_mode_prob); // printf(" mode: %d\n", m->bmi[2].as_mode.first); fflush(stdout); write_i8x8_mode(bc, m->bmi[8].as_mode.first, c->fc.i8x8_mode_prob); // printf(" mode: %d\n", m->bmi[8].as_mode.first); fflush(stdout); write_i8x8_mode(bc, m->bmi[10].as_mode.first, c->fc.i8x8_mode_prob); // printf(" mode: %d\n", m->bmi[10].as_mode.first); fflush(stdout); } else write_uv_mode(bc, m->mbmi.uv_mode, c->kf_uv_mode_prob[ym]); #if CONFIG_SUPERBLOCKS if (m->mbmi.encoded_as_sb) { assert(!i); mb_col += 2; m += 2; break; } #endif // Next MB mb_row += dy; mb_col += dx; m += offset_extended; } } mb_row += 2; }
1471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540
} /* This function is used for debugging probability trees. */ static void print_prob_tree(vp8_prob coef_probs[BLOCK_TYPES][COEF_BANDS][PREV_COEF_CONTEXTS][ENTROPY_NODES]) { /* print coef probability tree */ int i, j, k, l; FILE *f = fopen("enc_tree_probs.txt", "a"); fprintf(f, "{\n"); for (i = 0; i < BLOCK_TYPES; i++) { fprintf(f, " {\n"); for (j = 0; j < COEF_BANDS; j++) { fprintf(f, " {\n"); for (k = 0; k < PREV_COEF_CONTEXTS; k++) { fprintf(f, " {"); for (l = 0; l < ENTROPY_NODES; l++) { fprintf(f, "%3u, ", (unsigned int)(coef_probs [i][j][k][l])); } fprintf(f, " }\n"); } fprintf(f, " }\n"); } fprintf(f, " }\n"); } fprintf(f, "}\n"); fclose(f); } void build_coeff_contexts(VP8_COMP *cpi) { int i = 0, j, k; #ifdef ENTROPY_STATS int t = 0; #endif for (i = 0; i < BLOCK_TYPES; ++i) { for (j = 0; j < COEF_BANDS; ++j) { for (k = 0; k < PREV_COEF_CONTEXTS; ++k) { if (k >= 3 && ((i == 0 && j == 1) || (i > 0 && j == 0))) continue; vp8_tree_probs_from_distribution( MAX_ENTROPY_TOKENS, vp8_coef_encodings, vp8_coef_tree, cpi->frame_coef_probs [i][j][k], cpi->frame_branch_ct [i][j][k], cpi->coef_counts [i][j][k], 256, 1 ); #ifdef ENTROPY_STATS if (!cpi->dummy_packing) for (t = 0; t < MAX_ENTROPY_TOKENS; ++t) context_counters[i][j][k][t] += cpi->coef_counts[i][j][k][t]; #endif } } } #if CONFIG_HYBRIDTRANSFORM for (i = 0; i < BLOCK_TYPES; ++i) { for (j = 0; j < COEF_BANDS; ++j) { for (k = 0; k < PREV_COEF_CONTEXTS; ++k) { if (k >= 3 && ((i == 0 && j == 1) || (i > 0 && j == 0))) continue; vp8_tree_probs_from_distribution( MAX_ENTROPY_TOKENS, vp8_coef_encodings, vp8_coef_tree, cpi->frame_hybrid_coef_probs [i][j][k], cpi->frame_hybrid_branch_ct [i][j][k], cpi->hybrid_coef_counts [i][j][k], 256, 1 ); #ifdef ENTROPY_STATS
1541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610
if (!cpi->dummy_packing) for (t = 0; t < MAX_ENTROPY_TOKENS; ++t) hybrid_context_counters[i][j][k][t] += cpi->hybrid_coef_counts[i][j][k][t]; #endif } } } #endif if (cpi->common.txfm_mode == ALLOW_8X8) { for (i = 0; i < BLOCK_TYPES_8X8; ++i) { for (j = 0; j < COEF_BANDS; ++j) { for (k = 0; k < PREV_COEF_CONTEXTS; ++k) { /* at every context */ /* calc probs and branch cts for this frame only */ // vp8_prob new_p [ENTROPY_NODES]; // unsigned int branch_ct [ENTROPY_NODES] [2]; if (k >= 3 && ((i == 0 && j == 1) || (i > 0 && j == 0))) continue; vp8_tree_probs_from_distribution( MAX_ENTROPY_TOKENS, vp8_coef_encodings, vp8_coef_tree, cpi->frame_coef_probs_8x8 [i][j][k], cpi->frame_branch_ct_8x8 [i][j][k], cpi->coef_counts_8x8 [i][j][k], 256, 1 ); #ifdef ENTROPY_STATS if (!cpi->dummy_packing) for (t = 0; t < MAX_ENTROPY_TOKENS; ++t) context_counters_8x8[i][j][k][t] += cpi->coef_counts_8x8[i][j][k][t]; #endif } } } #if CONFIG_HYBRIDTRANSFORM8X8 for (i = 0; i < BLOCK_TYPES_8X8; ++i) { for (j = 0; j < COEF_BANDS; ++j) { for (k = 0; k < PREV_COEF_CONTEXTS; ++k) { /* at every context */ /* calc probs and branch cts for this frame only */ // vp8_prob new_p [ENTROPY_NODES]; // unsigned int branch_ct [ENTROPY_NODES] [2]; if (k >= 3 && ((i == 0 && j == 1) || (i > 0 && j == 0))) continue; vp8_tree_probs_from_distribution( MAX_ENTROPY_TOKENS, vp8_coef_encodings, vp8_coef_tree, cpi->frame_hybrid_coef_probs_8x8 [i][j][k], cpi->frame_hybrid_branch_ct_8x8 [i][j][k], cpi->hybrid_coef_counts_8x8 [i][j][k], 256, 1 ); #ifdef ENTROPY_STATS if (!cpi->dummy_packing) for (t = 0; t < MAX_ENTROPY_TOKENS; ++t) hybrid_context_counters_8x8[i][j][k][t] += cpi->hybrid_coef_counts_8x8[i][j][k][t]; #endif } } } #endif } #if CONFIG_TX16X16 //16x16 for (i = 0; i < BLOCK_TYPES_16X16; ++i) { for (j = 0; j < COEF_BANDS; ++j) { for (k = 0; k < PREV_COEF_CONTEXTS; ++k) { if (k >= 3 && ((i == 0 && j == 1) || (i > 0 && j == 0))) continue;
1611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680
vp8_tree_probs_from_distribution( MAX_ENTROPY_TOKENS, vp8_coef_encodings, vp8_coef_tree, cpi->frame_coef_probs_16x16[i][j][k], cpi->frame_branch_ct_16x16[i][j][k], cpi->coef_counts_16x16[i][j][k], 256, 1); #ifdef ENTROPY_STATS if (!cpi->dummy_packing) for (t = 0; t < MAX_ENTROPY_TOKENS; ++t) context_counters_16x16[i][j][k][t] += cpi->coef_counts_16x16[i][j][k][t]; #endif } } } #if CONFIG_HYBRIDTRANSFORM16X16 for (i = 0; i < BLOCK_TYPES_16X16; ++i) { for (j = 0; j < COEF_BANDS; ++j) { for (k = 0; k < PREV_COEF_CONTEXTS; ++k) { if (k >= 3 && ((i == 0 && j == 1) || (i > 0 && j == 0))) continue; vp8_tree_probs_from_distribution( MAX_ENTROPY_TOKENS, vp8_coef_encodings, vp8_coef_tree, cpi->frame_hybrid_coef_probs_16x16[i][j][k], cpi->frame_hybrid_branch_ct_16x16[i][j][k], cpi->hybrid_coef_counts_16x16[i][j][k], 256, 1); #ifdef ENTROPY_STATS if (!cpi->dummy_packing) for (t = 0; t < MAX_ENTROPY_TOKENS; ++t) hybrid_context_counters_16x16[i][j][k][t] += cpi->hybrid_coef_counts_16x16[i][j][k][t]; #endif } } } #endif #endif } #if 0 static void update_coef_probs2(VP8_COMP *cpi) { const vp8_prob grpupd = 192; int i, j, k, t; vp8_writer *const w = & cpi->bc; int update[2]; int savings; vp8_clear_system_state(); // __asm emms; // Build the cofficient contexts based on counts collected in encode loop build_coeff_contexts(cpi); for (t = 0; t < ENTROPY_NODES; ++t) { /* dry run to see if there is any udpate at all needed */ savings = 0; update[0] = update[1] = 0; for (i = 0; i < BLOCK_TYPES; ++i) { for (j = !i; j < COEF_BANDS; ++j) { for (k = 0; k < PREV_COEF_CONTEXTS; ++k) { vp8_prob newp = cpi->frame_coef_probs [i][j][k][t]; vp8_prob *Pold = cpi->common.fc.coef_probs [i][j][k] + t; const vp8_prob upd = COEF_UPDATE_PROB; int s; int u = 0; if (k >= 3 && ((i == 0 && j == 1) || (i > 0 && j == 0))) continue; #if defined(SEARCH_NEWP) s = prob_diff_update_savings_search( cpi->frame_branch_ct [i][j][k][t], *Pold, &newp, upd); if (s > 0 && newp != *Pold) u = 1; if (u) savings += s - (int)(vp8_cost_zero(upd)); else
1681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750
savings -= (int)(vp8_cost_zero(upd)); #else s = prob_update_savings( cpi->frame_branch_ct [i][j][k][t], *Pold, newp, upd); if (s > 0) u = 1; if (u) savings += s; #endif // printf(" %d %d %d: %d\n", i, j, k, u); update[u]++; } } } if (update[1] == 0 || savings < 0) { vp8_write(w, 0, grpupd); continue; } vp8_write(w, 1, grpupd); for (i = 0; i < BLOCK_TYPES; ++i) { for (j = !i; j < COEF_BANDS; ++j) { for (k = 0; k < PREV_COEF_CONTEXTS; ++k) { vp8_prob newp = cpi->frame_coef_probs [i][j][k][t]; vp8_prob *Pold = cpi->common.fc.coef_probs [i][j][k] + t; const vp8_prob upd = COEF_UPDATE_PROB; int s; int u = 0; if (k >= 3 && ((i == 0 && j == 1) || (i > 0 && j == 0))) continue; #if defined(SEARCH_NEWP) s = prob_diff_update_savings_search( cpi->frame_branch_ct [i][j][k][t], *Pold, &newp, upd); if (s > 0 && newp != *Pold) u = 1; #else s = prob_update_savings( cpi->frame_branch_ct [i][j][k][t], *Pold, newp, upd); if (s > 0) u = 1; #endif // printf(" %d %d %d: %d (%d)\n", i, j, k, u, upd); vp8_write(w, u, upd); #ifdef ENTROPY_STATS ++ tree_update_hist [i][j][k][t] [u]; #endif if (u) { /* send/use new probability */ write_prob_diff_update(w, newp, *Pold); *Pold = newp; } } } } } if (cpi->common.txfm_mode != ALLOW_8X8) return; for (t = 0; t < ENTROPY_NODES; ++t) { /* dry run to see if there is any udpate at all needed */ savings = 0; update[0] = update[1] = 0; for (i = 0; i < BLOCK_TYPES_8X8; ++i) { for (j = !i; j < COEF_BANDS; ++j) { for (k = 0; k < PREV_COEF_CONTEXTS; ++k) { vp8_prob newp = cpi->frame_coef_probs_8x8 [i][j][k][t]; vp8_prob *Pold = cpi->common.fc.coef_probs_8x8 [i][j][k] + t; const vp8_prob upd = COEF_UPDATE_PROB_8X8; int s; int u = 0; if (k >= 3 && ((i == 0 && j == 1) || (i > 0 && j == 0))) continue; #if defined(SEARCH_NEWP) s = prob_diff_update_savings_search(
1751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820
cpi->frame_branch_ct_8x8 [i][j][k][t], *Pold, &newp, upd); if (s > 0 && newp != *Pold) u = 1; if (u) savings += s - (int)(vp8_cost_zero(upd)); else savings -= (int)(vp8_cost_zero(upd)); #else s = prob_update_savings( cpi->frame_branch_ct_8x8 [i][j][k][t], *Pold, newp, upd); if (s > 0) u = 1; if (u) savings += s; #endif update[u]++; } } } if (update[1] == 0 || savings < 0) { vp8_write(w, 0, grpupd); continue; } vp8_write(w, 1, grpupd); for (i = 0; i < BLOCK_TYPES_8X8; ++i) { for (j = !i; j < COEF_BANDS; ++j) { for (k = 0; k < PREV_COEF_CONTEXTS; ++k) { vp8_prob newp = cpi->frame_coef_probs_8x8 [i][j][k][t]; vp8_prob *Pold = cpi->common.fc.coef_probs_8x8 [i][j][k] + t; const vp8_prob upd = COEF_UPDATE_PROB_8X8; int s; int u = 0; if (k >= 3 && ((i == 0 && j == 1) || (i > 0 && j == 0))) continue; #if defined(SEARCH_NEWP) s = prob_diff_update_savings_search( cpi->frame_branch_ct_8x8 [i][j][k][t], *Pold, &newp, upd); if (s > 0 && newp != *Pold) u = 1; #else s = prob_update_savings( cpi->frame_branch_ct_8x8 [i][j][k][t], *Pold, newp, upd); if (s > 0) u = 1; #endif vp8_write(w, u, upd); #ifdef ENTROPY_STATS if (!cpi->dummy_packing) ++ tree_update_hist_8x8 [i][j][k][t] [u]; #endif if (u) { /* send/use new probability */ write_prob_diff_update(w, newp, *Pold); *Pold = newp; } } } } } } #endif static void update_coef_probs(VP8_COMP *cpi) { int i, j, k, t; vp8_writer *const w = & cpi->bc; int update[2] = {0, 0};
1821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890
int savings; vp8_clear_system_state(); // __asm emms; // Build the cofficient contexts based on counts collected in encode loop build_coeff_contexts(cpi); // vp8_prob bestupd = find_coef_update_prob(cpi); /* dry run to see if there is any udpate at all needed */ savings = 0; for (i = 0; i < BLOCK_TYPES; ++i) { for (j = !i; j < COEF_BANDS; ++j) { int prev_coef_savings[ENTROPY_NODES] = {0}; for (k = 0; k < PREV_COEF_CONTEXTS; ++k) { for (t = 0; t < ENTROPY_NODES; ++t) { vp8_prob newp = cpi->frame_coef_probs [i][j][k][t]; vp8_prob *Pold = cpi->common.fc.coef_probs [i][j][k] + t; const vp8_prob upd = COEF_UPDATE_PROB; int s = prev_coef_savings[t]; int u = 0; if (k >= 3 && ((i == 0 && j == 1) || (i > 0 && j == 0))) continue; #if defined(SEARCH_NEWP) s = prob_diff_update_savings_search( cpi->frame_branch_ct [i][j][k][t], *Pold, &newp, upd); if (s > 0 && newp != *Pold) u = 1; if (u) savings += s - (int)(vp8_cost_zero(upd)); else savings -= (int)(vp8_cost_zero(upd)); #else s = prob_update_savings( cpi->frame_branch_ct [i][j][k][t], *Pold, newp, upd); if (s > 0) u = 1; if (u) savings += s; #endif update[u]++; } } } } // printf("Update %d %d, savings %d\n", update[0], update[1], savings); /* Is coef updated at all */ if (update[1] == 0 || savings < 0) vp8_write_bit(w, 0); else { vp8_write_bit(w, 1); for (i = 0; i < BLOCK_TYPES; ++i) { for (j = !i; j < COEF_BANDS; ++j) { int prev_coef_savings[ENTROPY_NODES] = {0}; for (k = 0; k < PREV_COEF_CONTEXTS; ++k) { // calc probs and branch cts for this frame only for (t = 0; t < ENTROPY_NODES; ++t) { vp8_prob newp = cpi->frame_coef_probs [i][j][k][t]; vp8_prob *Pold = cpi->common.fc.coef_probs [i][j][k] + t; const vp8_prob upd = COEF_UPDATE_PROB; int s = prev_coef_savings[t]; int u = 0; if (k >= 3 && ((i == 0 && j == 1) || (i > 0 && j == 0))) continue; #if defined(SEARCH_NEWP)
1891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960
s = prob_diff_update_savings_search( cpi->frame_branch_ct [i][j][k][t], *Pold, &newp, upd); if (s > 0 && newp != *Pold) u = 1; #else s = prob_update_savings( cpi->frame_branch_ct [i][j][k][t], *Pold, newp, upd); if (s > 0) u = 1; #endif vp8_write(w, u, upd); #ifdef ENTROPY_STATS if (!cpi->dummy_packing) ++ tree_update_hist [i][j][k][t] [u]; #endif if (u) { /* send/use new probability */ write_prob_diff_update(w, newp, *Pold); *Pold = newp; } } } } } } #if CONFIG_HYBRIDTRANSFORM savings = 0; update[0] = update[1] = 0; for (i = 0; i < BLOCK_TYPES; ++i) { for (j = !i; j < COEF_BANDS; ++j) { int prev_coef_savings[ENTROPY_NODES] = {0}; for (k = 0; k < PREV_COEF_CONTEXTS; ++k) { for (t = 0; t < ENTROPY_NODES; ++t) { vp8_prob newp = cpi->frame_hybrid_coef_probs [i][j][k][t]; vp8_prob *Pold = cpi->common.fc.hybrid_coef_probs [i][j][k] + t; const vp8_prob upd = COEF_UPDATE_PROB; int s = prev_coef_savings[t]; int u = 0; if (k >= 3 && ((i == 0 && j == 1) || (i > 0 && j == 0))) continue; #if defined(SEARCH_NEWP) s = prob_diff_update_savings_search( cpi->frame_hybrid_branch_ct [i][j][k][t], *Pold, &newp, upd); if (s > 0 && newp != *Pold) u = 1; if (u) savings += s - (int)(vp8_cost_zero(upd)); else savings -= (int)(vp8_cost_zero(upd)); #else s = prob_update_savings( cpi->frame_hybrid_branch_ct [i][j][k][t], *Pold, newp, upd); if (s > 0) u = 1; if (u) savings += s; #endif update[u]++; } } } } // printf("Update %d %d, savings %d\n", update[0], update[1], savings);
1961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030
/* Is coef updated at all */ if (update[1] == 0 || savings < 0) vp8_write_bit(w, 0); else { vp8_write_bit(w, 1); for (i = 0; i < BLOCK_TYPES; ++i) { for (j = !i; j < COEF_BANDS; ++j) { int prev_coef_savings[ENTROPY_NODES] = {0}; for (k = 0; k < PREV_COEF_CONTEXTS; ++k) { // calc probs and branch cts for this frame only for (t = 0; t < ENTROPY_NODES; ++t) { vp8_prob newp = cpi->frame_hybrid_coef_probs [i][j][k][t]; vp8_prob *Pold = cpi->common.fc.hybrid_coef_probs [i][j][k] + t; const vp8_prob upd = COEF_UPDATE_PROB; int s = prev_coef_savings[t]; int u = 0; if (k >= 3 && ((i == 0 && j == 1) || (i > 0 && j == 0))) continue; #if defined(SEARCH_NEWP) s = prob_diff_update_savings_search( cpi->frame_hybrid_branch_ct [i][j][k][t], *Pold, &newp, upd); if (s > 0 && newp != *Pold) u = 1; #else s = prob_update_savings( cpi->frame_hybrid_branch_ct [i][j][k][t], *Pold, newp, upd); if (s > 0) u = 1; #endif vp8_write(w, u, upd); #ifdef ENTROPY_STATS if (!cpi->dummy_packing) ++ hybrid_tree_update_hist [i][j][k][t] [u]; #endif if (u) { /* send/use new probability */ write_prob_diff_update(w, newp, *Pold); *Pold = newp; } } } } } } #endif /* do not do this if not even allowed */ if (cpi->common.txfm_mode == ALLOW_8X8) { /* dry run to see if update is necessary */ update[0] = update[1] = 0; savings = 0; for (i = 0; i < BLOCK_TYPES_8X8; ++i) { for (j = !i; j < COEF_BANDS; ++j) { for (k = 0; k < PREV_COEF_CONTEXTS; ++k) { // calc probs and branch cts for this frame only for (t = 0; t < ENTROPY_NODES; ++t) { const unsigned int *ct = cpi->frame_branch_ct_8x8 [i][j][k][t]; vp8_prob newp = cpi->frame_coef_probs_8x8 [i][j][k][t]; vp8_prob *Pold = cpi->common.fc.coef_probs_8x8 [i][j][k] + t; const vp8_prob oldp = *Pold; int s, u; const vp8_prob upd = COEF_UPDATE_PROB_8X8; if (k >= 3 && ((i == 0 && j == 1) || (i > 0 && j == 0))) continue; #if defined(SEARCH_NEWP) s = prob_diff_update_savings_search(ct, oldp, &newp, upd); u = s > 0 && newp != oldp ? 1 : 0;
2031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100
if (u) savings += s - (int)(vp8_cost_zero(upd)); else savings -= (int)(vp8_cost_zero(upd)); #else s = prob_update_savings(ct, oldp, newp, upd); u = s > 0 ? 1 : 0; if (u) savings += s; #endif update[u]++; } } } } if (update[1] == 0 || savings < 0) vp8_write_bit(w, 0); else { vp8_write_bit(w, 1); for (i = 0; i < BLOCK_TYPES_8X8; ++i) { for (j = !i; j < COEF_BANDS; ++j) { for (k = 0; k < PREV_COEF_CONTEXTS; ++k) { for (t = 0; t < ENTROPY_NODES; ++t) { const unsigned int *ct = cpi->frame_branch_ct_8x8 [i][j][k][t]; vp8_prob newp = cpi->frame_coef_probs_8x8 [i][j][k][t]; vp8_prob *Pold = cpi->common.fc.coef_probs_8x8 [i][j][k] + t; const vp8_prob oldp = *Pold; const vp8_prob upd = COEF_UPDATE_PROB_8X8; int s, u; if (k >= 3 && ((i == 0 && j == 1) || (i > 0 && j == 0))) continue; #if defined(SEARCH_NEWP) s = prob_diff_update_savings_search(ct, oldp, &newp, upd); u = s > 0 && newp != oldp ? 1 : 0; #else s = prob_update_savings(ct, oldp, newp, upd); u = s > 0 ? 1 : 0; #endif vp8_write(w, u, upd); #ifdef ENTROPY_STATS if (!cpi->dummy_packing) ++ tree_update_hist_8x8 [i][j][k][t] [u]; #endif if (u) { /* send/use new probability */ write_prob_diff_update(w, newp, oldp); *Pold = newp; } } } } } } #if CONFIG_HYBRIDTRANSFORM8X8 update[0] = update[1] = 0; savings = 0; for (i = 0; i < BLOCK_TYPES_8X8; ++i) { for (j = !i; j < COEF_BANDS; ++j) { for (k = 0; k < PREV_COEF_CONTEXTS; ++k) { // calc probs and branch cts for this frame only for (t = 0; t < ENTROPY_NODES; ++t) { const unsigned int *ct = cpi->frame_hybrid_branch_ct_8x8 [i][j][k][t]; vp8_prob newp = cpi->frame_hybrid_coef_probs_8x8 [i][j][k][t]; vp8_prob *Pold = cpi->common.fc.hybrid_coef_probs_8x8 [i][j][k] + t; const vp8_prob oldp = *Pold; int s, u; const vp8_prob upd = COEF_UPDATE_PROB_8X8; if (k >= 3 && ((i == 0 && j == 1) || (i > 0 && j == 0)))
2101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170
continue; #if defined(SEARCH_NEWP) s = prob_diff_update_savings_search(ct, oldp, &newp, upd); u = s > 0 && newp != oldp ? 1 : 0; if (u) savings += s - (int)(vp8_cost_zero(upd)); else savings -= (int)(vp8_cost_zero(upd)); #else s = prob_update_savings(ct, oldp, newp, upd); u = s > 0 ? 1 : 0; if (u) savings += s; #endif update[u]++; } } } } if (update[1] == 0 || savings < 0) vp8_write_bit(w, 0); else { vp8_write_bit(w, 1); for (i = 0; i < BLOCK_TYPES_8X8; ++i) { for (j = !i; j < COEF_BANDS; ++j) { for (k = 0; k < PREV_COEF_CONTEXTS; ++k) { for (t = 0; t < ENTROPY_NODES; ++t) { const unsigned int *ct = cpi->frame_hybrid_branch_ct_8x8 [i][j][k][t]; vp8_prob newp = cpi->frame_hybrid_coef_probs_8x8 [i][j][k][t]; vp8_prob *Pold = cpi->common.fc.hybrid_coef_probs_8x8 [i][j][k] + t; const vp8_prob oldp = *Pold; const vp8_prob upd = COEF_UPDATE_PROB_8X8; int s, u; if (k >= 3 && ((i == 0 && j == 1) || (i > 0 && j == 0))) continue; #if defined(SEARCH_NEWP) s = prob_diff_update_savings_search(ct, oldp, &newp, upd); u = s > 0 && newp != oldp ? 1 : 0; #else s = prob_update_savings(ct, oldp, newp, upd); u = s > 0 ? 1 : 0; #endif vp8_write(w, u, upd); #ifdef ENTROPY_STATS if (!cpi->dummy_packing) ++ hybrid_tree_update_hist_8x8 [i][j][k][t] [u]; #endif if (u) { /* send/use new probability */ write_prob_diff_update(w, newp, oldp); *Pold = newp; } } } } } } #endif } #if CONFIG_TX16X16 // 16x16 /* dry run to see if update is necessary */ update[0] = update[1] = 0; savings = 0; for (i = 0; i < BLOCK_TYPES_16X16; ++i) { for (j = !i; j < COEF_BANDS; ++j) { for (k = 0; k < PREV_COEF_CONTEXTS; ++k) {
2171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240
// calc probs and branch cts for this frame only for (t = 0; t < ENTROPY_NODES; ++t) { const unsigned int *ct = cpi->frame_branch_ct_16x16[i][j][k][t]; vp8_prob newp = cpi->frame_coef_probs_16x16[i][j][k][t]; vp8_prob *Pold = cpi->common.fc.coef_probs_16x16[i][j][k] + t; const vp8_prob oldp = *Pold; int s, u; const vp8_prob upd = COEF_UPDATE_PROB_16X16; if (k >= 3 && ((i == 0 && j == 1) || (i > 0 && j == 0))) continue; #if defined(SEARCH_NEWP) s = prob_diff_update_savings_search(ct, oldp, &newp, upd); u = s > 0 && newp != oldp ? 1 : 0; if (u) savings += s - (int)(vp8_cost_zero(upd)); else savings -= (int)(vp8_cost_zero(upd)); #else s = prob_update_savings(ct, oldp, newp, upd); u = s > 0 ? 1 : 0; if (u) savings += s; #endif update[u]++; } } } } if (update[1] == 0 || savings < 0) vp8_write_bit(w, 0); else { vp8_write_bit(w, 1); for (i = 0; i < BLOCK_TYPES_16X16; ++i) { for (j = !i; j < COEF_BANDS; ++j) { for (k = 0; k < PREV_COEF_CONTEXTS; ++k) { for (t = 0; t < ENTROPY_NODES; ++t) { const unsigned int *ct = cpi->frame_branch_ct_16x16[i][j][k][t]; vp8_prob newp = cpi->frame_coef_probs_16x16[i][j][k][t]; vp8_prob *Pold = cpi->common.fc.coef_probs_16x16[i][j][k] + t; const vp8_prob oldp = *Pold; const vp8_prob upd = COEF_UPDATE_PROB_16X16; int s, u; if (k >= 3 && ((i == 0 && j == 1) || (i > 0 && j == 0))) continue; #if defined(SEARCH_NEWP) s = prob_diff_update_savings_search(ct, oldp, &newp, upd); u = s > 0 && newp != oldp ? 1 : 0; #else s = prob_update_savings(ct, oldp, newp, upd); u = s > 0 ? 1 : 0; #endif vp8_write(w, u, upd); #ifdef ENTROPY_STATS if (!cpi->dummy_packing) ++tree_update_hist_16x16[i][j][k][t][u]; #endif if (u) { /* send/use new probability */ write_prob_diff_update(w, newp, oldp); *Pold = newp; } } } } } } #if CONFIG_HYBRIDTRANSFORM16X16 update[0] = update[1] = 0;
2241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310
savings = 0; for (i = 0; i < BLOCK_TYPES_16X16; ++i) { for (j = !i; j < COEF_BANDS; ++j) { for (k = 0; k < PREV_COEF_CONTEXTS; ++k) { // calc probs and branch cts for this frame only for (t = 0; t < ENTROPY_NODES; ++t) { const unsigned int *ct = cpi->frame_hybrid_branch_ct_16x16[i][j][k][t]; vp8_prob newp = cpi->frame_hybrid_coef_probs_16x16[i][j][k][t]; vp8_prob *Pold = cpi->common.fc.hybrid_coef_probs_16x16[i][j][k] + t; const vp8_prob oldp = *Pold; int s, u; const vp8_prob upd = COEF_UPDATE_PROB_16X16; if (k >= 3 && ((i == 0 && j == 1) || (i > 0 && j == 0))) continue; #if defined(SEARCH_NEWP) s = prob_diff_update_savings_search(ct, oldp, &newp, upd); u = s > 0 && newp != oldp ? 1 : 0; if (u) savings += s - (int)(vp8_cost_zero(upd)); else savings -= (int)(vp8_cost_zero(upd)); #else s = prob_update_savings(ct, oldp, newp, upd); u = s > 0 ? 1 : 0; if (u) savings += s; #endif update[u]++; } } } } if (update[1] == 0 || savings < 0) vp8_write_bit(w, 0); else { vp8_write_bit(w, 1); for (i = 0; i < BLOCK_TYPES_16X16; ++i) { for (j = !i; j < COEF_BANDS; ++j) { for (k = 0; k < PREV_COEF_CONTEXTS; ++k) { for (t = 0; t < ENTROPY_NODES; ++t) { const unsigned int *ct = cpi->frame_hybrid_branch_ct_16x16[i][j][k][t]; vp8_prob newp = cpi->frame_hybrid_coef_probs_16x16[i][j][k][t]; vp8_prob *Pold = cpi->common.fc.hybrid_coef_probs_16x16[i][j][k] + t; const vp8_prob oldp = *Pold; const vp8_prob upd = COEF_UPDATE_PROB_16X16; int s, u; if (k >= 3 && ((i == 0 && j == 1) || (i > 0 && j == 0))) continue; #if defined(SEARCH_NEWP) s = prob_diff_update_savings_search(ct, oldp, &newp, upd); u = s > 0 && newp != oldp ? 1 : 0; #else s = prob_update_savings(ct, oldp, newp, upd); u = s > 0 ? 1 : 0; #endif vp8_write(w, u, upd); #ifdef ENTROPY_STATS if (!cpi->dummy_packing) ++hybrid_tree_update_hist_16x16[i][j][k][t][u]; #endif if (u) { /* send/use new probability */ write_prob_diff_update(w, newp, oldp); *Pold = newp; } } } }