shell32/tests: Use GetProcAddress() on SHGetPathFromIDListW() because it is missing...
[wine] / dlls / quartz / parser.c
1 /*
2  * Parser (Base for parsers and splitters)
3  *
4  * Copyright 2003 Robert Shearman
5  * Copyright 2004-2005 Christian Costa
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "quartz_private.h"
23 #include "control_private.h"
24 #include "pin.h"
25
26 #include "vfwmsgs.h"
27 #include "amvideo.h"
28
29 #include "wine/unicode.h"
30 #include "wine/debug.h"
31
32 #include <math.h>
33 #include <assert.h>
34
35 #include "parser.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
38
39 static const WCHAR wcsInputPinName[] = {'i','n','p','u','t',' ','p','i','n',0};
40 static const IBaseFilterVtbl Parser_Vtbl;
41 static const IMediaSeekingVtbl Parser_Seeking_Vtbl;
42 static const IPinVtbl Parser_OutputPin_Vtbl;
43 static const IPinVtbl Parser_InputPin_Vtbl;
44
45 static HRESULT Parser_OutputPin_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt);
46 static HRESULT Parser_ChangeStart(LPVOID iface);
47 static HRESULT Parser_ChangeStop(LPVOID iface);
48 static HRESULT Parser_ChangeRate(LPVOID iface);
49
50 static HRESULT Parser_InputPin_Construct(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin);
51
52 static inline Parser_OutputPin *impl_from_IMediaSeeking( IMediaSeeking *iface )
53 {
54     return (Parser_OutputPin *)((char*)iface - FIELD_OFFSET(Parser_OutputPin, mediaSeeking.lpVtbl));
55 }
56
57
58 HRESULT Parser_Create(ParserImpl* pParser, const CLSID* pClsid, PFN_PROCESS_SAMPLE fnProcessSample, PFN_QUERY_ACCEPT fnQueryAccept, PFN_PRE_CONNECT fnPreConnect, PFN_CLEANUP fnCleanup)
59 {
60     HRESULT hr;
61     PIN_INFO piInput;
62
63     /* pTransformFilter is already allocated */
64     pParser->clsid = *pClsid;
65
66     pParser->lpVtbl = &Parser_Vtbl;
67     pParser->refCount = 1;
68     InitializeCriticalSection(&pParser->csFilter);
69     pParser->csFilter.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": ParserImpl.csFilter");
70     pParser->state = State_Stopped;
71     pParser->pClock = NULL;
72     pParser->fnCleanup = fnCleanup;
73     ZeroMemory(&pParser->filterInfo, sizeof(FILTER_INFO));
74
75     pParser->cStreams = 0;
76     pParser->ppPins = CoTaskMemAlloc(1 * sizeof(IPin *));
77
78     /* construct input pin */
79     piInput.dir = PINDIR_INPUT;
80     piInput.pFilter = (IBaseFilter *)pParser;
81     lstrcpynW(piInput.achName, wcsInputPinName, sizeof(piInput.achName) / sizeof(piInput.achName[0]));
82
83     hr = Parser_InputPin_Construct(&piInput, fnProcessSample, (LPVOID)pParser, fnQueryAccept, &pParser->csFilter, (IPin **)&pParser->pInputPin);
84
85     if (SUCCEEDED(hr))
86     {
87         pParser->ppPins[0] = (IPin *)pParser->pInputPin;
88         pParser->pInputPin->fnPreConnect = fnPreConnect;
89     }
90     else
91     {
92         CoTaskMemFree(pParser->ppPins);
93         pParser->csFilter.DebugInfo->Spare[0] = 0;
94         DeleteCriticalSection(&pParser->csFilter);
95         CoTaskMemFree(pParser);
96     }
97
98     return hr;
99 }
100
101 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)
102 {
103     pPinImpl->pmt = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
104     CopyMediaType(pPinImpl->pmt, pmt);
105     pPinImpl->dwSamplesProcessed = 0;
106     pPinImpl->dwSampleSize = 0;
107     pPinImpl->fSamplesPerSec = fSamplesPerSec;
108
109     MediaSeekingImpl_Init((LPVOID)pPinInfo->pFilter, Parser_ChangeStop, Parser_ChangeStart, Parser_ChangeRate, &pPinImpl->mediaSeeking);
110     pPinImpl->mediaSeeking.lpVtbl = &Parser_Seeking_Vtbl;
111
112     return OutputPin_Init(pPinInfo, props, pUserData, pQueryAccept, pCritSec, &pPinImpl->pin);
113 }
114
115 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)
116 {
117     Parser_OutputPin * pPinImpl;
118
119     *ppPin = NULL;
120
121     assert(pPinInfo->dir == PINDIR_OUTPUT);
122
123     pPinImpl = CoTaskMemAlloc(sizeof(Parser_OutputPin));
124
125     if (!pPinImpl)
126         return E_OUTOFMEMORY;
127
128     if (SUCCEEDED(Parser_OutputPin_Init(pPinInfo, props, pUserData, pQueryAccept, pmt, fSamplesPerSec, pCritSec, pPinImpl)))
129     {
130         pPinImpl->pin.pin.lpVtbl = &Parser_OutputPin_Vtbl;
131
132         *ppPin = (IPin *)pPinImpl;
133         return S_OK;
134     }
135
136     CoTaskMemFree(pPinImpl);
137     return E_FAIL;
138 }
139
140 static HRESULT WINAPI Parser_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
141 {
142     ParserImpl *This = (ParserImpl *)iface;
143     TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
144
145     *ppv = NULL;
146
147     if (IsEqualIID(riid, &IID_IUnknown))
148         *ppv = (LPVOID)This;
149     else if (IsEqualIID(riid, &IID_IPersist))
150         *ppv = (LPVOID)This;
151     else if (IsEqualIID(riid, &IID_IMediaFilter))
152         *ppv = (LPVOID)This;
153     else if (IsEqualIID(riid, &IID_IBaseFilter))
154         *ppv = (LPVOID)This;
155
156     if (*ppv)
157     {
158         IUnknown_AddRef((IUnknown *)(*ppv));
159         return S_OK;
160     }
161
162     FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
163
164     return E_NOINTERFACE;
165 }
166
167 static ULONG WINAPI Parser_AddRef(IBaseFilter * iface)
168 {
169     ParserImpl *This = (ParserImpl *)iface;
170     ULONG refCount = InterlockedIncrement(&This->refCount);
171
172     TRACE("(%p/%p)->() AddRef from %d\n", This, iface, refCount - 1);
173
174     return refCount;
175 }
176
177 static ULONG WINAPI Parser_Release(IBaseFilter * iface)
178 {
179     ParserImpl *This = (ParserImpl *)iface;
180     ULONG refCount = InterlockedDecrement(&This->refCount);
181
182     TRACE("(%p)->() Release from %d\n", This, refCount + 1);
183     
184     if (!refCount)
185     {
186         ULONG i;
187
188         if (This->fnCleanup)
189             This->fnCleanup(This);
190
191         if (This->pClock)
192             IReferenceClock_Release(This->pClock);
193         
194         for (i = 0; i < This->cStreams + 1; i++)
195         {
196             IPin *pConnectedTo;
197
198             if (SUCCEEDED(IPin_ConnectedTo(This->ppPins[i], &pConnectedTo)))
199             {
200                 IPin_Disconnect(pConnectedTo);
201                 IPin_Release(pConnectedTo);
202             }
203             IPin_Disconnect(This->ppPins[i]);
204
205             IPin_Release(This->ppPins[i]);
206         }
207         
208         CoTaskMemFree(This->ppPins);
209         This->lpVtbl = NULL;
210
211         This->csFilter.DebugInfo->Spare[0] = 0;
212         DeleteCriticalSection(&This->csFilter);
213         
214         TRACE("Destroying parser\n");
215         CoTaskMemFree(This);
216         
217         return 0;
218     }
219     else
220         return refCount;
221 }
222
223 /** IPersist methods **/
224
225 static HRESULT WINAPI Parser_GetClassID(IBaseFilter * iface, CLSID * pClsid)
226 {
227     ParserImpl *This = (ParserImpl *)iface;
228
229     TRACE("(%p)\n", pClsid);
230
231     *pClsid = This->clsid;
232
233     return S_OK;
234 }
235
236 /** IMediaFilter methods **/
237
238 static HRESULT WINAPI Parser_Stop(IBaseFilter * iface)
239 {
240     HRESULT hr;
241     ParserImpl *This = (ParserImpl *)iface;
242
243     TRACE("()\n");
244
245     EnterCriticalSection(&This->csFilter);
246     {
247         if (This->state == State_Stopped)
248         {
249             LeaveCriticalSection(&This->csFilter);
250             return S_OK;
251         }
252         hr = PullPin_StopProcessing(This->pInputPin);
253         This->state = State_Stopped;
254     }
255     LeaveCriticalSection(&This->csFilter);
256     
257     return hr;
258 }
259
260 static HRESULT WINAPI Parser_Pause(IBaseFilter * iface)
261 {
262     HRESULT hr = S_OK;
263     BOOL bInit;
264     ParserImpl *This = (ParserImpl *)iface;
265     
266     TRACE("()\n");
267
268     EnterCriticalSection(&This->csFilter);
269     {
270         if (This->state == State_Paused)
271         {
272             LeaveCriticalSection(&This->csFilter);
273             return S_OK;
274         }
275         bInit = (This->state == State_Stopped);
276         This->state = State_Paused;
277     }
278     LeaveCriticalSection(&This->csFilter);
279
280     if (bInit)
281     {
282         unsigned int i;
283
284         hr = PullPin_Seek(This->pInputPin, 0, ((LONGLONG)0x7fffffff << 32) | 0xffffffff);
285
286         if (SUCCEEDED(hr))
287             hr = PullPin_InitProcessing(This->pInputPin);
288
289         if (SUCCEEDED(hr))
290         {
291             for (i = 1; i < This->cStreams + 1; i++)
292             {
293                 Parser_OutputPin* StreamPin = (Parser_OutputPin *)This->ppPins[i];
294                 OutputPin_DeliverNewSegment((OutputPin *)This->ppPins[i], 0, (LONGLONG)ceil(10000000.0 * (float)StreamPin->dwLength / StreamPin->fSamplesPerSec), 1.0);
295                 StreamPin->mediaSeeking.llDuration = (LONGLONG)ceil(10000000.0 * (float)StreamPin->dwLength / StreamPin->fSamplesPerSec);
296                 StreamPin->mediaSeeking.llStop = (LONGLONG)ceil(10000000.0 * (float)StreamPin->dwLength / StreamPin->fSamplesPerSec);
297                 OutputPin_CommitAllocator((OutputPin *)This->ppPins[i]);
298             }
299
300             /* FIXME: this is a little hacky: we have to deliver (at least?) one sample
301              * to each renderer before they will complete their transitions. We should probably
302              * seek through the stream for the first of each, rather than do it this way which is
303              * probably a bit prone to deadlocking */
304             hr = PullPin_StartProcessing(This->pInputPin);
305         }
306     }
307     /* FIXME: else pause thread */
308
309     if (SUCCEEDED(hr))
310         hr = PullPin_PauseProcessing(This->pInputPin);
311
312     return hr;
313 }
314
315 static HRESULT WINAPI Parser_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
316 {
317     HRESULT hr = S_OK;
318     ParserImpl *This = (ParserImpl *)iface;
319     int i;
320
321     TRACE("(%s)\n", wine_dbgstr_longlong(tStart));
322
323     EnterCriticalSection(&This->csFilter);
324     {
325         if (This->state == State_Running)
326         {
327             LeaveCriticalSection(&This->csFilter);
328             return S_OK;
329         }
330
331         This->rtStreamStart = tStart;
332
333         hr = PullPin_Seek(This->pInputPin, tStart, ((LONGLONG)0x7fffffff << 32) | 0xffffffff);
334
335         if (SUCCEEDED(hr) && (This->state == State_Stopped))
336         {
337             hr = PullPin_InitProcessing(This->pInputPin);
338
339             if (SUCCEEDED(hr))
340             { 
341                 for (i = 1; i < (This->cStreams + 1); i++)
342                 {
343                     OutputPin_CommitAllocator((OutputPin *)This->ppPins[i]);
344                 }
345             }
346         }
347
348         if (SUCCEEDED(hr))
349             hr = PullPin_StartProcessing(This->pInputPin);
350
351         if (SUCCEEDED(hr))
352             This->state = State_Running;
353     }
354     LeaveCriticalSection(&This->csFilter);
355
356     return hr;
357 }
358
359 static HRESULT WINAPI Parser_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
360 {
361     ParserImpl *This = (ParserImpl *)iface;
362
363     TRACE("(%d, %p)\n", dwMilliSecsTimeout, pState);
364
365     EnterCriticalSection(&This->csFilter);
366     {
367         *pState = This->state;
368     }
369     LeaveCriticalSection(&This->csFilter);
370
371     /* FIXME: this is a little bit unsafe, but I don't see that we can do this
372      * while in the critical section. Maybe we could copy the pointer and addref in the
373      * critical section and then release after this.
374      */
375     if (This->pInputPin && (PullPin_WaitForStateChange(This->pInputPin, dwMilliSecsTimeout) == S_FALSE))
376         return VFW_S_STATE_INTERMEDIATE;
377
378     return S_OK;
379 }
380
381 static HRESULT WINAPI Parser_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
382 {
383     ParserImpl *This = (ParserImpl *)iface;
384
385     TRACE("(%p)\n", pClock);
386
387     EnterCriticalSection(&This->csFilter);
388     {
389         if (This->pClock)
390             IReferenceClock_Release(This->pClock);
391         This->pClock = pClock;
392         if (This->pClock)
393             IReferenceClock_AddRef(This->pClock);
394     }
395     LeaveCriticalSection(&This->csFilter);
396
397     return S_OK;
398 }
399
400 static HRESULT WINAPI Parser_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
401 {
402     ParserImpl *This = (ParserImpl *)iface;
403
404     TRACE("(%p)\n", ppClock);
405
406     EnterCriticalSection(&This->csFilter);
407     {
408         *ppClock = This->pClock;
409         if (This->pClock)
410             IReferenceClock_AddRef(This->pClock);
411     }
412     LeaveCriticalSection(&This->csFilter);
413     
414     return S_OK;
415 }
416
417 /** IBaseFilter implementation **/
418
419 static HRESULT WINAPI Parser_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
420 {
421     ENUMPINDETAILS epd;
422     ParserImpl *This = (ParserImpl *)iface;
423
424     TRACE("(%p)\n", ppEnum);
425
426     epd.cPins = This->cStreams + 1; /* +1 for input pin */
427     epd.ppPins = This->ppPins;
428     return IEnumPinsImpl_Construct(&epd, ppEnum);
429 }
430
431 static HRESULT WINAPI Parser_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
432 {
433     FIXME("(%p)->(%s,%p)\n", iface, debugstr_w(Id), ppPin);
434
435     /* FIXME: critical section */
436
437     return E_NOTIMPL;
438 }
439
440 static HRESULT WINAPI Parser_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
441 {
442     ParserImpl *This = (ParserImpl *)iface;
443
444     TRACE("(%p)\n", pInfo);
445
446     strcpyW(pInfo->achName, This->filterInfo.achName);
447     pInfo->pGraph = This->filterInfo.pGraph;
448
449     if (pInfo->pGraph)
450         IFilterGraph_AddRef(pInfo->pGraph);
451     
452     return S_OK;
453 }
454
455 static HRESULT WINAPI Parser_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
456 {
457     HRESULT hr = S_OK;
458     ParserImpl *This = (ParserImpl *)iface;
459
460     TRACE("(%p, %s)\n", pGraph, debugstr_w(pName));
461
462     EnterCriticalSection(&This->csFilter);
463     {
464         if (pName)
465             strcpyW(This->filterInfo.achName, pName);
466         else
467             *This->filterInfo.achName = '\0';
468         This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
469     }
470     LeaveCriticalSection(&This->csFilter);
471
472     return hr;
473 }
474
475 static HRESULT WINAPI Parser_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
476 {
477     TRACE("(%p)\n", pVendorInfo);
478     return E_NOTIMPL;
479 }
480
481 static const IBaseFilterVtbl Parser_Vtbl =
482 {
483     Parser_QueryInterface,
484     Parser_AddRef,
485     Parser_Release,
486     Parser_GetClassID,
487     Parser_Stop,
488     Parser_Pause,
489     Parser_Run,
490     Parser_GetState,
491     Parser_SetSyncSource,
492     Parser_GetSyncSource,
493     Parser_EnumPins,
494     Parser_FindPin,
495     Parser_QueryFilterInfo,
496     Parser_JoinFilterGraph,
497     Parser_QueryVendorInfo
498 };
499
500 HRESULT Parser_AddPin(ParserImpl * This, const PIN_INFO * piOutput, ALLOCATOR_PROPERTIES * props,
501                       const AM_MEDIA_TYPE * amt, float fSamplesPerSec, DWORD dwSampleSize, DWORD dwLength)
502 {
503     IPin ** ppOldPins;
504     HRESULT hr;
505
506     ppOldPins = This->ppPins;
507
508     This->ppPins = CoTaskMemAlloc((This->cStreams + 2) * sizeof(IPin *));
509     memcpy(This->ppPins, ppOldPins, (This->cStreams + 1) * sizeof(IPin *));
510
511     hr = Parser_OutputPin_Construct(piOutput, props, NULL, Parser_OutputPin_QueryAccept, amt, fSamplesPerSec, &This->csFilter, This->ppPins + This->cStreams + 1);
512
513     if (SUCCEEDED(hr))
514     {
515         ((Parser_OutputPin *)(This->ppPins[This->cStreams + 1]))->dwSampleSize = dwSampleSize;
516         ((Parser_OutputPin *)(This->ppPins[This->cStreams + 1]))->dwLength = dwLength;
517         ((Parser_OutputPin *)(This->ppPins[This->cStreams + 1]))->pin.pin.pUserData = (LPVOID)This->ppPins[This->cStreams + 1];
518         This->cStreams++;
519         CoTaskMemFree(ppOldPins);
520     }
521     else
522     {
523         CoTaskMemFree(This->ppPins);
524         This->ppPins = ppOldPins;
525         ERR("Failed with error %x\n", hr);
526     }
527
528     return hr;
529 }
530
531 static HRESULT Parser_RemoveOutputPins(ParserImpl * This)
532 {
533     /* NOTE: should be in critical section when calling this function */
534
535     ULONG i;
536     IPin ** ppOldPins = This->ppPins;
537
538     /* reduce the pin array down to 1 (just our input pin) */
539     This->ppPins = CoTaskMemAlloc(sizeof(IPin *) * 1);
540     memcpy(This->ppPins, ppOldPins, sizeof(IPin *) * 1);
541
542     for (i = 0; i < This->cStreams; i++)
543     {
544         OutputPin_DeliverDisconnect((OutputPin *)ppOldPins[i + 1]);
545         IPin_Release(ppOldPins[i + 1]);
546     }
547
548     This->cStreams = 0;
549     CoTaskMemFree(ppOldPins);
550
551     return S_OK;
552 }
553
554 static HRESULT Parser_ChangeStart(LPVOID iface)
555 {
556     FIXME("(%p)\n", iface);
557     return S_OK;
558 }
559
560 static HRESULT Parser_ChangeStop(LPVOID iface)
561 {
562     FIXME("(%p)\n", iface);
563     return S_OK;
564 }
565
566 static HRESULT Parser_ChangeRate(LPVOID iface)
567 {
568     FIXME("(%p)\n", iface);
569     return S_OK;
570 }
571
572
573 static HRESULT WINAPI Parser_Seeking_QueryInterface(IMediaSeeking * iface, REFIID riid, LPVOID * ppv)
574 {
575     Parser_OutputPin *This = impl_from_IMediaSeeking(iface);
576
577     return IUnknown_QueryInterface((IUnknown *)This, riid, ppv);
578 }
579
580 static ULONG WINAPI Parser_Seeking_AddRef(IMediaSeeking * iface)
581 {
582     Parser_OutputPin *This = impl_from_IMediaSeeking(iface);
583
584     return IUnknown_AddRef((IUnknown *)This);
585 }
586
587 static ULONG WINAPI Parser_Seeking_Release(IMediaSeeking * iface)
588 {
589     Parser_OutputPin *This = impl_from_IMediaSeeking(iface);
590
591     return IUnknown_Release((IUnknown *)This);
592 }
593
594 static const IMediaSeekingVtbl Parser_Seeking_Vtbl =
595 {
596     Parser_Seeking_QueryInterface,
597     Parser_Seeking_AddRef,
598     Parser_Seeking_Release,
599     MediaSeekingImpl_GetCapabilities,
600     MediaSeekingImpl_CheckCapabilities,
601     MediaSeekingImpl_IsFormatSupported,
602     MediaSeekingImpl_QueryPreferredFormat,
603     MediaSeekingImpl_GetTimeFormat,
604     MediaSeekingImpl_IsUsingTimeFormat,
605     MediaSeekingImpl_SetTimeFormat,
606     MediaSeekingImpl_GetDuration,
607     MediaSeekingImpl_GetStopPosition,
608     MediaSeekingImpl_GetCurrentPosition,
609     MediaSeekingImpl_ConvertTimeFormat,
610     MediaSeekingImpl_SetPositions,
611     MediaSeekingImpl_GetPositions,
612     MediaSeekingImpl_GetAvailable,
613     MediaSeekingImpl_SetRate,
614     MediaSeekingImpl_GetRate,
615     MediaSeekingImpl_GetPreroll
616 };
617
618 static HRESULT WINAPI Parser_OutputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
619 {
620     Parser_OutputPin *This = (Parser_OutputPin *)iface;
621
622     TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
623
624     *ppv = NULL;
625
626     if (IsEqualIID(riid, &IID_IUnknown))
627         *ppv = (LPVOID)iface;
628     else if (IsEqualIID(riid, &IID_IPin))
629         *ppv = (LPVOID)iface;
630     else if (IsEqualIID(riid, &IID_IMediaSeeking))
631         *ppv = (LPVOID)&This->mediaSeeking;
632
633     if (*ppv)
634     {
635         IUnknown_AddRef((IUnknown *)(*ppv));
636         return S_OK;
637     }
638
639     FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
640
641     return E_NOINTERFACE;
642 }
643
644 static ULONG WINAPI Parser_OutputPin_Release(IPin * iface)
645 {
646     Parser_OutputPin *This = (Parser_OutputPin *)iface;
647     ULONG refCount = InterlockedDecrement(&This->pin.pin.refCount);
648     
649     TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
650     
651     if (!refCount)
652     {
653         FreeMediaType(This->pmt);
654         CoTaskMemFree(This->pmt);
655         FreeMediaType(&This->pin.pin.mtCurrent);
656         CoTaskMemFree(This);
657         return 0;
658     }
659     return refCount;
660 }
661
662 static HRESULT WINAPI Parser_OutputPin_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
663 {
664     ENUMMEDIADETAILS emd;
665     Parser_OutputPin *This = (Parser_OutputPin *)iface;
666
667     TRACE("(%p)\n", ppEnum);
668
669     /* override this method to allow enumeration of your types */
670     emd.cMediaTypes = 1;
671     emd.pMediaTypes = This->pmt;
672
673     return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
674 }
675
676 static HRESULT Parser_OutputPin_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
677 {
678     Parser_OutputPin *This = (Parser_OutputPin *)iface;
679
680     TRACE("()\n");
681     dump_AM_MEDIA_TYPE(pmt);
682
683     return (memcmp(This->pmt, pmt, sizeof(AM_MEDIA_TYPE)) == 0);
684 }
685
686 static const IPinVtbl Parser_OutputPin_Vtbl = 
687 {
688     Parser_OutputPin_QueryInterface,
689     IPinImpl_AddRef,
690     Parser_OutputPin_Release,
691     OutputPin_Connect,
692     OutputPin_ReceiveConnection,
693     OutputPin_Disconnect,
694     IPinImpl_ConnectedTo,
695     IPinImpl_ConnectionMediaType,
696     IPinImpl_QueryPinInfo,
697     IPinImpl_QueryDirection,
698     IPinImpl_QueryId,
699     IPinImpl_QueryAccept,
700     Parser_OutputPin_EnumMediaTypes,
701     IPinImpl_QueryInternalConnections,
702     OutputPin_EndOfStream,
703     OutputPin_BeginFlush,
704     OutputPin_EndFlush,
705     OutputPin_NewSegment
706 };
707
708 static HRESULT Parser_InputPin_Construct(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
709 {
710     PullPin * pPinImpl;
711
712     *ppPin = NULL;
713
714     if (pPinInfo->dir != PINDIR_INPUT)
715     {
716         ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
717         return E_INVALIDARG;
718     }
719
720     pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
721
722     if (!pPinImpl)
723         return E_OUTOFMEMORY;
724
725     if (SUCCEEDED(PullPin_Init(pPinInfo, pSampleProc, pUserData, pQueryAccept, pCritSec, pPinImpl)))
726     {
727         pPinImpl->pin.lpVtbl = &Parser_InputPin_Vtbl;
728
729         *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
730         return S_OK;
731     }
732
733     CoTaskMemFree(pPinImpl);
734     return E_FAIL;
735 }
736
737 static HRESULT WINAPI Parser_InputPin_Disconnect(IPin * iface)
738 {
739     HRESULT hr;
740     IPinImpl *This = (IPinImpl *)iface;
741
742     TRACE("()\n");
743
744     EnterCriticalSection(This->pCritSec);
745     {
746         if (This->pConnectedTo)
747         {
748             FILTER_STATE state;
749
750             hr = IBaseFilter_GetState(This->pinInfo.pFilter, 0, &state);
751
752             if (SUCCEEDED(hr) && (state == State_Stopped))
753             {
754                 IPin_Release(This->pConnectedTo);
755                 This->pConnectedTo = NULL;
756                 hr = Parser_RemoveOutputPins((ParserImpl *)This->pinInfo.pFilter);
757             }
758             else
759                 hr = VFW_E_NOT_STOPPED;
760         }
761         else
762             hr = S_FALSE;
763     }
764     LeaveCriticalSection(This->pCritSec);
765     
766     return hr;
767 }
768
769 HRESULT WINAPI Parser_PullPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
770 {
771     HRESULT hr;
772
773     TRACE("()\n");
774
775     hr = PullPin_ReceiveConnection(iface, pReceivePin, pmt);
776     if (FAILED(hr))
777     {
778         IPinImpl *This = (IPinImpl *)iface;
779
780         EnterCriticalSection(This->pCritSec);
781         Parser_RemoveOutputPins((ParserImpl *)This->pinInfo.pFilter);
782         LeaveCriticalSection(This->pCritSec);
783     }
784
785     return hr;
786 }
787
788 static const IPinVtbl Parser_InputPin_Vtbl =
789 {
790     PullPin_QueryInterface,
791     IPinImpl_AddRef,
792     PullPin_Release,
793     OutputPin_Connect,
794     Parser_PullPin_ReceiveConnection,
795     Parser_InputPin_Disconnect,
796     IPinImpl_ConnectedTo,
797     IPinImpl_ConnectionMediaType,
798     IPinImpl_QueryPinInfo,
799     IPinImpl_QueryDirection,
800     IPinImpl_QueryId,
801     IPinImpl_QueryAccept,
802     IPinImpl_EnumMediaTypes,
803     IPinImpl_QueryInternalConnections,
804     PullPin_EndOfStream,
805     PullPin_BeginFlush,
806     PullPin_EndFlush,
807     PullPin_NewSegment
808 };