分析encode_one_frame函数.

 

       先来看下结构体Sourceframe的定义:

typedef struct
{
  // Size info
  int x_size, y_framesize, y_fieldsize;  
  char *yf, *uf, *vf;                    //!< frame representation
  char *yt, *ut, *vt;                    //!< top field
  char *yb, *ub, *vb;                    //!< bottom field
} Sourceframe;

 可见,其中将来要存得就是原始的YUV帧的尺寸和YUV信息, 接着看srcframe指针变量的定义:

static Sourceframe *srcframe;

 接着看对srcframe的赋值操作:

srcframe = AllocSourceframe (img->width, img->height);

进入AllocSourceframe函数看内部:

static Sourceframe *AllocSourceframe (int xs, int ys)
{
  Sourceframe *sf = NULL;
  const unsigned int bytes_y = xs*ys;
  const unsigned int bytes_uv = (xs*ys)/4;

  if ((sf = calloc (1, sizeof (Sourceframe))) == NULL) no_mem_exit ("ReadOneFrame: sf");
  if (sf->yf == NULL) if ((sf->yf = calloc (1, bytes_y)) == NULL) no_mem_exit ("ReadOneFrame: sf->yf");
  if (sf->yt == NULL) if ((sf->yt = calloc (1, bytes_y/2)) == NULL) no_mem_exit ("ReadOneFrame: sf->yt");
  if (sf->yb == NULL) if ((sf->yb = calloc (1, bytes_y/2)) == NULL) no_mem_exit ("ReadOneFrame: sf->yb");
  if (sf->uf == NULL) if ((sf->uf = calloc (1, bytes_uv)) == NULL) no_mem_exit ("ReadOneFrame: sf->uf");
  if (sf->ut == NULL) if ((sf->ut = calloc (1, bytes_uv/2)) == NULL) no_mem_exit ("ReadOneFrame: sf->ut");
  if (sf->ub == NULL) if ((sf->ub = calloc (1, bytes_uv/2)) == NULL) no_mem_exit ("ReadOneFrame: sf->ub");
  if (sf->vf == NULL) if ((sf->vf = calloc (1, bytes_uv)) == NULL) no_mem_exit ("ReadOneFrame: sf->vf");
  if (sf->vt == NULL) if ((sf->vt = calloc (1, bytes_uv/2)) == NULL) no_mem_exit ("ReadOneFrame: sf->vt");
  if (sf->vb == NULL) if ((sf->vb = calloc (1, bytes_uv/2)) == NULL) no_mem_exit ("ReadOneFrame: sf->vb");
  sf->x_size = xs;
  sf->y_framesize = ys;
  sf->y_fieldsize = ys/2;

  return sf;
}

   可以看到,实际上是就是在对内存上分配存储空间,并让栈指针sf指向分配的堆内存,返回sf后,srcframe指针就指向了该块对内存.

接着执行下面的语句:

ReadOneFrame (FrameNumberInFile, input->infile_header, img->width, img->height, srcframe);

执行后,就对srcframe指针指向的堆内存赋值了,进入ReadOneFrame函数大致看一下:

static void ReadOneFrame (int FrameNoInFile, int HeaderSize, int xs, int ys, Sourceframe *sf)
{
  int i;

  const unsigned int bytes_y = xs*ys;
  const unsigned int bytes_uv = (xs*ys)/4;
  const int framesize_in_bytes = bytes_y + 2*bytes_uv;

  assert (xs % MB_BLOCK_SIZE == 0);
  assert (ys % MB_BLOCK_SIZE == 0);
  assert (p_in != NULL);
  assert (sf != NULL);
  assert (sf->yf != NULL);

  assert (FrameNumberInFile == FrameNoInFile);
  // printf ("ReadOneFrame: frame_no %d xs %d ys %d\n", FrameNoInFile, xs, ys);

  if (fseek (p_in, HeaderSize, SEEK_SET) != 0)
    error ("ReadOneFrame: cannot fseek to (Header size) in p_in", -1);

  // the reason for the following loop is to support source files bigger than
  // MAXINT.  In most operating systems, including Windows, it is possible to
  // fseek to file positions bigger than MAXINT by using this relative seeking
  // technique.  StW, 12/30/02
  // Skip starting frames
  for (i=0; i<input->start_frame; i++)
    if (fseek (p_in, framesize_in_bytes, SEEK_CUR) != 0) 
    {
      printf ("ReadOneFrame: cannot advance file pointer in p_in beyond frame %d, looping to picture zero\n", i);
      if (fseek (p_in, HeaderSize, SEEK_SET) != 0)
        report_stats_on_error();
        exit (-1);
    } 

  for (i=0; i<FrameNoInFile; i++)
    if (fseek (p_in, framesize_in_bytes, SEEK_CUR) != 0) 
    {
      printf ("ReadOneFrame: cannot advance file pointer in p_in beyond frame %d, looping to picture zero\n", i);
      if (fseek (p_in, HeaderSize, SEEK_SET) != 0)
        error ("ReadOneFrame: cannot fseek to (Header size) in p_in", -1);
    }

  // Here we are at the correct position for the source frame in the file.  Now
  // read it.
  if (fread (sf->yf, 1, bytes_y, p_in) != bytes_y)
  {
    printf ("ReadOneFrame: cannot read %d bytes from input file, unexpected EOF?, exiting", bytes_y);
    report_stats_on_error();
    exit (-1);
  }
  if (fread (sf->uf, 1, bytes_uv, p_in) != bytes_uv)
  {
    printf ("ReadOneFrame: cannot read %d bytes from input file, unexpected EOF?, exiting", bytes_uv);
    report_stats_on_error();
    exit (-1);
  }
  if (fread (sf->vf, 1, bytes_uv, p_in) != bytes_uv)
  {
    printf ("ReadOneFrame: cannot read %d bytes from input file, unexpected EOF?, exiting", bytes_uv);
    report_stats_on_error();
    exit (-1);
  }

  // Complete frame is read into sf->?f, now setup 
  // top and bottom field (sf->?t and sf->?b)

  GenerateFieldComponent (sf->yf, sf->yt, sf->yb, xs, ys);
  GenerateFieldComponent (sf->uf, sf->ut, sf->ub, xs/2, ys/2);
  GenerateFieldComponent (sf->vf, sf->vt, sf->vb, xs/2, ys/2);
}

其中的p_in就是一个文件指针,指向了原始的YUV文件,p_in是一个全局变量,对p_in的赋值在Configure函数中,摘录如下:

// Open Files
  if ((p_in=fopen(input->infile,"rb"))==NULL)
  {
    snprintf(errortext, ET_SIZE, "Input file %s does not exist",input->infile);
    error (errortext, 500);
  }

     其中的input->infile是从配置文件(encoder_baseline.cfg)中得到的,input->infile指向了“foreman_part_qcif.yuv”

 

     回头再看语句:

ReadOneFrame (FrameNumberInFile, input->infile_header, img->width, img->height, srcframe);

 易知,把"foreman_part_qcif.yuv"中的像素值塞进了srcframe指针指向的堆内存, 通过上面语句塞进对内存之后怎么办呢?看下面的语句:

CopyFrameToOldImgOrgVariables (srcframe);

 进入CopyFrameToOldImgOrgVariables函数看一下:

static void CopyFrameToOldImgOrgVariables (Sourceframe *sf)
{
  int x, y;

  for (y=0; y<sf->y_framesize; y++)
    for (x=0; x<sf->x_size; x++)
    {
        imgY_org_frm [y][x] = sf->yf[y*sf->x_size+x];
    }

  for (y=0; y<sf->y_framesize/2; y++)
    for (x=0; x<sf->x_size/2; x++)
    {
      imgUV_org_frm[0][y][x] = sf->uf[y*sf->x_size/2+x];
      imgUV_org_frm[1][y][x] = sf->vf[y*sf->x_size/2+x];
    }
}

可见,这个函数实现的功能是把srcframe指向的对内存中的数据往imgY_org_frm和imgUV_org_frm中倾倒, 而后又有:

imgY_org = imgY_org_frm;
imgUV_org = imgUV_org_frm;

  也就是说,指向的地方相同,所以全局的 imgY_org和imgUV_org 中也就有了"foreman_part_qcif.yuv"中的YUV信息,而且都根据将来的需要进行了一定的组织(数据排列).

 以后用imgY_org和imgUV_org就相当于用到了"foreman_part_qcif.yuv"中的像素值. 以上就是YUV数据流向程序的简要过程.

posted on 2012-12-17 20:19  裴银祥的博客园  阅读(710)  评论(0编辑  收藏  举报