十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
說明FFmepg3.4版本
創(chuàng)新互聯(lián)秉承實(shí)現(xiàn)全網(wǎng)價(jià)值營銷的理念,以專業(yè)定制企業(yè)官網(wǎng),成都網(wǎng)站建設(shè)、做網(wǎng)站,成都微信小程序,網(wǎng)頁設(shè)計(jì)制作,移動(dòng)網(wǎng)站建設(shè),全網(wǎng)營銷推廣幫助傳統(tǒng)企業(yè)實(shí)現(xiàn)“互聯(lián)網(wǎng)+”轉(zhuǎn)型升級(jí)專業(yè)定制企業(yè)官網(wǎng),公司注重人才、技術(shù)和管理,匯聚了一批優(yōu)秀的互聯(lián)網(wǎng)技術(shù)人才,對(duì)客戶都以感恩的心態(tài)奉獻(xiàn)自己的專業(yè)和所長(zhǎng)。
需求
????????創(chuàng)建一個(gè)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);
這個(gè)函數(shù)的使用本質(zhì)上是為已經(jīng)分配的空間的結(jié)構(gòu)體AVPicture掛上一段用于保存數(shù)據(jù)的空間,這個(gè)結(jié)構(gòu)體中有一個(gè)指針數(shù)組data[4],掛在這個(gè)數(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。這個(gè)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);
這個(gè)函數(shù)的作用是給 picture掛上存放數(shù)據(jù)的代碼。在對(duì)幀數(shù)據(jù)進(jìn)行scale之前,對(duì)于接受數(shù)據(jù)的picture(等同AVFrame)要用av_frame_alloc()初始化,但AVFrame::data需要手動(dòng)初始化,即掛上內(nèi)存,在scale的時(shí)候是直接在data里寫入數(shù)據(jù)的。但在接收解碼數(shù)據(jù)時(shí),只需要av_frame_alloc(),不用手動(dòng)掛內(nèi)存
2.AVFrame的內(nèi)存釋放問題
在用AVFrame循環(huán)接受視頻的幀數(shù)據(jù)的時(shí)候,或者批量讀取圖片量比較大的時(shí)候,不釋放AVFrame會(huì)報(bào)指針越界錯(cuò)誤,在我添加了av_free()并釋放了AVFrame指針后,發(fā)現(xiàn)報(bào)錯(cuò)時(shí)間延后了,但任然有指針越界導(dǎo)致的報(bào)錯(cuò),調(diào)試后發(fā)現(xiàn),av_free()并沒有釋放AVFrame中data[x]指向的 數(shù)據(jù),僅僅是把data本身指向的數(shù)據(jù)釋放了,但其作為二級(jí)指針指向的數(shù)據(jù)跳過了,需要手動(dòng)釋放,添加 av_free(AVFrame->data[0])后問題解決。
總結(jié)???????
av_free( AVFrame* )????????????????????????????????????????????? 對(duì)應(yīng)??? av_frame_alloc()???
av_free( AVFrame->data[0] )? 或者av_free( ptr* )? 對(duì)應(yīng)?? avpicture_fill 函數(shù)或者 avcodec_encode_video2()
????