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;
68 InputPin *pin = (InputPin *)pTransformFilter->ppPins[0];
70 EnterCriticalSection(&pTransformFilter->csFilter);
71 if (pTransformFilter->state == State_Stopped)
73 LeaveCriticalSection(&pTransformFilter->csFilter);
74 return VFW_E_WRONG_STATE;
77 if (pin->end_of_stream || pin->flushing)
79 LeaveCriticalSection(&pTransformFilter->csFilter);
83 hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
86 ERR("Cannot get pointer to sample data (%x)\n", hr);
87 LeaveCriticalSection(&pTransformFilter->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 = %ld\n", pbSrcStream, (long)cbSrcStream);
111 hr = IPin_ConnectionMediaType(This->tf.ppPins[0], &amt);
114 ERR("Unable to retrieve media type\n");
115 LeaveCriticalSection(&pTransformFilter->csFilter);
119 ash.pbSrc = pbSrcStream;
120 ash.cbSrcLength = cbSrcStream;
122 while(hr == S_OK && ash.cbSrcLength)
124 hr = OutputPin_GetDeliveryBuffer((OutputPin*)This->tf.ppPins[1], &pOutSample, NULL, NULL, 0);
127 ERR("Unable to get delivery buffer (%x)\n", hr);
128 LeaveCriticalSection(&pTransformFilter->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.csFilter);
209 hr = OutputPin_SendSample((OutputPin*)This->tf.ppPins[1], pOutSample);
210 EnterCriticalSection(&This->tf.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(&pTransformFilter->csFilter);
238 static HRESULT ACMWrapper_ConnectInput(TransformFilterImpl* pTransformFilter, const AM_MEDIA_TYPE * pmt)
240 ACMWrapperImpl* This = (ACMWrapperImpl*)pTransformFilter;
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 = &((OutputPin*)This->tf.ppPins[1])->pin.mtCurrent;
252 This->pWfIn = (LPWAVEFORMATEX)pmt->pbFormat;
255 /* TRACE("ALIGN = %d\n", pACMWrapper->pWfIn->nBlockAlign); */
256 /* pACMWrapper->pWfIn->nBlockAlign = 1; */
258 /* Set output audio data to PCM */
259 CopyMediaType(outpmt, pmt);
260 outpmt->subtype.Data1 = WAVE_FORMAT_PCM;
261 This->pWfOut = (WAVEFORMATEX*)outpmt->pbFormat;
262 This->pWfOut->wFormatTag = WAVE_FORMAT_PCM;
263 This->pWfOut->wBitsPerSample = 16;
264 This->pWfOut->nBlockAlign = 4;
265 This->pWfOut->cbSize = 0;
266 This->pWfOut->nAvgBytesPerSec = This->pWfOut->nChannels * This->pWfOut->nSamplesPerSec
267 * (This->pWfOut->wBitsPerSample/8);
269 if (!(res = acmStreamOpen(&drv, NULL, This->pWfIn, This->pWfOut, NULL, 0, 0, 0)))
273 /* Update buffer size of media samples in output */
274 ((OutputPin*)This->tf.ppPins[1])->allocProps.cbBuffer = This->pWfOut->nAvgBytesPerSec / 2;
275 TRACE("Connection accepted\n");
279 FIXME("acmStreamOpen returned %d\n", res);
280 FreeMediaType(outpmt);
281 TRACE("Unable to find a suitable ACM decompressor\n");
284 TRACE("Connection refused\n");
285 return VFW_E_TYPE_NOT_ACCEPTED;
288 static HRESULT ACMWrapper_Cleanup(TransformFilterImpl* pTransformFilter)
290 ACMWrapperImpl* This = (ACMWrapperImpl*)pTransformFilter;
292 TRACE("(%p)->()\n", This);
295 acmStreamClose(This->has, 0);
298 This->lasttime_real = This->lasttime_sent = -1;
303 static const TransformFuncsTable ACMWrapper_FuncsTable = {
305 ACMWrapper_ProcessSampleData,
308 ACMWrapper_ConnectInput,
312 HRESULT ACMWrapper_create(IUnknown * pUnkOuter, LPVOID * ppv)
315 ACMWrapperImpl* This;
317 TRACE("(%p, %p)\n", pUnkOuter, ppv);
322 return CLASS_E_NOAGGREGATION;
324 /* Note: This memory is managed by the transform filter once created */
325 This = CoTaskMemAlloc(sizeof(ACMWrapperImpl));
326 ZeroMemory(This, sizeof(ACMWrapperImpl));
328 hr = TransformFilter_Create(&(This->tf), &CLSID_ACMWrapper, &ACMWrapper_FuncsTable, NULL, NULL, NULL);
334 This->lasttime_real = This->lasttime_sent = -1;