2010-05-18 17:58:33 +02:00
|
|
|
/*
|
2010-09-09 14:16:39 +02:00
|
|
|
* Copyright (c) 2010 The WebM project authors. All Rights Reserved.
|
2010-05-18 17:58:33 +02:00
|
|
|
*
|
2010-06-18 18:39:21 +02:00
|
|
|
* Use of this source code is governed by a BSD-style license
|
2010-06-04 22:19:40 +02:00
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
2010-06-18 18:39:21 +02:00
|
|
|
* in the file PATENTS. All contributing project authors may
|
2010-06-04 22:19:40 +02:00
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
2010-05-18 17:58:33 +02:00
|
|
|
*/
|
|
|
|
|
2013-10-04 20:19:06 +02:00
|
|
|
#include <math.h>
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-11-27 22:59:17 +01:00
|
|
|
#include "vp9/common/vp9_common.h"
|
|
|
|
#include "vp9/common/vp9_entropymode.h"
|
2014-03-05 20:57:57 +01:00
|
|
|
|
|
|
|
#include "vp9/encoder/vp9_cost.h"
|
2013-10-04 20:19:06 +02:00
|
|
|
#include "vp9/encoder/vp9_encodemv.h"
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2015-08-31 23:36:35 +02:00
|
|
|
#include "vpx_dsp/vpx_dsp_common.h"
|
|
|
|
|
2013-11-26 01:36:28 +01:00
|
|
|
static struct vp9_token mv_joint_encodings[MV_JOINTS];
|
|
|
|
static struct vp9_token mv_class_encodings[MV_CLASSES];
|
|
|
|
static struct vp9_token mv_fp_encodings[MV_FP_SIZE];
|
|
|
|
static struct vp9_token mv_class0_encodings[CLASS0_SIZE];
|
|
|
|
|
2015-05-15 05:18:45 +02:00
|
|
|
void vp9_entropy_mv_init(void) {
|
2013-11-26 01:36:28 +01:00
|
|
|
vp9_tokens_from_tree(mv_joint_encodings, vp9_mv_joint_tree);
|
|
|
|
vp9_tokens_from_tree(mv_class_encodings, vp9_mv_class_tree);
|
|
|
|
vp9_tokens_from_tree(mv_class0_encodings, vp9_mv_class0_tree);
|
|
|
|
vp9_tokens_from_tree(mv_fp_encodings, vp9_mv_fp_tree);
|
|
|
|
}
|
|
|
|
|
2015-07-20 23:13:38 +02:00
|
|
|
static void encode_mv_component(vpx_writer* w, int comp,
|
2013-05-15 21:37:03 +02:00
|
|
|
const nmv_component* mvcomp, int usehp) {
|
|
|
|
int offset;
|
|
|
|
const int sign = comp < 0;
|
|
|
|
const int mag = sign ? -comp : comp;
|
|
|
|
const int mv_class = vp9_get_mv_class(mag - 1, &offset);
|
|
|
|
const int d = offset >> 3; // int mv data
|
|
|
|
const int fr = (offset >> 1) & 3; // fractional mv data
|
|
|
|
const int hp = offset & 1; // high precision mv data
|
|
|
|
|
|
|
|
assert(comp != 0);
|
|
|
|
|
|
|
|
// Sign
|
2015-07-20 23:13:38 +02:00
|
|
|
vpx_write(w, sign, mvcomp->sign);
|
2013-05-15 21:37:03 +02:00
|
|
|
|
|
|
|
// Class
|
2013-12-11 03:13:29 +01:00
|
|
|
vp9_write_token(w, vp9_mv_class_tree, mvcomp->classes,
|
|
|
|
&mv_class_encodings[mv_class]);
|
2013-05-15 21:37:03 +02:00
|
|
|
|
|
|
|
// Integer bits
|
|
|
|
if (mv_class == MV_CLASS_0) {
|
2013-12-11 03:13:29 +01:00
|
|
|
vp9_write_token(w, vp9_mv_class0_tree, mvcomp->class0,
|
|
|
|
&mv_class0_encodings[d]);
|
2012-07-26 22:42:07 +02:00
|
|
|
} else {
|
2013-05-15 21:37:03 +02:00
|
|
|
int i;
|
|
|
|
const int n = mv_class + CLASS0_BITS - 1; // number of bits
|
|
|
|
for (i = 0; i < n; ++i)
|
2015-07-20 23:13:38 +02:00
|
|
|
vpx_write(w, (d >> i) & 1, mvcomp->bits[i]);
|
2012-07-26 22:42:07 +02:00
|
|
|
}
|
|
|
|
|
2013-05-15 21:37:03 +02:00
|
|
|
// Fractional bits
|
2013-12-11 03:13:29 +01:00
|
|
|
vp9_write_token(w, vp9_mv_fp_tree,
|
|
|
|
mv_class == MV_CLASS_0 ? mvcomp->class0_fp[d] : mvcomp->fp,
|
|
|
|
&mv_fp_encodings[fr]);
|
2013-05-15 21:37:03 +02:00
|
|
|
|
|
|
|
// High precision bit
|
|
|
|
if (usehp)
|
2015-07-20 23:13:38 +02:00
|
|
|
vpx_write(w, hp,
|
2013-05-15 21:37:03 +02:00
|
|
|
mv_class == MV_CLASS_0 ? mvcomp->class0_hp : mvcomp->hp);
|
2012-07-26 22:42:07 +02:00
|
|
|
}
|
|
|
|
|
2013-05-15 21:37:03 +02:00
|
|
|
|
2012-07-26 22:42:07 +02:00
|
|
|
static void build_nmv_component_cost_table(int *mvcost,
|
2012-10-18 01:47:38 +02:00
|
|
|
const nmv_component* const mvcomp,
|
2012-07-26 22:42:07 +02:00
|
|
|
int usehp) {
|
|
|
|
int sign_cost[2], class_cost[MV_CLASSES], class0_cost[CLASS0_SIZE];
|
|
|
|
int bits_cost[MV_OFFSET_BITS][2];
|
2013-11-20 05:18:01 +01:00
|
|
|
int class0_fp_cost[CLASS0_SIZE][MV_FP_SIZE], fp_cost[MV_FP_SIZE];
|
2012-07-26 22:42:07 +02:00
|
|
|
int class0_hp_cost[2], hp_cost[2];
|
2016-03-16 23:10:42 +01:00
|
|
|
int i;
|
|
|
|
int c, o;
|
2012-07-26 22:42:07 +02:00
|
|
|
|
2012-10-31 22:40:53 +01:00
|
|
|
sign_cost[0] = vp9_cost_zero(mvcomp->sign);
|
|
|
|
sign_cost[1] = vp9_cost_one(mvcomp->sign);
|
2012-10-31 01:12:12 +01:00
|
|
|
vp9_cost_tokens(class_cost, mvcomp->classes, vp9_mv_class_tree);
|
|
|
|
vp9_cost_tokens(class0_cost, mvcomp->class0, vp9_mv_class0_tree);
|
2012-07-26 22:42:07 +02:00
|
|
|
for (i = 0; i < MV_OFFSET_BITS; ++i) {
|
2012-10-31 22:40:53 +01:00
|
|
|
bits_cost[i][0] = vp9_cost_zero(mvcomp->bits[i]);
|
|
|
|
bits_cost[i][1] = vp9_cost_one(mvcomp->bits[i]);
|
2012-07-26 22:42:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < CLASS0_SIZE; ++i)
|
2012-10-31 01:12:12 +01:00
|
|
|
vp9_cost_tokens(class0_fp_cost[i], mvcomp->class0_fp[i], vp9_mv_fp_tree);
|
|
|
|
vp9_cost_tokens(fp_cost, mvcomp->fp, vp9_mv_fp_tree);
|
2012-07-26 22:42:07 +02:00
|
|
|
|
2016-03-16 23:10:42 +01:00
|
|
|
// Always build the hp costs to avoid an uninitialized warning from gcc
|
|
|
|
class0_hp_cost[0] = vp9_cost_zero(mvcomp->class0_hp);
|
|
|
|
class0_hp_cost[1] = vp9_cost_one(mvcomp->class0_hp);
|
|
|
|
hp_cost[0] = vp9_cost_zero(mvcomp->hp);
|
|
|
|
hp_cost[1] = vp9_cost_one(mvcomp->hp);
|
|
|
|
|
2012-07-26 22:42:07 +02:00
|
|
|
mvcost[0] = 0;
|
2016-03-16 23:10:42 +01:00
|
|
|
// MV_CLASS_0
|
|
|
|
for (o = 0; o < (CLASS0_SIZE << 3); ++o) {
|
|
|
|
int d, e, f;
|
|
|
|
int cost = class_cost[MV_CLASS_0];
|
|
|
|
int v = o + 1;
|
2012-07-26 22:42:07 +02:00
|
|
|
d = (o >> 3); /* int mv data */
|
|
|
|
f = (o >> 1) & 3; /* fractional pel mv data */
|
2016-03-16 23:10:42 +01:00
|
|
|
cost += class0_cost[d];
|
|
|
|
cost += class0_fp_cost[d][f];
|
2012-07-26 22:42:07 +02:00
|
|
|
if (usehp) {
|
2016-03-16 23:10:42 +01:00
|
|
|
e = (o & 1); /* high precision mv data */
|
|
|
|
cost += class0_hp_cost[e];
|
2012-07-26 22:42:07 +02:00
|
|
|
}
|
|
|
|
mvcost[v] = cost + sign_cost[0];
|
|
|
|
mvcost[-v] = cost + sign_cost[1];
|
|
|
|
}
|
2016-03-16 23:10:42 +01:00
|
|
|
for (c = MV_CLASS_1; c < MV_CLASSES; ++c) {
|
|
|
|
int d;
|
|
|
|
for (d = 0; d < (1 << c); ++d) {
|
|
|
|
int f;
|
|
|
|
int whole_cost = class_cost[c];
|
|
|
|
int b = c + CLASS0_BITS - 1; /* number of bits */
|
|
|
|
for (i = 0; i < b; ++i)
|
|
|
|
whole_cost += bits_cost[i][((d >> i) & 1)];
|
|
|
|
for (f = 0; f < 4; ++f) {
|
|
|
|
int cost = whole_cost + fp_cost[f];
|
|
|
|
int v = (CLASS0_SIZE << (c + 2)) + d * 8 + f * 2 /* + e */ + 1;
|
|
|
|
if (usehp) {
|
|
|
|
mvcost[v] = cost + hp_cost[0] + sign_cost[0];
|
|
|
|
mvcost[-v] = cost + hp_cost[0] + sign_cost[1];
|
|
|
|
if (v + 1 > MV_MAX) break;
|
|
|
|
mvcost[v + 1] = cost + hp_cost[1] + sign_cost[0];
|
|
|
|
mvcost[-v - 1] = cost + hp_cost[1] + sign_cost[1];
|
|
|
|
} else {
|
|
|
|
mvcost[v] = cost + sign_cost[0];
|
|
|
|
mvcost[-v] = cost + sign_cost[1];
|
|
|
|
if (v + 1 > MV_MAX) break;
|
|
|
|
mvcost[v + 1] = cost + sign_cost[0];
|
|
|
|
mvcost[-v - 1] = cost + sign_cost[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-07-26 22:42:07 +02:00
|
|
|
}
|
|
|
|
|
2015-07-20 23:13:38 +02:00
|
|
|
static int update_mv(vpx_writer *w, const unsigned int ct[2], vpx_prob *cur_p,
|
2015-07-20 22:49:15 +02:00
|
|
|
vpx_prob upd_p) {
|
|
|
|
const vpx_prob new_p = get_binary_prob(ct[0], ct[1]) | 1;
|
2013-11-19 23:59:12 +01:00
|
|
|
const int update = cost_branch256(ct, *cur_p) + vp9_cost_zero(upd_p) >
|
2016-01-15 23:32:52 +01:00
|
|
|
cost_branch256(ct, new_p) + vp9_cost_one(upd_p) +
|
|
|
|
(7 << VP9_PROB_COST_SHIFT);
|
2015-07-20 23:13:38 +02:00
|
|
|
vpx_write(w, update, upd_p);
|
2013-11-19 23:59:12 +01:00
|
|
|
if (update) {
|
|
|
|
*cur_p = new_p;
|
2015-07-20 23:13:38 +02:00
|
|
|
vpx_write_literal(w, new_p >> 1, 7);
|
2012-07-26 22:42:07 +02:00
|
|
|
}
|
2013-11-19 23:59:12 +01:00
|
|
|
return update;
|
2012-07-26 22:42:07 +02:00
|
|
|
}
|
|
|
|
|
2015-07-20 23:04:21 +02:00
|
|
|
static void write_mv_update(const vpx_tree_index *tree,
|
2015-07-20 22:49:15 +02:00
|
|
|
vpx_prob probs[/*n - 1*/],
|
2013-11-25 01:20:59 +01:00
|
|
|
const unsigned int counts[/*n - 1*/],
|
2015-07-20 23:13:38 +02:00
|
|
|
int n, vpx_writer *w) {
|
2013-11-25 01:20:59 +01:00
|
|
|
int i;
|
|
|
|
unsigned int branch_ct[32][2];
|
|
|
|
|
|
|
|
// Assuming max number of probabilities <= 32
|
|
|
|
assert(n <= 32);
|
|
|
|
|
|
|
|
vp9_tree_probs_from_distribution(tree, branch_ct, counts);
|
|
|
|
for (i = 0; i < n - 1; ++i)
|
2014-03-04 20:00:11 +01:00
|
|
|
update_mv(w, branch_ct[i], &probs[i], MV_UPDATE_PROB);
|
2013-06-25 00:58:18 +02:00
|
|
|
}
|
|
|
|
|
2015-07-20 23:13:38 +02:00
|
|
|
void vp9_write_nmv_probs(VP9_COMMON *cm, int usehp, vpx_writer *w,
|
2014-11-21 20:11:06 +01:00
|
|
|
nmv_context_counts *const counts) {
|
2012-07-26 22:42:07 +02:00
|
|
|
int i, j;
|
2014-10-22 23:37:38 +02:00
|
|
|
nmv_context *const mvc = &cm->fc->nmvc;
|
2013-06-26 00:19:18 +02:00
|
|
|
|
2013-11-25 01:20:59 +01:00
|
|
|
write_mv_update(vp9_mv_joint_tree, mvc->joints, counts->joints, MV_JOINTS, w);
|
2013-06-26 00:19:18 +02:00
|
|
|
|
2012-07-26 22:42:07 +02:00
|
|
|
for (i = 0; i < 2; ++i) {
|
2013-11-25 01:20:59 +01:00
|
|
|
nmv_component *comp = &mvc->comps[i];
|
|
|
|
nmv_component_counts *comp_counts = &counts->comps[i];
|
|
|
|
|
2014-03-04 20:00:11 +01:00
|
|
|
update_mv(w, comp_counts->sign, &comp->sign, MV_UPDATE_PROB);
|
2013-11-25 01:20:59 +01:00
|
|
|
write_mv_update(vp9_mv_class_tree, comp->classes, comp_counts->classes,
|
|
|
|
MV_CLASSES, w);
|
|
|
|
write_mv_update(vp9_mv_class0_tree, comp->class0, comp_counts->class0,
|
|
|
|
CLASS0_SIZE, w);
|
2013-06-26 00:19:18 +02:00
|
|
|
for (j = 0; j < MV_OFFSET_BITS; ++j)
|
2014-03-04 20:00:11 +01:00
|
|
|
update_mv(w, comp_counts->bits[j], &comp->bits[j], MV_UPDATE_PROB);
|
2012-07-26 22:42:07 +02:00
|
|
|
}
|
2013-06-26 00:19:18 +02:00
|
|
|
|
2012-07-26 22:42:07 +02:00
|
|
|
for (i = 0; i < 2; ++i) {
|
2013-11-25 01:20:59 +01:00
|
|
|
for (j = 0; j < CLASS0_SIZE; ++j)
|
|
|
|
write_mv_update(vp9_mv_fp_tree, mvc->comps[i].class0_fp[j],
|
|
|
|
counts->comps[i].class0_fp[j], MV_FP_SIZE, w);
|
2013-06-26 00:19:18 +02:00
|
|
|
|
2013-11-25 01:20:59 +01:00
|
|
|
write_mv_update(vp9_mv_fp_tree, mvc->comps[i].fp, counts->comps[i].fp,
|
|
|
|
MV_FP_SIZE, w);
|
2012-07-26 22:42:07 +02:00
|
|
|
}
|
2013-06-26 00:19:18 +02:00
|
|
|
|
2012-07-26 22:42:07 +02:00
|
|
|
if (usehp) {
|
|
|
|
for (i = 0; i < 2; ++i) {
|
2013-11-25 01:20:59 +01:00
|
|
|
update_mv(w, counts->comps[i].class0_hp, &mvc->comps[i].class0_hp,
|
2014-03-04 20:00:11 +01:00
|
|
|
MV_UPDATE_PROB);
|
|
|
|
update_mv(w, counts->comps[i].hp, &mvc->comps[i].hp, MV_UPDATE_PROB);
|
2012-07-26 22:42:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-20 23:13:38 +02:00
|
|
|
void vp9_encode_mv(VP9_COMP* cpi, vpx_writer* w,
|
2013-06-26 18:06:25 +02:00
|
|
|
const MV* mv, const MV* ref,
|
2013-05-15 21:37:03 +02:00
|
|
|
const nmv_context* mvctx, int usehp) {
|
|
|
|
const MV diff = {mv->row - ref->row,
|
|
|
|
mv->col - ref->col};
|
|
|
|
const MV_JOINT_TYPE j = vp9_get_mv_joint(&diff);
|
2016-01-13 17:02:05 +01:00
|
|
|
usehp = usehp && use_mv_hp(ref);
|
2013-05-15 21:37:03 +02:00
|
|
|
|
2013-12-11 03:13:29 +01:00
|
|
|
vp9_write_token(w, vp9_mv_joint_tree, mvctx->joints, &mv_joint_encodings[j]);
|
2013-04-17 20:21:28 +02:00
|
|
|
if (mv_joint_vertical(j))
|
2013-05-15 21:37:03 +02:00
|
|
|
encode_mv_component(w, diff.row, &mvctx->comps[0], usehp);
|
2013-04-17 20:21:28 +02:00
|
|
|
|
|
|
|
if (mv_joint_horizontal(j))
|
2013-05-15 21:37:03 +02:00
|
|
|
encode_mv_component(w, diff.col, &mvctx->comps[1], usehp);
|
2013-06-26 18:06:25 +02:00
|
|
|
|
2013-07-12 18:52:24 +02:00
|
|
|
// If auto_mv_step_size is enabled then keep track of the largest
|
|
|
|
// motion vector component used.
|
2014-08-22 02:28:44 +02:00
|
|
|
if (cpi->sf.mv.auto_mv_step_size) {
|
2015-08-18 03:19:22 +02:00
|
|
|
unsigned int maxv = VPXMAX(abs(mv->row), abs(mv->col)) >> 3;
|
|
|
|
cpi->max_mv_magnitude = VPXMAX(maxv, cpi->max_mv_magnitude);
|
2013-06-26 18:06:25 +02:00
|
|
|
}
|
2012-07-26 22:42:07 +02:00
|
|
|
}
|
|
|
|
|
2014-02-26 03:11:42 +01:00
|
|
|
void vp9_build_nmv_cost_table(int *mvjoint, int *mvcost[2],
|
|
|
|
const nmv_context* ctx, int usehp) {
|
|
|
|
vp9_cost_tokens(mvjoint, ctx->joints, vp9_mv_joint_tree);
|
|
|
|
build_nmv_component_cost_table(mvcost[0], &ctx->comps[0], usehp);
|
|
|
|
build_nmv_component_cost_table(mvcost[1], &ctx->comps[1], usehp);
|
2012-07-26 22:42:07 +02:00
|
|
|
}
|
2012-11-09 19:52:08 +01:00
|
|
|
|
2016-01-20 01:40:20 +01:00
|
|
|
static void inc_mvs(const MODE_INFO *mi, const MB_MODE_INFO_EXT *mbmi_ext,
|
2015-06-29 18:27:11 +02:00
|
|
|
const int_mv mvs[2],
|
2013-09-25 00:55:49 +02:00
|
|
|
nmv_context_counts *counts) {
|
|
|
|
int i;
|
2014-03-19 22:26:11 +01:00
|
|
|
|
2016-01-20 01:40:20 +01:00
|
|
|
for (i = 0; i < 1 + has_second_ref(mi); ++i) {
|
|
|
|
const MV *ref = &mbmi_ext->ref_mvs[mi->ref_frame[i]][0].as_mv;
|
2014-03-19 22:26:11 +01:00
|
|
|
const MV diff = {mvs[i].as_mv.row - ref->row,
|
|
|
|
mvs[i].as_mv.col - ref->col};
|
2013-09-25 00:55:49 +02:00
|
|
|
vp9_inc_mv(&diff, counts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-21 20:11:06 +01:00
|
|
|
void vp9_update_mv_count(ThreadData *td) {
|
|
|
|
const MACROBLOCKD *xd = &td->mb.e_mbd;
|
2015-04-21 14:36:58 +02:00
|
|
|
const MODE_INFO *mi = xd->mi[0];
|
2015-06-29 18:27:11 +02:00
|
|
|
const MB_MODE_INFO_EXT *mbmi_ext = td->mb.mbmi_ext;
|
2012-11-09 19:52:08 +01:00
|
|
|
|
2016-01-20 01:40:20 +01:00
|
|
|
if (mi->sb_type < BLOCK_8X8) {
|
|
|
|
const int num_4x4_w = num_4x4_blocks_wide_lookup[mi->sb_type];
|
|
|
|
const int num_4x4_h = num_4x4_blocks_high_lookup[mi->sb_type];
|
2013-09-25 00:55:49 +02:00
|
|
|
int idx, idy;
|
|
|
|
|
|
|
|
for (idy = 0; idy < 2; idy += num_4x4_h) {
|
|
|
|
for (idx = 0; idx < 2; idx += num_4x4_w) {
|
2013-07-17 02:01:08 +02:00
|
|
|
const int i = idy * 2 + idx;
|
2013-10-07 20:20:50 +02:00
|
|
|
if (mi->bmi[i].as_mode == NEWMV)
|
2016-01-20 01:40:20 +01:00
|
|
|
inc_mvs(mi, mbmi_ext, mi->bmi[i].as_mv, &td->counts->mv);
|
2012-11-09 19:52:08 +01:00
|
|
|
}
|
|
|
|
}
|
2014-03-19 22:26:11 +01:00
|
|
|
} else {
|
2016-01-20 01:40:20 +01:00
|
|
|
if (mi->mode == NEWMV)
|
|
|
|
inc_mvs(mi, mbmi_ext, mi->mv, &td->counts->mv);
|
2012-11-09 19:52:08 +01:00
|
|
|
}
|
|
|
|
}
|
2013-11-26 01:36:28 +01:00
|
|
|
|