博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
简要分析JM8.6代码中foreman_part_qcif.yuv文件的YUV数据如何悄无声息地进入程序
阅读量:4142 次
发布时间:2019-05-25

本文共 5406 字,大约阅读时间需要 18 分钟。

        分析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
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
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
y_framesize; y++) for (x=0; x
x_size; x++) { imgY_org_frm [y][x] = sf->yf[y*sf->x_size+x]; } for (y=0; y
y_framesize/2; y++) for (x=0; x
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数据流向程序的简要过程.

转载地址:http://xczti.baihongyu.com/

你可能感兴趣的文章
LOCAL_PRELINK_MODULE和prelink-linux-arm.map
查看>>
Simple Guide to use the gdb tool in Android environment
查看>>
Netconsole to capture the log
查看>>
Build GingerBread on 32 bit machine.
查看>>
How to make SD Card world wide writable
查看>>
Detecting Memory Leaks in Kernel
查看>>
Linux initial RAM disk (initrd) overview
查看>>
Timestamping Linux kernel printk output in dmesg for fun and profit
查看>>
There's Much More than Intel/AMD Inside
查看>>
apache和tomcat整合
查看>>
java虚拟机错误问题
查看>>
oracle建立表空间
查看>>
oracle分区表的性能提升
查看>>
"Cannot allocate memory" OutofMemory when call Ant to build Polish project in Tomcat
查看>>
dumpcap抓包(python)
查看>>
查看文件是否被其他进程访问
查看>>
字符编码详解
查看>>
python使用dpkt分析wireshak报文(Modbus规约)
查看>>
css中的IFC
查看>>
CentOS 6.5下 mysql用户root登录不了
查看>>