FFmepg--内存IO模式

功能:

内存IO模式:avio_alloc_context():

自定义读写文件方式,打开文件

api

cpp 复制代码
// 自定义IO
AVIOContext * avio_alloc_context (
	unsigend char * buffer;
	int buffer_size;
	void  *opaque;
	int (*read_packet)(void * opaque, uint8_t * buf,int buf_size);
	int (*write_packet)(void * opaque,uint8_t * buf, int buf_size);
	int64_t (*seek)(void *opaque, int64_t offset, int whence));
)

opaque: 指向文件

buffer ⽤作FFmpeg输⼊时,由⽤户负责向 buffer 中填充数据,FFmpeg取⾛数据。

buffer ⽤作FFmpeg输出时,由FFmpeg负责向 buffer 中填充数据,⽤户取⾛数据。

write_flag是缓冲区读写标志,读写的主语是指FFmpeg。

write_flag 为1时, buffer ⽤于写,即作为FFmpeg输出。

write_flag 为0时, buffer ⽤于读,即作为FFmpeg输⼊。

read_packet和write_packet是函数指针,指向⽤户编写的回调函数。

seek也是函数指针,需要⽀持seek时使⽤。 可以类⽐fseek的机制

buffer_size > read_packet: buf_size

avio_alloc_context读取文件:
  1. 命令行输入:输入文件(mp3/aac)输出文件(pcm)
  2. 自定义IO : avdio_alloc_context
  3. 绑定自定义IO: avformate_open_input

code

avio_alloc_context读取文件:
cpp 复制代码
static int read_packet(void *opaque,uint8_t *buf,int buf_size)
{
	FILE *in_file = (FILE *)opaque;
	int read_size = fread(buf,1,buf_size,in_file);
	printf("read_packet read_size:%d,buf_size:%d\n",read_size,buf_size);
	if(read.size <= 0)
		return AVERROR_EOF;
	return read_size;
}
int main()
{
	FILE *in_file = NULL;
	 in_file = fopen(in_file_name, "rb");
	// 自定义IO 
	uint8_t * io_buff = av_malloc(BUF_SIZE); 
	
	AVIOContext *avio_ctx = avio_alloc_context(io_buff,BUF_SIZE,0, (void *)in_file, read_packet, NULL);
	
	AVFormatContext * format_ctx = avformat_alloc_context();

	// struct AVFormatContext: 成员 AVIOContext *pb
	AVIOContext *pb = avio_ctx;
	avformat_open_input(&format_ctx,NULL,NULL,NULL);
}
avformat_alloc_context 读取文件:
cpp 复制代码
	AVFormatContext *format_ctx = avformat_alloc_context();
	
	int ret = avformat_open_input(&format_ctx, in_file, NULL, NULL);
相关推荐
betazhou1 天前
mysql shell8.4.10+mysql 8.4.10搭建MySQL replicaset主从
数据库·mysql·ffmpeg·replicaset·mysql shell
VidDown2 天前
# VP9 还是 H.265?一次压缩选型翻车后的复盘
ffmpeg·音视频·h.265·视频编解码·视频·vp9
heqingchun162 天前
RK3568 交叉编译与部署:MPP + FFmpeg 硬解全流程
ffmpeg·视频编解码
云贝贝贝3 天前
OceanBase 生产运维 7 个高频现场:从连不上到误删兜底
运维·ffmpeg·oceanbase
≮傷£≯√3 天前
ffmpeg 2个图片合并为视频
ffmpeg·音视频
zone_z6 天前
07 · 等待事件与 OWI 方法:从 OSW 到 BUG 的完整破案实录
linux·数据库·oracle·ffmpeg·bug·troubleshooting
'2067 天前
**写了个自动整理下载视频的脚本:按片名归档、自动重命名、生成目录索引
开发语言·python·ffmpeg·音视频·cctv
殷忆枫7 天前
嵌入式Linux下基于FFmpeg实现MP4解码为JPG图片(SSD202D交叉编译实战)
linux·运维·ffmpeg
≮傷£≯√8 天前
图片合并成视频 img2video ffmpeg
ffmpeg·音视频
HY小宝F9 天前
树莓派 FFmpeg 实战笔记(二):命令行、源码编译与硬件编码的真相
学习·职场和发展·ffmpeg