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