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
|
/*************************************************************
* mpgtx an mpeg toolbox *
* by Laurent Alacoque <laureck@users.sourceforge.net> *
* (c) 2001 *
* You may copy, modify and redistribute this *
* source file under the terms of the GNU Public License *
************************************************************/
#include "mpeg.hh"
#include <stdlib.h>
//philipp
#include <stdio.h>
extern bool desperate_mode;
extern bool preserve_header;
extern bool print_progress;
int mpeg2found = 0;
// #define _DEBUG_
int mpeg_bitrate_index [2][3][16] =
{ // MPEG 1
{
// layer 1
{0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,0},
// layer 2
{0,32,48,56, 64, 80, 96,112,128,160,192,224,256,320,384,0},
// layer 3
{0,32,40,48, 56, 64, 80, 96,112,128,160,192,224,256,320,0}
},
// MPEG 2 or 2.5
{
// layer 1
{0,32,48,56, 64, 80, 96,112,128,144,160,176,192,224,256,0},
// layer 2
{0, 8,16,24, 32, 40, 48, 56, 64, 80, 96,112,128,144,160,0},
// layer 3
{0, 8,16,24, 32, 40, 48, 56, 64, 80, 96,112,128,144,160,0}
}
};
int mpeg_sampling_index [3][4] =
{
{44100,48000,32000,0}, //mpeg 1
{22050,24000,16000,0}, //mpeg 2
{11025,12000, 8000,0} //mpeg 2.5
};
double mpeg_frame_rate_index [9] =
{
0., 24000./1001., 24., 25.,
30000./1001., 30., 50.,
60000./1001., 60.
};
// ripped from id3ed V1.10.2 (Thx to Matt Mueller)
// HEY you didn't want me to type *that* !
const char *genre[MAX_ID3_GENRE]={
"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", "AlternRock", "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", "Psychadelic", "Rave", "Showtunes", "Trailer", "Lo-Fi",
"Tribal", "Acid Punk", "Acid Jazz", "Polka", "Retro", "Musical",
"Rock & Roll", "Hard Rock", "Folk", "Folk/Rock", "National Folk", "Swing",
"Fast-Fusion", "Bebob", "Latin", "Revival", "Celtic", "Bluegrass",
"Avantgarde", "Gothic Rock", "Progressive Rock", "Psychedelic Rock", "Symphonic Rock", "Slow Rock",
"Big Band", "Chorus", "Easy Listening", "Acoustic", "Humour", "Speech",
"Chanson", "Opera", "Chamber Music", "Sonata", "Symphony", "Booty Bass",
"Primus", "Porn Groove", "Satire", "Slow Jam", "Club", "Tango",
"Samba", "Folklore", "Ballad", "Power Ballad", "Rhythmic Soul", "Freestyle",
"Duet", "Punk Rock", "Drum Solo", "A capella", "Euro-House", "Dance Hall",
"Goa", "Drum & Bass", "Club House", "Hardcore", "Terror", "Indie",
"BritPop", "NegerPunk", "Polsk Punk", "Beat", "Christian Gangsta", "Heavy Metal",
"Black Metal", "Crossover", "Contemporary C", "Christian Rock", "Merengue", "Salsa",
"Thrash Metal", "Anime", "JPop", "SynthPop"
};
float mpeg::Duration(){
BTRACK;
if (Audio) return float(Audio->duration);
if (Video) return float(Video->duration);
return 0;
}
bool mpeg::Match(mpeg* peer){
BTRACK;
if (MpegType != peer->MpegType) {
fprintf(stderr, "mpeg files are not the same type!\n");
return false;
}
if (MpegType == mpeg_AUDIO || MpegType == mpeg_SYSTEM) {
if (Audio->bitrate != peer->Audio->bitrate) {
fprintf (stderr, "Incompatible audio bitrates %s (%d bps) %s (%d bps)\n",
FileName, Audio->bitrate,
peer->FileName, peer->Audio->bitrate);
return false;
}
if (Audio->mpeg_ver != peer->Audio->mpeg_ver) {
fprintf (stderr, "Incompatible mpeg audio versions "
"%s is mpeg %d %s is mpeg %d\n",
FileName,Audio->mpeg_ver,
peer->FileName, peer->Audio->mpeg_ver);
return false;
}
if (Audio->layer != peer->Audio->layer) {
fprintf (stderr, "Incompatible audio layers %s is layer %d %s is layer %d",
FileName, Audio->layer,
peer->FileName, peer->Audio->layer);
return false;
}
if (Audio->sampling_rate != peer->Audio->sampling_rate){
fprintf (stderr,"Incompatible sampling rates %s (%d Hz) %s (%d Hz)\n",
FileName, Audio->sampling_rate,
peer->FileName, peer->Audio->sampling_rate);
return false;
}
return true;
}
if (MpegType == mpeg_VIDEO || MpegType == mpeg_SYSTEM){
if ( (Video->hsize != peer->Video->hsize) ||
(Video->vsize != peer->Video->vsize)) {
fprintf(stderr,"Incompatible size %s [%ldx%ld] %s [%ldx%ld]\n",
FileName, Video->hsize,Video->vsize,
peer->FileName, peer->Video->hsize, peer->Video->vsize);
return false;
}
if (Video->bitrate != peer->Video->bitrate) {
fprintf(stderr,"incompatible video bitrate %s (%ld bps) %s (%ld bps)\n",
FileName, Video->bitrate,
peer->FileName, peer->Video->bitrate);
return false;
}
if (Video->frame_rate != peer->Video->frame_rate) {
fprintf(stderr, "incompatible video frame rate %s (%f fps) %s (%f fps)\n",
FileName, Video->frame_rate,
peer->FileName, peer->Video->frame_rate);
return false;
}
return true;
}
// no known file
return false;
}
mpeg::mpeg(const char* filename,int verbosity)
: MpegFile(0), Verboseness(verbosity),
HasAudio(false), HasVideo(false),
composite(false), editable(false),
MpegType(mpeg_UNKNOWN),
Audio(0), n_audio(0), Video(0), n_video(0), System(0), Transport(0),
start_with_id3(false),
mpeg_version(1),
buffstart(0),buffend(0),buffer(0)
{
BTRACK;
UData=0;
SExt=0;
DExt=0;
// keep track of filename (usefull for debugging)
// TODO : only on mpeg_VERBOSE ?
FileName = new char[strlen(filename) + 1];
strcpy(FileName
, filename);
// open infile read-only, binary type (for compatibility with other than POSIX/GNU)
MpegFile = fopen(filename, "rb");
#ifdef _DEBUG_
Verboseness = mpeg_VERBOSE;
#endif
// case unable to open
if (MpegFile == 0) {
if (Verboseness!=mpeg_SILENT) {
printf("Unable to open %s\n",filename);
if(Verboseness == mpeg_VERBOSE)
perror(filename);
}
//nothing more to do...
return;
}
// Allocate buffer
buffer = new byte[BUFFERSIZE];
// Seek to end of file
if (FSEEK(MpegFile, 0, SEEK_END)) {
if (Verboseness != mpeg_SILENT) {
printf("Unable to seek in file %s\n", filename);
if (Verboseness == mpeg_VERBOSE)
perror(filename);
}
//give up..
return;
}
// File size
off_t off_eof = FTELL(MpegFile);
if (off_eof == -1) {
if (Verboseness != mpeg_SILENT) {
printf("Seeking to end of input file %s failed.\n", filename);
if (Verboseness == mpeg_VERBOSE)
perror(filename);
}
//give up..
return;
} else FileSize = off_eof;
// if (Verboseness==mpeg_VERBOSE){
#ifdef _DEBUG_
printf("%s has size " _OFF_d "\n", FileName, FileSize);
#endif
// }
// nothing to do on an empty file
if (!FileSize) {
if (Verboseness != mpeg_SILENT) {
printf("File %s is empty.\n", filename);
if (Verboseness == mpeg_VERBOSE)
perror(filename);
}
//give up..
return;
}
// first determine the file type between audio/video/system
if (ParseAudio(0)) {
// this is an Audio only mpeg file
MpegType = mpeg_AUDIO;
composite = false;
editable = true;
}
else if (ParseVideo(0)) {
// this is a Video only mpeg file
MpegType = mpeg_VIDEO;
composite = false;
editable = true;
}
else if (ParseSystem()) {
// this is an Audio and Video mpeg file
MpegType = mpeg_SYSTEM;
}
else if (ParseID3()) {
//this is an audio file with an ID3 v2
MpegType = mpeg_AUDIO;
editable = true;
composite = false;
}
else if (ParseRIFF()) {
// this is either a Wave file or a divx or something else
editable = false;
composite = false;
}
else if (ParseTransportStream(0)) {
// this is a MPEG 2 transport stream
MpegType = mpeg_TRANSPORT;
editable = false;
composite = true;
}
else
{
// can not handle this mpeg file
if (Verboseness == mpeg_VERBOSE)
fprintf(stderr,
"%s is not a valid MPEG file (can't handle it)\n",
FileName);
}
}
// mpeg class destructor
mpeg::~mpeg() {
BTRACK;
if (buffer) delete[] buffer;
if (MpegFile) fclose(MpegFile);
if (Audio) {
if (Audio->tag) delete Audio->tag;
delete Audio;
}
if (Video) {
if (Video->video_header)
delete[] Video->video_header;
delete Video;
}
if (System) {
if(System->audio_system_header)
delete[] System->audio_system_header;
if(System->video_system_header)
delete[] System->video_system_header;
delete System;
}
if (SExt) delete SExt;
if (DExt) delete DExt;
if (UData) {
if (UData->ud) delete[] UData->ud;
delete UData;
}
if (Transport) delete Transport;
}
// map a buffer onto file for efficiency
byte mpeg::GetByte(off_t offset) {
BTRACK;
START_CLOCK;
size_t nread;
if ((offset >= buffend) || (offset < buffstart))
{
// new buffer need (forward)
if (FSEEK(MpegFile, offset, SEEK_SET))
{
if (Verboseness == mpeg_VERBOSE)
fprintf(stderr, "could not get seek to offset (" _OFF_d ") in file %s (size:" _OFF_d ")\n",
offset, FileName, FileSize);
RTRACK;
ATRACK("GetByte Invalid offset");
fprintf(stderr,"mpgtx: AT EOF - please stop me!\n");
return 0x11;
}
nread = fread(buffer, 1, BUFFERSIZE, MpegFile);
buffstart = offset;
buffend = offset + nread;
if ((offset >= buffend) || (offset < buffstart))
{
// weird
if (Verboseness == mpeg_VERBOSE)
fprintf(stderr, "could not get offset " _OFF_d " in file %s [" _OFF_x "]\n",
offset, FileName, FileSize);
RTRACK;
ATRACK("GetByte Invalid offset");
fprintf(stderr,"mpgtx: AT EOF - please stop me!\n");
return 0x11;
}
}
STOP_CLOCK(GETBYTE);
RTRACK;
return buffer[offset - buffstart];
}
byte mpeg::Byte(off_t offset){
return GetByte(offset); // because GetByte is inline
}
// same as above but improved for backward search
byte mpeg::bdGetByte(off_t offset){
BTRACK;
size_t nread;
if ((offset >= buffend) || (offset < buffstart)){
// new buffer needed (backward)
off_t start = offset-BUFFERSIZE+1 ;
start = start >= 0?start:0;
FSEEK(MpegFile, start, SEEK_SET);
nread = fread(buffer, 1, BUFFERSIZE, MpegFile);
buffstart = start;
buffend = start + nread;
if ((offset >= buffend) || (offset < buffstart))
{
// weird
if (Verboseness == mpeg_VERBOSE)
fprintf(stderr,"could not get offset " _OFF_x " in file %s [" _OFF_x "]\n",
offset, FileName, FileSize);
RTRACK;
ATRACK("bdGetByte Invalid offset");
return 0x11;
}
}
RTRACK;
return buffer[offset - buffstart];
}
// make sure the sequence 0x00 00 01 mark
//
bool mpeg::EnsureMPEG(off_t offset, marker mark){
BTRACK;
if (( GetByte(offset) == 0x00 ) &&
( GetByte(offset+1) == 0x00 ) &&
( GetByte(offset+2) == 0x01 ) &&
( GetByte(offset+3) == mark ))
return true;
else return false;
}
// get a two byte size (word)
//
unsigned short int mpeg::GetSize(off_t offset ) {
BTRACK;
return GetByte(offset)*256 + GetByte(offset+1);
}
// print ID3v2 tag to stdout
//
bool mpeg::PrintID3() {
BTRACK;
// Byte 0-2: ID3v2/file identifier "ID3"
if ( (GetByte(0)!='I') ||
(GetByte(1)!='D') ||
(GetByte(2)!='3'))
return false;
// Byte 3-4: ID3v2 version
printf(" ID3 v2.%d.%d tag (more info on http://www.id3.org/)\n",
GetByte(3), GetByte(4));
printf(" ----------------\n");
// Byte 5: ID3v2 flags (%abcd0000)
// Byte 6-9: ID3v2 tag size (4 * %0xxxxxxx)
size_t framesize;
off_t tagsize;
size_t i;
tagsize = GetByte(6)<<21;
tagsize |= GetByte(7)<<14;
tagsize |= GetByte(8)<<7;
tagsize |= GetByte(9);
#ifdef _DEBUG_
printf("Tag size : " _OFF_d "\n", tagsize);
#endif
bool handled;
off_t offset = 10;
while (offset < tagsize+10) {
if ((GetByte(offset)<'0') || (GetByte(offset)>'Z')) break;
handled = false;
framesize = (GetByte(offset+4)<<24) | (GetByte(offset+5)<<16) |
(GetByte(offset+6)<<8) | (GetByte(offset+7));
// print tag
printf(" %c%c%c%c : ", GetByte(offset), GetByte(offset+1),
GetByte(offset+2), GetByte(offset+3));
// T... - all text tags
if (GetByte(offset) == 'T') {
if (!GetByte(offset+10)) //if text encoding = standard encoding
for (i = 1; i < framesize; i++) {
printf("%c", GetByte(offset + i + 10));
handled = true;
}
if (!handled) {
printf("(empty)");
handled=true;
}
}
// COMM - Comments tag
if ( (GetByte(offset) =='C') &&
(GetByte(offset+1)=='O') &&
(GetByte(offset+2)=='M') &&
(GetByte(offset+3)=='M')) {
printf("(lang: %c%c%c) : ",
GetByte(offset+11), GetByte(offset+12), GetByte(offset+13));
for (i = 5; i < framesize; i++) {
printf("%c", GetByte(offset + i + 10));
handled = true;
}
if (!handled) {
printf("(empty)");
handled=true;
}
}
if (!handled) printf("(skipped)");
printf("\n");
offset += 10 + framesize;
}
printf(" ----------------\n");
return true;
}
// check if ID3v2 tag is present and audio follows
//
bool mpeg::ParseID3(){
BTRACK;
if ( (GetByte(0)!='I') ||
(GetByte(1)!='D') ||
(GetByte(2)!='3'))
return false;
start_with_id3 = true;
// get tag size
size_t tagsize=0;
tagsize = GetByte(6)<<21;
tagsize |= GetByte(7)<<14;
tagsize |= GetByte(8)<<7;
tagsize |= GetByte(9);
return ParseAudio(tagsize + 10);
}
// Parses Audio header returns false on not audio
//
bool mpeg::ParseAudio(off_t myoffset) {
BTRACK;
HasAudio = false;
bool mpeg2_5 = false;
int i;
// ensure that two first bytes are FFFx
// TODO: give it a chance and search for a valid start after the beginning
// even if most audio players will refuse to play such a file
if ((GetByte(myoffset+0)!=0xFF)||((GetByte(myoffset+1) & 0xF0)!=0xF0)) {
// doesn't start with 12 bits set
if ((GetByte(myoffset+0)!=0xFF)||((GetByte(myoffset+1) & 0xE0)!=0xE0)) {
// doesn't start with 11 bits set
return false;
} else {
// start with 11 bits set
mpeg2_5 = true;
}
}
// seems to be audio, let's allocate the struct
Audio=new mpgtx_audio;
Audio->tag=0;
Audio->first_frame_offset=myoffset;
// Find mpeg version 1.0 or 2.0
if(GetByte(myoffset + 1) & 0x08) {
if (!mpeg2_5) Audio->mpeg_ver = 1;
else return false; // invalid 01 encountered
} else {
if (!mpeg2_5) Audio->mpeg_ver = 2;
else Audio->mpeg_ver = 3; //for mpeg 2.5
}
// Find Layer
Audio->layer = (GetByte(myoffset + 1) & 0x06)>>1;
switch(Audio->layer) {
case 0: Audio->layer=-1; return false;
case 1: Audio->layer=3; break;
case 2: Audio->layer=2; break;
case 3: Audio->layer=1; break;
}
// Protection Bit
Audio->protect = GetByte(myoffset + 1) & 0x01;
if (Audio->protect) Audio->protect = 0;
else Audio->protect = 1;
// Bitrate index and sampling index to pass through the array
int bitrate_index = GetByte(myoffset+2)>>4;
int sampling_index = (GetByte(myoffset+2)&0x0f)>>2;
if (sampling_index >= 3) return false;
if (bitrate_index == 15) return false;
Audio->bitrate = mpeg_bitrate_index[Audio->mpeg_ver-1][Audio->layer-1][bitrate_index];
Audio->byte_rate = (float)((Audio->bitrate*1000)/8.0);
Audio->sampling_rate = mpeg_sampling_index[Audio->mpeg_ver-1][sampling_index];
// Padding bit
if (GetByte(myoffset+2)&0x02) Audio->padding = 1;
else Audio->padding = 0;
// Audio mode
Audio->mode=GetByte(myoffset+3)>>6;
if (Audio->mode==1) Audio->modext=(GetByte(myoffset+3)>>4)&0x03;
else Audio->modext=-1;
// Copyright bit
if(GetByte(myoffset+3)&0x08) Audio->copyright=true;
else Audio->copyright=false;
// Original/Copy bit
if(GetByte(myoffset+3)&0x04) Audio->original=true;
else Audio->original=false;
// emphasis index
Audio->emphasis_index=GetByte(myoffset+3)&0x03;
// Frame Length
if (Audio->mpeg_ver == 1) {
if (Audio->layer == 1) Audio->frame_length =
int((48000.0*Audio->bitrate)/Audio->sampling_rate) + 4*Audio->padding;
else Audio->frame_length =
int((144000.0*Audio->bitrate)/Audio->sampling_rate) + Audio->padding;
}
else if (Audio->mpeg_ver == 2) {
if (Audio->layer == 1) Audio->frame_length =
int((24000.0*Audio->bitrate)/Audio->sampling_rate) +4*Audio->padding;
else Audio->frame_length =
int((72000.0*Audio->bitrate)/Audio->sampling_rate) +Audio->padding;
}
else {
if (Verboseness == mpeg_VERBOSE)
fprintf(stderr,
"%s: Audio mpeg file layer invalid : "
"%d (should be 1 or 2)\n",
FileName,Audio->mpeg_ver);
return false;
}
// size of CRC
if (Audio->protect) Audio->frame_length += 2;
//Audio->frame_length += 4; // size of standard header
// let's get the id3 tag if present.
if ( GetByte(FileSize-128) == 'T' &&
GetByte(FileSize-127) == 'A' &&
GetByte(FileSize-126) == 'G') {
FSEEK(MpegFile, FileSize-125, SEEK_SET);
Audio->tag = new id3;
fread(Audio->tag, 1, sizeof(id3), MpegFile);
FileSize -= 128;
//empty unused fields
bool keep_it=false;
for ( i=0; i<30;i++) {
if (Audio->tag->name[i] != ' ') {
if (Audio->tag->name[i] != '\0')
keep_it = true;
break;
}
}
if (!keep_it) Audio->tag->name[0] = 0;
keep_it = false;
for ( i=0; i<30;i++){
if (Audio->tag->artist[i] != ' ') {
if (Audio->tag->artist[i] != '\0')
keep_it = true;
break;
}
}
if (!keep_it) Audio->tag->artist[0] = 0;
keep_it = false;
for ( i=0; i<30;i++) {
if (Audio->tag->album[i] != ' ') {
if (Audio->tag->album[i] != '\0')
keep_it = true;
break;
}
}
if (!keep_it) Audio->tag->album[0] = 0;
keep_it = false;
for ( i=0; i<4;i++){
if (Audio->tag->year[i] != ' ') {
if (Audio->tag->year[i] != '\0')
keep_it = true;
break;
}
}
if (!keep_it) Audio->tag->year[0] = 0;
keep_it = false;
for ( i=0; i<30;i++) {
if (Audio->tag->comment[i] != ' '){
if (Audio->tag->comment[i] != '\0')
keep_it = true;
break;
}
}
if (!keep_it) Audio->tag->comment[0] = 0;
}
Audio->duration = ((FileSize*1.0)/Audio->bitrate)*0.008;
HasAudio = true;
// fprintf(stderr, "found audio.\n");
return true;
}
//Parses Video header returns false on not video
bool mpeg::ParseVideo(off_t myoffset){
BTRACK;
// the MPEG video file should start with
// the video start sequence, if not exit.
// TODO: give it a chance and seek a video start elsewhere
if(!EnsureMPEG(myoffset, 0xB3)) return false;
off_t header_start=myoffset;
// seems to be video okay, let's allocate the struct
Video = new mpgtx_video;
Video->video_header = 0;
// Get vertical and horizontal picture sizes
myoffset += 4; // after video sequence code
Video->hsize = GetSize(myoffset) >> 4;
Video->vsize = GetSize(myoffset+1) & 0x0FFF;
// Get picture rate
myoffset += 3; // after picture sizes
int frame_rate_index = GetByte(myoffset) & 0x0F;
if (frame_rate_index > 8) {
// invalid frame rate index
if (Verboseness == mpeg_VERBOSE)
fprintf(stderr, "Invalid frame rate index in file %s : %d\n",
FileName, frame_rate_index);
Video->frame_rate = 0.0;
}
else Video->frame_rate = mpeg_frame_rate_index[frame_rate_index];
Video->aspect_ratio = (GetByte(myoffset) & 0xF0) >>4;
if (!Video->aspect_ratio) {
if (Verboseness == mpeg_VERBOSE)
fprintf(stderr, "Invalid aspect ratio in file %s : %d\n",
FileName, Video->aspect_ratio);
}
//Get Bitrate for duration estimation
myoffset += 1; // after picture rate
// 18 following bytes are the bitrate /400
//read first 16 bytes
Video->bitrate = GetSize(myoffset);
// scale
Video->bitrate <<= 2;
byte lasttwo = GetByte(myoffset + 2);
lasttwo >>= 6;
Video->bitrate |= lasttwo;
//Given bitrate and FileSize, compute an estimation of the duration.
Video->duration = (FileSize*8.0) / (Video->bitrate*400);
//////////// Begin changes
marker mymark;
while (true) {
myoffset = FindNextMarker(myoffset, &mymark);
if (mymark == 0xB8) break;
switch (GetByte(myoffset + 3)) {
case 0xB5 :
ParseExtension(myoffset);
break;
case 0xB2 :
#ifdef _DEBUG_
printf("user data start code found\n");
#endif
ParseUserData(myoffset);
break;
}
myoffset++;
}
///////////// end changes
//Okay let's save the video sequence header somewhere
// find first GOP after video sequence header
off_t header_end = FindNextMarker(myoffset, 0xB8);
Video->first_gop_offset = header_end;
// did we find header end or are we lost in cyberspace?
if (header_end < 0){
//could not find first GOP
if(Verboseness == mpeg_VERBOSE)
fprintf(stderr,
"%s: could not find first GOP after Video Sequence start [" _OFF_x " (decimal)]\n",
FileName, header_start);
return false;
}
// okay we have header boundaries, let's fill the appropriate
// field.
Video->video_header_size = header_end - header_start;
Video->video_header = new byte[Video->video_header_size];
// fprintf(stderr,"philipp found header: 0x%qx 0x%qx\n", header_end, header_start);
FSEEK(MpegFile, header_start, SEEK_SET);
if(fread(Video->video_header, Video->video_header_size, 1, MpegFile) != 1) {
// couldn't read video header
if (Verboseness == mpeg_VERBOSE)
fprintf(stderr,
"%s: Found video header but couldn't read it [" _OFF_x "-" _OFF_x "]\n",
FileName, header_start, header_end);
return false;
}
HasVideo = true;
return true;
}
//Parses System header returns false on not system
bool mpeg::ParseSystem() {
BTRACK;
off_t offset = 0;
off_t PACKlength = 0;
unsigned short packetsize;
byte packettype;
int found = 0;
// the MPEG system file should start with
// the video start sequence, if not exit.
// TODO: give it a chance and seek a PACK elsewhere
// try to skip any zeros at mpeg start (some begin with a BUNCH of zeros
while(GetByte(offset)== 0x00) offset ++;
//here we're on the first non null byte let's get back to leave two zeros (packet start code)
offset -=2;
if (offset != 0) {
// we actually skipped some zeroes
fprintf(stderr,"Skipped " _OFF_d " zeroes at start of file\n",offset);
}
if (!EnsureMPEG(offset, 0xBA)){
//this file does not begin with a pack!
fprintf(stderr,"mmm, this file does not start with a pack, offset: " _OFF_d " \n",offset);
fprintf(stderr,"use the desperate_mode switch as the first option -X to search for a header in the whole file!\n");
fprintf(stderr,"if you want to force the operation. May yield to an endless loop if no valid header is found!\n");
if (GetByte(offset+2) != 0x01) {
fprintf(stderr,"Does not even begin with a 00 00 01 xx sequence!\n");
// does not even begin with a 00 00 01 xx sequence !
if (desperate_mode)
{
// philipp tries here to search further ....
offset +=3;
while (found == 0) {
if (print_progress) {
fprintf(stderr,"Searching for a mpeg signature:\n");
fprintf(stderr,"Searching for 0x00 at offset: ");
}
while (GetByte(offset) != 0x00) {
if (print_progress) fprintf (stderr,"\b\b\b\b\b\b\b\b\b\b%10qd", offset);
offset++;
}
if (print_progress) fprintf(stderr,"\nSearching for != 0x00 at offset: ");
while (GetByte(offset) == 0x00) {
offset++;
if (print_progress) fprintf (stderr,"\b\b\b\b\b\b\b\b\b\b%10qd", offset);
}
// now from the beginning ..
offset -=2;
if (!EnsureMPEG(offset, 0xBA)){
if (print_progress) fprintf(stderr,"\nno success......\n");
offset += 3;
}
else {
fprintf(stderr,"\nsuccess! at " _OFF_d " \n",offset);
found = 1;
fprintf(stderr,"I dont know which kind of crazy header you have put into this file!\n");
if (!preserve_header) {
fprintf(stderr,"But I will assume this is ok. If you split or join this file, it will be discarded\n");
}
else {
fprintf(stderr,"But I will assume this is ok. And if you split this file, it will be inserted\n");
}
fprintf(stderr,"Before any segment. This movie maybe wil not play with quicktime, use Videolan instead\n");
fprintf(stderr,"If you dont want this, please correct the file, so its start with a valid Pack!\n");
}
}
}
if (found == 0) {
fprintf(stderr,"\nNo success at all.\n");
return false;
}
}
}
// allocate necessary space
System = new mpgtx_system;
System->audio_system_header = 0;
System->video_system_header = 0;
System->first_video_packet = 0;
// tries to find the two system packets and store them
bool keep_going = true;
marker mark = 0;
// fprintf(stderr, "Philipp: Searching for system packets (audio, video).\n");
while (keep_going)
{
// fprintf(stderr,"philipp: offset4 ?: 0x%qx\n",offset);
offset = FindNextMarker(offset, &mark);
// fprintf(stderr,"philipp: offset5 ?: 0x%qx\n",offset);
// if error exit
if (offset == -1) {
fprintf(stderr, "offset error (-1)\n");
break;
}
// if this is a Video or Audio packet we're done with system
if ((mark == VideoPkt) || (mark == AudioPkt)) break;
// if this is a padding packet
if (mark == PaddingPkt) {
offset += GetSize(offset + 4);
continue;
}
// if this is a PACK
if (mark==0xBA) {
System->muxrate = ReadPACKMuxRate(offset + 4);
//standard pack length
offset += 12;
continue;
}
//Hey no more guess...
if (mark != SystemPkt) {
if (Verboseness == mpeg_VERBOSE)
fprintf(stderr,
"%s: Unhandled packet encountered (%x @" _OFF_x ") while seeking system headers\n",
FileName, mark, offset);
offset += 4;
continue;
}
// it has to be a system packet
// March 27 changed because of variable length PACK in mpeg 2
// are we lost?
// fprintf(stderr,"philipp: offset3 ?: 0x%qx\n",offset);
off_t startofpack = bdFindNextMarker(offset, 0xBA);
// fprintf(stderr,"philipp: offset2 ?: 0x%qx\n",offset);
if (startofpack !=-1){
//found a PACK before system packet, compute its size (mpeg1 != mpeg2)
if ((GetByte(startofpack + 4) & 0xF0) == 0x20) {
// standard mpeg1 pack
PACKlength=12;
}
else {
if ((GetByte(startofpack + 4) & 0xC0) == 0x40){
// new mpeg2 pack : 14 bytes + stuffing
PACKlength = 14 + (GetByte(startofpack + 13) & 0x07);
}
else{
// wazup?
fprintf(stderr, "Weird PACK encountered\n");
PACKlength = 12;
}
}
}
if ((startofpack == -1) || (startofpack + PACKlength != offset)) {
// we're probably lost
fprintf(stderr,
"%s: System Packet not preceded by a PACK [" _OFF_x
"] start of pack : " _OFF_x " PACKlength : " _OFF_x
" I'll probably crash but I love risk\n",
FileName, offset,startofpack,PACKlength);
startofpack=offset;
// keep going anyway return false;
}
// end of modif.
ParseSystemPacket(offset,startofpack);
packetsize = GetSize(offset + 4); // to size field
packettype = GetByte(offset + 12); // to ccode field
if (GetByte(offset + 15) == AudioPkt || GetByte(offset + 15) == VideoPkt) {
// system packet with both audio and vid infos
packettype = VideoPkt; // since video is mandatory
}
if (packettype == AudioPkt){
if (System->audio_system_header != 0){
if (Verboseness == mpeg_VERBOSE)
fprintf(stderr,
"%s: Warning two or more audio sys header encountered [" _OFF_x "]\n",
FileName, offset);
delete[] System->audio_system_header;
}
System->audio_system_header_length = PACKlength + 4 + 2 + packetsize;
System->audio_system_header = new byte [System->audio_system_header_length];
FSEEK(MpegFile, offset-PACKlength, SEEK_SET);
fread(
System->audio_system_header,
System->audio_system_header_length,
1, MpegFile);
}
else if (packettype == VideoPkt) {
if (System->video_system_header != 0) {
if (Verboseness == mpeg_VERBOSE)
fprintf(stderr,
"%s: Warning two or more video sys header encountered [" _OFF_x "]\n",
FileName, offset);
delete[] System->video_system_header;
}
// does this video packet begin
System->video_system_header_length =
PACKlength + 4 + 2 + packetsize;
System->video_system_header =
new byte [System->video_system_header_length];
// keep track of the initial timestamp
if (PACKlength == 12) {
#ifdef _DEBUG_
fprintf(stderr,"Now read mpeg initial_TS at [" _OFF_x "] \n",offset - PACKlength);
fprintf(stderr,"Should all TS be printed? [n/y]:");
if (getchar() == 'y')
print_all_ts(0xba);
while(getchar() != '\n')
;
#endif
System->initial_TS = ReadTS(offset - PACKlength);
mpeg2found = 0;
}
else {
// off_t firstmpeg2gop = FindNextMarker(1, 0xB8);
#ifdef _DEBUG_
fprintf(stderr,"Now read mpeg2 initial_TS at [" _OFF_x "] \n",offset - PACKlength);
fprintf(stderr,"Should all TS be printed? [n/y]:");
if (getchar() == 'y')
print_all_ts(0xba);
while(getchar() != '\n')
;
#endif
mpeg2found = 1;
// System->initial_TS = ReadTSMpeg2(firstmpeg2gop + 4);
System->initial_TS = ReadTSMpeg2(offset - PACKlength);
}
FSEEK(MpegFile, (offset - PACKlength), SEEK_SET);
fread(
System->video_system_header,
System->video_system_header_length,
1, MpegFile);
}
else {
if (Verboseness==mpeg_VERBOSE)
fprintf(stderr, "%s: Unknown system packet %x [" _OFF_x "]\n",
FileName, packettype, offset);
}
offset += 4;
}
//okay this is a miracle but we have what we wanted here.
// hey wait! are we really okay?
if (!System->video_system_header) {
//GOSH! we don't have video here!
if (Verboseness == mpeg_VERBOSE)
fprintf(stderr,
"%s: didn't find any video system header in this mpeg system file\n", FileName);
// return false;
fprintf(stderr,
"warning: couldn't find any valid system header. I'm continuing anyway\n");
}
// let's go on and find audio and video infos
// Video!
offset = FindNextMarker(0, 0xB3);
// fprintf(stderr,"philipp: offset1 ?: 0x%qx\n",offset);
if (offset == -1) {
//couldn't find any valid video
if (Verboseness == mpeg_VERBOSE)
fprintf(stderr,
"%s: didn't find any video sequence header in this mpeg system file\n", FileName);
return false;
}
// mkay, we have the video sequence header
if (!ParseVideo(offset)) {
return false;
}
off_t xB3_offset = offset;
// now get the pack and the packet header just before the video sequence
offset = bdFindNextMarker(offset, 0xBA);
if (offset == -1) {
//couldn't find any PACK before...
if (Verboseness == mpeg_VERBOSE)
fprintf(stderr,
"%s: didn't find any PACK before video sequence\n", FileName);
return false;
}
// May 2, PACK don't necessarily precede video sequence!
off_t start_of_vid_packet = bdFindNextMarker(xB3_offset, 0xE0);
// start of packet
off_t offset2 = bdFindNextMarker(start_of_vid_packet-1, &mark);
if (offset != offset2) {
// the packet that precedes video sequence is not a PACK
offset = start_of_vid_packet;
}
else {
// end May 2
// March 27 compute pack length
if ((GetByte(offset + 4) & 0xF0) == 0x20) {
//standard mpeg1 pack
PACKlength = 12;
}
else {
if ((GetByte(offset + 4) & 0xC0) == 0x40) {
//new mpeg2 pack : 14 bytes + stuffing
PACKlength = 14 + (GetByte(offset + 13) & 0x07);
}
else {
//wazup?
fprintf(stderr,"Weird PACK encountered\n");
PACKlength = 12;
}
}
// end March 27
// May 2
}
//end May 2
// okay let's save this...
System->first_video_packet = new byte[xB3_offset-offset];
System->first_video_packet_length = xB3_offset-offset;
readHeader(MpegFile,offset,1); // save this if needed for later
FSEEK(MpegFile,offset,SEEK_SET);
fread(System->first_video_packet, 1, xB3_offset-offset, MpegFile);
// Now look for an audio packet if necessary
HasAudio = false;
offset = FindNextMarker(0, AudioPkt);
if ((offset == -1) || (offset > FileSize - 15)) {
if (Verboseness == mpeg_VERBOSE)
fprintf(stderr,
"%s: didn't find any audio packet in this mpeg system file\n",FileName);
} else {
// we assume that we are on the first audio packet...
offset = SkipPacketHeader(offset);
if (!ParseAudio(offset)) {
//mmm audio packet doesn't begin with FFF
while ((offset<FileSize - 10) && !HasAudio) {
if ((GetByte(offset) == 0xFF) && (GetByte(offset + 1) & 0xF0) == 0xF0)
if (ParseAudio(offset))
HasAudio = true;
offset++;
} //if
} //else
} //while
//seek the file duration by fetching the last PACK
//and reading its timestamp
// [es] it seems a bad idea on joined MPEGs, where
// timestamps counting began on every cut :-(
off_t last_pack = bdFindNextMarker(FileSize - 13, 0xBA);
// -12 because a PACK is at least 12 bytes
double duration;
if ((GetByte(last_pack + 4) & 0xF0) == 0x20){
//standard mpeg1 pack
duration = ReadTS(last_pack + 4);
}
else {
last_pack = bdFindNextMarker(FileSize - 8, 0xB8);
if(((GetByte(last_pack + 4) & 0xC0) == 0x40) || (mpeg2found == 1)) {
//new mpeg2 pack : 14 bytes + stuffing
#ifdef _DEBUG_
fprintf(stderr,"now getting mpeg2 running time");
#endif
last_pack = bdFindNextMarker(FileSize - 8, 0xBA);
duration = ReadTSMpeg2(last_pack + 4);
#ifdef _DEBUG_
fprintf(stderr," : %lf\n", duration);
#endif
}
else {
//wazup?
duration = ReadTS(last_pack + 4);
}
}
duration -= System->initial_TS;
if (HasVideo) Video->duration = duration;
if (HasAudio) Audio->duration = duration;
//okay okay this is redundant
return true;
}
// print mpeg infos on stdout
void mpeg::PrintInfos() {
BTRACK;
char HMS[30];
if (MpegType == mpeg_AUDIO){
SecsToHMS(HMS, Audio->duration);
printf("%s\n", FileName);
if (Audio->mpeg_ver != 3)
printf(" Audio : Mpeg %d layer %d\n", Audio->mpeg_ver,Audio->layer);
else
printf(" Audio : Mpeg 2.5 (rare) layer %d\n", Audio->layer);
printf(" Estimated Duration: %s\n", HMS);
if (Audio->bitrate) {
printf(" %d kbps %d Hz\n", Audio->bitrate,Audio->sampling_rate);
printf(" Frame size: %d bytes\n", Audio->frame_length);
}
else {
printf(" free bitrate %d Hz (this file is currently unsplitable)\n",Audio->sampling_rate);
}
switch (Audio->mode) {
case 0: printf(" Stereo,"); break;
case 1: printf(" Joint Stereo: ");
if (Audio->layer==1 || Audio->layer==2) {
switch (Audio->modext) {
case 0: printf("(Intensity stereo on bands 4-31/32)\n"); break;
case 1: printf("(Intensity stereo on bands 8-31/32)\n"); break;
case 2: printf("(Intensity stereo on bands 12-31/32)\n"); break;
case 3: printf("(Intensity stereo on bands 16-31/32)\n"); break;
}
}
else {
// mp3
switch (Audio->modext){
case 0: printf("(Intensity stereo off, M/S stereo off)\n"); break;
case 1: printf("(Intensity stereo on, M/S stereo off)\n"); break;
case 2: printf("(Intensity stereo off, M/S stereo on)\n"); break;
case 3: printf("(Intensity stereo on, M/S stereo on)\n"); break;
}
}
break;
case 2: printf(" Dual Channel,"); break;
case 3: printf(" Mono,"); break;
}
switch (Audio->emphasis_index){
case 0: printf(" No emphasis,"); break;
case 1: printf(" Emphasis: 50/15 microsecs,"); break;
case 2: printf(" Emphasis Unknown,"); break;
case 3: printf(" Emphasis CCITT J 17,"); break;
}
if (Audio->copyright) printf(" (c),");
if (Audio->original) printf(" original\n");
else printf(" copy\n");
if (Audio->tag) {
//print id3v1 tag----------------
if (Audio->tag->comment[28] == 0)
printf (" ID3 v1.1 tag\n ----------------\n");
else
printf(" ID3 v1.0 tag\n ----------------\n");
if (Audio->tag->name[0])
printf(" title : %.30s\n", Audio->tag->name);
if (Audio->tag->artist[0])
printf(" artist : %.30s\n", Audio->tag->artist);
if (Audio->tag->album[0]) {
printf(" album : %.30s\n", Audio->tag->album);
if(Audio->tag->comment[28]==0)
printf(" track : %d\n", Audio->tag->comment[29]);
}
if (Audio->tag->year[0])
printf(" year : %.4s\n", Audio->tag->year);
if (Audio->tag->comment[0])
printf(" comment : %.30s\n", Audio->tag->comment);
if (Audio->tag->genre < MAX_ID3_GENRE)
printf(" genre : %s\n", genre[Audio->tag->genre]);
printf(" ----------------\n");
}
if (start_with_id3) PrintID3();
printf("\n");
return;
}
if (MpegType == mpeg_VIDEO) {
SecsToHMS(HMS, Video->duration);
printf("%s\n", FileName);
printf(" Mpeg %d Video File\n Estimated Duration: %s\n", mpeg_version, HMS);
switch(Video->aspect_ratio) {
case 0: printf(" Invalid aspect ratio (forbidden)\n"); break;
case 1: printf(" Aspect ratio 1/1 (VGA)\n"); break;
case 2: printf(" Aspect ratio 4/3 (TV)\n"); break;
case 3: printf(" Aspect ratio 16/9 (large TV)\n"); break;
case 4: printf(" Aspect ratio 2.21/1 (Cinema)\n"); break;
#ifdef _DEBUG_
default: printf(" Invalid Aspect ratio (reserved)\n");
#endif
}
if (SExt) {
if (SExt->progressive) printf(" Not interlaced, chroma format: ");
else printf(" Interlaced, chroma format: ");
switch (SExt->chroma_format) {
case 1 : printf ("4:2:0\n"); break;
case 2 : printf ("4:2:2\n"); break;
case 3 : printf ("4:4:4\n"); break;
}
}
if (DExt){
switch (DExt->video_format) {
case 0 : printf (" Video Format: Component\n"); break;
case 1 : printf (" Video Format: PAL\n"); break;
case 2 : printf (" Video Format: NTSC\n"); break;
case 3 : printf (" Video Format: SECAM\n"); break;
case 4 : printf (" Video Format: MAC\n"); break;
case 5 : printf (" Video Format: Unspecified\n"); break;
}
if ( (DExt->h_display_size != Video->hsize) ||
(DExt->v_display_size != Video->vsize))
printf(" Display Size [%ld x %ld]\n",
DExt->h_display_size,
DExt->v_display_size);
}
printf(" Size [%ld x %ld] %0.2f fps %.2f Mbps\n",
Video->hsize,Video->vsize,
Video->frame_rate,
Video->bitrate/2500.0);
if (UData)
printf("\nUser Data:\n------------\n%s------------\n\n",UData->ud);
return;
}
if (MpegType == mpeg_SYSTEM ) {
if (composite) {
printf(" Mpeg System File [%d Video/ %d Audio]\n", n_video,n_audio);
}
else {
if (HasVideo) {
SecsToHMS(HMS, Video->duration);
printf("%s\n", FileName);
if (mpeg_version == 1) {
printf(" Mpeg 1 System File [Video");
}
else {
// TODO: [es] What is this if-clause for?
if (mpeg_version!=2);
printf(" Mpeg 2 Program Stream File [Video");
}
if (HasAudio) printf("/Audio]\n");
else printf("]\n");
printf(" Muxrate : %.2f Mbps\n", (System->muxrate * 8.0) / 1000000);
printf(" Estimated Duration: %s\n", HMS);
if (print_progress) {
// do time stamps continuing increase?
int tlbreaks = 0;
printf(" Checking all time stamps (This may take a while.) ...\n");
if ( (tlbreaks = CheckTimeline(0xBA)) ) {
printf(" Time line is interupted at %d times.\n",tlbreaks);
printf(" That means at these points the time stamp is earlier than the previous one.\n");
printf(" You may have problems in joining and splitting this file, sorry.\n");
}
else
printf(" Time line seems to be ok.\n");
}
switch (Video->aspect_ratio) {
case 0: printf(" Invalid aspect ratio (forbidden)\n"); break;
case 1: printf(" Aspect ratio 1/1 (VGA)\n"); break;
case 2: printf(" Aspect ratio 4/3 (TV)\n"); break;
case 3: printf(" Aspect ratio 16/9 (large TV)\n"); break;
case 4: printf(" Aspect ratio 2.21/1 (Cinema)\n"); break;
#ifdef _DEBUG_
default: printf(" Invalid Aspect ratio (reserved)\n");
#endif
}
if (SExt){
if (SExt->progressive) printf(" Not interlaced, chroma format: ");
else printf(" Interlaced, chroma format: ");
switch (SExt->chroma_format){
case 1 : printf ("4:2:0\n");break;
case 2 : printf ("4:2:2\n");break;
case 3 : printf ("4:4:4\n");break;
}
}
if (DExt){
switch(DExt->video_format){
case 0 : printf (" Video Format: Component\n");break;
case 1 : printf (" Video Format: PAL\n");break;
case 2 : printf (" Video Format: NTSC\n");break;
case 3 : printf (" Video Format: SECAM\n");break;
case 4 : printf (" Video Format: MAC\n");break;
case 5 : printf (" Video Format: Unspecified\n");break;
}
if ((DExt->h_display_size != Video->hsize)||
(DExt->v_display_size != Video->vsize))
printf(" Display Size [%ld x %ld]\n",
DExt->h_display_size,DExt->v_display_size);
}
printf(" Size [%ld x %ld] %0.2f fps %.2f Mbps\n",
Video->hsize,Video->vsize,
Video->frame_rate,
(Video->bitrate/2500.0));
}
if(UData) printf("\nUser Data:\n------------\n%s------------\n\n",UData->ud);
if (HasAudio){
if (Audio->mpeg_ver !=3) printf(" Audio : Mpeg %d layer %d\n",Audio->mpeg_ver,Audio->layer);
else printf(" Audio : Mpeg 2.5 (rare) layer %d\n",Audio->layer);
if (Audio->bitrate) printf(" %d kbps %d Hz\n",Audio->bitrate,Audio->sampling_rate);
else printf(" free bitrate %d Hz\n",Audio->sampling_rate);
switch (Audio->mode){
case 0: printf(" Stereo,"); break;
case 1: printf(" Joint Stereo: ");
if (Audio->layer==1 || Audio->layer==2){
switch (Audio->modext){
case 0: printf("(Intensity stereo on bands 4-31/32)\n"); break;
case 1: printf("(Intensity stereo on bands 8-31/32)\n"); break;
case 2: printf("(Intensity stereo on bands 12-31/32)\n"); break;
case 3: printf("(Intensity stereo on bands 16-31/32)\n"); break;
}
}
else {
//mp3
switch (Audio->modext){
case 0: printf("(Intensity stereo off, M/S stereo off)\n"); break;
case 1: printf("(Intensity stereo on, M/S stereo off)\n"); break;
case 2: printf("(Intensity stereo off, M/S stereo on)\n"); break;
case 3: printf("(Intensity stereo on, M/S stereo on)\n"); break;
}
}
break;
case 2: printf(" Dual Channel,"); break;
case 3: printf(" Mono,"); break;
}
switch (Audio->emphasis_index){
case 0: printf(" No emphasis\n"); break;
case 1: printf(" Emphasis: 50/15 microsecs\n"); break;
case 2: printf(" Emphasis Unknown\n"); break;
case 3: printf(" Emphasis CCITT J 17\n"); break;
}
}
}
return;
}
if (MpegType== mpeg_TRANSPORT){
printf("%s\n",FileName);
if (!Transport) {
fprintf(stdout," Invalid MPEG 2 Transport Stream\n");
return;
}
Transport->PrintInfos();
return;
}
printf("%s can not be handled by this program\n",FileName);
return;
} //PrintInfos
// convert a time in second to HH:mm:ss notation
//
void mpeg::SecsToHMS(char* HMS, float duration){
BTRACK;
byte hours = (byte)(duration / 3600);
byte mins = (byte)((duration / 60) - (hours * 60));
float secs = duration - 60 * mins - 3600 * hours;
if (hours != 0) {
sprintf (HMS,"%02d:%02d:%05.2fs",hours,mins,secs);
return;
}
if (mins != 0) {
sprintf(HMS,"%02d:%05.2fs",mins,secs);
return;
}
sprintf(HMS,"%05.2fs",secs);
}
// find next 0x 00 00 01 xx sequence, returns offset or -1 on err
//
off_t mpeg::FindNextMarker(off_t from){
BTRACK;
off_t offset;
for (offset = from; offset < (FileSize - 4); offset++)
{
if (
(GetByte(offset + 0) == 0x00) &&
(GetByte(offset + 1) == 0x00) &&
(GetByte(offset + 2) == 0x01))
{
RTRACK;
return offset;
}
}
RTRACK;
return -1;
}
// find next 0x 00 00 01 xx sequence, returns offset or -1 on err and
// change mark to xx
//
off_t mpeg::FindNextMarker(off_t from, marker* mark){
BTRACK;
off_t offset = FindNextMarker(from);
if (offset >= 0) {
*mark = GetByte(offset + 3);
RTRACK;
return offset;
}
else {
RTRACK;
return -1;
}
}
// find next 0X00 00 01 mark
//
off_t mpeg::FindNextMarker(off_t from, marker mark){
BTRACK;
off_t offset = from;
while(offset >= 0){
offset = FindNextMarker(offset);
if (offset == -1) {
RTRACK;
return -1;
}
if (EnsureMPEG(offset, mark)) {
RTRACK;
return offset;
}
else offset++;
}
//shouldn't be here
RTRACK;
return -1;
}
// same as above but optimized for backward search
//
off_t mpeg::bdFindNextMarker(off_t from, marker mark) {
BTRACK;
off_t offset;
for( offset = from; offset >= 0; offset--)
{
if(
(bdGetByte(offset) == 0x00) &&
(bdGetByte(offset+1) == 0x00) &&
(bdGetByte(offset+2) == 0x01) &&
(bdGetByte(offset+3) == mark))
{
RTRACK;
return offset;
}
}
RTRACK;
return -1;
}
off_t mpeg::bdFindNextMarker(off_t from, marker* mark) {
BTRACK;
off_t offset;
for (offset = from; offset >= 0; offset--)
{
if ( (bdGetByte(offset) ==0x00) &&
(bdGetByte(offset+1)==0x00) &&
(bdGetByte(offset+2)==0x01))
{
*mark=bdGetByte(offset+3);
RTRACK;
return offset;
}
}
RTRACK;
return -1;
}
// read Time Stamp at given offset
// NOTE: The guys at MPEG are really mad.
// look at what we have to do to get a timestamp :/
double mpeg::ReadTS(off_t offset) {
BTRACK;
byte highbit;
unsigned long low4Bytes;
double TS;
#ifdef _DEBUG_
// fprintf(stderr,"Offset: [" _OFF_x "] The bytes...: %2x %2x %2x %2x %2x\n",offset,GetByte(offset + 0),GetByte(offset + 1),GetByte(offset + 2),GetByte(offset + 3),GetByte(offset + 4) );
#endif
highbit = (GetByte(offset) >> 3) & 0x01;
low4Bytes = ((GetByte(offset) >> 1) & 0x03) << 30;
low4Bytes |= GetByte(offset + 1) << 22;
low4Bytes |= (GetByte(offset + 2) >> 1) << 15;
low4Bytes |= GetByte(offset + 3) << 7;
low4Bytes |= GetByte(offset + 4) >> 1;
#define FLOAT_0x10000 (double)((unsigned long)1 << 16)
#define STD_SYSTEM_CLOCK_FREQ (unsigned long)90000
TS = (double)(highbit * FLOAT_0x10000 * FLOAT_0x10000);
TS += (double)(low4Bytes);
TS /= (double)(STD_SYSTEM_CLOCK_FREQ);
#ifdef _DEBUG_
// printf("read TS : %lf, base : 0x%d,%032lx\n",TS,highbit,low4Bytes);
#endif
return TS;
}
double mpeg::ReadTSMpeg2(off_t offset){
BTRACK;
byte highbit;
unsigned long low4Bytes;
unsigned long sys_clock_ref;
double TS;
#ifdef _DEBUG_
// fprintf(stderr,"Offset: [" _OFF_x "] The bytes...: %2x %2x %2x %2x %2x %2x",offset,GetByte(offset + 0),GetByte(offset + 1),GetByte(offset + 2),GetByte(offset + 3),GetByte(offset + 4),GetByte(offset + 5) );
#endif
highbit = (GetByte(offset)&0x20)>>5;
low4Bytes = ((GetByte(offset) & 0x18) >> 3) << 30;
low4Bytes |= (GetByte(offset) & 0x03) << 28;
low4Bytes |= GetByte(offset + 1 ) << 20;
low4Bytes |= (GetByte(offset + 2 ) & 0xF8) << 12;
low4Bytes |= (GetByte(offset + 2 ) & 0x03) << 13;
low4Bytes |= GetByte(offset + 3 ) << 5;
low4Bytes |= (GetByte(offset + 4 )) >> 3;
sys_clock_ref=(GetByte(offset + 4) & 0x3) << 7;
sys_clock_ref|=(GetByte(offset + 5) >> 1);
TS = (double)(highbit * FLOAT_0x10000 * FLOAT_0x10000);
TS += (double)(low4Bytes);
if (sys_clock_ref ==0) TS /= (double)(STD_SYSTEM_CLOCK_FREQ);
else {
// this is what I understood... CHECK
// if not zero, then we have a 27 MHz accuracy with a max of 27 MHz
// so clockfreq might well be 27MHz / sys_clock_ref
// nonsense: TS /= (double)(27000000 / sys_clock_ref);
TS /= (double)(STD_SYSTEM_CLOCK_FREQ);
}
//debug
#ifdef _DEBUG_
// printf("read TS2 : %lf, base : 0x%d,%032lx ref : %016lx\n",TS,highbit,low4Bytes,sys_clock_ref);
#endif
return TS;
}
off_t mpeg::SkipPacketHeader(off_t myoffset){
BTRACK;
byte mybyte;
if (mpeg_version == 1) {
// skip startcode and packet size
myoffset += 6;
//remove stuffing bytes
mybyte = GetByte(myoffset);
while (mybyte & 0x80) mybyte = GetByte(++myoffset);
if ((mybyte & 0xC0) == 0x40) // next two bits are 01
myoffset += 2;
mybyte=GetByte(myoffset);
if ((mybyte & 0xF0) == 0x20) myoffset += 5;
else if ((mybyte & 0xF0) == 0x30) myoffset += 10;
else myoffset++;
return myoffset;
}
else if (mpeg_version==2) {
// this is a PES, easyer
// offset + 9 is the header length (-9)
return (myoffset + 9 +GetByte(myoffset + 8));
}
else return (myoffset + 10);
}
bool mpeg::ParseSystemPacket(off_t startofpacket, off_t startofpack){
BTRACK;
int size = Read2Bytes(startofpacket + 4);
int i;
byte code;
size -= 6;
//TODO check if there's already a system packet
if ((size%3) != 0) return false;
#ifdef _DEBUG_
else printf("%d streams found\n", size/3);
#endif
for (i=0; i < size/3; i++) {
code = GetByte(startofpacket + 12 + i * 3);
#ifdef _DEBUG_
printf("%02x ",code);
#endif
if ((code&0xF0)==0xC0)
n_audio++;
else if ((code&0xF0)==0xE0 || (code & 0xF0)==0xD0)
n_video++;
}
if (n_audio) {
HasAudio = true;
if (n_audio > 1) {
composite = true;
editable = false;
}
}
if (n_video) {
HasVideo = true;
if (n_video > 1){
composite = true;
editable = false;
}
}
#ifdef _DEBUG_
printf("\n audio %d video %d \n",n_audio,n_video);
#endif
return true;
}
void mpeg::ParseFramesInGOP(off_t offset){
BTRACK;
int pict_ref;
int pict_type;
char type;
marker mark;
off_t off = offset + 1;
off = FindNextMarker(off , &mark);
while (off > 0) {
switch (mark) {
case 0xB8 :
printf("GOP ends at [" _OFF_x "]\n",off);
return;
case 0x00 :
//picture
pict_ref=(GetByte(off+4)<<2);
pict_ref |= ((GetByte(off+5)&0xC0)>>6);
pict_type = ((GetByte(off+5)&0x38)>>3);
switch (pict_type){
case 1: type ='I';break;
case 2: type ='P';break;
case 3: type ='B';break;
default:type ='U';break;
}
printf("\n\n%c (%.2d) [" _OFF_x "]\n", type, pict_ref, off);
break;
case 0xba: printf(" PACK [" _OFF_x "]\n", off); break;
case VideoPkt: printf(" Video [" _OFF_x "]\n", off); break;
case AudioPkt: printf(" Audio [" _OFF_x "]\n", off); break;
case PaddingPkt: printf(" Padding [" _OFF_x "]\n", off); break;
}
off++;
off=FindNextMarker(off,&mark);
}
}
bool mpeg::ParseExtension(off_t myoffset) {
BTRACK;
myoffset+=4;
switch (GetByte(myoffset) >> 4) {
case 1: return ParseSequenceExt(myoffset);
case 2: return ParseSequenceDisplayExt(myoffset);
}
return false;
}
bool mpeg::ParseUserData(off_t myoffset) {
BTRACK;
off_t end = FindNextMarker(myoffset + 1);
off_t i;
bool allprintable=true;
byte car;
off_t mysize = end - myoffset - 4;
if (mysize <= 0) return false;
for (i = myoffset+4 ; i < end; i++) {
car=GetByte(i);
if ((car < 0x20) && (car != 0x0A) && (car != 0x0D)) {
allprintable = false;
break;
}
}
if (allprintable) {
if (!UData) {
UData = new user_data;
UData->ud = new char[1];
UData->ud[0] = 0;
UData->ud_length = 1;
}
char* temp = new char[mysize+UData->ud_length+1]; //1 is for \n
//copy all until \0
for ( i=0; i < UData->ud_length-1; i++){
temp[i] = UData->ud[i];
}
//okay now copy the new string
for ( i=0; i < mysize; i++){
temp[i + UData->ud_length -1] = GetByte(myoffset + 4 + i);
}
temp[UData->ud_length- 1 + mysize] = '\n';
temp[UData->ud_length + mysize] = '\0';
UData->ud_length += mysize + 1;
delete[] UData->ud;
UData->ud = temp;
}
return true;
}
bool mpeg::ParseSequenceExt(off_t myoffset) {
BTRACK;
unsigned long hsize;
unsigned long vsize;
unsigned long bitrate;
// We are an mpeg 2 file
mpeg_version=2;
if (!SExt) SExt = new sequence_ext;
//13TH
if (GetByte(myoffset + 1) & 0x08) SExt->progressive = true;
SExt->chroma_format = (GetByte(myoffset + 1) & 0x06) >> 1;
hsize = (GetByte(myoffset + 1) & 0x01) << 1;
hsize |= (GetByte(myoffset + 2) & 80) >> 7;
hsize <<= 12;
if (!Video) return false;
Video->hsize |= hsize;
vsize = (GetByte(myoffset+2)&0x60) << 7;
Video->vsize |= vsize;
bitrate = (GetByte(myoffset + 2) & 0x1F) << 7;
bitrate |= (GetByte(myoffset+3)&0xFE) >> 1;
bitrate <<= 18;
Video->bitrate |= bitrate;
if (GetByte(myoffset + 5) & 0x80) SExt->low_delay = true;
else SExt->low_delay = false;
byte frate_n = (GetByte(myoffset + 5) & 0x60) >> 5;
byte frate_d = (GetByte(myoffset + 5) & 0x1F);
frate_n++;
frate_d++;
Video->frame_rate = (Video->frame_rate * frate_n) / frate_d;
return true;
}
bool mpeg::ParseSequenceDisplayExt(off_t myoffset){
BTRACK;
if (!DExt) DExt = new display_ext;
DExt->video_format = (GetByte(myoffset) & 0x0E) >> 1;
if (GetByte(myoffset) & 0x01) {
// three bytes to read
DExt->colour_prim = GetByte(myoffset + 1);
DExt->transfer_char = GetByte(myoffset + 2);
DExt->matrix_coef = GetByte(myoffset + 3);
myoffset += 3;
} else {
DExt->colour_prim = 0;
DExt->transfer_char = 0;
DExt->matrix_coef = 0;
}
DExt->h_display_size = GetByte(myoffset + 1) << 6;
DExt->h_display_size |= (GetByte(myoffset + 2) & 0xFC) >> 2;
DExt->v_display_size = (GetByte(myoffset + 2) & 0x01) << 13;
DExt->v_display_size |= GetByte(myoffset + 3) << 5;
DExt->v_display_size |= (GetByte(myoffset + 4) & 0xF8) >> 3;
return true;
}
bool mpeg::ParseRIFF(){
BTRACK;
// RIFF
if ( GetByte(0) != 'R' ||
GetByte(1) != 'I' ||
GetByte(2) != 'F' ||
GetByte(3) != 'F')
return false;
// 23 march 2001 != changed to == in the following
// Thank to Volker Moell
// WAVE
if ( GetByte(8) == 'W' ||
GetByte(9) == 'A' ||
GetByte(10) == 'V' ||
GetByte(11) =='E') {
fprintf(stderr,"%s is a Wave file\n", FileName);
return false;
}
// 'AVI '
if ( GetByte(8)=='A' ||
GetByte(9)=='V' ||
GetByte(10) == 'I' ||
GetByte(11) ==' ') {
fprintf(stderr,"%s is an AVI file\n",FileName);
}
return false;
}
off_t mpeg::FindMatchingAudio(off_t myoffset){
BTRACK;
for( ; myoffset < FileSize - 5; myoffset++) {
if (GetByte(myoffset) == 0xFF)
// maybe an audio sync
if (MatchAudio(myoffset)) return myoffset;
}
if (myoffset >= FileSize) return FileSize;
return -1;
}
off_t mpeg::bdFindMatchingAudio(off_t myoffset){
BTRACK;
for( ; myoffset >= Audio->first_frame_offset; myoffset--) {
if (bdGetByte(myoffset) == 0xFF)
if (MatchAudio(myoffset)) return myoffset;
}
return -1;
}
bool mpeg::MatchAudio(off_t myoffset){
BTRACK;
// do we have audio anyway?
if (!Audio) return false;
//does this start as an mpeg audio frame?
if (GetByte(myoffset) != 0xFF || (GetByte(myoffset + 1) & 0xE0) != 0xE0)
return false;
// are version compatibles?
int version = ((GetByte(myoffset + 1)) & 0x18) >> 3;
switch (Audio->mpeg_ver){
case 1: if (version != 3) return false; break;
case 2: if (version != 2) return false; break;
case 3: if (version != 0) return false; break;
default: return false; break;
}
//are layers compatible?
int layer = (GetByte(myoffset+1)&0x06)>>1;
switch (Audio->layer) {
case 1: if (layer != 3) return false; break;
case 2: if (layer != 2) return false; break;
case 3: if (layer != 1) return false; break;
default: return false; break;
}
//are mode compatibles?
int mode = (GetByte(myoffset + 3) & 0xC0) >> 6;
if (mode != Audio->mode) return false;
return true;
}
long mpeg::ReadPACKMuxRate(off_t offset) {
BTRACK;
// at start of a PACK
long muxrate = 0;
if ((GetByte(offset) & 0xC0) == 0x40) {
// mpeg2
muxrate = GetByte(offset + 6) << 14;
muxrate |= GetByte(offset + 7) << 6;
muxrate |= GetByte(offset + 8) >> 2;
} else {
//maybe mpeg1
if ((GetByte(offset)&0xF0)!=0x20)
fprintf(stderr,
"weird pack header while parsing muxrate (offset " _OFF_x ")\n", offset);
muxrate = (GetByte(offset + 5) & 0x7F) << 15;
muxrate |= (GetByte(offset + 6) << 7);
muxrate |= (GetByte(offset + 7) >> 1);
}
muxrate *= 50;
return muxrate;
}
void mpeg::ParsePAT(off_t offset){
BTRACK;
// offset++; //skip the first byte
int pnum,ppid,i;
int n_progs;
if (GetByte(offset + 1) != 0x00) {
#ifdef _DEBUG_
printf("wrong table_id in PAT\n");
#endif
return;
}
// next 4 bits are 10XX
int length = Read12bitLength(offset + 2);
#ifdef _DEBUG_
printf("PAT: %d programs in %d bytes described\n",(length-9)/4,length-9);
#endif
if ( ( (length - 9)%4) != 0) {
#ifdef _DEBUG_
printf("malformed PAT \n");
#endif
return;
}
n_progs=(length-9)/4;
if (Transport->PMT_PIDs) delete [] Transport->PMT_PIDs;
Transport->PMT_PIDs = new PID [n_progs];
Transport->n_progs = n_progs;
Transport->delete_programs();
Transport->programs = new program[n_progs];
for ( i=0; i < n_progs; i++){
Transport->programs[i].prog_num = -1;
Transport->programs[i].nstreams = -1;
Transport->programs[i].TStreams = 0;
}
for ( i=0; i< n_progs; i++){
pnum = Read2Bytes(offset + 9 + (i*4));
ppid = ReadPID(offset + 11 + (i*4));
if (pnum == 0) {
Transport->network_PID = ppid;
Transport->n_progs--;
} else {
Transport->PMT_PIDs[Transport->n_PMT_PIDs++] = ppid;
}
}
}
long mpeg::Read12bitLength(off_t offset){
BTRACK;
// length must be xxxxAAAA AAAAAAAA
return (((GetByte(offset) & 0x0F) << 8) | GetByte(offset + 1));
}
long mpeg::Read2Bytes(off_t offset){
BTRACK;
return((GetByte(offset) << 8) | GetByte(offset + 1));
}
long mpeg::ReadPID(off_t offset){
BTRACK;
// PID must be xxxAAAAA AAAAAAAA
return (((GetByte(offset) & 0x1F) << 8) | GetByte(offset + 1));
}
void mpeg::ParseCAT(off_t offset){
BTRACK;
// conditional access table
if(GetByte(offset + 1) != 0x01) {
fprintf(stderr,"malformed CAT\n");
return;
}
// int length=Read12bitLength(offset+2);
// long desc_offset=ParseDescriptor(offset+9);
// while (desc_offset < offset+length+4-4){
// desc_offset=ParseDescriptor(desc_offset);
// }
}
void mpeg::ParsePMT(off_t offset){
BTRACK;
off_t i, desc_offset;
int prog_index=0;
if (GetByte(offset + 1) != 0x02) {
#ifdef _DEBUG_
printf("malformed PMT\n");
#endif
return;
}
// next 4 bits are 10XX
int length = Read12bitLength(offset + 2);
#ifdef _DEBUG_
printf("PMT length : %d\n",length);
#endif
int pnum = Read2Bytes(offset + 4);
#ifdef _DEBUG_
printf("This PMT describes Program N %d\n",pnum);
#endif
// check if the program is already here
for (i = 0; i < Transport->n_progs; i++) {
if (Transport->programs[i].prog_num == pnum) {
// already have info on this!
fprintf(stderr, "Warning, prog num %d redefined ! \n", pnum);
if (Transport->programs[i].TStreams != 0)
Transport->delete_ES(Transport->programs[i].TStreams);
Transport->programs[i].TStreams = 0;
}
}
// fetch the first empty index
for (i = 0; i < Transport->n_progs; i++){
if (Transport->programs[i].prog_num == -1) {
prog_index = i;
}
}
if (prog_index >= Transport->n_progs) {
fprintf(stderr,"Error : no room for program %d\n", pnum);
return;
}
program* temp = &(Transport->programs[prog_index]);
temp->prog_num = pnum;
temp->nstreams = 0;
// temp->next=Transport->programs;
// Transport->programs=temp;
// Transport->n_progs++;
// temp->prog_num=pnum;
#ifdef _DEBUG_
int pcrpid = ReadPID(offset + 9);
if (pcrpid != 0x1FFF) printf("PCR of this program is in PID %d\n", pcrpid);
#endif
int infolen = Read12bitLength(offset + 11);
#ifdef _DEBUG_
printf("info length : %d\n", infolen);
#endif
//parsing descriptors
if (infolen > 0) {
desc_offset = ParseDescriptor(offset + 12, &(temp->descs));
while (desc_offset < (offset + 12 + infolen)){
desc_offset = ParseDescriptor(desc_offset, &(temp->descs));
}
}
desc_offset = (offset + 13 + infolen);
off_t max_offset = offset + 4 + length - 4; //-4 is for CRC
size_t desc_size;
int ES_type;
int ES_pid;
int ES_infolen;
for ( i = desc_offset;i<max_offset;i+=desc_size){
ES_type = GetByte(i);
ES_pid = ReadPID(i + 1);
ES_infolen = Read12bitLength(i + 3);
desc_size = 5 + ES_infolen;
#ifdef _DEBUG_
printf("Program [%d]: contains stream : ", pnum);
#endif
temp->nstreams++;
EStream* tempES = new EStream;
tempES->next = temp->TStreams;
temp->TStreams = tempES;
tempES->pid = ES_pid;
tempES->type = ES_type;
tempES->demuxFile = 0;
tempES->demuxFileOk = true;
/* switch(ES_type){
case 0 : printf("reserved"); break;
case 1: printf("MPEG 1 video");break;
case 2: printf("MPEG 2 video");break;
case 3: printf("MPEG 1 audio");break;
case 4: printf("MPEG 2 audio");break;
case 5: printf("MPEG 2 private section");break;
case 6: printf("MPEG 2 PES with private data");break;
case 7: printf("MHEG");break;
case 8: printf("DSM_CC");break;
case 9: printf("Private data");break;
default: if (ES_type <0x80) printf("MPEG 2 reserved");
else printf("User Private data");
break;
}
*/
/* printf(" info@%4lx (%d bytes) PID [%d]\n",i+5,ES_infolen,ES_pid);*/
if (ES_infolen > 0) {
desc_offset = ParseDescriptor(i + 5, &tempES->descs);
while (desc_offset < (i + 5 + ES_infolen))
desc_offset = ParseDescriptor(desc_offset, &tempES->descs);
}
}
}
off_t mpeg::ParseDescriptor(off_t offset,mpeg_descriptors* target){
BTRACK;
int tag = GetByte(offset);
int length = GetByte(offset+1);
#ifdef _DEBUG_
printf("Descriptor %d length %d\n",tag,length);
#endif
bool handled = false;
switch (tag) {
case 0:
case 1: //printf("reserved descriptor\n");
break;
case 2: //printf("video stream descriptor : video version %d\n",GetByte(offset+2));
target->video_coding_version = GetByte(offset + 2);
handled = true;
break;
case 3: //printf("audio stream descriptor : audio version %d\n",GetByte(offset+2));
target->audio_coding_version = GetByte(offset + 2);
handled = true;
break;
case 4: //printf("hierarchy descriptor\n");
break;
case 5: //printf("registration descriptor\n");
break;
case 6: //printf("data stream alignment descriptor\n");
break;
case 7: //printf("target background grid descriptor\n");
break;
case 8: //printf("video window descriptor\n");
break;
case 9: DescCA(offset, target);
handled = true;
break;
case 10:DescLang(offset, target);
handled = true;
break;
case 11: //printf("System clock descriptor\n");
break;
case 12: //printf("multiplex buffer utilization descriptor\n");
break;
case 13: //printf("copyright descriptor\n");
target->copyright = true;
break;
default :
#ifdef _DEBUG_
if (tag < 63) printf("MPEG 2 reserved descriptor\n");
else printf("User Private descriptor\n");
#endif
break;
}
if (!handled) {
/*
for (int i=0;i<length;i++){
printf("%02x ",GetByte(offset+i+2));
}
printf("\n");
*/
target->n_unhandled_desc++;
}
#ifdef _DEBUG_
printf("\n");
#endif
return (offset + length + 2);
}
void mpeg::DescCA(off_t offset, mpeg_descriptors* target){
BTRACK;
#ifdef _DEBUG_
printf("Conditional Access descriptor\n");
#endif
// int length=GetByte(offset+1);
int CAsysID = Read2Bytes(offset + 2);
PID CA_PID = ReadPID(offset + 4);
target->CA = CAsysID;
target->CA_PID = CA_PID;
/* printf("Conditional access ID : %d related PID is %d\n",CAsysID,CA_PID);
if (length>4){
printf("private data:\n");
byte thebyte;
for (long i=offset+6;i<offset+2+length;i++){
thebyte=GetByte(i);
if (thebyte>=0x20) printf("%02x ",thebyte);
else printf("%02x ",thebyte);
}
printf("\n");
for (long i=offset+6;i<offset+2+length;i++){
thebyte=GetByte(i);
if (thebyte>=0x20) printf(" %c ",thebyte);
else printf(" ");
}
printf("\n");
}
*/
}
void mpeg::DescLang(off_t offset, mpeg_descriptors* target){
BTRACK;
int i;
#ifdef _DEBUG_
printf("Language descriptor (untested)\n");
#endif
int length = GetByte(offset + 1);
int audio_type = GetByte(offset + 1 + length);
target->lang_audio_type=audio_type;
if (length > 1) {
// there are languages inside
length--; //without the trailing audio_type
if (length%3 != 0) {
fprintf(stderr,"invalid language in descriptor encountered\n");
return;
}
target->languages = new char[length + 1];
for (i=0; i < length; i++)
{
target->languages[i] = GetByte(offset + 2 + i);
}
target->languages[length] = '\0';
// length /=3;
#ifdef _DEBUG_
printf("languages(s) : ");
#endif
/* for (i=0; i < length; i++){
printf("%c%c%c ",
GetByte(offset+2+(i*3)),
GetByte(offset+3+(i*3)),
GetByte(offset+4+(i*3)));
}
printf("\n");
*/
}
}
bool mpeg::ParseTransportStream(off_t offset){
BTRACK;
if (GetByte(offset) != 0x47) return false;
// int packetnum=0;
// int pmt;
Transport = new transport;
int pid, scramble, adaptation;
int i;
off_t payload_offset;
// well this is a mpeg 2? transport stream
mpeg_version = 2;
#define MPEG2_TR_PKT_LENGTH 188
#define TRANSPORT_PAT 0
#define TRANSPORT_CAT 1
#define PAT_SID 0
#define CAT_SID 1
#define PMT_SID 2
for (; offset < FileSize-1; offset += MPEG2_TR_PKT_LENGTH) {
if (GetByte(offset) != 0x47) {
fprintf(stderr,"Bad Packet start code %#0x should be 0x47\n",GetByte(offset));
return false;
}
pid = ReadPID(offset + 1);
scramble = GetByte(offset + 3) >> 6;
adaptation = (GetByte(offset + 3) & 0x30) >> 4;
if (adaptation == 0) {
// this is reserved, skip!
continue;
}
//compute the payload offset
payload_offset = offset + 4;
if (adaptation & 0x02){
//adaptation fiel is present.
// skip it
// first byte of adaptation field is adaptation field length
payload_offset += GetByte(offset + 4) + 1;
}
// if this is a Program association table parse it
if (pid == 0x00) {
ParsePAT(payload_offset);
}
// if this is a Program Map Table parse it
for (i = 0; i < Transport->n_PMT_PIDs; i++) {
if (Transport->PMT_PIDs[i] == pid) {
ParsePMT(payload_offset);
Transport->read_pmts++;
#ifdef _DEBUG_
printf("PMT at [" _OFF_x "]\n", payload_offset);
#endif
break;
}
}
if (Transport->n_PMT_PIDs !=0 && Transport->n_PMT_PIDs == Transport->read_pmts) {
// we read all program map table...
// Transport->PrintInfos();
HasAudio=HasVideo = true;
return true;
}
/* switch(GetByte(payload_offset+1)){
case PAT_SID :
ParsePAT(payload_offset);
break;
case CAT_SID :
ParseCAT(payload_offset);
break;
case PMT_SID :
ParsePMT(payload_offset);
break;
default : continue; break;
}
printf ("\n\n");
*/
}
#ifdef _DEBUG_
fprintf(stderr,"Sorry MPEG-2 Transport Stream is currently not handled\n");
#endif
fprintf(stderr,"Warning didn't find the promised Program Map Tables\n");
return false;
}
PID mpeg::NextTrPacket(off_t* offset,off_t* payload_start, off_t* payload_end){
BTRACK;
PID pid;
byte adaptation;
off_t payload_offset;
*payload_start = 0;
*payload_end = 0;
// *offset = *offset + MPEG2_TR_PKT_LENGTH;
// *offset = *offset + 188;
pid = ReadPID(*offset + 1);
adaptation = (GetByte(*offset + 3) & 0x30) >> 4;
if (adaptation == 0){
// this is reserved, skip!
*offset += MPEG2_TR_PKT_LENGTH;
return pid;
}
if (pid == 0x1FFF) {
//null packet
*payload_start = 0;
*payload_end = 0;
*offset += MPEG2_TR_PKT_LENGTH;
return pid;
}
//compute the payload offset
payload_offset = *offset + 4;
if (adaptation & 0x02) {
//adaptation fiel is present.
// skip it
// first byte of adaptation field is adaptation field length
payload_offset += GetByte(*offset + 4) + 1;
}
*payload_start = payload_offset;
*offset += MPEG2_TR_PKT_LENGTH;
*payload_end = *offset;
if (*offset >= FileSize)
*payload_start = *payload_end=-1;
return pid;
}
transport::transport()
:programs(0),
n_progs(0),
n_audio_streams(0),
n_video_streams(0),
n_other_streams(0),
network_PID(-1),
PMT_PIDs(0),
n_PMT_PIDs(0),
read_pmts(0)
{}
transport::~transport() {
BTRACK;
if (n_progs >0) {
delete [] programs;
}
}
void transport::delete_programs(){
BTRACK;
if (!programs) return;
for (int i = 0; i < n_progs; i++){
if (programs[i].TStreams != 0) delete_ES(programs[i].TStreams);
}
delete[] programs;
programs=0;
}
void transport::delete_ES(EStream* stream) {
BTRACK;
if (stream == 0) return;
if (stream->next == 0) delete stream;
else delete_ES(stream->next);
}
void transport::PrintInfos(){
BTRACK;
EStream* current;
if (n_progs == 1)
printf(" Mpeg 2 Transport Stream [1 program]\n");
else
printf(" Mpeg 2 Transport Stream [%d programs]\n",n_progs);
for(int i = 0; i < n_progs; i++) {
printf(" Program N %d contains %d Elementary Streams:\n",programs[i].prog_num, programs[i].nstreams);
programs[i].descs.PrintInfos(" ");
current=programs[i].TStreams;
int stream_count = 1;
while (current != 0) {
printf(" Stream %d: ", stream_count++);
switch (current->type) {
case 0: printf("reserved"); break;
case 1: printf("MPEG 1 video");break;
case 2: printf("MPEG 2 video");break;
case 3: printf("MPEG 1 audio");break;
case 4: printf("MPEG 2 audio");break;
case 5: printf("MPEG 2 private section");break;
case 6: printf("MPEG 2 PES with private data");break;
case 7: printf("MHEG");break;
case 8: printf("DSM_CC");break;
case 9: printf("Private data");break;
default: if (current->type <0x80) printf("MPEG 2 reserved");
else printf("User Private data");
break;
}
printf(" [pid: %d]\n", current->pid);
current->descs.PrintInfos(" ");
current=current->next;
}
}
}
void mpeg_descriptors::PrintInfos(char* prefix){
BTRACK;
unsigned int i;
if (prefix==0) prefix = "";
char* description = new char[300];
char temp[100];
description[0] = '\0';
if (video_coding_version != -1) {
sprintf(temp, "%svideo version %d\n", prefix, video_coding_version);
strcat(description,temp);
}
if (audio_coding_version != -1) {
sprintf(temp,"%saudio version %d\n",prefix, audio_coding_version);
strcat(description, temp);
}
if (CA != -1) {
sprintf(temp,"%smaybe scrambled (CA ID %d)\n",prefix,CA);
strcat(description, temp);
}
if (languages) {
sprintf(temp,"%sstream language: ", prefix);
for (i = 0; i < strlen(languages)/3; i++) {
sprintf(temp,"%c%c%c ",languages[i*3], languages[i*3+1], languages[i*3+2]);
strcat(description,temp);
}
strcat(description,"\n");
}
if (lang_audio_type >= 1 && lang_audio_type <= 3) {
switch (lang_audio_type) {
case 1: sprintf(temp,"%sClean effects : not a language\n",prefix);break;
case 2: sprintf(temp,"%sStream is prepared for hearing impaired\n",prefix);break;
case 3: sprintf(temp,"%sStream is prepared for commentaries for visually impaired viewers\n",prefix);
}
strcat(description,temp);
}
if (copyright) {
sprintf(temp,"%sThis stream has copyright limitations\n",prefix);
strcat(description,temp);
}
if (n_unhandled_desc>0) {
if (n_unhandled_desc==1)
sprintf(temp,"%s1 additional descriptor was not handled\n",prefix);
else
sprintf(temp,"%s%d additional descriptors were not handled\n",prefix, n_unhandled_desc);
strcat(description,temp);
}
printf("%s", description);
delete[] description;
}
header_buf *readHeader(FILE *myMpegfile, off_t offset, int rw) {
static byte *nix;
static off_t size;
static header_buf *p;
if (rw == 1) {
p = new header_buf;
size = offset;
nix = new byte[size];
if (!nix) {
fprintf(stderr,"unable to alloc buffer for header\n");
exit(1);
}
FSEEK(myMpegfile,0L,SEEK_SET);
fread(nix,size,1L,myMpegfile);
p->size = size;
p->buf = nix;
if (preserve_header)
fprintf(stderr,"Size of Fix: 0x%qx\n",size);
return(NULL);
}
if (rw == 2)
return(p);
fprintf(stderr,"unable to understand command\n");
exit(1);
}
/// prints all time stamps on stdout
void mpeg::print_all_ts(byte kind){
off_t p = 0;
byte *nix;
double tsx;
// FIXME [es] What is "nix" for?
nix = &kind;
while( (p = FindNextMarker(p,kind)) != -1) {
if (mpeg_version == 2)
tsx = ReadTSMpeg2(p+4);
else
tsx = ReadTS(p+4);
fprintf (stderr,"offset: " _OFF_x " TS: %lf\n",p,tsx);
p+=4;
}
}
/// Checks time line of increasing time stamps and return amount of breaks
int mpeg::CheckTimeline(byte kind){
off_t p = 0;
double tsx = 0,
tsx_prev = 0;
int ret = 0;
while( (p = FindNextMarker(p,kind)) != -1) {
if (mpeg_version == 2)
tsx = ReadTSMpeg2(p+4);
else
tsx = ReadTS(p+4);
if ( !(tsx >= tsx_prev) ){
++ret;
//#ifdef _DEBUG_
printf("\tTime stamps jumped from %lf to %lf at position " _OFF_x "\n",tsx_prev,tsx,p);
//#endif
}
tsx_prev = tsx;
p+=4;
}
return ret;
}
|