crypt32/tests: Don't crash on some win9x boxes.
[wine.git] / dlls / quartz / mpegsplit.c
blobee93e2e98c5a498c0f4b8a2bfef596a4e29429ff
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 int MPEGSplitter_head_check(const BYTE *header)
82 /* If this is a possible start code, check for a system or video header */
83 if (header[0] == 0 && header[1] == 0 && header[2] == 1)
85 /* Check if we got a system or elementary stream start code */
86 if (header[3] == PACK_START_CODE ||
87 header[3] == VIDEO_ELEMENTARY_STREAM ||
88 header[3] == AUDIO_ELEMENTARY_STREAM)
89 return MPEG_SYSTEM_HEADER;
91 /* Check for a MPEG video sequence start code */
92 if (header[3] == SEQUENCE_HEADER_CODE)
93 return MPEG_VIDEO_HEADER;
96 /* This should give a good guess if we have an MPEG audio header */
97 if(header[0] == 0xff && ((header[1]>>5)&0x7) == 0x7 &&
98 ((header[1]>>1)&0x3) != 0 && ((header[2]>>4)&0xf) != 0xf &&
99 ((header[2]>>2)&0x3) != 0x3)
100 return MPEG_AUDIO_HEADER;
102 /* Nothing yet.. */
103 return MPEG_NO_HEADER;
106 static const WCHAR wszAudioStream[] = {'A','u','d','i','o',0};
107 static const WCHAR wszVideoStream[] = {'V','i','d','e','o',0};
109 static const DWORD freqs[10] = { 44100, 48000, 32000, 22050, 24000, 16000, 11025, 12000, 8000, 0 };
111 static const DWORD tabsel_123[2][3][16] = {
112 { {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,},
113 {0,32,48,56, 64, 80, 96,112,128,160,192,224,256,320,384,},
114 {0,32,40,48, 56, 64, 80, 96,112,128,160,192,224,256,320,} },
116 { {0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,},
117 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,},
118 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,} }
121 static HRESULT parse_header(BYTE *header, LONGLONG *plen, LONGLONG *pduration)
123 LONGLONG duration;
125 int bitrate_index, freq_index, lsf = 1, mpeg1, layer, padding, bitrate, length;
127 if (!(header[0] == 0xff && ((header[1]>>5)&0x7) == 0x7 &&
128 ((header[1]>>1)&0x3) != 0 && ((header[2]>>4)&0xf) != 0xf &&
129 ((header[2]>>2)&0x3) != 0x3))
131 FIXME("Not a valid header: %02x:%02x\n", header[0], header[1]);
132 return E_INVALIDARG;
135 mpeg1 = (header[1]>>4)&0x1;
136 if (mpeg1)
137 lsf = ((header[1]>>3)&0x1)^1;
139 layer = 4-((header[1]>>1)&0x3);
140 bitrate_index = ((header[2]>>4)&0xf);
141 freq_index = ((header[2]>>2)&0x3) + (mpeg1?(lsf*3):6);
142 padding = ((header[2]>>1)&0x1);
144 bitrate = tabsel_123[lsf][layer-1][bitrate_index] * 1000;
145 if (!bitrate || layer != 3)
147 FIXME("Not a valid header: %02x:%02x:%02x:%02x\n", header[0], header[1], header[2], header[3]);
148 return E_INVALIDARG;
152 if (layer == 3 || layer == 2)
153 length = 144 * bitrate / freqs[freq_index] + padding;
154 else
155 length = 4 * (12 * bitrate / freqs[freq_index] + padding);
157 duration = (ULONGLONG)10000000 * (ULONGLONG)(length) / (ULONGLONG)(bitrate/8);
158 *plen = length;
159 if (pduration)
160 *pduration += duration;
161 return S_OK;
164 static HRESULT FillBuffer(MPEGSplitterImpl *This, IMediaSample *pCurrentSample)
166 Parser_OutputPin * pOutputPin = (Parser_OutputPin*)This->Parser.ppPins[1];
167 LONGLONG length = 0;
168 LONGLONG pos = BYTES_FROM_MEDIATIME(This->Parser.pInputPin->rtNext);
169 LONGLONG time = This->position;
170 HRESULT hr;
171 BYTE *fbuf = NULL;
172 DWORD len = IMediaSample_GetActualDataLength(pCurrentSample);
174 TRACE("Source length: %u\n", len);
175 IMediaSample_GetPointer(pCurrentSample, &fbuf);
177 /* Find the next valid header.. it <SHOULD> be right here */
178 assert(parse_header(fbuf, &length, &This->position) == S_OK);
179 assert(length == len || length + 4 == len);
180 IMediaSample_SetActualDataLength(pCurrentSample, length);
182 /* Queue the next sample */
183 if (length + 4 == len)
185 PullPin *pin = This->Parser.pInputPin;
186 LONGLONG stop = BYTES_FROM_MEDIATIME(pin->rtStop);
188 hr = S_OK;
189 memcpy(This->header, fbuf + length, 4);
190 while (FAILED(hr = parse_header(This->header, &length, NULL)))
192 memmove(This->header, This->header+1, 3);
193 if (pos + 4 >= stop)
194 break;
195 IAsyncReader_SyncRead(pin->pReader, ++pos, 1, This->header + 3);
197 pin->rtNext = MEDIATIME_FROM_BYTES(pos);
199 if (SUCCEEDED(hr))
201 /* Remove 4 for the last header, which should hopefully work */
202 IMediaSample *sample = NULL;
203 LONGLONG rtSampleStart = pin->rtNext - MEDIATIME_FROM_BYTES(4);
204 LONGLONG rtSampleStop = rtSampleStart + MEDIATIME_FROM_BYTES(length + 4);
206 hr = IMemAllocator_GetBuffer(pin->pAlloc, &sample, NULL, NULL, 0);
208 if (rtSampleStop > pin->rtStop)
209 rtSampleStop = MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(pin->rtStop), pin->cbAlign));
211 IMediaSample_SetTime(sample, &rtSampleStart, &rtSampleStop);
212 IMediaSample_SetPreroll(sample, 0);
213 IMediaSample_SetDiscontinuity(sample, 0);
214 IMediaSample_SetSyncPoint(sample, 1);
215 pin->rtCurrent = rtSampleStart;
216 pin->rtNext = rtSampleStop;
218 if (SUCCEEDED(hr))
219 hr = IAsyncReader_Request(pin->pReader, sample, 0);
220 if (FAILED(hr))
221 FIXME("o_Ox%08x\n", hr);
224 /* If not, we're presumably at the end of file */
226 TRACE("Media time : %u.%03u\n", (DWORD)(This->position/10000000), (DWORD)((This->position/10000)%1000));
228 IMediaSample_SetTime(pCurrentSample, &time, &This->position);
230 hr = OutputPin_SendSample(&pOutputPin->pin, pCurrentSample);
232 if (hr != S_OK)
234 if (hr != S_FALSE)
235 TRACE("Error sending sample (%x)\n", hr);
236 else
237 TRACE("S_FALSE (%d), holding\n", IMediaSample_GetActualDataLength(pCurrentSample));
240 return hr;
244 static HRESULT MPEGSplitter_process_sample(LPVOID iface, IMediaSample * pSample, DWORD_PTR cookie)
246 MPEGSplitterImpl *This = (MPEGSplitterImpl*)iface;
247 BYTE *pbSrcStream;
248 DWORD cbSrcStream = 0;
249 REFERENCE_TIME tStart, tStop, tAviStart = This->position;
250 Parser_OutputPin * pOutputPin;
251 HRESULT hr;
253 pOutputPin = (Parser_OutputPin*)This->Parser.ppPins[1];
255 hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
256 if (SUCCEEDED(hr))
258 cbSrcStream = IMediaSample_GetActualDataLength(pSample);
259 hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
262 /* Flush occurring */
263 if (cbSrcStream == 0)
265 FIXME(".. Why do I need you?\n");
266 return S_OK;
269 /* trace removed for performance reasons */
270 /* TRACE("(%p), %llu -> %llu\n", pSample, tStart, tStop); */
272 /* Now, try to find a new header */
273 hr = FillBuffer(This, pSample);
274 if (hr != S_OK)
276 WARN("Failed with hres: %08x!\n", hr);
278 /* Unset progression if denied! */
279 if (hr == VFW_E_WRONG_STATE || hr == S_FALSE)
281 memcpy(This->header, pbSrcStream, 4);
282 This->Parser.pInputPin->rtCurrent = tStart;
283 This->position = tAviStart;
287 if (BYTES_FROM_MEDIATIME(tStop) >= This->EndOfFile || This->position >= This->Parser.mediaSeeking.llStop)
289 int i;
291 TRACE("End of file reached\n");
293 for (i = 0; i < This->Parser.cStreams; i++)
295 IPin* ppin;
297 hr = IPin_ConnectedTo(This->Parser.ppPins[i+1], &ppin);
298 if (SUCCEEDED(hr))
300 hr = IPin_EndOfStream(ppin);
301 IPin_Release(ppin);
303 if (FAILED(hr))
304 WARN("Error sending EndOfStream to pin %d (%x)\n", i, hr);
307 /* Force the pullpin thread to stop */
308 hr = S_FALSE;
311 return hr;
315 static HRESULT MPEGSplitter_query_accept(LPVOID iface, const AM_MEDIA_TYPE *pmt)
317 if (!IsEqualIID(&pmt->majortype, &MEDIATYPE_Stream))
318 return S_FALSE;
320 if (IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_MPEG1Audio))
321 return S_OK;
323 if (IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_MPEG1Video))
324 FIXME("MPEG-1 video streams not yet supported.\n");
325 else if (IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_MPEG1System))
326 FIXME("MPEG-1 system streams not yet supported.\n");
327 else if (IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_MPEG1VideoCD))
328 FIXME("MPEG-1 VideoCD streams not yet supported.\n");
330 return S_FALSE;
334 static HRESULT MPEGSplitter_init_audio(MPEGSplitterImpl *This, const BYTE *header, PIN_INFO *ppiOutput, AM_MEDIA_TYPE *pamt)
336 WAVEFORMATEX *format;
337 int bitrate_index;
338 int freq_index;
339 int mode_ext;
340 int emphasis;
341 int lsf = 1;
342 int mpeg1;
343 int layer;
344 int mode;
346 ZeroMemory(pamt, sizeof(*pamt));
347 ppiOutput->dir = PINDIR_OUTPUT;
348 ppiOutput->pFilter = (IBaseFilter*)This;
349 wsprintfW(ppiOutput->achName, wszAudioStream);
351 pamt->formattype = FORMAT_WaveFormatEx;
352 pamt->majortype = MEDIATYPE_Audio;
353 pamt->subtype = MEDIASUBTYPE_MPEG1AudioPayload;
355 pamt->lSampleSize = 0;
356 pamt->bFixedSizeSamples = FALSE;
357 pamt->bTemporalCompression = 0;
359 mpeg1 = (header[1]>>4)&0x1;
360 if (mpeg1)
361 lsf = ((header[1]>>3)&0x1)^1;
363 layer = 4-((header[1]>>1)&0x3);
364 bitrate_index = ((header[2]>>4)&0xf);
365 freq_index = ((header[2]>>2)&0x3) + (mpeg1?(lsf*3):6);
366 mode = ((header[3]>>6)&0x3);
367 mode_ext = ((header[3]>>4)&0x3);
368 emphasis = ((header[3]>>0)&0x3);
370 if (!bitrate_index)
372 /* Set to highest bitrate so samples will fit in for sure */
373 FIXME("Variable-bitrate audio not fully supported.\n");
374 bitrate_index = 15;
377 pamt->cbFormat = ((layer==3)? sizeof(MPEGLAYER3WAVEFORMAT) :
378 sizeof(MPEG1WAVEFORMAT));
379 pamt->pbFormat = CoTaskMemAlloc(pamt->cbFormat);
380 if (!pamt->pbFormat)
381 return E_OUTOFMEMORY;
382 ZeroMemory(pamt->pbFormat, pamt->cbFormat);
383 format = (WAVEFORMATEX*)pamt->pbFormat;
385 format->wFormatTag = ((layer == 3) ? WAVE_FORMAT_MPEGLAYER3 :
386 WAVE_FORMAT_MPEG);
387 format->nChannels = ((mode == 3) ? 1 : 2);
388 format->nSamplesPerSec = freqs[freq_index];
389 format->nAvgBytesPerSec = tabsel_123[lsf][layer-1][bitrate_index] * 1000 / 8;
391 if (layer == 3)
392 format->nBlockAlign = format->nAvgBytesPerSec * 8 * 144 /
393 (format->nSamplesPerSec<<lsf) + 1;
394 else if (layer == 2)
395 format->nBlockAlign = format->nAvgBytesPerSec * 8 * 144 /
396 format->nSamplesPerSec + 1;
397 else
398 format->nBlockAlign = 4 * (format->nAvgBytesPerSec * 8 * 12 / format->nSamplesPerSec + 1);
400 format->wBitsPerSample = 0;
402 if (layer == 3)
404 MPEGLAYER3WAVEFORMAT *mp3format = (MPEGLAYER3WAVEFORMAT*)format;
406 format->cbSize = MPEGLAYER3_WFX_EXTRA_BYTES;
408 mp3format->wID = MPEGLAYER3_ID_MPEG;
409 mp3format->fdwFlags = MPEGLAYER3_FLAG_PADDING_ON;
410 mp3format->nBlockSize = format->nBlockAlign;
411 mp3format->nFramesPerBlock = 1;
413 /* Beware the evil magic numbers. This struct is apparently horribly
414 * under-documented, and the only references I could find had it being
415 * set to this with no real explanation. It works fine though, so I'm
416 * not complaining (yet).
418 mp3format->nCodecDelay = 1393;
420 else
422 MPEG1WAVEFORMAT *mpgformat = (MPEG1WAVEFORMAT*)format;
424 format->cbSize = 22;
426 mpgformat->fwHeadLayer = ((layer == 1) ? ACM_MPEG_LAYER1 :
427 ((layer == 2) ? ACM_MPEG_LAYER2 :
428 ACM_MPEG_LAYER3));
429 mpgformat->dwHeadBitrate = format->nAvgBytesPerSec * 8;
430 mpgformat->fwHeadMode = ((mode == 3) ? ACM_MPEG_SINGLECHANNEL :
431 ((mode == 2) ? ACM_MPEG_DUALCHANNEL :
432 ((mode == 1) ? ACM_MPEG_JOINTSTEREO :
433 ACM_MPEG_STEREO)));
434 mpgformat->fwHeadModeExt = ((mode == 1) ? 0x0F : (1<<mode_ext));
435 mpgformat->wHeadEmphasis = emphasis + 1;
436 mpgformat->fwHeadFlags = ACM_MPEG_ID_MPEG1;
438 pamt->subtype.Data1 = format->wFormatTag;
440 TRACE("MPEG audio stream detected:\n"
441 "\tLayer %d (%#x)\n"
442 "\tFrequency: %d\n"
443 "\tChannels: %d (%d)\n"
444 "\tBytesPerSec: %d\n",
445 layer, format->wFormatTag, format->nSamplesPerSec,
446 format->nChannels, mode, format->nAvgBytesPerSec);
448 dump_AM_MEDIA_TYPE(pamt);
450 return S_OK;
454 static HRESULT MPEGSplitter_pre_connect(IPin *iface, IPin *pConnectPin, ALLOCATOR_PROPERTIES *props)
456 PullPin *pPin = (PullPin *)iface;
457 MPEGSplitterImpl *This = (MPEGSplitterImpl*)pPin->pin.pinInfo.pFilter;
458 HRESULT hr;
459 LONGLONG pos = 0; /* in bytes */
460 BYTE header[10];
461 int streamtype = 0;
462 LONGLONG total, avail;
463 AM_MEDIA_TYPE amt;
464 PIN_INFO piOutput;
466 IAsyncReader_Length(pPin->pReader, &total, &avail);
467 This->EndOfFile = total;
469 hr = IAsyncReader_SyncRead(pPin->pReader, pos, 4, header);
470 if (SUCCEEDED(hr))
471 pos += 4;
473 /* Skip ID3 v2 tag, if any */
474 if (SUCCEEDED(hr) && !strncmp("ID3", (char*)header, 3))
475 do {
476 UINT length;
477 hr = IAsyncReader_SyncRead(pPin->pReader, pos, 6, header + 4);
478 if (FAILED(hr))
479 break;
480 pos += 6;
481 TRACE("Found ID3 v2.%d.%d\n", header[3], header[4]);
482 length = (header[6] & 0x7F) << 21;
483 length += (header[7] & 0x7F) << 14;
484 length += (header[8] & 0x7F) << 7;
485 length += (header[9] & 0x7F);
486 TRACE("Length: %u\n", length);
487 pos += length;
489 /* Read the real header for the mpeg splitter */
490 hr = IAsyncReader_SyncRead(pPin->pReader, pos, 4, header);
491 if (SUCCEEDED(hr))
492 pos += 4;
493 TRACE("%x:%x:%x:%x\n", header[0], header[1], header[2], header[3]);
494 } while (0);
496 while(SUCCEEDED(hr) && !(streamtype=MPEGSplitter_head_check(header)))
498 TRACE("%x:%x:%x:%x\n", header[0], header[1], header[2], header[3]);
499 /* No valid header yet; shift by a byte and check again */
500 memmove(header, header+1, 3);
501 hr = IAsyncReader_SyncRead(pPin->pReader, pos++, 1, header + 3);
503 if (FAILED(hr))
504 return hr;
505 pos -= 4;
506 This->begin_offset = pos;
507 memcpy(This->header, header, 4);
509 This->seektable[0].bytepos = pos;
510 This->seektable[0].timepos = 0;
512 switch(streamtype)
514 case MPEG_AUDIO_HEADER:
516 LONGLONG duration = 0;
517 DWORD last_entry = 0;
519 DWORD ticks = GetTickCount();
521 hr = MPEGSplitter_init_audio(This, header, &piOutput, &amt);
522 if (SUCCEEDED(hr))
524 WAVEFORMATEX *format = (WAVEFORMATEX*)amt.pbFormat;
526 props->cbAlign = 1;
527 props->cbPrefix = 0;
528 /* Make the output buffer a multiple of the frame size */
529 props->cbBuffer = 0x4000 / format->nBlockAlign *
530 format->nBlockAlign;
531 props->cBuffers = 3;
532 hr = Parser_AddPin(&(This->Parser), &piOutput, props, &amt);
535 if (FAILED(hr))
537 if (amt.pbFormat)
538 CoTaskMemFree(amt.pbFormat);
539 ERR("Could not create pin for MPEG audio stream (%x)\n", hr);
540 break;
543 /* Check for idv1 tag, and remove it from stream if found */
544 hr = IAsyncReader_SyncRead(pPin->pReader, This->EndOfFile-128, 3, header+4);
545 if (FAILED(hr))
546 break;
547 if (!strncmp((char*)header+4, "TAG", 3))
548 This->EndOfFile -= 128;
549 This->Parser.pInputPin->rtStop = MEDIATIME_FROM_BYTES(This->EndOfFile);
550 This->Parser.pInputPin->rtStart = This->Parser.pInputPin->rtCurrent = MEDIATIME_FROM_BYTES(This->begin_offset);
552 /* http://mpgedit.org/mpgedit/mpeg_format/mpeghdr.htm has a whole read up on audio headers */
553 while (pos + 3 < This->EndOfFile)
555 LONGLONG length = 0;
556 hr = IAsyncReader_SyncRead(pPin->pReader, pos, 4, header);
557 if (hr != S_OK)
558 break;
559 while (parse_header(header, &length, &duration))
561 /* No valid header yet; shift by a byte and check again */
562 memmove(header, header+1, 3);
563 hr = IAsyncReader_SyncRead(pPin->pReader, pos++, 1, header + 3);
564 if (hr != S_OK || This->EndOfFile - pos < 4)
565 break;
567 pos += length;
569 if (This->seektable && (duration / SEEK_INTERVAL) > last_entry)
571 if (last_entry + 1 > duration / SEEK_INTERVAL)
573 ERR("Somehow skipped %d interval lengths instead of 1\n", (DWORD)(duration/SEEK_INTERVAL) - (last_entry + 1));
575 ++last_entry;
577 TRACE("Entry: %u\n", last_entry);
578 if (last_entry >= This->seek_entries)
580 This->seek_entries += 64;
581 This->seektable = CoTaskMemRealloc(This->seektable, (This->seek_entries)*sizeof(struct seek_entry));
583 This->seektable[last_entry].bytepos = pos;
584 This->seektable[last_entry].timepos = duration;
587 TRACE("Pos: %x%08x/%x%08x\n", (DWORD)(pos >> 32), (DWORD)pos, (DWORD)(This->EndOfFile>>32), (DWORD)This->EndOfFile);
589 hr = S_OK;
590 TRACE("Duration: %d seconds\n", (DWORD)(duration / 10000000));
591 TRACE("Parsing took %u ms\n", GetTickCount() - ticks);
592 This->duration = duration;
594 This->Parser.mediaSeeking.llCurrent = 0;
595 This->Parser.mediaSeeking.llDuration = duration;
596 This->Parser.mediaSeeking.llStop = duration;
597 break;
599 case MPEG_VIDEO_HEADER:
600 FIXME("MPEG video processing not yet supported!\n");
601 hr = E_FAIL;
602 break;
603 case MPEG_SYSTEM_HEADER:
604 FIXME("MPEG system streams not yet supported!\n");
605 hr = E_FAIL;
606 break;
608 default:
609 break;
611 This->position = 0;
613 return hr;
616 static HRESULT MPEGSplitter_cleanup(LPVOID iface)
618 MPEGSplitterImpl *This = (MPEGSplitterImpl*)iface;
620 TRACE("(%p)\n", This);
622 return S_OK;
625 static HRESULT MPEGSplitter_seek(IBaseFilter *iface)
627 MPEGSplitterImpl *This = (MPEGSplitterImpl*)iface;
628 PullPin *pPin = This->Parser.pInputPin;
629 LONGLONG newpos, timepos, bytepos;
630 HRESULT hr = S_OK;
631 BYTE header[4];
633 newpos = This->Parser.mediaSeeking.llCurrent;
635 if (newpos > This->duration)
637 WARN("Requesting position %x%08x beyond end of stream %x%08x\n", (DWORD)(newpos>>32), (DWORD)newpos, (DWORD)(This->duration>>32), (DWORD)This->duration);
638 return E_INVALIDARG;
641 if (This->position/1000000 == newpos/1000000)
643 TRACE("Requesting position %x%08x same as current position %x%08x\n", (DWORD)(newpos>>32), (DWORD)newpos, (DWORD)(This->position>>32), (DWORD)This->position);
644 return S_OK;
647 /* Position, cached */
648 bytepos = This->seektable[newpos / SEEK_INTERVAL].bytepos;
649 timepos = This->seektable[newpos / SEEK_INTERVAL].timepos;
651 hr = IAsyncReader_SyncRead(pPin->pReader, bytepos, 4, header);
652 while (bytepos + 3 < This->EndOfFile)
654 LONGLONG length = 0;
655 hr = IAsyncReader_SyncRead(pPin->pReader, bytepos, 4, header);
656 if (hr != S_OK || timepos >= newpos)
657 break;
659 while (parse_header(header, &length, &timepos) && bytepos + 3 < This->EndOfFile)
661 /* No valid header yet; shift by a byte and check again */
662 memmove(header, header+1, 3);
663 hr = IAsyncReader_SyncRead(pPin->pReader, ++bytepos, 1, header + 3);
664 if (hr != S_OK)
665 break;
667 bytepos += length;
668 TRACE("Pos: %x%08x/%x%08x\n", (DWORD)(bytepos >> 32), (DWORD)bytepos, (DWORD)(This->EndOfFile>>32), (DWORD)This->EndOfFile);
671 if (SUCCEEDED(hr))
673 PullPin *pin = This->Parser.pInputPin;
674 IPin *victim = NULL;
676 TRACE("Moving sound to %08u bytes!\n", (DWORD)bytepos);
678 EnterCriticalSection(&pin->thread_lock);
679 IPin_BeginFlush((IPin *)pin);
681 /* Make sure this is done while stopped, BeginFlush takes care of this */
682 EnterCriticalSection(&This->Parser.csFilter);
683 memcpy(This->header, header, 4);
684 IPin_ConnectedTo(This->Parser.ppPins[1], &victim);
685 if (victim)
687 IPin_NewSegment(victim, newpos, This->duration, pin->dRate);
688 IPin_Release(victim);
691 pin->rtStart = pin->rtCurrent = MEDIATIME_FROM_BYTES(bytepos);
692 pin->rtStop = MEDIATIME_FROM_BYTES((REFERENCE_TIME)This->EndOfFile);
693 This->seek = TRUE;
694 This->position = newpos;
695 LeaveCriticalSection(&This->Parser.csFilter);
697 TRACE("Done flushing\n");
698 IPin_EndFlush((IPin *)pin);
699 LeaveCriticalSection(&pin->thread_lock);
701 return hr;
704 static HRESULT MPEGSplitter_disconnect(LPVOID iface)
706 /* TODO: Find memory leaks etc */
707 return S_OK;
710 static HRESULT MPEGSplitter_first_request(LPVOID iface)
712 MPEGSplitterImpl *This = (MPEGSplitterImpl*)iface;
713 PullPin *pin = This->Parser.pInputPin;
714 HRESULT hr;
715 LONGLONG length;
716 IMediaSample *sample;
718 TRACE("Seeking? %d\n", This->seek);
719 assert(parse_header(This->header, &length, NULL) == S_OK);
721 if (pin->rtCurrent >= pin->rtStop)
723 /* Last sample has already been queued, request nothing more */
724 FIXME("Done!\n");
725 return S_OK;
728 hr = IMemAllocator_GetBuffer(pin->pAlloc, &sample, NULL, NULL, 0);
730 pin->rtNext = pin->rtCurrent;
731 if (SUCCEEDED(hr))
733 LONGLONG rtSampleStart = pin->rtNext;
734 /* Add 4 for the next header, which should hopefully work */
735 LONGLONG rtSampleStop = rtSampleStart + MEDIATIME_FROM_BYTES(length + 4);
737 if (rtSampleStop > pin->rtStop)
738 rtSampleStop = MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(pin->rtStop), pin->cbAlign));
740 hr = IMediaSample_SetTime(sample, &rtSampleStart, &rtSampleStop);
742 pin->rtCurrent = pin->rtNext;
743 pin->rtNext = rtSampleStop;
745 IMediaSample_SetPreroll(sample, FALSE);
746 IMediaSample_SetDiscontinuity(sample, This->seek);
747 IMediaSample_SetSyncPoint(sample, 1);
748 This->seek = 0;
750 hr = IAsyncReader_Request(pin->pReader, sample, 0);
752 if (FAILED(hr))
753 ERR("Horsemen of the apocalypse came to bring error 0x%08x\n", hr);
755 return hr;
758 static const IBaseFilterVtbl MPEGSplitter_Vtbl =
760 Parser_QueryInterface,
761 Parser_AddRef,
762 Parser_Release,
763 Parser_GetClassID,
764 Parser_Stop,
765 Parser_Pause,
766 Parser_Run,
767 Parser_GetState,
768 Parser_SetSyncSource,
769 Parser_GetSyncSource,
770 Parser_EnumPins,
771 Parser_FindPin,
772 Parser_QueryFilterInfo,
773 Parser_JoinFilterGraph,
774 Parser_QueryVendorInfo
777 HRESULT MPEGSplitter_create(IUnknown * pUnkOuter, LPVOID * ppv)
779 MPEGSplitterImpl *This;
780 HRESULT hr = E_FAIL;
782 TRACE("(%p, %p)\n", pUnkOuter, ppv);
784 *ppv = NULL;
786 if (pUnkOuter)
787 return CLASS_E_NOAGGREGATION;
789 This = CoTaskMemAlloc(sizeof(MPEGSplitterImpl));
790 if (!This)
791 return E_OUTOFMEMORY;
793 ZeroMemory(This, sizeof(MPEGSplitterImpl));
794 This->seektable = CoTaskMemAlloc(sizeof(struct seek_entry) * 64);
795 if (!This->seektable)
797 CoTaskMemFree(This);
798 return E_OUTOFMEMORY;
800 This->seek_entries = 64;
802 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);
803 if (FAILED(hr))
805 CoTaskMemFree(This);
806 return hr;
808 This->seek = 1;
810 /* Note: This memory is managed by the parser filter once created */
811 *ppv = (LPVOID)This;
813 return hr;