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