strmbase: Implement BaseFilter in strmbase.
[wine/multimedia.git] / dlls / quartz / acmwrapper.c
blob810a25220d038fde3ca0c01ce6ee18ee094e0917
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 WINAPI ACMWrapper_ProcessSampleData(IPin *iface, IMediaSample *pSample)
57 BaseInputPin *pin = (BaseInputPin*) iface;
58 ACMWrapperImpl* This = (ACMWrapperImpl*)pin->pin.pinInfo.pFilter;
59 AM_MEDIA_TYPE amt;
60 IMediaSample* pOutSample = NULL;
61 DWORD cbDstStream, cbSrcStream;
62 LPBYTE pbDstStream;
63 LPBYTE pbSrcStream = NULL;
64 ACMSTREAMHEADER ash;
65 BOOL unprepare_header = FALSE, preroll;
66 MMRESULT res;
67 HRESULT hr;
68 LONGLONG tStart = -1, tStop = -1, tMed;
70 EnterCriticalSection(&This->tf.filter.csFilter);
71 if (This->tf.filter.state == State_Stopped)
73 LeaveCriticalSection(&This->tf.filter.csFilter);
74 return VFW_E_WRONG_STATE;
77 if (pin->end_of_stream || pin->flushing)
79 LeaveCriticalSection(&This->tf.filter.csFilter);
80 return S_FALSE;
83 hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
84 if (FAILED(hr))
86 ERR("Cannot get pointer to sample data (%x)\n", hr);
87 LeaveCriticalSection(&This->tf.filter.csFilter);
88 return hr;
91 preroll = (IMediaSample_IsPreroll(pSample) == S_OK);
93 IMediaSample_GetTime(pSample, &tStart, &tStop);
94 cbSrcStream = IMediaSample_GetActualDataLength(pSample);
96 /* Prevent discontinuities when codecs 'absorb' data but not give anything back in return */
97 if (IMediaSample_IsDiscontinuity(pSample) == S_OK)
99 This->lasttime_real = tStart;
100 This->lasttime_sent = tStart;
102 else if (This->lasttime_real == tStart)
103 tStart = This->lasttime_sent;
104 else
105 WARN("Discontinuity\n");
107 tMed = tStart;
109 TRACE("Sample data ptr = %p, size = %d\n", pbSrcStream, cbSrcStream);
111 hr = IPin_ConnectionMediaType(This->tf.ppPins[0], &amt);
112 if (FAILED(hr))
114 ERR("Unable to retrieve media type\n");
115 LeaveCriticalSection(&This->tf.filter.csFilter);
116 return hr;
119 ash.pbSrc = pbSrcStream;
120 ash.cbSrcLength = cbSrcStream;
122 while(hr == S_OK && ash.cbSrcLength)
124 hr = BaseOutputPinImpl_GetDeliveryBuffer((BaseOutputPin*)This->tf.ppPins[1], &pOutSample, NULL, NULL, 0);
125 if (FAILED(hr))
127 ERR("Unable to get delivery buffer (%x)\n", hr);
128 LeaveCriticalSection(&This->tf.filter.csFilter);
129 return hr;
131 IMediaSample_SetPreroll(pOutSample, preroll);
133 hr = IMediaSample_SetActualDataLength(pOutSample, 0);
134 assert(hr == S_OK);
136 hr = IMediaSample_GetPointer(pOutSample, &pbDstStream);
137 if (FAILED(hr)) {
138 ERR("Unable to get pointer to buffer (%x)\n", hr);
139 goto error;
141 cbDstStream = IMediaSample_GetSize(pOutSample);
143 ash.cbStruct = sizeof(ash);
144 ash.fdwStatus = 0;
145 ash.dwUser = 0;
146 ash.pbDst = pbDstStream;
147 ash.cbDstLength = cbDstStream;
149 if ((res = acmStreamPrepareHeader(This->has, &ash, 0))) {
150 ERR("Cannot prepare header %d\n", res);
151 goto error;
153 unprepare_header = TRUE;
155 if (IMediaSample_IsDiscontinuity(pSample) == S_OK)
157 res = acmStreamConvert(This->has, &ash, ACM_STREAMCONVERTF_START);
158 IMediaSample_SetDiscontinuity(pOutSample, TRUE);
159 /* One sample could be converted to multiple packets */
160 IMediaSample_SetDiscontinuity(pSample, FALSE);
162 else
164 res = acmStreamConvert(This->has, &ash, 0);
165 IMediaSample_SetDiscontinuity(pOutSample, FALSE);
168 if (res)
170 if(res != MMSYSERR_MOREDATA)
171 ERR("Cannot convert data header %d\n", res);
172 goto error;
175 TRACE("used in %u/%u, used out %u/%u\n", ash.cbSrcLengthUsed, ash.cbSrcLength, ash.cbDstLengthUsed, ash.cbDstLength);
177 hr = IMediaSample_SetActualDataLength(pOutSample, ash.cbDstLengthUsed);
178 assert(hr == S_OK);
180 /* Bug in acm codecs? It apparantly uses the input, but doesn't necessarily output immediately kl*/
181 if (!ash.cbSrcLengthUsed)
183 WARN("Sample was skipped? Outputted: %u\n", ash.cbDstLengthUsed);
184 ash.cbSrcLength = 0;
185 goto error;
188 TRACE("Sample start time: %u.%03u\n", (DWORD)(tStart/10000000), (DWORD)((tStart/10000)%1000));
189 if (ash.cbSrcLengthUsed == cbSrcStream)
191 IMediaSample_SetTime(pOutSample, &tStart, &tStop);
192 tStart = tMed = tStop;
194 else if (tStop != tStart)
196 tMed = tStop - tStart;
197 tMed = tStart + tMed * ash.cbSrcLengthUsed / cbSrcStream;
198 IMediaSample_SetTime(pOutSample, &tStart, &tMed);
199 tStart = tMed;
201 else
203 ERR("No valid timestamp found\n");
204 IMediaSample_SetTime(pOutSample, NULL, NULL);
206 TRACE("Sample stop time: %u.%03u\n", (DWORD)(tStart/10000000), (DWORD)((tStart/10000)%1000));
208 LeaveCriticalSection(&This->tf.filter.csFilter);
209 hr = BaseOutputPinImpl_Deliver((BaseOutputPin*)This->tf.ppPins[1], pOutSample);
210 EnterCriticalSection(&This->tf.filter.csFilter);
212 if (hr != S_OK && hr != VFW_E_NOT_CONNECTED) {
213 if (FAILED(hr))
214 ERR("Error sending sample (%x)\n", hr);
215 goto error;
218 error:
219 if (unprepare_header && (res = acmStreamUnprepareHeader(This->has, &ash, 0)))
220 ERR("Cannot unprepare header %d\n", res);
221 unprepare_header = FALSE;
222 ash.pbSrc += ash.cbSrcLengthUsed;
223 ash.cbSrcLength -= ash.cbSrcLengthUsed;
225 if (pOutSample)
226 IMediaSample_Release(pOutSample);
227 pOutSample = NULL;
231 This->lasttime_real = tStop;
232 This->lasttime_sent = tMed;
234 LeaveCriticalSection(&This->tf.filter.csFilter);
235 return hr;
238 static HRESULT ACMWrapper_ConnectInput(BaseInputPin *pin, const AM_MEDIA_TYPE * pmt)
240 ACMWrapperImpl* This = (ACMWrapperImpl *)pin->pin.pinInfo.pFilter;
241 MMRESULT res;
243 TRACE("(%p)->(%p)\n", This, pmt);
245 /* Check root (GUID w/o FOURCC) */
246 if ((IsEqualIID(&pmt->majortype, &MEDIATYPE_Audio)) &&
247 (!memcmp(((const char *)&pmt->subtype)+4, ((const char *)&MEDIATYPE_Audio)+4, sizeof(GUID)-4)) &&
248 (IsEqualIID(&pmt->formattype, &FORMAT_WaveFormatEx)))
250 HACMSTREAM drv;
251 AM_MEDIA_TYPE* outpmt = &This->tf.pmt;
252 FreeMediaType(outpmt);
254 This->pWfIn = (LPWAVEFORMATEX)pmt->pbFormat;
256 /* HACK */
257 /* TRACE("ALIGN = %d\n", pACMWrapper->pWfIn->nBlockAlign); */
258 /* pACMWrapper->pWfIn->nBlockAlign = 1; */
260 /* Set output audio data to PCM */
261 CopyMediaType(outpmt, pmt);
262 outpmt->subtype.Data1 = WAVE_FORMAT_PCM;
263 This->pWfOut = (WAVEFORMATEX*)outpmt->pbFormat;
264 This->pWfOut->wFormatTag = WAVE_FORMAT_PCM;
265 This->pWfOut->wBitsPerSample = 16;
266 This->pWfOut->nBlockAlign = This->pWfOut->wBitsPerSample * This->pWfOut->nChannels / 8;
267 This->pWfOut->cbSize = 0;
268 This->pWfOut->nAvgBytesPerSec = This->pWfOut->nChannels * This->pWfOut->nSamplesPerSec
269 * (This->pWfOut->wBitsPerSample/8);
271 if (!(res = acmStreamOpen(&drv, NULL, This->pWfIn, This->pWfOut, NULL, 0, 0, 0)))
273 This->has = drv;
275 /* Update buffer size of media samples in output */
276 ((BaseOutputPin*)This->tf.ppPins[1])->allocProps.cbBuffer = This->pWfOut->nAvgBytesPerSec / 2;
277 TRACE("Connection accepted\n");
278 return S_OK;
280 else
281 FIXME("acmStreamOpen returned %d\n", res);
282 FreeMediaType(outpmt);
283 TRACE("Unable to find a suitable ACM decompressor\n");
286 TRACE("Connection refused\n");
287 return VFW_E_TYPE_NOT_ACCEPTED;
290 static HRESULT ACMWrapper_Cleanup(BaseInputPin *pin)
292 ACMWrapperImpl *This = (ACMWrapperImpl *)pin->pin.pinInfo.pFilter;
294 TRACE("(%p)->()\n", This);
296 if (This->has)
297 acmStreamClose(This->has, 0);
299 This->has = 0;
300 This->lasttime_real = This->lasttime_sent = -1;
302 return S_OK;
305 static const TransformFuncsTable ACMWrapper_FuncsTable = {
306 NULL,
307 ACMWrapper_ProcessSampleData,
308 NULL,
309 NULL,
310 ACMWrapper_ConnectInput,
311 ACMWrapper_Cleanup
314 HRESULT ACMWrapper_create(IUnknown * pUnkOuter, LPVOID * ppv)
316 HRESULT hr;
317 ACMWrapperImpl* This;
319 TRACE("(%p, %p)\n", pUnkOuter, ppv);
321 *ppv = NULL;
323 if (pUnkOuter)
324 return CLASS_E_NOAGGREGATION;
326 /* Note: This memory is managed by the transform filter once created */
327 This = CoTaskMemAlloc(sizeof(ACMWrapperImpl));
328 ZeroMemory(This, sizeof(ACMWrapperImpl));
330 hr = TransformFilter_Create(&(This->tf), &CLSID_ACMWrapper, &ACMWrapper_FuncsTable);
332 if (FAILED(hr))
333 return hr;
335 *ppv = This;
336 This->lasttime_real = This->lasttime_sent = -1;
338 return hr;