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 inline ACMWrapperImpl
*impl_from_IBaseFilter( IBaseFilter
*iface
)
63 return CONTAINING_RECORD(iface
, ACMWrapperImpl
, tf
.filter
.IBaseFilter_iface
);
66 static HRESULT WINAPI
ACMWrapper_Receive(TransformFilter
*tf
, IMediaSample
*pSample
)
68 ACMWrapperImpl
* This
= impl_from_TransformFilter(tf
);
70 IMediaSample
* pOutSample
= NULL
;
71 DWORD cbDstStream
, cbSrcStream
;
73 LPBYTE pbSrcStream
= NULL
;
75 BOOL unprepare_header
= FALSE
, preroll
;
78 LONGLONG tStart
= -1, tStop
= -1, tMed
;
79 LONGLONG mtStart
= -1, mtStop
= -1, mtMed
;
81 EnterCriticalSection(&This
->tf
.csReceive
);
82 hr
= IMediaSample_GetPointer(pSample
, &pbSrcStream
);
85 ERR("Cannot get pointer to sample data (%x)\n", hr
);
86 LeaveCriticalSection(&This
->tf
.csReceive
);
90 preroll
= (IMediaSample_IsPreroll(pSample
) == S_OK
);
92 IMediaSample_GetTime(pSample
, &tStart
, &tStop
);
93 if (IMediaSample_GetMediaTime(pSample
, &mtStart
, &mtStop
) != S_OK
)
94 mtStart
= mtStop
= -1;
95 cbSrcStream
= IMediaSample_GetActualDataLength(pSample
);
97 /* Prevent discontinuities when codecs 'absorb' data but not give anything back in return */
98 if (IMediaSample_IsDiscontinuity(pSample
) == S_OK
)
100 This
->lasttime_real
= tStart
;
101 This
->lasttime_sent
= tStart
;
103 else if (This
->lasttime_real
== tStart
)
104 tStart
= This
->lasttime_sent
;
106 WARN("Discontinuity\n");
111 TRACE("Sample data ptr = %p, size = %d\n", pbSrcStream
, cbSrcStream
);
113 hr
= IPin_ConnectionMediaType(This
->tf
.ppPins
[0], &amt
);
116 ERR("Unable to retrieve media type\n");
117 LeaveCriticalSection(&This
->tf
.csReceive
);
121 ash
.pbSrc
= pbSrcStream
;
122 ash
.cbSrcLength
= cbSrcStream
;
124 while(hr
== S_OK
&& ash
.cbSrcLength
)
126 hr
= BaseOutputPinImpl_GetDeliveryBuffer((BaseOutputPin
*)This
->tf
.ppPins
[1], &pOutSample
, NULL
, NULL
, 0);
129 ERR("Unable to get delivery buffer (%x)\n", hr
);
130 LeaveCriticalSection(&This
->tf
.csReceive
);
133 IMediaSample_SetPreroll(pOutSample
, preroll
);
135 hr
= IMediaSample_SetActualDataLength(pOutSample
, 0);
138 hr
= IMediaSample_GetPointer(pOutSample
, &pbDstStream
);
140 ERR("Unable to get pointer to buffer (%x)\n", hr
);
143 cbDstStream
= IMediaSample_GetSize(pOutSample
);
145 ash
.cbStruct
= sizeof(ash
);
148 ash
.pbDst
= pbDstStream
;
149 ash
.cbDstLength
= cbDstStream
;
151 if ((res
= acmStreamPrepareHeader(This
->has
, &ash
, 0))) {
152 ERR("Cannot prepare header %d\n", res
);
155 unprepare_header
= TRUE
;
157 if (IMediaSample_IsDiscontinuity(pSample
) == S_OK
)
159 res
= acmStreamConvert(This
->has
, &ash
, ACM_STREAMCONVERTF_START
);
160 IMediaSample_SetDiscontinuity(pOutSample
, TRUE
);
161 /* One sample could be converted to multiple packets */
162 IMediaSample_SetDiscontinuity(pSample
, FALSE
);
166 res
= acmStreamConvert(This
->has
, &ash
, 0);
167 IMediaSample_SetDiscontinuity(pOutSample
, FALSE
);
172 if(res
!= MMSYSERR_MOREDATA
)
173 ERR("Cannot convert data header %d\n", res
);
177 TRACE("used in %u/%u, used out %u/%u\n", ash
.cbSrcLengthUsed
, ash
.cbSrcLength
, ash
.cbDstLengthUsed
, ash
.cbDstLength
);
179 hr
= IMediaSample_SetActualDataLength(pOutSample
, ash
.cbDstLengthUsed
);
182 /* Bug in acm codecs? It apparently uses the input, but doesn't necessarily output immediately */
183 if (!ash
.cbSrcLengthUsed
)
185 WARN("Sample was skipped? Outputted: %u\n", ash
.cbDstLengthUsed
);
190 TRACE("Sample start time: %u.%03u\n", (DWORD
)(tStart
/10000000), (DWORD
)((tStart
/10000)%1000));
191 if (ash
.cbSrcLengthUsed
== cbSrcStream
)
193 IMediaSample_SetTime(pOutSample
, &tStart
, &tStop
);
194 tStart
= tMed
= tStop
;
196 else if (tStop
!= tStart
)
198 tMed
= tStop
- tStart
;
199 tMed
= tStart
+ tMed
* ash
.cbSrcLengthUsed
/ cbSrcStream
;
200 IMediaSample_SetTime(pOutSample
, &tStart
, &tMed
);
205 ERR("No valid timestamp found\n");
206 IMediaSample_SetTime(pOutSample
, NULL
, NULL
);
210 IMediaSample_SetMediaTime(pOutSample
, NULL
, NULL
);
211 } else if (ash
.cbSrcLengthUsed
== cbSrcStream
) {
212 IMediaSample_SetMediaTime(pOutSample
, &mtStart
, &mtStop
);
213 mtStart
= mtMed
= mtStop
;
214 } else if (mtStop
>= mtStart
) {
215 mtMed
= mtStop
- mtStart
;
216 mtMed
= mtStart
+ mtMed
* ash
.cbSrcLengthUsed
/ cbSrcStream
;
217 IMediaSample_SetMediaTime(pOutSample
, &mtStart
, &mtMed
);
220 IMediaSample_SetMediaTime(pOutSample
, NULL
, NULL
);
223 TRACE("Sample stop time: %u.%03u\n", (DWORD
)(tStart
/10000000), (DWORD
)((tStart
/10000)%1000));
225 LeaveCriticalSection(&This
->tf
.csReceive
);
226 hr
= BaseOutputPinImpl_Deliver((BaseOutputPin
*)This
->tf
.ppPins
[1], pOutSample
);
227 EnterCriticalSection(&This
->tf
.csReceive
);
229 if (hr
!= S_OK
&& hr
!= VFW_E_NOT_CONNECTED
) {
231 ERR("Error sending sample (%x)\n", hr
);
236 if (unprepare_header
&& (res
= acmStreamUnprepareHeader(This
->has
, &ash
, 0)))
237 ERR("Cannot unprepare header %d\n", res
);
238 unprepare_header
= FALSE
;
239 ash
.pbSrc
+= ash
.cbSrcLengthUsed
;
240 ash
.cbSrcLength
-= ash
.cbSrcLengthUsed
;
242 IMediaSample_Release(pOutSample
);
247 This
->lasttime_real
= tStop
;
248 This
->lasttime_sent
= tMed
;
250 LeaveCriticalSection(&This
->tf
.csReceive
);
254 static HRESULT WINAPI
ACMWrapper_SetMediaType(TransformFilter
*tf
, PIN_DIRECTION dir
, const AM_MEDIA_TYPE
* pmt
)
256 ACMWrapperImpl
* This
= impl_from_TransformFilter(tf
);
259 TRACE("(%p)->(%i %p)\n", This
, dir
, pmt
);
261 if (dir
!= PINDIR_INPUT
)
264 /* Check root (GUID w/o FOURCC) */
265 if ((IsEqualIID(&pmt
->majortype
, &MEDIATYPE_Audio
)) &&
266 (!memcmp(((const char *)&pmt
->subtype
)+4, ((const char *)&MEDIATYPE_Audio
)+4, sizeof(GUID
)-4)) &&
267 (IsEqualIID(&pmt
->formattype
, &FORMAT_WaveFormatEx
)))
270 WAVEFORMATEX
*wfx
= (WAVEFORMATEX
*)pmt
->pbFormat
;
271 AM_MEDIA_TYPE
* outpmt
= &This
->tf
.pmt
;
273 if (!wfx
|| wfx
->wFormatTag
== WAVE_FORMAT_PCM
|| wfx
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
)
274 return VFW_E_TYPE_NOT_ACCEPTED
;
275 FreeMediaType(outpmt
);
277 This
->pWfIn
= (LPWAVEFORMATEX
)pmt
->pbFormat
;
280 /* TRACE("ALIGN = %d\n", pACMWrapper->pWfIn->nBlockAlign); */
281 /* pACMWrapper->pWfIn->nBlockAlign = 1; */
283 /* Set output audio data to PCM */
284 CopyMediaType(outpmt
, pmt
);
285 outpmt
->subtype
.Data1
= WAVE_FORMAT_PCM
;
286 This
->pWfOut
= (WAVEFORMATEX
*)outpmt
->pbFormat
;
287 This
->pWfOut
->wFormatTag
= WAVE_FORMAT_PCM
;
288 This
->pWfOut
->wBitsPerSample
= 16;
289 This
->pWfOut
->nBlockAlign
= This
->pWfOut
->wBitsPerSample
* This
->pWfOut
->nChannels
/ 8;
290 This
->pWfOut
->cbSize
= 0;
291 This
->pWfOut
->nAvgBytesPerSec
= This
->pWfOut
->nChannels
* This
->pWfOut
->nSamplesPerSec
292 * (This
->pWfOut
->wBitsPerSample
/8);
294 if (!(res
= acmStreamOpen(&drv
, NULL
, This
->pWfIn
, This
->pWfOut
, NULL
, 0, 0, 0)))
298 TRACE("Connection accepted\n");
302 FIXME("acmStreamOpen returned %d\n", res
);
303 FreeMediaType(outpmt
);
304 TRACE("Unable to find a suitable ACM decompressor\n");
307 TRACE("Connection refused\n");
308 return VFW_E_TYPE_NOT_ACCEPTED
;
311 static HRESULT WINAPI
ACMWrapper_CompleteConnect(TransformFilter
*tf
, PIN_DIRECTION dir
, IPin
*pin
)
313 ACMWrapperImpl
* This
= impl_from_TransformFilter(tf
);
317 TRACE("(%p)\n", This
);
319 if (dir
!= PINDIR_INPUT
)
322 if (!(res
= acmStreamOpen(&drv
, NULL
, This
->pWfIn
, This
->pWfOut
, NULL
, 0, 0, 0)))
326 TRACE("Connection accepted\n");
330 FIXME("acmStreamOpen returned %d\n", res
);
331 TRACE("Unable to find a suitable ACM decompressor\n");
332 return VFW_E_TYPE_NOT_ACCEPTED
;
335 static HRESULT WINAPI
ACMWrapper_BreakConnect(TransformFilter
*tf
, PIN_DIRECTION dir
)
337 ACMWrapperImpl
*This
= impl_from_TransformFilter(tf
);
339 TRACE("(%p)->(%i)\n", This
,dir
);
341 if (dir
== PINDIR_INPUT
)
344 acmStreamClose(This
->has
, 0);
347 This
->lasttime_real
= This
->lasttime_sent
= -1;
353 static HRESULT WINAPI
ACMWrapper_DecideBufferSize(TransformFilter
*tf
, IMemAllocator
*pAlloc
, ALLOCATOR_PROPERTIES
*ppropInputRequest
)
355 ACMWrapperImpl
*pACM
= impl_from_TransformFilter(tf
);
356 ALLOCATOR_PROPERTIES actual
;
358 if (!ppropInputRequest
->cbAlign
)
359 ppropInputRequest
->cbAlign
= 1;
361 if (ppropInputRequest
->cbBuffer
< pACM
->pWfOut
->nAvgBytesPerSec
/ 2)
362 ppropInputRequest
->cbBuffer
= pACM
->pWfOut
->nAvgBytesPerSec
/ 2;
364 if (!ppropInputRequest
->cBuffers
)
365 ppropInputRequest
->cBuffers
= 1;
367 return IMemAllocator_SetProperties(pAlloc
, ppropInputRequest
, &actual
);
370 static const TransformFilterFuncTable ACMWrapper_FuncsTable
= {
371 ACMWrapper_DecideBufferSize
,
376 ACMWrapper_SetMediaType
,
377 ACMWrapper_CompleteConnect
,
378 ACMWrapper_BreakConnect
,
385 HRESULT
ACMWrapper_create(IUnknown
* pUnkOuter
, LPVOID
* ppv
)
388 ACMWrapperImpl
* This
;
390 TRACE("(%p, %p)\n", pUnkOuter
, ppv
);
395 return CLASS_E_NOAGGREGATION
;
397 hr
= TransformFilter_Construct(&ACMWrapper_Vtbl
, sizeof(ACMWrapperImpl
), &CLSID_ACMWrapper
, &ACMWrapper_FuncsTable
, (IBaseFilter
**)&This
);
403 This
->lasttime_real
= This
->lasttime_sent
= -1;
408 static const IBaseFilterVtbl ACMWrapper_Vtbl
=
410 TransformFilterImpl_QueryInterface
,
411 BaseFilterImpl_AddRef
,
412 TransformFilterImpl_Release
,
413 BaseFilterImpl_GetClassID
,
414 TransformFilterImpl_Stop
,
415 TransformFilterImpl_Pause
,
416 TransformFilterImpl_Run
,
417 BaseFilterImpl_GetState
,
418 BaseFilterImpl_SetSyncSource
,
419 BaseFilterImpl_GetSyncSource
,
420 BaseFilterImpl_EnumPins
,
421 TransformFilterImpl_FindPin
,
422 BaseFilterImpl_QueryFilterInfo
,
423 BaseFilterImpl_JoinFilterGraph
,
424 BaseFilterImpl_QueryVendorInfo