crypt32/tests: Add tests for decoding enveloped messages.
[wine/multimedia.git] / dlls / quartz / acmwrapper.c
blob66ae6c689c02c898c2619dc65a2438ae0cd917d3
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 TRACE("Connection accepted\n");
268 return S_OK;
270 else
271 FIXME("acmStreamOpen returned %d\n", res);
272 FreeMediaType(outpmt);
273 TRACE("Unable to find a suitable ACM decompressor\n");
276 TRACE("Connection refused\n");
277 return VFW_E_TYPE_NOT_ACCEPTED;
280 static HRESULT WINAPI ACMWrapper_CompleteConnect(TransformFilter *tf, PIN_DIRECTION dir, IPin *pin)
282 ACMWrapperImpl* This = (ACMWrapperImpl *)tf;
283 MMRESULT res;
284 HACMSTREAM drv;
286 TRACE("(%p)\n", This);
288 if (dir != PINDIR_INPUT)
289 return S_OK;
291 if (!(res = acmStreamOpen(&drv, NULL, This->pWfIn, This->pWfOut, NULL, 0, 0, 0)))
293 This->has = drv;
295 TRACE("Connection accepted\n");
296 return S_OK;
299 FIXME("acmStreamOpen returned %d\n", res);
300 TRACE("Unable to find a suitable ACM decompressor\n");
301 return VFW_E_TYPE_NOT_ACCEPTED;
304 static HRESULT WINAPI ACMWrapper_BreakConnect(TransformFilter *tf, PIN_DIRECTION dir)
306 ACMWrapperImpl *This = (ACMWrapperImpl *)tf;
308 TRACE("(%p)->(%i)\n", This,dir);
310 if (dir == PINDIR_INPUT)
312 if (This->has)
313 acmStreamClose(This->has, 0);
315 This->has = 0;
316 This->lasttime_real = This->lasttime_sent = -1;
319 return S_OK;
322 static HRESULT WINAPI ACMWrapper_DecideBufferSize(TransformFilter *tf, IMemAllocator *pAlloc, ALLOCATOR_PROPERTIES *ppropInputRequest)
324 ACMWrapperImpl *pACM = (ACMWrapperImpl*)tf;
325 ALLOCATOR_PROPERTIES actual;
327 if (!ppropInputRequest->cbAlign)
328 ppropInputRequest->cbAlign = 1;
330 if (ppropInputRequest->cbBuffer < pACM->pWfOut->nAvgBytesPerSec / 2)
331 ppropInputRequest->cbBuffer = pACM->pWfOut->nAvgBytesPerSec / 2;
333 if (!ppropInputRequest->cBuffers)
334 ppropInputRequest->cBuffers = 1;
336 return IMemAllocator_SetProperties(pAlloc, ppropInputRequest, &actual);
339 static const TransformFilterFuncTable ACMWrapper_FuncsTable = {
340 ACMWrapper_DecideBufferSize,
341 NULL,
342 ACMWrapper_Receive,
343 NULL,
344 NULL,
345 ACMWrapper_SetMediaType,
346 ACMWrapper_CompleteConnect,
347 ACMWrapper_BreakConnect,
348 NULL,
349 NULL,
350 NULL,
351 NULL
354 HRESULT ACMWrapper_create(IUnknown * pUnkOuter, LPVOID * ppv)
356 HRESULT hr;
357 ACMWrapperImpl* This;
359 TRACE("(%p, %p)\n", pUnkOuter, ppv);
361 *ppv = NULL;
363 if (pUnkOuter)
364 return CLASS_E_NOAGGREGATION;
366 hr = TransformFilter_Construct(&ACMWrapper_Vtbl, sizeof(ACMWrapperImpl), &CLSID_ACMWrapper, &ACMWrapper_FuncsTable, (IBaseFilter**)&This);
368 if (FAILED(hr))
369 return hr;
370 else
372 ISeekingPassThru *passthru;
373 hr = CoCreateInstance(&CLSID_SeekingPassThru, (IUnknown*)This, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void**)&This->seekthru_unk);
374 IUnknown_QueryInterface(This->seekthru_unk, &IID_ISeekingPassThru, (void**)&passthru);
375 ISeekingPassThru_Init(passthru, FALSE, (IPin*)This->tf.ppPins[0]);
376 ISeekingPassThru_Release(passthru);
379 *ppv = This;
380 This->lasttime_real = This->lasttime_sent = -1;
382 return hr;
385 HRESULT WINAPI ACMWrapper_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
387 HRESULT hr;
388 ACMWrapperImpl *This = (ACMWrapperImpl *)iface;
389 TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
391 if (IsEqualIID(riid, &IID_IMediaSeeking))
392 return IUnknown_QueryInterface(This->seekthru_unk, riid, ppv);
394 hr = TransformFilterImpl_QueryInterface(iface, riid, ppv);
396 return hr;
400 static const IBaseFilterVtbl ACMWrapper_Vtbl =
402 ACMWrapper_QueryInterface,
403 BaseFilterImpl_AddRef,
404 TransformFilterImpl_Release,
405 BaseFilterImpl_GetClassID,
406 TransformFilterImpl_Stop,
407 TransformFilterImpl_Pause,
408 TransformFilterImpl_Run,
409 BaseFilterImpl_GetState,
410 BaseFilterImpl_SetSyncSource,
411 BaseFilterImpl_GetSyncSource,
412 BaseFilterImpl_EnumPins,
413 TransformFilterImpl_FindPin,
414 BaseFilterImpl_QueryFilterInfo,
415 BaseFilterImpl_JoinFilterGraph,
416 BaseFilterImpl_QueryVendorInfo