From b7480454706a6b15bf091e659cd6227ab373c1a6 Mon Sep 17 00:00:00 2001 From: Yunqing Wang Date: Thu, 30 Jun 2011 11:20:13 -0400 Subject: [PATCH] Bug fix in motion vector limit calculation Motion vector limits are calculated using right shifts, which could give wrong results for negative numbers. James Berry's test on one clip showed encoder produced some artifacts. This change fixed that. Change-Id: I035fc02280b10455b7f6eb388f7c2e33b796b018 --- vp8/encoder/mcomp.c | 4 ++-- vp8/encoder/mcomp.h | 2 +- vp8/encoder/pickinter.c | 20 ++++++++++++-------- vp8/encoder/rdopt.c | 32 ++++++++++++++++++++------------ 4 files changed, 35 insertions(+), 23 deletions(-) diff --git a/vp8/encoder/mcomp.c b/vp8/encoder/mcomp.c index 416948870..6f314a386 100644 --- a/vp8/encoder/mcomp.c +++ b/vp8/encoder/mcomp.c @@ -286,8 +286,8 @@ int vp8_find_best_sub_pixel_step_iteratively(MACROBLOCK *x, BLOCK *b, BLOCKD *d, bestmv->as_mv.row = br << 1; bestmv->as_mv.col = bc << 1; - if ((abs(bestmv->as_mv.col - ref_mv->as_mv.col) > MAX_FULL_PEL_VAL) || - (abs(bestmv->as_mv.row - ref_mv->as_mv.row) > MAX_FULL_PEL_VAL)) + if ((abs(bestmv->as_mv.col - ref_mv->as_mv.col) > (MAX_FULL_PEL_VAL<<3)) || + (abs(bestmv->as_mv.row - ref_mv->as_mv.row) > (MAX_FULL_PEL_VAL<<3))) return INT_MAX; return besterr; diff --git a/vp8/encoder/mcomp.h b/vp8/encoder/mcomp.h index 44ed055db..416c4d5eb 100644 --- a/vp8/encoder/mcomp.h +++ b/vp8/encoder/mcomp.h @@ -22,7 +22,7 @@ extern void accum_mv_refs(MB_PREDICTION_MODE, const int near_mv_ref_cts[4]); #define MAX_MVSEARCH_STEPS 8 // The maximum number of steps in a step search given the largest allowed initial step -#define MAX_FULL_PEL_VAL ((1 << (MAX_MVSEARCH_STEPS+3)) - 8) // Max full pel mv specified in 1/8 pel units +#define MAX_FULL_PEL_VAL ((1 << (MAX_MVSEARCH_STEPS)) - 1) // Max full pel mv specified in 1 pel units #define MAX_FIRST_STEP (1 << (MAX_MVSEARCH_STEPS-1)) // Maximum size of the first step in full pel units extern void print_mode_context(void); diff --git a/vp8/encoder/pickinter.c b/vp8/encoder/pickinter.c index b60d2419f..6c73511a9 100644 --- a/vp8/encoder/pickinter.c +++ b/vp8/encoder/pickinter.c @@ -588,10 +588,10 @@ void vp8_pick_inter_mode(VP8_COMP *cpi, MACROBLOCK *x, int recon_yoffset, /* adjust mvp to make sure it is within MV range */ vp8_clamp_mv(&mvp, - best_ref_mv.as_mv.col - MAX_FULL_PEL_VAL, - best_ref_mv.as_mv.col + MAX_FULL_PEL_VAL, - best_ref_mv.as_mv.row - MAX_FULL_PEL_VAL, - best_ref_mv.as_mv.row + MAX_FULL_PEL_VAL); + best_ref_mv.as_mv.col - (MAX_FULL_PEL_VAL<<3), + best_ref_mv.as_mv.col + (MAX_FULL_PEL_VAL<<3), + best_ref_mv.as_mv.row - (MAX_FULL_PEL_VAL<<3), + best_ref_mv.as_mv.row + (MAX_FULL_PEL_VAL<<3)); } switch (this_mode) @@ -681,10 +681,14 @@ void vp8_pick_inter_mode(VP8_COMP *cpi, MACROBLOCK *x, int recon_yoffset, mvp.as_int = best_ref_mv.as_int; } - col_min = (best_ref_mv.as_mv.col - MAX_FULL_PEL_VAL) >>3; - col_max = (best_ref_mv.as_mv.col + MAX_FULL_PEL_VAL) >>3; - row_min = (best_ref_mv.as_mv.row - MAX_FULL_PEL_VAL) >>3; - row_max = (best_ref_mv.as_mv.row + MAX_FULL_PEL_VAL) >>3; + col_min = (best_ref_mv.as_mv.col < 0)?(-((abs(best_ref_mv.as_mv.col))>>3) - MAX_FULL_PEL_VAL) + :((best_ref_mv.as_mv.col>>3) - MAX_FULL_PEL_VAL); + col_max = (best_ref_mv.as_mv.col < 0)?(-((abs(best_ref_mv.as_mv.col))>>3) + MAX_FULL_PEL_VAL) + :((best_ref_mv.as_mv.col>>3) + MAX_FULL_PEL_VAL); + row_min = (best_ref_mv.as_mv.row < 0)?(-((abs(best_ref_mv.as_mv.row))>>3) - MAX_FULL_PEL_VAL) + :((best_ref_mv.as_mv.row>>3) - MAX_FULL_PEL_VAL); + row_max = (best_ref_mv.as_mv.row < 0)?(-((abs(best_ref_mv.as_mv.row))>>3) + MAX_FULL_PEL_VAL) + :((best_ref_mv.as_mv.row>>3) + MAX_FULL_PEL_VAL); // Get intersection of UMV window and valid MV window to reduce # of checks in diamond search. if (x->mv_col_min < col_min ) diff --git a/vp8/encoder/rdopt.c b/vp8/encoder/rdopt.c index e0359057a..fa5bb079b 100644 --- a/vp8/encoder/rdopt.c +++ b/vp8/encoder/rdopt.c @@ -1330,10 +1330,14 @@ static int vp8_rd_pick_best_mbsegmentation(VP8_COMP *cpi, MACROBLOCK *x, if (bsi.segment_rd < best_rd) { - int col_min = (best_ref_mv->as_mv.col - MAX_FULL_PEL_VAL) >>3; - int col_max = (best_ref_mv->as_mv.col + MAX_FULL_PEL_VAL) >>3; - int row_min = (best_ref_mv->as_mv.row - MAX_FULL_PEL_VAL) >>3; - int row_max = (best_ref_mv->as_mv.row + MAX_FULL_PEL_VAL) >>3; + int col_min = (best_ref_mv->as_mv.col < 0)?(-((abs(best_ref_mv->as_mv.col))>>3) - MAX_FULL_PEL_VAL) + :((best_ref_mv->as_mv.col>>3) - MAX_FULL_PEL_VAL); + int col_max = (best_ref_mv->as_mv.col < 0)?(-((abs(best_ref_mv->as_mv.col))>>3) + MAX_FULL_PEL_VAL) + :((best_ref_mv->as_mv.col>>3) + MAX_FULL_PEL_VAL); + int row_min = (best_ref_mv->as_mv.row < 0)?(-((abs(best_ref_mv->as_mv.row))>>3) - MAX_FULL_PEL_VAL) + :((best_ref_mv->as_mv.row>>3) - MAX_FULL_PEL_VAL); + int row_max = (best_ref_mv->as_mv.row < 0)?(-((abs(best_ref_mv->as_mv.row))>>3) + MAX_FULL_PEL_VAL) + :((best_ref_mv->as_mv.row>>3) + MAX_FULL_PEL_VAL); int tmp_col_min = x->mv_col_min; int tmp_col_max = x->mv_col_max; @@ -1876,10 +1880,10 @@ void vp8_rd_pick_inter_mode(VP8_COMP *cpi, MACROBLOCK *x, int recon_yoffset, int /* adjust mvp to make sure it is within MV range */ vp8_clamp_mv(&mvp, - best_ref_mv.as_mv.col - MAX_FULL_PEL_VAL, - best_ref_mv.as_mv.col + MAX_FULL_PEL_VAL, - best_ref_mv.as_mv.row - MAX_FULL_PEL_VAL, - best_ref_mv.as_mv.row + MAX_FULL_PEL_VAL); + best_ref_mv.as_mv.col - (MAX_FULL_PEL_VAL<<3), + best_ref_mv.as_mv.col + (MAX_FULL_PEL_VAL<<3), + best_ref_mv.as_mv.row - (MAX_FULL_PEL_VAL<<3), + best_ref_mv.as_mv.row + (MAX_FULL_PEL_VAL<<3)); } // Check to see if the testing frequency for this mode is at its max @@ -2011,10 +2015,14 @@ void vp8_rd_pick_inter_mode(VP8_COMP *cpi, MACROBLOCK *x, int recon_yoffset, int int sadpb = x->sadperbit16; - int col_min = (best_ref_mv.as_mv.col - MAX_FULL_PEL_VAL) >>3; - int col_max = (best_ref_mv.as_mv.col + MAX_FULL_PEL_VAL) >>3; - int row_min = (best_ref_mv.as_mv.row - MAX_FULL_PEL_VAL) >>3; - int row_max = (best_ref_mv.as_mv.row + MAX_FULL_PEL_VAL) >>3; + int col_min = (best_ref_mv.as_mv.col < 0)?(-((abs(best_ref_mv.as_mv.col))>>3) - MAX_FULL_PEL_VAL) + :((best_ref_mv.as_mv.col>>3) - MAX_FULL_PEL_VAL); + int col_max = (best_ref_mv.as_mv.col < 0)?(-((abs(best_ref_mv.as_mv.col))>>3) + MAX_FULL_PEL_VAL) + :((best_ref_mv.as_mv.col>>3) + MAX_FULL_PEL_VAL); + int row_min = (best_ref_mv.as_mv.row < 0)?(-((abs(best_ref_mv.as_mv.row))>>3) - MAX_FULL_PEL_VAL) + :((best_ref_mv.as_mv.row>>3) - MAX_FULL_PEL_VAL); + int row_max = (best_ref_mv.as_mv.row < 0)?(-((abs(best_ref_mv.as_mv.row))>>3) + MAX_FULL_PEL_VAL) + :((best_ref_mv.as_mv.row>>3) + MAX_FULL_PEL_VAL); int tmp_col_min = x->mv_col_min; int tmp_col_max = x->mv_col_max;