简体   繁体   中英

Render h264 video frames in directx 11

I am new to 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.

I am a c# developer and have used managed directx which is no longer updated by microsoft hence moved to wpf and 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. I am using SharpDX as the wrapper for 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.

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. Any suggestion to resolve this would be very helpful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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