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
;
71 EnterCriticalSection(&This
->tf
.filter
.csFilter
);
72 hr
= IMediaSample_GetPointer(pSample
, &pbSrcStream
);
75 ERR("Cannot get pointer to sample data (%x)\n", hr
);
76 LeaveCriticalSection(&This
->tf
.filter
.csFilter
);
80 preroll
= (IMediaSample_IsPreroll(pSample
) == S_OK
);
82 IMediaSample_GetTime(pSample
, &tStart
, &tStop
);
83 cbSrcStream
= IMediaSample_GetActualDataLength(pSample
);
85 /* Prevent discontinuities when codecs 'absorb' data but not give anything back in return */
86 if (IMediaSample_IsDiscontinuity(pSample
) == S_OK
)
88 This
->lasttime_real
= tStart
;
89 This
->lasttime_sent
= tStart
;
91 else if (This
->lasttime_real
== tStart
)
92 tStart
= This
->lasttime_sent
;
94 WARN("Discontinuity\n");
98 TRACE("Sample data ptr = %p, size = %d\n", pbSrcStream
, cbSrcStream
);
100 hr
= IPin_ConnectionMediaType(This
->tf
.ppPins
[0], &amt
);
103 ERR("Unable to retrieve media type\n");
104 LeaveCriticalSection(&This
->tf
.filter
.csFilter
);
108 ash
.pbSrc
= pbSrcStream
;
109 ash
.cbSrcLength
= cbSrcStream
;
111 while(hr
== S_OK
&& ash
.cbSrcLength
)
113 hr
= BaseOutputPinImpl_GetDeliveryBuffer((BaseOutputPin
*)This
->tf
.ppPins
[1], &pOutSample
, NULL
, NULL
, 0);
116 ERR("Unable to get delivery buffer (%x)\n", hr
);
117 LeaveCriticalSection(&This
->tf
.filter
.csFilter
);
120 IMediaSample_SetPreroll(pOutSample
, preroll
);
122 hr
= IMediaSample_SetActualDataLength(pOutSample
, 0);
125 hr
= IMediaSample_GetPointer(pOutSample
, &pbDstStream
);
127 ERR("Unable to get pointer to buffer (%x)\n", hr
);
130 cbDstStream
= IMediaSample_GetSize(pOutSample
);
132 ash
.cbStruct
= sizeof(ash
);
135 ash
.pbDst
= pbDstStream
;
136 ash
.cbDstLength
= cbDstStream
;
138 if ((res
= acmStreamPrepareHeader(This
->has
, &ash
, 0))) {
139 ERR("Cannot prepare header %d\n", res
);
142 unprepare_header
= TRUE
;
144 if (IMediaSample_IsDiscontinuity(pSample
) == S_OK
)
146 res
= acmStreamConvert(This
->has
, &ash
, ACM_STREAMCONVERTF_START
);
147 IMediaSample_SetDiscontinuity(pOutSample
, TRUE
);
148 /* One sample could be converted to multiple packets */
149 IMediaSample_SetDiscontinuity(pSample
, FALSE
);
153 res
= acmStreamConvert(This
->has
, &ash
, 0);
154 IMediaSample_SetDiscontinuity(pOutSample
, FALSE
);
159 if(res
!= MMSYSERR_MOREDATA
)
160 ERR("Cannot convert data header %d\n", res
);
164 TRACE("used in %u/%u, used out %u/%u\n", ash
.cbSrcLengthUsed
, ash
.cbSrcLength
, ash
.cbDstLengthUsed
, ash
.cbDstLength
);
166 hr
= IMediaSample_SetActualDataLength(pOutSample
, ash
.cbDstLengthUsed
);
169 /* Bug in acm codecs? It apparantly uses the input, but doesn't necessarily output immediately kl*/
170 if (!ash
.cbSrcLengthUsed
)
172 WARN("Sample was skipped? Outputted: %u\n", ash
.cbDstLengthUsed
);
177 TRACE("Sample start time: %u.%03u\n", (DWORD
)(tStart
/10000000), (DWORD
)((tStart
/10000)%1000));
178 if (ash
.cbSrcLengthUsed
== cbSrcStream
)
180 IMediaSample_SetTime(pOutSample
, &tStart
, &tStop
);
181 tStart
= tMed
= tStop
;
183 else if (tStop
!= tStart
)
185 tMed
= tStop
- tStart
;
186 tMed
= tStart
+ tMed
* ash
.cbSrcLengthUsed
/ cbSrcStream
;
187 IMediaSample_SetTime(pOutSample
, &tStart
, &tMed
);
192 ERR("No valid timestamp found\n");
193 IMediaSample_SetTime(pOutSample
, NULL
, NULL
);
195 TRACE("Sample stop time: %u.%03u\n", (DWORD
)(tStart
/10000000), (DWORD
)((tStart
/10000)%1000));
197 LeaveCriticalSection(&This
->tf
.filter
.csFilter
);
198 hr
= BaseOutputPinImpl_Deliver((BaseOutputPin
*)This
->tf
.ppPins
[1], pOutSample
);
199 EnterCriticalSection(&This
->tf
.filter
.csFilter
);
201 if (hr
!= S_OK
&& hr
!= VFW_E_NOT_CONNECTED
) {
203 ERR("Error sending sample (%x)\n", hr
);
208 if (unprepare_header
&& (res
= acmStreamUnprepareHeader(This
->has
, &ash
, 0)))
209 ERR("Cannot unprepare header %d\n", res
);
210 unprepare_header
= FALSE
;
211 ash
.pbSrc
+= ash
.cbSrcLengthUsed
;
212 ash
.cbSrcLength
-= ash
.cbSrcLengthUsed
;
215 IMediaSample_Release(pOutSample
);
220 This
->lasttime_real
= tStop
;
221 This
->lasttime_sent
= tMed
;
223 LeaveCriticalSection(&This
->tf
.filter
.csFilter
);
227 static HRESULT WINAPI
ACMWrapper_SetMediaType(TransformFilter
*tf
, PIN_DIRECTION dir
, const AM_MEDIA_TYPE
* pmt
)
229 ACMWrapperImpl
* This
= (ACMWrapperImpl
*)tf
;
232 TRACE("(%p)->(%i %p)\n", This
, dir
, pmt
);
234 if (dir
!= PINDIR_INPUT
)
237 /* Check root (GUID w/o FOURCC) */
238 if ((IsEqualIID(&pmt
->majortype
, &MEDIATYPE_Audio
)) &&
239 (!memcmp(((const char *)&pmt
->subtype
)+4, ((const char *)&MEDIATYPE_Audio
)+4, sizeof(GUID
)-4)) &&
240 (IsEqualIID(&pmt
->formattype
, &FORMAT_WaveFormatEx
)))
243 AM_MEDIA_TYPE
* outpmt
= &This
->tf
.pmt
;
244 FreeMediaType(outpmt
);
246 This
->pWfIn
= (LPWAVEFORMATEX
)pmt
->pbFormat
;
249 /* TRACE("ALIGN = %d\n", pACMWrapper->pWfIn->nBlockAlign); */
250 /* pACMWrapper->pWfIn->nBlockAlign = 1; */
252 /* Set output audio data to PCM */
253 CopyMediaType(outpmt
, pmt
);
254 outpmt
->subtype
.Data1
= WAVE_FORMAT_PCM
;
255 This
->pWfOut
= (WAVEFORMATEX
*)outpmt
->pbFormat
;
256 This
->pWfOut
->wFormatTag
= WAVE_FORMAT_PCM
;
257 This
->pWfOut
->wBitsPerSample
= 16;
258 This
->pWfOut
->nBlockAlign
= This
->pWfOut
->wBitsPerSample
* This
->pWfOut
->nChannels
/ 8;
259 This
->pWfOut
->cbSize
= 0;
260 This
->pWfOut
->nAvgBytesPerSec
= This
->pWfOut
->nChannels
* This
->pWfOut
->nSamplesPerSec
261 * (This
->pWfOut
->wBitsPerSample
/8);
263 if (!(res
= acmStreamOpen(&drv
, NULL
, This
->pWfIn
, This
->pWfOut
, NULL
, 0, 0, 0)))
267 /* Update buffer size of media samples in output */
268 ((BaseOutputPin
*)This
->tf
.ppPins
[1])->allocProps
.cbBuffer
= This
->pWfOut
->nAvgBytesPerSec
/ 2;
269 TRACE("Connection accepted\n");
273 FIXME("acmStreamOpen returned %d\n", res
);
274 FreeMediaType(outpmt
);
275 TRACE("Unable to find a suitable ACM decompressor\n");
278 TRACE("Connection refused\n");
279 return VFW_E_TYPE_NOT_ACCEPTED
;
282 static HRESULT WINAPI
ACMWrapper_CompleteConnect(TransformFilter
*tf
, PIN_DIRECTION dir
, IPin
*pin
)
284 ACMWrapperImpl
* This
= (ACMWrapperImpl
*)tf
;
288 TRACE("(%p)\n", This
);
290 if (dir
!= PINDIR_INPUT
)
293 if (!(res
= acmStreamOpen(&drv
, NULL
, This
->pWfIn
, This
->pWfOut
, NULL
, 0, 0, 0)))
297 /* Update buffer size of media samples in output */
298 ((BaseOutputPin
*)This
->tf
.ppPins
[1])->allocProps
.cbBuffer
= This
->pWfOut
->nAvgBytesPerSec
/ 2;
299 TRACE("Connection accepted\n");
303 FIXME("acmStreamOpen returned %d\n", res
);
304 TRACE("Unable to find a suitable ACM decompressor\n");
305 return VFW_E_TYPE_NOT_ACCEPTED
;
308 static HRESULT WINAPI
ACMWrapper_BreakConnect(TransformFilter
*tf
, PIN_DIRECTION dir
)
310 ACMWrapperImpl
*This
= (ACMWrapperImpl
*)tf
;
312 TRACE("(%p)->(%i)\n", This
,dir
);
314 if (dir
== PINDIR_INPUT
)
317 acmStreamClose(This
->has
, 0);
320 This
->lasttime_real
= This
->lasttime_sent
= -1;
326 static const TransformFilterFuncTable ACMWrapper_FuncsTable
= {
331 ACMWrapper_SetMediaType
,
332 ACMWrapper_CompleteConnect
,
333 ACMWrapper_BreakConnect
,
340 HRESULT
ACMWrapper_create(IUnknown
* pUnkOuter
, LPVOID
* ppv
)
343 ACMWrapperImpl
* This
;
345 TRACE("(%p, %p)\n", pUnkOuter
, ppv
);
350 return CLASS_E_NOAGGREGATION
;
352 hr
= TransformFilter_Construct(&ACMWrapper_Vtbl
, sizeof(ACMWrapperImpl
), &CLSID_ACMWrapper
, &ACMWrapper_FuncsTable
, (IBaseFilter
**)&This
);
358 ISeekingPassThru
*passthru
;
359 hr
= CoCreateInstance(&CLSID_SeekingPassThru
, (IUnknown
*)This
, CLSCTX_INPROC_SERVER
, &IID_IUnknown
, (void**)&This
->seekthru_unk
);
360 IUnknown_QueryInterface(This
->seekthru_unk
, &IID_ISeekingPassThru
, (void**)&passthru
);
361 ISeekingPassThru_Init(passthru
, FALSE
, (IPin
*)This
->tf
.ppPins
[0]);
362 ISeekingPassThru_Release(passthru
);
366 This
->lasttime_real
= This
->lasttime_sent
= -1;
371 HRESULT WINAPI
ACMWrapper_QueryInterface(IBaseFilter
* iface
, REFIID riid
, LPVOID
* ppv
)
374 ACMWrapperImpl
*This
= (ACMWrapperImpl
*)iface
;
375 TRACE("(%p/%p)->(%s, %p)\n", This
, iface
, qzdebugstr_guid(riid
), ppv
);
377 if (IsEqualIID(riid
, &IID_IMediaSeeking
))
378 return IUnknown_QueryInterface(This
->seekthru_unk
, riid
, ppv
);
380 hr
= TransformFilterImpl_QueryInterface(iface
, riid
, ppv
);
386 static const IBaseFilterVtbl ACMWrapper_Vtbl
=
388 ACMWrapper_QueryInterface
,
389 BaseFilterImpl_AddRef
,
390 TransformFilterImpl_Release
,
391 BaseFilterImpl_GetClassID
,
392 TransformFilterImpl_Stop
,
393 TransformFilterImpl_Pause
,
394 TransformFilterImpl_Run
,
395 BaseFilterImpl_GetState
,
396 BaseFilterImpl_SetSyncSource
,
397 BaseFilterImpl_GetSyncSource
,
398 BaseFilterImpl_EnumPins
,
399 TransformFilterImpl_FindPin
,
400 BaseFilterImpl_QueryFilterInfo
,
401 BaseFilterImpl_JoinFilterGraph
,
402 BaseFilterImpl_QueryVendorInfo