4 * Copyright 2003 Robert Shearman
5 * Copyright 2004-2005 Christian Costa
6 * Copyright 2007 Chris Robinson
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
26 #include "quartz_private.h"
27 #include "control_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
58 IMediaSample
*pCurrentSample
;
64 DWORD remaining_bytes
;
68 static int MPEGSplitter_head_check(const BYTE
*header
)
70 /* If this is a possible start code, check for a system or video header */
71 if (header
[0] == 0 && header
[1] == 0 && header
[2] == 1)
73 /* Check if we got a system or elementary stream start code */
74 if (header
[3] == PACK_START_CODE
||
75 header
[3] == VIDEO_ELEMENTARY_STREAM
||
76 header
[3] == AUDIO_ELEMENTARY_STREAM
)
77 return MPEG_SYSTEM_HEADER
;
79 /* Check for a MPEG video sequence start code */
80 if (header
[3] == SEQUENCE_HEADER_CODE
)
81 return MPEG_VIDEO_HEADER
;
84 /* This should give a good guess if we have an MPEG audio header */
85 if(header
[0] == 0xff && ((header
[1]>>5)&0x7) == 0x7 &&
86 ((header
[1]>>1)&0x3) != 0 && ((header
[2]>>4)&0xf) != 0xf &&
87 ((header
[2]>>2)&0x3) != 0x3)
88 return MPEG_AUDIO_HEADER
;
91 return MPEG_NO_HEADER
;
94 static const WCHAR wszAudioStream
[] = {'A','u','d','i','o',0};
95 static const WCHAR wszVideoStream
[] = {'V','i','d','e','o',0};
97 static const DWORD freqs
[10] = { 44100, 48000, 32000, 22050, 24000, 16000, 11025, 12000, 8000, 0 };
99 static const DWORD tabsel_123
[2][3][16] = {
100 { {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,},
101 {0,32,48,56, 64, 80, 96,112,128,160,192,224,256,320,384,},
102 {0,32,40,48, 56, 64, 80, 96,112,128,160,192,224,256,320,} },
104 { {0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,},
105 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,},
106 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,} }
110 static HRESULT
parse_header(BYTE
*header
, LONGLONG
*plen
, LONGLONG
*pduration
)
112 LONGLONG duration
= *pduration
;
114 int bitrate_index
, freq_index
, mode_ext
, emphasis
, lsf
= 1, mpeg1
, layer
, mode
, padding
, bitrate
, length
;
116 if (!(header
[0] == 0xff && ((header
[1]>>5)&0x7) == 0x7 &&
117 ((header
[1]>>1)&0x3) != 0 && ((header
[2]>>4)&0xf) != 0xf &&
118 ((header
[2]>>2)&0x3) != 0x3))
120 FIXME("Not a valid header: %02x:%02x\n", header
[0], header
[1]);
124 mpeg1
= (header
[1]>>4)&0x1;
126 lsf
= ((header
[1]>>3)&0x1)^1;
128 layer
= 4-((header
[1]>>1)&0x3);
129 bitrate_index
= ((header
[2]>>4)&0xf);
130 freq_index
= ((header
[2]>>2)&0x3) + (mpeg1
?(lsf
*3):6);
131 padding
= ((header
[2]>>1)&0x1);
132 mode
= ((header
[3]>>6)&0x3);
133 mode_ext
= ((header
[3]>>4)&0x3);
134 emphasis
= ((header
[3]>>0)&0x3);
136 bitrate
= tabsel_123
[lsf
][layer
-1][bitrate_index
] * 1000;
137 if (!bitrate
|| layer
!= 3)
139 ERR("Not a valid header: %02x:%02x:%02x:%02x\n", header
[0], header
[1], header
[2], header
[3]);
144 if (layer
== 3 || layer
== 2)
145 length
= 144 * bitrate
/ freqs
[freq_index
] + padding
;
147 length
= 4 * (12 * bitrate
/ freqs
[freq_index
] + padding
);
149 duration
= (ULONGLONG
)10000000 * (ULONGLONG
)(length
) / (ULONGLONG
)(bitrate
/8);
151 *pduration
+= duration
;
156 static void skip_data(BYTE
** from
, DWORD
*flen
, DWORD amount
)
165 static HRESULT
copy_data(IMediaSample
*to
, BYTE
** from
, DWORD
*flen
, DWORD amount
)
169 DWORD oldlength
= IMediaSample_GetActualDataLength(to
);
171 hr
= IMediaSample_SetActualDataLength(to
, oldlength
+ amount
);
174 if (!oldlength
|| oldlength
<= 4)
175 WARN("Could not set require length\n");
179 IMediaSample_GetPointer(to
, &ptr
);
180 memcpy(ptr
+ oldlength
, *from
, amount
);
181 skip_data(from
, flen
, amount
);
185 static HRESULT
FillBuffer(MPEGSplitterImpl
*This
, BYTE
** fbuf
, DWORD
*flen
, IMediaSample
*pCurrentSample
)
187 Parser_OutputPin
* pOutputPin
= (Parser_OutputPin
*)This
->Parser
.ppPins
[1];
191 LONGLONG time
= This
->position
, sampleduration
= 0;
193 TRACE("Source length: %u, skip length: %u, remaining: %u\n", *flen
, This
->skipbytes
, This
->remaining_bytes
);
195 /* Case where bytes are skipped */
198 DWORD skip
= min(This
->skipbytes
, *flen
);
199 skip_data(fbuf
, flen
, skip
);
200 This
->skipbytes
-= skip
;
204 /* Case where there is already an output sample being held */
205 if (This
->remaining_bytes
)
207 DWORD towrite
= min(This
->remaining_bytes
, *flen
);
209 hr
= copy_data(pCurrentSample
, fbuf
, flen
, towrite
);
212 WARN("Could not resize sample: %08x\n", hr
);
216 This
->remaining_bytes
-= towrite
;
217 if (This
->remaining_bytes
)
220 /* Optimize: Try appending more samples to the stream */
224 /* Special case, last source sample might (or might not have) had a header, and now we want to retrieve it */
225 dlen
= IMediaSample_GetActualDataLength(pCurrentSample
);
226 if (dlen
> 0 && dlen
< 4)
231 /* Shoot anyone with a small sample! */
234 hr
= IMediaSample_GetPointer(pCurrentSample
, &header
);
237 hr
= IMediaSample_SetActualDataLength(pCurrentSample
, 7);
241 WARN("Could not resize sample: %08x\n", hr
);
245 memcpy(header
+ dlen
, *fbuf
, 6 - dlen
);
247 while (FAILED(parse_header(header
+attempts
, &length
, &This
->position
)) && attempts
< dlen
)
252 /* No header found */
253 if (attempts
== dlen
)
255 hr
= IMediaSample_SetActualDataLength(pCurrentSample
, 0);
259 IMediaSample_SetActualDataLength(pCurrentSample
, 4);
260 IMediaSample_SetTime(pCurrentSample
, &time
, &This
->position
);
262 /* Move header back to beginning */
264 memmove(header
, header
+attempts
, 4);
266 This
->remaining_bytes
= length
- 4;
267 *flen
-= (4 - dlen
+ attempts
);
268 *fbuf
+= (4 - dlen
+ attempts
);
272 /* Destination sample should contain no data! But the source sample should */
276 /* Find the next valid header.. it <SHOULD> be right here */
277 while (*flen
> 3 && FAILED(parse_header(*fbuf
, &length
, &This
->position
)))
279 skip_data(fbuf
, flen
, 1);
282 /* Uh oh, no header found! */
286 hr
= copy_data(pCurrentSample
, fbuf
, flen
, *flen
);
290 IMediaSample_SetTime(pCurrentSample
, &time
, &This
->position
);
294 /* Partial copy: Copy 4 bytes, the rest will be copied by the logic for This->remaining_bytes */
295 This
->remaining_bytes
= length
- 4;
296 copy_data(pCurrentSample
, fbuf
, flen
, 4);
300 hr
= copy_data(pCurrentSample
, fbuf
, flen
, length
);
303 WARN("Couldn't set data size to %x%08x\n", (DWORD
)(length
>> 32), (DWORD
)length
);
304 This
->skipbytes
= length
;
309 /* Optimize: Send multiple samples! */
312 if (FAILED(parse_header(*fbuf
, &length
, &sampleduration
)))
318 if (FAILED(copy_data(pCurrentSample
, fbuf
, flen
, length
)))
321 This
->position
+= sampleduration
;
323 IMediaSample_SetTime(pCurrentSample
, &time
, &This
->position
);
325 TRACE("Media time: %u.%03u\n", (DWORD
)(This
->position
/10000000), (DWORD
)((This
->position
/10000)%1000));
327 IMediaSample_AddRef(pCurrentSample
);
328 LeaveCriticalSection(&This
->Parser
.csFilter
);
330 hr
= OutputPin_SendSample(&pOutputPin
->pin
, pCurrentSample
);
332 EnterCriticalSection(&This
->Parser
.csFilter
);
333 IMediaSample_Release(pCurrentSample
);
337 WARN("Error sending sample (%x)\n", hr
);
340 if (This
->pCurrentSample
)
342 IMediaSample_Release(pCurrentSample
);
343 This
->pCurrentSample
= NULL
;
349 static HRESULT
MPEGSplitter_process_sample(LPVOID iface
, IMediaSample
* pSample
)
351 MPEGSplitterImpl
*This
= (MPEGSplitterImpl
*)iface
;
353 DWORD cbSrcStream
= 0;
354 REFERENCE_TIME tStart
, tStop
;
355 Parser_OutputPin
* pOutputPin
;
358 pOutputPin
= (Parser_OutputPin
*)This
->Parser
.ppPins
[1];
360 hr
= IMediaSample_GetTime(pSample
, &tStart
, &tStop
);
363 cbSrcStream
= IMediaSample_GetActualDataLength(pSample
);
364 hr
= IMediaSample_GetPointer(pSample
, &pbSrcStream
);
367 /* trace removed for performance reasons */
368 /* TRACE("(%p), %llu -> %llu\n", pSample, tStart, tStop); */
370 EnterCriticalSection(&This
->Parser
.csFilter
);
371 /* Now, try to find a new header */
372 while (cbSrcStream
> 0)
374 if (!This
->pCurrentSample
)
376 if (FAILED(hr
= OutputPin_GetDeliveryBuffer(&pOutputPin
->pin
, &This
->pCurrentSample
, NULL
, NULL
, 0)))
378 FIXME("Failed with hres: %08x!\n", hr
);
382 IMediaSample_SetTime(This
->pCurrentSample
, NULL
, NULL
);
383 if (FAILED(hr
= IMediaSample_SetActualDataLength(This
->pCurrentSample
, 0)))
385 IMediaSample_SetSyncPoint(This
->pCurrentSample
, TRUE
);
386 IMediaSample_SetDiscontinuity(This
->pCurrentSample
, This
->seek
);
389 hr
= FillBuffer(This
, &pbSrcStream
, &cbSrcStream
, This
->pCurrentSample
);
390 if (SUCCEEDED(hr
) && hr
!= S_FALSE
)
395 FIXME("Failed with hres: %08x!\n", hr
);
396 This
->skipbytes
+= This
->remaining_bytes
;
397 This
->remaining_bytes
= 0;
398 if (This
->pCurrentSample
)
400 IMediaSample_SetActualDataLength(This
->pCurrentSample
, 0);
401 IMediaSample_Release(This
->pCurrentSample
);
402 This
->pCurrentSample
= NULL
;
407 if (BYTES_FROM_MEDIATIME(tStop
) >= This
->EndOfFile
)
411 TRACE("End of file reached\n");
413 if (This
->pCurrentSample
)
415 /* Drop last data, it's likely to be garbage anyway */
416 IMediaSample_SetActualDataLength(This
->pCurrentSample
, 0);
417 IMediaSample_Release(This
->pCurrentSample
);
418 This
->pCurrentSample
= NULL
;
421 for (i
= 0; i
< This
->Parser
.cStreams
; i
++)
426 TRACE("Send End Of Stream to output pin %d\n", i
);
428 hr
= IPin_ConnectedTo(This
->Parser
.ppPins
[i
+1], &ppin
);
431 hr
= IPin_EndOfStream(ppin
);
435 WARN("Error sending EndOfStream to pin %d (%x)\n", i
, hr
);
438 /* Force the pullpin thread to stop */
442 LeaveCriticalSection(&This
->Parser
.csFilter
);
447 static HRESULT
MPEGSplitter_query_accept(LPVOID iface
, const AM_MEDIA_TYPE
*pmt
)
449 if (!IsEqualIID(&pmt
->majortype
, &MEDIATYPE_Stream
))
452 if (IsEqualIID(&pmt
->subtype
, &MEDIASUBTYPE_MPEG1Audio
))
455 if (IsEqualIID(&pmt
->subtype
, &MEDIASUBTYPE_MPEG1Video
))
456 FIXME("MPEG-1 video streams not yet supported.\n");
457 else if (IsEqualIID(&pmt
->subtype
, &MEDIASUBTYPE_MPEG1System
))
458 FIXME("MPEG-1 system streams not yet supported.\n");
459 else if (IsEqualIID(&pmt
->subtype
, &MEDIASUBTYPE_MPEG1VideoCD
))
460 FIXME("MPEG-1 VideoCD streams not yet supported.\n");
466 static HRESULT
MPEGSplitter_init_audio(MPEGSplitterImpl
*This
, const BYTE
*header
, PIN_INFO
*ppiOutput
, AM_MEDIA_TYPE
*pamt
)
468 WAVEFORMATEX
*format
;
478 ZeroMemory(pamt
, sizeof(*pamt
));
479 ppiOutput
->dir
= PINDIR_OUTPUT
;
480 ppiOutput
->pFilter
= (IBaseFilter
*)This
;
481 wsprintfW(ppiOutput
->achName
, wszAudioStream
);
483 pamt
->formattype
= FORMAT_WaveFormatEx
;
484 pamt
->majortype
= MEDIATYPE_Audio
;
485 pamt
->subtype
= MEDIASUBTYPE_MPEG1AudioPayload
;
487 pamt
->lSampleSize
= 0;
488 pamt
->bFixedSizeSamples
= FALSE
;
489 pamt
->bTemporalCompression
= 0;
491 mpeg1
= (header
[1]>>4)&0x1;
493 lsf
= ((header
[1]>>3)&0x1)^1;
495 layer
= 4-((header
[1]>>1)&0x3);
496 bitrate_index
= ((header
[2]>>4)&0xf);
497 freq_index
= ((header
[2]>>2)&0x3) + (mpeg1
?(lsf
*3):6);
498 mode
= ((header
[3]>>6)&0x3);
499 mode_ext
= ((header
[3]>>4)&0x3);
500 emphasis
= ((header
[3]>>0)&0x3);
504 /* Set to highest bitrate so samples will fit in for sure */
505 FIXME("Variable-bitrate audio not fully supported.\n");
509 pamt
->cbFormat
= ((layer
==3)? sizeof(MPEGLAYER3WAVEFORMAT
) :
510 sizeof(MPEG1WAVEFORMAT
));
511 pamt
->pbFormat
= CoTaskMemAlloc(pamt
->cbFormat
);
513 return E_OUTOFMEMORY
;
514 ZeroMemory(pamt
->pbFormat
, pamt
->cbFormat
);
515 format
= (WAVEFORMATEX
*)pamt
->pbFormat
;
517 format
->wFormatTag
= ((layer
== 3) ? WAVE_FORMAT_MPEGLAYER3
:
519 format
->nChannels
= ((mode
== 3) ? 1 : 2);
520 format
->nSamplesPerSec
= freqs
[freq_index
];
521 format
->nAvgBytesPerSec
= tabsel_123
[lsf
][layer
-1][bitrate_index
] * 1000 / 8;
524 format
->nBlockAlign
= format
->nAvgBytesPerSec
* 8 * 144 /
525 (format
->nSamplesPerSec
<<lsf
) + 1;
527 format
->nBlockAlign
= format
->nAvgBytesPerSec
* 8 * 144 /
528 format
->nSamplesPerSec
+ 1;
530 format
->nBlockAlign
= 4 * (format
->nAvgBytesPerSec
* 8 * 12 / format
->nSamplesPerSec
+ 1);
532 format
->wBitsPerSample
= 0;
536 MPEGLAYER3WAVEFORMAT
*mp3format
= (MPEGLAYER3WAVEFORMAT
*)format
;
538 format
->cbSize
= MPEGLAYER3_WFX_EXTRA_BYTES
;
540 mp3format
->wID
= MPEGLAYER3_ID_MPEG
;
541 mp3format
->fdwFlags
= MPEGLAYER3_FLAG_PADDING_ON
;
542 mp3format
->nBlockSize
= format
->nBlockAlign
;
543 mp3format
->nFramesPerBlock
= 1;
545 /* Beware the evil magic numbers. This struct is apparently horribly
546 * under-documented, and the only references I could find had it being
547 * set to this with no real explanation. It works fine though, so I'm
548 * not complaining (yet).
550 mp3format
->nCodecDelay
= 1393;
554 MPEG1WAVEFORMAT
*mpgformat
= (MPEG1WAVEFORMAT
*)format
;
558 mpgformat
->fwHeadLayer
= ((layer
== 1) ? ACM_MPEG_LAYER1
:
559 ((layer
== 2) ? ACM_MPEG_LAYER2
:
561 mpgformat
->dwHeadBitrate
= format
->nAvgBytesPerSec
* 8;
562 mpgformat
->fwHeadMode
= ((mode
== 3) ? ACM_MPEG_SINGLECHANNEL
:
563 ((mode
== 2) ? ACM_MPEG_DUALCHANNEL
:
564 ((mode
== 1) ? ACM_MPEG_JOINTSTEREO
:
566 mpgformat
->fwHeadModeExt
= ((mode
== 1) ? 0x0F : (1<<mode_ext
));
567 mpgformat
->wHeadEmphasis
= emphasis
+ 1;
568 mpgformat
->fwHeadFlags
= ACM_MPEG_ID_MPEG1
;
570 pamt
->subtype
.Data1
= format
->wFormatTag
;
572 TRACE("MPEG audio stream detected:\n"
575 "\tChannels: %d (%d)\n"
576 "\tBytesPerSec: %d\n",
577 layer
, format
->wFormatTag
, format
->nSamplesPerSec
,
578 format
->nChannels
, mode
, format
->nAvgBytesPerSec
);
580 dump_AM_MEDIA_TYPE(pamt
);
586 static HRESULT
MPEGSplitter_pre_connect(IPin
*iface
, IPin
*pConnectPin
)
588 PullPin
*pPin
= (PullPin
*)iface
;
589 MPEGSplitterImpl
*This
= (MPEGSplitterImpl
*)pPin
->pin
.pinInfo
.pFilter
;
590 ALLOCATOR_PROPERTIES props
;
592 LONGLONG pos
= 0; /* in bytes */
595 LONGLONG total
, avail
;
599 IAsyncReader_Length(pPin
->pReader
, &total
, &avail
);
600 This
->EndOfFile
= total
;
602 hr
= IAsyncReader_SyncRead(pPin
->pReader
, pos
, 4, header
);
606 /* Skip ID3 v2 tag, if any */
607 if (SUCCEEDED(hr
) && !strncmp("ID3", (char*)header
, 3))
610 hr
= IAsyncReader_SyncRead(pPin
->pReader
, pos
, 6, header
+ 4);
614 TRACE("Found ID3 v2.%d.%d\n", header
[3], header
[4]);
615 length
= (header
[6] & 0x7F) << 21;
616 length
+= (header
[7] & 0x7F) << 14;
617 length
+= (header
[8] & 0x7F) << 7;
618 length
+= (header
[9] & 0x7F);
619 TRACE("Length: %u\n", length
);
622 /* Read the real header for the mpeg splitter */
623 hr
= IAsyncReader_SyncRead(pPin
->pReader
, pos
, 4, header
);
626 TRACE("%x:%x:%x:%x\n", header
[0], header
[1], header
[2], header
[3]);
629 while(SUCCEEDED(hr
) && !(streamtype
=MPEGSplitter_head_check(header
)))
631 TRACE("%x:%x:%x:%x\n", header
[0], header
[1], header
[2], header
[3]);
632 /* No valid header yet; shift by a byte and check again */
633 memmove(header
, header
+1, 3);
634 hr
= IAsyncReader_SyncRead(pPin
->pReader
, pos
++, 1, header
+ 3);
639 This
->header_bytes
= This
->skipbytes
= pos
;
643 case MPEG_AUDIO_HEADER
:
645 LONGLONG duration
= 0;
646 DWORD ticks
= GetTickCount();
648 hr
= MPEGSplitter_init_audio(This
, header
, &piOutput
, &amt
);
651 WAVEFORMATEX
*format
= (WAVEFORMATEX
*)amt
.pbFormat
;
655 /* Make the output buffer a multiple of the frame size */
656 props
.cbBuffer
= 0x4000 / format
->nBlockAlign
*
659 hr
= Parser_AddPin(&(This
->Parser
), &piOutput
, &props
, &amt
);
665 CoTaskMemFree(amt
.pbFormat
);
666 ERR("Could not create pin for MPEG audio stream (%x)\n", hr
);
670 /* Check for idv1 tag, and remove it from stream if found */
671 hr
= IAsyncReader_SyncRead(pPin
->pReader
, This
->EndOfFile
-128, 3, header
+4);
674 if (!strncmp((char*)header
+4, "TAG", 3))
675 This
->EndOfFile
-= 128;
677 /* http://mpgedit.org/mpgedit/mpeg_format/mpeghdr.htm has a whole readup on audio headers */
678 while (pos
+ 3 < This
->EndOfFile
)
681 hr
= IAsyncReader_SyncRead(pPin
->pReader
, pos
, 4, header
);
684 while (parse_header(header
, &length
, &duration
))
686 /* No valid header yet; shift by a byte and check again */
687 memmove(header
, header
+1, 3);
688 hr
= IAsyncReader_SyncRead(pPin
->pReader
, pos
++, 1, header
+ 3);
689 if (hr
!= S_OK
|| This
->EndOfFile
- pos
< 4)
693 TRACE("Pos: %x%08x/%x%08x\n", (DWORD
)(pos
>> 32), (DWORD
)pos
, (DWORD
)(This
->EndOfFile
>>32), (DWORD
)This
->EndOfFile
);
696 TRACE("Duration: %d seconds\n", (DWORD
)(duration
/ 10000000));
697 TRACE("Parsing took %u ms\n", GetTickCount() - ticks
);
698 This
->duration
= duration
;
701 case MPEG_VIDEO_HEADER
:
702 FIXME("MPEG video processing not yet supported!\n");
705 case MPEG_SYSTEM_HEADER
:
706 FIXME("MPEG system streams not yet supported!\n");
713 This
->remaining_bytes
= 0;
719 static HRESULT
MPEGSplitter_cleanup(LPVOID iface
)
721 MPEGSplitterImpl
*This
= (MPEGSplitterImpl
*)iface
;
723 TRACE("(%p)->()\n", This
);
725 if (This
->pCurrentSample
)
726 IMediaSample_Release(This
->pCurrentSample
);
727 This
->pCurrentSample
= NULL
;
729 if (This
->Parser
.pInputPin
&& !This
->seek
)
731 This
->skipbytes
+= This
->remaining_bytes
;
732 This
->Parser
.pInputPin
->rtCurrent
+= MEDIATIME_FROM_BYTES(This
->skipbytes
);
735 This
->skipbytes
= This
->remaining_bytes
= 0;
740 static HRESULT
MPEGSplitter_seek(IBaseFilter
*iface
)
742 MPEGSplitterImpl
*This
= (MPEGSplitterImpl
*)iface
;
743 PullPin
*pPin
= This
->Parser
.pInputPin
;
744 LONGLONG newpos
, timepos
, bytepos
;
748 /* Position, in bytes */
749 bytepos
= This
->header_bytes
;
751 /* Position, in media time, current and new */
753 newpos
= This
->Parser
.mediaSeeking
.llCurrent
;
755 if (newpos
> This
->duration
)
757 FIXME("Requesting position %x%08x beyond end of stream %x%08x\n", (DWORD
)(newpos
>>32), (DWORD
)newpos
, (DWORD
)(This
->duration
>>32), (DWORD
)This
->duration
);
761 if (This
->position
/1000000 == newpos
/1000000)
763 FIXME("Requesting position %x%08x same as current position %x%08x\n", (DWORD
)(newpos
>>32), (DWORD
)newpos
, (DWORD
)(This
->position
>>32), (DWORD
)This
->position
);
767 hr
= IAsyncReader_SyncRead(pPin
->pReader
, bytepos
, 4, header
);
769 while (bytepos
< This
->EndOfFile
&& SUCCEEDED(hr
))
772 hr
= IAsyncReader_SyncRead(pPin
->pReader
, bytepos
, 4, header
);
773 while (parse_header(header
, &length
, &timepos
))
775 /* No valid header yet; shift by a byte and check again */
776 memmove(header
, header
+1, 3);
777 hr
= IAsyncReader_SyncRead(pPin
->pReader
, ++bytepos
, 1, header
+ 3);
782 TRACE("Pos: %x%08x/%x%08x\n", (DWORD
)(bytepos
>> 32), (DWORD
)bytepos
, (DWORD
)(This
->EndOfFile
>>32), (DWORD
)This
->EndOfFile
);
783 if (timepos
>= newpos
)
789 PullPin
*pin
= This
->Parser
.pInputPin
;
791 TRACE("Moving sound to %x%08x bytes!\n", (DWORD
)(bytepos
>>32), (DWORD
)bytepos
);
793 IPin_BeginFlush((IPin
*)pin
);
794 IPin_NewSegment((IPin
*)pin
, newpos
, This
->duration
, pin
->dRate
);
795 IPin_EndFlush((IPin
*)pin
);
797 /* Make sure this is done while stopped */
798 EnterCriticalSection(&This
->Parser
.csFilter
);
799 pin
->rtStart
= pin
->rtCurrent
= MEDIATIME_FROM_BYTES(bytepos
);
800 pin
->rtStop
= MEDIATIME_FROM_BYTES((REFERENCE_TIME
)This
->EndOfFile
);
802 This
->skipbytes
= This
->remaining_bytes
= 0;
803 This
->position
= newpos
;
804 if (This
->pCurrentSample
)
806 IMediaSample_Release(This
->pCurrentSample
);
807 This
->pCurrentSample
= NULL
;
809 state
= This
->Parser
.state
;
810 LeaveCriticalSection(&This
->Parser
.csFilter
);
812 if (state
== State_Running
&& pin
->state
== State_Paused
)
813 PullPin_StartProcessing(pin
);
819 HRESULT
MPEGSplitter_create(IUnknown
* pUnkOuter
, LPVOID
* ppv
)
821 MPEGSplitterImpl
*This
;
824 TRACE("(%p, %p)\n", pUnkOuter
, ppv
);
829 return CLASS_E_NOAGGREGATION
;
831 This
= CoTaskMemAlloc(sizeof(MPEGSplitterImpl
));
833 return E_OUTOFMEMORY
;
835 ZeroMemory(This
, sizeof(MPEGSplitterImpl
));
836 hr
= Parser_Create(&(This
->Parser
), &CLSID_MPEG1Splitter
, MPEGSplitter_process_sample
, MPEGSplitter_query_accept
, MPEGSplitter_pre_connect
, MPEGSplitter_cleanup
, NULL
, MPEGSplitter_seek
, NULL
);
844 /* Note: This memory is managed by the parser filter once created */