// 不要用第四个参数传自定的数据,当av_read_frame的时候会出问题,无限循环
avio_ctx = avio_alloc_context(avio_ctx_buffer, avio_ctx_buffer_size, 0, NULL, read_packet, NULL, NULL);
/*
avio_alloc_context开头会读取部分数据探测流的信息,不会全部读取,除非设置的缓存过大
av_read_frame会在读帧的时候调用avio_alloc_context中的read_packet方法取流数据,每隔avio_ctx_buffer_size调用一次,直至读完
*/
/*正确方式*/
struct buffer_data
{
uint8_t *ptr; /* 文件中对应位置指针 */
size_t size; ///< size left in the buffer /* 文件当前指针到末尾 */
};
// 重点,自定的buffer数据要在外面这里定义
struct buffer_data bd = {0};
//用来将内存buffer的数据拷贝到buf
int read_packet(void *opaque, uint8_t *buf, int buf_size)
{
buf_size = FFMIN(buf_size, bd.size);
if (!buf_size)
return AVERROR_EOF;
printf("ptr:%p size:%zu bz%zu\n", bd.ptr, bd.size, buf_size);
/* copy internal buffer data to buf */
memcpy(buf, bd.ptr, buf_size);
bd.ptr += buf_size;
bd.size -= buf_size;
return buf_size;
}
/* 打开前端传来的视频buffer */
int open_input_buffer(uint8_t *buf, int len)
{
unsigned char *avio_ctx_buffer = NULL;
size_t avio_ctx_buffer_size = 32768;
AVInputFormat* in_fmt = av_find_input_format("h265");
bd.ptr = buf; /* will be grown as needed by the realloc above */
bd.size = len; /* no data at this point */
fmt_ctx = avformat_alloc_context();
avio_ctx_buffer = (unsigned char *)av_malloc(avio_ctx_buffer_size);
/* 读内存数据 */
avio_ctx = avio_alloc_context(avio_ctx_buffer, avio_ctx_buffer_size, 0, NULL, read_packet, NULL, NULL);
fmt_ctx->pb = avio_ctx;
fmt_ctx->flags = AVFMT_FLAG_CUSTOM_IO;
/* 打开内存缓存文件, and allocate format context */
if (avformat_open_input(&fmt_ctx, "", in_fmt, NULL) < 0)
{
fprintf(stderr, "Could not open input\n");
return -1;
}
return 0;
}
/*错误方式*/
struct buffer_data
{
uint8_t *ptr; /* 文件中对应位置指针 */
size_t size; ///< size left in the buffer /* 文件当前指针到末尾 */
};
//用来将内存buffer的数据拷贝到buf
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);
if (!buf_size)
return AVERROR_EOF;
printf("ptr:%p size:%zu bz%zu\n", bd->ptr, bd->size, buf_size);
/* copy internal buffer data to buf */
memcpy(buf, bd->ptr, buf_size);
bd->ptr += buf_size;
bd->size -= buf_size;
return buf_size;
}
/* 打开前端传来的视频buffer */
int open_input_buffer(uint8_t *buf, int len)
{
unsigned char *avio_ctx_buffer = NULL;
size_t avio_ctx_buffer_size = 32768;
struct buffer_data bd = { 0 };
AVInputFormat* in_fmt = av_find_input_format("h265");
bd.ptr = buf; /* will be grown as needed by the realloc above */
bd.size = len; /* no data at this point */
fmt_ctx = avformat_alloc_context();
avio_ctx_buffer = (unsigned char *)av_malloc(avio_ctx_buffer_size);
/* 读内存数据 */
//使用了第四个参数传buffer,虽然可以获得流的信息,但是读帧的时候会无限循环
avio_ctx = avio_alloc_context(avio_ctx_buffer, avio_ctx_buffer_size, 0, &bd, read_packet, NULL, NULL);
fmt_ctx->pb = avio_ctx;
fmt_ctx->flags = AVFMT_FLAG_CUSTOM_IO;
/* 打开内存缓存文件, and allocate format context */
if (avformat_open_input(&fmt_ctx, "", in_fmt, NULL) < 0)
{
fprintf(stderr, "Could not open input\n");
return -1;
}
return 0;
}