将 x264vfw 连接到自定义源过滤器

connect x264vfw to custom source filter

我正在尝试将自定义源过滤器连接到 x264。我可以将它连接到 leadtools 编码器,主要概念编码器。我可以直接在 graphedit 中连接它们。我得到 HR 结果 0x80040207 VFW_E_NO_ACCEPTABLE_TYPES.

HRESULT CStreaming::Init(){
    CoInitialize(NULL);
    HRESULT hr;
    hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,IID_PPV_ARGS(&m_pGraph));
#ifdef _DEBUG
    DWORD dwRegister;
    hr = AddToRot(m_pGraph, &dwRegister);
#endif
    A_CheckResult(hr);
    IBaseFilter* pSource;
    IBaseFilter* pEncoder;
    IBaseFilter* pColorSpaceConverter;
    IBaseFilter* pTransport;
    FILTER_INFO pfi;

    /* Source Filter*/
    hr = InitFilter(StreamingFilterGUID, &pSource);
    A_CheckResult(hr);
    hr = pSource->QueryFilterInfo(&pfi);
    A_CheckResult(hr);
    hr = m_pGraph->AddFilter(pSource, pfi.achName);
    A_CheckResult(hr);

    hr = InitFilter(x264EncodeGUID, &pEncoder);  
    A_CheckResult(hr);
    hr = pEncoder->QueryFilterInfo(&pfi);
    A_CheckResult(hr);
    hr = m_pGraph->AddFilter(pEncoder, pfi.achName);
    A_CheckResult(hr);
    ...
    /* Queryt Control and Events */
    hr = m_pGraph->QueryInterface(IID_PPV_ARGS(&m_pControl));
    A_CheckResult(hr);
    hr = m_pGraph->QueryInterface(IID_PPV_ARGS(&m_pEvent));
    A_CheckResult(hr);

    /* Connect */
    hr = ConnectFilters(pSource, pEncoder);
    A_CheckResult(hr);
    hr = ConnectFilters(pEncoder, pColorSpaceConverter);
    A_CheckResult(hr);
    hr = ConnectFilters(pColorSpaceConverter, pTransport);
    A_CheckResult(hr);

    /* Release */
    SafeRelease(&pSource);
    SafeRelease(&pEncoder);
    SafeRelease(&pColorSpaceConverter);
    SafeRelease(&pTransport);

    return S_OK;
}

Connect Filler 取自 MSDN

HRESULT CStreamingAgent::ConnectFilters(IBaseFilter *pSrc, IBaseFilter *pDest)
{
    IPin *pOut = NULL;

    // Find an output pin on the first filter.
    HRESULT hr = FindUnconnectedPin(pSrc, PINDIR_OUTPUT, &pOut);
    if (SUCCEEDED(hr))
    {
        hr = ConnectOutputPinToFilter(pDest, pOut);
        pOut->Release();
    }
    return hr;
}

ConnectOutputPinToFilter 已修改,但主要来自 MSDN

HRESULT CStreamingAgent::ConnectOutputPinToFilter(IBaseFilter *pDest,IPin *pOut)
{
    IPin *pIn = NULL;

    // Find an input pin on the downstream filter.
    HRESULT hr = FindUnconnectedPin(pDest, PINDIR_INPUT, &pIn);
    if (SUCCEEDED(hr))
    {
        // Try to connect them.
        hr = m_pGraph->ConnectDirect(pOut, pIn, NULL);
        pIn->Release();
    }
    return hr;
}

在 ConnectOutputPinToFilter 中,我检查了每个引脚的 MajorType 和 Subtype,它们匹配。
我的自定义输出过滤器被硬编码为输出 RGB32 1920x1080 24fp Init 过滤器是 cocreate 的包装器,带有一些错误检查和 addref

#define A_CheckResult(p) {if ((FAILED(p))) return(p);}

x264vfw 不是标准过滤器。旧的 VFW(Windows 的视频)编解码器被包装并且都共享相同的 CLSID。您需要使用友好名称或名字来查找它。此代码行不适用:

hr = InitFilter(x264EncodeGUID, &pEncoder);

这里的说明: Choosing a Compression Filter

另请参阅:Encoding with DirectShow filters