如何从Windows 8现代应用程序中的视频流中抓取帧?

Sha*_*ish 7 c++ windows-8 windows-runtime ms-media-foundation winrt-xaml

我试图从mp4视频流中提取图像.看完后,似乎正确的方法是在C++中使用Media Foundations并打开框架/读取内容.

通过文档和示例的方式很少,但经过一些挖掘,似乎有些人通过将帧读入纹理并将该纹理的内容复制到记忆可读纹理(我甚至不是确定我在这里使用正确的条款).尝试我发现的虽然给了我错误,我可能做了一堆错误.

这是我尝试做的一小段代码(项目本身附在底部).

    ComPtr<ID3D11Texture2D> spTextureDst;
    MEDIA::ThrowIfFailed(
        m_spDX11SwapChain->GetBuffer(0, IID_PPV_ARGS(&spTextureDst))
        );

    auto rcNormalized = MFVideoNormalizedRect();
    rcNormalized.left = 0;
    rcNormalized.right = 1;
    rcNormalized.top = 0;
    rcNormalized.bottom = 1;
    MEDIA::ThrowIfFailed(
        m_spMediaEngine->TransferVideoFrame(m_spRenderTexture.Get(), &rcNormalized, &m_rcTarget, &m_bkgColor)
        );

    //copy the render target texture to the readable texture.
    m_spDX11DeviceContext->CopySubresourceRegion(m_spCopyTexture.Get(),0,0,0,0,m_spRenderTexture.Get(),0,NULL);
    m_spDX11DeviceContext->Flush();

    //Map the readable texture;                 
    D3D11_MAPPED_SUBRESOURCE mapped = {0};
    m_spDX11DeviceContext->Map(m_spCopyTexture.Get(),0,D3D11_MAP_READ,0,&mapped);
    void* buffer = ::CoTaskMemAlloc(600 * 400 * 3);
    memcpy(buffer, mapped.pData,600 * 400 * 3);
    //unmap so we can copy during next update.
    m_spDX11DeviceContext->Unmap(m_spCopyTexture.Get(),0);


    // and the present it to the screen
    MEDIA::ThrowIfFailed(
        m_spDX11SwapChain->Present(1, 0)
        );            
}
Run Code Online (Sandbox Code Playgroud)

我得到的错误是:

App1.exe中0x76814B32的第一次机会异常:Microsoft C++异常:Platform :: InvalidArgumentException ^在内存位置0x07AFF60C.HRESULT:80070057

我不太确定如何进一步追求它,因为就像我说的那样,关于它的文档很少.

这是我正在处理的修改过的样本.此问题特定于WinRT(Windows 8应用程序).

Rog*_*and 6

更新成功!! 见底部编辑


一些局部的成功,但也许足以回答你的问题.请继续阅读.

在我的系统上,调试异常表明该OnTimer()函数在尝试调用时失败TransferVideoFrame().它给出的错误是InvalidArgumentException.

所以,一点谷歌搜索引发了我的第一个发现 - 显然NVIDIA驱动程序中存在一个错误 - 这意味着视频播放似乎失败了11和10个功能级别.

所以我的第一个改变是功能CreateDX11Device()如下:

static const D3D_FEATURE_LEVEL levels[] = {
/*
        D3D_FEATURE_LEVEL_11_1,
        D3D_FEATURE_LEVEL_11_0,  
        D3D_FEATURE_LEVEL_10_1,
        D3D_FEATURE_LEVEL_10_0,
*/
        D3D_FEATURE_LEVEL_9_3,
        D3D_FEATURE_LEVEL_9_2,
        D3D_FEATURE_LEVEL_9_1
    };
Run Code Online (Sandbox Code Playgroud)

现在TransferVideoFrame()仍然失败,但是给出E_FAIL(作为HRESULT)而不是无效的参数.

更多谷歌搜索引发了我的第二次发现 -

这是一个显示使用TransferVideoFrame()而不使用CreateTexture2D()预先创建纹理的示例.我看到你已经有OnTimer()类似的代码,但没有使用,所以我猜你找到了相同的链接.

无论如何,我现在使用此代码来获取视频帧:

ComPtr <ID3D11Texture2D> spTextureDst;
m_spDX11SwapChain->GetBuffer (0, IID_PPV_ARGS (&spTextureDst));
m_spMediaEngine->TransferVideoFrame (spTextureDst.Get (), nullptr, &m_rcTarget, &m_bkgColor);
Run Code Online (Sandbox Code Playgroud)

执行此操作后,我看到TransferVideoFrame()成功(好!)但调用Map()复制的纹理m_spCopyTexture- 失败,因为该纹理不是使用CPU读取访问创建的.

因此,我只是使用您的读/写作m_spRenderTexture为副本的目标,因为它具有正确的标志,并且由于之前的更改,我不再使用它.

        //copy the render target texture to the readable texture.
        m_spDX11DeviceContext->CopySubresourceRegion(m_spRenderTexture.Get(),0,0,0,0,spTextureDst.Get(),0,NULL);
        m_spDX11DeviceContext->Flush();

        //Map the readable texture;                 
        D3D11_MAPPED_SUBRESOURCE mapped = {0};
        HRESULT hr = m_spDX11DeviceContext->Map(m_spRenderTexture.Get(),0,D3D11_MAP_READ,0,&mapped);
        void* buffer = ::CoTaskMemAlloc(176 * 144 * 3);
        memcpy(buffer, mapped.pData,176 * 144 * 3);
        //unmap so we can copy during next update.
        m_spDX11DeviceContext->Unmap(m_spRenderTexture.Get(),0);
Run Code Online (Sandbox Code Playgroud)

现在,在我的系统上,该OnTimer()功能不会失败.视频帧被渲染到纹理,像素数据被成功复制到内存缓冲区.

在考虑是否还有其他问题之前,也许现在是时候看看你能否取得与我迄今为止相同的进步.如果您使用更多信息对此答案发表评论,我将编辑答案,以便在可能的情况下添加更多帮助.

编辑

对纹理描述所做的更改 FramePlayer::CreateBackBuffers()

        //make first texture cpu readable
        D3D11_TEXTURE2D_DESC texDesc = {0};
        texDesc.Width = 176;
        texDesc.Height = 144;
        texDesc.MipLevels = 1;
        texDesc.ArraySize = 1;
        texDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
        texDesc.SampleDesc.Count = 1;
        texDesc.SampleDesc.Quality =  0;
        texDesc.Usage = D3D11_USAGE_STAGING;
        texDesc.BindFlags = 0;
        texDesc.CPUAccessFlags =  D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE;
        texDesc.MiscFlags = 0;

        MEDIA::ThrowIfFailed(m_spDX11Device->CreateTexture2D(&texDesc,NULL,&m_spRenderTexture));
Run Code Online (Sandbox Code Playgroud)

还要注意,有时需要清除内存泄漏(我确定你知道) - 以下行中分配的内存永远不会被释放:

void* buffer = ::CoTaskMemAlloc(176 * 144 * 3); // sizes changed for my test
Run Code Online (Sandbox Code Playgroud)

成功

我现在已经成功保存了一个单独的框架,但现在没有使用复制纹理.

首先,我下载了最新版本的DirectXTex库,它提供了DX11纹理辅助函数,例如从纹理中提取图像并保存到文件.需要仔细遵循将DirectXTex库作为现有项目添加到解决方案的说明,并注意Windows 8 Store Apps所需的更改.

一旦,包含,引用和构建上面的库,添加以下#include的库FramePlayer.cpp

#include "..\DirectXTex\DirectXTex.h"  // nb - use the relative path you copied to
#include <wincodec.h>
Run Code Online (Sandbox Code Playgroud)

最后,代码的中心部分FramePlayer::OnTimer()需要与以下内容类似.您将看到我每次只保存到相同的文件名,因此需要修改例如在名称中添加例如帧号

// new frame available at the media engine so get it 
ComPtr<ID3D11Texture2D> spTextureDst;
MEDIA::ThrowIfFailed(m_spDX11SwapChain->GetBuffer(0, IID_PPV_ARGS(&spTextureDst)));

auto rcNormalized = MFVideoNormalizedRect();
rcNormalized.left = 0;
rcNormalized.right = 1;
rcNormalized.top = 0;
rcNormalized.bottom = 1;
MEDIA::ThrowIfFailed(m_spMediaEngine->TransferVideoFrame(spTextureDst.Get(), &rcNormalized, &m_rcTarget, &m_bkgColor));

// capture an image from the DX11 texture
DirectX::ScratchImage pImage;
HRESULT hr = DirectX::CaptureTexture(m_spDX11Device.Get(), m_spDX11DeviceContext.Get(), spTextureDst.Get(), pImage);
if (SUCCEEDED(hr))
{
    // get the image object from the wrapper
    const DirectX::Image *pRealImage = pImage.GetImage(0, 0, 0);

    // set some place to save the image frame
    StorageFolder ^dataFolder = ApplicationData::Current->LocalFolder;
    Platform::String ^szPath = dataFolder->Path + "\\frame.png";

    // save the image to file
    hr = DirectX::SaveToWICFile(*pRealImage, DirectX::WIC_FLAGS_NONE, GUID_ContainerFormatPng, szPath->Data());
}

// and the present it to the screen
MEDIA::ThrowIfFailed(m_spDX11SwapChain->Present(1, 0));            
Run Code Online (Sandbox Code Playgroud)

我现在没有时间进一步采取这一点,但我对目前取得的成就感到非常满意:-))

您能否重新审视并在评论中更新结果?