Fixed bug 3894 - Fuzzing crashes for SDL_LoadWAV Simon Hug I had a look at this and made some additions to SDL_wave.c. The attached patch adds many checks and error messages. For some reason I also added A-law and ?-law decoders. Forgot exactly why... but hey, they're small. The WAVE format is seriously underspecified (at least by the documents that are publicly available on the internet) and it's a shame Microsoft never put something better out there. The language used in them is so loose at times, it's not surprising the encoders and decoders behave very differently. The Windows Media Player doesn't even support MS ADPCM correctly. The patch also adds some hints to make the decoder more strict at the cost of compatibility with weird WAVE files. I still think it needs a bit of cleaning up (Not happy with the MultiplySize function. Don't like the name and other SDL code may want to use something like this too.) and some duplicated code may be folded together. It does work in this state and I have thrown all kinds of WAVE files at it. The AFL files also pass with it and some even play (obviously just noise). Crafty little fuzzer. Any critique would be welcome. I have a fork of SDL with a audio-loadwav branch over here if someone wants to use the commenting feature of Bitbucket: https://bitbucket.org/ChliHug/SDL I also cobbled some Lua scripts together to create WAVE test files: https://bitbucket.org/ChliHug/gendat
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 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003
diff --git a/include/SDL_audio.h b/include/SDL_audio.h
index 5634e09..305c01a 100644
--- a/include/SDL_audio.h
+++ b/include/SDL_audio.h
@@ -420,23 +420,56 @@ extern DECLSPEC void SDLCALL SDL_PauseAudioDevice(SDL_AudioDeviceID dev,
/* @} *//* Pause audio functions */
/**
- * This function loads a WAVE from the data source, automatically freeing
- * that source if \c freesrc is non-zero. For example, to load a WAVE file,
- * you could do:
+ * \brief Load the audio data of a WAVE file into memory
+ *
+ * Loading a WAVE file requires \c src, \c spec, \c audio_buf and \c audio_len
+ * to be valid pointers. The entire data portion of the file is then loaded
+ * into memory and decoded if necessary.
+ *
+ * If \c freesrc is non-zero, the data source gets automatically closed and
+ * freed before the function returns.
+ *
+ * Supported are RIFF WAVE files with the formats PCM (8, 16, 24, and 32 bits),
+ * IEEE Float (32 bits), Microsoft ADPCM and IMA ADPCM (4 bits), and A-law and
+ * µ-law (8 bits). Other formats are currently unsupported and cause an error.
+ *
+ * If this function succeeds, the pointer returned by it is equal to \c spec
+ * and the pointer to the audio data allocated by the function is written to
+ * \c audio_buf and its length in bytes to \c audio_len. The \ref SDL_AudioSpec
+ * members \c freq, \c channels, and \c format are set to the values of the
+ * audio data in the buffer. The \c samples member is set to a sane default and
+ * all others are set to zero.
+ *
+ * It's necessary to use SDL_FreeWAV() to free the audio data returned in
+ * \c audio_buf when it is no longer used.
+ *
+ * Because of the underspecification of the Waveform format, there are many
+ * problematic files in the wild that cause issues with strict decoders. To
+ * provide compatibility with these files, this decoder is lenient in regards
+ * to the truncation of the file, the fact chunk, and the size of the RIFF
+ * chunk. The hints SDL_HINT_WAVE_RIFF_CHUNK_SIZE, SDL_HINT_WAVE_TRUNCATION,
+ * and SDL_HINT_WAVE_FACT_CHUNK can be used to tune the behavior of the
+ * loading process.
+ *
+ * Any file that is invalid (due to truncation, corruption, or wrong values in
+ * the headers), too big, or unsupported causes an error. Additionally, any
+ * critical I/O error from the data source will terminate the loading process
+ * with an error. The function returns NULL on error and in all cases (with the
+ * exception of \c src being NULL), an appropriate error message will be set.
+ *
+ * It is required that the data source supports seeking.
+ *
+ * Example:
* \code
* SDL_LoadWAV_RW(SDL_RWFromFile("sample.wav", "rb"), 1, ...);
* \endcode
*
- * If this function succeeds, it returns the given SDL_AudioSpec,
- * filled with the audio data format of the wave data, and sets
- * \c *audio_buf to a malloc()'d buffer containing the audio data,
- * and sets \c *audio_len to the length of that audio buffer, in bytes.
- * You need to free the audio buffer with SDL_FreeWAV() when you are
- * done with it.
- *
- * This function returns NULL and sets the SDL error message if the
- * wave file cannot be opened, uses an unknown data format, or is
- * corrupt. Currently raw and MS-ADPCM WAVE files are supported.
+ * \param src The data source with the WAVE data
+ * \param freesrc A integer value that makes the function close the data source if non-zero
+ * \param spec A pointer filled with the audio format of the audio data
+ * \param audio_buf A pointer filled with the audio data allocated by the function
+ * \param audio_len A pointer filled with the length of the audio data buffer in bytes
+ * \return NULL on error, or non-NULL on success.
*/
extern DECLSPEC SDL_AudioSpec *SDLCALL SDL_LoadWAV_RW(SDL_RWops * src,
int freesrc,
diff --git a/include/SDL_hints.h b/include/SDL_hints.h
index e2bd691..4c83dd3 100644
--- a/include/SDL_hints.h
+++ b/include/SDL_hints.h
@@ -1122,6 +1122,70 @@ extern "C" {
/**
+ * \brief Controls how the size of the RIFF chunk affects the loading of a WAVE file.
+ *
+ * The size of the RIFF chunk (which includes all the sub-chunks of the WAVE
+ * file) is not always reliable. In case the size is wrong, it's possible to
+ * just ignore it and step through the chunks until a fixed limit is reached.
+ *
+ * Note that files that have trailing data unrelated to the WAVE file or
+ * corrupt files may slow down the loading process without a reliable boundary.
+ * By default, SDL stops after 10000 chunks to prevent wasting time. Use the
+ * environment variable SDL_WAVE_CHUNK_LIMIT to adjust this value.
+ *
+ * This variable can be set to the following values:
+ *
+ * "chunksearch" - Use the RIFF chunk size as a boundary for the chunk search
+ * "ignorezero" - Like "chunksearch", but a zero size searches up to 4 GiB (default)
+ * "ignore" - Ignore the RIFF chunk size and always search up to 4 GiB
+ * "maximum" - Search for chunks until the end of file (not recommended)
+ */
+#define SDL_HINT_WAVE_RIFF_CHUNK_SIZE "SDL_WAVE_RIFF_CHUNK_SIZE"
+
+/**
+ * \brief Controls how a truncated WAVE file is handled.
+ *
+ * A WAVE file is considered truncated if any of the chunks are incomplete or
+ * the data chunk size is not a multiple of the block size. By default, SDL
+ * decodes until the first incomplete block, as most applications seem to do.
+ *
+ * This variable can be set to the following values:
+ *
+ * "verystrict" - Raise an error if the file is truncated
+ * "strict" - Like "verystrict", but the size of the RIFF chunk is ignored
+ * "dropframe" - Decode until the first incomplete sample frame
+ * "dropblock" - Decode until the first incomplete block (default)
+ */
+#define SDL_HINT_WAVE_TRUNCATION "SDL_WAVE_TRUNCATION"
+
+/**
+ * \brief Controls how the fact chunk affects the loading of a WAVE file.
+ *
+ * The fact chunk stores information about the number of samples of a WAVE
+ * file. The Standards Update from Microsoft notes that this value can be used
+ * to 'determine the length of the data in seconds'. This is especially useful
+ * for compressed formats (for which this is a mandatory chunk) if they produce
+ * multiple sample frames per block and truncating the block is not allowed.
+ * The fact chunk can exactly specify how many sample frames there should be
+ * in this case.
+ *
+ * Unfortunately, most application seem to ignore the fact chunk and so SDL
+ * ignores it by default as well.
+ *
+ * This variable can be set to the following values:
+ *
+ * "truncate" - Use the number of samples to truncate the wave data if
+ * the fact chunk is present and valid
+ * "strict" - Like "truncate", but raise an error if the fact chunk
+ * is invalid, not present for non-PCM formats, or if the
+ * data chunk doesn't have that many samples
+ * "ignorezero" - Like "truncate", but ignore fact chunk if the number of
+ * samples is zero
+ * "ignore" - Ignore fact chunk entirely (default)
+ */
+#define SDL_HINT_WAVE_FACT_CHUNK "SDL_WAVE_FACT_CHUNK"
+
+/**
* \brief An enumeration of hint priorities
*/
typedef enum
diff --git a/src/audio/SDL_wave.c b/src/audio/SDL_wave.c
index 170a9c9..5c3b964 100644
--- a/src/audio/SDL_wave.c
+++ b/src/audio/SDL_wave.c
@@ -20,680 +20,2112 @@
*/
#include "../SDL_internal.h"
+#ifdef HAVE_LIMITS_H
+#include <limits.h>
+#else
+#ifndef SIZE_MAX
+#define SIZE_MAX ((size_t)-1)
+#endif
+#ifndef INT_MAX
+/* Make a lucky guess. */
+#define INT_MAX (SDL_MAX_SINT32)
+#endif
+#endif
+
/* Microsoft WAVE file loading routines */
+#include "SDL_log.h"
+#include "SDL_hints.h"
#include "SDL_audio.h"
#include "SDL_wave.h"
+/* Reads the value stored at the location of the f1 pointer, multiplies it
+ * with the second argument, and then stores it back to f1 again.
+ * Returns SDL_TRUE if the multiplication overflows, f1 does not get modified.
+ */
+static SDL_bool
+MultiplySize(size_t *f1, size_t f2)
+{
+ if (*f1 > 0 && SIZE_MAX / *f1 <= f2) {
+ return SDL_TRUE;
+ }
+ *f1 *= f2;
+ return SDL_FALSE;
+}
+
+typedef struct ADPCM_DecoderState
+{
+ Uint32 channels; /* Number of channels. */
+ size_t blocksize; /* Size of an ADPCM block in bytes. */
+ size_t blockheadersize; /* Size of an ADPCM block header in bytes. */
+ size_t samplesperblock; /* Number of samples per channel in an ADPCM block. */
+ size_t framesize; /* Size of a sample frame (16-bit PCM) in bytes. */
+ Sint64 framestotal; /* Total number of sample frames. */
+ Sint64 framesleft; /* Number of sample frames still to be decoded. */
+ void *ddata; /* Decoder data from initialization. */
+ void *cstate; /* Decoding state for each channel. */
+
+ /* ADPCM data. */
+ struct {
+ Uint8 *data;
+ size_t size;
+ size_t pos;
+ } input;
+
+ /* Current ADPCM block in the ADPCM data above. */
+ struct {
+ Uint8 *data;
+ size_t size;
+ size_t pos;
+ } block;
+
+ /* Decoded 16-bit PCM data. */
+ struct {
+ Sint16 *data;
+ size_t size;
+ size_t pos;
+ } output;
+} ADPCM_DecoderState;
+
+typedef struct MS_ADPCM_CoeffData
+{
+ Uint16 coeffcount;
+ Sint16 *coeff;
+ Sint16 aligndummy; /* Has to be last member. */
+} MS_ADPCM_CoeffData;
+
+typedef struct MS_ADPCM_ChannelState
+{
+ Uint16 delta;
+ Sint16 coeff1;
+ Sint16 coeff2;
+} MS_ADPCM_ChannelState;
+
+#ifdef SDL_WAVE_DEBUG_LOG_FORMAT
+static void
+WaveDebugLogFormat(WaveFile *file)
+{
+ WaveFormat *format = &file->format;
+ const char *fmtstr = "WAVE file: %s, %u Hz, %s, %u bits, %u %s/s";
+ const char *waveformat, *wavechannel, *wavebpsunit = "B";
+ Uint32 wavebps = format->byterate;
+ char channelstr[64] = {0};
+
+ switch (format->encoding) {
+ case PCM_CODE:
+ waveformat = "PCM";
+ break;
+ case IEEE_FLOAT_CODE:
+ waveformat = "IEEE Float";
+ break;
+ case ALAW_CODE:
+ waveformat = "A-law";
+ break;
+ case MULAW_CODE:
+ waveformat = "\xc2\xb5-law";
+ break;
+ case MS_ADPCM_CODE:
+ waveformat = "MS ADPCM";
+ break;
+ case IMA_ADPCM_CODE:
+ waveformat = "IMA ADPCM";
+ break;
+ default:
+ waveformat = "Unknown";
+ break;
+ }
+
+#define SDL_WAVE_DEBUG_CHANNELCFG(STR, CODE) case CODE: wavechannel = STR; break;
+#define SDL_WAVE_DEBUG_CHANNELSTR(STR, CODE) if (format->channelmask & CODE) { \
+ SDL_strlcat(channelstr, channelstr[0] ? "-" STR : STR, sizeof(channelstr));}
+
+ if (format->formattag == EXTENSIBLE_CODE && format->channelmask > 0) {
+ switch (format->channelmask) {
+ SDL_WAVE_DEBUG_CHANNELCFG("1.0 Mono", 0x4)
+ SDL_WAVE_DEBUG_CHANNELCFG("1.1 Mono", 0xc)
+ SDL_WAVE_DEBUG_CHANNELCFG("2.0 Stereo", 0x3)
+ SDL_WAVE_DEBUG_CHANNELCFG("2.1 Stereo", 0xb)
+ SDL_WAVE_DEBUG_CHANNELCFG("3.0 Stereo", 0x7)
+ SDL_WAVE_DEBUG_CHANNELCFG("3.1 Stereo", 0xf)
+ SDL_WAVE_DEBUG_CHANNELCFG("3.0 Surround", 0x103)
+ SDL_WAVE_DEBUG_CHANNELCFG("3.1 Surround", 0x10b)
+ SDL_WAVE_DEBUG_CHANNELCFG("4.0 Quad", 0x33)
+ SDL_WAVE_DEBUG_CHANNELCFG("4.1 Quad", 0x3b)
+ SDL_WAVE_DEBUG_CHANNELCFG("4.0 Surround", 0x107)
+ SDL_WAVE_DEBUG_CHANNELCFG("4.1 Surround", 0x10f)
+ SDL_WAVE_DEBUG_CHANNELCFG("5.0", 0x37)
+ SDL_WAVE_DEBUG_CHANNELCFG("5.1", 0x3f)
+ SDL_WAVE_DEBUG_CHANNELCFG("5.0 Side", 0x607)
+ SDL_WAVE_DEBUG_CHANNELCFG("5.1 Side", 0x60f)
+ SDL_WAVE_DEBUG_CHANNELCFG("6.0", 0x137)
+ SDL_WAVE_DEBUG_CHANNELCFG("6.1", 0x13f)
+ SDL_WAVE_DEBUG_CHANNELCFG("6.0 Side", 0x707)
+ SDL_WAVE_DEBUG_CHANNELCFG("6.1 Side", 0x70f)
+ SDL_WAVE_DEBUG_CHANNELCFG("7.0", 0xf7)
+ SDL_WAVE_DEBUG_CHANNELCFG("7.1", 0xff)
+ SDL_WAVE_DEBUG_CHANNELCFG("7.0 Side", 0x6c7)
+ SDL_WAVE_DEBUG_CHANNELCFG("7.1 Side", 0x6cf)
+ SDL_WAVE_DEBUG_CHANNELCFG("7.0 Surround", 0x637)
+ SDL_WAVE_DEBUG_CHANNELCFG("7.1 Surround", 0x63f)
+ SDL_WAVE_DEBUG_CHANNELCFG("9.0 Surround", 0x5637)
+ SDL_WAVE_DEBUG_CHANNELCFG("9.1 Surround", 0x563f)
+ SDL_WAVE_DEBUG_CHANNELCFG("11.0 Surround", 0x56f7)
+ SDL_WAVE_DEBUG_CHANNELCFG("11.1 Surround", 0x56ff)
+ default:
+ SDL_WAVE_DEBUG_CHANNELSTR("FL", 0x1)
+ SDL_WAVE_DEBUG_CHANNELSTR("FR", 0x2)
+ SDL_WAVE_DEBUG_CHANNELSTR("FC", 0x4)
+ SDL_WAVE_DEBUG_CHANNELSTR("LF", 0x8)
+ SDL_WAVE_DEBUG_CHANNELSTR("BL", 0x10)
+ SDL_WAVE_DEBUG_CHANNELSTR("BR", 0x20)
+ SDL_WAVE_DEBUG_CHANNELSTR("FLC", 0x40)
+ SDL_WAVE_DEBUG_CHANNELSTR("FRC", 0x80)
+ SDL_WAVE_DEBUG_CHANNELSTR("BC", 0x100)
+ SDL_WAVE_DEBUG_CHANNELSTR("SL", 0x200)
+ SDL_WAVE_DEBUG_CHANNELSTR("SR", 0x400)
+ SDL_WAVE_DEBUG_CHANNELSTR("TC", 0x800)
+ SDL_WAVE_DEBUG_CHANNELSTR("TFL", 0x1000)
+ SDL_WAVE_DEBUG_CHANNELSTR("TFC", 0x2000)
+ SDL_WAVE_DEBUG_CHANNELSTR("TFR", 0x4000)
+ SDL_WAVE_DEBUG_CHANNELSTR("TBL", 0x8000)
+ SDL_WAVE_DEBUG_CHANNELSTR("TBC", 0x10000)
+ SDL_WAVE_DEBUG_CHANNELSTR("TBR", 0x20000)
+ break;
+ }
+ } else {
+ switch (format->channels) {
+ default:
+ if (SDL_snprintf(channelstr, sizeof(channelstr), "%u channels", format->channels) >= 0) {
+ wavechannel = channelstr;
+ break;
+ }
+ case 0:
+ wavechannel = "Unknown";
+ break;
+ case 1:
+ wavechannel = "Mono";
+ break;
+ case 2:
+ wavechannel = "Setero";
+ break;
+ }
+ }
+
+#undef SDL_WAVE_DEBUG_CHANNELCFG
+#undef SDL_WAVE_DEBUG_CHANNELSTR
+
+ if (wavebps >= 1024) {
+ wavebpsunit = "KiB";
+ wavebps = wavebps / 1024 + (wavebps & 0x3ff ? 1 : 0);
+ }
+
+ SDL_LogDebug(SDL_LOG_CATEGORY_AUDIO, fmtstr, waveformat, format->frequency, wavechannel, format->bitspersample, wavebps, wavebpsunit);
+}
+#endif
+
+#ifdef SDL_WAVE_DEBUG_DUMP_FORMAT
+static void
+WaveDebugDumpFormat(WaveFile *file, Uint32 rifflen, Uint32 fmtlen, Uint32 datalen)
+{
+ WaveFormat *format = &file->format;
+ const char *fmtstr1 = "WAVE chunk dump:\n"
+ "-------------------------------------------\n"
+ "RIFF %11u\n"
+ "-------------------------------------------\n"
+ " fmt %11u\n"
+ " wFormatTag 0x%04x\n"
+ " nChannels %11u\n"
+ " nSamplesPerSec %11u\n"
+ " nAvgBytesPerSec %11u\n"
+ " nBlockAlign %11u\n";
+ const char *fmtstr2 = " wBitsPerSample %11u\n";
+ const char *fmtstr3 = " cbSize %11u\n";
+ const char *fmtstr4a = " wValidBitsPerSample %11u\n";
+ const char *fmtstr4b = " wSamplesPerBlock %11u\n";
+ const char *fmtstr5 = " dwChannelMask 0x%08x\n"
+ " SubFormat\n"
+ " %08x-%04x-%04x-%02x%02x%02x%02x%02x%02x%02x%02x\n";
+ const char *fmtstr6 = "-------------------------------------------\n"
+ " fact\n"
+ " dwSampleLength %11u\n";
+ const char *fmtstr7 = "-------------------------------------------\n"
+ " data %11u\n"
+ "-------------------------------------------\n";
+ char *dumpstr;
+ size_t dumppos = 0;
+ const size_t bufsize = 1024;
+ int res;
+
+ dumpstr = SDL_malloc(bufsize);
+ if (dumpstr == NULL) {
+ return;
+ }
+ dumpstr[0] = 0;
+
+ res = SDL_snprintf(dumpstr, bufsize, fmtstr1, rifflen, fmtlen, format->formattag, format->channels, format->frequency, format->byterate, format->blockalign);
+ dumppos += res > 0 ? res : 0;
+ if (fmtlen >= 16) {
+ res = SDL_snprintf(dumpstr + dumppos, bufsize - dumppos, fmtstr2, format->bitspersample);
+ dumppos += res > 0 ? res : 0;
+ }
+ if (fmtlen >= 18) {
+ res = SDL_snprintf(dumpstr + dumppos, bufsize - dumppos, fmtstr3, format->extsize);
+ dumppos += res > 0 ? res : 0;
+ }
+ if (format->formattag == EXTENSIBLE_CODE && fmtlen >= 40 && format->extsize >= 22) {
+ const Uint8 *g = format->subformat;
+ const Uint32 g1 = g[0] | ((Uint32)g[1] << 8) | ((Uint32)g[2] << 16) | ((Uint32)g[3] << 24);
+ const Uint32 g2 = g[4] | ((Uint32)g[5] << 8);
+ const Uint32 g3 = g[6] | ((Uint32)g[7] << 8);
+
+ switch (format->encoding) {
+ default:
+ res = SDL_snprintf(dumpstr + dumppos, bufsize - dumppos, fmtstr4a, format->validsamplebits);
+ dumppos += res > 0 ? res : 0;
+ break;
+ case MS_ADPCM_CODE:
+ case IMA_ADPCM_CODE:
+ res = SDL_snprintf(dumpstr + dumppos, bufsize - dumppos, fmtstr4b, format->samplesperblock);
+ dumppos += res > 0 ? res : 0;
+ break;
+ }
+ res = SDL_snprintf(dumpstr + dumppos, bufsize - dumppos, fmtstr5, format->channelmask, g1, g2, g3, g[8], g[9], g[10], g[11], g[12], g[13], g[14], g[15]);
+ dumppos += res > 0 ? res : 0;
+ } else {
+ switch (format->encoding) {
+ case MS_ADPCM_CODE:
+ case IMA_ADPCM_CODE:
+ if (fmtlen >= 20 && format->extsize >= 2) {
+ res = SDL_snprintf(dumpstr + dumppos, bufsize - dumppos, fmtstr4b, format->samplesperblock);
+ dumppos += res > 0 ? res : 0;
+ }
+ break;
+ }
+ }
+ if (file->fact.status >= 1) {
+ res = SDL_snprintf(dumpstr + dumppos, bufsize - dumppos, fmtstr6, file->fact.samplelength);
+ dumppos += res > 0 ? res : 0;
+ }
+ res = SDL_snprintf(dumpstr + dumppos, bufsize - dumppos, fmtstr7, datalen);
+ dumppos += res > 0 ? res : 0;
+
+ SDL_LogDebug(SDL_LOG_CATEGORY_AUDIO, "%s", dumpstr);
+
+ free(dumpstr);
+}
+#endif
+
+static Sint64
+WaveAdjustToFactValue(WaveFile *file, Sint64 sampleframes)
+{
+ if (file->fact.status == 2) {
+ if (file->facthint == FactStrict && sampleframes < file->fact.samplelength) {
+ return SDL_SetError("Invalid number of sample frames in WAVE fact chunk (too many)");
+ } else if (sampleframes > file->fact.samplelength) {
+ return file->fact.samplelength;
+ }
+ }
+
+ return sampleframes;
+}
+
+static int
+MS_ADPCM_CalculateSampleFrames(WaveFile *file, size_t datalength)
+{
+ WaveFormat *format = &file->format;
+ const size_t blockheadersize = file->format.channels * 7;
+ const size_t availableblocks = datalength / file->format.blockalign;
+ const size_t blockframebitsize = file->format.bitspersample * file->format.channels;
+ const size_t trailingdata = datalength % file->format.blockalign;
+
+ if (file->trunchint == TruncVeryStrict || file->trunchint == TruncStrict) {
+ /* The size of the data chunk must be a multiple of the block size. */
+ if (datalength < blockheadersize || trailingdata > 0) {
+ return SDL_SetError("Truncated MS ADPCM block");
+ }
+ }
+
+ /* Calculate number of sample frames that will be decoded. */
+ file->sampleframes = (Sint64)availableblocks * format->samplesperblock;
+ if (trailingdata > 0) {
+ /* The last block is truncated. Check if we can get any samples out of it. */
+ if (file->trunchint == TruncDropFrame) {
+ /* Drop incomplete sample frame. */
+ if (trailingdata >= blockheadersize) {
+ size_t trailingsamples = 2 + (trailingdata - blockheadersize) * 8 / blockframebitsize;
+ if (trailingsamples > format->samplesperblock) {
+ trailingsamples = format->samplesperblock;
+ }
+ file->sampleframes += trailingsamples;
+ }
+ }
+ }
+
+ file->sampleframes = WaveAdjustToFactValue(file, file->sampleframes);
+ if (file->sampleframes < 0) {
+ return -1;
+ }
+
+ return 0;
+}
+
+static int
+MS_ADPCM_Init(WaveFile *file, size_t datalength)
+{
+ WaveFormat *format = &file->format;
+ WaveChunk *chunk = &file->chunk;
+ const size_t blockheadersize = format->channels * 7;
+ const size_t blockdatasize = (size_t)format->blockalign - blockheadersize;
+ const size_t blockframebitsize = format->bitspersample * format->channels;
+ const size_t blockdatasamples = (blockdatasize * 8) / blockframebitsize;
+ const Sint16 presetcoeffs[14] = {256, 0, 512, -256, 0, 0, 192, 64, 240, 0, 460, -208, 392, -232};
+ size_t i, coeffcount;
+ MS_ADPCM_CoeffData *coeffdata;
+
+ /* Sanity checks. */
+
+ /* While it's clear how IMA ADPCM handles more than two channels, the nibble
+ * order of MS ADPCM makes it awkward. The Standards Update does not talk
+ * about supporting more than stereo anyway.
+ */
+ if (format->channels > 2) {
+ return SDL_SetError("Invalid number of channels");
+ }
+
+ if (format->bitspersample != 4) {
+ return SDL_SetError("Invalid MS ADPCM bits per sample of %d", (int)format->bitspersample);
+ }
+
+ /* The block size must be big enough to contain the block header. */
+ if (format->blockalign < blockheadersize) {
+ return SDL_SetError("Invalid MS ADPCM block size (nBlockAlign)");
+ }
+
+ if (format->formattag == EXTENSIBLE_CODE) {
+ /* Does have a GUID (like all format tags), but there's no specification
+ * for how the data is packed into the extensible header. Making
+ * assumptions here could lead to new formats nobody wants to support.
+ */
+ return SDL_SetError("MS ADPCM with the extensible header is not supported");
+ }
+
+ /* There are wSamplesPerBlock, wNumCoef, and at least 7 coefficient pairs in
+ * the extended part of the header.
+ */
+ if (chunk->size < 22) {
+ return SDL_SetError("Could not read MS ADPCM format header");
+ }
+
+ format->samplesperblock = chunk->data[18] | ((Uint16)chunk->data[19] << 8);
+ /* Number of coefficient pairs. A pair has two 16-bit integers. */
+ coeffcount = chunk->data[20] | ((size_t)chunk->data[21] << 8);
+ /* bPredictor, the integer offset into the coefficients array, is only
+ * 8 bits. It can only address the first 256 coefficients. Let's limit
+ * the count number here.
+ */
+ if (coeffcount > 256) {
+ coeffcount = 256;
+ }
+
+ if (chunk->size < 22 + coeffcount * 4) {
+ return SDL_SetError("Could not read custom coefficients in MS ADPCM format header");
+ } else if (format->extsize < 4 + coeffcount * 4) {
+ return SDL_SetError("Invalid MS ADPCM format header (too small)");
+ } else if (coeffcount < 7) {
+ return SDL_SetError("Missing required coefficients in MS ADPCM format header");
+ }
+
+ coeffdata = (MS_ADPCM_CoeffData *)SDL_malloc(sizeof(MS_ADPCM_CoeffData) + coeffcount * 4);
+ file->decoderdata = coeffdata; /* Freed in cleanup. */
+ if (coeffdata == NULL) {
+ return SDL_OutOfMemory();
+ }
+ coeffdata->coeff = &coeffdata->aligndummy;
+ coeffdata->coeffcount = (Uint16)coeffcount;
+
+ /* Copy the 16-bit pairs. */
+ for (i = 0; i < coeffcount * 2; i++) {
+ Sint32 c = chunk->data[22 + i * 2] | ((Sint32)chunk->data[23 + i * 2] << 8);
+ if (c >= 0x8000) {
+ c -= 0x10000;
+ }
+ if (i < 14 && c != presetcoeffs[i]) {
+ return SDL_SetError("Wrong preset coefficients in MS ADPCM format header");
+ }
+ coeffdata->coeff[i] = (Sint16)c;
+ }
+
+ /* Technically, wSamplesPerBlock is required, but we have all the
+ * information in the other fields to calculate it, if it's zero.
+ */
+ if (format->samplesperblock == 0) {
+ /* Let's be nice to the encoders that didn't know how to fill this.
+ * The Standards Update calculates it this way:
+ *
+ * x = Block size (in bits) minus header size (in bits)
+ * y = Bit depth multiplied by channel count
+ * z = Number of samples per channel in block header
+ * wSamplesPerBlock = x / y + z
+ */
+ format->samplesperblock = (Uint32)blockdatasamples + 2;
+ }
+
+ /* nBlockAlign can be in conflict with wSamplesPerBlock. For example, if
+ * the number of samples doesn't fit into the block. The Standards Update
+ * also describes wSamplesPerBlock with a formula that makes it necessary to
+ * always fill the block with the maximum amount of samples, but this is not
+ * enforced here as there are no compatibility issues.
+ * A truncated block header with just one sample is not supported.
+ */
+ if (format->samplesperblock == 1 || blockdatasamples < format->samplesperblock - 2) {
+ return SDL_SetError("Invalid number of samples per MS ADPCM block (wSamplesPerBlock)");
+ }
+
+ if (MS_ADPCM_CalculateSampleFrames(file, datalength) < 0) {
+ return -1;
+ }
+
+ return 0;
+}
+
+static Sint16
+MS_ADPCM_ProcessNibble(MS_ADPCM_ChannelState *cstate, Sint32 sample1, Sint32 sample2, Uint8 nybble)
+{
+ const Sint32 max_audioval = 32767;
+ const Sint32 min_audioval = -32768;
+ const Uint16 max_deltaval = 65535;
+ const Uint16 adaptive[] = {
+ 230, 230, 230, 230, 307, 409, 512, 614,
+ 768, 614, 512, 409, 307, 230, 230, 230
+ };
+ Sint32 new_sample;
+ Sint32 errordelta;
+ Uint32 delta = cstate->delta;
+
+ new_sample = (sample1 * cstate->coeff1 + sample2 * cstate->coeff2) / 256;
+ /* The nibble is a signed 4-bit error delta. */
+ errordelta = (Sint32)nybble - (nybble >= 0x08 ? 0x10 : 0);
+ new_sample += (Sint32)delta * errordelta;
+ if (new_sample < min_audioval) {
+ new_sample = min_audioval;
+ } else if (new_sample > max_audioval) {
+ new_sample = max_audioval;
+ }
+ delta = (delta * adaptive[nybble]) / 256;
+ if (delta < 16) {
+ delta = 16;
+ } else if (delta > max_deltaval) {
+ /* This issue is not described in the Standards Update and therefore
+ * undefined. It seems sensible to prevent overflows with a limit.
+ */
+ delta = max_deltaval;
+ }
+
+ cstate->delta = (Uint16)delta;
+ return (Sint16)new_sample;
+}
+
+static int
+MS_ADPCM_DecodeBlockHeader(ADPCM_DecoderState *state)
+{
+ Uint8 coeffindex;
+ const Uint32 channels = state->channels;
+ Sint32 sample;
+ Uint32 c;
+ MS_ADPCM_ChannelState *cstate = (MS_ADPCM_ChannelState *)state->cstate;
+ MS_ADPCM_CoeffData *ddata = (MS_ADPCM_CoeffData *)state->ddata;
+
+ for (c = 0; c < channels; c++) {
+ size_t o = c;
+
+ /* Load the coefficient pair into the channel state. */
+ coeffindex = state->block.data[o];
+ if (coeffindex > ddata->coeffcount) {
+ return SDL_SetError("Invalid MS ADPCM coefficient index in block header");
+ }
+ cstate[c].coeff1 = ddata->coeff[coeffindex * 2];
+ cstate[c].coeff2 = ddata->coeff[coeffindex * 2 + 1];
+
+ /* Initial delta value. */
+ o = channels + c * 2;
+ cstate[c].delta = state->block.data[o] | ((Uint16)state->block.data[o + 1] << 8);
+
+ /* Load the samples from the header. Interestingly, the sample later in
+ * the output stream comes first.
+ */
+ o = channels * 3 + c * 2;
+ sample = state->block.data[o] | ((Sint32)state->block.data[o + 1] << 8);
+ if (sample >= 0x8000) {
+ sample -= 0x10000;
+ }
+ state->output.data[state->output.pos + channels] = (Sint16)sample;
+
+ o = channels * 5 + c * 2;
+ sample = state->block.data[o] | ((Sint32)state->block.data[o + 1] << 8);
+ if (sample >= 0x8000) {
+ sample -= 0x10000;
+ }
+ state->output.data[state->output.pos] = (Sint16)sample;
+
+ state->output.pos++;
+ }
+
+ state->block.pos += state->blockheadersize;
+
+ /* Skip second sample frame that came from the header. */
+ state->output.pos += state->channels;
+
+ /* Header provided two sample frames. */
+ state->framesleft -= 2;
+
+ return 0;
+}
+
+/* Decodes the data of the MS ADPCM block. Decoding will stop if a block is too
+ * short, returning with none or partially decoded data. The partial data
+ * will always contain full sample frames (same sample count for each channel).
+ * Incomplete sample frames are discarded.
+ */
+static int
+MS_ADPCM_DecodeBlockData(ADPCM_DecoderState *state)
+{
+ Uint16 nybble = 0;
+ Sint16 sample1, sample2;
+ const Uint32 channels = state->channels;
+ Uint32 c;
+ MS_ADPCM_ChannelState *cstate = (MS_ADPCM_ChannelState *)state->cstate;
+
+ size_t blockpos = state->block.pos;
+ size_t blocksize = state->block.size;
+
+ size_t outpos = state->output.pos;
+
+ Sint64 blockframesleft = state->samplesperblock - 2;
+ if (blockframesleft > state->framesleft) {
+ blockframesleft = state->framesleft;
+ }
+
+ while (blockframesleft > 0) {
+ for (c = 0; c < channels; c++) {
+ if (nybble & 0x8000) {
+ nybble <<= 4;
+ } else if (blockpos < blocksize) {
+ nybble = state->block.data[blockpos++] | 0x8000;
+ } else {
+ /* Out of input data. Drop the incomplete frame and return. */
+ state->output.pos = outpos - c;
+ return -1;
+ }
+
+ /* Load previous samples which may come from the block header. */
+ sample1 = state->output.data[outpos - channels];
+ sample2 = state->output.data[outpos - channels * 2];
+
+ sample1 = MS_ADPCM_ProcessNibble(cstate + c, sample1, sample2, (nybble >> 4) & 0x0f);
+ state->output.data[outpos++] = sample1;
+ }
+
+ state->framesleft--;
+ blockframesleft--;
+ }
+
+ state->output.pos = outpos;
+
+ return 0;
+}
+
+static int
+MS_ADPCM_Decode(WaveFile *file, Uint8 **audio_buf, Uint32 *audio_len)
+{
+ int result;
+ size_t bytesleft, outputsize;
+ WaveChunk *chunk = &file->chunk;
+ ADPCM_DecoderState state = {0};
+ MS_ADPCM_ChannelState cstate[2] = {0};
+
+ if (chunk->size != chunk->length) {
+ /* Could not read everything. Recalculate number of sample frames. */
+ if (MS_ADPCM_CalculateSampleFrames(file, chunk->size) < 0) {
+ return -1;
+ }
+ }
+
+ /* Nothing to decode, nothing to return. */
+ if (file->sampleframes == 0) {
+ *audio_buf = NULL;
+ *audio_len = 0;
+ return 0;
+ }
+
+ state.blocksize = file->format.blockalign;
+ state.channels = file->format.channels;
+ state.blockheadersize = state.channels * 7;
+ state.samplesperblock = file->format.samplesperblock;
+ state.framesize = state.channels * sizeof(Sint16);
+ state.ddata = file->decoderdata;
+ state.framestotal = file->sampleframes;
+ state.framesleft = state.framestotal;
+
+ state.input.data = chunk->data;
+ state.input.size = chunk->size;
+ state.input.pos = 0;
+
+ /* The output size in bytes. May get modified if data is truncated. */
+ outputsize = (size_t)state.framestotal;
+ if (MultiplySize(&outputsize, state.framesize)) {
+ return SDL_OutOfMemory();
+ } else if (outputsize > SDL_MAX_UINT32 || state.framestotal > SIZE_MAX) {
+ return SDL_SetError("WAVE file too big");
+ }
+
+ state.output.pos = 0;
+ state.output.size = outputsize / sizeof(Sint16);
+ state.output.data = (Sint16 *)SDL_malloc(outputsize);
+ if (state.output.data == NULL) {
+ return SDL_OutOfMemory();
+ }
+
+ state.cstate = &cstate;
+
+ /* Decode block by block. A truncated block will stop the decoding. */
+ bytesleft = state.input.size - state.input.pos;
+ while (state.framesleft > 0 && bytesleft >= state.blockheadersize) {
+ state.block.data = state.input.data + state.input.pos;
+ state.block.size = bytesleft < state.blocksize ? bytesleft : state.blocksize;
+ state.block.pos = 0;
+
+ if (state.output.size - state.output.pos < (Uint64)state.framesleft * state.channels) {
+ /* Somehow didn't allocate enough space for the output. */
+ SDL_free(state.output.data);
+ return SDL_SetError("Unexpected overflow in MS ADPCM decoder");
+ }
+
+ /* Initialize decoder with the values from the block header. */
+ result = MS_ADPCM_DecodeBlockHeader(&state);
+ if (result == -1) {
+ SDL_free(state.output.data);
+ return -1;
+ }
+
+ /* Decode the block data. It stores the samples directly in the output. */
+ result = MS_ADPCM_DecodeBlockData(&state);
+ if (result == -1) {
+ /* Unexpected end. Stop decoding and return partial data if necessary. */
+ if (file->trunchint == TruncVeryStrict || file->trunchint == TruncVeryStrict) {
+ SDL_free(state.output.data);
+ return SDL_SetError("Truncated data chunk");
+ } else if (file->trunchint != TruncDropFrame) {
+ state.output.pos -= state.output.pos % (state.samplesperblock * state.channels);
+ }
+ outputsize = state.output.pos * sizeof(Sint16); /* Can't overflow, is always smaller. */
+ break;
+ }
+
+ state.input.pos += state.block.size;
+ bytesleft = state.input.size - state.input.pos;
+ }
+
+ *audio_buf = (Uint8 *)state.output.data;
+ *audio_len = (Uint32)outputsize;
+
+ return 0;
+}
+
+static int
+IMA_ADPCM_CalculateSampleFrames(WaveFile *file, size_t datalength)
+{
+ WaveFormat *format = &file->format;
+ const size_t blockheadersize = format->channels * 4;
+ const size_t subblockframesize = format->channels * 4;
+ const size_t availableblocks = datalength / format->blockalign;
+ const size_t trailingdata = datalength % format->blockalign;
+
+ if (file->trunchint == TruncVeryStrict || file->trunchint == TruncStrict) {
+ /* The size of the data chunk must be a multiple of the block size. */
+ if (datalength < blockheadersize || trailingdata > 0) {
+ return SDL_SetError("Truncated IMA ADPCM block");
+ }
+ }
+
+ /* Calculate number of sample frames that will be decoded. */
+ file->sampleframes = (Uint64)availableblocks * format->samplesperblock;
+ if (trailingdata > 0) {
+ /* The last block is truncated. Check if we can get any samples out of it. */
+ if (file->trunchint == TruncDropFrame && trailingdata > blockheadersize - 2) {
+ /* The sample frame in the header of the truncated block is present.
+ * Drop incomplete sample frames.
+ */
+ size_t trailingsamples = 1;
+
+ if (trailingdata > blockheadersize) {
+ /* More data following after the header. */
+ const size_t trailingblockdata = trailingdata - blockheadersize;
+ const size_t trailingsubblockdata = trailingblockdata % subblockframesize;
+ trailingsamples += (trailingblockdata / subblockframesize) * 8;
+ /* Due to the interleaved sub-blocks, the last 4 bytes determine
+ * how many samples of the truncated sub-block are lost.
+ */
+ if (trailingsubblockdata > subblockframesize - 4) {
+ trailingsamples += (trailingsubblockdata % 4) * 2;
+ }
+ }
+
+ if (trailingsamples > format->samplesperblock) {
+ trailingsamples = format->samplesperblock;
+ }
+ file->sampleframes += trailingsamples;
+ }
+ }
+
+ file->sampleframes = WaveAdjustToFactValue(file, file->sampleframes);
+ if (file->sampleframes < 0) {
+ return -1;
+ }
+
+ return 0;
+}
+
+static int
+IMA_ADPCM_Init(WaveFile *file, size_t datalength)
+{
+ WaveFormat *format = &file->format;
+ WaveChunk *chunk = &file->chunk;
+ const size_t blockheadersize = format->channels * 4;
+ const size_t blockdatasize = (size_t)format->blockalign - blockheadersize;
+ const size_t blockframebitsize = format->bitspersample * format->channels;
+ const size_t blockdatasamples = (blockdatasize * 8) / blockframebitsize;
+
+ /* Sanity checks. */
+
+ /* IMA ADPCAM can also have 3-bit samples, but it's not supported by SDL at this time. */
+ if (format->bitspersample == 3) {
+ return SDL_SetError("3-bit IMA ADPCM currently not supported");
+ } else if (format->bitspersample != 4) {
+ return SDL_SetError("Invalid IMA ADPCM bits per sample of %d", (int)format->bitspersample);
+ }
+
+ /* The block size is required to be a multiple of 4 and it must be able to
+ * hold a block header.
+ */
+ if (format->blockalign < blockheadersize || format->blockalign % 4) {
+ return SDL_SetError("Invalid IMA ADPCM block size (nBlockAlign)");
+ }
+
+ if (format->formattag == EXTENSIBLE_CODE) {
+ /* There's no specification for this, but it's basically the same
+ * format because the extensible header has wSampePerBlocks too.
+ */
+ } else {
+ /* The Standards Update says there 'should' be 2 bytes for wSamplesPerBlock. */
+ if (chunk->size >= 20 && format->extsize >= 2) {
+ format->samplesperblock = chunk->data[18] | ((Uint16)chunk->data[19] << 8);
+ }
+ }
+
+ if (format->samplesperblock == 0) {
+ /* Field zero? No problem. We just assume the encoder packed the block.
+ * The specification calculates it this way:
+ *
+ * x = Block size (in bits) minus header size (in bits)
+ * y = Bit depth multiplied by channel count
+ * z = Number of samples per channel in header
+ * wSamplesPerBlock = x / y + z
+ */
+ format->samplesperblock = (Uint32)blockdatasamples + 1;
+ }
+
+ /* nBlockAlign can be in conflict with wSamplesPerBlock. For example, if
+ * the number of samples doesn't fit into the block. The Standards Update
+ * also describes wSamplesPerBlock with a formula that makes it necessary
+ * to always fill the block with the maximum amount of samples, but this is
+ * not enforced here as there are no compatibility issues.
+ */
+ if (blockdatasamples < format->samplesperblock - 1) {
+ return SDL_SetError("Invalid number of samples per IMA ADPCM block (wSamplesPerBlock)");
+ }
+
+ if (IMA_ADPCM_CalculateSampleFrames(file, datalength) < 0) {
+ return -1;
+ }
+
+ return 0;
+}
+
+static Sint16
+IMA_ADPCM_ProcessNibble(Sint8 *cindex, Sint16 lastsample, Uint8 nybble)
+{
+ const Sint32 max_audioval = 32767;
+ const Sint32 min_audioval = -32768;
+ const Sint8 index_table_4b[16] = {
+ -1, -1, -1, -1,
+ 2, 4, 6, 8,
+ -1, -1, -1, -1,
+ 2, 4, 6, 8
+ };
+ const Uint16 step_table[89] = {
+ 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31,
+ 34, 37, 41, 45, 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 130,
+ 143, 157, 173, 190, 209, 230, 253, 279, 307, 337, 371, 408,
+ 449, 494, 544, 598, 658, 724, 796, 876, 963, 1060, 1166, 1282,
+ 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327,
+ 3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630,
+ 9493, 10442, 11487, 12635, 13899, 15289, 16818, 18500, 20350,
+ 22385, 24623, 27086, 29794, 32767
+ };
+ Uint32 step;
+ Sint32 sample, delta;
+ Sint8 index = *cindex;
+
+ /* Clamp index into valid range. */
+ if (index > 88) {
+ index = 88;
+ } else if (index < 0) {
+ index = 0;
+ }
+
+ /* explicit cast to avoid gcc warning about using 'char' as array index */
+ step = step_table[(size_t)index];
+
+ /* Update index value */
+ *cindex = index + index_table_4b[nybble];
+
+ /* This calculation uses shifts and additions because multiplications were
+ * much slower back then. Sadly, this can't just be replaced with an actual
+ * multiplication now as the old algorithm drops some bits. The closest
+ * approximation I could find is something like this:
+ * (nybble & 0x8 ? -1 : 1) * ((nybble & 0x7) * step / 4 + step / 8)
+ */
+ delta = step >> 3;
+ if (nybble & 0x04)
+ delta += step;
+ if (nybble & 0x02)
+ delta += step >> 1;
+ if (nybble & 0x01)
+ delta += step >> 2;
+ if (nybble & 0x08)
+ delta = -delta;
+
+ sample = lastsample + delta;
+
+ /* Clamp output sample */
+ if (sample > max_audioval) {
+ sample = max_audioval;
+ } else if (sample < min_audioval) {
+ sample = min_audioval;
+ }
+
+ return (Sint16)sample;
+}
+
+static int
+IMA_ADPCM_DecodeBlockHeader(ADPCM_DecoderState *state)
+{
+ Sint16 step;
+ Uint32 c;
+ Uint8 *cstate = state->cstate;
+
+ for (c = 0; c < state->channels; c++) {
+ size_t o = state->block.pos + c * 4;
+
+ /* Extract the sample from the header. */
+ Sint32 sample = state->block.data[o] | ((Sint32)state->block.data[o + 1] << 8);
+ if (sample >= 0x8000) {
+ sample -= 0x10000;
+ }
+ state->output.data[state->output.pos++] = (Sint16)sample;
+
+ /* Channel step index. */
+ step = (Sint16)state->block.data[o + 2];
+ cstate[c] = (Sint8)(step > 0x80 ? step - 0x100 : step);
+
+ /* Reserved byte in block header, should be 0. */
+ if (state->block.data[o + 3] != 0) {
+ /* Uh oh, corrupt data? Buggy code? */ ;
+ }
+ }
+
+ state->block.pos += state->blockheadersize;
+
+ /* Header provided one sample frame. */
+ state->framesleft--;
+
+ return 0;
+}
+
+/* Decodes the data of the IMA ADPCM block. Decoding will stop if a block is too
+ * short, returning with none or partially decoded data. The partial data always
+ * contains full sample frames (same sample count for each channel).
+ * Incomplete sample frames are discarded.
+ */
+static int
+IMA_ADPCM_DecodeBlockData(ADPCM_DecoderState *state)
+{
+ size_t i;
+ int retval = 0;
+ const Uint32 channels = state->channels;
+ const size_t subblockframesize = channels * 4;
+ Uint64 bytesrequired;
+ Uint32 c;
+
+ size_t blockpos = state->block.pos;
+ size_t blocksize = state->block.size;
+ size_t blockleft = blocksize - blockpos;
+
+ size_t outpos = state->output.pos;
+
+ Sint64 blockframesleft = state->samplesperblock - 1;
+ if (blockframesleft > state->framesleft) {
+ blockframesleft = state->framesleft;
+ }
+
+ bytesrequired = (blockframesleft + 7) / 8 * subblockframesize;
+ if (blockleft < bytesrequired) {
+ /* Data truncated. Calculate how many samples we can get out if it. */
+ const size_t guaranteedframes = blockleft / subblockframesize;
+ const size_t remainingbytes = blockleft % subblockframesize;
+ blockframesleft = guaranteedframes;
+ if (remainingbytes > subblockframesize - 4) {
+ blockframesleft += (remainingbytes % 4) * 2;
+ }
+ /* Signal the truncation. */
+ retval = -1;
+ }
+
+ /* Each channel has their nibbles packed into 32-bit blocks. These blocks
+ * are interleaved and make up the data part of the ADPCM block. This loop
+ * decodes the samples as they come from the input data and puts them at
+ * the appropriate places in the output data.
+ */
+ while (blockframesleft > 0) {
+ const size_t subblocksamples = blockframesleft < 8 ? (size_t)blockframesleft : 8;
+
+ for (c = 0; c < channels; c++) {
+ Uint8 nybble = 0;
+ /* Load previous sample which may come from the block header. */
+ Sint16 sample = state->output.data[outpos + c - channels];
+
+ for (i = 0; i < subblocksamples; i++) {
+ if (i & 1) {
+ nybble >>= 4;
+ } else {
+ nybble = state->block.data[blockpos++];
+ }
+
+ sample = IMA_ADPCM_ProcessNibble((Sint8 *)state->cstate + c, sample, nybble & 0x0f);
+ state->output.data[outpos + c + i * channels] = sample;
+ }
+ }
+
+ outpos += channels * subblocksamples;
+ state->framesleft -= subblocksamples;
+ blockframesleft -= subblocksamples;
+ }
+
+ state->block.pos = blockpos;
+ state->output.pos = outpos;
+
+ return retval;
+}
+
+static int
+IMA_ADPCM_Decode(WaveFile *file, Uint8 **audio_buf, Uint32 *audio_len)
+{
+ int result;
+ size_t bytesleft, outputsize;
+ WaveChunk *chunk = &file->chunk;
+ ADPCM_DecoderState state = {0};
+ Sint8 *cstate;
+
+ if (chunk->size != chunk->length) {
+ /* Could not read everything. Recalculate number of sample frames. */
+ if (IMA_ADPCM_CalculateSampleFrames(file, chunk->size) < 0) {
+ return -1;
+ }
+ }
+
+ /* Nothing to decode, nothing to return. */
+ if (file->sampleframes == 0) {
+ *audio_buf = NULL;
+ *audio_len = 0;
+ return 0;
+ }
+
+ state.channels = file->format.channels;
+ state.blocksize = file->format.blockalign;
+ state.blockheadersize = state.channels * 4;
+ state.samplesperblock = file->format.samplesperblock;
+ state.framesize = state.channels * sizeof(Sint16);
+ state.framestotal = file->sampleframes;
+ state.framesleft = state.framestotal;
+
+ state.input.data = chunk->data;
+ state.input.size = chunk->size;
+ state.input.pos = 0;
+
+ /* The output size in bytes. May get modified if data is truncated. */
+ outputsize = (size_t)state.framestotal;
+ if (MultiplySize(&outputsize, state.framesize)) {
+ return SDL_OutOfMemory();
+ } else if (outputsize > SDL_MAX_UINT32 || state.framestotal > SIZE_MAX) {
+ return SDL_SetError("WAVE file too big");
+ }
+
+ state.output.pos = 0;
+ state.output.size = outputsize / sizeof(Sint16);
+ state.output.data = (Sint16 *)SDL_malloc(outputsize);
+ if (state.output.data == NULL) {
+ return SDL_OutOfMemory();
+ }
+
+ cstate = (Sint8 *)SDL_calloc(state.channels, sizeof(Sint8));
+ if (cstate == NULL) {
+ SDL_free(state.output.data);
+ return SDL_OutOfMemory();
+ }
+ state.cstate = cstate;
+
+ /* Decode block by block. A truncated block will stop the decoding. */
+ bytesleft = state.input.size - state.input.pos;
+ while (state.framesleft > 0 && bytesleft >= state.blockheadersize) {
+ state.block.data = state.input.data + state.input.pos;
+ state.block.size = bytesleft < state.blocksize ? bytesleft : state.blocksize;
+ state.block.pos = 0;
+
+ if (state.output.size - state.output.pos < (Uint64)state.framesleft * state.channels) {
+ /* Somehow didn't allocate enough space for the output. */
+ SDL_free(state.output.data);
+ SDL_free(cstate);
+ return SDL_SetError("Unexpected overflow in IMA ADPCM decoder");
+ }
+
+ /* Initialize decoder with the values from the block header. */
+ result = IMA_ADPCM_DecodeBlockHeader(&state);
+
+ /* Decode the block data. It stores the samples directly in the output. */
+ result = IMA_ADPCM_DecodeBlockData(&state);
+ if (result == -1) {
+ /* Unexpected end. Stop decoding and return partial data if necessary. */
+ if (file->trunchint == TruncVeryStrict || file->trunchint == TruncVeryStrict) {
+ SDL_free(state.output.data);
+ SDL_free(cstate);
+ return SDL_SetError("Truncated data chunk");
+ } else if (file->trunchint != TruncDropFrame) {
+ state.output.pos -= state.output.pos % (state.samplesperblock * state.channels);
+ }
+ outputsize = state.output.pos * sizeof(Sint16); /* Can't overflow, is always smaller. */
+ break;
+ }
+
+ state.input.pos += state.block.size;
+ bytesleft = state.input.size - state.input.pos;
+ }
+
+ *audio_buf = (Uint8 *)state.output.data;
+ *audio_len = (Uint32)outputsize;
+
+ SDL_free(cstate);
+
+ return 0;
+}
+
+static int
+LAW_Init(WaveFile *file, size_t datalength)
+{
+ WaveFormat *format = &file->format;
+
+ /* Standards Update requires this to be 8. */
+ if (format->bitspersample != 8) {
+ return SDL_SetError("Invalid companded bits per sample of %d", (int)format->bitspersample);
+ }
+
+ /* Not going to bother with weird padding. */
+ if (format->blockalign != format->channels) {
+ return SDL_SetError("Unsupported block alignment");
+ }
+
+ if ((file->trunchint == TruncVeryStrict || file->trunchint == TruncStrict)) {
+ if (format->blockalign > 1 && datalength % format->blockalign) {
+ return SDL_SetError("Truncated data chunk in WAVE file");
+ }
+ }
+
+ file->sampleframes = WaveAdjustToFactValue(file, datalength / format->blockalign);
+ if (file->sampleframes < 0) {
+ return -1;
+ }
+
+ return 0;
+}
+
+static int
+LAW_Decode(WaveFile *file, Uint8 **audio_buf, Uint32 *audio_len)
+{
+#ifdef SDL_WAVE_LAW_LUT
+ const Sint16 alaw_lut[256] = {
+ -5504, -5248, -6016, -5760, -4480, -4224, -4992, -4736, -7552, -7296, -8064, -7808, -6528, -6272, -7040, -6784, -2752,
+ -2624, -3008, -2880, -2240, -2112, -2496, -2368, -3776, -3648, -4032, -3904, -3264, -3136, -3520, -3392, -22016,
+ -20992, -24064, -23040, -17920, -16896, -19968, -18944, -30208, -29184, -32256, -31232, -26112, -25088, -28160, -27136, -11008,
+ -10496, -12032, -11520, -8960, -8448, -9984, -9472, -15104, -14592, -16128, -15616, -13056, -12544, -14080, -13568, -344,
+ -328, -376, -360, -280, -264, -312, -296, -472, -456, -504, -488, -408, -392, -440, -424, -88,
+ -72, -120, -104, -24, -8, -56, -40, -216, -200, -248, -232, -152, -136, -184, -168, -1376,
+ -1312, -1504, -1440, -1120, -1056, -1248, -1184, -1888, -1824, -2016, -1952, -1632, -1568, -1760, -1696, -688,
+ -656, -752, -720, -560, -528, -624, -592, -944, -912, -1008, -976, -816, -784, -880, -848, 5504,
+ 5248, 6016, 5760, 4480, 4224, 4992, 4736, 7552, 7296, 8064, 7808, 6528, 6272, 7040, 6784, 2752,
+ 2624, 3008, 2880, 2240, 2112, 2496, 2368, 3776, 3648, 4032, 3904, 3264, 3136, 3520, 3392, 22016,
+ 20992, 24064, 23040, 17920, 16896, 19968, 18944, 30208, 29184, 32256, 31232, 26112, 25088, 28160, 27136, 11008,
+ 10496, 12032, 11520, 8960, 8448, 9984, 9472, 15104, 14592, 16128, 15616, 13056, 12544, 14080, 13568, 344,
+ 328, 376, 360, 280, 264, 312, 296, 472, 456, 504, 488, 408, 392, 440, 424, 88,
+ 72, 120, 104, 24, 8, 56, 40, 216, 200, 248, 232, 152, 136, 184, 168, 1376,
+ 1312, 1504, 1440, 1120, 1056, 1248, 1184, 1888, 1824, 2016, 1952, 1632, 1568, 1760, 1696, 688,
+ 656, 752, 720, 560, 528, 624, 592, 944, 912, 1008, 976, 816, 784, 880, 848
+ };
+ const Sint16 mulaw_lut[256] = {
+ -32124, -31100, -30076, -29052, -28028, -27004, -25980, -24956, -23932, -22908, -21884, -20860, -19836, -18812, -17788, -16764, -15996,
+ -15484, -14972, -14460, -13948, -13436, -12924, -12412, -11900, -11388, -10876, -10364, -9852, -9340, -8828, -8316, -7932,
+ -7676, -7420, -7164, -6908, -6652, -6396, -6140, -5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092, -3900,
+ -3772, -3644, -3516, -3388, -3260, -3132, -3004, -2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980, -1884,
+ -1820, -1756, -1692, -1628, -1564, -1500, -1436, -1372, -1308, -1244, -1180, -1116, -1052, -988, -924, -876,
+ -844, -812, -780, -748, -716, -684, -652, -620, -588, -556, -524, -492, -460, -428, -396, -372,
+ -356, -340, -324, -308, -292, -276, -260, -244, -228, -212, -196, -180, -164, -148, -132, -120,
+ -112, -104, -96, -88, -80, -72, -64, -56, -48, -40, -32, -24, -16, -8, 0, 32124,
+ 31100, 30076, 29052, 28028, 27004, 25980, 24956, 23932, 22908, 21884, 20860, 19836, 18812, 17788, 16764, 15996,
+ 15484, 14972, 14460, 13948, 13436, 12924, 12412, 11900, 11388, 10876, 10364, 9852, 9340, 8828, 8316, 7932,
+ 7676, 7420, 7164, 6908, 6652, 6396, 6140, 5884, 5628, 5372, 5116, 4860, 4604, 4348, 4092, 3900,
+ 3772, 3644, 3516, 3388, 3260, 3132, 3004, 2876, 2748, 2620, 2492, 2364, 2236, 2108, 1980, 1884,
+ 1820, 1756, 1692, 1628, 1564, 1500, 1436, 1372, 1308, 1244, 1180, 1116, 1052, 988, 924, 876,
+ 844, 812, 780, 748, 716, 684, 652, 620, 588, 556, 524, 492, 460, 428, 396, 372,
+ 356, 340, 324, 308, 292, 276, 260, 244, 228, 212, 196, 180, 164, 148, 132, 120,
+ 112, 104, 96, 88, 80, 72, 64, 56, 48, 40, 32, 24, 16, 8, 0
+ };
+#endif
+
+ WaveFormat *format = &file->format;
+ WaveChunk *chunk = &file->chunk;
+ size_t i, sample_count, expanded_len;
+ Uint8 *src;
+ Sint16 *dst;
+
+ if (chunk->length != chunk->size) {
+ file->sampleframes = WaveAdjustToFactValue(file, chunk->size / format->blockalign);
+ if (file->sampleframes < 0) {
+ return -1;
+ }
+ }
+
+ /* Nothing to decode, nothing to return. */
+ if (file->sampleframes == 0) {
+ *audio_buf = NULL;
+ *audio_len = 0;
+ return 0;
+ }
+
+ sample_count = (size_t)file->sampleframes;
+ if (MultiplySize(&sample_count, format->channels)) {
+ return SDL_OutOfMemory();
+ }
+
+ expanded_len = sample_count;
+ if (MultiplySize(&expanded_len, sizeof(Sint16))) {
+ return SDL_OutOfMemory();
+ } else if (expanded_len > SDL_MAX_UINT32 || file->sampleframes > SIZE_MAX) {
+ return SDL_SetError("WAVE file too big");
+ }
+
+ src = (Uint8 *)SDL_realloc(chunk->data, expanded_len);
+ if (src == NULL) {
+ return SDL_OutOfMemory();
+ }
+ chunk->data = NULL;
+ chunk->size = 0;
+
+ dst = (Sint16 *)src;
+
+ /* Work backwards, since we're expanding in-place. SDL_AudioSpec.format will
+ * inform the caller about the byte order.
+ */
+ i = sample_count;
+ switch (file->format.encoding) {
+#ifdef SDL_WAVE_LAW_LUT
+ case ALAW_CODE:
+ while (i--) {
+ dst[i] = alaw_lut[src[i]];
+ }
+ break;
+ case MULAW_CODE:
+ while (i--) {
+ dst[i] = mulaw_lut[src[i]];
+ }
+ break;
+#else
+ case ALAW_CODE:
+ while (i--) {
+ Uint8 nibble = src[i];
+ Uint8 exponent = (nibble & 0x7f) ^ 0x55;
+ Sint16 mantissa = exponent & 0xf;
+
+ exponent >>= 4;
+ if (exponent > 0) {
+ mantissa |= 0x10;
+ }
+ mantissa = mantissa << 4 | 0x8;
+ if (exponent > 1) {
+ mantissa <<= exponent - 1;
+ }
+
+ dst[i] = nibble & 0x80 ? mantissa : -mantissa;
+ }
+ break;
+ case MULAW_CODE:
+ while (i--) {
+ Uint8 nibble = ~src[i];
+ Sint16 mantissa = nibble & 0xf;
+ Uint8 exponent = nibble >> 4 & 0x7;
+ Sint16 step = 4 << (exponent + 1);
+
+ mantissa = (0x80 << exponent) + step * mantissa + step / 2 - 132;
+
+ dst[i] = nibble & 0x80 ? -mantissa : mantissa;
+ }
+ break;
+#endif
+ default:
+ SDL_free(src);
+ return SDL_SetError("Unknown companded encoding");
+ }
+
+ *audio_buf = src;
+ *audio_len = (Uint32)expanded_len;
+
+ return 0;
+}
+
+static int
+PCM_Init(WaveFile *file, size_t datalength)
+{
+ WaveFormat *format = &file->format;
+
+ if (format->encoding == PCM_CODE) {
+ switch (format->bitspersample) {
+ case 8:
+ case 16:
+ case 24:
+ case 32:
+ /* These are supported. */
+ break;
+ default:
+ return SDL_SetError("%d-bit PCM format not supported", (int)format->bitspersample);
+ }
+ } else if (format->encoding == IEEE_FLOAT_CODE) {
+ if (format->bitspersample != 32) {
+ return SDL_SetError("%d-bit IEEE floating-point format not supported", (int)format->bitspersample);
+ }
+ }
+
+ /* It wouldn't be that hard to support more exotic block sizes, but
+ * the most common formats should do for now.
+ */
+ if (format->blockalign * 8 != format->channels * format->bitspersample) {
+ return SDL_SetError("Unsupported block alignment");
+ }
+
+ if ((file->trunchint == TruncVeryStrict || file->trunchint == TruncStrict)) {
+ if (format->blockalign > 1 && datalength % format->blockalign) {
+ return SDL_SetError("Truncated data chunk in WAVE file");
+ }
+ }
+
+ file->sampleframes = WaveAdjustToFactValue(file, datalength / format->blockalign);
+ if (file->sampleframes < 0) {
+ return -1;
+ }
+
+ return 0;
+}
+
+static int
+PCM_ConvertSint24ToSint32(WaveFile *file, Uint8 **audio_buf, Uint32 *audio_len)
+{
+ WaveFormat *format = &file->format;
+ WaveChunk *chunk = &file->chunk;
+ size_t i, expanded_len, sample_count;
+ Uint8 *ptr;
+
+ sample_count = (size_t)file->sampleframes;
+ if (MultiplySize(&sample_count, format->channels)) {
+ return SDL_OutOfMemory();
+ }
+
+ expanded_len = sample_count;
+ if (MultiplySize(&expanded_len, sizeof(Sint32))) {
+ return SDL_OutOfMemory();
+ } else if (expanded_len > SDL_MAX_UINT32 || file->sampleframes > SIZE_MAX) {
+ return SDL_SetError("WAVE file too big");
+ }
+
+ ptr = (Uint8 *)SDL_realloc(chunk->data, expanded_len);
+ if (ptr == NULL) {
+ return SDL_OutOfMemory();
+ }
+
+ /* This pointer is now invalid. */
+ chunk->data = NULL;
+ chunk->size = 0;
+
+ *audio_buf = ptr;
+ *audio_len = (Uint32)expanded_len;
+
+ /* work from end to start, since we're expanding in-place. */
+ for (i = sample_count; i > 0; i--) {
+ const size_t o = i - 1;
+ uint8_t b[4];
+
+ b[0] = 0;
+ b[1] = ptr[o * 3];
+ b[2] = ptr[o * 3 + 1];
+ b[3] = ptr[o * 3 + 2];
+
+ ptr[o * 4 + 0] = b[0];
+ ptr[o * 4 + 1] = b[1];
+ ptr[o * 4 + 2] = b[2];
+ ptr[o * 4 + 3] = b[3];
+ }
+
+ return 0;
+}
+
+static int
+PCM_Decode(WaveFile *file, Uint8 **audio_buf, Uint32 *audio_len)
+{
+ WaveFormat *format = &file->format;
+ WaveChunk *chunk = &file->chunk;
+ size_t outputsize;
+
+ if (chunk->length != chunk->size) {
+ file->sampleframes = WaveAdjustToFactValue(file, chunk->size / format->blockalign);
+ if (file->sampleframes < 0) {
+ return -1;
+ }
+ }
+
+ /* Nothing to decode, nothing to return. */
+ if (file->sampleframes == 0) {
+ *audio_buf = NULL;
+ *audio_len = 0;
+ return 0;
+ }
+
+ /* 24-bit samples get shifted to 32 bits. */
+ if (format->encoding == PCM_CODE && format->bitspersample == 24) {
+ return PCM_ConvertSint24ToSint32(file, audio_buf, audio_len);
+ }
+
+ outputsize = (size_t)file->sampleframes;
+ if (MultiplySize(&outputsize, format->blockalign)) {
+ return SDL_OutOfMemory();
+ } else if (outputsize > SDL_MAX_UINT32 || file->sampleframes > SIZE_MAX) {
+ return SDL_SetError("WAVE file too big");
+ }
+
+ *audio_buf = chunk->data;
+ *audio_len = (Uint32)outputsize;
-static int ReadChunk(SDL_RWops * src, Chunk * chunk);
+ /* This pointer is going to be returned to the caller. Prevent free in cleanup. */
+ chunk->data = NULL;
+ chunk->size = 0;
-struct MS_ADPCM_decodestate
-{
- Uint8 hPredictor;
- Uint16 iDelta;
- Sint16 iSamp1;
- Sint16 iSamp2;
-};
-static struct MS_ADPCM_decoder
-{
- WaveFMT wavefmt;
- Uint16 wSamplesPerBlock;
- Uint16 wNumCoef;
- Sint16 aCoeff[7][2];
- /* * * */
- struct MS_ADPCM_decodestate state[2];
-} MS_ADPCM_state;
+ return 0;
+}
-static int
-InitMS_ADPCM(WaveFMT * format)
-{
- Uint8 *rogue_feel;
- int i;
-
- /* Set the rogue pointer to the MS_ADPCM specific data */
- MS_ADPCM_state.wavefmt.encoding = SDL_SwapLE16(format->encoding);
- MS_ADPCM_state.wavefmt.channels = SDL_SwapLE16(format->channels);
- MS_ADPCM_state.wavefmt.frequency = SDL_SwapLE32(format->frequency);
- MS_ADPCM_state.wavefmt.byterate = SDL_SwapLE32(format->byterate);
- MS_ADPCM_state.wavefmt.blockalign = SDL_SwapLE16(format->blockalign);
- MS_ADPCM_state.wavefmt.bitspersample =
- SDL_SwapLE16(format->bitspersample);
- rogue_feel = (Uint8 *) format + sizeof(*format);
- if (sizeof(*format) == 16) {
- /* const Uint16 extra_info = ((rogue_feel[1] << 8) | rogue_feel[0]); */
- rogue_feel += sizeof(Uint16);
- }
- MS_ADPCM_state.wSamplesPerBlock = ((rogue_feel[1] << 8) | rogue_feel[0]);
- rogue_feel += sizeof(Uint16);
- MS_ADPCM_state.wNumCoef = ((rogue_feel[1] << 8) | rogue_feel[0]);
- rogue_feel += sizeof(Uint16);
- if (MS_ADPCM_state.wNumCoef != 7) {
- SDL_SetError("Unknown set of MS_ADPCM coefficients");
- return (-1);
- }
- for (i = 0; i < MS_ADPCM_state.wNumCoef; ++i) {
- MS_ADPCM_state.aCoeff[i][0] = ((rogue_feel[1] << 8) | rogue_feel[0]);
- rogue_feel += sizeof(Uint16);
- MS_ADPCM_state.aCoeff[i][1] = ((rogue_feel[1] << 8) | rogue_feel[0]);
- rogue_feel += sizeof(Uint16);
- }
- return (0);
-}
-
-static Sint32
-MS_ADPCM_nibble(struct MS_ADPCM_decodestate *state,
- Uint8 nybble, Sint16 * coeff)
-{
- const Sint32 max_audioval = ((1 << (16 - 1)) - 1);
- const Sint32 min_audioval = -(1 << (16 - 1));
- const Sint32 adaptive[] = {
- 230, 230, 230, 230, 307, 409, 512, 614,
- 768, 614, 512, 409, 307, 230, 230, 230
- };
- Sint32 new_sample, delta;
+static WaveRiffSizeHint
+WaveGetRiffSizeHint()
+{
+ const char *hint = SDL_GetHint(SDL_HINT_WAVE_RIFF_CHUNK_SIZE);
- new_sample = ((state->iSamp1 * coeff[0]) +
- (state->iSamp2 * coeff[1])) / 256;
- if (nybble & 0x08) {
- new_sample += state->iDelta * (nybble - 0x10);
- } else {
- new_sample += state->iDelta * nybble;
- }
- if (new_sample < min_audioval) {
- new_sample = min_audioval;
- } else if (new_sample > max_audioval) {
- new_sample = max_audioval;
- }
- delta = ((Sint32) state->iDelta * adaptive[nybble]) / 256;
- if (delta < 16) {
- delta = 16;
+ if (hint != NULL) {
+ if (SDL_strcmp(hint, "chunksearch") == 0) {
+ return RiffSizeChunkSearch;
+ } else if (SDL_strcmp(hint, "ignore") == 0) {
+ return RiffSizeIgnore;
+ } else if (SDL_strcmp(hint, "ignorezero") == 0) {
+ return RiffSizeIgnoreZero;
+ } else if (SDL_strcmp(hint, "maximum") == 0) {
+ return RiffSizeMaximum;
+ }
}
- state->iDelta = (Uint16) delta;
- state->iSamp2 = state->iSamp1;
- state->iSamp1 = (Sint16) new_sample;
- return (new_sample);
+
+ return RiffSizeNoHint;
}
-static int
-MS_ADPCM_decode(Uint8 ** audio_buf, Uint32 * audio_len)
-{
- struct MS_ADPCM_decodestate *state[2];
- Uint8 *freeable, *encoded, *decoded;
- Sint32 encoded_len, samplesleft;
- Sint8 nybble;
- Uint8 stereo;
- Sint16 *coeff[2];
- Sint32 new_sample;
+static WaveTruncationHint
+WaveGetTruncationHint()
+{
+ const char *hint = SDL_GetHint(SDL_HINT_WAVE_TRUNCATION);
- /* Allocate the proper sized output buffer */
- encoded_len = *audio_len;
- encoded = *audio_buf;
- freeable = *audio_buf;
- *audio_len = (encoded_len / MS_ADPCM_state.wavefmt.blockalign) *
- MS_ADPCM_state.wSamplesPerBlock *
- MS_ADPCM_state.wavefmt.channels * sizeof(Sint16);
- *audio_buf = (Uint8 *) SDL_malloc(*audio_len);
- if (*audio_buf == NULL) {
- return SDL_OutOfMemory();
+ if (hint != NULL) {
+ if (SDL_strcmp(hint, "verystrict") == 0) {
+ return TruncVeryStrict;
+ } else if (SDL_strcmp(hint, "strict") == 0) {
+ return TruncStrict;
+ } else if (SDL_strcmp(hint, "dropframe") == 0) {
+ return TruncDropFrame;
+ } else if (SDL_strcmp(hint, "dropblock") == 0) {
+ return TruncDropBlock;
+ }
}
- decoded = *audio_buf;
-
- /* Get ready... Go! */
- stereo = (MS_ADPCM_state.wavefmt.channels == 2);
- state[0] = &MS_ADPCM_state.state[0];
- state[1] = &MS_ADPCM_state.state[stereo];
- while (encoded_len >= MS_ADPCM_state.wavefmt.blockalign) {
- /* Grab the initial information for this block */
- state[0]->hPredictor = *encoded++;
- if (stereo) {
- state[1]->hPredictor = *encoded++;
- }
- state[0]->iDelta = ((encoded[1] << 8) | encoded[0]);
- encoded += sizeof(Sint16);
- if (stereo) {
- state[1]->iDelta = ((encoded[1] << 8) | encoded[0]);
- encoded += sizeof(Sint16);
- }
- state[0]->iSamp1 = ((encoded[1] << 8) | encoded[0]);
- encoded += sizeof(Sint16);
- if (stereo) {
- state[1]->iSamp1 = ((encoded[1] << 8) | encoded[0]);
- encoded += sizeof(Sint16);
- }
- state[0]->iSamp2 = ((encoded[1] << 8) | encoded[0]);
- encoded += sizeof(Sint16);
- if (stereo) {
- state[1]->iSamp2 = ((encoded[1] << 8) | encoded[0]);
- encoded += sizeof(Sint16);
- }
- coeff[0] = MS_ADPCM_state.aCoeff[state[0]->hPredictor];
- coeff[1] = MS_ADPCM_state.aCoeff[state[1]->hPredictor];
-
- /* Store the two initial samples we start with */
- decoded[0] = state[0]->iSamp2 & 0xFF;
- decoded[1] = state[0]->iSamp2 >> 8;
- decoded += 2;
- if (stereo) {
- decoded[0] = state[1]->iSamp2 & 0xFF;
- decoded[1] = state[1]->iSamp2 >> 8;
- decoded += 2;
- }
- decoded[0] = state[0]->iSamp1 & 0xFF;
- decoded[1] = state[0]->iSamp1 >> 8;
- decoded += 2;
- if (stereo) {
- decoded[0] = state[1]->iSamp1 & 0xFF;
- decoded[1] = state[1]->iSamp1 >> 8;
- decoded += 2;
- }
-
- /* Decode and store the other samples in this block */
- samplesleft = (MS_ADPCM_state.wSamplesPerBlock - 2) *
- MS_ADPCM_state.wavefmt.channels;
- while (samplesleft > 0) {
- nybble = (*encoded) >> 4;
- new_sample = MS_ADPCM_nibble(state[0], nybble, coeff[0]);
- decoded[0] = new_sample & 0xFF;
- new_sample >>= 8;
- decoded[1] = new_sample & 0xFF;
- decoded += 2;
-
- nybble = (*encoded) & 0x0F;
- new_sample = MS_ADPCM_nibble(state[1], nybble, coeff[1]);
- decoded[0] = new_sample & 0xFF;
- new_sample >>= 8;
- decoded[1] = new_sample & 0xFF;
- decoded += 2;
-
- ++encoded;
- samplesleft -= 2;
- }
- encoded_len -= MS_ADPCM_state.wavefmt.blockalign;
- }
- SDL_free(freeable);
- return (0);
-}
-
-struct IMA_ADPCM_decodestate
-{
- Sint32 sample;
- Sint8 index;
-};
-static struct IMA_ADPCM_decoder
-{
- WaveFMT wavefmt;
- Uint16 wSamplesPerBlock;
- /* * * */
- struct IMA_ADPCM_decodestate state[2];
-} IMA_ADPCM_state;
-static int
-InitIMA_ADPCM(WaveFMT * format, int length)
-{
- Uint8 *rogue_feel, *rogue_feel_end;
-
- /* Set the rogue pointer to the IMA_ADPCM specific data */
- if (length < sizeof(*format)) goto too_short;
- IMA_ADPCM_state.wavefmt.encoding = SDL_SwapLE16(format->encoding);
- IMA_ADPCM_state.wavefmt.channels = SDL_SwapLE16(format->channels);
- IMA_ADPCM_state.wavefmt.frequency = SDL_SwapLE32(format->frequency);
- IMA_ADPCM_state.wavefmt.byterate = SDL_SwapLE32(format->byterate);
- IMA_ADPCM_state.wavefmt.blockalign = SDL_SwapLE16(format->blockalign);
- IMA_ADPCM_state.wavefmt.bitspersample = SDL_SwapLE16(format->bitspersample);
- rogue_feel = (Uint8 *) format + sizeof(*format);
- rogue_feel_end = (Uint8 *) format + length;
- if (sizeof(*format) == 16) {
- /* const Uint16 extra_info = ((rogue_feel[1] << 8) | rogue_feel[0]); */
- rogue_feel += sizeof(Uint16);
- }
- if (rogue_feel + 2 > rogue_feel_end) goto too_short;
- IMA_ADPCM_state.wSamplesPerBlock = ((rogue_feel[1] << 8) | rogue_feel[0]);
- return (0);
-too_short:
- SDL_SetError("Unexpected length of a chunk with an IMA ADPCM format");
- return (-1);
-}
-
-static Sint32
-IMA_ADPCM_nibble(struct IMA_ADPCM_decodestate *state, Uint8 nybble)
-{
- const Sint32 max_audioval = ((1 << (16 - 1)) - 1);
- const Sint32 min_audioval = -(1 << (16 - 1));
- const int index_table[16] = {
- -1, -1, -1, -1,
- 2, 4, 6, 8,
- -1, -1, -1, -1,
- 2, 4, 6, 8
- };
- const Sint32 step_table[89] = {
- 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31,
- 34, 37, 41, 45, 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 130,
- 143, 157, 173, 190, 209, 230, 253, 279, 307, 337, 371, 408,
- 449, 494, 544, 598, 658, 724, 796, 876, 963, 1060, 1166, 1282,
- 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327,
- 3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630,
- 9493, 10442, 11487, 12635, 13899, 15289, 16818, 18500, 20350,
- 22385, 24623, 27086, 29794, 32767
- };
- Sint32 delta, step;
+ return TruncNoHint;
+}
+
+static WaveFactChunkHint
+WaveGetFactChunkHint()
+{
+ const char *hint = SDL_GetHint(SDL_HINT_WAVE_FACT_CHUNK);
- /* Compute difference and new sample value */
- if (state->index > 88) {
- state->index = 88;
- } else if (state->index < 0) {
- state->index = 0;
+ if (hint != NULL) {
+ if (SDL_strcmp(hint, "truncate") == 0) {
+ return FactTruncate;
+ } else if (SDL_strcmp(hint, "strict") == 0) {
+ return FactStrict;
+ } else if (SDL_strcmp(hint, "ignorezero") == 0) {
+ return FactIgnoreZero;
+ } else if (SDL_strcmp(hint, "ignore") == 0) {
+ return FactIgnore;
+ }
}
- /* explicit cast to avoid gcc warning about using 'char' as array index */
- step = step_table[(int)state->index];
- delta = step >> 3;
- if (nybble & 0x04)
- delta += step;
- if (nybble & 0x02)
- delta += (step >> 1);
- if (nybble & 0x01)
- delta += (step >> 2);
- if (nybble & 0x08)
- delta = -delta;
- state->sample += delta;
- /* Update index value */
- state->index += index_table[nybble];
+ return FactNoHint;
+}
- /* Clamp output sample */
- if (state->sample > max_audioval) {
- state->sample = max_audioval;
- } else if (state->sample < min_audioval) {
- state->sample = min_audioval;
+static void
+WaveFreeChunkData(WaveChunk *chunk)
+{
+ if (chunk->data != NULL) {
+ SDL_free(chunk->data);
+ chunk->data = NULL;
}
- return (state->sample);
+ chunk->size = 0;
}
-/* Fill the decode buffer with a channel block of data (8 samples) */
-static void
-Fill_IMA_ADPCM_block(Uint8 * decoded, Uint8 * encoded,
- int channel, int numchannels,
- struct IMA_ADPCM_decodestate *state)
+static int
+WaveNextChunk(SDL_RWops *src, WaveChunk *chunk)
{
- int i;
- Sint8 nybble;
- Sint32 new_sample;
+ Uint32 chunkheader[2];
+ Sint64 nextposition = chunk->position + chunk->length;
- decoded += (channel * 2);
- for (i = 0; i < 4; ++i) {
- nybble = (*encoded) & 0x0F;
- new_sample = IMA_ADPCM_nibble(state, nybble);
- decoded[0] = new_sample & 0xFF;
- new_sample >>= 8;
- decoded[1] = new_sample & 0xFF;
- decoded += 2 * numchannels;
+ /* Data is no longer valid after this function returns. */
+ WaveFreeChunkData(chunk);
- nybble = (*encoded) >> 4;
- new_sample = IMA_ADPCM_nibble(state, nybble);
- decoded[0] = new_sample & 0xFF;
- new_sample >>= 8;
- decoded[1] = new_sample & 0xFF;
- decoded += 2 * numchannels;
+ /* RIFF chunks have a 2-byte alignment. Skip padding byte. */
+ if (chunk->length & 1) {
+ nextposition++;
+ }
- ++encoded;
+ if (SDL_RWseek(src, nextposition, RW_SEEK_SET) != nextposition) {
+ /* Not sure how we ended up here. Just abort. */
+ return -2;
+ } else if (SDL_RWread(src, chunkheader, 4, 2) != 2) {
+ return -1;
}
+
+ chunk->fourcc = SDL_SwapLE32(chunkheader[0]);
+ chunk->length = SDL_SwapLE32(chunkheader[1]);
+ chunk->position = nextposition + 8;
+
+ return 0;
}
static int
-IMA_ADPCM_decode(Uint8 ** audio_buf, Uint32 * audio_len)
-{
- struct IMA_ADPCM_decodestate *state;
- Uint8 *freeable, *encoded, *decoded;
- Sint32 encoded_len, samplesleft;
- unsigned int c, channels;
-
- /* Check to make sure we have enough variables in the state array */
- channels = IMA_ADPCM_state.wavefmt.channels;
- if (channels > SDL_arraysize(IMA_ADPCM_state.state)) {
- SDL_SetError("IMA ADPCM decoder can only handle %u channels",
- (unsigned int)SDL_arraysize(IMA_ADPCM_state.state));
- return (-1);
- }
- state = IMA_ADPCM_state.state;
-
- /* Allocate the proper sized output buffer */
- encoded_len = *audio_len;
- encoded = *audio_buf;
- freeable = *audio_buf;
- *audio_len = (encoded_len / IMA_ADPCM_state.wavefmt.blockalign) *
- IMA_ADPCM_state.wSamplesPerBlock *
- IMA_ADPCM_state.wavefmt.channels * sizeof(Sint16);
- *audio_buf = (Uint8 *) SDL_malloc(*audio_len);
- if (*audio_buf == NULL) {
- return SDL_OutOfMemory();
+WaveReadPartialChunkData(SDL_RWops *src, WaveChunk *chunk, size_t length)
+{
+ WaveFreeChunkData(chunk);
+
+ if (length > chunk->length) {
+ length = chunk->length;
}
- decoded = *audio_buf;
-
- /* Get ready... Go! */
- while (encoded_len >= IMA_ADPCM_state.wavefmt.blockalign) {
- /* Grab the initial information for this block */
- for (c = 0; c < channels; ++c) {
- /* Fill the state information for this block */
- state[c].sample = ((encoded[1] << 8) | encoded[0]);
- encoded += 2;
- if (state[c].sample & 0x8000) {
- state[c].sample -= 0x10000;
- }
- state[c].index = *encoded++;
- /* Reserved byte in buffer header, should be 0 */
- if (*encoded++ != 0) {
- /* Uh oh, corrupt data? Buggy code? */ ;
- }
- /* Store the initial sample we start with */
- decoded[0] = (Uint8) (state[c].sample & 0xFF);
- decoded[1] = (Uint8) (state[c].sample >> 8);
- decoded += 2;
+ if (length > 0) {
+ chunk->data = SDL_malloc(length);
+ if (chunk->data == NULL) {
+ return SDL_OutOfMemory();
}
- /* Decode and store the other samples in this block */
- samplesleft = (IMA_ADPCM_state.wSamplesPerBlock - 1) * channels;
- while (samplesleft > 0) {
- for (c = 0; c < channels; ++c) {
- Fill_IMA_ADPCM_block(decoded, encoded,
- c, channels, &state[c]);
- encoded += 4;
- samplesleft -= 8;
- }
- decoded += (channels * 8 * 2);
+ if (SDL_RWseek(src, chunk->position, RW_SEEK_SET) != chunk->position) {
+ /* Not sure how we ended up here. Just abort. */
+ return -2;
+ }
+
+ chunk->size = SDL_RWread(src, chunk->data, 1, length);
+ if (chunk->size != length) {
+ /* Expected to be handled by the caller. */
}
- encoded_len -= IMA_ADPCM_state.wavefmt.blockalign;
}
- SDL_free(freeable);
- return (0);
+
+ return 0;
+}
+
+static int
+WaveReadChunkData(SDL_RWops *src, WaveChunk *chunk)
+{
+ return WaveReadPartialChunkData(src, chunk, chunk->length);
}
+typedef struct WaveExtensibleGUID {
+ Uint16 encoding;
+ Uint8 guid[16];
+} WaveExtensibleGUID;
+
+/* Some of the GUIDs that are used by WAVEFORMATEXTENSIBLE. */
+#define WAVE_FORMATTAG_GUID(tag) {(tag) & 0xff, (tag) >> 8, 0, 0, 0, 0, 16, 0, 128, 0, 0, 170, 0, 56, 155, 113}
+static WaveExtensibleGUID extensible_guids[] = {
+ {PCM_CODE, WAVE_FORMATTAG_GUID(PCM_CODE)},
+ {MS_ADPCM_CODE, WAVE_FORMATTAG_GUID(MS_ADPCM_CODE)},
+ {IEEE_FLOAT_CODE, WAVE_FORMATTAG_GUID(IEEE_FLOAT_CODE)},
+ {ALAW_CODE, WAVE_FORMATTAG_GUID(ALAW_CODE)},
+ {MULAW_CODE, WAVE_FORMATTAG_GUID(MULAW_CODE)},
+ {IMA_ADPCM_CODE, WAVE_FORMATTAG_GUID(IMA_ADPCM_CODE)}
+};
+
+static Uint16
+WaveGetFormatGUIDEncoding(WaveFormat *format)
+{
+ size_t i;
+ for (i = 0; i < SDL_arraysize(extensible_guids); i++) {
+ if (SDL_memcmp(format->subformat, extensible_guids[i].guid, 16) == 0) {
+ return extensible_guids[i].encoding;
+ }
+ }
+ return UNKNOWN_CODE;
+}
static int
-ConvertSint24ToSint32(Uint8 ** audio_buf, Uint32 * audio_len)
-{
- const double DIVBY8388608 = 0.00000011920928955078125;
- const Uint32 original_len = *audio_len;
- const Uint32 samples = original_len / 3;
- const Uint32 expanded_len = samples * sizeof (Uint32);
- Uint8 *ptr = (Uint8 *) SDL_realloc(*audio_buf, expanded_len);
- const Uint8 *src;
- Uint32 *dst;
- Uint32 i;
-
- if (!ptr) {
+WaveReadFormat(WaveFile *file)
+{
+ WaveChunk *chunk = &file->chunk;
+ WaveFormat *format = &file->format;
+ SDL_RWops *fmtsrc;
+ size_t fmtlen = chunk->size;
+
+ if (fmtlen > SDL_MAX_SINT32) {
+ /* Limit given by SDL_RWFromConstMem. */
+ return SDL_SetError("Data of WAVE fmt chunk too big");
+ }
+ fmtsrc = SDL_RWFromConstMem(chunk->data, (int)chunk->size);
+ if (fmtsrc == NULL) {
return SDL_OutOfMemory();
}
- *audio_buf = ptr;
- *audio_len = expanded_len;
+ format->formattag = SDL_ReadLE16(fmtsrc);
+ format->encoding = format->formattag;
+ format->channels = SDL_ReadLE16(fmtsrc);
+ format->frequency = SDL_ReadLE32(fmtsrc);
+ format->byterate = SDL_ReadLE32(fmtsrc);
+ format->blockalign = SDL_ReadLE16(fmtsrc);
- /* work from end to start, since we're expanding in-place. */
- src = (ptr + original_len) - 3;
- dst = ((Uint32 *) (ptr + expanded_len)) - 1;
- for (i = 0; i < samples; i++) {
- /* There's probably a faster way to do all this. */
- const Sint32 converted = ((Sint32) ( (((Uint32) src[2]) << 24) |
- (((Uint32) src[1]) << 16) |
- (((Uint32) src[0]) << 8) )) >> 8;
- const double scaled = (((double) converted) * DIVBY8388608);
- src -= 3;
- *(dst--) = (Sint32) (scaled * 2147483647.0);
+ /* This is PCM specific in the first version of the specification. */
+ if (fmtlen >= 16) {
+ format->bitspersample = SDL_ReadLE16(fmtsrc);
+ } else if (format->encoding == PCM_CODE) {
+ SDL_RWclose(fmtsrc);
+ return SDL_SetError("Missing wBitsPerSample field in WAVE fmt chunk");
+ }
+
+ /* The earlier versions also don't have this field. */
+ if (fmtlen >= 18) {
+ format->extsize = SDL_ReadLE16(fmtsrc);
+ }
+
+ if (format->formattag == EXTENSIBLE_CODE) {
+ /* note that this ignores channel masks, smaller valid bit counts
+ * inside a larger container, and most subtypes. This is just enough
+ * to get things that didn't really _need_ WAVE_FORMAT_EXTENSIBLE
+ * to be useful working when they use this format flag.
+ */
+
+ /* Extensible header must be at least 22 bytes. */
+ if (fmtlen < 40 || format->extsize < 22) {
+ SDL_RWclose(fmtsrc);
+ return SDL_SetError("Extensible WAVE header too small");
+ }
+
+ format->validsamplebits = SDL_ReadLE16(fmtsrc);
+ format->samplesperblock = format->validsamplebits;
+ format->channelmask = SDL_ReadLE32(fmtsrc);
+ SDL_RWread(fmtsrc, format->subformat, 1, 16);
+ format->encoding = WaveGetFormatGUIDEncoding(format);
}
+ SDL_RWclose(fmtsrc);
+
return 0;
}
+static int
+WaveCheckFormat(WaveFile *file, size_t datalength)
+{
+ WaveFormat *format = &file->format;
+
+ /* Check for some obvious issues. */
-/* GUIDs that are used by WAVE_FORMAT_EXTENSIBLE */
-static const Uint8 extensible_pcm_guid[16] = { 1, 0, 0, 0, 0, 0, 16, 0, 128, 0, 0, 170, 0, 56, 155, 113 };
-static const Uint8 extensible_ieee_guid[16] = { 3, 0, 0, 0, 0, 0, 16, 0, 128, 0, 0, 170, 0, 56, 155, 113 };
+ if (format->channels == 0) {
+ return SDL_SetError("Invalid number of channels");
+ } else if (format->channels > 255) {
+ /* Limit given by SDL_AudioSpec.channels. */
+ return SDL_SetError("Number of channels exceeds limit of 255");
+ }
-SDL_AudioSpec *
-SDL_LoadWAV_RW(SDL_RWops * src, int freesrc,
- SDL_AudioSpec * spec, Uint8 ** audio_buf, Uint32 * audio_len)
-{
- int was_error;
- Chunk chunk;
- int lenread;
- int IEEE_float_encoded, MS_ADPCM_encoded, IMA_ADPCM_encoded;
- int samplesize;
+ if (format->frequency == 0) {
+ return SDL_SetError("Invalid sample rate");
+ } else if (format->frequency > INT_MAX) {
+ /* Limit given by SDL_AudioSpec.freq. */
+ return SDL_SetError("Sample rate exceeds limit of %d", INT_MAX);
+ }
- /* WAV magic header */
- Uint32 RIFFchunk;
- Uint32 wavelen = 0;
- Uint32 WAVEmagic;
- Uint32 headerDiff = 0;
+ /* Reject invalid fact chunks in strict mode. */
+ if (file->facthint == FactStrict && file->fact.status == -1) {
+ return SDL_SetError("Invalid fact chunk in WAVE file");
+ }
- /* FMT chunk */
- WaveFMT *format = NULL;
- WaveExtensibleFMT *ext = NULL;
+ /* Check the issues common to all encodings. Some unsupported formats set
+ * the bits per sample to zero. These fall through to the 'unsupported
+ * format' error.
+ */
+ switch (format->encoding) {
+ case IEEE_FLOAT_CODE:
+ case ALAW_CODE:
+ case MULAW_CODE:
+ case MS_ADPCM_CODE:
+ case IMA_ADPCM_CODE:
+ /* These formats require a fact chunk. */
+ if (file->facthint == FactStrict && file->fact.status <= 0) {
+ return SDL_SetError("Missing fact chunk in WAVE file");
+ }
+ /* fallthrough */
+ case PCM_CODE:
+ /* All supported formats require a non-zero bit depth. */
+ if (file->chunk.size < 16) {
+ return SDL_SetError("Missing wBitsPerSample field in WAVE fmt chunk");
+ } else if (format->bitspersample == 0) {
+ return SDL_SetError("Invalid bits per sample");
+ }
- SDL_zero(chunk);
+ /* All supported formats must have a proper block size. */
+ if (format->blockalign == 0) {
+ return SDL_SetError("Invalid block alignment");
+ }
- /* Make sure we are passed a valid data source */
- was_error = 0;
- if (src == NULL) {
- was_error = 1;
- goto done;
+ /* If the fact chunk is valid and the appropriate hint is set, the
+ * decoders will use the number of sample frames from the fact chunk.
+ */
+ if (file->fact.status == 1) {
+ WaveFactChunkHint hint = file->facthint;
+ Uint32 samples = file->fact.samplelength;
+ if (hint == FactTruncate || hint == FactStrict || (hint == FactIgnoreZero && samples > 0)) {
+ file->fact.status = 2;
+ }
+ }
}
- /* Check the magic header */
- RIFFchunk = SDL_ReadLE32(src);
- wavelen = SDL_ReadLE32(src);
- if (wavelen == WAVE) { /* The RIFFchunk has already been read */
- WAVEmagic = wavelen;
- wavelen = RIFFchunk;
- RIFFchunk = RIFF;
- } else {
- WAVEmagic = SDL_ReadLE32(src);
- }
- if ((RIFFchunk != RIFF) || (WAVEmagic != WAVE)) {
- SDL_SetError("Unrecognized file type (not WAVE)");
- was_error = 1;
- goto done;
- }
- headerDiff += sizeof(Uint32); /* for WAVE */
-
- /* Read the audio data format chunk */
- chunk.data = NULL;
- do {
- SDL_free(chunk.data);
- chunk.data = NULL;
- lenread = ReadChunk(src, &chunk);
- if (lenread < 0) {
- was_error = 1;
- goto done;
- }
- /* 2 Uint32's for chunk header+len, plus the lenread */
- headerDiff += lenread + 2 * sizeof(Uint32);
- } while ((chunk.magic == FACT) || (chunk.magic == LIST) || (chunk.magic == BEXT) || (chunk.magic == JUNK));
-
- /* Decode the audio data format */
- format = (WaveFMT *) chunk.data;
- if (chunk.magic != FMT) {
- SDL_SetError("Complex WAVE files not supported");
- was_error = 1;
- goto done;
- }
- IEEE_float_encoded = MS_ADPCM_encoded = IMA_ADPCM_encoded = 0;
- switch (SDL_SwapLE16(format->encoding)) {
+ /* Check the format for encoding specific issues and initialize decoders. */
+ switch (format->encoding) {
case PCM_CODE:
- /* We can understand this */
- break;
case IEEE_FLOAT_CODE:
- IEEE_float_encoded = 1;
- /* We can understand this */
+ if (PCM_Init(file, datalength) < 0) {
+ return -1;
+ }
+ break;
+ case ALAW_CODE:
+ case MULAW_CODE:
+ if (LAW_Init(file, datalength) < 0) {
+ return -1;
+ }
break;
case MS_ADPCM_CODE:
- /* Try to understand this */
- if (InitMS_ADPCM(format) < 0) {
- was_error = 1;
- goto done;
+ if (MS_ADPCM_Init(file, datalength) < 0) {
+ return -1;
}
- MS_ADPCM_encoded = 1;
break;
case IMA_ADPCM_CODE:
- /* Try to understand this */
- if (InitIMA_ADPCM(format, lenread) < 0) {
- was_error = 1;
- goto done;
+ if (IMA_ADPCM_Init(file, datalength) < 0) {
+ return -1;
}
- IMA_ADPCM_encoded = 1;
break;
- case EXTENSIBLE_CODE:
- /* note that this ignores channel masks, smaller valid bit counts
- inside a larger container, and most subtypes. This is just enough
- to get things that didn't really _need_ WAVE_FORMAT_EXTENSIBLE
- to be useful working when they use this format flag. */
- ext = (WaveExtensibleFMT *) format;
- if (SDL_SwapLE16(ext->size) < 22) {
- SDL_SetError("bogus extended .wav header");
- was_error = 1;
- goto done;
- }
- if (SDL_memcmp(ext->subformat, extensible_pcm_guid, 16) == 0) {
- break; /* cool. */
- } else if (SDL_memcmp(ext->subformat, extensible_ieee_guid, 16) == 0) {
- IEEE_float_encoded = 1;
- break;
+ case MPEG_CODE:
+ case MPEGLAYER3_CODE:
+ return SDL_SetError("MPEG formats not supported");
+ default:
+ if (format->formattag == EXTENSIBLE_CODE) {
+ const char *errstr = "Unknown WAVE format GUID: %08x-%04x-%04x-%02x%02x%02x%02x%02x%02x%02x%02x";
+ const Uint8 *g = format->subformat;
+ const Uint32 g1 = g[0] | ((Uint32)g[1] << 8) | ((Uint32)g[2] << 16) | ((Uint32)g[3] << 24);
+ const Uint32 g2 = g[4] | ((Uint32)g[5] << 8);
+ const Uint32 g3 = g[6] | ((Uint32)g[7] << 8);
+ return SDL_SetError(errstr, g1, g2, g3, g[8], g[9], g[10], g[11], g[12], g[13], g[14], g[15]);
+ }
+ return SDL_SetError("Unknown WAVE format tag: 0x%04x", (int)format->encoding);
+ }
+
+ return 0;
+}
+
+static int
+WaveLoad(SDL_RWops *src, WaveFile *file, SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len)
+{
+ int result;
+ Uint32 chunkcount = 0;
+ Uint32 chunkcountlimit = 10000;
+ char *envchunkcountlimit;
+ Sint64 RIFFstart, RIFFend, lastchunkpos;
+ SDL_bool RIFFlengthknown = SDL_FALSE;
+ WaveFormat *format = &file->format;
+ WaveChunk *chunk = &file->chunk;
+ WaveChunk RIFFchunk = {0};
+ WaveChunk fmtchunk = {0};
+ WaveChunk datachunk = {0};
+
+ envchunkcountlimit = SDL_getenv("SDL_WAVE_CHUNK_LIMIT");
+ if (envchunkcountlimit != NULL) {
+ unsigned int count;
+ if (SDL_sscanf(envchunkcountlimit, "%u", &count) == 1) {
+ chunkcountlimit = count <= SDL_MAX_UINT32 ? count : SDL_MAX_UINT32;
+ }
+ }
+
+ RIFFstart = SDL_RWtell(src);
+ if (RIFFstart < 0) {
+ return SDL_SetError("Could not seek in file");
+ }
+
+ RIFFchunk.position = RIFFstart;
+ if (WaveNextChunk(src, &RIFFchunk) < 0) {
+ return SDL_SetError("Could not read RIFF header");
+ }
+
+ /* Check main WAVE file identifiers. */
+ if (RIFFchunk.fourcc == RIFF) {
+ Uint32 formtype;
+ /* Read the form type. "WAVE" expected. */
+ if (SDL_RWread(src, &formtype, sizeof(Uint32), 1) != 1) {
+ return SDL_SetError("Could not read RIFF form type");
+ } else if (SDL_SwapLE32(formtype) != WAVE) {
+ return SDL_SetError("RIFF form type is not WAVE (not a Waveform file)");
}
+ } else if (RIFFchunk.fourcc == WAVE) {
+ /* RIFF chunk missing or skipped. Length unknown. */
+ RIFFchunk.position = 0;
+ RIFFchunk.length = 0;
+ } else {
+ return SDL_SetError("Could not find RIFF or WAVE identifiers (not a Waveform file)");
+ }
+
+ /* The 4-byte form type is immediately followed by the first chunk.*/
+ chunk->position = RIFFchunk.position + 4;
+
+ /* Use the RIFF chunk size to limit the search for the chunks. This is not
+ * always reliable and the hint can be used to tune the behavior. By
+ * default, it will never search past 4 GiB.
+ */
+ switch (file->riffhint) {
+ case RiffSizeIgnore:
+ RIFFend = RIFFchunk.position + SDL_MAX_UINT32;
break;
- case MP3_CODE:
- SDL_SetError("MPEG Layer 3 data not supported");
- was_error = 1;
- goto done;
default:
- SDL_SetError("Unknown WAVE data format: 0x%.4x",
- SDL_SwapLE16(format->encoding));
- was_error = 1;
- goto done;
+ case RiffSizeIgnoreZero:
+ if (RIFFchunk.length == 0) {
+ RIFFend = RIFFchunk.position + SDL_MAX_UINT32;
+ break;
+ }
+ /* fallthrough */
+ case RiffSizeChunkSearch:
+ RIFFend = RIFFchunk.position + RIFFchunk.length;
+ RIFFlengthknown = SDL_TRUE;
+ break;
+ case RiffSizeMaximum:
+ RIFFend = SDL_MAX_SINT64;
+ break;
}
- SDL_zerop(spec);
- spec->freq = SDL_SwapLE32(format->frequency);
- if (IEEE_float_encoded) {
- if ((SDL_SwapLE16(format->bitspersample)) != 32) {
- was_error = 1;
- } else {
- spec->format = AUDIO_F32;
+ /* Step through all chunks and save information on the fmt, data, and fact
+ * chunks. Ignore the chunks we don't know as per specification. This
+ * currently also ignores cue, list, and slnt chunks.
+ */
+ while (RIFFend > chunk->position + chunk->length + (chunk->length & 1)) {
+ /* Abort after too many chunks or else corrupt files may waste time. */
+ if (chunkcount++ >= chunkcountlimit) {
+ return SDL_SetError("Chunk count in WAVE file exceeds limit of %u", chunkcountlimit);
}
- } else {
- switch (SDL_SwapLE16(format->bitspersample)) {
- case 4:
- if (MS_ADPCM_encoded || IMA_ADPCM_encoded) {
- spec->format = AUDIO_S16;
- } else {
- was_error = 1;
+
+ result = WaveNextChunk(src, chunk);
+ if (result == -1) {
+ /* Unexpected EOF. Corrupt file or I/O issues. */
+ if (file->trunchint == TruncVeryStrict) {
+ return SDL_SetError("Unexpected end of WAVE file");
}
+ /* Let the checks after this loop sort this issue out. */
break;
+ } else if (result == -2) {
+ return SDL_SetError("Could not seek to WAVE chunk header");
+ }
+
+ if (chunk->fourcc == FMT) {
+ if (fmtchunk.fourcc == FMT) {
+ /* Multiple fmt chunks. Ignore or error? */
+ } else {
+ /* The fmt chunk must occur before the data chunk. */
+ if (datachunk.fourcc == DATA) {
+ return SDL_SetError("fmt chunk after data chunk in WAVE file");
+ }
+ fmtchunk = *chunk;
+ }
+ } else if (chunk->fourcc == DATA) {
+ /* Only use the first data chunk. Handling the wavl list madness
+ * may require a different approach.
+ */
+ if (datachunk.fourcc != DATA) {
+ datachunk = *chunk;
+ }
+ } else if (chunk->fourcc == FACT) {
+ /* The fact chunk data must be at least 4 bytes for the
+ * dwSampleLength field. Ignore all fact chunks after the first one.
+ */
+ if (file->fact.status == 0) {
+ if (chunk->length < 4) {
+ file->fact.status = -1;
+ } else {
+ /* Let's use src directly, it's just too convenient. */
+ Sint64 position = SDL_RWseek(src, chunk->position, RW_SEEK_SET);
+ Uint32 samplelength;
+ if (position == chunk->position && SDL_RWread(src, &samplelength, sizeof(Uint32), 1) == 1) {
+ file->fact.status = 1;
+ file->fact.samplelength = SDL_SwapLE32(samplelength);
+ } else {
+ file->fact.status = -1;
+ }
+ }
+ }
+ }
+
+ /* Go through all chunks in verystrict mode or stop the search early if
+ * all required chunks were found.
+ */
+ if (file->trunchint == TruncVeryStrict) {
+ if (RIFFend < chunk->position + chunk->length) {
+ return SDL_SetError("RIFF size truncates chunk");
+ }
+ } else if (fmtchunk.fourcc == FMT && datachunk.fourcc == DATA) {
+ if (file->fact.status == 1 || file->facthint == FactIgnore || file->facthint == FactNoHint) {
+ break;
+ }
+ }
+ }
+
+ /* Save the position after the last chunk. This position will be used if the
+ * RIFF length is unknown.
+ */
+ lastchunkpos = chunk->position + chunk->length;
+
+ /* The fmt chunk is mandatory. */
+ if (fmtchunk.fourcc != FMT) {
+ return SDL_SetError("Missing fmt chunk in WAVE file");
+ }
+ /* A data chunk must be present. */
+ if (datachunk.fourcc != DATA) {
+ return SDL_SetError("Missing data chunk in WAVE file");
+ }
+ /* Check if the last chunk has all of its data in verystrict mode. */
+ if (file->trunchint == TruncVeryStrict) {
+ /* data chunk is handled later. */
+ if (chunk->fourcc != DATA && chunk->length > 0) {
+ Uint8 tmp;
+ Sint64 position = chunk->position + chunk->length - 1;
+ if (SDL_RWseek(src, position, RW_SEEK_SET) != position) {
+ return SDL_SetError("Could not seek to WAVE chunk data");
+ } else if (SDL_RWread(src, &tmp, 1, 1) != 1) {
+ return SDL_SetError("RIFF size truncates chunk");
+ }
+ }
+ }
+
+ /* Process fmt chunk. */
+ *chunk = fmtchunk;
+
+ /* No need to read more than 1046 bytes of the fmt chunk data with the
+ * formats that are currently supported. (1046 because of MS ADPCM coefficients)
+ */
+ if (WaveReadPartialChunkData(src, chunk, 1046) < 0) {
+ return SDL_SetError("Could not read data of WAVE fmt chunk");
+ }
+
+ /* The fmt chunk data must be at least 14 bytes to include all common fields.
+ * It usually is 16 and larger depending on the header and encoding.
+ */
+ if (chunk->length < 14) {
+ return SDL_SetError("Invalid WAVE fmt chunk length (too small)");
+ } else if (chunk->size < 14) {
+ return SDL_SetError("Could not read data of WAVE fmt chunk");
+ } else if (WaveReadFormat(file) < 0) {
+ return -1;
+ } else if (WaveCheckFormat(file, (size_t)datachunk.length) < 0) {
+ return -1;
+ }
+
+#ifdef SDL_WAVE_DEBUG_LOG_FORMAT
+ WaveDebugLogFormat(file);
+#endif
+#ifdef SDL_WAVE_DEBUG_DUMP_FORMAT
+ WaveDebugDumpFormat(file, RIFFchunk.length, fmtchunk.length, datachunk.length);
+#endif
+
+ WaveFreeChunkData(chunk);
+
+ /* Process data chunk. */
+ *chunk = datachunk;
+
+ if (chunk->length > 0) {
+ result = WaveReadChunkData(src, chunk);
+ if (result == -1) {
+ return -1;
+ } else if (result == -2) {
+ return SDL_SetError("Could not seek data of WAVE data chunk");
+ }
+ }
+
+ if (chunk->length != chunk->size) {
+ /* I/O issues or corrupt file. */
+ if (file->trunchint == TruncVeryStrict || file->trunchint == TruncStrict) {
+ return SDL_SetError("Could not read data of WAVE data chunk");
+ }
+ /* The decoders handle this truncation. */
+ }
+
+ /* Decode or convert the data if necessary. */
+ switch (format->encoding) {
+ case PCM_CODE:
+ case IEEE_FLOAT_CODE:
+ if (PCM_Decode(file, audio_buf, audio_len) < 0) {
+ return -1;
+ }
+ break;
+ case ALAW_CODE:
+ case MULAW_CODE:
+ if (LAW_Decode(file, audio_buf, audio_len) < 0) {
+ return -1;
+ }
+ break;
+ case MS_ADPCM_CODE:
+ if (MS_ADPCM_Decode(file, audio_buf, audio_len) < 0) {
+ return -1;
+ }
+ break;
+ case IMA_ADPCM_CODE:
+ if (IMA_ADPCM_Decode(file, audio_buf, audio_len) < 0) {
+ return -1;
+ }
+ break;
+ }
+
+ /* Setting up the SDL_AudioSpec. All unsupported formats were filtered out
+ * by checks earlier in this function.
+ */
+ SDL_zerop(spec);
+ spec->freq = format->frequency;
+ spec->channels = (Uint8)format->channels;
+ spec->samples = 4096; /* Good default buffer size */
+
+ switch (format->encoding) {
+ case MS_ADPCM_CODE:
+ case IMA_ADPCM_CODE:
+ case ALAW_CODE:
+ case MULAW_CODE:
+ /* These can be easily stored in the byte order of the system. */
+ spec->format = AUDIO_S16SYS;
+ break;
+ case IEEE_FLOAT_CODE:
+ spec->format = AUDIO_F32LSB;
+ break;
+ case PCM_CODE:
+ switch (format->bitspersample) {
case 8:
spec->format = AUDIO_U8;
break;
case 16:
- spec->format = AUDIO_S16;
- break;
- case 24: /* convert this. */
- spec->format = AUDIO_S32;
+ spec->format = AUDIO_S16LSB;
break;
+ case 24: /* Has been shifted to 32 bits. */
case 32:
- spec->format = AUDIO_S32;
+ spec->format = AUDIO_S32LSB;
break;
default:
- was_error = 1;
- break;
+ /* Just in case something unexpected happened in the checks. */
+ return SDL_SetError("Unexpected %d-bit PCM data format", format->bitspersample);
}
+ break;
}
- if (was_error) {
- SDL_SetError("Unknown %d-bit PCM data format",
- SDL_SwapLE16(format->bitspersample));
- goto done;
+ /* Report the end position back to the cleanup code. */
+ if (RIFFlengthknown) {
+ chunk->position = RIFFend;
+ } else {
+ chunk->position = lastchunkpos;
}
- spec->channels = (Uint8) SDL_SwapLE16(format->channels);
- spec->samples = 4096; /* Good default buffer size */
- /* Read the audio data chunk */
- *audio_buf = NULL;
- do {
- SDL_free(*audio_buf);
- *audio_buf = NULL;
- lenread = ReadChunk(src, &chunk);
- if (lenread < 0) {
- was_error = 1;
- goto done;
- }
- *audio_len = lenread;
- *audio_buf = chunk.data;
- if (chunk.magic != DATA)
- headerDiff += lenread + 2 * sizeof(Uint32);
- } while (chunk.magic != DATA);
- headerDiff += 2 * sizeof(Uint32); /* for the data chunk and len */
+ return 0;
+}
- if (MS_ADPCM_encoded) {
- if (MS_ADPCM_decode(audio_buf, audio_len) < 0) {
- was_error = 1;
- goto done;
- }
- }
- if (IMA_ADPCM_encoded) {
- if (IMA_ADPCM_decode(audio_buf, audio_len) < 0) {
- was_error = 1;
- goto done;
- }
- }
+SDL_AudioSpec *
+SDL_LoadWAV_RW(SDL_RWops *src, int freesrc, SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len)
+{
+ int result;
+ WaveFile file = {0};
- if (SDL_SwapLE16(format->bitspersample) == 24) {
- if (ConvertSint24ToSint32(audio_buf, audio_len) < 0) {
- was_error = 1;
- goto done;
- }
+ /* Make sure we are passed a valid data source */
+ if (src == NULL) {
+ /* Error may come from RWops. */
+ return NULL;
+ } else if (spec == NULL) {
+ SDL_InvalidParamError("spec");
+ return NULL;
+ } else if (audio_buf == NULL) {
+ SDL_InvalidParamError("audio_buf");
+ return NULL;
+ } else if (audio_len == NULL) {
+ SDL_InvalidParamError("audio_len");
+ return NULL;
}
- /* Don't return a buffer that isn't a multiple of samplesize */
- samplesize = ((SDL_AUDIO_BITSIZE(spec->format)) / 8) * spec->channels;
- *audio_len &= ~(samplesize - 1);
+ *audio_buf = NULL;
+ *audio_len = 0;
- done:
- SDL_free(format);
- if (src) {
- if (freesrc) {
- SDL_RWclose(src);
- } else {
- /* seek to the end of the file (given by the RIFF chunk) */
- SDL_RWseek(src, wavelen - chunk.length - headerDiff, RW_SEEK_CUR);
- }
- }
- if (was_error) {
+ file.riffhint = WaveGetRiffSizeHint();
+ file.trunchint = WaveGetTruncationHint();
+ file.facthint = WaveGetFactChunkHint();
+
+ result = WaveLoad(src, &file, spec, audio_buf, audio_len);
+ if (result < 0) {
+ SDL_free(*audio_buf);
spec = NULL;
+ audio_buf = NULL;
+ audio_len = 0;
+ }
+
+ /* Cleanup */
+ if (freesrc) {
+ SDL_RWclose(src);
+ } else {
+ SDL_RWseek(src, file.chunk.position, RW_SEEK_SET);
}
- return (spec);
+ WaveFreeChunkData(&file.chunk);
+ SDL_free(file.decoderdata);
+
+ return spec;
}
/* Since the WAV memory is allocated in the shared library, it must also
be freed here. (Necessary under Win32, VC++)
*/
void
-SDL_FreeWAV(Uint8 * audio_buf)
+SDL_FreeWAV(Uint8 *audio_buf)
{
SDL_free(audio_buf);
}
-static int
-ReadChunk(SDL_RWops * src, Chunk * chunk)
-{
- chunk->magic = SDL_ReadLE32(src);
- chunk->length = SDL_ReadLE32(src);
- chunk->data = (Uint8 *) SDL_malloc(chunk->length);
- if (chunk->data == NULL) {
- return SDL_OutOfMemory();
- }
- if (SDL_RWread(src, chunk->data, chunk->length, 1) != 1) {
- SDL_free(chunk->data);
- chunk->data = NULL;
- return SDL_Error(SDL_EFREAD);
- }
- return (chunk->length);
-}
-
/* vi: set ts=4 sw=4 expandtab: */
diff --git a/src/audio/SDL_wave.h b/src/audio/SDL_wave.h
index c739c4b..3d0ae55 100644
--- a/src/audio/SDL_wave.h
+++ b/src/audio/SDL_wave.h
@@ -20,11 +20,12 @@
*/
#include "../SDL_internal.h"
-/* WAVE files are little-endian */
+/* RIFF WAVE files are little-endian */
/*******************************************/
/* Define values for Microsoft WAVE format */
/*******************************************/
+/* FOURCC */
#define RIFF 0x46464952 /* "RIFF" */
#define WAVE 0x45564157 /* "WAVE" */
#define FACT 0x74636166 /* "fact" */
@@ -33,45 +34,116 @@
#define JUNK 0x4B4E554A /* "JUNK" */
#define FMT 0x20746D66 /* "fmt " */
#define DATA 0x61746164 /* "data" */
+/* Format tags */
+#define UNKNOWN_CODE 0x0000
#define PCM_CODE 0x0001
#define MS_ADPCM_CODE 0x0002
#define IEEE_FLOAT_CODE 0x0003
+#define ALAW_CODE 0x0006
+#define MULAW_CODE 0x0007
#define IMA_ADPCM_CODE 0x0011
-#define MP3_CODE 0x0055
+#define MPEG_CODE 0x0050
+#define MPEGLAYER3_CODE 0x0055
#define EXTENSIBLE_CODE 0xFFFE
-#define WAVE_MONO 1
-#define WAVE_STEREO 2
-/* Normally, these three chunks come consecutively in a WAVE file */
-typedef struct WaveFMT
+/* Stores the WAVE format information. */
+typedef struct WaveFormat
{
-/* Not saved in the chunk we read:
- Uint32 FMTchunk;
- Uint32 fmtlen;
-*/
- Uint16 encoding;
- Uint16 channels; /* 1 = mono, 2 = stereo */
- Uint32 frequency; /* One of 11025, 22050, or 44100 Hz */
- Uint32 byterate; /* Average bytes per second */
- Uint16 blockalign; /* Bytes per sample block */
- Uint16 bitspersample; /* One of 8, 12, 16, or 4 for ADPCM */
-} WaveFMT;
-
-/* The general chunk found in the WAVE file */
-typedef struct Chunk
+ Uint16 formattag; /* Raw value of the first field in the fmt chunk data. */
+ Uint16 encoding; /* Actual encoding, possibly from the extensible header. */
+ Uint16 channels; /* Number of channels. */
+ Uint32 frequency; /* Sampling rate in Hz. */
+ Uint32 byterate; /* Average bytes per second. */
+ Uint16 blockalign; /* Bytes per block. */
+ Uint16 bitspersample; /* Currently supported are 8, 16, 24, 32, and 4 for ADPCM. */
+
+ /* Extra information size. Number of extra bytes starting at byte 18 in the
+ * fmt chunk data. This is at least 22 for the extensible header.
+ */
+ Uint16 extsize;
+
+ /* Extensible WAVE header fields */
+ Uint16 validsamplebits;
+ Uint32 samplesperblock; /* For compressed formats. Can be zero. Actually 16 bits in the header. */
+ Uint32 channelmask;
+ Uint8 subformat[16]; /* A format GUID. */
+} WaveFormat;
+
+/* Stores information on the fact chunk. */
+typedef struct WaveFact {
+ /* Represents the state of the fact chunk in the WAVE file.
+ * Set to -1 if the fact chunk is invalid.
+ * Set to 0 if the fact chunk is not present
+ * Set to 1 if the fact chunk is present and valid.
+ * Set to 2 if samplelength is going to be used as the number of sample frames.
+ */
+ Sint32 status;
+
+ /* Version 1 of the RIFF specification calls the field in the fact chunk
+ * dwFileSize. The Standards Update then calls it dwSampleLength and specifies
+ * that it is 'the length of the data in samples'. WAVE files from Windows
+ * with this chunk have it set to the samples per channel (sample frames).
+ * This is useful to truncate compressed audio to a specific sample count
+ * because a compressed block is usually decoded to a fixed number of
+ * sample frames.
+ */
+ Uint32 samplelength; /* Raw sample length value from the fact chunk. */
+} WaveFact;
+
+/* Generic struct for the chunks in the WAVE file. */
+typedef struct WaveChunk
{
- Uint32 magic;
- Uint32 length;
- Uint8 *data;
-} Chunk;
+ Uint32 fourcc; /* FOURCC of the chunk. */
+ Uint32 length; /* Size of the chunk data. */
+ Sint64 position; /* Position of the data in the stream. */
+ Uint8 *data; /* When allocated, this points to the chunk data. length is used for the malloc size. */
+ size_t size; /* Number of bytes in data that could be read from the stream. Can be smaller than length. */
+} WaveChunk;
-typedef struct WaveExtensibleFMT
+/* Controls how the size of the RIFF chunk affects the loading of a WAVE file. */
+typedef enum WaveRiffSizeHint {
+ RiffSizeNoHint,
+ RiffSizeChunkSearch,
+ RiffSizeIgnoreZero,
+ RiffSizeIgnore,
+ RiffSizeMaximum,
+} WaveRiffSizeHint;
+
+/* Controls how a truncated WAVE file is handled. */
+typedef enum WaveTruncationHint {
+ TruncNoHint,
+ TruncVeryStrict,
+ TruncStrict,
+ TruncDropFrame,
+ TruncDropBlock,
+} WaveTruncationHint;
+
+/* Controls how the fact chunk affects the loading of a WAVE file. */
+typedef enum WaveFactChunkHint {
+ FactNoHint,
+ FactTruncate,
+ FactStrict,
+ FactIgnoreZero,
+ FactIgnore,
+} WaveFactChunkHint;
+
+typedef struct WaveFile
{
- WaveFMT format;
- Uint16 size;
- Uint16 validbits;
- Uint32 channelmask;
- Uint8 subformat[16]; /* a GUID. */
-} WaveExtensibleFMT;
+ WaveChunk chunk;
+ WaveFormat format;
+ WaveFact fact;
+
+ /* Number of sample frames that will be decoded. Calculated either with the
+ * size of the data chunk or, if the appropriate hint is enabled, with the
+ * sample length value from the fact chunk.
+ */
+ Sint64 sampleframes;
+
+ void *decoderdata; /* Some decoders require extra data for a state. */
+
+ WaveRiffSizeHint riffhint;
+ WaveTruncationHint trunchint;
+ WaveFactChunkHint facthint;
+} WaveFile;
/* vi: set ts=4 sw=4 expandtab: */