Commit 5d0b5a17 authored by Adrian Grange's avatar Adrian Grange
Browse files

Added encoding in Superblock Order

As a precursor to encoding 32x32 blocks this cl adds the
ability to encode the frame superblock (=32x32 block) at
a time. Within a SB the 4 indiviual MBs are encoded in
raster-order (NW,NE,SW,SE).

This functionality is added as an experiment which can be
enabled by ispecifying --enable-superblocks in the
command line specified to configure (CONFIG_SUPERBLOCKS
macro in the code).

To make this work I had to disable the two intra
prediction modes that use data from the top-right of the
MB.

On the tests that I have run the results produce
almost exactly the same PSNRs & SSIMs with a very
slightly higher average data rate (and slightly higher
data rate than just disabling the two intra modes in
the original code).

NOTE: This will also break the multi-threaded code.

This replaces the abandoned change:
Iebebe0d1a50ce8c15c79862c537b765a2f67e162

Change-Id: I1bc1a00f236abc1a373c7210d756e25f970fcad8
parent 92ffb17c
......@@ -229,6 +229,7 @@ EXPERIMENT_LIST="
compred
newlpf
enhanced_interp
superblocks
"
CONFIG_LIST="
external_build
......
......@@ -58,7 +58,7 @@ static int vp8_read_uv_mode(vp8_reader *bc, const vp8_prob *p)
return i;
}
// This function reads the current macro block's segnment id to from bitstream
// This function reads the current macro block's segnent id from the bitstream
// It should only be called if a segment map update is indicated.
static void vp8_read_mb_segid(vp8_reader *r, MB_MODE_INFO *mi, MACROBLOCKD *x)
{
......@@ -923,6 +923,108 @@ static void read_mb_modes_mv(VP8D_COMP *pbi, MODE_INFO *mi, MB_MODE_INFO *mbmi,
}
#if CONFIG_SUPERBLOCKS
void vp8_decode_mode_mvs(VP8D_COMP *pbi)
{
int i;
VP8_COMMON *cm = &pbi->common;
int sb_row, sb_col;
int sb_rows = (cm->mb_rows + 1)>>1;
int sb_cols = (cm->mb_cols + 1)>>1;
MODE_INFO *mi = cm->mi;
int row_delta[4] = {-1, 0, +1, 0};
int col_delta[4] = {+1, +1, -1, +1};
#if CONFIG_NEWNEAR
MODE_INFO *prev_mi = cm->prev_mi;
#endif
mb_mode_mv_init(pbi);
#if CONFIG_QIMODE
if(cm->frame_type==KEY_FRAME && !cm->kf_ymode_probs_update)
{
cm->kf_ymode_probs_index = vp8_read_literal(&pbi->bc, 3);
}
#endif
for (sb_row=0; sb_row<sb_rows; sb_row++)
{
int mb_col = -col_delta[0];
int mb_row = (sb_row <<1)-row_delta[0];
for (sb_col=0; sb_col<sb_cols; sb_col++)
{
for ( i=0; i<4; i++ )
{
int mb_to_top_edge;
int mb_to_bottom_edge;
#if CONFIG_ERROR_CONCEALMENT
int mb_num;
#endif
int offset_extended = row_delta[(i+1) & 0x3]
* cm->mode_info_stride + col_delta[(i+1) & 0x3];
int dy = row_delta[i];
int dx = col_delta[i];
mb_row += dy;
mb_col += dx;
if ((mb_row >= cm->mb_rows) || (mb_col >= cm->mb_cols))
{
#if CONFIG_NEWNEAR
prev_mi += offset_extended;
#endif
mi += offset_extended; /* next macroblock */
continue;
}
pbi->mb.mb_to_top_edge =
mb_to_top_edge = -((mb_row * 16)) << 3;
mb_to_top_edge -= LEFT_TOP_MARGIN;
pbi->mb.mb_to_bottom_edge =
mb_to_bottom_edge = ((pbi->common.mb_rows - 1 - mb_row) * 16) << 3;
mb_to_bottom_edge += RIGHT_BOTTOM_MARGIN;
#if CONFIG_ERROR_CONCEALMENT
mb_num = mb_row * pbi->common.mb_cols + mb_col;
#endif
/*read_mb_modes_mv(pbi, cm->mode_info_context, &cm->mode_info_context->mbmi, mb_row, mb_col);*/
if(pbi->common.frame_type == KEY_FRAME)
vp8_kfread_modes(pbi, mi, mb_row, mb_col);
else
read_mb_modes_mv(pbi, mi, &mi->mbmi,
#if CONFIG_NEWNEAR
prev_mi,
#endif
mb_row, mb_col);
#if CONFIG_ERROR_CONCEALMENT
/* look for corruption. set mvs_corrupt_from_mb to the current
* mb_num if the frame is corrupt from this macroblock. */
if (vp8dx_bool_error(&pbi->bc) && mb_num < pbi->mvs_corrupt_from_mb)
{
pbi->mvs_corrupt_from_mb = mb_num;
/* no need to continue since the partition is corrupt from
* here on.
*/
return;
}
#endif
#if CONFIG_NEWNEAR
prev_mi += offset_extended;
#endif
mi += offset_extended; /* next macroblock */
}
}
mi += cm->mode_info_stride + (1 - (cm->mb_cols & 0x1));
}
}
#else
void vp8_decode_mode_mvs(VP8D_COMP *pbi)
{
MODE_INFO *mi = pbi->common.mi;
......@@ -1036,3 +1138,5 @@ void vp8_decode_mode_mvs(VP8D_COMP *pbi)
}
#endif /* CONFIG_SUPERBLOCKS */
......@@ -248,7 +248,9 @@ static void decode_macroblock(VP8D_COMP *pbi, MACROBLOCKD *xd,
RECON_INVOKE(&pbi->common.rtcd.recon,
build_intra_predictors_mby)(xd);
} else {
#if !CONFIG_SUPERBLOCKS
vp8_intra_prediction_down_copy(xd);
#endif
}
}
}
......@@ -464,8 +466,163 @@ static int get_delta_q(vp8_reader *bc, int prev, int *q_update)
FILE *vpxlog = 0;
#endif
#if CONFIG_SUPERBLOCKS
static void
decode_sb_row(VP8D_COMP *pbi, VP8_COMMON *pc, int mbrow, MACROBLOCKD *xd)
{
int i;
int recon_yoffset, recon_uvoffset;
int mb_row, mb_col;
int ref_fb_idx = pc->lst_fb_idx;
int dst_fb_idx = pc->new_fb_idx;
int recon_y_stride = pc->yv12_fb[ref_fb_idx].y_stride;
int recon_uv_stride = pc->yv12_fb[ref_fb_idx].uv_stride;
int sb_col;
int row_delta[4] = { 0, +1, 0, -1};
int col_delta[4] = {+1, -1, +1, +1};
int sb_cols = (pc->mb_cols + 1)>>1;
ENTROPY_CONTEXT_PLANES left_context[2];
vpx_memset(left_context, 0, sizeof(left_context));
mb_row = mbrow;
mb_col = 0;
for (sb_col=0; sb_col<sb_cols; sb_col++)
{
for ( i=0; i<4; i++ )
{
int dy = row_delta[i];
int dx = col_delta[i];
int offset_extended = dy * xd->mode_info_stride + dx;
if ((mb_row >= pc->mb_rows) || (mb_col >= pc->mb_cols))
{
// Skip on to the next MB
mb_row += dy;
mb_col += dx;
xd->mode_info_context += offset_extended;
continue;
}
// Copy in the appropriate left context
vpx_memcpy (&pc->left_context,
&left_context[(i>>1) & 0x1],
sizeof(ENTROPY_CONTEXT_PLANES));
// reset above block coeffs
xd->above_context = pc->above_context + mb_col;
/* Distance of Mb to the various image edges.
* These are specified to 8th pel as they are always compared to
* values that are in 1/8th pel units
*/
xd->mb_to_top_edge = -((mb_row * 16)) << 3;
xd->mb_to_bottom_edge = ((pc->mb_rows - 1 - mb_row) * 16) << 3;
xd->mb_to_left_edge = -((mb_col * 16) << 3);
xd->mb_to_right_edge = ((pc->mb_cols - 1 - mb_col) * 16) << 3;
xd->up_available = (mb_row != 0);
xd->left_available = (mb_col != 0);
#if CONFIG_ERROR_CONCEALMENT
{
int corrupt_residual = (!pbi->independent_partitions &&
pbi->frame_corrupt_residual) ||
vp8dx_bool_error(xd->current_bc);
if (pbi->ec_active &&
xd->mode_info_context->mbmi.ref_frame == INTRA_FRAME &&
corrupt_residual)
{
/* We have an intra block with corrupt coefficients, better
* to conceal with an inter block. Interpolate MVs from
* neighboring MBs.
*
* Note that for the first mb with corrupt residual in a
* frame, we might not discover that before decoding the
* residual. That happens after this check, and therefore
* no inter concealment will be done.
*/
vp8_interpolate_motion(xd,
mb_row, mb_col,
pc->mb_rows, pc->mb_cols,
pc->mode_info_stride);
}
}
#endif
update_blockd_bmi(xd);
recon_yoffset = (mb_row * recon_y_stride * 16) + (mb_col * 16);
recon_uvoffset = (mb_row * recon_uv_stride * 8) + (mb_col * 8);
xd->dst.y_buffer = pc->yv12_fb[dst_fb_idx].y_buffer + recon_yoffset;
xd->dst.u_buffer = pc->yv12_fb[dst_fb_idx].u_buffer + recon_uvoffset;
xd->dst.v_buffer = pc->yv12_fb[dst_fb_idx].v_buffer + recon_uvoffset;
/* Select the appropriate reference frame for this MB */
if (xd->mode_info_context->mbmi.ref_frame == LAST_FRAME)
ref_fb_idx = pc->lst_fb_idx;
else if (xd->mode_info_context->mbmi.ref_frame == GOLDEN_FRAME)
ref_fb_idx = pc->gld_fb_idx;
else
ref_fb_idx = pc->alt_fb_idx;
xd->pre.y_buffer = pc->yv12_fb[ref_fb_idx].y_buffer +recon_yoffset;
xd->pre.u_buffer = pc->yv12_fb[ref_fb_idx].u_buffer +recon_uvoffset;
xd->pre.v_buffer = pc->yv12_fb[ref_fb_idx].v_buffer +recon_uvoffset;
#if CONFIG_DUALPRED
if (xd->mode_info_context->mbmi.second_ref_frame)
{
int second_ref_fb_idx;
/* Select the appropriate reference frame for this MB */
if (xd->mode_info_context->mbmi.second_ref_frame == LAST_FRAME)
second_ref_fb_idx = pc->lst_fb_idx;
else if (xd->mode_info_context->mbmi.second_ref_frame ==
GOLDEN_FRAME)
second_ref_fb_idx = pc->gld_fb_idx;
else
second_ref_fb_idx = pc->alt_fb_idx;
xd->second_pre.y_buffer =
pc->yv12_fb[second_ref_fb_idx].y_buffer + recon_yoffset;
xd->second_pre.u_buffer =
pc->yv12_fb[second_ref_fb_idx].u_buffer + recon_uvoffset;
xd->second_pre.v_buffer =
pc->yv12_fb[second_ref_fb_idx].v_buffer + recon_uvoffset;
}
#endif /* CONFIG_DUALPRED */
if (xd->mode_info_context->mbmi.ref_frame != INTRA_FRAME)
{
/* propagate errors from reference frames */
xd->corrupted |= pc->yv12_fb[ref_fb_idx].corrupted;
}
decode_macroblock(pbi, xd, mb_row * pc->mb_cols + mb_col);
/* check if the boolean decoder has suffered an error */
xd->corrupted |= vp8dx_bool_error(xd->current_bc);
// Copy in the appropriate left context
vpx_memcpy (&left_context[(i>>1) & 0x1],
&pc->left_context,
sizeof(ENTROPY_CONTEXT_PLANES));
// skip to next MB
xd->mode_info_context += offset_extended;
mb_row += dy;
mb_col += dx;
}
}
/* skip prediction column */
xd->mode_info_context += 1 - (pc->mb_cols & 0x1) + xd->mode_info_stride;
}
#else
static void
decode_mb_row(VP8D_COMP *pbi, VP8_COMMON *pc, int mb_row, MACROBLOCKD *xd)
{
......@@ -591,7 +748,7 @@ decode_mb_row(VP8D_COMP *pbi, VP8_COMMON *pc, int mb_row, MACROBLOCKD *xd)
++xd->mode_info_context; /* skip prediction column */
}
#endif // CONFIG_SUPERBLOCKS
static unsigned int read_partition_size(const unsigned char *cx_size)
{
......@@ -1376,7 +1533,23 @@ int vp8_decode_frame(VP8D_COMP *pbi)
int num_part = 1 << pc->multi_token_partition;
pbi->frame_corrupt_residual = 0;
/* Decode the individual macro block */
#if CONFIG_SUPERBLOCKS
/* Decode a row of super-blocks */
for (mb_row = 0; mb_row < pc->mb_rows; mb_row+=2)
{
if (num_part > 1)
{
xd->current_bc = & pbi->mbc[ibc];
ibc++;
if (ibc == num_part)
ibc = 0;
}
decode_sb_row(pbi, pc, mb_row, xd);
}
#else
/* Decode a row of macro blocks */
for (mb_row = 0; mb_row < pc->mb_rows; mb_row++)
{
......@@ -1391,6 +1564,7 @@ int vp8_decode_frame(VP8D_COMP *pbi)
decode_mb_row(pbi, pc, mb_row, xd);
}
#endif /* CONFIG_SUPERBLOCKS */
corrupt_tokens |= xd->corrupted;
}
......@@ -1441,3 +1615,4 @@ int vp8_decode_frame(VP8D_COMP *pbi)
return 0;
}
......@@ -960,6 +960,422 @@ static void encode_ref_frame( vp8_writer *const w,
}
}
#if CONFIG_SUPERBLOCKS
static void pack_inter_mode_mvs(VP8_COMP *const cpi)
{
VP8_COMMON *const pc = & cpi->common;
vp8_writer *const w = & cpi->bc;
const MV_CONTEXT *mvc = pc->fc.mvc;
MACROBLOCKD *xd = &cpi->mb.e_mbd;
#if CONFIG_DUALPRED
int i;
#endif
int pred_context;
const int *const rfct = cpi->count_mb_ref_frame_usage;
const int rf_intra = rfct[INTRA_FRAME];
const int rf_inter = rfct[LAST_FRAME] + rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME];
MODE_INFO *m = pc->mi;
#if CONFIG_NEWNEAR
MODE_INFO *prev_m = pc->prev_mi;
#endif
const int mis = pc->mode_info_stride;
int mb_row, mb_col;
int row, col;
int prob_skip_false = 0;
#if CONFIG_DUALPRED
int prob_dual_pred[3];
#endif /* CONFIG_DUALPRED */
// Values used in prediction model coding
vp8_prob pred_prob;
unsigned char prediction_flag;
int row_delta[4] = { 0, +1, 0, -1};
int col_delta[4] = {+1, -1, +1, +1};
cpi->mb.partition_info = cpi->mb.pi;
// Calculate the probabilities to be used to code the reference frame
// based on actual useage this frame
//#if CONFIG_SEGFEATURES
pc->prob_intra_coded = (rf_intra + rf_inter)
? rf_intra * 255 / (rf_intra + rf_inter) : 1;
if (!pc->prob_intra_coded)
pc->prob_intra_coded = 1;
pc->prob_last_coded = rf_inter ? (rfct[LAST_FRAME] * 255) / rf_inter : 128;
if (!pc->prob_last_coded)
pc->prob_last_coded = 1;
pc->prob_gf_coded = (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME])
? (rfct[GOLDEN_FRAME] * 255) / (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME]) : 128;
if (!pc->prob_gf_coded)
pc->prob_gf_coded = 1;
#if CONFIG_COMPRED
// Compute a modified set of probabilities to use when prediction of the
// reference frame fails
compute_mod_refprobs( pc );
#endif
#ifdef ENTROPY_STATS
active_section = 1;
#endif
if (pc->mb_no_coeff_skip)
{
// Divide by 0 check. 0 case possible with segment features
if ( (cpi->skip_false_count + cpi->skip_true_count) )
{
prob_skip_false = cpi->skip_false_count * 256 /
(cpi->skip_false_count + cpi->skip_true_count);
if (prob_skip_false <= 1)
prob_skip_false = 1;
if (prob_skip_false > 255)
prob_skip_false = 255;
}
else
prob_skip_false = 255;
cpi->prob_skip_false = prob_skip_false;
vp8_write_literal(w, prob_skip_false, 8);
}
vp8_write_literal(w, pc->prob_intra_coded, 8);
vp8_write_literal(w, pc->prob_last_coded, 8);
vp8_write_literal(w, pc->prob_gf_coded, 8);
#if CONFIG_DUALPRED
if (cpi->common.dual_pred_mode == HYBRID_PREDICTION)
{
vp8_write(w, 1, 128);
vp8_write(w, 1, 128);
for (i = 0; i < 3; i++) {
if (cpi->single_pred_count[i] + cpi->dual_pred_count[i])
{
prob_dual_pred[i] = cpi->single_pred_count[i] * 256 /
(cpi->single_pred_count[i] + cpi->dual_pred_count[i]);
if (prob_dual_pred[i] < 1)
prob_dual_pred[i] = 1;
else if (prob_dual_pred[i] > 255)
prob_dual_pred[i] = 255;
}
else
{
prob_dual_pred[i] = 128;
}
vp8_write_literal(w, prob_dual_pred[i], 8);
}
}
else if (cpi->common.dual_pred_mode == SINGLE_PREDICTION_ONLY)
{
vp8_write(w, 0, 128);
}
else /* dual prediction only */
{
vp8_write(w, 1, 128);
vp8_write(w, 0, 128);
}
#endif /* CONFIG_DUALPRED */
update_mbintra_mode_probs(cpi);
vp8_write_mvprobs(cpi);
mb_row = 0;
for (row=0; row < pc->mb_rows; row += 2)
{
m = pc->mi + row * mis;
mb_col = 0;
for (col=0; col < pc->mb_cols; col += 2)
{
int i;
for (i=0; i<4; i++)
{
const MB_MODE_INFO *const mi = & m->mbmi;
const MV_REFERENCE_FRAME rf = mi->ref_frame;
const MB_PREDICTION_MODE mode = mi->mode;
const int segment_id = mi->segment_id;
int dy = row_delta[i];
int dx = col_delta[i];
int offset_extended = dy * mis + dx;
if ((mb_row >= pc->mb_rows) || (mb_col >= pc->mb_cols))
{
mb_row += dy;
mb_col += dx;
m += offset_extended;
cpi->mb.partition_info += offset_extended;
continue;
}
// Distance of Mb to the various image edges.
// These specified to 8th pel as they are always compared to MV
// values that are in 1/8th pel units
xd->mb_to_left_edge = -((mb_col * 16) << 3);
xd->mb_to_right_edge = ((pc->mb_cols - 1 - mb_col) * 16) << 3;
xd->mb_to_top_edge = -((mb_row * 16)) << 3;
xd->mb_to_bottom_edge = ((pc->mb_rows - 1 - mb_row) * 16) << 3;
// Make sure the MacroBlockD mode info pointer is set correctly
xd->mode_info_context = m;
#if CONFIG_NEWNEAR
xd->prev_mode_info_context = prev_m;
#endif
#ifdef ENTROPY_STATS
active_section = 9;
#endif
if (cpi->mb.e_mbd.update_mb_segmentation_map)
{
// Is temporal coding of the segment map enabled
if (pc->temporal_update)
{
prediction_flag =
get_pred_flag( xd, PRED_SEG_ID );
pred_prob =
get_pred_prob( pc, xd, PRED_SEG_ID);
// Code the segment id prediction flag for this mb
vp8_write( w, prediction_flag, pred_prob );
// If the mbs segment id was not predicted code explicitly
if (!prediction_flag)
write_mb_segid(w, mi, &cpi->mb.e_mbd);
}
else
{
// Normal undpredicted coding
write_mb_segid(w, mi, &cpi->mb.e_mbd);
}
}
//#if CONFIG_SEGFEATURES
if ( pc->mb_no_coeff_skip &&
( !segfeature_active( xd, segment_id, SEG_LVL_EOB ) ||
( get_segdata( xd, segment_id, SEG_LVL_EOB ) != 0 ) ) )
{
vp8_encode_bool(w, mi->mb_skip_coeff, prob_skip_false);
}
// Encode the reference frame.
encode_ref_frame( w, pc, xd,
segment_id, rf );
if (rf == INTRA_FRAME)
{
#ifdef ENTROPY_STATS
active_section = 6;
#endif
//#if CONFIG_SEGFEATURES
if ( !segfeature_active( xd, segment_id, SEG_LVL_MODE ) )
write_ymode(w, mode, pc->fc.ymode_prob);
if (mode == B_PRED)
{
int j = 0;
do
write_bmode(w, m->bmi[j].as_mode, pc->fc.bmode_prob);
while (++j < 16);
}
if(mode == I8X8_PRED)
{
write_i8x8_mode(w, m->bmi[0].as_mode, pc->i8x8_mode_prob);
write_i8x8_mode(w, m->bmi[2].as_mode, pc->i8x8_mode_prob);
write_i8x8_mode(w, m->bmi[8].as_mode, pc->i8x8_mode_prob);
write_i8x8_mode(w, m->bmi[10].as_mode, pc->i8x8_mode_prob);
}
else
{
#if CONFIG_UVINTRA
write_uv_mode(w, mi->uv_mode, pc->fc.uv_mode_prob[mode]);
#ifdef MODE_STATS
if(mode!=B_PRED)
++cpi->y_uv_mode_count[mode][mi->uv_mode];
#endif
#else
write_uv_mode(w, mi->uv_mode, pc->fc.uv_mode_prob);
#endif /*CONFIG_UVINTRA*/
}
}