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