FFmpegavformat_open_input函數(shù)剖析以及問(wèn)題-創(chuàng)新互聯(lián)

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

創(chuàng)新互聯(lián)建站堅(jiān)信:善待客戶(hù),將會(huì)成為終身客戶(hù)。我們能堅(jiān)持多年,是因?yàn)槲覀円恢笨芍档眯刨?lài)。我們從不忽悠初訪(fǎng)客戶(hù),我們用心做好本職工作,不忘初心,方得始終。10年網(wǎng)站建設(shè)經(jīng)驗(yàn)創(chuàng)新互聯(lián)建站是成都老牌網(wǎng)站營(yíng)銷(xiāo)服務(wù)商,為您提供成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、H5網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、品牌網(wǎng)站制作、成都微信小程序服務(wù),給眾多知名企業(yè)提供過(guò)好品質(zhì)的建站服務(wù)。

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

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

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

最終優(yōu)化方案
??? 沒(méi)有屏蔽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í)際的用途是在下面打開(kāi)實(shí)時(shí)流的時(shí)候,如果需要讀取數(shù)據(jù),可以通過(guò)ReadStreamData函數(shù)獲取到幀的消息內(nèi)容
而不用通過(guò)avformat_open_input函數(shù)的第二個(gè)參數(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

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線(xiàn),公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿(mǎn)足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。

網(wǎng)頁(yè)標(biāo)題:FFmpegavformat_open_input函數(shù)剖析以及問(wèn)題-創(chuàng)新互聯(lián)
標(biāo)題URL:http://muchs.cn/article2/djgiic.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、面包屑導(dǎo)航、網(wǎng)站導(dǎo)航、Google用戶(hù)體驗(yàn)關(guān)鍵詞優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作