summaryrefslogtreecommitdiff
path: root/src/video_out/video_out_directfb.c
blob: 71f3f8027e45c3218d0a74c54d3badea705bc4e9 (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
/*
 * Copyright (C) 2000-2005 the xine project and Claudio Ciccani
 * 
 * This file is part of xine, a free video player.
 * 
 * xine is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * xine is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
 *
 *
 * DirectFB based output plugin by Claudio Ciccani <klan@directfb.org>
 * 
 * Based on video_out_xv.c and video_out_vidix.c.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef HAVE_X11
# include <X11/Xlib.h>
#endif

#ifdef HAVE_FB
# include <fcntl.h>
# include <unistd.h>
# include <sys/ioctl.h>
# include <linux/fb.h>
#endif

#define LOG_MODULE "video_out_directfb"
#define LOG_VERBOSE

#include "xine.h"
#include "xine_internal.h"
#include "video_out.h"
#include "xineutils.h"
#include "vo_scale.h"

#include <directfb.h>
#include <directfb_version.h>


typedef struct directfb_frame_s {
  vo_frame_t                   vo_frame;

  int                          width;
  int                          height;
  DFBSurfacePixelFormat        format;
  double                       ratio;

  IDirectFBSurface            *surface;
  int                          locked;
} directfb_frame_t;

typedef struct directfb_driver_s {
  vo_driver_t                  vo_driver;
  
  int                          visual_type;

  xine_t                      *xine;
  
  directfb_frame_t            *recent_frames[VO_NUM_RECENT_FRAMES];

  /* DirectFB related stuff */
  IDirectFB                   *dfb;
  IDirectFBDisplayLayer       *layer;
  IDirectFBSurface            *surface;
  DFBDisplayLayerCapabilities  caps;
  DFBDisplayLayerConfig        config;
  DFBColorAdjustment           default_cadj;
  DFBColorAdjustment           cadj;
  
  /* for hardware scaling */
  IDirectFBSurface            *temp;
  int                          temp_frame_width;
  int                          temp_frame_height;
  DFBSurfacePixelFormat        temp_frame_format;
  
  /* wheter card supports stretchblit with deinterlacing */
  int                          hw_deinterlace;
  
  /* wheter to enable deinterlacing */
  int                          deinterlace;
  
  /* configurable options */
  int                          buffermode;
  int                          vsync;
  int                          colorkeying;
  uint32_t                     colorkey;
  int                          flicker_filtering;
  int                          field_parity;
  
#ifdef HAVE_X11
  /* X11 related stuff */
  Display                     *display;
  int                          screen;
  Drawable                     drawable;
  GC                           gc;
  int                          depth;
#endif

  /* screen size */
  int                          screen_width;
  int                          screen_height;
  
  /* size / aspect ratio calculations */
  vo_scale_t                   sc;

  /* gui callbacks */
  alphablend_t                 alphablend_extra_data;
} directfb_driver_t;

typedef struct {
  video_driver_class_t         driver_class;
  xine_t                      *xine;
} directfb_class_t;


#define DEFAULT_COLORKEY  0x202040


#ifndef MAX
# define MAX(a,b) (((a) > (b)) ? (a) : (b))
#endif

/*** driver functions ***/

static uint32_t directfb_get_capabilities (vo_driver_t *this_gen) {
  return VO_CAP_YV12 | VO_CAP_YUY2 | VO_CAP_CROP;
}

static void directfb_frame_field (vo_frame_t *vo_img, int which_field) {
  /* not needed */
}

static void directfb_frame_dispose (vo_frame_t *vo_img) {
  directfb_frame_t *frame = (directfb_frame_t *) vo_img;

  if (frame) {
    if (frame->surface) {
      if (frame->locked) 
        frame->surface->Unlock (frame->surface);
      frame->surface->Release (frame->surface);
    }
    
    free (frame);
  }
}

static vo_frame_t *directfb_alloc_frame (vo_driver_t *this_gen) {
  directfb_driver_t *this = (directfb_driver_t *) this_gen;
  directfb_frame_t  *frame;

  frame = (directfb_frame_t *) xine_xmalloc (sizeof (directfb_frame_t));
  if (!frame) {
    xprintf (this->xine, XINE_VERBOSITY_DEBUG, 
             "video_out_directfb: directfb_alloc_frame: out of memory\n");
    return NULL;
  }

  pthread_mutex_init (&frame->vo_frame.mutex, NULL);
  
  frame->vo_frame.proc_slice = NULL; 
  frame->vo_frame.proc_frame = NULL;
  frame->vo_frame.field      = directfb_frame_field; 
  frame->vo_frame.dispose    = directfb_frame_dispose;
  frame->vo_frame.driver     = this_gen;
  
  return &frame->vo_frame;
}

static void directfb_update_frame_format (vo_driver_t *this_gen,
                                          vo_frame_t *frame_gen,
                                          uint32_t width, uint32_t height,
                                          double ratio, int fmt, int flags) {
  directfb_driver_t     *this   = (directfb_driver_t *) this_gen;
  directfb_frame_t      *frame  = (directfb_frame_t *) frame_gen;
  DFBSurfacePixelFormat  format = (fmt == XINE_IMGFMT_YUY2) ? DSPF_YUY2 : DSPF_YV12;
  
  if (frame->surface == NULL   ||
      frame->width   != width  ||
      frame->height  != height ||
      frame->format  != format) 
  {
    DFBSurfaceDescription dsc;
    DFBResult             ret;
    
    lprintf ("frame %p: format changed to %dx%d %s.\n",
             frame, width, height, (format == DSPF_YUY2) ? "YUY2" : "YV12");
    
    if (frame->surface) {
      if (frame->locked)
        frame->surface->Unlock (frame->surface);
      frame->surface->Release (frame->surface);
      frame->surface = NULL;
      frame->locked  = 0;
    }
    
    dsc.flags       = DSDESC_CAPS   | DSDESC_WIDTH |
                      DSDESC_HEIGHT | DSDESC_PIXELFORMAT;
    dsc.caps        = DSCAPS_SYSTEMONLY;
    dsc.width       = (width  + 7) & ~7;
    dsc.height      = (height + 1) & ~1;
    dsc.pixelformat = format;
    
    ret = this->dfb->CreateSurface (this->dfb, &dsc, &frame->surface);
    if (ret != DFB_OK) {
      DirectFBError ("IDirectFB::CreateSurface()", ret);
      return;
    }
    
    frame->width  = width;
    frame->height = height;
    frame->format = format;
    
    ret = frame->surface->Lock (frame->surface, 
                                DSLF_READ | DSLF_WRITE,   
                                (void**)&frame->vo_frame.base[0],
                                (int  *)&frame->vo_frame.pitches[0]);
    if (ret != DFB_OK) {
      DirectFBError ("IDirectFBSurface::Lock()", ret);
      return;
    }
    frame->locked = 1;
    
    if (frame->format == DSPF_YV12) {
      frame->vo_frame.pitches[1] = frame->vo_frame.pitches[0]/2;
      frame->vo_frame.pitches[2] = frame->vo_frame.pitches[0]/2;
      frame->vo_frame.base[2]    = frame->vo_frame.base[0] + 
                                   dsc.height   * frame->vo_frame.pitches[0];
      frame->vo_frame.base[1]    = frame->vo_frame.base[2] +
                                   dsc.height/2 * frame->vo_frame.pitches[2];
    }
  }
  
  frame->ratio = ratio;
}

static void directfb_clean_output_area (directfb_driver_t *this) {
  if (this->visual_type == XINE_VISUAL_TYPE_X11) {
#ifdef HAVE_X11
    if (this->config.options & DLOP_DST_COLORKEY) {
      uint32_t pixel;
      int      i;
    
      switch (this->depth) {
        case 8:
          pixel = ((this->colorkey & 0xe00000) >> 16) |
                  ((this->colorkey & 0x00e000) >> 11) |
                  ((this->colorkey & 0x0000c0) >>  6);
          break;
        case 15:
          pixel = ((this->colorkey & 0xf80000) >>  9) |
                  ((this->colorkey & 0x00f800) >>  6) |
                  ((this->colorkey & 0x0000f8) >>  3);
          break;
        case 16:
          pixel = ((this->colorkey & 0xf80000) >>  8) |
                  ((this->colorkey & 0x00fc00) >>  5) |
                  ((this->colorkey & 0x0000f8) >>  3);
          break;
        default:
          pixel = this->colorkey;
          break;
      }
        
      XLockDisplay (this->display);    
      
      XSetForeground (this->display, this->gc, BlackPixel(this->display, this->screen));
    
      for (i = 0; i < 4; i++) {
        if (this->sc.border[i].w && this->sc.border[i].h) {
          XFillRectangle (this->display, this->drawable, this->gc,
                          this->sc.border[i].x, this->sc.border[i].y,
                          this->sc.border[i].w, this->sc.border[i].h);
        }
      }
    
      XSetForeground (this->display, this->gc, pixel);
      XFillRectangle (this->display, this->drawable, this->gc, 
                      this->sc.output_xoffset, this->sc.output_yoffset,
                      this->sc.output_width, this->sc.output_height);
    
      XFlush (this->display);

      XUnlockDisplay (this->display);
    }
#endif
  }
  else if (!(this->caps & DLCAPS_SCREEN_LOCATION)) {
    DFBRectangle rect[4];
    int          i; 

    for (i = 0; i < 4; i++) {
      rect[i].x = MAX(this->sc.border[i].x, 0);
      rect[i].y = MAX(this->sc.border[i].y, 0);
      rect[i].w = MAX(this->sc.border[i].w, 0);
      rect[i].h = MAX(this->sc.border[i].h, 0);
    }
      
    this->surface->SetColor (this->surface, 0x00, 0x00, 0x00, 0xff);
    this->surface->FillRectangles (this->surface, &rect[0], 4);
  }
}

static void directfb_overlay_blend (vo_driver_t *this_gen,
                                    vo_frame_t *frame_gen, vo_overlay_t *overlay) {
  directfb_driver_t *this  = (directfb_driver_t *) this_gen;
  directfb_frame_t  *frame = (directfb_frame_t *) frame_gen;
  
  this->alphablend_extra_data.offset_x = frame_gen->overlay_offset_x;
  this->alphablend_extra_data.offset_y = frame_gen->overlay_offset_y;
  
  if (frame->format == DSPF_YUY2) {
    _x_blend_yuy2 (frame->vo_frame.base[0], overlay,
                frame->width, frame->height,
                frame->vo_frame.pitches[0],
                &this->alphablend_extra_data);
  }
  else {
    _x_blend_yuv (frame->vo_frame.base, overlay, 
               frame->width, frame->height,
               frame->vo_frame.pitches,
               &this->alphablend_extra_data);
  }
}

static int directfb_redraw_needed (vo_driver_t *this_gen) {
  directfb_driver_t *this  = (directfb_driver_t *) this_gen;
  
  if (_x_vo_scale_redraw_needed (&this->sc)) {
    _x_vo_scale_compute_output_size (&this->sc);
    
    if (this->caps & DLCAPS_SCREEN_LOCATION) {  
      this->layer->SetSourceRectangle (this->layer,
                                       this->sc.displayed_xoffset,
                                       this->sc.displayed_yoffset,
                                       this->sc.displayed_width, 
                                       this->sc.displayed_height );
      this->layer->SetScreenRectangle (this->layer,
                                       this->sc.gui_win_x+this->sc.output_xoffset, 
                                       this->sc.gui_win_y+this->sc.output_yoffset,
                                       this->sc.output_width,
                                       this->sc.output_height);
    }
      
    directfb_clean_output_area (this);
  }
  
  return 0;
}

static void directfb_add_recent_frame (directfb_driver_t *this, directfb_frame_t *frame) {
  int i;

  i = VO_NUM_RECENT_FRAMES-1;
  if (this->recent_frames[i])
    this->recent_frames[i]->vo_frame.free (&this->recent_frames[i]->vo_frame);

  for (; i; i--)
    this->recent_frames[i] = this->recent_frames[i-1];

  this->recent_frames[0] = frame;
}

/* directfb_display_frame(): output to overlay */
static void directfb_display_frame (vo_driver_t *this_gen, vo_frame_t *frame_gen) {
  directfb_driver_t *this  = (directfb_driver_t *) this_gen;
  directfb_frame_t  *frame = (directfb_frame_t *) frame_gen;
  DFBResult          ret;
  
  directfb_add_recent_frame (this, frame);
  
  this->config.flags = DLCONF_NONE;
  
  if (frame->width != this->config.width) {
    this->config.flags |= DLCONF_WIDTH;
    this->config.width  = frame->width;
  }
   
  if (frame->height != this->config.height) {
    this->config.flags  |= DLCONF_HEIGHT;
    this->config.height  = frame->height;
  }
      
  if (frame->format != this->config.pixelformat) {
    this->config.flags      |= DLCONF_PIXELFORMAT;
    this->config.pixelformat = frame->format;
  }
  
  if (this->caps & DLCAPS_DEINTERLACING) {
    if (this->deinterlace &&
      !(this->config.options & DLOP_DEINTERLACING)) {
      this->config.flags   |= DLCONF_OPTIONS;
      this->config.options |= DLOP_DEINTERLACING;
    }
    else if (!this->deinterlace &&
            (this->config.options & DLOP_DEINTERLACING)) {
      this->config.flags   |= DLCONF_OPTIONS;
      this->config.options &= ~DLOP_DEINTERLACING;
    }
  }
  
  if (this->config.flags) {
    lprintf ("changing layer configuration to %dx%d %s.\n",
             this->config.width, this->config.height,
             (this->config.pixelformat == DSPF_YUY2) ? "YUY2" : "YV12");
    
    ret = this->layer->SetConfiguration (this->layer, &this->config);
    if (ret != DFB_OK)
      DirectFBError( "IDirectFBDisplayLayer::SetConfiguration()", ret );      
    this->layer->GetConfiguration (this->layer, &this->config);
  }
  
  if (this->sc.delivered_width  != frame->width  ||
      this->sc.delivered_height != frame->height ||
      this->sc.delivered_ratio  != frame->ratio)
  {
    this->sc.delivered_width  = frame->width; 
    this->sc.delivered_height = frame->height; 
    this->sc.delivered_ratio  = frame->ratio;  
    this->sc.crop_left        = frame->vo_frame.crop_left;
    this->sc.crop_right       = frame->vo_frame.crop_right;
    this->sc.crop_top         = frame->vo_frame.crop_top;
    this->sc.crop_bottom      = frame->vo_frame.crop_bottom;
    
    _x_vo_scale_compute_ideal_size (&this->sc);
    this->sc.force_redraw = 1;
  }
  
  directfb_redraw_needed (&this->vo_driver);
 
  this->layer->SetOpacity (this->layer, 0xff);

  if (frame->locked) {
    frame->surface->Unlock (frame->surface);
    frame->locked = 0;
  }
  
  if (this->deinterlace) {
    if (!(this->config.options & DLOP_DEINTERLACING))
      this->surface->SetBlittingFlags (this->surface, DSBLIT_DEINTERLACE);
  } else
    this->surface->SetBlittingFlags (this->surface, DSBLIT_NOFX);
  
  this->surface->Blit (this->surface, frame->surface, NULL, 0, 0);
  this->surface->Flip (this->surface, NULL,
                      (this->vsync) ? DSFLIP_WAITFORSYNC : DSFLIP_ONSYNC);
  
  frame->surface->Lock (frame->surface, 
                        DSLF_READ | DSLF_WRITE, 
                        (void**)&frame->vo_frame.base[0],
                        (int  *)&frame->vo_frame.pitches[0]);
  frame->locked = 1;
}

/* directfb_display_frame2(): output to a generic (fixed) layer */
static void directfb_display_frame2 (vo_driver_t *this_gen, vo_frame_t *frame_gen) {
  directfb_driver_t       *this   = (directfb_driver_t *) this_gen;
  directfb_frame_t        *frame  = (directfb_frame_t *) frame_gen;
  DFBSurfaceBlittingFlags  flags;
  DFBRectangle             s, d;
  DFBResult                ret;
  
  directfb_add_recent_frame (this, frame);
   
  /* TODO: try to change video mode when frame size changes */
   
  if (this->temp) {
    /* try to reduce video memory fragmentation */
    if (this->temp_frame_width  <  frame->width  ||
        this->temp_frame_height <  frame->height ||
        this->temp_frame_format != frame->format)
    {
      DFBSurfaceDescription dsc;

      lprintf ("reallocating temporary surface.\n");
      
      this->temp->Release (this->temp);
      this->temp = NULL;
      
      dsc.flags       = DSDESC_CAPS   | DSDESC_WIDTH      |
                        DSDESC_HEIGHT | DSDESC_PIXELFORMAT;
      dsc.caps        = DSCAPS_VIDEOONLY;
      dsc.width       = frame->width;
      dsc.height      = frame->height;
      dsc.pixelformat = frame->format;
      
      ret = this->dfb->CreateSurface (this->dfb, &dsc, &this->temp);
      if (ret != DFB_OK)
        DirectFBError ("IDirectFB::CreateSurface()", ret);
     
      this->temp_frame_width  = frame->width;
      this->temp_frame_height = frame->height;
      this->temp_frame_format = frame->format;
    }
  }
  
  if (this->sc.delivered_width  != frame->width  ||
      this->sc.delivered_height != frame->height ||
      this->sc.delivered_ratio  != frame->ratio)
  {
    this->sc.delivered_width  = frame->width; 
    this->sc.delivered_height = frame->height; 
    this->sc.delivered_ratio  = frame->ratio;  
    this->sc.crop_left        = frame->vo_frame.crop_left;
    this->sc.crop_right       = frame->vo_frame.crop_right;
    this->sc.crop_top         = frame->vo_frame.crop_top;
    this->sc.crop_bottom      = frame->vo_frame.crop_bottom;
    
    _x_vo_scale_compute_ideal_size (&this->sc);
    this->sc.force_redraw = 1;
  }
  
  directfb_redraw_needed (&this->vo_driver);

  this->layer->SetOpacity (this->layer, 0xff);

  if (frame->locked) {
    frame->surface->Unlock (frame->surface);
    frame->locked = 0;
  }
  
  /* source rectangle */
  s.x = this->sc.displayed_xoffset;
  s.y = this->sc.displayed_yoffset;
  s.w = this->sc.displayed_width;
  s.h = this->sc.displayed_height;
  
  /* destination rectangle */
  d.x = this->sc.gui_win_x+this->sc.output_xoffset;
  d.y = this->sc.gui_win_y+this->sc.output_yoffset;
  d.w = this->sc.output_width;
  d.h = this->sc.output_height;
  
  flags = (this->deinterlace) ? DSBLIT_DEINTERLACE : DSBLIT_NOFX;
    
  if (this->temp) {
    if (this->hw_deinterlace)
      this->surface->SetBlittingFlags (this->surface, flags);
    else
      this->temp->SetBlittingFlags (this->temp, flags);      
      
    this->temp->Blit (this->temp, frame->surface, &s, s.x, s.y);
    this->surface->StretchBlit (this->surface, this->temp, &s, &d);
  } 
  else {
    this->surface->SetBlittingFlags (this->surface, flags);
    this->surface->StretchBlit (this->surface, frame->surface, &s, &d);
  }
  
  this->surface->Flip (this->surface, NULL,
                      (this->vsync) ? DSFLIP_WAITFORSYNC : DSFLIP_ONSYNC);
  
  frame->surface->Lock (frame->surface, 
                        DSLF_READ | DSLF_WRITE, 
                        (void**)&frame->vo_frame.base[0],
                        (int  *)&frame->vo_frame.pitches[0]);
  frame->locked = 1;
}

static int directfb_get_property (vo_driver_t *this_gen, int property) {
  directfb_driver_t *this = (directfb_driver_t *) this_gen;
  
  switch (property) {
    case VO_PROP_INTERLACED:
      return this->deinterlace;
      
    case VO_PROP_ASPECT_RATIO:
      return this->sc.user_ratio;
    
    case VO_PROP_HUE:
      if (this->caps & DLCAPS_HUE)
        return this->cadj.hue;
      break;
    
    case VO_PROP_SATURATION:
      if (this->caps & DLCAPS_SATURATION)
        return this->cadj.saturation;
      break;
    
    case VO_PROP_CONTRAST:
      if (this->caps & DLCAPS_CONTRAST)
        return this->cadj.contrast;
      break;
    
    case VO_PROP_BRIGHTNESS:
      if (this->caps & DLCAPS_BRIGHTNESS)
        return this->cadj.brightness;
      break;
    
    case VO_PROP_COLORKEY:
      if (this->caps & DLCAPS_DST_COLORKEY)
        return this->colorkey;
      break;
    
    case VO_PROP_ZOOM_X:
      return this->sc.zoom_factor_x * XINE_VO_ZOOM_STEP;
    
    case VO_PROP_ZOOM_Y:
      return this->sc.zoom_factor_y * XINE_VO_ZOOM_STEP;
    
    case VO_PROP_WINDOW_WIDTH:
      return this->sc.gui_width;
    
    case VO_PROP_WINDOW_HEIGHT:
      return this->sc.gui_height;
    
    default:
      break;
  }
  
  return 0;
}

static int directfb_set_property (vo_driver_t *this_gen, 
                                  int property, int value) {
  directfb_driver_t *this = (directfb_driver_t *) this_gen;
  
  switch (property) {
    case VO_PROP_INTERLACED:
      xprintf (this->xine, XINE_VERBOSITY_DEBUG,
               "video_out_directfb: deinterlacing set to %d.\n", value);
      this->deinterlace = value;
      break;
      
    case VO_PROP_ASPECT_RATIO:
      if (value >= XINE_VO_ASPECT_NUM_RATIOS)
        value = XINE_VO_ASPECT_NUM_RATIOS-1;
      xprintf (this->xine, XINE_VERBOSITY_DEBUG,
               "video_out_directfb: aspect ratio changed to %s.\n",
               _x_vo_scale_aspect_ratio_name (value));
      this->sc.user_ratio = value;
      _x_vo_scale_compute_ideal_size (&this->sc);
      this->sc.force_redraw = 1;
      break;
    
    case VO_PROP_HUE:
      if (this->caps & DLCAPS_HUE) {
        if (value > 0xffff)
          value = 0xffff;
        xprintf (this->xine, XINE_VERBOSITY_DEBUG,
                 "video_out_directfb: setting hue to %d.\n", value);
        this->cadj.flags = DCAF_HUE;
        this->cadj.hue   = value;
        this->layer->SetColorAdjustment (this->layer, &this->cadj);
      }
      break;
    
    case VO_PROP_SATURATION:
      if (this->caps & DLCAPS_SATURATION) {
        if (value > 0xffff)
          value = 0xffff;
        xprintf (this->xine, XINE_VERBOSITY_DEBUG,
                 "video_out_directfb: setting saturation to %d.\n", value);
        this->cadj.flags      = DCAF_SATURATION;
        this->cadj.saturation = value;
        this->layer->SetColorAdjustment (this->layer, &this->cadj);
      }
      break;
    
    case VO_PROP_CONTRAST:
      if (this->caps & DLCAPS_CONTRAST) {
        if (value > 0xffff)
          value = 0xffff;
        xprintf (this->xine, XINE_VERBOSITY_DEBUG,
                 "video_out_directfb: setting contrast to %d.\n", value);
        this->cadj.flags    = DCAF_CONTRAST;
        this->cadj.contrast = value;
        this->layer->SetColorAdjustment (this->layer, &this->cadj);
      }
      break;
    
    case VO_PROP_BRIGHTNESS:
      if (this->caps & DLCAPS_BRIGHTNESS) {
        if (value > 0xffff)
          value = 0xffff;
        xprintf (this->xine, XINE_VERBOSITY_DEBUG,
                 "video_out_directfb: setting brightness to %d.\n", value);
        this->cadj.flags      = DCAF_BRIGHTNESS;
        this->cadj.brightness = value;
        this->layer->SetColorAdjustment (this->layer, &this->cadj);
      }
      break;
    
    case VO_PROP_COLORKEY:
      if (this->caps & DLCAPS_DST_COLORKEY) {
        if (value > 0xffffff)
          value = 0xffffff;
        xprintf (this->xine, XINE_VERBOSITY_DEBUG,
                 "video_out_directfb: setting colorkey to 0x%06x.\n", value);
        this->colorkey = value;
        this->layer->SetDstColorKey (this->layer, (value & 0xff0000) >> 16,
                                                  (value & 0x00ff00) >>  8,
                                                  (value & 0x0000ff) >>  0);
        directfb_clean_output_area (this);
      }
      break;
    
    case VO_PROP_ZOOM_X:
      if (value >= XINE_VO_ZOOM_MIN && value <= XINE_VO_ZOOM_MAX) {
        xprintf (this->xine, XINE_VERBOSITY_DEBUG,
                 "video_out_directfb: Zoom X by factor %d.\n", value);
        this->sc.zoom_factor_x = (double)value / (double)XINE_VO_ZOOM_STEP;
        _x_vo_scale_compute_ideal_size (&this->sc);
        this->sc.force_redraw = 1;
      }
      break;
    
    case VO_PROP_ZOOM_Y:
      if (value >= XINE_VO_ZOOM_MIN && value <= XINE_VO_ZOOM_MAX) {
        xprintf (this->xine, XINE_VERBOSITY_DEBUG,
                 "video_out_directfb: Zoom Y by factor %d.\n", value);
        this->sc.zoom_factor_y = (double)value / (double)XINE_VO_ZOOM_STEP;
        _x_vo_scale_compute_ideal_size (&this->sc);
        this->sc.force_redraw = 1;
      }
      break;
    
    default:
      break;
  } 
      
  return value;
}

static void directfb_get_property_min_max (vo_driver_t *this_gen,
				                                   int property, int *min, int *max) {
  directfb_driver_t *this = (directfb_driver_t *) this_gen;
  
  switch (property) {
    case VO_PROP_INTERLACED:
      *min = 0;
      *max = 1;
      return;
      
    case VO_PROP_ASPECT_RATIO:
      *min = 0; 
      *max = XINE_VO_ASPECT_NUM_RATIOS-1;
      return;
    
    case VO_PROP_HUE:
      if (this->caps & DLCAPS_HUE) {
        *min = 0x0000;
        *max = 0xffff;
        return;
      }
      break;
    
    case VO_PROP_SATURATION:
      if (this->caps & DLCAPS_SATURATION) {
        *min = 0x0000;
        *max = 0xffff;
        return;
      }
      break;
    
    case VO_PROP_CONTRAST:
      if (this->caps & DLCAPS_CONTRAST) {
        *min = 0x0000;
        *max = 0xffff;
        return;
      }
      break;
    
    case VO_PROP_BRIGHTNESS:
      if (this->caps & DLCAPS_BRIGHTNESS) {
        *min = 0x0000;
        *max = 0xffff;
        return;
      }
      break;
    
    case VO_PROP_COLORKEY:
      if (this->caps & DLCAPS_DST_COLORKEY) {
        *min = 0x000000;
        *max = 0xffffff;
        return;
      }
      break;
    
    case VO_PROP_ZOOM_X:
    case VO_PROP_ZOOM_Y:
      *min = XINE_VO_ZOOM_MIN;
      *max = XINE_VO_ZOOM_MAX;
      return;
    
    default:
      break;
  }
               
  *min = 0;
  *max = 0;
}

static int directfb_gui_data_exchange (vo_driver_t *this_gen, 
                                       int data_type, void *data) {
  directfb_driver_t *this = (directfb_driver_t *) this_gen;
  
  switch (data_type) {
    case XINE_GUI_SEND_DRAWABLE_CHANGED:
#ifdef HAVE_X11
      if (this->visual_type == XINE_VISUAL_TYPE_X11) {
        this->drawable = (Drawable) data;
        XLockDisplay (this->display);
        XFreeGC (this->display, this->gc);
        this->gc = XCreateGC (this->display, this->drawable, 0, NULL);
        XUnlockDisplay (this->display);
      }
#endif
      return 0;
      
    case XINE_GUI_SEND_TRANSLATE_GUI_TO_VIDEO: {
      int x1, y1, x2, y2;
      x11_rectangle_t *rect = data;
      
      _x_vo_scale_translate_gui2video (&this->sc,
                                       rect->x, rect->y, 
                                       &x1, &y1);
      _x_vo_scale_translate_gui2video (&this->sc,
                                       rect->x + rect->w, rect->y + rect->h,
			                                 &x2, &y2);
      rect->x = x1;
      rect->y = y1;
      rect->w = x2-x1;
      rect->h = y2-y1;
    } return 0;
    
    default:
      break;
  }
  
  return -1;
}

static void directfb_dispose (vo_driver_t *this_gen) {
  directfb_driver_t *this = (directfb_driver_t *) this_gen;
 
  if (this) {
    int i;
    
    for (i = 0; i < VO_NUM_RECENT_FRAMES; i++) {
      directfb_frame_t *frame = this->recent_frames[i];
      if (frame)
        frame->vo_frame.free (&frame->vo_frame);
    }
        
#ifdef HAVE_X11
    if (this->visual_type == XINE_VISUAL_TYPE_X11) {
      XLockDisplay (this->display);
      XFreeGC (this->display, this->gc);
      XUnlockDisplay (this->display);
    }
#endif

    if (this->temp)
      this->temp->Release (this->temp);
    
    if (this->surface)
      this->surface->Release (this->surface);
    
    if (this->layer) {
      this->layer->SetColorAdjustment (this->layer, &this->default_cadj);
      this->layer->Release (this->layer);
    }
    
    if (this->dfb)
      this->dfb->Release (this->dfb);

    _x_alphablend_free (&this->alphablend_extra_data);

    free (this);
  }
}

/*** misc functions ****/

static DFBEnumerationResult find_overlay (DFBDisplayLayerID id,
                                          DFBDisplayLayerDescription dsc, void *ctx) {
  DFBDisplayLayerID *ret_id = (DFBDisplayLayerID *) ctx;
  
  if (dsc.caps & DLCAPS_SURFACE &&
      dsc.caps & DLCAPS_SCREEN_LOCATION) {
    *ret_id = id;
    return DFENUM_CANCEL;
  }
  
  return DFENUM_OK;
}

static DFBResult probe_device (directfb_driver_t *this, DFBDisplayLayerID id) {
  IDirectFBDisplayLayer      *layer;
  DFBDisplayLayerDescription  dsc;
  DFBDisplayLayerConfig       config;
  DFBResult                   ret;
    
  ret = this->dfb->GetDisplayLayer (this->dfb, id, &layer);
  if (ret != DFB_OK) {
    DirectFBError( "IDirectFB::GetDisplayLayer()", ret );
    return ret;
  }
 
  layer->SetCooperativeLevel (layer, DLSCL_EXCLUSIVE);
    
  /* hide */
  layer->SetOpacity (layer, 0x00);
    
  /* check if YUY2 is supported */
  config.flags       = DLCONF_PIXELFORMAT;
  config.pixelformat = DSPF_YUY2;
    
  ret = layer->TestConfiguration (layer, &config, NULL);
  if (ret != DFB_OK) {
    xprintf (this->xine, XINE_VERBOSITY_LOG, 
             "video_out_directfb: Display Layer #%d doesn't support YUY2.\n", id);
    layer->Release (layer);
    return DFB_UNSUPPORTED;
  }
    
  /* check if YV12 is supported */
  config.flags       = DLCONF_PIXELFORMAT;
  config.pixelformat = DSPF_YV12;
    
  ret = layer->TestConfiguration (layer, &config, NULL);
  if (ret != DFB_OK) {
    xprintf (this->xine, XINE_VERBOSITY_LOG, 
             "video_out_directfb: Display Layer #%d doesn't support YV12.\n", id);
    layer->Release (layer);
    return DFB_UNSUPPORTED;
  }
  
  layer->GetDescription (layer, &dsc);
  
  this->layer = layer;
  this->caps  = dsc.caps;

  xprintf (this->xine, XINE_VERBOSITY_LOG, 
           "video_out_directfb: using Display Layer #%d.\n", id);
           
  return DFB_OK;
}

static void update_config_cb (void *data, xine_cfg_entry_t *entry) {
  directfb_driver_t *this = (directfb_driver_t *) data;
  
  if (strcmp (entry->key, "video.device.directfb_buffermode") == 0) {
    DFBDisplayLayerConfig config = { .flags = DLCONF_BUFFERMODE };
    
    switch (entry->num_value) {
      case 0:
        config.buffermode = DLBM_FRONTONLY;
        break;
      case 2:
        config.buffermode = DLBM_TRIPLE;
        break;
      default:
        config.buffermode = DLBM_BACKVIDEO;
        break;
    }
    
    if (config.buffermode != this->config.buffermode) {      
      if (this->layer->SetConfiguration (this->layer, &config) != DFB_OK) {
        xprintf (this->xine, XINE_VERBOSITY_NONE,
                 "video_out_directfb: failed to set buffermode to %d!\n", 
                 entry->num_value);
        return;
      }
      
      this->layer->GetConfiguration (this->layer, &this->config);
    }
  }
  else if (strcmp (entry->key, "video.device.directfb_colorkeying") == 0) {
    DFBDisplayLayerConfig config = { .flags = DLCONF_OPTIONS };
       
    if (entry->num_value)
      config.options = this->config.options | DLOP_DST_COLORKEY;
    else
      config.options = this->config.options & ~DLOP_DST_COLORKEY;
    
    if (config.options != this->config.options) {  
      if (this->layer->SetConfiguration (this->layer, &config) != DFB_OK) {
          xprintf (this->xine, XINE_VERBOSITY_NONE,
                   "video_out_directfb: failed to set colorkeying to %d!\n", 
                   entry->num_value);
          return;
      }
    
      this->layer->GetConfiguration (this->layer, &this->config);
      directfb_clean_output_area (this);
    }
  }  
  else if (strcmp (entry->key, "video.device.directfb_colorkey") == 0) {
    this->colorkey = entry->num_value;
    this->layer->SetDstColorKey (this->layer, (this->colorkey & 0xff0000) >> 16,
                                              (this->colorkey & 0x00ff00) >>  8,
                                              (this->colorkey & 0x0000ff) >>  0);
    directfb_clean_output_area (this);
  }
  else if (strcmp (entry->key, "video.device.directfb_flicker_filtering") == 0) {
    DFBDisplayLayerConfig config = { .flags = DLCONF_OPTIONS };
    
    if (entry->num_value)
      config.options = this->config.options | DLOP_FLICKER_FILTERING;
    else
      config.options = this->config.options & ~DLOP_FLICKER_FILTERING;
      
    if (config.options != this->config.options) {
      if (this->layer->SetConfiguration (this->layer, &config) != DFB_OK) {
          xprintf (this->xine, XINE_VERBOSITY_NONE,
                   "video_out_directfb: failed to set flicker_filtering to %d!\n", 
                   entry->num_value);
          return;
      }
    
      this->layer->GetConfiguration (this->layer, &this->config);
    }
  }
  else if (strcmp (entry->key, "video.device.directfb_field_parity") == 0) {
    DFBDisplayLayerConfig config = { .flags = DLCONF_OPTIONS };
    
    if (entry->num_value)
      config.options = this->config.options | DLOP_FIELD_PARITY;
    else
      config.options = this->config.options & ~DLOP_FIELD_PARITY;
      
    if (config.options != this->config.options) {
      if (this->layer->SetConfiguration (this->layer, &config) != DFB_OK) {
          xprintf (this->xine, XINE_VERBOSITY_NONE,
                   "video_out_directfb: failed to set field_parity to %d!\n", 
                   entry->num_value);
          return;
      }
    
      this->layer->GetConfiguration (this->layer, &this->config);
    } 
  }
  else if (strcmp (entry->key, "video.device.directfb_vsync") == 0) {
    this->vsync = entry->num_value;
  }
}
   
static void init_config (directfb_driver_t *this) {
  config_values_t   *config             = this->xine->config;
  static const char *buffermode_enum[]  = {"single", "double", "triple", 0};
  static const char *fieldparity_enum[] = {"none", "top", "bottom", 0};
  
  this->buffermode = config->register_enum (config,
      "video.device.directfb_buffermode", this->buffermode, (char**)buffermode_enum,
      _("video layer buffering mode"),
      _("Select the buffering mode of the output layer. "
        "Double or triple buffering give a smoother playback, "
        "but consume more video memory."),
      10, update_config_cb, (void *)this);
  
  this->vsync      = config->register_bool (config,
      "video.device.directfb_vsync", this->vsync,
      _("wait for vertical retrace"),
      _("Enable synchronizing the update of the video image to the "
		    "repainting of the entire screen (\"vertical retrace\")."),
		  10, update_config_cb, (void *)this);
  
  if (this->caps & DLCAPS_DST_COLORKEY) {
    this->colorkeying = config->register_bool  (config,
          "video.device.directfb_colorkeying", this->colorkeying,
          _("enable video color key"),
          _("Enable using a color key to tell the graphics card "
            "where to overlay the video image."),
          20, update_config_cb, (void *)this);
    
    this->colorkey    = config->register_range (config,
          "video.device.directfb_colorkey", this->colorkey, 0, 0xffffff,
          _("video color key"),
          _("The color key is used to tell the graphics card "
            "where to overlay the video image. Try different values, "
            "if you experience windows becoming transparent."),
          10, update_config_cb, (void  *)this);
  }
  
  if (this->caps & DLCAPS_FLICKER_FILTERING) {
    this->flicker_filtering = config->register_bool( config,
          "video.device.directfb_flicker_filtering", this->flicker_filtering,
          _("flicker filtering"),
          _("Enable Flicker Filetring for a smooth output on an interlaced display."),
          10, update_config_cb, (void *)this);
  }
  
  if (this->caps & DLCAPS_FIELD_PARITY) {
    this->field_parity  = config->register_enum( config,
          "video.device.directfb_field_parity", this->field_parity, (char**)fieldparity_enum,
          _("field parity"),
          _("For an interlaced display, enable controlling "
            "the field parity (\"none\"=disabled)."),
          10, update_config_cb, (void *)this);
  }
} 

static DFBResult init_device (directfb_driver_t *this) {
  IDirectFBSurface           *surface;
  DFBDisplayLayerConfig       config;
  DFBDisplayLayerConfigFlags  failed = 0;
  DFBResult                   ret;
   
  /* get current color cadjustment */
  this->layer->GetColorAdjustment (this->layer, &this->default_cadj);
  this->cadj = this->default_cadj;
  
  /* set layer configuration */
  config.flags       = DLCONF_BUFFERMODE | DLCONF_PIXELFORMAT | DLCONF_OPTIONS;
  config.pixelformat = DSPF_YV12;
  config.options     = DLOP_NONE;
  
  switch (this->buffermode) {
    case 0:
      config.buffermode = DLBM_FRONTONLY;
      break;
    case 2:
      config.buffermode = DLBM_TRIPLE;
      break;
    default:
      config.buffermode = DLBM_BACKVIDEO;
      break;
  }
  
  if (this->colorkeying)
    config.options |= DLOP_DST_COLORKEY;
    
  if (this->flicker_filtering)
    config.options |= DLOP_FLICKER_FILTERING;
    
  if (this->field_parity)
    config.options |= DLOP_FIELD_PARITY;
  
  /* test current configuration */
  ret = this->layer->TestConfiguration (this->layer, &config, &failed);
  if (failed & DLCONF_BUFFERMODE) {
    xprintf (this->xine, XINE_VERBOSITY_LOG,
             "video_out_directfb: layer doesn't support buffermode %d!\n",
             this->buffermode);   
    config.flags &= ~DLCONF_BUFFERMODE;
  }
  if (failed & DLCONF_OPTIONS) {
    xprintf (this->xine, XINE_VERBOSITY_LOG,
             "video_out_directfb: layer doesn't support options 0x%08x!\n",
             config.options);
    config.flags &= ~DLCONF_OPTIONS;
  }
  
  ret = this->layer->SetConfiguration (this->layer, &config);
  /* this should never happen */
  if (ret != DFB_OK) {
    DirectFBError ("IDirectFBDisplayLayer::SetConfiguration()", ret);
    return ret;
  }

  this->layer->GetConfiguration (this->layer, &this->config); 
  
  if (this->caps & DLCAPS_DST_COLORKEY) {
    this->layer->SetDstColorKey (this->layer, (this->colorkey & 0xff0000) >> 16,
                                              (this->colorkey & 0x00ff00) >>  8,
                                              (this->colorkey & 0x0000ff) >>  0);
  }
  
  if (this->field_parity)
    this->layer->SetFieldParity (this->layer, this->field_parity-1);
  
  /* retrieve layer's surface */
  ret = this->layer->GetSurface (this->layer, &surface);
  if (ret != DFB_OK) {
    DirectFBError ("IDirectFBDisplayLayer::GetSurface()", ret);
    return ret;
  }
  
  /* clear surface buffers */
  surface->Clear (surface, 0x00, 0x00, 0x00, 0xff);
  surface->Flip  (surface, NULL, 0);
  surface->Clear (surface, 0x00, 0x00, 0x00, 0xff);
  surface->Flip  (surface, NULL, 0);
  surface->Clear (surface, 0x00, 0x00, 0x00, 0xff);
  surface->Flip  (surface, NULL, 0);
  
  this->surface = surface;
  
  /* check if stretchblit is hardware accelerated */
  if (!(this->caps & DLCAPS_SCREEN_LOCATION)) {
    IDirectFBSurface      *temp;
    DFBSurfaceDescription  dsc;
    DFBAccelerationMask    mask = DFXL_NONE;
    
    dsc.flags       = DSDESC_CAPS   | DSDESC_WIDTH      |
                      DSDESC_HEIGHT | DSDESC_PIXELFORMAT;
    dsc.caps        = DSCAPS_VIDEOONLY;
    dsc.width       = 352;
    dsc.height      = 240;
    dsc.pixelformat = DSPF_YV12;
    
    if (this->dfb->CreateSurface (this->dfb, &dsc, &temp) == DFB_OK) {
      this->surface->GetAccelerationMask (this->surface, temp, &mask);
      
      if (mask & DFXL_STRETCHBLIT) {
        xprintf (this->xine, XINE_VERBOSITY_LOG,
                 _("video_out_directfb: "
                   "using hardware accelerated image scaling.\n"));
        this->temp = temp;
        
        /* check if stretchblit with deinterlacing is supported */
        this->surface->SetBlittingFlags (this->surface, DSBLIT_DEINTERLACE);
        this->surface->GetAccelerationMask (this->surface, temp, &mask);
        this->surface->SetBlittingFlags (this->surface, DSBLIT_NOFX);
        
        this->hw_deinterlace = (mask & DFXL_STRETCHBLIT) ? 1 : 0;
        if (this->hw_deinterlace) {
          xprintf (this->xine, XINE_VERBOSITY_LOG,
                  _("video_out_directfb: "
                    "image scaling with deinterlacing is hardware accelerated.\n"));
        }
        
        /* used to decide reallocation */
        this->temp_frame_width  = 352;
        this->temp_frame_height = 240;
        this->temp_frame_format = DSPF_YV12;
      } 
      else
        temp->Release (temp);
    }
  }
  
  return DFB_OK;
}    

static void get_screen_size (directfb_driver_t *this, int *ret_w, int *ret_h) {
  int w = -640, h = -480;
#ifdef HAVE_FB
  int fd;
  struct fb_var_screeninfo var;
  
  fd = open (getenv ("FRAMEBUFFER") ? : "/dev/fb0", O_RDONLY);
  if (fd > 0) {
    if (ioctl (fd, FBIOGET_VSCREENINFO, &var) == 0) {
      w = var.xres;
      h = var.yres;
    }
    close (fd);
  }
#endif
  if (w < 1 || h < 1) {
    IDirectFBDisplayLayer *primary;
    DFBDisplayLayerConfig  config;
    DFBResult              ret;
  
    ret = this->dfb->GetDisplayLayer (this->dfb, DLID_PRIMARY, &primary);
    if (ret == DFB_OK) {
      if (primary->GetConfiguration (primary, &config) == DFB_OK) {
        w = config.width;
        h = config.height;
      }
      primary->Release (primary);
    } 
    else
      DirectFBError( "IDirectFB::GetDisplayLayer( DLID_PRIMARY )", ret );
  }

  xprintf (this->xine, XINE_VERBOSITY_DEBUG,
           "video_out_directfb: screen size is %dx%d.\n", abs(w), abs(h));
  *ret_w = abs(w); *ret_h = abs(h);
}

static void directfb_frame_output_cb (void *user_data, int video_width, int video_height,
                                      double video_pixel_aspect, int *dest_x, int *dest_y,
                                      int *dest_width, int *dest_height,
                                      double *dest_pixel_aspect, int *win_x, int *win_y) {
  directfb_driver_t *this = (directfb_driver_t *) user_data;

  *dest_x            = 0;
  *dest_y            = 0;
  *dest_width        = this->screen_width;
  *dest_height       = this->screen_height;
  *dest_pixel_aspect = video_pixel_aspect ? : 1.0;
  *win_x             = 0;
  *win_y             = 0;
}

/*** DirectFB plugin functions ***/ 

static vo_driver_t *open_plugin_fb (video_driver_class_t *class_gen, const void *visual_gen) {
  directfb_class_t  *class  = (directfb_class_t *) class_gen;
  directfb_driver_t *this;
  fb_visual_t       *visual = (fb_visual_t *) visual_gen;
  config_values_t   *config = class->xine->config;
  IDirectFBScreen   *screen;
  DFBDisplayLayerID  id     = DLID_PRIMARY;
  DFBResult          ret;

  this = xine_xmalloc (sizeof (directfb_driver_t));
  if (!this)
    return NULL;
  
  this->visual_type = XINE_VISUAL_TYPE_FB;
  this->xine        = class->xine;
  
  /* initialize DirectFB */ 
  ret = DirectFBInit (NULL, NULL);
  if (ret != DFB_OK) {
    DirectFBError ("DirectFBInit()", ret);
    free (this);
    return NULL;
  }
  
  DirectFBSetOption ("bg-none", NULL);
  DirectFBSetOption ("no-vt"  , NULL);
  /* linux_input blocks input from console, disable it */
  DirectFBSetOption ("disable-module", "linux_input");
  
  /* create the main interface or retrieve an already existing one */
  ret = DirectFBCreate (&this->dfb);
  if (ret != DFB_OK) {
    DirectFBError ("DirectFBCreate()", ret);
    free (this);
    return NULL;
  }
  
  /* retrieve an interface to the current screen */
  ret = this->dfb->GetScreen (this->dfb, DSCID_PRIMARY, &screen);
  if (ret != DFB_OK) {
    DirectFBError ("IDirectFB::GetScreen( DSCID_PRIMARY )", ret);
    this->dfb->Release (this->dfb);
    free (this);
    return NULL;
  }
  
  /* find an overlay layer on the current screen */
  ret = screen->EnumDisplayLayers (screen, find_overlay, (void*)&id);
  screen->Release (screen);
  if (ret != DFB_OK) {
    DirectFBError( "IDirectFBScreen::EnumDisplayLayers()", ret);
    this->dfb->Release (this->dfb);
    free (this);
    return NULL;
  }
 
  /* allow user/application to select a different layer */
  id = config->register_num (config,
                             "video.device.directfb_layer_id", id,
                             _("video layer id"),
                             _("Select the video output layer by its id."),
                             20, NULL, 0);
                             
  if (probe_device (this, id) != DFB_OK) {
    xprintf (this->xine, XINE_VERBOSITY_NONE,
             _("video_out_directfb: no usable output layer was found!\n"));
    this->dfb->Release (this->dfb);
    free (this);
    return NULL;
  }

  /* debugging option */
  if (this->caps & DLCAPS_SCREEN_LOCATION) {
    if (config->register_bool (config, "video.device.directfb_fixed_overlay", 0,
                _("make the overlay behave like a fixed layer (for debugging)"),
                _("make the overlay behave like a fixed layer (for debugging)"),
                                                                  100, NULL, 0)) {
      this->caps &= ~DLCAPS_SCREEN_LOCATION;
    }
  }
  
  /* set default configuration */
  this->buffermode        = 1; // double
  this->vsync             = 0;
  this->colorkeying       = 0;
  this->colorkey          = DEFAULT_COLORKEY;
  this->flicker_filtering = 0;
  this->field_parity      = 0;
  
  /* get user configuration */
  init_config (this);

  /* set layer configuration */
  if (init_device (this) != DFB_OK) {
    this->layer->Release (this->layer);
    this->dfb->Release (this->dfb);
    free (this);
    return NULL;
  }
  
  if (!(this->caps & DLCAPS_SCREEN_LOCATION)) {
    this->screen_width  = this->config.width;
    this->screen_height = this->config.height;
  } else
    get_screen_size (this, &this->screen_width, &this->screen_height );

  _x_alphablend_init (&this->alphablend_extra_data, this->xine);
  
  _x_vo_scale_init (&this->sc, 1, 0, this->xine->config);
  this->sc.user_ratio = XINE_VO_ASPECT_AUTO;
  this->sc.gui_width  = this->screen_width;
  this->sc.gui_height = this->screen_height; 
  
  if (visual) {
    this->sc.frame_output_cb = visual->frame_output_cb;
    this->sc.user_data       = visual->user_data;
  } else {
    this->sc.frame_output_cb = directfb_frame_output_cb;
    this->sc.user_data       = (void *) this;
  }

  this->vo_driver.get_capabilities     = directfb_get_capabilities;
  this->vo_driver.alloc_frame          = directfb_alloc_frame;
  this->vo_driver.update_frame_format  = directfb_update_frame_format;
  this->vo_driver.overlay_begin        = NULL; /* not used */
  this->vo_driver.overlay_blend        = directfb_overlay_blend;
  this->vo_driver.overlay_end          = NULL; /* not used */
  this->vo_driver.display_frame        = (this->caps & DLCAPS_SCREEN_LOCATION)
                                         ? directfb_display_frame
                                         : directfb_display_frame2;
  this->vo_driver.get_property         = directfb_get_property;
  this->vo_driver.set_property         = directfb_set_property;
  this->vo_driver.get_property_min_max = directfb_get_property_min_max;
  this->vo_driver.gui_data_exchange    = directfb_gui_data_exchange;
  this->vo_driver.redraw_needed        = directfb_redraw_needed;
  this->vo_driver.dispose              = directfb_dispose;

  return &this->vo_driver;
}

static char* get_identifier_fb (video_driver_class_t *this_gen) {
  return "DirectFB";
}

static char* get_description_fb (video_driver_class_t *this_gen) {
  return _("xine video output plugin using DirectFB.");
}

static void dispose_class_fb (video_driver_class_t *this_gen) {
  directfb_class_t *this = (directfb_class_t *) this_gen;

  free (this);
}

static void *init_class_fb (xine_t *xine, void *visual_gen) {
  directfb_class_t *this;
  const char       *error;
  
  /* check DirectFB version */
  error = DirectFBCheckVersion( DIRECTFB_MAJOR_VERSION,
                                DIRECTFB_MINOR_VERSION,
                                DIRECTFB_MICRO_VERSION );
  if (error) {
    xprintf (xine, XINE_VERBOSITY_NONE,
             "video_out_directfb: %s!\n", error);
    return NULL;
  }

  this = (directfb_class_t *) xine_xmalloc (sizeof (directfb_class_t));
  this->driver_class.open_plugin     = open_plugin_fb;
  this->driver_class.get_identifier  = get_identifier_fb;
  this->driver_class.get_description = get_description_fb;
  this->driver_class.dispose         = dispose_class_fb;

  this->xine = xine;

  return this;
}

static vo_info_t vo_info_directfb_fb = {
  8,                   /* priority    */
  XINE_VISUAL_TYPE_FB  /* visual type */
};

/*** XDirectFB plugin functions ****/

#ifdef HAVE_X11
static vo_driver_t *open_plugin_x11 (video_driver_class_t *class_gen, const void *visual_gen) {
  directfb_class_t  *class  = (directfb_class_t *) class_gen;
  directfb_driver_t *this;
  x11_visual_t      *visual = (x11_visual_t *) visual_gen;
  XWindowAttributes  attrs;
  IDirectFBScreen   *screen;
  DFBDisplayLayerID  id     = (DFBDisplayLayerID) -1;
  DFBResult          ret;

  this = xine_xmalloc (sizeof (directfb_driver_t));
  if (!this)
    return NULL;
  
  this->visual_type = XINE_VISUAL_TYPE_X11;
  this->xine        = class->xine;
  
  /* initialize DirectFB */ 
  ret = DirectFBInit (NULL, NULL);
  if (ret != DFB_OK) {
    DirectFBError ("DirectFBInit()", ret);
    free (this);
    return NULL;
  }
  
  /* create the main interface or retrieve an already existing one */
  ret = DirectFBCreate (&this->dfb);
  if (ret != DFB_OK) {
    DirectFBError ("DirectFBCreate()", ret);
    free (this);
    return NULL;
  }
  
  /* retrieve an interface to the current screen */
  ret = this->dfb->GetScreen (this->dfb, DSCID_PRIMARY, &screen);
  if (ret != DFB_OK) {
    DirectFBError ("IDirectFB::GetScreen( DSCID_PRIMARY )", ret);
    this->dfb->Release (this->dfb);
    free (this);
    return NULL;
  }
  
  /* find an overlay layer on the current screen */
  ret = screen->EnumDisplayLayers (screen, find_overlay, (void*)&id);
  screen->Release (screen);
  if (ret != DFB_OK) {
    DirectFBError( "IDirectFBScreen::EnumDisplayLayers()", ret);
    this->dfb->Release (this->dfb);
    free (this);
    return NULL;
  }
                             
  if (probe_device (this, id) != DFB_OK) {
    xprintf (this->xine, XINE_VERBOSITY_NONE,
             _("video_out_directfb: no usable overlay layer was found!\n"));
    this->dfb->Release (this->dfb);
    free (this);
    return NULL;
  }
  
  /* set default configuration */
  this->buffermode        = 1; // double
  this->vsync             = 0;
  this->colorkeying       = (this->caps & DLCAPS_DST_COLORKEY) ? 1 : 0;
  this->colorkey          = DEFAULT_COLORKEY;
  this->flicker_filtering = 0;
  this->field_parity      = 0;
  
  /* get user configuration */
  init_config (this);

  /* set layer configuration */
  if (init_device (this) != DFB_OK) {
    this->layer->Release (this->layer);
    this->dfb->Release (this->dfb);
    free (this);
    return NULL;
  }
  
  this->display  = visual->display;
  this->screen   = visual->screen;
  this->drawable = visual->d;
  this->gc       = XCreateGC (this->display, this->drawable, 0, NULL);
  
  XGetWindowAttributes (this->display, this->drawable, &attrs);
  this->depth = attrs.depth;

  _x_alphablend_init (&this->alphablend_extra_data, this->xine);
  
  _x_vo_scale_init (&this->sc, 0, 0, this->xine->config);
  this->sc.user_ratio      = XINE_VO_ASPECT_AUTO;
  this->sc.gui_width       = attrs.width;
  this->sc.gui_height      = attrs.height;
  this->sc.frame_output_cb = visual->frame_output_cb;
  this->sc.user_data       = visual->user_data;

  this->vo_driver.get_capabilities     = directfb_get_capabilities;
  this->vo_driver.alloc_frame          = directfb_alloc_frame;
  this->vo_driver.update_frame_format  = directfb_update_frame_format;
  this->vo_driver.overlay_begin        = NULL; /* not used */
  this->vo_driver.overlay_blend        = directfb_overlay_blend;
  this->vo_driver.overlay_end          = NULL; /* not used */
  this->vo_driver.display_frame        = directfb_display_frame;
  this->vo_driver.get_property         = directfb_get_property;
  this->vo_driver.set_property         = directfb_set_property;
  this->vo_driver.get_property_min_max = directfb_get_property_min_max;
  this->vo_driver.gui_data_exchange    = directfb_gui_data_exchange;
  this->vo_driver.redraw_needed        = directfb_redraw_needed;
  this->vo_driver.dispose              = directfb_dispose;

  return &this->vo_driver;
}

static char* get_identifier_x11 (video_driver_class_t *this_gen) {
  return "XDirectFB";
}

static char* get_description_x11 (video_driver_class_t *this_gen) {
  return _("xine video output plugin using DirectFB under XDirectFB.");
}

static void dispose_class_x11 (video_driver_class_t *this_gen) {
  directfb_class_t *this = (directfb_class_t *) this_gen;

  free (this);
}

static void *init_class_x11 (xine_t *xine, void *visual_gen) {
  directfb_class_t *this;
  x11_visual_t     *visual = (x11_visual_t *) visual_gen;
  const char       *error;
  
  /* check DirectFB version */
  error = DirectFBCheckVersion( DIRECTFB_MAJOR_VERSION,
                                DIRECTFB_MINOR_VERSION,
                                DIRECTFB_MICRO_VERSION );
  if (error) {
    xprintf (xine, XINE_VERBOSITY_NONE,
             "video_out_directfb: %s!\n", error);
    return NULL;
  }
  
  if (!visual) {
    xprintf (xine, XINE_VERBOSITY_DEBUG,
             "video_out_directfb: x11 visual is required!\n");
    return NULL;
  }
  
  /* check if we are running under XDirectFB */
  if (strcmp (XServerVendor (visual->display), "Denis Oliver Kropp"))
    return NULL;

  this = (directfb_class_t *) xine_xmalloc (sizeof (directfb_class_t));
  this->driver_class.open_plugin     = open_plugin_x11;
  this->driver_class.get_identifier  = get_identifier_x11;
  this->driver_class.get_description = get_description_x11;
  this->driver_class.dispose         = dispose_class_x11;

  this->xine = xine;

  return this;
}

static vo_info_t vo_info_directfb_x11 = {
  8,                    /* priority    */
  XINE_VISUAL_TYPE_X11  /* visual type */
};
#endif /* HAVE_X11 */

/*********/

plugin_info_t xine_plugin_info[] = {
  /* type, API, "name", version, special_info, init_function */
  { PLUGIN_VIDEO_OUT, VIDEO_OUT_DRIVER_IFACE_VERSION, "DirectFB",
    XINE_VERSION_CODE, &vo_info_directfb_fb, init_class_fb },
#ifdef HAVE_X11
  { PLUGIN_VIDEO_OUT, VIDEO_OUT_DRIVER_IFACE_VERSION, "XDirectFB",
    XINE_VERSION_CODE, &vo_info_directfb_x11, init_class_x11 },
#endif
  { PLUGIN_NONE, 0, "", 0, NULL, NULL }
};