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