Synchronize with trunk.
[reactos.git] / dll / directx / quartz / mpegsplit.c
1 /*
2 * MPEG Splitter Filter
3 *
4 * Copyright 2003 Robert Shearman
5 * Copyright 2004-2005 Christian Costa
6 * Copyright 2007 Chris Robinson
7 * Copyright 2008 Maarten Lankhorst
8 *
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.
13 *
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.
18 *
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
22 */
23
24 #include <assert.h>
25 //#include <math.h>
26
27 #include "quartz_private.h"
28 #include "pin.h"
29
30 //#include "uuids.h"
31 #include <mmreg.h>
32 //#include "mmsystem.h"
33
34 //#include "winternl.h"
35
36 //#include "wine/unicode.h"
37 #include <wine/debug.h>
38
39 #include "parser.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
42
43 #define SEQUENCE_HEADER_CODE 0xB3
44 #define PACK_START_CODE 0xBA
45
46 #define SYSTEM_START_CODE 0xBB
47 #define AUDIO_ELEMENTARY_STREAM 0xC0
48 #define VIDEO_ELEMENTARY_STREAM 0xE0
49
50 #define MPEG_SYSTEM_HEADER 3
51 #define MPEG_VIDEO_HEADER 2
52 #define MPEG_AUDIO_HEADER 1
53 #define MPEG_NO_HEADER 0
54
55 typedef struct MPEGSplitterImpl
56 {
57 ParserImpl Parser;
58 LONGLONG EndOfFile;
59 LONGLONG position;
60 DWORD begin_offset;
61 BYTE header[4];
62
63 /* Whether we just seeked (or started playing) */
64 BOOL seek;
65 } MPEGSplitterImpl;
66
67 static inline MPEGSplitterImpl *impl_from_IMediaSeeking( IMediaSeeking *iface )
68 {
69 return CONTAINING_RECORD(iface, MPEGSplitterImpl, Parser.sourceSeeking.IMediaSeeking_iface);
70 }
71
72 static int MPEGSplitter_head_check(const BYTE *header)
73 {
74 /* If this is a possible start code, check for a system or video header */
75 if (header[0] == 0 && header[1] == 0 && header[2] == 1)
76 {
77 /* Check if we got a system or elementary stream start code */
78 if (header[3] == PACK_START_CODE ||
79 header[3] == VIDEO_ELEMENTARY_STREAM ||
80 header[3] == AUDIO_ELEMENTARY_STREAM)
81 return MPEG_SYSTEM_HEADER;
82
83 /* Check for a MPEG video sequence start code */
84 if (header[3] == SEQUENCE_HEADER_CODE)
85 return MPEG_VIDEO_HEADER;
86 }
87
88 /* This should give a good guess if we have an MPEG audio header */
89 if(header[0] == 0xff && ((header[1]>>5)&0x7) == 0x7 &&
90 ((header[1]>>1)&0x3) != 0 && ((header[2]>>4)&0xf) != 0xf &&
91 ((header[2]>>2)&0x3) != 0x3)
92 return MPEG_AUDIO_HEADER;
93
94 /* Nothing yet.. */
95 return MPEG_NO_HEADER;
96 }
97
98 static const WCHAR wszAudioStream[] = {'A','u','d','i','o',0};
99 static const WCHAR wszVideoStream[] = {'V','i','d','e','o',0};
100
101 static const DWORD freqs[10] = { 44100, 48000, 32000, 22050, 24000, 16000, 11025, 12000, 8000, 0 };
102
103 static const DWORD tabsel_123[2][3][16] = {
104 { {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,},
105 {0,32,48,56, 64, 80, 96,112,128,160,192,224,256,320,384,},
106 {0,32,40,48, 56, 64, 80, 96,112,128,160,192,224,256,320,} },
107
108 { {0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,},
109 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,},
110 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,} }
111 };
112
113 static HRESULT parse_header(BYTE *header, LONGLONG *plen, LONGLONG *pduration)
114 {
115 int bitrate_index, freq_index, lsf = 1, mpeg1, layer, padding, bitrate, length;
116 LONGLONG duration;
117
118 if (!(header[0] == 0xff && ((header[1]>>5)&0x7) == 0x7 &&
119 ((header[1]>>1)&0x3) != 0 && ((header[2]>>4)&0xf) != 0xf &&
120 ((header[2]>>2)&0x3) != 0x3))
121 {
122 FIXME("Not a valid header: %02x:%02x:%02x:%02x\n", header[0], header[1], header[2], header[3]);
123 return E_INVALIDARG;
124 }
125
126 mpeg1 = (header[1]>>4)&0x1;
127 if (mpeg1)
128 lsf = ((header[1]>>3)&0x1)^1;
129
130 layer = 4-((header[1]>>1)&0x3);
131 bitrate_index = ((header[2]>>4)&0xf);
132 freq_index = ((header[2]>>2)&0x3) + (mpeg1?(lsf*3):6);
133 padding = ((header[2]>>1)&0x1);
134
135 bitrate = tabsel_123[lsf][layer-1][bitrate_index] * 1000;
136 if (!bitrate)
137 {
138 FIXME("Not a valid header: %02x:%02x:%02x:%02x\n", header[0], header[1], header[2], header[3]);
139 return E_INVALIDARG;
140 }
141
142 if (layer == 1)
143 length = 4 * (12 * bitrate / freqs[freq_index] + padding);
144 else if (layer == 2)
145 length = 144 * bitrate / freqs[freq_index] + padding;
146 else if (layer == 3)
147 length = 144 * bitrate / (freqs[freq_index]<<lsf) + padding;
148 else
149 {
150 ERR("Impossible layer %d\n", layer);
151 return E_INVALIDARG;
152 }
153
154 duration = (ULONGLONG)10000000 * (ULONGLONG)(length) / (ULONGLONG)(bitrate/8);
155 *plen = length;
156 if (pduration)
157 *pduration += duration;
158 return S_OK;
159 }
160
161 static HRESULT FillBuffer(MPEGSplitterImpl *This, IMediaSample *pCurrentSample)
162 {
163 Parser_OutputPin * pOutputPin = unsafe_impl_Parser_OutputPin_from_IPin(This->Parser.ppPins[1]);
164 LONGLONG length = 0;
165 LONGLONG pos = BYTES_FROM_MEDIATIME(This->Parser.pInputPin->rtNext);
166 LONGLONG time = This->position, rtstop, rtstart;
167 HRESULT hr;
168 BYTE *fbuf = NULL;
169 DWORD len = IMediaSample_GetActualDataLength(pCurrentSample);
170
171 TRACE("Source length: %u\n", len);
172 IMediaSample_GetPointer(pCurrentSample, &fbuf);
173
174 /* Find the next valid header.. it <SHOULD> be right here */
175 hr = parse_header(fbuf, &length, &This->position);
176 assert(hr == S_OK);
177 IMediaSample_SetActualDataLength(pCurrentSample, length);
178
179 /* Queue the next sample */
180 if (length + 4 == len)
181 {
182 PullPin *pin = This->Parser.pInputPin;
183 LONGLONG stop = BYTES_FROM_MEDIATIME(pin->rtStop);
184
185 hr = S_OK;
186 memcpy(This->header, fbuf + length, 4);
187 while (FAILED(hr = parse_header(This->header, &length, NULL)))
188 {
189 memmove(This->header, This->header+1, 3);
190 if (pos + 4 >= stop)
191 break;
192 IAsyncReader_SyncRead(pin->pReader, ++pos, 1, This->header + 3);
193 }
194 pin->rtNext = MEDIATIME_FROM_BYTES(pos);
195
196 if (SUCCEEDED(hr))
197 {
198 /* Remove 4 for the last header, which should hopefully work */
199 IMediaSample *sample = NULL;
200 LONGLONG rtSampleStart = pin->rtNext - MEDIATIME_FROM_BYTES(4);
201 LONGLONG rtSampleStop = rtSampleStart + MEDIATIME_FROM_BYTES(length + 4);
202
203 if (rtSampleStop > pin->rtStop)
204 rtSampleStop = MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(pin->rtStop), pin->cbAlign));
205
206 hr = IMemAllocator_GetBuffer(pin->pAlloc, &sample, NULL, NULL, 0);
207 if (SUCCEEDED(hr))
208 {
209 IMediaSample_SetTime(sample, &rtSampleStart, &rtSampleStop);
210 IMediaSample_SetPreroll(sample, 0);
211 IMediaSample_SetDiscontinuity(sample, 0);
212 IMediaSample_SetSyncPoint(sample, 1);
213 hr = IAsyncReader_Request(pin->pReader, sample, 0);
214 if (SUCCEEDED(hr))
215 {
216 pin->rtCurrent = rtSampleStart;
217 pin->rtNext = rtSampleStop;
218 }
219 else
220 IMediaSample_Release(sample);
221 }
222 if (FAILED(hr))
223 FIXME("o_Ox%08x\n", hr);
224 }
225 }
226 /* If not, we're presumably at the end of file */
227
228 TRACE("Media time : %u.%03u\n", (DWORD)(This->position/10000000), (DWORD)((This->position/10000)%1000));
229
230 if (IMediaSample_IsDiscontinuity(pCurrentSample) == S_OK) {
231 IPin *victim;
232 EnterCriticalSection(&This->Parser.filter.csFilter);
233 pOutputPin->pin.pin.tStart = time;
234 pOutputPin->pin.pin.dRate = This->Parser.sourceSeeking.dRate;
235 hr = IPin_ConnectedTo(&pOutputPin->pin.pin.IPin_iface, &victim);
236 if (hr == S_OK)
237 {
238 hr = IPin_NewSegment(victim, time, This->Parser.sourceSeeking.llStop,
239 This->Parser.sourceSeeking.dRate);
240 if (hr != S_OK)
241 FIXME("NewSegment returns %08x\n", hr);
242 IPin_Release(victim);
243 }
244 LeaveCriticalSection(&This->Parser.filter.csFilter);
245 if (hr != S_OK)
246 return hr;
247 }
248 rtstart = (double)(time - pOutputPin->pin.pin.tStart) / pOutputPin->pin.pin.dRate;
249 rtstop = (double)(This->position - pOutputPin->pin.pin.tStart) / pOutputPin->pin.pin.dRate;
250 IMediaSample_SetTime(pCurrentSample, &rtstart, &rtstop);
251 IMediaSample_SetMediaTime(pCurrentSample, &time, &This->position);
252
253 hr = BaseOutputPinImpl_Deliver(&pOutputPin->pin, pCurrentSample);
254
255 if (hr != S_OK)
256 {
257 if (hr != S_FALSE)
258 TRACE("Error sending sample (%x)\n", hr);
259 else
260 TRACE("S_FALSE (%d), holding\n", IMediaSample_GetActualDataLength(pCurrentSample));
261 }
262
263 return hr;
264 }
265
266
267 static HRESULT MPEGSplitter_process_sample(LPVOID iface, IMediaSample * pSample, DWORD_PTR cookie)
268 {
269 MPEGSplitterImpl *This = iface;
270 BYTE *pbSrcStream;
271 DWORD cbSrcStream = 0;
272 REFERENCE_TIME tStart, tStop, tAviStart = This->position;
273 HRESULT hr;
274
275 hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
276 if (SUCCEEDED(hr))
277 {
278 cbSrcStream = IMediaSample_GetActualDataLength(pSample);
279 hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
280 }
281
282 /* Flush occurring */
283 if (cbSrcStream == 0)
284 {
285 FIXME(".. Why do I need you?\n");
286 return S_OK;
287 }
288
289 /* trace removed for performance reasons */
290 /* TRACE("(%p), %llu -> %llu\n", pSample, tStart, tStop); */
291
292 /* Now, try to find a new header */
293 hr = FillBuffer(This, pSample);
294 if (hr != S_OK)
295 {
296 WARN("Failed with hres: %08x!\n", hr);
297
298 /* Unset progression if denied! */
299 if (hr == VFW_E_WRONG_STATE || hr == S_FALSE)
300 {
301 memcpy(This->header, pbSrcStream, 4);
302 This->Parser.pInputPin->rtCurrent = tStart;
303 This->position = tAviStart;
304 }
305 }
306
307 if (BYTES_FROM_MEDIATIME(tStop) >= This->EndOfFile || This->position >= This->Parser.sourceSeeking.llStop)
308 {
309 unsigned int i;
310
311 TRACE("End of file reached\n");
312
313 for (i = 0; i < This->Parser.cStreams; i++)
314 {
315 IPin* ppin;
316
317 hr = IPin_ConnectedTo(This->Parser.ppPins[i+1], &ppin);
318 if (SUCCEEDED(hr))
319 {
320 hr = IPin_EndOfStream(ppin);
321 IPin_Release(ppin);
322 }
323 if (FAILED(hr))
324 WARN("Error sending EndOfStream to pin %u (%x)\n", i, hr);
325 }
326
327 /* Force the pullpin thread to stop */
328 hr = S_FALSE;
329 }
330
331 return hr;
332 }
333
334
335 static HRESULT MPEGSplitter_query_accept(LPVOID iface, const AM_MEDIA_TYPE *pmt)
336 {
337 if (!IsEqualIID(&pmt->majortype, &MEDIATYPE_Stream))
338 return S_FALSE;
339
340 if (IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_MPEG1Audio))
341 return S_OK;
342
343 if (IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_MPEG1Video))
344 FIXME("MPEG-1 video streams not yet supported.\n");
345 else if (IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_MPEG1System))
346 FIXME("MPEG-1 system streams not yet supported.\n");
347 else if (IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_MPEG1VideoCD))
348 FIXME("MPEG-1 VideoCD streams not yet supported.\n");
349 else FIXME("%s\n", debugstr_guid(&pmt->subtype));
350
351 return S_FALSE;
352 }
353
354
355 static HRESULT MPEGSplitter_init_audio(MPEGSplitterImpl *This, const BYTE *header, PIN_INFO *ppiOutput, AM_MEDIA_TYPE *pamt)
356 {
357 WAVEFORMATEX *format;
358 int bitrate_index;
359 int freq_index;
360 int mode_ext;
361 int emphasis;
362 int lsf = 1;
363 int mpeg1;
364 int layer;
365 int mode;
366
367 ZeroMemory(pamt, sizeof(*pamt));
368 ppiOutput->dir = PINDIR_OUTPUT;
369 ppiOutput->pFilter = (IBaseFilter*)This;
370 wsprintfW(ppiOutput->achName, wszAudioStream);
371
372 pamt->formattype = FORMAT_WaveFormatEx;
373 pamt->majortype = MEDIATYPE_Audio;
374 pamt->subtype = MEDIASUBTYPE_MPEG1AudioPayload;
375
376 pamt->lSampleSize = 0;
377 pamt->bFixedSizeSamples = FALSE;
378 pamt->bTemporalCompression = 0;
379
380 mpeg1 = (header[1]>>4)&0x1;
381 if (mpeg1)
382 lsf = ((header[1]>>3)&0x1)^1;
383
384 layer = 4-((header[1]>>1)&0x3);
385 bitrate_index = ((header[2]>>4)&0xf);
386 freq_index = ((header[2]>>2)&0x3) + (mpeg1?(lsf*3):6);
387 mode = ((header[3]>>6)&0x3);
388 mode_ext = ((header[3]>>4)&0x3);
389 emphasis = ((header[3]>>0)&0x3);
390
391 if (!bitrate_index)
392 {
393 /* Set to highest bitrate so samples will fit in for sure */
394 FIXME("Variable-bitrate audio not fully supported.\n");
395 bitrate_index = 15;
396 }
397
398 pamt->cbFormat = ((layer==3)? sizeof(MPEGLAYER3WAVEFORMAT) :
399 sizeof(MPEG1WAVEFORMAT));
400 pamt->pbFormat = CoTaskMemAlloc(pamt->cbFormat);
401 if (!pamt->pbFormat)
402 return E_OUTOFMEMORY;
403 ZeroMemory(pamt->pbFormat, pamt->cbFormat);
404 format = (WAVEFORMATEX*)pamt->pbFormat;
405
406 format->wFormatTag = ((layer == 3) ? WAVE_FORMAT_MPEGLAYER3 :
407 WAVE_FORMAT_MPEG);
408 format->nChannels = ((mode == 3) ? 1 : 2);
409 format->nSamplesPerSec = freqs[freq_index];
410 format->nAvgBytesPerSec = tabsel_123[lsf][layer-1][bitrate_index] * 1000 / 8;
411
412 if (layer == 3)
413 format->nBlockAlign = format->nAvgBytesPerSec * 8 * 144 /
414 (format->nSamplesPerSec<<lsf) + 1;
415 else if (layer == 2)
416 format->nBlockAlign = format->nAvgBytesPerSec * 8 * 144 /
417 format->nSamplesPerSec + 1;
418 else
419 format->nBlockAlign = 4 * (format->nAvgBytesPerSec * 8 * 12 / format->nSamplesPerSec + 1);
420
421 format->wBitsPerSample = 0;
422
423 if (layer == 3)
424 {
425 MPEGLAYER3WAVEFORMAT *mp3format = (MPEGLAYER3WAVEFORMAT*)format;
426
427 format->cbSize = MPEGLAYER3_WFX_EXTRA_BYTES;
428
429 mp3format->wID = MPEGLAYER3_ID_MPEG;
430 mp3format->fdwFlags = MPEGLAYER3_FLAG_PADDING_ON;
431 mp3format->nBlockSize = format->nBlockAlign;
432 mp3format->nFramesPerBlock = 1;
433
434 /* Beware the evil magic numbers. This struct is apparently horribly
435 * under-documented, and the only references I could find had it being
436 * set to this with no real explanation. It works fine though, so I'm
437 * not complaining (yet).
438 */
439 mp3format->nCodecDelay = 1393;
440 }
441 else
442 {
443 MPEG1WAVEFORMAT *mpgformat = (MPEG1WAVEFORMAT*)format;
444
445 format->cbSize = 22;
446
447 mpgformat->fwHeadLayer = ((layer == 1) ? ACM_MPEG_LAYER1 :
448 ((layer == 2) ? ACM_MPEG_LAYER2 :
449 ACM_MPEG_LAYER3));
450 mpgformat->dwHeadBitrate = format->nAvgBytesPerSec * 8;
451 mpgformat->fwHeadMode = ((mode == 3) ? ACM_MPEG_SINGLECHANNEL :
452 ((mode == 2) ? ACM_MPEG_DUALCHANNEL :
453 ((mode == 1) ? ACM_MPEG_JOINTSTEREO :
454 ACM_MPEG_STEREO)));
455 mpgformat->fwHeadModeExt = ((mode == 1) ? 0x0F : (1<<mode_ext));
456 mpgformat->wHeadEmphasis = emphasis + 1;
457 mpgformat->fwHeadFlags = ACM_MPEG_ID_MPEG1;
458 }
459 pamt->subtype.Data1 = format->wFormatTag;
460
461 TRACE("MPEG audio stream detected:\n"
462 "\tLayer %d (%#x)\n"
463 "\tFrequency: %d\n"
464 "\tChannels: %d (%d)\n"
465 "\tBytesPerSec: %d\n",
466 layer, format->wFormatTag, format->nSamplesPerSec,
467 format->nChannels, mode, format->nAvgBytesPerSec);
468
469 dump_AM_MEDIA_TYPE(pamt);
470
471 return S_OK;
472 }
473
474
475 static HRESULT MPEGSplitter_pre_connect(IPin *iface, IPin *pConnectPin, ALLOCATOR_PROPERTIES *props)
476 {
477 PullPin *pPin = impl_PullPin_from_IPin(iface);
478 MPEGSplitterImpl *This = (MPEGSplitterImpl*)pPin->pin.pinInfo.pFilter;
479 HRESULT hr;
480 LONGLONG pos = 0; /* in bytes */
481 BYTE header[10];
482 int streamtype = 0;
483 LONGLONG total, avail;
484 AM_MEDIA_TYPE amt;
485 PIN_INFO piOutput;
486
487 IAsyncReader_Length(pPin->pReader, &total, &avail);
488 This->EndOfFile = total;
489
490 hr = IAsyncReader_SyncRead(pPin->pReader, pos, 4, header);
491 if (SUCCEEDED(hr))
492 pos += 4;
493
494 /* Skip ID3 v2 tag, if any */
495 if (SUCCEEDED(hr) && !memcmp("ID3", header, 3))
496 do {
497 UINT length = 0;
498 hr = IAsyncReader_SyncRead(pPin->pReader, pos, 6, header + 4);
499 if (FAILED(hr))
500 break;
501 pos += 6;
502 TRACE("Found ID3 v2.%d.%d\n", header[3], header[4]);
503 if(header[3] <= 4 && header[4] != 0xff &&
504 (header[5]&0x0f) == 0 && (header[6]&0x80) == 0 &&
505 (header[7]&0x80) == 0 && (header[8]&0x80) == 0 &&
506 (header[9]&0x80) == 0)
507 {
508 length = (header[6]<<21) | (header[7]<<14) |
509 (header[8]<< 7) | (header[9] );
510 if((header[5]&0x10))
511 length += 10;
512 TRACE("Length: %u\n", length);
513 }
514 pos += length;
515
516 /* Read the real header for the mpeg splitter */
517 hr = IAsyncReader_SyncRead(pPin->pReader, pos, 4, header);
518 if (SUCCEEDED(hr))
519 pos += 4;
520 TRACE("%x:%x:%x:%x\n", header[0], header[1], header[2], header[3]);
521 } while (0);
522
523 while(SUCCEEDED(hr) && !(streamtype=MPEGSplitter_head_check(header)))
524 {
525 TRACE("%x:%x:%x:%x\n", header[0], header[1], header[2], header[3]);
526 /* No valid header yet; shift by a byte and check again */
527 memmove(header, header+1, 3);
528 hr = IAsyncReader_SyncRead(pPin->pReader, pos++, 1, header + 3);
529 }
530 if (FAILED(hr))
531 return hr;
532 pos -= 4;
533 This->begin_offset = pos;
534 memcpy(This->header, header, 4);
535
536 switch(streamtype)
537 {
538 case MPEG_AUDIO_HEADER:
539 {
540 LONGLONG duration = 0;
541 WAVEFORMATEX *format;
542
543 hr = MPEGSplitter_init_audio(This, header, &piOutput, &amt);
544 if (SUCCEEDED(hr))
545 {
546 format = (WAVEFORMATEX*)amt.pbFormat;
547
548 props->cbAlign = 1;
549 props->cbPrefix = 0;
550 /* Make the output buffer a multiple of the frame size */
551 props->cbBuffer = 0x4000 / format->nBlockAlign *
552 format->nBlockAlign;
553 props->cBuffers = 3;
554 hr = Parser_AddPin(&(This->Parser), &piOutput, props, &amt);
555 }
556
557 if (FAILED(hr))
558 {
559 if (amt.pbFormat)
560 CoTaskMemFree(amt.pbFormat);
561 ERR("Could not create pin for MPEG audio stream (%x)\n", hr);
562 break;
563 }
564
565 /* Check for idv1 tag, and remove it from stream if found */
566 hr = IAsyncReader_SyncRead(pPin->pReader, This->EndOfFile-128, 3, header);
567 if (FAILED(hr))
568 break;
569 if (!strncmp((char*)header, "TAG", 3))
570 This->EndOfFile -= 128;
571 This->Parser.pInputPin->rtStop = MEDIATIME_FROM_BYTES(This->EndOfFile);
572 This->Parser.pInputPin->rtStart = This->Parser.pInputPin->rtCurrent = MEDIATIME_FROM_BYTES(This->begin_offset);
573
574 duration = (This->EndOfFile-This->begin_offset) * 10000000 / format->nAvgBytesPerSec;
575 TRACE("Duration: %d seconds\n", (DWORD)(duration / 10000000));
576
577 This->Parser.sourceSeeking.llCurrent = 0;
578 This->Parser.sourceSeeking.llDuration = duration;
579 This->Parser.sourceSeeking.llStop = duration;
580 break;
581 }
582 case MPEG_VIDEO_HEADER:
583 FIXME("MPEG video processing not yet supported!\n");
584 hr = E_FAIL;
585 break;
586 case MPEG_SYSTEM_HEADER:
587 FIXME("MPEG system streams not yet supported!\n");
588 hr = E_FAIL;
589 break;
590
591 default:
592 break;
593 }
594 This->position = 0;
595
596 return hr;
597 }
598
599 static HRESULT MPEGSplitter_cleanup(LPVOID iface)
600 {
601 MPEGSplitterImpl *This = iface;
602
603 TRACE("(%p)\n", This);
604
605 return S_OK;
606 }
607
608 static HRESULT WINAPI MPEGSplitter_seek(IMediaSeeking *iface)
609 {
610 MPEGSplitterImpl *This = impl_from_IMediaSeeking(iface);
611 PullPin *pPin = This->Parser.pInputPin;
612 LONGLONG newpos, timepos, bytepos;
613 HRESULT hr = E_INVALIDARG;
614 BYTE header[4];
615
616 newpos = This->Parser.sourceSeeking.llCurrent;
617 if (This->position/1000000 == newpos/1000000)
618 {
619 TRACE("Requesting position %x%08x same as current position %x%08x\n", (DWORD)(newpos>>32), (DWORD)newpos, (DWORD)(This->position>>32), (DWORD)This->position);
620 return S_OK;
621 }
622
623 bytepos = This->begin_offset;
624 timepos = 0;
625 /* http://mpgedit.org/mpgedit/mpeg_format/mpeghdr.htm has a whole read up on audio headers */
626 while (bytepos + 3 < This->EndOfFile)
627 {
628 LONGLONG duration = timepos;
629 LONGLONG length = 0;
630 hr = IAsyncReader_SyncRead(pPin->pReader, bytepos, 4, header);
631 if (hr != S_OK)
632 break;
633 while ((hr=parse_header(header, &length, &duration)) != S_OK &&
634 bytepos + 4 < This->EndOfFile)
635 {
636 /* No valid header yet; shift by a byte and check again */
637 memmove(header, header+1, 3);
638 hr = IAsyncReader_SyncRead(pPin->pReader, ++bytepos + 3, 1, header + 3);
639 if (hr != S_OK)
640 break;
641 }
642 if (hr != S_OK || duration > newpos)
643 break;
644 bytepos += length;
645 timepos = duration;
646 }
647
648 if (SUCCEEDED(hr))
649 {
650 PullPin *pin = This->Parser.pInputPin;
651
652 TRACE("Moving sound to %08u bytes!\n", (DWORD)bytepos);
653
654 EnterCriticalSection(&pin->thread_lock);
655 IPin_BeginFlush(&pin->pin.IPin_iface);
656
657 /* Make sure this is done while stopped, BeginFlush takes care of this */
658 EnterCriticalSection(&This->Parser.filter.csFilter);
659 memcpy(This->header, header, 4);
660
661 pin->rtStart = pin->rtCurrent = MEDIATIME_FROM_BYTES(bytepos);
662 pin->rtStop = MEDIATIME_FROM_BYTES((REFERENCE_TIME)This->EndOfFile);
663 This->seek = TRUE;
664 This->position = newpos;
665 LeaveCriticalSection(&This->Parser.filter.csFilter);
666
667 TRACE("Done flushing\n");
668 IPin_EndFlush(&pin->pin.IPin_iface);
669 LeaveCriticalSection(&pin->thread_lock);
670 }
671 return hr;
672 }
673
674 static HRESULT MPEGSplitter_disconnect(LPVOID iface)
675 {
676 /* TODO: Find memory leaks etc */
677 return S_OK;
678 }
679
680 static HRESULT MPEGSplitter_first_request(LPVOID iface)
681 {
682 MPEGSplitterImpl *This = iface;
683 PullPin *pin = This->Parser.pInputPin;
684 HRESULT hr;
685 LONGLONG length;
686 IMediaSample *sample;
687
688 TRACE("Seeking? %d\n", This->seek);
689
690 hr = parse_header(This->header, &length, NULL);
691 assert(hr == S_OK);
692
693 if (pin->rtCurrent >= pin->rtStop)
694 {
695 /* Last sample has already been queued, request nothing more */
696 FIXME("Done!\n");
697 return S_OK;
698 }
699
700 hr = IMemAllocator_GetBuffer(pin->pAlloc, &sample, NULL, NULL, 0);
701
702 pin->rtNext = pin->rtCurrent;
703 if (SUCCEEDED(hr))
704 {
705 LONGLONG rtSampleStart = pin->rtNext;
706 /* Add 4 for the next header, which should hopefully work */
707 LONGLONG rtSampleStop = rtSampleStart + MEDIATIME_FROM_BYTES(length + 4);
708
709 if (rtSampleStop > pin->rtStop)
710 rtSampleStop = MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(pin->rtStop), pin->cbAlign));
711
712 hr = IMediaSample_SetTime(sample, &rtSampleStart, &rtSampleStop);
713
714 IMediaSample_SetPreroll(sample, FALSE);
715 IMediaSample_SetDiscontinuity(sample, TRUE);
716 IMediaSample_SetSyncPoint(sample, 1);
717 This->seek = 0;
718
719 hr = IAsyncReader_Request(pin->pReader, sample, 0);
720 if (SUCCEEDED(hr))
721 {
722 pin->rtCurrent = pin->rtNext;
723 pin->rtNext = rtSampleStop;
724 }
725 else
726 IMediaSample_Release(sample);
727 }
728 if (FAILED(hr))
729 ERR("Horsemen of the apocalypse came to bring error 0x%08x\n", hr);
730
731 return hr;
732 }
733
734 static const IBaseFilterVtbl MPEGSplitter_Vtbl =
735 {
736 Parser_QueryInterface,
737 Parser_AddRef,
738 Parser_Release,
739 Parser_GetClassID,
740 Parser_Stop,
741 Parser_Pause,
742 Parser_Run,
743 Parser_GetState,
744 Parser_SetSyncSource,
745 Parser_GetSyncSource,
746 Parser_EnumPins,
747 Parser_FindPin,
748 Parser_QueryFilterInfo,
749 Parser_JoinFilterGraph,
750 Parser_QueryVendorInfo
751 };
752
753 HRESULT MPEGSplitter_create(IUnknown * pUnkOuter, LPVOID * ppv)
754 {
755 MPEGSplitterImpl *This;
756 HRESULT hr = E_FAIL;
757
758 TRACE("(%p, %p)\n", pUnkOuter, ppv);
759
760 *ppv = NULL;
761
762 if (pUnkOuter)
763 return CLASS_E_NOAGGREGATION;
764
765 This = CoTaskMemAlloc(sizeof(MPEGSplitterImpl));
766 if (!This)
767 return E_OUTOFMEMORY;
768
769 ZeroMemory(This, sizeof(MPEGSplitterImpl));
770 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);
771 if (FAILED(hr))
772 {
773 CoTaskMemFree(This);
774 return hr;
775 }
776 This->seek = 1;
777
778 /* Note: This memory is managed by the parser filter once created */
779 *ppv = This;
780
781 return hr;
782 }