我使用avformat_alloc_context和avio_alloc_context实现自定义io,以便能够实时读取另一个函数的输出。此函数在boost asio线程中填充缓冲区,而ffmpeg正在从另一个线程读取此缓冲区。
我初始化此函数写入的io缓冲区,以及ffmpeg读取的内容:
BufferData input_buffer = {0};
input_buffer.size = 65536;
input_buffer.ptr = (uint8_t *) av_malloc(buf_size);
memset(input_buffer.ptr,'0',100);
fprintf(stdout, "initialisation: buffer pointer %p buffer data pointer: %p\n", &input_buffer, input_buffer.ptr);
为什么我要做memset is explained here。 然后测试我做的指针地址:
BufferData * decode_buffer;
decode_buffer->size = 65536;
decode_buffer->ptr = (uint8_t *) av_malloc(decode_buffer->size);
AVIOContext * av_io_ctx = avio_alloc_context(decode_buffer->ptr, decode_buffer->size, 0, &input_buffer, &read_function, NULL, NULL);
AVFormatContext *av_fmt_ctx = avformat_alloc_context();
av_fmt_ctx->pb = av_io_ctx;
BufferData * tmpPtr = (BufferData * ) video_input_file->av_io_ctx->opaque;
fprintf(stdout, "video decoder before: buffer pointer %p, buffer data pointer: %p\n", tmpPtr, tmpPtr->ptr);
open_res = avformat_open_input(&av_fmt_ctx, "anyname", in_fmt, options ? &options : NULL);
fprintf(stdout, "video decoder after: buffer pointer %p, buffer data pointer: %p\n", tmpPtr, tmpPtr->ptr);
供参考
typedef struct {
uint8_t *ptr;
size_t size;
} BufferData;
读取功能
static int read_function(void* opaque, uint8_t* buf, int buf_size) {
BufferData *bd = (BufferData *) opaque;
buf_size = FFMIN(buf_size, bd->size);
memcpy(buf, bd->ptr, buf_size);
bd->ptr += buf_size; //This seemed to cause the problem
bd->size -= buf_size;
return buf_size;
}
结果将是:
initialisation: buffer pointer 0x7f2c4a613620, buffer data pointer: 0x7f2c48c56040
video decoder before: buffer pointer 0x7f2c4a613620, buffer data pointer: 0x7f2c48c56040
video decoder after: buffer pointer 0x7f2c4a613620, buffer data pointer: 0x7f2c49e24b50
通过avformat_open_input更改缓冲区数据ptr是否正常?因为我想保留初始指针地址,因为我在另一个函数中使用它,并且已经将它所需的内存磁盘化了。
答案 0 :(得分:0)
这与我的阅读功能有关,与avformat_open_input无关。我把指针写成per described here(参见read_packet函数)。在考虑之后更多是的,这是有意义的,因为read函数应该将指针递增到下一帧的开头。