⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 predict.c

📁 avi2mpg1_src 中包含了mpeg1编码的源程序
💻 C
📖 第 1 页 / 共 4 页
字号:
/* predict.c, 运动补偿预测*/
#include <stdio.h>#include "global.h"/* 函数声明*/static void predict_mb(  unsigned char *oldref[], unsigned char *newref[], unsigned char *cur[],  int lx, int bx, int by, int pict_type, int pict_struct, int mb_type,  int motion_type, int secondfield,  int PMV[2][2][2], int mv_field_sel[2][2], int dmvector[2]);static void pred(unsigned char *src[], int sfield,  unsigned char *dst[], int dfield,  int lx, int w, int h, int x, int y, int dx, int dy, int addflag);static void pred_comp(unsigned char *src, unsigned char *dst,  int lx, int w, int h, int x, int y, int dx, int dy, int addflag);static void calc_DMV(int DMV[][2], int *dmvector, int mvx,  int mvy);static void clearblock(unsigned char *cur[], int i0, int j0);
static short PACKED_0[4] = { 0, 0, 0, 0 };
static short PACKED_1[4] = { 1, 1, 1, 1 };
static short PACKED_2[4] = { 2, 2, 2, 2 };
/* 为一个完整的图像形成预测 * * reff: 前向预测参考帧 * refb: 后向预测参考帧 * cur:  目标帧 * secondfield: 预测帧的第二场 * mbi:  宏块信息 *  */void predict(reff,refb,cur,secondfield,mbi)unsigned char *reff[],*refb[],*cur[3];int secondfield;struct mbinfo *mbi;{  int i, j, k;  k = 0;  for (j=0; j<height2; j+=16)    for (i=0; i<width; i+=16)    {      predict_mb(reff,refb,cur,width,i,j,pict_type,pict_struct,                 mbi[k].mb_type,mbi[k].motion_type,secondfield,                 mbi[k].MV,mbi[k].mv_field_sel,mbi[k].dmvector);      k++;    }}/* 为一个宏块形成预测
 * * oldref: 前向预测的参考帧 * newref: 后向预测的参考帧 * cur:    目标帧 * lx:     帧的宽度 * bx,by:  要预测的宏块的图像 (场或帧) 坐标
 * pict_type: I, P 或 B * pict_struct: FRAME_PICTURE, TOP_FIELD, BOTTOM_FIELD * mb_type:     MB_FORWARD, MB_BACKWARD, MB_INTRA * motion_type: MC_FRAME, MC_FIELD, MC_16X8, MC_DMV * secondfield: 预测一帧的第二场 * PMV[2][2][2]: 运动向量 * mv_field_sel[2][2]: 运动向量场选择 * dmvector: 差分运动向量 *  */static void predict_mb(oldref,newref,cur,lx,bx,by,pict_type,pict_struct,  mb_type,motion_type,secondfield,PMV,mv_field_sel,dmvector)unsigned char *oldref[],*newref[],*cur[];int lx;int bx,by;int pict_type;int pict_struct;int mb_type;int motion_type;int secondfield;int PMV[2][2][2], mv_field_sel[2][2], dmvector[2];{  int addflag, currentfield;  unsigned char **predframe;  int DMV[2][2];  if (mb_type&MB_INTRA)  {    clearblock(cur,bx,by);    return;  }  addflag = 0; /* first prediction is stored, second is added and averaged */  if ((mb_type & MB_FORWARD) || (pict_type==P_TYPE))  {    /* forward prediction, including zero MV in P pictures */    if (pict_struct==FRAME_PICTURE)    {      /* frame picture */      if ((motion_type==MC_FRAME) || !(mb_type & MB_FORWARD))      {        /* frame-based prediction in frame picture */        pred(oldref,0,cur,0,          lx,16,16,bx,by,PMV[0][0][0],PMV[0][0][1],0);      }      else if (motion_type==MC_FIELD)      {               /* top field prediction */        pred(oldref,mv_field_sel[0][0],cur,0,          lx<<1,16,8,bx,by>>1,PMV[0][0][0],PMV[0][0][1]>>1,0);        /* bottom field prediction */        pred(oldref,mv_field_sel[1][0],cur,1,          lx<<1,16,8,bx,by>>1,PMV[1][0][0],PMV[1][0][1]>>1,0);      }      else if (motion_type==MC_DMV)      {        /* calculate derived motion vectors */        calc_DMV(DMV,dmvector,PMV[0][0][0],PMV[0][0][1]>>1);        /* predict top field from top field */        pred(oldref,0,cur,0,          lx<<1,16,8,bx,by>>1,PMV[0][0][0],PMV[0][0][1]>>1,0);        /* predict bottom field from bottom field */        pred(oldref,1,cur,1,          lx<<1,16,8,bx,by>>1,PMV[0][0][0],PMV[0][0][1]>>1,0);        /* predict and add to top field from bottom field */        pred(oldref,1,cur,0,          lx<<1,16,8,bx,by>>1,DMV[0][0],DMV[0][1],1);        /* predict and add to bottom field from top field */        pred(oldref,0,cur,1,          lx<<1,16,8,bx,by>>1,DMV[1][0],DMV[1][1],1);      }      else      {        /* invalid motion_type in frame picture */        if (!quiet)          fprintf(stderr,"invalid motion_type\n");      }    }    else /* TOP_FIELD or BOTTOM_FIELD */    {      /* field picture */      currentfield = (pict_struct==BOTTOM_FIELD);      if ((pict_type==P_TYPE) && secondfield          && (currentfield!=mv_field_sel[0][0]))        predframe = newref; /* same frame */      else        predframe = oldref; /* previous frame */      if ((motion_type==MC_FIELD) || !(mb_type & MB_FORWARD))      {        /* field-based prediction in field picture */        pred(predframe,mv_field_sel[0][0],cur,currentfield,          lx<<1,16,16,bx,by,PMV[0][0][0],PMV[0][0][1],0);      }      else if (motion_type==MC_16X8)      {        /* 16 x 8 motion compensation in field picture */        /* upper half */        pred(predframe,mv_field_sel[0][0],cur,currentfield,          lx<<1,16,8,bx,by,PMV[0][0][0],PMV[0][0][1],0);        if ((pict_type==P_TYPE) && secondfield            && (currentfield!=mv_field_sel[1][0]))          predframe = newref; /* same frame */        else          predframe = oldref; /* previous frame */        /* lower half */        pred(predframe,mv_field_sel[1][0],cur,currentfield,          lx<<1,16,8,bx,by+8,PMV[1][0][0],PMV[1][0][1],0);      }      else if (motion_type==MC_DMV)      {                /* determine which frame to use for prediction */        if (secondfield)          predframe = newref; /* same frame */        else          predframe = oldref; /* previous frame */        /* calculate derived motion vectors */        calc_DMV(DMV,dmvector,PMV[0][0][0],PMV[0][0][1]);        /* predict from field of same parity */        pred(oldref,currentfield,cur,currentfield,          lx<<1,16,16,bx,by,PMV[0][0][0],PMV[0][0][1],0);        /* predict from field of opposite parity */        pred(predframe,!currentfield,cur,currentfield,          lx<<1,16,16,bx,by,DMV[0][0],DMV[0][1],1);      }      else      {        if (!quiet)          fprintf(stderr,"invalid motion_type\n");      }    }    addflag = 1;   }  if (mb_type & MB_BACKWARD)  {    /* backward prediction */    if (pict_struct==FRAME_PICTURE)    {      /* frame picture */      if (motion_type==MC_FRAME)      {        /* frame-based prediction in frame picture */        pred(newref,0,cur,0,          lx,16,16,bx,by,PMV[0][1][0],PMV[0][1][1],addflag);      }      else      {                /* top field prediction */        pred(newref,mv_field_sel[0][1],cur,0,          lx<<1,16,8,bx,by>>1,PMV[0][1][0],PMV[0][1][1]>>1,addflag);        /* bottom field prediction */        pred(newref,mv_field_sel[1][1],cur,1,          lx<<1,16,8,bx,by>>1,PMV[1][1][0],PMV[1][1][1]>>1,addflag);      }    }    else /* TOP_FIELD or BOTTOM_FIELD */    {      /* field picture */      currentfield = (pict_struct==BOTTOM_FIELD);      if (motion_type==MC_FIELD)      {        /* field-based prediction in field picture */        pred(newref,mv_field_sel[0][1],cur,currentfield,          lx<<1,16,16,bx,by,PMV[0][1][0],PMV[0][1][1],addflag);      }      else if (motion_type==MC_16X8)      {        /* 16 x 8 motion compensation in field picture */        /* upper half */        pred(newref,mv_field_sel[0][1],cur,currentfield,          lx<<1,16,8,bx,by,PMV[0][1][0],PMV[0][1][1],addflag);        /* lower half */        pred(newref,mv_field_sel[1][1],cur,currentfield,          lx<<1,16,8,bx,by+8,PMV[1][1][0],PMV[1][1][1],addflag);      }      else      {        /* invalid motion_type in field picture */        if (!quiet)          fprintf(stderr,"invalid motion_type\n");      }    }  }}/* 预测矩形块 * * src:     源帧(Y,U,V) * sfield:  源场选择(0: 帧或上半场, 1: 下半场) * dst:     目标帧(Y,U,V) * dfield:  目标场选择 * * 下面的数值是亮度图像的参数 * lx:      相邻像素的垂直距离 * w,h:     块的宽度和高度 * x,y:     目标块的坐标 * dx,dy:   半像素运动向量 * addflag: 存储或加预测 */static void pred(src,sfield,dst,dfield,lx,w,h,x,y,dx,dy,addflag)unsigned char *src[];int sfield;unsigned char *dst[];int dfield;int lx;int w, h;int x, y;int dx, dy;int addflag;{  int cc;  for (cc=0; cc<3; cc++)  {    if (cc==1)    {      /* scale for color components */      if (chroma_format==CHROMA420)      {        /* vertical */        h >>= 1; y >>= 1; dy /= 2;      }      if (chroma_format!=CHROMA444)      {        /* horizontal */        w >>= 1; x >>= 1; dx /= 2;        lx >>= 1;      }    }    pred_comp(src[cc]+(sfield?lx>>1:0),dst[cc]+(dfield?lx>>1:0),      lx,w,h,x,y,dx,dy,addflag);  }}/* 低等级预测
 *
 * src:     源帧(Y,U,V)
 * dst:     目标帧(Y,U,V)
 *
 * lx:      相邻像素的垂直距离
 * w,h:     块的宽度和高度
 * x,y:     目标块的坐标
 * dx,dy:   半像素运动向量
 * addflag: 存储或加预测

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -