📄 fast_me.c
字号:
}
//fourth_step: //Extended Hexagon-based Search
// the fourth step with a small search pattern
fourth_1_step: //sub step 1: small Hexagon search
for(i=0; i < search_range; i++) //change into 1/2
{
iXMinNow = best_x;
iYMinNow = best_y;
for (m = 0; m < 6; m++)
{
cand_x = iXMinNow + Hexagon_x[m];
cand_y = iYMinNow + Hexagon_y[m];
SEARCH_ONE_PIXEL
}
if(best_x == iXMinNow && best_y == iYMinNow)
break;
}
fourth_2_step: //sub step 2: small Diamond search
for(i = 0; i < search_range; i++) //change into 1/2
{
iXMinNow = best_x;
iYMinNow = best_y;
for (m = 0; m < 4; m++)
{
cand_x = iXMinNow + Diamond_x[m];
cand_y = iYMinNow + Diamond_y[m];
SEARCH_ONE_PIXEL
}
if(best_x == iXMinNow && best_y == iYMinNow)
break;
}
terminate_step:
// store SAD infomation for prediction
//FAST MOTION ESTIMATION. ZHIBO CHEN 2003.3
for (i=0; i < (blocksize_x>>2); i++)
{
for (j=0; j < (blocksize_y>>2); j++)
{
if(list == 0)
{
fastme_ref_cost[ref][blocktype][block_y+j][block_x+i] = min_mcost;
if (ref==0)
fastme_l0_cost[blocktype][(img->pix_y>>2)+block_y+j][(img->pix_x>>2)+block_x+i] = min_mcost;
}
else
{
fastme_l1_cost[blocktype][(img->pix_y>>2)+block_y+j][(img->pix_x>>2)+block_x+i] = min_mcost;
}
}
}
//for multi ref SAD prediction
if ((ref==0) || (SAD_prediction[pic_pix_x2] > min_mcost))
SAD_prediction[pic_pix_x2] = min_mcost;
*mv_x = best_x - pic_pix_x;
*mv_y = best_y - pic_pix_y;
return min_mcost;
}
/*!
************************************************************************
* \brief
* Functions for fast fractional pel motion estimation.
* 1. int AddUpSADQuarter() returns SADT of a fractiona pel MV
* 2. int FastSubPelBlockMotionSearch () proceed the fast fractional pel ME
* \authors
* Zhibo Chen
* Dept.of EE, Tsinghua Univ.
* \date
* 2003.4
************************************************************************
*/
int AddUpSADQuarter(int pic_pix_x,int pic_pix_y,int blocksize_x,int blocksize_y,
int cand_mv_x,int cand_mv_y, StorablePicture *ref_picture, pel_t** orig_pic,
int Mvmcost, int min_mcost,int useABT, int blocktype)
{
int j, i, k;
int diff[16], *d;
int mcost = Mvmcost;
int c_diff[MB_PIXELS];
int y_offset, ypels =(128 - 64 * (blocktype == 3));
int ry0, ry4, ry8, ry12;
int y0, y1, y2, y3;
int x0, x1, x2, x3;
int abort_search, rx0;
int img_width = ((ref_picture->size_x + 2*IMG_PAD_SIZE - 1)<<2);
int img_height = ((ref_picture->size_y + 2*IMG_PAD_SIZE - 1)<<2);
//===== Use weighted Reference for ME ====
pel_t **ref_pic;
pel_t *ref_line;
pel_t *orig_line;
int apply_weights = ( (active_pps->weighted_pred_flag && (img->type == P_SLICE || img->type == SP_SLICE)) ||
(active_pps->weighted_bipred_idc && (img->type == B_SLICE)));
if (apply_weights && input->UseWeightedReferenceME)
{
ref_pic = ref_picture->imgY_ups_w;
}
else
ref_pic = ref_picture->imgY_ups;
///////////////////////////////////////////
for (y0=0, abort_search=0; y0<blocksize_y && !abort_search; y0+=4)
{
y_offset = (y0>7)*ypels;
ry0 = (y0<<2) + cand_mv_y;
ry4 = ry0 + 4;
ry8 = ry4 + 4;
ry12 = ry8 + 4;
y1 = y0 + 1;
y2 = y1 + 1;
y3 = y2 + 1;
for (x0=0; x0<blocksize_x; x0+=4)
{
rx0 = (x0<<2) + cand_mv_x;
x1 = x0 + 1;
x2 = x1 + 1;
x3 = x2 + 1;
d = diff;
orig_line = orig_pic [y0];
ref_line = get_line (ref_pic, ry0, rx0, img_height, img_width);
*d++ = orig_line[x0] - *(ref_line );
*d++ = orig_line[x1] - *(ref_line += 4 );
*d++ = orig_line[x2] - *(ref_line += 4 );
*d++ = orig_line[x3] - *(ref_line += 4);
orig_line = orig_pic [y1];
ref_line = get_line (ref_pic, ry4, rx0, img_height, img_width);
*d++ = orig_line[x0] - *(ref_line );
*d++ = orig_line[x1] - *(ref_line += 4 );
*d++ = orig_line[x2] - *(ref_line += 4 );
*d++ = orig_line[x3] - *(ref_line += 4);
orig_line = orig_pic [y2];
ref_line = get_line (ref_pic, ry8, rx0, img_height, img_width);
*d++ = orig_line[x0] - *(ref_line );
*d++ = orig_line[x1] - *(ref_line += 4 );
*d++ = orig_line[x2] - *(ref_line += 4 );
*d++ = orig_line[x3] - *(ref_line += 4);
orig_line = orig_pic [y3];
ref_line = get_line (ref_pic, ry12, rx0, img_height, img_width);
*d++ = orig_line[x0] - *(ref_line );
*d++ = orig_line[x1] - *(ref_line += 4);
*d++ = orig_line[x2] - *(ref_line += 4);
*d = orig_line[x3] - *(ref_line += 4);
if (!useABT)
{
if ((mcost += SATD (diff, input->hadamard)) > min_mcost)
{
abort_search = 1;
break;
}
}
else // copy diff to curr_diff for ABT SATD calculation
{
i = (x0&0x7) + (x0>7) * 64 + y_offset;
for(k=0, j=y0; j<BLOCK_SIZE + y0; j++, k+=BLOCK_SIZE)
memcpy(&(c_diff[i + ((j&0x7)<<3)]), &diff[k], BLOCK_SIZE*sizeof(int));
}
}
}
if(useABT)
{
mcost += find_SATD (c_diff, blocktype);
}
return mcost;
}
#ifdef EIGHTH_PEL
int AddUpSADQuarter8(int pic_pix_x,int pic_pix_y,int blocksize_x,int blocksize_y,
int cand_mv_x,int cand_mv_y, StorablePicture *ref_picture, pel_t** orig_pic,
int Mvmcost, int min_mcost,int useABT, int blocktype)
{
int j, i, k;
int diff[16], *d;
int mcost = Mvmcost;
int c_diff[MB_PIXELS];
int y_offset, ypels =(128 - 64 * (blocktype == 3));
int ry0, ry4, ry8, ry12;
int y0, y1, y2, y3;
int abort_search, rx0, x0;
int img_width = ((ref_picture->size_x + 2*IMG_PAD_SIZE - 1)<<2);
int img_height = ((ref_picture->size_y + 2*IMG_PAD_SIZE - 1)<<2);
//===== Use weighted Reference for ME ====
pel_t **ref_pic;
pel_t *ref_line;
pel_t *orig_line;
int apply_weights = ( (active_pps->weighted_pred_flag && (img->type == P_SLICE || img->type == SP_SLICE)) ||
(active_pps->weighted_bipred_idc && (img->type == B_SLICE)));
if (apply_weights && input->UseWeightedReferenceME)
{
ref_pic = ref_picture->imgY_ups_w;
}
else
ref_pic = ref_picture->imgY_ups;
///////////////////////////////////////////
for (y0=0, abort_search=0; y0<blocksize_y && !abort_search; y0+=4)
{
y_offset = (y0>7)*ypels;
ry0 = (y0<<3) + cand_mv_y;
ry4 = ry0 + 8;
ry8 = ry4 + 8;
ry12 = ry8 + 8;
y1 = y0 + 1;
y2 = y1 + 1;
y3 = y2 + 1;
for (x0=0; x0<blocksize_x; x0+=4)
{
rx0 = (x0<<3) + cand_mv_x;
d = diff;
orig_line = &orig_pic [y0][x0];
ref_line = get_line (ref_pic, ry0, rx0, img_height, img_width);
*d++ = *orig_line++ - *(ref_line );
*d++ = *orig_line++ - *(ref_line += 4 );
*d++ = *orig_line++ - *(ref_line += 4 );
*d++ = *orig_line++ - *(ref_line += 4);
orig_line = &orig_pic [y1][x0];
ref_line = get_line (ref_pic, ry4, rx0, img_height, img_width);
*d++ = *orig_line++ - *(ref_line );
*d++ = *orig_line++ - *(ref_line += 4 );
*d++ = *orig_line++ - *(ref_line += 4 );
*d++ = *orig_line++ - *(ref_line += 4);
orig_line = &orig_pic [y2][x0];
ref_line = get_line (ref_pic, ry8, rx0, img_height, img_width);
*d++ = *orig_line++ - *(ref_line );
*d++ = *orig_line++ - *(ref_line += 4 );
*d++ = *orig_line++ - *(ref_line += 4 );
*d++ = *orig_line++ - *(ref_line += 4);
orig_line = &orig_pic [y3][x0];
ref_line = get_line (ref_pic, ry12, rx0, img_height, img_width);
*d++ = *orig_line++ - *(ref_line );
*d++ = *orig_line++ - *(ref_line += 4);
*d++ = *orig_line++ - *(ref_line += 4);
*d = *orig_line++ - *(ref_line += 4);
if (!useABT)
{
if ((mcost += SATD (diff, input->hadamard)) > min_mcost)
{
abort_search = 1;
break;
}
}
else // copy diff to curr_diff for ABT SATD calculation
{
i = (x0&0x7) + (x0>7) * 64 + y_offset;
for(k=0, j=y0; j<BLOCK_SIZE + y0; j++, k+=BLOCK_SIZE)
memcpy(&(c_diff[i + ((j&0x7)<<3)]), &diff[k], BLOCK_SIZE*sizeof(int));
}
}
}
if(useABT)
{
mcost += find_SATD (c_diff, blocktype);
}
return mcost;
}
#endif
int // ==> minimum motion cost after search
FastSubPelBlockMotionSearch (pel_t** orig_pic, // <-- original pixel values for the AxB block
short ref, // <-- reference frame (0... or -1 (backward))
int list,
int pic_pix_x, // <-- absolute x-coordinate of regarded AxB block
int pic_pix_y, // <-- absolute y-coordinate of regarded AxB block
int blocktype, // <-- block type (1-16x16 ... 7-4x4)
short pred_mv_x, // <-- motion vector predictor (x) in sub-pel units
short pred_mv_y, // <-- motion vector predictor (y) in sub-pel units
short* mv_x, // <--> in: search center (x) / out: motion vector (x) - in pel units
short* mv_y, // <--> in: search center (y) / out: motion vector (y) - in pel units
int search_pos2, // <-- search positions for half-pel search (default: 9)
int search_pos4, // <-- search positions for quarter-pel search (default: 9)
#ifdef EIGHTH_PEL
int search_pos8, // <-- search positions for eighth-pel search (default: 9)
#endif
int min_mcost, // <-- minimum motion cost (cost for center or huge value)
int lambda_factor,
int useABT) // <-- lagrangian parameter for determining motion cost
{
static int Diamond_x[4] = {-1, 0, 1, 0};
static int Diamond_y[4] = {0, 1, 0, -1};
int mcost;
int cand_mv_x, cand_mv_y;
int list_offset = ((img->MbaffFrameFlag)&&(img->mb_data[img->current_mb_nr].mb_field))? img->current_mb_nr%2 ? 4 : 2 : 0;
StorablePicture *ref_picture = listX[list+list_offset][ref];
#ifdef EIGHTH_PEL
int mv_shift = img->mv_res ? 1 : 0;
#else
int mv_shift = 0;
#endif
int blocksize_x = input->blc_size[blocktype][0];
int blocksize_y = input->blc_size[blocktype][1];
int pic4_pix_x = ((pic_pix_x + IMG_PAD_SIZE)<< 2);
int pic4_pix_y = ((pic_pix_y + IMG_PAD_SIZE)<< 2);
short max_pos_x4 = ((ref_picture->size_x - blocksize_x + 2*IMG_PAD_SIZE)<<2);
short max_pos_y4 = ((ref_picture->size_y - blocksize_y + 2*IMG_PAD_SIZE)<<2);
int search_range_dynamic,iXMinNow,iYMinNow,i;
int m,currmv_x = 0,currmv_y = 0;
int pred_frac_mv_x,pred_frac_mv_y,abort_search;
int mv_cost;
int pred_frac_up_mv_x, pred_frac_up_mv_y;
if ((pic4_pix_x + *mv_x > 1) && (pic4_pix_x + *mv_x < max_pos_x4 - 1) &&
(pic4_pix_y + *mv_y > 1) && (pic4_pix_y + *mv_y < max_pos_y4 - 1) )
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -