Commit 4e81a68a authored by Paul Wilkins's avatar Paul Wilkins
Browse files

Further activity masking changes:

Some further re-structuring of activity masking code.
Still has various experimental switches.
Supports a metric based on intra encode.
Experimental comparison against a fixed activity target  rather
than a frame average, for altering rd and zbin.

Overall the SSIM performance is similar  to TT's original
code but there is a much smaller PSNR hit of circa
0.5% instead of 3.2%

Change-Id: I0fd53b2dfb60620b3f74d7415e0b81c1ac58c39a
parent 7368dd4f
......@@ -50,6 +50,7 @@ void vp8_build_block_offsets(MACROBLOCK *x);
void vp8_setup_block_ptrs(MACROBLOCK *x);
int vp8cx_encode_inter_macroblock(VP8_COMP *cpi, MACROBLOCK *x, TOKENEXTRA **t, int recon_yoffset, int recon_uvoffset);
int vp8cx_encode_intra_macro_block(VP8_COMP *cpi, MACROBLOCK *x, TOKENEXTRA **t);
static void adjust_act_zbin( VP8_COMP *cpi, MACROBLOCK *x );
#ifdef MODE_STATS
unsigned int inter_y_modes[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
......@@ -103,70 +104,122 @@ static unsigned int tt_activity_measure( VP8_COMP *cpi, MACROBLOCK *x )
}
// Stub for alternative experimental activity measures.
static unsigned int alt_activity_measure( VP8_COMP *cpi, MACROBLOCK *x )
extern int encode_intra(VP8_COMP *cpi, MACROBLOCK *x, int use_dc_pred);
static unsigned int alt_activity_measure( VP8_COMP *cpi,
MACROBLOCK *x, int use_dc_pred )
{
unsigned int mb_activity = VP8_ACTIVITY_AVG_MIN;
x->e_mbd.mode_info_context->mbmi.mode = DC_PRED;
x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;
x->e_mbd.mode_info_context->mbmi.ref_frame = INTRA_FRAME;
vp8_encode_intra16x16mby(IF_RTCD(&cpi->rtcd), x);
mb_activity = VARIANCE_INVOKE(&cpi->rtcd.variance, getmbss)(x->src_diff);
return mb_activity;
return encode_intra(cpi,x, use_dc_pred);
}
// Measure the activity of the current macroblock
// What we measure here is TBD so abstracted to this function
static unsigned int mb_activity_measure( VP8_COMP *cpi, MACROBLOCK *x )
#define ALT_ACT_MEASURE 1
static unsigned int mb_activity_measure( VP8_COMP *cpi, MACROBLOCK *x,
int mb_row, int mb_col)
{
unsigned int mb_activity;
if ( 1 )
if ( ALT_ACT_MEASURE )
{
// Original activity measure from Tim T's code.
mb_activity = tt_activity_measure( cpi, x );
int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row);
// Or use and alternative.
mb_activity = alt_activity_measure( cpi, x, use_dc_pred );
}
else
{
// Or use and alternative.
mb_activity = alt_activity_measure( cpi, x );
// Original activity measure from Tim T's code.
mb_activity = tt_activity_measure( cpi, x );
}
if ( mb_activity < VP8_ACTIVITY_AVG_MIN )
mb_activity = VP8_ACTIVITY_AVG_MIN;
return mb_activity;
}
// Calculate an "average" mb activity value for the frame
#define ACT_MEDIAN 0
static void calc_av_activity( VP8_COMP *cpi, INT64 activity_sum )
{
#if ACT_MEDIAN
// Find median: Simple n^2 algorithm for experimentation
{
unsigned int median;
unsigned int i,j;
unsigned int * sortlist;
unsigned int tmp;
// Create a list to sort to
CHECK_MEM_ERROR(sortlist,
vpx_calloc(sizeof(unsigned int),
cpi->common.MBs));
// Copy map to sort list
vpx_memcpy( sortlist, cpi->mb_activity_map,
sizeof(unsigned int) * cpi->common.MBs );
// Ripple each value down to its correct position
for ( i = 1; i < cpi->common.MBs; i ++ )
{
for ( j = i; j > 0; j -- )
{
if ( sortlist[j] < sortlist[j-1] )
{
// Swap values
tmp = sortlist[j-1];
sortlist[j-1] = sortlist[j];
sortlist[j] = tmp;
}
else
break;
}
}
// Even number MBs so estimate median as mean of two either side.
median = ( 1 + sortlist[cpi->common.MBs >> 1] +
sortlist[(cpi->common.MBs >> 1) + 1] ) >> 1;
cpi->activity_avg = median;
vpx_free(sortlist);
}
#else
// Simple mean for now
cpi->activity_avg = (unsigned int)(activity_sum/cpi->common.MBs);
#endif
if (cpi->activity_avg < VP8_ACTIVITY_AVG_MIN)
cpi->activity_avg = VP8_ACTIVITY_AVG_MIN;
// Experimental code: return fixed value normalized for several clips
if ( ALT_ACT_MEASURE )
cpi->activity_avg = 100000;
}
#define USE_ACT_INDEX 0
#define OUTPUT_NORM_ACT_STATS 0
// Calculate a normalized activity value for each mb
static void calc_norm_activity( VP8_COMP *cpi, MACROBLOCK *x )
#if USE_ACT_INDEX
// Calculate and activity index for each mb
static void calc_activity_index( VP8_COMP *cpi, MACROBLOCK *x )
{
VP8_COMMON *const cm = & cpi->common;
int mb_row, mb_col;
unsigned int act;
unsigned int a;
unsigned int b;
INT64 act;
INT64 a;
INT64 b;
#if OUTPUT_NORM_ACT_STATS
FILE *f = fopen("norm_act.stt", "a");
fprintf(f, "\n");
fprintf(f, "\n%12d\n", cpi->activity_avg );
#endif
// Reset pointers to start of activity map
x->mb_activity_ptr = cpi->mb_activity_map;
x->mb_norm_activity_ptr = cpi->mb_norm_activity_map;
// Calculate normalized mb activity number.
for (mb_row = 0; mb_row < cm->mb_rows; mb_row++)
......@@ -178,25 +231,19 @@ static void calc_norm_activity( VP8_COMP *cpi, MACROBLOCK *x )
act = *(x->mb_activity_ptr);
// Calculate a normalized activity number
a = act + 2*cpi->activity_avg;
b = 2*act + cpi->activity_avg;
a = act + 4*cpi->activity_avg;
b = 4*act + cpi->activity_avg;
if ( b >= a )
*(x->mb_norm_activity_ptr) = (int)((b + (a>>1))/a);
*(x->activity_ptr) = (int)((b + (a>>1))/a) - 1;
else
*(x->mb_norm_activity_ptr) = -(int)((a + (b>>1))/b);
if ( *(x->mb_norm_activity_ptr) == 0 )
{
*(x->mb_norm_activity_ptr) = 1;
}
*(x->activity_ptr) = 1 - (int)((a + (b>>1))/b);
#if OUTPUT_NORM_ACT_STATS
fprintf(f, " %6d", *(x->mb_norm_activity_ptr));
fprintf(f, " %6d", *(x->mb_activity_ptr));
#endif
// Increment activity map pointers
x->mb_activity_ptr++;
x->mb_norm_activity_ptr++;
}
#if OUTPUT_NORM_ACT_STATS
......@@ -210,33 +257,44 @@ static void calc_norm_activity( VP8_COMP *cpi, MACROBLOCK *x )
#endif
}
#endif
// Loop through all MBs. Note activity of each, average activity and
// calculate a normalized activity for each
static void build_activity_map( VP8_COMP *cpi )
{
MACROBLOCK *const x = & cpi->mb;
MACROBLOCKD *xd = &x->e_mbd;
VP8_COMMON *const cm = & cpi->common;
#if ALT_ACT_MEASURE
YV12_BUFFER_CONFIG *new_yv12 = &cm->yv12_fb[cm->new_fb_idx];
int recon_yoffset;
int recon_y_stride = new_yv12->y_stride;
#endif
int mb_row, mb_col;
unsigned int mb_activity;
INT64 activity_sum = 0;
// Initialise source buffer pointer
x->src = *cpi->Source;
// Set pointer to start of activity map
x->mb_activity_ptr = cpi->mb_activity_map;
// for each macroblock row in image
for (mb_row = 0; mb_row < cm->mb_rows; mb_row++)
{
#if ALT_ACT_MEASURE
// reset above block coeffs
xd->up_available = (mb_row != 0);
recon_yoffset = (mb_row * recon_y_stride * 16);
#endif
// for each macroblock col in image
for (mb_col = 0; mb_col < cm->mb_cols; mb_col++)
{
#if ALT_ACT_MEASURE
xd->dst.y_buffer = new_yv12->y_buffer + recon_yoffset;
xd->left_available = (mb_col != 0);
recon_yoffset += 16;
#endif
// measure activity
mb_activity = mb_activity_measure( cpi, x );
mb_activity = mb_activity_measure( cpi, x, mb_row, mb_col );
// Keep frame sum
activity_sum += mb_activity;
......@@ -251,49 +309,47 @@ static void build_activity_map( VP8_COMP *cpi )
x->src.y_buffer += 16;
}
// adjust to the next row of mbs
x->src.y_buffer += 16 * x->src.y_stride - 16 * cm->mb_cols;
#if ALT_ACT_MEASURE
//extend the recon for intra prediction
vp8_extend_mb_row(new_yv12, xd->dst.y_buffer + 16,
xd->dst.u_buffer + 8, xd->dst.v_buffer + 8);
#endif
}
// Calculate an "average" MB activity
calc_av_activity(cpi, activity_sum);
// Calculate a normalized activity number of each mb
calc_norm_activity( cpi, x );
#if USE_ACT_INDEX
// Calculate an activity index number of each mb
calc_activity_index( cpi, x );
#endif
}
// Activity masking based on Tim T's original code
// Macroblock activity masking
void vp8_activity_masking(VP8_COMP *cpi, MACROBLOCK *x)
{
unsigned int a;
unsigned int b;
unsigned int act = *(x->mb_activity_ptr);
#if USE_ACT_INDEX
x->rdmult += *(x->mb_activity_ptr) * (x->rdmult >> 2);
#else
INT64 a;
INT64 b;
INT64 act = *(x->mb_activity_ptr);
// Apply the masking to the RD multiplier.
a = act + 2*cpi->activity_avg;
b = 2*act + cpi->activity_avg;
a = act + (2*cpi->activity_avg);
b = (2*act) + cpi->activity_avg;
//tmp = (unsigned int)(((INT64)tmp*b + (a>>1))/a);
x->rdmult = (unsigned int)(((INT64)x->rdmult*b + (a>>1))/a);
#endif
// For now now zbin adjustment on mode choice
x->act_zbin_adj = 0;
}
// Stub function to use a normalized activity measure stored at mb level.
void vp8_norm_activity_masking(VP8_COMP *cpi, MACROBLOCK *x)
{
int norm_act;
norm_act = *(x->mb_norm_activity_ptr);
if (norm_act > 0)
x->rdmult = norm_act * (x->rdmult);
else
x->rdmult = -(x->rdmult / norm_act);
// For now now zbin adjustment on mode choice
x->act_zbin_adj = 0;
// Activity based Zbin adjustment
adjust_act_zbin(cpi, x);
}
static
......@@ -349,7 +405,6 @@ void encode_mb_row(VP8_COMP *cpi,
// Set the mb activity pointer to the start of the row.
x->mb_activity_ptr = &cpi->mb_activity_map[map_index];
x->mb_norm_activity_ptr = &cpi->mb_norm_activity_map[map_index];
// for each macroblock col in image
for (mb_col = 0; mb_col < cm->mb_cols; mb_col++)
......@@ -469,7 +524,6 @@ void encode_mb_row(VP8_COMP *cpi,
// Increment the activity mask pointers.
x->mb_activity_ptr++;
x->mb_norm_activity_ptr++;
/* save the block info */
for (i = 0; i < 16; i++)
......@@ -518,6 +572,65 @@ void encode_mb_row(VP8_COMP *cpi,
#endif
}
void init_encode_frame_mb_context(VP8_COMP *cpi)
{
MACROBLOCK *const x = & cpi->mb;
VP8_COMMON *const cm = & cpi->common;
MACROBLOCKD *const xd = & x->e_mbd;
// GF active flags data structure
x->gf_active_ptr = (signed char *)cpi->gf_active_flags;
// Activity map pointer
x->mb_activity_ptr = cpi->mb_activity_map;
x->vector_range = 32;
x->act_zbin_adj = 0;
x->partition_info = x->pi;
xd->mode_info_context = cm->mi;
xd->mode_info_stride = cm->mode_info_stride;
xd->frame_type = cm->frame_type;
xd->frames_since_golden = cm->frames_since_golden;
xd->frames_till_alt_ref_frame = cm->frames_till_alt_ref_frame;
// reset intra mode contexts
if (cm->frame_type == KEY_FRAME)
vp8_init_mbmode_probs(cm);
// Copy data over into macro block data sturctures.
x->src = * cpi->Source;
xd->pre = cm->yv12_fb[cm->lst_fb_idx];
xd->dst = cm->yv12_fb[cm->new_fb_idx];
// set up frame for intra coded blocks
vp8_setup_intra_recon(&cm->yv12_fb[cm->new_fb_idx]);
vp8_build_block_offsets(x);
vp8_setup_block_dptrs(&x->e_mbd);
vp8_setup_block_ptrs(x);
xd->mode_info_context->mbmi.mode = DC_PRED;
xd->mode_info_context->mbmi.uv_mode = DC_PRED;
xd->left_context = &cm->left_context;
vp8_zero(cpi->count_mb_ref_frame_usage)
vp8_zero(cpi->ymode_count)
vp8_zero(cpi->uv_mode_count)
x->mvc = cm->fc.mvc;
vpx_memset(cm->above_context, 0,
sizeof(ENTROPY_CONTEXT_PLANES) * cm->mb_cols);
}
void vp8_encode_frame(VP8_COMP *cpi)
{
int mb_row;
......@@ -529,6 +642,17 @@ void vp8_encode_frame(VP8_COMP *cpi)
int segment_counts[MAX_MB_SEGMENTS];
int totalrate;
vpx_memset(segment_counts, 0, sizeof(segment_counts));
totalrate = 0;
if (cpi->compressor_speed == 2)
{
if (cpi->oxcf.cpu_used < 0)
cpi->Speed = -(cpi->oxcf.cpu_used);
else
vp8_auto_select_speed(cpi);
}
// Functions setup for all frame types so we can use MC in AltRef
if (cm->mcomp_filter_type == SIXTAP)
{
......@@ -553,10 +677,6 @@ void vp8_encode_frame(VP8_COMP *cpi)
&cpi->common.rtcd.subpix, bilinear16x16);
}
x->gf_active_ptr = (signed char *)cpi->gf_active_flags; // Point to base of GF active flags data structure
x->vector_range = 32;
// Reset frame count of inter 0,0 motion vector useage.
cpi->inter_zz_count = 0;
......@@ -567,89 +687,34 @@ void vp8_encode_frame(VP8_COMP *cpi)
cpi->skip_true_count = 0;
cpi->skip_false_count = 0;
x->act_zbin_adj = 0;
#if 0
// Experimental code
cpi->frame_distortion = 0;
cpi->last_mb_distortion = 0;
#endif
totalrate = 0;
x->partition_info = x->pi;
xd->mode_info_context = cm->mi;
xd->mode_info_stride = cm->mode_info_stride;
xd->frame_type = cm->frame_type;
xd->frames_since_golden = cm->frames_since_golden;
xd->frames_till_alt_ref_frame = cm->frames_till_alt_ref_frame;
vp8_zero(cpi->MVcount);
// vp8_zero( Contexts)
vp8_zero(cpi->coef_counts);
// reset intra mode contexts
if (cm->frame_type == KEY_FRAME)
vp8_init_mbmode_probs(cm);
vp8cx_frame_init_quantizer(cpi);
if (cpi->compressor_speed == 2)
{
if (cpi->oxcf.cpu_used < 0)
cpi->Speed = -(cpi->oxcf.cpu_used);
else
vp8_auto_select_speed(cpi);
}
vp8_initialize_rd_consts(cpi,
vp8_dc_quant(cm->base_qindex, cm->y1dc_delta_q));
vp8_initialize_rd_consts(cpi, vp8_dc_quant(cm->base_qindex, cm->y1dc_delta_q));
vp8cx_initialize_me_consts(cpi, cm->base_qindex);
// Copy data over into macro block data sturctures.
x->src = * cpi->Source;
xd->pre = cm->yv12_fb[cm->lst_fb_idx];
xd->dst = cm->yv12_fb[cm->new_fb_idx];
// set up frame new frame for intra coded blocks
vp8_setup_intra_recon(&cm->yv12_fb[cm->new_fb_idx]);
vp8_build_block_offsets(x);
vp8_setup_block_dptrs(&x->e_mbd);
vp8_setup_block_ptrs(x);
xd->mode_info_context->mbmi.mode = DC_PRED;
xd->mode_info_context->mbmi.uv_mode = DC_PRED;
xd->left_context = &cm->left_context;
vp8_zero(cpi->count_mb_ref_frame_usage)
vp8_zero(cpi->ymode_count)
vp8_zero(cpi->uv_mode_count)
x->mvc = cm->fc.mvc;
vpx_memset(cm->above_context, 0, sizeof(ENTROPY_CONTEXT_PLANES) * cm->mb_cols);
if(cpi->oxcf.tuning == VP8_TUNE_SSIM)
{
if(1)
{
// Build a frame level activity map
build_activity_map(cpi);
}
// Initialize encode frame context.
init_encode_frame_mb_context(cpi);
// Reset various MB pointers.
x->src = *cpi->Source;
x->mb_activity_ptr = cpi->mb_activity_map;
x->mb_norm_activity_ptr = cpi->mb_norm_activity_map;
// Build a frame level activity map
build_activity_map(cpi);
}
// re-initencode frame context.
init_encode_frame_mb_context(cpi);
{
struct vpx_usec_timer emr_timer;
vpx_usec_timer_start(&emr_timer);
......@@ -990,27 +1055,24 @@ static void sum_intra_stats(VP8_COMP *cpi, MACROBLOCK *x)
// Experimental stub function to create a per MB zbin adjustment based on
// some previously calculated measure of MB activity.
void adjust_act_zbin( VP8_COMP *cpi, int rate, MACROBLOCK *x )
static void adjust_act_zbin( VP8_COMP *cpi, MACROBLOCK *x )
{
INT64 act;
#if USE_ACT_INDEX
x->act_zbin_adj = *(x->mb_activity_ptr);
#else
INT64 a;
INT64 b;
INT64 act = *(x->mb_activity_ptr);
// Read activity from the map
act = (INT64)(*(x->mb_activity_ptr));
// Calculate a zbin adjustment for this mb
// Apply the masking to the RD multiplier.
a = act + 4*cpi->activity_avg;
b = 4*act + cpi->activity_avg;
if ( b > a )
//x->act_zbin_adj = (char)((b * 8) / a) - 8;
x->act_zbin_adj = 8;
else
x->act_zbin_adj = 0;
// Tmp force to 0 to disable.
x->act_zbin_adj = 0;
if ( act > cpi->activity_avg )
x->act_zbin_adj = (int)(((INT64)b + (a>>1))/a) - 1;
else
x->act_zbin_adj = 1 - (int)(((INT64)a + (b>>1))/b);
#endif
}
int vp8cx_encode_intra_macro_block(VP8_COMP *cpi, MACROBLOCK *x, TOKENEXTRA **t)
......@@ -1038,7 +1100,7 @@ int vp8cx_encode_intra_macro_block(VP8_COMP *cpi, MACROBLOCK *x, TOKENEXTRA **t)
if(cpi->oxcf.tuning == VP8_TUNE_SSIM)
{
adjust_act_zbin( cpi, rate, x );
adjust_act_zbin( cpi, x );
vp8_update_zbin_extra(cpi, x);
}
}
......@@ -1156,7 +1218,7 @@ int vp8cx_encode_inter_macroblock
if(cpi->oxcf.tuning == VP8_TUNE_SSIM)
{
// Adjust the zbin based on this MB rate.
adjust_act_zbin( cpi, rate, x );
adjust_act_zbin( cpi, x );
}
#if 0
......@@ -1186,11 +1248,10 @@ int vp8cx_encode_inter_macroblock
{
// Experimental code. Special case for gf and arf zeromv modes.
// Increase zbin size to supress noise
cpi->zbin_mode_boost = 0;
if (cpi->zbin_mode_boost_enabled)
{
if ( xd->mode_info_context->mbmi.ref_frame == INTRA_FRAME )
cpi->zbin_mode_boost = 0;
else
if ( xd->mode_info_context->mbmi.ref_frame != INTRA_FRAME )
{
if (xd->mode_info_context->mbmi.mode == ZEROMV)
{
......@@ -1205,9 +1266,6 @@ int vp8cx_encode_inter_macroblock
cpi->zbin_mode_boost = MV_ZBIN_BOOST;
}
}
else
cpi->zbin_mode_boost = 0;
vp8_update_zbin_extra(cpi, x);
}
......
......@@ -114,8 +114,6 @@ THREAD_FUNCTION thread_encoding_proc(void *p_data)
// Set the mb activity pointer to the start of the row.
x->mb_activity_ptr = &cpi->mb_activity_map[map_index];
x->mb_norm_activity_ptr =
&cpi->mb_norm_activity_map[map_index];
// for each macroblock col in image
for (mb_col = 0; mb_col < cm->mb_cols; mb_col++)
......@@ -230,7 +228,6 @@ THREAD_FUNCTION thread_encoding_proc(void *p_data)
// Increment the activity mask pointers.
x->mb_activity_ptr++;
x->mb_norm_activity_ptr++;
/* save the block info */
for (i = 0; i < 16; i++)
......
......@@ -81,7 +81,7 @@ static const int cq_level[QINDEX_RANGE] =
static void find_next_key_frame(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame);