--- video_out_directfb.xine-lib-1.1.6.c	2007-05-25 06:31:13.000000000 +0300
+++ video_out_directfb.c	2007-05-25 06:30:06.000000000 +0300
@@ -56,6 +56,9 @@
                                                DIRECTFB_MINOR_VERSION,\
                                                DIRECTFB_MICRO_VERSION )
 
+#define COLORKEY_OSD  1
+/*#define DBG(x...)*/
+#define DBG(x...) fprintf(stderr, "directfb: " x);
 
 typedef struct directfb_frame_s {
   vo_frame_t                   vo_frame;
@@ -97,6 +100,9 @@
   /* Subpicture layer */
   IDirectFBDisplayLayer       *spic_layer;
   IDirectFBSurface            *spic_surface;
+#ifdef COLORKEY_OSD
+  IDirectFBSurface            *tmp_surface;
+#endif
   
   /* for hardware scaling */
   IDirectFBSurface            *temp;
@@ -376,6 +382,31 @@
     }
 #endif
   }
+#ifdef COLORKEY_OSD
+  else if (this->config.options & DLOP_DST_COLORKEY) {
+# if 0
+    DFBSurfaceCapabilities caps;
+    int                    bufs = 1;
+    int                    i; 
+
+    this->surface->GetCapabilities (this->surface, &caps);
+    if (caps & DSCAPS_DOUBLE)
+      bufs++;
+    if (caps & DSCAPS_TRIPLE)
+      bufs++;
+
+    for (i = 0; i < bufs; i++) {  
+      this->spic_surface->Clear (this->spic_surface, 
+				 (this->colorkey&0xff0000)>>16, 
+				 (this->colorkey&0x00ff00)>>8,
+				 (this->colorkey&0x0000ff),
+				 0xff);
+      this->spic_surface->Flip (this->spic_surface, NULL, 0);
+    }
+# endif
+  }      
+#endif
+
   else if (!(this->caps & DLCAPS_SCREEN_LOCATION)) {
     DFBRectangle           rect[4];
     DFBSurfaceCapabilities caps;
@@ -417,6 +452,31 @@
       UNLOCK_DISPLAY();
     }
 #endif
+#ifdef COLORKEY_OSD
+    if (this->colorkeying) {
+      DFBResult             ret;
+      DFBSurfaceDescription sdsc;
+      DFBDisplayLayerConfig config;	
+      /*DBG("overlay_begin: Clearing spic layer with colorkey 0x%06x\n", this->colorkey);*/
+      this->tmp_surface = this->spic_surface;
+      this->spic_surface = NULL;
+      config.flags = DLCONF_WIDTH | DLCONF_HEIGHT | DLCONF_PIXELFORMAT; 
+      this->underlay->GetConfiguration (this->underlay, &config);
+      sdsc.flags = DSDESC_WIDTH | DSDESC_HEIGHT | DSDESC_PIXELFORMAT;
+      sdsc.pixelformat = config.pixelformat;
+      sdsc.width = config.width;
+      sdsc.height = config.height;
+      ret = this->dfb->CreateSurface(this->dfb, &sdsc, &this->spic_surface);
+      if(ret != DFB_OK) {
+	DirectFBError( "IDirectFB::CreateSurface(dfbosd.overlay)", ret );
+      }
+      this->spic_surface->Clear (this->spic_surface,
+				 (this->colorkey&0xff0000)>>16, 
+				 (this->colorkey&0x00ff00)>>8,
+				 (this->colorkey&0x0000ff),
+				 0xff);
+    } else
+#endif
     if (this->spic_surface) {
       lprintf ("clearing subpicture.\n");
       this->spic_surface->SetClip (this->spic_surface, NULL);
@@ -449,6 +509,10 @@
   clip.y1 = yoffset;
   clip.x2 = xoffset + overlay->width  - 1;
   clip.y2 = yoffset + overlay->height - 1;
+
+#ifdef COLORKEY_OSD
+  if (!this->colorkeying)
+#endif
   this->spic_surface->SetClip (this->spic_surface, &clip);
   
   for (x = 0, y= 0, i = 0; i < overlay->num_rle; i++) {
@@ -593,6 +657,15 @@
       UNLOCK_DISPLAY();
     }
 #endif
+#ifdef COLORKEY_OSD
+    if (this->colorkeying) {
+      this->tmp_surface->Blit(this->tmp_surface, this->spic_surface, NULL, 0, 0);
+      this->tmp_surface->Flip(this->tmp_surface, NULL, DSFLIP_ONSYNC);
+      this->spic_surface->Release(this->spic_surface);
+      this->spic_surface = this->tmp_surface;
+      this->tmp_surface = NULL;
+    } else
+#endif
     if (this->spic_surface) {
       lprintf ("flipping subpicture.\n");
       this->spic_surface->SetClip (this->spic_surface, NULL);
@@ -1500,6 +1573,60 @@
     }
   }
 #endif          
+#ifdef COLORKEY_OSD
+  /* fallback: subpicture support using colorkeying */
+  if (!this->spic_layer) {
+    fprintf(stderr, "directfb: no hardware subpicture available\n");
+    if(this->colorkeying) {
+      DBG("colorkeying enabled in configuration\n");
+      if(!(this->caps & DLCAPS_DST_COLORKEY))
+	fprintf(stderr, "directfb: Hardware does not support colorkeying\n");
+    } else 
+      fprintf(stderr, "directfb: colorkeying disabled in configuration\n");
+
+    if(this->colorkeying && (this->caps & DLCAPS_DST_COLORKEY)) {
+      fprintf(stderr, "directfb: Hardware supports colorkeying\n");
+      if (! this->underlay) 
+	fprintf(stderr, "directfb: Underlay NOT available\n");
+      if (this->underlay) {
+	DFBDisplayLayerConfig config;
+
+	DBG("Underlay available\n");
+	config.flags = DLCONF_WIDTH | DLCONF_HEIGHT | DLCONF_PIXELFORMAT | DLCONF_OPTIONS;
+	ret = this->underlay->GetConfiguration (this->underlay, &config);
+	if (ret == DFB_OK) {
+	  DBG("Screen size %dx%d, underlay surface size %dx%d, pixelformat 0x%x, colorkey 0x%08x\n",
+	      config.width, config.height,
+	      this->screen_width, this->screen_height,
+	      config.pixelformat, this->colorkey);
+	}
+	this->underlay->SetCooperativeLevel (this->underlay, 
+					     DLSCL_EXCLUSIVE);
+	config.flags = DLCONF_PIXELFORMAT; 
+	//config.flags = DLCONF_WIDTH | DLCONF_HEIGHT | DLCONF_PIXELFORMAT; 
+	//config.width = 720;
+	//config.height = 576;
+	config.pixelformat = DSPF_RGB32;
+	if ((ret=this->underlay->SetConfiguration (this->underlay, &config)) == DFB_OK) {
+	  this->underlay->GetConfiguration (this->underlay, &config);
+	  DBG("DFB GetConfiguration: underlay is now %dx%d, pixelformat %d\n", 
+	      config.width, config.height, config.pixelformat);
+	} else {
+	  DirectFBError ("IDirectFB::SetConfiguration()", ret);
+	  fprintf(stderr, "DFB SetConfiguration for underlay FAILED\n");
+	}
+	
+	this->layer->SetDstColorKey (this->layer,
+				     (this->colorkey & 0xff0000) >> 16,
+				     (this->colorkey & 0x00ff00) >>  8,
+				     (this->colorkey & 0x0000ff) >>  0);
+
+        this->spic_layer = this->underlay;
+      }
+    }
+  }
+#endif
+
   if (this->spic_layer) {
     ret = this->spic_layer->GetSurface (this->spic_layer, &this->spic_surface);
     if (ret) {
@@ -1511,6 +1638,18 @@
     
     this->spic_surface->Clear (this->spic_surface, 0, 0, 0, 0);
     this->spic_surface->Flip (this->spic_surface, NULL, 0);
+
+#ifdef COLORKEY_OSD
+    if(this->colorkeying) {
+      DBG("Clearing spic layer with colorkey 0x%06x\n", this->colorkey);
+      this->spic_surface->Clear (this->spic_surface,
+				 (this->colorkey&0xff0000)>>16, 
+				 (this->colorkey&0x00ff00)>>8,
+				 (this->colorkey&0x0000ff),
+				 0xff);
+      this->spic_surface->Flip (this->spic_surface, NULL, 0);
+    }
+#endif
     
     xprintf (this->xine, XINE_VERBOSITY_LOG,
            _("video_out_directfb: using hardware subpicture acceleration.\n"));
@@ -1830,8 +1969,17 @@
   this->flicker_filtering = 0;
   this->field_parity      = 0;
   
+#if 0
+  /* test */
+  this->colorkeying = 1;
+  this->colorkey    = 0x202060;
+#endif
   /* get user configuration */
   init_config (this);
+#ifdef COLORKEY_OSD
+  DBG("colorkeying=%s colorkey=0x%06x CAP=%d\n", this->colorkeying?"on":"off", this->colorkey, 
+      (this->caps & DLCAPS_DST_COLORKEY));
+#endif
 
   /* initialize video layer */
   if (init_device (this) != DFB_OK) {
