push 7f8c39dca3a5819e8ef115eebf7abed537de3a22
[wine/hacks.git] / dlls / quartz / acmwrapper.c
blobd87793058571dbcaf596f1964bf628202d437836
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 return hr;
216 static HRESULT ACMWrapper_ConnectInput(TransformFilterImpl* pTransformFilter, const AM_MEDIA_TYPE * pmt)
218 ACMWrapperImpl* This = (ACMWrapperImpl*)pTransformFilter;
219 MMRESULT res;
221 TRACE("(%p)->(%p)\n", This, pmt);
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)))
228 HACMSTREAM drv;
229 AM_MEDIA_TYPE* outpmt = &((OutputPin*)This->tf.ppPins[1])->pin.mtCurrent;
230 This->pWfIn = (LPWAVEFORMATEX)pmt->pbFormat;
232 /* HACK */
233 /* TRACE("ALIGN = %d\n", pACMWrapper->pWfIn->nBlockAlign); */
234 /* pACMWrapper->pWfIn->nBlockAlign = 1; */
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);
247 if (!(res = acmStreamOpen(&drv, NULL, This->pWfIn, This->pWfOut, NULL, 0, 0, 0)))
249 This->has = drv;
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;
256 else
257 FIXME("acmStreamOpen returned %d\n", res);
258 FreeMediaType(outpmt);
259 TRACE("Unable to find a suitable ACM decompressor\n");
262 TRACE("Connection refused\n");
263 return VFW_E_TYPE_NOT_ACCEPTED;
266 static HRESULT ACMWrapper_Cleanup(TransformFilterImpl* pTransformFilter)
268 ACMWrapperImpl* This = (ACMWrapperImpl*)pTransformFilter;
270 TRACE("(%p)->()\n", This);
272 if (This->has)
273 acmStreamClose(This->has, 0);
275 This->has = 0;
276 This->lasttime_real = This->lasttime_sent = -1;
278 return S_OK;
281 static const TransformFuncsTable ACMWrapper_FuncsTable = {
282 NULL,
283 ACMWrapper_ProcessSampleData,
284 NULL,
285 NULL,
286 ACMWrapper_ConnectInput,
287 ACMWrapper_Cleanup
290 HRESULT ACMWrapper_create(IUnknown * pUnkOuter, LPVOID * ppv)
292 HRESULT hr;
293 ACMWrapperImpl* This;
295 TRACE("(%p, %p)\n", pUnkOuter, ppv);
297 *ppv = NULL;
299 if (pUnkOuter)
300 return CLASS_E_NOAGGREGATION;
302 /* Note: This memory is managed by the transform filter once created */
303 This = CoTaskMemAlloc(sizeof(ACMWrapperImpl));
304 ZeroMemory(This, sizeof(ACMWrapperImpl));
306 hr = TransformFilter_Create(&(This->tf), &CLSID_ACMWrapper, &ACMWrapper_FuncsTable, NULL, NULL, NULL);
308 if (FAILED(hr))
309 return hr;
311 *ppv = (LPVOID)This;
312 This->lasttime_real = This->lasttime_sent = -1;
314 return hr;