// 不要用第四個參數(shù)傳自定的數(shù)據(jù)枷餐,當(dāng)av_read_frame的時候會出問題哨坪,無限循環(huán)
avio_ctx = avio_alloc_context(avio_ctx_buffer, avio_ctx_buffer_size, 0, NULL, read_packet, NULL, NULL);
/*
avio_alloc_context開頭會讀取部分?jǐn)?shù)據(jù)探測流的信息,不會全部讀取悦穿,除非設(shè)置的緩存過大
av_read_frame會在讀幀的時候調(diào)用avio_alloc_context中的read_packet方法取流數(shù)據(jù)向瓷,每隔avio_ctx_buffer_size調(diào)用一次,直至讀完
*/
/*正確方式*/
struct buffer_data
{
uint8_t *ptr; /* 文件中對應(yīng)位置指針 */
size_t size; ///< size left in the buffer /* 文件當(dāng)前指針到末尾 */
};
// 重點根竿,自定的buffer數(shù)據(jù)要在外面這里定義
struct buffer_data bd = {0};
//用來將內(nèi)存buffer的數(shù)據(jù)拷貝到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);
/* 讀內(nèi)存數(shù)據(jù) */
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;
/* 打開內(nèi)存緩存文件, 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; /* 文件中對應(yīng)位置指針 */
size_t size; ///< size left in the buffer /* 文件當(dāng)前指針到末尾 */
};
//用來將內(nèi)存buffer的數(shù)據(jù)拷貝到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);
/* 讀內(nèi)存數(shù)據(jù) */
//使用了第四個參數(shù)傳buffer陵像,雖然可以獲得流的信息,但是讀幀的時候會無限循環(huán)
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;
/* 打開內(nèi)存緩存文件, 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;
}