1.从文件中读取h264数据

参考ffmpeg avc.c写的从文件中一帧帧读取h.264数据的demo

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stddef.h>
  4. #include <string.h>
  5.  
  6. char* filebuf_;
  7. const char* pbuf_;
  8. int filesize_;
  9. unsigned char is_stop_;
  10.  
  11. const char* AVCFindStartCodeInternal(const char *p, const char *end)
  12. {
  13. const char *a = p + - ((ptrdiff_t)p & );
  14.  
  15. for (end -= ; p < a && p < end; p++) {
  16. if (p[] == && p[] == && p[] == )
  17. return p;
  18. }
  19.  
  20. for (end -= ; p < end; p += ) {
  21. unsigned int x = *(const unsigned int*)p;
  22. // if ((x - 0x01000100) & (~x) & 0x80008000) // little endian
  23. // if ((x - 0x00010001) & (~x) & 0x00800080) // big endian
  24. if ((x - 0x01010101) & (~x) & 0x80808080) { // generic
  25. if (p[] == ) {
  26. if (p[] == && p[] == )
  27. return p;
  28. if (p[] == && p[] == )
  29. return p + ;
  30. }
  31. if (p[] == ) {
  32. if (p[] == && p[] == )
  33. return p + ;
  34. if (p[] == && p[] == )
  35. return p + ;
  36. }
  37. }
  38. }
  39.  
  40. for (end += ; p < end; p++) {
  41. if (p[] == && p[] == && p[] == )
  42. return p;
  43. }
  44.  
  45. return end + ;
  46. }
  47.  
  48. const char* AVCFindStartCode(const char *p, const char *end)
  49. {
  50. const char *out = AVCFindStartCodeInternal(p, end);
  51. if (p<out && out<end && !out[-]) out--;
  52. return out;
  53. }
  54.  
  55. H264FrameReader_Init(const char* filename)
  56. {
  57. FILE* fp = fopen(filename, "rb");
  58. filebuf_ = ;
  59. filesize_ = ;
  60.  
  61. if (fp)
  62. {
  63. int retval = ;
  64. fseek(fp, , SEEK_END);
  65. filesize_ = ftell(fp);
  66. fseek(fp, , SEEK_SET);
  67.  
  68. filebuf_ = (char*)malloc(filesize_);
  69. retval = fread(filebuf_, , filesize_, fp);
  70.  
  71. fclose(fp);
  72. }
  73. pbuf_ = filebuf_;
  74. }
  75.  
  76. H264FrameReader_Free()
  77. {
  78. free(filebuf_);
  79. }
  80.  
  81. H264FrameReader_ReadFrame(char* outBuf, int* outBufSize)
  82. {
  83. char* pbufout = ;
  84. const char *p = ;
  85. const char *end = ;
  86. const char *nal_start, *nal_end;
  87.  
  88. char startcodebuf[] = { 0x00, 0x00, 0x00, 0x01 };
  89. if (pbuf_ >= filebuf_ + filesize_)
  90. {
  91. return ;
  92. }
  93.  
  94. pbufout = outBuf;
  95. p = pbuf_;
  96. end = filebuf_ + filesize_;
  97.  
  98. nal_start = AVCFindStartCode(p, end);
  99. while (nal_start < end)
  100. {
  101. unsigned int nal_size = ;
  102. unsigned char nal_type = ;
  103.  
  104. while (!*(nal_start++));
  105.  
  106. nal_end = AVCFindStartCode(nal_start, end);
  107.  
  108. nal_size = nal_end - nal_start;
  109. nal_type = nal_start[] & 0x1f;
  110.  
  111. memcpy(pbufout, startcodebuf, );
  112. pbufout += ;
  113. memcpy(pbufout, nal_start, nal_size);
  114. pbufout += nal_size;
  115.  
  116. nal_start = nal_end;
  117. break;
  118. }
  119.  
  120. *outBufSize = pbufout - outBuf;
  121. pbuf_ = nal_start;
  122.  
  123. return ;
  124. }
  125.  
  126. int main(int argc, char **argv)
  127. {
  128. unsigned long max_size = * ;
  129. int tmpbuf_len = ;
  130. int current_read_len = ;
  131. char* tmpbuf = (char*)malloc(max_size * );
  132.  
  133. FILE *fp = fopen("out.h264", "wb+");
  134. if (!fp)
  135. {
  136. printf("open file error\n");
  137. return -;
  138. }
  139.  
  140. H264FrameReader_Init("test.h264");
  141. printf("file size = %d\n", filesize_);
  142. while (current_read_len < filesize_)
  143. {
  144. if (H264FrameReader_ReadFrame(tmpbuf, &tmpbuf_len))
  145. {
  146. printf("tmpbuf_len = %d\n", tmpbuf_len);
  147. fwrite(tmpbuf, tmpbuf_len, , fp);
  148. current_read_len += tmpbuf_len;
  149. }
  150. }
  151. fclose(fp);
  152. H264FrameReader_Free();
  153.  
  154. return ;
  155. }

2.从文件中读取yuv数据

从planar yuv420 文件中读取每一帧数据,从nvenc demo中参考来的,原理如下

1.通过fseek和ftell计算出文件的大小

2.通过yuv的分辨率可以计算出每一帧yuv数据的大小

3.通过上面两步可以计算出文件中包含多少帧的yuv数据,然后通过每一帧数据在文件中的偏移,就可以读出该帧数据

  1. int loadframe(uint8_t *yuvInput[], FILE *hInputYUVFile, uint32_t frmIdx, uint32_t width, uint32_t height)
  2. {
  3. uint64_t fileOffset;
  4. uint32_t result;
  5. uint32_t dwInFrameSize = ;
  6. int anFrameSize[] = {};
  7.  
  8. dwInFrameSize = width * height * / ;
  9. anFrameSize[] = width * height;
  10. anFrameSize[] = anFrameSize[] = width * height / ;
  11.  
  12. //当前帧在文件中的偏移量:当前index * 每一帧的大小
  13. fileOffset = (uint64_t)dwInFrameSize * frmIdx;
  14. //seek到偏移处
  15. result = _fseeki64(hInputYUVFile, fileOffset, SEEK_SET);
  16. if (result == -)
  17. {
  18. return -;
  19. }
  20. //把当前帧的Y、U、V数据分别读取到对应的数组中
  21. fread(yuvInput[], anFrameSize[], , hInputYUVFile);
  22. fread(yuvInput[], anFrameSize[], , hInputYUVFile);
  23. fread(yuvInput[], anFrameSize[], , hInputYUVFile);
  24.  
  25. return ;
  26. }
  27.  
  28. int main()
  29. {
  30.  
  31. infp = fopen("yb.yuv", "rb");
  32. if (!infp)
  33. {
  34. printf("open in file failed\n");
  35. return -;
  36. }
  37.  
  38. uint8_t *yuv[];
  39. int lumaPlaneSize, chromaPlaneSize;
  40.  
  41. lumaPlaneSize = * ;
  42. chromaPlaneSize = lumaPlaneSize >> ;
  43.  
  44. yuv[] = new uint8_t[lumaPlaneSize];
  45. yuv[] = new uint8_t[chromaPlaneSize];
  46. yuv[] = new uint8_t[chromaPlaneSize];
  47. memset(yuv[], , lumaPlaneSize);
  48. memset(yuv[], , chromaPlaneSize);
  49. memset(yuv[], , chromaPlaneSize);
  50.  
  51. uint64_t file_size = ;
  52.  
  53. _fseeki64(infp, , SEEK_END);
  54. file_size = _ftelli64(infp);
  55. _fseeki64(infp, , SEEK_SET);
  56. int totalFrames = file_size / (lumaPlaneSize + chromaPlaneSize + chromaPlaneSize);
  57.  
  58. //遍历每一帧YUV数据
  59. for (int frm = ; frm < totalFrames; frm++)
  60. {
  61. loadframe(yuv, infp, frm, , );
  62. //处理yuv数据
  63. //....
  64. }
  65.  
  66. return ;
  67. }

从文件中读取yuv和h264数据的更多相关文章

  1. java从文件中读取数据然后插入到数据库表中

    实习工作中,完成了领导交给的任务,将搜集到的数据插入到数据库中,代码片段如下: static Connection getConnection() throws SQLException, IOExc ...

  2. 从PCD文件中读取点云数据

    博客转载自:http://www.pclcn.org/study/shownews.php?lang=cn&id=84 在本小节我们学习如何从PCD文件中读取点云数据. 代码 章例1文件夹中, ...

  3. 【Python】从文件中读取数据

    从文件中读取数据 1.1 读取整个文件 要读取文件,需要一个包含几行文本的文件(文件PI_DESC.txt与file_reader.py在同一目录下) PI_DESC.txt 3.1415926535 ...

  4. TF从文件中读取数据

    从文件中读取数据 在TensorFlow中进行模型训练时,在官网给出的三种读取方式,中最好的文件读取方式就是将利用队列进行文件读取,而且步骤有两步: 把样本数据写入TFRecords二进制文件 从队列 ...

  5. 一些常用的文本文件格式(TXT,JSON,CSV)以及如何从这些文件中读取和写入数据

    TXT文件: txt是微软在操作系统上附带的一种文本格式,文件以.txt为后缀. 从txt文件中读取数据: with open ('xxx.txt') as file: data=file.readl ...

  6. 归纳从文件中读取数据的六种方法-JAVA IO基础总结第2篇

    在上一篇文章中,我为大家介绍了<5种创建文件并写入文件数据的方法>,本节我们为大家来介绍6种从文件中读取数据的方法. 另外为了方便大家理解,我为这一篇文章录制了对应的视频:总结java从文 ...

  7. 从一个word文件中读取所有的表格和标题(1)

    首先讲需求: 从word文件中读表格里的数据,然后插入数据库中.word文件中的表格是带有标题的,把标题读出来,进行匹配数据库. 需求分析: word2007底层是以xml文件存储的,所以分析xml的 ...

  8. 从Excel文件中读取内容

    从Excel文件中读取内容 global::System.Web.HttpPostedFileBase file = Request.Files["txtFile"]; strin ...

  9. 在JavaScript文件中读取properties文件的方法

    假设有JavaScript文件叫做:readproperties.js,这个文件需要读取config.properties这个配置文件,步骤如下: 1.  下载插件jquery.i18n.proper ...

随机推荐

  1. pandas的Series

    pandas.Series(data=None, index=None, dtype=None, name=None, copy=False, fastpath=False) 首先介绍一下基本的: d ...

  2. review39

    不可以在非同步方法中使用wait().notify()和notifyAll().

  3. php特级课---3、常用的网站加速技术有哪些

    php特级课---3.常用的网站加速技术有哪些 一.总结 一句话总结:网站加速技术是一组技术的组合,来提升网站的速度 1.Squid代理缓存技术 2.页面静态化缓存 3.Memcache 4.Sphi ...

  4. 企业环境中部署 ActiveMQ

    这一章讲述了怎么配置 ActiveMQ 集群.

  5. oracle 不走索引的原因

    create table tb2 as select * from emp;alter table tb2 modify empno number(4) not null;翻到20W行 create ...

  6. 《Advanced Bash-scripting Guide》学习(十三):引用变量的两个例子

    本文所选的例子来自于<Advanced Bash-scripting Gudie>一书,译者 杨春敏 黄毅 例1. 使用""可以防止单词分割,将变量看成一个整体,没有& ...

  7. 下载 OS X 10.11 GM

    不清楚为什么OS X 10.11 GM版本是Coming Soon,可以通过下面简单方法启用App Store下载. 在终端执行: $ sudo softwareupdate --clear-cata ...

  8. LVM MBR分区(装载)

    必须有至少一个主分区(P),主分区个数+扩展分区个数<= 4个. 创建完主分区,可以创建扩展分区(E),扩展分区可以有1个,或者没有(扩展分区). 主分区(Primary  Partion)可以 ...

  9. 伯乐在线文章URL

    一段代码,可以跑出所有文章的url # encoding: utf-8 import requests from bs4 import BeautifulSoup base_url = 'http:/ ...

  10. db2还原离线备份文件报错SQL2071N 提示“访问共享库出现错误”解决

    db2 buffers buffer 提示报错: SQL2071N  An error occurred while accessing the shared library  “/home/db2a ...