'used' should be set even if we are only computing the necessary
[wine/testsucceed.git] / dlls / msacm / pcmconverter.c
blobf368538f8b2b5d1274c08c4d28238dc6d235bd05
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
3 /*
4 * MSACM32 library
6 * Copyright 2000 Eric Pouech
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * FIXME / TODO list
23 * + most of the computation should be done in fixed point arithmetic
24 * instead of floating point (16 bits for integral part, and 16 bits
25 * for fractional part for example)
26 * + implement PCM_FormatSuggest function
27 * + get rid of hack for PCM_DriverProc (msacm32.dll shouldn't export
28 * a DriverProc, but this would require implementing a generic
29 * embedded driver handling scheme in msacm32.dll which isn't done yet
32 #include "config.h"
34 #include <assert.h>
35 #include <string.h>
37 #include "msacm.h"
38 #include "winbase.h"
39 #include "wingdi.h"
40 #include "winnls.h"
41 #include "winuser.h"
43 #include "msacmdrv.h"
44 #include "wineacm.h"
46 #include "wine/debug.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(msacm);
50 /***********************************************************************
51 * PCM_drvOpen
53 static DWORD PCM_drvOpen(LPCSTR str, PACMDRVOPENDESCW adod)
55 return (adod == NULL) ||
56 (adod->fccType == ACMDRIVERDETAILS_FCCTYPE_AUDIOCODEC &&
57 adod->fccComp == ACMDRIVERDETAILS_FCCCOMP_UNDEFINED);
60 /***********************************************************************
61 * PCM_drvClose
63 static DWORD PCM_drvClose(DWORD dwDevID)
65 return 1;
68 #define NUM_PCM_FORMATS (sizeof(PCM_Formats) / sizeof(PCM_Formats[0]))
69 #define NUM_OF(a,b) (((a)+(b)-1)/(b))
71 /* flags for fdwDriver */
72 #define PCM_RESAMPLE 1
74 /* data used while converting */
75 typedef struct tagAcmPcmData {
76 /* conversion routine, depending if rate conversion is required */
77 union {
78 void (*cvtKeepRate)(const unsigned char*, int, unsigned char*);
79 void (*cvtChangeRate)(struct tagAcmPcmData*, const unsigned char*,
80 LPDWORD, unsigned char*, LPDWORD);
81 } cvt;
82 /* the following fields are used only with rate conversion) */
83 DWORD srcPos; /* position in source stream */
84 double dstPos; /* position in destination stream */
85 double dstIncr; /* value to increment dst stream when src stream
86 is incremented by 1 */
87 /* last source stream value read */
88 union {
89 unsigned char b; /* 8 bit value */
90 short s; /* 16 bit value */
91 } last[2]; /* two channels max (stereo) */
92 } AcmPcmData;
94 /* table to list all supported formats... those are the basic ones. this
95 * also helps given a unique index to each of the supported formats
97 static struct {
98 int nChannels;
99 int nBits;
100 int rate;
101 } PCM_Formats[] = {
102 {1, 8, 8000}, {2, 8, 8000}, {1, 16, 8000}, {2, 16, 8000},
103 {1, 8, 11025}, {2, 8, 11025}, {1, 16, 11025}, {2, 16, 11025},
104 {1, 8, 22050}, {2, 8, 22050}, {1, 16, 22050}, {2, 16, 22050},
105 {1, 8, 44100}, {2, 8, 44100}, {1, 16, 44100}, {2, 16, 44100},
108 /***********************************************************************
109 * PCM_GetFormatIndex
111 static DWORD PCM_GetFormatIndex(LPWAVEFORMATEX wfx)
113 int i;
115 for (i = 0; i < NUM_PCM_FORMATS; i++) {
116 if (wfx->nChannels == PCM_Formats[i].nChannels &&
117 wfx->nSamplesPerSec == PCM_Formats[i].rate &&
118 wfx->wBitsPerSample == PCM_Formats[i].nBits)
119 return i;
121 return 0xFFFFFFFF;
124 /* PCM Conversions:
126 * parameters:
127 * + 8 bit unsigned vs 16 bit signed
128 * + mono vs stereo (1 or 2 channels)
129 * + sampling rate (8.0, 11.025, 22.05, 44.1 kHz are defined, but algo shall work
130 * in all cases)
132 * mono => stereo: copy the same sample on Left & Right channels
133 * stereo =) mono: use the average value of samples from Left & Right channels
134 * resampling; we lookup for each destination sample the two source adjacent samples
135 * were src <= dst < src+1 (dst is increased by a fractional value which is
136 * equivalent to the increment by one on src); then we use a linear
137 * interpolation between src and src+1
140 /***********************************************************************
141 * C816
143 * Converts a 8 bit sample to a 16 bit one
145 static inline short C816(unsigned char b)
147 return (short)(b ^ 0x80) * 256;
150 /***********************************************************************
151 * C168
153 * Converts a 16 bit sample to a 8 bit one (data loss !!)
155 static inline unsigned char C168(short s)
157 return HIBYTE(s) ^ (unsigned char)0x80;
160 /***********************************************************************
161 * R16
163 * Read a 16 bit sample (correctly handles endianess)
165 static inline short R16(const unsigned char* src)
167 return (short)((unsigned short)src[0] | ((unsigned short)src[1] << 8));
170 /***********************************************************************
171 * W16
173 * Write a 16 bit sample (correctly handles endianess)
175 static inline void W16(unsigned char* dst, short s)
177 dst[0] = LOBYTE(s);
178 dst[1] = HIBYTE(s);
181 /***********************************************************************
182 * M16
184 * Convert the (l,r) 16 bit stereo sample into a 16 bit mono
185 * (takes the mid-point of the two values)
187 static inline short M16(short l, short r)
189 return (l + r) / 2;
192 /***********************************************************************
193 * M8
195 * Convert the (l,r) 8 bit stereo sample into a 8 bit mono
196 * (takes the mid-point of the two values)
198 static inline unsigned char M8(unsigned char a, unsigned char b)
200 return (unsigned char)((a + b) / 2);
203 /* the conversion routines without rate conversion are labelled cvt<X><Y><N><M>K
204 * where :
205 * <X> is the (M)ono/(S)tereo configuration of input channel
206 * <Y> is the (M)ono/(S)tereo configuration of output channel
207 * <N> is the number of bits of input channel (8 or 16)
208 * <M> is the number of bits of output channel (8 or 16)
210 * in the parameters, ns is always the number of samples, so the size of input
211 * buffer (resp output buffer) is ns * (<X> == 'Mono' ? 1:2) * (<N> == 8 ? 1:2)
214 static void cvtMM88K(const unsigned char* src, int ns, unsigned char* dst)
216 memcpy(dst, src, ns);
219 static void cvtSS88K(const unsigned char* src, int ns, unsigned char* dst)
221 memcpy(dst, src, ns * 2);
224 static void cvtMM1616K(const unsigned char* src, int ns, unsigned char* dst)
226 memcpy(dst, src, ns * 2);
229 static void cvtSS1616K(const unsigned char* src, int ns, unsigned char* dst)
231 memcpy(dst, src, ns * 4);
234 static void cvtMS88K(const unsigned char* src, int ns, unsigned char* dst)
236 while (ns--) {
237 *dst++ = *src;
238 *dst++ = *src++;
242 static void cvtMS816K(const unsigned char* src, int ns, unsigned char* dst)
244 short v;
246 while (ns--) {
247 v = C816(*src++);
248 W16(dst, v); dst += 2;
249 W16(dst, v); dst += 2;
253 static void cvtMS168K(const unsigned char* src, int ns, unsigned char* dst)
255 unsigned char v;
257 while (ns--) {
258 v = C168(R16(src)); src += 2;
259 *dst++ = v;
260 *dst++ = v;
264 static void cvtMS1616K(const unsigned char* src, int ns, unsigned char* dst)
266 short v;
268 while (ns--) {
269 v = R16(src); src += 2;
270 W16(dst, v); dst += 2;
271 W16(dst, v); dst += 2;
275 static void cvtSM88K(const unsigned char* src, int ns, unsigned char* dst)
277 while (ns--) {
278 *dst++ = M8(src[0], src[1]);
279 src += 2;
283 static void cvtSM816K(const unsigned char* src, int ns, unsigned char* dst)
285 short v;
287 while (ns--) {
288 v = M16(C816(src[0]), C816(src[1]));
289 src += 2;
290 W16(dst, v); dst += 2;
294 static void cvtSM168K(const unsigned char* src, int ns, unsigned char* dst)
296 while (ns--) {
297 *dst++ = C168(M16(R16(src), R16(src + 2)));
298 src += 4;
302 static void cvtSM1616K(const unsigned char* src, int ns, unsigned char* dst)
304 while (ns--) {
305 W16(dst, M16(R16(src),R16(src+2))); dst += 2;
306 src += 4;
310 static void cvtMM816K(const unsigned char* src, int ns, unsigned char* dst)
312 while (ns--) {
313 W16(dst, C816(*src++)); dst += 2;
317 static void cvtSS816K(const unsigned char* src, int ns, unsigned char* dst)
319 while (ns--) {
320 W16(dst, C816(*src++)); dst += 2;
321 W16(dst, C816(*src++)); dst += 2;
325 static void cvtMM168K(const unsigned char* src, int ns, unsigned char* dst)
327 while (ns--) {
328 *dst++ = C168(R16(src)); src += 2;
332 static void cvtSS168K(const unsigned char* src, int ns, unsigned char* dst)
334 while (ns--) {
335 *dst++ = C168(R16(src)); src += 2;
336 *dst++ = C168(R16(src)); src += 2;
340 static void (*PCM_ConvertKeepRate[16])(const unsigned char*, int, unsigned char*) = {
341 cvtSS88K, cvtSM88K, cvtMS88K, cvtMM88K,
342 cvtSS816K, cvtSM816K, cvtMS816K, cvtMM816K,
343 cvtSS168K, cvtSM168K, cvtMS168K, cvtMM168K,
344 cvtSS1616K, cvtSM1616K, cvtMS1616K, cvtMM1616K,
347 /***********************************************************************
350 * Interpolate the value at r (r in ]0, 1]) between the two points v1 and v2
351 * Linear interpolation is used
353 static inline double I(double v1, double v2, double r)
355 if (0.0 >= r || r > 1.0) FIXME("r!! %f\n", r);
356 return (1.0 - r) * v1 + r * v2;
359 static void cvtSS88C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
360 unsigned char* dst, LPDWORD ndst)
362 double r;
364 while (*nsrc != 0 && *ndst != 0) {
365 while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
366 if (*nsrc == 0) return;
367 apd->last[0].b = *src++;
368 apd->last[1].b = *src++;
369 apd->srcPos++;
370 (*nsrc)--;
372 /* now do the interpolation */
373 *dst++ = I(apd->last[0].b, src[0], r);
374 *dst++ = I(apd->last[1].b, src[1], r);
375 apd->dstPos += apd->dstIncr;
376 (*ndst)--;
380 /* the conversion routines with rate conversion are labelled cvt<X><Y><N><M>C
381 * where :
382 * <X> is the (M)ono/(S)tereo configuration of input channel
383 * <Y> is the (M)ono/(S)tereo configuration of output channel
384 * <N> is the number of bits of input channel (8 or 16)
385 * <M> is the number of bits of output channel (8 or 16)
388 static void cvtSM88C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
389 unsigned char* dst, LPDWORD ndst)
391 double r;
393 while (*nsrc != 0 && *ndst != 0) {
394 while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
395 if (*nsrc == 0) return;
396 apd->last[0].b = *src++;
397 apd->last[1].b = *src++;
398 apd->srcPos++;
399 (*nsrc)--;
401 /* now do the interpolation */
402 *dst++ = I(M8(apd->last[0].b, apd->last[1].b), M8(src[0], src[1]), r);
403 apd->dstPos += apd->dstIncr;
404 (*ndst)--;
408 static void cvtMS88C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
409 unsigned char* dst, LPDWORD ndst)
411 double r;
413 while (*nsrc != 0 && *ndst != 0) {
414 while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
415 if (*nsrc == 0) return;
416 apd->last[0].b = *src++;
417 apd->srcPos++;
418 (*nsrc)--;
420 /* now do the interpolation */
421 dst[0] = dst[1] = I(apd->last[0].b, src[0], r);
422 dst += 2;
423 apd->dstPos += apd->dstIncr;
424 (*ndst)--;
428 static void cvtMM88C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
429 unsigned char* dst, LPDWORD ndst)
431 double r;
433 while (*nsrc != 0 && *ndst != 0) {
434 while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
435 if (*nsrc == 0) return;
436 apd->last[0].b = *src++;
437 apd->srcPos++;
438 (*nsrc)--;
440 /* now do the interpolation */
441 *dst++ = I(apd->last[0].b, src[0], r);
442 apd->dstPos += apd->dstIncr;
443 (*ndst)--;
447 static void cvtSS816C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
448 unsigned char* dst, LPDWORD ndst)
450 double r;
452 while (*nsrc != 0 && *ndst != 0) {
453 while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
454 if (*nsrc == 0) return;
455 apd->last[0].b = *src++;
456 apd->last[1].b = *src++;
457 apd->srcPos++;
458 (*nsrc)--;
460 /* now do the interpolation */
461 W16(dst, I(C816(apd->last[0].b), C816(src[0]), r)); dst += 2;
462 W16(dst, I(C816(apd->last[1].b), C816(src[1]), r)); dst += 2;
463 apd->dstPos += apd->dstIncr;
464 (*ndst)--;
468 static void cvtSM816C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
469 unsigned char* dst, LPDWORD ndst)
471 double r;
473 while (*nsrc != 0 && *ndst != 0) {
474 while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
475 if (*nsrc == 0) return;
476 apd->last[0].b = *src++;
477 apd->last[1].b = *src++;
478 apd->srcPos++;
479 (*nsrc)--;
481 /* now do the interpolation */
482 W16(dst, I(M16(C816(apd->last[0].b), C816(apd->last[1].b)),
483 M16(C816(src[0]), C816(src[1])), r));
484 dst += 2;
485 apd->dstPos += apd->dstIncr;
486 (*ndst)--;
490 static void cvtMS816C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
491 unsigned char* dst, LPDWORD ndst)
493 double r;
494 short v;
496 while (*nsrc != 0 && *ndst != 0) {
497 while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
498 if (*nsrc == 0) return;
499 apd->last[0].b = *src++;
500 apd->srcPos++;
501 (*nsrc)--;
503 /* now do the interpolation */
504 v = I(C816(apd->last[0].b), C816(src[0]), r);
505 W16(dst, v); dst += 2;
506 W16(dst, v); dst += 2;
507 apd->dstPos += apd->dstIncr;
508 (*ndst)--;
512 static void cvtMM816C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
513 unsigned char* dst, LPDWORD ndst)
515 double r;
517 while (*nsrc != 0 && *ndst != 0) {
518 while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
519 if (*nsrc == 0) return;
520 apd->last[0].b = *src++;
521 apd->srcPos++;
522 (*nsrc)--;
524 /* now do the interpolation */
525 W16(dst, I(C816(apd->last[0].b), C816(src[0]), r));
526 dst += 2;
527 apd->dstPos += apd->dstIncr;
528 (*ndst)--;
532 static void cvtSS168C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
533 unsigned char* dst, LPDWORD ndst)
535 double r;
537 while (*nsrc != 0 && *ndst != 0) {
538 while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
539 if (*nsrc == 0) return;
540 apd->last[0].s = R16(src); src += 2;
541 apd->last[1].s = R16(src); src += 2;
542 apd->srcPos++;
543 (*nsrc)--;
545 /* now do the interpolation */
546 *dst++ = C168(I(apd->last[0].s, R16(src) , r));
547 *dst++ = C168(I(apd->last[1].s, R16(src+2), r));
548 apd->dstPos += apd->dstIncr;
549 (*ndst)--;
553 static void cvtSM168C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
554 unsigned char* dst, LPDWORD ndst)
556 double r;
558 while (*nsrc != 0 && *ndst != 0) {
559 while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
560 if (*nsrc == 0) return;
561 apd->last[0].s = R16(src); src += 2;
562 apd->last[1].s = R16(src); src += 2;
563 apd->srcPos++;
564 (*nsrc)--;
566 /* now do the interpolation */
567 *dst++ = C168(I(M16(apd->last[0].s, apd->last[1].s),
568 M16(R16(src), R16(src + 2)), r));
569 apd->dstPos += apd->dstIncr;
570 (*ndst)--;
575 static void cvtMS168C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
576 unsigned char* dst, LPDWORD ndst)
578 double r;
580 while (*nsrc != 0 && *ndst != 0) {
581 while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
582 if (*nsrc == 0) return;
583 apd->last[0].s = R16(src); src += 2;
584 apd->srcPos++;
585 (*nsrc)--;
587 /* now do the interpolation */
588 dst[0] = dst[1] = C168(I(apd->last[0].s, R16(src), r)); dst += 2;
589 apd->dstPos += apd->dstIncr;
590 (*ndst)--;
595 static void cvtMM168C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
596 unsigned char* dst, LPDWORD ndst)
598 double r;
600 while (*nsrc != 0 && *ndst != 0) {
601 while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
602 if (*nsrc == 0) return;
603 apd->last[0].s = R16(src); src += 2;
604 apd->srcPos++;
605 (*nsrc)--;
607 /* now do the interpolation */
608 *dst++ = C168(I(apd->last[0].s, R16(src), r));
609 apd->dstPos += apd->dstIncr;
610 (*ndst)--;
614 static void cvtSS1616C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
615 unsigned char* dst, LPDWORD ndst)
617 double r;
619 while (*nsrc != 0 && *ndst != 0) {
620 while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
621 if (*nsrc == 0) return;
622 apd->last[0].s = R16(src); src += 2;
623 apd->last[1].s = R16(src); src += 2;
624 apd->srcPos++;
625 (*nsrc)--;
627 /* now do the interpolation */
628 W16(dst, I(apd->last[0].s, R16(src) , r)); dst += 2;
629 W16(dst, I(apd->last[1].s, R16(src+2), r)); dst += 2;
630 apd->dstPos += apd->dstIncr;
631 (*ndst)--;
635 static void cvtSM1616C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
636 unsigned char* dst, LPDWORD ndst)
638 double r;
640 while (*nsrc != 0 && *ndst != 0) {
641 while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
642 if (*nsrc == 0) return;
643 apd->last[0].s = R16(src); src += 2;
644 apd->last[1].s = R16(src); src += 2;
645 apd->srcPos++;
646 (*nsrc)--;
648 /* now do the interpolation */
649 W16(dst, I(M16(apd->last[0].s, apd->last[1].s),
650 M16(R16(src), R16(src+2)), r));
651 dst += 2;
652 apd->dstPos += apd->dstIncr;
653 (*ndst)--;
657 static void cvtMS1616C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
658 unsigned char* dst, LPDWORD ndst)
660 double r;
661 short v;
663 while (*nsrc != 0 && *ndst != 0) {
664 while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
665 if (*nsrc == 0) return;
666 apd->last[0].s = R16(src); src += 2;
667 apd->srcPos++;
668 (*nsrc)--;
670 /* now do the interpolation */
671 v = I(apd->last[0].s, R16(src), r);
672 W16(dst, v); dst += 2;
673 W16(dst, v); dst += 2;
674 apd->dstPos += apd->dstIncr;
675 (*ndst)--;
679 static void cvtMM1616C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
680 unsigned char* dst, LPDWORD ndst)
682 double r;
684 while (*nsrc != 0 && *ndst != 0) {
685 while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
686 if (*nsrc == 0) return;
687 apd->last[0].s = R16(src); src += 2;
688 apd->srcPos++;
689 (*nsrc)--;
691 /* now do the interpolation */
692 W16(dst, I(apd->last[0].s, R16(src), r)); dst += 2;
693 apd->dstPos += apd->dstIncr;
694 (*ndst)--;
698 static void (*PCM_ConvertChangeRate[16])(AcmPcmData* apd,
699 const unsigned char* src, LPDWORD nsrc,
700 unsigned char* dst, LPDWORD ndst) = {
701 cvtSS88C, cvtSM88C, cvtMS88C, cvtMM88C,
702 cvtSS816C, cvtSM816C, cvtMS816C, cvtMM816C,
703 cvtSS168C, cvtSM168C, cvtMS168C, cvtMM168C,
704 cvtSS1616C, cvtSM1616C, cvtMS1616C, cvtMM1616C,
707 /***********************************************************************
708 * PCM_DriverDetails
711 static LRESULT PCM_DriverDetails(PACMDRIVERDETAILSW add)
713 add->fccType = ACMDRIVERDETAILS_FCCTYPE_AUDIOCODEC;
714 add->fccComp = ACMDRIVERDETAILS_FCCCOMP_UNDEFINED;
715 add->wMid = 0xFF;
716 add->wPid = 0x00;
717 add->vdwACM = 0x01000000;
718 add->vdwDriver = 0x01000000;
719 add->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CONVERTER;
720 add->cFormatTags = 1;
721 add->cFilterTags = 0;
722 add->hicon = (HICON)0;
723 MultiByteToWideChar( CP_ACP, 0, "WINE-PCM", -1,
724 add->szShortName, sizeof(add->szShortName)/sizeof(WCHAR) );
725 MultiByteToWideChar( CP_ACP, 0, "Wine PCM converter", -1,
726 add->szLongName, sizeof(add->szLongName)/sizeof(WCHAR) );
727 MultiByteToWideChar( CP_ACP, 0, "Brought to you by the Wine team...", -1,
728 add->szCopyright, sizeof(add->szCopyright)/sizeof(WCHAR) );
729 MultiByteToWideChar( CP_ACP, 0, "Refer to LICENSE file", -1,
730 add->szLicensing, sizeof(add->szLicensing)/sizeof(WCHAR) );
731 add->szFeatures[0] = 0;
733 return MMSYSERR_NOERROR;
736 /***********************************************************************
737 * PCM_FormatTagDetails
740 static LRESULT PCM_FormatTagDetails(PACMFORMATTAGDETAILSW aftd, DWORD dwQuery)
742 switch (dwQuery) {
743 case ACM_FORMATTAGDETAILSF_INDEX:
744 if (aftd->dwFormatTagIndex != 0) return ACMERR_NOTPOSSIBLE;
745 break;
746 case ACM_FORMATTAGDETAILSF_FORMATTAG:
747 if (aftd->dwFormatTag != WAVE_FORMAT_PCM) return ACMERR_NOTPOSSIBLE;
748 break;
749 case ACM_FORMATTAGDETAILSF_LARGESTSIZE:
750 if (aftd->dwFormatTag != WAVE_FORMAT_UNKNOWN &&
751 aftd->dwFormatTag != WAVE_FORMAT_PCM)
752 return ACMERR_NOTPOSSIBLE;
753 break;
754 default:
755 WARN("Unsupported query %08lx\n", dwQuery);
756 return MMSYSERR_NOTSUPPORTED;
759 aftd->dwFormatTagIndex = 0;
760 aftd->dwFormatTag = WAVE_FORMAT_PCM;
761 aftd->cbFormatSize = sizeof(PCMWAVEFORMAT);
762 aftd->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CONVERTER;
763 aftd->cStandardFormats = NUM_PCM_FORMATS;
764 aftd->szFormatTag[0] = 0;
766 return MMSYSERR_NOERROR;
769 /***********************************************************************
770 * PCM_FormatDetails
773 static LRESULT PCM_FormatDetails(PACMFORMATDETAILSW afd, DWORD dwQuery)
775 switch (dwQuery) {
776 case ACM_FORMATDETAILSF_FORMAT:
777 if (PCM_GetFormatIndex(afd->pwfx) == 0xFFFFFFFF) return ACMERR_NOTPOSSIBLE;
778 break;
779 case ACM_FORMATDETAILSF_INDEX:
780 assert(afd->dwFormatIndex < NUM_PCM_FORMATS);
781 afd->pwfx->wFormatTag = WAVE_FORMAT_PCM;
782 afd->pwfx->nChannels = PCM_Formats[afd->dwFormatIndex].nChannels;
783 afd->pwfx->nSamplesPerSec = PCM_Formats[afd->dwFormatIndex].rate;
784 afd->pwfx->wBitsPerSample = PCM_Formats[afd->dwFormatIndex].nBits;
785 /* native MSACM uses a PCMWAVEFORMAT structure, so cbSize is not accessible
786 * afd->pwfx->cbSize = 0;
788 afd->pwfx->nBlockAlign =
789 (afd->pwfx->nChannels * afd->pwfx->wBitsPerSample) / 8;
790 afd->pwfx->nAvgBytesPerSec =
791 afd->pwfx->nSamplesPerSec * afd->pwfx->nBlockAlign;
792 break;
793 default:
794 WARN("Unsupported query %08lx\n", dwQuery);
795 return MMSYSERR_NOTSUPPORTED;
798 afd->dwFormatTag = WAVE_FORMAT_PCM;
799 afd->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CONVERTER;
800 afd->szFormat[0] = 0; /* let MSACM format this for us... */
801 afd->cbwfx = sizeof(PCMWAVEFORMAT);
803 return MMSYSERR_NOERROR;
806 /***********************************************************************
807 * PCM_FormatSuggest
810 static LRESULT PCM_FormatSuggest(PACMDRVFORMATSUGGEST adfs)
812 /* some tests ... */
813 if (adfs->cbwfxSrc < sizeof(PCMWAVEFORMAT) ||
814 adfs->cbwfxDst < sizeof(PCMWAVEFORMAT) ||
815 PCM_GetFormatIndex(adfs->pwfxSrc) == 0xFFFFFFFF) return ACMERR_NOTPOSSIBLE;
817 /* is no suggestion for destination, then copy source value */
818 if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_NCHANNELS)) {
819 adfs->pwfxDst->nChannels = adfs->pwfxSrc->nChannels;
821 if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_NSAMPLESPERSEC)) {
822 adfs->pwfxDst->nSamplesPerSec = adfs->pwfxSrc->nSamplesPerSec;
824 if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_WBITSPERSAMPLE)) {
825 adfs->pwfxDst->wBitsPerSample = adfs->pwfxSrc->wBitsPerSample;
827 if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_WFORMATTAG)) {
828 if (adfs->pwfxSrc->wFormatTag != WAVE_FORMAT_PCM) return ACMERR_NOTPOSSIBLE;
829 adfs->pwfxDst->wFormatTag = adfs->pwfxSrc->wFormatTag;
831 /* check if result is ok */
832 if (PCM_GetFormatIndex(adfs->pwfxDst) == 0xFFFFFFFF) return ACMERR_NOTPOSSIBLE;
834 /* recompute other values */
835 adfs->pwfxDst->nBlockAlign = (adfs->pwfxDst->nChannels * adfs->pwfxDst->wBitsPerSample) / 8;
836 adfs->pwfxDst->nAvgBytesPerSec = adfs->pwfxDst->nSamplesPerSec * adfs->pwfxDst->nBlockAlign;
838 return MMSYSERR_NOERROR;
841 /***********************************************************************
842 * PCM_Reset
845 static void PCM_Reset(AcmPcmData* apd, int srcNumBits)
847 apd->srcPos = 0;
848 apd->dstPos = 0;
849 /* initialize with neutral value */
850 if (srcNumBits == 16) {
851 apd->last[0].s = 0;
852 apd->last[1].s = 0;
853 } else {
854 apd->last[0].b = (BYTE)0x80;
855 apd->last[1].b = (BYTE)0x80;
859 /***********************************************************************
860 * PCM_StreamOpen
863 static LRESULT PCM_StreamOpen(PACMDRVSTREAMINSTANCE adsi)
865 AcmPcmData* apd;
866 int idx = 0;
868 assert(!(adsi->fdwOpen & ACM_STREAMOPENF_ASYNC));
870 if (PCM_GetFormatIndex(adsi->pwfxSrc) == 0xFFFFFFFF ||
871 PCM_GetFormatIndex(adsi->pwfxDst) == 0xFFFFFFFF)
872 return ACMERR_NOTPOSSIBLE;
874 apd = HeapAlloc(GetProcessHeap(), 0, sizeof(AcmPcmData));
875 if (apd == 0) return MMSYSERR_NOMEM;
877 adsi->dwDriver = (DWORD)apd;
878 adsi->fdwDriver = 0;
880 if (adsi->pwfxSrc->wBitsPerSample == 16) idx += 8;
881 if (adsi->pwfxDst->wBitsPerSample == 16) idx += 4;
882 if (adsi->pwfxSrc->nChannels == 1) idx += 2;
883 if (adsi->pwfxDst->nChannels == 1) idx += 1;
885 if (adsi->pwfxSrc->nSamplesPerSec == adsi->pwfxDst->nSamplesPerSec) {
886 apd->cvt.cvtKeepRate = PCM_ConvertKeepRate[idx];
887 } else {
888 adsi->fdwDriver |= PCM_RESAMPLE;
889 apd->dstIncr = (double)(adsi->pwfxSrc->nSamplesPerSec) /
890 (double)(adsi->pwfxDst->nSamplesPerSec);
891 PCM_Reset(apd, adsi->pwfxSrc->wBitsPerSample);
892 apd->cvt.cvtChangeRate = PCM_ConvertChangeRate[idx];
895 return MMSYSERR_NOERROR;
898 /***********************************************************************
899 * PCM_StreamClose
902 static LRESULT PCM_StreamClose(PACMDRVSTREAMINSTANCE adsi)
904 HeapFree(GetProcessHeap(), 0, (void*)adsi->dwDriver);
905 return MMSYSERR_NOERROR;
908 /***********************************************************************
909 * PCM_round
912 static inline DWORD PCM_round(DWORD a, DWORD b, DWORD c)
914 assert(a && b && c);
915 /* to be sure, always return an entire number of c... */
916 return ((double)a * (double)b + (double)c - 1) / (double)c;
919 /***********************************************************************
920 * PCM_StreamSize
923 static LRESULT PCM_StreamSize(PACMDRVSTREAMINSTANCE adsi, PACMDRVSTREAMSIZE adss)
925 DWORD srcMask = ~(adsi->pwfxSrc->nBlockAlign - 1);
926 DWORD dstMask = ~(adsi->pwfxDst->nBlockAlign - 1);
928 switch (adss->fdwSize) {
929 case ACM_STREAMSIZEF_DESTINATION:
930 /* cbDstLength => cbSrcLength */
931 adss->cbSrcLength = PCM_round(adss->cbDstLength & dstMask,
932 adsi->pwfxSrc->nAvgBytesPerSec,
933 adsi->pwfxDst->nAvgBytesPerSec) & srcMask;
934 break;
935 case ACM_STREAMSIZEF_SOURCE:
936 /* cbSrcLength => cbDstLength */
937 adss->cbDstLength = PCM_round(adss->cbSrcLength & srcMask,
938 adsi->pwfxDst->nAvgBytesPerSec,
939 adsi->pwfxSrc->nAvgBytesPerSec) & dstMask;
940 break;
941 default:
942 WARN("Unsupported query %08lx\n", adss->fdwSize);
943 return MMSYSERR_NOTSUPPORTED;
945 return MMSYSERR_NOERROR;
948 /***********************************************************************
949 * PCM_StreamConvert
952 static LRESULT PCM_StreamConvert(PACMDRVSTREAMINSTANCE adsi, PACMDRVSTREAMHEADER adsh)
954 AcmPcmData* apd = (AcmPcmData*)adsi->dwDriver;
955 DWORD nsrc = NUM_OF(adsh->cbSrcLength, adsi->pwfxSrc->nBlockAlign);
956 DWORD ndst = NUM_OF(adsh->cbDstLength, adsi->pwfxDst->nBlockAlign);
958 if (adsh->fdwConvert &
959 ~(ACM_STREAMCONVERTF_BLOCKALIGN|
960 ACM_STREAMCONVERTF_END|
961 ACM_STREAMCONVERTF_START)) {
962 FIXME("Unsupported fdwConvert (%08lx), ignoring it\n", adsh->fdwConvert);
964 /* ACM_STREAMCONVERTF_BLOCKALIGN
965 * currently all conversions are block aligned, so do nothing for this flag
966 * ACM_STREAMCONVERTF_END
967 * no pending data, so do nothing for this flag
969 if ((adsh->fdwConvert & ACM_STREAMCONVERTF_START) &&
970 (adsi->fdwDriver & PCM_RESAMPLE)) {
971 PCM_Reset(apd, adsi->pwfxSrc->wBitsPerSample);
974 /* do the job */
975 if (adsi->fdwDriver & PCM_RESAMPLE) {
976 DWORD nsrc2 = nsrc;
977 DWORD ndst2 = ndst;
979 apd->cvt.cvtChangeRate(apd, adsh->pbSrc, &nsrc2, adsh->pbDst, &ndst2);
980 nsrc -= nsrc2;
981 ndst -= ndst2;
982 } else {
983 if (nsrc < ndst) ndst = nsrc; else nsrc = ndst;
985 /* nsrc is now equal to ndst */
986 apd->cvt.cvtKeepRate(adsh->pbSrc, nsrc, adsh->pbDst);
989 adsh->cbSrcLengthUsed = nsrc * adsi->pwfxSrc->nBlockAlign;
990 adsh->cbDstLengthUsed = ndst * adsi->pwfxDst->nBlockAlign;
992 return MMSYSERR_NOERROR;
995 /**************************************************************************
996 * DriverProc (MSACM32.@)
998 LRESULT CALLBACK PCM_DriverProc(DWORD dwDevID, HDRVR hDriv, UINT wMsg,
999 LPARAM dwParam1, LPARAM dwParam2)
1001 TRACE("(%08lx %08lx %u %08lx %08lx);\n",
1002 dwDevID, (DWORD)hDriv, wMsg, dwParam1, dwParam2);
1004 switch (wMsg) {
1005 case DRV_LOAD: return 1;
1006 case DRV_FREE: return 1;
1007 case DRV_OPEN: return PCM_drvOpen((LPSTR)dwParam1, (PACMDRVOPENDESCW)dwParam2);
1008 case DRV_CLOSE: return PCM_drvClose(dwDevID);
1009 case DRV_ENABLE: return 1;
1010 case DRV_DISABLE: return 1;
1011 case DRV_QUERYCONFIGURE: return 1;
1012 case DRV_CONFIGURE: MessageBoxA(0, "MSACM PCM filter !", "Wine Driver", MB_OK); return 1;
1013 case DRV_INSTALL: return DRVCNF_RESTART;
1014 case DRV_REMOVE: return DRVCNF_RESTART;
1016 case ACMDM_DRIVER_NOTIFY:
1017 /* no caching from other ACM drivers is done so far */
1018 return MMSYSERR_NOERROR;
1020 case ACMDM_DRIVER_DETAILS:
1021 return PCM_DriverDetails((PACMDRIVERDETAILSW)dwParam1);
1023 case ACMDM_FORMATTAG_DETAILS:
1024 return PCM_FormatTagDetails((PACMFORMATTAGDETAILSW)dwParam1, dwParam2);
1026 case ACMDM_FORMAT_DETAILS:
1027 return PCM_FormatDetails((PACMFORMATDETAILSW)dwParam1, dwParam2);
1029 case ACMDM_FORMAT_SUGGEST:
1030 return PCM_FormatSuggest((PACMDRVFORMATSUGGEST)dwParam1);
1032 case ACMDM_STREAM_OPEN:
1033 return PCM_StreamOpen((PACMDRVSTREAMINSTANCE)dwParam1);
1035 case ACMDM_STREAM_CLOSE:
1036 return PCM_StreamClose((PACMDRVSTREAMINSTANCE)dwParam1);
1038 case ACMDM_STREAM_SIZE:
1039 return PCM_StreamSize((PACMDRVSTREAMINSTANCE)dwParam1, (PACMDRVSTREAMSIZE)dwParam2);
1041 case ACMDM_STREAM_CONVERT:
1042 return PCM_StreamConvert((PACMDRVSTREAMINSTANCE)dwParam1, (PACMDRVSTREAMHEADER)dwParam2);
1044 case ACMDM_HARDWARE_WAVE_CAPS_INPUT:
1045 case ACMDM_HARDWARE_WAVE_CAPS_OUTPUT:
1046 /* this converter is not a hardware driver */
1047 case ACMDM_FILTERTAG_DETAILS:
1048 case ACMDM_FILTER_DETAILS:
1049 /* this converter is not a filter */
1050 case ACMDM_STREAM_RESET:
1051 /* only needed for asynchronous driver... we aren't, so just say it */
1052 case ACMDM_STREAM_PREPARE:
1053 case ACMDM_STREAM_UNPREPARE:
1054 /* nothing special to do here... so don't do anything */
1055 return MMSYSERR_NOTSUPPORTED;
1057 default:
1058 return DefDriverProc(dwDevID, hDriv, wMsg, dwParam1, dwParam2);
1060 return 0;