在Qt5中显示FFmpeg帧的最佳/最简单的方法

Best / simplest way to display FFmpeg frames in Qt5

本文关键字:最佳 最简单 方法 Qt5 显示 FFmpeg      更新时间:2023-10-16

我需要在Qt小部件上显示ffmpeg帧。我知道QtFFmpegWrapper,但它似乎已经过时了。我尝试使用memcpy()将数据从RGB ffmpeg帧复制到QImage,并在其中遇到未经处理的异常。

QImage lastFrame;
lastFrame = QImage( screen_w, screen_h, QImage::Format_RGB888 );
for( int y = 0; y < screen_h; ++y )
    memcpy( lastFrame.scanLine(y),
            frameRGB -> data[0] + y * frameRGB -> linesize[0],
            screen_w * 3 );

我尝试了sws_getContext()sws_getCachedContext()AV_PIX_FMT_BGR24AV_PIX_FMT_RGB24 ffmpeg处理的所有部分。所有 ffmpeg 代码都来自流行的教程,并且可以很好地与 SDLPIX_FMT_YUV420P 一起使用。

有什么想法吗?也许这不是在Qt小部件上显示ffmpeg帧的最佳/最简单的方法?

编辑。

好的,我将 Murat Şeker 的解决方案与QImage::copy()一起使用,但现在QImage::isNull()返回true

我的一些 ffmpeg 代码:

out_buffer = (uint8_t*)av_malloc( avpicture_get_size( AV_PIX_FMT_RGB32, 
                                  codecCtx -> width, codecCtx -> height ));
avpicture_fill((AVPicture *)frameRGB, out_buffer, AV_PIX_FMT_RGB32,
               codecCtx -> width, codecCtx -> height);
img_convert_ctx = sws_getContext( codecCtx -> width, codecCtx -> height, 
                                  codecCtx -> pix_fmt, codecCtx -> width, 
                                  codecCtx -> height, AV_PIX_FMT_RGB32, 
                                  SWS_BICUBIC, NULL, NULL, NULL );
/* ... */
if( got_picture ){
    sws_scale( img_convert_ctx, (const uint8_t* const*)frame -> data,
               frame -> linesize, 0, codecCtx -> height, frameRGB -> data,
               frameRGB -> linesize );
    QImage imgFrame = QImage( frameRGB -> data[0], frameRGB -> width,
                              frameRGB -> height, frameRGB -> linesize[0],
                              QImage::Format_RGB32 ).copy();
    if( imgFrame.isNull()){} // true
    // But I can write this frame to hard disk using BITMAPFILEHEADER
    SaveBMP( frameRGB, codecCtx -> width, codecCtx -> height ); // it works
}

以下内容对我有用:

QImage frame = QImage(avFrame->data[0], avFrame->width, avFrame->height,
                      avFrame->linesize[0], QImage::Format_RGB32)
                     .copy();