簡體   English   中英

在directx 11中渲染h264視頻幀

[英]Render h264 video frames in directx 11

我是DirectX新手。 我正在嘗試編寫一個自定義IP攝像機視頻播放器,我正在使用DirectX11以Wpf Gui作為我的前端渲染解碼圖像。

我是一個c#開發人員並使用了托管的directx,不再由microsoft更新,因此轉移到了wpf和directx11。

我的應用程序的所有部分直到幀的渲染工作正常。

我已經設法創建了一個D3DImage源,它將在Wpf應用程序中使用,成功創建我的視口和我的設備,包括我的共享資源,因為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);

使用上面的代碼,我現在能夠使用我收到的新圖像數據填充紋理,但圖像沒有以正確的顏色/像素呈現,如下圖中紅色框所示。

渲染圖像的屏幕截圖: 在此輸入圖像描述

通過BGRA32像素格式的解碼器接收圖像字節。 任何解決這個問題的建議都會非常有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM