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