我正在尝试将视频帧解码为QImage并显示它,但下面的代码将AVFrame转换为QImage会导致内存泄漏。当我禁用下面的代码时,应用程序工作正常,但启用原因使用更多的RAM和时间的香。
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 );