avio_reading.c

<pre name="code" class="cpp">#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavformat/avio.h>
#include <libavutil/file.h>
struct buffer_data {
	uint8_t *ptr;
	size_t size; ///< size left in the buffer
};
static int read_packet(void *opaque, uint8_t *buf, int buf_size) {
	struct buffer_data *bd = (struct buffer_data *) opaque;
	buf_size = FFMIN(buf_size, bd->size);
	printf("ptr:%p size:%zu\n", bd->ptr, bd->size);
	/* copy internal buffer data to buf */
	memcpy(buf, bd->ptr, buf_size);
	bd->ptr += buf_size;
	bd->size -= buf_size;
	return buf_size;
}
int main(int argc, char *argv[]) {
	AVFormatContext *fmt_ctx = NULL;                      //Format I/O context
	AVIOContext *avio_ctx = NULL;                         //ByteStream I/O context
	uint8_t *buffer = NULL, *avio_ctx_buffer = NULL;      //buffer for Inputfile and avio_ctx
	size_t buffer_size, avio_ctx_buffer_size = 4096;      //The buffer size is very important for performance. For protocol                                                              //For a typical size is a cache page, 4kb 
	char *input_filename = NULL;                          //The input file name                       
	int ret = 0;
	struct buffer_data bd = { 0 };                        //The data struct of buffer.
	if (argc != 2) {
		fprintf(stderr, "usage: %s input_file\n"
				"API example program to show how to read from a custom buffer "
				"accessed through AVIOContext.\n", argv[0]);
		return 1;
	}
	input_filename = argv[1];
	
	av_register_all()  //Initialize libavformat and register all muxers,demuxes and protocols 
	/* slurp file content into buffer */
	ret = av_file_map(input_filename, &buffer, &buffer_size, 0, NULL); 
	if (ret < 0)
		goto end;
	/* fill opaque structure used by the AVIOContext read callback */
	bd.ptr = buffer;
	bd.size = buffer_size;
	if (!(fmt_ctx = avformat_alloc_context())) {
		ret = AVERROR(ENOMEM);
		goto end;
	}
	avio_ctx_buffer = av_malloc(avio_ctx_buffer_size);
	if (!avio_ctx_buffer) {
		ret = AVERROR(ENOMEM);
		goto end;
	}
	avio_ctx = avio_alloc_context(avio_ctx_buffer, avio_ctx_buffer_size, 0, &bd,
			&read_packet, NULL, NULL); //Allocate and initialize an AVIOContext for buffered I/0.
	if (!avio_ctx) {
		ret = AVERROR(ENOMEM);
		goto end;
	}
	fmt_ctx->pb = avio_ctx;
	ret = avformat_open_input(&fmt_ctx, NULL, NULL, NULL);//Open an input stream and read the header.
	if (ret < 0) {
		fprintf(stderr, "Could not open input\n");
		goto end;
	}
	ret = avformat_find_stream_info(fmt_ctx, NULL);//Read packets of a media file to get stream information.
	if (ret < 0) {
		fprintf(stderr, "Could not find stream information\n");
		goto end;
	}
	av_dump_format(fmt_ctx, 0, input_filename, 0);//Print detailed information about the input or output format.Such as duration,bitrate, streams,container,programs.
	end: avformat_close_input(&fmt_ctx);
	/* note: the internal buffer could have changed, and be != avio_ctx_buffer */
	if (avio_ctx) {
		av_freep(&avio_ctx->buffer);
		av_freep(&avio_ctx);
	}
	av_file_unmap(buffer, buffer_size);
	if (ret < 0) {
		fprintf(stderr, "Error occurred: %s\n", av_err2str(ret));
		return 1;
	}
	return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值