strmbase: More properly standardized pin implementations for NewSegment.
[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 "pin.h"
24
25 #include "vfwmsgs.h"
26 #include "amvideo.h"
27
28 #include "wine/unicode.h"
29 #include "wine/debug.h"
30
31 #include <math.h>
32 #include <assert.h>
33
34 #include "parser.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
37
38 static const WCHAR wcsInputPinName[] = {'i','n','p','u','t',' ','p','i','n',0};
39 static const IMediaSeekingVtbl Parser_Seeking_Vtbl;
40 static const IPinVtbl Parser_OutputPin_Vtbl;
41 static const IPinVtbl Parser_InputPin_Vtbl;
42
43 static HRESULT WINAPI Parser_ChangeStart(IMediaSeeking *iface);
44 static HRESULT WINAPI Parser_ChangeStop(IMediaSeeking *iface);
45 static HRESULT WINAPI Parser_ChangeRate(IMediaSeeking *iface);
46 static HRESULT WINAPI Parser_OutputPin_DecideBufferSize(BaseOutputPin *iface, IMemAllocator *pAlloc, ALLOCATOR_PROPERTIES *ppropInputRequest);
47 static HRESULT WINAPI Parser_OutputPin_GetMediaType(BasePin *iface, int iPosition, AM_MEDIA_TYPE *pmt);
48 static HRESULT WINAPI Parser_OutputPin_DecideAllocator(BaseOutputPin *This, IMemInputPin *pPin, IMemAllocator **pAlloc);
49 static HRESULT WINAPI Parser_OutputPin_BreakConnect(BaseOutputPin *This);
50
51 static inline ParserImpl *impl_from_IMediaSeeking( IMediaSeeking *iface )
52 {
53     return (ParserImpl *)((char*)iface - FIELD_OFFSET(ParserImpl, sourceSeeking.lpVtbl));
54 }
55
56 /* FIXME: WRONG */
57 static IPin* WINAPI Parser_GetPin(BaseFilter *iface, int pos)
58 {
59     ParserImpl *This = (ParserImpl *)iface;
60
61     TRACE("Asking for pos %x\n", pos);
62
63     /* Input pin also has a pin, hence the > and not >= */
64     if (pos > This->cStreams || pos < 0)
65         return NULL;
66
67     IPin_AddRef(This->ppPins[pos]);
68     return This->ppPins[pos];
69 }
70
71 static LONG WINAPI Parser_GetPinCount(BaseFilter *iface)
72 {
73     ParserImpl *This = (ParserImpl *)iface;
74
75     return This->cStreams;
76 }
77
78 static const BaseFilterFuncTable BaseFuncTable = {
79     Parser_GetPin,
80     Parser_GetPinCount
81 };
82
83 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, SourceSeeking_ChangeStop stop, SourceSeeking_ChangeStart start, SourceSeeking_ChangeRate rate)
84 {
85     HRESULT hr;
86     PIN_INFO piInput;
87
88     /* pTransformFilter is already allocated */
89     BaseFilter_Init(&pParser->filter, Parser_Vtbl, pClsid, (DWORD_PTR)(__FILE__ ": ParserImpl.csFilter"), &BaseFuncTable);
90
91     pParser->fnDisconnect = fnDisconnect;
92
93     pParser->cStreams = 0;
94     pParser->ppPins = CoTaskMemAlloc(1 * sizeof(IPin *));
95
96     /* construct input pin */
97     piInput.dir = PINDIR_INPUT;
98     piInput.pFilter = (IBaseFilter *)pParser;
99     lstrcpynW(piInput.achName, wcsInputPinName, sizeof(piInput.achName) / sizeof(piInput.achName[0]));
100
101     if (!start)
102         start = Parser_ChangeStart;
103
104     if (!stop)
105         stop = Parser_ChangeStop;
106
107     if (!rate)
108         rate = Parser_ChangeRate;
109
110     SourceSeeking_Init(&pParser->sourceSeeking, &Parser_Seeking_Vtbl, stop, start, rate,  &pParser->filter.csFilter);
111
112     hr = PullPin_Construct(&Parser_InputPin_Vtbl, &piInput, fnProcessSample, (LPVOID)pParser, fnQueryAccept, fnCleanup, fnRequest, fnDone, &pParser->filter.csFilter, (IPin **)&pParser->pInputPin);
113
114     if (SUCCEEDED(hr))
115     {
116         pParser->ppPins[0] = (IPin *)pParser->pInputPin;
117         pParser->pInputPin->fnPreConnect = fnPreConnect;
118     }
119     else
120     {
121         CoTaskMemFree(pParser->ppPins);
122         BaseFilterImpl_Release((IBaseFilter*)pParser);
123         CoTaskMemFree(pParser);
124     }
125
126     return hr;
127 }
128
129 HRESULT WINAPI Parser_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
130 {
131     ParserImpl *This = (ParserImpl *)iface;
132     TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
133
134     *ppv = NULL;
135
136     if ( IsEqualIID(riid, &IID_IUnknown)
137       || IsEqualIID(riid, &IID_IPersist)
138       || IsEqualIID(riid, &IID_IMediaFilter)
139       || IsEqualIID(riid, &IID_IBaseFilter) )
140         *ppv = This;
141
142     if (*ppv)
143     {
144         IUnknown_AddRef((IUnknown *)(*ppv));
145         return S_OK;
146     }
147
148     if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IVideoWindow))
149         FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
150
151     return E_NOINTERFACE;
152 }
153
154 ULONG WINAPI Parser_AddRef(IBaseFilter * iface)
155 {
156     return BaseFilterImpl_AddRef(iface);
157 }
158
159 void Parser_Destroy(ParserImpl *This)
160 {
161     IPin *connected = NULL;
162     ULONG pinref;
163
164     assert(!This->filter.refCount);
165     PullPin_WaitForStateChange(This->pInputPin, INFINITE);
166
167     /* Don't need to clean up output pins, freeing input pin will do that */
168     IPin_ConnectedTo((IPin *)This->pInputPin, &connected);
169     if (connected)
170     {
171         assert(IPin_Disconnect(connected) == S_OK);
172         IPin_Release(connected);
173         assert(IPin_Disconnect((IPin *)This->pInputPin) == S_OK);
174     }
175     pinref = IPin_Release((IPin *)This->pInputPin);
176     if (pinref)
177     {
178         /* Valgrind could find this, if I kill it here */
179         ERR("pinref should be null, is %u, destroying anyway\n", pinref);
180         assert((LONG)pinref > 0);
181
182         while (pinref)
183             pinref = IPin_Release((IPin *)This->pInputPin);
184     }
185
186     CoTaskMemFree(This->ppPins);
187
188     TRACE("Destroying parser\n");
189     CoTaskMemFree(This);
190 }
191
192 ULONG WINAPI Parser_Release(IBaseFilter * iface)
193 {
194     ParserImpl *This = (ParserImpl *)iface;
195     ULONG refCount = BaseFilterImpl_Release(iface);
196
197     TRACE("(%p)->() Release from %d\n", This, refCount + 1);
198
199     if (!refCount)
200         Parser_Destroy(This);
201
202     return refCount;
203 }
204
205 /** IPersist methods **/
206
207 HRESULT WINAPI Parser_GetClassID(IBaseFilter * iface, CLSID * pClsid)
208 {
209     ParserImpl *This = (ParserImpl *)iface;
210
211     TRACE("(%p)\n", pClsid);
212
213     *pClsid = This->filter.clsid;
214
215     return S_OK;
216 }
217
218 /** IMediaFilter methods **/
219
220 HRESULT WINAPI Parser_Stop(IBaseFilter * iface)
221 {
222     ParserImpl *This = (ParserImpl *)iface;
223     PullPin *pin = (PullPin *)This->ppPins[0];
224     ULONG i;
225
226     TRACE("()\n");
227
228     EnterCriticalSection(&pin->thread_lock);
229
230     IAsyncReader_BeginFlush(This->pInputPin->pReader);
231     EnterCriticalSection(&This->filter.csFilter);
232
233     if (This->filter.state == State_Stopped)
234     {
235         LeaveCriticalSection(&This->filter.csFilter);
236         IAsyncReader_EndFlush(This->pInputPin->pReader);
237         LeaveCriticalSection(&pin->thread_lock);
238         return S_OK;
239     }
240
241     This->filter.state = State_Stopped;
242
243     for (i = 1; i < (This->cStreams + 1); i++)
244     {
245         BaseOutputPinImpl_Inactive((BaseOutputPin *)This->ppPins[i]);
246     }
247
248     LeaveCriticalSection(&This->filter.csFilter);
249
250     PullPin_PauseProcessing(This->pInputPin);
251     PullPin_WaitForStateChange(This->pInputPin, INFINITE);
252     IAsyncReader_EndFlush(This->pInputPin->pReader);
253
254     LeaveCriticalSection(&pin->thread_lock);
255     return S_OK;
256 }
257
258 HRESULT WINAPI Parser_Pause(IBaseFilter * iface)
259 {
260     HRESULT hr = S_OK;
261     ParserImpl *This = (ParserImpl *)iface;
262     PullPin *pin = (PullPin *)This->ppPins[0];
263
264     TRACE("()\n");
265
266     EnterCriticalSection(&pin->thread_lock);
267     EnterCriticalSection(&This->filter.csFilter);
268
269     if (This->filter.state == State_Paused)
270     {
271         LeaveCriticalSection(&This->filter.csFilter);
272         LeaveCriticalSection(&pin->thread_lock);
273         return S_OK;
274     }
275
276     if (This->filter.state == State_Stopped)
277     {
278         LeaveCriticalSection(&This->filter.csFilter);
279         hr = IBaseFilter_Run(iface, -1);
280         EnterCriticalSection(&This->filter.csFilter);
281     }
282
283     if (SUCCEEDED(hr))
284         This->filter.state = State_Paused;
285
286     LeaveCriticalSection(&This->filter.csFilter);
287     LeaveCriticalSection(&pin->thread_lock);
288
289     return hr;
290 }
291
292 HRESULT WINAPI Parser_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
293 {
294     HRESULT hr = S_OK;
295     ParserImpl *This = (ParserImpl *)iface;
296     PullPin *pin = (PullPin *)This->ppPins[0];
297
298     ULONG i;
299
300     TRACE("(%s)\n", wine_dbgstr_longlong(tStart));
301
302     EnterCriticalSection(&pin->thread_lock);
303     EnterCriticalSection(&This->filter.csFilter);
304     {
305         HRESULT hr_any = VFW_E_NOT_CONNECTED;
306
307         if (This->filter.state == State_Running || This->filter.state == State_Paused)
308         {
309             This->filter.state = State_Running;
310             LeaveCriticalSection(&This->filter.csFilter);
311             LeaveCriticalSection(&pin->thread_lock);
312             return S_OK;
313         }
314
315         This->filter.rtStreamStart = tStart;
316
317         for (i = 1; i < (This->cStreams + 1); i++)
318         {
319             hr = BaseOutputPinImpl_Active((BaseOutputPin *)This->ppPins[i]);
320             if (SUCCEEDED(hr))
321                 hr_any = hr;
322         }
323
324         hr = hr_any;
325         if (SUCCEEDED(hr))
326         {
327             LeaveCriticalSection(&This->filter.csFilter);
328             hr = PullPin_StartProcessing(This->pInputPin);
329             EnterCriticalSection(&This->filter.csFilter);
330         }
331
332         if (SUCCEEDED(hr))
333             This->filter.state = State_Running;
334     }
335     LeaveCriticalSection(&This->filter.csFilter);
336     LeaveCriticalSection(&pin->thread_lock);
337
338     return hr;
339 }
340
341 HRESULT WINAPI Parser_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
342 {
343     ParserImpl *This = (ParserImpl *)iface;
344     PullPin *pin = (PullPin *)This->ppPins[0];
345     HRESULT hr = S_OK;
346
347     TRACE("(%d, %p)\n", dwMilliSecsTimeout, pState);
348
349     EnterCriticalSection(&pin->thread_lock);
350     EnterCriticalSection(&This->filter.csFilter);
351     {
352         *pState = This->filter.state;
353     }
354     LeaveCriticalSection(&This->filter.csFilter);
355
356     if (This->pInputPin && (PullPin_WaitForStateChange(This->pInputPin, dwMilliSecsTimeout) == S_FALSE))
357         hr = VFW_S_STATE_INTERMEDIATE;
358     LeaveCriticalSection(&pin->thread_lock);
359
360     return hr;
361 }
362
363 HRESULT WINAPI Parser_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
364 {
365     ParserImpl *This = (ParserImpl *)iface;
366     PullPin *pin = (PullPin *)This->ppPins[0];
367
368     TRACE("(%p)\n", pClock);
369
370     EnterCriticalSection(&pin->thread_lock);
371     BaseFilterImpl_SetSyncSource(iface,pClock);
372     LeaveCriticalSection(&pin->thread_lock);
373
374     return S_OK;
375 }
376
377 HRESULT WINAPI Parser_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
378 {
379     return BaseFilterImpl_GetSyncSource(iface, ppClock);
380 }
381
382 /** IBaseFilter implementation **/
383
384 HRESULT WINAPI Parser_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
385 {
386     return BaseFilterImpl_EnumPins(iface,ppEnum);
387 }
388
389 HRESULT WINAPI Parser_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
390 {
391     FIXME("(%p)->(%s,%p)\n", iface, debugstr_w(Id), ppPin);
392
393     /* FIXME: critical section */
394
395     return E_NOTIMPL;
396 }
397
398 HRESULT WINAPI Parser_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
399 {
400     return BaseFilterImpl_QueryFilterInfo(iface, pInfo);
401 }
402
403 HRESULT WINAPI Parser_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
404 {
405     return BaseFilterImpl_JoinFilterGraph(iface, pGraph, pName);
406 }
407
408 HRESULT WINAPI Parser_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
409 {
410     return BaseFilterImpl_QueryVendorInfo(iface, pVendorInfo);
411 }
412
413 static const  BasePinFuncTable output_BaseFuncTable = {
414     NULL,
415     BaseOutputPinImpl_AttemptConnection,
416     BasePinImpl_GetMediaTypeVersion,
417     Parser_OutputPin_GetMediaType
418 };
419
420 static const BaseOutputPinFuncTable output_BaseOutputFuncTable = {
421     Parser_OutputPin_DecideBufferSize,
422     Parser_OutputPin_DecideAllocator,
423     Parser_OutputPin_BreakConnect
424 };
425
426 HRESULT Parser_AddPin(ParserImpl * This, const PIN_INFO * piOutput, ALLOCATOR_PROPERTIES * props, const AM_MEDIA_TYPE * amt)
427 {
428     IPin ** ppOldPins;
429     HRESULT hr;
430
431     ppOldPins = This->ppPins;
432
433     This->ppPins = CoTaskMemAlloc((This->cStreams + 2) * sizeof(IPin *));
434     memcpy(This->ppPins, ppOldPins, (This->cStreams + 1) * sizeof(IPin *));
435
436     hr = BaseOutputPin_Construct(&Parser_OutputPin_Vtbl, sizeof(Parser_OutputPin), piOutput, &output_BaseFuncTable, &output_BaseOutputFuncTable, &This->filter.csFilter, This->ppPins + (This->cStreams + 1));
437
438     if (SUCCEEDED(hr))
439     {
440         IPin *pPin = This->ppPins[This->cStreams + 1];
441         Parser_OutputPin *pin = (Parser_OutputPin *)pPin;
442         pin->pmt = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
443         CopyMediaType(pin->pmt, amt);
444         pin->dwSamplesProcessed = 0;
445
446         pin->pin.pin.pinInfo.pFilter = (LPVOID)This;
447         pin->allocProps = *props;
448         This->cStreams++;
449         BaseFilterImpl_IncrementPinVersion((BaseFilter*)This);
450         CoTaskMemFree(ppOldPins);
451     }
452     else
453     {
454         CoTaskMemFree(This->ppPins);
455         This->ppPins = ppOldPins;
456         ERR("Failed with error %x\n", hr);
457     }
458
459     return hr;
460 }
461
462 static HRESULT Parser_RemoveOutputPins(ParserImpl * This)
463 {
464     /* NOTE: should be in critical section when calling this function */
465     HRESULT hr;
466     ULONG i;
467     IPin ** ppOldPins = This->ppPins;
468
469     TRACE("(%p)\n", This);
470
471     /* reduce the pin array down to 1 (just our input pin) */
472     This->ppPins = CoTaskMemAlloc(sizeof(IPin *) * 1);
473     memcpy(This->ppPins, ppOldPins, sizeof(IPin *) * 1);
474
475     for (i = 0; i < This->cStreams; i++)
476     {
477         hr = ((BaseOutputPin *)ppOldPins[i + 1])->pFuncsTable->pfnBreakConnect((BaseOutputPin *)ppOldPins[i + 1]);
478         TRACE("Disconnect: %08x\n", hr);
479         IPin_Release(ppOldPins[i + 1]);
480     }
481
482     BaseFilterImpl_IncrementPinVersion((BaseFilter*)This);
483     This->cStreams = 0;
484     CoTaskMemFree(ppOldPins);
485
486     return S_OK;
487 }
488
489 static HRESULT WINAPI Parser_ChangeStart(IMediaSeeking *iface)
490 {
491     FIXME("(%p) filter hasn't implemented start position change!\n", iface);
492     return S_OK;
493 }
494
495 static HRESULT WINAPI Parser_ChangeStop(IMediaSeeking *iface)
496 {
497     FIXME("(%p) filter hasn't implemented stop position change!\n", iface);
498     return S_OK;
499 }
500
501 static HRESULT WINAPI Parser_ChangeRate(IMediaSeeking *iface)
502 {
503     FIXME("(%p) filter hasn't implemented rate change!\n", iface);
504     return S_OK;
505 }
506
507
508 static HRESULT WINAPI Parser_Seeking_QueryInterface(IMediaSeeking * iface, REFIID riid, LPVOID * ppv)
509 {
510     ParserImpl *This = impl_from_IMediaSeeking(iface);
511
512     return IUnknown_QueryInterface((IUnknown *)This, riid, ppv);
513 }
514
515 static ULONG WINAPI Parser_Seeking_AddRef(IMediaSeeking * iface)
516 {
517     ParserImpl *This = impl_from_IMediaSeeking(iface);
518
519     return IUnknown_AddRef((IUnknown *)This);
520 }
521
522 static ULONG WINAPI Parser_Seeking_Release(IMediaSeeking * iface)
523 {
524     ParserImpl *This = impl_from_IMediaSeeking(iface);
525
526     return IUnknown_Release((IUnknown *)This);
527 }
528
529 static const IMediaSeekingVtbl Parser_Seeking_Vtbl =
530 {
531     Parser_Seeking_QueryInterface,
532     Parser_Seeking_AddRef,
533     Parser_Seeking_Release,
534     SourceSeekingImpl_GetCapabilities,
535     SourceSeekingImpl_CheckCapabilities,
536     SourceSeekingImpl_IsFormatSupported,
537     SourceSeekingImpl_QueryPreferredFormat,
538     SourceSeekingImpl_GetTimeFormat,
539     SourceSeekingImpl_IsUsingTimeFormat,
540     SourceSeekingImpl_SetTimeFormat,
541     SourceSeekingImpl_GetDuration,
542     SourceSeekingImpl_GetStopPosition,
543     SourceSeekingImpl_GetCurrentPosition,
544     SourceSeekingImpl_ConvertTimeFormat,
545     SourceSeekingImpl_SetPositions,
546     SourceSeekingImpl_GetPositions,
547     SourceSeekingImpl_GetAvailable,
548     SourceSeekingImpl_SetRate,
549     SourceSeekingImpl_GetRate,
550     SourceSeekingImpl_GetPreroll
551 };
552
553 static HRESULT WINAPI Parser_OutputPin_DecideBufferSize(BaseOutputPin *iface, IMemAllocator *pAlloc, ALLOCATOR_PROPERTIES *ppropInputRequest)
554 {
555     Parser_OutputPin *This = (Parser_OutputPin *)iface;
556     ALLOCATOR_PROPERTIES actual;
557
558     if (ppropInputRequest->cbAlign && ppropInputRequest->cbAlign != This->allocProps.cbAlign)
559         FIXME("Requested Buffer cbAlign mismatch %i,%i\n",This->allocProps.cbAlign, ppropInputRequest->cbAlign);
560     if (ppropInputRequest->cbPrefix)
561         FIXME("Requested Buffer cbPrefix mismatch %i,%i\n",This->allocProps.cbPrefix, ppropInputRequest->cbPrefix);
562     if (ppropInputRequest->cbBuffer)
563         FIXME("Requested Buffer cbBuffer mismatch %i,%i\n",This->allocProps.cbBuffer, ppropInputRequest->cbBuffer);
564     if (ppropInputRequest->cBuffers)
565         FIXME("Requested Buffer cBuffers mismatch %i,%i\n",This->allocProps.cBuffers, ppropInputRequest->cBuffers);
566
567     return IMemAllocator_SetProperties(pAlloc, &This->allocProps, &actual);
568 }
569
570 static HRESULT WINAPI Parser_OutputPin_GetMediaType(BasePin *iface, int iPosition, AM_MEDIA_TYPE *pmt)
571 {
572     Parser_OutputPin *This = (Parser_OutputPin *)iface;
573     if (iPosition < 0)
574         return E_INVALIDARG;
575     if (iPosition > 0)
576         return VFW_S_NO_MORE_ITEMS;
577     CopyMediaType(pmt, This->pmt);
578     return S_OK;
579 }
580
581 static HRESULT WINAPI Parser_OutputPin_DecideAllocator(BaseOutputPin *iface, IMemInputPin *pPin, IMemAllocator **pAlloc)
582 {
583     Parser_OutputPin *This = (Parser_OutputPin *)iface;
584     HRESULT hr;
585
586     pAlloc = NULL;
587
588     if (This->alloc)
589         hr = IMemInputPin_NotifyAllocator(pPin, This->alloc, This->readonly);
590     else
591         hr = VFW_E_NO_ALLOCATOR;
592
593     return hr;
594 }
595
596 static HRESULT WINAPI Parser_OutputPin_BreakConnect(BaseOutputPin *This)
597 {
598     HRESULT hr;
599
600     TRACE("(%p)->()\n", This);
601
602     EnterCriticalSection(This->pin.pCritSec);
603     if (!This->pin.pConnectedTo || !This->pMemInputPin)
604         hr = VFW_E_NOT_CONNECTED;
605     else
606     {
607         hr = IPin_Disconnect(This->pin.pConnectedTo);
608         IPin_Disconnect((IPin *)This);
609     }
610     LeaveCriticalSection(This->pin.pCritSec);
611
612     return hr;
613 }
614
615
616 static HRESULT WINAPI Parser_OutputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
617 {
618     Parser_OutputPin *This = (Parser_OutputPin *)iface;
619
620     TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
621
622     *ppv = NULL;
623
624     if (IsEqualIID(riid, &IID_IUnknown))
625         *ppv = iface;
626     else if (IsEqualIID(riid, &IID_IPin))
627         *ppv = iface;
628     else if (IsEqualIID(riid, &IID_IMediaSeeking))
629     {
630         return IBaseFilter_QueryInterface(This->pin.pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
631     }
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_Connect(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
663 {
664     Parser_OutputPin *This = (Parser_OutputPin *)iface;
665     ParserImpl *parser = (ParserImpl *)This->pin.pin.pinInfo.pFilter;
666
667     /* Set the allocator to our input pin's */
668     EnterCriticalSection(This->pin.pin.pCritSec);
669     This->alloc = parser->pInputPin->pAlloc;
670     LeaveCriticalSection(This->pin.pin.pCritSec);
671
672     return BaseOutputPinImpl_Connect(iface, pReceivePin, pmt);
673 }
674
675 static HRESULT WINAPI Parser_OutputPin_QueryAccept(IPin *iface, const AM_MEDIA_TYPE * pmt)
676 {
677     Parser_OutputPin *This = (Parser_OutputPin *)iface;
678
679     TRACE("()\n");
680     dump_AM_MEDIA_TYPE(pmt);
681
682     return (memcmp(This->pmt, pmt, sizeof(AM_MEDIA_TYPE)) == 0);
683 }
684
685 static const IPinVtbl Parser_OutputPin_Vtbl = 
686 {
687     Parser_OutputPin_QueryInterface,
688     BasePinImpl_AddRef,
689     Parser_OutputPin_Release,
690     Parser_OutputPin_Connect,
691     BaseOutputPinImpl_ReceiveConnection,
692     BaseOutputPinImpl_Disconnect,
693     BasePinImpl_ConnectedTo,
694     BasePinImpl_ConnectionMediaType,
695     BasePinImpl_QueryPinInfo,
696     BasePinImpl_QueryDirection,
697     BasePinImpl_QueryId,
698     Parser_OutputPin_QueryAccept,
699     BasePinImpl_EnumMediaTypes,
700     BasePinImpl_QueryInternalConnections,
701     BaseOutputPinImpl_EndOfStream,
702     BaseOutputPinImpl_BeginFlush,
703     BaseOutputPinImpl_EndFlush,
704     BasePinImpl_NewSegment
705 };
706
707 static HRESULT WINAPI Parser_PullPin_Disconnect(IPin * iface)
708 {
709     HRESULT hr;
710     PullPin *This = (PullPin *)iface;
711
712     TRACE("()\n");
713
714     EnterCriticalSection(&This->thread_lock);
715     EnterCriticalSection(This->pin.pCritSec);
716     {
717         if (This->pin.pConnectedTo)
718         {
719             FILTER_STATE state;
720             ParserImpl *Parser = (ParserImpl *)This->pin.pinInfo.pFilter;
721
722             LeaveCriticalSection(This->pin.pCritSec);
723             hr = IBaseFilter_GetState(This->pin.pinInfo.pFilter, INFINITE, &state);
724             EnterCriticalSection(This->pin.pCritSec);
725
726             if (SUCCEEDED(hr) && (state == State_Stopped) && SUCCEEDED(Parser->fnDisconnect(Parser)))
727             {
728                 LeaveCriticalSection(This->pin.pCritSec);
729                 PullPin_Disconnect(iface);
730                 EnterCriticalSection(This->pin.pCritSec);
731                 hr = Parser_RemoveOutputPins((ParserImpl *)This->pin.pinInfo.pFilter);
732             }
733             else
734                 hr = VFW_E_NOT_STOPPED;
735         }
736         else
737             hr = S_FALSE;
738     }
739     LeaveCriticalSection(This->pin.pCritSec);
740     LeaveCriticalSection(&This->thread_lock);
741
742     return hr;
743 }
744
745 static HRESULT WINAPI Parser_PullPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
746 {
747     HRESULT hr;
748
749     TRACE("()\n");
750
751     hr = PullPin_ReceiveConnection(iface, pReceivePin, pmt);
752     if (FAILED(hr))
753     {
754         BasePin *This = (BasePin *)iface;
755
756         EnterCriticalSection(This->pCritSec);
757         Parser_RemoveOutputPins((ParserImpl *)This->pinInfo.pFilter);
758         LeaveCriticalSection(This->pCritSec);
759     }
760
761     return hr;
762 }
763
764 static const IPinVtbl Parser_InputPin_Vtbl =
765 {
766     PullPin_QueryInterface,
767     BasePinImpl_AddRef,
768     PullPin_Release,
769     BaseInputPinImpl_Connect,
770     Parser_PullPin_ReceiveConnection,
771     Parser_PullPin_Disconnect,
772     BasePinImpl_ConnectedTo,
773     BasePinImpl_ConnectionMediaType,
774     BasePinImpl_QueryPinInfo,
775     BasePinImpl_QueryDirection,
776     BasePinImpl_QueryId,
777     PullPin_QueryAccept,
778     BasePinImpl_EnumMediaTypes,
779     BasePinImpl_QueryInternalConnections,
780     PullPin_EndOfStream,
781     PullPin_BeginFlush,
782     PullPin_EndFlush,
783     PullPin_NewSegment
784 };