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
23 #include "quartz_private.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;
49 LPWAVEFORMATEX pWfOut;
50 BOOL reinit_codec; /* FIXME: Should use sync points instead */
53 static HRESULT ACMWrapper_ProcessSampleData(TransformFilterImpl* pTransformFilter, IMediaSample *pSample)
55 ACMWrapperImpl* This = (ACMWrapperImpl*)pTransformFilter;
57 IMediaSample* pOutSample = NULL;
58 DWORD cbDstStream, cbSrcStream;
60 LPBYTE pbSrcStream = NULL;
62 BOOL unprepare_header = FALSE;
65 LONGLONG tStart = -1, tStop = -1, tMed;
67 hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
70 ERR("Cannot get pointer to sample data (%x)\n", hr);
74 IMediaSample_GetTime(pSample, &tStart, &tStop);
75 cbSrcStream = IMediaSample_GetActualDataLength(pSample);
77 TRACE("Sample data ptr = %p, size = %ld\n", pbSrcStream, (long)cbSrcStream);
79 hr = IPin_ConnectionMediaType(This->tf.ppPins[0], &amt);
81 ERR("Unable to retrieve media type\n");
85 ash.pbSrc = pbSrcStream;
86 ash.cbSrcLength = cbSrcStream;
88 while(hr == S_OK && ash.cbSrcLength)
90 hr = OutputPin_GetDeliveryBuffer((OutputPin*)This->tf.ppPins[1], &pOutSample, NULL, NULL, 0);
92 ERR("Unable to get delivery buffer (%x)\n", hr);
96 hr = IMediaSample_SetActualDataLength(pOutSample, 0);
99 hr = IMediaSample_GetPointer(pOutSample, &pbDstStream);
101 ERR("Unable to get pointer to buffer (%x)\n", hr);
104 cbDstStream = IMediaSample_GetSize(pOutSample);
106 ash.cbStruct = sizeof(ash);
109 ash.pbDst = pbDstStream;
110 ash.cbDstLength = cbDstStream;
112 if ((res = acmStreamPrepareHeader(This->has, &ash, 0))) {
113 ERR("Cannot prepare header %d\n", res);
116 unprepare_header = TRUE;
118 if ((res = acmStreamConvert(This->has, &ash, This->reinit_codec ? ACM_STREAMCONVERTF_START : 0))) {
119 if(res != MMSYSERR_MOREDATA)
120 ERR("Cannot convert data header %d\n", res);
123 This->reinit_codec = FALSE;
125 TRACE("used in %u/%u, used out %u/%u\n", ash.cbSrcLengthUsed, ash.cbSrcLength, ash.cbDstLengthUsed, ash.cbDstLength);
127 hr = IMediaSample_SetActualDataLength(pOutSample, ash.cbDstLengthUsed);
130 assert(ash.cbSrcLengthUsed);
131 TRACE("Sample start time: %lld.%03lld\n", (tStart/10000000), (tStart/10000)%1000);
132 if (ash.cbSrcLengthUsed == cbSrcStream)
134 IMediaSample_SetTime(pOutSample, &tStart, &tStop);
137 else if (tStop != tStart)
139 tMed = tStop - tStart;
140 tMed = tStart + tMed * ash.cbSrcLengthUsed / cbSrcStream;
141 IMediaSample_SetTime(pOutSample, &tStart, &tMed);
146 ERR("No valid timestamp found\n");
147 IMediaSample_SetTime(pOutSample, NULL, NULL);
149 TRACE("Sample stop time: %lld.%03lld\n", (tStart/10000000), (tStart/10000)%1000);
151 hr = OutputPin_SendSample((OutputPin*)This->tf.ppPins[1], pOutSample);
152 if (hr != S_OK && hr != VFW_E_NOT_CONNECTED) {
154 ERR("Error sending sample (%x)\n", hr);
159 if (unprepare_header && (res = acmStreamUnprepareHeader(This->has, &ash, 0)))
160 ERR("Cannot unprepare header %d\n", res);
161 unprepare_header = FALSE;
162 ash.pbSrc += ash.cbSrcLengthUsed;
163 ash.cbSrcLength -= ash.cbSrcLengthUsed;
166 IMediaSample_Release(pOutSample);
173 static HRESULT ACMWrapper_ConnectInput(TransformFilterImpl* pTransformFilter, const AM_MEDIA_TYPE * pmt)
175 ACMWrapperImpl* This = (ACMWrapperImpl*)pTransformFilter;
178 TRACE("(%p)->(%p)\n", This, pmt);
180 /* Check root (GUID w/o FOURCC) */
181 if ((IsEqualIID(&pmt->majortype, &MEDIATYPE_Audio)) &&
182 (!memcmp(((const char *)&pmt->subtype)+4, ((const char *)&MEDIATYPE_Audio)+4, sizeof(GUID)-4)) &&
183 (IsEqualIID(&pmt->formattype, &FORMAT_WaveFormatEx)))
186 AM_MEDIA_TYPE* outpmt = &((OutputPin*)This->tf.ppPins[1])->pin.mtCurrent;
187 This->pWfIn = (LPWAVEFORMATEX)pmt->pbFormat;
190 /* TRACE("ALIGN = %d\n", pACMWrapper->pWfIn->nBlockAlign); */
191 /* pACMWrapper->pWfIn->nBlockAlign = 1; */
193 /* Set output audio data to PCM */
194 CopyMediaType(outpmt, pmt);
195 outpmt->subtype.Data1 = WAVE_FORMAT_PCM;
196 This->pWfOut = (WAVEFORMATEX*)outpmt->pbFormat;
197 This->pWfOut->wFormatTag = WAVE_FORMAT_PCM;
198 This->pWfOut->wBitsPerSample = 16;
199 This->pWfOut->nBlockAlign = 4;
200 This->pWfOut->cbSize = 0;
201 This->pWfOut->nAvgBytesPerSec = This->pWfOut->nChannels * This->pWfOut->nSamplesPerSec
202 * (This->pWfOut->wBitsPerSample/8);
204 if (!(res = acmStreamOpen(&drv, NULL, This->pWfIn, This->pWfOut, NULL, 0, 0, 0)))
208 /* Update buffer size of media samples in output */
209 ((OutputPin*)This->tf.ppPins[1])->allocProps.cbBuffer = This->pWfOut->nAvgBytesPerSec / 2;
210 TRACE("Connection accepted\n");
214 FIXME("acmStreamOpen returned %d\n", res);
215 FreeMediaType(outpmt);
216 TRACE("Unable to find a suitable ACM decompressor\n");
219 TRACE("Connection refused\n");
220 return VFW_E_TYPE_NOT_ACCEPTED;
223 static HRESULT ACMWrapper_Cleanup(TransformFilterImpl* pTransformFilter)
225 ACMWrapperImpl* This = (ACMWrapperImpl*)pTransformFilter;
227 TRACE("(%p)->()\n", This);
230 acmStreamClose(This->has, 0);
237 static const TransformFuncsTable ACMWrapper_FuncsTable = {
239 ACMWrapper_ProcessSampleData,
242 ACMWrapper_ConnectInput,
246 HRESULT ACMWrapper_create(IUnknown * pUnkOuter, LPVOID * ppv)
249 ACMWrapperImpl* This;
251 TRACE("(%p, %p)\n", pUnkOuter, ppv);
256 return CLASS_E_NOAGGREGATION;
258 /* Note: This memory is managed by the transform filter once created */
259 This = CoTaskMemAlloc(sizeof(ACMWrapperImpl));
260 ZeroMemory(This, sizeof(ACMWrapperImpl));
262 This->reinit_codec = TRUE;
264 hr = TransformFilter_Create(&(This->tf), &CLSID_ACMWrapper, &ACMWrapper_FuncsTable);