📄 dsp_mp4svp_mot.c
字号:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dsp_mp4svp_type.h"
#include "dsp_mp4svp_global.h"
#include "dsp_mp4svp_stream.h"
#include "dsp_mp4svp_vop.h"
//#include "dsp_mp4svp_dct.h"
#include "dsp_mp4svp_vlc.h"
Void
MotionEstimation (
unsigned char *curr_vop, /* <-- current Vop (for luminance) */
unsigned char *prev_rec_vop, /* <-- reference Vop (reconstructed)(1/2 pixel) */
//Int enable_8x8_mv, /* <-- 8x8 MV (=1) or only 16x16 MV (=0) */
//Int edge, /* <-- restricted(==0)/unrestricted(==edge) mode */
//Int f_code, /* <-- MV search range 1/2 pel: 1=32,2=64,...,7=2048*/
//unsigned char *curr_comp_vop, /* <-> motion compensated current VOP */
Int *mad, /* <-> mad value of the ME/MC result */
char *motx, /* --> horizontal MV coordinates */
char *moty, /* --> vertical MV coordinates */
char *MBmode /* --> modes for each MB */
)
{
short i, j, MBnumX,MBnumY;
Int sad=0;
*mad = 0;
MBnumX = (p_par->width)/MB_SIZE;
MBnumY = (p_par->height)/MB_SIZE;
for ( j=0; j< MBnumY; j++)
{
for ( i=0; i< MBnumX; i++)
{
loadUCharMBdata(curr_vop,i,j,p_par->width,p_par->height, g_currMB);//g_currMB now is the source MB
sad+=MBMotionEstimation( prev_rec_vop,i, j, p_par->hor_spat_ref, p_par->ver_spat_ref,
p_par->width + 32, p_par->height + 32, p_par->sr_for,
motx,moty,MBmode);
} /* end of i loop */
} /* end of j loop */
*mad = sad/(p_par->width * p_par->height);
}
void Obtain_Range(
short sr, /* <-- Serach range (radius) */
char type, /* <-- MBM_INTER16==16x16 search; MBM_INTER8==8x8 search */
short pmv_x, /* <-- predicted horizontal motion vector */
short pmv_y, /* <-- predicted horizontal motion vector */
short *mv_x_min, /* --> min horizontal range */
short *mv_x_max, /* --> max horizontal range */
short *mv_y_min, /* --> min vertical range */
short *mv_y_max /* --> max vertical range */
)
{
short aux_x_min, aux_y_min,aux_x_max, aux_y_max;
short range;
range = sr;
*mv_x_min=-range; *mv_x_max= range - 1;
*mv_y_min=-range; *mv_y_max= range - 1;
if (type==MBM_INTER8)
{
aux_x_min=pmv_x - DEFAULT_8_WIN;
aux_y_min=pmv_y - DEFAULT_8_WIN;
aux_x_max=pmv_x + DEFAULT_8_WIN;
aux_y_max=pmv_y + DEFAULT_8_WIN;
if(*mv_x_min < aux_x_min)
*mv_x_min = aux_x_min;
if(*mv_y_min < aux_y_min)
*mv_y_min = aux_y_min;
if(*mv_x_max > aux_x_max)
*mv_x_max = aux_x_max;
if(*mv_y_max > aux_y_max)
*mv_y_max = aux_y_max;
}
}
Void
RangeInSearchArea(
char i, /* <-- horizontal MBcoordinate in pixels */
char j, /* <-- vertical MB coordinate in pixels */
char block, /* <-- block position (0 16x16; 1-2-3-4 8x8) */
char prev_x, /* <-- absolute horizontal position of the previous vop*/
char prev_y, /* <-- absolute vertical position of the previous vop */
short vop_width, /* <-- horizontal vop dimension */
short vop_height, /* <-- vertical vop dimension */
// Int br_x, /* <-- absolute horizontal position of the current vop */
// Int br_y, /* <-- absolute vertical position of the current vop */
// Int edge, /* <-- edge arround the reference vop */
// Int f_code, /* <- MV search range 1/2 (or 1/4) pel: (0=16,) 1=32,2=64,...,7=2048 */
short *mv_x_min, /* <-- min horizontal range */
short *mv_x_max, /* <-- max horizontal range */
short *mv_y_min, /* <-- min vertical range */
short *mv_y_max, /* <-- max vertical range */
char *out /* --> the search area does not exist (the reference */ /* and current BB does not overlap) */
)
{
short dim_curr_x_max,
dim_curr_y_max,
dim_curr_x_min,
dim_curr_y_min;
short dim_prev_x_max,
dim_prev_y_max,
dim_prev_x_min,
dim_prev_y_min;
char mb_b_size,
block_x,
block_y;
*out=0;
switch (block)
{
case 0: /* 8x8 or 16x16 block search */
block_x=0; /*****************************/
block_y=0; /** 1 2 ******** 0 *********/
mb_b_size=MB_SIZE; /** 3 4 ******** *********/
break; /*****************************/
case 1:
block_x=0;
block_y=0;
mb_b_size=B_SIZE;
break;
case 2:
block_x=B_SIZE;
block_y=0;
mb_b_size=B_SIZE;
break;
case 3:
block_x=0;
block_y=B_SIZE;
mb_b_size=B_SIZE;
break;
case 4:
block_x=B_SIZE;
block_y=B_SIZE;
mb_b_size=B_SIZE;
break;
default:
return;
}
/* min x/y */
dim_curr_x_min=(short)(i*MB_SIZE+*mv_x_min+block_x);
dim_curr_y_min=(short)(j*MB_SIZE+*mv_y_min+block_y);
dim_prev_x_min=prev_x/*-edge*/;
dim_prev_y_min=prev_y/*-edge*/;
/* max x/y */
/*the MB right-pixels inside */
dim_curr_x_max=(short)(i*MB_SIZE+*mv_x_max+mb_b_size+block_x);
/*the MB bottom-pixels inside */
dim_curr_y_max=(Int)(j*MB_SIZE+*mv_y_max+mb_b_size+block_y);
dim_prev_x_max=prev_x+vop_width /*+edge*/;
dim_prev_y_max=prev_y+vop_height/*+edge*/;
/* range x/y min */
if (dim_curr_x_min > dim_prev_x_max)
{
*out=1;
}
else if(dim_curr_x_min < dim_prev_x_min)
{
*mv_x_min = *mv_x_min + ( dim_prev_x_min - dim_curr_x_min ) ;
}
if(!(*out))
{
if (dim_curr_y_min > dim_prev_y_max)
{
*out=1;
}
else if(dim_curr_y_min < dim_prev_y_min)
{
*mv_y_min = *mv_y_min + ( dim_prev_y_min - dim_curr_y_min ) ;
}
}
/* range x/y max */
if(!(*out))
{
if(dim_curr_x_max < dim_prev_x_min)
{
*out=1;
}
if ((!(*out))&&(dim_curr_x_max > dim_prev_x_max))
{
*mv_x_max = *mv_x_max - ( dim_curr_x_max - dim_prev_x_max) ;
}
}
if(!(*out))
{
if(dim_curr_y_max < dim_prev_y_min)
{
*out=1; /* already set */
}
if ((!(*out))&&(dim_curr_y_max > dim_prev_y_max))
{
*mv_y_max = *mv_y_max - ( dim_curr_y_max - dim_prev_y_max) ;
}
}
if(*mv_x_min>*mv_x_max)
{
*out=1;
}
if ( (!(*out)) && (*mv_y_min>*mv_y_max))
{
*out=1;
}
return;
}
static Int P_diff;
#define DIFF1(v1,v2,idx) (P_diff = (v1[idx]-v2[idx]), ABS(P_diff))
Int
SAD_Macroblock(
unsigned char *ii, /* <-- Pointer to the upper-left pel of first MB */
unsigned char *act_block, /* <-- Id, second MB (width=16) */
short h_length, /* <-- Width of first area */
Int Min_FRAME /* <-- Minimum prediction error so far */
)
{
Int i;
Int sad = 0;
unsigned char *kk;
register Int P_diff;
kk = act_block;
i = 16;
while (i--)
{
sad += (DIFF1(ii,kk,0)+DIFF1(ii,kk,1)
+DIFF1(ii,kk,2)+DIFF1(ii,kk,3)
+DIFF1(ii,kk,4)+DIFF1(ii,kk,5)
+DIFF1(ii,kk,6)+DIFF1(ii,kk,7)
+DIFF1(ii,kk,8)+DIFF1(ii,kk,9)
+DIFF1(ii,kk,10)+DIFF1(ii,kk,11)
+DIFF1(ii,kk,12)+DIFF1(ii,kk,13)
+DIFF1(ii,kk,14)+DIFF1(ii,kk,15)
);
ii += h_length;
kk += 16;
if (sad > Min_FRAME)
return MV_MAX_ERROR;
}
return sad;
}
Int
SAD_Block(
unsigned char *ii, /* <-- First area */
unsigned char *act_block, /* <-- Id. second MB (width=16) */
short h_length, /* <-- Width of first area */
Int min_sofar /* <-- Minimum prediction error so far */
)
{
Int i;
Int sad = 0;
unsigned char *kk;
register Int P_diff;
kk = act_block;
i = 8;
while (i--)
{
sad += (DIFF1(ii,kk,0)+DIFF1(ii,kk,1)
+DIFF1(ii,kk,2)+DIFF1(ii,kk,3)
+DIFF1(ii,kk,4)+DIFF1(ii,kk,5)
+DIFF1(ii,kk,6)+DIFF1(ii,kk,7)
);
ii += h_length;
kk += 16;
if (sad > min_sofar)
return INT_MAX;
}
return sad;
}
char
TryIntraMode(unsigned char *curr,Int min_SAD)
{
short j;
Int MB_mean = 0;
Int A = 0;
for (j = 0; j < 256; j++)
{
MB_mean += *(curr + j );
}
MB_mean /= 256;
for (j = 0; j < 256; j++)
{
A += ABS( *(curr + j) - MB_mean );
}
if (A < (min_SAD - 2*256))
return 1;
else
return 0;
}
Int
MBMotionEstimation(
unsigned char * prev,
char i, /* <-- horizontal MBcoordinate in pixels */
char j, /* <-- vertical MB coordinate in pixels */
char prev_x, /* <-- absolute horiz. position of previous vop */
char prev_y, /* <-- absolute verti. position of previous vop */
short vop_width, //reference vop's width ,including the edge 16
short vop_height, //reference vop's height,including the edge 16
short sr, /* <-- search range (corresponds to f_code) UB 990215*/
char *motx,// short is better,if use char ,then the mv sr should be <64
char *moty,// short is better,if use char ,then the mv sr should be <64
char *MB_md
)
{ char enable_8x8_mv=1;
char mv16_x,mv16_y;//ldm
char mv8_x[4],mv8_y[4];//ldm
char MBmode=MBM_INTRA;
short x, y,k;
Int sad, sad_min=MV_MAX_ERROR;
short mv_x, mv_y;// short is better,if use char ,then the mv sr should be <64
short mv_x_min, mv_x_max, mv_y_min, mv_y_max;//Float
short mvm_width,posBlock0,posBlock1,posBlock2,posBlock3;
short x_curr = i*MB_SIZE, y_curr = j*MB_SIZE;
char hb,vb,block,out;
char rel_ori_x;
char rel_ori_y;
Int min_error16, min_error_intra=0, min_error8 = 0, min_error =0 ;
#ifndef _FULL_SEARCH_
typedef struct
{
char x;
char y;
char start_nmbr;
} DPoint;
typedef struct
{
DPoint point[8];
} Diamond;
char d_type=1,stop_flag=0,pt_nmbr=0,check_pts,total_check_pts=8,mot_dirn=0;
short d_centre_x=0,d_centre_y=0,check_pt_x,check_pt_y;
Diamond diamond[2]=
{
{
{ {0,1,0}, {1,0,0}, {0,-1,0}, {-1,0,0} }
}
,
{
{ {0,2,6}, {1,1,0}, {2,0,0}, {1,-1,2},
{0,-2,2}, {-1,-1,4}, {-2,0,4}, {-1,1,6} }
}
};
#endif
rel_ori_x= 0 - prev_x;
rel_ori_y= 0 - prev_y;
Obtain_Range ( sr, MBM_INTER16, 0, 0, &mv_x_min, &mv_x_max, &mv_y_min, &mv_y_max);
RangeInSearchArea (i,j,0, prev_x, prev_y, vop_width, vop_height,
&mv_x_min, &mv_x_max,&mv_y_min, &mv_y_max,&out);
/* Compute */
if(!out)
{
sad_min=MV_MAX_ERROR;
mv_x = mv_y = 2000; /* A very large MV */
#ifdef _FULL_SEARCH_
for (y= mv_y_min; y<= mv_y_max; y++)
for (x= mv_x_min; x<= mv_x_max; x++)
{
if (x==0 && y==0)
sad=SAD_Macroblock(prev+INDEX_BIG(x_curr+rel_ori_x,
y_curr+rel_ori_y), g_currMB, (vop_width/*+2*edge*/), MV_MAX_ERROR)
- (128 + 1);
else
sad=SAD_Macroblock(prev+INDEX_BIG(x_curr+x+rel_ori_x,
y_curr+y+rel_ori_y), g_currMB, (vop_width/*+2*edge*/), sad_min);
if (sad<sad_min)
{
sad_min=sad;
mv_x=x;
mv_y=y;
}
else if (sad==sad_min)
if((ABS(x)+ABS(y)) < (ABS(mv_x)+ABS(mv_y)))
{
sad_min=sad;
mv_x=x;
mv_y=y;
}
}
#else
sad = SAD_Macroblock(prev+INDEX_BIG(x_curr+rel_ori_x,
y_curr+rel_ori_y), g_currMB, (vop_width/*+2*edge*/), MV_MAX_ERROR)- (128 + 1);
if (sad<sad_min)
{
sad_min=sad;
mv_x = mv_y = 0;
}
do
{
check_pts=total_check_pts;
do
{
check_pt_x = diamond[d_type].point[pt_nmbr].x + d_centre_x;
check_pt_y = diamond[d_type].point[pt_nmbr].y + d_centre_y;
/* Restrict the search to the searching window ; Note: This constraint can be removed */
if ( check_pt_x < mv_x_min || check_pt_x > mv_x_max || check_pt_y < mv_y_min || check_pt_y > mv_y_max)
{
sad = MV_MAX_ERROR;
}
else
{
sad=SAD_Macroblock(prev+INDEX_BIG(x_curr+check_pt_x+rel_ori_x,
y_curr+check_pt_y+rel_ori_y), g_currMB,
(vop_width/*+2*edge*/), sad_min);
#ifdef _SAD_EXHAUS_
fprintf(stdout,"+o+ [%2d,%2d] sad16(%3d,%3d)=%4d\n",i,j,x,y,sad);
#endif
}
if (sad<sad_min)
{
sad_min=sad;
mv_x=check_pt_x;
mv_y=check_pt_y;
mot_dirn=pt_nmbr;
}
else if (sad==sad_min)
if((ABS(check_pt_x)+ABS(check_pt_y)) < (ABS(mv_x)+ABS(mv_y)))
{
sad_min=sad;
mv_x=check_pt_x;
mv_y=check_pt_y;
mot_dirn=pt_nmbr;
}
pt_nmbr+=1;
if((pt_nmbr)>= 8) pt_nmbr-=8;
check_pts-=1;
}
while(check_pts>0);
if( d_type == 0)
{
stop_flag = 1;
}
else
{
if( (mv_x == d_centre_x) && (mv_y == d_centre_y) )
{
d_type=0;
pt_nmbr=0;
total_check_pts = 4;
}
else
{
if((mv_x==d_centre_x) ||(mv_y==d_centre_y))
total_check_pts=5;
else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -