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