michael@0: /* michael@0: * Copyright (c) 2010 The WebM project authors. All Rights Reserved. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license michael@0: * that can be found in the LICENSE file in the root of the source michael@0: * tree. An additional intellectual property rights grant can be found michael@0: * in the file PATENTS. All contributing project authors may michael@0: * be found in the AUTHORS file in the root of the source tree. michael@0: */ michael@0: michael@0: michael@0: #include "vp8/common/header.h" michael@0: #include "encodemv.h" michael@0: #include "vp8/common/entropymode.h" michael@0: #include "vp8/common/findnearmv.h" michael@0: #include "mcomp.h" michael@0: #include "vp8/common/systemdependent.h" michael@0: #include michael@0: #include michael@0: #include michael@0: #include "vp8/common/pragmas.h" michael@0: #include "vpx/vpx_encoder.h" michael@0: #include "vpx_mem/vpx_mem.h" michael@0: #include "bitstream.h" michael@0: michael@0: #include "defaultcoefcounts.h" michael@0: #include "vp8/common/common.h" michael@0: michael@0: const int vp8cx_base_skip_false_prob[128] = michael@0: { michael@0: 255, 255, 255, 255, 255, 255, 255, 255, michael@0: 255, 255, 255, 255, 255, 255, 255, 255, michael@0: 255, 255, 255, 255, 255, 255, 255, 255, michael@0: 255, 255, 255, 255, 255, 255, 255, 255, michael@0: 255, 255, 255, 255, 255, 255, 255, 255, michael@0: 255, 255, 255, 255, 255, 255, 255, 255, michael@0: 255, 255, 255, 255, 255, 255, 255, 255, michael@0: 251, 248, 244, 240, 236, 232, 229, 225, michael@0: 221, 217, 213, 208, 204, 199, 194, 190, michael@0: 187, 183, 179, 175, 172, 168, 164, 160, michael@0: 157, 153, 149, 145, 142, 138, 134, 130, michael@0: 127, 124, 120, 117, 114, 110, 107, 104, michael@0: 101, 98, 95, 92, 89, 86, 83, 80, michael@0: 77, 74, 71, 68, 65, 62, 59, 56, michael@0: 53, 50, 47, 44, 41, 38, 35, 32, michael@0: 30, 28, 26, 24, 22, 20, 18, 16, michael@0: }; michael@0: michael@0: #if defined(SECTIONBITS_OUTPUT) michael@0: unsigned __int64 Sectionbits[500]; michael@0: #endif michael@0: michael@0: #ifdef VP8_ENTROPY_STATS michael@0: int intra_mode_stats[10][10][10]; michael@0: static unsigned int tree_update_hist [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [ENTROPY_NODES] [2]; michael@0: extern unsigned int active_section; michael@0: #endif michael@0: michael@0: #ifdef MODE_STATS michael@0: int count_mb_seg[4] = { 0, 0, 0, 0 }; michael@0: #endif michael@0: michael@0: michael@0: static void update_mode( michael@0: vp8_writer *const w, michael@0: int n, michael@0: vp8_token tok [/* n */], michael@0: vp8_tree tree, michael@0: vp8_prob Pnew [/* n-1 */], michael@0: vp8_prob Pcur [/* n-1 */], michael@0: unsigned int bct [/* n-1 */] [2], michael@0: const unsigned int num_events[/* n */] michael@0: ) michael@0: { michael@0: unsigned int new_b = 0, old_b = 0; michael@0: int i = 0; michael@0: michael@0: vp8_tree_probs_from_distribution( michael@0: n--, tok, tree, michael@0: Pnew, bct, num_events, michael@0: 256, 1 michael@0: ); michael@0: michael@0: do michael@0: { michael@0: new_b += vp8_cost_branch(bct[i], Pnew[i]); michael@0: old_b += vp8_cost_branch(bct[i], Pcur[i]); michael@0: } michael@0: while (++i < n); michael@0: michael@0: if (new_b + (n << 8) < old_b) michael@0: { michael@0: int j = 0; michael@0: michael@0: vp8_write_bit(w, 1); michael@0: michael@0: do michael@0: { michael@0: const vp8_prob p = Pnew[j]; michael@0: michael@0: vp8_write_literal(w, Pcur[j] = p ? p : 1, 8); michael@0: } michael@0: while (++j < n); michael@0: } michael@0: else michael@0: vp8_write_bit(w, 0); michael@0: } michael@0: michael@0: static void update_mbintra_mode_probs(VP8_COMP *cpi) michael@0: { michael@0: VP8_COMMON *const x = & cpi->common; michael@0: michael@0: vp8_writer *const w = cpi->bc; michael@0: michael@0: { michael@0: vp8_prob Pnew [VP8_YMODES-1]; michael@0: unsigned int bct [VP8_YMODES-1] [2]; michael@0: michael@0: update_mode( michael@0: w, VP8_YMODES, vp8_ymode_encodings, vp8_ymode_tree, michael@0: Pnew, x->fc.ymode_prob, bct, (unsigned int *)cpi->mb.ymode_count michael@0: ); michael@0: } michael@0: { michael@0: vp8_prob Pnew [VP8_UV_MODES-1]; michael@0: unsigned int bct [VP8_UV_MODES-1] [2]; michael@0: michael@0: update_mode( michael@0: w, VP8_UV_MODES, vp8_uv_mode_encodings, vp8_uv_mode_tree, michael@0: Pnew, x->fc.uv_mode_prob, bct, (unsigned int *)cpi->mb.uv_mode_count michael@0: ); michael@0: } michael@0: } michael@0: michael@0: static void write_ymode(vp8_writer *bc, int m, const vp8_prob *p) michael@0: { michael@0: vp8_write_token(bc, vp8_ymode_tree, p, vp8_ymode_encodings + m); michael@0: } michael@0: michael@0: static void kfwrite_ymode(vp8_writer *bc, int m, const vp8_prob *p) michael@0: { michael@0: vp8_write_token(bc, vp8_kf_ymode_tree, p, vp8_kf_ymode_encodings + m); michael@0: } michael@0: michael@0: static void write_uv_mode(vp8_writer *bc, int m, const vp8_prob *p) michael@0: { michael@0: vp8_write_token(bc, vp8_uv_mode_tree, p, vp8_uv_mode_encodings + m); michael@0: } michael@0: michael@0: michael@0: static void write_bmode(vp8_writer *bc, int m, const vp8_prob *p) michael@0: { michael@0: vp8_write_token(bc, vp8_bmode_tree, p, vp8_bmode_encodings + m); michael@0: } michael@0: michael@0: static void write_split(vp8_writer *bc, int x) michael@0: { michael@0: vp8_write_token( michael@0: bc, vp8_mbsplit_tree, vp8_mbsplit_probs, vp8_mbsplit_encodings + x michael@0: ); michael@0: } michael@0: michael@0: void vp8_pack_tokens_c(vp8_writer *w, const TOKENEXTRA *p, int xcount) michael@0: { michael@0: const TOKENEXTRA *stop = p + xcount; michael@0: unsigned int split; michael@0: unsigned int shift; michael@0: int count = w->count; michael@0: unsigned int range = w->range; michael@0: unsigned int lowvalue = w->lowvalue; michael@0: michael@0: while (p < stop) michael@0: { michael@0: const int t = p->Token; michael@0: vp8_token *a = vp8_coef_encodings + t; michael@0: const vp8_extra_bit_struct *b = vp8_extra_bits + t; michael@0: int i = 0; michael@0: const unsigned char *pp = p->context_tree; michael@0: int v = a->value; michael@0: int n = a->Len; michael@0: michael@0: if (p->skip_eob_node) michael@0: { michael@0: n--; michael@0: i = 2; michael@0: } michael@0: michael@0: do michael@0: { michael@0: const int bb = (v >> --n) & 1; michael@0: split = 1 + (((range - 1) * pp[i>>1]) >> 8); michael@0: i = vp8_coef_tree[i+bb]; michael@0: michael@0: if (bb) michael@0: { michael@0: lowvalue += split; michael@0: range = range - split; michael@0: } michael@0: else michael@0: { michael@0: range = split; michael@0: } michael@0: michael@0: shift = vp8_norm[range]; michael@0: range <<= shift; michael@0: count += shift; michael@0: michael@0: if (count >= 0) michael@0: { michael@0: int offset = shift - count; michael@0: michael@0: if ((lowvalue << (offset - 1)) & 0x80000000) michael@0: { michael@0: int x = w->pos - 1; michael@0: michael@0: while (x >= 0 && w->buffer[x] == 0xff) michael@0: { michael@0: w->buffer[x] = (unsigned char)0; michael@0: x--; michael@0: } michael@0: michael@0: w->buffer[x] += 1; michael@0: } michael@0: michael@0: validate_buffer(w->buffer + w->pos, michael@0: 1, michael@0: w->buffer_end, michael@0: w->error); michael@0: michael@0: w->buffer[w->pos++] = (lowvalue >> (24 - offset)); michael@0: lowvalue <<= offset; michael@0: shift = count; michael@0: lowvalue &= 0xffffff; michael@0: count -= 8 ; michael@0: } michael@0: michael@0: lowvalue <<= shift; michael@0: } michael@0: while (n); michael@0: michael@0: michael@0: if (b->base_val) michael@0: { michael@0: const int e = p->Extra, L = b->Len; michael@0: michael@0: if (L) michael@0: { michael@0: const unsigned char *proba = b->prob; michael@0: const int v2 = e >> 1; michael@0: int n2 = L; /* number of bits in v2, assumed nonzero */ michael@0: i = 0; michael@0: michael@0: do michael@0: { michael@0: const int bb = (v2 >> --n2) & 1; michael@0: split = 1 + (((range - 1) * proba[i>>1]) >> 8); michael@0: i = b->tree[i+bb]; michael@0: michael@0: if (bb) michael@0: { michael@0: lowvalue += split; michael@0: range = range - split; michael@0: } michael@0: else michael@0: { michael@0: range = split; michael@0: } michael@0: michael@0: shift = vp8_norm[range]; michael@0: range <<= shift; michael@0: count += shift; michael@0: michael@0: if (count >= 0) michael@0: { michael@0: int offset = shift - count; michael@0: michael@0: if ((lowvalue << (offset - 1)) & 0x80000000) michael@0: { michael@0: int x = w->pos - 1; michael@0: michael@0: while (x >= 0 && w->buffer[x] == 0xff) michael@0: { michael@0: w->buffer[x] = (unsigned char)0; michael@0: x--; michael@0: } michael@0: michael@0: w->buffer[x] += 1; michael@0: } michael@0: michael@0: validate_buffer(w->buffer + w->pos, michael@0: 1, michael@0: w->buffer_end, michael@0: w->error); michael@0: michael@0: w->buffer[w->pos++] = (lowvalue >> (24 - offset)); michael@0: lowvalue <<= offset; michael@0: shift = count; michael@0: lowvalue &= 0xffffff; michael@0: count -= 8 ; michael@0: } michael@0: michael@0: lowvalue <<= shift; michael@0: } michael@0: while (n2); michael@0: } michael@0: michael@0: michael@0: { michael@0: michael@0: split = (range + 1) >> 1; michael@0: michael@0: if (e & 1) michael@0: { michael@0: lowvalue += split; michael@0: range = range - split; michael@0: } michael@0: else michael@0: { michael@0: range = split; michael@0: } michael@0: michael@0: range <<= 1; michael@0: michael@0: if ((lowvalue & 0x80000000)) michael@0: { michael@0: int x = w->pos - 1; michael@0: michael@0: while (x >= 0 && w->buffer[x] == 0xff) michael@0: { michael@0: w->buffer[x] = (unsigned char)0; michael@0: x--; michael@0: } michael@0: michael@0: w->buffer[x] += 1; michael@0: michael@0: } michael@0: michael@0: lowvalue <<= 1; michael@0: michael@0: if (!++count) michael@0: { michael@0: count = -8; michael@0: michael@0: validate_buffer(w->buffer + w->pos, michael@0: 1, michael@0: w->buffer_end, michael@0: w->error); michael@0: michael@0: w->buffer[w->pos++] = (lowvalue >> 24); michael@0: lowvalue &= 0xffffff; michael@0: } michael@0: } michael@0: michael@0: } michael@0: michael@0: ++p; michael@0: } michael@0: michael@0: w->count = count; michael@0: w->lowvalue = lowvalue; michael@0: w->range = range; michael@0: michael@0: } michael@0: michael@0: static void write_partition_size(unsigned char *cx_data, int size) michael@0: { michael@0: signed char csize; michael@0: michael@0: csize = size & 0xff; michael@0: *cx_data = csize; michael@0: csize = (size >> 8) & 0xff; michael@0: *(cx_data + 1) = csize; michael@0: csize = (size >> 16) & 0xff; michael@0: *(cx_data + 2) = csize; michael@0: michael@0: } michael@0: michael@0: static void pack_tokens_into_partitions_c(VP8_COMP *cpi, unsigned char *cx_data, michael@0: unsigned char * cx_data_end, michael@0: int num_part) michael@0: { michael@0: michael@0: int i; michael@0: unsigned char *ptr = cx_data; michael@0: unsigned char *ptr_end = cx_data_end; michael@0: vp8_writer * w; michael@0: michael@0: for (i = 0; i < num_part; i++) michael@0: { michael@0: int mb_row; michael@0: michael@0: w = cpi->bc + i + 1; michael@0: michael@0: vp8_start_encode(w, ptr, ptr_end); michael@0: michael@0: for (mb_row = i; mb_row < cpi->common.mb_rows; mb_row += num_part) michael@0: { michael@0: const TOKENEXTRA *p = cpi->tplist[mb_row].start; michael@0: const TOKENEXTRA *stop = cpi->tplist[mb_row].stop; michael@0: int tokens = (int)(stop - p); michael@0: michael@0: vp8_pack_tokens_c(w, p, tokens); michael@0: } michael@0: michael@0: vp8_stop_encode(w); michael@0: ptr += w->pos; michael@0: } michael@0: } michael@0: michael@0: michael@0: static void pack_mb_row_tokens_c(VP8_COMP *cpi, vp8_writer *w) michael@0: { michael@0: int mb_row; michael@0: michael@0: for (mb_row = 0; mb_row < cpi->common.mb_rows; mb_row++) michael@0: { michael@0: const TOKENEXTRA *p = cpi->tplist[mb_row].start; michael@0: const TOKENEXTRA *stop = cpi->tplist[mb_row].stop; michael@0: int tokens = (int)(stop - p); michael@0: michael@0: vp8_pack_tokens_c(w, p, tokens); michael@0: } michael@0: michael@0: } michael@0: michael@0: static void write_mv_ref michael@0: ( michael@0: vp8_writer *w, MB_PREDICTION_MODE m, const vp8_prob *p michael@0: ) michael@0: { michael@0: #if CONFIG_DEBUG michael@0: assert(NEARESTMV <= m && m <= SPLITMV); michael@0: #endif michael@0: vp8_write_token(w, vp8_mv_ref_tree, p, michael@0: vp8_mv_ref_encoding_array + (m - NEARESTMV)); michael@0: } michael@0: michael@0: static void write_sub_mv_ref michael@0: ( michael@0: vp8_writer *w, B_PREDICTION_MODE m, const vp8_prob *p michael@0: ) michael@0: { michael@0: #if CONFIG_DEBUG michael@0: assert(LEFT4X4 <= m && m <= NEW4X4); michael@0: #endif michael@0: vp8_write_token(w, vp8_sub_mv_ref_tree, p, michael@0: vp8_sub_mv_ref_encoding_array + (m - LEFT4X4)); michael@0: } michael@0: michael@0: static void write_mv michael@0: ( michael@0: vp8_writer *w, const MV *mv, const int_mv *ref, const MV_CONTEXT *mvc michael@0: ) michael@0: { michael@0: MV e; michael@0: e.row = mv->row - ref->as_mv.row; michael@0: e.col = mv->col - ref->as_mv.col; michael@0: michael@0: vp8_encode_motion_vector(w, &e, mvc); michael@0: } michael@0: michael@0: static void write_mb_features(vp8_writer *w, const MB_MODE_INFO *mi, const MACROBLOCKD *x) michael@0: { michael@0: /* Encode the MB segment id. */ michael@0: if (x->segmentation_enabled && x->update_mb_segmentation_map) michael@0: { michael@0: switch (mi->segment_id) michael@0: { michael@0: case 0: michael@0: vp8_write(w, 0, x->mb_segment_tree_probs[0]); michael@0: vp8_write(w, 0, x->mb_segment_tree_probs[1]); michael@0: break; michael@0: case 1: michael@0: vp8_write(w, 0, x->mb_segment_tree_probs[0]); michael@0: vp8_write(w, 1, x->mb_segment_tree_probs[1]); michael@0: break; michael@0: case 2: michael@0: vp8_write(w, 1, x->mb_segment_tree_probs[0]); michael@0: vp8_write(w, 0, x->mb_segment_tree_probs[2]); michael@0: break; michael@0: case 3: michael@0: vp8_write(w, 1, x->mb_segment_tree_probs[0]); michael@0: vp8_write(w, 1, x->mb_segment_tree_probs[2]); michael@0: break; michael@0: michael@0: /* TRAP.. This should not happen */ michael@0: default: michael@0: vp8_write(w, 0, x->mb_segment_tree_probs[0]); michael@0: vp8_write(w, 0, x->mb_segment_tree_probs[1]); michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: void vp8_convert_rfct_to_prob(VP8_COMP *const cpi) michael@0: { michael@0: const int *const rfct = cpi->mb.count_mb_ref_frame_usage; michael@0: const int rf_intra = rfct[INTRA_FRAME]; michael@0: const int rf_inter = rfct[LAST_FRAME] + rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME]; michael@0: michael@0: /* Calculate the probabilities used to code the ref frame based on usage */ michael@0: if (!(cpi->prob_intra_coded = rf_intra * 255 / (rf_intra + rf_inter))) michael@0: cpi->prob_intra_coded = 1; michael@0: michael@0: cpi->prob_last_coded = rf_inter ? (rfct[LAST_FRAME] * 255) / rf_inter : 128; michael@0: michael@0: if (!cpi->prob_last_coded) michael@0: cpi->prob_last_coded = 1; michael@0: michael@0: cpi->prob_gf_coded = (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME]) michael@0: ? (rfct[GOLDEN_FRAME] * 255) / (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME]) : 128; michael@0: michael@0: if (!cpi->prob_gf_coded) michael@0: cpi->prob_gf_coded = 1; michael@0: michael@0: } michael@0: michael@0: static void pack_inter_mode_mvs(VP8_COMP *const cpi) michael@0: { michael@0: VP8_COMMON *const pc = & cpi->common; michael@0: vp8_writer *const w = cpi->bc; michael@0: const MV_CONTEXT *mvc = pc->fc.mvc; michael@0: michael@0: michael@0: MODE_INFO *m = pc->mi; michael@0: const int mis = pc->mode_info_stride; michael@0: int mb_row = -1; michael@0: michael@0: int prob_skip_false = 0; michael@0: michael@0: cpi->mb.partition_info = cpi->mb.pi; michael@0: michael@0: vp8_convert_rfct_to_prob(cpi); michael@0: michael@0: #ifdef VP8_ENTROPY_STATS michael@0: active_section = 1; michael@0: #endif michael@0: michael@0: if (pc->mb_no_coeff_skip) michael@0: { michael@0: int total_mbs = pc->mb_rows * pc->mb_cols; michael@0: michael@0: prob_skip_false = (total_mbs - cpi->mb.skip_true_count ) * 256 / total_mbs; michael@0: michael@0: if (prob_skip_false <= 1) michael@0: prob_skip_false = 1; michael@0: michael@0: if (prob_skip_false > 255) michael@0: prob_skip_false = 255; michael@0: michael@0: cpi->prob_skip_false = prob_skip_false; michael@0: vp8_write_literal(w, prob_skip_false, 8); michael@0: } michael@0: michael@0: vp8_write_literal(w, cpi->prob_intra_coded, 8); michael@0: vp8_write_literal(w, cpi->prob_last_coded, 8); michael@0: vp8_write_literal(w, cpi->prob_gf_coded, 8); michael@0: michael@0: update_mbintra_mode_probs(cpi); michael@0: michael@0: vp8_write_mvprobs(cpi); michael@0: michael@0: while (++mb_row < pc->mb_rows) michael@0: { michael@0: int mb_col = -1; michael@0: michael@0: while (++mb_col < pc->mb_cols) michael@0: { michael@0: const MB_MODE_INFO *const mi = & m->mbmi; michael@0: const MV_REFERENCE_FRAME rf = mi->ref_frame; michael@0: const MB_PREDICTION_MODE mode = mi->mode; michael@0: michael@0: MACROBLOCKD *xd = &cpi->mb.e_mbd; michael@0: michael@0: /* Distance of Mb to the various image edges. michael@0: * These specified to 8th pel as they are always compared to MV michael@0: * values that are in 1/8th pel units michael@0: */ michael@0: xd->mb_to_left_edge = -((mb_col * 16) << 3); michael@0: xd->mb_to_right_edge = ((pc->mb_cols - 1 - mb_col) * 16) << 3; michael@0: xd->mb_to_top_edge = -((mb_row * 16) << 3); michael@0: xd->mb_to_bottom_edge = ((pc->mb_rows - 1 - mb_row) * 16) << 3; michael@0: michael@0: #ifdef VP8_ENTROPY_STATS michael@0: active_section = 9; michael@0: #endif michael@0: michael@0: if (cpi->mb.e_mbd.update_mb_segmentation_map) michael@0: write_mb_features(w, mi, &cpi->mb.e_mbd); michael@0: michael@0: if (pc->mb_no_coeff_skip) michael@0: vp8_encode_bool(w, m->mbmi.mb_skip_coeff, prob_skip_false); michael@0: michael@0: if (rf == INTRA_FRAME) michael@0: { michael@0: vp8_write(w, 0, cpi->prob_intra_coded); michael@0: #ifdef VP8_ENTROPY_STATS michael@0: active_section = 6; michael@0: #endif michael@0: write_ymode(w, mode, pc->fc.ymode_prob); michael@0: michael@0: if (mode == B_PRED) michael@0: { michael@0: int j = 0; michael@0: michael@0: do michael@0: write_bmode(w, m->bmi[j].as_mode, pc->fc.bmode_prob); michael@0: while (++j < 16); michael@0: } michael@0: michael@0: write_uv_mode(w, mi->uv_mode, pc->fc.uv_mode_prob); michael@0: } michael@0: else /* inter coded */ michael@0: { michael@0: int_mv best_mv; michael@0: vp8_prob mv_ref_p [VP8_MVREFS-1]; michael@0: michael@0: vp8_write(w, 1, cpi->prob_intra_coded); michael@0: michael@0: if (rf == LAST_FRAME) michael@0: vp8_write(w, 0, cpi->prob_last_coded); michael@0: else michael@0: { michael@0: vp8_write(w, 1, cpi->prob_last_coded); michael@0: vp8_write(w, (rf == GOLDEN_FRAME) ? 0 : 1, cpi->prob_gf_coded); michael@0: } michael@0: michael@0: { michael@0: int_mv n1, n2; michael@0: int ct[4]; michael@0: michael@0: vp8_find_near_mvs(xd, m, &n1, &n2, &best_mv, ct, rf, cpi->common.ref_frame_sign_bias); michael@0: vp8_clamp_mv2(&best_mv, xd); michael@0: michael@0: vp8_mv_ref_probs(mv_ref_p, ct); michael@0: michael@0: #ifdef VP8_ENTROPY_STATS michael@0: accum_mv_refs(mode, ct); michael@0: #endif michael@0: michael@0: } michael@0: michael@0: #ifdef VP8_ENTROPY_STATS michael@0: active_section = 3; michael@0: #endif michael@0: michael@0: write_mv_ref(w, mode, mv_ref_p); michael@0: michael@0: switch (mode) /* new, split require MVs */ michael@0: { michael@0: case NEWMV: michael@0: michael@0: #ifdef VP8_ENTROPY_STATS michael@0: active_section = 5; michael@0: #endif michael@0: michael@0: write_mv(w, &mi->mv.as_mv, &best_mv, mvc); michael@0: break; michael@0: michael@0: case SPLITMV: michael@0: { michael@0: int j = 0; michael@0: michael@0: #ifdef MODE_STATS michael@0: ++count_mb_seg [mi->partitioning]; michael@0: #endif michael@0: michael@0: write_split(w, mi->partitioning); michael@0: michael@0: do michael@0: { michael@0: B_PREDICTION_MODE blockmode; michael@0: int_mv blockmv; michael@0: const int *const L = vp8_mbsplits [mi->partitioning]; michael@0: int k = -1; /* first block in subset j */ michael@0: int mv_contz; michael@0: int_mv leftmv, abovemv; michael@0: michael@0: blockmode = cpi->mb.partition_info->bmi[j].mode; michael@0: blockmv = cpi->mb.partition_info->bmi[j].mv; michael@0: #if CONFIG_DEBUG michael@0: while (j != L[++k]) michael@0: if (k >= 16) michael@0: assert(0); michael@0: #else michael@0: while (j != L[++k]); michael@0: #endif michael@0: leftmv.as_int = left_block_mv(m, k); michael@0: abovemv.as_int = above_block_mv(m, k, mis); michael@0: mv_contz = vp8_mv_cont(&leftmv, &abovemv); michael@0: michael@0: write_sub_mv_ref(w, blockmode, vp8_sub_mv_ref_prob2 [mv_contz]); michael@0: michael@0: if (blockmode == NEW4X4) michael@0: { michael@0: #ifdef VP8_ENTROPY_STATS michael@0: active_section = 11; michael@0: #endif michael@0: write_mv(w, &blockmv.as_mv, &best_mv, (const MV_CONTEXT *) mvc); michael@0: } michael@0: } michael@0: while (++j < cpi->mb.partition_info->count); michael@0: } michael@0: break; michael@0: default: michael@0: break; michael@0: } michael@0: } michael@0: michael@0: ++m; michael@0: cpi->mb.partition_info++; michael@0: } michael@0: michael@0: ++m; /* skip L prediction border */ michael@0: cpi->mb.partition_info++; michael@0: } michael@0: } michael@0: michael@0: michael@0: static void write_kfmodes(VP8_COMP *cpi) michael@0: { michael@0: vp8_writer *const bc = cpi->bc; michael@0: const VP8_COMMON *const c = & cpi->common; michael@0: /* const */ michael@0: MODE_INFO *m = c->mi; michael@0: michael@0: int mb_row = -1; michael@0: int prob_skip_false = 0; michael@0: michael@0: if (c->mb_no_coeff_skip) michael@0: { michael@0: int total_mbs = c->mb_rows * c->mb_cols; michael@0: michael@0: prob_skip_false = (total_mbs - cpi->mb.skip_true_count ) * 256 / total_mbs; michael@0: michael@0: if (prob_skip_false <= 1) michael@0: prob_skip_false = 1; michael@0: michael@0: if (prob_skip_false >= 255) michael@0: prob_skip_false = 255; michael@0: michael@0: cpi->prob_skip_false = prob_skip_false; michael@0: vp8_write_literal(bc, prob_skip_false, 8); michael@0: } michael@0: michael@0: while (++mb_row < c->mb_rows) michael@0: { michael@0: int mb_col = -1; michael@0: michael@0: while (++mb_col < c->mb_cols) michael@0: { michael@0: const int ym = m->mbmi.mode; michael@0: michael@0: if (cpi->mb.e_mbd.update_mb_segmentation_map) michael@0: write_mb_features(bc, &m->mbmi, &cpi->mb.e_mbd); michael@0: michael@0: if (c->mb_no_coeff_skip) michael@0: vp8_encode_bool(bc, m->mbmi.mb_skip_coeff, prob_skip_false); michael@0: michael@0: kfwrite_ymode(bc, ym, vp8_kf_ymode_prob); michael@0: michael@0: if (ym == B_PRED) michael@0: { michael@0: const int mis = c->mode_info_stride; michael@0: int i = 0; michael@0: michael@0: do michael@0: { michael@0: const B_PREDICTION_MODE A = above_block_mode(m, i, mis); michael@0: const B_PREDICTION_MODE L = left_block_mode(m, i); michael@0: const int bm = m->bmi[i].as_mode; michael@0: michael@0: #ifdef VP8_ENTROPY_STATS michael@0: ++intra_mode_stats [A] [L] [bm]; michael@0: #endif michael@0: michael@0: write_bmode(bc, bm, vp8_kf_bmode_prob [A] [L]); michael@0: } michael@0: while (++i < 16); michael@0: } michael@0: michael@0: write_uv_mode(bc, (m++)->mbmi.uv_mode, vp8_kf_uv_mode_prob); michael@0: } michael@0: michael@0: m++; /* skip L prediction border */ michael@0: } michael@0: } michael@0: michael@0: #if 0 michael@0: /* This function is used for debugging probability trees. */ michael@0: static void print_prob_tree(vp8_prob michael@0: coef_probs[BLOCK_TYPES][COEF_BANDS][PREV_COEF_CONTEXTS][ENTROPY_NODES]) michael@0: { michael@0: /* print coef probability tree */ michael@0: int i,j,k,l; michael@0: FILE* f = fopen("enc_tree_probs.txt", "a"); michael@0: fprintf(f, "{\n"); michael@0: for (i = 0; i < BLOCK_TYPES; i++) michael@0: { michael@0: fprintf(f, " {\n"); michael@0: for (j = 0; j < COEF_BANDS; j++) michael@0: { michael@0: fprintf(f, " {\n"); michael@0: for (k = 0; k < PREV_COEF_CONTEXTS; k++) michael@0: { michael@0: fprintf(f, " {"); michael@0: for (l = 0; l < ENTROPY_NODES; l++) michael@0: { michael@0: fprintf(f, "%3u, ", michael@0: (unsigned int)(coef_probs [i][j][k][l])); michael@0: } michael@0: fprintf(f, " }\n"); michael@0: } michael@0: fprintf(f, " }\n"); michael@0: } michael@0: fprintf(f, " }\n"); michael@0: } michael@0: fprintf(f, "}\n"); michael@0: fclose(f); michael@0: } michael@0: #endif michael@0: michael@0: static void sum_probs_over_prev_coef_context( michael@0: const unsigned int probs[PREV_COEF_CONTEXTS][MAX_ENTROPY_TOKENS], michael@0: unsigned int* out) michael@0: { michael@0: int i, j; michael@0: for (i=0; i < MAX_ENTROPY_TOKENS; ++i) michael@0: { michael@0: for (j=0; j < PREV_COEF_CONTEXTS; ++j) michael@0: { michael@0: const unsigned int tmp = out[i]; michael@0: out[i] += probs[j][i]; michael@0: /* check for wrap */ michael@0: if (out[i] < tmp) michael@0: out[i] = UINT_MAX; michael@0: } michael@0: } michael@0: } michael@0: michael@0: static int prob_update_savings(const unsigned int *ct, michael@0: const vp8_prob oldp, const vp8_prob newp, michael@0: const vp8_prob upd) michael@0: { michael@0: const int old_b = vp8_cost_branch(ct, oldp); michael@0: const int new_b = vp8_cost_branch(ct, newp); michael@0: const int update_b = 8 + michael@0: ((vp8_cost_one(upd) - vp8_cost_zero(upd)) >> 8); michael@0: michael@0: return old_b - new_b - update_b; michael@0: } michael@0: michael@0: static int independent_coef_context_savings(VP8_COMP *cpi) michael@0: { michael@0: MACROBLOCK *const x = & cpi->mb; michael@0: int savings = 0; michael@0: int i = 0; michael@0: do michael@0: { michael@0: int j = 0; michael@0: do michael@0: { michael@0: int k = 0; michael@0: unsigned int prev_coef_count_sum[MAX_ENTROPY_TOKENS] = {0}; michael@0: int prev_coef_savings[MAX_ENTROPY_TOKENS] = {0}; michael@0: const unsigned int (*probs)[MAX_ENTROPY_TOKENS]; michael@0: /* Calculate new probabilities given the constraint that michael@0: * they must be equal over the prev coef contexts michael@0: */ michael@0: michael@0: probs = (const unsigned int (*)[MAX_ENTROPY_TOKENS]) michael@0: x->coef_counts[i][j]; michael@0: michael@0: /* Reset to default probabilities at key frames */ michael@0: if (cpi->common.frame_type == KEY_FRAME) michael@0: probs = default_coef_counts[i][j]; michael@0: michael@0: sum_probs_over_prev_coef_context(probs, prev_coef_count_sum); michael@0: michael@0: do michael@0: { michael@0: /* at every context */ michael@0: michael@0: /* calc probs and branch cts for this frame only */ michael@0: int t = 0; /* token/prob index */ michael@0: michael@0: vp8_tree_probs_from_distribution( michael@0: MAX_ENTROPY_TOKENS, vp8_coef_encodings, vp8_coef_tree, michael@0: cpi->frame_coef_probs[i][j][k], michael@0: cpi->frame_branch_ct [i][j][k], michael@0: prev_coef_count_sum, michael@0: 256, 1); michael@0: michael@0: do michael@0: { michael@0: const unsigned int *ct = cpi->frame_branch_ct [i][j][k][t]; michael@0: const vp8_prob newp = cpi->frame_coef_probs [i][j][k][t]; michael@0: const vp8_prob oldp = cpi->common.fc.coef_probs [i][j][k][t]; michael@0: const vp8_prob upd = vp8_coef_update_probs [i][j][k][t]; michael@0: const int s = prob_update_savings(ct, oldp, newp, upd); michael@0: michael@0: if (cpi->common.frame_type != KEY_FRAME || michael@0: (cpi->common.frame_type == KEY_FRAME && newp != oldp)) michael@0: prev_coef_savings[t] += s; michael@0: } michael@0: while (++t < ENTROPY_NODES); michael@0: } michael@0: while (++k < PREV_COEF_CONTEXTS); michael@0: k = 0; michael@0: do michael@0: { michael@0: /* We only update probabilities if we can save bits, except michael@0: * for key frames where we have to update all probabilities michael@0: * to get the equal probabilities across the prev coef michael@0: * contexts. michael@0: */ michael@0: if (prev_coef_savings[k] > 0 || michael@0: cpi->common.frame_type == KEY_FRAME) michael@0: savings += prev_coef_savings[k]; michael@0: } michael@0: while (++k < ENTROPY_NODES); michael@0: } michael@0: while (++j < COEF_BANDS); michael@0: } michael@0: while (++i < BLOCK_TYPES); michael@0: return savings; michael@0: } michael@0: michael@0: static int default_coef_context_savings(VP8_COMP *cpi) michael@0: { michael@0: MACROBLOCK *const x = & cpi->mb; michael@0: int savings = 0; michael@0: int i = 0; michael@0: do michael@0: { michael@0: int j = 0; michael@0: do michael@0: { michael@0: int k = 0; michael@0: do michael@0: { michael@0: /* at every context */ michael@0: michael@0: /* calc probs and branch cts for this frame only */ michael@0: int t = 0; /* token/prob index */ michael@0: michael@0: vp8_tree_probs_from_distribution( michael@0: MAX_ENTROPY_TOKENS, vp8_coef_encodings, vp8_coef_tree, michael@0: cpi->frame_coef_probs [i][j][k], michael@0: cpi->frame_branch_ct [i][j][k], michael@0: x->coef_counts [i][j][k], michael@0: 256, 1 michael@0: ); michael@0: michael@0: do michael@0: { michael@0: const unsigned int *ct = cpi->frame_branch_ct [i][j][k][t]; michael@0: const vp8_prob newp = cpi->frame_coef_probs [i][j][k][t]; michael@0: const vp8_prob oldp = cpi->common.fc.coef_probs [i][j][k][t]; michael@0: const vp8_prob upd = vp8_coef_update_probs [i][j][k][t]; michael@0: const int s = prob_update_savings(ct, oldp, newp, upd); michael@0: michael@0: if (s > 0) michael@0: { michael@0: savings += s; michael@0: } michael@0: } michael@0: while (++t < ENTROPY_NODES); michael@0: } michael@0: while (++k < PREV_COEF_CONTEXTS); michael@0: } michael@0: while (++j < COEF_BANDS); michael@0: } michael@0: while (++i < BLOCK_TYPES); michael@0: return savings; michael@0: } michael@0: michael@0: void vp8_calc_ref_frame_costs(int *ref_frame_cost, michael@0: int prob_intra, michael@0: int prob_last, michael@0: int prob_garf michael@0: ) michael@0: { michael@0: assert(prob_intra >= 0); michael@0: assert(prob_intra <= 255); michael@0: assert(prob_last >= 0); michael@0: assert(prob_last <= 255); michael@0: assert(prob_garf >= 0); michael@0: assert(prob_garf <= 255); michael@0: ref_frame_cost[INTRA_FRAME] = vp8_cost_zero(prob_intra); michael@0: ref_frame_cost[LAST_FRAME] = vp8_cost_one(prob_intra) michael@0: + vp8_cost_zero(prob_last); michael@0: ref_frame_cost[GOLDEN_FRAME] = vp8_cost_one(prob_intra) michael@0: + vp8_cost_one(prob_last) michael@0: + vp8_cost_zero(prob_garf); michael@0: ref_frame_cost[ALTREF_FRAME] = vp8_cost_one(prob_intra) michael@0: + vp8_cost_one(prob_last) michael@0: + vp8_cost_one(prob_garf); michael@0: michael@0: } michael@0: michael@0: int vp8_estimate_entropy_savings(VP8_COMP *cpi) michael@0: { michael@0: int savings = 0; michael@0: michael@0: const int *const rfct = cpi->mb.count_mb_ref_frame_usage; michael@0: const int rf_intra = rfct[INTRA_FRAME]; michael@0: const int rf_inter = rfct[LAST_FRAME] + rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME]; michael@0: int new_intra, new_last, new_garf, oldtotal, newtotal; michael@0: int ref_frame_cost[MAX_REF_FRAMES]; michael@0: michael@0: vp8_clear_system_state(); michael@0: michael@0: if (cpi->common.frame_type != KEY_FRAME) michael@0: { michael@0: if (!(new_intra = rf_intra * 255 / (rf_intra + rf_inter))) michael@0: new_intra = 1; michael@0: michael@0: new_last = rf_inter ? (rfct[LAST_FRAME] * 255) / rf_inter : 128; michael@0: michael@0: new_garf = (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME]) michael@0: ? (rfct[GOLDEN_FRAME] * 255) / (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME]) : 128; michael@0: michael@0: michael@0: vp8_calc_ref_frame_costs(ref_frame_cost,new_intra,new_last,new_garf); michael@0: michael@0: newtotal = michael@0: rfct[INTRA_FRAME] * ref_frame_cost[INTRA_FRAME] + michael@0: rfct[LAST_FRAME] * ref_frame_cost[LAST_FRAME] + michael@0: rfct[GOLDEN_FRAME] * ref_frame_cost[GOLDEN_FRAME] + michael@0: rfct[ALTREF_FRAME] * ref_frame_cost[ALTREF_FRAME]; michael@0: michael@0: michael@0: /* old costs */ michael@0: vp8_calc_ref_frame_costs(ref_frame_cost,cpi->prob_intra_coded, michael@0: cpi->prob_last_coded,cpi->prob_gf_coded); michael@0: michael@0: oldtotal = michael@0: rfct[INTRA_FRAME] * ref_frame_cost[INTRA_FRAME] + michael@0: rfct[LAST_FRAME] * ref_frame_cost[LAST_FRAME] + michael@0: rfct[GOLDEN_FRAME] * ref_frame_cost[GOLDEN_FRAME] + michael@0: rfct[ALTREF_FRAME] * ref_frame_cost[ALTREF_FRAME]; michael@0: michael@0: savings += (oldtotal - newtotal) / 256; michael@0: } michael@0: michael@0: michael@0: if (cpi->oxcf.error_resilient_mode & VPX_ERROR_RESILIENT_PARTITIONS) michael@0: savings += independent_coef_context_savings(cpi); michael@0: else michael@0: savings += default_coef_context_savings(cpi); michael@0: michael@0: michael@0: return savings; michael@0: } michael@0: michael@0: #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING michael@0: int vp8_update_coef_context(VP8_COMP *cpi) michael@0: { michael@0: int savings = 0; michael@0: michael@0: michael@0: if (cpi->common.frame_type == KEY_FRAME) michael@0: { michael@0: /* Reset to default counts/probabilities at key frames */ michael@0: vp8_copy(cpi->mb.coef_counts, default_coef_counts); michael@0: } michael@0: michael@0: if (cpi->oxcf.error_resilient_mode & VPX_ERROR_RESILIENT_PARTITIONS) michael@0: savings += independent_coef_context_savings(cpi); michael@0: else michael@0: savings += default_coef_context_savings(cpi); michael@0: michael@0: return savings; michael@0: } michael@0: #endif michael@0: michael@0: void vp8_update_coef_probs(VP8_COMP *cpi) michael@0: { michael@0: int i = 0; michael@0: #if !(CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING) michael@0: vp8_writer *const w = cpi->bc; michael@0: #endif michael@0: int savings = 0; michael@0: michael@0: vp8_clear_system_state(); michael@0: michael@0: do michael@0: { michael@0: int j = 0; michael@0: michael@0: do michael@0: { michael@0: int k = 0; michael@0: int prev_coef_savings[ENTROPY_NODES] = {0}; michael@0: if (cpi->oxcf.error_resilient_mode & VPX_ERROR_RESILIENT_PARTITIONS) michael@0: { michael@0: for (k = 0; k < PREV_COEF_CONTEXTS; ++k) michael@0: { michael@0: int t; /* token/prob index */ michael@0: for (t = 0; t < ENTROPY_NODES; ++t) michael@0: { michael@0: const unsigned int *ct = cpi->frame_branch_ct [i][j] michael@0: [k][t]; michael@0: const vp8_prob newp = cpi->frame_coef_probs[i][j][k][t]; michael@0: const vp8_prob oldp = cpi->common.fc.coef_probs[i][j] michael@0: [k][t]; michael@0: const vp8_prob upd = vp8_coef_update_probs[i][j][k][t]; michael@0: michael@0: prev_coef_savings[t] += michael@0: prob_update_savings(ct, oldp, newp, upd); michael@0: } michael@0: } michael@0: k = 0; michael@0: } michael@0: do michael@0: { michael@0: /* note: use result from vp8_estimate_entropy_savings, so no michael@0: * need to call vp8_tree_probs_from_distribution here. michael@0: */ michael@0: michael@0: /* at every context */ michael@0: michael@0: /* calc probs and branch cts for this frame only */ michael@0: int t = 0; /* token/prob index */ michael@0: michael@0: do michael@0: { michael@0: const vp8_prob newp = cpi->frame_coef_probs [i][j][k][t]; michael@0: michael@0: vp8_prob *Pold = cpi->common.fc.coef_probs [i][j][k] + t; michael@0: const vp8_prob upd = vp8_coef_update_probs [i][j][k][t]; michael@0: michael@0: int s = prev_coef_savings[t]; michael@0: int u = 0; michael@0: michael@0: if (!(cpi->oxcf.error_resilient_mode & michael@0: VPX_ERROR_RESILIENT_PARTITIONS)) michael@0: { michael@0: s = prob_update_savings( michael@0: cpi->frame_branch_ct [i][j][k][t], michael@0: *Pold, newp, upd); michael@0: } michael@0: michael@0: if (s > 0) michael@0: u = 1; michael@0: michael@0: /* Force updates on key frames if the new is different, michael@0: * so that we can be sure we end up with equal probabilities michael@0: * over the prev coef contexts. michael@0: */ michael@0: if ((cpi->oxcf.error_resilient_mode & michael@0: VPX_ERROR_RESILIENT_PARTITIONS) && michael@0: cpi->common.frame_type == KEY_FRAME && newp != *Pold) michael@0: u = 1; michael@0: michael@0: #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING michael@0: cpi->update_probs[i][j][k][t] = u; michael@0: #else michael@0: vp8_write(w, u, upd); michael@0: #endif michael@0: michael@0: michael@0: #ifdef VP8_ENTROPY_STATS michael@0: ++ tree_update_hist [i][j][k][t] [u]; michael@0: #endif michael@0: michael@0: if (u) michael@0: { michael@0: /* send/use new probability */ michael@0: michael@0: *Pold = newp; michael@0: #if !(CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING) michael@0: vp8_write_literal(w, newp, 8); michael@0: #endif michael@0: michael@0: savings += s; michael@0: michael@0: } michael@0: michael@0: } michael@0: while (++t < ENTROPY_NODES); michael@0: michael@0: /* Accum token counts for generation of default statistics */ michael@0: #ifdef VP8_ENTROPY_STATS michael@0: t = 0; michael@0: michael@0: do michael@0: { michael@0: context_counters [i][j][k][t] += cpi->coef_counts [i][j][k][t]; michael@0: } michael@0: while (++t < MAX_ENTROPY_TOKENS); michael@0: michael@0: #endif michael@0: michael@0: } michael@0: while (++k < PREV_COEF_CONTEXTS); michael@0: } michael@0: while (++j < COEF_BANDS); michael@0: } michael@0: while (++i < BLOCK_TYPES); michael@0: michael@0: } michael@0: michael@0: #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING michael@0: static void pack_coef_probs(VP8_COMP *cpi) michael@0: { michael@0: int i = 0; michael@0: vp8_writer *const w = cpi->bc; michael@0: michael@0: do michael@0: { michael@0: int j = 0; michael@0: michael@0: do michael@0: { michael@0: int k = 0; michael@0: michael@0: do michael@0: { michael@0: int t = 0; /* token/prob index */ michael@0: michael@0: do michael@0: { michael@0: const vp8_prob newp = cpi->common.fc.coef_probs [i][j][k][t]; michael@0: const vp8_prob upd = vp8_coef_update_probs [i][j][k][t]; michael@0: michael@0: const char u = cpi->update_probs[i][j][k][t] ; michael@0: michael@0: vp8_write(w, u, upd); michael@0: michael@0: if (u) michael@0: { michael@0: /* send/use new probability */ michael@0: vp8_write_literal(w, newp, 8); michael@0: } michael@0: } michael@0: while (++t < ENTROPY_NODES); michael@0: } michael@0: while (++k < PREV_COEF_CONTEXTS); michael@0: } michael@0: while (++j < COEF_BANDS); michael@0: } michael@0: while (++i < BLOCK_TYPES); michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PACKET_TESTING michael@0: FILE *vpxlogc = 0; michael@0: #endif michael@0: michael@0: static void put_delta_q(vp8_writer *bc, int delta_q) michael@0: { michael@0: if (delta_q != 0) michael@0: { michael@0: vp8_write_bit(bc, 1); michael@0: vp8_write_literal(bc, abs(delta_q), 4); michael@0: michael@0: if (delta_q < 0) michael@0: vp8_write_bit(bc, 1); michael@0: else michael@0: vp8_write_bit(bc, 0); michael@0: } michael@0: else michael@0: vp8_write_bit(bc, 0); michael@0: } michael@0: michael@0: void vp8_pack_bitstream(VP8_COMP *cpi, unsigned char *dest, unsigned char * dest_end, unsigned long *size) michael@0: { michael@0: int i, j; michael@0: VP8_HEADER oh; michael@0: VP8_COMMON *const pc = & cpi->common; michael@0: vp8_writer *const bc = cpi->bc; michael@0: MACROBLOCKD *const xd = & cpi->mb.e_mbd; michael@0: int extra_bytes_packed = 0; michael@0: michael@0: unsigned char *cx_data = dest; michael@0: unsigned char *cx_data_end = dest_end; michael@0: const int *mb_feature_data_bits; michael@0: michael@0: oh.show_frame = (int) pc->show_frame; michael@0: oh.type = (int)pc->frame_type; michael@0: oh.version = pc->version; michael@0: oh.first_partition_length_in_bytes = 0; michael@0: michael@0: mb_feature_data_bits = vp8_mb_feature_data_bits; michael@0: michael@0: bc[0].error = &pc->error; michael@0: michael@0: validate_buffer(cx_data, 3, cx_data_end, &cpi->common.error); michael@0: cx_data += 3; michael@0: michael@0: #if defined(SECTIONBITS_OUTPUT) michael@0: Sectionbits[active_section = 1] += sizeof(VP8_HEADER) * 8 * 256; michael@0: #endif michael@0: michael@0: /* every keyframe send startcode, width, height, scale factor, clamp michael@0: * and color type michael@0: */ michael@0: if (oh.type == KEY_FRAME) michael@0: { michael@0: int v; michael@0: michael@0: validate_buffer(cx_data, 7, cx_data_end, &cpi->common.error); michael@0: michael@0: /* Start / synch code */ michael@0: cx_data[0] = 0x9D; michael@0: cx_data[1] = 0x01; michael@0: cx_data[2] = 0x2a; michael@0: michael@0: v = (pc->horiz_scale << 14) | pc->Width; michael@0: cx_data[3] = v; michael@0: cx_data[4] = v >> 8; michael@0: michael@0: v = (pc->vert_scale << 14) | pc->Height; michael@0: cx_data[5] = v; michael@0: cx_data[6] = v >> 8; michael@0: michael@0: michael@0: extra_bytes_packed = 7; michael@0: cx_data += extra_bytes_packed ; michael@0: michael@0: vp8_start_encode(bc, cx_data, cx_data_end); michael@0: michael@0: /* signal clr type */ michael@0: vp8_write_bit(bc, 0); michael@0: vp8_write_bit(bc, pc->clamp_type); michael@0: michael@0: } michael@0: else michael@0: vp8_start_encode(bc, cx_data, cx_data_end); michael@0: michael@0: michael@0: /* Signal whether or not Segmentation is enabled */ michael@0: vp8_write_bit(bc, xd->segmentation_enabled); michael@0: michael@0: /* Indicate which features are enabled */ michael@0: if (xd->segmentation_enabled) michael@0: { michael@0: /* Signal whether or not the segmentation map is being updated. */ michael@0: vp8_write_bit(bc, xd->update_mb_segmentation_map); michael@0: vp8_write_bit(bc, xd->update_mb_segmentation_data); michael@0: michael@0: if (xd->update_mb_segmentation_data) michael@0: { michael@0: signed char Data; michael@0: michael@0: vp8_write_bit(bc, xd->mb_segement_abs_delta); michael@0: michael@0: /* For each segmentation feature (Quant and loop filter level) */ michael@0: for (i = 0; i < MB_LVL_MAX; i++) michael@0: { michael@0: /* For each of the segments */ michael@0: for (j = 0; j < MAX_MB_SEGMENTS; j++) michael@0: { michael@0: Data = xd->segment_feature_data[i][j]; michael@0: michael@0: /* Frame level data */ michael@0: if (Data) michael@0: { michael@0: vp8_write_bit(bc, 1); michael@0: michael@0: if (Data < 0) michael@0: { michael@0: Data = - Data; michael@0: vp8_write_literal(bc, Data, mb_feature_data_bits[i]); michael@0: vp8_write_bit(bc, 1); michael@0: } michael@0: else michael@0: { michael@0: vp8_write_literal(bc, Data, mb_feature_data_bits[i]); michael@0: vp8_write_bit(bc, 0); michael@0: } michael@0: } michael@0: else michael@0: vp8_write_bit(bc, 0); michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (xd->update_mb_segmentation_map) michael@0: { michael@0: /* Write the probs used to decode the segment id for each mb */ michael@0: for (i = 0; i < MB_FEATURE_TREE_PROBS; i++) michael@0: { michael@0: int Data = xd->mb_segment_tree_probs[i]; michael@0: michael@0: if (Data != 255) michael@0: { michael@0: vp8_write_bit(bc, 1); michael@0: vp8_write_literal(bc, Data, 8); michael@0: } michael@0: else michael@0: vp8_write_bit(bc, 0); michael@0: } michael@0: } michael@0: } michael@0: michael@0: vp8_write_bit(bc, pc->filter_type); michael@0: vp8_write_literal(bc, pc->filter_level, 6); michael@0: vp8_write_literal(bc, pc->sharpness_level, 3); michael@0: michael@0: /* Write out loop filter deltas applied at the MB level based on mode michael@0: * or ref frame (if they are enabled). michael@0: */ michael@0: vp8_write_bit(bc, xd->mode_ref_lf_delta_enabled); michael@0: michael@0: if (xd->mode_ref_lf_delta_enabled) michael@0: { michael@0: /* Do the deltas need to be updated */ michael@0: int send_update = xd->mode_ref_lf_delta_update michael@0: || cpi->oxcf.error_resilient_mode; michael@0: michael@0: vp8_write_bit(bc, send_update); michael@0: if (send_update) michael@0: { michael@0: int Data; michael@0: michael@0: /* Send update */ michael@0: for (i = 0; i < MAX_REF_LF_DELTAS; i++) michael@0: { michael@0: Data = xd->ref_lf_deltas[i]; michael@0: michael@0: /* Frame level data */ michael@0: if (xd->ref_lf_deltas[i] != xd->last_ref_lf_deltas[i] michael@0: || cpi->oxcf.error_resilient_mode) michael@0: { michael@0: xd->last_ref_lf_deltas[i] = xd->ref_lf_deltas[i]; michael@0: vp8_write_bit(bc, 1); michael@0: michael@0: if (Data > 0) michael@0: { michael@0: vp8_write_literal(bc, (Data & 0x3F), 6); michael@0: vp8_write_bit(bc, 0); /* sign */ michael@0: } michael@0: else michael@0: { michael@0: Data = -Data; michael@0: vp8_write_literal(bc, (Data & 0x3F), 6); michael@0: vp8_write_bit(bc, 1); /* sign */ michael@0: } michael@0: } michael@0: else michael@0: vp8_write_bit(bc, 0); michael@0: } michael@0: michael@0: /* Send update */ michael@0: for (i = 0; i < MAX_MODE_LF_DELTAS; i++) michael@0: { michael@0: Data = xd->mode_lf_deltas[i]; michael@0: michael@0: if (xd->mode_lf_deltas[i] != xd->last_mode_lf_deltas[i] michael@0: || cpi->oxcf.error_resilient_mode) michael@0: { michael@0: xd->last_mode_lf_deltas[i] = xd->mode_lf_deltas[i]; michael@0: vp8_write_bit(bc, 1); michael@0: michael@0: if (Data > 0) michael@0: { michael@0: vp8_write_literal(bc, (Data & 0x3F), 6); michael@0: vp8_write_bit(bc, 0); /* sign */ michael@0: } michael@0: else michael@0: { michael@0: Data = -Data; michael@0: vp8_write_literal(bc, (Data & 0x3F), 6); michael@0: vp8_write_bit(bc, 1); /* sign */ michael@0: } michael@0: } michael@0: else michael@0: vp8_write_bit(bc, 0); michael@0: } michael@0: } michael@0: } michael@0: michael@0: /* signal here is multi token partition is enabled */ michael@0: vp8_write_literal(bc, pc->multi_token_partition, 2); michael@0: michael@0: /* Frame Qbaseline quantizer index */ michael@0: vp8_write_literal(bc, pc->base_qindex, 7); michael@0: michael@0: /* Transmit Dc, Second order and Uv quantizer delta information */ michael@0: put_delta_q(bc, pc->y1dc_delta_q); michael@0: put_delta_q(bc, pc->y2dc_delta_q); michael@0: put_delta_q(bc, pc->y2ac_delta_q); michael@0: put_delta_q(bc, pc->uvdc_delta_q); michael@0: put_delta_q(bc, pc->uvac_delta_q); michael@0: michael@0: /* When there is a key frame all reference buffers are updated using michael@0: * the new key frame michael@0: */ michael@0: if (pc->frame_type != KEY_FRAME) michael@0: { michael@0: /* Should the GF or ARF be updated using the transmitted frame michael@0: * or buffer michael@0: */ michael@0: vp8_write_bit(bc, pc->refresh_golden_frame); michael@0: vp8_write_bit(bc, pc->refresh_alt_ref_frame); michael@0: michael@0: /* If not being updated from current frame should either GF or ARF michael@0: * be updated from another buffer michael@0: */ michael@0: if (!pc->refresh_golden_frame) michael@0: vp8_write_literal(bc, pc->copy_buffer_to_gf, 2); michael@0: michael@0: if (!pc->refresh_alt_ref_frame) michael@0: vp8_write_literal(bc, pc->copy_buffer_to_arf, 2); michael@0: michael@0: /* Indicate reference frame sign bias for Golden and ARF frames michael@0: * (always 0 for last frame buffer) michael@0: */ michael@0: vp8_write_bit(bc, pc->ref_frame_sign_bias[GOLDEN_FRAME]); michael@0: vp8_write_bit(bc, pc->ref_frame_sign_bias[ALTREF_FRAME]); michael@0: } michael@0: michael@0: #if !(CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING) michael@0: if (cpi->oxcf.error_resilient_mode & VPX_ERROR_RESILIENT_PARTITIONS) michael@0: { michael@0: if (pc->frame_type == KEY_FRAME) michael@0: pc->refresh_entropy_probs = 1; michael@0: else michael@0: pc->refresh_entropy_probs = 0; michael@0: } michael@0: #endif michael@0: michael@0: vp8_write_bit(bc, pc->refresh_entropy_probs); michael@0: michael@0: if (pc->frame_type != KEY_FRAME) michael@0: vp8_write_bit(bc, pc->refresh_last_frame); michael@0: michael@0: #ifdef VP8_ENTROPY_STATS michael@0: michael@0: if (pc->frame_type == INTER_FRAME) michael@0: active_section = 0; michael@0: else michael@0: active_section = 7; michael@0: michael@0: #endif michael@0: michael@0: vp8_clear_system_state(); michael@0: michael@0: #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING michael@0: pack_coef_probs(cpi); michael@0: #else michael@0: if (pc->refresh_entropy_probs == 0) michael@0: { michael@0: /* save a copy for later refresh */ michael@0: vpx_memcpy(&cpi->common.lfc, &cpi->common.fc, sizeof(cpi->common.fc)); michael@0: } michael@0: michael@0: vp8_update_coef_probs(cpi); michael@0: #endif michael@0: michael@0: #ifdef VP8_ENTROPY_STATS michael@0: active_section = 2; michael@0: #endif michael@0: michael@0: /* Write out the mb_no_coeff_skip flag */ michael@0: vp8_write_bit(bc, pc->mb_no_coeff_skip); michael@0: michael@0: if (pc->frame_type == KEY_FRAME) michael@0: { michael@0: write_kfmodes(cpi); michael@0: michael@0: #ifdef VP8_ENTROPY_STATS michael@0: active_section = 8; michael@0: #endif michael@0: } michael@0: else michael@0: { michael@0: pack_inter_mode_mvs(cpi); michael@0: michael@0: #ifdef VP8_ENTROPY_STATS michael@0: active_section = 1; michael@0: #endif michael@0: } michael@0: michael@0: vp8_stop_encode(bc); michael@0: michael@0: cx_data += bc->pos; michael@0: michael@0: oh.first_partition_length_in_bytes = cpi->bc->pos; michael@0: michael@0: /* update frame tag */ michael@0: { michael@0: int v = (oh.first_partition_length_in_bytes << 5) | michael@0: (oh.show_frame << 4) | michael@0: (oh.version << 1) | michael@0: oh.type; michael@0: michael@0: dest[0] = v; michael@0: dest[1] = v >> 8; michael@0: dest[2] = v >> 16; michael@0: } michael@0: michael@0: *size = VP8_HEADER_SIZE + extra_bytes_packed + cpi->bc->pos; michael@0: michael@0: cpi->partition_sz[0] = *size; michael@0: michael@0: #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING michael@0: { michael@0: const int num_part = (1 << pc->multi_token_partition); michael@0: unsigned char * dp = cpi->partition_d[0] + cpi->partition_sz[0]; michael@0: michael@0: if (num_part > 1) michael@0: { michael@0: /* write token part sizes (all but last) if more than 1 */ michael@0: validate_buffer(dp, 3 * (num_part - 1), cpi->partition_d_end[0], michael@0: &pc->error); michael@0: michael@0: cpi->partition_sz[0] += 3*(num_part-1); michael@0: michael@0: for(i = 1; i < num_part; i++) michael@0: { michael@0: write_partition_size(dp, cpi->partition_sz[i]); michael@0: dp += 3; michael@0: } michael@0: } michael@0: michael@0: if (!cpi->output_partition) michael@0: { michael@0: /* concatenate partition buffers */ michael@0: for(i = 0; i < num_part; i++) michael@0: { michael@0: vpx_memmove(dp, cpi->partition_d[i+1], cpi->partition_sz[i+1]); michael@0: cpi->partition_d[i+1] = dp; michael@0: dp += cpi->partition_sz[i+1]; michael@0: } michael@0: } michael@0: michael@0: /* update total size */ michael@0: *size = 0; michael@0: for(i = 0; i < num_part+1; i++) michael@0: { michael@0: *size += cpi->partition_sz[i]; michael@0: } michael@0: } michael@0: #else michael@0: if (pc->multi_token_partition != ONE_PARTITION) michael@0: { michael@0: int num_part = 1 << pc->multi_token_partition; michael@0: michael@0: /* partition size table at the end of first partition */ michael@0: cpi->partition_sz[0] += 3 * (num_part - 1); michael@0: *size += 3 * (num_part - 1); michael@0: michael@0: validate_buffer(cx_data, 3 * (num_part - 1), cx_data_end, michael@0: &pc->error); michael@0: michael@0: for(i = 1; i < num_part + 1; i++) michael@0: { michael@0: cpi->bc[i].error = &pc->error; michael@0: } michael@0: michael@0: pack_tokens_into_partitions(cpi, cx_data + 3 * (num_part - 1), michael@0: cx_data_end, num_part); michael@0: michael@0: for(i = 1; i < num_part; i++) michael@0: { michael@0: cpi->partition_sz[i] = cpi->bc[i].pos; michael@0: write_partition_size(cx_data, cpi->partition_sz[i]); michael@0: cx_data += 3; michael@0: *size += cpi->partition_sz[i]; /* add to total */ michael@0: } michael@0: michael@0: /* add last partition to total size */ michael@0: cpi->partition_sz[i] = cpi->bc[i].pos; michael@0: *size += cpi->partition_sz[i]; michael@0: } michael@0: else michael@0: { michael@0: bc[1].error = &pc->error; michael@0: michael@0: vp8_start_encode(&cpi->bc[1], cx_data, cx_data_end); michael@0: michael@0: #if CONFIG_MULTITHREAD michael@0: if (cpi->b_multi_threaded) michael@0: pack_mb_row_tokens(cpi, &cpi->bc[1]); michael@0: else michael@0: #endif michael@0: pack_tokens(&cpi->bc[1], cpi->tok, cpi->tok_count); michael@0: michael@0: vp8_stop_encode(&cpi->bc[1]); michael@0: michael@0: *size += cpi->bc[1].pos; michael@0: cpi->partition_sz[1] = cpi->bc[1].pos; michael@0: } michael@0: #endif michael@0: } michael@0: michael@0: #ifdef VP8_ENTROPY_STATS michael@0: void print_tree_update_probs() michael@0: { michael@0: int i, j, k, l; michael@0: FILE *f = fopen("context.c", "a"); michael@0: int Sum; michael@0: fprintf(f, "\n/* Update probabilities for token entropy tree. */\n\n"); michael@0: fprintf(f, "const vp8_prob tree_update_probs[BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [ENTROPY_NODES] = {\n"); michael@0: michael@0: for (i = 0; i < BLOCK_TYPES; i++) michael@0: { michael@0: fprintf(f, " { \n"); michael@0: michael@0: for (j = 0; j < COEF_BANDS; j++) michael@0: { michael@0: fprintf(f, " {\n"); michael@0: michael@0: for (k = 0; k < PREV_COEF_CONTEXTS; k++) michael@0: { michael@0: fprintf(f, " {"); michael@0: michael@0: for (l = 0; l < ENTROPY_NODES; l++) michael@0: { michael@0: Sum = tree_update_hist[i][j][k][l][0] + tree_update_hist[i][j][k][l][1]; michael@0: michael@0: if (Sum > 0) michael@0: { michael@0: if (((tree_update_hist[i][j][k][l][0] * 255) / Sum) > 0) michael@0: fprintf(f, "%3ld, ", (tree_update_hist[i][j][k][l][0] * 255) / Sum); michael@0: else michael@0: fprintf(f, "%3ld, ", 1); michael@0: } michael@0: else michael@0: fprintf(f, "%3ld, ", 128); michael@0: } michael@0: michael@0: fprintf(f, "},\n"); michael@0: } michael@0: michael@0: fprintf(f, " },\n"); michael@0: } michael@0: michael@0: fprintf(f, " },\n"); michael@0: } michael@0: michael@0: fprintf(f, "};\n"); michael@0: fclose(f); michael@0: } michael@0: #endif