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

📄 mpg2encdlg.cpp

📁 mpeg2视频编码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
  if (profile!=HP && chroma_format!=CHROMA420)
    error("chroma format must be 4:2:0 in specified Profile");

  if (profile==HP && chroma_format==CHROMA444)
    error("chroma format must be 4:2:0 or 4:2:2 in High Profile");

  if (profile>=MP) /* SP, MP: constrained repeat_first_field */
  {
    if (frame_rate_code<=2 && repeatfirst)
      error("repeat_first_first must be zero");
    if (frame_rate_code<=6 && prog_seq && repeatfirst)
      error("repeat_first_first must be zero");
  }

  if (profile!=HP && dc_prec==3)
    error("11 bit DC precision only allowed in High Profile");


  /* level (parameter value) constraints */

  /* Table 8-8 */
  if (frame_rate_code>5 && level>=ML)
    error("Picture rate greater than permitted in specified Level");

  for (i=0; i<M; i++)
  {
    if (motion_data[i].forw_hor_f_code > maxval->hor_f_code)
      error("forward horizontal f_code greater than permitted in specified Level");

    if (motion_data[i].forw_vert_f_code > maxval->vert_f_code)
      error("forward vertical f_code greater than permitted in specified Level");

    if (i!=0)
    {
      if (motion_data[i].back_hor_f_code > maxval->hor_f_code)
        error("backward horizontal f_code greater than permitted in specified Level");
  
      if (motion_data[i].back_vert_f_code > maxval->vert_f_code)
        error("backward vertical f_code greater than permitted in specified Level");
    }
  }

  /* Table 8-10 */
  if (horizontal_size > maxval->hor_size)
    error("Horizontal size is greater than permitted in specified Level");

  if (vertical_size > maxval->vert_size)
    error("Horizontal size is greater than permitted in specified Level");

  /* Table 8-11 */
  if (horizontal_size*vertical_size*frame_rate > maxval->sample_rate)
    error("Sample rate is greater than permitted in specified Level");

  /* Table 8-12 */
  if (bit_rate> 1.0e6 * maxval->bit_rate)
    error("Bit rate is greater than permitted in specified Level");

  /* Table 8-13 */
  if (vbv_buffer_size > maxval->vbv_buffer_size)
    error("vbv_buffer_size exceeds High Level limit");
}
/////////////////////////////////////////////////////////////////////////////////////////////////
/*
 * 前向和交织帧的运动估计
 *
 *oldorg:前向预测的原始参考图(供P图像和B图像使用)
 *neworg:后向预测的原始参考图(仅供B图像使用)
 *oldref:前向预测的重建图像(P图像和B图像)
 *newref:后向预测的重建图像(B图像)
 *cur:当前原始图像帧(由预测图像产生的)
 *curref:当前的重建图像帧(用来根据第一个场预测第二个场)
 *sxf,syf:前向搜索窗口
 *sxb,syb:后向搜索窗口
 *mbi:指向宏块信息结构体的指针
 *输出结果mbi->
 *mb_type: 0, MB_INTRA, MB_FORWARD, MB_BACKWARD, MB_FORWARD|MB_BACKWARD
 *MV[][][]: 运动向量 (场格式)
 *mv_field_sel: 顶部/底部场
 *motion_type: MC_FIELD, MC_16X8
 */
 //帧图像运动估计
 /* 场图片运动预测程序
 * toporg:原始上半部分场地址
 *topref:重建的上半部分场地址
 *botorg:原始下半部分参考场地址
 *botref:重建的下半部分参考场的地址
 *mb:待匹配的宏块
 *i,j:宏块的位置(即搜索窗口的中心)
 *iminp,jminp,selp,dfieldp:最佳预测场的位置和距离
 *imin8up,jmin8up,sel8up:上半部分宏块的最佳预测的位置
 *imin8lp,jmin8lp,sel8lp:下半部分宏块的最佳预测的位置
 *d8p:最佳16×8的预测块的距离
 *iminsp,jminsp,dsp:最佳同奇偶性预测场的位置和距离 */
/////////////////////////////////////////////////////////////////////
/*
 * 求两个块之间绝对差值
 * blk1,blk2:为两个块左上角像素坐标
 *lx:垂直相邻的像素之间的距离(以字节为单位)
 *hx,hy:水平和垂直插值的标志
 *h:块的高度(通常为8或16)
 *distlim:极值,如果结果超过该值则放弃之。
 */
static int dist1(unsigned char *blk1,unsigned char *blk2,
				 int lx,int hx,int hy,int h,int distlim)
{
  unsigned char *p1,*p1a,*p2;
  int i,j;
  int s,v;

  s = 0;
  p1 = blk1;
  p2 = blk2;

  if (!hx && !hy)
    for (j=0; j<h; j++)
    {
      if ((v = p1[0]  - p2[0])<0)  v = -v; s+= v;
      if ((v = p1[1]  - p2[1])<0)  v = -v; s+= v;
      if ((v = p1[2]  - p2[2])<0)  v = -v; s+= v;
      if ((v = p1[3]  - p2[3])<0)  v = -v; s+= v;
      if ((v = p1[4]  - p2[4])<0)  v = -v; s+= v;
      if ((v = p1[5]  - p2[5])<0)  v = -v; s+= v;
      if ((v = p1[6]  - p2[6])<0)  v = -v; s+= v;
      if ((v = p1[7]  - p2[7])<0)  v = -v; s+= v;
      if ((v = p1[8]  - p2[8])<0)  v = -v; s+= v;
      if ((v = p1[9]  - p2[9])<0)  v = -v; s+= v;
      if ((v = p1[10] - p2[10])<0) v = -v; s+= v;
      if ((v = p1[11] - p2[11])<0) v = -v; s+= v;
      if ((v = p1[12] - p2[12])<0) v = -v; s+= v;
      if ((v = p1[13] - p2[13])<0) v = -v; s+= v;
      if ((v = p1[14] - p2[14])<0) v = -v; s+= v;
      if ((v = p1[15] - p2[15])<0) v = -v; s+= v;

      if (s >= distlim)
        break;

      p1+= lx;
      p2+= lx;
    }
  else if (hx && !hy)
    for (j=0; j<h; j++)
    {
      for (i=0; i<16; i++)
      {
        v = ((unsigned int)(p1[i]+p1[i+1]+1)>>1) - p2[i];
        if (v>=0)
          s+= v;
        else
          s-= v;
      }
      p1+= lx;
      p2+= lx;
    }
  else if (!hx && hy)
  {
    p1a = p1 + lx;
    for (j=0; j<h; j++)
    {
      for (i=0; i<16; i++)
      {
        v = ((unsigned int)(p1[i]+p1a[i]+1)>>1) - p2[i];
        if (v>=0)
          s+= v;
        else
          s-= v;
      }
      p1 = p1a;
      p1a+= lx;
      p2+= lx;
    }
  }
  else /* if (hx && hy) */
  {
    p1a = p1 + lx;
    for (j=0; j<h; j++)
    {
      for (i=0; i<16; i++)
      {
        v = ((unsigned int)(p1[i]+p1[i+1]+p1a[i]+p1a[i+1]+2)>>2) - p2[i];
        if (v>=0)
          s+= v;
        else
          s-= v;
      }
      p1 = p1a;
      p1a+= lx;
      p2+= lx;
    }
  }

  return s;
}
////////////////////////////////////////////////////////////
/** 两个块之间均方误差*/
static int dist2(unsigned char *blk1,unsigned char *blk2,
				 int lx,int hx,int hy,int h)
{
  unsigned char *p1,*p1a,*p2;
  int i,j;
  int s,v;

  s = 0;
  p1 = blk1;
  p2 = blk2;
  if (!hx && !hy)
    for (j=0; j<h; j++)
    {
      for (i=0; i<16; i++)
      {
        v = p1[i] - p2[i];
        s+= v*v;
      }
      p1+= lx;
      p2+= lx;
    }
  else if (hx && !hy)
    for (j=0; j<h; j++)
    {
      for (i=0; i<16; i++)
      {
        v = ((unsigned int)(p1[i]+p1[i+1]+1)>>1) - p2[i];
        s+= v*v;
      }
      p1+= lx;
      p2+= lx;
    }
  else if (!hx && hy)
  {
    p1a = p1 + lx;
    for (j=0; j<h; j++)
    {
      for (i=0; i<16; i++)
      {
        v = ((unsigned int)(p1[i]+p1a[i]+1)>>1) - p2[i];
        s+= v*v;
      }
      p1 = p1a;
      p1a+= lx;
      p2+= lx;
    }
  }
  else /* if (hx && hy) */
  {
    p1a = p1 + lx;
    for (j=0; j<h; j++)
    {
      for (i=0; i<16; i++)
      {
        v = ((unsigned int)(p1[i]+p1[i+1]+p1a[i]+p1a[i+1]+2)>>2) - p2[i];
        s+= v*v;
      }
      p1 = p1a;
      p1a+= lx;
      p2+= lx;
    }
  }

  return s;
}
////////////////////////////////////////////////////////////
/**(16*h) 块和双向预测之间的绝对误差*/
static int bdist1(unsigned char *pf,unsigned char *pb,unsigned char *p2,
				  int lx,int hxf,int hyf,int hxb,int hyb,int h)
{
  unsigned char *pfa,*pfb,*pfc,*pba,*pbb,*pbc;
  int i,j;
  int s,v;

  pfa = pf + hxf;
  pfb = pf + lx*hyf;
  pfc = pfb + hxf;

  pba = pb + hxb;
  pbb = pb + lx*hyb;
  pbc = pbb + hxb;

  s = 0;

  for (j=0; j<h; j++)
  {
    for (i=0; i<16; i++)
    {
      v = ((((unsigned int)(*pf++ + *pfa++ + *pfb++ + *pfc++ + 2)>>2) +
            ((unsigned int)(*pb++ + *pba++ + *pbb++ + *pbc++ + 2)>>2) + 1)>>1)
           - *p2++;
      if (v>=0)
        s+= v;
      else
        s-= v;
    }
    p2+= lx-16;
    pf+= lx-16;
    pfa+= lx-16;
    pfb+= lx-16;
    pfc+= lx-16;
    pb+= lx-16;
    pba+= lx-16;
    pbb+= lx-16;
    pbc+= lx-16;
  }

  return s;
}
////////////////////////////////////////////////////////////
/** (16*h) 块和双向预测之间的均方误差*/
static int bdist2(unsigned char *pf,unsigned char *pb,unsigned char *p2,
				  int lx,int hxf,int hyf,int hxb,int hyb,int h)
{
  unsigned char *pfa,*pfb,*pfc,*pba,*pbb,*pbc;
  int i,j;
  int s,v;

  pfa = pf + hxf;
  pfb = pf + lx*hyf;
  pfc = pfb + hxf;

  pba = pb + hxb;
  pbb = pb + lx*hyb;
  pbc = pbb + hxb;

  s = 0;

  for (j=0; j<h; j++)
  {
    for (i=0; i<16; i++)
    {
      v = ((((unsigned int)(*pf++ + *pfa++ + *pfb++ + *pfc++ + 2)>>2) +
            ((unsigned int)(*pb++ + *pba++ + *pbb++ + *pbc++ + 2)>>2) + 1)>>1)
          - *p2++;
      s+=v*v;
    }
    p2+= lx-16;
    pf+= lx-16;
    pfa+= lx-16;
    pfb+= lx-16;
    pfc+= lx-16;
    pb+= lx-16;
    pba+= lx-16;
    pbb+= lx-16;
    pbc+= lx-16;
  }

  return s;
}
/////////////////////////////////////////////////////
/** 计算一个(16*16) 块的方差,并乘以256
 * p:  块左上角像素的地址
 * lx: 相邻像素的垂直距离(以字节为单位)*/
static int variance(unsigned char *p,int lx)
{
  int i,j;
  unsigned int v,s,s2;

  s = s2 = 0;

  for (j=0; j<16; j++)
  {
    for (i=0; i<16; i++)
    {
      v = *p++;
      s+= v;
      s2+= v*v;
    }
    p+= lx-16;
  }
  return s2 - (s*s)/256;
}
/*全搜索块匹配

 blk:块的左上角像素坐标 
h:块的高度 
lx:在参考块中,垂直相邻的像素之间的距离(以字节为单位)
org:源参考图像的左上角像素的坐标
ref:重建参考图像的左上角像素的坐标
i0,j0:搜索窗口的中心点
sx,sy:搜索窗口的半长和半宽
xmax,ymax:搜索区域的右边界和下边界
iminp,jminp/:指向存储结果的指针*/
static int fullsearch(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)
{
  int i,j,imin,jmin,ilow,ihigh,jlow,jhigh;
  int d,dmin;
  int k,l,sxy;

  ilow = i0 - sx;
  ihigh = i0 + sx;

  if (ilow<0)ilow = 0;

  if (ihigh>xmax-16)
    ihigh = xmax-16;

  jlow = j0 - sy;
  jhigh = j0 + sy;

  if (jlow<0)jlow = 0;

  if (jhigh>ymax-h)jhigh = ymax-h;
  /* full pel search, spiraling outwards */
  imin = i0;
  jmin = j0;
  dmin = dist1(org+imin+lx*jmin,blk,lx,0,0,h,65536);

  sxy = (sx>sy) ? sx : sy;

  for (l=1; l<=sxy; l++)
  {
    i = i0 - l;
    j = j0 - l;
    for (k=0; k<8*l; k++)
    {
      if (i>=ilow && i<=ihigh && j>=jlow && j<=jhigh)
      {
        d = dist1(org+i+lx*j,blk,lx,0,0,h,dmin);

        if (d<dmin)
        {
          dmin = d;
          imin = i;
          jmin = j;
        }
      }

      if      (k<2*l) i++;
      else if (k<4*l) j++;
      else if (k<6*l) i--;
      else            j--;
    }
  }

  /* half pel */
  dmin = 65536;
  imin <<= 1;
  jmin <<= 1;
  ilow = imin - (imin>0);
  ihigh = imin + (imin<((xmax-16)<<1));
  jlow = jmin - (jmin>0);
  jhigh = jmin + (jmin<((ymax-h)<<1));

  for (j=jlow; j<=jhigh; j++)
    for (i=ilow; i<=ihigh; i++)

⌨️ 快捷键说明

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