quartz: Add a function that can be called when stopping processing data.
[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 = 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     if (hr != S_OK)
214         FIXME("FATALITY: %08x\n", hr);
215
216     return hr;
217 }
218
219 static HRESULT ACMWrapper_ConnectInput(TransformFilterImpl* pTransformFilter, const AM_MEDIA_TYPE * pmt)
220 {
221     ACMWrapperImpl* This = (ACMWrapperImpl*)pTransformFilter;
222     MMRESULT res;
223
224     TRACE("(%p)->(%p)\n", This, pmt);
225
226     /* Check root (GUID w/o FOURCC) */
227     if ((IsEqualIID(&pmt->majortype, &MEDIATYPE_Audio)) &&
228         (!memcmp(((const char *)&pmt->subtype)+4, ((const char *)&MEDIATYPE_Audio)+4, sizeof(GUID)-4)) &&
229         (IsEqualIID(&pmt->formattype, &FORMAT_WaveFormatEx)))
230     {
231         HACMSTREAM drv;
232         AM_MEDIA_TYPE* outpmt = &((OutputPin*)This->tf.ppPins[1])->pin.mtCurrent;
233         This->pWfIn = (LPWAVEFORMATEX)pmt->pbFormat;
234
235         /* HACK */
236         /* TRACE("ALIGN = %d\n", pACMWrapper->pWfIn->nBlockAlign); */
237         /* pACMWrapper->pWfIn->nBlockAlign = 1; */
238
239         /* Set output audio data to PCM */
240         CopyMediaType(outpmt, pmt);
241         outpmt->subtype.Data1 = WAVE_FORMAT_PCM;
242         This->pWfOut = (WAVEFORMATEX*)outpmt->pbFormat;
243         This->pWfOut->wFormatTag = WAVE_FORMAT_PCM;
244         This->pWfOut->wBitsPerSample = 16;
245         This->pWfOut->nBlockAlign = 4;
246         This->pWfOut->cbSize = 0;
247         This->pWfOut->nAvgBytesPerSec = This->pWfOut->nChannels * This->pWfOut->nSamplesPerSec
248                                                 * (This->pWfOut->wBitsPerSample/8);
249
250         if (!(res = acmStreamOpen(&drv, NULL, This->pWfIn, This->pWfOut, NULL, 0, 0, 0)))
251         {
252             This->has = drv;
253
254             /* Update buffer size of media samples in output */
255             ((OutputPin*)This->tf.ppPins[1])->allocProps.cbBuffer = This->pWfOut->nAvgBytesPerSec / 2;
256             TRACE("Connection accepted\n");
257             return S_OK;
258         }
259         else
260             FIXME("acmStreamOpen returned %d\n", res);
261         FreeMediaType(outpmt);
262         TRACE("Unable to find a suitable ACM decompressor\n");
263     }
264
265     TRACE("Connection refused\n");
266     return VFW_E_TYPE_NOT_ACCEPTED;
267 }
268
269 static HRESULT ACMWrapper_Cleanup(TransformFilterImpl* pTransformFilter)
270 {
271     ACMWrapperImpl* This = (ACMWrapperImpl*)pTransformFilter;
272
273     TRACE("(%p)->()\n", This);
274     
275     if (This->has)
276         acmStreamClose(This->has, 0);
277
278     This->has = 0;
279     This->lasttime_real = This->lasttime_sent = -1;
280     
281     return S_OK;
282 }
283
284 static const TransformFuncsTable ACMWrapper_FuncsTable = {
285     NULL,
286     ACMWrapper_ProcessSampleData,
287     NULL,
288     NULL,
289     ACMWrapper_ConnectInput,
290     ACMWrapper_Cleanup
291 };
292
293 HRESULT ACMWrapper_create(IUnknown * pUnkOuter, LPVOID * ppv)
294 {
295     HRESULT hr;
296     ACMWrapperImpl* This;
297
298     TRACE("(%p, %p)\n", pUnkOuter, ppv);
299
300     *ppv = NULL;
301
302     if (pUnkOuter)
303         return CLASS_E_NOAGGREGATION;
304
305     /* Note: This memory is managed by the transform filter once created */
306     This = CoTaskMemAlloc(sizeof(ACMWrapperImpl));
307     ZeroMemory(This, sizeof(ACMWrapperImpl));
308
309     hr = TransformFilter_Create(&(This->tf), &CLSID_ACMWrapper, &ACMWrapper_FuncsTable, NULL, NULL, NULL);
310
311     if (FAILED(hr))
312         return hr;
313
314     *ppv = (LPVOID)This;
315     This->lasttime_real = This->lasttime_sent = -1;
316
317     return hr;
318 }