ole32: Return an error in CoMarshalInterface if pStream is NULL.
[wine] / dlls / msacm32 / pcmconverter.c
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
2
3 /*
4  *      MSACM32 library
5  *
6  *      Copyright 2000          Eric Pouech
7  *      Copyright 2004          Robert Reif
8  *
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.
13  *
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.
18  *
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
22  *
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
27  */
28
29 #include "config.h"
30
31 #include <assert.h>
32 #include <stdarg.h>
33 #include <string.h>
34
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"
44
45 #include "msacmdrv.h"
46 #include "wineacm.h"
47
48 #include "wine/debug.h"
49
50 WINE_DEFAULT_DEBUG_CHANNEL(msacm);
51
52 /***********************************************************************
53  *           PCM_drvOpen
54  */
55 static  DWORD   PCM_drvOpen(LPCSTR str, PACMDRVOPENDESCW adod)
56 {
57     TRACE("(%p, %p)\n", str, adod);
58
59     return (adod == NULL) ||
60         (adod->fccType == ACMDRIVERDETAILS_FCCTYPE_AUDIOCODEC &&
61          adod->fccComp == ACMDRIVERDETAILS_FCCCOMP_UNDEFINED);
62 }
63
64 /***********************************************************************
65  *           PCM_drvClose
66  */
67 static  DWORD   PCM_drvClose(DWORD dwDevID)
68 {
69     TRACE("(%d)\n", dwDevID);
70
71     return 1;
72 }
73
74 #define NUM_PCM_FORMATS (sizeof(PCM_Formats) / sizeof(PCM_Formats[0]))
75 #define NUM_OF(a,b)     ((a)/(b))
76
77 /* flags for fdwDriver */
78 #define PCM_RESAMPLE    1
79
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;
89
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
92  */
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}
104 };
105
106 /***********************************************************************
107  *           PCM_GetFormatIndex
108  */
109 static DWORD PCM_GetFormatIndex(LPWAVEFORMATEX wfx)
110 {
111     unsigned int i;
112     TRACE("(%p)\n", wfx);
113
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;
119     }
120     return 0xFFFFFFFF;
121 }
122
123 /* PCM Conversions:
124  *
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)
130  *
131  * mono => stereo: copy the same sample on Left & Right channels
132  * stereo => mono: use the sum of Left & Right channels
133  */
134
135 /***********************************************************************
136  *           C816
137  *
138  * Converts a 8 bit sample to a 16 bit one
139  */
140 static inline short C816(unsigned char b)
141 {
142     return (b - 128) << 8;
143 }
144
145 /***********************************************************************
146  *           C168
147  *
148  * Converts a 16 bit sample to a 8 bit one (data loss !!)
149  */
150 static inline unsigned char C168(short s)
151 {
152     return HIBYTE(s) ^ (unsigned char)0x80;
153 }
154
155 /***********************************************************************
156  *           R16
157  *
158  * Read a 16 bit sample (correctly handles endianess)
159  */
160 static inline short  R16(const unsigned char* src)
161 {
162     return (short)((unsigned short)src[0] | ((unsigned short)src[1] << 8));
163 }
164
165 /***********************************************************************
166  *           W16
167  *
168  * Write a 16 bit sample (correctly handles endianess)
169  */
170 static inline void  W16(unsigned char* dst, short s)
171 {
172     dst[0] = LOBYTE(s);
173     dst[1] = HIBYTE(s);
174 }
175
176 /***********************************************************************
177  *           M16
178  *
179  * Convert the (l,r) 16 bit stereo sample into a 16 bit mono
180  * (takes the sum of the two values)
181  */
182 static inline short M16(short l, short r)
183 {
184     int sum = l + r;
185
186     /* clip sum to saturation */
187     if (sum > 32767)
188         sum = 32767;
189     else if (sum < -32768)
190         sum = -32768;
191
192     return sum;
193 }
194
195 /***********************************************************************
196  *           M8
197  *
198  * Convert the (l,r) 8 bit stereo sample into a 8 bit mono
199  * (takes the sum of the two values)
200  */
201 static inline unsigned char M8(unsigned char a, unsigned char b)
202 {
203     int l = a - 128;
204     int r = b - 128;
205     int sum = (l + r) + 128;
206
207     /* clip sum to saturation */
208     if (sum > 0xff)
209         sum = 0xff;
210     else if (sum < 0)
211         sum = 0;
212
213     return sum;
214 }
215
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)
222  *
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)
225  */
226
227 static  void cvtMM88K(const unsigned char* src, int ns, unsigned char* dst)
228 {
229     TRACE("(%p, %d, %p)\n", src, ns, dst);
230     memcpy(dst, src, ns);
231 }
232
233 static  void cvtSS88K(const unsigned char* src, int ns, unsigned char* dst)
234 {
235     TRACE("(%p, %d, %p)\n", src, ns, dst);
236     memcpy(dst, src, ns * 2);
237 }
238
239 static  void cvtMM1616K(const unsigned char* src, int ns, unsigned char* dst)
240 {
241     TRACE("(%p, %d, %p)\n", src, ns, dst);
242     memcpy(dst, src, ns * 2);
243 }
244
245 static  void cvtSS1616K(const unsigned char* src, int ns, unsigned char* dst)
246 {
247     TRACE("(%p, %d, %p)\n", src, ns, dst);
248     memcpy(dst, src, ns * 4);
249 }
250
251 static  void cvtMS88K(const unsigned char* src, int ns, unsigned char* dst)
252 {
253     TRACE("(%p, %d, %p)\n", src, ns, dst);
254
255     while (ns--) {
256         *dst++ = *src;
257         *dst++ = *src++;
258     }
259 }
260
261 static  void cvtMS816K(const unsigned char* src, int ns, unsigned char* dst)
262 {
263     short       v;
264     TRACE("(%p, %d, %p)\n", src, ns, dst);
265
266     while (ns--) {
267         v = C816(*src++);
268         W16(dst, v);            dst += 2;
269         W16(dst, v);            dst += 2;
270     }
271 }
272
273 static  void cvtMS168K(const unsigned char* src, int ns, unsigned char* dst)
274 {
275     unsigned char v;
276     TRACE("(%p, %d, %p)\n", src, ns, dst);
277
278     while (ns--) {
279         v = C168(R16(src));     src += 2;
280         *dst++ = v;
281         *dst++ = v;
282     }
283 }
284
285 static  void cvtMS1616K(const unsigned char* src, int ns, unsigned char* dst)
286 {
287     short       v;
288     TRACE("(%p, %d, %p)\n", src, ns, dst);
289
290     while (ns--) {
291         v = R16(src);           src += 2;
292         W16(dst, v);            dst += 2;
293         W16(dst, v);            dst += 2;
294     }
295 }
296
297 static  void cvtSM88K(const unsigned char* src, int ns, unsigned char* dst)
298 {
299     TRACE("(%p, %d, %p)\n", src, ns, dst);
300
301     while (ns--) {
302         *dst++ = M8(src[0], src[1]);
303         src += 2;
304     }
305 }
306
307 static  void cvtSM816K(const unsigned char* src, int ns, unsigned char* dst)
308 {
309     short       v;
310     TRACE("(%p, %d, %p)\n", src, ns, dst);
311
312     while (ns--) {
313         v = M16(C816(src[0]), C816(src[1]));
314         src += 2;
315         W16(dst, v);            dst += 2;
316     }
317 }
318
319 static  void cvtSM168K(const unsigned char* src, int ns, unsigned char* dst)
320 {
321     TRACE("(%p, %d, %p)\n", src, ns, dst);
322
323     while (ns--) {
324         *dst++ = C168(M16(R16(src), R16(src + 2)));
325         src += 4;
326     }
327 }
328
329 static  void cvtSM1616K(const unsigned char* src, int ns, unsigned char* dst)
330 {
331     TRACE("(%p, %d, %p)\n", src, ns, dst);
332
333     while (ns--) {
334         W16(dst, M16(R16(src),R16(src+2)));     dst += 2;
335         src += 4;
336     }
337 }
338
339 static  void cvtMM816K(const unsigned char* src, int ns, unsigned char* dst)
340 {
341     TRACE("(%p, %d, %p)\n", src, ns, dst);
342
343     while (ns--) {
344         W16(dst, C816(*src++));         dst += 2;
345     }
346 }
347
348 static  void cvtSS816K(const unsigned char* src, int ns, unsigned char* dst)
349 {
350     TRACE("(%p, %d, %p)\n", src, ns, dst);
351
352     while (ns--) {
353         W16(dst, C816(*src++)); dst += 2;
354         W16(dst, C816(*src++)); dst += 2;
355     }
356 }
357
358 static  void cvtMM168K(const unsigned char* src, int ns, unsigned char* dst)
359 {
360     TRACE("(%p, %d, %p)\n", src, ns, dst);
361
362     while (ns--) {
363         *dst++ = C168(R16(src));        src += 2;
364     }
365 }
366
367 static  void cvtSS168K(const unsigned char* src, int ns, unsigned char* dst)
368 {
369     TRACE("(%p, %d, %p)\n", src, ns, dst);
370
371     while (ns--) {
372         *dst++ = C168(R16(src));        src += 2;
373         *dst++ = C168(R16(src));        src += 2;
374     }
375 }
376
377
378 typedef void (*PCM_CONVERT_KEEP_RATE)(const unsigned char*, int, unsigned char*);
379
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,
385 };
386
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)
393  *
394  */
395 static  void cvtSS88C(DWORD srcRate, const unsigned char* src, LPDWORD nsrc,
396                       DWORD dstRate, unsigned char* dst, LPDWORD ndst)
397 {
398     DWORD error = dstRate / 2;
399     TRACE("(%d, %p, %p, %d, %p, %p)\n", srcRate, src, nsrc, dstRate, dst, ndst);
400
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;
411         }
412     }
413 }
414
415 static  void cvtSM88C(DWORD srcRate, const unsigned char* src, LPDWORD nsrc,
416                       DWORD dstRate, unsigned char* dst, LPDWORD ndst)
417 {
418     DWORD error = dstRate / 2;
419     TRACE("(%d, %p, %p, %d, %p, %p)\n", srcRate, src, nsrc, dstRate, dst, ndst);
420
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;
430         }
431     }
432 }
433
434 static  void cvtMS88C(DWORD srcRate, const unsigned char* src, LPDWORD nsrc,
435                       DWORD dstRate, unsigned char* dst, LPDWORD ndst)
436 {
437     DWORD error = dstRate / 2;
438     TRACE("(%d, %p, %p, %d, %p, %p)\n", srcRate, src, nsrc, dstRate, dst, ndst);
439
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;
450         }
451     }
452 }
453
454 static  void cvtMM88C(DWORD srcRate, const unsigned char* src, LPDWORD nsrc,
455                       DWORD dstRate, unsigned char* dst, LPDWORD ndst)
456 {
457     DWORD error = dstRate / 2;
458     TRACE("(%d, %p, %p, %d, %p, %p)\n", srcRate, src, nsrc, dstRate, dst, ndst);
459
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;
469         }
470     }
471 }
472
473 static  void cvtSS816C(DWORD srcRate, const unsigned char* src, LPDWORD nsrc,
474                        DWORD dstRate, unsigned char* dst, LPDWORD ndst)
475 {
476     DWORD error = dstRate / 2;
477     TRACE("(%d, %p, %p, %d, %p, %p)\n", srcRate, src, nsrc, dstRate, dst, ndst);
478
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;
489         }
490     }
491 }
492
493 static  void cvtSM816C(DWORD srcRate, const unsigned char* src, LPDWORD nsrc,
494                        DWORD dstRate, unsigned char* dst, LPDWORD ndst)
495 {
496     DWORD error = dstRate / 2;
497     TRACE("(%d, %p, %p, %d, %p, %p)\n", srcRate, src, nsrc, dstRate, dst, ndst);
498
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;
508         }
509     }
510 }
511
512 static  void cvtMS816C(DWORD srcRate, const unsigned char* src, LPDWORD nsrc,
513                        DWORD dstRate, unsigned char* dst, LPDWORD ndst)
514 {
515     DWORD error = dstRate / 2;
516     TRACE("(%d, %p, %p, %d, %p, %p)\n", srcRate, src, nsrc, dstRate, dst, ndst);
517
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;
528         }
529     }
530 }
531
532 static  void cvtMM816C(DWORD srcRate, const unsigned char* src, LPDWORD nsrc,
533                        DWORD dstRate, unsigned char* dst, LPDWORD ndst)
534 {
535     DWORD error = dstRate / 2;
536     TRACE("(%d, %p, %p, %d, %p, %p)\n", srcRate, src, nsrc, dstRate, dst, ndst);
537
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;
547         }
548     }
549 }
550
551 static  void cvtSS168C(DWORD srcRate, const unsigned char* src, LPDWORD nsrc,
552                        DWORD dstRate, unsigned char* dst, LPDWORD ndst)
553 {
554     DWORD error = dstRate / 2;
555     TRACE("(%d, %p, %p, %d, %p, %p)\n", srcRate, src, nsrc, dstRate, dst, ndst);
556
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;
567         }
568     }
569 }
570
571 static  void cvtSM168C(DWORD srcRate, const unsigned char* src, LPDWORD nsrc,
572                        DWORD dstRate, unsigned char* dst, LPDWORD ndst)
573 {
574     DWORD error = dstRate / 2;
575     TRACE("(%d, %p, %p, %d, %p, %p)\n", srcRate, src, nsrc, dstRate, dst, ndst);
576
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;
586         }
587     }
588 }
589
590 static  void cvtMS168C(DWORD srcRate, const unsigned char* src, LPDWORD nsrc,
591                        DWORD dstRate, unsigned char* dst, LPDWORD ndst)
592 {
593     DWORD error = dstRate / 2;
594     TRACE("(%d, %p, %p, %d, %p, %p)\n", srcRate, src, nsrc, dstRate, dst, ndst);
595
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;
606         }
607     }
608 }
609
610 static  void cvtMM168C(DWORD srcRate, const unsigned char* src, LPDWORD nsrc,
611                        DWORD dstRate, unsigned char* dst, LPDWORD ndst)
612 {
613     DWORD error = dstRate / 2;
614     TRACE("(%d, %p, %p, %d, %p, %p)\n", srcRate, src, nsrc, dstRate, dst, ndst);
615
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;
625         }
626     }
627 }
628
629 static  void cvtSS1616C(DWORD srcRate, const unsigned char* src, LPDWORD nsrc,
630                         DWORD dstRate, unsigned char* dst, LPDWORD ndst)
631 {
632     DWORD error = dstRate / 2;
633     TRACE("(%d, %p, %p, %d, %p, %p)\n", srcRate, src, nsrc, dstRate, dst, ndst);
634
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;
645         }
646     }
647 }
648
649 static  void cvtSM1616C(DWORD srcRate, const unsigned char* src, LPDWORD nsrc,
650                         DWORD dstRate, unsigned char* dst, LPDWORD ndst)
651 {
652     DWORD error = dstRate / 2;
653     TRACE("(%d, %p, %p, %d, %p, %p)\n", srcRate, src, nsrc, dstRate, dst, ndst);
654
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;
664         }
665     }
666 }
667
668 static  void cvtMS1616C(DWORD srcRate, const unsigned char* src, LPDWORD nsrc,
669                         DWORD dstRate, unsigned char* dst, LPDWORD ndst)
670 {
671     DWORD error = dstRate / 2;
672     TRACE("(%d, %p, %p, %d, %p, %p)\n", srcRate, src, nsrc, dstRate, dst, ndst);
673
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;
684         }
685     }
686 }
687
688 static  void cvtMM1616C(DWORD srcRate, const unsigned char* src, LPDWORD nsrc,
689                         DWORD dstRate, unsigned char* dst, LPDWORD ndst)
690 {
691     DWORD error = dstRate / 2;
692     TRACE("(%d, %p, %p, %d, %p, %p)\n", srcRate, src, nsrc, dstRate, dst, ndst);
693
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;
703         }
704     }
705 }
706
707 typedef void (*PCM_CONVERT_CHANGE_RATE)(DWORD, const unsigned char*, LPDWORD, DWORD, unsigned char*, LPDWORD);
708
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,
714 };
715
716 /***********************************************************************
717  *           PCM_DriverDetails
718  *
719  */
720 static  LRESULT PCM_DriverDetails(PACMDRIVERDETAILSW add)
721 {
722     TRACE("(%p)\n", add);
723
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;
743
744     return MMSYSERR_NOERROR;
745 }
746
747 /***********************************************************************
748  *           PCM_FormatTagDetails
749  *
750  */
751 static  LRESULT PCM_FormatTagDetails(PACMFORMATTAGDETAILSW aftd, DWORD dwQuery)
752 {
753     TRACE("(%p, %08x)\n", aftd, dwQuery);
754
755     switch (dwQuery) {
756     case ACM_FORMATTAGDETAILSF_INDEX:
757         if (aftd->dwFormatTagIndex != 0) {
758             WARN("not possible\n");
759             return ACMERR_NOTPOSSIBLE;
760         }
761         break;
762     case ACM_FORMATTAGDETAILSF_FORMATTAG:
763         if (aftd->dwFormatTag != WAVE_FORMAT_PCM) {
764             WARN("not possible\n");
765             return ACMERR_NOTPOSSIBLE;
766         }
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;
773         }
774         break;
775     default:
776         WARN("Unsupported query %08x\n", dwQuery);
777         return MMSYSERR_NOTSUPPORTED;
778     }
779
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;
786
787     return MMSYSERR_NOERROR;
788 }
789
790 /***********************************************************************
791  *           PCM_FormatDetails
792  *
793  */
794 static  LRESULT PCM_FormatDetails(PACMFORMATDETAILSW afd, DWORD dwQuery)
795 {
796     TRACE("(%p, %08x)\n", afd, dwQuery);
797
798     switch (dwQuery) {
799     case ACM_FORMATDETAILSF_FORMAT:
800         if (PCM_GetFormatIndex(afd->pwfx) == 0xFFFFFFFF) {
801             WARN("not possible\n");
802             return ACMERR_NOTPOSSIBLE;
803         }
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;
813          */
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;
822     }
823
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);
828
829     return MMSYSERR_NOERROR;
830 }
831
832 /***********************************************************************
833  *           PCM_FormatSuggest
834  *
835  */
836 static  LRESULT PCM_FormatSuggest(PACMDRVFORMATSUGGEST adfs)
837 {
838     TRACE("(%p)\n", adfs);
839
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;
846     }
847
848     /* is no suggestion for destination, then copy source value */
849     if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_NCHANNELS)) {
850         adfs->pwfxDst->nChannels = adfs->pwfxSrc->nChannels;
851     }
852     if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_NSAMPLESPERSEC)) {
853         adfs->pwfxDst->nSamplesPerSec = adfs->pwfxSrc->nSamplesPerSec;
854     }
855     if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_WBITSPERSAMPLE)) {
856         adfs->pwfxDst->wBitsPerSample = adfs->pwfxSrc->wBitsPerSample;
857     }
858     if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_WFORMATTAG)) {
859         if (adfs->pwfxSrc->wFormatTag != WAVE_FORMAT_PCM) {
860             WARN("not possible\n");
861             return ACMERR_NOTPOSSIBLE;
862         }
863         adfs->pwfxDst->wFormatTag = adfs->pwfxSrc->wFormatTag;
864     }
865     /* check if result is ok */
866     if (PCM_GetFormatIndex(adfs->pwfxDst) == 0xFFFFFFFF) {
867         WARN("not possible\n");
868         return ACMERR_NOTPOSSIBLE;
869     }
870
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;
874
875     return MMSYSERR_NOERROR;
876 }
877
878 /***********************************************************************
879  *           PCM_StreamOpen
880  *
881  */
882 static  LRESULT PCM_StreamOpen(PACMDRVSTREAMINSTANCE adsi)
883 {
884     AcmPcmData* apd;
885     int         idx = 0;
886
887     TRACE("(%p)\n", adsi);
888
889     assert(!(adsi->fdwOpen & ACM_STREAMOPENF_ASYNC));
890
891     apd = HeapAlloc(GetProcessHeap(), 0, sizeof(AcmPcmData));
892     if (apd == 0) {
893         WARN("no memory\n");
894         return MMSYSERR_NOMEM;
895     }
896
897     adsi->dwDriver = (DWORD)apd;
898     adsi->fdwDriver = 0;
899
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;
904
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];
910     }
911
912     return MMSYSERR_NOERROR;
913 }
914
915 /***********************************************************************
916  *           PCM_StreamClose
917  *
918  */
919 static  LRESULT PCM_StreamClose(PACMDRVSTREAMINSTANCE adsi)
920 {
921     TRACE("(%p)\n", adsi);
922
923     HeapFree(GetProcessHeap(), 0, (void*)adsi->dwDriver);
924     return MMSYSERR_NOERROR;
925 }
926
927 /***********************************************************************
928  *           PCM_round
929  *
930  */
931 static  inline DWORD    PCM_round(DWORD a, DWORD b, DWORD c)
932 {
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;
936 }
937
938 /***********************************************************************
939  *           PCM_StreamSize
940  *
941  */
942 static  LRESULT PCM_StreamSize(PACMDRVSTREAMINSTANCE adsi, PACMDRVSTREAMSIZE adss)
943 {
944     DWORD       srcMask = ~(adsi->pwfxSrc->nBlockAlign - 1);
945     DWORD       dstMask = ~(adsi->pwfxDst->nBlockAlign - 1);
946
947     TRACE("(%p, %p)\n", adsi, adss);
948
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;
965     }
966     return MMSYSERR_NOERROR;
967 }
968
969 /***********************************************************************
970  *           PCM_StreamConvert
971  *
972  */
973 static LRESULT PCM_StreamConvert(PACMDRVSTREAMINSTANCE adsi, PACMDRVSTREAMHEADER adsh)
974 {
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);
978
979     TRACE("(%p, %p)\n", adsi, adsh);
980
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);
989
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);
995     }
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
1000      */
1001     if ((adsh->fdwConvert & ACM_STREAMCONVERTF_START) &&
1002         (adsi->fdwDriver & PCM_RESAMPLE)) {
1003     }
1004
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;
1015
1016         /* nsrc is now equal to ndst */
1017         apd->cvt.cvtKeepRate(adsh->pbSrc, nsrc, adsh->pbDst);
1018     }
1019
1020     adsh->cbSrcLengthUsed = nsrc * adsi->pwfxSrc->nBlockAlign;
1021     adsh->cbDstLengthUsed = ndst * adsi->pwfxDst->nBlockAlign;
1022
1023     return MMSYSERR_NOERROR;
1024 }
1025
1026 /**************************************************************************
1027  *                      DriverProc (MSACM32.@)
1028  */
1029 LRESULT CALLBACK PCM_DriverProc(DWORD_PTR dwDevID, HDRVR hDriv, UINT wMsg,
1030                                        LPARAM dwParam1, LPARAM dwParam2)
1031 {
1032     TRACE("(%08lx %p %u %08lx %08lx);\n",
1033           dwDevID, hDriv, wMsg, dwParam1, dwParam2);
1034
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;
1046
1047     case ACMDM_DRIVER_NOTIFY:
1048         /* no caching from other ACM drivers is done so far */
1049         return MMSYSERR_NOERROR;
1050
1051     case ACMDM_DRIVER_DETAILS:
1052         return PCM_DriverDetails((PACMDRIVERDETAILSW)dwParam1);
1053
1054     case ACMDM_FORMATTAG_DETAILS:
1055         return PCM_FormatTagDetails((PACMFORMATTAGDETAILSW)dwParam1, dwParam2);
1056
1057     case ACMDM_FORMAT_DETAILS:
1058         return PCM_FormatDetails((PACMFORMATDETAILSW)dwParam1, dwParam2);
1059
1060     case ACMDM_FORMAT_SUGGEST:
1061         return PCM_FormatSuggest((PACMDRVFORMATSUGGEST)dwParam1);
1062
1063     case ACMDM_STREAM_OPEN:
1064         return PCM_StreamOpen((PACMDRVSTREAMINSTANCE)dwParam1);
1065
1066     case ACMDM_STREAM_CLOSE:
1067         return PCM_StreamClose((PACMDRVSTREAMINSTANCE)dwParam1);
1068
1069     case ACMDM_STREAM_SIZE:
1070         return PCM_StreamSize((PACMDRVSTREAMINSTANCE)dwParam1, (PACMDRVSTREAMSIZE)dwParam2);
1071
1072     case ACMDM_STREAM_CONVERT:
1073         return PCM_StreamConvert((PACMDRVSTREAMINSTANCE)dwParam1, (PACMDRVSTREAMHEADER)dwParam2);
1074
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;
1087
1088     default:
1089         return DefDriverProc(dwDevID, hDriv, wMsg, dwParam1, dwParam2);
1090     }
1091 }