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);
相关推荐
落淼喵_G8 小时前
ffmpeg转化mp3至wav格式
ffmpeg
jndingxin1 天前
瑞芯微算法环境搭建(1)------编译ffmpeg
ffmpeg
彷徨而立2 天前
【FFmpeg】销毁解码器时,必须清理剩余帧吗?
ffmpeg
骄傲的心别枯萎2 天前
项目1:FFMPEG推流器讲解(五):FFMPEG时间戳、时间基、时间转换的讲解
ffmpeg·音视频·视频编解码·时间戳·rv1126
彷徨而立2 天前
【FFmpeg】HW 解码器销毁时,资源回收顺序
ffmpeg
彷徨而立3 天前
【FFmpeg】如何判断 HW解码器输出的是 硬件帧?
ffmpeg
派阿喵搞电子3 天前
基于ffmpeg库,在AGX上编译jetsonFFmpeg库带有硬件加速的h264_nvmpi视频编解码器
ffmpeg·视频编解码
彷徨而立3 天前
【FFmpeg】HW解码器输出 硬件帧 or 软件帧
ffmpeg
长沙红胖子Qt3 天前
FFmpeg开发笔记(十三):ffmpeg采集麦克风音频pcm重采样为aac录音为AAC文件
笔记·ffmpeg·音视频
feiyangqingyun3 天前
全网首发/Qt结合ffmpeg实现rist推拉流/可信赖的互联网流媒体协议/跨平台支持各个系统
qt·ffmpeg·rist推拉流