我知道,为时已晚,但也许有人会发现它有用。从here我得到了做同样转换的线索,看起来有点短。
所以我创建了 QImage,它被重复用于每个解码帧:
QImage img( width, height, QImage::Format_RGB888 );
创建帧RGB:
frameRGB = av_frame_alloc();
//Allocate memory for the pixels of a picture and setup the AVPicture fields for it.
avpicture_alloc( ( AVPicture *) frameRGB, AV_PIX_FMT_RGB24, width, height);
第一帧解码后,我以这种方式创建转换上下文 SwsContext(它将用于所有下一帧):
mImgConvertCtx = sws_getContext( codecContext->width, codecContext->height, codecContext->pix_fmt, width, height, AV_PIX_FMT_RGB24, SWS_BICUBIC, NULL, NULL, NULL);
最后对每一个解码的帧进行转换:
if( 1 == framesFinished && nullptr != imgConvertCtx )
{
//conversion frame to frameRGB
sws_scale(imgConvertCtx, frame->data, frame->linesize, 0, codecContext->height, frameRGB->data, frameRGB->linesize);
//setting QImage from frameRGB
for( int y = 0; y < height; ++y )
memcpy( img.scanLine(y), frameRGB->data[0]+y * frameRGB->linesize[0], mWidth * 3 );
}
详情请参阅link。