C# 将Unity texture2d转换为ffmpeg的YUV420P

C# 将Unity texture2d转换为ffmpeg的YUV420P,c#,c++,unity3d,ffmpeg,texture2d,C#,C++,Unity3d,Ffmpeg,Texture2d,我正在尝试使用FFmpeg创建Unity player内部的录像机 下面是代码片段: 统一,C#: 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 fra

我正在尝试使用FFmpeg创建Unity player内部的录像机

下面是代码片段:

统一,C#:

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 frame\n");

    int err;
    if (!videoFrame) {
        Log("Allocating Video Frame\n");

        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 context\n");
        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 data\n");
    // 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;
    }
    ...........
我认为这是因为“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\U刻度失败的原因是传递了
sws\U getContext
sws\U刻度
的错误源高度和宽度。您应该在Unity中将维度传递到方法或核心值中


<>代码> SWSsS垢返回输出片段的高度,不是错误。

C++ C代码>加号帧< /C>函数取3个参数,但是你的C++ <代码> AdgFrase< /Cult>函数只有2个PARAMS……而且,你不是在钉扎数组。你们基本上忽略了我在回答中说的话。@Programmer,我在主要问题中添加了所有遗漏的信息。我按照你在回答中所说的做了。以后不要删除它,即使你遇到了其他问题。如果不是必须的话,我就不会花时间写了。至于你的问题,我真的不知道,因为我想到的大多数事情现在都被消除了,但是你怎么知道问题来自
sws\u scale
line?@程序员对此表示抱歉。我在sws_缩放之前和之后都有日志记录,如果我删除这一行(只创建绿色屏幕的空视频),它不会失败,但是您是否看到诸如“缩放数据”之类的日志?如果是,在哪里?
  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
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);