Commit 1fe85a35 authored by Deb Mukherjee's avatar Deb Mukherjee

Adaptive entropy coding of coefficients, modes, mv.

This patch incorporates adaptive entropy coding of coefficient tokens,
and mode/mv information based on distributions encountered in a frame.
Specifically, there is an initial forward update to the probabilities
in the bitstream as before for coding the symbols in the frame, however
at the end of decoding each frame, the forward update to the
probabilities is reverted and instead the probabilities are updated
towards the actual distributions encountered within the frame.
The amount of update is weighted by the number of hits within each
context.

Results on derf/hd/std-hd are all up by 1.6%.

On derf, the most of the gains come from coefficients, however for the
hd and std-hd sets, the most of the gains come from the mode/mv
information updates.

Change-Id: I708c0e11fdacafee04940fe7ae159ba6844005fd
parent 378ba611
......@@ -230,6 +230,7 @@ EXPERIMENT_LIST="
expanded_coef_context
int_8x8fdct
newintramodes
adaptive_entropy
"
CONFIG_LIST="
external_build
......
......@@ -15,6 +15,7 @@
#include "onyxc_int.h"
#include "findnearmv.h"
#include "entropymode.h"
#include "entropymv.h"
#include "systemdependent.h"
......@@ -239,6 +240,8 @@ void vp8_initialize_common()
vp8_entropy_mode_init();
vp8_entropy_mv_init();
vp8_init_scan_order_mask();
}
File mode changed from 100755 to 100644
......@@ -15,6 +15,7 @@
#include "string.h"
#include "blockd.h"
#include "onyxc_int.h"
#include "entropymode.h"
#include "vpx_mem/vpx_mem.h"
#define uchar unsigned char /* typedefs can clash */
......@@ -192,3 +193,139 @@ void vp8_coef_tree_initialize()
init_bit_trees();
vp8_tokens_from_tree(vp8_coef_encodings, vp8_coef_tree);
}
#if CONFIG_ADAPTIVE_ENTROPY
//#define COEF_COUNT_TESTING
#define COEF_COUNT_SAT 24
#define COEF_MAX_UPDATE_FACTOR 112
#define COEF_COUNT_SAT_KEY 24
#define COEF_MAX_UPDATE_FACTOR_KEY 112
#define COEF_COUNT_SAT_AFTER_KEY 24
#define COEF_MAX_UPDATE_FACTOR_AFTER_KEY 128
void vp8_adapt_coef_probs(VP8_COMMON *cm)
{
int t, i, j, k, count;
unsigned int branch_ct[ENTROPY_NODES][2];
vp8_prob coef_probs[ENTROPY_NODES];
int update_factor; /* denominator 256 */
int factor;
int count_sat;
//printf("Frame type: %d\n", cm->frame_type);
if (cm->frame_type == KEY_FRAME)
{
update_factor = COEF_MAX_UPDATE_FACTOR_KEY;
count_sat = COEF_COUNT_SAT_KEY;
}
else if (cm->last_frame_type == KEY_FRAME)
{
update_factor = COEF_MAX_UPDATE_FACTOR_AFTER_KEY; /* adapt quickly */
count_sat = COEF_COUNT_SAT_AFTER_KEY;
}
else
{
update_factor = COEF_MAX_UPDATE_FACTOR;
count_sat = COEF_COUNT_SAT;
}
#ifdef COEF_COUNT_TESTING
{
printf("static const unsigned int\ncoef_counts"
"[BLOCK_TYPES] [COEF_BANDS]"
"[PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS] = {\n");
for (i = 0; i<BLOCK_TYPES; ++i)
{
printf(" {\n");
for (j = 0; j<COEF_BANDS; ++j)
{
printf(" {\n");
for (k = 0; k<PREV_COEF_CONTEXTS; ++k)
{
printf(" {");
for (t = 0; t<MAX_ENTROPY_TOKENS; ++t) printf("%d, ", cm->fc.coef_counts[i][j][k][t]);
printf("},\n");
}
printf(" },\n");
}
printf(" },\n");
}
printf("};\n");
printf("static const unsigned int\ncoef_counts_8x8"
"[BLOCK_TYPES_8X8] [COEF_BANDS]"
"[PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS] = {\n");
for (i = 0; i<BLOCK_TYPES_8X8; ++i)
{
printf(" {\n");
for (j = 0; j<COEF_BANDS; ++j)
{
printf(" {\n");
for (k = 0; k<PREV_COEF_CONTEXTS; ++k)
{
printf(" {");
for (t = 0; t<MAX_ENTROPY_TOKENS; ++t) printf("%d, ", cm->fc.coef_counts_8x8[i][j][k][t]);
printf("},\n");
}
printf(" },\n");
}
printf(" },\n");
}
printf("};\n");
}
#endif
for (i = 0; i<BLOCK_TYPES; ++i)
for (j = 0; j<COEF_BANDS; ++j)
for (k = 0; k<PREV_COEF_CONTEXTS; ++k)
{
#if CONFIG_EXPANDED_COEF_CONTEXT
if (k >=3 && ((i == 0 && j == 1) || (i > 0 && j == 0)))
continue;
#endif
vp8_tree_probs_from_distribution(
MAX_ENTROPY_TOKENS, vp8_coef_encodings, vp8_coef_tree,
coef_probs, branch_ct, cm->fc.coef_counts [i][j][k],
256, 1);
for (t=0; t<ENTROPY_NODES; ++t)
{
int prob;
count = branch_ct[t][0] + branch_ct[t][1];
count = count > count_sat ? count_sat : count;
factor = (update_factor * count / count_sat);
prob = ((int)cm->fc.pre_coef_probs[i][j][k][t] * (256-factor) +
(int)coef_probs[t] * factor + 128) >> 8;
if (prob <= 0) cm->fc.coef_probs[i][j][k][t] = 1;
else if (prob > 255) cm->fc.coef_probs[i][j][k][t] = 255;
else cm->fc.coef_probs[i][j][k][t] = prob;
}
}
for (i = 0; i<BLOCK_TYPES_8X8; ++i)
for (j = 0; j<COEF_BANDS; ++j)
for (k = 0; k<PREV_COEF_CONTEXTS; ++k)
{
#if CONFIG_EXPANDED_COEF_CONTEXT
if (k >=3 && ((i == 0 && j == 1) || (i > 0 && j == 0)))
continue;
#endif
vp8_tree_probs_from_distribution(
MAX_ENTROPY_TOKENS, vp8_coef_encodings, vp8_coef_tree,
coef_probs, branch_ct, cm->fc.coef_counts_8x8 [i][j][k],
256, 1);
for (t=0; t<ENTROPY_NODES; ++t)
{
int prob;
count = branch_ct[t][0] + branch_ct[t][1];
count = count > count_sat ? count_sat : count;
factor = (update_factor * count / count_sat);
prob = ((int)cm->fc.pre_coef_probs_8x8[i][j][k][t] * (256-factor) +
(int)coef_probs[t] * factor + 128) >> 8;
if (prob <= 0) cm->fc.coef_probs_8x8[i][j][k][t] = 1;
else if (prob > 255) cm->fc.coef_probs_8x8[i][j][k][t] = 255;
else cm->fc.coef_probs_8x8[i][j][k][t] = prob;
}
}
}
#endif
......@@ -92,9 +92,9 @@ extern DECLARE_ALIGNED(64, const unsigned char, vp8_coef_bands_8x8[64]);
#endif
#if CONFIG_NEWUPDATE
#define SUBEXP_PARAM 2 /* Subexponential code parameter */
#define MODULUS_PARAM 21 /* Modulus parameter */
#define COEFUPDATETYPE 2 /* coef update type to use (1/2/3) */
#define SUBEXP_PARAM 4 /* Subexponential code parameter */
#define MODULUS_PARAM 13 /* Modulus parameter */
#define COEFUPDATETYPE 1 /* coef update type to use (1/2/3) */
#endif
......@@ -107,4 +107,8 @@ extern short vp8_default_zig_zag_mask[16];
extern DECLARE_ALIGNED(64, const int, vp8_default_zig_zag1d_8x8[64]);
extern short vp8_default_zig_zag_mask_8x8[64];//int64_t
void vp8_coef_tree_initialize(void);
#if CONFIG_ADAPTIVE_ENTROPY
void vp8_adapt_coef_probs(struct VP8Common *);
#endif
#endif
......@@ -11,6 +11,7 @@
#include "modecont.h"
#include "entropymode.h"
#include "entropymv.h"
#include "entropy.h"
#include "vpx_mem/vpx_mem.h"
......@@ -325,40 +326,6 @@ struct vp8_token_struct vp8_mbsplit_encodings [VP8_NUMMBSPLITS];
struct vp8_token_struct vp8_mv_ref_encoding_array [VP8_MVREFS];
struct vp8_token_struct vp8_sub_mv_ref_encoding_array [VP8_SUBMVREFS];
#if CONFIG_HIGH_PRECISION_MV
const vp8_tree_index vp8_small_mvtree_hp [30] =
{
2, 16,
4, 10,
6, 8,
-0, -1,
-2, -3,
12, 14,
-4, -5,
-6, -7,
18, 24,
20, 22,
-8, -9,
-10, -11,
26, 28,
-12, -13,
-14, -15
};
struct vp8_token_struct vp8_small_mvencodings_hp [16];
#endif /* CONFIG_HIGH_PRECISION_MV */
const vp8_tree_index vp8_small_mvtree [14] =
{
2, 8,
4, 6,
-0, -1,
-2, -3,
10, 12,
-4, -5,
-6, -7
};
struct vp8_token_struct vp8_small_mvencodings [8];
void vp8_init_mbmode_probs(VP8_COMMON *x)
......@@ -396,7 +363,7 @@ void vp8_init_mbmode_probs(VP8_COMMON *x)
vp8_tree_probs_from_distribution(
VP8_I8X8_MODES, vp8_i8x8_mode_encodings, vp8_i8x8_mode_tree,
x->i8x8_mode_prob, bct, i8x8_mode_cts,
x->fc.i8x8_mode_prob, bct, i8x8_mode_cts,
256, 1);
vpx_memcpy(x->fc.sub_mv_ref_prob, sub_mv_ref_prob, sizeof(sub_mv_ref_prob));
......@@ -458,11 +425,6 @@ void vp8_entropy_mode_init()
vp8_mv_ref_tree, NEARESTMV);
vp8_tokens_from_tree_offset(vp8_sub_mv_ref_encoding_array,
vp8_sub_mv_ref_tree, LEFT4X4);
vp8_tokens_from_tree(vp8_small_mvencodings, vp8_small_mvtree);
#if CONFIG_HIGH_PRECISION_MV
vp8_tokens_from_tree(vp8_small_mvencodings_hp, vp8_small_mvtree_hp);
#endif
}
void vp8_init_mode_contexts(VP8_COMMON *pc)
......@@ -595,3 +557,110 @@ void print_mv_ref_cts(VP8_COMMON *pc)
printf("\n");
}
}
#if CONFIG_ADAPTIVE_ENTROPY
//#define MODE_COUNT_TESTING
#define MODE_COUNT_SAT 16
#define MODE_MAX_UPDATE_FACTOR 96
void vp8_adapt_mode_probs(VP8_COMMON *cm)
{
int i, t, count, factor;
unsigned int branch_ct[32][2];
int update_factor = MODE_MAX_UPDATE_FACTOR; /* denominator 256 */
int count_sat = MODE_COUNT_SAT;
vp8_prob ymode_probs[VP8_YMODES-1];
vp8_prob uvmode_probs[VP8_UV_MODES-1];
vp8_prob bmode_probs[VP8_BINTRAMODES-1];
vp8_prob i8x8_mode_probs[VP8_I8X8_MODES-1];
#ifdef MODE_COUNT_TESTING
printf("static const unsigned int\nymode_counts"
"[VP8_YMODES] = {\n");
for (t = 0; t<VP8_YMODES; ++t) printf("%d, ", cm->fc.ymode_counts[t]);
printf("};\n");
printf("static const unsigned int\nuv_mode_counts"
"[VP8_YMODES] [VP8_UV_MODES] = {\n");
for (i = 0; i < VP8_YMODES; ++i)
{
printf(" {");
for (t = 0; t < VP8_UV_MODES; ++t) printf("%d, ", cm->fc.uv_mode_counts[i][t]);
printf("},\n");
}
printf("};\n");
printf("static const unsigned int\nbmode_counts"
"[VP8_BINTRAMODES] = {\n");
for (t = 0; t<VP8_BINTRAMODES; ++t) printf("%d, ", cm->fc.bmode_counts[t]);
printf("};\n");
printf("static const unsigned int\ni8x8_mode_counts"
"[VP8_I8X8_MODES] = {\n");
for (t = 0; t<VP8_I8X8_MODES; ++t) printf("%d, ", cm->fc.i8x8_mode_counts[t]);
printf("};\n");
#endif
vp8_tree_probs_from_distribution(
VP8_YMODES, vp8_ymode_encodings, vp8_ymode_tree,
ymode_probs, branch_ct, cm->fc.ymode_counts,
256, 1);
for (t=0; t<VP8_YMODES-1; ++t)
{
int prob;
count = branch_ct[t][0] + branch_ct[t][1];
count = count > count_sat ? count_sat : count;
factor = (update_factor * count / count_sat);
prob = ((int)cm->fc.pre_ymode_prob[t] * (256-factor) +
(int)ymode_probs[t] * factor + 128) >> 8;
if (prob <= 0) cm->fc.ymode_prob[t] = 1;
else if (prob > 255) cm->fc.ymode_prob[t] = 255;
else cm->fc.ymode_prob[t] = prob;
}
for (i = 0; i < VP8_YMODES; ++i)
{
vp8_tree_probs_from_distribution(
VP8_UV_MODES, vp8_uv_mode_encodings, vp8_uv_mode_tree,
uvmode_probs, branch_ct, cm->fc.uv_mode_counts[i],
256, 1);
for (t = 0; t < VP8_UV_MODES-1; ++t)
{
int prob;
count = branch_ct[t][0] + branch_ct[t][1];
count = count > count_sat ? count_sat : count;
factor = (update_factor * count / count_sat);
prob = ((int)cm->fc.pre_uv_mode_prob[i][t] * (256-factor) +
(int)uvmode_probs[t] * factor + 128) >> 8;
if (prob <= 0) cm->fc.uv_mode_prob[i][t] = 1;
else if (prob > 255) cm->fc.uv_mode_prob[i][t] = 255;
else cm->fc.uv_mode_prob[i][t] = prob;
}
}
vp8_tree_probs_from_distribution(
VP8_BINTRAMODES, vp8_bmode_encodings, vp8_bmode_tree,
bmode_probs, branch_ct, cm->fc.bmode_counts,
256, 1);
for (t=0; t<VP8_BINTRAMODES-1; ++t)
{
int prob;
count = branch_ct[t][0] + branch_ct[t][1];
count = count > count_sat ? count_sat : count;
factor = (update_factor * count / count_sat);
prob = ((int)cm->fc.pre_bmode_prob[t] * (256-factor) +
(int)bmode_probs[t] * factor + 128) >> 8;
if (prob <= 0) cm->fc.bmode_prob[t] = 1;
else if (prob > 255) cm->fc.bmode_prob[t] = 255;
else cm->fc.bmode_prob[t] = prob;
}
vp8_tree_probs_from_distribution(
VP8_I8X8_MODES, vp8_i8x8_mode_encodings, vp8_i8x8_mode_tree,
i8x8_mode_probs, branch_ct, cm->fc.i8x8_mode_counts,
256, 1);
for (t=0; t<VP8_I8X8_MODES-1; ++t)
{
int prob;
count = branch_ct[t][0] + branch_ct[t][1];
count = count > count_sat ? count_sat : count;
factor = (update_factor * count / count_sat);
prob = ((int)cm->fc.pre_i8x8_mode_prob[t] * (256-factor) +
(int)i8x8_mode_probs[t] * factor + 128) >> 8;
if (prob <= 0) cm->fc.i8x8_mode_prob[t] = 1;
else if (prob > 255) cm->fc.i8x8_mode_prob[t] = 255;
else cm->fc.i8x8_mode_prob[t] = prob;
}
}
#endif
......@@ -55,13 +55,6 @@ extern struct vp8_token_struct vp8_mbsplit_encodings [VP8_NUMMBSPLITS];
extern struct vp8_token_struct vp8_mv_ref_encoding_array [VP8_MVREFS];
extern struct vp8_token_struct vp8_sub_mv_ref_encoding_array [VP8_SUBMVREFS];
extern const vp8_tree_index vp8_small_mvtree[];
extern struct vp8_token_struct vp8_small_mvencodings [8];
#if CONFIG_HIGH_PRECISION_MV
extern const vp8_tree_index vp8_small_mvtree_hp[];
extern struct vp8_token_struct vp8_small_mvencodings_hp [16];
#endif
void vp8_entropy_mode_init(void);
void vp8_init_mbmode_probs(VP8_COMMON *x);
......@@ -71,7 +64,10 @@ extern void vp8_accum_mv_refs(VP8_COMMON *pc,
MB_PREDICTION_MODE m,
const int ct[4]);
void vp8_default_bmode_probs(vp8_prob dest [VP8_BINTRAMODES-1]);
void vp8_default_bmode_probs(vp8_prob dest [VP8_BINTRAMODES-1]);
void vp8_kf_default_bmode_probs(vp8_prob dest [VP8_BINTRAMODES] [VP8_BINTRAMODES] [VP8_BINTRAMODES-1]);
#if CONFIG_ADAPTIVE_ENTROPY
void vp8_adapt_mode_probs(struct VP8Common *);
#endif
#endif
......@@ -9,6 +9,7 @@
*/
#include "onyxc_int.h"
#include "entropymv.h"
#if CONFIG_HIGH_PRECISION_MV
......@@ -78,3 +79,361 @@ const MV_CONTEXT vp8_default_mv_context[2] =
128, 130, 130, 74, 148, 180, 203, 236, 254, 254 /* long bits */
}}
};
#if CONFIG_HIGH_PRECISION_MV
const vp8_tree_index vp8_small_mvtree_hp [30] =
{
2, 16,
4, 10,
6, 8,
-0, -1,
-2, -3,
12, 14,
-4, -5,
-6, -7,
18, 24,
20, 22,
-8, -9,
-10, -11,
26, 28,
-12, -13,
-14, -15
};
struct vp8_token_struct vp8_small_mvencodings_hp [16];
#endif /* CONFIG_HIGH_PRECISION_MV */
const vp8_tree_index vp8_small_mvtree [14] =
{
2, 8,
4, 6,
-0, -1,
-2, -3,
10, 12,
-4, -5,
-6, -7
};
struct vp8_token_struct vp8_small_mvencodings [8];
__inline static void calc_prob(vp8_prob *p, const unsigned int ct[2], int pbits)
{
const unsigned int tot = ct[0] + ct[1];
if (tot)
{
const vp8_prob x = ((ct[0] * 255) / tot) & -(1<<(8-pbits));
*p = x ? x : 1;
}
}
static void compute_component_probs(
const unsigned int events [MVvals],
vp8_prob Pnew [MVPcount],
unsigned int is_short_ct[2],
unsigned int sign_ct[2],
unsigned int bit_ct [mvlong_width] [2],
unsigned int short_ct [mvnum_short],
unsigned int short_bct [mvnum_short-1] [2]
)
{
is_short_ct[0] = is_short_ct[1] = 0;
sign_ct[0] = sign_ct[1] = 0;
vpx_memset(bit_ct, 0, sizeof(unsigned int)*mvlong_width*2);
vpx_memset(short_ct, 0, sizeof(unsigned int)*mvnum_short);
vpx_memset(short_bct, 0, sizeof(unsigned int)*(mvnum_short-1)*2);
{
const int c = events [mv_max];
is_short_ct [0] += c; // Short vector
short_ct [0] += c; // Magnitude distribution
}
{
int j = 1;
do
{
const int c1 = events [mv_max + j]; //positive
const int c2 = events [mv_max - j]; //negative
const int c = c1 + c2;
int a = j;
sign_ct [0] += c1;
sign_ct [1] += c2;
if (a < mvnum_short)
{
is_short_ct [0] += c; // Short vector
short_ct [a] += c; // Magnitude distribution
}
else
{
int k = mvlong_width - 1;
is_short_ct [1] += c; // Long vector
do
bit_ct [k] [(a >> k) & 1] += c;
while (--k >= 0);
}
}
while (++j <= mv_max);
}
calc_prob(Pnew + mvpis_short, is_short_ct, 8);
calc_prob(Pnew + MVPsign, sign_ct, 8);
{
vp8_prob p [mvnum_short - 1]; /* actually only need branch ct */
int j = 0;
vp8_tree_probs_from_distribution(
mvnum_short, vp8_small_mvencodings, vp8_small_mvtree,
p, short_bct, short_ct,
256, 1
);
do
calc_prob(Pnew + MVPshort + j, short_bct[j], 8);
while (++j < mvnum_short - 1);
}
{
int j = 0;
do
calc_prob(Pnew + MVPbits + j, bit_ct[j], 8);
while (++j < mvlong_width);
}
}
#if CONFIG_HIGH_PRECISION_MV
static void compute_component_probs_hp(
const unsigned int events [MVvals_hp],
vp8_prob Pnew [MVPcount_hp],
unsigned int is_short_ct[2],
unsigned int sign_ct[2],
unsigned int bit_ct [mvlong_width_hp] [2],
unsigned int short_ct [mvnum_short_hp],
unsigned int short_bct [mvnum_short_hp-1] [2]
)
{
is_short_ct[0] = is_short_ct[1] = 0;
sign_ct[0] = sign_ct[1] = 0;
vpx_memset(bit_ct, 0, sizeof(unsigned int)*mvlong_width_hp*2);
vpx_memset(short_ct, 0, sizeof(unsigned int)*mvnum_short_hp);
vpx_memset(short_bct, 0, sizeof(unsigned int)*(mvnum_short_hp-1)*2);
{
const int c = events [mv_max_hp];
is_short_ct [0] += c; // Short vector
short_ct [0] += c; // Magnitude distribution
}
{
int j = 1;
do
{
const int c1 = events [mv_max_hp + j]; //positive
const int c2 = events [mv_max_hp - j]; //negative
const int c = c1 + c2;
int a = j;
sign_ct [0] += c1;
sign_ct [1] += c2;
if (a < mvnum_short_hp)
{
is_short_ct [0] += c; // Short vector
short_ct [a] += c; // Magnitude distribution
}
else
{
int k = mvlong_width_hp - 1;
is_short_ct [1] += c; // Long vector
do
bit_ct [k] [(a >> k) & 1] += c;
while (--k >= 0);
}
}
while (++j <= mv_max_hp);
}
calc_prob(Pnew + mvpis_short_hp, is_short_ct, 8);
calc_prob(Pnew + MVPsign_hp, sign_ct, 8);
{
vp8_prob p [mvnum_short_hp - 1]; /* actually only need branch ct */
int j = 0;
vp8_tree_probs_from_distribution(
mvnum_short_hp, vp8_small_mvencodings_hp, vp8_small_mvtree_hp,
p, short_bct, short_ct,
256, 1
);
do
calc_prob(Pnew + MVPshort_hp + j, short_bct[j], 8);
while (++j < mvnum_short_hp - 1);
}
{
int j = 0;
do
calc_prob(Pnew + MVPbits_hp + j, bit_ct[j], 8);
while (++j < mvlong_width_hp);
}
}
#endif /* CONFIG_HIGH_PRECISION_MV */
void vp8_entropy_mv_init()
{
vp8_tokens_from_tree(vp8_small_mvencodings, vp8_small_mvtree);
#if CONFIG_HIGH_PRECISION_MV
vp8_tokens_from_tree(vp8_small_mvencodings_hp, vp8_small_mvtree_hp);
#endif
}
#if CONFIG_ADAPTIVE_ENTROPY
//#define MV_COUNT_TESTING