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