【问题标题】:Converting an AVFrame to QImage with conversion of pixel format通过像素格式转换将 AVFrame 转换为 QImage
【发布时间】:2021-09-03 23:11:37
【问题描述】:

我需要在我的 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... ?我无法完全解释可能导致这种情况的原因): 查看参考图片,它的外观:

以及有问题的灰色图像:

那么,我的代码中的问题是什么导致它在大多数情况下表现良好,但在某些特定情况下无法按预期工作?不然怎么办?

【问题讨论】:

  • 对于您的内存问题 - 请参阅 QImage documentation - 您可以将 cleanuFunction 传递给 QImage ctor。
  • 哦,谢谢,好主意,所以我会传递一个函数,一旦 QImage 被删除,就会释放数据......!

标签: c++ c qt ffmpeg


【解决方案1】:

最后我想通了,使用手动分配的缓冲区,这不是很干净的 C++ 代码,但运行速度更快,而且没有过时的调用。无法将 image.bits 直接传递给 sws_scale,因为 QImages 至少 32 位对齐(@9​​87654321@),这意味着根据图像宽度,每行末尾的内存中有“空白空间”,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;
}

【讨论】:

  • 如果有人能更好地解释 malloc 的麻烦,那就太好了!
  • 可以获取bytesPerLine,直接写入QImage.bits。
  • @VainMan 你是说以某种方式将 bytesPerLine 传递给 sws_scale 吗?
  • 是的。将bytesPerLine 保存为rgb_linesizes,将QImage.bits or scanLine 保存为rgbData,然后将它们传递给sws_scale
猜你喜欢
  • 2018-04-13
  • 1970-01-01
  • 2014-06-27
  • 2016-09-03
  • 1970-01-01
  • 1970-01-01
  • 2018-08-27
  • 1970-01-01
  • 2014-03-11
相关资源
最近更新 更多