netapi32: Remove unneeded cast.
[wine] / dlls / quartz / avisplit.c
1 /*
2  * AVI Splitter Filter
3  *
4  * Copyright 2003 Robert Shearman
5  * Copyright 2004-2005 Christian Costa
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21 /* FIXME:
22  * - we don't do anything with indices yet (we could use them when seeking)
23  * - we don't support multiple RIFF sections (i.e. large AVI files > 2Gb)
24  */
25
26 #include "quartz_private.h"
27 #include "control_private.h"
28 #include "pin.h"
29
30 #include "uuids.h"
31 #include "aviriff.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 #include "parser.h"
44
45 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
46
47 typedef struct AVISplitterImpl
48 {
49     ParserImpl Parser;
50     IMediaSample * pCurrentSample;
51     RIFFCHUNK CurrentChunk;
52     LONGLONG CurrentChunkOffset; /* in media time */
53     LONGLONG EndOfFile;
54     AVIMAINHEADER AviHeader;
55 } AVISplitterImpl;
56
57 static HRESULT AVISplitter_NextChunk(LONGLONG * pllCurrentChunkOffset, RIFFCHUNK * pCurrentChunk, const REFERENCE_TIME * tStart, const REFERENCE_TIME * tStop, const BYTE * pbSrcStream, int inner)
58 {
59     if (inner)
60         *pllCurrentChunkOffset += MEDIATIME_FROM_BYTES(sizeof(RIFFLIST));
61     else
62         *pllCurrentChunkOffset += MEDIATIME_FROM_BYTES(sizeof(RIFFCHUNK) + RIFFROUND(pCurrentChunk->cb));
63     
64     if (*pllCurrentChunkOffset >= *tStop)
65         return S_FALSE; /* no more data - we couldn't even get the next chunk header! */
66     else if (*pllCurrentChunkOffset + MEDIATIME_FROM_BYTES(sizeof(RIFFCHUNK)) >= *tStop)
67     {
68         memcpy(pCurrentChunk, pbSrcStream + (DWORD)BYTES_FROM_MEDIATIME(*pllCurrentChunkOffset - *tStart), (DWORD)BYTES_FROM_MEDIATIME(*tStop - *pllCurrentChunkOffset));
69         return S_FALSE; /* no more data */
70     }
71     else
72         memcpy(pCurrentChunk, pbSrcStream + (DWORD)BYTES_FROM_MEDIATIME(*pllCurrentChunkOffset - *tStart), sizeof(RIFFCHUNK));
73
74     return S_OK;
75 }
76
77 static HRESULT AVISplitter_Sample(LPVOID iface, IMediaSample * pSample)
78 {
79     AVISplitterImpl *This = (AVISplitterImpl *)iface;
80     LPBYTE pbSrcStream = NULL;
81     long cbSrcStream = 0;
82     REFERENCE_TIME tStart, tStop;
83     HRESULT hr;
84     BOOL bMoreData = TRUE;
85
86     hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
87
88     hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
89
90     cbSrcStream = IMediaSample_GetActualDataLength(pSample);
91
92     /* trace removed for performance reasons */
93     /* TRACE("(%p)\n", pSample); */
94
95     assert(BYTES_FROM_MEDIATIME(tStop - tStart) == cbSrcStream);
96
97     if (This->CurrentChunkOffset <= tStart && This->CurrentChunkOffset + MEDIATIME_FROM_BYTES(sizeof(RIFFCHUNK)) > tStart)
98     {
99         DWORD offset = (DWORD)BYTES_FROM_MEDIATIME(tStart - This->CurrentChunkOffset);
100         assert(offset <= sizeof(RIFFCHUNK));
101         memcpy((BYTE *)&This->CurrentChunk + offset, pbSrcStream, sizeof(RIFFCHUNK) - offset);
102     }
103     else if (This->CurrentChunkOffset > tStart)
104     {
105         DWORD offset = (DWORD)BYTES_FROM_MEDIATIME(This->CurrentChunkOffset - tStart);
106         if (offset >= (DWORD)cbSrcStream)
107         {
108             FIXME("large offset\n");
109             hr = S_OK;
110             goto skip;
111         }
112
113         memcpy(&This->CurrentChunk, pbSrcStream + offset, sizeof(RIFFCHUNK));
114     }
115
116     assert(This->CurrentChunkOffset + MEDIATIME_FROM_BYTES(sizeof(RIFFCHUNK)) < tStop);
117
118     while (bMoreData)
119     {
120         BYTE * pbDstStream;
121         long cbDstStream;
122         long chunk_remaining_bytes = 0;
123         long offset_src;
124         WORD streamId;
125         Parser_OutputPin * pOutputPin;
126         BOOL bSyncPoint = TRUE;
127
128         if (This->CurrentChunkOffset >= tStart)
129             offset_src = (long)BYTES_FROM_MEDIATIME(This->CurrentChunkOffset - tStart) + sizeof(RIFFCHUNK);
130         else
131             offset_src = 0;
132
133         switch (This->CurrentChunk.fcc)
134         {
135         case ckidJUNK:
136         case aviFCC('i','d','x','1'): /* Index is not handled */
137             /* silently ignore */
138             if (S_FALSE == AVISplitter_NextChunk(&This->CurrentChunkOffset, &This->CurrentChunk, &tStart, &tStop, pbSrcStream, FALSE))
139                 bMoreData = FALSE;
140             continue;
141         case ckidLIST:
142             /* We only handle the 'rec ' list which contains the stream data */
143             if ((*(DWORD*)(pbSrcStream + BYTES_FROM_MEDIATIME(This->CurrentChunkOffset-tStart) + sizeof(RIFFCHUNK))) == aviFCC('r','e','c',' '))
144             {
145                 /* FIXME: We only advanced to the first chunk inside the list without keeping track that we are in it.
146                  *        This is not clean and the parser should be improved for that but it is enough for most AVI files. */
147                 if (S_FALSE == AVISplitter_NextChunk(&This->CurrentChunkOffset, &This->CurrentChunk, &tStart, &tStop, pbSrcStream, TRUE))
148                 {
149                     bMoreData = FALSE;
150                     continue;
151                 }
152                 This->CurrentChunk = *(RIFFCHUNK*) (pbSrcStream + BYTES_FROM_MEDIATIME(This->CurrentChunkOffset-tStart));
153                 offset_src = (long)BYTES_FROM_MEDIATIME(This->CurrentChunkOffset - tStart) + sizeof(RIFFCHUNK);
154                 break;
155             }
156             else if (S_FALSE == AVISplitter_NextChunk(&This->CurrentChunkOffset, &This->CurrentChunk, &tStart, &tStop, pbSrcStream, FALSE))
157                 bMoreData = FALSE;
158             continue;
159         default:
160             break;
161 #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 */
162             switch (TWOCCFromFOURCC(This->CurrentChunk.fcc))
163             {
164             case cktypeDIBcompressed:
165                 bSyncPoint = FALSE;
166                 /* fall-through */
167             case cktypeDIBbits:
168                 /* FIXME: check that pin is of type video */
169                 break;
170             case cktypeWAVEbytes:
171                 /* FIXME: check that pin is of type audio */
172                 break;
173             case cktypePALchange:
174                 FIXME("handle palette change\n");
175                 break;
176             default:
177                 FIXME("Skipping unknown chunk type: %s at file offset 0x%x\n", debugstr_an((LPSTR)&This->CurrentChunk.fcc, 4), (DWORD)BYTES_FROM_MEDIATIME(This->CurrentChunkOffset));
178                 if (S_FALSE == AVISplitter_NextChunk(&This->CurrentChunkOffset, &This->CurrentChunk, &tStart, &tStop, pbSrcStream, FALSE))
179                     bMoreData = FALSE;
180                 continue;
181             }
182 #endif
183         }
184
185         streamId = StreamFromFOURCC(This->CurrentChunk.fcc);
186
187         if (streamId > This->Parser.cStreams)
188         {
189             ERR("Corrupted AVI file (contains stream id %d, but supposed to only have %d streams)\n", streamId, This->Parser.cStreams);
190             hr = E_FAIL;
191             break;
192         }
193
194         pOutputPin = (Parser_OutputPin *)This->Parser.ppPins[streamId + 1];
195
196         if (!This->pCurrentSample)
197         {
198             /* cache media sample until it is ready to be despatched
199              * (i.e. we reach the end of the chunk) */
200             hr = OutputPin_GetDeliveryBuffer(&pOutputPin->pin, &This->pCurrentSample, NULL, NULL, 0);
201
202             if (SUCCEEDED(hr))
203             {
204                 hr = IMediaSample_SetActualDataLength(This->pCurrentSample, 0);
205                 assert(hr == S_OK);
206             }
207             else
208             {
209                 TRACE("Skipping sending sample for stream %02d due to error (%x)\n", streamId, hr);
210                 This->pCurrentSample = NULL;
211                 if (S_FALSE == AVISplitter_NextChunk(&This->CurrentChunkOffset, &This->CurrentChunk, &tStart, &tStop, pbSrcStream, FALSE))
212                     bMoreData = FALSE;
213                 continue;
214             }
215         }
216
217         hr = IMediaSample_GetPointer(This->pCurrentSample, &pbDstStream);
218
219         if (SUCCEEDED(hr))
220         {
221             cbDstStream = IMediaSample_GetSize(This->pCurrentSample);
222
223             chunk_remaining_bytes = (long)BYTES_FROM_MEDIATIME(This->CurrentChunkOffset + MEDIATIME_FROM_BYTES(This->CurrentChunk.cb + sizeof(RIFFCHUNK)) - tStart) - offset_src;
224         
225             assert(chunk_remaining_bytes >= 0);
226             assert(chunk_remaining_bytes <= cbDstStream - IMediaSample_GetActualDataLength(This->pCurrentSample));
227
228             /* trace removed for performance reasons */
229 /*          TRACE("chunk_remaining_bytes: 0x%x, cbSrcStream: 0x%x, offset_src: 0x%x\n", chunk_remaining_bytes, cbSrcStream, offset_src); */
230         }
231
232         if (chunk_remaining_bytes <= cbSrcStream - offset_src)
233         {
234             if (SUCCEEDED(hr))
235             {
236                 memcpy(pbDstStream + IMediaSample_GetActualDataLength(This->pCurrentSample), pbSrcStream + offset_src, chunk_remaining_bytes);
237                 hr = IMediaSample_SetActualDataLength(This->pCurrentSample, chunk_remaining_bytes + IMediaSample_GetActualDataLength(This->pCurrentSample));
238                 assert(hr == S_OK);
239             }
240
241             if (SUCCEEDED(hr))
242             {
243                 REFERENCE_TIME tAviStart, tAviStop;
244
245                 /* FIXME: hack */
246                 if (pOutputPin->dwSamplesProcessed == 0)
247                     IMediaSample_SetDiscontinuity(This->pCurrentSample, TRUE);
248
249                 IMediaSample_SetSyncPoint(This->pCurrentSample, bSyncPoint);
250
251                 pOutputPin->dwSamplesProcessed++;
252
253                 if (pOutputPin->dwSampleSize)
254                     tAviStart = (LONGLONG)ceil(10000000.0 * (float)(pOutputPin->dwSamplesProcessed - 1) * (float)IMediaSample_GetActualDataLength(This->pCurrentSample) / ((float)pOutputPin->dwSampleSize * pOutputPin->fSamplesPerSec));
255                 else
256                     tAviStart = (LONGLONG)ceil(10000000.0 * (float)(pOutputPin->dwSamplesProcessed - 1) / (float)pOutputPin->fSamplesPerSec);
257                 if (pOutputPin->dwSampleSize)
258                     tAviStop = (LONGLONG)ceil(10000000.0 * (float)pOutputPin->dwSamplesProcessed * (float)IMediaSample_GetActualDataLength(This->pCurrentSample) / ((float)pOutputPin->dwSampleSize * pOutputPin->fSamplesPerSec));
259                 else
260                     tAviStop = (LONGLONG)ceil(10000000.0 * (float)pOutputPin->dwSamplesProcessed / (float)pOutputPin->fSamplesPerSec);
261
262                 IMediaSample_SetTime(This->pCurrentSample, &tAviStart, &tAviStop);
263
264                 hr = OutputPin_SendSample(&pOutputPin->pin, This->pCurrentSample);
265                 if (hr != S_OK && hr != VFW_E_NOT_CONNECTED)
266                     ERR("Error sending sample (%x)\n", hr);
267             }
268
269             if (This->pCurrentSample)
270                 IMediaSample_Release(This->pCurrentSample);
271             
272             This->pCurrentSample = NULL;
273
274             if (S_FALSE == AVISplitter_NextChunk(&This->CurrentChunkOffset, &This->CurrentChunk, &tStart, &tStop, pbSrcStream, FALSE))
275                 bMoreData = FALSE;
276         }
277         else
278         {
279             if (SUCCEEDED(hr))
280             {
281                 memcpy(pbDstStream + IMediaSample_GetActualDataLength(This->pCurrentSample), pbSrcStream + offset_src, cbSrcStream - offset_src);
282                 IMediaSample_SetActualDataLength(This->pCurrentSample, cbSrcStream - offset_src + IMediaSample_GetActualDataLength(This->pCurrentSample));
283             }
284             bMoreData = FALSE;
285         }
286     }
287
288 skip:
289     if (tStop >= This->EndOfFile)
290     {
291         int i;
292
293         TRACE("End of file reached\n");
294
295         for (i = 0; i < This->Parser.cStreams; i++)
296         {
297             IPin* ppin;
298             HRESULT hr;
299
300             TRACE("Send End Of Stream to output pin %d\n", i);
301
302             hr = IPin_ConnectedTo(This->Parser.ppPins[i+1], &ppin);
303             if (SUCCEEDED(hr))
304             {
305                 hr = IPin_EndOfStream(ppin);
306                 IPin_Release(ppin);
307             }
308             if (FAILED(hr))
309             {
310                 ERR("%x\n", hr);
311                 break;
312             }
313         }
314
315         /* Force the pullpin thread to stop */
316         hr = S_FALSE;
317     }
318
319     return hr;
320 }
321
322 static HRESULT AVISplitter_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
323 {
324     if (IsEqualIID(&pmt->majortype, &MEDIATYPE_Stream) && IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_Avi))
325         return S_OK;
326     return S_FALSE;
327 }
328
329 static HRESULT AVISplitter_ProcessStreamList(AVISplitterImpl * This, const BYTE * pData, DWORD cb)
330 {
331     PIN_INFO piOutput;
332     const RIFFCHUNK * pChunk;
333     HRESULT hr;
334     AM_MEDIA_TYPE amt;
335     float fSamplesPerSec = 0.0f;
336     DWORD dwSampleSize = 0;
337     DWORD dwLength = 0;
338     ALLOCATOR_PROPERTIES props;
339     static const WCHAR wszStreamTemplate[] = {'S','t','r','e','a','m',' ','%','0','2','d',0};
340
341     props.cbAlign = 1;
342     props.cbPrefix = 0;
343     props.cbBuffer = 0x20000;
344     props.cBuffers = 2;
345     
346     ZeroMemory(&amt, sizeof(amt));
347     piOutput.dir = PINDIR_OUTPUT;
348     piOutput.pFilter = (IBaseFilter *)This;
349     wsprintfW(piOutput.achName, wszStreamTemplate, This->Parser.cStreams);
350
351     for (pChunk = (const RIFFCHUNK *)pData; 
352          ((const BYTE *)pChunk >= pData) && ((const BYTE *)pChunk + sizeof(RIFFCHUNK) < pData + cb) && (pChunk->cb > 0); 
353          pChunk = (const RIFFCHUNK *)((const BYTE*)pChunk + sizeof(RIFFCHUNK) + pChunk->cb)     
354         )
355     {
356         switch (pChunk->fcc)
357         {
358         case ckidSTREAMHEADER:
359             {
360                 const AVISTREAMHEADER * pStrHdr = (const AVISTREAMHEADER *)pChunk;
361                 TRACE("processing stream header\n");
362
363                 fSamplesPerSec = (float)pStrHdr->dwRate / (float)pStrHdr->dwScale;
364
365                 switch (pStrHdr->fccType)
366                 {
367                 case streamtypeVIDEO:
368                     memcpy(&amt.formattype, &FORMAT_VideoInfo, sizeof(GUID));
369                     amt.pbFormat = NULL;
370                     amt.cbFormat = 0;
371                     break;
372                 case streamtypeAUDIO:
373                     memcpy(&amt.formattype, &FORMAT_WaveFormatEx, sizeof(GUID));
374                     break;
375                 default:
376                     memcpy(&amt.formattype, &FORMAT_None, sizeof(GUID));
377                 }
378                 memcpy(&amt.majortype, &MEDIATYPE_Video, sizeof(GUID));
379                 amt.majortype.Data1 = pStrHdr->fccType;
380                 memcpy(&amt.subtype, &MEDIATYPE_Video, sizeof(GUID));
381                 amt.subtype.Data1 = pStrHdr->fccHandler;
382                 TRACE("Subtype FCC: %.04s\n", (LPCSTR)&pStrHdr->fccHandler);
383                 amt.lSampleSize = pStrHdr->dwSampleSize;
384                 amt.bFixedSizeSamples = (amt.lSampleSize != 0);
385
386                 /* FIXME: Is this right? */
387                 if (!amt.lSampleSize)
388                 {
389                     amt.lSampleSize = 1;
390                     dwSampleSize = 1;
391                 }
392
393                 amt.bTemporalCompression = IsEqualGUID(&amt.majortype, &MEDIATYPE_Video); /* FIXME? */
394                 dwSampleSize = pStrHdr->dwSampleSize;
395                 dwLength = pStrHdr->dwLength;
396                 if (!dwLength)
397                     dwLength = This->AviHeader.dwTotalFrames;
398
399                 if (pStrHdr->dwSuggestedBufferSize)
400                     props.cbBuffer = pStrHdr->dwSuggestedBufferSize;
401
402                 break;
403             }
404         case ckidSTREAMFORMAT:
405             TRACE("processing stream format data\n");
406             if (IsEqualIID(&amt.formattype, &FORMAT_VideoInfo))
407             {
408                 VIDEOINFOHEADER * pvi;
409                 /* biCompression member appears to override the value in the stream header.
410                  * i.e. the stream header can say something completely contradictory to what
411                  * is in the BITMAPINFOHEADER! */
412                 if (pChunk->cb < sizeof(BITMAPINFOHEADER))
413                 {
414                     ERR("Not enough bytes for BITMAPINFOHEADER\n");
415                     return E_FAIL;
416                 }
417                 amt.cbFormat = sizeof(VIDEOINFOHEADER) - sizeof(BITMAPINFOHEADER) + pChunk->cb;
418                 amt.pbFormat = CoTaskMemAlloc(amt.cbFormat);
419                 ZeroMemory(amt.pbFormat, amt.cbFormat);
420                 pvi = (VIDEOINFOHEADER *)amt.pbFormat;
421                 pvi->AvgTimePerFrame = (LONGLONG)(10000000.0 / fSamplesPerSec);
422                 CopyMemory(&pvi->bmiHeader, (const BYTE *)(pChunk + 1), pChunk->cb);
423                 if (pvi->bmiHeader.biCompression)
424                     amt.subtype.Data1 = pvi->bmiHeader.biCompression;
425             }
426             else
427             {
428                 amt.cbFormat = pChunk->cb;
429                 amt.pbFormat = CoTaskMemAlloc(amt.cbFormat);
430                 CopyMemory(amt.pbFormat, (const BYTE *)(pChunk + 1), amt.cbFormat);
431             }
432             break;
433         case ckidSTREAMNAME:
434             TRACE("processing stream name\n");
435             /* FIXME: this doesn't exactly match native version (we omit the "##)" prefix), but hey... */
436             MultiByteToWideChar(CP_ACP, 0, (LPCSTR)(pChunk + 1), pChunk->cb, piOutput.achName, sizeof(piOutput.achName) / sizeof(piOutput.achName[0]));
437             break;
438         case ckidSTREAMHANDLERDATA:
439             FIXME("process stream handler data\n");
440             break;
441         case ckidJUNK:
442             TRACE("JUNK chunk ignored\n");
443             break;
444         default:
445             FIXME("unknown chunk type \"%.04s\" ignored\n", (LPCSTR)&pChunk->fcc);
446         }
447     }
448
449     if (IsEqualGUID(&amt.formattype, &FORMAT_WaveFormatEx))
450     {
451         memcpy(&amt.subtype, &MEDIATYPE_Video, sizeof(GUID));
452         amt.subtype.Data1 = ((WAVEFORMATEX *)amt.pbFormat)->wFormatTag;
453     }
454
455     dump_AM_MEDIA_TYPE(&amt);
456     TRACE("fSamplesPerSec = %f\n", (double)fSamplesPerSec);
457     TRACE("dwSampleSize = %x\n", dwSampleSize);
458     TRACE("dwLength = %x\n", dwLength);
459
460     hr = Parser_AddPin(&(This->Parser), &piOutput, &props, &amt, fSamplesPerSec, dwSampleSize, dwLength);
461
462     return hr;
463 }
464
465 /* FIXME: fix leaks on failure here */
466 static HRESULT AVISplitter_InputPin_PreConnect(IPin * iface, IPin * pConnectPin)
467 {
468     PullPin *This = (PullPin *)iface;
469     HRESULT hr;
470     RIFFLIST list;
471     LONGLONG pos = 0; /* in bytes */
472     BYTE * pBuffer;
473     RIFFCHUNK * pCurrentChunk;
474     AVISplitterImpl * pAviSplit = (AVISplitterImpl *)This->pin.pinInfo.pFilter;
475
476     hr = IAsyncReader_SyncRead(This->pReader, pos, sizeof(list), (BYTE *)&list);
477     pos += sizeof(list);
478
479     if (list.fcc != ckidRIFF)
480     {
481         ERR("Input stream not a RIFF file\n");
482         return E_FAIL;
483     }
484     if (list.fccListType != ckidAVI)
485     {
486         ERR("Input stream not an AVI RIFF file\n");
487         return E_FAIL;
488     }
489
490     hr = IAsyncReader_SyncRead(This->pReader, pos, sizeof(list), (BYTE *)&list);
491     if (list.fcc != ckidLIST)
492     {
493         ERR("Expected LIST chunk, but got %.04s\n", (LPSTR)&list.fcc);
494         return E_FAIL;
495     }
496     if (list.fccListType != ckidHEADERLIST)
497     {
498         ERR("Header list expected. Got: %.04s\n", (LPSTR)&list.fccListType);
499         return E_FAIL;
500     }
501
502     pBuffer = HeapAlloc(GetProcessHeap(), 0, list.cb - sizeof(RIFFLIST) + sizeof(RIFFCHUNK));
503     hr = IAsyncReader_SyncRead(This->pReader, pos + sizeof(list), list.cb - sizeof(RIFFLIST) + sizeof(RIFFCHUNK), pBuffer);
504
505     pAviSplit->AviHeader.cb = 0;
506
507     for (pCurrentChunk = (RIFFCHUNK *)pBuffer; (BYTE *)pCurrentChunk + sizeof(*pCurrentChunk) < pBuffer + list.cb; pCurrentChunk = (RIFFCHUNK *)(((BYTE *)pCurrentChunk) + sizeof(*pCurrentChunk) + pCurrentChunk->cb))
508     {
509         RIFFLIST * pList;
510
511         switch (pCurrentChunk->fcc)
512         {
513         case ckidMAINAVIHEADER:
514             /* AVIMAINHEADER includes the structure that is pCurrentChunk at the moment */
515             memcpy(&pAviSplit->AviHeader, pCurrentChunk, sizeof(pAviSplit->AviHeader));
516             break;
517         case ckidLIST:
518             pList = (RIFFLIST *)pCurrentChunk;
519             switch (pList->fccListType)
520             {
521             case ckidSTREAMLIST:
522                 hr = AVISplitter_ProcessStreamList(pAviSplit, (BYTE *)pCurrentChunk + sizeof(RIFFLIST), pCurrentChunk->cb + sizeof(RIFFCHUNK) - sizeof(RIFFLIST));
523                 break;
524             case ckidODML:
525                 FIXME("process ODML header\n");
526                 break;
527             }
528             break;
529         case ckidJUNK:
530             /* ignore */
531             break;
532         default:
533             FIXME("unrecognised header list type: %.04s\n", (LPSTR)&pCurrentChunk->fcc);
534         }
535     }
536     HeapFree(GetProcessHeap(), 0, pBuffer);
537
538     if (pAviSplit->AviHeader.cb != sizeof(pAviSplit->AviHeader) - sizeof(RIFFCHUNK))
539     {
540         ERR("Avi Header wrong size!\n");
541         return E_FAIL;
542     }
543
544     pos += sizeof(RIFFCHUNK) + list.cb;
545     hr = IAsyncReader_SyncRead(This->pReader, pos, sizeof(list), (BYTE *)&list);
546
547     while (list.fcc == ckidJUNK || (list.fcc == ckidLIST && list.fccListType == ckidINFO))
548     {
549         pos += sizeof(RIFFCHUNK) + list.cb;
550         hr = IAsyncReader_SyncRead(This->pReader, pos, sizeof(list), (BYTE *)&list);
551     }
552
553     if (list.fcc != ckidLIST)
554     {
555         ERR("Expected LIST, but got %.04s\n", (LPSTR)&list.fcc);
556         return E_FAIL;
557     }
558     if (list.fccListType != ckidAVIMOVIE)
559     {
560         ERR("Expected AVI movie list, but got %.04s\n", (LPSTR)&list.fccListType);
561         return E_FAIL;
562     }
563
564     if (hr == S_OK)
565     {
566         pAviSplit->CurrentChunkOffset = MEDIATIME_FROM_BYTES(pos + sizeof(RIFFLIST));
567         pAviSplit->EndOfFile = MEDIATIME_FROM_BYTES(pos + list.cb + sizeof(RIFFLIST));
568         hr = IAsyncReader_SyncRead(This->pReader, BYTES_FROM_MEDIATIME(pAviSplit->CurrentChunkOffset), sizeof(pAviSplit->CurrentChunk), (BYTE *)&pAviSplit->CurrentChunk);
569     }
570
571     if (hr != S_OK)
572         return E_FAIL;
573
574     TRACE("AVI File ok\n");
575
576     return hr;
577 }
578
579 static HRESULT AVISplitter_Cleanup(LPVOID iface)
580 {
581     AVISplitterImpl *This = (AVISplitterImpl*)iface;
582
583     TRACE("(%p)->()\n", This);
584
585     if (This->pCurrentSample)
586         IMediaSample_Release(This->pCurrentSample);
587     This->pCurrentSample = NULL;
588
589     return S_OK;
590 }
591
592 HRESULT AVISplitter_create(IUnknown * pUnkOuter, LPVOID * ppv)
593 {
594     HRESULT hr;
595     AVISplitterImpl * This;
596
597     TRACE("(%p, %p)\n", pUnkOuter, ppv);
598
599     *ppv = NULL;
600
601     if (pUnkOuter)
602         return CLASS_E_NOAGGREGATION;
603
604     /* Note: This memory is managed by the transform filter once created */
605     This = CoTaskMemAlloc(sizeof(AVISplitterImpl));
606
607     This->pCurrentSample = NULL;
608
609     hr = Parser_Create(&(This->Parser), &CLSID_AviSplitter, AVISplitter_Sample, AVISplitter_QueryAccept, AVISplitter_InputPin_PreConnect, AVISplitter_Cleanup);
610
611     if (FAILED(hr))
612         return hr;
613
614     *ppv = (LPVOID)This;
615
616     return hr;
617 }