FFmepg--内存模型:avio_alloc_context分配上下文

功能:

内存IO模式:avio_alloc_context():
自定义读写文件方式,打开文件

api

// 自定义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读取文件:
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 读取文件:

	AVFormatContext *format_ctx = avformat_alloc_context();
	
	int ret = avformat_open_input(&format_ctx, in_file, NULL, NULL);

  • 10
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

八月的雨季997

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值