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