首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为ffmpeg将统一texture2d转换为YUV420P

为ffmpeg将统一texture2d转换为YUV420P
EN

Stack Overflow用户
提问于 2017-10-03 13:10:42
回答 1查看 596关注 0票数 2

我正在尝试使用FFmpeg在Unity中创建录像机。

我有下一个代码片段:

统一,C#:

代码语言:javascript
复制
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++代码:

代码语言:javascript
复制
__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_scale“时会崩溃。

来自团结的日志:

代码语言:javascript
复制
  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,将数组固定在内存中,但结果没有改变。

提前感谢

民主联盟::

更改的DLL函数调用

代码语言:javascript
复制
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);

但错误仍然存在。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-04 08:25:06

sws_scale失败的原因是sws_getContextsws_scale传递了不正确的源高度和宽度。您应该将维度传递到统一中的方法或硬核值中。

此外,sws_scale返回输出片的高度,而不是错误。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46545422

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档