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 #include "transform.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(quartz
);
44 typedef struct ACMWrapperImpl
46 TransformFilterImpl tf
;
49 LPWAVEFORMATEX pWfOut
;
51 LONGLONG lasttime_real
;
52 LONGLONG lasttime_sent
;
55 static HRESULT WINAPI
ACMWrapper_ProcessSampleData(IPin
*iface
, IMediaSample
*pSample
)
57 BaseInputPin
*pin
= (BaseInputPin
*) iface
;
58 ACMWrapperImpl
* This
= (ACMWrapperImpl
*)pin
->pin
.pinInfo
.pFilter
;
60 IMediaSample
* pOutSample
= NULL
;
61 DWORD cbDstStream
, cbSrcStream
;
63 LPBYTE pbSrcStream
= NULL
;
65 BOOL unprepare_header
= FALSE
, preroll
;
68 LONGLONG tStart
= -1, tStop
= -1, tMed
;
70 EnterCriticalSection(&This
->tf
.filter
.csFilter
);
71 if (This
->tf
.filter
.state
== State_Stopped
)
73 LeaveCriticalSection(&This
->tf
.filter
.csFilter
);
74 return VFW_E_WRONG_STATE
;
77 if (pin
->end_of_stream
|| pin
->flushing
)
79 LeaveCriticalSection(&This
->tf
.filter
.csFilter
);
83 hr
= IMediaSample_GetPointer(pSample
, &pbSrcStream
);
86 ERR("Cannot get pointer to sample data (%x)\n", hr
);
87 LeaveCriticalSection(&This
->tf
.filter
.csFilter
);
91 preroll
= (IMediaSample_IsPreroll(pSample
) == S_OK
);
93 IMediaSample_GetTime(pSample
, &tStart
, &tStop
);
94 cbSrcStream
= IMediaSample_GetActualDataLength(pSample
);
96 /* Prevent discontinuities when codecs 'absorb' data but not give anything back in return */
97 if (IMediaSample_IsDiscontinuity(pSample
) == S_OK
)
99 This
->lasttime_real
= tStart
;
100 This
->lasttime_sent
= tStart
;
102 else if (This
->lasttime_real
== tStart
)
103 tStart
= This
->lasttime_sent
;
105 WARN("Discontinuity\n");
109 TRACE("Sample data ptr = %p, size = %d\n", pbSrcStream
, cbSrcStream
);
111 hr
= IPin_ConnectionMediaType(This
->tf
.ppPins
[0], &amt
);
114 ERR("Unable to retrieve media type\n");
115 LeaveCriticalSection(&This
->tf
.filter
.csFilter
);
119 ash
.pbSrc
= pbSrcStream
;
120 ash
.cbSrcLength
= cbSrcStream
;
122 while(hr
== S_OK
&& ash
.cbSrcLength
)
124 hr
= BaseOutputPinImpl_GetDeliveryBuffer((BaseOutputPin
*)This
->tf
.ppPins
[1], &pOutSample
, NULL
, NULL
, 0);
127 ERR("Unable to get delivery buffer (%x)\n", hr
);
128 LeaveCriticalSection(&This
->tf
.filter
.csFilter
);
131 IMediaSample_SetPreroll(pOutSample
, preroll
);
133 hr
= IMediaSample_SetActualDataLength(pOutSample
, 0);
136 hr
= IMediaSample_GetPointer(pOutSample
, &pbDstStream
);
138 ERR("Unable to get pointer to buffer (%x)\n", hr
);
141 cbDstStream
= IMediaSample_GetSize(pOutSample
);
143 ash
.cbStruct
= sizeof(ash
);
146 ash
.pbDst
= pbDstStream
;
147 ash
.cbDstLength
= cbDstStream
;
149 if ((res
= acmStreamPrepareHeader(This
->has
, &ash
, 0))) {
150 ERR("Cannot prepare header %d\n", res
);
153 unprepare_header
= TRUE
;
155 if (IMediaSample_IsDiscontinuity(pSample
) == S_OK
)
157 res
= acmStreamConvert(This
->has
, &ash
, ACM_STREAMCONVERTF_START
);
158 IMediaSample_SetDiscontinuity(pOutSample
, TRUE
);
159 /* One sample could be converted to multiple packets */
160 IMediaSample_SetDiscontinuity(pSample
, FALSE
);
164 res
= acmStreamConvert(This
->has
, &ash
, 0);
165 IMediaSample_SetDiscontinuity(pOutSample
, FALSE
);
170 if(res
!= MMSYSERR_MOREDATA
)
171 ERR("Cannot convert data header %d\n", res
);
175 TRACE("used in %u/%u, used out %u/%u\n", ash
.cbSrcLengthUsed
, ash
.cbSrcLength
, ash
.cbDstLengthUsed
, ash
.cbDstLength
);
177 hr
= IMediaSample_SetActualDataLength(pOutSample
, ash
.cbDstLengthUsed
);
180 /* Bug in acm codecs? It apparantly uses the input, but doesn't necessarily output immediately kl*/
181 if (!ash
.cbSrcLengthUsed
)
183 WARN("Sample was skipped? Outputted: %u\n", ash
.cbDstLengthUsed
);
188 TRACE("Sample start time: %u.%03u\n", (DWORD
)(tStart
/10000000), (DWORD
)((tStart
/10000)%1000));
189 if (ash
.cbSrcLengthUsed
== cbSrcStream
)
191 IMediaSample_SetTime(pOutSample
, &tStart
, &tStop
);
192 tStart
= tMed
= tStop
;
194 else if (tStop
!= tStart
)
196 tMed
= tStop
- tStart
;
197 tMed
= tStart
+ tMed
* ash
.cbSrcLengthUsed
/ cbSrcStream
;
198 IMediaSample_SetTime(pOutSample
, &tStart
, &tMed
);
203 ERR("No valid timestamp found\n");
204 IMediaSample_SetTime(pOutSample
, NULL
, NULL
);
206 TRACE("Sample stop time: %u.%03u\n", (DWORD
)(tStart
/10000000), (DWORD
)((tStart
/10000)%1000));
208 LeaveCriticalSection(&This
->tf
.filter
.csFilter
);
209 hr
= BaseOutputPinImpl_Deliver((BaseOutputPin
*)This
->tf
.ppPins
[1], pOutSample
);
210 EnterCriticalSection(&This
->tf
.filter
.csFilter
);
212 if (hr
!= S_OK
&& hr
!= VFW_E_NOT_CONNECTED
) {
214 ERR("Error sending sample (%x)\n", hr
);
219 if (unprepare_header
&& (res
= acmStreamUnprepareHeader(This
->has
, &ash
, 0)))
220 ERR("Cannot unprepare header %d\n", res
);
221 unprepare_header
= FALSE
;
222 ash
.pbSrc
+= ash
.cbSrcLengthUsed
;
223 ash
.cbSrcLength
-= ash
.cbSrcLengthUsed
;
226 IMediaSample_Release(pOutSample
);
231 This
->lasttime_real
= tStop
;
232 This
->lasttime_sent
= tMed
;
234 LeaveCriticalSection(&This
->tf
.filter
.csFilter
);
238 static HRESULT
ACMWrapper_ConnectInput(BaseInputPin
*pin
, const AM_MEDIA_TYPE
* pmt
)
240 ACMWrapperImpl
* This
= (ACMWrapperImpl
*)pin
->pin
.pinInfo
.pFilter
;
243 TRACE("(%p)->(%p)\n", This
, pmt
);
245 /* Check root (GUID w/o FOURCC) */
246 if ((IsEqualIID(&pmt
->majortype
, &MEDIATYPE_Audio
)) &&
247 (!memcmp(((const char *)&pmt
->subtype
)+4, ((const char *)&MEDIATYPE_Audio
)+4, sizeof(GUID
)-4)) &&
248 (IsEqualIID(&pmt
->formattype
, &FORMAT_WaveFormatEx
)))
251 AM_MEDIA_TYPE
* outpmt
= &This
->tf
.pmt
;
252 FreeMediaType(outpmt
);
254 This
->pWfIn
= (LPWAVEFORMATEX
)pmt
->pbFormat
;
257 /* TRACE("ALIGN = %d\n", pACMWrapper->pWfIn->nBlockAlign); */
258 /* pACMWrapper->pWfIn->nBlockAlign = 1; */
260 /* Set output audio data to PCM */
261 CopyMediaType(outpmt
, pmt
);
262 outpmt
->subtype
.Data1
= WAVE_FORMAT_PCM
;
263 This
->pWfOut
= (WAVEFORMATEX
*)outpmt
->pbFormat
;
264 This
->pWfOut
->wFormatTag
= WAVE_FORMAT_PCM
;
265 This
->pWfOut
->wBitsPerSample
= 16;
266 This
->pWfOut
->nBlockAlign
= This
->pWfOut
->wBitsPerSample
* This
->pWfOut
->nChannels
/ 8;
267 This
->pWfOut
->cbSize
= 0;
268 This
->pWfOut
->nAvgBytesPerSec
= This
->pWfOut
->nChannels
* This
->pWfOut
->nSamplesPerSec
269 * (This
->pWfOut
->wBitsPerSample
/8);
271 if (!(res
= acmStreamOpen(&drv
, NULL
, This
->pWfIn
, This
->pWfOut
, NULL
, 0, 0, 0)))
275 /* Update buffer size of media samples in output */
276 ((BaseOutputPin
*)This
->tf
.ppPins
[1])->allocProps
.cbBuffer
= This
->pWfOut
->nAvgBytesPerSec
/ 2;
277 TRACE("Connection accepted\n");
281 FIXME("acmStreamOpen returned %d\n", res
);
282 FreeMediaType(outpmt
);
283 TRACE("Unable to find a suitable ACM decompressor\n");
286 TRACE("Connection refused\n");
287 return VFW_E_TYPE_NOT_ACCEPTED
;
290 static HRESULT
ACMWrapper_Cleanup(BaseInputPin
*pin
)
292 ACMWrapperImpl
*This
= (ACMWrapperImpl
*)pin
->pin
.pinInfo
.pFilter
;
294 TRACE("(%p)->()\n", This
);
297 acmStreamClose(This
->has
, 0);
300 This
->lasttime_real
= This
->lasttime_sent
= -1;
305 static const TransformFuncsTable ACMWrapper_FuncsTable
= {
307 ACMWrapper_ProcessSampleData
,
310 ACMWrapper_ConnectInput
,
314 HRESULT
ACMWrapper_create(IUnknown
* pUnkOuter
, LPVOID
* ppv
)
317 ACMWrapperImpl
* This
;
319 TRACE("(%p, %p)\n", pUnkOuter
, ppv
);
324 return CLASS_E_NOAGGREGATION
;
326 /* Note: This memory is managed by the transform filter once created */
327 This
= CoTaskMemAlloc(sizeof(ACMWrapperImpl
));
328 ZeroMemory(This
, sizeof(ACMWrapperImpl
));
330 hr
= TransformFilter_Create(&(This
->tf
), &CLSID_ACMWrapper
, &ACMWrapper_FuncsTable
);
336 This
->lasttime_real
= This
->lasttime_sent
= -1;