secur32: On Mac, recognize key exchange algorithm for cipher suites using ECDH.
[wine/multimedia.git] / dlls / quartz / mpegsplit.c
blobde03d17f4e3bad36d541f49738d138f1871e4a4a
1 /*
2 * MPEG Splitter Filter
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
24 #include <assert.h>
25 #include <math.h>
27 #include "quartz_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 LONGLONG EndOfFile;
59 LONGLONG position;
60 DWORD begin_offset;
61 BYTE header[4];
63 /* Whether we just seeked (or started playing) */
64 BOOL seek;
65 } MPEGSplitterImpl;
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;
94 /* Nothing yet.. */
95 return MPEG_NO_HEADER;
98 static const WCHAR wszAudioStream[] = {'A','u','d','i','o',0};
99 static const WCHAR wszVideoStream[] = {'V','i','d','e','o',0};
101 static const DWORD freqs[10] = { 44100, 48000, 32000, 22050, 24000, 16000, 11025, 12000, 8000, 0 };
103 static const DWORD tabsel_123[2][3][16] = {
104 { {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,},
105 {0,32,48,56, 64, 80, 96,112,128,160,192,224,256,320,384,},
106 {0,32,40,48, 56, 64, 80, 96,112,128,160,192,224,256,320,} },
108 { {0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,},
109 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,},
110 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,} }
113 static HRESULT parse_header(BYTE *header, LONGLONG *plen, LONGLONG *pduration)
115 int bitrate_index, freq_index, lsf = 1, mpeg1, layer, padding, bitrate, length;
116 LONGLONG duration;
118 if (!(header[0] == 0xff && ((header[1]>>5)&0x7) == 0x7 &&
119 ((header[1]>>1)&0x3) != 0 && ((header[2]>>4)&0xf) != 0xf &&
120 ((header[2]>>2)&0x3) != 0x3))
122 FIXME("Not a valid header: %02x:%02x:%02x:%02x\n", header[0], header[1], header[2], header[3]);
123 return E_INVALIDARG;
126 mpeg1 = (header[1]>>4)&0x1;
127 if (mpeg1)
128 lsf = ((header[1]>>3)&0x1)^1;
130 layer = 4-((header[1]>>1)&0x3);
131 bitrate_index = ((header[2]>>4)&0xf);
132 freq_index = ((header[2]>>2)&0x3) + (mpeg1?(lsf*3):6);
133 padding = ((header[2]>>1)&0x1);
135 bitrate = tabsel_123[lsf][layer-1][bitrate_index] * 1000;
136 if (!bitrate)
138 FIXME("Not a valid header: %02x:%02x:%02x:%02x\n", header[0], header[1], header[2], header[3]);
139 return E_INVALIDARG;
142 if (layer == 1)
143 length = 4 * (12 * bitrate / freqs[freq_index] + padding);
144 else if (layer == 2)
145 length = 144 * bitrate / freqs[freq_index] + padding;
146 else if (layer == 3)
147 length = 144 * bitrate / (freqs[freq_index]<<lsf) + padding;
148 else
150 ERR("Impossible layer %d\n", layer);
151 return E_INVALIDARG;
154 duration = (ULONGLONG)10000000 * (ULONGLONG)(length) / (ULONGLONG)(bitrate/8);
155 *plen = length;
156 if (pduration)
157 *pduration += duration;
158 return S_OK;
161 static HRESULT FillBuffer(MPEGSplitterImpl *This, IMediaSample *pCurrentSample)
163 Parser_OutputPin * pOutputPin = unsafe_impl_Parser_OutputPin_from_IPin(This->Parser.ppPins[1]);
164 LONGLONG length = 0;
165 LONGLONG pos = BYTES_FROM_MEDIATIME(This->Parser.pInputPin->rtNext);
166 LONGLONG time = This->position, rtstop, rtstart;
167 HRESULT hr;
168 BYTE *fbuf = NULL;
169 DWORD len = IMediaSample_GetActualDataLength(pCurrentSample);
171 TRACE("Source length: %u\n", len);
172 IMediaSample_GetPointer(pCurrentSample, &fbuf);
174 /* Find the next valid header.. it <SHOULD> be right here */
175 hr = parse_header(fbuf, &length, &This->position);
176 assert(hr == S_OK);
177 IMediaSample_SetActualDataLength(pCurrentSample, length);
179 /* Queue the next sample */
180 if (length + 4 == len)
182 PullPin *pin = This->Parser.pInputPin;
183 LONGLONG stop = BYTES_FROM_MEDIATIME(pin->rtStop);
185 hr = S_OK;
186 memcpy(This->header, fbuf + length, 4);
187 while (FAILED(hr = parse_header(This->header, &length, NULL)))
189 memmove(This->header, This->header+1, 3);
190 if (pos + 4 >= stop)
191 break;
192 IAsyncReader_SyncRead(pin->pReader, ++pos, 1, This->header + 3);
194 pin->rtNext = MEDIATIME_FROM_BYTES(pos);
196 if (SUCCEEDED(hr))
198 /* Remove 4 for the last header, which should hopefully work */
199 IMediaSample *sample = NULL;
200 LONGLONG rtSampleStart = pin->rtNext - MEDIATIME_FROM_BYTES(4);
201 LONGLONG rtSampleStop = rtSampleStart + MEDIATIME_FROM_BYTES(length + 4);
203 if (rtSampleStop > pin->rtStop)
204 rtSampleStop = MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(pin->rtStop), pin->cbAlign));
206 hr = IMemAllocator_GetBuffer(pin->pAlloc, &sample, NULL, NULL, 0);
207 if (SUCCEEDED(hr))
209 IMediaSample_SetTime(sample, &rtSampleStart, &rtSampleStop);
210 IMediaSample_SetPreroll(sample, 0);
211 IMediaSample_SetDiscontinuity(sample, 0);
212 IMediaSample_SetSyncPoint(sample, 1);
213 hr = IAsyncReader_Request(pin->pReader, sample, 0);
214 if (SUCCEEDED(hr))
216 pin->rtCurrent = rtSampleStart;
217 pin->rtNext = rtSampleStop;
219 else
220 IMediaSample_Release(sample);
222 if (FAILED(hr))
223 FIXME("o_Ox%08x\n", hr);
226 /* If not, we're presumably at the end of file */
228 TRACE("Media time : %u.%03u\n", (DWORD)(This->position/10000000), (DWORD)((This->position/10000)%1000));
230 if (IMediaSample_IsDiscontinuity(pCurrentSample) == S_OK) {
231 IPin *victim;
232 EnterCriticalSection(&This->Parser.filter.csFilter);
233 pOutputPin->pin.pin.tStart = time;
234 pOutputPin->pin.pin.dRate = This->Parser.sourceSeeking.dRate;
235 hr = IPin_ConnectedTo(&pOutputPin->pin.pin.IPin_iface, &victim);
236 if (hr == S_OK)
238 hr = IPin_NewSegment(victim, time, This->Parser.sourceSeeking.llStop,
239 This->Parser.sourceSeeking.dRate);
240 if (hr != S_OK)
241 FIXME("NewSegment returns %08x\n", hr);
242 IPin_Release(victim);
244 LeaveCriticalSection(&This->Parser.filter.csFilter);
245 if (hr != S_OK)
246 return hr;
248 rtstart = (double)(time - pOutputPin->pin.pin.tStart) / pOutputPin->pin.pin.dRate;
249 rtstop = (double)(This->position - pOutputPin->pin.pin.tStart) / pOutputPin->pin.pin.dRate;
250 IMediaSample_SetTime(pCurrentSample, &rtstart, &rtstop);
251 IMediaSample_SetMediaTime(pCurrentSample, &time, &This->position);
253 hr = BaseOutputPinImpl_Deliver(&pOutputPin->pin, pCurrentSample);
255 if (hr != S_OK)
257 if (hr != S_FALSE)
258 TRACE("Error sending sample (%x)\n", hr);
259 else
260 TRACE("S_FALSE (%d), holding\n", IMediaSample_GetActualDataLength(pCurrentSample));
263 return hr;
267 static HRESULT MPEGSplitter_process_sample(LPVOID iface, IMediaSample * pSample, DWORD_PTR cookie)
269 MPEGSplitterImpl *This = iface;
270 BYTE *pbSrcStream;
271 DWORD cbSrcStream = 0;
272 REFERENCE_TIME tStart, tStop, tAviStart = This->position;
273 HRESULT hr;
275 hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
276 if (SUCCEEDED(hr))
278 cbSrcStream = IMediaSample_GetActualDataLength(pSample);
279 hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
282 /* Flush occurring */
283 if (cbSrcStream == 0)
285 FIXME(".. Why do I need you?\n");
286 return S_OK;
289 /* trace removed for performance reasons */
290 /* TRACE("(%p), %llu -> %llu\n", pSample, tStart, tStop); */
292 /* Now, try to find a new header */
293 hr = FillBuffer(This, pSample);
294 if (hr != S_OK)
296 WARN("Failed with hres: %08x!\n", hr);
298 /* Unset progression if denied! */
299 if (hr == VFW_E_WRONG_STATE || hr == S_FALSE)
301 memcpy(This->header, pbSrcStream, 4);
302 This->Parser.pInputPin->rtCurrent = tStart;
303 This->position = tAviStart;
307 if (BYTES_FROM_MEDIATIME(tStop) >= This->EndOfFile || This->position >= This->Parser.sourceSeeking.llStop)
309 unsigned int i;
311 TRACE("End of file reached\n");
313 for (i = 0; i < This->Parser.cStreams; i++)
315 IPin* ppin;
317 hr = IPin_ConnectedTo(This->Parser.ppPins[i+1], &ppin);
318 if (SUCCEEDED(hr))
320 hr = IPin_EndOfStream(ppin);
321 IPin_Release(ppin);
323 if (FAILED(hr))
324 WARN("Error sending EndOfStream to pin %u (%x)\n", i, hr);
327 /* Force the pullpin thread to stop */
328 hr = S_FALSE;
331 return hr;
335 static HRESULT MPEGSplitter_query_accept(LPVOID iface, const AM_MEDIA_TYPE *pmt)
337 if (!IsEqualIID(&pmt->majortype, &MEDIATYPE_Stream))
338 return S_FALSE;
340 if (IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_MPEG1Audio))
341 return S_OK;
343 if (IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_MPEG1Video))
344 FIXME("MPEG-1 video streams not yet supported.\n");
345 else if (IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_MPEG1System))
346 FIXME("MPEG-1 system streams not yet supported.\n");
347 else if (IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_MPEG1VideoCD))
348 FIXME("MPEG-1 VideoCD streams not yet supported.\n");
349 else FIXME("%s\n", debugstr_guid(&pmt->subtype));
351 return S_FALSE;
355 static HRESULT MPEGSplitter_init_audio(MPEGSplitterImpl *This, const BYTE *header, PIN_INFO *ppiOutput, AM_MEDIA_TYPE *pamt)
357 WAVEFORMATEX *format;
358 int bitrate_index;
359 int freq_index;
360 int mode_ext;
361 int emphasis;
362 int lsf = 1;
363 int mpeg1;
364 int layer;
365 int mode;
367 ZeroMemory(pamt, sizeof(*pamt));
368 ppiOutput->dir = PINDIR_OUTPUT;
369 ppiOutput->pFilter = (IBaseFilter*)This;
370 wsprintfW(ppiOutput->achName, wszAudioStream);
372 pamt->formattype = FORMAT_WaveFormatEx;
373 pamt->majortype = MEDIATYPE_Audio;
374 pamt->subtype = MEDIASUBTYPE_MPEG1AudioPayload;
376 pamt->lSampleSize = 0;
377 pamt->bFixedSizeSamples = FALSE;
378 pamt->bTemporalCompression = 0;
380 mpeg1 = (header[1]>>4)&0x1;
381 if (mpeg1)
382 lsf = ((header[1]>>3)&0x1)^1;
384 layer = 4-((header[1]>>1)&0x3);
385 bitrate_index = ((header[2]>>4)&0xf);
386 freq_index = ((header[2]>>2)&0x3) + (mpeg1?(lsf*3):6);
387 mode = ((header[3]>>6)&0x3);
388 mode_ext = ((header[3]>>4)&0x3);
389 emphasis = ((header[3]>>0)&0x3);
391 if (!bitrate_index)
393 /* Set to highest bitrate so samples will fit in for sure */
394 FIXME("Variable-bitrate audio not fully supported.\n");
395 bitrate_index = 15;
398 pamt->cbFormat = ((layer==3)? sizeof(MPEGLAYER3WAVEFORMAT) :
399 sizeof(MPEG1WAVEFORMAT));
400 pamt->pbFormat = CoTaskMemAlloc(pamt->cbFormat);
401 if (!pamt->pbFormat)
402 return E_OUTOFMEMORY;
403 ZeroMemory(pamt->pbFormat, pamt->cbFormat);
404 format = (WAVEFORMATEX*)pamt->pbFormat;
406 format->wFormatTag = ((layer == 3) ? WAVE_FORMAT_MPEGLAYER3 :
407 WAVE_FORMAT_MPEG);
408 format->nChannels = ((mode == 3) ? 1 : 2);
409 format->nSamplesPerSec = freqs[freq_index];
410 format->nAvgBytesPerSec = tabsel_123[lsf][layer-1][bitrate_index] * 1000 / 8;
412 if (layer == 3)
413 format->nBlockAlign = format->nAvgBytesPerSec * 8 * 144 /
414 (format->nSamplesPerSec<<lsf) + 1;
415 else if (layer == 2)
416 format->nBlockAlign = format->nAvgBytesPerSec * 8 * 144 /
417 format->nSamplesPerSec + 1;
418 else
419 format->nBlockAlign = 4 * (format->nAvgBytesPerSec * 8 * 12 / format->nSamplesPerSec + 1);
421 format->wBitsPerSample = 0;
423 if (layer == 3)
425 MPEGLAYER3WAVEFORMAT *mp3format = (MPEGLAYER3WAVEFORMAT*)format;
427 format->cbSize = MPEGLAYER3_WFX_EXTRA_BYTES;
429 mp3format->wID = MPEGLAYER3_ID_MPEG;
430 mp3format->fdwFlags = MPEGLAYER3_FLAG_PADDING_ON;
431 mp3format->nBlockSize = format->nBlockAlign;
432 mp3format->nFramesPerBlock = 1;
434 /* Beware the evil magic numbers. This struct is apparently horribly
435 * under-documented, and the only references I could find had it being
436 * set to this with no real explanation. It works fine though, so I'm
437 * not complaining (yet).
439 mp3format->nCodecDelay = 1393;
441 else
443 MPEG1WAVEFORMAT *mpgformat = (MPEG1WAVEFORMAT*)format;
445 format->cbSize = 22;
447 mpgformat->fwHeadLayer = ((layer == 1) ? ACM_MPEG_LAYER1 :
448 ((layer == 2) ? ACM_MPEG_LAYER2 :
449 ACM_MPEG_LAYER3));
450 mpgformat->dwHeadBitrate = format->nAvgBytesPerSec * 8;
451 mpgformat->fwHeadMode = ((mode == 3) ? ACM_MPEG_SINGLECHANNEL :
452 ((mode == 2) ? ACM_MPEG_DUALCHANNEL :
453 ((mode == 1) ? ACM_MPEG_JOINTSTEREO :
454 ACM_MPEG_STEREO)));
455 mpgformat->fwHeadModeExt = ((mode == 1) ? 0x0F : (1<<mode_ext));
456 mpgformat->wHeadEmphasis = emphasis + 1;
457 mpgformat->fwHeadFlags = ACM_MPEG_ID_MPEG1;
459 pamt->subtype.Data1 = format->wFormatTag;
461 TRACE("MPEG audio stream detected:\n"
462 "\tLayer %d (%#x)\n"
463 "\tFrequency: %d\n"
464 "\tChannels: %d (%d)\n"
465 "\tBytesPerSec: %d\n",
466 layer, format->wFormatTag, format->nSamplesPerSec,
467 format->nChannels, mode, format->nAvgBytesPerSec);
469 dump_AM_MEDIA_TYPE(pamt);
471 return S_OK;
475 static HRESULT MPEGSplitter_pre_connect(IPin *iface, IPin *pConnectPin, ALLOCATOR_PROPERTIES *props)
477 PullPin *pPin = impl_PullPin_from_IPin(iface);
478 MPEGSplitterImpl *This = (MPEGSplitterImpl*)pPin->pin.pinInfo.pFilter;
479 HRESULT hr;
480 LONGLONG pos = 0; /* in bytes */
481 BYTE header[10];
482 int streamtype = 0;
483 LONGLONG total, avail;
484 AM_MEDIA_TYPE amt;
485 PIN_INFO piOutput;
487 IAsyncReader_Length(pPin->pReader, &total, &avail);
488 This->EndOfFile = total;
490 hr = IAsyncReader_SyncRead(pPin->pReader, pos, 4, header);
491 if (SUCCEEDED(hr))
492 pos += 4;
494 /* Skip ID3 v2 tag, if any */
495 if (SUCCEEDED(hr) && !memcmp("ID3", header, 3))
496 do {
497 UINT length = 0;
498 hr = IAsyncReader_SyncRead(pPin->pReader, pos, 6, header + 4);
499 if (FAILED(hr))
500 break;
501 pos += 6;
502 TRACE("Found ID3 v2.%d.%d\n", header[3], header[4]);
503 if(header[3] <= 4 && header[4] != 0xff &&
504 (header[5]&0x0f) == 0 && (header[6]&0x80) == 0 &&
505 (header[7]&0x80) == 0 && (header[8]&0x80) == 0 &&
506 (header[9]&0x80) == 0)
508 length = (header[6]<<21) | (header[7]<<14) |
509 (header[8]<< 7) | (header[9] );
510 if((header[5]&0x10))
511 length += 10;
512 TRACE("Length: %u\n", length);
514 pos += length;
516 /* Read the real header for the mpeg splitter */
517 hr = IAsyncReader_SyncRead(pPin->pReader, pos, 4, header);
518 if (SUCCEEDED(hr))
519 pos += 4;
520 TRACE("%x:%x:%x:%x\n", header[0], header[1], header[2], header[3]);
521 } while (0);
523 while(SUCCEEDED(hr) && !(streamtype=MPEGSplitter_head_check(header)))
525 TRACE("%x:%x:%x:%x\n", header[0], header[1], header[2], header[3]);
526 /* No valid header yet; shift by a byte and check again */
527 memmove(header, header+1, 3);
528 hr = IAsyncReader_SyncRead(pPin->pReader, pos++, 1, header + 3);
530 if (FAILED(hr))
531 return hr;
532 pos -= 4;
533 This->begin_offset = pos;
534 memcpy(This->header, header, 4);
536 switch(streamtype)
538 case MPEG_AUDIO_HEADER:
540 LONGLONG duration = 0;
541 WAVEFORMATEX *format;
543 hr = MPEGSplitter_init_audio(This, header, &piOutput, &amt);
544 if (SUCCEEDED(hr))
546 format = (WAVEFORMATEX*)amt.pbFormat;
548 props->cbAlign = 1;
549 props->cbPrefix = 0;
550 /* Make the output buffer a multiple of the frame size */
551 props->cbBuffer = 0x4000 / format->nBlockAlign *
552 format->nBlockAlign;
553 props->cBuffers = 3;
554 hr = Parser_AddPin(&(This->Parser), &piOutput, props, &amt);
557 if (FAILED(hr))
559 if (amt.pbFormat)
560 CoTaskMemFree(amt.pbFormat);
561 ERR("Could not create pin for MPEG audio stream (%x)\n", hr);
562 break;
565 /* Check for idv1 tag, and remove it from stream if found */
566 hr = IAsyncReader_SyncRead(pPin->pReader, This->EndOfFile-128, 3, header);
567 if (FAILED(hr))
568 break;
569 if (!strncmp((char*)header, "TAG", 3))
570 This->EndOfFile -= 128;
571 This->Parser.pInputPin->rtStop = MEDIATIME_FROM_BYTES(This->EndOfFile);
572 This->Parser.pInputPin->rtStart = This->Parser.pInputPin->rtCurrent = MEDIATIME_FROM_BYTES(This->begin_offset);
574 duration = (This->EndOfFile-This->begin_offset) * 10000000 / format->nAvgBytesPerSec;
575 TRACE("Duration: %d seconds\n", (DWORD)(duration / 10000000));
577 This->Parser.sourceSeeking.llCurrent = 0;
578 This->Parser.sourceSeeking.llDuration = duration;
579 This->Parser.sourceSeeking.llStop = duration;
580 break;
582 case MPEG_VIDEO_HEADER:
583 FIXME("MPEG video processing not yet supported!\n");
584 hr = E_FAIL;
585 break;
586 case MPEG_SYSTEM_HEADER:
587 FIXME("MPEG system streams not yet supported!\n");
588 hr = E_FAIL;
589 break;
591 default:
592 break;
594 This->position = 0;
596 return hr;
599 static HRESULT MPEGSplitter_cleanup(LPVOID iface)
601 MPEGSplitterImpl *This = iface;
603 TRACE("(%p)\n", This);
605 return S_OK;
608 static HRESULT WINAPI MPEGSplitter_seek(IMediaSeeking *iface)
610 MPEGSplitterImpl *This = impl_from_IMediaSeeking(iface);
611 PullPin *pPin = This->Parser.pInputPin;
612 LONGLONG newpos, timepos, bytepos;
613 HRESULT hr = E_INVALIDARG;
614 BYTE header[4];
616 newpos = This->Parser.sourceSeeking.llCurrent;
617 if (This->position/1000000 == newpos/1000000)
619 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 return S_OK;
623 bytepos = This->begin_offset;
624 timepos = 0;
625 /* http://mpgedit.org/mpgedit/mpeg_format/mpeghdr.htm has a whole read up on audio headers */
626 while (bytepos + 3 < This->EndOfFile)
628 LONGLONG duration = timepos;
629 LONGLONG length = 0;
630 hr = IAsyncReader_SyncRead(pPin->pReader, bytepos, 4, header);
631 if (hr != S_OK)
632 break;
633 while ((hr=parse_header(header, &length, &duration)) != S_OK &&
634 bytepos + 4 < This->EndOfFile)
636 /* No valid header yet; shift by a byte and check again */
637 memmove(header, header+1, 3);
638 hr = IAsyncReader_SyncRead(pPin->pReader, ++bytepos + 3, 1, header + 3);
639 if (hr != S_OK)
640 break;
642 if (hr != S_OK || duration > newpos)
643 break;
644 bytepos += length;
645 timepos = duration;
648 if (SUCCEEDED(hr))
650 PullPin *pin = This->Parser.pInputPin;
652 TRACE("Moving sound to %08u bytes!\n", (DWORD)bytepos);
654 EnterCriticalSection(&pin->thread_lock);
655 IPin_BeginFlush(&pin->pin.IPin_iface);
657 /* Make sure this is done while stopped, BeginFlush takes care of this */
658 EnterCriticalSection(&This->Parser.filter.csFilter);
659 memcpy(This->header, header, 4);
661 pin->rtStart = pin->rtCurrent = MEDIATIME_FROM_BYTES(bytepos);
662 pin->rtStop = MEDIATIME_FROM_BYTES((REFERENCE_TIME)This->EndOfFile);
663 This->seek = TRUE;
664 This->position = newpos;
665 LeaveCriticalSection(&This->Parser.filter.csFilter);
667 TRACE("Done flushing\n");
668 IPin_EndFlush(&pin->pin.IPin_iface);
669 LeaveCriticalSection(&pin->thread_lock);
671 return hr;
674 static HRESULT MPEGSplitter_disconnect(LPVOID iface)
676 /* TODO: Find memory leaks etc */
677 return S_OK;
680 static HRESULT MPEGSplitter_first_request(LPVOID iface)
682 MPEGSplitterImpl *This = iface;
683 PullPin *pin = This->Parser.pInputPin;
684 HRESULT hr;
685 LONGLONG length;
686 IMediaSample *sample;
688 TRACE("Seeking? %d\n", This->seek);
690 hr = parse_header(This->header, &length, NULL);
691 assert(hr == S_OK);
693 if (pin->rtCurrent >= pin->rtStop)
695 /* Last sample has already been queued, request nothing more */
696 FIXME("Done!\n");
697 return S_OK;
700 hr = IMemAllocator_GetBuffer(pin->pAlloc, &sample, NULL, NULL, 0);
702 pin->rtNext = pin->rtCurrent;
703 if (SUCCEEDED(hr))
705 LONGLONG rtSampleStart = pin->rtNext;
706 /* Add 4 for the next header, which should hopefully work */
707 LONGLONG rtSampleStop = rtSampleStart + MEDIATIME_FROM_BYTES(length + 4);
709 if (rtSampleStop > pin->rtStop)
710 rtSampleStop = MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(pin->rtStop), pin->cbAlign));
712 hr = IMediaSample_SetTime(sample, &rtSampleStart, &rtSampleStop);
714 IMediaSample_SetPreroll(sample, FALSE);
715 IMediaSample_SetDiscontinuity(sample, TRUE);
716 IMediaSample_SetSyncPoint(sample, 1);
717 This->seek = 0;
719 hr = IAsyncReader_Request(pin->pReader, sample, 0);
720 if (SUCCEEDED(hr))
722 pin->rtCurrent = pin->rtNext;
723 pin->rtNext = rtSampleStop;
725 else
726 IMediaSample_Release(sample);
728 if (FAILED(hr))
729 ERR("Horsemen of the apocalypse came to bring error 0x%08x\n", hr);
731 return hr;
734 static const IBaseFilterVtbl MPEGSplitter_Vtbl =
736 Parser_QueryInterface,
737 Parser_AddRef,
738 Parser_Release,
739 Parser_GetClassID,
740 Parser_Stop,
741 Parser_Pause,
742 Parser_Run,
743 Parser_GetState,
744 Parser_SetSyncSource,
745 Parser_GetSyncSource,
746 Parser_EnumPins,
747 Parser_FindPin,
748 Parser_QueryFilterInfo,
749 Parser_JoinFilterGraph,
750 Parser_QueryVendorInfo
753 HRESULT MPEGSplitter_create(IUnknown * pUnkOuter, LPVOID * ppv)
755 MPEGSplitterImpl *This;
756 HRESULT hr = E_FAIL;
758 TRACE("(%p, %p)\n", pUnkOuter, ppv);
760 *ppv = NULL;
762 if (pUnkOuter)
763 return CLASS_E_NOAGGREGATION;
765 This = CoTaskMemAlloc(sizeof(MPEGSplitterImpl));
766 if (!This)
767 return E_OUTOFMEMORY;
769 ZeroMemory(This, sizeof(MPEGSplitterImpl));
770 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);
771 if (FAILED(hr))
773 CoTaskMemFree(This);
774 return hr;
776 This->seek = 1;
778 /* Note: This memory is managed by the parser filter once created */
779 *ppv = This;
781 return hr;