Media Foundation - 如何在MFT中更改帧大小(Media Foundation Transform)

seb*_*aub 5 c++ windows video-processing mft ms-media-foundation

我正在尝试实现一个能够旋转视频的MFT.旋转本身将在变换函数内完成.为此,我需要更改输出帧大小,但我不知道如何做到这一点.

作为起点,我使用了Microsoft提供的MFT_Grayscale示例.我将此MFT作为转换节点包含在部分拓扑中

HRESULT Player::AddBranchToPartialTopology(
    IMFTopology *pTopology,
    IMFPresentationDescriptor *pSourcePD,
    DWORD iStream
    )
{
    ...
    IMFTopologyNode pTransformNode = NULL;
    ...
    hr = CreateTransformNode(CLSID_GrayscaleMFT, &pTransformNode);
    ...
    hr = pSourceNode->ConnectOutput(0, pTransformNode, 0);
    hr = pTransformNode->ConnectOutput(0, pOutputNode, 0);
    ...
}
Run Code Online (Sandbox Code Playgroud)

到目前为止,此代码正在运行.应用灰度mft并按预期工作.无论如何,我想改变这个mft来处理视频旋转.所以我们假设我想将视频旋转90度.为此,必须切换输入框架的宽度和高度.我尝试了不同的东西,但没有一个按预期工作.基于此线程中的第一条注释如何更改Media Foundation Transform输出帧(视频)大小?我开始改变SetOutputType的实现.我在GetOutputType中调用GetAttributeSize来接收实际的frame_size.当我尝试设置新的frame_size时失败(当开始播放时我收到hresult 0xc00d36b4(指定的数据无效,不一致或此对象不支持)

HRESULT CGrayscale::SetOutputType(
    DWORD           dwOutputStreamID,
    IMFMediaType    *pType, // Can be NULL to clear the output type.
    DWORD           dwFlags 
    )
{ ....
    //Receive the actual frame_size of pType (works as expected)
    hr = MFGetAttributeSize(
    pType,
    MF_MT_FRAME_SIZE,
    &width,
    &height
    ));
    ...
    //change the framesize 
    hr = MFSetAttributeSize(
    pType,
    MF_MT_FRAME_SIZE,
    height,
    width
    ));
}
Run Code Online (Sandbox Code Playgroud)

我相信我在这里会遗漏一些东西,所以任何提示都会受到高度赞赏.

提前致谢

Dav*_*erd 4

W8+ 中有一个可以进行旋转的变换。我自己在这方面运气不太好,但想必它可以发挥作用。我假设这对您来说不是一个可行的解决方案。

更有趣的情况是创建一个 MFT 来进行转换。

事实证明,将“灰度”变成旋转器有很多步骤。

1)正如您所猜测的,您需要影响输出类型的帧大小。但是,更改传递给 SetOutputType 的类型是错误的。发送到 SetOutputType 的 pType 是客户端要求您支持的类型。将该媒体类型更改为他们要求的以外的其他类型然后返回 S_OK 表示您支持它,这是没有意义的。

相反,您需要更改的是从 GetOutputAvailableType 发回的值。

2) 当计算从 GetOutputAvailableType 发回的类型时,您需要将其基于客户端发送到 SetInputType 的 IMFMediaType,并进行一些更改。是的,您想要调整 MF_MT_FRAME_SIZE,但您可能还需要调整 MF_MT_DEFAULT_STRIDE、MF_MT_GEOMETRIC_APERTURE 和(可能)MF_MT_MINIMUM_DISPLAY_APERTURE。可以想象,您可能还需要调整 MF_MT_SAMPLE_SIZE。

3)您没有说您是否打算在流开始时固定旋转量,或者在播放过程中改变旋转量。当我写这篇文章时,我使用从 IMFTransform::GetAttributes 返回的 IMFAttributes 来指定旋转。在处理每一帧之前,读取当前值。为了使这项工作正常进行,您需要能够从 OnProcessOutput 发送回 MF_E_TRANSFORM_STREAM_CHANGE。

4)因为懒,我不想弄清楚如何旋转NV12或YUY2或类似的东西。但是有一些函数可以轻松地为 RGB32 执行此操作。因此,当调用 GetInputAvailableType 时,我会请求 RGB32。

我尝试支持其他输入类型,如 RGB24、RGB565 等,但遇到了问题。当您的输出类型为 RGB24 时,MF 会在下游添加另一个 MFT,以将 RGB24 转换回更容易使用的内容(可能是 RGB32)。而且 MFT支持中途更改媒体类型。我能够通过接受各种子类型作为输入,但始终输出 RGB32,并按指定旋转来使其工作。

这听起来很复杂,但大多数情况并非如此。如果您阅读代码,您可能会说“哦,我明白了。” 我愿意向您提供我的源代码,但我不确定它对您有多大用处。这是用 c# 写的,而你问的是 c++。

另一方面,我正在制作一个模板,以便更轻松地编写 MFT。~十几行 C# 代码来创建最简单的 MFT。根据 VS 的分析/计算代码指标(不包括模板)计算,c# 旋转 MFT 约为 131 行。我正在尝试 C++ 版本,但它仍然有点粗糙。

我是不是忘记了什么?大概是一堆东西。不要忘记为 MFT 生成新的 Guid,而不是使用灰度的 Guid。但我认为我已经达到了最高点。

编辑:既然我的 C++ 版本的模板开始工作,我可以轻松地发布一些实际代码。这可能会让上面的一些观点变得更加清晰。例如,在#2 中,我讨论了基于输入类型的输出类型。您可以在 CreateOutputFromInput 中看到发生的情况。实际的旋转代码在 WriteIt() 中。

我对代码进行了一些简化,但希望这能让您“哦,我明白了。”

void OnProcessSample(IMFSample *pSample, bool Discontinuity, int InputMessageNumber)
{
    HRESULT hr = S_OK;

    int i = MFGetAttributeUINT32(GetAttributes(), AttribRotate, 0);
    i &= 7;

    // Will the output use different dimensions than the input?
    bool IsOdd = (i & 1) == 1;

    // Does the current AttribRotate rotation give a different 
    // orientation than the old one?
    if (IsOdd != m_WasOdd)
    {
        // Yes, change the output type.
        OutputSample(NULL, InputMessageNumber);
        m_WasOdd = IsOdd;
    }

    // Process it.
    DoWork(pSample, (RotateFlipType)i);

    // Send the modified input sample to the output sample queue.
    OutputSample(pSample, InputMessageNumber);
}

void OnSetInputType()
{
    HRESULT hr = S_OK;

    m_imageWidthInPixels = 0;
    m_imageHeightInPixels = 0;
    m_cbImageSize = 0;
    m_lInputStride = 0;

    IMFMediaType *pmt = GetInputType();

    // type can be null to clear
    if (pmt != NULL)
    {
        hr = MFGetAttributeSize(pmt, MF_MT_FRAME_SIZE, &m_imageWidthInPixels, &m_imageHeightInPixels);
        ThrowExceptionForHR(hr);

        hr = pmt->GetUINT32(MF_MT_DEFAULT_STRIDE, &m_lInputStride);
        ThrowExceptionForHR(hr);

        // Calculate the image size (not including padding)
        m_cbImageSize = m_imageHeightInPixels * m_lInputStride;
    }
    else
    {
        // Since the input must be set before the output, nulling the 
        // input must also clear the output.  Note that nulling the 
        // input is only valid if we are not actively streaming.

        SetOutputType(NULL);
    }
}

IMFMediaType *CreateOutputFromInput(IMFMediaType *inType)
{
    // For some MFTs, the output type is the same as the input type.  
    // However, since we are rotating, several attributes in the 
    // media type (like frame size) must be different on our output.  
    // This routine generates the appropriate output type for the 
    // current input type, given the current state of m_WasOdd.

    IMFMediaType *pOutputType = CloneMediaType(inType);

    if (m_WasOdd)
    {
        HRESULT hr;
        UINT32 h, w;

        // Intentionally backward
        hr = MFGetAttributeSize(inType, MF_MT_FRAME_SIZE, &h, &w);
        ThrowExceptionForHR(hr);

        hr = MFSetAttributeSize(pOutputType, MF_MT_FRAME_SIZE, w, h);
        ThrowExceptionForHR(hr);

        MFVideoArea *a = GetArea(inType, MF_MT_GEOMETRIC_APERTURE);
        if (a != NULL)
        {
            a->Area.cy = h;
            a->Area.cx = w;
            SetArea(pOutputType, MF_MT_GEOMETRIC_APERTURE, a);
        }

        a = GetArea(inType, MF_MT_MINIMUM_DISPLAY_APERTURE);
        if (a != NULL)
        {
            a->Area.cy = h;
            a->Area.cx = w;
            SetArea(pOutputType, MF_MT_MINIMUM_DISPLAY_APERTURE, a);
        }

        hr = pOutputType->SetUINT32(MF_MT_DEFAULT_STRIDE, w * 4);
        ThrowExceptionForHR(hr);
    }

    return pOutputType;
}

void WriteIt(BYTE *pBuffer, RotateFlipType fm)
{
    Bitmap *v = new Bitmap((int)m_imageWidthInPixels, (int)m_imageHeightInPixels, (int)m_lInputStride, PixelFormat32bppRGB, pBuffer);
    if (v == NULL)
        throw (HRESULT)E_OUTOFMEMORY;

    try
    {
        Status s;

        s = v->RotateFlip(fm);
        if (s != Ok)
            throw (HRESULT)E_UNEXPECTED;

        Rect r;

        if (!m_WasOdd)
        {
            r.Width = (int)m_imageWidthInPixels;
            r.Height = (int)m_imageHeightInPixels;
        }
        else
        {
            r.Height = (int)m_imageWidthInPixels;
            r.Width = (int)m_imageHeightInPixels;
        }

        BitmapData bmd;
        bmd.Width = r.Width,
        bmd.Height = r.Height,
        bmd.Stride = 4*bmd.Width;
        bmd.PixelFormat = PixelFormat32bppARGB; 
        bmd.Scan0 = (VOID*)pBuffer;
        bmd.Reserved = NULL;

        s = v->LockBits(&r, ImageLockModeRead + ImageLockModeUserInputBuf, PixelFormat32bppRGB, &bmd);
        if (s != Ok)
            throw (HRESULT)E_UNEXPECTED;

        s = v->UnlockBits(&bmd);
        if (s != Ok)
            throw (HRESULT)E_UNEXPECTED;
    }
    catch(...)
    {
        delete v;
        throw;
    }

    delete v;
}
Run Code Online (Sandbox Code Playgroud)