mv-search.c
来自「包含了从MPEG4的视频解码到H.264的视频编码部分的源代码」· C语言 代码 · 共 1,913 行 · 第 1/5 页
C
1,913 行
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);
}
//----- 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 );
*d++ = orig_line[x0+1] - PelY_14 (ref_pic, ry, rx0+ 4);
*d++ = orig_line[x0+2] - PelY_14 (ref_pic, ry, rx0+ 8);
*d++ = orig_line[x0+3] - PelY_14 (ref_pic, ry, rx0+12);
orig_line = orig_pic [y0+1]; ry=ry0+4;
*d++ = orig_line[x0 ] - PelY_14 (ref_pic, ry, rx0 );
*d++ = orig_line[x0+1] - PelY_14 (ref_pic, ry, rx0+ 4);
*d++ = orig_line[x0+2] - PelY_14 (ref_pic, ry, rx0+ 8);
*d++ = orig_line[x0+3] - PelY_14 (ref_pic, ry, rx0+12);
orig_line = orig_pic [y0+2]; ry=ry0+8;
*d++ = orig_line[x0 ] - PelY_14 (ref_pic, ry, rx0 );
*d++ = orig_line[x0+1] - PelY_14 (ref_pic, ry, rx0+ 4);
*d++ = orig_line[x0+2] - PelY_14 (ref_pic, ry, rx0+ 8);
*d++ = orig_line[x0+3] - PelY_14 (ref_pic, ry, rx0+12);
orig_line = orig_pic [y0+3]; ry=ry0+12;
*d++ = orig_line[x0 ] - PelY_14 (ref_pic, ry, rx0 );
*d++ = orig_line[x0+1] - PelY_14 (ref_pic, ry, rx0+ 4);
*d++ = orig_line[x0+2] - PelY_14 (ref_pic, ry, rx0+ 8);
*d = orig_line[x0+3] - PelY_14 (ref_pic, ry, rx0+12);
if ((mcost += SATD (diff, input->hadamard)) > min_mcost)
{
abort_search = 1;
break;
}
}
}
if (mcost < min_mcost)
{
min_mcost = mcost;
best_pos = pos;
}
}
if (best_pos)
{
*mv_x += (spiral_search_x [best_pos] << 1);
*mv_y += (spiral_search_y [best_pos] << 1);
}
/************************************
***** *****
***** QUARTER-PEL REFINEMENT *****
***** *****
************************************/
//===== set function for getting pixel values =====
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) )
{
PelY_14 = FastPelY_14;
}
else
{
PelY_14 = UMVPelY_14;
}
//===== loop over search positions =====
for (best_pos = 0, pos = 1; pos < search_pos4; pos++)
{
cand_mv_x = *mv_x + spiral_search_x[pos]; // quarter-pel units
cand_mv_y = *mv_y + spiral_search_y[pos]; // 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);
//----- 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 );
*d++ = orig_line[x0+1] - PelY_14 (ref_pic, ry, rx0+ 4);
*d++ = orig_line[x0+2] - PelY_14 (ref_pic, ry, rx0+ 8);
*d++ = orig_line[x0+3] - PelY_14 (ref_pic, ry, rx0+12);
orig_line = orig_pic [y0+1]; ry=ry0+4;
*d++ = orig_line[x0 ] - PelY_14 (ref_pic, ry, rx0 );
*d++ = orig_line[x0+1] - PelY_14 (ref_pic, ry, rx0+ 4);
*d++ = orig_line[x0+2] - PelY_14 (ref_pic, ry, rx0+ 8);
*d++ = orig_line[x0+3] - PelY_14 (ref_pic, ry, rx0+12);
orig_line = orig_pic [y0+2]; ry=ry0+8;
*d++ = orig_line[x0 ] - PelY_14 (ref_pic, ry, rx0 );
*d++ = orig_line[x0+1] - PelY_14 (ref_pic, ry, rx0+ 4);
*d++ = orig_line[x0+2] - PelY_14 (ref_pic, ry, rx0+ 8);
*d++ = orig_line[x0+3] - PelY_14 (ref_pic, ry, rx0+12);
orig_line = orig_pic [y0+3]; ry=ry0+12;
*d++ = orig_line[x0 ] - PelY_14 (ref_pic, ry, rx0 );
*d++ = orig_line[x0+1] - PelY_14 (ref_pic, ry, rx0+ 4);
*d++ = orig_line[x0+2] - PelY_14 (ref_pic, ry, rx0+ 8);
*d = orig_line[x0+3] - PelY_14 (ref_pic, ry, rx0+12);
if ((mcost += SATD (diff, input->hadamard)) > min_mcost)
{
abort_search = 1;
break;
}
}
}
if (mcost < min_mcost)
{
min_mcost = mcost;
best_pos = pos;
}
}
if (best_pos)
{
*mv_x += spiral_search_x [best_pos];
*mv_y += spiral_search_y [best_pos];
}
//===== return minimum motion cost =====
return min_mcost;
}
/*!
***********************************************************************
* \brief
* Block motion search
***********************************************************************
*/
int // ==> minimum motion cost after search
BlockMotionSearch (int ref, // <-- reference frame (0... or -1 (backward))
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 search_range, // <-- 1-d search range for integer-position search
double lambda // <-- lagrangian parameter for determining motion cost
)
{
static pel_t orig_val [256];
static pel_t *orig_pic [16] = {orig_val, orig_val+ 16, orig_val+ 32, orig_val+ 48,
orig_val+ 64, orig_val+ 80, orig_val+ 96, orig_val+112,
orig_val+128, orig_val+144, orig_val+160, orig_val+176,
orig_val+192, orig_val+208, orig_val+224, orig_val+240};
int pred_mv_x, pred_mv_y, mv_x, mv_y, i, j;
int max_value = (1<<20);
int min_mcost = max_value;
int mb_x = pic_pix_x-img->pix_x;
int mb_y = pic_pix_y-img->pix_y;
int block_x = (mb_x>>2);
int block_y = (mb_y>>2);
int bsx = input->blc_size[blocktype][0];
int bsy = input->blc_size[blocktype][1];
int refframe = (ref==-1 ? 0 : ref);
int* pred_mv;
int** ref_array = ((img->type!=B_IMG && img->type!=BS_IMG) ? refFrArr : ref>=0 ? fw_refFrArr : bw_refFrArr);
int*** mv_array = ((img->type!=B_IMG && img->type!=BS_IMG) ? tmp_mv : ref>=0 ? tmp_fwMV : tmp_bwMV);
int***** all_bmv = img->all_bmv;
int***** all_mv = (ref==-1 ? img->all_bmv : img->all_mv);
byte** imgY_org_pic = imgY_org;
if(input->InterlaceCodingOption >= MB_CODING && mb_adaptive && img->field_mode)
{
mb_y = pic_pix_y - img->field_pix_y;
block_y = mb_y >> 2;
if(img->top_field)
{
pred_mv = ((img->type!=B_IMG && img->type!=BS_IMG) ? img->mv_top : ref>=0 ? img->p_fwMV_top : img->p_bwMV_top)[mb_x>>2][mb_y>>2][refframe][blocktype];
ref_array = ((img->type!=B_IMG && img->type!=BS_IMG) ? refFrArr_top : ref>=0 ? fw_refFrArr_top : bw_refFrArr_top);
mv_array = ((img->type!=B_IMG && img->type!=BS_IMG) ? tmp_mv_top : ref>=0 ? tmp_fwMV_top : tmp_bwMV_top);
all_bmv = img->all_bmv_top;
all_mv = (ref==-1 ? img->all_bmv_top : img->all_mv_top);
imgY_org_pic = imgY_org_top;
}
else
{
pred_mv = ((img->type!=B_IMG && img->type!=BS_IMG) ? img->mv_bot : ref>=0 ? img->p_fwMV_bot : img->p_bwMV_bot)[mb_x>>2][mb_y>>2][refframe][blocktype];
ref_array = ((img->type!=B_IMG && img->type!=BS_IMG) ? refFrArr_bot : ref>=0 ? fw_refFrArr_bot : bw_refFrArr_bot);
mv_array = ((img->type!=B_IMG && img->type!=BS_IMG) ? tmp_mv_bot : ref>=0 ? tmp_fwMV_bot : tmp_bwMV_bot);
all_bmv = img->all_bmv_bot;
all_mv = (ref==-1 ? img->all_bmv_bot : img->all_mv_bot);
imgY_org_pic = imgY_org_bot;
}
}
else
pred_mv = ((img->type!=B_IMG && img->type!=BS_IMG) ? img->mv : ref>=0 ? img->p_fwMV : img->p_bwMV)[mb_x>>2][mb_y>>2][refframe][blocktype];
//==================================
//===== GET ORIGINAL BLOCK =====
//==================================
for (j = 0; j < bsy; j++)
{
for (i = 0; i < bsx; i++)
{
orig_pic[j][i] = imgY_org_pic[pic_pix_y+j][pic_pix_x+i];
}
}
//===========================================
//===== GET MOTION VECTOR PREDICTOR =====
//===========================================
SetMotionVectorPredictor (pred_mv, ref_array, mv_array, refframe, mb_x, mb_y, bsx, bsy);
pred_mv_x = pred_mv[0];
pred_mv_y = pred_mv[1];
min_mcost =MVFastSearch(orig_pic, ref, pic_pix_x, pic_pix_y, blocktype,
pred_mv_x, pred_mv_y, &mv_x, &mv_y, search_range,
min_mcost, lambda);
/*
//==================================
//===== INTEGER-PEL SEARCH =====
//==================================
#ifndef _FAST_FULL_ME_
//--- set search center ---
mv_x = pred_mv_x / 4;
mv_y = pred_mv_y / 4;
if (!input->rdopt)
{
//--- adjust search center so that the (0,0)-vector is inside ---
mv_x = max (-search_range, min (search_range, mv_x));
mv_y = max (-search_range, min (search_range, mv_y));
}
//--- perform motion search ---
min_mcost = FullPelBlockMotionSearch (orig_pic, ref, pic_pix_x, pic_pix_y, blocktype,
pred_mv_x, pred_mv_y, &mv_x, &mv_y, search_range,
min_mcost, lambda);
#else
// comments: - orig_pic is not used -> be careful
// - search center is automatically determined
min_mcost = FastFullPelBlockMotionSearch (orig_pic, ref, pic_pix_x, pic_pix_y, blocktype,
pred_mv_x, pred_mv_y, &mv_x, &mv_y, search_range,
min_mcost, lambda);
#endif
*/
//==============================
//===== SUB-PEL SEARCH =====
//==============================
if (input->hadamard)
{
min_mcost = max_value;
}
min_mcost = SubPelBlockMotionSearch (orig_pic, ref, pic_pix_x, pic_pix_y, blocktype,
pred_mv_x, pred_mv_y, &mv_x, &mv_y, 9, 9,
min_mcost, lambda);
if (!input->rdopt)
{
// Get the skip mode cost
if (blocktype == 1 && img->type == INTER_IMG)
{
int cost;
FindSkipModeMotionVector ();
cost = GetSkipCostMB (lambda);
cost -= (int)floor(8*lambda+0.4999);
if (cost < min_mcost)
{
min_mcost = cost;
mv_x = img->all_mv [0][0][0][0][0];
mv_y = img->all_mv [0][0][0][0][1];
}
}
}
//===============================================
//===== SET MV'S AND RETURN MOTION COST =====
//===============================================
for (i=0; i < (bsx>>2); i++)
{
for (j=0; j < (bsy>>2); j++)
{
all_mv[block_x+i][block_y+j][refframe][blocktype][0] = mv_x;
all_mv[block_x+i][block_y+j][refframe][blocktype][1] = mv_y;
}
}
if (img->type==BS_IMG)
{
for (i=0; i < (bsx>>2); i++)
for (j=0; j < (bsy>>2); j++)
{
// Backward
all_bmv[block_x+i][block_y+j][ref][blocktype][0] = mv_x;
all_bmv[block_x+i][block_y+j][ref][blocktype][1] = mv_y;
}
}
return min_mcost;
}
/*!
***********************************************************************
* \brief
* Motion Cost for Bidirectional modes
***********************************************************************
*/
int
BIDPartitionCost (int blocktype,
int block8x8,
int fw_ref,
int lambda_factor)
{
static int bx0[5][4] = {{0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {0,2,0,0}, {0,2,0,2}};
static int by0[5][4] = {{0,0,0,0}, {0,0,0,0}, {0,2,0,0}, {0,0,0,0}, {0,0,2,2}};
_int16 diff[16];
int pic_pix_x, pic_pix_y, block_x, block_y;
int v, h, mcost, i, j, k;
int mvd_bits = 0;
int parttype = (blocktype<4?blocktype:4);
int step_h0 = (input->blc_size[ parttype][0]>>2);
int step_v0 = (input->blc_size[ parttype][1]>>2);
int step_h = (input->blc_size[blocktype][0]>>2);
int step_v = (input->blc_size[blocktype][1]>>2);
int bxx, byy; // indexing curr_blk
byte** imgY_original = imgY_org;
int pix_y = img->pix_y;
int *****all_mv = img->all_mv;
int *****all_bmv = img->all_bmv;
int *****p_fwMV = img->p_fwMV;
int *****p_bwMV = img->p_bwMV;
if(input->InterlaceCodingOption >= MB_CODING && mb_adaptive && img->field_mode)
{
pix_y = img->field_pix_y;
if(img->top_field)
{
imgY_original = imgY_org_top;
all_mv = img->all_mv_top;
all_bmv = img->all_bmv_top;
p_fwMV = img->p_fwMV_top;
p_bwMV = img->p_bwMV_top;
}
else
{
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?