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