1.从文件中读取h264数据

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

#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h> char* filebuf_;
const char* pbuf_;
int filesize_;
unsigned char is_stop_; const char* AVCFindStartCodeInternal(const char *p, const char *end)
{
const char *a = p + - ((ptrdiff_t)p & ); for (end -= ; p < a && p < end; p++) {
if (p[] == && p[] == && p[] == )
return p;
} for (end -= ; p < end; p += ) {
unsigned int x = *(const unsigned int*)p;
// if ((x - 0x01000100) & (~x) & 0x80008000) // little endian
// if ((x - 0x00010001) & (~x) & 0x00800080) // big endian
if ((x - 0x01010101) & (~x) & 0x80808080) { // generic
if (p[] == ) {
if (p[] == && p[] == )
return p;
if (p[] == && p[] == )
return p + ;
}
if (p[] == ) {
if (p[] == && p[] == )
return p + ;
if (p[] == && p[] == )
return p + ;
}
}
} for (end += ; p < end; p++) {
if (p[] == && p[] == && p[] == )
return p;
} return end + ;
} const char* AVCFindStartCode(const char *p, const char *end)
{
const char *out = AVCFindStartCodeInternal(p, end);
if (p<out && out<end && !out[-]) out--;
return out;
} H264FrameReader_Init(const char* filename)
{
FILE* fp = fopen(filename, "rb");
filebuf_ = ;
filesize_ = ; if (fp)
{
int retval = ;
fseek(fp, , SEEK_END);
filesize_ = ftell(fp);
fseek(fp, , SEEK_SET); filebuf_ = (char*)malloc(filesize_);
retval = fread(filebuf_, , filesize_, fp); fclose(fp);
}
pbuf_ = filebuf_;
} H264FrameReader_Free()
{
free(filebuf_);
} H264FrameReader_ReadFrame(char* outBuf, int* outBufSize)
{
char* pbufout = ;
const char *p = ;
const char *end = ;
const char *nal_start, *nal_end; char startcodebuf[] = { 0x00, 0x00, 0x00, 0x01 };
if (pbuf_ >= filebuf_ + filesize_)
{
return ;
} pbufout = outBuf;
p = pbuf_;
end = filebuf_ + filesize_; nal_start = AVCFindStartCode(p, end);
while (nal_start < end)
{
unsigned int nal_size = ;
unsigned char nal_type = ; while (!*(nal_start++)); nal_end = AVCFindStartCode(nal_start, end); nal_size = nal_end - nal_start;
nal_type = nal_start[] & 0x1f; memcpy(pbufout, startcodebuf, );
pbufout += ;
memcpy(pbufout, nal_start, nal_size);
pbufout += nal_size; nal_start = nal_end;
break;
} *outBufSize = pbufout - outBuf;
pbuf_ = nal_start; return ;
} int main(int argc, char **argv)
{
unsigned long max_size = * ;
int tmpbuf_len = ;
int current_read_len = ;
char* tmpbuf = (char*)malloc(max_size * ); FILE *fp = fopen("out.h264", "wb+");
if (!fp)
{
printf("open file error\n");
return -;
} H264FrameReader_Init("test.h264");
printf("file size = %d\n", filesize_);
while (current_read_len < filesize_)
{
if (H264FrameReader_ReadFrame(tmpbuf, &tmpbuf_len))
{
printf("tmpbuf_len = %d\n", tmpbuf_len);
fwrite(tmpbuf, tmpbuf_len, , fp);
current_read_len += tmpbuf_len;
}
}
fclose(fp);
H264FrameReader_Free(); return ;
}

2.从文件中读取yuv数据

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

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

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

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

int loadframe(uint8_t *yuvInput[], FILE *hInputYUVFile, uint32_t frmIdx, uint32_t width, uint32_t height)
{
uint64_t fileOffset;
uint32_t result;
uint32_t dwInFrameSize = ;
int anFrameSize[] = {}; dwInFrameSize = width * height * / ;
anFrameSize[] = width * height;
anFrameSize[] = anFrameSize[] = width * height / ; //当前帧在文件中的偏移量:当前index * 每一帧的大小
fileOffset = (uint64_t)dwInFrameSize * frmIdx;
//seek到偏移处
result = _fseeki64(hInputYUVFile, fileOffset, SEEK_SET);
if (result == -)
{
return -;
}
//把当前帧的Y、U、V数据分别读取到对应的数组中
fread(yuvInput[], anFrameSize[], , hInputYUVFile);
fread(yuvInput[], anFrameSize[], , hInputYUVFile);
fread(yuvInput[], anFrameSize[], , hInputYUVFile); return ;
} int main()
{ infp = fopen("yb.yuv", "rb");
if (!infp)
{
printf("open in file failed\n");
return -;
} uint8_t *yuv[];
int lumaPlaneSize, chromaPlaneSize; lumaPlaneSize = * ;
chromaPlaneSize = lumaPlaneSize >> ; yuv[] = new uint8_t[lumaPlaneSize];
yuv[] = new uint8_t[chromaPlaneSize];
yuv[] = new uint8_t[chromaPlaneSize];
memset(yuv[], , lumaPlaneSize);
memset(yuv[], , chromaPlaneSize);
memset(yuv[], , chromaPlaneSize); uint64_t file_size = ; _fseeki64(infp, , SEEK_END);
file_size = _ftelli64(infp);
_fseeki64(infp, , SEEK_SET);
int totalFrames = file_size / (lumaPlaneSize + chromaPlaneSize + chromaPlaneSize); //遍历每一帧YUV数据
for (int frm = ; frm < totalFrames; frm++)
{
loadframe(yuv, infp, frm, , );
//处理yuv数据
//....
} return ;
}

从文件中读取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. php/oracle: 解析oracle表中的NCLOB,CLOB字段里面的内容

    php/oracle: 解析oracle表中的NCLOB,CLOB字段里面的内容 假如你的字段名是:passenger_info 字段类型是:NCLOB/CLOB,在读表的时候,需要将 passeng ...

  2. UGUI加载图片优化方法之一:打包成图集

    打包后的: 直接改变sprite中的packing tag,相同的packing tag就是同一张图集中的.改完运行会自动帮你打包

  3. JavaScript 获取输入时的光标位置及场景问题

    前言 在输入编辑的业务场景中,可能会需要在光标当前的位置或附近显示提示选项.比如社交评论中的@user功能,要确保提示的用户列表总是出现在@字符右下方,又或者是在自定义编辑器中 autocomplet ...

  4. rsync 实现断点续传

    Linux 主机之间即时传送文件,scp命令大家都很熟悉但当要传送的文件较大,过程中如果网络中断了,就比较悲剧了.这时候可以考虑使用rsync命令替代scp,实现断点续传文件. 试验:rsync使用 ...

  5. PowerDesigner 学习

    1. 创建主键,不能为空,递增列,唯一约束(key 通过唯一索引) 数据库-->根据数据库的改动更新model  或者 根据修改后的model (比如新添加的字段)产生新sql代码 2.powe ...

  6. vmware centos 连网方式

    1.自动获取IP地址 虚拟机使用桥接模式,相当于连接到物理机的网络里,物理机网络有DHCP服务器自动分配IP地址. #dhclient 自动获取ip地址命令 #ifconfig 查询系统里网卡信息,i ...

  7. winform中DataGridView使用DataGridViewCheckBoxColumn实现RadioBox单选功能

    private void dgvMaterial_CellContentClick(object sender, DataGridViewCellEventArgs e) { ; i < dgv ...

  8. 剑指offer--19.重建二叉树

    先序:根>左>右 中序:左>根>右 后序:左>右>根 e.g. {1,2,4,7,3,5,6,8} {4,7,2,1,5,3,8,6} 先序第一个元素是根节点,在中 ...

  9. Python 中的几种矩阵乘法 np.dot, np.multiply, *

    使用array时,运算符 * 用于计算数量积(点乘),函数 dot() 用于计算矢量积(叉乘).使用matrix时,运算符 * 用于计算矢量积,函数 multiply() 用于计算数量积. 下面是使用 ...

  10. BZOJ5319: [Jsoi2018]军训列队

    BZOJ5319: [Jsoi2018]军训列队 https://lydsy.com/JudgeOnline/problem.php?id=5319 分析: 易知把所有人按原本的顺序放到\([K,K+ ...