Best / simplest way to display FFmpeg frames in Qt5

2.7k Views Asked by At

I need to display ffmpeg frames on Qt widget. I know about QtFFmpegWrapper, but it seems outdated. I tried to use memcpy() to copy data from RGB ffmpeg frame to QImage and got unhandled exception inside it.

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 );

I tried sws_getContext() and sws_getCachedContext(), AV_PIX_FMT_BGR24 and AV_PIX_FMT_RGB24 in all parts of ffmpeg processing. All ffmpeg code is from popular tutorials and works fine with SDL and PIX_FMT_YUV420P.

Any ideas? Maybe it's not the best/simplest way to display ffmpeg frames on Qt widget?

Edit.

Ok, I used Murat Şeker's solution with QImage::copy() but now QImage::isNull() returns true.

Some of my ffmpeg code:

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
}
1

There are 1 best solutions below

2
On BEST ANSWER

The following works for me :

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