MFT编码器(h264)CPU利用率高

时间:2015-03-28 20:30:47

标签: h.264 ms-media-foundation

我能够使用Media Foundation Transform(MFT)成功地通过H264对数据进行编码,但不幸的是我得到了一个非常高的CPU(当我在程序中注释这个函数的调用我得到了低CPU)。这是几步接下来得到编码所以我无法做任何改进吗?任何想法都可以帮助

    HRESULT MFTransform::EncodeSample(IMFSample *videosample, LONGLONG llVideoTimeStamp, MFT_OUTPUT_STREAM_INFO &StreamInfo, MFT_OUTPUT_DATA_BUFFER &encDataBuffer)
{
    HRESULT hr;
    LONGLONG llSampleDuration;
    DWORD mftEncFlags, processOutputStatus;
    //used to set the output sample
    IMFSample *mftEncodedSample;
    //used to set the output sample
    IMFMediaBuffer *mftEncodedBuffer = NULL;
    memset(&encDataBuffer, 0, sizeof encDataBuffer);
    if (videosample)
    {
        //1=set the time stamp for the sample
        hr = videosample->SetSampleTime(llVideoTimeStamp);
        #ifdef _DEBUG
        printf("Passing sample to the H264 encoder with sample time %i.\n", llVideoTimeStamp);
        #endif
        if (SUCCEEDED(hr))
        {
            hr = MFT_encoder->ProcessInput(0, videosample, 0);
        }
        if (SUCCEEDED(hr))
        {
            MFT_encoder->GetOutputStatus(&mftEncFlags);
        }
        if (mftEncFlags == MFT_OUTPUT_STATUS_SAMPLE_READY)
        {
            hr = MFT_encoder->GetOutputStreamInfo(0, &StreamInfo);

            //create empty encoded sample
            if (SUCCEEDED(hr))
            {
                hr = MFCreateSample(&mftEncodedSample);
            }
            if (SUCCEEDED(hr))
            {
                hr = MFCreateMemoryBuffer(StreamInfo.cbSize, &mftEncodedBuffer);
            }
            if (SUCCEEDED(hr))
            {
                hr = mftEncodedSample->AddBuffer(mftEncodedBuffer);
            }
            if (SUCCEEDED(hr))
            {
                encDataBuffer.dwStatus = 0;
                encDataBuffer.pEvents = 0;
                encDataBuffer.dwStreamID = 0;
                //Two shall after this step points on the same address
                encDataBuffer.pSample = mftEncodedSample;
                hr = MFT_encoder->ProcessOutput(0, 1, &encDataBuffer, &processOutputStatus);


            }
        }
    }
    SafeRelease(&mftEncodedBuffer);

    return hr;
}

1 个答案:

答案 0 :(得分:2)

第一个关键是确保您使用MF_READWRITE_ENABLE_HARDWARE_TRANSFORMS配置接收器。我还设置了MF_LOW_LATENCY属性。

// error checking omitted for brevity
hr = attributes->SetUINT32(MF_READWRITE_ENABLE_HARDWARE_TRANSFORMS, TRUE);
hr = attributes->SetUINT32(MF_SINK_WRITER_DISABLE_THROTTLING, TRUE);
hr = attributes->SetUINT32(MF_LOW_LATENCY, TRUE);

另一个关键是确保您为源的输出选择本机格式。否则,你会非常失望。我详细描述了here

我还应该提一下,你应该考虑在开始时创建一次变换样本和内存缓冲区,而不是在每个接收到的样本上重新创建它们。

祝你好运。我希望这会有所帮助。