2016-08-10 36 views
0

我使用avformat_alloc_context和avio_alloc_context實現自定義io,以便能夠實時讀取另一個函數的輸出。這個函數在一個boost asio線程中填充了一個緩衝區,而ffmpeg正在從另一個線程讀取這個緩衝區。ffmpeg的自定義實時輸入C

我初始化一個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 

這是正常的嗎?爲什麼緩衝區數據ptr被avformat_open_input改變?因爲我想保留初始指針地址,因爲我在另一個函數中使用了它,並且將它加入了所需的內存。

回答

0

這與我的閱讀功能有關,與avformat_open_input無關。我正在寫指針per described here(請參閱read_packet函數)。考慮到它後,更多的是這是有道理的,因爲讀函數應該將指針遞增到下一幀的開始。