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