quartz: Make sure cached media samples are released for parser filters.
[wine/gsoc_dplay.git] / dlls / quartz / avisplit.c
blobf3a6f6474841f10a1c555b3cc5a8491e064ddf39
1 /*
2 * AVI Splitter Filter
4 * Copyright 2003 Robert Shearman
5 * Copyright 2004-2005 Christian Costa
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.
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.
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
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)
26 #include "quartz_private.h"
27 #include "control_private.h"
28 #include "pin.h"
30 #include "uuids.h"
31 #include "aviriff.h"
32 #include "mmreg.h"
33 #include "vfwmsgs.h"
34 #include "amvideo.h"
36 #include "fourcc.h"
38 #include "wine/unicode.h"
39 #include "wine/debug.h"
41 #include <math.h>
42 #include <assert.h>
44 #include "parser.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
48 typedef struct AVISplitterImpl
50 ParserImpl Parser;
51 IMediaSample * pCurrentSample;
52 RIFFCHUNK CurrentChunk;
53 LONGLONG CurrentChunkOffset; /* in media time */
54 LONGLONG EndOfFile;
55 AVIMAINHEADER AviHeader;
56 } AVISplitterImpl;
58 static HRESULT AVISplitter_NextChunk(LONGLONG * pllCurrentChunkOffset, RIFFCHUNK * pCurrentChunk, const REFERENCE_TIME * tStart, const REFERENCE_TIME * tStop, const BYTE * pbSrcStream, int inner)
60 if (inner)
61 *pllCurrentChunkOffset += MEDIATIME_FROM_BYTES(sizeof(RIFFLIST));
62 else
63 *pllCurrentChunkOffset += MEDIATIME_FROM_BYTES(sizeof(RIFFCHUNK) + RIFFROUND(pCurrentChunk->cb));
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)
69 memcpy(pCurrentChunk, pbSrcStream + (DWORD)BYTES_FROM_MEDIATIME(*pllCurrentChunkOffset - *tStart), (DWORD)BYTES_FROM_MEDIATIME(*tStop - *pllCurrentChunkOffset));
70 return S_FALSE; /* no more data */
72 else
73 memcpy(pCurrentChunk, pbSrcStream + (DWORD)BYTES_FROM_MEDIATIME(*pllCurrentChunkOffset - *tStart), sizeof(RIFFCHUNK));
75 return S_OK;
78 static HRESULT AVISplitter_Sample(LPVOID iface, IMediaSample * pSample)
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;
87 hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
89 hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
91 cbSrcStream = IMediaSample_GetActualDataLength(pSample);
93 /* trace removed for performance reasons */
94 /* TRACE("(%p)\n", pSample); */
96 assert(BYTES_FROM_MEDIATIME(tStop - tStart) == cbSrcStream);
98 if (This->CurrentChunkOffset <= tStart && This->CurrentChunkOffset + MEDIATIME_FROM_BYTES(sizeof(RIFFCHUNK)) > tStart)
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);
104 else if (This->CurrentChunkOffset > tStart)
106 DWORD offset = (DWORD)BYTES_FROM_MEDIATIME(This->CurrentChunkOffset - tStart);
107 if (offset >= (DWORD)cbSrcStream)
109 FIXME("large offset\n");
110 hr = S_OK;
111 goto skip;
114 memcpy(&This->CurrentChunk, pbSrcStream + offset, sizeof(RIFFCHUNK));
117 assert(This->CurrentChunkOffset + MEDIATIME_FROM_BYTES(sizeof(RIFFCHUNK)) < tStop);
119 while (bMoreData)
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;
129 if (This->CurrentChunkOffset >= tStart)
130 offset_src = (long)BYTES_FROM_MEDIATIME(This->CurrentChunkOffset - tStart) + sizeof(RIFFCHUNK);
131 else
132 offset_src = 0;
134 switch (This->CurrentChunk.fcc)
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',' '))
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))
150 bMoreData = FALSE;
151 continue;
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;
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))
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%x\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;
183 #endif
186 streamId = StreamFromFOURCC(This->CurrentChunk.fcc);
188 if (streamId > This->Parser.cStreams)
190 ERR("Corrupted AVI file (contains stream id %d, but supposed to only have %d streams)\n", streamId, This->Parser.cStreams);
191 hr = E_FAIL;
192 break;
195 pOutputPin = (Parser_OutputPin *)This->Parser.ppPins[streamId + 1];
197 if (!This->pCurrentSample)
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);
203 if (SUCCEEDED(hr))
205 hr = IMediaSample_SetActualDataLength(This->pCurrentSample, 0);
206 assert(hr == S_OK);
208 else
210 TRACE("Skipping sending sample for stream %02d due to error (%x)\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;
218 hr = IMediaSample_GetPointer(This->pCurrentSample, &pbDstStream);
220 if (SUCCEEDED(hr))
222 cbDstStream = IMediaSample_GetSize(This->pCurrentSample);
224 chunk_remaining_bytes = (long)BYTES_FROM_MEDIATIME(This->CurrentChunkOffset + MEDIATIME_FROM_BYTES(This->CurrentChunk.cb + sizeof(RIFFCHUNK)) - tStart) - offset_src;
226 assert(chunk_remaining_bytes >= 0);
227 assert(chunk_remaining_bytes <= cbDstStream - IMediaSample_GetActualDataLength(This->pCurrentSample));
229 /* trace removed for performance reasons */
230 /* TRACE("chunk_remaining_bytes: 0x%x, cbSrcStream: 0x%x, offset_src: 0x%x\n", chunk_remaining_bytes, cbSrcStream, offset_src); */
233 if (chunk_remaining_bytes <= cbSrcStream - offset_src)
235 if (SUCCEEDED(hr))
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);
242 if (SUCCEEDED(hr))
244 REFERENCE_TIME tAviStart, tAviStop;
246 /* FIXME: hack */
247 if (pOutputPin->dwSamplesProcessed == 0)
248 IMediaSample_SetDiscontinuity(This->pCurrentSample, TRUE);
250 IMediaSample_SetSyncPoint(This->pCurrentSample, bSyncPoint);
252 pOutputPin->dwSamplesProcessed++;
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);
263 IMediaSample_SetTime(This->pCurrentSample, &tAviStart, &tAviStop);
265 hr = OutputPin_SendSample(&pOutputPin->pin, This->pCurrentSample);
266 if (hr != S_OK && hr != VFW_E_NOT_CONNECTED)
267 ERR("Error sending sample (%x)\n", hr);
270 if (This->pCurrentSample)
271 IMediaSample_Release(This->pCurrentSample);
273 This->pCurrentSample = NULL;
275 if (S_FALSE == AVISplitter_NextChunk(&This->CurrentChunkOffset, &This->CurrentChunk, &tStart, &tStop, pbSrcStream, FALSE))
276 bMoreData = FALSE;
278 else
280 if (SUCCEEDED(hr))
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));
285 bMoreData = FALSE;
289 skip:
290 if (tStop >= This->EndOfFile)
292 int i;
294 TRACE("End of file reached\n");
296 for (i = 0; i < This->Parser.cStreams; i++)
298 IPin* ppin;
299 HRESULT hr;
301 TRACE("Send End Of Stream to output pin %d\n", i);
303 hr = IPin_ConnectedTo(This->Parser.ppPins[i+1], &ppin);
304 if (SUCCEEDED(hr))
306 hr = IPin_EndOfStream(ppin);
307 IPin_Release(ppin);
309 if (FAILED(hr))
311 ERR("%x\n", hr);
312 break;
316 /* Force the pullpin thread to stop */
317 hr = S_FALSE;
320 return hr;
323 static HRESULT AVISplitter_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
325 if (IsEqualIID(&pmt->majortype, &MEDIATYPE_Stream) && IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_Avi))
326 return S_OK;
327 return S_FALSE;
330 static HRESULT AVISplitter_ProcessStreamList(AVISplitterImpl * This, const BYTE * pData, DWORD cb)
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};
342 props.cbAlign = 1;
343 props.cbPrefix = 0;
344 props.cbBuffer = 0x20000;
345 props.cBuffers = 2;
347 ZeroMemory(&amt, sizeof(amt));
348 piOutput.dir = PINDIR_OUTPUT;
349 piOutput.pFilter = (IBaseFilter *)This;
350 wsprintfW(piOutput.achName, wszStreamTemplate, This->Parser.cStreams);
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)
357 switch (pChunk->fcc)
359 case ckidSTREAMHEADER:
361 const AVISTREAMHEADER * pStrHdr = (const AVISTREAMHEADER *)pChunk;
362 TRACE("processing stream header\n");
364 fSamplesPerSec = (float)pStrHdr->dwRate / (float)pStrHdr->dwScale;
366 switch (pStrHdr->fccType)
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));
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);
387 /* FIXME: Is this right? */
388 if (!amt.lSampleSize)
390 amt.lSampleSize = 1;
391 dwSampleSize = 1;
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;
400 if (pStrHdr->dwSuggestedBufferSize)
401 props.cbBuffer = pStrHdr->dwSuggestedBufferSize;
403 break;
405 case ckidSTREAMFORMAT:
406 TRACE("processing stream format data\n");
407 if (IsEqualIID(&amt.formattype, &FORMAT_VideoInfo))
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))
415 ERR("Not enough bytes for BITMAPINFOHEADER\n");
416 return E_FAIL;
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;
427 else
429 amt.cbFormat = pChunk->cb;
430 amt.pbFormat = CoTaskMemAlloc(amt.cbFormat);
431 CopyMemory(amt.pbFormat, (const BYTE *)(pChunk + 1), amt.cbFormat);
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);
450 if (IsEqualGUID(&amt.formattype, &FORMAT_WaveFormatEx))
452 memcpy(&amt.subtype, &MEDIATYPE_Video, sizeof(GUID));
453 amt.subtype.Data1 = ((WAVEFORMATEX *)amt.pbFormat)->wFormatTag;
456 dump_AM_MEDIA_TYPE(&amt);
457 TRACE("fSamplesPerSec = %f\n", (double)fSamplesPerSec);
458 TRACE("dwSampleSize = %x\n", dwSampleSize);
459 TRACE("dwLength = %x\n", dwLength);
461 hr = Parser_AddPin(&(This->Parser), &piOutput, &props, &amt, fSamplesPerSec, dwSampleSize, dwLength);
463 return hr;
466 /* FIXME: fix leaks on failure here */
467 static HRESULT AVISplitter_InputPin_PreConnect(IPin * iface, IPin * pConnectPin)
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;
477 hr = IAsyncReader_SyncRead(This->pReader, pos, sizeof(list), (BYTE *)&list);
478 pos += sizeof(list);
480 if (list.fcc != ckidRIFF)
482 ERR("Input stream not a RIFF file\n");
483 return E_FAIL;
485 if (list.fccListType != ckidAVI)
487 ERR("Input stream not an AVI RIFF file\n");
488 return E_FAIL;
491 hr = IAsyncReader_SyncRead(This->pReader, pos, sizeof(list), (BYTE *)&list);
492 if (list.fcc != ckidLIST)
494 ERR("Expected LIST chunk, but got %.04s\n", (LPSTR)&list.fcc);
495 return E_FAIL;
497 if (list.fccListType != ckidHEADERLIST)
499 ERR("Header list expected. Got: %.04s\n", (LPSTR)&list.fccListType);
500 return E_FAIL;
503 pBuffer = HeapAlloc(GetProcessHeap(), 0, list.cb - sizeof(RIFFLIST) + sizeof(RIFFCHUNK));
504 hr = IAsyncReader_SyncRead(This->pReader, pos + sizeof(list), list.cb - sizeof(RIFFLIST) + sizeof(RIFFCHUNK), pBuffer);
506 pAviSplit->AviHeader.cb = 0;
508 for (pCurrentChunk = (RIFFCHUNK *)pBuffer; (BYTE *)pCurrentChunk + sizeof(*pCurrentChunk) < pBuffer + list.cb; pCurrentChunk = (RIFFCHUNK *)(((BYTE *)pCurrentChunk) + sizeof(*pCurrentChunk) + pCurrentChunk->cb))
510 RIFFLIST * pList;
512 switch (pCurrentChunk->fcc)
514 case ckidMAINAVIHEADER:
515 /* AVIMAINHEADER includes the structure that is pCurrentChunk at the moment */
516 memcpy(&pAviSplit->AviHeader, pCurrentChunk, sizeof(pAviSplit->AviHeader));
517 break;
518 case ckidLIST:
519 pList = (RIFFLIST *)pCurrentChunk;
520 switch (pList->fccListType)
522 case ckidSTREAMLIST:
523 hr = AVISplitter_ProcessStreamList(pAviSplit, (BYTE *)pCurrentChunk + sizeof(RIFFLIST), pCurrentChunk->cb + sizeof(RIFFCHUNK) - sizeof(RIFFLIST));
524 break;
525 case ckidODML:
526 FIXME("process ODML header\n");
527 break;
529 break;
530 case ckidJUNK:
531 /* ignore */
532 break;
533 default:
534 FIXME("unrecognised header list type: %.04s\n", (LPSTR)&pCurrentChunk->fcc);
537 HeapFree(GetProcessHeap(), 0, pBuffer);
539 if (pAviSplit->AviHeader.cb != sizeof(pAviSplit->AviHeader) - sizeof(RIFFCHUNK))
541 ERR("Avi Header wrong size!\n");
542 return E_FAIL;
545 pos += sizeof(RIFFCHUNK) + list.cb;
546 hr = IAsyncReader_SyncRead(This->pReader, pos, sizeof(list), (BYTE *)&list);
548 while (list.fcc == ckidJUNK || (list.fcc == ckidLIST && list.fccListType == ckidINFO))
550 pos += sizeof(RIFFCHUNK) + list.cb;
551 hr = IAsyncReader_SyncRead(This->pReader, pos, sizeof(list), (BYTE *)&list);
554 if (list.fcc != ckidLIST)
556 ERR("Expected LIST, but got %.04s\n", (LPSTR)&list.fcc);
557 return E_FAIL;
559 if (list.fccListType != ckidAVIMOVIE)
561 ERR("Expected AVI movie list, but got %.04s\n", (LPSTR)&list.fccListType);
562 return E_FAIL;
565 if (hr == S_OK)
567 pAviSplit->CurrentChunkOffset = MEDIATIME_FROM_BYTES(pos + sizeof(RIFFLIST));
568 pAviSplit->EndOfFile = MEDIATIME_FROM_BYTES(pos + list.cb + sizeof(RIFFLIST));
569 hr = IAsyncReader_SyncRead(This->pReader, BYTES_FROM_MEDIATIME(pAviSplit->CurrentChunkOffset), sizeof(pAviSplit->CurrentChunk), (BYTE *)&pAviSplit->CurrentChunk);
572 if (hr != S_OK)
573 return E_FAIL;
575 TRACE("AVI File ok\n");
577 return hr;
580 static HRESULT AVISplitter_Cleanup(LPVOID iface)
582 AVISplitterImpl *This = (AVISplitterImpl*)iface;
584 TRACE("(%p)->()\n", This);
586 if (This->pCurrentSample)
587 IMediaSample_Release(This->pCurrentSample);
588 This->pCurrentSample = NULL;
590 return S_OK;
593 HRESULT AVISplitter_create(IUnknown * pUnkOuter, LPVOID * ppv)
595 HRESULT hr;
596 AVISplitterImpl * This;
598 TRACE("(%p, %p)\n", pUnkOuter, ppv);
600 *ppv = NULL;
602 if (pUnkOuter)
603 return CLASS_E_NOAGGREGATION;
605 /* Note: This memory is managed by the transform filter once created */
606 This = CoTaskMemAlloc(sizeof(AVISplitterImpl));
608 This->pCurrentSample = NULL;
610 hr = Parser_Create(&(This->Parser), &CLSID_AviSplitter, AVISplitter_Sample, AVISplitter_QueryAccept, AVISplitter_InputPin_PreConnect, AVISplitter_Cleanup);
612 if (FAILED(hr))
613 return hr;
615 *ppv = (LPVOID)This;
617 return hr;