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
23 #include "quartz_private.h"
37 #include "wine/unicode.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(quartz
);
42 typedef struct ACMWrapperImpl
45 IUnknown
*seekthru_unk
;
49 LPWAVEFORMATEX pWfOut
;
51 LONGLONG lasttime_real
;
52 LONGLONG lasttime_sent
;
55 static const IBaseFilterVtbl ACMWrapper_Vtbl
;
57 static inline ACMWrapperImpl
*impl_from_TransformFilter( TransformFilter
*iface
)
59 return CONTAINING_RECORD(iface
, ACMWrapperImpl
, tf
.filter
);
62 static inline ACMWrapperImpl
*impl_from_IBaseFilter( IBaseFilter
*iface
)
64 return CONTAINING_RECORD(iface
, ACMWrapperImpl
, tf
.filter
.IBaseFilter_iface
);
67 static HRESULT WINAPI
ACMWrapper_Receive(TransformFilter
*tf
, IMediaSample
*pSample
)
69 ACMWrapperImpl
* This
= impl_from_TransformFilter(tf
);
71 IMediaSample
* pOutSample
= NULL
;
72 DWORD cbDstStream
, cbSrcStream
;
74 LPBYTE pbSrcStream
= NULL
;
76 BOOL unprepare_header
= FALSE
, preroll
;
79 LONGLONG tStart
= -1, tStop
= -1, tMed
;
80 LONGLONG mtStart
= -1, mtStop
= -1, mtMed
;
82 EnterCriticalSection(&This
->tf
.csReceive
);
83 hr
= IMediaSample_GetPointer(pSample
, &pbSrcStream
);
86 ERR("Cannot get pointer to sample data (%x)\n", hr
);
87 LeaveCriticalSection(&This
->tf
.csReceive
);
91 preroll
= (IMediaSample_IsPreroll(pSample
) == S_OK
);
93 IMediaSample_GetTime(pSample
, &tStart
, &tStop
);
94 if (IMediaSample_GetMediaTime(pSample
, &mtStart
, &mtStop
) != S_OK
)
95 mtStart
= mtStop
= -1;
96 cbSrcStream
= IMediaSample_GetActualDataLength(pSample
);
98 /* Prevent discontinuities when codecs 'absorb' data but not give anything back in return */
99 if (IMediaSample_IsDiscontinuity(pSample
) == S_OK
)
101 This
->lasttime_real
= tStart
;
102 This
->lasttime_sent
= tStart
;
104 else if (This
->lasttime_real
== tStart
)
105 tStart
= This
->lasttime_sent
;
107 WARN("Discontinuity\n");
112 TRACE("Sample data ptr = %p, size = %d\n", pbSrcStream
, cbSrcStream
);
114 hr
= IPin_ConnectionMediaType(This
->tf
.ppPins
[0], &amt
);
117 ERR("Unable to retrieve media type\n");
118 LeaveCriticalSection(&This
->tf
.csReceive
);
122 ash
.pbSrc
= pbSrcStream
;
123 ash
.cbSrcLength
= cbSrcStream
;
125 while(hr
== S_OK
&& ash
.cbSrcLength
)
127 hr
= BaseOutputPinImpl_GetDeliveryBuffer((BaseOutputPin
*)This
->tf
.ppPins
[1], &pOutSample
, NULL
, NULL
, 0);
130 ERR("Unable to get delivery buffer (%x)\n", hr
);
131 LeaveCriticalSection(&This
->tf
.csReceive
);
134 IMediaSample_SetPreroll(pOutSample
, preroll
);
136 hr
= IMediaSample_SetActualDataLength(pOutSample
, 0);
139 hr
= IMediaSample_GetPointer(pOutSample
, &pbDstStream
);
141 ERR("Unable to get pointer to buffer (%x)\n", hr
);
144 cbDstStream
= IMediaSample_GetSize(pOutSample
);
146 ash
.cbStruct
= sizeof(ash
);
149 ash
.pbDst
= pbDstStream
;
150 ash
.cbDstLength
= cbDstStream
;
152 if ((res
= acmStreamPrepareHeader(This
->has
, &ash
, 0))) {
153 ERR("Cannot prepare header %d\n", res
);
156 unprepare_header
= TRUE
;
158 if (IMediaSample_IsDiscontinuity(pSample
) == S_OK
)
160 res
= acmStreamConvert(This
->has
, &ash
, ACM_STREAMCONVERTF_START
);
161 IMediaSample_SetDiscontinuity(pOutSample
, TRUE
);
162 /* One sample could be converted to multiple packets */
163 IMediaSample_SetDiscontinuity(pSample
, FALSE
);
167 res
= acmStreamConvert(This
->has
, &ash
, 0);
168 IMediaSample_SetDiscontinuity(pOutSample
, FALSE
);
173 if(res
!= MMSYSERR_MOREDATA
)
174 ERR("Cannot convert data header %d\n", res
);
178 TRACE("used in %u/%u, used out %u/%u\n", ash
.cbSrcLengthUsed
, ash
.cbSrcLength
, ash
.cbDstLengthUsed
, ash
.cbDstLength
);
180 hr
= IMediaSample_SetActualDataLength(pOutSample
, ash
.cbDstLengthUsed
);
183 /* Bug in acm codecs? It apparently uses the input, but doesn't necessarily output immediately */
184 if (!ash
.cbSrcLengthUsed
)
186 WARN("Sample was skipped? Outputted: %u\n", ash
.cbDstLengthUsed
);
191 TRACE("Sample start time: %u.%03u\n", (DWORD
)(tStart
/10000000), (DWORD
)((tStart
/10000)%1000));
192 if (ash
.cbSrcLengthUsed
== cbSrcStream
)
194 IMediaSample_SetTime(pOutSample
, &tStart
, &tStop
);
195 tStart
= tMed
= tStop
;
197 else if (tStop
!= tStart
)
199 tMed
= tStop
- tStart
;
200 tMed
= tStart
+ tMed
* ash
.cbSrcLengthUsed
/ cbSrcStream
;
201 IMediaSample_SetTime(pOutSample
, &tStart
, &tMed
);
206 ERR("No valid timestamp found\n");
207 IMediaSample_SetTime(pOutSample
, NULL
, NULL
);
211 IMediaSample_SetMediaTime(pOutSample
, NULL
, NULL
);
212 } else if (ash
.cbSrcLengthUsed
== cbSrcStream
) {
213 IMediaSample_SetMediaTime(pOutSample
, &mtStart
, &mtStop
);
214 mtStart
= mtMed
= mtStop
;
215 } else if (mtStop
>= mtStart
) {
216 mtMed
= mtStop
- mtStart
;
217 mtMed
= mtStart
+ mtMed
* ash
.cbSrcLengthUsed
/ cbSrcStream
;
218 IMediaSample_SetMediaTime(pOutSample
, &mtStart
, &mtMed
);
221 IMediaSample_SetMediaTime(pOutSample
, NULL
, NULL
);
224 TRACE("Sample stop time: %u.%03u\n", (DWORD
)(tStart
/10000000), (DWORD
)((tStart
/10000)%1000));
226 LeaveCriticalSection(&This
->tf
.csReceive
);
227 hr
= BaseOutputPinImpl_Deliver((BaseOutputPin
*)This
->tf
.ppPins
[1], pOutSample
);
228 EnterCriticalSection(&This
->tf
.csReceive
);
230 if (hr
!= S_OK
&& hr
!= VFW_E_NOT_CONNECTED
) {
232 ERR("Error sending sample (%x)\n", hr
);
237 if (unprepare_header
&& (res
= acmStreamUnprepareHeader(This
->has
, &ash
, 0)))
238 ERR("Cannot unprepare header %d\n", res
);
239 unprepare_header
= FALSE
;
240 ash
.pbSrc
+= ash
.cbSrcLengthUsed
;
241 ash
.cbSrcLength
-= ash
.cbSrcLengthUsed
;
243 IMediaSample_Release(pOutSample
);
248 This
->lasttime_real
= tStop
;
249 This
->lasttime_sent
= tMed
;
251 LeaveCriticalSection(&This
->tf
.csReceive
);
255 static HRESULT WINAPI
ACMWrapper_SetMediaType(TransformFilter
*tf
, PIN_DIRECTION dir
, const AM_MEDIA_TYPE
* pmt
)
257 ACMWrapperImpl
* This
= impl_from_TransformFilter(tf
);
260 TRACE("(%p)->(%i %p)\n", This
, dir
, pmt
);
262 if (dir
!= PINDIR_INPUT
)
265 /* Check root (GUID w/o FOURCC) */
266 if ((IsEqualIID(&pmt
->majortype
, &MEDIATYPE_Audio
)) &&
267 (!memcmp(((const char *)&pmt
->subtype
)+4, ((const char *)&MEDIATYPE_Audio
)+4, sizeof(GUID
)-4)) &&
268 (IsEqualIID(&pmt
->formattype
, &FORMAT_WaveFormatEx
)))
271 WAVEFORMATEX
*wfx
= (WAVEFORMATEX
*)pmt
->pbFormat
;
272 AM_MEDIA_TYPE
* outpmt
= &This
->tf
.pmt
;
274 if (!wfx
|| wfx
->wFormatTag
== WAVE_FORMAT_PCM
|| wfx
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
)
275 return VFW_E_TYPE_NOT_ACCEPTED
;
276 FreeMediaType(outpmt
);
278 This
->pWfIn
= (LPWAVEFORMATEX
)pmt
->pbFormat
;
281 /* TRACE("ALIGN = %d\n", pACMWrapper->pWfIn->nBlockAlign); */
282 /* pACMWrapper->pWfIn->nBlockAlign = 1; */
284 /* Set output audio data to PCM */
285 CopyMediaType(outpmt
, pmt
);
286 outpmt
->subtype
.Data1
= WAVE_FORMAT_PCM
;
287 This
->pWfOut
= (WAVEFORMATEX
*)outpmt
->pbFormat
;
288 This
->pWfOut
->wFormatTag
= WAVE_FORMAT_PCM
;
289 This
->pWfOut
->wBitsPerSample
= 16;
290 This
->pWfOut
->nBlockAlign
= This
->pWfOut
->wBitsPerSample
* This
->pWfOut
->nChannels
/ 8;
291 This
->pWfOut
->cbSize
= 0;
292 This
->pWfOut
->nAvgBytesPerSec
= This
->pWfOut
->nChannels
* This
->pWfOut
->nSamplesPerSec
293 * (This
->pWfOut
->wBitsPerSample
/8);
295 if (!(res
= acmStreamOpen(&drv
, NULL
, This
->pWfIn
, This
->pWfOut
, NULL
, 0, 0, 0)))
299 TRACE("Connection accepted\n");
303 FIXME("acmStreamOpen returned %d\n", res
);
304 FreeMediaType(outpmt
);
305 TRACE("Unable to find a suitable ACM decompressor\n");
308 TRACE("Connection refused\n");
309 return VFW_E_TYPE_NOT_ACCEPTED
;
312 static HRESULT WINAPI
ACMWrapper_CompleteConnect(TransformFilter
*tf
, PIN_DIRECTION dir
, IPin
*pin
)
314 ACMWrapperImpl
* This
= impl_from_TransformFilter(tf
);
318 TRACE("(%p)\n", This
);
320 if (dir
!= PINDIR_INPUT
)
323 if (!(res
= acmStreamOpen(&drv
, NULL
, This
->pWfIn
, This
->pWfOut
, NULL
, 0, 0, 0)))
327 TRACE("Connection accepted\n");
331 FIXME("acmStreamOpen returned %d\n", res
);
332 TRACE("Unable to find a suitable ACM decompressor\n");
333 return VFW_E_TYPE_NOT_ACCEPTED
;
336 static HRESULT WINAPI
ACMWrapper_BreakConnect(TransformFilter
*tf
, PIN_DIRECTION dir
)
338 ACMWrapperImpl
*This
= impl_from_TransformFilter(tf
);
340 TRACE("(%p)->(%i)\n", This
,dir
);
342 if (dir
== PINDIR_INPUT
)
345 acmStreamClose(This
->has
, 0);
348 This
->lasttime_real
= This
->lasttime_sent
= -1;
354 static HRESULT WINAPI
ACMWrapper_DecideBufferSize(TransformFilter
*tf
, IMemAllocator
*pAlloc
, ALLOCATOR_PROPERTIES
*ppropInputRequest
)
356 ACMWrapperImpl
*pACM
= impl_from_TransformFilter(tf
);
357 ALLOCATOR_PROPERTIES actual
;
359 if (!ppropInputRequest
->cbAlign
)
360 ppropInputRequest
->cbAlign
= 1;
362 if (ppropInputRequest
->cbBuffer
< pACM
->pWfOut
->nAvgBytesPerSec
/ 2)
363 ppropInputRequest
->cbBuffer
= pACM
->pWfOut
->nAvgBytesPerSec
/ 2;
365 if (!ppropInputRequest
->cBuffers
)
366 ppropInputRequest
->cBuffers
= 1;
368 return IMemAllocator_SetProperties(pAlloc
, ppropInputRequest
, &actual
);
371 static const TransformFilterFuncTable ACMWrapper_FuncsTable
= {
372 ACMWrapper_DecideBufferSize
,
377 ACMWrapper_SetMediaType
,
378 ACMWrapper_CompleteConnect
,
379 ACMWrapper_BreakConnect
,
386 HRESULT
ACMWrapper_create(IUnknown
* pUnkOuter
, LPVOID
* ppv
)
389 ACMWrapperImpl
* This
;
391 TRACE("(%p, %p)\n", pUnkOuter
, ppv
);
396 return CLASS_E_NOAGGREGATION
;
398 hr
= TransformFilter_Construct(&ACMWrapper_Vtbl
, sizeof(ACMWrapperImpl
), &CLSID_ACMWrapper
, &ACMWrapper_FuncsTable
, (IBaseFilter
**)&This
);
404 ISeekingPassThru
*passthru
;
405 hr
= CoCreateInstance(&CLSID_SeekingPassThru
, (IUnknown
*)This
, CLSCTX_INPROC_SERVER
, &IID_IUnknown
, (void**)&This
->seekthru_unk
);
406 IUnknown_QueryInterface(This
->seekthru_unk
, &IID_ISeekingPassThru
, (void**)&passthru
);
407 ISeekingPassThru_Init(passthru
, FALSE
, This
->tf
.ppPins
[0]);
408 ISeekingPassThru_Release(passthru
);
412 This
->lasttime_real
= This
->lasttime_sent
= -1;
417 static HRESULT WINAPI
ACMWrapper_QueryInterface(IBaseFilter
* iface
, REFIID riid
, LPVOID
* ppv
)
420 ACMWrapperImpl
*This
= impl_from_IBaseFilter(iface
);
421 TRACE("(%p/%p)->(%s, %p)\n", This
, iface
, qzdebugstr_guid(riid
), ppv
);
423 if (IsEqualIID(riid
, &IID_IMediaSeeking
))
424 return IUnknown_QueryInterface(This
->seekthru_unk
, riid
, ppv
);
426 hr
= TransformFilterImpl_QueryInterface(iface
, riid
, ppv
);
432 static const IBaseFilterVtbl ACMWrapper_Vtbl
=
434 ACMWrapper_QueryInterface
,
435 BaseFilterImpl_AddRef
,
436 TransformFilterImpl_Release
,
437 BaseFilterImpl_GetClassID
,
438 TransformFilterImpl_Stop
,
439 TransformFilterImpl_Pause
,
440 TransformFilterImpl_Run
,
441 BaseFilterImpl_GetState
,
442 BaseFilterImpl_SetSyncSource
,
443 BaseFilterImpl_GetSyncSource
,
444 BaseFilterImpl_EnumPins
,
445 TransformFilterImpl_FindPin
,
446 BaseFilterImpl_QueryFilterInfo
,
447 BaseFilterImpl_JoinFilterGraph
,
448 BaseFilterImpl_QueryVendorInfo