简体   繁体   English

在C中为ffmpeg自定义实时输入

[英]Custom real-time input for ffmpeg in C

I'm implementing a custom io with avformat_alloc_context and avio_alloc_context to be able to read the output of another function in real-time. 我正在使用avformat_alloc_context和avio_alloc_context实现一个自定义io,以便能够实时读取另一个函数的输出。 A buffer is populated by this function in a boost asio thread, while ffmpeg is reading this buffer from another thread. 此函数在boost asio线程中填充一个缓冲区,而ffmpeg正在从另一个线程读取此缓冲区。

I initialise an io buffer where this function writes into, and which ffmpeg reads: 我初始化一个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);

Why i do the memset is explained here . 为什么我做的memset 这里解释 Then to test out the pointer address I did: 然后测试一下我做的指针地址:

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);

For reference 以供参考

typedef struct {
    uint8_t *ptr;
    size_t size;
} BufferData;

The read function 读取功能

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;
}

And the result will be: 结果将是:

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

Is it a normal behaviour that the buffer data ptr is changed by avformat_open_input? avformat_open_input更改缓冲区数据ptr是正常现象吗? since I would like to keep the initial pointer address given I am using it in another function, and have malloced it the required memory. 因为我想保留给定的初始指针地址,所以要在另一个函数中使用它,并已为它分配了所需的内存。

It was something to do with my read function, nothing to do with the avformat_open_input. 这与我的读取功能有关,与avformat_open_input没有关系。 I was writing over the pointer as per described here (see read_packet function). 我正在按照此处所述覆盖指针(请参见read_packet函数)。 After thinking about it more yes it made sense to do, given the the read function should increment the pointer to the beginning of the next frame. 考虑了更多的是之后,这样做是有意义的,因为read函数应该将指针增加到下一帧的开头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM