📄 motion.c
字号:
* prediction error variance (vmc)
*
* blocks with small prediction error are always coded non-intra
* even if variance is smaller (is this reasonable?)
*/
if (vmc>var && vmc>=9*256)
mbi->mb_type = MB_INTRA;
else
{
var = vmc;
if (mbi->motion_type==MC_FRAME)
{
/* forward */
mbi->MV[0][0][0] = iminf - (i<<1);
mbi->MV[0][0][1] = jminf - (j<<1);
/* backward */
mbi->MV[0][1][0] = iminr - (i<<1);
mbi->MV[0][1][1] = jminr - (j<<1);
}
else
{
/* these are FRAME vectors */
/* forward */
mbi->MV[0][0][0] = imintf - (i<<1);
mbi->MV[0][0][1] = (jmintf<<1) - (j<<1);
mbi->MV[1][0][0] = iminbf - (i<<1);
mbi->MV[1][0][1] = (jminbf<<1) - (j<<1);
mbi->mv_field_sel[0][0] = tself;
mbi->mv_field_sel[1][0] = bself;
/* backward */
mbi->MV[0][1][0] = imintr - (i<<1);
mbi->MV[0][1][1] = (jmintr<<1) - (j<<1);
mbi->MV[1][1][0] = iminbr - (i<<1);
mbi->MV[1][1][1] = (jminbr<<1) - (j<<1);
mbi->mv_field_sel[0][1] = tselr;
mbi->mv_field_sel[1][1] = bselr;
}
}
}
mbi->var = var;
}
/*
* motion estimation for field pictures
*
* oldorg: original frame for forward prediction (P and B frames)
* neworg: original frame for backward prediction (B frames only)
* oldref: reconstructed frame for forward prediction (P and B frames)
* newref: reconstructed frame for backward prediction (B frames only)
* cur: current original frame (the one for which the prediction is formed)
* curref: current reconstructed frame (to predict second field from first)
* sxf,syf: forward search window (frame coordinates)
* sxb,syb: backward search window (frame coordinates)
* mbi: pointer to macroblock info structure
* secondfield: indicates second field of a frame (in P fields this means
* that reference field of opposite parity is in curref instead
* of oldref)
* ipflag: indicates a P type field which is the second field of a frame
* in which the first field is I type (this restricts predictions
* to be based only on the opposite parity (=I) field)
*
* results:
* mbi->
* mb_type: 0, MB_INTRA, MB_FORWARD, MB_BACKWARD, MB_FORWARD|MB_BACKWARD
* MV[][][]: motion vectors (field format)
* mv_field_sel: top/bottom field
* motion_type: MC_FIELD, MC_16X8
*
* uses global vars: pict_type, pict_struct
*/
static void field_ME(oldorg,neworg,oldref,newref,cur,curref,i,j,
sxf,syf,sxb,syb,mbi,secondfield,ipflag)
unsigned char *oldorg,*neworg,*oldref,*newref,*cur,*curref;
int i,j,sxf,syf,sxb,syb;
struct mbinfo *mbi;
int secondfield,ipflag;
{
int w2;
unsigned char *mb, *toporg, *topref, *botorg, *botref;
int var,vmc,v0,dmc,dmcfieldi,dmc8i;
int imin,jmin,imin8u,jmin8u,imin8l,jmin8l,dmcfield,dmc8,sel,sel8u,sel8l;
int iminf,jminf,imin8uf,jmin8uf,imin8lf,jmin8lf,dmcfieldf,dmc8f,self,sel8uf,sel8lf;
int iminr,jminr,imin8ur,jmin8ur,imin8lr,jmin8lr,dmcfieldr,dmc8r,selr,sel8ur,sel8lr;
int imins,jmins,ds,imindmv,jmindmv,vmc_dp,dmc_dp;
w2 = width<<1;
mb = cur + i + w2*j;
if (pict_struct==BOTTOM_FIELD)
mb += width;
var = variance(mb,w2);
if (pict_type==I_TYPE)
mbi->mb_type = MB_INTRA;
else if (pict_type==P_TYPE)
{
toporg = oldorg;
topref = oldref;
botorg = oldorg + width;
botref = oldref + width;
if (secondfield)
{
/* opposite parity field is in same frame */
if (pict_struct==TOP_FIELD)
{
/* current is top field */
botorg = cur + width;
botref = curref + width;
}
else
{
/* current is bottom field */
toporg = cur;
topref = curref;
}
}
field_estimate(toporg,topref,botorg,botref,mb,i,j,sxf,syf,ipflag,
&imin,&jmin,&imin8u,&jmin8u,&imin8l,&jmin8l,
&dmcfield,&dmc8,&sel,&sel8u,&sel8l,&imins,&jmins,&ds);
if (M==1 && !ipflag) /* generic condition which permits Dual Prime */
dpfield_estimate(topref,botref,mb,i,j,imins,jmins,&imindmv,&jmindmv,
&dmc_dp,&vmc_dp);
/* select between dual prime, field and 16x8 prediction */
if (M==1 && !ipflag && dmc_dp<dmc8 && dmc_dp<dmcfield)
{
/* Dual Prime prediction */
mbi->motion_type = MC_DMV;
dmc = dmc_dp; /* L1 metric */
vmc = vmc_dp; /* we already calculated L2 error for Dual */
}
else if (dmc8<dmcfield)
{
/* 16x8 prediction */
mbi->motion_type = MC_16X8;
/* upper half block */
vmc = dist2((sel8u?botref:topref) + (imin8u>>1) + w2*(jmin8u>>1),
mb,w2,imin8u&1,jmin8u&1,8);
/* lower half block */
vmc+= dist2((sel8l?botref:topref) + (imin8l>>1) + w2*(jmin8l>>1),
mb+8*w2,w2,imin8l&1,jmin8l&1,8);
}
else
{
/* field prediction */
mbi->motion_type = MC_FIELD;
vmc = dist2((sel?botref:topref) + (imin>>1) + w2*(jmin>>1),
mb,w2,imin&1,jmin&1,16);
}
/* select between intra and non-intra coding */
if (vmc>var && vmc>=9*256)
mbi->mb_type = MB_INTRA;
else
{
/* zero MV field prediction from same parity ref. field
* (not allowed if ipflag is set)
*/
if (!ipflag)
v0 = dist2(((pict_struct==BOTTOM_FIELD)?botref:topref) + i + w2*j,
mb,w2,0,0,16);
if (ipflag || (4*v0>5*vmc && v0>=9*256))
{
var = vmc;
mbi->mb_type = MB_FORWARD;
if (mbi->motion_type==MC_FIELD)
{
mbi->MV[0][0][0] = imin - (i<<1);
mbi->MV[0][0][1] = jmin - (j<<1);
mbi->mv_field_sel[0][0] = sel;
}
else if (mbi->motion_type==MC_DMV)
{
/* same parity vector */
mbi->MV[0][0][0] = imins - (i<<1);
mbi->MV[0][0][1] = jmins - (j<<1);
/* opposite parity vector */
mbi->dmvector[0] = imindmv;
mbi->dmvector[1] = jmindmv;
}
else
{
mbi->MV[0][0][0] = imin8u - (i<<1);
mbi->MV[0][0][1] = jmin8u - (j<<1);
mbi->MV[1][0][0] = imin8l - (i<<1);
mbi->MV[1][0][1] = jmin8l - ((j+8)<<1);
mbi->mv_field_sel[0][0] = sel8u;
mbi->mv_field_sel[1][0] = sel8l;
}
}
else
{
/* No MC */
var = v0;
mbi->mb_type = 0;
mbi->motion_type = MC_FIELD;
mbi->MV[0][0][0] = 0;
mbi->MV[0][0][1] = 0;
mbi->mv_field_sel[0][0] = (pict_struct==BOTTOM_FIELD);
}
}
}
else /* if (pict_type==B_TYPE) */
{
/* forward prediction */
field_estimate(oldorg,oldref,oldorg+width,oldref+width,mb,
i,j,sxf,syf,0,
&iminf,&jminf,&imin8uf,&jmin8uf,&imin8lf,&jmin8lf,
&dmcfieldf,&dmc8f,&self,&sel8uf,&sel8lf,&imins,&jmins,&ds);
/* backward prediction */
field_estimate(neworg,newref,neworg+width,newref+width,mb,
i,j,sxb,syb,0,
&iminr,&jminr,&imin8ur,&jmin8ur,&imin8lr,&jmin8lr,
&dmcfieldr,&dmc8r,&selr,&sel8ur,&sel8lr,&imins,&jmins,&ds);
/* calculate distances for bidirectional prediction */
/* field */
dmcfieldi = bdist1(oldref + (self?width:0) + (iminf>>1) + w2*(jminf>>1),
newref + (selr?width:0) + (iminr>>1) + w2*(jminr>>1),
mb,w2,iminf&1,jminf&1,iminr&1,jminr&1,16);
/* 16x8 upper half block */
dmc8i = bdist1(oldref + (sel8uf?width:0) + (imin8uf>>1) + w2*(jmin8uf>>1),
newref + (sel8ur?width:0) + (imin8ur>>1) + w2*(jmin8ur>>1),
mb,w2,imin8uf&1,jmin8uf&1,imin8ur&1,jmin8ur&1,8);
/* 16x8 lower half block */
dmc8i+= bdist1(oldref + (sel8lf?width:0) + (imin8lf>>1) + w2*(jmin8lf>>1),
newref + (sel8lr?width:0) + (imin8lr>>1) + w2*(jmin8lr>>1),
mb+8*w2,w2,imin8lf&1,jmin8lf&1,imin8lr&1,jmin8lr&1,8);
/* select prediction type of minimum distance */
if (dmcfieldi<dmc8i && dmcfieldi<dmcfieldf && dmcfieldi<dmc8f
&& dmcfieldi<dmcfieldr && dmcfieldi<dmc8r)
{
/* field, interpolated */
mbi->mb_type = MB_FORWARD|MB_BACKWARD;
mbi->motion_type = MC_FIELD;
vmc = bdist2(oldref + (self?width:0) + (iminf>>1) + w2*(jminf>>1),
newref + (selr?width:0) + (iminr>>1) + w2*(jminr>>1),
mb,w2,iminf&1,jminf&1,iminr&1,jminr&1,16);
}
else if (dmc8i<dmcfieldf && dmc8i<dmc8f
&& dmc8i<dmcfieldr && dmc8i<dmc8r)
{
/* 16x8, interpolated */
mbi->mb_type = MB_FORWARD|MB_BACKWARD;
mbi->motion_type = MC_16X8;
/* upper half block */
vmc = bdist2(oldref + (sel8uf?width:0) + (imin8uf>>1) + w2*(jmin8uf>>1),
newref + (sel8ur?width:0) + (imin8ur>>1) + w2*(jmin8ur>>1),
mb,w2,imin8uf&1,jmin8uf&1,imin8ur&1,jmin8ur&1,8);
/* lower half block */
vmc+= bdist2(oldref + (sel8lf?width:0) + (imin8lf>>1) + w2*(jmin8lf>>1),
newref + (sel8lr?width:0) + (imin8lr>>1) + w2*(jmin8lr>>1),
mb+8*w2,w2,imin8lf&1,jmin8lf&1,imin8lr&1,jmin8lr&1,8);
}
else if (dmcfieldf<dmc8f && dmcfieldf<dmcfieldr && dmcfieldf<dmc8r)
{
/* field, forward */
mbi->mb_type = MB_FORWARD;
mbi->motion_type = MC_FIELD;
vmc = dist2(oldref + (self?width:0) + (iminf>>1) + w2*(jminf>>1),
mb,w2,iminf&1,jminf&1,16);
}
else if (dmc8f<dmcfieldr && dmc8f<dmc8r)
{
/* 16x8, forward */
mbi->mb_type = MB_FORWARD;
mbi->motion_type = MC_16X8;
/* upper half block */
vmc = dist2(oldref + (sel8uf?width:0) + (imin8uf>>1) + w2*(jmin8uf>>1),
mb,w2,imin8uf&1,jmin8uf&1,8);
/* lower half block */
vmc+= dist2(oldref + (sel8lf?width:0) + (imin8lf>>1) + w2*(jmin8lf>>1),
mb+8*w2,w2,imin8lf&1,jmin8lf&1,8);
}
else if (dmcfieldr<dmc8r)
{
/* field, backward */
mbi->mb_type = MB_BACKWARD;
mbi->motion_type = MC_FIELD;
vmc = dist2(newref + (selr?width:0) + (iminr>>1) + w2*(jminr>>1),
mb,w2,iminr&1,jminr&1,16);
}
else
{
/* 16x8, backward */
mbi->mb_type = MB_BACKWARD;
mbi->motion_type = MC_16X8;
/* upper half block */
vmc = dist2(newref + (sel8ur?width:0) + (imin8ur>>1) + w2*(jmin8ur>>1),
mb,w2,imin8ur&1,jmin8ur&1,8);
/* lower half block */
vmc+= dist2(newref + (sel8lr?width:0) + (imin8lr>>1) + w2*(jmin8lr>>1),
mb+8*w2,w2,imin8lr&1,jmin8lr&1,8);
}
/* select between intra and non-intra coding */
if (vmc>var && vmc>=9*256)
mbi->mb_type = MB_INTRA;
else
{
var = vmc;
if (mbi->motion_type==MC_FIELD)
{
/* forward */
mbi->MV[0][0][0] = iminf - (i<<1);
mbi->MV[0][0][1] = jminf - (j<<1);
mbi->mv_field_sel[0][0] = self;
/* backward */
mbi->MV[0][1][0] = iminr - (i<<1);
mbi->MV[0][1][1] = jminr - (j<<1);
mbi->mv_field_sel[0][1] = selr;
}
else /* MC_16X8 */
{
/* forward */
mbi->MV[0][0][0] = imin8uf - (i<<1);
mbi->MV[0][0][1] = jmin8uf - (j<<1);
mbi->mv_field_sel[0][0] = sel8uf;
mbi->MV[1][0][0] = imin8lf - (i<<1);
mbi->MV[1][0][1] = jmin8lf - ((j+8)<<1);
mbi->mv_field_sel[1][0] = sel8lf;
/* backward */
mbi->MV[0][1][0] = imin8ur - (i<<1);
mbi->MV[0][1][1] = jmin8ur - (j<<1);
mbi->mv_field_sel[0][1] = sel8ur;
mbi->MV[1][1][0] = imin8lr - (i<<1);
mbi->MV[1][1][1] = jmin8lr - ((j+8)<<1);
mbi->mv_field_sel[1][1] = sel8lr;
}
}
}
mbi->var = var;
}
/*
* frame picture motion estimation
*
* org: top left pel of source reference frame
* ref: top left pel of reconstructed reference frame
* mb: macroblock to be matched
* i,j: location of mb relative to ref (=center of search window)
* sx,sy: half widths of search window
* iminp,jminp,dframep: location and value of best frame prediction
* imintp,jmintp,tselp: location of best field pred. for top field of mb
* iminbp,jminbp,bselp: location of best field pred. for bottom field of mb
* dfieldp: value of field prediction
*/
static void frame_estimate(org,ref,mb,i,j,sx,sy,
iminp,jminp,imintp,jmintp,iminbp,jminbp,dframep,dfieldp,tselp,bselp,
imins,jmins)
unsigned char *org,*ref,*mb;
int i,j,sx,sy;
int *iminp,*jminp;
int *imintp,*jmintp,*iminbp,*jminbp;
int *dframep,*dfieldp;
int *tselp,*bselp;
int imins[2][2],jmins[2][2];
{
int dt,db,dmint,dminb;
int imint,iminb,jmint,jminb;
/* frame prediction */
*dframep = fullsearch(org,ref,mb,width,i,j,sx,sy,16,width,height,
iminp,jminp);
/* predict top field from top field */
dt = fullsearch(org,ref,mb,width<<1,i,j>>1,sx,sy>>1,8,width,height>>1,
&imint,&jmint);
/* predict top field from bottom field */
db = fullsearch(org+width,ref+width,mb,width<<1,i,j>>1,sx,sy>>1,8,width,height>>1,
&iminb,&jminb);
imins[0][0] = imint;
jmins[0][0] = jmint;
imins[1][0] = iminb;
jmins[1][0] = jminb;
/* select prediction for top field */
if (dt<=db)
{
dmint=dt; *imintp=imint; *jmintp=jmint; *tselp=0;
}
else
{
dmint=db; *imintp=iminb; *jmintp=jminb; *tselp=1;
}
/* predict bottom field from top field */
dt = fullsearch(org,ref,mb+width,width<<1,i,j>>1,sx,sy>>1,8,width,height>>1,
&imint,&jmint);
/* predict bottom field from bottom field */
db = fullsearch(org+width,ref+width,mb+width,width<<1,i,j>>1,sx,sy>>1,8,width,height>>1,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -