如何为 WindowsMediaFoundation H.264 编码器 MFT 创建 IMFSample

Posted

技术标签:

【中文标题】如何为 WindowsMediaFoundation H.264 编码器 MFT 创建 IMFSample【英文标题】:How to create IMFSample for WindowsMediaFoundation H.264 encoder MFT 【发布时间】:2015-06-14 01:52:16 【问题描述】:

我正在学习使用 Windows Media Foundation 中的 H.264 编码器。

我目前拥有的是 YUV420p 格式的媒体样本,所以这是包含 YYYYYYYYUUVV 数据的缓冲区。

由于 H.264 编码器 MFT 需要 IMFSample 形式的输入,我不确定如何将缓冲区中的数据转换为 IMFSample。

我可以这样做吗:

IMFMediaBuffer *pBuffer = NULL;
MFCreateMemoryBuffer(cbSize, &pBuffer);
BYTE *pData = NULL;
pBuffer->Lock(&pData, NULL, NULL);
memcpy(pData, bufferIhaveinYYYYUV format, buffer size); // is it correct?
pBuffer->Unlock();
IMFSample *pSample = NULL;
MFCreateSample(&pSample);
pSample->AddBuffer(pBuffer);

谢谢

【问题讨论】:

【参考方案1】:

我就是这样做的(https://github.com/sipsorcery/mediafoundationsamples/blob/master/MFMP4ToYUVWithMFT/MFMP4ToYUVWithMFT.cpp 的完整示例):

IMFMediaBuffer *srcBuf = NULL;
DWORD srcBufLength;
byte *srcByteBuffer;
DWORD srcBuffCurrLen = 0;
DWORD srcBuffMaxLen = 0;
CHECK_HR(videoSample->ConvertToContiguousBuffer(&srcBuf), "ConvertToContiguousBuffer failed.\n");
CHECK_HR(srcBuf->GetCurrentLength(&srcBufLength), "Get buffer length failed.\n");
CHECK_HR(srcBuf->Lock(&srcByteBuffer, &srcBuffMaxLen, &srcBuffCurrLen), "Error locking source buffer.\n");

//// Now re-constuct.
MFCreateSample(&reConstructedVideoSample);
CHECK_HR(MFCreateMemoryBuffer(srcBufLength, &reConstructedBuffer), "Failed to create memory buffer.\n");
CHECK_HR(reConstructedVideoSample->AddBuffer(reConstructedBuffer), "Failed to add buffer to re-constructed sample.\n");
CHECK_HR(reConstructedVideoSample->SetSampleTime(llVideoTimeStamp), "Error setting the recon video sample time.\n");
CHECK_HR(reConstructedVideoSample->SetSampleDuration(llSampleDuration), "Error setting recon video sample duration.\n");

byte *reconByteBuffer;
DWORD reconBuffCurrLen = 0;
DWORD reconBuffMaxLen = 0;
CHECK_HR(reConstructedBuffer->Lock(&reconByteBuffer, &reconBuffMaxLen, &reconBuffCurrLen), "Error locking recon buffer.\n");
memcpy(reconByteBuffer, srcByteBuffer, srcBuffCurrLen);
CHECK_HR(reConstructedBuffer->Unlock(), "Error unlocking recon buffer.\n");
reConstructedBuffer->SetCurrentLength(srcBuffCurrLen);

【讨论】:

以上是关于如何为 WindowsMediaFoundation H.264 编码器 MFT 创建 IMFSample的主要内容,如果未能解决你的问题,请参考以下文章

如何为下拉菜单制作 CSS 边框?

如何为 CAShapeLayer 路径和填充颜色设置动画

iPhone - 如何为图像着色?

如何为 RecyclerView 设置 onItemClickListener? [复制]

WCF - 如何为 NTLM 身份验证配置 netTcpBinding?

如何为 UIToolbar 上的按钮添加滚动?