4 * Copyright 2003 Robert Shearman
5 * Copyright 2004-2005 Christian Costa
6 * Copyright 2007 Chris Robinson
7 * Copyright 2008 Maarten Lankhorst
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "quartz_private.h"
36 #include "wine/unicode.h"
37 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(quartz
);
43 #define SEQUENCE_HEADER_CODE 0xB3
44 #define PACK_START_CODE 0xBA
46 #define SYSTEM_START_CODE 0xBB
47 #define AUDIO_ELEMENTARY_STREAM 0xC0
48 #define VIDEO_ELEMENTARY_STREAM 0xE0
50 #define MPEG_SYSTEM_HEADER 3
51 #define MPEG_VIDEO_HEADER 2
52 #define MPEG_AUDIO_HEADER 1
53 #define MPEG_NO_HEADER 0
55 typedef struct MPEGSplitterImpl
63 /* Whether we just seeked (or started playing) */
67 static inline MPEGSplitterImpl
*impl_from_IMediaSeeking( IMediaSeeking
*iface
)
69 return CONTAINING_RECORD(iface
, MPEGSplitterImpl
, Parser
.sourceSeeking
.IMediaSeeking_iface
);
72 static int MPEGSplitter_head_check(const BYTE
*header
)
74 /* If this is a possible start code, check for a system or video header */
75 if (header
[0] == 0 && header
[1] == 0 && header
[2] == 1)
77 /* Check if we got a system or elementary stream start code */
78 if (header
[3] == PACK_START_CODE
||
79 header
[3] == VIDEO_ELEMENTARY_STREAM
||
80 header
[3] == AUDIO_ELEMENTARY_STREAM
)
81 return MPEG_SYSTEM_HEADER
;
83 /* Check for a MPEG video sequence start code */
84 if (header
[3] == SEQUENCE_HEADER_CODE
)
85 return MPEG_VIDEO_HEADER
;
88 /* This should give a good guess if we have an MPEG audio header */
89 if(header
[0] == 0xff && ((header
[1]>>5)&0x7) == 0x7 &&
90 ((header
[1]>>1)&0x3) != 0 && ((header
[2]>>4)&0xf) != 0xf &&
91 ((header
[2]>>2)&0x3) != 0x3)
92 return MPEG_AUDIO_HEADER
;
95 return MPEG_NO_HEADER
;
98 static const WCHAR wszAudioStream
[] = {'A','u','d','i','o',0};
100 static const DWORD freqs
[10] = { 44100, 48000, 32000, 22050, 24000, 16000, 11025, 12000, 8000, 0 };
102 static const DWORD tabsel_123
[2][3][16] = {
103 { {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,},
104 {0,32,48,56, 64, 80, 96,112,128,160,192,224,256,320,384,},
105 {0,32,40,48, 56, 64, 80, 96,112,128,160,192,224,256,320,} },
107 { {0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,},
108 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,},
109 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,} }
112 static HRESULT
parse_header(BYTE
*header
, LONGLONG
*plen
, LONGLONG
*pduration
)
114 int bitrate_index
, freq_index
, lsf
= 1, mpeg1
, layer
, padding
, bitrate
, length
;
117 if (MPEGSplitter_head_check(header
) != MPEG_AUDIO_HEADER
)
119 FIXME("Not a valid header: %02x:%02x:%02x:%02x\n", header
[0], header
[1], header
[2], header
[3]);
123 mpeg1
= (header
[1]>>4)&0x1;
125 lsf
= ((header
[1]>>3)&0x1)^1;
127 layer
= 4-((header
[1]>>1)&0x3);
128 bitrate_index
= ((header
[2]>>4)&0xf);
129 freq_index
= ((header
[2]>>2)&0x3) + (mpeg1
?(lsf
*3):6);
130 padding
= ((header
[2]>>1)&0x1);
132 bitrate
= tabsel_123
[lsf
][layer
-1][bitrate_index
] * 1000;
135 FIXME("Not a valid header: %02x:%02x:%02x:%02x\n", header
[0], header
[1], header
[2], header
[3]);
140 length
= 4 * (12 * bitrate
/ freqs
[freq_index
] + padding
);
142 length
= 144 * bitrate
/ freqs
[freq_index
] + padding
;
144 length
= 144 * bitrate
/ (freqs
[freq_index
]<<lsf
) + padding
;
147 ERR("Impossible layer %d\n", layer
);
151 duration
= (ULONGLONG
)10000000 * (ULONGLONG
)(length
) / (ULONGLONG
)(bitrate
/8);
154 *pduration
+= duration
;
158 static HRESULT
FillBuffer(MPEGSplitterImpl
*This
, IMediaSample
*pCurrentSample
)
160 Parser_OutputPin
* pOutputPin
= unsafe_impl_Parser_OutputPin_from_IPin(This
->Parser
.ppPins
[1]);
162 LONGLONG pos
= BYTES_FROM_MEDIATIME(This
->Parser
.pInputPin
->rtNext
);
163 LONGLONG time
= This
->position
, rtstop
, rtstart
;
166 DWORD len
= IMediaSample_GetActualDataLength(pCurrentSample
);
168 TRACE("Source length: %u\n", len
);
169 IMediaSample_GetPointer(pCurrentSample
, &fbuf
);
171 /* Find the next valid header.. it <SHOULD> be right here */
172 hr
= parse_header(fbuf
, &length
, &This
->position
);
174 IMediaSample_SetActualDataLength(pCurrentSample
, length
);
176 /* Queue the next sample */
177 if (length
+ 4 == len
)
179 PullPin
*pin
= This
->Parser
.pInputPin
;
180 LONGLONG stop
= BYTES_FROM_MEDIATIME(pin
->rtStop
);
183 memcpy(This
->header
, fbuf
+ length
, 4);
184 while (FAILED(hr
= parse_header(This
->header
, &length
, NULL
)))
186 memmove(This
->header
, This
->header
+1, 3);
189 IAsyncReader_SyncRead(pin
->pReader
, ++pos
, 1, This
->header
+ 3);
191 pin
->rtNext
= MEDIATIME_FROM_BYTES(pos
);
195 /* Remove 4 for the last header, which should hopefully work */
196 IMediaSample
*sample
= NULL
;
197 LONGLONG rtSampleStart
= pin
->rtNext
- MEDIATIME_FROM_BYTES(4);
198 LONGLONG rtSampleStop
= rtSampleStart
+ MEDIATIME_FROM_BYTES(length
+ 4);
200 if (rtSampleStop
> pin
->rtStop
)
201 rtSampleStop
= MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(pin
->rtStop
), pin
->cbAlign
));
203 hr
= IMemAllocator_GetBuffer(pin
->pAlloc
, &sample
, NULL
, NULL
, 0);
206 IMediaSample_SetTime(sample
, &rtSampleStart
, &rtSampleStop
);
207 IMediaSample_SetPreroll(sample
, FALSE
);
208 IMediaSample_SetDiscontinuity(sample
, FALSE
);
209 IMediaSample_SetSyncPoint(sample
, TRUE
);
210 hr
= IAsyncReader_Request(pin
->pReader
, sample
, 0);
213 pin
->rtCurrent
= rtSampleStart
;
214 pin
->rtNext
= rtSampleStop
;
217 IMediaSample_Release(sample
);
220 FIXME("o_Ox%08x\n", hr
);
223 /* If not, we're presumably at the end of file */
225 TRACE("Media time : %u.%03u\n", (DWORD
)(This
->position
/10000000), (DWORD
)((This
->position
/10000)%1000));
227 if (IMediaSample_IsDiscontinuity(pCurrentSample
) == S_OK
) {
229 EnterCriticalSection(&This
->Parser
.filter
.csFilter
);
230 pOutputPin
->pin
.pin
.tStart
= time
;
231 pOutputPin
->pin
.pin
.dRate
= This
->Parser
.sourceSeeking
.dRate
;
232 hr
= IPin_ConnectedTo(&pOutputPin
->pin
.pin
.IPin_iface
, &victim
);
235 hr
= IPin_NewSegment(victim
, time
, This
->Parser
.sourceSeeking
.llStop
,
236 This
->Parser
.sourceSeeking
.dRate
);
238 FIXME("NewSegment returns %08x\n", hr
);
239 IPin_Release(victim
);
241 LeaveCriticalSection(&This
->Parser
.filter
.csFilter
);
245 rtstart
= (double)(time
- pOutputPin
->pin
.pin
.tStart
) / pOutputPin
->pin
.pin
.dRate
;
246 rtstop
= (double)(This
->position
- pOutputPin
->pin
.pin
.tStart
) / pOutputPin
->pin
.pin
.dRate
;
247 IMediaSample_SetTime(pCurrentSample
, &rtstart
, &rtstop
);
248 IMediaSample_SetMediaTime(pCurrentSample
, &time
, &This
->position
);
250 hr
= BaseOutputPinImpl_Deliver(&pOutputPin
->pin
, pCurrentSample
);
255 TRACE("Error sending sample (%x)\n", hr
);
257 TRACE("S_FALSE (%d), holding\n", IMediaSample_GetActualDataLength(pCurrentSample
));
264 static HRESULT
MPEGSplitter_process_sample(LPVOID iface
, IMediaSample
* pSample
, DWORD_PTR cookie
)
266 MPEGSplitterImpl
*This
= iface
;
268 DWORD cbSrcStream
= 0;
269 REFERENCE_TIME tStart
, tStop
, tAviStart
= This
->position
;
272 hr
= IMediaSample_GetTime(pSample
, &tStart
, &tStop
);
275 cbSrcStream
= IMediaSample_GetActualDataLength(pSample
);
276 hr
= IMediaSample_GetPointer(pSample
, &pbSrcStream
);
279 /* Flush occurring */
280 if (cbSrcStream
== 0)
282 FIXME(".. Why do I need you?\n");
286 /* trace removed for performance reasons */
287 /* TRACE("(%p), %llu -> %llu\n", pSample, tStart, tStop); */
289 /* Now, try to find a new header */
290 hr
= FillBuffer(This
, pSample
);
293 WARN("Failed with hres: %08x!\n", hr
);
295 /* Unset progression if denied! */
296 if (hr
== VFW_E_WRONG_STATE
|| hr
== S_FALSE
)
298 memcpy(This
->header
, pbSrcStream
, 4);
299 This
->Parser
.pInputPin
->rtCurrent
= tStart
;
300 This
->position
= tAviStart
;
304 if (BYTES_FROM_MEDIATIME(tStop
) >= This
->EndOfFile
|| This
->position
>= This
->Parser
.sourceSeeking
.llStop
)
308 TRACE("End of file reached\n");
310 for (i
= 0; i
< This
->Parser
.cStreams
; i
++)
314 hr
= IPin_ConnectedTo(This
->Parser
.ppPins
[i
+1], &ppin
);
317 hr
= IPin_EndOfStream(ppin
);
321 WARN("Error sending EndOfStream to pin %u (%x)\n", i
, hr
);
324 /* Force the pullpin thread to stop */
332 static HRESULT
MPEGSplitter_query_accept(LPVOID iface
, const AM_MEDIA_TYPE
*pmt
)
334 if (!IsEqualIID(&pmt
->majortype
, &MEDIATYPE_Stream
))
337 if (IsEqualIID(&pmt
->subtype
, &MEDIASUBTYPE_MPEG1Audio
))
340 if (IsEqualIID(&pmt
->subtype
, &MEDIASUBTYPE_MPEG1Video
))
341 FIXME("MPEG-1 video streams not yet supported.\n");
342 else if (IsEqualIID(&pmt
->subtype
, &MEDIASUBTYPE_MPEG1System
))
343 FIXME("MPEG-1 system streams not yet supported.\n");
344 else if (IsEqualIID(&pmt
->subtype
, &MEDIASUBTYPE_MPEG1VideoCD
))
345 FIXME("MPEG-1 VideoCD streams not yet supported.\n");
346 else FIXME("%s\n", debugstr_guid(&pmt
->subtype
));
352 static HRESULT
MPEGSplitter_init_audio(MPEGSplitterImpl
*This
, const BYTE
*header
, PIN_INFO
*ppiOutput
, AM_MEDIA_TYPE
*pamt
)
354 WAVEFORMATEX
*format
;
364 ZeroMemory(pamt
, sizeof(*pamt
));
365 ppiOutput
->dir
= PINDIR_OUTPUT
;
366 ppiOutput
->pFilter
= (IBaseFilter
*)This
;
367 wsprintfW(ppiOutput
->achName
, wszAudioStream
);
369 pamt
->formattype
= FORMAT_WaveFormatEx
;
370 pamt
->majortype
= MEDIATYPE_Audio
;
371 pamt
->subtype
= MEDIASUBTYPE_MPEG1AudioPayload
;
373 pamt
->lSampleSize
= 0;
374 pamt
->bFixedSizeSamples
= FALSE
;
375 pamt
->bTemporalCompression
= 0;
377 mpeg1
= (header
[1]>>4)&0x1;
379 lsf
= ((header
[1]>>3)&0x1)^1;
381 layer
= 4-((header
[1]>>1)&0x3);
382 bitrate_index
= ((header
[2]>>4)&0xf);
383 freq_index
= ((header
[2]>>2)&0x3) + (mpeg1
?(lsf
*3):6);
384 mode
= ((header
[3]>>6)&0x3);
385 mode_ext
= ((header
[3]>>4)&0x3);
386 emphasis
= ((header
[3]>>0)&0x3);
390 /* Set to highest bitrate so samples will fit in for sure */
391 FIXME("Variable-bitrate audio not fully supported.\n");
395 pamt
->cbFormat
= ((layer
==3)? sizeof(MPEGLAYER3WAVEFORMAT
) :
396 sizeof(MPEG1WAVEFORMAT
));
397 pamt
->pbFormat
= CoTaskMemAlloc(pamt
->cbFormat
);
399 return E_OUTOFMEMORY
;
400 ZeroMemory(pamt
->pbFormat
, pamt
->cbFormat
);
401 format
= (WAVEFORMATEX
*)pamt
->pbFormat
;
403 format
->wFormatTag
= ((layer
== 3) ? WAVE_FORMAT_MPEGLAYER3
:
405 format
->nChannels
= ((mode
== 3) ? 1 : 2);
406 format
->nSamplesPerSec
= freqs
[freq_index
];
407 format
->nAvgBytesPerSec
= tabsel_123
[lsf
][layer
-1][bitrate_index
] * 1000 / 8;
410 format
->nBlockAlign
= format
->nAvgBytesPerSec
* 8 * 144 /
411 (format
->nSamplesPerSec
<<lsf
) + 1;
413 format
->nBlockAlign
= format
->nAvgBytesPerSec
* 8 * 144 /
414 format
->nSamplesPerSec
+ 1;
416 format
->nBlockAlign
= 4 * (format
->nAvgBytesPerSec
* 8 * 12 / format
->nSamplesPerSec
+ 1);
418 format
->wBitsPerSample
= 0;
422 MPEGLAYER3WAVEFORMAT
*mp3format
= (MPEGLAYER3WAVEFORMAT
*)format
;
424 format
->cbSize
= MPEGLAYER3_WFX_EXTRA_BYTES
;
426 mp3format
->wID
= MPEGLAYER3_ID_MPEG
;
427 mp3format
->fdwFlags
= MPEGLAYER3_FLAG_PADDING_ON
;
428 mp3format
->nBlockSize
= format
->nBlockAlign
;
429 mp3format
->nFramesPerBlock
= 1;
431 /* Beware the evil magic numbers. This struct is apparently horribly
432 * under-documented, and the only references I could find had it being
433 * set to this with no real explanation. It works fine though, so I'm
434 * not complaining (yet).
436 mp3format
->nCodecDelay
= 1393;
440 MPEG1WAVEFORMAT
*mpgformat
= (MPEG1WAVEFORMAT
*)format
;
444 mpgformat
->fwHeadLayer
= ((layer
== 1) ? ACM_MPEG_LAYER1
:
445 ((layer
== 2) ? ACM_MPEG_LAYER2
:
447 mpgformat
->dwHeadBitrate
= format
->nAvgBytesPerSec
* 8;
448 mpgformat
->fwHeadMode
= ((mode
== 3) ? ACM_MPEG_SINGLECHANNEL
:
449 ((mode
== 2) ? ACM_MPEG_DUALCHANNEL
:
450 ((mode
== 1) ? ACM_MPEG_JOINTSTEREO
:
452 mpgformat
->fwHeadModeExt
= ((mode
== 1) ? 0x0F : (1<<mode_ext
));
453 mpgformat
->wHeadEmphasis
= emphasis
+ 1;
454 mpgformat
->fwHeadFlags
= ACM_MPEG_ID_MPEG1
;
456 pamt
->subtype
.Data1
= format
->wFormatTag
;
458 TRACE("MPEG audio stream detected:\n"
461 "\tChannels: %d (%d)\n"
462 "\tBytesPerSec: %d\n",
463 layer
, format
->wFormatTag
, format
->nSamplesPerSec
,
464 format
->nChannels
, mode
, format
->nAvgBytesPerSec
);
466 dump_AM_MEDIA_TYPE(pamt
);
472 static HRESULT
MPEGSplitter_pre_connect(IPin
*iface
, IPin
*pConnectPin
, ALLOCATOR_PROPERTIES
*props
)
474 PullPin
*pPin
= impl_PullPin_from_IPin(iface
);
475 MPEGSplitterImpl
*This
= (MPEGSplitterImpl
*)pPin
->pin
.pinInfo
.pFilter
;
477 LONGLONG pos
= 0; /* in bytes */
480 LONGLONG total
, avail
;
484 IAsyncReader_Length(pPin
->pReader
, &total
, &avail
);
485 This
->EndOfFile
= total
;
487 hr
= IAsyncReader_SyncRead(pPin
->pReader
, pos
, 4, header
);
491 /* Skip ID3 v2 tag, if any */
492 if (SUCCEEDED(hr
) && !memcmp("ID3", header
, 3))
495 hr
= IAsyncReader_SyncRead(pPin
->pReader
, pos
, 6, header
+ 4);
499 TRACE("Found ID3 v2.%d.%d\n", header
[3], header
[4]);
500 if(header
[3] <= 4 && header
[4] != 0xff &&
501 (header
[5]&0x0f) == 0 && (header
[6]&0x80) == 0 &&
502 (header
[7]&0x80) == 0 && (header
[8]&0x80) == 0 &&
503 (header
[9]&0x80) == 0)
505 length
= (header
[6]<<21) | (header
[7]<<14) |
506 (header
[8]<< 7) | (header
[9] );
509 TRACE("Length: %u\n", length
);
513 /* Read the real header for the mpeg splitter */
514 hr
= IAsyncReader_SyncRead(pPin
->pReader
, pos
, 4, header
);
517 TRACE("%x:%x:%x:%x\n", header
[0], header
[1], header
[2], header
[3]);
520 while(SUCCEEDED(hr
) && !(streamtype
=MPEGSplitter_head_check(header
)))
522 TRACE("%x:%x:%x:%x\n", header
[0], header
[1], header
[2], header
[3]);
523 /* No valid header yet; shift by a byte and check again */
524 memmove(header
, header
+1, 3);
525 hr
= IAsyncReader_SyncRead(pPin
->pReader
, pos
++, 1, header
+ 3);
530 This
->begin_offset
= pos
;
531 memcpy(This
->header
, header
, 4);
535 case MPEG_AUDIO_HEADER
:
537 LONGLONG duration
= 0;
538 WAVEFORMATEX
*format
;
540 hr
= MPEGSplitter_init_audio(This
, header
, &piOutput
, &amt
);
543 format
= (WAVEFORMATEX
*)amt
.pbFormat
;
547 /* Make the output buffer a multiple of the frame size */
548 props
->cbBuffer
= 0x4000 / format
->nBlockAlign
*
551 hr
= Parser_AddPin(&(This
->Parser
), &piOutput
, props
, &amt
);
557 CoTaskMemFree(amt
.pbFormat
);
558 ERR("Could not create pin for MPEG audio stream (%x)\n", hr
);
562 /* Check for idv1 tag, and remove it from stream if found */
563 hr
= IAsyncReader_SyncRead(pPin
->pReader
, This
->EndOfFile
-128, 3, header
);
566 if (!strncmp((char*)header
, "TAG", 3))
567 This
->EndOfFile
-= 128;
568 This
->Parser
.pInputPin
->rtStop
= MEDIATIME_FROM_BYTES(This
->EndOfFile
);
569 This
->Parser
.pInputPin
->rtStart
= This
->Parser
.pInputPin
->rtCurrent
= MEDIATIME_FROM_BYTES(This
->begin_offset
);
571 duration
= (This
->EndOfFile
-This
->begin_offset
) * 10000000 / format
->nAvgBytesPerSec
;
572 TRACE("Duration: %d seconds\n", (DWORD
)(duration
/ 10000000));
574 This
->Parser
.sourceSeeking
.llCurrent
= 0;
575 This
->Parser
.sourceSeeking
.llDuration
= duration
;
576 This
->Parser
.sourceSeeking
.llStop
= duration
;
579 case MPEG_VIDEO_HEADER
:
580 FIXME("MPEG video processing not yet supported!\n");
583 case MPEG_SYSTEM_HEADER
:
584 FIXME("MPEG system streams not yet supported!\n");
596 static HRESULT
MPEGSplitter_cleanup(LPVOID iface
)
598 MPEGSplitterImpl
*This
= iface
;
600 TRACE("(%p)\n", This
);
605 static HRESULT WINAPI
MPEGSplitter_seek(IMediaSeeking
*iface
)
607 MPEGSplitterImpl
*This
= impl_from_IMediaSeeking(iface
);
608 PullPin
*pPin
= This
->Parser
.pInputPin
;
609 LONGLONG newpos
, timepos
, bytepos
;
610 HRESULT hr
= E_INVALIDARG
;
613 newpos
= This
->Parser
.sourceSeeking
.llCurrent
;
614 if (This
->position
/1000000 == newpos
/1000000)
616 TRACE("Requesting position %x%08x same as current position %x%08x\n", (DWORD
)(newpos
>>32), (DWORD
)newpos
, (DWORD
)(This
->position
>>32), (DWORD
)This
->position
);
620 bytepos
= This
->begin_offset
;
622 /* http://mpgedit.org/mpgedit/mpeg_format/mpeghdr.htm has a whole read up on audio headers */
623 while (bytepos
+ 3 < This
->EndOfFile
)
625 LONGLONG duration
= timepos
;
627 hr
= IAsyncReader_SyncRead(pPin
->pReader
, bytepos
, 4, header
);
630 while ((hr
=parse_header(header
, &length
, &duration
)) != S_OK
&&
631 bytepos
+ 4 < This
->EndOfFile
)
633 /* No valid header yet; shift by a byte and check again */
634 memmove(header
, header
+1, 3);
635 hr
= IAsyncReader_SyncRead(pPin
->pReader
, ++bytepos
+ 3, 1, header
+ 3);
639 if (hr
!= S_OK
|| duration
> newpos
)
647 PullPin
*pin
= This
->Parser
.pInputPin
;
649 TRACE("Moving sound to %08u bytes!\n", (DWORD
)bytepos
);
651 EnterCriticalSection(&pin
->thread_lock
);
652 IPin_BeginFlush(&pin
->pin
.IPin_iface
);
654 /* Make sure this is done while stopped, BeginFlush takes care of this */
655 EnterCriticalSection(&This
->Parser
.filter
.csFilter
);
656 memcpy(This
->header
, header
, 4);
658 pin
->rtStart
= pin
->rtCurrent
= MEDIATIME_FROM_BYTES(bytepos
);
659 pin
->rtStop
= MEDIATIME_FROM_BYTES((REFERENCE_TIME
)This
->EndOfFile
);
661 This
->position
= newpos
;
662 LeaveCriticalSection(&This
->Parser
.filter
.csFilter
);
664 TRACE("Done flushing\n");
665 IPin_EndFlush(&pin
->pin
.IPin_iface
);
666 LeaveCriticalSection(&pin
->thread_lock
);
671 static HRESULT
MPEGSplitter_disconnect(LPVOID iface
)
673 /* TODO: Find memory leaks etc */
677 static HRESULT
MPEGSplitter_first_request(LPVOID iface
)
679 MPEGSplitterImpl
*This
= iface
;
680 PullPin
*pin
= This
->Parser
.pInputPin
;
683 IMediaSample
*sample
;
685 TRACE("Seeking? %d\n", This
->seek
);
687 hr
= parse_header(This
->header
, &length
, NULL
);
690 if (pin
->rtCurrent
>= pin
->rtStop
)
692 /* Last sample has already been queued, request nothing more */
697 hr
= IMemAllocator_GetBuffer(pin
->pAlloc
, &sample
, NULL
, NULL
, 0);
699 pin
->rtNext
= pin
->rtCurrent
;
702 LONGLONG rtSampleStart
= pin
->rtNext
;
703 /* Add 4 for the next header, which should hopefully work */
704 LONGLONG rtSampleStop
= rtSampleStart
+ MEDIATIME_FROM_BYTES(length
+ 4);
706 if (rtSampleStop
> pin
->rtStop
)
707 rtSampleStop
= MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(pin
->rtStop
), pin
->cbAlign
));
709 IMediaSample_SetTime(sample
, &rtSampleStart
, &rtSampleStop
);
710 IMediaSample_SetPreroll(sample
, FALSE
);
711 IMediaSample_SetDiscontinuity(sample
, TRUE
);
712 IMediaSample_SetSyncPoint(sample
, 1);
715 hr
= IAsyncReader_Request(pin
->pReader
, sample
, 0);
718 pin
->rtCurrent
= pin
->rtNext
;
719 pin
->rtNext
= rtSampleStop
;
722 IMediaSample_Release(sample
);
725 ERR("Horsemen of the apocalypse came to bring error 0x%08x\n", hr
);
730 static const IBaseFilterVtbl MPEGSplitter_Vtbl
=
732 Parser_QueryInterface
,
740 Parser_SetSyncSource
,
741 Parser_GetSyncSource
,
744 Parser_QueryFilterInfo
,
745 Parser_JoinFilterGraph
,
746 Parser_QueryVendorInfo
749 HRESULT
MPEGSplitter_create(IUnknown
* pUnkOuter
, LPVOID
* ppv
)
751 MPEGSplitterImpl
*This
;
754 TRACE("(%p, %p)\n", pUnkOuter
, ppv
);
759 return CLASS_E_NOAGGREGATION
;
761 This
= CoTaskMemAlloc(sizeof(MPEGSplitterImpl
));
763 return E_OUTOFMEMORY
;
765 ZeroMemory(This
, sizeof(MPEGSplitterImpl
));
766 hr
= Parser_Create(&(This
->Parser
), &MPEGSplitter_Vtbl
, &CLSID_MPEG1Splitter
, MPEGSplitter_process_sample
, MPEGSplitter_query_accept
, MPEGSplitter_pre_connect
, MPEGSplitter_cleanup
, MPEGSplitter_disconnect
, MPEGSplitter_first_request
, NULL
, NULL
, MPEGSplitter_seek
, NULL
);
774 /* Note: This memory is managed by the parser filter once created */