services: Check for services without lpBinaryPathName in get_winedevice_process.
[wine.git] / dlls / quartz / avisplit.c
bloba1b4fbe22fde39bada34904559f7ccd22151573e
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 & ~(1u << 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 IMediaSample_SetTime(sample, &rtSampleStart, &rtSampleStop);
264 hr = IAsyncReader_Request(pin->pReader, sample, streamnumber);
266 if (FAILED(hr))
268 ref = IMediaSample_Release(sample);
269 assert(ref == 0);
272 else
274 stream->sample = sample;
275 IMediaSample_SetActualDataLength(sample, 0);
276 SetEvent(stream->packet_queued);
279 else
281 if (sample)
283 ERR("There should be no sample!\n");
284 ref = IMediaSample_Release(sample);
285 assert(ref == 0);
288 TRACE("--> %08x\n", hr);
290 return hr;
293 static HRESULT AVISplitter_Receive(AVISplitterImpl *This, IMediaSample *sample, DWORD streamnumber)
295 Parser_OutputPin *pin = unsafe_impl_Parser_OutputPin_from_IPin(This->Parser.ppPins[1+streamnumber]);
296 HRESULT hr;
297 LONGLONG start, stop, rtstart, rtstop;
298 StreamData *stream = &This->streams[streamnumber];
300 start = pin->dwSamplesProcessed;
301 start *= stream->streamheader.dwScale;
302 start *= 10000000;
303 start /= stream->streamheader.dwRate;
305 if (stream->streamheader.dwSampleSize)
307 ULONG len = IMediaSample_GetActualDataLength(sample);
308 ULONG size = stream->streamheader.dwSampleSize;
310 pin->dwSamplesProcessed += len / size;
312 else
313 ++pin->dwSamplesProcessed;
315 stop = pin->dwSamplesProcessed;
316 stop *= stream->streamheader.dwScale;
317 stop *= 10000000;
318 stop /= stream->streamheader.dwRate;
320 if (IMediaSample_IsDiscontinuity(sample) == S_OK) {
321 IPin *victim;
322 EnterCriticalSection(&This->Parser.filter.csFilter);
323 pin->pin.pin.tStart = start;
324 pin->pin.pin.dRate = This->Parser.sourceSeeking.dRate;
325 hr = IPin_ConnectedTo(&pin->pin.pin.IPin_iface, &victim);
326 if (hr == S_OK)
328 hr = IPin_NewSegment(victim, start, This->Parser.sourceSeeking.llStop,
329 This->Parser.sourceSeeking.dRate);
330 if (hr != S_OK)
331 FIXME("NewSegment returns %08x\n", hr);
332 IPin_Release(victim);
334 LeaveCriticalSection(&This->Parser.filter.csFilter);
335 if (hr != S_OK)
336 return hr;
338 rtstart = (double)(start - pin->pin.pin.tStart) / pin->pin.pin.dRate;
339 rtstop = (double)(stop - pin->pin.pin.tStart) / pin->pin.pin.dRate;
340 IMediaSample_SetMediaTime(sample, &start, &stop);
341 IMediaSample_SetTime(sample, &rtstart, &rtstop);
342 IMediaSample_SetMediaTime(sample, &start, &stop);
344 hr = BaseOutputPinImpl_Deliver(&pin->pin, sample);
346 /* Uncomment this if you want to debug the time differences between the
347 * different streams, it is useful for that
349 FIXME("stream %u, hr: %08x, Start: %u.%03u, Stop: %u.%03u\n", streamnumber, hr,
350 (DWORD)(start / 10000000), (DWORD)((start / 10000)%1000),
351 (DWORD)(stop / 10000000), (DWORD)((stop / 10000)%1000));
353 return hr;
356 static DWORD WINAPI AVISplitter_thread_reader(LPVOID data)
358 struct thread_args *args = data;
359 AVISplitterImpl *This = args->This;
360 DWORD streamnumber = args->stream;
361 HRESULT hr = S_OK;
365 HRESULT nexthr = S_FALSE;
366 IMediaSample *sample;
368 WaitForSingleObject(This->streams[streamnumber].packet_queued, INFINITE);
369 sample = This->streams[streamnumber].sample;
370 This->streams[streamnumber].sample = NULL;
371 if (!sample)
372 break;
374 nexthr = AVISplitter_next_request(This, streamnumber);
376 hr = AVISplitter_Receive(This, sample, streamnumber);
377 if (hr != S_OK)
378 FIXME("Receiving error: %08x\n", hr);
380 IMediaSample_Release(sample);
381 if (hr == S_OK)
382 hr = nexthr;
383 if (nexthr == S_FALSE)
384 AVISplitter_SendEndOfFile(This, streamnumber);
385 } while (hr == S_OK);
387 if (hr != S_FALSE)
388 FIXME("Thread %u terminated with hr %08x!\n", streamnumber, hr);
389 else
390 TRACE("Thread %u terminated properly\n", streamnumber);
391 return hr;
394 static HRESULT AVISplitter_Sample(LPVOID iface, IMediaSample * pSample, DWORD_PTR cookie)
396 AVISplitterImpl *This = iface;
397 StreamData *stream = This->streams + cookie;
398 HRESULT hr = S_OK;
400 if (!IMediaSample_GetActualDataLength(pSample))
402 ERR("Received empty sample\n");
403 return S_OK;
406 /* Send the sample to whatever thread is appropriate
407 * That thread should also not have a sample queued at the moment
409 /* Debugging */
410 TRACE("(%p)->(%p size: %u, %lu)\n", This, pSample, IMediaSample_GetActualDataLength(pSample), cookie);
411 assert(cookie < This->Parser.cStreams);
412 assert(!stream->sample);
413 assert(WaitForSingleObject(stream->packet_queued, 0) == WAIT_TIMEOUT);
415 IMediaSample_AddRef(pSample);
417 stream->sample = pSample;
418 SetEvent(stream->packet_queued);
420 return hr;
423 static HRESULT AVISplitter_done_process(LPVOID iface);
425 /* On the first request we have to be sure that (cStreams-1) samples have
426 * already been processed, because otherwise some pins might not ever finish
427 * a Pause state change
429 static HRESULT AVISplitter_first_request(LPVOID iface)
431 AVISplitterImpl *This = iface;
432 HRESULT hr = S_OK;
433 DWORD x;
434 IMediaSample *sample = NULL;
435 BOOL have_sample = FALSE;
437 TRACE("(%p)->()\n", This);
439 for (x = 0; x < This->Parser.cStreams; ++x)
441 StreamData *stream = This->streams + x;
443 /* Nothing should be running at this point */
444 assert(!stream->thread);
446 assert(!sample);
447 /* It could be we asked the thread to terminate, and the thread
448 * already terminated before receiving the deathwish */
449 ResetEvent(stream->packet_queued);
451 stream->pos_next = stream->pos;
452 stream->index_next = stream->index;
454 /* This was sent after stopped->paused or stopped->playing, so set seek */
455 stream->seek = TRUE;
457 /* There should be a packet queued from AVISplitter_next_request last time
458 * It needs to be done now because this is the only way to ensure that every
459 * stream will have at least 1 packet processed
460 * If this is done after the threads start it could go all awkward and we
461 * would have no guarantees that it's successful at all
464 if (have_sample)
466 DWORD_PTR dwUser = ~0;
467 hr = IAsyncReader_WaitForNext(This->Parser.pInputPin->pReader, 10000, &sample, &dwUser);
468 assert(hr == S_OK);
469 assert(sample);
471 AVISplitter_Sample(iface, sample, dwUser);
472 IMediaSample_Release(sample);
475 hr = AVISplitter_next_request(This, x);
476 TRACE("-->%08x\n", hr);
478 /* Could be an EOF instead */
479 have_sample = (hr == S_OK);
480 if (hr == S_FALSE)
481 AVISplitter_SendEndOfFile(This, x);
483 if (FAILED(hr) && hr != VFW_E_NOT_CONNECTED)
484 break;
485 hr = S_OK;
488 /* FIXME: Don't do this for each pin that sent an EOF */
489 for (x = 0; x < This->Parser.cStreams && SUCCEEDED(hr); ++x)
491 struct thread_args *args;
492 DWORD tid;
494 if ((This->streams[x].stdindex && This->streams[x].index_next >= This->streams[x].entries) ||
495 (!This->streams[x].stdindex && This->streams[x].index_next))
497 This->streams[x].thread = NULL;
498 continue;
501 args = CoTaskMemAlloc(sizeof(*args));
502 args->This = This;
503 args->stream = x;
504 This->streams[x].thread = CreateThread(NULL, 0, AVISplitter_thread_reader, args, 0, &tid);
505 TRACE("Created stream %u thread 0x%08x\n", x, tid);
508 if (FAILED(hr))
509 ERR("Horsemen of the apocalypse came to bring error 0x%08x\n", hr);
511 return hr;
514 static HRESULT AVISplitter_done_process(LPVOID iface)
516 AVISplitterImpl *This = iface;
517 DWORD x;
518 ULONG ref;
520 for (x = 0; x < This->Parser.cStreams; ++x)
522 StreamData *stream = This->streams + x;
524 TRACE("Waiting for %u to terminate\n", x);
525 /* Make the thread return first */
526 SetEvent(stream->packet_queued);
527 assert(WaitForSingleObject(stream->thread, 100000) != WAIT_TIMEOUT);
528 CloseHandle(stream->thread);
529 stream->thread = NULL;
531 if (stream->sample)
533 ref = IMediaSample_Release(stream->sample);
534 assert(ref == 0);
536 stream->sample = NULL;
538 ResetEvent(stream->packet_queued);
540 TRACE("All threads are now terminated\n");
542 return S_OK;
545 static HRESULT AVISplitter_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
547 if (IsEqualIID(&pmt->majortype, &MEDIATYPE_Stream) && IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_Avi))
548 return S_OK;
549 return S_FALSE;
552 static HRESULT AVISplitter_ProcessIndex(AVISplitterImpl *This, AVISTDINDEX **index, LONGLONG qwOffset, DWORD cb)
554 AVISTDINDEX *pIndex;
555 DWORD x;
556 int rest;
558 *index = NULL;
559 if (cb < sizeof(AVISTDINDEX))
561 FIXME("size %u too small\n", cb);
562 return E_INVALIDARG;
565 pIndex = CoTaskMemAlloc(cb);
566 if (!pIndex)
567 return E_OUTOFMEMORY;
569 IAsyncReader_SyncRead((impl_PullPin_from_IPin(This->Parser.ppPins[0]))->pReader, qwOffset, cb, (BYTE *)pIndex);
570 rest = cb - sizeof(AVISUPERINDEX) + sizeof(RIFFCHUNK) + sizeof(pIndex->aIndex);
572 TRACE("FOURCC: %s\n", debugstr_an((char *)&pIndex->fcc, 4));
573 TRACE("wLongsPerEntry: %hd\n", pIndex->wLongsPerEntry);
574 TRACE("bIndexSubType: %u\n", pIndex->bIndexSubType);
575 TRACE("bIndexType: %u\n", pIndex->bIndexType);
576 TRACE("nEntriesInUse: %u\n", pIndex->nEntriesInUse);
577 TRACE("dwChunkId: %.4s\n", (char *)&pIndex->dwChunkId);
578 TRACE("qwBaseOffset: %x%08x\n", (DWORD)(pIndex->qwBaseOffset >> 32), (DWORD)pIndex->qwBaseOffset);
579 TRACE("dwReserved_3: %u\n", pIndex->dwReserved_3);
581 if (pIndex->bIndexType != AVI_INDEX_OF_CHUNKS
582 || pIndex->wLongsPerEntry != 2
583 || rest < (pIndex->nEntriesInUse * sizeof(DWORD) * pIndex->wLongsPerEntry)
584 || (pIndex->bIndexSubType != AVI_INDEX_SUB_DEFAULT))
586 FIXME("Invalid index chunk encountered: %u/%u, %u/%u, %u/%u, %u/%u\n",
587 pIndex->bIndexType, AVI_INDEX_OF_CHUNKS, pIndex->wLongsPerEntry, 2,
588 rest, (DWORD)(pIndex->nEntriesInUse * sizeof(DWORD) * pIndex->wLongsPerEntry),
589 pIndex->bIndexSubType, AVI_INDEX_SUB_DEFAULT);
590 *index = NULL;
591 return E_INVALIDARG;
594 for (x = 0; x < pIndex->nEntriesInUse; ++x)
596 BOOL keyframe = !(pIndex->aIndex[x].dwSize >> 31);
597 DWORDLONG offset = pIndex->qwBaseOffset + pIndex->aIndex[x].dwOffset;
598 TRACE("dwOffset: %x%08x\n", (DWORD)(offset >> 32), (DWORD)offset);
599 TRACE("dwSize: %u\n", (pIndex->aIndex[x].dwSize & ~(1u << 31)));
600 TRACE("Frame is a keyframe: %s\n", keyframe ? "yes" : "no");
603 *index = pIndex;
604 return S_OK;
607 static HRESULT AVISplitter_ProcessOldIndex(AVISplitterImpl *This)
609 ULONGLONG mov_pos = BYTES_FROM_MEDIATIME(This->CurrentChunkOffset) - sizeof(DWORD);
610 AVIOLDINDEX *pAviOldIndex = This->oldindex;
611 int relative = -1;
612 DWORD x;
614 for (x = 0; x < pAviOldIndex->cb / sizeof(pAviOldIndex->aIndex[0]); ++x)
616 DWORD temp, temp2 = 0, offset, chunkid;
617 PullPin *pin = This->Parser.pInputPin;
619 offset = pAviOldIndex->aIndex[x].dwOffset;
620 chunkid = pAviOldIndex->aIndex[x].dwChunkId;
622 TRACE("dwChunkId: %.4s\n", (char *)&chunkid);
623 TRACE("dwFlags: %08x\n", pAviOldIndex->aIndex[x].dwFlags);
624 TRACE("dwOffset (%s): %08x\n", relative ? "relative" : "absolute", offset);
625 TRACE("dwSize: %08x\n", pAviOldIndex->aIndex[x].dwSize);
627 /* Only scan once, or else this will take too long */
628 if (relative == -1)
630 IAsyncReader_SyncRead(pin->pReader, offset, sizeof(DWORD), (BYTE *)&temp);
631 relative = (chunkid != temp);
633 if (chunkid == mmioFOURCC('7','F','x','x')
634 && ((char *)&temp)[0] == 'i' && ((char *)&temp)[1] == 'x')
635 relative = FALSE;
637 if (relative)
639 if (offset + mov_pos < BYTES_FROM_MEDIATIME(This->EndOfFile))
640 IAsyncReader_SyncRead(pin->pReader, offset + mov_pos, sizeof(DWORD), (BYTE *)&temp2);
642 if (chunkid == mmioFOURCC('7','F','x','x')
643 && ((char *)&temp2)[0] == 'i' && ((char *)&temp2)[1] == 'x')
645 /* Do nothing, all is great */
647 else if (temp2 != chunkid)
649 ERR("Faulty index or bug in handling: Wanted FCC: %s, Abs FCC: %s (@ %x), Rel FCC: %s (@ %.0x%08x)\n",
650 debugstr_an((char *)&chunkid, 4), debugstr_an((char *)&temp, 4), offset,
651 debugstr_an((char *)&temp2, 4), (DWORD)((mov_pos + offset) >> 32), (DWORD)(mov_pos + offset));
652 relative = -1;
654 else
655 TRACE("Scanned dwChunkId: %s\n", debugstr_an((char *)&temp2, 4));
657 else if (!relative)
658 TRACE("Scanned dwChunkId: %s\n", debugstr_an((char *)&temp, 4));
660 /* Only dump one packet */
661 else break;
664 if (relative == -1)
666 FIXME("Dropping index: no idea whether it is relative or absolute\n");
667 CoTaskMemFree(This->oldindex);
668 This->oldindex = NULL;
670 else if (!relative)
671 This->offset = 0;
672 else
673 This->offset = (DWORD)mov_pos;
675 return S_OK;
678 static HRESULT AVISplitter_ProcessStreamList(AVISplitterImpl * This, const BYTE * pData, DWORD cb, ALLOCATOR_PROPERTIES *props)
680 PIN_INFO piOutput;
681 const RIFFCHUNK * pChunk;
682 HRESULT hr;
683 AM_MEDIA_TYPE amt;
684 float fSamplesPerSec = 0.0f;
685 DWORD dwSampleSize = 0;
686 DWORD dwLength = 0;
687 DWORD nstdindex = 0;
688 static const WCHAR wszStreamTemplate[] = {'S','t','r','e','a','m',' ','%','0','2','d',0};
689 StreamData *stream;
691 ZeroMemory(&amt, sizeof(amt));
692 piOutput.dir = PINDIR_OUTPUT;
693 piOutput.pFilter = &This->Parser.filter.IBaseFilter_iface;
694 wsprintfW(piOutput.achName, wszStreamTemplate, This->Parser.cStreams);
695 This->streams = CoTaskMemRealloc(This->streams, sizeof(StreamData) * (This->Parser.cStreams+1));
696 stream = This->streams + This->Parser.cStreams;
697 ZeroMemory(stream, sizeof(*stream));
699 for (pChunk = (const RIFFCHUNK *)pData;
700 ((const BYTE *)pChunk >= pData) && ((const BYTE *)pChunk + sizeof(RIFFCHUNK) < pData + cb) && (pChunk->cb > 0);
701 pChunk = (const RIFFCHUNK *)((const BYTE*)pChunk + sizeof(RIFFCHUNK) + pChunk->cb)
704 switch (pChunk->fcc)
706 case ckidSTREAMHEADER:
708 const AVISTREAMHEADER * pStrHdr = (const AVISTREAMHEADER *)pChunk;
709 TRACE("processing stream header\n");
710 stream->streamheader = *pStrHdr;
712 fSamplesPerSec = (float)pStrHdr->dwRate / (float)pStrHdr->dwScale;
713 CoTaskMemFree(amt.pbFormat);
714 amt.pbFormat = NULL;
715 amt.cbFormat = 0;
717 switch (pStrHdr->fccType)
719 case streamtypeVIDEO:
720 amt.formattype = FORMAT_VideoInfo;
721 break;
722 case streamtypeAUDIO:
723 amt.formattype = FORMAT_WaveFormatEx;
724 break;
725 default:
726 FIXME("fccType %.4s not handled yet\n", (const char *)&pStrHdr->fccType);
727 amt.formattype = FORMAT_None;
729 amt.majortype = MEDIATYPE_Video;
730 amt.majortype.Data1 = pStrHdr->fccType;
731 amt.subtype = MEDIATYPE_Video;
732 amt.subtype.Data1 = pStrHdr->fccHandler;
733 TRACE("Subtype FCC: %.04s\n", (LPCSTR)&pStrHdr->fccHandler);
734 amt.lSampleSize = pStrHdr->dwSampleSize;
735 amt.bFixedSizeSamples = (amt.lSampleSize != 0);
737 /* FIXME: Is this right? */
738 if (!amt.lSampleSize)
740 amt.lSampleSize = 1;
741 dwSampleSize = 1;
744 amt.bTemporalCompression = IsEqualGUID(&amt.majortype, &MEDIATYPE_Video); /* FIXME? */
745 dwSampleSize = pStrHdr->dwSampleSize;
746 dwLength = pStrHdr->dwLength;
747 if (!dwLength)
748 dwLength = This->AviHeader.dwTotalFrames;
750 if (pStrHdr->dwSuggestedBufferSize && pStrHdr->dwSuggestedBufferSize > props->cbBuffer)
751 props->cbBuffer = pStrHdr->dwSuggestedBufferSize;
753 break;
755 case ckidSTREAMFORMAT:
756 TRACE("processing stream format data\n");
757 if (IsEqualIID(&amt.formattype, &FORMAT_VideoInfo))
759 VIDEOINFOHEADER * pvi;
760 /* biCompression member appears to override the value in the stream header.
761 * i.e. the stream header can say something completely contradictory to what
762 * is in the BITMAPINFOHEADER! */
763 if (pChunk->cb < sizeof(BITMAPINFOHEADER))
765 ERR("Not enough bytes for BITMAPINFOHEADER\n");
766 return E_FAIL;
768 amt.cbFormat = sizeof(VIDEOINFOHEADER) - sizeof(BITMAPINFOHEADER) + pChunk->cb;
769 amt.pbFormat = CoTaskMemAlloc(amt.cbFormat);
770 ZeroMemory(amt.pbFormat, amt.cbFormat);
771 pvi = (VIDEOINFOHEADER *)amt.pbFormat;
772 pvi->AvgTimePerFrame = (LONGLONG)(10000000.0 / fSamplesPerSec);
774 CopyMemory(&pvi->bmiHeader, pChunk + 1, pChunk->cb);
775 if (pvi->bmiHeader.biCompression)
776 amt.subtype.Data1 = pvi->bmiHeader.biCompression;
778 else if (IsEqualIID(&amt.formattype, &FORMAT_WaveFormatEx))
780 amt.cbFormat = pChunk->cb;
781 if (amt.cbFormat < sizeof(WAVEFORMATEX))
782 amt.cbFormat = sizeof(WAVEFORMATEX);
783 amt.pbFormat = CoTaskMemAlloc(amt.cbFormat);
784 ZeroMemory(amt.pbFormat, amt.cbFormat);
785 CopyMemory(amt.pbFormat, pChunk + 1, pChunk->cb);
787 else
789 amt.cbFormat = pChunk->cb;
790 amt.pbFormat = CoTaskMemAlloc(amt.cbFormat);
791 CopyMemory(amt.pbFormat, pChunk + 1, amt.cbFormat);
793 break;
794 case ckidSTREAMNAME:
795 TRACE("processing stream name\n");
796 /* FIXME: this doesn't exactly match native version (we omit the "##)" prefix), but hey... */
797 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)(pChunk + 1), pChunk->cb, piOutput.achName, sizeof(piOutput.achName) / sizeof(piOutput.achName[0]));
798 break;
799 case ckidSTREAMHANDLERDATA:
800 FIXME("process stream handler data\n");
801 break;
802 case ckidAVIPADDING:
803 TRACE("JUNK chunk ignored\n");
804 break;
805 case ckidAVISUPERINDEX:
807 const AVISUPERINDEX *pIndex = (const AVISUPERINDEX *)pChunk;
808 DWORD x;
809 UINT rest = pIndex->cb - sizeof(AVISUPERINDEX) + sizeof(RIFFCHUNK) + sizeof(pIndex->aIndex[0]) * ANYSIZE_ARRAY;
811 if (pIndex->cb < sizeof(AVISUPERINDEX) - sizeof(RIFFCHUNK))
813 FIXME("size %u\n", pIndex->cb);
814 break;
817 if (nstdindex++ > 0)
819 ERR("Stream %d got more than 1 superindex?\n", This->Parser.cStreams);
820 break;
823 TRACE("wLongsPerEntry: %hd\n", pIndex->wLongsPerEntry);
824 TRACE("bIndexSubType: %u\n", pIndex->bIndexSubType);
825 TRACE("bIndexType: %u\n", pIndex->bIndexType);
826 TRACE("nEntriesInUse: %u\n", pIndex->nEntriesInUse);
827 TRACE("dwChunkId: %.4s\n", (const char *)&pIndex->dwChunkId);
828 if (pIndex->dwReserved[0])
829 TRACE("dwReserved[0]: %u\n", pIndex->dwReserved[0]);
830 if (pIndex->dwReserved[1])
831 TRACE("dwReserved[1]: %u\n", pIndex->dwReserved[1]);
832 if (pIndex->dwReserved[2])
833 TRACE("dwReserved[2]: %u\n", pIndex->dwReserved[2]);
835 if (pIndex->bIndexType != AVI_INDEX_OF_INDEXES
836 || pIndex->wLongsPerEntry != 4
837 || rest < (pIndex->nEntriesInUse * sizeof(DWORD) * pIndex->wLongsPerEntry)
838 || (pIndex->bIndexSubType != AVI_INDEX_SUB_2FIELD && pIndex->bIndexSubType != AVI_INDEX_SUB_DEFAULT))
840 FIXME("Invalid index chunk encountered\n");
841 break;
844 stream->entries = pIndex->nEntriesInUse;
845 stream->stdindex = CoTaskMemRealloc(stream->stdindex, sizeof(*stream->stdindex) * stream->entries);
846 for (x = 0; x < pIndex->nEntriesInUse; ++x)
848 TRACE("qwOffset: %x%08x\n", (DWORD)(pIndex->aIndex[x].qwOffset >> 32), (DWORD)pIndex->aIndex[x].qwOffset);
849 TRACE("dwSize: %u\n", pIndex->aIndex[x].dwSize);
850 TRACE("dwDuration: %u (unreliable)\n", pIndex->aIndex[x].dwDuration);
852 AVISplitter_ProcessIndex(This, &stream->stdindex[x], pIndex->aIndex[x].qwOffset, pIndex->aIndex[x].dwSize);
854 break;
856 default:
857 FIXME("unknown chunk type \"%.04s\" ignored\n", (LPCSTR)&pChunk->fcc);
861 if (IsEqualGUID(&amt.formattype, &FORMAT_WaveFormatEx))
863 amt.subtype = MEDIATYPE_Video;
864 amt.subtype.Data1 = ((WAVEFORMATEX *)amt.pbFormat)->wFormatTag;
867 dump_AM_MEDIA_TYPE(&amt);
868 TRACE("fSamplesPerSec = %f\n", (double)fSamplesPerSec);
869 TRACE("dwSampleSize = %x\n", dwSampleSize);
870 TRACE("dwLength = %x\n", dwLength);
872 stream->fSamplesPerSec = fSamplesPerSec;
873 stream->dwSampleSize = dwSampleSize;
874 stream->dwLength = dwLength; /* TODO: Use this for mediaseeking */
875 stream->packet_queued = CreateEventW(NULL, 0, 0, NULL);
877 hr = Parser_AddPin(&(This->Parser), &piOutput, props, &amt);
878 CoTaskMemFree(amt.pbFormat);
881 return hr;
884 static HRESULT AVISplitter_ProcessODML(AVISplitterImpl * This, const BYTE * pData, DWORD cb)
886 const RIFFCHUNK * pChunk;
888 for (pChunk = (const RIFFCHUNK *)pData;
889 ((const BYTE *)pChunk >= pData) && ((const BYTE *)pChunk + sizeof(RIFFCHUNK) < pData + cb) && (pChunk->cb > 0);
890 pChunk = (const RIFFCHUNK *)((const BYTE*)pChunk + sizeof(RIFFCHUNK) + pChunk->cb)
893 switch (pChunk->fcc)
895 case ckidAVIEXTHEADER:
897 int x;
898 const AVIEXTHEADER * pExtHdr = (const AVIEXTHEADER *)pChunk;
900 TRACE("processing extension header\n");
901 if (pExtHdr->cb != sizeof(AVIEXTHEADER) - sizeof(RIFFCHUNK))
903 FIXME("Size: %u\n", pExtHdr->cb);
904 break;
906 TRACE("dwGrandFrames: %u\n", pExtHdr->dwGrandFrames);
907 for (x = 0; x < 61; ++x)
908 if (pExtHdr->dwFuture[x])
909 FIXME("dwFuture[%i] = %u (0x%08x)\n", x, pExtHdr->dwFuture[x], pExtHdr->dwFuture[x]);
910 This->ExtHeader = *pExtHdr;
911 break;
913 default:
914 FIXME("unknown chunk type \"%.04s\" ignored\n", (LPCSTR)&pChunk->fcc);
918 return S_OK;
921 static HRESULT AVISplitter_InitializeStreams(AVISplitterImpl *This)
923 unsigned int x;
925 if (This->oldindex)
927 DWORD nMax, n;
929 for (x = 0; x < This->Parser.cStreams; ++x)
931 This->streams[x].frames = 0;
932 This->streams[x].pos = ~0;
933 This->streams[x].index = 0;
936 nMax = This->oldindex->cb / sizeof(This->oldindex->aIndex[0]);
938 /* Ok, maybe this is more of an exercise to see if I interpret everything correctly or not, but that is useful for now. */
939 for (n = 0; n < nMax; ++n)
941 DWORD streamId = StreamFromFOURCC(This->oldindex->aIndex[n].dwChunkId);
942 if (streamId >= This->Parser.cStreams)
944 FIXME("Stream id %s ignored\n", debugstr_an((char*)&This->oldindex->aIndex[n].dwChunkId, 4));
945 continue;
947 if (This->streams[streamId].pos == ~0U)
948 This->streams[streamId].pos = n;
950 if (This->streams[streamId].streamheader.dwSampleSize)
951 This->streams[streamId].frames += This->oldindex->aIndex[n].dwSize / This->streams[streamId].streamheader.dwSampleSize;
952 else
953 ++This->streams[streamId].frames;
956 for (x = 0; x < This->Parser.cStreams; ++x)
958 if ((DWORD)This->streams[x].frames != This->streams[x].streamheader.dwLength)
960 FIXME("stream %u: frames found: %u, frames meant to be found: %u\n", x, (DWORD)This->streams[x].frames, This->streams[x].streamheader.dwLength);
965 else if (!This->streams[0].entries)
967 for (x = 0; x < This->Parser.cStreams; ++x)
969 This->streams[x].frames = This->streams[x].streamheader.dwLength;
971 /* MS Avi splitter does seek through the whole file, we should! */
972 ERR("We should be manually seeking through the entire file to build an index, because the index is missing!!!\n");
973 return E_NOTIMPL;
976 /* Not much here yet */
977 for (x = 0; x < This->Parser.cStreams; ++x)
979 StreamData *stream = This->streams + x;
980 DWORD y;
981 DWORD64 frames = 0;
983 stream->seek = TRUE;
985 if (stream->stdindex)
987 stream->index = 0;
988 stream->pos = 0;
989 for (y = 0; y < stream->entries; ++y)
991 if (stream->streamheader.dwSampleSize)
993 DWORD z;
995 for (z = 0; z < stream->stdindex[y]->nEntriesInUse; ++z)
997 UINT len = stream->stdindex[y]->aIndex[z].dwSize & ~(1u << 31);
998 frames += len / stream->streamheader.dwSampleSize + !!(len % stream->streamheader.dwSampleSize);
1001 else
1002 frames += stream->stdindex[y]->nEntriesInUse;
1005 else frames = stream->frames;
1007 frames *= stream->streamheader.dwScale;
1008 /* Keep accuracy as high as possible for duration */
1009 This->Parser.sourceSeeking.llDuration = frames * 10000000;
1010 This->Parser.sourceSeeking.llDuration /= stream->streamheader.dwRate;
1011 This->Parser.sourceSeeking.llStop = This->Parser.sourceSeeking.llDuration;
1012 This->Parser.sourceSeeking.llCurrent = 0;
1014 frames /= stream->streamheader.dwRate;
1016 TRACE("Duration: %d days, %d hours, %d minutes and %d.%03u seconds\n", (DWORD)(frames / 86400),
1017 (DWORD)((frames % 86400) / 3600), (DWORD)((frames % 3600) / 60), (DWORD)(frames % 60),
1018 (DWORD)(This->Parser.sourceSeeking.llDuration/10000) % 1000);
1021 return S_OK;
1024 static HRESULT AVISplitter_Disconnect(LPVOID iface);
1026 /* FIXME: fix leaks on failure here */
1027 static HRESULT AVISplitter_InputPin_PreConnect(IPin * iface, IPin * pConnectPin, ALLOCATOR_PROPERTIES *props)
1029 PullPin *This = impl_PullPin_from_IPin(iface);
1030 HRESULT hr;
1031 RIFFLIST list;
1032 LONGLONG pos = 0; /* in bytes */
1033 BYTE * pBuffer;
1034 RIFFCHUNK * pCurrentChunk;
1035 LONGLONG total, avail;
1036 ULONG x;
1037 DWORD indexes;
1039 AVISplitterImpl * pAviSplit = (AVISplitterImpl *)This->pin.pinInfo.pFilter;
1041 hr = IAsyncReader_SyncRead(This->pReader, pos, sizeof(list), (BYTE *)&list);
1042 pos += sizeof(list);
1044 if (list.fcc != FOURCC_RIFF)
1046 ERR("Input stream not a RIFF file\n");
1047 return E_FAIL;
1049 if (list.fccListType != formtypeAVI)
1051 ERR("Input stream not an AVI RIFF file\n");
1052 return E_FAIL;
1055 hr = IAsyncReader_SyncRead(This->pReader, pos, sizeof(list), (BYTE *)&list);
1056 if (list.fcc != FOURCC_LIST)
1058 ERR("Expected LIST chunk, but got %.04s\n", (LPSTR)&list.fcc);
1059 return E_FAIL;
1061 if (list.fccListType != listtypeAVIHEADER)
1063 ERR("Header list expected. Got: %.04s\n", (LPSTR)&list.fccListType);
1064 return E_FAIL;
1067 pBuffer = HeapAlloc(GetProcessHeap(), 0, list.cb - sizeof(RIFFLIST) + sizeof(RIFFCHUNK));
1068 hr = IAsyncReader_SyncRead(This->pReader, pos + sizeof(list), list.cb - sizeof(RIFFLIST) + sizeof(RIFFCHUNK), pBuffer);
1070 pAviSplit->AviHeader.cb = 0;
1072 /* Stream list will set the buffer size here, so set a default and allow an override */
1073 props->cbBuffer = 0x20000;
1075 for (pCurrentChunk = (RIFFCHUNK *)pBuffer; (BYTE *)pCurrentChunk + sizeof(*pCurrentChunk) < pBuffer + list.cb; pCurrentChunk = (RIFFCHUNK *)(((BYTE *)pCurrentChunk) + sizeof(*pCurrentChunk) + pCurrentChunk->cb))
1077 RIFFLIST * pList;
1079 switch (pCurrentChunk->fcc)
1081 case ckidMAINAVIHEADER:
1082 /* AVIMAINHEADER includes the structure that is pCurrentChunk at the moment */
1083 memcpy(&pAviSplit->AviHeader, pCurrentChunk, sizeof(pAviSplit->AviHeader));
1084 break;
1085 case FOURCC_LIST:
1086 pList = (RIFFLIST *)pCurrentChunk;
1087 switch (pList->fccListType)
1089 case ckidSTREAMLIST:
1090 hr = AVISplitter_ProcessStreamList(pAviSplit, (BYTE *)pCurrentChunk + sizeof(RIFFLIST), pCurrentChunk->cb + sizeof(RIFFCHUNK) - sizeof(RIFFLIST), props);
1091 break;
1092 case ckidODML:
1093 hr = AVISplitter_ProcessODML(pAviSplit, (BYTE *)pCurrentChunk + sizeof(RIFFLIST), pCurrentChunk->cb + sizeof(RIFFCHUNK) - sizeof(RIFFLIST));
1094 break;
1096 break;
1097 case ckidAVIPADDING:
1098 /* ignore */
1099 break;
1100 default:
1101 FIXME("unrecognised header list type: %.04s\n", (LPSTR)&pCurrentChunk->fcc);
1104 HeapFree(GetProcessHeap(), 0, pBuffer);
1106 if (pAviSplit->AviHeader.cb != sizeof(pAviSplit->AviHeader) - sizeof(RIFFCHUNK))
1108 ERR("Avi Header wrong size!\n");
1109 return E_FAIL;
1112 /* Skip any chunks until we find the LIST chunk */
1115 pos += sizeof(RIFFCHUNK) + list.cb;
1116 hr = IAsyncReader_SyncRead(This->pReader, pos, sizeof(list), (BYTE *)&list);
1118 while (hr == S_OK && (list.fcc != FOURCC_LIST ||
1119 (list.fcc == FOURCC_LIST && list.fccListType != listtypeAVIMOVIE)));
1121 if (hr != S_OK)
1123 ERR("Failed to find LIST chunk from AVI file\n");
1124 return E_FAIL;
1127 IAsyncReader_Length(This->pReader, &total, &avail);
1129 /* FIXME: AVIX files are extended beyond the FOURCC chunk "AVI ", and thus won't be played here,
1130 * once I get one of the files I'll try to fix it */
1131 This->rtStart = pAviSplit->CurrentChunkOffset = MEDIATIME_FROM_BYTES(pos + sizeof(RIFFLIST));
1132 pos += list.cb + sizeof(RIFFCHUNK);
1134 pAviSplit->EndOfFile = This->rtStop = MEDIATIME_FROM_BYTES(pos);
1135 if (pos > total)
1137 ERR("File smaller (%x%08x) then EndOfFile (%x%08x)\n", (DWORD)(total >> 32), (DWORD)total, (DWORD)(pAviSplit->EndOfFile >> 32), (DWORD)pAviSplit->EndOfFile);
1138 return E_FAIL;
1141 hr = IAsyncReader_SyncRead(This->pReader, BYTES_FROM_MEDIATIME(pAviSplit->CurrentChunkOffset), sizeof(pAviSplit->CurrentChunk), (BYTE *)&pAviSplit->CurrentChunk);
1143 props->cbAlign = 1;
1144 props->cbPrefix = 0;
1145 /* Comrades, prevent shortage of buffers, or you will feel the consequences! DA! */
1146 props->cBuffers = 2 * pAviSplit->Parser.cStreams;
1148 /* Now peek into the idx1 index, if available */
1149 if (hr == S_OK && (total - pos) > sizeof(RIFFCHUNK))
1151 memset(&list, 0, sizeof(list));
1153 hr = IAsyncReader_SyncRead(This->pReader, pos, sizeof(list), (BYTE *)&list);
1154 if (list.fcc == ckidAVIOLDINDEX)
1156 pAviSplit->oldindex = CoTaskMemRealloc(pAviSplit->oldindex, list.cb + sizeof(RIFFCHUNK));
1157 if (pAviSplit->oldindex)
1159 hr = IAsyncReader_SyncRead(This->pReader, pos, sizeof(RIFFCHUNK) + list.cb, (BYTE *)pAviSplit->oldindex);
1160 if (hr == S_OK)
1162 hr = AVISplitter_ProcessOldIndex(pAviSplit);
1164 else
1166 CoTaskMemFree(pAviSplit->oldindex);
1167 pAviSplit->oldindex = NULL;
1168 hr = S_OK;
1174 indexes = 0;
1175 for (x = 0; x < pAviSplit->Parser.cStreams; ++x)
1176 if (pAviSplit->streams[x].entries)
1177 ++indexes;
1179 if (indexes)
1181 CoTaskMemFree(pAviSplit->oldindex);
1182 pAviSplit->oldindex = NULL;
1183 if (indexes < pAviSplit->Parser.cStreams)
1185 /* This error could possible be survived by switching to old type index,
1186 * but I would rather find out why it doesn't find everything here
1188 ERR("%d indexes expected, but only have %d\n", indexes, pAviSplit->Parser.cStreams);
1189 indexes = 0;
1192 else if (pAviSplit->oldindex)
1193 indexes = pAviSplit->Parser.cStreams;
1195 if (!indexes && pAviSplit->AviHeader.dwFlags & AVIF_MUSTUSEINDEX)
1197 FIXME("No usable index was found!\n");
1198 hr = E_FAIL;
1201 /* Now, set up the streams */
1202 if (hr == S_OK)
1203 hr = AVISplitter_InitializeStreams(pAviSplit);
1205 if (hr != S_OK)
1207 AVISplitter_Disconnect(pAviSplit);
1208 return E_FAIL;
1211 TRACE("AVI File ok\n");
1213 return hr;
1216 static HRESULT AVISplitter_Flush(LPVOID iface)
1218 AVISplitterImpl *This = iface;
1219 DWORD x;
1220 ULONG ref;
1222 TRACE("(%p)->()\n", This);
1224 for (x = 0; x < This->Parser.cStreams; ++x)
1226 StreamData *stream = This->streams + x;
1228 if (stream->sample)
1230 ref = IMediaSample_Release(stream->sample);
1231 assert(ref == 0);
1233 stream->sample = NULL;
1235 ResetEvent(stream->packet_queued);
1236 assert(!stream->thread);
1239 return S_OK;
1242 static HRESULT AVISplitter_Disconnect(LPVOID iface)
1244 AVISplitterImpl *This = iface;
1245 ULONG x;
1247 /* TODO: Remove other memory that's allocated during connect */
1248 CoTaskMemFree(This->oldindex);
1249 This->oldindex = NULL;
1251 for (x = 0; x < This->Parser.cStreams; ++x)
1253 DWORD i;
1255 StreamData *stream = &This->streams[x];
1257 for (i = 0; i < stream->entries; ++i)
1258 CoTaskMemFree(stream->stdindex[i]);
1260 CoTaskMemFree(stream->stdindex);
1261 CloseHandle(stream->packet_queued);
1263 CoTaskMemFree(This->streams);
1264 This->streams = NULL;
1265 return S_OK;
1268 static ULONG WINAPI AVISplitter_Release(IBaseFilter *iface)
1270 AVISplitterImpl *This = (AVISplitterImpl *)iface;
1271 ULONG ref;
1273 ref = InterlockedDecrement(&This->Parser.filter.refCount);
1275 TRACE("(%p)->() Release from %d\n", This, ref + 1);
1277 if (!ref)
1279 AVISplitter_Flush(This);
1280 Parser_Destroy(&This->Parser);
1283 return ref;
1286 static HRESULT WINAPI AVISplitter_seek(IMediaSeeking *iface)
1288 AVISplitterImpl *This = impl_from_IMediaSeeking(iface);
1289 PullPin *pPin = This->Parser.pInputPin;
1290 LONGLONG newpos, endpos;
1291 DWORD x;
1293 newpos = This->Parser.sourceSeeking.llCurrent;
1294 endpos = This->Parser.sourceSeeking.llDuration;
1296 if (newpos > endpos)
1298 WARN("Requesting position %x%08x beyond end of stream %x%08x\n", (DWORD)(newpos>>32), (DWORD)newpos, (DWORD)(endpos>>32), (DWORD)endpos);
1299 return E_INVALIDARG;
1302 FIXME("Moving position to %u.%03u s!\n", (DWORD)(newpos / 10000000), (DWORD)((newpos / 10000)%1000));
1304 EnterCriticalSection(&pPin->thread_lock);
1305 /* Send a flush to all output pins */
1306 IPin_BeginFlush(&pPin->pin.IPin_iface);
1308 /* Make sure this is done while stopped, BeginFlush takes care of this */
1309 EnterCriticalSection(&This->Parser.filter.csFilter);
1310 for (x = 0; x < This->Parser.cStreams; ++x)
1312 Parser_OutputPin *pin = unsafe_impl_Parser_OutputPin_from_IPin(This->Parser.ppPins[1+x]);
1313 StreamData *stream = This->streams + x;
1314 LONGLONG wanted_frames;
1315 DWORD last_keyframe = 0, last_keyframeidx = 0, preroll = 0;
1317 wanted_frames = newpos;
1318 wanted_frames *= stream->streamheader.dwRate;
1319 wanted_frames /= 10000000;
1320 wanted_frames /= stream->streamheader.dwScale;
1322 pin->dwSamplesProcessed = 0;
1323 stream->index = 0;
1324 stream->pos = 0;
1325 stream->seek = TRUE;
1326 if (stream->stdindex)
1328 DWORD y, z = 0;
1330 for (y = 0; y < stream->entries; ++y)
1332 for (z = 0; z < stream->stdindex[y]->nEntriesInUse; ++z)
1334 if (stream->streamheader.dwSampleSize)
1336 ULONG len = stream->stdindex[y]->aIndex[z].dwSize & ~(1u << 31);
1337 ULONG size = stream->streamheader.dwSampleSize;
1339 pin->dwSamplesProcessed += len / size;
1340 if (len % size)
1341 ++pin->dwSamplesProcessed;
1343 else ++pin->dwSamplesProcessed;
1345 if (!(stream->stdindex[y]->aIndex[z].dwSize >> 31))
1347 last_keyframe = z;
1348 last_keyframeidx = y;
1349 preroll = 0;
1351 else
1352 ++preroll;
1354 if (pin->dwSamplesProcessed >= wanted_frames)
1355 break;
1357 if (pin->dwSamplesProcessed >= wanted_frames)
1358 break;
1360 stream->index = last_keyframeidx;
1361 stream->pos = last_keyframe;
1363 else
1365 DWORD nMax, n;
1366 nMax = This->oldindex->cb / sizeof(This->oldindex->aIndex[0]);
1368 for (n = 0; n < nMax; ++n)
1370 DWORD streamId = StreamFromFOURCC(This->oldindex->aIndex[n].dwChunkId);
1371 if (streamId != x)
1372 continue;
1374 if (stream->streamheader.dwSampleSize)
1376 ULONG len = This->oldindex->aIndex[n].dwSize;
1377 ULONG size = stream->streamheader.dwSampleSize;
1379 pin->dwSamplesProcessed += len / size;
1380 if (len % size)
1381 ++pin->dwSamplesProcessed;
1383 else ++pin->dwSamplesProcessed;
1385 if (This->oldindex->aIndex[n].dwFlags & AVIIF_KEYFRAME)
1387 last_keyframe = n;
1388 preroll = 0;
1390 else
1391 ++preroll;
1393 if (pin->dwSamplesProcessed >= wanted_frames)
1394 break;
1396 assert(n < nMax);
1397 stream->pos = last_keyframe;
1398 stream->index = 0;
1400 stream->preroll = preroll;
1401 stream->seek = TRUE;
1403 LeaveCriticalSection(&This->Parser.filter.csFilter);
1405 TRACE("Done flushing\n");
1406 IPin_EndFlush(&pPin->pin.IPin_iface);
1407 LeaveCriticalSection(&pPin->thread_lock);
1409 return S_OK;
1412 static const IBaseFilterVtbl AVISplitterImpl_Vtbl =
1414 Parser_QueryInterface,
1415 Parser_AddRef,
1416 AVISplitter_Release,
1417 Parser_GetClassID,
1418 Parser_Stop,
1419 Parser_Pause,
1420 Parser_Run,
1421 Parser_GetState,
1422 Parser_SetSyncSource,
1423 Parser_GetSyncSource,
1424 Parser_EnumPins,
1425 Parser_FindPin,
1426 Parser_QueryFilterInfo,
1427 Parser_JoinFilterGraph,
1428 Parser_QueryVendorInfo
1431 HRESULT AVISplitter_create(IUnknown * pUnkOuter, LPVOID * ppv)
1433 HRESULT hr;
1434 AVISplitterImpl * This;
1436 TRACE("(%p, %p)\n", pUnkOuter, ppv);
1438 *ppv = NULL;
1440 if (pUnkOuter)
1441 return CLASS_E_NOAGGREGATION;
1443 /* Note: This memory is managed by the transform filter once created */
1444 This = CoTaskMemAlloc(sizeof(AVISplitterImpl));
1446 This->streams = NULL;
1447 This->oldindex = NULL;
1449 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);
1451 if (FAILED(hr))
1452 return hr;
1454 *ppv = &This->Parser.filter.IBaseFilter_iface;
1456 return hr;