Commit 2fa5d5a2 authored by Scott LaVarnway's avatar Scott LaVarnway
Browse files

vp8_rd_pick_best_mbsegmentation code restructure

Moved the code from the segmentation loop into a function
which is now called for each segment. This will allow us
to change the segment order checking more easily.

Change-Id: I9510d26f0acae5a73043fcca8f1984b121d3e052
parent d283d9bb
......@@ -1023,46 +1023,46 @@ unsigned char vp8_mbsplit_offset2[4][16] = {
{ 0, 2, 8, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
};
static int vp8_rd_pick_best_mbsegmentation(VP8_COMP *cpi, MACROBLOCK *x, MV *best_ref_mv, int best_rd, int *mdcounts, int *returntotrate, int *returnyrate, int *returndistortion, int compressor_speed, int *mvcost[2], int mvthresh, int fullpixel)
static const unsigned int segmentation_to_sseshift[4] = {3, 3, 2, 0};
typedef struct
{
int i, segmentation;
B_PREDICTION_MODE this_mode;
MACROBLOCKD *xc = &x->e_mbd;
BLOCK *c;
BLOCKD *e;
int const *labels;
int best_segment_rd = INT_MAX;
int best_seg = 0;
int br = 0;
int bd = 0;
int bsr = 0;
int bsd = 0;
int bestsegmentyrate = 0;
MV *ref_mv;
static const int segmentation_to_sseshift[4] = {3, 3, 2, 0};
int segment_rd;
int segment_num;
int r;
int d;
int segment_yrate;
B_PREDICTION_MODE modes[16];
MV mvs[16];
unsigned char eobs[16];
// FIX TO Rd error outrange bug PGW 9 june 2004
B_PREDICTION_MODE bmodes[16] = {ZERO4X4, ZERO4X4, ZERO4X4, ZERO4X4,
ZERO4X4, ZERO4X4, ZERO4X4, ZERO4X4,
ZERO4X4, ZERO4X4, ZERO4X4, ZERO4X4,
ZERO4X4, ZERO4X4, ZERO4X4, ZERO4X4
};
int mvthresh;
int *mdcounts;
MV bmvs[16];
int beobs[16];
} BEST_SEG_INFO;
vpx_memset(beobs, 0, sizeof(beobs));
void vp8_rd_check_segment(VP8_COMP *cpi, MACROBLOCK *x, BEST_SEG_INFO *bsi,
unsigned int segmentation)
{
int i;
int const *labels;
int br = 0;
int bd = 0;
B_PREDICTION_MODE this_mode;
for (segmentation = 0; segmentation < VP8_NUMMBSPLITS; segmentation++)
{
int label_count;
int this_segment_rd = 0;
int label_mv_thresh;
int rate = 0;
int sbr = 0;
int sbd = 0;
int sseshift;
int segmentyrate = 0;
vp8_variance_fn_ptr_t *v_fn_ptr;
......@@ -1086,7 +1086,6 @@ static int vp8_rd_pick_best_mbsegmentation(VP8_COMP *cpi, MACROBLOCK *x, MV *bes
bd = 0;
v_fn_ptr = &cpi->fn_ptr[segmentation];
sseshift = segmentation_to_sseshift[segmentation];
labels = vp8_mbsplits[segmentation];
label_count = vp8_mbsplit_count[segmentation];
......@@ -1094,13 +1093,11 @@ static int vp8_rd_pick_best_mbsegmentation(VP8_COMP *cpi, MACROBLOCK *x, MV *bes
// making it so that we very rarely check mvs on
// segments. setting this to 1 would make mv thresh
// roughly equal to what it is for macroblocks
label_mv_thresh = 1 * mvthresh / label_count ;
label_mv_thresh = 1 * bsi->mvthresh / label_count ;
// Segmentation method overheads
rate = vp8_cost_token(vp8_mbsplit_tree, vp8_mbsplit_probs, vp8_mbsplit_encodings + segmentation);
rate += vp8_cost_mv_ref(SPLITMV, mdcounts);
rate += vp8_cost_mv_ref(SPLITMV, bsi->mdcounts);
this_segment_rd += RDFUNC(x->rdmult, x->rddiv, rate, 0, cpi->target_bits_per_mb);
br += rate;
......@@ -1109,22 +1106,13 @@ static int vp8_rd_pick_best_mbsegmentation(VP8_COMP *cpi, MACROBLOCK *x, MV *bes
MV mode_mv[B_MODE_COUNT];
int best_label_rd = INT_MAX;
B_PREDICTION_MODE mode_selected = ZERO4X4;
int j;
int bestlabelyrate = 0;
// find first label
j = vp8_mbsplit_offset2[segmentation][i];
c = &x->block[j];
e = &x->e_mbd.block[j];
// search for the best motion vector on this segment
for (this_mode = LEFT4X4; this_mode <= NEW4X4 ; this_mode ++)
{
int distortion;
int this_rd;
int num00;
int distortion;
int labelyrate;
ENTROPY_CONTEXT_PLANES t_above_s, t_left_s;
ENTROPY_CONTEXT *ta_s;
......@@ -1138,12 +1126,16 @@ static int vp8_rd_pick_best_mbsegmentation(VP8_COMP *cpi, MACROBLOCK *x, MV *bes
if (this_mode == NEW4X4)
{
int sseshift;
int num00;
int step_param = 0;
int further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param;
int n;
int thissme;
int bestsme = INT_MAX;
MV temp_mv;
BLOCK *c;
BLOCKD *e;
// Is the best so far sufficiently good that we cant justify doing and new motion search.
if (best_label_rd < label_mv_thresh)
......@@ -1152,11 +1144,21 @@ static int vp8_rd_pick_best_mbsegmentation(VP8_COMP *cpi, MACROBLOCK *x, MV *bes
{
int sadpb = x->sadperbit4;
// find first label
n = vp8_mbsplit_offset2[segmentation][i];
c = &x->block[n];
e = &x->e_mbd.block[n];
if (cpi->sf.search_method == HEX)
bestsme = vp8_hex_search(x, c, e, best_ref_mv, &mode_mv[NEW4X4], step_param, sadpb/*x->errorperbit*/, &num00, v_fn_ptr, x->mvsadcost, mvcost);
bestsme = vp8_hex_search(x, c, e, bsi->ref_mv,
&mode_mv[NEW4X4], step_param, sadpb, &num00, v_fn_ptr, x->mvsadcost, x->mvcost);
else
{
bestsme = cpi->diamond_search_sad(x, c, e, best_ref_mv, &mode_mv[NEW4X4], step_param, sadpb / 2/*x->errorperbit*/, &num00, v_fn_ptr, x->mvsadcost, mvcost, best_ref_mv);
bestsme = cpi->diamond_search_sad(x, c, e, bsi->ref_mv,
&mode_mv[NEW4X4], step_param,
sadpb / 2, &num00, v_fn_ptr, x->mvsadcost, x->mvcost, bsi->ref_mv);
n = num00;
num00 = 0;
......@@ -1169,7 +1171,9 @@ static int vp8_rd_pick_best_mbsegmentation(VP8_COMP *cpi, MACROBLOCK *x, MV *bes
num00--;
else
{
thissme = cpi->diamond_search_sad(x, c, e, best_ref_mv, &temp_mv, step_param + n, sadpb / 2/*x->errorperbit*/, &num00, v_fn_ptr, x->mvsadcost, mvcost, best_ref_mv);
thissme = cpi->diamond_search_sad(x, c, e, bsi->ref_mv,
&temp_mv, step_param + n,
sadpb / 2, &num00, v_fn_ptr, x->mvsadcost, x->mvcost, bsi->ref_mv);
if (thissme < bestsme)
{
......@@ -1181,10 +1185,13 @@ static int vp8_rd_pick_best_mbsegmentation(VP8_COMP *cpi, MACROBLOCK *x, MV *bes
}
}
sseshift = segmentation_to_sseshift[segmentation];
// Should we do a full search (best quality only)
if ((compressor_speed == 0) && (bestsme >> sseshift) > 4000)
if ((cpi->compressor_speed == 0) && (bestsme >> sseshift) > 4000)
{
thissme = cpi->full_search_sad(x, c, e, best_ref_mv, sadpb / 4, 16, v_fn_ptr, x->mvcost, x->mvsadcost, best_ref_mv);
thissme = cpi->full_search_sad(x, c, e, bsi->ref_mv,
sadpb / 4, 16, v_fn_ptr, x->mvcost, x->mvsadcost,bsi->ref_mv);
if (thissme < bestsme)
{
......@@ -1201,14 +1208,17 @@ static int vp8_rd_pick_best_mbsegmentation(VP8_COMP *cpi, MACROBLOCK *x, MV *bes
if (bestsme < INT_MAX)
{
if (!fullpixel)
cpi->find_fractional_mv_step(x, c, e, &mode_mv[NEW4X4], best_ref_mv, x->errorperbit / 2, v_fn_ptr, mvcost);
if (!cpi->common.full_pixel)
cpi->find_fractional_mv_step(x, c, e, &mode_mv[NEW4X4],
bsi->ref_mv, x->errorperbit / 2, v_fn_ptr, x->mvcost);
else
vp8_skip_fractional_mv_step(x, c, e, &mode_mv[NEW4X4], best_ref_mv, x->errorperbit, v_fn_ptr, mvcost);
}
vp8_skip_fractional_mv_step(x, c, e, &mode_mv[NEW4X4],
bsi->ref_mv, x->errorperbit, v_fn_ptr, x->mvcost);
}
} /* NEW4X4 */
rate = labels2mode(x, labels, i, this_mode, &mode_mv[this_mode], best_ref_mv, mvcost);
rate = labels2mode(x, labels, i, this_mode, &mode_mv[this_mode],
bsi->ref_mv, x->mvcost);
// Trap vectors that reach beyond the UMV borders
if (((mode_mv[this_mode].row >> 3) < x->mv_row_min) || ((mode_mv[this_mode].row >> 3) > x->mv_row_max) ||
......@@ -1236,72 +1246,101 @@ static int vp8_rd_pick_best_mbsegmentation(VP8_COMP *cpi, MACROBLOCK *x, MV *bes
vpx_memcpy(tl_b, tl_s, sizeof(ENTROPY_CONTEXT_PLANES));
}
}
} /*for each 4x4 mode*/
vpx_memcpy(ta, ta_b, sizeof(ENTROPY_CONTEXT_PLANES));
vpx_memcpy(tl, tl_b, sizeof(ENTROPY_CONTEXT_PLANES));
labels2mode(x, labels, i, mode_selected, &mode_mv[mode_selected], best_ref_mv, mvcost);
labels2mode(x, labels, i, mode_selected, &mode_mv[mode_selected],
bsi->ref_mv, x->mvcost);
br += sbr;
bd += sbd;
segmentyrate += bestlabelyrate;
this_segment_rd += best_label_rd;
if ((this_segment_rd > best_rd) || (this_segment_rd > best_segment_rd))
if (this_segment_rd > bsi->segment_rd)
break;
}
} /* for each label */
if ((this_segment_rd <= best_rd) && (this_segment_rd < best_segment_rd))
if (this_segment_rd < bsi->segment_rd)
{
bsr = br;
bsd = bd;
bestsegmentyrate = segmentyrate;
best_segment_rd = this_segment_rd;
best_seg = segmentation;
bsi->r = br;
bsi->d = bd;
bsi->segment_yrate = segmentyrate;
bsi->segment_rd = this_segment_rd;
bsi->segment_num = segmentation;
// store everything needed to come back to this!!
for (i = 0; i < 16; i++)
{
BLOCKD *bd = &x->e_mbd.block[i];
bmvs[i] = bd->bmi.mv.as_mv;
bmodes[i] = bd->bmi.mode;
beobs[i] = bd->eob;
bsi->mvs[i] = bd->bmi.mv.as_mv;
bsi->modes[i] = bd->bmi.mode;
bsi->eobs[i] = bd->eob;
}
}
}
static int vp8_rd_pick_best_mbsegmentation(VP8_COMP *cpi, MACROBLOCK *x,
MV *best_ref_mv, int best_rd,
int *mdcounts, int *returntotrate,
int *returnyrate, int *returndistortion,
int mvthresh)
{
int i;
BEST_SEG_INFO bsi;
BEST_SEG_INFO bsi_8x8;
int check_8x16 = 0;
int check_16x8 = 0;
vpx_memset(&bsi, 0, sizeof(bsi));
bsi.segment_rd = best_rd;
bsi.ref_mv = best_ref_mv;
bsi.mvthresh = mvthresh;
bsi.mdcounts = mdcounts;
for(i = 0; i < 16; i++)
{
bsi.modes[i] = ZERO4X4;
}
// set it to the best
/* original */
vp8_rd_check_segment(cpi, x, &bsi, 0);
vp8_rd_check_segment(cpi, x, &bsi, 1);
vp8_rd_check_segment(cpi, x, &bsi, 2);
vp8_rd_check_segment(cpi, x, &bsi, 3);
/* set it to the best */
for (i = 0; i < 16; i++)
{
BLOCKD *bd = &x->e_mbd.block[i];
bd->bmi.mv.as_mv = bmvs[i];
bd->bmi.mode = bmodes[i];
bd->eob = beobs[i];
bd->bmi.mv.as_mv = bsi.mvs[i];
bd->bmi.mode = bsi.modes[i];
bd->eob = bsi.eobs[i];
}
*returntotrate = bsr;
*returndistortion = bsd;
*returnyrate = bestsegmentyrate;
*returntotrate = bsi.r;
*returndistortion = bsi.d;
*returnyrate = bsi.segment_yrate;
// save partitions
labels = vp8_mbsplits[best_seg];
x->e_mbd.mode_info_context->mbmi.partitioning = best_seg;
x->partition_info->count = vp8_mbsplit_count[best_seg];
/* save partitions */
x->e_mbd.mode_info_context->mbmi.partitioning = bsi.segment_num;
x->partition_info->count = vp8_mbsplit_count[bsi.segment_num];
for (i = 0; i < x->partition_info->count; i++)
{
int j;
j = vp8_mbsplit_offset2[best_seg][i];
j = vp8_mbsplit_offset2[bsi.segment_num][i];
x->partition_info->bmi[i].mode = x->e_mbd.block[j].bmi.mode;
x->partition_info->bmi[i].mv.as_mv = x->e_mbd.block[j].bmi.mv.as_mv;
}
return best_segment_rd;
return bsi.segment_rd;
}
......@@ -1853,14 +1892,6 @@ int vp8_rd_pick_inter_mode(VP8_COMP *cpi, MACROBLOCK *x, int recon_yoffset, int
frame_cost = ref_frame_cost[x->e_mbd.mode_info_context->mbmi.ref_frame];
rate2 += frame_cost;
if (this_mode <= B_PRED)
{
for (i = 0; i < 16; i++)
{
vpx_memset(&x->e_mbd.block[i].bmi, 0, sizeof(B_MODE_INFO));
}
}
// Check to see if the testing frequency for this mode is at its max
// If so then prevent it from being tested and increase the threshold for its testing
if (cpi->mode_test_hit_counts[mode_index] && (cpi->mode_check_freq[mode_index] > 1))
......@@ -1909,6 +1940,10 @@ int vp8_rd_pick_inter_mode(VP8_COMP *cpi, MACROBLOCK *x, int recon_yoffset, int
{
case B_PRED:
for (i = 0; i < 16; i++)
{
vpx_memset(&x->e_mbd.block[i].bmi, 0, sizeof(B_MODE_INFO));
}
// Note the rate value returned here includes the cost of coding the BPRED mode : x->mbmode_cost[x->e_mbd.frame_type][BPRED];
vp8_rd_pick_intra4x4mby_modes(cpi, x, &rate, &rate_y, &distortion);
rate2 += rate;
......@@ -1928,13 +1963,14 @@ int vp8_rd_pick_inter_mode(VP8_COMP *cpi, MACROBLOCK *x, int recon_yoffset, int
// (best_rd - frame_cost_rd) is thus a conservative breakout number.
int breakout_rd = best_rd - frame_cost_rd;
int tmp_rd;
int this_rd_thresh;
if (x->e_mbd.mode_info_context->mbmi.ref_frame == LAST_FRAME)
tmp_rd = vp8_rd_pick_best_mbsegmentation(cpi, x, &best_ref_mv, breakout_rd, mdcounts, &rate, &rate_y, &distortion, cpi->compressor_speed, x->mvcost, cpi->rd_threshes[THR_NEWMV], cpi->common.full_pixel) ;
else if (x->e_mbd.mode_info_context->mbmi.ref_frame == GOLDEN_FRAME)
tmp_rd = vp8_rd_pick_best_mbsegmentation(cpi, x, &best_ref_mv, breakout_rd, mdcounts, &rate, &rate_y, &distortion, cpi->compressor_speed, x->mvcost, cpi->rd_threshes[THR_NEWG], cpi->common.full_pixel) ;
else
tmp_rd = vp8_rd_pick_best_mbsegmentation(cpi, x, &best_ref_mv, breakout_rd, mdcounts, &rate, &rate_y, &distortion, cpi->compressor_speed, x->mvcost, cpi->rd_threshes[THR_NEWA], cpi->common.full_pixel) ;
this_rd_thresh = (x->e_mbd.mode_info_context->mbmi.ref_frame == LAST_FRAME) ? cpi->rd_threshes[THR_NEWMV] : cpi->rd_threshes[THR_NEWA];
this_rd_thresh = (x->e_mbd.mode_info_context->mbmi.ref_frame == GOLDEN_FRAME) ? cpi->rd_threshes[THR_NEWG]: this_rd_thresh;
tmp_rd = vp8_rd_pick_best_mbsegmentation(cpi, x, &best_ref_mv,
breakout_rd, mdcounts,
&rate, &rate_y, &distortion, this_rd_thresh) ;
rate2 += rate;
distortion2 += distortion;
......@@ -2007,6 +2043,10 @@ int vp8_rd_pick_inter_mode(VP8_COMP *cpi, MACROBLOCK *x, int recon_yoffset, int
case V_PRED:
case H_PRED:
case TM_PRED:
for (i = 0; i < 16; i++)
{
vpx_memset(&x->e_mbd.block[i].bmi, 0, sizeof(B_MODE_INFO));
}
x->e_mbd.mode_info_context->mbmi.ref_frame = INTRA_FRAME;
vp8_build_intra_predictors_mby_ptr(&x->e_mbd);
{
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment