2 * Implements AVI Parser(Splitter).
4 * hidenori@a2.ctktv.ne.jp
21 #include "debugtools.h"
22 DEFAULT_DEBUG_CHANNEL(quartz);
24 #include "quartz_private.h"
30 static const WCHAR QUARTZ_AVIParser_Name[] =
31 { 'A','V','I',' ','S','p','l','i','t','t','e','r',0 };
32 static const WCHAR QUARTZ_AVIParserInPin_Name[] =
34 static const WCHAR QUARTZ_AVIParserOutPin_Basename[] =
35 { 'S','t','r','e','a','m',0 };
37 #define WINE_QUARTZ_AVIPINNAME_MAX 64
39 /****************************************************************************
45 typedef struct CAVIParseImpl CAVIParseImpl;
46 typedef struct CAVIParseStream CAVIParseStream;
51 CAVIParseStream* pStreamsBuf;
53 AVIINDEXENTRY* pIndexEntriesBuf;
54 WCHAR wchWork[ WINE_QUARTZ_AVIPINNAME_MAX ];
57 struct CAVIParseStream
63 AVIINDEXENTRY* pIndexEntries;
66 REFERENCE_TIME rtInternal;
70 static HRESULT CAVIParseImpl_ParseStreamList(
71 CParserImpl* pImpl, CAVIParseImpl* This, ULONG nStreamIndex,
72 LONGLONG llOfsTop, DWORD dwListLen, CAVIParseStream* pStream )
78 TRACE("search strh\n");
79 hr = RIFF_SearchChunk(
81 llOfsTop, PARSER_strh,
82 &llOfs, &dwChunkLength );
85 TRACE("strh has been detected\n");
86 if ( dwChunkLength < sizeof(AVIStreamHeader) )
89 hr = IAsyncReader_SyncRead( pImpl->m_pReader,
90 llOfs, sizeof(AVIStreamHeader), (BYTE*)&pStream->strh );
97 TRACE("search strf\n");
98 hr = RIFF_SearchChunk(
100 llOfsTop, PARSER_strf,
101 &llOfs, &dwChunkLength );
102 if ( hr == S_OK && dwChunkLength > 0 )
104 TRACE("strf has been detected\n");
105 pStream->cbFmt = dwChunkLength;
106 pStream->pFmtBuf = (BYTE*)QUARTZ_AllocMem( dwChunkLength );
107 if ( pStream->pFmtBuf == NULL )
110 hr = IAsyncReader_SyncRead( pImpl->m_pReader,
111 llOfs, dwChunkLength, pStream->pFmtBuf );
116 TRACE("search indx\n");
117 hr = RIFF_SearchChunk(
119 llOfsTop, PARSER_indx,
120 &llOfs, &dwChunkLength );
125 FIXME( "'indx' has been detected - not implemented now!\n" );
133 static HRESULT CAVIParseImpl_InitParser( CParserImpl* pImpl, ULONG* pcStreams )
135 CAVIParseImpl* This = NULL;
145 AVIINDEXENTRY* pEntriesBuf = NULL;
149 TRACE("(%p,%p)\n",pImpl,pcStreams);
151 hr = IAsyncReader_SyncRead( pImpl->m_pReader, 0, 12, riffhdr );
156 if ( memcmp( &riffhdr[0], "RIFF", 4 ) != 0 ||
157 memcmp( &riffhdr[8], "AVI ", 4 ) != 0 )
162 This = (CAVIParseImpl*)QUARTZ_AllocMem( sizeof(CAVIParseImpl) );
164 return E_OUTOFMEMORY;
165 pImpl->m_pUserData = This;
166 ZeroMemory( This, sizeof(CAVIParseImpl) );
167 This->pStreamsBuf = NULL;
168 This->cIndexEntries = 0;
169 This->pIndexEntriesBuf = 0;
171 hr = RIFF_SearchList(
172 pImpl, (DWORD)0xffffffff,
173 PARSER_RIFF_OfsFirst, PARSER_hdrl,
174 &llOfs_hdrl, &dwLen_hdrl );
181 TRACE("read avih\n");
182 hr = RIFF_SearchChunk(
184 llOfs_hdrl, PARSER_avih,
185 &llOfs, &dwChunkLength );
191 if ( dwChunkLength > sizeof(MainAVIHeader) )
192 dwChunkLength = sizeof(MainAVIHeader);
193 hr = IAsyncReader_SyncRead( pImpl->m_pReader, llOfs, dwChunkLength, (BYTE*)&(This->avih) );
198 if ( This->avih.dwStreams == 0 )
201 /* initialize streams. */
202 This->pStreamsBuf = (CAVIParseStream*)QUARTZ_AllocMem(
203 sizeof(CAVIParseStream) * This->avih.dwStreams );
204 if ( This->pStreamsBuf == NULL )
205 return E_OUTOFMEMORY;
206 ZeroMemory( This->pStreamsBuf,
207 sizeof(CAVIParseStream) * This->avih.dwStreams );
210 for ( nIndex = 0; nIndex < This->avih.dwStreams; nIndex++ )
212 TRACE("search strl for stream %lu\n",nIndex);
213 hr = RIFF_SearchList(
215 dwLen_hdrl, llOfs, PARSER_strl,
216 &llOfs, &dwChunkLength );
223 hr = CAVIParseImpl_ParseStreamList(
225 llOfs, dwChunkLength, &This->pStreamsBuf[nIndex] );
231 llOfs += dwChunkLength;
234 /* initialize idx1. */
235 TRACE("search idx1\n");
236 hr = RIFF_SearchChunk(
237 pImpl, (DWORD)0xffffffff,
238 PARSER_RIFF_OfsFirst, PARSER_idx1,
239 &llOfs, &dwChunkLength );
245 This->cIndexEntries = dwChunkLength / sizeof(AVIINDEXENTRY);
246 This->pIndexEntriesBuf = (AVIINDEXENTRY*)QUARTZ_AllocMem(
247 sizeof(AVIINDEXENTRY) * This->cIndexEntries );
248 if ( This->pIndexEntriesBuf == NULL )
249 return E_OUTOFMEMORY;
250 hr = IAsyncReader_SyncRead( pImpl->m_pReader, llOfs, sizeof(AVIINDEXENTRY) * This->cIndexEntries, (BYTE*)This->pIndexEntriesBuf );
256 pEntriesBuf = (AVIINDEXENTRY*)QUARTZ_AllocMem(
257 sizeof(AVIINDEXENTRY) * This->cIndexEntries );
258 if ( pEntriesBuf == NULL )
259 return E_OUTOFMEMORY;
261 for ( nIndex = 0; nIndex < This->avih.dwStreams; nIndex++ )
263 cEntriesCur = cEntries;
264 dwChunkId = (((nIndex%10)+'0')<<8) | ((nIndex/10)+'0');
265 for ( i = 0; i < This->cIndexEntries; i++ )
267 if ( (This->pIndexEntriesBuf[i].ckid & 0xffff) == dwChunkId )
268 memcpy( &pEntriesBuf[cEntries++], &This->pIndexEntriesBuf[i], sizeof(AVIINDEXENTRY) );
270 This->pStreamsBuf[nIndex].pIndexEntries = &pEntriesBuf[cEntriesCur];
271 This->pStreamsBuf[nIndex].cIndexEntries = cEntries - cEntriesCur;
272 This->pStreamsBuf[nIndex].cIndexCur = 0;
273 This->pStreamsBuf[nIndex].rtCur = 0;
274 This->pStreamsBuf[nIndex].rtInternal = 0;
275 TRACE("stream %lu - %lu entries\n",nIndex,This->pStreamsBuf[nIndex].cIndexEntries);
277 QUARTZ_FreeMem(This->pIndexEntriesBuf);
278 This->pIndexEntriesBuf = pEntriesBuf;
280 This->avih.dwSuggestedBufferSize = 0;
281 for ( i = 0; i < This->cIndexEntries; i++ )
283 if ( This->avih.dwSuggestedBufferSize < This->pIndexEntriesBuf[i].dwChunkLength )
284 This->avih.dwSuggestedBufferSize = This->pIndexEntriesBuf[i].dwChunkLength;
292 if ( This->avih.dwStreams > 100 )
295 *pcStreams = This->avih.dwStreams;
300 static HRESULT CAVIParseImpl_UninitParser( CParserImpl* pImpl )
302 CAVIParseImpl* This = (CAVIParseImpl*)pImpl->m_pUserData;
305 TRACE("(%p)\n",This);
311 if ( This->pStreamsBuf != NULL )
313 for ( nIndex = 0; nIndex < This->avih.dwStreams; nIndex++ )
315 /* release this stream */
316 if ( This->pStreamsBuf[nIndex].pFmtBuf != NULL )
317 QUARTZ_FreeMem(This->pStreamsBuf[nIndex].pFmtBuf);
319 QUARTZ_FreeMem( This->pStreamsBuf );
320 This->pStreamsBuf = NULL;
323 if ( This->pIndexEntriesBuf != NULL )
325 QUARTZ_FreeMem( This->pIndexEntriesBuf );
326 This->pIndexEntriesBuf = NULL;
329 QUARTZ_FreeMem( This );
330 pImpl->m_pUserData = NULL;
335 static LPCWSTR CAVIParseImpl_GetOutPinName( CParserImpl* pImpl, ULONG nStreamIndex )
337 CAVIParseImpl* This = (CAVIParseImpl*)pImpl->m_pUserData;
340 TRACE("(%p,%lu)\n",This,nStreamIndex);
342 if ( This == NULL || nStreamIndex >= This->avih.dwStreams )
345 wlen = lstrlenW(QUARTZ_AVIParserOutPin_Basename);
346 memcpy( This->wchWork, QUARTZ_AVIParserOutPin_Basename, sizeof(WCHAR)*wlen );
347 This->wchWork[ wlen ] = (nStreamIndex/10) + '0';
348 This->wchWork[ wlen+1 ] = (nStreamIndex%10) + '0';
349 This->wchWork[ wlen+2 ] = 0;
351 return This->wchWork;
354 static HRESULT CAVIParseImpl_GetStreamType( CParserImpl* pImpl, ULONG nStreamIndex, AM_MEDIA_TYPE* pmt )
356 CAVIParseImpl* This = (CAVIParseImpl*)pImpl->m_pUserData;
357 VIDEOINFOHEADER* pvi;
358 BITMAPINFOHEADER* pbi;
364 TRACE("(%p,%lu,%p)\n",This,nStreamIndex,pmt);
368 if ( nStreamIndex >= This->avih.dwStreams )
371 cbFmt = This->pStreamsBuf[nStreamIndex].cbFmt;
373 ZeroMemory( pmt, sizeof(AM_MEDIA_TYPE) );
374 switch ( This->pStreamsBuf[nStreamIndex].strh.fccType )
377 pbi = (BITMAPINFOHEADER*)This->pStreamsBuf[nStreamIndex].pFmtBuf;
378 if ( pbi == NULL || cbFmt < sizeof(BITMAPINFOHEADER) )
381 memcpy( &pmt->majortype, &MEDIATYPE_Video, sizeof(GUID) );
382 hr = QUARTZ_MediaSubType_FromBitmap( &pmt->subtype, pbi );
386 QUARTZ_MediaSubType_FromFourCC( &pmt->subtype, (DWORD)pbi->biCompression );
388 pmt->bFixedSizeSamples = ( pbi->biCompression == 0 || pbi->biCompression == 3 ) ? 1 : 0;
389 pmt->bTemporalCompression = 0;
390 pmt->lSampleSize = ( pbi->biCompression == 0 || pbi->biCompression == 3 ) ?
391 DIBSIZE(*pbi) : pbi->biSize;
392 memcpy( &pmt->formattype, &FORMAT_VideoInfo, sizeof(GUID) );
394 cb = sizeof(VIDEOINFOHEADER) + cbFmt;
395 pmt->pbFormat = (BYTE*)CoTaskMemAlloc( cb );
396 if ( pmt->pbFormat == NULL )
397 return E_OUTOFMEMORY;
398 ZeroMemory( pmt->pbFormat, cb );
399 pvi = (VIDEOINFOHEADER*)pmt->pbFormat;
401 memcpy( &pvi->bmiHeader, pbi, cbFmt );
404 pwfx = (WAVEFORMATEX*)This->pStreamsBuf[nStreamIndex].pFmtBuf;
405 if ( pwfx == NULL || cbFmt < (sizeof(WAVEFORMATEX)-2) )
408 memcpy( &pmt->majortype, &MEDIATYPE_Audio, sizeof(GUID) );
409 QUARTZ_MediaSubType_FromFourCC( &pmt->subtype, (DWORD)pwfx->wFormatTag );
410 pmt->bFixedSizeSamples = 1;
411 pmt->bTemporalCompression = 0;
412 pmt->lSampleSize = pwfx->nBlockAlign;
413 memcpy( &pmt->formattype, &FORMAT_WaveFormatEx, sizeof(GUID) );
416 cb = ( cbFmt < sizeof(WAVEFORMATEX) ) ? sizeof(WAVEFORMATEX) : cbFmt;
417 pmt->pbFormat = (BYTE*)CoTaskMemAlloc( cb );
418 if ( pmt->pbFormat == NULL )
419 return E_OUTOFMEMORY;
420 ZeroMemory( pmt->pbFormat, cb );
421 pmt->cbFormat = cbFmt;
422 memcpy( pmt->pbFormat, pwfx, cbFmt );
426 memcpy( &pmt->majortype, &MEDIATYPE_Midi, sizeof(GUID) );
427 memcpy( &pmt->subtype, &MEDIASUBTYPE_NULL, sizeof(GUID) );
428 pmt->bFixedSizeSamples = 0;
429 pmt->bTemporalCompression = 0;
430 pmt->lSampleSize = 1;
431 memcpy( &pmt->formattype, &FORMAT_None, sizeof(GUID) );
434 pmt->pbFormat = NULL;
438 memcpy( &pmt->majortype, &MEDIATYPE_Text, sizeof(GUID) );
439 memcpy( &pmt->subtype, &MEDIASUBTYPE_NULL, sizeof(GUID) );
440 pmt->bFixedSizeSamples = 0;
441 pmt->bTemporalCompression = 0;
442 pmt->lSampleSize = 1;
443 memcpy( &pmt->formattype, &FORMAT_None, sizeof(GUID) );
446 pmt->pbFormat = NULL;
455 FIXME( "(%p) unsupported stream type %c%c%c%c\n",This,
456 (int)((This->pStreamsBuf[nStreamIndex].strh.fccType>> 0)&0xff),
457 (int)((This->pStreamsBuf[nStreamIndex].strh.fccType>> 8)&0xff),
458 (int)((This->pStreamsBuf[nStreamIndex].strh.fccType>>16)&0xff),
459 (int)((This->pStreamsBuf[nStreamIndex].strh.fccType>>24)&0xff) );
461 memcpy( &pmt->majortype, &MEDIATYPE_NULL, sizeof(GUID) );
462 memcpy( &pmt->subtype, &MEDIASUBTYPE_NULL, sizeof(GUID) );
463 pmt->bFixedSizeSamples = 0;
464 pmt->bTemporalCompression = 0;
465 pmt->lSampleSize = 1;
466 memcpy( &pmt->formattype, &FORMAT_None, sizeof(GUID) );
469 pmt->pbFormat = NULL;
474 static HRESULT CAVIParseImpl_CheckStreamType( CParserImpl* pImpl, ULONG nStreamIndex, const AM_MEDIA_TYPE* pmt )
476 CAVIParseImpl* This = (CAVIParseImpl*)pImpl->m_pUserData;
479 VIDEOINFOHEADER* pvi;
480 VIDEOINFOHEADER* pviCheck;
482 WAVEFORMATEX* pwfxCheck;
484 TRACE("(%p,%lu,%p)\n",This,nStreamIndex,pmt);
486 hr = CAVIParseImpl_GetStreamType( pImpl, nStreamIndex, &mt );
490 TRACE("check GUIDs - %s,%s\n",debugstr_guid(&pmt->majortype),debugstr_guid(&pmt->subtype));
491 if ( !IsEqualGUID( &pmt->majortype, &mt.majortype ) ||
492 !IsEqualGUID( &pmt->subtype, &mt.subtype ) ||
493 !IsEqualGUID( &pmt->formattype, &mt.formattype ) )
499 TRACE("check format\n");
501 switch ( This->pStreamsBuf[nStreamIndex].strh.fccType )
504 TRACE("check vids\n");
505 pvi = (VIDEOINFOHEADER*)mt.pbFormat;
506 pviCheck = (VIDEOINFOHEADER*)pmt->pbFormat;
507 if ( pvi == NULL || pviCheck == NULL || pmt->cbFormat < sizeof(VIDEOINFOHEADER) )
509 if ( pvi->bmiHeader.biWidth != pviCheck->bmiHeader.biWidth ||
510 pvi->bmiHeader.biHeight != pviCheck->bmiHeader.biHeight ||
511 pvi->bmiHeader.biPlanes != pviCheck->bmiHeader.biPlanes ||
512 pvi->bmiHeader.biBitCount != pviCheck->bmiHeader.biBitCount ||
513 pvi->bmiHeader.biCompression != pviCheck->bmiHeader.biCompression ||
514 pvi->bmiHeader.biClrUsed != pviCheck->bmiHeader.biClrUsed )
518 TRACE("check auds\n");
519 pwfx = (WAVEFORMATEX*)mt.pbFormat;
520 pwfxCheck = (WAVEFORMATEX*)pmt->pbFormat;
521 if ( pwfx == NULL || pwfxCheck == NULL || pmt->cbFormat < (sizeof(WAVEFORMATEX)-2) )
523 if ( pwfx->wFormatTag != pwfxCheck->wFormatTag ||
524 pwfx->nBlockAlign != pwfxCheck->nBlockAlign ||
525 pwfx->wBitsPerSample != pwfxCheck->wBitsPerSample ||
526 pwfx->nChannels != pwfxCheck->nChannels ||
527 pwfx->nSamplesPerSec != pwfxCheck->nSamplesPerSec )
537 QUARTZ_MediaType_Free( &mt );
544 static HRESULT CAVIParseImpl_GetAllocProp( CParserImpl* pImpl, ALLOCATOR_PROPERTIES* pReqProp )
546 CAVIParseImpl* This = (CAVIParseImpl*)pImpl->m_pUserData;
551 ZeroMemory( pReqProp, sizeof(ALLOCATOR_PROPERTIES) );
552 pReqProp->cBuffers = This->avih.dwStreams;
553 pReqProp->cbBuffer = This->avih.dwSuggestedBufferSize;
558 static HRESULT CAVIParseImpl_GetNextRequest( CParserImpl* pImpl, ULONG* pnStreamIndex, LONGLONG* pllStart, LONG* plLength, REFERENCE_TIME* prtStart, REFERENCE_TIME* prtStop )
560 CAVIParseImpl* This = (CAVIParseImpl*)pImpl->m_pUserData;
561 REFERENCE_TIME rtNext;
564 CAVIParseStream* pStream;
565 const WAVEFORMATEX* pwfx;
570 TRACE("(%p)\n",This);
572 nIndexNext = This->avih.dwStreams;
573 rtNext = ((REFERENCE_TIME)0x7fffffff<<32)|((REFERENCE_TIME)0xffffffff);
574 for ( nIndex = 0; nIndex < This->avih.dwStreams; nIndex++ )
576 TRACE("stream %lu - %lu,%lu\n",nIndex,(unsigned long)(This->pStreamsBuf[nIndex].rtCur*1000/QUARTZ_TIMEUNITS),This->pStreamsBuf[nIndex].cIndexCur);
577 if ( rtNext > This->pStreamsBuf[nIndex].rtCur &&
578 This->pStreamsBuf[nIndex].cIndexCur < This->pStreamsBuf[nIndex].cIndexEntries )
581 rtNext = This->pStreamsBuf[nIndex].rtCur;
584 if ( nIndexNext >= This->avih.dwStreams )
587 if ( This->pIndexEntriesBuf != NULL )
589 pStream = &This->pStreamsBuf[nIndexNext];
590 *pnStreamIndex = nIndexNext;
591 *pllStart = (LONGLONG)pStream->pIndexEntries[pStream->cIndexCur].dwChunkOffset + 8;
592 *plLength = (LONG)pStream->pIndexEntries[pStream->cIndexCur].dwChunkLength;
596 switch ( pStream->strh.fccType )
600 pStream->rtInternal ++;
601 rtNext = pStream->rtInternal * (REFERENCE_TIME)QUARTZ_TIMEUNITS * (REFERENCE_TIME)pStream->strh.dwScale / (REFERENCE_TIME)pStream->strh.dwRate;
602 /* FIXME - handle AVIPALCHANGE */
606 pwfx = (const WAVEFORMATEX*)pStream->pFmtBuf;
607 if ( pwfx != NULL && pStream->cbFmt >= (sizeof(WAVEFORMATEX)-2) )
609 pStream->rtInternal += (REFERENCE_TIME)*plLength;
610 rtNext = pStream->rtInternal * (REFERENCE_TIME)QUARTZ_TIMEUNITS / (REFERENCE_TIME)pwfx->nAvgBytesPerSec;
614 pStream->rtInternal += (REFERENCE_TIME)(*plLength);
615 rtNext = pStream->rtInternal * (REFERENCE_TIME)QUARTZ_TIMEUNITS * (REFERENCE_TIME)pStream->strh.dwScale / ((REFERENCE_TIME)pStream->strh.dwSampleSize * (REFERENCE_TIME)pStream->strh.dwRate);
621 pStream->rtInternal += (REFERENCE_TIME)(*plLength);
622 rtNext = pStream->rtInternal * (REFERENCE_TIME)QUARTZ_TIMEUNITS * (REFERENCE_TIME)pStream->strh.dwScale / ((REFERENCE_TIME)pStream->strh.dwSampleSize * (REFERENCE_TIME)pStream->strh.dwRate);
625 pStream->cIndexCur ++;
626 pStream->rtCur = rtNext;
635 TRACE("return %lu / %ld-%ld / %lu-%lu\n",
636 *pnStreamIndex,(long)*pllStart,*plLength,
637 (unsigned long)((*prtStart)*1000/QUARTZ_TIMEUNITS),
638 (unsigned long)((*prtStop)*1000/QUARTZ_TIMEUNITS));
643 static HRESULT CAVIParseImpl_ProcessSample( CParserImpl* pImpl, ULONG nStreamIndex, LONGLONG llStart, LONG lLength, IMediaSample* pSample )
645 CAVIParseImpl* This = (CAVIParseImpl*)pImpl->m_pUserData;
647 TRACE("(%p,%lu,%ld,%ld,%p)\n",This,nStreamIndex,(long)llStart,lLength,pSample);
658 static const struct ParserHandlers CAVIParseImpl_Handlers =
660 CAVIParseImpl_InitParser,
661 CAVIParseImpl_UninitParser,
662 CAVIParseImpl_GetOutPinName,
663 CAVIParseImpl_GetStreamType,
664 CAVIParseImpl_CheckStreamType,
665 CAVIParseImpl_GetAllocProp,
666 CAVIParseImpl_GetNextRequest,
667 CAVIParseImpl_ProcessSample,
669 /* for IQualityControl */
670 NULL, /* pQualityNotify */
673 NULL, /* pGetSeekingCaps */
674 NULL, /* pIsTimeFormatSupported */
675 NULL, /* pGetCurPos */
676 NULL, /* pSetCurPos */
677 NULL, /* pGetDuration */
678 NULL, /* pSetDuration */
679 NULL, /* pGetStopPos */
680 NULL, /* pSetStopPos */
681 NULL, /* pGetPreroll */
682 NULL, /* pSetPreroll */
685 HRESULT QUARTZ_CreateAVISplitter(IUnknown* punkOuter,void** ppobj)
687 return QUARTZ_CreateParser(
690 QUARTZ_AVIParser_Name,
691 QUARTZ_AVIParserInPin_Name,
692 &CAVIParseImpl_Handlers );