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 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682
|
/*
* video.c --
*
* This file contains C code that implements the video decoder model.
*
*/
/*
* Copyright (c) 1995 The Regents of the University of California.
* All rights reserved.
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose, without fee, and without written agreement is
* hereby granted, provided that the above copyright notice and the following
* two paragraphs appear in all copies of this software.
*
* IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
* OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
* CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
* ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*/
/*
* Portions of this software Copyright (c) 1995 Brown University.
* All rights reserved.
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose, without fee, and without written agreement
* is hereby granted, provided that the above copyright notice and the
* following two paragraphs appear in all copies of this software.
*
* IN NO EVENT SHALL BROWN UNIVERSITY BE LIABLE TO ANY PARTY FOR
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
* OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF BROWN
* UNIVERSITY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* BROWN UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"
* BASIS, AND BROWN UNIVERSITY HAS NO OBLIGATION TO PROVIDE MAINTENANCE,
* SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*/
/* We use FULL_COLOR_DITHER code for SMPEG */
#define DISABLE_DITHER
/* Correct bad motion information */
#define LOOSE_MPEG
/* If hardware accelerated, prevent use of dither code */
#ifdef USE_ATI
#ifndef DISABLE_DITHER
#define DISABLE_DITHER
#endif
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "decoders.h"
#include "video.h"
#include "util.h"
#include "proto.h"
#ifdef USE_ATI
#include "vhar128.h"
#endif
/* Declarations of functions. */
#ifndef USE_ATI
static void ReconIMBlock( VidStream*, int bnum );
static void ReconPMBlock( VidStream*, int bnum,
int recon_right_for, int recon_down_for, int zflag );
static void ReconBMBlock( VidStream*, int bnum,
int recon_right_back, int recon_down_back, int zflag );
static void ReconBiMBlock( VidStream*, int bnum,
int recon_right_for, int recon_down_for, int recon_right_back,
int recon_down_back, int zflag );
static void ReconSkippedBlock( unsigned char *source, unsigned char *dest,
int row, int col, int row_size, int right, int down,
int right_half, int down_half, int width );
#endif /* USE_ATI */
static void DoPictureDisplay( VidStream* );
static int ParseSeqHead( VidStream* );
static int ParseGOP( VidStream* );
static int ParsePicture( VidStream*, TimeStamp );
static int ParseSlice( VidStream* );
static int ParseMacroBlock( VidStream* );
static void ProcessSkippedPFrameMBlocks( VidStream* );
static void ProcessSkippedBFrameMBlocks( VidStream* );
/*
Changes to make the code reentrant:
de-globalized: totNumFrames, realTimeStart, matched_depth, ditherType,
curBits, ReconPMBlock statics, first, [lc]max[xy], ditherFlags,
vid_stream, Parse_done, seekValue, ReadPack static, sys_layer,
bitOffset, bitLength, bitBuffer, curVidStream,
X globals to xinfo (window, et al)
use vid_stream->film_has_ended instead of FilmState
lookup tables only initialized once, global as possible
(default_intra_matrix, zigzag, zigzag_direct, scan)
get rid of setjmp, long jmp
Additional changes:
if DISABLE_DITHER defined then do not compile dithering code
-lsh@cs.brown.edu (Loring Holden)
*/
/* Macro for returning 1 if num is positive, -1 if negative, 0 if 0. */
#define Sign(num) ((num > 0) ? 1 : ((num == 0) ? 0 : -1))
/* Set up array for fast conversion from zig zag order to row/column
coordinates.
*/
const int zigzag[64][2] =
{
{ 0, 0 }, { 1, 0 }, { 0, 1 }, { 0, 2 }, { 1, 1 },
{ 2, 0 }, { 3, 0 }, { 2, 1 }, { 1, 2 }, { 0, 3 },
{ 0, 4 }, { 1, 3 }, { 2, 2 }, { 3, 1 }, { 4, 0 },
{ 5, 0 }, { 4, 1 }, { 3, 2 }, { 2, 3 }, { 1, 4 },
{ 0, 5 }, { 0, 6 }, { 1, 5 }, { 2, 4 }, { 3, 3 },
{ 4, 2 }, { 5, 1 }, { 6, 0 }, { 7, 0 }, { 6, 1 },
{ 5, 2 }, { 4, 3 }, { 3, 4 }, { 2, 5 }, { 1, 6 },
{ 0, 7 }, { 1, 7 }, { 2, 6 }, { 3, 5 }, { 4, 4 },
{ 5, 3 }, { 6, 2 }, { 7, 1 }, { 7, 2 }, { 6, 3 },
{ 5, 4 }, { 4, 5 }, { 3, 6 }, { 2, 7 }, { 3, 7 },
{ 4, 6 }, { 5, 5 }, { 6, 4 }, { 7, 3 }, { 7, 4 },
{ 6, 5 }, { 5, 6 }, { 4, 7 }, { 5, 7 }, { 6, 6 },
{ 7, 5 }, { 7, 6 }, { 6, 7 }, { 7, 7 }
};
/* Set up array for fast conversion from row/column coordinates to
zig zag order.
*/
const int scan[8][8] =
{
{ 0, 1, 5, 6, 14, 15, 27, 28 },
{ 2, 4, 7, 13, 16, 26, 29, 42 },
{ 3, 8, 12, 17, 25, 30, 41, 43 },
{ 9, 11, 18, 24, 31, 40, 44, 53 },
{ 10, 19, 23, 32, 39, 45, 52, 54 },
{ 20, 22, 33, 38, 46, 51, 55, 60 },
{ 21, 34, 37, 47, 50, 56, 59, 61 },
{ 35, 36, 48, 49, 57, 58, 62, 63 }
};
/* Max lum, chrom indices for illegal block checking. */
#ifdef USE_CROP_TABLE
/*
* We use a lookup table to make sure values stay in the 0..255 range.
* Since this is cropping (ie, x = (x < 0)?0:(x>255)?255:x; ), wee call this
* table the "crop table".
* MAX_NEG_CROP is the maximum neg/pos value we can handle.
*/
#define MAX_NEG_CROP 2048
#define NUM_CROP_ENTRIES (2048+2*MAX_NEG_CROP)
static unsigned char cropTbl[NUM_CROP_ENTRIES];
#define crop(x) cm[x]
#else
static inline unsigned char crop(int x)
{
if(x<=0)
return 0;
if(x>=255)
return 255;
return x;
}
#endif /* USE_CROP_TABLE */
/*
The following accounts for time and size spent in various parsing acitivites
if ANALYSIS has been defined.
*/
#ifdef ANALYSIS
unsigned int bitCount = 0;
/* #define SHOWMB_FLAG */
/* #define SHOWEACHFLAG */
typedef struct {
int frametype;
unsigned int totsize;
unsigned int number;
unsigned int i_mbsize;
unsigned int p_mbsize;
unsigned int b_mbsize;
unsigned int bi_mbsize;
unsigned int i_mbnum;
unsigned int p_mbnum;
unsigned int b_mbnum;
unsigned int bi_mbnum;
unsigned int i_mbcbp[64];
unsigned int p_mbcbp[64];
unsigned int b_mbcbp[64];
unsigned int bi_mbcbp[64];
unsigned int i_mbcoeff[64];
unsigned int p_mbcoeff[64];
unsigned int b_mbcoeff[64];
unsigned int bi_mbcoeff[64];
double tottime;
} Statval;
Statval stat_a[4];
unsigned int pictureSizeCount;
unsigned int mbSizeCount;
unsigned int *mbCBPPtr, *mbCoeffPtr, *mbSizePtr;
unsigned int cacheHit[8][8];
unsigned int cacheMiss[8][8];
static void
init_stat_struct(astat)
Statval *astat;
{
int j;
astat->frametype = 0;
astat->totsize = 0;
astat->number = 0;
astat->i_mbsize = 0;
astat->p_mbsize = 0;
astat->b_mbsize = 0;
astat->bi_mbsize = 0;
astat->i_mbnum = 0;
astat->p_mbnum = 0;
astat->b_mbnum = 0;
astat->bi_mbnum = 0;
for (j = 0; j < 64; j++) {
astat->i_mbcbp[j] = 0;
astat->p_mbcbp[j] = 0;
astat->b_mbcbp[j] = 0;
astat->bi_mbcbp[j] = 0;
astat->i_mbcoeff[j] = 0;
astat->p_mbcoeff[j] = 0;
astat->b_mbcoeff[j] = 0;
astat->bi_mbcoeff[j] = 0;
}
astat->tottime = 0.0;
}
void
init_stats()
{
int i, j;
for (i = 0; i < 4; i++) {
init_stat_struct(&(stat_a[i]));
stat_a[i].frametype = i;
}
for (i = 0; i < 8; i++) {
for (j = 0; j < 8; j++) {
cacheHit[i][j] = 0;
cacheMiss[i][j] = 0;
}
}
bitCount = 0;
}
static void
PrintOneStat()
{
int i;
printf("\n");
switch (stat_a[0].frametype) {
case I_TYPE:
printf("I FRAME\n");
break;
case P_TYPE:
printf("P FRAME\n");
break;
case B_TYPE:
printf("B FRAME\n");
break;
}
printf("Size: %d bytes + %d bits\n", stat_a[0].totsize / 8, stat_a[0].totsize % 8);
if (stat_a[0].i_mbnum > 0) {
printf("\tI Macro Block Stats:\n");
printf("\t%d I Macroblocks\n", stat_a[0].i_mbnum);
printf("\tAvg. Size: %d bytes + %d bits\n",
stat_a[0].i_mbsize / (8 * stat_a[0].i_mbnum),
(stat_a[0].i_mbsize * stat_a[0].i_mbnum) % 8);
printf("\t\tCoded Block Pattern Histogram:\n");
for (i = 0; i < 64; i += 8) {
printf("\t%.6d %.6d %.6d %.6d %.6d %.6d %.6d %.6d\n", stat_a[0].i_mbcbp[i],
stat_a[0].i_mbcbp[i + 1], stat_a[0].i_mbcbp[i + 2], stat_a[0].i_mbcbp[i + 3],
stat_a[0].i_mbcbp[i + 4], stat_a[0].i_mbcbp[i + 5], stat_a[0].i_mbcbp[i + 6],
stat_a[0].i_mbcbp[i + 7]);
}
printf("\n\t\tNumber of Coefficients/Block Histogram:\n");
for (i = 0; i < 64; i += 8) {
printf("\t%.6d %.6d %.6d %.6d %.6d %.6d %.6d %.6d\n", stat_a[0].i_mbcoeff[i],
stat_a[0].i_mbcoeff[i + 1], stat_a[0].i_mbcoeff[i + 2],
stat_a[0].i_mbcoeff[i + 3], stat_a[0].i_mbcoeff[i + 4],
stat_a[0].i_mbcoeff[i + 5], stat_a[0].i_mbcoeff[i + 6],
stat_a[0].i_mbcoeff[i + 7]);
}
}
if (stat_a[0].p_mbnum > 0) {
printf("\tP Macro Block Stats:\n");
printf("\t%d P Macroblocks\n", stat_a[0].p_mbnum);
printf("\tAvg. Size: %d bytes + %d bits\n",
stat_a[0].p_mbsize / (8 * stat_a[0].p_mbnum),
(stat_a[0].p_mbsize / stat_a[0].p_mbnum) % 8);
printf("\t\tCoded Block Pattern Histogram:\n");
for (i = 0; i < 64; i += 8) {
printf("\t%.6d %.6d %.6d %.6d %.6d %.6d %.6d %.6d\n", stat_a[0].p_mbcbp[i],
stat_a[0].p_mbcbp[i + 1], stat_a[0].p_mbcbp[i + 2], stat_a[0].p_mbcbp[i + 3],
stat_a[0].p_mbcbp[i + 4], stat_a[0].p_mbcbp[i + 5], stat_a[0].p_mbcbp[i + 6],
stat_a[0].p_mbcbp[i + 7]);
}
printf("\n\t\tNumber of Coefficients/Block Histogram:\n");
for (i = 0; i < 64; i += 8) {
printf("\t%.6d %.6d %.6d %.6d %.6d %.6d %.6d %.6d\n", stat_a[0].p_mbcoeff[i],
stat_a[0].p_mbcoeff[i + 1], stat_a[0].p_mbcoeff[i + 2],
stat_a[0].p_mbcoeff[i + 3], stat_a[0].p_mbcoeff[i + 4],
stat_a[0].p_mbcoeff[i + 5], stat_a[0].p_mbcoeff[i + 6],
stat_a[0].p_mbcoeff[i + 7]);
}
}
if (stat_a[0].b_mbnum > 0) {
printf("\tB Macro Block Stats:\n");
printf("\t%d B Macroblocks\n", stat_a[0].b_mbnum);
printf("\tAvg. Size: %d bytes + %d bits\n",
stat_a[0].b_mbsize / (8 * stat_a[0].b_mbnum),
(stat_a[0].b_mbsize / stat_a[0].b_mbnum) % 8);
printf("\t\tCoded Block Pattern Histogram:\n");
for (i = 0; i < 64; i += 8) {
printf("\t%.6d %.6d %.6d %.6d %.6d %.6d %.6d %.6d\n", stat_a[0].b_mbcbp[i],
stat_a[0].b_mbcbp[i + 1], stat_a[0].b_mbcbp[i + 2], stat_a[0].b_mbcbp[i + 3],
stat_a[0].b_mbcbp[i + 4], stat_a[0].b_mbcbp[i + 5], stat_a[0].b_mbcbp[i + 6],
stat_a[0].b_mbcbp[i + 7]);
}
printf("\n\t\tNumber of Coefficients/Block Histogram:\n");
for (i = 0; i < 64; i += 8) {
printf("\t%.6d %.6d %.6d %.6d %.6d %.6d %.6d %.6d\n", stat_a[0].b_mbcoeff[i],
stat_a[0].b_mbcoeff[i + 1], stat_a[0].b_mbcoeff[i + 2],
stat_a[0].b_mbcoeff[i + 3], stat_a[0].b_mbcoeff[i + 4],
stat_a[0].b_mbcoeff[i + 5], stat_a[0].b_mbcoeff[i + 6],
stat_a[0].b_mbcoeff[i + 7]);
}
}
if (stat_a[0].bi_mbnum > 0) {
printf("\tBi Macro Block Stats:\n");
printf("\t%d Bi Macroblocks\n", stat_a[0].bi_mbnum);
printf("\tAvg. Size: %d bytes + %d bits\n",
stat_a[0].bi_mbsize / (8 * stat_a[0].bi_mbnum),
(stat_a[0].bi_mbsize * stat_a[0].bi_mbnum) % 8);
printf("\t\tCoded Block Pattern Histogram:\n");
for (i = 0; i < 64; i += 8) {
printf("\t%.6d %.6d %.6d %.6d %.6d %.6d %.6d %.6d\n", stat_a[0].bi_mbcbp[i],
stat_a[0].bi_mbcbp[i + 1], stat_a[0].bi_mbcbp[i + 2], stat_a[0].bi_mbcbp[i + 3],
stat_a[0].bi_mbcbp[i + 4], stat_a[0].bi_mbcbp[i + 5], stat_a[0].bi_mbcbp[i + 6],
stat_a[0].bi_mbcbp[i + 7]);
}
printf("\n\t\tNumber of Coefficients/Block Histogram:\n");
for (i = 0; i < 64; i += 8) {
printf("\t%.6d %.6d %.6d %.6d %.6d %.6d %.6d %.6d\n", stat_a[0].bi_mbcoeff[i],
stat_a[0].bi_mbcoeff[i + 1], stat_a[0].bi_mbcoeff[i + 2],
stat_a[0].bi_mbcoeff[i + 3], stat_a[0].bi_mbcoeff[i + 4],
stat_a[0].bi_mbcoeff[i + 5], stat_a[0].bi_mbcoeff[i + 6],
stat_a[0].bi_mbcoeff[i + 7]);
}
}
printf("\nTime to Decode: %g secs.\n", stat_a[0].tottime);
printf("****************\n");
}
void
PrintAllStats(vid_stream)
VidStream *vid_stream;
{
int i, j;
unsigned int supertot, supernum;
double supertime;
printf("\n");
printf("General Info: \n");
printf("Width: %d\nHeight: %d\n", vid_stream->mb_width * 16, vid_stream->mb_height * 16);
for (i = 1; i < 4; i++) {
if (stat_a[i].number == 0)
continue;
switch (i) {
case 1:
printf("I FRAMES\n");
break;
case 2:
printf("P FRAMES\n");
break;
case 3:
printf("B FRAMES\n");
break;
}
printf("Number: %d\n", stat_a[i].number);
printf("Avg. Size: %d bytes + %d bits\n",
stat_a[i].totsize / (8 * stat_a[i].number), (stat_a[i].totsize / stat_a[i].number) % 8);
if (stat_a[i].i_mbnum > 0) {
printf("\tI Macro Block Stats:\n");
printf("\t%d I Macroblocks\n", stat_a[i].i_mbnum);
printf("\tAvg. Size: %d bytes + %d bits\n",
stat_a[i].i_mbsize / (8 * stat_a[i].i_mbnum),
(stat_a[i].i_mbsize / stat_a[i].i_mbnum) % 8);
printf("\t\tCoded Block Pattern Histogram:\n");
for (j = 0; j < 64; j += 8) {
printf("\t%.6d %.6d %.6d %.6d %.6d %.6d %.6d %.6d\n", stat_a[i].i_mbcbp[j],
stat_a[i].i_mbcbp[j + 1], stat_a[i].i_mbcbp[j + 2], stat_a[i].i_mbcbp[j + 3],
stat_a[i].i_mbcbp[j + 4], stat_a[i].i_mbcbp[j + 5], stat_a[i].i_mbcbp[j + 6],
stat_a[i].i_mbcbp[j + 7]);
}
printf("\n\t\tNumber of Coefficients/Block Histogram:\n");
for (j = 0; j < 64; j += 8) {
printf("\t%.6d %.6d %.6d %.6d %.6d %.6d %.6d %.6d\n", stat_a[i].i_mbcoeff[j],
stat_a[i].i_mbcoeff[j + 1], stat_a[i].i_mbcoeff[j + 2],
stat_a[i].i_mbcoeff[j + 3], stat_a[i].i_mbcoeff[j + 4],
stat_a[i].i_mbcoeff[j + 5], stat_a[i].i_mbcoeff[j + 6],
stat_a[i].i_mbcoeff[j + 7]);
}
}
if (stat_a[i].p_mbnum > 0) {
printf("\tP Macro Block Stats:\n");
printf("\t%d P Macroblocks\n", stat_a[i].p_mbnum);
printf("\tAvg. Size: %d bytes + %d bits\n",
stat_a[i].p_mbsize / (8 * stat_a[i].p_mbnum),
(stat_a[i].p_mbsize / stat_a[i].p_mbnum) % 8);
printf("\t\tCoded Block Pattern Histogram:\n");
for (j = 0; j < 64; j += 8) {
printf("\t%.6d %.6d %.6d %.6d %.6d %.6d %.6d %.6d\n", stat_a[i].p_mbcbp[j],
stat_a[i].p_mbcbp[j + 1], stat_a[i].p_mbcbp[j + 2], stat_a[i].p_mbcbp[j + 3],
stat_a[i].p_mbcbp[j + 4], stat_a[i].p_mbcbp[j + 5], stat_a[i].p_mbcbp[j + 6],
stat_a[i].p_mbcbp[j + 7]);
}
printf("\n\t\tNumber of Coefficients/Block Histogram:\n");
for (j = 0; j < 64; j += 8) {
printf("\t%.6d %.6d %.6d %.6d %.6d %.6d %.6d %.6d\n", stat_a[i].p_mbcoeff[j],
stat_a[i].p_mbcoeff[j + 1], stat_a[i].p_mbcoeff[j + 2],
stat_a[i].p_mbcoeff[j + 3], stat_a[i].p_mbcoeff[j + 4],
stat_a[i].p_mbcoeff[j + 5], stat_a[i].p_mbcoeff[j + 6],
stat_a[i].p_mbcoeff[j + 7]);
}
}
if (stat_a[i].b_mbnum > 0) {
printf("\tB Macro Block Stats:\n");
printf("\t%d B Macroblocks\n", stat_a[i].b_mbnum);
printf("\tAvg. Size: %d bytes + %d bits\n",
stat_a[i].b_mbsize / (8 * stat_a[i].b_mbnum),
(stat_a[i].b_mbsize * stat_a[i].b_mbnum) % 8);
printf("\t\tCoded Block Pattern Histogram:\n");
for (j = 0; j < 64; j += 8) {
printf("\t%.6d %.6d %.6d %.6d %.6d %.6d %.6d %.6d\n", stat_a[i].b_mbcbp[j],
stat_a[i].b_mbcbp[j + 1], stat_a[i].b_mbcbp[j + 2], stat_a[i].b_mbcbp[j + 3],
stat_a[i].b_mbcbp[j + 4], stat_a[i].b_mbcbp[j + 5], stat_a[i].b_mbcbp[j + 6],
stat_a[i].b_mbcbp[j + 7]);
}
printf("\n\t\tNumber of Coefficients/Block Histogram:\n");
for (j = 0; j < 64; j += 8) {
printf("\t%.6d %.6d %.6d %.6d %.6d %.6d %.6d %.6d\n", stat_a[i].b_mbcoeff[j],
stat_a[i].b_mbcoeff[j + 1], stat_a[i].b_mbcoeff[j + 2],
stat_a[i].b_mbcoeff[j + 3], stat_a[i].b_mbcoeff[j + 4],
stat_a[i].b_mbcoeff[j + 5], stat_a[i].b_mbcoeff[j + 6],
stat_a[i].b_mbcoeff[j + 7]);
}
}
if (stat_a[i].bi_mbnum > 0) {
printf("\tBi Macro Block Stats:\n");
printf("\t%d Bi Macroblocks\n", stat_a[i].bi_mbnum);
printf("\tAvg. Size: %d bytes + %d bits\n",
stat_a[i].bi_mbsize / (8 * stat_a[i].bi_mbnum),
(stat_a[i].bi_mbsize * stat_a[i].bi_mbnum) % 8);
printf("\t\tCoded Block Pattern Histogram:\n");
for (j = 0; j < 64; j += 8) {
printf("\t%.6d %.6d %.6d %.6d %.6d %.6d %.6d %.6d\n", stat_a[i].bi_mbcbp[j],
stat_a[i].bi_mbcbp[j + 1], stat_a[i].bi_mbcbp[j + 2], stat_a[i].bi_mbcbp[j + 3],
stat_a[i].bi_mbcbp[j + 4], stat_a[i].bi_mbcbp[j + 5], stat_a[i].bi_mbcbp[j + 6],
stat_a[i].bi_mbcbp[j + 7]);
}
printf("\n\t\tNumber of Coefficients/Block Histogram:\n");
for (j = 0; j < 64; j += 8) {
printf("\t%.6d %.6d %.6d %.6d %.6d %.6d %.6d %.6d\n", stat_a[i].bi_mbcoeff[j],
stat_a[i].bi_mbcoeff[j + 1], stat_a[i].bi_mbcoeff[j + 2],
stat_a[i].bi_mbcoeff[j + 3], stat_a[i].bi_mbcoeff[j + 4],
stat_a[i].bi_mbcoeff[j + 5], stat_a[i].bi_mbcoeff[j + 6],
stat_a[i].bi_mbcoeff[j + 7]);
}
}
printf("\nAvg. Time to Decode: %f secs.\n",
(stat_a[i].tottime / ((double) stat_a[i].number)));
printf("\n");
printf("*************************\n\n");
}
supertot = stat_a[1].totsize + stat_a[2].totsize + stat_a[3].totsize;
supernum = stat_a[1].number + stat_a[2].number + stat_a[3].number;
supertime = stat_a[1].tottime + stat_a[2].tottime + stat_a[3].tottime;
printf("Total Number of Frames: %d\n", supernum);
printf("Avg Frame Size: %d bytes %d bits\n",
supertot / (8 * supernum), (supertot / supernum) % 8);
printf("Total Time Decoding: %g secs.\n", supertime);
printf("Avg Decoding Time/Frame: %g secs.\n", supertime / ((double) supernum));
printf("Avg Decoding Frames/Sec: %g secs.\n", ((double) supernum) / supertime);
printf("\n");
printf("Cache Hits/Miss\n");
for (i = 0; i < 8; i++) {
printf("%.6d/%.6d\t%.6d/%.6d\t%.6d/%.6d\t%.6d/%.6d\n",
cacheHit[i][0], cacheMiss[i][0], cacheHit[i][1], cacheMiss[i][1],
cacheHit[i][2], cacheMiss[i][2], cacheHit[i][3], cacheMiss[i][3]);
printf("%.6d/%.6d\t%.6d/%.6d\t%.6d/%.6d\t%.6d/%.6d\n",
cacheHit[i][4], cacheMiss[i][4], cacheHit[i][5], cacheMiss[i][5],
cacheHit[i][6], cacheMiss[i][6], cacheHit[i][7], cacheMiss[i][7]);
}
}
static void
CollectStats()
{
int i, j;
i = stat_a[0].frametype;
stat_a[i].totsize += stat_a[0].totsize;
stat_a[i].number += stat_a[0].number;
stat_a[i].i_mbsize += stat_a[0].i_mbsize;
stat_a[i].p_mbsize += stat_a[0].p_mbsize;
stat_a[i].b_mbsize += stat_a[0].b_mbsize;
stat_a[i].bi_mbsize += stat_a[0].bi_mbsize;
stat_a[i].i_mbnum += stat_a[0].i_mbnum;
stat_a[i].p_mbnum += stat_a[0].p_mbnum;
stat_a[i].b_mbnum += stat_a[0].b_mbnum;
stat_a[i].bi_mbnum += stat_a[0].bi_mbnum;
for (j = 0; j < 64; j++) {
stat_a[i].i_mbcbp[j] += stat_a[0].i_mbcbp[j];
stat_a[i].p_mbcbp[j] += stat_a[0].p_mbcbp[j];
stat_a[i].b_mbcbp[j] += stat_a[0].b_mbcbp[j];
stat_a[i].bi_mbcbp[j] += stat_a[0].bi_mbcbp[j];
stat_a[i].i_mbcoeff[j] += stat_a[0].i_mbcoeff[j];
stat_a[i].p_mbcoeff[j] += stat_a[0].p_mbcoeff[j];
stat_a[i].b_mbcoeff[j] += stat_a[0].b_mbcoeff[j];
stat_a[i].bi_mbcoeff[j] += stat_a[0].bi_mbcoeff[j];
}
stat_a[i].tottime += stat_a[0].tottime;
init_stat_struct(&(stat_a[0]));
}
static unsigned int
bitCountRead()
{
return bitCount;
}
static void
StartTime()
{
stat_a[0].tottime = ReadSysClock();
}
static void
EndTime()
{
stat_a[0].tottime = ReadSysClock() - stat_a[0].tottime;
}
#endif
/*
*--------------------------------------------------------------
*
* ReadSysClock --
*
* Computes the current time according to the system clock.
*
* Results:
* The current time according to the system clock.
*
* Side effects:
* None.
*
*--------------------------------------------------------------
*/
double
ReadSysClock()
{
return(SDL_GetTicks()/1000.0);
}
/*
*--------------------------------------------------------------
*
* InitCrop --
*
* Initializes cropTbl - this was taken from newVidStream so
* that it wasn't done for each new video stream
*
* Results:
* None
*
* Side effects:
* cropTbl will be initialized
*
*--------------------------------------------------------------
*/
void
InitCrop()
{
#ifdef USE_CROP_TABLE
int i;
/* Initialize crop table. */
for (i = (-MAX_NEG_CROP); i < NUM_CROP_ENTRIES - MAX_NEG_CROP; i++) {
if (i <= 0) {
cropTbl[i + MAX_NEG_CROP] = 0;
#ifdef TWELVE_BITS
} else if (i >= 2047) {
cropTbl[i + MAX_NEG_CROP] = 2047;
#endif
} else if (i >= 255) {
cropTbl[i + MAX_NEG_CROP] = 255;
} else {
cropTbl[i + MAX_NEG_CROP] = i;
}
}
#endif /* USE_CROP_TABLE */
}
/*
*--------------------------------------------------------------
*
* NewVidStream --
*
* Allocates and initializes a VidStream structure. Takes
* as parameter requested size for buffer length.
*
* Results:
* A pointer to the new VidStream structure.
*
* Side effects:
* None.
*
*--------------------------------------------------------------
*/
VidStream* NewVidStream( unsigned int buffer_len )
{
int i, j;
VidStream* vs;
static const unsigned char default_intra_matrix[64] =
{
8, 16, 19, 22, 26, 27, 29, 34,
16, 16, 22, 24, 27, 29, 34, 37,
19, 22, 26, 27, 29, 34, 34, 38,
22, 22, 26, 27, 29, 34, 37, 40,
22, 26, 27, 29, 32, 35, 40, 48,
26, 27, 29, 32, 35, 40, 48, 58,
26, 27, 29, 34, 38, 46, 56, 69,
27, 29, 35, 38, 46, 56, 69, 83
};
/* Check for legal buffer length. */
if( buffer_len < 4 )
return NULL;
/* Make buffer length multiple of 4. */
buffer_len = (buffer_len + 3) >> 2;
/* Allocate memory for vs structure. */
vs = (VidStream *) malloc(sizeof(VidStream));
memset( vs, 0, (sizeof *vs) );
/* Initialize pointers to extension and user data. */
vs->group.ext_data = vs->group.user_data =
vs->picture.extra_info = vs->picture.user_data =
vs->picture.ext_data = vs->slice.extra_info =
vs->ext_data = vs->user_data = NULL;
/* Copy default intra matrix. */
for( i = 0; i < 8; i++ )
{
for( j = 0; j < 8; j++ )
{
vs->intra_quant_matrix[i][j] = default_intra_matrix[i * 8 + j];
}
}
/* Initialize non intra quantization matrix. */
for( i = 0; i < 8; i++ )
{
for( j = 0; j < 8; j++ )
{
vs->non_intra_quant_matrix[i][j] = 16;
}
}
/* Initialize noise base matrix */
for( i = 0; i < 8; i++ )
for( j = 0; j < 8; j++ )
vs->noise_base_matrix[i][j] = (short) vs->non_intra_quant_matrix[i][j];
j_rev_dct((short *) vs->noise_base_matrix);
for( i = 0; i < 8; i++ )
for( j = 0; j < 8; j++ )
vs->noise_base_matrix[i][j] *= vs->noise_base_matrix[i][j];
/* Initialize pointers to image spaces. */
vs->current = vs->past = vs->future = NULL;
for( i = 0; i < RING_BUF_SIZE; i++ )
{
vs->ring[i] = NULL;
}
/* Create buffer. */
vs->buf_start = (unsigned int *) malloc(buffer_len * 4);
/*
* Set max_buf_length to one less than actual length to deal with messy
* data without proper seq. end codes.
*/
vs->max_buf_length = buffer_len - 1;
/* Initialize fields that used to be global */
vs->ditherFlags = NULL;
vs->rate_deal = -1;
/* Reset everything for start of display */
ResetVidStream(vs);
#ifdef USE_ATI
/* Initialize Rage128 hardware */
vs->ati_handle = vhar128_new();
#endif
/* Return structure. */
return vs;
}
/*
*--------------------------------------------------------------
*
* ResetVidStream --
*
* Re-initializes a VidStream structure. Takes
* as parameter a pointer to the VidStream to reset.
*
* Results:
* None.
*
* Side effects:
* None.
*
*--------------------------------------------------------------
*/
void ResetVidStream( VidStream* vid )
{
int i;
/* Initialize pointers to image spaces. */
vid->current = vid->past = vid->future = NULL;
/* Initialize rings */
for (i = 0; i < RING_BUF_SIZE; i++) {
if ( vid->ring[i] )
vid->ring[i]->locked = 0; /* Unlock */
}
/* Initialize bitstream i/o fields. */
vid->bit_offset = 0;
vid->buf_length = 0;
vid->buffer = vid->buf_start;
vid->curBits = 0;
/* We are at the beginning of the film, so film has not ended */
vid->film_has_ended = FALSE;
/* Reset number of frames to zero */
vid->totNumFrames=0;
#ifdef CALCULATE_FPS
for ( i=0; i<FPS_WINDOW; ++i )
vid->frame_time[i] = 0.0;
vid->timestamp_index = 0;
#endif
/* Fields added by KR for synchronization */
vid->_skipFrame = 0;
vid->_skipCount = 0;
vid->_jumpFrame = -1;
vid->realTimeStart = 0;
/* Reset EOF_flag to 0 */
vid->EOF_flag = FALSE;
vid->current_frame=0;
vid->need_frameadjust=false;
}
/*
*--------------------------------------------------------------
*
* DestroyVidStream --
*
* Deallocates a VidStream structure.
*
* Results:
* None.
*
* Side effects:
* None.
*
*--------------------------------------------------------------
*/
void DestroyVidStream( VidStream* astream )
{
int i;
if( astream->ext_data != NULL )
free(astream->ext_data);
if( astream->user_data != NULL )
free(astream->user_data);
if( astream->group.ext_data != NULL )
free(astream->group.ext_data);
if( astream->group.user_data != NULL )
free(astream->group.user_data);
if( astream->picture.extra_info != NULL )
free(astream->picture.extra_info);
if( astream->picture.ext_data != NULL )
free(astream->picture.ext_data);
if( astream->picture.user_data != NULL )
free(astream->picture.user_data);
if( astream->slice.extra_info != NULL )
free(astream->slice.extra_info);
if( astream->buf_start != NULL )
free(astream->buf_start);
for( i = 0; i < RING_BUF_SIZE; i++ )
{
if( astream->ring[i] != NULL )
{
DestroyPictImage( astream, astream->ring[i] );
astream->ring[i] = NULL;
}
}
if( astream->ditherFlags != NULL )
free( astream->ditherFlags );
#ifdef USE_ATI
vhar128_close(astream->ati_handle);
#endif
#ifdef USE_ATI
vhar128_delete();
#endif
free( (char*) astream );
}
/*
*--------------------------------------------------------------
*
* NewPictImage --
*
* Allocates and initializes a PictImage structure.
* The width and height of the image space are passed in
* as parameters.
*
* Results:
* A pointer to the new PictImage structure.
*
* Side effects:
* None.
*
*--------------------------------------------------------------
*/
PictImage* NewPictImage( VidStream* vid_stream, int w, int h, SDL_Surface *dst )
{
PictImage* pi;
/* Allocate memory space for pi structure. */
pi = (PictImage *) malloc(sizeof(PictImage));
/* Create a YV12 image (Y + V + U) */
#ifdef USE_ATI
pi->image = vhar128_newimage(vid_stream->ati_handle, w, h);
#else
pi->image = (unsigned char *) malloc(w*h*12/8);
pi->luminance = (unsigned char *)pi->image;
pi->Cr = pi->luminance + (w*h);
pi->Cb = pi->luminance + (w*h) + (w*h)/4;
#endif
/* Alloc space for filter info */
pi->mb_qscale = (unsigned short int *) malloc(vid_stream->mb_width * vid_stream->mb_height * sizeof(unsigned int));
/* Reset locked flag. */
pi->locked = 0;
/* Return pointer to pi structure. */
return pi;
}
bool InitPictImages( VidStream* vid_stream, int w, int h, SDL_Surface* dst )
{
int i;
vid_stream->current = vid_stream->past = vid_stream->future = NULL;
for (i = 0; i < RING_BUF_SIZE; i++) {
if ( vid_stream->ring[i] ) {
DestroyPictImage(vid_stream, vid_stream->ring[i]);
}
vid_stream->ring[i] = NewPictImage( vid_stream, w, h, dst );
if ( ! vid_stream->ring[i] )
return false;
}
#ifdef USE_ATI
struct vhar128_image * ring[RING_BUF_SIZE];
for (i = 0; i < RING_BUF_SIZE; i++) {
ring[i] = vid_stream->ring[i]->image;
}
vhar128_init(vid_stream->ati_handle, w, h, ring, RING_BUF_SIZE);
#endif
return true;
}
/*
*--------------------------------------------------------------
*
* DestroyPictImage --
*
* Deallocates a PictImage structure.
*
* Results:
* None.
*
* Side effects:
* None.
*
*--------------------------------------------------------------
*/
void DestroyPictImage( VidStream* vid_stream, PictImage* apictimage )
{
#ifdef USE_ATI
vhar128_destroyimage(vid_stream->ati_handle, apictimage->image);
#else
if (apictimage->image != NULL) free(apictimage->image);
#endif
free(apictimage->mb_qscale);
free(apictimage);
}
/*
*--------------------------------------------------------------
*
* mpegVidRsrc --
*
* Parses bit stream until MB_QUANTUM number of
* macroblocks have been decoded or current slice or
* picture ends, whichever comes first. If the start
* of a frame is encountered, the frame is time stamped
* with the value passed in time_stamp. If the value
* passed in buffer is not null, the video stream buffer
* is set to buffer and the length of the buffer is
* expected in value passed in through length.
*
* Results:
* A pointer to the video stream structure used.
*
* Side effects:
* Bit stream is irreversibly parsed. If a picture is completed,
* a function is called to display the frame at the correct time.
*
*--------------------------------------------------------------
*/
VidStream* mpegVidRsrc( TimeStamp time_stamp, VidStream* vid_stream, int first )
{
unsigned int data;
int i, status;
/*
* If called for the first time, find start code, make sure it is a
* sequence start code.
*/
if( first )
{
vid_stream->num_left = 0;
vid_stream->leftover_bytes = 0;
vid_stream->Parse_done = FALSE;
next_start_code(vid_stream); /* sets curBits */
show_bits32(data);
if( data != SEQ_START_CODE )
{
vid_stream->_smpeg->SetError("Invalid sequence in video stream");
/* Let whoever called NewVidStream call DestroyVidStream - KR
DestroyVidStream( vid_stream );
*/
return 0;
}
}
else
{
#ifdef UTIL2
vid_stream->curBits = *vid_stream->buffer << vid_stream->bit_offset;
#else
vid_stream->curBits = *vid_stream->buffer;
#endif
}
/* Get next 32 bits (size of start codes). */
show_bits32(data);
/* Check for end of file */
if(vid_stream->EOF_flag)
{
/* Set ended flag first so that ExecuteDisplay may check it. */
vid_stream->film_has_ended = TRUE;
if( vid_stream->future != NULL )
{
vid_stream->current = vid_stream->future;
vid_stream->_smpeg->ExecuteDisplay( vid_stream );
}
#ifdef ANALYSIS
PrintAllStats(vid_stream);
#endif
goto done;
}
/*
* Process according to start code (or parse macroblock if not a start code
* at all).
*/
switch( data )
{
case SEQ_END_CODE:
case 0x000001b9: /* handle ISO_11172_END_CODE too */
#ifdef VERBOSE_DEBUG
printf("SEQ_END_CODE\n");
#endif
flush_bits32;
goto done;
break;
case SEQ_START_CODE:
/* Sequence start code. Parse sequence header. */
#ifdef VERBOSE_DEBUG
printf("SEQ_START_CODE\n");
#endif
if( ParseSeqHead( vid_stream ) != PARSE_OK )
{
fprintf( stderr, "mpegVidRsrc ParseSeqHead\n" );
goto error;
}
/*
* Return after sequence start code so that application above can use
* info in header.
*/
goto done;
case GOP_START_CODE:
/* Group of Pictures start code. Parse gop header. */
#ifdef VERBOSE_DEBUG
printf("GOP_START_CODE\n");
#endif
if( ParseGOP(vid_stream) != PARSE_OK )
{
fprintf( stderr, "mpegVidRsrc ParseGOP\n" );
goto error;
}
/* need adjust current_frame (after Seek) */
if (vid_stream->need_frameadjust) {
int prev;
prev = vid_stream->totNumFrames;
vid_stream->current_frame = (int)
(
vid_stream->group.tc_hours * 3600 * vid_stream->rate_deal +
vid_stream->group.tc_minutes * 60 * vid_stream->rate_deal +
vid_stream->group.tc_seconds * vid_stream->rate_deal +
vid_stream->group.tc_pictures);
vid_stream->need_frameadjust=false;
vid_stream->totNumFrames=vid_stream->current_frame;
#if 0
printf("Adjusted Frame %d -> %d\n",prev,vid_stream->current_frame);
#endif
}
goto done;
case PICTURE_START_CODE:
/* Picture start code. Parse picture header and first slice header. */
#ifdef VERBOSE_DEBUG
printf("PICTURE_START_CODE\n");
#endif
if (vid_stream->timestamp_mark < vid_stream->buffer
&& !vid_stream->timestamp_used){
vid_stream->timestamp_used = true;
status = ParsePicture( vid_stream, vid_stream->timestamp );
} else
status = ParsePicture( vid_stream, -1);
if((vid_stream->picture.code_type == B_TYPE) &&
vid_stream->_skipFrame && (vid_stream->_jumpFrame < 0))
{
status = SKIP_PICTURE;
}
if( !vid_stream->current )
status = SKIP_PICTURE;
if( status == SKIP_PICTURE )
{
//fprintf( stderr, "%d\r", vid_stream->totNumFrames );
next_start_code( vid_stream );
while( ! next_bits( 32, PICTURE_START_CODE, vid_stream ) )
{
if( next_bits( 32, GOP_START_CODE, vid_stream) )
break;
else if( next_bits( 32, SEQ_END_CODE, vid_stream ) )
break;
flush_bits( 24 );
next_start_code( vid_stream );
}
vid_stream->_smpeg->timeSync( vid_stream );
goto done;
}
else if( status != PARSE_OK )
{
fprintf( stderr, "mpegVidRsrc ParsePicture\n" );
goto error;
}
if( ParseSlice(vid_stream) != PARSE_OK )
{
fprintf( stderr, "mpegVidRsrc ParseSlice\n" );
goto error;
}
break;
case SEQUENCE_ERROR_CODE:
#ifdef VERBOSE_DEBUG
printf("SEQUENCE_ERROR_CODE\n");
#endif
flush_bits32;
next_start_code(vid_stream);
goto done;
default:
/* No base picture for decoding */
if( !vid_stream->current )
{
flush_bits32;
next_start_code(vid_stream);
#ifdef VERBOSE_DEBUG
printf("No base picture, flushing to next start code\n");
#endif
goto done;
}
/* Check for slice start code. */
if( (data >= SLICE_MIN_START_CODE) && (data <= SLICE_MAX_START_CODE) )
{
/* Slice start code. Parse slice header. */
if( ParseSlice(vid_stream) != PARSE_OK )
{
fprintf( stderr, "mpegVidRsrc ParseSlice\n" );
goto error;
}
}
#ifdef VERBOSE_DEBUG
else
fprintf(stderr, "Unknown data [%x] - not slice start code!\n", data);
#endif
break;
}
/* Parse next MB_QUANTUM macroblocks. */
for( i = 0; i < MB_QUANTUM; i++ )
{
/* Check to see if actually a startcode and not a macroblock. */
if( ! next_bits( 23, 0x00000000, vid_stream ) &&
! vid_stream->film_has_ended )
{
/* Not start code. Parse Macroblock. */
if (ParseMacroBlock(vid_stream) != PARSE_OK)
{
#ifdef VERBOSE_WARNINGS
fprintf( stderr, "mpegVidRsrc ParseMacroBlock\n" );
#endif
goto error;
}
}
else
{
/* Not macroblock, actually start code. Get start code. */
next_start_code(vid_stream);
show_bits32(data);
/*
* If start code is outside range of slice start codes, frame is
* complete, display frame.
*/
if( ((data<SLICE_MIN_START_CODE) || (data>SLICE_MAX_START_CODE)) &&
(data != SEQUENCE_ERROR_CODE) )
{
DoPictureDisplay( vid_stream );
}
goto done;
}
}
/* Check if we just finished a picture on the MB_QUANTUMth macroblock */
if( next_bits( 23, 0x00000000, vid_stream ) )
{
next_start_code(vid_stream);
show_bits32(data);
if( (data < SLICE_MIN_START_CODE) || (data > SLICE_MAX_START_CODE) )
{
DoPictureDisplay( vid_stream );
}
}
goto done;
error:
next_start_code( vid_stream );
return vid_stream;
done:
return vid_stream;
}
/*
*--------------------------------------------------------------
*
* ParseSeqHead --
*
* Assumes bit stream is at the begining of the sequence
* header start code. Parses off the sequence header.
*
* Results:
* Fills the vid_stream structure with values derived and
* decoded from the sequence header. Allocates the pict image
* structures based on the dimensions of the image space
* found in the sequence header.
*
* Side effects:
* Bit stream irreversibly parsed off.
*
*--------------------------------------------------------------
*/
static int ParseSeqHead( VidStream* vid_stream )
{
unsigned int data;
int i, j;
#ifndef DISABLE_DITHER
int ditherType=vid_stream->ditherType;
#endif
/* Flush off sequence start code. */
flush_bits32;
/* Get horizontal size of image space. */
get_bits12(data);
vid_stream->h_size = (data + 15) & ~15;
/* Get vertical size of image space. */
get_bits12(data);
vid_stream->v_size = (data + 15) & ~15;
/* Calculate macroblock width and height of image space. */
vid_stream->mb_width = (vid_stream->h_size + 15) / 16;
vid_stream->mb_height = (vid_stream->v_size + 15) / 16;
#ifndef DISABLE_DITHER
/* If dither type is MBORDERED allocate ditherFlags. */
if (ditherType == MBORDERED_DITHER) {
vid_stream->ditherFlags =
(char *) malloc(vid_stream->mb_width*vid_stream->mb_height);
}
#endif
/* Parse of aspect ratio code. */
get_bits4(data);
vid_stream->aspect_ratio = (unsigned char) data;
/* Parse off picture rate code. */
get_bits4(data);
vid_stream->picture_rate = (unsigned char) data;
/* Parse off bit rate. */
get_bits18(data);
vid_stream->bit_rate = data;
/* Flush marker bit. */
flush_bits(1);
/* Parse off vbv buffer size. */
get_bits10(data);
vid_stream->vbv_buffer_size = data;
#ifdef not_def
/* Lets not bother with this. Only increases memory image */
if (data*1024>vid_stream->max_buf_length) {
unsigned int *newbuf;
int sz=1024*data+1;
/* If they actually want a bigger buffer than we default to,
let them have it! (if we can) */
newbuf = (unsigned int *) realloc(vid_stream->buf_start, (unsigned int) 4*sz);
if (newbuf!=(unsigned int *)NULL) {
vid_stream->max_buf_length=sz;
vid_stream->buffer=
(vid_stream->buffer-vid_stream->buf_start)+newbuf;
vid_stream->buf_start=newbuf;
}}
#endif
/* Parse off contrained parameter flag. */
get_bits1(data);
if (data) {
vid_stream->const_param_flag = TRUE;
} else
vid_stream->const_param_flag = FALSE;
/*
* If intra_quant_matrix_flag set, parse off intra quant matrix values.
*/
get_bits1(data);
if (data) {
for (i = 0; i < 64; i++) {
get_bits8(data);
vid_stream->intra_quant_matrix[zigzag[i][1]][zigzag[i][0]] =
(unsigned char) data;
}
}
/*
* If non intra quant matrix flag set, parse off non intra quant matrix
* values.
*/
get_bits1(data);
if (data) {
for (i = 0; i < 64; i++) {
get_bits8(data);
vid_stream->non_intra_quant_matrix[zigzag[i][1]][zigzag[i][0]] =
(unsigned char) data;
}
}
/* Adjust noise base matrix according to non_intra matrix */
for( i = 0; i < 8; i++ )
for( j = 0; j < 8; j++ )
vid_stream->noise_base_matrix[i][j] = (short) vid_stream->non_intra_quant_matrix[i][j];
j_rev_dct((short *) vid_stream->noise_base_matrix);
for( i = 0; i < 8; i++ )
for( j = 0; j < 8; j++ )
vid_stream->noise_base_matrix[i][j] *= vid_stream->noise_base_matrix[i][j];
/* Go to next start code. */
next_start_code(vid_stream);
/*
* If next start code is extension start code, parse off extension data.
*/
if (next_bits(32, EXT_START_CODE, vid_stream)) {
flush_bits32;
if (vid_stream->ext_data != NULL) {
free(vid_stream->ext_data);
vid_stream->ext_data = NULL;
}
vid_stream->ext_data = get_ext_data(vid_stream);
}
/* If next start code is user start code, parse off user data. */
if (next_bits(32, USER_START_CODE, vid_stream)) {
flush_bits32;
if (vid_stream->user_data != NULL) {
free(vid_stream->user_data);
vid_stream->user_data = NULL;
}
vid_stream->user_data = get_ext_data(vid_stream);
}
return PARSE_OK;
}
/*
*--------------------------------------------------------------
*
* ParseGOP --
*
* Parses of group of pictures header from bit stream
* associated with vid_stream.
*
* Results:
* Values in gop header placed into video stream structure.
*
* Side effects:
* Bit stream irreversibly parsed.
*
*--------------------------------------------------------------
*/
static int ParseGOP( VidStream* vid_stream )
{
unsigned int data;
/* Flush group of pictures start code. */
flush_bits32;
/* Parse off drop frame flag. */
get_bits1(data);
if (data) {
vid_stream->group.drop_flag = TRUE;
} else
vid_stream->group.drop_flag = FALSE;
/* Parse off hour component of time code. */
get_bits5(data);
vid_stream->group.tc_hours = data;
/* Parse off minute component of time code. */
get_bits6(data);
vid_stream->group.tc_minutes = data;
/* Flush marker bit. */
flush_bits(1);
/* Parse off second component of time code. */
get_bits6(data);
vid_stream->group.tc_seconds = data;
/* Parse off picture count component of time code. */
get_bits6(data);
vid_stream->group.tc_pictures = data;
/* Parse off closed gop and broken link flags. */
get_bits2(data);
if (data > 1) {
vid_stream->group.closed_gop = TRUE;
if (data > 2) {
vid_stream->group.broken_link = TRUE;
} else
vid_stream->group.broken_link = FALSE;
} else {
vid_stream->group.closed_gop = FALSE;
if (data) {
vid_stream->group.broken_link = TRUE;
} else
vid_stream->group.broken_link = FALSE;
}
/* Goto next start code. */
next_start_code(vid_stream);
/* If next start code is extension data, parse off extension data. */
if (next_bits(32, EXT_START_CODE, vid_stream)) {
flush_bits32;
if (vid_stream->group.ext_data != NULL) {
free(vid_stream->group.ext_data);
vid_stream->group.ext_data = NULL;
}
vid_stream->group.ext_data = get_ext_data(vid_stream);
}
/* If next start code is user data, parse off user data. */
if (next_bits(32, USER_START_CODE,vid_stream)) {
flush_bits32;
if (vid_stream->group.user_data != NULL) {
free(vid_stream->group.user_data);
vid_stream->group.user_data = NULL;
}
vid_stream->group.user_data = get_ext_data(vid_stream);
}
return PARSE_OK;
}
/*
*--------------------------------------------------------------
*
* ParsePicture --
*
* Parses picture header. Marks picture to be presented
* at particular time given a time stamp.
*
* Results:
* Values from picture header put into video stream structure.
*
* Side effects:
* Bit stream irreversibly parsed.
*
*--------------------------------------------------------------
*/
static int ParsePicture( VidStream* vid_stream, TimeStamp time_stamp )
{
unsigned int data;
int i;
/* Flush header start code. */
flush_bits32;
/* This happens if there is a picture code before a sequence start */
if (vid_stream->ring[0] == NULL) {
printf("Warning: picture block before sequence header block\n");
return SKIP_PICTURE;
}
/* Parse off temporal reference. */
get_bits10(data);
vid_stream->picture.temp_ref = data;
/* Parse of picture type. */
get_bits3(data);
vid_stream->picture.code_type = data;
if ((vid_stream->picture.code_type == B_TYPE) &&
((vid_stream->future == NULL) ||
((vid_stream->past == NULL) && !(vid_stream->group.closed_gop))))
/* According to 2-D.5.1 (p D-18) this is ok, if the refereneces are OK */
return SKIP_PICTURE;
if ((vid_stream->picture.code_type == P_TYPE) && (vid_stream->future == NULL))
return SKIP_PICTURE;
#ifdef ANALYSIS
StartTime();
stat_a[0].frametype = vid_stream->picture.code_type;
stat_a[0].number = 1;
stat_a[0].totsize = 45;
pictureSizeCount = bitCountRead();
#endif
/* Parse off vbv buffer delay value. */
get_bits16(data);
vid_stream->picture.vbv_delay = data;
/* If P or B type frame... */
if ((vid_stream->picture.code_type == P_TYPE) ||
(vid_stream->picture.code_type == B_TYPE)) {
/* Parse off forward vector full pixel flag. */
get_bits1(data);
if (data) {
vid_stream->picture.full_pel_forw_vector = TRUE;
} else {
vid_stream->picture.full_pel_forw_vector = FALSE;
}
/* Parse of forw_r_code. */
get_bits3(data);
/* Decode forw_r_code into forw_r_size and forw_f. */
vid_stream->picture.forw_r_size = data - 1;
vid_stream->picture.forw_f = (1 << vid_stream->picture.forw_r_size);
}
/* If B type frame... */
if (vid_stream->picture.code_type == B_TYPE) {
/* Parse off back vector full pixel flag. */
get_bits1(data);
if (data)
vid_stream->picture.full_pel_back_vector = TRUE;
else
vid_stream->picture.full_pel_back_vector = FALSE;
/* Parse off back_r_code. */
get_bits3(data);
/* Decode back_r_code into back_r_size and back_f. */
vid_stream->picture.back_r_size = data - 1;
vid_stream->picture.back_f = (1 << vid_stream->picture.back_r_size);
}
/* Get extra bit picture info. */
if (vid_stream->picture.extra_info != NULL) {
free(vid_stream->picture.extra_info);
vid_stream->picture.extra_info = NULL;
}
vid_stream->picture.extra_info = get_extra_bit_info(vid_stream);
/* Goto next start code. */
next_start_code(vid_stream);
/* If start code is extension start code, parse off extension data. */
if (next_bits(32, EXT_START_CODE, vid_stream)) {
flush_bits32;
if (vid_stream->picture.ext_data != NULL) {
free(vid_stream->picture.ext_data);
vid_stream->picture.ext_data = NULL;
}
vid_stream->picture.ext_data = get_ext_data(vid_stream);
}
/* If start code is user start code, parse off user data. */
if (next_bits(32, USER_START_CODE, vid_stream)) {
flush_bits32;
if (vid_stream->picture.user_data != NULL) {
free(vid_stream->picture.user_data);
vid_stream->picture.user_data = NULL;
}
vid_stream->picture.user_data = get_ext_data(vid_stream);
}
/* Find a pict image structure in ring buffer not currently locked. */
i = 0;
while (vid_stream->ring[i]->locked != 0) {
if (++i >= RING_BUF_SIZE) {
perror("Fatal error. Ring buffer full.");
exit(1);
}
}
/* Set current pict image structure to the one just found in ring. */
vid_stream->current = vid_stream->ring[i];
/* Set time stamp. */
vid_stream->current->show_time = time_stamp;
/* Reset past macroblock address field. */
vid_stream->mblock.past_mb_addr = -1;
#ifdef USE_ATI
int back, forw, current;
back = forw = -1;
current = i;
/* Look for indexes of future and past frames */
for(i = 0; i < RING_BUF_SIZE; i++)
{
if(vid_stream->future == vid_stream->ring[i]) forw = i;
if(vid_stream->past == vid_stream->ring[i]) back = i;
}
/* Start decoding a new frame */
switch(vid_stream->picture.code_type)
{
case B_TYPE:
vhar128_newdecode(vid_stream->ati_handle, forw, back, current);
break;
case P_TYPE:
vhar128_newdecode(vid_stream->ati_handle, -1, forw, current);
break;
case I_TYPE:
vhar128_newdecode(vid_stream->ati_handle, -1, -1, current);
break;
}
#endif
return PARSE_OK;
}
/*
*--------------------------------------------------------------
*
* ParseSlice --
*
* Parses off slice header.
*
* Results:
* Values found in slice header put into video stream structure.
*
* Side effects:
* Bit stream irreversibly parsed.
*
*--------------------------------------------------------------
*/
static int ParseSlice( VidStream* vid_stream )
{
unsigned int data;
/* Flush slice start code. */
flush_bits(24);
/* Parse off slice vertical position. */
get_bits8(data);
vid_stream->slice.vert_pos = data;
/* Parse off quantization scale. */
get_bits5(data);
vid_stream->slice.quant_scale = data;
/* Parse off extra bit slice info. */
if (vid_stream->slice.extra_info != NULL) {
free(vid_stream->slice.extra_info);
vid_stream->slice.extra_info = NULL;
}
vid_stream->slice.extra_info = get_extra_bit_info(vid_stream);
/* Reset past intrablock address. */
vid_stream->mblock.past_intra_addr = -2;
/* Reset previous recon motion vectors. */
vid_stream->mblock.recon_right_for_prev = 0;
vid_stream->mblock.recon_down_for_prev = 0;
vid_stream->mblock.recon_right_back_prev = 0;
vid_stream->mblock.recon_down_back_prev = 0;
/* Reset macroblock address. */
vid_stream->mblock.mb_address = ((vid_stream->slice.vert_pos - 1) *
vid_stream->mb_width) - 1;
/* Reset past dct dc y, cr, and cb values. */
#ifdef USE_ATI
vid_stream->block.dct_dc_y_past = 0;
vid_stream->block.dct_dc_cr_past = 0;
vid_stream->block.dct_dc_cb_past = 0;
#else
vid_stream->block.dct_dc_y_past = 1024 << 3;
vid_stream->block.dct_dc_cr_past = 1024 << 3;
vid_stream->block.dct_dc_cb_past = 1024 << 3;
#endif
return PARSE_OK;
}
/*
*--------------------------------------------------------------
*
* ParseMacroBlock --
*
* Parseoff macroblock. Reconstructs DCT values. Applies
* inverse DCT, reconstructs motion vectors, calculates and
* set pixel values for macroblock in current pict image
* structure.
*
* Results:
* Here's where everything really happens. Welcome to the
* heart of darkness.
*
* Side effects:
* Bit stream irreversibly parsed off.
*
*--------------------------------------------------------------
*/
static int ParseMacroBlock( VidStream* vid_stream )
{
int addr_incr;
unsigned int data;
int mask, i, recon_right_for, recon_down_for, recon_right_back,
recon_down_back;
int zero_block_flag;
BOOLEAN mb_quant = 0, mb_motion_forw = 0, mb_motion_back = 0,
mb_pattern = 0;
#ifndef DISABLE_DITHER
int no_dith_flag = 0;
int ditherType = vid_stream->ditherType;
#endif
#ifdef ANALYSIS
mbSizeCount = bitCountRead();
#endif
#ifdef USE_ATI
/* Empty macroblock */
vid_stream->block.dct_recon[0][0] = 0xFFFFFFFF;
vid_stream->block.dct_recon[1][0] = 0xFFFFFFFF;
vid_stream->block.dct_recon[2][0] = 0xFFFFFFFF;
vid_stream->block.dct_recon[3][0] = 0xFFFFFFFF;
vid_stream->block.dct_recon[4][0] = 0xFFFFFFFF;
vid_stream->block.dct_recon[5][0] = 0xFFFFFFFF;
#endif
/*
* Parse off macroblock address increment and add to macroblock address.
*/
do
{
unsigned int ind;
show_bits11(ind);
DecodeMBAddrInc(addr_incr);
if (mb_addr_inc[ind].num_bits==0)
{
addr_incr = 1;
}
if (addr_incr == MB_ESCAPE)
{
vid_stream->mblock.mb_address += 33;
addr_incr = MB_STUFFING;
}
} while (addr_incr == MB_STUFFING);
vid_stream->mblock.mb_address += addr_incr;
if( vid_stream->mblock.mb_address > (int) (vid_stream->mb_height *
vid_stream->mb_width - 1) )
return SKIP_TO_START_CODE;
if( vid_stream->mblock.mb_address < 0 )
return SKIP_TO_START_CODE;
/*
* If macroblocks have been skipped, process skipped macroblocks.
*/
if (vid_stream->mblock.mb_address - vid_stream->mblock.past_mb_addr > 1) {
if (vid_stream->picture.code_type == P_TYPE)
ProcessSkippedPFrameMBlocks(vid_stream);
else if (vid_stream->picture.code_type == B_TYPE)
ProcessSkippedBFrameMBlocks(vid_stream);
}
/* Set past macroblock address to current macroblock address. */
vid_stream->mblock.past_mb_addr = vid_stream->mblock.mb_address;
/* Based on picture type decode macroblock type. */
switch (vid_stream->picture.code_type) {
case I_TYPE:
DecodeMBTypeI(mb_quant, mb_motion_forw, mb_motion_back, mb_pattern,
vid_stream->mblock.mb_intra);
break;
case P_TYPE:
DecodeMBTypeP(mb_quant, mb_motion_forw, mb_motion_back, mb_pattern,
vid_stream->mblock.mb_intra);
break;
case B_TYPE:
DecodeMBTypeB(mb_quant, mb_motion_forw, mb_motion_back, mb_pattern,
vid_stream->mblock.mb_intra);
break;
case D_TYPE:
fprintf(stderr, "ERROR: MPEG-1 Streams with D-frames are not supported\n");
exit(1);
}
/* If quantization flag set, parse off new quantization scale. */
if (mb_quant == TRUE) {
get_bits5(data);
vid_stream->slice.quant_scale = data;
}
/* If forward motion vectors exist... */
if (mb_motion_forw == TRUE) {
/* Parse off and decode horizontal forward motion vector. */
DecodeMotionVectors(vid_stream->mblock.motion_h_forw_code);
/* If horiz. forward r data exists, parse off. */
if ((vid_stream->picture.forw_f != 1) &&
(vid_stream->mblock.motion_h_forw_code != 0)) {
get_bitsn(vid_stream->picture.forw_r_size, data);
vid_stream->mblock.motion_h_forw_r = data;
}
/* Parse off and decode vertical forward motion vector. */
DecodeMotionVectors(vid_stream->mblock.motion_v_forw_code);
/* If vert. forw. r data exists, parse off. */
if ((vid_stream->picture.forw_f != 1) &&
(vid_stream->mblock.motion_v_forw_code != 0)) {
get_bitsn(vid_stream->picture.forw_r_size, data);
vid_stream->mblock.motion_v_forw_r = data;
}
}
/* If back motion vectors exist... */
if (mb_motion_back == TRUE) {
/* Parse off and decode horiz. back motion vector. */
DecodeMotionVectors(vid_stream->mblock.motion_h_back_code);
/* If horiz. back r data exists, parse off. */
if ((vid_stream->picture.back_f != 1) &&
(vid_stream->mblock.motion_h_back_code != 0)) {
get_bitsn(vid_stream->picture.back_r_size, data);
vid_stream->mblock.motion_h_back_r = data;
}
/* Parse off and decode vert. back motion vector. */
DecodeMotionVectors(vid_stream->mblock.motion_v_back_code);
/* If vert. back r data exists, parse off. */
if ((vid_stream->picture.back_f != 1) &&
(vid_stream->mblock.motion_v_back_code != 0)) {
get_bitsn(vid_stream->picture.back_r_size, data);
vid_stream->mblock.motion_v_back_r = data;
}
}
#ifdef ANALYSIS
if (vid_stream->mblock.mb_intra) {
stat_a[0].i_mbnum++;
mbCBPPtr = stat_a[0].i_mbcbp;
mbCoeffPtr = stat_a[0].i_mbcoeff;
mbSizePtr = &(stat_a[0].i_mbsize);
} else if (mb_motion_back && mb_motion_forw) {
stat_a[0].bi_mbnum++;
mbCBPPtr = stat_a[0].bi_mbcbp;
mbCoeffPtr = stat_a[0].bi_mbcoeff;
mbSizePtr = &(stat_a[0].bi_mbsize);
} else if (mb_motion_back) {
stat_a[0].b_mbnum++;
mbCBPPtr = stat_a[0].b_mbcbp;
mbCoeffPtr = stat_a[0].b_mbcoeff;
mbSizePtr = &(stat_a[0].b_mbsize);
} else {
stat_a[0].p_mbnum++;
mbCBPPtr = stat_a[0].p_mbcbp;
mbCoeffPtr = stat_a[0].p_mbcoeff;
mbSizePtr = &(stat_a[0].p_mbsize);
}
#endif
/* If mblock pattern flag set, parse and decode CBP (code block pattern). */
if (mb_pattern == TRUE) {
DecodeCBP(vid_stream->mblock.cbp);
}
/* Otherwise, set CBP to zero. */
else
vid_stream->mblock.cbp = 0;
#ifdef ANALYSIS
mbCBPPtr[vid_stream->mblock.cbp]++;
#endif
/* Reconstruct motion vectors depending on picture type. */
if (vid_stream->picture.code_type == P_TYPE) {
/*
* If no forw motion vectors, reset previous and current vectors to 0.
*/
if (!mb_motion_forw) {
recon_right_for = 0;
recon_down_for = 0;
vid_stream->mblock.recon_right_for_prev = 0;
vid_stream->mblock.recon_down_for_prev = 0;
}
/*
* Otherwise, compute new forw motion vectors. Reset previous vectors to
* current vectors.
*/
else {
ComputeForwVector(&recon_right_for, &recon_down_for, vid_stream);
}
}
if (vid_stream->picture.code_type == B_TYPE) {
/* Reset prev. and current vectors to zero if mblock is intracoded. */
if (vid_stream->mblock.mb_intra) {
vid_stream->mblock.recon_right_for_prev = 0;
vid_stream->mblock.recon_down_for_prev = 0;
vid_stream->mblock.recon_right_back_prev = 0;
vid_stream->mblock.recon_down_back_prev = 0;
} else {
/* If no forw vectors, current vectors equal prev. vectors. */
if (!mb_motion_forw) {
recon_right_for = vid_stream->mblock.recon_right_for_prev;
recon_down_for = vid_stream->mblock.recon_down_for_prev;
}
/*
* Otherwise compute forw. vectors. Reset prev vectors to new values.
*/
else {
ComputeForwVector(&recon_right_for, &recon_down_for, vid_stream);
}
/* If no back vectors, set back vectors to prev back vectors. */
if (!mb_motion_back) {
recon_right_back = vid_stream->mblock.recon_right_back_prev;
recon_down_back = vid_stream->mblock.recon_down_back_prev;
}
/* Otherwise compute new vectors and reset prev. back vectors. */
else {
ComputeBackVector(&recon_right_back, &recon_down_back, vid_stream);
}
/*
* Store vector existence flags in structure for possible skipped
* macroblocks to follow.
*/
vid_stream->mblock.bpict_past_forw = mb_motion_forw;
vid_stream->mblock.bpict_past_back = mb_motion_back;
}
}
#ifndef DISABLE_DITHER
/* For each possible block in macroblock. */
if (ditherType == GRAY_DITHER ||
ditherType == GRAY2_DITHER ||
ditherType == GRAY256_DITHER ||
ditherType == GRAY2562_DITHER ||
ditherType == MONO_DITHER ||
ditherType == MONO_THRESHOLD) {
for (mask = 32, i = 0; i < 4; mask >>= 1, i++) {
/* If block exists... */
if ((vid_stream->mblock.mb_intra) || (vid_stream->mblock.cbp & mask)) {
zero_block_flag = 0;
ParseReconBlock(i, vid_stream);
} else {
zero_block_flag = 1;
}
/* If macroblock is intra coded... */
if (vid_stream->mblock.mb_intra) {
ReconIMBlock(vid_stream, i);
} else if (mb_motion_forw && mb_motion_back) {
ReconBiMBlock(vid_stream, i, recon_right_for, recon_down_for,
recon_right_back, recon_down_back, zero_block_flag);
} else if (mb_motion_forw || (vid_stream->picture.code_type == P_TYPE)) {
ReconPMBlock(vid_stream, i, recon_right_for, recon_down_for,
zero_block_flag);
} else if (mb_motion_back) {
ReconBMBlock(vid_stream, i, recon_right_back, recon_down_back,
zero_block_flag);
}
}
/* Kill the Chrominance blocks... */
if ((vid_stream->mblock.mb_intra) || (vid_stream->mblock.cbp & 0x2)) {
ParseAwayBlock(4, vid_stream);
}
if ((vid_stream->mblock.mb_intra) || (vid_stream->mblock.cbp & 0x1)) {
ParseAwayBlock(5, vid_stream);
}
} else {
if ((ditherType == MBORDERED_DITHER) &&
(vid_stream->mblock.cbp == 0) &&
(vid_stream->picture.code_type == 3) &&
(!vid_stream->mblock.mb_intra) &&
(!(mb_motion_forw && mb_motion_back))) {
MBOrderedDitherDisplayCopy(vid_stream, vid_stream->mblock.mb_address,
mb_motion_forw, recon_right_for, recon_down_for,
mb_motion_back, recon_right_back, recon_down_back,
vid_stream->past->display, vid_stream->future->display);
vid_stream->ditherFlags[vid_stream->mblock.mb_address] = 0;
no_dith_flag = 1;
}
else {
#endif
for (mask = 32, i = 0; i < 6; mask >>= 1, i++) {
/* If block exists... */
if ((vid_stream->mblock.mb_intra) || (vid_stream->mblock.cbp & mask)) {
zero_block_flag = 0;
ParseReconBlock(i, vid_stream);
} else {
zero_block_flag = 1;
}
#ifndef USE_ATI
/* If macroblock is intra coded... */
if (vid_stream->mblock.mb_intra) {
ReconIMBlock(vid_stream, i);
} else if (mb_motion_forw && mb_motion_back) {
ReconBiMBlock(vid_stream, i, recon_right_for, recon_down_for,
recon_right_back, recon_down_back, zero_block_flag);
} else if (mb_motion_forw || (vid_stream->picture.code_type == P_TYPE)) {
ReconPMBlock(vid_stream, i, recon_right_for, recon_down_for,
zero_block_flag);
} else if (mb_motion_back) {
ReconBMBlock(vid_stream, i, recon_right_back, recon_down_back,
zero_block_flag);
}
#endif
}
#ifdef USE_ATI
vhar128_macroblock(vid_stream->ati_handle,
(vid_stream->mblock.mb_address % vid_stream->mb_width) << 4,
(vid_stream->mblock.mb_address / vid_stream->mb_width) << 4,
vid_stream->mblock.mb_intra,
mb_motion_back, mb_motion_forw,
recon_right_back, recon_down_back,
recon_right_for, recon_down_for,
vid_stream->block.dct_recon);
#endif
#ifndef DISABLE_DITHER
}
}
#endif
#ifndef DISABLE_DITHER
if ((ditherType == MBORDERED_DITHER) && (!no_dith_flag)) {
if ((vid_stream->picture.code_type == 2) &&
(vid_stream->mblock.cbp == 0) &&
(!vid_stream->mblock.mb_intra)) {
MBOrderedDitherDisplayCopy(vid_stream, vid_stream->mblock.mb_address,
1, recon_right_for, recon_down_for,
0, 0, 0,
vid_stream->future->display,
(unsigned char *) NULL);
vid_stream->ditherFlags[vid_stream->mblock.mb_address] = 0;
}
else {
vid_stream->ditherFlags[vid_stream->mblock.mb_address] = 1;
}
}
#endif
/* If D Type picture, flush marker bit. */
if (vid_stream->picture.code_type == 4)
flush_bits(1);
/* If macroblock was intracoded, set macroblock past intra address. */
if (vid_stream->mblock.mb_intra)
vid_stream->mblock.past_intra_addr =
vid_stream->mblock.mb_address;
/* Store macroblock error for filter info */
if (vid_stream->mblock.mb_intra)
vid_stream->current->mb_qscale[vid_stream->mblock.mb_address] = 0;
else
vid_stream->current->mb_qscale[vid_stream->mblock.mb_address] = vid_stream->slice.quant_scale;
#ifdef ANALYSIS
*mbSizePtr += bitCountRead() - mbSizeCount;
#endif
return PARSE_OK;
}
/* software decoder follows */
#ifndef USE_ATI
/*
*--------------------------------------------------------------
*
* ReconIMBlock --
*
* Reconstructs intra coded macroblock.
*
* Results:
* None.
*
* Side effects:
* None.
*
*--------------------------------------------------------------
*/
#if defined(USE_CROP_TABLE) && !defined(NDEBUG)
/* If people really want to see such things, check 'em */
#define myassert(x,expression)\
if (!(expression)) {\
fprintf (stderr,"Bad crop value (%d) at line %d\n", x, __LINE__);\
next_start_code(vid_stream); return;}
#define assertCrop(x) myassert(x,((x) >= -MAX_NEG_CROP) && \
((x) <= 2048+MAX_NEG_CROP))
#else
#define assertCrop(x)
#endif
static void ReconIMBlock( VidStream* vid_stream, int bnum )
{
int mb_row, mb_col, row, col, row_size, rr;
unsigned char *dest;
/* Calculate macroblock row and column from address. */
mb_row = vid_stream->mblock.mb_address / vid_stream->mb_width;
mb_col = vid_stream->mblock.mb_address % vid_stream->mb_width;
/* If block is luminance block... */
if (bnum < 4) {
/* Calculate row and col values for upper left pixel of block. */
row = mb_row * 16;
col = mb_col * 16;
if (bnum > 1)
row += 8;
if (bnum % 2)
col += 8;
/* Set dest to luminance plane of current pict image. */
dest = vid_stream->current->luminance;
/* Establish row size. */
row_size = vid_stream->mb_width * 16;
}
/* Otherwise if block is Cr block... */
/* Cr first because of the earlier mixup */
else if (bnum == 5) {
/* Set dest to Cr plane of current pict image. */
dest = vid_stream->current->Cr;
/* Establish row size. */
row_size = vid_stream->mb_width * 8;
/* Calculate row,col for upper left pixel of block. */
row = mb_row * 8;
col = mb_col * 8;
}
/* Otherwise block is Cb block, and ... */
else {
/* Set dest to Cb plane of current pict image. */
dest = vid_stream->current->Cb;
/* Establish row size. */
row_size = vid_stream->mb_width * 8;
/* Calculate row,col for upper left pixel value of block. */
row = mb_row * 8;
col = mb_col * 8;
}
/*
* For each pixel in block, set to cropped reconstructed value from inverse
* dct.
*/
{
short *sp = &vid_stream->block.dct_recon[0][0];
#ifdef USE_CROP_TABLE
unsigned char *cm = cropTbl + MAX_NEG_CROP;
#endif
dest += row * row_size + col;
for (rr = 0; rr < 4; rr++, sp += 16, dest += row_size) {
dest[0] = crop(sp[0]);
assertCrop(sp[0]);
dest[1] = crop(sp[1]);
assertCrop(sp[1]);
dest[2] = crop(sp[2]);
assertCrop(sp[2]);
dest[3] = crop(sp[3]);
assertCrop(sp[3]);
dest[4] = crop(sp[4]);
assertCrop(sp[4]);
dest[5] = crop(sp[5]);
assertCrop(sp[5]);
dest[6] = crop(sp[6]);
assertCrop(sp[6]);
dest[7] = crop(sp[7]);
assertCrop(sp[7]);
dest += row_size;
dest[0] = crop(sp[8]);
assertCrop(sp[8]);
dest[1] = crop(sp[9]);
assertCrop(sp[9]);
dest[2] = crop(sp[10]);
assertCrop(sp[10]);
dest[3] = crop(sp[11]);
assertCrop(sp[11]);
dest[4] = crop(sp[12]);
assertCrop(sp[12]);
dest[5] = crop(sp[13]);
assertCrop(sp[13]);
dest[6] = crop(sp[14]);
assertCrop(sp[14]);
dest[7] = crop(sp[15]);
assertCrop(sp[15]);
}
}
}
/*
*--------------------------------------------------------------
*
* ReconPMBlock --
*
* Reconstructs forward predicted macroblocks.
*
* Results:
* None.
*
* Side effects:
* None.
*
*--------------------------------------------------------------
*/
static void ReconPMBlock( VidStream* vid_stream, int bnum,
int recon_right_for, int recon_down_for, int zflag )
{
int mb_row, mb_col, row, col, row_size, rr;
unsigned char *dest, *past = 0;
unsigned char *rindex1, *rindex2, *rindex3, *rindex4;
unsigned char *index;
short int *blockvals;
#ifdef LOOSE_MPEG
int maxx, maxy, cc;
int illegalBlock = 0;
int row_start, row_end, rfirst, rlast, col_start, col_end, cfirst, clast;
#endif
/* Calculate macroblock row and column from address. */
mb_row = vid_stream->mblock.mb_address / vid_stream->mb_width;
mb_col = vid_stream->mblock.mb_address % vid_stream->mb_width;
if (bnum < 4) {
/* Calculate right_for, down_for motion vectors. */
vid_stream->right_for = recon_right_for >> 1;
vid_stream->down_for = recon_down_for >> 1;
vid_stream->right_half_for = recon_right_for & 0x1;
vid_stream->down_half_for = recon_down_for & 0x1;
/* Set dest to luminance plane of current pict image. */
dest = vid_stream->current->luminance;
if (vid_stream->picture.code_type == B_TYPE) {
if (vid_stream->past != NULL)
past = vid_stream->past->luminance;
} else {
/* Set predictive frame to current future frame. */
if (vid_stream->future != NULL)
past = vid_stream->future->luminance;
}
/* Establish row size. */
row_size = vid_stream->mb_width << 4;
/* Calculate row,col of upper left pixel in block. */
row = mb_row << 4;
col = mb_col << 4;
if (bnum > 1)
row += 8;
if (bnum % 2)
col += 8;
#ifdef LOOSE_MPEG
/* Check for block illegality. */
maxx = vid_stream->mb_width*16-1;
maxy = vid_stream->mb_height*16-1;
if (row + vid_stream->down_for + vid_stream->down_half_for + 7 > maxy) illegalBlock |= 0x4;
else if (row + vid_stream->down_for < 0) illegalBlock |= 0x1;
if (col + vid_stream->right_for + vid_stream->right_half_for + 7 > maxx) illegalBlock |= 0x2;
else if (col + vid_stream->right_for < 0) illegalBlock |= 0x8;
#endif
}
/* Otherwise, block is NOT luminance block, ... */
else {
/* Construct motion vectors. */
recon_right_for /= 2;
recon_down_for /= 2;
vid_stream->right_for = recon_right_for >> 1;
vid_stream->down_for = recon_down_for >> 1;
vid_stream->right_half_for = recon_right_for & 0x1;
vid_stream->down_half_for = recon_down_for & 0x1;
/* Establish row size. */
row_size = vid_stream->mb_width << 3;
/* Calculate row,col of upper left pixel in block. */
row = mb_row << 3;
col = mb_col << 3;
#ifdef LOOSE_MPEG
/* Check for block illegality. */
maxx = vid_stream->mb_width*8-1;
maxy = vid_stream->mb_height*8-1;
if (row + vid_stream->down_for + vid_stream->down_half_for + 7 > maxy) illegalBlock |= 0x4;
else if (row + vid_stream->down_for < 0) illegalBlock |= 0x1;
if (col + vid_stream->right_for + vid_stream->right_half_for + 7 > maxx) illegalBlock |= 0x2;
else if (col + vid_stream->right_for < 0) illegalBlock |= 0x8;
#endif
/* If block is Cr block... */
/* 5 first because order was mixed up in earlier versions */
if (bnum == 5) {
/* Set dest to Cr plane of current pict image. */
dest = vid_stream->current->Cr;
if (vid_stream->picture.code_type == B_TYPE) {
if (vid_stream->past != NULL)
past = vid_stream->past->Cr;
} else {
if (vid_stream->future != NULL)
past = vid_stream->future->Cr;
}
}
/* Otherwise, block is Cb block... */
else {
/* Set dest to Cb plane of current pict image. */
dest = vid_stream->current->Cb;
if (vid_stream->picture.code_type == B_TYPE) {
if (vid_stream->past != NULL)
past = vid_stream->past->Cb;
} else {
if (vid_stream->future != NULL)
past = vid_stream->future->Cb;
}
}
}
/* For each pixel in block... */
#ifdef LOOSE_MPEG
if (illegalBlock) {
if (illegalBlock & 0x1) {
row_start = 0;
row_end = row+vid_stream->down_for+8;
rfirst = rlast = 8 - row_end;
}
else if (illegalBlock & 0x4) {
row_start = row + vid_stream->down_for;
row_end = maxy+1;
rlast = row_end - row_start - 1;
rfirst = 0;
}
else {
row_start = row+vid_stream->down_for;
row_end = row_start+8;
rfirst = 0;
}
if (illegalBlock & 0x8) {
col_start = 0;
col_end = col + vid_stream->right_for + 8;
cfirst = clast = 8 - col_end;
}
else if (illegalBlock & 0x2) {
col_start = col + vid_stream->right_for;
col_end = maxx + 1;
clast = col_end - col_start - 1;
cfirst = 0;
}
else {
col_start = col + vid_stream->right_for;
col_end = col_start + 8;
cfirst = 0;
}
for (rr = row_start; rr < row_end; rr++) {
rindex1 = past + (rr * row_size) + col_start;
index = dest + ((row + rfirst) * row_size) + col + cfirst;
for (cc = col_start; cc < col_end; cc++) {
*index++ = *rindex1++;
}
}
if (illegalBlock & 0x1) {
for (rr = rlast -1; rr >=0; rr--) {
index = dest + ((row + rr) * row_size) + col;
rindex1 = dest + ((row + rlast) * row_size) + col;
for (cc = 0; cc < 8; cc ++) {
*index++ = *rindex1++;
}
}
}
else if (illegalBlock & 0x4) {
for (rr = rlast+1; rr < 8; rr++) {
index = dest + ((row + rr) * row_size) + col;
rindex1 = dest + ((row + rlast) * row_size) + col;
for (cc = 0; cc < 8; cc ++) {
*index++ = *rindex1++;
}
}
}
if (illegalBlock & 0x2) {
for (cc = clast+1; cc < 8; cc++) {
index = dest + (row * row_size) + (col + cc);
rindex1 = dest + (row * row_size) + (col + clast);
for (rr = 0; rr < 8; rr++) {
*index = *rindex1;
index += row_size;
rindex1 += row_size;
}
}
}
else if (illegalBlock & 0x8) {
for (cc = clast-1; cc >= 0; cc--) {
index = dest + (row * row_size) + (col + cc);
rindex1 = dest + (row * row_size) + (col + clast);
for (rr = 0; rr < 8; rr++) {
*index = *rindex1;
index += row_size;
rindex1 += row_size;
}
}
}
if (!zflag) {
for (rr = 0; rr < 8; rr++) {
index = dest + (row*row_size) + col;
blockvals = &(vid_stream->block.dct_recon[rr][0]);
index[0] += blockvals[0];
index[1] += blockvals[1];
index[2] += blockvals[2];
index[3] += blockvals[3];
index[4] += blockvals[4];
index[5] += blockvals[5];
index[6] += blockvals[6];
index[7] += blockvals[7];
}
}
}
else {
#endif
index = dest + (row * row_size) + col;
rindex1 = past + (row + vid_stream->down_for) * row_size
+ col + vid_stream->right_for;
blockvals = &(vid_stream->block.dct_recon[0][0]);
/*
* Calculate predictive pixel value based on motion vectors and copy to
* dest plane.
*/
if ((!vid_stream->down_half_for) && (!vid_stream->right_half_for)) {
#ifdef USE_CROP_TABLE
unsigned char *cm = cropTbl + MAX_NEG_CROP;
#endif
if (!zflag)
for (rr = 0; rr < 4; rr++) {
index[0] = crop((int) rindex1[0] + (int) blockvals[0]);
index[1] = crop((int) rindex1[1] + (int) blockvals[1]);
index[2] = crop((int) rindex1[2] + (int) blockvals[2]);
index[3] = crop((int) rindex1[3] + (int) blockvals[3]);
index[4] = crop((int) rindex1[4] + (int) blockvals[4]);
index[5] = crop((int) rindex1[5] + (int) blockvals[5]);
index[6] = crop((int) rindex1[6] + (int) blockvals[6]);
index[7] = crop((int) rindex1[7] + (int) blockvals[7]);
index += row_size;
rindex1 += row_size;
index[0] = crop((int) rindex1[0] + (int) blockvals[8]);
index[1] = crop((int) rindex1[1] + (int) blockvals[9]);
index[2] = crop((int) rindex1[2] + (int) blockvals[10]);
index[3] = crop((int) rindex1[3] + (int) blockvals[11]);
index[4] = crop((int) rindex1[4] + (int) blockvals[12]);
index[5] = crop((int) rindex1[5] + (int) blockvals[13]);
index[6] = crop((int) rindex1[6] + (int) blockvals[14]);
index[7] = crop((int) rindex1[7] + (int) blockvals[15]);
blockvals += 16;
index += row_size;
rindex1 += row_size;
}
else {
if (vid_stream->right_for & 0x1) {
/* No alignment, use bye copy */
for (rr = 0; rr < 4; rr++) {
index[0] = rindex1[0];
index[1] = rindex1[1];
index[2] = rindex1[2];
index[3] = rindex1[3];
index[4] = rindex1[4];
index[5] = rindex1[5];
index[6] = rindex1[6];
index[7] = rindex1[7];
index += row_size;
rindex1 += row_size;
index[0] = rindex1[0];
index[1] = rindex1[1];
index[2] = rindex1[2];
index[3] = rindex1[3];
index[4] = rindex1[4];
index[5] = rindex1[5];
index[6] = rindex1[6];
index[7] = rindex1[7];
index += row_size;
rindex1 += row_size;
}
} else if (vid_stream->right_for & 0x2) {
/* Half-word bit aligned, use 16 bit copy */
short *src = (short *)rindex1;
short *dest = (short *)index;
row_size >>= 1;
for (rr = 0; rr < 4; rr++) {
dest[0] = src[0];
dest[1] = src[1];
dest[2] = src[2];
dest[3] = src[3];
dest += row_size;
src += row_size;
dest[0] = src[0];
dest[1] = src[1];
dest[2] = src[2];
dest[3] = src[3];
dest += row_size;
src += row_size;
}
} else {
/* Word aligned, use 32 bit copy */
int *src = (int *)rindex1;
int *dest = (int *)index;
row_size >>= 2;
for (rr = 0; rr < 4; rr++) {
dest[0] = src[0];
dest[1] = src[1];
dest += row_size;
src += row_size;
dest[0] = src[0];
dest[1] = src[1];
dest += row_size;
src += row_size;
}
}
}
} else {
#ifdef USE_CROP_TABLE
unsigned char *cm = cropTbl + MAX_NEG_CROP;
#endif
rindex2 = rindex1 + vid_stream->right_half_for
+ (vid_stream->down_half_for * row_size);
/* if one of the two is zero, then quality makes no difference */
if ((!vid_stream->right_half_for) ||
(!vid_stream->down_half_for) || (!qualityFlag)) {
if (!zflag) {
for (rr = 0; rr < 4; rr++) {
index[0] = crop(((int) (rindex1[0] + rindex2[0] + 1) >> 1) + blockvals[0]);
index[1] = crop(((int) (rindex1[1] + rindex2[1] + 1) >> 1) + blockvals[1]);
index[2] = crop(((int) (rindex1[2] + rindex2[2] + 1) >> 1) + blockvals[2]);
index[3] = crop(((int) (rindex1[3] + rindex2[3] + 1) >> 1) + blockvals[3]);
index[4] = crop(((int) (rindex1[4] + rindex2[4] + 1) >> 1) + blockvals[4]);
index[5] = crop(((int) (rindex1[5] + rindex2[5] + 1) >> 1) + blockvals[5]);
index[6] = crop(((int) (rindex1[6] + rindex2[6] + 1) >> 1) + blockvals[6]);
index[7] = crop(((int) (rindex1[7] + rindex2[7] + 1) >> 1) + blockvals[7]);
index += row_size;
rindex1 += row_size;
rindex2 += row_size;
index[0] = crop(((int) (rindex1[0] + rindex2[0] + 1) >> 1) + blockvals[8]);
index[1] = crop(((int) (rindex1[1] + rindex2[1] + 1) >> 1) + blockvals[9]);
index[2] = crop(((int) (rindex1[2] + rindex2[2] + 1) >> 1) + blockvals[10]);
index[3] = crop(((int) (rindex1[3] + rindex2[3] + 1) >> 1) + blockvals[11]);
index[4] = crop(((int) (rindex1[4] + rindex2[4] + 1) >> 1) + blockvals[12]);
index[5] = crop(((int) (rindex1[5] + rindex2[5] + 1) >> 1) + blockvals[13]);
index[6] = crop(((int) (rindex1[6] + rindex2[6] + 1) >> 1) + blockvals[14]);
index[7] = crop(((int) (rindex1[7] + rindex2[7] + 1) >> 1) + blockvals[15]);
blockvals += 16;
index += row_size;
rindex1 += row_size;
rindex2 += row_size;
}
} else { /* zflag */
for (rr = 0; rr < 8; rr++) {
index[0] = (int) (rindex1[0] + rindex2[0] + 1) >> 1;
index[1] = (int) (rindex1[1] + rindex2[1] + 1) >> 1;
index[2] = (int) (rindex1[2] + rindex2[2] + 1) >> 1;
index[3] = (int) (rindex1[3] + rindex2[3] + 1) >> 1;
index[4] = (int) (rindex1[4] + rindex2[4] + 1) >> 1;
index[5] = (int) (rindex1[5] + rindex2[5] + 1) >> 1;
index[6] = (int) (rindex1[6] + rindex2[6] + 1) >> 1;
index[7] = (int) (rindex1[7] + rindex2[7] + 1) >> 1;
index += row_size;
rindex1 += row_size;
rindex2 += row_size;
}
}
} else { /* qualityFlag on and both vectors are non-zero */
rindex3 = rindex1 + vid_stream->right_half_for;
rindex4 = rindex1 + (vid_stream->down_half_for * row_size);
if (!zflag) {
for (rr = 0; rr < 4; rr++) {
index[0] = crop(((int) (rindex1[0] + rindex2[0] + rindex3[0] + rindex4[0] + 2) >> 2) + blockvals[0]);
index[1] = crop(((int) (rindex1[1] + rindex2[1] + rindex3[1] + rindex4[1] + 2) >> 2) + blockvals[1]);
index[2] = crop(((int) (rindex1[2] + rindex2[2] + rindex3[2] + rindex4[2] + 2) >> 2) + blockvals[2]);
index[3] = crop(((int) (rindex1[3] + rindex2[3] + rindex3[3] + rindex4[3] + 2) >> 2) + blockvals[3]);
index[4] = crop(((int) (rindex1[4] + rindex2[4] + rindex3[4] + rindex4[4] + 2) >> 2) + blockvals[4]);
index[5] = crop(((int) (rindex1[5] + rindex2[5] + rindex3[5] + rindex4[5] + 2) >> 2) + blockvals[5]);
index[6] = crop(((int) (rindex1[6] + rindex2[6] + rindex3[6] + rindex4[6] + 2) >> 2) + blockvals[6]);
index[7] = crop(((int) (rindex1[7] + rindex2[7] + rindex3[7] + rindex4[7] + 2) >> 2) + blockvals[7]);
index += row_size;
rindex1 += row_size;
rindex2 += row_size;
rindex3 += row_size;
rindex4 += row_size;
index[0] = crop(((int) (rindex1[0] + rindex2[0] + rindex3[0] + rindex4[0] + 2) >> 2) + blockvals[8]);
index[1] = crop(((int) (rindex1[1] + rindex2[1] + rindex3[1] + rindex4[1] + 2) >> 2) + blockvals[9]);
index[2] = crop(((int) (rindex1[2] + rindex2[2] + rindex3[2] + rindex4[2] + 2) >> 2) + blockvals[10]);
index[3] = crop(((int) (rindex1[3] + rindex2[3] + rindex3[3] + rindex4[3] + 2) >> 2) + blockvals[11]);
index[4] = crop(((int) (rindex1[4] + rindex2[4] + rindex3[4] + rindex4[4] + 2) >> 2) + blockvals[12]);
index[5] = crop(((int) (rindex1[5] + rindex2[5] + rindex3[5] + rindex4[5] + 2) >> 2) + blockvals[13]);
index[6] = crop(((int) (rindex1[6] + rindex2[6] + rindex3[6] + rindex4[6] + 2) >> 2) + blockvals[14]);
index[7] = crop(((int) (rindex1[7] + rindex2[7] + rindex3[7] + rindex4[7] + 2) >> 2) + blockvals[15]);
blockvals += 16;
index += row_size;
rindex1 += row_size;
rindex2 += row_size;
rindex3 += row_size;
rindex4 += row_size;
}
} else { /* zflag */
for (rr = 0; rr < 8; rr++) {
index[0] = (int) (rindex1[0] + rindex2[0] + rindex3[0] + rindex4[0] + 2) >> 2;
index[1] = (int) (rindex1[1] + rindex2[1] + rindex3[1] + rindex4[1] + 2) >> 2;
index[2] = (int) (rindex1[2] + rindex2[2] + rindex3[2] + rindex4[2] + 2) >> 2;
index[3] = (int) (rindex1[3] + rindex2[3] + rindex3[3] + rindex4[3] + 2) >> 2;
index[4] = (int) (rindex1[4] + rindex2[4] + rindex3[4] + rindex4[4] + 2) >> 2;
index[5] = (int) (rindex1[5] + rindex2[5] + rindex3[5] + rindex4[5] + 2) >> 2;
index[6] = (int) (rindex1[6] + rindex2[6] + rindex3[6] + rindex4[6] + 2) >> 2;
index[7] = (int) (rindex1[7] + rindex2[7] + rindex3[7] + rindex4[7] + 2) >> 2;
index += row_size;
rindex1 += row_size;
rindex2 += row_size;
rindex3 += row_size;
rindex4 += row_size;
}
}
}
}
#ifdef LOOSE_MPEG
}
#endif
}
/*
*--------------------------------------------------------------
*
* ReconBMBlock --
*
* Reconstructs back predicted macroblocks.
*
* Results:
* None.
*
* Side effects:
* None.
*
*--------------------------------------------------------------
*/
static void ReconBMBlock( VidStream* vid_stream, int bnum,
int recon_right_back, int recon_down_back, int zflag )
{
int mb_row, mb_col, row, col, row_size, rr;
unsigned char *dest, *future = 0;
int right_back, down_back, right_half_back, down_half_back;
unsigned char *rindex1, *rindex2, *rindex3, *rindex4;
unsigned char *index;
short int *blockvals;
#ifdef LOOSE_MPEG
int illegalBlock = 0;
int maxx, maxy, cc;
int row_start, row_end, rlast, rfirst, col_start, col_end, clast, cfirst;
#endif
/* Calculate macroblock row and column from address. */
mb_row = vid_stream->mblock.mb_address / vid_stream->mb_width;
mb_col = vid_stream->mblock.mb_address % vid_stream->mb_width;
/* If block is luminance block... */
if (bnum < 4) {
/* Calculate right_back, down_back motion vectors. */
right_back = recon_right_back >> 1;
down_back = recon_down_back >> 1;
right_half_back = recon_right_back & 0x1;
down_half_back = recon_down_back & 0x1;
/* Set dest to luminance plane of current pict image. */
dest = vid_stream->current->luminance;
/*
* If future frame exists, set future to luminance plane of future frame.
*/
if (vid_stream->future != NULL)
future = vid_stream->future->luminance;
/* Establish row size. */
row_size = vid_stream->mb_width << 4;
/* Calculate row,col of upper left pixel in block. */
row = mb_row << 4;
col = mb_col << 4;
if (bnum > 1)
row += 8;
if (bnum % 2)
col += 8;
#ifdef LOOSE_MPEG
/* Check for block illegality. */
maxx = vid_stream->mb_width*16-1;
maxy = vid_stream->mb_height*16-1;
if (row + down_back + down_half_back + 7 > maxy) illegalBlock |= 0x4;
else if (row + down_back < 0) illegalBlock |= 0x1;
if (col + right_back + right_half_back + 7 > maxx) illegalBlock |= 0x2;
else if (col + right_back < 0) illegalBlock |= 0x8;
#endif
}
/* Otherwise, block is NOT luminance block, ... */
else {
/* Construct motion vectors. */
recon_right_back /= 2;
recon_down_back /= 2;
right_back = recon_right_back >> 1;
down_back = recon_down_back >> 1;
right_half_back = recon_right_back & 0x1;
down_half_back = recon_down_back & 0x1;
/* Establish row size. */
row_size = vid_stream->mb_width << 3;
/* Calculate row,col of upper left pixel in block. */
row = mb_row << 3;
col = mb_col << 3;
#ifdef LOOSE_MPEG
/* Check for block illegality. */
maxx = vid_stream->mb_width*8-1;
maxy = vid_stream->mb_height*8-1;
if (row + down_back + down_half_back + 7 > maxy) illegalBlock |= 0x4;
else if (row + down_back < 0) illegalBlock |= 0x1;
if (col + right_back + right_half_back + 7 > maxx) illegalBlock |= 0x2;
else if (col + right_back < 0) illegalBlock |= 0x8;
#endif
/* If block is Cr block... */
/* They were switched earlier, so 5 is first - eyhung */
if (bnum == 5) {
/* Set dest to Cr plane of current pict image. */
dest = vid_stream->current->Cr;
/*
* If future frame exists, set future to Cr plane of future image.
*/
if (vid_stream->future != NULL)
future = vid_stream->future->Cr;
}
/* Otherwise, block is Cb block... */
else {
/* Set dest to Cb plane of current pict image. */
dest = vid_stream->current->Cb;
/*
* If future frame exists, set future to Cb plane of future frame.
*/
if (vid_stream->future != NULL)
future = vid_stream->future->Cb;
}
}
/* For each pixel in block do... */
#ifdef LOOSE_MPEG
if (illegalBlock) {
if (illegalBlock & 0x1) {
row_start = 0;
row_end = row+down_back+8;
rfirst = rlast = 8 - row_end;
}
else if (illegalBlock & 0x4) {
row_start = row + down_back;
row_end = maxy+1;
rlast = row_end - row_start - 1;
rfirst = 0;
}
else {
row_start = row+down_back;
row_end = row_start+8;
rfirst = 0;
}
if (illegalBlock & 0x8) {
col_start = 0;
col_end = col + right_back + 8;
cfirst = clast = 8 - col_end;
}
else if (illegalBlock & 0x2) {
col_start = col + right_back;
col_end = maxx + 1;
clast = col_end - col_start - 1;
cfirst = 0;
}
else {
col_start = col + right_back;
col_end = col_start + 8;
cfirst = 0;
}
for (rr = row_start; rr < row_end; rr++) {
rindex1 = future + (rr * row_size) + col_start;
index = dest + ((row + rfirst) * row_size) + col + cfirst;
for (cc = col_start; cc < col_end; cc++) {
*index++ = *rindex1++;
}
}
if (illegalBlock & 0x1) {
for (rr = rlast -1; rr >=0; rr--) {
index = dest + ((row + rr) * row_size) + col;
rindex1 = dest + ((row + rlast) * row_size) + col;
for (cc = 0; cc < 8; cc ++) {
*index++ = *rindex1++;
}
}
}
else if (illegalBlock & 0x4) {
for (rr = rlast+1; rr < 8; rr++) {
index = dest + ((row + rr) * row_size) + col;
rindex1 = dest + ((row + rlast) * row_size) + col;
for (cc = 0; cc < 8; cc ++) {
*index++ = *rindex1++;
}
}
}
if (illegalBlock & 0x2) {
for (cc = clast+1; cc < 8; cc++) {
index = dest + (row * row_size) + (col + cc);
rindex1 = dest + (row * row_size) + (col + clast);
for (rr = 0; rr < 8; rr++) {
*index = *rindex1;
index += row_size;
rindex1 += row_size;
}
}
}
else if (illegalBlock & 0x8) {
for (cc = clast-1; cc >= 0; cc--) {
index = dest + (row * row_size) + (col + cc);
rindex1 = dest + (row * row_size) + (col + clast);
for (rr = 0; rr < 8; rr++) {
*index = *rindex1;
index += row_size;
rindex1 += row_size;
}
}
}
if (!zflag) {
for (rr = 0; rr < 8; rr++) {
index = dest + (row*row_size) + col;
blockvals = &(vid_stream->block.dct_recon[rr][0]);
index[0] += blockvals[0];
index[1] += blockvals[1];
index[2] += blockvals[2];
index[3] += blockvals[3];
index[4] += blockvals[4];
index[5] += blockvals[5];
index[6] += blockvals[6];
index[7] += blockvals[7];
}
}
}
else {
#endif
index = dest + (row * row_size) + col;
rindex1 = future + (row + down_back) * row_size + col + right_back;
blockvals = &(vid_stream->block.dct_recon[0][0]);
if ((!right_half_back) && (!down_half_back)) {
#ifdef USE_CROP_TABLE
unsigned char *cm = cropTbl + MAX_NEG_CROP;
#endif
if (!zflag)
for (rr = 0; rr < 4; rr++) {
index[0] = crop((int) rindex1[0] + (int) blockvals[0]);
index[1] = crop((int) rindex1[1] + (int) blockvals[1]);
index[2] = crop((int) rindex1[2] + (int) blockvals[2]);
index[3] = crop((int) rindex1[3] + (int) blockvals[3]);
index[4] = crop((int) rindex1[4] + (int) blockvals[4]);
index[5] = crop((int) rindex1[5] + (int) blockvals[5]);
index[6] = crop((int) rindex1[6] + (int) blockvals[6]);
index[7] = crop((int) rindex1[7] + (int) blockvals[7]);
index += row_size;
rindex1 += row_size;
index[0] = crop((int) rindex1[0] + (int) blockvals[8]);
index[1] = crop((int) rindex1[1] + (int) blockvals[9]);
index[2] = crop((int) rindex1[2] + (int) blockvals[10]);
index[3] = crop((int) rindex1[3] + (int) blockvals[11]);
index[4] = crop((int) rindex1[4] + (int) blockvals[12]);
index[5] = crop((int) rindex1[5] + (int) blockvals[13]);
index[6] = crop((int) rindex1[6] + (int) blockvals[14]);
index[7] = crop((int) rindex1[7] + (int) blockvals[15]);
blockvals += 16;
index += row_size;
rindex1 += row_size;
}
else {
if (right_back & 0x1) {
/* No alignment, use bye copy */
for (rr = 0; rr < 4; rr++) {
index[0] = rindex1[0];
index[1] = rindex1[1];
index[2] = rindex1[2];
index[3] = rindex1[3];
index[4] = rindex1[4];
index[5] = rindex1[5];
index[6] = rindex1[6];
index[7] = rindex1[7];
index += row_size;
rindex1 += row_size;
index[0] = rindex1[0];
index[1] = rindex1[1];
index[2] = rindex1[2];
index[3] = rindex1[3];
index[4] = rindex1[4];
index[5] = rindex1[5];
index[6] = rindex1[6];
index[7] = rindex1[7];
index += row_size;
rindex1 += row_size;
}
} else if (right_back & 0x2) {
/* Half-word bit aligned, use 16 bit copy */
short *src = (short *)rindex1;
short *dest = (short *)index;
row_size >>= 1;
for (rr = 0; rr < 4; rr++) {
dest[0] = src[0];
dest[1] = src[1];
dest[2] = src[2];
dest[3] = src[3];
dest += row_size;
src += row_size;
dest[0] = src[0];
dest[1] = src[1];
dest[2] = src[2];
dest[3] = src[3];
dest += row_size;
src += row_size;
}
} else {
/* Word aligned, use 32 bit copy */
int *src = (int *)rindex1;
int *dest = (int *)index;
row_size >>= 2;
for (rr = 0; rr < 4; rr++) {
dest[0] = src[0];
dest[1] = src[1];
dest += row_size;
src += row_size;
dest[0] = src[0];
dest[1] = src[1];
dest += row_size;
src += row_size;
}
}
}
} else {
#ifdef USE_CROP_TABLE
unsigned char *cm = cropTbl + MAX_NEG_CROP;
#endif
rindex2 = rindex1 + right_half_back + (down_half_back * row_size);
if (!qualityFlag) {
if (!zflag) {
for (rr = 0; rr < 4; rr++) {
index[0] = crop(((int) (rindex1[0] + rindex2[0] + 1) >> 1) + blockvals[0]);
index[1] = crop(((int) (rindex1[1] + rindex2[1] + 1) >> 1) + blockvals[1]);
index[2] = crop(((int) (rindex1[2] + rindex2[2] + 1) >> 1) + blockvals[2]);
index[3] = crop(((int) (rindex1[3] + rindex2[3] + 1) >> 1) + blockvals[3]);
index[4] = crop(((int) (rindex1[4] + rindex2[4] + 1) >> 1) + blockvals[4]);
index[5] = crop(((int) (rindex1[5] + rindex2[5] + 1) >> 1) + blockvals[5]);
index[6] = crop(((int) (rindex1[6] + rindex2[6] + 1) >> 1) + blockvals[6]);
index[7] = crop(((int) (rindex1[7] + rindex2[7] + 1) >> 1) + blockvals[7]);
index += row_size;
rindex1 += row_size;
rindex2 += row_size;
index[0] = crop(((int) (rindex1[0] + rindex2[0] + 1) >> 1) + blockvals[8]);
index[1] = crop(((int) (rindex1[1] + rindex2[1] + 1) >> 1) + blockvals[9]);
index[2] = crop(((int) (rindex1[2] + rindex2[2] + 1) >> 1) + blockvals[10]);
index[3] = crop(((int) (rindex1[3] + rindex2[3] + 1) >> 1) + blockvals[11]);
index[4] = crop(((int) (rindex1[4] + rindex2[4] + 1) >> 1) + blockvals[12]);
index[5] = crop(((int) (rindex1[5] + rindex2[5] + 1) >> 1) + blockvals[13]);
index[6] = crop(((int) (rindex1[6] + rindex2[6] + 1) >> 1) + blockvals[14]);
index[7] = crop(((int) (rindex1[7] + rindex2[7] + 1) >> 1) + blockvals[15]);
blockvals += 16;
index += row_size;
rindex1 += row_size;
rindex2 += row_size;
}
} else { /* zflag */
for (rr = 0; rr < 8; rr++) {
index[0] = (int) (rindex1[0] + rindex2[0] + 1) >> 1;
index[1] = (int) (rindex1[1] + rindex2[1] + 1) >> 1;
index[2] = (int) (rindex1[2] + rindex2[2] + 1) >> 1;
index[3] = (int) (rindex1[3] + rindex2[3] + 1) >> 1;
index[4] = (int) (rindex1[4] + rindex2[4] + 1) >> 1;
index[5] = (int) (rindex1[5] + rindex2[5] + 1) >> 1;
index[6] = (int) (rindex1[6] + rindex2[6] + 1) >> 1;
index[7] = (int) (rindex1[7] + rindex2[7] + 1) >> 1;
index += row_size;
rindex1 += row_size;
rindex2 += row_size;
}
}
} else { /* qualityFlag on */
rindex3 = rindex1 + right_half_back;
rindex4 = rindex1 + (down_half_back * row_size);
if (!zflag) {
for (rr = 0; rr < 4; rr++) {
index[0] = crop(((int) (rindex1[0] + rindex2[0] + rindex3[0] + rindex4[0] + 2) >> 2) + blockvals[0]);
index[1] = crop(((int) (rindex1[1] + rindex2[1] + rindex3[1] + rindex4[1] + 2) >> 2) + blockvals[1]);
index[2] = crop(((int) (rindex1[2] + rindex2[2] + rindex3[2] + rindex4[2] + 2) >> 2) + blockvals[2]);
index[3] = crop(((int) (rindex1[3] + rindex2[3] + rindex3[3] + rindex4[3] + 2) >> 2) + blockvals[3]);
index[4] = crop(((int) (rindex1[4] + rindex2[4] + rindex3[4] + rindex4[4] + 2) >> 2) + blockvals[4]);
index[5] = crop(((int) (rindex1[5] + rindex2[5] + rindex3[5] + rindex4[5] + 2) >> 2) + blockvals[5]);
index[6] = crop(((int) (rindex1[6] + rindex2[6] + rindex3[6] + rindex4[6] + 2) >> 2) + blockvals[6]);
index[7] = crop(((int) (rindex1[7] + rindex2[7] + rindex3[7] + rindex4[7] + 2) >> 2) + blockvals[7]);
index += row_size;
rindex1 += row_size;
rindex2 += row_size;
rindex3 += row_size;
rindex4 += row_size;
index[0] = crop(((int) (rindex1[0] + rindex2[0] + rindex3[0] + rindex4[0] + 2) >> 2) + blockvals[8]);
index[1] = crop(((int) (rindex1[1] + rindex2[1] + rindex3[1] + rindex4[1] + 2) >> 2) + blockvals[9]);
index[2] = crop(((int) (rindex1[2] + rindex2[2] + rindex3[2] + rindex4[2] + 2) >> 2) + blockvals[10]);
index[3] = crop(((int) (rindex1[3] + rindex2[3] + rindex3[3] + rindex4[3] + 2) >> 2) + blockvals[11]);
index[4] = crop(((int) (rindex1[4] + rindex2[4] + rindex3[4] + rindex4[4] + 2) >> 2) + blockvals[12]);
index[5] = crop(((int) (rindex1[5] + rindex2[5] + rindex3[5] + rindex4[5] + 2) >> 2) + blockvals[13]);
index[6] = crop(((int) (rindex1[6] + rindex2[6] + rindex3[6] + rindex4[6] + 2) >> 2) + blockvals[14]);
index[7] = crop(((int) (rindex1[7] + rindex2[7] + rindex3[7] + rindex4[7] + 2) >> 2) + blockvals[15]);
blockvals += 16;
index += row_size;
rindex1 += row_size;
rindex2 += row_size;
rindex3 += row_size;
rindex4 += row_size;
}
} else { /* zflag */
for (rr = 0; rr < 8; rr++) {
index[0] = (int) (rindex1[0] + rindex2[0] + rindex3[0] + rindex4[0] + 2) >> 2;
index[1] = (int) (rindex1[1] + rindex2[1] + rindex3[1] + rindex4[1] + 2) >> 2;
index[2] = (int) (rindex1[2] + rindex2[2] + rindex3[2] + rindex4[2] + 2) >> 2;
index[3] = (int) (rindex1[3] + rindex2[3] + rindex3[3] + rindex4[3] + 2) >> 2;
index[4] = (int) (rindex1[4] + rindex2[4] + rindex3[4] + rindex4[4] + 2) >> 2;
index[5] = (int) (rindex1[5] + rindex2[5] + rindex3[5] + rindex4[5] + 2) >> 2;
index[6] = (int) (rindex1[6] + rindex2[6] + rindex3[6] + rindex4[6] + 2) >> 2;
index[7] = (int) (rindex1[7] + rindex2[7] + rindex3[7] + rindex4[7] + 2) >> 2;
index += row_size;
rindex1 += row_size;
rindex2 += row_size;
rindex3 += row_size;
rindex4 += row_size;
}
}
}
}
#ifdef LOOSE_MPEG
}
#endif
}
/*
*--------------------------------------------------------------
*
* ReconBiMBlock --
*
* Reconstructs bidirectionally predicted macroblocks.
*
* Results:
* None.
*
* Side effects:
* None.
*
*--------------------------------------------------------------
*/
static void ReconBiMBlock( VidStream* vid_stream, int bnum,
int recon_right_for, int recon_down_for, int recon_right_back,
int recon_down_back, int zflag )
{
int mb_row, mb_col, row, col, row_size, rr;
unsigned char *dest, *past=NULL, *future=NULL;
int right_for, down_for, right_half_for, down_half_for;
int right_back, down_back, right_half_back, down_half_back;
unsigned char *index, *rindex1, *bindex1;
short int *blockvals;
int forw_row_start, back_row_start, forw_col_start, back_col_start;
#ifdef LOOSE_MPEG
int lmaxx = vid_stream->mb_width*16-1;
int lmaxy = vid_stream->mb_height*16-1;
int cmaxx = vid_stream->mb_width*8-1;
int cmaxy = vid_stream->mb_height*8-1;
#endif
#ifdef LOOSE_MPEG
int illegal_forw = 0;
int illegal_back = 0;
#endif
/* Calculate macroblock row and column from address. */
mb_row = vid_stream->mblock.mb_address / vid_stream->mb_width;
mb_col = vid_stream->mblock.mb_address % vid_stream->mb_width;
/* If block is luminance block... */
if (bnum < 4) {
/*
* Calculate right_for, down_for, right_half_for, down_half_for,
* right_back, down_bakc, right_half_back, and down_half_back, motion
* vectors.
*/
right_for = recon_right_for >> 1;
down_for = recon_down_for >> 1;
right_half_for = recon_right_for & 0x1;
down_half_for = recon_down_for & 0x1;
right_back = recon_right_back >> 1;
down_back = recon_down_back >> 1;
right_half_back = recon_right_back & 0x1;
down_half_back = recon_down_back & 0x1;
/* Set dest to luminance plane of current pict image. */
dest = vid_stream->current->luminance;
/* If past frame exists, set past to luminance plane of past frame. */
if (vid_stream->past != NULL)
past = vid_stream->past->luminance;
/*
* If future frame exists, set future to luminance plane of future frame.
*/
if (vid_stream->future != NULL)
future = vid_stream->future->luminance;
/* Establish row size. */
row_size = (vid_stream->mb_width << 4);
/* Calculate row,col of upper left pixel in block. */
row = (mb_row << 4);
col = (mb_col << 4);
if (bnum > 1)
row += 8;
if (bnum & 0x01)
col += 8;
#ifdef LOOSE_MPEG
/* Check for illegal pred. blocks. */
/* Illegal forward Y block, right component */
if (col + right_for + right_half_for + 7 > lmaxx)
{
if(col > lmaxx) // fix mb column
col = lmaxx & ~15;
if(col + right_for + 7 > lmaxx) // fix full component
right_for = lmaxx - col - 7;
if(col + right_for + right_half_for + 7 > lmaxx) // fix half component
right_half_for = 0;
}
else if (col + right_for < 0)
{
if(col < 0) // fix mb column
col = 0;
if(col + right_for < 0) // fix full component
right_for = 0;
}
/* Illegal forward Y block, down component */
if (row + down_for + down_half_for + 7 > lmaxy)
{
if(row > lmaxy) // fix mb row
row = lmaxy & ~15;
if(row + down_for + 7 > lmaxy) // fix full component
down_for = lmaxy - row - 7;
if(row + down_for + down_half_for + 7 > lmaxy) // fix half component
down_half_for = 0;
}
else if (row + down_for < 0)
{
if(row < 0) // fix mb row
row = 0;
if(row + down_for < 0) // fix full component
down_for = 0;
}
/* Illegal backward Y block, right component */
if (col + right_back + right_half_back + 7 > lmaxx)
{
if(col > lmaxx) // fix mb column
col = lmaxx & ~15;
if(col + right_back + 7 > lmaxx) // fix full component
right_back = lmaxx - col - 7;
if(col + right_back + right_half_back + 7 > lmaxx) // fix half component
right_half_back = 0;
}
else if (col + right_back < 0)
{
if(col < 0) // fix mb column
col = 0;
if(col + right_back < 0) // fix full component
right_back = 0;
}
/* Illegal backward Y block, down component */
if (row + down_back + down_half_back + 7 > lmaxy)
{
if(row > lmaxy) // fix mb row
row = lmaxy & ~15;
if(row + down_back + 7 > lmaxy) // fix full component
down_back = lmaxy - row - 7;
if(row + down_back + down_half_back + 7 > lmaxy) // fix half component
down_half_back = 0;
}
else if (row + down_back < 0)
{
if(row < 0) // fix mb row
row = 0;
if(row + down_back < 0) // fix full component
down_back = 0;
}
#endif
forw_col_start = col + right_for;
forw_row_start = row + down_for;
back_col_start = col + right_back;
back_row_start = row + down_back;
}
/* Otherwise, block is NOT luminance block, ... */
else {
/* Construct motion vectors. */
recon_right_for /= 2;
recon_down_for /= 2;
right_for = recon_right_for >> 1;
down_for = recon_down_for >> 1;
right_half_for = recon_right_for & 0x1;
down_half_for = recon_down_for & 0x1;
recon_right_back /= 2;
recon_down_back /= 2;
right_back = recon_right_back >> 1;
down_back = recon_down_back >> 1;
right_half_back = recon_right_back & 0x1;
down_half_back = recon_down_back & 0x1;
/* Establish row size. */
row_size = (vid_stream->mb_width << 3);
/* Calculate row,col of upper left pixel in block. */
row = (mb_row << 3);
col = (mb_col << 3);
#ifdef LOOSE_MPEG
/* Check for illegal pred. blocks. */
/* Illegal forward C block, right component */
if (col + right_for + right_half_for + 7 > cmaxx)
{
if(col > cmaxx) // fix mb column
col = cmaxx & ~15;
if(col + right_for + 7 > cmaxx) // fix full component
right_for = cmaxx - col - 7;
if(col + right_for + right_half_for + 7 > cmaxx) // fix half component
right_half_for = 0;
}
else if (col + right_for < 0)
{
if(col < 0) // fix mb column
col = 0;
if(col + right_for < 0) // fix full component
right_for = 0;
}
/* Illegal forward C block, down component */
if (row + down_for + down_half_for + 7 > cmaxy)
{
if(row > cmaxy) // fix mb row
row = cmaxy & ~15;
if(row + down_for + 7 > cmaxy) // fix full component
down_for = cmaxy - row - 7;
if(row + down_for + down_half_for + 7 > cmaxy) // fix half component
down_half_for = 0;
}
else if (row + down_for < 0)
{
if(row < 0) // fix mb row
row = 0;
if(row + down_for < 0) // fix full component
down_for = 0;
}
/* Illegal backward C block, right component */
if (col + right_back + right_half_back + 7 > cmaxx)
{
if(col > cmaxx) // fix mb column
col = cmaxx & ~15;
if(col + right_back + 7 > cmaxx) // fix full component
right_back = cmaxx - col - 7;
if(col + right_back + right_half_back + 7 > cmaxx) // fix half component
right_half_back = 0;
}
else if (col + right_back < 0)
{
if(col < 0) // fix mb column
col = 0;
if(col + right_back < 0) // fix full component
right_back = 0;
}
/* Illegal backward C block, down component */
if (row + down_back + down_half_back + 7 > cmaxy)
{
if(row > cmaxy) // fix mb row
row = cmaxy & ~15;
if(row + down_back + 7 > cmaxy) // fix full component
down_back = cmaxy - row - 7;
if(row + down_back + down_half_back + 7 > cmaxy) // fix half component
down_half_back = 0;
}
else if (row + down_back < 0)
{
if(row < 0) // fix mb row
row = 0;
if(row + down_back < 0) // fix full component
down_back = 0;
}
#endif
forw_col_start = col + right_for;
forw_row_start = row + down_for;
back_col_start = col + right_back;
back_row_start = row + down_back;
/* If block is Cr block... */
/* Switched earlier, so we test Cr first - eyhung */
if (bnum == 5) {
/* Set dest to Cr plane of current pict image. */
dest = vid_stream->current->Cr;
/* If past frame exists, set past to Cr plane of past image. */
if (vid_stream->past != NULL)
past = vid_stream->past->Cr;
/*
* If future frame exists, set future to Cr plane of future image.
*/
if (vid_stream->future != NULL)
future = vid_stream->future->Cr;
}
/* Otherwise, block is Cb block... */
else {
/* Set dest to Cb plane of current pict image. */
dest = vid_stream->current->Cb;
/* If past frame exists, set past to Cb plane of past frame. */
if (vid_stream->past != NULL)
past = vid_stream->past->Cb;
/*
* If future frame exists, set future to Cb plane of future frame.
*/
if (vid_stream->future != NULL)
future = vid_stream->future->Cb;
}
}
/* For each pixel in block... */
index = dest + (row * row_size) + col;
#ifdef LOOSE_MPEG
if (illegal_forw)
rindex1 = future + back_row_start * row_size + back_col_start;
else
#endif
rindex1 = past + forw_row_start * row_size + forw_col_start;
#ifdef LOOSE_MPEG
if (illegal_back)
bindex1 = past + forw_row_start * row_size + forw_col_start;
else
#endif
bindex1 = future + back_row_start * row_size + back_col_start;
blockvals = (short int *) &(vid_stream->block.dct_recon[0][0]);
{
#ifdef USE_CROP_TABLE
unsigned char *cm = cropTbl + MAX_NEG_CROP;
#endif
if (!zflag)
for (rr = 0; rr < 4; rr++) {
index[0] = crop(((int) (rindex1[0] + bindex1[0]) >> 1) + blockvals[0]);
index[1] = crop(((int) (rindex1[1] + bindex1[1]) >> 1) + blockvals[1]);
index[2] = crop(((int) (rindex1[2] + bindex1[2]) >> 1) + blockvals[2]);
index[3] = crop(((int) (rindex1[3] + bindex1[3]) >> 1) + blockvals[3]);
index[4] = crop(((int) (rindex1[4] + bindex1[4]) >> 1) + blockvals[4]);
index[5] = crop(((int) (rindex1[5] + bindex1[5]) >> 1) + blockvals[5]);
index[6] = crop(((int) (rindex1[6] + bindex1[6]) >> 1) + blockvals[6]);
index[7] = crop(((int) (rindex1[7] + bindex1[7]) >> 1) + blockvals[7]);
index += row_size;
rindex1 += row_size;
bindex1 += row_size;
index[0] = crop(((int) (rindex1[0] + bindex1[0]) >> 1) + blockvals[8]);
index[1] = crop(((int) (rindex1[1] + bindex1[1]) >> 1) + blockvals[9]);
index[2] = crop(((int) (rindex1[2] + bindex1[2]) >> 1) + blockvals[10]);
index[3] = crop(((int) (rindex1[3] + bindex1[3]) >> 1) + blockvals[11]);
index[4] = crop(((int) (rindex1[4] + bindex1[4]) >> 1) + blockvals[12]);
index[5] = crop(((int) (rindex1[5] + bindex1[5]) >> 1) + blockvals[13]);
index[6] = crop(((int) (rindex1[6] + bindex1[6]) >> 1) + blockvals[14]);
index[7] = crop(((int) (rindex1[7] + bindex1[7]) >> 1) + blockvals[15]);
blockvals += 16;
index += row_size;
rindex1 += row_size;
bindex1 += row_size;
}
else
for (rr = 0; rr < 4; rr++) {
index[0] = (int) (rindex1[0] + bindex1[0]) >> 1;
index[1] = (int) (rindex1[1] + bindex1[1]) >> 1;
index[2] = (int) (rindex1[2] + bindex1[2]) >> 1;
index[3] = (int) (rindex1[3] + bindex1[3]) >> 1;
index[4] = (int) (rindex1[4] + bindex1[4]) >> 1;
index[5] = (int) (rindex1[5] + bindex1[5]) >> 1;
index[6] = (int) (rindex1[6] + bindex1[6]) >> 1;
index[7] = (int) (rindex1[7] + bindex1[7]) >> 1;
index += row_size;
rindex1 += row_size;
bindex1 += row_size;
index[0] = (int) (rindex1[0] + bindex1[0]) >> 1;
index[1] = (int) (rindex1[1] + bindex1[1]) >> 1;
index[2] = (int) (rindex1[2] + bindex1[2]) >> 1;
index[3] = (int) (rindex1[3] + bindex1[3]) >> 1;
index[4] = (int) (rindex1[4] + bindex1[4]) >> 1;
index[5] = (int) (rindex1[5] + bindex1[5]) >> 1;
index[6] = (int) (rindex1[6] + bindex1[6]) >> 1;
index[7] = (int) (rindex1[7] + bindex1[7]) >> 1;
index += row_size;
rindex1 += row_size;
bindex1 += row_size;
}
}
}
#endif /* USE_ATI */
/*
*--------------------------------------------------------------
*
* ProcessSkippedPFrameMBlocks --
*
* Processes skipped macroblocks in P frames.
*
* Results:
* Calculates pixel values for luminance, Cr, and Cb planes
* in current pict image for skipped macroblocks.
*
* Side effects:
* Pixel values in pict image changed.
*
*--------------------------------------------------------------
*/
static void ProcessSkippedPFrameMBlocks( VidStream* vid_stream )
{
int row_size, half_row, mb_row, mb_col, row, col, rr;
int addr, row_incr, half_row_incr, crow, ccol;
int *dest, *src, *dest1, *src1;
#ifndef DISABLE_DITHER
int ditherType=vid_stream->ditherType;
#endif
/* Calculate row sizes for luminance and Cr/Cb macroblock areas. */
row_size = vid_stream->mb_width << 4;
half_row = (row_size >> 1);
row_incr = row_size >> 2;
half_row_incr = half_row >> 2;
/* For each skipped macroblock, do... */
for (addr = vid_stream->mblock.past_mb_addr + 1;
addr < vid_stream->mblock.mb_address; addr++) {
/* Calculate macroblock row and col. */
mb_row = addr / vid_stream->mb_width;
mb_col = addr % vid_stream->mb_width;
/* Calculate upper left pixel row,col for luminance plane. */
row = mb_row << 4;
col = mb_col << 4;
#ifdef USE_ATI
vhar128_macroblock(vid_stream->ati_handle,
col,
row,
0, /* skipped block are empty non-intra blocks */
1, 0, /* P frames are based on past picture */
0, 0, /* backward motion is null */
0, 0, /* forward motion is null */
vid_stream->block.dct_recon);
#else
/* For each row in macroblock luminance plane... */
dest = (int *)(vid_stream->current->luminance + (row * row_size) + col);
src = (int *)(vid_stream->future->luminance + (row * row_size) + col);
for (rr = 0; rr < 8; rr++) {
/* Copy pixel values from last I or P picture. */
dest[0] = src[0];
dest[1] = src[1];
dest[2] = src[2];
dest[3] = src[3];
dest += row_incr;
src += row_incr;
dest[0] = src[0];
dest[1] = src[1];
dest[2] = src[2];
dest[3] = src[3];
dest += row_incr;
src += row_incr;
}
/*
* Divide row,col to get upper left pixel of macroblock in Cr and Cb
* planes.
*/
crow = row >> 1;
ccol = col >> 1;
/* For each row in Cr, and Cb planes... */
dest = (int *)(vid_stream->current->Cr + (crow * half_row) + ccol);
src = (int *)(vid_stream->future->Cr + (crow * half_row) + ccol);
dest1 = (int *)(vid_stream->current->Cb + (crow * half_row) + ccol);
src1 = (int *)(vid_stream->future->Cb + (crow * half_row) + ccol);
for (rr = 0; rr < 4; rr++) {
/* Copy pixel values from last I or P picture. */
dest[0] = src[0];
dest[1] = src[1];
dest1[0] = src1[0];
dest1[1] = src1[1];
dest += half_row_incr;
src += half_row_incr;
dest1 += half_row_incr;
src1 += half_row_incr;
dest[0] = src[0];
dest[1] = src[1];
dest1[0] = src1[0];
dest1[1] = src1[1];
dest += half_row_incr;
src += half_row_incr;
dest1 += half_row_incr;
src1 += half_row_incr;
}
#ifndef DISABLE_DITHER
if (ditherType == MBORDERED_DITHER) {
MBOrderedDitherDisplayCopy(vid_stream, addr,
1, 0, 0, 0, 0, 0,
vid_stream->future->display,
(unsigned char *) NULL);
vid_stream->ditherFlags[addr] = 0;
}
#endif
#endif /* USE_ATI */
}
vid_stream->mblock.recon_right_for_prev = 0;
vid_stream->mblock.recon_down_for_prev = 0;
}
/*
*--------------------------------------------------------------
*
* ProcessSkippedBFrameMBlocks --
*
* Processes skipped macroblocks in B frames.
*
* Results:
* Calculates pixel values for luminance, Cr, and Cb planes
* in current pict image for skipped macroblocks.
*
* Side effects:
* Pixel values in pict image changed.
*
*--------------------------------------------------------------
*/
static void ProcessSkippedBFrameMBlocks( VidStream* vid_stream )
{
int row_size, half_row, mb_row, mb_col, row, col, rr;
int right_half_for = 0, down_half_for = 0;
int c_right_half_for = 0, c_down_half_for = 0;
int right_half_back = 0, down_half_back = 0;
int c_right_half_back = 0, c_down_half_back = 0;
int addr, right_for = 0, down_for = 0;
int recon_right_for, recon_down_for;
int recon_right_back, recon_down_back;
int right_back = 0, down_back = 0;
int c_right_for = 0, c_down_for = 0;
int c_right_back = 0, c_down_back = 0;
unsigned char forw_lum[256];
unsigned char forw_cr[64], forw_cb[64];
unsigned char back_lum[256], back_cr[64], back_cb[64];
int row_incr, half_row_incr;
int ccol, crow;
#ifndef DISABLE_DITHER
int ditherType=vid_stream->ditherType;
#endif
#ifdef LOOSE_MPEG
int lmaxx = vid_stream->mb_width*16-1;
int lmaxy = vid_stream->mb_height*16-1;
int cmaxx = vid_stream->mb_width*8-1;
int cmaxy = vid_stream->mb_height*8-1;
#endif
/* Calculate row sizes for luminance and Cr/Cb macroblock areas. */
row_size = vid_stream->mb_width << 4;
half_row = (row_size >> 1);
row_incr = row_size >> 2;
half_row_incr = half_row >> 2;
/* Establish motion vector codes based on full pixel flag. */
if (vid_stream->picture.full_pel_forw_vector) {
recon_right_for = vid_stream->mblock.recon_right_for_prev << 1;
recon_down_for = vid_stream->mblock.recon_down_for_prev << 1;
} else {
recon_right_for = vid_stream->mblock.recon_right_for_prev;
recon_down_for = vid_stream->mblock.recon_down_for_prev;
}
if (vid_stream->picture.full_pel_back_vector) {
recon_right_back = vid_stream->mblock.recon_right_back_prev << 1;
recon_down_back = vid_stream->mblock.recon_down_back_prev << 1;
} else {
recon_right_back = vid_stream->mblock.recon_right_back_prev;
recon_down_back = vid_stream->mblock.recon_down_back_prev;
}
#ifdef USE_ATI
/* For each skipped macroblock do ... */
for (addr = vid_stream->mblock.past_mb_addr + 1;
addr < vid_stream->mblock.mb_address; addr++) {
vhar128_macroblock(vid_stream->ati_handle,
(addr % vid_stream->mb_width) << 4,
(addr / vid_stream->mb_width) << 4,
0, /* skipped blocks are empty non-intra blocks */
vid_stream->mblock.bpict_past_back,
vid_stream->mblock.bpict_past_forw,
recon_right_back, recon_down_back, /* backward motion */
recon_right_for, recon_down_for, /* forward motion */
vid_stream->block.dct_recon);
}
#else
/* If only one motion vector, do display copy, else do full
calculation.
*/
#ifndef DISABLE_DITHER
if (ditherType == MBORDERED_DITHER) {
if (vid_stream->mblock.bpict_past_forw &&
!vid_stream->mblock.bpict_past_back) {
for (addr = vid_stream->mblock.past_mb_addr+1;
addr < vid_stream->mblock.mb_address; addr++) {
MBOrderedDitherDisplayCopy(vid_stream, addr,
1, recon_right_for, recon_down_for,
0, 0, 0, vid_stream->past->display,
vid_stream->future->display);
vid_stream->ditherFlags[addr] = 0;
}
return;
}
if (vid_stream->mblock.bpict_past_back &&
!vid_stream->mblock.bpict_past_forw) {
for (addr = vid_stream->mblock.past_mb_addr+1;
addr < vid_stream->mblock.mb_address; addr++) {
MBOrderedDitherDisplayCopy(vid_stream, addr,
0, 0, 0,
1, recon_right_back, recon_down_back,
vid_stream->past->display, vid_stream->future->display);
vid_stream->ditherFlags[addr] = 0;
}
return;
}
}
#endif
/* Calculate motion vectors. */
if (vid_stream->mblock.bpict_past_forw) {
right_for = recon_right_for >> 1;
down_for = recon_down_for >> 1;
right_half_for = recon_right_for & 0x1;
down_half_for = recon_down_for & 0x1;
recon_right_for /= 2;
recon_down_for /= 2;
c_right_for = recon_right_for >> 1;
c_down_for = recon_down_for >> 1;
c_right_half_for = recon_right_for & 0x1;
c_down_half_for = recon_down_for & 0x1;
}
if (vid_stream->mblock.bpict_past_back) {
right_back = recon_right_back >> 1;
down_back = recon_down_back >> 1;
right_half_back = recon_right_back & 0x1;
down_half_back = recon_down_back & 0x1;
recon_right_back /= 2;
recon_down_back /= 2;
c_right_back = recon_right_back >> 1;
c_down_back = recon_down_back >> 1;
c_right_half_back = recon_right_back & 0x1;
c_down_half_back = recon_down_back & 0x1;
}
/* For each skipped macroblock, do... */
for (addr = vid_stream->mblock.past_mb_addr + 1;
addr < vid_stream->mblock.mb_address; addr++) {
/* Calculate macroblock row and col. */
mb_row = addr / vid_stream->mb_width;
mb_col = addr % vid_stream->mb_width;
/* Calculate upper left pixel row,col for luminance plane. */
row = mb_row << 4;
col = mb_col << 4;
crow = row / 2;
ccol = col / 2;
#ifdef LOOSE_MPEG
/* Check for illegal blocks. */
/* Illegal forward Y block, right component */
if (col + right_for + right_half_for + 7 > lmaxx)
{
if(col > lmaxx) // fix mb column
col = lmaxx & ~15;
if(col + right_for + 7 > lmaxx) // fix full component
right_for = lmaxx - col - 7;
if(col + right_for + right_half_for + 7 > lmaxx) // fix half component
right_half_for = 0;
}
else if (col + right_for < 0)
{
if(col < 0) // fix mb column
col = 0;
if(col + right_for < 0) // fix full component
right_for = 0;
}
/* Illegal forward Y block, down component */
if (row + down_for + down_half_for + 7 > lmaxy)
{
if(row > lmaxy) // fix mb row
row = lmaxy & ~15;
if(row + down_for + 7 > lmaxy) // fix full component
down_for = lmaxy - row - 7;
if(row + down_for + down_half_for + 7 > lmaxy) // fix half component
down_half_for = 0;
}
else if (row + down_for < 0)
{
if(row < 0) // fix mb row
row = 0;
if(row + down_for < 0) // fix full component
down_for = 0;
}
/* Illegal backward Y block, right component */
if (col + right_back + right_half_back + 7 > lmaxx)
{
if(col > lmaxx) // fix mb column
col = lmaxx & ~15;
if(col + right_back + 7 > lmaxx) // fix full component
right_back = lmaxx - col - 7;
if(col + right_back + right_half_back + 7 > lmaxx) // fix half component
right_half_back = 0;
}
else if (col + right_back < 0)
{
if(col < 0) // fix mb column
col = 0;
if(col + right_back < 0) // fix full component
right_back = 0;
}
/* Illegal backward Y block, down component */
if (row + down_back + down_half_back + 7 > lmaxy)
{
if(row > lmaxy) // fix mb row
row = lmaxy & ~15;
if(row + down_back + 7 > lmaxy) // fix full component
down_back = lmaxy - row - 7;
if(row + down_back + down_half_back + 7 > lmaxy) // fix half component
down_half_back = 0;
}
else if (row + down_back < 0)
{
if(row < 0) // fix mb row
row = 0;
if(row + down_back < 0) // fix full component
down_back = 0;
}
/* Illegal forward C block, right component */
if (ccol + c_right_for + c_right_half_for + 7 > cmaxx)
{
if(ccol > cmaxx) // fix mb column
ccol = cmaxx & ~15;
if(ccol + c_right_for + 7 > cmaxx) // fix full component
c_right_for = cmaxx - ccol - 7;
if(ccol + c_right_for + c_right_half_for + 7 > cmaxx) // fix half component
c_right_half_for = 0;
}
else if (ccol + c_right_for < 0)
{
if(ccol < 0) // fix mb column
ccol = 0;
if(ccol + c_right_for < 0) // fix full component
c_right_for = 0;
}
/* Illegal forward C block, down component */
if (crow + c_down_for + c_down_half_for + 7 > cmaxy)
{
if(crow > cmaxy) // fix mb row
crow = cmaxy & ~15;
if(crow + c_down_for + 7 > cmaxy) // fix full component
c_down_for = cmaxy - crow - 7;
if(crow + c_down_for + c_down_half_for + 7 > cmaxy) // fix half component
c_down_half_for = 0;
}
else if (crow + c_down_for < 0)
{
if(crow < 0) // fix mb row
crow = 0;
if(crow + c_down_for < 0) // fix full component
c_down_for = 0;
}
/* Illegal backward C block, right component */
if (ccol + c_right_back + c_right_half_back + 7 > cmaxx)
{
if(ccol > cmaxx) // fix mb column
ccol = cmaxx & ~15;
if(ccol + c_right_back + 7 > cmaxx) // fix full component
c_right_back = cmaxx - ccol - 7;
if(ccol + c_right_back + c_right_half_back + 7 > cmaxx) // fix half component
c_right_half_back = 0;
}
else if (ccol + c_right_back < 0)
{
if(ccol < 0) // fix mb column
ccol = 0;
if(ccol + c_right_back < 0) // fix full component
c_right_back = 0;
}
/* Illegal backward C block, down component */
if (crow + c_down_back + c_down_half_back + 7 > cmaxy)
{
if(crow > cmaxy) // fix mb row
crow = cmaxy & ~15;
if(crow + c_down_back + 7 > cmaxy) // fix full component
c_down_back = cmaxy - crow - 7;
if(crow + c_down_back + c_down_half_back + 7 > cmaxy) // fix half component
c_down_half_back = 0;
}
else if (crow + c_down_back < 0)
{
if(crow < 0) // fix mb row
crow = 0;
if(crow + c_down_back < 0) // fix full component
c_down_back = 0;
}
#endif
/* If forward predicted, calculate prediction values. */
if (vid_stream->mblock.bpict_past_forw) {
ReconSkippedBlock(vid_stream->past->luminance, forw_lum,
row, col, row_size, right_for, down_for,
right_half_for, down_half_for, 16);
ReconSkippedBlock(vid_stream->past->Cr, forw_cr, crow,
ccol, half_row,
c_right_for, c_down_for, c_right_half_for, c_down_half_for, 8);
ReconSkippedBlock(vid_stream->past->Cb, forw_cb, crow,
ccol, half_row,
c_right_for, c_down_for, c_right_half_for, c_down_half_for, 8);
}
/* If back predicted, calculate prediction values. */
if (vid_stream->mblock.bpict_past_back) {
ReconSkippedBlock(vid_stream->future->luminance, back_lum,
row, col, row_size, right_back, down_back,
right_half_back, down_half_back, 16);
ReconSkippedBlock(vid_stream->future->Cr, back_cr, crow,
ccol, half_row,
c_right_back, c_down_back,
c_right_half_back, c_down_half_back, 8);
ReconSkippedBlock(vid_stream->future->Cb, back_cb, crow,
ccol, half_row,
c_right_back, c_down_back,
c_right_half_back, c_down_half_back, 8);
}
if (vid_stream->mblock.bpict_past_forw &&
!vid_stream->mblock.bpict_past_back) {
int *dest, *dest1;
int *src, *src1;
dest = (int *)(vid_stream->current->luminance + (row * row_size) + col);
src = (int *)forw_lum;
for (rr = 0; rr < 16; rr++) {
/* memcpy(dest, forw_lum+(rr<<4), 16); */
dest[0] = src[0];
dest[1] = src[1];
dest[2] = src[2];
dest[3] = src[3];
dest += row_incr;
src += 4;
}
dest = (int *)(vid_stream->current->Cr + (crow * half_row) + ccol);
dest1 = (int *)(vid_stream->current->Cb + (crow * half_row) + ccol);
src = (int *)forw_cr;
src1 = (int *)forw_cb;
for (rr = 0; rr < 8; rr++) {
/*
* memcpy(dest, forw_cr+(rr<<3), 8); memcpy(dest1, forw_cb+(rr<<3),
* 8);
*/
dest[0] = src[0];
dest[1] = src[1];
dest1[0] = src1[0];
dest1[1] = src1[1];
dest += half_row_incr;
dest1 += half_row_incr;
src += 2;
src1 += 2;
}
} else if (vid_stream->mblock.bpict_past_back &&
!vid_stream->mblock.bpict_past_forw) {
int *src, *src1;
int *dest, *dest1;
dest = (int *)(vid_stream->current->luminance + (row * row_size) + col);
src = (int *)back_lum;
for (rr = 0; rr < 16; rr++) {
dest[0] = src[0];
dest[1] = src[1];
dest[2] = src[2];
dest[3] = src[3];
dest += row_incr;
src += 4;
}
dest = (int *)(vid_stream->current->Cr + (crow * half_row) + ccol);
dest1 = (int *)(vid_stream->current->Cb + (crow * half_row) + ccol);
src = (int *)back_cr;
src1 = (int *)back_cb;
for (rr = 0; rr < 8; rr++) {
/*
* memcpy(dest, back_cr+(rr<<3), 8); memcpy(dest1, back_cb+(rr<<3),
* 8);
*/
dest[0] = src[0];
dest[1] = src[1];
dest1[0] = src1[0];
dest1[1] = src1[1];
dest += half_row_incr;
dest1 += half_row_incr;
src += 2;
src1 += 2;
}
} else {
unsigned char *src1, *src2, *src1a, *src2a;
unsigned char *dest, *dest1;
dest = vid_stream->current->luminance + (row * row_size) + col;
src1 = forw_lum;
src2 = back_lum;
for (rr = 0; rr < 16; rr++) {
dest[0] = (int) (src1[0] + src2[0]) >> 1;
dest[1] = (int) (src1[1] + src2[1]) >> 1;
dest[2] = (int) (src1[2] + src2[2]) >> 1;
dest[3] = (int) (src1[3] + src2[3]) >> 1;
dest[4] = (int) (src1[4] + src2[4]) >> 1;
dest[5] = (int) (src1[5] + src2[5]) >> 1;
dest[6] = (int) (src1[6] + src2[6]) >> 1;
dest[7] = (int) (src1[7] + src2[7]) >> 1;
dest[8] = (int) (src1[8] + src2[8]) >> 1;
dest[9] = (int) (src1[9] + src2[9]) >> 1;
dest[10] = (int) (src1[10] + src2[10]) >> 1;
dest[11] = (int) (src1[11] + src2[11]) >> 1;
dest[12] = (int) (src1[12] + src2[12]) >> 1;
dest[13] = (int) (src1[13] + src2[13]) >> 1;
dest[14] = (int) (src1[14] + src2[14]) >> 1;
dest[15] = (int) (src1[15] + src2[15]) >> 1;
dest += row_size;
src1 += 16;
src2 += 16;
}
dest = vid_stream->current->Cr + (crow * half_row) + ccol;
dest1 = vid_stream->current->Cb + (crow * half_row) + ccol;
src1 = forw_cr;
src2 = back_cr;
src1a = forw_cb;
src2a = back_cb;
for (rr = 0; rr < 8; rr++) {
dest[0] = (int) (src1[0] + src2[0]) >> 1;
dest[1] = (int) (src1[1] + src2[1]) >> 1;
dest[2] = (int) (src1[2] + src2[2]) >> 1;
dest[3] = (int) (src1[3] + src2[3]) >> 1;
dest[4] = (int) (src1[4] + src2[4]) >> 1;
dest[5] = (int) (src1[5] + src2[5]) >> 1;
dest[6] = (int) (src1[6] + src2[6]) >> 1;
dest[7] = (int) (src1[7] + src2[7]) >> 1;
dest += half_row;
src1 += 8;
src2 += 8;
dest1[0] = (int) (src1a[0] + src2a[0]) >> 1;
dest1[1] = (int) (src1a[1] + src2a[1]) >> 1;
dest1[2] = (int) (src1a[2] + src2a[2]) >> 1;
dest1[3] = (int) (src1a[3] + src2a[3]) >> 1;
dest1[4] = (int) (src1a[4] + src2a[4]) >> 1;
dest1[5] = (int) (src1a[5] + src2a[5]) >> 1;
dest1[6] = (int) (src1a[6] + src2a[6]) >> 1;
dest1[7] = (int) (src1a[7] + src2a[7]) >> 1;
dest1 += half_row;
src1a += 8;
src2a += 8;
}
}
#ifndef DISABLE_DITHER
if (ditherType == MBORDERED_DITHER) {
vid_stream->ditherFlags[addr] = 1;
}
#endif
}
#endif /* USE_ATI */
}
#ifndef USE_ATI
/*
*--------------------------------------------------------------
*
* ReconSkippedBlock --
*
* Reconstructs predictive block for skipped macroblocks
* in B Frames.
*
* Results:
* No return values.
*
* Side effects:
* None.
*
*--------------------------------------------------------------
*/
static void ReconSkippedBlock( unsigned char *source, unsigned char *dest,
int row, int col, int row_size, int right, int down,
int right_half, int down_half, int width )
{
int rr;
unsigned char *source2;
source += ((row + down) * row_size) + col + right;
if (width == 16) {
if ((!right_half) && (!down_half)) {
if (right & 0x1) {
/* No alignment, use bye copy */
for (rr = 0; rr < 16; rr++) {
dest[0] = source[0];
dest[1] = source[1];
dest[2] = source[2];
dest[3] = source[3];
dest[4] = source[4];
dest[5] = source[5];
dest[6] = source[6];
dest[7] = source[7];
dest[8] = source[8];
dest[9] = source[9];
dest[10] = source[10];
dest[11] = source[11];
dest[12] = source[12];
dest[13] = source[13];
dest[14] = source[14];
dest[15] = source[15];
dest += 16;
source += row_size;
}
} else if (right & 0x2) {
/* Half-word bit aligned, use 16 bit copy */
short *src = (short *)source;
short *d = (short *)dest;
row_size >>= 1;
for (rr = 0; rr < 16; rr++) {
d[0] = src[0];
d[1] = src[1];
d[2] = src[2];
d[3] = src[3];
d[4] = src[4];
d[5] = src[5];
d[6] = src[6];
d[7] = src[7];
d += 8;
src += row_size;
}
} else {
/* Word aligned, use 32 bit copy */
int *src = (int *)source;
int *d = (int *)dest;
row_size >>= 2;
for (rr = 0; rr < 16; rr++) {
d[0] = src[0];
d[1] = src[1];
d[2] = src[2];
d[3] = src[3];
d += 4;
src += row_size;
}
}
} else {
source2 = source + right_half + (row_size * down_half);
for (rr = 0; rr < width; rr++) {
dest[0] = (int) (source[0] + source2[0]) >> 1;
dest[1] = (int) (source[1] + source2[1]) >> 1;
dest[2] = (int) (source[2] + source2[2]) >> 1;
dest[3] = (int) (source[3] + source2[3]) >> 1;
dest[4] = (int) (source[4] + source2[4]) >> 1;
dest[5] = (int) (source[5] + source2[5]) >> 1;
dest[6] = (int) (source[6] + source2[6]) >> 1;
dest[7] = (int) (source[7] + source2[7]) >> 1;
dest[8] = (int) (source[8] + source2[8]) >> 1;
dest[9] = (int) (source[9] + source2[9]) >> 1;
dest[10] = (int) (source[10] + source2[10]) >> 1;
dest[11] = (int) (source[11] + source2[11]) >> 1;
dest[12] = (int) (source[12] + source2[12]) >> 1;
dest[13] = (int) (source[13] + source2[13]) >> 1;
dest[14] = (int) (source[14] + source2[14]) >> 1;
dest[15] = (int) (source[15] + source2[15]) >> 1;
dest += width;
source += row_size;
source2 += row_size;
}
}
} else { /* (width == 8) */
assert(width == 8);
if ((!right_half) && (!down_half)) {
if (right & 0x1) {
for (rr = 0; rr < width; rr++) {
dest[0] = source[0];
dest[1] = source[1];
dest[2] = source[2];
dest[3] = source[3];
dest[4] = source[4];
dest[5] = source[5];
dest[6] = source[6];
dest[7] = source[7];
dest += 8;
source += row_size;
}
} else if (right & 0x02) {
short *d = (short *)dest;
short *src = (short *)source;
row_size >>= 1;
for (rr = 0; rr < width; rr++) {
d[0] = src[0];
d[1] = src[1];
d[2] = src[2];
d[3] = src[3];
d += 4;
src += row_size;
}
} else {
int *d = (int *)dest;
int *src = (int *)source;
row_size >>= 2;
for (rr = 0; rr < width; rr++) {
d[0] = src[0];
d[1] = src[1];
d += 2;
src += row_size;
}
}
} else {
source2 = source + right_half + (row_size * down_half);
for (rr = 0; rr < width; rr++) {
dest[0] = (int) (source[0] + source2[0]) >> 1;
dest[1] = (int) (source[1] + source2[1]) >> 1;
dest[2] = (int) (source[2] + source2[2]) >> 1;
dest[3] = (int) (source[3] + source2[3]) >> 1;
dest[4] = (int) (source[4] + source2[4]) >> 1;
dest[5] = (int) (source[5] + source2[5]) >> 1;
dest[6] = (int) (source[6] + source2[6]) >> 1;
dest[7] = (int) (source[7] + source2[7]) >> 1;
dest += width;
source += row_size;
source2 += row_size;
}
}
}
}
#endif /* USE_ATI */
/*
*--------------------------------------------------------------
*
* DoPictureDisplay --
*
* Converts image from Lum, Cr, Cb to colormap space. Puts
* image in lum plane. Updates past and future frame
* pointers. Dithers image. Sends to display mechanism.
*
* Results:
* Pict image structure locked if displaying or if frame
* is needed as past or future reference.
*
* Side effects:
* Lum plane pummelled.
*
*--------------------------------------------------------------
*/
static void DoPictureDisplay( VidStream *vid_stream )
{
#ifdef ANALYSIS
EndTime();
stat_a[0].totsize += bitCountRead() - pictureSizeCount;
#ifdef SHOWEACHFLAG
PrintOneStat();
#endif
CollectStats();
#endif
#ifdef USE_ATI
/* Flush the macroblocks to the hardware decoder */
vhar128_flush( vid_stream->ati_handle );
#endif
/* Update past and future references if needed. */
if( (vid_stream->picture.code_type == I_TYPE) ||
(vid_stream->picture.code_type == P_TYPE) )
{
if( vid_stream->future == NULL )
{
vid_stream->future = vid_stream->current;
vid_stream->future->locked |= FUTURE_LOCK;
}
else
{
if( vid_stream->past != NULL )
{
vid_stream->past->locked &= ~PAST_LOCK;
}
vid_stream->past = vid_stream->future;
vid_stream->past->locked &= ~FUTURE_LOCK;
vid_stream->past->locked |= PAST_LOCK;
vid_stream->future = vid_stream->current;
vid_stream->future->locked |= FUTURE_LOCK;
vid_stream->current = vid_stream->past;
vid_stream->_smpeg->ExecuteDisplay( vid_stream );
}
}
else
{
vid_stream->_smpeg->ExecuteDisplay( vid_stream );
}
}
/* EOF */
|