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 #define SEEK_INTERVAL (ULONGLONG)(10 * 10000000) /* Add an entry every 10 seconds */
62 typedef struct MPEGSplitterImpl
71 /* Whether we just seeked (or started playing) */
76 struct seek_entry
*seektable
;
79 static inline MPEGSplitterImpl
*impl_from_IMediaSeeking( IMediaSeeking
*iface
)
81 return CONTAINING_RECORD(iface
, MPEGSplitterImpl
, Parser
.sourceSeeking
.IMediaSeeking_iface
);
84 static int MPEGSplitter_head_check(const BYTE
*header
)
86 /* If this is a possible start code, check for a system or video header */
87 if (header
[0] == 0 && header
[1] == 0 && header
[2] == 1)
89 /* Check if we got a system or elementary stream start code */
90 if (header
[3] == PACK_START_CODE
||
91 header
[3] == VIDEO_ELEMENTARY_STREAM
||
92 header
[3] == AUDIO_ELEMENTARY_STREAM
)
93 return MPEG_SYSTEM_HEADER
;
95 /* Check for a MPEG video sequence start code */
96 if (header
[3] == SEQUENCE_HEADER_CODE
)
97 return MPEG_VIDEO_HEADER
;
100 /* This should give a good guess if we have an MPEG audio header */
101 if(header
[0] == 0xff && ((header
[1]>>5)&0x7) == 0x7 &&
102 ((header
[1]>>1)&0x3) != 0 && ((header
[2]>>4)&0xf) != 0xf &&
103 ((header
[2]>>2)&0x3) != 0x3)
104 return MPEG_AUDIO_HEADER
;
107 return MPEG_NO_HEADER
;
110 static const WCHAR wszAudioStream
[] = {'A','u','d','i','o',0};
111 static const WCHAR wszVideoStream
[] = {'V','i','d','e','o',0};
113 static const DWORD freqs
[10] = { 44100, 48000, 32000, 22050, 24000, 16000, 11025, 12000, 8000, 0 };
115 static const DWORD tabsel_123
[2][3][16] = {
116 { {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,},
117 {0,32,48,56, 64, 80, 96,112,128,160,192,224,256,320,384,},
118 {0,32,40,48, 56, 64, 80, 96,112,128,160,192,224,256,320,} },
120 { {0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,},
121 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,},
122 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,} }
125 static HRESULT
parse_header(BYTE
*header
, LONGLONG
*plen
, LONGLONG
*pduration
)
129 int bitrate_index
, freq_index
, lsf
= 1, mpeg1
, layer
, padding
, bitrate
, length
;
130 if (header
[0] != 0xff)
133 if (!(((header
[1]>>5)&0x7) == 0x7 &&
134 ((header
[1]>>1)&0x3) != 0 && ((header
[2]>>4)&0xf) != 0xf &&
135 ((header
[2]>>2)&0x3) != 0x3))
137 FIXME("Not a valid header: %02x:%02x\n", header
[1], header
[2]);
141 mpeg1
= (header
[1]>>4)&0x1;
143 lsf
= ((header
[1]>>3)&0x1)^1;
145 layer
= 4-((header
[1]>>1)&0x3);
146 bitrate_index
= ((header
[2]>>4)&0xf);
147 freq_index
= ((header
[2]>>2)&0x3) + (mpeg1
?(lsf
*3):6);
148 padding
= ((header
[2]>>1)&0x1);
150 bitrate
= tabsel_123
[lsf
][layer
-1][bitrate_index
] * 1000;
153 FIXME("Not a valid header: %02x:%02x:%02x:%02x\n", header
[0], header
[1], header
[2], header
[3]);
158 if (layer
== 3 || layer
== 2)
159 length
= 144 * bitrate
/ freqs
[freq_index
] + padding
;
161 length
= 4 * (12 * bitrate
/ freqs
[freq_index
] + padding
);
163 duration
= (ULONGLONG
)10000000 * (ULONGLONG
)(length
) / (ULONGLONG
)(bitrate
/8);
166 *pduration
+= duration
;
170 static HRESULT
FillBuffer(MPEGSplitterImpl
*This
, IMediaSample
*pCurrentSample
)
172 Parser_OutputPin
* pOutputPin
= unsafe_impl_Parser_OutputPin_from_IPin(This
->Parser
.ppPins
[1]);
174 LONGLONG pos
= BYTES_FROM_MEDIATIME(This
->Parser
.pInputPin
->rtNext
);
175 LONGLONG time
= This
->position
, rtstop
, rtstart
;
178 DWORD len
= IMediaSample_GetActualDataLength(pCurrentSample
);
180 TRACE("Source length: %u\n", len
);
181 IMediaSample_GetPointer(pCurrentSample
, &fbuf
);
183 /* Find the next valid header.. it <SHOULD> be right here */
184 hr
= parse_header(fbuf
, &length
, &This
->position
);
186 IMediaSample_SetActualDataLength(pCurrentSample
, length
);
188 /* Queue the next sample */
189 if (length
+ 4 == len
)
191 PullPin
*pin
= This
->Parser
.pInputPin
;
192 LONGLONG stop
= BYTES_FROM_MEDIATIME(pin
->rtStop
);
195 memcpy(This
->header
, fbuf
+ length
, 4);
196 while (FAILED(hr
= parse_header(This
->header
, &length
, NULL
)))
198 memmove(This
->header
, This
->header
+1, 3);
201 IAsyncReader_SyncRead(pin
->pReader
, ++pos
, 1, This
->header
+ 3);
203 pin
->rtNext
= MEDIATIME_FROM_BYTES(pos
);
207 /* Remove 4 for the last header, which should hopefully work */
208 IMediaSample
*sample
= NULL
;
209 LONGLONG rtSampleStart
= pin
->rtNext
- MEDIATIME_FROM_BYTES(4);
210 LONGLONG rtSampleStop
= rtSampleStart
+ MEDIATIME_FROM_BYTES(length
+ 4);
212 if (rtSampleStop
> pin
->rtStop
)
213 rtSampleStop
= MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(pin
->rtStop
), pin
->cbAlign
));
215 hr
= IMemAllocator_GetBuffer(pin
->pAlloc
, &sample
, NULL
, NULL
, 0);
218 IMediaSample_SetTime(sample
, &rtSampleStart
, &rtSampleStop
);
219 IMediaSample_SetPreroll(sample
, 0);
220 IMediaSample_SetDiscontinuity(sample
, 0);
221 IMediaSample_SetSyncPoint(sample
, 1);
222 hr
= IAsyncReader_Request(pin
->pReader
, sample
, 0);
225 pin
->rtCurrent
= rtSampleStart
;
226 pin
->rtNext
= rtSampleStop
;
229 IMediaSample_Release(sample
);
232 FIXME("o_Ox%08x\n", hr
);
235 /* If not, we're presumably at the end of file */
237 TRACE("Media time : %u.%03u\n", (DWORD
)(This
->position
/10000000), (DWORD
)((This
->position
/10000)%1000));
239 if (IMediaSample_IsDiscontinuity(pCurrentSample
) == S_OK
) {
241 EnterCriticalSection(&This
->Parser
.filter
.csFilter
);
242 pOutputPin
->pin
.pin
.tStart
= time
;
243 pOutputPin
->pin
.pin
.dRate
= This
->Parser
.sourceSeeking
.dRate
;
244 hr
= IPin_ConnectedTo(&pOutputPin
->pin
.pin
.IPin_iface
, &victim
);
247 hr
= IPin_NewSegment(victim
, time
, This
->Parser
.sourceSeeking
.llStop
,
248 This
->Parser
.sourceSeeking
.dRate
);
250 FIXME("NewSegment returns %08x\n", hr
);
251 IPin_Release(victim
);
253 LeaveCriticalSection(&This
->Parser
.filter
.csFilter
);
257 rtstart
= (double)(time
- pOutputPin
->pin
.pin
.tStart
) / pOutputPin
->pin
.pin
.dRate
;
258 rtstop
= (double)(This
->position
- pOutputPin
->pin
.pin
.tStart
) / pOutputPin
->pin
.pin
.dRate
;
259 IMediaSample_SetTime(pCurrentSample
, &rtstart
, &rtstop
);
260 IMediaSample_SetMediaTime(pCurrentSample
, &time
, &This
->position
);
262 hr
= BaseOutputPinImpl_Deliver(&pOutputPin
->pin
, pCurrentSample
);
267 TRACE("Error sending sample (%x)\n", hr
);
269 TRACE("S_FALSE (%d), holding\n", IMediaSample_GetActualDataLength(pCurrentSample
));
276 static HRESULT
MPEGSplitter_process_sample(LPVOID iface
, IMediaSample
* pSample
, DWORD_PTR cookie
)
278 MPEGSplitterImpl
*This
= iface
;
280 DWORD cbSrcStream
= 0;
281 REFERENCE_TIME tStart
, tStop
, tAviStart
= This
->position
;
284 hr
= IMediaSample_GetTime(pSample
, &tStart
, &tStop
);
287 cbSrcStream
= IMediaSample_GetActualDataLength(pSample
);
288 hr
= IMediaSample_GetPointer(pSample
, &pbSrcStream
);
291 /* Flush occurring */
292 if (cbSrcStream
== 0)
294 FIXME(".. Why do I need you?\n");
298 /* trace removed for performance reasons */
299 /* TRACE("(%p), %llu -> %llu\n", pSample, tStart, tStop); */
301 /* Now, try to find a new header */
302 hr
= FillBuffer(This
, pSample
);
305 WARN("Failed with hres: %08x!\n", hr
);
307 /* Unset progression if denied! */
308 if (hr
== VFW_E_WRONG_STATE
|| hr
== S_FALSE
)
310 memcpy(This
->header
, pbSrcStream
, 4);
311 This
->Parser
.pInputPin
->rtCurrent
= tStart
;
312 This
->position
= tAviStart
;
316 if (BYTES_FROM_MEDIATIME(tStop
) >= This
->EndOfFile
|| This
->position
>= This
->Parser
.sourceSeeking
.llStop
)
320 TRACE("End of file reached\n");
322 for (i
= 0; i
< This
->Parser
.cStreams
; i
++)
326 hr
= IPin_ConnectedTo(This
->Parser
.ppPins
[i
+1], &ppin
);
329 hr
= IPin_EndOfStream(ppin
);
333 WARN("Error sending EndOfStream to pin %u (%x)\n", i
, hr
);
336 /* Force the pullpin thread to stop */
344 static HRESULT
MPEGSplitter_query_accept(LPVOID iface
, const AM_MEDIA_TYPE
*pmt
)
346 if (!IsEqualIID(&pmt
->majortype
, &MEDIATYPE_Stream
))
349 if (IsEqualIID(&pmt
->subtype
, &MEDIASUBTYPE_MPEG1Audio
))
352 if (IsEqualIID(&pmt
->subtype
, &MEDIASUBTYPE_MPEG1Video
))
353 FIXME("MPEG-1 video streams not yet supported.\n");
354 else if (IsEqualIID(&pmt
->subtype
, &MEDIASUBTYPE_MPEG1System
))
355 FIXME("MPEG-1 system streams not yet supported.\n");
356 else if (IsEqualIID(&pmt
->subtype
, &MEDIASUBTYPE_MPEG1VideoCD
))
357 FIXME("MPEG-1 VideoCD streams not yet supported.\n");
358 else FIXME("%s\n", debugstr_guid(&pmt
->subtype
));
364 static HRESULT
MPEGSplitter_init_audio(MPEGSplitterImpl
*This
, const BYTE
*header
, PIN_INFO
*ppiOutput
, AM_MEDIA_TYPE
*pamt
)
366 WAVEFORMATEX
*format
;
376 ZeroMemory(pamt
, sizeof(*pamt
));
377 ppiOutput
->dir
= PINDIR_OUTPUT
;
378 ppiOutput
->pFilter
= (IBaseFilter
*)This
;
379 wsprintfW(ppiOutput
->achName
, wszAudioStream
);
381 pamt
->formattype
= FORMAT_WaveFormatEx
;
382 pamt
->majortype
= MEDIATYPE_Audio
;
383 pamt
->subtype
= MEDIASUBTYPE_MPEG1AudioPayload
;
385 pamt
->lSampleSize
= 0;
386 pamt
->bFixedSizeSamples
= FALSE
;
387 pamt
->bTemporalCompression
= 0;
389 mpeg1
= (header
[1]>>4)&0x1;
391 lsf
= ((header
[1]>>3)&0x1)^1;
393 layer
= 4-((header
[1]>>1)&0x3);
394 bitrate_index
= ((header
[2]>>4)&0xf);
395 freq_index
= ((header
[2]>>2)&0x3) + (mpeg1
?(lsf
*3):6);
396 mode
= ((header
[3]>>6)&0x3);
397 mode_ext
= ((header
[3]>>4)&0x3);
398 emphasis
= ((header
[3]>>0)&0x3);
402 /* Set to highest bitrate so samples will fit in for sure */
403 FIXME("Variable-bitrate audio not fully supported.\n");
407 pamt
->cbFormat
= ((layer
==3)? sizeof(MPEGLAYER3WAVEFORMAT
) :
408 sizeof(MPEG1WAVEFORMAT
));
409 pamt
->pbFormat
= CoTaskMemAlloc(pamt
->cbFormat
);
411 return E_OUTOFMEMORY
;
412 ZeroMemory(pamt
->pbFormat
, pamt
->cbFormat
);
413 format
= (WAVEFORMATEX
*)pamt
->pbFormat
;
415 format
->wFormatTag
= ((layer
== 3) ? WAVE_FORMAT_MPEGLAYER3
:
417 format
->nChannels
= ((mode
== 3) ? 1 : 2);
418 format
->nSamplesPerSec
= freqs
[freq_index
];
419 format
->nAvgBytesPerSec
= tabsel_123
[lsf
][layer
-1][bitrate_index
] * 1000 / 8;
422 format
->nBlockAlign
= format
->nAvgBytesPerSec
* 8 * 144 /
423 (format
->nSamplesPerSec
<<lsf
) + 1;
425 format
->nBlockAlign
= format
->nAvgBytesPerSec
* 8 * 144 /
426 format
->nSamplesPerSec
+ 1;
428 format
->nBlockAlign
= 4 * (format
->nAvgBytesPerSec
* 8 * 12 / format
->nSamplesPerSec
+ 1);
430 format
->wBitsPerSample
= 0;
434 MPEGLAYER3WAVEFORMAT
*mp3format
= (MPEGLAYER3WAVEFORMAT
*)format
;
436 format
->cbSize
= MPEGLAYER3_WFX_EXTRA_BYTES
;
438 mp3format
->wID
= MPEGLAYER3_ID_MPEG
;
439 mp3format
->fdwFlags
= MPEGLAYER3_FLAG_PADDING_ON
;
440 mp3format
->nBlockSize
= format
->nBlockAlign
;
441 mp3format
->nFramesPerBlock
= 1;
443 /* Beware the evil magic numbers. This struct is apparently horribly
444 * under-documented, and the only references I could find had it being
445 * set to this with no real explanation. It works fine though, so I'm
446 * not complaining (yet).
448 mp3format
->nCodecDelay
= 1393;
452 MPEG1WAVEFORMAT
*mpgformat
= (MPEG1WAVEFORMAT
*)format
;
456 mpgformat
->fwHeadLayer
= ((layer
== 1) ? ACM_MPEG_LAYER1
:
457 ((layer
== 2) ? ACM_MPEG_LAYER2
:
459 mpgformat
->dwHeadBitrate
= format
->nAvgBytesPerSec
* 8;
460 mpgformat
->fwHeadMode
= ((mode
== 3) ? ACM_MPEG_SINGLECHANNEL
:
461 ((mode
== 2) ? ACM_MPEG_DUALCHANNEL
:
462 ((mode
== 1) ? ACM_MPEG_JOINTSTEREO
:
464 mpgformat
->fwHeadModeExt
= ((mode
== 1) ? 0x0F : (1<<mode_ext
));
465 mpgformat
->wHeadEmphasis
= emphasis
+ 1;
466 mpgformat
->fwHeadFlags
= ACM_MPEG_ID_MPEG1
;
468 pamt
->subtype
.Data1
= format
->wFormatTag
;
470 TRACE("MPEG audio stream detected:\n"
473 "\tChannels: %d (%d)\n"
474 "\tBytesPerSec: %d\n",
475 layer
, format
->wFormatTag
, format
->nSamplesPerSec
,
476 format
->nChannels
, mode
, format
->nAvgBytesPerSec
);
478 dump_AM_MEDIA_TYPE(pamt
);
484 static HRESULT
MPEGSplitter_pre_connect(IPin
*iface
, IPin
*pConnectPin
, ALLOCATOR_PROPERTIES
*props
)
486 PullPin
*pPin
= impl_PullPin_from_IPin(iface
);
487 MPEGSplitterImpl
*This
= (MPEGSplitterImpl
*)pPin
->pin
.pinInfo
.pFilter
;
489 LONGLONG pos
= 0; /* in bytes */
492 LONGLONG total
, avail
;
496 IAsyncReader_Length(pPin
->pReader
, &total
, &avail
);
497 This
->EndOfFile
= total
;
499 hr
= IAsyncReader_SyncRead(pPin
->pReader
, pos
, 4, header
);
503 /* Skip ID3 v2 tag, if any */
504 if (SUCCEEDED(hr
) && !memcmp("ID3", header
, 3))
507 hr
= IAsyncReader_SyncRead(pPin
->pReader
, pos
, 6, header
+ 4);
511 TRACE("Found ID3 v2.%d.%d\n", header
[3], header
[4]);
512 length
= (header
[6] & 0x7F) << 21;
513 length
+= (header
[7] & 0x7F) << 14;
514 length
+= (header
[8] & 0x7F) << 7;
515 length
+= (header
[9] & 0x7F);
516 TRACE("Length: %u\n", length
);
519 /* Read the real header for the mpeg splitter */
520 hr
= IAsyncReader_SyncRead(pPin
->pReader
, pos
, 4, header
);
523 TRACE("%x:%x:%x:%x\n", header
[0], header
[1], header
[2], header
[3]);
526 while(SUCCEEDED(hr
) && !(streamtype
=MPEGSplitter_head_check(header
)))
528 TRACE("%x:%x:%x:%x\n", header
[0], header
[1], header
[2], header
[3]);
529 /* No valid header yet; shift by a byte and check again */
530 memmove(header
, header
+1, 3);
531 hr
= IAsyncReader_SyncRead(pPin
->pReader
, pos
++, 1, header
+ 3);
536 This
->begin_offset
= pos
;
537 memcpy(This
->header
, header
, 4);
539 This
->seektable
[0].bytepos
= pos
;
540 This
->seektable
[0].timepos
= 0;
544 case MPEG_AUDIO_HEADER
:
546 LONGLONG duration
= 0;
547 DWORD last_entry
= 0;
549 DWORD ticks
= GetTickCount();
551 hr
= MPEGSplitter_init_audio(This
, header
, &piOutput
, &amt
);
554 WAVEFORMATEX
*format
= (WAVEFORMATEX
*)amt
.pbFormat
;
558 /* Make the output buffer a multiple of the frame size */
559 props
->cbBuffer
= 0x4000 / format
->nBlockAlign
*
562 hr
= Parser_AddPin(&(This
->Parser
), &piOutput
, props
, &amt
);
568 CoTaskMemFree(amt
.pbFormat
);
569 ERR("Could not create pin for MPEG audio stream (%x)\n", hr
);
573 /* Check for idv1 tag, and remove it from stream if found */
574 hr
= IAsyncReader_SyncRead(pPin
->pReader
, This
->EndOfFile
-128, 3, header
+4);
577 if (!strncmp((char*)header
+4, "TAG", 3))
578 This
->EndOfFile
-= 128;
579 This
->Parser
.pInputPin
->rtStop
= MEDIATIME_FROM_BYTES(This
->EndOfFile
);
580 This
->Parser
.pInputPin
->rtStart
= This
->Parser
.pInputPin
->rtCurrent
= MEDIATIME_FROM_BYTES(This
->begin_offset
);
582 /* http://mpgedit.org/mpgedit/mpeg_format/mpeghdr.htm has a whole read up on audio headers */
583 while (pos
+ 3 < This
->EndOfFile
)
586 hr
= IAsyncReader_SyncRead(pPin
->pReader
, pos
, 4, header
);
589 while (parse_header(header
, &length
, &duration
))
591 /* No valid header yet; shift by a byte and check again */
592 memmove(header
, header
+1, 3);
593 hr
= IAsyncReader_SyncRead(pPin
->pReader
, pos
++, 1, header
+ 3);
594 if (hr
!= S_OK
|| This
->EndOfFile
- pos
< 4)
599 if (This
->seektable
&& (duration
/ SEEK_INTERVAL
) > last_entry
)
601 if (last_entry
+ 1 > duration
/ SEEK_INTERVAL
)
603 ERR("Somehow skipped %d interval lengths instead of 1\n", (DWORD
)(duration
/SEEK_INTERVAL
) - (last_entry
+ 1));
607 TRACE("Entry: %u\n", last_entry
);
608 if (last_entry
>= This
->seek_entries
)
610 This
->seek_entries
+= 64;
611 This
->seektable
= CoTaskMemRealloc(This
->seektable
, (This
->seek_entries
)*sizeof(struct seek_entry
));
613 This
->seektable
[last_entry
].bytepos
= pos
;
614 This
->seektable
[last_entry
].timepos
= duration
;
617 TRACE("Pos: %x%08x/%x%08x\n", (DWORD
)(pos
>> 32), (DWORD
)pos
, (DWORD
)(This
->EndOfFile
>>32), (DWORD
)This
->EndOfFile
);
620 TRACE("Duration: %d seconds\n", (DWORD
)(duration
/ 10000000));
621 TRACE("Parsing took %u ms\n", GetTickCount() - ticks
);
622 This
->duration
= duration
;
624 This
->Parser
.sourceSeeking
.llCurrent
= 0;
625 This
->Parser
.sourceSeeking
.llDuration
= duration
;
626 This
->Parser
.sourceSeeking
.llStop
= duration
;
629 case MPEG_VIDEO_HEADER
:
630 FIXME("MPEG video processing not yet supported!\n");
633 case MPEG_SYSTEM_HEADER
:
634 FIXME("MPEG system streams not yet supported!\n");
646 static HRESULT
MPEGSplitter_cleanup(LPVOID iface
)
648 MPEGSplitterImpl
*This
= iface
;
650 TRACE("(%p)\n", This
);
655 static HRESULT WINAPI
MPEGSplitter_seek(IMediaSeeking
*iface
)
657 MPEGSplitterImpl
*This
= impl_from_IMediaSeeking(iface
);
658 PullPin
*pPin
= This
->Parser
.pInputPin
;
659 LONGLONG newpos
, timepos
, bytepos
;
663 newpos
= This
->Parser
.sourceSeeking
.llCurrent
;
665 if (newpos
> This
->duration
)
667 WARN("Requesting position %x%08x beyond end of stream %x%08x\n", (DWORD
)(newpos
>>32), (DWORD
)newpos
, (DWORD
)(This
->duration
>>32), (DWORD
)This
->duration
);
671 if (This
->position
/1000000 == newpos
/1000000)
673 TRACE("Requesting position %x%08x same as current position %x%08x\n", (DWORD
)(newpos
>>32), (DWORD
)newpos
, (DWORD
)(This
->position
>>32), (DWORD
)This
->position
);
677 /* Position, cached */
678 bytepos
= This
->seektable
[newpos
/ SEEK_INTERVAL
].bytepos
;
679 timepos
= This
->seektable
[newpos
/ SEEK_INTERVAL
].timepos
;
681 hr
= IAsyncReader_SyncRead(pPin
->pReader
, bytepos
, 4, header
);
682 while (bytepos
+ 3 < This
->EndOfFile
)
685 hr
= IAsyncReader_SyncRead(pPin
->pReader
, bytepos
, 4, header
);
686 if (hr
!= S_OK
|| timepos
>= newpos
)
689 while (parse_header(header
, &length
, &timepos
) && bytepos
+ 3 < This
->EndOfFile
)
691 /* No valid header yet; shift by a byte and check again */
692 memmove(header
, header
+1, 3);
693 hr
= IAsyncReader_SyncRead(pPin
->pReader
, ++bytepos
, 1, header
+ 3);
698 TRACE("Pos: %x%08x/%x%08x\n", (DWORD
)(bytepos
>> 32), (DWORD
)bytepos
, (DWORD
)(This
->EndOfFile
>>32), (DWORD
)This
->EndOfFile
);
703 PullPin
*pin
= This
->Parser
.pInputPin
;
705 TRACE("Moving sound to %08u bytes!\n", (DWORD
)bytepos
);
707 EnterCriticalSection(&pin
->thread_lock
);
708 IPin_BeginFlush(&pin
->pin
.IPin_iface
);
710 /* Make sure this is done while stopped, BeginFlush takes care of this */
711 EnterCriticalSection(&This
->Parser
.filter
.csFilter
);
712 memcpy(This
->header
, header
, 4);
714 pin
->rtStart
= pin
->rtCurrent
= MEDIATIME_FROM_BYTES(bytepos
);
715 pin
->rtStop
= MEDIATIME_FROM_BYTES((REFERENCE_TIME
)This
->EndOfFile
);
717 This
->position
= newpos
;
718 LeaveCriticalSection(&This
->Parser
.filter
.csFilter
);
720 TRACE("Done flushing\n");
721 IPin_EndFlush(&pin
->pin
.IPin_iface
);
722 LeaveCriticalSection(&pin
->thread_lock
);
727 static HRESULT
MPEGSplitter_disconnect(LPVOID iface
)
729 /* TODO: Find memory leaks etc */
733 static HRESULT
MPEGSplitter_first_request(LPVOID iface
)
735 MPEGSplitterImpl
*This
= iface
;
736 PullPin
*pin
= This
->Parser
.pInputPin
;
739 IMediaSample
*sample
;
741 TRACE("Seeking? %d\n", This
->seek
);
743 hr
= parse_header(This
->header
, &length
, NULL
);
746 if (pin
->rtCurrent
>= pin
->rtStop
)
748 /* Last sample has already been queued, request nothing more */
753 hr
= IMemAllocator_GetBuffer(pin
->pAlloc
, &sample
, NULL
, NULL
, 0);
755 pin
->rtNext
= pin
->rtCurrent
;
758 LONGLONG rtSampleStart
= pin
->rtNext
;
759 /* Add 4 for the next header, which should hopefully work */
760 LONGLONG rtSampleStop
= rtSampleStart
+ MEDIATIME_FROM_BYTES(length
+ 4);
762 if (rtSampleStop
> pin
->rtStop
)
763 rtSampleStop
= MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(pin
->rtStop
), pin
->cbAlign
));
765 hr
= IMediaSample_SetTime(sample
, &rtSampleStart
, &rtSampleStop
);
767 IMediaSample_SetPreroll(sample
, FALSE
);
768 IMediaSample_SetDiscontinuity(sample
, TRUE
);
769 IMediaSample_SetSyncPoint(sample
, 1);
772 hr
= IAsyncReader_Request(pin
->pReader
, sample
, 0);
775 pin
->rtCurrent
= pin
->rtNext
;
776 pin
->rtNext
= rtSampleStop
;
779 IMediaSample_Release(sample
);
782 ERR("Horsemen of the apocalypse came to bring error 0x%08x\n", hr
);
787 static const IBaseFilterVtbl MPEGSplitter_Vtbl
=
789 Parser_QueryInterface
,
797 Parser_SetSyncSource
,
798 Parser_GetSyncSource
,
801 Parser_QueryFilterInfo
,
802 Parser_JoinFilterGraph
,
803 Parser_QueryVendorInfo
806 HRESULT
MPEGSplitter_create(IUnknown
* pUnkOuter
, LPVOID
* ppv
)
808 MPEGSplitterImpl
*This
;
811 TRACE("(%p, %p)\n", pUnkOuter
, ppv
);
816 return CLASS_E_NOAGGREGATION
;
818 This
= CoTaskMemAlloc(sizeof(MPEGSplitterImpl
));
820 return E_OUTOFMEMORY
;
822 ZeroMemory(This
, sizeof(MPEGSplitterImpl
));
823 This
->seektable
= CoTaskMemAlloc(sizeof(struct seek_entry
) * 64);
824 if (!This
->seektable
)
827 return E_OUTOFMEMORY
;
829 This
->seek_entries
= 64;
831 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
);
839 /* Note: This memory is managed by the parser filter once created */