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
|
|
|
*/
|
|
|
|
|
|
|
|
|
2012-11-27 22:59:17 +01:00
|
|
|
#include "vp9/common/vp9_common.h"
|
2012-11-28 19:41:40 +01:00
|
|
|
#include "vp9/encoder/vp9_encodemv.h"
|
2012-11-27 22:59:17 +01:00
|
|
|
#include "vp9/common/vp9_entropymode.h"
|
|
|
|
#include "vp9/common/vp9_systemdependent.h"
|
2010-05-18 17:58:33 +02:00
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
#ifdef ENTROPY_STATS
|
|
|
|
extern unsigned int active_section;
|
|
|
|
#endif
|
|
|
|
|
2012-07-26 22:42:07 +02:00
|
|
|
#ifdef NMV_STATS
|
|
|
|
nmv_context_counts tnmvcounts;
|
|
|
|
#endif
|
|
|
|
|
2013-05-15 21:37:03 +02:00
|
|
|
static void encode_mv_component(vp9_writer* w, int comp,
|
|
|
|
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
|
|
|
|
vp9_write(w, sign, mvcomp->sign);
|
|
|
|
|
|
|
|
// Class
|
|
|
|
write_token(w, vp9_mv_class_tree, mvcomp->classes,
|
|
|
|
&vp9_mv_class_encodings[mv_class]);
|
|
|
|
|
|
|
|
// Integer bits
|
|
|
|
if (mv_class == MV_CLASS_0) {
|
|
|
|
write_token(w, vp9_mv_class0_tree, mvcomp->class0,
|
|
|
|
&vp9_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)
|
|
|
|
vp9_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
|
|
|
|
write_token(w, vp9_mv_fp_tree,
|
|
|
|
mv_class == MV_CLASS_0 ? mvcomp->class0_fp[d] : mvcomp->fp,
|
|
|
|
&vp9_mv_fp_encodings[fr]);
|
|
|
|
|
|
|
|
// High precision bit
|
|
|
|
if (usehp)
|
|
|
|
vp9_write(w, hp,
|
|
|
|
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 i, v;
|
|
|
|
int sign_cost[2], class_cost[MV_CLASSES], class0_cost[CLASS0_SIZE];
|
|
|
|
int bits_cost[MV_OFFSET_BITS][2];
|
|
|
|
int class0_fp_cost[CLASS0_SIZE][4], fp_cost[4];
|
|
|
|
int class0_hp_cost[2], hp_cost[2];
|
|
|
|
|
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
|
|
|
|
|
|
|
if (usehp) {
|
2012-10-31 22:40:53 +01:00
|
|
|
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;
|
|
|
|
for (v = 1; v <= MV_MAX; ++v) {
|
|
|
|
int z, c, o, d, e, f, cost = 0;
|
|
|
|
z = v - 1;
|
2012-10-31 00:25:53 +01:00
|
|
|
c = vp9_get_mv_class(z, &o);
|
2012-07-26 22:42:07 +02:00
|
|
|
cost += class_cost[c];
|
|
|
|
d = (o >> 3); /* int mv data */
|
|
|
|
f = (o >> 1) & 3; /* fractional pel mv data */
|
|
|
|
e = (o & 1); /* high precision mv data */
|
|
|
|
if (c == MV_CLASS_0) {
|
|
|
|
cost += class0_cost[d];
|
|
|
|
} else {
|
|
|
|
int i, b;
|
|
|
|
b = c + CLASS0_BITS - 1; /* number of bits */
|
|
|
|
for (i = 0; i < b; ++i)
|
|
|
|
cost += bits_cost[i][((d >> i) & 1)];
|
|
|
|
}
|
|
|
|
if (c == MV_CLASS_0) {
|
|
|
|
cost += class0_fp_cost[d][f];
|
|
|
|
} else {
|
|
|
|
cost += fp_cost[f];
|
|
|
|
}
|
|
|
|
if (usehp) {
|
|
|
|
if (c == MV_CLASS_0) {
|
|
|
|
cost += class0_hp_cost[e];
|
|
|
|
} else {
|
|
|
|
cost += hp_cost[e];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mvcost[v] = cost + sign_cost[0];
|
|
|
|
mvcost[-v] = cost + sign_cost[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int update_nmv_savings(const unsigned int ct[2],
|
2012-10-31 22:40:53 +01:00
|
|
|
const vp9_prob cur_p,
|
|
|
|
const vp9_prob new_p,
|
|
|
|
const vp9_prob upd_p) {
|
2012-07-26 22:42:07 +02:00
|
|
|
|
|
|
|
#ifdef LOW_PRECISION_MV_UPDATE
|
2012-10-31 22:40:53 +01:00
|
|
|
vp9_prob mod_p = new_p | 1;
|
2012-07-26 22:42:07 +02:00
|
|
|
#else
|
2012-10-31 22:40:53 +01:00
|
|
|
vp9_prob mod_p = new_p;
|
2012-07-26 22:42:07 +02:00
|
|
|
#endif
|
2012-10-31 22:40:53 +01:00
|
|
|
const int cur_b = cost_branch256(ct, cur_p);
|
|
|
|
const int mod_b = cost_branch256(ct, mod_p);
|
2012-07-26 22:42:07 +02:00
|
|
|
const int cost = 7 * 256 +
|
|
|
|
#ifndef LOW_PRECISION_MV_UPDATE
|
|
|
|
256 +
|
|
|
|
#endif
|
2012-10-31 22:40:53 +01:00
|
|
|
(vp9_cost_one(upd_p) - vp9_cost_zero(upd_p));
|
2012-07-26 22:42:07 +02:00
|
|
|
if (cur_b - mod_b - cost > 0) {
|
|
|
|
return cur_b - mod_b - cost;
|
|
|
|
} else {
|
2012-11-05 23:22:59 +01:00
|
|
|
return 0 - vp9_cost_zero(upd_p);
|
2012-07-26 22:42:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-26 00:19:18 +02:00
|
|
|
static int update_mv(vp9_writer *bc, const unsigned int ct[2],
|
|
|
|
vp9_prob *cur_p, vp9_prob new_p, vp9_prob upd_p) {
|
2012-07-26 22:42:07 +02:00
|
|
|
|
|
|
|
#ifdef LOW_PRECISION_MV_UPDATE
|
2012-10-31 22:40:53 +01:00
|
|
|
vp9_prob mod_p = new_p | 1;
|
2012-07-26 22:42:07 +02:00
|
|
|
#else
|
2012-10-31 22:40:53 +01:00
|
|
|
vp9_prob mod_p = new_p;
|
2012-07-26 22:42:07 +02:00
|
|
|
#endif
|
|
|
|
|
2012-10-31 22:40:53 +01:00
|
|
|
const int cur_b = cost_branch256(ct, *cur_p);
|
|
|
|
const int mod_b = cost_branch256(ct, mod_p);
|
2012-07-26 22:42:07 +02:00
|
|
|
const int cost = 7 * 256 +
|
|
|
|
#ifndef LOW_PRECISION_MV_UPDATE
|
|
|
|
256 +
|
|
|
|
#endif
|
2012-10-31 22:40:53 +01:00
|
|
|
(vp9_cost_one(upd_p) - vp9_cost_zero(upd_p));
|
2012-07-26 22:42:07 +02:00
|
|
|
|
|
|
|
if (cur_b - mod_b > cost) {
|
|
|
|
*cur_p = mod_p;
|
2012-10-31 22:40:53 +01:00
|
|
|
vp9_write(bc, 1, upd_p);
|
2012-07-26 22:42:07 +02:00
|
|
|
#ifdef LOW_PRECISION_MV_UPDATE
|
2012-10-31 22:40:53 +01:00
|
|
|
vp9_write_literal(bc, mod_p >> 1, 7);
|
2012-07-26 22:42:07 +02:00
|
|
|
#else
|
2012-10-31 22:40:53 +01:00
|
|
|
vp9_write_literal(bc, mod_p, 8);
|
2012-07-26 22:42:07 +02:00
|
|
|
#endif
|
|
|
|
return 1;
|
|
|
|
} else {
|
2012-10-31 22:40:53 +01:00
|
|
|
vp9_write(bc, 0, upd_p);
|
2012-07-26 22:42:07 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-25 00:58:18 +02:00
|
|
|
static void counts_to_nmv_context(
|
|
|
|
nmv_context_counts *nmv_count,
|
|
|
|
nmv_context *prob,
|
|
|
|
int usehp,
|
|
|
|
unsigned int (*branch_ct_joint)[2],
|
|
|
|
unsigned int (*branch_ct_sign)[2],
|
|
|
|
unsigned int (*branch_ct_classes)[MV_CLASSES - 1][2],
|
|
|
|
unsigned int (*branch_ct_class0)[CLASS0_SIZE - 1][2],
|
|
|
|
unsigned int (*branch_ct_bits)[MV_OFFSET_BITS][2],
|
|
|
|
unsigned int (*branch_ct_class0_fp)[CLASS0_SIZE][4 - 1][2],
|
|
|
|
unsigned int (*branch_ct_fp)[4 - 1][2],
|
|
|
|
unsigned int (*branch_ct_class0_hp)[2],
|
|
|
|
unsigned int (*branch_ct_hp)[2]) {
|
|
|
|
int i, j, k;
|
|
|
|
vp9_counts_process(nmv_count, usehp);
|
|
|
|
vp9_tree_probs_from_distribution(vp9_mv_joint_tree,
|
|
|
|
prob->joints,
|
|
|
|
branch_ct_joint,
|
|
|
|
nmv_count->joints, 0);
|
|
|
|
for (i = 0; i < 2; ++i) {
|
|
|
|
const uint32_t s0 = nmv_count->comps[i].sign[0];
|
|
|
|
const uint32_t s1 = nmv_count->comps[i].sign[1];
|
|
|
|
|
|
|
|
prob->comps[i].sign = get_binary_prob(s0, s1);
|
|
|
|
branch_ct_sign[i][0] = s0;
|
|
|
|
branch_ct_sign[i][1] = s1;
|
|
|
|
vp9_tree_probs_from_distribution(vp9_mv_class_tree,
|
|
|
|
prob->comps[i].classes,
|
|
|
|
branch_ct_classes[i],
|
|
|
|
nmv_count->comps[i].classes, 0);
|
|
|
|
vp9_tree_probs_from_distribution(vp9_mv_class0_tree,
|
|
|
|
prob->comps[i].class0,
|
|
|
|
branch_ct_class0[i],
|
|
|
|
nmv_count->comps[i].class0, 0);
|
|
|
|
for (j = 0; j < MV_OFFSET_BITS; ++j) {
|
|
|
|
const uint32_t b0 = nmv_count->comps[i].bits[j][0];
|
|
|
|
const uint32_t b1 = nmv_count->comps[i].bits[j][1];
|
|
|
|
|
|
|
|
prob->comps[i].bits[j] = get_binary_prob(b0, b1);
|
|
|
|
branch_ct_bits[i][j][0] = b0;
|
|
|
|
branch_ct_bits[i][j][1] = b1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (i = 0; i < 2; ++i) {
|
|
|
|
for (k = 0; k < CLASS0_SIZE; ++k) {
|
|
|
|
vp9_tree_probs_from_distribution(vp9_mv_fp_tree,
|
|
|
|
prob->comps[i].class0_fp[k],
|
|
|
|
branch_ct_class0_fp[i][k],
|
|
|
|
nmv_count->comps[i].class0_fp[k], 0);
|
|
|
|
}
|
|
|
|
vp9_tree_probs_from_distribution(vp9_mv_fp_tree,
|
|
|
|
prob->comps[i].fp,
|
|
|
|
branch_ct_fp[i],
|
|
|
|
nmv_count->comps[i].fp, 0);
|
|
|
|
}
|
|
|
|
if (usehp) {
|
|
|
|
for (i = 0; i < 2; ++i) {
|
|
|
|
const uint32_t c0_hp0 = nmv_count->comps[i].class0_hp[0];
|
|
|
|
const uint32_t c0_hp1 = nmv_count->comps[i].class0_hp[1];
|
|
|
|
const uint32_t hp0 = nmv_count->comps[i].hp[0];
|
|
|
|
const uint32_t hp1 = nmv_count->comps[i].hp[1];
|
|
|
|
|
|
|
|
prob->comps[i].class0_hp = get_binary_prob(c0_hp0, c0_hp1);
|
|
|
|
branch_ct_class0_hp[i][0] = c0_hp0;
|
|
|
|
branch_ct_class0_hp[i][1] = c0_hp1;
|
|
|
|
|
|
|
|
prob->comps[i].hp = get_binary_prob(hp0, hp1);
|
|
|
|
branch_ct_hp[i][0] = hp0;
|
|
|
|
branch_ct_hp[i][1] = hp1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-26 22:42:07 +02:00
|
|
|
#ifdef NMV_STATS
|
|
|
|
void init_nmvstats() {
|
2012-10-31 22:40:53 +01:00
|
|
|
vp9_zero(tnmvcounts);
|
2012-07-26 22:42:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void print_nmvstats() {
|
|
|
|
nmv_context prob;
|
|
|
|
unsigned int branch_ct_joint[MV_JOINTS - 1][2];
|
|
|
|
unsigned int branch_ct_sign[2][2];
|
|
|
|
unsigned int branch_ct_classes[2][MV_CLASSES - 1][2];
|
|
|
|
unsigned int branch_ct_class0[2][CLASS0_SIZE - 1][2];
|
|
|
|
unsigned int branch_ct_bits[2][MV_OFFSET_BITS][2];
|
|
|
|
unsigned int branch_ct_class0_fp[2][CLASS0_SIZE][4 - 1][2];
|
|
|
|
unsigned int branch_ct_fp[2][4 - 1][2];
|
|
|
|
unsigned int branch_ct_class0_hp[2][2];
|
|
|
|
unsigned int branch_ct_hp[2][2];
|
|
|
|
int i, j, k;
|
2013-06-25 00:58:18 +02:00
|
|
|
counts_to_nmv_context(&tnmvcounts, &prob, 1,
|
|
|
|
branch_ct_joint, branch_ct_sign, branch_ct_classes,
|
|
|
|
branch_ct_class0, branch_ct_bits,
|
|
|
|
branch_ct_class0_fp, branch_ct_fp,
|
|
|
|
branch_ct_class0_hp, branch_ct_hp);
|
2012-07-26 22:42:07 +02:00
|
|
|
|
|
|
|
printf("\nCounts =\n { ");
|
|
|
|
for (j = 0; j < MV_JOINTS; ++j)
|
|
|
|
printf("%d, ", tnmvcounts.joints[j]);
|
|
|
|
printf("},\n");
|
2012-11-09 19:52:08 +01:00
|
|
|
for (i = 0; i < 2; ++i) {
|
2012-07-26 22:42:07 +02:00
|
|
|
printf(" {\n");
|
|
|
|
printf(" %d/%d,\n", tnmvcounts.comps[i].sign[0],
|
|
|
|
tnmvcounts.comps[i].sign[1]);
|
|
|
|
printf(" { ");
|
|
|
|
for (j = 0; j < MV_CLASSES; ++j)
|
|
|
|
printf("%d, ", tnmvcounts.comps[i].classes[j]);
|
|
|
|
printf("},\n");
|
|
|
|
printf(" { ");
|
|
|
|
for (j = 0; j < CLASS0_SIZE; ++j)
|
|
|
|
printf("%d, ", tnmvcounts.comps[i].class0[j]);
|
|
|
|
printf("},\n");
|
|
|
|
printf(" { ");
|
|
|
|
for (j = 0; j < MV_OFFSET_BITS; ++j)
|
|
|
|
printf("%d/%d, ", tnmvcounts.comps[i].bits[j][0],
|
|
|
|
tnmvcounts.comps[i].bits[j][1]);
|
|
|
|
printf("},\n");
|
|
|
|
|
|
|
|
printf(" {");
|
|
|
|
for (j = 0; j < CLASS0_SIZE; ++j) {
|
|
|
|
printf("{");
|
|
|
|
for (k = 0; k < 4; ++k)
|
|
|
|
printf("%d, ", tnmvcounts.comps[i].class0_fp[j][k]);
|
|
|
|
printf("}, ");
|
|
|
|
}
|
|
|
|
printf("},\n");
|
|
|
|
|
|
|
|
printf(" { ");
|
|
|
|
for (j = 0; j < 4; ++j)
|
|
|
|
printf("%d, ", tnmvcounts.comps[i].fp[j]);
|
|
|
|
printf("},\n");
|
|
|
|
|
|
|
|
printf(" %d/%d,\n",
|
|
|
|
tnmvcounts.comps[i].class0_hp[0],
|
|
|
|
tnmvcounts.comps[i].class0_hp[1]);
|
|
|
|
printf(" %d/%d,\n",
|
|
|
|
tnmvcounts.comps[i].hp[0],
|
|
|
|
tnmvcounts.comps[i].hp[1]);
|
|
|
|
printf(" },\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\nProbs =\n { ");
|
|
|
|
for (j = 0; j < MV_JOINTS - 1; ++j)
|
|
|
|
printf("%d, ", prob.joints[j]);
|
|
|
|
printf("},\n");
|
|
|
|
for (i=0; i< 2; ++i) {
|
|
|
|
printf(" {\n");
|
|
|
|
printf(" %d,\n", prob.comps[i].sign);
|
|
|
|
printf(" { ");
|
|
|
|
for (j = 0; j < MV_CLASSES - 1; ++j)
|
|
|
|
printf("%d, ", prob.comps[i].classes[j]);
|
|
|
|
printf("},\n");
|
|
|
|
printf(" { ");
|
|
|
|
for (j = 0; j < CLASS0_SIZE - 1; ++j)
|
|
|
|
printf("%d, ", prob.comps[i].class0[j]);
|
|
|
|
printf("},\n");
|
|
|
|
printf(" { ");
|
|
|
|
for (j = 0; j < MV_OFFSET_BITS; ++j)
|
|
|
|
printf("%d, ", prob.comps[i].bits[j]);
|
|
|
|
printf("},\n");
|
|
|
|
printf(" { ");
|
|
|
|
for (j = 0; j < CLASS0_SIZE; ++j) {
|
|
|
|
printf("{");
|
|
|
|
for (k = 0; k < 3; ++k)
|
|
|
|
printf("%d, ", prob.comps[i].class0_fp[j][k]);
|
|
|
|
printf("}, ");
|
|
|
|
}
|
|
|
|
printf("},\n");
|
|
|
|
printf(" { ");
|
|
|
|
for (j = 0; j < 3; ++j)
|
|
|
|
printf("%d, ", prob.comps[i].fp[j]);
|
|
|
|
printf("},\n");
|
|
|
|
|
|
|
|
printf(" %d,\n", prob.comps[i].class0_hp);
|
|
|
|
printf(" %d,\n", prob.comps[i].hp);
|
|
|
|
printf(" },\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-18 01:47:38 +02:00
|
|
|
static void add_nmvcount(nmv_context_counts* const dst,
|
|
|
|
const nmv_context_counts* const src) {
|
2012-07-26 22:42:07 +02:00
|
|
|
int i, j, k;
|
|
|
|
for (j = 0; j < MV_JOINTS; ++j) {
|
|
|
|
dst->joints[j] += src->joints[j];
|
|
|
|
}
|
|
|
|
for (i = 0; i < 2; ++i) {
|
|
|
|
for (j = 0; j < MV_VALS; ++j) {
|
|
|
|
dst->comps[i].mvcount[j] += src->comps[i].mvcount[j];
|
|
|
|
}
|
|
|
|
dst->comps[i].sign[0] += src->comps[i].sign[0];
|
|
|
|
dst->comps[i].sign[1] += src->comps[i].sign[1];
|
|
|
|
for (j = 0; j < MV_CLASSES; ++j) {
|
|
|
|
dst->comps[i].classes[j] += src->comps[i].classes[j];
|
|
|
|
}
|
|
|
|
for (j = 0; j < CLASS0_SIZE; ++j) {
|
|
|
|
dst->comps[i].class0[j] += src->comps[i].class0[j];
|
|
|
|
}
|
|
|
|
for (j = 0; j < MV_OFFSET_BITS; ++j) {
|
|
|
|
dst->comps[i].bits[j][0] += src->comps[i].bits[j][0];
|
|
|
|
dst->comps[i].bits[j][1] += src->comps[i].bits[j][1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (i = 0; i < 2; ++i) {
|
|
|
|
for (j = 0; j < CLASS0_SIZE; ++j) {
|
|
|
|
for (k = 0; k < 4; ++k) {
|
|
|
|
dst->comps[i].class0_fp[j][k] += src->comps[i].class0_fp[j][k];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (j = 0; j < 4; ++j) {
|
|
|
|
dst->comps[i].fp[j] += src->comps[i].fp[j];
|
|
|
|
}
|
|
|
|
dst->comps[i].class0_hp[0] += src->comps[i].class0_hp[0];
|
|
|
|
dst->comps[i].class0_hp[1] += src->comps[i].class0_hp[1];
|
|
|
|
dst->comps[i].hp[0] += src->comps[i].hp[0];
|
|
|
|
dst->comps[i].hp[1] += src->comps[i].hp[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-11-09 19:52:08 +01:00
|
|
|
void vp9_write_nmv_probs(VP9_COMP* const cpi, int usehp, vp9_writer* const bc) {
|
2012-07-26 22:42:07 +02:00
|
|
|
int i, j;
|
|
|
|
nmv_context prob;
|
|
|
|
unsigned int branch_ct_joint[MV_JOINTS - 1][2];
|
|
|
|
unsigned int branch_ct_sign[2][2];
|
|
|
|
unsigned int branch_ct_classes[2][MV_CLASSES - 1][2];
|
|
|
|
unsigned int branch_ct_class0[2][CLASS0_SIZE - 1][2];
|
|
|
|
unsigned int branch_ct_bits[2][MV_OFFSET_BITS][2];
|
|
|
|
unsigned int branch_ct_class0_fp[2][CLASS0_SIZE][4 - 1][2];
|
|
|
|
unsigned int branch_ct_fp[2][4 - 1][2];
|
|
|
|
unsigned int branch_ct_class0_hp[2][2];
|
|
|
|
unsigned int branch_ct_hp[2][2];
|
2013-06-26 00:19:18 +02:00
|
|
|
nmv_context *mvc = &cpi->common.fc.nmvc;
|
|
|
|
|
2012-07-26 22:42:07 +02:00
|
|
|
#ifdef NMV_STATS
|
|
|
|
if (!cpi->dummy_packing)
|
|
|
|
add_nmvcount(&tnmvcounts, &cpi->NMVcount);
|
|
|
|
#endif
|
2013-06-25 00:58:18 +02:00
|
|
|
counts_to_nmv_context(&cpi->NMVcount, &prob, usehp,
|
|
|
|
branch_ct_joint, branch_ct_sign, branch_ct_classes,
|
|
|
|
branch_ct_class0, branch_ct_bits,
|
|
|
|
branch_ct_class0_fp, branch_ct_fp,
|
|
|
|
branch_ct_class0_hp, branch_ct_hp);
|
2012-07-26 22:42:07 +02:00
|
|
|
|
2013-06-26 00:19:18 +02:00
|
|
|
for (j = 0; j < MV_JOINTS - 1; ++j)
|
|
|
|
update_mv(bc, branch_ct_joint[j], &mvc->joints[j], prob.joints[j],
|
|
|
|
VP9_NMV_UPDATE_PROB);
|
|
|
|
|
2012-07-26 22:42:07 +02:00
|
|
|
for (i = 0; i < 2; ++i) {
|
2013-06-26 00:19:18 +02:00
|
|
|
update_mv(bc, branch_ct_sign[i], &mvc->comps[i].sign,
|
|
|
|
prob.comps[i].sign, VP9_NMV_UPDATE_PROB);
|
|
|
|
for (j = 0; j < MV_CLASSES - 1; ++j)
|
|
|
|
update_mv(bc, branch_ct_classes[i][j], &mvc->comps[i].classes[j],
|
|
|
|
prob.comps[i].classes[j], VP9_NMV_UPDATE_PROB);
|
|
|
|
|
|
|
|
for (j = 0; j < CLASS0_SIZE - 1; ++j)
|
|
|
|
update_mv(bc, branch_ct_class0[i][j], &mvc->comps[i].class0[j],
|
|
|
|
prob.comps[i].class0[j], VP9_NMV_UPDATE_PROB);
|
|
|
|
|
|
|
|
for (j = 0; j < MV_OFFSET_BITS; ++j)
|
|
|
|
update_mv(bc, branch_ct_bits[i][j], &mvc->comps[i].bits[j],
|
|
|
|
prob.comps[i].bits[j], VP9_NMV_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) {
|
|
|
|
for (j = 0; j < CLASS0_SIZE; ++j) {
|
|
|
|
int k;
|
2013-06-26 00:19:18 +02:00
|
|
|
for (k = 0; k < 3; ++k)
|
|
|
|
update_mv(bc, branch_ct_class0_fp[i][j][k],
|
|
|
|
&mvc->comps[i].class0_fp[j][k],
|
|
|
|
prob.comps[i].class0_fp[j][k], VP9_NMV_UPDATE_PROB);
|
2012-07-26 22:42:07 +02:00
|
|
|
}
|
2013-06-26 00:19:18 +02:00
|
|
|
|
|
|
|
for (j = 0; j < 3; ++j)
|
|
|
|
update_mv(bc, branch_ct_fp[i][j], &mvc->comps[i].fp[j],
|
|
|
|
prob.comps[i].fp[j], VP9_NMV_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
|
|
|
if (usehp) {
|
|
|
|
for (i = 0; i < 2; ++i) {
|
2013-06-26 00:19:18 +02:00
|
|
|
update_mv(bc, branch_ct_class0_hp[i], &mvc->comps[i].class0_hp,
|
|
|
|
prob.comps[i].class0_hp, VP9_NMV_UPDATE_PROB);
|
|
|
|
update_mv(bc, branch_ct_hp[i], &mvc->comps[i].hp,
|
|
|
|
prob.comps[i].hp, VP9_NMV_UPDATE_PROB);
|
2012-07-26 22:42:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-26 18:06:25 +02:00
|
|
|
void vp9_encode_mv(VP9_COMP* cpi, vp9_writer* w,
|
|
|
|
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);
|
2013-06-19 03:28:10 +02:00
|
|
|
usehp = usehp && vp9_use_mv_hp(ref);
|
2013-05-15 21:37:03 +02:00
|
|
|
|
|
|
|
write_token(w, vp9_mv_joint_tree, mvctx->joints, &vp9_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.
|
|
|
|
if (!cpi->dummy_packing && cpi->sf.auto_mv_step_size) {
|
|
|
|
unsigned int maxv = MAX(abs(mv->row), abs(mv->col)) >> 3;
|
|
|
|
cpi->max_mv_magnitude = MAX(maxv, cpi->max_mv_magnitude);
|
2013-06-26 18:06:25 +02:00
|
|
|
}
|
2012-07-26 22:42:07 +02:00
|
|
|
}
|
|
|
|
|
2012-10-30 20:58:42 +01:00
|
|
|
void vp9_build_nmv_cost_table(int *mvjoint,
|
2012-07-26 22:42:07 +02:00
|
|
|
int *mvcost[2],
|
2012-10-18 01:47:38 +02:00
|
|
|
const nmv_context* const mvctx,
|
2012-07-26 22:42:07 +02:00
|
|
|
int usehp,
|
|
|
|
int mvc_flag_v,
|
|
|
|
int mvc_flag_h) {
|
2012-10-31 22:40:53 +01:00
|
|
|
vp9_clear_system_state();
|
2012-10-31 01:12:12 +01:00
|
|
|
vp9_cost_tokens(mvjoint, mvctx->joints, vp9_mv_joint_tree);
|
2012-07-26 22:42:07 +02:00
|
|
|
if (mvc_flag_v)
|
|
|
|
build_nmv_component_cost_table(mvcost[0], &mvctx->comps[0], usehp);
|
|
|
|
if (mvc_flag_h)
|
|
|
|
build_nmv_component_cost_table(mvcost[1], &mvctx->comps[1], usehp);
|
|
|
|
}
|
2012-11-09 19:52:08 +01:00
|
|
|
|
|
|
|
void vp9_update_nmv_count(VP9_COMP *cpi, MACROBLOCK *x,
|
|
|
|
int_mv *best_ref_mv, int_mv *second_best_ref_mv) {
|
2013-07-18 01:46:53 +02:00
|
|
|
MODE_INFO *mi = x->e_mbd.mode_info_context;
|
|
|
|
MB_MODE_INFO *const mbmi = &mi->mbmi;
|
2013-07-17 02:01:08 +02:00
|
|
|
MV diff;
|
2013-07-23 15:51:44 +02:00
|
|
|
const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[mbmi->sb_type];
|
|
|
|
const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[mbmi->sb_type];
|
2013-05-18 00:54:49 +02:00
|
|
|
int idx, idy;
|
2012-11-09 19:52:08 +01:00
|
|
|
|
2013-05-16 07:28:36 +02:00
|
|
|
if (mbmi->sb_type < BLOCK_SIZE_SB8X8) {
|
2013-04-11 20:08:00 +02:00
|
|
|
PARTITION_INFO *pi = x->partition_info;
|
2013-07-23 15:51:44 +02:00
|
|
|
for (idy = 0; idy < 2; idy += num_4x4_blocks_high) {
|
|
|
|
for (idx = 0; idx < 2; idx += num_4x4_blocks_wide) {
|
2013-07-17 02:01:08 +02:00
|
|
|
const int i = idy * 2 + idx;
|
2013-05-26 23:40:49 +02:00
|
|
|
if (pi->bmi[i].mode == NEWMV) {
|
2013-07-18 01:46:53 +02:00
|
|
|
diff.row = mi->bmi[i].as_mv[0].as_mv.row - best_ref_mv->as_mv.row;
|
|
|
|
diff.col = mi->bmi[i].as_mv[0].as_mv.col - best_ref_mv->as_mv.col;
|
2013-07-17 02:01:08 +02:00
|
|
|
vp9_inc_mv(&diff, &cpi->NMVcount);
|
|
|
|
|
2013-06-06 22:44:34 +02:00
|
|
|
if (x->e_mbd.mode_info_context->mbmi.ref_frame[1] > INTRA_FRAME) {
|
2013-07-18 01:46:53 +02:00
|
|
|
diff.row = mi->bmi[i].as_mv[1].as_mv.row -
|
2013-05-18 00:54:49 +02:00
|
|
|
second_best_ref_mv->as_mv.row;
|
2013-07-18 01:46:53 +02:00
|
|
|
diff.col = mi->bmi[i].as_mv[1].as_mv.col -
|
2013-05-18 00:54:49 +02:00
|
|
|
second_best_ref_mv->as_mv.col;
|
2013-07-17 02:01:08 +02:00
|
|
|
vp9_inc_mv(&diff, &cpi->NMVcount);
|
2013-05-18 00:54:49 +02:00
|
|
|
}
|
2012-11-09 19:52:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (mbmi->mode == NEWMV) {
|
2013-07-17 02:01:08 +02:00
|
|
|
diff.row = mbmi->mv[0].as_mv.row - best_ref_mv->as_mv.row;
|
|
|
|
diff.col = mbmi->mv[0].as_mv.col - best_ref_mv->as_mv.col;
|
|
|
|
vp9_inc_mv(&diff, &cpi->NMVcount);
|
|
|
|
|
2013-06-06 22:44:34 +02:00
|
|
|
if (mbmi->ref_frame[1] > INTRA_FRAME) {
|
2013-07-17 02:01:08 +02:00
|
|
|
diff.row = mbmi->mv[1].as_mv.row - second_best_ref_mv->as_mv.row;
|
|
|
|
diff.col = mbmi->mv[1].as_mv.col - second_best_ref_mv->as_mv.col;
|
|
|
|
vp9_inc_mv(&diff, &cpi->NMVcount);
|
2012-11-09 19:52:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|