/* 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 mot