将ffmpeg d3dva纹理资源复制到共享的渲染纹理 [英] Copy ffmpeg d3dva texture resource to shared rendering texture

查看:220
本文介绍了将ffmpeg d3dva纹理资源复制到共享的渲染纹理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于此示例,我正在使用ffmpeg通过d3dva解码视频 https://github.com/FFmpeg/FFmpeg/blob/master/doc/examples/hw_decode.c 。我能够成功解码视频。接下来,我需要渲染解码的NV12帧。我已基于此示例 https://github.com/balapradeepswork/D3D11NV12Rendering 创建了Directx渲染纹理

I'm using ffmpeg to decode video via d3dva based on this example https://github.com/FFmpeg/FFmpeg/blob/master/doc/examples/hw_decode.c. I'm able to succesfully decode video. What I need to do next is to render decoded NV12 frame. I have created directx rendering texture based on this example https://github.com/balapradeepswork/D3D11NV12Rendering and set it as shared.

    D3D11_TEXTURE2D_DESC texDesc;
    texDesc.Format = DXGI_FORMAT_NV12;              // Pixel format
    texDesc.Width = width;                          // Width of the video frames
    texDesc.Height = height;                        // Height of the video frames
    texDesc.ArraySize = 1;                          // Number of textures in the array
    texDesc.MipLevels = 1;                          // Number of miplevels in each texture
    texDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE; // We read from this texture in the shader
    texDesc.Usage = D3D11_USAGE_DEFAULT;
    texDesc.MiscFlags = D3D11_RESOURCE_MISC_SHARED;
    texDesc.CPUAccessFlags = 0;

    hr = device.CreateTexture2D(&texDesc, null, &nv12Texture);
    if (FAILED(hr))
    {
        error("Failed to create NV12 texture (hr: %s)", hr);
        return false;
    }

    // QI IDXGIResource interface to synchronized shared surface.
    IDXGIResource dxgiResource;
    nv12Texture.QueryInterface(&IID_IDXGIResource, cast(void**)&dxgiResource);

    // obtain handle to IDXGIResource object.
    hr = dxgiResource.GetSharedHandle(&sharedHandle);
    dxgiResource.Release();
    dxgiResource = null;

    if (FAILED(hr))
    {
        error("Failed to create NV12 texture shared handle (hr: %s)", hr);
        return false;
    }

然后我尝试将nv12从ffmpeg纹理复制到渲染纹理。完全与ffmpeg在函数 _data>

I'm then trying to copy nv12 from ffmpeg texture to my rendering texture. exactly as ffmpeg does in function d3d11va_transfer_data.

    ID3D11Texture2D hwTexture = cast(ID3D11Texture2D)frame.data[0];

    ID3D11Device hwDevice;
    hwTexture.GetDevice(&hwDevice);

    ID3D11DeviceContext hwDeviceCtx;
    hwDevice.GetImmediateContext(&hwDeviceCtx);

    ID3D11Texture2D sharedTexture;

    HRESULT hr = device.OpenSharedResource(sharedHandle, &IID_ID3D11Texture2D,cast(void**) &sharedTexture);
    if(FAILED(hr))
    {
        error("Failed to obtaion open shared resource.");
        return;
    }

    int index = cast(int)frame.data[1];
    hwDeviceCtx.CopySubresourceRegion(sharedTexture, 0, 0, 0, 0, hwTexture, index, null);

但是渲染窗口是绿色的,没有错误,没有失败。使用sw解码器时,我可以渲染帧,我只需使用D3D11_USAGE_DYNAMIC和D3D11_CPU_ACCESS_WRITE创建纹理。

But the rendering window is just green no error no FAILED hr result nothing. I'm able render frames when I'm using sw decoder I just create texture with D3D11_USAGE_DYNAMIC and D3D11_CPU_ACCESS_WRITE.

这里是每种纹理的描述。

Here us desc of each texture.

hwTexture desc D3D11_TEXTURE2D_DESC(1280, 720, 1, 20, 103, DXGI_SAMPLE_DESC(1, 0), 0, 512, 0, 0)
nv12Texture desc D3D11_TEXTURE2D_DESC(1280, 720, 1, 1, 103, DXGI_SAMPLE_DESC(1, 0), 0, 8, 0, 2)
sharedTexture desc D3D11_TEXTURE2D_DESC(1280, 720, 1, 1, 103, DXGI_SAMPLE_DESC(1, 0), 0, 8, 0, 2)

知道我缺少什么吗?

推荐答案

所以我终于弄清楚了什么地方出了问题,问题是从设备而不是hwDevice调用了OpenSharedResource。

So i finally figured out what is wrong, the problem is that OpenSharedResource is called from device instead of hwDevice.

这篇关于将ffmpeg d3dva纹理资源复制到共享的渲染纹理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆