quartz: Remove superfluous pointer casts.
[wine/hacks.git] / dlls / quartz / acmwrapper.c
blobc275d83397974ca40a890bffa459e99d37dacdd1
1 /*
2 * ACM Wrapper
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
21 #include "config.h"
23 #include "quartz_private.h"
24 #include "pin.h"
26 #include "uuids.h"
27 #include "mmreg.h"
28 #include "windef.h"
29 #include "winbase.h"
30 #include "dshow.h"
31 #include "strmif.h"
32 #include "vfwmsgs.h"
33 #include "msacm.h"
35 #include <assert.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;
47 HACMSTREAM has;
48 LPWAVEFORMATEX pWfIn;
49 LPWAVEFORMATEX pWfOut;
51 LONGLONG lasttime_real;
52 LONGLONG lasttime_sent;
53 } ACMWrapperImpl;
55 static HRESULT ACMWrapper_ProcessSampleData(InputPin *pin, IMediaSample *pSample)
57 ACMWrapperImpl* This = (ACMWrapperImpl*)pin->pin.pinInfo.pFilter;
58 AM_MEDIA_TYPE amt;
59 IMediaSample* pOutSample = NULL;
60 DWORD cbDstStream, cbSrcStream;
61 LPBYTE pbDstStream;
62 LPBYTE pbSrcStream = NULL;
63 ACMSTREAMHEADER ash;
64 BOOL unprepare_header = FALSE, preroll;
65 MMRESULT res;
66 HRESULT hr;
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);
79 return S_FALSE;
82 hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
83 if (FAILED(hr))
85 ERR("Cannot get pointer to sample data (%x)\n", hr);
86 LeaveCriticalSection(&This->tf.csFilter);
87 return hr;
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;
103 else
104 WARN("Discontinuity\n");
106 tMed = tStart;
108 TRACE("Sample data ptr = %p, size = %ld\n", pbSrcStream, (long)cbSrcStream);
110 hr = IPin_ConnectionMediaType(This->tf.ppPins[0], &amt);
111 if (FAILED(hr))
113 ERR("Unable to retrieve media type\n");
114 LeaveCriticalSection(&This->tf.csFilter);
115 return hr;
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);
124 if (FAILED(hr))
126 ERR("Unable to get delivery buffer (%x)\n", hr);
127 LeaveCriticalSection(&This->tf.csFilter);
128 return hr;
130 IMediaSample_SetPreroll(pOutSample, preroll);
132 hr = IMediaSample_SetActualDataLength(pOutSample, 0);
133 assert(hr == S_OK);
135 hr = IMediaSample_GetPointer(pOutSample, &pbDstStream);
136 if (FAILED(hr)) {
137 ERR("Unable to get pointer to buffer (%x)\n", hr);
138 goto error;
140 cbDstStream = IMediaSample_GetSize(pOutSample);
142 ash.cbStruct = sizeof(ash);
143 ash.fdwStatus = 0;
144 ash.dwUser = 0;
145 ash.pbDst = pbDstStream;
146 ash.cbDstLength = cbDstStream;
148 if ((res = acmStreamPrepareHeader(This->has, &ash, 0))) {
149 ERR("Cannot prepare header %d\n", res);
150 goto error;
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);
161 else
163 res = acmStreamConvert(This->has, &ash, 0);
164 IMediaSample_SetDiscontinuity(pOutSample, FALSE);
167 if (res)
169 if(res != MMSYSERR_MOREDATA)
170 ERR("Cannot convert data header %d\n", res);
171 goto error;
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);
177 assert(hr == S_OK);
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);
183 ash.cbSrcLength = 0;
184 goto error;
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);
198 tStart = tMed;
200 else
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) {
212 if (FAILED(hr))
213 ERR("Error sending sample (%x)\n", hr);
214 goto error;
217 error:
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;
224 if (pOutSample)
225 IMediaSample_Release(pOutSample);
226 pOutSample = NULL;
230 This->lasttime_real = tStop;
231 This->lasttime_sent = tMed;
233 LeaveCriticalSection(&This->tf.csFilter);
234 return hr;
237 static HRESULT ACMWrapper_ConnectInput(InputPin *pin, const AM_MEDIA_TYPE * pmt)
239 ACMWrapperImpl* This = (ACMWrapperImpl *)pin->pin.pinInfo.pFilter;
240 MMRESULT res;
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)))
249 HACMSTREAM drv;
250 AM_MEDIA_TYPE* outpmt = &This->tf.pmt;
251 FreeMediaType(outpmt);
253 This->pWfIn = (LPWAVEFORMATEX)pmt->pbFormat;
255 /* HACK */
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)))
272 This->has = drv;
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");
277 return S_OK;
279 else
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);
295 if (This->has)
296 acmStreamClose(This->has, 0);
298 This->has = 0;
299 This->lasttime_real = This->lasttime_sent = -1;
301 return S_OK;
304 static const TransformFuncsTable ACMWrapper_FuncsTable = {
305 NULL,
306 ACMWrapper_ProcessSampleData,
307 NULL,
308 NULL,
309 ACMWrapper_ConnectInput,
310 ACMWrapper_Cleanup
313 HRESULT ACMWrapper_create(IUnknown * pUnkOuter, LPVOID * ppv)
315 HRESULT hr;
316 ACMWrapperImpl* This;
318 TRACE("(%p, %p)\n", pUnkOuter, ppv);
320 *ppv = NULL;
322 if (pUnkOuter)
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);
331 if (FAILED(hr))
332 return hr;
334 *ppv = This;
335 This->lasttime_real = This->lasttime_sent = -1;
337 return hr;