push ab3cc77eb114d2bd400734a9dccad24563f936a6
[wine/hacks.git] / dlls / quartz / acmwrapper.c
blobb4e3274265983aa437d20c8edc45a7148289af6b
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 /* FIXME: Improve buffers management */
45 #define OUTPUT_BUFFER_SIZE 15000
46 #define INPUT_BUFFER_SIZE 4096
48 typedef struct ACMWrapperImpl
50 TransformFilterImpl tf;
51 HACMSTREAM has;
52 LPWAVEFORMATEX pWfIn;
53 LPWAVEFORMATEX pWfOut;
54 BYTE buffer[INPUT_BUFFER_SIZE];
55 DWORD max_size;
56 DWORD current_size;
57 BOOL reinit_codec; /* FIXME: Should use sync points instead */
58 } ACMWrapperImpl;
60 static HRESULT ACMWrapper_ProcessSampleData(TransformFilterImpl* pTransformFilter, IMediaSample *pSample)
62 ACMWrapperImpl* This = (ACMWrapperImpl*)pTransformFilter;
63 AM_MEDIA_TYPE amt;
64 IMediaSample* pOutSample = NULL;
65 DWORD cbDstStream;
66 LPBYTE pbDstStream;
67 DWORD cbSrcStream = 0;
68 LPBYTE pbSrcStream = NULL;
69 ACMSTREAMHEADER ash;
70 DWORD offset = 0;
71 BOOL stop = FALSE;
72 BOOL unprepare_header = FALSE;
73 MMRESULT res;
74 HRESULT hr;
76 hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
77 if (FAILED(hr))
79 ERR("Cannot get pointer to sample data (%x)\n", hr);
80 return hr;
83 cbSrcStream = IMediaSample_GetActualDataLength(pSample);
85 TRACE("Sample data ptr = %p, size = %ld\n", pbSrcStream, (long)cbSrcStream);
87 hr = IPin_ConnectionMediaType(This->tf.ppPins[0], &amt);
88 if (FAILED(hr)) {
89 ERR("Unable to retrieve media type\n");
90 return hr;
93 while(hr == S_OK && !stop)
95 DWORD rem_buf = This->max_size - This->current_size;
96 DWORD rem_smp = cbSrcStream - offset;
97 DWORD copy_size = min(rem_buf, rem_smp);
99 memcpy(This->buffer + This->current_size, pbSrcStream + offset, copy_size);
100 This->current_size += copy_size;
101 offset += copy_size;
103 if (offset >= cbSrcStream)
104 stop = TRUE;
105 if (This->current_size < This->max_size)
106 break;
108 hr = OutputPin_GetDeliveryBuffer((OutputPin*)This->tf.ppPins[1], &pOutSample, NULL, NULL, 0);
109 if (FAILED(hr)) {
110 ERR("Unable to get delivery buffer (%x)\n", hr);
111 return hr;
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.pbSrc = This->buffer;
128 ash.cbSrcLength = This->current_size;
129 ash.pbDst = pbDstStream;
130 ash.cbDstLength = cbDstStream;
132 if ((res = acmStreamPrepareHeader(This->has, &ash, 0))) {
133 ERR("Cannot prepare header %d\n", res);
134 goto error;
137 unprepare_header = TRUE;
139 if ((res = acmStreamConvert(This->has, &ash, This->reinit_codec ? ACM_STREAMCONVERTF_START : 0))) {
140 ERR("Cannot convert data header %d\n", res);
141 goto error;
143 This->reinit_codec = FALSE;
145 TRACE("used in %u, used out %u\n", ash.cbSrcLengthUsed, ash.cbDstLengthUsed);
147 hr = IMediaSample_SetActualDataLength(pOutSample, ash.cbDstLengthUsed);
148 assert(hr == S_OK);
150 if (ash.cbSrcLengthUsed < ash.cbSrcLength) {
151 This->current_size = ash.cbSrcLength - ash.cbSrcLengthUsed;
152 memmove(This->buffer, This->buffer + ash.cbSrcLengthUsed, This->current_size);
154 else
155 This->current_size = 0;
157 hr = OutputPin_SendSample((OutputPin*)This->tf.ppPins[1], pOutSample);
158 if (hr != S_OK && hr != VFW_E_NOT_CONNECTED) {
159 ERR("Error sending sample (%x)\n", hr);
160 goto error;
163 error:
164 if (unprepare_header && (res = acmStreamUnprepareHeader(This->has, &ash, 0)))
165 ERR("Cannot unprepare header %d\n", res);
166 unprepare_header = FALSE;
168 if (pOutSample)
169 IMediaSample_Release(pOutSample);
170 pOutSample = NULL;
173 return hr;
176 static HRESULT ACMWrapper_ConnectInput(TransformFilterImpl* pTransformFilter, const AM_MEDIA_TYPE * pmt)
178 ACMWrapperImpl* This = (ACMWrapperImpl*)pTransformFilter;
179 MMRESULT res;
181 TRACE("(%p)->(%p)\n", This, pmt);
183 /* Check root (GUID w/o FOURCC) */
184 if ((IsEqualIID(&pmt->majortype, &MEDIATYPE_Audio)) &&
185 (!memcmp(((const char *)&pmt->subtype)+4, ((const char *)&MEDIATYPE_Audio)+4, sizeof(GUID)-4)) &&
186 (IsEqualIID(&pmt->formattype, &FORMAT_WaveFormatEx)))
188 HACMSTREAM drv;
189 AM_MEDIA_TYPE* outpmt = &((OutputPin*)This->tf.ppPins[1])->pin.mtCurrent;
190 This->pWfIn = (LPWAVEFORMATEX)pmt->pbFormat;
192 /* HACK */
193 /* TRACE("ALIGN = %d\n", pACMWrapper->pWfIn->nBlockAlign); */
194 /* pACMWrapper->pWfIn->nBlockAlign = 1; */
196 /* Set output audio data to PCM */
197 CopyMediaType(outpmt, pmt);
198 outpmt->subtype.Data1 = WAVE_FORMAT_PCM;
199 This->pWfOut = (WAVEFORMATEX*)outpmt->pbFormat;
200 This->pWfOut->wFormatTag = WAVE_FORMAT_PCM;
201 This->pWfOut->wBitsPerSample = 16;
202 This->pWfOut->nBlockAlign = 4;
203 This->pWfOut->cbSize = 0;
204 This->pWfOut->nAvgBytesPerSec = This->pWfOut->nChannels * This->pWfOut->nSamplesPerSec
205 * (This->pWfOut->wBitsPerSample/8);
207 if (!(res = acmStreamOpen(&drv, NULL, This->pWfIn, This->pWfOut, NULL, 0, 0, 0)))
209 This->has = drv;
211 if ((res = acmStreamSize(drv, OUTPUT_BUFFER_SIZE, &This->max_size, ACM_STREAMSIZEF_DESTINATION))) {
212 ERR("Cannot retrieve input buffer size error %d!\n", res);
213 This->max_size = INPUT_BUFFER_SIZE;
216 TRACE("input buffer size %d\n", This->max_size);
218 /* Update buffer size of media samples in output */
219 ((OutputPin*)This->tf.ppPins[1])->allocProps.cbBuffer = OUTPUT_BUFFER_SIZE;
221 TRACE("Connection accepted\n");
222 return S_OK;
224 else
225 FIXME("acmStreamOpen returned %d\n", res);
226 FreeMediaType(outpmt);
227 TRACE("Unable to find a suitable ACM decompressor\n");
230 TRACE("Connection refused\n");
231 return VFW_E_TYPE_NOT_ACCEPTED;
234 static HRESULT ACMWrapper_Cleanup(TransformFilterImpl* pTransformFilter)
236 ACMWrapperImpl* This = (ACMWrapperImpl*)pTransformFilter;
238 TRACE("(%p)->()\n", This);
240 if (This->has)
241 acmStreamClose(This->has, 0);
243 This->has = 0;
245 return S_OK;
248 static const TransformFuncsTable ACMWrapper_FuncsTable = {
249 NULL,
250 ACMWrapper_ProcessSampleData,
251 NULL,
252 NULL,
253 ACMWrapper_ConnectInput,
254 ACMWrapper_Cleanup
257 HRESULT ACMWrapper_create(IUnknown * pUnkOuter, LPVOID * ppv)
259 HRESULT hr;
260 ACMWrapperImpl* This;
262 TRACE("(%p, %p)\n", pUnkOuter, ppv);
264 *ppv = NULL;
266 if (pUnkOuter)
267 return CLASS_E_NOAGGREGATION;
269 /* Note: This memory is managed by the transform filter once created */
270 This = CoTaskMemAlloc(sizeof(ACMWrapperImpl));
271 ZeroMemory(This, sizeof(ACMWrapperImpl));
273 This->reinit_codec = TRUE;
275 hr = TransformFilter_Create(&(This->tf), &CLSID_ACMWrapper, &ACMWrapper_FuncsTable);
277 if (FAILED(hr))
278 return hr;
280 *ppv = (LPVOID)This;
282 return hr;