c++ - FFmpeg 解码和转换 RGB sws_scale 错误

标签 c++ ffmpeg yuv libavcodec

我的代码有问题。

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

最佳答案

ffmpeg 文档涵盖了它:

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

AVFrame* av_frame_alloc ( void )

Allocate an AVFrame and set its fields to default values.

The resulting struct must be freed using av_frame_free().

Returns An AVFrame filled with default values or NULL on failure. Note this only allocates the AVFrame itself, not the data buffers. Those must be allocated through other means, e.g. with av_frame_get_buffer() or manually.



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

int av_frame_get_buffer ( AVFrame * frame, int align ) Allocate new buffer(s) for audio or video data.

The following fields must be set on frame before calling this function:

format (pixel format for video, sample format for audio) width and height for video nb_samples and channel_layout for audio This function will fill AVFrame.data and AVFrame.buf arrays and, if necessary, allocate and fill AVFrame.extended_data and AVFrame.extended_buf. For planar formats, one buffer will be allocated for each plane.

关于c++ - FFmpeg 解码和转换 RGB sws_scale 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54120723/

相关文章:

c# - ffmpeg 图像和音频转视频

php - 安全地使用 PHP exec 函数

android - 摄像头 2,增加 FPS

android - 在 c++ (android-ndk) 中从 YUV 转换为 RGB

c++ - 使用 share_from_this 而不是 this 指针

c++ - SDL2 - 找不到 lsdlmain 和 lsdl。代码块

c++ - 在 Windows、Visual C++ 2008 上安装 LEX/YACC 或 flex/bison

video - 如何将两个 ffmpeg 命令组合在一起 - 添加 Intro Outro 和介于两者之间的图像

ios - YUV-NV12 video buffer的y平面和uv平面的延续

c++ - boost::flyweight 作为 std::map 中的值