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