簡體   English   中英

FFmpeg 解碼並轉換RGB sws_scale 錯誤

[英]FFmpeg decode and convert RGB sws_scale error

我的代碼有問題。

我想將 YUV 轉換為 RGB,但sws_scale總是返回 0 並且數據填充為 00000000..NULL

我嘗試谷歌搜索,但我找不到任何問題。

我需要 AVPicture 還是 QImage? 誰能指出我的問題?

 pVFrame = av_frame_alloc();
 while (av_read_frame(pFormatCtx, &packet) == 0) {
        if (packet.stream_index == VSI) {
            if (bool res = avcodec_send_packet(pVideoCodecCtx, &packet)) {
                printf("avcodec_send_packet failed %d %d %d\n", res, AVERROR(EINVAL), AVERROR(ENOMEM));
            }
            avcodec_receive_frame(pVideoCodecCtx, pVFrame);

            AVFrame* YUV_frame = Con_yuv_YUV420P(pVFrame);
            QFrame->push(Con_yuv_RGB(YUV_frame));
        }
    }
}


AVFrame* Con_yuv_RGB(AVFrame* YUV_frame)
{
AVFrame* RGBFrame = av_frame_alloc();

SwsContext* sws_Context = NULL;
sws_Context = sws_getCachedContext(sws_Context, YUV_frame->width, YUV_frame->height, pVideoCodecCtx->pix_fmt,
    YUV_frame->width, YUV_frame->height, AV_PIX_FMT_RGB32, SWS_BICUBIC, NULL, NULL, NULL);

if (sws_Context == NULL) {
    return false;
}
result = sws_scale(sws_Context, YUV_frame->data, YUV_frame->linesize, 0, (int)YUV_frame->height, RGBFrame->data, RGBFrame->linesize);
if (result < 0)
{
    return false;
}
av_frame_unref(YUV_frame);
if (RGBFrame == NULL) {
    av_frame_unref(RGBFrame);
    return false;
}

sws_freeContext(sws_Context);

return RGBFrame;

問題是什么?

在此處輸入圖片說明

參考鏈接 -https://gist.github.com/nakaly/11eb992ebd134ee08b75e4c67afb5703

http://codefromabove.com/2014/10/ffmpeg-convert-rgba-to-yuv/ ,

sws_scale YUV --> RGB 失真圖像

https://www.gamedev.net/forums/topic/464977-sws_scale-problems/

https://ffmpeg.org/doxygen/2.7/group__libsws.html#gae531c9754c9205d90ad6800015046d74

添加:在此處輸入圖像描述

轉換前。 它必須填充緩沖區。

RGBFrame->width = YUV_frame->width;     RGBFrame->format = AV_PIX_FMT_RGB32;
RGBFrame->height = YUV_frame->height;
int result = av_frame_get_buffer(RGBFrame, 32);
if (result != 0)    return false;

ffmpeg 文檔涵蓋了它:

https://www.ffmpeg.org/doxygen/2.3/group__lavu__frame.html#gac700017c5270c79c1e1befdeeb008b2f

AVFrame* av_frame_alloc ( void )

分配一個 AVFrame 並將其字段設置為默認值。

必須使用 av_frame_free() 釋放生成的結構。

返回一個 AVFrame 填充了默認值或失敗時為 NULL。 請注意,這僅分配 AVFrame 本身,而不是數據緩沖區。 這些必須通過其他方式分配,例如使用 av_frame_get_buffer() 或手動分配。

https://www.ffmpeg.org/doxygen/2.3/group__lavu__frame.html#ga6b1acbfa82c79bf7fd78d868572f0ceb

int av_frame_get_buffer ( AVFrame * frame, int align ) 為音頻或視頻數據分配新的緩沖區。

在調用此函數之前,必須在框架上設置以下字段:

格式(視頻的像素格式,音頻的樣本格式)視頻的寬度和高度 nb_samples 和音頻的 channel_layout 此函數將填充 AVFrame.data 和 AVFrame.buf 數組,並在必要時分配和填充 AVFrame.extended_data 和 AVFrame.extended_buf。 對於平面格式,將為每個平面分配一個緩沖區。

暫無
暫無

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

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