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 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719
|
/**********************************************************
* libmp3splt -- library based on mp3splt,
* for mp3/ogg splitting without decoding
*
* Copyright (c) 2002-2005 M. Trotta - <mtrotta@users.sourceforge.net>
* Copyright (c) 2005-2009 Alexandru Munteanu - io_fx@yahoo.fr
*
*********************************************************/
/**********************************************************
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307,
* USA.
*********************************************************/
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <dirent.h>
#include <math.h>
#include <ctype.h>
#ifdef __WIN32__
#include <io.h>
#include <fcntl.h>
#endif
#include "mp3.h"
/****************************/
/* mp3 constants */
static const char *splt_mp3_chan[] =
{
"Mono",
"Dual Mono",
"Joint Stereo",
"Stereo",
"?"
};
//layer, bitrate..
static const int splt_mp3_tabsel_123[2][3][16] = {
{ {128,32,64,96,128,160,192,224,256,288,320,352,384,416,448,},
{128,32,48,56, 64, 80, 96,112,128,160,192,224,256,320,384,},
{128,32,40,48, 56, 64, 80, 96,112,128,160,192,224,256,320,} },
{ {128,32,48,56,64,80,96,112,128,144,160,176,192,224,256,},
{128,8,16,24,32,40,48,56,64,80,96,112,128,144,160,},
{128,8,16,24,32,40,48,56,64,80,96,112,128,144,160,} }
};
//categories of mp3 songs
static const char splt_mp3_id3v1_categories[SPLT_MP3_GENRENUM][25] = {
{"Blues"}, {"Classic Rock"}, {"Country"}, {"Dance"},
{"Disco"},{"Funk"},{"Grunge"},{"Hip-Hop"},{"Jazz"},
{"Metal"},{"New Age"},{"Oldies"}, {"Other"}, {"Pop"},
{"R&B"}, {"Rap"}, {"Reggae"}, {"Rock"}, {"Techno"},
{"Industrial"}, {"Alternative"}, {"Ska"}, {"Death metal"},
{"Pranks"}, {"Soundtrack"}, {"Euro-Techno"},
{"Ambient"}, {"Trip-hop"}, {"Vocal"}, {"Jazz+Funk"},
{"Fusion"}, {"Trance"}, {"Classical"}, {"Instrumental"},
{"Acid"}, {"House"}, {"Game"}, {"Sound clip"}, {"Gospel"},
{"Noise"}, {"Alt. Rock"}, {"Bass"}, {"Soul"}, {"Punk"},
{"Space"}, {"Meditative"}, {"Instrumental pop"},
{"Instrumental rock"}, {"Ethnic"}, {"Gothic"},{"Darkwave"},
{"Techno-Industrial"},{"Electronic"},{"Pop-Folk"},{"Eurodance"},
{"Dream"},{"Southern Rock"},{"Comedy"}, {"Cult"},{"Gangsta"},
{"Top 40"},{"Christian Rap"},{"Pop/Funk"}, {"Jungle"},
{"Native American"},{"Cabaret"},{"New Wave"}, {"Psychedelic"},
{"Rave"},{"Showtunes"},{"Trailer"}, {"Lo-Fi"},{"Tribal"},
{"Acid Punk"},{"Acid Jazz"}, {"Polka"}, {"Retro"},
{"Musical"},{"Rock & Roll"},{"Hard Rock"}, {"misc"}, {"misc"},
};
static const char unsigned splt_mp3_id3genre[SPLT_MP3_GENRENUM] =
{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F,
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F,
0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F,
0xFF };
static const unsigned long splt_mp3_crctab[256] = {
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba,
0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3,
0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91,
0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de,
0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec,
0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5,
0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940,
0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116,
0x21b4f4b5, 0x56b3c423, 0xcfba9599, 0xb8bda50f,
0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d,
0x76dc4190, 0x01db7106, 0x98d220bc, 0xefd5102a,
0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818,
0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457,
0x65b0d9c6, 0x12b7e950, 0x8bbeb8ea, 0xfcb9887c,
0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2,
0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb,
0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9,
0x5005713c, 0x270241aa, 0xbe0b1010, 0xc90c2086,
0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4,
0x59b33d17, 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad,
0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683,
0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8,
0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe,
0xf762575d, 0x806567cb, 0x196c3671, 0x6e6b06e7,
0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252,
0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60,
0xdf60efc3, 0xa867df55, 0x316e8eef, 0x4669be79,
0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f,
0xc5ba3bbe, 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04,
0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a,
0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21,
0x86d3d2d4, 0xf1d4e242, 0x68ddb3f8, 0x1fda836e,
0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c,
0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45,
0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db,
0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0,
0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6,
0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf,
0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d,
};
static void splt_mp3_save_end_point(splt_state *state, splt_mp3_state *mp3state,
int save_end_point, off_t end)
{
if (save_end_point)
{
mp3state->end = end;
}
else
{
mp3state->end = 0;
}
}
//-filename must not be null; if filename is NULL, then this plugin should
//not have been detected
//-returns NULL if error
static FILE *splt_mp3_open_file_read(splt_state *state, const char *filename,
int *error)
{
FILE *file_input = NULL;
if (filename != NULL && ((strcmp(filename,"-") == 0) ||
(strcmp(filename,"m-") == 0)))
{
file_input = stdin;
#ifdef __WIN32__
_setmode(fileno(file_input), _O_BINARY);
#endif
}
else
{
//we open the file
file_input = splt_u_fopen(filename, "rb");
if (file_input == NULL)
{
splt_t_set_strerror_msg(state);
splt_t_set_error_data(state,filename);
*error = SPLT_ERROR_CANNOT_OPEN_FILE;
}
}
return file_input;
}
/****************************/
/* CRC functions */
static unsigned long splt_mp3_c_crc(splt_state *state,
FILE *in, off_t begin, off_t end, int *error)
{
register unsigned long crc;
int c;
crc = 0xFFFFFFFF;
if (fseeko(in, begin, SEEK_SET) == -1)
{
splt_t_set_strerror_msg(state);
splt_t_set_error_data(state,splt_t_get_filename_to_split(state));
*error = SPLT_ERROR_SEEKING_FILE;
return 0;
}
while(begin++ < end)
{
c = fgetc(in);
crc = ((crc >> 8) & 0x00FFFFFF) ^ splt_mp3_crctab[(crc ^ c) & 0xFF];
}
return (crc ^ 0xFFFFFFFF);
}
/****************************/
/* mp3 utils */
static void splt_mp3_init_stream_frame(splt_mp3_state *mp3state)
{
mad_stream_init(&mp3state->stream);
mad_frame_init(&mp3state->frame);
}
static void splt_mp3_finish_stream_frame(splt_mp3_state *mp3state)
{
mad_stream_finish(&mp3state->stream);
mad_frame_finish(&mp3state->frame);
}
//does nothing important for libmp3splt
//review this..
static void splt_mp3_checksync (splt_mp3_state *mp3state)
{
//char junk[32];
//fprintf(stderr, "\nWarning: Too many sync errors! This may not be a mp3 file. Continue? (y/n) ");
//fgets(junk, 31, stdin);
//if (junk[0]=='y')
//we don't use user interactivity in a library
//always continue
mp3state->syncdetect = 0;
//else error("Aborted.",125);
}
//calculates bitrate
static int splt_mp3_c_bitrate (unsigned long head)
{
if ((head & 0xffe00000) != 0xffe00000) return 0;
if (!((head>>17)&3)) return 0;
if (((head>>12)&0xf) == 0xf) return 0;
if (!((head >> 12) & 0xf)) return 0;
if (((head>>10)&0x3) == 0x3 ) return 0;
if (((head >> 19) & 1)==1 && ((head>>17)&3)==3 &&
((head>>16)&1)==1) return 0;
if ((head & 0xffff0000) == 0xfffe0000) return 0;
return ((head>>12)&0xf);
}
//make mp3 header bitrate, padding, offset, framesize
static struct splt_header splt_mp3_makehead (unsigned long headword,
struct splt_mp3 mp3f, struct splt_header head, off_t ptr)
{
head.ptr = ptr;
head.bitrate = splt_mp3_tabsel_123[1 - mp3f.mpgid][mp3f.layer-1][splt_mp3_c_bitrate(headword)];
head.padding = ((headword>>9)&0x1);
head.framesize = (head.bitrate*144000)/
(mp3f.freq<<(1 - mp3f.mpgid)) + head.padding;
return head;
}
//finds first header from start_pos. Returns -1 if no header is found
static off_t splt_mp3_findhead (splt_mp3_state *mp3state, off_t start)
{
if (splt_u_getword(mp3state->file_input,
start, SEEK_SET, &mp3state->headw) == -1)
{
return -1;
}
if (feof(mp3state->file_input))
{
return -1;
}
while (!(splt_mp3_c_bitrate(mp3state->headw)))
{
if (feof(mp3state->file_input))
{
return -1;
}
mp3state->headw <<= 8;
mp3state->headw |= fgetc(mp3state->file_input);
start++;
}
return start;
}
// Finds first valid header from start. Will work with high probabilty, i hope :)
static off_t splt_mp3_findvalidhead (splt_mp3_state *mp3state, off_t start)
{
off_t begin;
struct splt_header h;
begin = splt_mp3_findhead(mp3state, start);
do {
start = begin;
if (start == -1)
{
break;
}
h = splt_mp3_makehead (mp3state->headw, mp3state->mp3file, h, start);
begin = splt_mp3_findhead(mp3state, (start + 1));
} while (begin!=(start + h.framesize));
return start;
}
//finds xing info offset and returns it?
static int splt_mp3_xing_info_off(splt_mp3_state *mp3state)
{
unsigned long headw = 0;
int i;
for (i=0; i<mp3state->mp3file.xing; i++)
{
if ((headw == SPLT_MP3_XING_MAGIC) ||
(headw == SPLT_MP3_INFO_MAGIC)) // "Xing" or "Info"
{
return i;
}
headw <<= 8;
headw |= mp3state->mp3file.xingbuffer[i];
}
return 0;
}
//get a frame
//-returns a negative value if error
static int splt_mp3_get_frame(splt_mp3_state *mp3state)
{
if(mp3state->stream.buffer==NULL ||
mp3state->stream.error==MAD_ERROR_BUFLEN)
{
size_t readSize, remaining;
unsigned char *readStart;
if (feof(mp3state->file_input))
{
return -2;
}
if(mp3state->stream.next_frame!=NULL)
{
remaining = mp3state->stream.bufend - mp3state->stream.next_frame;
memmove(mp3state->inputBuffer, mp3state->stream.next_frame, remaining);
readStart = mp3state->inputBuffer + remaining;
readSize = SPLT_MAD_BSIZE - remaining;
}
else
{
readSize = SPLT_MAD_BSIZE;
readStart=mp3state->inputBuffer;
remaining=0;
}
readSize=fread(readStart, 1, readSize, mp3state->file_input);
if (readSize <= 0)
{
return -2;
}
mp3state->buf_len = readSize + remaining;
mp3state->bytes += readSize;
//does not set any error
mad_stream_buffer(&mp3state->stream, mp3state->inputBuffer,
readSize+remaining);
mp3state->stream.error = MAD_ERROR_NONE;
}
//mad_frame_decode() returns -1 if error, 0 if no error
return mad_frame_decode(&mp3state->frame,&mp3state->stream);
}
//used by mp3split and mp3_scan_silence
//gets a frame and checks for its validity
//returns 1 if ok, -1 if end of file, 0 if nothing ?
//and -3 if other error; the error will be set in the '*error' parameter
//sets the mp3state->data_ptr the pointer to the frame
//and the mp3state->data_len the length of the frame
static int splt_mp3_get_valid_frame(splt_state *state, int *error)
{
splt_mp3_state *mp3state = state->codec;
int ok = 0;
do
{
int ret = splt_mp3_get_frame(mp3state);
if(ret != 0)
{
if (ret == -2)
{
return -1;
}
if (mp3state->stream.error == MAD_ERROR_LOSTSYNC)
{
//syncerrors
state->syncerrors++;
if ((mp3state->syncdetect)&&
(state->syncerrors>SPLT_MAXSYNC))
{
splt_mp3_checksync(mp3state);
}
}
if(MAD_RECOVERABLE(mp3state->stream.error))
{
continue;
}
else
{
if(mp3state->stream.error==MAD_ERROR_BUFLEN)
{
continue;
}
else
{
splt_t_set_error_data(state, mad_stream_errorstr(&mp3state->stream));
*error = SPLT_ERROR_PLUGIN_ERROR;
return -3;
}
}
}
else
{
//the important stuff
mp3state->data_ptr = (unsigned char *) mp3state->stream.this_frame;
if (mp3state->stream.next_frame!=NULL)
{
mp3state->data_len = (long) (mp3state->stream.next_frame - mp3state->stream.this_frame);
}
ok = 1;
}
} while (!ok);
return ok;
}
//search for ID3 v1 tag, "TAG" sequence
//if found returns offset of mp3 data
//-else returns 0
//-we don't check fseeko error
static int splt_mp3_getid3v1_offset(FILE *file_input)
{
if (fseeko(file_input, (off_t) -128, SEEK_END)==-1)
{
return 0;
}
if (fgetc(file_input)=='T')
if (fgetc(file_input)=='A')
if (fgetc(file_input)=='G')
return -128;
return 0;
}
//check if there is a ID3v2.
//if found, it returns offset of mp3 data.
//-else returns 0
//-we don't check fseeko error
static off_t splt_mp3_getid3v2_end_offset(FILE *in, off_t start)
{
unsigned long oword = 0;
if (fseeko(in, start, SEEK_SET)==-1)
{
return 0;
}
if (fgetc(in)=='I')
if (fgetc(in)=='D')
if (fgetc(in)=='3')
{
int i;
if (fseeko(in, (off_t) 3, SEEK_CUR)==-1)
{
return 0;
}
for (i=0; i<4; i++)
{
oword = (oword << 7) | fgetc(in);
}
return (off_t) (oword);
}
return 0;
}
//frees the splt_mp3_state structure,
//used in the splt_t_state_free() function
static void splt_mp3_state_free(splt_state *state)
{
splt_mp3_state *mp3state = state->codec;
if (mp3state)
{
if (mp3state->mp3file.xingbuffer)
{
free(mp3state->mp3file.xingbuffer);
mp3state->mp3file.xingbuffer = NULL;
}
//we free the state
free(mp3state);
state->codec = NULL;
}
}
/****************************/
/* mp3 tags */
//returns the genre of the song, mp3splt used this in cddb search
static unsigned char splt_mp3_getgenre (const char *genre_string)
{
int i;
for (i=0; i< SPLT_MP3_GENRENUM; i++)
{
if (strncmp(genre_string, splt_mp3_id3v1_categories[i],
strlen(genre_string))==0)
{
return splt_mp3_id3genre[i];
}
}
return 0xFF;
}
#ifndef NO_ID3TAG
/* get libid3tag original tags */
//returns the id3v2 tags as bytes from FILE 'file'
static id3_byte_t *splt_mp3_get_id3v2_tag_bytes(FILE *file, id3_length_t *length)
{
id3_byte_t *bytes = NULL;
*length = 0;
off_t id3v2_end_offset = splt_mp3_getid3v2_end_offset(file, 0);
if (id3v2_end_offset != 0)
{
unsigned long id3v2_size = (unsigned long) id3v2_end_offset + 10;
bytes = malloc(sizeof(unsigned char) * id3v2_size);
if (! bytes)
{
return NULL;
}
rewind(file);
//read the whole id3v2 tags
if (fread(bytes, 1, id3v2_size, file) != id3v2_size)
{
if (bytes)
{
free(bytes);
bytes = NULL;
}
return NULL;
}
*length = id3v2_size;
}
return bytes;
}
//returns the id3v1 tags as bytes from FILE 'file'
static id3_byte_t *splt_mp3_get_id3v1_tag_bytes(FILE *file, id3_length_t *length)
{
id3_byte_t *bytes = NULL;
*length = 0;
off_t id3v1_offset = splt_mp3_getid3v1_offset(file);
if (id3v1_offset != 0)
{
if (fseeko(file, id3v1_offset, SEEK_END) !=-1)
{
bytes = malloc(sizeof(unsigned char) * 128);
if (! bytes)
{
return NULL;
}
if (fread(bytes, 1 , 128, file) != 128)
{
if (bytes)
{
free(bytes);
bytes = NULL;
return NULL;
}
}
else
{
*length = (unsigned long) 128;
}
}
}
return bytes;
}
//returns the tag bytes from file 'filename' and sets the 'tags_version' to
//the version of the tags
static id3_byte_t *splt_mp3_get_id3_tag_bytes(splt_state *state, const char *filename,
id3_length_t *length, int *error, int *tags_version)
{
*length = 0;
id3_byte_t *bytes = NULL;
FILE *file = splt_u_fopen(filename, "r");
if (! file)
{
splt_t_set_strerror_msg(state);
splt_t_set_error_data(state,filename);
*error = SPLT_ERROR_CANNOT_OPEN_FILE;
goto end;
}
else
{
bytes = splt_mp3_get_id3v2_tag_bytes(file, length);
*tags_version = 2;
if (! bytes)
{
bytes = splt_mp3_get_id3v1_tag_bytes(file, length);
*tags_version = 1;
}
}
end:
if (fclose(file) != 0)
{
if (bytes)
{
free(bytes);
bytes = NULL;
}
return NULL;
}
return bytes;
}
//puts a original field on id3 conforming to frame_type
static int splt_mp3_put_original_libid3_frame(splt_state *state,
const struct id3_tag *id3tag, const char *frame_type, int id_type)
{
struct id3_frame *frame = NULL;
union id3_field *field = NULL;
id3_ucs4_t *ucs4 = NULL;
id3_utf8_t *tag_value = NULL;
int err = SPLT_OK;
frame = id3_tag_findframe(id3tag, frame_type,0);
if (frame != NULL)
{
if (id_type == SPLT_MP3_ID3_COMMENT)
{
field = id3_frame_field(frame, 3);
ucs4 = (id3_ucs4_t *) id3_field_getfullstring(field);
}
else
{
field = id3_frame_field(frame, 1);
ucs4 = (id3_ucs4_t *) id3_field_getstrings(field,0);
}
if (ucs4 != NULL)
{
tag_value = id3_ucs4_utf8duplicate(ucs4);
if (tag_value != NULL)
{
int length = strlen((char *)tag_value);
switch (id_type)
{
case SPLT_MP3_ID3_ALBUM:
err = splt_t_set_original_tags_field(state,SPLT_TAGS_ALBUM,
0,(char *)tag_value,0x0,length);
break;
case SPLT_MP3_ID3_ARTIST:
err = splt_t_set_original_tags_field(state,SPLT_TAGS_ARTIST,
0,(char *)tag_value,0x0,length);
break;
case SPLT_MP3_ID3_TITLE:
if (strcmp(frame_type,ID3_FRAME_TITLE) == 0)
{
err = splt_t_set_original_tags_field(state,SPLT_TAGS_TITLE,
0,(char *)tag_value,0x0,length);
}
break;
case SPLT_MP3_ID3_YEAR:
err = splt_t_set_original_tags_field(state,SPLT_TAGS_YEAR,
0,(char *)tag_value,0x0,length);
break;
case SPLT_MP3_ID3_TRACK:
err = splt_t_set_original_tags_field(state,SPLT_TAGS_TRACK,
atof((char*)tag_value), NULL,0x0,0);
break;
case SPLT_MP3_ID3_COMMENT:
err = splt_t_set_original_tags_field(state,SPLT_TAGS_COMMENT,
0,(char*)tag_value,0x0,length);
break;
case SPLT_MP3_ID3_GENRE:
err = splt_t_set_original_tags_field(state,SPLT_TAGS_GENRE,
0,NULL,splt_mp3_getgenre((char *)tag_value),0);
int number = 80;
number = atoi((char *)tag_value);
//if we have a number returned by tag_value
if ((number != 0) &&
(state->original_tags.genre == 0xFF))
{
err = splt_t_set_original_tags_field(state,SPLT_TAGS_GENRE,
0,NULL,number,0);
}
//if we have 0 returned
if (strcmp((char*)tag_value, "0") == 0)
{
err = splt_t_set_original_tags_field(state,SPLT_TAGS_GENRE,
0,NULL,12,0);
}
break;
default:
break;
}
free(tag_value);
tag_value = NULL;
}
else
{
err = SPLT_ERROR_CANNOT_ALLOCATE_MEMORY;
}
}
}
return err;
}
//macro used only in the following function splt_mp3_get_original_tags
#define MP3_VERIFY_ERROR() \
if (err != SPLT_OK) \
{ \
*tag_error = err; \
goto end; \
};
//this function puts the original id3 tags if we have libid3tag enabled
//at compilation time
static void splt_mp3_get_original_tags(const char *filename, splt_state *state,
int *tag_error)
{
//we get the id3 from the original file using libid3tag
struct id3_tag *id3tag = NULL;
//get out the tags from the file; id3_file_open doesn't work with win32 utf16 filenames
id3_length_t id3_tag_length = 0;
int tags_version = 0;
id3_byte_t *id3_tag_bytes = splt_mp3_get_id3_tag_bytes(state, filename, &id3_tag_length,
tag_error, &tags_version);
/*//client feedback
if (tags_version == 1)
{
splt_t_put_message_to_client(state, " info: detected input file original tags as ID3v1\n");
}
else if (tags_version == 2)
{
splt_t_put_message_to_client(state, " info: detected input file tags as ID3v2\n");
}*/
if (*tag_error >= 0)
{
if (id3_tag_bytes)
{
id3tag = id3_tag_parse(id3_tag_bytes, id3_tag_length);
if (id3tag)
{
int err = SPLT_OK;
err = splt_t_set_original_tags_field(state,SPLT_TAGS_VERSION,
tags_version, NULL, 0, 0);
MP3_VERIFY_ERROR();
err = splt_mp3_put_original_libid3_frame(state,id3tag,ID3_FRAME_ARTIST,
SPLT_MP3_ID3_ARTIST);
MP3_VERIFY_ERROR();
err = splt_mp3_put_original_libid3_frame(state,id3tag,ID3_FRAME_ALBUM,
SPLT_MP3_ID3_ALBUM);
MP3_VERIFY_ERROR();
err = splt_mp3_put_original_libid3_frame(state,id3tag,ID3_FRAME_TITLE,
SPLT_MP3_ID3_TITLE);
MP3_VERIFY_ERROR();
err = splt_mp3_put_original_libid3_frame(state,id3tag,ID3_FRAME_YEAR,
SPLT_MP3_ID3_YEAR);
MP3_VERIFY_ERROR();
err = splt_mp3_put_original_libid3_frame(state,id3tag,ID3_FRAME_GENRE,
SPLT_MP3_ID3_GENRE);
MP3_VERIFY_ERROR();
err = splt_mp3_put_original_libid3_frame(state,id3tag,ID3_FRAME_COMMENT,
SPLT_MP3_ID3_COMMENT);
MP3_VERIFY_ERROR();
err = splt_mp3_put_original_libid3_frame(state,id3tag,ID3_FRAME_TRACK,
SPLT_MP3_ID3_TRACK);
MP3_VERIFY_ERROR();
id3_tag_delete(id3tag);
}
}
end:
;
}
if (id3_tag_bytes)
{
free(id3_tag_bytes);
id3_tag_bytes = NULL;
}
}
/* build ID3 tags */
void splt_mp3_put_libid3_frame_in_tag_with_content(struct id3_tag *id,
const char *frame_type, int field_number, const char *content, int *error)
{
struct id3_frame *id_frame = NULL;
id3_ucs4_t *field_content = NULL;
union id3_field *id_field = NULL;
if (content)
{
id_frame = id3_frame_new(frame_type);
if (!id_frame)
{
goto error;
}
id_field = id3_frame_field(id_frame, field_number);
id3_field_settextencoding(id3_frame_field(id_frame, 0),
ID3_FIELD_TEXTENCODING_UTF_16);
field_content = id3_utf8_ucs4duplicate((signed char *)content);
if (! field_content)
{
goto error;
}
//1 is usually a string list
if (field_number == 1)
{
if (id3_field_addstring(id_field, field_content) == -1)
{
goto error;
}
}
//the comment is a full string : field number 3
else if (field_number == 3)
{
if (id3_field_setfullstring(id_field, field_content) == -1)
{
goto error;
}
}
if (field_content)
{
free(field_content);
field_content = NULL;
}
if (id3_tag_attachframe(id, id_frame) == -1)
{
goto error;
}
id3_frame_delete(id_frame);
}
return;
error:
*error = SPLT_ERROR_CANNOT_ALLOCATE_MEMORY;
if (id_frame)
{
id3_frame_delete(id_frame);
}
if (field_content)
{
free(field_content);
field_content = NULL;
}
return;
}
static char *splt_mp3_build_libid3tag(const char *title, const char *artist,
const char *album, const char *year, unsigned char genre,
const char *comment, int track, int *error, unsigned long *number_of_bytes,
int tags_version)
{
struct id3_tag *id = id3_tag_new();
id3_byte_t *bytes = NULL;
id3_length_t bytes_length = 0;
if (tags_version == 1)
{
id3_tag_options(id, ID3_TAG_OPTION_ID3V1, ID3_TAG_OPTION_ID3V1);
}
else
{
//turn off CRC and COMPRESSION; many players don't support that ?,
//resulting in No tags (oh !)
id3_tag_options(id, ID3_TAG_OPTION_CRC, 0);
id3_tag_options(id, ID3_TAG_OPTION_COMPRESSION, 0);
}
splt_mp3_put_libid3_frame_in_tag_with_content(id, ID3_FRAME_TITLE, 1, title, error);
if (*error < 0) { goto error; }
splt_mp3_put_libid3_frame_in_tag_with_content(id, ID3_FRAME_ARTIST, 1, artist, error);
if (*error < 0) { goto error; }
splt_mp3_put_libid3_frame_in_tag_with_content(id, ID3_FRAME_ALBUM, 1, album, error);
if (*error < 0) { goto error; }
splt_mp3_put_libid3_frame_in_tag_with_content(id, ID3_FRAME_YEAR, 1, year, error);
if (*error < 0) { goto error; }
splt_mp3_put_libid3_frame_in_tag_with_content(id, ID3_FRAME_COMMENT, 3, comment, error);
if (*error < 0) { goto error; }
if (track != -INT_MAX)
{
char track_str[255] = { '\0' };
snprintf(track_str,254,"%d",track);
splt_mp3_put_libid3_frame_in_tag_with_content(id, ID3_FRAME_TRACK, 1, track_str,
error);
}
if (*error < 0) { goto error; }
splt_mp3_put_libid3_frame_in_tag_with_content(id, ID3_FRAME_GENRE, 1,
splt_mp3_id3v1_categories[genre], error);
if (*error < 0) { goto error; }
//get the number of bytes needed for the tags
bytes_length = id3_tag_render(id, NULL);
if (bytes_length > 0)
{
//allocate memory for the tags
bytes = malloc(sizeof(id3_byte_t) * bytes_length);
if (!bytes)
{
goto error;
}
memset(bytes, '\0', sizeof(id3_byte_t) * bytes_length);
bytes_length = id3_tag_render(id, bytes);
id3_tag_delete(id);
*number_of_bytes = (unsigned long) bytes_length;
}
return (char *) bytes;
error:
*error = SPLT_ERROR_CANNOT_ALLOCATE_MEMORY;
id3_tag_delete(id);
*number_of_bytes = 0;
if (bytes)
{
free(bytes);
bytes = NULL;
}
return NULL;
}
#else
//returns a id3v1 buffer as string
//return must be freed
//-returns NULL if error
static char *splt_mp3_build_simple_id3v1(const char *title, const char *artist,
const char *album, const char *year, unsigned char genre,
const char *comment, int track, int *error, unsigned long *number_of_bytes)
{
char *id = NULL;
char buffer[30] = { '\0' };
int j = 3,i = 0;
if ((id = malloc(sizeof(char) * 128)) != NULL)
{
memset(id,'\0',128);
strncpy(id, SPLT_MP3_TAG, 4);
memset(buffer, '\0', 30);
if (title!=NULL) strncpy(buffer, title, 30);
for (i=0; i<30; i++) id[j++]=buffer[i];
memset(buffer, '\0', 30);
if (artist!=NULL) strncpy(buffer, artist, 30);
for (i=0; i<30; i++) id[j++]=buffer[i];
memset(buffer, '\0', 30);
if (album!=NULL) strncpy(buffer, album, 30);
for (i=0; i<30; i++) id[j++]=buffer[i];
memset(buffer, '\0', 30);
if (year!=NULL) strncpy(buffer, year, 4);
for (i=0; i<4; i++) id[j++]=buffer[i];
memset(buffer, '\0', 30);
if (comment!=NULL) strncpy(buffer, comment, 30);
for (i=0; i<30; i++)
{
id[j++]=buffer[i];
}
//if we have a positive track
if (track != -INT_MAX)
{
if (track != 0x00)
{
id[j-1] = (char) track;
}
}
id[j] = (char) genre;
}
else
{
*error = SPLT_ERROR_CANNOT_ALLOCATE_MEMORY;
return NULL;
}
*number_of_bytes = 128;
return id;
}
#endif
//returns a id3v2 or id3v1 buffer as array of char
//return must be freed
//-returns NULL if error
static char *splt_mp3_build_id3_tags(splt_state *state,
const char *title, const char *artist,
const char *album, const char *year, unsigned char genre,
const char *comment, int track, int *error,
unsigned long *number_of_bytes, int version)
{
char *id = NULL;
#ifdef NO_ID3TAG
if (version == 1)
{
id = splt_mp3_build_simple_id3v1(title, artist, album, year, genre, comment, track,
error, number_of_bytes);
}
#else
if (version == 1)
{
id = splt_mp3_build_libid3tag(title, artist, album, year, genre, comment, track,
error, number_of_bytes, 1);
}
else
{
id = splt_mp3_build_libid3tag(title, artist, album, year, genre, comment, track,
error, number_of_bytes, 2);
}
#endif
return id;
}
//put the song tags
//return a buffer containing the tags : must be freed
static char *splt_mp3_build_tags(const char *filename, splt_state *state, int *error,
unsigned long *number_of_bytes, int id3_version)
{
char *id3_data = NULL;
if (splt_t_get_int_option(state, SPLT_OPT_TAGS) == SPLT_TAGS_ORIGINAL_FILE)
{
id3_data = splt_mp3_build_id3_tags(state,
state->original_tags.title,
state->original_tags.artist,
state->original_tags.album,
state->original_tags.year,
state->original_tags.genre,
state->original_tags.comment,
state->original_tags.track,
error, number_of_bytes, id3_version);
}
else
{
if (splt_t_get_int_option(state,SPLT_OPT_TAGS) == SPLT_CURRENT_TAGS)
{
int current_split = splt_t_get_current_split_file_number(state) - 1;
//if we set all the tags like the x one
int remaining_tags_like_x = splt_t_get_int_option(state,
SPLT_OPT_ALL_REMAINING_TAGS_LIKE_X);
if ((current_split >= state->split.real_tagsnumber) &&
(remaining_tags_like_x != -1))
{
current_split = remaining_tags_like_x;
}
//only if the tags exists for the current split
if (splt_t_tags_exists(state,current_split))
{
//splt_t_set_auto_increment_tracknumber_tag(state, old_current_split, current_split);
//only if we have the artist or the title
int tags_number = 0;
splt_tags *tags = splt_t_get_tags(state, &tags_number);
int track = 0;
if (tags[current_split].track > 0)
{
track = tags[current_split].track;
}
else
{
track = current_split+1;
}
if (splt_t_tags_exists(state,current_split))
{
id3_data = splt_mp3_build_id3_tags(state,
tags[current_split].title,
tags[current_split].artist,
tags[current_split].album,
tags[current_split].year,
tags[current_split].genre,
tags[current_split].comment,
track, error, number_of_bytes, id3_version);
}
}
}
}
return id3_data;
}
//writes id3v1 tags to 'file_output'
int splt_mp3_write_id3v1_tags(splt_state *state, FILE *file_output,
const char *output_fname)
{
const char *filename = splt_t_get_filename_to_split(state);
unsigned long number_of_bytes = 0;
int error = SPLT_OK;
char *id3_tags = splt_mp3_build_tags(filename, state, &error, &number_of_bytes, 1);
if ((error >= 0) && (id3_tags) && (number_of_bytes > 0))
{
if (fseeko(file_output, splt_mp3_getid3v1_offset(file_output), SEEK_END)!=-1)
{
if (fwrite(id3_tags, 1, number_of_bytes, file_output) < number_of_bytes)
{
splt_t_set_error_data(state, output_fname);
error = SPLT_ERROR_CANT_WRITE_TO_OUTPUT_FILE;
}
}
else
{
splt_t_set_strerror_msg(state);
splt_t_set_error_data(state, output_fname);
error = SPLT_ERROR_SEEKING_FILE;
}
}
if (id3_tags)
{
free(id3_tags);
id3_tags = NULL;
}
return error;
}
#ifndef NO_ID3TAG
//writes id3v2 tags to 'file_output'
int splt_mp3_write_id3v2_tags(splt_state *state, FILE *file_output,
const char *output_fname, off_t *end_offset)
{
const char *filename = splt_t_get_filename_to_split(state);
unsigned long number_of_bytes = 0;
int error = SPLT_OK;
char *id3_tags = splt_mp3_build_tags(filename, state, &error, &number_of_bytes, 2);
if ((error >= 0) && (id3_tags) && (number_of_bytes > 0))
{
if (fwrite(id3_tags, 1, number_of_bytes, file_output) < number_of_bytes)
{
splt_t_set_error_data(state, output_fname);
error = SPLT_ERROR_CANT_WRITE_TO_OUTPUT_FILE;
}
else
{
if (end_offset != NULL)
{
*end_offset = number_of_bytes;
}
}
}
if (id3_tags)
{
free(id3_tags);
id3_tags = NULL;
}
return error;
}
#endif
//returns the output tags version
int splt_mp3_get_output_tags_version(splt_state *state)
{
#ifdef NO_ID3TAG
return 1;
#else
int original_tags_version = state->original_tags.tags_version;
int force_tags_version = splt_t_get_int_option(state, SPLT_OPT_FORCE_TAGS_VERSION);
int output_tags_version = original_tags_version;
if ((force_tags_version == 1) || (force_tags_version == 2))
{
output_tags_version = force_tags_version;
}
/*if (output_tags_version == 0)
{
output_tags_version = 2;
}*/
return output_tags_version;
#endif
}
/****************************/
/* mp3 infos */
//puts in the state informations about mp3 file
//must be called before splt_mp3_split()
//enables framemode if xing header found
//xing header is often associated with VBR (variable bit rate)
static splt_mp3_state *splt_mp3_info(FILE *file_input, splt_state *state,
int framemode, int *error)
{
splt_mp3_state *mp3state = state->codec;
int prev = -1, len;
if ((mp3state = malloc(sizeof(splt_mp3_state)))==NULL)
{
*error = SPLT_ERROR_CANNOT_ALLOCATE_MEMORY;
return NULL;
}
memset(mp3state, 0x0, sizeof(splt_mp3_state));
char *filename = splt_t_get_filename_to_split(state);
//always quiet
mp3state->syncdetect = 0;
//we initialise default values
mp3state->frames = 1;
mp3state->end = 0;
mp3state->first = 1;
mp3state->file_input = file_input;
mp3state->framemode = framemode;
mp3state->headw = 0;
mp3state->mp3file.xing = 0;
mp3state->mp3file.xing_offset = 0;
mp3state->mp3file.xingbuffer = NULL;
mp3state->mp3file.len = splt_u_flength(state, file_input, filename, error);
if (error < 0)
{
return NULL;
}
splt_t_set_total_time(state,0);
mp3state->data_ptr = NULL;
mp3state->data_len = 0;
mp3state->buf_len = 0;
mp3state->bytes = 0;
//we initialise the mad structures
splt_mp3_init_stream_frame(mp3state);
mad_synth_init(&mp3state->synth);
mad_timer_reset(&mp3state->timer);
//we read mp3 infos and set pointers to read the mp3 data
do
{
int ret = splt_mp3_get_frame(mp3state);
if (ret==-2)
{
splt_t_set_error_data(state,filename);
*error = SPLT_ERROR_INVALID;
goto function_end;
}
if ((prev == 0) &&
((ret == 0) ||
(mp3state->stream.error==MAD_ERROR_BUFLEN)))
{
break;
}
//if we have succeeded to read a frame
if (ret == 0)
{
//we set pointer to the frame
mp3state->data_ptr = (unsigned char *) mp3state->stream.this_frame;
//we set length of frame
if(mp3state->stream.next_frame!=NULL)
{
mp3state->data_len = (long) (mp3state->stream.next_frame - mp3state->stream.this_frame);
}
if (mp3state->stream.anc_bitlen > 64)
{
int tag = 0;
struct mad_bitptr ptr = mp3state->stream.anc_ptr;
struct mad_bitptr start = ptr;
//we search for xing (variable bit rate)
unsigned long xing_word = mad_bit_read(&ptr, 32);
if ((xing_word==SPLT_MP3_XING_MAGIC) ||
(xing_word==SPLT_MP3_INFO_MAGIC))
{
tag = 1;
}
//Handle misplaced Xing header in mp3 files with CRC
else
{
if (xing_word == ((SPLT_MP3_XING_MAGIC << 16) & 0xffffffffL)
|| xing_word == ((SPLT_MP3_INFO_MAGIC << 16) & 0xffffffffL))
{
ptr = start;
mad_bit_skip(&ptr, 16);
tag = 1;
}
}
//if we have xing, put infos
if (tag)
{
xing_word = mad_bit_read(&ptr, 32);
if (xing_word & SPLT_MP3_XING_FRAMES)
{
mad_timer_t total;
mp3state->frames = mad_bit_read(&ptr, 32);
total = mp3state->frame.header.duration;
mad_timer_multiply(&total, mp3state->frames);
float total_time_milliseconds = (float) mad_timer_count(total, MAD_UNITS_MILLISECONDS);
total_time_milliseconds /= 10.f;
splt_t_set_total_time(state, (long) ceilf(total_time_milliseconds));
}
if (xing_word & SPLT_MP3_XING_BYTES)
{
if (mp3state->mp3file.len == 0)
mp3state->mp3file.len = mad_bit_read(&ptr, 32);
}
if (splt_t_get_int_option(state, SPLT_OPT_XING))
{
mp3state->mp3file.xing = mp3state->data_len;
if ((mp3state->mp3file.xingbuffer =
malloc(mp3state->mp3file.xing))==NULL)
{
*error = SPLT_ERROR_CANNOT_ALLOCATE_MEMORY;
goto function_end;
}
memcpy(mp3state->mp3file.xingbuffer, mp3state->data_ptr,
mp3state->mp3file.xing);
mp3state->mp3file.xing_offset = splt_mp3_xing_info_off(mp3state);
}
//set framemode true (because VBR)
splt_t_set_int_option(state, SPLT_OPT_FRAME_MODE, SPLT_TRUE);
mp3state->framemode = 1;
//print message to client because frame mode enabled
if (!splt_t_messages_locked(state))
{
if (!splt_t_get_iopt(state,SPLT_INTERNAL_FRAME_MODE_ENABLED))
{
splt_t_put_message_to_client(state, " info: frame mode enabled\n");
splt_t_set_iopt(state,SPLT_INTERNAL_FRAME_MODE_ENABLED,SPLT_TRUE);
}
}
continue;
}
}
}
prev = ret;
} while (1);
len = (long) (mp3state->buf_len - (mp3state->data_ptr - mp3state->inputBuffer));
if (len < 0)
{
splt_t_set_error_data(state,filename);
*error = SPLT_ERROR_INVALID;
goto function_end;
}
//we put useful infos in the state
mp3state->mp3file.firsth = (off_t) (mp3state->bytes - len);
mp3state->bytes = mp3state->mp3file.firsth;
mp3state->headw =
(unsigned long) ((mp3state->data_ptr[0] << 24) |
(mp3state->data_ptr[1] << 16) |
(mp3state->data_ptr[2] << 8) | (mp3state->data_ptr[3]));
mp3state->mp3file.mpgid = (int) ((mp3state->headw >> 19)&1);
mp3state->mp3file.layer = mp3state->frame.header.layer;
mp3state->mp3file.freq = mp3state->frame.header.samplerate;
mp3state->mp3file.bitrate = mp3state->frame.header.bitrate/SPLT_MP3_BYTE;
mp3state->mp3file.firsthead =
splt_mp3_makehead(mp3state->headw, mp3state->mp3file, mp3state->mp3file.firsthead, mp3state->mp3file.firsth);
mp3state->mp3file.fps = (float) (mp3state->mp3file.freq*(2-mp3state->mp3file.mpgid));
mp3state->mp3file.fps /= SPLT_MP3_PCM;
//we put the channels stuff (mono, stereo)
switch(mp3state->frame.header.mode)
{
case MAD_MODE_SINGLE_CHANNEL:
mp3state->mp3file.channels = 0;
break;
case MAD_MODE_DUAL_CHANNEL:
mp3state->mp3file.channels = 1;
break;
case MAD_MODE_JOINT_STEREO:
mp3state->mp3file.channels = 2;
break;
case MAD_MODE_STEREO:
mp3state->mp3file.channels = 3;
break;
default:
mp3state->mp3file.channels = 4;
break;
}
//we put the total time for constant bit rate
//if it was not set for the variable bit rate
if (splt_t_get_total_time(state) == 0)
{
if (mp3state->mp3file.len > 0)
{
long temp =
((mp3state->mp3file.len - mp3state->mp3file.firsth)
/ mp3state->mp3file.bitrate) * 100;
splt_t_set_total_time(state, temp);
}
}
function_end:
//we free memory allocated by mad_frame_decode(..)
//splt_mp3_finish_stream_frame(mp3state);
mad_synth_finish(&mp3state->synth);
return mp3state;
}
static void splt_mp3_end(splt_state *state, int *error)
{
splt_mp3_state *mp3state = state->codec;
if (mp3state)
{
splt_mp3_finish_stream_frame(mp3state);
if (mp3state->file_input)
{
if (mp3state->file_input != stdin)
{
if (fclose(mp3state->file_input) != 0)
{
splt_t_set_strerror_msg(state);
splt_t_set_error_data(state,
splt_t_get_filename_to_split(state));
*error = SPLT_ERROR_CANNOT_CLOSE_FILE;
}
}
mp3state->file_input = NULL;
}
//we free the mp3 state
splt_mp3_state_free(state);
}
state->codec = NULL;
}
//gets the mp3 info and puts it in the state
static void splt_mp3_get_info(splt_state *state, FILE *file_input, int *error)
{
//checks if valid mp3 file
//before last argument, if framemode or not
//last argument if we put messages to clients or not
state->codec = splt_mp3_info(file_input, state,
splt_t_get_int_option(state,SPLT_OPT_FRAME_MODE), error);
//if error
if ((*error < 0) || (state->codec == NULL))
{
if (state->codec != NULL)
{
splt_mp3_end(state, error);
}
return;
}
//print informations about the current file to the client
else
{
if ((! splt_t_messages_locked(state)) &&
(splt_t_get_int_option(state, SPLT_OPT_SPLIT_MODE) != SPLT_OPTION_WRAP_MODE) &&
(splt_t_get_int_option(state, SPLT_OPT_SPLIT_MODE) != SPLT_OPTION_ERROR_MODE))
{
splt_mp3_state *mp3state = state->codec;
struct splt_mp3 *mfile = &mp3state->mp3file;
//codec infos
char mpeg_infos[2048] = { '\0' };
snprintf(mpeg_infos,2048, " info: MPEG %d Layer %d - %d Hz - %s", (2-mfile->mpgid), mfile->layer, mfile->freq, splt_mp3_chan[mfile->channels]);
//frame mode or bitrate
char frame_mode_infos[256] = { '\0' };
if (mp3state->framemode)
{
if (splt_t_get_int_option(state, SPLT_OPT_INPUT_NOT_SEEKABLE))
{
snprintf(frame_mode_infos,256," - FRAME MODE NS");
}
else
{
snprintf(frame_mode_infos,256," - FRAME MODE");
}
}
else
{
snprintf(frame_mode_infos,256," - %d Kb/s",mfile->bitrate * SPLT_MP3_BYTE / 1000);
}
//total time
char total_time[256] = { '\0' };
int total_seconds = (int) splt_t_get_total_time(state) / 100;
int minutes = total_seconds / 60;
int seconds = total_seconds % 60;
snprintf(total_time,256," - Total time: %dm.%02ds", minutes, seconds%60);
//put all the infos together
char all_infos[3072] = { '\0' };
snprintf(all_infos,3071,"%s%s%s\n",mpeg_infos,frame_mode_infos,total_time);
splt_t_put_message_to_client(state, all_infos);
}
}
}
/****************************/
/* mp3 scan for silence */
//used by mp3_scan_silence, and compare with threshold, returns 0 if
//silence spot > threshold, 1 otherwise
//-computes one frame
static int splt_mp3_silence(splt_mp3_state *mp3state, int channels, mad_fixed_t threshold)
{
int i, j;
mad_fixed_t sample;
int silence = 1;
for (j=0; j<channels; j++)
{
for(i=0; i<mp3state->synth.pcm.length; i++)
{
//get silence spot ?
sample = mad_f_abs(mp3state->synth.pcm.samples[j][i]);
mp3state->temp_level = mp3state->temp_level * 0.999 + sample * 0.001;
if (sample > threshold)
{
silence = 0;
}
}
}
return silence;
}
//scan for silence
//-returns the number of silence points found
//and -1 if error; the error is set in the '*error' parameter
static int splt_mp3_scan_silence(splt_state *state, off_t begin,
unsigned long length, float threshold,
float min, short output, int *error)
{
int len = 0, found = 0, shot;
short first, flush = 0, stop = 0;
unsigned long silence_begin = 0, silence_end = 0, time;
unsigned long count = 0;
off_t pos;
mad_fixed_t th;
splt_mp3_state *mp3state = state->codec;
splt_t_put_progress_text(state,SPLT_PROGRESS_SCAN_SILENCE);
pos = begin;
th = mad_f_tofixed(splt_u_convertfromdB(threshold));
//we seek to the begin
if (fseeko(mp3state->file_input, begin, SEEK_SET)==-1)
{
splt_t_set_strerror_msg(state);
splt_t_set_error_data(state, splt_t_get_filename_to_split(state));
*error = SPLT_ERROR_SEEKING_FILE;
return -1;
}
first = output;
shot = SPLT_DEFAULTSHOT;
//initialise mad stuff
splt_mp3_init_stream_frame(mp3state);
mad_synth_init(&mp3state->synth);
mad_timer_reset(&mp3state->timer);
mp3state->temp_level = 0.0;
//we do the effective scan
do
{
int mad_err = SPLT_OK;
switch (splt_mp3_get_valid_frame(state, &mad_err))
{
case 1:
//1 we have a valid frame
//we get mad infos and put them in the mp3state
mad_timer_add(&mp3state->timer, mp3state->frame.header.duration);
mad_synth_frame(&mp3state->synth,&mp3state->frame);
time = (unsigned long) mad_timer_count(mp3state->timer, MAD_UNITS_CENTISECONDS);
if (length > 0)
{
if (time >= length)
{
flush = 1;
stop = 1;
}
}
if ((!flush) && (splt_mp3_silence(mp3state, MAD_NCHANNELS(&mp3state->frame.header), th)))
{
if (len == 0) silence_begin = time;
if (first == 0)
{
len++;
}
if (shot < SPLT_DEFAULTSHOT)
shot+=2;
silence_end = time;
}
else
{
if (len > SPLT_DEFAULTSILLEN)
{
if ((flush) || (shot <= 0))
{
double begin_position, end_position;
begin_position = (double) (silence_begin / 100.f);
end_position = (double) (silence_end / 100.f);
len = (int) (silence_end - silence_begin);
if ((end_position - begin_position - min) >= 0.f)
{
if (splt_t_ssplit_new(&state->silence_list, begin_position, end_position,
len, error) == -1)
{
stop = 1;
found = -1;
break;
}
found++;
}
len = 0;
shot = SPLT_DEFAULTSHOT;
}
}
else
{
len = 0;
}
if ((first) && (shot <= 0))
{
first = 0;
}
if (shot > 0)
{
shot--;
}
}
if (mp3state->mp3file.len > 0)
{
pos = ftello(mp3state->file_input);
if (count++ % 10 == 0)
{
float level = splt_u_convert2dB(mad_f_todouble(mp3state->temp_level));
if (state->split.get_silence_level)
{
state->split.get_silence_level(time, level, state->split.silence_level_client_data);
}
state->split.p_bar->silence_db_level = level;
state->split.p_bar->silence_found_tracks = found;
}
//if we don't have silence split,
//put the 1/4 of progress
if (splt_t_get_int_option(state, SPLT_OPT_SPLIT_MODE) !=
SPLT_OPTION_SILENCE_MODE)
{
splt_t_update_progress(state,(float)(time),
(float)(length), 4,1/(float)4,
SPLT_DEFAULT_PROGRESS_RATE);
}
else
{
//if we have cancelled the split
if (splt_t_split_is_canceled(state))
{
stop = 1;
}
splt_t_update_progress(state,(float)pos,
(float)(mp3state->mp3file.len),
1,0,SPLT_DEFAULT_PROGRESS_RATE);
}
}
break;
case 0:
//0 we do nothing
break;
case -1:
// -1 means eof
stop = 1;
break;
case -3:
//error from libmad
stop = 1;
*error = mad_err;
found = -1;
break;
default:
break;
}
} while (!stop && (found < SPLT_MAXSILENCE));
//only if we have silence mode, we set progress to 100%
if (splt_t_get_int_option(state, SPLT_OPT_SPLIT_MODE) ==
SPLT_OPTION_SILENCE_MODE)
{
splt_t_update_progress(state,1.0,1.0,1,1,1);
}
//we finish with mad_*
splt_mp3_finish_stream_frame(mp3state);
mad_synth_finish(&mp3state->synth);
return found;
}
/****************************/
/* mp3 split */
//used for the mp3 sync errors, dewrap and mp3 seekable split(for header)
//returns 0 if no errors, SPLT_ defined errors if ones
//It justs copies the data of the input file from a begin offset
//to an end offset, and, eventually, a Xing frame (for VBR)
//at the beginning and a ID3v1 at the end, to an outputfile.
static int splt_mp3_simple_split(splt_state *state, const char *output_fname,
off_t begin, off_t end, int do_write_tags)
{
splt_u_print_debug("We do mp3 simple split on output...",0,output_fname);
splt_u_print_debug("Mp3 simple split offset begin is",begin,NULL);
splt_u_print_debug("Mp3 simple split offset end is",end,NULL);
splt_mp3_state *mp3state = state->codec;
int error = SPLT_OK_SPLIT;
FILE *file_output = NULL;
off_t position = 0;
unsigned char buffer[SPLT_MP3_READBSIZE] = { '\0' };
long readed = 0;
//for the progress
off_t temp_end = 0;
//the start point of the split
long start = begin;
int split_mode = splt_t_get_int_option(state, SPLT_OPT_SPLIT_MODE);
splt_t_put_progress_text(state,SPLT_PROGRESS_CREATE);
char *filename = splt_t_get_filename_to_split(state);
position = ftello(mp3state->file_input); // Save current position
if (fseeko(mp3state->file_input, begin, SEEK_SET)==-1)
{
return SPLT_ERROR_BEGIN_OUT_OF_FILE;
}
//get the file size
off_t st_size;
char *fname_to_split = splt_t_get_filename_to_split(state);
if(splt_u_stat(fname_to_split, NULL, &st_size) == 0)
{
mp3state->end2 = st_size;
}
else
{
splt_t_set_strerror_msg(state);
splt_t_set_error_data(state,fname_to_split);
return SPLT_ERROR_CANNOT_OPEN_FILE;
}
// - means stdout
if (strcmp(output_fname, "-")==0)
{
file_output = stdout;
#ifdef __WIN32__
_setmode(fileno(file_output), _O_BINARY);
#endif
}
else
{
if (!(file_output=splt_u_fopen(output_fname, "wb+")))
{
splt_t_set_strerror_msg(state);
splt_t_set_error_data(state, output_fname);
return SPLT_ERROR_CANNOT_OPEN_DEST_FILE;
}
}
int output_tags_version = splt_mp3_get_output_tags_version(state);
#ifndef NO_ID3TAG
//write id3 tags version 2 at the start of the file, if necessary
if (do_write_tags && (output_tags_version == 2))
{
int err = SPLT_OK;
if ((err = splt_mp3_write_id3v2_tags(state, file_output,
output_fname, NULL)) < 0)
{
error = err;
goto function_end;
}
}
#endif
if (mp3state->mp3file.xing!=0)
{
if (splt_t_get_int_option(state, SPLT_OPT_XING))
{
//error mode split must only contain original file data
if (state->options.split_mode != SPLT_OPTION_ERROR_MODE)
{
if(fwrite(mp3state->mp3file.xingbuffer, 1,
mp3state->mp3file.xing, file_output) < mp3state->mp3file.xing)
{
splt_t_set_error_data(state, output_fname);
error = SPLT_ERROR_CANT_WRITE_TO_OUTPUT_FILE;
goto function_end;
}
}
}
}
while (!feof(mp3state->file_input))
{
readed = SPLT_MP3_READBSIZE;
if (end!=-1)
{
if (begin>=end)
{
break;
}
if ((end-begin) < SPLT_MP3_READBSIZE)
{
readed = end-begin;
}
}
if ((readed = fread(buffer, 1, readed, mp3state->file_input))==-1)
{
break;
}
if (fwrite(buffer, 1, readed, file_output) < readed)
{
splt_t_set_error_data(state,output_fname);
error = SPLT_ERROR_CANT_WRITE_TO_OUTPUT_FILE;
goto function_end;
}
begin += readed;
//we update the progress bar
if ((split_mode == SPLT_OPTION_WRAP_MODE) ||
(split_mode == SPLT_OPTION_ERROR_MODE) ||
((split_mode == SPLT_OPTION_NORMAL_MODE)
&& (!splt_t_get_int_option(state, SPLT_OPT_AUTO_ADJUST))
&& (!splt_t_get_int_option(state, SPLT_OPT_FRAME_MODE))))
{
temp_end = end;
//for the last split
if (end == -1)
{
temp_end = mp3state->end2;
}
splt_t_update_progress(state,(float)(begin-start),
(float)(temp_end-start),1,0,
SPLT_DEFAULT_PROGRESS_RATE);
}
else
{
//if auto adjust, we have 50%
if (splt_t_get_int_option(state, SPLT_OPT_AUTO_ADJUST))
{
splt_t_update_progress(state,(float)(begin-start),
(float)(end-start),
2,0.5,
SPLT_DEFAULT_PROGRESS_RATE);
}
else
{
if (splt_t_get_int_option(state, SPLT_OPT_SPLIT_MODE)
== SPLT_OPTION_TIME_MODE)
{
temp_end = end;
//for the last split
if (end == -1)
{
temp_end = mp3state->end2;
}
//if framemode
if (splt_t_get_int_option(state, SPLT_OPT_FRAME_MODE))
{
splt_t_update_progress(state,(float)(begin-start),
(float)(temp_end-start),
2,0.5,
SPLT_DEFAULT_PROGRESS_RATE);
}
else
{
splt_t_update_progress(state,(float)(begin-start),
(float)(temp_end-start),
1,0,
SPLT_DEFAULT_PROGRESS_RATE);
}
}
else
{
splt_t_update_progress(state,(float)(begin-start),
(float)(end-start),
2,0.5,
SPLT_DEFAULT_PROGRESS_RATE);
}
}
}
}
//write id3 tags version 1 at the end of the file, if necessary
if (do_write_tags && (output_tags_version == 1))
{
int err = SPLT_OK;
if ((err = splt_mp3_write_id3v1_tags(state, file_output, output_fname)) < 0)
{
error = err;
goto function_end;
}
}
if (fseeko(mp3state->file_input, position, SEEK_SET)==-1)
{
splt_t_set_strerror_msg(state);
splt_t_set_error_data(state, filename);
goto function_end;
}
function_end:
if (file_output != stdout)
{
if (fclose(file_output) != 0)
{
splt_t_set_strerror_msg(state);
splt_t_set_error_data(state, filename);
return SPLT_ERROR_CANNOT_CLOSE_FILE;
}
}
file_output = NULL;
return error;
}
//the main mp3 split function
//filename is our filename
//state is our state
//fbegin_sec is the beggining splitpoint
//fend_sec is the end splitpoint
//adjustoption is if we adjust with silence detection or not
//seekable is if we split in seekable mode or not
//threshold - see manual
//must be called after splt_mp3_info()
//returns possible error in '*error'
static void splt_mp3_split(const char *output_fname, splt_state *state,
double fbegin_sec, double fend_sec, int *error, int save_end_point)
{
splt_u_print_debug("Mp3 split...",0,NULL);
splt_u_print_debug("Output filename is",0,output_fname);
splt_u_print_debug("Begin position",fbegin_sec,NULL);
splt_u_print_debug("End position",fend_sec,NULL);
splt_mp3_state *mp3state = state->codec;
short adjustoption = splt_t_get_int_option(state, SPLT_OPT_PARAM_GAP);
short seekable = ! splt_t_get_int_option(state, SPLT_OPT_INPUT_NOT_SEEKABLE);
float threshold = splt_t_get_float_option(state, SPLT_OPT_PARAM_THRESHOLD);
short eof=0, check_bitrate=0;
char *filename = splt_t_get_filename_to_split(state);
FILE *file_output = NULL;
short writing = 0, finished=0;
unsigned long fbegin=0;
off_t wrote = 0;
int len = 0;
//for the progress
unsigned long stopped_frames = 0;
int progress_adjust_val = 2;
if (adjustoption)
{
progress_adjust_val = 4;
}
splt_t_put_progress_text(state,SPLT_PROGRESS_CREATE);
//if not seekable
if (!seekable)
{
splt_u_print_debug("Starting not seekable...",0,NULL);
//for the stdout
if (strcmp(output_fname, "-")==0)
{
file_output = stdout;
#ifdef __WIN32__
_setmode(fileno(file_output), _O_BINARY);
#endif
}
else
{
if (!(file_output=splt_u_fopen(output_fname, "wb+")))
{
splt_t_set_strerror_msg(state);
splt_t_set_error_data(state,output_fname);
*error = SPLT_ERROR_CANNOT_OPEN_DEST_FILE;
return;
}
}
int output_tags_version = splt_mp3_get_output_tags_version(state);
off_t id3v2_end_offset = 0;
#ifndef NO_ID3TAG
//write id3 tags version 2 at the start of the file
if (output_tags_version == 2)
{
int err = SPLT_OK;
if ((err = splt_mp3_write_id3v2_tags(state, file_output,
output_fname, &id3v2_end_offset)) < 0)
{
*error = err;
goto bloc_end;
}
}
#endif
//if we have the framemode
if (mp3state->framemode)
{
splt_u_print_debug("Starting mp3 frame mode...",0,NULL);
long begin_c, end_c, time;
//convert seconds to hundreths
begin_c = (long) (fbegin_sec * 100);
if (fend_sec > 0)
{
end_c = (long) (fend_sec * 100);
}
else
{
end_c = 0;
}
time = 0;
do
{
//we write xing if vbr
if (!writing && (time >= begin_c))
{
writing = 1;
fbegin = mp3state->frames;
if (mp3state->mp3file.xing > 0)
{
wrote = fwrite(mp3state->mp3file.xingbuffer, 1, mp3state->mp3file.xing, file_output);
if (wrote < mp3state->mp3file.xing)
{
splt_t_set_error_data(state,output_fname);
*error = SPLT_ERROR_CANT_WRITE_TO_OUTPUT_FILE;
goto bloc_end;
}
}
}
//we do the split
if (writing)
{
if (mp3state->data_len > 0)
{
if ((len = fwrite(mp3state->data_ptr, 1, mp3state->data_len, file_output))
< mp3state->data_len)
{
splt_t_set_error_data(state,output_fname);
*error = SPLT_ERROR_CANT_WRITE_TO_OUTPUT_FILE;
goto bloc_end;
}
wrote = (off_t) (wrote + len);
mp3state->data_len = 0;
}
if ((end_c > 0) && (time > end_c))
{
finished = 1;
}
if (eof || finished)
{
finished = 1;
if (eof) { *error = SPLT_OK_SPLIT_EOF; }
break;
}
}
//progress bar
if (splt_t_get_int_option(state,SPLT_OPT_SPLIT_MODE)
== SPLT_OPTION_TIME_MODE)
{
splt_t_update_progress(state,(float)(time-begin_c),
(float)(end_c-begin_c),1,0,
SPLT_DEFAULT_PROGRESS_RATE);
}
else
{
splt_t_update_progress(state,(float)(time),
(float)(end_c),1,0,
SPLT_DEFAULT_PROGRESS_RATE);
}
int mad_err = SPLT_OK;
switch (splt_mp3_get_valid_frame(state, &mad_err))
{
case 1:
mad_timer_add(&mp3state->timer, mp3state->frame.header.duration);
mp3state->frames++;
time = (unsigned long) mad_timer_count(mp3state->timer, MAD_UNITS_CENTISECONDS);
break;
case 0:
break;
case -1:
eof = 1;
*error = SPLT_OK_SPLIT_EOF;
break;
case -3:
//error from libmad
*error = mad_err;
goto bloc_end;
break;
default:
break;
}
} while (!finished);
}
//if we don't have the framemode
else
{
splt_u_print_debug("Starting mp3 non frame mode...",0,NULL);
off_t begin = 0, end = 0;
if (fend_sec != -1)
{
end = (off_t) (fend_sec *
mp3state->mp3file.bitrate + mp3state->mp3file.firsth);
}
//find begin point because no 'end' saved point
if (mp3state->end == 0)
{
begin = (off_t) (fbegin_sec * mp3state->mp3file.bitrate + mp3state->mp3file.firsth);
if ((mp3state->bytes == begin) && (mp3state->data_len > 0))
{
len = (long) (mp3state->inputBuffer + mp3state->buf_len - mp3state->data_ptr);
if (len < 0)
{
splt_t_set_error_data(state,filename);
*error = SPLT_ERROR_WHILE_READING_FILE;
goto bloc_end;
}
if (fwrite(mp3state->data_ptr, 1, len, file_output) < len)
{
splt_t_set_error_data(state,output_fname);
*error = SPLT_ERROR_CANT_WRITE_TO_OUTPUT_FILE;
goto bloc_end;
}
wrote = (off_t) (wrote + len);
mp3state->data_len = 0;
}
else
{
while (mp3state->bytes < begin)
{
off_t to_read;
if (feof(mp3state->file_input))
{
*error = SPLT_ERROR_BEGIN_OUT_OF_FILE;
goto bloc_end;
}
to_read = (begin - mp3state->bytes);
if (to_read > SPLT_MAD_BSIZE)
to_read = SPLT_MAD_BSIZE;
if ((mp3state->data_len = fread(mp3state->inputBuffer,
1, to_read, mp3state->file_input))<=0)
{
*error = SPLT_ERROR_BEGIN_OUT_OF_FILE;
goto bloc_end;
}
mp3state->bytes+=mp3state->data_len;
}
int mad_err = SPLT_OK;
splt_mp3_init_stream_frame(mp3state);
switch (splt_mp3_get_valid_frame(state, &mad_err))
{
case 1:
len = (long) (mp3state->inputBuffer + mp3state->buf_len - mp3state->data_ptr);
if (len < 0)
{
splt_t_set_error_data(state,filename);
*error = SPLT_ERROR_WHILE_READING_FILE;
goto bloc_end;
}
if (fwrite(mp3state->data_ptr, 1, len, file_output) < len)
{
splt_t_set_error_data(state,output_fname);
*error = SPLT_ERROR_CANT_WRITE_TO_OUTPUT_FILE;
goto bloc_end;
}
wrote = (off_t) (wrote + len);
mp3state->data_len = 0;
break;
case 0:
break;
case -1:
eof = 1;
*error = SPLT_ERROR_BEGIN_OUT_OF_FILE;
break;
case -3:
//error from libmad
*error = mad_err;
goto bloc_end;
break;
default:
break;
}
}
}
//set the 'begin' as the saved 'end'
else
{
len = (long) (mp3state->inputBuffer + mp3state->buf_len - mp3state->data_ptr);
if (len < 0)
{
splt_t_set_error_data(state,filename);
*error = SPLT_ERROR_WHILE_READING_FILE;
goto bloc_end;
}
if (fwrite(mp3state->data_ptr, 1, len, file_output) < len)
{
splt_t_set_error_data(state,output_fname);
*error = SPLT_ERROR_CANT_WRITE_TO_OUTPUT_FILE;
goto bloc_end;
}
wrote = (off_t) (wrote + len);
mp3state->data_len = 0;
begin = mp3state->end;
}
long split_begin_point = mp3state->bytes;
//while not end of file, read write :
while (!eof)
{
off_t to_read = SPLT_MAD_BSIZE;
if (end > 0)
{
to_read = (end - mp3state->bytes);
if (to_read <= 0)
{
break;
}
if (to_read > SPLT_MAD_BSIZE)
to_read = SPLT_MAD_BSIZE;
}
//we read the file input
if (feof(mp3state->file_input) ||
((mp3state->data_len =
fread(mp3state->inputBuffer, 1, to_read, mp3state->file_input))<=0))
{
eof = 1;
*error = SPLT_OK_SPLIT_EOF;
break;
}
//we write to file output
if (fwrite(mp3state->inputBuffer, 1, mp3state->data_len, file_output) < mp3state->data_len)
{
splt_t_set_error_data(state,output_fname);
*error = SPLT_ERROR_CANT_WRITE_TO_OUTPUT_FILE;
goto bloc_end;
}
mp3state->bytes += mp3state->data_len;
splt_t_update_progress(state, (float) (mp3state->bytes-split_begin_point),
(float)(end-split_begin_point), 1,0,SPLT_DEFAULT_PROGRESS_RATE);
}
splt_mp3_save_end_point(state, mp3state, save_end_point, end);
if (!eof)
{
int mad_err = SPLT_OK;
splt_mp3_init_stream_frame(mp3state);
switch (splt_mp3_get_valid_frame(state, &mad_err))
{
case 1:
len = (long) (mp3state->data_ptr - mp3state->inputBuffer);
if (len < 0)
{
splt_t_set_error_data(state,filename);
*error = SPLT_ERROR_WHILE_READING_FILE;
goto bloc_end;
}
if (fwrite(mp3state->inputBuffer, 1, len, file_output) < len)
{
splt_t_set_error_data(state,output_fname);
*error = SPLT_ERROR_CANT_WRITE_TO_OUTPUT_FILE;
goto bloc_end;
}
break;
case 0:
break;
case -1:
eof = 1;
*error = SPLT_OK_SPLIT_EOF;
break;
case -3:
*error = mad_err;
goto bloc_end;
break;
default:
break;
}
}
}
//we write id3 and other stuff
if (file_output)
{
if (mp3state->mp3file.xing > 0)
{
if (fseeko(file_output, mp3state->mp3file.xing_offset+4+id3v2_end_offset, SEEK_SET)!=-1)
{
unsigned long headw = (unsigned long) (mp3state->frames - fbegin + 1); // Frames
fputc((headw >> 24) & 0xFF, file_output);
fputc((headw >> 16) & 0xFF, file_output);
fputc((headw >> 8) & 0xFF, file_output);
fputc((headw >> 0) & 0xFF, file_output);
headw = (unsigned long) (wrote); // Bytes
fputc((headw >> 24) & 0xFF, file_output);
fputc((headw >> 16) & 0xFF, file_output);
fputc((headw >> 8) & 0xFF, file_output);
fputc((headw >> 0) & 0xFF, file_output);
}
else
{
splt_t_set_strerror_msg(state);
splt_t_set_error_data(state, output_fname);
*error = SPLT_ERROR_SEEKING_FILE;
goto bloc_end;
}
}
//write id3 tags version 1 at the end of the file
if (output_tags_version == 1)
{
int err = SPLT_OK;
if ((err = splt_mp3_write_id3v1_tags(state, file_output, output_fname)) < 0)
{
*error = err;
goto bloc_end;
}
}
}
bloc_end:
if (file_output)
{
if (file_output != stdout)
{
if (fclose(file_output) != 0)
{
splt_t_set_strerror_msg(state);
splt_t_set_error_data(state, output_fname);
*error = SPLT_ERROR_CANNOT_CLOSE_FILE;
}
}
}
file_output = NULL;
if (*error == SPLT_OK) { *error = SPLT_OK_SPLIT; }
return;
}
//if seekable :
else
{
splt_u_print_debug("Starting mp3 seekable...",0,NULL);
off_t begin = 0, end = 0;
//if framemode
if (mp3state->framemode)
{
splt_u_print_debug("Starting mp3 frame mode...",0,NULL);
unsigned long fbegin, fend, adjust;
fbegin = fend = adjust = 0;
fbegin = fbegin_sec * mp3state->mp3file.fps;
if (fend_sec != -1)
{
//if adjustoption
if (adjustoption)
{
if (fend_sec != -1)
{
float adj = (float) (adjustoption);
float len = (fend_sec - fbegin_sec);
if (adj > len)
{
adj = len;
}
if (fend_sec > adj)
{
fend_sec -= adj;
}
adjust = (unsigned long) (adj * 100.f);
}
else
{
adjust=0;
}
}
fend = fend_sec * mp3state->mp3file.fps;
}
else
{
fend = 0xFFFFFFFF;
}
splt_u_print_debug("Finding begin...",0,NULL);
if (mp3state->end == 0)
{
if (mp3state->first)
{
mp3state->h.ptr = mp3state->mp3file.firsthead.ptr;
mp3state->h.framesize = mp3state->mp3file.firsthead.framesize;
begin = mp3state->mp3file.firsthead.ptr;
mp3state->first = 0;
}
splt_t_put_progress_text(state,SPLT_PROGRESS_PREPARE);
// Finds begin by counting frames
while (mp3state->frames < fbegin)
{
begin = splt_mp3_findhead(mp3state, mp3state->h.ptr + mp3state->h.framesize);
if (begin==-1)
{
*error = SPLT_ERROR_BEGIN_OUT_OF_FILE;
return;
}
//count the number of syncerrors
if ((begin!=mp3state->h.ptr + mp3state->h.framesize)&&(state->syncerrors>=0))
{
state->syncerrors++;
}
if ((mp3state->syncdetect)&&(state->syncerrors> SPLT_MAXSYNC))
{
splt_mp3_checksync(mp3state);
}
mp3state->h = splt_mp3_makehead (mp3state->headw, mp3state->mp3file, mp3state->h, begin);
mp3state->frames++;
//if we have adjust mode, then put only 25%
//else put 50%
if (adjustoption)
{
splt_t_update_progress(state,(float)(mp3state->frames),
(float)fend, 8,
0,SPLT_DEFAULT_PROGRESS_RATE);
}
else
{
splt_t_update_progress(state,(float)(mp3state->frames),
(float)fend,progress_adjust_val,
0,SPLT_DEFAULT_PROGRESS_RATE);
}
}
}
else
{
begin = mp3state->end;
}
splt_u_print_debug("Begin is...",begin,NULL);
if (mp3state->mp3file.len > 0)
{
if (begin >= mp3state->mp3file.len) // If we can check, we just do that :)
{
*error = SPLT_ERROR_BEGIN_OUT_OF_FILE;
return;
}
}
splt_t_put_progress_text(state,SPLT_PROGRESS_PREPARE);
long int frames_begin = mp3state->frames;
// Finds end by counting frames
while (mp3state->frames <= fend)
{
mp3state->frames++;
end = splt_mp3_findhead(mp3state, mp3state->h.ptr + mp3state->h.framesize);
if (end == -1)
{
end = mp3state->h.ptr + mp3state->h.framesize; // Last valid offset
eof=1;
*error = SPLT_OK_SPLIT_EOF;
break;
}
//count the number of syncerrors
if ((end != mp3state->h.ptr + mp3state->h.framesize)&&(state->syncerrors>=0))
{
state->syncerrors++;
}
if ((mp3state->syncdetect)&&(state->syncerrors>SPLT_MAXSYNC))
{
splt_mp3_checksync(mp3state);
}
mp3state->h = splt_mp3_makehead (mp3state->headw, mp3state->mp3file, mp3state->h, end);
//if we have a progress callback function
//time split only calculates the end of the
//split
int split_mode = splt_t_get_int_option(state, SPLT_OPT_SPLIT_MODE);
if (((split_mode == SPLT_OPTION_TIME_MODE) ||
(split_mode == SPLT_OPTION_SILENCE_MODE))
&& (!splt_t_get_int_option(state,SPLT_OPT_AUTO_ADJUST)))
{
splt_t_update_progress(state, (float)(mp3state->frames-fbegin),
(float)(fend-fbegin), progress_adjust_val,
0, SPLT_DEFAULT_PROGRESS_RATE);
}
else
{
if (adjustoption)
{
if(adjust)
{
if (split_mode == SPLT_OPTION_TIME_MODE)
{
splt_t_update_progress(state,
(float)(mp3state->frames-frames_begin),
(float)(fend-frames_begin),
4,0,SPLT_DEFAULT_PROGRESS_RATE);
}
else
{
splt_t_update_progress(state,
(float)(mp3state->frames-frames_begin),
(float)(fend-frames_begin),
8,1/(float)8,SPLT_DEFAULT_PROGRESS_RATE);
}
}
}
else
{
splt_t_update_progress(state,
(float)(mp3state->frames-stopped_frames),
(float)(fend-stopped_frames),
progress_adjust_val,
0,SPLT_DEFAULT_PROGRESS_RATE);
}
}
//if adjust option, scans for silence
if ((adjust) && (mp3state->frames >= fend))
{
int silence_points_found =
splt_mp3_scan_silence(state, end, 2 * adjust, threshold, 0.f, 0, error);
//if error, go out
if (silence_points_found == -1)
{
return;
}
else if (silence_points_found > 0)
{
adjust = (unsigned long) (splt_u_silence_position(state->silence_list, mp3state->off)
* mp3state->mp3file.fps);
}
else
{
adjust = (unsigned long) (adjustoption * mp3state->mp3file.fps);
}
fend += adjust;
end = splt_mp3_findhead(mp3state, end);
splt_t_ssplit_free(&state->silence_list);
adjust=0;
//progress
splt_t_put_progress_text(state,SPLT_PROGRESS_PREPARE);
stopped_frames = mp3state->frames;
}
}
splt_mp3_save_end_point(state, mp3state, save_end_point, end);
//if xing, we get xing
if (mp3state->mp3file.xing > 0)
{
unsigned long headw;
headw = (unsigned long) (mp3state->frames - fbegin + 1); // Frames
mp3state->mp3file.xingbuffer[mp3state->mp3file.xing_offset+4] = (headw >> 24) & 0xFF;
mp3state->mp3file.xingbuffer[mp3state->mp3file.xing_offset+5] = (headw >> 16) & 0xFF;
mp3state->mp3file.xingbuffer[mp3state->mp3file.xing_offset+6] = (headw >> 8) & 0xFF;
mp3state->mp3file.xingbuffer[mp3state->mp3file.xing_offset+7] = headw & 0xFF;
//we put the length of the file if end is -1
if (end == -1)
{
end = mp3state->mp3file.len;
}
headw = (unsigned long) (end - begin + mp3state->mp3file.xing); // Bytes
mp3state->mp3file.xingbuffer[mp3state->mp3file.xing_offset+8] = (headw >> 24) & 0xFF;
mp3state->mp3file.xingbuffer[mp3state->mp3file.xing_offset+9] = (headw >> 16) & 0xFF;
mp3state->mp3file.xingbuffer[mp3state->mp3file.xing_offset+10] = (headw >> 8) & 0xFF;
mp3state->mp3file.xingbuffer[mp3state->mp3file.xing_offset+11] = headw & 0xFF;
}
}
else
//if not framemode
{
splt_u_print_debug("Starting mp3 non frame mode...",0,NULL);
//find begin point if the last 'end' not saved
if (mp3state->end == 0)
{
begin = (off_t) (fbegin_sec * mp3state->mp3file.bitrate + mp3state->mp3file.firsth);
// Finds first valid header. Mantain clean files.
begin = splt_mp3_findvalidhead(mp3state, begin);
if (begin==-1)
{
*error = SPLT_ERROR_BEGIN_OUT_OF_FILE;
return;
}
if (splt_mp3_tabsel_123[1 - mp3state->mp3file.mpgid][mp3state->mp3file.layer-1][splt_mp3_c_bitrate(mp3state->headw)] !=
mp3state->mp3file.firsthead.bitrate)
{
check_bitrate = 1;
}
}
//set the begin point from the last 'end' saved
else
{
begin = mp3state->end;
}
if (fend_sec != -1)
{
end = (off_t) (fend_sec * mp3state->mp3file.bitrate + mp3state->mp3file.firsth);
end = splt_mp3_findvalidhead(mp3state, end); // We take the complete frame
if (splt_mp3_tabsel_123[1 - mp3state->mp3file.mpgid][mp3state->mp3file.layer-1][splt_mp3_c_bitrate(mp3state->headw)] !=
mp3state->mp3file.firsthead.bitrate)
check_bitrate = 1;
}
splt_mp3_save_end_point(state, mp3state, save_end_point, end);
}
//seekable real split
int err = splt_mp3_simple_split(state, output_fname, begin, end, SPLT_TRUE);
if (err < 0) { *error = err; }
if (!save_end_point)
{
if (splt_t_get_long_option(state, SPLT_OPT_OVERLAP_TIME) > 0)
{
mp3state->frames = 1;
mp3state->first = 1;
}
}
}
if (check_bitrate)
{
*error = SPLT_MIGHT_BE_VBR;
}
if (*error == SPLT_OK) { *error = SPLT_OK_SPLIT; }
}
/****************************/
/* mp3 syncerror */
//this function is searching for the id3v1 and id3v2 and returns the offset
static off_t splt_mp3_adjustsync(splt_mp3_state *mp3state, off_t begin, off_t end)
{
off_t position;
position = begin;
if (fseeko(mp3state->file_input, position, SEEK_SET)==-1)
{
return (off_t) (-1);
}
// First we search for ID3v1
while (position++ < end)
{
if (fgetc(mp3state->file_input)=='T') {
if (fgetc(mp3state->file_input)=='A') {
if (fgetc(mp3state->file_input)=='G')
return (position + 127);
else position++;
}
if (fseeko(mp3state->file_input, -1, SEEK_CUR) == -1)
{
return (off_t) (-1);
}
}
}
position = begin;
if (fseeko(mp3state->file_input, position, SEEK_SET)==-1)
{
return (off_t) (-1);
}
// Now we search for ID3v2
while (position++ < end)
{
if (fgetc(mp3state->file_input)=='I')
{
if (fgetc(mp3state->file_input)=='D')
{
if (fgetc(mp3state->file_input)=='3')
{
return (position - 1);
}
else
{
position++;
}
}
if(fseeko(mp3state->file_input, -1, SEEK_CUR)==-1)
{
return (off_t) (-1);
}
}
}
return end;
}
//the function counts the number of sync error splits,
//and sets the offsets
static void splt_mp3_syncerror_search(splt_state *state, int *error)
{
off_t offset = 0;
char *filename = splt_t_get_filename_to_split(state);
int sync_err = SPLT_OK;
splt_mp3_state *mp3state = state->codec;
splt_t_put_progress_text(state,SPLT_PROGRESS_SEARCH_SYNC);
mp3state->h.ptr = mp3state->mp3file.firsthead.ptr;
mp3state->h.framesize = mp3state->mp3file.firsthead.framesize;
//if the filename is correct
int is_file = splt_check_is_file(state, filename);
if (*error < 0) { return; }
if (!is_file)
{
*error = SPLT_ERROR_INEXISTENT_FILE;
return;
}
//we get the file length for the progress
off_t st_size;
if(splt_u_stat(filename, NULL, &st_size) == 0)
{
//put the start point
sync_err = splt_t_serrors_append_point(state, 0);
if (sync_err != SPLT_OK)
{
*error = sync_err;
return;
}
//search for sync errors and put in splitpoints
while (state->serrors->serrors_points_num < SPLT_MAXSYNC)
{
offset = splt_mp3_findhead(mp3state, mp3state->h.ptr + mp3state->h.framesize);
if (offset==-1)
{
break;
}
if (offset != mp3state->h.ptr + mp3state->h.framesize)
{
off_t serror_point =
splt_mp3_adjustsync(mp3state, mp3state->h.ptr, offset);
//put syncerror splitpoint offset
sync_err = splt_t_serrors_append_point(state, serror_point);
if (sync_err != SPLT_OK)
{
*error = sync_err;
return;
}
offset = splt_mp3_findvalidhead(mp3state, serror_point);
if (splt_u_getword(mp3state->file_input, offset, SEEK_SET, &mp3state->headw) == -1)
{
*error = SPLT_ERR_SYNC;
return;
}
}
mp3state->h = splt_mp3_makehead (mp3state->headw, mp3state->mp3file,
mp3state->h, offset);
if (splt_t_split_is_canceled(state))
{
*error = SPLT_SPLIT_CANCELLED;
return;
}
//progress
splt_t_update_progress(state,(float)(offset),
(float)(st_size),1,0,
SPLT_DEFAULT_PROGRESS_RATE);
}
}
else
{
splt_t_set_strerror_msg(state);
splt_t_set_error_data(state,filename);
*error = SPLT_ERROR_CANNOT_OPEN_FILE;
return;
}
if (state->serrors->serrors_points_num == 0)
{
*error = SPLT_ERR_NO_SYNC_FOUND;
return;
}
if (state->serrors->serrors_points_num == SPLT_MAXSYNC)
{
*error = SPLT_ERR_TOO_MANY_SYNC_ERR;
return;
}
//put the end point
sync_err = splt_t_serrors_append_point(state, LONG_MAX);
if (sync_err != SPLT_OK)
{
*error = sync_err;
return;
}
*error = SPLT_SYNC_OK;
return;
}
/****************************/
/* mp3 dewrap */
static const unsigned char splt_mp3_albumwraphead[22] =
{
0xa, 0x23, 0x54, 0x49, 0x54, 0x32, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x41, 0x6c, 0x62, 0x75, 0x6d, 0x57, 0x72, 0x61, 0x70,
};
//this function dewraps a file
//we return the possible error in the process_result parameter
static void splt_mp3_dewrap(int listonly, const char *dir, int *error, splt_state *state)
{
if (listonly)
{
*error = SPLT_OK;
}
else
{
*error = SPLT_DEWRAP_OK;
}
//if albumwrap or mp3wrap
short albumwrap=0, mp3wrap=0;
//wrapfiles = the wrapped files number
int wrapfiles=0, i, j, k=0;
unsigned char c;
char filename[2048] = { '\0' };
off_t begin=0, end=0, len = 0, id3offset = 0;
char junk[2048] = { '\0' };
char *file_to_dewrap = splt_t_get_filename_to_split(state);
//if error
if (*error < 0)
{
return;
}
else
{
splt_mp3_state *mp3state = state->codec;
if (*error >= 0)
{
len = splt_u_flength(state, mp3state->file_input, file_to_dewrap, error);
if (error < 0) { return; }
id3offset = splt_mp3_getid3v2_end_offset(mp3state->file_input, 0);
//we go at the beginning of the file
if (fseeko(mp3state->file_input, id3offset, SEEK_SET)==-1)
{
*error = SPLT_DEWRAP_ERR_FILE_NOT_WRAPED_DAMAGED;
return;
}
splt_u_print_debug("We search for wrap string...",0,NULL);
//we search the WRAP string in the file to see if it was wrapped
//with mp3wrap
for (i=0; i<16384; i++)
{
if (feof(mp3state->file_input))
{
*error = SPLT_DEWRAP_ERR_FILE_NOT_WRAPED_DAMAGED;
return;
}
if ((id3offset = ftello(mp3state->file_input))==-1)
{
*error = SPLT_DEWRAP_ERR_FILE_NOT_WRAPED_DAMAGED;
return;
}
if (fgetc(mp3state->file_input)=='W')
if (fgetc(mp3state->file_input)=='R')
if (fgetc(mp3state->file_input)=='A')
if (fgetc(mp3state->file_input)=='P')
{
mp3wrap = 1;
break;
}
}
//we check if the file was wrapped with albumwrap
//only if not mp3wrap
if (!mp3wrap && (id3offset!=0))
{
if (fseeko(mp3state->file_input, (off_t) 8, SEEK_SET)==-1)
{
*error = SPLT_DEWRAP_ERR_FILE_NOT_WRAPED_DAMAGED;
return;
}
albumwrap = 1;
for (i=0; i<22; i++)
{
if (splt_mp3_albumwraphead[i]!=fgetc(mp3state->file_input))
{
albumwrap = 0;
break;
}
}
}
//we do the mp3wrap or albumwrap
if (albumwrap || mp3wrap)
{
splt_u_print_debug("We do the effective dewrap...",0,NULL);
//client informations
char client_infos[1024] = { '\0' };
//mp3wrap checkings and we get the wrap file number
if (mp3wrap) {
splt_u_print_debug("We do mp3 mp3wrap check...",0,NULL);
short indexver;
//Mp3Wrap version
char major_v = fgetc(mp3state->file_input);
char minor_v = fgetc(mp3state->file_input);
snprintf(client_infos,1024,
" Detected file created with: Mp3Wrap v. %c.%c\n",major_v,minor_v);
splt_t_put_message_to_client(state, client_infos);
indexver = fgetc(mp3state->file_input);
if (indexver > SPLT_MP3_INDEXVERSION)
{
*error = SPLT_DEWRAP_ERR_VERSION_OLD;
return;
}
wrapfiles = (int) fgetc(mp3state->file_input);
if (feof(mp3state->file_input))
{
*error = SPLT_DEWRAP_ERR_FILE_NOT_WRAPED_DAMAGED;
return;
}
//crc
if (indexver > 0x0)
{
unsigned long crc = 0, fcrc = 0;
if (splt_u_getword(mp3state->file_input, 0, SEEK_CUR, &fcrc)==-1)
{
*error = SPLT_DEWRAP_ERR_FILE_NOT_WRAPED_DAMAGED;
return;
}
//perform CRC only if we don't have quiet mode
if (! splt_t_get_int_option(state, SPLT_OPT_QUIET_MODE))
{
begin = ftello(mp3state->file_input);
if (fseeko(mp3state->file_input,
splt_mp3_getid3v1_offset(mp3state->file_input), SEEK_END)==-1)
{
splt_t_set_strerror_msg(state);
splt_t_set_error_data(state, file_to_dewrap);
*error = SPLT_ERROR_SEEKING_FILE;
return;
}
end = ftello(mp3state->file_input);
splt_t_put_message_to_client(state,
" Check for file integrity: calculating CRC please wait... ");
crc = splt_mp3_c_crc(state, mp3state->file_input, begin, end, error);
if (*error < 0)
{
return;
}
if (crc != fcrc)
{
//No interactivity in the library (for the moment)
//fprintf (stderr, "BAD\nWARNING: Bad CRC. File might be damaged. Continue anyway? (y/n) ");
//fgets(junk, 32, stdin);
//if (junk[0]!='y')
//error("Aborted.",125);
//- no interactivity in the library for the moment
*error = SPLT_ERROR_CRC_FAILED;
return;
}
else
{
splt_t_put_message_to_client(state, " OK\n");
}
if (fseeko(mp3state->file_input, begin, SEEK_SET)==-1)
{
splt_t_set_strerror_msg(state);
splt_t_set_error_data(state, file_to_dewrap);
*error = SPLT_ERROR_SEEKING_FILE;
return;
}
}
}
}
//the albumwrap checkings and we get the wrap files number in
//wrapfiles variable
if (albumwrap)
{
splt_u_print_debug("We do mp3 albumwrap check...",0,NULL);
//Mp3Wrap version
snprintf(client_infos,1024, " Detected file created with: AlbumWrap\n");
splt_t_put_message_to_client(state, client_infos);
if (fseeko(mp3state->file_input, (off_t) 0x52d, SEEK_SET)==-1)
{
*error = SPLT_DEWRAP_ERR_FILE_NOT_WRAPED_DAMAGED;
return;
}
i = 0;
while (((c=fgetc(mp3state->file_input))!=0x20) &&(i<2048))
junk[i++] = c;
junk[i] = '\0';
wrapfiles = atoi (junk);
}
if (wrapfiles<=0)
{
*error = SPLT_DEWRAP_ERR_NO_FILE_OR_BAD_INDEX;
return;
}
//we put the number of "splitpoints"
state->split.splitnumber = wrapfiles+1;
splt_u_print_debug("Number of wrap splitpoints is",wrapfiles+1,NULL);
snprintf(client_infos, 1024, " Total files: %d\n",wrapfiles);
splt_t_put_message_to_client(state, client_infos);
//we do the dewrap
for (i=0; i<wrapfiles; i++)
{
if (!splt_t_split_is_canceled(state))
{
//we put the current file to split
splt_t_set_current_split(state, i+1);
//if the first time, we get the begin,
//otherwise the begin will be the end of the previous
if (i==0)
{
//we get the begin wrap
if (mp3wrap)
{
unsigned long w;
if (splt_u_getword (mp3state->file_input, 0, SEEK_CUR, &w)==-1)
{
splt_t_set_error_data(state,file_to_dewrap);
*error = SPLT_DEWRAP_ERR_FILE_DAMAGED_INCOMPLETE;
return;
}
begin = (off_t) (w + id3offset);
}
//we get the begin wrap
if (albumwrap)
{
if (fseeko (mp3state->file_input,
(off_t) SPLT_MP3_ABWINDEXOFFSET, SEEK_SET)==-1)
{
splt_t_set_error_data(state,file_to_dewrap);
*error = SPLT_DEWRAP_ERR_FILE_DAMAGED_INCOMPLETE;
return;
}
j = 0;
while ((c=fgetc(mp3state->file_input))!='[')
if (j++ > 32)
{
splt_t_set_error_data(state,file_to_dewrap);
*error = SPLT_DEWRAP_ERR_FILE_DAMAGED_INCOMPLETE;
return;
}
if (fseeko(mp3state->file_input, (off_t) 3, SEEK_CUR)==-1)
{
splt_t_set_error_data(state,file_to_dewrap);
*error = SPLT_DEWRAP_ERR_FILE_DAMAGED_INCOMPLETE;
return;
}
j = 0;
while ((j<2048) && ((c = fgetc(mp3state->file_input))!='['))
if (c!='.') junk[j++] = c;
else k = j;
junk[j] = '\0';
begin = (off_t) atol (junk);
k = j - k;
if (k<4)
{
for (j=0; j<(4-k); j++)
{
begin = begin * 10;
}
}
}
}
else
{
begin = end;
}
//we get the end and checkings..
if (mp3wrap)
{
unsigned long w;
j = 0;
do
{
c = fgetc(mp3state->file_input);
//for files wrapped using windows
if (c==SPLT_NDIRCHAR)
{
c=SPLT_DIRCHAR;
}
filename[j++] = c;
} while ((c!=0x00)&&(j<2048));
if (splt_u_getword (mp3state->file_input, 0, SEEK_CUR, &w) == -1)
{
splt_t_set_error_data(state,file_to_dewrap);
*error = SPLT_DEWRAP_ERR_FILE_DAMAGED_INCOMPLETE;
return;
}
end = (off_t) (w + id3offset);
//create output directories for the wrapped files
//if we don't list only and if we don't have an output
//directory
if (!listonly && (!dir || dir[0] == '\0'))
{
memset(junk, 0x00, 2048);
char *ptr = filename;
while (((ptr = strchr(ptr, SPLT_DIRCHAR))!=NULL)&&((ptr-filename)<2048))
{
ptr++;
strncpy(junk, filename, ptr-filename);
if (! splt_u_check_if_directory(junk))
{
if ((splt_u_mkdir(junk)) == -1)
{
*error = SPLT_ERROR_CANNOT_CREATE_DIRECTORY;
splt_t_set_strerror_msg(state);
splt_t_set_error_data(state,junk);
return;
}
}
}
}
}
//we get the end wrap point for albumwrap and
//checkings..
if (albumwrap)
{
if (i<wrapfiles-1)
{
if (fseeko (mp3state->file_input,
(off_t) (SPLT_MP3_ABWINDEXOFFSET + (i * SPLT_MP3_ABWLEN)), SEEK_SET)==-1)
{
splt_t_set_error_data(state,file_to_dewrap);
*error = SPLT_DEWRAP_ERR_FILE_DAMAGED_INCOMPLETE;
return;
}
j = 0;
while ((j<2048) && ((c = fgetc(mp3state->file_input))!='['))
if (c!='.') junk[j++] = c;
else k = j;
junk[j] = '\0';
end = (off_t) atol (junk);
k = j - k;
if (k<4)
for (j=0; j<(4-k); j++)
end = end * 10;
end += begin;
}
else end = len;
if (fseeko (mp3state->file_input,
(off_t) (SPLT_MP3_ABWINDEXOFFSET + (i*SPLT_MP3_ABWLEN)), SEEK_SET)==-1)
{
splt_t_set_error_data(state,file_to_dewrap);
*error = SPLT_DEWRAP_ERR_FILE_DAMAGED_INCOMPLETE;
}
j = 0;
while ((c=fgetc(mp3state->file_input))!='[')
if (j++ > 32)
{
splt_t_set_error_data(state,file_to_dewrap);
*error = SPLT_DEWRAP_ERR_FILE_DAMAGED_INCOMPLETE;
return;
}
if (fseeko (mp3state->file_input, (off_t) 3, SEEK_CUR)==-1)
{
splt_t_set_error_data(state,file_to_dewrap);
*error = SPLT_DEWRAP_ERR_FILE_DAMAGED_INCOMPLETE;
return;
}
j = 0;
while ((c=fgetc(mp3state->file_input))!='[')
if (j++ > 32)
{
splt_t_set_error_data(state,file_to_dewrap);
*error = SPLT_DEWRAP_ERR_FILE_DAMAGED_INCOMPLETE;
return;
}
if (fseeko(mp3state->file_input, (off_t) 3, SEEK_CUR)==-1)
{
splt_t_set_error_data(state,file_to_dewrap);
*error = SPLT_DEWRAP_ERR_FILE_DAMAGED_INCOMPLETE;
return;
}
j = 0;
while (j<=400)
filename[j++] = fgetc(mp3state->file_input);
for (j=400; j>0; j--) {
if (filename[j]==0x20)
filename[j]='\0';
else break;
}
filename[j+1] = '\0';
}
splt_u_print_debug("We have found the file",0,filename);
splt_u_print_debug("We cut the dirchar",0,NULL);
//we cut the .DIRCHAR before the filename
char str_temp[4];
snprintf(str_temp,4,"%c%c",'.',SPLT_DIRCHAR);
if (strstr(filename,str_temp) != NULL)
{
char *filename2 = strdup(filename);
if (!filename2)
{
*error = SPLT_ERROR_CANNOT_ALLOCATE_MEMORY;
return;
}
else
{
snprintf(filename,2048, "%s", filename2+2);
free(filename2);
filename2 = NULL;
}
}
if (feof(mp3state->file_input))
{
splt_t_set_error_data(state,file_to_dewrap);
*error = SPLT_DEWRAP_ERR_FILE_DAMAGED_INCOMPLETE;
return;
}
//if we only list the contents
//we put the files in the wrap_files
if (listonly)
{
splt_u_print_debug("We only list wrapped files",0,NULL);
int put_file_error = SPLT_OK;
put_file_error = splt_t_wrap_put_file(state, wrapfiles, i, filename);
if (put_file_error != SPLT_OK)
{
*error = put_file_error;
return;
}
}
//if we split the file
//we split from begin to end calculated previously
else
{
splt_u_print_debug("We split wrapped file",0,NULL);
int ret = 0;
//if we have an output directory
//-try to create it
if (dir && (dir[0] != '\0'))
{
char temp[2048] = { '\0' };
strncpy(temp, filename, 2048);
char *ptr = temp;
//if we have an output dir, cut directory path from filename
if ((ptr = strrchr(temp,SPLT_DIRCHAR)) == NULL)
{
ptr = temp;
}
else
{
if (ptr-temp > 0)
{
ptr++;
}
}
//if dir == .DIRCHAR
if (strcmp(dir,str_temp) == 0)
{
snprintf(filename, 2048,"%s%s", dir, ptr);
}
else
{
snprintf(filename, 2048,"%s%c%s", dir, SPLT_DIRCHAR, ptr);
}
splt_u_print_debug("wrap dir",0,dir);
splt_u_print_debug("wrap after dir",0,ptr);
}
//free xingbuffer
if (mp3state->mp3file.xingbuffer)
{
free(mp3state->mp3file.xingbuffer);
mp3state->mp3file.xingbuffer = NULL;
}
mp3state->mp3file.xing = 0;
int append_err = SPLT_OK;
append_err = splt_t_append_splitpoint(state,0,
splt_u_get_real_name(filename), SPLT_SPLITPOINT);
if (append_err != SPLT_OK)
{
*error = append_err;
return;
}
//cut extension
int cut_err = splt_u_cut_splitpoint_extension(state,i);
if (cut_err != SPLT_OK)
{
*error = cut_err;
return;
}
//do the real wrap split
ret = splt_mp3_simple_split(state, filename, begin, end, SPLT_FALSE);
//if we could split put the split file
if (ret >= 0)
{
ret = splt_t_put_split_file(state, filename);
if (ret < 0) { *error = ret; }
}
else
{
*error = ret;
}
}
}
}
}
else
{
*error = SPLT_DEWRAP_ERR_FILE_NOT_WRAPED_DAMAGED;
return;
}
}
}
}
void splt_mp3_init(splt_state *state, int *error)
{
FILE *file_input = NULL;
char *filename = splt_t_get_filename_to_split(state);
state->syncerrors = 0;
//if we can open the file
if ((file_input = splt_mp3_open_file_read(state, filename, error)) != NULL)
{
splt_mp3_get_info(state, file_input, error);
if (*error >= 0)
{
splt_mp3_state *mp3state = state->codec;
mp3state->off = splt_t_get_float_option(state,SPLT_OPT_PARAM_OFFSET);
//we initialise frames to 1
if (splt_t_get_total_time(state) > 0)
{
mp3state->frames = 1;
}
}
}
}
/****************************/
/* External plugin API */
//returns the plugin infos (name, version, extension)
//-alloced data in splt_plugin_info will be freed at the end of the program
void splt_pl_set_plugin_info(splt_plugin_info *info, int *error)
{
float plugin_version = 0.1;
//set plugin version
info->version = plugin_version;
//set plugin name
info->name = malloc(sizeof(char) * 40);
if (info->name != NULL)
{
snprintf(info->name, 39, "mp3 (libmad)");
}
else
{
*error = SPLT_ERROR_CANNOT_ALLOCATE_MEMORY;
return;
}
//set plugin extension
info->extension = malloc(sizeof(char) * (strlen(SPLT_MP3EXT)+2));
if (info->extension != NULL)
{
snprintf(info->extension, strlen(SPLT_MP3EXT)+1, SPLT_MP3EXT);
}
else
{
*error = SPLT_ERROR_CANNOT_ALLOCATE_MEMORY;
return;
}
}
void splt_pl_init(splt_state *state, int *error)
{
if (splt_t_is_stdin(state))
{
char *filename = splt_t_get_filename_to_split(state);
if (filename[1] == '\0')
{
char message[1024] = { '\0' };
snprintf(message, 1024, " warning: stdin '-' is supposed to be mp3 stream.\n");
splt_t_put_message_to_client(state, message);
}
}
splt_mp3_init(state, error);
}
void splt_pl_end(splt_state *state, int *error)
{
//put infos about the frames processed and the number of sync errors
//ONLY if framemode
if ((splt_t_get_int_option(state, SPLT_OPT_SPLIT_MODE) != SPLT_OPTION_SILENCE_MODE)
&& (splt_t_get_int_option(state, SPLT_OPT_SPLIT_MODE) != SPLT_OPTION_ERROR_MODE)
&& (splt_t_get_int_option(state, SPLT_OPT_SPLIT_MODE) != SPLT_OPTION_WRAP_MODE))
{
if (splt_t_get_int_option(state, SPLT_OPT_FRAME_MODE))
{
if (*error >= 0)
{
splt_mp3_state *mp3state = state->codec;
//-if we don't save the end point, the ->frames are set to 1 at the
//end of the split
if (mp3state->frames != 1)
{
char message[1024] = { '\0' };
snprintf(message, 1024, " Processed %lu frames - Sync errors: %lu\n",
mp3state->frames, state->syncerrors);
splt_t_put_message_to_client(state, message);
}
}
}
}
splt_mp3_end(state, error);
}
//check if file is mp3
int splt_pl_check_plugin_is_for_file(splt_state *state, int *error)
{
char *filename = splt_t_get_filename_to_split(state);
if (filename != NULL && ((strcmp(filename,"-") == 0) ||
(strcmp(filename,"m-") == 0)))
{
return SPLT_TRUE;
}
int is_mp3 = SPLT_FALSE;
splt_t_lock_messages(state);
splt_mp3_init(state, error);
splt_t_unlock_messages(state);
if (*error >= 0)
{
splt_mp3_state *mp3state = state->codec;
if (mp3state)
{
is_mp3 = SPLT_TRUE;
}
}
splt_mp3_end(state, error);
return is_mp3;
}
//search for syncerrors
void splt_pl_search_syncerrors(splt_state *state, int *error)
{
//we detect sync errors
splt_mp3_syncerror_search(state, error);
}
//get wrap files or dewrap
void splt_pl_dewrap(splt_state *state, int listonly, const char *dir, int *error)
{
splt_t_wrap_free(state);
splt_mp3_dewrap(listonly, dir, error, state);
}
void splt_pl_split(splt_state *state, const char *final_fname,
double begin_point, double end_point, int *error, int save_end_point)
{
//effective mp3 split
splt_mp3_split(final_fname, state, begin_point, end_point, error, save_end_point);
}
int splt_pl_simple_split(splt_state *state, char *output_fname, off_t begin, off_t end)
{
int error = SPLT_OK;
splt_mp3_state *mp3state = state->codec;
//we initialise frames to 1
if (splt_t_get_total_time(state) > 0)
{
mp3state->frames = 1;
}
//effective mp3 split
error = splt_mp3_simple_split(state, output_fname, begin, end, SPLT_FALSE);
return error;
}
int splt_pl_scan_silence(splt_state *state, int *error)
{
float offset = splt_t_get_float_option(state,SPLT_OPT_PARAM_OFFSET);
float threshold = splt_t_get_float_option(state, SPLT_OPT_PARAM_THRESHOLD);
float min_length = splt_t_get_float_option(state, SPLT_OPT_PARAM_MIN_LENGTH);
int found = 0;
splt_mp3_state *mp3state = state->codec;
mp3state->off = offset;
found = splt_mp3_scan_silence(state, mp3state->mp3file.firsthead.ptr, 0,
threshold, min_length, 1, error);
return found;
}
void splt_pl_set_original_tags(splt_state *state, int *error)
{
#ifndef NO_ID3TAG
char *filename = splt_t_get_filename_to_split(state);
splt_mp3_get_original_tags(filename, state, error);
#else
//splt_u_error(SPLT_IERROR_SET_ORIGINAL_TAGS,__func__, 0, NULL);
#endif
}
|