all: Use new navigation interface and API
[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_dfbvideosink_navigation_send_event
609                   (gst_navigation_event_new_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_dfbvideosink_navigation_send_event
620               (gst_navigation_event_new_mouse_button_press (event.input.button,
621                   x, y));
622         } else if (event.input.type == DIET_BUTTONRELEASE) {
623           gint x, y;
624
625           dfbvideosink->layer->GetCursorPosition (dfbvideosink->layer, &x, &y);
626
627           GST_DEBUG_OBJECT (dfbvideosink, "button %d released at %dx%d",
628               event.input.button, x, y);
629
630           gst_dfbvideosink_navigation_send_event
631               (gst_navigation_event_new_mouse_button_release
632               (event.input.button, x, y));
633         } else if (event.input.type == DIET_AXISMOTION) {
634           gint x, y;
635
636           dfbvideosink->layer->GetCursorPosition (dfbvideosink->layer, &x, &y);
637           gst_dfbvideosink_navigation_send_event
638               (gst_navigation_event_new_mouse_move (x, y));
639         } else {
640           GST_WARNING_OBJECT (dfbvideosink, "unhandled event type %d",
641               event.input.type);
642         }
643       }
644     }
645   }
646   return NULL;
647 }
648
649 static DFBEnumerationResult
650 gst_dfbvideosink_enum_layers (DFBDisplayLayerID id,
651     DFBDisplayLayerDescription desc, void *data)
652 {
653   GstDfbVideoSink *dfbvideosink = NULL;
654   IDirectFBDisplayLayer *layer = NULL;
655   DFBDisplayLayerConfig dlc;
656   DFBResult ret;
657   gboolean backbuffer = FALSE;
658
659   g_return_val_if_fail (GST_IS_DFBVIDEOSINK (data), DFENUM_CANCEL);
660
661   dfbvideosink = GST_DFBVIDEOSINK (data);
662
663   GST_DEBUG_OBJECT (dfbvideosink, "inspecting display layer %d with name: %s",
664       id, desc.name);
665
666   if ((desc.type & DLTF_VIDEO) && (desc.caps & DLCAPS_SURFACE)) {
667     GST_DEBUG_OBJECT (dfbvideosink,
668         "this layer can handle live video and has a surface");
669   } else {
670     if (desc.caps & DLCAPS_SURFACE) {
671       GST_DEBUG_OBJECT (dfbvideosink,
672           "this layer can not handle live video but has a surface");
673     } else {
674       GST_DEBUG_OBJECT (dfbvideosink, "no we can't use that layer, really...");
675       goto beach;
676     }
677   }
678
679   ret = dfbvideosink->dfb->GetDisplayLayer (dfbvideosink->dfb, id, &layer);
680   if (ret != DFB_OK) {
681     GST_WARNING_OBJECT (dfbvideosink, "failed getting display layer %s",
682         desc.name);
683     goto beach;
684   }
685
686   ret = layer->GetConfiguration (layer, &dlc);
687   if (ret != DFB_OK) {
688     GST_WARNING_OBJECT (dfbvideosink,
689         "failed getting display layer configuration");
690     goto beach;
691   }
692
693   if ((dlc.flags & DLCONF_BUFFERMODE) && (dlc.buffermode & DLBM_FRONTONLY)) {
694     GST_DEBUG_OBJECT (dfbvideosink, "no backbuffer");
695   }
696   if ((dlc.flags & DLCONF_BUFFERMODE) && (dlc.buffermode & DLBM_BACKVIDEO)) {
697     GST_DEBUG_OBJECT (dfbvideosink, "backbuffer is in video memory");
698     backbuffer = TRUE;
699   }
700   if ((dlc.flags & DLCONF_BUFFERMODE) && (dlc.buffermode & DLBM_BACKSYSTEM)) {
701     GST_DEBUG_OBJECT (dfbvideosink, "backbuffer is in system memory");
702     backbuffer = TRUE;
703   }
704   if ((dlc.flags & DLCONF_BUFFERMODE) && (dlc.buffermode & DLBM_TRIPLE)) {
705     GST_DEBUG_OBJECT (dfbvideosink, "triple buffering");
706     backbuffer = TRUE;
707   }
708
709   /* If the primary is suitable we prefer using it */
710   if (dfbvideosink->layer_id != DLID_PRIMARY) {
711     GST_DEBUG_OBJECT (dfbvideosink, "selecting layer named %s", desc.name);
712     dfbvideosink->layer_id = id;
713     dfbvideosink->backbuffer = backbuffer;
714   } else {
715     GST_DEBUG_OBJECT (dfbvideosink, "layer %s is suitable but the primary "
716         "is currently selected and we prefer that one", desc.name);
717   }
718
719 beach:
720   if (layer) {
721     layer->Release (layer);
722   }
723   return DFENUM_OK;
724 }
725
726 static DFBEnumerationResult
727 gst_dfbvideosink_enum_vmodes (gint width, gint height, gint bpp, void *data)
728 {
729   GstDfbVideoSink *dfbvideosink = NULL;
730   GstDfbVMode *vmode = NULL;
731
732   g_return_val_if_fail (GST_IS_DFBVIDEOSINK (data), DFENUM_CANCEL);
733
734   dfbvideosink = GST_DFBVIDEOSINK (data);
735
736   GST_DEBUG_OBJECT (dfbvideosink, "adding video mode %dx%d at %d bpp", width,
737       height, bpp);
738   vmode = g_new0 (GstDfbVMode, 1);
739   vmode->width = width;
740   vmode->height = height;
741   vmode->bpp = bpp;
742
743   /* We need to know the maximum video geometry we can accept for the caps */
744   if (width > dfbvideosink->out_width) {
745     dfbvideosink->out_width = width;
746   }
747   if (height > dfbvideosink->out_height) {
748     dfbvideosink->out_height = height;
749   }
750
751   dfbvideosink->vmodes = g_slist_append (dfbvideosink->vmodes, vmode);
752
753   return DFENUM_OK;
754 }
755
756 static DFBEnumerationResult
757 gst_dfbvideosink_enum_devices (DFBInputDeviceID id,
758     DFBInputDeviceDescription desc, void *data)
759 {
760   GstDfbVideoSink *dfbvideosink = NULL;
761   IDirectFBInputDevice *device = NULL;
762   DFBResult ret;
763
764   g_return_val_if_fail (GST_IS_DFBVIDEOSINK (data), DFENUM_CANCEL);
765
766   dfbvideosink = GST_DFBVIDEOSINK (data);
767
768   GST_DEBUG_OBJECT (dfbvideosink, "detected input device %s from vendor %s",
769       desc.name, desc.vendor);
770
771   /* Get that input device */
772   ret = dfbvideosink->dfb->GetInputDevice (dfbvideosink->dfb, id, &device);
773   if (ret != DFB_OK) {
774     GST_WARNING_OBJECT (dfbvideosink, "failed when getting input device id %d",
775         id);
776     goto beach;
777   }
778
779   ret = device->AttachEventBuffer (device, dfbvideosink->event_buffer);
780   if (ret != DFB_OK) {
781     GST_WARNING_OBJECT (dfbvideosink, "failed when attaching input device "
782         "%d to our event buffer", id);
783   }
784
785 beach:
786   if (device) {
787     device->Release (device);
788   }
789   return DFENUM_OK;
790 }
791
792 static gboolean
793 gst_dfbvideosink_setup (GstDfbVideoSink * dfbvideosink)
794 {
795   DFBResult ret;
796
797   g_return_val_if_fail (GST_IS_DFBVIDEOSINK (dfbvideosink), FALSE);
798
799   dfbvideosink->video_width = 0;
800   dfbvideosink->video_height = 0;
801   dfbvideosink->out_width = 0;
802   dfbvideosink->out_height = 0;
803   dfbvideosink->fps_d = 0;
804   dfbvideosink->fps_n = 0;
805   dfbvideosink->hw_scaling = FALSE;
806   dfbvideosink->backbuffer = FALSE;
807   dfbvideosink->pixel_format = DSPF_UNKNOWN;
808
809   /* If we do it all by ourself we create the DirectFB context, get the
810      primary layer and use a fullscreen configuration */
811   if (!dfbvideosink->ext_surface) {
812     GST_DEBUG_OBJECT (dfbvideosink, "no external surface, taking over "
813         "DirectFB fullscreen");
814     if (!dfbvideosink->dfb) {
815       DFBGraphicsDeviceDescription hw_caps;
816       char *argv[] = { (char *) "-", (char *) "--dfb:quiet",
817         (char *) "--dfb:no-sighandler", NULL
818       };
819       int argc = 3;
820       char **args;
821
822       GST_DEBUG_OBJECT (dfbvideosink, "initializing DirectFB");
823
824       args = argv;
825       ret = DirectFBInit (&argc, &args);
826
827       if (ret != DFB_OK) {
828         GST_WARNING_OBJECT (dfbvideosink, "DirectFB initialization failed");
829         goto beach;
830       }
831
832       ret = DirectFBCreate (&(dfbvideosink->dfb));
833
834       if (ret != DFB_OK) {
835         GST_WARNING_OBJECT (dfbvideosink, "failed creating the DirectFB "
836             "main object");
837         goto beach;
838       }
839
840       /* Get Hardware capabilities */
841       ret = dfbvideosink->dfb->GetDeviceDescription (dfbvideosink->dfb,
842           &hw_caps);
843
844       if (ret != DFB_OK) {
845         GST_WARNING_OBJECT (dfbvideosink, "failed grabbing the hardware "
846             "capabilities");
847         goto beach;
848       }
849
850       GST_DEBUG_OBJECT (dfbvideosink, "video card %s from vendor %s detected "
851           "with %d bytes of video memory", hw_caps.name, hw_caps.vendor,
852           hw_caps.video_memory);
853
854       if (hw_caps.acceleration_mask & DFXL_BLIT) {
855         GST_DEBUG_OBJECT (dfbvideosink, "Blit is accelerated");
856       }
857       if (hw_caps.acceleration_mask & DFXL_STRETCHBLIT) {
858         GST_DEBUG_OBJECT (dfbvideosink, "StretchBlit is accelerated");
859         dfbvideosink->hw_scaling = TRUE;
860       } else {
861         GST_DEBUG_OBJECT (dfbvideosink, "StretchBlit is not accelerated");
862         dfbvideosink->hw_scaling = FALSE;
863       }
864
865       dfbvideosink->layer_id = -1;
866
867       /* Inspect all the Display layers */
868       dfbvideosink->dfb->EnumDisplayLayers (dfbvideosink->dfb,
869           gst_dfbvideosink_enum_layers, dfbvideosink);
870       /* Inspect all Video modes */
871       dfbvideosink->dfb->EnumVideoModes (dfbvideosink->dfb,
872           gst_dfbvideosink_enum_vmodes, dfbvideosink);
873
874       /* Create an event buffer for input */
875       dfbvideosink->dfb->CreateEventBuffer (dfbvideosink->dfb,
876           &dfbvideosink->event_buffer);
877
878       /* Inspect all Input devices */
879       dfbvideosink->dfb->EnumInputDevices (dfbvideosink->dfb,
880           gst_dfbvideosink_enum_devices, dfbvideosink);
881       /* Create a thread to handle those events */
882       dfbvideosink->event_thread = g_thread_new ("dfbvsink-events",
883           (GThreadFunc) gst_dfbvideosink_event_thread, dfbvideosink);
884     }
885     if (!dfbvideosink->layer) {
886       GList *channels_list = NULL;
887       DFBDisplayLayerDescription dl_desc;
888
889       /* Get the best Display Layer */
890       ret = dfbvideosink->dfb->GetDisplayLayer (dfbvideosink->dfb,
891           dfbvideosink->layer_id, &dfbvideosink->layer);
892       if (ret != DFB_OK) {
893         GST_WARNING_OBJECT (dfbvideosink, "failed getting display layer");
894         goto beach;
895       }
896
897       if (dfbvideosink->layer_mode == LAYER_MODE_EXCLUSIVE ||
898           dfbvideosink->layer_mode == LAYER_MODE_ADMINISTRATIVE)
899         ret = dfbvideosink->layer->SetCooperativeLevel (dfbvideosink->layer,
900             dfbvideosink->layer_mode);
901       else {
902         GST_ERROR_OBJECT (dfbvideosink, "invalid layer cooperative level");
903         goto beach;
904       }
905
906       if (ret != DFB_OK) {
907         GST_WARNING_OBJECT (dfbvideosink, "failed setting display layer to "
908             "fullscreen mode");
909         goto beach;
910       }
911
912       dfbvideosink->layer->GetDescription (dfbvideosink->layer, &dl_desc);
913
914       /* Check that this layer is able to do colorbalance settings */
915       if (dl_desc.caps & DLCAPS_BRIGHTNESS) {
916         channels_list = g_list_append (channels_list, (char *) "BRIGHTNESS");
917       }
918       if (dl_desc.caps & DLCAPS_CONTRAST) {
919         channels_list = g_list_append (channels_list, (char *) "CONTRAST");
920       }
921       if (dl_desc.caps & DLCAPS_HUE) {
922         channels_list = g_list_append (channels_list, (char *) "HUE");
923       }
924       if (dl_desc.caps & DLCAPS_SATURATION) {
925         channels_list = g_list_append (channels_list, (char *) "SATURATION");
926       }
927
928       if (channels_list) {
929         GList *walk = channels_list;
930
931         /* Generate Color balance channel list */
932         while (walk) {
933           GstColorBalanceChannel *channel = NULL;
934
935           GST_DEBUG_OBJECT (dfbvideosink, "adding %s as a colorbalance channel",
936               (const char *) walk->data);
937
938           channel = g_object_new (GST_TYPE_COLOR_BALANCE_CHANNEL, NULL);
939           channel->label = g_strdup (walk->data);
940           channel->min_value = 0x0000;
941           channel->max_value = 0xFFFF;
942
943           dfbvideosink->cb_channels = g_list_append (dfbvideosink->cb_channels,
944               channel);
945
946           walk = g_list_next (walk);
947         }
948
949         /* If the colorbalance settings have not been touched we get current
950            values as defaults and update our internal variables */
951         if (!dfbvideosink->cb_changed) {
952           DFBColorAdjustment cb_adjust;
953
954           ret = dfbvideosink->layer->GetColorAdjustment (dfbvideosink->layer,
955               &cb_adjust);
956
957           if (ret != DFB_OK) {
958             GST_WARNING_OBJECT (dfbvideosink, "failed when getting color "
959                 "adjustment from layer");
960           }
961
962           if (cb_adjust.flags & DCAF_BRIGHTNESS) {
963             dfbvideosink->brightness = cb_adjust.brightness;
964           } else {
965             dfbvideosink->brightness = 0x8000;
966           }
967           if (cb_adjust.flags & DCAF_CONTRAST) {
968             dfbvideosink->contrast = cb_adjust.contrast;
969           } else {
970             dfbvideosink->contrast = 0x8000;
971           }
972           if (cb_adjust.flags & DCAF_HUE) {
973             dfbvideosink->hue = cb_adjust.hue;
974           } else {
975             dfbvideosink->hue = 0x8000;
976           }
977           if (cb_adjust.flags & DCAF_SATURATION) {
978             dfbvideosink->saturation = cb_adjust.saturation;
979           } else {
980             dfbvideosink->saturation = 0x8000;
981           }
982           GST_DEBUG_OBJECT (dfbvideosink, "brightness %d, contrast %d, "
983               "hue %d, saturation %d", dfbvideosink->brightness,
984               dfbvideosink->contrast, dfbvideosink->hue,
985               dfbvideosink->saturation);
986         }
987
988         g_list_free (channels_list);
989
990         gst_dfbvideosink_update_colorbalance (dfbvideosink);
991       }
992
993       dfbvideosink->layer->SetBackgroundColor (dfbvideosink->layer,
994           0x00, 0x00, 0x00, 0xFF);
995
996 #if (DIRECTFB_VER >= GST_DFBVIDEOSINK_VER (1,6,0))
997       if (dfbvideosink->layer_mode == LAYER_MODE_ADMINISTRATIVE)
998 #endif
999         dfbvideosink->layer->EnableCursor (dfbvideosink->layer, TRUE);
1000
1001       GST_DEBUG_OBJECT (dfbvideosink, "getting primary surface");
1002       dfbvideosink->layer->GetSurface (dfbvideosink->layer,
1003           &dfbvideosink->primary);
1004
1005       dfbvideosink->primary->SetBlittingFlags (dfbvideosink->primary,
1006           DSBLIT_NOFX);
1007     }
1008
1009     dfbvideosink->primary->GetPixelFormat (dfbvideosink->primary,
1010         &dfbvideosink->pixel_format);
1011   } else {
1012     DFBSurfaceCapabilities s_caps;
1013
1014     GST_DEBUG_OBJECT (dfbvideosink, "getting pixel format from foreign "
1015         "surface %p", dfbvideosink->ext_surface);
1016     dfbvideosink->ext_surface->GetPixelFormat (dfbvideosink->ext_surface,
1017         &dfbvideosink->pixel_format);
1018     dfbvideosink->ext_surface->GetSize (dfbvideosink->ext_surface,
1019         &dfbvideosink->out_width, &dfbvideosink->out_height);
1020     dfbvideosink->ext_surface->GetCapabilities (dfbvideosink->ext_surface,
1021         &s_caps);
1022     if ((s_caps & DSCAPS_DOUBLE) || (s_caps & DSCAPS_TRIPLE)) {
1023       dfbvideosink->backbuffer = TRUE;
1024     } else {
1025       dfbvideosink->backbuffer = FALSE;
1026     }
1027     GST_DEBUG_OBJECT (dfbvideosink, "external surface is %dx%d and uses %s "
1028         "pixel format", dfbvideosink->out_width, dfbvideosink->out_height,
1029         gst_dfbvideosink_get_format_name (dfbvideosink->pixel_format));
1030   }
1031
1032   dfbvideosink->setup = TRUE;
1033
1034 beach:
1035   return dfbvideosink->setup;
1036 }
1037
1038 static void
1039 gst_dfbvideosink_cleanup (GstDfbVideoSink * dfbvideosink)
1040 {
1041   g_return_if_fail (GST_IS_DFBVIDEOSINK (dfbvideosink));
1042
1043   GST_DEBUG_OBJECT (dfbvideosink, "cleaning up DirectFB environment");
1044
1045   /* Wait for our event thread */
1046   if (dfbvideosink->event_thread) {
1047     g_thread_join (dfbvideosink->event_thread);
1048     dfbvideosink->event_thread = NULL;
1049   }
1050
1051   if (dfbvideosink->event_buffer) {
1052     dfbvideosink->event_buffer->Release (dfbvideosink->event_buffer);
1053     dfbvideosink->event_buffer = NULL;
1054   }
1055
1056   if (dfbvideosink->vmodes) {
1057     GSList *walk = dfbvideosink->vmodes;
1058
1059     while (walk) {
1060       g_free (walk->data);
1061       walk = g_slist_next (walk);
1062     }
1063     g_slist_free (dfbvideosink->vmodes);
1064     dfbvideosink->vmodes = NULL;
1065   }
1066
1067   if (dfbvideosink->cb_channels) {
1068     GList *walk = dfbvideosink->cb_channels;
1069
1070     while (walk) {
1071       GstColorBalanceChannel *channel = walk->data;
1072
1073       g_object_unref (channel);
1074       walk = g_list_next (walk);
1075     }
1076     g_list_free (dfbvideosink->cb_channels);
1077     dfbvideosink->cb_channels = NULL;
1078   }
1079
1080   if (dfbvideosink->pool) {
1081     gst_object_unref (dfbvideosink->pool);
1082     dfbvideosink->pool = NULL;
1083   }
1084
1085   if (dfbvideosink->primary) {
1086     dfbvideosink->primary->Release (dfbvideosink->primary);
1087     dfbvideosink->primary = NULL;
1088   }
1089
1090   if (dfbvideosink->layer) {
1091 #if (DIRECTFB_VER >= GST_DFBVIDEOSINK_VER (1,6,0))
1092     if (dfbvideosink->layer_mode == LAYER_MODE_ADMINISTRATIVE)
1093 #endif
1094       dfbvideosink->layer->EnableCursor (dfbvideosink->layer, FALSE);
1095     dfbvideosink->layer->Release (dfbvideosink->layer);
1096     dfbvideosink->layer = NULL;
1097   }
1098
1099   if (dfbvideosink->dfb) {
1100     dfbvideosink->dfb->Release (dfbvideosink->dfb);
1101     dfbvideosink->dfb = NULL;
1102   }
1103
1104   dfbvideosink->setup = FALSE;
1105 }
1106
1107 static DFBSurfacePixelFormat
1108 gst_dfbvideosink_get_format_from_caps (GstCaps * caps)
1109 {
1110   GstStructure *structure;
1111   DFBSurfacePixelFormat pixel_format = DSPF_UNKNOWN;
1112   const gchar *str;
1113   GstVideoFormat format;
1114
1115   g_return_val_if_fail (GST_IS_CAPS (caps), DSPF_UNKNOWN);
1116
1117   structure = gst_caps_get_structure (caps, 0);
1118   str = gst_structure_get_string (structure, "format");
1119   if (str == NULL) {
1120     GST_WARNING ("failed grabbing fourcc from caps %" GST_PTR_FORMAT, caps);
1121     return DSPF_UNKNOWN;
1122   }
1123
1124   format = gst_video_format_from_string (str);
1125   switch (format) {
1126     case GST_VIDEO_FORMAT_RGB16:
1127       pixel_format = DSPF_RGB16;
1128       break;
1129     case GST_VIDEO_FORMAT_RGB:
1130       pixel_format = DSPF_RGB24;
1131       break;
1132     case GST_VIDEO_FORMAT_xRGB:
1133       pixel_format = DSPF_RGB32;
1134       break;
1135     case GST_VIDEO_FORMAT_ARGB:
1136       pixel_format = DSPF_ARGB;
1137       break;
1138     case GST_VIDEO_FORMAT_I420:
1139       pixel_format = DSPF_I420;
1140       break;
1141     case GST_VIDEO_FORMAT_YV12:
1142       pixel_format = DSPF_YV12;
1143       break;
1144     case GST_VIDEO_FORMAT_YUY2:
1145       pixel_format = DSPF_YUY2;
1146       break;
1147     case GST_VIDEO_FORMAT_UYVY:
1148       pixel_format = DSPF_UYVY;
1149       break;
1150     case GST_VIDEO_FORMAT_NV12:
1151       pixel_format = DSPF_NV12;
1152       break;
1153     default:
1154       GST_WARNING ("unhandled pixel format %s", str);
1155       return DSPF_UNKNOWN;
1156   }
1157
1158   return pixel_format;
1159 }
1160
1161 static GstCaps *
1162 gst_dfbvideosink_get_caps_from_format (DFBSurfacePixelFormat format)
1163 {
1164   const char *fourcc;
1165
1166   g_return_val_if_fail (format != DSPF_UNKNOWN, NULL);
1167
1168   switch (format) {
1169     case DSPF_RGB16:
1170       fourcc = gst_video_format_to_string (GST_VIDEO_FORMAT_RGB16);
1171       break;
1172     case DSPF_RGB24:
1173       fourcc = gst_video_format_to_string (GST_VIDEO_FORMAT_RGB);
1174       break;
1175     case DSPF_RGB32:
1176       fourcc = gst_video_format_to_string (GST_VIDEO_FORMAT_xRGB);
1177       break;
1178     case DSPF_ARGB:
1179       fourcc = gst_video_format_to_string (GST_VIDEO_FORMAT_ARGB);
1180       break;
1181     case DSPF_YUY2:
1182       fourcc = gst_video_format_to_string (GST_VIDEO_FORMAT_YUY2);
1183       break;
1184     case DSPF_UYVY:
1185       fourcc = gst_video_format_to_string (GST_VIDEO_FORMAT_UYVY);
1186       break;
1187     case DSPF_I420:
1188       fourcc = gst_video_format_to_string (GST_VIDEO_FORMAT_I420);
1189       break;
1190     case DSPF_YV12:
1191       fourcc = gst_video_format_to_string (GST_VIDEO_FORMAT_YV12);
1192       break;
1193     case DSPF_NV12:
1194       fourcc = gst_video_format_to_string (GST_VIDEO_FORMAT_NV12);
1195       break;
1196     default:
1197       GST_WARNING ("unknown pixel format %s",
1198           gst_dfbvideosink_get_format_name (format));
1199       return NULL;
1200   }
1201
1202   return gst_caps_new_simple ("video/x-raw", "format", G_TYPE_STRING, fourcc,
1203       NULL);
1204 }
1205
1206 static gboolean
1207 gst_dfbvideosink_can_blit_from_format (GstDfbVideoSink * dfbvideosink,
1208     DFBSurfacePixelFormat format, gboolean accelerated)
1209 {
1210   gboolean res = FALSE;
1211   DFBResult ret;
1212   IDirectFBSurface *surface = NULL;
1213   DFBSurfaceDescription s_dsc;
1214   DFBAccelerationMask mask;
1215   DFBDisplayLayerConfig dlc, prev_dlc;
1216
1217   g_return_val_if_fail (GST_IS_DFBVIDEOSINK (dfbvideosink), FALSE);
1218
1219   /* Create a surface of desired format */
1220   s_dsc.flags = DSDESC_PIXELFORMAT | DSDESC_WIDTH | DSDESC_HEIGHT;
1221   s_dsc.pixelformat = format;
1222   s_dsc.width = 10;
1223   s_dsc.height = 10;
1224
1225   ret = dfbvideosink->dfb->CreateSurface (dfbvideosink->dfb, &s_dsc, &surface);
1226   if (ret != DFB_OK) {
1227     GST_WARNING_OBJECT (dfbvideosink, "failed creating surface with format %s",
1228         gst_dfbvideosink_get_format_name (format));
1229     goto beach;
1230   }
1231
1232   /* Backup layer configuration */
1233   ret = dfbvideosink->layer->GetConfiguration (dfbvideosink->layer, &prev_dlc);
1234   if (ret != DFB_OK) {
1235     GST_WARNING_OBJECT (dfbvideosink, "failed when getting current layer "
1236         "configuration");
1237     goto beach;
1238   }
1239
1240   /* Test configuration of the layer to this pixel format */
1241   dlc.flags = DLCONF_PIXELFORMAT;
1242   dlc.pixelformat = format;
1243
1244   ret = dfbvideosink->layer->TestConfiguration (dfbvideosink->layer, &dlc,
1245       NULL);
1246   if (ret != DFB_OK) {
1247     GST_DEBUG_OBJECT (dfbvideosink, "our layer refuses to operate in pixel "
1248         "format %s", gst_dfbvideosink_get_format_name (format));
1249     goto beach;
1250   }
1251
1252   ret = dfbvideosink->layer->SetConfiguration (dfbvideosink->layer, &dlc);
1253   if (ret != DFB_OK) {
1254     GST_WARNING_OBJECT (dfbvideosink, "our layer refuses to operate in pixel "
1255         "format, though this format was successfully tested earlied %s",
1256         gst_dfbvideosink_get_format_name (format));
1257     goto beach;
1258   }
1259
1260   ret = dfbvideosink->primary->GetAccelerationMask (dfbvideosink->primary,
1261       surface, &mask);
1262   if (ret != DFB_OK) {
1263     GST_WARNING_OBJECT (dfbvideosink, "failed getting acceleration mask");
1264     goto beach;
1265   }
1266
1267   /* Blitting from this format to our primary is accelerated */
1268   if ((mask & DFXL_BLIT) && accelerated) {
1269     GST_DEBUG_OBJECT (dfbvideosink, "blitting from format %s to our primary "
1270         "is accelerated", gst_dfbvideosink_get_format_name (format));
1271     res = TRUE;
1272   } else if (!accelerated) {
1273     GST_DEBUG_OBJECT (dfbvideosink, "blitting from format %s to our primary "
1274         "is not accelerated", gst_dfbvideosink_get_format_name (format));
1275     res = TRUE;
1276   }
1277
1278   /* Restore original layer configuration */
1279   ret = dfbvideosink->layer->SetConfiguration (dfbvideosink->layer, &prev_dlc);
1280   if (ret != DFB_OK) {
1281     GST_WARNING_OBJECT (dfbvideosink, "failed when restoring layer "
1282         "configuration");
1283     goto beach;
1284   }
1285
1286 beach:
1287   if (surface) {
1288     surface->Release (surface);
1289   }
1290   return res;
1291 }
1292
1293 static gboolean
1294 gst_dfbvideosink_get_best_vmode (GstDfbVideoSink * dfbvideosink, gint v_width,
1295     gint v_height, GstDfbVMode * best_vmode)
1296 {
1297   GSList *walk = NULL;
1298   gboolean ret = FALSE;
1299   gint width, height, bpp;
1300   GstDfbVMode *vmode = NULL;
1301
1302   g_return_val_if_fail (GST_IS_DFBVIDEOSINK (dfbvideosink), FALSE);
1303
1304   if (!dfbvideosink->vmodes) {
1305     goto beach;
1306   }
1307
1308   walk = dfbvideosink->vmodes;
1309
1310   vmode = (GstDfbVMode *) walk->data;
1311
1312   /* First mode */
1313   width = vmode->width;
1314   height = vmode->height;
1315   bpp = vmode->bpp;
1316
1317   while (walk) {
1318     gint wgap, hgap, best_wgap, best_hgap;
1319
1320     vmode = (GstDfbVMode *) walk->data;
1321
1322     /* What are the gaps */
1323     wgap = abs (vmode->width - v_width);
1324     hgap = abs (vmode->height - v_height);
1325     best_wgap = abs (width - v_width);
1326     best_hgap = abs (height - v_height);
1327
1328     /* If this mode is better we ll use that */
1329     if (wgap + hgap < best_wgap + best_hgap) {
1330       width = vmode->width;
1331       height = vmode->height;
1332       bpp = vmode->bpp;
1333     }
1334
1335     walk = g_slist_next (walk);
1336   }
1337
1338   GST_DEBUG_OBJECT (dfbvideosink, "found video mode %dx%d for input at %dx%d",
1339       width, height, v_width, v_height);
1340
1341   best_vmode->width = width;
1342   best_vmode->height = height;
1343   best_vmode->bpp = bpp;
1344
1345   ret = TRUE;
1346
1347 beach:
1348   return ret;
1349 }
1350
1351 static GstCaps *
1352 gst_dfbvideosink_getcaps (GstBaseSink * bsink, GstCaps * filter)
1353 {
1354   GstDfbVideoSink *dfbvideosink;
1355   GstCaps *caps = NULL;
1356   GstCaps *returned_caps;
1357   gint i;
1358
1359   dfbvideosink = GST_DFBVIDEOSINK (bsink);
1360
1361   if (!dfbvideosink->setup) {
1362     GstCaps *tcaps =
1363         gst_pad_get_pad_template_caps (GST_VIDEO_SINK_PAD (dfbvideosink));
1364     caps = gst_caps_copy (tcaps);
1365     gst_caps_unref (tcaps);
1366     GST_DEBUG_OBJECT (dfbvideosink, "getcaps called and we are not setup yet, "
1367         "returning template %" GST_PTR_FORMAT, caps);
1368     goto beach;
1369   } else {
1370     GST_DEBUG_OBJECT (dfbvideosink, "getcaps called, checking our internal "
1371         "format");
1372     if (dfbvideosink->ext_surface) {
1373       /* We are not rendering to our own surface, returning this surface's
1374        *  pixel format */
1375       caps = gst_dfbvideosink_get_caps_from_format (dfbvideosink->pixel_format);
1376     } else {
1377       /* Try some formats */
1378       gboolean accelerated = TRUE;
1379       caps = gst_caps_new_empty ();
1380
1381       do {
1382         if (gst_dfbvideosink_can_blit_from_format (dfbvideosink, DSPF_RGB16,
1383                 accelerated)) {
1384           gst_caps_append (caps,
1385               gst_dfbvideosink_get_caps_from_format (DSPF_RGB16));
1386         }
1387         if (gst_dfbvideosink_can_blit_from_format (dfbvideosink, DSPF_RGB24,
1388                 accelerated)) {
1389           gst_caps_append (caps,
1390               gst_dfbvideosink_get_caps_from_format (DSPF_RGB24));
1391         }
1392         if (gst_dfbvideosink_can_blit_from_format (dfbvideosink, DSPF_RGB32,
1393                 accelerated)) {
1394           gst_caps_append (caps,
1395               gst_dfbvideosink_get_caps_from_format (DSPF_RGB32));
1396         }
1397         if (gst_dfbvideosink_can_blit_from_format (dfbvideosink, DSPF_ARGB,
1398                 accelerated)) {
1399           gst_caps_append (caps,
1400               gst_dfbvideosink_get_caps_from_format (DSPF_ARGB));
1401         }
1402         if (gst_dfbvideosink_can_blit_from_format (dfbvideosink, DSPF_NV12,
1403                 accelerated)) {
1404           gst_caps_append (caps,
1405               gst_dfbvideosink_get_caps_from_format (DSPF_NV12));
1406         }
1407         if (gst_dfbvideosink_can_blit_from_format (dfbvideosink, DSPF_YUY2,
1408                 accelerated)) {
1409           gst_caps_append (caps,
1410               gst_dfbvideosink_get_caps_from_format (DSPF_YUY2));
1411         }
1412         if (gst_dfbvideosink_can_blit_from_format (dfbvideosink, DSPF_UYVY,
1413                 accelerated)) {
1414           gst_caps_append (caps,
1415               gst_dfbvideosink_get_caps_from_format (DSPF_UYVY));
1416         }
1417         if (gst_dfbvideosink_can_blit_from_format (dfbvideosink, DSPF_I420,
1418                 accelerated)) {
1419           gst_caps_append (caps,
1420               gst_dfbvideosink_get_caps_from_format (DSPF_I420));
1421         }
1422         if (gst_dfbvideosink_can_blit_from_format (dfbvideosink, DSPF_YV12,
1423                 accelerated)) {
1424           gst_caps_append (caps,
1425               gst_dfbvideosink_get_caps_from_format (DSPF_YV12));
1426         }
1427         accelerated = !accelerated;
1428       } while (accelerated == FALSE);
1429     }
1430   }
1431
1432   for (i = 0; i < gst_caps_get_size (caps); i++) {
1433     GstStructure *structure = gst_caps_get_structure (caps, i);
1434
1435     gst_structure_set (structure,
1436         "width", GST_TYPE_INT_RANGE, 1, G_MAXINT,
1437         "height", GST_TYPE_INT_RANGE, 1, G_MAXINT,
1438         "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
1439
1440     if (!dfbvideosink->hw_scaling && dfbvideosink->par) {
1441       int nom, den;
1442
1443       nom = gst_value_get_fraction_numerator (dfbvideosink->par);
1444       den = gst_value_get_fraction_denominator (dfbvideosink->par);
1445       gst_structure_set (structure, "pixel-aspect-ratio",
1446           GST_TYPE_FRACTION, nom, den, NULL);
1447     }
1448   }
1449
1450 beach:
1451   if (filter) {
1452     returned_caps = gst_caps_intersect_full (filter, caps,
1453         GST_CAPS_INTERSECT_FIRST);
1454     gst_caps_unref (caps);
1455   } else
1456     returned_caps = caps;
1457
1458   GST_DEBUG_OBJECT (dfbvideosink, "returning our caps %" GST_PTR_FORMAT,
1459       returned_caps);
1460
1461   return returned_caps;
1462 }
1463
1464 static gboolean
1465 gst_dfbvideosink_setcaps (GstBaseSink * bsink, GstCaps * caps)
1466 {
1467   GstDfbVideoSink *dfbvideosink;
1468   GstStructure *structure;
1469   gboolean res, result = FALSE;
1470   gint video_width, video_height;
1471   const GValue *framerate;
1472   DFBSurfacePixelFormat pixel_format = DSPF_UNKNOWN;
1473
1474   dfbvideosink = GST_DFBVIDEOSINK (bsink);
1475
1476   structure = gst_caps_get_structure (caps, 0);
1477   res = gst_structure_get_int (structure, "width", &video_width);
1478   res &= gst_structure_get_int (structure, "height", &video_height);
1479   framerate = gst_structure_get_value (structure, "framerate");
1480   res &= (framerate != NULL);
1481   if (!res) {
1482     goto beach;
1483   }
1484
1485   dfbvideosink->fps_n = gst_value_get_fraction_numerator (framerate);
1486   dfbvideosink->fps_d = gst_value_get_fraction_denominator (framerate);
1487
1488   pixel_format = gst_dfbvideosink_get_format_from_caps (caps);
1489
1490   GST_DEBUG_OBJECT (dfbvideosink, "setcaps called with %" GST_PTR_FORMAT, caps);
1491   GST_DEBUG_OBJECT (dfbvideosink, "our format is: %dx%d %s video at %d/%d fps",
1492       video_width, video_height,
1493       gst_dfbvideosink_get_format_name (pixel_format), dfbvideosink->fps_n,
1494       dfbvideosink->fps_d);
1495
1496   if (dfbvideosink->hw_scaling && dfbvideosink->par) {
1497     gint video_par_n, video_par_d;      /* video's PAR */
1498     gint display_par_n, display_par_d;  /* display's PAR */
1499     gint num, den;
1500     GValue display_ratio = { 0, };      /* display w/h ratio */
1501     const GValue *caps_par;
1502
1503     /* get aspect ratio from caps if it's present, and
1504      * convert video width and height to a display width and height
1505      * using wd / hd = wv / hv * PARv / PARd
1506      * the ratio wd / hd will be stored in display_ratio */
1507     g_value_init (&display_ratio, GST_TYPE_FRACTION);
1508
1509     /* get video's PAR */
1510     caps_par = gst_structure_get_value (structure, "pixel-aspect-ratio");
1511     if (caps_par) {
1512       video_par_n = gst_value_get_fraction_numerator (caps_par);
1513       video_par_d = gst_value_get_fraction_denominator (caps_par);
1514     } else {
1515       video_par_n = 1;
1516       video_par_d = 1;
1517     }
1518     /* get display's PAR */
1519     if (dfbvideosink->par) {
1520       display_par_n = gst_value_get_fraction_numerator (dfbvideosink->par);
1521       display_par_d = gst_value_get_fraction_denominator (dfbvideosink->par);
1522     } else {
1523       display_par_n = 1;
1524       display_par_d = 1;
1525     }
1526
1527     gst_value_set_fraction (&display_ratio,
1528         video_width * video_par_n * display_par_d,
1529         video_height * video_par_d * display_par_n);
1530
1531     num = gst_value_get_fraction_numerator (&display_ratio);
1532     den = gst_value_get_fraction_denominator (&display_ratio);
1533     GST_DEBUG_OBJECT (dfbvideosink,
1534         "video width/height: %dx%d, calculated display ratio: %d/%d",
1535         video_width, video_height, num, den);
1536
1537     /* now find a width x height that respects this display ratio.
1538      * prefer those that have one of w/h the same as the incoming video
1539      * using wd / hd = num / den */
1540
1541     /* start with same height, because of interlaced video */
1542     /* check hd / den is an integer scale factor, and scale wd with the PAR */
1543     if (video_height % den == 0) {
1544       GST_DEBUG_OBJECT (dfbvideosink, "keeping video height");
1545       GST_VIDEO_SINK_WIDTH (dfbvideosink) = video_height * num / den;
1546       GST_VIDEO_SINK_HEIGHT (dfbvideosink) = video_height;
1547     } else if (video_width % num == 0) {
1548       GST_DEBUG_OBJECT (dfbvideosink, "keeping video width");
1549       GST_VIDEO_SINK_WIDTH (dfbvideosink) = video_width;
1550       GST_VIDEO_SINK_HEIGHT (dfbvideosink) = video_width * den / num;
1551     } else {
1552       GST_DEBUG_OBJECT (dfbvideosink, "approximating while keeping height");
1553       GST_VIDEO_SINK_WIDTH (dfbvideosink) = video_height * num / den;
1554       GST_VIDEO_SINK_HEIGHT (dfbvideosink) = video_height;
1555     }
1556     GST_DEBUG_OBJECT (dfbvideosink, "scaling to %dx%d",
1557         GST_VIDEO_SINK_WIDTH (dfbvideosink),
1558         GST_VIDEO_SINK_HEIGHT (dfbvideosink));
1559   } else {
1560     if (dfbvideosink->par) {
1561       const GValue *par;
1562
1563       par = gst_structure_get_value (structure, "pixel-aspect-ratio");
1564       if (par) {
1565         if (gst_value_compare (par, dfbvideosink->par) != GST_VALUE_EQUAL) {
1566           goto wrong_aspect;
1567         }
1568       }
1569     }
1570     GST_VIDEO_SINK_WIDTH (dfbvideosink) = video_width;
1571     GST_VIDEO_SINK_HEIGHT (dfbvideosink) = video_height;
1572   }
1573
1574   /* Try to adapt the video mode to the video geometry */
1575   if (dfbvideosink->dfb) {
1576     DFBResult ret;
1577     GstDfbVMode vmode;
1578     DFBDisplayLayerConfig lc;
1579
1580     GST_DEBUG_OBJECT (dfbvideosink, "trying to adapt the video mode to video "
1581         "geometry");
1582
1583     /* Set video mode and layer configuration appropriately */
1584     if (gst_dfbvideosink_get_best_vmode (dfbvideosink,
1585             GST_VIDEO_SINK_WIDTH (dfbvideosink),
1586             GST_VIDEO_SINK_HEIGHT (dfbvideosink), &vmode)) {
1587       gint width, height, bpp;
1588
1589       width = vmode.width;
1590       height = vmode.height;
1591       bpp = vmode.bpp;
1592
1593       GST_DEBUG_OBJECT (dfbvideosink, "setting video mode to %dx%d at %d bpp",
1594           width, height, bpp);
1595
1596       ret = dfbvideosink->dfb->SetVideoMode (dfbvideosink->dfb, width,
1597           height, bpp);
1598       if (ret != DFB_OK) {
1599         GST_WARNING_OBJECT (dfbvideosink, "failed setting video mode %dx%d "
1600             "at %d bpp", width, height, bpp);
1601       }
1602     }
1603
1604     lc.flags = DLCONF_PIXELFORMAT;
1605     lc.pixelformat = pixel_format;
1606
1607     ret = dfbvideosink->layer->SetConfiguration (dfbvideosink->layer, &lc);
1608     if (ret != DFB_OK) {
1609       GST_WARNING_OBJECT (dfbvideosink, "failed setting layer pixelformat "
1610           "to %s", gst_dfbvideosink_get_format_name (pixel_format));
1611     } else {
1612       dfbvideosink->layer->GetConfiguration (dfbvideosink->layer, &lc);
1613       dfbvideosink->out_width = lc.width;
1614       dfbvideosink->out_height = lc.height;
1615       dfbvideosink->pixel_format = lc.pixelformat;
1616       GST_DEBUG_OBJECT (dfbvideosink, "layer %d now configured to %dx%d %s",
1617           dfbvideosink->layer_id, lc.width, lc.height,
1618           gst_dfbvideosink_get_format_name (lc.pixelformat));
1619     }
1620   }
1621
1622   if (pixel_format != dfbvideosink->pixel_format) {
1623     GST_WARNING_OBJECT (dfbvideosink, "setcaps sent us a different pixel "
1624         "format %s", gst_dfbvideosink_get_format_name (pixel_format));
1625     goto beach;
1626   }
1627
1628   dfbvideosink->video_width = video_width;
1629   dfbvideosink->video_height = video_height;
1630
1631   if (dfbvideosink->pool) {
1632     if (gst_buffer_pool_is_active (dfbvideosink->pool))
1633       gst_buffer_pool_set_active (dfbvideosink->pool, FALSE);
1634     gst_object_unref (dfbvideosink->pool);
1635   }
1636
1637   /* create a new buffer pool of DirectFB surface */
1638   dfbvideosink->pool = gst_dfb_buffer_pool_new (dfbvideosink);
1639
1640   structure = gst_buffer_pool_get_config (dfbvideosink->pool);
1641   gst_buffer_pool_config_set_params (structure, caps, 0, 0, 0);
1642   if (!gst_buffer_pool_set_config (dfbvideosink->pool, structure)) {
1643     GST_WARNING_OBJECT (dfbvideosink,
1644         "failed to set buffer pool configuration");
1645     goto beach;
1646   }
1647   if (!gst_buffer_pool_set_active (dfbvideosink->pool, TRUE)) {
1648     GST_WARNING_OBJECT (dfbvideosink, "failed to activate buffer pool");
1649     goto beach;
1650   }
1651
1652   result = TRUE;
1653
1654 beach:
1655   return result;
1656
1657 /* ERRORS */
1658 wrong_aspect:
1659   {
1660     GST_INFO_OBJECT (dfbvideosink, "pixel aspect ratio does not match");
1661     return FALSE;
1662   }
1663 }
1664
1665 static GstStateChangeReturn
1666 gst_dfbvideosink_change_state (GstElement * element, GstStateChange transition)
1667 {
1668   GstDfbVideoSink *dfbvideosink;
1669   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1670
1671   dfbvideosink = GST_DFBVIDEOSINK (element);
1672
1673   switch (transition) {
1674     case GST_STATE_CHANGE_NULL_TO_READY:
1675       dfbvideosink->running = TRUE;
1676       if (!dfbvideosink->setup) {
1677         if (!gst_dfbvideosink_setup (dfbvideosink)) {
1678           GST_DEBUG_OBJECT (dfbvideosink, "setup failed when changing state "
1679               "from NULL to READY");
1680           GST_ELEMENT_ERROR (dfbvideosink, RESOURCE, OPEN_WRITE,
1681               (NULL), ("Failed initializing DirectFB system"));
1682           return GST_STATE_CHANGE_FAILURE;
1683         }
1684       }
1685       break;
1686     case GST_STATE_CHANGE_READY_TO_PAUSED:
1687       /* Blank surface if we have one */
1688       if (dfbvideosink->ext_surface) {
1689         dfbvideosink->ext_surface->Clear (dfbvideosink->ext_surface,
1690             0x00, 0x00, 0x00, 0xFF);
1691       }
1692       if (dfbvideosink->primary) {
1693         dfbvideosink->primary->Clear (dfbvideosink->primary, 0x00, 0x00,
1694             0x00, 0xFF);
1695       }
1696       break;
1697     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1698       break;
1699     default:
1700       break;
1701   }
1702
1703   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1704   if (ret == GST_STATE_CHANGE_FAILURE)
1705     return ret;
1706
1707   switch (transition) {
1708     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1709       break;
1710     case GST_STATE_CHANGE_PAUSED_TO_READY:
1711       dfbvideosink->fps_d = 0;
1712       dfbvideosink->fps_n = 0;
1713       dfbvideosink->video_width = 0;
1714       dfbvideosink->video_height = 0;
1715       if (dfbvideosink->pool)
1716         gst_buffer_pool_set_active (dfbvideosink->pool, FALSE);
1717       break;
1718     case GST_STATE_CHANGE_READY_TO_NULL:
1719       dfbvideosink->running = FALSE;
1720       if (dfbvideosink->setup) {
1721         gst_dfbvideosink_cleanup (dfbvideosink);
1722       }
1723       break;
1724     default:
1725       break;
1726   }
1727
1728   return ret;
1729 }
1730
1731 static void
1732 gst_dfbvideosink_get_times (GstBaseSink * bsink, GstBuffer * buf,
1733     GstClockTime * start, GstClockTime * end)
1734 {
1735   GstDfbVideoSink *dfbvideosink;
1736
1737   dfbvideosink = GST_DFBVIDEOSINK (bsink);
1738
1739   if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
1740     *start = GST_BUFFER_TIMESTAMP (buf);
1741     if (GST_BUFFER_DURATION_IS_VALID (buf)) {
1742       *end = *start + GST_BUFFER_DURATION (buf);
1743     } else {
1744       if (dfbvideosink->fps_n > 0) {
1745         *end =
1746             *start + (GST_SECOND * dfbvideosink->fps_d) / dfbvideosink->fps_n;
1747       }
1748     }
1749   }
1750 }
1751
1752 static GstFlowReturn
1753 gst_dfbvideosink_show_frame (GstBaseSink * bsink, GstBuffer * buf)
1754 {
1755   GstDfbVideoSink *dfbvideosink = NULL;
1756   DFBResult res;
1757   GstVideoRectangle dst = { 0, };
1758   GstVideoRectangle src = { 0, };
1759   GstVideoRectangle result;
1760   GstFlowReturn ret = GST_FLOW_OK;
1761   gboolean mem_cpy = TRUE;
1762   GstMetaDfbSurface *meta;
1763
1764   dfbvideosink = GST_DFBVIDEOSINK (bsink);
1765
1766   if (!dfbvideosink->setup) {
1767     ret = GST_FLOW_EOS;
1768     goto beach;
1769   }
1770
1771   meta = GST_META_DFBSURFACE_GET (buf);
1772
1773   /* Is that a buffer we allocated ourselves ? */
1774   if (meta != NULL) {
1775     /* Does it have a surface ? */
1776     if (meta->surface) {
1777       mem_cpy = FALSE;
1778       GST_DEBUG_OBJECT (dfbvideosink, "we have a buffer (%p) we allocated "
1779           "ourselves and it has a surface, no memcpy then", buf);
1780     } else {
1781       /* No surface, that's a malloc */
1782       GST_DEBUG_OBJECT (dfbvideosink, "we have a buffer (%p) we allocated "
1783           "ourselves but it does not hold a surface", buf);
1784     }
1785   } else {
1786     /* Not our baby */
1787     GST_DEBUG_OBJECT (dfbvideosink, "we have a buffer (%p) we did not allocate",
1788         buf);
1789   }
1790
1791   if (mem_cpy) {
1792     IDirectFBSurface *dest = NULL, *surface = NULL;
1793     guint8 *data;
1794     gint dest_pitch, line;
1795     GstStructure *structure;
1796     GstCaps *caps;
1797     gint plane;
1798     GstVideoInfo src_info;
1799     GstVideoFrame src_frame;
1800     const gchar *str;
1801     GstVideoFormat format;
1802     guint offset[GST_VIDEO_MAX_PLANES] = { 0 };
1803     guint stride[GST_VIDEO_MAX_PLANES] = { 0 };
1804
1805     /* As we are not blitting no acceleration is possible. If the surface is
1806      * too small we do clipping, if it's too big we center. Theoretically as
1807      * we are using propose_allocation, there's a chance that we have been
1808      * able to do reverse caps negotiation */
1809
1810     if (dfbvideosink->ext_surface) {
1811       surface = dfbvideosink->ext_surface;
1812       GST_DEBUG_OBJECT (dfbvideosink, "memcpy to an external surface "
1813           "subsurface (vsync %d)", dfbvideosink->vsync);
1814     } else {
1815       surface = dfbvideosink->primary;
1816       GST_DEBUG_OBJECT (dfbvideosink, "memcpy to a primary subsurface "
1817           "(vsync %d)", dfbvideosink->vsync);
1818     }
1819
1820     /* Get the video frame geometry from the buffer caps */
1821     caps = gst_pad_get_current_caps (GST_BASE_SINK_PAD (bsink));
1822     structure = gst_caps_get_structure (caps, 0);
1823     if (structure) {
1824       gst_structure_get_int (structure, "width", &src.w);
1825       gst_structure_get_int (structure, "height", &src.h);
1826     } else {
1827       src.w = dfbvideosink->video_width;
1828       src.h = dfbvideosink->video_height;
1829     }
1830     gst_caps_unref (caps);
1831     surface->GetSize (surface, &dst.w, &dst.h);
1832
1833     /* Center / Clip */
1834     gst_video_sink_center_rect (src, dst, &result, FALSE);
1835
1836     res =
1837         surface->GetSubSurface (surface, (DFBRectangle *) (void *) &result,
1838         &dest);
1839     if (res != DFB_OK) {
1840       GST_WARNING_OBJECT (dfbvideosink, "failed when getting a sub surface");
1841       ret = GST_FLOW_EOS;
1842       goto beach;
1843     }
1844
1845     /* If we are not using Flip we wait for VSYNC before blit */
1846     if (!dfbvideosink->backbuffer && dfbvideosink->vsync) {
1847       dfbvideosink->layer->WaitForSync (dfbvideosink->layer);
1848     }
1849
1850     res = dest->Lock (dest, DSLF_WRITE, (void *) &data, &dest_pitch);
1851     if (res != DFB_OK) {
1852       GST_WARNING_OBJECT (dfbvideosink, "failed locking the external "
1853           "subsurface for writing");
1854       ret = GST_FLOW_ERROR;
1855       goto beach;
1856     }
1857
1858     caps = gst_pad_get_current_caps (GST_BASE_SINK_PAD (bsink));
1859     if (!gst_video_info_from_caps (&src_info, caps)) {
1860       GST_WARNING_OBJECT (dfbvideosink, "failed getting video info");
1861       gst_caps_unref (caps);
1862       ret = GST_FLOW_ERROR;
1863       goto beach;
1864     }
1865
1866     str = gst_structure_get_string (structure, "format");
1867     if (str == NULL) {
1868       GST_WARNING ("failed grabbing fourcc from caps %" GST_PTR_FORMAT, caps);
1869       gst_caps_unref (caps);
1870       ret = GST_FLOW_ERROR;
1871       goto beach;
1872     }
1873     format = gst_video_format_from_string (str);
1874
1875     gst_caps_unref (caps);
1876
1877     if (!gst_video_frame_map (&src_frame, &src_info, buf, GST_MAP_READ)) {
1878       GST_WARNING_OBJECT (dfbvideosink, "failed mapping frame");
1879       ret = GST_FLOW_ERROR;
1880       goto beach;
1881     }
1882
1883     switch (format) {
1884       case GST_VIDEO_FORMAT_I420:
1885       case GST_VIDEO_FORMAT_YV12:
1886         offset[1] = dest_pitch * ((dfbvideosink->out_height - result.y) +
1887             result.y / 4);
1888         offset[2] = offset[1] + dest_pitch * dfbvideosink->out_height / 4;
1889         stride[0] = dest_pitch;
1890         stride[1] = stride[2] = dest_pitch / 2;
1891         break;
1892       case GST_VIDEO_FORMAT_NV12:
1893         offset[1] = dest_pitch * (dfbvideosink->out_height - result.y / 2);
1894         stride[0] = stride[1] = dest_pitch;
1895         break;
1896       default:
1897         stride[0] = dest_pitch;
1898         break;
1899     }
1900
1901     line = 0;
1902     for (plane = 0; plane < src_info.finfo->n_planes; plane++) {
1903       guint plane_h;
1904       guint plane_line;
1905       guint8 *w_buf;
1906       guint size;
1907
1908       w_buf = data + offset[plane];
1909
1910       plane_h = GST_VIDEO_FRAME_COMP_HEIGHT (&src_frame, plane);
1911       size = MIN (src_info.stride[plane], stride[plane]);
1912
1913       /* Write each line respecting subsurface pitch */
1914       for (plane_line = 0; line < result.h || plane_line < plane_h;
1915           line++, plane_line++) {
1916         /* We do clipping */
1917         memcpy (w_buf, (gchar *) src_frame.data[plane] +
1918             (plane_line * src_info.stride[plane]), size);
1919         w_buf += stride[plane];
1920       }
1921     }
1922
1923     gst_video_frame_unmap (&src_frame);
1924
1925     dest->Unlock (dest);
1926
1927     dest->Release (dest);
1928
1929     if (dfbvideosink->backbuffer) {
1930       if (dfbvideosink->vsync) {
1931         surface->Flip (surface, NULL, DSFLIP_ONSYNC);
1932       } else {
1933         surface->Flip (surface, NULL, DSFLIP_NONE);
1934       }
1935     }
1936   } else {
1937     /* Else we will [Stretch]Blit to our primary */
1938     GST_DEBUG_OBJECT (dfbvideosink, "blitting to a primary surface (vsync %d)",
1939         dfbvideosink->vsync);
1940
1941     src.w = GST_VIDEO_SINK_WIDTH (dfbvideosink);
1942     src.h = GST_VIDEO_SINK_HEIGHT (dfbvideosink);
1943
1944     dfbvideosink->primary->GetSize (dfbvideosink->primary, &dst.w, &dst.h);
1945
1946     /* Unlocking surface before blit */
1947     if (meta->locked) {
1948       meta->surface->Unlock (meta->surface);
1949       meta->locked = FALSE;
1950     }
1951
1952     gst_video_sink_center_rect (src, dst, &result, dfbvideosink->hw_scaling);
1953
1954     /* If we are not using Flip we wait for VSYNC before blit */
1955     if (!dfbvideosink->backbuffer && dfbvideosink->vsync) {
1956       dfbvideosink->layer->WaitForSync (dfbvideosink->layer);
1957     }
1958
1959     if (dfbvideosink->hw_scaling) {
1960       dfbvideosink->primary->StretchBlit (dfbvideosink->primary,
1961           meta->surface, NULL, (DFBRectangle *) (void *) &result);
1962     } else {
1963       DFBRectangle clip;
1964
1965       clip.x = clip.y = 0;
1966       clip.w = result.w;
1967       clip.h = result.h;
1968       dfbvideosink->primary->Blit (dfbvideosink->primary, meta->surface,
1969           &clip, result.x, result.y);
1970     }
1971
1972     if (dfbvideosink->backbuffer) {
1973       if (dfbvideosink->vsync) {
1974         dfbvideosink->primary->Flip (dfbvideosink->primary, NULL,
1975             DSFLIP_ONSYNC);
1976       } else {
1977         dfbvideosink->primary->Flip (dfbvideosink->primary, NULL, DSFLIP_NONE);
1978       }
1979     }
1980   }
1981
1982 beach:
1983   return ret;
1984 }
1985
1986 static void
1987 gst_dfbvideosink_navigation_send_event (GstNavigation * navigation,
1988     GstEvent * event)
1989 {
1990   GstDfbVideoSink *dfbvideosink = GST_DFBVIDEOSINK (navigation);
1991   GstVideoRectangle dst = { 0, };
1992   GstVideoRectangle src = { 0, };
1993   GstVideoRectangle result;
1994   double x, y, old_x, old_y;
1995   GstPad *pad = NULL;
1996
1997   src.w = GST_VIDEO_SINK_WIDTH (dfbvideosink);
1998   src.h = GST_VIDEO_SINK_HEIGHT (dfbvideosink);
1999   dst.w = dfbvideosink->out_width;
2000   dst.h = dfbvideosink->out_height;
2001   gst_video_sink_center_rect (src, dst, &result, dfbvideosink->hw_scaling);
2002
2003   event = gst_event_make_writable (event);
2004
2005   /* Our coordinates can be wrong here if we centered the video */
2006
2007   /* Converting pointer coordinates to the non scaled geometry */
2008   if gst_navigation_event_get_coordinates
2009     (event, &old_x, &old_y) {
2010     x = old_x;
2011     y = old_y;
2012
2013     if (x >= result.x && x <= (result.x + result.w)) {
2014       x -= result.x;
2015       x *= dfbvideosink->video_width;
2016       x /= result.w;
2017     } else {
2018       x = 0;
2019     }
2020     if (y >= result.y && y <= (result.y + result.h)) {
2021       y -= result.y;
2022       y *= dfbvideosink->video_height;
2023       y /= result.h;
2024     } else {
2025       y = 0;
2026     }
2027
2028     GST_DEBUG_OBJECT (dfbvideosink, "translated navigation event x "
2029         "coordinate from %f to %f", old_x, x);
2030     GST_DEBUG_OBJECT (dfbvideosink, "translated navigation event y "
2031         "coordinate from %fd to %fd", old_y, y);
2032     gst_navigation_event_set_coordinates (event, x, y);
2033     }
2034
2035   pad = gst_pad_get_peer (GST_VIDEO_SINK_PAD (dfbvideosink));
2036
2037   if (GST_IS_PAD (pad)) {
2038     if (!gst_pad_send_event (pad, gst_event_ref (event))) {
2039       /* If upstream didn't handle the event we'll post a message with it
2040        * for the application in case it wants to do something with it */
2041       gst_element_post_message (GST_ELEMENT_CAST (dfbvideosink),
2042           gst_navigation_message_new_event (GST_OBJECT_CAST (dfbvideosink),
2043               event));
2044     }
2045     gst_event_unref (event);
2046     gst_object_unref (pad);
2047   }
2048 }
2049
2050 static void
2051 gst_dfbvideosink_navigation_init (GstNavigationInterface * iface)
2052 {
2053   iface->send_event_simple = gst_dfbvideosink_navigation_send_event;
2054 }
2055
2056 static void
2057 gst_dfbvideosink_update_colorbalance (GstDfbVideoSink * dfbvideosink)
2058 {
2059   g_return_if_fail (GST_IS_DFBVIDEOSINK (dfbvideosink));
2060
2061   if (dfbvideosink->layer) {
2062     DFBColorAdjustment cb_adjust;
2063
2064     cb_adjust.flags = DCAF_NONE;
2065
2066     if (dfbvideosink->brightness >= 0) {
2067       cb_adjust.flags |= DCAF_BRIGHTNESS;
2068     }
2069     if (dfbvideosink->contrast >= 0) {
2070       cb_adjust.flags |= DCAF_CONTRAST;
2071     }
2072     if (dfbvideosink->hue >= 0) {
2073       cb_adjust.flags |= DCAF_HUE;
2074     }
2075     if (dfbvideosink->saturation >= 0) {
2076       cb_adjust.flags |= DCAF_SATURATION;
2077     }
2078
2079     cb_adjust.brightness = dfbvideosink->brightness;
2080     cb_adjust.contrast = dfbvideosink->contrast;
2081     cb_adjust.hue = dfbvideosink->hue;
2082     cb_adjust.saturation = dfbvideosink->saturation;
2083
2084     GST_DEBUG_OBJECT (dfbvideosink, "updating colorbalance: flags %d "
2085         "brightness %d contrast %d hue %d saturation %d", cb_adjust.flags,
2086         cb_adjust.brightness, cb_adjust.contrast, cb_adjust.hue,
2087         cb_adjust.saturation);
2088     dfbvideosink->layer->SetColorAdjustment (dfbvideosink->layer, &cb_adjust);
2089   }
2090 }
2091
2092 static const GList *
2093 gst_dfbvideosink_colorbalance_list_channels (GstColorBalance * balance)
2094 {
2095   GstDfbVideoSink *dfbvideosink = GST_DFBVIDEOSINK (balance);
2096
2097   g_return_val_if_fail (GST_IS_DFBVIDEOSINK (dfbvideosink), NULL);
2098
2099   return dfbvideosink->cb_channels;
2100 }
2101
2102 static void
2103 gst_dfbvideosink_colorbalance_set_value (GstColorBalance * balance,
2104     GstColorBalanceChannel * channel, gint value)
2105 {
2106   GstDfbVideoSink *dfbvideosink = GST_DFBVIDEOSINK (balance);
2107
2108   g_return_if_fail (GST_IS_DFBVIDEOSINK (dfbvideosink));
2109   g_return_if_fail (channel->label != NULL);
2110
2111   dfbvideosink->cb_changed = TRUE;
2112
2113   if (g_ascii_strcasecmp (channel->label, "HUE") == 0) {
2114     dfbvideosink->hue = value;
2115   } else if (g_ascii_strcasecmp (channel->label, "SATURATION") == 0) {
2116     dfbvideosink->saturation = value;
2117   } else if (g_ascii_strcasecmp (channel->label, "CONTRAST") == 0) {
2118     dfbvideosink->contrast = value;
2119   } else if (g_ascii_strcasecmp (channel->label, "BRIGHTNESS") == 0) {
2120     dfbvideosink->brightness = value;
2121   } else {
2122     GST_WARNING_OBJECT (dfbvideosink, "got an unknown channel %s",
2123         channel->label);
2124     return;
2125   }
2126
2127   gst_dfbvideosink_update_colorbalance (dfbvideosink);
2128 }
2129
2130 static gint
2131 gst_dfbvideosink_colorbalance_get_value (GstColorBalance * balance,
2132     GstColorBalanceChannel * channel)
2133 {
2134   GstDfbVideoSink *dfbvideosink = GST_DFBVIDEOSINK (balance);
2135   gint value = 0;
2136
2137   g_return_val_if_fail (GST_IS_DFBVIDEOSINK (dfbvideosink), 0);
2138   g_return_val_if_fail (channel->label != NULL, 0);
2139
2140   if (g_ascii_strcasecmp (channel->label, "HUE") == 0) {
2141     value = dfbvideosink->hue;
2142   } else if (g_ascii_strcasecmp (channel->label, "SATURATION") == 0) {
2143     value = dfbvideosink->saturation;
2144   } else if (g_ascii_strcasecmp (channel->label, "CONTRAST") == 0) {
2145     value = dfbvideosink->contrast;
2146   } else if (g_ascii_strcasecmp (channel->label, "BRIGHTNESS") == 0) {
2147     value = dfbvideosink->brightness;
2148   } else {
2149     GST_WARNING_OBJECT (dfbvideosink, "got an unknown channel %s",
2150         channel->label);
2151   }
2152
2153   return value;
2154 }
2155
2156 static GstColorBalanceType
2157 gst_dfbvideosink_colorbalance_get_balance_type (GstColorBalance * balance)
2158 {
2159   return GST_COLOR_BALANCE_HARDWARE;
2160 }
2161
2162 static void
2163 gst_dfbvideosink_colorbalance_init (GstColorBalanceInterface * iface)
2164 {
2165   iface->list_channels = gst_dfbvideosink_colorbalance_list_channels;
2166   iface->set_value = gst_dfbvideosink_colorbalance_set_value;
2167   iface->get_value = gst_dfbvideosink_colorbalance_get_value;
2168   iface->get_balance_type = gst_dfbvideosink_colorbalance_get_balance_type;
2169 }
2170
2171 /* Properties */
2172
2173 static void
2174 gst_dfbvideosink_set_property (GObject * object, guint prop_id,
2175     const GValue * value, GParamSpec * pspec)
2176 {
2177   GstDfbVideoSink *dfbvideosink;
2178
2179   g_return_if_fail (GST_IS_DFBVIDEOSINK (object));
2180   dfbvideosink = GST_DFBVIDEOSINK (object);
2181
2182   switch (prop_id) {
2183     case ARG_SURFACE:
2184       dfbvideosink->ext_surface = g_value_get_pointer (value);
2185       break;
2186     case ARG_HUE:
2187       dfbvideosink->hue = g_value_get_int (value);
2188       dfbvideosink->cb_changed = TRUE;
2189       gst_dfbvideosink_update_colorbalance (dfbvideosink);
2190       break;
2191     case ARG_CONTRAST:
2192       dfbvideosink->contrast = g_value_get_int (value);
2193       dfbvideosink->cb_changed = TRUE;
2194       gst_dfbvideosink_update_colorbalance (dfbvideosink);
2195       break;
2196     case ARG_BRIGHTNESS:
2197       dfbvideosink->brightness = g_value_get_int (value);
2198       dfbvideosink->cb_changed = TRUE;
2199       gst_dfbvideosink_update_colorbalance (dfbvideosink);
2200       break;
2201     case ARG_SATURATION:
2202       dfbvideosink->saturation = g_value_get_int (value);
2203       dfbvideosink->cb_changed = TRUE;
2204       gst_dfbvideosink_update_colorbalance (dfbvideosink);
2205       break;
2206     case ARG_PIXEL_ASPECT_RATIO:
2207       g_free (dfbvideosink->par);
2208       dfbvideosink->par = g_new0 (GValue, 1);
2209       g_value_init (dfbvideosink->par, GST_TYPE_FRACTION);
2210       if (!g_value_transform (value, dfbvideosink->par)) {
2211         GST_WARNING_OBJECT (dfbvideosink, "Could not transform string to "
2212             "aspect ratio");
2213         gst_value_set_fraction (dfbvideosink->par, 1, 1);
2214       }
2215       GST_DEBUG_OBJECT (dfbvideosink, "set PAR to %d/%d",
2216           gst_value_get_fraction_numerator (dfbvideosink->par),
2217           gst_value_get_fraction_denominator (dfbvideosink->par));
2218       break;
2219     case ARG_VSYNC:
2220       dfbvideosink->vsync = g_value_get_boolean (value);
2221       break;
2222     case ARG_LAYER_MODE:
2223       dfbvideosink->layer_mode = g_value_get_enum (value);
2224       break;
2225     default:
2226       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2227       break;
2228   }
2229 }
2230
2231 static void
2232 gst_dfbvideosink_get_property (GObject * object, guint prop_id,
2233     GValue * value, GParamSpec * pspec)
2234 {
2235   GstDfbVideoSink *dfbvideosink;
2236
2237   g_return_if_fail (GST_IS_DFBVIDEOSINK (object));
2238   dfbvideosink = GST_DFBVIDEOSINK (object);
2239
2240   switch (prop_id) {
2241     case ARG_HUE:
2242       g_value_set_int (value, dfbvideosink->hue);
2243       break;
2244     case ARG_CONTRAST:
2245       g_value_set_int (value, dfbvideosink->contrast);
2246       break;
2247     case ARG_BRIGHTNESS:
2248       g_value_set_int (value, dfbvideosink->brightness);
2249       break;
2250     case ARG_SATURATION:
2251       g_value_set_int (value, dfbvideosink->saturation);
2252       break;
2253     case ARG_PIXEL_ASPECT_RATIO:
2254       if (dfbvideosink->par)
2255         g_value_transform (dfbvideosink->par, value);
2256       break;
2257     case ARG_VSYNC:
2258       g_value_set_boolean (value, dfbvideosink->vsync);
2259       break;
2260     case ARG_LAYER_MODE:
2261       g_value_set_enum (value, dfbvideosink->layer_mode);
2262       break;
2263     default:
2264       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2265       break;
2266   }
2267 }
2268
2269 static gboolean
2270 gst_dfbvideosink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
2271 {
2272   GstDfbVideoSink *dfbvideosink;
2273   GstBufferPool *pool;
2274   GstCaps *caps;
2275   gboolean need_pool;
2276   guint size = 0;
2277
2278   dfbvideosink = GST_DFBVIDEOSINK (bsink);
2279
2280   gst_query_parse_allocation (query, &caps, &need_pool);
2281
2282   if (!caps) {
2283     GST_WARNING_OBJECT (dfbvideosink, "Missing caps in allocation query.");
2284     return FALSE;
2285   }
2286
2287   /* FIXME re-using buffer pool breaks renegotiation */
2288   if ((pool = dfbvideosink->pool))
2289     gst_object_ref (pool);
2290
2291   if (pool != NULL) {
2292     GstCaps *pcaps;
2293     GstStructure *config;
2294
2295     /* we had a pool, check caps */
2296     config = gst_buffer_pool_get_config (pool);
2297     gst_buffer_pool_config_get_params (config, &pcaps, &size, NULL, NULL);
2298
2299     GST_DEBUG_OBJECT (dfbvideosink,
2300         "buffer pool configuration caps %" GST_PTR_FORMAT, pcaps);
2301     if (!gst_caps_is_equal (caps, pcaps)) {
2302       gst_structure_free (config);
2303       gst_object_unref (pool);
2304       GST_WARNING_OBJECT (dfbvideosink, "pool has different caps");
2305       return FALSE;
2306     }
2307     gst_structure_free (config);
2308   } else {
2309     GstVideoInfo info;
2310
2311     if (!gst_video_info_from_caps (&info, caps)) {
2312       GST_WARNING_OBJECT (dfbvideosink,
2313           "Invalid video caps in allocation query");
2314       return FALSE;
2315     }
2316
2317     size = info.size;
2318   }
2319
2320   gst_query_add_allocation_pool (query, pool, size, 1, 0);
2321
2322   /* we also support various metadata */
2323   gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
2324
2325   if (pool)
2326     gst_object_unref (pool);
2327
2328   return TRUE;
2329 }
2330
2331 /* =========================================== */
2332 /*                                             */
2333 /*              Init & Class init              */
2334 /*                                             */
2335 /* =========================================== */
2336 static void
2337 gst_dfbvideosink_finalize (GObject * object)
2338 {
2339   GstDfbVideoSink *dfbvideosink;
2340
2341   dfbvideosink = GST_DFBVIDEOSINK (object);
2342
2343   if (dfbvideosink->par) {
2344     g_free (dfbvideosink->par);
2345     dfbvideosink->par = NULL;
2346   }
2347   if (dfbvideosink->setup) {
2348     gst_dfbvideosink_cleanup (dfbvideosink);
2349   }
2350
2351   G_OBJECT_CLASS (parent_class)->finalize (object);
2352 }
2353
2354 static void
2355 gst_dfbvideosink_init (GstDfbVideoSink * dfbvideosink)
2356 {
2357   dfbvideosink->pool = NULL;
2358
2359   dfbvideosink->video_height = dfbvideosink->out_height = 0;
2360   dfbvideosink->video_width = dfbvideosink->out_width = 0;
2361   dfbvideosink->fps_d = 0;
2362   dfbvideosink->fps_n = 0;
2363
2364   dfbvideosink->dfb = NULL;
2365   dfbvideosink->vmodes = NULL;
2366   dfbvideosink->layer_id = -1;
2367   dfbvideosink->layer = NULL;
2368   dfbvideosink->primary = NULL;
2369   dfbvideosink->event_buffer = NULL;
2370   dfbvideosink->event_thread = NULL;
2371
2372   dfbvideosink->ext_surface = NULL;
2373
2374   dfbvideosink->pixel_format = DSPF_UNKNOWN;
2375
2376   dfbvideosink->hw_scaling = FALSE;
2377   dfbvideosink->backbuffer = FALSE;
2378   dfbvideosink->vsync = TRUE;
2379   dfbvideosink->setup = FALSE;
2380   dfbvideosink->running = FALSE;
2381
2382   dfbvideosink->cb_channels = NULL;
2383   dfbvideosink->brightness = -1;
2384   dfbvideosink->contrast = -1;
2385   dfbvideosink->hue = -1;
2386   dfbvideosink->saturation = -1;
2387
2388   dfbvideosink->par = NULL;
2389
2390   dfbvideosink->layer_mode = DEFAULT_LAYER_MODE;
2391 }
2392
2393 static void
2394 gst_dfbvideosink_class_init (GstDfbVideoSinkClass * klass)
2395 {
2396   GObjectClass *gobject_class;
2397   GstElementClass *gstelement_class;
2398   GstBaseSinkClass *gstbasesink_class;
2399
2400   gobject_class = (GObjectClass *) klass;
2401   gstelement_class = (GstElementClass *) klass;
2402   gstbasesink_class = (GstBaseSinkClass *) klass;
2403
2404   parent_class = g_type_class_peek_parent (klass);
2405
2406   gobject_class->finalize = gst_dfbvideosink_finalize;
2407   gobject_class->set_property = gst_dfbvideosink_set_property;
2408   gobject_class->get_property = gst_dfbvideosink_get_property;
2409
2410   g_object_class_install_property (gobject_class, ARG_SURFACE,
2411       g_param_spec_pointer ("surface", "Surface",
2412           "The target surface for video",
2413           G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
2414   g_object_class_install_property (gobject_class, ARG_CONTRAST,
2415       g_param_spec_int ("contrast", "Contrast", "The contrast of the video",
2416           0x0000, 0xFFFF, 0x8000, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2417   g_object_class_install_property (gobject_class, ARG_BRIGHTNESS,
2418       g_param_spec_int ("brightness", "Brightness",
2419           "The brightness of the video", 0x0000, 0xFFFF, 0x8000,
2420           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2421   g_object_class_install_property (gobject_class, ARG_HUE,
2422       g_param_spec_int ("hue", "Hue", "The hue of the video", 0x0000, 0xFFFF,
2423           0x8000, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2424   g_object_class_install_property (gobject_class, ARG_SATURATION,
2425       g_param_spec_int ("saturation", "Saturation",
2426           "The saturation of the video", 0x0000, 0xFFFF, 0x8000,
2427           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2428   g_object_class_install_property (gobject_class, ARG_PIXEL_ASPECT_RATIO,
2429       g_param_spec_string ("pixel-aspect-ratio", "Pixel Aspect Ratio",
2430           "The pixel aspect ratio of the device", "1/1",
2431           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2432   g_object_class_install_property (gobject_class, ARG_VSYNC,
2433       g_param_spec_boolean ("vsync", "Vertical synchronisation",
2434           "Wait for next vertical sync to draw frames", TRUE,
2435           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2436   g_object_class_install_property (gobject_class, ARG_LAYER_MODE,
2437       g_param_spec_enum ("layer-mode",
2438           "The layer cooperative level (administrative or exclusive)",
2439           "The cooperative level handling the access permission (set this to "
2440           "'administrative' when the cursor is required)",
2441           gst_dfbvideosink_layer_mode_get_type (), DEFAULT_LAYER_MODE,
2442           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2443
2444   gst_type_mark_as_plugin_api (gst_dfbvideosink_layer_mode_get_type (), 0);
2445
2446   gst_element_class_set_static_metadata (gstelement_class,
2447       "DirectFB video sink", "Sink/Video", "A DirectFB based videosink",
2448       "Julien Moutte <julien@moutte.net>");
2449
2450   gst_element_class_add_static_pad_template (gstelement_class,
2451       &gst_dfbvideosink_sink_template_factory);
2452
2453   gstelement_class->change_state = gst_dfbvideosink_change_state;
2454
2455   gstbasesink_class->get_caps = gst_dfbvideosink_getcaps;
2456   gstbasesink_class->set_caps = gst_dfbvideosink_setcaps;
2457   gstbasesink_class->get_times = gst_dfbvideosink_get_times;
2458   gstbasesink_class->preroll = gst_dfbvideosink_show_frame;
2459   gstbasesink_class->render = gst_dfbvideosink_show_frame;
2460   gstbasesink_class->propose_allocation = gst_dfbvideosink_propose_allocation;
2461 }
2462
2463 static gboolean
2464 plugin_init (GstPlugin * plugin)
2465 {
2466   return GST_ELEMENT_REGISTER (dfbvideosink, plugin);
2467 }
2468
2469 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
2470     GST_VERSION_MINOR,
2471     directfb,
2472     "DirectFB video output plugin",
2473     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)