📄 motion.c
字号:
/* motion.c, motion estimation */
/* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
/*
* Disclaimer of Warranty
*
* These software programs are available to the user without any license fee or
* royalty on an "as is" basis. The MPEG Software Simulation Group disclaims
* any and all warranties, whether express, implied, or statuary, including any
* implied warranties or merchantability or of fitness for a particular
* purpose. In no event shall the copyright-holder be liable for any
* incidental, punitive, or consequential damages of any kind whatsoever
* arising from the use of these programs.
*
* This disclaimer of warranty extends to the user of these programs and user's
* customers, employees, agents, transferees, successors, and assigns.
*
* The MPEG Software Simulation Group does not represent or warrant that the
* programs furnished hereunder are free of infringement of any third-party
* patents.
*
* Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
* are subject to royalty fees to patent holders. Many of these patents are
* general enough such that they are unavoidable regardless of implementation
* design.
*
*/
#include <stdio.h>
#include "config.h"
#include "global.h"
/* private prototypes */
static void frame_ME _ANSI_ARGS_((unsigned char *oldorg, unsigned char *neworg,
unsigned char *oldref, unsigned char *newref, unsigned char *cur,
int i, int j, int sxf, int syf, int sxb, int syb, struct mbinfo *mbi));
static void field_ME _ANSI_ARGS_((unsigned char *oldorg, unsigned char *neworg,
unsigned char *oldref, unsigned char *newref, unsigned char *cur,
unsigned char *curref, int i, int j, int sxf, int syf, int sxb, int syb,
struct mbinfo *mbi, int secondfield, int ipflag));
static void frame_estimate _ANSI_ARGS_((unsigned char *org,
unsigned char *ref, unsigned char *mb,
int i, int j,
int sx, int sy, int *iminp, int *jminp, int *imintp, int *jmintp,
int *iminbp, int *jminbp, int *dframep, int *dfieldp,
int *tselp, int *bselp, int imins[2][2], int jmins[2][2]));
static void field_estimate _ANSI_ARGS_((unsigned char *toporg,
unsigned char *topref, unsigned char *botorg, unsigned char *botref,
unsigned char *mb, int i, int j, int sx, int sy, int ipflag,
int *iminp, int *jminp, int *imin8up, int *jmin8up, int *imin8lp,
int *jmin8lp, int *dfieldp, int *d8p, int *selp, int *sel8up, int *sel8lp,
int *iminsp, int *jminsp, int *dsp));
static void dpframe_estimate _ANSI_ARGS_((unsigned char *ref,
unsigned char *mb, int i, int j, int iminf[2][2], int jminf[2][2],
int *iminp, int *jminp, int *imindmvp, int *jmindmvp,
int *dmcp, int *vmcp));
static void dpfield_estimate _ANSI_ARGS_((unsigned char *topref,
unsigned char *botref, unsigned char *mb,
int i, int j, int imins, int jmins, int *imindmvp, int *jmindmvp,
int *dmcp, int *vmcp));
static int fullsearch _ANSI_ARGS_((unsigned char *org, unsigned char *ref,
unsigned char *blk,
int lx, int i0, int j0, int sx, int sy, int h, int xmax, int ymax,
int *iminp, int *jminp));
static int dist1 _ANSI_ARGS_((unsigned char *blk1, unsigned char *blk2,
int lx, int hx, int hy, int h, int distlim));
static int dist2 _ANSI_ARGS_((unsigned char *blk1, unsigned char *blk2,
int lx, int hx, int hy, int h));
static int bdist1 _ANSI_ARGS_((unsigned char *pf, unsigned char *pb,
unsigned char *p2, int lx, int hxf, int hyf, int hxb, int hyb, int h));
static int bdist2 _ANSI_ARGS_((unsigned char *pf, unsigned char *pb,
unsigned char *p2, int lx, int hxf, int hyf, int hxb, int hyb, int h));
static int variance _ANSI_ARGS_((unsigned char *p, int lx));
/*
* motion estimation for progressive and interlaced frame pictures
*
* oldorg: source frame for forward prediction (used for P and B frames)
* neworg: source 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 frame (the one for which the prediction is formed)
* sxf,syf: forward search window (frame coordinates)
* sxb,syb: backward search window (frame coordinates)
* mbi: pointer to macroblock info structure
*
* results:
* mbi->
* mb_type: 0, MB_INTRA, MB_FORWARD, MB_BACKWARD, MB_FORWARD|MB_BACKWARD
* MV[][][]: motion vectors (frame format)
* mv_field_sel: top/bottom field (for field prediction)
* motion_type: MC_FRAME, MC_FIELD
*
* uses global vars: pict_type, frame_pred_dct
*/
void motion_estimation(oldorg,neworg,oldref,newref,cur,curref,
sxf,syf,sxb,syb,mbi,secondfield,ipflag)
unsigned char *oldorg,*neworg,*oldref,*newref,*cur,*curref;
int sxf,syf,sxb,syb;
struct mbinfo *mbi;
int secondfield,ipflag;
{
int i, j;
/* loop through all macroblocks of the picture */
for (j=0; j<height2; j+=16)
{
for (i=0; i<width; i+=16)
{
if (pict_struct==FRAME_PICTURE)
frame_ME(oldorg,neworg,oldref,newref,cur,i,j,sxf,syf,sxb,syb,mbi);
else
field_ME(oldorg,neworg,oldref,newref,cur,curref,i,j,sxf,syf,sxb,syb,
mbi,secondfield,ipflag);
mbi++;
}
if (!quiet)
{
putc('.',stderr);
fflush(stderr);
}
}
if (!quiet)
putc('\n',stderr);
}
static void frame_ME(oldorg,neworg,oldref,newref,cur,i,j,sxf,syf,sxb,syb,mbi)
unsigned char *oldorg,*neworg,*oldref,*newref,*cur;
int i,j,sxf,syf,sxb,syb;
struct mbinfo *mbi;
{
int imin,jmin,iminf,jminf,iminr,jminr;
int imint,jmint,iminb,jminb;
int imintf,jmintf,iminbf,jminbf;
int imintr,jmintr,iminbr,jminbr;
int var,v0;
int dmc,dmcf,dmcr,dmci,vmc,vmcf,vmcr,vmci;
int dmcfield,dmcfieldf,dmcfieldr,dmcfieldi;
int tsel,bsel,tself,bself,tselr,bselr;
unsigned char *mb;
int imins[2][2],jmins[2][2];
int imindp,jmindp,imindmv,jmindmv,dmc_dp,vmc_dp;
mb = cur + i + width*j;
var = variance(mb,width);
if (pict_type==I_TYPE)
mbi->mb_type = MB_INTRA;
else if (pict_type==P_TYPE)
{
if (frame_pred_dct)
{
dmc = fullsearch(oldorg,oldref,mb,
width,i,j,sxf,syf,16,width,height,&imin,&jmin);
vmc = dist2(oldref+(imin>>1)+width*(jmin>>1),mb,
width,imin&1,jmin&1,16);
mbi->motion_type = MC_FRAME;
}
else
{
frame_estimate(oldorg,oldref,mb,i,j,sxf,syf,
&imin,&jmin,&imint,&jmint,&iminb,&jminb,
&dmc,&dmcfield,&tsel,&bsel,imins,jmins);
if (M==1)
dpframe_estimate(oldref,mb,i,j>>1,imins,jmins,
&imindp,&jmindp,&imindmv,&jmindmv,&dmc_dp,&vmc_dp);
/* select between dual prime, frame and field prediction */
if (M==1 && dmc_dp<dmc && dmc_dp<dmcfield)
{
mbi->motion_type = MC_DMV;
dmc = dmc_dp;
vmc = vmc_dp;
}
else if (dmc<=dmcfield)
{
mbi->motion_type = MC_FRAME;
vmc = dist2(oldref+(imin>>1)+width*(jmin>>1),mb,
width,imin&1,jmin&1,16);
}
else
{
mbi->motion_type = MC_FIELD;
dmc = dmcfield;
vmc = dist2(oldref+(tsel?width:0)+(imint>>1)+(width<<1)*(jmint>>1),
mb,width<<1,imint&1,jmint&1,8);
vmc+= dist2(oldref+(bsel?width:0)+(iminb>>1)+(width<<1)*(jminb>>1),
mb+width,width<<1,iminb&1,jminb&1,8);
}
}
/* select between intra or non-intra coding:
*
* selection is based on intra block variance (var) vs.
* 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
{
/* select between MC / No-MC
*
* use No-MC if var(No-MC) <= 1.25*var(MC)
* (i.e slightly biased towards No-MC)
*
* blocks with small prediction error are always coded as No-MC
* (requires no motion vectors, allows skipping)
*/
v0 = dist2(oldref+i+width*j,mb,width,0,0,16);
if (4*v0>5*vmc && v0>=9*256)
{
/* use MC */
var = vmc;
mbi->mb_type = MB_FORWARD;
if (mbi->motion_type==MC_FRAME)
{
mbi->MV[0][0][0] = imin - (i<<1);
mbi->MV[0][0][1] = jmin - (j<<1);
}
else if (mbi->motion_type==MC_DMV)
{
/* these are FRAME vectors */
/* same parity vector */
mbi->MV[0][0][0] = imindp - (i<<1);
mbi->MV[0][0][1] = (jmindp<<1) - (j<<1);
/* opposite parity vector */
mbi->dmvector[0] = imindmv;
mbi->dmvector[1] = jmindmv;
}
else
{
/* these are FRAME vectors */
mbi->MV[0][0][0] = imint - (i<<1);
mbi->MV[0][0][1] = (jmint<<1) - (j<<1);
mbi->MV[1][0][0] = iminb - (i<<1);
mbi->MV[1][0][1] = (jminb<<1) - (j<<1);
mbi->mv_field_sel[0][0] = tsel;
mbi->mv_field_sel[1][0] = bsel;
}
}
else
{
/* No-MC */
var = v0;
mbi->mb_type = 0;
mbi->motion_type = MC_FRAME;
mbi->MV[0][0][0] = 0;
mbi->MV[0][0][1] = 0;
}
}
}
else /* if (pict_type==B_TYPE) */
{
if (frame_pred_dct)
{
/* forward */
dmcf = fullsearch(oldorg,oldref,mb,
width,i,j,sxf,syf,16,width,height,&iminf,&jminf);
vmcf = dist2(oldref+(iminf>>1)+width*(jminf>>1),mb,
width,iminf&1,jminf&1,16);
/* backward */
dmcr = fullsearch(neworg,newref,mb,
width,i,j,sxb,syb,16,width,height,&iminr,&jminr);
vmcr = dist2(newref+(iminr>>1)+width*(jminr>>1),mb,
width,iminr&1,jminr&1,16);
/* interpolated (bidirectional) */
vmci = bdist2(oldref+(iminf>>1)+width*(jminf>>1),
newref+(iminr>>1)+width*(jminr>>1),
mb,width,iminf&1,jminf&1,iminr&1,jminr&1,16);
/* decisions */
/* select between forward/backward/interpolated prediction:
* use the one with smallest mean sqaured prediction error
*/
if (vmcf<=vmcr && vmcf<=vmci)
{
vmc = vmcf;
mbi->mb_type = MB_FORWARD;
}
else if (vmcr<=vmci)
{
vmc = vmcr;
mbi->mb_type = MB_BACKWARD;
}
else
{
vmc = vmci;
mbi->mb_type = MB_FORWARD|MB_BACKWARD;
}
mbi->motion_type = MC_FRAME;
}
else
{
/* forward prediction */
frame_estimate(oldorg,oldref,mb,i,j,sxf,syf,
&iminf,&jminf,&imintf,&jmintf,&iminbf,&jminbf,
&dmcf,&dmcfieldf,&tself,&bself,imins,jmins);
/* backward prediction */
frame_estimate(neworg,newref,mb,i,j,sxb,syb,
&iminr,&jminr,&imintr,&jmintr,&iminbr,&jminbr,
&dmcr,&dmcfieldr,&tselr,&bselr,imins,jmins);
/* calculate interpolated distance */
/* frame */
dmci = bdist1(oldref+(iminf>>1)+width*(jminf>>1),
newref+(iminr>>1)+width*(jminr>>1),
mb,width,iminf&1,jminf&1,iminr&1,jminr&1,16);
/* top field */
dmcfieldi = bdist1(
oldref+(imintf>>1)+(tself?width:0)+(width<<1)*(jmintf>>1),
newref+(imintr>>1)+(tselr?width:0)+(width<<1)*(jmintr>>1),
mb,width<<1,imintf&1,jmintf&1,imintr&1,jmintr&1,8);
/* bottom field */
dmcfieldi+= bdist1(
oldref+(iminbf>>1)+(bself?width:0)+(width<<1)*(jminbf>>1),
newref+(iminbr>>1)+(bselr?width:0)+(width<<1)*(jminbr>>1),
mb+width,width<<1,iminbf&1,jminbf&1,iminbr&1,jminbr&1,8);
/* select prediction type of minimum distance from the
* six candidates (field/frame * forward/backward/interpolated)
*/
if (dmci<dmcfieldi && dmci<dmcf && dmci<dmcfieldf
&& dmci<dmcr && dmci<dmcfieldr)
{
/* frame, interpolated */
mbi->mb_type = MB_FORWARD|MB_BACKWARD;
mbi->motion_type = MC_FRAME;
vmc = bdist2(oldref+(iminf>>1)+width*(jminf>>1),
newref+(iminr>>1)+width*(jminr>>1),
mb,width,iminf&1,jminf&1,iminr&1,jminr&1,16);
}
else if (dmcfieldi<dmcf && dmcfieldi<dmcfieldf
&& dmcfieldi<dmcr && dmcfieldi<dmcfieldr)
{
/* field, interpolated */
mbi->mb_type = MB_FORWARD|MB_BACKWARD;
mbi->motion_type = MC_FIELD;
vmc = bdist2(oldref+(imintf>>1)+(tself?width:0)+(width<<1)*(jmintf>>1),
newref+(imintr>>1)+(tselr?width:0)+(width<<1)*(jmintr>>1),
mb,width<<1,imintf&1,jmintf&1,imintr&1,jmintr&1,8);
vmc+= bdist2(oldref+(iminbf>>1)+(bself?width:0)+(width<<1)*(jminbf>>1),
newref+(iminbr>>1)+(bselr?width:0)+(width<<1)*(jminbr>>1),
mb+width,width<<1,iminbf&1,jminbf&1,iminbr&1,jminbr&1,8);
}
else if (dmcf<dmcfieldf && dmcf<dmcr && dmcf<dmcfieldr)
{
/* frame, forward */
mbi->mb_type = MB_FORWARD;
mbi->motion_type = MC_FRAME;
vmc = dist2(oldref+(iminf>>1)+width*(jminf>>1),mb,
width,iminf&1,jminf&1,16);
}
else if (dmcfieldf<dmcr && dmcfieldf<dmcfieldr)
{
/* field, forward */
mbi->mb_type = MB_FORWARD;
mbi->motion_type = MC_FIELD;
vmc = dist2(oldref+(tself?width:0)+(imintf>>1)+(width<<1)*(jmintf>>1),
mb,width<<1,imintf&1,jmintf&1,8);
vmc+= dist2(oldref+(bself?width:0)+(iminbf>>1)+(width<<1)*(jminbf>>1),
mb+width,width<<1,iminbf&1,jminbf&1,8);
}
else if (dmcr<dmcfieldr)
{
/* frame, backward */
mbi->mb_type = MB_BACKWARD;
mbi->motion_type = MC_FRAME;
vmc = dist2(newref+(iminr>>1)+width*(jminr>>1),mb,
width,iminr&1,jminr&1,16);
}
else
{
/* field, backward */
mbi->mb_type = MB_BACKWARD;
mbi->motion_type = MC_FIELD;
vmc = dist2(newref+(tselr?width:0)+(imintr>>1)+(width<<1)*(jmintr>>1),
mb,width<<1,imintr&1,jmintr&1,8);
vmc+= dist2(newref+(bselr?width:0)+(iminbr>>1)+(width<<1)*(jminbr>>1),
mb+width,width<<1,iminbr&1,jminbr&1,8);
}
}
/* select between intra or non-intra coding:
*
* selection is based on intra block variance (var) vs.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -