FFmpeg avformat_open_input簡化方案

簡介
avformat_open_input函數初始化AVFormatContext結構體。其中在初始化AVIOContext結構體參數中調用init_input函數,而它會默認調用av_probe_input_buffer2填充AVIOContext結構體參數。一般情況下,可以優先調用av_probe_input_buffer函數,填充AVIOContext結構體參數,然後賦值給AVFormatContext結構體中的AVIOContext字段

代碼
AVIOContext pIOContext= avio_alloc_context
AVInputFormat
pInputFormat = NULL
av_probe_input_buffer(pIOContext, &pInputFormat, "", NULL, 0, 0)
AVFormatContext* pFormatContext = avformat_alloc_context();
pFormatContext->pb = pIOContext;
if (avformat_open_input(&FormatContext, "", pInputFormat, NULL) < 0)

根據av_probe_input_buffer會調用av_probe_input_buffer2,包裹了一層
簡化代碼如下
AVFormatContext* pFormatContext = avformat_alloc_context();
if (avformat_open_input(&FormatContext, "", pInputFormat, NULL) < 0)

根據實際的測試,關鍵的地方是AVIOContext的初始化,該結構體將會保存一個讀取網絡數據的函數入口,根據函數入口來分析數據流。avformat_open_input函數在目前的
測試結果是耗時1秒多,這個應該是一個優化的方向

av_probe_input_buffer2主要是針對輸入輸出結構體AVIOContext的初始化,如果知道avformat_open_input的賦值內容,對各種協議的讀寫格式的探測,就可以優化這一塊代碼。協議的探測分別有file協議,rtmp協議等等,目前在項目中只需要實現文件協議,而文件協議應該如何進行讀寫?

調用ffio_fdopen()函數創建AVIOContext()結構體並獲取URLContext結構體引用的資源(調用avio_alloc_context()實現)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章