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
ACMWrapper_ProcessSampleData(TransformFilterImpl
* pTransformFilter
, IMediaSample
*pSample
)
57 ACMWrapperImpl
* This
= (ACMWrapperImpl
*)pTransformFilter
;
59 IMediaSample
* pOutSample
= NULL
;
60 DWORD cbDstStream
, cbSrcStream
;
62 LPBYTE pbSrcStream
= NULL
;
64 BOOL unprepare_header
= FALSE
, preroll
;
67 LONGLONG tStart
= -1, tStop
= -1, tMed
;
69 hr
= IMediaSample_GetPointer(pSample
, &pbSrcStream
);
72 ERR("Cannot get pointer to sample data (%x)\n", hr
);
76 preroll
= (IMediaSample_IsPreroll(pSample
) == S_OK
);
78 IMediaSample_GetTime(pSample
, &tStart
, &tStop
);
79 cbSrcStream
= IMediaSample_GetActualDataLength(pSample
);
81 /* Prevent discontinuities when codecs 'absorb' data but not give anything back in return */
82 if (IMediaSample_IsDiscontinuity(pSample
) == S_OK
)
84 This
->lasttime_real
= tStart
;
85 This
->lasttime_sent
= tStart
;
87 else if (This
->lasttime_real
== tStart
)
88 tStart
= This
->lasttime_sent
;
90 WARN("Discontinuity\n");
94 TRACE("Sample data ptr = %p, size = %ld\n", pbSrcStream
, (long)cbSrcStream
);
96 hr
= IPin_ConnectionMediaType(This
->tf
.ppPins
[0], &amt
);
98 ERR("Unable to retrieve media type\n");
102 ash
.pbSrc
= pbSrcStream
;
103 ash
.cbSrcLength
= cbSrcStream
;
105 while(hr
== S_OK
&& ash
.cbSrcLength
)
107 hr
= OutputPin_GetDeliveryBuffer((OutputPin
*)This
->tf
.ppPins
[1], &pOutSample
, NULL
, NULL
, 0);
109 ERR("Unable to get delivery buffer (%x)\n", hr
);
112 IMediaSample_SetPreroll(pOutSample
, preroll
);
114 hr
= IMediaSample_SetActualDataLength(pOutSample
, 0);
117 hr
= IMediaSample_GetPointer(pOutSample
, &pbDstStream
);
119 ERR("Unable to get pointer to buffer (%x)\n", hr
);
122 cbDstStream
= IMediaSample_GetSize(pOutSample
);
124 ash
.cbStruct
= sizeof(ash
);
127 ash
.pbDst
= pbDstStream
;
128 ash
.cbDstLength
= cbDstStream
;
130 if ((res
= acmStreamPrepareHeader(This
->has
, &ash
, 0))) {
131 ERR("Cannot prepare header %d\n", res
);
134 unprepare_header
= TRUE
;
136 if (IMediaSample_IsDiscontinuity(pSample
) == S_OK
)
138 res
= acmStreamConvert(This
->has
, &ash
, ACM_STREAMCONVERTF_START
);
139 IMediaSample_SetDiscontinuity(pOutSample
, TRUE
);
140 /* One sample could be converted to multiple packets */
141 IMediaSample_SetDiscontinuity(pSample
, FALSE
);
145 res
= acmStreamConvert(This
->has
, &ash
, 0);
146 IMediaSample_SetDiscontinuity(pOutSample
, FALSE
);
151 if(res
!= MMSYSERR_MOREDATA
)
152 ERR("Cannot convert data header %d\n", res
);
156 TRACE("used in %u/%u, used out %u/%u\n", ash
.cbSrcLengthUsed
, ash
.cbSrcLength
, ash
.cbDstLengthUsed
, ash
.cbDstLength
);
158 hr
= IMediaSample_SetActualDataLength(pOutSample
, ash
.cbDstLengthUsed
);
161 /* Bug in acm codecs? It apparantly uses the input, but doesn't necessarily output immediately kl*/
162 if (!ash
.cbSrcLengthUsed
)
164 WARN("Sample was skipped? Outputted: %u\n", ash
.cbDstLengthUsed
);
169 TRACE("Sample start time: %u.%03u\n", (DWORD
)(tStart
/10000000), (DWORD
)((tStart
/10000)%1000));
170 if (ash
.cbSrcLengthUsed
== cbSrcStream
)
172 IMediaSample_SetTime(pOutSample
, &tStart
, &tStop
);
173 tStart
= tMed
= tStop
;
175 else if (tStop
!= tStart
)
177 tMed
= tStop
- tStart
;
178 tMed
= tStart
+ tMed
* ash
.cbSrcLengthUsed
/ cbSrcStream
;
179 IMediaSample_SetTime(pOutSample
, &tStart
, &tMed
);
184 ERR("No valid timestamp found\n");
185 IMediaSample_SetTime(pOutSample
, NULL
, NULL
);
187 TRACE("Sample stop time: %u.%03u\n", (DWORD
)(tStart
/10000000), (DWORD
)((tStart
/10000)%1000));
189 hr
= OutputPin_SendSample((OutputPin
*)This
->tf
.ppPins
[1], pOutSample
);
191 if (hr
!= S_OK
&& hr
!= VFW_E_NOT_CONNECTED
) {
193 ERR("Error sending sample (%x)\n", hr
);
198 if (unprepare_header
&& (res
= acmStreamUnprepareHeader(This
->has
, &ash
, 0)))
199 ERR("Cannot unprepare header %d\n", res
);
200 unprepare_header
= FALSE
;
201 ash
.pbSrc
+= ash
.cbSrcLengthUsed
;
202 ash
.cbSrcLength
-= ash
.cbSrcLengthUsed
;
205 IMediaSample_Release(pOutSample
);
210 This
->lasttime_real
= tStop
;
211 This
->lasttime_sent
= tMed
;
216 static HRESULT
ACMWrapper_ConnectInput(TransformFilterImpl
* pTransformFilter
, const AM_MEDIA_TYPE
* pmt
)
218 ACMWrapperImpl
* This
= (ACMWrapperImpl
*)pTransformFilter
;
221 TRACE("(%p)->(%p)\n", This
, pmt
);
223 /* Check root (GUID w/o FOURCC) */
224 if ((IsEqualIID(&pmt
->majortype
, &MEDIATYPE_Audio
)) &&
225 (!memcmp(((const char *)&pmt
->subtype
)+4, ((const char *)&MEDIATYPE_Audio
)+4, sizeof(GUID
)-4)) &&
226 (IsEqualIID(&pmt
->formattype
, &FORMAT_WaveFormatEx
)))
229 AM_MEDIA_TYPE
* outpmt
= &((OutputPin
*)This
->tf
.ppPins
[1])->pin
.mtCurrent
;
230 This
->pWfIn
= (LPWAVEFORMATEX
)pmt
->pbFormat
;
233 /* TRACE("ALIGN = %d\n", pACMWrapper->pWfIn->nBlockAlign); */
234 /* pACMWrapper->pWfIn->nBlockAlign = 1; */
236 /* Set output audio data to PCM */
237 CopyMediaType(outpmt
, pmt
);
238 outpmt
->subtype
.Data1
= WAVE_FORMAT_PCM
;
239 This
->pWfOut
= (WAVEFORMATEX
*)outpmt
->pbFormat
;
240 This
->pWfOut
->wFormatTag
= WAVE_FORMAT_PCM
;
241 This
->pWfOut
->wBitsPerSample
= 16;
242 This
->pWfOut
->nBlockAlign
= 4;
243 This
->pWfOut
->cbSize
= 0;
244 This
->pWfOut
->nAvgBytesPerSec
= This
->pWfOut
->nChannels
* This
->pWfOut
->nSamplesPerSec
245 * (This
->pWfOut
->wBitsPerSample
/8);
247 if (!(res
= acmStreamOpen(&drv
, NULL
, This
->pWfIn
, This
->pWfOut
, NULL
, 0, 0, 0)))
251 /* Update buffer size of media samples in output */
252 ((OutputPin
*)This
->tf
.ppPins
[1])->allocProps
.cbBuffer
= This
->pWfOut
->nAvgBytesPerSec
/ 2;
253 TRACE("Connection accepted\n");
257 FIXME("acmStreamOpen returned %d\n", res
);
258 FreeMediaType(outpmt
);
259 TRACE("Unable to find a suitable ACM decompressor\n");
262 TRACE("Connection refused\n");
263 return VFW_E_TYPE_NOT_ACCEPTED
;
266 static HRESULT
ACMWrapper_Cleanup(TransformFilterImpl
* pTransformFilter
)
268 ACMWrapperImpl
* This
= (ACMWrapperImpl
*)pTransformFilter
;
270 TRACE("(%p)->()\n", This
);
273 acmStreamClose(This
->has
, 0);
276 This
->lasttime_real
= This
->lasttime_sent
= -1;
281 static const TransformFuncsTable ACMWrapper_FuncsTable
= {
283 ACMWrapper_ProcessSampleData
,
286 ACMWrapper_ConnectInput
,
290 HRESULT
ACMWrapper_create(IUnknown
* pUnkOuter
, LPVOID
* ppv
)
293 ACMWrapperImpl
* This
;
295 TRACE("(%p, %p)\n", pUnkOuter
, ppv
);
300 return CLASS_E_NOAGGREGATION
;
302 /* Note: This memory is managed by the transform filter once created */
303 This
= CoTaskMemAlloc(sizeof(ACMWrapperImpl
));
304 ZeroMemory(This
, sizeof(ACMWrapperImpl
));
306 hr
= TransformFilter_Create(&(This
->tf
), &CLSID_ACMWrapper
, &ACMWrapper_FuncsTable
, NULL
, NULL
, NULL
);
312 This
->lasttime_real
= This
->lasttime_sent
= -1;