📄 mv-search.c
字号:
d[ 7] = m[11] + m[15];
d[15] = m[15] - m[11];
m[ 0] = d[ 0] + d[ 3];
m[ 1] = d[ 1] + d[ 2];
m[ 2] = d[ 1] - d[ 2];
m[ 3] = d[ 0] - d[ 3];
m[ 4] = d[ 4] + d[ 7];
m[ 5] = d[ 5] + d[ 6];
m[ 6] = d[ 5] - d[ 6];
m[ 7] = d[ 4] - d[ 7];
m[ 8] = d[ 8] + d[11];
m[ 9] = d[ 9] + d[10];
m[10] = d[ 9] - d[10];
m[11] = d[ 8] - d[11];
m[12] = d[12] + d[15];
m[13] = d[13] + d[14];
m[14] = d[13] - d[14];
m[15] = d[12] - d[15];
d[ 0] = m[ 0] + m[ 1];
d[ 1] = m[ 0] - m[ 1];
d[ 2] = m[ 2] + m[ 3];
d[ 3] = m[ 3] - m[ 2];
d[ 4] = m[ 4] + m[ 5];
d[ 5] = m[ 4] - m[ 5];
d[ 6] = m[ 6] + m[ 7];
d[ 7] = m[ 7] - m[ 6];
d[ 8] = m[ 8] + m[ 9];
d[ 9] = m[ 8] - m[ 9];
d[10] = m[10] + m[11];
d[11] = m[11] - m[10];
d[12] = m[12] + m[13];
d[13] = m[12] - m[13];
d[14] = m[14] + m[15];
d[15] = m[15] - m[14];
/*===== sum up =====*/
for (dd=d[k=0]; k<16; dd=d[++k])
{
satd += (dd < 0 ? -dd : dd);
}
satd = ((satd+1)>>1);
}
else
{
/*===== sum up =====*/
for (k = 0; k < 16; k++)
{
satd += byte_abs [diff [k]];
}
}
return satd;
}
/*!
***********************************************************************
* \brief
* Calculate SA(T)D for 8x8
***********************************************************************
*/
int
SATD8X8 (int* diff, int use_hadamard)
{
int i, j, sad=0;
int m1[8][8], m2[8][8], m3[8][8];
if(use_hadamard)
{
//horizontal
for (j=0; j < 8; j++)
{
m2[j][0] = diff[(j<<3) ] + diff[(j<<3)+4];
m2[j][1] = diff[(j<<3)+1] + diff[(j<<3)+5];
m2[j][2] = diff[(j<<3)+2] + diff[(j<<3)+6];
m2[j][3] = diff[(j<<3)+3] + diff[(j<<3)+7];
m2[j][4] = diff[(j<<3) ] - diff[(j<<3)+4];
m2[j][5] = diff[(j<<3)+1] - diff[(j<<3)+5];
m2[j][6] = diff[(j<<3)+2] - diff[(j<<3)+6];
m2[j][7] = diff[(j<<3)+3] - diff[(j<<3)+7];
m1[j][0] = m2[j][0] + m2[j][2];
m1[j][1] = m2[j][1] + m2[j][3];
m1[j][2] = m2[j][0] - m2[j][2];
m1[j][3] = m2[j][1] - m2[j][3];
m1[j][4] = m2[j][4] + m2[j][6];
m1[j][5] = m2[j][5] + m2[j][7];
m1[j][6] = m2[j][4] - m2[j][6];
m1[j][7] = m2[j][5] - m2[j][7];
m2[j][0] = m1[j][0] + m1[j][1];
m2[j][1] = m1[j][0] - m1[j][1];
m2[j][2] = m1[j][2] + m1[j][3];
m2[j][3] = m1[j][2] - m1[j][3];
m2[j][4] = m1[j][4] + m1[j][5];
m2[j][5] = m1[j][4] - m1[j][5];
m2[j][6] = m1[j][6] + m1[j][7];
m2[j][7] = m1[j][6] - m1[j][7];
}
//vertical
for (i=0; i < 8; i++)
{
m3[0][i] = m2[0][i] + m2[4][i];
m3[1][i] = m2[1][i] + m2[5][i];
m3[2][i] = m2[2][i] + m2[6][i];
m3[3][i] = m2[3][i] + m2[7][i];
m3[4][i] = m2[0][i] - m2[4][i];
m3[5][i] = m2[1][i] - m2[5][i];
m3[6][i] = m2[2][i] - m2[6][i];
m3[7][i] = m2[3][i] - m2[7][i];
m1[0][i] = m3[0][i] + m3[2][i];
m1[1][i] = m3[1][i] + m3[3][i];
m1[2][i] = m3[0][i] - m3[2][i];
m1[3][i] = m3[1][i] - m3[3][i];
m1[4][i] = m3[4][i] + m3[6][i];
m1[5][i] = m3[5][i] + m3[7][i];
m1[6][i] = m3[4][i] - m3[6][i];
m1[7][i] = m3[5][i] - m3[7][i];
m2[0][i] = m1[0][i] + m1[1][i];
m2[1][i] = m1[0][i] - m1[1][i];
m2[2][i] = m1[2][i] + m1[3][i];
m2[3][i] = m1[2][i] - m1[3][i];
m2[4][i] = m1[4][i] + m1[5][i];
m2[5][i] = m1[4][i] - m1[5][i];
m2[6][i] = m1[6][i] + m1[7][i];
m2[7][i] = m1[6][i] - m1[7][i];
}
for (j=0; j < 8; j++)
for (i=0; i < 8; i++)
sad += (absm(m2[j][i]));
sad=((sad+2)>>2);
}
else
{
for(i=0; i<64; i++)
sad += byte_abs [diff [i]];
}
return sad;
}
/*!
***********************************************************************
* \brief
* Calculate SA(T)D for 8x8
***********************************************************************
*/
int
find_SATD (int curr_diff[MB_BLOCK_SIZE][MB_BLOCK_SIZE], int use_hadamard, int blocktype)
{
int i, j, k, x, y, sad=0;
int block_size_x = input->blc_size[blocktype][0];
int block_size_y = input->blc_size[blocktype][1];
int block_size = (blocktype>4) ? 4:8;
int diff[MB_BLOCK_SIZE*MB_BLOCK_SIZE];
k=0;
for(y=0; y<block_size_y; y+=block_size)
for(x=0; x<block_size_x; x+=block_size)
for(j=y; j<y+block_size; j++)
for(i=x; i<x+block_size; i++, k++)
diff[k]=curr_diff[j][i];
if(use_hadamard)
{
switch(blocktype)
{
//16x16
case 1:
sad = SATD8X8 (diff, input->hadamard);
sad += SATD8X8 (&diff[64], input->hadamard);
sad += SATD8X8 (&diff[128], input->hadamard);
sad += SATD8X8 (&diff[192], input->hadamard);
break;
//16x8 8x16
case 2:
case 3: sad = SATD8X8 (diff, input->hadamard);
sad += SATD8X8 (&diff[64], input->hadamard);
break;
//8x8
case 4: sad = SATD8X8 (diff, input->hadamard);
break;
//8x4 4x8
case 5:
case 6: sad = SATD (diff, input->hadamard);
sad += SATD (&diff[16], input->hadamard);
break;
//4x4
case 7: sad = SATD (diff, input->hadamard);
break;
default:sad=-1;
break;
}
}
else
{
for(i=0; i<(block_size_y*block_size_x); i++)
sad += byte_abs[diff[i]];
}
return sad;
}
/*!
***********************************************************************
* \brief
* Sub pixel block motion search
***********************************************************************
*/
int // ==> minimum motion cost after search
SubPelBlockMotionSearch (pel_t** orig_pic, // <-- original pixel values for the AxB block
short ref, // <-- reference frame (0... or -1 (backward))
int list, // <-- reference picture 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)
int min_mcost, // <-- minimum motion cost (cost for center or huge value)
double lambda // <-- lagrangian parameter for determining motion cost
)
{
int curr_diff[MB_BLOCK_SIZE][MB_BLOCK_SIZE], i, j, k;
int diff[16], *d;
int pos, best_pos, mcost, abort_search;
int y0, x0, ry0, rx0, ry;
int cand_mv_x, cand_mv_y;
int max_pos_x4, max_pos_y4;
pel_t *orig_line;
pel_t **ref_pic;
StorablePicture *ref_picture;
int lambda_factor = LAMBDA_FACTOR (lambda);
int mv_shift = 0;
int check_position0 = (blocktype==1 && *mv_x==0 && *mv_y==0 && input->hadamard && !input->rdopt && img->type!=B_SLICE && ref==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 min_pos2 = (input->hadamard ? 0 : 1);
int max_pos2 = (input->hadamard ? max(1,search_pos2) : search_pos2);
int list_offset = ((img->MbaffFrameFlag)&&(img->mb_data[img->current_mb_nr].mb_field))? img->current_mb_nr%2 ? 4 : 2 : 0;
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)));
int img_width, img_height;
ref_picture = listX[list+list_offset][ref];
//===== Use weighted Reference for ME ====
if (apply_weights && input->UseWeightedReferenceME)
{
ref_pic = listX[list+list_offset][ref]->imgY_ups_w;
}
else
ref_pic = listX[list+list_offset][ref]->imgY_ups;
img_width = ref_picture->size_x;
img_height = ref_picture->size_y;
max_pos_x4 = ((ref_picture->size_x - blocksize_x+1)<<2);
max_pos_y4 = ((ref_picture->size_y - blocksize_y+1)<<2);
/*********************************
***** *****
***** HALF-PEL REFINEMENT *****
***** *****
*********************************/
//===== convert search center to quarter-pel units =====
*mv_x <<= 2;
*mv_y <<= 2;
//===== set function for getting pixel values =====
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;
}
//===== loop over search positions =====
for (best_pos = 0, pos = min_pos2; pos < max_pos2; pos++)
{
cand_mv_x = *mv_x + (spiral_search_x[pos] << 1); // quarter-pel units
cand_mv_y = *mv_y + (spiral_search_y[pos] << 1); // quarter-pel units
//----- set motion vector cost -----
mcost = MV_COST (lambda_factor, mv_shift, cand_mv_x, cand_mv_y, pred_mv_x, pred_mv_y);
if (check_position0 && pos==0)
{
mcost -= WEIGHTED_COST (lambda_factor, 16);
}
if (mcost >= min_mcost) continue;
//----- add up SATD -----
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 (!input->AllowTransform8x8)
{
if ((mcost += SATD (diff, input->hadamard)) > min_mcost)
{
abort_search = 1;
break;
}
}
else
{
for(j=0, k=0; j<4; j++)
for(i=0; i<4; i++, k++)
curr_diff[y0+j][x0+i] = diff[k];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -