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