Add AppDefaults app selection to control panel
[wine/gsoc-2012-control.git] / dlls / quartz / acmwrapper.c
blobf98362caff5d2472000f7f45efa44739ea7e64d8
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 inline ACMWrapperImpl *impl_from_TransformFilter( TransformFilter *iface )
59 return CONTAINING_RECORD(iface, ACMWrapperImpl, tf.filter);
62 static inline ACMWrapperImpl *impl_from_IBaseFilter( IBaseFilter *iface )
64 return CONTAINING_RECORD(iface, ACMWrapperImpl, tf.filter.IBaseFilter_iface);
67 static HRESULT WINAPI ACMWrapper_Receive(TransformFilter *tf, IMediaSample *pSample)
69 ACMWrapperImpl* This = impl_from_TransformFilter(tf);
70 AM_MEDIA_TYPE amt;
71 IMediaSample* pOutSample = NULL;
72 DWORD cbDstStream, cbSrcStream;
73 LPBYTE pbDstStream;
74 LPBYTE pbSrcStream = NULL;
75 ACMSTREAMHEADER ash;
76 BOOL unprepare_header = FALSE, preroll;
77 MMRESULT res;
78 HRESULT hr;
79 LONGLONG tStart = -1, tStop = -1, tMed;
80 LONGLONG mtStart = -1, mtStop = -1, mtMed;
82 EnterCriticalSection(&This->tf.csReceive);
83 hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
84 if (FAILED(hr))
86 ERR("Cannot get pointer to sample data (%x)\n", hr);
87 LeaveCriticalSection(&This->tf.csReceive);
88 return hr;
91 preroll = (IMediaSample_IsPreroll(pSample) == S_OK);
93 IMediaSample_GetTime(pSample, &tStart, &tStop);
94 if (IMediaSample_GetMediaTime(pSample, &mtStart, &mtStop) != S_OK)
95 mtStart = mtStop = -1;
96 cbSrcStream = IMediaSample_GetActualDataLength(pSample);
98 /* Prevent discontinuities when codecs 'absorb' data but not give anything back in return */
99 if (IMediaSample_IsDiscontinuity(pSample) == S_OK)
101 This->lasttime_real = tStart;
102 This->lasttime_sent = tStart;
104 else if (This->lasttime_real == tStart)
105 tStart = This->lasttime_sent;
106 else
107 WARN("Discontinuity\n");
109 tMed = tStart;
110 mtMed = mtStart;
112 TRACE("Sample data ptr = %p, size = %d\n", pbSrcStream, cbSrcStream);
114 hr = IPin_ConnectionMediaType(This->tf.ppPins[0], &amt);
115 if (FAILED(hr))
117 ERR("Unable to retrieve media type\n");
118 LeaveCriticalSection(&This->tf.csReceive);
119 return hr;
122 ash.pbSrc = pbSrcStream;
123 ash.cbSrcLength = cbSrcStream;
125 while(hr == S_OK && ash.cbSrcLength)
127 hr = BaseOutputPinImpl_GetDeliveryBuffer((BaseOutputPin*)This->tf.ppPins[1], &pOutSample, NULL, NULL, 0);
128 if (FAILED(hr))
130 ERR("Unable to get delivery buffer (%x)\n", hr);
131 LeaveCriticalSection(&This->tf.csReceive);
132 return hr;
134 IMediaSample_SetPreroll(pOutSample, preroll);
136 hr = IMediaSample_SetActualDataLength(pOutSample, 0);
137 assert(hr == S_OK);
139 hr = IMediaSample_GetPointer(pOutSample, &pbDstStream);
140 if (FAILED(hr)) {
141 ERR("Unable to get pointer to buffer (%x)\n", hr);
142 goto error;
144 cbDstStream = IMediaSample_GetSize(pOutSample);
146 ash.cbStruct = sizeof(ash);
147 ash.fdwStatus = 0;
148 ash.dwUser = 0;
149 ash.pbDst = pbDstStream;
150 ash.cbDstLength = cbDstStream;
152 if ((res = acmStreamPrepareHeader(This->has, &ash, 0))) {
153 ERR("Cannot prepare header %d\n", res);
154 goto error;
156 unprepare_header = TRUE;
158 if (IMediaSample_IsDiscontinuity(pSample) == S_OK)
160 res = acmStreamConvert(This->has, &ash, ACM_STREAMCONVERTF_START);
161 IMediaSample_SetDiscontinuity(pOutSample, TRUE);
162 /* One sample could be converted to multiple packets */
163 IMediaSample_SetDiscontinuity(pSample, FALSE);
165 else
167 res = acmStreamConvert(This->has, &ash, 0);
168 IMediaSample_SetDiscontinuity(pOutSample, FALSE);
171 if (res)
173 if(res != MMSYSERR_MOREDATA)
174 ERR("Cannot convert data header %d\n", res);
175 goto error;
178 TRACE("used in %u/%u, used out %u/%u\n", ash.cbSrcLengthUsed, ash.cbSrcLength, ash.cbDstLengthUsed, ash.cbDstLength);
180 hr = IMediaSample_SetActualDataLength(pOutSample, ash.cbDstLengthUsed);
181 assert(hr == S_OK);
183 /* Bug in acm codecs? It apparently uses the input, but doesn't necessarily output immediately */
184 if (!ash.cbSrcLengthUsed)
186 WARN("Sample was skipped? Outputted: %u\n", ash.cbDstLengthUsed);
187 ash.cbSrcLength = 0;
188 goto error;
191 TRACE("Sample start time: %u.%03u\n", (DWORD)(tStart/10000000), (DWORD)((tStart/10000)%1000));
192 if (ash.cbSrcLengthUsed == cbSrcStream)
194 IMediaSample_SetTime(pOutSample, &tStart, &tStop);
195 tStart = tMed = tStop;
197 else if (tStop != tStart)
199 tMed = tStop - tStart;
200 tMed = tStart + tMed * ash.cbSrcLengthUsed / cbSrcStream;
201 IMediaSample_SetTime(pOutSample, &tStart, &tMed);
202 tStart = tMed;
204 else
206 ERR("No valid timestamp found\n");
207 IMediaSample_SetTime(pOutSample, NULL, NULL);
210 if (mtStart < 0) {
211 IMediaSample_SetMediaTime(pOutSample, NULL, NULL);
212 } else if (ash.cbSrcLengthUsed == cbSrcStream) {
213 IMediaSample_SetMediaTime(pOutSample, &mtStart, &mtStop);
214 mtStart = mtMed = mtStop;
215 } else if (mtStop >= mtStart) {
216 mtMed = mtStop - mtStart;
217 mtMed = mtStart + mtMed * ash.cbSrcLengthUsed / cbSrcStream;
218 IMediaSample_SetMediaTime(pOutSample, &mtStart, &mtMed);
219 mtStart = mtMed;
220 } else {
221 IMediaSample_SetMediaTime(pOutSample, NULL, NULL);
224 TRACE("Sample stop time: %u.%03u\n", (DWORD)(tStart/10000000), (DWORD)((tStart/10000)%1000));
226 LeaveCriticalSection(&This->tf.csReceive);
227 hr = BaseOutputPinImpl_Deliver((BaseOutputPin*)This->tf.ppPins[1], pOutSample);
228 EnterCriticalSection(&This->tf.csReceive);
230 if (hr != S_OK && hr != VFW_E_NOT_CONNECTED) {
231 if (FAILED(hr))
232 ERR("Error sending sample (%x)\n", hr);
233 goto error;
236 error:
237 if (unprepare_header && (res = acmStreamUnprepareHeader(This->has, &ash, 0)))
238 ERR("Cannot unprepare header %d\n", res);
239 unprepare_header = FALSE;
240 ash.pbSrc += ash.cbSrcLengthUsed;
241 ash.cbSrcLength -= ash.cbSrcLengthUsed;
243 IMediaSample_Release(pOutSample);
244 pOutSample = NULL;
248 This->lasttime_real = tStop;
249 This->lasttime_sent = tMed;
251 LeaveCriticalSection(&This->tf.csReceive);
252 return hr;
255 static HRESULT WINAPI ACMWrapper_SetMediaType(TransformFilter *tf, PIN_DIRECTION dir, const AM_MEDIA_TYPE * pmt)
257 ACMWrapperImpl* This = impl_from_TransformFilter(tf);
258 MMRESULT res;
260 TRACE("(%p)->(%i %p)\n", This, dir, pmt);
262 if (dir != PINDIR_INPUT)
263 return S_OK;
265 /* Check root (GUID w/o FOURCC) */
266 if ((IsEqualIID(&pmt->majortype, &MEDIATYPE_Audio)) &&
267 (!memcmp(((const char *)&pmt->subtype)+4, ((const char *)&MEDIATYPE_Audio)+4, sizeof(GUID)-4)) &&
268 (IsEqualIID(&pmt->formattype, &FORMAT_WaveFormatEx)))
270 HACMSTREAM drv;
271 WAVEFORMATEX *wfx = (WAVEFORMATEX*)pmt->pbFormat;
272 AM_MEDIA_TYPE* outpmt = &This->tf.pmt;
274 if (!wfx || wfx->wFormatTag == WAVE_FORMAT_PCM || wfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
275 return VFW_E_TYPE_NOT_ACCEPTED;
276 FreeMediaType(outpmt);
278 This->pWfIn = (LPWAVEFORMATEX)pmt->pbFormat;
280 /* HACK */
281 /* TRACE("ALIGN = %d\n", pACMWrapper->pWfIn->nBlockAlign); */
282 /* pACMWrapper->pWfIn->nBlockAlign = 1; */
284 /* Set output audio data to PCM */
285 CopyMediaType(outpmt, pmt);
286 outpmt->subtype.Data1 = WAVE_FORMAT_PCM;
287 This->pWfOut = (WAVEFORMATEX*)outpmt->pbFormat;
288 This->pWfOut->wFormatTag = WAVE_FORMAT_PCM;
289 This->pWfOut->wBitsPerSample = 16;
290 This->pWfOut->nBlockAlign = This->pWfOut->wBitsPerSample * This->pWfOut->nChannels / 8;
291 This->pWfOut->cbSize = 0;
292 This->pWfOut->nAvgBytesPerSec = This->pWfOut->nChannels * This->pWfOut->nSamplesPerSec
293 * (This->pWfOut->wBitsPerSample/8);
295 if (!(res = acmStreamOpen(&drv, NULL, This->pWfIn, This->pWfOut, NULL, 0, 0, 0)))
297 This->has = drv;
299 TRACE("Connection accepted\n");
300 return S_OK;
302 else
303 FIXME("acmStreamOpen returned %d\n", res);
304 FreeMediaType(outpmt);
305 TRACE("Unable to find a suitable ACM decompressor\n");
308 TRACE("Connection refused\n");
309 return VFW_E_TYPE_NOT_ACCEPTED;
312 static HRESULT WINAPI ACMWrapper_CompleteConnect(TransformFilter *tf, PIN_DIRECTION dir, IPin *pin)
314 ACMWrapperImpl* This = impl_from_TransformFilter(tf);
315 MMRESULT res;
316 HACMSTREAM drv;
318 TRACE("(%p)\n", This);
320 if (dir != PINDIR_INPUT)
321 return S_OK;
323 if (!(res = acmStreamOpen(&drv, NULL, This->pWfIn, This->pWfOut, NULL, 0, 0, 0)))
325 This->has = drv;
327 TRACE("Connection accepted\n");
328 return S_OK;
331 FIXME("acmStreamOpen returned %d\n", res);
332 TRACE("Unable to find a suitable ACM decompressor\n");
333 return VFW_E_TYPE_NOT_ACCEPTED;
336 static HRESULT WINAPI ACMWrapper_BreakConnect(TransformFilter *tf, PIN_DIRECTION dir)
338 ACMWrapperImpl *This = impl_from_TransformFilter(tf);
340 TRACE("(%p)->(%i)\n", This,dir);
342 if (dir == PINDIR_INPUT)
344 if (This->has)
345 acmStreamClose(This->has, 0);
347 This->has = 0;
348 This->lasttime_real = This->lasttime_sent = -1;
351 return S_OK;
354 static HRESULT WINAPI ACMWrapper_DecideBufferSize(TransformFilter *tf, IMemAllocator *pAlloc, ALLOCATOR_PROPERTIES *ppropInputRequest)
356 ACMWrapperImpl *pACM = impl_from_TransformFilter(tf);
357 ALLOCATOR_PROPERTIES actual;
359 if (!ppropInputRequest->cbAlign)
360 ppropInputRequest->cbAlign = 1;
362 if (ppropInputRequest->cbBuffer < pACM->pWfOut->nAvgBytesPerSec / 2)
363 ppropInputRequest->cbBuffer = pACM->pWfOut->nAvgBytesPerSec / 2;
365 if (!ppropInputRequest->cBuffers)
366 ppropInputRequest->cBuffers = 1;
368 return IMemAllocator_SetProperties(pAlloc, ppropInputRequest, &actual);
371 static const TransformFilterFuncTable ACMWrapper_FuncsTable = {
372 ACMWrapper_DecideBufferSize,
373 NULL,
374 ACMWrapper_Receive,
375 NULL,
376 NULL,
377 ACMWrapper_SetMediaType,
378 ACMWrapper_CompleteConnect,
379 ACMWrapper_BreakConnect,
380 NULL,
381 NULL,
382 NULL,
383 NULL
386 HRESULT ACMWrapper_create(IUnknown * pUnkOuter, LPVOID * ppv)
388 HRESULT hr;
389 ACMWrapperImpl* This;
391 TRACE("(%p, %p)\n", pUnkOuter, ppv);
393 *ppv = NULL;
395 if (pUnkOuter)
396 return CLASS_E_NOAGGREGATION;
398 hr = TransformFilter_Construct(&ACMWrapper_Vtbl, sizeof(ACMWrapperImpl), &CLSID_ACMWrapper, &ACMWrapper_FuncsTable, (IBaseFilter**)&This);
400 if (FAILED(hr))
401 return hr;
402 else
404 ISeekingPassThru *passthru;
405 hr = CoCreateInstance(&CLSID_SeekingPassThru, (IUnknown*)This, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void**)&This->seekthru_unk);
406 IUnknown_QueryInterface(This->seekthru_unk, &IID_ISeekingPassThru, (void**)&passthru);
407 ISeekingPassThru_Init(passthru, FALSE, This->tf.ppPins[0]);
408 ISeekingPassThru_Release(passthru);
411 *ppv = This;
412 This->lasttime_real = This->lasttime_sent = -1;
414 return hr;
417 static HRESULT WINAPI ACMWrapper_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
419 HRESULT hr;
420 ACMWrapperImpl *This = impl_from_IBaseFilter(iface);
421 TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
423 if (IsEqualIID(riid, &IID_IMediaSeeking))
424 return IUnknown_QueryInterface(This->seekthru_unk, riid, ppv);
426 hr = TransformFilterImpl_QueryInterface(iface, riid, ppv);
428 return hr;
432 static const IBaseFilterVtbl ACMWrapper_Vtbl =
434 ACMWrapper_QueryInterface,
435 BaseFilterImpl_AddRef,
436 TransformFilterImpl_Release,
437 BaseFilterImpl_GetClassID,
438 TransformFilterImpl_Stop,
439 TransformFilterImpl_Pause,
440 TransformFilterImpl_Run,
441 BaseFilterImpl_GetState,
442 BaseFilterImpl_SetSyncSource,
443 BaseFilterImpl_GetSyncSource,
444 BaseFilterImpl_EnumPins,
445 TransformFilterImpl_FindPin,
446 BaseFilterImpl_QueryFilterInfo,
447 BaseFilterImpl_JoinFilterGraph,
448 BaseFilterImpl_QueryVendorInfo