mot_est_comp.c
上传用户:tuheem
上传日期:2007-05-01
资源大小:21889k
文件大小:12k
- #include "vm_common_defs.h"
- #include "mom_util.h"
- #include "mot_util.h"
- #include "mot_est_mb.h"
- extern FILE *ftrace;
- static Int roundtab16[] = {0,0,0,1,1,1,1,1,1,1,1,1,1,1,2,2};
- Void MotionEstCompPicture (
- SInt *curr,
- SInt *prev,
- SInt *prev_ipol_y,
- SInt *prev_ipol_u,
- SInt *prev_ipol_v,
- Int prev_x,
- Int prev_y,
- Int vop_width,
- Int vop_height,
- Int enable_8x8_mv,
- Int edge,
- Int sr_for,
- Int f_code,
- Int rounding_type,
- Int br_x,
- Int br_y,
- Int br_width,
- Int br_height,
- SInt *curr_comp_y,
- SInt *curr_comp_u,
- SInt *curr_comp_v,
- Float *mad,
- Float *mv16_w,
- Float *mv16_h,
- Float *mv8_w,
- Float *mv8_h,
- SInt *mode16
- );
- Void GetPred_Chroma (
- Int x_curr,
- Int y_curr,
- Int dx,
- Int dy,
- SInt *prev_u,
- SInt *prev_v,
- SInt *comp_u,
- SInt *comp_v,
- Int width,
- Int width_prev,
- Int prev_x_min,
- Int prev_y_min,
- Int prev_x_max,
- Int prev_y_max,
- Int rounding_control
- );
- Void
- MotionEstimationCompensation (
- Vop *curr_vop,
- Vop *prev_rec_vop,
- Int enable_8x8_mv,
- Int edge,
- Int f_code,
- Vop *curr_comp_vop,
- Float *mad,
- Image **mot_x,
- Image **mot_y,
- Image **mode
- )
- {
- Image *pr_rec_y;
- Image *pi_y;
- Image *mode16;
- Image *mv16_w;
- Image *mv16_h;
- Image *mv8_w;
- Image *mv8_h;
- SInt *prev_ipol_y,
- *prev_orig_y;
- Int vop_width, vop_height;
- Int br_x;
- Int br_y;
- Int br_height;
- Int br_width;
- Int mv_h, mv_w;
-
- br_y=curr_vop->ver_spat_ref;
- br_x=curr_vop->hor_spat_ref;
- br_height=curr_vop->height;
- br_width=curr_vop->width;
- mv_h=br_height/MB_SIZE;
- mv_w=br_width/MB_SIZE;
-
- vop_width=prev_rec_vop->width;
- vop_height=prev_rec_vop->height;
-
- pr_rec_y = prev_rec_vop->y_chan;
- prev_orig_y = (SInt*)GetImageData(pr_rec_y);
- pi_y = AllocImage (2*vop_width, 2*vop_height, SHORT_TYPE);
- InterpolateImage(pr_rec_y, pi_y, GetVopRoundingType(curr_vop));
- prev_ipol_y = (SInt*)GetImageData(pi_y);
-
- mode16=AllocImage (mv_w,mv_h,SHORT_TYPE);
- SetConstantImage (mode16,(Float)MBM_INTRA);
-
- mv16_w=AllocImage (mv_w*2,mv_h*2,FLOAT_TYPE);
- mv16_h=AllocImage (mv_w*2,mv_h*2,FLOAT_TYPE);
- mv8_w =AllocImage (mv_w*2,mv_h*2,FLOAT_TYPE);
- mv8_h =AllocImage (mv_w*2,mv_h*2,FLOAT_TYPE);
- SetConstantImage (mv16_h,+0.0);
- SetConstantImage (mv16_w,+0.0);
- SetConstantImage (mv8_h,+0.0);
- SetConstantImage (mv8_w,+0.0);
- SetConstantImage (curr_comp_vop->u_chan, 0);
- SetConstantImage (curr_comp_vop->v_chan, 0);
-
- MotionEstCompPicture(
- (SInt *)GetImageData(GetVopY(curr_vop)),
- prev_orig_y,
- prev_ipol_y,
- (SInt*)GetImageData(prev_rec_vop->u_chan) + (vop_width/2) * (16/2) + (16/2),
- (SInt*)GetImageData(prev_rec_vop->v_chan) + (vop_width/2) * (16/2) + (16/2),
- prev_rec_vop->hor_spat_ref,
- prev_rec_vop->ver_spat_ref,
- vop_width,vop_height,
- enable_8x8_mv,
- edge,
- GetVopSearchRangeFor(curr_vop),
- f_code,
- GetVopRoundingType(curr_vop),
- br_x,br_y,
- br_width,br_height,
- (SInt*)GetImageData(curr_comp_vop->y_chan),
- (SInt*)GetImageData(curr_comp_vop->u_chan),
- (SInt*)GetImageData(curr_comp_vop->v_chan),
- mad,
- (Float*)GetImageData(mv16_w),
- (Float*)GetImageData(mv16_h),
- (Float*)GetImageData(mv8_w),
- (Float*)GetImageData(mv8_h),
- (SInt*) GetImageData(mode16)
- );
-
- GetMotionImages(mv16_w, mv16_h, mv8_w, mv8_h, mode16, mot_x, mot_y, mode);
-
- FreeImage(mv16_w); FreeImage(mv16_h);
- FreeImage(mv8_w); FreeImage(mv8_h);
- FreeImage(mode16);
- FreeImage(pi_y);
- }
- Void
- MotionEstCompPicture(
- SInt *curr,
- SInt *prev,
- SInt *prev_ipol,
- SInt *prev_u,
- SInt *prev_v,
- Int prev_x,
- Int prev_y,
- Int vop_width,
- Int vop_height,
- Int enable_8x8_mv,
- Int edge,
- Int sr_for,
- Int f_code, /* <-- MV search range 1/2 or 1/4 pel: 1=32,2=64,...,7=2048*/
- Int rounding_type,
- Int br_x,
- Int br_y,
- Int br_width,
- Int br_height,
- SInt *curr_comp_y,
- SInt *curr_comp_u,
- SInt *curr_comp_v,
- Float *mad,
- Float *mv16_w,
- Float *mv16_h,
- Float *mv8_w,
- Float *mv8_h,
- SInt *mode16
- )
- {
- Int i, j, k;
- SInt curr_mb[MB_SIZE][MB_SIZE];
- SInt curr_comp_mb_16[MB_SIZE][MB_SIZE];
- SInt curr_comp_mb_8[MB_SIZE][MB_SIZE];
- Int sad8 = MV_MAX_ERROR, sad16, sad;
- Int imas_w, imas_h, Mode;
- Int posmode, pos16, pos8;
- Int min_error16,
- min_error8_0, min_error8_1, min_error8_2, min_error8_3;
-
- SInt *halfpelflags;
- Float hint_mv_w, hint_mv_h;
- Int xsum,ysum,dx,dy;
- Int prev_x_min,prev_x_max,prev_y_min,prev_y_max;
- prev_x_min = 2 * prev_x + 2 * 16;
- prev_y_min = 2 * prev_y + 2 * 16;
- prev_x_max = prev_x_min + 2 * vop_width - 4 * 16;
- prev_y_max = prev_y_min + 2 * vop_height - 4 * 16;
- imas_w=br_width/MB_SIZE;
- imas_h=br_height/MB_SIZE;
-
- halfpelflags=(SInt*)malloc(5*4*sizeof(SInt));
-
- sad = 0;
- for ( j=0; j< (br_height/MB_SIZE); j++)
- {
- hint_mv_w = hint_mv_h = 0.f;
- for ( i=0; i< (br_width/MB_SIZE); i++)
- {
-
- Int min_error;
- posmode = j * imas_w + i;
- pos16 = pos8 = 2*j*2*imas_w + 2*i;
- MBMotionEstimation(curr,
- prev, br_x, br_y,
- br_width, i, j, prev_x, prev_y,
- vop_width, vop_height, enable_8x8_mv, edge,
- f_code, sr_for,
- hint_mv_w, hint_mv_h,
- mv16_w, mv16_h,
- mv8_w, mv8_h, &min_error, halfpelflags);
-
- Mode = ChooseMode(curr,
- i*MB_SIZE,j*MB_SIZE, min_error, br_width);
- hint_mv_w = mv16_w[pos16];
- hint_mv_h = mv16_h[pos16];
- LoadArea(curr, i*MB_SIZE, j*MB_SIZE, 16, 16, br_width, (SInt *)curr_mb);
-
- if ( Mode != 0)
- {
- FindSubPel (i*MB_SIZE,j*MB_SIZE, prev_ipol,
- &curr_mb[0][0], 16, 16 , 0,
- br_x-prev_x,br_y-prev_y,vop_width, vop_height,
- edge, halfpelflags, &curr_comp_mb_16[0][0],
- &mv16_w[pos16], &mv16_h[pos16], &min_error16);
-
- sad16 = min_error16;
- mode16[posmode] = MBM_INTER16;
- if (enable_8x8_mv)
- {
- xsum = 0; ysum = 0;
- FindSubPel(i*MB_SIZE, j*MB_SIZE, prev_ipol,
- &curr_mb[0][0], 8, 8 , 0,
- br_x-prev_x,br_y-prev_y, vop_width, vop_height,
- edge, halfpelflags, &curr_comp_mb_8[0][0],
- &mv8_w[pos8], &mv8_h[pos8], &min_error8_0);
- xsum += (Int)(2*(mv8_w[pos8]));
- ysum += (Int)(2*(mv8_h[pos8]));
- FindSubPel(i*MB_SIZE, j*MB_SIZE, prev_ipol,
- &curr_mb[0][8], 8, 8 , 1,
- br_x-prev_x,br_y-prev_y, vop_width,vop_height,
- edge, halfpelflags, &curr_comp_mb_8[0][8],
- &mv8_w[pos8+1], &mv8_h[pos8+1], &min_error8_1);
- xsum += (Int)(2*(mv8_w[pos8+1]));
- ysum += (Int)(2*(mv8_h[pos8+1]));
- pos8+=2*imas_w;
- FindSubPel(i*MB_SIZE, j*MB_SIZE, prev_ipol,
- &curr_mb[8][0], 8, 8 , 2,
- br_x-prev_x,br_y-prev_y, vop_width,vop_height,
- edge, halfpelflags, &curr_comp_mb_8[8][0],
- &mv8_w[pos8], &mv8_h[pos8], &min_error8_2);
- xsum += (Int)(2*(mv8_w[pos8]));
- ysum += (Int)(2*(mv8_h[pos8]));
- FindSubPel(i*MB_SIZE, j*MB_SIZE, prev_ipol,
- &curr_mb[8][8], 8, 8 , 3,
- br_x-prev_x,br_y-prev_y, vop_width,vop_height,
- edge, halfpelflags, &curr_comp_mb_8[8][8],
- &mv8_w[pos8+1], &mv8_h[pos8+1], &min_error8_3);
- xsum += (Int)(2*(mv8_w[pos8+1]));
- ysum += (Int)(2*(mv8_h[pos8+1]));
- sad8 = min_error8_0+min_error8_1+min_error8_2+min_error8_3;
-
- if (sad8 < (sad16 -(128+1)))
- mode16[posmode] = MBM_INTER8;
- }
-
-
- if ((mv16_w[pos16]==0.0) && (mv16_h[pos16]==0.0) && (mode16[posmode]==MBM_INTER16))
- sad16 += 128+1;
-
- if(mode16[posmode] == MBM_INTER8)
- {
- dx = SIGN (xsum) * (roundtab16[ABS (xsum) % 16] + (ABS (xsum) / 16) * 2);
- dy = SIGN (ysum) * (roundtab16[ABS (ysum) % 16] + (ABS (ysum) / 16) * 2);
- sad += sad8;
- }
- else
- {
- dx = (Int)(2 * mv16_w[pos16]);
- dy = (Int)(2 * mv16_h[pos16]);
- dx = ( dx % 4 == 0 ? dx >> 1 : (dx>>1)|1 );
- dy = ( dy % 4 == 0 ? dy >> 1 : (dy>>1)|1 );
- sad += sad16;
- }
- GetPred_Chroma (i*MB_SIZE, j*MB_SIZE, dx, dy,
- prev_u, prev_v, curr_comp_u, curr_comp_v,
- br_width, vop_width,
- prev_x_min/4,prev_y_min/4,prev_x_max/4,prev_y_max/4, rounding_type);
- }
- else
- {
- mode16[posmode] = MBM_INTRA;
- for (k = 0; k < MB_SIZE*MB_SIZE; k++)
- {
-
- curr_comp_mb_16[k/MB_SIZE][k%MB_SIZE] = 0;
-
- sad += curr_mb[k/MB_SIZE][k%MB_SIZE];
- }
- }
- if (mode16[posmode] == MBM_INTER8)
- SetArea((SInt*)curr_comp_mb_8, i*MB_SIZE, j*MB_SIZE, 16, 16, br_width, curr_comp_y);
- else
- SetArea((SInt*)curr_comp_mb_16, i*MB_SIZE, j*MB_SIZE, 16, 16, br_width, curr_comp_y);
- }
- }
- *mad = (float)sad/(br_width*br_height);
- free((Char*)halfpelflags);
- return;
- }
- #define unrestricted_MC_chro(x,y,npix,prev_x_min,prev_y_min,prev_x_max,prev_y_max) ((x)+(y)*(npix))
- Void GetPred_Chroma (
- Int x_curr,
- Int y_curr,
- Int dx,
- Int dy,
- SInt *prev_u,
- SInt *prev_v,
- SInt *comp_u,
- SInt *comp_v,
- Int width,
- Int width_prev,
- Int prev_x_min,
- Int prev_y_min,
- Int prev_x_max,
- Int prev_y_max,
- Int rounding_control)
- {
- Int m,n;
- Int x, y, ofx, ofy, lx;
- Int xint, yint;
- Int xh, yh;
- Int index1,index2,index3,index4;
- lx = width_prev/2;
- x = x_curr>>1;
- y = y_curr>>1;
- xint = dx>>1;
- xh = dx & 1;
- yint = dy>>1;
- yh = dy & 1;
- if (!xh && !yh)
- {
- for (n = 0; n < 8; n++)
- {
- for (m = 0; m < 8; m++)
- {
- ofx = x + xint + m;
- ofy = y + yint + n;
- index1 = unrestricted_MC_chro(ofx,ofy,lx,prev_x_min,
- prev_y_min,prev_x_max,prev_y_max);
- comp_u[(y+n)*width/2+x+m]
- = *(prev_u+index1);
- comp_v[(y+n)*width/2+x+m]
- = *(prev_v+index1);
- }
- }
- }
- else if (!xh && yh)
- {
- for (n = 0; n < 8; n++)
- {
- for (m = 0; m < 8; m++)
- {
- ofx = x + xint + m;
- ofy = y + yint + n;
- index1 = unrestricted_MC_chro(ofx,ofy,lx,prev_x_min,
- prev_y_min,prev_x_max,prev_y_max);
- index2 = unrestricted_MC_chro(ofx,ofy+yh,lx,prev_x_min,
- prev_y_min,prev_x_max,prev_y_max);
- comp_u[(y+n)*width/2+x+m]
- = (*(prev_u+index1) +
- *(prev_u+index2) + 1- rounding_control)>>1;
- comp_v[(y+n)*width/2+x+m]
- = (*(prev_v+index1) +
- *(prev_v+index2) + 1- rounding_control)>>1;
- }
- }
- }
- else if (xh && !yh)
- {
- for (n = 0; n < 8; n++)
- {
- for (m = 0; m < 8; m++)
- {
- ofx = x + xint + m;
- ofy = y + yint + n;
- index1 = unrestricted_MC_chro(ofx,ofy,lx,prev_x_min,
- prev_y_min,prev_x_max,prev_y_max);
- index2 = unrestricted_MC_chro(ofx+xh,ofy,lx,prev_x_min,
- prev_y_min,prev_x_max,prev_y_max);
- comp_u[(y+n)*width/2+x+m]
- = (*(prev_u+index1) +
- *(prev_u+index2) + 1- rounding_control)>>1;
- comp_v[(y+n)*width/2+x+m]
- = (*(prev_v+index1) +
- *(prev_v+index2) + 1- rounding_control)>>1;
- }
- }
- }
- else
- {
- for (n = 0; n < 8; n++)
- {
- for (m = 0; m < 8; m++)
- {
- ofx = x + xint + m;
- ofy = y + yint + n;
- index1 = unrestricted_MC_chro(ofx,ofy,lx,prev_x_min,
- prev_y_min,prev_x_max,prev_y_max);
- index2 = unrestricted_MC_chro(ofx+xh,ofy,lx,prev_x_min,
- prev_y_min,prev_x_max,prev_y_max);
- index3 = unrestricted_MC_chro(ofx,ofy+yh,lx,prev_x_min,
- prev_y_min,prev_x_max,prev_y_max);
- index4 = unrestricted_MC_chro(ofx+xh,ofy+yh,lx,prev_x_min,
- prev_y_min,prev_x_max,prev_y_max);
- comp_u[(y+n)*width/2+x+m]
- = (*(prev_u+index1)+
- *(prev_u+index2)+
- *(prev_u+index3)+
- *(prev_u+index4)+
- 2- rounding_control)>>2;
- comp_v[(y+n)*width/2+x+m]
- = (*(prev_v+index1)+
- *(prev_v+index2)+
- *(prev_v+index3)+
- *(prev_v+index4)+
- 2- rounding_control)>>2;
- }
- }
- }
- return;
- }