quartz: Don't pass reference time when running.
[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(IBaseFilter *iface);
47 static HRESULT Parser_ChangeStop(IBaseFilter *iface);
48 static HRESULT Parser_ChangeRate(IBaseFilter *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 ParserImpl *impl_from_IMediaSeeking( IMediaSeeking *iface )
53 {
54     return (ParserImpl *)((char*)iface - FIELD_OFFSET(ParserImpl, 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     MediaSeekingImpl_Init((IBaseFilter*)pParser, Parser_ChangeStop, Parser_ChangeStart, Parser_ChangeRate, &pParser->mediaSeeking);
84     pParser->mediaSeeking.lpVtbl = &Parser_Seeking_Vtbl;
85
86     hr = Parser_InputPin_Construct(&piInput, fnProcessSample, (LPVOID)pParser, fnQueryAccept, &pParser->csFilter, (IPin **)&pParser->pInputPin);
87
88     if (SUCCEEDED(hr))
89     {
90         pParser->ppPins[0] = (IPin *)pParser->pInputPin;
91         pParser->pInputPin->fnPreConnect = fnPreConnect;
92     }
93     else
94     {
95         CoTaskMemFree(pParser->ppPins);
96         pParser->csFilter.DebugInfo->Spare[0] = 0;
97         DeleteCriticalSection(&pParser->csFilter);
98         CoTaskMemFree(pParser);
99     }
100
101     return hr;
102 }
103
104 static HRESULT Parser_OutputPin_Init(const PIN_INFO * pPinInfo, ALLOCATOR_PROPERTIES * props, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, const AM_MEDIA_TYPE * pmt, LPCRITICAL_SECTION pCritSec, Parser_OutputPin * pPinImpl)
105 {
106     pPinImpl->pmt = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
107     CopyMediaType(pPinImpl->pmt, pmt);
108     pPinImpl->dwSamplesProcessed = 0;
109
110     return OutputPin_Init(pPinInfo, props, pUserData, pQueryAccept, pCritSec, &pPinImpl->pin);
111 }
112
113 static HRESULT Parser_OutputPin_Construct(const PIN_INFO * pPinInfo, ALLOCATOR_PROPERTIES * props, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, const AM_MEDIA_TYPE * pmt, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
114 {
115     Parser_OutputPin * pPinImpl;
116
117     *ppPin = NULL;
118
119     assert(pPinInfo->dir == PINDIR_OUTPUT);
120
121     pPinImpl = CoTaskMemAlloc(sizeof(Parser_OutputPin));
122
123     if (!pPinImpl)
124         return E_OUTOFMEMORY;
125
126     if (SUCCEEDED(Parser_OutputPin_Init(pPinInfo, props, pUserData, pQueryAccept, pmt, pCritSec, pPinImpl)))
127     {
128         pPinImpl->pin.pin.lpVtbl = &Parser_OutputPin_Vtbl;
129
130         *ppPin = (IPin *)pPinImpl;
131         return S_OK;
132     }
133
134     CoTaskMemFree(pPinImpl);
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     else if (IsEqualIID(riid, &IID_IMediaSeeking))
154         *ppv = (LPVOID)&This->mediaSeeking;
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                 LONGLONG duration;
294                 DOUBLE speed;
295
296                 IMediaSeeking_GetDuration((IMediaSeeking *)&This->mediaSeeking, &duration);
297                 IMediaSeeking_GetRate((IMediaSeeking *)&This->mediaSeeking, &speed);
298                 OutputPin_DeliverNewSegment((OutputPin *)This->ppPins[i], 0, duration, speed);
299                 OutputPin_CommitAllocator((OutputPin *)This->ppPins[i]);
300             }
301
302             /* FIXME: this is a little hacky: we have to deliver (at least?) one sample
303              * to each renderer before they will complete their transitions. We should probably
304              * seek through the stream for the first of each, rather than do it this way which is
305              * probably a bit prone to deadlocking */
306             hr = PullPin_StartProcessing(This->pInputPin);
307         }
308     }
309     /* FIXME: else pause thread */
310
311     if (SUCCEEDED(hr))
312         hr = PullPin_PauseProcessing(This->pInputPin);
313
314     return hr;
315 }
316
317 static HRESULT WINAPI Parser_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
318 {
319     HRESULT hr = S_OK;
320     ParserImpl *This = (ParserImpl *)iface;
321     int i;
322
323     TRACE("(%s)\n", wine_dbgstr_longlong(tStart));
324
325     EnterCriticalSection(&This->csFilter);
326     {
327         if (This->state == State_Running)
328         {
329             LeaveCriticalSection(&This->csFilter);
330             return S_OK;
331         }
332
333         This->rtStreamStart = tStart;
334
335         hr = PullPin_Seek(This->pInputPin, 0, ((LONGLONG)0x7fffffff << 32) | 0xffffffff);
336
337         if (SUCCEEDED(hr) && (This->state == State_Stopped))
338         {
339             hr = PullPin_InitProcessing(This->pInputPin);
340
341             if (SUCCEEDED(hr))
342             { 
343                 for (i = 1; i < (This->cStreams + 1); i++)
344                 {
345                     OutputPin_CommitAllocator((OutputPin *)This->ppPins[i]);
346                 }
347             }
348         }
349
350         if (SUCCEEDED(hr))
351             hr = PullPin_StartProcessing(This->pInputPin);
352
353         if (SUCCEEDED(hr))
354             This->state = State_Running;
355     }
356     LeaveCriticalSection(&This->csFilter);
357
358     return hr;
359 }
360
361 static HRESULT WINAPI Parser_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
362 {
363     ParserImpl *This = (ParserImpl *)iface;
364
365     TRACE("(%d, %p)\n", dwMilliSecsTimeout, pState);
366
367     EnterCriticalSection(&This->csFilter);
368     {
369         *pState = This->state;
370     }
371     LeaveCriticalSection(&This->csFilter);
372
373     /* FIXME: this is a little bit unsafe, but I don't see that we can do this
374      * while in the critical section. Maybe we could copy the pointer and addref in the
375      * critical section and then release after this.
376      */
377     if (This->pInputPin && (PullPin_WaitForStateChange(This->pInputPin, dwMilliSecsTimeout) == S_FALSE))
378         return VFW_S_STATE_INTERMEDIATE;
379
380     return S_OK;
381 }
382
383 static HRESULT WINAPI Parser_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
384 {
385     ParserImpl *This = (ParserImpl *)iface;
386
387     TRACE("(%p)\n", pClock);
388
389     EnterCriticalSection(&This->csFilter);
390     {
391         if (This->pClock)
392             IReferenceClock_Release(This->pClock);
393         This->pClock = pClock;
394         if (This->pClock)
395             IReferenceClock_AddRef(This->pClock);
396     }
397     LeaveCriticalSection(&This->csFilter);
398
399     return S_OK;
400 }
401
402 static HRESULT WINAPI Parser_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
403 {
404     ParserImpl *This = (ParserImpl *)iface;
405
406     TRACE("(%p)\n", ppClock);
407
408     EnterCriticalSection(&This->csFilter);
409     {
410         *ppClock = This->pClock;
411         if (This->pClock)
412             IReferenceClock_AddRef(This->pClock);
413     }
414     LeaveCriticalSection(&This->csFilter);
415     
416     return S_OK;
417 }
418
419 /** IBaseFilter implementation **/
420
421 static HRESULT WINAPI Parser_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
422 {
423     ENUMPINDETAILS epd;
424     ParserImpl *This = (ParserImpl *)iface;
425
426     TRACE("(%p)\n", ppEnum);
427
428     epd.cPins = This->cStreams + 1; /* +1 for input pin */
429     epd.ppPins = This->ppPins;
430     return IEnumPinsImpl_Construct(&epd, ppEnum);
431 }
432
433 static HRESULT WINAPI Parser_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
434 {
435     FIXME("(%p)->(%s,%p)\n", iface, debugstr_w(Id), ppPin);
436
437     /* FIXME: critical section */
438
439     return E_NOTIMPL;
440 }
441
442 static HRESULT WINAPI Parser_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
443 {
444     ParserImpl *This = (ParserImpl *)iface;
445
446     TRACE("(%p)\n", pInfo);
447
448     strcpyW(pInfo->achName, This->filterInfo.achName);
449     pInfo->pGraph = This->filterInfo.pGraph;
450
451     if (pInfo->pGraph)
452         IFilterGraph_AddRef(pInfo->pGraph);
453     
454     return S_OK;
455 }
456
457 static HRESULT WINAPI Parser_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
458 {
459     HRESULT hr = S_OK;
460     ParserImpl *This = (ParserImpl *)iface;
461
462     TRACE("(%p, %s)\n", pGraph, debugstr_w(pName));
463
464     EnterCriticalSection(&This->csFilter);
465     {
466         if (pName)
467             strcpyW(This->filterInfo.achName, pName);
468         else
469             *This->filterInfo.achName = '\0';
470         This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
471     }
472     LeaveCriticalSection(&This->csFilter);
473
474     return hr;
475 }
476
477 static HRESULT WINAPI Parser_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
478 {
479     TRACE("(%p)\n", pVendorInfo);
480     return E_NOTIMPL;
481 }
482
483 static const IBaseFilterVtbl Parser_Vtbl =
484 {
485     Parser_QueryInterface,
486     Parser_AddRef,
487     Parser_Release,
488     Parser_GetClassID,
489     Parser_Stop,
490     Parser_Pause,
491     Parser_Run,
492     Parser_GetState,
493     Parser_SetSyncSource,
494     Parser_GetSyncSource,
495     Parser_EnumPins,
496     Parser_FindPin,
497     Parser_QueryFilterInfo,
498     Parser_JoinFilterGraph,
499     Parser_QueryVendorInfo
500 };
501
502 HRESULT Parser_AddPin(ParserImpl * This, const PIN_INFO * piOutput, ALLOCATOR_PROPERTIES * props, const AM_MEDIA_TYPE * amt)
503 {
504     IPin ** ppOldPins;
505     HRESULT hr;
506
507     ppOldPins = This->ppPins;
508
509     This->ppPins = CoTaskMemAlloc((This->cStreams + 2) * sizeof(IPin *));
510     memcpy(This->ppPins, ppOldPins, (This->cStreams + 1) * sizeof(IPin *));
511
512     hr = Parser_OutputPin_Construct(piOutput, props, NULL, Parser_OutputPin_QueryAccept, amt, &This->csFilter, This->ppPins + This->cStreams + 1);
513
514     if (SUCCEEDED(hr))
515     {
516         ((Parser_OutputPin *)(This->ppPins[This->cStreams + 1]))->pin.pin.pUserData = (LPVOID)This->ppPins[This->cStreams + 1];
517         This->cStreams++;
518         CoTaskMemFree(ppOldPins);
519     }
520     else
521     {
522         CoTaskMemFree(This->ppPins);
523         This->ppPins = ppOldPins;
524         ERR("Failed with error %x\n", hr);
525     }
526
527     return hr;
528 }
529
530 static HRESULT Parser_RemoveOutputPins(ParserImpl * This)
531 {
532     /* NOTE: should be in critical section when calling this function */
533
534     ULONG i;
535     IPin ** ppOldPins = This->ppPins;
536
537     /* reduce the pin array down to 1 (just our input pin) */
538     This->ppPins = CoTaskMemAlloc(sizeof(IPin *) * 1);
539     memcpy(This->ppPins, ppOldPins, sizeof(IPin *) * 1);
540
541     for (i = 0; i < This->cStreams; i++)
542     {
543         OutputPin_DeliverDisconnect((OutputPin *)ppOldPins[i + 1]);
544         IPin_Release(ppOldPins[i + 1]);
545     }
546
547     This->cStreams = 0;
548     CoTaskMemFree(ppOldPins);
549
550     return S_OK;
551 }
552
553 static HRESULT Parser_ChangeStart(IBaseFilter *iface)
554 {
555     FIXME("(%p)\n", iface);
556     return S_OK;
557 }
558
559 static HRESULT Parser_ChangeStop(IBaseFilter *iface)
560 {
561     FIXME("(%p)\n", iface);
562     return S_OK;
563 }
564
565 static HRESULT Parser_ChangeRate(IBaseFilter *iface)
566 {
567     FIXME("(%p)\n", iface);
568     return S_OK;
569 }
570
571
572 static HRESULT WINAPI Parser_Seeking_QueryInterface(IMediaSeeking * iface, REFIID riid, LPVOID * ppv)
573 {
574     ParserImpl *This = impl_from_IMediaSeeking(iface);
575
576     return IUnknown_QueryInterface((IUnknown *)This, riid, ppv);
577 }
578
579 static ULONG WINAPI Parser_Seeking_AddRef(IMediaSeeking * iface)
580 {
581     ParserImpl *This = impl_from_IMediaSeeking(iface);
582
583     return IUnknown_AddRef((IUnknown *)This);
584 }
585
586 static ULONG WINAPI Parser_Seeking_Release(IMediaSeeking * iface)
587 {
588     ParserImpl *This = impl_from_IMediaSeeking(iface);
589
590     return IUnknown_Release((IUnknown *)This);
591 }
592
593 static const IMediaSeekingVtbl Parser_Seeking_Vtbl =
594 {
595     Parser_Seeking_QueryInterface,
596     Parser_Seeking_AddRef,
597     Parser_Seeking_Release,
598     MediaSeekingImpl_GetCapabilities,
599     MediaSeekingImpl_CheckCapabilities,
600     MediaSeekingImpl_IsFormatSupported,
601     MediaSeekingImpl_QueryPreferredFormat,
602     MediaSeekingImpl_GetTimeFormat,
603     MediaSeekingImpl_IsUsingTimeFormat,
604     MediaSeekingImpl_SetTimeFormat,
605     MediaSeekingImpl_GetDuration,
606     MediaSeekingImpl_GetStopPosition,
607     MediaSeekingImpl_GetCurrentPosition,
608     MediaSeekingImpl_ConvertTimeFormat,
609     MediaSeekingImpl_SetPositions,
610     MediaSeekingImpl_GetPositions,
611     MediaSeekingImpl_GetAvailable,
612     MediaSeekingImpl_SetRate,
613     MediaSeekingImpl_GetRate,
614     MediaSeekingImpl_GetPreroll
615 };
616
617 static HRESULT WINAPI Parser_OutputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
618 {
619     Parser_OutputPin *This = (Parser_OutputPin *)iface;
620
621     TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
622
623     *ppv = NULL;
624
625     if (IsEqualIID(riid, &IID_IUnknown))
626         *ppv = (LPVOID)iface;
627     else if (IsEqualIID(riid, &IID_IPin))
628         *ppv = (LPVOID)iface;
629     else if (IsEqualIID(riid, &IID_IMediaSeeking))
630     {
631         return IBaseFilter_QueryInterface((IBaseFilter*)&This->pin.pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
632     }
633
634     if (*ppv)
635     {
636         IUnknown_AddRef((IUnknown *)(*ppv));
637         return S_OK;
638     }
639
640     FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
641
642     return E_NOINTERFACE;
643 }
644
645 static ULONG WINAPI Parser_OutputPin_Release(IPin * iface)
646 {
647     Parser_OutputPin *This = (Parser_OutputPin *)iface;
648     ULONG refCount = InterlockedDecrement(&This->pin.pin.refCount);
649     
650     TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
651     
652     if (!refCount)
653     {
654         FreeMediaType(This->pmt);
655         CoTaskMemFree(This->pmt);
656         FreeMediaType(&This->pin.pin.mtCurrent);
657         CoTaskMemFree(This);
658         return 0;
659     }
660     return refCount;
661 }
662
663 static HRESULT WINAPI Parser_OutputPin_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
664 {
665     ENUMMEDIADETAILS emd;
666     Parser_OutputPin *This = (Parser_OutputPin *)iface;
667
668     TRACE("(%p)\n", ppEnum);
669
670     /* override this method to allow enumeration of your types */
671     emd.cMediaTypes = 1;
672     emd.pMediaTypes = This->pmt;
673
674     return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
675 }
676
677 static HRESULT Parser_OutputPin_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
678 {
679     Parser_OutputPin *This = (Parser_OutputPin *)iface;
680
681     TRACE("()\n");
682     dump_AM_MEDIA_TYPE(pmt);
683
684     return (memcmp(This->pmt, pmt, sizeof(AM_MEDIA_TYPE)) == 0);
685 }
686
687 static const IPinVtbl Parser_OutputPin_Vtbl = 
688 {
689     Parser_OutputPin_QueryInterface,
690     IPinImpl_AddRef,
691     Parser_OutputPin_Release,
692     OutputPin_Connect,
693     OutputPin_ReceiveConnection,
694     OutputPin_Disconnect,
695     IPinImpl_ConnectedTo,
696     IPinImpl_ConnectionMediaType,
697     IPinImpl_QueryPinInfo,
698     IPinImpl_QueryDirection,
699     IPinImpl_QueryId,
700     IPinImpl_QueryAccept,
701     Parser_OutputPin_EnumMediaTypes,
702     IPinImpl_QueryInternalConnections,
703     OutputPin_EndOfStream,
704     OutputPin_BeginFlush,
705     OutputPin_EndFlush,
706     OutputPin_NewSegment
707 };
708
709 static HRESULT Parser_InputPin_Construct(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
710 {
711     PullPin * pPinImpl;
712
713     *ppPin = NULL;
714
715     if (pPinInfo->dir != PINDIR_INPUT)
716     {
717         ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
718         return E_INVALIDARG;
719     }
720
721     pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
722
723     if (!pPinImpl)
724         return E_OUTOFMEMORY;
725
726     if (SUCCEEDED(PullPin_Init(pPinInfo, pSampleProc, pUserData, pQueryAccept, pCritSec, pPinImpl)))
727     {
728         pPinImpl->pin.lpVtbl = &Parser_InputPin_Vtbl;
729
730         *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
731         return S_OK;
732     }
733
734     CoTaskMemFree(pPinImpl);
735     return E_FAIL;
736 }
737
738 static HRESULT WINAPI Parser_InputPin_Disconnect(IPin * iface)
739 {
740     HRESULT hr;
741     IPinImpl *This = (IPinImpl *)iface;
742
743     TRACE("()\n");
744
745     EnterCriticalSection(This->pCritSec);
746     {
747         if (This->pConnectedTo)
748         {
749             FILTER_STATE state;
750
751             hr = IBaseFilter_GetState(This->pinInfo.pFilter, 0, &state);
752
753             if (SUCCEEDED(hr) && (state == State_Stopped))
754             {
755                 IPin_Release(This->pConnectedTo);
756                 This->pConnectedTo = NULL;
757                 hr = Parser_RemoveOutputPins((ParserImpl *)This->pinInfo.pFilter);
758             }
759             else
760                 hr = VFW_E_NOT_STOPPED;
761         }
762         else
763             hr = S_FALSE;
764     }
765     LeaveCriticalSection(This->pCritSec);
766     
767     return hr;
768 }
769
770 HRESULT WINAPI Parser_PullPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
771 {
772     HRESULT hr;
773
774     TRACE("()\n");
775
776     hr = PullPin_ReceiveConnection(iface, pReceivePin, pmt);
777     if (FAILED(hr))
778     {
779         IPinImpl *This = (IPinImpl *)iface;
780
781         EnterCriticalSection(This->pCritSec);
782         Parser_RemoveOutputPins((ParserImpl *)This->pinInfo.pFilter);
783         LeaveCriticalSection(This->pCritSec);
784     }
785
786     return hr;
787 }
788
789 static const IPinVtbl Parser_InputPin_Vtbl =
790 {
791     PullPin_QueryInterface,
792     IPinImpl_AddRef,
793     PullPin_Release,
794     OutputPin_Connect,
795     Parser_PullPin_ReceiveConnection,
796     Parser_InputPin_Disconnect,
797     IPinImpl_ConnectedTo,
798     IPinImpl_ConnectionMediaType,
799     IPinImpl_QueryPinInfo,
800     IPinImpl_QueryDirection,
801     IPinImpl_QueryId,
802     IPinImpl_QueryAccept,
803     IPinImpl_EnumMediaTypes,
804     IPinImpl_QueryInternalConnections,
805     PullPin_EndOfStream,
806     PullPin_BeginFlush,
807     PullPin_EndFlush,
808     PullPin_NewSegment
809 };