ntdll: Implement RtlValidRelativeSecurityDescriptor.
[wine.git] / dlls / quartz / mpegsplit.c
blob835eef6597ded11eb5312c26a2558b89e9e65aaf
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};
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;
115 LONGLONG duration;
117 if (MPEGSplitter_head_check(header) != MPEG_AUDIO_HEADER)
119 FIXME("Not a valid header: %02x:%02x:%02x:%02x\n", header[0], header[1], header[2], header[3]);
120 return E_INVALIDARG;
123 mpeg1 = (header[1]>>4)&0x1;
124 if (mpeg1)
125 lsf = ((header[1]>>3)&0x1)^1;
127 layer = 4-((header[1]>>1)&0x3);
128 bitrate_index = ((header[2]>>4)&0xf);
129 freq_index = ((header[2]>>2)&0x3) + (mpeg1?(lsf*3):6);
130 padding = ((header[2]>>1)&0x1);
132 bitrate = tabsel_123[lsf][layer-1][bitrate_index] * 1000;
133 if (!bitrate)
135 FIXME("Not a valid header: %02x:%02x:%02x:%02x\n", header[0], header[1], header[2], header[3]);
136 return E_INVALIDARG;
139 if (layer == 1)
140 length = 4 * (12 * bitrate / freqs[freq_index] + padding);
141 else if (layer == 2)
142 length = 144 * bitrate / freqs[freq_index] + padding;
143 else if (layer == 3)
144 length = 144 * bitrate / (freqs[freq_index]<<lsf) + padding;
145 else
147 ERR("Impossible layer %d\n", layer);
148 return E_INVALIDARG;
151 duration = (ULONGLONG)10000000 * (ULONGLONG)(length) / (ULONGLONG)(bitrate/8);
152 *plen = length;
153 if (pduration)
154 *pduration += duration;
155 return S_OK;
158 static HRESULT FillBuffer(MPEGSplitterImpl *This, IMediaSample *pCurrentSample)
160 Parser_OutputPin * pOutputPin = unsafe_impl_Parser_OutputPin_from_IPin(This->Parser.ppPins[1]);
161 LONGLONG length = 0;
162 LONGLONG pos = BYTES_FROM_MEDIATIME(This->Parser.pInputPin->rtNext);
163 LONGLONG time = This->position, rtstop, rtstart;
164 HRESULT hr;
165 BYTE *fbuf = NULL;
166 DWORD len = IMediaSample_GetActualDataLength(pCurrentSample);
168 TRACE("Source length: %u\n", len);
169 IMediaSample_GetPointer(pCurrentSample, &fbuf);
171 /* Find the next valid header.. it <SHOULD> be right here */
172 hr = parse_header(fbuf, &length, &This->position);
173 assert(hr == S_OK);
174 IMediaSample_SetActualDataLength(pCurrentSample, length);
176 /* Queue the next sample */
177 if (length + 4 == len)
179 PullPin *pin = This->Parser.pInputPin;
180 LONGLONG stop = BYTES_FROM_MEDIATIME(pin->rtStop);
182 hr = S_OK;
183 memcpy(This->header, fbuf + length, 4);
184 while (FAILED(hr = parse_header(This->header, &length, NULL)))
186 memmove(This->header, This->header+1, 3);
187 if (pos + 4 >= stop)
188 break;
189 IAsyncReader_SyncRead(pin->pReader, ++pos, 1, This->header + 3);
191 pin->rtNext = MEDIATIME_FROM_BYTES(pos);
193 if (SUCCEEDED(hr))
195 /* Remove 4 for the last header, which should hopefully work */
196 IMediaSample *sample = NULL;
197 LONGLONG rtSampleStart = pin->rtNext - MEDIATIME_FROM_BYTES(4);
198 LONGLONG rtSampleStop = rtSampleStart + MEDIATIME_FROM_BYTES(length + 4);
200 if (rtSampleStop > pin->rtStop)
201 rtSampleStop = MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(pin->rtStop), pin->cbAlign));
203 hr = IMemAllocator_GetBuffer(pin->pAlloc, &sample, NULL, NULL, 0);
204 if (SUCCEEDED(hr))
206 IMediaSample_SetTime(sample, &rtSampleStart, &rtSampleStop);
207 IMediaSample_SetPreroll(sample, FALSE);
208 IMediaSample_SetDiscontinuity(sample, FALSE);
209 IMediaSample_SetSyncPoint(sample, TRUE);
210 hr = IAsyncReader_Request(pin->pReader, sample, 0);
211 if (SUCCEEDED(hr))
213 pin->rtCurrent = rtSampleStart;
214 pin->rtNext = rtSampleStop;
216 else
217 IMediaSample_Release(sample);
219 if (FAILED(hr))
220 FIXME("o_Ox%08x\n", hr);
223 /* If not, we're presumably at the end of file */
225 TRACE("Media time : %u.%03u\n", (DWORD)(This->position/10000000), (DWORD)((This->position/10000)%1000));
227 if (IMediaSample_IsDiscontinuity(pCurrentSample) == S_OK) {
228 IPin *victim;
229 EnterCriticalSection(&This->Parser.filter.csFilter);
230 pOutputPin->pin.pin.tStart = time;
231 pOutputPin->pin.pin.dRate = This->Parser.sourceSeeking.dRate;
232 hr = IPin_ConnectedTo(&pOutputPin->pin.pin.IPin_iface, &victim);
233 if (hr == S_OK)
235 hr = IPin_NewSegment(victim, time, This->Parser.sourceSeeking.llStop,
236 This->Parser.sourceSeeking.dRate);
237 if (hr != S_OK)
238 FIXME("NewSegment returns %08x\n", hr);
239 IPin_Release(victim);
241 LeaveCriticalSection(&This->Parser.filter.csFilter);
242 if (hr != S_OK)
243 return hr;
245 rtstart = (double)(time - pOutputPin->pin.pin.tStart) / pOutputPin->pin.pin.dRate;
246 rtstop = (double)(This->position - pOutputPin->pin.pin.tStart) / pOutputPin->pin.pin.dRate;
247 IMediaSample_SetTime(pCurrentSample, &rtstart, &rtstop);
248 IMediaSample_SetMediaTime(pCurrentSample, &time, &This->position);
250 hr = BaseOutputPinImpl_Deliver(&pOutputPin->pin, pCurrentSample);
252 if (hr != S_OK)
254 if (hr != S_FALSE)
255 TRACE("Error sending sample (%x)\n", hr);
256 else
257 TRACE("S_FALSE (%d), holding\n", IMediaSample_GetActualDataLength(pCurrentSample));
260 return hr;
264 static HRESULT MPEGSplitter_process_sample(LPVOID iface, IMediaSample * pSample, DWORD_PTR cookie)
266 MPEGSplitterImpl *This = iface;
267 BYTE *pbSrcStream;
268 DWORD cbSrcStream = 0;
269 REFERENCE_TIME tStart, tStop, tAviStart = This->position;
270 HRESULT hr;
272 hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
273 if (SUCCEEDED(hr))
275 cbSrcStream = IMediaSample_GetActualDataLength(pSample);
276 hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
279 /* Flush occurring */
280 if (cbSrcStream == 0)
282 FIXME(".. Why do I need you?\n");
283 return S_OK;
286 /* trace removed for performance reasons */
287 /* TRACE("(%p), %llu -> %llu\n", pSample, tStart, tStop); */
289 /* Now, try to find a new header */
290 hr = FillBuffer(This, pSample);
291 if (hr != S_OK)
293 WARN("Failed with hres: %08x!\n", hr);
295 /* Unset progression if denied! */
296 if (hr == VFW_E_WRONG_STATE || hr == S_FALSE)
298 memcpy(This->header, pbSrcStream, 4);
299 This->Parser.pInputPin->rtCurrent = tStart;
300 This->position = tAviStart;
304 if (BYTES_FROM_MEDIATIME(tStop) >= This->EndOfFile || This->position >= This->Parser.sourceSeeking.llStop)
306 unsigned int i;
308 TRACE("End of file reached\n");
310 for (i = 0; i < This->Parser.cStreams; i++)
312 IPin* ppin;
314 hr = IPin_ConnectedTo(This->Parser.ppPins[i+1], &ppin);
315 if (SUCCEEDED(hr))
317 hr = IPin_EndOfStream(ppin);
318 IPin_Release(ppin);
320 if (FAILED(hr))
321 WARN("Error sending EndOfStream to pin %u (%x)\n", i, hr);
324 /* Force the pullpin thread to stop */
325 hr = S_FALSE;
328 return hr;
332 static HRESULT MPEGSplitter_query_accept(LPVOID iface, const AM_MEDIA_TYPE *pmt)
334 if (!IsEqualIID(&pmt->majortype, &MEDIATYPE_Stream))
335 return S_FALSE;
337 if (IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_MPEG1Audio))
338 return S_OK;
340 if (IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_MPEG1Video))
341 FIXME("MPEG-1 video streams not yet supported.\n");
342 else if (IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_MPEG1System))
343 FIXME("MPEG-1 system streams not yet supported.\n");
344 else if (IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_MPEG1VideoCD))
345 FIXME("MPEG-1 VideoCD streams not yet supported.\n");
346 else FIXME("%s\n", debugstr_guid(&pmt->subtype));
348 return S_FALSE;
352 static HRESULT MPEGSplitter_init_audio(MPEGSplitterImpl *This, const BYTE *header, PIN_INFO *ppiOutput, AM_MEDIA_TYPE *pamt)
354 WAVEFORMATEX *format;
355 int bitrate_index;
356 int freq_index;
357 int mode_ext;
358 int emphasis;
359 int lsf = 1;
360 int mpeg1;
361 int layer;
362 int mode;
364 ZeroMemory(pamt, sizeof(*pamt));
365 ppiOutput->dir = PINDIR_OUTPUT;
366 ppiOutput->pFilter = (IBaseFilter*)This;
367 wsprintfW(ppiOutput->achName, wszAudioStream);
369 pamt->formattype = FORMAT_WaveFormatEx;
370 pamt->majortype = MEDIATYPE_Audio;
371 pamt->subtype = MEDIASUBTYPE_MPEG1AudioPayload;
373 pamt->lSampleSize = 0;
374 pamt->bFixedSizeSamples = FALSE;
375 pamt->bTemporalCompression = 0;
377 mpeg1 = (header[1]>>4)&0x1;
378 if (mpeg1)
379 lsf = ((header[1]>>3)&0x1)^1;
381 layer = 4-((header[1]>>1)&0x3);
382 bitrate_index = ((header[2]>>4)&0xf);
383 freq_index = ((header[2]>>2)&0x3) + (mpeg1?(lsf*3):6);
384 mode = ((header[3]>>6)&0x3);
385 mode_ext = ((header[3]>>4)&0x3);
386 emphasis = ((header[3]>>0)&0x3);
388 if (!bitrate_index)
390 /* Set to highest bitrate so samples will fit in for sure */
391 FIXME("Variable-bitrate audio not fully supported.\n");
392 bitrate_index = 15;
395 pamt->cbFormat = ((layer==3)? sizeof(MPEGLAYER3WAVEFORMAT) :
396 sizeof(MPEG1WAVEFORMAT));
397 pamt->pbFormat = CoTaskMemAlloc(pamt->cbFormat);
398 if (!pamt->pbFormat)
399 return E_OUTOFMEMORY;
400 ZeroMemory(pamt->pbFormat, pamt->cbFormat);
401 format = (WAVEFORMATEX*)pamt->pbFormat;
403 format->wFormatTag = ((layer == 3) ? WAVE_FORMAT_MPEGLAYER3 :
404 WAVE_FORMAT_MPEG);
405 format->nChannels = ((mode == 3) ? 1 : 2);
406 format->nSamplesPerSec = freqs[freq_index];
407 format->nAvgBytesPerSec = tabsel_123[lsf][layer-1][bitrate_index] * 1000 / 8;
409 if (layer == 3)
410 format->nBlockAlign = format->nAvgBytesPerSec * 8 * 144 /
411 (format->nSamplesPerSec<<lsf) + 1;
412 else if (layer == 2)
413 format->nBlockAlign = format->nAvgBytesPerSec * 8 * 144 /
414 format->nSamplesPerSec + 1;
415 else
416 format->nBlockAlign = 4 * (format->nAvgBytesPerSec * 8 * 12 / format->nSamplesPerSec + 1);
418 format->wBitsPerSample = 0;
420 if (layer == 3)
422 MPEGLAYER3WAVEFORMAT *mp3format = (MPEGLAYER3WAVEFORMAT*)format;
424 format->cbSize = MPEGLAYER3_WFX_EXTRA_BYTES;
426 mp3format->wID = MPEGLAYER3_ID_MPEG;
427 mp3format->fdwFlags = MPEGLAYER3_FLAG_PADDING_ON;
428 mp3format->nBlockSize = format->nBlockAlign;
429 mp3format->nFramesPerBlock = 1;
431 /* Beware the evil magic numbers. This struct is apparently horribly
432 * under-documented, and the only references I could find had it being
433 * set to this with no real explanation. It works fine though, so I'm
434 * not complaining (yet).
436 mp3format->nCodecDelay = 1393;
438 else
440 MPEG1WAVEFORMAT *mpgformat = (MPEG1WAVEFORMAT*)format;
442 format->cbSize = 22;
444 mpgformat->fwHeadLayer = ((layer == 1) ? ACM_MPEG_LAYER1 :
445 ((layer == 2) ? ACM_MPEG_LAYER2 :
446 ACM_MPEG_LAYER3));
447 mpgformat->dwHeadBitrate = format->nAvgBytesPerSec * 8;
448 mpgformat->fwHeadMode = ((mode == 3) ? ACM_MPEG_SINGLECHANNEL :
449 ((mode == 2) ? ACM_MPEG_DUALCHANNEL :
450 ((mode == 1) ? ACM_MPEG_JOINTSTEREO :
451 ACM_MPEG_STEREO)));
452 mpgformat->fwHeadModeExt = ((mode == 1) ? 0x0F : (1<<mode_ext));
453 mpgformat->wHeadEmphasis = emphasis + 1;
454 mpgformat->fwHeadFlags = ACM_MPEG_ID_MPEG1;
456 pamt->subtype.Data1 = format->wFormatTag;
458 TRACE("MPEG audio stream detected:\n"
459 "\tLayer %d (%#x)\n"
460 "\tFrequency: %d\n"
461 "\tChannels: %d (%d)\n"
462 "\tBytesPerSec: %d\n",
463 layer, format->wFormatTag, format->nSamplesPerSec,
464 format->nChannels, mode, format->nAvgBytesPerSec);
466 dump_AM_MEDIA_TYPE(pamt);
468 return S_OK;
472 static HRESULT MPEGSplitter_pre_connect(IPin *iface, IPin *pConnectPin, ALLOCATOR_PROPERTIES *props)
474 PullPin *pPin = impl_PullPin_from_IPin(iface);
475 MPEGSplitterImpl *This = (MPEGSplitterImpl*)pPin->pin.pinInfo.pFilter;
476 HRESULT hr;
477 LONGLONG pos = 0; /* in bytes */
478 BYTE header[10];
479 int streamtype = 0;
480 LONGLONG total, avail;
481 AM_MEDIA_TYPE amt;
482 PIN_INFO piOutput;
484 IAsyncReader_Length(pPin->pReader, &total, &avail);
485 This->EndOfFile = total;
487 hr = IAsyncReader_SyncRead(pPin->pReader, pos, 4, header);
488 if (SUCCEEDED(hr))
489 pos += 4;
491 /* Skip ID3 v2 tag, if any */
492 if (SUCCEEDED(hr) && !memcmp("ID3", header, 3))
493 do {
494 UINT length = 0;
495 hr = IAsyncReader_SyncRead(pPin->pReader, pos, 6, header + 4);
496 if (FAILED(hr))
497 break;
498 pos += 6;
499 TRACE("Found ID3 v2.%d.%d\n", header[3], header[4]);
500 if(header[3] <= 4 && header[4] != 0xff &&
501 (header[5]&0x0f) == 0 && (header[6]&0x80) == 0 &&
502 (header[7]&0x80) == 0 && (header[8]&0x80) == 0 &&
503 (header[9]&0x80) == 0)
505 length = (header[6]<<21) | (header[7]<<14) |
506 (header[8]<< 7) | (header[9] );
507 if((header[5]&0x10))
508 length += 10;
509 TRACE("Length: %u\n", length);
511 pos += length;
513 /* Read the real header for the mpeg splitter */
514 hr = IAsyncReader_SyncRead(pPin->pReader, pos, 4, header);
515 if (SUCCEEDED(hr))
516 pos += 4;
517 TRACE("%x:%x:%x:%x\n", header[0], header[1], header[2], header[3]);
518 } while (0);
520 while(SUCCEEDED(hr) && !(streamtype=MPEGSplitter_head_check(header)))
522 TRACE("%x:%x:%x:%x\n", header[0], header[1], header[2], header[3]);
523 /* No valid header yet; shift by a byte and check again */
524 memmove(header, header+1, 3);
525 hr = IAsyncReader_SyncRead(pPin->pReader, pos++, 1, header + 3);
527 if (FAILED(hr))
528 return hr;
529 pos -= 4;
530 This->begin_offset = pos;
531 memcpy(This->header, header, 4);
533 switch(streamtype)
535 case MPEG_AUDIO_HEADER:
537 LONGLONG duration = 0;
538 WAVEFORMATEX *format;
540 hr = MPEGSplitter_init_audio(This, header, &piOutput, &amt);
541 if (SUCCEEDED(hr))
543 format = (WAVEFORMATEX*)amt.pbFormat;
545 props->cbAlign = 1;
546 props->cbPrefix = 0;
547 /* Make the output buffer a multiple of the frame size */
548 props->cbBuffer = 0x4000 / format->nBlockAlign *
549 format->nBlockAlign;
550 props->cBuffers = 3;
551 hr = Parser_AddPin(&(This->Parser), &piOutput, props, &amt);
554 if (FAILED(hr))
556 if (amt.pbFormat)
557 CoTaskMemFree(amt.pbFormat);
558 ERR("Could not create pin for MPEG audio stream (%x)\n", hr);
559 break;
562 /* Check for idv1 tag, and remove it from stream if found */
563 hr = IAsyncReader_SyncRead(pPin->pReader, This->EndOfFile-128, 3, header);
564 if (FAILED(hr))
565 break;
566 if (!strncmp((char*)header, "TAG", 3))
567 This->EndOfFile -= 128;
568 This->Parser.pInputPin->rtStop = MEDIATIME_FROM_BYTES(This->EndOfFile);
569 This->Parser.pInputPin->rtStart = This->Parser.pInputPin->rtCurrent = MEDIATIME_FROM_BYTES(This->begin_offset);
571 duration = (This->EndOfFile-This->begin_offset) * 10000000 / format->nAvgBytesPerSec;
572 TRACE("Duration: %d seconds\n", (DWORD)(duration / 10000000));
574 This->Parser.sourceSeeking.llCurrent = 0;
575 This->Parser.sourceSeeking.llDuration = duration;
576 This->Parser.sourceSeeking.llStop = duration;
577 break;
579 case MPEG_VIDEO_HEADER:
580 FIXME("MPEG video processing not yet supported!\n");
581 hr = E_FAIL;
582 break;
583 case MPEG_SYSTEM_HEADER:
584 FIXME("MPEG system streams not yet supported!\n");
585 hr = E_FAIL;
586 break;
588 default:
589 break;
591 This->position = 0;
593 return hr;
596 static HRESULT MPEGSplitter_cleanup(LPVOID iface)
598 MPEGSplitterImpl *This = iface;
600 TRACE("(%p)\n", This);
602 return S_OK;
605 static HRESULT WINAPI MPEGSplitter_seek(IMediaSeeking *iface)
607 MPEGSplitterImpl *This = impl_from_IMediaSeeking(iface);
608 PullPin *pPin = This->Parser.pInputPin;
609 LONGLONG newpos, timepos, bytepos;
610 HRESULT hr = E_INVALIDARG;
611 BYTE header[4];
613 newpos = This->Parser.sourceSeeking.llCurrent;
614 if (This->position/1000000 == newpos/1000000)
616 TRACE("Requesting position %x%08x same as current position %x%08x\n", (DWORD)(newpos>>32), (DWORD)newpos, (DWORD)(This->position>>32), (DWORD)This->position);
617 return S_OK;
620 bytepos = This->begin_offset;
621 timepos = 0;
622 /* http://mpgedit.org/mpgedit/mpeg_format/mpeghdr.htm has a whole read up on audio headers */
623 while (bytepos + 3 < This->EndOfFile)
625 LONGLONG duration = timepos;
626 LONGLONG length = 0;
627 hr = IAsyncReader_SyncRead(pPin->pReader, bytepos, 4, header);
628 if (hr != S_OK)
629 break;
630 while ((hr=parse_header(header, &length, &duration)) != S_OK &&
631 bytepos + 4 < This->EndOfFile)
633 /* No valid header yet; shift by a byte and check again */
634 memmove(header, header+1, 3);
635 hr = IAsyncReader_SyncRead(pPin->pReader, ++bytepos + 3, 1, header + 3);
636 if (hr != S_OK)
637 break;
639 if (hr != S_OK || duration > newpos)
640 break;
641 bytepos += length;
642 timepos = duration;
645 if (SUCCEEDED(hr))
647 PullPin *pin = This->Parser.pInputPin;
649 TRACE("Moving sound to %08u bytes!\n", (DWORD)bytepos);
651 EnterCriticalSection(&pin->thread_lock);
652 IPin_BeginFlush(&pin->pin.IPin_iface);
654 /* Make sure this is done while stopped, BeginFlush takes care of this */
655 EnterCriticalSection(&This->Parser.filter.csFilter);
656 memcpy(This->header, header, 4);
658 pin->rtStart = pin->rtCurrent = MEDIATIME_FROM_BYTES(bytepos);
659 pin->rtStop = MEDIATIME_FROM_BYTES((REFERENCE_TIME)This->EndOfFile);
660 This->seek = TRUE;
661 This->position = newpos;
662 LeaveCriticalSection(&This->Parser.filter.csFilter);
664 TRACE("Done flushing\n");
665 IPin_EndFlush(&pin->pin.IPin_iface);
666 LeaveCriticalSection(&pin->thread_lock);
668 return hr;
671 static HRESULT MPEGSplitter_disconnect(LPVOID iface)
673 /* TODO: Find memory leaks etc */
674 return S_OK;
677 static HRESULT MPEGSplitter_first_request(LPVOID iface)
679 MPEGSplitterImpl *This = iface;
680 PullPin *pin = This->Parser.pInputPin;
681 HRESULT hr;
682 LONGLONG length;
683 IMediaSample *sample;
685 TRACE("Seeking? %d\n", This->seek);
687 hr = parse_header(This->header, &length, NULL);
688 assert(hr == S_OK);
690 if (pin->rtCurrent >= pin->rtStop)
692 /* Last sample has already been queued, request nothing more */
693 FIXME("Done!\n");
694 return S_OK;
697 hr = IMemAllocator_GetBuffer(pin->pAlloc, &sample, NULL, NULL, 0);
699 pin->rtNext = pin->rtCurrent;
700 if (SUCCEEDED(hr))
702 LONGLONG rtSampleStart = pin->rtNext;
703 /* Add 4 for the next header, which should hopefully work */
704 LONGLONG rtSampleStop = rtSampleStart + MEDIATIME_FROM_BYTES(length + 4);
706 if (rtSampleStop > pin->rtStop)
707 rtSampleStop = MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(pin->rtStop), pin->cbAlign));
709 IMediaSample_SetTime(sample, &rtSampleStart, &rtSampleStop);
710 IMediaSample_SetPreroll(sample, FALSE);
711 IMediaSample_SetDiscontinuity(sample, TRUE);
712 IMediaSample_SetSyncPoint(sample, 1);
713 This->seek = FALSE;
715 hr = IAsyncReader_Request(pin->pReader, sample, 0);
716 if (SUCCEEDED(hr))
718 pin->rtCurrent = pin->rtNext;
719 pin->rtNext = rtSampleStop;
721 else
722 IMediaSample_Release(sample);
724 if (FAILED(hr))
725 ERR("Horsemen of the apocalypse came to bring error 0x%08x\n", hr);
727 return hr;
730 static const IBaseFilterVtbl MPEGSplitter_Vtbl =
732 Parser_QueryInterface,
733 Parser_AddRef,
734 Parser_Release,
735 Parser_GetClassID,
736 Parser_Stop,
737 Parser_Pause,
738 Parser_Run,
739 Parser_GetState,
740 Parser_SetSyncSource,
741 Parser_GetSyncSource,
742 Parser_EnumPins,
743 Parser_FindPin,
744 Parser_QueryFilterInfo,
745 Parser_JoinFilterGraph,
746 Parser_QueryVendorInfo
749 HRESULT MPEGSplitter_create(IUnknown * pUnkOuter, LPVOID * ppv)
751 MPEGSplitterImpl *This;
752 HRESULT hr = E_FAIL;
754 TRACE("(%p, %p)\n", pUnkOuter, ppv);
756 *ppv = NULL;
758 if (pUnkOuter)
759 return CLASS_E_NOAGGREGATION;
761 This = CoTaskMemAlloc(sizeof(MPEGSplitterImpl));
762 if (!This)
763 return E_OUTOFMEMORY;
765 ZeroMemory(This, sizeof(MPEGSplitterImpl));
766 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);
767 if (FAILED(hr))
769 CoTaskMemFree(This);
770 return hr;
772 This->seek = TRUE;
774 /* Note: This memory is managed by the parser filter once created */
775 *ppv = This;
777 return hr;