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/common.h" michael@0: #include "encodemv.h" michael@0: #include "vp8/common/entropymode.h" michael@0: #include "vp8/common/systemdependent.h" michael@0: michael@0: #include michael@0: michael@0: #ifdef VP8_ENTROPY_STATS michael@0: extern unsigned int active_section; michael@0: #endif michael@0: michael@0: static void encode_mvcomponent( michael@0: vp8_writer *const w, michael@0: const int v, michael@0: const struct mv_context *mvc michael@0: ) michael@0: { michael@0: const vp8_prob *p = mvc->prob; michael@0: const int x = v < 0 ? -v : v; michael@0: michael@0: if (x < mvnum_short) /* Small */ michael@0: { michael@0: vp8_write(w, 0, p [mvpis_short]); michael@0: vp8_treed_write(w, vp8_small_mvtree, p + MVPshort, x, 3); michael@0: michael@0: if (!x) michael@0: return; /* no sign bit */ michael@0: } michael@0: else /* Large */ michael@0: { michael@0: int i = 0; michael@0: michael@0: vp8_write(w, 1, p [mvpis_short]); michael@0: michael@0: do michael@0: vp8_write(w, (x >> i) & 1, p [MVPbits + i]); michael@0: michael@0: while (++i < 3); michael@0: michael@0: i = mvlong_width - 1; /* Skip bit 3, which is sometimes implicit */ michael@0: michael@0: do michael@0: vp8_write(w, (x >> i) & 1, p [MVPbits + i]); michael@0: michael@0: while (--i > 3); michael@0: michael@0: if (x & 0xFFF0) michael@0: vp8_write(w, (x >> 3) & 1, p [MVPbits + 3]); michael@0: } michael@0: michael@0: vp8_write(w, v < 0, p [MVPsign]); michael@0: } michael@0: #if 0 michael@0: static int max_mv_r = 0; michael@0: static int max_mv_c = 0; michael@0: #endif michael@0: void vp8_encode_motion_vector(vp8_writer *w, const MV *mv, const MV_CONTEXT *mvc) michael@0: { michael@0: michael@0: #if 0 michael@0: { michael@0: if (abs(mv->row >> 1) > max_mv_r) michael@0: { michael@0: FILE *f = fopen("maxmv.stt", "a"); michael@0: max_mv_r = abs(mv->row >> 1); michael@0: fprintf(f, "New Mv Row Max %6d\n", (mv->row >> 1)); michael@0: michael@0: if ((abs(mv->row) / 2) != max_mv_r) michael@0: fprintf(f, "MV Row conversion error %6d\n", abs(mv->row) / 2); michael@0: michael@0: fclose(f); michael@0: } michael@0: michael@0: if (abs(mv->col >> 1) > max_mv_c) michael@0: { michael@0: FILE *f = fopen("maxmv.stt", "a"); michael@0: fprintf(f, "New Mv Col Max %6d\n", (mv->col >> 1)); michael@0: max_mv_c = abs(mv->col >> 1); michael@0: fclose(f); michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: encode_mvcomponent(w, mv->row >> 1, &mvc[0]); michael@0: encode_mvcomponent(w, mv->col >> 1, &mvc[1]); michael@0: } michael@0: michael@0: michael@0: static unsigned int cost_mvcomponent(const int v, const struct mv_context *mvc) michael@0: { michael@0: const vp8_prob *p = mvc->prob; michael@0: const int x = v; michael@0: unsigned int cost; michael@0: michael@0: if (x < mvnum_short) michael@0: { michael@0: cost = vp8_cost_zero(p [mvpis_short]) michael@0: + vp8_treed_cost(vp8_small_mvtree, p + MVPshort, x, 3); michael@0: michael@0: if (!x) michael@0: return cost; michael@0: } michael@0: else michael@0: { michael@0: int i = 0; michael@0: cost = vp8_cost_one(p [mvpis_short]); michael@0: michael@0: do michael@0: cost += vp8_cost_bit(p [MVPbits + i], (x >> i) & 1); michael@0: michael@0: while (++i < 3); michael@0: michael@0: i = mvlong_width - 1; /* Skip bit 3, which is sometimes implicit */ michael@0: michael@0: do michael@0: cost += vp8_cost_bit(p [MVPbits + i], (x >> i) & 1); michael@0: michael@0: while (--i > 3); michael@0: michael@0: if (x & 0xFFF0) michael@0: cost += vp8_cost_bit(p [MVPbits + 3], (x >> 3) & 1); michael@0: } michael@0: michael@0: return cost; /* + vp8_cost_bit( p [MVPsign], v < 0); */ michael@0: } michael@0: michael@0: void vp8_build_component_cost_table(int *mvcost[2], const MV_CONTEXT *mvc, int mvc_flag[2]) michael@0: { michael@0: int i = 1; michael@0: unsigned int cost0 = 0; michael@0: unsigned int cost1 = 0; michael@0: michael@0: vp8_clear_system_state(); michael@0: michael@0: i = 1; michael@0: michael@0: if (mvc_flag[0]) michael@0: { michael@0: mvcost [0] [0] = cost_mvcomponent(0, &mvc[0]); michael@0: michael@0: do michael@0: { michael@0: cost0 = cost_mvcomponent(i, &mvc[0]); michael@0: michael@0: mvcost [0] [i] = cost0 + vp8_cost_zero(mvc[0].prob[MVPsign]); michael@0: mvcost [0] [-i] = cost0 + vp8_cost_one(mvc[0].prob[MVPsign]); michael@0: } michael@0: while (++i <= mv_max); michael@0: } michael@0: michael@0: i = 1; michael@0: michael@0: if (mvc_flag[1]) michael@0: { michael@0: mvcost [1] [0] = cost_mvcomponent(0, &mvc[1]); michael@0: michael@0: do michael@0: { michael@0: cost1 = cost_mvcomponent(i, &mvc[1]); michael@0: michael@0: mvcost [1] [i] = cost1 + vp8_cost_zero(mvc[1].prob[MVPsign]); michael@0: mvcost [1] [-i] = cost1 + vp8_cost_one(mvc[1].prob[MVPsign]); michael@0: } michael@0: while (++i <= mv_max); michael@0: } michael@0: } michael@0: michael@0: michael@0: /* Motion vector probability table update depends on benefit. michael@0: * Small correction allows for the fact that an update to an MV probability michael@0: * may have benefit in subsequent frames as well as the current one. michael@0: */ michael@0: #define MV_PROB_UPDATE_CORRECTION -1 michael@0: michael@0: michael@0: static void calc_prob(vp8_prob *p, const unsigned int ct[2]) michael@0: { michael@0: const unsigned int tot = ct[0] + ct[1]; michael@0: michael@0: if (tot) michael@0: { michael@0: const vp8_prob x = ((ct[0] * 255) / tot) & -2; michael@0: *p = x ? x : 1; michael@0: } michael@0: } michael@0: michael@0: static void update( michael@0: vp8_writer *const w, michael@0: const unsigned int ct[2], michael@0: vp8_prob *const cur_p, michael@0: const vp8_prob new_p, michael@0: const vp8_prob update_p, michael@0: int *updated michael@0: ) michael@0: { michael@0: const int cur_b = vp8_cost_branch(ct, *cur_p); michael@0: const int new_b = vp8_cost_branch(ct, new_p); michael@0: const int cost = 7 + MV_PROB_UPDATE_CORRECTION + ((vp8_cost_one(update_p) - vp8_cost_zero(update_p) + 128) >> 8); michael@0: michael@0: if (cur_b - new_b > cost) michael@0: { michael@0: *cur_p = new_p; michael@0: vp8_write(w, 1, update_p); michael@0: vp8_write_literal(w, new_p >> 1, 7); michael@0: *updated = 1; michael@0: michael@0: } michael@0: else michael@0: vp8_write(w, 0, update_p); michael@0: } michael@0: michael@0: static void write_component_probs( michael@0: vp8_writer *const w, michael@0: struct mv_context *cur_mvc, michael@0: const struct mv_context *default_mvc_, michael@0: const struct mv_context *update_mvc, michael@0: const unsigned int events [MVvals], michael@0: unsigned int rc, michael@0: int *updated michael@0: ) michael@0: { michael@0: vp8_prob *Pcur = cur_mvc->prob; michael@0: const vp8_prob *default_mvc = default_mvc_->prob; michael@0: const vp8_prob *Pupdate = update_mvc->prob; michael@0: unsigned int is_short_ct[2], sign_ct[2]; michael@0: michael@0: unsigned int bit_ct [mvlong_width] [2]; michael@0: michael@0: unsigned int short_ct [mvnum_short]; michael@0: unsigned int short_bct [mvnum_short-1] [2]; michael@0: michael@0: vp8_prob Pnew [MVPcount]; michael@0: michael@0: (void) rc; michael@0: vp8_copy_array(Pnew, default_mvc, MVPcount); michael@0: michael@0: vp8_zero(is_short_ct) michael@0: vp8_zero(sign_ct) michael@0: vp8_zero(bit_ct) michael@0: vp8_zero(short_ct) michael@0: vp8_zero(short_bct) michael@0: michael@0: michael@0: /* j=0 */ michael@0: { michael@0: const int c = events [mv_max]; michael@0: michael@0: is_short_ct [0] += c; /* Short vector */ michael@0: short_ct [0] += c; /* Magnitude distribution */ michael@0: } michael@0: michael@0: /* j: 1 ~ mv_max (1023) */ michael@0: { michael@0: int j = 1; michael@0: michael@0: do michael@0: { michael@0: const int c1 = events [mv_max + j]; /* positive */ michael@0: const int c2 = events [mv_max - j]; /* negative */ michael@0: const int c = c1 + c2; michael@0: int a = j; michael@0: michael@0: sign_ct [0] += c1; michael@0: sign_ct [1] += c2; michael@0: michael@0: if (a < mvnum_short) michael@0: { michael@0: is_short_ct [0] += c; /* Short vector */ michael@0: short_ct [a] += c; /* Magnitude distribution */ michael@0: } michael@0: else michael@0: { michael@0: int k = mvlong_width - 1; michael@0: is_short_ct [1] += c; /* Long vector */ michael@0: michael@0: /* bit 3 not always encoded. */ michael@0: do michael@0: bit_ct [k] [(a >> k) & 1] += c; michael@0: michael@0: while (--k >= 0); michael@0: } michael@0: } michael@0: while (++j <= mv_max); michael@0: } michael@0: michael@0: calc_prob(Pnew + mvpis_short, is_short_ct); michael@0: michael@0: calc_prob(Pnew + MVPsign, sign_ct); michael@0: michael@0: { michael@0: vp8_prob p [mvnum_short - 1]; /* actually only need branch ct */ michael@0: int j = 0; michael@0: michael@0: vp8_tree_probs_from_distribution( michael@0: 8, vp8_small_mvencodings, vp8_small_mvtree, michael@0: p, short_bct, short_ct, michael@0: 256, 1 michael@0: ); michael@0: michael@0: do michael@0: calc_prob(Pnew + MVPshort + j, short_bct[j]); michael@0: michael@0: while (++j < mvnum_short - 1); michael@0: } michael@0: michael@0: { michael@0: int j = 0; michael@0: michael@0: do michael@0: calc_prob(Pnew + MVPbits + j, bit_ct[j]); michael@0: michael@0: while (++j < mvlong_width); michael@0: } michael@0: michael@0: update(w, is_short_ct, Pcur + mvpis_short, Pnew[mvpis_short], *Pupdate++, updated); michael@0: michael@0: update(w, sign_ct, Pcur + MVPsign, Pnew[MVPsign], *Pupdate++, updated); michael@0: michael@0: { michael@0: const vp8_prob *const new_p = Pnew + MVPshort; michael@0: vp8_prob *const cur_p = Pcur + MVPshort; michael@0: michael@0: int j = 0; michael@0: michael@0: do michael@0: michael@0: update(w, short_bct[j], cur_p + j, new_p[j], *Pupdate++, updated); michael@0: michael@0: while (++j < mvnum_short - 1); michael@0: } michael@0: michael@0: { michael@0: const vp8_prob *const new_p = Pnew + MVPbits; michael@0: vp8_prob *const cur_p = Pcur + MVPbits; michael@0: michael@0: int j = 0; michael@0: michael@0: do michael@0: michael@0: update(w, bit_ct[j], cur_p + j, new_p[j], *Pupdate++, updated); michael@0: michael@0: while (++j < mvlong_width); michael@0: } michael@0: } michael@0: michael@0: void vp8_write_mvprobs(VP8_COMP *cpi) michael@0: { michael@0: vp8_writer *const w = cpi->bc; michael@0: MV_CONTEXT *mvc = cpi->common.fc.mvc; michael@0: int flags[2] = {0, 0}; michael@0: #ifdef VP8_ENTROPY_STATS michael@0: active_section = 4; michael@0: #endif michael@0: write_component_probs( michael@0: w, &mvc[0], &vp8_default_mv_context[0], &vp8_mv_update_probs[0], michael@0: cpi->mb.MVcount[0], 0, &flags[0] michael@0: ); michael@0: write_component_probs( michael@0: w, &mvc[1], &vp8_default_mv_context[1], &vp8_mv_update_probs[1], michael@0: cpi->mb.MVcount[1], 1, &flags[1] michael@0: ); michael@0: michael@0: if (flags[0] || flags[1]) michael@0: vp8_build_component_cost_table(cpi->mb.mvcost, (const MV_CONTEXT *) cpi->common.fc.mvc, flags); michael@0: michael@0: #ifdef VP8_ENTROPY_STATS michael@0: active_section = 5; michael@0: #endif michael@0: }