wined3d: IWineD3DBuffer_Unmap() can't fail.
[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     IUnknown *seekthru_unk;
46
47     HACMSTREAM has;
48     LPWAVEFORMATEX pWfIn;
49     LPWAVEFORMATEX pWfOut;
50
51     LONGLONG lasttime_real;
52     LONGLONG lasttime_sent;
53 } ACMWrapperImpl;
54
55 static const IBaseFilterVtbl ACMWrapper_Vtbl;
56
57 static HRESULT WINAPI ACMWrapper_Receive(TransformFilter *tf, IMediaSample *pSample)
58 {
59     ACMWrapperImpl* This = (ACMWrapperImpl*)tf;
60     AM_MEDIA_TYPE amt;
61     IMediaSample* pOutSample = NULL;
62     DWORD cbDstStream, cbSrcStream;
63     LPBYTE pbDstStream;
64     LPBYTE pbSrcStream = NULL;
65     ACMSTREAMHEADER ash;
66     BOOL unprepare_header = FALSE, preroll;
67     MMRESULT res;
68     HRESULT hr;
69     LONGLONG tStart = -1, tStop = -1, tMed;
70
71     EnterCriticalSection(&This->tf.filter.csFilter);
72     hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
73     if (FAILED(hr))
74     {
75         ERR("Cannot get pointer to sample data (%x)\n", hr);
76         LeaveCriticalSection(&This->tf.filter.csFilter);
77         return hr;
78     }
79
80     preroll = (IMediaSample_IsPreroll(pSample) == S_OK);
81
82     IMediaSample_GetTime(pSample, &tStart, &tStop);
83     cbSrcStream = IMediaSample_GetActualDataLength(pSample);
84
85     /* Prevent discontinuities when codecs 'absorb' data but not give anything back in return */
86     if (IMediaSample_IsDiscontinuity(pSample) == S_OK)
87     {
88         This->lasttime_real = tStart;
89         This->lasttime_sent = tStart;
90     }
91     else if (This->lasttime_real == tStart)
92         tStart = This->lasttime_sent;
93     else
94         WARN("Discontinuity\n");
95
96     tMed = tStart;
97
98     TRACE("Sample data ptr = %p, size = %d\n", pbSrcStream, cbSrcStream);
99
100     hr = IPin_ConnectionMediaType(This->tf.ppPins[0], &amt);
101     if (FAILED(hr))
102     {
103         ERR("Unable to retrieve media type\n");
104         LeaveCriticalSection(&This->tf.filter.csFilter);
105         return hr;
106     }
107
108     ash.pbSrc = pbSrcStream;
109     ash.cbSrcLength = cbSrcStream;
110
111     while(hr == S_OK && ash.cbSrcLength)
112     {
113         hr = BaseOutputPinImpl_GetDeliveryBuffer((BaseOutputPin*)This->tf.ppPins[1], &pOutSample, NULL, NULL, 0);
114         if (FAILED(hr))
115         {
116             ERR("Unable to get delivery buffer (%x)\n", hr);
117             LeaveCriticalSection(&This->tf.filter.csFilter);
118             return hr;
119         }
120         IMediaSample_SetPreroll(pOutSample, preroll);
121
122         hr = IMediaSample_SetActualDataLength(pOutSample, 0);
123         assert(hr == S_OK);
124
125         hr = IMediaSample_GetPointer(pOutSample, &pbDstStream);
126         if (FAILED(hr)) {
127             ERR("Unable to get pointer to buffer (%x)\n", hr);
128             goto error;
129         }
130         cbDstStream = IMediaSample_GetSize(pOutSample);
131
132         ash.cbStruct = sizeof(ash);
133         ash.fdwStatus = 0;
134         ash.dwUser = 0;
135         ash.pbDst = pbDstStream;
136         ash.cbDstLength = cbDstStream;
137
138         if ((res = acmStreamPrepareHeader(This->has, &ash, 0))) {
139             ERR("Cannot prepare header %d\n", res);
140             goto error;
141         }
142         unprepare_header = TRUE;
143
144         if (IMediaSample_IsDiscontinuity(pSample) == S_OK)
145         {
146             res = acmStreamConvert(This->has, &ash, ACM_STREAMCONVERTF_START);
147             IMediaSample_SetDiscontinuity(pOutSample, TRUE);
148             /* One sample could be converted to multiple packets */
149             IMediaSample_SetDiscontinuity(pSample, FALSE);
150         }
151         else
152         {
153             res = acmStreamConvert(This->has, &ash, 0);
154             IMediaSample_SetDiscontinuity(pOutSample, FALSE);
155         }
156
157         if (res)
158         {
159             if(res != MMSYSERR_MOREDATA)
160                 ERR("Cannot convert data header %d\n", res);
161             goto error;
162         }
163
164         TRACE("used in %u/%u, used out %u/%u\n", ash.cbSrcLengthUsed, ash.cbSrcLength, ash.cbDstLengthUsed, ash.cbDstLength);
165
166         hr = IMediaSample_SetActualDataLength(pOutSample, ash.cbDstLengthUsed);
167         assert(hr == S_OK);
168
169         /* Bug in acm codecs? It apparantly uses the input, but doesn't necessarily output immediately kl*/
170         if (!ash.cbSrcLengthUsed)
171         {
172             WARN("Sample was skipped? Outputted: %u\n", ash.cbDstLengthUsed);
173             ash.cbSrcLength = 0;
174             goto error;
175         }
176
177         TRACE("Sample start time: %u.%03u\n", (DWORD)(tStart/10000000), (DWORD)((tStart/10000)%1000));
178         if (ash.cbSrcLengthUsed == cbSrcStream)
179         {
180             IMediaSample_SetTime(pOutSample, &tStart, &tStop);
181             tStart = tMed = tStop;
182         }
183         else if (tStop != tStart)
184         {
185             tMed = tStop - tStart;
186             tMed = tStart + tMed * ash.cbSrcLengthUsed / cbSrcStream;
187             IMediaSample_SetTime(pOutSample, &tStart, &tMed);
188             tStart = tMed;
189         }
190         else
191         {
192             ERR("No valid timestamp found\n");
193             IMediaSample_SetTime(pOutSample, NULL, NULL);
194         }
195         TRACE("Sample stop time: %u.%03u\n", (DWORD)(tStart/10000000), (DWORD)((tStart/10000)%1000));
196
197         LeaveCriticalSection(&This->tf.filter.csFilter);
198         hr = BaseOutputPinImpl_Deliver((BaseOutputPin*)This->tf.ppPins[1], pOutSample);
199         EnterCriticalSection(&This->tf.filter.csFilter);
200
201         if (hr != S_OK && hr != VFW_E_NOT_CONNECTED) {
202             if (FAILED(hr))
203                 ERR("Error sending sample (%x)\n", hr);
204             goto error;
205         }
206
207 error:
208         if (unprepare_header && (res = acmStreamUnprepareHeader(This->has, &ash, 0)))
209             ERR("Cannot unprepare header %d\n", res);
210         unprepare_header = FALSE;
211         ash.pbSrc += ash.cbSrcLengthUsed;
212         ash.cbSrcLength -= ash.cbSrcLengthUsed;
213
214         if (pOutSample)
215             IMediaSample_Release(pOutSample);
216         pOutSample = NULL;
217
218     }
219
220     This->lasttime_real = tStop;
221     This->lasttime_sent = tMed;
222
223     LeaveCriticalSection(&This->tf.filter.csFilter);
224     return hr;
225 }
226
227 static HRESULT WINAPI ACMWrapper_SetMediaType(TransformFilter *tf, PIN_DIRECTION dir, const AM_MEDIA_TYPE * pmt)
228 {
229     ACMWrapperImpl* This = (ACMWrapperImpl *)tf;
230     MMRESULT res;
231
232     TRACE("(%p)->(%i %p)\n", This, dir, pmt);
233
234     if (dir != PINDIR_INPUT)
235         return S_OK;
236
237     /* Check root (GUID w/o FOURCC) */
238     if ((IsEqualIID(&pmt->majortype, &MEDIATYPE_Audio)) &&
239         (!memcmp(((const char *)&pmt->subtype)+4, ((const char *)&MEDIATYPE_Audio)+4, sizeof(GUID)-4)) &&
240         (IsEqualIID(&pmt->formattype, &FORMAT_WaveFormatEx)))
241     {
242         HACMSTREAM drv;
243         AM_MEDIA_TYPE* outpmt = &This->tf.pmt;
244         FreeMediaType(outpmt);
245
246         This->pWfIn = (LPWAVEFORMATEX)pmt->pbFormat;
247
248         /* HACK */
249         /* TRACE("ALIGN = %d\n", pACMWrapper->pWfIn->nBlockAlign); */
250         /* pACMWrapper->pWfIn->nBlockAlign = 1; */
251
252         /* Set output audio data to PCM */
253         CopyMediaType(outpmt, pmt);
254         outpmt->subtype.Data1 = WAVE_FORMAT_PCM;
255         This->pWfOut = (WAVEFORMATEX*)outpmt->pbFormat;
256         This->pWfOut->wFormatTag = WAVE_FORMAT_PCM;
257         This->pWfOut->wBitsPerSample = 16;
258         This->pWfOut->nBlockAlign = This->pWfOut->wBitsPerSample * This->pWfOut->nChannels / 8;
259         This->pWfOut->cbSize = 0;
260         This->pWfOut->nAvgBytesPerSec = This->pWfOut->nChannels * This->pWfOut->nSamplesPerSec
261                                                 * (This->pWfOut->wBitsPerSample/8);
262
263         if (!(res = acmStreamOpen(&drv, NULL, This->pWfIn, This->pWfOut, NULL, 0, 0, 0)))
264         {
265             This->has = drv;
266
267             TRACE("Connection accepted\n");
268             return S_OK;
269         }
270         else
271             FIXME("acmStreamOpen returned %d\n", res);
272         FreeMediaType(outpmt);
273         TRACE("Unable to find a suitable ACM decompressor\n");
274     }
275
276     TRACE("Connection refused\n");
277     return VFW_E_TYPE_NOT_ACCEPTED;
278 }
279
280 static HRESULT WINAPI ACMWrapper_CompleteConnect(TransformFilter *tf, PIN_DIRECTION dir, IPin *pin)
281 {
282     ACMWrapperImpl* This = (ACMWrapperImpl *)tf;
283     MMRESULT res;
284     HACMSTREAM drv;
285
286     TRACE("(%p)\n", This);
287
288     if (dir != PINDIR_INPUT)
289         return S_OK;
290
291     if (!(res = acmStreamOpen(&drv, NULL, This->pWfIn, This->pWfOut, NULL, 0, 0, 0)))
292     {
293         This->has = drv;
294
295         TRACE("Connection accepted\n");
296         return S_OK;
297     }
298
299     FIXME("acmStreamOpen returned %d\n", res);
300     TRACE("Unable to find a suitable ACM decompressor\n");
301     return VFW_E_TYPE_NOT_ACCEPTED;
302 }
303
304 static HRESULT WINAPI ACMWrapper_BreakConnect(TransformFilter *tf, PIN_DIRECTION dir)
305 {
306     ACMWrapperImpl *This = (ACMWrapperImpl *)tf;
307
308     TRACE("(%p)->(%i)\n", This,dir);
309
310     if (dir == PINDIR_INPUT)
311     {
312         if (This->has)
313             acmStreamClose(This->has, 0);
314
315         This->has = 0;
316         This->lasttime_real = This->lasttime_sent = -1;
317     }
318
319     return S_OK;
320 }
321
322 static HRESULT WINAPI ACMWrapper_DecideBufferSize(TransformFilter *tf, IMemAllocator *pAlloc, ALLOCATOR_PROPERTIES *ppropInputRequest)
323 {
324     ACMWrapperImpl *pACM = (ACMWrapperImpl*)tf;
325     ALLOCATOR_PROPERTIES actual;
326
327     if (!ppropInputRequest->cbAlign)
328         ppropInputRequest->cbAlign = 1;
329
330     if (ppropInputRequest->cbBuffer < pACM->pWfOut->nAvgBytesPerSec / 2)
331             ppropInputRequest->cbBuffer = pACM->pWfOut->nAvgBytesPerSec / 2;
332
333     if (!ppropInputRequest->cBuffers)
334         ppropInputRequest->cBuffers = 1;
335
336     return IMemAllocator_SetProperties(pAlloc, ppropInputRequest, &actual);
337 }
338
339 static const TransformFilterFuncTable ACMWrapper_FuncsTable = {
340     ACMWrapper_DecideBufferSize,
341     NULL,
342     ACMWrapper_Receive,
343     NULL,
344     NULL,
345     ACMWrapper_SetMediaType,
346     ACMWrapper_CompleteConnect,
347     ACMWrapper_BreakConnect,
348     NULL,
349     NULL,
350     NULL,
351     NULL
352 };
353
354 HRESULT ACMWrapper_create(IUnknown * pUnkOuter, LPVOID * ppv)
355 {
356     HRESULT hr;
357     ACMWrapperImpl* This;
358
359     TRACE("(%p, %p)\n", pUnkOuter, ppv);
360
361     *ppv = NULL;
362
363     if (pUnkOuter)
364         return CLASS_E_NOAGGREGATION;
365
366     hr = TransformFilter_Construct(&ACMWrapper_Vtbl, sizeof(ACMWrapperImpl), &CLSID_ACMWrapper, &ACMWrapper_FuncsTable, (IBaseFilter**)&This);
367
368     if (FAILED(hr))
369         return hr;
370     else
371     {
372         ISeekingPassThru *passthru;
373         hr = CoCreateInstance(&CLSID_SeekingPassThru, (IUnknown*)This, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void**)&This->seekthru_unk);
374         IUnknown_QueryInterface(This->seekthru_unk, &IID_ISeekingPassThru, (void**)&passthru);
375         ISeekingPassThru_Init(passthru, FALSE, (IPin*)This->tf.ppPins[0]);
376         ISeekingPassThru_Release(passthru);
377     }
378
379     *ppv = This;
380     This->lasttime_real = This->lasttime_sent = -1;
381
382     return hr;
383 }
384
385 HRESULT WINAPI ACMWrapper_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
386 {
387     HRESULT hr;
388     ACMWrapperImpl *This = (ACMWrapperImpl *)iface;
389     TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
390
391     if (IsEqualIID(riid, &IID_IMediaSeeking))
392         return IUnknown_QueryInterface(This->seekthru_unk, riid, ppv);
393
394     hr = TransformFilterImpl_QueryInterface(iface, riid, ppv);
395
396     return hr;
397 }
398
399
400 static const IBaseFilterVtbl ACMWrapper_Vtbl =
401 {
402     ACMWrapper_QueryInterface,
403     BaseFilterImpl_AddRef,
404     TransformFilterImpl_Release,
405     BaseFilterImpl_GetClassID,
406     TransformFilterImpl_Stop,
407     TransformFilterImpl_Pause,
408     TransformFilterImpl_Run,
409     BaseFilterImpl_GetState,
410     BaseFilterImpl_SetSyncSource,
411     BaseFilterImpl_GetSyncSource,
412     BaseFilterImpl_EnumPins,
413     TransformFilterImpl_FindPin,
414     BaseFilterImpl_QueryFilterInfo,
415     BaseFilterImpl_JoinFilterGraph,
416     BaseFilterImpl_QueryVendorInfo
417 };