c# - 在 directx 11 中渲染 h264 视频帧

标签 c# wpf video-streaming directx directx-11

我是 DirectX 的新手。我正在尝试编写一个自定义 IP 摄像机视频播放器,为此我使用 DirectX11 以 Wpf Gui 作为我的前端来呈现解码图像。

我是一名 c# 开发人员,并且使用过 microsoft 不再更新的托管 directx,因此转移到 wpf 和 directx11。

我的应用程序的所有部分直到帧的渲染都工作正常。

我已设法创建一个将在 Wpf 应用程序中使用的 D3DImage 源,成功创建我的视口(viewport)和我的设备,包括我的共享资源,因为 D3DImage 仅适用于 Directx9。我正在使用 SharpDX 作为 DirectX API 的包装器。

现在我的问题是我似乎无法找到一种方法来从解码图像字节创建纹理/更新纹理,或者从接收到的字节渲染解码图像的正确方法是什么。

在这方面的任何帮助都会很棒,或者如果有人可以指导我正确的方向来解决这个问题?

谢谢。

最佳答案

经过将近 2 周的搜索和尝试找到我所陈述问题的解决方案,我终于找到了它,如下所示。

但是,这确实显示了图像,但并不像预期的那样,但我相信这对我来说是一个开始,因为下面的代码回答了我最初提出的问题。

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);

使用上面的代码,我现在可以用我收到的新图像数据填充纹理,但是图像没有以正确的颜色/像素呈现,如下图中的红色框内所示。

渲染图截图: enter image description here

图像字节以 BGRA32 像素格式通过解码器接收。 解决此问题的任何建议都会非常有帮助。

关于c# - 在 directx 11 中渲染 h264 视频帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33087080/

相关文章:

c# - 使用泛型时转换为抽象类或接口(interface)

c# - 使用 ItemsSource 时添加额外的项目

c# - 滑动覆盖面板

c++ - 通过服务器在互联网上传输加密视频的跨平台方法

c# - 将 HttpWebRequest 从 .NET 发送到 php 站点时出错

c# - 获取 PC 中已安装的完整更新列表

python - 如何停止将opencv帧流式传输到浏览器

ios - 在我的 iOS 应用程序中与外部配件通信以播放流中的视频

c# - 删除使用 lambda 表达式添加的 eventHandler

c# - WPF 中来自 Silverlight 的 DoubleTap 事件