dwrite/tests: Fix some test failures on older dwrite versions.
[wine.git] / dlls / quartz / avisplit.c
blob1f5704598146172d6271123d2c81a71107d9e87c
1 /*
2 * AVI Splitter Filter
4 * Copyright 2003 Robert Shearman
5 * Copyright 2004-2005 Christian Costa
6 * Copyright 2008 Maarten Lankhorst
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 /* FIXME:
23 * - Reference leaks, if they still exist
24 * - Files without an index are not handled correctly yet.
25 * - When stopping/starting, a sample is lost. This should be compensated by
26 * keeping track of previous index/position.
27 * - Debugging channels are noisy at the moment, especially with thread
28 * related messages, however this is the only correct thing to do right now,
29 * since wine doesn't correctly handle all messages yet.
32 #include "quartz_private.h"
33 #include "pin.h"
35 #include "uuids.h"
36 #include "vfw.h"
37 #include "aviriff.h"
38 #include "vfwmsgs.h"
39 #include "amvideo.h"
41 #include "wine/unicode.h"
42 #include "wine/debug.h"
44 #include <math.h>
45 #include <assert.h>
47 #include "parser.h"
49 #define TWOCCFromFOURCC(fcc) HIWORD(fcc)
51 /* four character codes used in AVI files */
52 #define ckidINFO mmioFOURCC('I','N','F','O')
53 #define ckidREC mmioFOURCC('R','E','C',' ')
55 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
57 typedef struct StreamData
59 DWORD dwSampleSize;
60 FLOAT fSamplesPerSec;
61 DWORD dwLength;
63 AVISTREAMHEADER streamheader;
64 DWORD entries;
65 AVISTDINDEX **stdindex;
66 DWORD frames;
67 BOOL seek;
69 /* Position, in index units */
70 DWORD pos, pos_next, index, index_next;
72 /* Packet handling: a thread is created and waits on the packet event handle
73 * On an event acquire the sample lock, addref the sample and set it to NULL,
74 * then queue a new packet.
76 HANDLE thread, packet_queued;
77 IMediaSample *sample;
79 /* Amount of preroll samples for this stream */
80 DWORD preroll;
81 } StreamData;
83 typedef struct AVISplitterImpl
85 ParserImpl Parser;
86 RIFFCHUNK CurrentChunk;
87 LONGLONG CurrentChunkOffset; /* in media time */
88 LONGLONG EndOfFile;
89 AVIMAINHEADER AviHeader;
90 AVIEXTHEADER ExtHeader;
92 AVIOLDINDEX *oldindex;
93 DWORD offset;
95 StreamData *streams;
96 } AVISplitterImpl;
98 struct thread_args {
99 AVISplitterImpl *This;
100 DWORD stream;
103 static inline AVISplitterImpl *impl_from_IMediaSeeking( IMediaSeeking *iface )
105 return CONTAINING_RECORD(iface, AVISplitterImpl, Parser.sourceSeeking.IMediaSeeking_iface);
108 /* The threading stuff cries for an explanation
110 * PullPin starts processing and calls AVISplitter_first_request
111 * AVISplitter_first_request creates a thread for each stream
112 * A stream can be audio, video, subtitles or something undefined.
114 * AVISplitter_first_request loads a single packet to each but one stream,
115 * and queues it for that last stream. This is to prevent WaitForNext to time
116 * out badly.
118 * The processing loop is entered. It calls IAsyncReader_WaitForNext in the
119 * PullPin. Every time it receives a packet, it will call AVISplitter_Sample
120 * AVISplitter_Sample will signal the relevant thread that a new sample is
121 * arrived, when that thread is ready it will read the packet and transmits
122 * it downstream with AVISplitter_Receive
124 * Threads terminate upon receiving NULL as packet or when ANY error code
125 * != S_OK occurs. This means that any error is fatal to processing.
128 static HRESULT AVISplitter_SendEndOfFile(AVISplitterImpl *This, DWORD streamnumber)
130 IPin* ppin = NULL;
131 HRESULT hr;
133 TRACE("End of file reached\n");
135 hr = IPin_ConnectedTo(This->Parser.ppPins[streamnumber+1], &ppin);
136 if (SUCCEEDED(hr))
138 hr = IPin_EndOfStream(ppin);
139 IPin_Release(ppin);
141 TRACE("--> %x\n", hr);
143 /* Force the pullpin thread to stop */
144 return S_FALSE;
147 /* Thread worker horse */
148 static HRESULT AVISplitter_next_request(AVISplitterImpl *This, DWORD streamnumber)
150 StreamData *stream = This->streams + streamnumber;
151 PullPin *pin = This->Parser.pInputPin;
152 IMediaSample *sample = NULL;
153 HRESULT hr;
154 ULONG ref;
156 TRACE("(%p, %u)->()\n", This, streamnumber);
158 hr = IMemAllocator_GetBuffer(pin->pAlloc, &sample, NULL, NULL, 0);
159 if (hr != S_OK)
160 ERR("... %08x?\n", hr);
162 if (SUCCEEDED(hr))
164 LONGLONG rtSampleStart;
165 /* Add 4 for the next header, which should hopefully work */
166 LONGLONG rtSampleStop;
168 stream->pos = stream->pos_next;
169 stream->index = stream->index_next;
171 IMediaSample_SetDiscontinuity(sample, stream->seek);
172 stream->seek = FALSE;
173 if (stream->preroll)
175 --stream->preroll;
176 IMediaSample_SetPreroll(sample, TRUE);
178 else
179 IMediaSample_SetPreroll(sample, FALSE);
180 IMediaSample_SetSyncPoint(sample, TRUE);
182 if (stream->stdindex)
184 AVISTDINDEX *index = stream->stdindex[stream->index];
185 AVISTDINDEX_ENTRY *entry = &index->aIndex[stream->pos];
187 /* End of file */
188 if (stream->index >= stream->entries)
190 TRACE("END OF STREAM ON %u\n", streamnumber);
191 IMediaSample_Release(sample);
192 return S_FALSE;
195 rtSampleStart = index->qwBaseOffset;
196 rtSampleStart += entry->dwOffset;
197 rtSampleStart = MEDIATIME_FROM_BYTES(rtSampleStart);
199 ++stream->pos_next;
200 if (index->nEntriesInUse == stream->pos_next)
202 stream->pos_next = 0;
203 ++stream->index_next;
206 rtSampleStop = rtSampleStart + MEDIATIME_FROM_BYTES(entry->dwSize & ~(1 << 31));
208 TRACE("offset(%u) size(%u)\n", (DWORD)BYTES_FROM_MEDIATIME(rtSampleStart), (DWORD)BYTES_FROM_MEDIATIME(rtSampleStop - rtSampleStart));
210 else if (This->oldindex)
212 DWORD flags = This->oldindex->aIndex[stream->pos].dwFlags;
213 DWORD size = This->oldindex->aIndex[stream->pos].dwSize;
215 /* End of file */
216 if (stream->index)
218 TRACE("END OF STREAM ON %u\n", streamnumber);
219 IMediaSample_Release(sample);
220 return S_FALSE;
223 rtSampleStart = MEDIATIME_FROM_BYTES(This->offset);
224 rtSampleStart += MEDIATIME_FROM_BYTES(This->oldindex->aIndex[stream->pos].dwOffset);
225 rtSampleStop = rtSampleStart + MEDIATIME_FROM_BYTES(size);
226 if (flags & AVIIF_MIDPART)
228 FIXME("Only stand alone frames are currently handled correctly!\n");
230 if (flags & AVIIF_LIST)
232 FIXME("Not sure if this is handled correctly\n");
233 rtSampleStart += MEDIATIME_FROM_BYTES(sizeof(RIFFLIST));
234 rtSampleStop += MEDIATIME_FROM_BYTES(sizeof(RIFFLIST));
236 else
238 rtSampleStart += MEDIATIME_FROM_BYTES(sizeof(RIFFCHUNK));
239 rtSampleStop += MEDIATIME_FROM_BYTES(sizeof(RIFFCHUNK));
242 /* Slow way of finding next index */
243 do {
244 stream->pos_next++;
245 } while (stream->pos_next * sizeof(This->oldindex->aIndex[0]) < This->oldindex->cb
246 && StreamFromFOURCC(This->oldindex->aIndex[stream->pos_next].dwChunkId) != streamnumber);
248 /* End of file soon */
249 if (stream->pos_next * sizeof(This->oldindex->aIndex[0]) >= This->oldindex->cb)
251 stream->pos_next = 0;
252 ++stream->index_next;
255 else /* TODO: Generate an index automagically */
257 ERR("CAN'T PLAY WITHOUT AN INDEX! SOS! SOS! SOS!\n");
258 assert(0);
261 if (rtSampleStart != rtSampleStop)
263 hr = IMediaSample_SetTime(sample, &rtSampleStart, &rtSampleStop);
265 hr = IAsyncReader_Request(pin->pReader, sample, streamnumber);
267 if (FAILED(hr))
269 ref = IMediaSample_Release(sample);
270 assert(ref == 0);
273 else
275 stream->sample = sample;
276 IMediaSample_SetActualDataLength(sample, 0);
277 SetEvent(stream->packet_queued);
280 else
282 if (sample)
284 ERR("There should be no sample!\n");
285 ref = IMediaSample_Release(sample);
286 assert(ref == 0);
289 TRACE("--> %08x\n", hr);
291 return hr;
294 static HRESULT AVISplitter_Receive(AVISplitterImpl *This, IMediaSample *sample, DWORD streamnumber)
296 Parser_OutputPin *pin = unsafe_impl_Parser_OutputPin_from_IPin(This->Parser.ppPins[1+streamnumber]);
297 HRESULT hr;
298 LONGLONG start, stop, rtstart, rtstop;
299 StreamData *stream = &This->streams[streamnumber];
301 start = pin->dwSamplesProcessed;
302 start *= stream->streamheader.dwScale;
303 start *= 10000000;
304 start /= stream->streamheader.dwRate;
306 if (stream->streamheader.dwSampleSize)
308 ULONG len = IMediaSample_GetActualDataLength(sample);
309 ULONG size = stream->streamheader.dwSampleSize;
311 pin->dwSamplesProcessed += len / size;
313 else
314 ++pin->dwSamplesProcessed;
316 stop = pin->dwSamplesProcessed;
317 stop *= stream->streamheader.dwScale;
318 stop *= 10000000;
319 stop /= stream->streamheader.dwRate;
321 if (IMediaSample_IsDiscontinuity(sample) == S_OK) {
322 IPin *victim;
323 EnterCriticalSection(&This->Parser.filter.csFilter);
324 pin->pin.pin.tStart = start;
325 pin->pin.pin.dRate = This->Parser.sourceSeeking.dRate;
326 hr = IPin_ConnectedTo(&pin->pin.pin.IPin_iface, &victim);
327 if (hr == S_OK)
329 hr = IPin_NewSegment(victim, start, This->Parser.sourceSeeking.llStop,
330 This->Parser.sourceSeeking.dRate);
331 if (hr != S_OK)
332 FIXME("NewSegment returns %08x\n", hr);
333 IPin_Release(victim);
335 LeaveCriticalSection(&This->Parser.filter.csFilter);
336 if (hr != S_OK)
337 return hr;
339 rtstart = (double)(start - pin->pin.pin.tStart) / pin->pin.pin.dRate;
340 rtstop = (double)(stop - pin->pin.pin.tStart) / pin->pin.pin.dRate;
341 hr = IMediaSample_SetMediaTime(sample, &start, &stop);
342 IMediaSample_SetTime(sample, &rtstart, &rtstop);
343 IMediaSample_SetMediaTime(sample, &start, &stop);
345 hr = BaseOutputPinImpl_Deliver(&pin->pin, sample);
347 /* Uncomment this if you want to debug the time differences between the
348 * different streams, it is useful for that
350 FIXME("stream %u, hr: %08x, Start: %u.%03u, Stop: %u.%03u\n", streamnumber, hr,
351 (DWORD)(start / 10000000), (DWORD)((start / 10000)%1000),
352 (DWORD)(stop / 10000000), (DWORD)((stop / 10000)%1000));
354 return hr;
357 static DWORD WINAPI AVISplitter_thread_reader(LPVOID data)
359 struct thread_args *args = data;
360 AVISplitterImpl *This = args->This;
361 DWORD streamnumber = args->stream;
362 HRESULT hr = S_OK;
366 HRESULT nexthr = S_FALSE;
367 IMediaSample *sample;
369 WaitForSingleObject(This->streams[streamnumber].packet_queued, INFINITE);
370 sample = This->streams[streamnumber].sample;
371 This->streams[streamnumber].sample = NULL;
372 if (!sample)
373 break;
375 nexthr = AVISplitter_next_request(This, streamnumber);
377 hr = AVISplitter_Receive(This, sample, streamnumber);
378 if (hr != S_OK)
379 FIXME("Receiving error: %08x\n", hr);
381 IMediaSample_Release(sample);
382 if (hr == S_OK)
383 hr = nexthr;
384 if (nexthr == S_FALSE)
385 AVISplitter_SendEndOfFile(This, streamnumber);
386 } while (hr == S_OK);
388 if (hr != S_FALSE)
389 FIXME("Thread %u terminated with hr %08x!\n", streamnumber, hr);
390 else
391 TRACE("Thread %u terminated properly\n", streamnumber);
392 return hr;
395 static HRESULT AVISplitter_Sample(LPVOID iface, IMediaSample * pSample, DWORD_PTR cookie)
397 AVISplitterImpl *This = iface;
398 StreamData *stream = This->streams + cookie;
399 HRESULT hr = S_OK;
401 if (!IMediaSample_GetActualDataLength(pSample))
403 ERR("Received empty sample\n");
404 return S_OK;
407 /* Send the sample to whatever thread is appropriate
408 * That thread should also not have a sample queued at the moment
410 /* Debugging */
411 TRACE("(%p)->(%p size: %u, %lu)\n", This, pSample, IMediaSample_GetActualDataLength(pSample), cookie);
412 assert(cookie < This->Parser.cStreams);
413 assert(!stream->sample);
414 assert(WaitForSingleObject(stream->packet_queued, 0) == WAIT_TIMEOUT);
416 IMediaSample_AddRef(pSample);
418 stream->sample = pSample;
419 SetEvent(stream->packet_queued);
421 return hr;
424 static HRESULT AVISplitter_done_process(LPVOID iface);
426 /* On the first request we have to be sure that (cStreams-1) samples have
427 * already been processed, because otherwise some pins might not ever finish
428 * a Pause state change
430 static HRESULT AVISplitter_first_request(LPVOID iface)
432 AVISplitterImpl *This = iface;
433 HRESULT hr = S_OK;
434 DWORD x;
435 IMediaSample *sample = NULL;
436 BOOL have_sample = FALSE;
438 TRACE("(%p)->()\n", This);
440 for (x = 0; x < This->Parser.cStreams; ++x)
442 StreamData *stream = This->streams + x;
444 /* Nothing should be running at this point */
445 assert(!stream->thread);
447 assert(!sample);
448 /* It could be we asked the thread to terminate, and the thread
449 * already terminated before receiving the deathwish */
450 ResetEvent(stream->packet_queued);
452 stream->pos_next = stream->pos;
453 stream->index_next = stream->index;
455 /* This was sent after stopped->paused or stopped->playing, so set seek */
456 stream->seek = TRUE;
458 /* There should be a packet queued from AVISplitter_next_request last time
459 * It needs to be done now because this is the only way to ensure that every
460 * stream will have at least 1 packet processed
461 * If this is done after the threads start it could go all awkward and we
462 * would have no guarantees that it's successful at all
465 if (have_sample)
467 DWORD_PTR dwUser = ~0;
468 hr = IAsyncReader_WaitForNext(This->Parser.pInputPin->pReader, 10000, &sample, &dwUser);
469 assert(hr == S_OK);
470 assert(sample);
472 AVISplitter_Sample(iface, sample, dwUser);
473 IMediaSample_Release(sample);
476 hr = AVISplitter_next_request(This, x);
477 TRACE("-->%08x\n", hr);
479 /* Could be an EOF instead */
480 have_sample = (hr == S_OK);
481 if (hr == S_FALSE)
482 AVISplitter_SendEndOfFile(This, x);
484 if (FAILED(hr) && hr != VFW_E_NOT_CONNECTED)
485 break;
486 hr = S_OK;
489 /* FIXME: Don't do this for each pin that sent an EOF */
490 for (x = 0; x < This->Parser.cStreams && SUCCEEDED(hr); ++x)
492 struct thread_args *args;
493 DWORD tid;
495 if ((This->streams[x].stdindex && This->streams[x].index_next >= This->streams[x].entries) ||
496 (!This->streams[x].stdindex && This->streams[x].index_next))
498 This->streams[x].thread = NULL;
499 continue;
502 args = CoTaskMemAlloc(sizeof(*args));
503 args->This = This;
504 args->stream = x;
505 This->streams[x].thread = CreateThread(NULL, 0, AVISplitter_thread_reader, args, 0, &tid);
506 TRACE("Created stream %u thread 0x%08x\n", x, tid);
509 if (FAILED(hr))
510 ERR("Horsemen of the apocalypse came to bring error 0x%08x\n", hr);
512 return hr;
515 static HRESULT AVISplitter_done_process(LPVOID iface)
517 AVISplitterImpl *This = iface;
518 DWORD x;
519 ULONG ref;
521 for (x = 0; x < This->Parser.cStreams; ++x)
523 StreamData *stream = This->streams + x;
525 TRACE("Waiting for %u to terminate\n", x);
526 /* Make the thread return first */
527 SetEvent(stream->packet_queued);
528 assert(WaitForSingleObject(stream->thread, 100000) != WAIT_TIMEOUT);
529 CloseHandle(stream->thread);
530 stream->thread = NULL;
532 if (stream->sample)
534 ref = IMediaSample_Release(stream->sample);
535 assert(ref == 0);
537 stream->sample = NULL;
539 ResetEvent(stream->packet_queued);
541 TRACE("All threads are now terminated\n");
543 return S_OK;
546 static HRESULT AVISplitter_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
548 if (IsEqualIID(&pmt->majortype, &MEDIATYPE_Stream) && IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_Avi))
549 return S_OK;
550 return S_FALSE;
553 static HRESULT AVISplitter_ProcessIndex(AVISplitterImpl *This, AVISTDINDEX **index, LONGLONG qwOffset, DWORD cb)
555 AVISTDINDEX *pIndex;
556 DWORD x;
557 int rest;
559 *index = NULL;
560 if (cb < sizeof(AVISTDINDEX))
562 FIXME("size %u too small\n", cb);
563 return E_INVALIDARG;
566 pIndex = CoTaskMemAlloc(cb);
567 if (!pIndex)
568 return E_OUTOFMEMORY;
570 IAsyncReader_SyncRead((impl_PullPin_from_IPin(This->Parser.ppPins[0]))->pReader, qwOffset, cb, (BYTE *)pIndex);
571 rest = cb - sizeof(AVISUPERINDEX) + sizeof(RIFFCHUNK) + sizeof(pIndex->aIndex);
573 TRACE("FOURCC: %s\n", debugstr_an((char *)&pIndex->fcc, 4));
574 TRACE("wLongsPerEntry: %hd\n", pIndex->wLongsPerEntry);
575 TRACE("bIndexSubType: %u\n", pIndex->bIndexSubType);
576 TRACE("bIndexType: %u\n", pIndex->bIndexType);
577 TRACE("nEntriesInUse: %u\n", pIndex->nEntriesInUse);
578 TRACE("dwChunkId: %.4s\n", (char *)&pIndex->dwChunkId);
579 TRACE("qwBaseOffset: %x%08x\n", (DWORD)(pIndex->qwBaseOffset >> 32), (DWORD)pIndex->qwBaseOffset);
580 TRACE("dwReserved_3: %u\n", pIndex->dwReserved_3);
582 if (pIndex->bIndexType != AVI_INDEX_OF_CHUNKS
583 || pIndex->wLongsPerEntry != 2
584 || rest < (pIndex->nEntriesInUse * sizeof(DWORD) * pIndex->wLongsPerEntry)
585 || (pIndex->bIndexSubType != AVI_INDEX_SUB_DEFAULT))
587 FIXME("Invalid index chunk encountered: %u/%u, %u/%u, %u/%u, %u/%u\n",
588 pIndex->bIndexType, AVI_INDEX_OF_CHUNKS, pIndex->wLongsPerEntry, 2,
589 rest, (DWORD)(pIndex->nEntriesInUse * sizeof(DWORD) * pIndex->wLongsPerEntry),
590 pIndex->bIndexSubType, AVI_INDEX_SUB_DEFAULT);
591 *index = NULL;
592 return E_INVALIDARG;
595 for (x = 0; x < pIndex->nEntriesInUse; ++x)
597 BOOL keyframe = !(pIndex->aIndex[x].dwSize >> 31);
598 DWORDLONG offset = pIndex->qwBaseOffset + pIndex->aIndex[x].dwOffset;
599 TRACE("dwOffset: %x%08x\n", (DWORD)(offset >> 32), (DWORD)offset);
600 TRACE("dwSize: %u\n", (pIndex->aIndex[x].dwSize & ~(1<<31)));
601 TRACE("Frame is a keyframe: %s\n", keyframe ? "yes" : "no");
604 *index = pIndex;
605 return S_OK;
608 static HRESULT AVISplitter_ProcessOldIndex(AVISplitterImpl *This)
610 ULONGLONG mov_pos = BYTES_FROM_MEDIATIME(This->CurrentChunkOffset) - sizeof(DWORD);
611 AVIOLDINDEX *pAviOldIndex = This->oldindex;
612 int relative = -1;
613 DWORD x;
615 for (x = 0; x < pAviOldIndex->cb / sizeof(pAviOldIndex->aIndex[0]); ++x)
617 DWORD temp, temp2 = 0, offset, chunkid;
618 PullPin *pin = This->Parser.pInputPin;
620 offset = pAviOldIndex->aIndex[x].dwOffset;
621 chunkid = pAviOldIndex->aIndex[x].dwChunkId;
623 TRACE("dwChunkId: %.4s\n", (char *)&chunkid);
624 TRACE("dwFlags: %08x\n", pAviOldIndex->aIndex[x].dwFlags);
625 TRACE("dwOffset (%s): %08x\n", relative ? "relative" : "absolute", offset);
626 TRACE("dwSize: %08x\n", pAviOldIndex->aIndex[x].dwSize);
628 /* Only scan once, or else this will take too long */
629 if (relative == -1)
631 IAsyncReader_SyncRead(pin->pReader, offset, sizeof(DWORD), (BYTE *)&temp);
632 relative = (chunkid != temp);
634 if (chunkid == mmioFOURCC('7','F','x','x')
635 && ((char *)&temp)[0] == 'i' && ((char *)&temp)[1] == 'x')
636 relative = FALSE;
638 if (relative)
640 if (offset + mov_pos < BYTES_FROM_MEDIATIME(This->EndOfFile))
641 IAsyncReader_SyncRead(pin->pReader, offset + mov_pos, sizeof(DWORD), (BYTE *)&temp2);
643 if (chunkid == mmioFOURCC('7','F','x','x')
644 && ((char *)&temp2)[0] == 'i' && ((char *)&temp2)[1] == 'x')
646 /* Do nothing, all is great */
648 else if (temp2 != chunkid)
650 ERR("Faulty index or bug in handling: Wanted FCC: %s, Abs FCC: %s (@ %x), Rel FCC: %s (@ %.0x%08x)\n",
651 debugstr_an((char *)&chunkid, 4), debugstr_an((char *)&temp, 4), offset,
652 debugstr_an((char *)&temp2, 4), (DWORD)((mov_pos + offset) >> 32), (DWORD)(mov_pos + offset));
653 relative = -1;
655 else
656 TRACE("Scanned dwChunkId: %s\n", debugstr_an((char *)&temp2, 4));
658 else if (!relative)
659 TRACE("Scanned dwChunkId: %s\n", debugstr_an((char *)&temp, 4));
661 /* Only dump one packet */
662 else break;
665 if (relative == -1)
667 FIXME("Dropping index: no idea whether it is relative or absolute\n");
668 CoTaskMemFree(This->oldindex);
669 This->oldindex = NULL;
671 else if (!relative)
672 This->offset = 0;
673 else
674 This->offset = (DWORD)mov_pos;
676 return S_OK;
679 static HRESULT AVISplitter_ProcessStreamList(AVISplitterImpl * This, const BYTE * pData, DWORD cb, ALLOCATOR_PROPERTIES *props)
681 PIN_INFO piOutput;
682 const RIFFCHUNK * pChunk;
683 HRESULT hr;
684 AM_MEDIA_TYPE amt;
685 float fSamplesPerSec = 0.0f;
686 DWORD dwSampleSize = 0;
687 DWORD dwLength = 0;
688 DWORD nstdindex = 0;
689 static const WCHAR wszStreamTemplate[] = {'S','t','r','e','a','m',' ','%','0','2','d',0};
690 StreamData *stream;
692 ZeroMemory(&amt, sizeof(amt));
693 piOutput.dir = PINDIR_OUTPUT;
694 piOutput.pFilter = (IBaseFilter *)This;
695 wsprintfW(piOutput.achName, wszStreamTemplate, This->Parser.cStreams);
696 This->streams = CoTaskMemRealloc(This->streams, sizeof(StreamData) * (This->Parser.cStreams+1));
697 stream = This->streams + This->Parser.cStreams;
698 ZeroMemory(stream, sizeof(*stream));
700 for (pChunk = (const RIFFCHUNK *)pData;
701 ((const BYTE *)pChunk >= pData) && ((const BYTE *)pChunk + sizeof(RIFFCHUNK) < pData + cb) && (pChunk->cb > 0);
702 pChunk = (const RIFFCHUNK *)((const BYTE*)pChunk + sizeof(RIFFCHUNK) + pChunk->cb)
705 switch (pChunk->fcc)
707 case ckidSTREAMHEADER:
709 const AVISTREAMHEADER * pStrHdr = (const AVISTREAMHEADER *)pChunk;
710 TRACE("processing stream header\n");
711 stream->streamheader = *pStrHdr;
713 fSamplesPerSec = (float)pStrHdr->dwRate / (float)pStrHdr->dwScale;
714 CoTaskMemFree(amt.pbFormat);
715 amt.pbFormat = NULL;
716 amt.cbFormat = 0;
718 switch (pStrHdr->fccType)
720 case streamtypeVIDEO:
721 amt.formattype = FORMAT_VideoInfo;
722 break;
723 case streamtypeAUDIO:
724 amt.formattype = FORMAT_WaveFormatEx;
725 break;
726 default:
727 FIXME("fccType %.4s not handled yet\n", (const char *)&pStrHdr->fccType);
728 amt.formattype = FORMAT_None;
730 amt.majortype = MEDIATYPE_Video;
731 amt.majortype.Data1 = pStrHdr->fccType;
732 amt.subtype = MEDIATYPE_Video;
733 amt.subtype.Data1 = pStrHdr->fccHandler;
734 TRACE("Subtype FCC: %.04s\n", (LPCSTR)&pStrHdr->fccHandler);
735 amt.lSampleSize = pStrHdr->dwSampleSize;
736 amt.bFixedSizeSamples = (amt.lSampleSize != 0);
738 /* FIXME: Is this right? */
739 if (!amt.lSampleSize)
741 amt.lSampleSize = 1;
742 dwSampleSize = 1;
745 amt.bTemporalCompression = IsEqualGUID(&amt.majortype, &MEDIATYPE_Video); /* FIXME? */
746 dwSampleSize = pStrHdr->dwSampleSize;
747 dwLength = pStrHdr->dwLength;
748 if (!dwLength)
749 dwLength = This->AviHeader.dwTotalFrames;
751 if (pStrHdr->dwSuggestedBufferSize && pStrHdr->dwSuggestedBufferSize > props->cbBuffer)
752 props->cbBuffer = pStrHdr->dwSuggestedBufferSize;
754 break;
756 case ckidSTREAMFORMAT:
757 TRACE("processing stream format data\n");
758 if (IsEqualIID(&amt.formattype, &FORMAT_VideoInfo))
760 VIDEOINFOHEADER * pvi;
761 /* biCompression member appears to override the value in the stream header.
762 * i.e. the stream header can say something completely contradictory to what
763 * is in the BITMAPINFOHEADER! */
764 if (pChunk->cb < sizeof(BITMAPINFOHEADER))
766 ERR("Not enough bytes for BITMAPINFOHEADER\n");
767 return E_FAIL;
769 amt.cbFormat = sizeof(VIDEOINFOHEADER) - sizeof(BITMAPINFOHEADER) + pChunk->cb;
770 amt.pbFormat = CoTaskMemAlloc(amt.cbFormat);
771 ZeroMemory(amt.pbFormat, amt.cbFormat);
772 pvi = (VIDEOINFOHEADER *)amt.pbFormat;
773 pvi->AvgTimePerFrame = (LONGLONG)(10000000.0 / fSamplesPerSec);
775 CopyMemory(&pvi->bmiHeader, pChunk + 1, pChunk->cb);
776 if (pvi->bmiHeader.biCompression)
777 amt.subtype.Data1 = pvi->bmiHeader.biCompression;
779 else if (IsEqualIID(&amt.formattype, &FORMAT_WaveFormatEx))
781 amt.cbFormat = pChunk->cb;
782 if (amt.cbFormat < sizeof(WAVEFORMATEX))
783 amt.cbFormat = sizeof(WAVEFORMATEX);
784 amt.pbFormat = CoTaskMemAlloc(amt.cbFormat);
785 ZeroMemory(amt.pbFormat, amt.cbFormat);
786 CopyMemory(amt.pbFormat, pChunk + 1, pChunk->cb);
788 else
790 amt.cbFormat = pChunk->cb;
791 amt.pbFormat = CoTaskMemAlloc(amt.cbFormat);
792 CopyMemory(amt.pbFormat, pChunk + 1, amt.cbFormat);
794 break;
795 case ckidSTREAMNAME:
796 TRACE("processing stream name\n");
797 /* FIXME: this doesn't exactly match native version (we omit the "##)" prefix), but hey... */
798 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)(pChunk + 1), pChunk->cb, piOutput.achName, sizeof(piOutput.achName) / sizeof(piOutput.achName[0]));
799 break;
800 case ckidSTREAMHANDLERDATA:
801 FIXME("process stream handler data\n");
802 break;
803 case ckidAVIPADDING:
804 TRACE("JUNK chunk ignored\n");
805 break;
806 case ckidAVISUPERINDEX:
808 const AVISUPERINDEX *pIndex = (const AVISUPERINDEX *)pChunk;
809 DWORD x;
810 UINT rest = pIndex->cb - sizeof(AVISUPERINDEX) + sizeof(RIFFCHUNK) + sizeof(pIndex->aIndex[0]) * ANYSIZE_ARRAY;
812 if (pIndex->cb < sizeof(AVISUPERINDEX) - sizeof(RIFFCHUNK))
814 FIXME("size %u\n", pIndex->cb);
815 break;
818 if (nstdindex++ > 0)
820 ERR("Stream %d got more than 1 superindex?\n", This->Parser.cStreams);
821 break;
824 TRACE("wLongsPerEntry: %hd\n", pIndex->wLongsPerEntry);
825 TRACE("bIndexSubType: %u\n", pIndex->bIndexSubType);
826 TRACE("bIndexType: %u\n", pIndex->bIndexType);
827 TRACE("nEntriesInUse: %u\n", pIndex->nEntriesInUse);
828 TRACE("dwChunkId: %.4s\n", (const char *)&pIndex->dwChunkId);
829 if (pIndex->dwReserved[0])
830 TRACE("dwReserved[0]: %u\n", pIndex->dwReserved[0]);
831 if (pIndex->dwReserved[1])
832 TRACE("dwReserved[1]: %u\n", pIndex->dwReserved[1]);
833 if (pIndex->dwReserved[2])
834 TRACE("dwReserved[2]: %u\n", pIndex->dwReserved[2]);
836 if (pIndex->bIndexType != AVI_INDEX_OF_INDEXES
837 || pIndex->wLongsPerEntry != 4
838 || rest < (pIndex->nEntriesInUse * sizeof(DWORD) * pIndex->wLongsPerEntry)
839 || (pIndex->bIndexSubType != AVI_INDEX_SUB_2FIELD && pIndex->bIndexSubType != AVI_INDEX_SUB_DEFAULT))
841 FIXME("Invalid index chunk encountered\n");
842 break;
845 stream->entries = pIndex->nEntriesInUse;
846 stream->stdindex = CoTaskMemRealloc(stream->stdindex, sizeof(*stream->stdindex) * stream->entries);
847 for (x = 0; x < pIndex->nEntriesInUse; ++x)
849 TRACE("qwOffset: %x%08x\n", (DWORD)(pIndex->aIndex[x].qwOffset >> 32), (DWORD)pIndex->aIndex[x].qwOffset);
850 TRACE("dwSize: %u\n", pIndex->aIndex[x].dwSize);
851 TRACE("dwDuration: %u (unreliable)\n", pIndex->aIndex[x].dwDuration);
853 AVISplitter_ProcessIndex(This, &stream->stdindex[x], pIndex->aIndex[x].qwOffset, pIndex->aIndex[x].dwSize);
855 break;
857 default:
858 FIXME("unknown chunk type \"%.04s\" ignored\n", (LPCSTR)&pChunk->fcc);
862 if (IsEqualGUID(&amt.formattype, &FORMAT_WaveFormatEx))
864 amt.subtype = MEDIATYPE_Video;
865 amt.subtype.Data1 = ((WAVEFORMATEX *)amt.pbFormat)->wFormatTag;
868 dump_AM_MEDIA_TYPE(&amt);
869 TRACE("fSamplesPerSec = %f\n", (double)fSamplesPerSec);
870 TRACE("dwSampleSize = %x\n", dwSampleSize);
871 TRACE("dwLength = %x\n", dwLength);
873 stream->fSamplesPerSec = fSamplesPerSec;
874 stream->dwSampleSize = dwSampleSize;
875 stream->dwLength = dwLength; /* TODO: Use this for mediaseeking */
876 stream->packet_queued = CreateEventW(NULL, 0, 0, NULL);
878 hr = Parser_AddPin(&(This->Parser), &piOutput, props, &amt);
879 CoTaskMemFree(amt.pbFormat);
882 return hr;
885 static HRESULT AVISplitter_ProcessODML(AVISplitterImpl * This, const BYTE * pData, DWORD cb)
887 const RIFFCHUNK * pChunk;
889 for (pChunk = (const RIFFCHUNK *)pData;
890 ((const BYTE *)pChunk >= pData) && ((const BYTE *)pChunk + sizeof(RIFFCHUNK) < pData + cb) && (pChunk->cb > 0);
891 pChunk = (const RIFFCHUNK *)((const BYTE*)pChunk + sizeof(RIFFCHUNK) + pChunk->cb)
894 switch (pChunk->fcc)
896 case ckidAVIEXTHEADER:
898 int x;
899 const AVIEXTHEADER * pExtHdr = (const AVIEXTHEADER *)pChunk;
901 TRACE("processing extension header\n");
902 if (pExtHdr->cb != sizeof(AVIEXTHEADER) - sizeof(RIFFCHUNK))
904 FIXME("Size: %u\n", pExtHdr->cb);
905 break;
907 TRACE("dwGrandFrames: %u\n", pExtHdr->dwGrandFrames);
908 for (x = 0; x < 61; ++x)
909 if (pExtHdr->dwFuture[x])
910 FIXME("dwFuture[%i] = %u (0x%08x)\n", x, pExtHdr->dwFuture[x], pExtHdr->dwFuture[x]);
911 This->ExtHeader = *pExtHdr;
912 break;
914 default:
915 FIXME("unknown chunk type \"%.04s\" ignored\n", (LPCSTR)&pChunk->fcc);
919 return S_OK;
922 static HRESULT AVISplitter_InitializeStreams(AVISplitterImpl *This)
924 unsigned int x;
926 if (This->oldindex)
928 DWORD nMax, n;
930 for (x = 0; x < This->Parser.cStreams; ++x)
932 This->streams[x].frames = 0;
933 This->streams[x].pos = ~0;
934 This->streams[x].index = 0;
937 nMax = This->oldindex->cb / sizeof(This->oldindex->aIndex[0]);
939 /* Ok, maybe this is more of an exercise to see if I interpret everything correctly or not, but that is useful for now. */
940 for (n = 0; n < nMax; ++n)
942 DWORD streamId = StreamFromFOURCC(This->oldindex->aIndex[n].dwChunkId);
943 if (streamId >= This->Parser.cStreams)
945 FIXME("Stream id %s ignored\n", debugstr_an((char*)&This->oldindex->aIndex[n].dwChunkId, 4));
946 continue;
948 if (This->streams[streamId].pos == ~0U)
949 This->streams[streamId].pos = n;
951 if (This->streams[streamId].streamheader.dwSampleSize)
952 This->streams[streamId].frames += This->oldindex->aIndex[n].dwSize / This->streams[streamId].streamheader.dwSampleSize;
953 else
954 ++This->streams[streamId].frames;
957 for (x = 0; x < This->Parser.cStreams; ++x)
959 if ((DWORD)This->streams[x].frames != This->streams[x].streamheader.dwLength)
961 FIXME("stream %u: frames found: %u, frames meant to be found: %u\n", x, (DWORD)This->streams[x].frames, This->streams[x].streamheader.dwLength);
966 else if (!This->streams[0].entries)
968 for (x = 0; x < This->Parser.cStreams; ++x)
970 This->streams[x].frames = This->streams[x].streamheader.dwLength;
972 /* MS Avi splitter does seek through the whole file, we should! */
973 ERR("We should be manually seeking through the entire file to build an index, because the index is missing!!!\n");
974 return E_NOTIMPL;
977 /* Not much here yet */
978 for (x = 0; x < This->Parser.cStreams; ++x)
980 StreamData *stream = This->streams + x;
981 DWORD y;
982 DWORD64 frames = 0;
984 stream->seek = TRUE;
986 if (stream->stdindex)
988 stream->index = 0;
989 stream->pos = 0;
990 for (y = 0; y < stream->entries; ++y)
992 if (stream->streamheader.dwSampleSize)
994 DWORD z;
996 for (z = 0; z < stream->stdindex[y]->nEntriesInUse; ++z)
998 UINT len = stream->stdindex[y]->aIndex[z].dwSize & ~(1 << 31);
999 frames += len / stream->streamheader.dwSampleSize + !!(len % stream->streamheader.dwSampleSize);
1002 else
1003 frames += stream->stdindex[y]->nEntriesInUse;
1006 else frames = stream->frames;
1008 frames *= stream->streamheader.dwScale;
1009 /* Keep accuracy as high as possible for duration */
1010 This->Parser.sourceSeeking.llDuration = frames * 10000000;
1011 This->Parser.sourceSeeking.llDuration /= stream->streamheader.dwRate;
1012 This->Parser.sourceSeeking.llStop = This->Parser.sourceSeeking.llDuration;
1013 This->Parser.sourceSeeking.llCurrent = 0;
1015 frames /= stream->streamheader.dwRate;
1017 TRACE("Duration: %d days, %d hours, %d minutes and %d.%03u seconds\n", (DWORD)(frames / 86400),
1018 (DWORD)((frames % 86400) / 3600), (DWORD)((frames % 3600) / 60), (DWORD)(frames % 60),
1019 (DWORD)(This->Parser.sourceSeeking.llDuration/10000) % 1000);
1022 return S_OK;
1025 static HRESULT AVISplitter_Disconnect(LPVOID iface);
1027 /* FIXME: fix leaks on failure here */
1028 static HRESULT AVISplitter_InputPin_PreConnect(IPin * iface, IPin * pConnectPin, ALLOCATOR_PROPERTIES *props)
1030 PullPin *This = impl_PullPin_from_IPin(iface);
1031 HRESULT hr;
1032 RIFFLIST list;
1033 LONGLONG pos = 0; /* in bytes */
1034 BYTE * pBuffer;
1035 RIFFCHUNK * pCurrentChunk;
1036 LONGLONG total, avail;
1037 ULONG x;
1038 DWORD indexes;
1040 AVISplitterImpl * pAviSplit = (AVISplitterImpl *)This->pin.pinInfo.pFilter;
1042 hr = IAsyncReader_SyncRead(This->pReader, pos, sizeof(list), (BYTE *)&list);
1043 pos += sizeof(list);
1045 if (list.fcc != FOURCC_RIFF)
1047 ERR("Input stream not a RIFF file\n");
1048 return E_FAIL;
1050 if (list.fccListType != formtypeAVI)
1052 ERR("Input stream not an AVI RIFF file\n");
1053 return E_FAIL;
1056 hr = IAsyncReader_SyncRead(This->pReader, pos, sizeof(list), (BYTE *)&list);
1057 if (list.fcc != FOURCC_LIST)
1059 ERR("Expected LIST chunk, but got %.04s\n", (LPSTR)&list.fcc);
1060 return E_FAIL;
1062 if (list.fccListType != listtypeAVIHEADER)
1064 ERR("Header list expected. Got: %.04s\n", (LPSTR)&list.fccListType);
1065 return E_FAIL;
1068 pBuffer = HeapAlloc(GetProcessHeap(), 0, list.cb - sizeof(RIFFLIST) + sizeof(RIFFCHUNK));
1069 hr = IAsyncReader_SyncRead(This->pReader, pos + sizeof(list), list.cb - sizeof(RIFFLIST) + sizeof(RIFFCHUNK), pBuffer);
1071 pAviSplit->AviHeader.cb = 0;
1073 /* Stream list will set the buffer size here, so set a default and allow an override */
1074 props->cbBuffer = 0x20000;
1076 for (pCurrentChunk = (RIFFCHUNK *)pBuffer; (BYTE *)pCurrentChunk + sizeof(*pCurrentChunk) < pBuffer + list.cb; pCurrentChunk = (RIFFCHUNK *)(((BYTE *)pCurrentChunk) + sizeof(*pCurrentChunk) + pCurrentChunk->cb))
1078 RIFFLIST * pList;
1080 switch (pCurrentChunk->fcc)
1082 case ckidMAINAVIHEADER:
1083 /* AVIMAINHEADER includes the structure that is pCurrentChunk at the moment */
1084 memcpy(&pAviSplit->AviHeader, pCurrentChunk, sizeof(pAviSplit->AviHeader));
1085 break;
1086 case FOURCC_LIST:
1087 pList = (RIFFLIST *)pCurrentChunk;
1088 switch (pList->fccListType)
1090 case ckidSTREAMLIST:
1091 hr = AVISplitter_ProcessStreamList(pAviSplit, (BYTE *)pCurrentChunk + sizeof(RIFFLIST), pCurrentChunk->cb + sizeof(RIFFCHUNK) - sizeof(RIFFLIST), props);
1092 break;
1093 case ckidODML:
1094 hr = AVISplitter_ProcessODML(pAviSplit, (BYTE *)pCurrentChunk + sizeof(RIFFLIST), pCurrentChunk->cb + sizeof(RIFFCHUNK) - sizeof(RIFFLIST));
1095 break;
1097 break;
1098 case ckidAVIPADDING:
1099 /* ignore */
1100 break;
1101 default:
1102 FIXME("unrecognised header list type: %.04s\n", (LPSTR)&pCurrentChunk->fcc);
1105 HeapFree(GetProcessHeap(), 0, pBuffer);
1107 if (pAviSplit->AviHeader.cb != sizeof(pAviSplit->AviHeader) - sizeof(RIFFCHUNK))
1109 ERR("Avi Header wrong size!\n");
1110 return E_FAIL;
1113 pos += sizeof(RIFFCHUNK) + list.cb;
1114 hr = IAsyncReader_SyncRead(This->pReader, pos, sizeof(list), (BYTE *)&list);
1116 while (list.fcc == ckidAVIPADDING || (list.fcc == FOURCC_LIST && list.fccListType != listtypeAVIMOVIE))
1118 pos += sizeof(RIFFCHUNK) + list.cb;
1120 hr = IAsyncReader_SyncRead(This->pReader, pos, sizeof(list), (BYTE *)&list);
1123 if (list.fcc != FOURCC_LIST)
1125 ERR("Expected LIST, but got %.04s\n", (LPSTR)&list.fcc);
1126 return E_FAIL;
1128 if (list.fccListType != listtypeAVIMOVIE)
1130 ERR("Expected AVI movie list, but got %.04s\n", (LPSTR)&list.fccListType);
1131 return E_FAIL;
1134 IAsyncReader_Length(This->pReader, &total, &avail);
1136 /* FIXME: AVIX files are extended beyond the FOURCC chunk "AVI ", and thus won't be played here,
1137 * once I get one of the files I'll try to fix it */
1138 if (hr == S_OK)
1140 This->rtStart = pAviSplit->CurrentChunkOffset = MEDIATIME_FROM_BYTES(pos + sizeof(RIFFLIST));
1141 pos += list.cb + sizeof(RIFFCHUNK);
1143 pAviSplit->EndOfFile = This->rtStop = MEDIATIME_FROM_BYTES(pos);
1144 if (pos > total)
1146 ERR("File smaller (%x%08x) then EndOfFile (%x%08x)\n", (DWORD)(total >> 32), (DWORD)total, (DWORD)(pAviSplit->EndOfFile >> 32), (DWORD)pAviSplit->EndOfFile);
1147 return E_FAIL;
1150 hr = IAsyncReader_SyncRead(This->pReader, BYTES_FROM_MEDIATIME(pAviSplit->CurrentChunkOffset), sizeof(pAviSplit->CurrentChunk), (BYTE *)&pAviSplit->CurrentChunk);
1153 props->cbAlign = 1;
1154 props->cbPrefix = 0;
1155 /* Comrades, prevent shortage of buffers, or you will feel the consequences! DA! */
1156 props->cBuffers = 2 * pAviSplit->Parser.cStreams;
1158 /* Now peek into the idx1 index, if available */
1159 if (hr == S_OK && (total - pos) > sizeof(RIFFCHUNK))
1161 memset(&list, 0, sizeof(list));
1163 hr = IAsyncReader_SyncRead(This->pReader, pos, sizeof(list), (BYTE *)&list);
1164 if (list.fcc == ckidAVIOLDINDEX)
1166 pAviSplit->oldindex = CoTaskMemRealloc(pAviSplit->oldindex, list.cb + sizeof(RIFFCHUNK));
1167 if (pAviSplit->oldindex)
1169 hr = IAsyncReader_SyncRead(This->pReader, pos, sizeof(RIFFCHUNK) + list.cb, (BYTE *)pAviSplit->oldindex);
1170 if (hr == S_OK)
1172 hr = AVISplitter_ProcessOldIndex(pAviSplit);
1174 else
1176 CoTaskMemFree(pAviSplit->oldindex);
1177 pAviSplit->oldindex = NULL;
1178 hr = S_OK;
1184 indexes = 0;
1185 for (x = 0; x < pAviSplit->Parser.cStreams; ++x)
1186 if (pAviSplit->streams[x].entries)
1187 ++indexes;
1189 if (indexes)
1191 CoTaskMemFree(pAviSplit->oldindex);
1192 pAviSplit->oldindex = NULL;
1193 if (indexes < pAviSplit->Parser.cStreams)
1195 /* This error could possible be survived by switching to old type index,
1196 * but I would rather find out why it doesn't find everything here
1198 ERR("%d indexes expected, but only have %d\n", indexes, pAviSplit->Parser.cStreams);
1199 indexes = 0;
1202 else if (!indexes && pAviSplit->oldindex)
1203 indexes = pAviSplit->Parser.cStreams;
1205 if (!indexes && pAviSplit->AviHeader.dwFlags & AVIF_MUSTUSEINDEX)
1207 FIXME("No usable index was found!\n");
1208 hr = E_FAIL;
1211 /* Now, set up the streams */
1212 if (hr == S_OK)
1213 hr = AVISplitter_InitializeStreams(pAviSplit);
1215 if (hr != S_OK)
1217 AVISplitter_Disconnect(pAviSplit);
1218 return E_FAIL;
1221 TRACE("AVI File ok\n");
1223 return hr;
1226 static HRESULT AVISplitter_Flush(LPVOID iface)
1228 AVISplitterImpl *This = iface;
1229 DWORD x;
1230 ULONG ref;
1232 TRACE("(%p)->()\n", This);
1234 for (x = 0; x < This->Parser.cStreams; ++x)
1236 StreamData *stream = This->streams + x;
1238 if (stream->sample)
1240 ref = IMediaSample_Release(stream->sample);
1241 assert(ref == 0);
1243 stream->sample = NULL;
1245 ResetEvent(stream->packet_queued);
1246 assert(!stream->thread);
1249 return S_OK;
1252 static HRESULT AVISplitter_Disconnect(LPVOID iface)
1254 AVISplitterImpl *This = iface;
1255 ULONG x;
1257 /* TODO: Remove other memory that's allocated during connect */
1258 CoTaskMemFree(This->oldindex);
1259 This->oldindex = NULL;
1261 for (x = 0; x < This->Parser.cStreams; ++x)
1263 DWORD i;
1265 StreamData *stream = &This->streams[x];
1267 for (i = 0; i < stream->entries; ++i)
1268 CoTaskMemFree(stream->stdindex[i]);
1270 CoTaskMemFree(stream->stdindex);
1271 CloseHandle(stream->packet_queued);
1273 CoTaskMemFree(This->streams);
1274 This->streams = NULL;
1275 return S_OK;
1278 static ULONG WINAPI AVISplitter_Release(IBaseFilter *iface)
1280 AVISplitterImpl *This = (AVISplitterImpl *)iface;
1281 ULONG ref;
1283 ref = InterlockedDecrement(&This->Parser.filter.refCount);
1285 TRACE("(%p)->() Release from %d\n", This, ref + 1);
1287 if (!ref)
1289 AVISplitter_Flush(This);
1290 Parser_Destroy(&This->Parser);
1293 return ref;
1296 static HRESULT WINAPI AVISplitter_seek(IMediaSeeking *iface)
1298 AVISplitterImpl *This = impl_from_IMediaSeeking(iface);
1299 PullPin *pPin = This->Parser.pInputPin;
1300 LONGLONG newpos, endpos;
1301 DWORD x;
1303 newpos = This->Parser.sourceSeeking.llCurrent;
1304 endpos = This->Parser.sourceSeeking.llDuration;
1306 if (newpos > endpos)
1308 WARN("Requesting position %x%08x beyond end of stream %x%08x\n", (DWORD)(newpos>>32), (DWORD)newpos, (DWORD)(endpos>>32), (DWORD)endpos);
1309 return E_INVALIDARG;
1312 FIXME("Moving position to %u.%03u s!\n", (DWORD)(newpos / 10000000), (DWORD)((newpos / 10000)%1000));
1314 EnterCriticalSection(&pPin->thread_lock);
1315 /* Send a flush to all output pins */
1316 IPin_BeginFlush(&pPin->pin.IPin_iface);
1318 /* Make sure this is done while stopped, BeginFlush takes care of this */
1319 EnterCriticalSection(&This->Parser.filter.csFilter);
1320 for (x = 0; x < This->Parser.cStreams; ++x)
1322 Parser_OutputPin *pin = unsafe_impl_Parser_OutputPin_from_IPin(This->Parser.ppPins[1+x]);
1323 StreamData *stream = This->streams + x;
1324 LONGLONG wanted_frames;
1325 DWORD last_keyframe = 0, last_keyframeidx = 0, preroll = 0;
1327 wanted_frames = newpos;
1328 wanted_frames *= stream->streamheader.dwRate;
1329 wanted_frames /= 10000000;
1330 wanted_frames /= stream->streamheader.dwScale;
1332 pin->dwSamplesProcessed = 0;
1333 stream->index = 0;
1334 stream->pos = 0;
1335 stream->seek = TRUE;
1336 if (stream->stdindex)
1338 DWORD y, z = 0;
1340 for (y = 0; y < stream->entries; ++y)
1342 for (z = 0; z < stream->stdindex[y]->nEntriesInUse; ++z)
1344 if (stream->streamheader.dwSampleSize)
1346 ULONG len = stream->stdindex[y]->aIndex[z].dwSize & ~(1 << 31);
1347 ULONG size = stream->streamheader.dwSampleSize;
1349 pin->dwSamplesProcessed += len / size;
1350 if (len % size)
1351 ++pin->dwSamplesProcessed;
1353 else ++pin->dwSamplesProcessed;
1355 if (!(stream->stdindex[y]->aIndex[z].dwSize >> 31))
1357 last_keyframe = z;
1358 last_keyframeidx = y;
1359 preroll = 0;
1361 else
1362 ++preroll;
1364 if (pin->dwSamplesProcessed >= wanted_frames)
1365 break;
1367 if (pin->dwSamplesProcessed >= wanted_frames)
1368 break;
1370 stream->index = last_keyframeidx;
1371 stream->pos = last_keyframe;
1373 else
1375 DWORD nMax, n;
1376 nMax = This->oldindex->cb / sizeof(This->oldindex->aIndex[0]);
1378 for (n = 0; n < nMax; ++n)
1380 DWORD streamId = StreamFromFOURCC(This->oldindex->aIndex[n].dwChunkId);
1381 if (streamId != x)
1382 continue;
1384 if (stream->streamheader.dwSampleSize)
1386 ULONG len = This->oldindex->aIndex[n].dwSize;
1387 ULONG size = stream->streamheader.dwSampleSize;
1389 pin->dwSamplesProcessed += len / size;
1390 if (len % size)
1391 ++pin->dwSamplesProcessed;
1393 else ++pin->dwSamplesProcessed;
1395 if (This->oldindex->aIndex[n].dwFlags & AVIIF_KEYFRAME)
1397 last_keyframe = n;
1398 preroll = 0;
1400 else
1401 ++preroll;
1403 if (pin->dwSamplesProcessed >= wanted_frames)
1404 break;
1406 assert(n < nMax);
1407 stream->pos = last_keyframe;
1408 stream->index = 0;
1410 stream->preroll = preroll;
1411 stream->seek = TRUE;
1413 LeaveCriticalSection(&This->Parser.filter.csFilter);
1415 TRACE("Done flushing\n");
1416 IPin_EndFlush(&pPin->pin.IPin_iface);
1417 LeaveCriticalSection(&pPin->thread_lock);
1419 return S_OK;
1422 static const IBaseFilterVtbl AVISplitterImpl_Vtbl =
1424 Parser_QueryInterface,
1425 Parser_AddRef,
1426 AVISplitter_Release,
1427 Parser_GetClassID,
1428 Parser_Stop,
1429 Parser_Pause,
1430 Parser_Run,
1431 Parser_GetState,
1432 Parser_SetSyncSource,
1433 Parser_GetSyncSource,
1434 Parser_EnumPins,
1435 Parser_FindPin,
1436 Parser_QueryFilterInfo,
1437 Parser_JoinFilterGraph,
1438 Parser_QueryVendorInfo
1441 HRESULT AVISplitter_create(IUnknown * pUnkOuter, LPVOID * ppv)
1443 HRESULT hr;
1444 AVISplitterImpl * This;
1446 TRACE("(%p, %p)\n", pUnkOuter, ppv);
1448 *ppv = NULL;
1450 if (pUnkOuter)
1451 return CLASS_E_NOAGGREGATION;
1453 /* Note: This memory is managed by the transform filter once created */
1454 This = CoTaskMemAlloc(sizeof(AVISplitterImpl));
1456 This->streams = NULL;
1457 This->oldindex = NULL;
1459 hr = Parser_Create(&(This->Parser), &AVISplitterImpl_Vtbl, &CLSID_AviSplitter, AVISplitter_Sample, AVISplitter_QueryAccept, AVISplitter_InputPin_PreConnect, AVISplitter_Flush, AVISplitter_Disconnect, AVISplitter_first_request, AVISplitter_done_process, NULL, AVISplitter_seek, NULL);
1461 if (FAILED(hr))
1462 return hr;
1464 *ppv = This;
1466 return hr;