blob: 008a0a555098caf28468fa159f5ed511de25c816 [file] [log] [blame]
Chris Robinsonb3fab142007-04-24 05:28:14 -07001/*
2 * MPEG Splitter Filter
3 *
4 * Copyright 2003 Robert Shearman
5 * Copyright 2004-2005 Christian Costa
6 * Copyright 2007 Chris Robinson
Maarten Lankhorst2734e632008-04-09 12:07:14 -07007 * Copyright 2008 Maarten Lankhorst
Chris Robinsonb3fab142007-04-24 05:28:14 -07008 *
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"
Chris Robinsonb3fab142007-04-24 05:28:14 -070028#include "pin.h"
29
30#include "uuids.h"
31#include "mmreg.h"
Chris Robinsonb3fab142007-04-24 05:28:14 -070032#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
41WINE_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
Maarten Lankhorstc1a4acd2008-04-18 22:21:59 -070055#define SEEK_INTERVAL (ULONGLONG)(10 * 10000000) /* Add an entry every 10 seconds */
Maarten Lankhorst24cac932008-04-08 12:28:09 -070056
57struct seek_entry {
58 ULONGLONG bytepos;
59 ULONGLONG timepos;
60};
61
Chris Robinsonb3fab142007-04-24 05:28:14 -070062typedef struct MPEGSplitterImpl
63{
64 ParserImpl Parser;
Chris Robinsonb3fab142007-04-24 05:28:14 -070065 LONGLONG EndOfFile;
Maarten Lankhorstb0c6a342008-03-20 22:33:47 -070066 LONGLONG duration;
67 LONGLONG position;
Maarten Lankhorst3a398052008-04-22 13:57:11 -070068 DWORD begin_offset;
69 BYTE header[4];
70
71 /* Whether we just seeked (or started playing) */
Maarten Lankhorstb1b75242008-04-01 14:42:32 -070072 BOOL seek;
Maarten Lankhorst3a398052008-04-22 13:57:11 -070073
74 /* Seeking cache */
Maarten Lankhorst24cac932008-04-08 12:28:09 -070075 ULONG seek_entries;
76 struct seek_entry *seektable;
Chris Robinsonb3fab142007-04-24 05:28:14 -070077} MPEGSplitterImpl;
78
Aric Stewart602d44a2010-10-11 10:15:35 -050079static inline MPEGSplitterImpl *impl_from_IMediaSeeking( IMediaSeeking *iface )
80{
Aric Stewartd3c5b2c2012-03-24 11:43:09 -050081 return CONTAINING_RECORD(iface, MPEGSplitterImpl, Parser.sourceSeeking.IMediaSeeking_iface);
Aric Stewart602d44a2010-10-11 10:15:35 -050082}
83
Chris Robinsonb3fab142007-04-24 05:28:14 -070084static int MPEGSplitter_head_check(const BYTE *header)
85{
86 /* If this is a possible start code, check for a system or video header */
87 if (header[0] == 0 && header[1] == 0 && header[2] == 1)
88 {
89 /* Check if we got a system or elementary stream start code */
90 if (header[3] == PACK_START_CODE ||
91 header[3] == VIDEO_ELEMENTARY_STREAM ||
92 header[3] == AUDIO_ELEMENTARY_STREAM)
93 return MPEG_SYSTEM_HEADER;
94
95 /* Check for a MPEG video sequence start code */
96 if (header[3] == SEQUENCE_HEADER_CODE)
97 return MPEG_VIDEO_HEADER;
98 }
99
100 /* This should give a good guess if we have an MPEG audio header */
101 if(header[0] == 0xff && ((header[1]>>5)&0x7) == 0x7 &&
102 ((header[1]>>1)&0x3) != 0 && ((header[2]>>4)&0xf) != 0xf &&
103 ((header[2]>>2)&0x3) != 0x3)
104 return MPEG_AUDIO_HEADER;
105
106 /* Nothing yet.. */
107 return MPEG_NO_HEADER;
108}
109
Maarten Lankhorstb0c6a342008-03-20 22:33:47 -0700110static const WCHAR wszAudioStream[] = {'A','u','d','i','o',0};
111static const WCHAR wszVideoStream[] = {'V','i','d','e','o',0};
112
113static const DWORD freqs[10] = { 44100, 48000, 32000, 22050, 24000, 16000, 11025, 12000, 8000, 0 };
114
115static const DWORD tabsel_123[2][3][16] = {
116 { {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,},
117 {0,32,48,56, 64, 80, 96,112,128,160,192,224,256,320,384,},
118 {0,32,40,48, 56, 64, 80, 96,112,128,160,192,224,256,320,} },
119
120 { {0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,},
121 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,},
122 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,} }
123};
124
Maarten Lankhorstb0c6a342008-03-20 22:33:47 -0700125static HRESULT parse_header(BYTE *header, LONGLONG *plen, LONGLONG *pduration)
126{
Maarten Lankhorst3a398052008-04-22 13:57:11 -0700127 LONGLONG duration;
Maarten Lankhorstb0c6a342008-03-20 22:33:47 -0700128
Andrew Talbot523552f2008-04-23 22:13:04 +0100129 int bitrate_index, freq_index, lsf = 1, mpeg1, layer, padding, bitrate, length;
Maarten Lankhorst12e132e2011-09-26 17:34:44 +0200130 if (header[0] != 0xff)
131 return E_INVALIDARG;
Maarten Lankhorstb0c6a342008-03-20 22:33:47 -0700132
Maarten Lankhorst12e132e2011-09-26 17:34:44 +0200133 if (!(((header[1]>>5)&0x7) == 0x7 &&
Maarten Lankhorstb0c6a342008-03-20 22:33:47 -0700134 ((header[1]>>1)&0x3) != 0 && ((header[2]>>4)&0xf) != 0xf &&
135 ((header[2]>>2)&0x3) != 0x3))
136 {
Maarten Lankhorst12e132e2011-09-26 17:34:44 +0200137 FIXME("Not a valid header: %02x:%02x\n", header[1], header[2]);
Maarten Lankhorstb0c6a342008-03-20 22:33:47 -0700138 return E_INVALIDARG;
139 }
140
141 mpeg1 = (header[1]>>4)&0x1;
142 if (mpeg1)
143 lsf = ((header[1]>>3)&0x1)^1;
144
145 layer = 4-((header[1]>>1)&0x3);
146 bitrate_index = ((header[2]>>4)&0xf);
147 freq_index = ((header[2]>>2)&0x3) + (mpeg1?(lsf*3):6);
148 padding = ((header[2]>>1)&0x1);
Maarten Lankhorstb0c6a342008-03-20 22:33:47 -0700149
150 bitrate = tabsel_123[lsf][layer-1][bitrate_index] * 1000;
Maarten Lankhorst12e132e2011-09-26 17:34:44 +0200151 if (!bitrate)
Maarten Lankhorstb1b75242008-04-01 14:42:32 -0700152 {
Maarten Lankhorst4bc44572008-04-07 16:11:36 -0700153 FIXME("Not a valid header: %02x:%02x:%02x:%02x\n", header[0], header[1], header[2], header[3]);
Maarten Lankhorstb1b75242008-04-01 14:42:32 -0700154 return E_INVALIDARG;
155 }
156
157
Maarten Lankhorstb0c6a342008-03-20 22:33:47 -0700158 if (layer == 3 || layer == 2)
159 length = 144 * bitrate / freqs[freq_index] + padding;
160 else
161 length = 4 * (12 * bitrate / freqs[freq_index] + padding);
162
163 duration = (ULONGLONG)10000000 * (ULONGLONG)(length) / (ULONGLONG)(bitrate/8);
164 *plen = length;
Maarten Lankhorst3a398052008-04-22 13:57:11 -0700165 if (pduration)
166 *pduration += duration;
Maarten Lankhorstb0c6a342008-03-20 22:33:47 -0700167 return S_OK;
168}
169
Maarten Lankhorst3a398052008-04-22 13:57:11 -0700170static HRESULT FillBuffer(MPEGSplitterImpl *This, IMediaSample *pCurrentSample)
Maarten Lankhorstb0c6a342008-03-20 22:33:47 -0700171{
172 Parser_OutputPin * pOutputPin = (Parser_OutputPin*)This->Parser.ppPins[1];
173 LONGLONG length = 0;
Maarten Lankhorst3a398052008-04-22 13:57:11 -0700174 LONGLONG pos = BYTES_FROM_MEDIATIME(This->Parser.pInputPin->rtNext);
Maarten Lankhorst8c425952010-11-07 15:02:49 +0100175 LONGLONG time = This->position, rtstop, rtstart;
Maarten Lankhorst3a398052008-04-22 13:57:11 -0700176 HRESULT hr;
177 BYTE *fbuf = NULL;
178 DWORD len = IMediaSample_GetActualDataLength(pCurrentSample);
Maarten Lankhorstb0c6a342008-03-20 22:33:47 -0700179
Maarten Lankhorst3a398052008-04-22 13:57:11 -0700180 TRACE("Source length: %u\n", len);
181 IMediaSample_GetPointer(pCurrentSample, &fbuf);
Maarten Lankhorstb0c6a342008-03-20 22:33:47 -0700182
183 /* Find the next valid header.. it <SHOULD> be right here */
Michael Stefaniuca948e5e2011-10-07 13:26:51 +0200184 hr = parse_header(fbuf, &length, &This->position);
185 assert(hr == S_OK);
Maarten Lankhorst3a398052008-04-22 13:57:11 -0700186 IMediaSample_SetActualDataLength(pCurrentSample, length);
Maarten Lankhorstb0c6a342008-03-20 22:33:47 -0700187
Maarten Lankhorst0f73bba2008-04-30 14:34:07 -0700188 /* Queue the next sample */
Maarten Lankhorst3a398052008-04-22 13:57:11 -0700189 if (length + 4 == len)
Maarten Lankhorstb0c6a342008-03-20 22:33:47 -0700190 {
Maarten Lankhorst3a398052008-04-22 13:57:11 -0700191 PullPin *pin = This->Parser.pInputPin;
192 LONGLONG stop = BYTES_FROM_MEDIATIME(pin->rtStop);
193
194 hr = S_OK;
195 memcpy(This->header, fbuf + length, 4);
196 while (FAILED(hr = parse_header(This->header, &length, NULL)))
197 {
198 memmove(This->header, This->header+1, 3);
199 if (pos + 4 >= stop)
200 break;
201 IAsyncReader_SyncRead(pin->pReader, ++pos, 1, This->header + 3);
202 }
203 pin->rtNext = MEDIATIME_FROM_BYTES(pos);
204
205 if (SUCCEEDED(hr))
206 {
207 /* Remove 4 for the last header, which should hopefully work */
208 IMediaSample *sample = NULL;
209 LONGLONG rtSampleStart = pin->rtNext - MEDIATIME_FROM_BYTES(4);
210 LONGLONG rtSampleStop = rtSampleStart + MEDIATIME_FROM_BYTES(length + 4);
211
Maarten Lankhorst3a398052008-04-22 13:57:11 -0700212 if (rtSampleStop > pin->rtStop)
213 rtSampleStop = MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(pin->rtStop), pin->cbAlign));
214
Juan Lange73f31a2009-08-27 09:04:50 -0700215 hr = IMemAllocator_GetBuffer(pin->pAlloc, &sample, NULL, NULL, 0);
Maarten Lankhorst3a398052008-04-22 13:57:11 -0700216 if (SUCCEEDED(hr))
Juan Lange73f31a2009-08-27 09:04:50 -0700217 {
218 IMediaSample_SetTime(sample, &rtSampleStart, &rtSampleStop);
219 IMediaSample_SetPreroll(sample, 0);
220 IMediaSample_SetDiscontinuity(sample, 0);
221 IMediaSample_SetSyncPoint(sample, 1);
Maarten Lankhorst3a398052008-04-22 13:57:11 -0700222 hr = IAsyncReader_Request(pin->pReader, sample, 0);
Erich Hoovercd4ee692011-09-11 14:10:42 -0600223 if (SUCCEEDED(hr))
224 {
225 pin->rtCurrent = rtSampleStart;
226 pin->rtNext = rtSampleStop;
227 }
228 else
229 IMediaSample_Release(sample);
Juan Lange73f31a2009-08-27 09:04:50 -0700230 }
Maarten Lankhorst3a398052008-04-22 13:57:11 -0700231 if (FAILED(hr))
232 FIXME("o_Ox%08x\n", hr);
233 }
Maarten Lankhorstb0c6a342008-03-20 22:33:47 -0700234 }
Maarten Lankhorst3a398052008-04-22 13:57:11 -0700235 /* If not, we're presumably at the end of file */
236
237 TRACE("Media time : %u.%03u\n", (DWORD)(This->position/10000000), (DWORD)((This->position/10000)%1000));
Maarten Lankhorstb0c6a342008-03-20 22:33:47 -0700238
Maarten Lankhorst8c425952010-11-07 15:02:49 +0100239 if (IMediaSample_IsDiscontinuity(pCurrentSample) == S_OK) {
240 IPin *victim;
241 EnterCriticalSection(&This->Parser.filter.csFilter);
242 pOutputPin->pin.pin.tStart = time;
243 pOutputPin->pin.pin.dRate = This->Parser.sourceSeeking.dRate;
244 hr = IPin_ConnectedTo((IPin *)pOutputPin, &victim);
245 if (hr == S_OK)
246 {
247 hr = IPin_NewSegment(victim, time, This->Parser.sourceSeeking.llStop,
248 This->Parser.sourceSeeking.dRate);
249 if (hr != S_OK)
250 FIXME("NewSegment returns %08x\n", hr);
251 IPin_Release(victim);
252 }
253 LeaveCriticalSection(&This->Parser.filter.csFilter);
254 if (hr != S_OK)
255 return hr;
256 }
257 rtstart = (double)(time - pOutputPin->pin.pin.tStart) / pOutputPin->pin.pin.dRate;
258 rtstop = (double)(This->position - pOutputPin->pin.pin.tStart) / pOutputPin->pin.pin.dRate;
259 IMediaSample_SetTime(pCurrentSample, &rtstart, &rtstop);
260 IMediaSample_SetMediaTime(pCurrentSample, &time, &This->position);
Maarten Lankhorstb0c6a342008-03-20 22:33:47 -0700261
Aric Stewart5c1409b2010-10-05 14:37:56 -0500262 hr = BaseOutputPinImpl_Deliver((BaseOutputPin*)&pOutputPin->pin, pCurrentSample);
Maarten Lankhorstb1b75242008-04-01 14:42:32 -0700263
Maarten Lankhorst4bc44572008-04-07 16:11:36 -0700264 if (hr != S_OK)
Maarten Lankhorstb1b75242008-04-01 14:42:32 -0700265 {
Maarten Lankhorst4bc44572008-04-07 16:11:36 -0700266 if (hr != S_FALSE)
267 TRACE("Error sending sample (%x)\n", hr);
268 else
Maarten Lankhorst3a398052008-04-22 13:57:11 -0700269 TRACE("S_FALSE (%d), holding\n", IMediaSample_GetActualDataLength(pCurrentSample));
Maarten Lankhorstb1b75242008-04-01 14:42:32 -0700270 }
Maarten Lankhorst4bc44572008-04-07 16:11:36 -0700271
Maarten Lankhorstb0c6a342008-03-20 22:33:47 -0700272 return hr;
273}
274
Chris Robinsonb3fab142007-04-24 05:28:14 -0700275
Maarten Lankhorsta0224672008-04-18 19:30:25 -0700276static HRESULT MPEGSplitter_process_sample(LPVOID iface, IMediaSample * pSample, DWORD_PTR cookie)
Chris Robinsonb3fab142007-04-24 05:28:14 -0700277{
Michael Stefaniuccc7fc4a2009-01-29 11:14:55 +0100278 MPEGSplitterImpl *This = iface;
Maarten Lankhorstb0c6a342008-03-20 22:33:47 -0700279 BYTE *pbSrcStream;
Chris Robinsonb3fab142007-04-24 05:28:14 -0700280 DWORD cbSrcStream = 0;
Maarten Lankhorst98f0b5f2008-05-09 16:38:29 -0700281 REFERENCE_TIME tStart, tStop, tAviStart = This->position;
Maarten Lankhorstb0c6a342008-03-20 22:33:47 -0700282 HRESULT hr;
Chris Robinsonb3fab142007-04-24 05:28:14 -0700283
Chris Robinsonb3fab142007-04-24 05:28:14 -0700284 hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
285 if (SUCCEEDED(hr))
286 {
287 cbSrcStream = IMediaSample_GetActualDataLength(pSample);
288 hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
289 }
290
Francois Gouget633ee952008-05-06 20:01:59 +0200291 /* Flush occurring */
Maarten Lankhorst3a398052008-04-22 13:57:11 -0700292 if (cbSrcStream == 0)
293 {
294 FIXME(".. Why do I need you?\n");
295 return S_OK;
296 }
297
Chris Robinsonb3fab142007-04-24 05:28:14 -0700298 /* trace removed for performance reasons */
299 /* TRACE("(%p), %llu -> %llu\n", pSample, tStart, tStop); */
300
Maarten Lankhorstb0c6a342008-03-20 22:33:47 -0700301 /* Now, try to find a new header */
Maarten Lankhorst3a398052008-04-22 13:57:11 -0700302 hr = FillBuffer(This, pSample);
303 if (hr != S_OK)
Chris Robinsonb3fab142007-04-24 05:28:14 -0700304 {
Maarten Lankhorst3a398052008-04-22 13:57:11 -0700305 WARN("Failed with hres: %08x!\n", hr);
Maarten Lankhorst2734e632008-04-09 12:07:14 -0700306
Maarten Lankhorst0f73bba2008-04-30 14:34:07 -0700307 /* Unset progression if denied! */
308 if (hr == VFW_E_WRONG_STATE || hr == S_FALSE)
Maarten Lankhorst714e8072008-03-28 13:10:20 -0700309 {
Maarten Lankhorst0f73bba2008-04-30 14:34:07 -0700310 memcpy(This->header, pbSrcStream, 4);
311 This->Parser.pInputPin->rtCurrent = tStart;
Maarten Lankhorst98f0b5f2008-05-09 16:38:29 -0700312 This->position = tAviStart;
Maarten Lankhorst714e8072008-03-28 13:10:20 -0700313 }
Chris Robinsonb3fab142007-04-24 05:28:14 -0700314 }
315
Aric Stewart602d44a2010-10-11 10:15:35 -0500316 if (BYTES_FROM_MEDIATIME(tStop) >= This->EndOfFile || This->position >= This->Parser.sourceSeeking.llStop)
Chris Robinsonb3fab142007-04-24 05:28:14 -0700317 {
Andrew Talbota19ff5f2008-11-04 22:22:53 +0000318 unsigned int i;
Chris Robinsonb3fab142007-04-24 05:28:14 -0700319
Maarten Lankhorstb0c6a342008-03-20 22:33:47 -0700320 TRACE("End of file reached\n");
Chris Robinsonb3fab142007-04-24 05:28:14 -0700321
Chris Robinsonb3fab142007-04-24 05:28:14 -0700322 for (i = 0; i < This->Parser.cStreams; i++)
323 {
324 IPin* ppin;
Chris Robinsonb3fab142007-04-24 05:28:14 -0700325
326 hr = IPin_ConnectedTo(This->Parser.ppPins[i+1], &ppin);
327 if (SUCCEEDED(hr))
328 {
329 hr = IPin_EndOfStream(ppin);
330 IPin_Release(ppin);
331 }
332 if (FAILED(hr))
Andrew Talbota19ff5f2008-11-04 22:22:53 +0000333 WARN("Error sending EndOfStream to pin %u (%x)\n", i, hr);
Chris Robinsonb3fab142007-04-24 05:28:14 -0700334 }
335
336 /* Force the pullpin thread to stop */
337 hr = S_FALSE;
338 }
339
340 return hr;
341}
342
343
344static HRESULT MPEGSplitter_query_accept(LPVOID iface, const AM_MEDIA_TYPE *pmt)
345{
346 if (!IsEqualIID(&pmt->majortype, &MEDIATYPE_Stream))
347 return S_FALSE;
348
349 if (IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_MPEG1Audio))
350 return S_OK;
351
352 if (IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_MPEG1Video))
353 FIXME("MPEG-1 video streams not yet supported.\n");
354 else if (IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_MPEG1System))
355 FIXME("MPEG-1 system streams not yet supported.\n");
356 else if (IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_MPEG1VideoCD))
357 FIXME("MPEG-1 VideoCD streams not yet supported.\n");
Maarten Lankhorst12e132e2011-09-26 17:34:44 +0200358 else FIXME("%s\n", debugstr_guid(&pmt->subtype));
Chris Robinsonb3fab142007-04-24 05:28:14 -0700359
360 return S_FALSE;
361}
362
363
Chris Robinsonb3fab142007-04-24 05:28:14 -0700364static HRESULT MPEGSplitter_init_audio(MPEGSplitterImpl *This, const BYTE *header, PIN_INFO *ppiOutput, AM_MEDIA_TYPE *pamt)
365{
Chris Robinsonb3fab142007-04-24 05:28:14 -0700366 WAVEFORMATEX *format;
367 int bitrate_index;
368 int freq_index;
369 int mode_ext;
370 int emphasis;
Chris Robinsonb3fab142007-04-24 05:28:14 -0700371 int lsf = 1;
372 int mpeg1;
373 int layer;
374 int mode;
375
376 ZeroMemory(pamt, sizeof(*pamt));
377 ppiOutput->dir = PINDIR_OUTPUT;
378 ppiOutput->pFilter = (IBaseFilter*)This;
379 wsprintfW(ppiOutput->achName, wszAudioStream);
380
Andrew Talbot442f29a2008-03-12 20:32:07 +0000381 pamt->formattype = FORMAT_WaveFormatEx;
382 pamt->majortype = MEDIATYPE_Audio;
383 pamt->subtype = MEDIASUBTYPE_MPEG1AudioPayload;
Chris Robinsonb3fab142007-04-24 05:28:14 -0700384
385 pamt->lSampleSize = 0;
Maarten Lankhorstb0c6a342008-03-20 22:33:47 -0700386 pamt->bFixedSizeSamples = FALSE;
Chris Robinsonb3fab142007-04-24 05:28:14 -0700387 pamt->bTemporalCompression = 0;
388
389 mpeg1 = (header[1]>>4)&0x1;
390 if (mpeg1)
391 lsf = ((header[1]>>3)&0x1)^1;
392
393 layer = 4-((header[1]>>1)&0x3);
394 bitrate_index = ((header[2]>>4)&0xf);
395 freq_index = ((header[2]>>2)&0x3) + (mpeg1?(lsf*3):6);
Chris Robinsonb3fab142007-04-24 05:28:14 -0700396 mode = ((header[3]>>6)&0x3);
397 mode_ext = ((header[3]>>4)&0x3);
398 emphasis = ((header[3]>>0)&0x3);
399
Maarten Lankhorstb0c6a342008-03-20 22:33:47 -0700400 if (!bitrate_index)
401 {
402 /* Set to highest bitrate so samples will fit in for sure */
403 FIXME("Variable-bitrate audio not fully supported.\n");
404 bitrate_index = 15;
405 }
406
Chris Robinsonb3fab142007-04-24 05:28:14 -0700407 pamt->cbFormat = ((layer==3)? sizeof(MPEGLAYER3WAVEFORMAT) :
408 sizeof(MPEG1WAVEFORMAT));
409 pamt->pbFormat = CoTaskMemAlloc(pamt->cbFormat);
410 if (!pamt->pbFormat)
411 return E_OUTOFMEMORY;
412 ZeroMemory(pamt->pbFormat, pamt->cbFormat);
413 format = (WAVEFORMATEX*)pamt->pbFormat;
414
415 format->wFormatTag = ((layer == 3) ? WAVE_FORMAT_MPEGLAYER3 :
416 WAVE_FORMAT_MPEG);
417 format->nChannels = ((mode == 3) ? 1 : 2);
418 format->nSamplesPerSec = freqs[freq_index];
419 format->nAvgBytesPerSec = tabsel_123[lsf][layer-1][bitrate_index] * 1000 / 8;
Chris Robinsonb3fab142007-04-24 05:28:14 -0700420
421 if (layer == 3)
422 format->nBlockAlign = format->nAvgBytesPerSec * 8 * 144 /
Maarten Lankhorstb0c6a342008-03-20 22:33:47 -0700423 (format->nSamplesPerSec<<lsf) + 1;
Chris Robinsonb3fab142007-04-24 05:28:14 -0700424 else if (layer == 2)
425 format->nBlockAlign = format->nAvgBytesPerSec * 8 * 144 /
Maarten Lankhorstb0c6a342008-03-20 22:33:47 -0700426 format->nSamplesPerSec + 1;
Chris Robinsonb3fab142007-04-24 05:28:14 -0700427 else
Maarten Lankhorstb0c6a342008-03-20 22:33:47 -0700428 format->nBlockAlign = 4 * (format->nAvgBytesPerSec * 8 * 12 / format->nSamplesPerSec + 1);
Chris Robinsonb3fab142007-04-24 05:28:14 -0700429
430 format->wBitsPerSample = 0;
431
432 if (layer == 3)
433 {
434 MPEGLAYER3WAVEFORMAT *mp3format = (MPEGLAYER3WAVEFORMAT*)format;
435
436 format->cbSize = MPEGLAYER3_WFX_EXTRA_BYTES;
437
438 mp3format->wID = MPEGLAYER3_ID_MPEG;
Maarten Lankhorstb0c6a342008-03-20 22:33:47 -0700439 mp3format->fdwFlags = MPEGLAYER3_FLAG_PADDING_ON;
Chris Robinsonb3fab142007-04-24 05:28:14 -0700440 mp3format->nBlockSize = format->nBlockAlign;
441 mp3format->nFramesPerBlock = 1;
442
443 /* Beware the evil magic numbers. This struct is apparently horribly
444 * under-documented, and the only references I could find had it being
445 * set to this with no real explanation. It works fine though, so I'm
446 * not complaining (yet).
447 */
448 mp3format->nCodecDelay = 1393;
449 }
450 else
451 {
452 MPEG1WAVEFORMAT *mpgformat = (MPEG1WAVEFORMAT*)format;
453
454 format->cbSize = 22;
455
456 mpgformat->fwHeadLayer = ((layer == 1) ? ACM_MPEG_LAYER1 :
457 ((layer == 2) ? ACM_MPEG_LAYER2 :
458 ACM_MPEG_LAYER3));
459 mpgformat->dwHeadBitrate = format->nAvgBytesPerSec * 8;
460 mpgformat->fwHeadMode = ((mode == 3) ? ACM_MPEG_SINGLECHANNEL :
461 ((mode == 2) ? ACM_MPEG_DUALCHANNEL :
462 ((mode == 1) ? ACM_MPEG_JOINTSTEREO :
463 ACM_MPEG_STEREO)));
464 mpgformat->fwHeadModeExt = ((mode == 1) ? 0x0F : (1<<mode_ext));
465 mpgformat->wHeadEmphasis = emphasis + 1;
466 mpgformat->fwHeadFlags = ACM_MPEG_ID_MPEG1;
467 }
468 pamt->subtype.Data1 = format->wFormatTag;
469
470 TRACE("MPEG audio stream detected:\n"
471 "\tLayer %d (%#x)\n"
472 "\tFrequency: %d\n"
473 "\tChannels: %d (%d)\n"
474 "\tBytesPerSec: %d\n",
475 layer, format->wFormatTag, format->nSamplesPerSec,
476 format->nChannels, mode, format->nAvgBytesPerSec);
477
478 dump_AM_MEDIA_TYPE(pamt);
479
480 return S_OK;
481}
482
483
Maarten Lankhorst3a398052008-04-22 13:57:11 -0700484static HRESULT MPEGSplitter_pre_connect(IPin *iface, IPin *pConnectPin, ALLOCATOR_PROPERTIES *props)
Chris Robinsonb3fab142007-04-24 05:28:14 -0700485{
Aric Stewart018161e2012-04-04 08:27:25 -0500486 PullPin *pPin = impl_PullPin_from_IPin(iface);
Chris Robinsonb3fab142007-04-24 05:28:14 -0700487 MPEGSplitterImpl *This = (MPEGSplitterImpl*)pPin->pin.pinInfo.pFilter;
Chris Robinsonb3fab142007-04-24 05:28:14 -0700488 HRESULT hr;
489 LONGLONG pos = 0; /* in bytes */
Maarten Lankhorst6c1d0892008-03-13 20:24:58 -0700490 BYTE header[10];
Chris Robinsonb3fab142007-04-24 05:28:14 -0700491 int streamtype = 0;
492 LONGLONG total, avail;
493 AM_MEDIA_TYPE amt;
494 PIN_INFO piOutput;
495
496 IAsyncReader_Length(pPin->pReader, &total, &avail);
497 This->EndOfFile = total;
498
Maarten Lankhorst6c1d0892008-03-13 20:24:58 -0700499 hr = IAsyncReader_SyncRead(pPin->pReader, pos, 4, header);
Chris Robinsonb3fab142007-04-24 05:28:14 -0700500 if (SUCCEEDED(hr))
501 pos += 4;
502
Maarten Lankhorst6c1d0892008-03-13 20:24:58 -0700503 /* Skip ID3 v2 tag, if any */
Rob Shearmana35431a2008-08-17 18:33:17 +0100504 if (SUCCEEDED(hr) && !memcmp("ID3", header, 3))
Maarten Lankhorst6c1d0892008-03-13 20:24:58 -0700505 do {
506 UINT length;
507 hr = IAsyncReader_SyncRead(pPin->pReader, pos, 6, header + 4);
508 if (FAILED(hr))
509 break;
510 pos += 6;
511 TRACE("Found ID3 v2.%d.%d\n", header[3], header[4]);
512 length = (header[6] & 0x7F) << 21;
513 length += (header[7] & 0x7F) << 14;
514 length += (header[8] & 0x7F) << 7;
515 length += (header[9] & 0x7F);
516 TRACE("Length: %u\n", length);
517 pos += length;
518
519 /* Read the real header for the mpeg splitter */
520 hr = IAsyncReader_SyncRead(pPin->pReader, pos, 4, header);
521 if (SUCCEEDED(hr))
522 pos += 4;
523 TRACE("%x:%x:%x:%x\n", header[0], header[1], header[2], header[3]);
524 } while (0);
525
Chris Robinsonb3fab142007-04-24 05:28:14 -0700526 while(SUCCEEDED(hr) && !(streamtype=MPEGSplitter_head_check(header)))
527 {
Maarten Lankhorst6c1d0892008-03-13 20:24:58 -0700528 TRACE("%x:%x:%x:%x\n", header[0], header[1], header[2], header[3]);
Chris Robinsonb3fab142007-04-24 05:28:14 -0700529 /* No valid header yet; shift by a byte and check again */
Maarten Lankhorstb0c6a342008-03-20 22:33:47 -0700530 memmove(header, header+1, 3);
Maarten Lankhorst6c1d0892008-03-13 20:24:58 -0700531 hr = IAsyncReader_SyncRead(pPin->pReader, pos++, 1, header + 3);
Chris Robinsonb3fab142007-04-24 05:28:14 -0700532 }
533 if (FAILED(hr))
534 return hr;
Maarten Lankhorstb0c6a342008-03-20 22:33:47 -0700535 pos -= 4;
Maarten Lankhorst3a398052008-04-22 13:57:11 -0700536 This->begin_offset = pos;
537 memcpy(This->header, header, 4);
Chris Robinsonb3fab142007-04-24 05:28:14 -0700538
Maarten Lankhorst24cac932008-04-08 12:28:09 -0700539 This->seektable[0].bytepos = pos;
540 This->seektable[0].timepos = 0;
541
Chris Robinsonb3fab142007-04-24 05:28:14 -0700542 switch(streamtype)
543 {
544 case MPEG_AUDIO_HEADER:
Maarten Lankhorstb0c6a342008-03-20 22:33:47 -0700545 {
546 LONGLONG duration = 0;
Maarten Lankhorst24cac932008-04-08 12:28:09 -0700547 DWORD last_entry = 0;
548
Maarten Lankhorstb0c6a342008-03-20 22:33:47 -0700549 DWORD ticks = GetTickCount();
550
Chris Robinsonb3fab142007-04-24 05:28:14 -0700551 hr = MPEGSplitter_init_audio(This, header, &piOutput, &amt);
552 if (SUCCEEDED(hr))
553 {
554 WAVEFORMATEX *format = (WAVEFORMATEX*)amt.pbFormat;
555
Maarten Lankhorst3a398052008-04-22 13:57:11 -0700556 props->cbAlign = 1;
557 props->cbPrefix = 0;
Chris Robinsonb3fab142007-04-24 05:28:14 -0700558 /* Make the output buffer a multiple of the frame size */
Maarten Lankhorst3a398052008-04-22 13:57:11 -0700559 props->cbBuffer = 0x4000 / format->nBlockAlign *
Chris Robinsonb3fab142007-04-24 05:28:14 -0700560 format->nBlockAlign;
Maarten Lankhorst0f73bba2008-04-30 14:34:07 -0700561 props->cBuffers = 3;
Maarten Lankhorst3a398052008-04-22 13:57:11 -0700562 hr = Parser_AddPin(&(This->Parser), &piOutput, props, &amt);
Chris Robinsonb3fab142007-04-24 05:28:14 -0700563 }
564
565 if (FAILED(hr))
566 {
567 if (amt.pbFormat)
568 CoTaskMemFree(amt.pbFormat);
569 ERR("Could not create pin for MPEG audio stream (%x)\n", hr);
Maarten Lankhorstb0c6a342008-03-20 22:33:47 -0700570 break;
Chris Robinsonb3fab142007-04-24 05:28:14 -0700571 }
Maarten Lankhorstb0c6a342008-03-20 22:33:47 -0700572
573 /* Check for idv1 tag, and remove it from stream if found */
574 hr = IAsyncReader_SyncRead(pPin->pReader, This->EndOfFile-128, 3, header+4);
575 if (FAILED(hr))
576 break;
577 if (!strncmp((char*)header+4, "TAG", 3))
578 This->EndOfFile -= 128;
Maarten Lankhorst4bc44572008-04-07 16:11:36 -0700579 This->Parser.pInputPin->rtStop = MEDIATIME_FROM_BYTES(This->EndOfFile);
Maarten Lankhorst3a398052008-04-22 13:57:11 -0700580 This->Parser.pInputPin->rtStart = This->Parser.pInputPin->rtCurrent = MEDIATIME_FROM_BYTES(This->begin_offset);
Maarten Lankhorstb0c6a342008-03-20 22:33:47 -0700581
Austin English313a85f2008-04-10 21:17:14 -0500582 /* http://mpgedit.org/mpgedit/mpeg_format/mpeghdr.htm has a whole read up on audio headers */
Maarten Lankhorst6fd90f12008-03-30 21:18:49 -0700583 while (pos + 3 < This->EndOfFile)
Maarten Lankhorstb0c6a342008-03-20 22:33:47 -0700584 {
585 LONGLONG length = 0;
Maarten Lankhorst714e8072008-03-28 13:10:20 -0700586 hr = IAsyncReader_SyncRead(pPin->pReader, pos, 4, header);
Maarten Lankhorst6fd90f12008-03-30 21:18:49 -0700587 if (hr != S_OK)
588 break;
Maarten Lankhorstb0c6a342008-03-20 22:33:47 -0700589 while (parse_header(header, &length, &duration))
590 {
591 /* No valid header yet; shift by a byte and check again */
592 memmove(header, header+1, 3);
593 hr = IAsyncReader_SyncRead(pPin->pReader, pos++, 1, header + 3);
Maarten Lankhorst6fd90f12008-03-30 21:18:49 -0700594 if (hr != S_OK || This->EndOfFile - pos < 4)
Maarten Lankhorstb0c6a342008-03-20 22:33:47 -0700595 break;
596 }
Maarten Lankhorst714e8072008-03-28 13:10:20 -0700597 pos += length;
Maarten Lankhorst24cac932008-04-08 12:28:09 -0700598
599 if (This->seektable && (duration / SEEK_INTERVAL) > last_entry)
600 {
601 if (last_entry + 1 > duration / SEEK_INTERVAL)
602 {
603 ERR("Somehow skipped %d interval lengths instead of 1\n", (DWORD)(duration/SEEK_INTERVAL) - (last_entry + 1));
604 }
605 ++last_entry;
606
607 TRACE("Entry: %u\n", last_entry);
608 if (last_entry >= This->seek_entries)
609 {
610 This->seek_entries += 64;
611 This->seektable = CoTaskMemRealloc(This->seektable, (This->seek_entries)*sizeof(struct seek_entry));
612 }
613 This->seektable[last_entry].bytepos = pos;
614 This->seektable[last_entry].timepos = duration;
615 }
616
Maarten Lankhorst714e8072008-03-28 13:10:20 -0700617 TRACE("Pos: %x%08x/%x%08x\n", (DWORD)(pos >> 32), (DWORD)pos, (DWORD)(This->EndOfFile>>32), (DWORD)This->EndOfFile);
Maarten Lankhorstb0c6a342008-03-20 22:33:47 -0700618 }
619 hr = S_OK;
Alexandre Julliard6af76b32008-03-26 18:01:09 +0100620 TRACE("Duration: %d seconds\n", (DWORD)(duration / 10000000));
Maarten Lankhorstb0c6a342008-03-20 22:33:47 -0700621 TRACE("Parsing took %u ms\n", GetTickCount() - ticks);
Maarten Lankhorstb1b75242008-04-01 14:42:32 -0700622 This->duration = duration;
Maarten Lankhorst2734e632008-04-09 12:07:14 -0700623
Aric Stewart602d44a2010-10-11 10:15:35 -0500624 This->Parser.sourceSeeking.llCurrent = 0;
625 This->Parser.sourceSeeking.llDuration = duration;
626 This->Parser.sourceSeeking.llStop = duration;
Chris Robinsonb3fab142007-04-24 05:28:14 -0700627 break;
Maarten Lankhorstb0c6a342008-03-20 22:33:47 -0700628 }
Chris Robinsonb3fab142007-04-24 05:28:14 -0700629 case MPEG_VIDEO_HEADER:
630 FIXME("MPEG video processing not yet supported!\n");
631 hr = E_FAIL;
632 break;
633 case MPEG_SYSTEM_HEADER:
634 FIXME("MPEG system streams not yet supported!\n");
635 hr = E_FAIL;
636 break;
637
638 default:
639 break;
640 }
Maarten Lankhorstb0c6a342008-03-20 22:33:47 -0700641 This->position = 0;
Chris Robinsonb3fab142007-04-24 05:28:14 -0700642
643 return hr;
644}
645
646static HRESULT MPEGSplitter_cleanup(LPVOID iface)
647{
Michael Stefaniuccc7fc4a2009-01-29 11:14:55 +0100648 MPEGSplitterImpl *This = iface;
Chris Robinsonb3fab142007-04-24 05:28:14 -0700649
Maarten Lankhorst0f73bba2008-04-30 14:34:07 -0700650 TRACE("(%p)\n", This);
Chris Robinsonb3fab142007-04-24 05:28:14 -0700651
652 return S_OK;
653}
654
Aric Stewart602d44a2010-10-11 10:15:35 -0500655static HRESULT WINAPI MPEGSplitter_seek(IMediaSeeking *iface)
Maarten Lankhorst553fe852008-04-01 14:43:09 -0700656{
Aric Stewart602d44a2010-10-11 10:15:35 -0500657 MPEGSplitterImpl *This = impl_from_IMediaSeeking(iface);
Maarten Lankhorst553fe852008-04-01 14:43:09 -0700658 PullPin *pPin = This->Parser.pInputPin;
659 LONGLONG newpos, timepos, bytepos;
660 HRESULT hr = S_OK;
661 BYTE header[4];
662
Aric Stewart602d44a2010-10-11 10:15:35 -0500663 newpos = This->Parser.sourceSeeking.llCurrent;
Maarten Lankhorst553fe852008-04-01 14:43:09 -0700664
665 if (newpos > This->duration)
666 {
Maarten Lankhorst4bc44572008-04-07 16:11:36 -0700667 WARN("Requesting position %x%08x beyond end of stream %x%08x\n", (DWORD)(newpos>>32), (DWORD)newpos, (DWORD)(This->duration>>32), (DWORD)This->duration);
Maarten Lankhorst553fe852008-04-01 14:43:09 -0700668 return E_INVALIDARG;
669 }
670
671 if (This->position/1000000 == newpos/1000000)
672 {
Maarten Lankhorst4bc44572008-04-07 16:11:36 -0700673 TRACE("Requesting position %x%08x same as current position %x%08x\n", (DWORD)(newpos>>32), (DWORD)newpos, (DWORD)(This->position>>32), (DWORD)This->position);
Maarten Lankhorst553fe852008-04-01 14:43:09 -0700674 return S_OK;
675 }
676
Maarten Lankhorst24cac932008-04-08 12:28:09 -0700677 /* Position, cached */
678 bytepos = This->seektable[newpos / SEEK_INTERVAL].bytepos;
679 timepos = This->seektable[newpos / SEEK_INTERVAL].timepos;
680
Maarten Lankhorst553fe852008-04-01 14:43:09 -0700681 hr = IAsyncReader_SyncRead(pPin->pReader, bytepos, 4, header);
Maarten Lankhorst3a398052008-04-22 13:57:11 -0700682 while (bytepos + 3 < This->EndOfFile)
Maarten Lankhorst553fe852008-04-01 14:43:09 -0700683 {
684 LONGLONG length = 0;
685 hr = IAsyncReader_SyncRead(pPin->pReader, bytepos, 4, header);
Maarten Lankhorst3a398052008-04-22 13:57:11 -0700686 if (hr != S_OK || timepos >= newpos)
Maarten Lankhorst24cac932008-04-08 12:28:09 -0700687 break;
688
689 while (parse_header(header, &length, &timepos) && bytepos + 3 < This->EndOfFile)
Maarten Lankhorst553fe852008-04-01 14:43:09 -0700690 {
691 /* No valid header yet; shift by a byte and check again */
692 memmove(header, header+1, 3);
693 hr = IAsyncReader_SyncRead(pPin->pReader, ++bytepos, 1, header + 3);
Maarten Lankhorst24cac932008-04-08 12:28:09 -0700694 if (hr != S_OK)
Maarten Lankhorst553fe852008-04-01 14:43:09 -0700695 break;
696 }
697 bytepos += length;
698 TRACE("Pos: %x%08x/%x%08x\n", (DWORD)(bytepos >> 32), (DWORD)bytepos, (DWORD)(This->EndOfFile>>32), (DWORD)This->EndOfFile);
Maarten Lankhorst553fe852008-04-01 14:43:09 -0700699 }
Maarten Lankhorst4bc44572008-04-07 16:11:36 -0700700
Maarten Lankhorst553fe852008-04-01 14:43:09 -0700701 if (SUCCEEDED(hr))
702 {
Maarten Lankhorst553fe852008-04-01 14:43:09 -0700703 PullPin *pin = This->Parser.pInputPin;
704
Maarten Lankhorst4bc44572008-04-07 16:11:36 -0700705 TRACE("Moving sound to %08u bytes!\n", (DWORD)bytepos);
Maarten Lankhorst553fe852008-04-01 14:43:09 -0700706
Maarten Lankhorst2734e632008-04-09 12:07:14 -0700707 EnterCriticalSection(&pin->thread_lock);
Aric Stewart018161e2012-04-04 08:27:25 -0500708 IPin_BeginFlush(&pin->pin.IPin_iface);
Maarten Lankhorst553fe852008-04-01 14:43:09 -0700709
Maarten Lankhorst4bc44572008-04-07 16:11:36 -0700710 /* Make sure this is done while stopped, BeginFlush takes care of this */
Aric Stewart1d426592010-10-07 14:47:33 -0500711 EnterCriticalSection(&This->Parser.filter.csFilter);
Maarten Lankhorst3a398052008-04-22 13:57:11 -0700712 memcpy(This->header, header, 4);
Maarten Lankhorst4bc44572008-04-07 16:11:36 -0700713
Maarten Lankhorst553fe852008-04-01 14:43:09 -0700714 pin->rtStart = pin->rtCurrent = MEDIATIME_FROM_BYTES(bytepos);
715 pin->rtStop = MEDIATIME_FROM_BYTES((REFERENCE_TIME)This->EndOfFile);
716 This->seek = TRUE;
Maarten Lankhorst553fe852008-04-01 14:43:09 -0700717 This->position = newpos;
Aric Stewart1d426592010-10-07 14:47:33 -0500718 LeaveCriticalSection(&This->Parser.filter.csFilter);
Maarten Lankhorst553fe852008-04-01 14:43:09 -0700719
Maarten Lankhorst4bc44572008-04-07 16:11:36 -0700720 TRACE("Done flushing\n");
Aric Stewart018161e2012-04-04 08:27:25 -0500721 IPin_EndFlush(&pin->pin.IPin_iface);
Maarten Lankhorst2734e632008-04-09 12:07:14 -0700722 LeaveCriticalSection(&pin->thread_lock);
Maarten Lankhorst553fe852008-04-01 14:43:09 -0700723 }
724 return hr;
725}
Maarten Lankhorstb1b75242008-04-01 14:42:32 -0700726
Maarten Lankhorst3a398052008-04-22 13:57:11 -0700727static HRESULT MPEGSplitter_disconnect(LPVOID iface)
Maarten Lankhorst6165d872008-04-15 20:39:36 -0700728{
729 /* TODO: Find memory leaks etc */
730 return S_OK;
731}
732
Maarten Lankhorst3a398052008-04-22 13:57:11 -0700733static HRESULT MPEGSplitter_first_request(LPVOID iface)
734{
Michael Stefaniuccc7fc4a2009-01-29 11:14:55 +0100735 MPEGSplitterImpl *This = iface;
Maarten Lankhorst3a398052008-04-22 13:57:11 -0700736 PullPin *pin = This->Parser.pInputPin;
737 HRESULT hr;
738 LONGLONG length;
739 IMediaSample *sample;
740
741 TRACE("Seeking? %d\n", This->seek);
Michael Stefaniuca948e5e2011-10-07 13:26:51 +0200742
743 hr = parse_header(This->header, &length, NULL);
744 assert(hr == S_OK);
Maarten Lankhorst3a398052008-04-22 13:57:11 -0700745
746 if (pin->rtCurrent >= pin->rtStop)
747 {
748 /* Last sample has already been queued, request nothing more */
749 FIXME("Done!\n");
750 return S_OK;
751 }
752
753 hr = IMemAllocator_GetBuffer(pin->pAlloc, &sample, NULL, NULL, 0);
754
755 pin->rtNext = pin->rtCurrent;
756 if (SUCCEEDED(hr))
757 {
758 LONGLONG rtSampleStart = pin->rtNext;
759 /* Add 4 for the next header, which should hopefully work */
760 LONGLONG rtSampleStop = rtSampleStart + MEDIATIME_FROM_BYTES(length + 4);
761
762 if (rtSampleStop > pin->rtStop)
763 rtSampleStop = MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(pin->rtStop), pin->cbAlign));
764
765 hr = IMediaSample_SetTime(sample, &rtSampleStart, &rtSampleStop);
766
Maarten Lankhorst3a398052008-04-22 13:57:11 -0700767 IMediaSample_SetPreroll(sample, FALSE);
Maarten Lankhorst8c425952010-11-07 15:02:49 +0100768 IMediaSample_SetDiscontinuity(sample, TRUE);
Maarten Lankhorst3a398052008-04-22 13:57:11 -0700769 IMediaSample_SetSyncPoint(sample, 1);
770 This->seek = 0;
771
772 hr = IAsyncReader_Request(pin->pReader, sample, 0);
Erich Hoovercd4ee692011-09-11 14:10:42 -0600773 if (SUCCEEDED(hr))
774 {
775 pin->rtCurrent = pin->rtNext;
776 pin->rtNext = rtSampleStop;
777 }
778 else
779 IMediaSample_Release(sample);
Maarten Lankhorst3a398052008-04-22 13:57:11 -0700780 }
781 if (FAILED(hr))
782 ERR("Horsemen of the apocalypse came to bring error 0x%08x\n", hr);
783
784 return hr;
785}
786
Maarten Lankhorst1f136a52008-04-23 11:23:43 -0700787static const IBaseFilterVtbl MPEGSplitter_Vtbl =
788{
789 Parser_QueryInterface,
790 Parser_AddRef,
791 Parser_Release,
792 Parser_GetClassID,
793 Parser_Stop,
794 Parser_Pause,
795 Parser_Run,
796 Parser_GetState,
797 Parser_SetSyncSource,
798 Parser_GetSyncSource,
799 Parser_EnumPins,
800 Parser_FindPin,
801 Parser_QueryFilterInfo,
802 Parser_JoinFilterGraph,
803 Parser_QueryVendorInfo
804};
805
Chris Robinsonb3fab142007-04-24 05:28:14 -0700806HRESULT MPEGSplitter_create(IUnknown * pUnkOuter, LPVOID * ppv)
807{
808 MPEGSplitterImpl *This;
809 HRESULT hr = E_FAIL;
810
811 TRACE("(%p, %p)\n", pUnkOuter, ppv);
812
813 *ppv = NULL;
814
815 if (pUnkOuter)
816 return CLASS_E_NOAGGREGATION;
817
818 This = CoTaskMemAlloc(sizeof(MPEGSplitterImpl));
819 if (!This)
820 return E_OUTOFMEMORY;
821
822 ZeroMemory(This, sizeof(MPEGSplitterImpl));
Maarten Lankhorst24cac932008-04-08 12:28:09 -0700823 This->seektable = CoTaskMemAlloc(sizeof(struct seek_entry) * 64);
824 if (!This->seektable)
825 {
826 CoTaskMemFree(This);
827 return E_OUTOFMEMORY;
828 }
829 This->seek_entries = 64;
830
Maarten Lankhorst512ee922008-04-25 14:59:05 -0700831 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);
Chris Robinsonb3fab142007-04-24 05:28:14 -0700832 if (FAILED(hr))
833 {
834 CoTaskMemFree(This);
835 return hr;
836 }
Maarten Lankhorst3a398052008-04-22 13:57:11 -0700837 This->seek = 1;
Chris Robinsonb3fab142007-04-24 05:28:14 -0700838
839 /* Note: This memory is managed by the parser filter once created */
Michael Stefaniuccc7fc4a2009-01-29 11:14:55 +0100840 *ppv = This;
Chris Robinsonb3fab142007-04-24 05:28:14 -0700841
842 return hr;
843}