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: #include michael@0: michael@0: #include "vp9/common/vp9_common.h" michael@0: #include "vp9/common/vp9_entropymode.h" michael@0: #include "vp9/common/vp9_systemdependent.h" michael@0: #include "vp9/encoder/vp9_encodemv.h" michael@0: michael@0: michael@0: #ifdef ENTROPY_STATS michael@0: extern unsigned int active_section; michael@0: #endif michael@0: michael@0: static void encode_mv_component(vp9_writer* w, int comp, michael@0: const nmv_component* mvcomp, int usehp) { michael@0: int offset; michael@0: const int sign = comp < 0; michael@0: const int mag = sign ? -comp : comp; michael@0: const int mv_class = vp9_get_mv_class(mag - 1, &offset); michael@0: const int d = offset >> 3; // int mv data michael@0: const int fr = (offset >> 1) & 3; // fractional mv data michael@0: const int hp = offset & 1; // high precision mv data michael@0: michael@0: assert(comp != 0); michael@0: michael@0: // Sign michael@0: vp9_write(w, sign, mvcomp->sign); michael@0: michael@0: // Class michael@0: write_token(w, vp9_mv_class_tree, mvcomp->classes, michael@0: &vp9_mv_class_encodings[mv_class]); michael@0: michael@0: // Integer bits michael@0: if (mv_class == MV_CLASS_0) { michael@0: write_token(w, vp9_mv_class0_tree, mvcomp->class0, michael@0: &vp9_mv_class0_encodings[d]); michael@0: } else { michael@0: int i; michael@0: const int n = mv_class + CLASS0_BITS - 1; // number of bits michael@0: for (i = 0; i < n; ++i) michael@0: vp9_write(w, (d >> i) & 1, mvcomp->bits[i]); michael@0: } michael@0: michael@0: // Fractional bits michael@0: write_token(w, vp9_mv_fp_tree, michael@0: mv_class == MV_CLASS_0 ? mvcomp->class0_fp[d] : mvcomp->fp, michael@0: &vp9_mv_fp_encodings[fr]); michael@0: michael@0: // High precision bit michael@0: if (usehp) michael@0: vp9_write(w, hp, michael@0: mv_class == MV_CLASS_0 ? mvcomp->class0_hp : mvcomp->hp); michael@0: } michael@0: michael@0: michael@0: static void build_nmv_component_cost_table(int *mvcost, michael@0: const nmv_component* const mvcomp, michael@0: int usehp) { michael@0: int i, v; michael@0: int sign_cost[2], class_cost[MV_CLASSES], class0_cost[CLASS0_SIZE]; michael@0: int bits_cost[MV_OFFSET_BITS][2]; michael@0: int class0_fp_cost[CLASS0_SIZE][4], fp_cost[4]; michael@0: int class0_hp_cost[2], hp_cost[2]; michael@0: michael@0: sign_cost[0] = vp9_cost_zero(mvcomp->sign); michael@0: sign_cost[1] = vp9_cost_one(mvcomp->sign); michael@0: vp9_cost_tokens(class_cost, mvcomp->classes, vp9_mv_class_tree); michael@0: vp9_cost_tokens(class0_cost, mvcomp->class0, vp9_mv_class0_tree); michael@0: for (i = 0; i < MV_OFFSET_BITS; ++i) { michael@0: bits_cost[i][0] = vp9_cost_zero(mvcomp->bits[i]); michael@0: bits_cost[i][1] = vp9_cost_one(mvcomp->bits[i]); michael@0: } michael@0: michael@0: for (i = 0; i < CLASS0_SIZE; ++i) michael@0: vp9_cost_tokens(class0_fp_cost[i], mvcomp->class0_fp[i], vp9_mv_fp_tree); michael@0: vp9_cost_tokens(fp_cost, mvcomp->fp, vp9_mv_fp_tree); michael@0: michael@0: if (usehp) { michael@0: class0_hp_cost[0] = vp9_cost_zero(mvcomp->class0_hp); michael@0: class0_hp_cost[1] = vp9_cost_one(mvcomp->class0_hp); michael@0: hp_cost[0] = vp9_cost_zero(mvcomp->hp); michael@0: hp_cost[1] = vp9_cost_one(mvcomp->hp); michael@0: } michael@0: mvcost[0] = 0; michael@0: for (v = 1; v <= MV_MAX; ++v) { michael@0: int z, c, o, d, e, f, cost = 0; michael@0: z = v - 1; michael@0: c = vp9_get_mv_class(z, &o); michael@0: cost += class_cost[c]; michael@0: d = (o >> 3); /* int mv data */ michael@0: f = (o >> 1) & 3; /* fractional pel mv data */ michael@0: e = (o & 1); /* high precision mv data */ michael@0: if (c == MV_CLASS_0) { michael@0: cost += class0_cost[d]; michael@0: } else { michael@0: int i, b; michael@0: b = c + CLASS0_BITS - 1; /* number of bits */ michael@0: for (i = 0; i < b; ++i) michael@0: cost += bits_cost[i][((d >> i) & 1)]; michael@0: } michael@0: if (c == MV_CLASS_0) { michael@0: cost += class0_fp_cost[d][f]; michael@0: } else { michael@0: cost += fp_cost[f]; michael@0: } michael@0: if (usehp) { michael@0: if (c == MV_CLASS_0) { michael@0: cost += class0_hp_cost[e]; michael@0: } else { michael@0: cost += hp_cost[e]; michael@0: } michael@0: } michael@0: mvcost[v] = cost + sign_cost[0]; michael@0: mvcost[-v] = cost + sign_cost[1]; michael@0: } michael@0: } michael@0: michael@0: static int update_mv(vp9_writer *w, const unsigned int ct[2], vp9_prob *cur_p, michael@0: vp9_prob upd_p) { michael@0: const vp9_prob new_p = get_binary_prob(ct[0], ct[1]); michael@0: vp9_prob mod_p = new_p | 1; michael@0: const int cur_b = cost_branch256(ct, *cur_p); michael@0: const int mod_b = cost_branch256(ct, mod_p); michael@0: const int cost = 7 * 256 + (vp9_cost_one(upd_p) - vp9_cost_zero(upd_p)); michael@0: if (cur_b - mod_b > cost) { michael@0: *cur_p = mod_p; michael@0: vp9_write(w, 1, upd_p); michael@0: vp9_write_literal(w, mod_p >> 1, 7); michael@0: return 1; michael@0: } else { michael@0: vp9_write(w, 0, upd_p); michael@0: return 0; michael@0: } michael@0: } michael@0: michael@0: static void counts_to_nmv_context( michael@0: nmv_context_counts *nmv_count, michael@0: int usehp, michael@0: unsigned int (*branch_ct_joint)[2], michael@0: unsigned int (*branch_ct_sign)[2], michael@0: unsigned int (*branch_ct_classes)[MV_CLASSES - 1][2], michael@0: unsigned int (*branch_ct_class0)[CLASS0_SIZE - 1][2], michael@0: unsigned int (*branch_ct_bits)[MV_OFFSET_BITS][2], michael@0: unsigned int (*branch_ct_class0_fp)[CLASS0_SIZE][4 - 1][2], michael@0: unsigned int (*branch_ct_fp)[4 - 1][2], michael@0: unsigned int (*branch_ct_class0_hp)[2], michael@0: unsigned int (*branch_ct_hp)[2]) { michael@0: int i, j, k; michael@0: vp9_tree_probs_from_distribution(vp9_mv_joint_tree, branch_ct_joint, michael@0: nmv_count->joints); michael@0: for (i = 0; i < 2; ++i) { michael@0: const uint32_t s0 = nmv_count->comps[i].sign[0]; michael@0: const uint32_t s1 = nmv_count->comps[i].sign[1]; michael@0: michael@0: branch_ct_sign[i][0] = s0; michael@0: branch_ct_sign[i][1] = s1; michael@0: vp9_tree_probs_from_distribution(vp9_mv_class_tree, michael@0: branch_ct_classes[i], michael@0: nmv_count->comps[i].classes); michael@0: vp9_tree_probs_from_distribution(vp9_mv_class0_tree, michael@0: branch_ct_class0[i], michael@0: nmv_count->comps[i].class0); michael@0: for (j = 0; j < MV_OFFSET_BITS; ++j) { michael@0: const uint32_t b0 = nmv_count->comps[i].bits[j][0]; michael@0: const uint32_t b1 = nmv_count->comps[i].bits[j][1]; michael@0: michael@0: branch_ct_bits[i][j][0] = b0; michael@0: branch_ct_bits[i][j][1] = b1; michael@0: } michael@0: } michael@0: for (i = 0; i < 2; ++i) { michael@0: for (k = 0; k < CLASS0_SIZE; ++k) { michael@0: vp9_tree_probs_from_distribution(vp9_mv_fp_tree, michael@0: branch_ct_class0_fp[i][k], michael@0: nmv_count->comps[i].class0_fp[k]); michael@0: } michael@0: vp9_tree_probs_from_distribution(vp9_mv_fp_tree, michael@0: branch_ct_fp[i], michael@0: nmv_count->comps[i].fp); michael@0: } michael@0: if (usehp) { michael@0: for (i = 0; i < 2; ++i) { michael@0: const uint32_t c0_hp0 = nmv_count->comps[i].class0_hp[0]; michael@0: const uint32_t c0_hp1 = nmv_count->comps[i].class0_hp[1]; michael@0: const uint32_t hp0 = nmv_count->comps[i].hp[0]; michael@0: const uint32_t hp1 = nmv_count->comps[i].hp[1]; michael@0: michael@0: branch_ct_class0_hp[i][0] = c0_hp0; michael@0: branch_ct_class0_hp[i][1] = c0_hp1; michael@0: michael@0: branch_ct_hp[i][0] = hp0; michael@0: branch_ct_hp[i][1] = hp1; michael@0: } michael@0: } michael@0: } michael@0: michael@0: void vp9_write_nmv_probs(VP9_COMP* const cpi, int usehp, vp9_writer* const bc) { michael@0: int i, j; michael@0: unsigned int branch_ct_joint[MV_JOINTS - 1][2]; michael@0: unsigned int branch_ct_sign[2][2]; michael@0: unsigned int branch_ct_classes[2][MV_CLASSES - 1][2]; michael@0: unsigned int branch_ct_class0[2][CLASS0_SIZE - 1][2]; michael@0: unsigned int branch_ct_bits[2][MV_OFFSET_BITS][2]; michael@0: unsigned int branch_ct_class0_fp[2][CLASS0_SIZE][4 - 1][2]; michael@0: unsigned int branch_ct_fp[2][4 - 1][2]; michael@0: unsigned int branch_ct_class0_hp[2][2]; michael@0: unsigned int branch_ct_hp[2][2]; michael@0: nmv_context *mvc = &cpi->common.fc.nmvc; michael@0: michael@0: counts_to_nmv_context(&cpi->NMVcount, usehp, michael@0: branch_ct_joint, branch_ct_sign, branch_ct_classes, michael@0: branch_ct_class0, branch_ct_bits, michael@0: branch_ct_class0_fp, branch_ct_fp, michael@0: branch_ct_class0_hp, branch_ct_hp); michael@0: michael@0: for (j = 0; j < MV_JOINTS - 1; ++j) michael@0: update_mv(bc, branch_ct_joint[j], &mvc->joints[j], NMV_UPDATE_PROB); michael@0: michael@0: for (i = 0; i < 2; ++i) { michael@0: update_mv(bc, branch_ct_sign[i], &mvc->comps[i].sign, NMV_UPDATE_PROB); michael@0: for (j = 0; j < MV_CLASSES - 1; ++j) michael@0: update_mv(bc, branch_ct_classes[i][j], &mvc->comps[i].classes[j], michael@0: NMV_UPDATE_PROB); michael@0: michael@0: for (j = 0; j < CLASS0_SIZE - 1; ++j) michael@0: update_mv(bc, branch_ct_class0[i][j], &mvc->comps[i].class0[j], michael@0: NMV_UPDATE_PROB); michael@0: michael@0: for (j = 0; j < MV_OFFSET_BITS; ++j) michael@0: update_mv(bc, branch_ct_bits[i][j], &mvc->comps[i].bits[j], michael@0: NMV_UPDATE_PROB); michael@0: } michael@0: michael@0: for (i = 0; i < 2; ++i) { michael@0: for (j = 0; j < CLASS0_SIZE; ++j) { michael@0: int k; michael@0: for (k = 0; k < 3; ++k) michael@0: update_mv(bc, branch_ct_class0_fp[i][j][k], michael@0: &mvc->comps[i].class0_fp[j][k], NMV_UPDATE_PROB); michael@0: } michael@0: michael@0: for (j = 0; j < 3; ++j) michael@0: update_mv(bc, branch_ct_fp[i][j], &mvc->comps[i].fp[j], NMV_UPDATE_PROB); michael@0: } michael@0: michael@0: if (usehp) { michael@0: for (i = 0; i < 2; ++i) { michael@0: update_mv(bc, branch_ct_class0_hp[i], &mvc->comps[i].class0_hp, michael@0: NMV_UPDATE_PROB); michael@0: update_mv(bc, branch_ct_hp[i], &mvc->comps[i].hp, michael@0: NMV_UPDATE_PROB); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void vp9_encode_mv(VP9_COMP* cpi, vp9_writer* w, michael@0: const MV* mv, const MV* ref, michael@0: const nmv_context* mvctx, int usehp) { michael@0: const MV diff = {mv->row - ref->row, michael@0: mv->col - ref->col}; michael@0: const MV_JOINT_TYPE j = vp9_get_mv_joint(&diff); michael@0: usehp = usehp && vp9_use_mv_hp(ref); michael@0: michael@0: write_token(w, vp9_mv_joint_tree, mvctx->joints, &vp9_mv_joint_encodings[j]); michael@0: if (mv_joint_vertical(j)) michael@0: encode_mv_component(w, diff.row, &mvctx->comps[0], usehp); michael@0: michael@0: if (mv_joint_horizontal(j)) michael@0: encode_mv_component(w, diff.col, &mvctx->comps[1], usehp); michael@0: michael@0: // If auto_mv_step_size is enabled then keep track of the largest michael@0: // motion vector component used. michael@0: if (!cpi->dummy_packing && cpi->sf.auto_mv_step_size) { michael@0: unsigned int maxv = MAX(abs(mv->row), abs(mv->col)) >> 3; michael@0: cpi->max_mv_magnitude = MAX(maxv, cpi->max_mv_magnitude); michael@0: } michael@0: } michael@0: michael@0: void vp9_build_nmv_cost_table(int *mvjoint, michael@0: int *mvcost[2], michael@0: const nmv_context* const mvctx, michael@0: int usehp, michael@0: int mvc_flag_v, michael@0: int mvc_flag_h) { michael@0: vp9_clear_system_state(); michael@0: vp9_cost_tokens(mvjoint, mvctx->joints, vp9_mv_joint_tree); michael@0: if (mvc_flag_v) michael@0: build_nmv_component_cost_table(mvcost[0], &mvctx->comps[0], usehp); michael@0: if (mvc_flag_h) michael@0: build_nmv_component_cost_table(mvcost[1], &mvctx->comps[1], usehp); michael@0: } michael@0: michael@0: static void inc_mvs(int_mv mv[2], int_mv ref[2], int is_compound, michael@0: nmv_context_counts *counts) { michael@0: int i; michael@0: for (i = 0; i < 1 + is_compound; ++i) { michael@0: const MV diff = { mv[i].as_mv.row - ref[i].as_mv.row, michael@0: mv[i].as_mv.col - ref[i].as_mv.col }; michael@0: vp9_inc_mv(&diff, counts); michael@0: } michael@0: } michael@0: michael@0: void vp9_update_mv_count(VP9_COMP *cpi, MACROBLOCK *x, int_mv best_ref_mv[2]) { michael@0: MODE_INFO *mi = x->e_mbd.mi_8x8[0]; michael@0: MB_MODE_INFO *const mbmi = &mi->mbmi; michael@0: const int is_compound = has_second_ref(mbmi); michael@0: michael@0: if (mbmi->sb_type < BLOCK_8X8) { michael@0: const int num_4x4_w = num_4x4_blocks_wide_lookup[mbmi->sb_type]; michael@0: const int num_4x4_h = num_4x4_blocks_high_lookup[mbmi->sb_type]; michael@0: int idx, idy; michael@0: michael@0: for (idy = 0; idy < 2; idy += num_4x4_h) { michael@0: for (idx = 0; idx < 2; idx += num_4x4_w) { michael@0: const int i = idy * 2 + idx; michael@0: if (mi->bmi[i].as_mode == NEWMV) michael@0: inc_mvs(mi->bmi[i].as_mv, best_ref_mv, is_compound, &cpi->NMVcount); michael@0: } michael@0: } michael@0: } else if (mbmi->mode == NEWMV) { michael@0: inc_mvs(mbmi->mv, best_ref_mv, is_compound, &cpi->NMVcount); michael@0: } michael@0: }