溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

FFmpeg avformat_open_input函數(shù)剖析以及問題

發(fā)布時間:2020-03-30 14:46:26 來源:網(wǎng)絡 閱讀:1578 作者:fengyuzaitu 欄目:軟件技術(shù)

函數(shù)調(diào)用邏輯
avformat_open_input
?????? init_input
??????????? av_probe_input_buffer2
??????????????? av_probe_input_format3
??????????????????????read_header


簡介
avformat_open_input函數(shù)初始化AVFormatContext結(jié)構(gòu)體。其中在初始化AVIOContext結(jié)構(gòu)體參數(shù)中調(diào)用init_input函數(shù),而它會默認調(diào)用av_probe_input_buffer2填充AVIOContext結(jié)構(gòu)體參數(shù)。一般情況下,可以優(yōu)先調(diào)用av_probe_input_buffer函數(shù),填充AVIOContext結(jié)構(gòu)體參數(shù),然后賦值給AVFormatContext結(jié)構(gòu)體中的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)
根據(jù)av_probe_input_buffer會調(diào)用av_probe_input_buffer2,包裹了一層
簡化代碼如下
AVFormatContext* pFormatContext = avformat_alloc_context();
if (avformat_open_input(&FormatContext, "", pInputFormat, NULL) < 0)
根據(jù)實際的測試,關(guān)鍵的地方是AVIOContext的初始化,該結(jié)構(gòu)體將會保存一個讀取網(wǎng)絡數(shù)據(jù)的函數(shù)入口,根據(jù)函數(shù)入口來分析數(shù)據(jù)流。avformat_open_input函數(shù)在目前的
測試結(jié)果是耗時1秒多,這個應該是一個優(yōu)化的方向
av_probe_input_buffer2主要是針對輸入輸出結(jié)構(gòu)體AVIOContext的初始化,如果知道avformat_open_input的賦值內(nèi)容,對各種協(xié)議的讀寫格式的探測,就可以優(yōu)化這一塊代碼。協(xié)議的探測分別有file協(xié)議,rtmp協(xié)議等等,目前在項目中只需要實現(xiàn)文件協(xié)議,而文件協(xié)議應該如何進行讀寫?
調(diào)用ffio_fdopen()函數(shù)創(chuàng)建AVIOContext()結(jié)構(gòu)體并獲取URLContext結(jié)構(gòu)體引用的資源(調(diào)用avio_alloc_context()實現(xiàn))


問題
??? avformat_open_input函數(shù)探測ES流開銷是150毫秒,探測PS流開銷是500毫秒。avformat_open_input函數(shù)里面已經(jīng)實現(xiàn)了av_probe_input_buffer函數(shù)的調(diào)用,去探測AVInputFormat結(jié)構(gòu)體的相關(guān)變量。所以在avformat_open_input函數(shù)之前,調(diào)用av_probe_input_buffer函數(shù)之后,就不會去探測AVInputFormat結(jié)構(gòu)體

優(yōu)化方向
??? 嘗試屏蔽avformat_open_input函數(shù),直接指定碼流的輸入格式pInputFormat,代碼如下:
??????? pInputFormat = av_find_input_format("h364");
??????? pFormatCtx->iformat = pInputFormat;
如果這個時候屏蔽掉avformat_open_input,圖像是條帶狀的,按照參考文獻的說法,該avformat_open_input
函數(shù)就是為了探測AVInputFormat結(jié)構(gòu)體的相關(guān)變量

最終優(yōu)化方案
??? 沒有屏蔽avformat_open_input函數(shù),而是在調(diào)用之前指定AVInputFormat的碼流輸入格式
代碼
??? AVInputFormat* pInputFormat = NULL;
??? AVFormatContext* pFormatContext = NULL;
??? pFormatContext = avformat_alloc_context();
??? pInputFormat = av_find_input_format("h364");
??? if (avformat_open_input(&pFormatContext, "", InputFormat, NULL) < 0)
??? {
??????? av_free(...);
??????? avformat_free_context(...);
??????? fprintf(stdout, "open stream failed!\n");
??? }
??? else
??? {
??????? fprintf(stdout, "open stream success!\n");
??? }


筆記
m_pVideoc->io_ctx = avio_alloc_context(m_pAvioBuf, BUF_SIZE, 0, this, ReadStreamData, NULL, NULL);
if (avformat_open_input(&pFormatCtx, "", piFmt, NULL) < 0)
上面的ReadStreamData實際的用途是在下面打開實時流的時候,如果需要讀取數(shù)據(jù),可以通過ReadStreamData函數(shù)獲取到幀的消息內(nèi)容
而不用通過avformat_open_input函數(shù)的第二個參數(shù)傳遞url

參考
http://blog.csdn.net/leo2007608/article/details/53421528
http://blog.csdn.net/leixiaohua1020/article/details/39759163
http://blog.csdn.net/leixiaohua1020/article/details/44064715


向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI