1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834
|
/*
* PsychSourceGL/Source/Common/Screen/PsychVideoCaptureSupportLibDC1394.c
*
* PLATFORMS:
*
* GNU/Linux, Apple MacOS/X for now.
*
* AUTHORS:
*
* Mario Kleiner mk mario.kleiner.de@gmail.com
*
* HISTORY:
*
* DESCRIPTION:
*
* This is the videocapture engine based on the free, open-source
* LibDC-1394 V2 library. It only supports video capture, no sound capture and
* no recording of video or sound.
*
* It only supports machine vision cameras that are connected via the IEEE-1394
* Firewire-bus and that conform to the IIDC-1.0 (and later) standard for firewire
* machine vision cameras. These cameras are handled via the (statically linked)
* libdc1394-2.0 in combination with (on Linux) libraw1394.
*
* The functions provide high performance streaming of uncompressed camera data over
* firewire and a lot of features (e.g., external sync. triggers) not useful for
* consumer cameras, but very useful for computer vision applications and things
* like eye-trackers, ...
*
* NOTES:
*
* TODO:
* - Implement Query/WaitTrigger support via new Basler smart feature api.
*/
#include "Screen.h"
#include <float.h>
#ifdef PTBVIDEOCAPTURE_LIBDC
#include <inttypes.h>
#if PSYCH_SYSTEM == PSYCH_LINUX
// On Linux we use the syslog facility for logging, and libraw1394 for low-level access
// to the firewire bus, libraw13943 at least on oldish Linux systems without juju-stack:
#include <libraw1394/raw1394.h>
#include <syslog.h>
#endif
// This is the master include file for libdc1394. It includes all other public header files:
#include <dc1394/dc1394.h>
// Basler specific extensions (Baser SFF Smart Feature Framework):
#include <dc1394/vendor/basler.h>
// Include for dynamic loading of external plugin:
#include <dlfcn.h>
// Sync modes and sync roles for multi-camera synchronization:
#define kPsychIsSyncMaster 1
#define kPsychIsSyncSlave 2
#define kPsychIsSoftSynced 4
#define kPsychIsBusSynced 8
#define kPsychIsHwSynced 16
#define kPsychNoLockStepSync 32
// Record which defines all state for a capture device:
typedef struct {
int valid; // Is this a valid device record? zero == Invalid.
int capturehandle; // Userspace visible capture handle.
psych_mutex mutex;
psych_condition condition;
psych_thread recorderThread; // Thread handle for background video recording thread.
dc1394camera_t *camera; // Ptr to a DC1394 camera object that holds the internal state for such cams.
dc1394video_frame_t *frame; // Ptr to a structure which contains the most recently captured/dequeued frame.
dc1394video_frame_t *convframe; // Ptr to a structuve which contains bayer- or YUV- converted frames.
uint64_t total_bytes; // Total bytes per frame in Format-7 mode, as queried from camera. Needed for Basler SFF support.
unsigned char* current_frame; // Ptr to target buffer for most recent frame if video recorder thread is active in low-latency mode.
unsigned char* pulled_frame; // Ptr to fetched frame from video recorder thread (if active). This is the "front buffer" equivalent of current_frame.
int syncmode; // 0 = free-running. 1 = sync-master, 2 = sync-slave, 4 = soft-sync, 8 = bus-sync, 16 = ttl-sync.
int syncmasterhandle; // -1 for "undefined" or the capturehandle of the sync master, if a camera is a sync slave.
int dropframes; // 1 == Always deliver most recent frame in FIFO, even if dropping of frames is neccessary.
dc1394video_mode_t dc_imageformat;// Encodes image size and pixelformat.
dc1394framerate_t dc_framerate; // Encodes framerate.
dc1394color_coding_t colormode; // Encodes color encoding of cameras data.
dc1394bayer_method_t debayer_method; // Type if debayering method to use.
dc1394color_filter_t color_filter_override; // Override Bayer pattern to use.
int dataconversionmode; // Shall raw sensor data be requested or preprocessed data? Postprocess raw data or not?
int reqlayers; // Requested number of layers (1=Luminance, 2 = LA, 3 = RGB, 4 = RGBA, 5 = YUV) in output texture.
int actuallayers; // Actual number of layers (1=Luminance, 2 = LA, 3 = RGB, 4 = RGBA, 5 = YUV) in output texture.
int bitdepth; // Requested or actual bpc - bits per color/luminance channel.
int num_dmabuffers; // Number of DMA ringbuffers to use in DMA capture.
int nrframes; // Total count of decompressed images.
unsigned int framecounter; // Total number of captured frames which have been already fetched from the DMA queue.
unsigned int pulled_framecounter; // Total number of captured frames which have been already fetched from the DMA queue. Need extra accounting for GStreamer feedback case.
unsigned int nrframes_pending; // Number of frames still pending in the DMA queue for fetching at last check.
unsigned int stopAtFramecount; // Maximum total number of frames to capture before capture stops - used by recorderThread as stop criterion.
unsigned int stopAtFramecountLatch; // Value to latch into stopAtFramecount at 'StartCapture', as set by 'SetVideoCaptureParameter'.
double fps; // Acquisition framerate of capture device.
int width; // Width x height of captured images.
int height;
double current_pts; // Capture timestamp of current frame fetched from engine.
double pulled_pts; // Capture timestamp of actually pulled frame.
int current_dropped; // Dropped count for this fetch cycle...
int pulled_dropped; // -""- by master thread.
int nr_droppedframes; // Counter for dropped frames.
int corrupt_count; // Counter for corrupt frames, as detected by libdc1394 or cameras SFF functions.
int frame_ready; // Signals availability of new frames for conversion into GL-Texture.
int grabber_active; // Grabber running?
PsychRectType roirect; // Region of interest rectangle - denotes subarea of full video capture area.
double avg_decompresstime; // Average time spent in decompressor.
double avg_gfxtime; // Average time spent in buffer --> OpenGL texture conversion and statistics.
int nrgfxframes; // Count of fetched textures.
psych_bool recording_active; // Is video recording active/requested on this camera?
char* targetmoviefilename; // Filename of a movie file to record.
char* codecSpec; // Codec specification string for video recording.
int moviehandle; // Handle of movie file to be written during video recording.
unsigned int recordingflags; // Flags used for recording and similar activities.
unsigned int specialFlags; // Additional flags set via 'SetCaptureParameter' functions.
char* processingString; // Optional GStreamer video processing string, NULL if undefined.
void* markerTrackerPlugin; // Opaque pointer to instance handle of a markerTrackerPlugin.
} PsychVidcapRecordType;
static PsychVidcapRecordType vidcapRecordBANK[PSYCH_MAX_CAPTUREDEVICES];
static int numCaptureRecords = 0;
static psych_bool firsttime = TRUE;
static dc1394_t *libdc = NULL; // Master handle to DC1394 library.
// Global Bayer filter settings for helper routine which is used for movie playback:
static dc1394bayer_method_t global_debayer_method = DC1394_BAYER_METHOD_NEAREST;
static dc1394color_filter_t global_color_filter = DC1394_COLOR_FILTER_MIN;
// Global state and functions related to special markerTrackerPlugin's:
// Global handle to shared library:
static void* markerTrackerPlugin_libraryhandle = NULL;
// Function prototypes of functions we want to use in the plugin library:
static void* (*TrackerPlugin_initialize)(void);
static bool (*TrackerPlugin_shutdown)(void* handle);
static bool (*TrackerPlugin_processFrame)(void* handle, unsigned long* source_ptr, int imgwidth, int imgheight, int xmin, int ymin, unsigned int timeidx, double capturetimestamp, unsigned int absolute_frameindex);
static bool (*TrackerPlugin_processPluginDataBuffer)(void* handle, unsigned long* buffer, int size);
// Forward declaration of internal helper function:
void PsychDCDeleteAllCaptureDevices(void);
void PsychDCLibInit(void);
#if PSYCH_SYSTEM == PSYCH_OSX
extern dc1394_t* dc1394_new(void) __attribute__((weak_import));
#endif
void PsychDCLibInit(void)
{
if (firsttime) {
// First time invocation:
// Check if linker was able to dynamically runtime-link
// the library on OSX, where we weak-link the library to
// allow operation of Screen() without need to have libdc1394
// installed, as long as user doesn't want to use it.
#if PSYCH_SYSTEM == PSYCH_OSX
if (NULL == dc1394_new) {
printf("\n\n");
printf("PTB-ERROR: Could not load and link libdc1394 firewire video capture library!\n");
printf("PTB-ERROR: Most likely because the library is not (properly) installed on this\n");
printf("PTB-ERROR: machine. Please read 'help VideoCaptureDC1394' for installation or\n");
printf("PTB-ERROR: troubleshooting instructions. Firewire capture support is disabled\n");
printf("PTB-ERROR: until you have resolved the problem.\n\n");
PsychErrorExitMsg(PsychError_user, "Failed to load and link libDC1394 V2 Firewire video capture library! Capture engine unavailable.");
}
#endif
// Initialize library:
libdc = dc1394_new();
if (libdc == NULL) PsychErrorExitMsg(PsychError_user, "Failed to initialize libDC1394 V2 Firewire video capture library! Capture engine unavailable.");
markerTrackerPlugin_libraryhandle = NULL;
firsttime = FALSE;
}
return;
}
/* PsychGetVidcapRecord() -- Given a handle, return ptr to video capture record.
* --> Internal helper function of PsychVideoCaptureSupport.
*/
PsychVidcapRecordType* PsychGetVidcapRecord(int deviceIndex)
{
// Sanity checks:
if (deviceIndex < 0) {
PsychErrorExitMsg(PsychError_user, "Invalid (negative) deviceIndex for video capture device passed!");
}
if (deviceIndex >= PSYCH_MAX_CAPTUREDEVICES) {
PsychErrorExitMsg(PsychError_user, "Invalid deviceIndex for video capture device passed. Index exceeds number of registered devices!");
}
if (!vidcapRecordBANK[deviceIndex].valid) {
PsychErrorExitMsg(PsychError_user, "Invalid deviceIndex for video capture device passed. No such device open!");
}
// Ok, we have a valid device record, return a ptr to it:
return(&vidcapRecordBANK[deviceIndex]);
}
/*
* PsychVideoCaptureInit() -- Initialize video capture subsystem.
* This routine is called by Screen's RegisterProject.c PsychModuleInit()
* routine at Screen load-time. It clears out the vidcapRecordBANK to
* bring the subsystem into a clean initial state.
*/
void PsychDCVideoCaptureInit(void)
{
// Initialize vidcapRecordBANK with NULL-entries:
int i;
for (i = 0; i < PSYCH_MAX_CAPTUREDEVICES; i++) {
vidcapRecordBANK[i].valid = 0;
}
numCaptureRecords = 0;
libdc = NULL;
return;
}
/*
* void PsychExitVideoCapture() - Shutdown handler.
*
* This routine is called by Screen('CloseAll') and on clear Screen time to
* do final cleanup. It deletes all capture objects
*
*/
void PsychDCExitVideoCapture(void)
{
// Release all capture devices
PsychDCDeleteAllCaptureDevices();
// Shutdown library:
if (libdc && !firsttime) dc1394_free(libdc);
libdc = NULL;
// Reset global library handle for markertracker plugin:
markerTrackerPlugin_libraryhandle = NULL;
// Reset firsttime flag to get a cold restart on next invocation of Screen:
firsttime = TRUE;
return;
}
/*
* PsychDeleteAllCaptureDevices() -- Delete all capture objects and release all associated ressources.
*/
void PsychDCDeleteAllCaptureDevices(void)
{
int i;
for (i=0; i<PSYCH_MAX_CAPTUREDEVICES; i++) {
if (vidcapRecordBANK[i].valid) PsychDCCloseVideoCaptureDevice(i);
}
return;
}
/*
* PsychCloseVideoCaptureDevice() -- Close a capture device and release all associated ressources.
*/
void PsychDCCloseVideoCaptureDevice(int capturehandle)
{
// Retrieve device record for handle:
PsychVidcapRecordType* capdev = PsychGetVidcapRecord(capturehandle);
// Stop capture immediately if it is still running:
PsychDCVideoCaptureRate(capturehandle, 0, 0, NULL);
// Initiate a power-down cycle to bring camera into standby mode:
if (dc1394_camera_set_power(capdev->camera, DC1394_OFF)!=DC1394_SUCCESS) {
printf("PTB-WARNING: Tried to power down camera %i, but powerdown-cycle failed for some reason!\n", capturehandle); fflush(NULL);
}
// Close & Shutdown camera, release ressources:
dc1394_camera_free(capdev->camera);
capdev->camera = NULL;
// Release video recording related data:
if (capdev->codecSpec) free(capdev->codecSpec);
capdev->codecSpec = NULL;
if (capdev->targetmoviefilename) free(capdev->targetmoviefilename);
capdev->targetmoviefilename = NULL;
if (capdev->processingString) free(capdev->processingString);
capdev->processingString = NULL;
// Shutdown and release an assigned markerTrackerPlugin:
if (capdev->markerTrackerPlugin) {
// Try to shutdown this instance of the plugin:
if (!(*TrackerPlugin_shutdown)(capdev->markerTrackerPlugin)) {
printf("PTB-WARNING: Failed to shutdown markertracker plugin for device %i.\n", capturehandle);
}
capdev->markerTrackerPlugin = NULL;
// Release one reference to this instance of the plugin:
// There may be more instances. If the last reference is released, the plugin
// will get truly unloaded by the dynamic linker and the markerTrackerPlugin_libraryhandle
// will become invalid / a stale pointer which we will clean up in PsychDCExitVideoCapture():
dlclose(markerTrackerPlugin_libraryhandle);
}
PsychDestroyMutex(&capdev->mutex);
PsychDestroyCondition(&capdev->condition);
// Invalidate device record to free up this slot in the array:
capdev->valid = 0;
// Decrease counter of open capture devices:
if (numCaptureRecords>0) numCaptureRecords--;
// Done.
return;
}
/*
* PsychOpenVideoCaptureDevice() -- Create a video capture object.
*
* This function tries to open and initialize a connection to a IEEE1394
* Firewire machine vision camera and return the associated captureHandle for it.
*
* slotid = Number of slot in vidcapRecordBANK[] array to use for this camera.
* win = Pointer to window record of associated onscreen window.
* deviceIndex = Index of the grabber device. (Currently ignored)
* capturehandle = handle to the new capture object.
* capturerectangle = If non-NULL a ptr to a PsychRectangle which contains the ROI for capture.
* reqdepth = Number of layers for captured output textures. (0=Don't care, 1=LUMINANCE8, 2=LUMINANCE8_ALPHA8, 3=RGB8, 4=RGBA8)
* num_dmabuffers = Number of buffers in the ringbuffer queue (e.g., DMA buffers) - This is OS specific. Zero = Don't care.
* allow_lowperf_fallback = If set to 1 then PTB can use a slower, low-performance fallback path to get nasty devices working.
* targetmoviefilename and recordingflags are currently ignored, they would refer to video harddics recording capabilities.
*
* bitdepth = Number of bits per color component / channel, aka bpc. Default is 8bpc, but some cams may be up to more.
*
*/
psych_bool PsychDCOpenVideoCaptureDevice(int slotid, PsychWindowRecordType *win, int deviceIndex, int* capturehandle, double* capturerectangle,
int reqdepth, int num_dmabuffers, int allow_lowperf_fallback, char* targetmoviefilename, unsigned int recordingflags, int bitdepth)
{
PsychVidcapRecordType *capdev = NULL;
dc1394camera_list_t *cameras=NULL;
unsigned int numCameras;
dc1394error_t err;
char msgerr[10000];
char* codecSpec = NULL;
uint32_t rc_dummy1;
uint64_t rc_dummy2;
(void) win, (void) allow_lowperf_fallback;
*capturehandle = -1;
// Perform first-time init, if needed:
PsychDCLibInit();
// Slot 'slotid' will contain the record for our new capture object:
// Initialize new record:
vidcapRecordBANK[slotid].valid = 1;
// Retrieve device record for slotid:
capdev = PsychGetVidcapRecord(slotid);
// Clear out the record to have a nice clean start:
memset(capdev, 0, sizeof(PsychVidcapRecordType));
// Need to set valid flag again after the memset():
capdev->valid = 1;
// Zero inits are not really needed, but anyhow...
capdev->camera = NULL;
capdev->grabber_active = 0;
capdev->convframe = NULL;
capdev->debayer_method = DC1394_BAYER_METHOD_NEAREST;
capdev->capturehandle = slotid;
capdev->moviehandle = -1;
PsychInitMutex(&capdev->mutex);
PsychInitCondition(&capdev->condition, NULL);
// Name of target movie file for video and audio recording specified?
if (targetmoviefilename) {
// Codec settings or type specified?
if ((codecSpec = strstr(targetmoviefilename, ":CodecSettings="))) {
// Replace ':' with a zero in targetmoviefilename, so it gets null-terminated
// and only points to the actual movie filename:
*codecSpec = 0;
// Move after null-terminator:
codecSpec++;
// Replace the ':CodecSettings=' with the special keyword 'DEFAULTenc', so
// so the default video codec is chosen, but the given settings override its
// default parameters.
strncpy(codecSpec, "DEFAULTenc ", strlen("DEFAULTenc "));
if (strlen(codecSpec) == 0) PsychErrorExitMsg(PsychError_user, "Invalid (empty) :CodecSettings= parameter specified. Aborted.");
} else if ((codecSpec = strstr(targetmoviefilename, ":CodecType="))) {
// Replace ':' with a zero in targetmoviefilename, so it gets null-terminated
// and only points to the actual movie filename:
*codecSpec = 0;
// Advance codecSpec to point to the actual codec spec string:
codecSpec+= 11;
if (strlen(codecSpec) == 0) PsychErrorExitMsg(PsychError_user, "Invalid (empty) :CodecType= parameter specified. Aborted.");
} else {
// No codec specified: Use our default encoder, the one that's been shown to
// produce good results:
codecSpec = strdup("DEFAULTenc");
}
// Copy codecSpec for later use in start of video capture and recording:
capdev->codecSpec = strdup(codecSpec);
if (strcmp(codecSpec, "DEFAULTenc") == 0) free(codecSpec);
codecSpec = NULL;
// Audio recording is so far unsupported, so clear the "with audio" flag:
recordingflags &= ~2;
if (PsychPrefStateGet_Verbosity() > 2) printf("PTB-INFO: Video%s recording into file [%s] enabled for camera with deviceIndex %i.\n",
((recordingflags & 2) ? " and audio" : ""), targetmoviefilename, deviceIndex);
capdev->targetmoviefilename = strdup(targetmoviefilename);
capdev->recording_active = TRUE;
} else {
capdev->recording_active = FALSE;
}
// Assign recordingflags:
capdev->recordingflags = recordingflags;
// Query a list of all available (connected) Firewire cameras:
err = dc1394_camera_enumerate(libdc, &cameras);
if (err != DC1394_SUCCESS) {
// Failed to detect any cameras: Invalidate our record.
capdev->valid = 0;
printf("Unable to enumerate firewire cameras: %s\n", dc1394_error_get_string(err));
PsychErrorExitMsg(PsychError_user, "Unable to detect Firewire cameras: Read 'help VideoCaptureDC1394' for troubleshooting tips.\n");
}
// Get number of detected cameras:
numCameras = cameras->num;
// Any cameras?
if (numCameras<1) {
// Failed to find a camera: Invalidate our record.
capdev->valid = 0;
PsychErrorExitMsg(PsychError_user, "Unable to find any Firewire camera: Please make sure that there are actually any connected.\n"
"Please note that this capture engine only supports IIDC compliant machine vision cameras, not standard consumer DV cameras!");
}
// Specific cam requested?
if (deviceIndex==-1) {
// Nope. We just use the first one.
capdev->camera = dc1394_camera_new_unit(libdc, cameras->ids[0].guid, cameras->ids[0].unit);
printf("PTB-INFO: Opening the first Firewire camera on the IEEE1394 bus.\n");
deviceIndex = 0;
}
else {
// Does a camera with requested index exist?
if (deviceIndex >= (int) numCameras) {
// No such cam.
capdev->valid = 0;
sprintf(msgerr, "You wanted me to open the %i th camera (deviceIndex: %i), but there are only %i cameras available!",
deviceIndex + 1, deviceIndex, numCameras);
PsychErrorExitMsg(PsychError_user, msgerr);
}
// Ok, valid device index: Open and assign cam:
capdev->camera = dc1394_camera_new_unit(libdc, cameras->ids[deviceIndex].guid, cameras->ids[deviceIndex].unit);
printf("PTB-INFO: Opening the %i. Firewire camera (deviceIndex=%i) out of %i cams on the IEEE1394 bus.\n",
deviceIndex + 1, deviceIndex, numCameras);
}
fflush(NULL);
// Prepare error message in case its needed below:
sprintf(msgerr, "PTB-ERROR: Opening the %i. Firewire camera (deviceIndex=%i) failed! Failed to initialize camera with GUID %"PRIx64"\n", deviceIndex + 1, deviceIndex, cameras->ids[deviceIndex].guid);
// Free the unused cameras:
dc1394_camera_free_list(cameras);
cameras=NULL;
// Error abort if camera init failed:
if(capdev->camera == NULL) {
// Error abort here:
capdev->valid = 0;
PsychErrorExitMsg(PsychError_user, msgerr);
}
// ROI rectangle specified?
if (capturerectangle) {
PsychCopyRect(capdev->roirect, capturerectangle);
}
else {
// Create empty pseudo-rect, which means "don't care":
PsychMakeRect(capdev->roirect, 0, 0, 1 , 1);
}
// Our camera should be ready: Assign final handle.
*capturehandle = slotid;
// Increase counter of open capture devices:
numCaptureRecords++;
// Set zero framerate:
capdev->fps = 0;
// Set image size:
capdev->width = 0;
capdev->height = 0;
// Requested output texture pixel depth in layers:
capdev->reqlayers = reqdepth;
// Assign requested bpc bitdepth. libDC1394 and IIDC standard support
// 8 bpc or - for some higher end cameras - 16 bpc, so these are the
// only useful values to request and we threshold accordingly. Note
// that while requesting 16 bpc from a camera will lead to transfer of
// 16 bpc values per pixel component, ie. 2 Bytes for each color channel,
// if the camera supports > 8 bpc at all, this doesn't mean that the
// 16 bpc "container" actually contains 16 bpc of net payload. Usually
// most cameras will only support some lower bpc payloads, packed into the
// 16 bpc value. Typical pro-class cameras may support 10 bpc or 12 bpc, ie.
// a 16 bpc value whose 6 or 4 least significant bits are all zero and therefore
// meaningless. Video -> Texture conversion code will take this into account,
// trying a bit of texture memory if possible.
capdev->bitdepth = (bitdepth <= 8) ? 8 : 16;
if ((capdev->bitdepth > 8) && (PsychPrefStateGet_Verbosity() > 2)) printf("PTB-INFO: Requesting %i bpc data from camera %i.\n", bitdepth, deviceIndex);
// Number of DMA ringbuffers to use in DMA capture mode: If no number provided (==0), set it to 8 buffers...
capdev->num_dmabuffers = (num_dmabuffers>0) ? num_dmabuffers : 8;
// Reset framecounter:
capdev->framecounter = 0;
capdev->nrframes = 0;
capdev->grabber_active = 0;
// Set initial target stop framecount to "infinity", ie., 2^32 frames. This is good enough
// for over 1.5 months of operation at 1000 fps capture rate:
capdev->stopAtFramecountLatch = 0xffffffff;
// Initiate a power-up cycle in case the camera is in standby mode:
if (dc1394_camera_set_power(capdev->camera, DC1394_ON)!=DC1394_SUCCESS) {
printf("PTB-WARNING: Tried to power up camera %i, but powerup-cycle failed for some reason!\n", deviceIndex); fflush(NULL);
}
// Initiate a reset-cycle on the camera to bring it into a clean state to start with:
if (dc1394_camera_reset(capdev->camera)!=DC1394_SUCCESS) {
printf("PTB-WARNING: Tried to reset camera %i, but reset cycle failed for some reason!\n", deviceIndex); fflush(NULL);
}
// Presence or absence of dc1394_read_cycle_timer() support is an indicator if we are running
// on top of USB backend or real firewire backend. Only firewire has support, and only firewire
// provides high precision frame timestamping (as of December 2014), so use this as check for
// USB vs. Firewire:
if (dc1394_read_cycle_timer(capdev->camera, &rc_dummy1, &rc_dummy2) != DC1394_FUNCTION_NOT_SUPPORTED) {
// This seems to be a IEEE-1394 Firewire backend which supports
// OS/driver level timestamping. Mark it as such for use in timestamping:
capdev->specialFlags |= 16;
if (PsychPrefStateGet_Verbosity() > 2) printf("PTB-INFO: Camera %i seems to be a Firewire camera. Capture timestamps should be precise.\n", deviceIndex);
}
else if (PsychPrefStateGet_Verbosity() > 2) printf("PTB-INFO: Camera %i seems to be a USB camera. Capture timestamps will be only approximations.\n", deviceIndex);
printf("PTB-INFO: Camera successfully opened...\n"); fflush(NULL);
return(TRUE);
}
/* Internal function: Find best matching non-Format 7 mode:
*/
int PsychVideoFindNonFormat7Mode(PsychVidcapRecordType* capdev, double capturerate)
{
int maximgarea = 0;
dc1394video_mode_t maximgmode, mode;
int i, j, w, h;
unsigned int mw, mh;
float framerate;
dc1394framerate_t dc1394_framerate = DC1394_FRAMERATE_15; // Suppress compiler warning.
dc1394framerates_t supported_framerates;
dc1394video_modes_t video_modes;
dc1394color_coding_t color_code;
dc1394bool_t iscolor;
uint32_t bpc;
int nonyuvbonus;
int mode_found = false;
// Query supported video modes for this camera:
dc1394_video_get_supported_modes(capdev->camera, &video_modes);
w = (int) PsychGetWidthFromRect(capdev->roirect);
h = (int) PsychGetHeightFromRect(capdev->roirect);
maximgmode = DC1394_VIDEO_MODE_MIN;
for (i = 0; i < (int) video_modes.num; i++) {
// Query properties of this mode and match them against our requirements:
mode = video_modes.modes[i];
// We first check non-format 7 types: Skip format-7 types...
if (mode >= DC1394_VIDEO_MODE_FORMAT7_MIN && mode <= DC1394_VIDEO_MODE_FORMAT7_MAX) continue;
if(PsychPrefStateGet_Verbosity() > 4){
printf("PTB-Info: Probing non Format-7 mode %i ...\n", mode);
}
// Pixeldepth supported?
dc1394_get_color_coding_from_video_mode(capdev->camera, mode, &color_code);
if (capdev->reqlayers > 0) {
// Specific pixelsize requested:
// Luminance only format?
if (capdev->reqlayers < 3) {
// mode 1: Only accept raw data, which we will pass on later unprocessed:
if (capdev->dataconversionmode == 1 && (color_code != ((capdev->bitdepth <= 8) ? DC1394_COLOR_CODING_RAW8 : DC1394_COLOR_CODING_RAW16))) continue;
// mode 2: Only accept raw data, which we will post-process later on:
if (capdev->dataconversionmode == 2 && (color_code != ((capdev->bitdepth <= 8) ? DC1394_COLOR_CODING_RAW8 : DC1394_COLOR_CODING_RAW16))) continue;
// mode 3: Only accept filtered post-processed data:
if (capdev->dataconversionmode == 3 && (color_code != ((capdev->bitdepth <= 8) ? DC1394_COLOR_CODING_MONO8 : DC1394_COLOR_CODING_MONO16))) continue;
// mode 4: Only accept MONO data, but treat it as if it were RAW data and post-process it accordingly.
// This is a workaround for broken cams which deliver sensor raw data as MONO instead of RAW, e.g.,
// apparently some Bayer cams:
if (capdev->dataconversionmode == 4 && (color_code != ((capdev->bitdepth <= 8) ? DC1394_COLOR_CODING_MONO8 : DC1394_COLOR_CODING_MONO16))) continue;
// If we end here, then mode is 0 aka don't care. We take raw or luminance data:
if ((capdev->bitdepth <= 8 && color_code!=DC1394_COLOR_CODING_RAW8 && color_code!=DC1394_COLOR_CODING_MONO8) ||
(capdev->bitdepth > 8 && color_code!=DC1394_COLOR_CODING_RAW16 && color_code!=DC1394_COLOR_CODING_MONO16)) continue;
}
// RGB true color format?
if (capdev->reqlayers > 2) {
// mode 1: Is not handled for 3 or 4 layer formats, because 3 or 4 layer formats always
// require some post-processing of raw data, otherwise it would end up as 1 layer raw!
// mode 2: Only accept raw data, which we will post-process later on:
if (capdev->dataconversionmode == 2 && (color_code != ((capdev->bitdepth <= 8) ? DC1394_COLOR_CODING_RAW8 : DC1394_COLOR_CODING_RAW16))) continue;
// mode 4: Only accept MONO data, but treat it as if it were RAW data and post-process it accordingly.
// This is a workaround for broken cams which deliver sensor raw data as MONO instead of RAW, e.g.,
// apparently some Bayer cams:
if (capdev->dataconversionmode == 4 && (color_code != ((capdev->bitdepth <= 8) ? DC1394_COLOR_CODING_MONO8 : DC1394_COLOR_CODING_MONO16))) continue;
// If we end here, then mode is 0 aka don't care or 3 aka only accept color data. We take any color data of 8/16 bpc depth:
if ((dc1394_is_color(color_code, &iscolor) != DC1394_SUCCESS) || (!iscolor && (capdev->dataconversionmode != 2) && (capdev->dataconversionmode != 4)) ||
(dc1394_get_color_coding_data_depth(color_code, &bpc) != DC1394_SUCCESS) || (bpc != ((capdev->bitdepth <= 8) ? 8 : 16))) continue;
}
if (capdev->reqlayers == 5 && color_code!=DC1394_COLOR_CODING_YUV422 && color_code!=DC1394_COLOR_CODING_YUV411) continue;
}
else {
// No specific pixelsize req. check our minimum requirements - Anything of 8/16 bpc depths:
bpc = -1;
if ((dc1394_get_color_coding_data_depth(color_code, &bpc) != DC1394_SUCCESS) || (bpc != ((capdev->bitdepth <= 8) ? 8 : 16))) {
if (PsychPrefStateGet_Verbosity() > 4) printf("PTB-Info: Rejected due to wrong bpc %i\n", bpc);
continue;
}
}
if (PsychPrefStateGet_Verbosity() > 4) printf("PTB-Info: Passes color coding/depth check...\n");
// ROI specified?
dc1394_get_image_size_from_video_mode(capdev->camera, mode, &mw, &mh);
if (capdev->roirect[kPsychLeft]==0 && capdev->roirect[kPsychTop]==0 && w==1 && h==1) {
// No. Just find biggest one:
if (mw*mh < (unsigned int) maximgarea) continue;
maximgarea = mw * mh;
maximgmode = mode;
mode_found = true;
if (PsychPrefStateGet_Verbosity() > 4) printf("PTB-Info: Passes max image size check mw %i x mh %i --> New favorite.\n", mw, mh);
}
else {
// Yes. Check for exact match, reject everything else:
if (capdev->roirect[kPsychLeft]!=0 || capdev->roirect[kPsychTop]!=0 || w != (int) mw || h != (int) mh) continue;
if (PsychPrefStateGet_Verbosity() > 4) printf("PTB-Info: Passes exact ROI size match mw %i x mh %i\n", mw, mh);
// Ok, this is a valid mode wrt. reqlayers and exact image size. Check for matching framerate:
dc1394_video_get_supported_framerates(capdev->camera, mode, &supported_framerates);
for (j = 0; j < (int) supported_framerates.num; j++) {
dc1394_framerate = supported_framerates.framerates[j];
dc1394_framerate_as_float(dc1394_framerate, &framerate);
if (PsychPrefStateGet_Verbosity() > 4) printf("PTB-Info: Candidate fps %f vs max fps %f.\n", framerate, capturerate);
if (framerate >= capturerate) break;
}
dc1394_framerate_as_float(dc1394_framerate, &framerate);
// nonyuvbonus is true, if a color capture mode is requested and the given mode
// allows for RGB8 transfer instead of a YUV format. We try to prefer non-YUV
// modes in selection of final mode, because YUV modes need a computationally
// expensive conversion YUVxxx --> RGB8, whereas RGB8 doesn't need that.
nonyuvbonus = ((capdev->reqlayers == 0) || (capdev->reqlayers > 2)) && (capdev->dataconversionmode == 0) && (color_code == DC1394_COLOR_CODING_RGB8);
// Compare whatever framerate we've got as closest match against current fastest one:
if ((framerate > maximgarea) ||
(framerate == capturerate && nonyuvbonus) ||
(framerate == maximgarea && nonyuvbonus)) {
maximgarea = (int) framerate;
maximgmode = mode;
mode_found = true;
if (PsychPrefStateGet_Verbosity() > 4) printf("PTB-Info: New max fps %f--> New favorite.\n", framerate);
}
}
}
// Sanity check: Any valid mode found?
if (!mode_found) {
// None found!
PsychErrorExitMsg(PsychError_user, "Couldn't find any capture mode settings for your camera which satisfy your minimum requirements! Aborted.");
}
// maximgmode contains the best matching non-format-7 mode for our specs:
mode = maximgmode;
capdev->dc_imageformat = mode;
// Query final color format and therefore pixel-depth:
dc1394_get_color_coding_from_video_mode(capdev->camera, mode, &color_code);
// This is the actuallayers delivered by the capture engine:
capdev->actuallayers = (color_code == DC1394_COLOR_CODING_MONO8 || color_code == DC1394_COLOR_CODING_RAW8 || color_code == DC1394_COLOR_CODING_MONO16 || color_code == DC1394_COLOR_CODING_RAW16) ? 1 : 3;
// Special case: conversion mode 2 or 4 for rgb layers, aka bayer-filter raw data as provided in raw or mono container into rgb data:
if ((capdev->actuallayers == 1) && (capdev->reqlayers >= 3) && ((capdev->dataconversionmode == 2) || (capdev->dataconversionmode == 4))) capdev->actuallayers = 3;
// Match this against requested actuallayers:
if (capdev->reqlayers == 0) {
// No specific depth requested: Just use native depth of captured image:
capdev->reqlayers = capdev->actuallayers;
}
else {
// Specific depth requested: Match it against native format:
switch (capdev->reqlayers) {
case 1:
// Pure LUMINANCE8 requested:
case 2:
// LUMINANCE+ALPHA requested: This is not yet supported.
if (capdev->actuallayers != capdev->reqlayers && PsychPrefStateGet_Verbosity()>1) {
printf("PTB-WARNING: Wanted a depth of %i layers (%s) for captured images, but capture device delivers\n"
"PTB-WARNING: %i layers! Adapted to capture device native format for performance reasons.\n",
capdev->reqlayers, (capdev->reqlayers==1) ? "LUMINANCE":"LUMINANCE+ALPHA", capdev->actuallayers);
}
capdev->reqlayers = capdev->actuallayers;
break;
case 3:
// RGB requested:
case 4:
// RGBA requested: This is not yet supported.
if (capdev->actuallayers != capdev->reqlayers && PsychPrefStateGet_Verbosity()>1) {
printf("PTB-WARNING: Wanted a depth of %i layers (%s) for captured images, but capture device delivers\n"
"PTB-WARNING: %i layers! Adapted to capture device native format for performance reasons.\n",
capdev->reqlayers, (capdev->reqlayers==3) ? "RGB":"RGB+ALPHA", capdev->actuallayers);
}
capdev->reqlayers = capdev->actuallayers;
break;
case 5:
// YUV format requested: This is sort-of a RGB equivalent for us, for now:
capdev->reqlayers = 3;
break;
default:
capdev->reqlayers = 0;
PsychErrorExitMsg(PsychError_user, "You requested a invalid capture image format (more than 4 layers). Aborted.");
}
}
if (capdev->reqlayers > 1 && color_code != DC1394_COLOR_CODING_RGB8 && color_code != DC1394_COLOR_CODING_RGB16 && PsychPrefStateGet_Verbosity() > 2) {
// Color capture with a non RGB mode aka a YUV or RAW/RAW encapsulated in MONO mode -- expensive.
printf("PTB-INFO: Using a %s input color format instead of a RGB color format. This requires expensive color conversion and\n",
(color_code == DC1394_COLOR_CODING_RAW8 || color_code == DC1394_COLOR_CODING_MONO8 || color_code == DC1394_COLOR_CODING_RAW16 || color_code == DC1394_COLOR_CODING_MONO16) ? "RAW" : "YUV");
printf("PTB-INFO: can lead to higher cpu load and longer latencies. You may be able to avoid this with different settings\n");
printf("PTB-INFO: for ROI, color depth and framerate...\n"); fflush(NULL);
}
// Query final image size and therefore ROI:
dc1394_get_image_size_from_video_mode(capdev->camera, mode, &mw, &mh);
capdev->roirect[kPsychLeft] = 0;
capdev->roirect[kPsychTop] = 0;
capdev->roirect[kPsychRight] = mw;
capdev->roirect[kPsychBottom] = mh;
// Recheck capture framerate:
// We probe all available non mode-7 framerates of camera for the best match, aka
// the slowest framerate equal or faster to the requested framerate:
dc1394_video_get_supported_framerates(capdev->camera, mode, &supported_framerates);
for (i = 0; i < (int) supported_framerates.num; i++) {
dc1394_framerate = supported_framerates.framerates[i];
dc1394_framerate_as_float(dc1394_framerate, &framerate);
if (PsychPrefStateGet_Verbosity() > 4) printf("PTB-Info: Checking for optimal framerate: [%i of %i] fps %f vs. requested %f.\n", i, (int) supported_framerates.num - 1, framerate, capturerate);
if (framerate >= capturerate) break;
}
dc1394_framerate_as_float(dc1394_framerate, &framerate);
// Ok, we've got the closest match we could get. Good enough?
if ((fabs(framerate - capturerate) < 0.5) || (capturerate == DBL_MAX)) {
// Perfect match of delivered and requested framerate. Nothing to do so far...
}
else {
// No perfect match :(.
if(framerate < capturerate) {
printf("PTB-WARNING: Camera does not support requested capture framerate of %f fps. Using maximum of %f fps instead.\n",
(float) capturerate, framerate);
fflush(NULL);
}
}
// Return framerate:
capdev->dc_framerate = dc1394_framerate;
if(PsychPrefStateGet_Verbosity() > 4){
printf("PTB-INFO: Will use non-Format7 mode %i: Width x Height = %i x %i, fps=%f, colormode=%i ...\n",
(int) mode, mw, mh, framerate, (int) color_code); fflush(NULL);
}
// Success!
return(true);
}
/* Internal function: Find best matching Format 7 mode:
* Returns calculated optimal iso-packet size.
*/
int PsychVideoFindFormat7Mode(PsychVidcapRecordType* capdev, double capturerate)
{
float mindiff = FLT_MAX;
float mindifframerate = 0;
int minpacket_size = 0;
dc1394video_mode_t minimgmode, mode;
int i, w, h, numF7Available=0;
dc1394speed_t speed;
unsigned int mw, mh, pbmin, pbmax, depth;
int num_packets, packet_size;
float framerate;
dc1394video_modes_t video_modes;
dc1394color_coding_t color_code;
dc1394bool_t iscolor;
uint32_t bpc;
float bus_period;
// Query IEEE1394 bus speed code and map it to bus_period:
if (dc1394_video_get_iso_speed(capdev->camera, &speed)!=DC1394_SUCCESS) {
PsychErrorExitMsg(PsychError_user, "Unable to query bus-speed - Start of video capture failed!");
}
// Special hack for Unibrain Fire-i: This camera can do 400 Megabit/second, but reports
// a speed of 100 MBit after a cold-start! We enforce a 400 Megabit speed if this is a
// Unibrain Fire-i:
if (strstr(capdev->camera->vendor, "Unibrain") && strstr(capdev->camera->model, "Fire-i")) {
// Unibrain Fire-i: Enforce correct speed:
speed = DC1394_ISO_SPEED_400;
}
switch(speed) {
case DC1394_ISO_SPEED_100:
bus_period = 0.000500f;
break;
case DC1394_ISO_SPEED_200:
bus_period = 0.000250f;
break;
case DC1394_ISO_SPEED_400:
bus_period = 0.000125f;
break;
case DC1394_ISO_SPEED_800:
bus_period = 0.0000625f;
break;
case DC1394_ISO_SPEED_1600:
bus_period = 0.00003125f;
break;
case DC1394_ISO_SPEED_3200:
bus_period = 0.000015625f;
break;
default:
bus_period = 0; // To suppress compiler warnings.
PsychErrorExitMsg(PsychError_user, "Unknown bus speed specification! Start of video capture failed!");
}
if(PsychPrefStateGet_Verbosity() > 4){
printf("PTB-INFO: IEEE-1394 Firewire bus speed is %i Megabit/second --> Bus period is %f usecs.\n",
(int) (100 << speed), bus_period * 1000000.0f);
}
// Query supported video modes for this camera:
dc1394_video_get_supported_modes(capdev->camera, &video_modes);
minimgmode = DC1394_VIDEO_MODE_MIN;
for (i = 0; i < (int) video_modes.num; i++) {
// Query properties of this mode and match them against our requirements:
mode = video_modes.modes[i];
// Skip non-format-7 types...
if (mode < DC1394_VIDEO_MODE_FORMAT7_MIN || mode > DC1394_VIDEO_MODE_FORMAT7_MAX) continue;
// Increment count of available Format-7 modes:
numF7Available++;
if (PsychPrefStateGet_Verbosity() > 4) {
printf("PTB-Info: Probing Format-7 mode %i ...\n", mode);
}
// Pixeldepth supported? We reject anything except RAW8 or MONO8 for luminance formats
// and RGB8 for color formats.
dc1394_format7_get_color_coding(capdev->camera, mode, &color_code);
if (capdev->reqlayers > 0) {
// Specific pixelsize requested:
// Luminance only format?
if (capdev->reqlayers < 3) {
// mode 1: Only accept raw data, which we will pass on later unprocessed:
if (capdev->dataconversionmode == 1 && (color_code != ((capdev->bitdepth <= 8) ? DC1394_COLOR_CODING_RAW8 : DC1394_COLOR_CODING_RAW16))) continue;
// mode 2: Only accept raw data, which we will post-process later on:
if (capdev->dataconversionmode == 2 && (color_code != ((capdev->bitdepth <= 8) ? DC1394_COLOR_CODING_RAW8 : DC1394_COLOR_CODING_RAW16))) continue;
// mode 3: Only accept filtered post-processed data:
if (capdev->dataconversionmode == 3 && (color_code != ((capdev->bitdepth <= 8) ? DC1394_COLOR_CODING_MONO8 : DC1394_COLOR_CODING_MONO16))) continue;
// mode 4: Only accept MONO data, but treat it as if it were RAW data and post-process it accordingly.
// This is a workaround for broken cams which deliver sensor raw data as MONO instead of RAW, e.g.,
// apparently some Bayer cams:
if (capdev->dataconversionmode == 4 && (color_code != ((capdev->bitdepth <= 8) ? DC1394_COLOR_CODING_MONO8 : DC1394_COLOR_CODING_MONO16))) continue;
// If we end here, then mode is 0 aka don't care. We take raw or luminance data:
if ((capdev->bitdepth <= 8 && color_code!=DC1394_COLOR_CODING_RAW8 && color_code!=DC1394_COLOR_CODING_MONO8) ||
(capdev->bitdepth > 8 && color_code!=DC1394_COLOR_CODING_RAW16 && color_code!=DC1394_COLOR_CODING_MONO16)) continue;
}
// RGB true color format?
if (capdev->reqlayers > 2) {
// mode 1: Is not handled for 3 or 4 layer formats, because 3 or 4 layer formats always
// require some post-processing of raw data, otherwise it would end up as 1 layer raw!
// mode 2: Only accept raw data, which we will post-process later on:
if (capdev->dataconversionmode == 2 && (color_code != ((capdev->bitdepth <= 8) ? DC1394_COLOR_CODING_RAW8 : DC1394_COLOR_CODING_RAW16))) continue;
// mode 4: Only accept MONO data, but treat it as if it were RAW data and post-process it accordingly.
// This is a workaround for broken cams which deliver sensor raw data as MONO instead of RAW, e.g.,
// apparently some Bayer cams:
if (capdev->dataconversionmode == 4 && (color_code != ((capdev->bitdepth <= 8) ? DC1394_COLOR_CODING_MONO8 : DC1394_COLOR_CODING_MONO16))) continue;
// If we end here, then mode is 0 aka don't care or 3 aka only accept color data. We take any color data of 8/16 bpc depth:
if ((dc1394_is_color(color_code, &iscolor) != DC1394_SUCCESS) || (!iscolor && (capdev->dataconversionmode != 2) && (capdev->dataconversionmode != 4)) ||
(dc1394_get_color_coding_data_depth(color_code, &bpc) != DC1394_SUCCESS) || (bpc != ((capdev->bitdepth <= 8) ? 8 : 16))) continue;
}
if (capdev->reqlayers == 5 && color_code!=DC1394_COLOR_CODING_YUV422 && color_code!=DC1394_COLOR_CODING_YUV411) continue;
}
else {
// No specific pixelsize req. check our minimum requirements - Anything of 8/16 bpc depths:
if ((dc1394_get_color_coding_data_depth(color_code, &bpc) != DC1394_SUCCESS) || (bpc != ((capdev->bitdepth <= 8) ? 8 : 16))) continue;
}
if (PsychPrefStateGet_Verbosity() > 4) printf("PTB-Info: Passes color validation...\n");
// ROI specified?
w = (int) PsychGetWidthFromRect(capdev->roirect);
h = (int) PsychGetHeightFromRect(capdev->roirect);
if (capdev->roirect[kPsychLeft]==0 && capdev->roirect[kPsychTop]==0 && w==1 && h==1) {
// No. Just set biggest one for this mode:
// Query maximum size for mode:
if(dc1394_format7_get_max_image_size(capdev->camera, mode, &mw, &mh)!=DC1394_SUCCESS) continue;
// Set zero position offset:
if (dc1394_format7_set_image_position(capdev->camera, mode, 0, 0)!=DC1394_SUCCESS) continue;
// Set maximum size:
if (dc1394_format7_set_image_size(capdev->camera, mode, mw, mh)!=DC1394_SUCCESS) continue;
w=mw; h=mh;
}
else {
// Yes. Check for exact match, reject everything else:
if(dc1394_format7_get_max_image_size(capdev->camera, mode, &mw, &mh)!=DC1394_SUCCESS) continue;
if (w > (int) mw || h > (int) mh) continue;
// This mode allows for a ROI as big as the one we request. Try to set it up:
// First set zero position offset:
if (dc1394_format7_set_image_position(capdev->camera, mode, 0, 0)!=DC1394_SUCCESS) continue;
// Reject mode if size isn't supported:
if (dc1394_format7_set_image_size(capdev->camera, mode, (unsigned int) w, (unsigned int) h)!=DC1394_SUCCESS) continue;
// Now set real position:
if (dc1394_format7_set_image_position(capdev->camera, mode, (unsigned int) capdev->roirect[kPsychLeft], (unsigned int) capdev->roirect[kPsychTop])!=DC1394_SUCCESS) continue;
// If we reach this point, then we should have exactly the ROI we wanted.
}
if (PsychPrefStateGet_Verbosity() > 4) printf("PTB-Info: Passes size / ROI validation for mw x mh = %i x %i...\n", mw, mh);
// Try to set the requested framerate as well:
// We need to calculate the ISO packet size depending on wanted framerate, Firewire bus speed,
// image size and image depth + some IIDC spec. restrictions...
// First we query the range of available packet sizes:
if (dc1394_format7_get_packet_parameters(capdev->camera, mode, &pbmin, &pbmax)!=DC1394_SUCCESS) continue;
// Special case handling:
if (pbmin==0) pbmin = pbmax;
if (PsychPrefStateGet_Verbosity() > 4) printf("PTB-Info: Packet pbmin = %i, pbmax = %i\n", pbmin, pbmax);
// Compute number of ISO-Packets, assuming a 400 MBit bus (125 usec cycle time):
num_packets = (int) (1.0/(bus_period * capturerate) + 0.5);
if (num_packets < 1 || num_packets > 4095) {
// Invalid num_packets. Adapt it to fit IIDC constraints:
if (num_packets < 1) {
num_packets = 1;
}
else {
num_packets = 4095;
}
}
if (PsychPrefStateGet_Verbosity() > 4) printf("PTB-Info: num_packets = %i, effective %i.\n", num_packets, num_packets * 8);
num_packets*=8;
if (dc1394_format7_get_data_depth(capdev->camera, mode, &depth)!=DC1394_SUCCESS) continue;
packet_size = (int)((w * h * depth + num_packets - 1) / num_packets);
if (PsychPrefStateGet_Verbosity() > 4) printf("PTB-Info: packet_size preval = %i\n", packet_size);
// Make sure that packet_size is an integral multiple of pbmin (IIDC constraint):
if (packet_size < (int) pbmin) packet_size = pbmin;
if (packet_size % pbmin != 0) {
packet_size = packet_size - (packet_size % pbmin);
}
// Make sure that packet size is smaller than pbmax:
while (packet_size > (int) pbmax) packet_size=packet_size - pbmin;
if (PsychPrefStateGet_Verbosity() > 4) printf("PTB-Info: packet_size postval = %i\n", packet_size);
// Ok, we should now have the closest valid packet size for the given ROI and framerate:
// Inverse compute framerate for this packetsize:
num_packets = (int) ((w * h * depth + (packet_size*8) - 1)/(packet_size*8));
framerate = 1.0/(bus_period * (float) num_packets);
if (PsychPrefStateGet_Verbosity() > 4) printf("PTB-Info: Effective num_packets = %i, packet_size = %i, fps = %f\n", num_packets, packet_size, framerate);
// Compare whatever framerate we've got as closest match against current fastest one:
if ((fabs(capturerate - framerate) < mindiff) || (capturerate == DBL_MAX)) {
mindiff = fabs(capturerate - framerate);
mindifframerate = framerate;
minimgmode = mode;
minpacket_size = packet_size;
if (PsychPrefStateGet_Verbosity() > 4) printf("PTB-Info: New favorite settings for fps %f vs. target %f\n", framerate, capturerate);
}
if(PsychPrefStateGet_Verbosity() > 4){
if (capdev->roirect[kPsychLeft]!=0 || capdev->roirect[kPsychTop]!=0 || capdev->roirect[kPsychRight]!=1 || capdev->roirect[kPsychBottom]!=1) {
printf("PTB-INFO: Checking Format-7 mode %i: ROI = [l=%f t=%f r=%f b=%f] , FPS = %f\n", mode, (float) capdev->roirect[kPsychLeft], (float) capdev->roirect[kPsychTop],
(float) capdev->roirect[kPsychRight], (float) capdev->roirect[kPsychBottom], framerate);
}
else {
printf("PTB-INFO: Checking Format-7 mode %i: ROI = [l=0 t=0 r=%i b=%i] , FPS = %f\n", mode, w, h, framerate);
}
}
// Test next mode...
}
// Sanity check: Any valid mode found?
if (minimgmode == DC1394_VIDEO_MODE_MIN || numF7Available == 0) {
// None found!
if (numF7Available > 0) {
printf("PTB-INFO: Couldn't find any Format-7 capture mode settings for your camera which satisfy your minimum requirements!\n");
}
else {
printf("PTB-INFO: This camera does not support *any* Format-7 capture modes.\n");
}
printf("PTB-INFO: Will now try standard (non Format-7) capture modes for the best match and try to use that...\n");
return(0);
}
// Success (more or less...):
// minimgmode contains the best matching Format-7 mode for our specs:
mode = minimgmode;
capdev->dc_imageformat = mode;
capdev->dc_framerate = DC1394_FRAMERATE_MAX;
packet_size = minpacket_size;
framerate = mindifframerate;
// Query final color format and therefore pixel-depth:
dc1394_get_color_coding_from_video_mode(capdev->camera, mode, &color_code);
// This is the actuallayers delivered by the capture engine:
capdev->actuallayers = (color_code == DC1394_COLOR_CODING_MONO8 || color_code == DC1394_COLOR_CODING_RAW8 || color_code == DC1394_COLOR_CODING_MONO16 || color_code == DC1394_COLOR_CODING_RAW16) ? 1 : 3;
// Special case: conversion mode 2 or 4 for rgb layers, aka bayer-filter raw data as provided in raw or mono container into rgb data:
if ((capdev->actuallayers == 1) && (capdev->reqlayers >= 3) && ((capdev->dataconversionmode == 2) || (capdev->dataconversionmode == 4))) capdev->actuallayers = 3;
// Match this against requested actuallayers:
if (capdev->reqlayers == 0) {
// No specific depth requested: Just use native depth of captured image:
capdev->reqlayers = capdev->actuallayers;
}
else {
// Specific depth requested: Match it against native format:
switch (capdev->reqlayers) {
case 1:
// Pure LUMINANCE requested:
case 2:
// LUMINANCE+ALPHA requested: This is not yet supported.
if (capdev->actuallayers != capdev->reqlayers && PsychPrefStateGet_Verbosity()>1) {
printf("PTB-WARNING: Wanted a depth of %i layers (%s) for captured images, but capture device delivers\n"
"PTB-WARNING: %i layers! Adapted to capture device native format for performance reasons.\n",
capdev->reqlayers, (capdev->reqlayers==1) ? "LUMINANCE":"LUMINANCE+ALPHA", capdev->actuallayers);
}
capdev->reqlayers = capdev->actuallayers;
break;
case 3:
// RGB requested:
case 4:
// RGBA requested: This is not yet supported.
if (capdev->actuallayers != capdev->reqlayers && PsychPrefStateGet_Verbosity()>1) {
printf("PTB-WARNING: Wanted a depth of %i layers (%s) for captured images, but capture device delivers\n"
"PTB-WARNING: %i layers! Adapted to capture device native format for performance reasons.\n",
capdev->reqlayers, (capdev->reqlayers==3) ? "RGB":"RGB+ALPHA", capdev->actuallayers);
}
capdev->reqlayers = capdev->actuallayers;
break;
case 5:
// YUV format requested: This is sort-of a RGB equivalent for us, for now:
capdev->reqlayers = 3;
break;
default:
capdev->reqlayers = 0;
PsychErrorExitMsg(PsychError_user, "You requested a invalid capture image format (more than 4 layers). Aborted.");
}
}
// Query final image size and therefore ROI:
dc1394_get_image_size_from_video_mode(capdev->camera, mode, &mw, &mh);
capdev->roirect[kPsychRight] = capdev->roirect[kPsychLeft] + mw;
capdev->roirect[kPsychBottom] = capdev->roirect[kPsychTop] + mh;
// Ok, we've got the closest match we could get. Good enough?
if ((mindiff < 0.5) || (capturerate == DBL_MAX)) {
// Perfect match of delivered and requested framerate. Nothing to do so far...
}
else {
// No perfect match :(.
if(framerate < capturerate) {
printf("PTB-WARNING: Camera does not support requested capture framerate of %f fps at given ROI setting. Using %f fps instead.\n",
(float) capturerate, framerate);
fflush(NULL);
}
}
// Assign computed framerate as best guess for real framerate, in case frame-interval query fails...
capdev->fps = framerate;
// Return packet_size:
return(packet_size);
}
// Helper function: Push captured video frame data buffer into GStreamer video encoding pipeline:
static psych_bool PsychDCPushFrameToMovie(PsychVidcapRecordType* capdev, psych_uint16* input_image, double frameTimestamp, psych_bool onMasterthread)
{
// Yes. Need to add this video frame to the encoding pipeline.
unsigned int twidth, theight, numChannels, bitdepth, i;
unsigned int count;
unsigned char* framepixels;
// Get memory pointer to target memory buffer:
framepixels = PsychGetVideoFrameForMoviePtr(capdev->moviehandle, &twidth, &theight, &numChannels, &bitdepth);
// Validate number of color channels and bits per channel values for a match:
if (numChannels != (unsigned int) capdev->actuallayers || bitdepth != ((capdev->bitdepth > 8) ? 16 : 8)) {
printf("PTB-ERROR: Mismatch between number of color channels %i or bpc %i of captured video frame and number of channels %i or bpc %i of video pipeline target buffer!\n",
capdev->actuallayers, ((capdev->bitdepth > 8) ? 16 : 8), numChannels, bitdepth);
if (onMasterthread) {
PsychErrorExitMsg(PsychError_system, "Encoding current captured video frame failed. Video format mismatch.");
}
else {
printf("PTB-ERROR: Encoding current captured video frame on video recorder thread failed. Video format mismatch!\n");
}
return(FALSE);
}
// Dimensions match?
if (twidth != (unsigned int) capdev->width || theight > (unsigned int) capdev->height) {
printf("PTB-ERROR: Mismatch between size of captured video frame %i x %i and size of video pipeline target buffer %i x %i !\n", capdev->width, capdev->height, twidth, theight);
if (onMasterthread) {
PsychErrorExitMsg(PsychError_system, "Encoding current captured video frame failed. Video frame size mismatch.");
}
else {
printf("PTB-ERROR: Encoding current captured video frame failed. Video frame size mismatch!\n");
}
return(FALSE);
}
// Target buffer available?
if (framepixels) {
// Copy the pixels:
count = (twidth * theight * ((capdev->actuallayers == 3) ? 3 : 1) * ((capdev->bitdepth > 8) ? 2 : 1));
// True bitdepth in the 9 to 15 bpc range?
if (capdev->bitdepth > 8 && capdev->bitdepth < 16) {
// Yes. Need to bit-shift, so the most significant bit of the video data,
// gets placed in the 16th bit of the 16 bit word. This to make sure the
// "dead bits" for bpc < 16 are the least significant bits and they are
// all zeros. This makes sure that black is always all-zero and white is
// always 0xffff - all ones:
psych_uint16 *frameinwords = (psych_uint16*) input_image;
psych_uint16 *frameoutwords = (psych_uint16*) framepixels;
count /= 2; // Half as many words as bytes.
for (i = 0; i < count; i++) *(frameoutwords++) = *(frameinwords++) << (16 - capdev->bitdepth);
}
else {
// No, either 8 bpc or 16 bpc - A simple memcpy does the job efficiently:
memcpy(framepixels, (const void*) input_image, count);
}
// Add to GStreamer encoding pipeline: Format is upright, and 1 video frame duration per frame and a
// capture timestamp of frameTimestamp:
if (PsychAddVideoFrameToMovie(capdev->moviehandle, 1, FALSE, frameTimestamp) != 0) {
if (onMasterthread) {
PsychErrorExitMsg(PsychError_system, "Encoding current captured video frame failed. Failed to add frame to pipeline.");
}
else {
printf("PTB-ERROR: Encoding current captured video frame failed. Failed to add frame to pipeline!\n");
}
return(FALSE);
}
}
else {
if (onMasterthread) {
PsychErrorExitMsg(PsychError_system, "Encoding current captured video frame failed. No videobuffer available.");
}
else {
printf("PTB-ERROR: Encoding current captured video frame failed. No videobuffer available!\n");
}
return(FALSE);
}
return(TRUE);
}
// Helper function: Convert image from dc1394 engine into final color format. Apply YUV->RGB colorspace
// conversion or debayering if neccessary:
static unsigned char* PsychDCPreprocessFrame(PsychVidcapRecordType* capdev)
{
dc1394error_t error;
int capturehandle = capdev->capturehandle;
// input_image points to the image buffer in our cam:
unsigned char* input_image = (unsigned char*) (capdev->frame->image);
// Check if frame is corrupt: As of year 2013, supported on USB and MacOSX, no-ops on Linux and Windows.
// The dc1394 method detects if the operating system reports corrupted / failed data transmission, therefore
// it can detect the types of corruption that the operating systems firewire stack / drivers can detect.
if (dc1394_capture_is_frame_corrupt(capdev->camera, capdev->frame)) {
if (PsychPrefStateGet_Verbosity() > 1) printf("PTB-WARNING: OS reports that a corrupt video frame with framecount %i was received from capture device %i.\n", capdev->framecounter, capdev->capturehandle);
capdev->corrupt_count++;
} else if ((capdev->specialFlags & 8) && (capdev->total_bytes > 0)) {
// Usercode requested additional CRC checksum checks on frames from camera via SFF and
// use of a Format7 mode allows us to do so, so do it.
//
// SFF checksumming is a feature of Basler cameras with SFF CRC support. The camera computes
// a CRC checksum of the video frame at sensor readout time and stores the checksum at the end
// of each frame transmitted over the bus. The following routine recomputes the CRC checksum of
// the received frame and compares it against the CRC stored in the frame. If both CRC's match
// then the frame survived untampered, otherwise the frame was corrupted somewhere on its way from
// the sensor to our buffer (Camera transceiver/firmware, firewire bus, firewire controller, operating
// system, libdc1394, ...).
// Note: This will cause a very significant increase in computational load!
if (!dc1394_basler_sff_check_crc((const uint8_t*) input_image, (uint32_t) capdev->total_bytes)) {
if (PsychPrefStateGet_Verbosity() > 1) printf("PTB-WARNING: Basler SFF CRC reports that a corrupt video frame with framecount %i was received from capture device %i.\n", capdev->framecounter, capdev->capturehandle);
capdev->corrupt_count++;
}
}
// Do we want to do something with the image data and have a scratch buffer for color conversion alloc'ed?
if (capdev->convframe) {
// Yes. Perform color-conversion YUV->RGB from cameras DMA buffer
// into the scratch buffer and set scratch buffer as source for
// all further operations:
if (capdev->colormode == DC1394_COLOR_CODING_RAW8 || capdev->colormode == DC1394_COLOR_CODING_MONO8 ||
capdev->colormode == DC1394_COLOR_CODING_RAW16 || capdev->colormode == DC1394_COLOR_CODING_MONO16) {
// Non-Format 7 capture modes do not allow to query the camera for the type of its Bayer-pattern.
// Therefore, if the bayer color_filter pattern is unknown due to non-Format-7 capture, assign
// the pattern manually set by usercode as color_filter_override setting:
if (capdev->frame->color_filter < DC1394_COLOR_FILTER_MIN || capdev->frame->color_filter > DC1394_COLOR_FILTER_MAX) {
capdev->frame->color_filter = capdev->color_filter_override;
}
// Trigger bayer filtering for debayering via 'method':
if (DC1394_SUCCESS != (error = dc1394_debayer_frames(capdev->frame, capdev->convframe, capdev->debayer_method))) {
printf("PTB-WARNING: Debayering of raw sensor image data failed! %s\n", dc1394_error_get_string(error));
if (error == DC1394_INVALID_COLOR_FILTER) {
printf("PTB-WARNING: Could not find out proper Bayer filter pattern for camera %i. Either select a\n", capturehandle);
printf("PTB-WARNING: Format-7 video capture mode to allow auto-detection, or use Screen('SetVideoCaptureParameter', ..., 'OverrideBayerPattern');\n");
printf("PTB-WARNING: to assign a suitable pattern manually.\n");
}
if (error == DC1394_INVALID_BAYER_METHOD) {
printf("PTB-WARNING: Invalid debayering method selected for camera %i. Select a different method via \n", capturehandle);
printf("PTB-WARNING: Screen('SetVideoCaptureParameter', ..., 'DebayerMethod');\n");
}
// Failure:
printf("PTB-ERROR: Bayer filtering of video frame failed.\n");
return(NULL);
}
}
else {
// Input data is in YUV format. Convert into RGB8:
capdev->convframe->color_coding = DC1394_COLOR_CODING_RGB8;
if (DC1394_SUCCESS != dc1394_convert_frames(capdev->frame, capdev->convframe)) {
// Failure:
printf("PTB-ERROR: Colorspace conversion of video frame failed.\n");
return(NULL);
}
}
// Success: Point to decoded image buffer:
input_image = (unsigned char*) capdev->convframe->image;
}
// Is a special markertracker plugin loaded for this camera? If so, execute it on this frame:
if (capdev->markerTrackerPlugin) {
// Yes! Execute: Plugin reads from (unsigned long*) input_image:
if (!(*TrackerPlugin_processFrame)(capdev->markerTrackerPlugin, (unsigned long*) input_image, capdev->width, capdev->height, capdev->roirect[kPsychLeft], capdev->roirect[kPsychTop], capdev->framecounter,
capdev->current_pts, capdev->framecounter)) {
if (PsychPrefStateGet_Verbosity() > 0) printf("PTB-WARNING: Failed to process video frame with framecount %i by markertracker plugin for capture device %i.\n", capdev->framecounter, capdev->capturehandle);
}
}
return(input_image);
}
// Helper routine: Updates the capdev->framecounter of a camera. Assumes capdev->frame contains
// the most recent frame dequeued from libdc1394 - the one the framecounter should correspond to
// after the update.
// - By default this will simply increment the capdev->framecounter++
//
// - On Basler cameras with SFF framecounter support it can query the framecounter value embedded
// into each captured frame, measuring the count of captured frames since camera power-up. This
// needs to be explicitely enabled by usercode. The problem which limits the usefulness of this
// camera hw counter is that the only way to reset it is a power-cycle, and many (most?) Basler
// cams can not be instructed to powerdown/powerup via IIDC commands. As we need a zero count at
// camera open time for our accounting logic to work, and as our powerup at device open and powerdown
// at device close is ineffective on Basler cameras, this would force the user to physically unplug
// replug each camera at start of each session, or power-cycle the whole machine! Not practical :(
// Implement it anyway as a off-by-default reference, in case this is of use to someone.
//
void PsychDCUpdateCameraFramecounter(PsychVidcapRecordType* capdev)
{
dc1394error_t error;
// SFF framecounter enabled and supported?
if ((capdev->specialFlags & 2) && (capdev->total_bytes > 0)) {
// Basler SFF framecounter enabled: Query it.
dc1394basler_sff_frame_counter_t *sff_framecount;
error = dc1394_basler_sff_chunk_find(DC1394_BASLER_SFF_FRAME_COUNTER, (void**) &sff_framecount, capdev->frame->image, capdev->total_bytes, (capdev->specialFlags & 8) ? DC1394_TRUE : DC1394_FALSE);
if (error) {
printf("PTB-ERROR: In PsychDCUpdateCameraFramecounter: dc1394_basler_sff_chunk_find() failed for SFF frame counter query [%s].\n", dc1394_error_get_string(error));
}
else {
// Retrieve value for this frame as tagged by camera itself:
capdev->framecounter = sff_framecount->counter;
if (PsychPrefStateGet_Verbosity() > 10) printf("PTB-INFO: Basler SFF framecount on device %i is %i\n", capdev->capturehandle, capdev->framecounter);
}
}
else {
// Classic: Increase counter of total number of captured frames by this camera:
capdev->framecounter++;
}
}
// Helper: Converts 32-Bit raw firewire bus cycle time into seconds. Firewire time wraps to zero
// every 128 seconds!
double PsychDCBusCycleTimeToSecs(uint32_t busCycleTime)
{
double cycleTimeSecs;
dc1394basler_sff_cycle_time_stamp_t ct;
// Copy busCycleTime into struct for parsing into seconds, iso-cycles and ticks:
ct.cycle_time_stamp.unstructured.value = busCycleTime;
// Convert 32-Bit count to seconds: 0-128 seconds + number of 125 usec iso cycles + offset in units of 1/24.576Mhz:
cycleTimeSecs = ((double) ct.cycle_time_stamp.structured.second_count) + ((double) (ct.cycle_time_stamp.structured.cycle_count * 125)) / 1e6 + ((double) ct.cycle_time_stamp.structured.cycle_offset) / ( 24.576e6);
return(cycleTimeSecs);
}
// Helper routine: Updates the capdev->current_pts frame timestamp for the current capdev->frame. Assumes capdev->frame contains
// the most recent frame dequeued from libdc1394 - the one the timestamp should correspond to after the update.
//
// - By default this will simply use the timestamp stored in capdev->frame->timestamp and map it to GetSecs time.
// This timestamp corresponds to when the firewire controller received the frame from the camera. It is usually
// close to microsecond accurate at least on Firewire bus cameras, but excludes any timing variability during fw
// transmission (usually in the sub-millisecond range due to bus iso channel arbitration on multi-cam capture),
// and an unknown latency from start of camera sensor exposure to start of sensor readout + data transfer.
//
// - On Basler cameras with SFF timestamp feature, it can read the firewire bus cycle count at start of exposure and
// remap that into GetSecs time, so a timestamp of actual start of sensor exposure is made available.
//
void PsychDCUpdateCameraFrameTimestamp(PsychVidcapRecordType* capdev)
{
dc1394error_t error;
dc1394basler_sff_cycle_time_stamp_t ct;
uint64_t systemtime;
double frameCycleTimeSecs, nowCycleTimeSecs, nowTimeSecs;
// SFF timestamping enabled and supported?
if ((capdev->specialFlags & 4) && (capdev->total_bytes > 0)) {
// Basler SFF timestamp enabled: Query it.
dc1394basler_sff_cycle_time_stamp_t *sff_cycletimestamp;
error = dc1394_basler_sff_chunk_find(DC1394_BASLER_SFF_CYCLE_TIME_STAMP, (void**) &sff_cycletimestamp, capdev->frame->image, capdev->total_bytes, (capdev->specialFlags & 8) ? DC1394_TRUE : DC1394_FALSE);
if (error) {
printf("PTB-ERROR: In PsychDCUpdateCameraFrameTimestamp: dc1394_basler_sff_chunk_find() failed for SFF timestamp query [%s].\n", dc1394_error_get_string(error));
}
else {
// Retrieve value for this frame as tagged by camera itself:
frameCycleTimeSecs = PsychDCBusCycleTimeToSecs(sff_cycletimestamp->cycle_time_stamp.unstructured.value);
// Retrieve current bus cycle time:
error = dc1394_read_cycle_timer(capdev->camera, &(ct.cycle_time_stamp.unstructured.value), &systemtime);
if (error) {
printf("PTB-ERROR: In PsychDCUpdateCameraFrameTimestamp: Failed to query bus cycle timer!\n");
}
// Get current Firewire bus time converted into seconds:
nowCycleTimeSecs = PsychDCBusCycleTimeToSecs(ct.cycle_time_stamp.unstructured.value);
nowTimeSecs = ((double) ((psych_uint64) systemtime)) / 1000000.0f;
// Check for wraparound every 128 seconds:
if (nowCycleTimeSecs > frameCycleTimeSecs) {
// No wraparound - Just compute how far in the past the frame was exposed on the camera before now,
// and subtract it from the current system time:
capdev->current_pts = nowTimeSecs - (nowCycleTimeSecs - frameCycleTimeSecs);
}
else {
// Wraparound! Same logic, but assume we are one 128 seconds cycle ahead of when the
// frame was exposed by the camera: [frameCycleTimeSecs ---> 128.0 aka 0.0 --> nowCycleTimeSecs]
capdev->current_pts = nowTimeSecs - (nowCycleTimeSecs + (128 - frameCycleTimeSecs));
}
// On both macOS and Linux our libdc1394 code computes CLOCK_REALTIME / gettimeofday()
// timestamps. Need to convert them to GetSecs reference time:
capdev->current_pts = PsychOSRealtimeToRefTime(capdev->current_pts);
if (PsychPrefStateGet_Verbosity() > 10) {
printf("PTB-INFO: Basler SFF timestamp on device %i is %f secs [raw value %i].\n", capdev->capturehandle, capdev->current_pts, sff_cycletimestamp->cycle_time_stamp.unstructured.value);
}
}
}
else if (capdev->specialFlags & 16) {
// Classic path:
// Query capture timestamp (in microseconds) and convert to seconds. This comes from the capture
// engine with (theroretically) microsecond precision and is assumed to be pretty accurate:
capdev->current_pts = ((double) capdev->frame->timestamp) / 1000000.0f;
// On both macOS and Linux our libdc1394 code computes CLOCK_REALTIME / gettimeofday()
// timestamps. Need to convert them to GetSecs reference time:
capdev->current_pts = PsychOSRealtimeToRefTime(capdev->current_pts);
}
}
// Main function of the asynchronous background video recording/processing thread:
static void* PsychDCRecorderThreadMain(void* capdevToCast)
{
int rc;
double tstart, tend;
dc1394error_t error;
unsigned char* input_image = NULL;
// Get a pointer to our associated capture device:
PsychVidcapRecordType* capdev = (PsychVidcapRecordType*) capdevToCast;
// Assign a name to ourselves, for debugging:
PsychSetThreadName("ScreenDC1394Rec");
// We are running at elevated realtime priority. Enter the while loop
// which waits for new video frames from libDC1394 and pushes them into
// the GStreamer movie recording pipeline and the receive slots/sinks for
// live video capture data:
while (TRUE) {
PsychLockMutex(&capdev->mutex);
// Start timestamp of processing cycle:
PsychGetAdjustedPrecisionTimerSeconds(&tstart);
// Stop if target framecount for end of capture/recording is reached:
if (capdev->framecounter >= capdev->stopAtFramecount) break;
// If we are a sync slave with an assigned sync master, make sure we don't get ahead of the master in our
// capture operations. This simplifies final synchronization at end of capture, so all cameras end with a
// identical framecount, regardless of different timing in execution and termination of recorderThreads:
if ((capdev->syncmasterhandle >= 0) && (capdev->syncmode & kPsychIsSyncSlave) && vidcapRecordBANK[capdev->syncmasterhandle].valid &&
(capdev->framecounter >= vidcapRecordBANK[capdev->syncmasterhandle].framecounter)) {
// Sync master is not ahead of us. Let's wait for it to get ahead of us, before
// we try to dequeue new frames:
if (PsychPrefStateGet_Verbosity() > 5) printf("PTB-DEBUG Sync slave %i waiting for sync master's framecount getting ahead...\n", capdev->capturehandle, capdev->syncmasterhandle);
// Wait with a timeout of 1 second for the syncmaster thread to signal arrival of a new frame:
PsychTimedWaitCondition(&(vidcapRecordBANK[capdev->syncmasterhandle].condition), &(capdev->mutex), 1.0);
// Retry comparison between master and slave framecounter:
PsychUnlockMutex(&capdev->mutex);
continue;
}
// Check for new video frame in polling mode:
error = dc1394_capture_dequeue(capdev->camera, DC1394_CAPTURE_POLICY_POLL, &(capdev->frame));
// Success?
if (error != DC1394_SUCCESS) {
// Error! Abort:
printf("PTB-ERROR: In background video processing thread: dc1394_capture_dequeue() failed [%s]! Aborting processing thread.\n", dc1394_error_get_string(error));
break;
}
// New frame received?
if (capdev->frame) {
// Ok, new frame ready and dequeued from DMA ringbuffer:
capdev->frame_ready = 1;
// Store count of currently queued frames (in addition to the one just fetched).
// This is an indication of how well the users script is keeping up with the video stream,
// technically the number of frames that would need to be dropped to keep in sync with the
// stream.
capdev->current_dropped = (int) capdev->frame->frames_behind;
capdev->nrframes_pending = (unsigned int) capdev->frame->frames_behind;
// Update the master framecounter for this camera and the new fetched frame:
PsychDCUpdateCameraFramecounter(capdev);
// Also increase "decompressed frames" counter, which is the same in case of threaded processing:
capdev->nrframes++;
// Update and assign capture timestamp capdev->current_pts for dequeued frame. But first we
// assign tstart as a fallback timestamp, in case PsychDCUpdateCameraFrameTimestamp() can't
// fetch a timestamp from the video capture device or operating system, e.g., as on USB-3
// cameras with libdc1394 versions from December 2014. Using tstart as approximation is as
// good as it gets if proper timestamping won't work, but could be multiple msecs off:
capdev->current_pts = tstart;
PsychDCUpdateCameraFrameTimestamp(capdev);
// Perform potential processing on image, e.g., debayering:
input_image = PsychDCPreprocessFrame(capdev);
if (NULL == input_image) {
printf("PTB-ERROR: Bayer filtering or color space conversion of video frame in video recorder thread failed. Aborting recorder thread.\n");
break;
}
// Push new frame to the GStreamer video encoding pipeline:
if (capdev->moviehandle != -1) {
// Yes. Push data to encoder now. Abort thread on failure to push/encode:
if (!PsychDCPushFrameToMovie(capdev, (psych_uint16*) input_image, capdev->current_pts, FALSE)) break;
}
// Provide new frame to masterthread / usercode, if frame delivery isn't disabled:
if (!(capdev->recordingflags & 4)) {
// Copy frame to "current frame" buffer if low-latency mode is active:
if (capdev->dropframes) {
unsigned int count = (capdev->width * capdev->height * ((capdev->actuallayers == 3) ? 3 : 1) * ((capdev->bitdepth > 8) ? 2 : 1));
// Release previous one, if not fetched by now by masterthread:
if (capdev->current_frame) free(capdev->current_frame);
// Allocate target buffer for most recent captured frame from video recorder thread:
capdev->current_frame = (unsigned char*) malloc(count);
// Copy image into it:
memcpy(capdev->current_frame, input_image, count);
}
}
// Signal availability of new video frame: This is not only important for frame fetching on the master thread,
// but also for slave cameras recorderThreads if multi-cam synchronization is enabled:
if ((rc = PsychBroadcastCondition(&(capdev->condition)))) {
printf("PTB-ERROR: In background video processing thread: PsychBroadcastCondition() failed [%s]!\n", strerror(rc));
}
// Requeue the recently dequeued and no longer needed buffer:
if (dc1394_capture_enqueue(capdev->camera, capdev->frame) != DC1394_SUCCESS) {
printf("PTB-ERROR: Requeuing of used up video frame buffer in video recorder/processing thread failed! Aborting recorder thread.\n");
break;
}
capdev->frame = NULL;
// Update stats for decompression:
PsychGetAdjustedPrecisionTimerSeconds(&tend);
// Update avg. decompress time:
capdev->avg_decompresstime += (tend - tstart);
// Release mutex, so masterthread can get frame data or control camera/capture:
PsychUnlockMutex(&capdev->mutex);
}
else {
// No new frame received in this poll iteration.
// This means there aren't any pending in the queue:
capdev->nrframes_pending = 0;
// Release mutex, so masterthread can get frame data or control camera/capture:
PsychUnlockMutex(&capdev->mutex);
// Sleep a bit, so we won't overload cpu. We are more agressive in low latency mode:
PsychYieldIntervalSeconds((capdev->dropframes) ? 0.001 : 0.004);
}
// Next capture loop iteration...
}
// End of capture thread execution. Clean up and unlock mutex:
capdev->frame_ready = 0;
PsychUnlockMutex(&capdev->mutex);
// Ok, we are done: Go and die peacefully...
return(NULL);
}
// Helper for start/stop capture with bus-sync:
static void PsychDCEnableBusBroadcast(PsychVidcapRecordType* capdev, psych_bool enable)
{
dc1394error_t err;
// Only the sync master ever sends broadcast commands over the bus, so we only need
// to en/disable its broadcast behaviour. All cameras always listen to bus broadcast,
// broadcast reception can't be disabled, so we can only control the behaviour of a
// camera wrt. sending commands. This means that you have to make sure that you only
// connect cameras to a bus which should participate in synced capture - it is not
// possible for a camera on such a bus to be free-running, as it will automatically
// listen in on the broadcast commands from the sync master on the same bus.
if (capdev->syncmode & kPsychIsSyncMaster) {
if ((err = dc1394_camera_set_broadcast(capdev->camera, (enable) ? DC1394_TRUE : DC1394_FALSE)) != DC1394_SUCCESS) {
// Failed! Shutdown DMA capture engine again:
dc1394_capture_stop(capdev->camera);
printf("PTB-ERROR: Could not %s bus broadcast for bus-synced multi-camera capture operation: %s\n", (enable) ? "enable" : "disable", dc1394_error_get_string(err));
PsychErrorExitMsg(PsychError_user, "Unable to control bus broadcast for master camera for bus sync - Operation aborted!");
}
}
}
/*
* PsychVideoCaptureRate() - Start- and stop video capture.
*
* capturehandle = Grabber to start-/stop.
* capturerate = zero == Stop capture, non-zero == Capture. Tries to choose smallest framerate
* that is equal or higher than requested rate. Special value capturerate == DBL_MAX will choose
* the fasted supported framerate for the other given capture settings.
* dropframes = 0 - Always deliver oldest frame in DMA ringbuffer. 1 - Always deliver newest frame.
* --> 1 == drop frames in ringbuffer if behind -- low-latency capture.
* startattime = Deadline (in system time) for which to wait before real start of capture.
* Returns Number of dropped frames during capture.
*/
int PsychDCVideoCaptureRate(int capturehandle, double capturerate, int dropframes, double* startattime)
{
char feedbackString[8192];
char processingString[8000];
int dropped = 0;
float framerate = 0;
dc1394speed_t speed;
dc1394video_mode_t mode;
int i, w, h, packetsize;
dc1394framerate_t dc1394_framerate;
dc1394color_coding_t color_code;
uint32_t depth;
dc1394error_t err;
int rc;
// Retrieve device record for handle:
PsychVidcapRecordType* capdev = PsychGetVidcapRecord(capturehandle);
// Start- or stop capture?
if (capturerate > 0) {
// Start capture:
if (capdev->grabber_active) PsychErrorExitMsg(PsychError_user, "You tried to start video capture, but capture is already started!");
// Reset statistics:
capdev->nr_droppedframes = 0;
capdev->frame_ready = 0;
// Resync framecounters: Only important for multi-threaded recording with 'dropframes = 0' video buffer
// feedback over GStreamer pipeline appsrc -> process -> appsink -> pull.
capdev->pulled_framecounter = capdev->framecounter;
// Select best matching mode for requested image size and pixel format:
// ====================================================================
w = (int) PsychGetWidthFromRect(capdev->roirect);
h = (int) PsychGetHeightFromRect(capdev->roirect);
// Can we (potentially) get along with a non-Format-7 mode?
// Check minimum requirements for non-Format-7 mode and if the "prefer Format-7" specialFlags setting 1
// is chosen:
if ((capdev->specialFlags & 1) || !((capdev->roirect[kPsychLeft]==0 && capdev->roirect[kPsychTop]==0) &&
((capdev->roirect[kPsychRight]==1 && capdev->roirect[kPsychBottom]==1) || (w==640 && h==480) ||
(w==800 && h==600) || (w==1024 && h==768) || (w==1280 && h==960) || (w==1600 && h==1200) ||
(w==320 && h==240) || (w==160 && h==120)) &&
(capturerate==1.875 || capturerate==3.75 || capturerate==7.5 || capturerate==15 || capturerate==30 ||
capturerate==60 || capturerate==120 || capturerate==240))) {
// Ok, the requested ROI and/or framerate is not directly supported by non-Format7 capture modes,
// or usercode explicitely requested preference for Format-7 capture.
// Try to find a good format-7 mode, fall back to NF-7 if format 7 doesn't work out:
if ((packetsize=PsychVideoFindFormat7Mode(capdev, capturerate))==0) {
// Could not find good Format-7 mode! Try NF-7: This function will exit if we don't find
// a useful match at all:
PsychVideoFindNonFormat7Mode(capdev, capturerate);
}
// Ok either we have a format-7 mode ready to go (packetsize>0) or we have a default
// non-format-7 mode ready (packetsize==0)...
}
else {
// The requested combo of ROI and framerate should be supported by standard non-format-7 capture:
// Try it and exit in case of non-match:
PsychVideoFindNonFormat7Mode(capdev, capturerate);
packetsize = 0;
}
// The PsychVideoFind(Non)Format7Mode() logic above already set the ROI for capture...
// Setup capture hardware and DMA engine:
if (dc1394_video_get_iso_speed(capdev->camera, &speed)!=DC1394_SUCCESS) {
PsychErrorExitMsg(PsychError_user, "Unable to query bus-speed - Start of video capture failed!");
}
// Special hack for Unibrain Fire-i: This camera can do 400 Megabit/second, but reports
// a speed of 100 MBit after a cold-start! We enforce a 400 Megabit speed if this is a
// Unibrain Fire-i:
if (strstr(capdev->camera->vendor, "Unibrain") && strstr(capdev->camera->model, "Fire-i")) {
// Unibrain Fire-i: Enforce correct speed:
speed = DC1394_ISO_SPEED_400;
if(PsychPrefStateGet_Verbosity()>5) printf("PTB-DEBUG: Unibrain Fire-i detected. Setting override bus speed 400 MBit...\n"); fflush(NULL);
}
// Assign final mode and framerate:
dc1394_framerate = capdev->dc_framerate;
mode = capdev->dc_imageformat;
// Query final color format of captured data:
dc1394_get_color_coding_from_video_mode(capdev->camera, mode, &color_code);
capdev->colormode = color_code;
// Setup DMA engine:
// =================
// Set ISO speed:
if(PsychPrefStateGet_Verbosity()>5) printf("PTB-DEBUG: Setting ISO speed... "); fflush(NULL);
err = dc1394_video_set_iso_speed(capdev->camera, speed);
if (err != DC1394_SUCCESS) {
PsychErrorExitMsg(PsychError_user, "Unable to setup and start capture engine: Setting ISO speed failed!");
}
if(PsychPrefStateGet_Verbosity()>5) printf("...done.\n"); fflush(NULL);
// Set Mode:
if(PsychPrefStateGet_Verbosity()>5) printf("PTB-DEBUG: Setting mode..."); fflush(NULL);
err = dc1394_video_set_mode(capdev->camera, mode);
if (err != DC1394_SUCCESS) {
PsychErrorExitMsg(PsychError_user, "Unable to setup and start capture engine: Setting mode failed!");
}
if(PsychPrefStateGet_Verbosity()>5) printf("...done.\n"); fflush(NULL);
// Set Framerate for non-Format7 modes: This is redundant in Format7 mode...
if(PsychPrefStateGet_Verbosity()>5) printf("PTB-DEBUG: Setting framerate (even in nonFormat-7!)..."); fflush(NULL);
err = dc1394_video_set_framerate(capdev->camera, dc1394_framerate);
if (err != DC1394_SUCCESS) {
PsychErrorExitMsg(PsychError_user, "Unable to setup and start capture engine: Setting fixed framerate failed!");
}
if(PsychPrefStateGet_Verbosity()>5) printf("...done.\n"); fflush(NULL);
// Format-7 capture?
if (packetsize > 0) {
// Format-7 capture DMA setup: We only set mode, color format, packetsize (and thereby framerate) and ROI (and thereby imagesize):
if(PsychPrefStateGet_Verbosity()>5) printf("PTB-DEBUG: Setting format-7 ROI..."); fflush(NULL);
err = dc1394_format7_set_roi(capdev->camera, mode, color_code, packetsize, (unsigned int) capdev->roirect[kPsychLeft], (unsigned int) capdev->roirect[kPsychTop],
(unsigned int) PsychGetWidthFromRect(capdev->roirect), (unsigned int) PsychGetHeightFromRect(capdev->roirect));
if (err != DC1394_SUCCESS) {
PsychErrorExitMsg(PsychError_system, "Unable to setup and start capture engine: Setting Format7 ROI failed!");
}
if(PsychPrefStateGet_Verbosity()>5) printf("...done. \n"); fflush(NULL);
// Query total number of bytes per delivered frame (including SFF chunks and other meta-data):
capdev->total_bytes = 0;
err = dc1394_format7_get_total_bytes(capdev->camera, capdev->dc_imageformat, &(capdev->total_bytes));
if (err != DC1394_SUCCESS) {
PsychErrorExitMsg(PsychError_system, "Unable to setup and start capture engine: Query of total bytes per frame for Format7 mode failed!");
}
}
else {
// Non-Format-7 capture DMA setup: mode encodes image size, ROI and color format.
// Set total_bytes to zero to signal that SFF features are not available in this capture session:
capdev->total_bytes = 0;
// SFF is a no-go in non-Format7 so warn user if somebody tries:
if ((capdev->specialFlags & (2 | 4 | 8)) && (PsychPrefStateGet_Verbosity() > 1)) {
printf("PTB-WARNING: Usercode requested use of a Basler-SFF feature on camera %i, but capture is using a non-Format7 mode. SFF is unsupported in this configuration! Using fallbacks.\n");
}
}
// Framedropping is no longer supported by libdc, so we implement it ourselves.
// Store the 'dropframes' flag in our capdev struct, so the PsychDCGetTextureFromCapture()
// knows how to handle this:
capdev->dropframes = (dropframes > 0) ? 1 : 0;
// Finally setup and start DMA video capture engine with default flags (auto allocation and
// release of iso bandwidth and channels) and num_dmabuffers DMA buffer slots in internal video FIFO:
if(PsychPrefStateGet_Verbosity()>5) printf("PTB-DEBUG: Setting up DMA capture now!"); fflush(NULL);
err = dc1394_capture_setup(capdev->camera, capdev->num_dmabuffers, DC1394_CAPTURE_FLAGS_DEFAULT);
if (err != DC1394_SUCCESS) {
// Failed! We clean up and fail:
// Non-DMA path no longer supported by final libdc V2, so this is the end of the game...
PsychErrorExitMsg(PsychError_system, "Unable to setup and start DMA capture engine - Start of video capture failed!");
}
if(PsychPrefStateGet_Verbosity()>5) printf(" DMA-Engine started.\n"); fflush(NULL);
// No sync master by default. Once the sync master gets started, it will set this
// field on all slaves to reference himself:
capdev->syncmasterhandle = -1;
// If this is the start of the designated sync master, then assign its handle to all (already started/engaged) sync slaves:
if (capdev->syncmode & kPsychIsSyncMaster) {
for (i = 0; i < PSYCH_MAX_CAPTUREDEVICES; i++) {
// Sync slave?
if ((vidcapRecordBANK[i].valid) && (i != capturehandle) && (vidcapRecordBANK[i].syncmode & kPsychIsSyncSlave) && !(vidcapRecordBANK[i].syncmode & kPsychNoLockStepSync)) {
// Yes. Assign capturehandle of sync master:
PsychLockMutex(&vidcapRecordBANK[i].mutex);
vidcapRecordBANK[i].syncmasterhandle = capturehandle;
PsychUnlockMutex(&vidcapRecordBANK[i].mutex);
if (PsychPrefStateGet_Verbosity() > 3) printf("PTB-DEBUG: Sync slave %i recorder thread will lock-step with sync master %i.\n", i, capturehandle);
}
}
}
// Ready to go! Now we just need to tell the camera to start its capture cycle:
// Wait until start deadline reached:
if (*startattime != 0) PsychWaitUntilSeconds(*startattime);
// Firewire bus-sync via bus-wide broadcast of iso-on command requested?
if (capdev->syncmode & kPsychIsBusSynced) {
// Yes. The master should broadcast its start command to all clients on
// the bus:
PsychDCEnableBusBroadcast(capdev, TRUE);
}
// Only actually send start command for iso-transmission if this isn't a slave cam, or
// if this is hardware-synced via external trigger cable, so iso-start doesn't actually
// start capture of the slave camera but only engages it for trigger reception:
if (!(capdev->syncmode & kPsychIsSyncSlave) || (capdev->syncmode & kPsychIsHwSynced)) {
// Start isochronous data transfer:
// This will engage the slave camera for trigger reception with hw-sync.
// It will start capture and engage trigger signal emission on a master cam with hw-sync.
// It will start capture on a free running cam or a soft-synced master camera.
// It will start capture of a master camera and broadcast the start signal to all slave
// cameras with bus-sync.
if ((capdev->syncmode > 0) && (PsychPrefStateGet_Verbosity() > 3)) {
printf("PTB-DEBUG: Enabling cameras ISO transmission in syncmode %i -- Start of capture...\n", capdev->syncmode);
if (capdev->syncmode & kPsychIsBusSynced) printf("PTB-DEBUG: Bus synced operation.");
if (capdev->syncmode & kPsychIsSoftSynced) printf("PTB-DEBUG: Software synced operation.");
if (capdev->syncmode & kPsychIsHwSynced) printf("PTB-DEBUG: Hardware synced operation.");
printf(" Start of %s%s.\n", (capdev->syncmode & kPsychIsSyncMaster) ? "master" : "slave", (capdev->syncmode & kPsychNoLockStepSync) ? " without lockstep sync" : "");
}
if (capdev->syncmode & kPsychIsHwSynced) {
// Disable use of external trigger signals on both master and slaves to start with:
if (dc1394_external_trigger_set_power(capdev->camera, DC1394_OFF)) {
// Failed! Shutdown DMA capture engine again:
dc1394_capture_stop(capdev->camera);
PsychErrorExitMsg(PsychError_user, "Unable to disable trigger reception on camera - Phase I - Start of video capture failed!");
}
// If this is a slave, enable use of external trigger signals now:
if (capdev->syncmode & kPsychIsSyncSlave) {
// Now enable trigger reception on slave:
if (dc1394_external_trigger_set_power(capdev->camera, DC1394_ON)) {
// Failed! Shutdown DMA capture engine again:
dc1394_capture_stop(capdev->camera);
PsychErrorExitMsg(PsychError_user, "Unable to enable trigger reception on slave camera - Phase II - Start of video capture failed!");
}
}
}
if (dc1394_video_set_transmission(capdev->camera, DC1394_ON) !=DC1394_SUCCESS) {
// Failed! Shutdown DMA capture engine again:
dc1394_capture_stop(capdev->camera);
PsychErrorExitMsg(PsychError_user, "Unable to start isochronous data transfer from camera - Start of video capture failed!");
}
// Is this a soft-synced configuration and the master has just started capturing?
if ((capdev->syncmode & kPsychIsSoftSynced) && (capdev->syncmode & kPsychIsSyncMaster)) {
// Yes. Quickly start all soft-synced slaves in our setup:
// TODO: Would be better to have per-master lists of sync slaves for more flexibility.
for (i = 0; i < PSYCH_MAX_CAPTUREDEVICES; i++) {
// Sync slave participating in this soft sync start operation?
if ((vidcapRecordBANK[i].valid) && (i != capturehandle) && (vidcapRecordBANK[i].syncmode & kPsychIsSoftSynced) &&
(vidcapRecordBANK[i].syncmode & kPsychIsSyncSlave)) {
// Yes. Start it:
PsychLockMutex(&vidcapRecordBANK[i].mutex);
if (dc1394_video_set_transmission(vidcapRecordBANK[i].camera, DC1394_ON) !=DC1394_SUCCESS) {
// Failed!
PsychUnlockMutex(&vidcapRecordBANK[i].mutex);
PsychErrorExitMsg(PsychError_user, "Unable to start isochronous data transfer of soft-synced slave camera - Start of sync video capture failed!");
}
PsychUnlockMutex(&vidcapRecordBANK[i].mutex);
}
}
}
}
// Record real start time:
PsychGetAdjustedPrecisionTimerSeconds(startattime);
if(PsychPrefStateGet_Verbosity()>5) printf("PTB-DEBUG: Capture engine fully running...\n"); fflush(NULL);
// Firewire bus-sync via bus-wide broadcast of iso-on command requested?
if (capdev->syncmode & kPsychIsBusSynced) {
// Yes. Capture started, so the master should stop broadcasting all of its other commands to all clients on
// the bus:
PsychDCEnableBusBroadcast(capdev, FALSE);
}
// Map framerate enum to floating point value and assign it:
if (packetsize == 0) {
dc1394_framerate_as_float(dc1394_framerate, &framerate);
}
else {
dc1394_format7_get_frame_interval(capdev->camera, mode, &framerate);
if (framerate == 0) {
framerate = capdev->fps;
}
else {
if (PsychPrefStateGet_Verbosity() > 4) printf("PTB-Info: Camera reported frame interval %f\n", framerate);
// FIXME: This is most likely a wrong conversion constant 1e9, but
// could not find the proper value in the spec and don't have a
// camera which reports a sensible value, so this is pure speculation:
// This does not seem to work at all on the only camera with which i could
// test it (all other fw cams just report 0 == not supported),
// so disable it for more sane results:
// framerate = 1e9 / framerate;
framerate = capdev->fps;
}
}
capdev->fps = (double) framerate;
// Setup size and position:
capdev->width = (int) PsychGetWidthFromRect(capdev->roirect);
capdev->height = (int) PsychGetHeightFromRect(capdev->roirect);
// Ok, capture is now started:
capdev->grabber_active = 1;
// Reset stopAtFramecount from latched value:
capdev->stopAtFramecount = capdev->stopAtFramecountLatch;
capdev->nrframes_pending = 0;
// Query effective bpc value of video mode: The number of actual bits of information per color/luminance channel:
if (DC1394_SUCCESS != dc1394_video_get_data_depth(capdev->camera, &depth)) {
printf("PTB-WARNING: Could not query data depth of video mode for camera %i - Assuming i got the requested %i bpc and hoping for the best.\n", capturehandle, capdev->bitdepth);
}
else {
// Assign queried size: For a 8 bit mode typically 8 bpc, but for a 16 bit "container" mode,
// it could be anywhere between 9 bpc and 16 bpc, depending on true sensor bit depth of camera:
capdev->bitdepth = depth;
}
// Allocate conversion buffer if needed for YUV->RGB or Bayer->RGB conversions.
if (capdev->actuallayers == 3 && color_code != DC1394_COLOR_CODING_RGB8 && color_code != DC1394_COLOR_CODING_RGB16) {
// Software conversion of YUV or RAW -> RGB needed. Allocate a proper scratch-buffer:
capdev->convframe = (dc1394video_frame_t*) malloc(sizeof(dc1394video_frame_t));
memset(capdev->convframe, 0, sizeof(dc1394video_frame_t));
}
if (PsychPrefStateGet_Verbosity() > 2) {
printf("PTB-INFO: Capture started on device %i - Width x Height = %i x %i - Framerate: %f fps, bpc = %i.\n", capturehandle, capdev->width, capdev->height, capdev->fps, capdev->bitdepth);
}
// Initialize and potentially assign usercode provided GStreamer pipeline processing string:
processingString[0] = 0;
processingString[sizeof(processingString) - 1] = 0;
if (capdev->processingString) strncpy(&(processingString[0]), capdev->processingString, sizeof(processingString) - 1);
// Now that capture is successfully started, do we also want to record video to a file?
if (capdev->recording_active) {
// Yes. Setup movie writing:
if ((capdev->recordingflags & 16) && !dropframes && !(capdev->recordingflags & 4)) {
// Multi-threaded video recording/processing and due to dropframes = FALSE, video data is enqeued by recorderThread
// into GStreamer pipeline and then processed and dequeued from our special appsink via the following snippet of
// pipeline:
sprintf(feedbackString, "tee name=ptbframediverter ! %s%s appsink name=ptbvideoappsink sync=false async=false enable-last-sample=false emit-signals=false ptbframediverter. ! queue ! ", processingString,
(strlen(processingString) > 0) ? " !" : "");
capdev->moviehandle = PsychCreateNewMovieFile(capdev->targetmoviefilename, capdev->width, capdev->height, (double) framerate, capdev->actuallayers, ((capdev->bitdepth > 8) ? 16 : 8), capdev->codecSpec,
(char*) &(feedbackString[0]));
}
else {
// No multi-threaded video recording with dropframes = FALSE or feedback via Screen('GetCapturedImage') disabled
// via recordingflags & 4. No need for feedback of video data via GStreamer, but need for recording, so create
// a classic video encoding pipeline with GStreamer:
capdev->moviehandle = PsychCreateNewMovieFile(capdev->targetmoviefilename, capdev->width, capdev->height, (double) framerate, capdev->actuallayers, ((capdev->bitdepth > 8) ? 16 : 8), capdev->codecSpec, NULL);
}
// Failed?
if (capdev->moviehandle == -1) {
PsychErrorExitMsg(PsychError_user, "Setup of video recording failed.");
}
else {
if (PsychPrefStateGet_Verbosity() > 2) {
printf("PTB-INFO: Video recording started on device %i into moviefile '%s'.\n", capturehandle, capdev->targetmoviefilename);
}
}
}
else if ((capdev->recordingflags & 16) && !dropframes) {
// This path only makes sense if Screen('GetCapturedImage') is enabled, ie., not suppressed via recordingflags & 4:
if (capdev->recordingflags & 4) {
PsychErrorExitMsg(PsychError_user, "You set recordingflags & 4 to suppress live video capture and didn't request video recording?!? This is pointless, you need to do at least one of those!");
}
// Multi-threaded video processing and due to dropframes = FALSE, video data is enqeued by recorderThread
// into GStreamer pipeline and then processed and dequeued from our special appsink via the following snippet of
// pipeline:
sprintf(feedbackString, "%s%s appsink name=ptbvideoappsink sync=false async=false enable-last-sample=false emit-signals=false", processingString, (strlen(processingString) > 0) ? " !" : "");
capdev->moviehandle = PsychCreateNewMovieFile("", capdev->width, capdev->height, (double) framerate, capdev->actuallayers, ((capdev->bitdepth > 8) ? 16 : 8),
((capdev->codecSpec) && (strlen(capdev->codecSpec) > 0)) ? capdev->codecSpec : "UseVFR", (char*) &(feedbackString[0]));
// Failed?
if (capdev->moviehandle == -1) {
PsychErrorExitMsg(PsychError_user, "Setup of background GStreamer video processing failed.");
}
else {
if (PsychPrefStateGet_Verbosity() > 2) {
printf("PTB-INFO: GStreamer background video processing started on device %i.\n", capturehandle);
}
}
}
// Async background recording/processing requested?
if (capdev->recordingflags & 16) {
// Yes. Setup and start recording thread.
PsychLockMutex(&capdev->mutex);
PsychUnlockMutex(&capdev->mutex);
// Create and startup thread:
if ((rc = PsychCreateThread(&(capdev->recorderThread), NULL, PsychDCRecorderThreadMain, (void*) capdev))) {
printf("PTB-ERROR: In Screen('StartVideoCapture'): Could not create background video recording/processing thread [%s].\n", strerror(rc));
PsychErrorExitMsg(PsychError_system, "Thread creation for video recording/processing failed!");
}
// Boost priority of recorderThread by 1 level and switch it to RT scheduling,
// unless it is already RT-Scheduled. As the thread inherited our scheduling
// priority from PsychCreateThread(), we only need to +1 tweak it from there:
PsychSetThreadPriority(&(capdev->recorderThread), 2, 1);
// Recorder thread is in charge of dequeuing video frames from libdc1394 and pushing it
// into the movie recording pipeline and into our own receive slot or videosink.
if (PsychPrefStateGet_Verbosity() > 3) {
printf("PTB-INFO: Video %s on device %i is performed on async background thread.\n", (capdev->recording_active) ? "recording" : "capture", capturehandle);
}
}
}
else {
// Stop capture:
if (capdev->grabber_active) {
// recorderThread might be running for this camera, so use locking:
PsychLockMutex(&capdev->mutex);
// If the target stopAtFramecount is undefined (== 0xffffffff) or higher than what can
// possibly be reached during this session due to imminent stop of capture, then adapt/set
// the stopAtFramecount to the maximum attainable value, which is the current framecount +
// whatever is left in the capture queue at the moment:
if (capdev->stopAtFramecount > (capdev->framecounter + capdev->nrframes_pending)) {
capdev->stopAtFramecount = capdev->framecounter + capdev->nrframes_pending;
}
// If this is the sync master then its final framecount determines the target framecount
// for itself and all its slaves, e.g., for pushing dequeued frames to video recording:
if (capdev->syncmode & kPsychIsSyncMaster) {
// This is the sync-master. Its final framecount after it stopped capture determines
// how many frames its own recorderThread and all the slaves recorderThreads should
// dequeue and potentially push to the video recording pipeline.
//
// Set masters stopAtFramecount also for all slaves, unless the slaves already have a
// lower limit assigned manually or due to preceeding Screen('StopCapture'):
for (i = 0; i < PSYCH_MAX_CAPTUREDEVICES; i++) {
// Sync slave participating in this synced stop operation?
if ((vidcapRecordBANK[i].valid) && (i != capturehandle) && (vidcapRecordBANK[i].syncmode & kPsychIsSyncSlave)) {
// Yes. Assign stopAtFramecount:
PsychLockMutex(&vidcapRecordBANK[i].mutex);
if (vidcapRecordBANK[i].stopAtFramecount > capdev->stopAtFramecount) vidcapRecordBANK[i].stopAtFramecount = capdev->stopAtFramecount;
PsychUnlockMutex(&vidcapRecordBANK[i].mutex);
}
}
}
if (PsychPrefStateGet_Verbosity() > 3) {
printf("PTB-INFO: Video capture on device %i will stop at target framecount %i.\n", capturehandle, capdev->stopAtFramecount);
}
// Firewire bus-sync via bus-wide broadcast of iso-on command requested?
if (capdev->syncmode & kPsychIsBusSynced) {
// Yes. The master should broadcast its stop command to all clients on the bus:
PsychDCEnableBusBroadcast(capdev, TRUE);
}
// Only actually send stop command for iso-transmission if this isn't a slave cam, or
// if this is hardware-synced via external trigger cable, so iso-stop doesn't actually
// stop capture of the slave camera but only disable it for trigger reception:
if (!(capdev->syncmode & kPsychIsSyncSlave) || (capdev->syncmode & kPsychIsHwSynced)) {
// Stop isochronous data transfer:
// This will disable the slave camera for trigger reception with hw-sync.
// It will stop capture and disable trigger signal emission on a master cam with hw-sync.
// It will stop capture on a free running cam or a soft-synced master camera.
// It will stop capture of a master camera and broadcast the stop signal to all slave
// cameras with bus-sync.
if (dc1394_video_set_transmission(capdev->camera, DC1394_OFF) !=DC1394_SUCCESS) {
// Failed! Shutdown DMA capture engine again:
dc1394_capture_stop(capdev->camera);
PsychUnlockMutex(&capdev->mutex);
PsychErrorExitMsg(PsychError_user, "Unable to stop isochronous data transfer from camera - Stop of video capture failed!");
}
// Is this a soft-synced configuration and the master has just stopped capture?
if ((capdev->syncmode & kPsychIsSoftSynced) && (capdev->syncmode & kPsychIsSyncMaster)) {
// Yes. Quickly stop all soft-synced slaves in our setup:
// TODO: Would be better to have per-master lists of sync slaves for more flexibility.
for (i = 0; i < PSYCH_MAX_CAPTUREDEVICES; i++) {
// Sync slave participating in this soft sync stop operation?
if ((vidcapRecordBANK[i].valid) && (i != capturehandle) && (vidcapRecordBANK[i].syncmode & kPsychIsSoftSynced) &&
(vidcapRecordBANK[i].syncmode & kPsychIsSyncSlave)) {
// Yes. Stop it:
PsychLockMutex(&vidcapRecordBANK[i].mutex);
if (dc1394_video_set_transmission(vidcapRecordBANK[i].camera, DC1394_OFF) !=DC1394_SUCCESS) {
// Failed!
PsychUnlockMutex(&vidcapRecordBANK[i].mutex);
PsychUnlockMutex(&capdev->mutex);
PsychErrorExitMsg(PsychError_user, "Unable to stop isochronous data transfer of soft-synced slave camera - Stop of sync video capture failed!");
}
PsychUnlockMutex(&vidcapRecordBANK[i].mutex);
}
}
}
if (capdev->syncmode & kPsychIsHwSynced) {
// Disable use of external trigger signals:
dc1394_external_trigger_set_power(capdev->camera, DC1394_OFF);
}
}
// Firewire bus-sync via bus-wide broadcast of iso-on command requested?
if (capdev->syncmode & kPsychIsBusSynced) {
// Yes. Capture stopped, so the master should stop broadcasting all of its other commands to all clients on the bus:
PsychDCEnableBusBroadcast(capdev, FALSE);
}
// Stop capture engine of current cam:
dc1394_capture_stop(capdev->camera);
// Ok, capture is now stopped.
capdev->grabber_active = 0;
// Done with camera access for now:
PsychUnlockMutex(&capdev->mutex);
// Was async background recording/processing active?
if (capdev->recordingflags & 16) {
// Yes. Stop and destroy recording thread.
// Wait for thread termination, cleanup and release the thread:
PsychDeleteThread(&(capdev->recorderThread));
// Ok, thread is dead. Mark it as such:
capdev->recorderThread = (psych_thread) NULL;
// Recorder thread is in charge of dequeuing video frames from libdc1394 and pushing it
// into the movie recording pipeline and into our own receive slot or videosink.
if (PsychPrefStateGet_Verbosity() > 3) {
printf("PTB-INFO: Async video %s thread on device %i stopped.\n", (capdev->recording_active) ? "recording" : "processing", capturehandle);
}
}
// Video recording/GStreamer processing active? Then we should stop it now:
if (capdev->moviehandle > -1) {
if (capdev->recording_active && (PsychPrefStateGet_Verbosity() > 2)) printf("PTB-INFO: Stopping video recording on device %i and closing moviefile '%s'\n", capturehandle, capdev->targetmoviefilename);
if (!capdev->recording_active && (PsychPrefStateGet_Verbosity() > 2)) printf("PTB-INFO: Stopping GStreamer video background processing on device %i.\n", capturehandle);
// Flush and close video encoding pipeline, finalize and close movie file:
if (PsychFinalizeNewMovieFile(capdev->moviehandle) == 0) {
capdev->moviehandle = -1;
PsychErrorExitMsg(PsychError_user, "Stop of video recording/processing failed.");
}
// Done with recording:
capdev->moviehandle = -1;
}
// Release dc1394video_frame_t convframe used for Debayering, if any:
if (capdev->convframe) {
if (capdev->convframe->image) free(capdev->convframe->image);
free(capdev->convframe);
capdev->convframe = NULL;
}
// Release current frame buffer, if any remaining:
if (capdev->current_frame) free(capdev->current_frame);
capdev->current_frame = NULL;
// No frame ready anymore:
capdev->frame_ready = 0;
if (PsychPrefStateGet_Verbosity() > 2) {
// Output count of dropped frames:
if ((dropped = capdev->nr_droppedframes) > 0) {
printf("PTB-INFO: Video capture dropped %i frames on device %i to keep capture running in sync with realtime.\n", dropped, capturehandle);
}
// Output count of corrupt frames:
if (capdev->corrupt_count > 0) {
printf("PTB-WARNING: Video capture detected %i corrupt frames on device %i since opening the capture device.\n", capdev->corrupt_count, capturehandle);
}
printf("PTB-INFO: Total number of captured frames since this camera %i was opened: %i\n", capturehandle, capdev->framecounter);
if (capdev->nrframes > 0) capdev->avg_decompresstime/= (double) capdev->nrframes;
printf("PTB-INFO: Average time spent %s was %lf milliseconds.\n", ((capdev->recordingflags & 16) ? "in video processing thread" : "waiting/polling for new frames"), capdev->avg_decompresstime * 1000.0f);
if (capdev->nrgfxframes > 0) capdev->avg_gfxtime/= (double) capdev->nrgfxframes;
printf("PTB-INFO: Average time spent in GetCapturedImage (intensity calculation and Video->OpenGL texture conversion) was %lf milliseconds.\n", capdev->avg_gfxtime * 1000.0f);
}
}
}
// Reset framecounters and statistics:
capdev->nrframes = 0;
capdev->avg_decompresstime = 0;
capdev->nrgfxframes = 0;
capdev->avg_gfxtime = 0;
// Return either the real capture framerate (at start of capture) or count of dropped frames - at end of capture.
return((capturerate!=0) ? (int) (capdev->fps + 0.5) : dropped);
}
/*
* PsychGetTextureFromCapture() -- Create an OpenGL texturemap from a specific videoframe from given capture object.
*
* win = Window pointer of onscreen window for which a OpenGL texture should be created.
* capturehandle = Handle to the capture object.
* checkForImage = >0 == Just check if new image available, 0 == really retrieve the image, blocking if necessary.
* 2 == Check for new image, block inside this function (if possible) if no image available.
*
* timeindex = This parameter is currently ignored and reserved for future use.
* out_texture = Pointer to the Psychtoolbox texture-record where the new texture should be stored.
* presentation_timestamp = A ptr to a double variable, where the presentation timestamp of the returned frame should be stored.
* summed_intensity = An optional ptr to a double variable. If non-NULL, then the average of intensities over all channels and pixels is calculated and returned.
* outrawbuffer = An optional ptr to a memory buffer of sufficient size. If non-NULL, the buffer will be filled with the captured raw image data, e.g., for use inside Matlab or whatever...
* Returns Number of pending or dropped frames after fetch on success (>=0), -1 if no new image available yet, -2 if no new image available and there won't be any in future.
*/
int PsychDCGetTextureFromCapture(PsychWindowRecordType *win, int capturehandle, int checkForImage, double timeindex,
PsychWindowRecordType *out_texture, double *presentation_timestamp, double* summed_intensity, rawcapimgdata* outrawbuffer)
{
int w, h;
psych_uint64 intensity = 0;
unsigned int count, i;
unsigned char* pixptr;
psych_uint16* pixptrs;
psych_bool frame_ready;
double tstart, tend;
dc1394error_t error;
int rc;
int nrdropped = 0;
unsigned char* input_image = NULL;
(void) timeindex;
// Retrieve device record for handle:
PsychVidcapRecordType* capdev = PsychGetVidcapRecord(capturehandle);
// Compute width and height for later creation of textures etc. Need to do this here,
// so we can return the values for raw data retrieval:
w=capdev->width;
h=capdev->height;
// If a outrawbuffer struct is provided, we fill it with info needed to allocate a
// sufficient memory buffer for returned raw image data later on:
if (outrawbuffer) {
outrawbuffer->w = w;
outrawbuffer->h = h;
outrawbuffer->depth = ((capdev->actuallayers == 3) ? 3 : 1);
outrawbuffer->bitdepth = (capdev->bitdepth > 8) ? 16 : 8;
}
int waitforframe = (checkForImage > 1) ? 1:0; // Blocking wait for new image requested?
// A mode 4 means "no op" with the libdc capture engine.
if (checkForImage == 4) return(0);
// Take start timestamp for timing stats:
PsychGetAdjustedPrecisionTimerSeconds(&tstart);
// Should we just check for new image?
if (checkForImage) {
// Synchronous frame fetch from masterthread?
if (!(capdev->recordingflags & 16)) {
// Capture handled by masterthread.
// Target framecount for end of capture/recording reached?
if (capdev->framecounter >= capdev->stopAtFramecount) {
// Yes: We will never get a new frame, so tell usercode:
return(-2);
}
// Reset current dropped count to zero:
capdev->current_dropped = 0;
// Grabber active: Polling mode or wait for new frame mode?
if (waitforframe) {
// Check for image in blocking mode: We actually try to capture a frame in
// blocking mode, so we will wait here until a new frame arrives.
error = dc1394_capture_dequeue(capdev->camera, DC1394_CAPTURE_POLICY_WAIT, &(capdev->frame));
if (error == DC1394_SUCCESS) {
// Ok, new frame ready and dequeued from DMA ringbuffer. We'll return it on next non-poll invocation.
capdev->frame_ready = 1;
}
else {
// Blocking wait failed! Somethings seriously wrong:
PsychErrorExitMsg(PsychError_system, "Blocking wait for new frame failed!!!");
}
}
else {
// Check for image in polling mode: We capture in non-blocking mode:
if (dc1394_capture_dequeue(capdev->camera, DC1394_CAPTURE_POLICY_POLL, &(capdev->frame)) == DC1394_SUCCESS) {
// Ok, call succeeded. If the 'frame' pointer is non-NULL then there's a new frame ready and dequeued from DMA
// ringbuffer. We'll return it on next non-poll invocation. Otherwise no new video data ready yet:
capdev->frame_ready = (capdev->frame != NULL) ? 1 : 0;
}
else {
// Polling wait failed for some reason...
PsychErrorExitMsg(PsychError_system, "Polling for new video frame failed!!!");
}
}
if (capdev->frame_ready) {
// Store count of currently queued frames (in addition to the one just fetched).
// This is an indication of how well the users script is keeping up with the video stream,
// technically the number of frames that would need to be dropped to keep in sync with the
// stream.
capdev->current_dropped = (int) capdev->frame->frames_behind;
// Ok, at least one new frame ready. If more than one frame has queued up and
// we are in 'dropframes' mode, ie. we should always deliver the most recent available
// frame, then we quickly fetch & discard all queued frames except the last one.
while((capdev->dropframes) && ((int) capdev->frame->frames_behind > 0)) {
// We just poll - fetch the frames. As we know there are some queued frames, it
// doesn't matter if we poll or block, but polling sounds like a bit less overhead
// at the OS level:
// Update the master framecounter for this camera and the new fetched frame:
PsychDCUpdateCameraFramecounter(capdev);
// First enqueue the recently dequeued buffer...
if (dc1394_capture_enqueue((capdev->camera), (capdev->frame)) != DC1394_SUCCESS) {
PsychErrorExitMsg(PsychError_system, "Requeuing of discarded video frame failed while dropping frames (dropframes=1)!!!");
}
// Then fetch the next one:
if (dc1394_capture_dequeue(capdev->camera, DC1394_CAPTURE_POLICY_POLL, &(capdev->frame)) != DC1394_SUCCESS || capdev->frame == NULL) {
// Polling failed for some reason...
PsychErrorExitMsg(PsychError_system, "Polling for new video frame failed while dropping frames (dropframes=1)!!!");
}
}
// Update stats for decompression:
PsychGetAdjustedPrecisionTimerSeconds(&tend);
// Increase counter of decompressed frames:
capdev->nrframes++;
// Update the master framecounter for this camera and the new fetched frame:
PsychDCUpdateCameraFramecounter(capdev);
// Update count of frames still pending in DMA queue:
capdev->nrframes_pending = (unsigned int) capdev->frame->frames_behind;
// Update avg. decompress time:
capdev->avg_decompresstime+=(tend - tstart);
// Update and assign capture timestamp capdev->current_pts for dequeued frame:
capdev->current_pts = 0;
PsychDCUpdateCameraFrameTimestamp(capdev);
}
// Assign final ready state:
frame_ready = capdev->frame_ready;
capdev->pulled_pts = capdev->current_pts;
capdev->pulled_dropped = capdev->current_dropped;
capdev->pulled_framecounter = capdev->framecounter;
}
else {
// Capture and recording handled on recorderThread.
// No actual return of video data to usercode wanted? If so, skip all further processing and report "no new frames yet":
if (capdev->recordingflags & 4) return((checkForImage) ? -1 : FALSE);
// Low-Latency fetch requested?
if (capdev->dropframes) {
// Check what the recorderThread has for us:
PsychLockMutex(&capdev->mutex);
// Loop as long as no new frame is ready...
while (!capdev->frame_ready) {
// Target framecount for end of capture/recording reached?
if (capdev->framecounter >= capdev->stopAtFramecount) {
// Yes: We will never get a new frame, so tell usercode:
PsychUnlockMutex(&capdev->mutex);
return(-2);
}
// ...unless this is a polling request, in which case we immediately give up:
if (!waitforframe) break;
// ... blocking wait. Release mutex and wait for new frame signal from recorderThread, with a timeout of 5 secs:
if ((rc = PsychTimedWaitCondition(&(capdev->condition), &(capdev->mutex), 5))) {
// Failed:
printf("PTB-ERROR: Waiting on video recorder thread to deliver new video frame failed [%s]. Aborting wait.\n", strerror(rc));
break;
}
}
// Do we finally have a frame?
frame_ready = capdev->frame_ready;
if (capdev->frame_ready) {
// Yes! Get a hand on the current video image buffer and timestamp:
capdev->pulled_frame = capdev->current_frame;
capdev->current_frame = NULL;
capdev->pulled_pts = capdev->current_pts;
capdev->pulled_dropped = capdev->current_dropped;
capdev->current_dropped = 0;
capdev->frame_ready = 0;
capdev->pulled_framecounter = capdev->framecounter;
}
PsychUnlockMutex(&capdev->mutex);
}
else {
// Pulling from GStreamer requested:
if (capdev->moviehandle < 0) {
PsychErrorExitMsg(PsychError_system, "Tried to pull captured video frame from ptbvideosink, but GStreamer based video processing not active?!?");
}
// Check what the recorderThread has for us:
PsychLockMutex(&capdev->mutex);
// Loop as long as no new frame is ready...
while (capdev->framecounter <= capdev->pulled_framecounter) {
// Target framecount for end of capture/recording on our side of the pipeline reached?
if (capdev->pulled_framecounter >= capdev->stopAtFramecount) {
// Yes: We will never get a new frame, so tell usercode:
PsychUnlockMutex(&capdev->mutex);
return(-2);
}
// ...unless this is a polling request, in which case we immediately give up:
if (!waitforframe) break;
// ... blocking wait. Release mutex and wait for new frame signal from recorderThread, with a timeout of 5 secs:
if ((rc = PsychTimedWaitCondition(&(capdev->condition), &(capdev->mutex), 5))) {
// Failed:
printf("PTB-ERROR: Waiting on video processing thread to deliver new video frame in ptbvideosink failed [%s]. Aborting wait.\n", strerror(rc));
break;
}
}
// Do we finally have a frame?
frame_ready = (capdev->framecounter > capdev->pulled_framecounter) ? TRUE : FALSE;
if (frame_ready) {
// This is the best approximation we can make: The difference between what the recorderThread
// pushed into the GStreamer pipeline and what we've so far pulled out:
capdev->pulled_dropped = capdev->framecounter - capdev->pulled_framecounter -1;
}
else {
// Unknown - Set to zero.
capdev->pulled_dropped = 0;
}
// No need for capdev lock from here on:
PsychUnlockMutex(&capdev->mutex);
if (frame_ready) {
unsigned int twidth, theight, numChannels, bitdepth;
// Yes! Get a hand on the current video image buffer and timestamp:
capdev->pulled_frame = PsychMovieCopyPulledPipelineBuffer(capdev->moviehandle, &twidth, &theight, &numChannels, &bitdepth, &(capdev->pulled_pts));
// Increase counter of pulled frames: This is the framecounter value corresponding to the retrieved frame:
capdev->pulled_framecounter++;
// Sanity checks:
// Validate number of color channels and bits per channel values for a match:
if (numChannels != (unsigned int) capdev->actuallayers || bitdepth != ((capdev->bitdepth > 8) ? 16 : 8)) {
printf("PTB-ERROR: Mismatch between number of color channels %i or bpc %i of captured video frame and number of channels %i or bpc %i of video capture target buffer!\n",
capdev->actuallayers, ((capdev->bitdepth > 8) ? 16 : 8), numChannels, bitdepth);
free(capdev->pulled_frame);
return(-2);
}
// Dimensions match?
if (twidth != (unsigned int) capdev->width || theight > (unsigned int) capdev->height) {
printf("PTB-ERROR: Mismatch between size of captured video frame %i x %i and size of video capture target buffer %i x %i !\n", capdev->width, capdev->height, twidth, theight);
free(capdev->pulled_frame);
return(-2);
}
// Ok, we're done. All pulled_xxx settings updated, our own copy of the video date in pulled_frame,
// pulled_framecounter up to date, frame_ready flag up to date.
}
}
}
// Return availability status: 0 = new frame ready for retrieval. -1 = No new frame ready yet.
return((frame_ready) ? 0 : -1);
}
// This point is only reached if checkForImage == FALSE, which only happens
// if a new frame is available in our buffer:
// Presentation timestamp requested?
if (presentation_timestamp) {
// Return it:
*presentation_timestamp = capdev->pulled_pts;
}
// Synchronous texture fetch: Copy content of capture buffer into a texture:
// =========================================================================
// Synchronous frame fetch on masterthread?
if (!(capdev->recordingflags & 16)) {
// Yes. Do pre-processing of frame:
input_image = PsychDCPreprocessFrame(capdev);
if (NULL == input_image) PsychErrorExitMsg(PsychError_system, "Bayer filtering or color space conversion of video frame failed.");
}
else {
// No. Already pre-processed in recorderThread, just assign:
input_image = capdev->pulled_frame;
}
// Only setup if really a texture is requested (non-benchmarking mode):
if (out_texture) {
// Activate OpenGL context of target window:
PsychSetGLContext(win);
#if PSYCH_SYSTEM == PSYCH_OSX
// Explicitely disable Apple's Client storage extensions. For now they are not really useful to us.
glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
#endif
PsychMakeRect(out_texture->rect, 0, 0, w, h);
// Set texture orientation as if it were an inverted Offscreen window: Upside-down.
out_texture->textureOrientation = 3;
// Setup a pointer to our buffer as texture data pointer: Setting memsize to zero
// prevents unwanted free() operation in PsychDeleteTexture...
out_texture->textureMemorySizeBytes = 0;
// Set texture depth: Could be 8, 16, 24 or 32 bpp for 1, 2, 3 or 4 layer textures:
out_texture->depth = capdev->reqlayers * 8;
// This will retrieve an OpenGL compatible pointer to the pixel data and assign it to our texmemptr:
out_texture->textureMemory = (GLuint*) input_image;
// Is this a > 8 bpc image format? If not, we ain't nothing more to prepare.
// If yes, we need to use a high precision floating point texture to represent
// the > 8 bpc image payload without loss of image information:
if (capdev->bitdepth > 8) {
// highbitthreshold: If the net bpc value is greater than this, then use 32bpc floats
// instead of 16 bpc half-floats, because 16 bpc would not be sufficient to represent
// more than highbitthreshold bits faithfully:
const int highbitthreshold = 11;
// 9 - 16 bpc color/luminance resolution:
out_texture->depth = capdev->reqlayers * ((capdev->bitdepth > highbitthreshold) ? 32 : 16);
if (capdev->reqlayers == 1) {
// 1 layer Luminance:
out_texture->textureinternalformat = (capdev->bitdepth > highbitthreshold) ? GL_LUMINANCE_FLOAT32_APPLE : GL_LUMINANCE_FLOAT16_APPLE;
out_texture->textureexternalformat = GL_LUMINANCE;
// Override for missing floating point texture support: Try to use 16 bit fixed point signed normalized textures [-1.0 ; 1.0] resolved at 15 bits:
if (!(win->gfxcaps & kPsychGfxCapFPTex16)) out_texture->textureinternalformat = GL_LUMINANCE16_SNORM;
}
else {
// 3 layer RGB:
out_texture->textureinternalformat = (capdev->bitdepth > highbitthreshold) ? GL_RGB_FLOAT32_APPLE : GL_RGB_FLOAT16_APPLE;
out_texture->textureexternalformat = GL_RGB;
// Override for missing floating point texture support: Try to use 16 bit fixed point signed normalized textures [-1.0 ; 1.0] resolved at 15 bits:
if (!(win->gfxcaps & kPsychGfxCapFPTex16)) out_texture->textureinternalformat = GL_RGB16_SNORM;
}
// External datatype is 16 bit unsigned integer, each color component encoded in a 16 bit value:
out_texture->textureexternaltype = GL_UNSIGNED_SHORT;
// Scale input data, so highest significant bit of payload is in bit 16:
glPixelTransferi(GL_RED_SCALE, 1 << (16 - capdev->bitdepth));
glPixelTransferi(GL_GREEN_SCALE, 1 << (16 - capdev->bitdepth));
glPixelTransferi(GL_BLUE_SCALE, 1 << (16 - capdev->bitdepth));
// Let PsychCreateTexture() do the rest of the job of creating, setting up and
// filling an OpenGL texture with content:
PsychCreateTexture(out_texture);
// Undo scaling:
glPixelTransferi(GL_RED_SCALE, 1);
glPixelTransferi(GL_GREEN_SCALE, 1);
glPixelTransferi(GL_BLUE_SCALE, 1);
}
else {
// Let PsychCreateTexture() do the rest of the job of creating, setting up and
// filling an OpenGL texture with content:
PsychCreateTexture(out_texture);
}
// Undo hack from above after texture creation: Now we need the real width of the
// texture for proper texture coordinate assignments in drawing code et al.
PsychMakeRect(out_texture->rect, 0, 0, w, h);
// Ready to use the texture...
}
// Sum of pixel intensities requested? 8 bpc?
if (summed_intensity && (capdev->bitdepth <= 8)) {
pixptr = (unsigned char*) input_image;
count = (w*h*((capdev->actuallayers == 3) ? 3 : 1));
for (i=0; i<count; i++) intensity+=(psych_uint64) pixptr[i];
*summed_intensity = ((double) intensity) / w / h / ((capdev->actuallayers == 3) ? 3 : 1) / 255;
}
// Sum of pixel intensities requested? 16 bpc?
if (summed_intensity && (capdev->bitdepth > 8)) {
pixptrs = (psych_uint16*) input_image;
count = (w*h*((capdev->actuallayers == 3) ? 3 : 1));
for (i=0; i<count; i++) intensity+=(psych_uint64) pixptrs[i];
*summed_intensity = ((double) intensity) / w / h / ((capdev->actuallayers == 3) ? 3 : 1) / ((1 << (capdev->bitdepth)) - 1);
}
// Raw data requested?
if (outrawbuffer) {
// Copy it out:
outrawbuffer->w = w;
outrawbuffer->h = h;
outrawbuffer->depth = ((capdev->actuallayers == 3) ? 3 : 1);
outrawbuffer->bitdepth = (capdev->bitdepth > 8) ? 16 : 8;
count = (w * h * outrawbuffer->depth * (outrawbuffer->bitdepth / 8));
// True bitdepth in the 9 to 15 bpc range?
if (capdev->bitdepth > 8 && capdev->bitdepth < 16) {
// Yes. Need to bit-shift, so the most significant bit of the video data,
// gets placed in the 16th bit of the 16 bit word. This to make sure the
// "dead bits" for bpc < 16 are the least significant bits and they are
// all zeros. This makes sure that black is always all-zero and white is
// at least close to 0xffff - minus the all-zero undefined lsb bits:
psych_uint16 *frameinwords = (psych_uint16*) input_image;
psych_uint16 *frameoutwords = (psych_uint16*) outrawbuffer->data;
count /= 2; // Half as many words as bytes.
for (i = 0; i < count; i++) *(frameoutwords++) = *(frameinwords++) << (16 - capdev->bitdepth);
}
else {
// No, either 8 bpc or 16 bpc - A simple memcpy does the job efficiently:
memcpy(outrawbuffer->data, (const void*) input_image, count);
}
}
// Synchronous video recording on masterthread active?
if (capdev->recording_active && (capdev->moviehandle != -1) && !(capdev->recordingflags & 16)) {
// Yes. Push data to encoder now:
PsychDCPushFrameToMovie(capdev, (psych_uint16*) input_image, capdev->pulled_pts, TRUE);
}
// Synchronous operation?
if (!(capdev->recordingflags & 16)) {
// Yes: Release the capture buffer. Return it to the DMA ringbuffer pool:
if (dc1394_capture_enqueue((capdev->camera), (capdev->frame)) != DC1394_SUCCESS) {
PsychErrorExitMsg(PsychError_system, "Re-Enqueuing processed video frame failed.");
}
// Reset current drop count for this cycle:
capdev->current_dropped = 0;
}
// Release cached frame buffer, if any:
if (capdev->pulled_frame) free(capdev->pulled_frame);
capdev->pulled_frame = NULL;
// Update total count of dropped frames. Only makes sense if frame dropping for low latency
// frame fetch is enabled. Otherwise this would be just confusing:
capdev->nr_droppedframes += (capdev->dropframes) ? capdev->pulled_dropped : 0;
// Find number of dropped frames at time of return of this frame:
nrdropped = capdev->pulled_dropped;
// Timestamping:
PsychGetAdjustedPrecisionTimerSeconds(&tend);
// Increase counter of retrieved textures:
capdev->nrgfxframes++;
// Update average time spent in texture conversion:
capdev->avg_gfxtime+=(tend - tstart);
// We're successfully done! Return number of dropped (or pending in DMA ringbuffer) frames:
return(nrdropped);
}
/* Set capture device specific parameters:
* Currently, the named parameters are a subset of the parameters supported by the
* IIDC specification, mapped to more convenient names.
*
* Input: pname = Name string to specify the parameter.
* value = Either DBL_MAX to not set but only query the parameter, or some other
* value, that we try to set in the Firewire camera.
*
* Returns: Old value of the setting
*/
double PsychDCVideoCaptureSetParameter(int capturehandle, const char* pname, double value)
{
dc1394feature_t feature;
dc1394bool_t present;
dc1394error_t err;
unsigned int minval, maxval, intval, oldintval;
int i;
double oldvalue = DBL_MAX; // Initialize return value to the "unknown/unsupported" default.
psych_bool assigned = false;
// Retrieve device record for handle:
PsychVidcapRecordType* capdev = (capturehandle != -1) ? PsychGetVidcapRecord(capturehandle) : NULL;
oldintval = 0xFFFFFFFF;
// Round value to integer:
intval = (int) (value + 0.5);
// Get/Set debayering method for raw sensor data to RGB conversion:
if (strcmp(pname, "DebayerMethod")==0) {
oldvalue = ((capdev) ? capdev->debayer_method : global_debayer_method) - DC1394_BAYER_METHOD_MIN;
if (value != DBL_MAX) {
if (capdev) {
// For capture device:
capdev->debayer_method = intval + DC1394_BAYER_METHOD_MIN;
}
else {
// Globally for movie playback support:
global_debayer_method = intval + DC1394_BAYER_METHOD_MIN;
}
}
// Note: This only arrives at caller if capturehandle != -1
return(oldvalue);
}
// Get/Set debayering method for raw sensor data to RGB conversion:
if (strcmp(pname, "OverrideBayerPattern")==0) {
oldvalue = ((capdev) ? capdev->color_filter_override : global_color_filter) - DC1394_COLOR_FILTER_MIN;
if (value != DBL_MAX) {
if (capdev) {
// For capture device:
capdev->color_filter_override = intval + DC1394_COLOR_FILTER_MIN;
}
else {
// Globally for movie playback support:
global_color_filter = intval + DC1394_COLOR_FILTER_MIN;
}
}
// Note: This only arrives at caller if capturehandle != -1
return(oldvalue);
}
// A -1 capturehandle is currently not supported on this engine for any other parameter settings. No-Op return:
if (capdev == NULL) return(oldvalue);
// Basler SFF Smart Feature Framework feature requested?
if (strstr(pname, "Basler")) {
// Basler cam?
if (!strstr(capdev->camera->vendor, "Basler")) {
// Non Basler cam :( We have to give up for now...
PsychErrorExitMsg(PsychError_user, "Basler SFF feature function called on a non-Basler camera. Unsupported!");
}
// SFF supported?
err = dc1394_basler_sff_is_available(capdev->camera, &present);
if (err || !present) PsychErrorExitMsg(PsychError_user, "Basler SFF feature function called, but this Basler camera does not support SFF. Unsupported!");
// To use any SFF features, we must have the feature enabled:
err = dc1394_basler_sff_feature_is_enabled(capdev->camera, DC1394_BASLER_SFF_EXTENDED_DATA_STREAM, &present);
if (err) PsychErrorExitMsg(PsychError_system, "Basler SFF feature function called, but could not query status of basic SFF extended data stream feature enable.");
if (!present) {
err = dc1394_basler_sff_feature_enable(capdev->camera, DC1394_BASLER_SFF_EXTENDED_DATA_STREAM, DC1394_ON);
if (err) PsychErrorExitMsg(PsychError_system, "Basler SFF feature function called, but could not enable basic SFF extended data stream feature mode.");
// SFF features enabled. These only work in Format7 modes, because only Format7 allows augmenting video frames
// with the meta-data chunks in which SFF transports its info. Therefore imply preference of Format-7 capture,
// with proper warnings to user at runtime if that should fail.
capdev->specialFlags |= 1;
if (PsychPrefStateGet_Verbosity() > 2) printf("PTB-INFO: Basler SFF features enabled. Trying to use Format7 video capture modes to make this functional.\n");
if (PsychPrefStateGet_Verbosity() > 5) {
printf("PTB-INFO: The following SFF Smart-Features are supported by the library (but not neccessarily this camera:\n");
dc1394_basler_sff_feature_print_all (capdev->camera, stdout);
printf("\n");
}
}
}
// Check parameter name pname and call the appropriate subroutine:
// Basler frame counter: Counts number of captured frames by camera since power-up time:
if (strcmp(pname, "BaslerFrameCounterEnable")==0) {
// Query of cameras internal trigger counter requested. Trigger counters are special features,
// (so called "Smart Features" or "Advanced Features" in the IIDC spec) which are only available
// on selected cameras. We currently only know how to do this on Basler cameras.
err = dc1394_basler_sff_feature_is_available(capdev->camera, DC1394_BASLER_SFF_FRAME_COUNTER, &present);
if (err) PsychErrorExitMsg(PsychError_system, "Basler: Could not query presence of SFF frame counter.");
if (!present) PsychErrorExitMsg(PsychError_user, "Basler SFF frame counter not supported on this camera. Enable failed.");
err = dc1394_basler_sff_feature_is_enabled(capdev->camera, DC1394_BASLER_SFF_FRAME_COUNTER, &present);
if (err) PsychErrorExitMsg(PsychError_system, "Basler: Could not query status of SFF frame counter enable.");
if (!present) {
err = dc1394_basler_sff_feature_enable(capdev->camera, DC1394_BASLER_SFF_FRAME_COUNTER, DC1394_ON);
if (err) PsychErrorExitMsg(PsychError_system, "Basler: Could not enable SFF frame counter.");
}
// Mark SFF framecounter active:
capdev->specialFlags |= 2;
// Return success:
return(1);
}
// Basler cycle timestamp: Timestamps each frame at start of exposure with firewire bus cycle count:
if (strcmp(pname, "BaslerFrameTimestampEnable")==0) {
err = dc1394_basler_sff_feature_is_available(capdev->camera, DC1394_BASLER_SFF_CYCLE_TIME_STAMP, &present);
if (err) PsychErrorExitMsg(PsychError_system, "Basler: Could not query presence of SFF timestamp.");
if (!present) PsychErrorExitMsg(PsychError_user, "Basler SFF timestamp not supported on this camera. Enable failed.");
err = dc1394_basler_sff_feature_is_enabled(capdev->camera, DC1394_BASLER_SFF_CYCLE_TIME_STAMP, &present);
if (err) PsychErrorExitMsg(PsychError_system, "Basler: Could not query status of SFF timestamp enable.");
if (!present) {
err = dc1394_basler_sff_feature_enable(capdev->camera, DC1394_BASLER_SFF_CYCLE_TIME_STAMP, DC1394_ON);
if (err) PsychErrorExitMsg(PsychError_system, "Basler: Could not enable SFF timestamp.");
}
// Mark SFF timestamp active:
capdev->specialFlags |= 4;
// Return success:
return(1);
}
// Basler CRC checksums: The camera compute a CRC and tags the frame. We recompute the CRC on frame reception to find corrupt frames.
if (strcmp(pname, "BaslerChecksumEnable")==0) {
err = dc1394_basler_sff_feature_is_available(capdev->camera, DC1394_BASLER_SFF_CRC_CHECKSUM, &present);
if (err) PsychErrorExitMsg(PsychError_system, "Basler: Could not query presence of SFF checksum.");
if (!present) PsychErrorExitMsg(PsychError_user, "Basler SFF checksum not supported on this camera. Enable failed.");
err = dc1394_basler_sff_feature_is_enabled(capdev->camera, DC1394_BASLER_SFF_CRC_CHECKSUM, &present);
if (err) PsychErrorExitMsg(PsychError_system, "Basler: Could not query status of SFF checksum enable.");
if (!present) {
err = dc1394_basler_sff_feature_enable(capdev->camera, DC1394_BASLER_SFF_CRC_CHECKSUM, DC1394_ON);
if (err) PsychErrorExitMsg(PsychError_system, "Basler: Could not enable SFF checksum.");
}
// Mark SFF CRC active:
capdev->specialFlags |= 8;
// Return success:
return(1);
}
// Basler test image: The camera transmits a synthetic test image to check onboard electronics, bus and reception. Sensor and ADC are off.
/* This is not yet supported by libdc1394:
if (strcmp(pname, "BaslerTestImageEnable")==0) {
err = dc1394_basler_sff_feature_is_available(capdev->camera, DC1394_BASLER_SFF_TEST_IMAGES, &present);
if (err) PsychErrorExitMsg(PsychError_system, "Basler: Could not query presence of SFF test image.");
if (!present) PsychErrorExitMsg(PsychError_user, "Basler SFF test image not supported on this camera. Enable failed.");
err = dc1394_basler_sff_feature_is_enabled(capdev->camera, DC1394_BASLER_SFF_TEST_IMAGES, &present);
if (err) PsychErrorExitMsg(PsychError_system, "Basler: Could not query status of SFF test image enable.");
if (!present) {
err = dc1394_basler_sff_feature_enable(capdev->camera, DC1394_BASLER_SFF_TEST_IMAGES, DC1394_ON);
if (err) PsychErrorExitMsg(PsychError_system, "Basler: Could not enable SFF test image.");
}
// Return success:
return(1);
}
*/
// Set a new target movie name for video recordings:
if (strstr(pname, "SetNewMoviename=")) {
// Find start of movie namestring and assign to pname:
pname = strstr(pname, "=");
pname++;
// Child protection:
if (!capdev->recording_active) {
if (PsychPrefStateGet_Verbosity() > 1) {
printf("PTB-WARNING: Tried to change name of target movie file on device %i, but recording not enabled on that device! Ignored.\n", capturehandle);
}
return(-2);
}
// Can't reassign new codec without reopening the device:
if (strstr(pname, ":CodecType")) {
*(strstr(pname, ":CodecType")) = 0;
if (PsychPrefStateGet_Verbosity() > 1) {
printf("PTB-WARNING: Tried to change recording codec on device %i, but this isn't possible without reopening the device. Ignored.\n", capturehandle);
}
}
// Can't reassign new codec without reopening the device:
if (strstr(pname, ":CodecSettings=")) {
*(strstr(pname, ":CodecSettings=")) = 0;
if (PsychPrefStateGet_Verbosity() > 1) {
printf("PTB-WARNING: Tried to change recording codec settings on device %i, but this isn't possible without reopening the device. Ignored.\n", capturehandle);
}
}
// Release old movie name:
if (capdev->targetmoviefilename) free(capdev->targetmoviefilename);
capdev->targetmoviefilename = NULL;
// Assign new movie name:
capdev->targetmoviefilename = strdup(pname);
if (PsychPrefStateGet_Verbosity() > 2) {
printf("PTB-INFO: Changed name of movie file for recording on device %i to '%s'.\n", capturehandle, pname);
}
return(0);
}
// Set an optional GStreamer pipeline processing string:
if (strstr(pname, "SetGStreamerProcessingPipeline=")) {
// Find start of string and assign to pname:
pname = strstr(pname, "=");
pname++;
// Release old string, if any:
if (capdev->processingString) free(capdev->processingString);
capdev->processingString = NULL;
// Assign new string:
capdev->processingString = strdup(pname);
if (PsychPrefStateGet_Verbosity() > 2) {
printf("PTB-INFO: Assigned GStreamer pipeline spec video processing string for device %i as '%s'.\n", capturehandle, pname);
}
return(0);
}
// Load a 2D marker tracking plugin and initialize it:
if (strstr(pname, "LoadMarkerTrackingPlugin=")) {
// Find start of string and assign to pname:
pname = strstr(pname, "=");
pname++;
// Load shared library which implements the marker tracker plugin from given filename:
if ((markerTrackerPlugin_libraryhandle = dlopen(pname, RTLD_NOW | RTLD_LOCAL)) == NULL) {
printf("PTB-ERROR: Failed to load markertracker plugin for device %i under name '%s'. OS reports: '%s'\n", capturehandle, pname, dlerror());
PsychErrorExitMsg(PsychError_user, "Loading markertracker plugin failed!");
}
// Library loaded and linked. Get function handles for functions we do care about:
TrackerPlugin_initialize = (void* (*)()) dlsym(markerTrackerPlugin_libraryhandle, "TrackerPlugin_initialize");
TrackerPlugin_shutdown = (bool (*)(void*)) dlsym(markerTrackerPlugin_libraryhandle, "TrackerPlugin_shutdown");
TrackerPlugin_processFrame = (bool (*)(void*, unsigned long* source_ptr, int imgwidth, int imgheight, int xmin, int ymin, unsigned int timeidx, double capturetimestamp, unsigned int absolute_frameindex)) dlsym(markerTrackerPlugin_libraryhandle, "TrackerPlugin_processFrame");
TrackerPlugin_processPluginDataBuffer = (bool (*)(void*, unsigned long* buffer, int size)) dlsym(markerTrackerPlugin_libraryhandle, "TrackerPlugin_processPluginDataBuffer");
// Binding successfull?
if (!TrackerPlugin_initialize || !TrackerPlugin_shutdown || !TrackerPlugin_processFrame || !TrackerPlugin_processPluginDataBuffer) {
dlclose(markerTrackerPlugin_libraryhandle);
printf("PTB-ERROR: Failed to link/bind markertracker plugin for device %i under name '%s'. Could not resolve at least one entry point.\n", capturehandle, pname);
PsychErrorExitMsg(PsychError_user, "Linking/Binding markertracker plugin failed!");
}
// Call init method. This will return a unique handle for this instance of the plugin for this capture device:
if (NULL == (capdev->markerTrackerPlugin = (*TrackerPlugin_initialize)())) {
dlclose(markerTrackerPlugin_libraryhandle);
printf("PTB-ERROR: Failed to initialize markertracker plugin for device %i under name '%s'.\n", capturehandle, pname);
PsychErrorExitMsg(PsychError_user, "Initializing markertracker plugin failed!");
}
if (PsychPrefStateGet_Verbosity() > 2) {
printf("PTB-INFO: Markertracker plugin loaded and initialized for device %i as '%s'.\n", capturehandle, pname);
}
return(0);
}
// Send command to a 2D marker tracking plugin:
if (strstr(pname, "SendCommandToMarkerTrackingPlugin=")) {
unsigned long buffer[1024];
// Find start of string and assign to pname:
pname = strstr(pname, "=");
pname++;
if (capdev->markerTrackerPlugin) {
// Yes! Execute: Plugin reads from (unsigned long*) input_image and could theoretically write
// back into (unsigned char*) input_image to modify its content. In practice, it doesn't do that,
// so this argument is provided as NULL-Ptr:
//
// Need funky data wrangling here, as plugin expects buffer of unsigned long's, and a size spec
// in units of unsigned long's:
if (strlen(pname) + 1 > sizeof(buffer)) PsychErrorExitMsg(PsychError_user, "Tried to send too much data to a markertracker plugin for this capture device!");
memcpy(&(buffer[0]), pname, strlen(pname) + 1);
if (!(*TrackerPlugin_processPluginDataBuffer)(capdev->markerTrackerPlugin, &(buffer[0]), (strlen(pname) / sizeof(unsigned long)) + 1 )) {
printf("PTB-ERROR: SendCommandToMarkerTrackingPlugin: Failed to send command to markertracker plugin for device %i! Command was '%s'.\n", capturehandle, pname);
PsychErrorExitMsg(PsychError_user, "Failed to send data to markertracker plugin for this capture device!");
}
}
else {
// No plugin loaded?!?
printf("PTB-ERROR: SendCommandToMarkerTrackingPlugin: Tried to send command to non-existent markertracker plugin for device %i! Command was '%s'.\n", capturehandle, pname);
PsychErrorExitMsg(PsychError_user, "Tried to send data to a markertracker plugin for this capture device, but there isn't any plugin loaded!");
}
return(0);
}
if (strcmp(pname, "PrintParameters")==0) {
dc1394featureset_t camfeatures;
// Special command: List and print all features...
printf("PTB-INFO: The camera provides the following information and featureset:\n");
if (dc1394_camera_print_info(capdev->camera, stdout) !=DC1394_SUCCESS) {
printf("PTB-WARNING: Unable to query general information about camera.\n");
}
if (dc1394_feature_get_all(capdev->camera, &camfeatures) != DC1394_SUCCESS) {
printf("PTB-WARNING: Unable to query features of camera.\n");
}
else {
printf("\n");
dc1394_feature_print_all(&camfeatures, stdout);
}
return(0);
}
// Return current framerate:
if (strcmp(pname, "GetFramerate")==0) {
PsychCopyOutDoubleArg(1, FALSE, capdev->fps);
return(0);
}
// Return current ROI of camera, as requested (and potentially modified during
// PsychOpenCaptureDevice(). This is a read-only parameter, as the ROI can
// only be set during Screen('OpenVideoCapture').
if (strcmp(pname, "GetROI")==0) {
PsychCopyOutRectArg(1, FALSE, capdev->roirect);
return(0);
}
// Return vendor name string:
if (strcmp(pname, "GetVendorname")==0) {
PsychCopyOutCharArg(1, FALSE, capdev->camera->vendor);
return(0);
}
// Return model name string:
if (strcmp(pname, "GetModelname")==0) {
PsychCopyOutCharArg(1, FALSE, capdev->camera->model);
return(0);
}
// Return current firewire bus bandwidth usage of camera::
if (strcmp(pname, "GetBandwidthUsage")==0) {
dc1394_video_get_bandwidth_usage(capdev->camera, (uint32_t *) &intval);
PsychCopyOutDoubleArg(1, FALSE, ((double) intval) / 4915);
return(0);
}
// Get/Set option to prefer Format7 modes, even if a non-Format7 mode would do:
if (strcmp(pname, "PreferFormat7Modes")==0) {
oldvalue = (double) (capdev->specialFlags & 1) ? 1 : 0;
if (value != DBL_MAX) {
if (value > 0) {
capdev->specialFlags |= 1;
}
else {
capdev->specialFlags &= ~1;
}
}
return(oldvalue);
}
// Get current count of corrupted frames:
if (strcmp(pname, "GetCorruptFramecount")==0) {
PsychLockMutex(&capdev->mutex);
PsychCopyOutDoubleArg(1, FALSE, (double) capdev->corrupt_count);
PsychUnlockMutex(&capdev->mutex);
return(0);
}
// Get current framecount of already captured and dequeued frames:
if (strcmp(pname, "GetCurrentFramecount")==0) {
PsychLockMutex(&capdev->mutex);
PsychCopyOutDoubleArg(1, FALSE, (double) capdev->framecounter);
PsychUnlockMutex(&capdev->mutex);
return(0);
}
// Get capture framecount of last video frame which was successfully retrieved via Screen('GetCapturedImage'):
if (strcmp(pname, "GetFetchedFramecount")==0) {
PsychCopyOutDoubleArg(1, FALSE, (double) capdev->pulled_framecounter);
return(0);
}
// Get possible framecount, including frames pending in DMA queue:
if (strcmp(pname, "GetFutureMaxFramecount")==0) {
PsychLockMutex(&capdev->mutex);
PsychCopyOutDoubleArg(1, FALSE, (double) (capdev->framecounter + capdev->nrframes_pending));
PsychUnlockMutex(&capdev->mutex);
return(0);
}
// Get/Set target framecount for stop of capture of a camera:
if (strcmp(pname, "StopAtFramecount")==0) {
oldvalue = (double) capdev->stopAtFramecountLatch;
if (value != DBL_MAX) {
// Store new value in latch. It will be transferred at 'StartCapture' into
// the live stopAtFramecount variable:
capdev->stopAtFramecountLatch = (unsigned int) intval;
// Capture already started?
if (capdev->grabber_active) {
// Capture already running, so we want to dynamically change the limit.
// Latch the value into the live limit variable under lock protection:
PsychLockMutex(&capdev->mutex);
capdev->stopAtFramecount = capdev->stopAtFramecountLatch;
// If this is the sync master then its stopAtFramecount determines the target framecount
// for itself and all its slaves, e.g., for pushing dequeued frames to video recording:
if (capdev->syncmode & kPsychIsSyncMaster) {
// This is the sync-master. Its final framecount after it stopped capture determines
// how many frames its own recorderThread and all the slaves recorderThreads should
// dequeue and potentially push to the video recording pipeline.
//
// Set masters stopAtFramecount also for all slaves, unless the slaves already have a
// lower limit assigned manually or due to preceeding Screen('StopCapture'):
for (i = 0; i < PSYCH_MAX_CAPTUREDEVICES; i++) {
// Sync slave participating in this synced stop operation?
if ((vidcapRecordBANK[i].valid) && (i != capturehandle) && (vidcapRecordBANK[i].syncmode & kPsychIsSyncSlave)) {
// Yes. Assign stopAtFramecount:
PsychLockMutex(&vidcapRecordBANK[i].mutex);
if (vidcapRecordBANK[i].grabber_active && (vidcapRecordBANK[i].stopAtFramecount > capdev->stopAtFramecount)) vidcapRecordBANK[i].stopAtFramecount = capdev->stopAtFramecount;
if (!vidcapRecordBANK[i].grabber_active && (vidcapRecordBANK[i].stopAtFramecountLatch > capdev->stopAtFramecount)) vidcapRecordBANK[i].stopAtFramecountLatch = capdev->stopAtFramecount;
PsychUnlockMutex(&vidcapRecordBANK[i].mutex);
}
}
}
PsychUnlockMutex(&capdev->mutex);
if (PsychPrefStateGet_Verbosity() > 3) {
printf("PTB-INFO: Video %s on device %i will stop at target framecount %i.\n", (capdev->recording_active) ? "recording" : "capture", capturehandle, capdev->stopAtFramecount);
}
}
}
return(oldvalue);
}
// Get/Set special treatment mode for raw sensor data:
if (strcmp(pname, "DataConversionMode")==0) {
oldvalue = capdev->dataconversionmode;
if (value != DBL_MAX) {
capdev->dataconversionmode = intval;
}
return(oldvalue);
}
// Get/Set synchronization mode for multi-camera operation:
if (strcmp(pname, "SyncMode")==0) {
oldvalue = capdev->syncmode;
if (value != DBL_MAX) {
// Sanity check syncmode spec:
// Free-running? That's always fine.
if (intval != 0) {
// Not free-running, but somehow synced:
if ((intval & kPsychIsSyncMaster) && (intval & kPsychIsSyncSlave)) {
// Master and slave at the same time? That's not possible.
PsychErrorExitMsg(PsychError_user, "Invalid syncmode provided: Camera can't be master and slave at the same time!");
}
if (!(intval & kPsychIsSyncMaster) && !(intval & kPsychIsSyncSlave)) {
// Neither master or slave? That's not possible.
PsychErrorExitMsg(PsychError_user, "Invalid syncmode provided: Camera must be either master or slave. Can't be none of both!");
}
// Must be either soft-, bus-, or hw- synced, but not none or multiple of all if it is a sync slave.
// Must be at least one of soft-, bus- or hw- synced if it is a sync master:
oldintval = intval & (kPsychIsSoftSynced | kPsychIsBusSynced | kPsychIsHwSynced);
if (((intval & kPsychIsSyncMaster) && !oldintval) || ((intval & kPsychIsSyncSlave) && (oldintval != kPsychIsSoftSynced) && (oldintval != kPsychIsBusSynced) && (oldintval != kPsychIsHwSynced))) {
PsychErrorExitMsg(PsychError_user, "Invalid syncmode provided: Missing sync strategy, either soft-, or bus-, or hw-synced!");
}
// If this is a cam that should take on the role of a hardware synced slave, check if
// this cam is actually capable of receiving hardware trigger signals:
if ((intval & kPsychIsHwSynced) && (intval & kPsychIsSyncSlave)) {
err = dc1394_feature_is_present(capdev->camera, DC1394_FEATURE_TRIGGER, &present);
if (err || !present) {
// Camera is not hw sync capable: Do not change the SyncMode but no-op. A successive
// query of 'SyncMode' would report the failure to change to hw-synced mode, so the
// calling usercode can search for alternatives:
if (PsychPrefStateGet_Verbosity() > 3) printf("PTB-INFO: No hardware trigger support on cam %i. Ignored hw-sync setting for slave cam.\n", capturehandle);
return(oldvalue);
}
}
}
// Assign new syncmode:
capdev->syncmode = (int) intval;
}
return(oldvalue);
}
// Set trigger mode:
// Note: Videolab used DC1394_TRIGGER_MODE_0 aka DC1394_TRIGGER_MODE_MIN aka 'TriggerMode' zero,
// so exposure duration is controlled by programmable shutter time, whereas start of exposure
// is controlled by trigger signal.
if (strcmp(pname, "TriggerMode")==0) {
dc1394trigger_mode_t mode;
// Double-Check the camera supports hw triggers:
err = dc1394_feature_is_present(capdev->camera, DC1394_FEATURE_TRIGGER, &present);
if (!err && present) {
err = dc1394_external_trigger_get_mode(capdev->camera, &mode);
}
else err = DC1394_FUNCTION_NOT_SUPPORTED;
if (err) {
if(PsychPrefStateGet_Verbosity()>1) printf("PTB-WARNING: Requested capture device setting %s not available on cam %i. Ignored.\n", pname, capturehandle);
return(oldvalue);
}
else {
oldvalue = (unsigned int) mode - (unsigned int) DC1394_TRIGGER_MODE_MIN;
if (value != DBL_MAX) {
mode = DC1394_TRIGGER_MODE_MIN + (dc1394trigger_mode_t) intval;
if (mode > DC1394_TRIGGER_MODE_MAX || mode < DC1394_TRIGGER_MODE_MIN) PsychErrorExitMsg(PsychError_user, "Invalid TriggerMode provided: Outside valid range!");
err = dc1394_external_trigger_set_mode(capdev->camera, mode);
if (err) PsychErrorExitMsg(PsychError_system, "Failed to set current trigger mode!");
}
return(oldvalue);
}
}
// Set trigger source:
// Note: Videolab used DC1394_TRIGGER_SOURCE_0 aka DC1394_TRIGGER_SOURCE_MIN aka TriggerSource zero.
// This source corresponds to port 0 == Pin 5 of the RJ-45 connector on a Basler camera.
// Source 1 = Port 1 = Pin 9, Source 2 = Port 2 = Pin 8, Source 3 = Port 3 = Pin 10.
if (strcmp(pname, "TriggerSource")==0) {
dc1394trigger_source_t source;
// Double-Check the camera supports hw triggers:
err = dc1394_feature_is_present(capdev->camera, DC1394_FEATURE_TRIGGER, &present);
if (!err && present) {
err = dc1394_external_trigger_get_source(capdev->camera, &source);
}
else err = DC1394_FUNCTION_NOT_SUPPORTED;
if (err) {
if(PsychPrefStateGet_Verbosity()>1) printf("PTB-WARNING: Requested capture device setting %s not available on cam %i. Ignored.\n", pname, capturehandle);
return(oldvalue);
}
else {
oldvalue = (unsigned int) source - (unsigned int) DC1394_TRIGGER_SOURCE_MIN;
if (value != DBL_MAX) {
source = DC1394_TRIGGER_SOURCE_MIN + (dc1394trigger_source_t) intval;
if (source > DC1394_TRIGGER_SOURCE_MAX || source < DC1394_TRIGGER_SOURCE_MIN) PsychErrorExitMsg(PsychError_user, "Invalid TriggerSource provided: Outside valid range!");
err = dc1394_external_trigger_set_source(capdev->camera, source);
if (err) PsychErrorExitMsg(PsychError_system, "Failed to set current trigger source!");
}
return(oldvalue);
}
}
// Retrieve list of supported trigger sources:
if (strcmp(pname, "GetTriggerSources")==0) {
dc1394trigger_sources_t sources;
double *outsources;
// Double-Check the camera supports hw triggers:
err = dc1394_feature_is_present(capdev->camera, DC1394_FEATURE_TRIGGER, &present);
if (!err && present) {
err = dc1394_external_trigger_get_supported_sources(capdev->camera, &sources);
}
else err = DC1394_FUNCTION_NOT_SUPPORTED;
if (err) {
if(PsychPrefStateGet_Verbosity()>1) printf("PTB-WARNING: Requested capture device setting %s not available on cam %i. Ignored.\n", pname, capturehandle);
return(oldvalue);
}
PsychAllocOutDoubleMatArg(1, FALSE, 1, sources.num, 0, &outsources);
for (i = 0; i < (int) sources.num; i++) *(outsources++) = (double) (sources.sources[i] - DC1394_TRIGGER_SOURCE_MIN);
return(0);
}
// Set trigger polarity:
// Note: Videolab used TriggerPolarity zero == DC1394_TRIGGER_ACTIVE_LOW == Trigger on falling edge.
if (strcmp(pname, "TriggerPolarity")==0) {
dc1394trigger_polarity_t polarity;
// Double-Check the camera supports hw triggers:
err = dc1394_feature_is_present(capdev->camera, DC1394_FEATURE_TRIGGER, &present);
if (!err && present) {
err = dc1394_external_trigger_has_polarity(capdev->camera, &present);
}
else err = DC1394_FUNCTION_NOT_SUPPORTED;
if (err || !present) {
if(PsychPrefStateGet_Verbosity()>1) printf("PTB-WARNING: Failed to query/set TriggerPolarity on camera %i. Unsupported feature. Ignored.\n", capturehandle);
return(oldvalue);
}
else {
err = dc1394_external_trigger_get_polarity(capdev->camera, &polarity);
if (err) PsychErrorExitMsg(PsychError_system, "Failed to query trigger polarity!");
oldvalue = (polarity == DC1394_TRIGGER_ACTIVE_HIGH) ? 1 : 0;
if (value != DBL_MAX) {
err = dc1394_external_trigger_set_polarity(capdev->camera, (intval > 0) ? DC1394_TRIGGER_ACTIVE_HIGH : DC1394_TRIGGER_ACTIVE_LOW);
if (err) PsychErrorExitMsg(PsychError_system, "Failed to set trigger polarity!");
}
return(oldvalue);
}
}
// Get/Set GPIO pins on camera, if any:
if (strstr(pname, "PIO")!=0) {
unsigned int pio;
err = dc1394_pio_get(capdev->camera, &pio);
if (err) return(DBL_MAX); // Query failed.
if (value != DBL_MAX) {
err = dc1394_pio_set(capdev->camera, (uint32_t) intval);
if (err) PsychErrorExitMsg(PsychError_system, "Failed to set new values on GPIO pins on camera!");
}
// Return old value:
return(pio);
}
// Get/Set if 1394B mode aka Firewire-800+ mode is active, or if legacy Firewire-400 mode is active on this camera:
if (strstr(pname, "1394BModeActive")!=0) {
dc1394operation_mode_t opmode;
err = dc1394_video_get_operation_mode(capdev->camera, &opmode);
if (err) return(0); // Query failed. Assume legacy mode is active.
if (value != DBL_MAX) {
err = dc1394_video_set_operation_mode(capdev->camera, (intval > 0) ? DC1394_OPERATION_MODE_1394B : DC1394_OPERATION_MODE_LEGACY);
if (err) PsychErrorExitMsg(PsychError_system, "Failed to switch between 1394A and 1394B operation mode!");
}
return((opmode == DC1394_OPERATION_MODE_1394B) ? 1 : 0);
}
// Get/Set firewire ISO bus speed:
if (strstr(pname, "ISOSpeed")!=0) {
dc1394speed_t isospeed;
err = dc1394_video_get_iso_speed(capdev->camera, &isospeed);
if (err) return(DBL_MAX); // Query failed.
if (value != DBL_MAX) {
err = dc1394_video_set_iso_speed(capdev->camera, ((int) log2((double) (intval / 100))) + DC1394_ISO_SPEED_100);
if (err) PsychErrorExitMsg(PsychError_system, "Failed to set new firewire ISO bus speed on camera!");
}
// Return old value: Offset 0 = 100 MBIT, 1 = 200, 2 = 400, ...
return((1 << (isospeed - DC1394_ISO_SPEED_100)) * 100);
}
// Get current count of firewire cycle timer and corresponding system time:
if (strstr(pname, "GetCycleTimer")!=0) {
dc1394basler_sff_cycle_time_stamp_t ct;
uint64_t systemtime;
double getsecstime;
err = dc1394_read_cycle_timer(capdev->camera, &(ct.cycle_time_stamp.unstructured.value), &systemtime);
if (err && (err != DC1394_FUNCTION_NOT_SUPPORTED)) PsychErrorExitMsg(PsychError_system, "Failed to query cycle timer!");
if (err == DC1394_FUNCTION_NOT_SUPPORTED) return(oldvalue);
// System time is CLOCK_REALTIME time in microseconds, so convert to GetSecs seconds:
getsecstime = PsychOSRealtimeToRefTime(((double) ((psych_uint64) systemtime)) / 1000000.0f);
// Copy out Firewire bus time converted into seconds:
PsychCopyOutDoubleArg(1, FALSE, PsychDCBusCycleTimeToSecs(ct.cycle_time_stamp.unstructured.value));
// Copy out GetSecs timestamp:
PsychCopyOutDoubleArg(2, FALSE, getsecstime);
// Copy out Firewire bus time in seconds:
PsychCopyOutDoubleArg(3, FALSE, (double) ct.cycle_time_stamp.structured.second_count);
// Copy out Firewire bus time in number of 125 usec iso cycles:
PsychCopyOutDoubleArg(4, FALSE, (double) ct.cycle_time_stamp.structured.cycle_count);
// Copy out Firewire bus time in clock ticks of the 24.576Mhz bus clock on top of seconds and iso cycles:
PsychCopyOutDoubleArg(5, FALSE, (double) ct.cycle_time_stamp.structured.cycle_offset);
return(0);
}
// Initiate reset of the firewire or USB bus to which a camera is connected:
if (strstr(pname, "ResetBus")!=0) {
err = dc1394_reset_bus(capdev->camera);
if (err) PsychErrorExitMsg(PsychError_system, "Failed to reset bus!");
return(0);
}
// Initiate reset of a camera: This is not a power-cycle.
if (strstr(pname, "ResetCamera")!=0) {
err = dc1394_camera_reset(capdev->camera);
if (err) PsychErrorExitMsg(PsychError_system, "Failed to reset camera!");
return(0);
}
if (strstr(pname, "Temperature")!=0) {
err = dc1394_feature_is_present(capdev->camera, DC1394_FEATURE_TEMPERATURE, &present);
if ((err == DC1394_SUCCESS) && present) {
// Retrieve and return current settings:
unsigned int target_temperature, temperature;
err = dc1394_feature_temperature_get_value(capdev->camera, &target_temperature, &temperature);
if (err) PsychErrorExitMsg(PsychError_system, "Failed to get target temperature and current temperature!");
PsychCopyOutDoubleArg(2, FALSE, temperature);
// Set new target temperature:
if (value != DBL_MAX) {
err = dc1394_feature_temperature_set_value(capdev->camera, intval);
if (err) PsychErrorExitMsg(PsychError_system, "Failed to set target temperature!");
}
return((double) target_temperature);
}
else {
// Feature unsupported:
return(oldvalue);
}
}
if (strstr(pname, "WhiteBalance")!=0) {
assigned = true;
err = dc1394_feature_is_present(capdev->camera, DC1394_FEATURE_WHITE_BALANCE, &present);
if ((err == DC1394_SUCCESS) && present) {
// Retrieve and return current settings:
unsigned int ub, vr;
err = dc1394_feature_whitebalance_get_value(capdev->camera, &ub, &vr);
if (err) PsychErrorExitMsg(PsychError_system, "Failed to get white balance U/B and V/R values!");
PsychCopyOutDoubleArg(2, FALSE, vr);
// Set new white balance values:
if (value != DBL_MAX) {
PsychCopyInIntegerArg(4, TRUE, (int*) &vr);
err = dc1394_feature_whitebalance_set_value(capdev->camera, intval, vr);
if (err) PsychErrorExitMsg(PsychError_system, "Failed to set white balance!");
}
return((double) ub);
}
else {
// Feature unsupported:
return(oldvalue);
}
}
if (strstr(pname, "WhiteShading")!=0) {
assigned = true;
err = dc1394_feature_is_present(capdev->camera, DC1394_FEATURE_WHITE_SHADING, &present);
if ((err == DC1394_SUCCESS) && present) {
// Retrieve and return current settings:
unsigned int rv, gv, bv;
err = dc1394_feature_whiteshading_get_value(capdev->camera, &rv, &gv, &bv);
if (err) PsychErrorExitMsg(PsychError_system, "Failed to get white shading R, G, B values!");
PsychCopyOutDoubleArg(2, FALSE, gv);
PsychCopyOutDoubleArg(2, FALSE, bv);
// Set new white shading values:
if (value != DBL_MAX) {
PsychCopyInIntegerArg(4, TRUE, (int*) &gv);
PsychCopyInIntegerArg(5, TRUE, (int*) &bv);
err = dc1394_feature_whiteshading_set_value(capdev->camera, intval, gv, bv);
if (err) PsychErrorExitMsg(PsychError_system, "Failed to set white shading!");
}
return((double) rv);
}
else {
// Feature unsupported:
return(oldvalue);
}
}
if (strstr(pname, "Brightness")!=0) {
assigned = true;
feature = DC1394_FEATURE_BRIGHTNESS;
}
if (strstr(pname, "Gain")!=0) {
assigned = true;
feature = DC1394_FEATURE_GAIN;
}
if (strstr(pname, "Exposure")!=0) {
assigned = true;
feature = DC1394_FEATURE_EXPOSURE;
}
if (strstr(pname, "Shutter")!=0) {
assigned = true;
feature = DC1394_FEATURE_SHUTTER;
}
if (strstr(pname, "Sharpness")!=0) {
assigned = true;
feature = DC1394_FEATURE_SHARPNESS;
}
if (strstr(pname, "Hue")!=0) {
assigned = true;
feature = DC1394_FEATURE_HUE;
}
if (strstr(pname, "Saturation")!=0) {
assigned = true;
feature = DC1394_FEATURE_SATURATION;
}
if (strstr(pname, "Gamma")!=0) {
assigned = true;
feature = DC1394_FEATURE_GAMMA;
}
if (strstr(pname, "Iris")!=0) {
assigned = true;
feature = DC1394_FEATURE_IRIS;
}
if (strstr(pname, "Focus")!=0) {
assigned = true;
feature = DC1394_FEATURE_FOCUS;
}
if (strstr(pname, "Zoom")!=0) {
assigned = true;
feature = DC1394_FEATURE_ZOOM;
}
if (strstr(pname, "Pan")!=0) {
assigned = true;
feature = DC1394_FEATURE_PAN;
}
if (strstr(pname, "Tilt")!=0) {
assigned = true;
feature = DC1394_FEATURE_TILT;
}
if (strstr(pname, "OpticalFilter")!=0) {
assigned = true;
feature = DC1394_FEATURE_OPTICAL_FILTER;
}
if (strstr(pname, "CaptureSize")!=0) {
assigned = true;
feature = DC1394_FEATURE_CAPTURE_SIZE;
}
if (strstr(pname, "CaptureQuality")!=0) {
assigned = true;
feature = DC1394_FEATURE_CAPTURE_QUALITY;
}
if (strstr(pname, "FrameRate")!=0) {
assigned = true;
feature = DC1394_FEATURE_FRAME_RATE;
}
if (strstr(pname, "TriggerDelay")!=0) {
assigned = true;
feature = DC1394_FEATURE_TRIGGER_DELAY;
}
// Check if feature is present on this camera:
if (dc1394_feature_is_present(capdev->camera, feature, &present)!=DC1394_SUCCESS) {
if(PsychPrefStateGet_Verbosity()>1) printf("PTB-WARNING: Failed to query presence of feature %s on camera %i! Ignored.\n", pname, capturehandle);
}
else if (present) {
// Feature is available:
// Retrieve current value:
if (dc1394_feature_get_value(capdev->camera, feature, &oldintval)!=DC1394_SUCCESS) {
if(PsychPrefStateGet_Verbosity()>1) printf("PTB-WARNING: Failed to query value of feature %s on camera %i! Ignored.\n", pname, capturehandle);
}
else {
// Do we want to set the value?
if (value != DBL_MAX) {
// Query allowed bounds for its value:
if (dc1394_feature_get_boundaries(capdev->camera, feature, &minval, &maxval)!=DC1394_SUCCESS) {
if(PsychPrefStateGet_Verbosity()>1) printf("PTB-WARNING: Failed to query valid value range for feature %s on camera %i! Ignored.\n", pname, capturehandle);
}
else {
// Sanity check against range:
if (intval < minval || intval > maxval) {
if(PsychPrefStateGet_Verbosity()>1) printf("PTB-WARNING: Requested setting %i for parameter %s not in allowed range (%i - %i) for camera %i. Ignored.\n",
intval, pname, minval, maxval, capturehandle);
}
else {
// Ok intval is valid for this feature: Can we manually set this feature?
// Switch feature to manual control mode:
if (dc1394_feature_set_mode(capdev->camera, feature, DC1394_FEATURE_MODE_MANUAL)!=DC1394_SUCCESS) {
if(PsychPrefStateGet_Verbosity()>1) printf("PTB-WARNING: Failed to set feature %s on camera %i to manual control! Ignored.\n", pname, capturehandle);
}
else {
// Ok, try to set the features new value:
if (dc1394_feature_set_value(capdev->camera, feature, intval)!=DC1394_SUCCESS) {
if(PsychPrefStateGet_Verbosity()>1) printf("PTB-WARNING: Failed to set value of feature %s on camera %i to %i! Ignored.\n", pname, capturehandle,
intval);
}
}
}
}
}
else {
// Don't want to set new value. Do we want to reset feature into auto-mode?
// Prefixing a parameter name with "Auto"
// does not switch the parameter into manual
// control mode + set its value, as normal,
// but it switches the parameter into automatic
// mode, if automatic mode is supported by the
// device.
if (strstr(pname, "Auto")!=0) {
// Switch to automatic control requested - Try it:
if (dc1394_feature_set_mode(capdev->camera, feature, DC1394_FEATURE_MODE_AUTO)!=DC1394_SUCCESS) {
if(PsychPrefStateGet_Verbosity()>1) printf("PTB-WARNING: Failed to set feature %s on camera %i to automatic control! Ignored.\n", pname, capturehandle);
}
}
}
}
}
else {
if(PsychPrefStateGet_Verbosity()>1) printf("PTB-WARNING: Requested capture device setting %s not available on cam %i. Ignored.\n", pname, capturehandle);
}
// Output a warning on unknown parameters:
if (!assigned) {
if(PsychPrefStateGet_Verbosity()>1) printf("PTB-WARNING: Screen('SetVideoCaptureParameter', ...) called with unknown parameter %s. Ignored...\n",
pname);
}
if (assigned && oldintval!=0xFFFFFFFF) oldvalue = (double) oldintval;
// Return the old value. Could be DBL_MAX if parameter was unknown or not accepted for some reason.
return(oldvalue);
}
/* PsychDCEnumerateVideoSources(int outPos);
*
* Enumerates all connected and supported video sources into an internal
* array "devices".
*
* If deviceIndex >= 0 : Returns pointer to PsychVideosourceRecordType struct
* with info about the detected device with index 'deviceIndex'
* or NULL if no such device exists. The pointer is valid until
* the Screen module returns control to the runtime - then it
* will get deallocated and must not be accessed anymore!
*
* If deviceIndex < 0 : Returns NULL to caller, returns a struct array to runtime
* environment return argument position 'outPos' with all info
* about the detected sources.
*/
void PsychDCEnumerateVideoSources(int outPos)
{
PsychGenericScriptType *devs;
const char *FieldNames[]={"DeviceIndex", "ClassIndex", "InputIndex", "ClassName", "InputHandle", "Device", "DevicePath", "DeviceName", "GUID", "DevicePlugin", "DeviceSelectorProperty" };
dc1394camera_list_t *cameras = NULL;
dc1394camera_t *camera = NULL;
unsigned int numCameras, i;
dc1394error_t err;
char guid[100];
char unit[10];
char deviceName[1024];
uint32_t node, generation;
// Perform first-time init, if needed:
PsychDCLibInit();
// Query a list of all available (connected) Firewire cameras:
err = dc1394_camera_enumerate(libdc, &cameras);
if (err != DC1394_SUCCESS) {
// Failed to detect any cameras:
printf("Unable to enumerate firewire cameras: %s\n", dc1394_error_get_string(err));
PsychErrorExitMsg(PsychError_user, "Unable to detect Firewire cameras: Read 'help VideoCaptureDC1394' for troubleshooting tips.\n");
}
// Get number of detected cameras:
numCameras = cameras->num;
// Create output struct array with n output slots:
PsychAllocOutStructArray(outPos, TRUE, numCameras, 11, FieldNames, &devs);
// Iterate all available devices:
for(i = 0; i < numCameras; i++) {
// Open connection to camera so we can query some more info about it:
camera = dc1394_camera_new_unit(libdc, cameras->ids[i].guid, cameras->ids[i].unit);
if (camera) {
// Open success: Query vendor and model name:
sprintf(deviceName, "'%s':'%s'", camera->vendor, camera->model);
PsychSetStructArrayStringElement("DeviceName", i, deviceName, devs);
// Get node to which the camera is attached, and its current generation count:
dc1394_camera_get_node(camera, &node, &generation);
sprintf(deviceName, "%x:%x", node, generation);
PsychSetStructArrayStringElement("InputHandle", i, deviceName, devs);
// Done with query, release camera:
dc1394_camera_free(camera);
}
else {
// Open failed: Assign empty DeviceName:
PsychSetStructArrayStringElement("DeviceName", i, "'UNKNOWN':'UNKNOWN'", devs);
PsychSetStructArrayStringElement("InputHandle", i, "", devs);
}
PsychSetStructArrayDoubleElement("DeviceIndex", i, i, devs);
PsychSetStructArrayDoubleElement("ClassIndex", i, 7, devs);
PsychSetStructArrayDoubleElement("InputIndex", i, i, devs);
PsychSetStructArrayStringElement("ClassName", i, "1394-IIDC", devs);
sprintf(unit, "%i", cameras->ids[i].unit);
PsychSetStructArrayStringElement("Device", i, unit, devs);
PsychSetStructArrayStringElement("DevicePath", i, "", devs);
sprintf(guid, "%" PRIx64, cameras->ids[i].guid);
PsychSetStructArrayStringElement("GUID", i, guid, devs);
PsychSetStructArrayStringElement("DevicePlugin", i, "libDC1394", devs);
PsychSetStructArrayStringElement("DeviceSelectorProperty", i, "GUID + Device(==IIDC-Unit id)", devs);
}
// Free camera list:
dc1394_camera_free_list(cameras);
cameras=NULL;
return;
}
// Helper for GStreamer movie playback: Debayers a raw bayer input image into a RGB output image: Caller has to free() outRGBImage after use!
unsigned char* PsychDCDebayerFrame(unsigned char* inBayerImage, unsigned int width, unsigned int height, unsigned int bitdepth)
{
dc1394error_t error;
dc1394video_frame_t inFrame, outFrame;
unsigned char* outImage;
memset(&inFrame, 0, sizeof(dc1394video_frame_t));
memset(&outFrame, 0, sizeof(dc1394video_frame_t));
inFrame.image = inBayerImage;
inFrame.size[0] = width;
inFrame.size[1] = height;
inFrame.color_coding = (bitdepth > 8) ? DC1394_COLOR_CODING_RAW16 : DC1394_COLOR_CODING_RAW8;
inFrame.data_depth = bitdepth;
inFrame.stride = width * ((bitdepth > 8) ? 2 : 1);
inFrame.color_filter = global_color_filter;
// Trigger bayer filtering for debayering via 'global_debayer_method':
if (DC1394_SUCCESS != (error = dc1394_debayer_frames(&inFrame, &outFrame, global_debayer_method))) {
printf("PTB-WARNING: Debayering of raw sensor image data failed! %s\n", dc1394_error_get_string(error));
if (error == DC1394_INVALID_COLOR_FILTER) {
printf("PTB-WARNING: Invalid Bayer filter pattern selected.\n");
printf("PTB-WARNING: Use Screen('SetVideoCaptureParameter', -1, 'OverrideBayerPattern', pattern);\n");
printf("PTB-WARNING: to assign a suitable 'pattern' manually.\n");
}
if (error == DC1394_INVALID_BAYER_METHOD) {
printf("PTB-WARNING: Invalid debayering method selected. Select a different 'method' via \n");
printf("PTB-WARNING: Screen('SetVideoCaptureParameter', -1, 'DebayerMethod', method);\n");
}
// Failure:
printf("PTB-ERROR: Bayer filtering of video frame failed.\n");
return(NULL);
}
// Success. Allocate output buffer:
outImage = (unsigned char*) malloc(outFrame.allocated_image_bytes);
// Copy data into it:
memcpy(outImage, outFrame.image, outFrame.allocated_image_bytes);
// Release outFrame's image store:
free(outFrame.image);
// Return pointer to final RGB image:
return(outImage);
}
#endif
|