strmbase: Move TransformFilter implementation to strmbase.
[wine/multimedia.git] / dlls / quartz / acmwrapper.c
blob3fdb320c939578952e722b6aa1a0910a0f9b153c
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 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
42 typedef struct ACMWrapperImpl
44 TransformFilter tf;
45 IUnknown *seekthru_unk;
47 HACMSTREAM has;
48 LPWAVEFORMATEX pWfIn;
49 LPWAVEFORMATEX pWfOut;
51 LONGLONG lasttime_real;
52 LONGLONG lasttime_sent;
53 } ACMWrapperImpl;
55 static const IBaseFilterVtbl ACMWrapper_Vtbl;
57 static HRESULT WINAPI ACMWrapper_Receive(TransformFilter *tf, IMediaSample *pSample)
59 ACMWrapperImpl* This = (ACMWrapperImpl*)tf;
60 AM_MEDIA_TYPE amt;
61 IMediaSample* pOutSample = NULL;
62 DWORD cbDstStream, cbSrcStream;
63 LPBYTE pbDstStream;
64 LPBYTE pbSrcStream = NULL;
65 ACMSTREAMHEADER ash;
66 BOOL unprepare_header = FALSE, preroll;
67 MMRESULT res;
68 HRESULT hr;
69 LONGLONG tStart = -1, tStop = -1, tMed;
71 EnterCriticalSection(&This->tf.filter.csFilter);
72 hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
73 if (FAILED(hr))
75 ERR("Cannot get pointer to sample data (%x)\n", hr);
76 LeaveCriticalSection(&This->tf.filter.csFilter);
77 return hr;
80 preroll = (IMediaSample_IsPreroll(pSample) == S_OK);
82 IMediaSample_GetTime(pSample, &tStart, &tStop);
83 cbSrcStream = IMediaSample_GetActualDataLength(pSample);
85 /* Prevent discontinuities when codecs 'absorb' data but not give anything back in return */
86 if (IMediaSample_IsDiscontinuity(pSample) == S_OK)
88 This->lasttime_real = tStart;
89 This->lasttime_sent = tStart;
91 else if (This->lasttime_real == tStart)
92 tStart = This->lasttime_sent;
93 else
94 WARN("Discontinuity\n");
96 tMed = tStart;
98 TRACE("Sample data ptr = %p, size = %d\n", pbSrcStream, cbSrcStream);
100 hr = IPin_ConnectionMediaType(This->tf.ppPins[0], &amt);
101 if (FAILED(hr))
103 ERR("Unable to retrieve media type\n");
104 LeaveCriticalSection(&This->tf.filter.csFilter);
105 return hr;
108 ash.pbSrc = pbSrcStream;
109 ash.cbSrcLength = cbSrcStream;
111 while(hr == S_OK && ash.cbSrcLength)
113 hr = BaseOutputPinImpl_GetDeliveryBuffer((BaseOutputPin*)This->tf.ppPins[1], &pOutSample, NULL, NULL, 0);
114 if (FAILED(hr))
116 ERR("Unable to get delivery buffer (%x)\n", hr);
117 LeaveCriticalSection(&This->tf.filter.csFilter);
118 return hr;
120 IMediaSample_SetPreroll(pOutSample, preroll);
122 hr = IMediaSample_SetActualDataLength(pOutSample, 0);
123 assert(hr == S_OK);
125 hr = IMediaSample_GetPointer(pOutSample, &pbDstStream);
126 if (FAILED(hr)) {
127 ERR("Unable to get pointer to buffer (%x)\n", hr);
128 goto error;
130 cbDstStream = IMediaSample_GetSize(pOutSample);
132 ash.cbStruct = sizeof(ash);
133 ash.fdwStatus = 0;
134 ash.dwUser = 0;
135 ash.pbDst = pbDstStream;
136 ash.cbDstLength = cbDstStream;
138 if ((res = acmStreamPrepareHeader(This->has, &ash, 0))) {
139 ERR("Cannot prepare header %d\n", res);
140 goto error;
142 unprepare_header = TRUE;
144 if (IMediaSample_IsDiscontinuity(pSample) == S_OK)
146 res = acmStreamConvert(This->has, &ash, ACM_STREAMCONVERTF_START);
147 IMediaSample_SetDiscontinuity(pOutSample, TRUE);
148 /* One sample could be converted to multiple packets */
149 IMediaSample_SetDiscontinuity(pSample, FALSE);
151 else
153 res = acmStreamConvert(This->has, &ash, 0);
154 IMediaSample_SetDiscontinuity(pOutSample, FALSE);
157 if (res)
159 if(res != MMSYSERR_MOREDATA)
160 ERR("Cannot convert data header %d\n", res);
161 goto error;
164 TRACE("used in %u/%u, used out %u/%u\n", ash.cbSrcLengthUsed, ash.cbSrcLength, ash.cbDstLengthUsed, ash.cbDstLength);
166 hr = IMediaSample_SetActualDataLength(pOutSample, ash.cbDstLengthUsed);
167 assert(hr == S_OK);
169 /* Bug in acm codecs? It apparantly uses the input, but doesn't necessarily output immediately kl*/
170 if (!ash.cbSrcLengthUsed)
172 WARN("Sample was skipped? Outputted: %u\n", ash.cbDstLengthUsed);
173 ash.cbSrcLength = 0;
174 goto error;
177 TRACE("Sample start time: %u.%03u\n", (DWORD)(tStart/10000000), (DWORD)((tStart/10000)%1000));
178 if (ash.cbSrcLengthUsed == cbSrcStream)
180 IMediaSample_SetTime(pOutSample, &tStart, &tStop);
181 tStart = tMed = tStop;
183 else if (tStop != tStart)
185 tMed = tStop - tStart;
186 tMed = tStart + tMed * ash.cbSrcLengthUsed / cbSrcStream;
187 IMediaSample_SetTime(pOutSample, &tStart, &tMed);
188 tStart = tMed;
190 else
192 ERR("No valid timestamp found\n");
193 IMediaSample_SetTime(pOutSample, NULL, NULL);
195 TRACE("Sample stop time: %u.%03u\n", (DWORD)(tStart/10000000), (DWORD)((tStart/10000)%1000));
197 LeaveCriticalSection(&This->tf.filter.csFilter);
198 hr = BaseOutputPinImpl_Deliver((BaseOutputPin*)This->tf.ppPins[1], pOutSample);
199 EnterCriticalSection(&This->tf.filter.csFilter);
201 if (hr != S_OK && hr != VFW_E_NOT_CONNECTED) {
202 if (FAILED(hr))
203 ERR("Error sending sample (%x)\n", hr);
204 goto error;
207 error:
208 if (unprepare_header && (res = acmStreamUnprepareHeader(This->has, &ash, 0)))
209 ERR("Cannot unprepare header %d\n", res);
210 unprepare_header = FALSE;
211 ash.pbSrc += ash.cbSrcLengthUsed;
212 ash.cbSrcLength -= ash.cbSrcLengthUsed;
214 if (pOutSample)
215 IMediaSample_Release(pOutSample);
216 pOutSample = NULL;
220 This->lasttime_real = tStop;
221 This->lasttime_sent = tMed;
223 LeaveCriticalSection(&This->tf.filter.csFilter);
224 return hr;
227 static HRESULT WINAPI ACMWrapper_SetMediaType(TransformFilter *tf, PIN_DIRECTION dir, const AM_MEDIA_TYPE * pmt)
229 ACMWrapperImpl* This = (ACMWrapperImpl *)tf;
230 MMRESULT res;
232 TRACE("(%p)->(%i %p)\n", This, dir, pmt);
234 if (dir != PINDIR_INPUT)
235 return S_OK;
237 /* Check root (GUID w/o FOURCC) */
238 if ((IsEqualIID(&pmt->majortype, &MEDIATYPE_Audio)) &&
239 (!memcmp(((const char *)&pmt->subtype)+4, ((const char *)&MEDIATYPE_Audio)+4, sizeof(GUID)-4)) &&
240 (IsEqualIID(&pmt->formattype, &FORMAT_WaveFormatEx)))
242 HACMSTREAM drv;
243 AM_MEDIA_TYPE* outpmt = &This->tf.pmt;
244 FreeMediaType(outpmt);
246 This->pWfIn = (LPWAVEFORMATEX)pmt->pbFormat;
248 /* HACK */
249 /* TRACE("ALIGN = %d\n", pACMWrapper->pWfIn->nBlockAlign); */
250 /* pACMWrapper->pWfIn->nBlockAlign = 1; */
252 /* Set output audio data to PCM */
253 CopyMediaType(outpmt, pmt);
254 outpmt->subtype.Data1 = WAVE_FORMAT_PCM;
255 This->pWfOut = (WAVEFORMATEX*)outpmt->pbFormat;
256 This->pWfOut->wFormatTag = WAVE_FORMAT_PCM;
257 This->pWfOut->wBitsPerSample = 16;
258 This->pWfOut->nBlockAlign = This->pWfOut->wBitsPerSample * This->pWfOut->nChannels / 8;
259 This->pWfOut->cbSize = 0;
260 This->pWfOut->nAvgBytesPerSec = This->pWfOut->nChannels * This->pWfOut->nSamplesPerSec
261 * (This->pWfOut->wBitsPerSample/8);
263 if (!(res = acmStreamOpen(&drv, NULL, This->pWfIn, This->pWfOut, NULL, 0, 0, 0)))
265 This->has = drv;
267 /* Update buffer size of media samples in output */
268 ((BaseOutputPin*)This->tf.ppPins[1])->allocProps.cbBuffer = This->pWfOut->nAvgBytesPerSec / 2;
269 TRACE("Connection accepted\n");
270 return S_OK;
272 else
273 FIXME("acmStreamOpen returned %d\n", res);
274 FreeMediaType(outpmt);
275 TRACE("Unable to find a suitable ACM decompressor\n");
278 TRACE("Connection refused\n");
279 return VFW_E_TYPE_NOT_ACCEPTED;
282 static HRESULT WINAPI ACMWrapper_CompleteConnect(TransformFilter *tf, PIN_DIRECTION dir, IPin *pin)
284 ACMWrapperImpl* This = (ACMWrapperImpl *)tf;
285 MMRESULT res;
286 HACMSTREAM drv;
288 TRACE("(%p)\n", This);
290 if (dir != PINDIR_INPUT)
291 return S_OK;
293 if (!(res = acmStreamOpen(&drv, NULL, This->pWfIn, This->pWfOut, NULL, 0, 0, 0)))
295 This->has = drv;
297 /* Update buffer size of media samples in output */
298 ((BaseOutputPin*)This->tf.ppPins[1])->allocProps.cbBuffer = This->pWfOut->nAvgBytesPerSec / 2;
299 TRACE("Connection accepted\n");
300 return S_OK;
303 FIXME("acmStreamOpen returned %d\n", res);
304 TRACE("Unable to find a suitable ACM decompressor\n");
305 return VFW_E_TYPE_NOT_ACCEPTED;
308 static HRESULT WINAPI ACMWrapper_BreakConnect(TransformFilter *tf, PIN_DIRECTION dir)
310 ACMWrapperImpl *This = (ACMWrapperImpl *)tf;
312 TRACE("(%p)->(%i)\n", This,dir);
314 if (dir == PINDIR_INPUT)
316 if (This->has)
317 acmStreamClose(This->has, 0);
319 This->has = 0;
320 This->lasttime_real = This->lasttime_sent = -1;
323 return S_OK;
326 static const TransformFilterFuncTable ACMWrapper_FuncsTable = {
327 NULL,
328 ACMWrapper_Receive,
329 NULL,
330 NULL,
331 ACMWrapper_SetMediaType,
332 ACMWrapper_CompleteConnect,
333 ACMWrapper_BreakConnect,
334 NULL,
335 NULL,
336 NULL,
337 NULL
340 HRESULT ACMWrapper_create(IUnknown * pUnkOuter, LPVOID * ppv)
342 HRESULT hr;
343 ACMWrapperImpl* This;
345 TRACE("(%p, %p)\n", pUnkOuter, ppv);
347 *ppv = NULL;
349 if (pUnkOuter)
350 return CLASS_E_NOAGGREGATION;
352 hr = TransformFilter_Construct(&ACMWrapper_Vtbl, sizeof(ACMWrapperImpl), &CLSID_ACMWrapper, &ACMWrapper_FuncsTable, (IBaseFilter**)&This);
354 if (FAILED(hr))
355 return hr;
356 else
358 ISeekingPassThru *passthru;
359 hr = CoCreateInstance(&CLSID_SeekingPassThru, (IUnknown*)This, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void**)&This->seekthru_unk);
360 IUnknown_QueryInterface(This->seekthru_unk, &IID_ISeekingPassThru, (void**)&passthru);
361 ISeekingPassThru_Init(passthru, FALSE, (IPin*)This->tf.ppPins[0]);
362 ISeekingPassThru_Release(passthru);
365 *ppv = This;
366 This->lasttime_real = This->lasttime_sent = -1;
368 return hr;
371 HRESULT WINAPI ACMWrapper_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
373 HRESULT hr;
374 ACMWrapperImpl *This = (ACMWrapperImpl *)iface;
375 TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
377 if (IsEqualIID(riid, &IID_IMediaSeeking))
378 return IUnknown_QueryInterface(This->seekthru_unk, riid, ppv);
380 hr = TransformFilterImpl_QueryInterface(iface, riid, ppv);
382 return hr;
386 static const IBaseFilterVtbl ACMWrapper_Vtbl =
388 ACMWrapper_QueryInterface,
389 BaseFilterImpl_AddRef,
390 TransformFilterImpl_Release,
391 BaseFilterImpl_GetClassID,
392 TransformFilterImpl_Stop,
393 TransformFilterImpl_Pause,
394 TransformFilterImpl_Run,
395 BaseFilterImpl_GetState,
396 BaseFilterImpl_SetSyncSource,
397 BaseFilterImpl_GetSyncSource,
398 BaseFilterImpl_EnumPins,
399 TransformFilterImpl_FindPin,
400 BaseFilterImpl_QueryFilterInfo,
401 BaseFilterImpl_JoinFilterGraph,
402 BaseFilterImpl_QueryVendorInfo