user32/tests: Add some tests for UpdateLayeredWindow.
[wine] / dlls / quartz / avisplit.c
1 /*
2  * AVI Splitter Filter
3  *
4  * Copyright 2003 Robert Shearman
5  * Copyright 2004-2005 Christian Costa
6  * Copyright 2008 Maarten Lankhorst
7  *
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.
12  *
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.
17  *
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
21  */
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.
30  */
31
32 #include "quartz_private.h"
33 #include "pin.h"
34
35 #include "uuids.h"
36 #include "vfw.h"
37 #include "aviriff.h"
38 #include "vfwmsgs.h"
39 #include "amvideo.h"
40
41 #include "wine/unicode.h"
42 #include "wine/debug.h"
43
44 #include <math.h>
45 #include <assert.h>
46
47 #include "parser.h"
48
49 #define TWOCCFromFOURCC(fcc) HIWORD(fcc)
50
51 /* four character codes used in AVI files */
52 #define ckidINFO       mmioFOURCC('I','N','F','O')
53 #define ckidREC        mmioFOURCC('R','E','C',' ')
54
55 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
56
57 typedef struct StreamData
58 {
59     DWORD dwSampleSize;
60     FLOAT fSamplesPerSec;
61     DWORD dwLength;
62
63     AVISTREAMHEADER streamheader;
64     DWORD entries;
65     AVISTDINDEX **stdindex;
66     DWORD frames;
67     DWORD seek;
68
69     /* Position, in index units */
70     DWORD pos, pos_next, index, index_next;
71
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.
75      */
76     HANDLE thread, packet_queued;
77     IMediaSample *sample;
78
79     /* Amount of preroll samples for this stream */
80     DWORD preroll;
81 } StreamData;
82
83 typedef struct AVISplitterImpl
84 {
85     ParserImpl Parser;
86     RIFFCHUNK CurrentChunk;
87     LONGLONG CurrentChunkOffset; /* in media time */
88     LONGLONG EndOfFile;
89     AVIMAINHEADER AviHeader;
90     AVIEXTHEADER ExtHeader;
91
92     AVIOLDINDEX *oldindex;
93     DWORD offset;
94
95     StreamData *streams;
96 } AVISplitterImpl;
97
98 struct thread_args {
99     AVISplitterImpl *This;
100     DWORD stream;
101 };
102
103 static inline AVISplitterImpl *impl_from_IMediaSeeking( IMediaSeeking *iface )
104 {
105     return CONTAINING_RECORD(iface, AVISplitterImpl, Parser.sourceSeeking.IMediaSeeking_iface);
106 }
107
108 /* The threading stuff cries for an explanation
109  *
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.
113  *
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.
117  *
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
123  *
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.
126  */
127
128 static HRESULT AVISplitter_SendEndOfFile(AVISplitterImpl *This, DWORD streamnumber)
129 {
130     IPin* ppin = NULL;
131     HRESULT hr;
132
133     TRACE("End of file reached\n");
134
135     hr = IPin_ConnectedTo(This->Parser.ppPins[streamnumber+1], &ppin);
136     if (SUCCEEDED(hr))
137     {
138         hr = IPin_EndOfStream(ppin);
139         IPin_Release(ppin);
140     }
141     TRACE("--> %x\n", hr);
142
143     /* Force the pullpin thread to stop */
144     return S_FALSE;
145 }
146
147 /* Thread worker horse */
148 static HRESULT AVISplitter_next_request(AVISplitterImpl *This, DWORD streamnumber)
149 {
150     StreamData *stream = This->streams + streamnumber;
151     PullPin *pin = This->Parser.pInputPin;
152     IMediaSample *sample = NULL;
153     HRESULT hr;
154
155     TRACE("(%p, %u)->()\n", This, streamnumber);
156
157     hr = IMemAllocator_GetBuffer(pin->pAlloc, &sample, NULL, NULL, 0);
158     if (hr != S_OK)
159         ERR("... %08x?\n", hr);
160
161     if (SUCCEEDED(hr))
162     {
163         LONGLONG rtSampleStart;
164         /* Add 4 for the next header, which should hopefully work */
165         LONGLONG rtSampleStop;
166
167         stream->pos = stream->pos_next;
168         stream->index = stream->index_next;
169
170         IMediaSample_SetDiscontinuity(sample, stream->seek);
171         stream->seek = FALSE;
172         if (stream->preroll)
173         {
174             --stream->preroll;
175             IMediaSample_SetPreroll(sample, TRUE);
176         }
177         else
178             IMediaSample_SetPreroll(sample, FALSE);
179         IMediaSample_SetSyncPoint(sample, TRUE);
180
181         if (stream->stdindex)
182         {
183             AVISTDINDEX *index = stream->stdindex[stream->index];
184             AVISTDINDEX_ENTRY *entry = &index->aIndex[stream->pos];
185
186             /* End of file */
187             if (stream->index >= stream->entries)
188             {
189                 TRACE("END OF STREAM ON %u\n", streamnumber);
190                 IMediaSample_Release(sample);
191                 return S_FALSE;
192             }
193
194             rtSampleStart = index->qwBaseOffset;
195             rtSampleStart += entry->dwOffset;
196             rtSampleStart = MEDIATIME_FROM_BYTES(rtSampleStart);
197
198             ++stream->pos_next;
199             if (index->nEntriesInUse == stream->pos_next)
200             {
201                 stream->pos_next = 0;
202                 ++stream->index_next;
203             }
204
205             rtSampleStop = rtSampleStart + MEDIATIME_FROM_BYTES(entry->dwSize & ~(1 << 31));
206
207             TRACE("offset(%u) size(%u)\n", (DWORD)BYTES_FROM_MEDIATIME(rtSampleStart), (DWORD)BYTES_FROM_MEDIATIME(rtSampleStop - rtSampleStart));
208         }
209         else if (This->oldindex)
210         {
211             DWORD flags = This->oldindex->aIndex[stream->pos].dwFlags;
212             DWORD size = This->oldindex->aIndex[stream->pos].dwSize;
213
214             /* End of file */
215             if (stream->index)
216             {
217                 TRACE("END OF STREAM ON %u\n", streamnumber);
218                 IMediaSample_Release(sample);
219                 return S_FALSE;
220             }
221
222             rtSampleStart = MEDIATIME_FROM_BYTES(This->offset);
223             rtSampleStart += MEDIATIME_FROM_BYTES(This->oldindex->aIndex[stream->pos].dwOffset);
224             rtSampleStop = rtSampleStart + MEDIATIME_FROM_BYTES(size);
225             if (flags & AVIIF_MIDPART)
226             {
227                 FIXME("Only stand alone frames are currently handled correctly!\n");
228             }
229             if (flags & AVIIF_LIST)
230             {
231                 FIXME("Not sure if this is handled correctly\n");
232                 rtSampleStart += MEDIATIME_FROM_BYTES(sizeof(RIFFLIST));
233                 rtSampleStop += MEDIATIME_FROM_BYTES(sizeof(RIFFLIST));
234             }
235             else
236             {
237                 rtSampleStart += MEDIATIME_FROM_BYTES(sizeof(RIFFCHUNK));
238                 rtSampleStop += MEDIATIME_FROM_BYTES(sizeof(RIFFCHUNK));
239             }
240
241             /* Slow way of finding next index */
242             do {
243                 stream->pos_next++;
244             } while (stream->pos_next * sizeof(This->oldindex->aIndex[0]) < This->oldindex->cb
245                      && StreamFromFOURCC(This->oldindex->aIndex[stream->pos_next].dwChunkId) != streamnumber);
246
247             /* End of file soon */
248             if (stream->pos_next * sizeof(This->oldindex->aIndex[0]) >= This->oldindex->cb)
249             {
250                 stream->pos_next = 0;
251                 ++stream->index_next;
252             }
253         }
254         else /* TODO: Generate an index automagically */
255         {
256             ERR("CAN'T PLAY WITHOUT AN INDEX! SOS! SOS! SOS!\n");
257             assert(0);
258         }
259
260         if (rtSampleStart != rtSampleStop)
261         {
262             hr = IMediaSample_SetTime(sample, &rtSampleStart, &rtSampleStop);
263
264             hr = IAsyncReader_Request(pin->pReader, sample, streamnumber);
265
266             if (FAILED(hr))
267                 assert(IMediaSample_Release(sample) == 0);
268         }
269         else
270         {
271             stream->sample = sample;
272             IMediaSample_SetActualDataLength(sample, 0);
273             SetEvent(stream->packet_queued);
274         }
275     }
276     else
277     {
278         if (sample)
279         {
280             ERR("There should be no sample!\n");
281             assert(IMediaSample_Release(sample) == 0);
282         }
283     }
284     TRACE("--> %08x\n", hr);
285
286     return hr;
287 }
288
289 static HRESULT AVISplitter_Receive(AVISplitterImpl *This, IMediaSample *sample, DWORD streamnumber)
290 {
291     Parser_OutputPin *pin = unsafe_impl_Parser_OutputPin_from_IPin(This->Parser.ppPins[1+streamnumber]);
292     HRESULT hr;
293     LONGLONG start, stop, rtstart, rtstop;
294     StreamData *stream = &This->streams[streamnumber];
295
296     start = pin->dwSamplesProcessed;
297     start *= stream->streamheader.dwScale;
298     start *= 10000000;
299     start /= stream->streamheader.dwRate;
300
301     if (stream->streamheader.dwSampleSize)
302     {
303         ULONG len = IMediaSample_GetActualDataLength(sample);
304         ULONG size = stream->streamheader.dwSampleSize;
305
306         pin->dwSamplesProcessed += len / size;
307     }
308     else
309         ++pin->dwSamplesProcessed;
310
311     stop = pin->dwSamplesProcessed;
312     stop *= stream->streamheader.dwScale;
313     stop *= 10000000;
314     stop /= stream->streamheader.dwRate;
315
316     if (IMediaSample_IsDiscontinuity(sample) == S_OK) {
317         IPin *victim;
318         EnterCriticalSection(&This->Parser.filter.csFilter);
319         pin->pin.pin.tStart = start;
320         pin->pin.pin.dRate = This->Parser.sourceSeeking.dRate;
321         hr = IPin_ConnectedTo(&pin->pin.pin.IPin_iface, &victim);
322         if (hr == S_OK)
323         {
324             hr = IPin_NewSegment(victim, start, This->Parser.sourceSeeking.llStop,
325                                  This->Parser.sourceSeeking.dRate);
326             if (hr != S_OK)
327                 FIXME("NewSegment returns %08x\n", hr);
328             IPin_Release(victim);
329         }
330         LeaveCriticalSection(&This->Parser.filter.csFilter);
331         if (hr != S_OK)
332             return hr;
333     }
334     rtstart = (double)(start - pin->pin.pin.tStart) / pin->pin.pin.dRate;
335     rtstop = (double)(stop - pin->pin.pin.tStart) / pin->pin.pin.dRate;
336     hr = IMediaSample_SetMediaTime(sample, &start, &stop);
337     IMediaSample_SetTime(sample, &rtstart, &rtstop);
338     IMediaSample_SetMediaTime(sample, &start, &stop);
339
340     hr = BaseOutputPinImpl_Deliver(&pin->pin, sample);
341
342 /* Uncomment this if you want to debug the time differences between the
343  * different streams, it is useful for that
344  *
345     FIXME("stream %u, hr: %08x, Start: %u.%03u, Stop: %u.%03u\n", streamnumber, hr,
346            (DWORD)(start / 10000000), (DWORD)((start / 10000)%1000),
347            (DWORD)(stop / 10000000), (DWORD)((stop / 10000)%1000));
348 */
349     return hr;
350 }
351
352 static DWORD WINAPI AVISplitter_thread_reader(LPVOID data)
353 {
354     struct thread_args *args = data;
355     AVISplitterImpl *This = args->This;
356     DWORD streamnumber = args->stream;
357     HRESULT hr = S_OK;
358
359     do
360     {
361         HRESULT nexthr = S_FALSE;
362         IMediaSample *sample;
363
364         WaitForSingleObject(This->streams[streamnumber].packet_queued, INFINITE);
365         sample = This->streams[streamnumber].sample;
366         This->streams[streamnumber].sample = NULL;
367         if (!sample)
368             break;
369
370         nexthr = AVISplitter_next_request(This, streamnumber);
371
372         hr = AVISplitter_Receive(This, sample, streamnumber);
373         if (hr != S_OK)
374             FIXME("Receiving error: %08x\n", hr);
375
376         IMediaSample_Release(sample);
377         if (hr == S_OK)
378             hr = nexthr;
379         if (nexthr == S_FALSE)
380             AVISplitter_SendEndOfFile(This, streamnumber);
381     } while (hr == S_OK);
382
383     if (hr != S_FALSE)
384         FIXME("Thread %u terminated with hr %08x!\n", streamnumber, hr);
385     else
386         TRACE("Thread %u terminated properly\n", streamnumber);
387     return hr;
388 }
389
390 static HRESULT AVISplitter_Sample(LPVOID iface, IMediaSample * pSample, DWORD_PTR cookie)
391 {
392     AVISplitterImpl *This = iface;
393     StreamData *stream = This->streams + cookie;
394     HRESULT hr = S_OK;
395
396     if (!IMediaSample_GetActualDataLength(pSample))
397     {
398         ERR("Received empty sample\n");
399         return S_OK;
400     }
401
402     /* Send the sample to whatever thread is appropriate
403      * That thread should also not have a sample queued at the moment
404      */
405     /* Debugging */
406     TRACE("(%p)->(%p size: %u, %lu)\n", This, pSample, IMediaSample_GetActualDataLength(pSample), cookie);
407     assert(cookie < This->Parser.cStreams);
408     assert(!stream->sample);
409     assert(WaitForSingleObject(stream->packet_queued, 0) == WAIT_TIMEOUT);
410
411     IMediaSample_AddRef(pSample);
412
413     stream->sample = pSample;
414     SetEvent(stream->packet_queued);
415
416     return hr;
417 }
418
419 static HRESULT AVISplitter_done_process(LPVOID iface);
420
421 /* On the first request we have to be sure that (cStreams-1) samples have
422  * already been processed, because otherwise some pins might not ever finish
423  * a Pause state change
424  */
425 static HRESULT AVISplitter_first_request(LPVOID iface)
426 {
427     AVISplitterImpl *This = iface;
428     HRESULT hr = S_OK;
429     DWORD x;
430     IMediaSample *sample = NULL;
431     BOOL have_sample = FALSE;
432
433     TRACE("(%p)->()\n", This);
434
435     for (x = 0; x < This->Parser.cStreams; ++x)
436     {
437         StreamData *stream = This->streams + x;
438
439         /* Nothing should be running at this point */
440         assert(!stream->thread);
441
442         assert(!sample);
443         /* It could be we asked the thread to terminate, and the thread
444          * already terminated before receiving the deathwish */
445         ResetEvent(stream->packet_queued);
446
447         stream->pos_next = stream->pos;
448         stream->index_next = stream->index;
449
450         /* This was sent after stopped->paused or stopped->playing, so set seek */
451         stream->seek = 1;
452
453         /* There should be a packet queued from AVISplitter_next_request last time
454          * It needs to be done now because this is the only way to ensure that every
455          * stream will have at least 1 packet processed
456          * If this is done after the threads start it could go all awkward and we
457          * would have no guarantees that it's successful at all
458          */
459
460         if (have_sample)
461         {
462             DWORD_PTR dwUser = ~0;
463             hr = IAsyncReader_WaitForNext(This->Parser.pInputPin->pReader, 10000, &sample, &dwUser);
464             assert(hr == S_OK);
465             assert(sample);
466
467             AVISplitter_Sample(iface, sample, dwUser);
468             IMediaSample_Release(sample);
469         }
470
471         hr = AVISplitter_next_request(This, x);
472         TRACE("-->%08x\n", hr);
473
474         /* Could be an EOF instead */
475         have_sample = (hr == S_OK);
476         if (hr == S_FALSE)
477             AVISplitter_SendEndOfFile(This, x);
478
479         if (FAILED(hr) && hr != VFW_E_NOT_CONNECTED)
480             break;
481         hr = S_OK;
482     }
483
484     /* FIXME: Don't do this for each pin that sent an EOF */
485     for (x = 0; x < This->Parser.cStreams && SUCCEEDED(hr); ++x)
486     {
487         struct thread_args *args;
488         DWORD tid;
489
490         if ((This->streams[x].stdindex && This->streams[x].index_next >= This->streams[x].entries) ||
491             (!This->streams[x].stdindex && This->streams[x].index_next))
492         {
493             This->streams[x].thread = NULL;
494             continue;
495         }
496
497         args = CoTaskMemAlloc(sizeof(*args));
498         args->This = This;
499         args->stream = x;
500         This->streams[x].thread = CreateThread(NULL, 0, AVISplitter_thread_reader, args, 0, &tid);
501         TRACE("Created stream %u thread 0x%08x\n", x, tid);
502     }
503
504     if (FAILED(hr))
505         ERR("Horsemen of the apocalypse came to bring error 0x%08x\n", hr);
506
507     return hr;
508 }
509
510 static HRESULT AVISplitter_done_process(LPVOID iface)
511 {
512     AVISplitterImpl *This = iface;
513
514     DWORD x;
515
516     for (x = 0; x < This->Parser.cStreams; ++x)
517     {
518         StreamData *stream = This->streams + x;
519
520         TRACE("Waiting for %u to terminate\n", x);
521         /* Make the thread return first */
522         SetEvent(stream->packet_queued);
523         assert(WaitForSingleObject(stream->thread, 100000) != WAIT_TIMEOUT);
524         CloseHandle(stream->thread);
525         stream->thread = NULL;
526
527         if (stream->sample)
528             assert(IMediaSample_Release(stream->sample) == 0);
529         stream->sample = NULL;
530
531         ResetEvent(stream->packet_queued);
532     }
533     TRACE("All threads are now terminated\n");
534
535     return S_OK;
536 }
537
538 static HRESULT AVISplitter_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
539 {
540     if (IsEqualIID(&pmt->majortype, &MEDIATYPE_Stream) && IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_Avi))
541         return S_OK;
542     return S_FALSE;
543 }
544
545 static HRESULT AVISplitter_ProcessIndex(AVISplitterImpl *This, AVISTDINDEX **index, LONGLONG qwOffset, DWORD cb)
546 {
547     AVISTDINDEX *pIndex;
548     DWORD x;
549     int rest;
550
551     *index = NULL;
552     if (cb < sizeof(AVISTDINDEX))
553     {
554         FIXME("size %u too small\n", cb);
555         return E_INVALIDARG;
556     }
557
558     pIndex = CoTaskMemAlloc(cb);
559     if (!pIndex)
560         return E_OUTOFMEMORY;
561
562     IAsyncReader_SyncRead((impl_PullPin_from_IPin(This->Parser.ppPins[0]))->pReader, qwOffset, cb, (BYTE *)pIndex);
563     rest = cb - sizeof(AVISUPERINDEX) + sizeof(RIFFCHUNK) + sizeof(pIndex->aIndex);
564
565     TRACE("FOURCC: %s\n", debugstr_an((char *)&pIndex->fcc, 4));
566     TRACE("wLongsPerEntry: %hd\n", pIndex->wLongsPerEntry);
567     TRACE("bIndexSubType: %u\n", pIndex->bIndexSubType);
568     TRACE("bIndexType: %u\n", pIndex->bIndexType);
569     TRACE("nEntriesInUse: %u\n", pIndex->nEntriesInUse);
570     TRACE("dwChunkId: %.4s\n", (char *)&pIndex->dwChunkId);
571     TRACE("qwBaseOffset: %x%08x\n", (DWORD)(pIndex->qwBaseOffset >> 32), (DWORD)pIndex->qwBaseOffset);
572     TRACE("dwReserved_3: %u\n", pIndex->dwReserved_3);
573
574     if (pIndex->bIndexType != AVI_INDEX_OF_CHUNKS
575         || pIndex->wLongsPerEntry != 2
576         || rest < (pIndex->nEntriesInUse * sizeof(DWORD) * pIndex->wLongsPerEntry)
577         || (pIndex->bIndexSubType != AVI_INDEX_SUB_DEFAULT))
578     {
579         FIXME("Invalid index chunk encountered: %u/%u, %u/%u, %u/%u, %u/%u\n",
580               pIndex->bIndexType, AVI_INDEX_OF_CHUNKS, pIndex->wLongsPerEntry, 2,
581               rest, (DWORD)(pIndex->nEntriesInUse * sizeof(DWORD) * pIndex->wLongsPerEntry),
582               pIndex->bIndexSubType, AVI_INDEX_SUB_DEFAULT);
583         *index = NULL;
584         return E_INVALIDARG;
585     }
586
587     for (x = 0; x < pIndex->nEntriesInUse; ++x)
588     {
589         BOOL keyframe = !(pIndex->aIndex[x].dwSize >> 31);
590         DWORDLONG offset = pIndex->qwBaseOffset + pIndex->aIndex[x].dwOffset;
591         TRACE("dwOffset: %x%08x\n", (DWORD)(offset >> 32), (DWORD)offset);
592         TRACE("dwSize: %u\n", (pIndex->aIndex[x].dwSize & ~(1<<31)));
593         TRACE("Frame is a keyframe: %s\n", keyframe ? "yes" : "no");
594     }
595
596     *index = pIndex;
597     return S_OK;
598 }
599
600 static HRESULT AVISplitter_ProcessOldIndex(AVISplitterImpl *This)
601 {
602     ULONGLONG mov_pos = BYTES_FROM_MEDIATIME(This->CurrentChunkOffset) - sizeof(DWORD);
603     AVIOLDINDEX *pAviOldIndex = This->oldindex;
604     int relative = -1;
605     DWORD x;
606
607     for (x = 0; x < pAviOldIndex->cb / sizeof(pAviOldIndex->aIndex[0]); ++x)
608     {
609         DWORD temp, temp2 = 0, offset, chunkid;
610         PullPin *pin = This->Parser.pInputPin;
611
612         offset = pAviOldIndex->aIndex[x].dwOffset;
613         chunkid = pAviOldIndex->aIndex[x].dwChunkId;
614
615         TRACE("dwChunkId: %.4s\n", (char *)&chunkid);
616         TRACE("dwFlags: %08x\n", pAviOldIndex->aIndex[x].dwFlags);
617         TRACE("dwOffset (%s): %08x\n", relative ? "relative" : "absolute", offset);
618         TRACE("dwSize: %08x\n", pAviOldIndex->aIndex[x].dwSize);
619
620         /* Only scan once, or else this will take too long */
621         if (relative == -1)
622         {
623             IAsyncReader_SyncRead(pin->pReader, offset, sizeof(DWORD), (BYTE *)&temp);
624             relative = (chunkid != temp);
625
626             if (chunkid == mmioFOURCC('7','F','x','x')
627                 && ((char *)&temp)[0] == 'i' && ((char *)&temp)[1] == 'x')
628                 relative = FALSE;
629
630             if (relative)
631             {
632                 if (offset + mov_pos < BYTES_FROM_MEDIATIME(This->EndOfFile))
633                     IAsyncReader_SyncRead(pin->pReader, offset + mov_pos, sizeof(DWORD), (BYTE *)&temp2);
634
635                 if (chunkid == mmioFOURCC('7','F','x','x')
636                     && ((char *)&temp2)[0] == 'i' && ((char *)&temp2)[1] == 'x')
637                 {
638                     /* Do nothing, all is great */
639                 }
640                 else if (temp2 != chunkid)
641                 {
642                     ERR("Faulty index or bug in handling: Wanted FCC: %s, Abs FCC: %s (@ %x), Rel FCC: %s (@ %.0x%08x)\n",
643                         debugstr_an((char *)&chunkid, 4), debugstr_an((char *)&temp, 4), offset,
644                         debugstr_an((char *)&temp2, 4), (DWORD)((mov_pos + offset) >> 32), (DWORD)(mov_pos + offset));
645                     relative = -1;
646                 }
647                 else
648                     TRACE("Scanned dwChunkId: %s\n", debugstr_an((char *)&temp2, 4));
649             }
650             else if (!relative)
651                 TRACE("Scanned dwChunkId: %s\n", debugstr_an((char *)&temp, 4));
652         }
653         /* Only dump one packet */
654         else break;
655     }
656
657     if (relative == -1)
658     {
659         FIXME("Dropping index: no idea whether it is relative or absolute\n");
660         CoTaskMemFree(This->oldindex);
661         This->oldindex = NULL;
662     }
663     else if (!relative)
664         This->offset = 0;
665     else
666         This->offset = (DWORD)mov_pos;
667
668     return S_OK;
669 }
670
671 static HRESULT AVISplitter_ProcessStreamList(AVISplitterImpl * This, const BYTE * pData, DWORD cb, ALLOCATOR_PROPERTIES *props)
672 {
673     PIN_INFO piOutput;
674     const RIFFCHUNK * pChunk;
675     HRESULT hr;
676     AM_MEDIA_TYPE amt;
677     float fSamplesPerSec = 0.0f;
678     DWORD dwSampleSize = 0;
679     DWORD dwLength = 0;
680     DWORD nstdindex = 0;
681     static const WCHAR wszStreamTemplate[] = {'S','t','r','e','a','m',' ','%','0','2','d',0};
682     StreamData *stream;
683
684     ZeroMemory(&amt, sizeof(amt));
685     piOutput.dir = PINDIR_OUTPUT;
686     piOutput.pFilter = (IBaseFilter *)This;
687     wsprintfW(piOutput.achName, wszStreamTemplate, This->Parser.cStreams);
688     This->streams = CoTaskMemRealloc(This->streams, sizeof(StreamData) * (This->Parser.cStreams+1));
689     stream = This->streams + This->Parser.cStreams;
690     ZeroMemory(stream, sizeof(*stream));
691
692     for (pChunk = (const RIFFCHUNK *)pData; 
693          ((const BYTE *)pChunk >= pData) && ((const BYTE *)pChunk + sizeof(RIFFCHUNK) < pData + cb) && (pChunk->cb > 0); 
694          pChunk = (const RIFFCHUNK *)((const BYTE*)pChunk + sizeof(RIFFCHUNK) + pChunk->cb)     
695         )
696     {
697         switch (pChunk->fcc)
698         {
699         case ckidSTREAMHEADER:
700             {
701                 const AVISTREAMHEADER * pStrHdr = (const AVISTREAMHEADER *)pChunk;
702                 TRACE("processing stream header\n");
703                 stream->streamheader = *pStrHdr;
704
705                 fSamplesPerSec = (float)pStrHdr->dwRate / (float)pStrHdr->dwScale;
706                 CoTaskMemFree(amt.pbFormat);
707                 amt.pbFormat = NULL;
708                 amt.cbFormat = 0;
709
710                 switch (pStrHdr->fccType)
711                 {
712                 case streamtypeVIDEO:
713                     amt.formattype = FORMAT_VideoInfo;
714                     break;
715                 case streamtypeAUDIO:
716                     amt.formattype = FORMAT_WaveFormatEx;
717                     break;
718                 default:
719                     FIXME("fccType %.4s not handled yet\n", (const char *)&pStrHdr->fccType);
720                     amt.formattype = FORMAT_None;
721                 }
722                 amt.majortype = MEDIATYPE_Video;
723                 amt.majortype.Data1 = pStrHdr->fccType;
724                 amt.subtype = MEDIATYPE_Video;
725                 amt.subtype.Data1 = pStrHdr->fccHandler;
726                 TRACE("Subtype FCC: %.04s\n", (LPCSTR)&pStrHdr->fccHandler);
727                 amt.lSampleSize = pStrHdr->dwSampleSize;
728                 amt.bFixedSizeSamples = (amt.lSampleSize != 0);
729
730                 /* FIXME: Is this right? */
731                 if (!amt.lSampleSize)
732                 {
733                     amt.lSampleSize = 1;
734                     dwSampleSize = 1;
735                 }
736
737                 amt.bTemporalCompression = IsEqualGUID(&amt.majortype, &MEDIATYPE_Video); /* FIXME? */
738                 dwSampleSize = pStrHdr->dwSampleSize;
739                 dwLength = pStrHdr->dwLength;
740                 if (!dwLength)
741                     dwLength = This->AviHeader.dwTotalFrames;
742
743                 if (pStrHdr->dwSuggestedBufferSize && pStrHdr->dwSuggestedBufferSize > props->cbBuffer)
744                     props->cbBuffer = pStrHdr->dwSuggestedBufferSize;
745
746                 break;
747             }
748         case ckidSTREAMFORMAT:
749             TRACE("processing stream format data\n");
750             if (IsEqualIID(&amt.formattype, &FORMAT_VideoInfo))
751             {
752                 VIDEOINFOHEADER * pvi;
753                 /* biCompression member appears to override the value in the stream header.
754                  * i.e. the stream header can say something completely contradictory to what
755                  * is in the BITMAPINFOHEADER! */
756                 if (pChunk->cb < sizeof(BITMAPINFOHEADER))
757                 {
758                     ERR("Not enough bytes for BITMAPINFOHEADER\n");
759                     return E_FAIL;
760                 }
761                 amt.cbFormat = sizeof(VIDEOINFOHEADER) - sizeof(BITMAPINFOHEADER) + pChunk->cb;
762                 amt.pbFormat = CoTaskMemAlloc(amt.cbFormat);
763                 ZeroMemory(amt.pbFormat, amt.cbFormat);
764                 pvi = (VIDEOINFOHEADER *)amt.pbFormat;
765                 pvi->AvgTimePerFrame = (LONGLONG)(10000000.0 / fSamplesPerSec);
766
767                 CopyMemory(&pvi->bmiHeader, pChunk + 1, pChunk->cb);
768                 if (pvi->bmiHeader.biCompression)
769                     amt.subtype.Data1 = pvi->bmiHeader.biCompression;
770             }
771             else if (IsEqualIID(&amt.formattype, &FORMAT_WaveFormatEx))
772             {
773                 amt.cbFormat = pChunk->cb;
774                 if (amt.cbFormat < sizeof(WAVEFORMATEX))
775                     amt.cbFormat = sizeof(WAVEFORMATEX);
776                 amt.pbFormat = CoTaskMemAlloc(amt.cbFormat);
777                 ZeroMemory(amt.pbFormat, amt.cbFormat);
778                 CopyMemory(amt.pbFormat, pChunk + 1, pChunk->cb);
779             }
780             else
781             {
782                 amt.cbFormat = pChunk->cb;
783                 amt.pbFormat = CoTaskMemAlloc(amt.cbFormat);
784                 CopyMemory(amt.pbFormat, pChunk + 1, amt.cbFormat);
785             }
786             break;
787         case ckidSTREAMNAME:
788             TRACE("processing stream name\n");
789             /* FIXME: this doesn't exactly match native version (we omit the "##)" prefix), but hey... */
790             MultiByteToWideChar(CP_ACP, 0, (LPCSTR)(pChunk + 1), pChunk->cb, piOutput.achName, sizeof(piOutput.achName) / sizeof(piOutput.achName[0]));
791             break;
792         case ckidSTREAMHANDLERDATA:
793             FIXME("process stream handler data\n");
794             break;
795         case ckidAVIPADDING:
796             TRACE("JUNK chunk ignored\n");
797             break;
798         case ckidAVISUPERINDEX:
799         {
800             const AVISUPERINDEX *pIndex = (const AVISUPERINDEX *)pChunk;
801             DWORD x;
802             UINT rest = pIndex->cb - sizeof(AVISUPERINDEX) + sizeof(RIFFCHUNK) + sizeof(pIndex->aIndex[0]) * ANYSIZE_ARRAY;
803
804             if (pIndex->cb < sizeof(AVISUPERINDEX) - sizeof(RIFFCHUNK))
805             {
806                 FIXME("size %u\n", pIndex->cb);
807                 break;
808             }
809
810             if (nstdindex++ > 0)
811             {
812                 ERR("Stream %d got more than 1 superindex?\n", This->Parser.cStreams);
813                 break;
814             }
815
816             TRACE("wLongsPerEntry: %hd\n", pIndex->wLongsPerEntry);
817             TRACE("bIndexSubType: %u\n", pIndex->bIndexSubType);
818             TRACE("bIndexType: %u\n", pIndex->bIndexType);
819             TRACE("nEntriesInUse: %u\n", pIndex->nEntriesInUse);
820             TRACE("dwChunkId: %.4s\n", (const char *)&pIndex->dwChunkId);
821             if (pIndex->dwReserved[0])
822                 TRACE("dwReserved[0]: %u\n", pIndex->dwReserved[0]);
823             if (pIndex->dwReserved[2])
824                 TRACE("dwReserved[1]: %u\n", pIndex->dwReserved[1]);
825             if (pIndex->dwReserved[2])
826                 TRACE("dwReserved[2]: %u\n", pIndex->dwReserved[2]);
827
828             if (pIndex->bIndexType != AVI_INDEX_OF_INDEXES
829                 || pIndex->wLongsPerEntry != 4
830                 || rest < (pIndex->nEntriesInUse * sizeof(DWORD) * pIndex->wLongsPerEntry)
831                 || (pIndex->bIndexSubType != AVI_INDEX_SUB_2FIELD && pIndex->bIndexSubType != AVI_INDEX_SUB_DEFAULT))
832             {
833                 FIXME("Invalid index chunk encountered\n");
834                 break;
835             }
836
837             stream->entries = pIndex->nEntriesInUse;
838             stream->stdindex = CoTaskMemRealloc(stream->stdindex, sizeof(*stream->stdindex) * stream->entries);
839             for (x = 0; x < pIndex->nEntriesInUse; ++x)
840             {
841                 TRACE("qwOffset: %x%08x\n", (DWORD)(pIndex->aIndex[x].qwOffset >> 32), (DWORD)pIndex->aIndex[x].qwOffset);
842                 TRACE("dwSize: %u\n", pIndex->aIndex[x].dwSize);
843                 TRACE("dwDuration: %u (unreliable)\n", pIndex->aIndex[x].dwDuration);
844
845                 AVISplitter_ProcessIndex(This, &stream->stdindex[x], pIndex->aIndex[x].qwOffset, pIndex->aIndex[x].dwSize);
846             }
847             break;
848         }
849         default:
850             FIXME("unknown chunk type \"%.04s\" ignored\n", (LPCSTR)&pChunk->fcc);
851         }
852     }
853
854     if (IsEqualGUID(&amt.formattype, &FORMAT_WaveFormatEx))
855     {
856         amt.subtype = MEDIATYPE_Video;
857         amt.subtype.Data1 = ((WAVEFORMATEX *)amt.pbFormat)->wFormatTag;
858     }
859
860     dump_AM_MEDIA_TYPE(&amt);
861     TRACE("fSamplesPerSec = %f\n", (double)fSamplesPerSec);
862     TRACE("dwSampleSize = %x\n", dwSampleSize);
863     TRACE("dwLength = %x\n", dwLength);
864
865     stream->fSamplesPerSec = fSamplesPerSec;
866     stream->dwSampleSize = dwSampleSize;
867     stream->dwLength = dwLength; /* TODO: Use this for mediaseeking */
868     stream->packet_queued = CreateEventW(NULL, 0, 0, NULL);
869
870     hr = Parser_AddPin(&(This->Parser), &piOutput, props, &amt);
871     CoTaskMemFree(amt.pbFormat);
872
873
874     return hr;
875 }
876
877 static HRESULT AVISplitter_ProcessODML(AVISplitterImpl * This, const BYTE * pData, DWORD cb)
878 {
879     const RIFFCHUNK * pChunk;
880
881     for (pChunk = (const RIFFCHUNK *)pData;
882          ((const BYTE *)pChunk >= pData) && ((const BYTE *)pChunk + sizeof(RIFFCHUNK) < pData + cb) && (pChunk->cb > 0);
883          pChunk = (const RIFFCHUNK *)((const BYTE*)pChunk + sizeof(RIFFCHUNK) + pChunk->cb)
884         )
885     {
886         switch (pChunk->fcc)
887         {
888         case ckidAVIEXTHEADER:
889             {
890                 int x;
891                 const AVIEXTHEADER * pExtHdr = (const AVIEXTHEADER *)pChunk;
892
893                 TRACE("processing extension header\n");
894                 if (pExtHdr->cb != sizeof(AVIEXTHEADER) - sizeof(RIFFCHUNK))
895                 {
896                     FIXME("Size: %u\n", pExtHdr->cb);
897                     break;
898                 }
899                 TRACE("dwGrandFrames: %u\n", pExtHdr->dwGrandFrames);
900                 for (x = 0; x < 61; ++x)
901                     if (pExtHdr->dwFuture[x])
902                         FIXME("dwFuture[%i] = %u (0x%08x)\n", x, pExtHdr->dwFuture[x], pExtHdr->dwFuture[x]);
903                 This->ExtHeader = *pExtHdr;
904                 break;
905             }
906         default:
907             FIXME("unknown chunk type \"%.04s\" ignored\n", (LPCSTR)&pChunk->fcc);
908         }
909     }
910
911     return S_OK;
912 }
913
914 static HRESULT AVISplitter_InitializeStreams(AVISplitterImpl *This)
915 {
916     unsigned int x;
917
918     if (This->oldindex)
919     {
920         DWORD nMax, n;
921
922         for (x = 0; x < This->Parser.cStreams; ++x)
923         {
924             This->streams[x].frames = 0;
925             This->streams[x].pos = ~0;
926             This->streams[x].index = 0;
927         }
928
929         nMax = This->oldindex->cb / sizeof(This->oldindex->aIndex[0]);
930
931         /* Ok, maybe this is more of an exercise to see if I interpret everything correctly or not, but that is useful for now. */
932         for (n = 0; n < nMax; ++n)
933         {
934             DWORD streamId = StreamFromFOURCC(This->oldindex->aIndex[n].dwChunkId);
935             if (streamId >= This->Parser.cStreams)
936             {
937                 FIXME("Stream id %s ignored\n", debugstr_an((char*)&This->oldindex->aIndex[n].dwChunkId, 4));
938                 continue;
939             }
940             if (This->streams[streamId].pos == ~0U)
941                 This->streams[streamId].pos = n;
942
943             if (This->streams[streamId].streamheader.dwSampleSize)
944                 This->streams[streamId].frames += This->oldindex->aIndex[n].dwSize / This->streams[streamId].streamheader.dwSampleSize;
945             else
946                 ++This->streams[streamId].frames;
947         }
948
949         for (x = 0; x < This->Parser.cStreams; ++x)
950         {
951             if ((DWORD)This->streams[x].frames != This->streams[x].streamheader.dwLength)
952             {
953                 FIXME("stream %u: frames found: %u, frames meant to be found: %u\n", x, (DWORD)This->streams[x].frames, This->streams[x].streamheader.dwLength);
954             }
955         }
956
957     }
958     else if (!This->streams[0].entries)
959     {
960         for (x = 0; x < This->Parser.cStreams; ++x)
961         {
962             This->streams[x].frames = This->streams[x].streamheader.dwLength;
963         }
964         /* MS Avi splitter does seek through the whole file, we should! */
965         ERR("We should be manually seeking through the entire file to build an index, because the index is missing!!!\n");
966         return E_NOTIMPL;
967     }
968
969     /* Not much here yet */
970     for (x = 0; x < This->Parser.cStreams; ++x)
971     {
972         StreamData *stream = This->streams + x;
973         DWORD y;
974         DWORD64 frames = 0;
975
976         stream->seek = 1;
977
978         if (stream->stdindex)
979         {
980             stream->index = 0;
981             stream->pos = 0;
982             for (y = 0; y < stream->entries; ++y)
983             {
984                 if (stream->streamheader.dwSampleSize)
985                 {
986                     DWORD z;
987
988                     for (z = 0; z < stream->stdindex[y]->nEntriesInUse; ++z)
989                     {
990                         UINT len = stream->stdindex[y]->aIndex[z].dwSize & ~(1 << 31);
991                         frames += len / stream->streamheader.dwSampleSize + !!(len % stream->streamheader.dwSampleSize);
992                     }
993                 }
994                 else
995                     frames += stream->stdindex[y]->nEntriesInUse;
996             }
997         }
998         else frames = stream->frames;
999
1000         frames *= stream->streamheader.dwScale;
1001         /* Keep accuracy as high as possible for duration */
1002         This->Parser.sourceSeeking.llDuration = frames * 10000000;
1003         This->Parser.sourceSeeking.llDuration /= stream->streamheader.dwRate;
1004         This->Parser.sourceSeeking.llStop = This->Parser.sourceSeeking.llDuration;
1005         This->Parser.sourceSeeking.llCurrent = 0;
1006
1007         frames /= stream->streamheader.dwRate;
1008
1009         TRACE("Duration: %d days, %d hours, %d minutes and %d.%03u seconds\n", (DWORD)(frames / 86400),
1010         (DWORD)((frames % 86400) / 3600), (DWORD)((frames % 3600) / 60), (DWORD)(frames % 60),
1011         (DWORD)(This->Parser.sourceSeeking.llDuration/10000) % 1000);
1012     }
1013
1014     return S_OK;
1015 }
1016
1017 static HRESULT AVISplitter_Disconnect(LPVOID iface);
1018
1019 /* FIXME: fix leaks on failure here */
1020 static HRESULT AVISplitter_InputPin_PreConnect(IPin * iface, IPin * pConnectPin, ALLOCATOR_PROPERTIES *props)
1021 {
1022     PullPin *This = impl_PullPin_from_IPin(iface);
1023     HRESULT hr;
1024     RIFFLIST list;
1025     LONGLONG pos = 0; /* in bytes */
1026     BYTE * pBuffer;
1027     RIFFCHUNK * pCurrentChunk;
1028     LONGLONG total, avail;
1029     ULONG x;
1030     DWORD indexes;
1031
1032     AVISplitterImpl * pAviSplit = (AVISplitterImpl *)This->pin.pinInfo.pFilter;
1033
1034     hr = IAsyncReader_SyncRead(This->pReader, pos, sizeof(list), (BYTE *)&list);
1035     pos += sizeof(list);
1036
1037     if (list.fcc != FOURCC_RIFF)
1038     {
1039         ERR("Input stream not a RIFF file\n");
1040         return E_FAIL;
1041     }
1042     if (list.fccListType != formtypeAVI)
1043     {
1044         ERR("Input stream not an AVI RIFF file\n");
1045         return E_FAIL;
1046     }
1047
1048     hr = IAsyncReader_SyncRead(This->pReader, pos, sizeof(list), (BYTE *)&list);
1049     if (list.fcc != FOURCC_LIST)
1050     {
1051         ERR("Expected LIST chunk, but got %.04s\n", (LPSTR)&list.fcc);
1052         return E_FAIL;
1053     }
1054     if (list.fccListType != listtypeAVIHEADER)
1055     {
1056         ERR("Header list expected. Got: %.04s\n", (LPSTR)&list.fccListType);
1057         return E_FAIL;
1058     }
1059
1060     pBuffer = HeapAlloc(GetProcessHeap(), 0, list.cb - sizeof(RIFFLIST) + sizeof(RIFFCHUNK));
1061     hr = IAsyncReader_SyncRead(This->pReader, pos + sizeof(list), list.cb - sizeof(RIFFLIST) + sizeof(RIFFCHUNK), pBuffer);
1062
1063     pAviSplit->AviHeader.cb = 0;
1064
1065     /* Stream list will set the buffer size here, so set a default and allow an override */
1066     props->cbBuffer = 0x20000;
1067
1068     for (pCurrentChunk = (RIFFCHUNK *)pBuffer; (BYTE *)pCurrentChunk + sizeof(*pCurrentChunk) < pBuffer + list.cb; pCurrentChunk = (RIFFCHUNK *)(((BYTE *)pCurrentChunk) + sizeof(*pCurrentChunk) + pCurrentChunk->cb))
1069     {
1070         RIFFLIST * pList;
1071
1072         switch (pCurrentChunk->fcc)
1073         {
1074         case ckidMAINAVIHEADER:
1075             /* AVIMAINHEADER includes the structure that is pCurrentChunk at the moment */
1076             memcpy(&pAviSplit->AviHeader, pCurrentChunk, sizeof(pAviSplit->AviHeader));
1077             break;
1078         case FOURCC_LIST:
1079             pList = (RIFFLIST *)pCurrentChunk;
1080             switch (pList->fccListType)
1081             {
1082             case ckidSTREAMLIST:
1083                 hr = AVISplitter_ProcessStreamList(pAviSplit, (BYTE *)pCurrentChunk + sizeof(RIFFLIST), pCurrentChunk->cb + sizeof(RIFFCHUNK) - sizeof(RIFFLIST), props);
1084                 break;
1085             case ckidODML:
1086                 hr = AVISplitter_ProcessODML(pAviSplit, (BYTE *)pCurrentChunk + sizeof(RIFFLIST), pCurrentChunk->cb + sizeof(RIFFCHUNK) - sizeof(RIFFLIST));
1087                 break;
1088             }
1089             break;
1090         case ckidAVIPADDING:
1091             /* ignore */
1092             break;
1093         default:
1094             FIXME("unrecognised header list type: %.04s\n", (LPSTR)&pCurrentChunk->fcc);
1095         }
1096     }
1097     HeapFree(GetProcessHeap(), 0, pBuffer);
1098
1099     if (pAviSplit->AviHeader.cb != sizeof(pAviSplit->AviHeader) - sizeof(RIFFCHUNK))
1100     {
1101         ERR("Avi Header wrong size!\n");
1102         return E_FAIL;
1103     }
1104
1105     pos += sizeof(RIFFCHUNK) + list.cb;
1106     hr = IAsyncReader_SyncRead(This->pReader, pos, sizeof(list), (BYTE *)&list);
1107
1108     while (list.fcc == ckidAVIPADDING || (list.fcc == FOURCC_LIST && list.fccListType != listtypeAVIMOVIE))
1109     {
1110         pos += sizeof(RIFFCHUNK) + list.cb;
1111
1112         hr = IAsyncReader_SyncRead(This->pReader, pos, sizeof(list), (BYTE *)&list);
1113     }
1114
1115     if (list.fcc != FOURCC_LIST)
1116     {
1117         ERR("Expected LIST, but got %.04s\n", (LPSTR)&list.fcc);
1118         return E_FAIL;
1119     }
1120     if (list.fccListType != listtypeAVIMOVIE)
1121     {
1122         ERR("Expected AVI movie list, but got %.04s\n", (LPSTR)&list.fccListType);
1123         return E_FAIL;
1124     }
1125
1126     IAsyncReader_Length(This->pReader, &total, &avail);
1127
1128     /* FIXME: AVIX files are extended beyond the FOURCC chunk "AVI ", and thus won't be played here,
1129      * once I get one of the files I'll try to fix it */
1130     if (hr == S_OK)
1131     {
1132         This->rtStart = pAviSplit->CurrentChunkOffset = MEDIATIME_FROM_BYTES(pos + sizeof(RIFFLIST));
1133         pos += list.cb + sizeof(RIFFCHUNK);
1134
1135         pAviSplit->EndOfFile = This->rtStop = MEDIATIME_FROM_BYTES(pos);
1136         if (pos > total)
1137         {
1138             ERR("File smaller (%x%08x) then EndOfFile (%x%08x)\n", (DWORD)(total >> 32), (DWORD)total, (DWORD)(pAviSplit->EndOfFile >> 32), (DWORD)pAviSplit->EndOfFile);
1139             return E_FAIL;
1140         }
1141
1142         hr = IAsyncReader_SyncRead(This->pReader, BYTES_FROM_MEDIATIME(pAviSplit->CurrentChunkOffset), sizeof(pAviSplit->CurrentChunk), (BYTE *)&pAviSplit->CurrentChunk);
1143     }
1144
1145     props->cbAlign = 1;
1146     props->cbPrefix = 0;
1147     /* Comrades, prevent shortage of buffers, or you will feel the consequences! DA! */
1148     props->cBuffers = 2 * pAviSplit->Parser.cStreams;
1149
1150     /* Now peek into the idx1 index, if available */
1151     if (hr == S_OK && (total - pos) > sizeof(RIFFCHUNK))
1152     {
1153         memset(&list, 0, sizeof(list));
1154
1155         hr = IAsyncReader_SyncRead(This->pReader, pos, sizeof(list), (BYTE *)&list);
1156         if (list.fcc == ckidAVIOLDINDEX)
1157         {
1158             pAviSplit->oldindex = CoTaskMemRealloc(pAviSplit->oldindex, list.cb + sizeof(RIFFCHUNK));
1159             if (pAviSplit->oldindex)
1160             {
1161                 hr = IAsyncReader_SyncRead(This->pReader, pos, sizeof(RIFFCHUNK) + list.cb, (BYTE *)pAviSplit->oldindex);
1162                 if (hr == S_OK)
1163                 {
1164                     hr = AVISplitter_ProcessOldIndex(pAviSplit);
1165                 }
1166                 else
1167                 {
1168                     CoTaskMemFree(pAviSplit->oldindex);
1169                     pAviSplit->oldindex = NULL;
1170                     hr = S_OK;
1171                 }
1172             }
1173         }
1174     }
1175
1176     indexes = 0;
1177     for (x = 0; x < pAviSplit->Parser.cStreams; ++x)
1178         if (pAviSplit->streams[x].entries)
1179             ++indexes;
1180
1181     if (indexes)
1182     {
1183         CoTaskMemFree(pAviSplit->oldindex);
1184         pAviSplit->oldindex = NULL;
1185         if (indexes < pAviSplit->Parser.cStreams)
1186         {
1187             /* This error could possible be survived by switching to old type index,
1188              * but I would rather find out why it doesn't find everything here
1189              */
1190             ERR("%d indexes expected, but only have %d\n", indexes, pAviSplit->Parser.cStreams);
1191             indexes = 0;
1192         }
1193     }
1194     else if (!indexes && pAviSplit->oldindex)
1195         indexes = pAviSplit->Parser.cStreams;
1196
1197     if (!indexes && pAviSplit->AviHeader.dwFlags & AVIF_MUSTUSEINDEX)
1198     {
1199         FIXME("No usable index was found!\n");
1200         hr = E_FAIL;
1201     }
1202
1203     /* Now, set up the streams */
1204     if (hr == S_OK)
1205         hr = AVISplitter_InitializeStreams(pAviSplit);
1206
1207     if (hr != S_OK)
1208     {
1209         AVISplitter_Disconnect(pAviSplit);
1210         return E_FAIL;
1211     }
1212
1213     TRACE("AVI File ok\n");
1214
1215     return hr;
1216 }
1217
1218 static HRESULT AVISplitter_Flush(LPVOID iface)
1219 {
1220     AVISplitterImpl *This = iface;
1221     DWORD x;
1222
1223     TRACE("(%p)->()\n", This);
1224
1225     for (x = 0; x < This->Parser.cStreams; ++x)
1226     {
1227         StreamData *stream = This->streams + x;
1228
1229         if (stream->sample)
1230             assert(IMediaSample_Release(stream->sample) == 0);
1231         stream->sample = NULL;
1232
1233         ResetEvent(stream->packet_queued);
1234         assert(!stream->thread);
1235     }
1236
1237     return S_OK;
1238 }
1239
1240 static HRESULT AVISplitter_Disconnect(LPVOID iface)
1241 {
1242     AVISplitterImpl *This = iface;
1243     ULONG x;
1244
1245     /* TODO: Remove other memory that's allocated during connect */
1246     CoTaskMemFree(This->oldindex);
1247     This->oldindex = NULL;
1248
1249     for (x = 0; x < This->Parser.cStreams; ++x)
1250     {
1251         DWORD i;
1252
1253         StreamData *stream = &This->streams[x];
1254
1255         for (i = 0; i < stream->entries; ++i)
1256             CoTaskMemFree(stream->stdindex[i]);
1257
1258         CoTaskMemFree(stream->stdindex);
1259         CloseHandle(stream->packet_queued);
1260     }
1261     CoTaskMemFree(This->streams);
1262     This->streams = NULL;
1263     return S_OK;
1264 }
1265
1266 static ULONG WINAPI AVISplitter_Release(IBaseFilter *iface)
1267 {
1268     AVISplitterImpl *This = (AVISplitterImpl *)iface;
1269     ULONG ref;
1270
1271     ref = InterlockedDecrement(&This->Parser.filter.refCount);
1272
1273     TRACE("(%p)->() Release from %d\n", This, ref + 1);
1274
1275     if (!ref)
1276     {
1277         AVISplitter_Flush(This);
1278         Parser_Destroy(&This->Parser);
1279     }
1280
1281     return ref;
1282 }
1283
1284 static HRESULT WINAPI AVISplitter_seek(IMediaSeeking *iface)
1285 {
1286     AVISplitterImpl *This = impl_from_IMediaSeeking(iface);
1287     PullPin *pPin = This->Parser.pInputPin;
1288     LONGLONG newpos, endpos;
1289     DWORD x;
1290
1291     newpos = This->Parser.sourceSeeking.llCurrent;
1292     endpos = This->Parser.sourceSeeking.llDuration;
1293
1294     if (newpos > endpos)
1295     {
1296         WARN("Requesting position %x%08x beyond end of stream %x%08x\n", (DWORD)(newpos>>32), (DWORD)newpos, (DWORD)(endpos>>32), (DWORD)endpos);
1297         return E_INVALIDARG;
1298     }
1299
1300     FIXME("Moving position to %u.%03u s!\n", (DWORD)(newpos / 10000000), (DWORD)((newpos / 10000)%1000));
1301
1302     EnterCriticalSection(&pPin->thread_lock);
1303     /* Send a flush to all output pins */
1304     IPin_BeginFlush(&pPin->pin.IPin_iface);
1305
1306     /* Make sure this is done while stopped, BeginFlush takes care of this */
1307     EnterCriticalSection(&This->Parser.filter.csFilter);
1308     for (x = 0; x < This->Parser.cStreams; ++x)
1309     {
1310         Parser_OutputPin *pin = unsafe_impl_Parser_OutputPin_from_IPin(This->Parser.ppPins[1+x]);
1311         StreamData *stream = This->streams + x;
1312         LONGLONG wanted_frames;
1313         DWORD last_keyframe = 0, last_keyframeidx = 0, preroll = 0;
1314
1315         wanted_frames = newpos;
1316         wanted_frames *= stream->streamheader.dwRate;
1317         wanted_frames /= 10000000;
1318         wanted_frames /= stream->streamheader.dwScale;
1319
1320         pin->dwSamplesProcessed = 0;
1321         stream->index = 0;
1322         stream->pos = 0;
1323         stream->seek = 1;
1324         if (stream->stdindex)
1325         {
1326             DWORD y, z = 0;
1327
1328             for (y = 0; y < stream->entries; ++y)
1329             {
1330                 for (z = 0; z < stream->stdindex[y]->nEntriesInUse; ++z)
1331                 {
1332                     if (stream->streamheader.dwSampleSize)
1333                     {
1334                         ULONG len = stream->stdindex[y]->aIndex[z].dwSize & ~(1 << 31);
1335                         ULONG size = stream->streamheader.dwSampleSize;
1336
1337                         pin->dwSamplesProcessed += len / size;
1338                         if (len % size)
1339                             ++pin->dwSamplesProcessed;
1340                     }
1341                     else ++pin->dwSamplesProcessed;
1342
1343                     if (!(stream->stdindex[y]->aIndex[z].dwSize >> 31))
1344                     {
1345                         last_keyframe = z;
1346                         last_keyframeidx = y;
1347                         preroll = 0;
1348                     }
1349                     else
1350                         ++preroll;
1351
1352                     if (pin->dwSamplesProcessed >= wanted_frames)
1353                         break;
1354                 }
1355                 if (pin->dwSamplesProcessed >= wanted_frames)
1356                     break;
1357             }
1358             stream->index = last_keyframeidx;
1359             stream->pos = last_keyframe;
1360         }
1361         else
1362         {
1363             DWORD nMax, n;
1364             nMax = This->oldindex->cb / sizeof(This->oldindex->aIndex[0]);
1365
1366             for (n = 0; n < nMax; ++n)
1367             {
1368                 DWORD streamId = StreamFromFOURCC(This->oldindex->aIndex[n].dwChunkId);
1369                 if (streamId != x)
1370                     continue;
1371
1372                 if (stream->streamheader.dwSampleSize)
1373                 {
1374                     ULONG len = This->oldindex->aIndex[n].dwSize;
1375                     ULONG size = stream->streamheader.dwSampleSize;
1376
1377                     pin->dwSamplesProcessed += len / size;
1378                     if (len % size)
1379                         ++pin->dwSamplesProcessed;
1380                 }
1381                 else ++pin->dwSamplesProcessed;
1382
1383                 if (This->oldindex->aIndex[n].dwFlags & AVIIF_KEYFRAME)
1384                 {
1385                     last_keyframe = n;
1386                     preroll = 0;
1387                 }
1388                 else
1389                     ++preroll;
1390
1391                 if (pin->dwSamplesProcessed >= wanted_frames)
1392                     break;
1393             }
1394             assert(n < nMax);
1395             stream->pos = last_keyframe;
1396             stream->index = 0;
1397         }
1398         stream->preroll = preroll;
1399         stream->seek = 1;
1400     }
1401     LeaveCriticalSection(&This->Parser.filter.csFilter);
1402
1403     TRACE("Done flushing\n");
1404     IPin_EndFlush(&pPin->pin.IPin_iface);
1405     LeaveCriticalSection(&pPin->thread_lock);
1406
1407     return S_OK;
1408 }
1409
1410 static const IBaseFilterVtbl AVISplitterImpl_Vtbl =
1411 {
1412     Parser_QueryInterface,
1413     Parser_AddRef,
1414     AVISplitter_Release,
1415     Parser_GetClassID,
1416     Parser_Stop,
1417     Parser_Pause,
1418     Parser_Run,
1419     Parser_GetState,
1420     Parser_SetSyncSource,
1421     Parser_GetSyncSource,
1422     Parser_EnumPins,
1423     Parser_FindPin,
1424     Parser_QueryFilterInfo,
1425     Parser_JoinFilterGraph,
1426     Parser_QueryVendorInfo
1427 };
1428
1429 HRESULT AVISplitter_create(IUnknown * pUnkOuter, LPVOID * ppv)
1430 {
1431     HRESULT hr;
1432     AVISplitterImpl * This;
1433
1434     TRACE("(%p, %p)\n", pUnkOuter, ppv);
1435
1436     *ppv = NULL;
1437
1438     if (pUnkOuter)
1439         return CLASS_E_NOAGGREGATION;
1440
1441     /* Note: This memory is managed by the transform filter once created */
1442     This = CoTaskMemAlloc(sizeof(AVISplitterImpl));
1443
1444     This->streams = NULL;
1445     This->oldindex = NULL;
1446
1447     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);
1448
1449     if (FAILED(hr))
1450         return hr;
1451
1452     *ppv = This;
1453
1454     return hr;
1455 }