diff options
Diffstat (limited to 'vp8/encoder/bitstream.c')
-rw-r--r-- | vp8/encoder/bitstream.c | 1115 |
1 files changed, 467 insertions, 648 deletions
diff --git a/vp8/encoder/bitstream.c b/vp8/encoder/bitstream.c index 812565915..36776ab21 100644 --- a/vp8/encoder/bitstream.c +++ b/vp8/encoder/bitstream.c @@ -45,32 +45,26 @@ 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 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] [COEF_BANDS] [PREV_COEF_CONTEXTS] [ENTROPY_NODES] [2]; -#endif extern unsigned int active_section; #endif @@ -111,10 +105,10 @@ static int remap_prob(int v, int m) { return i; } -static void write_prob_diff_update(vp8_writer *const w, +static void write_prob_diff_update(vp8_writer *const bc, vp8_prob newp, vp8_prob oldp) { int delp = remap_prob(newp, oldp); - vp8_encode_term_subexp(w, delp, SUBEXP_PARAM, 255); + vp8_encode_term_subexp(bc, delp, SUBEXP_PARAM, 255); } static int prob_diff_update_cost(vp8_prob newp, vp8_prob oldp) { @@ -186,7 +180,7 @@ unsigned int pick_best_mv_ref( MACROBLOCK *x, #endif static void update_mode( - vp8_writer *const w, + vp8_writer *const bc, int n, vp8_token tok [/* n */], vp8_tree tree, @@ -212,34 +206,33 @@ static void update_mode( if (new_b + (n << 8) < old_b) { int i = 0; - vp8_write_bit(w, 1); + vp8_write_bit(bc, 1); do { const vp8_prob p = Pnew[i]; - vp8_write_literal(w, Pcur[i] = p ? p : 1, 8); + vp8_write_literal(bc, Pcur[i] = p ? p : 1, 8); } while (++i < n); } else - vp8_write_bit(w, 0); + vp8_write_bit(bc, 0); } -static void update_mbintra_mode_probs(VP8_COMP *cpi) { - VP8_COMMON *const cm = & cpi->common; - - vp8_writer *const w = & cpi->bc; +static void update_mbintra_mode_probs(VP8_COMP* const cpi, + vp8_writer* const bc) { + VP8_COMMON *const cm = &cpi->common; { vp8_prob Pnew [VP8_YMODES - 1]; unsigned int bct [VP8_YMODES - 1] [2]; update_mode( - w, VP8_YMODES, vp8_ymode_encodings, vp8_ymode_tree, + bc, VP8_YMODES, vp8_ymode_encodings, vp8_ymode_tree, Pnew, cm->fc.ymode_prob, bct, (unsigned int *)cpi->ymode_count ); } } -static __inline int get_prob(int num, int den) { +static int get_prob(int num, int den) { int p; if (den <= 0) return 128; @@ -251,85 +244,42 @@ static __inline int get_prob(int num, int den) { return p; } +static int get_binary_prob(int n0, int n1) { + return get_prob(n0, n0 + n1); +} + void update_skip_probs(VP8_COMP *cpi) { - VP8_COMMON *const pc = & cpi->common; + 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]; + pc->mbskip_pred_probs[k] = get_binary_prob(cpi->skip_false_count[k], + cpi->skip_true_count[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; +void update_switchable_interp_probs(VP8_COMP *cpi, vp8_writer* const bc) { + VP8_COMMON *const pc = &cpi->common; 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 - 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 - ); + 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 -*/ + vp8_write_literal(bc, 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[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; + VP8_COMMON *const cm = &cpi->common; int i; int tot_count; vp8_prob new_pred_probs[PREDICTION_PROBS]; @@ -347,15 +297,8 @@ static void update_refpred_stats(VP8_COMP *cpi) { } 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; + new_pred_probs[i] = get_binary_prob(cpi->ref_pred_count[i][0], + cpi->ref_pred_count[i][1]); // Decide whether or not to update the reference frame probs. // Returned costs are in 1/256 bit units. @@ -454,13 +397,15 @@ static int prob_diff_update_savings_search(const unsigned int *ct, return bestsavings; } -static void pack_tokens_c(vp8_writer *w, const TOKENEXTRA *p, int xcount) { - const TOKENEXTRA *const stop = p + xcount; +static void pack_mb_tokens(vp8_writer* const bc, + TOKENEXTRA **tp, + const TOKENEXTRA *const stop) { unsigned int split; unsigned int shift; - int count = w->count; - unsigned int range = w->range; - unsigned int lowvalue = w->lowvalue; + int count = bc->count; + unsigned int range = bc->range; + unsigned int lowvalue = bc->lowvalue; + TOKENEXTRA *p = *tp; while (p < stop) { const int t = p->Token; @@ -471,6 +416,12 @@ static void pack_tokens_c(vp8_writer *w, const TOKENEXTRA *p, int xcount) { int v = a->value; int n = a->Len; + if (t == EOSB_TOKEN) + { + ++p; + break; + } + /* skip one or two nodes */ if (p->skip_eob_node) { n -= p->skip_eob_node; @@ -497,17 +448,17 @@ static void pack_tokens_c(vp8_writer *w, const TOKENEXTRA *p, int xcount) { int offset = shift - count; if ((lowvalue << (offset - 1)) & 0x80000000) { - int x = w->pos - 1; + int x = bc->pos - 1; - while (x >= 0 && w->buffer[x] == 0xff) { - w->buffer[x] = (unsigned char)0; + while (x >= 0 && bc->buffer[x] == 0xff) { + bc->buffer[x] = (unsigned char)0; x--; } - w->buffer[x] += 1; + bc->buffer[x] += 1; } - w->buffer[w->pos++] = (lowvalue >> (24 - offset)); + bc->buffer[bc->pos++] = (lowvalue >> (24 - offset)); lowvalue <<= offset; shift = count; lowvalue &= 0xffffff; @@ -547,17 +498,17 @@ static void pack_tokens_c(vp8_writer *w, const TOKENEXTRA *p, int xcount) { int offset = shift - count; if ((lowvalue << (offset - 1)) & 0x80000000) { - int x = w->pos - 1; + int x = bc->pos - 1; - while (x >= 0 && w->buffer[x] == 0xff) { - w->buffer[x] = (unsigned char)0; + while (x >= 0 && bc->buffer[x] == 0xff) { + bc->buffer[x] = (unsigned char)0; x--; } - w->buffer[x] += 1; + bc->buffer[x] += 1; } - w->buffer[w->pos++] = (lowvalue >> (24 - offset)); + bc->buffer[bc->pos++] = (lowvalue >> (24 - offset)); lowvalue <<= offset; shift = count; lowvalue &= 0xffffff; @@ -583,14 +534,14 @@ static void pack_tokens_c(vp8_writer *w, const TOKENEXTRA *p, int xcount) { range <<= 1; if ((lowvalue & 0x80000000)) { - int x = w->pos - 1; + int x = bc->pos - 1; - while (x >= 0 && w->buffer[x] == 0xff) { - w->buffer[x] = (unsigned char)0; + while (x >= 0 && bc->buffer[x] == 0xff) { + bc->buffer[x] = (unsigned char)0; x--; } - w->buffer[x] += 1; + bc->buffer[x] += 1; } @@ -598,20 +549,19 @@ static void pack_tokens_c(vp8_writer *w, const TOKENEXTRA *p, int xcount) { if (!++count) { count = -8; - w->buffer[w->pos++] = (lowvalue >> 24); + bc->buffer[bc->pos++] = (lowvalue >> 24); lowvalue &= 0xffffff; } } } - ++p; } - w->count = count; - w->lowvalue = lowvalue; - w->range = range; - + bc->count = count; + bc->lowvalue = lowvalue; + bc->range = range; + *tp = p; } static void write_partition_size(unsigned char *cx_data, int size) { @@ -628,107 +578,82 @@ static void write_partition_size(unsigned char *cx_data, int size) { static void write_mv_ref ( - vp8_writer *w, MB_PREDICTION_MODE m, const vp8_prob *p + vp8_writer *bc, 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_write_token(bc, vp8_mv_ref_tree, p, vp8_mv_ref_encoding_array - NEARESTMV + m); } #if CONFIG_SUPERBLOCKS -static void write_sb_mv_ref(vp8_writer *w, MB_PREDICTION_MODE m, const vp8_prob *p) { +static void write_sb_mv_ref(vp8_writer *bc, 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_write_token(bc, 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 + vp8_writer *bc, 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_write_token(bc, 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 -) { +static void write_nmv(vp8_writer *bc, 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_motion_vector(w, &e, mvc); + vp8_encode_nmv(bc, &e, &ref->as_mv, nmvc); + vp8_encode_nmv_fp(bc, &e, &ref->as_mv, nmvc, usehp); } -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, +static void write_mb_segid(vp8_writer *bc, 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]); + vp8_write(bc, 0, xd->mb_segment_tree_probs[0]); + vp8_write(bc, 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]); + vp8_write(bc, 0, xd->mb_segment_tree_probs[0]); + vp8_write(bc, 1, xd->mb_segment_tree_probs[1]); break; case 2: - vp8_write(w, 1, xd->mb_segment_tree_probs[0]); - vp8_write(w, 0, xd->mb_segment_tree_probs[2]); + vp8_write(bc, 1, xd->mb_segment_tree_probs[0]); + vp8_write(bc, 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]); + vp8_write(bc, 1, xd->mb_segment_tree_probs[0]); + vp8_write(bc, 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]); + vp8_write(bc, 0, xd->mb_segment_tree_probs[0]); + vp8_write(bc, 0, xd->mb_segment_tree_probs[1]); break; } } } // This function encodes the reference frame -static void encode_ref_frame(vp8_writer *const w, +static void encode_ref_frame(vp8_writer *const bc, VP8_COMMON *const cm, MACROBLOCKD *xd, int segment_id, @@ -765,7 +690,7 @@ static void encode_ref_frame(vp8_writer *const w, (xd->mode_info_context->mbmi.ref_frame == pred_rf); set_pred_flag(xd, PRED_REF, prediction_flag); - vp8_write(w, prediction_flag, pred_prob); + vp8_write(bc, prediction_flag, pred_prob); // If not predicted correctly then code value explicitly if (!prediction_flag) { @@ -787,18 +712,18 @@ static void encode_ref_frame(vp8_writer *const w, } if (mod_refprobs[0]) { - vp8_write(w, (rf != INTRA_FRAME), mod_refprobs[0]); + vp8_write(bc, (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]); + vp8_write(bc, (rf != LAST_FRAME), mod_refprobs[1]); } if (rf != LAST_FRAME) { if (mod_refprobs[2]) { - vp8_write(w, (rf != GOLDEN_FRAME), mod_refprobs[2]); + vp8_write(bc, (rf != GOLDEN_FRAME), mod_refprobs[2]); } } } @@ -811,50 +736,32 @@ static void encode_ref_frame(vp8_writer *const w, // Update the probabilities used to encode reference frame data static void update_ref_probs(VP8_COMP *const cpi) { - VP8_COMMON *const cm = & cpi->common; + 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; + cm->prob_intra_coded = get_binary_prob(rf_intra, rf_inter); + cm->prob_last_coded = get_prob(rfct[LAST_FRAME], rf_inter); + cm->prob_gf_coded = get_binary_prob(rfct[GOLDEN_FRAME], rfct[ALTREF_FRAME]); // 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) { +static void pack_inter_mode_mvs(VP8_COMP *const cpi, vp8_writer *const bc) { int i; - VP8_COMMON *const pc = & cpi->common; - vp8_writer *const w = & cpi->bc; -#if CONFIG_NEWMVENTROPY + VP8_COMMON *const pc = &cpi->common; const nmv_context *nmvc = &pc->fc.nmvc; -#else - 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; + TOKENEXTRA *tok = cpi->tok; + TOKENEXTRA *tok_end = tok + cpi->tok_count; const int mis = pc->mode_info_stride; int mb_row, mb_col; @@ -871,73 +778,6 @@ static void pack_inter_mode_mvs(VP8_COMP *const cpi) { 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; - } - 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; @@ -950,7 +790,7 @@ static void pack_inter_mode_mvs(VP8_COMP *const cpi) { // 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); + vp8_write(bc, m->mbmi.encoded_as_sb, pc->sb_coded); #endif for (i = 0; i < 4; i++) { MB_MODE_INFO *mi; @@ -972,7 +812,7 @@ static void pack_inter_mode_mvs(VP8_COMP *const cpi) { continue; } - mi = & m->mbmi; + mi = &m->mbmi; rf = mi->ref_frame; mode = mi->mode; segment_id = mi->segment_id; @@ -1000,14 +840,14 @@ static void pack_inter_mode_mvs(VP8_COMP *const cpi) { 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); + vp8_write(bc, 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); + write_mb_segid(bc, mi, &cpi->mb.e_mbd); } else { // Normal unpredicted coding - write_mb_segid(w, mi, &cpi->mb.e_mbd); + write_mb_segid(bc, mi, &cpi->mb.e_mbd); } } @@ -1022,12 +862,12 @@ static void pack_inter_mode_mvs(VP8_COMP *const cpi) { skip_coeff &= m[mis + 1].mbmi.mb_skip_coeff; } #endif - vp8_encode_bool(w, skip_coeff, + vp8_encode_bool(bc, skip_coeff, get_pred_prob(pc, xd, PRED_MBSKIP)); } // Encode the reference frame. - encode_ref_frame(w, pc, xd, segment_id, rf); + encode_ref_frame(bc, pc, xd, segment_id, rf); if (rf == INTRA_FRAME) { #ifdef ENTROPY_STATS @@ -1037,7 +877,7 @@ static void pack_inter_mode_mvs(VP8_COMP *const cpi) { // TODO(rbultje) write using SB tree structure if (!segfeature_active(xd, segment_id, SEG_LVL_MODE)) { - write_ymode(w, mode, pc->fc.ymode_prob); + write_ymode(bc, mode, pc->fc.ymode_prob); } if (mode == B_PRED) { @@ -1046,32 +886,40 @@ static void pack_inter_mode_mvs(VP8_COMP *const cpi) { int uses_second = m->bmi[0].as_mode.second != (B_PREDICTION_MODE)(B_DC_PRED - 1); - vp8_write(w, uses_second, 128); + vp8_write(bc, 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, + write_bmode(bc, m->bmi[j].as_mode.first, pc->fc.bmode_prob); + /* + if (!cpi->dummy_packing) { + int p; + for (p = 0; p < VP8_BINTRAMODES - 1; ++p) + printf(" %d", pc->fc.bmode_prob[p]); + printf("\nbmode[%d][%d]: %d\n", pc->current_video_frame, j, m->bmi[j].as_mode.first); + } + */ #if CONFIG_COMP_INTRA_PRED if (uses_second) { - write_bmode(w, mode2, pc->fc.bmode_prob); + write_bmode(bc, mode2, pc->fc.bmode_prob); } #endif } while (++j < 16); } if (mode == I8X8_PRED) { - write_i8x8_mode(w, m->bmi[0].as_mode.first, + write_i8x8_mode(bc, m->bmi[0].as_mode.first, pc->fc.i8x8_mode_prob); - write_i8x8_mode(w, m->bmi[2].as_mode.first, + write_i8x8_mode(bc, m->bmi[2].as_mode.first, pc->fc.i8x8_mode_prob); - write_i8x8_mode(w, m->bmi[8].as_mode.first, + write_i8x8_mode(bc, m->bmi[8].as_mode.first, pc->fc.i8x8_mode_prob); - write_i8x8_mode(w, m->bmi[10].as_mode.first, + write_i8x8_mode(bc, m->bmi[10].as_mode.first, pc->fc.i8x8_mode_prob); } else { - write_uv_mode(w, mi->uv_mode, + write_uv_mode(bc, mi->uv_mode, pc->fc.uv_mode_prob[mode]); } } else { @@ -1103,11 +951,11 @@ static void pack_inter_mode_mvs(VP8_COMP *const cpi) { 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); + write_sb_mv_ref(bc, mode, mv_ref_p); } else #endif { - write_mv_ref(w, mode, mv_ref_p); + write_mv_ref(bc, mode, mv_ref_p); } vp8_accum_mv_refs(&cpi->common, mode, ct); } @@ -1116,28 +964,26 @@ static void pack_inter_mode_mvs(VP8_COMP *const cpi) { // Is the prediction filter enabled if (mode >= NEARESTMV && mode < SPLITMV) { if (cpi->common.pred_filter_mode == 2) - vp8_write(w, mi->pred_filter_enabled, + vp8_write(bc, 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, - get_pred_probs(&cpi->common, xd, PRED_SWITCHABLE_INTERP), + vp8_write_token(bc, vp8_switchable_interp_tree, + 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; @@ -1155,7 +1001,7 @@ static void pack_inter_mode_mvs(VP8_COMP *const cpi) { // 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, + vp8_write(bc, mi->second_ref_frame != INTRA_FRAME, get_pred_prob(pc, xd, PRED_COMP)); } @@ -1180,17 +1026,9 @@ static void pack_inter_mode_mvs(VP8_COMP *const cpi) { } #endif -#if CONFIG_NEWMVENTROPY - write_nmv(w, &mi->mv[0].as_mv, &best_mv, + write_nmv(bc, &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 @@ -1207,17 +1045,9 @@ static void pack_inter_mode_mvs(VP8_COMP *const cpi) { &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, + write_nmv(bc, &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: { @@ -1227,7 +1057,7 @@ static void pack_inter_mode_mvs(VP8_COMP *const cpi) { ++count_mb_seg [mi->partitioning]; #endif - write_split(w, mi->partitioning, cpi->common.fc.mbsplit_prob); + write_split(bc, mi->partitioning, cpi->common.fc.mbsplit_prob); cpi->mbsplit_count[mi->partitioning]++; do { @@ -1252,43 +1082,23 @@ static void pack_inter_mode_mvs(VP8_COMP *const cpi) { abovemv.as_int = above_block_mv(m, k, mis); mv_contz = vp8_mv_cont(&leftmv, &abovemv); - write_sub_mv_ref(w, blockmode, + write_sub_mv_ref(bc, 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, + write_nmv(bc, &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 { - write_mv(w, &blockmv.as_mv, &best_mv, - (const MV_CONTEXT *) mvc); - } -#endif if (mi->second_ref_frame) { -#if CONFIG_NEWMVENTROPY - write_nmv(w, + write_nmv(bc, &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); @@ -1300,20 +1110,25 @@ static void pack_inter_mode_mvs(VP8_COMP *const cpi) { } } -#if CONFIG_TX_SELECT if (((rf == INTRA_FRAME && mode <= I8X8_PRED) || - (rf != INTRA_FRAME && mode != SPLITMV)) && + (rf != INTRA_FRAME && !(mode == SPLITMV && + mi->partitioning == PARTITIONING_4X4))) && pc->txfm_mode == TX_MODE_SELECT && !((pc->mb_no_coeff_skip && mi->mb_skip_coeff) || (segfeature_active(xd, segment_id, SEG_LVL_EOB) && get_segdata(xd, segment_id, SEG_LVL_EOB) == 0))) { TX_SIZE sz = mi->txfm_size; // FIXME(rbultje) code ternary symbol once all experiments are merged - vp8_write(w, sz != TX_4X4, pc->prob_tx[0]); - if (sz != TX_4X4 && mode != I8X8_PRED) - vp8_write(w, sz != TX_8X8, pc->prob_tx[1]); + vp8_write(bc, sz != TX_4X4, pc->prob_tx[0]); + if (sz != TX_4X4 && mode != I8X8_PRED && mode != SPLITMV) + vp8_write(bc, sz != TX_8X8, pc->prob_tx[1]); } + +#ifdef ENTROPY_STATS + active_section = 1; #endif + assert(tok < tok_end); + pack_mb_tokens(bc, &tok, tok_end); #if CONFIG_SUPERBLOCKS if (m->mbmi.encoded_as_sb) { @@ -1348,9 +1163,108 @@ static void pack_inter_mode_mvs(VP8_COMP *const cpi) { } -static void write_kfmodes(VP8_COMP *cpi) { - vp8_writer *const bc = & cpi->bc; - VP8_COMMON *const c = & cpi->common; +static void write_mb_modes_kf(const VP8_COMMON *c, + const MACROBLOCKD *xd, + const MODE_INFO *m, + int mode_info_stride, + vp8_writer *const bc) { + const int mis = mode_info_stride; + int ym; + int segment_id; + + ym = m->mbmi.mode; + segment_id = m->mbmi.segment_id; + + if (xd->update_mb_segmentation_map) { + write_mb_segid(bc, &m->mbmi, xd); + } + + 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, + 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 (ym <= I8X8_PRED && c->txfm_mode == TX_MODE_SELECT && + !((c->mb_no_coeff_skip && m->mbmi.mb_skip_coeff) || + (segfeature_active(xd, segment_id, SEG_LVL_EOB) && + get_segdata(xd, segment_id, SEG_LVL_EOB) == 0))) { + TX_SIZE sz = m->mbmi.txfm_size; + // FIXME(rbultje) code ternary symbol once all experiments are merged + vp8_write(bc, sz != TX_4X4, c->prob_tx[0]); + if (sz != TX_4X4 && ym <= TM_PRED) + vp8_write(bc, sz != TX_8X8, c->prob_tx[1]); + } +} + +static void write_kfmodes(VP8_COMP* const cpi, vp8_writer* const bc) { + VP8_COMMON *const c = &cpi->common; const int mis = c->mode_info_stride; MACROBLOCKD *xd = &cpi->mb.e_mbd; MODE_INFO *m; @@ -1359,16 +1273,8 @@ static void write_kfmodes(VP8_COMP *cpi) { 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); - } + TOKENEXTRA *tok = cpi->tok; + TOKENEXTRA *tok_end = tok + cpi->tok_count; mb_row = 0; for (row = 0; row < c->mb_rows; row += 2) { @@ -1382,8 +1288,6 @@ static void write_kfmodes(VP8_COMP *cpi) { // 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; @@ -1399,97 +1303,12 @@ static void write_kfmodes(VP8_COMP *cpi) { // 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, - 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 - + write_mb_modes_kf(c, xd, m, mis, bc); #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_TX_SELECT - if (ym <= I8X8_PRED && c->txfm_mode == TX_MODE_SELECT && - !((c->mb_no_coeff_skip && m->mbmi.mb_skip_coeff) || - (segfeature_active(xd, segment_id, SEG_LVL_EOB) && - get_segdata(xd, segment_id, SEG_LVL_EOB) == 0))) { - TX_SIZE sz = m->mbmi.txfm_size; - // FIXME(rbultje) code ternary symbol once all experiments are merged - vp8_write(bc, sz != TX_4X4, c->prob_tx[0]); - if (sz != TX_4X4 && ym <= TM_PRED) - vp8_write(bc, sz != TX_8X8, c->prob_tx[1]); - } + active_section = 8; #endif + assert(tok < tok_end); + pack_mb_tokens(bc, &tok, tok_end); #if CONFIG_SUPERBLOCKS if (m->mbmi.encoded_as_sb) { @@ -1563,7 +1382,6 @@ void build_coeff_contexts(VP8_COMP *cpi) { } } } -#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) { @@ -1584,8 +1402,6 @@ void build_coeff_contexts(VP8_COMP *cpi) { } } } -#endif - if (cpi->common.txfm_mode != ONLY_4X4) { for (i = 0; i < BLOCK_TYPES_8X8; ++i) { @@ -1612,7 +1428,6 @@ void build_coeff_contexts(VP8_COMP *cpi) { } } } -#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) { @@ -1637,7 +1452,6 @@ void build_coeff_contexts(VP8_COMP *cpi) { } } } -#endif } if (cpi->common.txfm_mode > ALLOW_8X8) { @@ -1660,7 +1474,6 @@ void build_coeff_contexts(VP8_COMP *cpi) { } } } -#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) { @@ -1679,14 +1492,13 @@ void build_coeff_contexts(VP8_COMP *cpi) { } } } -#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; + vp8_writer *const w = &cpi->bc; int update[2]; int savings; @@ -1851,9 +1663,8 @@ static void update_coef_probs2(VP8_COMP *cpi) { } #endif -static void update_coef_probs(VP8_COMP *cpi) { +static void update_coef_probs(VP8_COMP* const cpi, vp8_writer* const bc) { int i, j, k, t; - vp8_writer *const w = & cpi->bc; int update[2] = {0, 0}; int savings; @@ -1906,10 +1717,10 @@ static void update_coef_probs(VP8_COMP *cpi) { // 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); + if (update[1] == 0 || savings < 0) { + vp8_write_bit(bc, 0); + } else { + vp8_write_bit(bc, 1); for (i = 0; i < BLOCK_TYPES; ++i) { for (j = !i; j < COEF_BANDS; ++j) { int prev_coef_savings[ENTROPY_NODES] = {0}; @@ -1937,14 +1748,14 @@ static void update_coef_probs(VP8_COMP *cpi) { if (s > 0) u = 1; #endif - vp8_write(w, u, upd); + vp8_write(bc, 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); + write_prob_diff_update(bc, newp, *Pold); *Pold = newp; } } @@ -1953,7 +1764,6 @@ static void update_coef_probs(VP8_COMP *cpi) { } } -#if CONFIG_HYBRIDTRANSFORM savings = 0; update[0] = update[1] = 0; for (i = 0; i < BLOCK_TYPES; ++i) { @@ -1996,10 +1806,10 @@ static void update_coef_probs(VP8_COMP *cpi) { // 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); + if (update[1] == 0 || savings < 0) { + vp8_write_bit(bc, 0); + } else { + vp8_write_bit(bc, 1); for (i = 0; i < BLOCK_TYPES; ++i) { for (j = !i; j < COEF_BANDS; ++j) { int prev_coef_savings[ENTROPY_NODES] = {0}; @@ -2027,14 +1837,14 @@ static void update_coef_probs(VP8_COMP *cpi) { if (s > 0) u = 1; #endif - vp8_write(w, u, upd); + vp8_write(bc, 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); + write_prob_diff_update(bc, newp, *Pold); *Pold = newp; } } @@ -2042,7 +1852,6 @@ static void update_coef_probs(VP8_COMP *cpi) { } } } -#endif /* do not do this if not even allowed */ if (cpi->common.txfm_mode != ONLY_4X4) { @@ -2081,10 +1890,10 @@ static void update_coef_probs(VP8_COMP *cpi) { } } - if (update[1] == 0 || savings < 0) - vp8_write_bit(w, 0); - else { - vp8_write_bit(w, 1); + if (update[1] == 0 || savings < 0) { + vp8_write_bit(bc, 0); + } else { + vp8_write_bit(bc, 1); for (i = 0; i < BLOCK_TYPES_8X8; ++i) { for (j = !i; j < COEF_BANDS; ++j) { for (k = 0; k < PREV_COEF_CONTEXTS; ++k) { @@ -2105,14 +1914,14 @@ static void update_coef_probs(VP8_COMP *cpi) { s = prob_update_savings(ct, oldp, newp, upd); u = s > 0 ? 1 : 0; #endif - vp8_write(w, u, upd); + vp8_write(bc, 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); + write_prob_diff_update(bc, newp, oldp); *Pold = newp; } } @@ -2120,7 +1929,6 @@ static void update_coef_probs(VP8_COMP *cpi) { } } } -#if CONFIG_HYBRIDTRANSFORM8X8 update[0] = update[1] = 0; savings = 0; for (i = 0; i < BLOCK_TYPES_8X8; ++i) { @@ -2155,10 +1963,10 @@ static void update_coef_probs(VP8_COMP *cpi) { } } - if (update[1] == 0 || savings < 0) - vp8_write_bit(w, 0); - else { - vp8_write_bit(w, 1); + if (update[1] == 0 || savings < 0) { + vp8_write_bit(bc, 0); + } else { + vp8_write_bit(bc, 1); for (i = 0; i < BLOCK_TYPES_8X8; ++i) { for (j = !i; j < COEF_BANDS; ++j) { for (k = 0; k < PREV_COEF_CONTEXTS; ++k) { @@ -2179,14 +1987,14 @@ static void update_coef_probs(VP8_COMP *cpi) { s = prob_update_savings(ct, oldp, newp, upd); u = s > 0 ? 1 : 0; #endif - vp8_write(w, u, upd); + vp8_write(bc, 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); + write_prob_diff_update(bc, newp, oldp); *Pold = newp; } } @@ -2194,7 +2002,6 @@ static void update_coef_probs(VP8_COMP *cpi) { } } } -#endif } if (cpi->common.txfm_mode > ALLOW_8X8) { @@ -2233,10 +2040,10 @@ static void update_coef_probs(VP8_COMP *cpi) { } } - if (update[1] == 0 || savings < 0) - vp8_write_bit(w, 0); - else { - vp8_write_bit(w, 1); + if (update[1] == 0 || savings < 0) { + vp8_write_bit(bc, 0); + } else { + vp8_write_bit(bc, 1); for (i = 0; i < BLOCK_TYPES_16X16; ++i) { for (j = !i; j < COEF_BANDS; ++j) { for (k = 0; k < PREV_COEF_CONTEXTS; ++k) { @@ -2257,14 +2064,14 @@ static void update_coef_probs(VP8_COMP *cpi) { s = prob_update_savings(ct, oldp, newp, upd); u = s > 0 ? 1 : 0; #endif - vp8_write(w, u, upd); + vp8_write(bc, 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); + write_prob_diff_update(bc, newp, oldp); *Pold = newp; } } @@ -2272,7 +2079,6 @@ static void update_coef_probs(VP8_COMP *cpi) { } } } -#if CONFIG_HYBRIDTRANSFORM16X16 update[0] = update[1] = 0; savings = 0; for (i = 0; i < BLOCK_TYPES_16X16; ++i) { @@ -2307,10 +2113,10 @@ static void update_coef_probs(VP8_COMP *cpi) { } } - if (update[1] == 0 || savings < 0) - vp8_write_bit(w, 0); - else { - vp8_write_bit(w, 1); + if (update[1] == 0 || savings < 0) { + vp8_write_bit(bc, 0); + } else { + vp8_write_bit(bc, 1); for (i = 0; i < BLOCK_TYPES_16X16; ++i) { for (j = !i; j < COEF_BANDS; ++j) { for (k = 0; k < PREV_COEF_CONTEXTS; ++k) { @@ -2331,14 +2137,14 @@ static void update_coef_probs(VP8_COMP *cpi) { s = prob_update_savings(ct, oldp, newp, upd); u = s > 0 ? 1 : 0; #endif - vp8_write(w, u, upd); + vp8_write(bc, 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); + write_prob_diff_update(bc, newp, oldp); *Pold = newp; } } @@ -2346,7 +2152,6 @@ static void update_coef_probs(VP8_COMP *cpi) { } } } -#endif } } @@ -2402,7 +2207,7 @@ static void segment_reference_frames(VP8_COMP *cpi) { int ref[MAX_MB_SEGMENTS] = {0}; int i, j; int mb_index = 0; - MACROBLOCKD *const xd = & cpi->mb.e_mbd; + MACROBLOCKD *const xd = &cpi->mb.e_mbd; for (i = 0; i < oci->mb_rows; i++) { for (j = 0; j < oci->mb_cols; j++, mb_index++) { @@ -2419,9 +2224,9 @@ static void segment_reference_frames(VP8_COMP *cpi) { void vp8_pack_bitstream(VP8_COMP *cpi, unsigned char *dest, unsigned long *size) { int i, j; VP8_HEADER oh; - VP8_COMMON *const pc = & cpi->common; - vp8_writer *const bc = & cpi->bc; - MACROBLOCKD *const xd = & cpi->mb.e_mbd; + VP8_COMMON *const pc = &cpi->common; + vp8_writer header_bc, residual_bc; + MACROBLOCKD *const xd = &cpi->mb.e_mbd; int extra_bytes_packed = 0; unsigned char *cx_data = dest; @@ -2464,40 +2269,65 @@ void vp8_pack_bitstream(VP8_COMP *cpi, unsigned char *dest, unsigned long *size) extra_bytes_packed = 7; cx_data += extra_bytes_packed; - vp8_start_encode(bc, cx_data); + vp8_start_encode(&header_bc, cx_data); // signal clr type - vp8_write_bit(bc, pc->clr_type); - vp8_write_bit(bc, pc->clamp_type); + vp8_write_bit(&header_bc, pc->clr_type); + vp8_write_bit(&header_bc, pc->clamp_type); - } else - vp8_start_encode(bc, cx_data); + } else { + vp8_start_encode(&header_bc, cx_data); + } // Signal whether or not Segmentation is enabled - vp8_write_bit(bc, (xd->segmentation_enabled) ? 1 : 0); + vp8_write_bit(&header_bc, (xd->segmentation_enabled) ? 1 : 0); // Indicate which features are enabled if (xd->segmentation_enabled) { // Indicate whether or not the segmentation map is being updated. - vp8_write_bit(bc, (xd->update_mb_segmentation_map) ? 1 : 0); + vp8_write_bit(&header_bc, (xd->update_mb_segmentation_map) ? 1 : 0); // If it is, then indicate the method that will be used. if (xd->update_mb_segmentation_map) { // Select the coding strategy (temporal or spatial) choose_segmap_coding_method(cpi); + // Send the tree probabilities used to decode unpredicted + // macro-block segments + for (i = 0; i < MB_FEATURE_TREE_PROBS; i++) { + int data = xd->mb_segment_tree_probs[i]; + + if (data != 255) { + vp8_write_bit(&header_bc, 1); + vp8_write_literal(&header_bc, data, 8); + } else { + vp8_write_bit(&header_bc, 0); + } + } // Write out the chosen coding method. - vp8_write_bit(bc, (pc->temporal_update) ? 1 : 0); + vp8_write_bit(&header_bc, (pc->temporal_update) ? 1 : 0); + if (pc->temporal_update) { + for (i = 0; i < PREDICTION_PROBS; i++) { + int data = pc->segment_pred_probs[i]; + + if (data != 255) { + vp8_write_bit(&header_bc, 1); + vp8_write_literal(&header_bc, data, 8); + } else { + vp8_write_bit(&header_bc, 0); + } + } + } } - vp8_write_bit(bc, (xd->update_mb_segmentation_data) ? 1 : 0); + vp8_write_bit(&header_bc, (xd->update_mb_segmentation_data) ? 1 : 0); // segment_reference_frames(cpi); if (xd->update_mb_segmentation_data) { signed char Data; - vp8_write_bit(bc, (xd->mb_segment_abs_delta) ? 1 : 0); + vp8_write_bit(&header_bc, (xd->mb_segment_abs_delta) ? 1 : 0); // For each segments id... for (i = 0; i < MAX_MB_SEGMENTS; i++) { @@ -2510,67 +2340,67 @@ void vp8_pack_bitstream(VP8_COMP *cpi, unsigned char *dest, unsigned long *size) // check if there's an update if (segfeature_changed(xd, i, j)) { - vp8_write_bit(bc, 1); + vp8_write_bit(&header_bc, 1); if (segfeature_active(xd, i, j)) { // this bit is to say we are still // active/ if we were inactive // this is unnecessary if (old_segfeature_active(xd, i, j)) { - vp8_write_bit(bc, 1); + vp8_write_bit(&header_bc, 1); } // Is the segment data signed.. if (is_segfeature_signed(j)) { // Encode the relevant feature data if (Data < 0) { Data = - Data; - vp8_write_literal(bc, Data, + vp8_write_literal(&header_bc, Data, seg_feature_data_bits(j)); - vp8_write_bit(bc, 1); + vp8_write_bit(&header_bc, 1); } else { - vp8_write_literal(bc, Data, + vp8_write_literal(&header_bc, Data, seg_feature_data_bits(j)); - vp8_write_bit(bc, 0); + vp8_write_bit(&header_bc, 0); } } // Unsigned data element so no sign bit needed else - vp8_write_literal(bc, Data, + vp8_write_literal(&header_bc, Data, seg_feature_data_bits(j)); } // feature is inactive now else if (old_segfeature_active(xd, i, j)) { - vp8_write_bit(bc, 0); + vp8_write_bit(&header_bc, 0); } } else { - vp8_write_bit(bc, 0); + vp8_write_bit(&header_bc, 0); } #else // If the feature is enabled... if (segfeature_active(xd, i, j)) { - vp8_write_bit(bc, 1); + vp8_write_bit(&header_bc, 1); // Is the segment data signed.. if (is_segfeature_signed(j)) { // Encode the relevant feature data if (Data < 0) { Data = - Data; - vp8_write_literal(bc, Data, + vp8_write_literal(&header_bc, Data, seg_feature_data_bits(j)); - vp8_write_bit(bc, 1); + vp8_write_bit(&header_bc, 1); } else { - vp8_write_literal(bc, Data, + vp8_write_literal(&header_bc, Data, seg_feature_data_bits(j)); - vp8_write_bit(bc, 0); + vp8_write_bit(&header_bc, 0); } } // Unsigned data element so no sign bit needed else - vp8_write_literal(bc, Data, + vp8_write_literal(&header_bc, Data, seg_feature_data_bits(j)); } else - vp8_write_bit(bc, 0); + vp8_write_bit(&header_bc, 0); #endif } } @@ -2581,33 +2411,6 @@ void vp8_pack_bitstream(VP8_COMP *cpi, unsigned char *dest, unsigned long *size) save_segment_info(xd); #endif - if (xd->update_mb_segmentation_map) { - // Send the tree probabilities used to decode unpredicted - // macro-block segments - for (i = 0; i < MB_FEATURE_TREE_PROBS; i++) { - int Data = xd->mb_segment_tree_probs[i]; - - if (Data != 255) { - vp8_write_bit(bc, 1); - vp8_write_literal(bc, Data, 8); - } else - vp8_write_bit(bc, 0); - } - - // If predictive coding of segment map is enabled send the - // prediction probabilities. - if (pc->temporal_update) { - for (i = 0; i < PREDICTION_PROBS; i++) { - int Data = pc->segment_pred_probs[i]; - - if (Data != 255) { - vp8_write_bit(bc, 1); - vp8_write_literal(bc, Data, 8); - } else - vp8_write_bit(bc, 0); - } - } - } } // Encode the common prediction model status flag probability updates for @@ -2616,27 +2419,24 @@ void vp8_pack_bitstream(VP8_COMP *cpi, unsigned char *dest, unsigned long *size) if (pc->frame_type != KEY_FRAME) { for (i = 0; i < PREDICTION_PROBS; i++) { if (cpi->ref_pred_probs_update[i]) { - vp8_write_bit(bc, 1); - vp8_write_literal(bc, pc->ref_pred_probs[i], 8); - } else - vp8_write_bit(bc, 0); + vp8_write_bit(&header_bc, 1); + vp8_write_literal(&header_bc, pc->ref_pred_probs[i], 8); + } else { + vp8_write_bit(&header_bc, 0); + } } } #if CONFIG_SUPERBLOCKS { /* sb mode probability */ - int sb_coded = 256 - (cpi->sb_count << 8) / (((pc->mb_rows + 1) >> 1) * ((pc->mb_cols + 1) >> 1)); - if (sb_coded <= 0) - sb_coded = 1; - else if (sb_coded >= 256) - sb_coded = 255; - pc->sb_coded = sb_coded; - vp8_write_literal(bc, pc->sb_coded, 8); + const int sb_max = (((pc->mb_rows + 1) >> 1) * ((pc->mb_cols + 1) >> 1)); + + pc->sb_coded = get_prob(sb_max - cpi->sb_count, sb_max); + vp8_write_literal(&header_bc, pc->sb_coded, 8); } #endif -#if CONFIG_TX_SELECT { if (pc->txfm_mode == TX_MODE_SELECT) { pc->prob_tx[0] = get_prob(cpi->txfm_count[0] + cpi->txfm_count_8x8p[0], @@ -2647,29 +2447,26 @@ void vp8_pack_bitstream(VP8_COMP *cpi, unsigned char *dest, unsigned long *size) pc->prob_tx[0] = 128; pc->prob_tx[1] = 128; } - vp8_write_literal(bc, pc->txfm_mode, 2); + vp8_write_literal(&header_bc, pc->txfm_mode, 2); if (pc->txfm_mode == TX_MODE_SELECT) { - vp8_write_literal(bc, pc->prob_tx[0], 8); - vp8_write_literal(bc, pc->prob_tx[1], 8); + vp8_write_literal(&header_bc, pc->prob_tx[0], 8); + vp8_write_literal(&header_bc, pc->prob_tx[1], 8); } } -#else - vp8_write_bit(bc, !!pc->txfm_mode); -#endif // Encode the loop filter level and type - vp8_write_bit(bc, pc->filter_type); - vp8_write_literal(bc, pc->filter_level, 6); - vp8_write_literal(bc, pc->sharpness_level, 3); + vp8_write_bit(&header_bc, pc->filter_type); + vp8_write_literal(&header_bc, pc->filter_level, 6); + vp8_write_literal(&header_bc, pc->sharpness_level, 3); // Write out loop filter deltas applied at the MB level based on mode or ref frame (if they are enabled). - vp8_write_bit(bc, (xd->mode_ref_lf_delta_enabled) ? 1 : 0); + vp8_write_bit(&header_bc, (xd->mode_ref_lf_delta_enabled) ? 1 : 0); if (xd->mode_ref_lf_delta_enabled) { // Do the deltas need to be updated int send_update = xd->mode_ref_lf_delta_update; - vp8_write_bit(bc, send_update); + vp8_write_bit(&header_bc, send_update); if (send_update) { int Data; @@ -2680,18 +2477,19 @@ void vp8_pack_bitstream(VP8_COMP *cpi, unsigned char *dest, unsigned long *size) // Frame level data if (xd->ref_lf_deltas[i] != xd->last_ref_lf_deltas[i]) { xd->last_ref_lf_deltas[i] = xd->ref_lf_deltas[i]; - vp8_write_bit(bc, 1); + vp8_write_bit(&header_bc, 1); if (Data > 0) { - vp8_write_literal(bc, (Data & 0x3F), 6); - vp8_write_bit(bc, 0); // sign + vp8_write_literal(&header_bc, (Data & 0x3F), 6); + vp8_write_bit(&header_bc, 0); // sign } else { Data = -Data; - vp8_write_literal(bc, (Data & 0x3F), 6); - vp8_write_bit(bc, 1); // sign + vp8_write_literal(&header_bc, (Data & 0x3F), 6); + vp8_write_bit(&header_bc, 1); // sign } - } else - vp8_write_bit(bc, 0); + } else { + vp8_write_bit(&header_bc, 0); + } } // Send update @@ -2700,41 +2498,42 @@ void vp8_pack_bitstream(VP8_COMP *cpi, unsigned char *dest, unsigned long *size) if (xd->mode_lf_deltas[i] != xd->last_mode_lf_deltas[i]) { xd->last_mode_lf_deltas[i] = xd->mode_lf_deltas[i]; - vp8_write_bit(bc, 1); + vp8_write_bit(&header_bc, 1); if (Data > 0) { - vp8_write_literal(bc, (Data & 0x3F), 6); - vp8_write_bit(bc, 0); // sign + vp8_write_literal(&header_bc, (Data & 0x3F), 6); + vp8_write_bit(&header_bc, 0); // sign } else { Data = -Data; - vp8_write_literal(bc, (Data & 0x3F), 6); - vp8_write_bit(bc, 1); // sign + vp8_write_literal(&header_bc, (Data & 0x3F), 6); + vp8_write_bit(&header_bc, 1); // sign } - } else - vp8_write_bit(bc, 0); + } else { + vp8_write_bit(&header_bc, 0); + } } } } // signal here is multi token partition is enabled - // vp8_write_literal(bc, pc->multi_token_partition, 2); - vp8_write_literal(bc, 0, 2); + // vp8_write_literal(&header_bc, pc->multi_token_partition, 2); + vp8_write_literal(&header_bc, 0, 2); // Frame Q baseline quantizer index - vp8_write_literal(bc, pc->base_qindex, QINDEX_BITS); + vp8_write_literal(&header_bc, pc->base_qindex, QINDEX_BITS); // Transmit Dc, Second order and Uv quantizer delta information - put_delta_q(bc, pc->y1dc_delta_q); - put_delta_q(bc, pc->y2dc_delta_q); - put_delta_q(bc, pc->y2ac_delta_q); - put_delta_q(bc, pc->uvdc_delta_q); - put_delta_q(bc, pc->uvac_delta_q); + put_delta_q(&header_bc, pc->y1dc_delta_q); + put_delta_q(&header_bc, pc->y2dc_delta_q); + put_delta_q(&header_bc, pc->y2ac_delta_q); + put_delta_q(&header_bc, pc->uvdc_delta_q); + put_delta_q(&header_bc, pc->uvac_delta_q); // When there is a key frame all reference buffers are updated using the new key frame if (pc->frame_type != KEY_FRAME) { // Should the GF or ARF be updated using the transmitted frame or buffer - vp8_write_bit(bc, pc->refresh_golden_frame); - vp8_write_bit(bc, pc->refresh_alt_ref_frame); + vp8_write_bit(&header_bc, pc->refresh_golden_frame); + vp8_write_bit(&header_bc, pc->refresh_alt_ref_frame); // For inter frames the current default behavior is that when // cm->refresh_golden_frame is set we copy the old GF over to @@ -2744,18 +2543,17 @@ void vp8_pack_bitstream(VP8_COMP *cpi, unsigned char *dest, unsigned long *size) // If not being updated from current frame should either GF or ARF be updated from another buffer if (!pc->refresh_golden_frame) - vp8_write_literal(bc, pc->copy_buffer_to_gf, 2); + vp8_write_literal(&header_bc, pc->copy_buffer_to_gf, 2); if (!pc->refresh_alt_ref_frame) - vp8_write_literal(bc, pc->copy_buffer_to_arf, 2); + vp8_write_literal(&header_bc, pc->copy_buffer_to_arf, 2); // Indicate reference frame sign bias for Golden and ARF frames (always 0 for last frame buffer) - vp8_write_bit(bc, pc->ref_frame_sign_bias[GOLDEN_FRAME]); - vp8_write_bit(bc, pc->ref_frame_sign_bias[ALTREF_FRAME]); + vp8_write_bit(&header_bc, pc->ref_frame_sign_bias[GOLDEN_FRAME]); + vp8_write_bit(&header_bc, pc->ref_frame_sign_bias[ALTREF_FRAME]); // Signal whether to allow high MV precision - vp8_write_bit(bc, (xd->allow_high_precision_mv) ? 1 : 0); -#if CONFIG_SWITCHABLE_INTERP + vp8_write_bit(&header_bc, (xd->allow_high_precision_mv) ? 1 : 0); if (pc->mcomp_filter_type == SWITCHABLE) { /* Check to see if only one of the filters is actually used */ int count[VP8_SWITCHABLE_FILTERS]; @@ -2778,16 +2576,15 @@ void vp8_pack_bitstream(VP8_COMP *cpi, unsigned char *dest, unsigned long *size) } } // Signal the type of subpel filter to use - vp8_write_bit(bc, (pc->mcomp_filter_type == SWITCHABLE)); + vp8_write_bit(&header_bc, (pc->mcomp_filter_type == SWITCHABLE)); if (pc->mcomp_filter_type != SWITCHABLE) -#endif /* CONFIG_SWITCHABLE_INTERP */ - vp8_write_literal(bc, (pc->mcomp_filter_type), 2); + vp8_write_literal(&header_bc, (pc->mcomp_filter_type), 2); } - vp8_write_bit(bc, pc->refresh_entropy_probs); + vp8_write_bit(&header_bc, pc->refresh_entropy_probs); if (pc->frame_type != KEY_FRAME) - vp8_write_bit(bc, pc->refresh_last_frame); + vp8_write_bit(&header_bc, pc->refresh_last_frame); #ifdef ENTROPY_STATS if (pc->frame_type == INTER_FRAME) @@ -2799,62 +2596,93 @@ void vp8_pack_bitstream(VP8_COMP *cpi, unsigned char *dest, unsigned long *size) vp8_clear_system_state(); // __asm emms; vp8_copy(cpi->common.fc.pre_coef_probs, cpi->common.fc.coef_probs); -#if CONFIG_HYBRIDTRANSFORM vp8_copy(cpi->common.fc.pre_hybrid_coef_probs, cpi->common.fc.hybrid_coef_probs); -#endif vp8_copy(cpi->common.fc.pre_coef_probs_8x8, cpi->common.fc.coef_probs_8x8); -#if CONFIG_HYBRIDTRANSFORM8X8 vp8_copy(cpi->common.fc.pre_hybrid_coef_probs_8x8, cpi->common.fc.hybrid_coef_probs_8x8); -#endif vp8_copy(cpi->common.fc.pre_coef_probs_16x16, cpi->common.fc.coef_probs_16x16); -#if CONFIG_HYBRIDTRANSFORM16X16 vp8_copy(cpi->common.fc.pre_hybrid_coef_probs_16x16, cpi->common.fc.hybrid_coef_probs_16x16); -#endif vp8_copy(cpi->common.fc.pre_ymode_prob, cpi->common.fc.ymode_prob); vp8_copy(cpi->common.fc.pre_uv_mode_prob, cpi->common.fc.uv_mode_prob); vp8_copy(cpi->common.fc.pre_bmode_prob, cpi->common.fc.bmode_prob); vp8_copy(cpi->common.fc.pre_sub_mv_ref_prob, cpi->common.fc.sub_mv_ref_prob); vp8_copy(cpi->common.fc.pre_mbsplit_prob, cpi->common.fc.mbsplit_prob); vp8_copy(cpi->common.fc.pre_i8x8_mode_prob, cpi->common.fc.i8x8_mode_prob); -#if CONFIG_NEWMVENTROPY cpi->common.fc.pre_nmvc = cpi->common.fc.nmvc; -#else - vp8_copy(cpi->common.fc.pre_mvc, cpi->common.fc.mvc); - vp8_copy(cpi->common.fc.pre_mvc_hp, cpi->common.fc.mvc_hp); -#endif vp8_zero(cpi->sub_mv_ref_count); vp8_zero(cpi->mbsplit_count); vp8_zero(cpi->common.fc.mv_ref_ct) vp8_zero(cpi->common.fc.mv_ref_ct_a) - update_coef_probs(cpi); + update_coef_probs(cpi, &header_bc); #ifdef ENTROPY_STATS active_section = 2; #endif // Write out the mb_no_coeff_skip flag - vp8_write_bit(bc, pc->mb_no_coeff_skip); + vp8_write_bit(&header_bc, pc->mb_no_coeff_skip); + if (pc->mb_no_coeff_skip) { + int k; - if (pc->frame_type == KEY_FRAME) { - decide_kf_ymode_entropy(cpi); - write_kfmodes(cpi); + update_skip_probs(cpi); + for (k = 0; k < MBSKIP_CONTEXTS; ++k) + vp8_write_literal(&header_bc, pc->mbskip_pred_probs[k], 8); + } -#ifdef ENTROPY_STATS - active_section = 8; -#endif + if (pc->frame_type == KEY_FRAME) { + if (!pc->kf_ymode_probs_update) { + vp8_write_literal(&header_bc, pc->kf_ymode_probs_index, 3); + } } else { - pack_inter_mode_mvs(cpi); - vp8_update_mode_context(&cpi->common); + // Update the probabilities used to encode reference frame data + update_ref_probs(cpi); #ifdef ENTROPY_STATS active_section = 1; #endif + +#if CONFIG_PRED_FILTER + // Write the prediction filter mode used for this frame + vp8_write_literal(&header_bc, 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(&header_bc, pc->prob_pred_filter_off, 8); + +#endif + if (pc->mcomp_filter_type == SWITCHABLE) + update_switchable_interp_probs(cpi, &header_bc); + + vp8_write_literal(&header_bc, pc->prob_intra_coded, 8); + vp8_write_literal(&header_bc, pc->prob_last_coded, 8); + vp8_write_literal(&header_bc, pc->prob_gf_coded, 8); + + { + const int comp_pred_mode = cpi->common.comp_pred_mode; + const int use_compound_pred = (comp_pred_mode != SINGLE_PREDICTION_ONLY); + const int use_hybrid_pred = (comp_pred_mode == HYBRID_PREDICTION); + + vp8_write(&header_bc, use_compound_pred, 128); + if (use_compound_pred) { + vp8_write(&header_bc, use_hybrid_pred, 128); + if (use_hybrid_pred) { + for (i = 0; i < COMP_PRED_CONTEXTS; i++) { + pc->prob_comppred[i] = get_binary_prob(cpi->single_pred_count[i], + cpi->comp_pred_count[i]); + vp8_write_literal(&header_bc, pc->prob_comppred[i], 8); + } + } + } + } + + update_mbintra_mode_probs(cpi, &header_bc); + + vp8_write_nmvprobs(cpi, xd->allow_high_precision_mv, &header_bc); } - vp8_stop_encode(bc); + vp8_stop_encode(&header_bc); - oh.first_partition_length_in_bytes = cpi->bc.pos; + oh.first_partition_length_in_bytes = header_bc.pos; /* update frame tag */ { @@ -2868,15 +2696,21 @@ void vp8_pack_bitstream(VP8_COMP *cpi, unsigned char *dest, unsigned long *size) dest[2] = v >> 16; } - *size = VP8_HEADER_SIZE + extra_bytes_packed + cpi->bc.pos; + *size = VP8_HEADER_SIZE + extra_bytes_packed + header_bc.pos; + vp8_start_encode(&residual_bc, cx_data + header_bc.pos); - vp8_start_encode(&cpi->bc2, cx_data + bc->pos); + if (pc->frame_type == KEY_FRAME) { + decide_kf_ymode_entropy(cpi); + write_kfmodes(cpi, &residual_bc); + } else { + pack_inter_mode_mvs(cpi, &residual_bc); + vp8_update_mode_context(&cpi->common); + } - pack_tokens(&cpi->bc2, cpi->tok, cpi->tok_count); - vp8_stop_encode(&cpi->bc2); + vp8_stop_encode(&residual_bc); - *size += cpi->bc2.pos; + *size += residual_bc.pos; } @@ -2899,14 +2733,9 @@ void print_tree_update_probs() { for (k = 0; k < PREV_COEF_CONTEXTS; k++) { fprintf(f, " {"); for (l = 0; l < ENTROPY_NODES; l++) { - Sum = tree_update_hist[i][j][k][l][0] + tree_update_hist[i][j][k][l][1]; - if (Sum > 0) { - if (((tree_update_hist[i][j][k][l][0] * 255) / Sum) > 0) - fprintf(f, "%3ld, ", (tree_update_hist[i][j][k][l][0] * 255) / Sum); - else - fprintf(f, "%3ld, ", 1); - } else - fprintf(f, "%3ld, ", 128); + fprintf(f, "%3ld, ", + get_binary_prob(tree_update_hist[i][j][k][l][0], + tree_update_hist[i][j][k][l][1])); } fprintf(f, "},\n"); } @@ -2928,14 +2757,9 @@ void print_tree_update_probs() { for (k = 0; k < PREV_COEF_CONTEXTS; k++) { fprintf(f, " {"); for (l = 0; l < MAX_ENTROPY_TOKENS - 1; l++) { - Sum = tree_update_hist_8x8[i][j][k][l][0] + tree_update_hist_8x8[i][j][k][l][1]; - if (Sum > 0) { - if (((tree_update_hist_8x8[i][j][k][l][0] * 255) / Sum) > 0) - fprintf(f, "%3ld, ", (tree_update_hist_8x8[i][j][k][l][0] * 255) / Sum); - else - fprintf(f, "%3ld, ", 1); - } else - fprintf(f, "%3ld, ", 128); + fprintf(f, "%3ld, ", + get_binary_prob(tree_update_hist_8x8[i][j][k][l][0], + tree_update_hist_8x8[i][j][k][l][1])); } fprintf(f, "},\n"); } @@ -2956,14 +2780,9 @@ void print_tree_update_probs() { for (k = 0; k < PREV_COEF_CONTEXTS; k++) { fprintf(f, " {"); for (l = 0; l < MAX_ENTROPY_TOKENS - 1; l++) { - Sum = tree_update_hist_16x16[i][j][k][l][0] + tree_update_hist_16x16[i][j][k][l][1]; - if (Sum > 0) { - if (((tree_update_hist_16x16[i][j][k][l][0] * 255) / Sum) > 0) - fprintf(f, "%3ld, ", (tree_update_hist_16x16[i][j][k][l][0] * 255) / Sum); - else - fprintf(f, "%3ld, ", 1); - } else - fprintf(f, "%3ld, ", 128); + fprintf(f, "%3ld, ", + get_binary_prob(tree_update_hist_16x16[i][j][k][l][0], + tree_update_hist_16x16[i][j][k][l][1])); } fprintf(f, "},\n"); } |