wined3d: In window mode (!pbuffer) we want both a window drawable format and double...
[wine.git] / dlls / quartz / acmwrapper.c
blob60b8fd1be42562595a2ed2fba75208b8a3e2a9e0
1 /*
2 * ACM Wrapper
4 * Copyright 2005 Christian Costa
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.
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.
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
21 #include "config.h"
23 #include "quartz_private.h"
24 #include "pin.h"
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"
35 #include <assert.h>
37 #include "wine/unicode.h"
38 #include "wine/debug.h"
40 #include "transform.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
44 typedef struct ACMWrapperImpl
46 TransformFilterImpl tf;
47 HACMSTREAM has;
48 LPWAVEFORMATEX pWfIn;
49 LPWAVEFORMATEX pWfOut;
51 LONGLONG lasttime_real;
52 LONGLONG lasttime_sent;
53 } ACMWrapperImpl;
55 static HRESULT ACMWrapper_ProcessSampleData(TransformFilterImpl* pTransformFilter, IMediaSample *pSample)
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;
69 hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
70 if (FAILED(hr))
72 ERR("Cannot get pointer to sample data (%x)\n", hr);
73 return hr;
76 preroll = (IMediaSample_IsPreroll(pSample) == S_OK);
78 IMediaSample_GetTime(pSample, &tStart, &tStop);
79 cbSrcStream = IMediaSample_GetActualDataLength(pSample);
81 /* Prevent discontinuities when codecs 'absorb' data but not give anything back in return */
82 if (IMediaSample_IsDiscontinuity(pSample) == S_OK)
84 This->lasttime_real = tStart;
85 This->lasttime_sent = tStart;
87 else if (This->lasttime_real == tStart)
88 tStart = This->lasttime_sent;
89 else
90 WARN("Discontinuity\n");
92 tMed = tStart;
94 TRACE("Sample data ptr = %p, size = %ld\n", pbSrcStream, (long)cbSrcStream);
96 hr = IPin_ConnectionMediaType(This->tf.ppPins[0], &amt);
97 if (FAILED(hr)) {
98 ERR("Unable to retrieve media type\n");
99 return hr;
102 ash.pbSrc = pbSrcStream;
103 ash.cbSrcLength = cbSrcStream;
105 while(hr == S_OK && ash.cbSrcLength)
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;
112 IMediaSample_SetPreroll(pOutSample, preroll);
114 hr = IMediaSample_SetActualDataLength(pOutSample, 0);
115 assert(hr == S_OK);
117 hr = IMediaSample_GetPointer(pOutSample, &pbDstStream);
118 if (FAILED(hr)) {
119 ERR("Unable to get pointer to buffer (%x)\n", hr);
120 goto error;
122 cbDstStream = IMediaSample_GetSize(pOutSample);
124 ash.cbStruct = sizeof(ash);
125 ash.fdwStatus = 0;
126 ash.dwUser = 0;
127 ash.pbDst = pbDstStream;
128 ash.cbDstLength = cbDstStream;
130 if ((res = acmStreamPrepareHeader(This->has, &ash, 0))) {
131 ERR("Cannot prepare header %d\n", res);
132 goto error;
134 unprepare_header = TRUE;
136 if (IMediaSample_IsDiscontinuity(pSample) == S_OK)
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);
143 else
145 res = acmStreamConvert(This->has, &ash, 0);
146 IMediaSample_SetDiscontinuity(pOutSample, FALSE);
149 if (res)
151 if(res != MMSYSERR_MOREDATA)
152 ERR("Cannot convert data header %d\n", res);
153 goto error;
156 TRACE("used in %u/%u, used out %u/%u\n", ash.cbSrcLengthUsed, ash.cbSrcLength, ash.cbDstLengthUsed, ash.cbDstLength);
158 hr = IMediaSample_SetActualDataLength(pOutSample, ash.cbDstLengthUsed);
159 assert(hr == S_OK);
161 /* Bug in acm codecs? It apparantly uses the input, but doesn't necessarily output immediately kl*/
162 if (!ash.cbSrcLengthUsed)
164 WARN("Sample was skipped? Outputted: %u\n", ash.cbDstLengthUsed);
165 ash.cbSrcLength = 0;
166 goto error;
169 TRACE("Sample start time: %u.%03u\n", (DWORD)(tStart/10000000), (DWORD)((tStart/10000)%1000));
170 if (ash.cbSrcLengthUsed == cbSrcStream)
172 IMediaSample_SetTime(pOutSample, &tStart, &tStop);
173 tStart = tMed = tStop;
175 else if (tStop != tStart)
177 tMed = tStop - tStart;
178 tMed = tStart + tMed * ash.cbSrcLengthUsed / cbSrcStream;
179 IMediaSample_SetTime(pOutSample, &tStart, &tMed);
180 tStart = tMed;
182 else
184 ERR("No valid timestamp found\n");
185 IMediaSample_SetTime(pOutSample, NULL, NULL);
187 TRACE("Sample stop time: %u.%03u\n", (DWORD)(tStart/10000000), (DWORD)((tStart/10000)%1000));
189 hr = OutputPin_SendSample((OutputPin*)This->tf.ppPins[1], pOutSample);
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;
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;
204 if (pOutSample)
205 IMediaSample_Release(pOutSample);
206 pOutSample = NULL;
210 This->lasttime_real = tStop;
211 This->lasttime_sent = tMed;
213 if (hr != S_OK)
214 FIXME("FATALITY: %08x\n", hr);
216 return hr;
219 static HRESULT ACMWrapper_ConnectInput(TransformFilterImpl* pTransformFilter, const AM_MEDIA_TYPE * pmt)
221 ACMWrapperImpl* This = (ACMWrapperImpl*)pTransformFilter;
222 MMRESULT res;
224 TRACE("(%p)->(%p)\n", This, pmt);
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)))
231 HACMSTREAM drv;
232 AM_MEDIA_TYPE* outpmt = &((OutputPin*)This->tf.ppPins[1])->pin.mtCurrent;
233 This->pWfIn = (LPWAVEFORMATEX)pmt->pbFormat;
235 /* HACK */
236 /* TRACE("ALIGN = %d\n", pACMWrapper->pWfIn->nBlockAlign); */
237 /* pACMWrapper->pWfIn->nBlockAlign = 1; */
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);
250 if (!(res = acmStreamOpen(&drv, NULL, This->pWfIn, This->pWfOut, NULL, 0, 0, 0)))
252 This->has = drv;
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;
259 else
260 FIXME("acmStreamOpen returned %d\n", res);
261 FreeMediaType(outpmt);
262 TRACE("Unable to find a suitable ACM decompressor\n");
265 TRACE("Connection refused\n");
266 return VFW_E_TYPE_NOT_ACCEPTED;
269 static HRESULT ACMWrapper_Cleanup(TransformFilterImpl* pTransformFilter)
271 ACMWrapperImpl* This = (ACMWrapperImpl*)pTransformFilter;
273 TRACE("(%p)->()\n", This);
275 if (This->has)
276 acmStreamClose(This->has, 0);
278 This->has = 0;
279 This->lasttime_real = This->lasttime_sent = -1;
281 return S_OK;
284 static const TransformFuncsTable ACMWrapper_FuncsTable = {
285 NULL,
286 ACMWrapper_ProcessSampleData,
287 NULL,
288 NULL,
289 ACMWrapper_ConnectInput,
290 ACMWrapper_Cleanup
293 HRESULT ACMWrapper_create(IUnknown * pUnkOuter, LPVOID * ppv)
295 HRESULT hr;
296 ACMWrapperImpl* This;
298 TRACE("(%p, %p)\n", pUnkOuter, ppv);
300 *ppv = NULL;
302 if (pUnkOuter)
303 return CLASS_E_NOAGGREGATION;
305 /* Note: This memory is managed by the transform filter once created */
306 This = CoTaskMemAlloc(sizeof(ACMWrapperImpl));
307 ZeroMemory(This, sizeof(ACMWrapperImpl));
309 hr = TransformFilter_Create(&(This->tf), &CLSID_ACMWrapper, &ACMWrapper_FuncsTable, NULL, NULL, NULL);
311 if (FAILED(hr))
312 return hr;
314 *ppv = (LPVOID)This;
315 This->lasttime_real = This->lasttime_sent = -1;
317 return hr;