quartz: Reset EcCompleteCount before starting filters.
[wine/wine64.git] / dlls / quartz / mpegsplit.c
blobfcd0a5c87e22085bb0f0feeafd7200a26e7bc7ff
1 /*
2 * MPEG Splitter Filter
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
23 #include <assert.h>
24 #include <math.h>
26 #include "quartz_private.h"
27 #include "control_private.h"
28 #include "pin.h"
30 #include "uuids.h"
31 #include "mmreg.h"
32 #include "mmsystem.h"
34 #include "winternl.h"
36 #include "wine/unicode.h"
37 #include "wine/debug.h"
39 #include "parser.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
57 ParserImpl Parser;
58 IMediaSample *pCurrentSample;
59 LONGLONG EndOfFile;
60 LONGLONG duration;
61 LONGLONG position;
62 DWORD skipbytes;
63 DWORD header_bytes;
64 DWORD remaining_bytes;
65 BOOL seek;
66 } MPEGSplitterImpl;
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;
90 /* Nothing yet.. */
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]);
121 return E_INVALIDARG;
124 mpeg1 = (header[1]>>4)&0x1;
125 if (mpeg1)
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]);
140 return E_INVALIDARG;
144 if (layer == 3 || layer == 2)
145 length = 144 * bitrate / freqs[freq_index] + padding;
146 else
147 length = 4 * (12 * bitrate / freqs[freq_index] + padding);
149 duration = (ULONGLONG)10000000 * (ULONGLONG)(length) / (ULONGLONG)(bitrate/8);
150 *plen = length;
151 *pduration += duration;
152 return S_OK;
156 static void skip_data(BYTE** from, DWORD *flen, DWORD amount)
158 *flen -= amount;
159 if (!*flen)
160 *from = NULL;
161 else
162 *from += amount;
165 static HRESULT copy_data(IMediaSample *to, BYTE** from, DWORD *flen, DWORD amount)
167 HRESULT hr = S_OK;
168 BYTE *ptr = NULL;
169 DWORD oldlength = IMediaSample_GetActualDataLength(to);
171 hr = IMediaSample_SetActualDataLength(to, oldlength + amount);
172 if (FAILED(hr))
174 if (!oldlength || oldlength <= 4)
175 WARN("Could not set require length\n");
176 return hr;
179 IMediaSample_GetPointer(to, &ptr);
180 memcpy(ptr + oldlength, *from, amount);
181 skip_data(from, flen, amount);
182 return hr;
185 static HRESULT FillBuffer(MPEGSplitterImpl *This, BYTE** fbuf, DWORD *flen, IMediaSample *pCurrentSample)
187 Parser_OutputPin * pOutputPin = (Parser_OutputPin*)This->Parser.ppPins[1];
188 LONGLONG length = 0;
189 HRESULT hr = S_OK;
190 DWORD dlen;
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 */
196 if (This->skipbytes)
198 DWORD skip = min(This->skipbytes, *flen);
199 skip_data(fbuf, flen, skip);
200 This->skipbytes -= skip;
201 return S_OK;
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);
210 if (FAILED(hr))
212 WARN("Could not resize sample: %08x\n", hr);
213 return hr;
216 This->remaining_bytes -= towrite;
217 if (This->remaining_bytes)
218 return hr;
220 /* Optimize: Try appending more samples to the stream */
221 goto out_append;
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)
228 BYTE *header = NULL;
229 DWORD attempts = 0;
231 /* Shoot anyone with a small sample! */
232 assert(*flen >= 6);
234 hr = IMediaSample_GetPointer(pCurrentSample, &header);
236 if (SUCCEEDED(hr))
237 hr = IMediaSample_SetActualDataLength(pCurrentSample, 7);
239 if (FAILED(hr))
241 WARN("Could not resize sample: %08x\n", hr);
242 return hr;
245 memcpy(header + dlen, *fbuf, 6 - dlen);
247 while (FAILED(parse_header(header+attempts, &length, &This->position)) && attempts < dlen)
249 attempts++;
252 /* No header found */
253 if (attempts == dlen)
255 hr = IMediaSample_SetActualDataLength(pCurrentSample, 0);
256 return hr;
259 IMediaSample_SetActualDataLength(pCurrentSample, 4);
260 IMediaSample_SetTime(pCurrentSample, &time, &This->position);
262 /* Move header back to beginning */
263 if (attempts)
264 memmove(header, header+attempts, 4);
266 This->remaining_bytes = length - 4;
267 *flen -= (4 - dlen + attempts);
268 *fbuf += (4 - dlen + attempts);
269 return hr;
272 /* Destination sample should contain no data! But the source sample should */
273 assert(!dlen);
274 assert(*flen);
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! */
283 if (*flen < 4)
285 assert(!length);
286 hr = copy_data(pCurrentSample, fbuf, flen, *flen);
287 return hr;
290 IMediaSample_SetTime(pCurrentSample, &time, &This->position);
292 if (*flen < length)
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);
297 return hr;
300 hr = copy_data(pCurrentSample, fbuf, flen, length);
301 if (FAILED(hr))
303 WARN("Couldn't set data size to %x%08x\n", (DWORD)(length >> 32), (DWORD)length);
304 This->skipbytes = length;
305 return hr;
308 out_append:
309 /* Optimize: Send multiple samples! */
310 while (*flen >= 4)
312 if (FAILED(parse_header(*fbuf, &length, &sampleduration)))
313 break;
315 if (length > *flen)
316 break;
318 if (FAILED(copy_data(pCurrentSample, fbuf, flen, length)))
319 break;
321 This->position += sampleduration;
322 sampleduration = 0;
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);
335 if (FAILED(hr))
337 WARN("Error sending sample (%x)\n", hr);
338 return hr;
340 if (This->pCurrentSample)
342 IMediaSample_Release(pCurrentSample);
343 This->pCurrentSample = NULL;
345 return hr;
349 static HRESULT MPEGSplitter_process_sample(LPVOID iface, IMediaSample * pSample)
351 MPEGSplitterImpl *This = (MPEGSplitterImpl*)iface;
352 BYTE *pbSrcStream;
353 DWORD cbSrcStream = 0;
354 REFERENCE_TIME tStart, tStop;
355 Parser_OutputPin * pOutputPin;
356 HRESULT hr;
358 pOutputPin = (Parser_OutputPin*)This->Parser.ppPins[1];
360 hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
361 if (SUCCEEDED(hr))
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);
379 break;
382 IMediaSample_SetTime(This->pCurrentSample, NULL, NULL);
383 if (FAILED(hr = IMediaSample_SetActualDataLength(This->pCurrentSample, 0)))
384 goto fail;
385 IMediaSample_SetSyncPoint(This->pCurrentSample, TRUE);
386 IMediaSample_SetDiscontinuity(This->pCurrentSample, This->seek);
387 This->seek = FALSE;
389 hr = FillBuffer(This, &pbSrcStream, &cbSrcStream, This->pCurrentSample);
390 if (SUCCEEDED(hr) && hr != S_FALSE)
391 continue;
393 fail:
394 if (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;
404 break;
407 if (BYTES_FROM_MEDIATIME(tStop) >= This->EndOfFile)
409 int i;
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++)
423 IPin* ppin;
424 HRESULT hr;
426 TRACE("Send End Of Stream to output pin %d\n", i);
428 hr = IPin_ConnectedTo(This->Parser.ppPins[i+1], &ppin);
429 if (SUCCEEDED(hr))
431 hr = IPin_EndOfStream(ppin);
432 IPin_Release(ppin);
434 if (FAILED(hr))
435 WARN("Error sending EndOfStream to pin %d (%x)\n", i, hr);
438 /* Force the pullpin thread to stop */
439 hr = S_FALSE;
442 LeaveCriticalSection(&This->Parser.csFilter);
443 return hr;
447 static HRESULT MPEGSplitter_query_accept(LPVOID iface, const AM_MEDIA_TYPE *pmt)
449 if (!IsEqualIID(&pmt->majortype, &MEDIATYPE_Stream))
450 return S_FALSE;
452 if (IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_MPEG1Audio))
453 return S_OK;
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");
462 return S_FALSE;
466 static HRESULT MPEGSplitter_init_audio(MPEGSplitterImpl *This, const BYTE *header, PIN_INFO *ppiOutput, AM_MEDIA_TYPE *pamt)
468 WAVEFORMATEX *format;
469 int bitrate_index;
470 int freq_index;
471 int mode_ext;
472 int emphasis;
473 int lsf = 1;
474 int mpeg1;
475 int layer;
476 int mode;
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;
492 if (mpeg1)
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);
502 if (!bitrate_index)
504 /* Set to highest bitrate so samples will fit in for sure */
505 FIXME("Variable-bitrate audio not fully supported.\n");
506 bitrate_index = 15;
509 pamt->cbFormat = ((layer==3)? sizeof(MPEGLAYER3WAVEFORMAT) :
510 sizeof(MPEG1WAVEFORMAT));
511 pamt->pbFormat = CoTaskMemAlloc(pamt->cbFormat);
512 if (!pamt->pbFormat)
513 return E_OUTOFMEMORY;
514 ZeroMemory(pamt->pbFormat, pamt->cbFormat);
515 format = (WAVEFORMATEX*)pamt->pbFormat;
517 format->wFormatTag = ((layer == 3) ? WAVE_FORMAT_MPEGLAYER3 :
518 WAVE_FORMAT_MPEG);
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;
523 if (layer == 3)
524 format->nBlockAlign = format->nAvgBytesPerSec * 8 * 144 /
525 (format->nSamplesPerSec<<lsf) + 1;
526 else if (layer == 2)
527 format->nBlockAlign = format->nAvgBytesPerSec * 8 * 144 /
528 format->nSamplesPerSec + 1;
529 else
530 format->nBlockAlign = 4 * (format->nAvgBytesPerSec * 8 * 12 / format->nSamplesPerSec + 1);
532 format->wBitsPerSample = 0;
534 if (layer == 3)
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;
552 else
554 MPEG1WAVEFORMAT *mpgformat = (MPEG1WAVEFORMAT*)format;
556 format->cbSize = 22;
558 mpgformat->fwHeadLayer = ((layer == 1) ? ACM_MPEG_LAYER1 :
559 ((layer == 2) ? ACM_MPEG_LAYER2 :
560 ACM_MPEG_LAYER3));
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 :
565 ACM_MPEG_STEREO)));
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"
573 "\tLayer %d (%#x)\n"
574 "\tFrequency: %d\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);
582 return S_OK;
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;
591 HRESULT hr;
592 LONGLONG pos = 0; /* in bytes */
593 BYTE header[10];
594 int streamtype = 0;
595 LONGLONG total, avail;
596 AM_MEDIA_TYPE amt;
597 PIN_INFO piOutput;
599 IAsyncReader_Length(pPin->pReader, &total, &avail);
600 This->EndOfFile = total;
602 hr = IAsyncReader_SyncRead(pPin->pReader, pos, 4, header);
603 if (SUCCEEDED(hr))
604 pos += 4;
606 /* Skip ID3 v2 tag, if any */
607 if (SUCCEEDED(hr) && !strncmp("ID3", (char*)header, 3))
608 do {
609 UINT length;
610 hr = IAsyncReader_SyncRead(pPin->pReader, pos, 6, header + 4);
611 if (FAILED(hr))
612 break;
613 pos += 6;
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);
620 pos += length;
622 /* Read the real header for the mpeg splitter */
623 hr = IAsyncReader_SyncRead(pPin->pReader, pos, 4, header);
624 if (SUCCEEDED(hr))
625 pos += 4;
626 TRACE("%x:%x:%x:%x\n", header[0], header[1], header[2], header[3]);
627 } while (0);
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);
636 if (FAILED(hr))
637 return hr;
638 pos -= 4;
639 This->header_bytes = This->skipbytes = pos;
641 switch(streamtype)
643 case MPEG_AUDIO_HEADER:
645 LONGLONG duration = 0;
646 DWORD ticks = GetTickCount();
648 hr = MPEGSplitter_init_audio(This, header, &piOutput, &amt);
649 if (SUCCEEDED(hr))
651 WAVEFORMATEX *format = (WAVEFORMATEX*)amt.pbFormat;
653 props.cbAlign = 1;
654 props.cbPrefix = 0;
655 /* Make the output buffer a multiple of the frame size */
656 props.cbBuffer = 0x4000 / format->nBlockAlign *
657 format->nBlockAlign;
658 props.cBuffers = 1;
659 hr = Parser_AddPin(&(This->Parser), &piOutput, &props, &amt);
662 if (FAILED(hr))
664 if (amt.pbFormat)
665 CoTaskMemFree(amt.pbFormat);
666 ERR("Could not create pin for MPEG audio stream (%x)\n", hr);
667 break;
670 /* Check for idv1 tag, and remove it from stream if found */
671 hr = IAsyncReader_SyncRead(pPin->pReader, This->EndOfFile-128, 3, header+4);
672 if (FAILED(hr))
673 break;
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)
680 LONGLONG length = 0;
681 hr = IAsyncReader_SyncRead(pPin->pReader, pos, 4, header);
682 if (hr != S_OK)
683 break;
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)
690 break;
692 pos += length;
693 TRACE("Pos: %x%08x/%x%08x\n", (DWORD)(pos >> 32), (DWORD)pos, (DWORD)(This->EndOfFile>>32), (DWORD)This->EndOfFile);
695 hr = S_OK;
696 TRACE("Duration: %d seconds\n", (DWORD)(duration / 10000000));
697 TRACE("Parsing took %u ms\n", GetTickCount() - ticks);
698 This->duration = duration;
699 break;
701 case MPEG_VIDEO_HEADER:
702 FIXME("MPEG video processing not yet supported!\n");
703 hr = E_FAIL;
704 break;
705 case MPEG_SYSTEM_HEADER:
706 FIXME("MPEG system streams not yet supported!\n");
707 hr = E_FAIL;
708 break;
710 default:
711 break;
713 This->remaining_bytes = 0;
714 This->position = 0;
716 return hr;
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);
734 if (!This->seek)
735 This->skipbytes = This->remaining_bytes = 0;
737 return S_OK;
740 static HRESULT MPEGSplitter_seek(IBaseFilter *iface)
742 MPEGSplitterImpl *This = (MPEGSplitterImpl*)iface;
743 PullPin *pPin = This->Parser.pInputPin;
744 LONGLONG newpos, timepos, bytepos;
745 HRESULT hr = S_OK;
746 BYTE header[4];
748 /* Position, in bytes */
749 bytepos = This->header_bytes;
751 /* Position, in media time, current and new */
752 timepos = 0;
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);
758 return E_INVALIDARG;
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);
764 return S_OK;
767 hr = IAsyncReader_SyncRead(pPin->pReader, bytepos, 4, header);
769 while (bytepos < This->EndOfFile && SUCCEEDED(hr))
771 LONGLONG length = 0;
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);
778 if (FAILED(hr))
779 break;
781 bytepos += length;
782 TRACE("Pos: %x%08x/%x%08x\n", (DWORD)(bytepos >> 32), (DWORD)bytepos, (DWORD)(This->EndOfFile>>32), (DWORD)This->EndOfFile);
783 if (timepos >= newpos)
784 break;
786 if (SUCCEEDED(hr))
788 FILTER_STATE state;
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);
801 This->seek = TRUE;
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);
816 return hr;
819 HRESULT MPEGSplitter_create(IUnknown * pUnkOuter, LPVOID * ppv)
821 MPEGSplitterImpl *This;
822 HRESULT hr = E_FAIL;
824 TRACE("(%p, %p)\n", pUnkOuter, ppv);
826 *ppv = NULL;
828 if (pUnkOuter)
829 return CLASS_E_NOAGGREGATION;
831 This = CoTaskMemAlloc(sizeof(MPEGSplitterImpl));
832 if (!This)
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);
837 if (FAILED(hr))
839 CoTaskMemFree(This);
840 return hr;
842 This->seek = TRUE;
844 /* Note: This memory is managed by the parser filter once created */
845 *ppv = (LPVOID)This;
847 return hr;