1. 程式人生 > >FFmpeg 將YUV數據轉RGB

FFmpeg 將YUV數據轉RGB

分配內存 () alloc idt img yuv420 init() 復制 ext

void init() //分配兩個Frame,兩段buff,一個轉換上下文
{
 //為每幀圖像分配內存
    m_pFrameYUV = av_frame_alloc();
    m_pFrameRGB = av_frame_alloc();
    int numBytes = avpicture_get_size(AV_PIX_FMT_RGB32, nwidth,nheight);
    m_rgbBuffer = (uint8_t *) av_malloc(numBytes * sizeof(uint8_t));
  // width和heigt為傳入的分辨率的大小
    int yuvSize = nwidth * nheight * 3
/2; m_yuvBuffer = (uint8_t *)av_malloc(yuvSize); //特別註意sws_getContext內存泄露問題, //註意sws_getContext只能調用一次,在初始化時候調用即可,另外調用完後,在析構函數中使用sws_freeContext,將它的內存釋放。 //設置圖像轉換上下文 m_img_convert_ctx = sws_getContext(nwidth, nheight, AV_PIX_FMT_YUV420P, \
                        nwidth, nheight, AV_PIX_FMT_RGB32, SWS_BICUBIC, NULL, NULL, NULL); }
void play(char* pbuff_in,int nwidth,int nheight)
{
//這裏的長度和高度跟之前保持一致
    avpicture_fill((AVPicture *) m_pFrameRGB, m_rgbBuffer, AV_PIX_FMT_RGB32,nwidth, nheight);
    avpicture_fill((AVPicture *) m_pFrameYUV, (uint8_t *)pbuff_in, AV_PIX_FMT_YUV420P, nwidth, nheight);
    //轉換圖像格式,將解壓出來的YUV420P的圖像轉換為RGB的圖像
sws_scale(m_img_convert_ctx, (uint8_t const * const *) m_pFrameYUV->data, m_pFrameYUV->linesize, 0, nheight, m_pFrameRGB->data, m_pFrameRGB->linesize); //把這個RGB數據 用QImage加載 QImage tmpImg((uchar *)m_rgbBuffer,nwidth,nheight,QImage::Format_RGB32); //把圖像復制一份 傳遞給界面顯示 m_mapImage[nWindowIndex] = tmpImg.copy(); }

FFmpeg 將YUV數據轉RGB