我能够使用 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;
}
第一个关键是确保您已配置接收器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.
我还应该提到,您应该考虑在开始时创建一次转换样本和内存缓冲区,而不是在收到的每个样本上重新创建它们。
祝你好运。我希望这有帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)