summaryrefslogtreecommitdiff
path: root/src/legacy/i810/xvmc/I810XvMC.c
blob: e6b63d301c464025e1cd2a428b617628564f72db (plain)
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
/***************************************************************************

Copyright 2001 Intel Corporation.  All Rights Reserved.

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sub license, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice (including the
next paragraph) shall be included in all copies or substantial portions
of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
IN NO EVENT SHALL INTEL, AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
THE USE OR OTHER DEALINGS IN THE SOFTWARE.

**************************************************************************/

/*************************************************************************
** File libI810XvMC.c
**
** Authors:
**      Matt Sottek <matthew.j.sottek@intel.com>
**      Bob Paauwe  <bob.j.paauwe@intel.com>
**
**
***************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <fcntl.h>
#include <dirent.h>
#include <string.h>

#include <sys/ioctl.h>
#include <X11/Xlibint.h>
#include <fourcc.h>
#include <X11/extensions/Xv.h>
#include <X11/extensions/Xvlib.h>
#include <X11/extensions/XvMC.h>
#include <X11/extensions/XvMClib.h>
#include "I810XvMC.h"

static int error_base;
static int event_base;

/***************************************************************************
// Function: i810_get_free_buffer
// Description: Allocates a free dma page using kernel ioctls, then
//   programs the data into the already allocated dma buffer list.
// Arguments: pI810XvMC private data structure from the current context.
// Notes: We faked the drmMapBufs for the i810's security so now we have
//   to insert an allocated page into the correct spot in the faked
//   list to keep up appearences.
//   Concept for this function was taken from Mesa sources.
// Returns: drmBufPtr containing the information about the allocated page.
***************************************************************************/
drmBufPtr i810_get_free_buffer(i810XvMCContext *pI810XvMC) {
   drmI810DMA dma;
   drmBufPtr buf;

   dma.granted = 0;
   dma.request_size = 4096;
   while(!dma.granted) {
     if(GET_BUFFER(pI810XvMC, dma) || !dma.granted)
       FLUSH(pI810XvMC);
   } /* No DMA granted */

   buf = &(pI810XvMC->dmabufs->list[dma.request_idx]);
   buf->idx = dma.request_idx;
   buf->used = 0;
   buf->total = dma.request_size;
   buf->address = (drmAddress)dma.virtual;
   return buf;
}

/***************************************************************************
// Function: free_privContext
// Description: Free's the private context structure if the reference
//  count is 0.
***************************************************************************/
void i810_free_privContext(i810XvMCContext *pI810XvMC) {

  I810_LOCK(pI810XvMC,DRM_LOCK_QUIESCENT);


  pI810XvMC->ref--;
  if(!pI810XvMC->ref) {
    drmUnmapBufs(pI810XvMC->dmabufs);
    drmUnmap(pI810XvMC->overlay.address,pI810XvMC->overlay.size);
    drmUnmap(pI810XvMC->surfaces.address,pI810XvMC->surfaces.size);
    drmClose(pI810XvMC->fd);

    free(pI810XvMC->dmabufs->list);
    free(pI810XvMC);
  }

  I810_UNLOCK(pI810XvMC);
}


/***************************************************************************
// Function: XvMCCreateContext
// Description: Create a XvMC context for the given surface parameters.
// Arguments:
//   display - Connection to the X server.
//   port - XvPortID to use as avertised by the X connection.
//   surface_type_id - Unique identifier for the Surface type.
//   width - Width of the surfaces.
//   height - Height of the surfaces.
//   flags - one or more of the following
//      XVMC_DIRECT - A direct rendered context is requested.
//
// Notes: surface_type_id and width/height parameters must match those
//        returned by XvMCListSurfaceTypes.
// Returns: Status
***************************************************************************/
_X_EXPORT Status XvMCCreateContext(Display *display, XvPortID port,
			 int surface_type_id, int width, int height, int flags,
			 XvMCContext *context) {  
  i810XvMCContext *pI810XvMC;
  int priv_count;
  uint *priv_data;
  uint magic;
  Status ret;
  int major, minor;

  /* Verify Obvious things first */
  if(context == NULL) {
    return XvMCBadContext;
  }

  if(!(flags & XVMC_DIRECT)) {
    /* Indirect */
    printf("Indirect Rendering not supported!\nUsing Direct.");
  }

  /* Limit use to root for now */
  if(geteuid()) {
    printf("Use of XvMC on i810 is currently limited to root\n");
    return BadAccess;
  }

  /* FIXME: Check $DISPLAY for legal values here */

  context->surface_type_id = surface_type_id;
  context->width = (unsigned short)width;
  context->height = (unsigned short)height;
  context->flags = flags;
  context->port = port;
  /* 
     Width, Height, and flags are checked against surface_type_id
     and port for validity inside the X server, no need to check
     here.
  */

  /* Allocate private Context data */
  context->privData = (void *)malloc(sizeof(i810XvMCContext));
  if(!context->privData) {
    printf("Unable to allocate resources for XvMC context.\n");
    return BadAlloc;
  }
  pI810XvMC = (i810XvMCContext *)context->privData;


  /* Verify the XvMC extension exists */
  if(! XvMCQueryExtension(display, &event_base,
			  &error_base)) {
    printf("XvMC Extension is not available!\n");
    return BadAlloc;
  }
  /* Verify XvMC version */
  ret = XvMCQueryVersion(display, &major, &minor);
  if(ret) {
    printf("XvMCQuery Version Failed, unable to determine protocol version\n");
  }
  /* FIXME: Check Major and Minor here */

  /* Check for drm */
  if(! drmAvailable()) {
    printf("Direct Rendering is not avilable on this system!\n");
    return BadAlloc;
  }

  /* 
     Build the Attribute Atoms, and Initialize the ones that exist
     in Xv.
  */
  pI810XvMC->xv_colorkey = XInternAtom(display,"XV_COLORKEY",0);
  if(!pI810XvMC->xv_colorkey) {
    return XvBadPort;
  }
  ret = XvGetPortAttribute(display,port,pI810XvMC->xv_colorkey,
			   &pI810XvMC->colorkey);
  if(ret) {
    return ret;
  }
  pI810XvMC->xv_brightness = XInternAtom(display,"XV_BRIGHTNESS",0);
  pI810XvMC->xv_saturation = XInternAtom(display,"XV_SATURATION",0);
  pI810XvMC->xv_contrast = XInternAtom(display,"XV_CONTRAST",0);
  pI810XvMC->brightness = 0;
  pI810XvMC->saturation = 0x80;  /* 1.0 in 3.7 format */
  pI810XvMC->contrast = 0x40; /* 1.0 in 3.6 format */

  /* Open DRI Device */
  if((pI810XvMC->fd = drmOpen("i810",NULL)) < 0) {
    printf("DRM Device for i810 could not be opened.\n");
    free(pI810XvMC);
    return BadAccess;
  } /* !pI810XvMC->fd */

  /* Get magic number and put it in privData for passing */
  drmGetMagic(pI810XvMC->fd,&magic);
  context->flags = (unsigned long)magic;

  /*
    Pass control to the X server to create a drm_context_t for us and
    validate the with/height and flags.
  */
  if((ret = _xvmc_create_context(display, context, &priv_count, &priv_data))) {
    printf("Unable to create XvMC Context.\n");
    return ret;
  }

  /* 
     X server returns a structure like this:
     drm_context_t
     fbBase
     OverlayOffset
     OverlaySize
     SurfacesOffset
     SurfacesSize
     busIdString = 9 char + 1
  */
  if(priv_count != 9) {
    printf("_xvmc_create_context() returned incorrect data size!\n");
    printf("\tExpected 9, got %d\n",priv_count);
    _xvmc_destroy_context(display, context);
    free(pI810XvMC);
    return BadAlloc;
  }
  pI810XvMC->drmcontext = priv_data[0];
  pI810XvMC->fb_base = priv_data[1];
  pI810XvMC->overlay.offset = priv_data[2] + priv_data[1];
  pI810XvMC->overlay.size = priv_data[3];
  pI810XvMC->surfaces.offset = priv_data[4] + priv_data[1];
  pI810XvMC->surfaces.size = priv_data[5];
  strncpy(pI810XvMC->busIdString,(char *)&priv_data[6],9);
  pI810XvMC->busIdString[9] = '\0';

  /* Must free the private data we were passed from X */
  free(priv_data);
  
  /* Initialize private context values */
  pI810XvMC->current = 0;
  pI810XvMC->lock = 0;
  pI810XvMC->last_flip = 0;
  pI810XvMC->dual_prime = 0;

  /* 
     Map dma Buffers: Not really, this would be a drmMapBufs
     but due to the i810 security model we have to just create an
     empty data structure to fake it.
  */
  pI810XvMC->dmabufs = (drmBufMapPtr)malloc(sizeof(drmBufMap));
  if(pI810XvMC->dmabufs == NULL) {
    printf("Dma Bufs could not be mapped.\n");
    _xvmc_destroy_context(display, context);
    free(pI810XvMC);
    return BadAlloc;
  } /* pI810XvMC->dmabufs == NULL */
  memset(pI810XvMC->dmabufs, 0, sizeof(drmBufMap));
  pI810XvMC->dmabufs->list = (drmBufPtr)malloc(sizeof(drmBuf) *
					       I810_DMA_BUF_NR);
  if(pI810XvMC->dmabufs->list == NULL) {
    printf("Dma Bufs could not be mapped.\n");
    _xvmc_destroy_context(display, context);
    free(pI810XvMC);
    return BadAlloc;
  } /* pI810XvMC->dmabufs->list == NULL */
  memset(pI810XvMC->dmabufs->list, 0, sizeof(drmBuf) * I810_DMA_BUF_NR);
  
  /* Map the Overlay memory */
  if(drmMap(pI810XvMC->fd,pI810XvMC->overlay.offset,
	    pI810XvMC->overlay.size,&(pI810XvMC->overlay.address)) < 0) {
    printf("Unable to map Overlay at offset 0x%x and size 0x%x\n",
	   (unsigned int)pI810XvMC->overlay.offset,pI810XvMC->overlay.size);
    _xvmc_destroy_context(display, context);
    free(pI810XvMC->dmabufs->list);
    free(pI810XvMC);
    return BadAlloc;
  } /* drmMap() < 0 */
  
  /* Overlay Regs are offset 1024 into Overlay Map */
  pI810XvMC->oregs = (i810OverlayRec *)
    ((unsigned char *)pI810XvMC->overlay.address + 1024);

  /* Map Surfaces */
  if(drmMap(pI810XvMC->fd,pI810XvMC->surfaces.offset,
	    pI810XvMC->surfaces.size,&(pI810XvMC->surfaces.address)) < 0) {
    printf("Unable to map XvMC Surfaces.\n");
    _xvmc_destroy_context(display, context);
    free(pI810XvMC->dmabufs->list);
    free(pI810XvMC);
    return BadAlloc;
  } /* drmMap() < 0 */

  /*
    There is a tiny chance that someone was using the overlay and
    issued a flip that hasn't finished. To be 100% sure I'll just
    take the lock and sleep for the worst case time for a flip.
  */
  I810_LOCK(pI810XvMC,DRM_LOCK_QUIESCENT);
  usleep(20000);  /* 1/50th Sec for 50hz refresh */

  /* Set up Overlay regs with Initial Values */
  pI810XvMC->oregs->YRGB_VPH = 0;
  pI810XvMC->oregs->UV_VPH = 0;
  pI810XvMC->oregs->HORZ_PH = 0;
  pI810XvMC->oregs->INIT_PH = 0;
  pI810XvMC->oregs->DWINPOS = 0;
  pI810XvMC->oregs->DWINSZ = (I810_XVMC_MAXHEIGHT << 16) |
    I810_XVMC_MAXWIDTH;
  pI810XvMC->oregs->SWID =  I810_XVMC_MAXWIDTH | (I810_XVMC_MAXWIDTH << 15);
  pI810XvMC->oregs->SWIDQW = (I810_XVMC_MAXWIDTH >> 3) |
    (I810_XVMC_MAXWIDTH << 12);
  pI810XvMC->oregs->SHEIGHT = I810_XVMC_MAXHEIGHT |
    (I810_XVMC_MAXHEIGHT << 15);
  pI810XvMC->oregs->YRGBSCALE = 0x80004000; /* scale factor 1 */
  pI810XvMC->oregs->UVSCALE = 0x80004000;   /* scale factor 1 */
  pI810XvMC->oregs->OV0CLRC0 = 0x4000;      /* brightness: 0 contrast: 1.0 */
  pI810XvMC->oregs->OV0CLRC1 = 0x80;        /* saturation: bypass */
  
  /* Destination Colorkey Setup */
  pI810XvMC->oregs->DCLRKV = RGB16ToColorKey(pI810XvMC->colorkey);
  pI810XvMC->oregs->DCLRKM = 0x80070307;

  
  pI810XvMC->oregs->SCLRKVH = 0;
  pI810XvMC->oregs->SCLRKVL = 0;
  pI810XvMC->oregs->SCLRKM = 0; /* source color key disable */
  pI810XvMC->oregs->OV0CONF = 0; /* two 720 pixel line buffers */
  
  pI810XvMC->oregs->OV0CMD = VC_UP_INTERPOLATION | HC_UP_INTERPOLATION | 
    Y_ADJUST | YUV_420;

  pI810XvMC->ref = 1;

  I810_UNLOCK(pI810XvMC);

  return Success;

}

/***************************************************************************
// Function: XvMCDestroyContext
// Description: Destorys the specified context.
//
// Arguments:
//   display - Specifies the connection to the server.
//   context - The context to be destroyed.
//
// Returns: Status
***************************************************************************/
_X_EXPORT Status XvMCDestroyContext(Display *display, XvMCContext *context) {
  i810XvMCContext *pI810XvMC;

  if(context == NULL) {
    return (error_base + XvMCBadContext);
  }
  if(context->privData == NULL) {
    return (error_base + XvMCBadContext);
  }
  pI810XvMC = (i810XvMCContext *)context->privData;

  /* Turn off the overlay */
  if(pI810XvMC->last_flip) {
    I810_LOCK(pI810XvMC,DRM_LOCK_QUIESCENT);

    /* Make sure last flip is done */
    BLOCK_OVERLAY(pI810XvMC,pI810XvMC->current);

    pI810XvMC->oregs->OV0CMD = VC_UP_INTERPOLATION | HC_UP_INTERPOLATION |
      Y_ADJUST;
    pI810XvMC->current = !pI810XvMC->current;
    if(pI810XvMC->current == 1) {
      pI810XvMC->oregs->OV0CMD |= BUFFER1_FIELD0;
    }
    else {
      pI810XvMC->oregs->OV0CMD |= BUFFER0_FIELD0;
    }
    OVERLAY_FLIP(pI810XvMC);
    pI810XvMC->last_flip++;
  
    /* Wait for the flip */
    BLOCK_OVERLAY(pI810XvMC,pI810XvMC->current);

    I810_UNLOCK(pI810XvMC);
  }

  /* Pass Control to the X server to destroy the drm_context_t */
  _xvmc_destroy_context(display, context);

  i810_free_privContext(pI810XvMC);
  context->privData = NULL;

  return Success;
}


/***************************************************************************
// Function: XvMCCreateSurface
***************************************************************************/
_X_EXPORT Status XvMCCreateSurface( Display *display, XvMCContext *context,
			  XvMCSurface *surface) {
  i810XvMCContext *pI810XvMC;
  i810XvMCSurface *pI810Surface;
  int priv_count;
  uint *priv_data;
  Status ret;

  if((surface == NULL) || (context == NULL) || (display == NULL)){
    return BadValue;
  }
  
  pI810XvMC = (i810XvMCContext *)context->privData;
  if(pI810XvMC == NULL) {
    return (error_base + XvMCBadContext);
  }


  surface->privData = (i810XvMCSurface *)malloc(sizeof(i810XvMCSurface));
  if(!surface->privData) {
    return BadAlloc;
  }
  pI810Surface = (i810XvMCSurface *)surface->privData;

  /* Initialize private values */
  pI810Surface->privContext = pI810XvMC;
  pI810Surface->last_render = 0;
  pI810Surface->last_flip = 0;
  pI810Surface->second_field = 0;

  if((ret = _xvmc_create_surface(display, context, surface,
				&priv_count, &priv_data))) {
    free(pI810Surface);
    printf("Unable to create XvMCSurface.\n");
    return ret;
  }

  /*
    _xvmc_create_subpicture returns 2 uints with the offset into
    the DRM map for the Y surface and UV surface.
  */
  if(priv_count != 2) {
    printf("_xvmc_create_surface() return incorrect data size.\n");
    printf("Expected 2 got %d\n",priv_count);
    free(priv_data);
    free(pI810Surface);
    return BadAlloc;
  }
  /* Data == Client Address, offset == Physical address offset */
  pI810Surface->data = pI810XvMC->surfaces.address;
  pI810Surface->offset = pI810XvMC->surfaces.offset;


  /* 
     i810's MC Engine needs surfaces of 2^x (x= 9,10,11,12) pitch
     and the Tiler need 512k aligned surfaces, basically we are
     stuck with fixed memory with pitch 1024 for Y data. UV = 512.
  */
  pI810Surface->pitch = 10;
  if((surface->surface_type_id == FOURCC_UYVY) ||
     (surface->surface_type_id == FOURCC_YUY2)) {
    /* This is not implemented server side. */
    pI810Surface->pitch++;
  }

  /*
    offsets[0,1,2] == Offsets from either data or offset for the Y
    U and V surfaces.
  */
  pI810Surface->offsets[0] = priv_data[0];
  if(((unsigned long)pI810Surface->data + pI810Surface->offsets[0]) & 4095) {
    printf("XvMCCreateSurface: Surface offset 0 is not 4096 aligned\n");
  }

  if((surface->surface_type_id == FOURCC_UYVY) ||
     (surface->surface_type_id == FOURCC_YUY2)) {
    /* Packed surface, not fully implemented */
    pI810Surface->offsets[1] = 0;
    pI810Surface->offsets[2] = 0;
  }
  else {
    /* Planar surface */
    pI810Surface->offsets[1] = priv_data[1];
    if(((unsigned long)pI810Surface->data + pI810Surface->offsets[1]) & 2047) {
      printf("XvMCCreateSurface: Surface offset 1 is not 2048 aligned\n");
    }

    pI810Surface->offsets[2] = ((unsigned long)pI810Surface->offsets[1] +
				(1<<(pI810Surface->pitch - 1)) * 288);
    if(((unsigned long)pI810Surface->data + pI810Surface->offsets[2]) & 2047) {
      printf("XvMCCreateSurface: Surface offset 2 is not 2048 aligned\n");
    }

  }

  /* Free data returned from xvmc_create_surface */
  free(priv_data);

  /* Clear the surface to 0 */
  memset((void *)((unsigned long)pI810Surface->data + (unsigned long)pI810Surface->offsets[0]),
	 0, ((1<<pI810Surface->pitch) * surface->height));

  switch(surface->surface_type_id) {
  case FOURCC_YV12:
  case FOURCC_I420:
    /* Destination buffer info command */
    pI810Surface->dbi1y = ((((unsigned int)pI810Surface->offset +
			     pI810Surface->offsets[0]) & ~0xfc000fff) |
			   (pI810Surface->pitch - 9));
    pI810Surface->dbi1u = ((((unsigned int)pI810Surface->offset +
			     pI810Surface->offsets[1]) & ~0xfc000fff) |
			   (pI810Surface->pitch - 10));
    pI810Surface->dbi1v = ((((unsigned int)pI810Surface->offset +
			     pI810Surface->offsets[2]) & ~0xfc000fff) |
			   (pI810Surface->pitch - 10));

    /* Destination buffer variables command */
    pI810Surface->dbv1 = (0x8<<20) | (0x8<<16);
    /* Map info command */
    pI810Surface->mi1y = (0x1<<24) | (1<<9) | (pI810Surface->pitch - 3);
    pI810Surface->mi1u = (0x1<<24) | (1<<9) | (pI810Surface->pitch - 4);
    pI810Surface->mi1v = (0x1<<24) | (1<<9) | (pI810Surface->pitch - 4);

    pI810Surface->mi2y = (((unsigned int)surface->height - 1)<<16) |
      ((unsigned int)surface->width - 1);
    pI810Surface->mi2u = (((unsigned int)surface->height - 1)<<15) |
      (((unsigned int)surface->width - 1)>>1);
    pI810Surface->mi2v = pI810Surface->mi2u;

    pI810Surface->mi3y = ((unsigned int)pI810Surface->offset +
			  pI810Surface->offsets[0]) & ~0x0000000f;
    pI810Surface->mi3u = ((unsigned int)pI810Surface->offset +
			  pI810Surface->offsets[1]) & ~0x0000000f;
    pI810Surface->mi3v = ((unsigned int)pI810Surface->offset +
			  pI810Surface->offsets[2]) & ~0x0000000f;
    break;
  case FOURCC_UYVY:
  case FOURCC_YUY2:
  default:
    /* Destination buffer info command */
    pI810Surface->dbi1y = ((((unsigned int)pI810Surface->offset +
			     pI810Surface->offsets[0]) & ~0xfc000fff) |
			   (pI810Surface->pitch - 9));
    /* Destination buffer variables command */
    if(surface->surface_type_id == FOURCC_YUY2) {
      pI810Surface->dbv1 = 0x5<<8;
      pI810Surface->mi1y = 0x5<<24 | pI810Surface->pitch | 0x1<<21;
    }
    else {
      pI810Surface->dbv1 = 0x4<<8;
      pI810Surface->mi1y = 0x5<<24 | (pI810Surface->pitch - 3);
    }
    pI810Surface->mi2y = (((unsigned int)surface->width - 1)<<16) |
      ((unsigned int)surface->height - 1);
    pI810Surface->mi3y = ((unsigned int)pI810Surface->offset +
			  pI810Surface->offsets[0]) & ~0xfc000fff;
    break;
  }
  pI810XvMC->ref++;

  return Success;
}


/***************************************************************************
// Function: XvMCDestroySurface
***************************************************************************/
_X_EXPORT Status XvMCDestroySurface(Display *display, XvMCSurface *surface) {
  i810XvMCSurface *pI810Surface;
  i810XvMCContext *pI810XvMC;

  if((display == NULL) || (surface == NULL)) {
    return BadValue;
  }
  if(surface->privData == NULL) {
    return (error_base + XvMCBadSurface);
  }

  pI810Surface = (i810XvMCSurface *)surface->privData;
  if(pI810Surface->last_flip) {
    XvMCSyncSurface(display,surface);
  }
  pI810XvMC = (i810XvMCContext *)pI810Surface->privContext;

  _xvmc_destroy_surface(display,surface);

  i810_free_privContext(pI810XvMC);

  free(pI810Surface);
  surface->privData = NULL;
  return Success;
}

/***************************************************************************
// Function: XvMCCreateBlocks
***************************************************************************/
_X_EXPORT Status XvMCCreateBlocks(Display *display, XvMCContext *context,
			unsigned int num_blocks,
			XvMCBlockArray *block) {

  if((display == NULL) || (context == NULL) || (num_blocks == 0)) {
    return BadValue;
  }

  block->blocks = (short *)malloc(num_blocks<<6 * sizeof(short));
  if(block->blocks == NULL) {
    return BadAlloc;
  }

  block->num_blocks = num_blocks;
  block->context_id = context->context_id;

  block->privData = NULL;

  return Success;
}

/***************************************************************************
// Function: XvMCDestroyBlocks
***************************************************************************/
_X_EXPORT Status XvMCDestroyBlocks(Display *display, XvMCBlockArray *block) {
  if(display == NULL) {
    return BadValue;
  }

  free(block->blocks);
  block->num_blocks = 0;
  block->context_id = 0;
  block->privData = NULL;
  return Success;
}

/***************************************************************************
// Function: XvMCCreateMacroBlocks
***************************************************************************/
_X_EXPORT Status XvMCCreateMacroBlocks(Display *display, XvMCContext *context,
			     unsigned int num_blocks,
			     XvMCMacroBlockArray *blocks) {

  if((display == NULL) || (context == NULL) || (blocks == NULL) ||
      (num_blocks == 0)) {
    return BadValue;
  }
  memset(blocks,0,sizeof(XvMCMacroBlockArray));
  blocks->context_id = context->context_id;
  blocks->privData = NULL;
  
  blocks->macro_blocks = (XvMCMacroBlock *)
    malloc(num_blocks * sizeof(XvMCMacroBlock));
  if(blocks->macro_blocks == NULL) {
    return BadAlloc;
  }
  blocks->num_blocks = num_blocks;

  return Success;
}

/***************************************************************************
// Function: XvMCDestroyMacroBlocks
***************************************************************************/
_X_EXPORT Status XvMCDestroyMacroBlocks(Display *display, XvMCMacroBlockArray *block) {
  if((display == NULL) || (block == NULL)) {
    return BadValue;
  }
  if(block->macro_blocks) {
    free(block->macro_blocks);
  }
  block->context_id = 0;
  block->num_blocks = 0;
  block->privData = NULL;

  return Success;
}


/***************************************************************************
// Function: dp (Debug Print)
// Description: This function prints out in hex i * uint32_t at the address
//  supplied. This enables you to print out the dma buffers from
//  within the debugger even though they are not in your address space.
***************************************************************************/
void dp(unsigned int *address, unsigned int i) {
  int j;

  printf("DebugPrint:\n");
  for(j=0; j<i; j++) {
    printf("0x%8.8x ",address[j]);
    if(j && !(j & 7)) { printf("\n");}
  }
}

/***************************************************************************
// Macro: PACK_*
// Description: Packs 16bit signed data from blocks into either 8bit unsigned
//  intra data or 16bit signed correction data, both packed into
//  32 bit integers.
***************************************************************************/
#define PACK_INTRA_DATA(d,b,n)                  \
  do {                                          \
   char *dp = (char *)d;                        \
   char *bp = (char *)b;                        \
   int counter;                                 \
   for(counter = 0; counter < n; counter++) {   \
     *dp++ = *bp;                               \
     bp += 2;                                   \
   }                                            \
  }while(0);

#define PACK_CORR_DATA(d,b,n)          \
            memcpy(d,b,n);             \
            d = (uint *)((unsigned long)d + n);

#define MARK_CORR_DATA(d,n)                          \
            do {                                     \
              uint* q = (uint*)((unsigned long)d - n);        \
              while((unsigned long)q < (unsigned long)d) {             \
               *q++ += 0x00330033;                   \
              }                                      \
	    }while(0);

#define MARK_INTRA_BLOCK(d)         \
           do {                     \
             int q;                 \
             for(q=0; q<16; q++) {  \
               d[q] += 0x33333333;  \
             }                      \
	    }while(0);

/*
  Used for DCT 1 when we need DCT 0. Instead
  of reading from one block we read from two and
  interlace.
*/
#define PACK_CORR_DATA_1to0(d,top,bottom)            \
            do {                                     \
              short *t = top,*b = bottom;            \
              PACK_CORR_DATA(d,t,16);                \
              t = (short *)((unsigned long)t + 16);           \
              PACK_CORR_DATA(d,b,16);                \
              b = (short *)((unsigned long)b + 16);           \
              PACK_CORR_DATA(d,t,16);                \
              t = (short *)((unsigned long)t + 16);           \
              PACK_CORR_DATA(d,b,16);                \
              b = (short *)((unsigned long)b + 16);           \
              PACK_CORR_DATA(d,t,16);                \
              t = (short *)((unsigned long)t + 16);           \
              PACK_CORR_DATA(d,b,16);                \
              b = (short *)((unsigned long)b + 16);           \
              PACK_CORR_DATA(d,t,16);                \
              t = (short *)((unsigned long)t + 16);           \
              PACK_CORR_DATA(d,b,16);                \
              b = (short *)((unsigned long)b + 16);           \
            }while(0);

/* Used for DCT 0 when we need DCT 1. */
#define PACK_CORR_DATA_0to1(d,top,bottom)            \
            do{                                      \
              short *t = top,*b = bottom;            \
              PACK_CORR_DATA(d,t,16);                \
              t = (short *)((unsigned long)t + 32);           \
              PACK_CORR_DATA(d,t,16);                \
              t = (short *)((unsigned long)t + 32);           \
              PACK_CORR_DATA(d,t,16);                \
              t = (short *)((unsigned long)t + 32);           \
              PACK_CORR_DATA(d,t,16);                \
              t = (short *)((unsigned long)t + 32);           \
              PACK_CORR_DATA(d,b,16);                \
              b = (short *)((unsigned long)b + 32);           \
              PACK_CORR_DATA(d,b,16);                \
              b = (short *)((unsigned long)b + 32);           \
              PACK_CORR_DATA(d,b,16);                \
              b = (short *)((unsigned long)b + 32);           \
              PACK_CORR_DATA(d,b,16);                \
              b = (short *)((unsigned long)b + 32);           \
            }while(0);

#define PACK_CORR_DATA_SHORT(d,block)                \
            do {                                     \
              short *b = block;                      \
              PACK_CORR_DATA(d,b,16);                \
              b = (short *)((unsigned long)b + 32);           \
              PACK_CORR_DATA(d,b,16);                \
              b = (short *)((unsigned long)b + 32);           \
              PACK_CORR_DATA(d,b,16);                \
              b = (short *)((unsigned long)b + 32);           \
              PACK_CORR_DATA(d,b,16);                \
              b = (short *)((unsigned long)b + 32);           \
            }while(0);

/* Lookup tables to speed common calculations */
static unsigned int drps_table[] = {2<<6,3<<6};

static unsigned int mvfs_table[] = {
  0x12,
  0x1a,
  0x13,
  0x1b
};

static unsigned int type_table[] = {
  0x1<<12,   /* This is an error so make it Forward motion */
  0x1<<12,
  0x1<<12,
  0x1<<12,
  0x2<<12,
  0x2<<12,
  0x3<<12,
  0x3<<12,
  0x1<<12,  /* Pattern but no Motion, Make motion Forward */
  0x1<<12,
  0x1<<12,
  0x1<<12,
  0x2<<12,
  0x2<<12,
  0x3<<12,
  0x3<<12
};

static unsigned int y_frame_bytes[] = {
  0,0,0,0,128,128,128,128,
  128,128,128,128,256,256,256,256,
  128,128,128,128,256,256,256,256,
  256,256,256,256,384,384,384,384,
  128,128,128,128,256,256,256,256,
  256,256,256,256,384,384,384,384,
  256,256,256,256,384,384,384,384,
  384,384,384,384,512,512,512,512
};

static unsigned int u_frame_bytes[] = {
  0,0,128,128,0,0,128,128,
  0,0,128,128,0,0,128,128,
  0,0,128,128,0,0,128,128,
  0,0,128,128,0,0,128,128,
  0,0,128,128,0,0,128,128,
  0,0,128,128,0,0,128,128,
  0,0,128,128,0,0,128,128,
  0,0,128,128,0,0,128,128
};

static unsigned int v_frame_bytes[] = {
  0,128,0,128,0,128,0,128,
  0,128,0,128,0,128,0,128,
  0,128,0,128,0,128,0,128,
  0,128,0,128,0,128,0,128,
  0,128,0,128,0,128,0,128,
  0,128,0,128,0,128,0,128,
  0,128,0,128,0,128,0,128,
  0,128,0,128,0,128,0,128
};

static unsigned int y_first_field_bytes[] = {
  0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,
  128,128,128,128,128,128,128,128,
  128,128,128,128,128,128,128,128,
  128,128,128,128,128,128,128,128,
  128,128,128,128,128,128,128,128,
  256,256,256,256,256,256,256,256,
  256,256,256,256,256,256,256,256
};

static unsigned int y_second_field_bytes[] = {
  0,0,0,0,128,128,128,128,
  128,128,128,128,256,256,256,256,
  0,0,0,0,128,128,128,128,
  128,128,128,128,256,256,256,256,
  0,0,0,0,128,128,128,128,
  128,128,128,128,256,256,256,256,
  0,0,0,0,128,128,128,128,
  128,128,128,128,256,256,256,256
};

static unsigned int y_dct0_field_bytes[] = {
  0,0,0,0,128,128,128,128,
  128,128,128,128,256,256,256,256,
  128,128,128,128,128,128,128,128,
  256,256,256,256,256,256,256,256,
  128,128,128,128,256,256,256,256,
  128,128,128,128,256,256,256,256,
  256,256,256,256,256,256,256,256,
  256,256,256,256,256,256,256,256
};

static unsigned int y_dct1_frame_bytes[] = {
  0,0,0,0,256,256,256,256,
  256,256,256,256,512,512,512,512,
  256,256,256,256,256,256,256,256,
  512,512,512,512,512,512,512,512,
  256,256,256,256,512,512,512,512,
  256,256,256,256,512,512,512,512,
  512,512,512,512,512,512,512,512,
  512,512,512,512,512,512,512,512
};

static unsigned int u_field_bytes[] = {
  0,0,64,64,0,0,64,64,
  0,0,64,64,0,0,64,64,
  0,0,64,64,0,0,64,64,
  0,0,64,64,0,0,64,64,
  0,0,64,64,0,0,64,64,
  0,0,64,64,0,0,64,64,
  0,0,64,64,0,0,64,64,
  0,0,64,64,0,0,64,64
};

static unsigned int v_field_bytes[] = {
  0,64,0,64,0,64,0,64,
  0,64,0,64,0,64,0,64,
  0,64,0,64,0,64,0,64,
  0,64,0,64,0,64,0,64,
  0,64,0,64,0,64,0,64,
  0,64,0,64,0,64,0,64,
  0,64,0,64,0,64,0,64,
  0,64,0,64,0,64,0,64
};

static short empty_block[] = {
  0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0
};


/***************************************************************************
// Function: dispatchYContext
// Description: Allocate a DMA buffer write the Y MC Context info in it,
//  and dispatch it to hardware.
***************************************************************************/

static __inline__ void dispatchYContext(i810XvMCSurface *privTarget,
					i810XvMCSurface *privPast,
					i810XvMCSurface *privFuture,
					i810XvMCContext *pI810XvMC) {
  uint *data;
  drmBufPtr pDMA;
  drm_i810_mc_t mc;

  pDMA = i810_get_free_buffer(pI810XvMC);
  data = pDMA->address;
  *data++ = CMD_FLUSH;
  *data++ = BOOLEAN_ENA_2;
  *data++ = CMD_FLUSH;
  *data++ = DEST_BUFFER_INFO;
  *data++ = privTarget->dbi1y;
  *data++ = DEST_BUFFER_VAR;
  *data++ = privTarget->dbv1;
  /* Past Surface */
  *data++ = CMD_MAP_INFO;
  *data++ = privPast->mi1y;
  *data++ = privPast->mi2y;
  *data++ = privPast->mi3y;
  /* Future Surface */
  *data++ = CMD_MAP_INFO;
  *data++ = privFuture->mi1y | 0x1<<28;
  *data++ = privFuture->mi2y;
  *data++ = privFuture->mi3y;

  mc.idx = pDMA->idx;
  mc.used = (unsigned long)data - (unsigned long)pDMA->address;
  mc.last_render = ++pI810XvMC->last_render;
  privTarget->last_render = pI810XvMC->last_render;
  I810_MC(pI810XvMC,mc);
}

static __inline__ void renderError(void) {
  printf("Invalid Macroblock Parameters found.\n");
  return;
}

/***************************************************************************
// Function: renderIntrainFrame
// Description: inline function that sets hardware parameters for an Intra
//  encoded macroblock in a Frame picture.
***************************************************************************/
static __inline__ void renderIntrainFrame(uint **datay,uint **datau,
					  uint **datav,
					  XvMCMacroBlock *mb,
					  short *block_ptr) {

  register uint *dy = *datay;
  register uint *du = *datau;
  register uint *dv = *datav;

  /* Y Blocks */
  *dy++ = GFXBLOCK + 68;
  *dy++ = (1<<30) | (3<<28) | (0xf<<24);
  *dy++ = ((uint)mb->x<<20) | ((uint)mb->y<<4);
  *dy++ = (16<<16) | 16;
  *dy++ = 0;
  *dy++ = 0;
  PACK_INTRA_DATA(dy,block_ptr,256);
  dy += 64;
  block_ptr += 256;
  /* End Y Blocks */

  /* U Block */
  *du++ = GFXBLOCK + 20;
  *du++ = (2<<30) | (1<<28) | (1<<23);
  *du++ = (((uint)mb->x)<<19) | (((uint)mb->y)<<3);
  *du++ = (8<<16) | 8;
  *du++ = 0;
  *du++ = 0;
  PACK_INTRA_DATA(du,block_ptr,64);
  du += 16;
  block_ptr += 64;
  
  /* V Block */
  *dv++ = GFXBLOCK + 20;
  *dv++ = (3<<30) | (1<<28) | (1<<22);
  *dv++ = (((uint)mb->x)<<19) | (((uint)mb->y)<<3);
  *dv++ = (8<<16) | 8;
  *dv++ = 0;
  *dv++ = 0;
  PACK_INTRA_DATA(dv,block_ptr,64);
  dv += 16;
  block_ptr += 64;

  *datay = dy;
  *datau = du;
  *datav = dv;
}

/***************************************************************************
// Function: renderIntrainFrameDCT1
// Description: inline function that sets hardware parameters for an Intra
//  encoded macroblock in a Frame picture with DCT type 1.
***************************************************************************/
static __inline__ void renderIntrainFrameDCT1(uint **datay,uint **datau,
					      uint **datav,XvMCMacroBlock *mb,
					      short *block_ptr,uint flags) {

  register uint *dy = *datay;
  register uint *du = *datau;
  register uint *dv = *datav;


  /* Y Blocks */
  *dy++ = GFXBLOCK + 36;
  *dy++ = (1<<30) | (2<<28) | (0x3<<26) | (0x2<<6);
  *dy++ = ((uint)mb->x<<20) | ((uint)mb->y<<3);
  *dy++ = (8<<16) | 16;
  *dy++ = 0;
  *dy++ = 0;
  PACK_INTRA_DATA(dy,block_ptr,128);
  dy += 32;
  block_ptr += 128;

  /* Second Y block */
  *dy++ = GFXBLOCK + 36;
  *dy++ = (1<<30) | (2<<28) | (0x3<<26) | (0x3<<6);
  *dy++ = ((uint)mb->x<<20) | ((uint)mb->y<<3);
  *dy++ = (8<<16) | 16;
  *dy++ = 0;
  *dy++ = 0;
  PACK_INTRA_DATA(dy,block_ptr,128);
  dy += 32;
  block_ptr += 128;
  /* End Y Blocks */

    
  /* U Block */
  *du++ = GFXBLOCK + 20;
  *du++ = (2<<30) | (1<<28) | (1<<23);
  *du++ = (((uint)mb->x)<<19) | (((uint)mb->y)<<3);
  *du++ = (8<<16) | 8;
  *du++ = 0;
  *du++ = 0;
  PACK_INTRA_DATA(du,block_ptr,64);
  du += 16;
  block_ptr += 64;
  
  /* V Block */
  *dv++ = GFXBLOCK + 20;
  *dv++ = (3<<30) | (1<<28) | (1<<22);
  *dv++ = (((uint)mb->x)<<19) | (((uint)mb->y)<<3);
  *dv++ = (8<<16) | 8;
  *dv++ = 0;
  *dv++ = 0;
  PACK_INTRA_DATA(dv,block_ptr,64);
  dv += 16;
  block_ptr += 64;

  *datay = dy;
  *datau = du;
  *datav = dv;
}


/***************************************************************************
// Function: renderIntrainField
// Description: inline function that sets hardware parameters for an Intra
//  encoded macroblock in Field pictures.
***************************************************************************/
static __inline__ void renderIntrainField(uint **datay,uint **datau,
					  uint **datav,
					  XvMCMacroBlock *mb,short *block_ptr,
					  uint ps) {

  register uint *dy = *datay;
  register uint *du = *datau;
  register uint *dv = *datav;

  uint xy = ((uint)mb->x<<20) | ((uint)mb->y<<4);
  uint dw1 = drps_table[~ps & 0x1];

  /* Y Blocks */
  *dy++ = GFXBLOCK + 68;
  *dy++ = (1<<30) | (3<<28) | (0xf<<24) | dw1;
  *dy++ = xy;
  *dy++ = (16<<16) | 16;
  *dy++ = 0;
  *dy++ = 0;
  PACK_INTRA_DATA(dy,block_ptr,256);
  dy += 64;
  block_ptr += 256;
  /* End Y Blocks */

  xy >>= 1;

  /* U Block */
  *du++ = GFXBLOCK + 20;
  *du++ = (2<<30) | (1<<28) | (1<<23) | dw1;
  *du++ = xy;
  *du++ = (8<<16) | 8;
  *du++ = 0;
  *du++ = 0;
  PACK_INTRA_DATA(du,block_ptr,64);
  du += 16;
  block_ptr += 64;
  
  /* V Block */
  *dv++ = GFXBLOCK + 20;
  *dv++ = (3<<30) | (1<<28) | (1<<22) | dw1;
  *dv++ = xy;
  *dv++ = (8<<16) | 8;
  *dv++ = 0;
  *dv++ = 0;
  PACK_INTRA_DATA(dv,block_ptr,64);
  dv += 16;
  block_ptr += 64;

  *datay = dy;
  *datau = du;
  *datav = dv;
}


/***************************************************************************
// Function: renderFieldinField
// Description: inline function that sets hardware parameters for a Field
//  encoded macroblock in a Field Picture.
***************************************************************************/
static __inline__ void renderFieldinField(uint **datay,uint **datau,
					  uint **datav,
					  XvMCMacroBlock *mb,short *block_ptr,
					  uint ps, uint flags) {

  register uint *dy = *datay;
  register uint *du = *datau;
  register uint *dv = *datav;

  /* Motion Vectors */
  short fmv[2];
  short bmv[2];
  /* gfxblock dword 1 */
  uint dw1;

  uint parity = ~ps & XVMC_TOP_FIELD;

  uint ysize = y_frame_bytes[mb->coded_block_pattern];
  uint usize = u_frame_bytes[mb->coded_block_pattern];
  uint vsize = v_frame_bytes[mb->coded_block_pattern];

  uint xy = ((uint)mb->x<<20) | ((uint)mb->y<<4);

  /* i810 Specific flag used to identify the second field in a P frame */
  if(flags & 0x80000000) {
    /* P Frame */
    if((mb->motion_vertical_field_select & XVMC_SELECT_FIRST_FORWARD) ==
      parity) {
      /* Same parity, use reference field (map0) */
      dw1 = 1<<12 | ((0x2 + parity)<<6) | ((0x2 + parity)<<3) |
	(((uint)mb->coded_block_pattern)<<22);
      fmv[0] = mb->PMV[0][0][1];
      fmv[1] = mb->PMV[0][0][0];
      bmv[0] = 0;
      bmv[1] = 0;
    }
    else {
      /*
	Opposite parity, set up as if it were backward
	motion and use map1.
      */
      dw1 = 2<<12 | ((0x2 + parity)<<6) | (0x3 - parity) |
	(((uint)mb->coded_block_pattern)<<22);
      bmv[0] = mb->PMV[0][0][1];
      bmv[1] = mb->PMV[0][0][0];
      fmv[0] = 0;
      fmv[1] = 0;
    }
  }
  else {
    dw1 = type_table[mb->macroblock_type & 0xf] |
      drps_table[~ps & 0x1] |
      mvfs_table[mb->motion_vertical_field_select & 3] |
      (((uint)mb->coded_block_pattern)<<22);
    
    fmv[0] = mb->PMV[0][0][1];
    fmv[1] = mb->PMV[0][0][0];
    
    bmv[0] = mb->PMV[0][1][1];
    bmv[1] = mb->PMV[0][1][0];
  }

  /* Y Block */
  *dy++ = GFXBLOCK + 4 + (ysize>>2);
  *dy++ = (1<<30) | (3<<28) | dw1;
  *dy++ = xy;
  *dy++ = (16<<16) | 16;
  *dy++ = *(uint *)fmv;
  *dy++ = *(uint *)bmv;
  PACK_CORR_DATA(dy,block_ptr,ysize);
  block_ptr = (short *)((unsigned long)block_ptr + ysize);
  /* End Y Blocks */

  fmv[0] /= 2;
  fmv[1] /= 2;
  bmv[0] /= 2;
  bmv[1] /= 2;
  xy >>= 1;

  /* U Block */
  *du++ = GFXBLOCK + 4 + (usize>>2);
  *du++ = (2<<30) | (1<<28) | dw1;
  *du++ = xy;
  *du++ = (8<<16) | 8;
  *du++ = *(uint *)fmv;
  *du++ = *(uint *)bmv;
  PACK_CORR_DATA(du,block_ptr,usize);
  block_ptr = (short *)((unsigned long)block_ptr + usize);

  /* V Block */
  *dv++ = GFXBLOCK + 4 + (vsize>>2);
  *dv++ = (3<<30) | (1<<28) | dw1;
  *dv++ = xy;
  *dv++ = (8<<16) | 8;
  *dv++ = *(uint *)fmv;
  *dv++ = *(uint *)bmv;
  PACK_CORR_DATA(dv,block_ptr,vsize);
  block_ptr = (short *)((unsigned long)block_ptr + vsize);

  *datay = dy;
  *datau = du;
  *datav = dv;
}

/***************************************************************************
// Function: render16x8inField
// Description: inline function that sets hardware parameters for a 16x8
//  encoded macroblock in a field picture.
***************************************************************************/
static __inline__ void render16x8inField(uint **datay,uint **datau,
					 uint **datav,
					 XvMCMacroBlock *mb,short *block_ptr,
					 uint ps, uint flags) {

  register uint *dy = *datay;
  register uint *du = *datau;
  register uint *dv = *datav;

  /* Motion Vectors */
  short fmv[4];
  short bmv[4];
  /* gfxblock dword 1 */
  uint dw1[2];

  uint y1size = y_first_field_bytes[mb->coded_block_pattern];
  uint y2size = y_second_field_bytes[mb->coded_block_pattern];
  uint usize = u_field_bytes[mb->coded_block_pattern];
  uint vsize = v_field_bytes[mb->coded_block_pattern];

  uint parity = ~ps & XVMC_TOP_FIELD;

  uint xy = ((uint)mb->x<<20) | ((uint)mb->y<<4);

  /* i810 Specific flag used to identify the second field in a P frame */
  if(flags & 0x80000000) {
    /* P Frame */
    if((mb->motion_vertical_field_select & XVMC_SELECT_FIRST_FORWARD) ==
      parity) {
      /* Same parity, use reference field (map0) */
      dw1[0] = 1<<12 | ((0x2 + parity)<<6) | ((0x2 + parity)<<3) |
	(((uint)mb->coded_block_pattern)<<22);

      fmv[0] = mb->PMV[0][0][1];
      fmv[1] = mb->PMV[0][0][0];
      bmv[0] = 0;
      bmv[1] = 0;
    }
    else {
      /*
	Opposite parity, set up as if it were backward
	motion and use map1.
      */
      dw1[0] = 2<<12 | ((0x2 + parity)<<6) | (0x3 - parity) |
	(((uint)mb->coded_block_pattern)<<22);

      bmv[0] = mb->PMV[0][0][1];
      bmv[1] = mb->PMV[0][0][0];
      fmv[0] = 0;
      fmv[1] = 0;
    }
    if((mb->motion_vertical_field_select & XVMC_SELECT_SECOND_FORWARD) ==
       (parity<<2)) {
      /* Same parity, use reference field (map0) */
      dw1[1] = 1<<12 | ((0x2 + parity)<<6) | ((0x2 + parity)<<3) |
	((((uint)mb->coded_block_pattern<<22) & (0x3<<22)) |
	 (((uint)mb->coded_block_pattern<<24) & (0x3<<26)));

      fmv[2] = mb->PMV[1][0][1];
      fmv[3] = mb->PMV[1][0][0];
      bmv[2] = 0;
      bmv[3] = 0;
    }
    else {
      /* 
	 Opposite parity, set up as if it were backward
	 motion and use map1.
      */
      dw1[1] = 2<<12 | ((0x2 + parity)<<6) | (0x3 - parity) |
	((((uint)mb->coded_block_pattern<<22) & (0x3<<22)) |
	 (((uint)mb->coded_block_pattern<<24) & (0x3<<26)));

      bmv[2] = mb->PMV[1][0][1];
      bmv[3] = mb->PMV[1][0][0];
      fmv[2] = 0;
      fmv[3] = 0;
    }
  }
  else {
    dw1[0] = type_table[mb->macroblock_type & 0xf] |
      drps_table[~ps & 0x1] |
      mvfs_table[mb->motion_vertical_field_select & 3] |
      (((uint)mb->coded_block_pattern)<<22);

    dw1[1] = type_table[mb->macroblock_type & 0xf] |
      drps_table[~ps & 0x1] |
      mvfs_table[(mb->motion_vertical_field_select>>2) & 0x3] |
      ((((uint)mb->coded_block_pattern<<22) & (0x3<<22)) |
       (((uint)mb->coded_block_pattern<<24) & (0x3<<26)));

    fmv[0] = mb->PMV[0][0][1];
    fmv[1] = mb->PMV[0][0][0];
    fmv[2] = mb->PMV[1][0][1];
    fmv[3] = mb->PMV[1][0][0];
    
    bmv[0] = mb->PMV[0][1][1];
    bmv[1] = mb->PMV[0][1][0];
    bmv[2] = mb->PMV[1][1][1];
    bmv[3] = mb->PMV[1][1][0];
  }

  /* First Y Block */
  *dy++ = GFXBLOCK + 4 + (y1size>>2);
  *dy++ = (1<<30) | (2<<28) | dw1[0];
  *dy++ = xy;
  *dy++ = (8<<16) | 16;
  *dy++ = *(uint *)fmv;
  *dy++ = *(uint *)bmv;
  PACK_CORR_DATA(dy,block_ptr,y1size);
  block_ptr = (short *)((unsigned long)block_ptr + y1size);

  /* Second Y Block */
  *dy++ = GFXBLOCK + 4 + (y2size>>2);
  *dy++ = (1<<30) | (2<<28) | dw1[1];
  *dy++ = (xy + 8);
  *dy++ = (8<<16) | 16;
  *dy++ = *(uint *)&fmv[2];
  *dy++ = *(uint *)&bmv[2];
  PACK_CORR_DATA(dy,block_ptr,y2size);
  block_ptr = (short *)((unsigned long)block_ptr + y2size);
  /* End Y Blocks */

  fmv[0] /= 2;
  fmv[1] /= 2;
  fmv[2] /= 2;
  fmv[3] /= 2;
  
  bmv[0] /= 2;
  bmv[1] /= 2;
  bmv[2] /= 2;
  bmv[3] /= 2;

  xy >>= 1;

  /* U Blocks */
  *du++ = GFXBLOCK + 4 + (usize>>2);
  *du++ = (2<<30) | (1<<28) | dw1[0];
  *du++ = xy;
  *du++ = (4<<16) | 8;
  *du++ = *(uint *)fmv;
  *du++ = *(uint *)bmv;
  PACK_CORR_DATA(du,block_ptr,usize);
  block_ptr = (short *)((unsigned long)block_ptr + usize);

  /* Second U block */
  *du++ = GFXBLOCK + 4 + (usize>>2);
  *du++ = (2<<30) | (1<<28) | dw1[1];
  *du++ = (xy + 4);
  *du++ = (4<<16) | 8;
  *du++ = *(uint *)&fmv[2];
  *du++ = *(uint *)&bmv[2];
  PACK_CORR_DATA(du,block_ptr,usize);
  block_ptr = (short *)((unsigned long)block_ptr + usize);
  /* End U Blocks */

  /* V Blocks */
  *dv++ = GFXBLOCK + 4 + (vsize>>2);
  *dv++ = (3<<30) | (1<<28) | dw1[0];
  *dv++ = xy;
  *dv++ = (4<<16) | 8;
  *dv++ = *(uint *)fmv;
  *dv++ = *(uint *)bmv;
  PACK_CORR_DATA(dv,block_ptr,vsize);
  block_ptr = (short *)((unsigned long)block_ptr + vsize);

  /* Second V Block */
  *dv++ = GFXBLOCK + 4 + (vsize>>2);
  *dv++ = (3<<30) | (1<<28) | dw1[1];
  *dv++ = (xy + 4);
  *dv++ = (4<<16) | 8;
  *dv++ = *(uint *)&fmv[2];
  *dv++ = *(uint *)&bmv[2];
  PACK_CORR_DATA(dv,block_ptr,vsize);
  block_ptr = (short *)((unsigned long)block_ptr + vsize);
  /* End V Blocks */

  *datay = dy;
  *datau = du;
  *datav = dv;
}

/***************************************************************************
// Function: renderDualPrimeinField
// Description: inline function that sets hardware parameters for a Dual
//  prime encoded macroblock in a field picture.
***************************************************************************/
static __inline__ void renderDualPrimeinField(uint **datay,uint **datau,
					      uint **datav,XvMCMacroBlock *mb,
					      short *block_ptr,uint ps,
					      uint flags) {

  register uint *dy = *datay;
  register uint *du = *datau;
  register uint *dv = *datav;

  /* Motion Vectors */
  short fmv[2];
  short bmv[2];
  /* gfxblock dword 1 */
  uint dw1;


  uint ysize = y_frame_bytes[mb->coded_block_pattern];
  uint usize = u_frame_bytes[mb->coded_block_pattern];
  uint vsize = v_frame_bytes[mb->coded_block_pattern];

  uint xy = ((uint)mb->x<<20) | ((uint)mb->y<<4);


  if(ps & XVMC_TOP_FIELD) {
    dw1 = (mb->coded_block_pattern<<22) | 3<<12 | 2<<6 | 2<<3 | 3;
  }
  else {
    dw1 = (mb->coded_block_pattern<<22) | 3<<12 | 3<<6 | 3<<3 | 2;
  }
  fmv[0] = mb->PMV[0][0][1];
  fmv[1] = mb->PMV[0][0][0];
  bmv[0] = mb->PMV[0][1][1];
  bmv[1] = mb->PMV[0][1][0];

  /* Y Block */
  *dy++ = GFXBLOCK + 4 + (ysize>>2);
  *dy++ = (1<<30) | (3<<28) | dw1;
  *dy++ = xy;
  *dy++ = (16<<16) | 16;
  *dy++ = *(uint *)fmv;
  *dy++ = *(uint *)bmv;
  PACK_CORR_DATA(dy,block_ptr,ysize);
  block_ptr = (short *)((unsigned long)block_ptr + ysize);
  /* End Y Blocks */
 
  fmv[0] /= 2;
  fmv[1] /= 2;
  bmv[0] /= 2;
  bmv[1] /= 2;
  xy >>= 1;

  /* U Block */
  *du++ = GFXBLOCK + 4 + (usize>>2);
  *du++ = (2<<30) | (1<<28) | dw1;
  *du++ = xy;
  *du++ = (8<<16) | 8;
  *du++ = *(uint *)fmv;
  *du++ = *(uint *)bmv;
  PACK_CORR_DATA(du,block_ptr,usize);
  block_ptr = (short *)((unsigned long)block_ptr + usize);

  /* V Block */
  *dv++ = GFXBLOCK + 4 + (vsize>>2);
  *dv++ = (3<<30) | (1<<28) | dw1;
  *dv++ = xy;
  *dv++ = (8<<16) | 8;
  *dv++ = *(uint *)fmv;
  *dv++ = *(uint *)bmv;
  PACK_CORR_DATA(dv,block_ptr,vsize);
  block_ptr = (short *)((unsigned long)block_ptr + vsize);

  *datay = dy;
  *datau = du;
  *datav = dv;
}

/***************************************************************************
// Function: renderFieldinFrame
// Description: inline function that sets hardware parameters for a Field
//  encoded macroblock in a frame picture.
***************************************************************************/
typedef union {
  short	s[4];
  uint  u[2];
} su_t;

static __inline__ void renderFieldinFrame(uint **datay,uint **datau,
					  uint **datav,
					  XvMCMacroBlock *mb,short *block_ptr,
					  uint flags) {

  register uint *dy = *datay;
  register uint *du = *datau;
  register uint *dv = *datav;

  /* Motion Vectors */
  su_t fmv;
  su_t bmv;
  /* gfxblock dword 1 */
  uint dw1[2];

  uint y1size = y_first_field_bytes[mb->coded_block_pattern];
  uint y2size = y_second_field_bytes[mb->coded_block_pattern];
  uint usize = u_field_bytes[mb->coded_block_pattern];
  uint vsize = v_field_bytes[mb->coded_block_pattern];

  uint xy = ((uint)mb->x<<20) | ((uint)mb->y<<3);

  dw1[0] = type_table[mb->macroblock_type & 0xf] | (0x2<<6) |
    mvfs_table[mb->motion_vertical_field_select & 3] |
    (((uint)mb->coded_block_pattern)<<22);
  
  dw1[1] = type_table[mb->macroblock_type & 0xf] | (0x3<<6) |
    mvfs_table[mb->motion_vertical_field_select>>2] |
    (((mb->coded_block_pattern & 0x3) |
      ((mb->coded_block_pattern & 0xc)<<2))<<22);

  fmv.s[0] = mb->PMV[0][0][1]/2;
  fmv.s[1] = mb->PMV[0][0][0];
  fmv.s[2] = mb->PMV[1][0][1]/2;
  fmv.s[3] = mb->PMV[1][0][0];
  
  bmv.s[0] = mb->PMV[0][1][1]/2;
  bmv.s[1] = mb->PMV[0][1][0];
  bmv.s[2] = mb->PMV[1][1][1]/2;
  bmv.s[3] = mb->PMV[1][1][0];

  /* First Y Block */
  *dy++ = GFXBLOCK + 4 + (y1size>>2);
  *dy++ = (1<<30) | (2<<28) | dw1[0];
  *dy++ = xy;
  *dy++ = (8<<16) | 16;
  *dy++ = fmv.u[0];
  *dy++ = bmv.u[0];
  PACK_CORR_DATA(dy,block_ptr,y1size);
  block_ptr = (short *)((unsigned long)block_ptr + y1size);

  /* Second Y Block */
  *dy++ = GFXBLOCK + 4 + (y2size>>2);
  *dy++ = (1<<30) | (2<<28) | dw1[1];
  *dy++ = xy;
  *dy++ = (8<<16) | 16;
  *dy++ = fmv.u[1];
  *dy++ = bmv.u[1];
  PACK_CORR_DATA(dy,block_ptr,y2size);
  block_ptr = (short *)((unsigned long)block_ptr + y2size);
  /* End Y Blocks */

  fmv.s[0] /= 2;
  fmv.s[1] /= 2;
  fmv.s[2] /= 2;
  fmv.s[3] /= 2;
  
  bmv.s[0] /= 2;
  bmv.s[1] /= 2;
  bmv.s[2] /= 2;
  bmv.s[3] /= 2;

  xy >>= 1;

  /* U Blocks */
  *du++ = GFXBLOCK + 4 + (usize>>2);
  *du++ = (2<<30) | (1<<28) | dw1[0];
  *du++ = xy;
  *du++ = (4<<16) | 8;
  *du++ = fmv.u[0];
  *du++ = bmv.u[0];
  if(usize) {
    PACK_CORR_DATA_SHORT(du,block_ptr);
  }

  /* Second U Block */
  *du++ = GFXBLOCK + 4 + (usize>>2);
  *du++ = (2<<30) | (1<<28) | dw1[1];
  *du++ = xy;
  *du++ = (4<<16) | 8;
  *du++ = fmv.u[1];
  *du++ = bmv.u[1];
  if(usize) {
    block_ptr = (short *)((unsigned long)block_ptr + 16);
    PACK_CORR_DATA_SHORT(du,block_ptr);
    block_ptr = (short *)((unsigned long)block_ptr + 112);
  }
  /* End U Blocks */

  /* V Blocks */
  *dv++ = GFXBLOCK + 4 + (vsize>>2);
  *dv++ = (3<<30) | (1<<28) | dw1[0];
  *dv++ = xy;
  *dv++ = (4<<16) | 8;
  *dv++ = fmv.u[0];
  *dv++ = bmv.u[0];
  if(vsize) {
    PACK_CORR_DATA_SHORT(dv,block_ptr);
  }

  /* Second V Block */
  *dv++ = GFXBLOCK + 4 + (vsize>>2);
  *dv++ = (3<<30) | (1<<28) | dw1[1];
  *dv++ = xy;
  *dv++ = (4<<16) | 8;
  *dv++ = fmv.u[1];
  *dv++ = bmv.u[1];
  if(vsize) {
    block_ptr = (short *)((unsigned long)block_ptr + 16);
    PACK_CORR_DATA_SHORT(dv,block_ptr);
    block_ptr = (short *)((unsigned long)block_ptr + 112);
  }
  /* End V Blocks */

  *datay = dy;
  *datau = du;
  *datav = dv;
}

/***************************************************************************
// Function: renderFieldinFrameDCT0
// Description: inline function that sets hardware parameters for a Field
//  encoded macroblock in a frame picture with DCT0.
***************************************************************************/
static __inline__ void renderFieldinFrameDCT0(uint **datay,uint **datau,
					      uint **datav,XvMCMacroBlock *mb,
					      short *block_ptr,uint flags) {

  register uint *dy = *datay;
  register uint *du = *datau;
  register uint *dv = *datav;

  /* Motion Vectors */
  su_t fmv;
  su_t bmv;
  /* CBP */
  uint cbp = (uint)mb->coded_block_pattern;
  /* gfxblock dword 1 */
  uint dw1[2];

  short * top_left_b = NULL;
  short * top_right_b = NULL;
  short * bottom_left_b = NULL;
  short * bottom_right_b = NULL;

  unsigned int ysize = y_dct0_field_bytes[cbp];
  unsigned int usize = u_field_bytes[cbp];
  unsigned int vsize = v_field_bytes[cbp];

  uint xy = ((uint)mb->x<<20) | ((uint)mb->y<<3);

  dw1[0] = type_table[mb->macroblock_type & 0xf] | (0x2<<6) |
    mvfs_table[mb->motion_vertical_field_select & 3] |
    ((cbp | ((cbp<<2) & 0x30))<<22);
  
  dw1[1] = type_table[mb->macroblock_type & 0xf] | (0x3<<6) |
    mvfs_table[mb->motion_vertical_field_select>>2] |
    ((cbp | ((cbp<<2) & 0x30))<<22);


  fmv.s[0] = mb->PMV[0][0][1]/2;
  fmv.s[1] = mb->PMV[0][0][0];
  fmv.s[2] = mb->PMV[1][0][1]/2;
  fmv.s[3] = mb->PMV[1][0][0];
  
  bmv.s[0] = mb->PMV[0][1][1]/2;
  bmv.s[1] = mb->PMV[0][1][0];
  bmv.s[2] = mb->PMV[1][1][1]/2;
  bmv.s[3] = mb->PMV[1][1][0];

  /*
    The i810 cannot use DCT0 directly with field motion, we have to
    interlace the data for it. We use a zero block when the CBP has
    one half of the to-be-interlaced data but not the other half.
  */
  top_left_b = &empty_block[0];
  if(cbp & 0x20) {
    top_left_b = block_ptr;
    block_ptr += 64;
  }

  top_right_b = &empty_block[0];
  if(cbp & 0x10) {
    top_right_b = block_ptr;
    block_ptr += 64;
  }

  bottom_left_b = &empty_block[0];
  if(cbp & 0x8) {
    bottom_left_b = block_ptr;
    block_ptr += 64;
  }

  bottom_right_b = &empty_block[0];
  if(cbp & 0x4) {
    bottom_right_b = block_ptr;
    block_ptr += 64;
  }

  /* First Y Block */
  *dy++ = GFXBLOCK + 4 + (ysize>>2);
  *dy++ = (1<<30) | (2<<28) | dw1[0];
  *dy++ = xy;
  *dy++ = (8<<16) | 16;
  *dy++ = fmv.u[0];
  *dy++ = bmv.u[0];
  if(dw1[0] & (1<<27)) {
    PACK_CORR_DATA_0to1(dy,top_left_b,bottom_left_b);
  }
  if(dw1[0] & (1<<26)) {
    PACK_CORR_DATA_0to1(dy,top_right_b,bottom_right_b);
  }

  /* Second Y Block */
  *dy++ = GFXBLOCK + 4 + (ysize>>2);
  *dy++ = (1<<30) | (2<<28) | dw1[1];
  *dy++ = xy;
  *dy++ = (8<<16) | 16;
  *dy++ = fmv.u[1];
  *dy++ = bmv.u[1];
  if(dw1[1] & (1<<27)) {
    top_left_b = (short *)((unsigned long)top_left_b + 16);
    bottom_left_b = (short *)((unsigned long)bottom_left_b + 16);
    PACK_CORR_DATA_0to1(dy,top_left_b,bottom_left_b);
  }
  if(dw1[1] & (1<<26)) {
    top_right_b = (short *)((unsigned long)top_right_b + 16);
    bottom_right_b = (short *)((unsigned long)bottom_right_b + 16);
    PACK_CORR_DATA_0to1(dy,top_right_b,bottom_right_b);
  }
  /* End Y Blocks */

  fmv.s[0] /= 2;
  fmv.s[1] /= 2;
  fmv.s[2] /= 2;
  fmv.s[3] /= 2;
  
  bmv.s[0] /= 2;
  bmv.s[1] /= 2;
  bmv.s[2] /= 2;
  bmv.s[3] /= 2;

  xy >>= 1;

  /* U Blocks */
  *du++ = GFXBLOCK + 4 + (usize>>2);
  *du++ = (2<<30) | (1<<28) | dw1[0];
  *du++ = xy;
  *du++ = (4<<16) | 8;
  *du++ = fmv.u[0];
  *du++ = bmv.u[0];
  if(usize) {
    PACK_CORR_DATA_SHORT(du,block_ptr);
  }

  /* Second U Block */
  *du++ = GFXBLOCK + 4 + (usize>>2);
  *du++ = (2<<30) | (1<<28) | dw1[1];
  *du++ = xy;
  *du++ = (4<<16) | 8;
  *du++ = fmv.u[1];
  *du++ = bmv.u[1];
  if(usize) {
    block_ptr = (short *)((unsigned long)block_ptr + 16);
    PACK_CORR_DATA_SHORT(du,block_ptr);
    block_ptr = (short *)((unsigned long)block_ptr + 112);
  }
  /* End U Blocks */

  /* V Blocks */
  *dv++ = GFXBLOCK + 4 + (vsize>>2);
  *dv++ = (3<<30) | (1<<28) | dw1[0];
  *dv++ = xy;
  *dv++ = (4<<16) | 8;
  *dv++ = fmv.u[0];
  *dv++ = bmv.u[0];
  if(vsize) {
    PACK_CORR_DATA_SHORT(dv,block_ptr);
  }

  /* Second V Block */
  *dv++ = GFXBLOCK + 4 + (vsize>>2);
  *dv++ = (3<<30) | (1<<28) | dw1[1];
  *dv++ = xy;
  *dv++ = (4<<16) | 8;
  *dv++ = fmv.u[1];
  *dv++ = bmv.u[1];
  if(vsize) {
    block_ptr = (short *)((unsigned long)block_ptr + 16);
    PACK_CORR_DATA_SHORT(dv,block_ptr);
    block_ptr = (short *)((unsigned long)block_ptr + 112);
  }
  /* End V Blocks */

  *datay = dy;
  *datau = du;
  *datav = dv;
}

/***************************************************************************
// Function: renderFrameinFrame
// Description: inline function that sets hardware parameters for a Frame
//  encoded macroblock in a frame picture.
***************************************************************************/
static __inline__ void renderFrameinFrame(uint **datay,uint **datau,
					  uint **datav,
					  XvMCMacroBlock *mb,short *block_ptr,
					  uint flags) {

  register uint *dy = *datay;
  register uint *du = *datau;
  register uint *dv = *datav;

  /* Motion Vectors */
  su_t fmv;
  su_t bmv;
  /* gfxblock dword 1 */
  uint dw1;

  unsigned int ysize = y_frame_bytes[mb->coded_block_pattern];
  unsigned int usize = u_frame_bytes[mb->coded_block_pattern];
  unsigned int vsize = v_frame_bytes[mb->coded_block_pattern];

  uint xy = ((uint)mb->x<<20) | ((uint)mb->y<<4);

  dw1 = type_table[mb->macroblock_type & 0xf] |
    (((uint)mb->coded_block_pattern)<<22);


  fmv.s[0] = mb->PMV[0][0][1];
  fmv.s[1] = mb->PMV[0][0][0];
  
  bmv.s[0] = mb->PMV[0][1][1];
  bmv.s[1] = mb->PMV[0][1][0];

  /* Y Block */
  *dy++ = GFXBLOCK + 4 + (ysize>>2);
  *dy++ = (1<<30) | (3<<28) | dw1;
  *dy++ = xy;
  *dy++ = (16<<16) | 16;
  *dy++ = fmv.u[0];
  *dy++ = bmv.u[0];
  PACK_CORR_DATA(dy,block_ptr,ysize);
  block_ptr = (short *)((unsigned long)block_ptr + ysize);
  /* End Y Blocks */

  fmv.s[0] /= 2;
  fmv.s[1] /= 2;
  
  bmv.s[0] /= 2;
  bmv.s[1] /= 2;

  xy >>= 1;

  /* U Block */
  *du++ = GFXBLOCK + 4 + (usize>>2);
  *du++ = (2<<30) | (1<<28) | dw1;
  *du++ = xy;
  *du++ = (8<<16) | 8;
  *du++ = fmv.u[0];
  *du++ = bmv.u[0];
  PACK_CORR_DATA(du,block_ptr,usize);
  block_ptr = (short *)((unsigned long)block_ptr + usize);
  /* End U Block */

  /* V Block */
  *dv++ = GFXBLOCK + 4 + (vsize>>2);
  *dv++ = (3<<30) | (1<<28) | dw1;
  *dv++ = xy;
  *dv++ = (8<<16) | 8;
  *dv++ = fmv.u[0];
  *dv++ = bmv.u[0];
  PACK_CORR_DATA(dv,block_ptr,vsize);
  block_ptr = (short *)((unsigned long)block_ptr + vsize);
  /* End V Block */

  *datay = dy;
  *datau = du;
  *datav = dv;
}

/***************************************************************************
// Function: renderFrameinFrameDCT1
// Description: inline function that sets hardware parameters for a Frame
//  encoded macroblock in a frame picture with DCT type 1.
***************************************************************************/
static __inline__ void renderFrameinFrameDCT1(uint **datay,uint **datau,
					      uint **datav,XvMCMacroBlock *mb,
					      short *block_ptr,uint flags) {

  register uint *dy = *datay;
  register uint *du = *datau;
  register uint *dv = *datav;

  /* Motion Vectors */
  su_t fmv;
  su_t bmv;

  short * top_left_b = NULL;
  short * top_right_b = NULL;
  short * bottom_left_b = NULL;
  short * bottom_right_b = NULL;

  uint temp_bp = 0;

  uint ysize = y_dct1_frame_bytes[mb->coded_block_pattern];
  uint usize = u_frame_bytes[mb->coded_block_pattern];
  uint vsize = v_frame_bytes[mb->coded_block_pattern];

  uint xy = ((uint)mb->x<<20) | ((uint)mb->y<<4);

  uint dw1 = type_table[mb->macroblock_type & 0xf] |
    (((uint)mb->coded_block_pattern)<<22);

  fmv.s[0] = mb->PMV[0][0][1];
  fmv.s[1] = mb->PMV[0][0][0];
  
  bmv.s[0] = mb->PMV[0][1][1];
  bmv.s[1] = mb->PMV[0][1][0];

  /*
    It is easiest to find out what blocks are in need of reading first
    rather than as we go.
  */
  top_left_b = &empty_block[0];
  if(dw1 & (1<<27)) {
    temp_bp |= (1<<25);
    top_left_b = block_ptr;
    block_ptr += 64;
  }

  top_right_b = &empty_block[0];
  if(dw1 & (1<<26)) {
    temp_bp |= (1<<24);
    top_right_b = block_ptr;
    block_ptr += 64;
  }

  bottom_left_b = &empty_block[0];
  if(dw1 & (1<<25)) {
    temp_bp |= (1<<27);
    bottom_left_b = block_ptr;
    block_ptr += 64;
  }

  bottom_right_b = &empty_block[0];
  if(dw1 & (1<<24)) {
    temp_bp |= (1<<26);
    bottom_right_b = block_ptr;
    block_ptr += 64;
  }
  dw1 |= temp_bp;

  /* Y Block */
  *dy++ = GFXBLOCK + 4 + (ysize>>2);
  *dy++ = (1<<30) | (3<<28) | dw1;
  *dy++ = xy;
  *dy++ = (16<<16) | 16;
  *dy++ = fmv.u[0];
  *dy++ = bmv.u[0];
  if(dw1 & (1<<27)) {
    PACK_CORR_DATA_1to0(dy,top_left_b,bottom_left_b);
    top_left_b = (short *)((unsigned long)top_left_b + 64);
    bottom_left_b = (short *)((unsigned long)bottom_left_b + 64);
  }
  if(dw1 & (1<<26)) {
    PACK_CORR_DATA_1to0(dy,top_right_b,bottom_right_b);
    top_right_b = (short *)((unsigned long)top_right_b + 64);
    bottom_right_b = (short *)((unsigned long)bottom_right_b + 64);
  }
  if(dw1 & (1<<27)) {
    PACK_CORR_DATA_1to0(dy,top_left_b,bottom_left_b);
  }
  if(dw1 & (1<<26)) {
    PACK_CORR_DATA_1to0(dy,top_right_b,bottom_right_b);
  }
  /* End Y Block */

  fmv.s[0] /= 2;
  fmv.s[1] /= 2;
  
  bmv.s[0] /= 2;
  bmv.s[1] /= 2;

  xy >>= 1;

  /* U Block */
  *du++ = GFXBLOCK + 4 + (usize>>2);
  *du++ = (2<<30) | (1<<28) | dw1;
  *du++ = xy;
  *du++ = (8<<16) | 8;
  *du++ = fmv.u[0];
  *du++ = bmv.u[0];
  PACK_CORR_DATA(du,block_ptr,usize);
  block_ptr = (short *)((unsigned long)block_ptr + usize);

  /* V Block */
  *dv++ = GFXBLOCK + 4 + (vsize>>2);
  *dv++ = (3<<30) | (1<<28) | dw1;
  *dv++ = xy;
  *dv++ = (8<<16) | 8;
  *dv++ = fmv.u[0];
  *dv++ = bmv.u[0];
  PACK_CORR_DATA(dv,block_ptr,vsize);
  block_ptr = (short *)((unsigned long)block_ptr + vsize);

  *datay = dy;
  *datau = du;
  *datav = dv;
}

/***************************************************************************
// Function: renderDualPrimeinFrame
// Description: inline function that sets hardware parameters for a Dual
//  Prime encoded macroblock in a frame picture with dct 1.
***************************************************************************/
static __inline__ void renderDualPrimeinFrame(uint **datay,uint **datau,
					      uint **datav,XvMCMacroBlock *mb,
					      short *block_ptr,uint flags) {

  register uint *dy = *datay;
  register uint *du = *datau;
  register uint *dv = *datav;

  /* Motion Vectors */
  su_t fmv;
  su_t bmv;
  /* gfxblock dword 1 */
  uint dw1[2];

  uint y1size = y_first_field_bytes[mb->coded_block_pattern];
  uint y2size = y_second_field_bytes[mb->coded_block_pattern];
  uint usize = u_field_bytes[mb->coded_block_pattern];
  uint vsize = v_field_bytes[mb->coded_block_pattern];

  uint xy = ((uint)mb->x<<20) | ((uint)mb->y<<3);

  /*
    Past Surface (map 0) is used for same parity prediction,
    Future surface (map 1) is used for opposite.
  */
  dw1[0] = (((uint)mb->coded_block_pattern)<<22) |
    3<<12 | 2<<6 | 2<<3 | 3;
  dw1[1] = (((mb->coded_block_pattern & 0x3) |
	     ((mb->coded_block_pattern & 0xc)<<2))<<22) |
    3<<12 | 3<<6 | 3<<3 | 2;
  
  fmv.s[0] = mb->PMV[0][0][1];
  fmv.s[1] = mb->PMV[0][0][0];
  bmv.s[0] = mb->PMV[1][0][1];
  bmv.s[1] = mb->PMV[1][0][0];
  
  fmv.s[2] = mb->PMV[0][0][1];
  fmv.s[3] = mb->PMV[0][0][0];
  bmv.s[2] = mb->PMV[1][1][1];
  bmv.s[3] = mb->PMV[1][1][0];

  /* First Y Block */
  *dy++ = GFXBLOCK + 4 + (y1size>>2);
  *dy++ = (1<<30) | (2<<28) | dw1[0];
  *dy++ = xy;
  *dy++ = (8<<16) | 16;
  *dy++ = fmv.u[0];
  *dy++ = bmv.u[0];;
  PACK_CORR_DATA(dy,block_ptr,y1size);
  block_ptr = (short *)((unsigned long)block_ptr + y1size);

  /* Second Y Block */
  *dy++ = GFXBLOCK + 4 + (y2size>>2);
  *dy++ = (1<<30) | (2<<28) | dw1[1];
  *dy++ = xy;
  *dy++ = (8<<16) | 16;
  *dy++ = fmv.u[1];
  *dy++ = bmv.u[1];
  PACK_CORR_DATA(dy,block_ptr,y2size);
  block_ptr = (short *)((unsigned long)block_ptr + y2size);

  fmv.s[0] /= 2;
  fmv.s[1] /= 2;
  bmv.s[0] /= 2;
  bmv.s[1] /= 2;
  
  fmv.s[2] /= 2;
  fmv.s[3] /= 2;
  bmv.s[2] /= 2;
  bmv.s[3] /= 2;

  xy >>= 1;

  /* U Blocks */
  *du++ = GFXBLOCK + 4 + (usize>>2);
  *du++ = (2<<30) | (1<<28) | dw1[0];
  *du++ = xy;
  *du++ = (4<<16) | 8;
  *du++ = fmv.u[0];
  *du++ = bmv.u[0];
  if(dw1[0] & (1<<23)) {
    PACK_CORR_DATA_SHORT(du,block_ptr);
  }

  /* Second U Block */
  *du++ = GFXBLOCK + 4 + (usize>>2);
  *du++ = (2<<30) | (1<<28) | dw1[1];
  *du++ = xy;
  *du++ = (4<<16) | 8;
  *du++ = fmv.u[1];
  *du++ = bmv.u[1];
  if(dw1[1] & (1<<23)) {
    block_ptr = (short *)((unsigned long)block_ptr + 16);
    PACK_CORR_DATA_SHORT(du,block_ptr);
    block_ptr = (short *)((unsigned long)block_ptr + 112);
  }
  /* End U Blocks */

  /* V Blocks */
  *dv++ = GFXBLOCK + 4 + (vsize>>2);
  *dv++ = (3<<30) | (1<<28) | dw1[0];
  *dv++ = xy;
  *dv++ = (4<<16) | 8;
  *dv++ = fmv.u[0];
  *dv++ = bmv.u[0];
  if(dw1[0] & (1<<22)) {
    PACK_CORR_DATA_SHORT(dv,block_ptr);
  }

  /* Second V Block */
  *dv++ = GFXBLOCK + 4 + (vsize>>2);
  *dv++ = (3<<30) | (1<<28) | dw1[1];
  *dv++ = xy;
  *dv++ = (4<<16) | 8;
  *dv++ = fmv.u[1];
  *dv++ = bmv.u[1];
  if(dw1[1] & (1<<22)) {
    block_ptr = (short *)((unsigned long)block_ptr + 16);
    PACK_CORR_DATA_SHORT(dv,block_ptr);
    block_ptr = (short *)((unsigned long)block_ptr + 112);
  }
  /* End V Blocks */

  *datay = dy;
  *datau = du;
  *datav = dv;
}

/***************************************************************************
// Function: renderDualPrimeinFrameDCT0
// Description: inline function that sets hardware parameters for a Dual
//  Prime encoded macroblock in a frame picture with dct 0.
***************************************************************************/
static __inline__ void renderDualPrimeinFrameDCT0(uint **datay,uint **datau,
						  uint **datav,
						  XvMCMacroBlock *mb,
						  short *block_ptr,
						  uint flags) {

  register uint *dy = *datay;
  register uint *du = *datau;
  register uint *dv = *datav;

  /* Motion Vectors */
  su_t fmv;
  su_t bmv;
  /* gfxblock dword 1 */
  uint dw1[2];

  short * top_left_b = NULL;
  short * top_right_b = NULL;
  short * bottom_left_b = NULL;
  short * bottom_right_b = NULL;

  uint cbp = (uint)mb->coded_block_pattern;

  uint ysize = y_dct0_field_bytes[cbp];
  uint usize = u_field_bytes[cbp];
  uint vsize = v_field_bytes[cbp];

  uint xy = ((uint)mb->x<<20) | ((uint)mb->y<<3);

  /*
    Past Surface (map 0) is used for same parity prediction,
    Future surface (map 1) is used for opposite.
  */
  dw1[0] = ((cbp | ((cbp<<2) & 0x30))<<22) |
    3<<12 | 2<<6 | 2<<3 | 3;
  dw1[1] = ((cbp | ((cbp<<2) & 0x30))<<22) |
    3<<12 | 3<<6 | 3<<3 | 2;
  
  fmv.s[0] = mb->PMV[0][0][1];
  fmv.s[1] = mb->PMV[0][0][0];
  bmv.s[0] = mb->PMV[1][0][1];
  bmv.s[1] = mb->PMV[1][0][0];
  
  fmv.s[2] = mb->PMV[0][0][1];
  fmv.s[3] = mb->PMV[0][0][0];
  bmv.s[2] = mb->PMV[1][1][1];
  bmv.s[3] = mb->PMV[1][1][0];
  
  /*
    The i810 cannot use DCT0 directly with field motion, we have to
    interlace the data for it. We use a zero block when the CBP has
    one half of the to-be-interlaced data but not the other half.
  */
  top_left_b = &empty_block[0];
  if(cbp & 0x20) {
    top_left_b = block_ptr;
    block_ptr += 64;
  }

  top_right_b = &empty_block[0];
  if(cbp & 0x10) {
    top_right_b = block_ptr;
    block_ptr += 64;
  }

  bottom_left_b = &empty_block[0];
  if(cbp & 0x8) {
    bottom_left_b = block_ptr;
    block_ptr += 64;
  }

  bottom_right_b = &empty_block[0];
  if(cbp & 0x4) {
    bottom_right_b = block_ptr;
    block_ptr += 64;
  }

  /* First Y Block */
  *dy++ = GFXBLOCK + 4 + (ysize>>2);
  *dy++ = (1<<30) | (2<<28) | dw1[0];
  *dy++ = xy;
  *dy++ = (8<<16) | 16;
  *dy++ = fmv.u[0];
  *dy++ = bmv.u[0];
  if(cbp & 0x20) {
    PACK_CORR_DATA_0to1(dy,top_left_b,bottom_left_b);
  }
  if(cbp & 0x10) {
    PACK_CORR_DATA_0to1(dy,top_right_b,bottom_right_b);
  }

  /* Second Y Block */
  *dy++ = GFXBLOCK + 4 + (ysize>>2);
  *dy++ = (1<<30) | (2<<28) | dw1[1];
  *dy++ = xy;
  *dy++ = (8<<16) | 16;
  *dy++ = fmv.u[1];
  *dy++ = bmv.u[1];
  if(cbp & 0x20) {
    top_left_b = (short *)((unsigned long)top_left_b + 16);
    bottom_left_b = (short *)((unsigned long)bottom_left_b + 16);
    PACK_CORR_DATA_0to1(dy,top_left_b,bottom_left_b);
  }
  if(cbp & 0x10) {
    top_right_b = (short *)((unsigned long)top_right_b + 16);
    bottom_right_b = (short *)((unsigned long)bottom_right_b + 16);
    PACK_CORR_DATA_0to1(dy,top_right_b,bottom_right_b);
  }
  /* End Y Blocks */


  fmv.s[0] /= 2;
  fmv.s[1] /= 2;
  bmv.s[0] /= 2;
  bmv.s[1] /= 2;
  
  fmv.s[2] /= 2;
  fmv.s[3] /= 2;
  bmv.s[2] /= 2;
  bmv.s[3] /= 2;

  xy >>= 1;

  /* U Blocks */
  *du++ = GFXBLOCK + 4 + (usize>>2);
  *du++ = (2<<30) | (1<<28) | dw1[0];
  *du++ = xy;
  *du++ = (4<<16) | 8;
  *du++ = fmv.u[0];
  *du++ = bmv.u[0];
  if(cbp & (1<<23)) {
    PACK_CORR_DATA_SHORT(du,block_ptr);
  }

  /* Second U Block */
  *du++ = GFXBLOCK + 4 + (usize>>2);
  *du++ = (2<<30) | (1<<28) | dw1[1];
  *du++ = xy;
  *du++ = (4<<16) | 8;
  *du++ = fmv.u[1];
  *du++ = bmv.u[1];
  if(cbp & (1<<23)) {
    block_ptr = (short *)((unsigned long)block_ptr + 16);
    PACK_CORR_DATA_SHORT(du,block_ptr);
    block_ptr = (short *)((unsigned long)block_ptr + 112);
  }
  /* End U Blocks */

  /* V Blocks */
  *dv++ = GFXBLOCK + 4 + (vsize>>2);
  *dv++ = (3<<30) | (1<<28) | dw1[0];
  *dv++ = xy;
  *dv++ = (4<<16) | 8;
  *dv++ = fmv.u[0];
  *dv++ = bmv.u[0];
  if(cbp & (1<<22)) {
    PACK_CORR_DATA_SHORT(dv,block_ptr);
  }

  /* Second V Block */
  *dv++ = GFXBLOCK + 4 + (vsize>>2);
  *dv++ = (3<<30) | (1<<28) | dw1[1];
  *dv++ = xy;
  *dv++ = (4<<16) | 8;
  *dv++ = fmv.u[1];
  *dv++ = bmv.u[1];
  if(cbp & (1<<22)) {
    block_ptr = (short *)((unsigned long)block_ptr + 16);
    PACK_CORR_DATA_SHORT(dv,block_ptr);
    block_ptr = (short *)((unsigned long)block_ptr + 112);
  }
  /* End V Blocks */

  *datay = dy;
  *datau = du;
  *datav = dv;
}


/***************************************************************************
// Function: XvMCRenderSurface
// Description: This function does the actual HWMC. Given a list of
//  macroblock structures it dispatched the hardware commands to execute
//  them. DMA buffer containing Y data are dispatched as they fill up
//  U and V DMA buffers are queued until all Y's are done. This minimizes
//  the context flipping and flushing required when switching between Y
//  U and V surfaces.
***************************************************************************/
#define UV_QUEUE 14
_X_EXPORT Status XvMCRenderSurface(Display *display, XvMCContext *context,
			 unsigned int picture_structure,
			 XvMCSurface *target_surface,
			 XvMCSurface *past_surface,
			 XvMCSurface *future_surface,
			 unsigned int flags,
			 unsigned int num_macroblocks,
			 unsigned int first_macroblock,
			 XvMCMacroBlockArray *macroblock_array,
			 XvMCBlockArray *blocks) {
  /* Dma Data Structures */
  drmBufPtr pDMAy = NULL,pDMAu[UV_QUEUE],pDMAv[UV_QUEUE];
  int u_index = 0,v_index = 0;
  int dirty_context = 1;

  /* Block Pointer */
  short *block_ptr;
  /* Current Macroblock Pointer */
  XvMCMacroBlock *mb;

  drm_i810_mc_t mc;
  int i,j;
  i810XvMCSurface *privTarget;
  i810XvMCSurface *privFuture = NULL;
  i810XvMCSurface *privPast = NULL;
  i810XvMCContext *pI810XvMC;

  /* DMA Pointers set to NULL */
  uint *datay = NULL;
  uint *datau = NULL;
  uint *datav = NULL;


  /* Check Parameters for validity */
  if((target_surface == NULL) || (context == NULL) || (display == NULL)) {
    printf("Error, Invalid Target,Context, or DIsplay!\n");   
    return BadValue;
  }

  if(num_macroblocks == 0) {return Success;}
  if((macroblock_array == NULL) || (blocks == NULL)) {return BadValue;}
  if(context->privData == NULL) {return BadValue;}
  pI810XvMC = (i810XvMCContext *)context->privData;


  if(target_surface->privData == NULL) {
    printf("Error, Invalid Target Surface!\n");
    return BadValue;
  }
  privTarget = (i810XvMCSurface *)target_surface->privData;

  if(macroblock_array->num_blocks < (num_macroblocks + first_macroblock)) {
    printf("Error, Too many macroblocks requested for MB array size.\n");
    return BadValue;
  }

  /* Test For YV12 Surface */
  if(context->surface_type_id != FOURCC_YV12) {
    printf("Error, HWMC only possible on YV12 Surfaces\n");
    return BadValue;
  }

  /* P Frame Test */
  if(past_surface == NULL) {
    /* Just to avoid some ifs later. */
    privPast = privTarget;
  }
  else {
    if(past_surface->privData == NULL) {
      printf("Error, Invalid Past Surface!\n");
      return BadValue;
    }
    privPast = (i810XvMCSurface *)past_surface->privData;
  }


  /* B Frame Test */
  if(future_surface == NULL) {
    privFuture = privTarget;
    if(pI810XvMC->dual_prime) {
      privFuture = privPast;
      /* I810 Specific flag for marking when dual prime is in use. */
      flags |= 0x40000000;
    }

    /*
      References are different for the Second Field Picture. The
      i810 needs to know if it is the second field picture in a
      P picture. We use a Private flag to mark this.
    */
    if(flags & XVMC_SECOND_FIELD) {
      /* I810 Specific flag for marking second fields. */
      flags |= 0x80000000;
    }
  }
  else {
    if((future_surface->privData == NULL) || (past_surface == NULL)) {
      printf("Error, Invalid Future Surface or No Past Surface!\n");
      return BadValue;
    }
    privFuture = (i810XvMCSurface *)future_surface->privData;

    /*
      Undo Second Field flag since the second field in B frames is just like
      the first.
    */
    flags &= ~0x80000000;
  }

  /* Lock For DMA */
  I810_LOCK(pI810XvMC,0);

  for(i=first_macroblock; i<(num_macroblocks + first_macroblock); i++) {
    /* Set up values needed for each macroblock */
    mb = &macroblock_array->macro_blocks[i];
    block_ptr = &(blocks->blocks[mb->index<<6]);

    /* Lockup can happen if the coordinates are too far out of range */
    if(mb->x > target_surface->width>>4) {
      mb->x = 0;
    }
    if(mb->y > target_surface->height>>4) {
      mb->y = 0;
    }

    /* If buffers are almost full dispatch them */
    if(datay) {
      pDMAy->used = (unsigned long)datay - (unsigned long)pDMAy->address;
      if(pDMAy->used  > 3520) {
	if(dirty_context) {
	  dispatchYContext(privTarget,privPast,privFuture,pI810XvMC);
	}
	dirty_context = 0;
	mc.idx = pDMAy->idx;
	mc.used = pDMAy->used;
	datay = NULL;
	mc.last_render = ++pI810XvMC->last_render;
	privTarget->last_render = pI810XvMC->last_render;
	I810_MC(pI810XvMC,mc);
      } /* datay near full */
    } /* if(datay) */
    if(datau) {
      pDMAu[u_index]->used = (unsigned long)datau - (unsigned long)pDMAu[u_index]->address;
      if(pDMAu[u_index]->used > 3904) {
	u_index++;
	datau = NULL;
	if(u_index == UV_QUEUE) {
	  for(j=0; j<UV_QUEUE; j++) {
	    mc.idx = pDMAu[j]->idx;
	    mc.used = pDMAu[j]->used;
	    mc.last_render = ++pI810XvMC->last_render;
	    privTarget->last_render = pI810XvMC->last_render;
	    I810_MC(pI810XvMC,mc);
	  }
	  u_index = 0;
	  dirty_context = 1;
	} /* if(u_index == UV_QUEUE) */
      } /* datau near full */
    } /* if(datau) */
    if(datav) {
      pDMAv[v_index]->used = (unsigned long)datav - (unsigned long)pDMAv[v_index]->address;
      if(pDMAv[v_index]->used > 3904) {
	v_index++;
	datav = NULL;
	if(v_index == UV_QUEUE) {
	  for(j=0; j<UV_QUEUE; j++) {
	    mc.idx = pDMAv[j]->idx;
	    mc.used = pDMAv[j]->used;
	    mc.last_render = ++pI810XvMC->last_render;
	    privTarget->last_render = pI810XvMC->last_render;
	    I810_MC(pI810XvMC,mc);
	  }
	  v_index = 0;
	  dirty_context = 1;
	} /* if(v_index == UV_QUEUE) */
      } /* datav near full */
    } /* if(datav) */

    /* Allocate buffers if this is the first loop,or if we just dispatched */
    if(datay == NULL) {
      pDMAy = i810_get_free_buffer(pI810XvMC);
      datay = pDMAy->address;
    }/* if(datay == NULL) */
    if(datau == NULL) {
      pDMAu[u_index] = i810_get_free_buffer(pI810XvMC);
      datau = pDMAu[u_index]->address;
      if(u_index == 0) {
	*datau++ = CMD_FLUSH;
	*datau++ = BOOLEAN_ENA_2;
	*datau++ = CMD_FLUSH;
	*datau++ = DEST_BUFFER_INFO;
	*datau++ = privTarget->dbi1u;
	*datau++ = DEST_BUFFER_VAR;
	*datau++ = privTarget->dbv1;
	/* Past Surface */
	*datau++ = CMD_MAP_INFO;
	*datau++ = privPast->mi1u;
	*datau++ = privPast->mi2u;
	*datau++ = privPast->mi3u;
	/* Future Surface */
	*datau++ = CMD_MAP_INFO;
	*datau++ = privFuture->mi1u | 0x1<<28;
	*datau++ = privFuture->mi2u;
	*datau++ = privFuture->mi3u;
      }
    } /* if(datau == NULL) */
    if(datav == NULL) {
      pDMAv[v_index] = i810_get_free_buffer(pI810XvMC);
      datav = pDMAv[v_index]->address;
      if(v_index == 0) {
	*datav++ = CMD_FLUSH;
	*datav++ = BOOLEAN_ENA_2;
	*datav++ = CMD_FLUSH;
	*datav++ = DEST_BUFFER_INFO;
	*datav++ = privTarget->dbi1v;
	*datav++ = DEST_BUFFER_VAR;
	*datav++ = privTarget->dbv1;
	/* Past Surface */
	*datav++ = CMD_MAP_INFO;
	*datav++ = privPast->mi1v;
	*datav++ = privPast->mi2v;
	*datav++ = privPast->mi3v;
	/* Future Surface */
	*datav++ = CMD_MAP_INFO;
	*datav++ = privFuture->mi1v | 0x1<<28;
	*datav++ = privFuture->mi2v;
	*datav++ = privFuture->mi3v;
      }
    }/* if(datav == NULL) */

    /* Catch no pattern case */
    if(!(mb->macroblock_type & 0x8)) {
      mb->coded_block_pattern = 0;
    }


    if(mb->motion_type == XVMC_PREDICTION_DUAL_PRIME) {
      /*
	By default the maps will not be set up for dual
	prime. We have to change them.
      */
      if(!pI810XvMC->dual_prime) {
	pI810XvMC->dual_prime = 1;
	privFuture = privPast;
	/* Y */
	*datay++ = CMD_MAP_INFO;
	*datay++ = privFuture->mi1y | 0x1<<28;
	*datay++ = privFuture->mi2y;
	*datay++ = privFuture->mi3y;
	/* U */
	*datau++ = CMD_MAP_INFO;
	*datau++ = privFuture->mi1u | 0x1<<28;
	*datau++ = privFuture->mi2u;
	*datau++ = privFuture->mi3u;
	/* V */
	*datav++ = CMD_MAP_INFO;
	*datav++ = privFuture->mi1v | 0x1<<28;
	*datav++ = privFuture->mi2v;
	*datav++ = privFuture->mi3v;
      }
    }
    if((pI810XvMC->dual_prime) &&
       (mb->motion_type != XVMC_PREDICTION_DUAL_PRIME)) {
      	pI810XvMC->dual_prime = 0;
	privFuture = privTarget;
	/* Y */
	*datay++ = CMD_MAP_INFO;
	*datay++ = privFuture->mi1y | 0x1<<28;
	*datay++ = privFuture->mi2y;
	*datay++ = privFuture->mi3y;
	/* U */
	*datau++ = CMD_MAP_INFO;
	*datau++ = privFuture->mi1u | 0x1<<28;
	*datau++ = privFuture->mi2u;
	*datau++ = privFuture->mi3u;
	/* V */
	*datav++ = CMD_MAP_INFO;
	*datav++ = privFuture->mi1v | 0x1<<28;
	*datav++ = privFuture->mi2v;
	*datav++ = privFuture->mi3v;
    }


    /* Frame Picture */
    if((picture_structure & XVMC_FRAME_PICTURE) == XVMC_FRAME_PICTURE) {
      /* Intra Blocks */
      if(mb->macroblock_type & XVMC_MB_TYPE_INTRA) {
	if(mb->dct_type) {
	  renderIntrainFrameDCT1(&datay,&datau,&datav,mb,block_ptr,flags);
	  continue;
	}
	renderIntrainFrame(&datay,&datau,&datav,mb,block_ptr);
	continue;
      }
      switch((mb->motion_type & 0x3) | (mb->dct_type<<2)) {
      case 0x2:  /* Frame DCT0 */
	renderFrameinFrame(&datay,&datau,&datav,mb,block_ptr,flags);
	continue;
      case 0x5:  /* Field DCT1 */
	renderFieldinFrame(&datay,&datau,&datav,mb,block_ptr,flags);
	continue;
      case 0x6: /* Frame DCT1 */
	renderFrameinFrameDCT1(&datay,&datau,&datav,mb,block_ptr,flags);
	continue;
      case 0x1: /* Field DCT0 */
	renderFieldinFrameDCT0(&datay,&datau,&datav,mb,block_ptr,flags);
	continue;
      case 0x3:  /* Dual Prime DCT0 */
	renderDualPrimeinFrame(&datay,&datau,&datav,mb,block_ptr,flags);
	continue;
      case 0x7:  /* Dual Prime DCT1 */
	renderDualPrimeinFrameDCT0(&datay,&datau,&datav,mb,block_ptr,flags);
	continue;
      default:  /* No Motion Type */
	renderError();
	continue;
      } /* Switch */
    } /* Frame Picture */

    /* Field Pictures */
    if(mb->macroblock_type & XVMC_MB_TYPE_INTRA) {
      renderIntrainField(&datay,&datau,&datav,mb,block_ptr,picture_structure);
      continue;
    }
    switch(mb->motion_type & 0x3) {
    case 0x1: /* Field Motion */
      renderFieldinField(&datay,&datau,&datav,mb,block_ptr,picture_structure,
			 flags);
      continue;
    case 0x2: /* 16x8 Motion */
      render16x8inField(&datay,&datau,&datav,mb,block_ptr,picture_structure,
			flags);
      continue;
    case 0x3: /* Dual Prime */
      renderDualPrimeinField(&datay,&datau,&datav,mb,block_ptr,
			     picture_structure,flags);
      continue;
    default:  /* No Motion Type */
      renderError();
      continue;
    }
    continue;

  } /* for each Macroblock */

  /* Dispatch remaining DMA buffers */
  if(dirty_context) {
    dispatchYContext(privTarget,privPast,privFuture,pI810XvMC);
  }
  mc.idx = pDMAy->idx;
  mc.used = (unsigned long)datay - (unsigned long)pDMAy->address;
  mc.last_render = ++pI810XvMC->last_render;
  privTarget->last_render = pI810XvMC->last_render;
  I810_MC(pI810XvMC,mc);

  pDMAu[u_index]->used = (unsigned long)datau - (unsigned long)pDMAu[u_index]->address;
  for(j=0; j<=u_index; j++) {
    mc.idx = pDMAu[j]->idx;
    mc.used = pDMAu[j]->used;
    mc.last_render = ++pI810XvMC->last_render;
    privTarget->last_render = pI810XvMC->last_render;
    I810_MC(pI810XvMC,mc);
  }
  pDMAv[v_index]->used = (unsigned long)datav - (unsigned long)pDMAv[v_index]->address;
  for(j=0; j<=v_index; j++) {
    mc.idx = pDMAv[j]->idx;
    mc.used = pDMAv[j]->used;
    mc.last_render = ++pI810XvMC->last_render;
    privTarget->last_render = pI810XvMC->last_render;
    I810_MC(pI810XvMC,mc);
  }

  I810_UNLOCK(pI810XvMC);

  return Success;
}

/***************************************************************************
// Function: XvMCPutSurface
// Description:
// Arguments:
//  display: Connection to X server
//  surface: Surface to be displayed
//  draw: X Drawable on which to display the surface
//  srcx: X coordinate of the top left corner of the region to be
//          displayed within the surface.
//  srcy: Y coordinate of the top left corner of the region to be
//          displayed within the surface.
//  srcw: Width of the region to be displayed.
//  srch: Height of the region to be displayed.
//  destx: X cordinate of the top left corner of the destination region
//         in the drawable coordinates.
//  desty: Y cordinate of the top left corner of the destination region
//         in the drawable coordinates.
//  destw: Width of the destination region.
//  desth: Height of the destination region.
//  flags: One or more of the following.
//     XVMC_TOP_FIELD - Display only the Top field of the surface.
//     XVMC_BOTTOM_FIELD - Display only the Bottom Field of the surface.
//     XVMC_FRAME_PICTURE - Display both fields or frame.
//
// Info: Portions of this function derived from i810_video.c (XFree86)
//
//   This function is organized so that we wait as long as possible before
//   touching the overlay registers. Since we don't know that the last
//   flip has happened yet we want to give the overlay as long as
//   possible to catch up before we have to check on its progress. This
//   makes it unlikely that we have to wait on the last flip.
***************************************************************************/
_X_EXPORT Status XvMCPutSurface(Display *display,XvMCSurface *surface,
		      Drawable draw, short srcx, short srcy,
		      unsigned short srcw, unsigned short srch,
		      short destx, short desty,
		      unsigned short destw, unsigned short desth,
		      int flags) {
  i810XvMCContext *pI810XvMC;
  i810XvMCSurface *pI810Surface;
  i810OverlayRecPtr pORegs;
  unsigned int ysrc_offset,uvsrc_offset;
  Box extents;
  uint window_width,window_height;
  unsigned int xscaleInt = 0,xscaleFract = 0,yscaleInt = 0,yscaleFract = 0;
  unsigned int xscaleFractUV = 0,xscaleIntUV = 0,yscaleFractUV = 0;
  unsigned int yscaleIntUV = 0,yPitch = 0,uvPitch = 0;
  unsigned int ovcmd = 0;
  uint d;
  double xscale,yscale;
  int diff;
  int clipped_srcx, clipped_srcy, clipped_destx, clipped_desty;
  int clipped_srcw, clipped_srch, clipped_destw, clipped_desth;
  uint x1,y1,root_width,root_height;
  int x2 = 0, y2 = 0,unused;
  uint nChilds;
  int stat;
  Window win,root,parent,*pChilds;


  if((display == NULL) || (surface == NULL)) {
    return BadValue;
  }

  if(surface->privData == NULL) {
    return (error_base + XvMCBadSurface);
  }
  pI810Surface = (i810XvMCSurface *)surface->privData;
  pI810XvMC = (i810XvMCContext *)pI810Surface->privContext;
  pORegs = (i810OverlayRecPtr)pI810XvMC->oregs;


  switch(surface->surface_type_id) {
  case FOURCC_YV12:
  case FOURCC_I420:
    yPitch = (srcw + 7) & ~7;
    uvPitch = ((srcw>>1) + 7) & ~7;
    if((flags & XVMC_FRAME_PICTURE) != XVMC_FRAME_PICTURE) {
      srch = srch>>1;
    }
    break;
  case FOURCC_UYVY:
  case FOURCC_YUY2:
  default:
    /* FIXME: Non Planar not fully implemented. */
    return BadValue;
    yPitch = ((srcw + 7) & ~7) << 1;
    break;
  }/* switch(surface->surface_type_id) */

  /*
    FIXME: This should be using the DRI's clip rect but that isn't
    all hooked up yet. This has some latency but we get by.
  */
  win = draw;
  XQueryTree(display,win,&root,&parent,&pChilds,&nChilds);
  if(nChilds) XFree(pChilds);
  XGetGeometry(display,win, &root, &x2, &y2, &window_width,
	       &window_height, &d, &d);
  x1 = x2;
  y1 = y2;
  win = parent;
  do {
    XQueryTree(display,win,&root,&parent,&pChilds,&nChilds);
    if(nChilds) XFree(pChilds);
    XGetGeometry(display,win, &root, &x2, &y2, &d, &d, &d, &d);
    x1 += x2;
    y1 += y2;
    win = parent;
  }while(win != root);
  XGetGeometry(display,root, &root, &unused, &unused,
               &root_width, &root_height, &d, &d);

  /* Left edge of Video window clipped to screen */
  extents.x1 = 0;
  if(x1 > extents.x1) {
    extents.x1 = x1;
  }
  /* Right edge of Video window clipped to screen */
  extents.x2 = root_width;
  if(extents.x2 > (x1 + window_width)) {
    extents.x2 = x1 + window_width;
  }
  /* Top edge of Video window clipped to screen */
  extents.y1 = 0;
  if(y1 > extents.y1) {
    extents.y1 = y1;
  }
  /* Bottom edge of Video window clipped to screen */
  extents.y2 = root_height;
  if(extents.y2 > (y1 + window_height)) {
    extents.y2 = y1 + window_height;
  }

  /*
    Clipping is more difficult than is seems. We need to keep the
    scaling factors even if the destination window needs to be clipped.
    We clip the destination window first then apply a scaled version
    to the source window.
  */

  /* Put destination coords in screen coords */
  destx += x1;
  desty += y1;

  /* Scale factors requested */
  xscale = (double)srcw / (double)destw;
  yscale = (double)srch / (double)desth;

  /*
    If destination window needs to be clipped we actually adjust both
    the src and dest window so as to keep the scaling that was requested
  */
  clipped_srcx = srcx;
  clipped_srcy = srcy;
  clipped_destx = destx;
  clipped_desty = desty;
  clipped_srcw = srcw;
  clipped_srch = srch;
  clipped_destw = destw;
  clipped_desth = desth;

  /* Clip to the source surface boundaries */
  if(clipped_srcx < 0) {
    clipped_destx += (0 - clipped_srcx) / xscale;
    clipped_srcw -= clipped_srcx;
    clipped_destw -= clipped_srcx / xscale;
    clipped_srcx = 0;
  }
  if((clipped_srcw + clipped_srcx) > surface->width) {
    clipped_srcw = surface->width - clipped_srcx;
    clipped_destw -= (clipped_srcw - srcw) / xscale;
  }
  if(clipped_srcy < 0) {
    clipped_desty += (0 - clipped_srcy) / yscale;
    clipped_srch -= clipped_srcy;
    clipped_desth -= clipped_srcy / yscale;
    clipped_srcy = 0;
  }
  if((clipped_srch + clipped_srcy) > surface->height) {
    clipped_srch = surface->height - clipped_srcy;
    clipped_desth -= (clipped_srch - srch) / yscale;
  }

  /* Clip to the extents */
  if(clipped_destx < extents.x1) {
    diff = extents.x1 - clipped_destx;
    clipped_srcx += diff * xscale;
    clipped_srcw -= diff * xscale;
    clipped_destw -= diff;
    clipped_destx = extents.x1;
  }

  diff = (clipped_destx + clipped_destw) - extents.x2;
  if(diff > 0) {
    clipped_destw -= diff;
    clipped_srcw -= diff * xscale;
  }

  if(clipped_desty < extents.y1) {
    diff = extents.y1 - clipped_desty;
    clipped_srcy += diff * yscale;
    clipped_srch -= diff * yscale;
    clipped_desth -= diff;
    clipped_desty = 0;
  }

  diff = (clipped_desty + clipped_desth) - extents.y2;
  if(diff > 0) {
    clipped_desth -= diff;
    clipped_srch -= diff * yscale;
  }

  /* If the whole window is clipped turn off the overlay */
  if((clipped_destx + clipped_destw < extents.x1) ||
     (clipped_desty + clipped_desth < extents.y1) ||
     (clipped_destx > extents.x2) ||
     (clipped_desty > extents.y2)) {
    return XvMCHideSurface(display, surface);
  }

  /*
    Adjust the source offset width and height according to the clipped
    destination window.
  */
  ysrc_offset = ((clipped_srcx + 1) & ~1) +
    ((clipped_srcy + 1) & ~1) * (1<<pI810Surface->pitch);
  uvsrc_offset = (clipped_srcx>>1) +
    (clipped_srcy>>1) * (1<<(pI810Surface->pitch - 1));

  /*
    Initially, YCbCr and Overlay Enable and
    vertical chrominance up interpolation and horozontal chrominance
    up interpolation
  */
  ovcmd = VC_UP_INTERPOLATION | HC_UP_INTERPOLATION |
    Y_ADJUST | OVERLAY_ENABLE;

  if ((clipped_destw != clipped_srcw) ||
      (clipped_desth != clipped_srch)) {
    xscaleInt = (clipped_srcw / clipped_destw) & 0x3;
    xscaleFract = (clipped_srcw << 12) / clipped_destw;
    yscaleInt = (clipped_srch / clipped_desth) & 0x3;
    yscaleFract = (clipped_srch << 12) / clipped_desth;
   
    if (clipped_destw > clipped_srcw) {
      /* horizontal up-scaling */
      ovcmd &= ~HORIZONTAL_CHROMINANCE_FILTER;
      ovcmd &= ~HORIZONTAL_LUMINANCE_FILTER;
      ovcmd |= (HC_UP_INTERPOLATION | HL_UP_INTERPOLATION);
    }

    if (clipped_desth > clipped_srch) {
      /* vertical up-scaling */
      ovcmd &= ~VERTICAL_CHROMINANCE_FILTER;
      ovcmd &= ~VERTICAL_LUMINANCE_FILTER;
      ovcmd |= (VC_UP_INTERPOLATION | VL_UP_INTERPOLATION);
    }

    if (clipped_destw < clipped_srcw) {
      /* horizontal down-scaling */
      ovcmd &= ~HORIZONTAL_CHROMINANCE_FILTER;
      ovcmd &= ~HORIZONTAL_LUMINANCE_FILTER;
      ovcmd |= (HC_DOWN_INTERPOLATION | HL_DOWN_INTERPOLATION);
    }

    if (clipped_desth < clipped_srch) {
      /* vertical down-scaling */
      ovcmd &= ~VERTICAL_CHROMINANCE_FILTER;
      ovcmd &= ~VERTICAL_LUMINANCE_FILTER;
      ovcmd |= (VC_DOWN_INTERPOLATION | VL_DOWN_INTERPOLATION);
    }

    /* now calculate the UV scaling factor */
    if (xscaleFract) {
      xscaleFractUV = xscaleFract >> MINUV_SCALE;
      ovcmd &= ~HC_DOWN_INTERPOLATION;
      ovcmd |= HC_UP_INTERPOLATION;
    }

    if (xscaleInt) {
      xscaleIntUV = xscaleInt >> MINUV_SCALE;
      if (xscaleIntUV) {
	ovcmd &= ~HC_UP_INTERPOLATION;
      }
    }

    if (yscaleFract) {
      yscaleFractUV = yscaleFract >> MINUV_SCALE;
      ovcmd &= ~VC_DOWN_INTERPOLATION;
      ovcmd |= VC_UP_INTERPOLATION;
    }

    if (yscaleInt) {
      yscaleIntUV = yscaleInt >> MINUV_SCALE;
      if (yscaleIntUV) {
	ovcmd &= ~VC_UP_INTERPOLATION;
	ovcmd |= VC_DOWN_INTERPOLATION;
      }
    }

  }/* if((destw != srcw) || (desth != srch)) */

  /* Lock the DRM */
  I810_LOCK(pI810XvMC,0);

  /* Block until rendering on this surface is finished */
  stat = XVMC_RENDERING;
  while(stat & XVMC_RENDERING) {
    XvMCGetSurfaceStatus(display,surface,&stat);
  }
  /* Block until the last flip is finished */
  if(pI810XvMC->last_flip) {
    BLOCK_OVERLAY(pI810XvMC,pI810XvMC->current);
  }

  pI810XvMC->current = !pI810XvMC->current;
  pORegs->OV0CMD = ovcmd;

  if ((clipped_destw != clipped_srcw) ||
      (clipped_desth != clipped_srch)) {
    pORegs->YRGBSCALE = (xscaleInt << 15) |
      ((xscaleFract & 0xFFF) << 3) | (yscaleInt) |
      ((yscaleFract & 0xFFF) << 20);

    pORegs->UVSCALE = yscaleIntUV | ((xscaleFractUV & 0xFFF) << 3) |
      ((yscaleFractUV & 0xFFF) << 20);
  }
  else {
    /* Normal 1:1 scaling */
    pORegs->YRGBSCALE = 0x80004000;
    pORegs->UVSCALE = 0x80004000;
  }

  pORegs->SHEIGHT = clipped_srch | (clipped_srch << 15);
  pORegs->DWINPOS = (clipped_desty << 16) | clipped_destx;
  pORegs->DWINSZ = ((clipped_desth<< 16) | (clipped_destw));

  /* Attributes */
  pORegs->OV0CLRC0 = ((pI810XvMC->contrast & 0x1ff)<<8) |
    (pI810XvMC->brightness & 0xff);
  pORegs->OV0CLRC1 = (pI810XvMC->saturation & 0x3ff);
  
  /* Destination Colorkey Setup */
  pI810XvMC->oregs->DCLRKV = RGB16ToColorKey(pI810XvMC->colorkey);

  /* buffer locations, add the offset from the clipping */
  if(pI810XvMC->current) {
    pORegs->OBUF_1Y = (unsigned long)pI810Surface->offset +
      (unsigned long)pI810Surface->offsets[0] + ysrc_offset;
    pORegs->OBUF_1V = (unsigned long)pI810Surface->offset +
      (unsigned long)pI810Surface->offsets[2] + uvsrc_offset;
    pORegs->OBUF_1U = (unsigned long)pI810Surface->offset +
      (unsigned long)pI810Surface->offsets[1] + uvsrc_offset;
  }
  else {
    pORegs->OBUF_0Y = (unsigned long)pI810Surface->offset +
      (unsigned long)pI810Surface->offsets[0] + ysrc_offset;
    pORegs->OBUF_0V = (unsigned long)pI810Surface->offset +
      (unsigned long)pI810Surface->offsets[2] + uvsrc_offset;
    pORegs->OBUF_0U = (unsigned long)pI810Surface->offset +
      (unsigned long)pI810Surface->offsets[1] + uvsrc_offset;
  }

  switch(surface->surface_type_id) {
  case FOURCC_YV12:
  case FOURCC_I420:
    pORegs->SWID = (uvPitch << 16) | yPitch;
    pORegs->SWIDQW = (uvPitch << 13) | (yPitch >> 3);
    pORegs->OV0STRIDE = (1<<pI810Surface->pitch) | 
      ((1<<pI810Surface->pitch) << 15);
    pORegs->OV0CMD &= ~SOURCE_FORMAT;
    pORegs->OV0CMD |= YUV_420;
    if((flags & XVMC_FRAME_PICTURE) != XVMC_FRAME_PICTURE) {
      /* Top Field Only */
      if(flags & XVMC_TOP_FIELD) {
	if(pI810XvMC->current == 1) {
	  pORegs->OV0CMD |= (VERTICAL_PHASE_BOTH | FLIP_TYPE_FIELD |
			     BUFFER1_FIELD0);
	}
	else {
	  pORegs->OV0CMD |= (VERTICAL_PHASE_BOTH | FLIP_TYPE_FIELD |
			     BUFFER0_FIELD0);
	}
	pORegs->YRGB_VPH = 1<<15 | 1<<31;
	pORegs->UV_VPH = 3<<14 | 3<<30;
	pORegs->INIT_PH = 0x06 | 0x18;
      }
      /* Bottom Field Only */
      else {
	if(pI810XvMC->current == 1) {
	  pORegs->OV0CMD |= (VERTICAL_PHASE_BOTH | FLIP_TYPE_FIELD |
			     BUFFER1_FIELD1);
	}
	else {
	  pORegs->OV0CMD |= (VERTICAL_PHASE_BOTH | FLIP_TYPE_FIELD |
			     BUFFER0_FIELD1);
	}
	pORegs->YRGB_VPH = 0;
	pORegs->UV_VPH = 7<<29 | 7<<13;
	pORegs->INIT_PH = 0x06;
      }
    }
    /* Frame Picture */
    else {
      if(pI810XvMC->current == 1) {
	pORegs->OV0CMD |= BUFFER1_FIELD0;
      }
      else {
	pORegs->OV0CMD |= BUFFER0_FIELD0;
      }
      pORegs->YRGB_VPH = 0;
      pORegs->UV_VPH = 0;
      pORegs->INIT_PH = 0;
    }
    break;
  case FOURCC_UYVY:
  case FOURCC_YUY2:
  default:
    pORegs->SWID = srcw;
    pORegs->SWIDQW = srcw >> 3;
    pORegs->OV0STRIDE = pI810Surface->pitch;
    pORegs->OV0CMD &= ~SOURCE_FORMAT;
    pORegs->OV0CMD |= YUV_422;
    pORegs->OV0CMD &= ~OV_BYTE_ORDER;
    if (surface->surface_type_id == FOURCC_UYVY) {
      pORegs->OV0CMD |= Y_SWAP;
    }

    pORegs->OV0CMD &= ~BUFFER_AND_FIELD;
    if(pI810XvMC->current == 1) {
      pORegs->OV0CMD |= BUFFER1_FIELD0;
    }
    else {
      pORegs->OV0CMD |= BUFFER0_FIELD0;
    }

    break;
  } /* switch(surface->surface_type_id) */
  


  OVERLAY_FLIP(pI810XvMC);

  /*
    The Overlay only flips when it knows you changed
    something. So the first time change stuff while it
    is watching to be sure.
  */
  if(!pI810XvMC->last_flip) {
    pORegs->OV0CMD &= ~0x4;
    if(pI810XvMC->current == 1) {
      pORegs->OV0CMD |= BUFFER1_FIELD0;
    }
    else {
      pORegs->OV0CMD |= BUFFER0_FIELD0;
    }
  }
  pI810Surface->last_flip = ++pI810XvMC->last_flip;
  I810_UNLOCK(pI810XvMC);

  return Success;
}

/***************************************************************************
// Function: XvMCSyncSurface
// Arguments:
//   display - Connection to the X server
//   surface - The surface to synchronize
// Info:
// Returns: Status
***************************************************************************/
_X_EXPORT Status XvMCSyncSurface(Display *display,XvMCSurface *surface) {
  Status ret;
  int stat=0;
  /*
    FIXME: Perhaps a timer here to prevent lockup?
    FIXME: Perhaps a usleep to not be busy waiting?
  */
  do {
    ret = XvMCGetSurfaceStatus(display,surface,&stat);
  }while(!ret && (stat & XVMC_RENDERING));
  return ret;
}

/***************************************************************************
// Function: XvMCFlushSurface
// Description:
//   This function commits pending rendering requests to ensure that they
//   wll be completed in a finite amount of time.
// Arguments:
//   display - Connection to X server
//   surface - Surface to flush
// Info:
//   This command is a noop for i810 becuase we always dispatch buffers in
//   render. There is little gain to be had with 4k buffers.
// Returns: Status
***************************************************************************/
_X_EXPORT Status XvMCFlushSurface(Display * display, XvMCSurface *surface) {
  return Success;
}

/***************************************************************************
// Function: XvMCGetSurfaceStatus
// Description:
// Arguments:
//  display: connection to X server
//  surface: The surface to query
//  stat: One of the Following
//    XVMC_RENDERING - The last XvMCRenderSurface command has not
//                     completed.
//    XVMC_DISPLAYING - The surface is currently being displayed or a
//                     display is pending.
***************************************************************************/
_X_EXPORT Status XvMCGetSurfaceStatus(Display *display, XvMCSurface *surface,
			    int *stat) {
  i810XvMCSurface *privSurface;
  i810XvMCContext *pI810XvMC;
  int temp;

  if((display == NULL) || (surface == NULL) || (stat == NULL)) {
    return BadValue;
  }
  if(surface->privData == NULL) {
    return BadValue;
  }
  *stat = 0;
  privSurface = surface->privData;

  pI810XvMC = privSurface->privContext;
  if(pI810XvMC == NULL) {
    return (error_base + XvMCBadSurface);
  }

  I810_LOCK(pI810XvMC,0);
  if(privSurface->last_flip) {
    /* This can not happen */
    if(pI810XvMC->last_flip < privSurface->last_flip) {
      printf("Error: Context last flip is less than surface last flip.\n");
      return BadValue;
    }
    /*
      If the context has 2 or more flips after this surface it
      cannot be displaying. Don't bother to check.
    */
    if(!(pI810XvMC->last_flip > (privSurface->last_flip + 1))) {
      /*
	If this surface was the last flipped it is either displaying
	or about to be so don't bother checking.
      */
      if(pI810XvMC->last_flip == privSurface->last_flip) {
	*stat |= XVMC_DISPLAYING;
      }
      else {
	/*
	  In this case there has been one more flip since our surface's
	  but we need to check if it is finished or not.
	*/
	temp = GET_FSTATUS(pI810XvMC);
	if(((temp & (1<<20))>>20) != pI810XvMC->current) { 
	  *stat |= XVMC_DISPLAYING;
	}
      }
    }
  }

  if(privSurface->last_render &&
      (privSurface->last_render > GET_RSTATUS(pI810XvMC))) {
    *stat |= XVMC_RENDERING;
  }
  I810_UNLOCK(pI810XvMC);

  return Success;
}

/***************************************************************************
// 
//  Surface manipulation functions
//
***************************************************************************/

/***************************************************************************
// Function: XvMCHideSurface
// Description: Stops the display of a surface.
// Arguments:
//   display - Connection to the X server.
//   surface - surface to be hidden.
//
// Returns: Status
***************************************************************************/
_X_EXPORT Status XvMCHideSurface(Display *display, XvMCSurface *surface) {
  i810XvMCSurface *pI810Surface;
  i810XvMCContext *pI810XvMC;
  int ss, xx;

  /* Did we get a good display and surface passed into us? */
  if(display == NULL) {
    return BadValue;
  }

  if(surface == NULL) {
    return (error_base + XvMCBadSurface);
  }

  XvMCSyncSurface(display, surface);

  /* Get surface private data pointer */
  if(surface->privData == NULL) {
    return (error_base + XvMCBadSurface);
  }
  pI810Surface = (i810XvMCSurface *)surface->privData;

  /*
    Get the status of the surface, if it is not currently displayed
    we don't need to worry about it.
  */
  if((xx = XvMCGetSurfaceStatus(display, surface, &ss)) != Success) {
    return xx;
  }
  if(! (ss & XVMC_DISPLAYING)) {
    return Success;
  }
 
  /* Get the associated context pointer */
  pI810XvMC = (i810XvMCContext *)pI810Surface->privContext;
  if(pI810XvMC == NULL) {
    return (error_base + XvMCBadSurface);
  }

  if(pI810XvMC->last_flip) {
    I810_LOCK(pI810XvMC,DRM_LOCK_QUIESCENT);
    
    /* Make sure last flip is done */
    BLOCK_OVERLAY(pI810XvMC,pI810XvMC->current);

    /* Set the registers to turn the overlay off */
    pI810XvMC->oregs->OV0CMD = VC_UP_INTERPOLATION | HC_UP_INTERPOLATION |
      Y_ADJUST;
    pI810XvMC->current = !pI810XvMC->current;
    if(pI810XvMC->current == 1) {
      pI810XvMC->oregs->OV0CMD |= BUFFER1_FIELD0;
    }
    else {
      pI810XvMC->oregs->OV0CMD |= BUFFER0_FIELD0;
    }
    OVERLAY_FLIP(pI810XvMC);
    /*
      Increment the context flip but not the surface. This way no
      surface has the last flip #.
    */
    pI810XvMC->last_flip++;


    /* Now wait until the hardware reads the registers and makes the change. */
    BLOCK_OVERLAY(pI810XvMC,pI810XvMC->current)

      I810_UNLOCK(pI810XvMC);
  }

  return Success;
}




/***************************************************************************
//
// Functions that deal with subpictures
//
***************************************************************************/



/***************************************************************************
// Function: XvMCCreateSubpicture
// Description: This creates a subpicture by filling out the XvMCSubpicture
//              structure passed to it and returning Success.
// Arguments:
//   display - Connection to the X server.
//   context - The context to create the subpicture for.
//   subpicture - Pre-allocated XvMCSubpicture structure to be filled in.
//   width - of subpicture
//   height - of subpicture
//   xvimage_id - The id describing the XvImage format.
//
// Returns: Status
***************************************************************************/
_X_EXPORT Status XvMCCreateSubpicture(Display *display, XvMCContext *context,
                          XvMCSubpicture *subpicture,
                          unsigned short width, unsigned short height,
                          int xvimage_id) {
  i810XvMCContext *pI810XvMC;
  i810XvMCSubpicture *pI810Subpicture;
  int priv_count;
  uint *priv_data;
  Status ret;

  if((subpicture == NULL) || (context == NULL) || (display == NULL)){
    return BadValue;
  }
  
  pI810XvMC = (i810XvMCContext *)context->privData;
  if(pI810XvMC == NULL) {
    return (error_base + XvMCBadContext);
  }


  subpicture->context_id = context->context_id;
  subpicture->xvimage_id = xvimage_id;
  
  /* These need to be checked to make sure they are not too big! */
  subpicture->width = width;
  subpicture->height = height;

  subpicture->privData =
    (i810XvMCSubpicture *)malloc(sizeof(i810XvMCSubpicture));

  if(!subpicture->privData) {
    return BadAlloc;
  }
  pI810Subpicture = (i810XvMCSubpicture *)subpicture->privData;


  if((ret = _xvmc_create_subpicture(display, context, subpicture,
				    &priv_count, &priv_data))) {
    printf("Unable to create XvMCSubpicture.\n");
    return ret;
  }

  if(priv_count != 1) {
    printf("_xvmc_create_subpicture() returned incorrect data size.\n");
    printf("Expected 1 got %d\n",priv_count);
    free(priv_data);
    return BadAlloc;
  }
  /* Data == Client Address, offset == Physical address offset */
  pI810Subpicture->data = pI810XvMC->surfaces.address;
  pI810Subpicture->offset = pI810XvMC->surfaces.offset;

  /* Initialize private values */
  pI810Subpicture->privContext = pI810XvMC;

  pI810Subpicture->last_render = 0;
  pI810Subpicture->last_flip = 0;

  /* Based on the xvimage_id we will need to set the other values */
  subpicture->num_palette_entries = 16;
  subpicture->entry_bytes = 3;
  strcpy(subpicture->component_order,"YUV");

  /*
    i810's MC Engine needs surfaces of 2^x (x= 9,10,11,12) pitch
    and the Tiler need 512k aligned surfaces, basically we are
    stuck with fixed memory with pitch 1024.
  */
  pI810Subpicture->pitch = 10;

  /* 
     offsets[0] == offset into the map described by either
     address (Client memeory address) or offset (physical offset from fb base)
  */
  pI810Subpicture->offsets[0] = priv_data[0];
  if(((unsigned long)pI810Subpicture->data + pI810Subpicture->offsets[0]) & 4095) {
    printf("XvMCCreateSubpicture: Subpicture offset 0 is not 4096 aligned\n");
  }

  /* Free data returned from xvmc_create_surface */
  free(priv_data);

  /* Clear the surface to 0 */
  memset((void *)((unsigned long)pI810Subpicture->data + (unsigned long)pI810Subpicture->offsets[0]),
	 0, ((1<<pI810Subpicture->pitch) * subpicture->height));

  switch(subpicture->xvimage_id) {
  case FOURCC_IA44:
  case FOURCC_AI44:
    /* Destination buffer info command */
    pI810Subpicture->dbi1 = ((((unsigned int)pI810Subpicture->offset +
			       pI810Subpicture->offsets[0]) & ~0xfc000fff) |
			     (pI810Subpicture->pitch - 9));
    
    /* Destination buffer variables command */
    pI810Subpicture->dbv1 = (0x8<<20) | (0x8<<16);

    /* Map info command */
    pI810Subpicture->mi1 = (0x0<<24) | (3<<21) | (1<<9) |
      (pI810Subpicture->pitch - 3);

    pI810Subpicture->mi2 = (((unsigned int)subpicture->height - 1)<<16) |
      ((unsigned int)subpicture->width - 1);

    pI810Subpicture->mi3 = ((unsigned int)pI810Subpicture->offset +
			 pI810Subpicture->offsets[0]) & ~0xfc00000f;
    break;
  default:
    free(subpicture->privData);
    return BadMatch;
  }

  pI810XvMC->ref++;
  return Success;
}



/***************************************************************************
// Function: XvMCClearSubpicture
// Description: Clear the area of the given subpicture to "color".
//              structure passed to it and returning Success.
// Arguments:
//   display - Connection to the X server.
//   subpicture - Subpicture to clear.
//   x, y, width, height - rectangle in the subpicture to clear.
//   color - The data to file the rectangle with.
//
// Returns: Status
***************************************************************************/
_X_EXPORT Status XvMCClearSubpicture(Display *display, XvMCSubpicture *subpicture,
                          short x, short y,
                          unsigned short width, unsigned short height,
                          unsigned int color) {

  i810XvMCContext *pI810XvMC;
  i810XvMCSubpicture *pI810Subpicture;
  int i;

  if((subpicture == NULL) || (display == NULL)){
    return BadValue;
  }

  if(!subpicture->privData) {
    return (error_base + XvMCBadSubpicture);
  }
  pI810Subpicture = (i810XvMCSubpicture *)subpicture->privData;

  pI810XvMC = (i810XvMCContext *)pI810Subpicture->privContext;
  if(pI810XvMC == NULL) {
    return (error_base + XvMCBadSubpicture);
  }

  if((x < 0) || (x + width > subpicture->width)) {
    return BadValue;
  }

  if((y < 0) || (y + height > subpicture->height)) {
    return BadValue;
  }

  for(i=y; i<y + height; i++) {
    memset((void *)((unsigned long)pI810Subpicture->data +
		    (unsigned long)pI810Subpicture->offsets[0] + x +
		    (1<<pI810Subpicture->pitch) * i),(char)color,width);
  }

  return Success;
}

/***************************************************************************
// Function: XvMCCompositeSubpicture
// Description: Composite the XvImae on the subpicture. This composit uses
//              non-premultiplied alpha. Destination alpha is utilized
//              except for with indexed subpictures. Indexed subpictures
//              use a simple "replace".
// Arguments:
//   display - Connection to the X server.
//   subpicture - Subpicture to clear.
//   image - the XvImage to be used as the source of the composite.
//   srcx, srcy, width, height - The rectangle from the image to be used.
//   dstx, dsty - location in the subpicture to composite the source.
//
// Returns: Status
***************************************************************************/
_X_EXPORT Status XvMCCompositeSubpicture(Display *display, XvMCSubpicture *subpicture,
                               XvImage *image,
                               short srcx, short srcy,
                               unsigned short width, unsigned short height,
                               short dstx, short dsty) {

  i810XvMCContext *pI810XvMC;
  i810XvMCSubpicture *pI810Subpicture;
  int i;

  if((subpicture == NULL) || (display == NULL)){
    return BadValue;
  }

  if(!subpicture->privData) {
    return (error_base + XvMCBadSubpicture);
  }
  pI810Subpicture = (i810XvMCSubpicture *)subpicture->privData;

  pI810XvMC = (i810XvMCContext *)pI810Subpicture->privContext;
  if(pI810XvMC == NULL) {
    return (error_base + XvMCBadSubpicture);
  }

  if((srcx < 0) || (srcx + width > image->width)) {
    return BadValue;
  }

  if((dstx < 0) || (dstx + width > subpicture->width)) {
    return BadValue;
  }

  if((srcy < 0) || (srcy + height > image->height)) {
    return BadValue;
  }

  if((dsty < 0) || (dsty + height > subpicture->height)) {
    return BadValue;
  }

  for(i=0; i<height; i++) {
    memcpy((void *)((unsigned long)pI810Subpicture->data +
		    (unsigned long)pI810Subpicture->offsets[0] + dstx +
		    (1<<pI810Subpicture->pitch) * (i + dsty)),
	   (void *)((unsigned long)image->data +
		    (unsigned long)image->offsets[0] + srcx +
		    image->pitches[0] * (i + srcy))
	   ,width);
  }

  return Success;

}


/***************************************************************************
// Function: XvMCDestroySubpicture
// Description: Destroys the specified subpicture.
// Arguments:
//   display - Connection to the X server.
//   subpicture - Subpicture to be destroyed.
//
// Returns: Status
***************************************************************************/
_X_EXPORT Status XvMCDestroySubpicture(Display *display, XvMCSubpicture *subpicture) {

  i810XvMCSubpicture *pI810Subpicture;
  i810XvMCContext *pI810XvMC;

  if((display == NULL) || (subpicture == NULL)) {
    return BadValue;
  }
  if(!subpicture->privData) {
    return (error_base + XvMCBadSubpicture);
  }
  pI810Subpicture = (i810XvMCSubpicture *)subpicture->privData;

  pI810XvMC = (i810XvMCContext *)pI810Subpicture->privContext;
  if(!pI810XvMC) {
    return (error_base + XvMCBadSubpicture);
  }


  if(pI810Subpicture->last_render) {
    XvMCSyncSubpicture(display,subpicture);
  }

  _xvmc_destroy_subpicture(display,subpicture);

  i810_free_privContext(pI810XvMC);

  free(pI810Subpicture);
  subpicture->privData = NULL;
  return Success;
}


/***************************************************************************
// Function: XvMCSetSubpicturePalette
// Description: Set the subpictures palette
// Arguments:
//   display - Connection to the X server.
//   subpicture - Subpiture to set palette for.
//   palette - A pointer to an array holding the palette data. The array
//     is num_palette_entries * entry_bytes in size.
// Returns: Status
***************************************************************************/

_X_EXPORT Status XvMCSetSubpicturePalette(Display *display, XvMCSubpicture *subpicture,
				unsigned char *palette) {
 i810XvMCSubpicture *privSubpicture;
 int i,j;

 if((display == NULL) || (subpicture == NULL)) {
   return BadValue;
 }
 if(subpicture->privData == NULL) {
   return (error_base + XvMCBadSubpicture);
 }
 privSubpicture = (i810XvMCSubpicture *)subpicture->privData;

 j=0;
 for(i=0; i<16; i++) {
   privSubpicture->palette[0][i] = palette[j++];
   privSubpicture->palette[1][i] = palette[j++];
   privSubpicture->palette[2][i] = palette[j++];
 }
 return Success;
}

/***************************************************************************
// Function: XvMCBlendSubpicture
// Description: 
//    The behavior of this function is different depending on whether
//    or not the XVMC_BACKEND_SUBPICTURE flag is set in the XvMCSurfaceInfo.
//    i810 only support frontend behavior.
//  
//    XVMC_BACKEND_SUBPICTURE not set ("frontend" behavior):
//   
//    XvMCBlendSubpicture is a no-op in this case.
//   
// Arguments:
//   display - Connection to the X server.
//   subpicture - The subpicture to be blended into the video.
//   target_surface - The surface to be displayed with the blended subpic.
//   source_surface - Source surface prior to blending.
//   subx, suby, subw, subh - The rectangle from the subpicture to use.
//   surfx, surfy, surfw, surfh - The rectangle in the surface to blend
//      blend the subpicture rectangle into. Scaling can ocure if 
//      XVMC_SUBPICTURE_INDEPENDENT_SCALING is set.
//
// Returns: Status
***************************************************************************/
_X_EXPORT Status XvMCBlendSubpicture(Display *display, XvMCSurface *target_surface,
                         XvMCSubpicture *subpicture,
                         short subx, short suby,
                         unsigned short subw, unsigned short subh,
                         short surfx, short surfy,
                         unsigned short surfw, unsigned short surfh) {

  return BadMatch;
}



/***************************************************************************
// Function: XvMCBlendSubpicture2
// Description: 
//    The behavior of this function is different depending on whether
//    or not the XVMC_BACKEND_SUBPICTURE flag is set in the XvMCSurfaceInfo.
//    i810 only supports frontend blending.
//  
//    XVMC_BACKEND_SUBPICTURE not set ("frontend" behavior):
//   
//    XvMCBlendSubpicture2 blends the source_surface and subpicture and
//    puts it in the target_surface.  This does not effect the status of
//    the source surface but will cause the target_surface to query
//    XVMC_RENDERING until the blend is completed.
//   
// Arguments:
//   display - Connection to the X server.
//   subpicture - The subpicture to be blended into the video.
//   target_surface - The surface to be displayed with the blended subpic.
//   source_surface - Source surface prior to blending.
//   subx, suby, subw, subh - The rectangle from the subpicture to use.
//   surfx, surfy, surfw, surfh - The rectangle in the surface to blend
//      blend the subpicture rectangle into. Scaling can ocure if 
//      XVMC_SUBPICTURE_INDEPENDENT_SCALING is set.
//
// Returns: Status
***************************************************************************/
_X_EXPORT Status XvMCBlendSubpicture2(Display *display, 
                          XvMCSurface *source_surface,
                          XvMCSurface *target_surface,
                          XvMCSubpicture *subpicture,
                          short subx, short suby,
                          unsigned short subw, unsigned short subh,
                          short surfx, short surfy,
                          unsigned short surfw, unsigned short surfh) {
  drmBufPtr pDMA;
  unsigned int *data;
  i810XvMCContext *pI810XvMC;
  i810XvMCSubpicture *privSubpicture;
  i810XvMCSurface *privTarget;
  i810XvMCSurface *privSource;
  drm_i810_mc_t mc;
  int i,j;

  if(display == NULL) {
    return BadValue;
  }

  if(subpicture == NULL) {
    return (error_base + XvMCBadSubpicture);
  }

  if((target_surface == NULL) || (source_surface == NULL)) {
    return (error_base + XvMCBadSurface);
  }

  if((subpicture->xvimage_id != FOURCC_AI44) &&
     (subpicture->xvimage_id != FOURCC_IA44)) {
      return (error_base + XvMCBadSubpicture);
  }

  if(!subpicture->privData) {
    return (error_base + XvMCBadSubpicture);
  }
  privSubpicture = (i810XvMCSubpicture *)subpicture->privData;

  pI810XvMC = (i810XvMCContext *)privSubpicture->privContext;
  if(pI810XvMC == NULL) {
    return (error_base + XvMCBadSubpicture);
  }

  if(!target_surface->privData) {
    return (error_base + XvMCBadSurface);
  }
  privTarget = (i810XvMCSurface *)target_surface->privData;
  
  if(!source_surface->privData) {
    return (error_base + XvMCBadSurface);
  }
  privSource = (i810XvMCSurface *)source_surface->privData;
 

  /* Check that size isn't bigger than subpicture */
  if((subx + subw) > subpicture->width) {
    return BadValue;
  }
  if((suby + subh) > subpicture->height) {
    return BadValue;
  }
  /* Check that dest isn't bigger than surface */
  if((surfx + surfw) > target_surface->width) {
    return BadValue;
  }
  if((surfy + surfh) > target_surface->height) {
    return BadValue;
  }
  /* Make sure surfaces match */
  if(target_surface->width != source_surface->width) {
    return BadValue;
  }
  if(target_surface->height != source_surface->height) {
    return BadValue;
  }

  /* Lock For DMA */
  I810_LOCK(pI810XvMC,0);

  /* Allocate DMA buffer */
  pDMA = i810_get_free_buffer(pI810XvMC);
  data = pDMA->address;

  /* Copy Y data first */
  /* SOURCE_COPY_BLT */
  *data++ = (2<<29) | (0x43<<22) | 0x4;
  *data++ = (0xcc<<16) | (1<<26) | (1<<privTarget->pitch);
  *data++ = (target_surface->height<<16) | target_surface->width;
  *data++ = privTarget->offset + privTarget->offsets[0];
  *data++ = (1<<privSource->pitch);
  *data++ = privSource->offset + privSource->offsets[0];

  /* Select Context 1 for loading */
  *data++ = CMD_FLUSH;
  *data++ = (5<<23) | (1<<17) | (1<<8);
  *data++ = CMD_FLUSH;

  /* Load Palette */
  *data++ = MAP_PALETTE_LOAD;
  /* 16 levels of alpha for each Y */
  switch(subpicture->xvimage_id) {
  case FOURCC_IA44:
    for(i=0; i<16; i++) {
      for(j=0; j<16; j++) {
        *data++ = (j<<12) | (j<<8) | privSubpicture->palette[0][i];
      }
    }
    break;
  case FOURCC_AI44:
    for(i=0; i<16; i++) {
      for(j=0; j<16; j++) {
        *data++ = (i<<12) | (i<<8) | privSubpicture->palette[0][j];
      }
    }
    break;
  }
  /* TARGET */
  /* *data++ = CMD_FLUSH; */
  /* *data++ = BOOLEAN_ENA_2; */
  *data++ = CMD_FLUSH;
  *data++ = DEST_BUFFER_INFO;
  *data++ = privTarget->dbi1y;
  *data++ = DEST_BUFFER_VAR;
  *data++ = privTarget->dbv1;

  /* ALPHA */
  *data++ = CMD_MAP_INFO;
  *data++ = privSubpicture->mi1;
  *data++ = privSubpicture->mi2;
  *data++ = privSubpicture->mi3;

  *data++ = VERTEX_FORMAT | (1<<8) | (3<<1);
  *data++ = BOOLEAN_ENA_1;
  *data++ = SRC_DEST_BLEND_MONO | (0x940);
  /* Map Filter */
  *data++ = (3<<29) | (0x1c<<24) | (2<<19) | (0x224);

  /* Use context 1 */
  *data++ = CMD_FLUSH;
  *data++ = (5<<23) | (1<<16) | 1;
  *data++ = CMD_FLUSH;

  /* Drawing Rect Info */
  *data++ = DRAWING_RECT_INFO;
  *data++ = 0x0;
  *data++ = 0x0;
  *data++ = 0x0;
  *data++ = 0x0;
  *data++ = 0x0;

  /* GFXPRIMITIVE RECTANGLE */
  *data++ = (3<<29) | (0x1f<<24) | (0x7<<18) | 11;
  /* Bottom Right Vertex */
  *(float *)data++ = (float) (surfx + surfw);
  *(float *)data++ = (float) (surfy + surfh);
  *(float *)data++ = (float) (subx + subw);
  *(float *)data++ = (float) (suby + subh);
  /* Bottom Left Vertex */
  *(float *)data++ = (float) surfx;
  *(float *)data++ = (float) (surfy + surfh);
  *(float *)data++ = (float) subx;
  *(float *)data++ = (float) (suby + subh);
  /* Top Left Vertex */
  *(float *)data++ = (float) surfx;
  *(float *)data++ = (float) surfy;
  *(float *)data++ = (float) subx;
  *(float *)data++ = (float) suby;

  /* Load and Use Context 0 */
  *data++ = CMD_FLUSH;
  *data++ = (5<<23) | (1<<17) | (1<<16);
  *data++ = CMD_FLUSH;

  /* U data */
  /* SOURCE_COPY_BLT */
  *data++ = (2<<29) | (0x43<<22) | 0x4;
  *data++ = (0xcc<<16) | (1<<26) | (1<<(privTarget->pitch - 1));
  *data++ = (target_surface->height<<15) | (target_surface->width>>1);
  *data++ = (unsigned long)privTarget->offset + (unsigned long)privTarget->offsets[1];
  *data++ = (1<<(privSource->pitch - 1));
  *data++ = (unsigned long)privSource->offset + (unsigned long)privSource->offsets[1];

  /* Context 1 select */
  *data++ = CMD_FLUSH;
  *data++ = (5<<23) | (1<<17) | (1<<8);
  *data++ = CMD_FLUSH;
  /* ALPHA PALETTE */
  *data++ = MAP_PALETTE_LOAD;
  /* 16 levels of alpha for each Y */
  switch(subpicture->xvimage_id) {
  case FOURCC_IA44:
    for(i=0; i<16; i++) {
      for(j=0; j<16; j++) {
        *data++ = (j<<12) | (j<<8) | privSubpicture->palette[2][i];
      }
    }
    break;
  case FOURCC_AI44:
    for(i=0; i<16; i++) {
      for(j=0; j<16; j++) {
        *data++ = (i<<12) | (i<<8) | privSubpicture->palette[2][j];
      }
    }
    break;
  }
  /* TARGET */
  *data++ = CMD_FLUSH;
  *data++ = BOOLEAN_ENA_2;
  *data++ = CMD_FLUSH;
  *data++ = DEST_BUFFER_INFO;
  *data++ = privTarget->dbi1u;
  *data++ = DEST_BUFFER_VAR;
  *data++ = privTarget->dbv1;

  /* ALPHA */
  *data++ = CMD_MAP_INFO;
  *data++ = privSubpicture->mi1;
  *data++ = privSubpicture->mi2;
  *data++ = privSubpicture->mi3;

  *data++ = VERTEX_FORMAT | (1<<8) | (3<<1);
  *data++ = BOOLEAN_ENA_1;
  *data++ = SRC_DEST_BLEND_MONO | (0x940);
  /* Map Filter */
  *data++ = (3<<29) | (0x1c<<24) | (2<<19) | (1<<16) | (0x224);

  /* Use context 1 */
  *data++ = CMD_FLUSH;
  *data++ = (5<<23) | (1<<16) | 1;
  *data++ = CMD_FLUSH;

  /* Drawing Rect Info */
  *data++ = (3<<29) | (0x1d<<24) | (0x80<<16) | 3;
  *data++ = 0;
  *data++ = 0;
  *data++ = 0;
  *data++ = 0;
  *data++ = 0;

  /* Rectangle */
  *data++ = (3<<29) | (0x1f<<24) | (0x7<<18) | 11;
  /* Bottom Right */
  *(float *)data++ = (float) ((surfx + surfw)>>1);
  *(float *)data++ = (float) ((surfy + surfh)>>1);
  *(float *)data++ = (float) subx + subw;
  *(float *)data++ = (float) suby + subh;
  /* Bottom Left */
  *(float *)data++ = (float) (surfx>>1);
  *(float *)data++ = (float) ((surfy + surfh)>>1);
  *(float *)data++ = (float) subx;
  *(float *)data++ = (float) suby + subh;
  /* Top Left */
  *(float *)data++ = (float) (surfx>>1);
  *(float *)data++ = (float) (surfy>>1);
  *(float *)data++ = (float) subx;
  *(float *)data++ = (float) suby;

  /* Load and Use Context 0 */
  *data++ = CMD_FLUSH;
  *data++ = (5<<23) | (1<<17) | (1<<16);
  *data++ = CMD_FLUSH;

  /* V data */
  /* SOURCE_COPY_BLT */
  *data++ = (2<<29) | (0x43<<22) | 0x4;
  *data++ = (0xcc<<16) | (1<<26) | (1<<(privTarget->pitch - 1));
  *data++ = (target_surface->height<<15) | (target_surface->width>>1);
  *data++ = (unsigned long)privTarget->offset + (unsigned long)privTarget->offsets[2];
  *data++ = (1<<(privSource->pitch - 1));
  *data++ = (unsigned long)privSource->offset + (unsigned long)privSource->offsets[2];

  /* Context 1 select */
  *data++ = CMD_FLUSH;
  *data++ = (5<<23) | (1<<17) | (1<<8);
  *data++ = CMD_FLUSH;

  /* ALPHA PALETTE */
  *data++ = MAP_PALETTE_LOAD;
  /* 16 levels of alpha for each Y */
  switch(subpicture->xvimage_id) {
  case FOURCC_IA44:
    for(i=0; i<16; i++) {
      for(j=0; j<16; j++) {
        *data++ = (j<<12) | (j<<8) | privSubpicture->palette[1][i];
      }
    }
    break;
  case FOURCC_AI44:
    for(i=0; i<16; i++) {
      for(j=0; j<16; j++) {
        *data++ = (i<<12) | (i<<8) | privSubpicture->palette[1][j];
      }
    }
    break;
  }
  /* TARGET */
  *data++ = CMD_FLUSH;
  *data++ = BOOLEAN_ENA_2;
  *data++ = CMD_FLUSH;
  *data++ = DEST_BUFFER_INFO;
  *data++ = privTarget->dbi1v;
  *data++ = DEST_BUFFER_VAR;
  *data++ = privTarget->dbv1;

  /* ALPHA */
  *data++ = CMD_MAP_INFO;
  *data++ = privSubpicture->mi1;
  *data++ = privSubpicture->mi2;
  *data++ = privSubpicture->mi3;

  *data++ = VERTEX_FORMAT | (1<<8) | (3<<1);
  *data++ = BOOLEAN_ENA_1;
  *data++ = SRC_DEST_BLEND_MONO | (0x940);
  /* Map Filter */
  *data++ = (3<<29) | (0x1c<<24) | (2<<19) | (1<<16) | (0x224);

  /* Use context 1 */
  *data++ = CMD_FLUSH;
  *data++ = (5<<23) | (1<<16) | 1;
  *data++ = CMD_FLUSH;

  /* Drawing Rect Info */
  *data++ = (3<<29) | (0x1d<<24) | (0x80<<16) | 3;
  *data++ = 0;
  *data++ = 0;
  *data++ = 0;
  *data++ = 0;
  *data++ = 0;

  /* Rectangle */
  *data++ = (3<<29) | (0x1f<<24) | (0x7<<18) | 11;
  /* Bottom Right */
  *(float *)data++ = (float) ((surfx + surfw)>>1);
  *(float *)data++ = (float) ((surfy + surfh)>>1);
  *(float *)data++ = (float) subx + subw;
  *(float *)data++ = (float) suby + subh;
  /* Bottom Left */
  *(float *)data++ = (float) (surfx>>1);
  *(float *)data++ = (float) ((surfy + surfh)>>1);
  *(float *)data++ = (float) subx;
  *(float *)data++ = (float) suby + subh;
  /* Top Left */
  *(float *)data++ = (float) (surfx>>1);
  *(float *)data++ = (float) (surfy>>1);
  *(float *)data++ = (float) subx;
  *(float *)data++ = (float) suby;

  /* Load and Use Context 0 */
  *data++ = CMD_FLUSH;
  *data++ = (5<<23) | (1<<17) | (1<<16);
  *data++ = CMD_FLUSH;


  /* Dispatch */
  pDMA->used = (unsigned long)data - (unsigned long)pDMA->address;
  mc.idx = pDMA->idx;
  mc.used = pDMA->used;
  mc.last_render = ++pI810XvMC->last_render;
  privTarget->last_render = pI810XvMC->last_render;
  I810_MC(pI810XvMC,mc);

  I810_UNLOCK(pI810XvMC);
  return Success;
}



/***************************************************************************
// Function: XvMCSyncSubpicture
// Description: This function blocks until all composite/clear requests on
//              the subpicture have been complete.
// Arguments:
//   display - Connection to the X server.
//   subpicture - The subpicture to synchronize
//
// Returns: Status
***************************************************************************/
_X_EXPORT Status XvMCSyncSubpicture(Display *display, XvMCSubpicture *subpicture) {
  Status ret;
  int stat=0;
  do {
    ret = XvMCGetSubpictureStatus(display,subpicture,&stat);
  }while(!ret && (stat & XVMC_RENDERING));
  return ret;
}



/***************************************************************************
// Function: XvMCFlushSubpicture
// Description: This function commits pending composite/clear requests to
//              ensure that they will be completed in a finite amount of
//              time.
// Arguments:
//   display - Connection to the X server.
//   subpicture - The subpicture whos compsiting should be flushed
//
// Returns: Status
// NOTES: i810 always dispatches commands so flush is a no-op
***************************************************************************/
_X_EXPORT Status XvMCFlushSubpicture(Display *display, XvMCSubpicture *subpicture) {
  if(display == NULL) {
    return BadValue;
  }
  if(subpicture == NULL) {
    return (error_base + XvMCBadSubpicture);
  }

  return Success;
}



/***************************************************************************
// Function: XvMCGetSubpictureStatus
// Description: This function gets the current status of a subpicture
//
// Arguments:
//   display - Connection to the X server.
//   subpicture - The subpicture whos status is being queried
//   stat - The status of the subpicture. It can be any of the following
//          OR'd together:
//          XVMC_RENDERING  - Last composite or clear request not completed
//          XVMC_DISPLAYING - Suppicture currently being displayed.
//
// Returns: Status
// Notes: i810 always blends into a third surface so the subpicture is
//  never actually displaying, only a copy of it is displaying. We only
//  have to worry about the rendering case.
***************************************************************************/
_X_EXPORT Status XvMCGetSubpictureStatus(Display *display, XvMCSubpicture *subpicture,
                             int *stat) {

  i810XvMCSubpicture *privSubpicture;
  i810XvMCContext *pI810XvMC;

  if((display == NULL) || (stat == NULL)) {
    return BadValue;
  }
  if((subpicture == NULL) || (subpicture->privData == NULL)) {
    return (error_base + XvMCBadSubpicture);
  }
  *stat = 0;
  privSubpicture = (i810XvMCSubpicture *)subpicture->privData;

  pI810XvMC = (i810XvMCContext *)privSubpicture->privContext;
  if(pI810XvMC == NULL) {
    return (error_base + XvMCBadSubpicture);
  }

  I810_LOCK(pI810XvMC,0);

  if(privSubpicture->last_render &&
      (privSubpicture->last_render > GET_RSTATUS(pI810XvMC))) {
    *stat |= XVMC_RENDERING;
  }
  I810_UNLOCK(pI810XvMC);

  return Success;
}

#define NUM_XVMC_ATTRIBUTES 4
static XvAttribute I810_XVMC_ATTRIBUTES[] = {
  {XvGettable | XvSettable, 0, 0xffffff, "XV_COLORKEY"},
  {XvGettable | XvSettable, -127, +127, "XV_BRIGHTNESS"},
  {XvGettable | XvSettable, 0, 0x1ff, "XV_CONTRAST"},
  {XvGettable | XvSettable, 0, 0x3ff, "XV_SATURATION"}
};


/***************************************************************************
// Function: XvMCQueryAttributes
// Description: An array of XvAttributes of size "number" is returned by
//   this function. If there are no attributes, NULL is returned and number
//   is set to 0. The array may be freed with free().
//
// Arguments:
//   display - Connection to the X server.
//   context - The context whos attributes we are querying.
//   number - The number of returned atoms.
//
// Returns:
//  An array of XvAttributes.
// Notes:
//   For i810 we support these Attributes:
//    XV_COLORKEY: The colorkey value, initialized from the Xv value at
//                 context creation time.
//    XV_BRIGHTNESS
//    XV_CONTRAST
//    XV_SATURATION
***************************************************************************/
_X_EXPORT XvAttribute *XvMCQueryAttributes(Display *display, XvMCContext *context,
				 int *number) {
  i810XvMCContext *pI810XvMC;
  XvAttribute *attributes;

  if(number == NULL) {
    return NULL;
  }
  if(display == NULL) {
    *number = 0;
    return NULL;
  }
  if(context == NULL) {
    *number = 0;
    return NULL;
  }
  pI810XvMC = context->privData;
  if(pI810XvMC == NULL) {
    *number = 0;
    return NULL;
  }

  attributes = (XvAttribute *)malloc(NUM_XVMC_ATTRIBUTES *
				     sizeof(XvAttribute));
  if(attributes == NULL) {
    *number = 0;
    return NULL;
  }

  memcpy(attributes,I810_XVMC_ATTRIBUTES,(NUM_XVMC_ATTRIBUTES *
					  sizeof(XvAttribute)));

  *number = NUM_XVMC_ATTRIBUTES;
  return attributes;
}

/***************************************************************************
// Function: XvMCSetAttribute
// Description: This function sets a context-specific attribute.
//
// Arguments:
//   display - Connection to the X server.
//   context - The context whos attributes we are querying.
//   attribute - The X atom of the attribute to be changed.
//   value - The new value for the attribute.
//
// Returns:
//  Status
// Notes:
//   For i810 we support these Attributes:
//    XV_COLORKEY: The colorkey value, initialized from the Xv value at
//                 context creation time.
//    XV_BRIGHTNESS
//    XV_CONTRAST
//    XV_SATURATION
***************************************************************************/
_X_EXPORT Status XvMCSetAttribute(Display *display, XvMCContext *context,
			Atom attribute, int value) {
  i810XvMCContext *pI810XvMC;

  if(display == NULL) {
    return BadValue;
  }
  if(context == NULL) {
    return (error_base + XvMCBadContext);
  }
  pI810XvMC = context->privData;
  if(pI810XvMC == NULL) {
    return (error_base + XvMCBadContext);
  }

  if(attribute == pI810XvMC->xv_colorkey) {
    if((value < I810_XVMC_ATTRIBUTES[0].min_value) ||
       (value > I810_XVMC_ATTRIBUTES[0].max_value)) {
      return BadValue;
    }
    pI810XvMC->colorkey = value;
    return Success;
  }
  if(attribute == pI810XvMC->xv_brightness) {
    if((value < I810_XVMC_ATTRIBUTES[1].min_value) ||
       (value > I810_XVMC_ATTRIBUTES[1].max_value)) {
      return BadValue;
    }
    pI810XvMC->brightness = value;
    return Success;
  }
  if(attribute == pI810XvMC->xv_saturation) {
    if((value < I810_XVMC_ATTRIBUTES[2].min_value) ||
       (value > I810_XVMC_ATTRIBUTES[2].max_value)) {
      return BadValue;
    }
    pI810XvMC->saturation = value;
    return Success;
  }
  if(attribute == pI810XvMC->xv_contrast) {
    if((value < I810_XVMC_ATTRIBUTES[3].min_value) ||
       (value > I810_XVMC_ATTRIBUTES[3].max_value)) {
      return BadValue;
    }
    pI810XvMC->contrast = value;
    return Success;
  }
  return BadValue;
}

/***************************************************************************
// Function: XvMCGetAttribute
// Description: This function queries a context-specific attribute and
//   returns the value.
//
// Arguments:
//   display - Connection to the X server.
//   context - The context whos attributes we are querying.
//   attribute - The X atom of the attribute to be queried
//   value - The returned attribute value
//
// Returns:
//  Status
// Notes:
//   For i810 we support these Attributes:
//    XV_COLORKEY: The colorkey value, initialized from the Xv value at
//                 context creation time.
//    XV_BRIGHTNESS
//    XV_CONTRAST
//    XV_SATURATION
***************************************************************************/
_X_EXPORT Status XvMCGetAttribute(Display *display, XvMCContext *context,
			Atom attribute, int *value) {
  i810XvMCContext *pI810XvMC;

  if(display == NULL) {
    return BadValue;
  }
  if(context == NULL) {
    return (error_base + XvMCBadContext);
  }
  pI810XvMC = context->privData;
  if(pI810XvMC == NULL) {
    return (error_base + XvMCBadContext);
  }
  if(value == NULL) {
    return BadValue;
  }

  if(attribute == pI810XvMC->xv_colorkey) {
    *value = pI810XvMC->colorkey;
    return Success;
  }
  if(attribute == pI810XvMC->xv_brightness) {
    *value = pI810XvMC->brightness;
    return Success;
  }
  if(attribute == pI810XvMC->xv_saturation) {
    *value = pI810XvMC->saturation;
    return Success;
  }
  if(attribute == pI810XvMC->xv_contrast) {
    *value = pI810XvMC->contrast;
    return Success;
  }
  return BadValue;
}