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"
23 static const WCHAR wcsOutputPinName
[] = {'o','u','t','p','u','t',' ','p','i','n',0};
25 typedef struct WAVEParserImpl
28 LONGLONG StartOfFile
; /* in media time */
35 static inline WAVEParserImpl
*impl_from_IMediaSeeking( IMediaSeeking
*iface
)
37 return CONTAINING_RECORD(iface
, WAVEParserImpl
, Parser
.sourceSeeking
.IMediaSeeking_iface
);
40 static inline WAVEParserImpl
*impl_from_IBaseFilter( IBaseFilter
*iface
)
42 return CONTAINING_RECORD(iface
, WAVEParserImpl
, Parser
.filter
.IBaseFilter_iface
);
45 static LONGLONG
bytepos_to_duration(WAVEParserImpl
*This
, LONGLONG bytepos
)
47 LONGLONG duration
= BYTES_FROM_MEDIATIME(bytepos
- This
->StartOfFile
);
49 duration
/= (This
->dwSampleSize
* This
->nSamplesPerSec
);
54 static LONGLONG
duration_to_bytepos(WAVEParserImpl
*This
, LONGLONG duration
)
58 bytepos
= (This
->dwSampleSize
* This
->nSamplesPerSec
);
61 bytepos
+= BYTES_FROM_MEDIATIME(This
->StartOfFile
);
62 bytepos
-= bytepos
% This
->dwSampleSize
;
64 return MEDIATIME_FROM_BYTES(bytepos
);
67 static HRESULT
WAVEParser_Sample(LPVOID iface
, IMediaSample
* pSample
, DWORD_PTR cookie
)
69 WAVEParserImpl
*This
= iface
;
70 LPBYTE pbSrcStream
= NULL
;
71 ULONG cbSrcStream
= 0;
72 REFERENCE_TIME tStart
, tStop
;
74 IMediaSample
*newsample
= NULL
;
75 Parser_OutputPin
*pOutputPin
;
76 PullPin
*pin
= This
->Parser
.pInputPin
;
78 IMediaSample_GetPointer(pSample
, &pbSrcStream
);
79 hr
= IMediaSample_GetTime(pSample
, &tStart
, &tStop
);
81 cbSrcStream
= IMediaSample_GetActualDataLength(pSample
);
86 TRACE(".. Why do I need you?\n");
90 pOutputPin
= unsafe_impl_Parser_OutputPin_from_IPin(This
->Parser
.ppPins
[1]);
93 hr
= IMemAllocator_GetBuffer(pin
->pAlloc
, &newsample
, NULL
, NULL
, 0);
97 LONGLONG rtSampleStart
= pin
->rtNext
;
98 /* Add 4 for the next header, which should hopefully work */
99 LONGLONG rtSampleStop
= rtSampleStart
+ MEDIATIME_FROM_BYTES(IMediaSample_GetSize(newsample
));
101 if (rtSampleStop
> pin
->rtStop
)
102 rtSampleStop
= MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(pin
->rtStop
), pin
->cbAlign
));
104 IMediaSample_SetTime(newsample
, &rtSampleStart
, &rtSampleStop
);
106 pin
->rtCurrent
= pin
->rtNext
;
107 pin
->rtNext
= rtSampleStop
;
109 IMediaSample_SetPreroll(newsample
, 0);
110 IMediaSample_SetDiscontinuity(newsample
, 0);
111 IMediaSample_SetSyncPoint(newsample
, 1);
113 hr
= IAsyncReader_Request(pin
->pReader
, newsample
, 0);
118 REFERENCE_TIME tAviStart
, tAviStop
;
120 IMediaSample_SetSyncPoint(pSample
, TRUE
);
121 pOutputPin
->dwSamplesProcessed
++;
123 tAviStart
= bytepos_to_duration(This
, tStart
);
124 tAviStop
= bytepos_to_duration(This
, tStart
+ MEDIATIME_FROM_BYTES(IMediaSample_GetActualDataLength(pSample
)));
126 IMediaSample_SetTime(pSample
, &tAviStart
, &tAviStop
);
128 hr
= BaseOutputPinImpl_Deliver(&pOutputPin
->pin
, pSample
);
129 if (hr
!= S_OK
&& hr
!= S_FALSE
&& hr
!= VFW_E_WRONG_STATE
)
130 ERR("Error sending sample (%x)\n", hr
);
132 /* Unset progression if denied! */
133 This
->Parser
.pInputPin
->rtCurrent
= tStart
;
136 if (tStop
>= This
->EndOfFile
|| (bytepos_to_duration(This
, tStop
) >= This
->Parser
.sourceSeeking
.llStop
) || hr
== VFW_E_NOT_CONNECTED
)
140 TRACE("End of file reached\n");
142 for (i
= 0; i
< This
->Parser
.cStreams
; i
++)
147 TRACE("Send End Of Stream to output pin %u\n", i
);
149 hr
= IPin_ConnectedTo(This
->Parser
.ppPins
[i
+1], &ppin
);
152 hr
= IPin_EndOfStream(ppin
);
162 /* Force the pullpin thread to stop */
169 static HRESULT
WAVEParser_QueryAccept(LPVOID iface
, const AM_MEDIA_TYPE
* pmt
)
171 if (!IsEqualIID(&pmt
->majortype
, &MEDIATYPE_Stream
))
173 if (IsEqualIID(&pmt
->subtype
, &MEDIASUBTYPE_WAVE
))
175 if (IsEqualIID(&pmt
->subtype
, &MEDIASUBTYPE_AU
) || IsEqualIID(&pmt
->subtype
, &MEDIASUBTYPE_AIFF
))
176 FIXME("AU and AIFF files not supported yet!\n");
180 static HRESULT WINAPI
WAVEParserImpl_seek(IMediaSeeking
*iface
)
182 WAVEParserImpl
*This
= impl_from_IMediaSeeking(iface
);
183 PullPin
*pPin
= This
->Parser
.pInputPin
;
185 LONGLONG newpos
, curpos
, endpos
, bytepos
;
187 newpos
= This
->Parser
.sourceSeeking
.llCurrent
;
188 curpos
= bytepos_to_duration(This
, pPin
->rtCurrent
);
189 endpos
= bytepos_to_duration(This
, This
->EndOfFile
);
190 bytepos
= duration_to_bytepos(This
, newpos
);
194 WARN("Requesting position %x%08x beyond end of stream %x%08x\n", (DWORD
)(newpos
>>32), (DWORD
)newpos
, (DWORD
)(endpos
>>32), (DWORD
)endpos
);
198 if (curpos
/1000000 == newpos
/1000000)
200 TRACE("Requesting position %x%08x same as current position %x%08x\n", (DWORD
)(newpos
>>32), (DWORD
)newpos
, (DWORD
)(curpos
>>32), (DWORD
)curpos
);
204 TRACE("Moving sound to %08u bytes!\n", (DWORD
)BYTES_FROM_MEDIATIME(bytepos
));
206 EnterCriticalSection(&pPin
->thread_lock
);
207 IPin_BeginFlush(&pPin
->pin
.IPin_iface
);
209 /* Make sure this is done while stopped, BeginFlush takes care of this */
210 EnterCriticalSection(&This
->Parser
.filter
.csFilter
);
211 IPin_ConnectedTo(This
->Parser
.ppPins
[1], &victim
);
214 IPin_NewSegment(victim
, newpos
, endpos
, pPin
->dRate
);
215 IPin_Release(victim
);
218 pPin
->rtStart
= pPin
->rtCurrent
= bytepos
;
219 unsafe_impl_Parser_OutputPin_from_IPin(This
->Parser
.ppPins
[1])->dwSamplesProcessed
= 0;
220 LeaveCriticalSection(&This
->Parser
.filter
.csFilter
);
222 TRACE("Done flushing\n");
223 IPin_EndFlush(&pPin
->pin
.IPin_iface
);
224 LeaveCriticalSection(&pPin
->thread_lock
);
229 static HRESULT
WAVEParser_InputPin_PreConnect(IPin
* iface
, IPin
* pConnectPin
, ALLOCATOR_PROPERTIES
*props
)
231 PullPin
*This
= impl_PullPin_from_IPin(iface
);
235 LONGLONG pos
= 0; /* in bytes */
238 WAVEParserImpl
* pWAVEParser
= impl_from_IBaseFilter(This
->pin
.pinInfo
.pFilter
);
239 LONGLONG length
, avail
;
241 piOutput
.dir
= PINDIR_OUTPUT
;
242 piOutput
.pFilter
= &pWAVEParser
->Parser
.filter
.IBaseFilter_iface
;
243 lstrcpynW(piOutput
.achName
, wcsOutputPinName
, sizeof(piOutput
.achName
) / sizeof(piOutput
.achName
[0]));
245 hr
= IAsyncReader_SyncRead(This
->pReader
, pos
, sizeof(list
), (BYTE
*)&list
);
248 if (list
.fcc
!= FOURCC_RIFF
)
250 ERR("Input stream not a RIFF file\n");
253 if (list
.cb
> 1 * 1024 * 1024 * 1024) /* cannot be more than 1Gb in size */
255 ERR("Input stream violates RIFF spec\n");
258 if (list
.fccListType
!= mmioFOURCC('W','A','V','E'))
260 ERR("Input stream not an WAVE RIFF file\n");
264 hr
= IAsyncReader_SyncRead(This
->pReader
, pos
, sizeof(chunk
), (BYTE
*)&chunk
);
265 pos
+= sizeof(chunk
);
266 if (chunk
.fcc
!= mmioFOURCC('f','m','t',' '))
268 ERR("Expected 'fmt ' chunk, but got %.04s\n", (LPSTR
)&chunk
.fcc
);
272 amt
.majortype
= MEDIATYPE_Audio
;
273 amt
.formattype
= FORMAT_WaveFormatEx
;
274 amt
.cbFormat
= chunk
.cb
;
275 amt
.pbFormat
= CoTaskMemAlloc(amt
.cbFormat
);
277 IAsyncReader_SyncRead(This
->pReader
, pos
, amt
.cbFormat
, amt
.pbFormat
);
278 amt
.subtype
= MEDIATYPE_Audio
;
279 amt
.subtype
.Data1
= ((WAVEFORMATEX
*)amt
.pbFormat
)->wFormatTag
;
282 hr
= IAsyncReader_SyncRead(This
->pReader
, pos
, sizeof(chunk
), (BYTE
*)&chunk
);
283 if (chunk
.fcc
== mmioFOURCC('f','a','c','t'))
285 FIXME("'fact' chunk not supported yet\n");
286 pos
+= sizeof(chunk
) + chunk
.cb
;
287 hr
= IAsyncReader_SyncRead(This
->pReader
, pos
, sizeof(chunk
), (BYTE
*)&chunk
);
289 if (chunk
.fcc
!= mmioFOURCC('d','a','t','a'))
291 ERR("Expected 'data' chunk, but got %.04s\n", (LPSTR
)&chunk
.fcc
);
297 pWAVEParser
->StartOfFile
= MEDIATIME_FROM_BYTES(pos
+ sizeof(RIFFCHUNK
));
298 pWAVEParser
->EndOfFile
= MEDIATIME_FROM_BYTES(pos
+ chunk
.cb
+ sizeof(RIFFCHUNK
));
304 props
->cbAlign
= ((WAVEFORMATEX
*)amt
.pbFormat
)->nBlockAlign
;
306 props
->cbBuffer
= 4096;
308 pWAVEParser
->dwSampleSize
= ((WAVEFORMATEX
*)amt
.pbFormat
)->nBlockAlign
;
309 IAsyncReader_Length(This
->pReader
, &length
, &avail
);
310 pWAVEParser
->dwLength
= length
/ (ULONGLONG
)pWAVEParser
->dwSampleSize
;
311 pWAVEParser
->nSamplesPerSec
= ((WAVEFORMATEX
*)amt
.pbFormat
)->nSamplesPerSec
;
312 hr
= Parser_AddPin(&(pWAVEParser
->Parser
), &piOutput
, props
, &amt
);
313 CoTaskMemFree(amt
.pbFormat
);
315 pWAVEParser
->Parser
.sourceSeeking
.llCurrent
= 0;
316 pWAVEParser
->Parser
.sourceSeeking
.llStop
= pWAVEParser
->Parser
.sourceSeeking
.llDuration
= bytepos_to_duration(pWAVEParser
, pWAVEParser
->EndOfFile
);
317 TRACE("Duration: %u seconds\n", (DWORD
)(pWAVEParser
->Parser
.sourceSeeking
.llDuration
/ (LONGLONG
)10000000));
319 This
->rtStop
= pWAVEParser
->EndOfFile
;
320 This
->rtStart
= pWAVEParser
->StartOfFile
;
322 TRACE("WAVE File ok\n");
327 static HRESULT
WAVEParser_Cleanup(LPVOID iface
)
329 WAVEParserImpl
*This
= iface
;
331 TRACE("(%p)->()\n", This
);
336 static HRESULT
WAVEParser_first_request(LPVOID iface
)
338 WAVEParserImpl
*This
= iface
;
339 PullPin
*pin
= This
->Parser
.pInputPin
;
341 IMediaSample
*sample
;
343 if (pin
->rtCurrent
>= pin
->rtStop
)
345 /* Last sample has already been queued, request nothing more */
350 hr
= IMemAllocator_GetBuffer(pin
->pAlloc
, &sample
, NULL
, NULL
, 0);
352 pin
->rtNext
= pin
->rtCurrent
;
355 LONGLONG rtSampleStart
= pin
->rtNext
;
356 /* Add 4 for the next header, which should hopefully work */
357 LONGLONG rtSampleStop
= rtSampleStart
+ MEDIATIME_FROM_BYTES(IMediaSample_GetSize(sample
));
358 Parser_OutputPin
*outpin
= unsafe_impl_Parser_OutputPin_from_IPin(This
->Parser
.ppPins
[1]);
360 if (rtSampleStop
> pin
->rtStop
)
361 rtSampleStop
= MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(pin
->rtStop
), pin
->cbAlign
));
363 IMediaSample_SetTime(sample
, &rtSampleStart
, &rtSampleStop
);
365 pin
->rtCurrent
= pin
->rtNext
;
366 pin
->rtNext
= rtSampleStop
;
368 IMediaSample_SetPreroll(sample
, FALSE
);
369 if (!outpin
->dwSamplesProcessed
++)
370 IMediaSample_SetDiscontinuity(sample
, TRUE
);
372 IMediaSample_SetDiscontinuity(sample
, FALSE
);
374 hr
= IAsyncReader_Request(pin
->pReader
, sample
, 0);
377 ERR("Horsemen of the apocalypse came to bring error 0x%08x %p\n", hr
, sample
);
382 static HRESULT
WAVEParser_disconnect(LPVOID iface
)
384 /* TODO: Find and plug memory leaks */
388 static const IBaseFilterVtbl WAVEParser_Vtbl
=
390 Parser_QueryInterface
,
398 Parser_SetSyncSource
,
399 Parser_GetSyncSource
,
402 Parser_QueryFilterInfo
,
403 Parser_JoinFilterGraph
,
404 Parser_QueryVendorInfo
407 HRESULT
WAVEParser_create(IUnknown
* pUnkOuter
, LPVOID
* ppv
)
410 WAVEParserImpl
* This
;
412 TRACE("(%p, %p)\n", pUnkOuter
, ppv
);
417 return CLASS_E_NOAGGREGATION
;
419 /* Note: This memory is managed by the transform filter once created */
420 This
= CoTaskMemAlloc(sizeof(WAVEParserImpl
));
422 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
);