rpcrt4: Initialise pStubMsg->MemorySize to zero before calling ComplexStructMemorySiz...
[wine/multimedia.git] / dlls / msacm32 / pcmconverter.c
blob1a6a065016c2c796de5f3f4354ce7cf3149f548b
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
3 /*
4 * MSACM32 library
6 * Copyright 2000 Eric Pouech
7 * Copyright 2004 Robert Reif
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
23 * FIXME / TODO list
24 * + get rid of hack for PCM_DriverProc (msacm32.dll shouldn't export
25 * a DriverProc, but this would require implementing a generic
26 * embedded driver handling scheme in msacm32.dll which isn't done yet
29 #include "config.h"
31 #include <assert.h>
32 #include <stdarg.h>
33 #include <string.h>
35 #include "windef.h"
36 #include "winbase.h"
37 #include "mmsystem.h"
38 #define NOBITMAP
39 #include "mmreg.h"
40 #include "msacm.h"
41 #include "wingdi.h"
42 #include "winnls.h"
43 #include "winuser.h"
45 #include "msacmdrv.h"
46 #include "wineacm.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(msacm);
52 /***********************************************************************
53 * PCM_drvOpen
55 static DWORD PCM_drvOpen(LPCSTR str, PACMDRVOPENDESCW adod)
57 TRACE("(%p, %p)\n", str, adod);
59 return (adod == NULL) ||
60 (adod->fccType == ACMDRIVERDETAILS_FCCTYPE_AUDIOCODEC &&
61 adod->fccComp == ACMDRIVERDETAILS_FCCCOMP_UNDEFINED);
64 /***********************************************************************
65 * PCM_drvClose
67 static DWORD PCM_drvClose(DWORD dwDevID)
69 TRACE("(%ld)\n", dwDevID);
71 return 1;
74 #define NUM_PCM_FORMATS (sizeof(PCM_Formats) / sizeof(PCM_Formats[0]))
75 #define NUM_OF(a,b) ((a)/(b))
77 /* flags for fdwDriver */
78 #define PCM_RESAMPLE 1
80 /* data used while converting */
81 typedef struct tagAcmPcmData {
82 /* conversion routine, depending if rate conversion is required */
83 union {
84 void (*cvtKeepRate)(const unsigned char*, int, unsigned char*);
85 void (*cvtChangeRate)(DWORD, const unsigned char*, LPDWORD,
86 DWORD, unsigned char*, LPDWORD);
87 } cvt;
88 } AcmPcmData;
90 /* table to list all supported formats... those are the basic ones. this
91 * also helps given a unique index to each of the supported formats
93 static struct {
94 int nChannels;
95 int nBits;
96 int rate;
97 } PCM_Formats[] = {
98 {1, 8, 8000}, {2, 8, 8000}, {1, 16, 8000}, {2, 16, 8000},
99 {1, 8, 11025}, {2, 8, 11025}, {1, 16, 11025}, {2, 16, 11025},
100 {1, 8, 22050}, {2, 8, 22050}, {1, 16, 22050}, {2, 16, 22050},
101 {1, 8, 44100}, {2, 8, 44100}, {1, 16, 44100}, {2, 16, 44100},
102 {1, 8, 48000}, {2, 8, 48000}, {1, 16, 48000}, {2, 16, 48000},
103 {1, 8, 96000}, {2, 8, 96000}, {1, 16, 96000}, {2, 16, 96000}
106 /***********************************************************************
107 * PCM_GetFormatIndex
109 static DWORD PCM_GetFormatIndex(LPWAVEFORMATEX wfx)
111 unsigned int i;
112 TRACE("(%p)\n", wfx);
114 for (i = 0; i < NUM_PCM_FORMATS; i++) {
115 if (wfx->nChannels == PCM_Formats[i].nChannels &&
116 wfx->nSamplesPerSec == PCM_Formats[i].rate &&
117 wfx->wBitsPerSample == PCM_Formats[i].nBits)
118 return i;
120 return 0xFFFFFFFF;
123 /* PCM Conversions:
125 * parameters:
126 * + 8 bit unsigned vs 16 bit signed
127 * + mono vs stereo (1 or 2 channels)
128 * + sampling rate (8.0, 11.025, 22.05, 44.1 kHz are defined, but algo
129 * shall work in all cases)
131 * mono => stereo: copy the same sample on Left & Right channels
132 * stereo => mono: use the sum of Left & Right channels
135 /***********************************************************************
136 * C816
138 * Converts a 8 bit sample to a 16 bit one
140 static inline short C816(unsigned char b)
142 return (b - 128) << 8;
145 /***********************************************************************
146 * C168
148 * Converts a 16 bit sample to a 8 bit one (data loss !!)
150 static inline unsigned char C168(short s)
152 return HIBYTE(s) ^ (unsigned char)0x80;
155 /***********************************************************************
156 * R16
158 * Read a 16 bit sample (correctly handles endianess)
160 static inline short R16(const unsigned char* src)
162 return (short)((unsigned short)src[0] | ((unsigned short)src[1] << 8));
165 /***********************************************************************
166 * W16
168 * Write a 16 bit sample (correctly handles endianess)
170 static inline void W16(unsigned char* dst, short s)
172 dst[0] = LOBYTE(s);
173 dst[1] = HIBYTE(s);
176 /***********************************************************************
177 * M16
179 * Convert the (l,r) 16 bit stereo sample into a 16 bit mono
180 * (takes the sum of the two values)
182 static inline short M16(short l, short r)
184 int sum = l + r;
186 /* clip sum to saturation */
187 if (sum > 32767)
188 sum = 32767;
189 else if (sum < -32768)
190 sum = -32768;
192 return sum;
195 /***********************************************************************
196 * M8
198 * Convert the (l,r) 8 bit stereo sample into a 8 bit mono
199 * (takes the sum of the two values)
201 static inline unsigned char M8(unsigned char a, unsigned char b)
203 int l = a - 128;
204 int r = b - 128;
205 int sum = (l + r) + 128;
207 /* clip sum to saturation */
208 if (sum > 0xff)
209 sum = 0xff;
210 else if (sum < 0)
211 sum = 0;
213 return sum;
216 /* the conversion routines without rate conversion are labelled cvt<X><Y><N><M>K
217 * where :
218 * <X> is the (M)ono/(S)tereo configuration of input channel
219 * <Y> is the (M)ono/(S)tereo configuration of output channel
220 * <N> is the number of bits of input channel (8 or 16)
221 * <M> is the number of bits of output channel (8 or 16)
223 * in the parameters, ns is always the number of samples, so the size of input
224 * buffer (resp output buffer) is ns * (<X> == 'Mono' ? 1:2) * (<N> == 8 ? 1:2)
227 static void cvtMM88K(const unsigned char* src, int ns, unsigned char* dst)
229 TRACE("(%p, %d, %p)\n", src, ns, dst);
230 memcpy(dst, src, ns);
233 static void cvtSS88K(const unsigned char* src, int ns, unsigned char* dst)
235 TRACE("(%p, %d, %p)\n", src, ns, dst);
236 memcpy(dst, src, ns * 2);
239 static void cvtMM1616K(const unsigned char* src, int ns, unsigned char* dst)
241 TRACE("(%p, %d, %p)\n", src, ns, dst);
242 memcpy(dst, src, ns * 2);
245 static void cvtSS1616K(const unsigned char* src, int ns, unsigned char* dst)
247 TRACE("(%p, %d, %p)\n", src, ns, dst);
248 memcpy(dst, src, ns * 4);
251 static void cvtMS88K(const unsigned char* src, int ns, unsigned char* dst)
253 TRACE("(%p, %d, %p)\n", src, ns, dst);
255 while (ns--) {
256 *dst++ = *src;
257 *dst++ = *src++;
261 static void cvtMS816K(const unsigned char* src, int ns, unsigned char* dst)
263 short v;
264 TRACE("(%p, %d, %p)\n", src, ns, dst);
266 while (ns--) {
267 v = C816(*src++);
268 W16(dst, v); dst += 2;
269 W16(dst, v); dst += 2;
273 static void cvtMS168K(const unsigned char* src, int ns, unsigned char* dst)
275 unsigned char v;
276 TRACE("(%p, %d, %p)\n", src, ns, dst);
278 while (ns--) {
279 v = C168(R16(src)); src += 2;
280 *dst++ = v;
281 *dst++ = v;
285 static void cvtMS1616K(const unsigned char* src, int ns, unsigned char* dst)
287 short v;
288 TRACE("(%p, %d, %p)\n", src, ns, dst);
290 while (ns--) {
291 v = R16(src); src += 2;
292 W16(dst, v); dst += 2;
293 W16(dst, v); dst += 2;
297 static void cvtSM88K(const unsigned char* src, int ns, unsigned char* dst)
299 TRACE("(%p, %d, %p)\n", src, ns, dst);
301 while (ns--) {
302 *dst++ = M8(src[0], src[1]);
303 src += 2;
307 static void cvtSM816K(const unsigned char* src, int ns, unsigned char* dst)
309 short v;
310 TRACE("(%p, %d, %p)\n", src, ns, dst);
312 while (ns--) {
313 v = M16(C816(src[0]), C816(src[1]));
314 src += 2;
315 W16(dst, v); dst += 2;
319 static void cvtSM168K(const unsigned char* src, int ns, unsigned char* dst)
321 TRACE("(%p, %d, %p)\n", src, ns, dst);
323 while (ns--) {
324 *dst++ = C168(M16(R16(src), R16(src + 2)));
325 src += 4;
329 static void cvtSM1616K(const unsigned char* src, int ns, unsigned char* dst)
331 TRACE("(%p, %d, %p)\n", src, ns, dst);
333 while (ns--) {
334 W16(dst, M16(R16(src),R16(src+2))); dst += 2;
335 src += 4;
339 static void cvtMM816K(const unsigned char* src, int ns, unsigned char* dst)
341 TRACE("(%p, %d, %p)\n", src, ns, dst);
343 while (ns--) {
344 W16(dst, C816(*src++)); dst += 2;
348 static void cvtSS816K(const unsigned char* src, int ns, unsigned char* dst)
350 TRACE("(%p, %d, %p)\n", src, ns, dst);
352 while (ns--) {
353 W16(dst, C816(*src++)); dst += 2;
354 W16(dst, C816(*src++)); dst += 2;
358 static void cvtMM168K(const unsigned char* src, int ns, unsigned char* dst)
360 TRACE("(%p, %d, %p)\n", src, ns, dst);
362 while (ns--) {
363 *dst++ = C168(R16(src)); src += 2;
367 static void cvtSS168K(const unsigned char* src, int ns, unsigned char* dst)
369 TRACE("(%p, %d, %p)\n", src, ns, dst);
371 while (ns--) {
372 *dst++ = C168(R16(src)); src += 2;
373 *dst++ = C168(R16(src)); src += 2;
377 static void (*PCM_ConvertKeepRate[16])(const unsigned char*, int, unsigned char*) = {
378 cvtSS88K, cvtSM88K, cvtMS88K, cvtMM88K,
379 cvtSS816K, cvtSM816K, cvtMS816K, cvtMM816K,
380 cvtSS168K, cvtSM168K, cvtMS168K, cvtMM168K,
381 cvtSS1616K, cvtSM1616K, cvtMS1616K, cvtMM1616K,
384 /* the conversion routines with rate conversion are labelled cvt<X><Y><N><M>C
385 * where :
386 * <X> is the (M)ono/(S)tereo configuration of input channel
387 * <Y> is the (M)ono/(S)tereo configuration of output channel
388 * <N> is the number of bits of input channel (8 or 16)
389 * <M> is the number of bits of output channel (8 or 16)
392 static void cvtSS88C(DWORD srcRate, const unsigned char* src, LPDWORD nsrc,
393 DWORD dstRate, unsigned char* dst, LPDWORD ndst)
395 DWORD error = dstRate / 2;
396 TRACE("(%ld, %p, %p, %ld, %p, %p)\n", srcRate, src, nsrc, dstRate, dst, ndst);
398 while ((*ndst)--) {
399 *dst++ = *src;
400 *dst++ = *src;
401 error = error + srcRate;
402 while (error > dstRate) {
403 src += 2;
404 (*nsrc)--;
405 if (*nsrc == 0)
406 return;
407 error = error - dstRate;
412 static void cvtSM88C(DWORD srcRate, const unsigned char* src, LPDWORD nsrc,
413 DWORD dstRate, unsigned char* dst, LPDWORD ndst)
415 DWORD error = dstRate / 2;
416 TRACE("(%ld, %p, %p, %ld, %p, %p)\n", srcRate, src, nsrc, dstRate, dst, ndst);
418 while ((*ndst)--) {
419 *dst++ = M8(src[0], src[1]);
420 error = error + srcRate;
421 while (error > dstRate) {
422 src += 2;
423 (*nsrc)--;
424 if (*nsrc == 0)
425 return;
426 error = error - dstRate;
431 static void cvtMS88C(DWORD srcRate, const unsigned char* src, LPDWORD nsrc,
432 DWORD dstRate, unsigned char* dst, LPDWORD ndst)
434 DWORD error = dstRate / 2;
435 TRACE("(%ld, %p, %p, %ld, %p, %p)\n", srcRate, src, nsrc, dstRate, dst, ndst);
437 while ((*ndst)--) {
438 *dst++ = *src;
439 *dst++ = *src;
440 error = error + srcRate;
441 while (error > dstRate) {
442 src++;
443 (*nsrc)--;
444 if (*nsrc == 0)
445 return;
446 error = error - dstRate;
451 static void cvtMM88C(DWORD srcRate, const unsigned char* src, LPDWORD nsrc,
452 DWORD dstRate, unsigned char* dst, LPDWORD ndst)
454 DWORD error = dstRate / 2;
455 TRACE("(%ld, %p, %p, %ld, %p, %p)\n", srcRate, src, nsrc, dstRate, dst, ndst);
457 while ((*ndst)--) {
458 *dst++ = *src;
459 error = error + srcRate;
460 while (error > dstRate) {
461 src++;
462 (*nsrc)--;
463 if (*nsrc==0)
464 return;
465 error = error - dstRate;
470 static void cvtSS816C(DWORD srcRate, const unsigned char* src, LPDWORD nsrc,
471 DWORD dstRate, unsigned char* dst, LPDWORD ndst)
473 DWORD error = dstRate / 2;
474 TRACE("(%ld, %p, %p, %ld, %p, %p)\n", srcRate, src, nsrc, dstRate, dst, ndst);
476 while ((*ndst)--) {
477 W16(dst, C816(src[0])); dst += 2;
478 W16(dst, C816(src[1])); dst += 2;
479 error = error + srcRate;
480 while (error > dstRate) {
481 src += 2;
482 (*nsrc)--;
483 if (*nsrc==0)
484 return;
485 error = error - dstRate;
490 static void cvtSM816C(DWORD srcRate, const unsigned char* src, LPDWORD nsrc,
491 DWORD dstRate, unsigned char* dst, LPDWORD ndst)
493 DWORD error = dstRate / 2;
494 TRACE("(%ld, %p, %p, %ld, %p, %p)\n", srcRate, src, nsrc, dstRate, dst, ndst);
496 while ((*ndst)--) {
497 W16(dst, M16(C816(src[0]), C816(src[1]))); dst += 2;
498 error = error + srcRate;
499 while (error > dstRate) {
500 src += 2;
501 (*nsrc)--;
502 if (*nsrc==0)
503 return;
504 error = error - dstRate;
509 static void cvtMS816C(DWORD srcRate, const unsigned char* src, LPDWORD nsrc,
510 DWORD dstRate, unsigned char* dst, LPDWORD ndst)
512 DWORD error = dstRate / 2;
513 TRACE("(%ld, %p, %p, %ld, %p, %p)\n", srcRate, src, nsrc, dstRate, dst, ndst);
515 while ((*ndst)--) {
516 W16(dst, C816(*src)); dst += 2;
517 W16(dst, C816(*src)); dst += 2;
518 error = error + srcRate;
519 while (error > dstRate) {
520 src++;
521 (*nsrc)--;
522 if (*nsrc==0)
523 return;
524 error = error - dstRate;
529 static void cvtMM816C(DWORD srcRate, const unsigned char* src, LPDWORD nsrc,
530 DWORD dstRate, unsigned char* dst, LPDWORD ndst)
532 DWORD error = dstRate / 2;
533 TRACE("(%ld, %p, %p, %ld, %p, %p)\n", srcRate, src, nsrc, dstRate, dst, ndst);
535 while ((*ndst)--) {
536 W16(dst, C816(*src)); dst += 2;
537 error = error + srcRate;
538 while (error > dstRate) {
539 src++;
540 (*nsrc)--;
541 if (*nsrc==0)
542 return;
543 error = error - dstRate;
548 static void cvtSS168C(DWORD srcRate, const unsigned char* src, LPDWORD nsrc,
549 DWORD dstRate, unsigned char* dst, LPDWORD ndst)
551 DWORD error = dstRate / 2;
552 TRACE("(%ld, %p, %p, %ld, %p, %p)\n", srcRate, src, nsrc, dstRate, dst, ndst);
554 while ((*ndst)--) {
555 *dst++ = C168(R16(src));
556 *dst++ = C168(R16(src + 2));
557 error = error + srcRate;
558 while (error > dstRate) {
559 src += 4;
560 (*nsrc)--;
561 if (*nsrc==0)
562 return;
563 error = error - dstRate;
568 static void cvtSM168C(DWORD srcRate, const unsigned char* src, LPDWORD nsrc,
569 DWORD dstRate, unsigned char* dst, LPDWORD ndst)
571 DWORD error = dstRate / 2;
572 TRACE("(%ld, %p, %p, %ld, %p, %p)\n", srcRate, src, nsrc, dstRate, dst, ndst);
574 while ((*ndst)--) {
575 *dst++ = C168(M16(R16(src), R16(src + 2)));
576 error = error + srcRate;
577 while (error > dstRate) {
578 src += 4;
579 (*nsrc)--;
580 if (*nsrc==0)
581 return;
582 error = error - dstRate;
587 static void cvtMS168C(DWORD srcRate, const unsigned char* src, LPDWORD nsrc,
588 DWORD dstRate, unsigned char* dst, LPDWORD ndst)
590 DWORD error = dstRate / 2;
591 TRACE("(%ld, %p, %p, %ld, %p, %p)\n", srcRate, src, nsrc, dstRate, dst, ndst);
593 while ((*ndst)--) {
594 *dst++ = C168(R16(src));
595 *dst++ = C168(R16(src));
596 error = error + srcRate;
597 while (error > dstRate) {
598 src += 2;
599 (*nsrc)--;
600 if (*nsrc==0)
601 return;
602 error = error - dstRate;
607 static void cvtMM168C(DWORD srcRate, const unsigned char* src, LPDWORD nsrc,
608 DWORD dstRate, unsigned char* dst, LPDWORD ndst)
610 DWORD error = dstRate / 2;
611 TRACE("(%ld, %p, %p, %ld, %p, %p)\n", srcRate, src, nsrc, dstRate, dst, ndst);
613 while ((*ndst)--) {
614 *dst++ = C168(R16(src));
615 error = error + srcRate;
616 while (error > dstRate) {
617 src += 2;
618 (*nsrc)--;
619 if (*nsrc == 0)
620 return;
621 error = error - dstRate;
626 static void cvtSS1616C(DWORD srcRate, const unsigned char* src, LPDWORD nsrc,
627 DWORD dstRate, unsigned char* dst, LPDWORD ndst)
629 DWORD error = dstRate / 2;
630 TRACE("(%ld, %p, %p, %ld, %p, %p)\n", srcRate, src, nsrc, dstRate, dst, ndst);
632 while ((*ndst)--) {
633 W16(dst, R16(src)); dst += 2;
634 W16(dst, R16(src)); dst += 2;
635 error = error + srcRate;
636 while (error > dstRate) {
637 src += 4;
638 (*nsrc)--;
639 if (*nsrc == 0)
640 return;
641 error = error - dstRate;
646 static void cvtSM1616C(DWORD srcRate, const unsigned char* src, LPDWORD nsrc,
647 DWORD dstRate, unsigned char* dst, LPDWORD ndst)
649 DWORD error = dstRate / 2;
650 TRACE("(%ld, %p, %p, %ld, %p, %p)\n", srcRate, src, nsrc, dstRate, dst, ndst);
652 while ((*ndst)--) {
653 W16(dst, M16(R16(src), R16(src + 2))); dst += 2;
654 error = error + srcRate;
655 while (error > dstRate) {
656 src += 4;
657 (*nsrc)--;
658 if (*nsrc == 0)
659 return;
660 error = error - dstRate;
665 static void cvtMS1616C(DWORD srcRate, const unsigned char* src, LPDWORD nsrc,
666 DWORD dstRate, unsigned char* dst, LPDWORD ndst)
668 DWORD error = dstRate / 2;
669 TRACE("(%ld, %p, %p, %ld, %p, %p)\n", srcRate, src, nsrc, dstRate, dst, ndst);
671 while((*ndst)--) {
672 W16(dst, R16(src)); dst += 2;
673 W16(dst, R16(src)); dst += 2;
674 error = error + srcRate;
675 while (error > dstRate) {
676 src += 2;
677 (*nsrc)--;
678 if (*nsrc == 0)
679 return;
680 error = error - dstRate;
685 static void cvtMM1616C(DWORD srcRate, const unsigned char* src, LPDWORD nsrc,
686 DWORD dstRate, unsigned char* dst, LPDWORD ndst)
688 DWORD error = dstRate / 2;
689 TRACE("(%ld, %p, %p, %ld, %p, %p)\n", srcRate, src, nsrc, dstRate, dst, ndst);
691 while ((*ndst)--) {
692 W16(dst, R16(src)); dst += 2;
693 error = error + srcRate;
694 while (error > dstRate) {
695 src += 2;
696 (*nsrc)--;
697 if (*nsrc == 0)
698 return;
699 error = error - dstRate;
704 static void (*PCM_ConvertChangeRate[16])(DWORD srcRate, const unsigned char* src, LPDWORD nsrc,
705 DWORD dstRate, unsigned char* dst, LPDWORD ndst) = {
706 cvtSS88C, cvtSM88C, cvtMS88C, cvtMM88C,
707 cvtSS816C, cvtSM816C, cvtMS816C, cvtMM816C,
708 cvtSS168C, cvtSM168C, cvtMS168C, cvtMM168C,
709 cvtSS1616C, cvtSM1616C, cvtMS1616C, cvtMM1616C,
712 /***********************************************************************
713 * PCM_DriverDetails
716 static LRESULT PCM_DriverDetails(PACMDRIVERDETAILSW add)
718 TRACE("(%p)\n", add);
720 add->fccType = ACMDRIVERDETAILS_FCCTYPE_AUDIOCODEC;
721 add->fccComp = ACMDRIVERDETAILS_FCCCOMP_UNDEFINED;
722 add->wMid = 0xFF;
723 add->wPid = 0x00;
724 add->vdwACM = 0x01000000;
725 add->vdwDriver = 0x01000000;
726 add->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CONVERTER;
727 add->cFormatTags = 1;
728 add->cFilterTags = 0;
729 add->hicon = NULL;
730 MultiByteToWideChar( CP_ACP, 0, "WINE-PCM", -1,
731 add->szShortName, sizeof(add->szShortName)/sizeof(WCHAR) );
732 MultiByteToWideChar( CP_ACP, 0, "Wine PCM converter", -1,
733 add->szLongName, sizeof(add->szLongName)/sizeof(WCHAR) );
734 MultiByteToWideChar( CP_ACP, 0, "Brought to you by the Wine team...", -1,
735 add->szCopyright, sizeof(add->szCopyright)/sizeof(WCHAR) );
736 MultiByteToWideChar( CP_ACP, 0, "Refer to LICENSE file", -1,
737 add->szLicensing, sizeof(add->szLicensing)/sizeof(WCHAR) );
738 add->szFeatures[0] = 0;
740 return MMSYSERR_NOERROR;
743 /***********************************************************************
744 * PCM_FormatTagDetails
747 static LRESULT PCM_FormatTagDetails(PACMFORMATTAGDETAILSW aftd, DWORD dwQuery)
749 TRACE("(%p, %08lx)\n", aftd, dwQuery);
751 switch (dwQuery) {
752 case ACM_FORMATTAGDETAILSF_INDEX:
753 if (aftd->dwFormatTagIndex != 0) {
754 WARN("not possible\n");
755 return ACMERR_NOTPOSSIBLE;
757 break;
758 case ACM_FORMATTAGDETAILSF_FORMATTAG:
759 if (aftd->dwFormatTag != WAVE_FORMAT_PCM) {
760 WARN("not possible\n");
761 return ACMERR_NOTPOSSIBLE;
763 break;
764 case ACM_FORMATTAGDETAILSF_LARGESTSIZE:
765 if (aftd->dwFormatTag != WAVE_FORMAT_UNKNOWN &&
766 aftd->dwFormatTag != WAVE_FORMAT_PCM) {
767 WARN("not possible\n");
768 return ACMERR_NOTPOSSIBLE;
770 break;
771 default:
772 WARN("Unsupported query %08lx\n", dwQuery);
773 return MMSYSERR_NOTSUPPORTED;
776 aftd->dwFormatTagIndex = 0;
777 aftd->dwFormatTag = WAVE_FORMAT_PCM;
778 aftd->cbFormatSize = sizeof(PCMWAVEFORMAT);
779 aftd->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CONVERTER;
780 aftd->cStandardFormats = NUM_PCM_FORMATS;
781 aftd->szFormatTag[0] = 0;
783 return MMSYSERR_NOERROR;
786 /***********************************************************************
787 * PCM_FormatDetails
790 static LRESULT PCM_FormatDetails(PACMFORMATDETAILSW afd, DWORD dwQuery)
792 TRACE("(%p, %08lx)\n", afd, dwQuery);
794 switch (dwQuery) {
795 case ACM_FORMATDETAILSF_FORMAT:
796 if (PCM_GetFormatIndex(afd->pwfx) == 0xFFFFFFFF) {
797 WARN("not possible\n");
798 return ACMERR_NOTPOSSIBLE;
800 break;
801 case ACM_FORMATDETAILSF_INDEX:
802 assert(afd->dwFormatIndex < NUM_PCM_FORMATS);
803 afd->pwfx->wFormatTag = WAVE_FORMAT_PCM;
804 afd->pwfx->nChannels = PCM_Formats[afd->dwFormatIndex].nChannels;
805 afd->pwfx->nSamplesPerSec = PCM_Formats[afd->dwFormatIndex].rate;
806 afd->pwfx->wBitsPerSample = PCM_Formats[afd->dwFormatIndex].nBits;
807 /* native MSACM uses a PCMWAVEFORMAT structure, so cbSize is not
808 * accessible afd->pwfx->cbSize = 0;
810 afd->pwfx->nBlockAlign =
811 (afd->pwfx->nChannels * afd->pwfx->wBitsPerSample) / 8;
812 afd->pwfx->nAvgBytesPerSec =
813 afd->pwfx->nSamplesPerSec * afd->pwfx->nBlockAlign;
814 break;
815 default:
816 WARN("Unsupported query %08lx\n", dwQuery);
817 return MMSYSERR_NOTSUPPORTED;
820 afd->dwFormatTag = WAVE_FORMAT_PCM;
821 afd->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CONVERTER;
822 afd->szFormat[0] = 0; /* let MSACM format this for us... */
823 afd->cbwfx = sizeof(PCMWAVEFORMAT);
825 return MMSYSERR_NOERROR;
828 /***********************************************************************
829 * PCM_FormatSuggest
832 static LRESULT PCM_FormatSuggest(PACMDRVFORMATSUGGEST adfs)
834 TRACE("(%p)\n", adfs);
836 /* some tests ... */
837 if (adfs->cbwfxSrc < sizeof(PCMWAVEFORMAT) ||
838 adfs->cbwfxDst < sizeof(PCMWAVEFORMAT) ||
839 PCM_GetFormatIndex(adfs->pwfxSrc) == 0xFFFFFFFF) {
840 WARN("not possible\n");
841 return ACMERR_NOTPOSSIBLE;
844 /* is no suggestion for destination, then copy source value */
845 if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_NCHANNELS)) {
846 adfs->pwfxDst->nChannels = adfs->pwfxSrc->nChannels;
848 if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_NSAMPLESPERSEC)) {
849 adfs->pwfxDst->nSamplesPerSec = adfs->pwfxSrc->nSamplesPerSec;
851 if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_WBITSPERSAMPLE)) {
852 adfs->pwfxDst->wBitsPerSample = adfs->pwfxSrc->wBitsPerSample;
854 if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_WFORMATTAG)) {
855 if (adfs->pwfxSrc->wFormatTag != WAVE_FORMAT_PCM) {
856 WARN("not possible\n");
857 return ACMERR_NOTPOSSIBLE;
859 adfs->pwfxDst->wFormatTag = adfs->pwfxSrc->wFormatTag;
861 /* check if result is ok */
862 if (PCM_GetFormatIndex(adfs->pwfxDst) == 0xFFFFFFFF) {
863 WARN("not possible\n");
864 return ACMERR_NOTPOSSIBLE;
867 /* recompute other values */
868 adfs->pwfxDst->nBlockAlign = (adfs->pwfxDst->nChannels * adfs->pwfxDst->wBitsPerSample) / 8;
869 adfs->pwfxDst->nAvgBytesPerSec = adfs->pwfxDst->nSamplesPerSec * adfs->pwfxDst->nBlockAlign;
871 return MMSYSERR_NOERROR;
874 /***********************************************************************
875 * PCM_StreamOpen
878 static LRESULT PCM_StreamOpen(PACMDRVSTREAMINSTANCE adsi)
880 AcmPcmData* apd;
881 int idx = 0;
883 TRACE("(%p)\n", adsi);
885 assert(!(adsi->fdwOpen & ACM_STREAMOPENF_ASYNC));
887 apd = HeapAlloc(GetProcessHeap(), 0, sizeof(AcmPcmData));
888 if (apd == 0) {
889 WARN("no memory\n");
890 return MMSYSERR_NOMEM;
893 adsi->dwDriver = (DWORD)apd;
894 adsi->fdwDriver = 0;
896 if (adsi->pwfxSrc->wBitsPerSample == 16) idx += 8;
897 if (adsi->pwfxDst->wBitsPerSample == 16) idx += 4;
898 if (adsi->pwfxSrc->nChannels == 1) idx += 2;
899 if (adsi->pwfxDst->nChannels == 1) idx += 1;
901 if (adsi->pwfxSrc->nSamplesPerSec == adsi->pwfxDst->nSamplesPerSec) {
902 apd->cvt.cvtKeepRate = PCM_ConvertKeepRate[idx];
903 } else {
904 adsi->fdwDriver |= PCM_RESAMPLE;
905 apd->cvt.cvtChangeRate = PCM_ConvertChangeRate[idx];
908 return MMSYSERR_NOERROR;
911 /***********************************************************************
912 * PCM_StreamClose
915 static LRESULT PCM_StreamClose(PACMDRVSTREAMINSTANCE adsi)
917 TRACE("(%p)\n", adsi);
919 HeapFree(GetProcessHeap(), 0, (void*)adsi->dwDriver);
920 return MMSYSERR_NOERROR;
923 /***********************************************************************
924 * PCM_round
927 static inline DWORD PCM_round(DWORD a, DWORD b, DWORD c)
929 assert(c);
930 /* to be sure, always return an entire number of c... */
931 return ((double)a * (double)b + (double)c - 1) / (double)c;
934 /***********************************************************************
935 * PCM_StreamSize
938 static LRESULT PCM_StreamSize(PACMDRVSTREAMINSTANCE adsi, PACMDRVSTREAMSIZE adss)
940 DWORD srcMask = ~(adsi->pwfxSrc->nBlockAlign - 1);
941 DWORD dstMask = ~(adsi->pwfxDst->nBlockAlign - 1);
943 TRACE("(%p, %p)\n", adsi, adss);
945 switch (adss->fdwSize) {
946 case ACM_STREAMSIZEF_DESTINATION:
947 /* cbDstLength => cbSrcLength */
948 adss->cbSrcLength = PCM_round(adss->cbDstLength & dstMask,
949 adsi->pwfxSrc->nAvgBytesPerSec,
950 adsi->pwfxDst->nAvgBytesPerSec) & srcMask;
951 break;
952 case ACM_STREAMSIZEF_SOURCE:
953 /* cbSrcLength => cbDstLength */
954 adss->cbDstLength = PCM_round(adss->cbSrcLength & srcMask,
955 adsi->pwfxDst->nAvgBytesPerSec,
956 adsi->pwfxSrc->nAvgBytesPerSec) & dstMask;
957 break;
958 default:
959 WARN("Unsupported query %08lx\n", adss->fdwSize);
960 return MMSYSERR_NOTSUPPORTED;
962 return MMSYSERR_NOERROR;
965 /***********************************************************************
966 * PCM_StreamConvert
969 static LRESULT PCM_StreamConvert(PACMDRVSTREAMINSTANCE adsi, PACMDRVSTREAMHEADER adsh)
971 AcmPcmData* apd = (AcmPcmData*)adsi->dwDriver;
972 DWORD nsrc = NUM_OF(adsh->cbSrcLength, adsi->pwfxSrc->nBlockAlign);
973 DWORD ndst = NUM_OF(adsh->cbDstLength, adsi->pwfxDst->nBlockAlign);
975 TRACE("(%p, %p)\n", adsi, adsh);
977 TRACE("nsrc=%ld,adsh->cbSrcLength=%ld\n", nsrc, adsh->cbSrcLength);
978 TRACE("ndst=%ld,adsh->cbDstLength=%ld\n", ndst, adsh->cbDstLength);
979 TRACE("src [wFormatTag=%u, nChannels=%u, nSamplesPerSec=%lu, nAvgBytesPerSec=%lu, nBlockAlign=%u, wBitsPerSample=%u, cbSize=%u]\n",
980 adsi->pwfxSrc->wFormatTag, adsi->pwfxSrc->nChannels, adsi->pwfxSrc->nSamplesPerSec, adsi->pwfxSrc->nAvgBytesPerSec,
981 adsi->pwfxSrc->nBlockAlign, adsi->pwfxSrc->wBitsPerSample, adsi->pwfxSrc->cbSize);
982 TRACE("dst [wFormatTag=%u, nChannels=%u, nSamplesPerSec=%lu, nAvgBytesPerSec=%lu, nBlockAlign=%u, wBitsPerSample=%u, cbSize=%u]\n",
983 adsi->pwfxDst->wFormatTag, adsi->pwfxDst->nChannels, adsi->pwfxDst->nSamplesPerSec, adsi->pwfxDst->nAvgBytesPerSec,
984 adsi->pwfxDst->nBlockAlign, adsi->pwfxDst->wBitsPerSample, adsi->pwfxDst->cbSize);
986 if (adsh->fdwConvert &
987 ~(ACM_STREAMCONVERTF_BLOCKALIGN|
988 ACM_STREAMCONVERTF_END|
989 ACM_STREAMCONVERTF_START)) {
990 FIXME("Unsupported fdwConvert (%08lx), ignoring it\n", adsh->fdwConvert);
992 /* ACM_STREAMCONVERTF_BLOCKALIGN
993 * currently all conversions are block aligned, so do nothing for this flag
994 * ACM_STREAMCONVERTF_END
995 * no pending data, so do nothing for this flag
997 if ((adsh->fdwConvert & ACM_STREAMCONVERTF_START) &&
998 (adsi->fdwDriver & PCM_RESAMPLE)) {
1001 /* do the job */
1002 if (adsi->fdwDriver & PCM_RESAMPLE) {
1003 DWORD nsrc2 = nsrc;
1004 DWORD ndst2 = ndst;
1005 apd->cvt.cvtChangeRate((DWORD)adsi->pwfxSrc->nSamplesPerSec, adsh->pbSrc, &nsrc2,
1006 (DWORD)adsi->pwfxDst->nSamplesPerSec, adsh->pbDst, &ndst2);
1007 nsrc -= nsrc2;
1008 ndst -= ndst2;
1009 } else {
1010 if (nsrc < ndst) ndst = nsrc; else nsrc = ndst;
1012 /* nsrc is now equal to ndst */
1013 apd->cvt.cvtKeepRate(adsh->pbSrc, nsrc, adsh->pbDst);
1016 adsh->cbSrcLengthUsed = nsrc * adsi->pwfxSrc->nBlockAlign;
1017 adsh->cbDstLengthUsed = ndst * adsi->pwfxDst->nBlockAlign;
1019 return MMSYSERR_NOERROR;
1022 /**************************************************************************
1023 * DriverProc (MSACM32.@)
1025 LRESULT CALLBACK PCM_DriverProc(DWORD_PTR dwDevID, HDRVR hDriv, UINT wMsg,
1026 LPARAM dwParam1, LPARAM dwParam2)
1028 TRACE("(%08lx %p %u %08lx %08lx);\n",
1029 dwDevID, hDriv, wMsg, dwParam1, dwParam2);
1031 switch (wMsg) {
1032 case DRV_LOAD: return 1;
1033 case DRV_FREE: return 1;
1034 case DRV_OPEN: return PCM_drvOpen((LPSTR)dwParam1, (PACMDRVOPENDESCW)dwParam2);
1035 case DRV_CLOSE: return PCM_drvClose(dwDevID);
1036 case DRV_ENABLE: return 1;
1037 case DRV_DISABLE: return 1;
1038 case DRV_QUERYCONFIGURE: return 1;
1039 case DRV_CONFIGURE: MessageBoxA(0, "MSACM PCM filter !", "Wine Driver", MB_OK); return 1;
1040 case DRV_INSTALL: return DRVCNF_RESTART;
1041 case DRV_REMOVE: return DRVCNF_RESTART;
1043 case ACMDM_DRIVER_NOTIFY:
1044 /* no caching from other ACM drivers is done so far */
1045 return MMSYSERR_NOERROR;
1047 case ACMDM_DRIVER_DETAILS:
1048 return PCM_DriverDetails((PACMDRIVERDETAILSW)dwParam1);
1050 case ACMDM_FORMATTAG_DETAILS:
1051 return PCM_FormatTagDetails((PACMFORMATTAGDETAILSW)dwParam1, dwParam2);
1053 case ACMDM_FORMAT_DETAILS:
1054 return PCM_FormatDetails((PACMFORMATDETAILSW)dwParam1, dwParam2);
1056 case ACMDM_FORMAT_SUGGEST:
1057 return PCM_FormatSuggest((PACMDRVFORMATSUGGEST)dwParam1);
1059 case ACMDM_STREAM_OPEN:
1060 return PCM_StreamOpen((PACMDRVSTREAMINSTANCE)dwParam1);
1062 case ACMDM_STREAM_CLOSE:
1063 return PCM_StreamClose((PACMDRVSTREAMINSTANCE)dwParam1);
1065 case ACMDM_STREAM_SIZE:
1066 return PCM_StreamSize((PACMDRVSTREAMINSTANCE)dwParam1, (PACMDRVSTREAMSIZE)dwParam2);
1068 case ACMDM_STREAM_CONVERT:
1069 return PCM_StreamConvert((PACMDRVSTREAMINSTANCE)dwParam1, (PACMDRVSTREAMHEADER)dwParam2);
1071 case ACMDM_HARDWARE_WAVE_CAPS_INPUT:
1072 case ACMDM_HARDWARE_WAVE_CAPS_OUTPUT:
1073 /* this converter is not a hardware driver */
1074 case ACMDM_FILTERTAG_DETAILS:
1075 case ACMDM_FILTER_DETAILS:
1076 /* this converter is not a filter */
1077 case ACMDM_STREAM_RESET:
1078 /* only needed for asynchronous driver... we aren't, so just say it */
1079 case ACMDM_STREAM_PREPARE:
1080 case ACMDM_STREAM_UNPREPARE:
1081 /* nothing special to do here... so don't do anything */
1082 return MMSYSERR_NOTSUPPORTED;
1084 default:
1085 return DefDriverProc(dwDevID, hDriv, wMsg, dwParam1, dwParam2);