winex11.drv: Remove unused code (Clang).
[wine.git] / dlls / quartz / mpegsplit.c
blob6f098226a8cb4e61c4a277a3c99915e60343e4f8
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 (!(header[0] == 0xff && ((header[1]>>5)&0x7) == 0x7 &&
118 ((header[1]>>1)&0x3) != 0 && ((header[2]>>4)&0xf) != 0xf &&
119 ((header[2]>>2)&0x3) != 0x3))
121 FIXME("Not a valid header: %02x:%02x:%02x:%02x\n", header[0], header[1], header[2], header[3]);
122 return E_INVALIDARG;
125 mpeg1 = (header[1]>>4)&0x1;
126 if (mpeg1)
127 lsf = ((header[1]>>3)&0x1)^1;
129 layer = 4-((header[1]>>1)&0x3);
130 bitrate_index = ((header[2]>>4)&0xf);
131 freq_index = ((header[2]>>2)&0x3) + (mpeg1?(lsf*3):6);
132 padding = ((header[2]>>1)&0x1);
134 bitrate = tabsel_123[lsf][layer-1][bitrate_index] * 1000;
135 if (!bitrate)
137 FIXME("Not a valid header: %02x:%02x:%02x:%02x\n", header[0], header[1], header[2], header[3]);
138 return E_INVALIDARG;
141 if (layer == 1)
142 length = 4 * (12 * bitrate / freqs[freq_index] + padding);
143 else if (layer == 2)
144 length = 144 * bitrate / freqs[freq_index] + padding;
145 else if (layer == 3)
146 length = 144 * bitrate / (freqs[freq_index]<<lsf) + padding;
147 else
149 ERR("Impossible layer %d\n", layer);
150 return E_INVALIDARG;
153 duration = (ULONGLONG)10000000 * (ULONGLONG)(length) / (ULONGLONG)(bitrate/8);
154 *plen = length;
155 if (pduration)
156 *pduration += duration;
157 return S_OK;
160 static HRESULT FillBuffer(MPEGSplitterImpl *This, IMediaSample *pCurrentSample)
162 Parser_OutputPin * pOutputPin = unsafe_impl_Parser_OutputPin_from_IPin(This->Parser.ppPins[1]);
163 LONGLONG length = 0;
164 LONGLONG pos = BYTES_FROM_MEDIATIME(This->Parser.pInputPin->rtNext);
165 LONGLONG time = This->position, rtstop, rtstart;
166 HRESULT hr;
167 BYTE *fbuf = NULL;
168 DWORD len = IMediaSample_GetActualDataLength(pCurrentSample);
170 TRACE("Source length: %u\n", len);
171 IMediaSample_GetPointer(pCurrentSample, &fbuf);
173 /* Find the next valid header.. it <SHOULD> be right here */
174 hr = parse_header(fbuf, &length, &This->position);
175 assert(hr == S_OK);
176 IMediaSample_SetActualDataLength(pCurrentSample, length);
178 /* Queue the next sample */
179 if (length + 4 == len)
181 PullPin *pin = This->Parser.pInputPin;
182 LONGLONG stop = BYTES_FROM_MEDIATIME(pin->rtStop);
184 hr = S_OK;
185 memcpy(This->header, fbuf + length, 4);
186 while (FAILED(hr = parse_header(This->header, &length, NULL)))
188 memmove(This->header, This->header+1, 3);
189 if (pos + 4 >= stop)
190 break;
191 IAsyncReader_SyncRead(pin->pReader, ++pos, 1, This->header + 3);
193 pin->rtNext = MEDIATIME_FROM_BYTES(pos);
195 if (SUCCEEDED(hr))
197 /* Remove 4 for the last header, which should hopefully work */
198 IMediaSample *sample = NULL;
199 LONGLONG rtSampleStart = pin->rtNext - MEDIATIME_FROM_BYTES(4);
200 LONGLONG rtSampleStop = rtSampleStart + MEDIATIME_FROM_BYTES(length + 4);
202 if (rtSampleStop > pin->rtStop)
203 rtSampleStop = MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(pin->rtStop), pin->cbAlign));
205 hr = IMemAllocator_GetBuffer(pin->pAlloc, &sample, NULL, NULL, 0);
206 if (SUCCEEDED(hr))
208 IMediaSample_SetTime(sample, &rtSampleStart, &rtSampleStop);
209 IMediaSample_SetPreroll(sample, 0);
210 IMediaSample_SetDiscontinuity(sample, 0);
211 IMediaSample_SetSyncPoint(sample, 1);
212 hr = IAsyncReader_Request(pin->pReader, sample, 0);
213 if (SUCCEEDED(hr))
215 pin->rtCurrent = rtSampleStart;
216 pin->rtNext = rtSampleStop;
218 else
219 IMediaSample_Release(sample);
221 if (FAILED(hr))
222 FIXME("o_Ox%08x\n", hr);
225 /* If not, we're presumably at the end of file */
227 TRACE("Media time : %u.%03u\n", (DWORD)(This->position/10000000), (DWORD)((This->position/10000)%1000));
229 if (IMediaSample_IsDiscontinuity(pCurrentSample) == S_OK) {
230 IPin *victim;
231 EnterCriticalSection(&This->Parser.filter.csFilter);
232 pOutputPin->pin.pin.tStart = time;
233 pOutputPin->pin.pin.dRate = This->Parser.sourceSeeking.dRate;
234 hr = IPin_ConnectedTo(&pOutputPin->pin.pin.IPin_iface, &victim);
235 if (hr == S_OK)
237 hr = IPin_NewSegment(victim, time, This->Parser.sourceSeeking.llStop,
238 This->Parser.sourceSeeking.dRate);
239 if (hr != S_OK)
240 FIXME("NewSegment returns %08x\n", hr);
241 IPin_Release(victim);
243 LeaveCriticalSection(&This->Parser.filter.csFilter);
244 if (hr != S_OK)
245 return hr;
247 rtstart = (double)(time - pOutputPin->pin.pin.tStart) / pOutputPin->pin.pin.dRate;
248 rtstop = (double)(This->position - pOutputPin->pin.pin.tStart) / pOutputPin->pin.pin.dRate;
249 IMediaSample_SetTime(pCurrentSample, &rtstart, &rtstop);
250 IMediaSample_SetMediaTime(pCurrentSample, &time, &This->position);
252 hr = BaseOutputPinImpl_Deliver(&pOutputPin->pin, pCurrentSample);
254 if (hr != S_OK)
256 if (hr != S_FALSE)
257 TRACE("Error sending sample (%x)\n", hr);
258 else
259 TRACE("S_FALSE (%d), holding\n", IMediaSample_GetActualDataLength(pCurrentSample));
262 return hr;
266 static HRESULT MPEGSplitter_process_sample(LPVOID iface, IMediaSample * pSample, DWORD_PTR cookie)
268 MPEGSplitterImpl *This = iface;
269 BYTE *pbSrcStream;
270 DWORD cbSrcStream = 0;
271 REFERENCE_TIME tStart, tStop, tAviStart = This->position;
272 HRESULT hr;
274 hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
275 if (SUCCEEDED(hr))
277 cbSrcStream = IMediaSample_GetActualDataLength(pSample);
278 hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
281 /* Flush occurring */
282 if (cbSrcStream == 0)
284 FIXME(".. Why do I need you?\n");
285 return S_OK;
288 /* trace removed for performance reasons */
289 /* TRACE("(%p), %llu -> %llu\n", pSample, tStart, tStop); */
291 /* Now, try to find a new header */
292 hr = FillBuffer(This, pSample);
293 if (hr != S_OK)
295 WARN("Failed with hres: %08x!\n", hr);
297 /* Unset progression if denied! */
298 if (hr == VFW_E_WRONG_STATE || hr == S_FALSE)
300 memcpy(This->header, pbSrcStream, 4);
301 This->Parser.pInputPin->rtCurrent = tStart;
302 This->position = tAviStart;
306 if (BYTES_FROM_MEDIATIME(tStop) >= This->EndOfFile || This->position >= This->Parser.sourceSeeking.llStop)
308 unsigned int i;
310 TRACE("End of file reached\n");
312 for (i = 0; i < This->Parser.cStreams; i++)
314 IPin* ppin;
316 hr = IPin_ConnectedTo(This->Parser.ppPins[i+1], &ppin);
317 if (SUCCEEDED(hr))
319 hr = IPin_EndOfStream(ppin);
320 IPin_Release(ppin);
322 if (FAILED(hr))
323 WARN("Error sending EndOfStream to pin %u (%x)\n", i, hr);
326 /* Force the pullpin thread to stop */
327 hr = S_FALSE;
330 return hr;
334 static HRESULT MPEGSplitter_query_accept(LPVOID iface, const AM_MEDIA_TYPE *pmt)
336 if (!IsEqualIID(&pmt->majortype, &MEDIATYPE_Stream))
337 return S_FALSE;
339 if (IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_MPEG1Audio))
340 return S_OK;
342 if (IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_MPEG1Video))
343 FIXME("MPEG-1 video streams not yet supported.\n");
344 else if (IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_MPEG1System))
345 FIXME("MPEG-1 system streams not yet supported.\n");
346 else if (IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_MPEG1VideoCD))
347 FIXME("MPEG-1 VideoCD streams not yet supported.\n");
348 else FIXME("%s\n", debugstr_guid(&pmt->subtype));
350 return S_FALSE;
354 static HRESULT MPEGSplitter_init_audio(MPEGSplitterImpl *This, const BYTE *header, PIN_INFO *ppiOutput, AM_MEDIA_TYPE *pamt)
356 WAVEFORMATEX *format;
357 int bitrate_index;
358 int freq_index;
359 int mode_ext;
360 int emphasis;
361 int lsf = 1;
362 int mpeg1;
363 int layer;
364 int mode;
366 ZeroMemory(pamt, sizeof(*pamt));
367 ppiOutput->dir = PINDIR_OUTPUT;
368 ppiOutput->pFilter = (IBaseFilter*)This;
369 wsprintfW(ppiOutput->achName, wszAudioStream);
371 pamt->formattype = FORMAT_WaveFormatEx;
372 pamt->majortype = MEDIATYPE_Audio;
373 pamt->subtype = MEDIASUBTYPE_MPEG1AudioPayload;
375 pamt->lSampleSize = 0;
376 pamt->bFixedSizeSamples = FALSE;
377 pamt->bTemporalCompression = 0;
379 mpeg1 = (header[1]>>4)&0x1;
380 if (mpeg1)
381 lsf = ((header[1]>>3)&0x1)^1;
383 layer = 4-((header[1]>>1)&0x3);
384 bitrate_index = ((header[2]>>4)&0xf);
385 freq_index = ((header[2]>>2)&0x3) + (mpeg1?(lsf*3):6);
386 mode = ((header[3]>>6)&0x3);
387 mode_ext = ((header[3]>>4)&0x3);
388 emphasis = ((header[3]>>0)&0x3);
390 if (!bitrate_index)
392 /* Set to highest bitrate so samples will fit in for sure */
393 FIXME("Variable-bitrate audio not fully supported.\n");
394 bitrate_index = 15;
397 pamt->cbFormat = ((layer==3)? sizeof(MPEGLAYER3WAVEFORMAT) :
398 sizeof(MPEG1WAVEFORMAT));
399 pamt->pbFormat = CoTaskMemAlloc(pamt->cbFormat);
400 if (!pamt->pbFormat)
401 return E_OUTOFMEMORY;
402 ZeroMemory(pamt->pbFormat, pamt->cbFormat);
403 format = (WAVEFORMATEX*)pamt->pbFormat;
405 format->wFormatTag = ((layer == 3) ? WAVE_FORMAT_MPEGLAYER3 :
406 WAVE_FORMAT_MPEG);
407 format->nChannels = ((mode == 3) ? 1 : 2);
408 format->nSamplesPerSec = freqs[freq_index];
409 format->nAvgBytesPerSec = tabsel_123[lsf][layer-1][bitrate_index] * 1000 / 8;
411 if (layer == 3)
412 format->nBlockAlign = format->nAvgBytesPerSec * 8 * 144 /
413 (format->nSamplesPerSec<<lsf) + 1;
414 else if (layer == 2)
415 format->nBlockAlign = format->nAvgBytesPerSec * 8 * 144 /
416 format->nSamplesPerSec + 1;
417 else
418 format->nBlockAlign = 4 * (format->nAvgBytesPerSec * 8 * 12 / format->nSamplesPerSec + 1);
420 format->wBitsPerSample = 0;
422 if (layer == 3)
424 MPEGLAYER3WAVEFORMAT *mp3format = (MPEGLAYER3WAVEFORMAT*)format;
426 format->cbSize = MPEGLAYER3_WFX_EXTRA_BYTES;
428 mp3format->wID = MPEGLAYER3_ID_MPEG;
429 mp3format->fdwFlags = MPEGLAYER3_FLAG_PADDING_ON;
430 mp3format->nBlockSize = format->nBlockAlign;
431 mp3format->nFramesPerBlock = 1;
433 /* Beware the evil magic numbers. This struct is apparently horribly
434 * under-documented, and the only references I could find had it being
435 * set to this with no real explanation. It works fine though, so I'm
436 * not complaining (yet).
438 mp3format->nCodecDelay = 1393;
440 else
442 MPEG1WAVEFORMAT *mpgformat = (MPEG1WAVEFORMAT*)format;
444 format->cbSize = 22;
446 mpgformat->fwHeadLayer = ((layer == 1) ? ACM_MPEG_LAYER1 :
447 ((layer == 2) ? ACM_MPEG_LAYER2 :
448 ACM_MPEG_LAYER3));
449 mpgformat->dwHeadBitrate = format->nAvgBytesPerSec * 8;
450 mpgformat->fwHeadMode = ((mode == 3) ? ACM_MPEG_SINGLECHANNEL :
451 ((mode == 2) ? ACM_MPEG_DUALCHANNEL :
452 ((mode == 1) ? ACM_MPEG_JOINTSTEREO :
453 ACM_MPEG_STEREO)));
454 mpgformat->fwHeadModeExt = ((mode == 1) ? 0x0F : (1<<mode_ext));
455 mpgformat->wHeadEmphasis = emphasis + 1;
456 mpgformat->fwHeadFlags = ACM_MPEG_ID_MPEG1;
458 pamt->subtype.Data1 = format->wFormatTag;
460 TRACE("MPEG audio stream detected:\n"
461 "\tLayer %d (%#x)\n"
462 "\tFrequency: %d\n"
463 "\tChannels: %d (%d)\n"
464 "\tBytesPerSec: %d\n",
465 layer, format->wFormatTag, format->nSamplesPerSec,
466 format->nChannels, mode, format->nAvgBytesPerSec);
468 dump_AM_MEDIA_TYPE(pamt);
470 return S_OK;
474 static HRESULT MPEGSplitter_pre_connect(IPin *iface, IPin *pConnectPin, ALLOCATOR_PROPERTIES *props)
476 PullPin *pPin = impl_PullPin_from_IPin(iface);
477 MPEGSplitterImpl *This = (MPEGSplitterImpl*)pPin->pin.pinInfo.pFilter;
478 HRESULT hr;
479 LONGLONG pos = 0; /* in bytes */
480 BYTE header[10];
481 int streamtype = 0;
482 LONGLONG total, avail;
483 AM_MEDIA_TYPE amt;
484 PIN_INFO piOutput;
486 IAsyncReader_Length(pPin->pReader, &total, &avail);
487 This->EndOfFile = total;
489 hr = IAsyncReader_SyncRead(pPin->pReader, pos, 4, header);
490 if (SUCCEEDED(hr))
491 pos += 4;
493 /* Skip ID3 v2 tag, if any */
494 if (SUCCEEDED(hr) && !memcmp("ID3", header, 3))
495 do {
496 UINT length = 0;
497 hr = IAsyncReader_SyncRead(pPin->pReader, pos, 6, header + 4);
498 if (FAILED(hr))
499 break;
500 pos += 6;
501 TRACE("Found ID3 v2.%d.%d\n", header[3], header[4]);
502 if(header[3] <= 4 && header[4] != 0xff &&
503 (header[5]&0x0f) == 0 && (header[6]&0x80) == 0 &&
504 (header[7]&0x80) == 0 && (header[8]&0x80) == 0 &&
505 (header[9]&0x80) == 0)
507 length = (header[6]<<21) | (header[7]<<14) |
508 (header[8]<< 7) | (header[9] );
509 if((header[5]&0x10))
510 length += 10;
511 TRACE("Length: %u\n", length);
513 pos += length;
515 /* Read the real header for the mpeg splitter */
516 hr = IAsyncReader_SyncRead(pPin->pReader, pos, 4, header);
517 if (SUCCEEDED(hr))
518 pos += 4;
519 TRACE("%x:%x:%x:%x\n", header[0], header[1], header[2], header[3]);
520 } while (0);
522 while(SUCCEEDED(hr) && !(streamtype=MPEGSplitter_head_check(header)))
524 TRACE("%x:%x:%x:%x\n", header[0], header[1], header[2], header[3]);
525 /* No valid header yet; shift by a byte and check again */
526 memmove(header, header+1, 3);
527 hr = IAsyncReader_SyncRead(pPin->pReader, pos++, 1, header + 3);
529 if (FAILED(hr))
530 return hr;
531 pos -= 4;
532 This->begin_offset = pos;
533 memcpy(This->header, header, 4);
535 switch(streamtype)
537 case MPEG_AUDIO_HEADER:
539 LONGLONG duration = 0;
540 WAVEFORMATEX *format;
542 hr = MPEGSplitter_init_audio(This, header, &piOutput, &amt);
543 if (SUCCEEDED(hr))
545 format = (WAVEFORMATEX*)amt.pbFormat;
547 props->cbAlign = 1;
548 props->cbPrefix = 0;
549 /* Make the output buffer a multiple of the frame size */
550 props->cbBuffer = 0x4000 / format->nBlockAlign *
551 format->nBlockAlign;
552 props->cBuffers = 3;
553 hr = Parser_AddPin(&(This->Parser), &piOutput, props, &amt);
556 if (FAILED(hr))
558 if (amt.pbFormat)
559 CoTaskMemFree(amt.pbFormat);
560 ERR("Could not create pin for MPEG audio stream (%x)\n", hr);
561 break;
564 /* Check for idv1 tag, and remove it from stream if found */
565 hr = IAsyncReader_SyncRead(pPin->pReader, This->EndOfFile-128, 3, header);
566 if (FAILED(hr))
567 break;
568 if (!strncmp((char*)header, "TAG", 3))
569 This->EndOfFile -= 128;
570 This->Parser.pInputPin->rtStop = MEDIATIME_FROM_BYTES(This->EndOfFile);
571 This->Parser.pInputPin->rtStart = This->Parser.pInputPin->rtCurrent = MEDIATIME_FROM_BYTES(This->begin_offset);
573 duration = (This->EndOfFile-This->begin_offset) * 10000000 / format->nAvgBytesPerSec;
574 TRACE("Duration: %d seconds\n", (DWORD)(duration / 10000000));
576 This->Parser.sourceSeeking.llCurrent = 0;
577 This->Parser.sourceSeeking.llDuration = duration;
578 This->Parser.sourceSeeking.llStop = duration;
579 break;
581 case MPEG_VIDEO_HEADER:
582 FIXME("MPEG video processing not yet supported!\n");
583 hr = E_FAIL;
584 break;
585 case MPEG_SYSTEM_HEADER:
586 FIXME("MPEG system streams not yet supported!\n");
587 hr = E_FAIL;
588 break;
590 default:
591 break;
593 This->position = 0;
595 return hr;
598 static HRESULT MPEGSplitter_cleanup(LPVOID iface)
600 MPEGSplitterImpl *This = iface;
602 TRACE("(%p)\n", This);
604 return S_OK;
607 static HRESULT WINAPI MPEGSplitter_seek(IMediaSeeking *iface)
609 MPEGSplitterImpl *This = impl_from_IMediaSeeking(iface);
610 PullPin *pPin = This->Parser.pInputPin;
611 LONGLONG newpos, timepos, bytepos;
612 HRESULT hr = E_INVALIDARG;
613 BYTE header[4];
615 newpos = This->Parser.sourceSeeking.llCurrent;
616 if (This->position/1000000 == newpos/1000000)
618 TRACE("Requesting position %x%08x same as current position %x%08x\n", (DWORD)(newpos>>32), (DWORD)newpos, (DWORD)(This->position>>32), (DWORD)This->position);
619 return S_OK;
622 bytepos = This->begin_offset;
623 timepos = 0;
624 /* http://mpgedit.org/mpgedit/mpeg_format/mpeghdr.htm has a whole read up on audio headers */
625 while (bytepos + 3 < This->EndOfFile)
627 LONGLONG duration = timepos;
628 LONGLONG length = 0;
629 hr = IAsyncReader_SyncRead(pPin->pReader, bytepos, 4, header);
630 if (hr != S_OK)
631 break;
632 while ((hr=parse_header(header, &length, &duration)) != S_OK &&
633 bytepos + 4 < This->EndOfFile)
635 /* No valid header yet; shift by a byte and check again */
636 memmove(header, header+1, 3);
637 hr = IAsyncReader_SyncRead(pPin->pReader, ++bytepos + 3, 1, header + 3);
638 if (hr != S_OK)
639 break;
641 if (hr != S_OK || duration > newpos)
642 break;
643 bytepos += length;
644 timepos = duration;
647 if (SUCCEEDED(hr))
649 PullPin *pin = This->Parser.pInputPin;
651 TRACE("Moving sound to %08u bytes!\n", (DWORD)bytepos);
653 EnterCriticalSection(&pin->thread_lock);
654 IPin_BeginFlush(&pin->pin.IPin_iface);
656 /* Make sure this is done while stopped, BeginFlush takes care of this */
657 EnterCriticalSection(&This->Parser.filter.csFilter);
658 memcpy(This->header, header, 4);
660 pin->rtStart = pin->rtCurrent = MEDIATIME_FROM_BYTES(bytepos);
661 pin->rtStop = MEDIATIME_FROM_BYTES((REFERENCE_TIME)This->EndOfFile);
662 This->seek = TRUE;
663 This->position = newpos;
664 LeaveCriticalSection(&This->Parser.filter.csFilter);
666 TRACE("Done flushing\n");
667 IPin_EndFlush(&pin->pin.IPin_iface);
668 LeaveCriticalSection(&pin->thread_lock);
670 return hr;
673 static HRESULT MPEGSplitter_disconnect(LPVOID iface)
675 /* TODO: Find memory leaks etc */
676 return S_OK;
679 static HRESULT MPEGSplitter_first_request(LPVOID iface)
681 MPEGSplitterImpl *This = iface;
682 PullPin *pin = This->Parser.pInputPin;
683 HRESULT hr;
684 LONGLONG length;
685 IMediaSample *sample;
687 TRACE("Seeking? %d\n", This->seek);
689 hr = parse_header(This->header, &length, NULL);
690 assert(hr == S_OK);
692 if (pin->rtCurrent >= pin->rtStop)
694 /* Last sample has already been queued, request nothing more */
695 FIXME("Done!\n");
696 return S_OK;
699 hr = IMemAllocator_GetBuffer(pin->pAlloc, &sample, NULL, NULL, 0);
701 pin->rtNext = pin->rtCurrent;
702 if (SUCCEEDED(hr))
704 LONGLONG rtSampleStart = pin->rtNext;
705 /* Add 4 for the next header, which should hopefully work */
706 LONGLONG rtSampleStop = rtSampleStart + MEDIATIME_FROM_BYTES(length + 4);
708 if (rtSampleStop > pin->rtStop)
709 rtSampleStop = MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(pin->rtStop), pin->cbAlign));
711 hr = IMediaSample_SetTime(sample, &rtSampleStart, &rtSampleStop);
713 IMediaSample_SetPreroll(sample, FALSE);
714 IMediaSample_SetDiscontinuity(sample, TRUE);
715 IMediaSample_SetSyncPoint(sample, 1);
716 This->seek = 0;
718 hr = IAsyncReader_Request(pin->pReader, sample, 0);
719 if (SUCCEEDED(hr))
721 pin->rtCurrent = pin->rtNext;
722 pin->rtNext = rtSampleStop;
724 else
725 IMediaSample_Release(sample);
727 if (FAILED(hr))
728 ERR("Horsemen of the apocalypse came to bring error 0x%08x\n", hr);
730 return hr;
733 static const IBaseFilterVtbl MPEGSplitter_Vtbl =
735 Parser_QueryInterface,
736 Parser_AddRef,
737 Parser_Release,
738 Parser_GetClassID,
739 Parser_Stop,
740 Parser_Pause,
741 Parser_Run,
742 Parser_GetState,
743 Parser_SetSyncSource,
744 Parser_GetSyncSource,
745 Parser_EnumPins,
746 Parser_FindPin,
747 Parser_QueryFilterInfo,
748 Parser_JoinFilterGraph,
749 Parser_QueryVendorInfo
752 HRESULT MPEGSplitter_create(IUnknown * pUnkOuter, LPVOID * ppv)
754 MPEGSplitterImpl *This;
755 HRESULT hr = E_FAIL;
757 TRACE("(%p, %p)\n", pUnkOuter, ppv);
759 *ppv = NULL;
761 if (pUnkOuter)
762 return CLASS_E_NOAGGREGATION;
764 This = CoTaskMemAlloc(sizeof(MPEGSplitterImpl));
765 if (!This)
766 return E_OUTOFMEMORY;
768 ZeroMemory(This, sizeof(MPEGSplitterImpl));
769 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);
770 if (FAILED(hr))
772 CoTaskMemFree(This);
773 return hr;
775 This->seek = 1;
777 /* Note: This memory is managed by the parser filter once created */
778 *ppv = This;
780 return hr;