简体   繁体   English

在directx 11中渲染h264视频帧

[英]Render h264 video frames in directx 11

I am new to DirectX . 我是DirectX新手。 I am trying to write a custom IP camera video player and for which I am using DirectX11 to render the decoded image with Wpf Gui as my front end. 我正在尝试编写一个自定义IP摄像机视频播放器,我正在使用DirectX11以Wpf Gui作为我的前端渲染解码图像。

I am a c# developer and have used managed directx which is no longer updated by microsoft hence moved to wpf and directx11. 我是一个c#开发人员并使用了托管的directx,不再由microsoft更新,因此转移到了wpf和directx11。

All parts of my application up to the rendering of the frames is working fine. 我的应用程序的所有部分直到帧的渲染工作正常。

I have managed to create a D3DImage source which will be used in Wpf app, successfully create my viewports and my device including my shared resource since D3DImage only works Directx9. 我已经设法创建了一个D3DImage源,它将在Wpf应用程序中使用,成功创建我的视口和我的设备,包括我的共享资源,因为D3DImage仅适用于Directx9。 I am using SharpDX as the wrapper for DirectX API . 我使用SharpDX作为DirectX API的包装器。

Now my problem is I can't seem to find a way to create a texture/update a texture from decoded image bytes or what would be the correct way to do so in order to render the decoded image from the bytes received. 现在我的问题是我似乎无法找到一种方法来创建纹理/从解码的图像字节更新纹理,或者这样做的正确方法是从接收的字节渲染解码图像。

Any help on this would be great or if someone can direct me to the right direction as to how this is to be approached? 任何有关这方面的帮助都会很棒,或者如果有人能指导我如何接近这个方向?

Thanks. 谢谢。

After nearly 2 weeks of searching and trying to find the solution to my stated problem, i have finally found it as below. 经过将近2周的搜索并试图找到我所述问题的解决方案,我终于找到了如下。

However, this does display the image but not as expected but i believe it is a start for me as the code below answers my question originally asked. 但是,这确实显示了图像但不是预期的,但我相信这对我来说是一个开始,因为下面的代码回答了我最初提出的问题。

Device.ImmediateContext.ClearRenderTargetView(this.m_RenderTargetView, Color4.Black);

    Texture2DDescription colordesc = new Texture2DDescription
    {
        BindFlags = BindFlags.ShaderResource,
        Format = m_PixelFormat,
        Width = iWidth,
        Height = iHeight,
        MipLevels = 1,
        SampleDescription = new SampleDescription(1, 0),
        Usage = ResourceUsage.Dynamic,
        OptionFlags = ResourceOptionFlags.None,
        CpuAccessFlags = CpuAccessFlags.Write,
        ArraySize = 1
    };

    Texture2D newFrameTexture = new Texture2D(this.Device, colordesc);

    DataStream dtStream = null;
    DataBox dBox = Device.ImmediateContext.MapSubresource(newFrameTexture, 0, MapMode.WriteDiscard, 0, out dtStream);
    if (dtStream != null)
    {
        int iRowPitch = dBox.RowPitch;

        for (int iHeightIndex = 0; iHeightIndex < iHeight; iHeightIndex++)
        {
            //Copy the image bytes to Texture
            dtStream.Position = iHeightIndex * iRowPitch;
             Marshal.Copy(decodedData, iHeightIndex * iWidth * 4, new IntPtr(dtStream.DataPointer.ToInt64() + iHeightIndex * iRowPitch), iWidth * 4);
        }
    }

    Device.ImmediateContext.UnmapSubresource(newFrameTexture, 0);


    Device.ImmediateContext.CopySubresourceRegion(newFrameTexture, 0, null, this.RenderTarget, 0);
    var shaderRescVw = new ShaderResourceView(this.Device, this.RenderTarget);

    Device.ImmediateContext.PixelShader.SetShaderResource(0, shaderRescVw);

    Device.ImmediateContext.Draw(6, 0);
    Device.ImmediateContext.Flush();
    this.D3DSurface.InvalidateD3DImage();

    Disposer.SafeDispose(ref newFrameTexture);

With the code above i am now able to populate the texture with the new images data i receive but the images are not being rendered in correct colors/pixels as shown within the red box in the image below. 使用上面的代码,我现在能够使用我收到的新图像数据填充纹理,但图像没有以正确的颜色/像素呈现,如下图中红色框所示。

Screenshot of the rendered image: 渲染图像的屏幕截图: 在此输入图像描述

The image bytes are received via decoder in BGRA32 pixel format. 通过BGRA32像素格式的解码器接收图像字节。 Any suggestion to resolve this would be very helpful. 任何解决这个问题的建议都会非常有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM