首页 > 解决方案 > 未能为 sws_scale 转换正确初始化 AVFrame

问题描述

我正在使用 FFMpeg 解码视频,并想使用 OpenGL 编辑解码的帧,但为了做到这一点,我需要将 AVFrame 中的数据从 YUV 转换为 RGB。

为此,我创建了一个新的 AVFrame:

AVFrame *inputFrame = av_frame_alloc();
AVFrame *outputFrame = av_frame_alloc();
av_image_alloc(outputFrame->data, outputFrame->linesize, width, height, AV_PIX_FMT_RGB24, 1);
av_image_fill_arrays(outputFrame->data, outputFrame->linesize, NULL, AV_PIX_FMT_RGB24, width, height, 1);

创建转换上下文:

struct SwsContext *img_convert_ctx = sws_getContext(width, height, AV_PIX_FMT_YUV420P,
                                                  width, height, AV_PIX_FMT_RGB24,
                                                  0, NULL, NULL, NULL);

然后尝试将其转换为 RGB:

sws_scale(img_convert_ctx, (const uint8_t *const *)&inputFrame->data, inputFrame->linesize, 0, inputFrame->height, outputFrame->data, outputFrame->linesize);

但这会在运行时导致“[swscaler @ 0x123f15000] bad dst image pointers”错误。当我查看 FFMpeg 的源代码时,我发现原因是 outputFrame 的数据没有初始化,但我不明白它应该是怎样的。

我发现的所有现有答案或教程(参见示例)似乎都使用了已弃用的 API,并且不清楚如何使用新的 API。我会很感激任何帮助。

标签: openglffmpeg

解决方案


我是这样称呼的sws_scale

image buf2((buf.w + 15)/16*16, buf.h, 3);
sws_scale(sws_ctx, (const uint8_t * const *)frame->data, frame->linesize, 0, c->height, (uint8_t * const *)buf2.c, &buf2.ys);

这里有两个区别:

  1. 你通过&inputFrame->data了,但它应该inputFrame->data没有地址运算符。

  2. 您不必分配第二个框架结构。本sws_scale不在乎。它只需要一块适当大小的内存(可能还有对齐)。


推荐阅读