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 HRESULT WINAPI
ACMWrapper_Receive(TransformFilter
*tf
, IMediaSample
*pSample
)
59 ACMWrapperImpl
* This
= (ACMWrapperImpl
*)tf
;
61 IMediaSample
* pOutSample
= NULL
;
62 DWORD cbDstStream
, cbSrcStream
;
64 LPBYTE pbSrcStream
= NULL
;
66 BOOL unprepare_header
= FALSE
, preroll
;
69 LONGLONG tStart
= -1, tStop
= -1, tMed
;
70 LONGLONG mtStart
= -1, mtStop
= -1, mtMed
;
72 EnterCriticalSection(&This
->tf
.filter
.csFilter
);
73 hr
= IMediaSample_GetPointer(pSample
, &pbSrcStream
);
76 ERR("Cannot get pointer to sample data (%x)\n", hr
);
77 LeaveCriticalSection(&This
->tf
.filter
.csFilter
);
81 preroll
= (IMediaSample_IsPreroll(pSample
) == S_OK
);
83 IMediaSample_GetTime(pSample
, &tStart
, &tStop
);
84 if (IMediaSample_GetMediaTime(pSample
, &mtStart
, &mtStop
) != S_OK
)
85 mtStart
= mtStop
= -1;
86 cbSrcStream
= IMediaSample_GetActualDataLength(pSample
);
88 /* Prevent discontinuities when codecs 'absorb' data but not give anything back in return */
89 if (IMediaSample_IsDiscontinuity(pSample
) == S_OK
)
91 This
->lasttime_real
= tStart
;
92 This
->lasttime_sent
= tStart
;
94 else if (This
->lasttime_real
== tStart
)
95 tStart
= This
->lasttime_sent
;
97 WARN("Discontinuity\n");
102 TRACE("Sample data ptr = %p, size = %d\n", pbSrcStream
, cbSrcStream
);
104 hr
= IPin_ConnectionMediaType(This
->tf
.ppPins
[0], &amt
);
107 ERR("Unable to retrieve media type\n");
108 LeaveCriticalSection(&This
->tf
.filter
.csFilter
);
112 ash
.pbSrc
= pbSrcStream
;
113 ash
.cbSrcLength
= cbSrcStream
;
115 while(hr
== S_OK
&& ash
.cbSrcLength
)
117 hr
= BaseOutputPinImpl_GetDeliveryBuffer((BaseOutputPin
*)This
->tf
.ppPins
[1], &pOutSample
, NULL
, NULL
, 0);
120 ERR("Unable to get delivery buffer (%x)\n", hr
);
121 LeaveCriticalSection(&This
->tf
.filter
.csFilter
);
124 IMediaSample_SetPreroll(pOutSample
, preroll
);
126 hr
= IMediaSample_SetActualDataLength(pOutSample
, 0);
129 hr
= IMediaSample_GetPointer(pOutSample
, &pbDstStream
);
131 ERR("Unable to get pointer to buffer (%x)\n", hr
);
134 cbDstStream
= IMediaSample_GetSize(pOutSample
);
136 ash
.cbStruct
= sizeof(ash
);
139 ash
.pbDst
= pbDstStream
;
140 ash
.cbDstLength
= cbDstStream
;
142 if ((res
= acmStreamPrepareHeader(This
->has
, &ash
, 0))) {
143 ERR("Cannot prepare header %d\n", res
);
146 unprepare_header
= TRUE
;
148 if (IMediaSample_IsDiscontinuity(pSample
) == S_OK
)
150 res
= acmStreamConvert(This
->has
, &ash
, ACM_STREAMCONVERTF_START
);
151 IMediaSample_SetDiscontinuity(pOutSample
, TRUE
);
152 /* One sample could be converted to multiple packets */
153 IMediaSample_SetDiscontinuity(pSample
, FALSE
);
157 res
= acmStreamConvert(This
->has
, &ash
, 0);
158 IMediaSample_SetDiscontinuity(pOutSample
, FALSE
);
163 if(res
!= MMSYSERR_MOREDATA
)
164 ERR("Cannot convert data header %d\n", res
);
168 TRACE("used in %u/%u, used out %u/%u\n", ash
.cbSrcLengthUsed
, ash
.cbSrcLength
, ash
.cbDstLengthUsed
, ash
.cbDstLength
);
170 hr
= IMediaSample_SetActualDataLength(pOutSample
, ash
.cbDstLengthUsed
);
173 /* Bug in acm codecs? It apparantly uses the input, but doesn't necessarily output immediately kl*/
174 if (!ash
.cbSrcLengthUsed
)
176 WARN("Sample was skipped? Outputted: %u\n", ash
.cbDstLengthUsed
);
181 TRACE("Sample start time: %u.%03u\n", (DWORD
)(tStart
/10000000), (DWORD
)((tStart
/10000)%1000));
182 if (ash
.cbSrcLengthUsed
== cbSrcStream
)
184 IMediaSample_SetTime(pOutSample
, &tStart
, &tStop
);
185 tStart
= tMed
= tStop
;
187 else if (tStop
!= tStart
)
189 tMed
= tStop
- tStart
;
190 tMed
= tStart
+ tMed
* ash
.cbSrcLengthUsed
/ cbSrcStream
;
191 IMediaSample_SetTime(pOutSample
, &tStart
, &tMed
);
196 ERR("No valid timestamp found\n");
197 IMediaSample_SetTime(pOutSample
, NULL
, NULL
);
201 IMediaSample_SetMediaTime(pOutSample
, NULL
, NULL
);
202 } else if (ash
.cbSrcLengthUsed
== cbSrcStream
) {
203 IMediaSample_SetMediaTime(pOutSample
, &mtStart
, &mtStop
);
204 mtStart
= mtMed
= mtStop
;
205 } else if (mtStop
>= mtStart
) {
206 mtMed
= mtStop
- mtStart
;
207 mtMed
= mtStart
+ mtMed
* ash
.cbSrcLengthUsed
/ cbSrcStream
;
208 IMediaSample_SetMediaTime(pOutSample
, &mtStart
, &mtMed
);
211 IMediaSample_SetMediaTime(pOutSample
, NULL
, NULL
);
214 TRACE("Sample stop time: %u.%03u\n", (DWORD
)(tStart
/10000000), (DWORD
)((tStart
/10000)%1000));
216 LeaveCriticalSection(&This
->tf
.filter
.csFilter
);
217 hr
= BaseOutputPinImpl_Deliver((BaseOutputPin
*)This
->tf
.ppPins
[1], pOutSample
);
218 EnterCriticalSection(&This
->tf
.filter
.csFilter
);
220 if (hr
!= S_OK
&& hr
!= VFW_E_NOT_CONNECTED
) {
222 ERR("Error sending sample (%x)\n", hr
);
227 if (unprepare_header
&& (res
= acmStreamUnprepareHeader(This
->has
, &ash
, 0)))
228 ERR("Cannot unprepare header %d\n", res
);
229 unprepare_header
= FALSE
;
230 ash
.pbSrc
+= ash
.cbSrcLengthUsed
;
231 ash
.cbSrcLength
-= ash
.cbSrcLengthUsed
;
234 IMediaSample_Release(pOutSample
);
239 This
->lasttime_real
= tStop
;
240 This
->lasttime_sent
= tMed
;
242 LeaveCriticalSection(&This
->tf
.filter
.csFilter
);
246 static HRESULT WINAPI
ACMWrapper_SetMediaType(TransformFilter
*tf
, PIN_DIRECTION dir
, const AM_MEDIA_TYPE
* pmt
)
248 ACMWrapperImpl
* This
= (ACMWrapperImpl
*)tf
;
251 TRACE("(%p)->(%i %p)\n", This
, dir
, pmt
);
253 if (dir
!= PINDIR_INPUT
)
256 /* Check root (GUID w/o FOURCC) */
257 if ((IsEqualIID(&pmt
->majortype
, &MEDIATYPE_Audio
)) &&
258 (!memcmp(((const char *)&pmt
->subtype
)+4, ((const char *)&MEDIATYPE_Audio
)+4, sizeof(GUID
)-4)) &&
259 (IsEqualIID(&pmt
->formattype
, &FORMAT_WaveFormatEx
)))
262 AM_MEDIA_TYPE
* outpmt
= &This
->tf
.pmt
;
263 FreeMediaType(outpmt
);
265 This
->pWfIn
= (LPWAVEFORMATEX
)pmt
->pbFormat
;
268 /* TRACE("ALIGN = %d\n", pACMWrapper->pWfIn->nBlockAlign); */
269 /* pACMWrapper->pWfIn->nBlockAlign = 1; */
271 /* Set output audio data to PCM */
272 CopyMediaType(outpmt
, pmt
);
273 outpmt
->subtype
.Data1
= WAVE_FORMAT_PCM
;
274 This
->pWfOut
= (WAVEFORMATEX
*)outpmt
->pbFormat
;
275 This
->pWfOut
->wFormatTag
= WAVE_FORMAT_PCM
;
276 This
->pWfOut
->wBitsPerSample
= 16;
277 This
->pWfOut
->nBlockAlign
= This
->pWfOut
->wBitsPerSample
* This
->pWfOut
->nChannels
/ 8;
278 This
->pWfOut
->cbSize
= 0;
279 This
->pWfOut
->nAvgBytesPerSec
= This
->pWfOut
->nChannels
* This
->pWfOut
->nSamplesPerSec
280 * (This
->pWfOut
->wBitsPerSample
/8);
282 if (!(res
= acmStreamOpen(&drv
, NULL
, This
->pWfIn
, This
->pWfOut
, NULL
, 0, 0, 0)))
286 TRACE("Connection accepted\n");
290 FIXME("acmStreamOpen returned %d\n", res
);
291 FreeMediaType(outpmt
);
292 TRACE("Unable to find a suitable ACM decompressor\n");
295 TRACE("Connection refused\n");
296 return VFW_E_TYPE_NOT_ACCEPTED
;
299 static HRESULT WINAPI
ACMWrapper_CompleteConnect(TransformFilter
*tf
, PIN_DIRECTION dir
, IPin
*pin
)
301 ACMWrapperImpl
* This
= (ACMWrapperImpl
*)tf
;
305 TRACE("(%p)\n", This
);
307 if (dir
!= PINDIR_INPUT
)
310 if (!(res
= acmStreamOpen(&drv
, NULL
, This
->pWfIn
, This
->pWfOut
, NULL
, 0, 0, 0)))
314 TRACE("Connection accepted\n");
318 FIXME("acmStreamOpen returned %d\n", res
);
319 TRACE("Unable to find a suitable ACM decompressor\n");
320 return VFW_E_TYPE_NOT_ACCEPTED
;
323 static HRESULT WINAPI
ACMWrapper_BreakConnect(TransformFilter
*tf
, PIN_DIRECTION dir
)
325 ACMWrapperImpl
*This
= (ACMWrapperImpl
*)tf
;
327 TRACE("(%p)->(%i)\n", This
,dir
);
329 if (dir
== PINDIR_INPUT
)
332 acmStreamClose(This
->has
, 0);
335 This
->lasttime_real
= This
->lasttime_sent
= -1;
341 static HRESULT WINAPI
ACMWrapper_DecideBufferSize(TransformFilter
*tf
, IMemAllocator
*pAlloc
, ALLOCATOR_PROPERTIES
*ppropInputRequest
)
343 ACMWrapperImpl
*pACM
= (ACMWrapperImpl
*)tf
;
344 ALLOCATOR_PROPERTIES actual
;
346 if (!ppropInputRequest
->cbAlign
)
347 ppropInputRequest
->cbAlign
= 1;
349 if (ppropInputRequest
->cbBuffer
< pACM
->pWfOut
->nAvgBytesPerSec
/ 2)
350 ppropInputRequest
->cbBuffer
= pACM
->pWfOut
->nAvgBytesPerSec
/ 2;
352 if (!ppropInputRequest
->cBuffers
)
353 ppropInputRequest
->cBuffers
= 1;
355 return IMemAllocator_SetProperties(pAlloc
, ppropInputRequest
, &actual
);
358 static const TransformFilterFuncTable ACMWrapper_FuncsTable
= {
359 ACMWrapper_DecideBufferSize
,
364 ACMWrapper_SetMediaType
,
365 ACMWrapper_CompleteConnect
,
366 ACMWrapper_BreakConnect
,
373 HRESULT
ACMWrapper_create(IUnknown
* pUnkOuter
, LPVOID
* ppv
)
376 ACMWrapperImpl
* This
;
378 TRACE("(%p, %p)\n", pUnkOuter
, ppv
);
383 return CLASS_E_NOAGGREGATION
;
385 hr
= TransformFilter_Construct(&ACMWrapper_Vtbl
, sizeof(ACMWrapperImpl
), &CLSID_ACMWrapper
, &ACMWrapper_FuncsTable
, (IBaseFilter
**)&This
);
391 ISeekingPassThru
*passthru
;
392 hr
= CoCreateInstance(&CLSID_SeekingPassThru
, (IUnknown
*)This
, CLSCTX_INPROC_SERVER
, &IID_IUnknown
, (void**)&This
->seekthru_unk
);
393 IUnknown_QueryInterface(This
->seekthru_unk
, &IID_ISeekingPassThru
, (void**)&passthru
);
394 ISeekingPassThru_Init(passthru
, FALSE
, (IPin
*)This
->tf
.ppPins
[0]);
395 ISeekingPassThru_Release(passthru
);
399 This
->lasttime_real
= This
->lasttime_sent
= -1;
404 HRESULT WINAPI
ACMWrapper_QueryInterface(IBaseFilter
* iface
, REFIID riid
, LPVOID
* ppv
)
407 ACMWrapperImpl
*This
= (ACMWrapperImpl
*)iface
;
408 TRACE("(%p/%p)->(%s, %p)\n", This
, iface
, qzdebugstr_guid(riid
), ppv
);
410 if (IsEqualIID(riid
, &IID_IMediaSeeking
))
411 return IUnknown_QueryInterface(This
->seekthru_unk
, riid
, ppv
);
413 hr
= TransformFilterImpl_QueryInterface(iface
, riid
, ppv
);
419 static const IBaseFilterVtbl ACMWrapper_Vtbl
=
421 ACMWrapper_QueryInterface
,
422 BaseFilterImpl_AddRef
,
423 TransformFilterImpl_Release
,
424 BaseFilterImpl_GetClassID
,
425 TransformFilterImpl_Stop
,
426 TransformFilterImpl_Pause
,
427 TransformFilterImpl_Run
,
428 BaseFilterImpl_GetState
,
429 BaseFilterImpl_SetSyncSource
,
430 BaseFilterImpl_GetSyncSource
,
431 BaseFilterImpl_EnumPins
,
432 TransformFilterImpl_FindPin
,
433 BaseFilterImpl_QueryFilterInfo
,
434 BaseFilterImpl_JoinFilterGraph
,
435 BaseFilterImpl_QueryVendorInfo