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

标签 c++ c qt ffmpeg

我需要在我的 QT 应用程序中将视频帧提取到图像中。我事先不知道源视频/帧(yuv、rgb ...)的像素格式,但我需要获得可靠的图像像素格式,以便以后可以始终如一地处理图像。
我正在使用 ffmpeg 库来获取已经解码的帧。我试图避免使用不推荐使用的函数,并且我需要优化速度。
我尝试实现这个例子:https://stackoverflow.com/a/42615610/7360943哪个

  • 将原始帧转换为 rgb 帧
  • 从第二帧的数据创建一个 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
    那么我的代码中的问题是什么导致它在大多数情况下表现良好,但在某些特定情况下无法按预期工作?或者如何才能做到这一点?

    最佳答案

    最后我想通了,使用手动分配的缓冲区,这不是很干净的 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 字节 block ......但无论如何,这是新的实现:
    (注意:我在专用命名空间下导入 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;
    }
    

    关于c++ - 通过像素格式转换将 AVFrame 转换为 QImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68048292/

    相关文章:

    c - 无法在 OpenGL 中设置颜色

    c - 使用 CV_THRESH_OTSU 返回 cvThreshold 的值

    c++ - 程序崩溃而没有错误消息

    qt - 无需用户交互即可启动 Qt 应用程序

    c++ - tolua++:向已导出到 Lua 的 C++ 类添加 lua 函数

    c++ - 读取txt文件的行和列的最佳方法

    更改 argv 后的 C 段错误

    qt - Ubuntu 的原生 gui 工具包,过去、现在和 future

    C++:如果用户输入的数字错误,则要求用户输入新数字

    c++ - Windows Python C 扩展仅适用于我自己的 Python 构建(使用 VC++ 2008 Express 的 32 位构建)