簡體   English   中英

通過像素格式轉換將 AVFrame 轉換為 QImage

[英]Converting an AVFrame to QImage with conversion of pixel format

我需要在 QT 應用程序中將視頻幀提取為圖像。 我事先不知道源視頻/幀(yuv、rgb ...)的像素格式,但我需要獲得可靠的圖像像素格式,以便我以后可以一致地處理圖像。 我正在使用 ffmpeg 庫來獲取已經解碼的幀。 我試圖避免使用已棄用的函數,我需要優化速度。

我嘗試實現這個例子: https : //stackoverflow.com/a/42615610/7360943

  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...?我無法完全解釋可能導致此問題的原因):查看參考圖像,它應該是什么樣子:

查看參考圖像,它應該是什么樣子

和有問題的灰色圖像:

有問題的灰色圖像

那么我的代碼中有什么問題導致它在大多數情況下表現良好,但在某些特定情況下卻無法按預期工作? 否則怎么可能做到這一點?

最后我想通了,使用手動分配的緩沖區,這不是很干凈的 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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM