quartz: Fix deadlock with transform filter.
[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 #include "transform.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
43
44 typedef struct ACMWrapperImpl
45 {
46     TransformFilterImpl tf;
47     HACMSTREAM has;
48     LPWAVEFORMATEX pWfIn;
49     LPWAVEFORMATEX pWfOut;
50
51     LONGLONG lasttime_real;
52     LONGLONG lasttime_sent;
53 } ACMWrapperImpl;
54
55 static HRESULT ACMWrapper_ProcessSampleData(TransformFilterImpl* pTransformFilter, IMediaSample *pSample)
56 {
57     ACMWrapperImpl* This = (ACMWrapperImpl*)pTransformFilter;
58     AM_MEDIA_TYPE amt;
59     IMediaSample* pOutSample = NULL;
60     DWORD cbDstStream, cbSrcStream;
61     LPBYTE pbDstStream;
62     LPBYTE pbSrcStream = NULL;
63     ACMSTREAMHEADER ash;
64     BOOL unprepare_header = FALSE, preroll;
65     MMRESULT res;
66     HRESULT hr;
67     LONGLONG tStart = -1, tStop = -1, tMed;
68     InputPin *pin = (InputPin *)pTransformFilter->ppPins[0];
69
70     EnterCriticalSection(&pTransformFilter->csFilter);
71     if (pTransformFilter->state == State_Stopped)
72     {
73         LeaveCriticalSection(&pTransformFilter->csFilter);
74         return VFW_E_WRONG_STATE;
75     }
76
77     if (pin->end_of_stream || pin->flushing)
78     {
79         LeaveCriticalSection(&pTransformFilter->csFilter);
80         return S_FALSE;
81     }
82
83     hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
84     if (FAILED(hr))
85     {
86         ERR("Cannot get pointer to sample data (%x)\n", hr);
87         LeaveCriticalSection(&pTransformFilter->csFilter);
88         return hr;
89     }
90
91     preroll = (IMediaSample_IsPreroll(pSample) == S_OK);
92
93     IMediaSample_GetTime(pSample, &tStart, &tStop);
94     cbSrcStream = IMediaSample_GetActualDataLength(pSample);
95
96     /* Prevent discontinuities when codecs 'absorb' data but not give anything back in return */
97     if (IMediaSample_IsDiscontinuity(pSample) == S_OK)
98     {
99         This->lasttime_real = tStart;
100         This->lasttime_sent = tStart;
101     }
102     else if (This->lasttime_real == tStart)
103         tStart = This->lasttime_sent;
104     else
105         WARN("Discontinuity\n");
106
107     tMed = tStart;
108
109     TRACE("Sample data ptr = %p, size = %ld\n", pbSrcStream, (long)cbSrcStream);
110
111     hr = IPin_ConnectionMediaType(This->tf.ppPins[0], &amt);
112     if (FAILED(hr))
113     {
114         ERR("Unable to retrieve media type\n");
115         LeaveCriticalSection(&pTransformFilter->csFilter);
116         return hr;
117     }
118
119     ash.pbSrc = pbSrcStream;
120     ash.cbSrcLength = cbSrcStream;
121
122     while(hr == S_OK && ash.cbSrcLength)
123     {
124         hr = OutputPin_GetDeliveryBuffer((OutputPin*)This->tf.ppPins[1], &pOutSample, NULL, NULL, 0);
125         if (FAILED(hr))
126         {
127             ERR("Unable to get delivery buffer (%x)\n", hr);
128             LeaveCriticalSection(&pTransformFilter->csFilter);
129             return hr;
130         }
131         IMediaSample_SetPreroll(pOutSample, preroll);
132
133         hr = IMediaSample_SetActualDataLength(pOutSample, 0);
134         assert(hr == S_OK);
135
136         hr = IMediaSample_GetPointer(pOutSample, &pbDstStream);
137         if (FAILED(hr)) {
138             ERR("Unable to get pointer to buffer (%x)\n", hr);
139             goto error;
140         }
141         cbDstStream = IMediaSample_GetSize(pOutSample);
142
143         ash.cbStruct = sizeof(ash);
144         ash.fdwStatus = 0;
145         ash.dwUser = 0;
146         ash.pbDst = pbDstStream;
147         ash.cbDstLength = cbDstStream;
148
149         if ((res = acmStreamPrepareHeader(This->has, &ash, 0))) {
150             ERR("Cannot prepare header %d\n", res);
151             goto error;
152         }
153         unprepare_header = TRUE;
154
155         if (IMediaSample_IsDiscontinuity(pSample) == S_OK)
156         {
157             res = acmStreamConvert(This->has, &ash, ACM_STREAMCONVERTF_START);
158             IMediaSample_SetDiscontinuity(pOutSample, TRUE);
159             /* One sample could be converted to multiple packets */
160             IMediaSample_SetDiscontinuity(pSample, FALSE);
161         }
162         else
163         {
164             res = acmStreamConvert(This->has, &ash, 0);
165             IMediaSample_SetDiscontinuity(pOutSample, FALSE);
166         }
167
168         if (res)
169         {
170             if(res != MMSYSERR_MOREDATA)
171                 ERR("Cannot convert data header %d\n", res);
172             goto error;
173         }
174
175         TRACE("used in %u/%u, used out %u/%u\n", ash.cbSrcLengthUsed, ash.cbSrcLength, ash.cbDstLengthUsed, ash.cbDstLength);
176
177         hr = IMediaSample_SetActualDataLength(pOutSample, ash.cbDstLengthUsed);
178         assert(hr == S_OK);
179
180         /* Bug in acm codecs? It apparantly uses the input, but doesn't necessarily output immediately kl*/
181         if (!ash.cbSrcLengthUsed)
182         {
183             WARN("Sample was skipped? Outputted: %u\n", ash.cbDstLengthUsed);
184             ash.cbSrcLength = 0;
185             goto error;
186         }
187
188         TRACE("Sample start time: %u.%03u\n", (DWORD)(tStart/10000000), (DWORD)((tStart/10000)%1000));
189         if (ash.cbSrcLengthUsed == cbSrcStream)
190         {
191             IMediaSample_SetTime(pOutSample, &tStart, &tStop);
192             tStart = tMed = tStop;
193         }
194         else if (tStop != tStart)
195         {
196             tMed = tStop - tStart;
197             tMed = tStart + tMed * ash.cbSrcLengthUsed / cbSrcStream;
198             IMediaSample_SetTime(pOutSample, &tStart, &tMed);
199             tStart = tMed;
200         }
201         else
202         {
203             ERR("No valid timestamp found\n");
204             IMediaSample_SetTime(pOutSample, NULL, NULL);
205         }
206         TRACE("Sample stop time: %u.%03u\n", (DWORD)(tStart/10000000), (DWORD)((tStart/10000)%1000));
207
208         LeaveCriticalSection(&This->tf.csFilter);
209         hr = OutputPin_SendSample((OutputPin*)This->tf.ppPins[1], pOutSample);
210         EnterCriticalSection(&This->tf.csFilter);
211
212         if (hr != S_OK && hr != VFW_E_NOT_CONNECTED) {
213             if (FAILED(hr))
214                 ERR("Error sending sample (%x)\n", hr);
215             goto error;
216         }
217
218 error:
219         if (unprepare_header && (res = acmStreamUnprepareHeader(This->has, &ash, 0)))
220             ERR("Cannot unprepare header %d\n", res);
221         unprepare_header = FALSE;
222         ash.pbSrc += ash.cbSrcLengthUsed;
223         ash.cbSrcLength -= ash.cbSrcLengthUsed;
224
225         if (pOutSample)
226             IMediaSample_Release(pOutSample);
227         pOutSample = NULL;
228
229     }
230
231     This->lasttime_real = tStop;
232     This->lasttime_sent = tMed;
233
234     LeaveCriticalSection(&pTransformFilter->csFilter);
235     return hr;
236 }
237
238 static HRESULT ACMWrapper_ConnectInput(TransformFilterImpl* pTransformFilter, const AM_MEDIA_TYPE * pmt)
239 {
240     ACMWrapperImpl* This = (ACMWrapperImpl*)pTransformFilter;
241     MMRESULT res;
242
243     TRACE("(%p)->(%p)\n", This, pmt);
244
245     /* Check root (GUID w/o FOURCC) */
246     if ((IsEqualIID(&pmt->majortype, &MEDIATYPE_Audio)) &&
247         (!memcmp(((const char *)&pmt->subtype)+4, ((const char *)&MEDIATYPE_Audio)+4, sizeof(GUID)-4)) &&
248         (IsEqualIID(&pmt->formattype, &FORMAT_WaveFormatEx)))
249     {
250         HACMSTREAM drv;
251         AM_MEDIA_TYPE* outpmt = &((OutputPin*)This->tf.ppPins[1])->pin.mtCurrent;
252         This->pWfIn = (LPWAVEFORMATEX)pmt->pbFormat;
253
254         /* HACK */
255         /* TRACE("ALIGN = %d\n", pACMWrapper->pWfIn->nBlockAlign); */
256         /* pACMWrapper->pWfIn->nBlockAlign = 1; */
257
258         /* Set output audio data to PCM */
259         CopyMediaType(outpmt, pmt);
260         outpmt->subtype.Data1 = WAVE_FORMAT_PCM;
261         This->pWfOut = (WAVEFORMATEX*)outpmt->pbFormat;
262         This->pWfOut->wFormatTag = WAVE_FORMAT_PCM;
263         This->pWfOut->wBitsPerSample = 16;
264         This->pWfOut->nBlockAlign = 4;
265         This->pWfOut->cbSize = 0;
266         This->pWfOut->nAvgBytesPerSec = This->pWfOut->nChannels * This->pWfOut->nSamplesPerSec
267                                                 * (This->pWfOut->wBitsPerSample/8);
268
269         if (!(res = acmStreamOpen(&drv, NULL, This->pWfIn, This->pWfOut, NULL, 0, 0, 0)))
270         {
271             This->has = drv;
272
273             /* Update buffer size of media samples in output */
274             ((OutputPin*)This->tf.ppPins[1])->allocProps.cbBuffer = This->pWfOut->nAvgBytesPerSec / 2;
275             TRACE("Connection accepted\n");
276             return S_OK;
277         }
278         else
279             FIXME("acmStreamOpen returned %d\n", res);
280         FreeMediaType(outpmt);
281         TRACE("Unable to find a suitable ACM decompressor\n");
282     }
283
284     TRACE("Connection refused\n");
285     return VFW_E_TYPE_NOT_ACCEPTED;
286 }
287
288 static HRESULT ACMWrapper_Cleanup(TransformFilterImpl* pTransformFilter)
289 {
290     ACMWrapperImpl* This = (ACMWrapperImpl*)pTransformFilter;
291
292     TRACE("(%p)->()\n", This);
293     
294     if (This->has)
295         acmStreamClose(This->has, 0);
296
297     This->has = 0;
298     This->lasttime_real = This->lasttime_sent = -1;
299
300     return S_OK;
301 }
302
303 static const TransformFuncsTable ACMWrapper_FuncsTable = {
304     NULL,
305     ACMWrapper_ProcessSampleData,
306     NULL,
307     NULL,
308     ACMWrapper_ConnectInput,
309     ACMWrapper_Cleanup
310 };
311
312 HRESULT ACMWrapper_create(IUnknown * pUnkOuter, LPVOID * ppv)
313 {
314     HRESULT hr;
315     ACMWrapperImpl* This;
316
317     TRACE("(%p, %p)\n", pUnkOuter, ppv);
318
319     *ppv = NULL;
320
321     if (pUnkOuter)
322         return CLASS_E_NOAGGREGATION;
323
324     /* Note: This memory is managed by the transform filter once created */
325     This = CoTaskMemAlloc(sizeof(ACMWrapperImpl));
326     ZeroMemory(This, sizeof(ACMWrapperImpl));
327
328     hr = TransformFilter_Create(&(This->tf), &CLSID_ACMWrapper, &ACMWrapper_FuncsTable, NULL, NULL, NULL);
329
330     if (FAILED(hr))
331         return hr;
332
333     *ppv = (LPVOID)This;
334     This->lasttime_real = This->lasttime_sent = -1;
335
336     return hr;
337 }