将Unity Texture2D转换为YUV420P的FFMPEG
Converting Unity texture2d to YUV420P for ffmpeg
我正在尝试使用FFMPEG在Unity Player内部创建视频记录器。
我有下一个代码段:
Unity,C#:
Texture2D frameTexture = new Texture2D(frameWidth, frameHeight, TextureFormat.RGB24, false);
//...
frameTexture.ReadPixels(new Rect(0, 0, frameWidth, frameHeight), 0, 0, false);
frameTexture.Apply();
//.....
byte[] pixels = frameTexture.GetRawTextureData();
AddFrame(pixels, pixels.Length, libApi);
//DLL Function Declaration
[DllImport("VideoCapture", CallingConvention = CallingConvention.Cdecl)]
static extern void AddFrame(byte[] data, int size, System.IntPtr api);
C 代码:
__declspec(dllexport) void AddFrame(uint8_t *data, int size, VideoCapture *vc) {
vc->AddFrame(data, size);
}
void VideoCapture::AddFrame(uint8_t *data, int size) {
Log("nAdding framen");
int err;
if (!videoFrame) {
Log("Allocating Video Framen");
videoFrame = av_frame_alloc();
videoFrame->format = AV_PIX_FMT_YUV420P;
videoFrame->width = cctx->width;
videoFrame->height = cctx->height;
if ((err = av_frame_get_buffer(videoFrame, 32)) < 0) {
Debug("Failed to allocate picture", err);
return;
}
}
if (!swsCtx) {
Log("Creating SWS contextn");
swsCtx = sws_getContext(cctx->width, cctx->height, AV_PIX_FMT_RGB24, cctx->width, cctx->height, AV_PIX_FMT_YUV420P, 0, 0, 0, 0);
}
uint8_t * inData[1] = { data };
int inLinesize[1] = { 3 * cctx->width };
Log("Scaling datan");
// From RGB to YUV
if ((err = sws_scale(swsCtx, inData, inLinesize, 0, cctx->height, videoFrame->data, videoFrame->linesize)) < 0) {
Debug("Failed to scale img", err);
return;
}
...........
Unity播放器在执行" SWS_SCALE"时会崩溃。
来自Unity的日志:
ERROR: SymGetSymFromAddr64, GetLastError: 'Attempt to access invalid address.' (Address: 00007FF8437B49A6)
0x00007FF8437B49A6 (swscale-4)
ERROR: SymGetSymFromAddr64, GetLastError: 'Attempt to access invalid address.' (Address: 00007FF8437B2982)
0x00007FF8437B2982 (swscale-4)
0x00007FF8437E78A6 (swscale-4) sws_get_class
0x00007FF8437E8E47 (swscale-4) sws_scale
我认为它是因为" sws_get_class",但是如果我直接调用此功能,它可以工作。如果我将空数据传递到" sws_scale"中,它也可以工作,只是抱怨它是无效的。因此,原因是数据本身,但我没有什么问题。
我还尝试将纹理编码为JPG和PNG,将数组固定在内存中,但结果没有改变。
预先感谢
upd :::
将dll函数更改为
unsafe void AddFrame(byte[] data)
{
fixed (byte* p = data)
{
AddFrame((IntPtr)p, data.Length, customLib);
}
}
//Function declaration
static extern void AddFrame(IntPtr data, int size, System.IntPtr api);
但是错误仍然存在。
sws_scale
失败的原因是sws_getContext
和sws_scale
的源高度和宽度不正确。您应该将尺寸传递到统一的方法或硬核值中。
sws_scale
还返回输出切片的高度,而不是错误。
相关文章:
- FFmpeg:制作一个应用程序比直接使用ffmepg更好吗
- 使用FFMPEG将RGB图像序列保存到.mp4时出现问题
- FFMPEG配置文件级别id大小无效
- FFMpeg库:如何在音频文件中精确查找
- OpenCV RTP-Stream with FFMPEG
- FFmpeg——使用硬件加速进行视频解码
- 如何在 C/C++ 中使用 ffmpeg 的黑框 avfilter
- 在 GLFW 窗口中显示 FFMPEG 解码帧
- FFMPEG:sws_scale返回错误:切片参数 0, 2160 无效
- 为什么有些延迟加载 DLL 会立即使用 FFMPEG 卸载?
- 将FFMpeg AVFrame对象从C++应用程序流式传输到Python的最佳方法?
- 面临分段故障 使用 ffmpeg 读取视频时,因为"pFormatCtx-> streams [i]-> codecpar"的地址0x00
- 将 FFMPEG 帧写入 png/jpeg 文件
- FFMPEG,C++,内存泄漏,我做错了什么?
- 如何在 c++ 中使用 ffmpeg 获取 DirectShow 设备列表?
- 矛盾的未解决的外部符号+未使用的库与VS2017和FFMPEG 4
- FFmpeg av_read_frame从音频流返回数据包
- 使用ffmpeg时如何提高图像导出/提取速度?
- FFmpeg原始视频大小参数
- 将Unity Texture2D转换为YUV420P的FFMPEG