d2d1: Add a pixel shader for triangle/solid/bitmap draws.
[wine.git] / dlls / quartz / mpegsplit.c
blob1fd80024d1ab2c339899bd7a3c7570313b29bacd
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 IAMStreamSelect IAMStreamSelect_iface;
59 LONGLONG EndOfFile;
60 LONGLONG position;
61 DWORD begin_offset;
62 BYTE header[4];
64 /* Whether we just seeked (or started playing) */
65 BOOL seek;
66 } MPEGSplitterImpl;
68 static inline MPEGSplitterImpl *impl_from_IBaseFilter( IBaseFilter *iface )
70 return CONTAINING_RECORD(iface, MPEGSplitterImpl, Parser.filter.IBaseFilter_iface);
73 static inline MPEGSplitterImpl *impl_from_IMediaSeeking( IMediaSeeking *iface )
75 return CONTAINING_RECORD(iface, MPEGSplitterImpl, Parser.sourceSeeking.IMediaSeeking_iface);
78 static inline MPEGSplitterImpl *impl_from_IAMStreamSelect( IAMStreamSelect *iface )
80 return CONTAINING_RECORD(iface, MPEGSplitterImpl, IAMStreamSelect_iface);
83 static int MPEGSplitter_head_check(const BYTE *header)
85 /* If this is a possible start code, check for a system or video header */
86 if (header[0] == 0 && header[1] == 0 && header[2] == 1)
88 /* Check if we got a system or elementary stream start code */
89 if (header[3] == PACK_START_CODE ||
90 header[3] == VIDEO_ELEMENTARY_STREAM ||
91 header[3] == AUDIO_ELEMENTARY_STREAM)
92 return MPEG_SYSTEM_HEADER;
94 /* Check for a MPEG video sequence start code */
95 if (header[3] == SEQUENCE_HEADER_CODE)
96 return MPEG_VIDEO_HEADER;
99 /* This should give a good guess if we have an MPEG audio header */
100 if(header[0] == 0xff && ((header[1]>>5)&0x7) == 0x7 &&
101 ((header[1]>>1)&0x3) != 0 && ((header[2]>>4)&0xf) != 0xf &&
102 ((header[2]>>2)&0x3) != 0x3)
103 return MPEG_AUDIO_HEADER;
105 /* Nothing yet.. */
106 return MPEG_NO_HEADER;
109 static const WCHAR wszAudioStream[] = {'A','u','d','i','o',0};
111 static const DWORD freqs[10] = { 44100, 48000, 32000, 22050, 24000, 16000, 11025, 12000, 8000, 0 };
113 static const DWORD tabsel_123[2][3][16] = {
114 { {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,},
115 {0,32,48,56, 64, 80, 96,112,128,160,192,224,256,320,384,},
116 {0,32,40,48, 56, 64, 80, 96,112,128,160,192,224,256,320,} },
118 { {0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,},
119 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,},
120 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,} }
123 static HRESULT parse_header(BYTE *header, LONGLONG *plen, LONGLONG *pduration)
125 int bitrate_index, freq_index, lsf = 1, mpeg1, layer, padding, bitrate, length;
126 LONGLONG duration;
128 if (MPEGSplitter_head_check(header) != MPEG_AUDIO_HEADER)
130 FIXME("Not a valid header: %02x:%02x:%02x:%02x\n", header[0], header[1], header[2], header[3]);
131 return E_INVALIDARG;
134 mpeg1 = (header[1]>>4)&0x1;
135 if (mpeg1)
136 lsf = ((header[1]>>3)&0x1)^1;
138 layer = 4-((header[1]>>1)&0x3);
139 bitrate_index = ((header[2]>>4)&0xf);
140 freq_index = ((header[2]>>2)&0x3) + (mpeg1?(lsf*3):6);
141 padding = ((header[2]>>1)&0x1);
143 bitrate = tabsel_123[lsf][layer-1][bitrate_index] * 1000;
144 if (!bitrate)
146 FIXME("Not a valid header: %02x:%02x:%02x:%02x\n", header[0], header[1], header[2], header[3]);
147 return E_INVALIDARG;
150 if (layer == 1)
151 length = 4 * (12 * bitrate / freqs[freq_index] + padding);
152 else if (layer == 2)
153 length = 144 * bitrate / freqs[freq_index] + padding;
154 else if (layer == 3)
155 length = 144 * bitrate / (freqs[freq_index]<<lsf) + padding;
156 else
158 ERR("Impossible layer %d\n", layer);
159 return E_INVALIDARG;
162 duration = (ULONGLONG)10000000 * (ULONGLONG)(length) / (ULONGLONG)(bitrate/8);
163 *plen = length;
164 if (pduration)
165 *pduration += duration;
166 return S_OK;
169 static HRESULT FillBuffer(MPEGSplitterImpl *This, IMediaSample *pCurrentSample)
171 Parser_OutputPin * pOutputPin = unsafe_impl_Parser_OutputPin_from_IPin(This->Parser.ppPins[1]);
172 LONGLONG length = 0;
173 LONGLONG pos = BYTES_FROM_MEDIATIME(This->Parser.pInputPin->rtNext);
174 LONGLONG time = This->position, rtstop, rtstart;
175 HRESULT hr;
176 BYTE *fbuf = NULL;
177 DWORD len = IMediaSample_GetActualDataLength(pCurrentSample);
179 TRACE("Source length: %u\n", len);
180 IMediaSample_GetPointer(pCurrentSample, &fbuf);
182 /* Find the next valid header.. it <SHOULD> be right here */
183 hr = parse_header(fbuf, &length, &This->position);
184 assert(hr == S_OK);
185 IMediaSample_SetActualDataLength(pCurrentSample, length);
187 /* Queue the next sample */
188 if (length + 4 == len)
190 PullPin *pin = This->Parser.pInputPin;
191 LONGLONG stop = BYTES_FROM_MEDIATIME(pin->rtStop);
193 hr = S_OK;
194 memcpy(This->header, fbuf + length, 4);
195 while (FAILED(hr = parse_header(This->header, &length, NULL)))
197 memmove(This->header, This->header+1, 3);
198 if (pos + 4 >= stop)
199 break;
200 IAsyncReader_SyncRead(pin->pReader, ++pos, 1, This->header + 3);
202 pin->rtNext = MEDIATIME_FROM_BYTES(pos);
204 if (SUCCEEDED(hr))
206 /* Remove 4 for the last header, which should hopefully work */
207 IMediaSample *sample = NULL;
208 LONGLONG rtSampleStart = pin->rtNext - MEDIATIME_FROM_BYTES(4);
209 LONGLONG rtSampleStop = rtSampleStart + MEDIATIME_FROM_BYTES(length + 4);
211 if (rtSampleStop > pin->rtStop)
212 rtSampleStop = MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(pin->rtStop), pin->cbAlign));
214 hr = IMemAllocator_GetBuffer(pin->pAlloc, &sample, NULL, NULL, 0);
215 if (SUCCEEDED(hr))
217 IMediaSample_SetTime(sample, &rtSampleStart, &rtSampleStop);
218 IMediaSample_SetPreroll(sample, FALSE);
219 IMediaSample_SetDiscontinuity(sample, FALSE);
220 IMediaSample_SetSyncPoint(sample, TRUE);
221 hr = IAsyncReader_Request(pin->pReader, sample, 0);
222 if (SUCCEEDED(hr))
224 pin->rtCurrent = rtSampleStart;
225 pin->rtNext = rtSampleStop;
227 else
228 IMediaSample_Release(sample);
230 if (FAILED(hr))
231 FIXME("o_Ox%08x\n", hr);
234 /* If not, we're presumably at the end of file */
236 TRACE("Media time : %u.%03u\n", (DWORD)(This->position/10000000), (DWORD)((This->position/10000)%1000));
238 if (IMediaSample_IsDiscontinuity(pCurrentSample) == S_OK) {
239 IPin *victim;
240 EnterCriticalSection(&This->Parser.filter.csFilter);
241 pOutputPin->pin.pin.tStart = time;
242 pOutputPin->pin.pin.dRate = This->Parser.sourceSeeking.dRate;
243 hr = IPin_ConnectedTo(&pOutputPin->pin.pin.IPin_iface, &victim);
244 if (hr == S_OK)
246 hr = IPin_NewSegment(victim, time, This->Parser.sourceSeeking.llStop,
247 This->Parser.sourceSeeking.dRate);
248 if (hr != S_OK)
249 FIXME("NewSegment returns %08x\n", hr);
250 IPin_Release(victim);
252 LeaveCriticalSection(&This->Parser.filter.csFilter);
253 if (hr != S_OK)
254 return hr;
256 rtstart = (double)(time - pOutputPin->pin.pin.tStart) / pOutputPin->pin.pin.dRate;
257 rtstop = (double)(This->position - pOutputPin->pin.pin.tStart) / pOutputPin->pin.pin.dRate;
258 IMediaSample_SetTime(pCurrentSample, &rtstart, &rtstop);
259 IMediaSample_SetMediaTime(pCurrentSample, &time, &This->position);
261 hr = BaseOutputPinImpl_Deliver(&pOutputPin->pin, pCurrentSample);
263 if (hr != S_OK)
265 if (hr != S_FALSE)
266 TRACE("Error sending sample (%x)\n", hr);
267 else
268 TRACE("S_FALSE (%d), holding\n", IMediaSample_GetActualDataLength(pCurrentSample));
271 return hr;
275 static HRESULT MPEGSplitter_process_sample(LPVOID iface, IMediaSample * pSample, DWORD_PTR cookie)
277 MPEGSplitterImpl *This = iface;
278 BYTE *pbSrcStream;
279 DWORD cbSrcStream = 0;
280 REFERENCE_TIME tStart, tStop, tAviStart = This->position;
281 HRESULT hr;
283 hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
284 if (SUCCEEDED(hr))
286 cbSrcStream = IMediaSample_GetActualDataLength(pSample);
287 hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
290 /* Flush occurring */
291 if (cbSrcStream == 0)
293 FIXME(".. Why do I need you?\n");
294 return S_OK;
297 /* trace removed for performance reasons */
298 /* TRACE("(%p), %llu -> %llu\n", pSample, tStart, tStop); */
300 /* Now, try to find a new header */
301 hr = FillBuffer(This, pSample);
302 if (hr != S_OK)
304 WARN("Failed with hres: %08x!\n", hr);
306 /* Unset progression if denied! */
307 if (hr == VFW_E_WRONG_STATE || hr == S_FALSE)
309 memcpy(This->header, pbSrcStream, 4);
310 This->Parser.pInputPin->rtCurrent = tStart;
311 This->position = tAviStart;
315 if (BYTES_FROM_MEDIATIME(tStop) >= This->EndOfFile || This->position >= This->Parser.sourceSeeking.llStop)
317 unsigned int i;
319 TRACE("End of file reached\n");
321 for (i = 0; i < This->Parser.cStreams; i++)
323 IPin* ppin;
325 hr = IPin_ConnectedTo(This->Parser.ppPins[i+1], &ppin);
326 if (SUCCEEDED(hr))
328 hr = IPin_EndOfStream(ppin);
329 IPin_Release(ppin);
331 if (FAILED(hr))
332 WARN("Error sending EndOfStream to pin %u (%x)\n", i, hr);
335 /* Force the pullpin thread to stop */
336 hr = S_FALSE;
339 return hr;
343 static HRESULT MPEGSplitter_query_accept(LPVOID iface, const AM_MEDIA_TYPE *pmt)
345 if (!IsEqualIID(&pmt->majortype, &MEDIATYPE_Stream))
346 return S_FALSE;
348 if (IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_MPEG1Audio))
349 return S_OK;
351 if (IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_MPEG1Video))
352 FIXME("MPEG-1 video streams not yet supported.\n");
353 else if (IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_MPEG1System))
354 FIXME("MPEG-1 system streams not yet supported.\n");
355 else if (IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_MPEG1VideoCD))
356 FIXME("MPEG-1 VideoCD streams not yet supported.\n");
357 else FIXME("%s\n", debugstr_guid(&pmt->subtype));
359 return S_FALSE;
363 static HRESULT MPEGSplitter_init_audio(MPEGSplitterImpl *This, const BYTE *header, PIN_INFO *ppiOutput, AM_MEDIA_TYPE *pamt)
365 WAVEFORMATEX *format;
366 int bitrate_index;
367 int freq_index;
368 int mode_ext;
369 int emphasis;
370 int lsf = 1;
371 int mpeg1;
372 int layer;
373 int mode;
375 ZeroMemory(pamt, sizeof(*pamt));
376 ppiOutput->dir = PINDIR_OUTPUT;
377 ppiOutput->pFilter = (IBaseFilter*)This;
378 wsprintfW(ppiOutput->achName, wszAudioStream);
380 pamt->formattype = FORMAT_WaveFormatEx;
381 pamt->majortype = MEDIATYPE_Audio;
382 pamt->subtype = MEDIASUBTYPE_MPEG1AudioPayload;
384 pamt->lSampleSize = 0;
385 pamt->bFixedSizeSamples = FALSE;
386 pamt->bTemporalCompression = 0;
388 mpeg1 = (header[1]>>4)&0x1;
389 if (mpeg1)
390 lsf = ((header[1]>>3)&0x1)^1;
392 layer = 4-((header[1]>>1)&0x3);
393 bitrate_index = ((header[2]>>4)&0xf);
394 freq_index = ((header[2]>>2)&0x3) + (mpeg1?(lsf*3):6);
395 mode = ((header[3]>>6)&0x3);
396 mode_ext = ((header[3]>>4)&0x3);
397 emphasis = ((header[3]>>0)&0x3);
399 if (!bitrate_index)
401 /* Set to highest bitrate so samples will fit in for sure */
402 FIXME("Variable-bitrate audio not fully supported.\n");
403 bitrate_index = 15;
406 pamt->cbFormat = ((layer==3)? sizeof(MPEGLAYER3WAVEFORMAT) :
407 sizeof(MPEG1WAVEFORMAT));
408 pamt->pbFormat = CoTaskMemAlloc(pamt->cbFormat);
409 if (!pamt->pbFormat)
410 return E_OUTOFMEMORY;
411 ZeroMemory(pamt->pbFormat, pamt->cbFormat);
412 format = (WAVEFORMATEX*)pamt->pbFormat;
414 format->wFormatTag = ((layer == 3) ? WAVE_FORMAT_MPEGLAYER3 :
415 WAVE_FORMAT_MPEG);
416 format->nChannels = ((mode == 3) ? 1 : 2);
417 format->nSamplesPerSec = freqs[freq_index];
418 format->nAvgBytesPerSec = tabsel_123[lsf][layer-1][bitrate_index] * 1000 / 8;
420 if (layer == 3)
421 format->nBlockAlign = format->nAvgBytesPerSec * 8 * 144 /
422 (format->nSamplesPerSec<<lsf) + 1;
423 else if (layer == 2)
424 format->nBlockAlign = format->nAvgBytesPerSec * 8 * 144 /
425 format->nSamplesPerSec + 1;
426 else
427 format->nBlockAlign = 4 * (format->nAvgBytesPerSec * 8 * 12 / format->nSamplesPerSec + 1);
429 format->wBitsPerSample = 0;
431 if (layer == 3)
433 MPEGLAYER3WAVEFORMAT *mp3format = (MPEGLAYER3WAVEFORMAT*)format;
435 format->cbSize = MPEGLAYER3_WFX_EXTRA_BYTES;
437 mp3format->wID = MPEGLAYER3_ID_MPEG;
438 mp3format->fdwFlags = MPEGLAYER3_FLAG_PADDING_ON;
439 mp3format->nBlockSize = format->nBlockAlign;
440 mp3format->nFramesPerBlock = 1;
442 /* Beware the evil magic numbers. This struct is apparently horribly
443 * under-documented, and the only references I could find had it being
444 * set to this with no real explanation. It works fine though, so I'm
445 * not complaining (yet).
447 mp3format->nCodecDelay = 1393;
449 else
451 MPEG1WAVEFORMAT *mpgformat = (MPEG1WAVEFORMAT*)format;
453 format->cbSize = 22;
455 mpgformat->fwHeadLayer = ((layer == 1) ? ACM_MPEG_LAYER1 :
456 ((layer == 2) ? ACM_MPEG_LAYER2 :
457 ACM_MPEG_LAYER3));
458 mpgformat->dwHeadBitrate = format->nAvgBytesPerSec * 8;
459 mpgformat->fwHeadMode = ((mode == 3) ? ACM_MPEG_SINGLECHANNEL :
460 ((mode == 2) ? ACM_MPEG_DUALCHANNEL :
461 ((mode == 1) ? ACM_MPEG_JOINTSTEREO :
462 ACM_MPEG_STEREO)));
463 mpgformat->fwHeadModeExt = ((mode == 1) ? 0x0F : (1<<mode_ext));
464 mpgformat->wHeadEmphasis = emphasis + 1;
465 mpgformat->fwHeadFlags = ACM_MPEG_ID_MPEG1;
467 pamt->subtype.Data1 = format->wFormatTag;
469 TRACE("MPEG audio stream detected:\n"
470 "\tLayer %d (%#x)\n"
471 "\tFrequency: %d\n"
472 "\tChannels: %d (%d)\n"
473 "\tBytesPerSec: %d\n",
474 layer, format->wFormatTag, format->nSamplesPerSec,
475 format->nChannels, mode, format->nAvgBytesPerSec);
477 dump_AM_MEDIA_TYPE(pamt);
479 return S_OK;
483 static HRESULT MPEGSplitter_pre_connect(IPin *iface, IPin *pConnectPin, ALLOCATOR_PROPERTIES *props)
485 PullPin *pPin = impl_PullPin_from_IPin(iface);
486 MPEGSplitterImpl *This = (MPEGSplitterImpl*)pPin->pin.pinInfo.pFilter;
487 HRESULT hr;
488 LONGLONG pos = 0; /* in bytes */
489 BYTE header[10];
490 int streamtype;
491 LONGLONG total, avail;
492 AM_MEDIA_TYPE amt;
493 PIN_INFO piOutput;
495 IAsyncReader_Length(pPin->pReader, &total, &avail);
496 This->EndOfFile = total;
498 hr = IAsyncReader_SyncRead(pPin->pReader, pos, 4, header);
499 if (SUCCEEDED(hr))
500 pos += 4;
502 /* Skip ID3 v2 tag, if any */
503 if (SUCCEEDED(hr) && !memcmp("ID3", header, 3))
504 do {
505 UINT length = 0;
506 hr = IAsyncReader_SyncRead(pPin->pReader, pos, 6, header + 4);
507 if (FAILED(hr))
508 break;
509 pos += 6;
510 TRACE("Found ID3 v2.%d.%d\n", header[3], header[4]);
511 if(header[3] <= 4 && header[4] != 0xff &&
512 (header[5]&0x0f) == 0 && (header[6]&0x80) == 0 &&
513 (header[7]&0x80) == 0 && (header[8]&0x80) == 0 &&
514 (header[9]&0x80) == 0)
516 length = (header[6]<<21) | (header[7]<<14) |
517 (header[8]<< 7) | (header[9] );
518 if((header[5]&0x10))
519 length += 10;
520 TRACE("Length: %u\n", length);
522 pos += length;
524 /* Read the real header for the mpeg splitter */
525 hr = IAsyncReader_SyncRead(pPin->pReader, pos, 4, header);
526 if (SUCCEEDED(hr))
527 pos += 4;
528 } while (0);
530 while(SUCCEEDED(hr))
532 TRACE("Testing header %x:%x:%x:%x\n", header[0], header[1], header[2], header[3]);
534 streamtype = MPEGSplitter_head_check(header);
535 if (streamtype == MPEG_AUDIO_HEADER)
537 LONGLONG length;
538 if (parse_header(header, &length, NULL) == S_OK)
540 BYTE next_header[4];
541 /* Ensure we have a valid header by seeking for the next frame, some bad
542 * encoded ID3v2 may have an incorrect length and we end up finding bytes
543 * like FF FE 00 28 which are nothing more than a Unicode BOM followed by
544 * ')' character from inside a ID3v2 tag. Unfortunately that sequence
545 * matches like a valid mpeg audio header.
547 hr = IAsyncReader_SyncRead(pPin->pReader, pos + length - 4, 4, next_header);
548 if (FAILED(hr))
549 break;
550 if (parse_header(next_header, &length, NULL) == S_OK)
551 break;
552 TRACE("%x:%x:%x:%x is a fake audio header, looking for next...\n",
553 header[0], header[1], header[2], header[3]);
556 else if (streamtype) /* Video or System stream */
557 break;
559 /* No valid header yet; shift by a byte and check again */
560 memmove(header, header+1, 3);
561 hr = IAsyncReader_SyncRead(pPin->pReader, pos++, 1, header + 3);
563 if (FAILED(hr))
564 return hr;
565 pos -= 4;
566 This->begin_offset = pos;
567 memcpy(This->header, header, 4);
569 switch(streamtype)
571 case MPEG_AUDIO_HEADER:
573 LONGLONG duration = 0;
574 WAVEFORMATEX *format;
576 hr = MPEGSplitter_init_audio(This, header, &piOutput, &amt);
577 if (SUCCEEDED(hr))
579 format = (WAVEFORMATEX*)amt.pbFormat;
581 props->cbAlign = 1;
582 props->cbPrefix = 0;
583 /* Make the output buffer a multiple of the frame size */
584 props->cbBuffer = 0x4000 / format->nBlockAlign *
585 format->nBlockAlign;
586 props->cBuffers = 3;
587 hr = Parser_AddPin(&(This->Parser), &piOutput, props, &amt);
590 if (FAILED(hr))
592 if (amt.pbFormat)
593 CoTaskMemFree(amt.pbFormat);
594 ERR("Could not create pin for MPEG audio stream (%x)\n", hr);
595 break;
598 /* Check for idv1 tag, and remove it from stream if found */
599 hr = IAsyncReader_SyncRead(pPin->pReader, This->EndOfFile-128, 3, header);
600 if (FAILED(hr))
601 break;
602 if (!strncmp((char*)header, "TAG", 3))
603 This->EndOfFile -= 128;
604 This->Parser.pInputPin->rtStop = MEDIATIME_FROM_BYTES(This->EndOfFile);
605 This->Parser.pInputPin->rtStart = This->Parser.pInputPin->rtCurrent = MEDIATIME_FROM_BYTES(This->begin_offset);
607 duration = (This->EndOfFile-This->begin_offset) * 10000000 / format->nAvgBytesPerSec;
608 TRACE("Duration: %d seconds\n", (DWORD)(duration / 10000000));
610 This->Parser.sourceSeeking.llCurrent = 0;
611 This->Parser.sourceSeeking.llDuration = duration;
612 This->Parser.sourceSeeking.llStop = duration;
613 break;
615 case MPEG_VIDEO_HEADER:
616 FIXME("MPEG video processing not yet supported!\n");
617 hr = E_FAIL;
618 break;
619 case MPEG_SYSTEM_HEADER:
620 FIXME("MPEG system streams not yet supported!\n");
621 hr = E_FAIL;
622 break;
624 default:
625 break;
627 This->position = 0;
629 return hr;
632 static HRESULT MPEGSplitter_cleanup(LPVOID iface)
634 MPEGSplitterImpl *This = iface;
636 TRACE("(%p)\n", This);
638 return S_OK;
641 static HRESULT WINAPI MPEGSplitter_seek(IMediaSeeking *iface)
643 MPEGSplitterImpl *This = impl_from_IMediaSeeking(iface);
644 PullPin *pPin = This->Parser.pInputPin;
645 LONGLONG newpos, timepos, bytepos;
646 HRESULT hr = E_INVALIDARG;
647 BYTE header[4];
649 newpos = This->Parser.sourceSeeking.llCurrent;
650 if (This->position/1000000 == newpos/1000000)
652 TRACE("Requesting position %x%08x same as current position %x%08x\n", (DWORD)(newpos>>32), (DWORD)newpos, (DWORD)(This->position>>32), (DWORD)This->position);
653 return S_OK;
656 bytepos = This->begin_offset;
657 timepos = 0;
658 /* http://mpgedit.org/mpgedit/mpeg_format/mpeghdr.htm has a whole read up on audio headers */
659 while (bytepos + 3 < This->EndOfFile)
661 LONGLONG duration = timepos;
662 LONGLONG length = 0;
663 hr = IAsyncReader_SyncRead(pPin->pReader, bytepos, 4, header);
664 if (hr != S_OK)
665 break;
666 while ((hr=parse_header(header, &length, &duration)) != S_OK &&
667 bytepos + 4 < This->EndOfFile)
669 /* No valid header yet; shift by a byte and check again */
670 memmove(header, header+1, 3);
671 hr = IAsyncReader_SyncRead(pPin->pReader, ++bytepos + 3, 1, header + 3);
672 if (hr != S_OK)
673 break;
675 if (hr != S_OK || duration > newpos)
676 break;
677 bytepos += length;
678 timepos = duration;
681 if (SUCCEEDED(hr))
683 PullPin *pin = This->Parser.pInputPin;
685 TRACE("Moving sound to %08u bytes!\n", (DWORD)bytepos);
687 EnterCriticalSection(&pin->thread_lock);
688 IPin_BeginFlush(&pin->pin.IPin_iface);
690 /* Make sure this is done while stopped, BeginFlush takes care of this */
691 EnterCriticalSection(&This->Parser.filter.csFilter);
692 memcpy(This->header, header, 4);
694 pin->rtStart = pin->rtCurrent = MEDIATIME_FROM_BYTES(bytepos);
695 pin->rtStop = MEDIATIME_FROM_BYTES((REFERENCE_TIME)This->EndOfFile);
696 This->seek = TRUE;
697 This->position = newpos;
698 LeaveCriticalSection(&This->Parser.filter.csFilter);
700 TRACE("Done flushing\n");
701 IPin_EndFlush(&pin->pin.IPin_iface);
702 LeaveCriticalSection(&pin->thread_lock);
704 return hr;
707 static HRESULT MPEGSplitter_disconnect(LPVOID iface)
709 /* TODO: Find memory leaks etc */
710 return S_OK;
713 static HRESULT MPEGSplitter_first_request(LPVOID iface)
715 MPEGSplitterImpl *This = iface;
716 PullPin *pin = This->Parser.pInputPin;
717 HRESULT hr;
718 LONGLONG length;
719 IMediaSample *sample;
721 TRACE("Seeking? %d\n", This->seek);
723 hr = parse_header(This->header, &length, NULL);
724 assert(hr == S_OK);
726 if (pin->rtCurrent >= pin->rtStop)
728 /* Last sample has already been queued, request nothing more */
729 FIXME("Done!\n");
730 return S_OK;
733 hr = IMemAllocator_GetBuffer(pin->pAlloc, &sample, NULL, NULL, 0);
735 pin->rtNext = pin->rtCurrent;
736 if (SUCCEEDED(hr))
738 LONGLONG rtSampleStart = pin->rtNext;
739 /* Add 4 for the next header, which should hopefully work */
740 LONGLONG rtSampleStop = rtSampleStart + MEDIATIME_FROM_BYTES(length + 4);
742 if (rtSampleStop > pin->rtStop)
743 rtSampleStop = MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(pin->rtStop), pin->cbAlign));
745 IMediaSample_SetTime(sample, &rtSampleStart, &rtSampleStop);
746 IMediaSample_SetPreroll(sample, FALSE);
747 IMediaSample_SetDiscontinuity(sample, TRUE);
748 IMediaSample_SetSyncPoint(sample, 1);
749 This->seek = FALSE;
751 hr = IAsyncReader_Request(pin->pReader, sample, 0);
752 if (SUCCEEDED(hr))
754 pin->rtCurrent = pin->rtNext;
755 pin->rtNext = rtSampleStop;
757 else
758 IMediaSample_Release(sample);
760 if (FAILED(hr))
761 ERR("Horsemen of the apocalypse came to bring error 0x%08x\n", hr);
763 return hr;
766 static HRESULT WINAPI MPEGSplitter_QueryInterface(IBaseFilter *iface, REFIID riid, void **ppv)
768 MPEGSplitterImpl *This = impl_from_IBaseFilter(iface);
769 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
771 *ppv = NULL;
773 if ( IsEqualIID(riid, &IID_IUnknown)
774 || IsEqualIID(riid, &IID_IPersist)
775 || IsEqualIID(riid, &IID_IMediaFilter)
776 || IsEqualIID(riid, &IID_IBaseFilter) )
777 *ppv = iface;
778 else if ( IsEqualIID(riid, &IID_IAMStreamSelect) )
779 *ppv = &This->IAMStreamSelect_iface;
781 if (*ppv)
783 IBaseFilter_AddRef(iface);
784 return S_OK;
787 if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IVideoWindow))
788 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
790 return E_NOINTERFACE;
793 static const IBaseFilterVtbl MPEGSplitter_Vtbl =
795 MPEGSplitter_QueryInterface,
796 Parser_AddRef,
797 Parser_Release,
798 Parser_GetClassID,
799 Parser_Stop,
800 Parser_Pause,
801 Parser_Run,
802 Parser_GetState,
803 Parser_SetSyncSource,
804 Parser_GetSyncSource,
805 Parser_EnumPins,
806 Parser_FindPin,
807 Parser_QueryFilterInfo,
808 Parser_JoinFilterGraph,
809 Parser_QueryVendorInfo
812 static HRESULT WINAPI AMStreamSelect_QueryInterface(IAMStreamSelect *iface, REFIID riid, void **ppv)
814 MPEGSplitterImpl *This = impl_from_IAMStreamSelect(iface);
816 return IBaseFilter_QueryInterface(&This->Parser.filter.IBaseFilter_iface, riid, ppv);
819 static ULONG WINAPI AMStreamSelect_AddRef(IAMStreamSelect *iface)
821 MPEGSplitterImpl *This = impl_from_IAMStreamSelect(iface);
823 return IBaseFilter_AddRef(&This->Parser.filter.IBaseFilter_iface);
826 static ULONG WINAPI AMStreamSelect_Release(IAMStreamSelect *iface)
828 MPEGSplitterImpl *This = impl_from_IAMStreamSelect(iface);
830 return IBaseFilter_Release(&This->Parser.filter.IBaseFilter_iface);
833 static HRESULT WINAPI AMStreamSelect_Count(IAMStreamSelect *iface, DWORD *streams)
835 MPEGSplitterImpl *This = impl_from_IAMStreamSelect(iface);
837 FIXME("(%p/%p)->(%p) stub!\n", This, iface, streams);
839 return E_NOTIMPL;
842 static HRESULT WINAPI AMStreamSelect_Info(IAMStreamSelect *iface, LONG index, AM_MEDIA_TYPE **media_type, DWORD *flags, LCID *lcid, DWORD *group, WCHAR **name, IUnknown **object, IUnknown **unknown)
844 MPEGSplitterImpl *This = impl_from_IAMStreamSelect(iface);
846 FIXME("(%p/%p)->(%d,%p,%p,%p,%p,%p,%p,%p) stub!\n", This, iface, index, media_type, flags, lcid, group, name, object, unknown);
848 return E_NOTIMPL;
851 static HRESULT WINAPI AMStreamSelect_Enable(IAMStreamSelect *iface, LONG index, DWORD flags)
853 MPEGSplitterImpl *This = impl_from_IAMStreamSelect(iface);
855 FIXME("(%p/%p)->(%d,%x) stub!\n", This, iface, index, flags);
857 return E_NOTIMPL;
860 static const IAMStreamSelectVtbl AMStreamSelectVtbl =
862 AMStreamSelect_QueryInterface,
863 AMStreamSelect_AddRef,
864 AMStreamSelect_Release,
865 AMStreamSelect_Count,
866 AMStreamSelect_Info,
867 AMStreamSelect_Enable
870 HRESULT MPEGSplitter_create(IUnknown * pUnkOuter, LPVOID * ppv)
872 MPEGSplitterImpl *This;
873 HRESULT hr = E_FAIL;
875 TRACE("(%p, %p)\n", pUnkOuter, ppv);
877 *ppv = NULL;
879 if (pUnkOuter)
880 return CLASS_E_NOAGGREGATION;
882 This = CoTaskMemAlloc(sizeof(MPEGSplitterImpl));
883 if (!This)
884 return E_OUTOFMEMORY;
886 ZeroMemory(This, sizeof(MPEGSplitterImpl));
887 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);
888 if (FAILED(hr))
890 CoTaskMemFree(This);
891 return hr;
893 This->IAMStreamSelect_iface.lpVtbl = &AMStreamSelectVtbl;
894 This->seek = TRUE;
896 /* Note: This memory is managed by the parser filter once created */
897 *ppv = &This->Parser.filter.IBaseFilter_iface;
899 return hr;