790d0d928f5cef3e1d0f422f6810f5155651eec9
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / ext / directfb / dfbvideosink.c
1 /* GStreamer DirectFB plugin
2  * Copyright (C) 2005 Julien MOUTTE <julien@moutte.net>
3  * Copyright (C) 2013 Kazunori Kobayashi <kkobayas@igel.co.jp>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 /**
22  * SECTION:element-dfbvideosink
23  * @title: dfbvideosink
24  *
25  * DfbVideoSink renders video frames using the
26  * [DirectFB](http://www.directfb.org/) library.
27  * Rendering can happen in two different modes :
28  *
29  * * Standalone: this mode will take complete control of the monitor forcing
30  *   DirectFB to fullscreen layout.
31  *
32  *   This is convenient to test using the  gst-launch-1.0 command line tool or
33  *   other simple applications. It is possible to interrupt playback while
34  *   being in this mode by pressing the Escape key.
35  *   This mode handles navigation events for every input device supported by
36  *   the DirectFB library, it will look for available video modes in the fb.modes
37  *   file and try to switch the framebuffer video mode to the most suitable one.
38  *   Depending on hardware acceleration capabilities the element will handle
39  *   scaling or not.
40  *
41  *   If no acceleration is available it will do clipping or centering of the
42  *   video frames respecting the original aspect ratio.
43  *
44  * * Embedded: this mode will render video frames in a
45  *   #GstDfbVideoSink:surface provided by the
46  *   application developer. This is a more advanced usage of the element and
47  *   it is required to integrate video playback in existing
48  *   DirectFB applications.
49  *
50  *   When using this mode the element just renders to the
51  *   #GstDfbVideoSink:surface provided by the
52  *   application, that means it won't handle navigation events and won't resize
53  *   the #GstDfbVideoSink:surface to fit video
54  *   frames geometry. Application has to implement the necessary code to grab
55  *   information about the negotiated geometry and resize there
56  *   #GstDfbVideoSink:surface accordingly.
57  *
58  * For both modes the element implements a buffer pool allocation system to
59  * optimize memory allocation time and handle reverse negotiation. Indeed if
60  * you insert an element like videoscale in the pipeline the video sink will
61  * negotiate with it to try get a scaled video for either the fullscreen layout
62  * or the application provided external #GstDfbVideoSink:surface.
63  *
64  * ## Example application
65  *
66  * <include xmlns="http://www.w3.org/2003/XInclude" href="element-dfb-example.xml" />
67  *
68  * ## Example pipelines
69  * |[
70  * gst-launch-1.0 -v videotestsrc ! dfbvideosink hue=20000 saturation=40000 brightness=25000
71  * ]| test the colorbalance interface implementation in dfbvideosink
72  */
73
74 #ifdef HAVE_CONFIG_H
75 #include "config.h"
76 #endif
77
78 #include <gst/video/video.h>
79
80 /* Object header */
81 #include "dfbvideosink.h"
82
83 #include <string.h>
84 #include <stdlib.h>
85
86 /* Debugging category */
87 GST_DEBUG_CATEGORY_STATIC (dfbvideosink_debug);
88 #define GST_CAT_DEFAULT dfbvideosink_debug
89
90 /* Default template */
91 static GstStaticPadTemplate gst_dfbvideosink_sink_template_factory =
92 GST_STATIC_PAD_TEMPLATE ("sink",
93     GST_PAD_SINK,
94     GST_PAD_ALWAYS,
95     GST_STATIC_CAPS ("video/x-raw, "
96         "framerate = (fraction) [ 0, MAX ], "
97         "width = (int) [ 1, MAX ], " "height = (int) [ 1, MAX ]")
98     );
99
100 /* Signals and args */
101 enum
102 {
103   ARG_0,
104   ARG_SURFACE,
105   ARG_CONTRAST,
106   ARG_BRIGHTNESS,
107   ARG_HUE,
108   ARG_SATURATION,
109   ARG_PIXEL_ASPECT_RATIO,
110   ARG_VSYNC,
111   ARG_LAYER_MODE
112 };
113
114 #define DEFAULT_LAYER_MODE LAYER_MODE_EXCLUSIVE
115
116 static DFBSurfacePixelFormat gst_dfbvideosink_get_format_from_caps (GstCaps *
117     caps);
118 static void gst_dfbvideosink_update_colorbalance (GstDfbVideoSink *
119     dfbvideosink);
120 static void gst_dfbvideosink_navigation_init (GstNavigationInterface * iface);
121 static void gst_dfbvideosink_colorbalance_init (GstColorBalanceInterface
122     * iface);
123 static const char *gst_dfbvideosink_get_format_name (DFBSurfacePixelFormat
124     format);
125
126 #define gst_dfbvideosink_parent_class parent_class
127
128 static GType
129 gst_dfbvideosink_layer_mode_get_type (void)
130 {
131   static gsize id = 0;
132   static const GEnumValue values[] = {
133     {0, "NONE", "none"},
134     {DLSCL_EXCLUSIVE, "DLSCL_EXCLUSIVE", "exclusive"},
135     {DLSCL_ADMINISTRATIVE, "DLSCL_ADMINISTRATIVE", "administrative"},
136     {0, NULL, NULL}
137   };
138
139   if (g_once_init_enter (&id)) {
140     GType tmp = g_enum_register_static ("GstDfbVideoSinkLayerMode", values);
141     g_once_init_leave (&id, tmp);
142   }
143
144   return (GType) id;
145 }
146
147 GType
148 gst_meta_dfbsurface_api_get_type (void)
149 {
150   static GType type;
151   static const gchar *tags[] = { "memory", NULL };
152
153   if (g_once_init_enter (&type)) {
154     GType _type = gst_meta_api_type_register ("GstMetaDfbSurfaceAPI", tags);
155     g_once_init_leave (&type, _type);
156   }
157   return type;
158 }
159
160 static gboolean
161 gst_meta_dfbsurface_init (GstMetaDfbSurface * meta, gpointer params,
162     GstBuffer * buf)
163 {
164   meta->surface = NULL;
165   meta->width = meta->height = 0;
166   meta->locked = FALSE;
167   meta->pixel_format = 0;
168   meta->dfbvideosink = NULL;
169
170   return TRUE;
171 }
172
173 /* our metadata */
174 const GstMetaInfo *
175 gst_meta_dfbsurface_get_info (void)
176 {
177   static const GstMetaInfo *meta_info = NULL;
178
179   if (g_once_init_enter (&meta_info)) {
180     const GstMetaInfo *meta =
181         gst_meta_register (gst_meta_dfbsurface_api_get_type (),
182         "GstMetaDfbSurface", sizeof (GstMetaDfbSurface),
183         (GstMetaInitFunction) gst_meta_dfbsurface_init,
184         (GstMetaFreeFunction) NULL,
185         (GstMetaTransformFunction) NULL);
186     g_once_init_leave (&meta_info, meta);
187   }
188   return meta_info;
189 }
190
191 G_DEFINE_TYPE (GstDfbBufferPool, gst_dfb_buffer_pool, GST_TYPE_BUFFER_POOL);
192
193 static gboolean
194 gst_dfb_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
195 {
196   GstDfbBufferPool *dfbpool = GST_DFB_BUFFER_POOL_CAST (pool);
197   GstCaps *caps;
198   DFBSurfacePixelFormat pixel_format = DSPF_UNKNOWN;
199   gint width, height;
200   DFBResult ret;
201   DFBSurfaceDescription s_dsc;
202   IDirectFBSurface *surface;
203   gpointer data;
204   gint pitch;
205   guint size;
206   guint min_buffers;
207   guint max_buffers;
208   GstVideoInfo info;
209
210   if (!dfbpool->dfbvideosink->setup) {
211     GST_WARNING_OBJECT (pool, "DirectFB hasn't been initialized yet.");
212     return FALSE;
213   }
214
215   if (!gst_buffer_pool_config_get_params (config, &caps, NULL, &min_buffers,
216           &max_buffers)) {
217     GST_WARNING_OBJECT (pool, "invalid config");
218     return FALSE;
219   }
220
221   pixel_format = gst_dfbvideosink_get_format_from_caps (caps);
222
223   if (!gst_video_info_from_caps (&info, caps)) {
224     GST_WARNING_OBJECT (pool, "failed getting video info from caps %"
225         GST_PTR_FORMAT, caps);
226     return FALSE;
227   }
228
229   width = GST_VIDEO_INFO_WIDTH (&info);
230   height = GST_VIDEO_INFO_HEIGHT (&info);
231
232   /* temporarily create a surface to get the pitch */
233   s_dsc.flags = DSDESC_PIXELFORMAT | DSDESC_WIDTH | DSDESC_HEIGHT;
234   s_dsc.pixelformat = pixel_format;
235   s_dsc.width = width;
236   s_dsc.height = height;
237
238   ret = dfbpool->dfbvideosink->dfb->CreateSurface (dfbpool->dfbvideosink->dfb,
239       &s_dsc, &surface);
240   if (ret != DFB_OK) {
241     GST_WARNING_OBJECT (pool, "failed creating surface with format %s",
242         gst_dfbvideosink_get_format_name (pixel_format));
243     return FALSE;
244   }
245
246   ret = surface->Lock (surface, DSLF_READ, &data, &pitch);
247   if (ret != DFB_OK) {
248     GST_WARNING_OBJECT (pool, "failed locking the surface");
249     surface->Release (surface);
250     return FALSE;
251   }
252   surface->Unlock (surface);
253   surface->Release (surface);
254
255   switch (GST_VIDEO_INFO_FORMAT (&info)) {
256     case GST_VIDEO_FORMAT_I420:
257     case GST_VIDEO_FORMAT_YV12:
258     case GST_VIDEO_FORMAT_NV12:
259       size = pitch * height * 3 / 2;
260       break;
261     default:
262       size = pitch * height;
263       break;
264   }
265
266   gst_buffer_pool_config_set_params (config, caps, size, min_buffers,
267       max_buffers);
268
269   dfbpool->caps = gst_caps_ref (caps);
270
271   return GST_BUFFER_POOL_CLASS (gst_dfb_buffer_pool_parent_class)->set_config
272       (pool, config);
273 }
274
275 static void
276 gst_dfb_buffer_pool_free_buffer (GstBufferPool * bpool, GstBuffer * surface)
277 {
278   GstMetaDfbSurface *meta;
279
280   meta = GST_META_DFBSURFACE_GET (surface);
281
282   /* Release our internal surface */
283   if (meta->surface) {
284     if (meta->locked) {
285       meta->surface->Unlock (meta->surface);
286       meta->locked = FALSE;
287     }
288     meta->surface->Release (meta->surface);
289   }
290
291   if (meta->dfbvideosink)
292     /* Release the ref to our sink */
293     gst_object_unref (meta->dfbvideosink);
294
295   GST_BUFFER_POOL_CLASS (gst_dfb_buffer_pool_parent_class)->free_buffer (bpool,
296       surface);
297 }
298
299 static GstFlowReturn
300 gst_dfb_buffer_pool_alloc_buffer (GstBufferPool * bpool,
301     GstBuffer ** buffer, GstBufferPoolAcquireParams * params)
302 {
303   GstDfbBufferPool *dfbpool = GST_DFB_BUFFER_POOL_CAST (bpool);
304   GstBuffer *surface;
305   GstMetaDfbSurface *meta;
306   GstStructure *structure;
307   DFBResult ret;
308   DFBSurfaceDescription s_dsc;
309   gpointer data;
310   gint pitch;
311   GstFlowReturn result = GST_FLOW_ERROR;
312   gsize alloc_size;
313   gsize offset[GST_VIDEO_MAX_PLANES] = { 0 };
314   gint stride[GST_VIDEO_MAX_PLANES] = { 0 };
315   gsize max_size;
316   gsize plane_size[GST_VIDEO_MAX_PLANES] = { 0 };
317   guint n_planes;
318   const gchar *str;
319   GstVideoFormat format;
320   gint i;
321
322   surface = gst_buffer_new ();
323   meta = GST_META_DFBSURFACE_ADD (surface);
324
325   /* Keep a ref to our sink */
326   meta->dfbvideosink = gst_object_ref (dfbpool->dfbvideosink);
327   /* Surface is not locked yet */
328   meta->locked = FALSE;
329
330   structure = gst_caps_get_structure (dfbpool->caps, 0);
331
332   if (!gst_structure_get_int (structure, "width", &meta->width) ||
333       !gst_structure_get_int (structure, "height", &meta->height)) {
334     GST_WARNING_OBJECT (bpool, "failed getting geometry from caps %"
335         GST_PTR_FORMAT, dfbpool->caps);
336     goto fallback;
337   }
338
339   /* Pixel format from caps */
340   meta->pixel_format = gst_dfbvideosink_get_format_from_caps (dfbpool->caps);
341   if (meta->pixel_format == DSPF_UNKNOWN) {
342     goto fallback;
343   }
344
345   if (!dfbpool->dfbvideosink->dfb) {
346     GST_DEBUG_OBJECT (bpool, "no DirectFB context to create a surface");
347     goto fallback;
348   }
349
350   /* Creating an internal surface which will be used as GstBuffer, we used
351      the detected pixel format and video dimensions */
352
353   s_dsc.flags = DSDESC_PIXELFORMAT | DSDESC_WIDTH | DSDESC_HEIGHT;
354
355   s_dsc.pixelformat = meta->pixel_format;
356   s_dsc.width = meta->width;
357   s_dsc.height = meta->height;
358
359   ret =
360       dfbpool->dfbvideosink->dfb->CreateSurface (dfbpool->dfbvideosink->dfb,
361       &s_dsc, &meta->surface);
362   if (ret != DFB_OK) {
363     GST_WARNING_OBJECT (bpool, "failed creating a DirectFB surface");
364     meta->surface = NULL;
365     goto fallback;
366   }
367
368   /* Clearing surface */
369   meta->surface->Clear (meta->surface, 0x00, 0x00, 0x00, 0xFF);
370
371   /* Locking the surface to acquire the memory pointer */
372   meta->surface->Lock (meta->surface, DSLF_WRITE, &data, &pitch);
373   meta->locked = TRUE;
374
375   GST_DEBUG_OBJECT (bpool, "creating a %dx%d surface (%p) with %s "
376       "pixel format, line pitch %d", meta->width, meta->height, surface,
377       gst_dfbvideosink_get_format_name (meta->pixel_format), pitch);
378
379   structure = gst_caps_get_structure (dfbpool->caps, 0);
380   str = gst_structure_get_string (structure, "format");
381   if (str == NULL) {
382     GST_WARNING ("failed grabbing fourcc from caps %" GST_PTR_FORMAT,
383         dfbpool->caps);
384     return GST_FLOW_ERROR;
385   }
386
387   format = gst_video_format_from_string (str);
388   switch (format) {
389     case GST_VIDEO_FORMAT_I420:
390     case GST_VIDEO_FORMAT_YV12:
391       offset[1] = pitch * meta->height;
392       offset[2] = offset[1] + pitch / 2 * meta->height / 2;
393       stride[0] = pitch;
394       stride[1] = stride[2] = pitch / 2;
395
396       plane_size[0] = offset[1];
397       plane_size[1] = plane_size[2] = plane_size[0] / 4;
398       max_size = plane_size[0] * 3 / 2;
399       n_planes = 3;
400       break;
401     case GST_VIDEO_FORMAT_NV12:
402       offset[1] = pitch * meta->height;
403       stride[0] = stride[1] = pitch;
404
405       plane_size[0] = offset[1];
406       plane_size[1] = pitch * meta->height / 2;
407       max_size = plane_size[0] * 3 / 2;
408       n_planes = 2;
409       break;
410     default:
411       stride[0] = pitch;
412       plane_size[0] = max_size = pitch * meta->height;
413       n_planes = 1;
414       break;
415   }
416
417   for (i = 0; i < n_planes; i++) {
418     gst_buffer_append_memory (surface,
419         gst_memory_new_wrapped (0, data, max_size, offset[i], plane_size[i],
420             NULL, NULL));
421   }
422
423   gst_buffer_add_video_meta_full (surface, GST_VIDEO_FRAME_FLAG_NONE,
424       format, meta->width, meta->height, n_planes, offset, stride);
425
426   result = GST_FLOW_OK;
427
428   goto beach;
429
430 fallback:
431
432   /* We allocate a standard buffer ourselves to store it in our buffer pool,
433      this is an optimisation for memory allocation */
434   alloc_size = meta->width * meta->height;
435   surface = gst_buffer_new_allocate (NULL, alloc_size, NULL);
436   if (surface == NULL) {
437     GST_WARNING_OBJECT (bpool, "failed allocating a gstbuffer");
438     goto beach;
439   }
440
441   if (meta->surface) {
442     if (meta->locked) {
443       meta->surface->Unlock (meta->surface);
444       meta->locked = FALSE;
445     }
446     meta->surface->Release (meta->surface);
447     meta->surface = NULL;
448   }
449   GST_DEBUG_OBJECT (bpool, "allocating a buffer (%p) of %u bytes",
450       surface, (guint) alloc_size);
451
452   result = GST_FLOW_OK;
453
454 beach:
455   if (result != GST_FLOW_OK) {
456     gst_dfb_buffer_pool_free_buffer (bpool, surface);
457     *buffer = NULL;
458   } else
459     *buffer = surface;
460
461   return result;
462 }
463
464 static GstBufferPool *
465 gst_dfb_buffer_pool_new (GstDfbVideoSink * dfbvideosink)
466 {
467   GstDfbBufferPool *pool;
468
469   g_return_val_if_fail (GST_IS_DFBVIDEOSINK (dfbvideosink), NULL);
470
471   pool = g_object_new (GST_TYPE_DFB_BUFFER_POOL, NULL);
472   g_object_ref_sink (pool);
473   pool->dfbvideosink = gst_object_ref (dfbvideosink);
474
475   GST_LOG_OBJECT (pool, "new dfb buffer pool %p", pool);
476
477   return GST_BUFFER_POOL_CAST (pool);
478 }
479
480 static void
481 gst_dfb_buffer_pool_finalize (GObject * object)
482 {
483   GstDfbBufferPool *pool = GST_DFB_BUFFER_POOL_CAST (object);
484
485   if (pool->caps)
486     gst_caps_unref (pool->caps);
487   gst_object_unref (pool->dfbvideosink);
488
489   G_OBJECT_CLASS (gst_dfb_buffer_pool_parent_class)->finalize (object);
490 }
491
492 static void
493 gst_dfb_buffer_pool_init (GstDfbBufferPool * pool)
494 {
495   /* No processing */
496 }
497
498 static void
499 gst_dfb_buffer_pool_class_init (GstDfbBufferPoolClass * klass)
500 {
501   GObjectClass *gobject_class = (GObjectClass *) klass;
502   GstBufferPoolClass *gstbufferpool_class = (GstBufferPoolClass *) klass;
503
504   gobject_class->finalize = gst_dfb_buffer_pool_finalize;
505
506   gstbufferpool_class->alloc_buffer = gst_dfb_buffer_pool_alloc_buffer;
507   gstbufferpool_class->set_config = gst_dfb_buffer_pool_set_config;
508   gstbufferpool_class->free_buffer = gst_dfb_buffer_pool_free_buffer;
509 }
510
511 G_DEFINE_TYPE_WITH_CODE (GstDfbVideoSink, gst_dfbvideosink, GST_TYPE_VIDEO_SINK,
512     G_IMPLEMENT_INTERFACE (GST_TYPE_NAVIGATION,
513         gst_dfbvideosink_navigation_init);
514     G_IMPLEMENT_INTERFACE (GST_TYPE_COLOR_BALANCE,
515         gst_dfbvideosink_colorbalance_init);
516     GST_DEBUG_CATEGORY_INIT (dfbvideosink_debug, "dfbvideosink", 0,
517         "DirectFB video sink element");
518     );
519 GST_ELEMENT_REGISTER_DEFINE (dfbvideosink, "dfbvideosink", GST_RANK_MARGINAL,
520     GST_TYPE_DFBVIDEOSINK);
521 #ifndef GST_DISABLE_GST_DEBUG
522 static const char *
523 gst_dfbvideosink_get_format_name (DFBSurfacePixelFormat format)
524 {
525   switch (format) {
526     case DSPF_ARGB1555:
527       return "ARGB1555";
528     case DSPF_RGB16:
529       return "RGB16";
530     case DSPF_RGB24:
531       return "RGB24";
532     case DSPF_RGB32:
533       return "RGB32";
534     case DSPF_ARGB:
535       return "ARGB";
536     case DSPF_A8:
537       return "A8";
538     case DSPF_YUY2:
539       return "YUY2";
540     case DSPF_RGB332:
541       return "RGB33";
542     case DSPF_UYVY:
543       return "UYVY";
544     case DSPF_I420:
545       return "I420";
546     case DSPF_YV12:
547       return "YV12";
548     case DSPF_LUT8:
549       return "LUT8";
550     case DSPF_ALUT44:
551       return "ALUT44";
552     case DSPF_AiRGB:
553       return "AiRGB";
554     case DSPF_A1:
555       return "A1";
556     case DSPF_NV12:
557       return "NV12";
558     case DSPF_NV16:
559       return "NV16";
560     case DSPF_ARGB2554:
561       return "ARGB2554";
562     case DSPF_ARGB4444:
563       return "ARGB4444";
564     case DSPF_NV21:
565       return "NV21";
566     default:
567       return "UNKNOWN";
568   }
569 }
570 #endif /* GST_DISABLE_GST_DEBUG */
571
572 static gpointer
573 gst_dfbvideosink_event_thread (GstDfbVideoSink * dfbvideosink)
574 {
575   DFBResult ret;
576
577   while (dfbvideosink->running) {
578     /* Wait for an event with a 50 ms timeout */
579     dfbvideosink->event_buffer->WaitForEventWithTimeout (dfbvideosink->
580         event_buffer, 0, 50);
581
582     /* Do we have an event ? */
583     ret = dfbvideosink->event_buffer->HasEvent (dfbvideosink->event_buffer);
584
585     if (ret == DFB_OK) {
586       DFBEvent event;
587
588       GST_DEBUG_OBJECT (dfbvideosink, "we have an event");
589
590       ret = dfbvideosink->event_buffer->GetEvent (dfbvideosink->event_buffer,
591           &event);
592       if (ret != DFB_OK) {      /* Error */
593         GST_WARNING_OBJECT (dfbvideosink, "failed when getting event from "
594             "event buffer");
595       } else {                  /* Handle event */
596         if (event.input.type == DIET_KEYPRESS) {
597           switch (event.input.key_symbol) {
598             case DIKS_ESCAPE:
599             {
600               GST_ELEMENT_ERROR (dfbvideosink, RESOURCE, OPEN_WRITE,
601                   ("Video output device is gone."),
602                   ("We were running fullscreen and user "
603                       "pressed the ESC key, stopping playback."));
604             }
605             default:
606               GST_DEBUG_OBJECT (dfbvideosink, "key press event %c !",
607                   event.input.key_symbol);
608               gst_navigation_send_key_event (GST_NAVIGATION (dfbvideosink),
609                   "key-press", "prout");
610           }
611         } else if (event.input.type == DIET_BUTTONPRESS) {
612           gint x, y;
613
614           dfbvideosink->layer->GetCursorPosition (dfbvideosink->layer, &x, &y);
615
616           GST_DEBUG_OBJECT (dfbvideosink, "button %d pressed at %dx%d",
617               event.input.button, x, y);
618
619           gst_navigation_send_mouse_event (GST_NAVIGATION (dfbvideosink),
620               "mouse-button-press", event.input.button, x, y);
621         } else if (event.input.type == DIET_BUTTONRELEASE) {
622           gint x, y;
623
624           dfbvideosink->layer->GetCursorPosition (dfbvideosink->layer, &x, &y);
625
626           GST_DEBUG_OBJECT (dfbvideosink, "button %d released at %dx%d",
627               event.input.button, x, y);
628
629           gst_navigation_send_mouse_event (GST_NAVIGATION (dfbvideosink),
630               "mouse-button-release", event.input.button, x, y);
631         } else if (event.input.type == DIET_AXISMOTION) {
632           gint x, y;
633
634           dfbvideosink->layer->GetCursorPosition (dfbvideosink->layer, &x, &y);
635           gst_navigation_send_mouse_event (GST_NAVIGATION (dfbvideosink),
636               "mouse-move", 0, x, y);
637         } else {
638           GST_WARNING_OBJECT (dfbvideosink, "unhandled event type %d",
639               event.input.type);
640         }
641       }
642     }
643   }
644   return NULL;
645 }
646
647 static DFBEnumerationResult
648 gst_dfbvideosink_enum_layers (DFBDisplayLayerID id,
649     DFBDisplayLayerDescription desc, void *data)
650 {
651   GstDfbVideoSink *dfbvideosink = NULL;
652   IDirectFBDisplayLayer *layer = NULL;
653   DFBDisplayLayerConfig dlc;
654   DFBResult ret;
655   gboolean backbuffer = FALSE;
656
657   g_return_val_if_fail (GST_IS_DFBVIDEOSINK (data), DFENUM_CANCEL);
658
659   dfbvideosink = GST_DFBVIDEOSINK (data);
660
661   GST_DEBUG_OBJECT (dfbvideosink, "inspecting display layer %d with name: %s",
662       id, desc.name);
663
664   if ((desc.type & DLTF_VIDEO) && (desc.caps & DLCAPS_SURFACE)) {
665     GST_DEBUG_OBJECT (dfbvideosink,
666         "this layer can handle live video and has a surface");
667   } else {
668     if (desc.caps & DLCAPS_SURFACE) {
669       GST_DEBUG_OBJECT (dfbvideosink,
670           "this layer can not handle live video but has a surface");
671     } else {
672       GST_DEBUG_OBJECT (dfbvideosink, "no we can't use that layer, really...");
673       goto beach;
674     }
675   }
676
677   ret = dfbvideosink->dfb->GetDisplayLayer (dfbvideosink->dfb, id, &layer);
678   if (ret != DFB_OK) {
679     GST_WARNING_OBJECT (dfbvideosink, "failed getting display layer %s",
680         desc.name);
681     goto beach;
682   }
683
684   ret = layer->GetConfiguration (layer, &dlc);
685   if (ret != DFB_OK) {
686     GST_WARNING_OBJECT (dfbvideosink,
687         "failed getting display layer configuration");
688     goto beach;
689   }
690
691   if ((dlc.flags & DLCONF_BUFFERMODE) && (dlc.buffermode & DLBM_FRONTONLY)) {
692     GST_DEBUG_OBJECT (dfbvideosink, "no backbuffer");
693   }
694   if ((dlc.flags & DLCONF_BUFFERMODE) && (dlc.buffermode & DLBM_BACKVIDEO)) {
695     GST_DEBUG_OBJECT (dfbvideosink, "backbuffer is in video memory");
696     backbuffer = TRUE;
697   }
698   if ((dlc.flags & DLCONF_BUFFERMODE) && (dlc.buffermode & DLBM_BACKSYSTEM)) {
699     GST_DEBUG_OBJECT (dfbvideosink, "backbuffer is in system memory");
700     backbuffer = TRUE;
701   }
702   if ((dlc.flags & DLCONF_BUFFERMODE) && (dlc.buffermode & DLBM_TRIPLE)) {
703     GST_DEBUG_OBJECT (dfbvideosink, "triple buffering");
704     backbuffer = TRUE;
705   }
706
707   /* If the primary is suitable we prefer using it */
708   if (dfbvideosink->layer_id != DLID_PRIMARY) {
709     GST_DEBUG_OBJECT (dfbvideosink, "selecting layer named %s", desc.name);
710     dfbvideosink->layer_id = id;
711     dfbvideosink->backbuffer = backbuffer;
712   } else {
713     GST_DEBUG_OBJECT (dfbvideosink, "layer %s is suitable but the primary "
714         "is currently selected and we prefer that one", desc.name);
715   }
716
717 beach:
718   if (layer) {
719     layer->Release (layer);
720   }
721   return DFENUM_OK;
722 }
723
724 static DFBEnumerationResult
725 gst_dfbvideosink_enum_vmodes (gint width, gint height, gint bpp, void *data)
726 {
727   GstDfbVideoSink *dfbvideosink = NULL;
728   GstDfbVMode *vmode = NULL;
729
730   g_return_val_if_fail (GST_IS_DFBVIDEOSINK (data), DFENUM_CANCEL);
731
732   dfbvideosink = GST_DFBVIDEOSINK (data);
733
734   GST_DEBUG_OBJECT (dfbvideosink, "adding video mode %dx%d at %d bpp", width,
735       height, bpp);
736   vmode = g_new0 (GstDfbVMode, 1);
737   vmode->width = width;
738   vmode->height = height;
739   vmode->bpp = bpp;
740
741   /* We need to know the maximum video geometry we can accept for the caps */
742   if (width > dfbvideosink->out_width) {
743     dfbvideosink->out_width = width;
744   }
745   if (height > dfbvideosink->out_height) {
746     dfbvideosink->out_height = height;
747   }
748
749   dfbvideosink->vmodes = g_slist_append (dfbvideosink->vmodes, vmode);
750
751   return DFENUM_OK;
752 }
753
754 static DFBEnumerationResult
755 gst_dfbvideosink_enum_devices (DFBInputDeviceID id,
756     DFBInputDeviceDescription desc, void *data)
757 {
758   GstDfbVideoSink *dfbvideosink = NULL;
759   IDirectFBInputDevice *device = NULL;
760   DFBResult ret;
761
762   g_return_val_if_fail (GST_IS_DFBVIDEOSINK (data), DFENUM_CANCEL);
763
764   dfbvideosink = GST_DFBVIDEOSINK (data);
765
766   GST_DEBUG_OBJECT (dfbvideosink, "detected input device %s from vendor %s",
767       desc.name, desc.vendor);
768
769   /* Get that input device */
770   ret = dfbvideosink->dfb->GetInputDevice (dfbvideosink->dfb, id, &device);
771   if (ret != DFB_OK) {
772     GST_WARNING_OBJECT (dfbvideosink, "failed when getting input device id %d",
773         id);
774     goto beach;
775   }
776
777   ret = device->AttachEventBuffer (device, dfbvideosink->event_buffer);
778   if (ret != DFB_OK) {
779     GST_WARNING_OBJECT (dfbvideosink, "failed when attaching input device "
780         "%d to our event buffer", id);
781   }
782
783 beach:
784   if (device) {
785     device->Release (device);
786   }
787   return DFENUM_OK;
788 }
789
790 static gboolean
791 gst_dfbvideosink_setup (GstDfbVideoSink * dfbvideosink)
792 {
793   DFBResult ret;
794
795   g_return_val_if_fail (GST_IS_DFBVIDEOSINK (dfbvideosink), FALSE);
796
797   dfbvideosink->video_width = 0;
798   dfbvideosink->video_height = 0;
799   dfbvideosink->out_width = 0;
800   dfbvideosink->out_height = 0;
801   dfbvideosink->fps_d = 0;
802   dfbvideosink->fps_n = 0;
803   dfbvideosink->hw_scaling = FALSE;
804   dfbvideosink->backbuffer = FALSE;
805   dfbvideosink->pixel_format = DSPF_UNKNOWN;
806
807   /* If we do it all by ourself we create the DirectFB context, get the
808      primary layer and use a fullscreen configuration */
809   if (!dfbvideosink->ext_surface) {
810     GST_DEBUG_OBJECT (dfbvideosink, "no external surface, taking over "
811         "DirectFB fullscreen");
812     if (!dfbvideosink->dfb) {
813       DFBGraphicsDeviceDescription hw_caps;
814       char *argv[] = { (char *) "-", (char *) "--dfb:quiet",
815         (char *) "--dfb:no-sighandler", NULL
816       };
817       int argc = 3;
818       char **args;
819
820       GST_DEBUG_OBJECT (dfbvideosink, "initializing DirectFB");
821
822       args = argv;
823       ret = DirectFBInit (&argc, &args);
824
825       if (ret != DFB_OK) {
826         GST_WARNING_OBJECT (dfbvideosink, "DirectFB initialization failed");
827         goto beach;
828       }
829
830       ret = DirectFBCreate (&(dfbvideosink->dfb));
831
832       if (ret != DFB_OK) {
833         GST_WARNING_OBJECT (dfbvideosink, "failed creating the DirectFB "
834             "main object");
835         goto beach;
836       }
837
838       /* Get Hardware capabilities */
839       ret = dfbvideosink->dfb->GetDeviceDescription (dfbvideosink->dfb,
840           &hw_caps);
841
842       if (ret != DFB_OK) {
843         GST_WARNING_OBJECT (dfbvideosink, "failed grabbing the hardware "
844             "capabilities");
845         goto beach;
846       }
847
848       GST_DEBUG_OBJECT (dfbvideosink, "video card %s from vendor %s detected "
849           "with %d bytes of video memory", hw_caps.name, hw_caps.vendor,
850           hw_caps.video_memory);
851
852       if (hw_caps.acceleration_mask & DFXL_BLIT) {
853         GST_DEBUG_OBJECT (dfbvideosink, "Blit is accelerated");
854       }
855       if (hw_caps.acceleration_mask & DFXL_STRETCHBLIT) {
856         GST_DEBUG_OBJECT (dfbvideosink, "StretchBlit is accelerated");
857         dfbvideosink->hw_scaling = TRUE;
858       } else {
859         GST_DEBUG_OBJECT (dfbvideosink, "StretchBlit is not accelerated");
860         dfbvideosink->hw_scaling = FALSE;
861       }
862
863       dfbvideosink->layer_id = -1;
864
865       /* Inspect all the Display layers */
866       dfbvideosink->dfb->EnumDisplayLayers (dfbvideosink->dfb,
867           gst_dfbvideosink_enum_layers, dfbvideosink);
868       /* Inspect all Video modes */
869       dfbvideosink->dfb->EnumVideoModes (dfbvideosink->dfb,
870           gst_dfbvideosink_enum_vmodes, dfbvideosink);
871
872       /* Create an event buffer for input */
873       dfbvideosink->dfb->CreateEventBuffer (dfbvideosink->dfb,
874           &dfbvideosink->event_buffer);
875
876       /* Inspect all Input devices */
877       dfbvideosink->dfb->EnumInputDevices (dfbvideosink->dfb,
878           gst_dfbvideosink_enum_devices, dfbvideosink);
879       /* Create a thread to handle those events */
880       dfbvideosink->event_thread = g_thread_new ("dfbvsink-events",
881           (GThreadFunc) gst_dfbvideosink_event_thread, dfbvideosink);
882     }
883     if (!dfbvideosink->layer) {
884       GList *channels_list = NULL;
885       DFBDisplayLayerDescription dl_desc;
886
887       /* Get the best Display Layer */
888       ret = dfbvideosink->dfb->GetDisplayLayer (dfbvideosink->dfb,
889           dfbvideosink->layer_id, &dfbvideosink->layer);
890       if (ret != DFB_OK) {
891         GST_WARNING_OBJECT (dfbvideosink, "failed getting display layer");
892         goto beach;
893       }
894
895       if (dfbvideosink->layer_mode == LAYER_MODE_EXCLUSIVE ||
896           dfbvideosink->layer_mode == LAYER_MODE_ADMINISTRATIVE)
897         ret = dfbvideosink->layer->SetCooperativeLevel (dfbvideosink->layer,
898             dfbvideosink->layer_mode);
899       else {
900         GST_ERROR_OBJECT (dfbvideosink, "invalid layer cooperative level");
901         goto beach;
902       }
903
904       if (ret != DFB_OK) {
905         GST_WARNING_OBJECT (dfbvideosink, "failed setting display layer to "
906             "fullscreen mode");
907         goto beach;
908       }
909
910       dfbvideosink->layer->GetDescription (dfbvideosink->layer, &dl_desc);
911
912       /* Check that this layer is able to do colorbalance settings */
913       if (dl_desc.caps & DLCAPS_BRIGHTNESS) {
914         channels_list = g_list_append (channels_list, (char *) "BRIGHTNESS");
915       }
916       if (dl_desc.caps & DLCAPS_CONTRAST) {
917         channels_list = g_list_append (channels_list, (char *) "CONTRAST");
918       }
919       if (dl_desc.caps & DLCAPS_HUE) {
920         channels_list = g_list_append (channels_list, (char *) "HUE");
921       }
922       if (dl_desc.caps & DLCAPS_SATURATION) {
923         channels_list = g_list_append (channels_list, (char *) "SATURATION");
924       }
925
926       if (channels_list) {
927         GList *walk = channels_list;
928
929         /* Generate Color balance channel list */
930         while (walk) {
931           GstColorBalanceChannel *channel = NULL;
932
933           GST_DEBUG_OBJECT (dfbvideosink, "adding %s as a colorbalance channel",
934               (const char *) walk->data);
935
936           channel = g_object_new (GST_TYPE_COLOR_BALANCE_CHANNEL, NULL);
937           channel->label = g_strdup (walk->data);
938           channel->min_value = 0x0000;
939           channel->max_value = 0xFFFF;
940
941           dfbvideosink->cb_channels = g_list_append (dfbvideosink->cb_channels,
942               channel);
943
944           walk = g_list_next (walk);
945         }
946
947         /* If the colorbalance settings have not been touched we get current
948            values as defaults and update our internal variables */
949         if (!dfbvideosink->cb_changed) {
950           DFBColorAdjustment cb_adjust;
951
952           ret = dfbvideosink->layer->GetColorAdjustment (dfbvideosink->layer,
953               &cb_adjust);
954
955           if (ret != DFB_OK) {
956             GST_WARNING_OBJECT (dfbvideosink, "failed when getting color "
957                 "adjustment from layer");
958           }
959
960           if (cb_adjust.flags & DCAF_BRIGHTNESS) {
961             dfbvideosink->brightness = cb_adjust.brightness;
962           } else {
963             dfbvideosink->brightness = 0x8000;
964           }
965           if (cb_adjust.flags & DCAF_CONTRAST) {
966             dfbvideosink->contrast = cb_adjust.contrast;
967           } else {
968             dfbvideosink->contrast = 0x8000;
969           }
970           if (cb_adjust.flags & DCAF_HUE) {
971             dfbvideosink->hue = cb_adjust.hue;
972           } else {
973             dfbvideosink->hue = 0x8000;
974           }
975           if (cb_adjust.flags & DCAF_SATURATION) {
976             dfbvideosink->saturation = cb_adjust.saturation;
977           } else {
978             dfbvideosink->saturation = 0x8000;
979           }
980           GST_DEBUG_OBJECT (dfbvideosink, "brightness %d, contrast %d, "
981               "hue %d, saturation %d", dfbvideosink->brightness,
982               dfbvideosink->contrast, dfbvideosink->hue,
983               dfbvideosink->saturation);
984         }
985
986         g_list_free (channels_list);
987
988         gst_dfbvideosink_update_colorbalance (dfbvideosink);
989       }
990
991       dfbvideosink->layer->SetBackgroundColor (dfbvideosink->layer,
992           0x00, 0x00, 0x00, 0xFF);
993
994 #if (DIRECTFB_VER >= GST_DFBVIDEOSINK_VER (1,6,0))
995       if (dfbvideosink->layer_mode == LAYER_MODE_ADMINISTRATIVE)
996 #endif
997         dfbvideosink->layer->EnableCursor (dfbvideosink->layer, TRUE);
998
999       GST_DEBUG_OBJECT (dfbvideosink, "getting primary surface");
1000       dfbvideosink->layer->GetSurface (dfbvideosink->layer,
1001           &dfbvideosink->primary);
1002
1003       dfbvideosink->primary->SetBlittingFlags (dfbvideosink->primary,
1004           DSBLIT_NOFX);
1005     }
1006
1007     dfbvideosink->primary->GetPixelFormat (dfbvideosink->primary,
1008         &dfbvideosink->pixel_format);
1009   } else {
1010     DFBSurfaceCapabilities s_caps;
1011
1012     GST_DEBUG_OBJECT (dfbvideosink, "getting pixel format from foreign "
1013         "surface %p", dfbvideosink->ext_surface);
1014     dfbvideosink->ext_surface->GetPixelFormat (dfbvideosink->ext_surface,
1015         &dfbvideosink->pixel_format);
1016     dfbvideosink->ext_surface->GetSize (dfbvideosink->ext_surface,
1017         &dfbvideosink->out_width, &dfbvideosink->out_height);
1018     dfbvideosink->ext_surface->GetCapabilities (dfbvideosink->ext_surface,
1019         &s_caps);
1020     if ((s_caps & DSCAPS_DOUBLE) || (s_caps & DSCAPS_TRIPLE)) {
1021       dfbvideosink->backbuffer = TRUE;
1022     } else {
1023       dfbvideosink->backbuffer = FALSE;
1024     }
1025     GST_DEBUG_OBJECT (dfbvideosink, "external surface is %dx%d and uses %s "
1026         "pixel format", dfbvideosink->out_width, dfbvideosink->out_height,
1027         gst_dfbvideosink_get_format_name (dfbvideosink->pixel_format));
1028   }
1029
1030   dfbvideosink->setup = TRUE;
1031
1032 beach:
1033   return dfbvideosink->setup;
1034 }
1035
1036 static void
1037 gst_dfbvideosink_cleanup (GstDfbVideoSink * dfbvideosink)
1038 {
1039   g_return_if_fail (GST_IS_DFBVIDEOSINK (dfbvideosink));
1040
1041   GST_DEBUG_OBJECT (dfbvideosink, "cleaning up DirectFB environment");
1042
1043   /* Wait for our event thread */
1044   if (dfbvideosink->event_thread) {
1045     g_thread_join (dfbvideosink->event_thread);
1046     dfbvideosink->event_thread = NULL;
1047   }
1048
1049   if (dfbvideosink->event_buffer) {
1050     dfbvideosink->event_buffer->Release (dfbvideosink->event_buffer);
1051     dfbvideosink->event_buffer = NULL;
1052   }
1053
1054   if (dfbvideosink->vmodes) {
1055     GSList *walk = dfbvideosink->vmodes;
1056
1057     while (walk) {
1058       g_free (walk->data);
1059       walk = g_slist_next (walk);
1060     }
1061     g_slist_free (dfbvideosink->vmodes);
1062     dfbvideosink->vmodes = NULL;
1063   }
1064
1065   if (dfbvideosink->cb_channels) {
1066     GList *walk = dfbvideosink->cb_channels;
1067
1068     while (walk) {
1069       GstColorBalanceChannel *channel = walk->data;
1070
1071       g_object_unref (channel);
1072       walk = g_list_next (walk);
1073     }
1074     g_list_free (dfbvideosink->cb_channels);
1075     dfbvideosink->cb_channels = NULL;
1076   }
1077
1078   if (dfbvideosink->pool) {
1079     gst_object_unref (dfbvideosink->pool);
1080     dfbvideosink->pool = NULL;
1081   }
1082
1083   if (dfbvideosink->primary) {
1084     dfbvideosink->primary->Release (dfbvideosink->primary);
1085     dfbvideosink->primary = NULL;
1086   }
1087
1088   if (dfbvideosink->layer) {
1089 #if (DIRECTFB_VER >= GST_DFBVIDEOSINK_VER (1,6,0))
1090     if (dfbvideosink->layer_mode == LAYER_MODE_ADMINISTRATIVE)
1091 #endif
1092       dfbvideosink->layer->EnableCursor (dfbvideosink->layer, FALSE);
1093     dfbvideosink->layer->Release (dfbvideosink->layer);
1094     dfbvideosink->layer = NULL;
1095   }
1096
1097   if (dfbvideosink->dfb) {
1098     dfbvideosink->dfb->Release (dfbvideosink->dfb);
1099     dfbvideosink->dfb = NULL;
1100   }
1101
1102   dfbvideosink->setup = FALSE;
1103 }
1104
1105 static DFBSurfacePixelFormat
1106 gst_dfbvideosink_get_format_from_caps (GstCaps * caps)
1107 {
1108   GstStructure *structure;
1109   DFBSurfacePixelFormat pixel_format = DSPF_UNKNOWN;
1110   const gchar *str;
1111   GstVideoFormat format;
1112
1113   g_return_val_if_fail (GST_IS_CAPS (caps), DSPF_UNKNOWN);
1114
1115   structure = gst_caps_get_structure (caps, 0);
1116   str = gst_structure_get_string (structure, "format");
1117   if (str == NULL) {
1118     GST_WARNING ("failed grabbing fourcc from caps %" GST_PTR_FORMAT, caps);
1119     return DSPF_UNKNOWN;
1120   }
1121
1122   format = gst_video_format_from_string (str);
1123   switch (format) {
1124     case GST_VIDEO_FORMAT_RGB16:
1125       pixel_format = DSPF_RGB16;
1126       break;
1127     case GST_VIDEO_FORMAT_RGB:
1128       pixel_format = DSPF_RGB24;
1129       break;
1130     case GST_VIDEO_FORMAT_xRGB:
1131       pixel_format = DSPF_RGB32;
1132       break;
1133     case GST_VIDEO_FORMAT_ARGB:
1134       pixel_format = DSPF_ARGB;
1135       break;
1136     case GST_VIDEO_FORMAT_I420:
1137       pixel_format = DSPF_I420;
1138       break;
1139     case GST_VIDEO_FORMAT_YV12:
1140       pixel_format = DSPF_YV12;
1141       break;
1142     case GST_VIDEO_FORMAT_YUY2:
1143       pixel_format = DSPF_YUY2;
1144       break;
1145     case GST_VIDEO_FORMAT_UYVY:
1146       pixel_format = DSPF_UYVY;
1147       break;
1148     case GST_VIDEO_FORMAT_NV12:
1149       pixel_format = DSPF_NV12;
1150       break;
1151     default:
1152       GST_WARNING ("unhandled pixel format %s", str);
1153       return DSPF_UNKNOWN;
1154   }
1155
1156   return pixel_format;
1157 }
1158
1159 static GstCaps *
1160 gst_dfbvideosink_get_caps_from_format (DFBSurfacePixelFormat format)
1161 {
1162   const char *fourcc;
1163
1164   g_return_val_if_fail (format != DSPF_UNKNOWN, NULL);
1165
1166   switch (format) {
1167     case DSPF_RGB16:
1168       fourcc = gst_video_format_to_string (GST_VIDEO_FORMAT_RGB16);
1169       break;
1170     case DSPF_RGB24:
1171       fourcc = gst_video_format_to_string (GST_VIDEO_FORMAT_RGB);
1172       break;
1173     case DSPF_RGB32:
1174       fourcc = gst_video_format_to_string (GST_VIDEO_FORMAT_xRGB);
1175       break;
1176     case DSPF_ARGB:
1177       fourcc = gst_video_format_to_string (GST_VIDEO_FORMAT_ARGB);
1178       break;
1179     case DSPF_YUY2:
1180       fourcc = gst_video_format_to_string (GST_VIDEO_FORMAT_YUY2);
1181       break;
1182     case DSPF_UYVY:
1183       fourcc = gst_video_format_to_string (GST_VIDEO_FORMAT_UYVY);
1184       break;
1185     case DSPF_I420:
1186       fourcc = gst_video_format_to_string (GST_VIDEO_FORMAT_I420);
1187       break;
1188     case DSPF_YV12:
1189       fourcc = gst_video_format_to_string (GST_VIDEO_FORMAT_YV12);
1190       break;
1191     case DSPF_NV12:
1192       fourcc = gst_video_format_to_string (GST_VIDEO_FORMAT_NV12);
1193       break;
1194     default:
1195       GST_WARNING ("unknown pixel format %s",
1196           gst_dfbvideosink_get_format_name (format));
1197       return NULL;
1198   }
1199
1200   return gst_caps_new_simple ("video/x-raw", "format", G_TYPE_STRING, fourcc,
1201       NULL);
1202 }
1203
1204 static gboolean
1205 gst_dfbvideosink_can_blit_from_format (GstDfbVideoSink * dfbvideosink,
1206     DFBSurfacePixelFormat format, gboolean accelerated)
1207 {
1208   gboolean res = FALSE;
1209   DFBResult ret;
1210   IDirectFBSurface *surface = NULL;
1211   DFBSurfaceDescription s_dsc;
1212   DFBAccelerationMask mask;
1213   DFBDisplayLayerConfig dlc, prev_dlc;
1214
1215   g_return_val_if_fail (GST_IS_DFBVIDEOSINK (dfbvideosink), FALSE);
1216
1217   /* Create a surface of desired format */
1218   s_dsc.flags = DSDESC_PIXELFORMAT | DSDESC_WIDTH | DSDESC_HEIGHT;
1219   s_dsc.pixelformat = format;
1220   s_dsc.width = 10;
1221   s_dsc.height = 10;
1222
1223   ret = dfbvideosink->dfb->CreateSurface (dfbvideosink->dfb, &s_dsc, &surface);
1224   if (ret != DFB_OK) {
1225     GST_WARNING_OBJECT (dfbvideosink, "failed creating surface with format %s",
1226         gst_dfbvideosink_get_format_name (format));
1227     goto beach;
1228   }
1229
1230   /* Backup layer configuration */
1231   ret = dfbvideosink->layer->GetConfiguration (dfbvideosink->layer, &prev_dlc);
1232   if (ret != DFB_OK) {
1233     GST_WARNING_OBJECT (dfbvideosink, "failed when getting current layer "
1234         "configuration");
1235     goto beach;
1236   }
1237
1238   /* Test configuration of the layer to this pixel format */
1239   dlc.flags = DLCONF_PIXELFORMAT;
1240   dlc.pixelformat = format;
1241
1242   ret = dfbvideosink->layer->TestConfiguration (dfbvideosink->layer, &dlc,
1243       NULL);
1244   if (ret != DFB_OK) {
1245     GST_DEBUG_OBJECT (dfbvideosink, "our layer refuses to operate in pixel "
1246         "format %s", gst_dfbvideosink_get_format_name (format));
1247     goto beach;
1248   }
1249
1250   ret = dfbvideosink->layer->SetConfiguration (dfbvideosink->layer, &dlc);
1251   if (ret != DFB_OK) {
1252     GST_WARNING_OBJECT (dfbvideosink, "our layer refuses to operate in pixel "
1253         "format, though this format was successfully tested earlied %s",
1254         gst_dfbvideosink_get_format_name (format));
1255     goto beach;
1256   }
1257
1258   ret = dfbvideosink->primary->GetAccelerationMask (dfbvideosink->primary,
1259       surface, &mask);
1260   if (ret != DFB_OK) {
1261     GST_WARNING_OBJECT (dfbvideosink, "failed getting acceleration mask");
1262     goto beach;
1263   }
1264
1265   /* Blitting from this format to our primary is accelerated */
1266   if ((mask & DFXL_BLIT) && accelerated) {
1267     GST_DEBUG_OBJECT (dfbvideosink, "blitting from format %s to our primary "
1268         "is accelerated", gst_dfbvideosink_get_format_name (format));
1269     res = TRUE;
1270   } else if (!accelerated) {
1271     GST_DEBUG_OBJECT (dfbvideosink, "blitting from format %s to our primary "
1272         "is not accelerated", gst_dfbvideosink_get_format_name (format));
1273     res = TRUE;
1274   }
1275
1276   /* Restore original layer configuration */
1277   ret = dfbvideosink->layer->SetConfiguration (dfbvideosink->layer, &prev_dlc);
1278   if (ret != DFB_OK) {
1279     GST_WARNING_OBJECT (dfbvideosink, "failed when restoring layer "
1280         "configuration");
1281     goto beach;
1282   }
1283
1284 beach:
1285   if (surface) {
1286     surface->Release (surface);
1287   }
1288   return res;
1289 }
1290
1291 static gboolean
1292 gst_dfbvideosink_get_best_vmode (GstDfbVideoSink * dfbvideosink, gint v_width,
1293     gint v_height, GstDfbVMode * best_vmode)
1294 {
1295   GSList *walk = NULL;
1296   gboolean ret = FALSE;
1297   gint width, height, bpp;
1298   GstDfbVMode *vmode = NULL;
1299
1300   g_return_val_if_fail (GST_IS_DFBVIDEOSINK (dfbvideosink), FALSE);
1301
1302   if (!dfbvideosink->vmodes) {
1303     goto beach;
1304   }
1305
1306   walk = dfbvideosink->vmodes;
1307
1308   vmode = (GstDfbVMode *) walk->data;
1309
1310   /* First mode */
1311   width = vmode->width;
1312   height = vmode->height;
1313   bpp = vmode->bpp;
1314
1315   while (walk) {
1316     gint wgap, hgap, best_wgap, best_hgap;
1317
1318     vmode = (GstDfbVMode *) walk->data;
1319
1320     /* What are the gaps */
1321     wgap = abs (vmode->width - v_width);
1322     hgap = abs (vmode->height - v_height);
1323     best_wgap = abs (width - v_width);
1324     best_hgap = abs (height - v_height);
1325
1326     /* If this mode is better we ll use that */
1327     if (wgap + hgap < best_wgap + best_hgap) {
1328       width = vmode->width;
1329       height = vmode->height;
1330       bpp = vmode->bpp;
1331     }
1332
1333     walk = g_slist_next (walk);
1334   }
1335
1336   GST_DEBUG_OBJECT (dfbvideosink, "found video mode %dx%d for input at %dx%d",
1337       width, height, v_width, v_height);
1338
1339   best_vmode->width = width;
1340   best_vmode->height = height;
1341   best_vmode->bpp = bpp;
1342
1343   ret = TRUE;
1344
1345 beach:
1346   return ret;
1347 }
1348
1349 static GstCaps *
1350 gst_dfbvideosink_getcaps (GstBaseSink * bsink, GstCaps * filter)
1351 {
1352   GstDfbVideoSink *dfbvideosink;
1353   GstCaps *caps = NULL;
1354   GstCaps *returned_caps;
1355   gint i;
1356
1357   dfbvideosink = GST_DFBVIDEOSINK (bsink);
1358
1359   if (!dfbvideosink->setup) {
1360     GstCaps *tcaps =
1361         gst_pad_get_pad_template_caps (GST_VIDEO_SINK_PAD (dfbvideosink));
1362     caps = gst_caps_copy (tcaps);
1363     gst_caps_unref (tcaps);
1364     GST_DEBUG_OBJECT (dfbvideosink, "getcaps called and we are not setup yet, "
1365         "returning template %" GST_PTR_FORMAT, caps);
1366     goto beach;
1367   } else {
1368     GST_DEBUG_OBJECT (dfbvideosink, "getcaps called, checking our internal "
1369         "format");
1370     if (dfbvideosink->ext_surface) {
1371       /* We are not rendering to our own surface, returning this surface's
1372        *  pixel format */
1373       caps = gst_dfbvideosink_get_caps_from_format (dfbvideosink->pixel_format);
1374     } else {
1375       /* Try some formats */
1376       gboolean accelerated = TRUE;
1377       caps = gst_caps_new_empty ();
1378
1379       do {
1380         if (gst_dfbvideosink_can_blit_from_format (dfbvideosink, DSPF_RGB16,
1381                 accelerated)) {
1382           gst_caps_append (caps,
1383               gst_dfbvideosink_get_caps_from_format (DSPF_RGB16));
1384         }
1385         if (gst_dfbvideosink_can_blit_from_format (dfbvideosink, DSPF_RGB24,
1386                 accelerated)) {
1387           gst_caps_append (caps,
1388               gst_dfbvideosink_get_caps_from_format (DSPF_RGB24));
1389         }
1390         if (gst_dfbvideosink_can_blit_from_format (dfbvideosink, DSPF_RGB32,
1391                 accelerated)) {
1392           gst_caps_append (caps,
1393               gst_dfbvideosink_get_caps_from_format (DSPF_RGB32));
1394         }
1395         if (gst_dfbvideosink_can_blit_from_format (dfbvideosink, DSPF_ARGB,
1396                 accelerated)) {
1397           gst_caps_append (caps,
1398               gst_dfbvideosink_get_caps_from_format (DSPF_ARGB));
1399         }
1400         if (gst_dfbvideosink_can_blit_from_format (dfbvideosink, DSPF_NV12,
1401                 accelerated)) {
1402           gst_caps_append (caps,
1403               gst_dfbvideosink_get_caps_from_format (DSPF_NV12));
1404         }
1405         if (gst_dfbvideosink_can_blit_from_format (dfbvideosink, DSPF_YUY2,
1406                 accelerated)) {
1407           gst_caps_append (caps,
1408               gst_dfbvideosink_get_caps_from_format (DSPF_YUY2));
1409         }
1410         if (gst_dfbvideosink_can_blit_from_format (dfbvideosink, DSPF_UYVY,
1411                 accelerated)) {
1412           gst_caps_append (caps,
1413               gst_dfbvideosink_get_caps_from_format (DSPF_UYVY));
1414         }
1415         if (gst_dfbvideosink_can_blit_from_format (dfbvideosink, DSPF_I420,
1416                 accelerated)) {
1417           gst_caps_append (caps,
1418               gst_dfbvideosink_get_caps_from_format (DSPF_I420));
1419         }
1420         if (gst_dfbvideosink_can_blit_from_format (dfbvideosink, DSPF_YV12,
1421                 accelerated)) {
1422           gst_caps_append (caps,
1423               gst_dfbvideosink_get_caps_from_format (DSPF_YV12));
1424         }
1425         accelerated = !accelerated;
1426       } while (accelerated == FALSE);
1427     }
1428   }
1429
1430   for (i = 0; i < gst_caps_get_size (caps); i++) {
1431     GstStructure *structure = gst_caps_get_structure (caps, i);
1432
1433     gst_structure_set (structure,
1434         "width", GST_TYPE_INT_RANGE, 1, G_MAXINT,
1435         "height", GST_TYPE_INT_RANGE, 1, G_MAXINT,
1436         "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
1437
1438     if (!dfbvideosink->hw_scaling && dfbvideosink->par) {
1439       int nom, den;
1440
1441       nom = gst_value_get_fraction_numerator (dfbvideosink->par);
1442       den = gst_value_get_fraction_denominator (dfbvideosink->par);
1443       gst_structure_set (structure, "pixel-aspect-ratio",
1444           GST_TYPE_FRACTION, nom, den, NULL);
1445     }
1446   }
1447
1448 beach:
1449   if (filter) {
1450     returned_caps = gst_caps_intersect_full (filter, caps,
1451         GST_CAPS_INTERSECT_FIRST);
1452     gst_caps_unref (caps);
1453   } else
1454     returned_caps = caps;
1455
1456   GST_DEBUG_OBJECT (dfbvideosink, "returning our caps %" GST_PTR_FORMAT,
1457       returned_caps);
1458
1459   return returned_caps;
1460 }
1461
1462 static gboolean
1463 gst_dfbvideosink_setcaps (GstBaseSink * bsink, GstCaps * caps)
1464 {
1465   GstDfbVideoSink *dfbvideosink;
1466   GstStructure *structure;
1467   gboolean res, result = FALSE;
1468   gint video_width, video_height;
1469   const GValue *framerate;
1470   DFBSurfacePixelFormat pixel_format = DSPF_UNKNOWN;
1471
1472   dfbvideosink = GST_DFBVIDEOSINK (bsink);
1473
1474   structure = gst_caps_get_structure (caps, 0);
1475   res = gst_structure_get_int (structure, "width", &video_width);
1476   res &= gst_structure_get_int (structure, "height", &video_height);
1477   framerate = gst_structure_get_value (structure, "framerate");
1478   res &= (framerate != NULL);
1479   if (!res) {
1480     goto beach;
1481   }
1482
1483   dfbvideosink->fps_n = gst_value_get_fraction_numerator (framerate);
1484   dfbvideosink->fps_d = gst_value_get_fraction_denominator (framerate);
1485
1486   pixel_format = gst_dfbvideosink_get_format_from_caps (caps);
1487
1488   GST_DEBUG_OBJECT (dfbvideosink, "setcaps called with %" GST_PTR_FORMAT, caps);
1489   GST_DEBUG_OBJECT (dfbvideosink, "our format is: %dx%d %s video at %d/%d fps",
1490       video_width, video_height,
1491       gst_dfbvideosink_get_format_name (pixel_format), dfbvideosink->fps_n,
1492       dfbvideosink->fps_d);
1493
1494   if (dfbvideosink->hw_scaling && dfbvideosink->par) {
1495     gint video_par_n, video_par_d;      /* video's PAR */
1496     gint display_par_n, display_par_d;  /* display's PAR */
1497     gint num, den;
1498     GValue display_ratio = { 0, };      /* display w/h ratio */
1499     const GValue *caps_par;
1500
1501     /* get aspect ratio from caps if it's present, and
1502      * convert video width and height to a display width and height
1503      * using wd / hd = wv / hv * PARv / PARd
1504      * the ratio wd / hd will be stored in display_ratio */
1505     g_value_init (&display_ratio, GST_TYPE_FRACTION);
1506
1507     /* get video's PAR */
1508     caps_par = gst_structure_get_value (structure, "pixel-aspect-ratio");
1509     if (caps_par) {
1510       video_par_n = gst_value_get_fraction_numerator (caps_par);
1511       video_par_d = gst_value_get_fraction_denominator (caps_par);
1512     } else {
1513       video_par_n = 1;
1514       video_par_d = 1;
1515     }
1516     /* get display's PAR */
1517     if (dfbvideosink->par) {
1518       display_par_n = gst_value_get_fraction_numerator (dfbvideosink->par);
1519       display_par_d = gst_value_get_fraction_denominator (dfbvideosink->par);
1520     } else {
1521       display_par_n = 1;
1522       display_par_d = 1;
1523     }
1524
1525     gst_value_set_fraction (&display_ratio,
1526         video_width * video_par_n * display_par_d,
1527         video_height * video_par_d * display_par_n);
1528
1529     num = gst_value_get_fraction_numerator (&display_ratio);
1530     den = gst_value_get_fraction_denominator (&display_ratio);
1531     GST_DEBUG_OBJECT (dfbvideosink,
1532         "video width/height: %dx%d, calculated display ratio: %d/%d",
1533         video_width, video_height, num, den);
1534
1535     /* now find a width x height that respects this display ratio.
1536      * prefer those that have one of w/h the same as the incoming video
1537      * using wd / hd = num / den */
1538
1539     /* start with same height, because of interlaced video */
1540     /* check hd / den is an integer scale factor, and scale wd with the PAR */
1541     if (video_height % den == 0) {
1542       GST_DEBUG_OBJECT (dfbvideosink, "keeping video height");
1543       GST_VIDEO_SINK_WIDTH (dfbvideosink) = video_height * num / den;
1544       GST_VIDEO_SINK_HEIGHT (dfbvideosink) = video_height;
1545     } else if (video_width % num == 0) {
1546       GST_DEBUG_OBJECT (dfbvideosink, "keeping video width");
1547       GST_VIDEO_SINK_WIDTH (dfbvideosink) = video_width;
1548       GST_VIDEO_SINK_HEIGHT (dfbvideosink) = video_width * den / num;
1549     } else {
1550       GST_DEBUG_OBJECT (dfbvideosink, "approximating while keeping height");
1551       GST_VIDEO_SINK_WIDTH (dfbvideosink) = video_height * num / den;
1552       GST_VIDEO_SINK_HEIGHT (dfbvideosink) = video_height;
1553     }
1554     GST_DEBUG_OBJECT (dfbvideosink, "scaling to %dx%d",
1555         GST_VIDEO_SINK_WIDTH (dfbvideosink),
1556         GST_VIDEO_SINK_HEIGHT (dfbvideosink));
1557   } else {
1558     if (dfbvideosink->par) {
1559       const GValue *par;
1560
1561       par = gst_structure_get_value (structure, "pixel-aspect-ratio");
1562       if (par) {
1563         if (gst_value_compare (par, dfbvideosink->par) != GST_VALUE_EQUAL) {
1564           goto wrong_aspect;
1565         }
1566       }
1567     }
1568     GST_VIDEO_SINK_WIDTH (dfbvideosink) = video_width;
1569     GST_VIDEO_SINK_HEIGHT (dfbvideosink) = video_height;
1570   }
1571
1572   /* Try to adapt the video mode to the video geometry */
1573   if (dfbvideosink->dfb) {
1574     DFBResult ret;
1575     GstDfbVMode vmode;
1576     DFBDisplayLayerConfig lc;
1577
1578     GST_DEBUG_OBJECT (dfbvideosink, "trying to adapt the video mode to video "
1579         "geometry");
1580
1581     /* Set video mode and layer configuration appropriately */
1582     if (gst_dfbvideosink_get_best_vmode (dfbvideosink,
1583             GST_VIDEO_SINK_WIDTH (dfbvideosink),
1584             GST_VIDEO_SINK_HEIGHT (dfbvideosink), &vmode)) {
1585       gint width, height, bpp;
1586
1587       width = vmode.width;
1588       height = vmode.height;
1589       bpp = vmode.bpp;
1590
1591       GST_DEBUG_OBJECT (dfbvideosink, "setting video mode to %dx%d at %d bpp",
1592           width, height, bpp);
1593
1594       ret = dfbvideosink->dfb->SetVideoMode (dfbvideosink->dfb, width,
1595           height, bpp);
1596       if (ret != DFB_OK) {
1597         GST_WARNING_OBJECT (dfbvideosink, "failed setting video mode %dx%d "
1598             "at %d bpp", width, height, bpp);
1599       }
1600     }
1601
1602     lc.flags = DLCONF_PIXELFORMAT;
1603     lc.pixelformat = pixel_format;
1604
1605     ret = dfbvideosink->layer->SetConfiguration (dfbvideosink->layer, &lc);
1606     if (ret != DFB_OK) {
1607       GST_WARNING_OBJECT (dfbvideosink, "failed setting layer pixelformat "
1608           "to %s", gst_dfbvideosink_get_format_name (pixel_format));
1609     } else {
1610       dfbvideosink->layer->GetConfiguration (dfbvideosink->layer, &lc);
1611       dfbvideosink->out_width = lc.width;
1612       dfbvideosink->out_height = lc.height;
1613       dfbvideosink->pixel_format = lc.pixelformat;
1614       GST_DEBUG_OBJECT (dfbvideosink, "layer %d now configured to %dx%d %s",
1615           dfbvideosink->layer_id, lc.width, lc.height,
1616           gst_dfbvideosink_get_format_name (lc.pixelformat));
1617     }
1618   }
1619
1620   if (pixel_format != dfbvideosink->pixel_format) {
1621     GST_WARNING_OBJECT (dfbvideosink, "setcaps sent us a different pixel "
1622         "format %s", gst_dfbvideosink_get_format_name (pixel_format));
1623     goto beach;
1624   }
1625
1626   dfbvideosink->video_width = video_width;
1627   dfbvideosink->video_height = video_height;
1628
1629   if (dfbvideosink->pool) {
1630     if (gst_buffer_pool_is_active (dfbvideosink->pool))
1631       gst_buffer_pool_set_active (dfbvideosink->pool, FALSE);
1632     gst_object_unref (dfbvideosink->pool);
1633   }
1634
1635   /* create a new buffer pool of DirectFB surface */
1636   dfbvideosink->pool = gst_dfb_buffer_pool_new (dfbvideosink);
1637
1638   structure = gst_buffer_pool_get_config (dfbvideosink->pool);
1639   gst_buffer_pool_config_set_params (structure, caps, 0, 0, 0);
1640   if (!gst_buffer_pool_set_config (dfbvideosink->pool, structure)) {
1641     GST_WARNING_OBJECT (dfbvideosink,
1642         "failed to set buffer pool configuration");
1643     goto beach;
1644   }
1645   if (!gst_buffer_pool_set_active (dfbvideosink->pool, TRUE)) {
1646     GST_WARNING_OBJECT (dfbvideosink, "failed to activate buffer pool");
1647     goto beach;
1648   }
1649
1650   result = TRUE;
1651
1652 beach:
1653   return result;
1654
1655 /* ERRORS */
1656 wrong_aspect:
1657   {
1658     GST_INFO_OBJECT (dfbvideosink, "pixel aspect ratio does not match");
1659     return FALSE;
1660   }
1661 }
1662
1663 static GstStateChangeReturn
1664 gst_dfbvideosink_change_state (GstElement * element, GstStateChange transition)
1665 {
1666   GstDfbVideoSink *dfbvideosink;
1667   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1668
1669   dfbvideosink = GST_DFBVIDEOSINK (element);
1670
1671   switch (transition) {
1672     case GST_STATE_CHANGE_NULL_TO_READY:
1673       dfbvideosink->running = TRUE;
1674       if (!dfbvideosink->setup) {
1675         if (!gst_dfbvideosink_setup (dfbvideosink)) {
1676           GST_DEBUG_OBJECT (dfbvideosink, "setup failed when changing state "
1677               "from NULL to READY");
1678           GST_ELEMENT_ERROR (dfbvideosink, RESOURCE, OPEN_WRITE,
1679               (NULL), ("Failed initializing DirectFB system"));
1680           return GST_STATE_CHANGE_FAILURE;
1681         }
1682       }
1683       break;
1684     case GST_STATE_CHANGE_READY_TO_PAUSED:
1685       /* Blank surface if we have one */
1686       if (dfbvideosink->ext_surface) {
1687         dfbvideosink->ext_surface->Clear (dfbvideosink->ext_surface,
1688             0x00, 0x00, 0x00, 0xFF);
1689       }
1690       if (dfbvideosink->primary) {
1691         dfbvideosink->primary->Clear (dfbvideosink->primary, 0x00, 0x00,
1692             0x00, 0xFF);
1693       }
1694       break;
1695     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1696       break;
1697     default:
1698       break;
1699   }
1700
1701   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1702   if (ret == GST_STATE_CHANGE_FAILURE)
1703     return ret;
1704
1705   switch (transition) {
1706     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1707       break;
1708     case GST_STATE_CHANGE_PAUSED_TO_READY:
1709       dfbvideosink->fps_d = 0;
1710       dfbvideosink->fps_n = 0;
1711       dfbvideosink->video_width = 0;
1712       dfbvideosink->video_height = 0;
1713       if (dfbvideosink->pool)
1714         gst_buffer_pool_set_active (dfbvideosink->pool, FALSE);
1715       break;
1716     case GST_STATE_CHANGE_READY_TO_NULL:
1717       dfbvideosink->running = FALSE;
1718       if (dfbvideosink->setup) {
1719         gst_dfbvideosink_cleanup (dfbvideosink);
1720       }
1721       break;
1722     default:
1723       break;
1724   }
1725
1726   return ret;
1727 }
1728
1729 static void
1730 gst_dfbvideosink_get_times (GstBaseSink * bsink, GstBuffer * buf,
1731     GstClockTime * start, GstClockTime * end)
1732 {
1733   GstDfbVideoSink *dfbvideosink;
1734
1735   dfbvideosink = GST_DFBVIDEOSINK (bsink);
1736
1737   if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
1738     *start = GST_BUFFER_TIMESTAMP (buf);
1739     if (GST_BUFFER_DURATION_IS_VALID (buf)) {
1740       *end = *start + GST_BUFFER_DURATION (buf);
1741     } else {
1742       if (dfbvideosink->fps_n > 0) {
1743         *end =
1744             *start + (GST_SECOND * dfbvideosink->fps_d) / dfbvideosink->fps_n;
1745       }
1746     }
1747   }
1748 }
1749
1750 static GstFlowReturn
1751 gst_dfbvideosink_show_frame (GstBaseSink * bsink, GstBuffer * buf)
1752 {
1753   GstDfbVideoSink *dfbvideosink = NULL;
1754   DFBResult res;
1755   GstVideoRectangle dst = { 0, };
1756   GstVideoRectangle src = { 0, };
1757   GstVideoRectangle result;
1758   GstFlowReturn ret = GST_FLOW_OK;
1759   gboolean mem_cpy = TRUE;
1760   GstMetaDfbSurface *meta;
1761
1762   dfbvideosink = GST_DFBVIDEOSINK (bsink);
1763
1764   if (!dfbvideosink->setup) {
1765     ret = GST_FLOW_EOS;
1766     goto beach;
1767   }
1768
1769   meta = GST_META_DFBSURFACE_GET (buf);
1770
1771   /* Is that a buffer we allocated ourselves ? */
1772   if (meta != NULL) {
1773     /* Does it have a surface ? */
1774     if (meta->surface) {
1775       mem_cpy = FALSE;
1776       GST_DEBUG_OBJECT (dfbvideosink, "we have a buffer (%p) we allocated "
1777           "ourselves and it has a surface, no memcpy then", buf);
1778     } else {
1779       /* No surface, that's a malloc */
1780       GST_DEBUG_OBJECT (dfbvideosink, "we have a buffer (%p) we allocated "
1781           "ourselves but it does not hold a surface", buf);
1782     }
1783   } else {
1784     /* Not our baby */
1785     GST_DEBUG_OBJECT (dfbvideosink, "we have a buffer (%p) we did not allocate",
1786         buf);
1787   }
1788
1789   if (mem_cpy) {
1790     IDirectFBSurface *dest = NULL, *surface = NULL;
1791     guint8 *data;
1792     gint dest_pitch, line;
1793     GstStructure *structure;
1794     GstCaps *caps;
1795     gint plane;
1796     GstVideoInfo src_info;
1797     GstVideoFrame src_frame;
1798     const gchar *str;
1799     GstVideoFormat format;
1800     guint offset[GST_VIDEO_MAX_PLANES] = { 0 };
1801     guint stride[GST_VIDEO_MAX_PLANES] = { 0 };
1802
1803     /* As we are not blitting no acceleration is possible. If the surface is
1804      * too small we do clipping, if it's too big we center. Theoretically as
1805      * we are using propose_allocation, there's a chance that we have been
1806      * able to do reverse caps negotiation */
1807
1808     if (dfbvideosink->ext_surface) {
1809       surface = dfbvideosink->ext_surface;
1810       GST_DEBUG_OBJECT (dfbvideosink, "memcpy to an external surface "
1811           "subsurface (vsync %d)", dfbvideosink->vsync);
1812     } else {
1813       surface = dfbvideosink->primary;
1814       GST_DEBUG_OBJECT (dfbvideosink, "memcpy to a primary subsurface "
1815           "(vsync %d)", dfbvideosink->vsync);
1816     }
1817
1818     /* Get the video frame geometry from the buffer caps */
1819     caps = gst_pad_get_current_caps (GST_BASE_SINK_PAD (bsink));
1820     structure = gst_caps_get_structure (caps, 0);
1821     if (structure) {
1822       gst_structure_get_int (structure, "width", &src.w);
1823       gst_structure_get_int (structure, "height", &src.h);
1824     } else {
1825       src.w = dfbvideosink->video_width;
1826       src.h = dfbvideosink->video_height;
1827     }
1828     gst_caps_unref (caps);
1829     surface->GetSize (surface, &dst.w, &dst.h);
1830
1831     /* Center / Clip */
1832     gst_video_sink_center_rect (src, dst, &result, FALSE);
1833
1834     res =
1835         surface->GetSubSurface (surface, (DFBRectangle *) (void *) &result,
1836         &dest);
1837     if (res != DFB_OK) {
1838       GST_WARNING_OBJECT (dfbvideosink, "failed when getting a sub surface");
1839       ret = GST_FLOW_EOS;
1840       goto beach;
1841     }
1842
1843     /* If we are not using Flip we wait for VSYNC before blit */
1844     if (!dfbvideosink->backbuffer && dfbvideosink->vsync) {
1845       dfbvideosink->layer->WaitForSync (dfbvideosink->layer);
1846     }
1847
1848     res = dest->Lock (dest, DSLF_WRITE, (void *) &data, &dest_pitch);
1849     if (res != DFB_OK) {
1850       GST_WARNING_OBJECT (dfbvideosink, "failed locking the external "
1851           "subsurface for writing");
1852       ret = GST_FLOW_ERROR;
1853       goto beach;
1854     }
1855
1856     caps = gst_pad_get_current_caps (GST_BASE_SINK_PAD (bsink));
1857     if (!gst_video_info_from_caps (&src_info, caps)) {
1858       GST_WARNING_OBJECT (dfbvideosink, "failed getting video info");
1859       gst_caps_unref (caps);
1860       ret = GST_FLOW_ERROR;
1861       goto beach;
1862     }
1863
1864     str = gst_structure_get_string (structure, "format");
1865     if (str == NULL) {
1866       GST_WARNING ("failed grabbing fourcc from caps %" GST_PTR_FORMAT, caps);
1867       gst_caps_unref (caps);
1868       ret = GST_FLOW_ERROR;
1869       goto beach;
1870     }
1871     format = gst_video_format_from_string (str);
1872
1873     gst_caps_unref (caps);
1874
1875     if (!gst_video_frame_map (&src_frame, &src_info, buf, GST_MAP_READ)) {
1876       GST_WARNING_OBJECT (dfbvideosink, "failed mapping frame");
1877       ret = GST_FLOW_ERROR;
1878       goto beach;
1879     }
1880
1881     switch (format) {
1882       case GST_VIDEO_FORMAT_I420:
1883       case GST_VIDEO_FORMAT_YV12:
1884         offset[1] = dest_pitch * ((dfbvideosink->out_height - result.y) +
1885             result.y / 4);
1886         offset[2] = offset[1] + dest_pitch * dfbvideosink->out_height / 4;
1887         stride[0] = dest_pitch;
1888         stride[1] = stride[2] = dest_pitch / 2;
1889         break;
1890       case GST_VIDEO_FORMAT_NV12:
1891         offset[1] = dest_pitch * (dfbvideosink->out_height - result.y / 2);
1892         stride[0] = stride[1] = dest_pitch;
1893         break;
1894       default:
1895         stride[0] = dest_pitch;
1896         break;
1897     }
1898
1899     line = 0;
1900     for (plane = 0; plane < src_info.finfo->n_planes; plane++) {
1901       guint plane_h;
1902       guint plane_line;
1903       guint8 *w_buf;
1904       guint size;
1905
1906       w_buf = data + offset[plane];
1907
1908       plane_h = GST_VIDEO_FRAME_COMP_HEIGHT (&src_frame, plane);
1909       size = MIN (src_info.stride[plane], stride[plane]);
1910
1911       /* Write each line respecting subsurface pitch */
1912       for (plane_line = 0; line < result.h || plane_line < plane_h;
1913           line++, plane_line++) {
1914         /* We do clipping */
1915         memcpy (w_buf, (gchar *) src_frame.data[plane] +
1916             (plane_line * src_info.stride[plane]), size);
1917         w_buf += stride[plane];
1918       }
1919     }
1920
1921     gst_video_frame_unmap (&src_frame);
1922
1923     dest->Unlock (dest);
1924
1925     dest->Release (dest);
1926
1927     if (dfbvideosink->backbuffer) {
1928       if (dfbvideosink->vsync) {
1929         surface->Flip (surface, NULL, DSFLIP_ONSYNC);
1930       } else {
1931         surface->Flip (surface, NULL, DSFLIP_NONE);
1932       }
1933     }
1934   } else {
1935     /* Else we will [Stretch]Blit to our primary */
1936     GST_DEBUG_OBJECT (dfbvideosink, "blitting to a primary surface (vsync %d)",
1937         dfbvideosink->vsync);
1938
1939     src.w = GST_VIDEO_SINK_WIDTH (dfbvideosink);
1940     src.h = GST_VIDEO_SINK_HEIGHT (dfbvideosink);
1941
1942     dfbvideosink->primary->GetSize (dfbvideosink->primary, &dst.w, &dst.h);
1943
1944     /* Unlocking surface before blit */
1945     if (meta->locked) {
1946       meta->surface->Unlock (meta->surface);
1947       meta->locked = FALSE;
1948     }
1949
1950     gst_video_sink_center_rect (src, dst, &result, dfbvideosink->hw_scaling);
1951
1952     /* If we are not using Flip we wait for VSYNC before blit */
1953     if (!dfbvideosink->backbuffer && dfbvideosink->vsync) {
1954       dfbvideosink->layer->WaitForSync (dfbvideosink->layer);
1955     }
1956
1957     if (dfbvideosink->hw_scaling) {
1958       dfbvideosink->primary->StretchBlit (dfbvideosink->primary,
1959           meta->surface, NULL, (DFBRectangle *) (void *) &result);
1960     } else {
1961       DFBRectangle clip;
1962
1963       clip.x = clip.y = 0;
1964       clip.w = result.w;
1965       clip.h = result.h;
1966       dfbvideosink->primary->Blit (dfbvideosink->primary, meta->surface,
1967           &clip, result.x, result.y);
1968     }
1969
1970     if (dfbvideosink->backbuffer) {
1971       if (dfbvideosink->vsync) {
1972         dfbvideosink->primary->Flip (dfbvideosink->primary, NULL,
1973             DSFLIP_ONSYNC);
1974       } else {
1975         dfbvideosink->primary->Flip (dfbvideosink->primary, NULL, DSFLIP_NONE);
1976       }
1977     }
1978   }
1979
1980 beach:
1981   return ret;
1982 }
1983
1984 static void
1985 gst_dfbvideosink_navigation_send_event (GstNavigation * navigation,
1986     GstStructure * structure)
1987 {
1988   GstDfbVideoSink *dfbvideosink = GST_DFBVIDEOSINK (navigation);
1989   GstEvent *event;
1990   GstVideoRectangle dst = { 0, };
1991   GstVideoRectangle src = { 0, };
1992   GstVideoRectangle result;
1993   double x, y, old_x, old_y;
1994   GstPad *pad = NULL;
1995
1996   src.w = GST_VIDEO_SINK_WIDTH (dfbvideosink);
1997   src.h = GST_VIDEO_SINK_HEIGHT (dfbvideosink);
1998   dst.w = dfbvideosink->out_width;
1999   dst.h = dfbvideosink->out_height;
2000   gst_video_sink_center_rect (src, dst, &result, dfbvideosink->hw_scaling);
2001
2002   event = gst_event_new_navigation (structure);
2003
2004   /* Our coordinates can be wrong here if we centered the video */
2005
2006   /* Converting pointer coordinates to the non scaled geometry */
2007   if (gst_structure_get_double (structure, "pointer_x", &old_x)) {
2008     x = old_x;
2009
2010     if (x >= result.x && x <= (result.x + result.w)) {
2011       x -= result.x;
2012       x *= dfbvideosink->video_width;
2013       x /= result.w;
2014     } else {
2015       x = 0;
2016     }
2017     GST_DEBUG_OBJECT (dfbvideosink, "translated navigation event x "
2018         "coordinate from %f to %f", old_x, x);
2019     gst_structure_set (structure, "pointer_x", G_TYPE_DOUBLE, x, NULL);
2020   }
2021   if (gst_structure_get_double (structure, "pointer_y", &old_y)) {
2022     y = old_y;
2023
2024     if (y >= result.y && y <= (result.y + result.h)) {
2025       y -= result.y;
2026       y *= dfbvideosink->video_height;
2027       y /= result.h;
2028     } else {
2029       y = 0;
2030     }
2031     GST_DEBUG_OBJECT (dfbvideosink, "translated navigation event y "
2032         "coordinate from %fd to %fd", old_y, y);
2033     gst_structure_set (structure, "pointer_y", G_TYPE_DOUBLE, y, NULL);
2034   }
2035
2036   pad = gst_pad_get_peer (GST_VIDEO_SINK_PAD (dfbvideosink));
2037
2038   if (GST_IS_PAD (pad) && GST_IS_EVENT (event)) {
2039     if (!gst_pad_send_event (pad, gst_event_ref (event))) {
2040       /* If upstream didn't handle the event we'll post a message with it
2041        * for the application in case it wants to do something with it */
2042       gst_element_post_message (GST_ELEMENT_CAST (dfbvideosink),
2043           gst_navigation_message_new_event (GST_OBJECT_CAST (dfbvideosink),
2044               event));
2045     }
2046     gst_event_unref (event);
2047     gst_object_unref (pad);
2048   }
2049 }
2050
2051 static void
2052 gst_dfbvideosink_navigation_init (GstNavigationInterface * iface)
2053 {
2054   iface->send_event = gst_dfbvideosink_navigation_send_event;
2055 }
2056
2057 static void
2058 gst_dfbvideosink_update_colorbalance (GstDfbVideoSink * dfbvideosink)
2059 {
2060   g_return_if_fail (GST_IS_DFBVIDEOSINK (dfbvideosink));
2061
2062   if (dfbvideosink->layer) {
2063     DFBColorAdjustment cb_adjust;
2064
2065     cb_adjust.flags = DCAF_NONE;
2066
2067     if (dfbvideosink->brightness >= 0) {
2068       cb_adjust.flags |= DCAF_BRIGHTNESS;
2069     }
2070     if (dfbvideosink->contrast >= 0) {
2071       cb_adjust.flags |= DCAF_CONTRAST;
2072     }
2073     if (dfbvideosink->hue >= 0) {
2074       cb_adjust.flags |= DCAF_HUE;
2075     }
2076     if (dfbvideosink->saturation >= 0) {
2077       cb_adjust.flags |= DCAF_SATURATION;
2078     }
2079
2080     cb_adjust.brightness = dfbvideosink->brightness;
2081     cb_adjust.contrast = dfbvideosink->contrast;
2082     cb_adjust.hue = dfbvideosink->hue;
2083     cb_adjust.saturation = dfbvideosink->saturation;
2084
2085     GST_DEBUG_OBJECT (dfbvideosink, "updating colorbalance: flags %d "
2086         "brightness %d contrast %d hue %d saturation %d", cb_adjust.flags,
2087         cb_adjust.brightness, cb_adjust.contrast, cb_adjust.hue,
2088         cb_adjust.saturation);
2089     dfbvideosink->layer->SetColorAdjustment (dfbvideosink->layer, &cb_adjust);
2090   }
2091 }
2092
2093 static const GList *
2094 gst_dfbvideosink_colorbalance_list_channels (GstColorBalance * balance)
2095 {
2096   GstDfbVideoSink *dfbvideosink = GST_DFBVIDEOSINK (balance);
2097
2098   g_return_val_if_fail (GST_IS_DFBVIDEOSINK (dfbvideosink), NULL);
2099
2100   return dfbvideosink->cb_channels;
2101 }
2102
2103 static void
2104 gst_dfbvideosink_colorbalance_set_value (GstColorBalance * balance,
2105     GstColorBalanceChannel * channel, gint value)
2106 {
2107   GstDfbVideoSink *dfbvideosink = GST_DFBVIDEOSINK (balance);
2108
2109   g_return_if_fail (GST_IS_DFBVIDEOSINK (dfbvideosink));
2110   g_return_if_fail (channel->label != NULL);
2111
2112   dfbvideosink->cb_changed = TRUE;
2113
2114   if (g_ascii_strcasecmp (channel->label, "HUE") == 0) {
2115     dfbvideosink->hue = value;
2116   } else if (g_ascii_strcasecmp (channel->label, "SATURATION") == 0) {
2117     dfbvideosink->saturation = value;
2118   } else if (g_ascii_strcasecmp (channel->label, "CONTRAST") == 0) {
2119     dfbvideosink->contrast = value;
2120   } else if (g_ascii_strcasecmp (channel->label, "BRIGHTNESS") == 0) {
2121     dfbvideosink->brightness = value;
2122   } else {
2123     GST_WARNING_OBJECT (dfbvideosink, "got an unknown channel %s",
2124         channel->label);
2125     return;
2126   }
2127
2128   gst_dfbvideosink_update_colorbalance (dfbvideosink);
2129 }
2130
2131 static gint
2132 gst_dfbvideosink_colorbalance_get_value (GstColorBalance * balance,
2133     GstColorBalanceChannel * channel)
2134 {
2135   GstDfbVideoSink *dfbvideosink = GST_DFBVIDEOSINK (balance);
2136   gint value = 0;
2137
2138   g_return_val_if_fail (GST_IS_DFBVIDEOSINK (dfbvideosink), 0);
2139   g_return_val_if_fail (channel->label != NULL, 0);
2140
2141   if (g_ascii_strcasecmp (channel->label, "HUE") == 0) {
2142     value = dfbvideosink->hue;
2143   } else if (g_ascii_strcasecmp (channel->label, "SATURATION") == 0) {
2144     value = dfbvideosink->saturation;
2145   } else if (g_ascii_strcasecmp (channel->label, "CONTRAST") == 0) {
2146     value = dfbvideosink->contrast;
2147   } else if (g_ascii_strcasecmp (channel->label, "BRIGHTNESS") == 0) {
2148     value = dfbvideosink->brightness;
2149   } else {
2150     GST_WARNING_OBJECT (dfbvideosink, "got an unknown channel %s",
2151         channel->label);
2152   }
2153
2154   return value;
2155 }
2156
2157 static GstColorBalanceType
2158 gst_dfbvideosink_colorbalance_get_balance_type (GstColorBalance * balance)
2159 {
2160   return GST_COLOR_BALANCE_HARDWARE;
2161 }
2162
2163 static void
2164 gst_dfbvideosink_colorbalance_init (GstColorBalanceInterface * iface)
2165 {
2166   iface->list_channels = gst_dfbvideosink_colorbalance_list_channels;
2167   iface->set_value = gst_dfbvideosink_colorbalance_set_value;
2168   iface->get_value = gst_dfbvideosink_colorbalance_get_value;
2169   iface->get_balance_type = gst_dfbvideosink_colorbalance_get_balance_type;
2170 }
2171
2172 /* Properties */
2173
2174 static void
2175 gst_dfbvideosink_set_property (GObject * object, guint prop_id,
2176     const GValue * value, GParamSpec * pspec)
2177 {
2178   GstDfbVideoSink *dfbvideosink;
2179
2180   g_return_if_fail (GST_IS_DFBVIDEOSINK (object));
2181   dfbvideosink = GST_DFBVIDEOSINK (object);
2182
2183   switch (prop_id) {
2184     case ARG_SURFACE:
2185       dfbvideosink->ext_surface = g_value_get_pointer (value);
2186       break;
2187     case ARG_HUE:
2188       dfbvideosink->hue = g_value_get_int (value);
2189       dfbvideosink->cb_changed = TRUE;
2190       gst_dfbvideosink_update_colorbalance (dfbvideosink);
2191       break;
2192     case ARG_CONTRAST:
2193       dfbvideosink->contrast = g_value_get_int (value);
2194       dfbvideosink->cb_changed = TRUE;
2195       gst_dfbvideosink_update_colorbalance (dfbvideosink);
2196       break;
2197     case ARG_BRIGHTNESS:
2198       dfbvideosink->brightness = g_value_get_int (value);
2199       dfbvideosink->cb_changed = TRUE;
2200       gst_dfbvideosink_update_colorbalance (dfbvideosink);
2201       break;
2202     case ARG_SATURATION:
2203       dfbvideosink->saturation = g_value_get_int (value);
2204       dfbvideosink->cb_changed = TRUE;
2205       gst_dfbvideosink_update_colorbalance (dfbvideosink);
2206       break;
2207     case ARG_PIXEL_ASPECT_RATIO:
2208       g_free (dfbvideosink->par);
2209       dfbvideosink->par = g_new0 (GValue, 1);
2210       g_value_init (dfbvideosink->par, GST_TYPE_FRACTION);
2211       if (!g_value_transform (value, dfbvideosink->par)) {
2212         GST_WARNING_OBJECT (dfbvideosink, "Could not transform string to "
2213             "aspect ratio");
2214         gst_value_set_fraction (dfbvideosink->par, 1, 1);
2215       }
2216       GST_DEBUG_OBJECT (dfbvideosink, "set PAR to %d/%d",
2217           gst_value_get_fraction_numerator (dfbvideosink->par),
2218           gst_value_get_fraction_denominator (dfbvideosink->par));
2219       break;
2220     case ARG_VSYNC:
2221       dfbvideosink->vsync = g_value_get_boolean (value);
2222       break;
2223     case ARG_LAYER_MODE:
2224       dfbvideosink->layer_mode = g_value_get_enum (value);
2225       break;
2226     default:
2227       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2228       break;
2229   }
2230 }
2231
2232 static void
2233 gst_dfbvideosink_get_property (GObject * object, guint prop_id,
2234     GValue * value, GParamSpec * pspec)
2235 {
2236   GstDfbVideoSink *dfbvideosink;
2237
2238   g_return_if_fail (GST_IS_DFBVIDEOSINK (object));
2239   dfbvideosink = GST_DFBVIDEOSINK (object);
2240
2241   switch (prop_id) {
2242     case ARG_HUE:
2243       g_value_set_int (value, dfbvideosink->hue);
2244       break;
2245     case ARG_CONTRAST:
2246       g_value_set_int (value, dfbvideosink->contrast);
2247       break;
2248     case ARG_BRIGHTNESS:
2249       g_value_set_int (value, dfbvideosink->brightness);
2250       break;
2251     case ARG_SATURATION:
2252       g_value_set_int (value, dfbvideosink->saturation);
2253       break;
2254     case ARG_PIXEL_ASPECT_RATIO:
2255       if (dfbvideosink->par)
2256         g_value_transform (dfbvideosink->par, value);
2257       break;
2258     case ARG_VSYNC:
2259       g_value_set_boolean (value, dfbvideosink->vsync);
2260       break;
2261     case ARG_LAYER_MODE:
2262       g_value_set_enum (value, dfbvideosink->layer_mode);
2263       break;
2264     default:
2265       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2266       break;
2267   }
2268 }
2269
2270 static gboolean
2271 gst_dfbvideosink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
2272 {
2273   GstDfbVideoSink *dfbvideosink;
2274   GstBufferPool *pool;
2275   GstCaps *caps;
2276   gboolean need_pool;
2277   guint size = 0;
2278
2279   dfbvideosink = GST_DFBVIDEOSINK (bsink);
2280
2281   gst_query_parse_allocation (query, &caps, &need_pool);
2282
2283   if (!caps) {
2284     GST_WARNING_OBJECT (dfbvideosink, "Missing caps in allocation query.");
2285     return FALSE;
2286   }
2287
2288   /* FIXME re-using buffer pool breaks renegotiation */
2289   if ((pool = dfbvideosink->pool))
2290     gst_object_ref (pool);
2291
2292   if (pool != NULL) {
2293     GstCaps *pcaps;
2294     GstStructure *config;
2295
2296     /* we had a pool, check caps */
2297     config = gst_buffer_pool_get_config (pool);
2298     gst_buffer_pool_config_get_params (config, &pcaps, &size, NULL, NULL);
2299
2300     GST_DEBUG_OBJECT (dfbvideosink,
2301         "buffer pool configuration caps %" GST_PTR_FORMAT, pcaps);
2302     if (!gst_caps_is_equal (caps, pcaps)) {
2303       gst_structure_free (config);
2304       gst_object_unref (pool);
2305       GST_WARNING_OBJECT (dfbvideosink, "pool has different caps");
2306       return FALSE;
2307     }
2308     gst_structure_free (config);
2309   } else {
2310     GstVideoInfo info;
2311
2312     if (!gst_video_info_from_caps (&info, caps)) {
2313       GST_WARNING_OBJECT (dfbvideosink,
2314           "Invalid video caps in allocation query");
2315       return FALSE;
2316     }
2317
2318     size = info.size;
2319   }
2320
2321   gst_query_add_allocation_pool (query, pool, size, 1, 0);
2322
2323   /* we also support various metadata */
2324   gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
2325
2326   if (pool)
2327     gst_object_unref (pool);
2328
2329   return TRUE;
2330 }
2331
2332 /* =========================================== */
2333 /*                                             */
2334 /*              Init & Class init              */
2335 /*                                             */
2336 /* =========================================== */
2337 static void
2338 gst_dfbvideosink_finalize (GObject * object)
2339 {
2340   GstDfbVideoSink *dfbvideosink;
2341
2342   dfbvideosink = GST_DFBVIDEOSINK (object);
2343
2344   if (dfbvideosink->par) {
2345     g_free (dfbvideosink->par);
2346     dfbvideosink->par = NULL;
2347   }
2348   if (dfbvideosink->setup) {
2349     gst_dfbvideosink_cleanup (dfbvideosink);
2350   }
2351
2352   G_OBJECT_CLASS (parent_class)->finalize (object);
2353 }
2354
2355 static void
2356 gst_dfbvideosink_init (GstDfbVideoSink * dfbvideosink)
2357 {
2358   dfbvideosink->pool = NULL;
2359
2360   dfbvideosink->video_height = dfbvideosink->out_height = 0;
2361   dfbvideosink->video_width = dfbvideosink->out_width = 0;
2362   dfbvideosink->fps_d = 0;
2363   dfbvideosink->fps_n = 0;
2364
2365   dfbvideosink->dfb = NULL;
2366   dfbvideosink->vmodes = NULL;
2367   dfbvideosink->layer_id = -1;
2368   dfbvideosink->layer = NULL;
2369   dfbvideosink->primary = NULL;
2370   dfbvideosink->event_buffer = NULL;
2371   dfbvideosink->event_thread = NULL;
2372
2373   dfbvideosink->ext_surface = NULL;
2374
2375   dfbvideosink->pixel_format = DSPF_UNKNOWN;
2376
2377   dfbvideosink->hw_scaling = FALSE;
2378   dfbvideosink->backbuffer = FALSE;
2379   dfbvideosink->vsync = TRUE;
2380   dfbvideosink->setup = FALSE;
2381   dfbvideosink->running = FALSE;
2382
2383   dfbvideosink->cb_channels = NULL;
2384   dfbvideosink->brightness = -1;
2385   dfbvideosink->contrast = -1;
2386   dfbvideosink->hue = -1;
2387   dfbvideosink->saturation = -1;
2388
2389   dfbvideosink->par = NULL;
2390
2391   dfbvideosink->layer_mode = DEFAULT_LAYER_MODE;
2392 }
2393
2394 static void
2395 gst_dfbvideosink_class_init (GstDfbVideoSinkClass * klass)
2396 {
2397   GObjectClass *gobject_class;
2398   GstElementClass *gstelement_class;
2399   GstBaseSinkClass *gstbasesink_class;
2400
2401   gobject_class = (GObjectClass *) klass;
2402   gstelement_class = (GstElementClass *) klass;
2403   gstbasesink_class = (GstBaseSinkClass *) klass;
2404
2405   parent_class = g_type_class_peek_parent (klass);
2406
2407   gobject_class->finalize = gst_dfbvideosink_finalize;
2408   gobject_class->set_property = gst_dfbvideosink_set_property;
2409   gobject_class->get_property = gst_dfbvideosink_get_property;
2410
2411   g_object_class_install_property (gobject_class, ARG_SURFACE,
2412       g_param_spec_pointer ("surface", "Surface",
2413           "The target surface for video",
2414           G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
2415   g_object_class_install_property (gobject_class, ARG_CONTRAST,
2416       g_param_spec_int ("contrast", "Contrast", "The contrast of the video",
2417           0x0000, 0xFFFF, 0x8000, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2418   g_object_class_install_property (gobject_class, ARG_BRIGHTNESS,
2419       g_param_spec_int ("brightness", "Brightness",
2420           "The brightness of the video", 0x0000, 0xFFFF, 0x8000,
2421           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2422   g_object_class_install_property (gobject_class, ARG_HUE,
2423       g_param_spec_int ("hue", "Hue", "The hue of the video", 0x0000, 0xFFFF,
2424           0x8000, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2425   g_object_class_install_property (gobject_class, ARG_SATURATION,
2426       g_param_spec_int ("saturation", "Saturation",
2427           "The saturation of the video", 0x0000, 0xFFFF, 0x8000,
2428           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2429   g_object_class_install_property (gobject_class, ARG_PIXEL_ASPECT_RATIO,
2430       g_param_spec_string ("pixel-aspect-ratio", "Pixel Aspect Ratio",
2431           "The pixel aspect ratio of the device", "1/1",
2432           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2433   g_object_class_install_property (gobject_class, ARG_VSYNC,
2434       g_param_spec_boolean ("vsync", "Vertical synchronisation",
2435           "Wait for next vertical sync to draw frames", TRUE,
2436           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2437   g_object_class_install_property (gobject_class, ARG_LAYER_MODE,
2438       g_param_spec_enum ("layer-mode",
2439           "The layer cooperative level (administrative or exclusive)",
2440           "The cooperative level handling the access permission (set this to "
2441           "'administrative' when the cursor is required)",
2442           gst_dfbvideosink_layer_mode_get_type (), DEFAULT_LAYER_MODE,
2443           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2444
2445   gst_type_mark_as_plugin_api (gst_dfbvideosink_layer_mode_get_type (), 0);
2446
2447   gst_element_class_set_static_metadata (gstelement_class,
2448       "DirectFB video sink", "Sink/Video", "A DirectFB based videosink",
2449       "Julien Moutte <julien@moutte.net>");
2450
2451   gst_element_class_add_static_pad_template (gstelement_class,
2452       &gst_dfbvideosink_sink_template_factory);
2453
2454   gstelement_class->change_state = gst_dfbvideosink_change_state;
2455
2456   gstbasesink_class->get_caps = gst_dfbvideosink_getcaps;
2457   gstbasesink_class->set_caps = gst_dfbvideosink_setcaps;
2458   gstbasesink_class->get_times = gst_dfbvideosink_get_times;
2459   gstbasesink_class->preroll = gst_dfbvideosink_show_frame;
2460   gstbasesink_class->render = gst_dfbvideosink_show_frame;
2461   gstbasesink_class->propose_allocation = gst_dfbvideosink_propose_allocation;
2462 }
2463
2464 static gboolean
2465 plugin_init (GstPlugin * plugin)
2466 {
2467   return GST_ELEMENT_REGISTER (dfbvideosink, plugin);
2468 }
2469
2470 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
2471     GST_VERSION_MINOR,
2472     directfb,
2473     "DirectFB video output plugin",
2474     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)