Release 1.5.29.
[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
46     HACMSTREAM has;
47     LPWAVEFORMATEX pWfIn;
48     LPWAVEFORMATEX pWfOut;
49
50     LONGLONG lasttime_real;
51     LONGLONG lasttime_sent;
52 } ACMWrapperImpl;
53
54 static const IBaseFilterVtbl ACMWrapper_Vtbl;
55
56 static inline ACMWrapperImpl *impl_from_TransformFilter( TransformFilter *iface )
57 {
58     return CONTAINING_RECORD(iface, ACMWrapperImpl, tf.filter);
59 }
60
61 static inline ACMWrapperImpl *impl_from_IBaseFilter( IBaseFilter *iface )
62 {
63     return CONTAINING_RECORD(iface, ACMWrapperImpl, tf.filter.IBaseFilter_iface);
64 }
65
66 static HRESULT WINAPI ACMWrapper_Receive(TransformFilter *tf, IMediaSample *pSample)
67 {
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;
80
81     EnterCriticalSection(&This->tf.csReceive);
82     hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
83     if (FAILED(hr))
84     {
85         ERR("Cannot get pointer to sample data (%x)\n", hr);
86         LeaveCriticalSection(&This->tf.csReceive);
87         return hr;
88     }
89
90     preroll = (IMediaSample_IsPreroll(pSample) == S_OK);
91
92     IMediaSample_GetTime(pSample, &tStart, &tStop);
93     if (IMediaSample_GetMediaTime(pSample, &mtStart, &mtStop) != S_OK)
94         mtStart = mtStop = -1;
95     cbSrcStream = IMediaSample_GetActualDataLength(pSample);
96
97     /* Prevent discontinuities when codecs 'absorb' data but not give anything back in return */
98     if (IMediaSample_IsDiscontinuity(pSample) == S_OK)
99     {
100         This->lasttime_real = tStart;
101         This->lasttime_sent = tStart;
102     }
103     else if (This->lasttime_real == tStart)
104         tStart = This->lasttime_sent;
105     else
106         WARN("Discontinuity\n");
107
108     tMed = tStart;
109     mtMed = mtStart;
110
111     TRACE("Sample data ptr = %p, size = %d\n", pbSrcStream, cbSrcStream);
112
113     hr = IPin_ConnectionMediaType(This->tf.ppPins[0], &amt);
114     if (FAILED(hr))
115     {
116         ERR("Unable to retrieve media type\n");
117         LeaveCriticalSection(&This->tf.csReceive);
118         return hr;
119     }
120
121     ash.pbSrc = pbSrcStream;
122     ash.cbSrcLength = cbSrcStream;
123
124     while(hr == S_OK && ash.cbSrcLength)
125     {
126         hr = BaseOutputPinImpl_GetDeliveryBuffer((BaseOutputPin*)This->tf.ppPins[1], &pOutSample, NULL, NULL, 0);
127         if (FAILED(hr))
128         {
129             ERR("Unable to get delivery buffer (%x)\n", hr);
130             LeaveCriticalSection(&This->tf.csReceive);
131             return hr;
132         }
133         IMediaSample_SetPreroll(pOutSample, preroll);
134
135         hr = IMediaSample_SetActualDataLength(pOutSample, 0);
136         assert(hr == S_OK);
137
138         hr = IMediaSample_GetPointer(pOutSample, &pbDstStream);
139         if (FAILED(hr)) {
140             ERR("Unable to get pointer to buffer (%x)\n", hr);
141             goto error;
142         }
143         cbDstStream = IMediaSample_GetSize(pOutSample);
144
145         ash.cbStruct = sizeof(ash);
146         ash.fdwStatus = 0;
147         ash.dwUser = 0;
148         ash.pbDst = pbDstStream;
149         ash.cbDstLength = cbDstStream;
150
151         if ((res = acmStreamPrepareHeader(This->has, &ash, 0))) {
152             ERR("Cannot prepare header %d\n", res);
153             goto error;
154         }
155         unprepare_header = TRUE;
156
157         if (IMediaSample_IsDiscontinuity(pSample) == S_OK)
158         {
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);
163         }
164         else
165         {
166             res = acmStreamConvert(This->has, &ash, 0);
167             IMediaSample_SetDiscontinuity(pOutSample, FALSE);
168         }
169
170         if (res)
171         {
172             if(res != MMSYSERR_MOREDATA)
173                 ERR("Cannot convert data header %d\n", res);
174             goto error;
175         }
176
177         TRACE("used in %u/%u, used out %u/%u\n", ash.cbSrcLengthUsed, ash.cbSrcLength, ash.cbDstLengthUsed, ash.cbDstLength);
178
179         hr = IMediaSample_SetActualDataLength(pOutSample, ash.cbDstLengthUsed);
180         assert(hr == S_OK);
181
182         /* Bug in acm codecs? It apparently uses the input, but doesn't necessarily output immediately */
183         if (!ash.cbSrcLengthUsed)
184         {
185             WARN("Sample was skipped? Outputted: %u\n", ash.cbDstLengthUsed);
186             ash.cbSrcLength = 0;
187             goto error;
188         }
189
190         TRACE("Sample start time: %u.%03u\n", (DWORD)(tStart/10000000), (DWORD)((tStart/10000)%1000));
191         if (ash.cbSrcLengthUsed == cbSrcStream)
192         {
193             IMediaSample_SetTime(pOutSample, &tStart, &tStop);
194             tStart = tMed = tStop;
195         }
196         else if (tStop != tStart)
197         {
198             tMed = tStop - tStart;
199             tMed = tStart + tMed * ash.cbSrcLengthUsed / cbSrcStream;
200             IMediaSample_SetTime(pOutSample, &tStart, &tMed);
201             tStart = tMed;
202         }
203         else
204         {
205             ERR("No valid timestamp found\n");
206             IMediaSample_SetTime(pOutSample, NULL, NULL);
207         }
208
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);
221         }
222
223         TRACE("Sample stop time: %u.%03u\n", (DWORD)(tStart/10000000), (DWORD)((tStart/10000)%1000));
224
225         LeaveCriticalSection(&This->tf.csReceive);
226         hr = BaseOutputPinImpl_Deliver((BaseOutputPin*)This->tf.ppPins[1], pOutSample);
227         EnterCriticalSection(&This->tf.csReceive);
228
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;
233         }
234
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;
241
242         IMediaSample_Release(pOutSample);
243         pOutSample = NULL;
244
245     }
246
247     This->lasttime_real = tStop;
248     This->lasttime_sent = tMed;
249
250     LeaveCriticalSection(&This->tf.csReceive);
251     return hr;
252 }
253
254 static HRESULT WINAPI ACMWrapper_SetMediaType(TransformFilter *tf, PIN_DIRECTION dir, const AM_MEDIA_TYPE * pmt)
255 {
256     ACMWrapperImpl* This = impl_from_TransformFilter(tf);
257     MMRESULT res;
258
259     TRACE("(%p)->(%i %p)\n", This, dir, pmt);
260
261     if (dir != PINDIR_INPUT)
262         return S_OK;
263
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)))
268     {
269         HACMSTREAM drv;
270         WAVEFORMATEX *wfx = (WAVEFORMATEX*)pmt->pbFormat;
271         AM_MEDIA_TYPE* outpmt = &This->tf.pmt;
272
273         if (!wfx || wfx->wFormatTag == WAVE_FORMAT_PCM || wfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
274             return VFW_E_TYPE_NOT_ACCEPTED;
275         FreeMediaType(outpmt);
276
277         This->pWfIn = (LPWAVEFORMATEX)pmt->pbFormat;
278
279         /* HACK */
280         /* TRACE("ALIGN = %d\n", pACMWrapper->pWfIn->nBlockAlign); */
281         /* pACMWrapper->pWfIn->nBlockAlign = 1; */
282
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);
293
294         if (!(res = acmStreamOpen(&drv, NULL, This->pWfIn, This->pWfOut, NULL, 0, 0, 0)))
295         {
296             This->has = drv;
297
298             TRACE("Connection accepted\n");
299             return S_OK;
300         }
301         else
302             FIXME("acmStreamOpen returned %d\n", res);
303         FreeMediaType(outpmt);
304         TRACE("Unable to find a suitable ACM decompressor\n");
305     }
306
307     TRACE("Connection refused\n");
308     return VFW_E_TYPE_NOT_ACCEPTED;
309 }
310
311 static HRESULT WINAPI ACMWrapper_CompleteConnect(TransformFilter *tf, PIN_DIRECTION dir, IPin *pin)
312 {
313     ACMWrapperImpl* This = impl_from_TransformFilter(tf);
314     MMRESULT res;
315     HACMSTREAM drv;
316
317     TRACE("(%p)\n", This);
318
319     if (dir != PINDIR_INPUT)
320         return S_OK;
321
322     if (!(res = acmStreamOpen(&drv, NULL, This->pWfIn, This->pWfOut, NULL, 0, 0, 0)))
323     {
324         This->has = drv;
325
326         TRACE("Connection accepted\n");
327         return S_OK;
328     }
329
330     FIXME("acmStreamOpen returned %d\n", res);
331     TRACE("Unable to find a suitable ACM decompressor\n");
332     return VFW_E_TYPE_NOT_ACCEPTED;
333 }
334
335 static HRESULT WINAPI ACMWrapper_BreakConnect(TransformFilter *tf, PIN_DIRECTION dir)
336 {
337     ACMWrapperImpl *This = impl_from_TransformFilter(tf);
338
339     TRACE("(%p)->(%i)\n", This,dir);
340
341     if (dir == PINDIR_INPUT)
342     {
343         if (This->has)
344             acmStreamClose(This->has, 0);
345
346         This->has = 0;
347         This->lasttime_real = This->lasttime_sent = -1;
348     }
349
350     return S_OK;
351 }
352
353 static HRESULT WINAPI ACMWrapper_DecideBufferSize(TransformFilter *tf, IMemAllocator *pAlloc, ALLOCATOR_PROPERTIES *ppropInputRequest)
354 {
355     ACMWrapperImpl *pACM = impl_from_TransformFilter(tf);
356     ALLOCATOR_PROPERTIES actual;
357
358     if (!ppropInputRequest->cbAlign)
359         ppropInputRequest->cbAlign = 1;
360
361     if (ppropInputRequest->cbBuffer < pACM->pWfOut->nAvgBytesPerSec / 2)
362             ppropInputRequest->cbBuffer = pACM->pWfOut->nAvgBytesPerSec / 2;
363
364     if (!ppropInputRequest->cBuffers)
365         ppropInputRequest->cBuffers = 1;
366
367     return IMemAllocator_SetProperties(pAlloc, ppropInputRequest, &actual);
368 }
369
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
383 };
384
385 HRESULT ACMWrapper_create(IUnknown * pUnkOuter, LPVOID * ppv)
386 {
387     HRESULT hr;
388     ACMWrapperImpl* This;
389
390     TRACE("(%p, %p)\n", pUnkOuter, ppv);
391
392     *ppv = NULL;
393
394     if (pUnkOuter)
395         return CLASS_E_NOAGGREGATION;
396
397     hr = TransformFilter_Construct(&ACMWrapper_Vtbl, sizeof(ACMWrapperImpl), &CLSID_ACMWrapper, &ACMWrapper_FuncsTable, (IBaseFilter**)&This);
398
399     if (FAILED(hr))
400         return hr;
401
402     *ppv = This;
403     This->lasttime_real = This->lasttime_sent = -1;
404
405     return hr;
406 }
407
408 static const IBaseFilterVtbl ACMWrapper_Vtbl =
409 {
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
425 };