opengl - 未能为 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。我会很感激任何帮助。
解决方案
我是这样称呼的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
了,但它应该inputFrame->data
没有地址运算符。您不必分配第二个框架结构。本
sws_scale
不在乎。它只需要一块适当大小的内存(可能还有对齐)。
推荐阅读
- android - Android TextView在奇怪的地方自动换行
- ms-access - 拆分 MS Access 数据库单个用户遇到错误“不是有效的文件名”
- javascript - 使用线条以编程方式绘制圆
- io - 在调用 Pascal 的“写入”过程时,字段宽度为 1 的目的是什么?
- ios - 使用 Google UMP SDK 构建 Xcode 失败并显示“找不到框架”
- sql - 按id分组后从表中获取所有值
- python - 遍历数据框
- c# - ASP.NET 网格视图 C# 中的行数据绑定
- javascript - 从表单输入更改 HTML 文档标题
- amazon-web-services - lambda 函数是否可以创建可用于触发另一个 lambda 函数的 CloudWatch 计划事件?