5 #include <Propvarutil.h>
11 initializeFrameLoader();
16 initializeFrameLoader();
20 void CVideoFrameLoader::initializeFrameLoader()
29 m_totalFrameCount = 0;
32 m_videoBufferTransfered =
false;
35 if (SUCCEEDED(CoInitializeEx(0, COINIT_MULTITHREADED)))
36 m_comInitialisationSuccessful =
true;
39 if (SUCCEEDED(MFStartup(MF_VERSION)))
40 m_mediaFoundationInitalisationSuccessful =
true;
54 while (!p_videoList->empty())
56 PBYTE frame = p_videoList->front();
57 p_videoList->pop_front();
65 SafeRelease((IUnknown**) &p_videoReader);
67 if (m_mediaFoundationInitalisationSuccessful)
70 if (m_comInitialisationSuccessful)
81 SafeRelease((IUnknown**) &p_videoReader);
104 if(FAILED(UrlCreateFromPath(filePath, p_videoURL, &videoURLLength, NULL)))
107 return openFileInSourceReader();
122 if (StringCbCopy(p_videoURL,
MAXVideoURLLenght, fileURL) == STRSAFE_E_INSUFFICIENT_BUFFER)
125 return openFileInSourceReader();
128 VFLResult CVideoFrameLoader::openFileInSourceReader()
130 IMFAttributes *pSourceReaderAttributes = NULL;
132 MFCreateAttributes(&pSourceReaderAttributes, 1);
134 pSourceReaderAttributes->SetUINT32(MF_SOURCE_READER_ENABLE_VIDEO_PROCESSING, TRUE);
137 if(FAILED(MFCreateSourceReaderFromURL(p_videoURL, pSourceReaderAttributes, &p_videoReader)))
139 SafeRelease((IUnknown **) &pSourceReaderAttributes);
143 SafeRelease((IUnknown **) &pSourceReaderAttributes);
145 if(FAILED(configureDecoder(p_videoReader)))
150 if (FAILED(p_videoReader->GetCurrentMediaType(MF_SOURCE_READER_FIRST_VIDEO_STREAM, &pType)))
153 if (FAILED(MFGetAttributeSize(pType, MF_MT_FRAME_SIZE, &m_videoWidth, &m_videoHeight)))
187 while (vr ==
VFL_OK && m_totalFrameCount + distanceBetween < lastFrame)
206 if (p_videoList == NULL)
207 p_videoList =
new list<PBYTE>;
209 IMFSample *pSample = NULL;
210 DWORD streamIndex,flags;
211 LONGLONG llTimeStamp;
213 for (
int frameCount = 0; frameCount < distanceFromLast + 1; frameCount++)
215 SafeRelease((IUnknown**) &pSample);
217 p_videoReader->ReadSample(
218 MF_SOURCE_READER_FIRST_VIDEO_STREAM,
234 copyFrameToBuffer(pSample);
235 SafeRelease((IUnknown**) &pSample);
243 if (m_videoWidth == 0 || m_videoHeight == 0)
246 *width = m_videoWidth;
247 *height = m_videoHeight;
261 m_videoBufferTransfered =
true;
268 if (m_frameCount == 0)
271 *frameCount = m_frameCount;
278 if (p_videoReader != NULL)
283 while (!p_videoList->empty())
285 PBYTE frame = p_videoList->front();
286 p_videoList->pop_front();
299 m_totalFrameCount = 0;
300 m_videoBufferTransfered =
false;
303 HRESULT CVideoFrameLoader::configureDecoder(IMFSourceReader *pReader)
305 IMFMediaType *pType = NULL;
308 HRESULT hr = MFCreateMediaType(&pType);
311 SafeRelease((IUnknown**) &pType);
315 hr = pType->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Video);
318 SafeRelease((IUnknown**) &pType);
322 hr = pType->SetGUID(MF_MT_SUBTYPE, MFVideoFormat_RGB32);
325 SafeRelease((IUnknown**) &pType);
330 hr = pReader->SetCurrentMediaType(MF_SOURCE_READER_FIRST_VIDEO_STREAM, NULL, pType);
333 SafeRelease((IUnknown**) &pType);
343 HRESULT hr = InitPropVariantFromInt64(hnsPosition, &var);
346 hr = p_videoReader->SetCurrentPosition(GUID_NULL, var);
347 PropVariantClear(&var);
355 int byteCount = m_videoWidth * m_videoHeight * 4;
357 p_videoArray =
new BYTE[(long) byteCount * (
long) m_frameCount];
359 for (
int frameCounter = 0; frameCounter < m_frameCount; frameCounter++)
361 PBYTE frame = p_videoList->front();
362 p_videoList->pop_front();
364 for (
int byteCounter = 0; byteCounter < byteCount; byteCounter++)
366 p_videoArray[(long) frameCounter * (
long) byteCount + byteCounter] = frame[byteCounter];
376 VFLResult CVideoFrameLoader::copyFrameToBuffer(IMFSample *pSample)
378 IMFMediaBuffer * pSampleBuffer = NULL;
380 pSample->ConvertToContiguousBuffer(&pSampleBuffer);
382 PBYTE singleFrameByteBuffer;
384 int byteCount = m_videoWidth * m_videoHeight * 4;
385 PBYTE singleFrameByteBufferCopy =
new BYTE[byteCount];
387 pSampleBuffer->Lock(&singleFrameByteBuffer, NULL, NULL);
390 for (
int byteCounter = 0; byteCounter < byteCount; byteCounter++)
391 singleFrameByteBufferCopy[byteCounter] = singleFrameByteBuffer[byteCounter];
395 pSampleBuffer->Unlock();
396 pSampleBuffer->Release();
398 p_videoList->push_back(singleFrameByteBufferCopy);
403 void CVideoFrameLoader::SafeRelease(IUnknown **ppT)