Added regedit unit test, a couple minor changes to regedit.
[wine] / dlls / msacm / pcmconverter.c
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
2
3 /*
4  *      MSACM32 library
5  *
6  *      Copyright 2000          Eric Pouech
7  *
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.
12  *
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.
17  *
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
21  *
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
30  */
31
32 #include "config.h"
33
34 #include <assert.h>
35 #include <string.h>
36
37 #include "msacm.h"
38 #include "winbase.h"
39 #include "wingdi.h"
40 #include "winnls.h"
41 #include "winuser.h"
42
43 #include "msacmdrv.h"
44 #include "wineacm.h"
45
46 #include "wine/debug.h"
47
48 WINE_DEFAULT_DEBUG_CHANNEL(msacm);
49
50 /***********************************************************************
51  *           PCM_drvOpen
52  */
53 static  DWORD   PCM_drvOpen(LPCSTR str, PACMDRVOPENDESCW adod)
54 {
55     return (adod == NULL) ||
56         (adod->fccType == ACMDRIVERDETAILS_FCCTYPE_AUDIOCODEC &&
57          adod->fccComp == ACMDRIVERDETAILS_FCCCOMP_UNDEFINED);
58 }
59
60 /***********************************************************************
61  *           PCM_drvClose
62  */
63 static  DWORD   PCM_drvClose(DWORD dwDevID)
64 {
65     return 1;
66 }
67
68 #define NUM_PCM_FORMATS (sizeof(PCM_Formats) / sizeof(PCM_Formats[0]))
69 #define NUM_OF(a,b)     (((a)+(b)-1)/(b))
70
71 /* flags for fdwDriver */
72 #define PCM_RESAMPLE    1
73
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;
93
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
96  */
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},
106 };
107
108 /***********************************************************************
109  *           PCM_GetFormatIndex
110  */
111 static  DWORD   PCM_GetFormatIndex(LPWAVEFORMATEX wfx)
112 {
113     int i;
114
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;
120     }
121     return 0xFFFFFFFF;
122 }
123
124 /* PCM Conversions:
125  *
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)
131  *
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
138  */
139
140 /***********************************************************************
141  *           C816
142  *
143  * Converts a 8 bit sample to a 16 bit one
144  */
145 static inline short C816(unsigned char b)
146 {
147     return (short)(b ^ 0x80) * 256;
148 }
149
150 /***********************************************************************
151  *           C168
152  *
153  * Converts a 16 bit sample to a 8 bit one (data loss !!)
154  */
155 static inline unsigned char C168(short s)
156 {
157     return HIBYTE(s) ^ (unsigned char)0x80;
158 }
159
160 /***********************************************************************
161  *           R16
162  *
163  * Read a 16 bit sample (correctly handles endianess)
164  */
165 static inline short  R16(const unsigned char* src)
166 {
167     return (short)((unsigned short)src[0] | ((unsigned short)src[1] << 8));
168 }
169
170 /***********************************************************************
171  *           W16
172  *
173  * Write a 16 bit sample (correctly handles endianess)
174  */
175 static inline void  W16(unsigned char* dst, short s)
176 {
177     dst[0] = LOBYTE(s);
178     dst[1] = HIBYTE(s);
179 }
180
181 /***********************************************************************
182  *           M16
183  *
184  * Convert the (l,r) 16 bit stereo sample into a 16 bit mono
185  * (takes the mid-point of the two values)
186  */
187 static inline short M16(short l, short r)
188 {
189     return (l + r) / 2;
190 }
191
192 /***********************************************************************
193  *           M8
194  *
195  * Convert the (l,r) 8 bit stereo sample into a 8 bit mono
196  * (takes the mid-point of the two values)
197  */
198 static inline unsigned char M8(unsigned char a, unsigned char b)
199 {
200     return (unsigned char)((a + b) / 2);
201 }
202
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)
209  *
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)
212  */
213
214 static  void cvtMM88K(const unsigned char* src, int ns, unsigned char* dst)
215 {
216     memcpy(dst, src, ns);
217 }
218
219 static  void cvtSS88K(const unsigned char* src, int ns, unsigned char* dst)
220 {
221     memcpy(dst, src, ns * 2);
222 }
223
224 static  void cvtMM1616K(const unsigned char* src, int ns, unsigned char* dst)
225 {
226     memcpy(dst, src, ns * 2);
227 }
228
229 static  void cvtSS1616K(const unsigned char* src, int ns, unsigned char* dst)
230 {
231     memcpy(dst, src, ns * 4);
232 }
233
234 static  void cvtMS88K(const unsigned char* src, int ns, unsigned char* dst)
235 {
236     while (ns--) {
237         *dst++ = *src;
238         *dst++ = *src++;
239     }
240 }
241
242 static  void cvtMS816K(const unsigned char* src, int ns, unsigned char* dst)
243 {
244     short       v;
245
246     while (ns--) {
247         v = C816(*src++);
248         W16(dst, v);            dst += 2;
249         W16(dst, v);            dst += 2;
250     }
251 }
252
253 static  void cvtMS168K(const unsigned char* src, int ns, unsigned char* dst)
254 {
255     unsigned char v;
256
257     while (ns--) {
258         v = C168(R16(src));             src += 2;
259         *dst++ = v;
260         *dst++ = v;
261     }
262 }
263
264 static  void cvtMS1616K(const unsigned char* src, int ns, unsigned char* dst)
265 {
266     short       v;
267
268     while (ns--) {
269         v = R16(src);           src += 2;
270         W16(dst, v);            dst += 2;
271         W16(dst, v);            dst += 2;
272     }
273 }
274
275 static  void cvtSM88K(const unsigned char* src, int ns, unsigned char* dst)
276 {
277     while (ns--) {
278         *dst++ = M8(src[0], src[1]);
279         src += 2;
280     }
281 }
282
283 static  void cvtSM816K(const unsigned char* src, int ns, unsigned char* dst)
284 {
285     short       v;
286
287     while (ns--) {
288         v = M16(C816(src[0]), C816(src[1]));
289         src += 2;
290         W16(dst, v);            dst += 2;
291     }
292 }
293
294 static  void cvtSM168K(const unsigned char* src, int ns, unsigned char* dst)
295 {
296     while (ns--) {
297         *dst++ = C168(M16(R16(src), R16(src + 2)));
298         src += 4;
299     }
300 }
301
302 static  void cvtSM1616K(const unsigned char* src, int ns, unsigned char* dst)
303 {
304     while (ns--) {
305         W16(dst, M16(R16(src),R16(src+2)));     dst += 2;
306         src += 4;
307     }
308 }
309
310 static  void cvtMM816K(const unsigned char* src, int ns, unsigned char* dst)
311 {
312     while (ns--) {
313         W16(dst, C816(*src++));         dst += 2;
314     }
315 }
316
317 static  void cvtSS816K(const unsigned char* src, int ns, unsigned char* dst)
318 {
319     while (ns--) {
320         W16(dst, C816(*src++)); dst += 2;
321         W16(dst, C816(*src++)); dst += 2;
322     }
323 }
324
325 static  void cvtMM168K(const unsigned char* src, int ns, unsigned char* dst)
326 {
327     while (ns--) {
328         *dst++ = C168(R16(src));        src += 2;
329     }
330 }
331
332 static  void cvtSS168K(const unsigned char* src, int ns, unsigned char* dst)
333 {
334     while (ns--) {
335         *dst++ = C168(R16(src));        src += 2;
336         *dst++ = C168(R16(src));        src += 2;
337     }
338 }
339
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,
345 };
346
347 /***********************************************************************
348  *           I
349  *
350  * Interpolate the value at r (r in ]0, 1]) between the two points v1 and v2
351  * Linear interpolation is used
352  */
353 static  inline double   I(double v1, double v2, double r)
354 {
355     if (0.0 >= r || r > 1.0) FIXME("r!! %f\n", r);
356     return (1.0 - r) * v1 + r * v2;
357 }
358
359 static  void cvtSS88C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
360                       unsigned char* dst, LPDWORD ndst)
361 {
362     double              r;
363
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)--;
371         }
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)--;
377     }
378 }
379
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)
386  *
387  */
388 static  void cvtSM88C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
389                       unsigned char* dst, LPDWORD ndst)
390 {
391     double      r;
392
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)--;
400         }
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)--;
405     }
406 }
407
408 static  void cvtMS88C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
409                       unsigned char* dst, LPDWORD ndst)
410 {
411     double      r;
412
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)--;
419         }
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)--;
425     }
426 }
427
428 static  void cvtMM88C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
429                       unsigned char* dst, LPDWORD ndst)
430 {
431     double      r;
432
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)--;
439         }
440         /* now do the interpolation */
441         *dst++ = I(apd->last[0].b, src[0], r);
442         apd->dstPos += apd->dstIncr;
443         (*ndst)--;
444     }
445 }
446
447 static  void cvtSS816C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
448                        unsigned char* dst, LPDWORD ndst)
449 {
450     double      r;
451
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)--;
459         }
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)--;
465     }
466 }
467
468 static  void cvtSM816C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
469                         unsigned char* dst, LPDWORD ndst)
470 {
471     double      r;
472
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)--;
480         }
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)--;
487     }
488 }
489
490 static  void cvtMS816C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
491                         unsigned char* dst, LPDWORD ndst)
492 {
493     double      r;
494     short       v;
495
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)--;
502         }
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)--;
509     }
510 }
511
512 static  void cvtMM816C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
513                         unsigned char* dst, LPDWORD ndst)
514 {
515     double      r;
516
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)--;
523         }
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)--;
529     }
530 }
531
532 static  void cvtSS168C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
533                         unsigned char* dst, LPDWORD ndst)
534 {
535     double      r;
536
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)--;
544         }
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)--;
550     }
551 }
552
553 static  void cvtSM168C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
554                         unsigned char* dst, LPDWORD ndst)
555 {
556     double      r;
557
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)--;
565         }
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)--;
571     }
572 }
573
574
575 static  void cvtMS168C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
576                         unsigned char* dst, LPDWORD ndst)
577 {
578     double      r;
579
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)--;
586         }
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)--;
591     }
592 }
593
594
595 static  void cvtMM168C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
596                         unsigned char* dst, LPDWORD ndst)
597 {
598     double      r;
599
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)--;
606         }
607         /* now do the interpolation */
608         *dst++ = C168(I(apd->last[0].s, R16(src), r));
609         apd->dstPos += apd->dstIncr;
610         (*ndst)--;
611     }
612 }
613
614 static  void cvtSS1616C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
615                         unsigned char* dst, LPDWORD ndst)
616 {
617     double      r;
618
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)--;
626         }
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)--;
632     }
633 }
634
635 static  void cvtSM1616C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
636                         unsigned char* dst, LPDWORD ndst)
637 {
638     double      r;
639
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)--;
647         }
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)--;
654     }
655 }
656
657 static  void cvtMS1616C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
658                         unsigned char* dst, LPDWORD ndst)
659 {
660     double      r;
661     short       v;
662
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)--;
669         }
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)--;
676     }
677 }
678
679 static  void cvtMM1616C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
680                         unsigned char* dst, LPDWORD ndst)
681 {
682     double      r;
683
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)--;
690         }
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)--;
695     }
696 }
697
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,
705 };
706
707 /***********************************************************************
708  *           PCM_DriverDetails
709  *
710  */
711 static  LRESULT PCM_DriverDetails(PACMDRIVERDETAILSW add)
712 {
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;
732
733     return MMSYSERR_NOERROR;
734 }
735
736 /***********************************************************************
737  *           PCM_FormatTagDetails
738  *
739  */
740 static  LRESULT PCM_FormatTagDetails(PACMFORMATTAGDETAILSW aftd, DWORD dwQuery)
741 {
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;
757     }
758
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;
765
766     return MMSYSERR_NOERROR;
767 }
768
769 /***********************************************************************
770  *           PCM_FormatDetails
771  *
772  */
773 static  LRESULT PCM_FormatDetails(PACMFORMATDETAILSW afd, DWORD dwQuery)
774 {
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;
787          */
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;
796     }
797
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);
802
803     return MMSYSERR_NOERROR;
804 }
805
806 /***********************************************************************
807  *           PCM_FormatSuggest
808  *
809  */
810 static  LRESULT PCM_FormatSuggest(PACMDRVFORMATSUGGEST adfs)
811 {
812     /* some tests ... */
813     if (adfs->cbwfxSrc < sizeof(PCMWAVEFORMAT) ||
814         adfs->cbwfxDst < sizeof(PCMWAVEFORMAT) ||
815         PCM_GetFormatIndex(adfs->pwfxSrc) == 0xFFFFFFFF) return ACMERR_NOTPOSSIBLE;
816
817     /* is no suggestion for destination, then copy source value */
818     if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_NCHANNELS)) {
819         adfs->pwfxDst->nChannels = adfs->pwfxSrc->nChannels;
820     }
821     if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_NSAMPLESPERSEC)) {
822         adfs->pwfxDst->nSamplesPerSec = adfs->pwfxSrc->nSamplesPerSec;
823     }
824     if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_WBITSPERSAMPLE)) {
825         adfs->pwfxDst->wBitsPerSample = adfs->pwfxSrc->wBitsPerSample;
826     }
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;
830     }
831     /* check if result is ok */
832     if (PCM_GetFormatIndex(adfs->pwfxDst) == 0xFFFFFFFF) return ACMERR_NOTPOSSIBLE;
833
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;
837
838     return MMSYSERR_NOERROR;
839 }
840
841 /***********************************************************************
842  *           PCM_Reset
843  *
844  */
845 static  void    PCM_Reset(AcmPcmData* apd, int srcNumBits)
846 {
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;
856     }
857 }
858
859 /***********************************************************************
860  *           PCM_StreamOpen
861  *
862  */
863 static  LRESULT PCM_StreamOpen(PACMDRVSTREAMINSTANCE adsi)
864 {
865     AcmPcmData* apd;
866     int         idx = 0;
867
868     assert(!(adsi->fdwOpen & ACM_STREAMOPENF_ASYNC));
869
870     if (PCM_GetFormatIndex(adsi->pwfxSrc) == 0xFFFFFFFF ||
871         PCM_GetFormatIndex(adsi->pwfxDst) == 0xFFFFFFFF)
872         return ACMERR_NOTPOSSIBLE;
873
874     apd = HeapAlloc(GetProcessHeap(), 0, sizeof(AcmPcmData));
875     if (apd == 0) return MMSYSERR_NOMEM;
876
877     adsi->dwDriver = (DWORD)apd;
878     adsi->fdwDriver = 0;
879
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;
884
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];
893     }
894
895     return MMSYSERR_NOERROR;
896 }
897
898 /***********************************************************************
899  *           PCM_StreamClose
900  *
901  */
902 static  LRESULT PCM_StreamClose(PACMDRVSTREAMINSTANCE adsi)
903 {
904     HeapFree(GetProcessHeap(), 0, (void*)adsi->dwDriver);
905     return MMSYSERR_NOERROR;
906 }
907
908 /***********************************************************************
909  *           PCM_round
910  *
911  */
912 static  inline DWORD    PCM_round(DWORD a, DWORD b, DWORD c)
913 {
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;
917 }
918
919 /***********************************************************************
920  *           PCM_StreamSize
921  *
922  */
923 static  LRESULT PCM_StreamSize(PACMDRVSTREAMINSTANCE adsi, PACMDRVSTREAMSIZE adss)
924 {
925     DWORD       srcMask = ~(adsi->pwfxSrc->nBlockAlign - 1);
926     DWORD       dstMask = ~(adsi->pwfxDst->nBlockAlign - 1);
927
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;
944     }
945     return MMSYSERR_NOERROR;
946 }
947
948 /***********************************************************************
949  *           PCM_StreamConvert
950  *
951  */
952 static LRESULT PCM_StreamConvert(PACMDRVSTREAMINSTANCE adsi, PACMDRVSTREAMHEADER adsh)
953 {
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);
957
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);
963     }
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
968      */
969     if ((adsh->fdwConvert & ACM_STREAMCONVERTF_START) &&
970         (adsi->fdwDriver & PCM_RESAMPLE)) {
971         PCM_Reset(apd, adsi->pwfxSrc->wBitsPerSample);
972     }
973
974     /* do the job */
975     if (adsi->fdwDriver & PCM_RESAMPLE) {
976         DWORD   nsrc2 = nsrc;
977         DWORD   ndst2 = ndst;
978
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;
984
985         /* nsrc is now equal to ndst */
986         apd->cvt.cvtKeepRate(adsh->pbSrc, nsrc, adsh->pbDst);
987     }
988
989     adsh->cbSrcLengthUsed = nsrc * adsi->pwfxSrc->nBlockAlign;
990     adsh->cbDstLengthUsed = ndst * adsi->pwfxDst->nBlockAlign;
991
992     return MMSYSERR_NOERROR;
993 }
994
995 /**************************************************************************
996  *                      DriverProc (MSACM32.@)
997  */
998 LRESULT CALLBACK        PCM_DriverProc(DWORD dwDevID, HDRVR hDriv, UINT wMsg,
999                                        LPARAM dwParam1, LPARAM dwParam2)
1000 {
1001     TRACE("(%08lx %08lx %u %08lx %08lx);\n",
1002           dwDevID, (DWORD)hDriv, wMsg, dwParam1, dwParam2);
1003
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;
1015
1016     case ACMDM_DRIVER_NOTIFY:
1017         /* no caching from other ACM drivers is done so far */
1018         return MMSYSERR_NOERROR;
1019
1020     case ACMDM_DRIVER_DETAILS:
1021         return PCM_DriverDetails((PACMDRIVERDETAILSW)dwParam1);
1022
1023     case ACMDM_FORMATTAG_DETAILS:
1024         return PCM_FormatTagDetails((PACMFORMATTAGDETAILSW)dwParam1, dwParam2);
1025
1026     case ACMDM_FORMAT_DETAILS:
1027         return PCM_FormatDetails((PACMFORMATDETAILSW)dwParam1, dwParam2);
1028
1029     case ACMDM_FORMAT_SUGGEST:
1030         return PCM_FormatSuggest((PACMDRVFORMATSUGGEST)dwParam1);
1031
1032     case ACMDM_STREAM_OPEN:
1033         return PCM_StreamOpen((PACMDRVSTREAMINSTANCE)dwParam1);
1034
1035     case ACMDM_STREAM_CLOSE:
1036         return PCM_StreamClose((PACMDRVSTREAMINSTANCE)dwParam1);
1037
1038     case ACMDM_STREAM_SIZE:
1039         return PCM_StreamSize((PACMDRVSTREAMINSTANCE)dwParam1, (PACMDRVSTREAMSIZE)dwParam2);
1040
1041     case ACMDM_STREAM_CONVERT:
1042         return PCM_StreamConvert((PACMDRVSTREAMINSTANCE)dwParam1, (PACMDRVSTREAMHEADER)dwParam2);
1043
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;
1056
1057     default:
1058         return DefDriverProc(dwDevID, hDriv, wMsg, dwParam1, dwParam2);
1059     }
1060     return 0;
1061 }