d2d1: Implement d2d_d3d_render_target_GetTransform().
[wine.git] / dlls / msacm32 / stream.c
blobbdfc3ccf2aad552d72e9e89ba369f993b83c86f4
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,
145 PWAVEFORMATEX pwfxSrc, PWAVEFORMATEX pwfxDst,
146 PWAVEFILTER pwfltr, DWORD_PTR dwCallback,
147 DWORD_PTR dwInstance, DWORD fdwOpen)
149 PWINE_ACMSTREAM was;
150 PWINE_ACMDRIVER wad;
151 MMRESULT ret;
152 int wfxSrcSize;
153 int wfxDstSize;
154 WAVEFORMATEX wfxSrc, wfxDst;
156 TRACE("(%p, %p, %p, %p, %p, %ld, %ld, %d)\n",
157 phas, had, pwfxSrc, pwfxDst, pwfltr, dwCallback, dwInstance, fdwOpen);
159 /* NOTE: pwfxSrc and/or pwfxDst can point to a structure smaller than
160 * WAVEFORMATEX so don't use them directly when not sure */
161 if (pwfxSrc->wFormatTag == WAVE_FORMAT_PCM) {
162 memcpy(&wfxSrc, pwfxSrc, sizeof(PCMWAVEFORMAT));
163 wfxSrc.wBitsPerSample = pwfxSrc->wBitsPerSample;
164 wfxSrc.cbSize = 0;
165 pwfxSrc = &wfxSrc;
168 if (pwfxDst->wFormatTag == WAVE_FORMAT_PCM) {
169 memcpy(&wfxDst, pwfxDst, sizeof(PCMWAVEFORMAT));
170 wfxDst.wBitsPerSample = pwfxDst->wBitsPerSample;
171 wfxDst.cbSize = 0;
172 pwfxDst = &wfxDst;
175 TRACE("src [wFormatTag=%u, nChannels=%u, nSamplesPerSec=%u, nAvgBytesPerSec=%u, nBlockAlign=%u, wBitsPerSample=%u, cbSize=%u]\n",
176 pwfxSrc->wFormatTag, pwfxSrc->nChannels, pwfxSrc->nSamplesPerSec, pwfxSrc->nAvgBytesPerSec,
177 pwfxSrc->nBlockAlign, pwfxSrc->wBitsPerSample, pwfxSrc->cbSize);
179 TRACE("dst [wFormatTag=%u, nChannels=%u, nSamplesPerSec=%u, nAvgBytesPerSec=%u, nBlockAlign=%u, wBitsPerSample=%u, cbSize=%u]\n",
180 pwfxDst->wFormatTag, pwfxDst->nChannels, pwfxDst->nSamplesPerSec, pwfxDst->nAvgBytesPerSec,
181 pwfxDst->nBlockAlign, pwfxDst->wBitsPerSample, pwfxDst->cbSize);
183 /* (WS) In query mode, phas should be NULL. If it is not, then instead
184 * of returning an error we are making sure it is NULL, preventing some
185 * applications that pass garbage for phas from crashing.
187 if (fdwOpen & ACM_STREAMOPENF_QUERY) phas = NULL;
189 if (pwfltr && (pwfxSrc->wFormatTag != pwfxDst->wFormatTag)) {
190 WARN("invalid parameter\n");
191 return MMSYSERR_INVALPARAM;
194 wfxSrcSize = wfxDstSize = sizeof(WAVEFORMATEX);
195 if (pwfxSrc->wFormatTag != WAVE_FORMAT_PCM) wfxSrcSize += pwfxSrc->cbSize;
196 if (pwfxDst->wFormatTag != WAVE_FORMAT_PCM) wfxDstSize += pwfxDst->cbSize;
198 was = HeapAlloc(MSACM_hHeap, 0, sizeof(*was) + wfxSrcSize + wfxDstSize +
199 ((pwfltr) ? sizeof(WAVEFILTER) : 0));
200 if (was == NULL) {
201 WARN("no memory\n");
202 return MMSYSERR_NOMEM;
205 was->drvInst.cbStruct = sizeof(was->drvInst);
206 was->drvInst.pwfxSrc = (PWAVEFORMATEX)((LPSTR)was + sizeof(*was));
207 memcpy(was->drvInst.pwfxSrc, pwfxSrc, wfxSrcSize);
208 was->drvInst.pwfxDst = (PWAVEFORMATEX)((LPSTR)was + sizeof(*was) + wfxSrcSize);
209 memcpy(was->drvInst.pwfxDst, pwfxDst, wfxDstSize);
210 if (pwfltr) {
211 was->drvInst.pwfltr = (PWAVEFILTER)((LPSTR)was + sizeof(*was) + wfxSrcSize + wfxDstSize);
212 memcpy(was->drvInst.pwfltr, pwfltr, sizeof(WAVEFILTER));
213 } else {
214 was->drvInst.pwfltr = NULL;
216 was->drvInst.dwCallback = dwCallback;
217 was->drvInst.dwInstance = dwInstance;
218 was->drvInst.fdwOpen = fdwOpen;
219 was->drvInst.fdwDriver = 0L;
220 was->drvInst.dwDriver = 0L;
221 /* real value will be stored once ACMDM_STREAM_OPEN succeeds */
222 was->drvInst.has = 0L;
224 if (had) {
225 if (!(wad = MSACM_GetDriver(had))) {
226 ret = MMSYSERR_INVALPARAM;
227 goto errCleanUp;
230 was->obj.dwType = WINE_ACMOBJ_STREAM;
231 was->obj.pACMDriverID = wad->obj.pACMDriverID;
232 was->pDrv = wad;
233 was->hAcmDriver = 0; /* not to close it in acmStreamClose */
235 ret = MSACM_Message((HACMDRIVER)wad, ACMDM_STREAM_OPEN, (LPARAM)&was->drvInst, 0L);
236 if (ret != MMSYSERR_NOERROR)
237 goto errCleanUp;
238 } else {
239 PWINE_ACMDRIVERID wadi;
241 ret = ACMERR_NOTPOSSIBLE;
242 for (wadi = MSACM_pFirstACMDriverID; wadi; wadi = wadi->pNextACMDriverID) {
243 if ((wadi->fdwSupport & ACMDRIVERDETAILS_SUPPORTF_DISABLED) ||
244 !MSACM_FindFormatTagInCache(wadi, pwfxSrc->wFormatTag, NULL) ||
245 !MSACM_FindFormatTagInCache(wadi, pwfxDst->wFormatTag, NULL))
246 continue;
247 ret = acmDriverOpen(&had, (HACMDRIVERID)wadi, 0L);
248 if (ret != MMSYSERR_NOERROR)
249 continue;
250 if ((wad = MSACM_GetDriver(had)) != 0) {
251 was->obj.dwType = WINE_ACMOBJ_STREAM;
252 was->obj.pACMDriverID = wad->obj.pACMDriverID;
253 was->pDrv = wad;
254 was->hAcmDriver = had;
256 ret = MSACM_Message((HACMDRIVER)wad, ACMDM_STREAM_OPEN, (LPARAM)&was->drvInst, 0L);
257 TRACE("%s => %08x\n", debugstr_w(wadi->pszDriverAlias), ret);
258 if (ret == MMSYSERR_NOERROR) {
259 if (fdwOpen & ACM_STREAMOPENF_QUERY) {
260 MSACM_Message((HACMDRIVER)wad, ACMDM_STREAM_CLOSE, (LPARAM)&was->drvInst, 0);
261 acmDriverClose(had, 0L);
263 break;
266 /* no match, close this acm driver and try next one */
267 acmDriverClose(had, 0L);
269 if (ret != MMSYSERR_NOERROR) {
270 ret = ACMERR_NOTPOSSIBLE;
271 goto errCleanUp;
274 ret = MMSYSERR_NOERROR;
275 was->drvInst.has = (HACMSTREAM)was;
276 if (!(fdwOpen & ACM_STREAMOPENF_QUERY)) {
277 if (phas)
278 *phas = (HACMSTREAM)was;
279 TRACE("=> (%d)\n", ret);
280 return ret;
282 errCleanUp:
283 if (phas)
284 *phas = NULL;
285 HeapFree(MSACM_hHeap, 0, was);
286 TRACE("=> (%d)\n", ret);
287 return ret;
291 /***********************************************************************
292 * acmStreamPrepareHeader (MSACM32.@)
294 MMRESULT WINAPI acmStreamPrepareHeader(HACMSTREAM has, PACMSTREAMHEADER pash,
295 DWORD fdwPrepare)
297 PWINE_ACMSTREAM was;
298 MMRESULT ret = MMSYSERR_NOERROR;
299 PACMDRVSTREAMHEADER padsh;
301 TRACE("(%p, %p, %d)\n", has, pash, fdwPrepare);
303 if ((was = ACM_GetStream(has)) == NULL) {
304 WARN("invalid handle\n");
305 return MMSYSERR_INVALHANDLE;
307 if (!pash || pash->cbStruct < sizeof(ACMSTREAMHEADER)) {
308 WARN("invalid parameter\n");
309 return MMSYSERR_INVALPARAM;
311 if (fdwPrepare)
312 ret = MMSYSERR_INVALFLAG;
314 /* Note: the ACMSTREAMHEADER and ACMDRVSTREAMHEADER structs are of same
315 * size. some fields are private to msacm internals, and are exposed
316 * in ACMSTREAMHEADER in the dwReservedDriver array
318 padsh = (PACMDRVSTREAMHEADER)pash;
320 padsh->fdwConvert = fdwPrepare;
321 padsh->padshNext = NULL;
322 padsh->fdwDriver = padsh->dwDriver = 0L;
324 padsh->fdwPrepared = 0;
325 padsh->dwPrepared = 0;
326 padsh->pbPreparedSrc = 0;
327 padsh->cbPreparedSrcLength = 0;
328 padsh->pbPreparedDst = 0;
329 padsh->cbPreparedDstLength = 0;
331 ret = MSACM_Message((HACMDRIVER)was->pDrv, ACMDM_STREAM_PREPARE, (LPARAM)&was->drvInst, (LPARAM)padsh);
332 if (ret == MMSYSERR_NOERROR || ret == MMSYSERR_NOTSUPPORTED) {
333 ret = MMSYSERR_NOERROR;
334 padsh->fdwStatus &= ~ACMSTREAMHEADER_STATUSF_INQUEUE;
335 padsh->fdwStatus |= ACMSTREAMHEADER_STATUSF_PREPARED;
336 padsh->fdwPrepared = padsh->fdwStatus;
337 padsh->dwPrepared = 0;
338 padsh->pbPreparedSrc = padsh->pbSrc;
339 padsh->cbPreparedSrcLength = padsh->cbSrcLength;
340 padsh->pbPreparedDst = padsh->pbDst;
341 padsh->cbPreparedDstLength = padsh->cbDstLength;
342 } else {
343 padsh->fdwPrepared = 0;
344 padsh->dwPrepared = 0;
345 padsh->pbPreparedSrc = 0;
346 padsh->cbPreparedSrcLength = 0;
347 padsh->pbPreparedDst = 0;
348 padsh->cbPreparedDstLength = 0;
350 TRACE("=> (%d)\n", ret);
351 return ret;
354 /***********************************************************************
355 * acmStreamReset (MSACM32.@)
357 MMRESULT WINAPI acmStreamReset(HACMSTREAM has, DWORD fdwReset)
359 PWINE_ACMSTREAM was;
360 MMRESULT ret = MMSYSERR_NOERROR;
362 TRACE("(%p, %d)\n", has, fdwReset);
364 if (fdwReset) {
365 WARN("invalid flag\n");
366 ret = MMSYSERR_INVALFLAG;
367 } else if ((was = ACM_GetStream(has)) == NULL) {
368 WARN("invalid handle\n");
369 return MMSYSERR_INVALHANDLE;
370 } else if (was->drvInst.fdwOpen & ACM_STREAMOPENF_ASYNC) {
371 ret = MSACM_Message((HACMDRIVER)was->pDrv, ACMDM_STREAM_RESET, (LPARAM)&was->drvInst, 0);
373 TRACE("=> (%d)\n", ret);
374 return ret;
377 /***********************************************************************
378 * acmStreamSize (MSACM32.@)
380 MMRESULT WINAPI acmStreamSize(HACMSTREAM has, DWORD cbInput,
381 LPDWORD pdwOutputBytes, DWORD fdwSize)
383 PWINE_ACMSTREAM was;
384 ACMDRVSTREAMSIZE adss;
385 MMRESULT ret;
387 TRACE("(%p, %d, %p, %d)\n", has, cbInput, pdwOutputBytes, fdwSize);
389 if ((was = ACM_GetStream(has)) == NULL) {
390 WARN("invalid handle\n");
391 return MMSYSERR_INVALHANDLE;
393 if ((fdwSize & ~ACM_STREAMSIZEF_QUERYMASK) != 0) {
394 WARN("invalid flag\n");
395 return MMSYSERR_INVALFLAG;
398 *pdwOutputBytes = 0L;
400 switch (fdwSize & ACM_STREAMSIZEF_QUERYMASK) {
401 case ACM_STREAMSIZEF_DESTINATION:
402 adss.cbDstLength = cbInput;
403 adss.cbSrcLength = 0;
404 break;
405 case ACM_STREAMSIZEF_SOURCE:
406 adss.cbSrcLength = cbInput;
407 adss.cbDstLength = 0;
408 break;
409 default:
410 WARN("invalid flag\n");
411 return MMSYSERR_INVALFLAG;
414 adss.cbStruct = sizeof(adss);
415 adss.fdwSize = fdwSize;
416 ret = MSACM_Message((HACMDRIVER)was->pDrv, ACMDM_STREAM_SIZE,
417 (LPARAM)&was->drvInst, (LPARAM)&adss);
418 if (ret == MMSYSERR_NOERROR) {
419 switch (fdwSize & ACM_STREAMSIZEF_QUERYMASK) {
420 case ACM_STREAMSIZEF_DESTINATION:
421 *pdwOutputBytes = adss.cbSrcLength;
422 break;
423 case ACM_STREAMSIZEF_SOURCE:
424 *pdwOutputBytes = adss.cbDstLength;
425 break;
428 TRACE("=> (%d) [%u]\n", ret, *pdwOutputBytes);
429 return ret;
432 /***********************************************************************
433 * acmStreamUnprepareHeader (MSACM32.@)
435 MMRESULT WINAPI acmStreamUnprepareHeader(HACMSTREAM has, PACMSTREAMHEADER pash,
436 DWORD fdwUnprepare)
438 PWINE_ACMSTREAM was;
439 MMRESULT ret = MMSYSERR_NOERROR;
440 PACMDRVSTREAMHEADER padsh;
442 TRACE("(%p, %p, %d)\n", has, pash, fdwUnprepare);
444 if ((was = ACM_GetStream(has)) == NULL) {
445 WARN("invalid handle\n");
446 return MMSYSERR_INVALHANDLE;
448 if (!pash || pash->cbStruct < sizeof(ACMSTREAMHEADER)) {
449 WARN("invalid parameter\n");
450 return MMSYSERR_INVALPARAM;
452 if (!(pash->fdwStatus & ACMSTREAMHEADER_STATUSF_PREPARED)) {
453 WARN("unprepared header\n");
454 return ACMERR_UNPREPARED;
457 /* Note: the ACMSTREAMHEADER and ACMDRVSTREAMHEADER structs are of same
458 * size. some fields are private to msacm internals, and are exposed
459 * in ACMSTREAMHEADER in the dwReservedDriver array
461 padsh = (PACMDRVSTREAMHEADER)pash;
463 /* check that pointers have not been modified */
464 if (padsh->pbPreparedSrc != padsh->pbSrc ||
465 padsh->cbPreparedSrcLength < padsh->cbSrcLength ||
466 padsh->pbPreparedDst != padsh->pbDst ||
467 padsh->cbPreparedDstLength < padsh->cbDstLength) {
468 WARN("invalid parameter\n");
469 return MMSYSERR_INVALPARAM;
472 padsh->fdwConvert = fdwUnprepare;
474 ret = MSACM_Message((HACMDRIVER)was->pDrv, ACMDM_STREAM_UNPREPARE, (LPARAM)&was->drvInst, (LPARAM)padsh);
475 if (ret == MMSYSERR_NOERROR || ret == MMSYSERR_NOTSUPPORTED) {
476 ret = MMSYSERR_NOERROR;
477 padsh->fdwStatus &= ~(ACMSTREAMHEADER_STATUSF_INQUEUE|ACMSTREAMHEADER_STATUSF_PREPARED);
479 TRACE("=> (%d)\n", ret);
480 return ret;