shlwapi: Remove redundant "not NULL" checks of the len arg (coccicheck).
[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 IMediaSeekingVtbl Parser_Seeking_Vtbl;
41 static const IPinVtbl Parser_OutputPin_Vtbl;
42 static const IPinVtbl Parser_InputPin_Vtbl;
43
44 static HRESULT Parser_OutputPin_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt);
45 static HRESULT Parser_ChangeCurrent(IBaseFilter *iface);
46 static HRESULT Parser_ChangeStop(IBaseFilter *iface);
47 static HRESULT Parser_ChangeRate(IBaseFilter *iface);
48
49 static inline ParserImpl *impl_from_IMediaSeeking( IMediaSeeking *iface )
50 {
51     return (ParserImpl *)((char*)iface - FIELD_OFFSET(ParserImpl, mediaSeeking.lpVtbl));
52 }
53
54
55 HRESULT Parser_Create(ParserImpl* pParser, const IBaseFilterVtbl *Parser_Vtbl, const CLSID* pClsid, PFN_PROCESS_SAMPLE fnProcessSample, PFN_QUERY_ACCEPT fnQueryAccept, PFN_PRE_CONNECT fnPreConnect, PFN_CLEANUP fnCleanup, PFN_DISCONNECT fnDisconnect, REQUESTPROC fnRequest, STOPPROCESSPROC fnDone, CHANGEPROC stop, CHANGEPROC current, CHANGEPROC rate)
56 {
57     HRESULT hr;
58     PIN_INFO piInput;
59
60     /* pTransformFilter is already allocated */
61     pParser->clsid = *pClsid;
62     pParser->lpVtbl = Parser_Vtbl;
63     pParser->refCount = 1;
64     InitializeCriticalSection(&pParser->csFilter);
65     pParser->csFilter.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": ParserImpl.csFilter");
66     pParser->state = State_Stopped;
67     pParser->pClock = NULL;
68     pParser->fnDisconnect = fnDisconnect;
69     ZeroMemory(&pParser->filterInfo, sizeof(FILTER_INFO));
70     pParser->lastpinchange = GetTickCount();
71
72     pParser->cStreams = 0;
73     pParser->ppPins = CoTaskMemAlloc(1 * sizeof(IPin *));
74
75     /* construct input pin */
76     piInput.dir = PINDIR_INPUT;
77     piInput.pFilter = (IBaseFilter *)pParser;
78     lstrcpynW(piInput.achName, wcsInputPinName, sizeof(piInput.achName) / sizeof(piInput.achName[0]));
79
80     if (!current)
81         current = Parser_ChangeCurrent;
82
83     if (!stop)
84         stop = Parser_ChangeStop;
85
86     if (!rate)
87         rate = Parser_ChangeRate;
88
89     MediaSeekingImpl_Init((IBaseFilter*)pParser, stop, current, rate, &pParser->mediaSeeking, &pParser->csFilter);
90     pParser->mediaSeeking.lpVtbl = &Parser_Seeking_Vtbl;
91
92     hr = PullPin_Construct(&Parser_InputPin_Vtbl, &piInput, fnProcessSample, (LPVOID)pParser, fnQueryAccept, fnCleanup, fnRequest, fnDone, &pParser->csFilter, (IPin **)&pParser->pInputPin);
93
94     if (SUCCEEDED(hr))
95     {
96         pParser->ppPins[0] = (IPin *)pParser->pInputPin;
97         pParser->pInputPin->fnPreConnect = fnPreConnect;
98     }
99     else
100     {
101         CoTaskMemFree(pParser->ppPins);
102         pParser->csFilter.DebugInfo->Spare[0] = 0;
103         DeleteCriticalSection(&pParser->csFilter);
104         CoTaskMemFree(pParser);
105     }
106
107     return hr;
108 }
109
110 HRESULT WINAPI Parser_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
111 {
112     ParserImpl *This = (ParserImpl *)iface;
113     TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
114
115     *ppv = NULL;
116
117     if (IsEqualIID(riid, &IID_IUnknown))
118         *ppv = This;
119     else if (IsEqualIID(riid, &IID_IPersist))
120         *ppv = This;
121     else if (IsEqualIID(riid, &IID_IMediaFilter))
122         *ppv = This;
123     else if (IsEqualIID(riid, &IID_IBaseFilter))
124         *ppv = This;
125     else if (IsEqualIID(riid, &IID_IMediaSeeking))
126         *ppv = &This->mediaSeeking;
127
128     if (*ppv)
129     {
130         IUnknown_AddRef((IUnknown *)(*ppv));
131         return S_OK;
132     }
133
134     if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IVideoWindow))
135         FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
136
137     return E_NOINTERFACE;
138 }
139
140 ULONG WINAPI Parser_AddRef(IBaseFilter * iface)
141 {
142     ParserImpl *This = (ParserImpl *)iface;
143     ULONG refCount = InterlockedIncrement(&This->refCount);
144
145     TRACE("(%p/%p)->() AddRef from %d\n", This, iface, refCount - 1);
146
147     return refCount;
148 }
149
150 void Parser_Destroy(ParserImpl *This)
151 {
152     IPin *connected = NULL;
153     ULONG pinref;
154
155     assert(!This->refCount);
156     PullPin_WaitForStateChange(This->pInputPin, INFINITE);
157
158     if (This->pClock)
159         IReferenceClock_Release(This->pClock);
160
161     /* Don't need to clean up output pins, freeing input pin will do that */
162     IPin_ConnectedTo((IPin *)This->pInputPin, &connected);
163     if (connected)
164     {
165         assert(IPin_Disconnect(connected) == S_OK);
166         IPin_Release(connected);
167         assert(IPin_Disconnect((IPin *)This->pInputPin) == S_OK);
168     }
169     pinref = IPin_Release((IPin *)This->pInputPin);
170     if (pinref)
171     {
172         /* Valgrind could find this, if I kill it here */
173         ERR("pinref should be null, is %u, destroying anyway\n", pinref);
174         assert((LONG)pinref > 0);
175
176         while (pinref)
177             pinref = IPin_Release((IPin *)This->pInputPin);
178     }
179
180     CoTaskMemFree(This->ppPins);
181     This->lpVtbl = NULL;
182
183     This->csFilter.DebugInfo->Spare[0] = 0;
184     DeleteCriticalSection(&This->csFilter);
185
186     TRACE("Destroying parser\n");
187     CoTaskMemFree(This);
188 }
189
190 ULONG WINAPI Parser_Release(IBaseFilter * iface)
191 {
192     ParserImpl *This = (ParserImpl *)iface;
193     ULONG refCount = InterlockedDecrement(&This->refCount);
194
195     TRACE("(%p)->() Release from %d\n", This, refCount + 1);
196
197     if (!refCount)
198         Parser_Destroy(This);
199
200     return refCount;
201 }
202
203 /** IPersist methods **/
204
205 HRESULT WINAPI Parser_GetClassID(IBaseFilter * iface, CLSID * pClsid)
206 {
207     ParserImpl *This = (ParserImpl *)iface;
208
209     TRACE("(%p)\n", pClsid);
210
211     *pClsid = This->clsid;
212
213     return S_OK;
214 }
215
216 /** IMediaFilter methods **/
217
218 HRESULT WINAPI Parser_Stop(IBaseFilter * iface)
219 {
220     ParserImpl *This = (ParserImpl *)iface;
221     PullPin *pin = (PullPin *)This->ppPins[0];
222     ULONG i;
223
224     TRACE("()\n");
225
226     EnterCriticalSection(&pin->thread_lock);
227
228     IAsyncReader_BeginFlush(This->pInputPin->pReader);
229     EnterCriticalSection(&This->csFilter);
230
231     if (This->state == State_Stopped)
232     {
233         LeaveCriticalSection(&This->csFilter);
234         IAsyncReader_EndFlush(This->pInputPin->pReader);
235         LeaveCriticalSection(&pin->thread_lock);
236         return S_OK;
237     }
238
239     This->state = State_Stopped;
240
241     for (i = 1; i < (This->cStreams + 1); i++)
242     {
243         OutputPin_DecommitAllocator((OutputPin *)This->ppPins[i]);
244     }
245
246     LeaveCriticalSection(&This->csFilter);
247
248     PullPin_PauseProcessing(This->pInputPin);
249     PullPin_WaitForStateChange(This->pInputPin, INFINITE);
250     IAsyncReader_EndFlush(This->pInputPin->pReader);
251
252     LeaveCriticalSection(&pin->thread_lock);
253     return S_OK;
254 }
255
256 HRESULT WINAPI Parser_Pause(IBaseFilter * iface)
257 {
258     HRESULT hr = S_OK;
259     ParserImpl *This = (ParserImpl *)iface;
260     PullPin *pin = (PullPin *)This->ppPins[0];
261
262     TRACE("()\n");
263
264     EnterCriticalSection(&pin->thread_lock);
265     EnterCriticalSection(&This->csFilter);
266
267     if (This->state == State_Paused)
268     {
269         LeaveCriticalSection(&This->csFilter);
270         LeaveCriticalSection(&pin->thread_lock);
271         return S_OK;
272     }
273
274     if (This->state == State_Stopped)
275     {
276         LeaveCriticalSection(&This->csFilter);
277         hr = IBaseFilter_Run(iface, -1);
278         EnterCriticalSection(&This->csFilter);
279     }
280
281     if (SUCCEEDED(hr))
282         This->state = State_Paused;
283
284     LeaveCriticalSection(&This->csFilter);
285     LeaveCriticalSection(&pin->thread_lock);
286
287     return hr;
288 }
289
290 HRESULT WINAPI Parser_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
291 {
292     HRESULT hr = S_OK;
293     ParserImpl *This = (ParserImpl *)iface;
294     PullPin *pin = (PullPin *)This->ppPins[0];
295
296     ULONG i;
297
298     TRACE("(%s)\n", wine_dbgstr_longlong(tStart));
299
300     EnterCriticalSection(&pin->thread_lock);
301     EnterCriticalSection(&This->csFilter);
302     {
303         HRESULT hr_any = VFW_E_NOT_CONNECTED;
304
305         if (This->state == State_Running || This->state == State_Paused)
306         {
307             This->state = State_Running;
308             LeaveCriticalSection(&This->csFilter);
309             LeaveCriticalSection(&pin->thread_lock);
310             return S_OK;
311         }
312
313         This->rtStreamStart = tStart;
314
315         for (i = 1; i < (This->cStreams + 1); i++)
316         {
317             hr = OutputPin_CommitAllocator((OutputPin *)This->ppPins[i]);
318             if (SUCCEEDED(hr))
319                 hr_any = hr;
320         }
321
322         hr = hr_any;
323         if (SUCCEEDED(hr))
324         {
325             LeaveCriticalSection(&This->csFilter);
326             hr = PullPin_StartProcessing(This->pInputPin);
327             EnterCriticalSection(&This->csFilter);
328         }
329
330         if (SUCCEEDED(hr))
331             This->state = State_Running;
332     }
333     LeaveCriticalSection(&This->csFilter);
334     LeaveCriticalSection(&pin->thread_lock);
335
336     return hr;
337 }
338
339 HRESULT WINAPI Parser_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
340 {
341     ParserImpl *This = (ParserImpl *)iface;
342     PullPin *pin = (PullPin *)This->ppPins[0];
343     HRESULT hr = S_OK;
344
345     TRACE("(%d, %p)\n", dwMilliSecsTimeout, pState);
346
347     EnterCriticalSection(&pin->thread_lock);
348     EnterCriticalSection(&This->csFilter);
349     {
350         *pState = This->state;
351     }
352     LeaveCriticalSection(&This->csFilter);
353
354     if (This->pInputPin && (PullPin_WaitForStateChange(This->pInputPin, dwMilliSecsTimeout) == S_FALSE))
355         hr = VFW_S_STATE_INTERMEDIATE;
356     LeaveCriticalSection(&pin->thread_lock);
357
358     return hr;
359 }
360
361 HRESULT WINAPI Parser_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
362 {
363     ParserImpl *This = (ParserImpl *)iface;
364     PullPin *pin = (PullPin *)This->ppPins[0];
365
366     TRACE("(%p)\n", pClock);
367
368     EnterCriticalSection(&pin->thread_lock);
369     EnterCriticalSection(&This->csFilter);
370     {
371         if (This->pClock)
372             IReferenceClock_Release(This->pClock);
373         This->pClock = pClock;
374         if (This->pClock)
375             IReferenceClock_AddRef(This->pClock);
376     }
377     LeaveCriticalSection(&This->csFilter);
378     LeaveCriticalSection(&pin->thread_lock);
379
380     return S_OK;
381 }
382
383 HRESULT WINAPI Parser_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
384 {
385     ParserImpl *This = (ParserImpl *)iface;
386
387     TRACE("(%p)\n", ppClock);
388
389     EnterCriticalSection(&This->csFilter);
390     {
391         *ppClock = This->pClock;
392         if (This->pClock)
393             IReferenceClock_AddRef(This->pClock);
394     }
395     LeaveCriticalSection(&This->csFilter);
396     
397     return S_OK;
398 }
399
400 /** IBaseFilter implementation **/
401
402 /* FIXME: WRONG */
403 static HRESULT Parser_GetPin(IBaseFilter *iface, ULONG pos, IPin **pin, DWORD *lastsynctick)
404 {
405     ParserImpl *This = (ParserImpl *)iface;
406
407     *lastsynctick = This->lastpinchange;
408
409     TRACE("Asking for pos %x\n", pos);
410
411     /* Input pin also has a pin, hence the > and not >= */
412     if (pos > This->cStreams)
413         return S_FALSE;
414
415     *pin = This->ppPins[pos];
416     IPin_AddRef(*pin);
417     return S_OK;
418 }
419
420 HRESULT WINAPI Parser_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
421 {
422     ParserImpl *This = (ParserImpl *)iface;
423
424     TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
425
426     return IEnumPinsImpl_Construct(ppEnum, Parser_GetPin, iface);
427 }
428
429 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 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 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 HRESULT WINAPI Parser_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
474 {
475     TRACE("(%p)\n", pVendorInfo);
476     return E_NOTIMPL;
477 }
478
479 HRESULT Parser_AddPin(ParserImpl * This, const PIN_INFO * piOutput, ALLOCATOR_PROPERTIES * props, const AM_MEDIA_TYPE * amt)
480 {
481     IPin ** ppOldPins;
482     HRESULT hr;
483
484     ppOldPins = This->ppPins;
485
486     This->ppPins = CoTaskMemAlloc((This->cStreams + 2) * sizeof(IPin *));
487     memcpy(This->ppPins, ppOldPins, (This->cStreams + 1) * sizeof(IPin *));
488
489     hr = OutputPin_Construct(&Parser_OutputPin_Vtbl, sizeof(Parser_OutputPin), piOutput, props, NULL, Parser_OutputPin_QueryAccept, &This->csFilter, This->ppPins + (This->cStreams + 1));
490
491     if (SUCCEEDED(hr))
492     {
493         IPin *pPin = This->ppPins[This->cStreams + 1];
494         Parser_OutputPin *pin = (Parser_OutputPin *)pPin;
495         pin->pmt = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
496         CopyMediaType(pin->pmt, amt);
497         pin->dwSamplesProcessed = 0;
498
499         pin->pin.pin.pUserData = This->ppPins[This->cStreams + 1];
500         pin->pin.pin.pinInfo.pFilter = (LPVOID)This;
501         pin->pin.custom_allocator = 1;
502         This->cStreams++;
503         This->lastpinchange = GetTickCount();
504         CoTaskMemFree(ppOldPins);
505     }
506     else
507     {
508         CoTaskMemFree(This->ppPins);
509         This->ppPins = ppOldPins;
510         ERR("Failed with error %x\n", hr);
511     }
512
513     return hr;
514 }
515
516 static HRESULT Parser_RemoveOutputPins(ParserImpl * This)
517 {
518     /* NOTE: should be in critical section when calling this function */
519     HRESULT hr;
520     ULONG i;
521     IPin ** ppOldPins = This->ppPins;
522
523     TRACE("(%p)\n", This);
524
525     /* reduce the pin array down to 1 (just our input pin) */
526     This->ppPins = CoTaskMemAlloc(sizeof(IPin *) * 1);
527     memcpy(This->ppPins, ppOldPins, sizeof(IPin *) * 1);
528
529     for (i = 0; i < This->cStreams; i++)
530     {
531         hr = OutputPin_DeliverDisconnect((OutputPin *)ppOldPins[i + 1]);
532         TRACE("Disconnect: %08x\n", hr);
533         IPin_Release(ppOldPins[i + 1]);
534     }
535
536     This->lastpinchange = GetTickCount();
537     This->cStreams = 0;
538     CoTaskMemFree(ppOldPins);
539
540     return S_OK;
541 }
542
543 static HRESULT Parser_ChangeCurrent(IBaseFilter *iface)
544 {
545     FIXME("(%p) filter hasn't implemented current position change!\n", iface);
546     return S_OK;
547 }
548
549 static HRESULT Parser_ChangeStop(IBaseFilter *iface)
550 {
551     FIXME("(%p) filter hasn't implemented stop position change!\n", iface);
552     return S_OK;
553 }
554
555 static HRESULT Parser_ChangeRate(IBaseFilter *iface)
556 {
557     FIXME("(%p) filter hasn't implemented rate change!\n", iface);
558     return S_OK;
559 }
560
561
562 static HRESULT WINAPI Parser_Seeking_QueryInterface(IMediaSeeking * iface, REFIID riid, LPVOID * ppv)
563 {
564     ParserImpl *This = impl_from_IMediaSeeking(iface);
565
566     return IUnknown_QueryInterface((IUnknown *)This, riid, ppv);
567 }
568
569 static ULONG WINAPI Parser_Seeking_AddRef(IMediaSeeking * iface)
570 {
571     ParserImpl *This = impl_from_IMediaSeeking(iface);
572
573     return IUnknown_AddRef((IUnknown *)This);
574 }
575
576 static ULONG WINAPI Parser_Seeking_Release(IMediaSeeking * iface)
577 {
578     ParserImpl *This = impl_from_IMediaSeeking(iface);
579
580     return IUnknown_Release((IUnknown *)This);
581 }
582
583 static const IMediaSeekingVtbl Parser_Seeking_Vtbl =
584 {
585     Parser_Seeking_QueryInterface,
586     Parser_Seeking_AddRef,
587     Parser_Seeking_Release,
588     MediaSeekingImpl_GetCapabilities,
589     MediaSeekingImpl_CheckCapabilities,
590     MediaSeekingImpl_IsFormatSupported,
591     MediaSeekingImpl_QueryPreferredFormat,
592     MediaSeekingImpl_GetTimeFormat,
593     MediaSeekingImpl_IsUsingTimeFormat,
594     MediaSeekingImpl_SetTimeFormat,
595     MediaSeekingImpl_GetDuration,
596     MediaSeekingImpl_GetStopPosition,
597     MediaSeekingImpl_GetCurrentPosition,
598     MediaSeekingImpl_ConvertTimeFormat,
599     MediaSeekingImpl_SetPositions,
600     MediaSeekingImpl_GetPositions,
601     MediaSeekingImpl_GetAvailable,
602     MediaSeekingImpl_SetRate,
603     MediaSeekingImpl_GetRate,
604     MediaSeekingImpl_GetPreroll
605 };
606
607 static HRESULT WINAPI Parser_OutputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
608 {
609     Parser_OutputPin *This = (Parser_OutputPin *)iface;
610
611     TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
612
613     *ppv = NULL;
614
615     if (IsEqualIID(riid, &IID_IUnknown))
616         *ppv = iface;
617     else if (IsEqualIID(riid, &IID_IPin))
618         *ppv = iface;
619     else if (IsEqualIID(riid, &IID_IMediaSeeking))
620     {
621         return IBaseFilter_QueryInterface(This->pin.pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
622     }
623
624     if (*ppv)
625     {
626         IUnknown_AddRef((IUnknown *)(*ppv));
627         return S_OK;
628     }
629
630     FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
631
632     return E_NOINTERFACE;
633 }
634
635 static ULONG WINAPI Parser_OutputPin_Release(IPin * iface)
636 {
637     Parser_OutputPin *This = (Parser_OutputPin *)iface;
638     ULONG refCount = InterlockedDecrement(&This->pin.pin.refCount);
639     
640     TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
641
642     if (!refCount)
643     {
644         FreeMediaType(This->pmt);
645         CoTaskMemFree(This->pmt);
646         FreeMediaType(&This->pin.pin.mtCurrent);
647         CoTaskMemFree(This);
648         return 0;
649     }
650     return refCount;
651 }
652
653 static HRESULT WINAPI Parser_OutputPin_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
654 {
655     ENUMMEDIADETAILS emd;
656     Parser_OutputPin *This = (Parser_OutputPin *)iface;
657
658     TRACE("(%p)\n", ppEnum);
659
660     /* override this method to allow enumeration of your types */
661     emd.cMediaTypes = 1;
662     emd.pMediaTypes = This->pmt;
663
664     return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
665 }
666
667 static HRESULT WINAPI Parser_OutputPin_Connect(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
668 {
669     Parser_OutputPin *This = (Parser_OutputPin *)iface;
670     ParserImpl *parser = (ParserImpl *)This->pin.pin.pinInfo.pFilter;
671
672     /* Set the allocator to our input pin's */
673     EnterCriticalSection(This->pin.pin.pCritSec);
674     This->pin.alloc = parser->pInputPin->pAlloc;
675     LeaveCriticalSection(This->pin.pin.pCritSec);
676
677     return OutputPin_Connect(iface, pReceivePin, pmt);
678 }
679
680 static HRESULT Parser_OutputPin_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
681 {
682     Parser_OutputPin *This = iface;
683
684     TRACE("()\n");
685     dump_AM_MEDIA_TYPE(pmt);
686
687     return (memcmp(This->pmt, pmt, sizeof(AM_MEDIA_TYPE)) == 0);
688 }
689
690 static const IPinVtbl Parser_OutputPin_Vtbl = 
691 {
692     Parser_OutputPin_QueryInterface,
693     IPinImpl_AddRef,
694     Parser_OutputPin_Release,
695     Parser_OutputPin_Connect,
696     OutputPin_ReceiveConnection,
697     OutputPin_Disconnect,
698     IPinImpl_ConnectedTo,
699     IPinImpl_ConnectionMediaType,
700     IPinImpl_QueryPinInfo,
701     IPinImpl_QueryDirection,
702     IPinImpl_QueryId,
703     IPinImpl_QueryAccept,
704     Parser_OutputPin_EnumMediaTypes,
705     IPinImpl_QueryInternalConnections,
706     OutputPin_EndOfStream,
707     OutputPin_BeginFlush,
708     OutputPin_EndFlush,
709     OutputPin_NewSegment
710 };
711
712 static HRESULT WINAPI Parser_PullPin_Disconnect(IPin * iface)
713 {
714     HRESULT hr;
715     PullPin *This = (PullPin *)iface;
716
717     TRACE("()\n");
718
719     EnterCriticalSection(&This->thread_lock);
720     EnterCriticalSection(This->pin.pCritSec);
721     {
722         if (This->pin.pConnectedTo)
723         {
724             FILTER_STATE state;
725             ParserImpl *Parser = (ParserImpl *)This->pin.pinInfo.pFilter;
726
727             LeaveCriticalSection(This->pin.pCritSec);
728             hr = IBaseFilter_GetState(This->pin.pinInfo.pFilter, INFINITE, &state);
729             EnterCriticalSection(This->pin.pCritSec);
730
731             if (SUCCEEDED(hr) && (state == State_Stopped) && SUCCEEDED(Parser->fnDisconnect(Parser)))
732             {
733                 LeaveCriticalSection(This->pin.pCritSec);
734                 PullPin_Disconnect(iface);
735                 EnterCriticalSection(This->pin.pCritSec);
736                 hr = Parser_RemoveOutputPins((ParserImpl *)This->pin.pinInfo.pFilter);
737             }
738             else
739                 hr = VFW_E_NOT_STOPPED;
740         }
741         else
742             hr = S_FALSE;
743     }
744     LeaveCriticalSection(This->pin.pCritSec);
745     LeaveCriticalSection(&This->thread_lock);
746
747     return hr;
748 }
749
750 static HRESULT WINAPI Parser_PullPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
751 {
752     HRESULT hr;
753
754     TRACE("()\n");
755
756     hr = PullPin_ReceiveConnection(iface, pReceivePin, pmt);
757     if (FAILED(hr))
758     {
759         IPinImpl *This = (IPinImpl *)iface;
760
761         EnterCriticalSection(This->pCritSec);
762         Parser_RemoveOutputPins((ParserImpl *)This->pinInfo.pFilter);
763         LeaveCriticalSection(This->pCritSec);
764     }
765
766     return hr;
767 }
768
769 static const IPinVtbl Parser_InputPin_Vtbl =
770 {
771     PullPin_QueryInterface,
772     IPinImpl_AddRef,
773     PullPin_Release,
774     InputPin_Connect,
775     Parser_PullPin_ReceiveConnection,
776     Parser_PullPin_Disconnect,
777     IPinImpl_ConnectedTo,
778     IPinImpl_ConnectionMediaType,
779     IPinImpl_QueryPinInfo,
780     IPinImpl_QueryDirection,
781     IPinImpl_QueryId,
782     IPinImpl_QueryAccept,
783     IPinImpl_EnumMediaTypes,
784     IPinImpl_QueryInternalConnections,
785     PullPin_EndOfStream,
786     PullPin_BeginFlush,
787     PullPin_EndFlush,
788     PullPin_NewSegment
789 };