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