ddraw: Some drivers return a refresh rate of 0.
[wine/winequartzdrv.git] / dlls / msacm32 / stream.c
blobb05948d671957d32a93273aa43a3db7f1ac9c658
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
3 /*
4 * MSACM32 library
6 * Copyright 1998 Patrik Stridvall
7 * 1999 Eric Pouech
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 /* TODO
25 * + asynchronous conversion is not implemented
26 * + callback/notification
27 * * acmStreamMessage
28 * + properly close ACM streams
31 #include <stdarg.h>
32 #include <string.h>
33 #include "windef.h"
34 #include "winbase.h"
35 #include "winerror.h"
36 #include "wine/debug.h"
37 #include "mmsystem.h"
38 #define NOBITMAP
39 #include "mmreg.h"
40 #include "msacm.h"
41 #include "msacmdrv.h"
42 #include "wineacm.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(msacm);
46 static PWINE_ACMSTREAM ACM_GetStream(HACMSTREAM has)
48 TRACE("(%p)\n", has);
50 return (PWINE_ACMSTREAM)has;
53 /***********************************************************************
54 * acmStreamClose (MSACM32.@)
56 MMRESULT WINAPI acmStreamClose(HACMSTREAM has, DWORD fdwClose)
58 PWINE_ACMSTREAM was;
59 MMRESULT ret;
61 TRACE("(%p, %d)\n", has, fdwClose);
63 if ((was = ACM_GetStream(has)) == NULL) {
64 WARN("invalid handle\n");
65 return MMSYSERR_INVALHANDLE;
67 ret = MSACM_Message((HACMDRIVER)was->pDrv, ACMDM_STREAM_CLOSE, (LPARAM)&was->drvInst, 0);
68 if (ret == MMSYSERR_NOERROR) {
69 if (was->hAcmDriver)
70 acmDriverClose(was->hAcmDriver, 0L);
71 HeapFree(MSACM_hHeap, 0, was);
73 TRACE("=> (%d)\n", ret);
74 return ret;
77 /***********************************************************************
78 * acmStreamConvert (MSACM32.@)
80 MMRESULT WINAPI acmStreamConvert(HACMSTREAM has, PACMSTREAMHEADER pash,
81 DWORD fdwConvert)
83 PWINE_ACMSTREAM was;
84 MMRESULT ret = MMSYSERR_NOERROR;
85 PACMDRVSTREAMHEADER padsh;
87 TRACE("(%p, %p, %d)\n", has, pash, fdwConvert);
89 if ((was = ACM_GetStream(has)) == NULL) {
90 WARN("invalid handle\n");
91 return MMSYSERR_INVALHANDLE;
93 if (!pash || pash->cbStruct < sizeof(ACMSTREAMHEADER)) {
94 WARN("invalid parameter\n");
95 return MMSYSERR_INVALPARAM;
97 if (!(pash->fdwStatus & ACMSTREAMHEADER_STATUSF_PREPARED)) {
98 WARN("unprepared header\n");
99 return ACMERR_UNPREPARED;
102 pash->cbSrcLengthUsed = 0;
103 pash->cbDstLengthUsed = 0;
105 /* Note: the ACMSTREAMHEADER and ACMDRVSTREAMHEADER structs are of same
106 * size. some fields are private to msacm internals, and are exposed
107 * in ACMSTREAMHEADER in the dwReservedDriver array
109 padsh = (PACMDRVSTREAMHEADER)pash;
111 /* check that pointers have not been modified */
112 if (padsh->pbPreparedSrc != padsh->pbSrc ||
113 padsh->cbPreparedSrcLength < padsh->cbSrcLength ||
114 padsh->pbPreparedDst != padsh->pbDst ||
115 padsh->cbPreparedDstLength < padsh->cbDstLength) {
116 WARN("invalid parameter\n");
117 return MMSYSERR_INVALPARAM;
120 padsh->fdwConvert = fdwConvert;
122 ret = MSACM_Message((HACMDRIVER)was->pDrv, ACMDM_STREAM_CONVERT, (LPARAM)&was->drvInst, (LPARAM)padsh);
123 if (ret == MMSYSERR_NOERROR) {
124 padsh->fdwStatus |= ACMSTREAMHEADER_STATUSF_DONE;
126 TRACE("=> (%d)\n", ret);
127 return ret;
130 /***********************************************************************
131 * acmStreamMessage (MSACM32.@)
133 MMRESULT WINAPI acmStreamMessage(HACMSTREAM has, UINT uMsg, LPARAM lParam1,
134 LPARAM lParam2)
136 FIXME("(%p, %u, %ld, %ld): stub\n", has, uMsg, lParam1, lParam2);
137 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
138 return MMSYSERR_ERROR;
141 /***********************************************************************
142 * acmStreamOpen (MSACM32.@)
144 MMRESULT WINAPI acmStreamOpen(PHACMSTREAM phas, HACMDRIVER had, PWAVEFORMATEX pwfxSrc,
145 PWAVEFORMATEX pwfxDst, PWAVEFILTER pwfltr, DWORD dwCallback,
146 DWORD dwInstance, DWORD fdwOpen)
148 PWINE_ACMSTREAM was;
149 PWINE_ACMDRIVER wad;
150 MMRESULT ret;
151 int wfxSrcSize;
152 int wfxDstSize;
153 WAVEFORMATEX wfxSrc, wfxDst;
155 TRACE("(%p, %p, %p, %p, %p, %d, %d, %d)\n",
156 phas, had, pwfxSrc, pwfxDst, pwfltr, dwCallback, dwInstance, fdwOpen);
158 /* NOTE: pwfxSrc and/or pwfxDst can point to a structure smaller than
159 * WAVEFORMATEX so don't use them directly when not sure */
160 if (pwfxSrc->wFormatTag == WAVE_FORMAT_PCM) {
161 memcpy(&wfxSrc, pwfxSrc, sizeof(PCMWAVEFORMAT));
162 wfxSrc.wBitsPerSample = pwfxSrc->wBitsPerSample;
163 wfxSrc.cbSize = 0;
164 pwfxSrc = &wfxSrc;
167 if (pwfxDst->wFormatTag == WAVE_FORMAT_PCM) {
168 memcpy(&wfxDst, pwfxDst, sizeof(PCMWAVEFORMAT));
169 wfxDst.wBitsPerSample = pwfxDst->wBitsPerSample;
170 wfxDst.cbSize = 0;
171 pwfxDst = &wfxDst;
174 TRACE("src [wFormatTag=%u, nChannels=%u, nSamplesPerSec=%u, nAvgBytesPerSec=%u, nBlockAlign=%u, wBitsPerSample=%u, cbSize=%u]\n",
175 pwfxSrc->wFormatTag, pwfxSrc->nChannels, pwfxSrc->nSamplesPerSec, pwfxSrc->nAvgBytesPerSec,
176 pwfxSrc->nBlockAlign, pwfxSrc->wBitsPerSample, pwfxSrc->cbSize);
178 TRACE("dst [wFormatTag=%u, nChannels=%u, nSamplesPerSec=%u, nAvgBytesPerSec=%u, nBlockAlign=%u, wBitsPerSample=%u, cbSize=%u]\n",
179 pwfxDst->wFormatTag, pwfxDst->nChannels, pwfxDst->nSamplesPerSec, pwfxDst->nAvgBytesPerSec,
180 pwfxDst->nBlockAlign, pwfxDst->wBitsPerSample, pwfxDst->cbSize);
182 /* (WS) In query mode, phas should be NULL. If it is not, then instead
183 * of returning an error we are making sure it is NULL, preventing some
184 * applications that pass garbage for phas from crashing.
186 if (fdwOpen & ACM_STREAMOPENF_QUERY) phas = NULL;
188 if (pwfltr && (pwfxSrc->wFormatTag != pwfxDst->wFormatTag)) {
189 WARN("invalid parameter\n");
190 return MMSYSERR_INVALPARAM;
193 wfxSrcSize = wfxDstSize = sizeof(WAVEFORMATEX);
194 if (pwfxSrc->wFormatTag != WAVE_FORMAT_PCM) wfxSrcSize += pwfxSrc->cbSize;
195 if (pwfxDst->wFormatTag != WAVE_FORMAT_PCM) wfxDstSize += pwfxDst->cbSize;
197 was = HeapAlloc(MSACM_hHeap, 0, sizeof(*was) + wfxSrcSize + wfxDstSize +
198 ((pwfltr) ? sizeof(WAVEFILTER) : 0));
199 if (was == NULL) {
200 WARN("no memory\n");
201 return MMSYSERR_NOMEM;
204 was->drvInst.cbStruct = sizeof(was->drvInst);
205 was->drvInst.pwfxSrc = (PWAVEFORMATEX)((LPSTR)was + sizeof(*was));
206 memcpy(was->drvInst.pwfxSrc, pwfxSrc, wfxSrcSize);
207 was->drvInst.pwfxDst = (PWAVEFORMATEX)((LPSTR)was + sizeof(*was) + wfxSrcSize);
208 memcpy(was->drvInst.pwfxDst, pwfxDst, wfxDstSize);
209 if (pwfltr) {
210 was->drvInst.pwfltr = (PWAVEFILTER)((LPSTR)was + sizeof(*was) + wfxSrcSize + wfxDstSize);
211 memcpy(was->drvInst.pwfltr, pwfltr, sizeof(WAVEFILTER));
212 } else {
213 was->drvInst.pwfltr = NULL;
215 was->drvInst.dwCallback = dwCallback;
216 was->drvInst.dwInstance = dwInstance;
217 was->drvInst.fdwOpen = fdwOpen;
218 was->drvInst.fdwDriver = 0L;
219 was->drvInst.dwDriver = 0L;
220 /* real value will be stored once ACMDM_STREAM_OPEN succeeds */
221 was->drvInst.has = 0L;
223 if (had) {
224 if (!(wad = MSACM_GetDriver(had))) {
225 ret = MMSYSERR_INVALPARAM;
226 goto errCleanUp;
229 was->obj.dwType = WINE_ACMOBJ_STREAM;
230 was->obj.pACMDriverID = wad->obj.pACMDriverID;
231 was->pDrv = wad;
232 was->hAcmDriver = 0; /* not to close it in acmStreamClose */
234 ret = MSACM_Message((HACMDRIVER)wad, ACMDM_STREAM_OPEN, (LPARAM)&was->drvInst, 0L);
235 if (ret != MMSYSERR_NOERROR)
236 goto errCleanUp;
237 } else {
238 PWINE_ACMDRIVERID wadi;
240 ret = ACMERR_NOTPOSSIBLE;
241 for (wadi = MSACM_pFirstACMDriverID; wadi; wadi = wadi->pNextACMDriverID) {
242 if ((wadi->fdwSupport & ACMDRIVERDETAILS_SUPPORTF_DISABLED) ||
243 !MSACM_FindFormatTagInCache(wadi, pwfxSrc->wFormatTag, NULL) ||
244 !MSACM_FindFormatTagInCache(wadi, pwfxDst->wFormatTag, NULL))
245 continue;
246 ret = acmDriverOpen(&had, (HACMDRIVERID)wadi, 0L);
247 if (ret != MMSYSERR_NOERROR)
248 continue;
249 if ((wad = MSACM_GetDriver(had)) != 0) {
250 was->obj.dwType = WINE_ACMOBJ_STREAM;
251 was->obj.pACMDriverID = wad->obj.pACMDriverID;
252 was->pDrv = wad;
253 was->hAcmDriver = had;
255 ret = MSACM_Message((HACMDRIVER)wad, ACMDM_STREAM_OPEN, (LPARAM)&was->drvInst, 0L);
256 TRACE("%s => %08x\n", debugstr_w(wadi->pszDriverAlias), ret);
257 if (ret == MMSYSERR_NOERROR) {
258 if (fdwOpen & ACM_STREAMOPENF_QUERY) {
259 acmDriverClose(had, 0L);
261 break;
264 /* no match, close this acm driver and try next one */
265 acmDriverClose(had, 0L);
267 if (ret != MMSYSERR_NOERROR) {
268 ret = ACMERR_NOTPOSSIBLE;
269 goto errCleanUp;
272 ret = MMSYSERR_NOERROR;
273 was->drvInst.has = (HACMSTREAM)was;
274 if (!(fdwOpen & ACM_STREAMOPENF_QUERY)) {
275 if (phas)
276 *phas = (HACMSTREAM)was;
277 TRACE("=> (%d)\n", ret);
278 return ret;
280 errCleanUp:
281 if (phas)
282 *phas = NULL;
283 HeapFree(MSACM_hHeap, 0, was);
284 TRACE("=> (%d)\n", ret);
285 return ret;
289 /***********************************************************************
290 * acmStreamPrepareHeader (MSACM32.@)
292 MMRESULT WINAPI acmStreamPrepareHeader(HACMSTREAM has, PACMSTREAMHEADER pash,
293 DWORD fdwPrepare)
295 PWINE_ACMSTREAM was;
296 MMRESULT ret = MMSYSERR_NOERROR;
297 PACMDRVSTREAMHEADER padsh;
299 TRACE("(%p, %p, %d)\n", has, pash, fdwPrepare);
301 if ((was = ACM_GetStream(has)) == NULL) {
302 WARN("invalid handle\n");
303 return MMSYSERR_INVALHANDLE;
305 if (!pash || pash->cbStruct < sizeof(ACMSTREAMHEADER)) {
306 WARN("invalid parameter\n");
307 return MMSYSERR_INVALPARAM;
309 if (fdwPrepare)
310 ret = MMSYSERR_INVALFLAG;
312 if (pash->fdwStatus & ACMSTREAMHEADER_STATUSF_DONE)
313 return MMSYSERR_NOERROR;
315 /* Note: the ACMSTREAMHEADER and ACMDRVSTREAMHEADER structs are of same
316 * size. some fields are private to msacm internals, and are exposed
317 * in ACMSTREAMHEADER in the dwReservedDriver array
319 padsh = (PACMDRVSTREAMHEADER)pash;
321 padsh->fdwConvert = fdwPrepare;
322 padsh->padshNext = NULL;
323 padsh->fdwDriver = padsh->dwDriver = 0L;
325 padsh->fdwPrepared = 0;
326 padsh->dwPrepared = 0;
327 padsh->pbPreparedSrc = 0;
328 padsh->cbPreparedSrcLength = 0;
329 padsh->pbPreparedDst = 0;
330 padsh->cbPreparedDstLength = 0;
332 ret = MSACM_Message((HACMDRIVER)was->pDrv, ACMDM_STREAM_PREPARE, (LPARAM)&was->drvInst, (DWORD)padsh);
333 if (ret == MMSYSERR_NOERROR || ret == MMSYSERR_NOTSUPPORTED) {
334 ret = MMSYSERR_NOERROR;
335 padsh->fdwStatus &= ~(ACMSTREAMHEADER_STATUSF_DONE|ACMSTREAMHEADER_STATUSF_INQUEUE);
336 padsh->fdwStatus |= ACMSTREAMHEADER_STATUSF_PREPARED;
337 padsh->fdwPrepared = padsh->fdwStatus;
338 padsh->dwPrepared = 0;
339 padsh->pbPreparedSrc = padsh->pbSrc;
340 padsh->cbPreparedSrcLength = padsh->cbSrcLength;
341 padsh->pbPreparedDst = padsh->pbDst;
342 padsh->cbPreparedDstLength = padsh->cbDstLength;
343 } else {
344 padsh->fdwPrepared = 0;
345 padsh->dwPrepared = 0;
346 padsh->pbPreparedSrc = 0;
347 padsh->cbPreparedSrcLength = 0;
348 padsh->pbPreparedDst = 0;
349 padsh->cbPreparedDstLength = 0;
351 TRACE("=> (%d)\n", ret);
352 return ret;
355 /***********************************************************************
356 * acmStreamReset (MSACM32.@)
358 MMRESULT WINAPI acmStreamReset(HACMSTREAM has, DWORD fdwReset)
360 PWINE_ACMSTREAM was;
361 MMRESULT ret = MMSYSERR_NOERROR;
363 TRACE("(%p, %d)\n", has, fdwReset);
365 if (fdwReset) {
366 WARN("invalid flag\n");
367 ret = MMSYSERR_INVALFLAG;
368 } else if ((was = ACM_GetStream(has)) == NULL) {
369 WARN("invalid handle\n");
370 return MMSYSERR_INVALHANDLE;
371 } else if (was->drvInst.fdwOpen & ACM_STREAMOPENF_ASYNC) {
372 ret = MSACM_Message((HACMDRIVER)was->pDrv, ACMDM_STREAM_RESET, (LPARAM)&was->drvInst, 0);
374 TRACE("=> (%d)\n", ret);
375 return ret;
378 /***********************************************************************
379 * acmStreamSize (MSACM32.@)
381 MMRESULT WINAPI acmStreamSize(HACMSTREAM has, DWORD cbInput,
382 LPDWORD pdwOutputBytes, DWORD fdwSize)
384 PWINE_ACMSTREAM was;
385 ACMDRVSTREAMSIZE adss;
386 MMRESULT ret;
388 TRACE("(%p, %d, %p, %d)\n", has, cbInput, pdwOutputBytes, fdwSize);
390 if ((was = ACM_GetStream(has)) == NULL) {
391 WARN("invalid handle\n");
392 return MMSYSERR_INVALHANDLE;
394 if ((fdwSize & ~ACM_STREAMSIZEF_QUERYMASK) != 0) {
395 WARN("invalid flag\n");
396 return MMSYSERR_INVALFLAG;
399 *pdwOutputBytes = 0L;
401 switch (fdwSize & ACM_STREAMSIZEF_QUERYMASK) {
402 case ACM_STREAMSIZEF_DESTINATION:
403 adss.cbDstLength = cbInput;
404 adss.cbSrcLength = 0;
405 break;
406 case ACM_STREAMSIZEF_SOURCE:
407 adss.cbSrcLength = cbInput;
408 adss.cbDstLength = 0;
409 break;
410 default:
411 WARN("invalid flag\n");
412 return MMSYSERR_INVALFLAG;
415 adss.cbStruct = sizeof(adss);
416 adss.fdwSize = fdwSize;
417 ret = MSACM_Message((HACMDRIVER)was->pDrv, ACMDM_STREAM_SIZE,
418 (LPARAM)&was->drvInst, (LPARAM)&adss);
419 if (ret == MMSYSERR_NOERROR) {
420 switch (fdwSize & ACM_STREAMSIZEF_QUERYMASK) {
421 case ACM_STREAMSIZEF_DESTINATION:
422 *pdwOutputBytes = adss.cbSrcLength;
423 break;
424 case ACM_STREAMSIZEF_SOURCE:
425 *pdwOutputBytes = adss.cbDstLength;
426 break;
429 TRACE("=> (%d) [%u]\n", ret, *pdwOutputBytes);
430 return ret;
433 /***********************************************************************
434 * acmStreamUnprepareHeader (MSACM32.@)
436 MMRESULT WINAPI acmStreamUnprepareHeader(HACMSTREAM has, PACMSTREAMHEADER pash,
437 DWORD fdwUnprepare)
439 PWINE_ACMSTREAM was;
440 MMRESULT ret = MMSYSERR_NOERROR;
441 PACMDRVSTREAMHEADER padsh;
443 TRACE("(%p, %p, %d)\n", has, pash, fdwUnprepare);
445 if ((was = ACM_GetStream(has)) == NULL) {
446 WARN("invalid handle\n");
447 return MMSYSERR_INVALHANDLE;
449 if (!pash || pash->cbStruct < sizeof(ACMSTREAMHEADER)) {
450 WARN("invalid parameter\n");
451 return MMSYSERR_INVALPARAM;
453 if (!(pash->fdwStatus & ACMSTREAMHEADER_STATUSF_PREPARED)) {
454 WARN("unprepared header\n");
455 return ACMERR_UNPREPARED;
458 /* Note: the ACMSTREAMHEADER and ACMDRVSTREAMHEADER structs are of same
459 * size. some fields are private to msacm internals, and are exposed
460 * in ACMSTREAMHEADER in the dwReservedDriver array
462 padsh = (PACMDRVSTREAMHEADER)pash;
464 /* check that pointers have not been modified */
465 if (padsh->pbPreparedSrc != padsh->pbSrc ||
466 padsh->cbPreparedSrcLength < padsh->cbSrcLength ||
467 padsh->pbPreparedDst != padsh->pbDst ||
468 padsh->cbPreparedDstLength < padsh->cbDstLength) {
469 WARN("invalid parameter\n");
470 return MMSYSERR_INVALPARAM;
473 padsh->fdwConvert = fdwUnprepare;
475 ret = MSACM_Message((HACMDRIVER)was->pDrv, ACMDM_STREAM_UNPREPARE, (LPARAM)&was->drvInst, (LPARAM)padsh);
476 if (ret == MMSYSERR_NOERROR || ret == MMSYSERR_NOTSUPPORTED) {
477 ret = MMSYSERR_NOERROR;
478 padsh->fdwStatus &= ~(ACMSTREAMHEADER_STATUSF_DONE|ACMSTREAMHEADER_STATUSF_INQUEUE|ACMSTREAMHEADER_STATUSF_PREPARED);
480 TRACE("=> (%d)\n", ret);
481 return ret;