溫馨提示×

溫馨提示×

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

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

FFmpeg av_image_fill_arrays填充AVFrame數(shù)據(jù)緩沖

發(fā)布時間:2020-07-19 22:33:02 來源:網(wǎng)絡(luò) 閱讀:2790 作者:fengyuzaitu 欄目:編程語言

說明FFmepg3.4版本

需求

????????創(chuàng)建一個BGR24的AVFrame幀,用于YUV420轉(zhuǎn)換BGR24幀


代碼

? AVFrame *pBGRFrame = NULL;
??pBGRFrame = av_frame_alloc();
??uint8_t *pszBGRBuffer = NULL;
??int nBGRFrameSize;
? nBGRFrameSize = av_image_get_buffer_size(AV_PIX_FMT_BGR24, pVideoc->m_pAVCodecContext->width, pVideoc->m_pAVCodecContext->height, 1);
? pszBGRBuffer = (uint8_t*)av_malloc(nBGRFrameSize);
? av_image_fill_arrays(pBGRFrame->data, pBGRFrame->linesize, pszBGRBuffer, AV_PIX_FMT_BGR24, pFrame->width, pFrame->height, 1);


舊版本函數(shù)
int avpicture_fill(AVPicture *picture, uint8_t *ptr,
?????????????????? int pix_fmt, int width, int height);


這個函數(shù)的使用本質(zhì)上是為已經(jīng)分配的空間的結(jié)構(gòu)體AVPicture掛上一段用于保存數(shù)據(jù)的空間,這個結(jié)構(gòu)體中有一個指針數(shù)組data[4],掛在這個數(shù)組里。一般我們這么使用:
1) pFrameRGB=avcodec_alloc_frame();
2) numBytes=avpicture_get_size(PIX_FMT_RGB24, pCodecCtx->width,pCodecCtx->height);
??? buffer=(uint8_t *)av_malloc(numBytes*sizeof(uint8_t));
3) avpicture_fill((AVPicture *)pFrameRGB, buffer, PIX_FMT_RGB24,pCodecCtx->width, pCodecCtx-???????? >height);
以上就是為pFrameRGB掛上buffer。這個buffer是用于存緩沖數(shù)據(jù)的。
好,現(xiàn)在讓我們來看一下tutorials里常出現(xiàn)的pFrame為什么不用fill空間。主要是下面這句:
avcodec_decode_video(pCodecCtx, pFrame, &frameFinished,packet.data, packet.size);


1.int avpicture_fill(AVPicture *picture, const uint8_t *ptr,enum AVPixelFormat pix_fmt, int width, int height);
這個函數(shù)的作用是給 picture掛上存放數(shù)據(jù)的代碼。在對幀數(shù)據(jù)進行scale之前,對于接受數(shù)據(jù)的picture(等同AVFrame)要用av_frame_alloc()初始化,但AVFrame::data需要手動初始化,即掛上內(nèi)存,在scale的時候是直接在data里寫入數(shù)據(jù)的。但在接收解碼數(shù)據(jù)時,只需要av_frame_alloc(),不用手動掛內(nèi)存
2.AVFrame的內(nèi)存釋放問題
在用AVFrame循環(huán)接受視頻的幀數(shù)據(jù)的時候,或者批量讀取圖片量比較大的時候,不釋放AVFrame會報指針越界錯誤,在我添加了av_free()并釋放了AVFrame指針后,發(fā)現(xiàn)報錯時間延后了,但任然有指針越界導致的報錯,調(diào)試后發(fā)現(xiàn),av_free()并沒有釋放AVFrame中data[x]指向的 數(shù)據(jù),僅僅是把data本身指向的數(shù)據(jù)釋放了,但其作為二級指針指向的數(shù)據(jù)跳過了,需要手動釋放,添加 av_free(AVFrame->data[0])后問題解決。
總結(jié)???????
av_free( AVFrame* )????????????????????????????????????????????? 對應(yīng)??? av_frame_alloc()???
av_free( AVFrame->data[0] )? 或者av_free( ptr* )? 對應(yīng)?? avpicture_fill 函數(shù)或者 avcodec_encode_video2()
????


向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