2016-06-28 78 views
1

我想解碼視頻幀到QImage並顯示它,但下面的代碼將AVFrame轉換爲QImage導致內存泄漏。當我禁用下面的代碼時,應用程序可以正常工作,但是可以使用更多的RAM和時間。AVFrame到QImage內存泄漏

AVFrame *frameRGB = av_frame_alloc(); 
    int width = frame->width, height = frame->height; 
    avpicture_alloc((AVPicture *) frameRGB, AV_PIX_FMT_RGB24, width,height); 

    struct SwsContext *convert_ctx=NULL; 
    enum PixelFormat src_pixfmt = (enum PixelFormat)frame->format; 
    enum PixelFormat dst_pixfmt = PIX_FMT_RGB24; 
    convert_ctx = sws_getContext(width, height, src_pixfmt, width, height, dst_pixfmt, 
    SWS_FAST_BILINEAR, NULL, NULL, NULL); 
    sws_scale(convert_ctx,frame->data, frame->linesize,0,height, frameRGB->data, frameRGB->linesize); 

    //decodedQimage = QImage(width, height, QImage::Format_RGB888); 
    for(int y = 0; y < height; ++y){ 
     memcpy(decodedQimage.scanLine(y), frameRGB->data[0]+y * frameRGB->linesize[0], frameRGB->linesize[0]); 
    } 

    av_free(frameRGB); 
    sws_freeContext(convert_ctx); 

這裏decodedQimage是QImage的類型和int它像

 decodedQimage = QImage(outputwidth, outputheight, QImage::Format_RGB888); 

回答