2 * Parser (Base for parsers and splitters)
4 * Copyright 2003 Robert Shearman
5 * Copyright 2004-2005 Christian Costa
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "quartz_private.h"
23 #include "control_private.h"
34 #include "wine/unicode.h"
35 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
44 static const WCHAR wcsInputPinName[] = {'i','n','p','u','t',' ','p','i','n',0};
45 static const IBaseFilterVtbl Parser_Vtbl;
46 static const IMediaSeekingVtbl Parser_Seeking_Vtbl;
47 static const IPinVtbl Parser_OutputPin_Vtbl;
48 static const IPinVtbl Parser_InputPin_Vtbl;
50 static HRESULT Parser_OutputPin_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt);
51 static HRESULT Parser_ChangeStart(LPVOID iface);
52 static HRESULT Parser_ChangeStop(LPVOID iface);
53 static HRESULT Parser_ChangeRate(LPVOID iface);
55 static HRESULT Parser_InputPin_Construct(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin);
57 static inline Parser_OutputPin *impl_from_IMediaSeeking( IMediaSeeking *iface )
59 return (Parser_OutputPin *)((char*)iface - FIELD_OFFSET(Parser_OutputPin, mediaSeeking.lpVtbl));
63 HRESULT Parser_Create(ParserImpl* pParser, const CLSID* pClsid, PFN_PROCESS_SAMPLE fnProcessSample, PFN_QUERY_ACCEPT fnQueryAccept, PFN_PRE_CONNECT fnPreConnect)
68 /* pTransformFilter is already allocated */
69 pParser->clsid = *pClsid;
71 pParser->lpVtbl = &Parser_Vtbl;
72 pParser->refCount = 1;
73 InitializeCriticalSection(&pParser->csFilter);
74 pParser->state = State_Stopped;
75 pParser->pClock = NULL;
76 ZeroMemory(&pParser->filterInfo, sizeof(FILTER_INFO));
78 pParser->cStreams = 0;
79 pParser->ppPins = CoTaskMemAlloc(1 * sizeof(IPin *));
81 /* construct input pin */
82 piInput.dir = PINDIR_INPUT;
83 piInput.pFilter = (IBaseFilter *)pParser;
84 lstrcpynW(piInput.achName, wcsInputPinName, sizeof(piInput.achName) / sizeof(piInput.achName[0]));
86 hr = Parser_InputPin_Construct(&piInput, fnProcessSample, (LPVOID)pParser, fnQueryAccept, &pParser->csFilter, (IPin **)&pParser->pInputPin);
90 pParser->ppPins[0] = (IPin *)pParser->pInputPin;
91 pParser->pInputPin->fnPreConnect = fnPreConnect;
95 CoTaskMemFree(pParser->ppPins);
96 DeleteCriticalSection(&pParser->csFilter);
97 CoTaskMemFree(pParser);
103 static HRESULT Parser_OutputPin_Init(const PIN_INFO * pPinInfo, ALLOCATOR_PROPERTIES * props, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, const AM_MEDIA_TYPE * pmt, float fSamplesPerSec, LPCRITICAL_SECTION pCritSec, Parser_OutputPin * pPinImpl)
105 pPinImpl->pmt = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
106 CopyMediaType(pPinImpl->pmt, pmt);
107 pPinImpl->dwSamplesProcessed = 0;
108 pPinImpl->dwSampleSize = 0;
109 pPinImpl->fSamplesPerSec = fSamplesPerSec;
111 MediaSeekingImpl_Init((LPVOID)pPinInfo->pFilter, Parser_ChangeStop, Parser_ChangeStart, Parser_ChangeRate, &pPinImpl->mediaSeeking);
112 pPinImpl->mediaSeeking.lpVtbl = &Parser_Seeking_Vtbl;
114 return OutputPin_Init(pPinInfo, props, pUserData, pQueryAccept, pCritSec, &pPinImpl->pin);
117 static HRESULT Parser_OutputPin_Construct(const PIN_INFO * pPinInfo, ALLOCATOR_PROPERTIES * props, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, const AM_MEDIA_TYPE * pmt, float fSamplesPerSec, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
119 Parser_OutputPin * pPinImpl;
123 assert(pPinInfo->dir == PINDIR_OUTPUT);
125 pPinImpl = CoTaskMemAlloc(sizeof(Parser_OutputPin));
128 return E_OUTOFMEMORY;
130 if (SUCCEEDED(Parser_OutputPin_Init(pPinInfo, props, pUserData, pQueryAccept, pmt, fSamplesPerSec, pCritSec, pPinImpl)))
132 pPinImpl->pin.pin.lpVtbl = &Parser_OutputPin_Vtbl;
134 *ppPin = (IPin *)pPinImpl;
140 static HRESULT WINAPI Parser_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
142 ParserImpl *This = (ParserImpl *)iface;
143 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
147 if (IsEqualIID(riid, &IID_IUnknown))
149 else if (IsEqualIID(riid, &IID_IPersist))
151 else if (IsEqualIID(riid, &IID_IMediaFilter))
153 else if (IsEqualIID(riid, &IID_IBaseFilter))
158 IUnknown_AddRef((IUnknown *)(*ppv));
162 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
164 return E_NOINTERFACE;
167 static ULONG WINAPI Parser_AddRef(IBaseFilter * iface)
169 ParserImpl *This = (ParserImpl *)iface;
170 ULONG refCount = InterlockedIncrement(&This->refCount);
172 TRACE("(%p/%p)->() AddRef from %ld\n", This, iface, refCount - 1);
177 static ULONG WINAPI Parser_Release(IBaseFilter * iface)
179 ParserImpl *This = (ParserImpl *)iface;
180 ULONG refCount = InterlockedDecrement(&This->refCount);
182 TRACE("(%p/%p)->() Release from %ld\n", This, iface, refCount + 1);
188 DeleteCriticalSection(&This->csFilter);
190 IReferenceClock_Release(This->pClock);
192 for (i = 0; i < This->cStreams + 1; i++)
193 IPin_Release(This->ppPins[i]);
195 HeapFree(GetProcessHeap(), 0, This->ppPins);
198 TRACE("Destroying AVI splitter\n");
207 /** IPersist methods **/
209 static HRESULT WINAPI Parser_GetClassID(IBaseFilter * iface, CLSID * pClsid)
211 TRACE("(%p)\n", pClsid);
213 *pClsid = CLSID_AviSplitter;
218 /** IMediaFilter methods **/
220 static HRESULT WINAPI Parser_Stop(IBaseFilter * iface)
223 ParserImpl *This = (ParserImpl *)iface;
227 EnterCriticalSection(&This->csFilter);
229 if (This->state == State_Stopped)
231 LeaveCriticalSection(&This->csFilter);
234 hr = PullPin_StopProcessing(This->pInputPin);
235 This->state = State_Stopped;
237 LeaveCriticalSection(&This->csFilter);
242 static HRESULT WINAPI Parser_Pause(IBaseFilter * iface)
246 ParserImpl *This = (ParserImpl *)iface;
250 EnterCriticalSection(&This->csFilter);
252 if (This->state == State_Paused)
254 LeaveCriticalSection(&This->csFilter);
257 bInit = (This->state == State_Stopped);
258 This->state = State_Paused;
260 LeaveCriticalSection(&This->csFilter);
266 hr = PullPin_Seek(This->pInputPin, 0, ((LONGLONG)0x7fffffff << 32) | 0xffffffff);
269 hr = PullPin_InitProcessing(This->pInputPin);
273 for (i = 1; i < This->cStreams + 1; i++)
275 Parser_OutputPin* StreamPin = (Parser_OutputPin *)This->ppPins[i];
276 OutputPin_DeliverNewSegment((OutputPin *)This->ppPins[i], 0, (LONGLONG)ceil(10000000.0 * (float)StreamPin->dwLength / StreamPin->fSamplesPerSec), 1.0);
277 StreamPin->mediaSeeking.llDuration = (LONGLONG)ceil(10000000.0 * (float)StreamPin->dwLength / StreamPin->fSamplesPerSec);
278 StreamPin->mediaSeeking.llStop = (LONGLONG)ceil(10000000.0 * (float)StreamPin->dwLength / StreamPin->fSamplesPerSec);
279 OutputPin_CommitAllocator((OutputPin *)This->ppPins[i]);
282 /* FIXME: this is a little hacky: we have to deliver (at least?) one sample
283 * to each renderer before they will complete their transitions. We should probably
284 * seek through the stream for the first of each, rather than do it this way which is
285 * probably a bit prone to deadlocking */
286 hr = PullPin_StartProcessing(This->pInputPin);
289 /* FIXME: else pause thread */
292 hr = PullPin_PauseProcessing(This->pInputPin);
297 static HRESULT WINAPI Parser_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
300 ParserImpl *This = (ParserImpl *)iface;
303 TRACE("(%s)\n", wine_dbgstr_longlong(tStart));
305 EnterCriticalSection(&This->csFilter);
307 if (This->state == State_Running)
309 LeaveCriticalSection(&This->csFilter);
313 This->rtStreamStart = tStart;
315 hr = PullPin_Seek(This->pInputPin, tStart, ((LONGLONG)0x7fffffff << 32) | 0xffffffff);
317 if (SUCCEEDED(hr) && (This->state == State_Stopped))
319 hr = PullPin_InitProcessing(This->pInputPin);
323 for (i = 1; i < (This->cStreams + 1); i++)
325 OutputPin_CommitAllocator((OutputPin *)This->ppPins[i]);
331 hr = PullPin_StartProcessing(This->pInputPin);
334 This->state = State_Running;
336 LeaveCriticalSection(&This->csFilter);
341 static HRESULT WINAPI Parser_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
343 ParserImpl *This = (ParserImpl *)iface;
345 TRACE("(%ld, %p)\n", dwMilliSecsTimeout, pState);
347 EnterCriticalSection(&This->csFilter);
349 *pState = This->state;
351 LeaveCriticalSection(&This->csFilter);
353 /* FIXME: this is a little bit unsafe, but I don't see that we can do this
354 * while in the critical section. Maybe we could copy the pointer and addref in the
355 * critical section and then release after this.
357 if (This->pInputPin && (PullPin_WaitForStateChange(This->pInputPin, dwMilliSecsTimeout) == S_FALSE))
358 return VFW_S_STATE_INTERMEDIATE;
363 static HRESULT WINAPI Parser_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
365 ParserImpl *This = (ParserImpl *)iface;
367 TRACE("(%p)\n", pClock);
369 EnterCriticalSection(&This->csFilter);
372 IReferenceClock_Release(This->pClock);
373 This->pClock = pClock;
375 IReferenceClock_AddRef(This->pClock);
377 LeaveCriticalSection(&This->csFilter);
382 static HRESULT WINAPI Parser_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
384 ParserImpl *This = (ParserImpl *)iface;
386 TRACE("(%p)\n", ppClock);
388 EnterCriticalSection(&This->csFilter);
390 *ppClock = This->pClock;
392 IReferenceClock_AddRef(This->pClock);
394 LeaveCriticalSection(&This->csFilter);
399 /** IBaseFilter implementation **/
401 static HRESULT WINAPI Parser_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
404 ParserImpl *This = (ParserImpl *)iface;
406 TRACE("(%p)\n", ppEnum);
408 epd.cPins = This->cStreams + 1; /* +1 for input pin */
409 epd.ppPins = This->ppPins;
410 return IEnumPinsImpl_Construct(&epd, ppEnum);
413 static HRESULT WINAPI Parser_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
415 FIXME("(%p)->(%s,%p)\n", iface, debugstr_w(Id), ppPin);
417 /* FIXME: critical section */
422 static HRESULT WINAPI Parser_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
424 ParserImpl *This = (ParserImpl *)iface;
426 TRACE("(%p)\n", pInfo);
428 strcpyW(pInfo->achName, This->filterInfo.achName);
429 pInfo->pGraph = This->filterInfo.pGraph;
432 IFilterGraph_AddRef(pInfo->pGraph);
437 static HRESULT WINAPI Parser_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
440 ParserImpl *This = (ParserImpl *)iface;
442 TRACE("(%p, %s)\n", pGraph, debugstr_w(pName));
444 EnterCriticalSection(&This->csFilter);
447 strcpyW(This->filterInfo.achName, pName);
449 *This->filterInfo.achName = '\0';
450 This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
452 LeaveCriticalSection(&This->csFilter);
457 static HRESULT WINAPI Parser_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
459 TRACE("(%p)\n", pVendorInfo);
463 static const IBaseFilterVtbl Parser_Vtbl =
465 Parser_QueryInterface,
473 Parser_SetSyncSource,
474 Parser_GetSyncSource,
477 Parser_QueryFilterInfo,
478 Parser_JoinFilterGraph,
479 Parser_QueryVendorInfo
482 HRESULT Parser_AddPin(ParserImpl * This, PIN_INFO * piOutput, ALLOCATOR_PROPERTIES * props, AM_MEDIA_TYPE * amt, float fSamplesPerSec, DWORD dwSampleSize, DWORD dwLength)
487 ppOldPins = This->ppPins;
489 This->ppPins = HeapAlloc(GetProcessHeap(), 0, (This->cStreams + 2) * sizeof(IPin *));
490 memcpy(This->ppPins, ppOldPins, (This->cStreams + 1) * sizeof(IPin *));
492 hr = Parser_OutputPin_Construct(piOutput, props, NULL, Parser_OutputPin_QueryAccept, amt, fSamplesPerSec, &This->csFilter, This->ppPins + This->cStreams + 1);
496 ((Parser_OutputPin *)(This->ppPins[This->cStreams + 1]))->dwSampleSize = dwSampleSize;
497 ((Parser_OutputPin *)(This->ppPins[This->cStreams + 1]))->dwLength = dwLength;
498 ((Parser_OutputPin *)(This->ppPins[This->cStreams + 1]))->pin.pin.pUserData = (LPVOID)This->ppPins[This->cStreams + 1];
500 HeapFree(GetProcessHeap(), 0, ppOldPins);
504 HeapFree(GetProcessHeap(), 0, This->ppPins);
505 This->ppPins = ppOldPins;
506 ERR("Failed with error %lx\n", hr);
512 static HRESULT Parser_RemoveOutputPins(ParserImpl * This)
514 /* NOTE: should be in critical section when calling this function */
517 IPin ** ppOldPins = This->ppPins;
519 /* reduce the pin array down to 1 (just our input pin) */
520 This->ppPins = HeapAlloc(GetProcessHeap(), 0, sizeof(IPin *) * 1);
521 memcpy(This->ppPins, ppOldPins, sizeof(IPin *) * 1);
523 for (i = 0; i < This->cStreams; i++)
525 OutputPin_DeliverDisconnect((OutputPin *)ppOldPins[i + 1]);
526 IPin_Release(ppOldPins[i + 1]);
530 HeapFree(GetProcessHeap(), 0, ppOldPins);
535 static HRESULT Parser_ChangeStart(LPVOID iface)
537 FIXME("(%p)\n", iface);
541 static HRESULT Parser_ChangeStop(LPVOID iface)
543 FIXME("(%p)\n", iface);
547 static HRESULT Parser_ChangeRate(LPVOID iface)
549 FIXME("(%p)\n", iface);
554 static HRESULT WINAPI Parser_Seeking_QueryInterface(IMediaSeeking * iface, REFIID riid, LPVOID * ppv)
556 Parser_OutputPin *This = impl_from_IMediaSeeking(iface);
558 return IUnknown_QueryInterface((IUnknown *)This, riid, ppv);
561 static ULONG WINAPI Parser_Seeking_AddRef(IMediaSeeking * iface)
563 Parser_OutputPin *This = impl_from_IMediaSeeking(iface);
565 return IUnknown_AddRef((IUnknown *)This);
568 static ULONG WINAPI Parser_Seeking_Release(IMediaSeeking * iface)
570 Parser_OutputPin *This = impl_from_IMediaSeeking(iface);
572 return IUnknown_Release((IUnknown *)This);
575 static const IMediaSeekingVtbl Parser_Seeking_Vtbl =
577 Parser_Seeking_QueryInterface,
578 Parser_Seeking_AddRef,
579 Parser_Seeking_Release,
580 MediaSeekingImpl_GetCapabilities,
581 MediaSeekingImpl_CheckCapabilities,
582 MediaSeekingImpl_IsFormatSupported,
583 MediaSeekingImpl_QueryPreferredFormat,
584 MediaSeekingImpl_GetTimeFormat,
585 MediaSeekingImpl_IsUsingTimeFormat,
586 MediaSeekingImpl_SetTimeFormat,
587 MediaSeekingImpl_GetDuration,
588 MediaSeekingImpl_GetStopPosition,
589 MediaSeekingImpl_GetCurrentPosition,
590 MediaSeekingImpl_ConvertTimeFormat,
591 MediaSeekingImpl_SetPositions,
592 MediaSeekingImpl_GetPositions,
593 MediaSeekingImpl_GetAvailable,
594 MediaSeekingImpl_SetRate,
595 MediaSeekingImpl_GetRate,
596 MediaSeekingImpl_GetPreroll
599 HRESULT WINAPI Parser_OutputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
601 Parser_OutputPin *This = (Parser_OutputPin *)iface;
603 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
607 if (IsEqualIID(riid, &IID_IUnknown))
608 *ppv = (LPVOID)iface;
609 else if (IsEqualIID(riid, &IID_IPin))
610 *ppv = (LPVOID)iface;
611 else if (IsEqualIID(riid, &IID_IMediaSeeking))
612 *ppv = (LPVOID)&This->mediaSeeking;
616 IUnknown_AddRef((IUnknown *)(*ppv));
620 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
622 return E_NOINTERFACE;
625 static ULONG WINAPI Parser_OutputPin_Release(IPin * iface)
627 Parser_OutputPin *This = (Parser_OutputPin *)iface;
628 ULONG refCount = InterlockedDecrement(&This->pin.pin.refCount);
630 TRACE("(%p)->() Release from %ld\n", iface, refCount + 1);
634 FreeMediaType(This->pmt);
635 CoTaskMemFree(This->pmt);
636 FreeMediaType(&This->pin.pin.mtCurrent);
643 static HRESULT WINAPI Parser_OutputPin_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
645 ENUMMEDIADETAILS emd;
646 Parser_OutputPin *This = (Parser_OutputPin *)iface;
648 TRACE("(%p)\n", ppEnum);
650 /* override this method to allow enumeration of your types */
652 emd.pMediaTypes = This->pmt;
654 return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
657 static HRESULT Parser_OutputPin_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
659 Parser_OutputPin *This = (Parser_OutputPin *)iface;
662 dump_AM_MEDIA_TYPE(pmt);
664 return (memcmp(This->pmt, pmt, sizeof(AM_MEDIA_TYPE)) == 0);
667 static const IPinVtbl Parser_OutputPin_Vtbl =
669 Parser_OutputPin_QueryInterface,
671 Parser_OutputPin_Release,
673 OutputPin_ReceiveConnection,
674 OutputPin_Disconnect,
675 IPinImpl_ConnectedTo,
676 IPinImpl_ConnectionMediaType,
677 IPinImpl_QueryPinInfo,
678 IPinImpl_QueryDirection,
680 IPinImpl_QueryAccept,
681 Parser_OutputPin_EnumMediaTypes,
682 IPinImpl_QueryInternalConnections,
683 OutputPin_EndOfStream,
684 OutputPin_BeginFlush,
689 static HRESULT Parser_InputPin_Construct(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
695 if (pPinInfo->dir != PINDIR_INPUT)
697 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
701 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
704 return E_OUTOFMEMORY;
706 if (SUCCEEDED(PullPin_Init(pPinInfo, pSampleProc, pUserData, pQueryAccept, pCritSec, pPinImpl)))
708 pPinImpl->pin.lpVtbl = &Parser_InputPin_Vtbl;
710 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
716 static HRESULT WINAPI Parser_InputPin_Disconnect(IPin * iface)
719 IPinImpl *This = (IPinImpl *)iface;
723 EnterCriticalSection(This->pCritSec);
725 if (This->pConnectedTo)
729 hr = IBaseFilter_GetState(This->pinInfo.pFilter, 0, &state);
731 if (SUCCEEDED(hr) && (state == State_Stopped))
733 IPin_Release(This->pConnectedTo);
734 This->pConnectedTo = NULL;
735 hr = Parser_RemoveOutputPins((ParserImpl *)This->pinInfo.pFilter);
738 hr = VFW_E_NOT_STOPPED;
743 LeaveCriticalSection(This->pCritSec);
748 static const IPinVtbl Parser_InputPin_Vtbl =
750 PullPin_QueryInterface,
754 PullPin_ReceiveConnection,
755 Parser_InputPin_Disconnect,
756 IPinImpl_ConnectedTo,
757 IPinImpl_ConnectionMediaType,
758 IPinImpl_QueryPinInfo,
759 IPinImpl_QueryDirection,
761 IPinImpl_QueryAccept,
762 IPinImpl_EnumMediaTypes,
763 IPinImpl_QueryInternalConnections,