📄 motion.c
字号:
&iminb,&jminb);
imins[0][1] = imint;
jmins[0][1] = jmint;
imins[1][1] = iminb;
jmins[1][1] = jminb;
/* select prediction for bottom field */
if (db<=dt)
{
dminb=db; *iminbp=iminb; *jminbp=jminb; *bselp=1;
}
else
{
dminb=dt; *iminbp=imint; *jminbp=jmint; *bselp=0;
}
*dfieldp=dmint+dminb;
}
/*
* field picture motion estimation subroutine
*
* toporg: address of original top reference field
* topref: address of reconstructed top reference field
* botorg: address of original bottom reference field
* botref: address of reconstructed bottom reference field
* mb: macroblock to be matched
* i,j: location of mb (=center of search window)
* sx,sy: half width/height of search window
*
* iminp,jminp,selp,dfieldp: location and distance of best field prediction
* imin8up,jmin8up,sel8up: location of best 16x8 pred. for upper half of mb
* imin8lp,jmin8lp,sel8lp: location of best 16x8 pred. for lower half of mb
* d8p: distance of best 16x8 prediction
* iminsp,jminsp,dsp: location and distance of best same parity field
* prediction (needed for dual prime, only valid if
* ipflag==0)
*/
static void field_estimate(toporg,topref,botorg,botref,mb,i,j,sx,sy,ipflag,
iminp,jminp,imin8up,jmin8up,imin8lp,jmin8lp,dfieldp,d8p,selp,sel8up,sel8lp,
iminsp,jminsp,dsp)
unsigned char *toporg, *topref, *botorg, *botref, *mb;
int i,j,sx,sy;
int ipflag;
int *iminp, *jminp;
int *imin8up, *jmin8up, *imin8lp, *jmin8lp;
int *dfieldp,*d8p;
int *selp, *sel8up, *sel8lp;
int *iminsp, *jminsp, *dsp;
{
int dt, db, imint, jmint, iminb, jminb, notop, nobot;
/* if ipflag is set, predict from field of opposite parity only */
notop = ipflag && (pict_struct==TOP_FIELD);
nobot = ipflag && (pict_struct==BOTTOM_FIELD);
/* field prediction */
/* predict current field from top field */
if (notop)
dt = 65536; /* infinity */
else
dt = fullsearch(toporg,topref,mb,width<<1,
i,j,sx,sy>>1,16,width,height>>1,
&imint,&jmint);
/* predict current field from bottom field */
if (nobot)
db = 65536; /* infinity */
else
db = fullsearch(botorg,botref,mb,width<<1,
i,j,sx,sy>>1,16,width,height>>1,
&iminb,&jminb);
/* same parity prediction (only valid if ipflag==0) */
if (pict_struct==TOP_FIELD)
{
*iminsp = imint; *jminsp = jmint; *dsp = dt;
}
else
{
*iminsp = iminb; *jminsp = jminb; *dsp = db;
}
/* select field prediction */
if (dt<=db)
{
*dfieldp = dt; *iminp = imint; *jminp = jmint; *selp = 0;
}
else
{
*dfieldp = db; *iminp = iminb; *jminp = jminb; *selp = 1;
}
/* 16x8 motion compensation */
/* predict upper half field from top field */
if (notop)
dt = 65536;
else
dt = fullsearch(toporg,topref,mb,width<<1,
i,j,sx,sy>>1,8,width,height>>1,
&imint,&jmint);
/* predict upper half field from bottom field */
if (nobot)
db = 65536;
else
db = fullsearch(botorg,botref,mb,width<<1,
i,j,sx,sy>>1,8,width,height>>1,
&iminb,&jminb);
/* select prediction for upper half field */
if (dt<=db)
{
*d8p = dt; *imin8up = imint; *jmin8up = jmint; *sel8up = 0;
}
else
{
*d8p = db; *imin8up = iminb; *jmin8up = jminb; *sel8up = 1;
}
/* predict lower half field from top field */
if (notop)
dt = 65536;
else
dt = fullsearch(toporg,topref,mb+(width<<4),width<<1,
i,j+8,sx,sy>>1,8,width,height>>1,
&imint,&jmint);
/* predict lower half field from bottom field */
if (nobot)
db = 65536;
else
db = fullsearch(botorg,botref,mb+(width<<4),width<<1,
i,j+8,sx,sy>>1,8,width,height>>1,
&iminb,&jminb);
/* select prediction for lower half field */
if (dt<=db)
{
*d8p += dt; *imin8lp = imint; *jmin8lp = jmint; *sel8lp = 0;
}
else
{
*d8p += db; *imin8lp = iminb; *jmin8lp = jminb; *sel8lp = 1;
}
}
static void dpframe_estimate(ref,mb,i,j,iminf,jminf,
iminp,jminp,imindmvp, jmindmvp, dmcp, vmcp)
unsigned char *ref, *mb;
int i,j;
int iminf[2][2], jminf[2][2];
int *iminp, *jminp;
int *imindmvp, *jmindmvp;
int *dmcp,*vmcp;
{
int pref,ppred,delta_x,delta_y;
int is,js,it,jt,ib,jb,it0,jt0,ib0,jb0;
int imins,jmins,imint,jmint,iminb,jminb,imindmv,jmindmv;
int vmc,local_dist;
/* Calculate Dual Prime distortions for 9 delta candidates
* for each of the four minimum field vectors
* Note: only for P pictures!
*/
/* initialize minimum dual prime distortion to large value */
vmc = 1 << 30;
for (pref=0; pref<2; pref++)
{
for (ppred=0; ppred<2; ppred++)
{
/* convert Cartesian absolute to relative motion vector
* values (wrt current macroblock address (i,j)
*/
is = iminf[pref][ppred] - (i<<1);
js = jminf[pref][ppred] - (j<<1);
if (pref!=ppred)
{
/* vertical field shift adjustment */
if (ppred==0)
js++;
else
js--;
/* mvxs and mvys scaling*/
is<<=1;
js<<=1;
if (topfirst == ppred)
{
/* second field: scale by 1/3 */
is = (is>=0) ? (is+1)/3 : -((-is+1)/3);
js = (js>=0) ? (js+1)/3 : -((-js+1)/3);
}
else
continue;
}
/* vector for prediction from field of opposite 'parity' */
if (topfirst)
{
/* vector for prediction of top field from bottom field */
it0 = ((is+(is>0))>>1);
jt0 = ((js+(js>0))>>1) - 1;
/* vector for prediction of bottom field from top field */
ib0 = ((3*is+(is>0))>>1);
jb0 = ((3*js+(js>0))>>1) + 1;
}
else
{
/* vector for prediction of top field from bottom field */
it0 = ((3*is+(is>0))>>1);
jt0 = ((3*js+(js>0))>>1) - 1;
/* vector for prediction of bottom field from top field */
ib0 = ((is+(is>0))>>1);
jb0 = ((js+(js>0))>>1) + 1;
}
/* convert back to absolute half-pel field picture coordinates */
is += i<<1;
js += j<<1;
it0 += i<<1;
jt0 += j<<1;
ib0 += i<<1;
jb0 += j<<1;
if (is >= 0 && is <= (width-16)<<1 &&
js >= 0 && js <= (height-16))
{
for (delta_y=-1; delta_y<=1; delta_y++)
{
for (delta_x=-1; delta_x<=1; delta_x++)
{
/* opposite field coordinates */
it = it0 + delta_x;
jt = jt0 + delta_y;
ib = ib0 + delta_x;
jb = jb0 + delta_y;
if (it >= 0 && it <= (width-16)<<1 &&
jt >= 0 && jt <= (height-16) &&
ib >= 0 && ib <= (width-16)<<1 &&
jb >= 0 && jb <= (height-16))
{
/* compute prediction error */
local_dist = bdist2(
ref + (is>>1) + (width<<1)*(js>>1),
ref + width + (it>>1) + (width<<1)*(jt>>1),
mb, /* current mb location */
width<<1, /* adjacent line distance */
is&1, js&1, it&1, jt&1, /* half-pel flags */
8); /* block height */
local_dist += bdist2(
ref + width + (is>>1) + (width<<1)*(js>>1),
ref + (ib>>1) + (width<<1)*(jb>>1),
mb + width, /* current mb location */
width<<1, /* adjacent line distance */
is&1, js&1, ib&1, jb&1, /* half-pel flags */
8); /* block height */
/* update delta with least distortion vector */
if (local_dist < vmc)
{
imins = is;
jmins = js;
imint = it;
jmint = jt;
iminb = ib;
jminb = jb;
imindmv = delta_x;
jmindmv = delta_y;
vmc = local_dist;
}
}
} /* end delta x loop */
} /* end delta y loop */
}
}
}
/* Compute L1 error for decision purposes */
local_dist = bdist1(
ref + (imins>>1) + (width<<1)*(jmins>>1),
ref + width + (imint>>1) + (width<<1)*(jmint>>1),
mb,
width<<1,
imins&1, jmins&1, imint&1, jmint&1,
8);
local_dist += bdist1(
ref + width + (imins>>1) + (width<<1)*(jmins>>1),
ref + (iminb>>1) + (width<<1)*(jminb>>1),
mb + width,
width<<1,
imins&1, jmins&1, iminb&1, jminb&1,
8);
*dmcp = local_dist;
*iminp = imins;
*jminp = jmins;
*imindmvp = imindmv;
*jmindmvp = jmindmv;
*vmcp = vmc;
}
static void dpfield_estimate(topref,botref,mb,i,j,imins,jmins,
imindmvp, jmindmvp, dmcp, vmcp)
unsigned char *topref, *botref, *mb;
int i,j;
int imins, jmins;
int *imindmvp, *jmindmvp;
int *dmcp,*vmcp;
{
unsigned char *sameref, *oppref;
int io0,jo0,io,jo,delta_x,delta_y,mvxs,mvys,mvxo0,mvyo0;
int imino,jmino,imindmv,jmindmv,vmc_dp,local_dist;
/* Calculate Dual Prime distortions for 9 delta candidates */
/* Note: only for P pictures! */
/* Assign opposite and same reference pointer */
if (pict_struct==TOP_FIELD)
{
sameref = topref;
oppref = botref;
}
else
{
sameref = botref;
oppref = topref;
}
/* convert Cartesian absolute to relative motion vector
* values (wrt current macroblock address (i,j)
*/
mvxs = imins - (i<<1);
mvys = jmins - (j<<1);
/* vector for prediction from field of opposite 'parity' */
mvxo0 = (mvxs+(mvxs>0)) >> 1; /* mvxs // 2 */
mvyo0 = (mvys+(mvys>0)) >> 1; /* mvys // 2 */
/* vertical field shift correction */
if (pict_struct==TOP_FIELD)
mvyo0--;
else
mvyo0++;
/* convert back to absolute coordinates */
io0 = mvxo0 + (i<<1);
jo0 = mvyo0 + (j<<1);
/* initialize minimum dual prime distortion to large value */
vmc_dp = 1 << 30;
for (delta_y = -1; delta_y <= 1; delta_y++)
{
for (delta_x = -1; delta_x <=1; delta_x++)
{
/* opposite field coordinates */
io = io0 + delta_x;
jo = jo0 + delta_y;
if (io >= 0 && io <= (width-16)<<1 &&
jo >= 0 && jo <= (height2-16)<<1)
{
/* compute prediction error */
local_dist = bdist2(
sameref + (imins>>1) + width2*(jmins>>1),
oppref + (io>>1) + width2*(jo>>1),
mb, /* current mb location */
width2, /* adjacent line distance */
imins&1, jmins&1, io&1, jo&1, /* half-pel flags */
16); /* block height */
/* update delta with least distortion vector */
if (local_dist < vmc_dp)
{
imino = io;
jmino = jo;
imindmv = delta_x;
jmindmv = delta_y;
vmc_dp = local_dist;
}
}
} /* end delta x loop */
} /* end delta y loop */
/* Compute L1 error for decision purposes */
*dmcp = bdist1(
sameref + (imins>>1) + width2*(jmins>>1),
oppref + (imino>>1) + width2*(jmino>>1),
mb, /* current mb location */
width2, /* adjacent line distance */
imins&1, jmins&1, imino&1, jmino&1, /* half-pel flags */
16); /* block height */
*imindmvp = imindmv;
*jmindmvp = jmindmv;
*vmcp = vmc_dp;
}
/*
* full search block matching
*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -