gpt4 book ai didi

c++ - 如何复制 IMFSample 对象

转载 作者:可可西里 更新时间:2023-11-01 10:33:53 34 4
gpt4 key购买 nike

我需要复制 IMFSample 对象,这样我就会有单独的独立对象。Microsoft API 没有提供简单的方法来复制 IMFSample 视频帧。

最佳答案

这是一个示例代码:

#define MmfThrowIfError(hrVal) \
{ \
HRESULT hrMmfTraceInternal = (hrVal); \
if (FAILED(hrMmfTraceInternal)) \
{ \
/* LOG_F(LS_ERROR)<<"MMF hr: 0x"<<std::hex<<hrMmfTraceInternal; */ \
throw ref new ::Platform::Exception(hrMmfTraceInternal); \
} \
}

MFComPtr<IMFMediaBuffer> DuplicateBuffer(const MFComPtr<IMFMediaBuffer>& srcBuf)
{
byte* srcByteBuffer = nullptr;
DWORD srcBuffMaxLen = 0;
DWORD srcBuffCurrLen = 0;
MmfThrowIfError(srcBuf->Lock(&srcByteBuffer, &srcBuffMaxLen, &srcBuffCurrLen));

MFComPtr<IMFMediaBuffer> destBuf = nullptr;
MmfThrowIfError(MFCreateMemoryBuffer(srcBuffCurrLen, &destBuf));

byte* destByteBuffer = nullptr;
MmfThrowIfError(destBuf->Lock(&destByteBuffer, nullptr, nullptr));
memcpy(destByteBuffer, srcByteBuffer, srcBuffCurrLen);
MmfThrowIfError(destBuf->Unlock());
MmfThrowIfError(srcBuf->Unlock());

MmfThrowIfError(destBuf->SetCurrentLength(srcBuffCurrLen));
return destBuf;
}

SampleComPtr DuplicateSample(const SampleComPtr& sample)
{
if (!sample)
return nullptr;

DWORD sampleFlags = 0;
LONGLONG llVideoTimeStamp = 0;
LONGLONG llSampleDuration = 0;
MmfThrowIfError(sample->GetSampleFlags(&sampleFlags));
MmfThrowIfError(sample->GetSampleTime(&llVideoTimeStamp));
MmfThrowIfError(sample->GetSampleDuration(&llSampleDuration));

SampleComPtr outSample;
MFCreateSample(&outSample);
MmfThrowIfError(outSample->SetSampleFlags(sampleFlags));
MmfThrowIfError(outSample->SetSampleTime(llVideoTimeStamp));
MmfThrowIfError(outSample->SetSampleDuration(llSampleDuration));

DWORD bufferCount = 0;
MmfThrowIfError(sample->GetBufferCount(&bufferCount));

for (DWORD index = 0; index < bufferCount; ++index)
{
MFComPtr<IMFMediaBuffer> srcBuf = nullptr;
MmfThrowIfError(sample->GetBufferByIndex(index, &srcBuf));

MFComPtr<IMFMediaBuffer> reConstructedBuffer = DuplicateBuffer(srcBuf);
srcBuf = nullptr;
MmfThrowIfError(outSample->AddBuffer(reConstructedBuffer.Get()));
}

return outSample;
}

关于c++ - 如何复制 IMFSample 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38354857/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com