通过像素格式转换将 AVFrame 转换为 QImage

时间:2021-06-19 15:29:52

标签: c++ c qt ffmpeg

我需要在 QT 应用程序中将视频帧提取为图像。我事先不知道源视频/帧(yuv、rgb ...)的像素格式,但我需要获得可靠的图像像素格式,以便我以后可以一致地处理图像。 我正在使用 ffmpeg 库来获取已经解码的帧。我试图避免使用已弃用的函数,我需要优化速度。

我尝试实现这个例子:https://stackoverflow.com/a/42615610/7360943 which

  1. 将原始帧转换为 rgb 帧

  2. 根据第二帧的数据创建一个 QImage。

但是,这有两个主要问题:它使用了不推荐使用的 avpicture_alloc 函数,并且不释放分配的内存,这导致我的应用程序很快崩溃,使用越来越多的 RAM,直到需要拍摄数千张图像时它才会崩溃。我还没有找到独立解决这两个问题的方法,因为我不知道用什么来代替 avpicture_alloc,如果我使用 avpicture_free,它实际上会释放 QImage 底层的数据,这会破坏 QImage。

我尝试了以下方法,将 QImage 预分配的数据直接传递给 sws_scale,大多数情况下效果很好

// we will convert the original color format to rgb24
SwsContext* img_convert_ctx = sws_getContext(
                                 pFrame->width,
                                 pFrame->height,
                                 (AVPixelFormat)pFrame->format,
                                 pFrame->width,
                                 pFrame->height,
                                 AV_PIX_FMT_RGB24,
                                 SWS_BICUBIC, NULL, NULL, NULL);

QImage image(pFrame->width,
             pFrame->height,
             QImage::Format_RGB888);

int rgb_linesizes[8] = {0};
rgb_linesizes[0] = 3*pFrame->width;

sws_scale(img_convert_ctx,
            pFrame->data,
            pFrame->linesize, 0,
            pFrame->height,
            (uint8_t *[]){image.bits()},
            rgb_linesizes);

ffmpeg::sws_freeContext(img_convert_ctx);

问题在于,对于某些特定视频,它会输出一些看起来有点黑白的奇怪图像(并且似乎显示输入宽度和输出宽度之间的偏移量为 1... ? 我一直无法完全解释是什么导致了这种情况): 查看参考图片,它应该是什么样子:

See the reference image, how it should look

以及有问题的灰色图像:

problematic grey-is image

那么我的代码中有什么问题导致它在大多数情况下表现良好,但在某些特定情况下却无法按预期工作?否则怎么可能做到这一点?

1 个答案:

答案 0 :(得分:0)

我最后想通了,使用手动分配的缓冲区,这不是很干净的 C++ 代码,但运行速度更快,而且没有弃用的调用。无法将 image.bits 直接传递给 sws_scale,因为 QImages 最小 32 位对齐(https://doc.qt.io/qt-5/qimage.html#scanLine),这意味着根据图像宽度,每行末尾的内存中有“空白空间”,这sws_scale 不跳过/不考虑。太糟糕了,因为我们现在在sws_scale和memcpy中有两个内存复制操作,而不是一个,但我还没有找到更好的方法。

我仍然有缓冲区分配大小的问题,需要至少 64 个额外字节,我无法理解,否则我们有时会遇到分段错误。这可能是由于 memcpy 的工作方式,复制整个 32 或 64 字节块......但无论如何这里是新的实现:

(注意:我在专用命名空间下导入 ffmpeg 函数,在每次调用之前解释 ffmpeg::)

QImage getQImageFromFrame(const ffmpeg::AVFrame* pFrame) const
{
    // first convert the input AVFrame to the desired format

    ffmpeg::SwsContext* img_convert_ctx = ffmpeg::sws_getContext(
                                     pFrame->width,
                                     pFrame->height,
                                     (ffmpeg::AVPixelFormat)pFrame->format,
                                     pFrame->width,
                                     pFrame->height,
                                     ffmpeg::AV_PIX_FMT_RGB24,
                                     SWS_BICUBIC, NULL, NULL, NULL);
    if(!img_convert_ctx){
        qDebug() << "Failed to create sws context";
        return QImage();
    }

    // prepare line sizes structure as sws_scale expects
    int rgb_linesizes[8] = {0};
    rgb_linesizes[0] = 3*pFrame->width;

    // prepare char buffer in array, as sws_scale expects
    unsigned char* rgbData[8];
    int imgBytesSyze = 3*pFrame->height*pFrame->width;
    // as explained above, we need to alloc extra 64 bytes
    rgbData[0] = (unsigned char *)malloc(imgBytesSyze+64); 
    if(!rgbData[0]){
        qDebug() << "Error allocating buffer for frame conversion";
        free(rgbData[0]);
        ffmpeg::sws_freeContext(img_convert_ctx);
        return QImage();
    }
    if(ffmpeg::sws_scale(img_convert_ctx,
                pFrame->data,
                pFrame->linesize, 0,
                pFrame->height,
                rgbData,
                rgb_linesizes)
            != pFrame->height){
        qDebug() << "Error changing frame color range";
        free(rgbData[0]);
        ffmpeg::sws_freeContext(img_convert_ctx);
        return QImage();
    }

    // then create QImage and copy converted frame data into it

    QImage image(pFrame->width,
                 pFrame->height,
                 QImage::Format_RGB888);

    for(int y=0; y<pFrame->height; y++){
        memcpy(image.scanLine(y), rgbData[0]+y*3*pFrame->width, 3*pFrame->width);
    }

    free(rgbData[0]);
    ffmpeg::sws_freeContext(img_convert_ctx);
    return image;
}