summaryrefslogtreecommitdiff
path: root/src/via_swov.c
blob: ab0739032834b5379d71044dead5c407be89257c (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
/* $XFree86: xc/programs/Xserver/hw/xfree86/drivers/via/via_swov.c,v 1.11 2004/02/04 04:15:09 dawes Exp $ */
/*
 * Copyright 1998-2003 VIA Technologies, Inc. All Rights Reserved.
 * Copyright 2001-2003 S3 Graphics, Inc. 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
 * VIA, S3 GRAPHICS, 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.
 */
 
#include "xf86.h"
#include "xf86_OSproc.h"
#include "xf86_ansic.h"
#include "xf86fbman.h"

#include "via_compose.h"
#include "via_capture.h"

#include "via.h"
#ifdef XF86DRI
#include "xf86drm.h"
#endif

#include "via_overlay.h"
#include "via_driver.h"
#include "via_regrec.h"
#include "via_priv.h"
#include "via_swov.h"
#ifdef XF86DRI
#include "via_common.h"
#endif
#include "via_vgahw.h"
#include "via_id.h"

/*
 * Warning: this file contains revision checks which are CLE266 specific.
 * There seems to be no checking present for KM400 or more recent devices.
 *
 * TODO:
 *   - pVia->Chipset checking of course.
 *   - move content of pVia->HWDiff into pVia->swov
 *   - merge with CLEXF40040
 */
/*
 * HW Difference Flag
 * Moved here from via_hwdiff.c
 *
 * These are the entries of HWDiff used in our code (currently):
 *                     CLE266Ax   CLE266Cx   KM400     K8M800    PM800
 * ThreeHQVBuffer      FALSE      TRUE       TRUE      TRUE      TRUE
 * HQVFetchByteUnit    FALSE      TRUE       TRUE      TRUE      TRUE
 * SupportTwoColorKey  FALSE      TRUE       FALSE     FALSE     TRUE
 * HQVInitPatch        TRUE       FALSE      FALSE     FALSE     FALSE
 * HQVDisablePatch     FALSE      TRUE       TRUE      TRUE      FALSE
 *
 * This is now up to date with CLEXF40040. All unused entries were removed.
 * The functions depending on this struct are untouched. 
 *
 */
void 
VIAVidHWDiffInit(ScrnInfoPtr pScrn)
{
    VIAPtr pVia = VIAPTR(pScrn);
    VIAHWDiff *HWDiff = &pVia->HWDiff;

    switch(pVia->Chipset) {
    case VIA_CLE266:
	if (CLE266_REV_IS_AX(pVia->ChipRev)) {
	    HWDiff->dwThreeHQVBuffer = VID_HWDIFF_FALSE;
	    HWDiff->dwHQVFetchByteUnit = VID_HWDIFF_FALSE;
	    HWDiff->dwSupportTwoColorKey = VID_HWDIFF_FALSE;
	    HWDiff->dwHQVInitPatch = VID_HWDIFF_TRUE;
	    HWDiff->dwHQVDisablePatch = VID_HWDIFF_FALSE;
	} else {
	    HWDiff->dwThreeHQVBuffer = VID_HWDIFF_TRUE;
	    HWDiff->dwHQVFetchByteUnit = VID_HWDIFF_TRUE;
	    HWDiff->dwSupportTwoColorKey = VID_HWDIFF_TRUE;
	    HWDiff->dwHQVInitPatch = VID_HWDIFF_FALSE;
	    HWDiff->dwHQVDisablePatch = VID_HWDIFF_TRUE;
	}
	break;
    case VIA_KM400:
	HWDiff->dwThreeHQVBuffer = VID_HWDIFF_TRUE;
	HWDiff->dwHQVFetchByteUnit = VID_HWDIFF_TRUE;
	HWDiff->dwSupportTwoColorKey = VID_HWDIFF_FALSE;
	HWDiff->dwHQVInitPatch = VID_HWDIFF_FALSE;
	HWDiff->dwHQVDisablePatch = VID_HWDIFF_TRUE;
	break;
#ifdef HAVE_K8M800
    case VIA_K8M800:
	HWDiff->dwThreeHQVBuffer = VID_HWDIFF_TRUE;
	HWDiff->dwHQVFetchByteUnit = VID_HWDIFF_TRUE;
	HWDiff->dwSupportTwoColorKey = VID_HWDIFF_FALSE;
	HWDiff->dwHQVInitPatch = VID_HWDIFF_FALSE;
	HWDiff->dwHQVDisablePatch = VID_HWDIFF_TRUE;
	break;
#endif /* HAVE_K8M800 */
#ifdef HAVE_PM800
    case VIA_PM800:
	HWDiff->dwThreeHQVBuffer = VID_HWDIFF_TRUE;
	HWDiff->dwHQVFetchByteUnit = VID_HWDIFF_TRUE;
	HWDiff->dwSupportTwoColorKey = VID_HWDIFF_TRUE;
	HWDiff->dwHQVInitPatch = VID_HWDIFF_FALSE;
	HWDiff->dwHQVDisablePatch = VID_HWDIFF_FALSE;
	break;
#endif /* HAVE_PM800 */
    default:
	xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "VIAVidHWDiffInit: Unhandled ChipSet.\n");
    }
}     

void viaSetColorSpace(VIAPtr pVia, int hue, int saturation, int brightness, int contrast,
		      Bool reset)

{
    CARD32
	col1,col2;

    viaCalculateVideoColor(pVia,hue,saturation,brightness, contrast,reset,&col1,&col2);
    switch ( pVia->ChipId ) {
    case PCI_CHIP_VT3205:
	VIDOutD(V3_ColorSpaceReg_1, col1);
	VIDOutD(V3_ColorSpaceReg_2, col2);                    
	DBG_DD(ErrorF("000002C4 %08x\n",col1));
	DBG_DD(ErrorF("000002C8 %08x\n",col2));
	break;
    case PCI_CHIP_CLE3122:
	VIDOutD(V1_ColorSpaceReg_2, col2);
	VIDOutD(V1_ColorSpaceReg_1, col1);
	DBG_DD(ErrorF("00000288 %08x\n",col2));
	DBG_DD(ErrorF("00000284 %08x\n",col1));
	break;
    default:
	DBG_DD(ErrorF("Unknown DeviceID\n"));
	break;
    }
}


static unsigned long ViaInitVideoStatusFlag(VIAPtr pVia) 
{
    switch ( pVia->ChipId ) {
    case PCI_CHIP_VT3205:
	return VIDEO_HQV_INUSE | SW_USE_HQV | VIDEO_3_INUSE;
    case PCI_CHIP_CLE3122:
	return VIDEO_HQV_INUSE | SW_USE_HQV | VIDEO_1_INUSE;
    default:
	DBG_DD(ErrorF("Unknown DeviceID\n"));
	break;
    }
    return 0UL;
}

static unsigned long ViaSetVidCtl(VIAPtr pVia) 
{
    switch ( pVia->ChipId ) {
    case PCI_CHIP_VT3205:
	return(V3_ENABLE|V3_EXPIRE_NUM_3205);
    case PCI_CHIP_CLE3122:
      if (CLE266_REV_IS_CX(pVia->ChipRev)) 
	  return (V3_ENABLE|V3_EXPIRE_NUM_F);
      else 
	  return (V3_ENABLE|V3_EXPIRE_NUM);
      break; 
    default:
        DBG_DD(ErrorF("Unknown DeviceID\n"));
        break;
    }
    return 0;
}/*End of DeviceID*/

static void ViaVideoRegWrite(VIAPtr pVia) 
{
    switch ( pVia-> ChipId ) {
    case PCI_CHIP_VT3205:
	viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, ALPHA_V3_FIFO_CONTROL,
			   (VIDInD(ALPHA_V3_FIFO_CONTROL)&ALPHA_FIFO_MASK)| V3_FIFO_DEPTH32 | 
			   V3_FIFO_THRESHOLD29);
	viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, ALPHA_V3_PREFIFO_CONTROL ,
			   V3_FIFO_PRETHRESHOLD29 |
			   ( VIDInD(ALPHA_V3_PREFIFO_CONTROL)& (~V3_FIFO_MASK)) );
	break;
    case PCI_CHIP_CLE3122:
        if (CLE266_REV_IS_CX(pVia->ChipRev)) {
	  viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, ALPHA_V3_FIFO_CONTROL,
			     (VIDInD(ALPHA_V3_FIFO_CONTROL)&ALPHA_FIFO_MASK)|V3_FIFO_DEPTH64 | 
			     V3_FIFO_THRESHOLD56);                            
	  viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, ALPHA_V3_PREFIFO_CONTROL ,
			     V3_FIFO_PRETHRESHOLD56 |
			     ( VIDInD(ALPHA_V3_PREFIFO_CONTROL)& (~V3_FIFO_MASK)) );
	} else {
	    viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, ALPHA_V3_FIFO_CONTROL,
			       (VIDInD(ALPHA_V3_FIFO_CONTROL)&ALPHA_FIFO_MASK)|V3_FIFO_DEPTH16 | 
			       V3_FIFO_THRESHOLD8);                            
	    viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, ALPHA_V3_PREFIFO_CONTROL ,
			       (V3_FIFO_THRESHOLD16>>8) |
			       ( VIDInD(ALPHA_V3_PREFIFO_CONTROL)& (~V3_FIFO_MASK)) );
	}
	break;
    default:
	break;
    }
}

/*
 * Fill the buffer with 0x8000 (YUV2 black)
 */
static void
ViaYUVFillBlack(VIAPtr pVia, int offset, int num)
{
    CARD16 *ptr = (CARD16 *)(pVia->FBBase + offset);

    while(num-- > 0)
#if X_BYTE_ORDER == X_LITTLE_ENDIAN
	*ptr++ = 0x0080;
#else
        *ptr++ = 0x8000;
#endif
}

/*************************************************************************
   Function : VIAVidCreateSurface
   Create overlay surface depend on FOURCC
*************************************************************************/
unsigned long VIAVidCreateSurface(ScrnInfoPtr pScrn, LPDDSURFACEDESC lpDDSurfaceDesc)
{
    VIAPtr  pVia = VIAPTR(pScrn);
    unsigned long   dwWidth, dwHeight, dwPitch=0;
    unsigned long   size;
    unsigned long   dwAddr;
    unsigned long   HQVFBSIZE = 0, SWFBSIZE = 0;
    VIAHWDiff *hwDiff = &pVia->HWDiff;
    unsigned long retCode;
    
    DBG_DD(ErrorF("//VIAVidCreateSurface: \n"));

    if ( lpDDSurfaceDesc == NULL )
        return BadAccess;
        
    DBG_DD(ErrorF("Creating 0x%08X surface\n", lpDDSurfaceDesc->dwFourCC));

    switch (lpDDSurfaceDesc->dwFourCC)
    {
       case FOURCC_YUY2 :
            pVia->swov.DPFsrc.dwFlags = DDPF_FOURCC;
            pVia->swov.DPFsrc.dwFourCC = FOURCC_YUY2;

            /* init Video status flag*/
            pVia->swov.gdwVideoFlagSW = ViaInitVideoStatusFlag(pVia);

            /*write Color Space Conversion param.*/
	    /*            ViaSetColorspace(pVia);*/

            dwWidth  = lpDDSurfaceDesc->dwWidth;
            dwHeight = lpDDSurfaceDesc->dwHeight;
            dwPitch  = ALIGN_TO_32_BYTES(dwWidth)*2;
            DBG_DD(ErrorF("    srcWidth= %ld \n", dwWidth));
            DBG_DD(ErrorF("    srcHeight= %ld \n", dwHeight));

            SWFBSIZE = dwPitch*dwHeight;    /*YUYV*/

	    VIAFreeLinear(&pVia->swov.SWOVMem);            
            if(Success != (retCode = VIAAllocLinear(&pVia->swov.SWOVMem, pScrn, SWFBSIZE * 2)))
            	return retCode;
            
            dwAddr = pVia->swov.SWOVMem.base;

	    ViaYUVFillBlack(pVia, dwAddr, SWFBSIZE);

            pVia->swov.SWDevice.dwSWPhysicalAddr[0]   = dwAddr;
            pVia->swov.SWDevice.lpSWOverlaySurface[0] = pVia->FBBase+dwAddr;

            pVia->swov.SWDevice.dwSWPhysicalAddr[1] = pVia->swov.SWDevice.dwSWPhysicalAddr[0] + SWFBSIZE;
            pVia->swov.SWDevice.lpSWOverlaySurface[1] = pVia->swov.SWDevice.lpSWOverlaySurface[0] + SWFBSIZE;

            DBG_DD(ErrorF("pVia->swov.SWDevice.dwSWPhysicalAddr[0] %08lx\n", dwAddr));
            DBG_DD(ErrorF("pVia->swov.SWDevice.dwSWPhysicalAddr[1] %08lx\n", dwAddr + SWFBSIZE));

            pVia->swov.SWDevice.gdwSWSrcWidth = dwWidth;
            pVia->swov.SWDevice.gdwSWSrcHeight= dwHeight;
            pVia->swov.SWDevice.dwPitch = dwPitch;

            /* Fill image data in overlay record*/
            pVia->swov.overlayRecordV1.dwV1OriWidth  = dwWidth;
            pVia->swov.overlayRecordV1.dwV1OriHeight = dwHeight;
            pVia->swov.overlayRecordV1.dwV1OriPitch  = dwPitch;
	    if (!(pVia->swov.gdwVideoFlagSW & SW_USE_HQV))
            	break;

        case FOURCC_HQVSW :
            DBG_DD(ErrorF("//Create HQV_SW Surface\n"));
            dwWidth  = pVia->swov.SWDevice.gdwSWSrcWidth;
            dwHeight = pVia->swov.SWDevice.gdwSWSrcHeight;
            dwPitch  = pVia->swov.SWDevice.dwPitch; 

            HQVFBSIZE = dwPitch * dwHeight;
            
            if(hwDiff->dwThreeHQVBuffer)    /* CLE_C0 */
                size = HQVFBSIZE*3;
            else
                size = HQVFBSIZE*2;
            
	    VIAFreeLinear(&pVia->swov.HQVMem);                
            if(Success != (retCode = VIAAllocLinear(&pVia->swov.HQVMem, pScrn, size)))
            	return retCode;
            dwAddr = pVia->swov.HQVMem.base;
/*            dwAddr = pVia->swov.SWOVlinear->offset * depth + SWOVFBSIZE; */

            pVia->swov.overlayRecordV1.dwHQVAddr[0] = dwAddr;
            pVia->swov.overlayRecordV1.dwHQVAddr[1] = dwAddr + HQVFBSIZE;

            if ( hwDiff->dwThreeHQVBuffer )    /*CLE_C0*/
                pVia->swov.overlayRecordV1.dwHQVAddr[2] = dwAddr + 2 * HQVFBSIZE;
                
	    ViaYUVFillBlack(pVia, dwAddr, HQVFBSIZE);
            
            VIDOutD(HQV_DST_STARTADDR1,pVia->swov.overlayRecordV1.dwHQVAddr[1]);
            VIDOutD(HQV_DST_STARTADDR0,pVia->swov.overlayRecordV1.dwHQVAddr[0]);

            DBG_DD(ErrorF("000003F0 %08lx\n",VIDInD(HQV_DST_STARTADDR1) ));
            DBG_DD(ErrorF("000003EC %08lx\n",VIDInD(HQV_DST_STARTADDR0) ));
           
            if ( hwDiff->dwThreeHQVBuffer )    /*CLE_C0*/
            {
                VIDOutD(HQV_DST_STARTADDR2,pVia->swov.overlayRecordV1.dwHQVAddr[2]);
                DBG_DD(ErrorF("000003FC %08lx\n", (unsigned long)VIDInD(HQV_DST_STARTADDR2)) );
            }
           
            break;

    case FOURCC_YV12 :
    case FOURCC_VIA:
            DBG_DD(ErrorF("    Create SW YV12 Surface: \n"));
            pVia->swov.DPFsrc.dwFlags = DDPF_FOURCC;
            pVia->swov.DPFsrc.dwFourCC = lpDDSurfaceDesc->dwFourCC;


            /* init Video status flag */
            pVia->swov.gdwVideoFlagSW = ViaInitVideoStatusFlag(pVia);

            /* pVia->swov.gdwVideoFlagSW = VIDEO_1_INUSE; */

            /* if (pVia->swov.gdwVideoFlagTV1 & VIDEO_HQV_INUSE) */
/*
            if (gdwVideoFlagTV0 & VIDEO_HQV_INUSE)
            {
                lpNewVidCtrl->dwHighQVDO = VW_HIGHQVDO_OFF;
                VIADriverProc( HQVCONTROL , lpNewVidCtrl );
            }
*/
            /* write Color Space Conversion param. */
	    /*ViaSetColorspace(pVia);*/

            dwWidth  = lpDDSurfaceDesc->dwWidth;
            dwHeight = lpDDSurfaceDesc->dwHeight;
            dwPitch  = ALIGN_TO_32_BYTES(dwWidth);
            DBG_DD(ErrorF("    srcWidth= %ld \n", dwWidth));
            DBG_DD(ErrorF("    srcHeight= %ld \n", dwHeight));

            SWFBSIZE = dwPitch * dwHeight * 1.5;    /* 1.5 bytes per pixel */

	    VIAFreeLinear(&pVia->swov.SWfbMem);                
            if (FOURCC_VIA != lpDDSurfaceDesc->dwFourCC) {
	    if(Success != (retCode = VIAAllocLinear(&pVia->swov.SWfbMem, pScrn, 2 * SWFBSIZE)))
	    	return retCode;
	    
	    dwAddr = pVia->swov.SWfbMem.base;
	    
	    DEBUG(ErrorF("dwAddr for SWfbMem is %lu\n", dwAddr));

	    ViaYUVFillBlack(pVia, dwAddr, SWFBSIZE);

            pVia->swov.SWDevice.dwSWPhysicalAddr[0]   = dwAddr;
            pVia->swov.SWDevice.dwSWCrPhysicalAddr[0] = pVia->swov.SWDevice.dwSWPhysicalAddr[0]
                                             + (dwPitch*dwHeight);
            pVia->swov.SWDevice.dwSWCbPhysicalAddr[0] = pVia->swov.SWDevice.dwSWCrPhysicalAddr[0]
                                             + ((dwPitch>>1)*(dwHeight>>1));
            pVia->swov.SWDevice.lpSWOverlaySurface[0] = pVia->FBBase+dwAddr;

            pVia->swov.SWDevice.dwSWPhysicalAddr[1] = dwAddr + SWFBSIZE;
            pVia->swov.SWDevice.dwSWCrPhysicalAddr[1] = pVia->swov.SWDevice.dwSWPhysicalAddr[1]
                                             + (dwPitch*dwHeight);
            pVia->swov.SWDevice.dwSWCbPhysicalAddr[1] = pVia->swov.SWDevice.dwSWCrPhysicalAddr[1]
                                             + ((dwPitch>>1)*(dwHeight>>1));
            pVia->swov.SWDevice.lpSWOverlaySurface[1] = pVia->swov.SWDevice.lpSWOverlaySurface[0] + SWFBSIZE;

            DBG_DD(ErrorF("pVia->swov.SWDevice.dwSWPhysicalAddr[0] %08lx\n", dwAddr));
            DBG_DD(ErrorF("pVia->swov.SWDevice.dwSWPhysicalAddr[1] %08lx\n", dwAddr + SWFBSIZE));
	    }
            pVia->swov.SWDevice.gdwSWSrcWidth = dwWidth;
            pVia->swov.SWDevice.gdwSWSrcHeight= dwHeight;
            pVia->swov.SWDevice.dwPitch = dwPitch;

            /* Fill image data in overlay record */
            pVia->swov.overlayRecordV1.dwV1OriWidth  = dwWidth;
            pVia->swov.overlayRecordV1.dwV1OriHeight = dwHeight;
            pVia->swov.overlayRecordV1.dwV1OriPitch  = dwPitch;
/*
if (!(pVia->swov.gdwVideoFlagSW & SW_USE_HQV))
            break;
        case FOURCC_HQVSW :
*/
            /* 
             *  if sw video use HQV dwpitch should changed
             */
            DBG_DD(ErrorF("//Create HQV_SW Surface\n"));
/*          pVia->swov.DPFsrc.dwFourCC = FOURCC_YUY2; */
            dwWidth  = pVia->swov.SWDevice.gdwSWSrcWidth;
            dwHeight = pVia->swov.SWDevice.gdwSWSrcHeight;
            dwPitch  = pVia->swov.SWDevice.dwPitch; 

            HQVFBSIZE = dwPitch * dwHeight * 2;

            if ( hwDiff->dwThreeHQVBuffer )    /* CLE_C0 */
                size = HQVFBSIZE * 3;
            else
                size = HQVFBSIZE * 2;

	    VIAFreeLinear(&pVia->swov.HQVMem);                
	    if(Success != (retCode = VIAAllocLinear(&pVia->swov.HQVMem, pScrn, size)))
	    	return retCode;
            
            dwAddr = pVia->swov.HQVMem.base;
	    DEBUG(ErrorF("dwAddr for HQV is %lu\n", dwAddr));
            DBG_DD(ErrorF("HQV dwAddr = 0x%x!!!! \n",dwAddr));

            pVia->swov.overlayRecordV1.dwHQVAddr[0] = dwAddr;
            pVia->swov.overlayRecordV1.dwHQVAddr[1] = dwAddr + HQVFBSIZE;

            if ( hwDiff->dwThreeHQVBuffer )    /*CLE_C0*/
                pVia->swov.overlayRecordV1.dwHQVAddr[2] = dwAddr + 2 * HQVFBSIZE;

            /* FIXME: Check if should fill 3 on C0 */
	    ViaYUVFillBlack(pVia, dwAddr, HQVFBSIZE);
            
            VIDOutD(HQV_DST_STARTADDR1,pVia->swov.overlayRecordV1.dwHQVAddr[1]);
            VIDOutD(HQV_DST_STARTADDR0,pVia->swov.overlayRecordV1.dwHQVAddr[0]);

            DBG_DD(ErrorF("000003F0 %08lx\n",VIDInD(HQV_DST_STARTADDR1) ));
            DBG_DD(ErrorF("000003EC %08lx\n",VIDInD(HQV_DST_STARTADDR0) ));
             
            if ( hwDiff->dwThreeHQVBuffer )    /*CLE_C0*/
            {
                VIDOutD(HQV_DST_STARTADDR2,pVia->swov.overlayRecordV1.dwHQVAddr[2]);
                DBG_DD(ErrorF("000003FC %08lx\n", (unsigned long)VIDInD(HQV_DST_STARTADDR2)) );
            }
             
            break;

        default :
            break;
    }

    return Success;

} /*VIAVidCreateSurface*/

/*************************************************************************
   Function : VIAVidLockSurface
   Lock Surface
*************************************************************************/
unsigned long VIAVidLockSurface(ScrnInfoPtr pScrn, LPDDLOCK lpLock)
{
    VIAPtr  pVia = VIAPTR(pScrn);

    switch (lpLock->dwFourCC)
    {
       case FOURCC_YUY2 :
       case FOURCC_YV12 :
    case FOURCC_VIA :
            lpLock->SWDevice = pVia->swov.SWDevice ;
            lpLock->dwPhysicalBase = pVia->FrameBufferBase;

    }
         
    return PI_OK;

} /*VIAVidLockSurface*/

/*************************************************************************
 *  Destroy Surface
*************************************************************************/
unsigned long VIAVidDestroySurface(ScrnInfoPtr pScrn,  LPDDSURFACEDESC lpDDSurfaceDesc)
{
    VIAPtr pVia = VIAPTR(pScrn);

    DBG_DD(ErrorF("//VIAVidDestroySurface: \n"));

    switch (lpDDSurfaceDesc->dwFourCC)
    {
       case FOURCC_YUY2 :
            pVia->swov.DPFsrc.dwFlags = 0;
            pVia->swov.DPFsrc.dwFourCC = 0;
            
            VIAFreeLinear(&pVia->swov.SWOVMem);
            if (!(pVia->swov.gdwVideoFlagSW & SW_USE_HQV))
            {
                pVia->swov.gdwVideoFlagSW = 0;
                break;
            }

       case FOURCC_HQVSW :
       	    VIAFreeLinear(&pVia->swov.HQVMem);
            pVia->swov.gdwVideoFlagSW = 0;
/*            if (pVia->swov.gdwVideoFlagTV1 != 0)
            {
                DBG_DD(ErrorF(" Assign HQV to TV1 \n"));
                lpNewVidCtrl->dwHighQVDO = VW_HIGHQVDO_TV1;
                DriverProc( HQVCONTROL , lpNewVidCtrl );
            }
*/
            break;

       case FOURCC_YV12 :
    case FOURCC_VIA:
            pVia->swov.DPFsrc.dwFlags = 0;
            pVia->swov.DPFsrc.dwFourCC = 0;
                    
            VIAFreeLinear(&pVia->swov.SWfbMem);
            VIAFreeLinear(&pVia->swov.HQVMem);
            pVia->swov.gdwVideoFlagSW = 0;
            break;
    }
    DBG_DD(ErrorF("\n//VIAVidDestroySurface : OK!!\n"));
    return PI_OK;

} /*VIAVidDestroySurface*/


/****************************************************************************
 *
 * Upd_Video()
 *
 ***************************************************************************/
static unsigned long Upd_Video(ScrnInfoPtr pScrn, unsigned long dwVideoFlag,unsigned long dwStartAddr,RECTL rSrc,RECTL rDest,unsigned long dwSrcPitch,
                 unsigned long dwOriSrcWidth,unsigned long dwOriSrcHeight,LPDDPIXELFORMAT lpDPFsrc,
                 unsigned long dwDeinterlaceMode,unsigned long dwColorKey,unsigned long dwChromaKey,
                 unsigned long dwKeyLow,unsigned long dwKeyHigh,unsigned long dwChromaLow,unsigned long dwChromaHigh, unsigned long dwFlags)
{
    VIAPtr  pVia = VIAPTR(pScrn);
    vgaHWPtr hwp = VGAHWPTR(pScrn);
    unsigned long dwVidCtl=0, dwCompose=(VIDInD(V_COMPOSE_MODE)&~(SELECT_VIDEO_IF_COLOR_KEY|V1_COMMAND_FIRE|V3_COMMAND_FIRE))|V_COMMAND_LOAD_VBI;
    unsigned long srcWidth, srcHeight,dstWidth,dstHeight;
    unsigned long zoomCtl=0, miniCtl=0;
    unsigned long dwHQVCtl=0;
    unsigned long dwHQVfilterCtl=0,dwHQVminiCtl=0;
    unsigned long dwHQVzoomflagH=0,dwHQVzoomflagV=0;
    unsigned long dwHQVsrcWidth=0,dwHQVdstWidth=0;
    unsigned long dwHQVsrcFetch = 0,dwHQVoffset=0;
    unsigned long dwOffset=0,dwFetch=0,dwTmp=0;
    unsigned long dwDisplayCountW=0;
    VIAHWDiff *hwDiff = &pVia->HWDiff;

    DBG_DD(ErrorF("// Upd_Video:\n"));
    DBG_DD(ErrorF("Modified rSrc  X (%ld,%ld) Y (%ld,%ld)\n",
                rSrc.left, rSrc.right,rSrc.top, rSrc.bottom));
    DBG_DD(ErrorF("Modified rDest  X (%ld,%ld) Y (%ld,%ld)\n",
                rDest.left, rDest.right,rDest.top, rDest.bottom));

    if (dwVideoFlag & VIDEO_SHOW)    
    {        
        pVia->swov.overlayRecordV1.dwWidth=dstWidth = rDest.right - rDest.left;
        pVia->swov.overlayRecordV1.dwHeight=dstHeight = rDest.bottom - rDest.top;
        srcWidth = (unsigned long)rSrc.right - rSrc.left;
        srcHeight = (unsigned long)rSrc.bottom - rSrc.top;
        DBG_DD(ErrorF("===srcWidth= %ld \n", srcWidth));
        DBG_DD(ErrorF("===srcHeight= %ld \n", srcHeight));

        if (dwVideoFlag & VIDEO_1_INUSE)
        {
            /*=* Modify for C1 FIFO *=*/
	     /* WARNING: not checking Chipset! */
	    if (CLE266_REV_IS_CX(pVia->ChipRev))
                    dwVidCtl = (V1_ENABLE|V1_EXPIRE_NUM_F);
	    else {
                    /* Overlay source format for V1*/
                    if (pVia->swov.gdwUseExtendedFIFO)
                        dwVidCtl = (V1_ENABLE|V1_EXPIRE_NUM_A|V1_FIFO_EXTENDED);
                    else
                        dwVidCtl = (V1_ENABLE|V1_EXPIRE_NUM);
            }
            
            viaOverlayGetV1Format(pVia, dwVideoFlag,lpDPFsrc,&dwVidCtl,&dwHQVCtl);
            viaOverlayGetV1Format(pVia, dwVideoFlag,lpDPFsrc,&dwVidCtl,&dwHQVCtl);
        }
        else
        {
	  dwVidCtl = ViaSetVidCtl(pVia);
	  viaOverlayGetV3Format(pVia, dwVideoFlag,lpDPFsrc,&dwVidCtl,&dwHQVCtl);            
        }

        if ( hwDiff->dwThreeHQVBuffer )    /*CLE_C0*/
        {
            /* HQV support 3 HQV buffer */
            dwHQVCtl &= ~HQV_SW_FLIP;
            dwHQVCtl |= HQV_TRIPLE_BUFF | HQV_FLIP_STATUS; 
        }

        /* Starting address of source and Source offset*/
        dwOffset = viaOverlayGetSrcStartAddress (pVia, dwVideoFlag,rSrc,rDest,dwSrcPitch,lpDPFsrc,&dwHQVoffset );
        DBG_DD(ErrorF("===dwOffset= 0x%lx \n", dwOffset));

        pVia->swov.overlayRecordV1.dwOffset = dwOffset;

        if (pVia->swov.DPFsrc.dwFourCC == FOURCC_YV12 
	    || pVia->swov.DPFsrc.dwFourCC == FOURCC_VIA)
        {
            YCBCRREC YCbCr;
            if (dwVideoFlag & VIDEO_HQV_INUSE)    
            {
                dwHQVsrcWidth=(unsigned long)rSrc.right - rSrc.left;
                dwHQVdstWidth=(unsigned long)rDest.right - rDest.left;
                if (dwHQVsrcWidth>dwHQVdstWidth)
                {
                    dwOffset = dwOffset * dwHQVdstWidth / dwHQVsrcWidth;
                }
                
                if (dwVideoFlag & VIDEO_1_INUSE)
                {
                    viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, V1_STARTADDR_0, pVia->swov.overlayRecordV1.dwHQVAddr[0]+dwOffset);
                    viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, V1_STARTADDR_1, pVia->swov.overlayRecordV1.dwHQVAddr[1]+dwOffset);
                    
                    if ( hwDiff->dwThreeHQVBuffer )    /*CLE_C0*/
                        viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, V1_STARTADDR_2, pVia->swov.overlayRecordV1.dwHQVAddr[2]+dwOffset);
                }
                else
                {
                    viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, V3_STARTADDR_0, pVia->swov.overlayRecordV1.dwHQVAddr[0]+dwOffset);                    
                    viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, V3_STARTADDR_1, pVia->swov.overlayRecordV1.dwHQVAddr[1]+dwOffset);
                    
                    if ( hwDiff->dwThreeHQVBuffer )    /*CLE_C0*/
                        viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, V3_STARTADDR_2, pVia->swov.overlayRecordV1.dwHQVAddr[2]+dwOffset); 
                }
                YCbCr = viaOverlayGetYCbCrStartAddress(dwVideoFlag,dwStartAddr,pVia->swov.overlayRecordV1.dwOffset,pVia->swov.overlayRecordV1.dwUVoffset,dwSrcPitch,dwOriSrcHeight);
                viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, HQV_SRC_STARTADDR_Y, YCbCr.dwY);
                viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, HQV_SRC_STARTADDR_U, YCbCr.dwCR);
                viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, HQV_SRC_STARTADDR_V, YCbCr.dwCB);
            }
            else
            {
                YCbCr = viaOverlayGetYCbCrStartAddress(dwVideoFlag,dwStartAddr,pVia->swov.overlayRecordV1.dwOffset,pVia->swov.overlayRecordV1.dwUVoffset,dwSrcPitch,dwOriSrcHeight);
                if (dwVideoFlag & VIDEO_1_INUSE)
                {
                    viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, V1_STARTADDR_0, YCbCr.dwY);
                    viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, V1_STARTADDR_CB0, YCbCr.dwCR);
                    viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, V1_STARTADDR_CR0, YCbCr.dwCB);
                }
                else
                {
                    DBG_DD(ErrorF("Upd_Video() : We do not support YV12 with V3!\n"));
                }
            }
        }
        else
        {
            if (dwVideoFlag & VIDEO_HQV_INUSE)    
            {
                dwHQVsrcWidth=(unsigned long)rSrc.right - rSrc.left;
                dwHQVdstWidth=(unsigned long)rDest.right - rDest.left;
                if (dwHQVsrcWidth>dwHQVdstWidth)
                {
                    dwOffset = dwOffset * dwHQVdstWidth / dwHQVsrcWidth;
                }
                
                if (dwVideoFlag & VIDEO_1_INUSE)
                {
                    viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, V1_STARTADDR_0, pVia->swov.overlayRecordV1.dwHQVAddr[0]+dwHQVoffset);
                    viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, V1_STARTADDR_1, pVia->swov.overlayRecordV1.dwHQVAddr[1]+dwHQVoffset);
                    
                    if ( hwDiff->dwThreeHQVBuffer )    /*CLE_C0*/
                        viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, V1_STARTADDR_2, pVia->swov.overlayRecordV1.dwHQVAddr[2]+dwHQVoffset);
                }
                else
                {
                    viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, V3_STARTADDR_0, pVia->swov.overlayRecordV1.dwHQVAddr[0]+dwHQVoffset);
                    viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, V3_STARTADDR_1, pVia->swov.overlayRecordV1.dwHQVAddr[1]+dwHQVoffset);                    
                    
                    if ( hwDiff->dwThreeHQVBuffer )    /*CLE_C0*/
                        viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, V3_STARTADDR_2, pVia->swov.overlayRecordV1.dwHQVAddr[2]+dwHQVoffset);
                }
                viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, HQV_SRC_STARTADDR_Y, dwStartAddr);
            }
            else
            {
                dwStartAddr += dwOffset;

                if (dwVideoFlag & VIDEO_1_INUSE)
                {
                    viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, V1_STARTADDR_0, dwStartAddr);
                }
                else
                {
                    viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, V3_STARTADDR_0, dwStartAddr);                    
                }
            }
        }

        dwFetch = viaOverlayGetFetch(dwVideoFlag,lpDPFsrc,srcWidth,dstWidth,dwOriSrcWidth,&dwHQVsrcFetch);
        DBG_DD(ErrorF("===dwFetch= 0x%lx \n", dwFetch));
/*
        //For DCT450 test-BOB INTERLEAVE
        if ( (dwDeinterlaceMode & DDOVER_INTERLEAVED) && (dwDeinterlaceMode & DDOVER_BOB ) )
        {
            if (dwVideoFlag & VIDEO_HQV_INUSE)    
            {
                dwHQVCtl |= HQV_FIELD_2_FRAME|HQV_FRAME_2_FIELD|HQV_DEINTERLACE;                   
            }
            else
            {
                dwVidCtl |= (V1_BOB_ENABLE | V1_FRAME_BASE);
            }
        }
        else if (dwDeinterlaceMode & DDOVER_BOB )
        {
            if (dwVideoFlag & VIDEO_HQV_INUSE)
            {
                //The HQV source data line count should be two times of the original line count
                dwHQVCtl |= HQV_FIELD_2_FRAME|HQV_DEINTERLACE;
            }
            else
            {
                dwVidCtl |= V1_BOB_ENABLE;                
            }
        }
*/
        if (dwVideoFlag & VIDEO_HQV_INUSE)
        {
            if ( !(dwDeinterlaceMode & DDOVER_INTERLEAVED) && (dwDeinterlaceMode & DDOVER_BOB ) )
            {
                if ( hwDiff->dwHQVFetchByteUnit )     /* CLE_C0 */
                    viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, HQV_SRC_FETCH_LINE, (((dwHQVsrcFetch)-1)<<16)|((dwOriSrcHeight<<1)-1)); 
                else
                    viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, HQV_SRC_FETCH_LINE, (((dwHQVsrcFetch>>3)-1)<<16)|((dwOriSrcHeight<<1)-1));
            }
            else
            {
                if ( hwDiff->dwHQVFetchByteUnit )     /* CLE_C0 */
                    viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, HQV_SRC_FETCH_LINE, (((dwHQVsrcFetch)-1)<<16)|(dwOriSrcHeight-1));
                else
                viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, HQV_SRC_FETCH_LINE, (((dwHQVsrcFetch>>3)-1)<<16)|(dwOriSrcHeight-1));
            }
            if (pVia->swov.DPFsrc.dwFourCC == FOURCC_YV12 
		|| pVia->swov.DPFsrc.dwFourCC == FOURCC_VIA)
            {
                if (dwVideoFlag & VIDEO_1_INUSE)
                {
                    viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, V1_STRIDE, dwSrcPitch<<1);
                }
                else
                {                  
                    viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, V3_STRIDE, dwSrcPitch<<1);
                }
                viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, HQV_SRC_STRIDE, ((dwSrcPitch>>1)<<16)|dwSrcPitch);
                viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, HQV_DST_STRIDE, (dwSrcPitch<<1));
            }
            else
            {
                if (dwVideoFlag & VIDEO_1_INUSE)
                {
                    viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, V1_STRIDE, dwSrcPitch);
                }
                else
                {                  
                    viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, V3_STRIDE, dwSrcPitch);
                }
                viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, HQV_SRC_STRIDE, dwSrcPitch);
                viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, HQV_DST_STRIDE, dwSrcPitch);
            }
                
        }
        else
        {
            if (dwVideoFlag & VIDEO_1_INUSE)
            {
/*                viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, V1_STRIDE, dwSrcPitch | (dwSrcPitch <<15) );*/
                viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, V1_STRIDE, dwSrcPitch );
            }
            else
            {
                viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, V3_STRIDE, dwSrcPitch | (dwSrcPitch <<15) );                
            }
        }

        DBG_DD(ErrorF("rSrc  X (%ld,%ld) Y (%ld,%ld)\n",
                rSrc.left, rSrc.right,rSrc.top, rSrc.bottom));
        DBG_DD(ErrorF("rDest  X (%ld,%ld) Y (%ld,%ld)\n",
                rDest.left, rDest.right,rDest.top, rDest.bottom));

        /* Destination window key*/

        if (dwVideoFlag & VIDEO_1_INUSE)    
        {
            /*modify for HW DVI limitation,
            //When we enable the CRT and DVI both, then change resolution.
            //If the resolution small than the panel physical size, the video display in Y direction will be cut.
            //So, we need to adjust the Y top and bottom position.                                   */
            if  (pVia->pBIOSInfo->SetDVI && pVia->pBIOSInfo->scaleY)
            {
                 viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER,V1_WIN_END_Y,
                                ((rDest.right-1)<<16) + (rDest.bottom * pVia->pBIOSInfo->panelY / pScrn->currentMode->VDisplay));
                 if (rDest.top > 0)
                 {
                      viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, V1_WIN_START_Y,
                                     (rDest.left<<16) + (rDest.top * pVia->pBIOSInfo->panelY / pScrn->currentMode->VDisplay));
                 }
                 else
                 {
                      viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, V1_WIN_START_Y,(rDest.left<<16));
                 }
            }
            else
            {
                 viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER,V1_WIN_END_Y, ((rDest.right-1)<<16) + (rDest.bottom-1));
                 if (rDest.top > 0)
                 {
                     viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, V1_WIN_START_Y, (rDest.left<<16) + rDest.top );
                 }
                 else 
                 {
                     viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, V1_WIN_START_Y, (rDest.left<<16));
                 }
	        }
        }
        else
        {
            viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER,V3_WIN_END_Y, ((rDest.right-1)<<16) + (rDest.bottom-1));
            if (rDest.top > 0)
            {
                viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, V3_WIN_START_Y, (rDest.left<<16) + rDest.top );
            }
            else 
            {
                viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, V3_WIN_START_Y, (rDest.left<<16));
            }
        }

        dwCompose |= ALWAYS_SELECT_VIDEO;


        /* Setup X zoom factor*/
        pVia->swov.overlayRecordV1.dwFetchAlignment = 0;

        if ( viaOverlayHQVCalcZoomWidth(pVia, dwVideoFlag, srcWidth , dstWidth,
                                  &zoomCtl, &miniCtl, &dwHQVfilterCtl, &dwHQVminiCtl,&dwHQVzoomflagH) == PI_ERR )
        {
            /* too small to handle*/
            dwFetch <<= 20;
            if (dwVideoFlag & VIDEO_1_INUSE)    
            {
                viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, V12_QWORD_PER_LINE, dwFetch);
                viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, V_COMPOSE_MODE , dwCompose|V1_COMMAND_FIRE );
            }
            else
            {
                dwFetch |=(VIDInD(V3_ALPHA_QWORD_PER_LINE)&(~V3_FETCH_COUNT));
                viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, V3_ALPHA_QWORD_PER_LINE, dwFetch);
                viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, V_COMPOSE_MODE , dwCompose|V3_COMMAND_FIRE );
                
            }
            viaMacro_VidREGFlush(pVia);
            return PI_ERR;
        }

        dwFetch <<= 20;
        if (dwVideoFlag & VIDEO_1_INUSE)    
        {
            viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, V12_QWORD_PER_LINE, dwFetch);
        }
        else
        {
            dwFetch |=(VIDInD(V3_ALPHA_QWORD_PER_LINE)&(~V3_FETCH_COUNT));
            viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, V3_ALPHA_QWORD_PER_LINE, dwFetch);
        }

        /*
        // Setup Y zoom factor
        */

        /*For DCT450 test-BOB INTERLEAVE*/
        if ( (dwDeinterlaceMode & DDOVER_INTERLEAVED) && (dwDeinterlaceMode & DDOVER_BOB))
        {
            if (!(dwVideoFlag & VIDEO_HQV_INUSE))
            {
                srcHeight /=2;
                if (dwVideoFlag & VIDEO_1_INUSE)    
                {
                    dwVidCtl |= (V1_BOB_ENABLE | V1_FRAME_BASE);
                }
                else
                {
                    dwVidCtl |= (V3_BOB_ENABLE | V3_FRAME_BASE);                    
                }
            }
            else
            {
                dwHQVCtl |= HQV_FIELD_2_FRAME|HQV_FRAME_2_FIELD|HQV_DEINTERLACE;                 
            }
        }
        else if (dwDeinterlaceMode & DDOVER_BOB )
        {
            if (dwVideoFlag & VIDEO_HQV_INUSE)
            {
                srcHeight <<=1;
                dwHQVCtl |= HQV_FIELD_2_FRAME|HQV_DEINTERLACE;
            }
            else
            {
                if (dwVideoFlag & VIDEO_1_INUSE)    
                {
                    dwVidCtl |= V1_BOB_ENABLE;                
                }
                else
                {
                    dwVidCtl |= V3_BOB_ENABLE;                    
                }
            }
        }

        viaOverlayGetDisplayCount(pVia, dwVideoFlag,lpDPFsrc,srcWidth,&dwDisplayCountW);
        
        if (dwVideoFlag & VIDEO_1_INUSE) 
        {
            viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, V1_SOURCE_HEIGHT, (srcHeight<<16)|dwDisplayCountW);
        }
        else
        {
            viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER,V3_SOURCE_WIDTH, dwDisplayCountW);
        }
        
        if ( viaOverlayHQVCalcZoomHeight(pVia, srcHeight,dstHeight,&zoomCtl,&miniCtl, &dwHQVfilterCtl, &dwHQVminiCtl ,&dwHQVzoomflagV) == PI_ERR )
        {
            if (dwVideoFlag & VIDEO_1_INUSE)    
            {
                viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, V_COMPOSE_MODE , dwCompose|V1_COMMAND_FIRE );
            }
            else
            {
                viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, V_COMPOSE_MODE , dwCompose|V3_COMMAND_FIRE );                
            }

            viaMacro_VidREGFlush(pVia);
            return PI_ERR;
        }

        if (miniCtl & V1_Y_INTERPOLY)
        {
            if (pVia->swov.DPFsrc.dwFourCC == FOURCC_YV12 
		|| pVia->swov.DPFsrc.dwFourCC == FOURCC_VIA)
            {                
                if (dwVideoFlag & VIDEO_HQV_INUSE)
                {
                    if (dwVideoFlag & VIDEO_1_INUSE)    
                    {
                        /*=* Modify for C1 FIFO *=*/
                        /* WARNING: not checking Chipset! */
			if (CLE266_REV_IS_CX(pVia->ChipRev))
                                viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, V_FIFO_CONTROL,
                                    V1_FIFO_DEPTH64 | V1_FIFO_PRETHRESHOLD56 | V1_FIFO_THRESHOLD56);
			else
                                viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, V_FIFO_CONTROL,
                                    V1_FIFO_DEPTH32 | V1_FIFO_PRETHRESHOLD29 | V1_FIFO_THRESHOLD16);
                    }
                    else
                    {
		        ViaVideoRegWrite(pVia);
                    }
                }
                else
                {
                    /*Minified Video will be skewed if not work around*/
                    if (srcWidth <= 80) /*Fetch count <= 5*/
                    {
                        if (dwVideoFlag & VIDEO_1_INUSE)    
                        {
                            viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, V_FIFO_CONTROL,V1_FIFO_DEPTH16 );                            
                        }    
                        else
                        {
                            viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, ALPHA_V3_FIFO_CONTROL,
                                (VIDInD(ALPHA_V3_FIFO_CONTROL)&ALPHA_FIFO_MASK)|V3_FIFO_DEPTH16 );                            
                            viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, ALPHA_V3_PREFIFO_CONTROL ,(V3_FIFO_THRESHOLD16>>8) |
                                ( VIDInD(ALPHA_V3_PREFIFO_CONTROL)& (~V3_FIFO_MASK)) );
                        }
                    }
                    else
                    {
                        if (dwVideoFlag & VIDEO_1_INUSE)    
                        {
                            /*=* Modify for C1 FIFO *=*/
                            /* WARNING: not checking Chipset! */
			    if (CLE266_REV_IS_CX(pVia->ChipRev))
                                    viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, V_FIFO_CONTROL,
                                        V1_FIFO_DEPTH64 | V1_FIFO_PRETHRESHOLD56 | V1_FIFO_THRESHOLD56);
			    else
                                    viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, V_FIFO_CONTROL,
                                        V1_FIFO_DEPTH16 | V1_FIFO_PRETHRESHOLD12 | V1_FIFO_THRESHOLD8);
                        }
                        else
                        {
   			    ViaVideoRegWrite(pVia);
                        }
                    }
                }
            }
            else
            {
                if (dwVideoFlag & VIDEO_1_INUSE)    
                {
                    /*=* Modify for C1 FIFO *=*/
                    /* WARNING: not checking Chipset! */
		    if (CLE266_REV_IS_CX(pVia->ChipRev))
                            viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, V_FIFO_CONTROL,
                                V1_FIFO_DEPTH64 | V1_FIFO_PRETHRESHOLD56 | V1_FIFO_THRESHOLD56);
		    else {
                            if (pVia->swov.gdwUseExtendedFIFO)
                            {
                                viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, V_FIFO_CONTROL,
                                    V1_FIFO_DEPTH48 | V1_FIFO_PRETHRESHOLD40 | V1_FIFO_THRESHOLD40);
                            }
                            else
                            {
                                viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, V_FIFO_CONTROL,
                                    V1_FIFO_DEPTH32 | V1_FIFO_PRETHRESHOLD29 | V1_FIFO_THRESHOLD16);
                            }                        
                    }                        
                }
                else
                {
                    /*Fix V3 bug*/
                    if (srcWidth <= 8)
                    {
                        viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, ALPHA_V3_FIFO_CONTROL,
                            (VIDInD(ALPHA_V3_FIFO_CONTROL)&ALPHA_FIFO_MASK));
                        viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, ALPHA_V3_PREFIFO_CONTROL ,
                            ( VIDInD(ALPHA_V3_PREFIFO_CONTROL)& (~V3_FIFO_MASK)) );
                    }
                    else
                    {
		        ViaVideoRegWrite(pVia);
                    }
                }
            }
        }
        else 
        {
            if (pVia->swov.DPFsrc.dwFourCC == FOURCC_YV12 
		|| pVia->swov.DPFsrc.dwFourCC == FOURCC_VIA)
            {                
                if (dwVideoFlag & VIDEO_HQV_INUSE)
                {
                    if (dwVideoFlag & VIDEO_1_INUSE)    
                    {
                        /*=* Modify for C1 FIFO *=*/
                        /* WARNING: not checking Chipset! */
			if (CLE266_REV_IS_CX(pVia->ChipRev))
                                viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, V_FIFO_CONTROL,
                                    V1_FIFO_DEPTH64 | V1_FIFO_PRETHRESHOLD56 | V1_FIFO_THRESHOLD56);
			else
                                viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, V_FIFO_CONTROL,
                                    V1_FIFO_DEPTH32 | V1_FIFO_PRETHRESHOLD29 | V1_FIFO_THRESHOLD16);
                    }
                    else
                    {
  		        ViaVideoRegWrite( pVia );
                    }
                }
                else
                {
                    /*Minified Video will be skewed if not work around*/
                    if (srcWidth <= 80) /*Fetch count <= 5*/
                    {
                        if (dwVideoFlag & VIDEO_1_INUSE)    
                        {
                            viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, V_FIFO_CONTROL,V1_FIFO_DEPTH16 );                            
                        }    
                        else
                        {
                            viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, ALPHA_V3_FIFO_CONTROL,
                                (VIDInD(ALPHA_V3_FIFO_CONTROL)&ALPHA_FIFO_MASK)|V3_FIFO_DEPTH16 );                            
                            viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, ALPHA_V3_PREFIFO_CONTROL ,(V3_FIFO_THRESHOLD16>>8) |
                                ( VIDInD(ALPHA_V3_PREFIFO_CONTROL)& (~V3_FIFO_MASK)) );
                        }
                    }
                    else
                    {
                        if (dwVideoFlag & VIDEO_1_INUSE)    
                        {
                            /*=* Modify for C1 FIFO *=*/
                            /* WARNING: not checking Chipset! */
			    if (CLE266_REV_IS_CX(pVia->ChipRev))
                                    viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, V_FIFO_CONTROL,
                                        V1_FIFO_DEPTH64 | V1_FIFO_PRETHRESHOLD56 | V1_FIFO_THRESHOLD56);
			    else
                                    viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, V_FIFO_CONTROL,
                                        V1_FIFO_DEPTH16 | V1_FIFO_PRETHRESHOLD12 | V1_FIFO_THRESHOLD8);
                        }
                        else
                        {
			    ViaVideoRegWrite(pVia);
                        }
                    }
                }
            }
            else
            {
                if (dwVideoFlag & VIDEO_1_INUSE)    
                {
                    /*=* Modify for C1 FIFO *=*/
                    /* WARNING: not checking Chipset! */
		    if (CLE266_REV_IS_CX(pVia->ChipRev))
                            viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, V_FIFO_CONTROL,
                                V1_FIFO_DEPTH64 | V1_FIFO_PRETHRESHOLD56 | V1_FIFO_THRESHOLD56);                        
		    else {
                            if (pVia->swov.gdwUseExtendedFIFO)
                            {
                                viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, V_FIFO_CONTROL,
                                    V1_FIFO_DEPTH48 | V1_FIFO_PRETHRESHOLD40 | V1_FIFO_THRESHOLD40);                        
                            }
                            else
                            {
                                viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, V_FIFO_CONTROL,
                                    V1_FIFO_DEPTH32 | V1_FIFO_PRETHRESHOLD29 | V1_FIFO_THRESHOLD16);
                            }
                    }
                }
                else
                {
                    /*Fix V3 bug*/
                    if (srcWidth <= 8)
                    {
                        viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, ALPHA_V3_FIFO_CONTROL,
                            (VIDInD(ALPHA_V3_FIFO_CONTROL)&ALPHA_FIFO_MASK));
                        viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, ALPHA_V3_PREFIFO_CONTROL ,
                            ( VIDInD(ALPHA_V3_PREFIFO_CONTROL)& (~V3_FIFO_MASK)) );
                    }
                    else
                    {
		        ViaVideoRegWrite(pVia);
                    }
                }
            }
        }

        if (dwVideoFlag & VIDEO_HQV_INUSE)
        {
            miniCtl=0;
            if (dwHQVzoomflagH||dwHQVzoomflagV)
            {
                dwTmp = 0;
                if (dwHQVzoomflagH)
                {
                    miniCtl = V1_X_INTERPOLY;
                    dwTmp = (zoomCtl&0xffff0000);
                }
                
                if (dwHQVzoomflagV)
                {
                    miniCtl |= (V1_Y_INTERPOLY | V1_YCBCR_INTERPOLY);
                    dwTmp |= (zoomCtl&0x0000ffff);
                    dwHQVfilterCtl &= 0xfffdffff;
                }
       
                /*Temporarily fix for 2D bandwidth problem. 2002/08/01*/
                if ((pVia->swov.gdwUseExtendedFIFO))
                {
                    miniCtl &= ~V1_Y_INTERPOLY;
                }
       
                if (dwVideoFlag & VIDEO_1_INUSE)
                {
                    viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER,V1_MINI_CONTROL, miniCtl);
                    viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER,V1_ZOOM_CONTROL, dwTmp);                
                }
                else
                {
                    viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER,V3_MINI_CONTROL, miniCtl);
                    viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER,V3_ZOOM_CONTROL, dwTmp);                                    
                }
            }
            else
            {
                if (srcHeight==dstHeight)
                {
                    dwHQVfilterCtl &= 0xfffdffff;
                }
                if (dwVideoFlag & VIDEO_1_INUSE)
                {
                    viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER,V1_MINI_CONTROL, 0);
                    viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER,V1_ZOOM_CONTROL, 0);
                }
                else
                {
                    viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER,V3_MINI_CONTROL, 0);
                    viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER,V3_ZOOM_CONTROL, 0);
                }
            }
            viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER,HQV_MINIFY_CONTROL, dwHQVminiCtl);
            viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER,HQV_FILTER_CONTROL, dwHQVfilterCtl);
        }
        else
        {
            if (dwVideoFlag & VIDEO_1_INUSE)
            {
                viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER,V1_MINI_CONTROL, miniCtl);
                viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER,V1_ZOOM_CONTROL, zoomCtl);
            }
            else
            {
                viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER,V3_MINI_CONTROL, miniCtl);
                viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER,V3_ZOOM_CONTROL, zoomCtl);                                
            }
        }


        /* Colorkey*/
        if (dwColorKey) {
            DBG_DD(ErrorF("Overlay colorkey= low:%08lx high:%08lx\n", dwKeyLow, dwKeyHigh));

            dwKeyLow &= 0x00FFFFFF;
            /*viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER,V_COLOR_KEY, dwKeyLow);*/
            
            if (dwVideoFlag & VIDEO_1_INUSE)
            {
                viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER,V_COLOR_KEY, dwKeyLow);
            }
            else
            {
                if ( hwDiff->dwSupportTwoColorKey )    /*CLE_C0*/
                    viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER,V3_COLOR_KEY, dwKeyLow);
            }

            /*dwCompose = (dwCompose & ~0x0f) | SELECT_VIDEO_IF_COLOR_KEY;*/
            /*CLE_C0*/
            dwCompose = (dwCompose & ~0x0f) | SELECT_VIDEO_IF_COLOR_KEY | SELECT_VIDEO3_IF_COLOR_KEY;
            /*dwCompose = (dwCompose & ~0x0f)  ;*/
        }

        if (dwChromaKey) {
            DBG_DD(ErrorF("Overlay Chromakey= low:%08lx high:%08lx\n", dwKeyLow, dwKeyHigh));

            dwChromaLow  &= CHROMA_KEY_LOW;
            dwChromaHigh &= CHROMA_KEY_HIGH;

            dwChromaLow  |= (VIDInD(V_CHROMAKEY_LOW)&(~CHROMA_KEY_LOW));
            dwChromaHigh |= (VIDInD(V_CHROMAKEY_HIGH)&(~CHROMA_KEY_HIGH));

            /*Added by Scottie[2001.12.5] for Chroma Key*/
            if (pVia->swov.DPFsrc.dwFlags & DDPF_FOURCC)
            {
                    switch (pVia->swov.DPFsrc.dwFourCC) {
                    case FOURCC_YV12:
		    case FOURCC_VIA:
                            /*to be continued...*/
                            break;
                    case FOURCC_YUY2:
                            /*to be continued...*/
                            break;
                    default:
                            /*TOINT3;*/
                            break;
                    }
            }
            else if (pVia->swov.DPFsrc.dwFlags & DDPF_RGB)
            {
                    unsigned long dwtmpLowR;
                    unsigned long dwtmpLowG;
                    unsigned long dwtmpLowB;
                    unsigned long dwtmpChromaLow;
                    unsigned long dwtmpHighR;
                    unsigned long dwtmpHighG;
                    unsigned long dwtmpHighB;
                    unsigned long dwtmpChromaHigh;

                    switch (pVia->swov.DPFsrc.dwRGBBitCount) {
                    case 16:
                            if (pVia->swov.DPFsrc.dwGBitMask==0x07E0) /*RGB16(5:6:5)*/
                            {
                                    dwtmpLowR = (((dwChromaLow >> 11) << 3) | ((dwChromaLow >> 13) & 0x07)) & 0xFF;
                                    dwtmpLowG = (((dwChromaLow >> 5) << 2) | ((dwChromaLow >> 9) & 0x03)) & 0xFF;

                                    dwtmpHighR = (((dwChromaHigh >> 11) << 3) | ((dwChromaHigh >> 13) & 0x07)) & 0xFF;
                                    dwtmpHighG = (((dwChromaHigh >> 5) << 2) | ((dwChromaHigh >> 9) & 0x03)) & 0xFF;
                            }
                            else /*RGB15(5:5:5)*/
                            {
                                    dwtmpLowR = (((dwChromaLow >> 10) << 3) | ((dwChromaLow >> 12) & 0x07)) & 0xFF;
                                    dwtmpLowG = (((dwChromaLow >> 5) << 3) | ((dwChromaLow >> 7) & 0x07)) & 0xFF;

                                    dwtmpHighR = (((dwChromaHigh >> 10) << 3) | ((dwChromaHigh >> 12) & 0x07)) & 0xFF;
                                    dwtmpHighG = (((dwChromaHigh >> 5) << 3) | ((dwChromaHigh >> 7) & 0x07)) & 0xFF;
                            }
                            dwtmpLowB = (((dwChromaLow << 3) | (dwChromaLow >> 2)) & 0x07) & 0xFF;
                            dwtmpChromaLow = (dwtmpLowG << 16) | (dwtmpLowR << 8) | dwtmpLowB;
                            dwChromaLow = ((dwChromaLow >> 24) << 24) | dwtmpChromaLow;

                            dwtmpHighB = (((dwChromaHigh << 3) | (dwChromaHigh >> 2)) & 0x07) & 0xFF;
                            dwtmpChromaHigh = (dwtmpHighG << 16) | (dwtmpHighR << 8) | dwtmpHighB;
                            dwChromaHigh = ((dwChromaHigh >> 24) << 24) | dwtmpChromaHigh;
                            break;

                    case 32: /*32 bit RGB*/
                            dwtmpLowR = (dwChromaLow >> 16) & 0xFF;
                            dwtmpLowG = (dwChromaLow >> 8) & 0xFF;
                            dwtmpLowB = dwChromaLow & 0xFF;
                            dwtmpChromaLow = (dwtmpLowG << 16) | (dwtmpLowR << 8) | dwtmpLowB;
                            dwChromaLow = ((dwChromaLow >> 24) << 24) | dwtmpChromaLow;

                            dwtmpHighR = (dwChromaHigh >> 16) & 0xFF;
                            dwtmpHighG = (dwChromaHigh >> 8) & 0xFF;
                            dwtmpHighB = dwChromaHigh & 0xFF;
                            dwtmpChromaHigh = (dwtmpHighG << 16) | (dwtmpHighR << 8) | dwtmpHighB;
                            dwChromaHigh = ((dwChromaHigh >> 24) << 24) | dwtmpChromaHigh;
                            break;

                    default:
                            /*TOINT3;*/
                            break;
                    }
            }/*End of DDPF_FOURCC*/

            viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER,V_CHROMAKEY_HIGH,dwChromaHigh);
            if (dwVideoFlag & VIDEO_1_INUSE)
            {
                viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER,V_CHROMAKEY_LOW, dwChromaLow);
                /*Temporarily solve the H/W Interpolation error when using Chroma Key*/
                viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER,V1_MINI_CONTROL, miniCtl & 0xFFFFFFF8);
            }
            else
            {
                viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER,V_CHROMAKEY_LOW, dwChromaLow|V_CHROMAKEY_V3);
                viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER,V3_MINI_CONTROL, miniCtl & 0xFFFFFFF8);
            }

            /*Modified by Scottie[2001.12.5] for select video if (color key & chroma key)*/
            if (dwCompose==SELECT_VIDEO_IF_COLOR_KEY)
               dwCompose = SELECT_VIDEO_IF_COLOR_KEY | SELECT_VIDEO_IF_CHROMA_KEY;
            else
               dwCompose = (dwCompose & ~0x0f) | SELECT_VIDEO_IF_CHROMA_KEY;
        }

        /* determine which video stream is on top */
        /*
        DBG_DD(ErrorF("        dwFlags= 0x%08lx\n", dwFlags));
        if (dwFlags & DDOVER_CLIP)
            dwCompose |= COMPOSE_V3_TOP;
        else
            dwCompose |= COMPOSE_V1_TOP;
        */    
        DBG_DD(ErrorF("        pVia->Video.dwCompose 0x%lx\n", pVia->Video.dwCompose));

        if (pVia->Video.dwCompose & (VW_TV1_TOP | VW_TV_TOP) )
            dwCompose |= COMPOSE_V3_TOP;
        else if (pVia->Video.dwCompose & (VW_TV0_TOP | VW_DVD_TOP) )
            dwCompose &= ~COMPOSE_V3_TOP;

        DBG_DD(ErrorF("        dwCompose 0x%8lx\n", dwCompose));

        /* Setup video control*/
        if (dwVideoFlag & VIDEO_HQV_INUSE)
        {
            if (!pVia->swov.SWVideo_ON)
            /*if (0)*/
            {
                DBG_DD(ErrorF("    First HQV\n")); 
        
                viaMacro_VidREGFlush(pVia);
                
                DBG_DD(ErrorF(" Wait flips"));
                if ( hwDiff->dwHQVInitPatch )  
                {
                    DBG_DD(ErrorF(" Wait flips 1"));
                    viaWaitHQVFlipClear(pVia, ((dwHQVCtl&~HQV_SW_FLIP)|HQV_FLIP_STATUS)&~HQV_ENABLE);
                    VIDOutD(HQV_CONTROL, dwHQVCtl);
                    DBG_DD(ErrorF(" Wait flips2"));
                    viaWaitHQVFlip(pVia);
                    DBG_DD(ErrorF(" Wait flips 3"));
                    viaWaitHQVFlipClear(pVia, ((dwHQVCtl&~HQV_SW_FLIP)|HQV_FLIP_STATUS)&~HQV_ENABLE);
                    VIDOutD(HQV_CONTROL, dwHQVCtl);
                    DBG_DD(ErrorF(" Wait flips4"));
                    viaWaitHQVFlip(pVia);
                }
                else    /* CLE_C0 */
                {
                    /* check HQV is idle */
                    {
                        CARD32 volatile *pdwState = (CARD32 volatile *) (pVia->VidMapBase+HQV_CONTROL);
                        DBG_DD(ErrorF("HQV control wf - %08x\n", *pdwState));
                        
                        while(!(*pdwState & HQV_IDLE))
                        {
                            DBG_DD(ErrorF("HQV control busy - %08x\n", *pdwState));
                            usleep(1);
                        }
                    }

                    VIDOutD(HQV_CONTROL, dwHQVCtl & ~HQV_SW_FLIP);
                    VIDOutD(HQV_CONTROL, dwHQVCtl | HQV_SW_FLIP);

                    {
                        CARD32 volatile *pdwState = (CARD32 volatile *) (pVia->VidMapBase+HQV_CONTROL);
			int i = 50;
			
                        DBG_DD(ErrorF("HQV control wf5 - %08x\n", *pdwState));

                        DBG_DD(ErrorF(" Wait flips5")); 

                        while (!(*pdwState & HQV_FLIP_STATUS))
                        {
                            DBG_DD(ErrorF(" HQV wait %d %08x\n",i, *pdwState));

                            *pdwState |= HQV_SW_FLIP|HQV_FLIP_STATUS;

                            usleep(1);

                            if (!i--)
                                break;
                        }
#if 0
                        viaWaitHQVFlip(pVia);
#endif
                        DBG_DD(ErrorF(" Wait flips6"));
                    }
                }
                
                if (dwVideoFlag & VIDEO_1_INUSE)
                {
                    VIDOutD(V1_CONTROL, dwVidCtl);
                    VIDOutD(V_COMPOSE_MODE, (dwCompose|V1_COMMAND_FIRE ));
                    if (pVia->swov.gdwUseExtendedFIFO)
                    {
                        /*Set Display FIFO*/
			DBG_DD(ErrorF(" Wait flips7"));
                        viaWaitVBI(pVia);
			DBG_DD(ErrorF(" Wait flips 8"));
			hwp->writeSeq(hwp, 0x17, 0x2F);
			ViaSeqMask(hwp, 0x16, 0x14, 0x1F);
			hwp->writeSeq(hwp, 0x18, 0x56);
			DBG_DD(ErrorF(" Wait flips 9"));
                    }
                }
                else
                {
		    DBG_DD(ErrorF(" Wait flips 10"));
                    VIDOutD(V3_CONTROL, dwVidCtl);
                    VIDOutD(V_COMPOSE_MODE, (dwCompose|V3_COMMAND_FIRE ));                    
                }
		DBG_DD(ErrorF(" Done flips"));
            }
            else
            {
                DBG_DD(ErrorF("    Normal called\n"));
                if (dwVideoFlag & VIDEO_1_INUSE)
                {
                    viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER,V1_CONTROL, dwVidCtl);
                    viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER,V_COMPOSE_MODE, (dwCompose|V1_COMMAND_FIRE ));
                }
                else
                {
                    viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER,V3_CONTROL, dwVidCtl);
                    viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER,V_COMPOSE_MODE, (dwCompose|V3_COMMAND_FIRE ));                    
                }
                viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER, HQV_CONTROL, dwHQVCtl|HQV_FLIP_STATUS);
                viaWaitHQVDone(pVia);
                viaMacro_VidREGFlush(pVia);                
            }
        }
        else
        {
            if (dwVideoFlag & VIDEO_1_INUSE)
            {
                viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER,V1_CONTROL, dwVidCtl);
                viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER,V_COMPOSE_MODE, (dwCompose|V1_COMMAND_FIRE ));
            }
            else
            {
                viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER,V3_CONTROL, dwVidCtl);
                viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER,V_COMPOSE_MODE, (dwCompose|V3_COMMAND_FIRE ));                
            }
            viaWaitHQVDone(pVia);
            viaMacro_VidREGFlush(pVia);
        }
        pVia->swov.SWVideo_ON = TRUE;      
    }
    else
    {
        /*Hide overlay*/
        
        if ( hwDiff->dwHQVDisablePatch )     /*CLE_C0*/
	    ViaSeqMask(hwp, 0x2E, 0x00, 0x10);
        
        viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER,V_FIFO_CONTROL,V1_FIFO_PRETHRESHOLD12 |
             V1_FIFO_THRESHOLD8 |V1_FIFO_DEPTH16);
        viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER,ALPHA_V3_FIFO_CONTROL, ALPHA_FIFO_THRESHOLD4 
             | ALPHA_FIFO_DEPTH8  | V3_FIFO_THRESHOLD24 | V3_FIFO_DEPTH32 );

        if (dwVideoFlag&VIDEO_HQV_INUSE)
        {
            viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER,HQV_CONTROL, (VIDInD(HQV_CONTROL) & (~HQV_ENABLE)));
        }

        if (dwVideoFlag&VIDEO_1_INUSE)
        {
            viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER,V1_CONTROL, (VIDInD(V1_CONTROL) & (~V1_ENABLE)));
            viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER,V_COMPOSE_MODE, (VIDInD(V_COMPOSE_MODE)|V1_COMMAND_FIRE));
        }
        else
        {
            viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER,V3_CONTROL, (VIDInD(V3_CONTROL) & (~V3_ENABLE)));        
            viaMacro_VidREGRec(pVia, VIDREGREC_SAVE_REGISTER,V_COMPOSE_MODE, (VIDInD(V_COMPOSE_MODE)|V3_COMMAND_FIRE));
        }

        viaMacro_VidREGFlush(pVia);
        
        if ( hwDiff->dwHQVDisablePatch )     /*CLE_C0*/
	     ViaSeqMask(hwp, 0x2E, 0x10, 0x10);
    }  
    DBG_DD(ErrorF(" Done Upd_Video"));

    return PI_OK;    
    
} /* Upd_Video */

/*************************************************************************
 *  VIAVidUpdateOverlay
 *  Parameters:   src rectangle, dst rectangle, colorkey...
 *  Return Value: unsigned long of state
 *  note: Update the overlay image param.
*************************************************************************/
unsigned long VIAVidUpdateOverlay(ScrnInfoPtr pScrn, LPDDUPDATEOVERLAY lpUpdate)
{
    VIAPtr  pVia = VIAPTR(pScrn);
    vgaHWPtr hwp = VGAHWPTR(pScrn);
    unsigned long dwFlags = lpUpdate->dwFlags;
    unsigned long dwKeyLow=0, dwKeyHigh=0;
    unsigned long dwChromaLow=0, dwChromaHigh=0;
    unsigned long dwVideoFlag=0;
    unsigned long dwColorKey=0, dwChromaKey=0;
    /*DDUPDATEOVERLAY UpdateOverlayTemp;*/
    int   nDstTop, nDstBottom, nDstLeft, nDstRight;

    DBG_DD(ErrorF("// VIAVidUpdateOverlay: %08lx\n", dwFlags));
    
    /* Adjust to fix panning mode bug */
    lpUpdate->rDest.left = lpUpdate->rDest.left - (pVia->swov.panning_x - pVia->swov.panning_old_x);
    lpUpdate->rDest.top = lpUpdate->rDest.top - (pVia->swov.panning_y - pVia->swov.panning_old_y);
    lpUpdate->rDest.right = lpUpdate->rDest.right - (pVia->swov.panning_x - pVia->swov.panning_old_x);
    lpUpdate->rDest.bottom = lpUpdate->rDest.bottom - (pVia->swov.panning_y - pVia->swov.panning_old_y);

    DBG_DD(ErrorF("Raw rSrc  X (%ld,%ld) Y (%ld,%ld)\n",
                lpUpdate->rSrc.left, lpUpdate->rSrc.right, lpUpdate->rSrc.top, lpUpdate->rSrc.bottom));
    DBG_DD(ErrorF("Raw rDest  X (%ld,%ld) Y (%ld,%ld)\n",
                lpUpdate->rDest.left, lpUpdate->rDest.right, lpUpdate->rDest.top, lpUpdate->rDest.bottom));

/*    if (pVia->swov.gdwVideoFlagTV1 && !gdwOverlaySupport)
        return PI_OK;
*/
    if ( (pVia->swov.DPFsrc.dwFourCC == FOURCC_YUY2)||( pVia->swov.DPFsrc.dwFourCC == FOURCC_YV12 ) || (  pVia->swov.DPFsrc.dwFourCC == FOURCC_VIA))
        dwVideoFlag = pVia->swov.gdwVideoFlagSW;

    dwFlags |= DDOVER_INTERLEAVED;

    /* For Alpha windows setting */
    if (pVia->swov.gdwAlphaEnabled)
        dwFlags &= ~DDOVER_KEYDEST;

    viaMacro_VidREGRec(pVia, VIDREGREC_RESET_COUNTER, 0,0);

    if ( dwFlags & DDOVER_HIDE )
    {
        DBG_DD(ErrorF("//    :DDOVER_HIDE \n"));
        
        dwVideoFlag &= ~VIDEO_SHOW;
        if (Upd_Video(pScrn, dwVideoFlag,0,lpUpdate->rSrc,lpUpdate->rDest,0,0,0,&pVia->swov.DPFsrc,0,
                0,0,0,0,0,0, dwFlags)== PI_ERR)
        {
                return PI_ERR;                    
        }
        pVia->swov.SWVideo_ON = FALSE;
        pVia->swov.UpdateOverlayBackup.rDest.left = 0;
        pVia->swov.UpdateOverlayBackup.rDest.top = 0;
        pVia->swov.UpdateOverlayBackup.rDest.right = 0;
        pVia->swov.UpdateOverlayBackup.rDest.bottom = 0;

        if (pVia->swov.gdwUseExtendedFIFO)
        {
            /*Restore Display fifo*/
	    hwp->writeSeq(hwp, 0x16, pVia->swov.Save_3C4_16);
	    hwp->writeSeq(hwp, 0x17, pVia->swov.Save_3C4_17);
	    hwp->writeSeq(hwp, 0x18, pVia->swov.Save_3C4_18);
	    DBG_DD(ErrorF("Restore 3c4.16 : %08x \n", hwp->readSeq(hwp, 0x16)));
	    DBG_DD(ErrorF("        3c4.17 : %08x \n", hwp->readSeq(hwp, 0x17)));
	    DBG_DD(ErrorF("        3c4.18 : %08x \n", hwp->readSeq(hwp, 0x18)));
            pVia->swov.gdwUseExtendedFIFO = 0;
	}
        return PI_OK;
    }

    /*
    if ( (pVia->swov.UpdateOverlayBackup.rDest.left ==  lpUpdate->rDest.left) &&
         (pVia->swov.UpdateOverlayBackup.rDest.top ==  lpUpdate->rDest.top) &&
         (pVia->swov.UpdateOverlayBackup.rDest.right ==  lpUpdate->rDest.right) &&
         (pVia->swov.UpdateOverlayBackup.rDest.bottom ==  lpUpdate->rDest.bottom) )
	 return PI_OK; */
    pVia->swov.UpdateOverlayBackup = * (LPDDUPDATEOVERLAY) lpUpdate;

    if ( dwFlags & DDOVER_KEYDEST )
    {
        DBG_DD(ErrorF("//    :DDOVER_KEYDEST \n"));
        
        dwColorKey = 1;
        dwKeyLow = lpUpdate->dwColorSpaceLowValue;
    }

    if (dwFlags & DDOVER_SHOW)    
    {
        unsigned long dwStartAddr=0, dwDeinterlaceMode=0;
        unsigned long dwScnWidth, dwScnHeight;

        DBG_DD(ErrorF("//    :DDOVER_SHOW \n"));

        /*for SW decode HW overlay use*/
        dwStartAddr = VIDInD(HQV_SRC_STARTADDR_Y);
        DBG_DD(ErrorF("dwStartAddr= 0x%lx\n", dwStartAddr));

        if (dwFlags & DDOVER_INTERLEAVED)
        {
            dwDeinterlaceMode |= DDOVER_INTERLEAVED;
            DBG_DD(ErrorF("DDOVER_INTERLEAVED\n"));
        }
        if (dwFlags & DDOVER_BOB)
        {
            dwDeinterlaceMode |= DDOVER_BOB;
            DBG_DD(ErrorF("DDOVER_BOB\n"));
        }

	if (pVia->ChipId == PCI_CHIP_CLE3122) {
	  if ((pScrn->currentMode->HDisplay > 1024))
	    {
	      DBG_DD(ErrorF("UseExtendedFIFO\n"));
	      pVia->swov.gdwUseExtendedFIFO = 1;
	    }

	  /*
	    else
	    {
	    //Set Display FIFO
	    ViaSeqMask(hwp, 0x16, 0x0C, 0xE0);
	    DBG_DD(ErrorF("set     3c4.16 : %08x \n",hwp->readSeq(hwp, 0x16)));
	    hwp->writeSeq(hwp, 0x18, 0x4c);
	    DBG_DD(ErrorF("        3c4.18 : %08x \n",hwp->readSeq(hwp, 0x18)));
	    }
	  */
	} else {
	  pVia->swov.gdwUseExtendedFIFO = 0;
	}

        dwVideoFlag |= VIDEO_SHOW;

        /*
         * Figure out actual rSrc rectangle
         * Coz the Src rectangle AP sent is always original, ex:size(720,480) at (0,0)
         * so the driver need to re-calc
         * 
         * transfer unsigned long to signed int for calc
         */
        nDstLeft = lpUpdate->rDest.left;
        nDstTop  = lpUpdate->rDest.top;
        nDstRight= lpUpdate->rDest.right;
        nDstBottom=lpUpdate->rDest.bottom;

        dwScnWidth  = pScrn->currentMode->HDisplay;
        dwScnHeight = pScrn->currentMode->VDisplay;

        if (nDstLeft<0)
            lpUpdate->rSrc.left  = (((-nDstLeft) * pVia->swov.overlayRecordV1.dwV1OriWidth) + ((nDstRight-nDstLeft)>>1)) / (nDstRight-nDstLeft);
        else 
            lpUpdate->rSrc.left = 0;

        if (nDstRight>dwScnWidth)
            lpUpdate->rSrc.right = (((dwScnWidth-nDstLeft) * pVia->swov.overlayRecordV1.dwV1OriWidth) + ((nDstRight-nDstLeft)>>1)) / (nDstRight-nDstLeft);
        else    
            lpUpdate->rSrc.right = pVia->swov.overlayRecordV1.dwV1OriWidth;

        if (nDstTop<0)
           lpUpdate->rSrc.top   =  (((-nDstTop) * pVia->swov.overlayRecordV1.dwV1OriHeight) + ((nDstBottom-nDstTop)>>1))/ (nDstBottom-nDstTop);
        else
           lpUpdate->rSrc.top   = 0;

        if (nDstBottom >dwScnHeight)
            lpUpdate->rSrc.bottom = (((dwScnHeight-nDstTop) * pVia->swov.overlayRecordV1.dwV1OriHeight) + ((nDstBottom-nDstTop)>>1)) / (nDstBottom-nDstTop);
        else 
            lpUpdate->rSrc.bottom = pVia->swov.overlayRecordV1.dwV1OriHeight;

        /* save modified src & original dest rectangle param.*/

	if ( (pVia->swov.DPFsrc.dwFourCC == FOURCC_YUY2)||( pVia->swov.DPFsrc.dwFourCC == FOURCC_YV12 ) || (  pVia->swov.DPFsrc.dwFourCC == FOURCC_VIA))
        {   
            pVia->swov.SWDevice.gdwSWDstLeft     = lpUpdate->rDest.left + (pVia->swov.panning_x - pVia->swov.panning_old_x);
            pVia->swov.SWDevice.gdwSWDstTop      = lpUpdate->rDest.top + (pVia->swov.panning_y - pVia->swov.panning_old_y);
            pVia->swov.SWDevice.gdwSWDstWidth    = lpUpdate->rDest.right - lpUpdate->rDest.left;
            pVia->swov.SWDevice.gdwSWDstHeight   = lpUpdate->rDest.bottom - lpUpdate->rDest.top;

            pVia->swov.SWDevice.gdwSWSrcWidth    = pVia->swov.overlayRecordV1.dwV1SrcWidth = lpUpdate->rSrc.right - lpUpdate->rSrc.left;
            pVia->swov.SWDevice.gdwSWSrcHeight   = pVia->swov.overlayRecordV1.dwV1SrcHeight = lpUpdate->rSrc.bottom - lpUpdate->rSrc.top;
        }

        pVia->swov.overlayRecordV1.dwV1SrcLeft   = lpUpdate->rSrc.left;
        pVia->swov.overlayRecordV1.dwV1SrcRight  = lpUpdate->rSrc.right;
        pVia->swov.overlayRecordV1.dwV1SrcTop    = lpUpdate->rSrc.top;
        pVia->swov.overlayRecordV1.dwV1SrcBot    = lpUpdate->rSrc.bottom;
        
        /*
        // Figure out actual rDest rectangle
        */
        lpUpdate->rDest.left= nDstLeft<0 ? 0 : nDstLeft;
        lpUpdate->rDest.top= nDstTop<0 ? 0 : nDstTop;
        if ( lpUpdate->rDest.top >= dwScnHeight)
           lpUpdate->rDest.top = dwScnHeight-1;
        /*lpUpdate->rDest.top= top>=dwScnHeight   ? dwScnHeight-1: top;*/
        lpUpdate->rDest.right= nDstRight>dwScnWidth ? dwScnWidth: nDstRight;
        lpUpdate->rDest.bottom= nDstBottom>dwScnHeight ? dwScnHeight: nDstBottom;

        /* 
         *	Check which update func. (upd_MPEG, upd_video, 
         *	upd_capture) to call
         */
        if (Upd_Video(pScrn, dwVideoFlag,dwStartAddr,lpUpdate->rSrc,lpUpdate->rDest,pVia->swov.SWDevice.dwPitch,
             pVia->swov.overlayRecordV1.dwV1OriWidth,pVia->swov.overlayRecordV1.dwV1OriHeight,&pVia->swov.DPFsrc,
             dwDeinterlaceMode,dwColorKey,dwChromaKey,
             dwKeyLow,dwKeyHigh,dwChromaLow,dwChromaHigh, dwFlags)== PI_ERR)
             {
                 return PI_ERR;                    
             }                
            pVia->swov.SWVideo_ON = FALSE;

        return PI_OK;
        
    } /*end of DDOVER_SHOW*/

    pVia->swov.panning_old_x = pVia->swov.panning_x;
    pVia->swov.panning_old_y = pVia->swov.panning_y;

    return PI_OK;

} /*VIAVidUpdateOverlay*/



/*************************************************************************
 *  ADJUST FRAME
*************************************************************************/
unsigned long VIAVidAdjustFrame(ScrnInfoPtr pScrn, LPADJUSTFRAME lpAdjustFrame)
{
    VIAPtr pVia = VIAPTR(pScrn);
    DBG_DD(ErrorF("//VIAVidAdjustFrame\n"));

    pVia->swov.panning_x = lpAdjustFrame->x;
    pVia->swov.panning_y = lpAdjustFrame->y;            

    return PI_OK;
}