我正在使用 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] 错误的 dst 图像指针”错误。当我查看 FFMpeg 的源代码时,我发现原因是 outputFrame 的数据未初始化,但我不明白它应该如何。
我找到的所有现有答案或教程 ( see example ) 似乎都使用已弃用的 API,并且不清楚如何使用新的 API。如果有任何帮助,我将不胜感激。
最佳答案
下面是我调用 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);
这里有两个区别:
您传递了
&inputFrame->data
,但它应该是没有 address-of 运算符的inputFrame->data
。您不必分配第二个框架结构。
sws_scale
不关心它。它只需要一 block 适当大小的内存(可能还有对齐方式)。
关于opengl - 无法正确初始化 AVFrame 以进行 sws_scale 转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55441043/