oleaut32: Add a helper to return INVOKEKIND flags value.
[wine] / dlls / quartz / acmwrapper.c
1 /*
2  * ACM Wrapper
3  *
4  * Copyright 2005 Christian Costa
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22
23 #include "quartz_private.h"
24 #include "pin.h"
25
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"
34
35 #include <assert.h>
36
37 #include "wine/unicode.h"
38 #include "wine/debug.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
41
42 typedef struct ACMWrapperImpl
43 {
44     TransformFilter tf;
45     IUnknown *seekthru_unk;
46
47     HACMSTREAM has;
48     LPWAVEFORMATEX pWfIn;
49     LPWAVEFORMATEX pWfOut;
50
51     LONGLONG lasttime_real;
52     LONGLONG lasttime_sent;
53 } ACMWrapperImpl;
54
55 static const IBaseFilterVtbl ACMWrapper_Vtbl;
56
57 static HRESULT WINAPI ACMWrapper_Receive(TransformFilter *tf, IMediaSample *pSample)
58 {
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;
70     LONGLONG mtStart = -1, mtStop = -1, mtMed;
71
72     EnterCriticalSection(&This->tf.filter.csFilter);
73     hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
74     if (FAILED(hr))
75     {
76         ERR("Cannot get pointer to sample data (%x)\n", hr);
77         LeaveCriticalSection(&This->tf.filter.csFilter);
78         return hr;
79     }
80
81     preroll = (IMediaSample_IsPreroll(pSample) == S_OK);
82
83     IMediaSample_GetTime(pSample, &tStart, &tStop);
84     if (IMediaSample_GetMediaTime(pSample, &mtStart, &mtStop) != S_OK)
85         mtStart = mtStop = -1;
86     cbSrcStream = IMediaSample_GetActualDataLength(pSample);
87
88     /* Prevent discontinuities when codecs 'absorb' data but not give anything back in return */
89     if (IMediaSample_IsDiscontinuity(pSample) == S_OK)
90     {
91         This->lasttime_real = tStart;
92         This->lasttime_sent = tStart;
93     }
94     else if (This->lasttime_real == tStart)
95         tStart = This->lasttime_sent;
96     else
97         WARN("Discontinuity\n");
98
99     tMed = tStart;
100     mtMed = mtStart;
101
102     TRACE("Sample data ptr = %p, size = %d\n", pbSrcStream, cbSrcStream);
103
104     hr = IPin_ConnectionMediaType(This->tf.ppPins[0], &amt);
105     if (FAILED(hr))
106     {
107         ERR("Unable to retrieve media type\n");
108         LeaveCriticalSection(&This->tf.filter.csFilter);
109         return hr;
110     }
111
112     ash.pbSrc = pbSrcStream;
113     ash.cbSrcLength = cbSrcStream;
114
115     while(hr == S_OK && ash.cbSrcLength)
116     {
117         hr = BaseOutputPinImpl_GetDeliveryBuffer((BaseOutputPin*)This->tf.ppPins[1], &pOutSample, NULL, NULL, 0);
118         if (FAILED(hr))
119         {
120             ERR("Unable to get delivery buffer (%x)\n", hr);
121             LeaveCriticalSection(&This->tf.filter.csFilter);
122             return hr;
123         }
124         IMediaSample_SetPreroll(pOutSample, preroll);
125
126         hr = IMediaSample_SetActualDataLength(pOutSample, 0);
127         assert(hr == S_OK);
128
129         hr = IMediaSample_GetPointer(pOutSample, &pbDstStream);
130         if (FAILED(hr)) {
131             ERR("Unable to get pointer to buffer (%x)\n", hr);
132             goto error;
133         }
134         cbDstStream = IMediaSample_GetSize(pOutSample);
135
136         ash.cbStruct = sizeof(ash);
137         ash.fdwStatus = 0;
138         ash.dwUser = 0;
139         ash.pbDst = pbDstStream;
140         ash.cbDstLength = cbDstStream;
141
142         if ((res = acmStreamPrepareHeader(This->has, &ash, 0))) {
143             ERR("Cannot prepare header %d\n", res);
144             goto error;
145         }
146         unprepare_header = TRUE;
147
148         if (IMediaSample_IsDiscontinuity(pSample) == S_OK)
149         {
150             res = acmStreamConvert(This->has, &ash, ACM_STREAMCONVERTF_START);
151             IMediaSample_SetDiscontinuity(pOutSample, TRUE);
152             /* One sample could be converted to multiple packets */
153             IMediaSample_SetDiscontinuity(pSample, FALSE);
154         }
155         else
156         {
157             res = acmStreamConvert(This->has, &ash, 0);
158             IMediaSample_SetDiscontinuity(pOutSample, FALSE);
159         }
160
161         if (res)
162         {
163             if(res != MMSYSERR_MOREDATA)
164                 ERR("Cannot convert data header %d\n", res);
165             goto error;
166         }
167
168         TRACE("used in %u/%u, used out %u/%u\n", ash.cbSrcLengthUsed, ash.cbSrcLength, ash.cbDstLengthUsed, ash.cbDstLength);
169
170         hr = IMediaSample_SetActualDataLength(pOutSample, ash.cbDstLengthUsed);
171         assert(hr == S_OK);
172
173         /* Bug in acm codecs? It apparantly uses the input, but doesn't necessarily output immediately kl*/
174         if (!ash.cbSrcLengthUsed)
175         {
176             WARN("Sample was skipped? Outputted: %u\n", ash.cbDstLengthUsed);
177             ash.cbSrcLength = 0;
178             goto error;
179         }
180
181         TRACE("Sample start time: %u.%03u\n", (DWORD)(tStart/10000000), (DWORD)((tStart/10000)%1000));
182         if (ash.cbSrcLengthUsed == cbSrcStream)
183         {
184             IMediaSample_SetTime(pOutSample, &tStart, &tStop);
185             tStart = tMed = tStop;
186         }
187         else if (tStop != tStart)
188         {
189             tMed = tStop - tStart;
190             tMed = tStart + tMed * ash.cbSrcLengthUsed / cbSrcStream;
191             IMediaSample_SetTime(pOutSample, &tStart, &tMed);
192             tStart = tMed;
193         }
194         else
195         {
196             ERR("No valid timestamp found\n");
197             IMediaSample_SetTime(pOutSample, NULL, NULL);
198         }
199
200         if (mtStart < 0) {
201             IMediaSample_SetMediaTime(pOutSample, NULL, NULL);
202         } else if (ash.cbSrcLengthUsed == cbSrcStream) {
203             IMediaSample_SetMediaTime(pOutSample, &mtStart, &mtStop);
204             mtStart = mtMed = mtStop;
205         } else if (mtStop >= mtStart) {
206             mtMed = mtStop - mtStart;
207             mtMed = mtStart + mtMed * ash.cbSrcLengthUsed / cbSrcStream;
208             IMediaSample_SetMediaTime(pOutSample, &mtStart, &mtMed);
209             mtStart = mtMed;
210         } else {
211             IMediaSample_SetMediaTime(pOutSample, NULL, NULL);
212         }
213
214         TRACE("Sample stop time: %u.%03u\n", (DWORD)(tStart/10000000), (DWORD)((tStart/10000)%1000));
215
216         LeaveCriticalSection(&This->tf.filter.csFilter);
217         hr = BaseOutputPinImpl_Deliver((BaseOutputPin*)This->tf.ppPins[1], pOutSample);
218         EnterCriticalSection(&This->tf.filter.csFilter);
219
220         if (hr != S_OK && hr != VFW_E_NOT_CONNECTED) {
221             if (FAILED(hr))
222                 ERR("Error sending sample (%x)\n", hr);
223             goto error;
224         }
225
226 error:
227         if (unprepare_header && (res = acmStreamUnprepareHeader(This->has, &ash, 0)))
228             ERR("Cannot unprepare header %d\n", res);
229         unprepare_header = FALSE;
230         ash.pbSrc += ash.cbSrcLengthUsed;
231         ash.cbSrcLength -= ash.cbSrcLengthUsed;
232
233         if (pOutSample)
234             IMediaSample_Release(pOutSample);
235         pOutSample = NULL;
236
237     }
238
239     This->lasttime_real = tStop;
240     This->lasttime_sent = tMed;
241
242     LeaveCriticalSection(&This->tf.filter.csFilter);
243     return hr;
244 }
245
246 static HRESULT WINAPI ACMWrapper_SetMediaType(TransformFilter *tf, PIN_DIRECTION dir, const AM_MEDIA_TYPE * pmt)
247 {
248     ACMWrapperImpl* This = (ACMWrapperImpl *)tf;
249     MMRESULT res;
250
251     TRACE("(%p)->(%i %p)\n", This, dir, pmt);
252
253     if (dir != PINDIR_INPUT)
254         return S_OK;
255
256     /* Check root (GUID w/o FOURCC) */
257     if ((IsEqualIID(&pmt->majortype, &MEDIATYPE_Audio)) &&
258         (!memcmp(((const char *)&pmt->subtype)+4, ((const char *)&MEDIATYPE_Audio)+4, sizeof(GUID)-4)) &&
259         (IsEqualIID(&pmt->formattype, &FORMAT_WaveFormatEx)))
260     {
261         HACMSTREAM drv;
262         WAVEFORMATEX *wfx = (WAVEFORMATEX*)pmt->pbFormat;
263         AM_MEDIA_TYPE* outpmt = &This->tf.pmt;
264
265         if (!wfx || wfx->wFormatTag == WAVE_FORMAT_PCM || wfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
266             return VFW_E_TYPE_NOT_ACCEPTED;
267         FreeMediaType(outpmt);
268
269         This->pWfIn = (LPWAVEFORMATEX)pmt->pbFormat;
270
271         /* HACK */
272         /* TRACE("ALIGN = %d\n", pACMWrapper->pWfIn->nBlockAlign); */
273         /* pACMWrapper->pWfIn->nBlockAlign = 1; */
274
275         /* Set output audio data to PCM */
276         CopyMediaType(outpmt, pmt);
277         outpmt->subtype.Data1 = WAVE_FORMAT_PCM;
278         This->pWfOut = (WAVEFORMATEX*)outpmt->pbFormat;
279         This->pWfOut->wFormatTag = WAVE_FORMAT_PCM;
280         This->pWfOut->wBitsPerSample = 16;
281         This->pWfOut->nBlockAlign = This->pWfOut->wBitsPerSample * This->pWfOut->nChannels / 8;
282         This->pWfOut->cbSize = 0;
283         This->pWfOut->nAvgBytesPerSec = This->pWfOut->nChannels * This->pWfOut->nSamplesPerSec
284                                                 * (This->pWfOut->wBitsPerSample/8);
285
286         if (!(res = acmStreamOpen(&drv, NULL, This->pWfIn, This->pWfOut, NULL, 0, 0, 0)))
287         {
288             This->has = drv;
289
290             TRACE("Connection accepted\n");
291             return S_OK;
292         }
293         else
294             FIXME("acmStreamOpen returned %d\n", res);
295         FreeMediaType(outpmt);
296         TRACE("Unable to find a suitable ACM decompressor\n");
297     }
298
299     TRACE("Connection refused\n");
300     return VFW_E_TYPE_NOT_ACCEPTED;
301 }
302
303 static HRESULT WINAPI ACMWrapper_CompleteConnect(TransformFilter *tf, PIN_DIRECTION dir, IPin *pin)
304 {
305     ACMWrapperImpl* This = (ACMWrapperImpl *)tf;
306     MMRESULT res;
307     HACMSTREAM drv;
308
309     TRACE("(%p)\n", This);
310
311     if (dir != PINDIR_INPUT)
312         return S_OK;
313
314     if (!(res = acmStreamOpen(&drv, NULL, This->pWfIn, This->pWfOut, NULL, 0, 0, 0)))
315     {
316         This->has = drv;
317
318         TRACE("Connection accepted\n");
319         return S_OK;
320     }
321
322     FIXME("acmStreamOpen returned %d\n", res);
323     TRACE("Unable to find a suitable ACM decompressor\n");
324     return VFW_E_TYPE_NOT_ACCEPTED;
325 }
326
327 static HRESULT WINAPI ACMWrapper_BreakConnect(TransformFilter *tf, PIN_DIRECTION dir)
328 {
329     ACMWrapperImpl *This = (ACMWrapperImpl *)tf;
330
331     TRACE("(%p)->(%i)\n", This,dir);
332
333     if (dir == PINDIR_INPUT)
334     {
335         if (This->has)
336             acmStreamClose(This->has, 0);
337
338         This->has = 0;
339         This->lasttime_real = This->lasttime_sent = -1;
340     }
341
342     return S_OK;
343 }
344
345 static HRESULT WINAPI ACMWrapper_DecideBufferSize(TransformFilter *tf, IMemAllocator *pAlloc, ALLOCATOR_PROPERTIES *ppropInputRequest)
346 {
347     ACMWrapperImpl *pACM = (ACMWrapperImpl*)tf;
348     ALLOCATOR_PROPERTIES actual;
349
350     if (!ppropInputRequest->cbAlign)
351         ppropInputRequest->cbAlign = 1;
352
353     if (ppropInputRequest->cbBuffer < pACM->pWfOut->nAvgBytesPerSec / 2)
354             ppropInputRequest->cbBuffer = pACM->pWfOut->nAvgBytesPerSec / 2;
355
356     if (!ppropInputRequest->cBuffers)
357         ppropInputRequest->cBuffers = 1;
358
359     return IMemAllocator_SetProperties(pAlloc, ppropInputRequest, &actual);
360 }
361
362 static const TransformFilterFuncTable ACMWrapper_FuncsTable = {
363     ACMWrapper_DecideBufferSize,
364     NULL,
365     ACMWrapper_Receive,
366     NULL,
367     NULL,
368     ACMWrapper_SetMediaType,
369     ACMWrapper_CompleteConnect,
370     ACMWrapper_BreakConnect,
371     NULL,
372     NULL,
373     NULL,
374     NULL
375 };
376
377 HRESULT ACMWrapper_create(IUnknown * pUnkOuter, LPVOID * ppv)
378 {
379     HRESULT hr;
380     ACMWrapperImpl* This;
381
382     TRACE("(%p, %p)\n", pUnkOuter, ppv);
383
384     *ppv = NULL;
385
386     if (pUnkOuter)
387         return CLASS_E_NOAGGREGATION;
388
389     hr = TransformFilter_Construct(&ACMWrapper_Vtbl, sizeof(ACMWrapperImpl), &CLSID_ACMWrapper, &ACMWrapper_FuncsTable, (IBaseFilter**)&This);
390
391     if (FAILED(hr))
392         return hr;
393     else
394     {
395         ISeekingPassThru *passthru;
396         hr = CoCreateInstance(&CLSID_SeekingPassThru, (IUnknown*)This, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void**)&This->seekthru_unk);
397         IUnknown_QueryInterface(This->seekthru_unk, &IID_ISeekingPassThru, (void**)&passthru);
398         ISeekingPassThru_Init(passthru, FALSE, (IPin*)This->tf.ppPins[0]);
399         ISeekingPassThru_Release(passthru);
400     }
401
402     *ppv = This;
403     This->lasttime_real = This->lasttime_sent = -1;
404
405     return hr;
406 }
407
408 HRESULT WINAPI ACMWrapper_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
409 {
410     HRESULT hr;
411     ACMWrapperImpl *This = (ACMWrapperImpl *)iface;
412     TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
413
414     if (IsEqualIID(riid, &IID_IMediaSeeking))
415         return IUnknown_QueryInterface(This->seekthru_unk, riid, ppv);
416
417     hr = TransformFilterImpl_QueryInterface(iface, riid, ppv);
418
419     return hr;
420 }
421
422
423 static const IBaseFilterVtbl ACMWrapper_Vtbl =
424 {
425     ACMWrapper_QueryInterface,
426     BaseFilterImpl_AddRef,
427     TransformFilterImpl_Release,
428     BaseFilterImpl_GetClassID,
429     TransformFilterImpl_Stop,
430     TransformFilterImpl_Pause,
431     TransformFilterImpl_Run,
432     BaseFilterImpl_GetState,
433     BaseFilterImpl_SetSyncSource,
434     BaseFilterImpl_GetSyncSource,
435     BaseFilterImpl_EnumPins,
436     TransformFilterImpl_FindPin,
437     BaseFilterImpl_QueryFilterInfo,
438     BaseFilterImpl_JoinFilterGraph,
439     BaseFilterImpl_QueryVendorInfo
440 };