quartz: Free two assert calls from having side effects.
[wine/testsucceed.git] / dlls / quartz / mpegsplit.c
blob68348a353562e2c190807f58d55dd68bda9ee5d8
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 "control_private.h"
29 #include "pin.h"
31 #include "uuids.h"
32 #include "mmreg.h"
33 #include "mmsystem.h"
35 #include "winternl.h"
37 #include "wine/unicode.h"
38 #include "wine/debug.h"
40 #include "parser.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
44 #define SEQUENCE_HEADER_CODE 0xB3
45 #define PACK_START_CODE 0xBA
47 #define SYSTEM_START_CODE 0xBB
48 #define AUDIO_ELEMENTARY_STREAM 0xC0
49 #define VIDEO_ELEMENTARY_STREAM 0xE0
51 #define MPEG_SYSTEM_HEADER 3
52 #define MPEG_VIDEO_HEADER 2
53 #define MPEG_AUDIO_HEADER 1
54 #define MPEG_NO_HEADER 0
56 #define SEEK_INTERVAL (ULONGLONG)(10 * 10000000) /* Add an entry every 10 seconds */
58 struct seek_entry {
59 ULONGLONG bytepos;
60 ULONGLONG timepos;
63 typedef struct MPEGSplitterImpl
65 ParserImpl Parser;
66 LONGLONG EndOfFile;
67 LONGLONG duration;
68 LONGLONG position;
69 DWORD begin_offset;
70 BYTE header[4];
72 /* Whether we just seeked (or started playing) */
73 BOOL seek;
75 /* Seeking cache */
76 ULONG seek_entries;
77 struct seek_entry *seektable;
78 } MPEGSplitterImpl;
80 static inline MPEGSplitterImpl *impl_from_IMediaSeeking( IMediaSeeking *iface )
82 return (MPEGSplitterImpl *)((char*)iface - FIELD_OFFSET(MPEGSplitterImpl, Parser.sourceSeeking.lpVtbl));
85 static int MPEGSplitter_head_check(const BYTE *header)
87 /* If this is a possible start code, check for a system or video header */
88 if (header[0] == 0 && header[1] == 0 && header[2] == 1)
90 /* Check if we got a system or elementary stream start code */
91 if (header[3] == PACK_START_CODE ||
92 header[3] == VIDEO_ELEMENTARY_STREAM ||
93 header[3] == AUDIO_ELEMENTARY_STREAM)
94 return MPEG_SYSTEM_HEADER;
96 /* Check for a MPEG video sequence start code */
97 if (header[3] == SEQUENCE_HEADER_CODE)
98 return MPEG_VIDEO_HEADER;
101 /* This should give a good guess if we have an MPEG audio header */
102 if(header[0] == 0xff && ((header[1]>>5)&0x7) == 0x7 &&
103 ((header[1]>>1)&0x3) != 0 && ((header[2]>>4)&0xf) != 0xf &&
104 ((header[2]>>2)&0x3) != 0x3)
105 return MPEG_AUDIO_HEADER;
107 /* Nothing yet.. */
108 return MPEG_NO_HEADER;
111 static const WCHAR wszAudioStream[] = {'A','u','d','i','o',0};
112 static const WCHAR wszVideoStream[] = {'V','i','d','e','o',0};
114 static const DWORD freqs[10] = { 44100, 48000, 32000, 22050, 24000, 16000, 11025, 12000, 8000, 0 };
116 static const DWORD tabsel_123[2][3][16] = {
117 { {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,},
118 {0,32,48,56, 64, 80, 96,112,128,160,192,224,256,320,384,},
119 {0,32,40,48, 56, 64, 80, 96,112,128,160,192,224,256,320,} },
121 { {0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,},
122 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,},
123 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,} }
126 static HRESULT parse_header(BYTE *header, LONGLONG *plen, LONGLONG *pduration)
128 LONGLONG duration;
130 int bitrate_index, freq_index, lsf = 1, mpeg1, layer, padding, bitrate, length;
131 if (header[0] != 0xff)
132 return E_INVALIDARG;
134 if (!(((header[1]>>5)&0x7) == 0x7 &&
135 ((header[1]>>1)&0x3) != 0 && ((header[2]>>4)&0xf) != 0xf &&
136 ((header[2]>>2)&0x3) != 0x3))
138 FIXME("Not a valid header: %02x:%02x\n", header[1], header[2]);
139 return E_INVALIDARG;
142 mpeg1 = (header[1]>>4)&0x1;
143 if (mpeg1)
144 lsf = ((header[1]>>3)&0x1)^1;
146 layer = 4-((header[1]>>1)&0x3);
147 bitrate_index = ((header[2]>>4)&0xf);
148 freq_index = ((header[2]>>2)&0x3) + (mpeg1?(lsf*3):6);
149 padding = ((header[2]>>1)&0x1);
151 bitrate = tabsel_123[lsf][layer-1][bitrate_index] * 1000;
152 if (!bitrate)
154 FIXME("Not a valid header: %02x:%02x:%02x:%02x\n", header[0], header[1], header[2], header[3]);
155 return E_INVALIDARG;
159 if (layer == 3 || layer == 2)
160 length = 144 * bitrate / freqs[freq_index] + padding;
161 else
162 length = 4 * (12 * bitrate / freqs[freq_index] + padding);
164 duration = (ULONGLONG)10000000 * (ULONGLONG)(length) / (ULONGLONG)(bitrate/8);
165 *plen = length;
166 if (pduration)
167 *pduration += duration;
168 return S_OK;
171 static HRESULT FillBuffer(MPEGSplitterImpl *This, IMediaSample *pCurrentSample)
173 Parser_OutputPin * pOutputPin = (Parser_OutputPin*)This->Parser.ppPins[1];
174 LONGLONG length = 0;
175 LONGLONG pos = BYTES_FROM_MEDIATIME(This->Parser.pInputPin->rtNext);
176 LONGLONG time = This->position, rtstop, rtstart;
177 HRESULT hr;
178 BYTE *fbuf = NULL;
179 DWORD len = IMediaSample_GetActualDataLength(pCurrentSample);
181 TRACE("Source length: %u\n", len);
182 IMediaSample_GetPointer(pCurrentSample, &fbuf);
184 /* Find the next valid header.. it <SHOULD> be right here */
185 hr = parse_header(fbuf, &length, &This->position);
186 assert(hr == S_OK);
187 IMediaSample_SetActualDataLength(pCurrentSample, length);
189 /* Queue the next sample */
190 if (length + 4 == len)
192 PullPin *pin = This->Parser.pInputPin;
193 LONGLONG stop = BYTES_FROM_MEDIATIME(pin->rtStop);
195 hr = S_OK;
196 memcpy(This->header, fbuf + length, 4);
197 while (FAILED(hr = parse_header(This->header, &length, NULL)))
199 memmove(This->header, This->header+1, 3);
200 if (pos + 4 >= stop)
201 break;
202 IAsyncReader_SyncRead(pin->pReader, ++pos, 1, This->header + 3);
204 pin->rtNext = MEDIATIME_FROM_BYTES(pos);
206 if (SUCCEEDED(hr))
208 /* Remove 4 for the last header, which should hopefully work */
209 IMediaSample *sample = NULL;
210 LONGLONG rtSampleStart = pin->rtNext - MEDIATIME_FROM_BYTES(4);
211 LONGLONG rtSampleStop = rtSampleStart + MEDIATIME_FROM_BYTES(length + 4);
213 if (rtSampleStop > pin->rtStop)
214 rtSampleStop = MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(pin->rtStop), pin->cbAlign));
216 hr = IMemAllocator_GetBuffer(pin->pAlloc, &sample, NULL, NULL, 0);
217 if (SUCCEEDED(hr))
219 IMediaSample_SetTime(sample, &rtSampleStart, &rtSampleStop);
220 IMediaSample_SetPreroll(sample, 0);
221 IMediaSample_SetDiscontinuity(sample, 0);
222 IMediaSample_SetSyncPoint(sample, 1);
223 hr = IAsyncReader_Request(pin->pReader, sample, 0);
224 if (SUCCEEDED(hr))
226 pin->rtCurrent = rtSampleStart;
227 pin->rtNext = rtSampleStop;
229 else
230 IMediaSample_Release(sample);
232 if (FAILED(hr))
233 FIXME("o_Ox%08x\n", hr);
236 /* If not, we're presumably at the end of file */
238 TRACE("Media time : %u.%03u\n", (DWORD)(This->position/10000000), (DWORD)((This->position/10000)%1000));
240 if (IMediaSample_IsDiscontinuity(pCurrentSample) == S_OK) {
241 IPin *victim;
242 EnterCriticalSection(&This->Parser.filter.csFilter);
243 pOutputPin->pin.pin.tStart = time;
244 pOutputPin->pin.pin.dRate = This->Parser.sourceSeeking.dRate;
245 hr = IPin_ConnectedTo((IPin *)pOutputPin, &victim);
246 if (hr == S_OK)
248 hr = IPin_NewSegment(victim, time, This->Parser.sourceSeeking.llStop,
249 This->Parser.sourceSeeking.dRate);
250 if (hr != S_OK)
251 FIXME("NewSegment returns %08x\n", hr);
252 IPin_Release(victim);
254 LeaveCriticalSection(&This->Parser.filter.csFilter);
255 if (hr != S_OK)
256 return hr;
258 rtstart = (double)(time - pOutputPin->pin.pin.tStart) / pOutputPin->pin.pin.dRate;
259 rtstop = (double)(This->position - pOutputPin->pin.pin.tStart) / pOutputPin->pin.pin.dRate;
260 IMediaSample_SetTime(pCurrentSample, &rtstart, &rtstop);
261 IMediaSample_SetMediaTime(pCurrentSample, &time, &This->position);
263 hr = BaseOutputPinImpl_Deliver((BaseOutputPin*)&pOutputPin->pin, pCurrentSample);
265 if (hr != S_OK)
267 if (hr != S_FALSE)
268 TRACE("Error sending sample (%x)\n", hr);
269 else
270 TRACE("S_FALSE (%d), holding\n", IMediaSample_GetActualDataLength(pCurrentSample));
273 return hr;
277 static HRESULT MPEGSplitter_process_sample(LPVOID iface, IMediaSample * pSample, DWORD_PTR cookie)
279 MPEGSplitterImpl *This = iface;
280 BYTE *pbSrcStream;
281 DWORD cbSrcStream = 0;
282 REFERENCE_TIME tStart, tStop, tAviStart = This->position;
283 HRESULT hr;
285 hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
286 if (SUCCEEDED(hr))
288 cbSrcStream = IMediaSample_GetActualDataLength(pSample);
289 hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
292 /* Flush occurring */
293 if (cbSrcStream == 0)
295 FIXME(".. Why do I need you?\n");
296 return S_OK;
299 /* trace removed for performance reasons */
300 /* TRACE("(%p), %llu -> %llu\n", pSample, tStart, tStop); */
302 /* Now, try to find a new header */
303 hr = FillBuffer(This, pSample);
304 if (hr != S_OK)
306 WARN("Failed with hres: %08x!\n", hr);
308 /* Unset progression if denied! */
309 if (hr == VFW_E_WRONG_STATE || hr == S_FALSE)
311 memcpy(This->header, pbSrcStream, 4);
312 This->Parser.pInputPin->rtCurrent = tStart;
313 This->position = tAviStart;
317 if (BYTES_FROM_MEDIATIME(tStop) >= This->EndOfFile || This->position >= This->Parser.sourceSeeking.llStop)
319 unsigned int i;
321 TRACE("End of file reached\n");
323 for (i = 0; i < This->Parser.cStreams; i++)
325 IPin* ppin;
327 hr = IPin_ConnectedTo(This->Parser.ppPins[i+1], &ppin);
328 if (SUCCEEDED(hr))
330 hr = IPin_EndOfStream(ppin);
331 IPin_Release(ppin);
333 if (FAILED(hr))
334 WARN("Error sending EndOfStream to pin %u (%x)\n", i, hr);
337 /* Force the pullpin thread to stop */
338 hr = S_FALSE;
341 return hr;
345 static HRESULT MPEGSplitter_query_accept(LPVOID iface, const AM_MEDIA_TYPE *pmt)
347 if (!IsEqualIID(&pmt->majortype, &MEDIATYPE_Stream))
348 return S_FALSE;
350 if (IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_MPEG1Audio))
351 return S_OK;
353 if (IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_MPEG1Video))
354 FIXME("MPEG-1 video streams not yet supported.\n");
355 else if (IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_MPEG1System))
356 FIXME("MPEG-1 system streams not yet supported.\n");
357 else if (IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_MPEG1VideoCD))
358 FIXME("MPEG-1 VideoCD streams not yet supported.\n");
359 else FIXME("%s\n", debugstr_guid(&pmt->subtype));
361 return S_FALSE;
365 static HRESULT MPEGSplitter_init_audio(MPEGSplitterImpl *This, const BYTE *header, PIN_INFO *ppiOutput, AM_MEDIA_TYPE *pamt)
367 WAVEFORMATEX *format;
368 int bitrate_index;
369 int freq_index;
370 int mode_ext;
371 int emphasis;
372 int lsf = 1;
373 int mpeg1;
374 int layer;
375 int mode;
377 ZeroMemory(pamt, sizeof(*pamt));
378 ppiOutput->dir = PINDIR_OUTPUT;
379 ppiOutput->pFilter = (IBaseFilter*)This;
380 wsprintfW(ppiOutput->achName, wszAudioStream);
382 pamt->formattype = FORMAT_WaveFormatEx;
383 pamt->majortype = MEDIATYPE_Audio;
384 pamt->subtype = MEDIASUBTYPE_MPEG1AudioPayload;
386 pamt->lSampleSize = 0;
387 pamt->bFixedSizeSamples = FALSE;
388 pamt->bTemporalCompression = 0;
390 mpeg1 = (header[1]>>4)&0x1;
391 if (mpeg1)
392 lsf = ((header[1]>>3)&0x1)^1;
394 layer = 4-((header[1]>>1)&0x3);
395 bitrate_index = ((header[2]>>4)&0xf);
396 freq_index = ((header[2]>>2)&0x3) + (mpeg1?(lsf*3):6);
397 mode = ((header[3]>>6)&0x3);
398 mode_ext = ((header[3]>>4)&0x3);
399 emphasis = ((header[3]>>0)&0x3);
401 if (!bitrate_index)
403 /* Set to highest bitrate so samples will fit in for sure */
404 FIXME("Variable-bitrate audio not fully supported.\n");
405 bitrate_index = 15;
408 pamt->cbFormat = ((layer==3)? sizeof(MPEGLAYER3WAVEFORMAT) :
409 sizeof(MPEG1WAVEFORMAT));
410 pamt->pbFormat = CoTaskMemAlloc(pamt->cbFormat);
411 if (!pamt->pbFormat)
412 return E_OUTOFMEMORY;
413 ZeroMemory(pamt->pbFormat, pamt->cbFormat);
414 format = (WAVEFORMATEX*)pamt->pbFormat;
416 format->wFormatTag = ((layer == 3) ? WAVE_FORMAT_MPEGLAYER3 :
417 WAVE_FORMAT_MPEG);
418 format->nChannels = ((mode == 3) ? 1 : 2);
419 format->nSamplesPerSec = freqs[freq_index];
420 format->nAvgBytesPerSec = tabsel_123[lsf][layer-1][bitrate_index] * 1000 / 8;
422 if (layer == 3)
423 format->nBlockAlign = format->nAvgBytesPerSec * 8 * 144 /
424 (format->nSamplesPerSec<<lsf) + 1;
425 else if (layer == 2)
426 format->nBlockAlign = format->nAvgBytesPerSec * 8 * 144 /
427 format->nSamplesPerSec + 1;
428 else
429 format->nBlockAlign = 4 * (format->nAvgBytesPerSec * 8 * 12 / format->nSamplesPerSec + 1);
431 format->wBitsPerSample = 0;
433 if (layer == 3)
435 MPEGLAYER3WAVEFORMAT *mp3format = (MPEGLAYER3WAVEFORMAT*)format;
437 format->cbSize = MPEGLAYER3_WFX_EXTRA_BYTES;
439 mp3format->wID = MPEGLAYER3_ID_MPEG;
440 mp3format->fdwFlags = MPEGLAYER3_FLAG_PADDING_ON;
441 mp3format->nBlockSize = format->nBlockAlign;
442 mp3format->nFramesPerBlock = 1;
444 /* Beware the evil magic numbers. This struct is apparently horribly
445 * under-documented, and the only references I could find had it being
446 * set to this with no real explanation. It works fine though, so I'm
447 * not complaining (yet).
449 mp3format->nCodecDelay = 1393;
451 else
453 MPEG1WAVEFORMAT *mpgformat = (MPEG1WAVEFORMAT*)format;
455 format->cbSize = 22;
457 mpgformat->fwHeadLayer = ((layer == 1) ? ACM_MPEG_LAYER1 :
458 ((layer == 2) ? ACM_MPEG_LAYER2 :
459 ACM_MPEG_LAYER3));
460 mpgformat->dwHeadBitrate = format->nAvgBytesPerSec * 8;
461 mpgformat->fwHeadMode = ((mode == 3) ? ACM_MPEG_SINGLECHANNEL :
462 ((mode == 2) ? ACM_MPEG_DUALCHANNEL :
463 ((mode == 1) ? ACM_MPEG_JOINTSTEREO :
464 ACM_MPEG_STEREO)));
465 mpgformat->fwHeadModeExt = ((mode == 1) ? 0x0F : (1<<mode_ext));
466 mpgformat->wHeadEmphasis = emphasis + 1;
467 mpgformat->fwHeadFlags = ACM_MPEG_ID_MPEG1;
469 pamt->subtype.Data1 = format->wFormatTag;
471 TRACE("MPEG audio stream detected:\n"
472 "\tLayer %d (%#x)\n"
473 "\tFrequency: %d\n"
474 "\tChannels: %d (%d)\n"
475 "\tBytesPerSec: %d\n",
476 layer, format->wFormatTag, format->nSamplesPerSec,
477 format->nChannels, mode, format->nAvgBytesPerSec);
479 dump_AM_MEDIA_TYPE(pamt);
481 return S_OK;
485 static HRESULT MPEGSplitter_pre_connect(IPin *iface, IPin *pConnectPin, ALLOCATOR_PROPERTIES *props)
487 PullPin *pPin = (PullPin *)iface;
488 MPEGSplitterImpl *This = (MPEGSplitterImpl*)pPin->pin.pinInfo.pFilter;
489 HRESULT hr;
490 LONGLONG pos = 0; /* in bytes */
491 BYTE header[10];
492 int streamtype = 0;
493 LONGLONG total, avail;
494 AM_MEDIA_TYPE amt;
495 PIN_INFO piOutput;
497 IAsyncReader_Length(pPin->pReader, &total, &avail);
498 This->EndOfFile = total;
500 hr = IAsyncReader_SyncRead(pPin->pReader, pos, 4, header);
501 if (SUCCEEDED(hr))
502 pos += 4;
504 /* Skip ID3 v2 tag, if any */
505 if (SUCCEEDED(hr) && !memcmp("ID3", header, 3))
506 do {
507 UINT length;
508 hr = IAsyncReader_SyncRead(pPin->pReader, pos, 6, header + 4);
509 if (FAILED(hr))
510 break;
511 pos += 6;
512 TRACE("Found ID3 v2.%d.%d\n", header[3], header[4]);
513 length = (header[6] & 0x7F) << 21;
514 length += (header[7] & 0x7F) << 14;
515 length += (header[8] & 0x7F) << 7;
516 length += (header[9] & 0x7F);
517 TRACE("Length: %u\n", length);
518 pos += length;
520 /* Read the real header for the mpeg splitter */
521 hr = IAsyncReader_SyncRead(pPin->pReader, pos, 4, header);
522 if (SUCCEEDED(hr))
523 pos += 4;
524 TRACE("%x:%x:%x:%x\n", header[0], header[1], header[2], header[3]);
525 } while (0);
527 while(SUCCEEDED(hr) && !(streamtype=MPEGSplitter_head_check(header)))
529 TRACE("%x:%x:%x:%x\n", header[0], header[1], header[2], header[3]);
530 /* No valid header yet; shift by a byte and check again */
531 memmove(header, header+1, 3);
532 hr = IAsyncReader_SyncRead(pPin->pReader, pos++, 1, header + 3);
534 if (FAILED(hr))
535 return hr;
536 pos -= 4;
537 This->begin_offset = pos;
538 memcpy(This->header, header, 4);
540 This->seektable[0].bytepos = pos;
541 This->seektable[0].timepos = 0;
543 switch(streamtype)
545 case MPEG_AUDIO_HEADER:
547 LONGLONG duration = 0;
548 DWORD last_entry = 0;
550 DWORD ticks = GetTickCount();
552 hr = MPEGSplitter_init_audio(This, header, &piOutput, &amt);
553 if (SUCCEEDED(hr))
555 WAVEFORMATEX *format = (WAVEFORMATEX*)amt.pbFormat;
557 props->cbAlign = 1;
558 props->cbPrefix = 0;
559 /* Make the output buffer a multiple of the frame size */
560 props->cbBuffer = 0x4000 / format->nBlockAlign *
561 format->nBlockAlign;
562 props->cBuffers = 3;
563 hr = Parser_AddPin(&(This->Parser), &piOutput, props, &amt);
566 if (FAILED(hr))
568 if (amt.pbFormat)
569 CoTaskMemFree(amt.pbFormat);
570 ERR("Could not create pin for MPEG audio stream (%x)\n", hr);
571 break;
574 /* Check for idv1 tag, and remove it from stream if found */
575 hr = IAsyncReader_SyncRead(pPin->pReader, This->EndOfFile-128, 3, header+4);
576 if (FAILED(hr))
577 break;
578 if (!strncmp((char*)header+4, "TAG", 3))
579 This->EndOfFile -= 128;
580 This->Parser.pInputPin->rtStop = MEDIATIME_FROM_BYTES(This->EndOfFile);
581 This->Parser.pInputPin->rtStart = This->Parser.pInputPin->rtCurrent = MEDIATIME_FROM_BYTES(This->begin_offset);
583 /* http://mpgedit.org/mpgedit/mpeg_format/mpeghdr.htm has a whole read up on audio headers */
584 while (pos + 3 < This->EndOfFile)
586 LONGLONG length = 0;
587 hr = IAsyncReader_SyncRead(pPin->pReader, pos, 4, header);
588 if (hr != S_OK)
589 break;
590 while (parse_header(header, &length, &duration))
592 /* No valid header yet; shift by a byte and check again */
593 memmove(header, header+1, 3);
594 hr = IAsyncReader_SyncRead(pPin->pReader, pos++, 1, header + 3);
595 if (hr != S_OK || This->EndOfFile - pos < 4)
596 break;
598 pos += length;
600 if (This->seektable && (duration / SEEK_INTERVAL) > last_entry)
602 if (last_entry + 1 > duration / SEEK_INTERVAL)
604 ERR("Somehow skipped %d interval lengths instead of 1\n", (DWORD)(duration/SEEK_INTERVAL) - (last_entry + 1));
606 ++last_entry;
608 TRACE("Entry: %u\n", last_entry);
609 if (last_entry >= This->seek_entries)
611 This->seek_entries += 64;
612 This->seektable = CoTaskMemRealloc(This->seektable, (This->seek_entries)*sizeof(struct seek_entry));
614 This->seektable[last_entry].bytepos = pos;
615 This->seektable[last_entry].timepos = duration;
618 TRACE("Pos: %x%08x/%x%08x\n", (DWORD)(pos >> 32), (DWORD)pos, (DWORD)(This->EndOfFile>>32), (DWORD)This->EndOfFile);
620 hr = S_OK;
621 TRACE("Duration: %d seconds\n", (DWORD)(duration / 10000000));
622 TRACE("Parsing took %u ms\n", GetTickCount() - ticks);
623 This->duration = duration;
625 This->Parser.sourceSeeking.llCurrent = 0;
626 This->Parser.sourceSeeking.llDuration = duration;
627 This->Parser.sourceSeeking.llStop = duration;
628 break;
630 case MPEG_VIDEO_HEADER:
631 FIXME("MPEG video processing not yet supported!\n");
632 hr = E_FAIL;
633 break;
634 case MPEG_SYSTEM_HEADER:
635 FIXME("MPEG system streams not yet supported!\n");
636 hr = E_FAIL;
637 break;
639 default:
640 break;
642 This->position = 0;
644 return hr;
647 static HRESULT MPEGSplitter_cleanup(LPVOID iface)
649 MPEGSplitterImpl *This = iface;
651 TRACE("(%p)\n", This);
653 return S_OK;
656 static HRESULT WINAPI MPEGSplitter_seek(IMediaSeeking *iface)
658 MPEGSplitterImpl *This = impl_from_IMediaSeeking(iface);
659 PullPin *pPin = This->Parser.pInputPin;
660 LONGLONG newpos, timepos, bytepos;
661 HRESULT hr = S_OK;
662 BYTE header[4];
664 newpos = This->Parser.sourceSeeking.llCurrent;
666 if (newpos > This->duration)
668 WARN("Requesting position %x%08x beyond end of stream %x%08x\n", (DWORD)(newpos>>32), (DWORD)newpos, (DWORD)(This->duration>>32), (DWORD)This->duration);
669 return E_INVALIDARG;
672 if (This->position/1000000 == newpos/1000000)
674 TRACE("Requesting position %x%08x same as current position %x%08x\n", (DWORD)(newpos>>32), (DWORD)newpos, (DWORD)(This->position>>32), (DWORD)This->position);
675 return S_OK;
678 /* Position, cached */
679 bytepos = This->seektable[newpos / SEEK_INTERVAL].bytepos;
680 timepos = This->seektable[newpos / SEEK_INTERVAL].timepos;
682 hr = IAsyncReader_SyncRead(pPin->pReader, bytepos, 4, header);
683 while (bytepos + 3 < This->EndOfFile)
685 LONGLONG length = 0;
686 hr = IAsyncReader_SyncRead(pPin->pReader, bytepos, 4, header);
687 if (hr != S_OK || timepos >= newpos)
688 break;
690 while (parse_header(header, &length, &timepos) && bytepos + 3 < This->EndOfFile)
692 /* No valid header yet; shift by a byte and check again */
693 memmove(header, header+1, 3);
694 hr = IAsyncReader_SyncRead(pPin->pReader, ++bytepos, 1, header + 3);
695 if (hr != S_OK)
696 break;
698 bytepos += length;
699 TRACE("Pos: %x%08x/%x%08x\n", (DWORD)(bytepos >> 32), (DWORD)bytepos, (DWORD)(This->EndOfFile>>32), (DWORD)This->EndOfFile);
702 if (SUCCEEDED(hr))
704 PullPin *pin = This->Parser.pInputPin;
706 TRACE("Moving sound to %08u bytes!\n", (DWORD)bytepos);
708 EnterCriticalSection(&pin->thread_lock);
709 IPin_BeginFlush((IPin *)pin);
711 /* Make sure this is done while stopped, BeginFlush takes care of this */
712 EnterCriticalSection(&This->Parser.filter.csFilter);
713 memcpy(This->header, header, 4);
715 pin->rtStart = pin->rtCurrent = MEDIATIME_FROM_BYTES(bytepos);
716 pin->rtStop = MEDIATIME_FROM_BYTES((REFERENCE_TIME)This->EndOfFile);
717 This->seek = TRUE;
718 This->position = newpos;
719 LeaveCriticalSection(&This->Parser.filter.csFilter);
721 TRACE("Done flushing\n");
722 IPin_EndFlush((IPin *)pin);
723 LeaveCriticalSection(&pin->thread_lock);
725 return hr;
728 static HRESULT MPEGSplitter_disconnect(LPVOID iface)
730 /* TODO: Find memory leaks etc */
731 return S_OK;
734 static HRESULT MPEGSplitter_first_request(LPVOID iface)
736 MPEGSplitterImpl *This = iface;
737 PullPin *pin = This->Parser.pInputPin;
738 HRESULT hr;
739 LONGLONG length;
740 IMediaSample *sample;
742 TRACE("Seeking? %d\n", This->seek);
744 hr = parse_header(This->header, &length, NULL);
745 assert(hr == S_OK);
747 if (pin->rtCurrent >= pin->rtStop)
749 /* Last sample has already been queued, request nothing more */
750 FIXME("Done!\n");
751 return S_OK;
754 hr = IMemAllocator_GetBuffer(pin->pAlloc, &sample, NULL, NULL, 0);
756 pin->rtNext = pin->rtCurrent;
757 if (SUCCEEDED(hr))
759 LONGLONG rtSampleStart = pin->rtNext;
760 /* Add 4 for the next header, which should hopefully work */
761 LONGLONG rtSampleStop = rtSampleStart + MEDIATIME_FROM_BYTES(length + 4);
763 if (rtSampleStop > pin->rtStop)
764 rtSampleStop = MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(pin->rtStop), pin->cbAlign));
766 hr = IMediaSample_SetTime(sample, &rtSampleStart, &rtSampleStop);
768 IMediaSample_SetPreroll(sample, FALSE);
769 IMediaSample_SetDiscontinuity(sample, TRUE);
770 IMediaSample_SetSyncPoint(sample, 1);
771 This->seek = 0;
773 hr = IAsyncReader_Request(pin->pReader, sample, 0);
774 if (SUCCEEDED(hr))
776 pin->rtCurrent = pin->rtNext;
777 pin->rtNext = rtSampleStop;
779 else
780 IMediaSample_Release(sample);
782 if (FAILED(hr))
783 ERR("Horsemen of the apocalypse came to bring error 0x%08x\n", hr);
785 return hr;
788 static const IBaseFilterVtbl MPEGSplitter_Vtbl =
790 Parser_QueryInterface,
791 Parser_AddRef,
792 Parser_Release,
793 Parser_GetClassID,
794 Parser_Stop,
795 Parser_Pause,
796 Parser_Run,
797 Parser_GetState,
798 Parser_SetSyncSource,
799 Parser_GetSyncSource,
800 Parser_EnumPins,
801 Parser_FindPin,
802 Parser_QueryFilterInfo,
803 Parser_JoinFilterGraph,
804 Parser_QueryVendorInfo
807 HRESULT MPEGSplitter_create(IUnknown * pUnkOuter, LPVOID * ppv)
809 MPEGSplitterImpl *This;
810 HRESULT hr = E_FAIL;
812 TRACE("(%p, %p)\n", pUnkOuter, ppv);
814 *ppv = NULL;
816 if (pUnkOuter)
817 return CLASS_E_NOAGGREGATION;
819 This = CoTaskMemAlloc(sizeof(MPEGSplitterImpl));
820 if (!This)
821 return E_OUTOFMEMORY;
823 ZeroMemory(This, sizeof(MPEGSplitterImpl));
824 This->seektable = CoTaskMemAlloc(sizeof(struct seek_entry) * 64);
825 if (!This->seektable)
827 CoTaskMemFree(This);
828 return E_OUTOFMEMORY;
830 This->seek_entries = 64;
832 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);
833 if (FAILED(hr))
835 CoTaskMemFree(This);
836 return hr;
838 This->seek = 1;
840 /* Note: This memory is managed by the parser filter once created */
841 *ppv = This;
843 return hr;