基本上,我有一个简洁的H.264字节流,采用I和P样本的形式。我可以使用MediaStreamSource和MediaElement播放这些样本,它们可以很好地播放。我还需要将它们保存为MP4文件,以便以后可以使用Media Element或VLC播放。这就是我尝试使用Media Foundation的方式; 我从MFCreateMPEG4MediaSink创建了一个IMFMediaSink;这是我的代码:
IMFMediaType *pMediaType = NULL;
IMFByteStream *pByteStream = NULL;
HRESULT hr = S_OK;
if (SUCCEEDED(hr))
{
hr = MFCreateMediaType(&pMediaType);
}
pSeqHdr = reinterpret_cast<UINT8 *>(mSamplesQueue.SequenceHeader());
if (SUCCEEDED(hr))
{
hr = pMediaType->SetBlob(MF_MT_MPEG_SEQUENCE_HEADER, pSeqHdr, 35);
}
UINT32 pcbBlobSize = {0};
hr = pMediaType->GetBlobSize(MF_MT_MPEG_SEQUENCE_HEADER, &pcbBlobSize);
/*if (SUCCEEDED(hr))
{
hr = pMediaType->SetUINT32(MF_MPEG4SINK_SPSPPS_PASSTHROUGH, TRUE);
}*/
if (SUCCEEDED(hr))
{
hr = pMediaType->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Video);
}
if (SUCCEEDED(hr))
{
hr = pMediaType->SetGUID(MF_MT_SUBTYPE, VIDEO_INPUT_FORMAT);
}
if (SUCCEEDED(hr))
{
hr = MFSetAttributeRatio(pMediaType, MF_MT_FRAME_RATE, VIDEO_FPS, 1);
}
if (SUCCEEDED(hr))
{
hr = pMediaType->SetUINT32(MF_MT_AVG_BITRATE, VIDEO_BIT_RATE);
}
if (SUCCEEDED(hr))
{
hr = pMediaType->SetUINT32(MF_MT_INTERLACE_MODE, MFVideoInterlace_Progressive);
}
if (SUCCEEDED(hr))
{
hr = MFSetAttributeSize(pMediaType, MF_MT_FRAME_SIZE, VIDEO_WIDTH, VIDEO_HEIGHT);
}
if (SUCCEEDED(hr))
{
// Pixel aspect ratio
hr = MFSetAttributeRatio(pMediaType, MF_MT_PIXEL_ASPECT_RATIO, 1, 1);
}
if (SUCCEEDED(hr))
{
hr = MFCreateFile(
MF_ACCESSMODE_READWRITE,
MF_OPENMODE_DELETE_IF_EXIST,
MF_FILEFLAGS_NONE,
L"output1.mp4",
&pByteStream);
}
if (SUCCEEDED(hr))
{
hr = MFCreateMPEG4MediaSink(
pByteStream,
pMediaType,
NULL,
&pMediaSink);
}
然后我使用MFCreateSinkWriterFromMediaSink从此媒体接收器创建IMFSinkWriter;这是我的代码:
if (SUCCEEDED(hr))
{
hr = MFCreateSinkWriterFromMediaSink(pMediaSink, NULL, &pSinkWriter);
}
// Tell the sink writer to start accepting data.
if (SUCCEEDED(hr))
{
hr = pSinkWriter->BeginWriting();
}
if (SUCCEEDED(hr))
{
pSinkWriter->AddRef();
}
然后我用IMFSinkWriter :: WriteSample(0,IMFSample)将每个样本写入接收器编写器;这是我的代码: IMFSample * pSample = NULL; IMFMediaBuffer * pBuffer = NULL;
const DWORD cbBuffer = mSamplesQueue.GetNextSampleSize();
UINT32 isIDR = mSamplesQueue.GetNextSampleIsIDR();
BYTE *pData = NULL;
// Create a new memory buffer.
HRESULT hr = MFCreateMemoryBuffer(cbBuffer, &pBuffer);
// Lock the buffer and copy the video frame to the buffer.
if (SUCCEEDED(hr))
{
DWORD buffLen = cbBuffer;
hr = pBuffer->Lock(&pData, &buffLen, 0);
}
if (SUCCEEDED(hr))
{
hr = mSamplesQueue.Dequeu(&pData);
}
if (pBuffer)
{
pBuffer->Unlock();
}
// Set the data length of the buffer.
if (SUCCEEDED(hr))
{
hr = pBuffer->SetCurrentLength(cbBuffer);
}
// Create a media sample and add the buffer to the sample.
if (SUCCEEDED(hr))
{
hr = MFCreateSample(&pSample);
}
if (SUCCEEDED(hr))
{
hr = pSample->AddBuffer(pBuffer);
}
// Set the time stamp and the duration.
if (SUCCEEDED(hr))
{
hr = pSample->SetSampleTime(rtStart);
}
if (SUCCEEDED(hr))
{
hr = pSample->SetSampleDuration(rtDuration);
}
if (SUCCEEDED(hr))
{
hr = pSample->SetUINT32(MFSampleExtension_CleanPoint, isIDR);
}
//pSample->
// Send the sample to the Sink Writer.
if (SUCCEEDED(hr))
{
hr = pSinkWriter->WriteSample(0, pSample);
}
SafeRelease(&pSample);
SafeRelease(&pBuffer);
样本的编写是一个迭代代码,从我拥有的每个样本调用(我正在使用1k I和P样本进行测试)。现在,当我调用IMFSinkWriter :: Finalize()时,它告诉我“0xc00d4a45:Sink无法创建有效的输出文件,因为未向接收器提供所需的标头。”。它确实创建了一个非常有效的MP4文件(对于我的1k样本,4.6 MB)。 This is the link to the trace from MFTrace. 如果它要求MF_MT_MPEG_SEQUENCE_HEADER,那么我用IMFMediaType :: SetBlob(MF_MT_MPEG_SEQUENCE_HEADER,BYTE [],UINT32)设置它们 我用Elecard视频格式分析器检查了文件,标题似乎不完整。 我可以帮助找出我缺少的东西,或者是否有更好的/其他方式来做我想要实现的目标? 谢谢!
答案 0 :(得分:4)
对我来说,问题是MF_MT_MPEG_SEQUENCE_HEADER blob的格式。
与dwSequenceHeader和H.264上的MSDN文档相比,SPS和PPS应该在前面加上起始码(0x00,0x00,0x01)而不是2字节长度字段。
http://msdn.microsoft.com/en-us/library/dd757808%28VS.85%29.aspx
答案 1 :(得分:0)
不确定,但不应该:
hr = pMediaType->SetGUID(MF_MT_SUBTYPE, VIDEO_ENCODING_FORMAT);
而不是:
hr = pMediaType->SetGUID(MF_MT_SUBTYPE, VIDEO_INPUT_FORMAT);