3

I am able successfully to encode the data by H264 using Media Foundation Transform (MFT) but unfortunately I got a very high CPU(when I comment in the program the calling of this function I got low CPU).It is few steps followed to get the encoding so I can't do anything to improve it?Any idea can help

    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;
}
tulipe
  • 676
  • 2
  • 8
  • 22
  • It is reasonable that it requires high CPU usage. Do you aim to encode in real time? How do you configure the encoder (profile, bitrate, etc)? – Anton Angelov Apr 06 '15 at 08:49
  • @AntonAngelov Frame rate (33 frame/s) ...bit rate (400kbps)...resolution (640x480)....yes encoder works in real time – tulipe Apr 07 '15 at 13:42

1 Answers1

2

The first key is to ensure you have configured the sink with MF_READWRITE_ENABLE_HARDWARE_TRANSFORMS. I also set the MF_LOW_LATENCY attribute.

// 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);

The other key is to ensure you are selecting the native format for the output of the source. Otherwise, you will remain very disappointed. I describe this in detail here.

I should also mention that you should consider creating the transform sample and memory buffer once at the beginning, instead of recreating them on each sample received.

Good luck. I hope this helps.

Community
  • 1
  • 1
Jeff
  • 2,495
  • 18
  • 38