设置IMTransform视频处理器以进行色彩空间转换

时间:2018-07-16 19:34:12

标签: visual-studio image-processing sdk ms-media-foundation color-space

我正在尝试使用视频处理器MFT进行一些基本的色彩空间转换。我的相机本机支持NV12,我需要RGB24,以编写一些可以提供类似卡通效果的着色器。

这是用于执行MF的Media类的定义。

   {
        "python.pythonPath": "C:\\Users\\declan\\python.exe",
        "python.unitTest.unittestArgs": [
            "-v",
            "-s",
            ".",
            "-p",
            "*Test*.py"
        ],
        "python.unitTest.pyTestEnabled": false,
        "python.unitTest.nosetestsEnabled": false,
        "python.unitTest.unittestEnabled": true
    }

我通过以下方式进行IMFTransform的设置:

class Media : public IMFSourceReaderCallback //this class inhertis from IMFSourceReaderCallback
{
    CRITICAL_SECTION criticalSection;
    long referenceCount;
    WCHAR                   *wSymbolicLink;
    UINT32                  cchSymbolicLink;
    IMFSourceReader* sourceReader;
    MFT_REGISTER_TYPE_INFO *inputVideoTypes;
    MFT_REGISTER_TYPE_INFO *outputVideoTypes;
    IMFMediaType* mediaType = NULL;
    IMFMediaType* streamType = NULL;
    IMFMediaType* streamType2 = NULL;
    IMFMediaType* streamType3 = NULL;
    IMFTransform **VP;

public:
    LONG stride;
    float bytesPerPixel;
    GUID videoFormat;
    UINT height;
    UINT width;
    WCHAR deviceNameString[2048];
    BYTE* rawData;
    UINT32 count;
    DWORD devices_found = 0;
    HRESULT CreateCaptureDevice();
    HRESULT SetSourceReader(IMFActivate *device);
    HRESULT IsMediaTypeSupported(IMFMediaType* type);
    HRESULT GetDefaultStride(IMFMediaType *pType, LONG *plStride);
    HRESULT Close();
    Media();
    ~Media();   

    // the class must implement the methods from IUnknown 
    STDMETHODIMP QueryInterface(REFIID iid, void** ppv);
    STDMETHODIMP_(ULONG) AddRef();
    STDMETHODIMP_(ULONG) Release();

    //  the class must implement the methods from IMFSourceReaderCallback 
    STDMETHODIMP OnReadSample(HRESULT status, DWORD streamIndex, DWORD streamFlags, LONGLONG timeStamp, IMFSample *sample);
    STDMETHODIMP OnEvent(DWORD, IMFMediaEvent *);
    STDMETHODIMP OnFlush(DWORD);

};

问题是SetOutput方法返回:无法找到所需的属性,而我却没有真正得到,这是什么问题。 谁能指出我在哪里做坏事? 谢谢

编辑:输入的LogMediaType:

inputVideoTypes = new MFT_REGISTER_TYPE_INFO;
inputVideoTypes->guidMajorType = MFMediaType_Video;
inputVideoTypes->guidSubtype = MFVideoFormat_NV12;
outputVideoTypes = new MFT_REGISTER_TYPE_INFO;
outputVideoTypes->guidMajorType = MFMediaType_Video;
outputVideoTypes->guidSubtype = MFVideoFormat_RGB24;
hr = sourceReader->GetNativeMediaType(MF_SOURCE_READER_FIRST_VIDEO_STREAM, i, &streamType2);
IMFActivate **transformActivateArray = NULL;
UINT32 MFTcount;
hr = MFTEnumEx(MFT_CATEGORY_VIDEO_PROCESSOR, MFT_ENUM_FLAG_ALL, inputVideoTypes, outputVideoTypes, &transformActivateArray, &MFTcount);
if (FAILED(hr))
{
    exit(3);
}
if (MFTcount == 0)
exit(7);
VP = new IMFTransform*[MFTcount];
for (DWORD i = 0; i < MFTcount; i++)
{
    hr = transformActivateArray[i]->ActivateObject(__uuidof(IMFTransform), (void**)&VP[i]);
}
DWORD* inputCount = new DWORD[MFTcount];
DWORD* outputCount = new DWORD[MFTcount];
for (DWORD i = 0; i < MFTcount; i++)
{
    hr = VP[i]->GetStreamCount(&inputCount[i], &outputCount[i]);
}
DWORD **inputids = new DWORD*[MFTcount];
DWORD **outputids = new DWORD*[MFTcount];
for (DWORD i = 0; i < MFTcount; i++)
{
        inputids[i] = new DWORD[inputCount[i]];
        outputids[i] = new DWORD[outputCount[i]];
}
for (DWORD i = 0; i < MFTcount; i++)
{
    VP[i]->GetStreamIDs(inputCount[i], inputids[i], outputCount[i], outputids[i]);
    if (FAILED(hr))
    exit(5);
}
DWORD flag1 = -1;
DWORD flag2 = -1;
for (DWORD i = 0; i < MFTcount; i++)
{
    for (DWORD j = 0; j < inputCount[i]; j++)
    {
        hr = VP[i]->GetInputAvailableType(0, 0, &streamType);
        if (SUCCEEDED(hr))
        {
            flag1 = i;
            flag2 = j;
            break;
        }
    }
}
if (flag1 == -1 && flag2 == -1)
exit(2);
hr = VP[0]->SetInputType(0, streamType2, 0);
hr = VP[0]->GetOutputAvailableType(0, 0, &streamType3);
hr = VP[0]->SetOutputType(0, streamType3, 0);

输出日志无效

MF_MT_FRAME_SIZE    1280 x 720
MF_MT_YUV_MATRIX    2
MF_MT_MAJOR_TYPE    MFMediaType_Video
MF_MT_VIDEO_LIGHTING    3
MF_MT_VIDEO_CHROMA_SITING   1
MF_MT_AM_FORMAT_TYPE    {F72A76A0-EB0A-11D0-ACE4-0000C0CC16BA}
MF_MT_FIXED_SIZE_SAMPLES    1
MF_MT_VIDEO_NOMINAL_RANGE   1
MF_MT_FRAME_RATE    30 x 1
MF_MT_PIXEL_ASPECT_RATIO    1 x 1
MF_MT_ALL_SAMPLES_INDEPENDENT   1
MF_MT_FRAME_RATE_RANGE_MIN  128849018881
MF_MT_VIDEO_PRIMARIES   2
MF_MT_INTERLACE_MODE    2
MF_MT_FRAME_RATE_RANGE_MAX  128849018881
{EA031A62-8BBB-43C5-B5C4-572D2D231C18}  1
MF_MT_SUBTYPE   MFVideoFormat_NV12

EDIT2

我只有一个由EnumEx方法枚举的VP,并且它具有固定数量的输入(1)和输出(1)流,因此以前的日志是唯一一个用于输入的日志

编辑3

Exception thrown: read access violation.
**pType** was nullptr.

和onReadSample方法中的处理如下:

hr = VP->SetInputType(0, streamType2, 0);
//MediaFoundationSamples::LogMediaType(streamType2);
DWORD dwIndex = 4;
hr = VP->GetOutputAvailableType(0, dwIndex, &streamType3);
hr = MFSetAttributeSize(streamType3, MF_MT_FRAME_SIZE, 1280, 720);
hr = streamType3->SetUINT32(MF_MT_FIXED_SIZE_SAMPLES, 1);
hr = MFSetAttributeRatio(streamType3, MF_MT_FRAME_RATE, 30, 1);
hr = MFSetAttributeRatio(streamType3, MF_MT_PIXEL_ASPECT_RATIO, 1, 1);
streamType3->SetUINT32(MF_MT_ALL_SAMPLES_INDEPENDENT, 1);
streamType3->SetUINT32(MF_MT_INTERLACE_MODE, 2);
MediaFoundationSamples::LogMediaType(streamType3);
hr = VP->SetOutputType(0, streamType3, 0);
hr = VP->GetInputStreamInfo(0, &InputInfo);
hr = VP->GetOutputStreamInfo(0, &OutputInfo);

1 个答案:

答案 0 :(得分:1)

从Mediafoundation示例中,您具有LogMediaType函数LogMediaType

您可以显示streamType2 / streamType3的日志吗?

还可以从此处video-format-attributes

检查现有的视频媒体类型

它可以帮助您找到缺少的属性。

EDIT1

因为我使用Windows 7,所以没有视频处理器MFT。我使用Color Converter DSP,他也可以进行色彩空间转换Color Converter DSP

IMFTransform* pVideoColorConverter = NULL;
IMFMediaType* pVideoOutputType = NULL;
// RGB24 media type at index 10, but can be different on your system
DWORD dwRGB24Index = 10;

hr = CoCreateInstance(CLSID_CColorConvertDMO, NULL, CLSCTX_INPROC_SERVER, IID_IMFTransform, reinterpret_cast<void**>(&pVideoColorConverter);
hr = pVideoColorConverter->SetInputType(0, pVideoInputType, 0);
hr = pVideoColorConverter->GetOutputAvailableType(0, dwRGB24Index, &pVideoOutputType);
hr = pVideoColorConverter->SetOutputType(0, pVideoOutputType, 0);

LogMediaType(pVideoInputType);
LogMediaType(pVideoOutputType);

这是日志:

pVideoInputType (my camera provide YUY2 not NV12) :

MF_MT_MAJOR_TYPE                MFMediaType_Video
MF_MT_SUBTYPE                   MFVideoFormat_YUY2
MF_MT_FRAME_SIZE                640 x 480
MF_MT_DEFAULT_STRIDE            1280
MF_MT_ALL_SAMPLES_INDEPENDENT   1
MF_MT_FIXED_SIZE_SAMPLES        1
MF_MT_SAMPLE_SIZE               614400
MF_MT_AVG_BITRATE               147456000
MF_MT_FRAME_RATE                30 x 1
MF_MT_PIXEL_ASPECT_RATIO        1 x 1
MF_MT_INTERLACE_MODE            2
MF_MT_AM_FORMAT_TYPE            {05589F80-C356-11CE-BF01-00AA0055595A}
MF_MT_FRAME_RATE_RANGE_MAX      128849018881
MF_MT_FRAME_RATE_RANGE_MIN      42949672961333333


pVideoOutputType :

MF_MT_MAJOR_TYPE                MFMediaType_Video
MF_MT_SUBTYPE                   MFVideoFormat_RGB24
MF_MT_FRAME_SIZE                640 x 480
MF_MT_FRAME_RATE                10000000 x 333333
MF_MT_GEOMETRIC_APERTURE        <<byte array>>
MF_MT_PIXEL_ASPECT_RATIO        1 x 1
MF_MT_INTERLACE_MODE            2
MF_MT_DEFAULT_STRIDE            1920
MF_MT_ALL_SAMPLES_INDEPENDENT   1
MF_MT_FIXED_SIZE_SAMPLES        1
MF_MT_SAMPLE_SIZE               921600

EDIT2

好的,您的视频输入类型似乎正确。

您现在可以记录所有VP的所有输入类型(streamType)了吗?

DWORD* inputCount = new DWORD[MFTcount];
DWORD* outputCount = new DWORD[MFTcount];

for(DWORD i = 0; i < MFTcount; i++){

    hr = VP[i]->GetStreamCount(&inputCount[i], &outputCount[i]);

    if(FAILED(hr)){
        exit(3);
    }
}

DWORD** inputids = new DWORD*[MFTcount];
DWORD** outputids = new DWORD*[MFTcount];

for(DWORD i = 0; i < MFTcount; i++){

    inputids[i] = new DWORD[inputCount[i]];
    outputids[i] = new DWORD[outputCount[i]];
}

for(DWORD i = 0; i < MFTcount; i++){

    hr = VP[i]->GetStreamIDs(inputCount[i], inputids[i], outputCount[i], outputids[i]);

    // By convention, if an MFT has exactly one fixed input stream and one fixed output stream, it should assign the identifier 0 to both streams
    if(hr == E_NOTIMPL && inputCount[i] == 1 && outputCount[i] == 1){

        inputids[i][0] = 0;
        outputids[i][0] = 0;
    }
    else if(FAILED(hr)){
        exit(4);
    }
}

for(DWORD i = 0; i < MFTcount; i++){

    // todo : log VP = i

    for(DWORD j = 0; j < inputCount[i]; j++){

        // todo : log stream id = inputids[i][j]

        DWORD dwTypeIndex = 0;
        hr = S_OK;

        while(hr == S_OK){

            // todo :log dwTypeIndex

            hr = VP[i]->GetInputAvailableType(inputids[i][j], dwTypeIndex, &streamType);

            if(SUCCEEDED(hr)){

                LogMediaType(streamType);
                SAFE_RELEASE(streamType);
                dwTypeIndex++;
            }
            else{

                // todo : check hr, should be MF_E_NO_MORE_TYPES
                // if hr == E_NOTIMPL/MF_E_INVALIDSTREAMNUMBER... should be error
            }
        }
    }
}

EDIT3

这里是视频处理器MFT的使用:VideoProcessor MTF sample

似乎您需要在使用它之前先提供一个D3DManager(HRESULT DX11VideoRenderer :: CPresenter :: CreateXVP(void)->第1118行)

我不确定D3DManager,因为我无法在系统上对其进行测试。

hr = CoCreateInstance(CLSID_VideoProcessorMFT, nullptr, CLSCTX_INPROC_SERVER, IID_IMFTransform, (void**)&m_pXVP);
if (FAILED(hr))
{
    break;
}

hr = m_pXVP->ProcessMessage(MFT_MESSAGE_SET_D3D_MANAGER, ULONG_PTR(m_pDXGIManager));
if (FAILED(hr))
{
    break;
}

// Tell the XVP that we are the swapchain allocator
hr = m_pXVP->GetAttributes(&pAttributes);
if (FAILED(hr))
{
    break;
}

hr = pAttributes->SetUINT32(MF_XVP_PLAYBACK_MODE, TRUE);
if (FAILED(hr))
{
    break;
}

从代码中检查所有m_pXVP和m_pXVPControl。

EDIT4

根据您的inputType,尝试手动创建outputType,并添加以下属性:

MF_MT_MAJOR_TYPE                MFMediaType_Video
MF_MT_SUBTYPE                   MFVideoFormat_RGB24
MF_MT_FRAME_SIZE                1280 x 720
MF_MT_FIXED_SIZE_SAMPLES        1
MF_MT_FRAME_RATE                30 x 1
MF_MT_PIXEL_ASPECT_RATIO        1 x 1
MF_MT_ALL_SAMPLES_INDEPENDENT   1
MF_MT_INTERLACE_MODE            2

因此,在SetInputType之后,创建视频媒体输出类型,然后使用此新的mediaType调用SetOutputType。

您只能先尝试使用MF_MT_MAJOR_TYPE / MF_MT_SUBTYPE / MF_MT_FRAME_SIZE,然后再添加一个。