4 * Copyright 2003 Robert Shearman
5 * Copyright 2004-2005 Christian Costa
6 * Copyright 2008 Maarten Lankhorst
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 * - Reference leaks, if they still exist
24 * - Files without an index are not handled correctly yet.
25 * - When stopping/starting, a sample is lost. This should be compensated by
26 * keeping track of previous index/position.
27 * - Debugging channels are noisy at the moment, especially with thread
28 * related messages, however this is the only correct thing to do right now,
29 * since wine doesn't correctly handle all messages yet.
32 #include "quartz_private.h"
34 #define TWOCCFromFOURCC(fcc) HIWORD(fcc)
36 /* four character codes used in AVI files */
37 #define ckidINFO mmioFOURCC('I','N','F','O')
38 #define ckidREC mmioFOURCC('R','E','C',' ')
40 typedef struct StreamData
46 AVISTREAMHEADER streamheader
;
48 AVISTDINDEX
**stdindex
;
52 /* Position, in index units */
53 DWORD pos
, pos_next
, index
, index_next
;
55 /* Packet handling: a thread is created and waits on the packet event handle
56 * On an event acquire the sample lock, addref the sample and set it to NULL,
57 * then queue a new packet.
59 HANDLE thread
, packet_queued
;
62 /* Amount of preroll samples for this stream */
66 typedef struct AVISplitterImpl
69 RIFFCHUNK CurrentChunk
;
70 LONGLONG CurrentChunkOffset
; /* in media time */
72 AVIMAINHEADER AviHeader
;
73 AVIEXTHEADER ExtHeader
;
75 AVIOLDINDEX
*oldindex
;
82 AVISplitterImpl
*This
;
86 static inline AVISplitterImpl
*impl_from_IMediaSeeking( IMediaSeeking
*iface
)
88 return CONTAINING_RECORD(iface
, AVISplitterImpl
, Parser
.sourceSeeking
.IMediaSeeking_iface
);
91 /* The threading stuff cries for an explanation
93 * PullPin starts processing and calls AVISplitter_first_request
94 * AVISplitter_first_request creates a thread for each stream
95 * A stream can be audio, video, subtitles or something undefined.
97 * AVISplitter_first_request loads a single packet to each but one stream,
98 * and queues it for that last stream. This is to prevent WaitForNext to time
101 * The processing loop is entered. It calls IAsyncReader_WaitForNext in the
102 * PullPin. Every time it receives a packet, it will call AVISplitter_Sample
103 * AVISplitter_Sample will signal the relevant thread that a new sample is
104 * arrived, when that thread is ready it will read the packet and transmits
105 * it downstream with AVISplitter_Receive
107 * Threads terminate upon receiving NULL as packet or when ANY error code
108 * != S_OK occurs. This means that any error is fatal to processing.
111 static HRESULT
AVISplitter_SendEndOfFile(AVISplitterImpl
*This
, DWORD streamnumber
)
116 TRACE("End of file reached\n");
118 hr
= IPin_ConnectedTo(This
->Parser
.ppPins
[streamnumber
+1], &ppin
);
121 hr
= IPin_EndOfStream(ppin
);
124 TRACE("--> %x\n", hr
);
126 /* Force the pullpin thread to stop */
130 /* Thread worker horse */
131 static HRESULT
AVISplitter_next_request(AVISplitterImpl
*This
, DWORD streamnumber
)
133 StreamData
*stream
= This
->streams
+ streamnumber
;
134 PullPin
*pin
= This
->Parser
.pInputPin
;
135 IMediaSample
*sample
= NULL
;
139 TRACE("(%p, %u)->()\n", This
, streamnumber
);
141 hr
= IMemAllocator_GetBuffer(pin
->pAlloc
, &sample
, NULL
, NULL
, 0);
143 ERR("... %08x?\n", hr
);
147 LONGLONG rtSampleStart
;
148 /* Add 4 for the next header, which should hopefully work */
149 LONGLONG rtSampleStop
;
151 stream
->pos
= stream
->pos_next
;
152 stream
->index
= stream
->index_next
;
154 IMediaSample_SetDiscontinuity(sample
, stream
->seek
);
155 stream
->seek
= FALSE
;
159 IMediaSample_SetPreroll(sample
, TRUE
);
162 IMediaSample_SetPreroll(sample
, FALSE
);
163 IMediaSample_SetSyncPoint(sample
, TRUE
);
165 if (stream
->stdindex
)
167 AVISTDINDEX
*index
= stream
->stdindex
[stream
->index
];
168 AVISTDINDEX_ENTRY
*entry
= &index
->aIndex
[stream
->pos
];
171 if (stream
->index
>= stream
->entries
)
173 TRACE("END OF STREAM ON %u\n", streamnumber
);
174 IMediaSample_Release(sample
);
178 rtSampleStart
= index
->qwBaseOffset
;
179 rtSampleStart
+= entry
->dwOffset
;
180 rtSampleStart
= MEDIATIME_FROM_BYTES(rtSampleStart
);
183 if (index
->nEntriesInUse
== stream
->pos_next
)
185 stream
->pos_next
= 0;
186 ++stream
->index_next
;
189 rtSampleStop
= rtSampleStart
+ MEDIATIME_FROM_BYTES(entry
->dwSize
& ~(1u << 31));
191 TRACE("offset(%u) size(%u)\n", (DWORD
)BYTES_FROM_MEDIATIME(rtSampleStart
), (DWORD
)BYTES_FROM_MEDIATIME(rtSampleStop
- rtSampleStart
));
193 else if (This
->oldindex
)
195 DWORD flags
= This
->oldindex
->aIndex
[stream
->pos
].dwFlags
;
196 DWORD size
= This
->oldindex
->aIndex
[stream
->pos
].dwSize
;
201 TRACE("END OF STREAM ON %u\n", streamnumber
);
202 IMediaSample_Release(sample
);
206 rtSampleStart
= MEDIATIME_FROM_BYTES(This
->offset
);
207 rtSampleStart
+= MEDIATIME_FROM_BYTES(This
->oldindex
->aIndex
[stream
->pos
].dwOffset
);
208 rtSampleStop
= rtSampleStart
+ MEDIATIME_FROM_BYTES(size
);
209 if (flags
& AVIIF_MIDPART
)
211 FIXME("Only stand alone frames are currently handled correctly!\n");
213 if (flags
& AVIIF_LIST
)
215 FIXME("Not sure if this is handled correctly\n");
216 rtSampleStart
+= MEDIATIME_FROM_BYTES(sizeof(RIFFLIST
));
217 rtSampleStop
+= MEDIATIME_FROM_BYTES(sizeof(RIFFLIST
));
221 rtSampleStart
+= MEDIATIME_FROM_BYTES(sizeof(RIFFCHUNK
));
222 rtSampleStop
+= MEDIATIME_FROM_BYTES(sizeof(RIFFCHUNK
));
225 /* Slow way of finding next index */
228 } while (stream
->pos_next
* sizeof(This
->oldindex
->aIndex
[0]) < This
->oldindex
->cb
229 && StreamFromFOURCC(This
->oldindex
->aIndex
[stream
->pos_next
].dwChunkId
) != streamnumber
);
231 /* End of file soon */
232 if (stream
->pos_next
* sizeof(This
->oldindex
->aIndex
[0]) >= This
->oldindex
->cb
)
234 stream
->pos_next
= 0;
235 ++stream
->index_next
;
238 else /* TODO: Generate an index automagically */
240 ERR("CAN'T PLAY WITHOUT AN INDEX! SOS! SOS! SOS!\n");
244 if (rtSampleStart
!= rtSampleStop
)
246 IMediaSample_SetTime(sample
, &rtSampleStart
, &rtSampleStop
);
247 hr
= IAsyncReader_Request(pin
->pReader
, sample
, streamnumber
);
251 ref
= IMediaSample_Release(sample
);
257 stream
->sample
= sample
;
258 IMediaSample_SetActualDataLength(sample
, 0);
259 SetEvent(stream
->packet_queued
);
266 ERR("There should be no sample!\n");
267 ref
= IMediaSample_Release(sample
);
271 TRACE("--> %08x\n", hr
);
276 static HRESULT
AVISplitter_Receive(AVISplitterImpl
*This
, IMediaSample
*sample
, DWORD streamnumber
)
278 Parser_OutputPin
*pin
= unsafe_impl_Parser_OutputPin_from_IPin(This
->Parser
.ppPins
[1+streamnumber
]);
280 LONGLONG start
, stop
, rtstart
, rtstop
;
281 StreamData
*stream
= &This
->streams
[streamnumber
];
283 start
= pin
->dwSamplesProcessed
;
284 start
*= stream
->streamheader
.dwScale
;
286 start
/= stream
->streamheader
.dwRate
;
288 if (stream
->streamheader
.dwSampleSize
)
290 ULONG len
= IMediaSample_GetActualDataLength(sample
);
291 ULONG size
= stream
->streamheader
.dwSampleSize
;
293 pin
->dwSamplesProcessed
+= len
/ size
;
296 ++pin
->dwSamplesProcessed
;
298 stop
= pin
->dwSamplesProcessed
;
299 stop
*= stream
->streamheader
.dwScale
;
301 stop
/= stream
->streamheader
.dwRate
;
303 if (IMediaSample_IsDiscontinuity(sample
) == S_OK
) {
305 EnterCriticalSection(&This
->Parser
.filter
.csFilter
);
306 pin
->pin
.pin
.tStart
= start
;
307 pin
->pin
.pin
.dRate
= This
->Parser
.sourceSeeking
.dRate
;
308 hr
= IPin_ConnectedTo(&pin
->pin
.pin
.IPin_iface
, &victim
);
311 hr
= IPin_NewSegment(victim
, start
, This
->Parser
.sourceSeeking
.llStop
,
312 This
->Parser
.sourceSeeking
.dRate
);
314 FIXME("NewSegment returns %08x\n", hr
);
315 IPin_Release(victim
);
317 LeaveCriticalSection(&This
->Parser
.filter
.csFilter
);
321 rtstart
= (double)(start
- pin
->pin
.pin
.tStart
) / pin
->pin
.pin
.dRate
;
322 rtstop
= (double)(stop
- pin
->pin
.pin
.tStart
) / pin
->pin
.pin
.dRate
;
323 IMediaSample_SetMediaTime(sample
, &start
, &stop
);
324 IMediaSample_SetTime(sample
, &rtstart
, &rtstop
);
325 IMediaSample_SetMediaTime(sample
, &start
, &stop
);
327 hr
= BaseOutputPinImpl_Deliver(&pin
->pin
, sample
);
329 /* Uncomment this if you want to debug the time differences between the
330 * different streams, it is useful for that
332 FIXME("stream %u, hr: %08x, Start: %u.%03u, Stop: %u.%03u\n", streamnumber, hr,
333 (DWORD)(start / 10000000), (DWORD)((start / 10000)%1000),
334 (DWORD)(stop / 10000000), (DWORD)((stop / 10000)%1000));
339 static DWORD WINAPI
AVISplitter_thread_reader(LPVOID data
)
341 struct thread_args
*args
= data
;
342 AVISplitterImpl
*This
= args
->This
;
343 DWORD streamnumber
= args
->stream
;
348 HRESULT nexthr
= S_FALSE
;
349 IMediaSample
*sample
;
351 WaitForSingleObject(This
->streams
[streamnumber
].packet_queued
, INFINITE
);
352 sample
= This
->streams
[streamnumber
].sample
;
353 This
->streams
[streamnumber
].sample
= NULL
;
357 nexthr
= AVISplitter_next_request(This
, streamnumber
);
359 hr
= AVISplitter_Receive(This
, sample
, streamnumber
);
361 FIXME("Receiving error: %08x\n", hr
);
363 IMediaSample_Release(sample
);
366 if (nexthr
== S_FALSE
)
367 AVISplitter_SendEndOfFile(This
, streamnumber
);
368 } while (hr
== S_OK
);
371 FIXME("Thread %u terminated with hr %08x!\n", streamnumber
, hr
);
373 TRACE("Thread %u terminated properly\n", streamnumber
);
377 static HRESULT
AVISplitter_Sample(LPVOID iface
, IMediaSample
* pSample
, DWORD_PTR cookie
)
379 AVISplitterImpl
*This
= iface
;
380 StreamData
*stream
= This
->streams
+ cookie
;
383 if (!IMediaSample_GetActualDataLength(pSample
))
385 ERR("Received empty sample\n");
389 /* Send the sample to whatever thread is appropriate
390 * That thread should also not have a sample queued at the moment
393 TRACE("(%p)->(%p size: %u, %lu)\n", This
, pSample
, IMediaSample_GetActualDataLength(pSample
), cookie
);
394 assert(cookie
< This
->Parser
.cStreams
);
395 assert(!stream
->sample
);
396 assert(WaitForSingleObject(stream
->packet_queued
, 0) == WAIT_TIMEOUT
);
398 IMediaSample_AddRef(pSample
);
400 stream
->sample
= pSample
;
401 SetEvent(stream
->packet_queued
);
406 static HRESULT
AVISplitter_done_process(LPVOID iface
);
408 /* On the first request we have to be sure that (cStreams-1) samples have
409 * already been processed, because otherwise some pins might not ever finish
410 * a Pause state change
412 static HRESULT
AVISplitter_first_request(LPVOID iface
)
414 AVISplitterImpl
*This
= iface
;
417 IMediaSample
*sample
= NULL
;
418 BOOL have_sample
= FALSE
;
420 TRACE("(%p)->()\n", This
);
422 for (x
= 0; x
< This
->Parser
.cStreams
; ++x
)
424 StreamData
*stream
= This
->streams
+ x
;
426 /* Nothing should be running at this point */
427 assert(!stream
->thread
);
430 /* It could be we asked the thread to terminate, and the thread
431 * already terminated before receiving the deathwish */
432 ResetEvent(stream
->packet_queued
);
434 stream
->pos_next
= stream
->pos
;
435 stream
->index_next
= stream
->index
;
437 /* This was sent after stopped->paused or stopped->playing, so set seek */
440 /* There should be a packet queued from AVISplitter_next_request last time
441 * It needs to be done now because this is the only way to ensure that every
442 * stream will have at least 1 packet processed
443 * If this is done after the threads start it could go all awkward and we
444 * would have no guarantees that it's successful at all
449 DWORD_PTR dwUser
= ~0;
450 hr
= IAsyncReader_WaitForNext(This
->Parser
.pInputPin
->pReader
, 10000, &sample
, &dwUser
);
454 AVISplitter_Sample(iface
, sample
, dwUser
);
455 IMediaSample_Release(sample
);
458 hr
= AVISplitter_next_request(This
, x
);
459 TRACE("-->%08x\n", hr
);
461 /* Could be an EOF instead */
462 have_sample
= (hr
== S_OK
);
464 AVISplitter_SendEndOfFile(This
, x
);
466 if (FAILED(hr
) && hr
!= VFW_E_NOT_CONNECTED
)
471 /* FIXME: Don't do this for each pin that sent an EOF */
472 for (x
= 0; x
< This
->Parser
.cStreams
&& SUCCEEDED(hr
); ++x
)
474 struct thread_args
*args
;
477 if ((This
->streams
[x
].stdindex
&& This
->streams
[x
].index_next
>= This
->streams
[x
].entries
) ||
478 (!This
->streams
[x
].stdindex
&& This
->streams
[x
].index_next
))
480 This
->streams
[x
].thread
= NULL
;
484 args
= CoTaskMemAlloc(sizeof(*args
));
487 This
->streams
[x
].thread
= CreateThread(NULL
, 0, AVISplitter_thread_reader
, args
, 0, &tid
);
488 TRACE("Created stream %u thread 0x%08x\n", x
, tid
);
492 ERR("Horsemen of the apocalypse came to bring error 0x%08x\n", hr
);
497 static HRESULT
AVISplitter_done_process(LPVOID iface
)
499 AVISplitterImpl
*This
= iface
;
503 for (x
= 0; x
< This
->Parser
.cStreams
; ++x
)
505 StreamData
*stream
= This
->streams
+ x
;
507 TRACE("Waiting for %u to terminate\n", x
);
508 /* Make the thread return first */
509 SetEvent(stream
->packet_queued
);
510 assert(WaitForSingleObject(stream
->thread
, 100000) != WAIT_TIMEOUT
);
511 CloseHandle(stream
->thread
);
512 stream
->thread
= NULL
;
516 ref
= IMediaSample_Release(stream
->sample
);
519 stream
->sample
= NULL
;
521 ResetEvent(stream
->packet_queued
);
523 TRACE("All threads are now terminated\n");
528 static HRESULT
AVISplitter_QueryAccept(LPVOID iface
, const AM_MEDIA_TYPE
* pmt
)
530 if (IsEqualIID(&pmt
->majortype
, &MEDIATYPE_Stream
) && IsEqualIID(&pmt
->subtype
, &MEDIASUBTYPE_Avi
))
535 static HRESULT
AVISplitter_ProcessIndex(AVISplitterImpl
*This
, AVISTDINDEX
**index
, LONGLONG qwOffset
, DWORD cb
)
542 if (cb
< sizeof(AVISTDINDEX
))
544 FIXME("size %u too small\n", cb
);
548 pIndex
= CoTaskMemAlloc(cb
);
550 return E_OUTOFMEMORY
;
552 IAsyncReader_SyncRead((impl_PullPin_from_IPin(This
->Parser
.ppPins
[0]))->pReader
, qwOffset
, cb
, (BYTE
*)pIndex
);
553 rest
= cb
- sizeof(AVISUPERINDEX
) + sizeof(RIFFCHUNK
) + sizeof(pIndex
->aIndex
);
555 TRACE("FOURCC: %s\n", debugstr_an((char *)&pIndex
->fcc
, 4));
556 TRACE("wLongsPerEntry: %hd\n", pIndex
->wLongsPerEntry
);
557 TRACE("bIndexSubType: %u\n", pIndex
->bIndexSubType
);
558 TRACE("bIndexType: %u\n", pIndex
->bIndexType
);
559 TRACE("nEntriesInUse: %u\n", pIndex
->nEntriesInUse
);
560 TRACE("dwChunkId: %.4s\n", (char *)&pIndex
->dwChunkId
);
561 TRACE("qwBaseOffset: %s\n", wine_dbgstr_longlong(pIndex
->qwBaseOffset
));
562 TRACE("dwReserved_3: %u\n", pIndex
->dwReserved_3
);
564 if (pIndex
->bIndexType
!= AVI_INDEX_OF_CHUNKS
565 || pIndex
->wLongsPerEntry
!= 2
566 || rest
< (pIndex
->nEntriesInUse
* sizeof(DWORD
) * pIndex
->wLongsPerEntry
)
567 || (pIndex
->bIndexSubType
!= AVI_INDEX_SUB_DEFAULT
))
569 FIXME("Invalid index chunk encountered: %u/%u, %u/%u, %u/%u, %u/%u\n",
570 pIndex
->bIndexType
, AVI_INDEX_OF_CHUNKS
, pIndex
->wLongsPerEntry
, 2,
571 rest
, (DWORD
)(pIndex
->nEntriesInUse
* sizeof(DWORD
) * pIndex
->wLongsPerEntry
),
572 pIndex
->bIndexSubType
, AVI_INDEX_SUB_DEFAULT
);
577 for (x
= 0; x
< pIndex
->nEntriesInUse
; ++x
)
579 BOOL keyframe
= !(pIndex
->aIndex
[x
].dwSize
>> 31);
580 DWORDLONG offset
= pIndex
->qwBaseOffset
+ pIndex
->aIndex
[x
].dwOffset
;
581 TRACE("dwOffset: %s\n", wine_dbgstr_longlong(offset
));
582 TRACE("dwSize: %u\n", (pIndex
->aIndex
[x
].dwSize
& ~(1u << 31)));
583 TRACE("Frame is a keyframe: %s\n", keyframe
? "yes" : "no");
590 static HRESULT
AVISplitter_ProcessOldIndex(AVISplitterImpl
*This
)
592 ULONGLONG mov_pos
= BYTES_FROM_MEDIATIME(This
->CurrentChunkOffset
) - sizeof(DWORD
);
593 AVIOLDINDEX
*pAviOldIndex
= This
->oldindex
;
597 for (x
= 0; x
< pAviOldIndex
->cb
/ sizeof(pAviOldIndex
->aIndex
[0]); ++x
)
599 DWORD temp
, temp2
= 0, offset
, chunkid
;
600 PullPin
*pin
= This
->Parser
.pInputPin
;
602 offset
= pAviOldIndex
->aIndex
[x
].dwOffset
;
603 chunkid
= pAviOldIndex
->aIndex
[x
].dwChunkId
;
605 TRACE("dwChunkId: %.4s\n", (char *)&chunkid
);
606 TRACE("dwFlags: %08x\n", pAviOldIndex
->aIndex
[x
].dwFlags
);
607 TRACE("dwOffset (%s): %08x\n", relative
? "relative" : "absolute", offset
);
608 TRACE("dwSize: %08x\n", pAviOldIndex
->aIndex
[x
].dwSize
);
610 /* Only scan once, or else this will take too long */
613 IAsyncReader_SyncRead(pin
->pReader
, offset
, sizeof(DWORD
), (BYTE
*)&temp
);
614 relative
= (chunkid
!= temp
);
616 if (chunkid
== mmioFOURCC('7','F','x','x')
617 && ((char *)&temp
)[0] == 'i' && ((char *)&temp
)[1] == 'x')
622 if (offset
+ mov_pos
< BYTES_FROM_MEDIATIME(This
->EndOfFile
))
623 IAsyncReader_SyncRead(pin
->pReader
, offset
+ mov_pos
, sizeof(DWORD
), (BYTE
*)&temp2
);
625 if (chunkid
== mmioFOURCC('7','F','x','x')
626 && ((char *)&temp2
)[0] == 'i' && ((char *)&temp2
)[1] == 'x')
628 /* Do nothing, all is great */
630 else if (temp2
!= chunkid
)
632 ERR("Faulty index or bug in handling: Wanted FCC: %s, Abs FCC: %s (@ %x), Rel FCC: %s (@ %s)\n",
633 debugstr_an((char *)&chunkid
, 4), debugstr_an((char *)&temp
, 4), offset
,
634 debugstr_an((char *)&temp2
, 4), wine_dbgstr_longlong(mov_pos
+ offset
));
638 TRACE("Scanned dwChunkId: %s\n", debugstr_an((char *)&temp2
, 4));
641 TRACE("Scanned dwChunkId: %s\n", debugstr_an((char *)&temp
, 4));
643 /* Only dump one packet */
649 FIXME("Dropping index: no idea whether it is relative or absolute\n");
650 CoTaskMemFree(This
->oldindex
);
651 This
->oldindex
= NULL
;
656 This
->offset
= (DWORD
)mov_pos
;
661 static HRESULT
AVISplitter_ProcessStreamList(AVISplitterImpl
* This
, const BYTE
* pData
, DWORD cb
, ALLOCATOR_PROPERTIES
*props
)
664 const RIFFCHUNK
* pChunk
;
667 float fSamplesPerSec
= 0.0f
;
668 DWORD dwSampleSize
= 0;
671 static const WCHAR wszStreamTemplate
[] = {'S','t','r','e','a','m',' ','%','0','2','d',0};
674 ZeroMemory(&amt
, sizeof(amt
));
675 piOutput
.dir
= PINDIR_OUTPUT
;
676 piOutput
.pFilter
= &This
->Parser
.filter
.IBaseFilter_iface
;
677 wsprintfW(piOutput
.achName
, wszStreamTemplate
, This
->Parser
.cStreams
);
678 This
->streams
= CoTaskMemRealloc(This
->streams
, sizeof(StreamData
) * (This
->Parser
.cStreams
+1));
679 stream
= This
->streams
+ This
->Parser
.cStreams
;
680 ZeroMemory(stream
, sizeof(*stream
));
682 for (pChunk
= (const RIFFCHUNK
*)pData
;
683 ((const BYTE
*)pChunk
>= pData
) && ((const BYTE
*)pChunk
+ sizeof(RIFFCHUNK
) < pData
+ cb
) && (pChunk
->cb
> 0);
684 pChunk
= (const RIFFCHUNK
*)((const BYTE
*)pChunk
+ sizeof(RIFFCHUNK
) + pChunk
->cb
)
689 case ckidSTREAMHEADER
:
691 const AVISTREAMHEADER
* pStrHdr
= (const AVISTREAMHEADER
*)pChunk
;
692 TRACE("processing stream header\n");
693 stream
->streamheader
= *pStrHdr
;
695 fSamplesPerSec
= (float)pStrHdr
->dwRate
/ (float)pStrHdr
->dwScale
;
696 CoTaskMemFree(amt
.pbFormat
);
700 switch (pStrHdr
->fccType
)
702 case streamtypeVIDEO
:
703 amt
.formattype
= FORMAT_VideoInfo
;
705 case streamtypeAUDIO
:
706 amt
.formattype
= FORMAT_WaveFormatEx
;
709 FIXME("fccType %.4s not handled yet\n", (const char *)&pStrHdr
->fccType
);
710 amt
.formattype
= FORMAT_None
;
712 amt
.majortype
= MEDIATYPE_Video
;
713 amt
.majortype
.Data1
= pStrHdr
->fccType
;
714 amt
.subtype
= MEDIATYPE_Video
;
715 amt
.subtype
.Data1
= pStrHdr
->fccHandler
;
716 TRACE("Subtype FCC: %.04s\n", (LPCSTR
)&pStrHdr
->fccHandler
);
717 amt
.lSampleSize
= pStrHdr
->dwSampleSize
;
718 amt
.bFixedSizeSamples
= (amt
.lSampleSize
!= 0);
720 /* FIXME: Is this right? */
721 if (!amt
.lSampleSize
)
727 amt
.bTemporalCompression
= IsEqualGUID(&amt
.majortype
, &MEDIATYPE_Video
); /* FIXME? */
728 dwSampleSize
= pStrHdr
->dwSampleSize
;
729 dwLength
= pStrHdr
->dwLength
;
731 dwLength
= This
->AviHeader
.dwTotalFrames
;
733 if (pStrHdr
->dwSuggestedBufferSize
&& pStrHdr
->dwSuggestedBufferSize
> props
->cbBuffer
)
734 props
->cbBuffer
= pStrHdr
->dwSuggestedBufferSize
;
738 case ckidSTREAMFORMAT
:
739 TRACE("processing stream format data\n");
740 if (IsEqualIID(&amt
.formattype
, &FORMAT_VideoInfo
))
742 VIDEOINFOHEADER
* pvi
;
743 /* biCompression member appears to override the value in the stream header.
744 * i.e. the stream header can say something completely contradictory to what
745 * is in the BITMAPINFOHEADER! */
746 if (pChunk
->cb
< sizeof(BITMAPINFOHEADER
))
748 ERR("Not enough bytes for BITMAPINFOHEADER\n");
751 amt
.cbFormat
= sizeof(VIDEOINFOHEADER
) - sizeof(BITMAPINFOHEADER
) + pChunk
->cb
;
752 amt
.pbFormat
= CoTaskMemAlloc(amt
.cbFormat
);
753 ZeroMemory(amt
.pbFormat
, amt
.cbFormat
);
754 pvi
= (VIDEOINFOHEADER
*)amt
.pbFormat
;
755 pvi
->AvgTimePerFrame
= (LONGLONG
)(10000000.0 / fSamplesPerSec
);
757 CopyMemory(&pvi
->bmiHeader
, pChunk
+ 1, pChunk
->cb
);
758 if (pvi
->bmiHeader
.biCompression
)
759 amt
.subtype
.Data1
= pvi
->bmiHeader
.biCompression
;
761 else if (IsEqualIID(&amt
.formattype
, &FORMAT_WaveFormatEx
))
763 amt
.cbFormat
= pChunk
->cb
;
764 if (amt
.cbFormat
< sizeof(WAVEFORMATEX
))
765 amt
.cbFormat
= sizeof(WAVEFORMATEX
);
766 amt
.pbFormat
= CoTaskMemAlloc(amt
.cbFormat
);
767 ZeroMemory(amt
.pbFormat
, amt
.cbFormat
);
768 CopyMemory(amt
.pbFormat
, pChunk
+ 1, pChunk
->cb
);
772 amt
.cbFormat
= pChunk
->cb
;
773 amt
.pbFormat
= CoTaskMemAlloc(amt
.cbFormat
);
774 CopyMemory(amt
.pbFormat
, pChunk
+ 1, amt
.cbFormat
);
778 TRACE("processing stream name\n");
779 /* FIXME: this doesn't exactly match native version (we omit the "##)" prefix), but hey... */
780 MultiByteToWideChar(CP_ACP
, 0, (LPCSTR
)(pChunk
+ 1), pChunk
->cb
, piOutput
.achName
, sizeof(piOutput
.achName
) / sizeof(piOutput
.achName
[0]));
782 case ckidSTREAMHANDLERDATA
:
783 FIXME("process stream handler data\n");
786 TRACE("JUNK chunk ignored\n");
788 case ckidAVISUPERINDEX
:
790 const AVISUPERINDEX
*pIndex
= (const AVISUPERINDEX
*)pChunk
;
792 UINT rest
= pIndex
->cb
- sizeof(AVISUPERINDEX
) + sizeof(RIFFCHUNK
) + sizeof(pIndex
->aIndex
[0]) * ANYSIZE_ARRAY
;
794 if (pIndex
->cb
< sizeof(AVISUPERINDEX
) - sizeof(RIFFCHUNK
))
796 FIXME("size %u\n", pIndex
->cb
);
802 ERR("Stream %d got more than 1 superindex?\n", This
->Parser
.cStreams
);
806 TRACE("wLongsPerEntry: %hd\n", pIndex
->wLongsPerEntry
);
807 TRACE("bIndexSubType: %u\n", pIndex
->bIndexSubType
);
808 TRACE("bIndexType: %u\n", pIndex
->bIndexType
);
809 TRACE("nEntriesInUse: %u\n", pIndex
->nEntriesInUse
);
810 TRACE("dwChunkId: %.4s\n", (const char *)&pIndex
->dwChunkId
);
811 if (pIndex
->dwReserved
[0])
812 TRACE("dwReserved[0]: %u\n", pIndex
->dwReserved
[0]);
813 if (pIndex
->dwReserved
[1])
814 TRACE("dwReserved[1]: %u\n", pIndex
->dwReserved
[1]);
815 if (pIndex
->dwReserved
[2])
816 TRACE("dwReserved[2]: %u\n", pIndex
->dwReserved
[2]);
818 if (pIndex
->bIndexType
!= AVI_INDEX_OF_INDEXES
819 || pIndex
->wLongsPerEntry
!= 4
820 || rest
< (pIndex
->nEntriesInUse
* sizeof(DWORD
) * pIndex
->wLongsPerEntry
)
821 || (pIndex
->bIndexSubType
!= AVI_INDEX_SUB_2FIELD
&& pIndex
->bIndexSubType
!= AVI_INDEX_SUB_DEFAULT
))
823 FIXME("Invalid index chunk encountered\n");
827 stream
->entries
= pIndex
->nEntriesInUse
;
828 stream
->stdindex
= CoTaskMemRealloc(stream
->stdindex
, sizeof(*stream
->stdindex
) * stream
->entries
);
829 for (x
= 0; x
< pIndex
->nEntriesInUse
; ++x
)
831 TRACE("qwOffset: %s\n", wine_dbgstr_longlong(pIndex
->aIndex
[x
].qwOffset
));
832 TRACE("dwSize: %u\n", pIndex
->aIndex
[x
].dwSize
);
833 TRACE("dwDuration: %u (unreliable)\n", pIndex
->aIndex
[x
].dwDuration
);
835 AVISplitter_ProcessIndex(This
, &stream
->stdindex
[x
], pIndex
->aIndex
[x
].qwOffset
, pIndex
->aIndex
[x
].dwSize
);
840 FIXME("unknown chunk type \"%.04s\" ignored\n", (LPCSTR
)&pChunk
->fcc
);
844 if (IsEqualGUID(&amt
.formattype
, &FORMAT_WaveFormatEx
))
846 amt
.subtype
= MEDIATYPE_Video
;
847 amt
.subtype
.Data1
= ((WAVEFORMATEX
*)amt
.pbFormat
)->wFormatTag
;
850 dump_AM_MEDIA_TYPE(&amt
);
851 TRACE("fSamplesPerSec = %f\n", (double)fSamplesPerSec
);
852 TRACE("dwSampleSize = %x\n", dwSampleSize
);
853 TRACE("dwLength = %x\n", dwLength
);
855 stream
->fSamplesPerSec
= fSamplesPerSec
;
856 stream
->dwSampleSize
= dwSampleSize
;
857 stream
->dwLength
= dwLength
; /* TODO: Use this for mediaseeking */
858 stream
->packet_queued
= CreateEventW(NULL
, 0, 0, NULL
);
860 hr
= Parser_AddPin(&(This
->Parser
), &piOutput
, props
, &amt
);
861 CoTaskMemFree(amt
.pbFormat
);
867 static HRESULT
AVISplitter_ProcessODML(AVISplitterImpl
* This
, const BYTE
* pData
, DWORD cb
)
869 const RIFFCHUNK
* pChunk
;
871 for (pChunk
= (const RIFFCHUNK
*)pData
;
872 ((const BYTE
*)pChunk
>= pData
) && ((const BYTE
*)pChunk
+ sizeof(RIFFCHUNK
) < pData
+ cb
) && (pChunk
->cb
> 0);
873 pChunk
= (const RIFFCHUNK
*)((const BYTE
*)pChunk
+ sizeof(RIFFCHUNK
) + pChunk
->cb
)
878 case ckidAVIEXTHEADER
:
881 const AVIEXTHEADER
* pExtHdr
= (const AVIEXTHEADER
*)pChunk
;
883 TRACE("processing extension header\n");
884 if (pExtHdr
->cb
!= sizeof(AVIEXTHEADER
) - sizeof(RIFFCHUNK
))
886 FIXME("Size: %u\n", pExtHdr
->cb
);
889 TRACE("dwGrandFrames: %u\n", pExtHdr
->dwGrandFrames
);
890 for (x
= 0; x
< 61; ++x
)
891 if (pExtHdr
->dwFuture
[x
])
892 FIXME("dwFuture[%i] = %u (0x%08x)\n", x
, pExtHdr
->dwFuture
[x
], pExtHdr
->dwFuture
[x
]);
893 This
->ExtHeader
= *pExtHdr
;
897 FIXME("unknown chunk type \"%.04s\" ignored\n", (LPCSTR
)&pChunk
->fcc
);
904 static HRESULT
AVISplitter_InitializeStreams(AVISplitterImpl
*This
)
912 for (x
= 0; x
< This
->Parser
.cStreams
; ++x
)
914 This
->streams
[x
].frames
= 0;
915 This
->streams
[x
].pos
= ~0;
916 This
->streams
[x
].index
= 0;
919 nMax
= This
->oldindex
->cb
/ sizeof(This
->oldindex
->aIndex
[0]);
921 /* Ok, maybe this is more of an exercise to see if I interpret everything correctly or not, but that is useful for now. */
922 for (n
= 0; n
< nMax
; ++n
)
924 DWORD streamId
= StreamFromFOURCC(This
->oldindex
->aIndex
[n
].dwChunkId
);
925 if (streamId
>= This
->Parser
.cStreams
)
927 FIXME("Stream id %s ignored\n", debugstr_an((char*)&This
->oldindex
->aIndex
[n
].dwChunkId
, 4));
930 if (This
->streams
[streamId
].pos
== ~0U)
931 This
->streams
[streamId
].pos
= n
;
933 if (This
->streams
[streamId
].streamheader
.dwSampleSize
)
934 This
->streams
[streamId
].frames
+= This
->oldindex
->aIndex
[n
].dwSize
/ This
->streams
[streamId
].streamheader
.dwSampleSize
;
936 ++This
->streams
[streamId
].frames
;
939 for (x
= 0; x
< This
->Parser
.cStreams
; ++x
)
941 if ((DWORD
)This
->streams
[x
].frames
!= This
->streams
[x
].streamheader
.dwLength
)
943 FIXME("stream %u: frames found: %u, frames meant to be found: %u\n", x
, (DWORD
)This
->streams
[x
].frames
, This
->streams
[x
].streamheader
.dwLength
);
948 else if (!This
->streams
[0].entries
)
950 for (x
= 0; x
< This
->Parser
.cStreams
; ++x
)
952 This
->streams
[x
].frames
= This
->streams
[x
].streamheader
.dwLength
;
954 /* MS Avi splitter does seek through the whole file, we should! */
955 ERR("We should be manually seeking through the entire file to build an index, because the index is missing!!!\n");
959 /* Not much here yet */
960 for (x
= 0; x
< This
->Parser
.cStreams
; ++x
)
962 StreamData
*stream
= This
->streams
+ x
;
968 if (stream
->stdindex
)
972 for (y
= 0; y
< stream
->entries
; ++y
)
974 if (stream
->streamheader
.dwSampleSize
)
978 for (z
= 0; z
< stream
->stdindex
[y
]->nEntriesInUse
; ++z
)
980 UINT len
= stream
->stdindex
[y
]->aIndex
[z
].dwSize
& ~(1u << 31);
981 frames
+= len
/ stream
->streamheader
.dwSampleSize
+ !!(len
% stream
->streamheader
.dwSampleSize
);
985 frames
+= stream
->stdindex
[y
]->nEntriesInUse
;
988 else frames
= stream
->frames
;
990 frames
*= stream
->streamheader
.dwScale
;
991 /* Keep accuracy as high as possible for duration */
992 This
->Parser
.sourceSeeking
.llDuration
= frames
* 10000000;
993 This
->Parser
.sourceSeeking
.llDuration
/= stream
->streamheader
.dwRate
;
994 This
->Parser
.sourceSeeking
.llStop
= This
->Parser
.sourceSeeking
.llDuration
;
995 This
->Parser
.sourceSeeking
.llCurrent
= 0;
997 frames
/= stream
->streamheader
.dwRate
;
999 TRACE("Duration: %d days, %d hours, %d minutes and %d.%03u seconds\n", (DWORD
)(frames
/ 86400),
1000 (DWORD
)((frames
% 86400) / 3600), (DWORD
)((frames
% 3600) / 60), (DWORD
)(frames
% 60),
1001 (DWORD
)(This
->Parser
.sourceSeeking
.llDuration
/10000) % 1000);
1007 static HRESULT
AVISplitter_Disconnect(LPVOID iface
);
1009 /* FIXME: fix leaks on failure here */
1010 static HRESULT
AVISplitter_InputPin_PreConnect(IPin
* iface
, IPin
* pConnectPin
, ALLOCATOR_PROPERTIES
*props
)
1012 PullPin
*This
= impl_PullPin_from_IPin(iface
);
1015 LONGLONG pos
= 0; /* in bytes */
1017 RIFFCHUNK
* pCurrentChunk
;
1018 LONGLONG total
, avail
;
1022 AVISplitterImpl
* pAviSplit
= (AVISplitterImpl
*)This
->pin
.pinInfo
.pFilter
;
1024 hr
= IAsyncReader_SyncRead(This
->pReader
, pos
, sizeof(list
), (BYTE
*)&list
);
1025 pos
+= sizeof(list
);
1027 if (list
.fcc
!= FOURCC_RIFF
)
1029 ERR("Input stream not a RIFF file\n");
1032 if (list
.fccListType
!= formtypeAVI
)
1034 ERR("Input stream not an AVI RIFF file\n");
1038 hr
= IAsyncReader_SyncRead(This
->pReader
, pos
, sizeof(list
), (BYTE
*)&list
);
1039 if (list
.fcc
!= FOURCC_LIST
)
1041 ERR("Expected LIST chunk, but got %.04s\n", (LPSTR
)&list
.fcc
);
1044 if (list
.fccListType
!= listtypeAVIHEADER
)
1046 ERR("Header list expected. Got: %.04s\n", (LPSTR
)&list
.fccListType
);
1050 pBuffer
= HeapAlloc(GetProcessHeap(), 0, list
.cb
- sizeof(RIFFLIST
) + sizeof(RIFFCHUNK
));
1051 hr
= IAsyncReader_SyncRead(This
->pReader
, pos
+ sizeof(list
), list
.cb
- sizeof(RIFFLIST
) + sizeof(RIFFCHUNK
), pBuffer
);
1053 pAviSplit
->AviHeader
.cb
= 0;
1055 /* Stream list will set the buffer size here, so set a default and allow an override */
1056 props
->cbBuffer
= 0x20000;
1058 for (pCurrentChunk
= (RIFFCHUNK
*)pBuffer
; (BYTE
*)pCurrentChunk
+ sizeof(*pCurrentChunk
) < pBuffer
+ list
.cb
; pCurrentChunk
= (RIFFCHUNK
*)(((BYTE
*)pCurrentChunk
) + sizeof(*pCurrentChunk
) + pCurrentChunk
->cb
))
1062 switch (pCurrentChunk
->fcc
)
1064 case ckidMAINAVIHEADER
:
1065 /* AVIMAINHEADER includes the structure that is pCurrentChunk at the moment */
1066 memcpy(&pAviSplit
->AviHeader
, pCurrentChunk
, sizeof(pAviSplit
->AviHeader
));
1069 pList
= (RIFFLIST
*)pCurrentChunk
;
1070 switch (pList
->fccListType
)
1072 case ckidSTREAMLIST
:
1073 hr
= AVISplitter_ProcessStreamList(pAviSplit
, (BYTE
*)pCurrentChunk
+ sizeof(RIFFLIST
), pCurrentChunk
->cb
+ sizeof(RIFFCHUNK
) - sizeof(RIFFLIST
), props
);
1076 hr
= AVISplitter_ProcessODML(pAviSplit
, (BYTE
*)pCurrentChunk
+ sizeof(RIFFLIST
), pCurrentChunk
->cb
+ sizeof(RIFFCHUNK
) - sizeof(RIFFLIST
));
1080 case ckidAVIPADDING
:
1084 FIXME("unrecognised header list type: %.04s\n", (LPSTR
)&pCurrentChunk
->fcc
);
1087 HeapFree(GetProcessHeap(), 0, pBuffer
);
1089 if (pAviSplit
->AviHeader
.cb
!= sizeof(pAviSplit
->AviHeader
) - sizeof(RIFFCHUNK
))
1091 ERR("Avi Header wrong size!\n");
1095 /* Skip any chunks until we find the LIST chunk */
1098 pos
+= sizeof(RIFFCHUNK
) + list
.cb
;
1099 hr
= IAsyncReader_SyncRead(This
->pReader
, pos
, sizeof(list
), (BYTE
*)&list
);
1101 while (hr
== S_OK
&& (list
.fcc
!= FOURCC_LIST
||
1102 (list
.fcc
== FOURCC_LIST
&& list
.fccListType
!= listtypeAVIMOVIE
)));
1106 ERR("Failed to find LIST chunk from AVI file\n");
1110 IAsyncReader_Length(This
->pReader
, &total
, &avail
);
1112 /* FIXME: AVIX files are extended beyond the FOURCC chunk "AVI ", and thus won't be played here,
1113 * once I get one of the files I'll try to fix it */
1114 This
->rtStart
= pAviSplit
->CurrentChunkOffset
= MEDIATIME_FROM_BYTES(pos
+ sizeof(RIFFLIST
));
1115 pos
+= list
.cb
+ sizeof(RIFFCHUNK
);
1117 pAviSplit
->EndOfFile
= This
->rtStop
= MEDIATIME_FROM_BYTES(pos
);
1120 ERR("File smaller (%s) then EndOfFile (%s)\n", wine_dbgstr_longlong(total
), wine_dbgstr_longlong(pAviSplit
->EndOfFile
));
1124 hr
= IAsyncReader_SyncRead(This
->pReader
, BYTES_FROM_MEDIATIME(pAviSplit
->CurrentChunkOffset
), sizeof(pAviSplit
->CurrentChunk
), (BYTE
*)&pAviSplit
->CurrentChunk
);
1127 props
->cbPrefix
= 0;
1128 /* Comrades, prevent shortage of buffers, or you will feel the consequences! DA! */
1129 props
->cBuffers
= 2 * pAviSplit
->Parser
.cStreams
;
1131 /* Now peek into the idx1 index, if available */
1132 if (hr
== S_OK
&& (total
- pos
) > sizeof(RIFFCHUNK
))
1134 memset(&list
, 0, sizeof(list
));
1136 hr
= IAsyncReader_SyncRead(This
->pReader
, pos
, sizeof(list
), (BYTE
*)&list
);
1137 if (list
.fcc
== ckidAVIOLDINDEX
)
1139 pAviSplit
->oldindex
= CoTaskMemRealloc(pAviSplit
->oldindex
, list
.cb
+ sizeof(RIFFCHUNK
));
1140 if (pAviSplit
->oldindex
)
1142 hr
= IAsyncReader_SyncRead(This
->pReader
, pos
, sizeof(RIFFCHUNK
) + list
.cb
, (BYTE
*)pAviSplit
->oldindex
);
1145 hr
= AVISplitter_ProcessOldIndex(pAviSplit
);
1149 CoTaskMemFree(pAviSplit
->oldindex
);
1150 pAviSplit
->oldindex
= NULL
;
1158 for (x
= 0; x
< pAviSplit
->Parser
.cStreams
; ++x
)
1159 if (pAviSplit
->streams
[x
].entries
)
1164 CoTaskMemFree(pAviSplit
->oldindex
);
1165 pAviSplit
->oldindex
= NULL
;
1166 if (indexes
< pAviSplit
->Parser
.cStreams
)
1168 /* This error could possible be survived by switching to old type index,
1169 * but I would rather find out why it doesn't find everything here
1171 ERR("%d indexes expected, but only have %d\n", indexes
, pAviSplit
->Parser
.cStreams
);
1175 else if (pAviSplit
->oldindex
)
1176 indexes
= pAviSplit
->Parser
.cStreams
;
1178 if (!indexes
&& pAviSplit
->AviHeader
.dwFlags
& AVIF_MUSTUSEINDEX
)
1180 FIXME("No usable index was found!\n");
1184 /* Now, set up the streams */
1186 hr
= AVISplitter_InitializeStreams(pAviSplit
);
1190 AVISplitter_Disconnect(pAviSplit
);
1194 TRACE("AVI File ok\n");
1199 static HRESULT
AVISplitter_Flush(LPVOID iface
)
1201 AVISplitterImpl
*This
= iface
;
1205 TRACE("(%p)->()\n", This
);
1207 for (x
= 0; x
< This
->Parser
.cStreams
; ++x
)
1209 StreamData
*stream
= This
->streams
+ x
;
1213 ref
= IMediaSample_Release(stream
->sample
);
1216 stream
->sample
= NULL
;
1218 ResetEvent(stream
->packet_queued
);
1219 assert(!stream
->thread
);
1225 static HRESULT
AVISplitter_Disconnect(LPVOID iface
)
1227 AVISplitterImpl
*This
= iface
;
1230 /* TODO: Remove other memory that's allocated during connect */
1231 CoTaskMemFree(This
->oldindex
);
1232 This
->oldindex
= NULL
;
1234 for (x
= 0; x
< This
->Parser
.cStreams
; ++x
)
1238 StreamData
*stream
= &This
->streams
[x
];
1240 for (i
= 0; i
< stream
->entries
; ++i
)
1241 CoTaskMemFree(stream
->stdindex
[i
]);
1243 CoTaskMemFree(stream
->stdindex
);
1244 CloseHandle(stream
->packet_queued
);
1246 CoTaskMemFree(This
->streams
);
1247 This
->streams
= NULL
;
1251 static ULONG WINAPI
AVISplitter_Release(IBaseFilter
*iface
)
1253 AVISplitterImpl
*This
= (AVISplitterImpl
*)iface
;
1256 ref
= InterlockedDecrement(&This
->Parser
.filter
.refCount
);
1258 TRACE("(%p)->() Release from %d\n", This
, ref
+ 1);
1262 AVISplitter_Flush(This
);
1263 Parser_Destroy(&This
->Parser
);
1269 static HRESULT WINAPI
AVISplitter_seek(IMediaSeeking
*iface
)
1271 AVISplitterImpl
*This
= impl_from_IMediaSeeking(iface
);
1272 PullPin
*pPin
= This
->Parser
.pInputPin
;
1273 LONGLONG newpos
, endpos
;
1276 newpos
= This
->Parser
.sourceSeeking
.llCurrent
;
1277 endpos
= This
->Parser
.sourceSeeking
.llDuration
;
1279 if (newpos
> endpos
)
1281 WARN("Requesting position %x%08x beyond end of stream %x%08x\n", (DWORD
)(newpos
>>32), (DWORD
)newpos
, (DWORD
)(endpos
>>32), (DWORD
)endpos
);
1282 return E_INVALIDARG
;
1285 FIXME("Moving position to %u.%03u s!\n", (DWORD
)(newpos
/ 10000000), (DWORD
)((newpos
/ 10000)%1000));
1287 EnterCriticalSection(&pPin
->thread_lock
);
1288 /* Send a flush to all output pins */
1289 IPin_BeginFlush(&pPin
->pin
.IPin_iface
);
1291 /* Make sure this is done while stopped, BeginFlush takes care of this */
1292 EnterCriticalSection(&This
->Parser
.filter
.csFilter
);
1293 for (x
= 0; x
< This
->Parser
.cStreams
; ++x
)
1295 Parser_OutputPin
*pin
= unsafe_impl_Parser_OutputPin_from_IPin(This
->Parser
.ppPins
[1+x
]);
1296 StreamData
*stream
= This
->streams
+ x
;
1297 LONGLONG wanted_frames
;
1298 DWORD last_keyframe
= 0, last_keyframeidx
= 0, preroll
= 0;
1300 wanted_frames
= newpos
;
1301 wanted_frames
*= stream
->streamheader
.dwRate
;
1302 wanted_frames
/= 10000000;
1303 wanted_frames
/= stream
->streamheader
.dwScale
;
1305 pin
->dwSamplesProcessed
= 0;
1308 stream
->seek
= TRUE
;
1309 if (stream
->stdindex
)
1313 for (y
= 0; y
< stream
->entries
; ++y
)
1315 for (z
= 0; z
< stream
->stdindex
[y
]->nEntriesInUse
; ++z
)
1317 if (stream
->streamheader
.dwSampleSize
)
1319 ULONG len
= stream
->stdindex
[y
]->aIndex
[z
].dwSize
& ~(1u << 31);
1320 ULONG size
= stream
->streamheader
.dwSampleSize
;
1322 pin
->dwSamplesProcessed
+= len
/ size
;
1324 ++pin
->dwSamplesProcessed
;
1326 else ++pin
->dwSamplesProcessed
;
1328 if (!(stream
->stdindex
[y
]->aIndex
[z
].dwSize
>> 31))
1331 last_keyframeidx
= y
;
1337 if (pin
->dwSamplesProcessed
>= wanted_frames
)
1340 if (pin
->dwSamplesProcessed
>= wanted_frames
)
1343 stream
->index
= last_keyframeidx
;
1344 stream
->pos
= last_keyframe
;
1349 nMax
= This
->oldindex
->cb
/ sizeof(This
->oldindex
->aIndex
[0]);
1351 for (n
= 0; n
< nMax
; ++n
)
1353 DWORD streamId
= StreamFromFOURCC(This
->oldindex
->aIndex
[n
].dwChunkId
);
1357 if (stream
->streamheader
.dwSampleSize
)
1359 ULONG len
= This
->oldindex
->aIndex
[n
].dwSize
;
1360 ULONG size
= stream
->streamheader
.dwSampleSize
;
1362 pin
->dwSamplesProcessed
+= len
/ size
;
1364 ++pin
->dwSamplesProcessed
;
1366 else ++pin
->dwSamplesProcessed
;
1368 if (This
->oldindex
->aIndex
[n
].dwFlags
& AVIIF_KEYFRAME
)
1376 if (pin
->dwSamplesProcessed
>= wanted_frames
)
1380 stream
->pos
= last_keyframe
;
1383 stream
->preroll
= preroll
;
1384 stream
->seek
= TRUE
;
1386 LeaveCriticalSection(&This
->Parser
.filter
.csFilter
);
1388 TRACE("Done flushing\n");
1389 IPin_EndFlush(&pPin
->pin
.IPin_iface
);
1390 LeaveCriticalSection(&pPin
->thread_lock
);
1395 static const IBaseFilterVtbl AVISplitterImpl_Vtbl
=
1397 Parser_QueryInterface
,
1399 AVISplitter_Release
,
1405 Parser_SetSyncSource
,
1406 Parser_GetSyncSource
,
1409 Parser_QueryFilterInfo
,
1410 Parser_JoinFilterGraph
,
1411 Parser_QueryVendorInfo
1414 HRESULT
AVISplitter_create(IUnknown
* pUnkOuter
, LPVOID
* ppv
)
1417 AVISplitterImpl
* This
;
1419 TRACE("(%p, %p)\n", pUnkOuter
, ppv
);
1424 return CLASS_E_NOAGGREGATION
;
1426 /* Note: This memory is managed by the transform filter once created */
1427 This
= CoTaskMemAlloc(sizeof(AVISplitterImpl
));
1429 This
->streams
= NULL
;
1430 This
->oldindex
= NULL
;
1432 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
);
1437 *ppv
= &This
->Parser
.filter
.IBaseFilter_iface
;