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