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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "quartz_private.h"
24 #include "control_private.h"
39 #include "wine/unicode.h"
40 #include "wine/debug.h"
42 #include "transform.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
46 /* FIXME: Improve buffers management */
47 #define OUTPUT_BUFFER_SIZE 15000
48 #define INPUT_BUFFER_SIZE 4096
50 typedef struct ACMWrapperImpl
52 TransformFilterImpl tf;
55 LPWAVEFORMATEX pWfOut;
56 BYTE buffer[INPUT_BUFFER_SIZE];
59 BOOL reinit_codec; /* FIXME: Should use sync points instead */
62 static HRESULT ACMWrapper_ProcessSampleData(TransformFilterImpl* pTransformFilter, LPBYTE data, DWORD size)
64 ACMWrapperImpl* This = (ACMWrapperImpl*)pTransformFilter;
67 IMediaSample* pSample = NULL;
73 BOOL unprepare_header = FALSE;
76 TRACE("(%p)->(%p,%ld)\n", This, data, size);
78 hr = IPin_ConnectionMediaType(This->tf.ppPins[0], &amt);
80 ERR("Unable to retrieve media type\n");
86 DWORD rem_buf = This->max_size - This->current_size;
87 DWORD rem_smp = size - offset;
88 DWORD copy_size = min(rem_buf, rem_smp);
90 memcpy(This->buffer + This->current_size, data + offset, copy_size);
91 This->current_size += copy_size;
96 if (This->current_size < This->max_size)
99 hr = OutputPin_GetDeliveryBuffer((OutputPin*)This->tf.ppPins[1], &pSample, NULL, NULL, 0);
101 ERR("Unable to get delivery buffer (%lx)\n", hr);
105 hr = IMediaSample_SetActualDataLength(pSample, 0);
108 hr = IMediaSample_GetPointer(pSample, &pbDstStream);
110 ERR("Unable to get pointer to buffer (%lx)\n", hr);
113 cbDstStream = IMediaSample_GetSize(pSample);
115 ash.cbStruct = sizeof(ash);
118 ash.pbSrc = This->buffer;
119 ash.cbSrcLength = This->current_size;
120 ash.pbDst = pbDstStream;
121 ash.cbDstLength = cbDstStream;
123 if ((res = acmStreamPrepareHeader(This->has, &ash, 0))) {
124 ERR("Cannot prepare header %d\n", res);
128 unprepare_header = TRUE;
130 if ((res = acmStreamConvert(This->has, &ash, This->reinit_codec ? ACM_STREAMCONVERTF_START : 0))) {
131 ERR("Cannot convert data header %d\n", res);
134 This->reinit_codec = FALSE;
136 TRACE("used in %lu, used out %lu\n", ash.cbSrcLengthUsed, ash.cbDstLengthUsed);
138 hr = IMediaSample_SetActualDataLength(pSample, ash.cbDstLengthUsed);
141 if (ash.cbSrcLengthUsed < ash.cbSrcLength) {
142 This->current_size = ash.cbSrcLength - ash.cbSrcLengthUsed;
143 memmove(This->buffer, This->buffer + ash.cbSrcLengthUsed, This->current_size);
146 This->current_size = 0;
148 hr = OutputPin_SendSample((OutputPin*)This->tf.ppPins[1], pSample);
149 if (hr != S_OK && hr != VFW_E_NOT_CONNECTED) {
150 ERR("Error sending sample (%lx)\n", hr);
155 if (unprepare_header && (res = acmStreamUnprepareHeader(This->has, &ash, 0)))
156 ERR("Cannot unprepare header %d\n", res);
159 IMediaSample_Release(pSample);
165 static HRESULT ACMWrapper_ConnectInput(TransformFilterImpl* pTransformFilter, const AM_MEDIA_TYPE * pmt)
167 ACMWrapperImpl* This = (ACMWrapperImpl*)pTransformFilter;
170 TRACE("(%p)->(%p)\n", This, pmt);
172 if ((IsEqualIID(&pmt->majortype, &MEDIATYPE_Audio)) &&
173 (!memcmp(((char*)&pmt->subtype)+4, ((char*)&MEDIATYPE_Audio)+4, sizeof(GUID)-4)) && /* Check root (GUID w/o FOURCC) */
174 (IsEqualIID(&pmt->formattype, &FORMAT_WaveFormatEx)))
177 AM_MEDIA_TYPE* outpmt = &((OutputPin*)This->tf.ppPins[1])->pin.mtCurrent;
178 This->pWfIn = (LPWAVEFORMATEX)pmt->pbFormat;
181 /* TRACE("ALIGN = %d\n", pACMWrapper->pWfIn->nBlockAlign); */
182 /* pACMWrapper->pWfIn->nBlockAlign = 1; */
184 /* Set output audio data to PCM */
185 CopyMediaType(outpmt, pmt);
186 outpmt->subtype.Data1 = WAVE_FORMAT_PCM;
187 This->pWfOut = (WAVEFORMATEX*)outpmt->pbFormat;
188 This->pWfOut->wFormatTag = WAVE_FORMAT_PCM;
189 This->pWfOut->wBitsPerSample = 16;
190 This->pWfOut->nBlockAlign = 4;
191 This->pWfOut->cbSize = 0;
192 This->pWfOut->nAvgBytesPerSec = This->pWfOut->nChannels * This->pWfOut->nSamplesPerSec
193 * (This->pWfOut->wBitsPerSample/8);
195 if (!(res = acmStreamOpen(&drv, NULL, This->pWfIn, This->pWfOut, NULL, 0, 0, 0)))
199 if ((res = acmStreamSize(drv, OUTPUT_BUFFER_SIZE, &This->max_size, ACM_STREAMSIZEF_DESTINATION))) {
200 ERR("Cannot retrieve input buffer size error %d!\n", res);
201 This->max_size = INPUT_BUFFER_SIZE;
204 TRACE("input buffer size %ld\n", This->max_size);
206 /* Update buffer size of media samples in output */
207 ((OutputPin*)This->tf.ppPins[1])->allocProps.cbBuffer = OUTPUT_BUFFER_SIZE;
209 TRACE("Connection accepted\n");
213 FIXME("acmStreamOpen returned %d\n", res);
214 DeleteMediaType(outpmt);
215 TRACE("Unable to find a suitable ACM decompressor\n");
218 TRACE("Connection refused\n");
222 static HRESULT ACMWrapper_Cleanup(TransformFilterImpl* pTransformFilter)
224 ACMWrapperImpl* This = (ACMWrapperImpl*)pTransformFilter;
226 TRACE("(%p)->()\n", This);
229 acmStreamClose(This->has, 0);
236 TransformFuncsTable ACMWrapper_FuncsTable = {
238 ACMWrapper_ProcessSampleData,
240 ACMWrapper_ConnectInput,
244 HRESULT ACMWrapper_create(IUnknown * pUnkOuter, LPVOID * ppv)
247 ACMWrapperImpl* This;
249 TRACE("(%p, %p)\n", pUnkOuter, ppv);
254 return CLASS_E_NOAGGREGATION;
256 /* Note: This memory is managed by the transform filter once created */
257 This = CoTaskMemAlloc(sizeof(ACMWrapperImpl));
260 This->reinit_codec = TRUE;
262 hr = TransformFilter_Create(&(This->tf), &CLSID_ACMWrapper, &ACMWrapper_FuncsTable);