需求數組
建立一個BGR24的AVFrame幀,用於YUV420轉換BGR24幀ide
代碼函數
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);ui
舊版本函數
int avpicture_fill(AVPicture *picture, uint8_t *ptr,
int pix_fmt, int width, int height);spa
這個函數的使用本質上是爲已經分配的空間的結構體AVPicture掛上一段用於保存數據的空間,這個結構體中有一個指針數組data[4],掛在這個數組裏。通常咱們這麼使用:
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是用於存緩衝數據的。
好,如今讓咱們來看一下tutorials裏常出現的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);
這個函數的做用是給 picture掛上存放數據的代碼。在對幀數據進行scale以前,對於接受數據的picture(等同AVFrame)要用av_frame_alloc()初始化,但AVFrame::data須要手動初始化,即掛上內存,在scale的時候是直接在data裏寫入數據的。但在接收解碼數據時,只須要av_frame_alloc(),不用手動掛內存
2.AVFrame的內存釋放問題
在用AVFrame循環接受視頻的幀數據的時候,或者批量讀取圖片量比較大的時候,不釋放AVFrame會報指針越界錯誤,在我添加了av_free()並釋放了AVFrame指針後,發現報錯時間延後了,但任然有指針越界致使的報錯,調試後發現,av_free()並無釋放AVFrame中data[x]指向的 數據,僅僅是把data自己指向的數據釋放了,但其做爲二級指針指向的數據跳過了,須要手動釋放,添加 av_free(AVFrame->data[0])後問題解決。
總結
av_free( AVFrame* ) 對應 av_frame_alloc()
av_free( AVFrame->data[0] ) 或者av_free( ptr* ) 對應 avpicture_fill 函數或者 avcodec_encode_video2()
調試