Commit 226e55c1 authored by John Koleszar's avatar John Koleszar

coef_probs: remove duplicate read/update code

Refactor per-transform copy & paste into a common function
update_coef_probs_common() and read_coef_probs_common(). The dry-run and
bit-writing loops in the encoder are still obvious candidates to be made
common, but they start to diverge a bit in the next commit, so are left
as-is for now.

Change-Id: I896bd3f4a073a6296eab7e92463fef79d8c6c08c
parent 6baa2b47
......@@ -857,122 +857,47 @@ static void read_coef_probs2(VP8D_COMP *pbi) {
}
#endif
static void read_coef_probs(VP8D_COMP *pbi, BOOL_DECODER* const bc) {
static void read_coef_probs_common(
BOOL_DECODER* const bc,
vp8_prob coef_probs[BLOCK_TYPES][COEF_BANDS]
[PREV_COEF_CONTEXTS][ENTROPY_NODES]) {
int i, j, k, l;
VP8_COMMON *const pc = &pbi->common;
{
if (vp8_read_bit(bc)) {
/* read coef probability tree */
for (i = 0; i < BLOCK_TYPES; i++)
for (j = !i; j < COEF_BANDS; j++)
for (k = 0; k < PREV_COEF_CONTEXTS; k++) {
if (k >= 3 && ((i == 0 && j == 1) ||
(i > 0 && j == 0)))
continue;
for (l = 0; l < ENTROPY_NODES; l++) {
vp8_prob *const p = pc->fc.coef_probs [i][j][k] + l;
if (vp8_read(bc, COEF_UPDATE_PROB)) {
*p = read_prob_diff_update(bc, *p);
}
}
}
}
}
{
if (vp8_read_bit(bc)) {
/* read coef probability tree */
for (i = 0; i < BLOCK_TYPES; i++)
for (j = !i; j < COEF_BANDS; j++)
for (k = 0; k < PREV_COEF_CONTEXTS; k++) {
if (k >= 3 && ((i == 0 && j == 1) ||
(i > 0 && j == 0)))
continue;
for (l = 0; l < ENTROPY_NODES; l++) {
vp8_prob *const p = pc->fc.hybrid_coef_probs [i][j][k] + l;
if (vp8_read(bc, COEF_UPDATE_PROB)) {
*p = read_prob_diff_update(bc, *p);
}
}
}
}
}
if (pbi->common.txfm_mode != ONLY_4X4 && vp8_read_bit(bc)) {
// read coef probability tree
for (i = 0; i < BLOCK_TYPES_8X8; i++)
for (j = !i; j < COEF_BANDS; j++)
for (k = 0; k < PREV_COEF_CONTEXTS; k++) {
if (k >= 3 && ((i == 0 && j == 1) ||
(i > 0 && j == 0)))
continue;
for (l = 0; l < ENTROPY_NODES; l++) {
vp8_prob *const p = pc->fc.coef_probs_8x8 [i][j][k] + l;
if (vp8_read(bc, COEF_UPDATE_PROB_8X8)) {
*p = read_prob_diff_update(bc, *p);
}
}
}
}
if (pbi->common.txfm_mode != ONLY_4X4 && vp8_read_bit(bc)) {
// read coef probability tree
for (i = 0; i < BLOCK_TYPES_8X8; i++)
for (j = !i; j < COEF_BANDS; j++)
if (vp8_read_bit(bc)) {
for (i = 0; i < BLOCK_TYPES; i++) {
for (j = !i; j < COEF_BANDS; j++) {
/* NB: This j loop starts from 1 on block type i == 0 */
for (k = 0; k < PREV_COEF_CONTEXTS; k++) {
if (k >= 3 && ((i == 0 && j == 1) ||
(i > 0 && j == 0)))
continue;
for (l = 0; l < ENTROPY_NODES; l++) {
vp8_prob *const p = coef_probs[i][j][k] + l;
vp8_prob *const p = pc->fc.hybrid_coef_probs_8x8 [i][j][k] + l;
if (vp8_read(bc, COEF_UPDATE_PROB_8X8)) {
if (vp8_read(bc, COEF_UPDATE_PROB)) {
*p = read_prob_diff_update(bc, *p);
}
}
}
}
}
}
}
// 16x16
if (pbi->common.txfm_mode > ALLOW_8X8 && vp8_read_bit(bc)) {
// read coef probability tree
for (i = 0; i < BLOCK_TYPES_16X16; ++i)
for (j = !i; j < COEF_BANDS; ++j)
for (k = 0; k < PREV_COEF_CONTEXTS; ++k) {
if (k >= 3 && ((i == 0 && j == 1) ||
(i > 0 && j == 0)))
continue;
for (l = 0; l < ENTROPY_NODES; ++l) {
vp8_prob *const p = pc->fc.coef_probs_16x16[i][j][k] + l;
static void read_coef_probs(VP8D_COMP *pbi, BOOL_DECODER* const bc) {
VP8_COMMON *const pc = &pbi->common;
if (vp8_read(bc, COEF_UPDATE_PROB_16X16)) {
*p = read_prob_diff_update(bc, *p);
}
}
}
read_coef_probs_common(bc, pc->fc.coef_probs);
#if CONFIG_HYBRIDTRANSFORM
read_coef_probs_common(bc, pc->fc.hybrid_coef_probs);
#endif
if (pbi->common.txfm_mode != ONLY_4X4) {
read_coef_probs_common(bc, pc->fc.coef_probs_8x8);
read_coef_probs_common(bc, pc->fc.hybrid_coef_probs_8x8);
}
if (pbi->common.txfm_mode > ALLOW_8X8 && vp8_read_bit(bc)) {
// read coef probability tree
for (i = 0; i < BLOCK_TYPES_16X16; ++i)
for (j = !i; j < COEF_BANDS; ++j)
for (k = 0; k < PREV_COEF_CONTEXTS; ++k) {
if (k >= 3 && ((i == 0 && j == 1) ||
(i > 0 && j == 0)))
continue;
for (l = 0; l < ENTROPY_NODES; ++l) {
vp8_prob *const p = pc->fc.hybrid_coef_probs_16x16[i][j][k] + l;
if (vp8_read(bc, COEF_UPDATE_PROB_16X16)) {
*p = read_prob_diff_update(bc, *p);
}
}
}
if (pbi->common.txfm_mode > ALLOW_8X8) {
read_coef_probs_common(bc, pc->fc.coef_probs_16x16);
read_coef_probs_common(bc, pc->fc.hybrid_coef_probs_16x16);
}
}
......
......@@ -1494,185 +1494,17 @@ void build_coeff_contexts(VP8_COMP *cpi) {
}
}
#if 0
static void update_coef_probs2(VP8_COMP *cpi) {
const vp8_prob grpupd = 192;
int i, j, k, t;
vp8_writer *const w = &cpi->bc;
int update[2];
int savings;
vp8_clear_system_state(); // __asm emms;
// Build the cofficient contexts based on counts collected in encode loop
build_coeff_contexts(cpi);
for (t = 0; t < ENTROPY_NODES; ++t) {
/* dry run to see if there is any udpate at all needed */
savings = 0;
update[0] = update[1] = 0;
for (i = 0; i < BLOCK_TYPES; ++i) {
for (j = !i; j < COEF_BANDS; ++j) {
for (k = 0; k < PREV_COEF_CONTEXTS; ++k) {
vp8_prob newp = cpi->frame_coef_probs [i][j][k][t];
vp8_prob *Pold = cpi->common.fc.coef_probs [i][j][k] + t;
const vp8_prob upd = COEF_UPDATE_PROB;
int s;
int u = 0;
if (k >= 3 && ((i == 0 && j == 1) || (i > 0 && j == 0)))
continue;
#if defined(SEARCH_NEWP)
s = prob_diff_update_savings_search(
cpi->frame_branch_ct [i][j][k][t], *Pold, &newp, upd);
if (s > 0 && newp != *Pold) u = 1;
if (u)
savings += s - (int)(vp8_cost_zero(upd));
else
savings -= (int)(vp8_cost_zero(upd));
#else
s = prob_update_savings(
cpi->frame_branch_ct [i][j][k][t], *Pold, newp, upd);
if (s > 0) u = 1;
if (u)
savings += s;
#endif
// printf(" %d %d %d: %d\n", i, j, k, u);
update[u]++;
}
}
}
if (update[1] == 0 || savings < 0) {
vp8_write(w, 0, grpupd);
continue;
}
vp8_write(w, 1, grpupd);
for (i = 0; i < BLOCK_TYPES; ++i) {
for (j = !i; j < COEF_BANDS; ++j) {
for (k = 0; k < PREV_COEF_CONTEXTS; ++k) {
vp8_prob newp = cpi->frame_coef_probs [i][j][k][t];
vp8_prob *Pold = cpi->common.fc.coef_probs [i][j][k] + t;
const vp8_prob upd = COEF_UPDATE_PROB;
int s;
int u = 0;
if (k >= 3 && ((i == 0 && j == 1) || (i > 0 && j == 0)))
continue;
#if defined(SEARCH_NEWP)
s = prob_diff_update_savings_search(
cpi->frame_branch_ct [i][j][k][t], *Pold, &newp, upd);
if (s > 0 && newp != *Pold) u = 1;
#else
s = prob_update_savings(
cpi->frame_branch_ct [i][j][k][t], *Pold, newp, upd);
if (s > 0) u = 1;
#endif
// printf(" %d %d %d: %d (%d)\n", i, j, k, u, upd);
vp8_write(w, u, upd);
#ifdef ENTROPY_STATS
++ tree_update_hist [i][j][k][t] [u];
#endif
if (u) {
/* send/use new probability */
write_prob_diff_update(w, newp, *Pold);
*Pold = newp;
}
}
}
}
}
if (cpi->common.txfm_mode != ONLY_4X4)
for (t = 0; t < ENTROPY_NODES; ++t) {
/* dry run to see if there is any udpate at all needed */
savings = 0;
update[0] = update[1] = 0;
for (i = 0; i < BLOCK_TYPES_8X8; ++i) {
for (j = !i; j < COEF_BANDS; ++j) {
for (k = 0; k < PREV_COEF_CONTEXTS; ++k) {
vp8_prob newp = cpi->frame_coef_probs_8x8 [i][j][k][t];
vp8_prob *Pold = cpi->common.fc.coef_probs_8x8 [i][j][k] + t;
const vp8_prob upd = COEF_UPDATE_PROB_8X8;
int s;
int u = 0;
if (k >= 3 && ((i == 0 && j == 1) || (i > 0 && j == 0)))
continue;
#if defined(SEARCH_NEWP)
s = prob_diff_update_savings_search(
cpi->frame_branch_ct_8x8 [i][j][k][t],
*Pold, &newp, upd);
if (s > 0 && newp != *Pold)
u = 1;
if (u)
savings += s - (int)(vp8_cost_zero(upd));
else
savings -= (int)(vp8_cost_zero(upd));
#else
s = prob_update_savings(
cpi->frame_branch_ct_8x8 [i][j][k][t],
*Pold, newp, upd);
if (s > 0)
u = 1;
if (u)
savings += s;
#endif
update[u]++;
}
}
}
if (update[1] == 0 || savings < 0) {
vp8_write(w, 0, grpupd);
continue;
}
vp8_write(w, 1, grpupd);
for (i = 0; i < BLOCK_TYPES_8X8; ++i) {
for (j = !i; j < COEF_BANDS; ++j) {
for (k = 0; k < PREV_COEF_CONTEXTS; ++k) {
vp8_prob newp = cpi->frame_coef_probs_8x8 [i][j][k][t];
vp8_prob *Pold = cpi->common.fc.coef_probs_8x8 [i][j][k] + t;
const vp8_prob upd = COEF_UPDATE_PROB_8X8;
int s;
int u = 0;
if (k >= 3 && ((i == 0 && j == 1) || (i > 0 && j == 0)))
continue;
#if defined(SEARCH_NEWP)
s = prob_diff_update_savings_search(
cpi->frame_branch_ct_8x8 [i][j][k][t],
*Pold, &newp, upd);
if (s > 0 && newp != *Pold)
u = 1;
#else
s = prob_update_savings(
cpi->frame_branch_ct_8x8 [i][j][k][t],
*Pold, newp, upd);
if (s > 0)
u = 1;
#endif
vp8_write(w, u, upd);
#ifdef ENTROPY_STATS
if (!cpi->dummy_packing)
++ tree_update_hist_8x8 [i][j][k][t] [u];
#endif
if (u) {
/* send/use new probability */
write_prob_diff_update(w, newp, *Pold);
*Pold = newp;
}
}
}
}
}
}
#endif
static void update_coef_probs(VP8_COMP* const cpi, vp8_writer* const bc) {
static void update_coef_probs_common(
vp8_writer* const bc,
vp8_prob new_frame_coef_probs[BLOCK_TYPES][COEF_BANDS]
[PREV_COEF_CONTEXTS][ENTROPY_NODES],
vp8_prob old_frame_coef_probs[BLOCK_TYPES][COEF_BANDS]
[PREV_COEF_CONTEXTS][ENTROPY_NODES],
unsigned int frame_branch_ct[BLOCK_TYPES][COEF_BANDS]
[PREV_COEF_CONTEXTS][ENTROPY_NODES][2]) {
int i, j, k, t;
int update[2] = {0, 0};
int savings;
vp8_clear_system_state(); // __asm emms;
// Build the cofficient contexts based on counts collected in encode loop
build_coeff_contexts(cpi);
// vp8_prob bestupd = find_coef_update_prob(cpi);
/* dry run to see if there is any udpate at all needed */
......@@ -1682,8 +1514,8 @@ static void update_coef_probs(VP8_COMP* const cpi, vp8_writer* const bc) {
int prev_coef_savings[ENTROPY_NODES] = {0};
for (k = 0; k < PREV_COEF_CONTEXTS; ++k) {
for (t = 0; t < ENTROPY_NODES; ++t) {
vp8_prob newp = cpi->frame_coef_probs [i][j][k][t];
vp8_prob *Pold = cpi->common.fc.coef_probs [i][j][k] + t;
vp8_prob newp = new_frame_coef_probs[i][j][k][t];
const vp8_prob oldp = old_frame_coef_probs[i][j][k][t];
const vp8_prob upd = COEF_UPDATE_PROB;
int s = prev_coef_savings[t];
int u = 0;
......@@ -1691,9 +1523,9 @@ static void update_coef_probs(VP8_COMP* const cpi, vp8_writer* const bc) {
continue;
#if defined(SEARCH_NEWP)
s = prob_diff_update_savings_search(
cpi->frame_branch_ct [i][j][k][t],
*Pold, &newp, upd);
if (s > 0 && newp != *Pold)
frame_branch_ct[i][j][k][t],
oldp, &newp, upd);
if (s > 0 && newp != oldp)
u = 1;
if (u)
savings += s - (int)(vp8_cost_zero(upd));
......@@ -1701,8 +1533,8 @@ static void update_coef_probs(VP8_COMP* const cpi, vp8_writer* const bc) {
savings -= (int)(vp8_cost_zero(upd));
#else
s = prob_update_savings(
cpi->frame_branch_ct [i][j][k][t],
*Pold, newp, upd);
frame_branch_ct[i][j][k][t],
oldp, newp, upd);
if (s > 0)
u = 1;
if (u)
......@@ -1727,8 +1559,8 @@ static void update_coef_probs(VP8_COMP* const cpi, vp8_writer* const bc) {
for (k = 0; k < PREV_COEF_CONTEXTS; ++k) {
// calc probs and branch cts for this frame only
for (t = 0; t < ENTROPY_NODES; ++t) {
vp8_prob newp = cpi->frame_coef_probs [i][j][k][t];
vp8_prob *Pold = cpi->common.fc.coef_probs [i][j][k] + t;
vp8_prob newp = new_frame_coef_probs[i][j][k][t];
vp8_prob *oldp = old_frame_coef_probs[i][j][k] + t;
const vp8_prob upd = COEF_UPDATE_PROB;
int s = prev_coef_savings[t];
int u = 0;
......@@ -1737,14 +1569,14 @@ static void update_coef_probs(VP8_COMP* const cpi, vp8_writer* const bc) {
#if defined(SEARCH_NEWP)
s = prob_diff_update_savings_search(
cpi->frame_branch_ct [i][j][k][t],
*Pold, &newp, upd);
if (s > 0 && newp != *Pold)
frame_branch_ct[i][j][k][t],
*oldp, &newp, upd);
if (s > 0 && newp != *oldp)
u = 1;
#else
s = prob_update_savings(
cpi->frame_branch_ct [i][j][k][t],
*Pold, newp, upd);
frame_branch_ct[i][j][k][t],
*oldp, newp, upd);
if (s > 0)
u = 1;
#endif
......@@ -1755,403 +1587,54 @@ static void update_coef_probs(VP8_COMP* const cpi, vp8_writer* const bc) {
#endif
if (u) {
/* send/use new probability */
write_prob_diff_update(bc, newp, *Pold);
*Pold = newp;
write_prob_diff_update(bc, newp, *oldp);
*oldp = newp;
}
}
}
}
}
}
}
savings = 0;
update[0] = update[1] = 0;
for (i = 0; i < BLOCK_TYPES; ++i) {
for (j = !i; j < COEF_BANDS; ++j) {
int prev_coef_savings[ENTROPY_NODES] = {0};
for (k = 0; k < PREV_COEF_CONTEXTS; ++k) {
for (t = 0; t < ENTROPY_NODES; ++t) {
vp8_prob newp = cpi->frame_hybrid_coef_probs [i][j][k][t];
vp8_prob *Pold = cpi->common.fc.hybrid_coef_probs [i][j][k] + t;
const vp8_prob upd = COEF_UPDATE_PROB;
int s = prev_coef_savings[t];
int u = 0;
if (k >= 3 && ((i == 0 && j == 1) || (i > 0 && j == 0)))
continue;
#if defined(SEARCH_NEWP)
s = prob_diff_update_savings_search(
cpi->frame_hybrid_branch_ct [i][j][k][t],
*Pold, &newp, upd);
if (s > 0 && newp != *Pold)
u = 1;
if (u)
savings += s - (int)(vp8_cost_zero(upd));
else
savings -= (int)(vp8_cost_zero(upd));
#else
s = prob_update_savings(
cpi->frame_hybrid_branch_ct [i][j][k][t],
*Pold, newp, upd);
if (s > 0)
u = 1;
if (u)
savings += s;
#endif
static void update_coef_probs(VP8_COMP* const cpi, vp8_writer* const bc) {
vp8_clear_system_state();
update[u]++;
}
}
}
}
// Build the cofficient contexts based on counts collected in encode loop
build_coeff_contexts(cpi);
// printf("Update %d %d, savings %d\n", update[0], update[1], savings);
/* Is coef updated at all */
if (update[1] == 0 || savings < 0) {
vp8_write_bit(bc, 0);
} else {
vp8_write_bit(bc, 1);
for (i = 0; i < BLOCK_TYPES; ++i) {
for (j = !i; j < COEF_BANDS; ++j) {
int prev_coef_savings[ENTROPY_NODES] = {0};
for (k = 0; k < PREV_COEF_CONTEXTS; ++k) {
// calc probs and branch cts for this frame only
for (t = 0; t < ENTROPY_NODES; ++t) {
vp8_prob newp = cpi->frame_hybrid_coef_probs [i][j][k][t];
vp8_prob *Pold = cpi->common.fc.hybrid_coef_probs [i][j][k] + t;
const vp8_prob upd = COEF_UPDATE_PROB;
int s = prev_coef_savings[t];
int u = 0;
if (k >= 3 && ((i == 0 && j == 1) || (i > 0 && j == 0)))
continue;
update_coef_probs_common(bc,
cpi->frame_coef_probs,
cpi->common.fc.coef_probs,
cpi->frame_branch_ct);
#if defined(SEARCH_NEWP)
s = prob_diff_update_savings_search(
cpi->frame_hybrid_branch_ct [i][j][k][t],
*Pold, &newp, upd);
if (s > 0 && newp != *Pold)
u = 1;
#else
s = prob_update_savings(
cpi->frame_hybrid_branch_ct [i][j][k][t],
*Pold, newp, upd);
if (s > 0)
u = 1;
#endif
vp8_write(bc, u, upd);
#ifdef ENTROPY_STATS
if (!cpi->dummy_packing)
++ hybrid_tree_update_hist [i][j][k][t] [u];
#endif
if (u) {
/* send/use new probability */
write_prob_diff_update(bc, newp, *Pold);
*Pold = newp;
}
}
}
}
}
}
update_coef_probs_common(bc,
cpi->frame_hybrid_coef_probs,
cpi->common.fc.hybrid_coef_probs,
cpi->frame_hybrid_branch_ct);
/* do not do this if not even allowed */
if (cpi->common.txfm_mode != ONLY_4X4) {
/* dry run to see if update is necessary */
update[0] = update[1] = 0;
savings = 0;
for (i = 0; i < BLOCK_TYPES_8X8; ++i) {
for (j = !i; j < COEF_BANDS; ++j) {
for (k = 0; k < PREV_COEF_CONTEXTS; ++k) {
// calc probs and branch cts for this frame only
for (t = 0; t < ENTROPY_NODES; ++t) {
const unsigned int *ct = cpi->frame_branch_ct_8x8 [i][j][k][t];
vp8_prob newp = cpi->frame_coef_probs_8x8 [i][j][k][t];
vp8_prob *Pold = cpi->common.fc.coef_probs_8x8 [i][j][k] + t;
const vp8_prob oldp = *Pold;
int s, u;
const vp8_prob upd = COEF_UPDATE_PROB_8X8;
if (k >= 3 && ((i == 0 && j == 1) || (i > 0 && j == 0)))
continue;
#if defined(SEARCH_NEWP)
s = prob_diff_update_savings_search(ct, oldp, &newp, upd);
u = s > 0 && newp != oldp ? 1 : 0;
if (u)
savings += s - (int)(vp8_cost_zero(upd));
else
savings -= (int)(vp8_cost_zero(upd));
#else
s = prob_update_savings(ct, oldp, newp, upd);
u = s > 0 ? 1 : 0;
if (u)
savings += s;
#endif
update[u]++;
}
}
}
}
if (update[1] == 0 || savings < 0) {
vp8_write_bit(bc, 0);
} else {
vp8_write_bit(bc, 1);
for (i = 0; i < BLOCK_TYPES_8X8; ++i) {
for (j = !i; j < COEF_BANDS; ++j) {
for (k = 0; k < PREV_COEF_CONTEXTS; ++k) {
for (t = 0; t < ENTROPY_NODES; ++t) {
const unsigned int *ct = cpi->frame_branch_ct_8x8 [i][j][k][t];
vp8_prob newp = cpi->frame_coef_probs_8x8 [i][j][k][t];
vp8_prob *Pold = cpi->common.fc.coef_probs_8x8 [i][j][k] + t;
const vp8_prob oldp = *Pold;
const vp8_prob upd = COEF_UPDATE_PROB_8X8;
int s, u;
if (k >= 3 && ((i == 0 && j == 1) ||
(i > 0 && j == 0)))
continue;
#if defined(SEARCH_NEWP)
s = prob_diff_update_savings_search(ct, oldp, &newp, upd);
u = s > 0 && newp != oldp ? 1 : 0;
#else
s = prob_update_savings(ct, oldp, newp, upd);
u = s > 0 ? 1 : 0;
#endif
vp8_write(bc, u, upd);
#ifdef ENTROPY_STATS
if (!cpi->dummy_packing)
++ tree_update_hist_8x8 [i][j][k][t] [u];
#endif
if (u) {
/* send/use new probability */
write_prob_diff_update(bc, newp, oldp);
*Pold = newp;
}