Added TASK_GetPtr/TASK_GetCurrent functions to get the TDB for a task
[wine/multimedia.git] / dlls / msacm / pcmconverter.c
blob9e68a1e25eb771522c1cb15248988b36d7741b7d
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
3 /*
4 * MSACM32 library
6 * Copyright 2000 Eric Pouech
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
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"
28 DEFAULT_DEBUG_CHANNEL(msacm);
30 /***********************************************************************
31 * PCM_drvOpen
33 static DWORD PCM_drvOpen(LPCSTR str)
35 return 1;
38 /***********************************************************************
39 * PCM_drvClose
41 static DWORD PCM_drvClose(DWORD dwDevID)
43 return 1;
46 #define NUM_PCM_FORMATS (sizeof(PCM_Formats) / sizeof(PCM_Formats[0]))
47 #define NUM_OF(a,b) (((a)+(b)-1)/(b))
49 /* flags for fdwDriver */
50 #define PCM_RESAMPLE 1
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;
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
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},
86 /***********************************************************************
87 * PCM_GetFormatIndex
89 static DWORD PCM_GetFormatIndex(LPWAVEFORMATEX wfx)
91 int i;
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;
99 return 0xFFFFFFFF;
102 /* PCM Conversions:
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)
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
118 /***********************************************************************
119 * C816
121 * Converts a 8 bit sample to a 16 bit one
123 static inline short C816(unsigned char b)
125 return (short)(b ^ 0x80) * 256;
128 /***********************************************************************
129 * C168
131 * Converts a 16 bit sample to a 8 bit one (data loss !!)
133 static inline unsigned char C168(short s)
135 return HIBYTE(s) ^ (unsigned char)0x80;
138 /***********************************************************************
139 * R16
141 * Read a 16 bit sample (correctly handles endianess)
143 static inline short R16(const unsigned char* src)
145 return (short)((unsigned short)src[0] | ((unsigned short)src[1] << 8));
148 /***********************************************************************
149 * W16
151 * Write a 16 bit sample (correctly handles endianess)
153 static inline void W16(unsigned char* dst, short s)
155 dst[0] = LOBYTE(s);
156 dst[1] = HIBYTE(s);
159 /***********************************************************************
160 * M16
162 * Convert the (l,r) 16 bit stereo sample into a 16 bit mono
163 * (takes the mid-point of the two values)
165 static inline short M16(short l, short r)
167 return (l + r) / 2;
170 /***********************************************************************
171 * M8
173 * Convert the (l,r) 8 bit stereo sample into a 8 bit mono
174 * (takes the mid-point of the two values)
176 static inline unsigned char M8(unsigned char a, unsigned char b)
178 return (unsigned char)((a + b) / 2);
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)
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)
192 static void cvtMM88K(const unsigned char* src, int ns, unsigned char* dst)
194 memcpy(dst, src, ns);
197 static void cvtSS88K(const unsigned char* src, int ns, unsigned char* dst)
199 memcpy(dst, src, ns * 2);
202 static void cvtMM1616K(const unsigned char* src, int ns, unsigned char* dst)
204 memcpy(dst, src, ns * 2);
207 static void cvtSS1616K(const unsigned char* src, int ns, unsigned char* dst)
209 memcpy(dst, src, ns * 4);
212 static void cvtMS88K(const unsigned char* src, int ns, unsigned char* dst)
214 while (ns--) {
215 *dst++ = *src;
216 *dst++ = *src++;
220 static void cvtMS816K(const unsigned char* src, int ns, unsigned char* dst)
222 short v;
224 while (ns--) {
225 v = C816(*src++);
226 W16(dst, v); dst += 2;
227 W16(dst, v); dst += 2;
231 static void cvtMS168K(const unsigned char* src, int ns, unsigned char* dst)
233 unsigned char v;
235 while (ns--) {
236 v = C168(R16(src)); src += 2;
237 *dst++ = v;
238 *dst++ = v;
242 static void cvtMS1616K(const unsigned char* src, int ns, unsigned char* dst)
244 short v;
246 while (ns--) {
247 v = R16(src); src += 2;
248 W16(dst, v); dst += 2;
249 W16(dst, v); dst += 2;
253 static void cvtSM88K(const unsigned char* src, int ns, unsigned char* dst)
255 while (ns--) {
256 *dst++ = M8(src[0], src[1]);
257 src += 2;
261 static void cvtSM816K(const unsigned char* src, int ns, unsigned char* dst)
263 short v;
265 while (ns--) {
266 v = M16(C816(src[0]), C816(src[1]));
267 src += 2;
268 W16(dst, v); dst += 2;
272 static void cvtSM168K(const unsigned char* src, int ns, unsigned char* dst)
274 while (ns--) {
275 *dst++ = C168(M16(R16(src), R16(src + 2)));
276 src += 4;
280 static void cvtSM1616K(const unsigned char* src, int ns, unsigned char* dst)
282 while (ns--) {
283 W16(dst, M16(R16(src),R16(src+2))); dst += 2;
284 src += 4;
288 static void cvtMM816K(const unsigned char* src, int ns, unsigned char* dst)
290 while (ns--) {
291 W16(dst, C816(*src++)); dst += 2;
295 static void cvtSS816K(const unsigned char* src, int ns, unsigned char* dst)
297 while (ns--) {
298 W16(dst, C816(*src++)); dst += 2;
299 W16(dst, C816(*src++)); dst += 2;
303 static void cvtMM168K(const unsigned char* src, int ns, unsigned char* dst)
305 while (ns--) {
306 *dst++ = C168(R16(src)); src += 2;
310 static void cvtSS168K(const unsigned char* src, int ns, unsigned char* dst)
312 while (ns--) {
313 *dst++ = C168(R16(src)); src += 2;
314 *dst++ = C168(R16(src)); src += 2;
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,
325 /***********************************************************************
328 * Interpolate the value at r (r in ]0, 1]) between the two points v1 and v2
329 * Linear interpolation is used
331 static inline double I(double v1, double v2, double r)
333 if (0.0 >= r || r > 1.0) FIXME("r!! %f\n", r);
334 return (1.0 - r) * v1 + r * v2;
337 static void cvtSS88C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
338 unsigned char* dst, LPDWORD ndst)
340 double r;
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)--;
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)--;
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)
366 static void cvtSM88C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
367 unsigned char* dst, LPDWORD ndst)
369 double r;
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)--;
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)--;
386 static void cvtMS88C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
387 unsigned char* dst, LPDWORD ndst)
389 double r;
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)--;
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)--;
406 static void cvtMM88C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
407 unsigned char* dst, LPDWORD ndst)
409 double r;
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)--;
418 /* now do the interpolation */
419 *dst++ = I(apd->last[0].b, src[0], r);
420 apd->dstPos += apd->dstIncr;
421 (*ndst)--;
425 static void cvtSS816C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
426 unsigned char* dst, LPDWORD ndst)
428 double r;
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)--;
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)--;
446 static void cvtSM816C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
447 unsigned char* dst, LPDWORD ndst)
449 double r;
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)--;
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)--;
468 static void cvtMS816C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
469 unsigned char* dst, LPDWORD ndst)
471 double r;
472 short v;
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)--;
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)--;
490 static void cvtMM816C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
491 unsigned char* dst, LPDWORD ndst)
493 double r;
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)--;
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)--;
510 static void cvtSS168C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
511 unsigned char* dst, LPDWORD ndst)
513 double r;
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)--;
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)--;
531 static void cvtSM168C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
532 unsigned char* dst, LPDWORD ndst)
534 double r;
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)--;
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)--;
553 static void cvtMS168C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
554 unsigned char* dst, LPDWORD ndst)
556 double r;
558 while (*nsrc != 0 && *ndst != 0) {
559 while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
560 if (*nsrc == 0) return;
561 apd->last[0].s = R16(src); src += 2;
562 apd->srcPos++;
563 (*nsrc)--;
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)--;
573 static void cvtMM168C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
574 unsigned char* dst, LPDWORD ndst)
576 double r;
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)--;
585 /* now do the interpolation */
586 *dst++ = C168(I(apd->last[0].s, R16(src), r));
587 apd->dstPos += apd->dstIncr;
588 (*ndst)--;
592 static void cvtSS1616C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
593 unsigned char* dst, LPDWORD ndst)
595 double r;
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)--;
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)--;
613 static void cvtSM1616C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
614 unsigned char* dst, LPDWORD ndst)
616 double r;
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)--;
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)--;
635 static void cvtMS1616C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
636 unsigned char* dst, LPDWORD ndst)
638 double r;
639 short v;
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)--;
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)--;
657 static void cvtMM1616C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
658 unsigned char* dst, LPDWORD ndst)
660 double r;
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)--;
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)--;
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,
685 /***********************************************************************
686 * PCM_DriverDetails
689 static LRESULT PCM_DriverDetails(PACMDRIVERDETAILSW add)
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;
711 return MMSYSERR_NOERROR;
714 /***********************************************************************
715 * PCM_FormatTagDetails
718 static LRESULT PCM_FormatTagDetails(PACMFORMATTAGDETAILSW aftd, DWORD dwQuery)
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;
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;
744 return MMSYSERR_NOERROR;
747 /***********************************************************************
748 * PCM_FormatDetails
751 static LRESULT PCM_FormatDetails(PACMFORMATDETAILSW afd, DWORD dwQuery)
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;
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;
777 afd->dwFormatTag = WAVE_FORMAT_PCM;
778 afd->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CONVERTER;
779 afd->szFormat[0] = 0; /* let MSACM format this for us... */
781 return MMSYSERR_NOERROR;
784 /***********************************************************************
785 * PCM_FormatSuggest
788 static LRESULT PCM_FormatSuggest(PACMDRVFORMATSUGGEST adfs)
790 FIXME("(%p);\n", adfs);
791 return MMSYSERR_NOTSUPPORTED;
794 /***********************************************************************
795 * PCM_Reset
798 static void PCM_Reset(AcmPcmData* apd, int srcNumBits)
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;
812 /***********************************************************************
813 * PCM_StreamOpen
816 static LRESULT PCM_StreamOpen(PACMDRVSTREAMINSTANCE adsi)
818 AcmPcmData* apd;
819 int idx = 0;
821 assert(!(adsi->fdwOpen & ACM_STREAMOPENF_ASYNC));
823 if (PCM_GetFormatIndex(adsi->pwfxSrc) == 0xFFFFFFFF ||
824 PCM_GetFormatIndex(adsi->pwfxDst) == 0xFFFFFFFF)
825 return ACMERR_NOTPOSSIBLE;
827 apd = HeapAlloc(GetProcessHeap(), 0, sizeof(AcmPcmData));
828 if (apd == 0) return MMSYSERR_NOMEM;
830 adsi->dwDriver = (DWORD)apd;
831 adsi->fdwDriver = 0;
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;
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];
848 return MMSYSERR_NOERROR;
851 /***********************************************************************
852 * PCM_StreamClose
855 static LRESULT PCM_StreamClose(PACMDRVSTREAMINSTANCE adsi)
857 HeapFree(GetProcessHeap(), 0, (void*)adsi->dwDriver);
858 return MMSYSERR_NOERROR;
861 /***********************************************************************
862 * PCM_round
865 static inline DWORD PCM_round(DWORD a, DWORD b, DWORD c)
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;
872 /***********************************************************************
873 * PCM_StreamSize
876 static LRESULT PCM_StreamSize(PACMDRVSTREAMINSTANCE adsi, PACMDRVSTREAMSIZE adss)
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;
895 return MMSYSERR_NOERROR;
898 /***********************************************************************
899 * PCM_StreamConvert
902 static LRESULT PCM_StreamConvert(PACMDRVSTREAMINSTANCE adsi, PACMDRVSTREAMHEADER adsh)
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);
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);
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
919 if ((adsh->fdwConvert & ACM_STREAMCONVERTF_START) &&
920 (adsi->fdwDriver & PCM_RESAMPLE)) {
921 PCM_Reset(apd, adsi->pwfxSrc->wBitsPerSample);
924 /* do the job */
925 if (adsi->fdwDriver & PCM_RESAMPLE) {
926 DWORD nsrc2 = nsrc;
927 DWORD ndst2 = ndst;
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;
935 /* nsrc is now equal to ndst */
936 apd->cvt.cvtKeepRate(adsh->pbSrc, nsrc, adsh->pbDst);
939 adsh->cbSrcLengthUsed = nsrc * adsi->pwfxSrc->nBlockAlign;
940 adsh->cbDstLengthUsed = ndst * adsi->pwfxDst->nBlockAlign;
942 return MMSYSERR_NOERROR;
945 /**************************************************************************
946 * PCM_DriverProc [exported]
948 LRESULT CALLBACK PCM_DriverProc(DWORD dwDevID, HDRVR hDriv, UINT wMsg,
949 LPARAM dwParam1, LPARAM dwParam2)
951 TRACE("(%08lx %08lx %u %08lx %08lx);\n",
952 dwDevID, (DWORD)hDriv, wMsg, dwParam1, dwParam2);
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;
966 case ACMDM_DRIVER_NOTIFY:
967 /* no caching from other ACM drivers is done so far */
968 return MMSYSERR_NOERROR;
970 case ACMDM_DRIVER_DETAILS:
971 return PCM_DriverDetails((PACMDRIVERDETAILSW)dwParam1);
973 case ACMDM_FORMATTAG_DETAILS:
974 return PCM_FormatTagDetails((PACMFORMATTAGDETAILSW)dwParam1, dwParam2);
976 case ACMDM_FORMAT_DETAILS:
977 return PCM_FormatDetails((PACMFORMATDETAILSW)dwParam1, dwParam2);
979 case ACMDM_FORMAT_SUGGEST:
980 return PCM_FormatSuggest((PACMDRVFORMATSUGGEST)dwParam1);
982 case ACMDM_STREAM_OPEN:
983 return PCM_StreamOpen((PACMDRVSTREAMINSTANCE)dwParam1);
985 case ACMDM_STREAM_CLOSE:
986 return PCM_StreamClose((PACMDRVSTREAMINSTANCE)dwParam1);
988 case ACMDM_STREAM_SIZE:
989 return PCM_StreamSize((PACMDRVSTREAMINSTANCE)dwParam1, (PACMDRVSTREAMSIZE)dwParam2);
991 case ACMDM_STREAM_CONVERT:
992 return PCM_StreamConvert((PACMDRVSTREAMINSTANCE)dwParam1, (PACMDRVSTREAMHEADER)dwParam2);
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;
1007 default:
1008 return DefDriverProc(dwDevID, hDriv, wMsg, dwParam1, dwParam2);
1010 return 0;