4 * Copyright 2005 Christian Costa
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "quartz_private.h"
22 #include "control_private.h"
30 #include "wine/unicode.h"
31 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
40 static const WCHAR wcsOutputPinName[] = {'o','u','t','p','u','t',' ','p','i','n',0};
42 typedef struct WAVEParserImpl
45 IMediaSample * pCurrentSample;
46 LONGLONG StartOfFile; /* in media time */
53 static LONGLONG bytepos_to_duration(WAVEParserImpl *This, LONGLONG bytepos)
55 LONGLONG duration = BYTES_FROM_MEDIATIME(bytepos - This->StartOfFile);
57 duration /= (This->dwSampleSize * This->nSamplesPerSec);
62 static LONGLONG duration_to_bytepos(WAVEParserImpl *This, LONGLONG duration)
66 bytepos = (This->dwSampleSize * This->nSamplesPerSec);
69 bytepos += BYTES_FROM_MEDIATIME(This->StartOfFile);
70 bytepos -= bytepos % This->dwSampleSize;
72 return MEDIATIME_FROM_BYTES(bytepos);
75 static HRESULT WAVEParser_Sample(LPVOID iface, IMediaSample * pSample, DWORD_PTR cookie)
77 WAVEParserImpl *This = (WAVEParserImpl *)iface;
78 LPBYTE pbSrcStream = NULL;
79 ULONG cbSrcStream = 0;
80 REFERENCE_TIME tStart, tStop;
82 IMediaSample *newsample = NULL;
83 Parser_OutputPin *pOutputPin;
84 PullPin *pin = This->Parser.pInputPin;
86 IMediaSample_GetPointer(pSample, &pbSrcStream);
87 hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
89 cbSrcStream = IMediaSample_GetActualDataLength(pSample);
94 TRACE(".. Why do I need you?\n");
98 pOutputPin = (Parser_OutputPin *)This->Parser.ppPins[1];
100 /* Try to get rid of the current sample in case we had a S_FALSE last time */
101 if (This->pCurrentSample)
103 Parser_OutputPin * pOutputPin = (Parser_OutputPin*)This->Parser.ppPins[1];
104 IMediaSample *pCurrentSample = This->pCurrentSample;
107 hr = OutputPin_SendSample(&pOutputPin->pin, pCurrentSample);
112 TRACE("Requeueing!\n");
113 IMediaSample_AddRef(pSample);
114 IAsyncReader_Request(This->Parser.pInputPin->pReader, pSample, 0);
118 IMediaSample_Release(This->pCurrentSample);
119 This->pCurrentSample = NULL;
123 hr = IMemAllocator_GetBuffer(pin->pAlloc, &newsample, NULL, NULL, 0);
127 LONGLONG rtSampleStart = pin->rtNext;
128 /* Add 4 for the next header, which should hopefully work */
129 LONGLONG rtSampleStop = rtSampleStart + MEDIATIME_FROM_BYTES(IMediaSample_GetSize(newsample));
131 if (rtSampleStop > pin->rtStop)
132 rtSampleStop = MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(pin->rtStop), pin->cbAlign));
134 hr = IMediaSample_SetTime(newsample, &rtSampleStart, &rtSampleStop);
136 pin->rtCurrent = pin->rtNext;
137 pin->rtNext = rtSampleStop;
139 IMediaSample_SetPreroll(newsample, 0);
140 IMediaSample_SetDiscontinuity(newsample, 0);
141 IMediaSample_SetSyncPoint(newsample, 1);
143 hr = IAsyncReader_Request(pin->pReader, newsample, 0);
148 REFERENCE_TIME tAviStart, tAviStop;
150 IMediaSample_SetSyncPoint(pSample, TRUE);
151 pOutputPin->dwSamplesProcessed++;
153 tAviStart = bytepos_to_duration(This, tStart);
154 tAviStop = bytepos_to_duration(This, tStart + MEDIATIME_FROM_BYTES(IMediaSample_GetActualDataLength(pSample)));
156 IMediaSample_SetTime(pSample, &tAviStart, &tAviStop);
158 hr = OutputPin_SendSample(&pOutputPin->pin, pSample);
159 if (hr != S_OK && hr != VFW_E_NOT_CONNECTED && hr != S_FALSE)
160 ERR("Error sending sample (%x)\n", hr);
164 This->pCurrentSample = pSample;
165 IMediaSample_AddRef(pSample);
170 if (tStop >= This->EndOfFile || (bytepos_to_duration(This, tStop) >= This->Parser.mediaSeeking.llStop))
174 TRACE("End of file reached\n");
176 for (i = 0; i < This->Parser.cStreams; i++)
181 TRACE("Send End Of Stream to output pin %d\n", i);
183 hr = IPin_ConnectedTo(This->Parser.ppPins[i+1], &ppin);
186 hr = IPin_EndOfStream(ppin);
196 /* Force the pullpin thread to stop */
203 static HRESULT WAVEParser_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
205 if (!IsEqualIID(&pmt->majortype, &MEDIATYPE_Stream))
207 if (IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_WAVE))
209 if (IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_AU) || IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_AIFF))
210 FIXME("AU and AIFF files not supported yet!\n");
214 static HRESULT WAVEParserImpl_seek(IBaseFilter *iface)
216 WAVEParserImpl *This = (WAVEParserImpl *)iface;
217 PullPin *pPin = This->Parser.pInputPin;
219 LONGLONG newpos, curpos, endpos, bytepos;
221 newpos = This->Parser.mediaSeeking.llCurrent;
222 curpos = bytepos_to_duration(This, pPin->rtCurrent);
223 endpos = bytepos_to_duration(This, This->EndOfFile);
224 bytepos = duration_to_bytepos(This, newpos);
228 WARN("Requesting position %x%08x beyond end of stream %x%08x\n", (DWORD)(newpos>>32), (DWORD)newpos, (DWORD)(endpos>>32), (DWORD)endpos);
232 if (curpos/1000000 == newpos/1000000)
234 TRACE("Requesting position %x%08x same as current position %x%08x\n", (DWORD)(newpos>>32), (DWORD)newpos, (DWORD)(curpos>>32), (DWORD)curpos);
238 TRACE("Moving sound to %08u bytes!\n", (DWORD)BYTES_FROM_MEDIATIME(bytepos));
240 EnterCriticalSection(&pPin->thread_lock);
241 IPin_BeginFlush((IPin *)pPin);
243 /* Make sure this is done while stopped, BeginFlush takes care of this */
244 EnterCriticalSection(&This->Parser.csFilter);
245 IPin_ConnectedTo(This->Parser.ppPins[1], &victim);
248 IPin_NewSegment(victim, newpos, endpos, pPin->dRate);
249 IPin_Release(victim);
252 pPin->rtStart = pPin->rtCurrent = bytepos;
253 ((Parser_OutputPin *)This->Parser.ppPins[1])->dwSamplesProcessed = 0;
254 LeaveCriticalSection(&This->Parser.csFilter);
256 TRACE("Done flushing\n");
257 IPin_EndFlush((IPin *)pPin);
258 LeaveCriticalSection(&pPin->thread_lock);
263 static HRESULT WAVEParser_InputPin_PreConnect(IPin * iface, IPin * pConnectPin, ALLOCATOR_PROPERTIES *props)
265 PullPin *This = (PullPin *)iface;
269 LONGLONG pos = 0; /* in bytes */
272 WAVEParserImpl * pWAVEParser = (WAVEParserImpl *)This->pin.pinInfo.pFilter;
273 LONGLONG length, avail;
275 piOutput.dir = PINDIR_OUTPUT;
276 piOutput.pFilter = (IBaseFilter *)This;
277 lstrcpynW(piOutput.achName, wcsOutputPinName, sizeof(piOutput.achName) / sizeof(piOutput.achName[0]));
279 hr = IAsyncReader_SyncRead(This->pReader, pos, sizeof(list), (BYTE *)&list);
282 if (list.fcc != FOURCC_RIFF)
284 ERR("Input stream not a RIFF file\n");
287 if (list.cb > 1 * 1024 * 1024 * 1024) /* cannot be more than 1Gb in size */
289 ERR("Input stream violates RIFF spec\n");
292 if (list.fccListType != mmioFOURCC('W','A','V','E'))
294 ERR("Input stream not an WAVE RIFF file\n");
298 hr = IAsyncReader_SyncRead(This->pReader, pos, sizeof(chunk), (BYTE *)&chunk);
299 pos += sizeof(chunk);
300 if (chunk.fcc != mmioFOURCC('f','m','t',' '))
302 ERR("Expected 'fmt ' chunk, but got %.04s\n", (LPSTR)&chunk.fcc);
306 amt.majortype = MEDIATYPE_Audio;
307 amt.formattype = FORMAT_WaveFormatEx;
308 amt.cbFormat = chunk.cb;
309 amt.pbFormat = CoTaskMemAlloc(amt.cbFormat);
311 hr = IAsyncReader_SyncRead(This->pReader, pos, amt.cbFormat, amt.pbFormat);
312 amt.subtype = MEDIATYPE_Audio;
313 amt.subtype.Data1 = ((WAVEFORMATEX*)amt.pbFormat)->wFormatTag;
316 hr = IAsyncReader_SyncRead(This->pReader, pos, sizeof(chunk), (BYTE *)&chunk);
317 if (chunk.fcc == mmioFOURCC('f','a','c','t'))
319 FIXME("'fact' chunk not supported yet\n");
320 pos += sizeof(chunk) + chunk.cb;
321 hr = IAsyncReader_SyncRead(This->pReader, pos, sizeof(chunk), (BYTE *)&chunk);
323 if (chunk.fcc != mmioFOURCC('d','a','t','a'))
325 ERR("Expected 'data' chunk, but got %.04s\n", (LPSTR)&chunk.fcc);
331 pWAVEParser->StartOfFile = MEDIATIME_FROM_BYTES(pos + sizeof(RIFFCHUNK));
332 pWAVEParser->EndOfFile = MEDIATIME_FROM_BYTES(pos + chunk.cb + sizeof(RIFFCHUNK));
338 props->cbAlign = ((WAVEFORMATEX*)amt.pbFormat)->nBlockAlign;
340 props->cbBuffer = 4096;
342 pWAVEParser->dwSampleSize = ((WAVEFORMATEX*)amt.pbFormat)->nBlockAlign;
343 IAsyncReader_Length(This->pReader, &length, &avail);
344 pWAVEParser->dwLength = length / (ULONGLONG)pWAVEParser->dwSampleSize;
345 pWAVEParser->nSamplesPerSec = ((WAVEFORMATEX*)amt.pbFormat)->nSamplesPerSec;
346 hr = Parser_AddPin(&(pWAVEParser->Parser), &piOutput, props, &amt);
347 CoTaskMemFree(amt.pbFormat);
349 pWAVEParser->Parser.mediaSeeking.llCurrent = 0;
350 pWAVEParser->Parser.mediaSeeking.llStop = pWAVEParser->Parser.mediaSeeking.llDuration = bytepos_to_duration(pWAVEParser, pWAVEParser->EndOfFile);
351 TRACE("Duration: %u seconds\n", (DWORD)(pWAVEParser->Parser.mediaSeeking.llDuration / (LONGLONG)10000000));
353 This->rtStop = pWAVEParser->EndOfFile;
354 This->rtStart = pWAVEParser->StartOfFile;
356 TRACE("WAVE File ok\n");
361 static HRESULT WAVEParser_Cleanup(LPVOID iface)
363 WAVEParserImpl *This = (WAVEParserImpl*)iface;
365 TRACE("(%p)->()\n", This);
367 if (This->pCurrentSample)
368 IMediaSample_Release(This->pCurrentSample);
369 This->pCurrentSample = NULL;
374 static HRESULT WAVEParser_first_request(LPVOID iface)
376 WAVEParserImpl *This = (WAVEParserImpl *)iface;
377 PullPin *pin = This->Parser.pInputPin;
379 IMediaSample *sample;
381 if (pin->rtCurrent >= pin->rtStop)
383 /* Last sample has already been queued, request nothing more */
388 hr = IMemAllocator_GetBuffer(pin->pAlloc, &sample, NULL, NULL, 0);
390 pin->rtNext = pin->rtCurrent;
393 LONGLONG rtSampleStart = pin->rtNext;
394 /* Add 4 for the next header, which should hopefully work */
395 LONGLONG rtSampleStop = rtSampleStart + MEDIATIME_FROM_BYTES(IMediaSample_GetSize(sample));
396 Parser_OutputPin *outpin = (Parser_OutputPin *)This->Parser.ppPins[1];
398 if (rtSampleStop > pin->rtStop)
399 rtSampleStop = MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(pin->rtStop), pin->cbAlign));
401 hr = IMediaSample_SetTime(sample, &rtSampleStart, &rtSampleStop);
403 pin->rtCurrent = pin->rtNext;
404 pin->rtNext = rtSampleStop;
406 IMediaSample_SetPreroll(sample, FALSE);
407 if (!outpin->dwSamplesProcessed++)
408 IMediaSample_SetDiscontinuity(sample, TRUE);
410 IMediaSample_SetDiscontinuity(sample, FALSE);
412 hr = IAsyncReader_Request(pin->pReader, sample, 0);
415 ERR("Horsemen of the apocalypse came to bring error 0x%08x\n", hr);
420 static HRESULT WAVEParser_disconnect(LPVOID iface)
422 /* TODO: Find and plug memory leaks */
426 static const IBaseFilterVtbl WAVEParser_Vtbl =
428 Parser_QueryInterface,
436 Parser_SetSyncSource,
437 Parser_GetSyncSource,
440 Parser_QueryFilterInfo,
441 Parser_JoinFilterGraph,
442 Parser_QueryVendorInfo
445 HRESULT WAVEParser_create(IUnknown * pUnkOuter, LPVOID * ppv)
448 WAVEParserImpl * This;
450 TRACE("(%p, %p)\n", pUnkOuter, ppv);
455 return CLASS_E_NOAGGREGATION;
457 /* Note: This memory is managed by the transform filter once created */
458 This = CoTaskMemAlloc(sizeof(WAVEParserImpl));
460 This->pCurrentSample = NULL;
462 hr = Parser_Create(&(This->Parser), &WAVEParser_Vtbl, &CLSID_WAVEParser, WAVEParser_Sample, WAVEParser_QueryAccept, WAVEParser_InputPin_PreConnect, WAVEParser_Cleanup, WAVEParser_disconnect, WAVEParser_first_request, NULL, NULL, WAVEParserImpl_seek, NULL);