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
48 LPWAVEFORMATEX pWfOut
;
50 LONGLONG lasttime_real
;
51 LONGLONG lasttime_sent
;
54 static const IBaseFilterVtbl ACMWrapper_Vtbl
;
56 static inline ACMWrapperImpl
*impl_from_TransformFilter( TransformFilter
*iface
)
58 return CONTAINING_RECORD(iface
, ACMWrapperImpl
, tf
.filter
);
61 static HRESULT WINAPI
ACMWrapper_Receive(TransformFilter
*tf
, IMediaSample
*pSample
)
63 ACMWrapperImpl
* This
= impl_from_TransformFilter(tf
);
65 IMediaSample
* pOutSample
= NULL
;
66 DWORD cbDstStream
, cbSrcStream
;
68 LPBYTE pbSrcStream
= NULL
;
70 BOOL unprepare_header
= FALSE
, preroll
;
73 LONGLONG tStart
= -1, tStop
= -1, tMed
;
74 LONGLONG mtStart
= -1, mtStop
= -1, mtMed
;
76 EnterCriticalSection(&This
->tf
.csReceive
);
77 hr
= IMediaSample_GetPointer(pSample
, &pbSrcStream
);
80 ERR("Cannot get pointer to sample data (%x)\n", hr
);
81 LeaveCriticalSection(&This
->tf
.csReceive
);
85 preroll
= (IMediaSample_IsPreroll(pSample
) == S_OK
);
87 IMediaSample_GetTime(pSample
, &tStart
, &tStop
);
88 if (IMediaSample_GetMediaTime(pSample
, &mtStart
, &mtStop
) != S_OK
)
89 mtStart
= mtStop
= -1;
90 cbSrcStream
= IMediaSample_GetActualDataLength(pSample
);
92 /* Prevent discontinuities when codecs 'absorb' data but not give anything back in return */
93 if (IMediaSample_IsDiscontinuity(pSample
) == S_OK
)
95 This
->lasttime_real
= tStart
;
96 This
->lasttime_sent
= tStart
;
98 else if (This
->lasttime_real
== tStart
)
99 tStart
= This
->lasttime_sent
;
101 WARN("Discontinuity\n");
106 TRACE("Sample data ptr = %p, size = %d\n", pbSrcStream
, cbSrcStream
);
108 hr
= IPin_ConnectionMediaType(This
->tf
.ppPins
[0], &amt
);
111 ERR("Unable to retrieve media type\n");
112 LeaveCriticalSection(&This
->tf
.csReceive
);
116 ash
.pbSrc
= pbSrcStream
;
117 ash
.cbSrcLength
= cbSrcStream
;
119 while(hr
== S_OK
&& ash
.cbSrcLength
)
121 hr
= BaseOutputPinImpl_GetDeliveryBuffer((BaseOutputPin
*)This
->tf
.ppPins
[1], &pOutSample
, NULL
, NULL
, 0);
124 ERR("Unable to get delivery buffer (%x)\n", hr
);
125 LeaveCriticalSection(&This
->tf
.csReceive
);
128 IMediaSample_SetPreroll(pOutSample
, preroll
);
130 hr
= IMediaSample_SetActualDataLength(pOutSample
, 0);
133 hr
= IMediaSample_GetPointer(pOutSample
, &pbDstStream
);
135 ERR("Unable to get pointer to buffer (%x)\n", hr
);
138 cbDstStream
= IMediaSample_GetSize(pOutSample
);
140 ash
.cbStruct
= sizeof(ash
);
143 ash
.pbDst
= pbDstStream
;
144 ash
.cbDstLength
= cbDstStream
;
146 if ((res
= acmStreamPrepareHeader(This
->has
, &ash
, 0))) {
147 ERR("Cannot prepare header %d\n", res
);
150 unprepare_header
= TRUE
;
152 if (IMediaSample_IsDiscontinuity(pSample
) == S_OK
)
154 res
= acmStreamConvert(This
->has
, &ash
, ACM_STREAMCONVERTF_START
);
155 IMediaSample_SetDiscontinuity(pOutSample
, TRUE
);
156 /* One sample could be converted to multiple packets */
157 IMediaSample_SetDiscontinuity(pSample
, FALSE
);
161 res
= acmStreamConvert(This
->has
, &ash
, 0);
162 IMediaSample_SetDiscontinuity(pOutSample
, FALSE
);
167 if(res
!= MMSYSERR_MOREDATA
)
168 ERR("Cannot convert data header %d\n", res
);
172 TRACE("used in %u/%u, used out %u/%u\n", ash
.cbSrcLengthUsed
, ash
.cbSrcLength
, ash
.cbDstLengthUsed
, ash
.cbDstLength
);
174 hr
= IMediaSample_SetActualDataLength(pOutSample
, ash
.cbDstLengthUsed
);
177 /* Bug in acm codecs? It apparently uses the input, but doesn't necessarily output immediately */
178 if (!ash
.cbSrcLengthUsed
)
180 WARN("Sample was skipped? Outputted: %u\n", ash
.cbDstLengthUsed
);
185 TRACE("Sample start time: %u.%03u\n", (DWORD
)(tStart
/10000000), (DWORD
)((tStart
/10000)%1000));
186 if (ash
.cbSrcLengthUsed
== cbSrcStream
)
188 IMediaSample_SetTime(pOutSample
, &tStart
, &tStop
);
189 tStart
= tMed
= tStop
;
191 else if (tStop
!= tStart
)
193 tMed
= tStop
- tStart
;
194 tMed
= tStart
+ tMed
* ash
.cbSrcLengthUsed
/ cbSrcStream
;
195 IMediaSample_SetTime(pOutSample
, &tStart
, &tMed
);
200 ERR("No valid timestamp found\n");
201 IMediaSample_SetTime(pOutSample
, NULL
, NULL
);
205 IMediaSample_SetMediaTime(pOutSample
, NULL
, NULL
);
206 } else if (ash
.cbSrcLengthUsed
== cbSrcStream
) {
207 IMediaSample_SetMediaTime(pOutSample
, &mtStart
, &mtStop
);
208 mtStart
= mtMed
= mtStop
;
209 } else if (mtStop
>= mtStart
) {
210 mtMed
= mtStop
- mtStart
;
211 mtMed
= mtStart
+ mtMed
* ash
.cbSrcLengthUsed
/ cbSrcStream
;
212 IMediaSample_SetMediaTime(pOutSample
, &mtStart
, &mtMed
);
215 IMediaSample_SetMediaTime(pOutSample
, NULL
, NULL
);
218 TRACE("Sample stop time: %u.%03u\n", (DWORD
)(tStart
/10000000), (DWORD
)((tStart
/10000)%1000));
220 LeaveCriticalSection(&This
->tf
.csReceive
);
221 hr
= BaseOutputPinImpl_Deliver((BaseOutputPin
*)This
->tf
.ppPins
[1], pOutSample
);
222 EnterCriticalSection(&This
->tf
.csReceive
);
224 if (hr
!= S_OK
&& hr
!= VFW_E_NOT_CONNECTED
) {
226 ERR("Error sending sample (%x)\n", hr
);
231 if (unprepare_header
&& (res
= acmStreamUnprepareHeader(This
->has
, &ash
, 0)))
232 ERR("Cannot unprepare header %d\n", res
);
233 unprepare_header
= FALSE
;
234 ash
.pbSrc
+= ash
.cbSrcLengthUsed
;
235 ash
.cbSrcLength
-= ash
.cbSrcLengthUsed
;
237 IMediaSample_Release(pOutSample
);
242 This
->lasttime_real
= tStop
;
243 This
->lasttime_sent
= tMed
;
245 LeaveCriticalSection(&This
->tf
.csReceive
);
249 static HRESULT WINAPI
ACMWrapper_SetMediaType(TransformFilter
*tf
, PIN_DIRECTION dir
, const AM_MEDIA_TYPE
* pmt
)
251 ACMWrapperImpl
* This
= impl_from_TransformFilter(tf
);
254 TRACE("(%p)->(%i %p)\n", This
, dir
, pmt
);
256 if (dir
!= PINDIR_INPUT
)
259 /* Check root (GUID w/o FOURCC) */
260 if ((IsEqualIID(&pmt
->majortype
, &MEDIATYPE_Audio
)) &&
261 (!memcmp(((const char *)&pmt
->subtype
)+4, ((const char *)&MEDIATYPE_Audio
)+4, sizeof(GUID
)-4)) &&
262 (IsEqualIID(&pmt
->formattype
, &FORMAT_WaveFormatEx
)))
265 WAVEFORMATEX
*wfx
= (WAVEFORMATEX
*)pmt
->pbFormat
;
266 AM_MEDIA_TYPE
* outpmt
= &This
->tf
.pmt
;
268 if (!wfx
|| wfx
->wFormatTag
== WAVE_FORMAT_PCM
|| wfx
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
)
269 return VFW_E_TYPE_NOT_ACCEPTED
;
270 FreeMediaType(outpmt
);
272 This
->pWfIn
= (LPWAVEFORMATEX
)pmt
->pbFormat
;
275 /* TRACE("ALIGN = %d\n", pACMWrapper->pWfIn->nBlockAlign); */
276 /* pACMWrapper->pWfIn->nBlockAlign = 1; */
278 /* Set output audio data to PCM */
279 CopyMediaType(outpmt
, pmt
);
280 outpmt
->subtype
.Data1
= WAVE_FORMAT_PCM
;
281 This
->pWfOut
= (WAVEFORMATEX
*)outpmt
->pbFormat
;
282 This
->pWfOut
->wFormatTag
= WAVE_FORMAT_PCM
;
283 This
->pWfOut
->wBitsPerSample
= 16;
284 This
->pWfOut
->nBlockAlign
= This
->pWfOut
->wBitsPerSample
* This
->pWfOut
->nChannels
/ 8;
285 This
->pWfOut
->cbSize
= 0;
286 This
->pWfOut
->nAvgBytesPerSec
= This
->pWfOut
->nChannels
* This
->pWfOut
->nSamplesPerSec
287 * (This
->pWfOut
->wBitsPerSample
/8);
289 if (!(res
= acmStreamOpen(&drv
, NULL
, This
->pWfIn
, This
->pWfOut
, NULL
, 0, 0, 0)))
293 TRACE("Connection accepted\n");
297 FIXME("acmStreamOpen returned %d\n", res
);
298 FreeMediaType(outpmt
);
299 TRACE("Unable to find a suitable ACM decompressor\n");
302 TRACE("Connection refused\n");
303 return VFW_E_TYPE_NOT_ACCEPTED
;
306 static HRESULT WINAPI
ACMWrapper_CompleteConnect(TransformFilter
*tf
, PIN_DIRECTION dir
, IPin
*pin
)
308 ACMWrapperImpl
* This
= impl_from_TransformFilter(tf
);
312 TRACE("(%p)\n", This
);
314 if (dir
!= PINDIR_INPUT
)
317 if (!(res
= acmStreamOpen(&drv
, NULL
, This
->pWfIn
, This
->pWfOut
, NULL
, 0, 0, 0)))
321 TRACE("Connection accepted\n");
325 FIXME("acmStreamOpen returned %d\n", res
);
326 TRACE("Unable to find a suitable ACM decompressor\n");
327 return VFW_E_TYPE_NOT_ACCEPTED
;
330 static HRESULT WINAPI
ACMWrapper_BreakConnect(TransformFilter
*tf
, PIN_DIRECTION dir
)
332 ACMWrapperImpl
*This
= impl_from_TransformFilter(tf
);
334 TRACE("(%p)->(%i)\n", This
,dir
);
336 if (dir
== PINDIR_INPUT
)
339 acmStreamClose(This
->has
, 0);
342 This
->lasttime_real
= This
->lasttime_sent
= -1;
348 static HRESULT WINAPI
ACMWrapper_DecideBufferSize(TransformFilter
*tf
, IMemAllocator
*pAlloc
, ALLOCATOR_PROPERTIES
*ppropInputRequest
)
350 ACMWrapperImpl
*pACM
= impl_from_TransformFilter(tf
);
351 ALLOCATOR_PROPERTIES actual
;
353 if (!ppropInputRequest
->cbAlign
)
354 ppropInputRequest
->cbAlign
= 1;
356 if (ppropInputRequest
->cbBuffer
< pACM
->pWfOut
->nAvgBytesPerSec
/ 2)
357 ppropInputRequest
->cbBuffer
= pACM
->pWfOut
->nAvgBytesPerSec
/ 2;
359 if (!ppropInputRequest
->cBuffers
)
360 ppropInputRequest
->cBuffers
= 1;
362 return IMemAllocator_SetProperties(pAlloc
, ppropInputRequest
, &actual
);
365 static const TransformFilterFuncTable ACMWrapper_FuncsTable
= {
366 ACMWrapper_DecideBufferSize
,
371 ACMWrapper_SetMediaType
,
372 ACMWrapper_CompleteConnect
,
373 ACMWrapper_BreakConnect
,
380 HRESULT
ACMWrapper_create(IUnknown
* pUnkOuter
, LPVOID
* ppv
)
383 ACMWrapperImpl
* This
;
385 TRACE("(%p, %p)\n", pUnkOuter
, ppv
);
390 return CLASS_E_NOAGGREGATION
;
392 hr
= TransformFilter_Construct(&ACMWrapper_Vtbl
, sizeof(ACMWrapperImpl
), &CLSID_ACMWrapper
, &ACMWrapper_FuncsTable
, (IBaseFilter
**)&This
);
397 *ppv
= &This
->tf
.filter
.IBaseFilter_iface
;
398 This
->lasttime_real
= This
->lasttime_sent
= -1;
403 static const IBaseFilterVtbl ACMWrapper_Vtbl
=
405 TransformFilterImpl_QueryInterface
,
406 BaseFilterImpl_AddRef
,
407 TransformFilterImpl_Release
,
408 BaseFilterImpl_GetClassID
,
409 TransformFilterImpl_Stop
,
410 TransformFilterImpl_Pause
,
411 TransformFilterImpl_Run
,
412 BaseFilterImpl_GetState
,
413 BaseFilterImpl_SetSyncSource
,
414 BaseFilterImpl_GetSyncSource
,
415 BaseFilterImpl_EnumPins
,
416 TransformFilterImpl_FindPin
,
417 BaseFilterImpl_QueryFilterInfo
,
418 BaseFilterImpl_JoinFilterGraph
,
419 BaseFilterImpl_QueryVendorInfo