Added a spec file for 16bit imm.
[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  *      FIXME / TODO list
9  *      + most of the computation should be done in fixed point arithmetic 
10  *        instead of floating point (16 bits for integral part, and 16 bits
11  *        for fractional part for example)
12  *      + implement PCM_FormatSuggest function
13  *      + get rid of hack for PCM_DriverProc (msacm32.dll shouldn't export
14  *        a DriverProc, but this would require implementing a generic
15  *        embedded driver handling scheme in msacm32.dll which isn't done yet
16  */
17
18 #include <assert.h>
19 #include "winnls.h"
20 #include "winbase.h"
21 #include "wingdi.h"
22 #include "winuser.h"
23 #include "msacm.h"
24 #include "msacmdrv.h"
25 #include "debugtools.h"
26
27 DEFAULT_DEBUG_CHANNEL(msacm);
28
29 /***********************************************************************
30  *           PCM_drvOpen
31  */
32 static  DWORD   PCM_drvOpen(LPCSTR str)
33 {
34     return 1;
35 }
36
37 /***********************************************************************
38  *           PCM_drvClose
39  */
40 static  DWORD   PCM_drvClose(DWORD dwDevID)
41 {
42     return 1;
43 }
44
45 #define NUM_PCM_FORMATS (sizeof(PCM_Formats) / sizeof(PCM_Formats[0]))
46 #define NUM_OF(a,b)     (((a)+(b)-1)/(b))
47
48 /* flags for fdwDriver */
49 #define PCM_RESAMPLE    1
50
51 /* data used while converting */
52 typedef struct tagAcmPcmData {
53     /* conversion routine, depending if rate conversion is required */
54     union {
55         void (*cvtKeepRate)(const unsigned char*, int, unsigned char*);
56         void (*cvtChangeRate)(struct tagAcmPcmData*, const unsigned char*, 
57                               LPDWORD, unsigned char*, LPDWORD);
58     } cvt;
59     /* the following fields are used only with rate conversion) */
60     DWORD       srcPos;         /* position in source stream */
61     double      dstPos;         /* position in destination stream */
62     double      dstIncr;        /* value to increment dst stream when src stream 
63                                    is incremented by 1 */
64     /* last source stream value read */
65     union {
66         unsigned char   b;      /*  8 bit value */
67         short           s;      /* 16 bit value */
68     } last[2]; /* two channels max (stereo) */
69 } AcmPcmData;
70
71 /* table to list all supported formats... those are the basic ones. this
72  * also helps given a unique index to each of the supported formats
73  */
74 static  struct {
75     int         nChannels;
76     int         nBits;
77     int         rate;
78 } PCM_Formats[] = {
79     {1,  8,  8000}, {2,  8,  8000}, {1, 16,  8000}, {2, 16,  8000},
80     {1,  8, 11025}, {2,  8, 11025}, {1, 16, 11025}, {2, 16, 11025},
81     {1,  8, 22050}, {2,  8, 22050}, {1, 16, 22050}, {2, 16, 22050},
82     {1,  8, 44100}, {2,  8, 44100}, {1, 16, 44100}, {2, 16, 44100},
83 };
84
85 /***********************************************************************
86  *           PCM_GetFormatIndex
87  */
88 static  DWORD   PCM_GetFormatIndex(LPWAVEFORMATEX wfx)
89 {
90     int i;
91     
92     for (i = 0; i < NUM_PCM_FORMATS; i++) {
93         if (wfx->nChannels == PCM_Formats[i].nChannels &&
94             wfx->nSamplesPerSec == PCM_Formats[i].rate &&
95             wfx->wBitsPerSample == PCM_Formats[i].nBits)
96             return i;
97     }
98     return 0xFFFFFFFF;
99 }
100
101 /* PCM Conversions:
102  *
103  * parameters:
104  *      + 8 bit unsigned vs 16 bit signed
105  *      + mono vs stereo (1 or 2 channels)
106  *      + sampling rate (8.0, 11.025, 22.05, 44.1 kHz are defined, but algo shall work
107  *        in all cases)
108  *
109  * mono => stereo: copy the same sample on Left & Right channels
110  * stereo =) mono: use the average value of samples from Left & Right channels
111  * resampling; we lookup for each destination sample the two source adjacent samples
112  *      were src <= dst < src+1 (dst is increased by a fractional value which is 
113  *      equivalent to the increment by one on src); then we use a linear
114  *      interpolation between src and src+1
115  */
116
117 /***********************************************************************
118  *           C816
119  *
120  * Converts a 8 bit sample to a 16 bit one
121  */
122 static inline short C816(unsigned char b) 
123 {
124     return (short)(b ^ 0x80) * 256;
125 }
126
127 /***********************************************************************
128  *           C168
129  *
130  * Converts a 16 bit sample to a 8 bit one (data loss !!)
131  */
132 static inline unsigned char C168(short s) 
133 {
134     return HIBYTE(s) ^ (unsigned char)0x80;
135 }
136
137 /***********************************************************************
138  *           R16
139  *
140  * Read a 16 bit sample (correctly handles endianess)
141  */
142 static inline short  R16(const unsigned char* src)
143 {
144     return (short)((unsigned short)src[0] | ((unsigned short)src[1] << 8));
145 }
146
147 /***********************************************************************
148  *           W16
149  *
150  * Write a 16 bit sample (correctly handles endianess)
151  */
152 static inline void  W16(unsigned char* dst, short s)
153 {
154     dst[0] = LOBYTE(s);
155     dst[1] = HIBYTE(s);
156 }
157
158 /***********************************************************************
159  *           M16
160  *
161  * Convert the (l,r) 16 bit stereo sample into a 16 bit mono 
162  * (takes the mid-point of the two values)
163  */
164 static inline short M16(short l, short r)
165 {
166     return (l + r) / 2;
167 }
168
169 /***********************************************************************
170  *           M8
171  *
172  * Convert the (l,r) 8 bit stereo sample into a 8 bit mono 
173  * (takes the mid-point of the two values)
174  */
175 static inline unsigned char M8(unsigned char a, unsigned char b)
176 {
177     return (unsigned char)((a + b) / 2);
178 }
179
180 /* the conversion routines without rate conversion are labelled cvt<X><Y><N><M>K
181  * where :
182  * <X> is the (M)ono/(S)tereo configuration of  input channel
183  * <Y> is the (M)ono/(S)tereo configuration of output channel
184  * <N> is the number of bits of  input channel (8 or 16)
185  * <M> is the number of bits of output channel (8 or 16)
186  *
187  * in the parameters, ns is always the number of samples, so the size of input
188  * buffer (resp output buffer) is ns * (<X> == 'Mono' ? 1:2) * (<N> == 8 ? 1:2)
189  */
190
191 static  void cvtMM88K(const unsigned char* src, int ns, unsigned char* dst)
192 {
193     memcpy(dst, src, ns);
194 }
195
196 static  void cvtSS88K(const unsigned char* src, int ns, unsigned char* dst)
197 {
198     memcpy(dst, src, ns * 2);
199 }
200
201 static  void cvtMM1616K(const unsigned char* src, int ns, unsigned char* dst)
202 {
203     memcpy(dst, src, ns * 2);
204 }
205
206 static  void cvtSS1616K(const unsigned char* src, int ns, unsigned char* dst)
207 {
208     memcpy(dst, src, ns * 4);
209 }
210
211 static  void cvtMS88K(const unsigned char* src, int ns, unsigned char* dst)
212 {
213     while (ns--) {
214         *dst++ = *src;
215         *dst++ = *src++;
216     }
217 }
218
219 static  void cvtMS816K(const unsigned char* src, int ns, unsigned char* dst)
220 {
221     short       v;
222     
223     while (ns--) {
224         v = C816(*src++);
225         W16(dst, v);            dst += 2;
226         W16(dst, v);            dst += 2;
227     }
228 }
229
230 static  void cvtMS168K(const unsigned char* src, int ns, unsigned char* dst)
231 {
232     unsigned char v;
233     
234     while (ns--) {
235         v = C168(R16(src));             src += 2;
236         *dst++ = v;
237         *dst++ = v;
238     }
239 }
240
241 static  void cvtMS1616K(const unsigned char* src, int ns, unsigned char* dst)
242 {
243     short       v;
244
245     while (ns--) {
246         v = R16(src);           src += 2;
247         W16(dst, v);            dst += 2;
248         W16(dst, v);            dst += 2;
249     }
250 }
251
252 static  void cvtSM88K(const unsigned char* src, int ns, unsigned char* dst)
253 {
254     while (ns--) {
255         *dst++ = M8(src[0], src[1]);
256         src += 2;
257     }
258 }
259
260 static  void cvtSM816K(const unsigned char* src, int ns, unsigned char* dst)
261 {
262     short       v;
263     
264     while (ns--) {
265         v = M16(C816(src[0]), C816(src[1]));
266         src += 2;
267         W16(dst, v);            dst += 2;
268     }
269 }
270
271 static  void cvtSM168K(const unsigned char* src, int ns, unsigned char* dst)
272 {
273     while (ns--) {
274         *dst++ = C168(M16(R16(src), R16(src + 2)));
275         src += 4;
276     }
277 }
278
279 static  void cvtSM1616K(const unsigned char* src, int ns, unsigned char* dst)
280 {
281     while (ns--) {
282         W16(dst, M16(R16(src),R16(src+2)));     dst += 2;
283         src += 4;
284     }
285 }
286
287 static  void cvtMM816K(const unsigned char* src, int ns, unsigned char* dst)
288 {
289     while (ns--) {
290         W16(dst, C816(*src++));         dst += 2;
291     }
292 }
293
294 static  void cvtSS816K(const unsigned char* src, int ns, unsigned char* dst)
295 {
296     while (ns--) {
297         W16(dst, C816(*src++)); dst += 2;
298         W16(dst, C816(*src++)); dst += 2;
299     }
300 }
301
302 static  void cvtMM168K(const unsigned char* src, int ns, unsigned char* dst)
303 {
304     while (ns--) {
305         *dst++ = C168(R16(src));        src += 2;
306     }
307 }
308
309 static  void cvtSS168K(const unsigned char* src, int ns, unsigned char* dst)
310 {
311     while (ns--) {
312         *dst++ = C168(R16(src));        src += 2;
313         *dst++ = C168(R16(src));        src += 2;
314     }
315 }
316
317 static  void (*PCM_ConvertKeepRate[16])(const unsigned char*, int, unsigned char*) = {
318     cvtSS88K,   cvtSM88K,   cvtMS88K,   cvtMM88K,
319     cvtSS816K,  cvtSM816K,  cvtMS816K,  cvtMM816K,
320     cvtSS168K,  cvtSM168K,  cvtMS168K,  cvtMM168K,
321     cvtSS1616K, cvtSM1616K, cvtMS1616K, cvtMM1616K,
322 };
323
324 /***********************************************************************
325  *           I
326  *
327  * Interpolate the value at r (r in ]0, 1]) between the two points v1 and v2
328  * Linear interpolation is used
329  */
330 static  inline double   I(double v1, double v2, double r)
331 {
332     if (0.0 >= r || r > 1.0) FIXME("r!! %f\n", r);
333     return (1.0 - r) * v1 + r * v2;
334 }
335
336 static  void cvtSS88C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc, 
337                       unsigned char* dst, LPDWORD ndst)
338 {
339     double              r;
340
341     while (*nsrc != 0 && *ndst != 0) {
342         while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
343             if (*nsrc == 0) return;
344             apd->last[0].b = *src++;
345             apd->last[1].b = *src++;
346             apd->srcPos++;
347             (*nsrc)--;
348         }
349         /* now do the interpolation */
350         *dst++ = I(apd->last[0].b, src[0], r);
351         *dst++ = I(apd->last[1].b, src[1], r);
352         apd->dstPos += apd->dstIncr;
353         (*ndst)--;
354     }
355 }
356
357 /* the conversion routines with rate conversion are labelled cvt<X><Y><N><M>C
358  * where :
359  * <X> is the (M)ono/(S)tereo configuration of  input channel
360  * <Y> is the (M)ono/(S)tereo configuration of output channel
361  * <N> is the number of bits of  input channel (8 or 16)
362  * <M> is the number of bits of output channel (8 or 16)
363  *
364  */
365 static  void cvtSM88C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc, 
366                       unsigned char* dst, LPDWORD ndst)
367 {
368     double      r;
369
370     while (*nsrc != 0 && *ndst != 0) {
371         while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
372             if (*nsrc == 0) return;
373             apd->last[0].b = *src++;
374             apd->last[1].b = *src++;
375             apd->srcPos++;
376             (*nsrc)--;
377         }
378         /* now do the interpolation */
379         *dst++ = I(M8(apd->last[0].b, apd->last[1].b), M8(src[0], src[1]), r);
380         apd->dstPos += apd->dstIncr;
381         (*ndst)--;
382     }
383 }
384
385 static  void cvtMS88C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc, 
386                       unsigned char* dst, LPDWORD ndst)
387 {
388     double      r;
389
390     while (*nsrc != 0 && *ndst != 0) {
391         while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
392             if (*nsrc == 0) return;
393             apd->last[0].b = *src++;
394             apd->srcPos++;
395             (*nsrc)--;
396         }
397         /* now do the interpolation */
398         dst[0] = dst[1] = I(apd->last[0].b, src[0], r);
399         dst += 2;
400         apd->dstPos += apd->dstIncr;
401         (*ndst)--;
402     }
403 }
404
405 static  void cvtMM88C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc, 
406                       unsigned char* dst, LPDWORD ndst)
407 {
408     double      r;
409
410     while (*nsrc != 0 && *ndst != 0) {
411         while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
412             if (*nsrc == 0) return;
413             apd->last[0].b = *src++;
414             apd->srcPos++;
415             (*nsrc)--;
416         }
417         /* now do the interpolation */
418         *dst++ = I(apd->last[0].b, src[0], r);
419         apd->dstPos += apd->dstIncr;
420         (*ndst)--;
421     }
422 }
423
424 static  void cvtSS816C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc, 
425                        unsigned char* dst, LPDWORD ndst)
426 {
427     double      r;
428         
429     while (*nsrc != 0 && *ndst != 0) {
430         while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
431             if (*nsrc == 0) return;
432             apd->last[0].b = *src++;
433             apd->last[1].b = *src++;
434             apd->srcPos++;
435             (*nsrc)--;
436         }
437         /* now do the interpolation */
438         W16(dst, I(C816(apd->last[0].b), C816(src[0]), r));     dst += 2;
439         W16(dst, I(C816(apd->last[1].b), C816(src[1]), r));     dst += 2;
440         apd->dstPos += apd->dstIncr;
441         (*ndst)--;
442     }
443 }
444
445 static  void cvtSM816C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc, 
446                         unsigned char* dst, LPDWORD ndst)
447 {
448     double      r;
449
450     while (*nsrc != 0 && *ndst != 0) {
451         while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
452             if (*nsrc == 0) return;
453             apd->last[0].b = *src++;
454             apd->last[1].b = *src++;
455             apd->srcPos++;
456             (*nsrc)--;
457         }
458         /* now do the interpolation */
459         W16(dst, I(M16(C816(apd->last[0].b), C816(apd->last[1].b)),
460                     M16(C816(src[0]), C816(src[1])), r));
461         dst += 2;
462         apd->dstPos += apd->dstIncr;
463         (*ndst)--;
464     }
465 }
466
467 static  void cvtMS816C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc, 
468                         unsigned char* dst, LPDWORD ndst)
469 {
470     double      r;
471     short       v;
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->srcPos++;
478             (*nsrc)--;
479         }
480         /* now do the interpolation */
481         v = I(C816(apd->last[0].b), C816(src[0]), r);
482         W16(dst, v);            dst += 2;
483         W16(dst, v);            dst += 2;
484         apd->dstPos += apd->dstIncr;
485         (*ndst)--;
486     }
487 }
488
489 static  void cvtMM816C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc, 
490                         unsigned char* dst, LPDWORD ndst)
491 {
492     double      r;
493
494     while (*nsrc != 0 && *ndst != 0) {
495         while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
496             if (*nsrc == 0) return;
497             apd->last[0].b = *src++;
498             apd->srcPos++;
499             (*nsrc)--;
500         }
501         /* now do the interpolation */
502         W16(dst, I(C816(apd->last[0].b), C816(src[0]), r));
503         dst += 2;
504         apd->dstPos += apd->dstIncr;
505         (*ndst)--;
506     }
507 }
508
509 static  void cvtSS168C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc, 
510                         unsigned char* dst, LPDWORD ndst)
511 {
512     double      r;
513
514     while (*nsrc != 0 && *ndst != 0) {
515         while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
516             if (*nsrc == 0) return;
517             apd->last[0].s = R16(src);  src += 2;
518             apd->last[1].s = R16(src);  src += 2;
519             apd->srcPos++;
520             (*nsrc)--;
521         }
522         /* now do the interpolation */
523         *dst++ = C168(I(apd->last[0].s, R16(src)  , r));
524         *dst++ = C168(I(apd->last[1].s, R16(src+2), r));
525         apd->dstPos += apd->dstIncr;
526         (*ndst)--;
527     }
528 }
529
530 static  void cvtSM168C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc, 
531                         unsigned char* dst, LPDWORD ndst)
532 {
533     double      r;
534
535     while (*nsrc != 0 && *ndst != 0) {
536         while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
537             if (*nsrc == 0) return;
538             apd->last[0].s = R16(src);  src += 2;
539             apd->last[1].s = R16(src);  src += 2;
540             apd->srcPos++;
541             (*nsrc)--;
542         }
543         /* now do the interpolation */
544         *dst++ = C168(I(M16(apd->last[0].s, apd->last[1].s), 
545                         M16(R16(src), R16(src + 2)), r));
546         apd->dstPos += apd->dstIncr;
547         (*ndst)--;
548     }
549 }
550
551
552 static  void cvtMS168C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc, 
553                         unsigned char* dst, LPDWORD ndst)
554 {
555     double      r;
556
557     while (*nsrc != 0 && *ndst != 0) {
558         while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
559             if (*nsrc == 0) return;
560             apd->last[0].s = R16(src);  src += 2;
561             apd->srcPos++;
562             (*nsrc)--;
563         }
564         /* now do the interpolation */
565         dst[0] = dst[1] = C168(I(apd->last[0].s, R16(src), r)); dst += 2;
566         apd->dstPos += apd->dstIncr;
567         (*ndst)--;
568     }
569 }
570
571
572 static  void cvtMM168C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc, 
573                         unsigned char* dst, LPDWORD ndst)
574 {
575     double      r;
576
577     while (*nsrc != 0 && *ndst != 0) {
578         while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
579             if (*nsrc == 0) return;
580             apd->last[0].s = R16(src);  src += 2;
581             apd->srcPos++;
582             (*nsrc)--;
583         }
584         /* now do the interpolation */
585         *dst++ = C168(I(apd->last[0].s, R16(src), r));
586         apd->dstPos += apd->dstIncr;
587         (*ndst)--;
588     }
589 }
590
591 static  void cvtSS1616C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc, 
592                         unsigned char* dst, LPDWORD ndst)
593 {
594     double      r;
595
596     while (*nsrc != 0 && *ndst != 0) {
597         while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
598             if (*nsrc == 0) return;
599             apd->last[0].s = R16(src);  src += 2;
600             apd->last[1].s = R16(src);  src += 2;
601             apd->srcPos++;
602             (*nsrc)--;
603         }
604         /* now do the interpolation */
605         W16(dst, I(apd->last[0].s, R16(src)  , r));     dst += 2;
606         W16(dst, I(apd->last[1].s, R16(src+2), r));     dst += 2;
607         apd->dstPos += apd->dstIncr;
608         (*ndst)--;
609     }
610 }
611
612 static  void cvtSM1616C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc, 
613                         unsigned char* dst, LPDWORD ndst)
614 {
615     double      r;
616
617     while (*nsrc != 0 && *ndst != 0) {
618         while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
619             if (*nsrc == 0) return;
620             apd->last[0].s = R16(src);  src += 2;
621             apd->last[1].s = R16(src);  src += 2;
622             apd->srcPos++;
623             (*nsrc)--;
624         }
625         /* now do the interpolation */
626         W16(dst, I(M16(apd->last[0].s, apd->last[1].s),
627                    M16(R16(src), R16(src+2)), r));
628         dst += 2;
629         apd->dstPos += apd->dstIncr;
630         (*ndst)--;
631     }
632 }
633
634 static  void cvtMS1616C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc, 
635                         unsigned char* dst, LPDWORD ndst)
636 {
637     double      r;
638     short       v;
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->srcPos++;
645             (*nsrc)--;
646         }
647         /* now do the interpolation */
648         v = I(apd->last[0].s, R16(src), r);
649         W16(dst, v);            dst += 2;
650         W16(dst, v);            dst += 2;
651         apd->dstPos += apd->dstIncr;
652         (*ndst)--;
653     }
654 }
655
656 static  void cvtMM1616C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc, 
657                         unsigned char* dst, LPDWORD ndst)
658 {
659     double      r;
660
661     while (*nsrc != 0 && *ndst != 0) {
662         while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
663             if (*nsrc == 0) return;
664             apd->last[0].s = R16(src);  src += 2;
665             apd->srcPos++;
666             (*nsrc)--;
667         }
668         /* now do the interpolation */
669         W16(dst, I(apd->last[0].s, R16(src), r));       dst += 2;
670         apd->dstPos += apd->dstIncr;
671         (*ndst)--;
672     }
673 }
674
675 static  void (*PCM_ConvertChangeRate[16])(AcmPcmData* apd, 
676                                           const unsigned char* src, LPDWORD nsrc, 
677                                           unsigned char* dst, LPDWORD ndst) = {
678     cvtSS88C,   cvtSM88C,   cvtMS88C,   cvtMM88C,
679     cvtSS816C,  cvtSM816C,  cvtMS816C,  cvtMM816C,
680     cvtSS168C,  cvtSM168C,  cvtMS168C,  cvtMM168C,
681     cvtSS1616C, cvtSM1616C, cvtMS1616C, cvtMM1616C,
682 };
683
684 /***********************************************************************
685  *           PCM_DriverDetails
686  *
687  */
688 static  LRESULT PCM_DriverDetails(PACMDRIVERDETAILSW add)
689 {
690     add->fccType = ACMDRIVERDETAILS_FCCTYPE_AUDIOCODEC;
691     add->fccComp = ACMDRIVERDETAILS_FCCCOMP_UNDEFINED;
692     add->wMid = 0xFF;
693     add->wPid = 0x00;
694     add->vdwACM = 0x01000000;
695     add->vdwDriver = 0x01000000;
696     add->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CONVERTER;
697     add->cFormatTags = 1;
698     add->cFilterTags = 0;
699     add->hicon = (HICON)0;
700     MultiByteToWideChar( CP_ACP, 0, "WINE-PCM", -1,
701                          add->szShortName, sizeof(add->szShortName)/sizeof(WCHAR) );
702     MultiByteToWideChar( CP_ACP, 0, "Wine PCM converter", -1,
703                          add->szLongName, sizeof(add->szLongName)/sizeof(WCHAR) );
704     MultiByteToWideChar( CP_ACP, 0, "Brought to you by the Wine team...", -1,
705                          add->szCopyright, sizeof(add->szCopyright)/sizeof(WCHAR) );
706     MultiByteToWideChar( CP_ACP, 0, "Refer to LICENSE file", -1,
707                          add->szLicensing, sizeof(add->szLicensing)/sizeof(WCHAR) );
708     add->szFeatures[0] = 0;
709     
710     return MMSYSERR_NOERROR;
711 }
712
713 /***********************************************************************
714  *           PCM_FormatTagDetails
715  *
716  */
717 static  LRESULT PCM_FormatTagDetails(PACMFORMATTAGDETAILSW aftd, DWORD dwQuery)
718 {
719     switch (dwQuery) {
720     case ACM_FORMATTAGDETAILSF_INDEX:
721         if (aftd->dwFormatTagIndex != 0) return ACMERR_NOTPOSSIBLE;
722         break;
723     case ACM_FORMATTAGDETAILSF_FORMATTAG: 
724         if (aftd->dwFormatTag != WAVE_FORMAT_PCM) return ACMERR_NOTPOSSIBLE;
725         break;
726     case ACM_FORMATTAGDETAILSF_LARGESTSIZE:
727         if (aftd->dwFormatTag != WAVE_FORMAT_UNKNOWN && 
728             aftd->dwFormatTag != WAVE_FORMAT_UNKNOWN)
729             return ACMERR_NOTPOSSIBLE;
730         break;
731     default:
732         WARN("Unsupported query %08lx\n", dwQuery);
733         return MMSYSERR_NOTSUPPORTED;
734     }
735     
736     aftd->dwFormatTagIndex = 0;
737     aftd->dwFormatTag = WAVE_FORMAT_PCM;
738     aftd->cbFormatSize = sizeof(PCMWAVEFORMAT);
739     aftd->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CONVERTER;
740     aftd->cStandardFormats = NUM_PCM_FORMATS;
741     aftd->szFormatTag[0] = 0;
742     
743     return MMSYSERR_NOERROR;
744 }
745
746 /***********************************************************************
747  *           PCM_FormatDetails
748  *
749  */
750 static  LRESULT PCM_FormatDetails(PACMFORMATDETAILSW afd, DWORD dwQuery)
751 {
752     switch (dwQuery) {
753     case ACM_FORMATDETAILSF_FORMAT:
754         afd->dwFormatIndex = PCM_GetFormatIndex(afd->pwfx);
755         if (afd->dwFormatIndex == 0xFFFFFFFF) return ACMERR_NOTPOSSIBLE;
756         break;
757     case ACM_FORMATDETAILSF_INDEX:
758         assert(afd->dwFormatIndex < NUM_PCM_FORMATS);
759         afd->pwfx->wFormatTag = WAVE_FORMAT_PCM;
760         afd->pwfx->nChannels = PCM_Formats[afd->dwFormatIndex].nChannels;
761         afd->pwfx->nSamplesPerSec = PCM_Formats[afd->dwFormatIndex].rate;
762         afd->pwfx->wBitsPerSample = PCM_Formats[afd->dwFormatIndex].nBits;
763         /* native MSACM uses a PCMWAVEFORMAT structure, so cbSize is not accessible
764          * afd->pwfx->cbSize = 0; 
765          */
766         afd->pwfx->nBlockAlign = 
767             (afd->pwfx->nChannels * afd->pwfx->wBitsPerSample) / 8;
768         afd->pwfx->nAvgBytesPerSec = 
769             afd->pwfx->nSamplesPerSec * afd->pwfx->nBlockAlign;
770         break;
771     default:
772         WARN("Unsupported query %08lx\n", dwQuery);
773         return MMSYSERR_NOTSUPPORTED;   
774     }
775     
776     afd->dwFormatTag = WAVE_FORMAT_PCM;
777     afd->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CONVERTER;
778     afd->szFormat[0] = 0; /* let MSACM format this for us... */
779     
780     return MMSYSERR_NOERROR;
781 }
782
783 /***********************************************************************
784  *           PCM_FormatSuggest
785  *
786  */
787 static  LRESULT PCM_FormatSuggest(PACMDRVFORMATSUGGEST adfs)
788 {
789     FIXME("(%p);\n", adfs);
790     return MMSYSERR_NOTSUPPORTED;
791 }
792
793 /***********************************************************************
794  *           PCM_Reset
795  *
796  */
797 static  void    PCM_Reset(AcmPcmData* apd, int srcNumBits)
798 {
799     apd->srcPos = 0;
800     apd->dstPos = 0;
801     /* initialize with neutral value */
802     if (srcNumBits == 16) {
803         apd->last[0].s = 0;
804         apd->last[1].s = 0;
805     } else {
806         apd->last[0].b = (BYTE)0x80;
807         apd->last[1].b = (BYTE)0x80;
808     }
809 }
810
811 /***********************************************************************
812  *           PCM_StreamOpen
813  *
814  */
815 static  LRESULT PCM_StreamOpen(PACMDRVSTREAMINSTANCE adsi)
816 {
817     AcmPcmData* apd;
818     int         idx = 0;
819
820     assert(!(adsi->fdwOpen & ACM_STREAMOPENF_ASYNC));
821     
822     if (PCM_GetFormatIndex(adsi->pwfxSrc) == 0xFFFFFFFF ||
823         PCM_GetFormatIndex(adsi->pwfxDst) == 0xFFFFFFFF)
824         return ACMERR_NOTPOSSIBLE;
825
826     apd = HeapAlloc(GetProcessHeap(), 0, sizeof(AcmPcmData));
827     if (apd == 0) return MMSYSERR_NOMEM;
828
829     adsi->dwDriver = (DWORD)apd;
830     adsi->fdwDriver = 0;
831     
832     if (adsi->pwfxSrc->wBitsPerSample == 16) idx += 8;
833     if (adsi->pwfxDst->wBitsPerSample == 16) idx += 4;
834     if (adsi->pwfxSrc->nChannels      == 1)  idx += 2;
835     if (adsi->pwfxDst->nChannels      == 1)  idx += 1;
836
837     if (adsi->pwfxSrc->nSamplesPerSec == adsi->pwfxDst->nSamplesPerSec) {
838         apd->cvt.cvtKeepRate = PCM_ConvertKeepRate[idx];
839     } else {
840         adsi->fdwDriver |= PCM_RESAMPLE;
841         apd->dstIncr = (double)(adsi->pwfxSrc->nSamplesPerSec) /
842             (double)(adsi->pwfxDst->nSamplesPerSec);
843         PCM_Reset(apd, adsi->pwfxSrc->wBitsPerSample);
844         apd->cvt.cvtChangeRate = PCM_ConvertChangeRate[idx];
845     }
846
847     return MMSYSERR_NOERROR;
848 }
849
850 /***********************************************************************
851  *           PCM_StreamClose
852  *
853  */
854 static  LRESULT PCM_StreamClose(PACMDRVSTREAMINSTANCE adsi)
855 {
856     HeapFree(GetProcessHeap(), 0, (void*)adsi->dwDriver);
857     return MMSYSERR_NOERROR;
858 }
859
860 /***********************************************************************
861  *           PCM_round
862  *
863  */
864 static  inline DWORD    PCM_round(DWORD a, DWORD b, DWORD c)
865 {
866     assert(a && b && c);
867     /* to be sure, always return an entire number of c... */
868     return ((double)a * (double)b + (double)c - 1) / (double)c;
869 }
870
871 /***********************************************************************
872  *           PCM_StreamSize
873  *
874  */
875 static  LRESULT PCM_StreamSize(PACMDRVSTREAMINSTANCE adsi, PACMDRVSTREAMSIZE adss)
876 {
877     switch (adss->fdwSize) {
878     case ACM_STREAMSIZEF_DESTINATION:
879         /* cbDstLength => cbSrcLength */
880         adss->cbSrcLength = PCM_round(adss->cbDstLength, 
881                                       adsi->pwfxSrc->nAvgBytesPerSec, 
882                                       adsi->pwfxDst->nAvgBytesPerSec);
883         break;
884     case ACM_STREAMSIZEF_SOURCE:
885         /* cbSrcLength => cbDstLength */
886         adss->cbDstLength =  PCM_round(adss->cbSrcLength, 
887                                        adsi->pwfxDst->nAvgBytesPerSec, 
888                                        adsi->pwfxSrc->nAvgBytesPerSec);
889         break;
890     default:
891         WARN("Unsupported query %08lx\n", adss->fdwSize);
892         return MMSYSERR_NOTSUPPORTED;   
893     }
894     return MMSYSERR_NOERROR;
895 }
896
897 /***********************************************************************
898  *           PCM_StreamConvert
899  *
900  */
901 static LRESULT PCM_StreamConvert(PACMDRVSTREAMINSTANCE adsi, PACMDRVSTREAMHEADER adsh)
902 {
903     AcmPcmData* apd = (AcmPcmData*)adsi->dwDriver;
904     DWORD       nsrc = NUM_OF(adsh->cbSrcLength, adsi->pwfxSrc->nBlockAlign);
905     DWORD       ndst = NUM_OF(adsh->cbDstLength, adsi->pwfxDst->nBlockAlign);
906
907     if (adsh->fdwConvert & 
908         ~(ACM_STREAMCONVERTF_BLOCKALIGN|
909           ACM_STREAMCONVERTF_END|
910           ACM_STREAMCONVERTF_START)) {
911         FIXME("Unsupported fdwConvert (%08lx), ignoring it\n", adsh->fdwConvert);
912     }
913     /* ACM_STREAMCONVERTF_BLOCKALIGN
914      *  currently all conversions are block aligned, so do nothing for this flag
915      * ACM_STREAMCONVERTF_END
916      *  no pending data, so do nothing for this flag
917      */
918     if ((adsh->fdwConvert & ACM_STREAMCONVERTF_START) && 
919         (adsi->fdwDriver & PCM_RESAMPLE)) {
920         PCM_Reset(apd, adsi->pwfxSrc->wBitsPerSample);
921     }
922
923     /* do the job */
924     if (adsi->fdwDriver & PCM_RESAMPLE) {
925         DWORD   nsrc2 = nsrc;
926         DWORD   ndst2 = ndst;
927
928         apd->cvt.cvtChangeRate(apd, adsh->pbSrc, &nsrc2, adsh->pbDst, &ndst2);
929         nsrc -= nsrc2;
930         ndst -= ndst2;
931     } else {
932         if (nsrc < ndst) ndst = nsrc; else nsrc = ndst;
933
934         /* nsrc is now equal to ndst */
935         apd->cvt.cvtKeepRate(adsh->pbSrc, nsrc, adsh->pbDst);
936     }
937
938     adsh->cbSrcLengthUsed = nsrc * adsi->pwfxSrc->nBlockAlign;
939     adsh->cbDstLengthUsed = ndst * adsi->pwfxDst->nBlockAlign;
940
941     return MMSYSERR_NOERROR;
942 }
943
944 /**************************************************************************
945  *                      PCM_DriverProc                  [exported]
946  */
947 LRESULT CALLBACK        PCM_DriverProc(DWORD dwDevID, HDRVR hDriv, UINT wMsg, 
948                                        LPARAM dwParam1, LPARAM dwParam2)
949 {
950     TRACE("(%08lx %08lx %u %08lx %08lx);\n", 
951           dwDevID, (DWORD)hDriv, wMsg, dwParam1, dwParam2);
952     
953     switch (wMsg) {
954     case DRV_LOAD:              return 1;
955     case DRV_FREE:              return 1;
956     case DRV_OPEN:              return PCM_drvOpen((LPSTR)dwParam1);
957     case DRV_CLOSE:             return PCM_drvClose(dwDevID);
958     case DRV_ENABLE:            return 1;       
959     case DRV_DISABLE:           return 1;
960     case DRV_QUERYCONFIGURE:    return 1;
961     case DRV_CONFIGURE:         MessageBoxA(0, "MSACM PCM filter !", "Wine Driver", MB_OK); return 1;
962     case DRV_INSTALL:           return DRVCNF_RESTART;
963     case DRV_REMOVE:            return DRVCNF_RESTART;
964         
965     case ACMDM_DRIVER_NOTIFY:
966         /* no caching from other ACM drivers is done so far */
967         return MMSYSERR_NOERROR;
968         
969     case ACMDM_DRIVER_DETAILS:
970         return PCM_DriverDetails((PACMDRIVERDETAILSW)dwParam1);
971         
972     case ACMDM_FORMATTAG_DETAILS:
973         return PCM_FormatTagDetails((PACMFORMATTAGDETAILSW)dwParam1, dwParam2);
974         
975     case ACMDM_FORMAT_DETAILS:
976         return PCM_FormatDetails((PACMFORMATDETAILSW)dwParam1, dwParam2);
977         
978     case ACMDM_FORMAT_SUGGEST:
979         return PCM_FormatSuggest((PACMDRVFORMATSUGGEST)dwParam1);
980         
981     case ACMDM_STREAM_OPEN:
982         return PCM_StreamOpen((PACMDRVSTREAMINSTANCE)dwParam1);
983         
984     case ACMDM_STREAM_CLOSE:
985         return PCM_StreamClose((PACMDRVSTREAMINSTANCE)dwParam1);
986         
987     case ACMDM_STREAM_SIZE:
988         return PCM_StreamSize((PACMDRVSTREAMINSTANCE)dwParam1, (PACMDRVSTREAMSIZE)dwParam2);
989         
990     case ACMDM_STREAM_CONVERT:
991         return PCM_StreamConvert((PACMDRVSTREAMINSTANCE)dwParam1, (PACMDRVSTREAMHEADER)dwParam2);
992         
993     case ACMDM_HARDWARE_WAVE_CAPS_INPUT:
994     case ACMDM_HARDWARE_WAVE_CAPS_OUTPUT:
995         /* this converter is not a hardware driver */
996     case ACMDM_FILTERTAG_DETAILS:
997     case ACMDM_FILTER_DETAILS:
998         /* this converter is not a filter */
999     case ACMDM_STREAM_RESET:
1000         /* only needed for asynchronous driver... we aren't, so just say it */
1001     case ACMDM_STREAM_PREPARE:
1002     case ACMDM_STREAM_UNPREPARE:
1003         /* nothing special to do here... so don't do anything */
1004         return MMSYSERR_NOTSUPPORTED;
1005         
1006     default:
1007         return DefDriverProc(dwDevID, hDriv, wMsg, dwParam1, dwParam2);
1008     }
1009     return 0;
1010 }