简体   繁体   English

实施“ IMFTransform”以编码或解码H264或AAC

[英]Implement 'IMFTransform' to encode or decode H264 or AAC

IMFTransform可以实现IMFTransform接口以对H264或AAC数据进行编码或解码,还是应该使用FFmpegOpenH264

When you encode or decode media, IMFTransform is the interface codecs expose in Media Foundation API. 在对媒体进行编码或解码时, IMFTransform是Media Foundation API中公开的接口编解码器。 That is, you don't implement it - you take advantage of existing implementation of codecs which are available to you (you implement it when you want to extend the API and supply additional codec). 也就是说,您没有实现它-您可以利用现有的编解码器实现(您想扩展API并提供其他编解码器时就可以实现)。

Stock Windows provides you with: Stock Windows为您提供:

Additional hardware accelerated encoders might be provided with hardware drivers. 附加的硬件加速编码器可能随硬件驱动程序一起提供。 All mentioned above are available in the form of IMFTransform , can be consumed directly or using higher level Media Foundation APIs. 上面提到的所有内容都以IMFTransform的形式提供,可以直接使用,也可以使用更高级别的Media Foundation API来使用。

You can implement the IMFTransform interface to decode and encode H264 and AAC. 您可以实现IMFTransform接口以对H264和AAC进行解码和编码。 Refer to CLSID_CMSH264DecoderMFT and CLSID_CMSAACDecMFT to decode H264 and ACC, also CLSID_CMSH264EncoderMFT and CLSID_AACMFTEncoder to encode H264 and ACC. 请参考CLSID_CMSH264DecoderMFTCLSID_CMSAACDecMFT来解码H264和ACC,还请参考CLSID_CMSH264EncoderMFTCLSID_AACMFTEncoder来编码H264和ACC。

Encoder example : initialise the encoder. 编码器示例:初始化编码器。

        IUnknown    *_transformUnk;
        IMFTransform *_encoder;

        HRESULT MediaEncoder::InitialiseEncoder(EncoderType encoder)
        {
            HRESULT hr = S_OK;

            // Has the encoder been init.
            if (!_isOpen)
            {
                _encoderType = encoder;

                // Init the COM.
                CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);

                // Create a new close event handler.
                _hCloseEvent = CreateEvent(NULL, FALSE, FALSE, NULL);

                // If event was not created.
                if (_hCloseEvent == NULL)
                {
                    // Get the result value.
                    hr = __HRESULT_FROM_WIN32(GetLastError());
                }

                // If successful creation of the close event.
                if (SUCCEEDED(hr))
                {
                    // Start up Media Foundation platform.
                    hr = MFStartup(MF_VERSION);
                    _isOpen = true;
                }

                if (SUCCEEDED(hr))
                {
                    // Select the encoder.
                    switch (encoder)
                    {
                    case Nequeo::Media::Foundation::EncoderType::H264:
                        // Create the H264 encoder.
                        hr = CreateEncoder(CLSID_CMSH264EncoderMFT);
                        break;

                    case Nequeo::Media::Foundation::EncoderType::AAC:
                        // Create the AAC encoder.
                        hr = CreateEncoder(CLSID_AACMFTEncoder);
                        break;

                    case Nequeo::Media::Foundation::EncoderType::MP3:
                        // Create the MP3 encoder.
                        hr = CreateEncoder(CLSID_MP3ACMCodecWrapper);
                        break;

                    default:
                        hr = ((HRESULT)-1L);
                        break;
                    }
                }

                if (SUCCEEDED(hr))
                {
                    // Query for the IMFTransform interface 
                    hr = _transformUnk->QueryInterface(IID_PPV_ARGS(&_encoder));

                    // Encoder has been created.
                    _created = true;
                }
            }

            // Return the result.
            return hr;
        }

        HRESULT MediaEncoder::CreateEncoder(const CLSID encoder)
        {
            HRESULT hr = S_OK;

            // Create the decoder.
            hr = CoCreateInstance(encoder, NULL, CLSCTX_INPROC_SERVER, IID_IUnknown, (void**)&_transformUnk);

            // Return the result.
            return hr;
        }

Decoder example : initialise the decoder. 解码器示例:初始化解码器。

    IUnknown    *_transformUnk;
    IMFTransform    *_decoder;

    HRESULT MediaDecoder::InitialiseDecoder(DecoderType decoder)
        {
            HRESULT hr = S_OK;

            // Has the decoder been init.
            if (!_isOpen)
            {
                _decoderType = decoder;

                // Init the COM.
                CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);

                // Create a new close event handler.
                _hCloseEvent = CreateEvent(NULL, FALSE, FALSE, NULL);

                // If event was not created.
                if (_hCloseEvent == NULL)
                {
                    // Get the result value.
                    hr = __HRESULT_FROM_WIN32(GetLastError());
                }

                // If successful creation of the close event.
                if (SUCCEEDED(hr))
                {
                    // Start up Media Foundation platform.
                    hr = MFStartup(MF_VERSION);
                    _isOpen = true;
                }

                if (SUCCEEDED(hr))
                {
                    // Select the decoder.
                    switch (decoder)
                    {
                    case Nequeo::Media::Foundation::DecoderType::H264:
                        // Create the H264 decoder.
                        hr = CreateDecoder(CLSID_CMSH264DecoderMFT);
                        break;

                    case Nequeo::Media::Foundation::DecoderType::AAC:
                        // Create the AAC decoder.
                        hr = CreateDecoder(CLSID_CMSAACDecMFT);
                        break;

                    case Nequeo::Media::Foundation::DecoderType::MP3:
                        // Create the MP3 decoder.
                        hr = CreateDecoder(CLSID_CMP3DecMediaObject);
                        break;

                    case Nequeo::Media::Foundation::DecoderType::MPEG4:
                        // Create the MPEG4 decoder.
                        hr = CreateDecoder(CLSID_CMpeg4sDecMFT);
                        break;

                    default:
                        hr = ((HRESULT)-1L);
                        break;
                    }
                }

                if (SUCCEEDED(hr))
                {
                    // Query for the IMFTransform interface 
                    hr = _transformUnk->QueryInterface(IID_PPV_ARGS(&_decoder));

                    // Decoder has been created.
                    _created = true;
                }
            }

            // Return the result.
            return hr;
        }

        HRESULT MediaDecoder::CreateDecoder(const CLSID decoder)
        {
            HRESULT hr = S_OK;

            // Create the decoder.
            hr = CoCreateInstance(decoder, NULL, CLSCTX_INPROC_SERVER, IID_IUnknown, (void**)&_transformUnk);

            // Return the result.
            return hr;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM