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(InputPin *pin, IMediaSample *pSample)
57 ACMWrapperImpl* This = (ACMWrapperImpl*)pin->pin.pinInfo.pFilter;
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 EnterCriticalSection(&This->tf.csFilter);
70 if (This->tf.state == State_Stopped)
72 LeaveCriticalSection(&This->tf.csFilter);
73 return VFW_E_WRONG_STATE;
76 if (pin->end_of_stream || pin->flushing)
78 LeaveCriticalSection(&This->tf.csFilter);
82 hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
85 ERR("Cannot get pointer to sample data (%x)\n", hr);
86 LeaveCriticalSection(&This->tf.csFilter);
90 preroll = (IMediaSample_IsPreroll(pSample) == S_OK);
92 IMediaSample_GetTime(pSample, &tStart, &tStop);
93 cbSrcStream = IMediaSample_GetActualDataLength(pSample);
95 /* Prevent discontinuities when codecs 'absorb' data but not give anything back in return */
96 if (IMediaSample_IsDiscontinuity(pSample) == S_OK)
98 This->lasttime_real = tStart;
99 This->lasttime_sent = tStart;
101 else if (This->lasttime_real == tStart)
102 tStart = This->lasttime_sent;
104 WARN("Discontinuity\n");
108 TRACE("Sample data ptr = %p, size = %d\n", pbSrcStream, cbSrcStream);
110 hr = IPin_ConnectionMediaType(This->tf.ppPins[0], &amt);
113 ERR("Unable to retrieve media type\n");
114 LeaveCriticalSection(&This->tf.csFilter);
118 ash.pbSrc = pbSrcStream;
119 ash.cbSrcLength = cbSrcStream;
121 while(hr == S_OK && ash.cbSrcLength)
123 hr = OutputPin_GetDeliveryBuffer((OutputPin*)This->tf.ppPins[1], &pOutSample, NULL, NULL, 0);
126 ERR("Unable to get delivery buffer (%x)\n", hr);
127 LeaveCriticalSection(&This->tf.csFilter);
130 IMediaSample_SetPreroll(pOutSample, preroll);
132 hr = IMediaSample_SetActualDataLength(pOutSample, 0);
135 hr = IMediaSample_GetPointer(pOutSample, &pbDstStream);
137 ERR("Unable to get pointer to buffer (%x)\n", hr);
140 cbDstStream = IMediaSample_GetSize(pOutSample);
142 ash.cbStruct = sizeof(ash);
145 ash.pbDst = pbDstStream;
146 ash.cbDstLength = cbDstStream;
148 if ((res = acmStreamPrepareHeader(This->has, &ash, 0))) {
149 ERR("Cannot prepare header %d\n", res);
152 unprepare_header = TRUE;
154 if (IMediaSample_IsDiscontinuity(pSample) == S_OK)
156 res = acmStreamConvert(This->has, &ash, ACM_STREAMCONVERTF_START);
157 IMediaSample_SetDiscontinuity(pOutSample, TRUE);
158 /* One sample could be converted to multiple packets */
159 IMediaSample_SetDiscontinuity(pSample, FALSE);
163 res = acmStreamConvert(This->has, &ash, 0);
164 IMediaSample_SetDiscontinuity(pOutSample, FALSE);
169 if(res != MMSYSERR_MOREDATA)
170 ERR("Cannot convert data header %d\n", res);
174 TRACE("used in %u/%u, used out %u/%u\n", ash.cbSrcLengthUsed, ash.cbSrcLength, ash.cbDstLengthUsed, ash.cbDstLength);
176 hr = IMediaSample_SetActualDataLength(pOutSample, ash.cbDstLengthUsed);
179 /* Bug in acm codecs? It apparantly uses the input, but doesn't necessarily output immediately kl*/
180 if (!ash.cbSrcLengthUsed)
182 WARN("Sample was skipped? Outputted: %u\n", ash.cbDstLengthUsed);
187 TRACE("Sample start time: %u.%03u\n", (DWORD)(tStart/10000000), (DWORD)((tStart/10000)%1000));
188 if (ash.cbSrcLengthUsed == cbSrcStream)
190 IMediaSample_SetTime(pOutSample, &tStart, &tStop);
191 tStart = tMed = tStop;
193 else if (tStop != tStart)
195 tMed = tStop - tStart;
196 tMed = tStart + tMed * ash.cbSrcLengthUsed / cbSrcStream;
197 IMediaSample_SetTime(pOutSample, &tStart, &tMed);
202 ERR("No valid timestamp found\n");
203 IMediaSample_SetTime(pOutSample, NULL, NULL);
205 TRACE("Sample stop time: %u.%03u\n", (DWORD)(tStart/10000000), (DWORD)((tStart/10000)%1000));
207 LeaveCriticalSection(&This->tf.csFilter);
208 hr = OutputPin_SendSample((OutputPin*)This->tf.ppPins[1], pOutSample);
209 EnterCriticalSection(&This->tf.csFilter);
211 if (hr != S_OK && hr != VFW_E_NOT_CONNECTED) {
213 ERR("Error sending sample (%x)\n", hr);
218 if (unprepare_header && (res = acmStreamUnprepareHeader(This->has, &ash, 0)))
219 ERR("Cannot unprepare header %d\n", res);
220 unprepare_header = FALSE;
221 ash.pbSrc += ash.cbSrcLengthUsed;
222 ash.cbSrcLength -= ash.cbSrcLengthUsed;
225 IMediaSample_Release(pOutSample);
230 This->lasttime_real = tStop;
231 This->lasttime_sent = tMed;
233 LeaveCriticalSection(&This->tf.csFilter);
237 static HRESULT ACMWrapper_ConnectInput(InputPin *pin, const AM_MEDIA_TYPE * pmt)
239 ACMWrapperImpl* This = (ACMWrapperImpl *)pin->pin.pinInfo.pFilter;
242 TRACE("(%p)->(%p)\n", This, pmt);
244 /* Check root (GUID w/o FOURCC) */
245 if ((IsEqualIID(&pmt->majortype, &MEDIATYPE_Audio)) &&
246 (!memcmp(((const char *)&pmt->subtype)+4, ((const char *)&MEDIATYPE_Audio)+4, sizeof(GUID)-4)) &&
247 (IsEqualIID(&pmt->formattype, &FORMAT_WaveFormatEx)))
250 AM_MEDIA_TYPE* outpmt = &This->tf.pmt;
251 FreeMediaType(outpmt);
253 This->pWfIn = (LPWAVEFORMATEX)pmt->pbFormat;
256 /* TRACE("ALIGN = %d\n", pACMWrapper->pWfIn->nBlockAlign); */
257 /* pACMWrapper->pWfIn->nBlockAlign = 1; */
259 /* Set output audio data to PCM */
260 CopyMediaType(outpmt, pmt);
261 outpmt->subtype.Data1 = WAVE_FORMAT_PCM;
262 This->pWfOut = (WAVEFORMATEX*)outpmt->pbFormat;
263 This->pWfOut->wFormatTag = WAVE_FORMAT_PCM;
264 This->pWfOut->wBitsPerSample = 16;
265 This->pWfOut->nBlockAlign = This->pWfOut->wBitsPerSample * This->pWfOut->nChannels / 8;
266 This->pWfOut->cbSize = 0;
267 This->pWfOut->nAvgBytesPerSec = This->pWfOut->nChannels * This->pWfOut->nSamplesPerSec
268 * (This->pWfOut->wBitsPerSample/8);
270 if (!(res = acmStreamOpen(&drv, NULL, This->pWfIn, This->pWfOut, NULL, 0, 0, 0)))
274 /* Update buffer size of media samples in output */
275 ((OutputPin*)This->tf.ppPins[1])->allocProps.cbBuffer = This->pWfOut->nAvgBytesPerSec / 2;
276 TRACE("Connection accepted\n");
280 FIXME("acmStreamOpen returned %d\n", res);
281 FreeMediaType(outpmt);
282 TRACE("Unable to find a suitable ACM decompressor\n");
285 TRACE("Connection refused\n");
286 return VFW_E_TYPE_NOT_ACCEPTED;
289 static HRESULT ACMWrapper_Cleanup(InputPin *pin)
291 ACMWrapperImpl *This = (ACMWrapperImpl *)pin->pin.pinInfo.pFilter;
293 TRACE("(%p)->()\n", This);
296 acmStreamClose(This->has, 0);
299 This->lasttime_real = This->lasttime_sent = -1;
304 static const TransformFuncsTable ACMWrapper_FuncsTable = {
306 ACMWrapper_ProcessSampleData,
309 ACMWrapper_ConnectInput,
313 HRESULT ACMWrapper_create(IUnknown * pUnkOuter, LPVOID * ppv)
316 ACMWrapperImpl* This;
318 TRACE("(%p, %p)\n", pUnkOuter, ppv);
323 return CLASS_E_NOAGGREGATION;
325 /* Note: This memory is managed by the transform filter once created */
326 This = CoTaskMemAlloc(sizeof(ACMWrapperImpl));
327 ZeroMemory(This, sizeof(ACMWrapperImpl));
329 hr = TransformFilter_Create(&(This->tf), &CLSID_ACMWrapper, &ACMWrapper_FuncsTable, NULL, NULL, NULL);
335 This->lasttime_real = This->lasttime_sent = -1;