ntoskrnl.exe: Implemented a couple of time-related APIs.
[wine/dibdrv.git] / dlls / msacm32 / pcmconverter.c
blob4b8fdfeb21a7006dfce250fb623c82b2543bccbc
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("(%d)\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 const 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;
378 typedef void (*PCM_CONVERT_KEEP_RATE)(const unsigned char*, int, unsigned char*);
380 static const PCM_CONVERT_KEEP_RATE PCM_ConvertKeepRate[16] = {
381 cvtSS88K, cvtSM88K, cvtMS88K, cvtMM88K,
382 cvtSS816K, cvtSM816K, cvtMS816K, cvtMM816K,
383 cvtSS168K, cvtSM168K, cvtMS168K, cvtMM168K,
384 cvtSS1616K, cvtSM1616K, cvtMS1616K, cvtMM1616K,
387 /* the conversion routines with rate conversion are labelled cvt<X><Y><N><M>C
388 * where :
389 * <X> is the (M)ono/(S)tereo configuration of input channel
390 * <Y> is the (M)ono/(S)tereo configuration of output channel
391 * <N> is the number of bits of input channel (8 or 16)
392 * <M> is the number of bits of output channel (8 or 16)
395 static void cvtSS88C(DWORD srcRate, const unsigned char* src, LPDWORD nsrc,
396 DWORD dstRate, unsigned char* dst, LPDWORD ndst)
398 DWORD error = dstRate / 2;
399 TRACE("(%d, %p, %p, %d, %p, %p)\n", srcRate, src, nsrc, dstRate, dst, ndst);
401 while ((*ndst)--) {
402 *dst++ = *src;
403 *dst++ = *src;
404 error = error + srcRate;
405 while (error > dstRate) {
406 src += 2;
407 (*nsrc)--;
408 if (*nsrc == 0)
409 return;
410 error = error - dstRate;
415 static void cvtSM88C(DWORD srcRate, const unsigned char* src, LPDWORD nsrc,
416 DWORD dstRate, unsigned char* dst, LPDWORD ndst)
418 DWORD error = dstRate / 2;
419 TRACE("(%d, %p, %p, %d, %p, %p)\n", srcRate, src, nsrc, dstRate, dst, ndst);
421 while ((*ndst)--) {
422 *dst++ = M8(src[0], src[1]);
423 error = error + srcRate;
424 while (error > dstRate) {
425 src += 2;
426 (*nsrc)--;
427 if (*nsrc == 0)
428 return;
429 error = error - dstRate;
434 static void cvtMS88C(DWORD srcRate, const unsigned char* src, LPDWORD nsrc,
435 DWORD dstRate, unsigned char* dst, LPDWORD ndst)
437 DWORD error = dstRate / 2;
438 TRACE("(%d, %p, %p, %d, %p, %p)\n", srcRate, src, nsrc, dstRate, dst, ndst);
440 while ((*ndst)--) {
441 *dst++ = *src;
442 *dst++ = *src;
443 error = error + srcRate;
444 while (error > dstRate) {
445 src++;
446 (*nsrc)--;
447 if (*nsrc == 0)
448 return;
449 error = error - dstRate;
454 static void cvtMM88C(DWORD srcRate, const unsigned char* src, LPDWORD nsrc,
455 DWORD dstRate, unsigned char* dst, LPDWORD ndst)
457 DWORD error = dstRate / 2;
458 TRACE("(%d, %p, %p, %d, %p, %p)\n", srcRate, src, nsrc, dstRate, dst, ndst);
460 while ((*ndst)--) {
461 *dst++ = *src;
462 error = error + srcRate;
463 while (error > dstRate) {
464 src++;
465 (*nsrc)--;
466 if (*nsrc==0)
467 return;
468 error = error - dstRate;
473 static void cvtSS816C(DWORD srcRate, const unsigned char* src, LPDWORD nsrc,
474 DWORD dstRate, unsigned char* dst, LPDWORD ndst)
476 DWORD error = dstRate / 2;
477 TRACE("(%d, %p, %p, %d, %p, %p)\n", srcRate, src, nsrc, dstRate, dst, ndst);
479 while ((*ndst)--) {
480 W16(dst, C816(src[0])); dst += 2;
481 W16(dst, C816(src[1])); dst += 2;
482 error = error + srcRate;
483 while (error > dstRate) {
484 src += 2;
485 (*nsrc)--;
486 if (*nsrc==0)
487 return;
488 error = error - dstRate;
493 static void cvtSM816C(DWORD srcRate, const unsigned char* src, LPDWORD nsrc,
494 DWORD dstRate, unsigned char* dst, LPDWORD ndst)
496 DWORD error = dstRate / 2;
497 TRACE("(%d, %p, %p, %d, %p, %p)\n", srcRate, src, nsrc, dstRate, dst, ndst);
499 while ((*ndst)--) {
500 W16(dst, M16(C816(src[0]), C816(src[1]))); dst += 2;
501 error = error + srcRate;
502 while (error > dstRate) {
503 src += 2;
504 (*nsrc)--;
505 if (*nsrc==0)
506 return;
507 error = error - dstRate;
512 static void cvtMS816C(DWORD srcRate, const unsigned char* src, LPDWORD nsrc,
513 DWORD dstRate, unsigned char* dst, LPDWORD ndst)
515 DWORD error = dstRate / 2;
516 TRACE("(%d, %p, %p, %d, %p, %p)\n", srcRate, src, nsrc, dstRate, dst, ndst);
518 while ((*ndst)--) {
519 W16(dst, C816(*src)); dst += 2;
520 W16(dst, C816(*src)); dst += 2;
521 error = error + srcRate;
522 while (error > dstRate) {
523 src++;
524 (*nsrc)--;
525 if (*nsrc==0)
526 return;
527 error = error - dstRate;
532 static void cvtMM816C(DWORD srcRate, const unsigned char* src, LPDWORD nsrc,
533 DWORD dstRate, unsigned char* dst, LPDWORD ndst)
535 DWORD error = dstRate / 2;
536 TRACE("(%d, %p, %p, %d, %p, %p)\n", srcRate, src, nsrc, dstRate, dst, ndst);
538 while ((*ndst)--) {
539 W16(dst, C816(*src)); dst += 2;
540 error = error + srcRate;
541 while (error > dstRate) {
542 src++;
543 (*nsrc)--;
544 if (*nsrc==0)
545 return;
546 error = error - dstRate;
551 static void cvtSS168C(DWORD srcRate, const unsigned char* src, LPDWORD nsrc,
552 DWORD dstRate, unsigned char* dst, LPDWORD ndst)
554 DWORD error = dstRate / 2;
555 TRACE("(%d, %p, %p, %d, %p, %p)\n", srcRate, src, nsrc, dstRate, dst, ndst);
557 while ((*ndst)--) {
558 *dst++ = C168(R16(src));
559 *dst++ = C168(R16(src + 2));
560 error = error + srcRate;
561 while (error > dstRate) {
562 src += 4;
563 (*nsrc)--;
564 if (*nsrc==0)
565 return;
566 error = error - dstRate;
571 static void cvtSM168C(DWORD srcRate, const unsigned char* src, LPDWORD nsrc,
572 DWORD dstRate, unsigned char* dst, LPDWORD ndst)
574 DWORD error = dstRate / 2;
575 TRACE("(%d, %p, %p, %d, %p, %p)\n", srcRate, src, nsrc, dstRate, dst, ndst);
577 while ((*ndst)--) {
578 *dst++ = C168(M16(R16(src), R16(src + 2)));
579 error = error + srcRate;
580 while (error > dstRate) {
581 src += 4;
582 (*nsrc)--;
583 if (*nsrc==0)
584 return;
585 error = error - dstRate;
590 static void cvtMS168C(DWORD srcRate, const unsigned char* src, LPDWORD nsrc,
591 DWORD dstRate, unsigned char* dst, LPDWORD ndst)
593 DWORD error = dstRate / 2;
594 TRACE("(%d, %p, %p, %d, %p, %p)\n", srcRate, src, nsrc, dstRate, dst, ndst);
596 while ((*ndst)--) {
597 *dst++ = C168(R16(src));
598 *dst++ = C168(R16(src));
599 error = error + srcRate;
600 while (error > dstRate) {
601 src += 2;
602 (*nsrc)--;
603 if (*nsrc==0)
604 return;
605 error = error - dstRate;
610 static void cvtMM168C(DWORD srcRate, const unsigned char* src, LPDWORD nsrc,
611 DWORD dstRate, unsigned char* dst, LPDWORD ndst)
613 DWORD error = dstRate / 2;
614 TRACE("(%d, %p, %p, %d, %p, %p)\n", srcRate, src, nsrc, dstRate, dst, ndst);
616 while ((*ndst)--) {
617 *dst++ = C168(R16(src));
618 error = error + srcRate;
619 while (error > dstRate) {
620 src += 2;
621 (*nsrc)--;
622 if (*nsrc == 0)
623 return;
624 error = error - dstRate;
629 static void cvtSS1616C(DWORD srcRate, const unsigned char* src, LPDWORD nsrc,
630 DWORD dstRate, unsigned char* dst, LPDWORD ndst)
632 DWORD error = dstRate / 2;
633 TRACE("(%d, %p, %p, %d, %p, %p)\n", srcRate, src, nsrc, dstRate, dst, ndst);
635 while ((*ndst)--) {
636 W16(dst, R16(src)); dst += 2;
637 W16(dst, R16(src)); dst += 2;
638 error = error + srcRate;
639 while (error > dstRate) {
640 src += 4;
641 (*nsrc)--;
642 if (*nsrc == 0)
643 return;
644 error = error - dstRate;
649 static void cvtSM1616C(DWORD srcRate, const unsigned char* src, LPDWORD nsrc,
650 DWORD dstRate, unsigned char* dst, LPDWORD ndst)
652 DWORD error = dstRate / 2;
653 TRACE("(%d, %p, %p, %d, %p, %p)\n", srcRate, src, nsrc, dstRate, dst, ndst);
655 while ((*ndst)--) {
656 W16(dst, M16(R16(src), R16(src + 2))); dst += 2;
657 error = error + srcRate;
658 while (error > dstRate) {
659 src += 4;
660 (*nsrc)--;
661 if (*nsrc == 0)
662 return;
663 error = error - dstRate;
668 static void cvtMS1616C(DWORD srcRate, const unsigned char* src, LPDWORD nsrc,
669 DWORD dstRate, unsigned char* dst, LPDWORD ndst)
671 DWORD error = dstRate / 2;
672 TRACE("(%d, %p, %p, %d, %p, %p)\n", srcRate, src, nsrc, dstRate, dst, ndst);
674 while((*ndst)--) {
675 W16(dst, R16(src)); dst += 2;
676 W16(dst, R16(src)); dst += 2;
677 error = error + srcRate;
678 while (error > dstRate) {
679 src += 2;
680 (*nsrc)--;
681 if (*nsrc == 0)
682 return;
683 error = error - dstRate;
688 static void cvtMM1616C(DWORD srcRate, const unsigned char* src, LPDWORD nsrc,
689 DWORD dstRate, unsigned char* dst, LPDWORD ndst)
691 DWORD error = dstRate / 2;
692 TRACE("(%d, %p, %p, %d, %p, %p)\n", srcRate, src, nsrc, dstRate, dst, ndst);
694 while ((*ndst)--) {
695 W16(dst, R16(src)); dst += 2;
696 error = error + srcRate;
697 while (error > dstRate) {
698 src += 2;
699 (*nsrc)--;
700 if (*nsrc == 0)
701 return;
702 error = error - dstRate;
707 typedef void (*PCM_CONVERT_CHANGE_RATE)(DWORD, const unsigned char*, LPDWORD, DWORD, unsigned char*, LPDWORD);
709 static const PCM_CONVERT_CHANGE_RATE PCM_ConvertChangeRate[16] = {
710 cvtSS88C, cvtSM88C, cvtMS88C, cvtMM88C,
711 cvtSS816C, cvtSM816C, cvtMS816C, cvtMM816C,
712 cvtSS168C, cvtSM168C, cvtMS168C, cvtMM168C,
713 cvtSS1616C, cvtSM1616C, cvtMS1616C, cvtMM1616C,
716 /***********************************************************************
717 * PCM_DriverDetails
720 static LRESULT PCM_DriverDetails(PACMDRIVERDETAILSW add)
722 TRACE("(%p)\n", add);
724 add->fccType = ACMDRIVERDETAILS_FCCTYPE_AUDIOCODEC;
725 add->fccComp = ACMDRIVERDETAILS_FCCCOMP_UNDEFINED;
726 add->wMid = 0xFF;
727 add->wPid = 0x00;
728 add->vdwACM = 0x01000000;
729 add->vdwDriver = 0x01000000;
730 add->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CONVERTER;
731 add->cFormatTags = 1;
732 add->cFilterTags = 0;
733 add->hicon = NULL;
734 MultiByteToWideChar( CP_ACP, 0, "WINE-PCM", -1,
735 add->szShortName, sizeof(add->szShortName)/sizeof(WCHAR) );
736 MultiByteToWideChar( CP_ACP, 0, "Wine PCM converter", -1,
737 add->szLongName, sizeof(add->szLongName)/sizeof(WCHAR) );
738 MultiByteToWideChar( CP_ACP, 0, "Brought to you by the Wine team...", -1,
739 add->szCopyright, sizeof(add->szCopyright)/sizeof(WCHAR) );
740 MultiByteToWideChar( CP_ACP, 0, "Refer to LICENSE file", -1,
741 add->szLicensing, sizeof(add->szLicensing)/sizeof(WCHAR) );
742 add->szFeatures[0] = 0;
744 return MMSYSERR_NOERROR;
747 /***********************************************************************
748 * PCM_FormatTagDetails
751 static LRESULT PCM_FormatTagDetails(PACMFORMATTAGDETAILSW aftd, DWORD dwQuery)
753 TRACE("(%p, %08x)\n", aftd, dwQuery);
755 switch (dwQuery) {
756 case ACM_FORMATTAGDETAILSF_INDEX:
757 if (aftd->dwFormatTagIndex != 0) {
758 WARN("not possible\n");
759 return ACMERR_NOTPOSSIBLE;
761 break;
762 case ACM_FORMATTAGDETAILSF_FORMATTAG:
763 if (aftd->dwFormatTag != WAVE_FORMAT_PCM) {
764 WARN("not possible\n");
765 return ACMERR_NOTPOSSIBLE;
767 break;
768 case ACM_FORMATTAGDETAILSF_LARGESTSIZE:
769 if (aftd->dwFormatTag != WAVE_FORMAT_UNKNOWN &&
770 aftd->dwFormatTag != WAVE_FORMAT_PCM) {
771 WARN("not possible\n");
772 return ACMERR_NOTPOSSIBLE;
774 break;
775 default:
776 WARN("Unsupported query %08x\n", dwQuery);
777 return MMSYSERR_NOTSUPPORTED;
780 aftd->dwFormatTagIndex = 0;
781 aftd->dwFormatTag = WAVE_FORMAT_PCM;
782 aftd->cbFormatSize = sizeof(PCMWAVEFORMAT);
783 aftd->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CONVERTER;
784 aftd->cStandardFormats = NUM_PCM_FORMATS;
785 aftd->szFormatTag[0] = 0;
787 return MMSYSERR_NOERROR;
790 /***********************************************************************
791 * PCM_FormatDetails
794 static LRESULT PCM_FormatDetails(PACMFORMATDETAILSW afd, DWORD dwQuery)
796 TRACE("(%p, %08x)\n", afd, dwQuery);
798 switch (dwQuery) {
799 case ACM_FORMATDETAILSF_FORMAT:
800 if (PCM_GetFormatIndex(afd->pwfx) == 0xFFFFFFFF) {
801 WARN("not possible\n");
802 return ACMERR_NOTPOSSIBLE;
804 break;
805 case ACM_FORMATDETAILSF_INDEX:
806 assert(afd->dwFormatIndex < NUM_PCM_FORMATS);
807 afd->pwfx->wFormatTag = WAVE_FORMAT_PCM;
808 afd->pwfx->nChannels = PCM_Formats[afd->dwFormatIndex].nChannels;
809 afd->pwfx->nSamplesPerSec = PCM_Formats[afd->dwFormatIndex].rate;
810 afd->pwfx->wBitsPerSample = PCM_Formats[afd->dwFormatIndex].nBits;
811 /* native MSACM uses a PCMWAVEFORMAT structure, so cbSize is not
812 * accessible afd->pwfx->cbSize = 0;
814 afd->pwfx->nBlockAlign =
815 (afd->pwfx->nChannels * afd->pwfx->wBitsPerSample) / 8;
816 afd->pwfx->nAvgBytesPerSec =
817 afd->pwfx->nSamplesPerSec * afd->pwfx->nBlockAlign;
818 break;
819 default:
820 WARN("Unsupported query %08x\n", dwQuery);
821 return MMSYSERR_NOTSUPPORTED;
824 afd->dwFormatTag = WAVE_FORMAT_PCM;
825 afd->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CONVERTER;
826 afd->szFormat[0] = 0; /* let MSACM format this for us... */
827 afd->cbwfx = sizeof(PCMWAVEFORMAT);
829 return MMSYSERR_NOERROR;
832 /***********************************************************************
833 * PCM_FormatSuggest
836 static LRESULT PCM_FormatSuggest(PACMDRVFORMATSUGGEST adfs)
838 TRACE("(%p)\n", adfs);
840 /* some tests ... */
841 if (adfs->cbwfxSrc < sizeof(PCMWAVEFORMAT) ||
842 adfs->cbwfxDst < sizeof(PCMWAVEFORMAT) ||
843 PCM_GetFormatIndex(adfs->pwfxSrc) == 0xFFFFFFFF) {
844 WARN("not possible\n");
845 return ACMERR_NOTPOSSIBLE;
848 /* is no suggestion for destination, then copy source value */
849 if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_NCHANNELS)) {
850 adfs->pwfxDst->nChannels = adfs->pwfxSrc->nChannels;
852 if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_NSAMPLESPERSEC)) {
853 adfs->pwfxDst->nSamplesPerSec = adfs->pwfxSrc->nSamplesPerSec;
855 if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_WBITSPERSAMPLE)) {
856 adfs->pwfxDst->wBitsPerSample = adfs->pwfxSrc->wBitsPerSample;
858 if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_WFORMATTAG)) {
859 if (adfs->pwfxSrc->wFormatTag != WAVE_FORMAT_PCM) {
860 WARN("not possible\n");
861 return ACMERR_NOTPOSSIBLE;
863 adfs->pwfxDst->wFormatTag = adfs->pwfxSrc->wFormatTag;
865 /* check if result is ok */
866 if (PCM_GetFormatIndex(adfs->pwfxDst) == 0xFFFFFFFF) {
867 WARN("not possible\n");
868 return ACMERR_NOTPOSSIBLE;
871 /* recompute other values */
872 adfs->pwfxDst->nBlockAlign = (adfs->pwfxDst->nChannels * adfs->pwfxDst->wBitsPerSample) / 8;
873 adfs->pwfxDst->nAvgBytesPerSec = adfs->pwfxDst->nSamplesPerSec * adfs->pwfxDst->nBlockAlign;
875 return MMSYSERR_NOERROR;
878 /***********************************************************************
879 * PCM_StreamOpen
882 static LRESULT PCM_StreamOpen(PACMDRVSTREAMINSTANCE adsi)
884 AcmPcmData* apd;
885 int idx = 0;
887 TRACE("(%p)\n", adsi);
889 assert(!(adsi->fdwOpen & ACM_STREAMOPENF_ASYNC));
891 apd = HeapAlloc(GetProcessHeap(), 0, sizeof(AcmPcmData));
892 if (apd == 0) {
893 WARN("no memory\n");
894 return MMSYSERR_NOMEM;
897 adsi->dwDriver = (DWORD)apd;
898 adsi->fdwDriver = 0;
900 if (adsi->pwfxSrc->wBitsPerSample == 16) idx += 8;
901 if (adsi->pwfxDst->wBitsPerSample == 16) idx += 4;
902 if (adsi->pwfxSrc->nChannels == 1) idx += 2;
903 if (adsi->pwfxDst->nChannels == 1) idx += 1;
905 if (adsi->pwfxSrc->nSamplesPerSec == adsi->pwfxDst->nSamplesPerSec) {
906 apd->cvt.cvtKeepRate = PCM_ConvertKeepRate[idx];
907 } else {
908 adsi->fdwDriver |= PCM_RESAMPLE;
909 apd->cvt.cvtChangeRate = PCM_ConvertChangeRate[idx];
912 return MMSYSERR_NOERROR;
915 /***********************************************************************
916 * PCM_StreamClose
919 static LRESULT PCM_StreamClose(PACMDRVSTREAMINSTANCE adsi)
921 TRACE("(%p)\n", adsi);
923 HeapFree(GetProcessHeap(), 0, (void*)adsi->dwDriver);
924 return MMSYSERR_NOERROR;
927 /***********************************************************************
928 * PCM_round
931 static inline DWORD PCM_round(DWORD a, DWORD b, DWORD c)
933 assert(c);
934 /* to be sure, always return an entire number of c... */
935 return ((double)a * (double)b + (double)c - 1) / (double)c;
938 /***********************************************************************
939 * PCM_StreamSize
942 static LRESULT PCM_StreamSize(PACMDRVSTREAMINSTANCE adsi, PACMDRVSTREAMSIZE adss)
944 DWORD srcMask = ~(adsi->pwfxSrc->nBlockAlign - 1);
945 DWORD dstMask = ~(adsi->pwfxDst->nBlockAlign - 1);
947 TRACE("(%p, %p)\n", adsi, adss);
949 switch (adss->fdwSize) {
950 case ACM_STREAMSIZEF_DESTINATION:
951 /* cbDstLength => cbSrcLength */
952 adss->cbSrcLength = PCM_round(adss->cbDstLength & dstMask,
953 adsi->pwfxSrc->nAvgBytesPerSec,
954 adsi->pwfxDst->nAvgBytesPerSec) & srcMask;
955 break;
956 case ACM_STREAMSIZEF_SOURCE:
957 /* cbSrcLength => cbDstLength */
958 adss->cbDstLength = PCM_round(adss->cbSrcLength & srcMask,
959 adsi->pwfxDst->nAvgBytesPerSec,
960 adsi->pwfxSrc->nAvgBytesPerSec) & dstMask;
961 break;
962 default:
963 WARN("Unsupported query %08x\n", adss->fdwSize);
964 return MMSYSERR_NOTSUPPORTED;
966 return MMSYSERR_NOERROR;
969 /***********************************************************************
970 * PCM_StreamConvert
973 static LRESULT PCM_StreamConvert(PACMDRVSTREAMINSTANCE adsi, PACMDRVSTREAMHEADER adsh)
975 AcmPcmData* apd = (AcmPcmData*)adsi->dwDriver;
976 DWORD nsrc = NUM_OF(adsh->cbSrcLength, adsi->pwfxSrc->nBlockAlign);
977 DWORD ndst = NUM_OF(adsh->cbDstLength, adsi->pwfxDst->nBlockAlign);
979 TRACE("(%p, %p)\n", adsi, adsh);
981 TRACE("nsrc=%d,adsh->cbSrcLength=%d\n", nsrc, adsh->cbSrcLength);
982 TRACE("ndst=%d,adsh->cbDstLength=%d\n", ndst, adsh->cbDstLength);
983 TRACE("src [wFormatTag=%u, nChannels=%u, nSamplesPerSec=%u, nAvgBytesPerSec=%u, nBlockAlign=%u, wBitsPerSample=%u, cbSize=%u]\n",
984 adsi->pwfxSrc->wFormatTag, adsi->pwfxSrc->nChannels, adsi->pwfxSrc->nSamplesPerSec, adsi->pwfxSrc->nAvgBytesPerSec,
985 adsi->pwfxSrc->nBlockAlign, adsi->pwfxSrc->wBitsPerSample, adsi->pwfxSrc->cbSize);
986 TRACE("dst [wFormatTag=%u, nChannels=%u, nSamplesPerSec=%u, nAvgBytesPerSec=%u, nBlockAlign=%u, wBitsPerSample=%u, cbSize=%u]\n",
987 adsi->pwfxDst->wFormatTag, adsi->pwfxDst->nChannels, adsi->pwfxDst->nSamplesPerSec, adsi->pwfxDst->nAvgBytesPerSec,
988 adsi->pwfxDst->nBlockAlign, adsi->pwfxDst->wBitsPerSample, adsi->pwfxDst->cbSize);
990 if (adsh->fdwConvert &
991 ~(ACM_STREAMCONVERTF_BLOCKALIGN|
992 ACM_STREAMCONVERTF_END|
993 ACM_STREAMCONVERTF_START)) {
994 FIXME("Unsupported fdwConvert (%08x), ignoring it\n", adsh->fdwConvert);
996 /* ACM_STREAMCONVERTF_BLOCKALIGN
997 * currently all conversions are block aligned, so do nothing for this flag
998 * ACM_STREAMCONVERTF_END
999 * no pending data, so do nothing for this flag
1001 if ((adsh->fdwConvert & ACM_STREAMCONVERTF_START) &&
1002 (adsi->fdwDriver & PCM_RESAMPLE)) {
1005 /* do the job */
1006 if (adsi->fdwDriver & PCM_RESAMPLE) {
1007 DWORD nsrc2 = nsrc;
1008 DWORD ndst2 = ndst;
1009 apd->cvt.cvtChangeRate((DWORD)adsi->pwfxSrc->nSamplesPerSec, adsh->pbSrc, &nsrc2,
1010 (DWORD)adsi->pwfxDst->nSamplesPerSec, adsh->pbDst, &ndst2);
1011 nsrc -= nsrc2;
1012 ndst -= ndst2;
1013 } else {
1014 if (nsrc < ndst) ndst = nsrc; else nsrc = ndst;
1016 /* nsrc is now equal to ndst */
1017 apd->cvt.cvtKeepRate(adsh->pbSrc, nsrc, adsh->pbDst);
1020 adsh->cbSrcLengthUsed = nsrc * adsi->pwfxSrc->nBlockAlign;
1021 adsh->cbDstLengthUsed = ndst * adsi->pwfxDst->nBlockAlign;
1023 return MMSYSERR_NOERROR;
1026 /**************************************************************************
1027 * DriverProc (MSACM32.@)
1029 LRESULT CALLBACK PCM_DriverProc(DWORD_PTR dwDevID, HDRVR hDriv, UINT wMsg,
1030 LPARAM dwParam1, LPARAM dwParam2)
1032 TRACE("(%08lx %p %u %08lx %08lx);\n",
1033 dwDevID, hDriv, wMsg, dwParam1, dwParam2);
1035 switch (wMsg) {
1036 case DRV_LOAD: return 1;
1037 case DRV_FREE: return 1;
1038 case DRV_OPEN: return PCM_drvOpen((LPSTR)dwParam1, (PACMDRVOPENDESCW)dwParam2);
1039 case DRV_CLOSE: return PCM_drvClose(dwDevID);
1040 case DRV_ENABLE: return 1;
1041 case DRV_DISABLE: return 1;
1042 case DRV_QUERYCONFIGURE: return 1;
1043 case DRV_CONFIGURE: MessageBoxA(0, "MSACM PCM filter !", "Wine Driver", MB_OK); return 1;
1044 case DRV_INSTALL: return DRVCNF_RESTART;
1045 case DRV_REMOVE: return DRVCNF_RESTART;
1047 case ACMDM_DRIVER_NOTIFY:
1048 /* no caching from other ACM drivers is done so far */
1049 return MMSYSERR_NOERROR;
1051 case ACMDM_DRIVER_DETAILS:
1052 return PCM_DriverDetails((PACMDRIVERDETAILSW)dwParam1);
1054 case ACMDM_FORMATTAG_DETAILS:
1055 return PCM_FormatTagDetails((PACMFORMATTAGDETAILSW)dwParam1, dwParam2);
1057 case ACMDM_FORMAT_DETAILS:
1058 return PCM_FormatDetails((PACMFORMATDETAILSW)dwParam1, dwParam2);
1060 case ACMDM_FORMAT_SUGGEST:
1061 return PCM_FormatSuggest((PACMDRVFORMATSUGGEST)dwParam1);
1063 case ACMDM_STREAM_OPEN:
1064 return PCM_StreamOpen((PACMDRVSTREAMINSTANCE)dwParam1);
1066 case ACMDM_STREAM_CLOSE:
1067 return PCM_StreamClose((PACMDRVSTREAMINSTANCE)dwParam1);
1069 case ACMDM_STREAM_SIZE:
1070 return PCM_StreamSize((PACMDRVSTREAMINSTANCE)dwParam1, (PACMDRVSTREAMSIZE)dwParam2);
1072 case ACMDM_STREAM_CONVERT:
1073 return PCM_StreamConvert((PACMDRVSTREAMINSTANCE)dwParam1, (PACMDRVSTREAMHEADER)dwParam2);
1075 case ACMDM_HARDWARE_WAVE_CAPS_INPUT:
1076 case ACMDM_HARDWARE_WAVE_CAPS_OUTPUT:
1077 /* this converter is not a hardware driver */
1078 case ACMDM_FILTERTAG_DETAILS:
1079 case ACMDM_FILTER_DETAILS:
1080 /* this converter is not a filter */
1081 case ACMDM_STREAM_RESET:
1082 /* only needed for asynchronous driver... we aren't, so just say it */
1083 case ACMDM_STREAM_PREPARE:
1084 case ACMDM_STREAM_UNPREPARE:
1085 /* nothing special to do here... so don't do anything */
1086 return MMSYSERR_NOTSUPPORTED;
1088 default:
1089 return DefDriverProc(dwDevID, hDriv, wMsg, dwParam1, dwParam2);