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 LONGLONG StartOfFile
; /* in media time */
52 static LONGLONG
bytepos_to_duration(WAVEParserImpl
*This
, LONGLONG bytepos
)
54 LONGLONG duration
= BYTES_FROM_MEDIATIME(bytepos
- This
->StartOfFile
);
56 duration
/= (This
->dwSampleSize
* This
->nSamplesPerSec
);
61 static LONGLONG
duration_to_bytepos(WAVEParserImpl
*This
, LONGLONG duration
)
65 bytepos
= (This
->dwSampleSize
* This
->nSamplesPerSec
);
68 bytepos
+= BYTES_FROM_MEDIATIME(This
->StartOfFile
);
69 bytepos
-= bytepos
% This
->dwSampleSize
;
71 return MEDIATIME_FROM_BYTES(bytepos
);
74 static HRESULT
WAVEParser_Sample(LPVOID iface
, IMediaSample
* pSample
, DWORD_PTR cookie
)
76 WAVEParserImpl
*This
= iface
;
77 LPBYTE pbSrcStream
= NULL
;
78 ULONG cbSrcStream
= 0;
79 REFERENCE_TIME tStart
, tStop
;
81 IMediaSample
*newsample
= NULL
;
82 Parser_OutputPin
*pOutputPin
;
83 PullPin
*pin
= This
->Parser
.pInputPin
;
85 IMediaSample_GetPointer(pSample
, &pbSrcStream
);
86 hr
= IMediaSample_GetTime(pSample
, &tStart
, &tStop
);
88 cbSrcStream
= IMediaSample_GetActualDataLength(pSample
);
93 TRACE(".. Why do I need you?\n");
97 pOutputPin
= (Parser_OutputPin
*)This
->Parser
.ppPins
[1];
100 hr
= IMemAllocator_GetBuffer(pin
->pAlloc
, &newsample
, NULL
, NULL
, 0);
104 LONGLONG rtSampleStart
= pin
->rtNext
;
105 /* Add 4 for the next header, which should hopefully work */
106 LONGLONG rtSampleStop
= rtSampleStart
+ MEDIATIME_FROM_BYTES(IMediaSample_GetSize(newsample
));
108 if (rtSampleStop
> pin
->rtStop
)
109 rtSampleStop
= MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(pin
->rtStop
), pin
->cbAlign
));
111 hr
= IMediaSample_SetTime(newsample
, &rtSampleStart
, &rtSampleStop
);
113 pin
->rtCurrent
= pin
->rtNext
;
114 pin
->rtNext
= rtSampleStop
;
116 IMediaSample_SetPreroll(newsample
, 0);
117 IMediaSample_SetDiscontinuity(newsample
, 0);
118 IMediaSample_SetSyncPoint(newsample
, 1);
120 hr
= IAsyncReader_Request(pin
->pReader
, newsample
, 0);
125 REFERENCE_TIME tAviStart
, tAviStop
;
127 IMediaSample_SetSyncPoint(pSample
, TRUE
);
128 pOutputPin
->dwSamplesProcessed
++;
130 tAviStart
= bytepos_to_duration(This
, tStart
);
131 tAviStop
= bytepos_to_duration(This
, tStart
+ MEDIATIME_FROM_BYTES(IMediaSample_GetActualDataLength(pSample
)));
133 IMediaSample_SetTime(pSample
, &tAviStart
, &tAviStop
);
135 hr
= OutputPin_SendSample(&pOutputPin
->pin
, pSample
);
136 if (hr
!= S_OK
&& hr
!= S_FALSE
&& hr
!= VFW_E_WRONG_STATE
)
137 ERR("Error sending sample (%x)\n", hr
);
139 /* Unset progression if denied! */
140 This
->Parser
.pInputPin
->rtCurrent
= tStart
;
143 if (tStop
>= This
->EndOfFile
|| (bytepos_to_duration(This
, tStop
) >= This
->Parser
.mediaSeeking
.llStop
) || hr
== VFW_E_NOT_CONNECTED
)
147 TRACE("End of file reached\n");
149 for (i
= 0; i
< This
->Parser
.cStreams
; i
++)
154 TRACE("Send End Of Stream to output pin %u\n", i
);
156 hr
= IPin_ConnectedTo(This
->Parser
.ppPins
[i
+1], &ppin
);
159 hr
= IPin_EndOfStream(ppin
);
169 /* Force the pullpin thread to stop */
176 static HRESULT
WAVEParser_QueryAccept(LPVOID iface
, const AM_MEDIA_TYPE
* pmt
)
178 if (!IsEqualIID(&pmt
->majortype
, &MEDIATYPE_Stream
))
180 if (IsEqualIID(&pmt
->subtype
, &MEDIASUBTYPE_WAVE
))
182 if (IsEqualIID(&pmt
->subtype
, &MEDIASUBTYPE_AU
) || IsEqualIID(&pmt
->subtype
, &MEDIASUBTYPE_AIFF
))
183 FIXME("AU and AIFF files not supported yet!\n");
187 static HRESULT
WAVEParserImpl_seek(IBaseFilter
*iface
)
189 WAVEParserImpl
*This
= (WAVEParserImpl
*)iface
;
190 PullPin
*pPin
= This
->Parser
.pInputPin
;
192 LONGLONG newpos
, curpos
, endpos
, bytepos
;
194 newpos
= This
->Parser
.mediaSeeking
.llCurrent
;
195 curpos
= bytepos_to_duration(This
, pPin
->rtCurrent
);
196 endpos
= bytepos_to_duration(This
, This
->EndOfFile
);
197 bytepos
= duration_to_bytepos(This
, newpos
);
201 WARN("Requesting position %x%08x beyond end of stream %x%08x\n", (DWORD
)(newpos
>>32), (DWORD
)newpos
, (DWORD
)(endpos
>>32), (DWORD
)endpos
);
205 if (curpos
/1000000 == newpos
/1000000)
207 TRACE("Requesting position %x%08x same as current position %x%08x\n", (DWORD
)(newpos
>>32), (DWORD
)newpos
, (DWORD
)(curpos
>>32), (DWORD
)curpos
);
211 TRACE("Moving sound to %08u bytes!\n", (DWORD
)BYTES_FROM_MEDIATIME(bytepos
));
213 EnterCriticalSection(&pPin
->thread_lock
);
214 IPin_BeginFlush((IPin
*)pPin
);
216 /* Make sure this is done while stopped, BeginFlush takes care of this */
217 EnterCriticalSection(&This
->Parser
.csFilter
);
218 IPin_ConnectedTo(This
->Parser
.ppPins
[1], &victim
);
221 IPin_NewSegment(victim
, newpos
, endpos
, pPin
->dRate
);
222 IPin_Release(victim
);
225 pPin
->rtStart
= pPin
->rtCurrent
= bytepos
;
226 ((Parser_OutputPin
*)This
->Parser
.ppPins
[1])->dwSamplesProcessed
= 0;
227 LeaveCriticalSection(&This
->Parser
.csFilter
);
229 TRACE("Done flushing\n");
230 IPin_EndFlush((IPin
*)pPin
);
231 LeaveCriticalSection(&pPin
->thread_lock
);
236 static HRESULT
WAVEParser_InputPin_PreConnect(IPin
* iface
, IPin
* pConnectPin
, ALLOCATOR_PROPERTIES
*props
)
238 PullPin
*This
= (PullPin
*)iface
;
242 LONGLONG pos
= 0; /* in bytes */
245 WAVEParserImpl
* pWAVEParser
= (WAVEParserImpl
*)This
->pin
.pinInfo
.pFilter
;
246 LONGLONG length
, avail
;
248 piOutput
.dir
= PINDIR_OUTPUT
;
249 piOutput
.pFilter
= (IBaseFilter
*)This
;
250 lstrcpynW(piOutput
.achName
, wcsOutputPinName
, sizeof(piOutput
.achName
) / sizeof(piOutput
.achName
[0]));
252 hr
= IAsyncReader_SyncRead(This
->pReader
, pos
, sizeof(list
), (BYTE
*)&list
);
255 if (list
.fcc
!= FOURCC_RIFF
)
257 ERR("Input stream not a RIFF file\n");
260 if (list
.cb
> 1 * 1024 * 1024 * 1024) /* cannot be more than 1Gb in size */
262 ERR("Input stream violates RIFF spec\n");
265 if (list
.fccListType
!= mmioFOURCC('W','A','V','E'))
267 ERR("Input stream not an WAVE RIFF file\n");
271 hr
= IAsyncReader_SyncRead(This
->pReader
, pos
, sizeof(chunk
), (BYTE
*)&chunk
);
272 pos
+= sizeof(chunk
);
273 if (chunk
.fcc
!= mmioFOURCC('f','m','t',' '))
275 ERR("Expected 'fmt ' chunk, but got %.04s\n", (LPSTR
)&chunk
.fcc
);
279 amt
.majortype
= MEDIATYPE_Audio
;
280 amt
.formattype
= FORMAT_WaveFormatEx
;
281 amt
.cbFormat
= chunk
.cb
;
282 amt
.pbFormat
= CoTaskMemAlloc(amt
.cbFormat
);
284 hr
= IAsyncReader_SyncRead(This
->pReader
, pos
, amt
.cbFormat
, amt
.pbFormat
);
285 amt
.subtype
= MEDIATYPE_Audio
;
286 amt
.subtype
.Data1
= ((WAVEFORMATEX
*)amt
.pbFormat
)->wFormatTag
;
289 hr
= IAsyncReader_SyncRead(This
->pReader
, pos
, sizeof(chunk
), (BYTE
*)&chunk
);
290 if (chunk
.fcc
== mmioFOURCC('f','a','c','t'))
292 FIXME("'fact' chunk not supported yet\n");
293 pos
+= sizeof(chunk
) + chunk
.cb
;
294 hr
= IAsyncReader_SyncRead(This
->pReader
, pos
, sizeof(chunk
), (BYTE
*)&chunk
);
296 if (chunk
.fcc
!= mmioFOURCC('d','a','t','a'))
298 ERR("Expected 'data' chunk, but got %.04s\n", (LPSTR
)&chunk
.fcc
);
304 pWAVEParser
->StartOfFile
= MEDIATIME_FROM_BYTES(pos
+ sizeof(RIFFCHUNK
));
305 pWAVEParser
->EndOfFile
= MEDIATIME_FROM_BYTES(pos
+ chunk
.cb
+ sizeof(RIFFCHUNK
));
311 props
->cbAlign
= ((WAVEFORMATEX
*)amt
.pbFormat
)->nBlockAlign
;
313 props
->cbBuffer
= 4096;
315 pWAVEParser
->dwSampleSize
= ((WAVEFORMATEX
*)amt
.pbFormat
)->nBlockAlign
;
316 IAsyncReader_Length(This
->pReader
, &length
, &avail
);
317 pWAVEParser
->dwLength
= length
/ (ULONGLONG
)pWAVEParser
->dwSampleSize
;
318 pWAVEParser
->nSamplesPerSec
= ((WAVEFORMATEX
*)amt
.pbFormat
)->nSamplesPerSec
;
319 hr
= Parser_AddPin(&(pWAVEParser
->Parser
), &piOutput
, props
, &amt
);
320 CoTaskMemFree(amt
.pbFormat
);
322 pWAVEParser
->Parser
.mediaSeeking
.llCurrent
= 0;
323 pWAVEParser
->Parser
.mediaSeeking
.llStop
= pWAVEParser
->Parser
.mediaSeeking
.llDuration
= bytepos_to_duration(pWAVEParser
, pWAVEParser
->EndOfFile
);
324 TRACE("Duration: %u seconds\n", (DWORD
)(pWAVEParser
->Parser
.mediaSeeking
.llDuration
/ (LONGLONG
)10000000));
326 This
->rtStop
= pWAVEParser
->EndOfFile
;
327 This
->rtStart
= pWAVEParser
->StartOfFile
;
329 TRACE("WAVE File ok\n");
334 static HRESULT
WAVEParser_Cleanup(LPVOID iface
)
336 WAVEParserImpl
*This
= iface
;
338 TRACE("(%p)->()\n", This
);
343 static HRESULT
WAVEParser_first_request(LPVOID iface
)
345 WAVEParserImpl
*This
= iface
;
346 PullPin
*pin
= This
->Parser
.pInputPin
;
348 IMediaSample
*sample
;
350 if (pin
->rtCurrent
>= pin
->rtStop
)
352 /* Last sample has already been queued, request nothing more */
357 hr
= IMemAllocator_GetBuffer(pin
->pAlloc
, &sample
, NULL
, NULL
, 0);
359 pin
->rtNext
= pin
->rtCurrent
;
362 LONGLONG rtSampleStart
= pin
->rtNext
;
363 /* Add 4 for the next header, which should hopefully work */
364 LONGLONG rtSampleStop
= rtSampleStart
+ MEDIATIME_FROM_BYTES(IMediaSample_GetSize(sample
));
365 Parser_OutputPin
*outpin
= (Parser_OutputPin
*)This
->Parser
.ppPins
[1];
367 if (rtSampleStop
> pin
->rtStop
)
368 rtSampleStop
= MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(pin
->rtStop
), pin
->cbAlign
));
370 hr
= IMediaSample_SetTime(sample
, &rtSampleStart
, &rtSampleStop
);
372 pin
->rtCurrent
= pin
->rtNext
;
373 pin
->rtNext
= rtSampleStop
;
375 IMediaSample_SetPreroll(sample
, FALSE
);
376 if (!outpin
->dwSamplesProcessed
++)
377 IMediaSample_SetDiscontinuity(sample
, TRUE
);
379 IMediaSample_SetDiscontinuity(sample
, FALSE
);
381 hr
= IAsyncReader_Request(pin
->pReader
, sample
, 0);
384 ERR("Horsemen of the apocalypse came to bring error 0x%08x %p\n", hr
, sample
);
389 static HRESULT
WAVEParser_disconnect(LPVOID iface
)
391 /* TODO: Find and plug memory leaks */
395 static const IBaseFilterVtbl WAVEParser_Vtbl
=
397 Parser_QueryInterface
,
405 Parser_SetSyncSource
,
406 Parser_GetSyncSource
,
409 Parser_QueryFilterInfo
,
410 Parser_JoinFilterGraph
,
411 Parser_QueryVendorInfo
414 HRESULT
WAVEParser_create(IUnknown
* pUnkOuter
, LPVOID
* ppv
)
417 WAVEParserImpl
* This
;
419 TRACE("(%p, %p)\n", pUnkOuter
, ppv
);
424 return CLASS_E_NOAGGREGATION
;
426 /* Note: This memory is managed by the transform filter once created */
427 This
= CoTaskMemAlloc(sizeof(WAVEParserImpl
));
429 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
);