简要分析JM8.6代码中foreman_part_qcif.yuv文件的YUV数据如何悄无声息地进入程序

本文详细解析了YUV数据从文件加载到程序的过程,包括内存分配、读取YUV文件、数据存储及组织等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

      分析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数据流向程序的简要过程.

本文详细介绍了如何利用Python语言结合MySQL数据库开发一个学生管理系统。通过这一过程,读者不仅能够掌握系统设计的基本思路,还能学习到如何使用Python进行数据库操作。该系统涵盖了用户界面设计、数据验证以及数据库的增删改查等多个关键环节。 Python作为一种高级编程语言,以简洁易懂著称,广泛应用于数据分析、机器学习和网络爬虫等领域,同时也非常适合用于快速开发数据库管理应用。MySQL是一个广泛使用的开源关系型数据库管理系统,具有轻量级、高性能、高可靠性和良好的编程语言兼容性等特点,是数据存储的理想选择。在本系统中,通过Python的pymysql库实现了与MySQL数据库的交互。 pymysql是一个Python第三方库,它允许程序通过类似DB-API接口连接MySQL数据库,执行SQL语句并获取结果。在系统中,通过pymysql建立数据库连接,执行SQL语句完成数据的增删改查操作,并对结果进行处理。 系统采用命令行界面供用户操作。程序开始时,提示用户输入学生信息,如学号、姓名和各科成绩,并设计了输入验证逻辑,确保数据符合预期格式,例如学号为1至3位整数,成绩为0至100分的整数。 数据库设计方面,系统使用名为“test”的数据库和“StuSys”表,表中存储学生的学号、姓名、各科成绩及总成绩等信息。通过pymysql的cursor对象执行SQL语句,实现数据的增删改查操作。在构建SQL语句时,采用参数化查询以降低SQL注入风险。 系统在接收用户输入时进行了严格验证,包括正则表达式匹配和数字范围检查等,确保数据的准确性和安全性。同时,提供了错误处理机制,如输入不符合要求时提示用户重新输入,数据库操作出错时给出相应提示。 在数据库操作流程中,用户可以通过命令行添加学生信息或删除记录。添加时会检查学号是否重复以避免数据冲突,删除时需用户确认。通过上述分析,本文展示了从
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值