【问题标题】:FFmpeg decode and convert RGB sws_scale errorFFmpeg 解码和转换 RGB sws_scale 错误
【发布时间】:2019-01-10 01:14:47
【问题描述】:

我的代码有问题。

我想将 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;

有什么问题?

enter image description here

参考链接-https://gist.github.com/nakaly/11eb992ebd134ee08b75e4c67afb5703

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

sws_scale YUV --> RGB distorted image,

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

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

添加: enter image description here

转换前。它必须填满缓冲区。

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;

【问题讨论】:

    标签: c++ ffmpeg yuv libavcodec


    【解决方案1】:

    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。为了 平面格式,每个平面分配一个缓冲区。

    【讨论】:

    • 感谢您的回答!我看到了你的其他答案。你是我的偶像。所以我添加了我的代码宽度、高度和格式。和 av_frame_get_buffer 看起来工作正常。但我不能确定它是否完全转换 RGB。如何检查它的 RGBFrame?调试 Frame->data 时字符串中有无效字符。我在问题中添加捕获图像
    • data 是指向指针的指针,因此请确保您正在查看 RGBFrame->data[0]。由于每个通道的像素值可以是 0-255,因此不可能有无效字节。
    • RGBFrame->data [0] 有奇怪的数据.. 意思是转换失败?请多解释。
    • 请展开。有什么奇怪的?
    • 在调试中,RGBFrame->data[0] 显示“字符串中的字符无效”。我猜这个转换成功了。对吗?
    猜你喜欢
    • 2021-11-25
    • 2018-02-19
    • 2021-05-28
    • 1970-01-01
    • 1970-01-01
    • 2020-12-18
    • 2017-04-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多