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