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
|
/*****************************************************************
* gmerlin - a general purpose multimedia framework and applications
*
* Copyright (c) 2001 - 2010 Members of the Gmerlin project
* gmerlin-general@lists.sourceforge.net
* http://gmerlin.sourceforge.net
*
* 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, see <http://www.gnu.org/licenses/>.
* *****************************************************************/
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <math.h>
// mkdir()
#include <sys/stat.h>
#include <sys/types.h>
#include <config.h>
#include <gmerlin/translation.h>
#include <gmerlin/pluginregistry.h>
#include <gmerlin/log.h>
#include <gmerlin/msgqueue.h>
#include <gmerlin/transcoder.h>
#include <gmerlin/transcodermsg.h>
#include <gmerlin/utils.h>
#include <gmerlin/bggavl.h>
#include <gmerlin/textrenderer.h>
#include <gmerlin/converters.h>
#include <gmerlin/filters.h>
#include <gmerlin/encoder.h>
// #define DUMP_VIDEO_TIMESTAMPS
#define LOG_DOMAIN "transcoder"
#define STREAM_STATE_OFF 0
#define STREAM_STATE_ON 1
#define STREAM_STATE_FINISHED 2
#define STREAM_TYPE_AUDIO 0
#define STREAM_TYPE_VIDEO 1
#define STREAM_TYPE_SUBTITLE_TEXT 2
#define STREAM_TYPE_SUBTITLE_OVERLAY 3
#define STREAM_ACTION_FORGET 0
#define STREAM_ACTION_TRANSCODE 1
/* The followings are for subtitles only */
#define STREAM_ACTION_BLEND 2
/* The following is only for text subtitles and means, that they'll be
converted to graphical overlays using the text renderer */
#define STREAM_ACTION_TRANSCODE_OVERLAY 4
#define TRANSCODER_STATE_INIT 0
#define TRANSCODER_STATE_RUNNING 1
#define TRANSCODER_STATE_FINISHED 2
#define TRANSCODER_STATE_ERROR 3
/* Language utilities */
static void copy_language(char * dst, const char * src)
{
if(src)
strncpy(dst, src, 3);
else
*dst = '\0';
}
static char * get_language(char * in_lang, char * lang, int force)
{
if(in_lang)
return force ? lang : in_lang;
return lang;
}
typedef struct subtitle_stream_s subtitle_stream_t;
typedef struct
{
int status;
int type;
int action;
int in_index;
int out_index;
gavl_time_t time;
bg_plugin_handle_t * in_handle;
bg_input_plugin_t * in_plugin;
int do_encode; /* Whether this stream should be really encoded */
int do_decode; /* Whether this stream should be decoded */
} stream_t;
static int set_stream_parameters_general(stream_t * s,
const char * name,
const bg_parameter_value_t * val)
{
if(!strcmp(name, "action"))
{
if(!strcmp(val->val_str, "transcode"))
s->action = STREAM_ACTION_TRANSCODE;
else if(!strcmp(val->val_str, "transcode"))
s->action = STREAM_ACTION_TRANSCODE;
else if(!strcmp(val->val_str, "transcode_overlay"))
s->action = STREAM_ACTION_TRANSCODE_OVERLAY;
else if(!strcmp(val->val_str, "blend"))
s->action = STREAM_ACTION_BLEND;
else
s->action = STREAM_ACTION_FORGET;
return 1;
}
return 0;
}
typedef struct
{
stream_t com;
// int do_convert_in;
int do_convert_out;
gavl_audio_converter_t * cnv_out;
bg_audio_filter_chain_t * fc;
gavl_audio_frame_t * out_frame;
gavl_audio_frame_t * pipe_frame;
gavl_audio_format_t in_format;
gavl_audio_format_t pipe_format;
gavl_audio_format_t out_format;
bg_read_audio_func_t in_func;
void * in_data;
int in_stream;
/* Set by set_parameter */
bg_gavl_audio_options_t options;
/* Do normalization */
int normalize;
int64_t samples_to_read; /* Samples to read (in OUTPUT samplerate) */
int64_t samples_read; /* Samples read so far (in OUTPUT samplerate) */
int initialized;
gavl_peak_detector_t * peak_detector;
gavl_volume_control_t * volume_control;
char in_language[4];
char out_language[4];
int force_language;
} audio_stream_t;
static void set_audio_parameter_general(void * data, const char * name,
const bg_parameter_value_t * val)
{
audio_stream_t * stream;
stream = (audio_stream_t*)data;
if(!name)
return;
if(!strcmp(name, "normalize"))
{
stream->normalize = val->val_i;
return;
}
if(!strcmp(name, "in_language"))
{
copy_language(stream->in_language, val->val_str);
return;
}
if(!strcmp(name, "language"))
{
copy_language(stream->out_language, val->val_str);
return;
}
if(!strcmp(name, "force_language"))
{
stream->force_language = val->val_i;
return;
}
if(set_stream_parameters_general(&(stream->com),
name, val))
return;
else if(bg_gavl_audio_set_parameter(&(stream->options), name, val))
return;
}
typedef struct
{
stream_t com;
bg_video_filter_chain_t * fc;
gavl_video_frame_t * frame;
gavl_video_format_t in_format;
gavl_video_format_t out_format;
int64_t frames_written;
/* Set by set_parameter */
bg_gavl_video_options_t options;
/* Data source */
bg_read_video_func_t in_func;
void * in_data;
int in_stream;
/* Other stuff */
int initialized;
int64_t start_time_scaled;
/* Whether 2-pass transcoding is requested */
int twopass;
char * stats_file;
/* Subtitle streams for blending */
int num_subtitle_streams;
subtitle_stream_t ** subtitle_streams;
} video_stream_t;
static void set_video_parameter_general(void * data,
const char * name,
const bg_parameter_value_t * val)
{
video_stream_t * stream;
stream = (video_stream_t*)data;
if(!name)
return;
if(!strcmp(name, "twopass"))
stream->twopass = val->val_i;
if(set_stream_parameters_general(&(stream->com),
name, val))
return;
else if(bg_gavl_video_set_parameter(&(stream->options),
name, val))
return;
}
struct subtitle_stream_s
{
stream_t com;
gavl_video_converter_t * cnv;
int do_convert;
gavl_overlay_t ovl1, ovl2;
gavl_overlay_t * current_ovl;
gavl_overlay_t * next_ovl;
int has_current;
int has_next;
gavl_overlay_blend_context_t * blend_context;
int video_stream;
gavl_video_format_t in_format;
gavl_video_format_t out_format;
int do_blend; /* Set by check_video_blend() */
int eof; /* Set STREAM finished not before the last subtitle expired */
char in_language[4];
char out_language[4];
int force_language;
};
typedef struct
{
subtitle_stream_t com;
bg_text_renderer_t * textrenderer;
char * text;
int text_alloc;
gavl_time_t subtitle_start;
gavl_time_t subtitle_duration;
} subtitle_text_stream_t;
static void set_subtitle_parameter_general(void * data,
const char * name,
const bg_parameter_value_t * val)
{
subtitle_stream_t * stream;
stream = (subtitle_stream_t*)data;
if(!name)
return;
if(!strcmp(name, "video_stream"))
stream->video_stream = val->val_i-1;
if(!strcmp(name, "in_language"))
{
copy_language(stream->in_language, val->val_str);
return;
}
if(!strcmp(name, "language"))
{
copy_language(stream->out_language, val->val_str);
return;
}
if(!strcmp(name, "force_language"))
{
stream->force_language = val->val_i;
return;
}
if(set_stream_parameters_general(&(stream->com),
name, val))
return;
}
struct bg_transcoder_s
{
int num_audio_streams;
int num_video_streams;
int num_subtitle_text_streams;
int num_subtitle_overlay_streams;
int num_audio_streams_real;
int num_video_streams_real;
int num_subtitle_text_streams_real;
int num_subtitle_overlay_streams_real;
audio_stream_t * audio_streams;
video_stream_t * video_streams;
subtitle_text_stream_t * subtitle_text_streams;
subtitle_stream_t * subtitle_overlay_streams;
float percentage_done;
gavl_time_t remaining_time; /* Remaining time (Transcoding time, NOT track time!!!) */
double last_seconds;
int state;
bg_plugin_handle_t * in_handle;
bg_input_plugin_t * in_plugin;
bg_track_info_t * track_info;
bg_plugin_handle_t * out_handle;
/* Set by set_parameter */
char * name;
char * location;
char * plugin;
int track;
gavl_time_t start_time;
gavl_time_t end_time;
int set_start_time;
int set_end_time;
bg_metadata_t metadata;
/* General configuration stuff */
char * output_directory;
char * output_path;
char * subdir;
int delete_incomplete;
int send_finished;
/* Timing stuff */
gavl_timer_t * timer;
gavl_time_t time;
/* Duration of the section to be transcoded */
gavl_time_t duration;
char * output_filename;
int is_url;
/* Message queues */
bg_msg_queue_list_t * message_queues;
/* Multi threading stuff */
pthread_t thread;
int do_stop;
pthread_mutex_t stop_mutex;
/* Multipass */
int total_passes;
int pass;
bg_plugin_registry_t * plugin_reg;
/* Track we are created from */
bg_transcoder_track_t * transcoder_track;
/* Encoder frontend */
bg_encoder_t * enc;
/* Postprocess only */
int pp_only;
bg_encoder_callbacks_t cb;
char ** output_files;
int num_output_files;
};
static void add_output_file(bg_transcoder_t * t, const char * filename)
{
t->output_files = realloc(t->output_files,
(t->num_output_files+2) * sizeof(*t->output_files));
memset(t->output_files + t->num_output_files, 0, 2 * sizeof(*t->output_files));
t->output_files[t->num_output_files] = bg_strdup(NULL, filename);
t->num_output_files++;
}
static void free_output_files(bg_transcoder_t * t)
{
int i = 0;
for(i = 0; i < t->num_output_files; i++)
free(t->output_files[i]);
if(t->output_files)
free(t->output_files);
}
static void log_transcoding_time(bg_transcoder_t * t)
{
gavl_time_t transcoding_time;
char time_str[GAVL_TIME_STRING_LEN];
transcoding_time = gavl_timer_get(t->timer);
gavl_time_prettyprint(transcoding_time, time_str);
bg_log(BG_LOG_INFO, LOG_DOMAIN, "Transcoding took %s (%.2f %% of realtime duration)",
time_str,
100.0 * gavl_time_to_seconds(transcoding_time) /
gavl_time_to_seconds(t->duration));
}
static const bg_parameter_info_t parameters[] =
{
{
.name = "output_path",
.long_name = TRS("Output Directory"),
.type = BG_PARAMETER_DIRECTORY,
.val_default = { .val_str = "." },
},
{
.name = "delete_incomplete",
.long_name = TRS("Delete incomplete output files"),
.type = BG_PARAMETER_CHECKBUTTON,
.val_default = { .val_i = 1 },
.help_string = TRS("Delete the encoded files if you hit the stop button. \
This option will automatically be disabled, when the track is an URL"),
},
{
.name = "cleanup_pp",
.long_name = TRS("Clean up after postprocessing"),
.type = BG_PARAMETER_CHECKBUTTON,
.help_string = TRS("Clean up all encoded files, which were postprocessed"),
},
{
.name = "send_finished",
.long_name = TRS("Send finished files to player"),
.type = BG_PARAMETER_CHECKBUTTON,
.val_default = { .val_i = 1 },
},
{ /* End of parameters */ }
};
void bg_transcoder_set_parameter(void * data, const char * name, const bg_parameter_value_t * val)
{
bg_transcoder_t * w = (bg_transcoder_t*)data;
if(!name)
return;
if(!strcmp(name, "output_path"))
{
w->output_path = bg_strdup(w->output_path, val->val_str);
}
else if(!strcmp(name, "delete_incomplete"))
{
w->delete_incomplete = val->val_i;
}
else if(!strcmp(name, "send_finished"))
{
w->send_finished = val->val_i;
}
}
const bg_parameter_info_t * bg_transcoder_get_parameters()
{
return parameters;
}
/* Message stuff */
typedef struct
{
int id;
int num;
} message_num_t;
static void set_message_num(bg_msg_t * msg, const void * data)
{
message_num_t * num = (message_num_t *)data;
bg_msg_set_id(msg, num->id);
bg_msg_set_arg_int(msg, 0, num->num);
}
void bg_transcoder_send_msg_num_audio_streams(bg_msg_queue_list_t * l,
int num)
{
message_num_t n;
n.id = BG_TRANSCODER_MSG_NUM_AUDIO_STREAMS;
n.num = num;
bg_msg_queue_list_send(l,
set_message_num, &n);
}
void bg_transcoder_send_msg_num_video_streams(bg_msg_queue_list_t * l,
int num)
{
message_num_t n;
n.id = BG_TRANSCODER_MSG_NUM_VIDEO_STREAMS;
n.num = num;
bg_msg_queue_list_send(l,
set_message_num, &n);
}
typedef struct
{
int id;
int index;
gavl_audio_format_t * ifmt;
gavl_audio_format_t * ofmt;
} message_af_t;
static void set_message_audio_format(bg_msg_t * msg, const void * data)
{
message_af_t * m = (message_af_t *)data;
bg_msg_set_id(msg, m->id);
bg_msg_set_arg_int(msg, 0, m->index);
bg_msg_set_arg_audio_format(msg, 1, m->ifmt);
bg_msg_set_arg_audio_format(msg, 2, m->ofmt);
}
void bg_transcoder_send_msg_audio_format(bg_msg_queue_list_t * l,
int index,
gavl_audio_format_t * input_format,
gavl_audio_format_t * output_format)
{
message_af_t m;
m.id = BG_TRANSCODER_MSG_AUDIO_FORMAT;
m.index = index;
m.ifmt = input_format;
m.ofmt = output_format;
bg_msg_queue_list_send(l,
set_message_audio_format, &m);
}
typedef struct
{
int id;
int index;
gavl_video_format_t * ifmt;
gavl_video_format_t * ofmt;
} message_vf_t;
static void set_message_video_format(bg_msg_t * msg, const void * data)
{
message_vf_t * m = (message_vf_t *)data;
bg_msg_set_id(msg, m->id);
bg_msg_set_arg_int(msg, 0, m->index);
bg_msg_set_arg_video_format(msg, 1, m->ifmt);
bg_msg_set_arg_video_format(msg, 2, m->ofmt);
}
void bg_transcoder_send_msg_video_format(bg_msg_queue_list_t * l,
int index,
gavl_video_format_t * input_format,
gavl_video_format_t * output_format)
{
message_vf_t m;
m.id = BG_TRANSCODER_MSG_VIDEO_FORMAT;
m.index = index;
m.ifmt = input_format;
m.ofmt = output_format;
bg_msg_queue_list_send(l,
set_message_video_format, &m);
}
typedef struct
{
const char * name;
int pp_only;
} message_file_t;
static void set_message_file(bg_msg_t * msg, const void * data)
{
message_file_t * m = (message_file_t *)data;
bg_msg_set_id(msg, BG_TRANSCODER_MSG_FILE);
bg_msg_set_arg_string(msg, 0, m->name);
bg_msg_set_arg_int(msg, 1, m->pp_only);
}
void bg_transcoder_send_msg_file(bg_msg_queue_list_t * l,
const char * filename, int pp_only)
{
message_file_t m;
m.name = filename;
m.pp_only = pp_only;
bg_msg_queue_list_send(l,
set_message_file, &m);
}
typedef struct
{
float perc;
gavl_time_t rem;
} message_progress_t;
static void set_message_progress(bg_msg_t * msg, const void * data)
{
message_progress_t * m = (message_progress_t *)data;
bg_msg_set_id(msg, BG_TRANSCODER_MSG_PROGRESS);
bg_msg_set_arg_float(msg, 0, m->perc);
bg_msg_set_arg_time(msg, 1, m->rem);
}
void bg_transcoder_send_msg_progress(bg_msg_queue_list_t * l,
float percentage_done,
gavl_time_t remaining_time)
{
message_progress_t n;
n.perc = percentage_done;
n.rem = remaining_time;
bg_msg_queue_list_send(l,
set_message_progress, &n);
}
static void set_message_finished(bg_msg_t * msg, const void * data)
{
bg_msg_set_id(msg, BG_TRANSCODER_MSG_FINISHED);
}
void bg_transcoder_send_msg_finished(bg_msg_queue_list_t * l)
{
bg_msg_queue_list_send(l, set_message_finished, NULL);
}
static void set_message_start(bg_msg_t * msg, const void * data)
{
bg_msg_set_id(msg, BG_TRANSCODER_MSG_START);
bg_msg_set_arg_string(msg, 0, (char*)data);
}
void bg_transcoder_send_msg_start(bg_msg_queue_list_t * l, char * what)
{
bg_msg_queue_list_send(l,
set_message_start, what);
}
static void set_message_error(bg_msg_t * msg, const void * data)
{
bg_msg_set_id(msg, BG_TRANSCODER_MSG_ERROR);
}
void bg_transcoder_send_msg_error(bg_msg_queue_list_t * l)
{
bg_msg_queue_list_send(l, set_message_error, (void*)0);
}
static void set_message_metadata(bg_msg_t * msg, const void * data)
{
bg_msg_set_id(msg, BG_TRANSCODER_MSG_METADATA);
bg_msg_set_arg_metadata(msg, 0, (const bg_metadata_t*)data);
}
void bg_transcoder_send_msg_metadata(bg_msg_queue_list_t * l, bg_metadata_t * m)
{
bg_msg_queue_list_send(l,
set_message_metadata, m);
}
/* */
static int decode_video_frame(void * priv, gavl_video_frame_t * f, int stream)
{
video_stream_t * s;
bg_transcoder_t * t;
int result;
t = (bg_transcoder_t *)priv;
s = &t->video_streams[stream];
result = s->com.in_plugin->read_video(s->com.in_handle->priv,
f, s->com.in_index);
if(!result)
return 0;
#ifdef DUMP_VIDEO_TIMESTAMPS
bg_debug("Input timestamp: %"PRId64" Offset: %"PRId64"\n",
f->timestamp, s->start_time_scaled);
#endif
/* Check for end of stream */
if((t->end_time != GAVL_TIME_UNDEFINED) &&
(gavl_time_unscale(s->in_format.timescale, f->timestamp) >= t->end_time))
return 0;
/* Correct timestamps */
if(s->start_time_scaled)
{
f->timestamp -= s->start_time_scaled;
if(f->timestamp < 0)
f->timestamp = 0;
}
return 1;
}
static void init_audio_stream(audio_stream_t * ret,
bg_transcoder_track_audio_t * s,
int in_index, bg_plugin_registry_t * plugin_reg)
{
ret->com.type = STREAM_TYPE_AUDIO;
/* Default options */
ret->volume_control = gavl_volume_control_create();
ret->peak_detector = gavl_peak_detector_create();
/* Create converter */
bg_gavl_audio_options_init(&(ret->options));
ret->cnv_out = gavl_audio_converter_create();
ret->fc = bg_audio_filter_chain_create(&(ret->options), plugin_reg);
/* Apply parameters */
bg_cfg_section_apply(s->general_section,
bg_transcoder_track_audio_get_general_parameters(),
set_audio_parameter_general, ret);
bg_cfg_section_apply(s->filter_section,
s->filter_parameters,
bg_audio_filter_chain_set_parameter, ret->fc);
ret->com.in_index = in_index;
}
static void start_audio_stream_i(audio_stream_t * ret,
bg_plugin_handle_t * in_handle)
{
ret->com.in_handle = in_handle;
ret->com.in_plugin = (bg_input_plugin_t*)(in_handle->plugin);
/* Set stream */
if(ret->com.in_plugin->set_audio_stream)
{
if(ret->com.action == STREAM_ACTION_TRANSCODE)
ret->com.in_plugin->set_audio_stream(ret->com.in_handle->priv,
ret->com.in_index,
BG_STREAM_ACTION_DECODE);
else
ret->com.in_plugin->set_audio_stream(ret->com.in_handle->priv,
ret->com.in_index,
BG_STREAM_ACTION_OFF);
}
}
static void init_video_stream(video_stream_t * ret,
bg_transcoder_track_video_t * s,
int in_index, bg_plugin_registry_t * plugin_reg)
{
ret->com.type = STREAM_TYPE_VIDEO;
/* Default options */
bg_gavl_video_options_init(&(ret->options));
/* Create converter */
ret->fc = bg_video_filter_chain_create(&ret->options, plugin_reg);
/* Apply parameters */
bg_cfg_section_apply(s->general_section,
bg_transcoder_track_video_get_general_parameters(),
set_video_parameter_general, ret);
bg_cfg_section_apply(s->filter_section,
s->filter_parameters,
bg_video_filter_chain_set_parameter, ret->fc);
ret->com.in_index = in_index;
}
static void start_video_stream_i(video_stream_t * ret, bg_plugin_handle_t * in_handle)
{
ret->com.in_handle = in_handle;
ret->com.in_plugin = (bg_input_plugin_t*)(in_handle->plugin);
/* Set stream */
if(ret->com.in_plugin->set_video_stream)
{
if(ret->com.action == STREAM_ACTION_TRANSCODE)
ret->com.in_plugin->set_video_stream(ret->com.in_handle->priv,
ret->com.in_index,
BG_STREAM_ACTION_DECODE);
else
ret->com.in_plugin->set_video_stream(ret->com.in_handle->priv,
ret->com.in_index,
BG_STREAM_ACTION_OFF);
}
}
static void init_subtitle_overlay_stream(subtitle_stream_t * ret,
bg_transcoder_track_subtitle_overlay_t * s)
{
ret->com.type = STREAM_TYPE_SUBTITLE_OVERLAY;
/* Apply parameters */
bg_cfg_section_apply(s->general_section,
s->general_parameters,
set_subtitle_parameter_general, ret);
ret->com.in_index = s->in_index;
if(ret->com.action == STREAM_ACTION_BLEND)
{
ret->blend_context = gavl_overlay_blend_context_create();
}
ret->cnv = gavl_video_converter_create();
}
static void init_subtitle_text_stream(subtitle_text_stream_t * ret,
bg_transcoder_track_subtitle_text_t * s)
{
ret->com.com.type = STREAM_TYPE_SUBTITLE_TEXT;
/* Apply parameters */
bg_cfg_section_apply(s->general_section,
s->general_parameters,
set_subtitle_parameter_general, ret);
ret->com.com.in_index = s->in_index;
if(ret->com.com.action == STREAM_ACTION_BLEND)
{
ret->com.blend_context = gavl_overlay_blend_context_create();
ret->textrenderer = bg_text_renderer_create();
bg_cfg_section_apply(s->textrenderer_section,
bg_text_renderer_get_parameters(),
bg_text_renderer_set_parameter,
ret->textrenderer);
}
else if(ret->com.com.action == STREAM_ACTION_TRANSCODE_OVERLAY)
{
ret->textrenderer = bg_text_renderer_create();
bg_cfg_section_apply(s->textrenderer_section,
bg_text_renderer_get_parameters(),
bg_text_renderer_set_parameter,
ret->textrenderer);
ret->com.cnv = gavl_video_converter_create();
}
}
static void start_subtitle_stream_i(subtitle_stream_t * ret,
bg_plugin_handle_t * in_handle)
{
ret->com.in_handle = in_handle;
ret->com.in_plugin = (bg_input_plugin_t*)(in_handle->plugin);
/* Set stream */
if(ret->com.in_plugin->set_subtitle_stream)
{
if(ret->com.action != STREAM_ACTION_FORGET)
ret->com.in_plugin->set_subtitle_stream(ret->com.in_handle->priv,
ret->com.in_index,
BG_STREAM_ACTION_DECODE);
else
ret->com.in_plugin->set_subtitle_stream(ret->com.in_handle->priv,
ret->com.in_index,
BG_STREAM_ACTION_OFF);
}
}
static void set_input_formats(bg_transcoder_t * ret)
{
int i;
for(i = 0; i < ret->num_audio_streams; i++)
{
if(ret->audio_streams[i].com.do_decode)
{
gavl_audio_format_copy(&(ret->audio_streams[i].in_format),
&(ret->track_info->audio_streams[ret->audio_streams[i].com.in_index].format));
}
}
for(i = 0; i < ret->num_video_streams; i++)
{
if(ret->video_streams[i].com.do_decode)
gavl_video_format_copy(&(ret->video_streams[i].in_format),
&(ret->track_info->video_streams[ret->video_streams[i].com.in_index].format));
}
for(i = 0; i < ret->num_subtitle_overlay_streams; i++)
{
if(ret->subtitle_overlay_streams[i].com.do_decode)
gavl_video_format_copy(&(ret->subtitle_overlay_streams[i].in_format),
&(ret->track_info->subtitle_streams[ret->subtitle_overlay_streams[i].com.in_index].format));
}
for(i = 0; i < ret->num_subtitle_text_streams; i++)
{
if(ret->subtitle_text_streams[i].com.com.do_decode)
gavl_video_format_copy(&(ret->subtitle_text_streams[i].com.in_format),
&(ret->track_info->subtitle_streams[ret->subtitle_text_streams[i].com.com.in_index].format));
}
}
static void add_audio_stream(audio_stream_t * ret,
bg_transcoder_track_audio_t * s,
bg_transcoder_t * t)
{
char * language;
ret->in_func = ret->com.in_plugin->read_audio;
ret->in_data = ret->com.in_handle->priv;
ret->in_stream = ret->com.in_index;
/* We set the frame size so we have roughly half second long audio chunks */
#if 1
ret->in_format.samples_per_frame = gavl_time_to_samples(ret->in_format.samplerate,
GAVL_TIME_SCALE/2);
#endif
bg_audio_filter_chain_connect_input(ret->fc,
ret->in_func,
ret->in_data,
ret->in_stream);
bg_audio_filter_chain_init(ret->fc, &(ret->in_format), &(ret->pipe_format));
ret->pipe_format.samples_per_frame = gavl_time_to_samples(ret->pipe_format.samplerate,
GAVL_TIME_SCALE/2);
ret->in_func = bg_audio_filter_chain_read;
ret->in_data = ret->fc;
ret->in_stream = 0;
gavl_audio_format_copy(&(ret->out_format),
&(ret->pipe_format));
/* Decide language */
language = get_language(ret->in_language,
ret->out_language,
ret->force_language);
/* Add the audio stream */
ret->com.out_index =
bg_encoder_add_audio_stream(t->enc,
language,
&ret->out_format,
ret->com.in_index);
}
static void add_subtitle_text_stream(subtitle_text_stream_t * ret,
bg_transcoder_track_subtitle_text_t * s,
bg_transcoder_t * t)
{
char * language;
if(ret->com.com.action == STREAM_ACTION_TRANSCODE)
{
/* Decide language */
language = get_language(ret->com.in_language,
ret->com.out_language,
ret->com.force_language);
gavl_video_format_copy(&(ret->com.out_format),
&(ret->com.in_format));
/* TODO: timescale might get changed by the encoder!!! */
ret->com.com.out_index =
bg_encoder_add_subtitle_text_stream(t->enc,
language, ret->com.out_format.timescale,
ret->com.com.in_index);
}
else if(ret->com.com.action == STREAM_ACTION_TRANSCODE_OVERLAY)
{
/* Get the video format for overlay encoding. This is a bit nasty, since we have no idea yet,
how the format will look like (the video output format isn't known by now). We'll copy the
format, which was passed to the video encoder and hope that the overlay encoder will choose
a proper pixelformat for us. Then, we pass the same format as *frame* format to the
textrenderer */
if(t->video_streams[ret->com.video_stream].com.do_encode)
{
gavl_video_format_copy(&ret->com.out_format,
&t->video_streams[ret->com.video_stream].out_format);
}
else
{
/* Video stream won't get encoded: Use video format associatd with the text subtitle stream */
gavl_video_format_copy(&ret->com.out_format, &ret->com.in_format);
}
/* Decide language */
language = get_language(ret->com.in_language,
ret->com.out_language,
ret->com.force_language);
ret->com.com.out_index =
bg_encoder_add_subtitle_overlay_stream(t->enc,
language,
&ret->com.out_format, ret->com.com.in_index,
BG_STREAM_SUBTITLE_TEXT);
}
}
static void add_subtitle_overlay_stream(subtitle_stream_t * ret,
bg_transcoder_track_subtitle_overlay_t * s,
bg_transcoder_t * t)
{
char * language;
gavl_video_format_copy(&ret->out_format, &ret->in_format);
/* Decide language */
language = get_language(ret->in_language,
ret->out_language,
ret->force_language);
ret->com.out_index =
bg_encoder_add_subtitle_overlay_stream(t->enc,
language,
&ret->out_format, ret->com.in_index,
BG_STREAM_SUBTITLE_OVERLAY);
}
static void add_video_stream(video_stream_t * ret,
bg_transcoder_track_video_t * s,
bg_transcoder_t * t)
{
ret->in_func = decode_video_frame;
ret->in_data = t;
ret->in_stream = ret->com.in_index;
bg_video_filter_chain_connect_input(ret->fc,
ret->in_func, ret->in_data, ret->in_stream);
bg_video_filter_chain_init(ret->fc, &(ret->in_format),
&(ret->out_format));
ret->in_func = bg_video_filter_chain_read;
ret->in_data = ret->fc;
ret->in_stream = 0;
/* Add the video stream */
ret->com.out_index =
bg_encoder_add_video_stream(t->enc,
&ret->out_format,
ret->com.in_index);
}
static int set_video_pass(bg_transcoder_t * t, int i)
{
video_stream_t * s;
s = &(t->video_streams[i]);
if(!s->twopass)
return 1;
if(!s->stats_file)
{
s->stats_file = bg_sprintf("%s/%s_video_%02d.stats", t->output_directory, t->name, i+1);
bg_log(BG_LOG_INFO, LOG_DOMAIN, "Using statistics file: %s", s->stats_file);
}
bg_encoder_set_video_pass(t->enc,
s->com.out_index, t->pass, t->total_passes,
s->stats_file);
return 1;
}
static int audio_iteration(audio_stream_t*s, bg_transcoder_t * t)
{
int ret = 1;
int num_samples;
int samples_decoded;
gavl_audio_frame_t * frame;
/* Get one frame worth of input data */
if(!s->initialized)
{
if((t->start_time != GAVL_TIME_UNDEFINED) &&
(t->end_time != GAVL_TIME_UNDEFINED) &&
(t->end_time > t->start_time))
{
s->samples_to_read = gavl_time_to_samples(s->out_format.samplerate,
t->end_time - t->start_time);
}
else if(t->end_time != GAVL_TIME_UNDEFINED)
{
s->samples_to_read = gavl_time_to_samples(s->out_format.samplerate,
t->end_time);
}
else
s->samples_to_read = 0; /* Zero == Infinite */
s->initialized = 1;
}
if(s->samples_to_read &&
(s->samples_read + s->out_format.samples_per_frame > s->samples_to_read))
num_samples = s->samples_to_read - s->samples_read;
else
num_samples = s->out_format.samples_per_frame;
if(s->do_convert_out)
frame = s->pipe_frame;
else
frame = s->out_frame;
samples_decoded = s->in_func(s->in_data, frame, s->in_stream,
num_samples);
/* Nothing more to transcode */
if(!samples_decoded)
{
s->com.status = STREAM_STATE_FINISHED;
return ret;
}
s->samples_read += frame->valid_samples;
/* Last samples */
if(s->samples_to_read && (s->samples_to_read <= s->samples_read))
s->com.status = STREAM_STATE_FINISHED;
s->com.time = gavl_samples_to_time(s->out_format.samplerate,
s->samples_read);
/* Volume normalization */
if(s->normalize)
{
if(t->pass == t->total_passes)
{
gavl_volume_control_apply(s->volume_control, frame);
}
else if((t->pass > 1) && (t->pass < t->total_passes))
frame = (gavl_audio_frame_t*)0;
}
/* Output conversion */
if(s->do_convert_out)
{
gavl_audio_convert(s->cnv_out, s->pipe_frame, s->out_frame);
frame = s->out_frame;
}
/* Peak detection is done for the final frame */
if(s->normalize)
{
if(t->pass == 1)
{
gavl_peak_detector_update(s->peak_detector, frame);
frame = (gavl_audio_frame_t*)0;
}
}
if(frame)
ret = bg_encoder_write_audio_frame(t->enc,
frame,
s->com.out_index);
if(!ret)
bg_log(BG_LOG_ERROR, LOG_DOMAIN, "Encoding audio failed");
return ret;
}
static void correct_subtitle_timestamp(subtitle_stream_t * s,
int64_t * start,
int64_t * duration,
bg_transcoder_t * t)
{
/* Correct timestamps */
if(t->start_time != GAVL_TIME_UNDEFINED)
{
*start -= gavl_time_scale(s->in_format.timescale, t->start_time);
if(*start < 0)
{
*duration += *start;
if(*duration < 0)
*duration = 0;
*start = 0;
}
}
/* Rescale */
*start = gavl_time_rescale(s->in_format.timescale, s->out_format.timescale,
*start);
*duration = gavl_time_rescale(s->in_format.timescale, s->out_format.timescale,
*duration);
}
static int decode_subtitle_overlay(subtitle_stream_t * s, bg_transcoder_t * t,
gavl_overlay_t * ovl)
{
int result;
subtitle_text_stream_t * st;
result = s->com.in_plugin->has_subtitle(s->com.in_handle->priv, s->com.in_index);
if(!result)
return 0;
if(s->com.type == STREAM_TYPE_SUBTITLE_TEXT)
{
st = (subtitle_text_stream_t*)s;
result = s->com.in_plugin->read_subtitle_text(s->com.in_handle->priv,
&st->text, &st->text_alloc,
&st->subtitle_start,
&st->subtitle_duration,
s->com.in_index);
if(!result || ((t->end_time != GAVL_TIME_UNDEFINED) &&
(st->subtitle_start >= t->end_time)))
{
s->eof = 1;
return 0;
}
correct_subtitle_timestamp(&st->com, &st->subtitle_start,
&st->subtitle_duration, t);
ovl->frame->timestamp = st->subtitle_start;
ovl->frame->duration = st->subtitle_duration;
bg_text_renderer_render(st->textrenderer, st->text, ovl);
}
else
{
result = s->com.in_plugin->read_subtitle_overlay(s->com.in_handle->priv,
ovl, s->com.in_index);
if(!result || ((t->end_time != GAVL_TIME_UNDEFINED) &&
(ovl->frame->timestamp >= t->end_time)))
{
s->eof = 1;
return 0;
}
correct_subtitle_timestamp(s, &ovl->frame->timestamp,
&ovl->frame->duration, t);
}
return 1;
}
static int decode_subtitle_text(subtitle_text_stream_t * s, bg_transcoder_t * t)
{
int result;
result =
s->com.com.in_plugin->has_subtitle(s->com.com.in_handle->priv,
s->com.com.in_index);
if(!result)
return 0;
result = s->com.com.in_plugin->read_subtitle_text(s->com.com.in_handle->priv,
&s->text, &s->text_alloc,
&s->subtitle_start,
&s->subtitle_duration,
s->com.com.in_index);
if(!result || ((t->end_time != GAVL_TIME_UNDEFINED) &&
(s->subtitle_start >= t->end_time)))
{
s->com.eof = 1;
return 0;
}
correct_subtitle_timestamp(&s->com, &s->subtitle_start,
&s->subtitle_duration, t);
return 1;
}
static int check_video_blend(video_stream_t * vs,
bg_transcoder_t * t, int64_t time)
{
gavl_overlay_t * tmp_ovl;
int i;
subtitle_stream_t * ss;
int ret = 0;
int current_changed = 0;
for(i = 0; i < vs->num_subtitle_streams; i++)
{
ss = vs->subtitle_streams[i];
if(ss->com.status != STREAM_STATE_ON)
continue;
/* Check if the overlay expired */
if(ss->has_current)
{
if(bg_overlay_too_old(time,
ss->current_ovl->frame->timestamp,
ss->current_ovl->frame->duration))
{
tmp_ovl = ss->current_ovl;
ss->current_ovl = ss->next_ovl;
ss->next_ovl = tmp_ovl;
ss->has_current = ss->has_next;
ss->has_next = 0;
if(ss->has_current)
current_changed = 1;
}
}
/* Check if the next overlay replaces the current one */
if(ss->has_next)
{
if(!bg_overlay_too_new(time,
ss->next_ovl->frame->timestamp))
{
tmp_ovl = ss->current_ovl;
ss->current_ovl = ss->next_ovl;
ss->next_ovl = tmp_ovl;
ss->has_current = 1;
ss->has_next = 0;
if(bg_overlay_too_old(time,
ss->current_ovl->frame->timestamp,
ss->current_ovl->frame->duration))
ss->has_current = 0;
else
current_changed = 1;
}
}
if(!ss->has_current && !ss->eof)
{
if(decode_subtitle_overlay(ss, t, ss->current_ovl))
{
ss->has_current = 1;
current_changed = 1;
}
else
continue;
}
if(!ss->has_next && !ss->eof)
{
if(decode_subtitle_overlay(ss, t, ss->next_ovl))
ss->has_next = 1;
}
if(ss->has_current &&
!bg_overlay_too_new(time,
ss->current_ovl->frame->timestamp))
{
ss->do_blend = 1;
ret = 1;
}
else
ss->do_blend = 0;
if(current_changed)
gavl_overlay_blend_context_set_overlay(ss->blend_context,
ss->current_ovl);
if(!ss->has_current && !ss->has_next &&
ss->eof)
ss->com.status = STREAM_STATE_FINISHED;
}
return ret;
}
#define SWAP_FRAMES \
tmp_frame=s->in_frame_1;\
s->in_frame_1=s->in_frame_2;\
s->in_frame_2=tmp_frame
static int video_iteration(video_stream_t * s, bg_transcoder_t * t)
{
int ret = 1;
int i;
int result;
if(!s->initialized)
{
if(t->start_time != GAVL_TIME_UNDEFINED)
{
s->start_time_scaled = gavl_time_scale(s->in_format.timescale,
t->start_time);
// bg_video_converter_reset(s->cnv, s->start_time_scaled);
}
s->initialized = 1;
}
result = s->in_func(s->in_data, s->frame, s->in_stream);
if(!result)
{
s->com.status = STREAM_STATE_FINISHED;
/* Set this also for all attached subtitle streams */
for(i = 0; i < s->num_subtitle_streams; i++)
s->subtitle_streams[i]->com.status = STREAM_STATE_FINISHED;
return ret;
}
s->com.time = gavl_time_unscale(s->out_format.timescale,
s->frame->timestamp);
// if(check_video_blend(s, t, s->com.time))
if(check_video_blend(s, t, s->frame->timestamp))
{
for(i = 0; i < s->num_subtitle_streams; i++)
{
if(s->subtitle_streams[i]->do_blend)
{
gavl_overlay_blend(s->subtitle_streams[i]->blend_context,
s->frame);
}
}
}
#ifdef DUMP_VIDEO_TIMESTAMPS
bg_debug("Output timestamp: %"PRId64"\n", s->frame->timestamp);
#endif
ret = bg_encoder_write_video_frame(t->enc,
s->frame,
s->com.out_index);
if(!ret)
bg_log(BG_LOG_ERROR, LOG_DOMAIN, "Encoding video failed");
s->frames_written++;
return ret;
}
/* Time offset of 0.5 seconds means, that we encode subtitles maximum
0.5 seconds before the subtitle should appear. This is only interesting
for formats, which don't allow random access to subtitles */
#define SUBTITLE_TIME_OFFSET (GAVL_TIME_SCALE/2)
static int subtitle_iteration(bg_transcoder_t * t)
{
int i, ret = 1;
subtitle_text_stream_t * st;
subtitle_stream_t * ss;
video_stream_t * vs;
gavl_video_frame_t * tmp_frame;
for(i = 0; i < t->num_subtitle_text_streams; i++)
{
st = &t->subtitle_text_streams[i];
if(!st->com.com.do_encode)
continue;
if(st->com.eof)
{
st->com.com.status = STREAM_STATE_FINISHED;
continue;
}
/* Check for decoding */
if(!st->com.has_current)
{
if(st->com.com.action == STREAM_ACTION_TRANSCODE)
{
st->com.has_current = decode_subtitle_text(st, t);
}
else if(st->com.com.action == STREAM_ACTION_TRANSCODE_OVERLAY)
{
st->com.has_current = decode_subtitle_overlay((subtitle_stream_t*)st, t, &st->com.ovl1);
if(st->com.has_current && st->com.do_convert)
{
gavl_video_convert(st->com.cnv, st->com.ovl1.frame, st->com.ovl2.frame);
tmp_frame = st->com.ovl1.frame;
st->com.ovl1.frame = st->com.ovl2.frame;
st->com.ovl2.frame = tmp_frame;
}
}
}
/* Check for encoding */
if(st->com.has_current)
{
vs = &(t->video_streams[st->com.video_stream]);
if(!vs->com.do_encode || (st->subtitle_start - vs->com.time < SUBTITLE_TIME_OFFSET))
{
if(st->com.com.action == STREAM_ACTION_TRANSCODE)
{
ret =
bg_encoder_write_subtitle_text(t->enc,
st->text, st->subtitle_start,
st->subtitle_duration,
st->com.com.out_index);
if(st->subtitle_start > t->time)
t->time = st->subtitle_start;
}
else if(st->com.com.action == STREAM_ACTION_TRANSCODE_OVERLAY)
{
ret =
bg_encoder_write_subtitle_overlay(t->enc,
&st->com.ovl1,
st->com.com.out_index);
if(st->com.ovl1.frame->timestamp > t->time)
t->time = st->com.ovl1.frame->timestamp;
}
st->com.has_current = 0;
}
}
if(!ret)
bg_log(BG_LOG_ERROR, LOG_DOMAIN, "Encoding subtitles failed");
}
if(!ret)
return ret;
for(i = 0; i < t->num_subtitle_overlay_streams; i++)
{
ss = &t->subtitle_overlay_streams[i];
if(!ss->com.do_encode)
continue;
if(ss->eof)
{
ss->com.status = STREAM_STATE_FINISHED;
continue;
}
/* Check for decoding */
if(!ss->has_current)
{
ss->has_current = decode_subtitle_overlay(ss, t, &ss->ovl1);
if(ss->has_current && ss->do_convert)
{
gavl_video_convert(ss->cnv, ss->ovl1.frame, ss->ovl2.frame);
tmp_frame = ss->ovl1.frame;
ss->ovl1.frame = ss->ovl2.frame;
ss->ovl2.frame = tmp_frame;
}
}
/* Check for encoding */
if(ss->has_current)
{
vs = &(t->video_streams[ss->video_stream]);
if(!vs->com.do_encode || (ss->ovl1.frame->timestamp - vs->com.time < SUBTITLE_TIME_OFFSET))
{
ret = bg_encoder_write_subtitle_overlay(t->enc,
&ss->ovl1,
ss->com.out_index);
if(ss->ovl1.frame->timestamp > t->time)
t->time = ss->ovl1.frame->timestamp;
ss->has_current = 0;
}
}
if(!ret)
{
bg_log(BG_LOG_ERROR, LOG_DOMAIN, "Encoding subtitles failed");
break;
}
}
return ret;
}
/* Parameter passing for the Transcoder */
#define SP_STR(s) if(!strcmp(name, # s)) \
{ \
t->s = bg_strdup(t->s, val->val_str); \
return; \
}
#define SP_INT(s) if(!strcmp(name, # s)) \
{ \
t->s = val->val_i; \
return; \
}
#define SP_TIME(s) if(!strcmp(name, # s)) \
{ \
t->s = val->val_time; \
return; \
}
static void
set_parameter_general(void * data, const char * name,
const bg_parameter_value_t * val)
{
int i, name_len;
bg_transcoder_t * t;
t = (bg_transcoder_t *)data;
if(!name)
{
/* Set start and end times */
if(!t->set_start_time)
t->start_time = GAVL_TIME_UNDEFINED;
if(!t->set_end_time)
t->end_time = GAVL_TIME_UNDEFINED;
/* Replace all '/' by '-' */
if(t->name)
{
name_len = strlen(t->name);
for(i = 0; i < name_len; i++)
{
if(t->name[i] == '/')
t->name[i] = '-';
}
}
/* Create the subdirectory if necessary */
if(t->subdir)
{
t->output_directory = bg_sprintf("%s/%s", t->output_path, t->subdir);
if(mkdir(t->output_directory,
S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IWOTH) == -1)
{
if(errno != EEXIST)
{
bg_log(BG_LOG_ERROR, LOG_DOMAIN, "Cannot create directory %s: %s, using default",
t->output_directory, strerror(errno));
t->output_directory = bg_strdup(t->output_directory, t->output_path);
}
}
else
bg_log(BG_LOG_INFO, LOG_DOMAIN, "Created directory %s", t->output_directory);
}
else
t->output_directory = bg_strdup(t->output_directory, t->output_path);
return;
}
SP_STR(name);
SP_STR(location);
SP_STR(plugin);
SP_STR(subdir);
SP_INT(track);
SP_INT(set_start_time);
SP_INT(set_end_time);
SP_TIME(start_time);
SP_TIME(end_time);
SP_INT(pp_only);
}
#undef SP_INT
#undef SP_TIME
#undef SP_STR
void bg_transcoder_add_message_queue(bg_transcoder_t * t,
bg_msg_queue_t * message_queue)
{
bg_msg_queue_list_add(t->message_queues, message_queue);
}
bg_transcoder_t * bg_transcoder_create()
{
bg_transcoder_t * ret;
ret = calloc(1, sizeof(*ret));
ret->timer = gavl_timer_create();
ret->message_queues = bg_msg_queue_list_create();
pthread_mutex_init(&(ret->stop_mutex), (pthread_mutexattr_t *)0);
return ret;
}
static void send_init_messages(bg_transcoder_t * t)
{
int i;
char * tmp_string;
if(t->pp_only)
{
tmp_string = bg_sprintf(TR("Postprocessing %s"), t->location);
}
else if(t->total_passes > 1)
{
tmp_string = bg_sprintf(TR("Transcoding %s [Track %d, Pass %d/%d]"),
t->location, t->track+1, t->pass, t->total_passes);
}
else
{
tmp_string = bg_sprintf(TR("Transcoding %s [Track %d]"), t->location, t->track+1);
}
bg_transcoder_send_msg_start(t->message_queues, tmp_string);
bg_log_notranslate(BG_LOG_INFO, LOG_DOMAIN, "%s", tmp_string);
free(tmp_string);
bg_transcoder_send_msg_metadata(t->message_queues, &t->metadata);
tmp_string = bg_metadata_to_string(&t->metadata, 1);
if(tmp_string)
{
bg_log(BG_LOG_INFO, LOG_DOMAIN, "Metadata:\n%s", tmp_string);
free(tmp_string);
}
if(!t->pp_only)
{
bg_transcoder_send_msg_num_audio_streams(t->message_queues, t->num_audio_streams);
for(i = 0; i < t->num_audio_streams; i++)
{
if(t->audio_streams[i].com.do_decode)
{
bg_transcoder_send_msg_audio_format(t->message_queues, i, &(t->audio_streams[i].in_format),
&(t->audio_streams[i].out_format));
tmp_string = bg_audio_format_to_string(&(t->audio_streams[i].in_format), 1);
bg_log(BG_LOG_INFO, LOG_DOMAIN, "Audio stream %d input format:\n%s",
i+1, tmp_string);
free(tmp_string);
tmp_string = bg_audio_format_to_string(&(t->audio_streams[i].out_format), 1);
bg_log(BG_LOG_INFO, LOG_DOMAIN, "Audio stream %d output format:\n%s",
i+1, tmp_string);
free(tmp_string);
}
}
bg_transcoder_send_msg_num_video_streams(t->message_queues, t->num_video_streams);
for(i = 0; i < t->num_video_streams; i++)
{
if(t->video_streams[i].com.do_decode)
{
bg_transcoder_send_msg_video_format(t->message_queues, i, &(t->video_streams[i].in_format),
&(t->video_streams[i].out_format));
tmp_string = bg_video_format_to_string(&(t->video_streams[i].in_format), 0);
bg_log(BG_LOG_INFO, LOG_DOMAIN, "Video stream %d input format:\n%s",
i+1, tmp_string);
free(tmp_string);
tmp_string = bg_video_format_to_string(&(t->video_streams[i].out_format), 0);
bg_log(BG_LOG_INFO, LOG_DOMAIN, "Video stream %d output format:\n%s",
i+1, tmp_string);
free(tmp_string);
}
}
for(i = 0; i < t->num_subtitle_text_streams; i++)
{
if(t->subtitle_text_streams[i].com.com.do_decode)
{
switch(t->subtitle_text_streams[i].com.com.action)
{
case STREAM_ACTION_BLEND:
bg_log(BG_LOG_INFO, LOG_DOMAIN, "Text subtitle stream %d: Blending onto video stream %d",
i+1, t->subtitle_text_streams[i].com.video_stream);
break;
case STREAM_ACTION_TRANSCODE:
case STREAM_ACTION_TRANSCODE_OVERLAY:
bg_log(BG_LOG_INFO, LOG_DOMAIN, "Text subtitle stream %d: Exporting to file", i+1);
break;
}
}
}
for(i = 0; i < t->num_subtitle_overlay_streams; i++)
{
if(t->subtitle_overlay_streams[i].com.do_decode)
{
switch(t->subtitle_overlay_streams[i].com.action)
{
case STREAM_ACTION_BLEND:
bg_log(BG_LOG_INFO, LOG_DOMAIN, "Text subtitle stream %d: Blending onto video stream %d",
i+1, t->subtitle_overlay_streams[i].video_stream);
break;
case STREAM_ACTION_TRANSCODE:
case STREAM_ACTION_TRANSCODE_OVERLAY:
bg_log(BG_LOG_INFO, LOG_DOMAIN, "Text subtitle stream %d: Exporting to file", i+1);
break;
}
}
}
}
}
static void send_file(const char * name)
{
char * command;
command = bg_sprintf("gmerlin_remote -add \"%s\"\n", name);
system(command);
free(command);
}
static void send_file_messages(bg_transcoder_t * t)
{
int i;
for(i = 0; i < t->num_output_files; i++)
{
bg_transcoder_send_msg_file(t->message_queues,
t->output_files[i],
t->pp_only);
if(t->send_finished)
send_file(t->output_files[i]);
}
}
static int open_input(bg_transcoder_t * ret)
{
const bg_plugin_info_t * plugin_info;
plugin_info = bg_plugin_find_by_name(ret->plugin_reg, ret->plugin);
if(!plugin_info)
{
bg_log(BG_LOG_ERROR, LOG_DOMAIN, "Cannot find plugin %s", ret->plugin);
goto fail;
}
ret->in_handle = bg_plugin_load(ret->plugin_reg, plugin_info);
if(!ret->in_handle)
{
bg_log(BG_LOG_ERROR, LOG_DOMAIN, "Cannot open plugin %s", ret->plugin);
goto fail;
}
ret->in_plugin = (bg_input_plugin_t*)(ret->in_handle->plugin);
if(ret->in_plugin->common.set_parameter &&
ret->in_plugin->common.get_parameters &&
ret->transcoder_track->input_section)
{
bg_cfg_section_apply(ret->transcoder_track->input_section,
ret->in_plugin->common.get_parameters(ret->in_handle->priv),
ret->in_plugin->common.set_parameter, ret->in_handle->priv);
}
if(!ret->in_plugin->open(ret->in_handle->priv, ret->location))
{
bg_log(BG_LOG_ERROR, LOG_DOMAIN, "Cannot open %s with plugin %s",
ret->location, ret->plugin);
goto fail;
}
if(bg_string_is_url(ret->location))
ret->is_url = 1;
/* Select track and get track info */
if(ret->in_plugin->get_num_tracks &&
(ret->track >= ret->in_plugin->get_num_tracks(ret->in_handle->priv)))
{
bg_log(BG_LOG_ERROR, LOG_DOMAIN, "Invalid track number %d", ret->track);
goto fail;
}
ret->track_info = ret->in_plugin->get_track_info(ret->in_handle->priv,
ret->track);
if(ret->in_plugin->set_track)
ret->in_plugin->set_track(ret->in_handle->priv, ret->track);
return 1;
fail:
return 0;
}
static void create_streams(bg_transcoder_t * ret,
bg_transcoder_track_t * track)
{
int i;
/* Allocate streams */
ret->num_audio_streams = track->num_audio_streams;
if(ret->num_audio_streams)
ret->audio_streams = calloc(ret->num_audio_streams,
sizeof(*(ret->audio_streams)));
ret->num_video_streams = track->num_video_streams;
if(ret->num_video_streams)
ret->video_streams = calloc(ret->num_video_streams,
sizeof(*(ret->video_streams)));
ret->num_subtitle_text_streams = track->num_subtitle_text_streams;
if(ret->num_subtitle_text_streams)
ret->subtitle_text_streams = calloc(ret->num_subtitle_text_streams,
sizeof(*(ret->subtitle_text_streams)));
ret->num_subtitle_overlay_streams = track->num_subtitle_overlay_streams;
if(ret->num_subtitle_overlay_streams)
ret->subtitle_overlay_streams = calloc(ret->num_subtitle_overlay_streams,
sizeof(*(ret->subtitle_overlay_streams)));
/* Prepare streams */
ret->num_audio_streams_real = 0;
for(i = 0; i < ret->num_audio_streams; i++)
{
init_audio_stream(&(ret->audio_streams[i]),
&(track->audio_streams[i]),
i, ret->plugin_reg);
if(ret->audio_streams[i].com.action == STREAM_ACTION_TRANSCODE)
ret->num_audio_streams_real++;
}
ret->num_video_streams_real = 0;
for(i = 0; i < ret->num_video_streams; i++)
{
init_video_stream(&(ret->video_streams[i]),
&(track->video_streams[i]),
i, ret->plugin_reg);
if(ret->video_streams[i].com.action == STREAM_ACTION_TRANSCODE)
ret->num_video_streams_real++;
}
ret->num_subtitle_text_streams_real = 0;
ret->num_subtitle_overlay_streams_real = 0;
for(i = 0; i < ret->num_subtitle_text_streams; i++)
{
init_subtitle_text_stream(&(ret->subtitle_text_streams[i]),
&(track->subtitle_text_streams[i]));
if(ret->subtitle_text_streams[i].com.com.action == STREAM_ACTION_TRANSCODE)
ret->num_subtitle_text_streams_real++;
else if(ret->subtitle_text_streams[i].com.com.action == STREAM_ACTION_TRANSCODE_OVERLAY)
ret->num_subtitle_overlay_streams_real++;
}
for(i = 0; i < ret->num_subtitle_overlay_streams; i++)
{
init_subtitle_overlay_stream(&(ret->subtitle_overlay_streams[i]),
&(track->subtitle_overlay_streams[i]));
if(ret->subtitle_overlay_streams[i].com.action == STREAM_ACTION_TRANSCODE)
ret->num_subtitle_overlay_streams_real++;
}
}
static int start_input(bg_transcoder_t * ret)
{
int i;
for(i = 0; i < ret->num_audio_streams; i++)
{
if(ret->audio_streams[i].com.do_decode)
{
start_audio_stream_i(&(ret->audio_streams[i]),
ret->in_handle);
}
}
for(i = 0; i < ret->num_video_streams; i++)
{
if(ret->video_streams[i].com.do_decode)
{
start_video_stream_i(&(ret->video_streams[i]),
ret->in_handle);
}
}
for(i = 0; i < ret->num_subtitle_text_streams; i++)
{
if(ret->subtitle_text_streams[i].com.com.do_decode)
{
start_subtitle_stream_i((subtitle_stream_t*)(&(ret->subtitle_text_streams[i])),
ret->in_handle);
}
}
for(i = 0; i < ret->num_subtitle_overlay_streams; i++)
{
if(ret->subtitle_overlay_streams[i].com.do_decode)
{
start_subtitle_stream_i(&(ret->subtitle_overlay_streams[i]),
ret->in_handle);
}
}
if(ret->in_plugin->start)
{
if(!ret->in_plugin->start(ret->in_handle->priv))
{
bg_log(BG_LOG_ERROR, LOG_DOMAIN, "Starting input plugin failed");
goto fail;
}
}
/* Check if we must seek to the start position */
if(ret->start_time != GAVL_TIME_UNDEFINED)
{
if(!ret->in_plugin->seek)
{
bg_log(BG_LOG_ERROR, LOG_DOMAIN, "Cannot seek to start point");
goto fail;
}
ret->in_plugin->seek(ret->in_handle->priv, &(ret->start_time), GAVL_TIME_SCALE);
/* This happens, if the decoder reached EOF during the seek */
if(ret->start_time == GAVL_TIME_UNDEFINED)
{
bg_log(BG_LOG_ERROR, LOG_DOMAIN, "Cannot seek to start point");
goto fail;
}
}
/* Check, if the user entered bullshit */
if((ret->start_time != GAVL_TIME_UNDEFINED) &&
(ret->end_time != GAVL_TIME_UNDEFINED) &&
(ret->end_time < ret->start_time))
{
bg_log(BG_LOG_ERROR, LOG_DOMAIN, "End time if before start time");
goto fail;
}
return 1;
fail:
return 0;
}
static void check_passes(bg_transcoder_t * ret)
{
int i;
ret->total_passes = 1;
for(i = 0; i < ret->num_audio_streams; i++)
{
if((ret->audio_streams[i].com.action == STREAM_ACTION_TRANSCODE) &&
ret->audio_streams[i].normalize)
{
ret->total_passes = 2;
return;
}
}
for(i = 0; i < ret->num_video_streams; i++)
{
if((ret->video_streams[i].com.action == STREAM_ACTION_TRANSCODE) &&
ret->video_streams[i].twopass)
{
ret->total_passes = 2;
return;
}
}
}
static int setup_pass(bg_transcoder_t * ret)
{
int i;
int result = 0;
for(i = 0; i < ret->num_audio_streams; i++)
{
/* Reset the samples already decoded */
ret->audio_streams[i].samples_read = 0;
if(ret->audio_streams[i].com.action != STREAM_ACTION_TRANSCODE)
{
ret->audio_streams[i].com.do_decode = 0;
ret->audio_streams[i].com.do_encode = 0;
}
else if(ret->pass == ret->total_passes)
{
ret->audio_streams[i].com.do_decode = 1;
ret->audio_streams[i].com.do_encode = 1;
result = 1;
}
else if((ret->pass == 1) && (ret->audio_streams[i].normalize))
{
ret->audio_streams[i].com.do_decode = 1;
ret->audio_streams[i].com.do_encode = 0;
result = 1;
}
else
{
ret->audio_streams[i].com.do_decode = 0;
ret->audio_streams[i].com.do_encode = 0;
}
if(!ret->audio_streams[i].com.do_decode)
ret->audio_streams[i].com.status = STREAM_STATE_OFF;
else
ret->audio_streams[i].com.status = STREAM_STATE_ON;
}
for(i = 0; i < ret->num_video_streams; i++)
{
if(ret->video_streams[i].com.action != STREAM_ACTION_TRANSCODE)
{
ret->video_streams[i].com.do_decode = 0;
ret->video_streams[i].com.do_encode = 0;
}
else if((ret->pass == ret->total_passes) || ret->video_streams[i].twopass)
{
ret->video_streams[i].com.do_decode = 1;
ret->video_streams[i].com.do_encode = 1;
result = 1;
}
else
{
ret->video_streams[i].com.do_decode = 0;
ret->video_streams[i].com.do_encode = 0;
}
if(!ret->video_streams[i].com.do_decode)
ret->video_streams[i].com.status = STREAM_STATE_OFF;
else
ret->video_streams[i].com.status = STREAM_STATE_ON;
}
/* Subtitles */
for(i = 0; i < ret->num_subtitle_text_streams; i++)
{
switch(ret->subtitle_text_streams[i].com.com.action)
{
case STREAM_ACTION_FORGET:
ret->subtitle_text_streams[i].com.com.do_decode = 0;
ret->subtitle_text_streams[i].com.com.do_encode = 0;
break;
case STREAM_ACTION_BLEND:
if((ret->video_streams[ret->subtitle_text_streams[i].com.video_stream].twopass) ||
(ret->pass == ret->total_passes))
{
ret->subtitle_text_streams[i].com.com.do_decode = 1;
ret->subtitle_text_streams[i].com.com.do_encode = 0;
}
else
{
ret->subtitle_text_streams[i].com.com.do_decode = 0;
ret->subtitle_text_streams[i].com.com.do_encode = 0;
}
result = 1;
break;
case STREAM_ACTION_TRANSCODE:
case STREAM_ACTION_TRANSCODE_OVERLAY:
if(ret->pass == ret->total_passes)
{
ret->subtitle_text_streams[i].com.com.do_decode = 1;
ret->subtitle_text_streams[i].com.com.do_encode = 1;
result = 1;
}
else
{
ret->subtitle_text_streams[i].com.com.do_decode = 0;
ret->subtitle_text_streams[i].com.com.do_encode = 0;
}
break;
default:
bg_log(BG_LOG_WARNING, LOG_DOMAIN, "Subtitle stream cannot be handled");
ret->subtitle_text_streams[i].com.com.do_decode = 0;
ret->subtitle_text_streams[i].com.com.do_encode = 0;
ret->subtitle_text_streams[i].com.com.action = STREAM_ACTION_FORGET;
break;
}
if(!ret->subtitle_text_streams[i].com.com.do_decode)
ret->subtitle_text_streams[i].com.com.status = STREAM_STATE_OFF;
else
ret->subtitle_text_streams[i].com.com.status = STREAM_STATE_ON;
}
for(i = 0; i < ret->num_subtitle_overlay_streams; i++)
{
switch(ret->subtitle_overlay_streams[i].com.action)
{
case STREAM_ACTION_FORGET:
ret->subtitle_overlay_streams[i].com.do_decode = 0;
ret->subtitle_overlay_streams[i].com.do_encode = 0;
break;
case STREAM_ACTION_BLEND:
if(ret->video_streams[ret->subtitle_overlay_streams[i].video_stream].com.do_encode)
{
ret->subtitle_overlay_streams[i].com.do_decode = 1;
ret->subtitle_overlay_streams[i].com.do_encode = 1;
result = 1;
}
else
{
ret->subtitle_overlay_streams[i].com.do_decode = 0;
ret->subtitle_overlay_streams[i].com.do_encode = 0;
}
break;
case STREAM_ACTION_TRANSCODE:
if(ret->pass == ret->total_passes)
{
ret->subtitle_overlay_streams[i].com.do_decode = 1;
ret->subtitle_overlay_streams[i].com.do_encode = 1;
result = 1;
}
else
{
ret->subtitle_overlay_streams[i].com.do_decode = 0;
ret->subtitle_overlay_streams[i].com.do_encode = 0;
}
break;
default:
bg_log(BG_LOG_WARNING, LOG_DOMAIN, "Subtitle stream cannot be handled");
ret->subtitle_overlay_streams[i].com.do_decode = 0;
ret->subtitle_overlay_streams[i].com.do_encode = 0;
ret->subtitle_overlay_streams[i].com.action = STREAM_ACTION_FORGET;
break;
}
if(!ret->subtitle_overlay_streams[i].com.do_decode)
ret->subtitle_overlay_streams[i].com.status = STREAM_STATE_OFF;
else
ret->subtitle_overlay_streams[i].com.status = STREAM_STATE_ON;
}
return result;
}
static int create_output_file_cb(void * priv, const char * filename)
{
bg_transcoder_t * t = priv;
if(!strcmp(filename, t->location))
{
bg_log(BG_LOG_ERROR, LOG_DOMAIN, "Input and output are the same file");
return 0;
}
if(t->pass == t->total_passes)
add_output_file(t, filename);
return 1;
}
static int init_encoders(bg_transcoder_t * ret)
{
int i;
char * tmp_string;
ret->enc = bg_encoder_create(ret->plugin_reg,
NULL,
ret->transcoder_track,
BG_STREAM_AUDIO |
BG_STREAM_VIDEO |
BG_STREAM_SUBTITLE_TEXT |
BG_STREAM_SUBTITLE_OVERLAY,
BG_PLUGIN_FILE);
ret->cb.create_output_file = create_output_file_cb;
ret->cb.data = ret;
bg_encoder_set_callbacks(ret->enc, &ret->cb);
tmp_string = bg_sprintf("%s/%s", ret->output_directory, ret->name);
bg_encoder_open(ret->enc, tmp_string,
&ret->metadata,
ret->transcoder_track->chapter_list);
for(i = 0; i < ret->num_audio_streams; i++)
{
if(!ret->audio_streams[i].com.do_decode) /* If we don't encode we still need to open the
plugin to get the final output format */
continue;
add_audio_stream(&(ret->audio_streams[i]), &ret->transcoder_track->audio_streams[i],
ret);
}
/* Video streams: Must be added before the subtitle streams, because we need to know
the output format (at least the output size) of the stream */
for(i = 0; i < ret->num_video_streams; i++)
{
if(!ret->video_streams[i].com.do_decode)/* If we don't encode we still need to open the
plugin to get the final output format */
continue;
add_video_stream(&(ret->video_streams[i]),
&(ret->transcoder_track->video_streams[i]), ret);
set_video_pass(ret, i);
}
for(i = 0; i < ret->num_subtitle_text_streams; i++)
{
if(!ret->subtitle_text_streams[i].com.com.do_encode)
continue;
add_subtitle_text_stream(&ret->subtitle_text_streams[i],
&ret->transcoder_track->subtitle_text_streams[i], ret);
}
for(i = 0; i < ret->num_subtitle_overlay_streams; i++)
{
if(!ret->subtitle_overlay_streams[i].com.do_encode)
continue;
add_subtitle_overlay_stream(&ret->subtitle_overlay_streams[i],
&ret->transcoder_track->subtitle_overlay_streams[i], ret);
}
return bg_encoder_start(ret->enc);
}
static int init_audio_converter(audio_stream_t * ret, bg_transcoder_t * t)
{
gavl_audio_options_t * opt;
bg_encoder_get_audio_format(t->enc,
ret->com.out_index,
&(ret->out_format));
gavl_audio_format_copy(&ret->pipe_format, &ret->out_format);
if(ret->options.force_format != GAVL_SAMPLE_NONE)
ret->pipe_format.sample_format = ret->options.force_format;
bg_audio_filter_chain_set_out_format(ret->fc, &ret->pipe_format);
/* Initialize output converter */
opt = gavl_audio_converter_get_options(ret->cnv_out);
gavl_audio_options_copy(opt, ret->options.opt);
ret->do_convert_out = gavl_audio_converter_init(ret->cnv_out,
&(ret->pipe_format),
&(ret->out_format));
/* Create frames (could be left from the previous pass) */
if(ret->do_convert_out && !ret->pipe_frame)
ret->pipe_frame = gavl_audio_frame_create(&(ret->pipe_format));
if(!ret->out_frame)
ret->out_frame = gavl_audio_frame_create(&(ret->out_format));
return 1;
}
static int init_video_converter(video_stream_t * ret, bg_transcoder_t * t)
{
bg_encoder_get_video_format(t->enc,
ret->com.out_index, &ret->out_format);
/* Initialize converter */
bg_video_filter_chain_set_out_format(ret->fc, &(ret->out_format));
/* Create frames */
if(!ret->frame)
ret->frame = gavl_video_frame_create(&(ret->out_format));
gavl_video_frame_clear(ret->frame, &(ret->out_format));
return 1;
}
static void subtitle_init_blend(subtitle_stream_t * ss, video_stream_t * vs)
{
subtitle_text_stream_t * sst;
vs->subtitle_streams =
realloc(vs->subtitle_streams,
sizeof(*(vs->subtitle_streams))*(vs->num_subtitle_streams+1));
vs->subtitle_streams[vs->num_subtitle_streams] = ss;
vs->num_subtitle_streams++;
/* Check whether to initialize the text renderer */
if(ss->com.type == STREAM_TYPE_SUBTITLE_TEXT)
{
sst = (subtitle_text_stream_t*)ss;
bg_text_renderer_init(sst->textrenderer,
&(vs->out_format),
&(ss->in_format));
}
if(!ss->ovl1.frame) ss->ovl1.frame = gavl_video_frame_create(&(ss->in_format));
if(!ss->ovl2.frame) ss->ovl2.frame = gavl_video_frame_create(&(ss->in_format));
ss->current_ovl = &(ss->ovl1);
ss->next_ovl = &(ss->ovl2);
gavl_overlay_blend_context_init(ss->blend_context,
&(vs->out_format), &(ss->in_format));
gavl_video_format_copy(&ss->out_format, &vs->out_format);
}
static void subtitle_init_encode_text(subtitle_text_stream_t * ss,
bg_transcoder_t * t)
{
/* Nothing to do here for now */
bg_encoder_get_subtitle_text_timescale(t->enc,
ss->com.com.out_index,
&ss->com.out_format.timescale);
}
static void subtitle_init_encode_overlay(subtitle_stream_t * ss,
bg_transcoder_t * t)
{
subtitle_text_stream_t * sst;
bg_encoder_get_subtitle_overlay_format(t->enc,
ss->com.out_index,
&(ss->out_format));
/* Check whether to initialize the text renderer */
if(ss->com.type == STREAM_TYPE_SUBTITLE_TEXT)
{
sst = (subtitle_text_stream_t*)ss;
bg_text_renderer_init(sst->textrenderer,
&(ss->out_format),
&(ss->in_format));
// gavl_video_format_dump(&(ss->in_format));
// gavl_video_format_dump(&(ss->out_format));
}
ss->do_convert = gavl_video_converter_init(ss->cnv, &(ss->in_format),
&(ss->out_format));
if(!ss->ovl1.frame) ss->ovl1.frame = gavl_video_frame_create(&(ss->in_format));
if(ss->do_convert)
{
if(!ss->ovl2.frame) ss->ovl2.frame = gavl_video_frame_create(&(ss->out_format));
}
}
static int init_converters(bg_transcoder_t * ret)
{
int i;
for(i = 0; i < ret->num_audio_streams; i++)
{
if(!ret->audio_streams[i].com.do_decode)
continue;
if(!init_audio_converter(&ret->audio_streams[i], ret))
return 0;
}
for(i = 0; i < ret->num_video_streams; i++)
{
if(!ret->video_streams[i].com.do_decode)
continue;
if(!init_video_converter(&ret->video_streams[i], ret))
return 0;
if(ret->video_streams[i].subtitle_streams)
{
free(ret->video_streams[i].subtitle_streams);
ret->video_streams[i].subtitle_streams = (subtitle_stream_t**)0;
ret->video_streams[i].num_subtitle_streams = 0;
}
}
for(i = 0; i < ret->num_subtitle_text_streams; i++)
{
if(!ret->subtitle_text_streams[i].com.com.do_decode)
continue;
switch(ret->subtitle_text_streams[i].com.com.action)
{
case STREAM_ACTION_BLEND:
subtitle_init_blend((subtitle_stream_t*)(&ret->subtitle_text_streams[i]),
&ret->video_streams[ret->subtitle_text_streams[i].com.video_stream]);
break;
case STREAM_ACTION_TRANSCODE:
subtitle_init_encode_text((&ret->subtitle_text_streams[i]), ret);
break;
case STREAM_ACTION_TRANSCODE_OVERLAY:
subtitle_init_encode_overlay((subtitle_stream_t*)&ret->subtitle_text_streams[i], ret);
break;
}
}
for(i = 0; i < ret->num_subtitle_overlay_streams; i++)
{
if(!ret->subtitle_overlay_streams[i].com.do_decode)
continue;
switch(ret->subtitle_overlay_streams[i].com.action)
{
case STREAM_ACTION_BLEND:
subtitle_init_blend(&ret->subtitle_overlay_streams[i],
&ret->video_streams[ret->subtitle_overlay_streams[i].video_stream]);
break;
case STREAM_ACTION_TRANSCODE:
subtitle_init_encode_overlay(&ret->subtitle_overlay_streams[i], ret);
break;
}
}
return 1;
}
static void init_normalize(bg_transcoder_t * ret)
{
int i;
double absolute;
double volume_dB = 0.0;
for(i = 0; i < ret->num_audio_streams; i++)
{
if(!ret->audio_streams[i].normalize)
continue;
if(ret->pass == 1)
{
gavl_peak_detector_set_format(ret->audio_streams[i].peak_detector,
&ret->audio_streams[i].out_format);
}
else if(ret->pass == ret->total_passes)
{
gavl_volume_control_set_format(ret->audio_streams[i].volume_control,
&ret->audio_streams[i].pipe_format);
/* Set the volume */
gavl_peak_detector_get_peak(ret->audio_streams[i].peak_detector,
(double*)0, (double*)0, &absolute);
if(absolute == 0.0)
{
gavl_volume_control_set_volume(ret->audio_streams[i].volume_control, volume_dB);
bg_log(BG_LOG_WARNING, LOG_DOMAIN, "Zero peaks detected (silent file?). Disabling normalization.");
}
else
{
volume_dB = - 20.0 * log10(absolute);
gavl_volume_control_set_volume(ret->audio_streams[i].volume_control, volume_dB);
bg_log(BG_LOG_INFO, LOG_DOMAIN, "Correcting volume by %.2f dB", volume_dB);
}
}
}
}
int bg_transcoder_init(bg_transcoder_t * ret,
bg_plugin_registry_t * plugin_reg,
bg_transcoder_track_t * track)
{
ret->plugin_reg = plugin_reg;
ret->transcoder_track = track;
/* Initialize encoder info */
/* Set general parameter */
bg_cfg_section_apply(track->general_section,
track->general_parameters,
set_parameter_general,
ret);
/* Set Metadata */
bg_cfg_section_apply(track->metadata_section,
track->metadata_parameters,
bg_metadata_set_parameter,
&(ret->metadata));
/* Postprocess only: Send messages and return */
if(ret->pp_only)
{
ret->output_filename = bg_strdup(ret->output_filename, ret->location);
send_init_messages(ret);
ret->state = STREAM_STATE_FINISHED;
return 1;
}
/* Open input plugin */
if(!open_input(ret))
goto fail;
create_streams(ret, track);
/* Set first transcoding pass */
check_passes(ret);
ret->pass = 1;
if(!setup_pass(ret))
{
bg_log(BG_LOG_ERROR, LOG_DOMAIN, "No stream to encode");
goto fail;
}
/* Start input plugin */
if(!start_input(ret))
goto fail;
set_input_formats(ret);
/* Set up the streams in the encoders */
if(!init_encoders(ret))
goto fail;
/* Set formats */
if(!init_converters(ret))
goto fail;
/* Init normalizing */
init_normalize(ret);
/* Send messages */
send_init_messages(ret);
/* Set duration */
if(ret->set_start_time && ret->set_end_time)
ret->duration = ret->end_time - ret->start_time;
else if(ret->set_start_time)
ret->duration = ret->track_info->duration - ret->start_time;
else if(ret->set_end_time)
ret->duration = ret->end_time;
else
ret->duration = ret->track_info->duration;
/* Start timer */
gavl_timer_start(ret->timer);
ret->state = TRANSCODER_STATE_RUNNING;
return 1;
fail:
return 0;
}
#define FREE_STR(str) if(str)free(str);
static void cleanup_stream(stream_t * s)
{
}
static void cleanup_audio_stream(audio_stream_t * s)
{
cleanup_stream(&(s->com));
/* Free all resources */
if(s->out_frame)
gavl_audio_frame_destroy(s->out_frame);
if(s->pipe_frame)
gavl_audio_frame_destroy(s->pipe_frame);
if(s->cnv_out)
gavl_audio_converter_destroy(s->cnv_out);
if(s->fc)
bg_audio_filter_chain_destroy(s->fc);
if(s->volume_control)
gavl_volume_control_destroy(s->volume_control);
if(s->peak_detector)
gavl_peak_detector_destroy(s->peak_detector);
bg_gavl_audio_options_free(&s->options);
}
static void cleanup_video_stream(video_stream_t * s)
{
cleanup_stream(&(s->com));
/* Free all resources */
if(s->frame)
gavl_video_frame_destroy(s->frame);
if(s->fc)
bg_video_filter_chain_destroy(s->fc);
if(s->subtitle_streams)
free(s->subtitle_streams);
/* Delete stats file */
if(s->stats_file)
{
remove(s->stats_file);
free(s->stats_file);
}
bg_gavl_video_options_free(&s->options);
}
static void cleanup_subtitle_stream(subtitle_stream_t * s)
{
subtitle_text_stream_t * ts;
if(s->ovl1.frame) gavl_video_frame_destroy(s->ovl1.frame);
if(s->ovl2.frame) gavl_video_frame_destroy(s->ovl2.frame);
if(s->blend_context) gavl_overlay_blend_context_destroy(s->blend_context);
if(s->com.type == STREAM_TYPE_SUBTITLE_TEXT)
{
ts = (subtitle_text_stream_t*)s;
if(ts->textrenderer) bg_text_renderer_destroy(ts->textrenderer);
}
}
static void reset_streams(bg_transcoder_t * t)
{
int i;
for(i = 0; i < t->num_audio_streams; i++)
{
t->audio_streams[i].samples_read = 0;
t->audio_streams[i].com.time = 0;
}
for(i = 0; i < t->num_video_streams; i++)
{
t->video_streams[i].frames_written = 0;
t->video_streams[i].com.time = 0;
}
}
static void close_input(bg_transcoder_t * t)
{
if(t->pp_only)
return;
if(t->in_plugin->stop)
t->in_plugin->stop(t->in_handle->priv);
t->in_plugin->close(t->in_handle->priv);
bg_plugin_unref(t->in_handle);
t->in_handle = (bg_plugin_handle_t*)0;
}
/* Switch to next pass */
static void next_pass(bg_transcoder_t * t)
{
char * tmp_string;
bg_encoder_destroy(t->enc, 1);
close_input(t);
t->pass++;
/* Reset stream times */
reset_streams(t);
gavl_timer_stop(t->timer);
gavl_timer_set(t->timer, 0);
t->percentage_done = 0.0;
t->time = 0;
t->last_seconds = 0.0;
open_input(t);
/* Decide, which stream will be en/decoded*/
setup_pass(t);
start_input(t);
/* Some streams don't have this already */
set_input_formats(t);
/* Initialize encoding plugins */
init_encoders(t);
/* Set formats */
init_converters(t);
/* Init normalizing */
init_normalize(t);
/* Send message */
tmp_string = bg_sprintf(TR("Transcoding %s [Track %d, Pass %d/%d]"), t->location, t->track+1, t->pass, t->total_passes);
bg_transcoder_send_msg_start(t->message_queues, tmp_string);
bg_log_notranslate(BG_LOG_INFO, LOG_DOMAIN, "%s", tmp_string);
free(tmp_string);
gavl_timer_start(t->timer);
}
/*
* Do one iteration.
* If return value is FALSE, we are done
*/
int bg_transcoder_iteration(bg_transcoder_t * t)
{
int i;
gavl_time_t time;
stream_t * stream = (stream_t*)0;
gavl_time_t real_time;
double real_seconds;
double remaining_seconds;
int done = 1;
if(t->pp_only)
{
t->state = TRANSCODER_STATE_FINISHED;
bg_transcoder_send_msg_finished(t->message_queues);
log_transcoding_time(t);
return 0;
}
time = GAVL_TIME_MAX;
/* Find the stream with the smallest time */
for(i = 0; i < t->num_audio_streams; i++)
{
if(t->audio_streams[i].com.status != STREAM_STATE_ON)
continue;
done = 0;
if(t->audio_streams[i].com.time < time)
{
time = t->audio_streams[i].com.time;
stream = &(t->audio_streams[i].com);
}
}
for(i = 0; i < t->num_video_streams; i++)
{
if(t->video_streams[i].com.status != STREAM_STATE_ON)
continue;
done = 0;
if(t->video_streams[i].com.time < time)
{
time = t->video_streams[i].com.time;
stream = &(t->video_streams[i].com);
}
}
for(i = 0; i < t->num_subtitle_text_streams; i++)
{
if(t->subtitle_text_streams[i].com.com.status != STREAM_STATE_ON)
continue;
done = 0;
}
for(i = 0; i < t->num_subtitle_overlay_streams; i++)
{
if(t->subtitle_overlay_streams[i].com.status != STREAM_STATE_ON)
continue;
done = 0;
}
if(done)
{
if(t->pass < t->total_passes)
{
log_transcoding_time(t);
next_pass(t);
return 1;
}
else
{
t->state = TRANSCODER_STATE_FINISHED;
bg_transcoder_send_msg_finished(t->message_queues);
log_transcoding_time(t);
return 0;
}
}
/* Do the actual transcoding */
/* Subtitle iteration must always be done */
if(!subtitle_iteration(t))
{
t->state = TRANSCODER_STATE_ERROR;
bg_transcoder_send_msg_error(t->message_queues);
return 0;
}
if(stream)
{
if(stream->type == STREAM_TYPE_AUDIO)
{
if(!audio_iteration((audio_stream_t*)stream, t))
{
t->state = TRANSCODER_STATE_ERROR;
bg_transcoder_send_msg_error(t->message_queues);
return 0;
}
}
else if(stream->type == STREAM_TYPE_VIDEO)
{
if(!video_iteration((video_stream_t*)stream, t))
{
t->state = TRANSCODER_STATE_ERROR;
bg_transcoder_send_msg_error(t->message_queues);
return 0;
}
}
if(stream->time > t->time)
t->time = stream->time;
}
/* Update status */
real_time = gavl_timer_get(t->timer);
real_seconds = gavl_time_to_seconds(real_time);
t->percentage_done =
gavl_time_to_seconds(t->time) /
gavl_time_to_seconds(t->duration);
if(t->percentage_done < 0.0)
t->percentage_done = 0.0;
if(t->percentage_done > 1.0)
t->percentage_done = 1.0;
if(t->percentage_done == 0.0)
remaining_seconds = 0.0;
else
{
remaining_seconds = real_seconds *
(1.0 / t->percentage_done - 1.0);
}
t->remaining_time =
gavl_seconds_to_time(remaining_seconds);
if(real_seconds - t->last_seconds > 1.0)
{
bg_transcoder_send_msg_progress(t->message_queues, t->percentage_done, t->remaining_time);
t->last_seconds = real_seconds;
}
return 1;
}
void bg_transcoder_destroy(bg_transcoder_t * t)
{
int i;
int do_delete = 0;
char tmp_string[128];
if((t->state == TRANSCODER_STATE_RUNNING) && t->delete_incomplete &&
!t->is_url)
do_delete = 1;
else if(t->state == TRANSCODER_STATE_INIT)
do_delete = 1;
else if(t->state == TRANSCODER_STATE_ERROR)
do_delete = 1;
/* Close all encoders so the files are finished */
if(t->enc)
bg_encoder_destroy(t->enc, do_delete);
/* Send created files to gmerlin */
if((t->state != TRANSCODER_STATE_RUNNING) && !do_delete)
{
send_file_messages(t);
}
free_output_files(t);
/* Cleanup streams */
for(i = 0; i < t->num_video_streams; i++)
{
if((t->video_streams[i].com.action != STREAM_ACTION_FORGET) && !do_delete)
{
sprintf(tmp_string, "%" PRId64, t->video_streams[i].frames_written);
bg_log(BG_LOG_INFO, LOG_DOMAIN,
"Video stream %d: Transcoded %s frames", i+1,
tmp_string);
}
cleanup_video_stream(&(t->video_streams[i]));
}
for(i = 0; i < t->num_audio_streams; i++)
{
if((t->audio_streams[i].com.action != STREAM_ACTION_FORGET) && !do_delete)
{
sprintf(tmp_string, "%" PRId64, t->audio_streams[i].samples_read);
bg_log(BG_LOG_INFO, LOG_DOMAIN,
"Audio stream %d: Transcoded %s samples", i+1,
tmp_string);
}
cleanup_audio_stream(&(t->audio_streams[i]));
}
for(i = 0; i < t->num_subtitle_text_streams; i++)
{
cleanup_subtitle_stream((subtitle_stream_t*)(&(t->subtitle_text_streams[i])));
}
for(i = 0; i < t->num_subtitle_overlay_streams; i++)
{
cleanup_subtitle_stream(&(t->subtitle_overlay_streams[i]));
}
if(t->audio_streams)
free(t->audio_streams);
if(t->video_streams)
free(t->video_streams);
if(t->subtitle_text_streams)
free(t->subtitle_text_streams);
if(t->subtitle_overlay_streams)
free(t->subtitle_overlay_streams);
/* Free metadata */
bg_metadata_free(&(t->metadata));
/* Close and destroy the input plugin */
close_input(t);
/* Free rest */
FREE_STR(t->name);
FREE_STR(t->location);
FREE_STR(t->plugin);
FREE_STR(t->output_directory);
FREE_STR(t->output_path);
FREE_STR(t->subdir);
FREE_STR(t->output_filename);
gavl_timer_destroy(t->timer);
bg_msg_queue_list_destroy(t->message_queues);
pthread_mutex_destroy(&(t->stop_mutex));
free(t);
}
static void * thread_func(void * data)
{
int do_stop;
bg_transcoder_t * t = (bg_transcoder_t*)data;
while(bg_transcoder_iteration(t))
{
pthread_mutex_lock(&t->stop_mutex);
do_stop = t->do_stop;
pthread_mutex_unlock(&t->stop_mutex);
if(do_stop)
break;
}
return NULL;
}
void bg_transcoder_run(bg_transcoder_t * t)
{
pthread_create(&(t->thread), (pthread_attr_t*)0, thread_func, t);
}
void bg_transcoder_stop(bg_transcoder_t * t)
{
pthread_mutex_lock(&t->stop_mutex);
t->do_stop = 1;
pthread_mutex_unlock(&t->stop_mutex);
}
void bg_transcoder_finish(bg_transcoder_t * t)
{
pthread_join(t->thread, NULL);
}
|