msvcrt: Added _fread_nolock_s implementation.
[wine.git] / dlls / quartz / acmwrapper.c
blobcd0b0d83ab5dfdc29c076af1e3f38a14046151a8
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;
46 HACMSTREAM has;
47 LPWAVEFORMATEX pWfIn;
48 LPWAVEFORMATEX pWfOut;
50 LONGLONG lasttime_real;
51 LONGLONG lasttime_sent;
52 } ACMWrapperImpl;
54 static const IBaseFilterVtbl ACMWrapper_Vtbl;
56 static inline ACMWrapperImpl *impl_from_TransformFilter( TransformFilter *iface )
58 return CONTAINING_RECORD(iface, ACMWrapperImpl, tf.filter);
61 static inline ACMWrapperImpl *impl_from_IBaseFilter( IBaseFilter *iface )
63 return CONTAINING_RECORD(iface, ACMWrapperImpl, tf.filter.IBaseFilter_iface);
66 static HRESULT WINAPI ACMWrapper_Receive(TransformFilter *tf, IMediaSample *pSample)
68 ACMWrapperImpl* This = impl_from_TransformFilter(tf);
69 AM_MEDIA_TYPE amt;
70 IMediaSample* pOutSample = NULL;
71 DWORD cbDstStream, cbSrcStream;
72 LPBYTE pbDstStream;
73 LPBYTE pbSrcStream = NULL;
74 ACMSTREAMHEADER ash;
75 BOOL unprepare_header = FALSE, preroll;
76 MMRESULT res;
77 HRESULT hr;
78 LONGLONG tStart = -1, tStop = -1, tMed;
79 LONGLONG mtStart = -1, mtStop = -1, mtMed;
81 EnterCriticalSection(&This->tf.csReceive);
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.csReceive);
87 return hr;
90 preroll = (IMediaSample_IsPreroll(pSample) == S_OK);
92 IMediaSample_GetTime(pSample, &tStart, &tStop);
93 if (IMediaSample_GetMediaTime(pSample, &mtStart, &mtStop) != S_OK)
94 mtStart = mtStop = -1;
95 cbSrcStream = IMediaSample_GetActualDataLength(pSample);
97 /* Prevent discontinuities when codecs 'absorb' data but not give anything back in return */
98 if (IMediaSample_IsDiscontinuity(pSample) == S_OK)
100 This->lasttime_real = tStart;
101 This->lasttime_sent = tStart;
103 else if (This->lasttime_real == tStart)
104 tStart = This->lasttime_sent;
105 else
106 WARN("Discontinuity\n");
108 tMed = tStart;
109 mtMed = mtStart;
111 TRACE("Sample data ptr = %p, size = %d\n", pbSrcStream, cbSrcStream);
113 hr = IPin_ConnectionMediaType(This->tf.ppPins[0], &amt);
114 if (FAILED(hr))
116 ERR("Unable to retrieve media type\n");
117 LeaveCriticalSection(&This->tf.csReceive);
118 return hr;
121 ash.pbSrc = pbSrcStream;
122 ash.cbSrcLength = cbSrcStream;
124 while(hr == S_OK && ash.cbSrcLength)
126 hr = BaseOutputPinImpl_GetDeliveryBuffer((BaseOutputPin*)This->tf.ppPins[1], &pOutSample, NULL, NULL, 0);
127 if (FAILED(hr))
129 ERR("Unable to get delivery buffer (%x)\n", hr);
130 LeaveCriticalSection(&This->tf.csReceive);
131 return hr;
133 IMediaSample_SetPreroll(pOutSample, preroll);
135 hr = IMediaSample_SetActualDataLength(pOutSample, 0);
136 assert(hr == S_OK);
138 hr = IMediaSample_GetPointer(pOutSample, &pbDstStream);
139 if (FAILED(hr)) {
140 ERR("Unable to get pointer to buffer (%x)\n", hr);
141 goto error;
143 cbDstStream = IMediaSample_GetSize(pOutSample);
145 ash.cbStruct = sizeof(ash);
146 ash.fdwStatus = 0;
147 ash.dwUser = 0;
148 ash.pbDst = pbDstStream;
149 ash.cbDstLength = cbDstStream;
151 if ((res = acmStreamPrepareHeader(This->has, &ash, 0))) {
152 ERR("Cannot prepare header %d\n", res);
153 goto error;
155 unprepare_header = TRUE;
157 if (IMediaSample_IsDiscontinuity(pSample) == S_OK)
159 res = acmStreamConvert(This->has, &ash, ACM_STREAMCONVERTF_START);
160 IMediaSample_SetDiscontinuity(pOutSample, TRUE);
161 /* One sample could be converted to multiple packets */
162 IMediaSample_SetDiscontinuity(pSample, FALSE);
164 else
166 res = acmStreamConvert(This->has, &ash, 0);
167 IMediaSample_SetDiscontinuity(pOutSample, FALSE);
170 if (res)
172 if(res != MMSYSERR_MOREDATA)
173 ERR("Cannot convert data header %d\n", res);
174 goto error;
177 TRACE("used in %u/%u, used out %u/%u\n", ash.cbSrcLengthUsed, ash.cbSrcLength, ash.cbDstLengthUsed, ash.cbDstLength);
179 hr = IMediaSample_SetActualDataLength(pOutSample, ash.cbDstLengthUsed);
180 assert(hr == S_OK);
182 /* Bug in acm codecs? It apparently uses the input, but doesn't necessarily output immediately */
183 if (!ash.cbSrcLengthUsed)
185 WARN("Sample was skipped? Outputted: %u\n", ash.cbDstLengthUsed);
186 ash.cbSrcLength = 0;
187 goto error;
190 TRACE("Sample start time: %u.%03u\n", (DWORD)(tStart/10000000), (DWORD)((tStart/10000)%1000));
191 if (ash.cbSrcLengthUsed == cbSrcStream)
193 IMediaSample_SetTime(pOutSample, &tStart, &tStop);
194 tStart = tMed = tStop;
196 else if (tStop != tStart)
198 tMed = tStop - tStart;
199 tMed = tStart + tMed * ash.cbSrcLengthUsed / cbSrcStream;
200 IMediaSample_SetTime(pOutSample, &tStart, &tMed);
201 tStart = tMed;
203 else
205 ERR("No valid timestamp found\n");
206 IMediaSample_SetTime(pOutSample, NULL, NULL);
209 if (mtStart < 0) {
210 IMediaSample_SetMediaTime(pOutSample, NULL, NULL);
211 } else if (ash.cbSrcLengthUsed == cbSrcStream) {
212 IMediaSample_SetMediaTime(pOutSample, &mtStart, &mtStop);
213 mtStart = mtMed = mtStop;
214 } else if (mtStop >= mtStart) {
215 mtMed = mtStop - mtStart;
216 mtMed = mtStart + mtMed * ash.cbSrcLengthUsed / cbSrcStream;
217 IMediaSample_SetMediaTime(pOutSample, &mtStart, &mtMed);
218 mtStart = mtMed;
219 } else {
220 IMediaSample_SetMediaTime(pOutSample, NULL, NULL);
223 TRACE("Sample stop time: %u.%03u\n", (DWORD)(tStart/10000000), (DWORD)((tStart/10000)%1000));
225 LeaveCriticalSection(&This->tf.csReceive);
226 hr = BaseOutputPinImpl_Deliver((BaseOutputPin*)This->tf.ppPins[1], pOutSample);
227 EnterCriticalSection(&This->tf.csReceive);
229 if (hr != S_OK && hr != VFW_E_NOT_CONNECTED) {
230 if (FAILED(hr))
231 ERR("Error sending sample (%x)\n", hr);
232 goto error;
235 error:
236 if (unprepare_header && (res = acmStreamUnprepareHeader(This->has, &ash, 0)))
237 ERR("Cannot unprepare header %d\n", res);
238 unprepare_header = FALSE;
239 ash.pbSrc += ash.cbSrcLengthUsed;
240 ash.cbSrcLength -= ash.cbSrcLengthUsed;
242 IMediaSample_Release(pOutSample);
243 pOutSample = NULL;
247 This->lasttime_real = tStop;
248 This->lasttime_sent = tMed;
250 LeaveCriticalSection(&This->tf.csReceive);
251 return hr;
254 static HRESULT WINAPI ACMWrapper_SetMediaType(TransformFilter *tf, PIN_DIRECTION dir, const AM_MEDIA_TYPE * pmt)
256 ACMWrapperImpl* This = impl_from_TransformFilter(tf);
257 MMRESULT res;
259 TRACE("(%p)->(%i %p)\n", This, dir, pmt);
261 if (dir != PINDIR_INPUT)
262 return S_OK;
264 /* Check root (GUID w/o FOURCC) */
265 if ((IsEqualIID(&pmt->majortype, &MEDIATYPE_Audio)) &&
266 (!memcmp(((const char *)&pmt->subtype)+4, ((const char *)&MEDIATYPE_Audio)+4, sizeof(GUID)-4)) &&
267 (IsEqualIID(&pmt->formattype, &FORMAT_WaveFormatEx)))
269 HACMSTREAM drv;
270 WAVEFORMATEX *wfx = (WAVEFORMATEX*)pmt->pbFormat;
271 AM_MEDIA_TYPE* outpmt = &This->tf.pmt;
273 if (!wfx || wfx->wFormatTag == WAVE_FORMAT_PCM || wfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
274 return VFW_E_TYPE_NOT_ACCEPTED;
275 FreeMediaType(outpmt);
277 This->pWfIn = (LPWAVEFORMATEX)pmt->pbFormat;
279 /* HACK */
280 /* TRACE("ALIGN = %d\n", pACMWrapper->pWfIn->nBlockAlign); */
281 /* pACMWrapper->pWfIn->nBlockAlign = 1; */
283 /* Set output audio data to PCM */
284 CopyMediaType(outpmt, pmt);
285 outpmt->subtype.Data1 = WAVE_FORMAT_PCM;
286 This->pWfOut = (WAVEFORMATEX*)outpmt->pbFormat;
287 This->pWfOut->wFormatTag = WAVE_FORMAT_PCM;
288 This->pWfOut->wBitsPerSample = 16;
289 This->pWfOut->nBlockAlign = This->pWfOut->wBitsPerSample * This->pWfOut->nChannels / 8;
290 This->pWfOut->cbSize = 0;
291 This->pWfOut->nAvgBytesPerSec = This->pWfOut->nChannels * This->pWfOut->nSamplesPerSec
292 * (This->pWfOut->wBitsPerSample/8);
294 if (!(res = acmStreamOpen(&drv, NULL, This->pWfIn, This->pWfOut, NULL, 0, 0, 0)))
296 This->has = drv;
298 TRACE("Connection accepted\n");
299 return S_OK;
301 else
302 FIXME("acmStreamOpen returned %d\n", res);
303 FreeMediaType(outpmt);
304 TRACE("Unable to find a suitable ACM decompressor\n");
307 TRACE("Connection refused\n");
308 return VFW_E_TYPE_NOT_ACCEPTED;
311 static HRESULT WINAPI ACMWrapper_CompleteConnect(TransformFilter *tf, PIN_DIRECTION dir, IPin *pin)
313 ACMWrapperImpl* This = impl_from_TransformFilter(tf);
314 MMRESULT res;
315 HACMSTREAM drv;
317 TRACE("(%p)\n", This);
319 if (dir != PINDIR_INPUT)
320 return S_OK;
322 if (!(res = acmStreamOpen(&drv, NULL, This->pWfIn, This->pWfOut, NULL, 0, 0, 0)))
324 This->has = drv;
326 TRACE("Connection accepted\n");
327 return S_OK;
330 FIXME("acmStreamOpen returned %d\n", res);
331 TRACE("Unable to find a suitable ACM decompressor\n");
332 return VFW_E_TYPE_NOT_ACCEPTED;
335 static HRESULT WINAPI ACMWrapper_BreakConnect(TransformFilter *tf, PIN_DIRECTION dir)
337 ACMWrapperImpl *This = impl_from_TransformFilter(tf);
339 TRACE("(%p)->(%i)\n", This,dir);
341 if (dir == PINDIR_INPUT)
343 if (This->has)
344 acmStreamClose(This->has, 0);
346 This->has = 0;
347 This->lasttime_real = This->lasttime_sent = -1;
350 return S_OK;
353 static HRESULT WINAPI ACMWrapper_DecideBufferSize(TransformFilter *tf, IMemAllocator *pAlloc, ALLOCATOR_PROPERTIES *ppropInputRequest)
355 ACMWrapperImpl *pACM = impl_from_TransformFilter(tf);
356 ALLOCATOR_PROPERTIES actual;
358 if (!ppropInputRequest->cbAlign)
359 ppropInputRequest->cbAlign = 1;
361 if (ppropInputRequest->cbBuffer < pACM->pWfOut->nAvgBytesPerSec / 2)
362 ppropInputRequest->cbBuffer = pACM->pWfOut->nAvgBytesPerSec / 2;
364 if (!ppropInputRequest->cBuffers)
365 ppropInputRequest->cBuffers = 1;
367 return IMemAllocator_SetProperties(pAlloc, ppropInputRequest, &actual);
370 static const TransformFilterFuncTable ACMWrapper_FuncsTable = {
371 ACMWrapper_DecideBufferSize,
372 NULL,
373 ACMWrapper_Receive,
374 NULL,
375 NULL,
376 ACMWrapper_SetMediaType,
377 ACMWrapper_CompleteConnect,
378 ACMWrapper_BreakConnect,
379 NULL,
380 NULL,
381 NULL,
382 NULL
385 HRESULT ACMWrapper_create(IUnknown * pUnkOuter, LPVOID * ppv)
387 HRESULT hr;
388 ACMWrapperImpl* This;
390 TRACE("(%p, %p)\n", pUnkOuter, ppv);
392 *ppv = NULL;
394 if (pUnkOuter)
395 return CLASS_E_NOAGGREGATION;
397 hr = TransformFilter_Construct(&ACMWrapper_Vtbl, sizeof(ACMWrapperImpl), &CLSID_ACMWrapper, &ACMWrapper_FuncsTable, (IBaseFilter**)&This);
399 if (FAILED(hr))
400 return hr;
402 *ppv = This;
403 This->lasttime_real = This->lasttime_sent = -1;
405 return hr;
408 static const IBaseFilterVtbl ACMWrapper_Vtbl =
410 TransformFilterImpl_QueryInterface,
411 BaseFilterImpl_AddRef,
412 TransformFilterImpl_Release,
413 BaseFilterImpl_GetClassID,
414 TransformFilterImpl_Stop,
415 TransformFilterImpl_Pause,
416 TransformFilterImpl_Run,
417 BaseFilterImpl_GetState,
418 BaseFilterImpl_SetSyncSource,
419 BaseFilterImpl_GetSyncSource,
420 BaseFilterImpl_EnumPins,
421 TransformFilterImpl_FindPin,
422 BaseFilterImpl_QueryFilterInfo,
423 BaseFilterImpl_JoinFilterGraph,
424 BaseFilterImpl_QueryVendorInfo