Removed W->A from DEFWND_ImmIsUIMessageW.
[wine] / dlls / quartz / avisplit.c
1 /*
2  * AVI Splitter Filter
3  *
4  * Copyright 2003 Robert Shearman
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 /* FIXME:
21  * - we don't do anything with indices yet (we could use them when seeking)
22  * - we don't support multiple RIFF sections (i.e. large AVI files > 2Gb)
23  */
24
25 #include "quartz_private.h"
26 #include "control_private.h"
27 #include "pin.h"
28
29 #include "uuids.h"
30 #include "aviriff.h"
31 #include "mmreg.h"
32 #include "vfwmsgs.h"
33 #include "amvideo.h"
34
35 #include "fourcc.h"
36
37 #include "wine/unicode.h"
38 #include "wine/debug.h"
39
40 #include <math.h>
41 #include <assert.h>
42
43 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
44
45 static const WCHAR wcsInputPinName[] = {'i','n','p','u','t',' ','p','i','n',0};
46 static const struct IBaseFilterVtbl AVISplitter_Vtbl;
47 static const struct IMediaSeekingVtbl AVISplitter_Seeking_Vtbl;
48 static const struct IPinVtbl AVISplitter_OutputPin_Vtbl;
49 static const struct IPinVtbl AVISplitter_InputPin_Vtbl;
50
51 static HRESULT AVISplitter_Sample(LPVOID iface, IMediaSample * pSample);
52 static HRESULT AVISplitter_OutputPin_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt);
53 static HRESULT AVISplitter_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt);
54 static HRESULT AVISplitter_InputPin_PreConnect(IPin * iface, IPin * pConnectPin);
55 static HRESULT AVISplitter_ChangeStart(LPVOID iface);
56 static HRESULT AVISplitter_ChangeStop(LPVOID iface);
57 static HRESULT AVISplitter_ChangeRate(LPVOID iface);
58
59 static HRESULT AVISplitter_InputPin_Construct(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin);
60
61 typedef struct AVISplitter
62 {
63     const IBaseFilterVtbl * lpVtbl;
64
65     ULONG refCount;
66     CRITICAL_SECTION csFilter;
67     FILTER_STATE state;
68     REFERENCE_TIME rtStreamStart;
69     IReferenceClock * pClock;
70     FILTER_INFO filterInfo;
71
72     PullPin * pInputPin;
73     ULONG cStreams;
74     IPin ** ppPins;
75     IMediaSample * pCurrentSample;
76     RIFFCHUNK CurrentChunk;
77     LONGLONG CurrentChunkOffset; /* in media time */
78     LONGLONG EndOfFile;
79     AVIMAINHEADER AviHeader;
80 } AVISplitter;
81
82 typedef struct AVISplitter_OutputPin
83 {
84     OutputPin pin;
85
86     AM_MEDIA_TYPE * pmt;
87     float fSamplesPerSec;
88     DWORD dwSamplesProcessed;
89     DWORD dwSampleSize;
90     DWORD dwLength;
91     MediaSeekingImpl mediaSeeking;
92 } AVISplitter_OutputPin;
93
94
95 #define _IMediaSeeking_Offset ((int)(&(((AVISplitter_OutputPin*)0)->mediaSeeking)))
96 #define ICOM_THIS_From_IMediaSeeking(impl, iface) impl* This = (impl*)(((char*)iface)-_IMediaSeeking_Offset);
97
98 HRESULT AVISplitter_create(IUnknown * pUnkOuter, LPVOID * ppv)
99 {
100     HRESULT hr;
101     PIN_INFO piInput;
102     AVISplitter * pAviSplit;
103
104     TRACE("(%p, %p)\n", pUnkOuter, ppv);
105
106     *ppv = NULL;
107
108     if (pUnkOuter)
109         return CLASS_E_NOAGGREGATION;
110     
111     pAviSplit = CoTaskMemAlloc(sizeof(AVISplitter));
112
113     pAviSplit->lpVtbl = &AVISplitter_Vtbl;
114     pAviSplit->refCount = 1;
115     InitializeCriticalSection(&pAviSplit->csFilter);
116     pAviSplit->state = State_Stopped;
117     pAviSplit->pClock = NULL;
118     pAviSplit->pCurrentSample = NULL;
119     ZeroMemory(&pAviSplit->filterInfo, sizeof(FILTER_INFO));
120
121     pAviSplit->cStreams = 0;
122     pAviSplit->ppPins = CoTaskMemAlloc(1 * sizeof(IPin *));
123
124     /* construct input pin */
125     piInput.dir = PINDIR_INPUT;
126     piInput.pFilter = (IBaseFilter *)pAviSplit;
127     strncpyW(piInput.achName, wcsInputPinName, sizeof(piInput.achName) / sizeof(piInput.achName[0]));
128
129     hr = AVISplitter_InputPin_Construct(&piInput, AVISplitter_Sample, (LPVOID)pAviSplit, AVISplitter_QueryAccept, &pAviSplit->csFilter, (IPin **)&pAviSplit->pInputPin);
130
131     if (SUCCEEDED(hr))
132     {
133         pAviSplit->ppPins[0] = (IPin *)pAviSplit->pInputPin;
134         pAviSplit->pInputPin->fnPreConnect = AVISplitter_InputPin_PreConnect;
135         *ppv = (LPVOID)pAviSplit;
136     }
137     else
138     {
139         CoTaskMemFree(pAviSplit->ppPins);
140         DeleteCriticalSection(&pAviSplit->csFilter);
141         CoTaskMemFree(pAviSplit);
142     }
143
144     return hr;
145 }
146
147 static HRESULT AVISplitter_OutputPin_Init(const PIN_INFO * pPinInfo, ALLOCATOR_PROPERTIES * props, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, const AM_MEDIA_TYPE * pmt, float fSamplesPerSec, LPCRITICAL_SECTION pCritSec, AVISplitter_OutputPin * pPinImpl)
148 {
149     pPinImpl->pmt = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
150     CopyMediaType(pPinImpl->pmt, pmt);
151     pPinImpl->dwSamplesProcessed = 0;
152     pPinImpl->dwSampleSize = 0;
153     pPinImpl->fSamplesPerSec = fSamplesPerSec;
154
155     MediaSeekingImpl_Init((LPVOID)pPinInfo->pFilter, AVISplitter_ChangeStop, AVISplitter_ChangeStart, AVISplitter_ChangeRate, &pPinImpl->mediaSeeking);
156     pPinImpl->mediaSeeking.lpVtbl = &AVISplitter_Seeking_Vtbl;
157
158     return OutputPin_Init(pPinInfo, props, pUserData, pQueryAccept, pCritSec, &pPinImpl->pin);
159 }
160
161 static HRESULT AVISplitter_OutputPin_Construct(const PIN_INFO * pPinInfo, ALLOCATOR_PROPERTIES * props, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, const AM_MEDIA_TYPE * pmt, float fSamplesPerSec, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
162 {
163     AVISplitter_OutputPin * pPinImpl;
164
165     *ppPin = NULL;
166
167     assert(pPinInfo->dir == PINDIR_OUTPUT);
168
169     pPinImpl = CoTaskMemAlloc(sizeof(AVISplitter_OutputPin));
170
171     if (!pPinImpl)
172         return E_OUTOFMEMORY;
173
174     if (SUCCEEDED(AVISplitter_OutputPin_Init(pPinInfo, props, pUserData, pQueryAccept, pmt, fSamplesPerSec, pCritSec, pPinImpl)))
175     {
176         pPinImpl->pin.pin.lpVtbl = &AVISplitter_OutputPin_Vtbl;
177         
178         *ppPin = (IPin *)pPinImpl;
179         return S_OK;
180     }
181     return E_FAIL;
182 }
183
184 static HRESULT WINAPI AVISplitter_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
185 {
186     AVISplitter *This = (AVISplitter *)iface;
187     TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
188
189     *ppv = NULL;
190
191     if (IsEqualIID(riid, &IID_IUnknown))
192         *ppv = (LPVOID)This;
193     else if (IsEqualIID(riid, &IID_IPersist))
194         *ppv = (LPVOID)This;
195     else if (IsEqualIID(riid, &IID_IMediaFilter))
196         *ppv = (LPVOID)This;
197     else if (IsEqualIID(riid, &IID_IBaseFilter))
198         *ppv = (LPVOID)This;
199
200     if (*ppv)
201     {
202         IUnknown_AddRef((IUnknown *)(*ppv));
203         return S_OK;
204     }
205
206     FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
207
208     return E_NOINTERFACE;
209 }
210
211 static ULONG WINAPI AVISplitter_AddRef(IBaseFilter * iface)
212 {
213     AVISplitter *This = (AVISplitter *)iface;
214     TRACE("()\n");
215     return InterlockedIncrement(&This->refCount);
216 }
217
218 static ULONG WINAPI AVISplitter_Release(IBaseFilter * iface)
219 {
220     AVISplitter *This = (AVISplitter *)iface;
221     TRACE("()\n");
222     if (!InterlockedDecrement(&This->refCount))
223     {
224         ULONG i;
225
226         DeleteCriticalSection(&This->csFilter);
227         IReferenceClock_Release(This->pClock);
228         
229         for (i = 0; i < This->cStreams + 1; i++)
230             IPin_Release(This->ppPins[i]);
231         
232         HeapFree(GetProcessHeap(), 0, This->ppPins);
233         This->lpVtbl = NULL;
234         
235         TRACE("Destroying AVI splitter\n");
236         CoTaskMemFree(This);
237         
238         return 0;
239     }
240     else
241         return This->refCount;
242 }
243
244 /** IPersist methods **/
245
246 static HRESULT WINAPI AVISplitter_GetClassID(IBaseFilter * iface, CLSID * pClsid)
247 {
248     TRACE("(%p)\n", pClsid);
249
250     *pClsid = CLSID_AviSplitter;
251
252     return S_OK;
253 }
254
255 /** IMediaFilter methods **/
256
257 static HRESULT WINAPI AVISplitter_Stop(IBaseFilter * iface)
258 {
259     HRESULT hr;
260     AVISplitter *This = (AVISplitter *)iface;
261
262     TRACE("()\n");
263
264     EnterCriticalSection(&This->csFilter);
265     {
266         hr = PullPin_StopProcessing(This->pInputPin);
267         This->state = State_Stopped;
268     }
269     LeaveCriticalSection(&This->csFilter);
270     
271     return hr;
272 }
273
274 static HRESULT WINAPI AVISplitter_Pause(IBaseFilter * iface)
275 {
276     HRESULT hr = S_OK;
277     BOOL bInit;
278     AVISplitter *This = (AVISplitter *)iface;
279     
280     TRACE("()\n");
281
282     EnterCriticalSection(&This->csFilter);
283     {
284         bInit = (This->state == State_Stopped);
285         This->state = State_Paused;
286     }
287     LeaveCriticalSection(&This->csFilter);
288
289     if (bInit)
290     {
291         unsigned int i;
292
293         hr = PullPin_Seek(This->pInputPin, This->CurrentChunkOffset, This->EndOfFile);
294
295         if (SUCCEEDED(hr))
296             hr = PullPin_InitProcessing(This->pInputPin);
297
298         if (SUCCEEDED(hr))
299         {
300             for (i = 1; i < This->cStreams + 1; i++)
301             {
302                 AVISplitter_OutputPin* StreamPin = (AVISplitter_OutputPin *)This->ppPins[i];
303                 OutputPin_DeliverNewSegment((OutputPin *)This->ppPins[i], 0, (LONGLONG)ceil(10000000.0 * (float)StreamPin->dwLength / StreamPin->fSamplesPerSec), 1.0);
304                 StreamPin->mediaSeeking.llDuration = (LONGLONG)ceil(10000000.0 * (float)StreamPin->dwLength / StreamPin->fSamplesPerSec);
305                 StreamPin->mediaSeeking.llStop = (LONGLONG)ceil(10000000.0 * (float)StreamPin->dwLength / StreamPin->fSamplesPerSec);
306                 OutputPin_CommitAllocator((OutputPin *)This->ppPins[i]);
307             }
308
309             /* FIXME: this is a little hacky: we have to deliver (at least?) one sample
310              * to each renderer before they will complete their transitions. We should probably
311              * seek through the stream for the first of each, rather than do it this way which is
312              * probably a bit prone to deadlocking */
313             hr = PullPin_StartProcessing(This->pInputPin);
314         }
315     }
316     /* FIXME: else pause thread */
317
318     return hr;
319 }
320
321 static HRESULT WINAPI AVISplitter_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
322 {
323     HRESULT hr = S_OK;
324     AVISplitter *This = (AVISplitter *)iface;
325
326     TRACE("(%s)\n", wine_dbgstr_longlong(tStart));
327
328     EnterCriticalSection(&This->csFilter);
329     {
330         This->rtStreamStart = tStart;
331         This->state = State_Running;
332     }
333     LeaveCriticalSection(&This->csFilter);
334
335     return hr;
336 }
337
338 static HRESULT WINAPI AVISplitter_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
339 {
340     AVISplitter *This = (AVISplitter *)iface;
341
342     TRACE("(%ld, %p)\n", dwMilliSecsTimeout, pState);
343
344     EnterCriticalSection(&This->csFilter);
345     {
346         *pState = This->state;
347     }
348     LeaveCriticalSection(&This->csFilter);
349
350     /* FIXME: this is a little bit unsafe, but I don't see that we can do this
351      * while in the critical section. Maybe we could copy the pointer and addref in the
352      * critical section and then release after this.
353      */
354     if (This->pInputPin && (PullPin_WaitForStateChange(This->pInputPin, dwMilliSecsTimeout) == S_FALSE))
355         return VFW_S_STATE_INTERMEDIATE;
356
357     return S_OK;
358 }
359
360 static HRESULT WINAPI AVISplitter_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
361 {
362     AVISplitter *This = (AVISplitter *)iface;
363
364     TRACE("(%p)\n", pClock);
365
366     EnterCriticalSection(&This->csFilter);
367     {
368         if (This->pClock)
369             IReferenceClock_Release(This->pClock);
370         This->pClock = pClock;
371         if (This->pClock)
372             IReferenceClock_AddRef(This->pClock);
373     }
374     LeaveCriticalSection(&This->csFilter);
375
376     return S_OK;
377 }
378
379 static HRESULT WINAPI AVISplitter_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
380 {
381     AVISplitter *This = (AVISplitter *)iface;
382
383     TRACE("(%p)\n", ppClock);
384
385     EnterCriticalSection(&This->csFilter);
386     {
387         *ppClock = This->pClock;
388         IReferenceClock_AddRef(This->pClock);
389     }
390     LeaveCriticalSection(&This->csFilter);
391     
392     return S_OK;
393 }
394
395 /** IBaseFilter implementation **/
396
397 static HRESULT WINAPI AVISplitter_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
398 {
399     ENUMPINDETAILS epd;
400     AVISplitter *This = (AVISplitter *)iface;
401
402     TRACE("(%p)\n", ppEnum);
403
404     epd.cPins = This->cStreams + 1; /* +1 for input pin */
405     epd.ppPins = This->ppPins;
406     return IEnumPinsImpl_Construct(&epd, ppEnum);
407 }
408
409 static HRESULT WINAPI AVISplitter_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
410 {
411     FIXME("AVISplitter::FindPin(...)\n");
412
413     /* FIXME: critical section */
414
415     return E_NOTIMPL;
416 }
417
418 static HRESULT WINAPI AVISplitter_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
419 {
420     AVISplitter *This = (AVISplitter *)iface;
421
422     TRACE("(%p)\n", pInfo);
423
424     strcpyW(pInfo->achName, This->filterInfo.achName);
425     pInfo->pGraph = This->filterInfo.pGraph;
426
427     if (pInfo->pGraph)
428         IFilterGraph_AddRef(pInfo->pGraph);
429     
430     return S_OK;
431 }
432
433 static HRESULT WINAPI AVISplitter_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
434 {
435     HRESULT hr = S_OK;
436     AVISplitter *This = (AVISplitter *)iface;
437
438     TRACE("(%p, %s)\n", pGraph, debugstr_w(pName));
439
440     EnterCriticalSection(&This->csFilter);
441     {
442         if (pName)
443             strcpyW(This->filterInfo.achName, pName);
444         else
445             *This->filterInfo.achName = '\0';
446         This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
447     }
448     LeaveCriticalSection(&This->csFilter);
449
450     return hr;
451 }
452
453 static HRESULT WINAPI AVISplitter_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
454 {
455     TRACE("(%p)\n", pVendorInfo);
456     return E_NOTIMPL;
457 }
458
459 static const IBaseFilterVtbl AVISplitter_Vtbl =
460 {
461     AVISplitter_QueryInterface,
462     AVISplitter_AddRef,
463     AVISplitter_Release,
464     AVISplitter_GetClassID,
465     AVISplitter_Stop,
466     AVISplitter_Pause,
467     AVISplitter_Run,
468     AVISplitter_GetState,
469     AVISplitter_SetSyncSource,
470     AVISplitter_GetSyncSource,
471     AVISplitter_EnumPins,
472     AVISplitter_FindPin,
473     AVISplitter_QueryFilterInfo,
474     AVISplitter_JoinFilterGraph,
475     AVISplitter_QueryVendorInfo
476 };
477
478 static HRESULT AVISplitter_NextChunk(LONGLONG * pllCurrentChunkOffset, RIFFCHUNK * pCurrentChunk, const REFERENCE_TIME * tStart, const REFERENCE_TIME * tStop, const BYTE * pbSrcStream)
479 {
480     *pllCurrentChunkOffset += MEDIATIME_FROM_BYTES(sizeof(RIFFCHUNK) + RIFFROUND(pCurrentChunk->cb));
481
482     if (*pllCurrentChunkOffset > *tStop)
483         return S_FALSE; /* no more data - we couldn't even get the next chunk header! */
484     else if (*pllCurrentChunkOffset + MEDIATIME_FROM_BYTES(sizeof(RIFFCHUNK)) >= *tStop)
485     {
486         memcpy(pCurrentChunk, pbSrcStream + (DWORD)BYTES_FROM_MEDIATIME(*pllCurrentChunkOffset - *tStart), (DWORD)BYTES_FROM_MEDIATIME(*tStop - *pllCurrentChunkOffset));
487         return S_FALSE; /* no more data */
488     }
489     else
490         memcpy(pCurrentChunk, pbSrcStream + (DWORD)BYTES_FROM_MEDIATIME(*pllCurrentChunkOffset - *tStart), sizeof(RIFFCHUNK));
491
492     return S_OK;
493 }
494
495 static HRESULT AVISplitter_Sample(LPVOID iface, IMediaSample * pSample)
496 {
497     AVISplitter *This = (AVISplitter *)iface;
498     LPBYTE pbSrcStream = NULL;
499     long cbSrcStream = 0;
500     REFERENCE_TIME tStart, tStop;
501     HRESULT hr;
502     BOOL bMoreData = TRUE;
503
504     hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
505
506     hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
507
508     cbSrcStream = IMediaSample_GetActualDataLength(pSample);
509
510     /* trace removed for performance reasons */
511 /*  TRACE("(%p)\n", pSample); */
512
513     assert(BYTES_FROM_MEDIATIME(tStop - tStart) == cbSrcStream);
514
515     if (This->CurrentChunkOffset <= tStart && This->CurrentChunkOffset + MEDIATIME_FROM_BYTES(sizeof(RIFFCHUNK)) > tStart)
516     {
517         DWORD offset = (DWORD)BYTES_FROM_MEDIATIME(tStart - This->CurrentChunkOffset);
518         assert(offset <= sizeof(RIFFCHUNK));
519         memcpy((BYTE *)&This->CurrentChunk + offset, pbSrcStream, sizeof(RIFFCHUNK) - offset);
520     }
521     else if (This->CurrentChunkOffset > tStart)
522     {
523         DWORD offset = (DWORD)BYTES_FROM_MEDIATIME(This->CurrentChunkOffset - tStart);
524         if (offset >= (DWORD)cbSrcStream)
525         {
526             FIXME("large offset\n");
527             return S_OK;
528         }
529
530         memcpy(&This->CurrentChunk, pbSrcStream + offset, sizeof(RIFFCHUNK));
531     }
532
533     assert(This->CurrentChunkOffset + MEDIATIME_FROM_BYTES(sizeof(RIFFCHUNK)) < tStop);
534
535     while (bMoreData)
536     {
537         BYTE * pbDstStream;
538         long cbDstStream;
539         long chunk_remaining_bytes = 0;
540         long offset_src;
541         WORD streamId;
542         AVISplitter_OutputPin * pOutputPin;
543         BOOL bSyncPoint = TRUE;
544
545         if (This->CurrentChunkOffset >= tStart)
546             offset_src = (long)BYTES_FROM_MEDIATIME(This->CurrentChunkOffset - tStart) + sizeof(RIFFCHUNK);
547         else
548             offset_src = 0;
549
550         switch (This->CurrentChunk.fcc)
551         {
552         case ckidJUNK:
553         case aviFCC('i','d','x','1'): /* Index is not handled */
554             /* silently ignore */
555             if (S_FALSE == AVISplitter_NextChunk(&This->CurrentChunkOffset, &This->CurrentChunk, &tStart, &tStop, pbSrcStream))
556                 bMoreData = FALSE;
557             continue;
558         case ckidLIST:
559             /* We only handle the 'rec ' list which contains the stream data */
560             if ((*(DWORD*)(pbSrcStream + BYTES_FROM_MEDIATIME(This->CurrentChunkOffset-tStart) + sizeof(RIFFCHUNK))) == aviFCC('r','e','c',' '))
561             {
562                 /* FIXME: We only advanced to the first chunk inside the list without keeping track that we are in it.
563                  *        This is not clean and the parser should be improved for that but it is enough for most AVI files. */
564                 This->CurrentChunkOffset = MEDIATIME_FROM_BYTES(BYTES_FROM_MEDIATIME(This->CurrentChunkOffset) + sizeof(RIFFLIST));
565                 This->CurrentChunk = *(RIFFCHUNK*) (pbSrcStream + BYTES_FROM_MEDIATIME(This->CurrentChunkOffset-tStart));
566                 break;
567             }
568             else if (S_FALSE == AVISplitter_NextChunk(&This->CurrentChunkOffset, &This->CurrentChunk, &tStart, &tStop, pbSrcStream))
569                 bMoreData = FALSE;
570             continue;
571         default:
572             break;
573 #if 0 /* According to the AVI specs, a stream data chunk should be ABXX where AB is the stream number and X means don't care */
574             switch (TWOCCFromFOURCC(This->CurrentChunk.fcc))
575             {
576             case cktypeDIBcompressed:
577                 bSyncPoint = FALSE;
578                 /* fall-through */
579             case cktypeDIBbits:
580                 /* FIXME: check that pin is of type video */
581                 break;
582             case cktypeWAVEbytes:
583                 /* FIXME: check that pin is of type audio */
584                 break;
585             case cktypePALchange:
586                 FIXME("handle palette change\n");
587                 break;
588             default:
589                 FIXME("Skipping unknown chunk type: %s at file offset 0x%lx\n", debugstr_an((LPSTR)&This->CurrentChunk.fcc, 4), (DWORD)BYTES_FROM_MEDIATIME(This->CurrentChunkOffset));
590                 if (S_FALSE == AVISplitter_NextChunk(&This->CurrentChunkOffset, &This->CurrentChunk, &tStart, &tStop, pbSrcStream))
591                     bMoreData = FALSE;
592                 continue;
593             }
594 #endif
595         }
596
597         streamId = StreamFromFOURCC(This->CurrentChunk.fcc);
598
599         if (streamId > This->cStreams)
600         {
601             ERR("Corrupted AVI file (contains stream id %d, but supposed to only have %ld streams)\n", streamId, This->cStreams);
602             return E_FAIL;
603         }
604
605         pOutputPin = (AVISplitter_OutputPin *)This->ppPins[streamId + 1];
606
607         if (!This->pCurrentSample)
608         {
609             /* cache media sample until it is ready to be despatched
610              * (i.e. we reach the end of the chunk) */
611             hr = OutputPin_GetDeliveryBuffer(&pOutputPin->pin, &This->pCurrentSample, NULL, NULL, 0);
612
613             if (SUCCEEDED(hr))
614             {
615                 hr = IMediaSample_SetActualDataLength(This->pCurrentSample, 0);
616                 assert(hr == S_OK);
617             }
618             else
619             {
620                 TRACE("Skipping sending sample for stream %02d due to error (%lx)\n", streamId, hr);
621                 This->pCurrentSample = NULL;
622                 if (S_FALSE == AVISplitter_NextChunk(&This->CurrentChunkOffset, &This->CurrentChunk, &tStart, &tStop, pbSrcStream))
623                     bMoreData = FALSE;
624                 continue;
625             }
626         }
627
628         hr = IMediaSample_GetPointer(This->pCurrentSample, &pbDstStream);
629
630         if (SUCCEEDED(hr))
631         {
632             cbDstStream = IMediaSample_GetSize(This->pCurrentSample);
633
634             chunk_remaining_bytes = (long)BYTES_FROM_MEDIATIME(This->CurrentChunkOffset + MEDIATIME_FROM_BYTES(This->CurrentChunk.cb + sizeof(RIFFCHUNK)) - tStart) - offset_src;
635         
636             assert(chunk_remaining_bytes >= 0);
637             assert(chunk_remaining_bytes <= cbDstStream - IMediaSample_GetActualDataLength(This->pCurrentSample));
638
639             /* trace removed for performance reasons */
640 /*          TRACE("chunk_remaining_bytes: 0x%lx, cbSrcStream: 0x%lx, offset_src: 0x%lx\n", chunk_remaining_bytes, cbSrcStream, offset_src); */
641         }
642
643         if (chunk_remaining_bytes <= cbSrcStream - offset_src)
644         {
645             if (SUCCEEDED(hr))
646             {
647                 memcpy(pbDstStream + IMediaSample_GetActualDataLength(This->pCurrentSample), pbSrcStream + offset_src, chunk_remaining_bytes);
648                 hr = IMediaSample_SetActualDataLength(This->pCurrentSample, chunk_remaining_bytes + IMediaSample_GetActualDataLength(This->pCurrentSample));
649                 assert(hr == S_OK);
650             }
651
652             if (SUCCEEDED(hr))
653             {
654                 REFERENCE_TIME tAviStart, tAviStop;
655
656                 /* FIXME: hack */
657                 if (pOutputPin->dwSamplesProcessed == 0)
658                     IMediaSample_SetDiscontinuity(This->pCurrentSample, TRUE);
659
660                 IMediaSample_SetSyncPoint(This->pCurrentSample, bSyncPoint);
661
662                 pOutputPin->dwSamplesProcessed++;
663
664                 if (pOutputPin->dwSampleSize)
665                     tAviStart = (LONGLONG)ceil(10000000.0 * (float)(pOutputPin->dwSamplesProcessed - 1) * (float)IMediaSample_GetActualDataLength(This->pCurrentSample) / ((float)pOutputPin->dwSampleSize * pOutputPin->fSamplesPerSec));
666                 else
667                     tAviStart = (LONGLONG)ceil(10000000.0 * (float)(pOutputPin->dwSamplesProcessed - 1) / (float)pOutputPin->fSamplesPerSec);
668                 if (pOutputPin->dwSampleSize)
669                     tAviStop = (LONGLONG)ceil(10000000.0 * (float)pOutputPin->dwSamplesProcessed * (float)IMediaSample_GetActualDataLength(This->pCurrentSample) / ((float)pOutputPin->dwSampleSize * pOutputPin->fSamplesPerSec));
670                 else
671                     tAviStop = (LONGLONG)ceil(10000000.0 * (float)pOutputPin->dwSamplesProcessed / (float)pOutputPin->fSamplesPerSec);
672
673                 IMediaSample_SetTime(This->pCurrentSample, &tAviStart, &tAviStop);
674
675                 hr = OutputPin_SendSample(&pOutputPin->pin, This->pCurrentSample);
676                 if (hr != S_OK && hr != VFW_E_NOT_CONNECTED)
677                     ERR("Error sending sample (%lx)\n", hr);
678             }
679
680             /* If we have a sample that has not been delivered, release it */
681             if (FAILED(hr) && This->pCurrentSample)
682                 IMediaSample_Release(This->pCurrentSample);
683             
684             This->pCurrentSample = NULL;
685
686             if (S_FALSE == AVISplitter_NextChunk(&This->CurrentChunkOffset, &This->CurrentChunk, &tStart, &tStop, pbSrcStream))
687                 bMoreData = FALSE;
688         }
689         else
690         {
691             if (SUCCEEDED(hr))
692             {
693                 memcpy(pbDstStream + IMediaSample_GetActualDataLength(This->pCurrentSample), pbSrcStream + offset_src, cbSrcStream - offset_src);
694                 IMediaSample_SetActualDataLength(This->pCurrentSample, cbSrcStream - offset_src + IMediaSample_GetActualDataLength(This->pCurrentSample));
695             }
696             bMoreData = FALSE;
697         }
698     }
699     return hr;
700 }
701
702 static HRESULT AVISplitter_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
703 {
704     if (IsEqualIID(&pmt->majortype, &MEDIATYPE_Stream) && IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_Avi))
705         return S_OK;
706     return S_FALSE;
707 }
708
709 static HRESULT AVISplitter_ProcessStreamList(AVISplitter * This, const BYTE * pData, DWORD cb)
710 {
711     PIN_INFO piOutput;
712     RIFFCHUNK * pChunk;
713     IPin ** ppOldPins;
714     HRESULT hr;
715     AM_MEDIA_TYPE amt;
716     float fSamplesPerSec = 0.0f;
717     DWORD dwSampleSize = 0;
718     DWORD dwLength = 0;
719     ALLOCATOR_PROPERTIES props;
720     static const WCHAR wszStreamTemplate[] = {'S','t','r','e','a','m',' ','%','0','2','d',0};
721
722     props.cbAlign = 1;
723     props.cbPrefix = 0;
724     props.cbBuffer = 0x20000;
725     props.cBuffers = 2;
726     
727     ZeroMemory(&amt, sizeof(amt));
728     piOutput.dir = PINDIR_OUTPUT;
729     piOutput.pFilter = (IBaseFilter *)This;
730     wsprintfW(piOutput.achName, wszStreamTemplate, This->cStreams);
731
732     for (pChunk = (RIFFCHUNK *)pData; ((BYTE *)pChunk >= pData) && ((BYTE *)pChunk + sizeof(RIFFCHUNK) < pData + cb) && (pChunk->cb > 0); pChunk = (RIFFCHUNK *)((BYTE*)pChunk + sizeof(RIFFCHUNK) + pChunk->cb))
733     {
734         switch (pChunk->fcc)
735         {
736         case ckidSTREAMHEADER:
737             {
738                 const AVISTREAMHEADER * pStrHdr = (const AVISTREAMHEADER *)pChunk;
739                 TRACE("processing stream header\n");
740
741                 fSamplesPerSec = (float)pStrHdr->dwRate / (float)pStrHdr->dwScale;
742
743                 switch (pStrHdr->fccType)
744                 {
745                 case streamtypeVIDEO:
746                     memcpy(&amt.formattype, &FORMAT_VideoInfo, sizeof(GUID));
747                     amt.pbFormat = NULL;
748                     amt.cbFormat = 0;
749                     break;
750                 case streamtypeAUDIO:
751                     memcpy(&amt.formattype, &FORMAT_WaveFormatEx, sizeof(GUID));
752                     break;
753                 default:
754                     memcpy(&amt.formattype, &FORMAT_None, sizeof(GUID));
755                 }
756                 memcpy(&amt.majortype, &MEDIATYPE_Video, sizeof(GUID));
757                 amt.majortype.Data1 = pStrHdr->fccType;
758                 memcpy(&amt.subtype, &MEDIATYPE_Video, sizeof(GUID));
759                 amt.subtype.Data1 = pStrHdr->fccHandler;
760                 TRACE("Subtype FCC: %.04s\n", (LPSTR)&pStrHdr->fccHandler);
761                 amt.lSampleSize = pStrHdr->dwSampleSize;
762                 amt.bFixedSizeSamples = (amt.lSampleSize != 0);
763
764                 /* FIXME: Is this right? */
765                 if (!amt.lSampleSize)
766                 {
767                     amt.lSampleSize = 1;
768                     dwSampleSize = 1;
769                 }
770
771                 amt.bTemporalCompression = IsEqualGUID(&amt.majortype, &MEDIATYPE_Video); /* FIXME? */
772                 dwSampleSize = pStrHdr->dwSampleSize;
773                 dwLength = pStrHdr->dwLength;
774                 if (!dwLength)
775                     dwLength = This->AviHeader.dwTotalFrames;
776
777                 if (pStrHdr->dwSuggestedBufferSize)
778                     props.cbBuffer = pStrHdr->dwSuggestedBufferSize;
779
780                 break;
781             }
782         case ckidSTREAMFORMAT:
783             TRACE("processing stream format data\n");
784             if (IsEqualIID(&amt.formattype, &FORMAT_VideoInfo))
785             {
786                 VIDEOINFOHEADER * pvi;
787                 /* biCompression member appears to override the value in the stream header.
788                  * i.e. the stream header can say something completely contradictory to what
789                  * is in the BITMAPINFOHEADER! */
790                 if (pChunk->cb < sizeof(BITMAPINFOHEADER))
791                 {
792                     ERR("Not enough bytes for BITMAPINFOHEADER\n");
793                     return E_FAIL;
794                 }
795                 amt.cbFormat = sizeof(VIDEOINFOHEADER) - sizeof(BITMAPINFOHEADER) + pChunk->cb;
796                 amt.pbFormat = CoTaskMemAlloc(amt.cbFormat);
797                 ZeroMemory(amt.pbFormat, amt.cbFormat);
798                 pvi = (VIDEOINFOHEADER *)amt.pbFormat;
799                 pvi->AvgTimePerFrame = (LONGLONG)(10000000.0 / fSamplesPerSec);
800                 CopyMemory(&pvi->bmiHeader, (const BYTE *)(pChunk + 1), pChunk->cb);
801                 if (pvi->bmiHeader.biCompression)
802                     amt.subtype.Data1 = pvi->bmiHeader.biCompression;
803             }
804             else
805             {
806                 amt.cbFormat = pChunk->cb;
807                 amt.pbFormat = CoTaskMemAlloc(amt.cbFormat);
808                 CopyMemory(amt.pbFormat, (const BYTE *)(pChunk + 1), amt.cbFormat);
809             }
810             break;
811         case ckidSTREAMNAME:
812             TRACE("processing stream name\n");
813             /* FIXME: this doesn't exactly match native version (we omit the "##)" prefix), but hey... */
814             MultiByteToWideChar(CP_ACP, 0, (LPCSTR)(pChunk + 1), pChunk->cb, piOutput.achName, sizeof(piOutput.achName) / sizeof(piOutput.achName[0]));
815             break;
816         case ckidSTREAMHANDLERDATA:
817             FIXME("process stream handler data\n");
818             break;
819         case ckidJUNK:
820             TRACE("JUNK chunk ignored\n");
821             break;
822         default:
823             FIXME("unknown chunk type \"%.04s\" ignored\n", (LPSTR)&pChunk->fcc);
824         }
825     }
826
827     if (IsEqualGUID(&amt.formattype, &FORMAT_WaveFormatEx))
828     {
829         memcpy(&amt.subtype, &MEDIATYPE_Video, sizeof(GUID));
830         amt.subtype.Data1 = ((WAVEFORMATEX *)amt.pbFormat)->wFormatTag;
831     }
832
833     dump_AM_MEDIA_TYPE(&amt);
834     FIXME("fSamplesPerSec = %f\n", (double)fSamplesPerSec);
835     FIXME("dwSampleSize = %lx\n", dwSampleSize);
836     FIXME("dwLength = %lx\n", dwLength);
837
838     ppOldPins = This->ppPins;
839
840     This->ppPins = HeapAlloc(GetProcessHeap(), 0, (This->cStreams + 2) * sizeof(IPin *));
841     memcpy(This->ppPins, ppOldPins, (This->cStreams + 1) * sizeof(IPin *));
842
843     hr = AVISplitter_OutputPin_Construct(&piOutput, &props, NULL, AVISplitter_OutputPin_QueryAccept, &amt, fSamplesPerSec, &This->csFilter, This->ppPins + This->cStreams + 1);
844
845     if (SUCCEEDED(hr))
846     {
847         ((AVISplitter_OutputPin *)(This->ppPins[This->cStreams + 1]))->dwSampleSize = dwSampleSize;
848         ((AVISplitter_OutputPin *)(This->ppPins[This->cStreams + 1]))->dwLength = dwLength;
849         ((AVISplitter_OutputPin *)(This->ppPins[This->cStreams + 1]))->pin.pin.pUserData = (LPVOID)This->ppPins[This->cStreams + 1];
850         This->cStreams++;
851         HeapFree(GetProcessHeap(), 0, ppOldPins);
852     }
853     else
854     {
855         HeapFree(GetProcessHeap(), 0, This->ppPins);
856         This->ppPins = ppOldPins;
857         ERR("Failed with error %lx\n", hr);
858     }
859
860     return hr;
861 }
862
863 static HRESULT AVISplitter_RemoveOutputPins(AVISplitter * This)
864 {
865     /* NOTE: should be in critical section when calling this function */
866
867     ULONG i;
868     IPin ** ppOldPins = This->ppPins;
869
870     /* reduce the pin array down to 1 (just our input pin) */
871     This->ppPins = HeapAlloc(GetProcessHeap(), 0, sizeof(IPin *) * 1);
872     memcpy(This->ppPins, ppOldPins, sizeof(IPin *) * 1);
873
874     for (i = 0; i < This->cStreams; i++)
875     {
876         OutputPin_DeliverDisconnect((OutputPin *)ppOldPins[i + 1]);
877         IPin_Release(ppOldPins[i + 1]);
878     }
879
880     This->cStreams = 0;
881     HeapFree(GetProcessHeap(), 0, ppOldPins);
882
883     return S_OK;
884 }
885
886 /* FIXME: fix leaks on failure here */
887 static HRESULT AVISplitter_InputPin_PreConnect(IPin * iface, IPin * pConnectPin)
888 {
889     PullPin *This = (PullPin *)iface;
890     HRESULT hr;
891     RIFFLIST list;
892     LONGLONG pos = 0; /* in bytes */
893     BYTE * pBuffer;
894     RIFFCHUNK * pCurrentChunk;
895     AVISplitter * pAviSplit = (AVISplitter *)This->pin.pinInfo.pFilter;
896
897     hr = IAsyncReader_SyncRead(This->pReader, pos, sizeof(list), (BYTE *)&list);
898     pos += sizeof(list);
899
900     if (list.fcc != ckidRIFF)
901     {
902         ERR("Input stream not a RIFF file\n");
903         return E_FAIL;
904     }
905     if (list.cb > 1 * 1024 * 1024 * 1024) /* cannot be more than 1Gb in size */
906     {
907         ERR("Input stream violates RIFF spec\n");
908         return E_FAIL;
909     }
910     if (list.fccListType != ckidAVI)
911     {
912         ERR("Input stream not an AVI RIFF file\n");
913         return E_FAIL;
914     }
915
916     hr = IAsyncReader_SyncRead(This->pReader, pos, sizeof(list), (BYTE *)&list);
917     if (list.fcc != ckidLIST)
918     {
919         ERR("Expected LIST chunk, but got %.04s\n", (LPSTR)&list.fcc);
920         return E_FAIL;
921     }
922     if (list.fccListType != ckidHEADERLIST)
923     {
924         ERR("Header list expected. Got: %.04s\n", (LPSTR)&list.fccListType);
925         return E_FAIL;
926     }
927
928     pBuffer = HeapAlloc(GetProcessHeap(), 0, list.cb - sizeof(RIFFLIST) + sizeof(RIFFCHUNK));
929     hr = IAsyncReader_SyncRead(This->pReader, pos + sizeof(list), list.cb - sizeof(RIFFLIST) + sizeof(RIFFCHUNK), pBuffer);
930
931     pAviSplit->AviHeader.cb = 0;
932
933     for (pCurrentChunk = (RIFFCHUNK *)pBuffer; (BYTE *)pCurrentChunk + sizeof(*pCurrentChunk) < pBuffer + list.cb; pCurrentChunk = (RIFFCHUNK *)(((BYTE *)pCurrentChunk) + sizeof(*pCurrentChunk) + pCurrentChunk->cb))
934     {
935         RIFFLIST * pList;
936
937         switch (pCurrentChunk->fcc)
938         {
939         case ckidMAINAVIHEADER:
940             /* AVIMAINHEADER includes the structure that is pCurrentChunk at the moment */
941             memcpy(&pAviSplit->AviHeader, pCurrentChunk, sizeof(pAviSplit->AviHeader));
942             break;
943         case ckidLIST:
944             pList = (RIFFLIST *)pCurrentChunk;
945             switch (pList->fccListType)
946             {
947             case ckidSTREAMLIST:
948                 hr = AVISplitter_ProcessStreamList(pAviSplit, (BYTE *)pCurrentChunk + sizeof(RIFFLIST), pCurrentChunk->cb + sizeof(RIFFCHUNK) - sizeof(RIFFLIST));
949                 break;
950             case ckidODML:
951                 FIXME("process ODML header\n");
952                 break;
953             }
954             break;
955         case ckidJUNK:
956             /* ignore */
957             break;
958         default:
959             FIXME("unrecognised header list type: %.04s\n", (LPSTR)&pCurrentChunk->fcc);
960         }
961     }
962     HeapFree(GetProcessHeap(), 0, pBuffer);
963
964     if (pAviSplit->AviHeader.cb != sizeof(pAviSplit->AviHeader) - sizeof(RIFFCHUNK))
965     {
966         ERR("Avi Header wrong size!\n");
967         return E_FAIL;
968     }
969
970     pos += sizeof(RIFFCHUNK) + list.cb;
971     hr = IAsyncReader_SyncRead(This->pReader, pos, sizeof(list), (BYTE *)&list);
972
973     if (list.fcc == ckidJUNK)
974     {
975         pos += sizeof(RIFFCHUNK) + list.cb;
976         hr = IAsyncReader_SyncRead(This->pReader, pos, sizeof(list), (BYTE *)&list);
977     }
978
979     if (list.fcc != ckidLIST)
980     {
981         ERR("Expected LIST, but got %.04s\n", (LPSTR)&list.fcc);
982         return E_FAIL;
983     }
984     if (list.fccListType != ckidAVIMOVIE)
985     {
986         ERR("Expected AVI movie list, but got %.04s\n", (LPSTR)&list.fccListType);
987         return E_FAIL;
988     }
989
990     if (hr == S_OK)
991     {
992         pAviSplit->CurrentChunkOffset = MEDIATIME_FROM_BYTES(pos + sizeof(RIFFLIST));
993         pAviSplit->EndOfFile = MEDIATIME_FROM_BYTES(pos + list.cb + sizeof(RIFFLIST));
994         hr = IAsyncReader_SyncRead(This->pReader, BYTES_FROM_MEDIATIME(pAviSplit->CurrentChunkOffset), sizeof(pAviSplit->CurrentChunk), (BYTE *)&pAviSplit->CurrentChunk);
995     }
996
997     if (hr != S_OK)
998         return E_FAIL;
999
1000     TRACE("AVI File ok\n");
1001
1002     return hr;
1003 }
1004
1005 static HRESULT AVISplitter_ChangeStart(LPVOID iface)
1006 {
1007     FIXME("(%p)\n", iface);
1008     return S_OK;
1009 }
1010
1011 static HRESULT AVISplitter_ChangeStop(LPVOID iface)
1012 {
1013     FIXME("(%p)\n", iface);
1014     return S_OK;
1015 }
1016
1017 static HRESULT AVISplitter_ChangeRate(LPVOID iface)
1018 {
1019     FIXME("(%p)\n", iface);
1020     return S_OK;
1021 }
1022
1023
1024 static HRESULT WINAPI AVISplitter_Seeking_QueryInterface(IMediaSeeking * iface, REFIID riid, LPVOID * ppv)
1025 {
1026     ICOM_THIS_From_IMediaSeeking(AVISplitter_OutputPin, iface);
1027
1028     return IUnknown_QueryInterface((IUnknown *)This, riid, ppv);
1029 }
1030
1031 static ULONG WINAPI AVISplitter_Seeking_AddRef(IMediaSeeking * iface)
1032 {
1033     ICOM_THIS_From_IMediaSeeking(AVISplitter_OutputPin, iface);
1034
1035     return IUnknown_AddRef((IUnknown *)This);
1036 }
1037
1038 static ULONG WINAPI AVISplitter_Seeking_Release(IMediaSeeking * iface)
1039 {
1040     ICOM_THIS_From_IMediaSeeking(AVISplitter_OutputPin, iface);
1041
1042     return IUnknown_Release((IUnknown *)This);
1043 }
1044
1045 static const IMediaSeekingVtbl AVISplitter_Seeking_Vtbl =
1046 {
1047     AVISplitter_Seeking_QueryInterface,
1048     AVISplitter_Seeking_AddRef,
1049     AVISplitter_Seeking_Release,
1050     MediaSeekingImpl_GetCapabilities,
1051     MediaSeekingImpl_CheckCapabilities,
1052     MediaSeekingImpl_IsFormatSupported,
1053     MediaSeekingImpl_QueryPreferredFormat,
1054     MediaSeekingImpl_GetTimeFormat,
1055     MediaSeekingImpl_IsUsingTimeFormat,
1056     MediaSeekingImpl_SetTimeFormat,
1057     MediaSeekingImpl_GetDuration,
1058     MediaSeekingImpl_GetStopPosition,
1059     MediaSeekingImpl_GetCurrentPosition,
1060     MediaSeekingImpl_ConvertTimeFormat,
1061     MediaSeekingImpl_SetPositions,
1062     MediaSeekingImpl_GetPositions,
1063     MediaSeekingImpl_GetAvailable,
1064     MediaSeekingImpl_SetRate,
1065     MediaSeekingImpl_GetRate,
1066     MediaSeekingImpl_GetPreroll
1067 };
1068
1069 HRESULT WINAPI AVISplitter_OutputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
1070 {
1071     AVISplitter_OutputPin *This = (AVISplitter_OutputPin *)iface;
1072
1073     TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
1074
1075     *ppv = NULL;
1076
1077     if (IsEqualIID(riid, &IID_IUnknown))
1078         *ppv = (LPVOID)iface;
1079     else if (IsEqualIID(riid, &IID_IPin))
1080         *ppv = (LPVOID)iface;
1081     else if (IsEqualIID(riid, &IID_IMediaSeeking))
1082         *ppv = (LPVOID)&This->mediaSeeking;
1083
1084     if (*ppv)
1085     {
1086         IUnknown_AddRef((IUnknown *)(*ppv));
1087         return S_OK;
1088     }
1089
1090     FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
1091
1092     return E_NOINTERFACE;
1093 }
1094
1095 static ULONG WINAPI AVISplitter_OutputPin_Release(IPin * iface)
1096 {
1097     AVISplitter_OutputPin *This = (AVISplitter_OutputPin *)iface;
1098     
1099     TRACE("()\n");
1100     
1101     if (!InterlockedDecrement(&This->pin.pin.refCount))
1102     {
1103         DeleteMediaType(This->pmt);
1104         CoTaskMemFree(This->pmt);
1105         DeleteMediaType(&This->pin.pin.mtCurrent);
1106         CoTaskMemFree(This);
1107         return 0;
1108     }
1109     return This->pin.pin.refCount;
1110 }
1111
1112 static HRESULT WINAPI AVISplitter_OutputPin_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
1113 {
1114     ENUMMEDIADETAILS emd;
1115     AVISplitter_OutputPin *This = (AVISplitter_OutputPin *)iface;
1116
1117     TRACE("(%p)\n", ppEnum);
1118
1119     /* override this method to allow enumeration of your types */
1120     emd.cMediaTypes = 1;
1121     emd.pMediaTypes = This->pmt;
1122
1123     return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
1124 }
1125
1126 static HRESULT AVISplitter_OutputPin_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
1127 {
1128     AVISplitter_OutputPin *This = (AVISplitter_OutputPin *)iface;
1129
1130     TRACE("()\n");
1131     dump_AM_MEDIA_TYPE(pmt);
1132
1133     return (memcmp(This->pmt, pmt, sizeof(AM_MEDIA_TYPE)) == 0);
1134 }
1135
1136 static const IPinVtbl AVISplitter_OutputPin_Vtbl = 
1137 {
1138     AVISplitter_OutputPin_QueryInterface,
1139     IPinImpl_AddRef,
1140     AVISplitter_OutputPin_Release,
1141     OutputPin_Connect,
1142     OutputPin_ReceiveConnection,
1143     OutputPin_Disconnect,
1144     IPinImpl_ConnectedTo,
1145     IPinImpl_ConnectionMediaType,
1146     IPinImpl_QueryPinInfo,
1147     IPinImpl_QueryDirection,
1148     IPinImpl_QueryId,
1149     IPinImpl_QueryAccept,
1150     AVISplitter_OutputPin_EnumMediaTypes,
1151     IPinImpl_QueryInternalConnections,
1152     OutputPin_EndOfStream,
1153     OutputPin_BeginFlush,
1154     OutputPin_EndFlush,
1155     OutputPin_NewSegment
1156 };
1157
1158 static HRESULT AVISplitter_InputPin_Construct(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
1159 {
1160     PullPin * pPinImpl;
1161
1162     *ppPin = NULL;
1163
1164     if (pPinInfo->dir != PINDIR_INPUT)
1165     {
1166         ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
1167         return E_INVALIDARG;
1168     }
1169
1170     pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
1171
1172     if (!pPinImpl)
1173         return E_OUTOFMEMORY;
1174
1175     if (SUCCEEDED(PullPin_Init(pPinInfo, pSampleProc, pUserData, pQueryAccept, pCritSec, pPinImpl)))
1176     {
1177         pPinImpl->pin.lpVtbl = &AVISplitter_InputPin_Vtbl;
1178         
1179         *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
1180         return S_OK;
1181     }
1182     return E_FAIL;
1183 }
1184
1185 static HRESULT WINAPI AVISplitter_InputPin_Disconnect(IPin * iface)
1186 {
1187     HRESULT hr;
1188     IPinImpl *This = (IPinImpl *)iface;
1189
1190     TRACE("()\n");
1191
1192     EnterCriticalSection(This->pCritSec);
1193     {
1194         if (This->pConnectedTo)
1195         {
1196             FILTER_STATE state;
1197
1198             hr = IBaseFilter_GetState(This->pinInfo.pFilter, 0, &state);
1199
1200             if (SUCCEEDED(hr) && (state == State_Stopped))
1201             {
1202                 IPin_Release(This->pConnectedTo);
1203                 This->pConnectedTo = NULL;
1204                 hr = AVISplitter_RemoveOutputPins((AVISplitter *)This->pinInfo.pFilter);
1205             }
1206             else
1207                 hr = VFW_E_NOT_STOPPED;
1208         }
1209         else
1210             hr = S_FALSE;
1211     }
1212     LeaveCriticalSection(This->pCritSec);
1213     
1214     return hr;
1215 }
1216
1217 static const IPinVtbl AVISplitter_InputPin_Vtbl =
1218 {
1219     PullPin_QueryInterface,
1220     IPinImpl_AddRef,
1221     PullPin_Release,
1222     OutputPin_Connect,
1223     PullPin_ReceiveConnection,
1224     AVISplitter_InputPin_Disconnect,
1225     IPinImpl_ConnectedTo,
1226     IPinImpl_ConnectionMediaType,
1227     IPinImpl_QueryPinInfo,
1228     IPinImpl_QueryDirection,
1229     IPinImpl_QueryId,
1230     IPinImpl_QueryAccept,
1231     IPinImpl_EnumMediaTypes,
1232     IPinImpl_QueryInternalConnections,
1233     PullPin_EndOfStream,
1234     PullPin_BeginFlush,
1235     PullPin_EndFlush,
1236     PullPin_NewSegment
1237 };