📄 fast_me.c
字号:
iXMinNow = best_x;
iYMinNow = best_y;
}
*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 abort_search, y0, x0, rx0, ry0, ry;
pel_t *orig_line;
int diff[16], *d;
int mcost = Mvmcost;
int yy,kk,xx;
int curr_diff[MB_BLOCK_SIZE][MB_BLOCK_SIZE]; // for ABT SATD calculation
//2004.3.3
pel_t **ref_pic = ref_picture->imgY_ups;
int img_width = ref_picture->size_x;
int img_height = ref_picture->size_y;
for (y0=0, abort_search=0; y0<blocksize_y && !abort_search; y0+=4)
{
ry0 = ((pic_pix_y+y0)<<2) + cand_mv_y;
for (x0=0; x0<blocksize_x; x0+=4)
{
rx0 = ((pic_pix_x+x0)<<2) + cand_mv_x;
d = diff;
orig_line = orig_pic [y0 ]; ry=ry0;
*d++ = orig_line[x0 ] - PelY_14 (ref_pic, ry, rx0 , img_height, img_width);
*d++ = orig_line[x0+1] - PelY_14 (ref_pic, ry, rx0+ 4, img_height, img_width);
*d++ = orig_line[x0+2] - PelY_14 (ref_pic, ry, rx0+ 8, img_height, img_width);
*d++ = orig_line[x0+3] - PelY_14 (ref_pic, ry, rx0+ 12, img_height, img_width);
orig_line = orig_pic [y0+1]; ry=ry0+4;
*d++ = orig_line[x0 ] - PelY_14 (ref_pic, ry, rx0 , img_height, img_width);
*d++ = orig_line[x0+1] - PelY_14 (ref_pic, ry, rx0+ 4, img_height, img_width);
*d++ = orig_line[x0+2] - PelY_14 (ref_pic, ry, rx0+ 8, img_height, img_width);
*d++ = orig_line[x0+3] - PelY_14 (ref_pic, ry, rx0+ 12, img_height, img_width);
orig_line = orig_pic [y0+2]; ry=ry0+8;
*d++ = orig_line[x0 ] - PelY_14 (ref_pic, ry, rx0 , img_height, img_width);
*d++ = orig_line[x0+1] - PelY_14 (ref_pic, ry, rx0+ 4, img_height, img_width);
*d++ = orig_line[x0+2] - PelY_14 (ref_pic, ry, rx0+ 8, img_height, img_width);
*d++ = orig_line[x0+3] - PelY_14 (ref_pic, ry, rx0+ 12, img_height, img_width);
orig_line = orig_pic [y0+3]; ry=ry0+12;
*d++ = orig_line[x0 ] - PelY_14 (ref_pic, ry, rx0 , img_height, img_width);
*d++ = orig_line[x0+1] - PelY_14 (ref_pic, ry, rx0+ 4, img_height, img_width);
*d++ = orig_line[x0+2] - PelY_14 (ref_pic, ry, rx0+ 8, img_height, img_width);
*d = orig_line[x0+3] - PelY_14 (ref_pic, ry, rx0+ 12, img_height, img_width);
if (!useABT)
{
if ((mcost += SATD (diff, input->hadamard)) > min_mcost)
{
abort_search = 1;
break;
}
}
else // copy diff to curr_diff for ABT SATD calculation
{
for (yy=y0,kk=0; yy<y0+4; yy++)
for (xx=x0; xx<x0+4; xx++, kk++)
curr_diff[yy][xx] = diff[kk];
}
}
}
return mcost;
}
int // ==> minimum motion cost after search
FastSubPelBlockMotionSearch (pel_t** orig_pic, // <-- original pixel values for the AxB block
int 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)
int pred_mv_x, // <-- motion vector predictor (x) in sub-pel units
int pred_mv_y, // <-- motion vector predictor (y) in sub-pel units
int* mv_x, // <--> in: search center (x) / out: motion vector (x) - in pel units
int* 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)
int min_mcost, // <-- minimum motion cost (cost for center or huge value)
double lambda,
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];
int lambda_factor = LAMBDA_FACTOR (lambda);
int mv_shift = 0;
int blocksize_x = input->blc_size[blocktype][0];
int blocksize_y = input->blc_size[blocktype][1];
int pic4_pix_x = (pic_pix_x << 2);
int pic4_pix_y = (pic_pix_y << 2);
int max_pos_x4 = ((ref_picture->size_x/*img->width*/-blocksize_x+1)<<2);
int max_pos_y4 = ((ref_picture->size_y/*img->height*/-blocksize_y+1)<<2);
int search_range_dynamic,iXMinNow,iYMinNow,i;
int iSADLayer,m,currmv_x = 0,currmv_y = 0,iCurrSearchRange;
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;
*mv_x <<= 2;
*mv_y <<= 2;
if ((pic4_pix_x + *mv_x > 1) && (pic4_pix_x + *mv_x < max_pos_x4 - 2) &&
(pic4_pix_y + *mv_y > 1) && (pic4_pix_y + *mv_y < max_pos_y4 - 2) )
{
PelY_14 = FastPelY_14;
}
else
{
PelY_14 = UMVPelY_14;
}
search_range_dynamic = 3;
pred_frac_mv_x = (pred_mv_x - *mv_x)%4;
pred_frac_mv_y = (pred_mv_y - *mv_y)%4;
pred_frac_up_mv_x = (pred_MV_uplayer[0] - *mv_x)%4;
pred_frac_up_mv_y = (pred_MV_uplayer[1] - *mv_y)%4;
memset(SearchState[0],0,(2*search_range_dynamic+1)*(2*search_range_dynamic+1));
if(input->hadamard)
{
cand_mv_x = *mv_x;
cand_mv_y = *mv_y;
mv_cost = MV_COST (lambda_factor, mv_shift, cand_mv_x, cand_mv_y, pred_mv_x, pred_mv_y);
mcost = AddUpSADQuarter(pic_pix_x,pic_pix_y,blocksize_x,blocksize_y,cand_mv_x,cand_mv_y,ref_picture/*ref_pic*//*Wenfang Fu 2004.3.12*/,orig_pic,mv_cost,min_mcost,useABT);
SearchState[search_range_dynamic][search_range_dynamic] = 1;
if (mcost < min_mcost)
{
min_mcost = mcost;
currmv_x = cand_mv_x;
currmv_y = cand_mv_y;
}
}
else
{
SearchState[search_range_dynamic][search_range_dynamic] = 1;
currmv_x = *mv_x;
currmv_y = *mv_y;
}
if(pred_frac_mv_x!=0 || pred_frac_mv_y!=0)
{
cand_mv_x = *mv_x + pred_frac_mv_x;
cand_mv_y = *mv_y + pred_frac_mv_y;
mv_cost = MV_COST (lambda_factor, mv_shift, cand_mv_x, cand_mv_y, pred_mv_x, pred_mv_y);
mcost = AddUpSADQuarter(pic_pix_x,pic_pix_y,blocksize_x,blocksize_y,cand_mv_x,cand_mv_y,ref_picture/*ref_pic*//*Wenfang Fu 2004.3.12*/,orig_pic,mv_cost,min_mcost,useABT);
SearchState[cand_mv_y -*mv_y + search_range_dynamic][cand_mv_x - *mv_x + search_range_dynamic] = 1;
if (mcost < min_mcost)
{
min_mcost = mcost;
currmv_x = cand_mv_x;
currmv_y = cand_mv_y;
}
}
iXMinNow = currmv_x;
iYMinNow = currmv_y;
iCurrSearchRange = 2*search_range_dynamic+1;
for(i=0;i<iCurrSearchRange;i++)
{
abort_search=1;
iSADLayer = 65536;
for (m = 0; m < 4; m++)
{
cand_mv_x = iXMinNow + Diamond_x[m];
cand_mv_y = iYMinNow + Diamond_y[m];
if(abs(cand_mv_x - *mv_x) <=search_range_dynamic && abs(cand_mv_y - *mv_y)<= search_range_dynamic)
{
if(!SearchState[cand_mv_y -*mv_y+ search_range_dynamic][cand_mv_x -*mv_x+ search_range_dynamic])
{
mv_cost = MV_COST (lambda_factor, mv_shift, cand_mv_x, cand_mv_y, pred_mv_x, pred_mv_y);
mcost = AddUpSADQuarter(pic_pix_x,pic_pix_y,blocksize_x,blocksize_y,cand_mv_x,cand_mv_y,ref_picture/*ref_pic*//*Wenfang Fu 2004.3.12*/,orig_pic,mv_cost,min_mcost,useABT);
SearchState[cand_mv_y - *mv_y + search_range_dynamic][cand_mv_x - *mv_x + search_range_dynamic] = 1;
if (mcost < min_mcost)
{
min_mcost = mcost;
currmv_x = cand_mv_x;
currmv_y = cand_mv_y;
abort_search = 0;
}
}
}
}
iXMinNow = currmv_x;
iYMinNow = currmv_y;
if(abort_search)
break;
}
*mv_x = currmv_x;
*mv_y = currmv_y;
//===== return minimum motion cost =====
return min_mcost;
}
/*!
************************************************************************
* \brief
* Functions for SAD prediction of intra block cases.
* 1. void decide_intrabk_SAD() judges the block coding type(intra/inter)
* of neibouring blocks
* 2. void skip_intrabk_SAD() set the SAD to zero if neigouring block coding
* type is intra
* \date
* 2003.4
************************************************************************
*/
void decide_intrabk_SAD()
{
if (img->type != 0)
{
if (img->pix_x == 0 && img->pix_y == 0)
{
flag_intra_SAD = 0;
}
else if (img->pix_x == 0)
{
flag_intra_SAD = flag_intra[(img->pix_x)>>4];
}
else if (img->pix_y == 0)
{
flag_intra_SAD = flag_intra[((img->pix_x)>>4)-1];
}
else
{
flag_intra_SAD = ((flag_intra[(img->pix_x)>>4])||(flag_intra[((img->pix_x)>>4)-1])||(flag_intra[((img->pix_x)>>4)+1])) ;
}
}
return;
}
void skip_intrabk_SAD(int best_mode, int ref_max)
{
int i,j,k, ref;
if (img->number > 0)
flag_intra[(img->pix_x)>>4] = (best_mode == 9 || best_mode == 10) ? 1:0;
if (img->type!=0 && (best_mode == 9 || best_mode == 10))
{
for (i=0; i < 4; i++)
{
for (j=0; j < 4; j++)
{
for (k=1; k < 8;k++)
{
for (ref=0; ref<ref_max;ref++)
{
all_mincost[(img->pix_x>>2)+i][(img->pix_y>>2)+j][ref][k][0] = 0;
}
}
}
}
}
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -