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 (!(header
[0] == 0xff && ((header
[1]>>5)&0x7) == 0x7 &&
118 ((header
[1]>>1)&0x3) != 0 && ((header
[2]>>4)&0xf) != 0xf &&
119 ((header
[2]>>2)&0x3) != 0x3))
121 FIXME("Not a valid header: %02x:%02x:%02x:%02x\n", header
[0], header
[1], header
[2], header
[3]);
125 mpeg1
= (header
[1]>>4)&0x1;
127 lsf
= ((header
[1]>>3)&0x1)^1;
129 layer
= 4-((header
[1]>>1)&0x3);
130 bitrate_index
= ((header
[2]>>4)&0xf);
131 freq_index
= ((header
[2]>>2)&0x3) + (mpeg1
?(lsf
*3):6);
132 padding
= ((header
[2]>>1)&0x1);
134 bitrate
= tabsel_123
[lsf
][layer
-1][bitrate_index
] * 1000;
137 FIXME("Not a valid header: %02x:%02x:%02x:%02x\n", header
[0], header
[1], header
[2], header
[3]);
142 length
= 4 * (12 * bitrate
/ freqs
[freq_index
] + padding
);
144 length
= 144 * bitrate
/ freqs
[freq_index
] + padding
;
146 length
= 144 * bitrate
/ (freqs
[freq_index
]<<lsf
) + padding
;
149 ERR("Impossible layer %d\n", layer
);
153 duration
= (ULONGLONG
)10000000 * (ULONGLONG
)(length
) / (ULONGLONG
)(bitrate
/8);
156 *pduration
+= duration
;
160 static HRESULT
FillBuffer(MPEGSplitterImpl
*This
, IMediaSample
*pCurrentSample
)
162 Parser_OutputPin
* pOutputPin
= unsafe_impl_Parser_OutputPin_from_IPin(This
->Parser
.ppPins
[1]);
164 LONGLONG pos
= BYTES_FROM_MEDIATIME(This
->Parser
.pInputPin
->rtNext
);
165 LONGLONG time
= This
->position
, rtstop
, rtstart
;
168 DWORD len
= IMediaSample_GetActualDataLength(pCurrentSample
);
170 TRACE("Source length: %u\n", len
);
171 IMediaSample_GetPointer(pCurrentSample
, &fbuf
);
173 /* Find the next valid header.. it <SHOULD> be right here */
174 hr
= parse_header(fbuf
, &length
, &This
->position
);
176 IMediaSample_SetActualDataLength(pCurrentSample
, length
);
178 /* Queue the next sample */
179 if (length
+ 4 == len
)
181 PullPin
*pin
= This
->Parser
.pInputPin
;
182 LONGLONG stop
= BYTES_FROM_MEDIATIME(pin
->rtStop
);
185 memcpy(This
->header
, fbuf
+ length
, 4);
186 while (FAILED(hr
= parse_header(This
->header
, &length
, NULL
)))
188 memmove(This
->header
, This
->header
+1, 3);
191 IAsyncReader_SyncRead(pin
->pReader
, ++pos
, 1, This
->header
+ 3);
193 pin
->rtNext
= MEDIATIME_FROM_BYTES(pos
);
197 /* Remove 4 for the last header, which should hopefully work */
198 IMediaSample
*sample
= NULL
;
199 LONGLONG rtSampleStart
= pin
->rtNext
- MEDIATIME_FROM_BYTES(4);
200 LONGLONG rtSampleStop
= rtSampleStart
+ MEDIATIME_FROM_BYTES(length
+ 4);
202 if (rtSampleStop
> pin
->rtStop
)
203 rtSampleStop
= MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(pin
->rtStop
), pin
->cbAlign
));
205 hr
= IMemAllocator_GetBuffer(pin
->pAlloc
, &sample
, NULL
, NULL
, 0);
208 IMediaSample_SetTime(sample
, &rtSampleStart
, &rtSampleStop
);
209 IMediaSample_SetPreroll(sample
, 0);
210 IMediaSample_SetDiscontinuity(sample
, 0);
211 IMediaSample_SetSyncPoint(sample
, 1);
212 hr
= IAsyncReader_Request(pin
->pReader
, sample
, 0);
215 pin
->rtCurrent
= rtSampleStart
;
216 pin
->rtNext
= rtSampleStop
;
219 IMediaSample_Release(sample
);
222 FIXME("o_Ox%08x\n", hr
);
225 /* If not, we're presumably at the end of file */
227 TRACE("Media time : %u.%03u\n", (DWORD
)(This
->position
/10000000), (DWORD
)((This
->position
/10000)%1000));
229 if (IMediaSample_IsDiscontinuity(pCurrentSample
) == S_OK
) {
231 EnterCriticalSection(&This
->Parser
.filter
.csFilter
);
232 pOutputPin
->pin
.pin
.tStart
= time
;
233 pOutputPin
->pin
.pin
.dRate
= This
->Parser
.sourceSeeking
.dRate
;
234 hr
= IPin_ConnectedTo(&pOutputPin
->pin
.pin
.IPin_iface
, &victim
);
237 hr
= IPin_NewSegment(victim
, time
, This
->Parser
.sourceSeeking
.llStop
,
238 This
->Parser
.sourceSeeking
.dRate
);
240 FIXME("NewSegment returns %08x\n", hr
);
241 IPin_Release(victim
);
243 LeaveCriticalSection(&This
->Parser
.filter
.csFilter
);
247 rtstart
= (double)(time
- pOutputPin
->pin
.pin
.tStart
) / pOutputPin
->pin
.pin
.dRate
;
248 rtstop
= (double)(This
->position
- pOutputPin
->pin
.pin
.tStart
) / pOutputPin
->pin
.pin
.dRate
;
249 IMediaSample_SetTime(pCurrentSample
, &rtstart
, &rtstop
);
250 IMediaSample_SetMediaTime(pCurrentSample
, &time
, &This
->position
);
252 hr
= BaseOutputPinImpl_Deliver(&pOutputPin
->pin
, pCurrentSample
);
257 TRACE("Error sending sample (%x)\n", hr
);
259 TRACE("S_FALSE (%d), holding\n", IMediaSample_GetActualDataLength(pCurrentSample
));
266 static HRESULT
MPEGSplitter_process_sample(LPVOID iface
, IMediaSample
* pSample
, DWORD_PTR cookie
)
268 MPEGSplitterImpl
*This
= iface
;
270 DWORD cbSrcStream
= 0;
271 REFERENCE_TIME tStart
, tStop
, tAviStart
= This
->position
;
274 hr
= IMediaSample_GetTime(pSample
, &tStart
, &tStop
);
277 cbSrcStream
= IMediaSample_GetActualDataLength(pSample
);
278 hr
= IMediaSample_GetPointer(pSample
, &pbSrcStream
);
281 /* Flush occurring */
282 if (cbSrcStream
== 0)
284 FIXME(".. Why do I need you?\n");
288 /* trace removed for performance reasons */
289 /* TRACE("(%p), %llu -> %llu\n", pSample, tStart, tStop); */
291 /* Now, try to find a new header */
292 hr
= FillBuffer(This
, pSample
);
295 WARN("Failed with hres: %08x!\n", hr
);
297 /* Unset progression if denied! */
298 if (hr
== VFW_E_WRONG_STATE
|| hr
== S_FALSE
)
300 memcpy(This
->header
, pbSrcStream
, 4);
301 This
->Parser
.pInputPin
->rtCurrent
= tStart
;
302 This
->position
= tAviStart
;
306 if (BYTES_FROM_MEDIATIME(tStop
) >= This
->EndOfFile
|| This
->position
>= This
->Parser
.sourceSeeking
.llStop
)
310 TRACE("End of file reached\n");
312 for (i
= 0; i
< This
->Parser
.cStreams
; i
++)
316 hr
= IPin_ConnectedTo(This
->Parser
.ppPins
[i
+1], &ppin
);
319 hr
= IPin_EndOfStream(ppin
);
323 WARN("Error sending EndOfStream to pin %u (%x)\n", i
, hr
);
326 /* Force the pullpin thread to stop */
334 static HRESULT
MPEGSplitter_query_accept(LPVOID iface
, const AM_MEDIA_TYPE
*pmt
)
336 if (!IsEqualIID(&pmt
->majortype
, &MEDIATYPE_Stream
))
339 if (IsEqualIID(&pmt
->subtype
, &MEDIASUBTYPE_MPEG1Audio
))
342 if (IsEqualIID(&pmt
->subtype
, &MEDIASUBTYPE_MPEG1Video
))
343 FIXME("MPEG-1 video streams not yet supported.\n");
344 else if (IsEqualIID(&pmt
->subtype
, &MEDIASUBTYPE_MPEG1System
))
345 FIXME("MPEG-1 system streams not yet supported.\n");
346 else if (IsEqualIID(&pmt
->subtype
, &MEDIASUBTYPE_MPEG1VideoCD
))
347 FIXME("MPEG-1 VideoCD streams not yet supported.\n");
348 else FIXME("%s\n", debugstr_guid(&pmt
->subtype
));
354 static HRESULT
MPEGSplitter_init_audio(MPEGSplitterImpl
*This
, const BYTE
*header
, PIN_INFO
*ppiOutput
, AM_MEDIA_TYPE
*pamt
)
356 WAVEFORMATEX
*format
;
366 ZeroMemory(pamt
, sizeof(*pamt
));
367 ppiOutput
->dir
= PINDIR_OUTPUT
;
368 ppiOutput
->pFilter
= (IBaseFilter
*)This
;
369 wsprintfW(ppiOutput
->achName
, wszAudioStream
);
371 pamt
->formattype
= FORMAT_WaveFormatEx
;
372 pamt
->majortype
= MEDIATYPE_Audio
;
373 pamt
->subtype
= MEDIASUBTYPE_MPEG1AudioPayload
;
375 pamt
->lSampleSize
= 0;
376 pamt
->bFixedSizeSamples
= FALSE
;
377 pamt
->bTemporalCompression
= 0;
379 mpeg1
= (header
[1]>>4)&0x1;
381 lsf
= ((header
[1]>>3)&0x1)^1;
383 layer
= 4-((header
[1]>>1)&0x3);
384 bitrate_index
= ((header
[2]>>4)&0xf);
385 freq_index
= ((header
[2]>>2)&0x3) + (mpeg1
?(lsf
*3):6);
386 mode
= ((header
[3]>>6)&0x3);
387 mode_ext
= ((header
[3]>>4)&0x3);
388 emphasis
= ((header
[3]>>0)&0x3);
392 /* Set to highest bitrate so samples will fit in for sure */
393 FIXME("Variable-bitrate audio not fully supported.\n");
397 pamt
->cbFormat
= ((layer
==3)? sizeof(MPEGLAYER3WAVEFORMAT
) :
398 sizeof(MPEG1WAVEFORMAT
));
399 pamt
->pbFormat
= CoTaskMemAlloc(pamt
->cbFormat
);
401 return E_OUTOFMEMORY
;
402 ZeroMemory(pamt
->pbFormat
, pamt
->cbFormat
);
403 format
= (WAVEFORMATEX
*)pamt
->pbFormat
;
405 format
->wFormatTag
= ((layer
== 3) ? WAVE_FORMAT_MPEGLAYER3
:
407 format
->nChannels
= ((mode
== 3) ? 1 : 2);
408 format
->nSamplesPerSec
= freqs
[freq_index
];
409 format
->nAvgBytesPerSec
= tabsel_123
[lsf
][layer
-1][bitrate_index
] * 1000 / 8;
412 format
->nBlockAlign
= format
->nAvgBytesPerSec
* 8 * 144 /
413 (format
->nSamplesPerSec
<<lsf
) + 1;
415 format
->nBlockAlign
= format
->nAvgBytesPerSec
* 8 * 144 /
416 format
->nSamplesPerSec
+ 1;
418 format
->nBlockAlign
= 4 * (format
->nAvgBytesPerSec
* 8 * 12 / format
->nSamplesPerSec
+ 1);
420 format
->wBitsPerSample
= 0;
424 MPEGLAYER3WAVEFORMAT
*mp3format
= (MPEGLAYER3WAVEFORMAT
*)format
;
426 format
->cbSize
= MPEGLAYER3_WFX_EXTRA_BYTES
;
428 mp3format
->wID
= MPEGLAYER3_ID_MPEG
;
429 mp3format
->fdwFlags
= MPEGLAYER3_FLAG_PADDING_ON
;
430 mp3format
->nBlockSize
= format
->nBlockAlign
;
431 mp3format
->nFramesPerBlock
= 1;
433 /* Beware the evil magic numbers. This struct is apparently horribly
434 * under-documented, and the only references I could find had it being
435 * set to this with no real explanation. It works fine though, so I'm
436 * not complaining (yet).
438 mp3format
->nCodecDelay
= 1393;
442 MPEG1WAVEFORMAT
*mpgformat
= (MPEG1WAVEFORMAT
*)format
;
446 mpgformat
->fwHeadLayer
= ((layer
== 1) ? ACM_MPEG_LAYER1
:
447 ((layer
== 2) ? ACM_MPEG_LAYER2
:
449 mpgformat
->dwHeadBitrate
= format
->nAvgBytesPerSec
* 8;
450 mpgformat
->fwHeadMode
= ((mode
== 3) ? ACM_MPEG_SINGLECHANNEL
:
451 ((mode
== 2) ? ACM_MPEG_DUALCHANNEL
:
452 ((mode
== 1) ? ACM_MPEG_JOINTSTEREO
:
454 mpgformat
->fwHeadModeExt
= ((mode
== 1) ? 0x0F : (1<<mode_ext
));
455 mpgformat
->wHeadEmphasis
= emphasis
+ 1;
456 mpgformat
->fwHeadFlags
= ACM_MPEG_ID_MPEG1
;
458 pamt
->subtype
.Data1
= format
->wFormatTag
;
460 TRACE("MPEG audio stream detected:\n"
463 "\tChannels: %d (%d)\n"
464 "\tBytesPerSec: %d\n",
465 layer
, format
->wFormatTag
, format
->nSamplesPerSec
,
466 format
->nChannels
, mode
, format
->nAvgBytesPerSec
);
468 dump_AM_MEDIA_TYPE(pamt
);
474 static HRESULT
MPEGSplitter_pre_connect(IPin
*iface
, IPin
*pConnectPin
, ALLOCATOR_PROPERTIES
*props
)
476 PullPin
*pPin
= impl_PullPin_from_IPin(iface
);
477 MPEGSplitterImpl
*This
= (MPEGSplitterImpl
*)pPin
->pin
.pinInfo
.pFilter
;
479 LONGLONG pos
= 0; /* in bytes */
482 LONGLONG total
, avail
;
486 IAsyncReader_Length(pPin
->pReader
, &total
, &avail
);
487 This
->EndOfFile
= total
;
489 hr
= IAsyncReader_SyncRead(pPin
->pReader
, pos
, 4, header
);
493 /* Skip ID3 v2 tag, if any */
494 if (SUCCEEDED(hr
) && !memcmp("ID3", header
, 3))
497 hr
= IAsyncReader_SyncRead(pPin
->pReader
, pos
, 6, header
+ 4);
501 TRACE("Found ID3 v2.%d.%d\n", header
[3], header
[4]);
502 if(header
[3] <= 4 && header
[4] != 0xff &&
503 (header
[5]&0x0f) == 0 && (header
[6]&0x80) == 0 &&
504 (header
[7]&0x80) == 0 && (header
[8]&0x80) == 0 &&
505 (header
[9]&0x80) == 0)
507 length
= (header
[6]<<21) | (header
[7]<<14) |
508 (header
[8]<< 7) | (header
[9] );
511 TRACE("Length: %u\n", length
);
515 /* Read the real header for the mpeg splitter */
516 hr
= IAsyncReader_SyncRead(pPin
->pReader
, pos
, 4, header
);
519 TRACE("%x:%x:%x:%x\n", header
[0], header
[1], header
[2], header
[3]);
522 while(SUCCEEDED(hr
) && !(streamtype
=MPEGSplitter_head_check(header
)))
524 TRACE("%x:%x:%x:%x\n", header
[0], header
[1], header
[2], header
[3]);
525 /* No valid header yet; shift by a byte and check again */
526 memmove(header
, header
+1, 3);
527 hr
= IAsyncReader_SyncRead(pPin
->pReader
, pos
++, 1, header
+ 3);
532 This
->begin_offset
= pos
;
533 memcpy(This
->header
, header
, 4);
537 case MPEG_AUDIO_HEADER
:
539 LONGLONG duration
= 0;
540 WAVEFORMATEX
*format
;
542 hr
= MPEGSplitter_init_audio(This
, header
, &piOutput
, &amt
);
545 format
= (WAVEFORMATEX
*)amt
.pbFormat
;
549 /* Make the output buffer a multiple of the frame size */
550 props
->cbBuffer
= 0x4000 / format
->nBlockAlign
*
553 hr
= Parser_AddPin(&(This
->Parser
), &piOutput
, props
, &amt
);
559 CoTaskMemFree(amt
.pbFormat
);
560 ERR("Could not create pin for MPEG audio stream (%x)\n", hr
);
564 /* Check for idv1 tag, and remove it from stream if found */
565 hr
= IAsyncReader_SyncRead(pPin
->pReader
, This
->EndOfFile
-128, 3, header
);
568 if (!strncmp((char*)header
, "TAG", 3))
569 This
->EndOfFile
-= 128;
570 This
->Parser
.pInputPin
->rtStop
= MEDIATIME_FROM_BYTES(This
->EndOfFile
);
571 This
->Parser
.pInputPin
->rtStart
= This
->Parser
.pInputPin
->rtCurrent
= MEDIATIME_FROM_BYTES(This
->begin_offset
);
573 duration
= (This
->EndOfFile
-This
->begin_offset
) * 10000000 / format
->nAvgBytesPerSec
;
574 TRACE("Duration: %d seconds\n", (DWORD
)(duration
/ 10000000));
576 This
->Parser
.sourceSeeking
.llCurrent
= 0;
577 This
->Parser
.sourceSeeking
.llDuration
= duration
;
578 This
->Parser
.sourceSeeking
.llStop
= duration
;
581 case MPEG_VIDEO_HEADER
:
582 FIXME("MPEG video processing not yet supported!\n");
585 case MPEG_SYSTEM_HEADER
:
586 FIXME("MPEG system streams not yet supported!\n");
598 static HRESULT
MPEGSplitter_cleanup(LPVOID iface
)
600 MPEGSplitterImpl
*This
= iface
;
602 TRACE("(%p)\n", This
);
607 static HRESULT WINAPI
MPEGSplitter_seek(IMediaSeeking
*iface
)
609 MPEGSplitterImpl
*This
= impl_from_IMediaSeeking(iface
);
610 PullPin
*pPin
= This
->Parser
.pInputPin
;
611 LONGLONG newpos
, timepos
, bytepos
;
612 HRESULT hr
= E_INVALIDARG
;
615 newpos
= This
->Parser
.sourceSeeking
.llCurrent
;
616 if (This
->position
/1000000 == newpos
/1000000)
618 TRACE("Requesting position %x%08x same as current position %x%08x\n", (DWORD
)(newpos
>>32), (DWORD
)newpos
, (DWORD
)(This
->position
>>32), (DWORD
)This
->position
);
622 bytepos
= This
->begin_offset
;
624 /* http://mpgedit.org/mpgedit/mpeg_format/mpeghdr.htm has a whole read up on audio headers */
625 while (bytepos
+ 3 < This
->EndOfFile
)
627 LONGLONG duration
= timepos
;
629 hr
= IAsyncReader_SyncRead(pPin
->pReader
, bytepos
, 4, header
);
632 while ((hr
=parse_header(header
, &length
, &duration
)) != S_OK
&&
633 bytepos
+ 4 < This
->EndOfFile
)
635 /* No valid header yet; shift by a byte and check again */
636 memmove(header
, header
+1, 3);
637 hr
= IAsyncReader_SyncRead(pPin
->pReader
, ++bytepos
+ 3, 1, header
+ 3);
641 if (hr
!= S_OK
|| duration
> newpos
)
649 PullPin
*pin
= This
->Parser
.pInputPin
;
651 TRACE("Moving sound to %08u bytes!\n", (DWORD
)bytepos
);
653 EnterCriticalSection(&pin
->thread_lock
);
654 IPin_BeginFlush(&pin
->pin
.IPin_iface
);
656 /* Make sure this is done while stopped, BeginFlush takes care of this */
657 EnterCriticalSection(&This
->Parser
.filter
.csFilter
);
658 memcpy(This
->header
, header
, 4);
660 pin
->rtStart
= pin
->rtCurrent
= MEDIATIME_FROM_BYTES(bytepos
);
661 pin
->rtStop
= MEDIATIME_FROM_BYTES((REFERENCE_TIME
)This
->EndOfFile
);
663 This
->position
= newpos
;
664 LeaveCriticalSection(&This
->Parser
.filter
.csFilter
);
666 TRACE("Done flushing\n");
667 IPin_EndFlush(&pin
->pin
.IPin_iface
);
668 LeaveCriticalSection(&pin
->thread_lock
);
673 static HRESULT
MPEGSplitter_disconnect(LPVOID iface
)
675 /* TODO: Find memory leaks etc */
679 static HRESULT
MPEGSplitter_first_request(LPVOID iface
)
681 MPEGSplitterImpl
*This
= iface
;
682 PullPin
*pin
= This
->Parser
.pInputPin
;
685 IMediaSample
*sample
;
687 TRACE("Seeking? %d\n", This
->seek
);
689 hr
= parse_header(This
->header
, &length
, NULL
);
692 if (pin
->rtCurrent
>= pin
->rtStop
)
694 /* Last sample has already been queued, request nothing more */
699 hr
= IMemAllocator_GetBuffer(pin
->pAlloc
, &sample
, NULL
, NULL
, 0);
701 pin
->rtNext
= pin
->rtCurrent
;
704 LONGLONG rtSampleStart
= pin
->rtNext
;
705 /* Add 4 for the next header, which should hopefully work */
706 LONGLONG rtSampleStop
= rtSampleStart
+ MEDIATIME_FROM_BYTES(length
+ 4);
708 if (rtSampleStop
> pin
->rtStop
)
709 rtSampleStop
= MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(pin
->rtStop
), pin
->cbAlign
));
711 hr
= IMediaSample_SetTime(sample
, &rtSampleStart
, &rtSampleStop
);
713 IMediaSample_SetPreroll(sample
, FALSE
);
714 IMediaSample_SetDiscontinuity(sample
, TRUE
);
715 IMediaSample_SetSyncPoint(sample
, 1);
718 hr
= IAsyncReader_Request(pin
->pReader
, sample
, 0);
721 pin
->rtCurrent
= pin
->rtNext
;
722 pin
->rtNext
= rtSampleStop
;
725 IMediaSample_Release(sample
);
728 ERR("Horsemen of the apocalypse came to bring error 0x%08x\n", hr
);
733 static const IBaseFilterVtbl MPEGSplitter_Vtbl
=
735 Parser_QueryInterface
,
743 Parser_SetSyncSource
,
744 Parser_GetSyncSource
,
747 Parser_QueryFilterInfo
,
748 Parser_JoinFilterGraph
,
749 Parser_QueryVendorInfo
752 HRESULT
MPEGSplitter_create(IUnknown
* pUnkOuter
, LPVOID
* ppv
)
754 MPEGSplitterImpl
*This
;
757 TRACE("(%p, %p)\n", pUnkOuter
, ppv
);
762 return CLASS_E_NOAGGREGATION
;
764 This
= CoTaskMemAlloc(sizeof(MPEGSplitterImpl
));
766 return E_OUTOFMEMORY
;
768 ZeroMemory(This
, sizeof(MPEGSplitterImpl
));
769 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
);
777 /* Note: This memory is managed by the parser filter once created */