strmbase: Move the MediaSeekingPassThru functions from quartz into strmbase.
[wine/multimedia.git] / dlls / quartz / mpegsplit.c
blobacb72edb3e41503b61f6da8ad1d39367f2e9526c
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 #define SEEK_INTERVAL (ULONGLONG)(10 * 10000000) /* Add an entry every 10 seconds */
57 struct seek_entry {
58 ULONGLONG bytepos;
59 ULONGLONG timepos;
62 typedef struct MPEGSplitterImpl
64 ParserImpl Parser;
65 LONGLONG EndOfFile;
66 LONGLONG duration;
67 LONGLONG position;
68 DWORD begin_offset;
69 BYTE header[4];
71 /* Whether we just seeked (or started playing) */
72 BOOL seek;
74 /* Seeking cache */
75 ULONG seek_entries;
76 struct seek_entry *seektable;
77 } MPEGSplitterImpl;
79 static inline MPEGSplitterImpl *impl_from_IMediaSeeking( IMediaSeeking *iface )
81 return (MPEGSplitterImpl *)((char*)iface - FIELD_OFFSET(MPEGSplitterImpl, Parser.sourceSeeking.lpVtbl));
84 static int MPEGSplitter_head_check(const BYTE *header)
86 /* If this is a possible start code, check for a system or video header */
87 if (header[0] == 0 && header[1] == 0 && header[2] == 1)
89 /* Check if we got a system or elementary stream start code */
90 if (header[3] == PACK_START_CODE ||
91 header[3] == VIDEO_ELEMENTARY_STREAM ||
92 header[3] == AUDIO_ELEMENTARY_STREAM)
93 return MPEG_SYSTEM_HEADER;
95 /* Check for a MPEG video sequence start code */
96 if (header[3] == SEQUENCE_HEADER_CODE)
97 return MPEG_VIDEO_HEADER;
100 /* This should give a good guess if we have an MPEG audio header */
101 if(header[0] == 0xff && ((header[1]>>5)&0x7) == 0x7 &&
102 ((header[1]>>1)&0x3) != 0 && ((header[2]>>4)&0xf) != 0xf &&
103 ((header[2]>>2)&0x3) != 0x3)
104 return MPEG_AUDIO_HEADER;
106 /* Nothing yet.. */
107 return MPEG_NO_HEADER;
110 static const WCHAR wszAudioStream[] = {'A','u','d','i','o',0};
111 static const WCHAR wszVideoStream[] = {'V','i','d','e','o',0};
113 static const DWORD freqs[10] = { 44100, 48000, 32000, 22050, 24000, 16000, 11025, 12000, 8000, 0 };
115 static const DWORD tabsel_123[2][3][16] = {
116 { {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,},
117 {0,32,48,56, 64, 80, 96,112,128,160,192,224,256,320,384,},
118 {0,32,40,48, 56, 64, 80, 96,112,128,160,192,224,256,320,} },
120 { {0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,},
121 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,},
122 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,} }
125 static HRESULT parse_header(BYTE *header, LONGLONG *plen, LONGLONG *pduration)
127 LONGLONG duration;
129 int bitrate_index, freq_index, lsf = 1, mpeg1, layer, padding, bitrate, length;
130 if (header[0] != 0xff)
131 return E_INVALIDARG;
133 if (!(((header[1]>>5)&0x7) == 0x7 &&
134 ((header[1]>>1)&0x3) != 0 && ((header[2]>>4)&0xf) != 0xf &&
135 ((header[2]>>2)&0x3) != 0x3))
137 FIXME("Not a valid header: %02x:%02x\n", header[1], header[2]);
138 return E_INVALIDARG;
141 mpeg1 = (header[1]>>4)&0x1;
142 if (mpeg1)
143 lsf = ((header[1]>>3)&0x1)^1;
145 layer = 4-((header[1]>>1)&0x3);
146 bitrate_index = ((header[2]>>4)&0xf);
147 freq_index = ((header[2]>>2)&0x3) + (mpeg1?(lsf*3):6);
148 padding = ((header[2]>>1)&0x1);
150 bitrate = tabsel_123[lsf][layer-1][bitrate_index] * 1000;
151 if (!bitrate)
153 FIXME("Not a valid header: %02x:%02x:%02x:%02x\n", header[0], header[1], header[2], header[3]);
154 return E_INVALIDARG;
158 if (layer == 3 || layer == 2)
159 length = 144 * bitrate / freqs[freq_index] + padding;
160 else
161 length = 4 * (12 * bitrate / freqs[freq_index] + padding);
163 duration = (ULONGLONG)10000000 * (ULONGLONG)(length) / (ULONGLONG)(bitrate/8);
164 *plen = length;
165 if (pduration)
166 *pduration += duration;
167 return S_OK;
170 static HRESULT FillBuffer(MPEGSplitterImpl *This, IMediaSample *pCurrentSample)
172 Parser_OutputPin * pOutputPin = (Parser_OutputPin*)This->Parser.ppPins[1];
173 LONGLONG length = 0;
174 LONGLONG pos = BYTES_FROM_MEDIATIME(This->Parser.pInputPin->rtNext);
175 LONGLONG time = This->position, rtstop, rtstart;
176 HRESULT hr;
177 BYTE *fbuf = NULL;
178 DWORD len = IMediaSample_GetActualDataLength(pCurrentSample);
180 TRACE("Source length: %u\n", len);
181 IMediaSample_GetPointer(pCurrentSample, &fbuf);
183 /* Find the next valid header.. it <SHOULD> be right here */
184 hr = parse_header(fbuf, &length, &This->position);
185 assert(hr == S_OK);
186 IMediaSample_SetActualDataLength(pCurrentSample, length);
188 /* Queue the next sample */
189 if (length + 4 == len)
191 PullPin *pin = This->Parser.pInputPin;
192 LONGLONG stop = BYTES_FROM_MEDIATIME(pin->rtStop);
194 hr = S_OK;
195 memcpy(This->header, fbuf + length, 4);
196 while (FAILED(hr = parse_header(This->header, &length, NULL)))
198 memmove(This->header, This->header+1, 3);
199 if (pos + 4 >= stop)
200 break;
201 IAsyncReader_SyncRead(pin->pReader, ++pos, 1, This->header + 3);
203 pin->rtNext = MEDIATIME_FROM_BYTES(pos);
205 if (SUCCEEDED(hr))
207 /* Remove 4 for the last header, which should hopefully work */
208 IMediaSample *sample = NULL;
209 LONGLONG rtSampleStart = pin->rtNext - MEDIATIME_FROM_BYTES(4);
210 LONGLONG rtSampleStop = rtSampleStart + MEDIATIME_FROM_BYTES(length + 4);
212 if (rtSampleStop > pin->rtStop)
213 rtSampleStop = MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(pin->rtStop), pin->cbAlign));
215 hr = IMemAllocator_GetBuffer(pin->pAlloc, &sample, NULL, NULL, 0);
216 if (SUCCEEDED(hr))
218 IMediaSample_SetTime(sample, &rtSampleStart, &rtSampleStop);
219 IMediaSample_SetPreroll(sample, 0);
220 IMediaSample_SetDiscontinuity(sample, 0);
221 IMediaSample_SetSyncPoint(sample, 1);
222 hr = IAsyncReader_Request(pin->pReader, sample, 0);
223 if (SUCCEEDED(hr))
225 pin->rtCurrent = rtSampleStart;
226 pin->rtNext = rtSampleStop;
228 else
229 IMediaSample_Release(sample);
231 if (FAILED(hr))
232 FIXME("o_Ox%08x\n", hr);
235 /* If not, we're presumably at the end of file */
237 TRACE("Media time : %u.%03u\n", (DWORD)(This->position/10000000), (DWORD)((This->position/10000)%1000));
239 if (IMediaSample_IsDiscontinuity(pCurrentSample) == S_OK) {
240 IPin *victim;
241 EnterCriticalSection(&This->Parser.filter.csFilter);
242 pOutputPin->pin.pin.tStart = time;
243 pOutputPin->pin.pin.dRate = This->Parser.sourceSeeking.dRate;
244 hr = IPin_ConnectedTo((IPin *)pOutputPin, &victim);
245 if (hr == S_OK)
247 hr = IPin_NewSegment(victim, time, This->Parser.sourceSeeking.llStop,
248 This->Parser.sourceSeeking.dRate);
249 if (hr != S_OK)
250 FIXME("NewSegment returns %08x\n", hr);
251 IPin_Release(victim);
253 LeaveCriticalSection(&This->Parser.filter.csFilter);
254 if (hr != S_OK)
255 return hr;
257 rtstart = (double)(time - pOutputPin->pin.pin.tStart) / pOutputPin->pin.pin.dRate;
258 rtstop = (double)(This->position - pOutputPin->pin.pin.tStart) / pOutputPin->pin.pin.dRate;
259 IMediaSample_SetTime(pCurrentSample, &rtstart, &rtstop);
260 IMediaSample_SetMediaTime(pCurrentSample, &time, &This->position);
262 hr = BaseOutputPinImpl_Deliver((BaseOutputPin*)&pOutputPin->pin, pCurrentSample);
264 if (hr != S_OK)
266 if (hr != S_FALSE)
267 TRACE("Error sending sample (%x)\n", hr);
268 else
269 TRACE("S_FALSE (%d), holding\n", IMediaSample_GetActualDataLength(pCurrentSample));
272 return hr;
276 static HRESULT MPEGSplitter_process_sample(LPVOID iface, IMediaSample * pSample, DWORD_PTR cookie)
278 MPEGSplitterImpl *This = iface;
279 BYTE *pbSrcStream;
280 DWORD cbSrcStream = 0;
281 REFERENCE_TIME tStart, tStop, tAviStart = This->position;
282 HRESULT hr;
284 hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
285 if (SUCCEEDED(hr))
287 cbSrcStream = IMediaSample_GetActualDataLength(pSample);
288 hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
291 /* Flush occurring */
292 if (cbSrcStream == 0)
294 FIXME(".. Why do I need you?\n");
295 return S_OK;
298 /* trace removed for performance reasons */
299 /* TRACE("(%p), %llu -> %llu\n", pSample, tStart, tStop); */
301 /* Now, try to find a new header */
302 hr = FillBuffer(This, pSample);
303 if (hr != S_OK)
305 WARN("Failed with hres: %08x!\n", hr);
307 /* Unset progression if denied! */
308 if (hr == VFW_E_WRONG_STATE || hr == S_FALSE)
310 memcpy(This->header, pbSrcStream, 4);
311 This->Parser.pInputPin->rtCurrent = tStart;
312 This->position = tAviStart;
316 if (BYTES_FROM_MEDIATIME(tStop) >= This->EndOfFile || This->position >= This->Parser.sourceSeeking.llStop)
318 unsigned int i;
320 TRACE("End of file reached\n");
322 for (i = 0; i < This->Parser.cStreams; i++)
324 IPin* ppin;
326 hr = IPin_ConnectedTo(This->Parser.ppPins[i+1], &ppin);
327 if (SUCCEEDED(hr))
329 hr = IPin_EndOfStream(ppin);
330 IPin_Release(ppin);
332 if (FAILED(hr))
333 WARN("Error sending EndOfStream to pin %u (%x)\n", i, hr);
336 /* Force the pullpin thread to stop */
337 hr = S_FALSE;
340 return hr;
344 static HRESULT MPEGSplitter_query_accept(LPVOID iface, const AM_MEDIA_TYPE *pmt)
346 if (!IsEqualIID(&pmt->majortype, &MEDIATYPE_Stream))
347 return S_FALSE;
349 if (IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_MPEG1Audio))
350 return S_OK;
352 if (IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_MPEG1Video))
353 FIXME("MPEG-1 video streams not yet supported.\n");
354 else if (IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_MPEG1System))
355 FIXME("MPEG-1 system streams not yet supported.\n");
356 else if (IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_MPEG1VideoCD))
357 FIXME("MPEG-1 VideoCD streams not yet supported.\n");
358 else FIXME("%s\n", debugstr_guid(&pmt->subtype));
360 return S_FALSE;
364 static HRESULT MPEGSplitter_init_audio(MPEGSplitterImpl *This, const BYTE *header, PIN_INFO *ppiOutput, AM_MEDIA_TYPE *pamt)
366 WAVEFORMATEX *format;
367 int bitrate_index;
368 int freq_index;
369 int mode_ext;
370 int emphasis;
371 int lsf = 1;
372 int mpeg1;
373 int layer;
374 int mode;
376 ZeroMemory(pamt, sizeof(*pamt));
377 ppiOutput->dir = PINDIR_OUTPUT;
378 ppiOutput->pFilter = (IBaseFilter*)This;
379 wsprintfW(ppiOutput->achName, wszAudioStream);
381 pamt->formattype = FORMAT_WaveFormatEx;
382 pamt->majortype = MEDIATYPE_Audio;
383 pamt->subtype = MEDIASUBTYPE_MPEG1AudioPayload;
385 pamt->lSampleSize = 0;
386 pamt->bFixedSizeSamples = FALSE;
387 pamt->bTemporalCompression = 0;
389 mpeg1 = (header[1]>>4)&0x1;
390 if (mpeg1)
391 lsf = ((header[1]>>3)&0x1)^1;
393 layer = 4-((header[1]>>1)&0x3);
394 bitrate_index = ((header[2]>>4)&0xf);
395 freq_index = ((header[2]>>2)&0x3) + (mpeg1?(lsf*3):6);
396 mode = ((header[3]>>6)&0x3);
397 mode_ext = ((header[3]>>4)&0x3);
398 emphasis = ((header[3]>>0)&0x3);
400 if (!bitrate_index)
402 /* Set to highest bitrate so samples will fit in for sure */
403 FIXME("Variable-bitrate audio not fully supported.\n");
404 bitrate_index = 15;
407 pamt->cbFormat = ((layer==3)? sizeof(MPEGLAYER3WAVEFORMAT) :
408 sizeof(MPEG1WAVEFORMAT));
409 pamt->pbFormat = CoTaskMemAlloc(pamt->cbFormat);
410 if (!pamt->pbFormat)
411 return E_OUTOFMEMORY;
412 ZeroMemory(pamt->pbFormat, pamt->cbFormat);
413 format = (WAVEFORMATEX*)pamt->pbFormat;
415 format->wFormatTag = ((layer == 3) ? WAVE_FORMAT_MPEGLAYER3 :
416 WAVE_FORMAT_MPEG);
417 format->nChannels = ((mode == 3) ? 1 : 2);
418 format->nSamplesPerSec = freqs[freq_index];
419 format->nAvgBytesPerSec = tabsel_123[lsf][layer-1][bitrate_index] * 1000 / 8;
421 if (layer == 3)
422 format->nBlockAlign = format->nAvgBytesPerSec * 8 * 144 /
423 (format->nSamplesPerSec<<lsf) + 1;
424 else if (layer == 2)
425 format->nBlockAlign = format->nAvgBytesPerSec * 8 * 144 /
426 format->nSamplesPerSec + 1;
427 else
428 format->nBlockAlign = 4 * (format->nAvgBytesPerSec * 8 * 12 / format->nSamplesPerSec + 1);
430 format->wBitsPerSample = 0;
432 if (layer == 3)
434 MPEGLAYER3WAVEFORMAT *mp3format = (MPEGLAYER3WAVEFORMAT*)format;
436 format->cbSize = MPEGLAYER3_WFX_EXTRA_BYTES;
438 mp3format->wID = MPEGLAYER3_ID_MPEG;
439 mp3format->fdwFlags = MPEGLAYER3_FLAG_PADDING_ON;
440 mp3format->nBlockSize = format->nBlockAlign;
441 mp3format->nFramesPerBlock = 1;
443 /* Beware the evil magic numbers. This struct is apparently horribly
444 * under-documented, and the only references I could find had it being
445 * set to this with no real explanation. It works fine though, so I'm
446 * not complaining (yet).
448 mp3format->nCodecDelay = 1393;
450 else
452 MPEG1WAVEFORMAT *mpgformat = (MPEG1WAVEFORMAT*)format;
454 format->cbSize = 22;
456 mpgformat->fwHeadLayer = ((layer == 1) ? ACM_MPEG_LAYER1 :
457 ((layer == 2) ? ACM_MPEG_LAYER2 :
458 ACM_MPEG_LAYER3));
459 mpgformat->dwHeadBitrate = format->nAvgBytesPerSec * 8;
460 mpgformat->fwHeadMode = ((mode == 3) ? ACM_MPEG_SINGLECHANNEL :
461 ((mode == 2) ? ACM_MPEG_DUALCHANNEL :
462 ((mode == 1) ? ACM_MPEG_JOINTSTEREO :
463 ACM_MPEG_STEREO)));
464 mpgformat->fwHeadModeExt = ((mode == 1) ? 0x0F : (1<<mode_ext));
465 mpgformat->wHeadEmphasis = emphasis + 1;
466 mpgformat->fwHeadFlags = ACM_MPEG_ID_MPEG1;
468 pamt->subtype.Data1 = format->wFormatTag;
470 TRACE("MPEG audio stream detected:\n"
471 "\tLayer %d (%#x)\n"
472 "\tFrequency: %d\n"
473 "\tChannels: %d (%d)\n"
474 "\tBytesPerSec: %d\n",
475 layer, format->wFormatTag, format->nSamplesPerSec,
476 format->nChannels, mode, format->nAvgBytesPerSec);
478 dump_AM_MEDIA_TYPE(pamt);
480 return S_OK;
484 static HRESULT MPEGSplitter_pre_connect(IPin *iface, IPin *pConnectPin, ALLOCATOR_PROPERTIES *props)
486 PullPin *pPin = (PullPin *)iface;
487 MPEGSplitterImpl *This = (MPEGSplitterImpl*)pPin->pin.pinInfo.pFilter;
488 HRESULT hr;
489 LONGLONG pos = 0; /* in bytes */
490 BYTE header[10];
491 int streamtype = 0;
492 LONGLONG total, avail;
493 AM_MEDIA_TYPE amt;
494 PIN_INFO piOutput;
496 IAsyncReader_Length(pPin->pReader, &total, &avail);
497 This->EndOfFile = total;
499 hr = IAsyncReader_SyncRead(pPin->pReader, pos, 4, header);
500 if (SUCCEEDED(hr))
501 pos += 4;
503 /* Skip ID3 v2 tag, if any */
504 if (SUCCEEDED(hr) && !memcmp("ID3", header, 3))
505 do {
506 UINT length;
507 hr = IAsyncReader_SyncRead(pPin->pReader, pos, 6, header + 4);
508 if (FAILED(hr))
509 break;
510 pos += 6;
511 TRACE("Found ID3 v2.%d.%d\n", header[3], header[4]);
512 length = (header[6] & 0x7F) << 21;
513 length += (header[7] & 0x7F) << 14;
514 length += (header[8] & 0x7F) << 7;
515 length += (header[9] & 0x7F);
516 TRACE("Length: %u\n", length);
517 pos += length;
519 /* Read the real header for the mpeg splitter */
520 hr = IAsyncReader_SyncRead(pPin->pReader, pos, 4, header);
521 if (SUCCEEDED(hr))
522 pos += 4;
523 TRACE("%x:%x:%x:%x\n", header[0], header[1], header[2], header[3]);
524 } while (0);
526 while(SUCCEEDED(hr) && !(streamtype=MPEGSplitter_head_check(header)))
528 TRACE("%x:%x:%x:%x\n", header[0], header[1], header[2], header[3]);
529 /* No valid header yet; shift by a byte and check again */
530 memmove(header, header+1, 3);
531 hr = IAsyncReader_SyncRead(pPin->pReader, pos++, 1, header + 3);
533 if (FAILED(hr))
534 return hr;
535 pos -= 4;
536 This->begin_offset = pos;
537 memcpy(This->header, header, 4);
539 This->seektable[0].bytepos = pos;
540 This->seektable[0].timepos = 0;
542 switch(streamtype)
544 case MPEG_AUDIO_HEADER:
546 LONGLONG duration = 0;
547 DWORD last_entry = 0;
549 DWORD ticks = GetTickCount();
551 hr = MPEGSplitter_init_audio(This, header, &piOutput, &amt);
552 if (SUCCEEDED(hr))
554 WAVEFORMATEX *format = (WAVEFORMATEX*)amt.pbFormat;
556 props->cbAlign = 1;
557 props->cbPrefix = 0;
558 /* Make the output buffer a multiple of the frame size */
559 props->cbBuffer = 0x4000 / format->nBlockAlign *
560 format->nBlockAlign;
561 props->cBuffers = 3;
562 hr = Parser_AddPin(&(This->Parser), &piOutput, props, &amt);
565 if (FAILED(hr))
567 if (amt.pbFormat)
568 CoTaskMemFree(amt.pbFormat);
569 ERR("Could not create pin for MPEG audio stream (%x)\n", hr);
570 break;
573 /* Check for idv1 tag, and remove it from stream if found */
574 hr = IAsyncReader_SyncRead(pPin->pReader, This->EndOfFile-128, 3, header+4);
575 if (FAILED(hr))
576 break;
577 if (!strncmp((char*)header+4, "TAG", 3))
578 This->EndOfFile -= 128;
579 This->Parser.pInputPin->rtStop = MEDIATIME_FROM_BYTES(This->EndOfFile);
580 This->Parser.pInputPin->rtStart = This->Parser.pInputPin->rtCurrent = MEDIATIME_FROM_BYTES(This->begin_offset);
582 /* http://mpgedit.org/mpgedit/mpeg_format/mpeghdr.htm has a whole read up on audio headers */
583 while (pos + 3 < This->EndOfFile)
585 LONGLONG length = 0;
586 hr = IAsyncReader_SyncRead(pPin->pReader, pos, 4, header);
587 if (hr != S_OK)
588 break;
589 while (parse_header(header, &length, &duration))
591 /* No valid header yet; shift by a byte and check again */
592 memmove(header, header+1, 3);
593 hr = IAsyncReader_SyncRead(pPin->pReader, pos++, 1, header + 3);
594 if (hr != S_OK || This->EndOfFile - pos < 4)
595 break;
597 pos += length;
599 if (This->seektable && (duration / SEEK_INTERVAL) > last_entry)
601 if (last_entry + 1 > duration / SEEK_INTERVAL)
603 ERR("Somehow skipped %d interval lengths instead of 1\n", (DWORD)(duration/SEEK_INTERVAL) - (last_entry + 1));
605 ++last_entry;
607 TRACE("Entry: %u\n", last_entry);
608 if (last_entry >= This->seek_entries)
610 This->seek_entries += 64;
611 This->seektable = CoTaskMemRealloc(This->seektable, (This->seek_entries)*sizeof(struct seek_entry));
613 This->seektable[last_entry].bytepos = pos;
614 This->seektable[last_entry].timepos = duration;
617 TRACE("Pos: %x%08x/%x%08x\n", (DWORD)(pos >> 32), (DWORD)pos, (DWORD)(This->EndOfFile>>32), (DWORD)This->EndOfFile);
619 hr = S_OK;
620 TRACE("Duration: %d seconds\n", (DWORD)(duration / 10000000));
621 TRACE("Parsing took %u ms\n", GetTickCount() - ticks);
622 This->duration = duration;
624 This->Parser.sourceSeeking.llCurrent = 0;
625 This->Parser.sourceSeeking.llDuration = duration;
626 This->Parser.sourceSeeking.llStop = duration;
627 break;
629 case MPEG_VIDEO_HEADER:
630 FIXME("MPEG video processing not yet supported!\n");
631 hr = E_FAIL;
632 break;
633 case MPEG_SYSTEM_HEADER:
634 FIXME("MPEG system streams not yet supported!\n");
635 hr = E_FAIL;
636 break;
638 default:
639 break;
641 This->position = 0;
643 return hr;
646 static HRESULT MPEGSplitter_cleanup(LPVOID iface)
648 MPEGSplitterImpl *This = iface;
650 TRACE("(%p)\n", This);
652 return S_OK;
655 static HRESULT WINAPI MPEGSplitter_seek(IMediaSeeking *iface)
657 MPEGSplitterImpl *This = impl_from_IMediaSeeking(iface);
658 PullPin *pPin = This->Parser.pInputPin;
659 LONGLONG newpos, timepos, bytepos;
660 HRESULT hr = S_OK;
661 BYTE header[4];
663 newpos = This->Parser.sourceSeeking.llCurrent;
665 if (newpos > This->duration)
667 WARN("Requesting position %x%08x beyond end of stream %x%08x\n", (DWORD)(newpos>>32), (DWORD)newpos, (DWORD)(This->duration>>32), (DWORD)This->duration);
668 return E_INVALIDARG;
671 if (This->position/1000000 == newpos/1000000)
673 TRACE("Requesting position %x%08x same as current position %x%08x\n", (DWORD)(newpos>>32), (DWORD)newpos, (DWORD)(This->position>>32), (DWORD)This->position);
674 return S_OK;
677 /* Position, cached */
678 bytepos = This->seektable[newpos / SEEK_INTERVAL].bytepos;
679 timepos = This->seektable[newpos / SEEK_INTERVAL].timepos;
681 hr = IAsyncReader_SyncRead(pPin->pReader, bytepos, 4, header);
682 while (bytepos + 3 < This->EndOfFile)
684 LONGLONG length = 0;
685 hr = IAsyncReader_SyncRead(pPin->pReader, bytepos, 4, header);
686 if (hr != S_OK || timepos >= newpos)
687 break;
689 while (parse_header(header, &length, &timepos) && bytepos + 3 < This->EndOfFile)
691 /* No valid header yet; shift by a byte and check again */
692 memmove(header, header+1, 3);
693 hr = IAsyncReader_SyncRead(pPin->pReader, ++bytepos, 1, header + 3);
694 if (hr != S_OK)
695 break;
697 bytepos += length;
698 TRACE("Pos: %x%08x/%x%08x\n", (DWORD)(bytepos >> 32), (DWORD)bytepos, (DWORD)(This->EndOfFile>>32), (DWORD)This->EndOfFile);
701 if (SUCCEEDED(hr))
703 PullPin *pin = This->Parser.pInputPin;
705 TRACE("Moving sound to %08u bytes!\n", (DWORD)bytepos);
707 EnterCriticalSection(&pin->thread_lock);
708 IPin_BeginFlush((IPin *)pin);
710 /* Make sure this is done while stopped, BeginFlush takes care of this */
711 EnterCriticalSection(&This->Parser.filter.csFilter);
712 memcpy(This->header, header, 4);
714 pin->rtStart = pin->rtCurrent = MEDIATIME_FROM_BYTES(bytepos);
715 pin->rtStop = MEDIATIME_FROM_BYTES((REFERENCE_TIME)This->EndOfFile);
716 This->seek = TRUE;
717 This->position = newpos;
718 LeaveCriticalSection(&This->Parser.filter.csFilter);
720 TRACE("Done flushing\n");
721 IPin_EndFlush((IPin *)pin);
722 LeaveCriticalSection(&pin->thread_lock);
724 return hr;
727 static HRESULT MPEGSplitter_disconnect(LPVOID iface)
729 /* TODO: Find memory leaks etc */
730 return S_OK;
733 static HRESULT MPEGSplitter_first_request(LPVOID iface)
735 MPEGSplitterImpl *This = iface;
736 PullPin *pin = This->Parser.pInputPin;
737 HRESULT hr;
738 LONGLONG length;
739 IMediaSample *sample;
741 TRACE("Seeking? %d\n", This->seek);
743 hr = parse_header(This->header, &length, NULL);
744 assert(hr == S_OK);
746 if (pin->rtCurrent >= pin->rtStop)
748 /* Last sample has already been queued, request nothing more */
749 FIXME("Done!\n");
750 return S_OK;
753 hr = IMemAllocator_GetBuffer(pin->pAlloc, &sample, NULL, NULL, 0);
755 pin->rtNext = pin->rtCurrent;
756 if (SUCCEEDED(hr))
758 LONGLONG rtSampleStart = pin->rtNext;
759 /* Add 4 for the next header, which should hopefully work */
760 LONGLONG rtSampleStop = rtSampleStart + MEDIATIME_FROM_BYTES(length + 4);
762 if (rtSampleStop > pin->rtStop)
763 rtSampleStop = MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(pin->rtStop), pin->cbAlign));
765 hr = IMediaSample_SetTime(sample, &rtSampleStart, &rtSampleStop);
767 IMediaSample_SetPreroll(sample, FALSE);
768 IMediaSample_SetDiscontinuity(sample, TRUE);
769 IMediaSample_SetSyncPoint(sample, 1);
770 This->seek = 0;
772 hr = IAsyncReader_Request(pin->pReader, sample, 0);
773 if (SUCCEEDED(hr))
775 pin->rtCurrent = pin->rtNext;
776 pin->rtNext = rtSampleStop;
778 else
779 IMediaSample_Release(sample);
781 if (FAILED(hr))
782 ERR("Horsemen of the apocalypse came to bring error 0x%08x\n", hr);
784 return hr;
787 static const IBaseFilterVtbl MPEGSplitter_Vtbl =
789 Parser_QueryInterface,
790 Parser_AddRef,
791 Parser_Release,
792 Parser_GetClassID,
793 Parser_Stop,
794 Parser_Pause,
795 Parser_Run,
796 Parser_GetState,
797 Parser_SetSyncSource,
798 Parser_GetSyncSource,
799 Parser_EnumPins,
800 Parser_FindPin,
801 Parser_QueryFilterInfo,
802 Parser_JoinFilterGraph,
803 Parser_QueryVendorInfo
806 HRESULT MPEGSplitter_create(IUnknown * pUnkOuter, LPVOID * ppv)
808 MPEGSplitterImpl *This;
809 HRESULT hr = E_FAIL;
811 TRACE("(%p, %p)\n", pUnkOuter, ppv);
813 *ppv = NULL;
815 if (pUnkOuter)
816 return CLASS_E_NOAGGREGATION;
818 This = CoTaskMemAlloc(sizeof(MPEGSplitterImpl));
819 if (!This)
820 return E_OUTOFMEMORY;
822 ZeroMemory(This, sizeof(MPEGSplitterImpl));
823 This->seektable = CoTaskMemAlloc(sizeof(struct seek_entry) * 64);
824 if (!This->seektable)
826 CoTaskMemFree(This);
827 return E_OUTOFMEMORY;
829 This->seek_entries = 64;
831 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);
832 if (FAILED(hr))
834 CoTaskMemFree(This);
835 return hr;
837 This->seek = 1;
839 /* Note: This memory is managed by the parser filter once created */
840 *ppv = This;
842 return hr;