mot_code.c
资源名称:VC++视频传输.rar [点击查看]
上传用户:hxb_1234
上传日期:2010-03-30
资源大小:8328k
文件大小:9k
源码类别:
VC书籍
开发平台:
Visual C++
- #include "vm_common_defs.h"
- #include "bitstream.h"
- #include "putvlc.h"
- #define MBV_H(h,v) (ph[2*(v)*2*hdim+2*(h)])
- #define MBV_V(h,v) (pv[2*(v)*2*hdim+2*(h)])
- #define BV_H(h,v,h2,v2) (ph[(2*(v)+(v2))*2*hdim+2*(h)+(h2)])
- #define BV_V(h,v,h2,v2) (pv[(2*(v)+(v2))*2*hdim+2*(h)+(h2)])
- #define MB_MODE(h,v) ( ((h)<0||(h)>=hdim||(v)<0||(v)>=vdim ? MBM_OUT : (pm[(v)*hdim+(h)])) )
- #define BV(p,xdim,h,v,h2,v2) (p[(2*(v)+(v2))*(xdim)+2*(h)+(h2)])
- Int WriteMVcomponent(Int f_code, Int dmv, Image *bs);
- Void ScaleMVD (Int f_code, Int diff_vector, Int *residual, Int *vlc_code_mag);
- Void find_pmvs (Image *mot_x, Image *mot_y, Image *MB_decisions, Image *B_decisions,
- Int x, Int y, Int block, Int transparent_value, Int quarter_pel, Int *error_flag,
- Int *mvx, Int *mvy, Int newgob);
- SInt ModeMB (Image *MB_decision, Int i, Int j);
- Int
- Bits_CountMB_Motion(
- Image *mot_h,
- Image *mot_v,
- Image *alpha,
- Image *modes,
- Int h,
- Int v,
- Int f_code,
- Int quarter_pel,
- Image *bs,
- Int error_res_disable,
- Int after_marker,
- Int **slice_nb,
- Int arbitrary_shape
- )
- {
- Int vdim, hdim;
- Float *ph, *pv;
- SInt *pm;
- SInt *pa;
- Int mode;
- Int bits_mot = 0;
- Int i, error_flag=0,mvx=0,mvy=0;
- Float pred_h, pred_v;
- Float diff_h, diff_v;
- Int bh, bv;
- Int local_f_code;
- Float subdim;
- vdim = (Int)modes->y;
- hdim = (Int)modes->x;
- ph=(Float*)GetImageData(mot_h);
- pv=(Float*)GetImageData(mot_v);
- pm=(SInt*)GetImageData(modes);
- pa=NULL;
- if (quarter_pel)
- {
- local_f_code = f_code+1;
- subdim = 4.0;
- }
- else
- {
- local_f_code = f_code;
- subdim = 2.0;
- }
- switch (mode=MB_MODE(h,v))
- {
- case MBM_INTRA:
- break;
- case MBM_INTER16:
- find_pmvs(mot_h,mot_v,modes,alpha,h,v,0,MBM_TRANSPARENT,
- quarter_pel,&error_flag,&mvx,&mvy,0);
- pred_h=((Float)mvx)/subdim;
- pred_v=((Float)mvy)/subdim;
- bits_mot += WriteMVcomponent(local_f_code, (Int)(subdim*(MBV_H(h,v) - pred_h)), bs);
- bits_mot += WriteMVcomponent(local_f_code, (Int)(subdim*(MBV_V(h,v) - pred_v)), bs);
- break;
- case MBM_INTER8:
- i=1;
- for (bv=0; bv<=1; bv++)
- for (bh=0; bh<=1; bh++)
- {
- find_pmvs(mot_h,mot_v,modes,alpha,h,v,i,MBM_TRANSPARENT,
- quarter_pel,&error_flag,&mvx,&mvy,0);
- pred_h=((Float)mvx)/subdim;
- pred_v=((Float)mvy)/subdim;
- i++;
- diff_h=BV_H(h,v,bh,bv)-pred_h;
- diff_v=BV_V(h,v,bh,bv)-pred_v;
- bits_mot += WriteMVcomponent(local_f_code, (Int)(subdim*diff_h), bs);
- bits_mot += WriteMVcomponent(local_f_code, (Int)(subdim*diff_v), bs);
- }
- break;
- }
- return bits_mot;
- }
- Int
- WriteMVcomponent(
- Int f_code,
- Int dmv,
- Image *bs
- )
- {
- Int residual, vlc_code_mag, bits, entry;
- ScaleMVD(f_code, dmv, &residual, &vlc_code_mag);
- if (vlc_code_mag < 0)
- entry = vlc_code_mag + 65;
- else
- entry = vlc_code_mag;
- bits = PutMV (entry, bs);
- if ((f_code != 1) && (vlc_code_mag != 0))
- {
- BitstreamPutBits(bs, residual, f_code-1);
- bits += f_code - 1;
- }
- return(bits);
- }
- Void
- ScaleMVD (
- Int f_code,
- Int diff_vector,
- Int *residual,
- Int *vlc_code_mag
- )
- {
- Int range;
- Int scale_factor;
- Int r_size;
- Int low;
- Int high;
- Int aux;
- r_size = f_code-1;
- scale_factor = 1<<r_size;
- range = 32*scale_factor;
- low = -range;
- high = range-1;
- if (diff_vector < low)
- {
- diff_vector += 2*range;
- }
- else if (diff_vector > high)
- {
- diff_vector -= 2*range;
- }
- if (diff_vector==0)
- {
- *vlc_code_mag = 0;
- *residual = 0;
- }
- else if (scale_factor==1)
- {
- *vlc_code_mag = diff_vector;
- *residual = 0;
- }
- else
- {
- aux = ABS(diff_vector) + scale_factor - 1;
- *vlc_code_mag = aux>>r_size;
- if (diff_vector<0)
- *vlc_code_mag = -*vlc_code_mag;
- *residual = aux & (scale_factor-1);
- }
- }
- Void
- find_pmvs(
- Image *mot_x,
- Image *mot_y,
- Image *MB_decisions,
- Image *B_decisions,
- Int x,
- Int y,
- Int block,
- Int transparent_value,
- Int quarter_pel,
- Int *error_flag,
- Int *mvx,
- Int *mvy,
- Int newgob
- )
- {
- Float p1x,p2x,p3x;
- Float p1y,p2y,p3y;
- Int xin1, xin2, xin3;
- Int yin1, yin2, yin3;
- Int vec1, vec2, vec3;
- Int rule1, rule2, rule3;
- Int subdim;
- Float *motxdata = (Float *) GetImageData(mot_x);
- Float *motydata = (Float *) GetImageData(mot_y);
- Int xM = GetImageSizeX(mot_x);
- Int xB = xM;
- Int mb_mode, sum;
- if (quarter_pel)
- {
- subdim=4;
- }
- else
- {
- subdim=2;
- }
- switch (block)
- {
- case 0:
- vec1 = 1 ; yin1 = y ; xin1 = x-1;
- vec2 = 2 ; yin2 = y-1; xin2 = x;
- vec3 = 2 ; yin3 = y-1; xin3 = x+1;
- break;
- case 1:
- vec1 = 1 ; yin1 = y ; xin1 = x-1;
- vec2 = 2 ; yin2 = y-1; xin2 = x;
- vec3 = 2 ; yin3 = y-1; xin3 = x+1;
- break;
- case 2:
- vec1 = 0 ; yin1 = y ; xin1 = x;
- vec2 = 3 ; yin2 = y-1; xin2 = x;
- vec3 = 2 ; yin3 = y-1; xin3 = x+1;
- break;
- case 3:
- vec1 = 3 ; yin1 = y ; xin1 = x-1;
- vec2 = 0 ; yin2 = y ; xin2 = x;
- vec3 = 1 ; yin3 = y ; xin3 = x;
- break;
- case 4:
- vec1 = 2 ; yin1 = y ; xin1 = x;
- vec2 = 0 ; yin2 = y ; xin2 = x;
- vec3 = 1 ; yin3 = y ; xin3 = x;
- break;
- default:
- printf("Illegal block number in find_pmv (mot_decode.c)");
- *error_flag = 1;
- *mvx=*mvy=0;
- return ;
- }
- if (block==0)
- {
- if (x>0 )
- rule1 = 0;
- else
- rule1 = 1;
- if ((y>0 && newgob==0) )
- rule2 = 0;
- else
- rule2 = 1;
- if ((x != xB/2 -1) &&
- ((y>0 && newgob==0)) )
- rule3 = 0;
- else
- rule3 = 1;
- }
- else
- {
- if (((block == 1 || block == 3) && (x == 0)) )
- rule1 = 1;
- else
- rule1 = 0;
- if (((block == 1 || block == 2) && (y == 0)) )
- rule2 = 1;
- else
- rule2 = 0;
- if (((block == 1 || block == 2) && (x == xB/2 -1 || y == 0)) )
- rule3 = 1;
- else
- rule3 = 0;
- }
- if (rule1 )
- {
- p1x = p1y = 0;
- }
- else if (((mb_mode = ModeMB(MB_decisions,xin1,yin1)) >= MBM_FIELD00) && (mb_mode <= MBM_FIELD11))
- {
- sum = subdim*(BV(motxdata, xM, xin1, yin1, 0, 0) + BV(motxdata, xM, xin1, yin1, 1, 0));
- p1x = (Float)((sum & 3) ? ((sum | 2) >> 1) : (sum >> 1))/(Float)subdim;
- sum = subdim*(BV(motydata, xM, xin1, yin1, 0, 0) + BV(motydata, xM, xin1, yin1, 1, 0));
- p1y = (Float)((sum & 3) ? ((sum | 2) >> 1) : (sum >> 1))/(Float)subdim;
- }
- else
- {
- p1x = BV(motxdata, xM, xin1, yin1, vec1&0x1, vec1>>1 );
- p1y = BV(motydata, xM, xin1, yin1, vec1&0x1, vec1>>1 );
- }
- if (rule2)
- {
- p2x = p2y = 0 ;
- }
- else if (((mb_mode = ModeMB(MB_decisions,xin2,yin2)) >= MBM_FIELD00) && (mb_mode <= MBM_FIELD11))
- {
- sum = subdim*(BV(motxdata, xM, xin2, yin2, 0, 0) + BV(motxdata, xM, xin2, yin2, 1, 0));
- p2x = (Float)((sum & 3) ? ((sum | 2) >> 1) : (sum >> 1))/(Float)subdim;
- sum = subdim*(BV(motydata, xM, xin2, yin2, 0, 0) + BV(motydata, xM, xin2, yin2, 1, 0));
- p2y = (Float)((sum & 3) ? ((sum | 2) >> 1) : (sum >> 1))/(Float)subdim;
- }
- else
- {
- p2x = BV(motxdata, xM, xin2, yin2, vec2&0x1, vec2>>1 );
- p2y = BV(motydata, xM, xin2, yin2, vec2&0x1, vec2>>1 );
- }
- if (rule3 )
- {
- p3x = p3y =0;
- }
- else if (((mb_mode = ModeMB(MB_decisions,xin3,yin3)) >= MBM_FIELD00) && (mb_mode <= MBM_FIELD11))
- {
- sum = subdim*(BV(motxdata, xM, xin3, yin3, 0, 0) + BV(motxdata, xM, xin3, yin3, 1, 0));
- p3x = (Float)((sum & 3) ? ((sum | 2) >> 1) : (sum >> 1))/(Float)subdim;
- sum = subdim*(BV(motydata, xM, xin3, yin3, 0, 0) + BV(motydata, xM, xin3, yin3, 1, 0));
- p3y = (Float)((sum & 3) ? ((sum | 2) >> 1) : (sum >> 1))/(Float)subdim;
- }
- else
- {
- p3x = BV(motxdata, xM, xin3, yin3, vec3&0x1, vec3>>1 );
- p3y = BV(motydata, xM, xin3, yin3, vec3&0x1, vec3>>1 );
- }
- if (rule1 && rule2 && rule3 )
- {
- *mvx=*mvy=0;
- }
- else if (rule1+rule2+rule3 == 2)
- {
- *mvx=(Int) subdim*(p1x+p2x+p3x);
- *mvy=(Int) subdim*(p1y+p2y+p3y);
- }
- else
- {
- *mvx=(Int)(subdim*(p1x+p2x+p3x-MAX(p1x,MAX(p2x,p3x))-MIN(p1x,MIN(p2x,p3x))));
- *mvy=(Int)(subdim*(p1y+p2y+p3y-MAX(p1y,MAX(p2y,p3y))-MIN(p1y,MIN(p2y,p3y))));
- }
- #ifdef _DEBUG_PMVS_
- fprintf(stdout,"find_pmvs (%2d,%2d, rule %1d%1d%1d) :np1 %6.2f / %6.2fnp2 %6.2f / %6.2fnp3 %6.2f / %6.2fn",x,y,rule1,rule2,rule3,p1x,p1y,p2x,p2y,p3x,p3y);
- #endif
- return;
- }
- SInt ModeMB (Image *MB_decision, Int i, Int j)
- {
- Int width = MB_decision->x;
- SInt *p = (SInt *)GetImageData(MB_decision);
- return p[width*j+i];
- }