dfbvideosink.c 75.4 KB
Newer Older
1 2
/* GStreamer DirectFB plugin
 * Copyright (C) 2005 Julien MOUTTE <julien@moutte.net>
Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
3
 * Copyright (C) 2013 Kazunori Kobayashi <kkobayas@igel.co.jp>
4 5 6 7 8 9 10 11 12 13 14 15 16
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
17 18
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
19 20
 */

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
/**
 * SECTION:element-dfbvideosink
 *
 * DfbVideoSink renders video frames using the
 * <ulink url="http://www.directfb.org/">DirectFB</ulink> library.
 * Rendering can happen in two different modes :
 * <itemizedlist>
 * <listitem>
 *   <para>
 *   Standalone: this mode will take complete control of the monitor forcing
 *   <ulink url="http://www.directfb.org/">DirectFB</ulink> to fullscreen layout.
 *   This is convenient to test using the  gst-launch command line tool or
 *   other simple applications. It is possible to interrupt playback while
 *   being in this mode by pressing the Escape key.
 *   </para>
 *   <para>
 *   This mode handles navigation events for every input device supported by
 *   the <ulink url="http://www.directfb.org/">DirectFB</ulink> library, it will
 *   look for available video modes in the fb.modes file and try to switch
 *   the framebuffer video mode to the most suitable one. Depending on 
 *   hardware acceleration capabilities the element will handle scaling or not.
 *   If no acceleration is available it will do clipping or centering of the
 *   video frames respecting the original aspect ratio.
 *   </para>
 * </listitem>
 * <listitem>
 *   <para>
 *   Embedded: this mode will render video frames in a 
49
 *   #GstDfbVideoSink:surface provided by the
50 51 52 53 54 55
 *   application developer. This is a more advanced usage of the element and
 *   it is required to integrate video playback in existing 
 *   <ulink url="http://www.directfb.org/">DirectFB</ulink> applications.
 *   </para>
 *   <para>
 *   When using this mode the element just renders to the
56
 *   #GstDfbVideoSink:surface provided by the 
57
 *   application, that means it won't handle navigation events and won't resize
58
 *   the #GstDfbVideoSink:surface to fit video
59 60
 *   frames geometry. Application has to implement the necessary code to grab
 *   informations about the negotiated geometry and resize there
61
 *   #GstDfbVideoSink:surface accordingly.
62 63 64 65 66 67 68
 *   </para>
 * </listitem>
 * </itemizedlist>
 * For both modes the element implements a buffer pool allocation system to 
 * optimize memory allocation time and handle reverse negotiation. Indeed if 
 * you insert an element like videoscale in the pipeline the video sink will
 * negotiate with it to try get a scaled video for either the fullscreen layout
69 70 71
 * or the application provided external #GstDfbVideoSink:surface.
 *
 * <refsect2>
72 73 74 75
 * <title>Example application</title>
 * <para>
 * <include xmlns="http://www.w3.org/2003/XInclude" href="element-dfb-example.xml" />
 * </para>
76 77
 * </refsect2>
 * <refsect2>
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
78
 * <title>Example pipelines</title>
79
 * |[
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
80
 * gst-launch -v videotestsrc ! dfbvideosink hue=20000 saturation=40000 brightness=25000
81
 * ]| test the colorbalance interface implementation in dfbvideosink
82 83 84
 * </refsect2>
 */

85 86 87 88
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
89
#include <gst/video/video.h>
90 91 92 93

/* Object header */
#include "dfbvideosink.h"

94
#include <string.h>
95
#include <stdlib.h>
96

97
/* Debugging category */
98
GST_DEBUG_CATEGORY_STATIC (dfbvideosink_debug);
99 100 101 102
#define GST_CAT_DEFAULT dfbvideosink_debug

/* Default template */
static GstStaticPadTemplate gst_dfbvideosink_sink_template_factory =
Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
103
GST_STATIC_PAD_TEMPLATE ("sink",
104 105
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
106
    GST_STATIC_CAPS ("video/x-raw, "
107
        "framerate = (fraction) [ 0, MAX ], "
108 109 110 111 112 113 114
        "width = (int) [ 1, MAX ], " "height = (int) [ 1, MAX ]")
    );

/* Signals and args */
enum
{
  ARG_0,
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
115 116 117 118 119
  ARG_SURFACE,
  ARG_CONTRAST,
  ARG_BRIGHTNESS,
  ARG_HUE,
  ARG_SATURATION,
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
120
  ARG_PIXEL_ASPECT_RATIO,
Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
121 122
  ARG_VSYNC,
  ARG_LAYER_MODE
123 124
};

125 126
#define DEFAULT_LAYER_MODE LAYER_MODE_EXCLUSIVE

127 128
static DFBSurfacePixelFormat gst_dfbvideosink_get_format_from_caps (GstCaps *
    caps);
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
129 130
static void gst_dfbvideosink_update_colorbalance (GstDfbVideoSink *
    dfbvideosink);
Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
131 132 133 134 135
static void gst_dfbvideosink_navigation_init (GstNavigationInterface * iface);
static void gst_dfbvideosink_colorbalance_init (GstColorBalanceInterface
    * iface);
static const char *gst_dfbvideosink_get_format_name (DFBSurfacePixelFormat
    format);
136

Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
137
#define gst_dfbvideosink_parent_class parent_class
138

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
static GType
gst_dfbvideosink_layer_mode_get_type (void)
{
  static gsize id = 0;
  static const GEnumValue values[] = {
    {0, "NONE", "none"},
    {DLSCL_EXCLUSIVE, "DLSCL_EXCLUSIVE", "exclusive"},
    {DLSCL_ADMINISTRATIVE, "DLSCL_ADMINISTRATIVE", "administrative"},
    {0, NULL, NULL}
  };

  if (g_once_init_enter (&id)) {
    GType tmp = g_enum_register_static ("GstDfbVideoSinkLayerMode", values);
    g_once_init_leave (&id, tmp);
  }

  return (GType) id;
}

Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
158 159
GType
gst_meta_dfbsurface_api_get_type (void)
160
{
Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
  static volatile GType type;
  static const gchar *tags[] = { "memory", NULL };

  if (g_once_init_enter (&type)) {
    GType _type = gst_meta_api_type_register ("GstMetaDfbSurfaceAPI", tags);
    g_once_init_leave (&type, _type);
  }
  return type;
}

/* our metadata */
const GstMetaInfo *
gst_meta_dfbsurface_get_info (void)
{
  static const GstMetaInfo *meta_info = NULL;

  if (g_once_init_enter (&meta_info)) {
    const GstMetaInfo *meta =
        gst_meta_register (gst_meta_dfbsurface_api_get_type (),
        "GstMetaDfbSurface", sizeof (GstMetaDfbSurface),
        (GstMetaInitFunction) NULL, (GstMetaFreeFunction) NULL,
        (GstMetaTransformFunction) NULL);
    g_once_init_leave (&meta_info, meta);
  }
  return meta_info;
}

G_DEFINE_TYPE (GstDfbBufferPool, gst_dfb_buffer_pool, GST_TYPE_BUFFER_POOL);

static gboolean
gst_dfb_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
{
  GstDfbBufferPool *dfbpool = GST_DFB_BUFFER_POOL_CAST (pool);
  GstCaps *caps;
  DFBSurfacePixelFormat pixel_format = DSPF_UNKNOWN;
  gint width, height;
  DFBResult ret;
  DFBSurfaceDescription s_dsc;
  IDirectFBSurface *surface;
  gpointer data;
  gint pitch;
  guint size;
  guint min_buffers;
  guint max_buffers;
  GstVideoInfo info;

  if (!dfbpool->dfbvideosink->setup) {
    GST_WARNING_OBJECT (pool, "DirectFB hasn't been initialized yet.");
    return FALSE;
  }

  if (!gst_buffer_pool_config_get_params (config, &caps, NULL, &min_buffers,
          &max_buffers)) {
    GST_WARNING_OBJECT (pool, "invalid config");
    return FALSE;
  }

  pixel_format = gst_dfbvideosink_get_format_from_caps (caps);

  if (!gst_video_info_from_caps (&info, caps)) {
    GST_WARNING_OBJECT (pool, "failed getting video info from caps %"
        GST_PTR_FORMAT, caps);
    return FALSE;
  }

  width = GST_VIDEO_INFO_WIDTH (&info);
  height = GST_VIDEO_INFO_HEIGHT (&info);

  /* temporarily create a surface to get the pitch */
  s_dsc.flags = DSDESC_PIXELFORMAT | DSDESC_WIDTH | DSDESC_HEIGHT;
  s_dsc.pixelformat = pixel_format;
  s_dsc.width = width;
  s_dsc.height = height;

  ret = dfbpool->dfbvideosink->dfb->CreateSurface (dfbpool->dfbvideosink->dfb,
      &s_dsc, &surface);
  if (ret != DFB_OK) {
    GST_WARNING_OBJECT (pool, "failed creating surface with format %s",
        gst_dfbvideosink_get_format_name (pixel_format));
    return FALSE;
  }

  ret = surface->Lock (surface, DSLF_READ, &data, &pitch);
  if (ret != DFB_OK) {
    GST_WARNING_OBJECT (pool, "failed locking the surface");
    surface->Release (surface);
    return FALSE;
  }
  surface->Unlock (surface);
  surface->Release (surface);

  switch (GST_VIDEO_INFO_FORMAT (&info)) {
    case GST_VIDEO_FORMAT_I420:
    case GST_VIDEO_FORMAT_YV12:
    case GST_VIDEO_FORMAT_NV12:
      size = pitch * height * 3 / 2;
      break;
258
    default:
Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
259 260
      size = pitch * height;
      break;
261
  }
Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
262 263 264 265 266 267 268 269

  gst_buffer_pool_config_set_params (config, caps, size, min_buffers,
      max_buffers);

  dfbpool->caps = gst_caps_ref (caps);

  return GST_BUFFER_POOL_CLASS (gst_dfb_buffer_pool_parent_class)->set_config
      (pool, config);
270 271
}

Wim Taymans's avatar
Wim Taymans committed
272
static void
Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
273
gst_dfb_buffer_pool_free_buffer (GstBufferPool * bpool, GstBuffer * surface)
Wim Taymans's avatar
Wim Taymans committed
274 275 276
{
  GstMetaDfbSurface *meta;

Wim Taymans's avatar
Wim Taymans committed
277
  meta = GST_META_DFBSURFACE_GET (surface);
Wim Taymans's avatar
Wim Taymans committed
278

Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
279 280 281 282 283 284 285
  /* Release our internal surface */
  if (meta->surface) {
    if (meta->locked) {
      meta->surface->Unlock (meta->surface);
      meta->locked = FALSE;
    }
    meta->surface->Release (meta->surface);
Wim Taymans's avatar
Wim Taymans committed
286 287
  }

Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
288 289 290
  if (meta->dfbvideosink)
    /* Release the ref to our sink */
    gst_object_unref (meta->dfbvideosink);
Wim Taymans's avatar
Wim Taymans committed
291

Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
292 293
  GST_BUFFER_POOL_CLASS (gst_dfb_buffer_pool_parent_class)->free_buffer (bpool,
      surface);
Wim Taymans's avatar
Wim Taymans committed
294 295
}

Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
296 297 298
static GstFlowReturn
gst_dfb_buffer_pool_alloc_buffer (GstBufferPool * bpool,
    GstBuffer ** buffer, GstBufferPoolAcquireParams * params)
299
{
Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
300 301 302 303
  GstDfbBufferPool *dfbpool = GST_DFB_BUFFER_POOL_CAST (bpool);
  GstBuffer *surface;
  GstMetaDfbSurface *meta;
  GstStructure *structure;
304 305 306 307
  DFBResult ret;
  DFBSurfaceDescription s_dsc;
  gpointer data;
  gint pitch;
Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
308 309 310 311 312 313 314 315 316 317
  GstFlowReturn result = GST_FLOW_ERROR;
  gsize alloc_size;
  gsize offset[GST_VIDEO_MAX_PLANES] = { 0 };
  gint stride[GST_VIDEO_MAX_PLANES] = { 0 };
  gsize max_size;
  gsize plane_size[GST_VIDEO_MAX_PLANES] = { 0 };
  guint n_planes;
  const gchar *str;
  GstVideoFormat format;
  gint i;
318

Wim Taymans's avatar
Wim Taymans committed
319
  surface = gst_buffer_new ();
Wim Taymans's avatar
Wim Taymans committed
320
  meta = GST_META_DFBSURFACE_ADD (surface);
321

322
  /* Keep a ref to our sink */
Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
323
  meta->dfbvideosink = gst_object_ref (dfbpool->dfbvideosink);
324
  /* Surface is not locked yet */
Wim Taymans's avatar
Wim Taymans committed
325
  meta->locked = FALSE;
326

Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
327
  structure = gst_caps_get_structure (dfbpool->caps, 0);
328

Wim Taymans's avatar
Wim Taymans committed
329 330
  if (!gst_structure_get_int (structure, "width", &meta->width) ||
      !gst_structure_get_int (structure, "height", &meta->height)) {
Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
331 332
    GST_WARNING_OBJECT (bpool, "failed getting geometry from caps %"
        GST_PTR_FORMAT, dfbpool->caps);
333
    goto fallback;
334 335
  }

336
  /* Pixel format from caps */
Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
337
  meta->pixel_format = gst_dfbvideosink_get_format_from_caps (dfbpool->caps);
Wim Taymans's avatar
Wim Taymans committed
338
  if (meta->pixel_format == DSPF_UNKNOWN) {
339 340
    goto fallback;
  }
341

Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
342 343
  if (!dfbpool->dfbvideosink->dfb) {
    GST_DEBUG_OBJECT (bpool, "no DirectFB context to create a surface");
344 345
    goto fallback;
  }
346

347 348
  /* Creating an internal surface which will be used as GstBuffer, we used
     the detected pixel format and video dimensions */
349

Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
350
  s_dsc.flags = DSDESC_PIXELFORMAT | DSDESC_WIDTH | DSDESC_HEIGHT;
351

Wim Taymans's avatar
Wim Taymans committed
352 353 354
  s_dsc.pixelformat = meta->pixel_format;
  s_dsc.width = meta->width;
  s_dsc.height = meta->height;
355

Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
356 357 358
  ret =
      dfbpool->dfbvideosink->dfb->CreateSurface (dfbpool->dfbvideosink->dfb,
      &s_dsc, &meta->surface);
359
  if (ret != DFB_OK) {
Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
360
    GST_WARNING_OBJECT (bpool, "failed creating a DirectFB surface");
Wim Taymans's avatar
Wim Taymans committed
361
    meta->surface = NULL;
362
    goto fallback;
363 364
  }

365
  /* Clearing surface */
Wim Taymans's avatar
Wim Taymans committed
366
  meta->surface->Clear (meta->surface, 0x00, 0x00, 0x00, 0xFF);
367 368

  /* Locking the surface to acquire the memory pointer */
Wim Taymans's avatar
Wim Taymans committed
369 370
  meta->surface->Lock (meta->surface, DSLF_WRITE, &data, &pitch);
  meta->locked = TRUE;
371

Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
372
  GST_DEBUG_OBJECT (bpool, "creating a %dx%d surface (%p) with %s "
Wim Taymans's avatar
Wim Taymans committed
373 374
      "pixel format, line pitch %d", meta->width, meta->height, surface,
      gst_dfbvideosink_get_format_name (meta->pixel_format), pitch);
375

Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
  structure = gst_caps_get_structure (dfbpool->caps, 0);
  str = gst_structure_get_string (structure, "format");
  if (str == NULL) {
    GST_WARNING ("failed grabbing fourcc from caps %" GST_PTR_FORMAT,
        dfbpool->caps);
    return GST_FLOW_ERROR;
  }

  format = gst_video_format_from_string (str);
  switch (format) {
    case GST_VIDEO_FORMAT_I420:
    case GST_VIDEO_FORMAT_YV12:
      offset[1] = pitch * meta->height;
      offset[2] = offset[1] + pitch / 2 * meta->height / 2;
      stride[0] = pitch;
      stride[1] = stride[2] = pitch / 2;

      plane_size[0] = offset[1];
      plane_size[1] = plane_size[2] = plane_size[0] / 4;
      max_size = plane_size[0] * 3 / 2;
      n_planes = 3;
      break;
    case GST_VIDEO_FORMAT_NV12:
      offset[1] = pitch * meta->height;
      stride[0] = stride[1] = pitch;

      plane_size[0] = offset[1];
      plane_size[1] = pitch * meta->height / 2;
      max_size = plane_size[0] * 3 / 2;
      n_planes = 2;
      break;
    default:
      stride[0] = pitch;
      plane_size[0] = max_size = pitch * meta->height;
      n_planes = 1;
      break;
  }

  for (i = 0; i < n_planes; i++) {
    gst_buffer_append_memory (surface,
        gst_memory_new_wrapped (0, data, max_size, offset[i], plane_size[i],
            NULL, NULL));
  }

  gst_buffer_add_video_meta_full (surface, GST_VIDEO_FRAME_FLAG_NONE,
      format, meta->width, meta->height, n_planes, offset, stride);

  result = GST_FLOW_OK;
424 425 426 427 428 429 430

  goto beach;

fallback:

  /* We allocate a standard buffer ourselves to store it in our buffer pool,
     this is an optimisation for memory allocation */
Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
431 432 433 434 435 436
  alloc_size = meta->width * meta->height;
  surface = gst_buffer_new_allocate (NULL, alloc_size, NULL);
  if (surface == NULL) {
    GST_WARNING_OBJECT (bpool, "failed allocating a gstbuffer");
    goto beach;
  }
Wim Taymans's avatar
Wim Taymans committed
437 438 439 440 441

  if (meta->surface) {
    if (meta->locked) {
      meta->surface->Unlock (meta->surface);
      meta->locked = FALSE;
442
    }
Wim Taymans's avatar
Wim Taymans committed
443 444
    meta->surface->Release (meta->surface);
    meta->surface = NULL;
445
  }
Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
446 447
  GST_DEBUG_OBJECT (bpool, "allocating a buffer (%p) of %u bytes",
      surface, (guint) alloc_size);
448

Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
449
  result = GST_FLOW_OK;
450 451

beach:
Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
  if (result != GST_FLOW_OK) {
    gst_dfb_buffer_pool_free_buffer (bpool, surface);
    *buffer = NULL;
  } else
    *buffer = surface;

  return result;
}

static GstBufferPool *
gst_dfb_buffer_pool_new (GstDfbVideoSink * dfbvideosink)
{
  GstDfbBufferPool *pool;

  g_return_val_if_fail (GST_IS_DFBVIDEOSINK (dfbvideosink), NULL);

  pool = g_object_new (GST_TYPE_DFB_BUFFER_POOL, NULL);
  pool->dfbvideosink = gst_object_ref (dfbvideosink);

  GST_LOG_OBJECT (pool, "new dfb buffer pool %p", pool);

  return GST_BUFFER_POOL_CAST (pool);
474 475 476
}

static void
Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
477
gst_dfb_buffer_pool_finalize (GObject * object)
478
{
Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
479
  GstDfbBufferPool *pool = GST_DFB_BUFFER_POOL_CAST (object);
Wim Taymans's avatar
Wim Taymans committed
480

Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
481 482 483
  if (pool->caps)
    gst_caps_unref (pool->caps);
  gst_object_unref (pool->dfbvideosink);
484

Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
485 486
  G_OBJECT_CLASS (gst_dfb_buffer_pool_parent_class)->finalize (object);
}
Wim Taymans's avatar
Wim Taymans committed
487

Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
488 489 490 491 492
static void
gst_dfb_buffer_pool_init (GstDfbBufferPool * pool)
{
  /* No processing */
}
493

Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
static void
gst_dfb_buffer_pool_class_init (GstDfbBufferPoolClass * klass)
{
  GObjectClass *gobject_class = (GObjectClass *) klass;
  GstBufferPoolClass *gstbufferpool_class = (GstBufferPoolClass *) klass;

  gobject_class->finalize = gst_dfb_buffer_pool_finalize;

  gstbufferpool_class->alloc_buffer = gst_dfb_buffer_pool_alloc_buffer;
  gstbufferpool_class->set_config = gst_dfb_buffer_pool_set_config;
  gstbufferpool_class->free_buffer = gst_dfb_buffer_pool_free_buffer;
}

G_DEFINE_TYPE_WITH_CODE (GstDfbVideoSink, gst_dfbvideosink, GST_TYPE_VIDEO_SINK,
    G_IMPLEMENT_INTERFACE (GST_TYPE_NAVIGATION,
        gst_dfbvideosink_navigation_init);
    G_IMPLEMENT_INTERFACE (GST_TYPE_COLOR_BALANCE,
        gst_dfbvideosink_colorbalance_init));

#ifndef GST_DISABLE_GST_DEBUG
static const char *
gst_dfbvideosink_get_format_name (DFBSurfacePixelFormat format)
{
  switch (format) {
    case DSPF_ARGB1555:
      return "ARGB1555";
    case DSPF_RGB16:
      return "RGB16";
    case DSPF_RGB24:
      return "RGB24";
    case DSPF_RGB32:
      return "RGB32";
    case DSPF_ARGB:
      return "ARGB";
    case DSPF_A8:
      return "A8";
    case DSPF_YUY2:
      return "YUY2";
    case DSPF_RGB332:
      return "RGB33";
    case DSPF_UYVY:
      return "UYVY";
    case DSPF_I420:
      return "I420";
    case DSPF_YV12:
      return "YV12";
    case DSPF_LUT8:
      return "LUT8";
    case DSPF_ALUT44:
      return "ALUT44";
    case DSPF_AiRGB:
      return "AiRGB";
    case DSPF_A1:
      return "A1";
    case DSPF_NV12:
      return "NV12";
    case DSPF_NV16:
      return "NV16";
    case DSPF_ARGB2554:
      return "ARGB2554";
    case DSPF_ARGB4444:
      return "ARGB4444";
    case DSPF_NV21:
      return "NV21";
    default:
      return "UNKNOWN";
560 561
  }
}
Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
562
#endif /* GST_DISABLE_GST_DEBUG */
563 564 565 566 567 568 569 570

static gpointer
gst_dfbvideosink_event_thread (GstDfbVideoSink * dfbvideosink)
{
  DFBResult ret;

  while (dfbvideosink->running) {
    /* Wait for an event with a 50 ms timeout */
571 572
    dfbvideosink->event_buffer->WaitForEventWithTimeout (dfbvideosink->
        event_buffer, 0, 50);
573 574 575 576 577 578 579

    /* Do we have an event ? */
    ret = dfbvideosink->event_buffer->HasEvent (dfbvideosink->event_buffer);

    if (ret == DFB_OK) {
      DFBEvent event;

Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
580
      GST_DEBUG_OBJECT (dfbvideosink, "we have an event");
581 582 583 584

      ret = dfbvideosink->event_buffer->GetEvent (dfbvideosink->event_buffer,
          &event);
      if (ret != DFB_OK) {      /* Error */
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
585 586
        GST_WARNING_OBJECT (dfbvideosink, "failed when getting event from "
            "event buffer");
587 588 589 590 591 592 593 594 595 596 597
      } else {                  /* Handle event */
        if (event.input.type == DIET_KEYPRESS) {
          switch (event.input.key_symbol) {
            case DIKS_ESCAPE:
            {
              GST_ELEMENT_ERROR (dfbvideosink, RESOURCE, OPEN_WRITE,
                  ("Video output device is gone."),
                  ("We were running fullscreen and user "
                      "pressed the ESC key, stopping playback."));
            }
            default:
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
598 599
              GST_DEBUG_OBJECT (dfbvideosink, "key press event %c !",
                  event.input.key_symbol);
600 601 602 603 604 605 606 607
              gst_navigation_send_key_event (GST_NAVIGATION (dfbvideosink),
                  "key-press", "prout");
          }
        } else if (event.input.type == DIET_BUTTONPRESS) {
          gint x, y;

          dfbvideosink->layer->GetCursorPosition (dfbvideosink->layer, &x, &y);

Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
608 609
          GST_DEBUG_OBJECT (dfbvideosink, "button %d pressed at %dx%d",
              event.input.button, x, y);
610 611 612 613 614 615 616 617

          gst_navigation_send_mouse_event (GST_NAVIGATION (dfbvideosink),
              "mouse-button-press", event.input.button, x, y);
        } else if (event.input.type == DIET_BUTTONRELEASE) {
          gint x, y;

          dfbvideosink->layer->GetCursorPosition (dfbvideosink->layer, &x, &y);

Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
618 619
          GST_DEBUG_OBJECT (dfbvideosink, "button %d released at %dx%d",
              event.input.button, x, y);
620 621 622 623

          gst_navigation_send_mouse_event (GST_NAVIGATION (dfbvideosink),
              "mouse-button-release", event.input.button, x, y);
        } else if (event.input.type == DIET_AXISMOTION) {
624 625 626 627 628
          gint x, y;

          dfbvideosink->layer->GetCursorPosition (dfbvideosink->layer, &x, &y);
          gst_navigation_send_mouse_event (GST_NAVIGATION (dfbvideosink),
              "mouse-move", 0, x, y);
629
        } else {
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
630 631
          GST_WARNING_OBJECT (dfbvideosink, "unhandled event type %d",
              event.input.type);
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
        }
      }
    }
  }
  return NULL;
}

static DFBEnumerationResult
gst_dfbvideosink_enum_layers (DFBDisplayLayerID id,
    DFBDisplayLayerDescription desc, void *data)
{
  GstDfbVideoSink *dfbvideosink = NULL;
  IDirectFBDisplayLayer *layer = NULL;
  DFBDisplayLayerConfig dlc;
  DFBResult ret;
  gboolean backbuffer = FALSE;

  g_return_val_if_fail (GST_IS_DFBVIDEOSINK (data), DFENUM_CANCEL);

  dfbvideosink = GST_DFBVIDEOSINK (data);

653 654
  GST_DEBUG_OBJECT (dfbvideosink, "inspecting display layer %d with name: %s",
      id, desc.name);
655 656

  if ((desc.type & DLTF_VIDEO) && (desc.caps & DLCAPS_SURFACE)) {
657 658
    GST_DEBUG_OBJECT (dfbvideosink,
        "this layer can handle live video and has a surface");
659 660
  } else {
    if (desc.caps & DLCAPS_SURFACE) {
661 662
      GST_DEBUG_OBJECT (dfbvideosink,
          "this layer can not handle live video but has a surface");
663
    } else {
664
      GST_DEBUG_OBJECT (dfbvideosink, "no we can't use that layer, really...");
665 666 667 668 669 670
      goto beach;
    }
  }

  ret = dfbvideosink->dfb->GetDisplayLayer (dfbvideosink->dfb, id, &layer);
  if (ret != DFB_OK) {
671 672
    GST_WARNING_OBJECT (dfbvideosink, "failed getting display layer %s",
        desc.name);
673 674 675 676 677
    goto beach;
  }

  ret = layer->GetConfiguration (layer, &dlc);
  if (ret != DFB_OK) {
678 679
    GST_WARNING_OBJECT (dfbvideosink,
        "failed getting display layer configuration");
680 681 682 683
    goto beach;
  }

  if ((dlc.flags & DLCONF_BUFFERMODE) && (dlc.buffermode & DLBM_FRONTONLY)) {
684
    GST_DEBUG_OBJECT (dfbvideosink, "no backbuffer");
685 686
  }
  if ((dlc.flags & DLCONF_BUFFERMODE) && (dlc.buffermode & DLBM_BACKVIDEO)) {
687
    GST_DEBUG_OBJECT (dfbvideosink, "backbuffer is in video memory");
688 689 690
    backbuffer = TRUE;
  }
  if ((dlc.flags & DLCONF_BUFFERMODE) && (dlc.buffermode & DLBM_BACKSYSTEM)) {
691
    GST_DEBUG_OBJECT (dfbvideosink, "backbuffer is in system memory");
692 693 694
    backbuffer = TRUE;
  }
  if ((dlc.flags & DLCONF_BUFFERMODE) && (dlc.buffermode & DLBM_TRIPLE)) {
695
    GST_DEBUG_OBJECT (dfbvideosink, "triple buffering");
696 697 698
    backbuffer = TRUE;
  }

699 700 701 702 703 704 705 706 707
  /* If the primary is suitable we prefer using it */
  if (dfbvideosink->layer_id != DLID_PRIMARY) {
    GST_DEBUG_OBJECT (dfbvideosink, "selecting layer named %s", desc.name);
    dfbvideosink->layer_id = id;
    dfbvideosink->backbuffer = backbuffer;
  } else {
    GST_DEBUG_OBJECT (dfbvideosink, "layer %s is suitable but the primary "
        "is currently selected and we prefer that one", desc.name);
  }
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725

beach:
  if (layer) {
    layer->Release (layer);
  }
  return DFENUM_OK;
}

static DFBEnumerationResult
gst_dfbvideosink_enum_vmodes (gint width, gint height, gint bpp, void *data)
{
  GstDfbVideoSink *dfbvideosink = NULL;
  GstDfbVMode *vmode = NULL;

  g_return_val_if_fail (GST_IS_DFBVIDEOSINK (data), DFENUM_CANCEL);

  dfbvideosink = GST_DFBVIDEOSINK (data);

Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
726 727
  GST_DEBUG_OBJECT (dfbvideosink, "adding video mode %dx%d at %d bpp", width,
      height, bpp);
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
  vmode = g_new0 (GstDfbVMode, 1);
  vmode->width = width;
  vmode->height = height;
  vmode->bpp = bpp;

  /* We need to know the maximum video geometry we can accept for the caps */
  if (width > dfbvideosink->out_width) {
    dfbvideosink->out_width = width;
  }
  if (height > dfbvideosink->out_height) {
    dfbvideosink->out_height = height;
  }

  dfbvideosink->vmodes = g_slist_append (dfbvideosink->vmodes, vmode);

  return DFENUM_OK;
}

static DFBEnumerationResult
gst_dfbvideosink_enum_devices (DFBInputDeviceID id,
    DFBInputDeviceDescription desc, void *data)
{
  GstDfbVideoSink *dfbvideosink = NULL;
  IDirectFBInputDevice *device = NULL;
  DFBResult ret;

  g_return_val_if_fail (GST_IS_DFBVIDEOSINK (data), DFENUM_CANCEL);

  dfbvideosink = GST_DFBVIDEOSINK (data);

Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
758 759
  GST_DEBUG_OBJECT (dfbvideosink, "detected input device %s from vendor %s",
      desc.name, desc.vendor);
760 761 762 763

  /* Get that input device */
  ret = dfbvideosink->dfb->GetInputDevice (dfbvideosink->dfb, id, &device);
  if (ret != DFB_OK) {
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
764 765
    GST_WARNING_OBJECT (dfbvideosink, "failed when getting input device id %d",
        id);
766 767 768 769 770
    goto beach;
  }

  ret = device->AttachEventBuffer (device, dfbvideosink->event_buffer);
  if (ret != DFB_OK) {
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
771 772
    GST_WARNING_OBJECT (dfbvideosink, "failed when attaching input device "
        "%d to our event buffer", id);
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
  }

beach:
  if (device) {
    device->Release (device);
  }
  return DFENUM_OK;
}

static gboolean
gst_dfbvideosink_setup (GstDfbVideoSink * dfbvideosink)
{
  DFBResult ret;

  g_return_val_if_fail (GST_IS_DFBVIDEOSINK (dfbvideosink), FALSE);

  dfbvideosink->video_width = 0;
  dfbvideosink->video_height = 0;
  dfbvideosink->out_width = 0;
  dfbvideosink->out_height = 0;
793 794
  dfbvideosink->fps_d = 0;
  dfbvideosink->fps_n = 0;
795 796 797 798 799 800 801
  dfbvideosink->hw_scaling = FALSE;
  dfbvideosink->backbuffer = FALSE;
  dfbvideosink->pixel_format = DSPF_UNKNOWN;

  /* If we do it all by ourself we create the DirectFB context, get the 
     primary layer and use a fullscreen configuration */
  if (!dfbvideosink->ext_surface) {
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
802 803
    GST_DEBUG_OBJECT (dfbvideosink, "no external surface, taking over "
        "DirectFB fullscreen");
804 805
    if (!dfbvideosink->dfb) {
      DFBGraphicsDeviceDescription hw_caps;
Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
806 807 808 809
      char *argv[] = { (char *) "-", (char *) "--dfb:quiet",
        (char *) "--dfb:no-sighandler", NULL
      };
      int argc = 3;
810
      char **args;
811

Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
812
      GST_DEBUG_OBJECT (dfbvideosink, "initializing DirectFB");
813

814 815
      args = argv;
      ret = DirectFBInit (&argc, &args);
816 817

      if (ret != DFB_OK) {
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
818
        GST_WARNING_OBJECT (dfbvideosink, "DirectFB initialization failed");
819 820 821 822 823 824
        goto beach;
      }

      ret = DirectFBCreate (&(dfbvideosink->dfb));

      if (ret != DFB_OK) {
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
825 826
        GST_WARNING_OBJECT (dfbvideosink, "failed creating the DirectFB "
            "main object");
827 828 829
        goto beach;
      }

830
      /* Get Hardware capabilities */
831 832 833 834
      ret = dfbvideosink->dfb->GetDeviceDescription (dfbvideosink->dfb,
          &hw_caps);

      if (ret != DFB_OK) {
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
835 836
        GST_WARNING_OBJECT (dfbvideosink, "failed grabbing the hardware "
            "capabilities");
837 838 839
        goto beach;
      }

Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
840 841 842
      GST_DEBUG_OBJECT (dfbvideosink, "video card %s from vendor %s detected "
          "with %d bytes of video memory", hw_caps.name, hw_caps.vendor,
          hw_caps.video_memory);
843 844

      if (hw_caps.acceleration_mask & DFXL_BLIT) {
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
845
        GST_DEBUG_OBJECT (dfbvideosink, "Blit is accelerated");
846 847
      }
      if (hw_caps.acceleration_mask & DFXL_STRETCHBLIT) {
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
848
        GST_DEBUG_OBJECT (dfbvideosink, "StretchBlit is accelerated");
849 850
        dfbvideosink->hw_scaling = TRUE;
      } else {
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
851
        GST_DEBUG_OBJECT (dfbvideosink, "StretchBlit is not accelerated");
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871
        dfbvideosink->hw_scaling = FALSE;
      }

      dfbvideosink->layer_id = -1;

      /* Inspect all the Display layers */
      dfbvideosink->dfb->EnumDisplayLayers (dfbvideosink->dfb,
          gst_dfbvideosink_enum_layers, dfbvideosink);
      /* Inspect all Video modes */
      dfbvideosink->dfb->EnumVideoModes (dfbvideosink->dfb,
          gst_dfbvideosink_enum_vmodes, dfbvideosink);

      /* Create an event buffer for input */
      dfbvideosink->dfb->CreateEventBuffer (dfbvideosink->dfb,
          &dfbvideosink->event_buffer);

      /* Inspect all Input devices */
      dfbvideosink->dfb->EnumInputDevices (dfbvideosink->dfb,
          gst_dfbvideosink_enum_devices, dfbvideosink);
      /* Create a thread to handle those events */
872 873
      dfbvideosink->event_thread = g_thread_new ("dfbvsink-events",
          (GThreadFunc) gst_dfbvideosink_event_thread, dfbvideosink);
874 875
    }
    if (!dfbvideosink->layer) {
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
876 877 878
      GList *channels_list = NULL;
      DFBDisplayLayerDescription dl_desc;

879 880 881 882
      /* Get the best Display Layer */
      ret = dfbvideosink->dfb->GetDisplayLayer (dfbvideosink->dfb,
          dfbvideosink->layer_id, &dfbvideosink->layer);
      if (ret != DFB_OK) {
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
883
        GST_WARNING_OBJECT (dfbvideosink, "failed getting display layer");
884 885 886
        goto beach;
      }

Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
887 888 889 890 891 892 893 894
      if (dfbvideosink->layer_mode == LAYER_MODE_EXCLUSIVE ||
          dfbvideosink->layer_mode == LAYER_MODE_ADMINISTRATIVE)
        ret = dfbvideosink->layer->SetCooperativeLevel (dfbvideosink->layer,
            dfbvideosink->layer_mode);
      else {
        GST_ERROR_OBJECT (dfbvideosink, "invalid layer cooperative level");
        goto beach;
      }
895 896

      if (ret != DFB_OK) {
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
897 898
        GST_WARNING_OBJECT (dfbvideosink, "failed setting display layer to "
            "fullscreen mode");
899 900 901
        goto beach;
      }

Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
902 903 904 905
      dfbvideosink->layer->GetDescription (dfbvideosink->layer, &dl_desc);

      /* Check that this layer is able to do colorbalance settings */
      if (dl_desc.caps & DLCAPS_BRIGHTNESS) {
Benjamin Otte's avatar
Benjamin Otte committed
906
        channels_list = g_list_append (channels_list, (char *) "BRIGHTNESS");
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
907 908
      }
      if (dl_desc.caps & DLCAPS_CONTRAST) {
Benjamin Otte's avatar
Benjamin Otte committed
909
        channels_list = g_list_append (channels_list, (char *) "CONTRAST");
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
910 911
      }
      if (dl_desc.caps & DLCAPS_HUE) {
Benjamin Otte's avatar
Benjamin Otte committed
912
        channels_list = g_list_append (channels_list, (char *) "HUE");
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
913 914
      }
      if (dl_desc.caps & DLCAPS_SATURATION) {
Benjamin Otte's avatar
Benjamin Otte committed
915
        channels_list = g_list_append (channels_list, (char *) "SATURATION");
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
916 917 918 919 920 921 922 923 924 925
      }

      if (channels_list) {
        GList *walk = channels_list;

        /* Generate Color balance channel list */
        while (walk) {
          GstColorBalanceChannel *channel = NULL;

          GST_DEBUG_OBJECT (dfbvideosink, "adding %s as a colorbalance channel",
926
              (const char *) walk->data);
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982

          channel = g_object_new (GST_TYPE_COLOR_BALANCE_CHANNEL, NULL);
          channel->label = g_strdup (walk->data);
          channel->min_value = 0x0000;
          channel->max_value = 0xFFFF;

          dfbvideosink->cb_channels = g_list_append (dfbvideosink->cb_channels,
              channel);

          walk = g_list_next (walk);
        }

        /* If the colorbalance settings have not been touched we get current
           values as defaults and update our internal variables */
        if (!dfbvideosink->cb_changed) {
          DFBColorAdjustment cb_adjust;

          ret = dfbvideosink->layer->GetColorAdjustment (dfbvideosink->layer,
              &cb_adjust);

          if (ret != DFB_OK) {
            GST_WARNING_OBJECT (dfbvideosink, "failed when getting color "
                "adjustment from layer");
          }

          if (cb_adjust.flags & DCAF_BRIGHTNESS) {
            dfbvideosink->brightness = cb_adjust.brightness;
          } else {
            dfbvideosink->brightness = 0x8000;
          }
          if (cb_adjust.flags & DCAF_CONTRAST) {
            dfbvideosink->contrast = cb_adjust.contrast;
          } else {
            dfbvideosink->contrast = 0x8000;
          }
          if (cb_adjust.flags & DCAF_HUE) {
            dfbvideosink->hue = cb_adjust.hue;
          } else {
            dfbvideosink->hue = 0x8000;
          }
          if (cb_adjust.flags & DCAF_SATURATION) {
            dfbvideosink->saturation = cb_adjust.saturation;
          } else {
            dfbvideosink->saturation = 0x8000;
          }
          GST_DEBUG_OBJECT (dfbvideosink, "brightness %d, contrast %d, "
              "hue %d, saturation %d", dfbvideosink->brightness,
              dfbvideosink->contrast, dfbvideosink->hue,
              dfbvideosink->saturation);
        }

        g_list_free (channels_list);

        gst_dfbvideosink_update_colorbalance (dfbvideosink);
      }

983 984 985
      dfbvideosink->layer->SetBackgroundColor (dfbvideosink->layer,
          0x00, 0x00, 0x00, 0xFF);

Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
986 987 988 989
#if (DIRECTFB_VER >= GST_DFBVIDEOSINK_VER (1,6,0))
      if (dfbvideosink->layer_mode == LAYER_MODE_ADMINISTRATIVE)
#endif
        dfbvideosink->layer->EnableCursor (dfbvideosink->layer, TRUE);
990

Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
991
      GST_DEBUG_OBJECT (dfbvideosink, "getting primary surface");
992 993
      dfbvideosink->layer->GetSurface (dfbvideosink->layer,
          &dfbvideosink->primary);
994 995 996

      dfbvideosink->primary->SetBlittingFlags (dfbvideosink->primary,
          DSBLIT_NOFX);
997 998 999 1000 1001 1002 1003
    }

    dfbvideosink->primary->GetPixelFormat (dfbvideosink->primary,
        &dfbvideosink->pixel_format);
  } else {
    DFBSurfaceCapabilities s_caps;

Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
1004 1005
    GST_DEBUG_OBJECT (dfbvideosink, "getting pixel format from foreign "
        "surface %p", dfbvideosink->ext_surface);
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
    dfbvideosink->ext_surface->GetPixelFormat (dfbvideosink->ext_surface,
        &dfbvideosink->pixel_format);
    dfbvideosink->ext_surface->GetSize (dfbvideosink->ext_surface,
        &dfbvideosink->out_width, &dfbvideosink->out_height);
    dfbvideosink->ext_surface->GetCapabilities (dfbvideosink->ext_surface,
        &s_caps);
    if ((s_caps & DSCAPS_DOUBLE) || (s_caps & DSCAPS_TRIPLE)) {
      dfbvideosink->backbuffer = TRUE;
    } else {
      dfbvideosink->backbuffer = FALSE;
    }
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
1017 1018
    GST_DEBUG_OBJECT (dfbvideosink, "external surface is %dx%d and uses %s "
        "pixel format", dfbvideosink->out_width, dfbvideosink->out_height,
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
        gst_dfbvideosink_get_format_name (dfbvideosink->pixel_format));
  }

  dfbvideosink->setup = TRUE;

beach:
  return dfbvideosink->setup;
}

static void
gst_dfbvideosink_cleanup (GstDfbVideoSink * dfbvideosink)
{
  g_return_if_fail (GST_IS_DFBVIDEOSINK (dfbvideosink));

Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
1033
  GST_DEBUG_OBJECT (dfbvideosink, "cleaning up DirectFB environment");
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056

  /* Wait for our event thread */
  if (dfbvideosink->event_thread) {
    g_thread_join (dfbvideosink->event_thread);
    dfbvideosink->event_thread = NULL;
  }

  if (dfbvideosink->event_buffer) {
    dfbvideosink->event_buffer->Release (dfbvideosink->event_buffer);
    dfbvideosink->event_buffer = NULL;
  }

  if (dfbvideosink->vmodes) {
    GSList *walk = dfbvideosink->vmodes;

    while (walk) {
      g_free (walk->data);
      walk = g_slist_next (walk);
    }
    g_slist_free (dfbvideosink->vmodes);
    dfbvideosink->vmodes = NULL;
  }

Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
  if (dfbvideosink->cb_channels) {
    GList *walk = dfbvideosink->cb_channels;

    while (walk) {
      GstColorBalanceChannel *channel = walk->data;

      g_object_unref (channel);
      walk = g_list_next (walk);
    }
    g_list_free (dfbvideosink->cb_channels);
    dfbvideosink->cb_channels = NULL;
  }

Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
1070 1071 1072
  if (dfbvideosink->pool) {
    gst_object_unref (dfbvideosink->pool);
    dfbvideosink->pool = NULL;
1073 1074 1075 1076 1077 1078 1079 1080
  }

  if (dfbvideosink->primary) {
    dfbvideosink->primary->Release (dfbvideosink->primary);
    dfbvideosink->primary = NULL;
  }

  if (dfbvideosink->layer) {
Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
1081 1082 1083 1084
#if (DIRECTFB_VER >= GST_DFBVIDEOSINK_VER (1,6,0))
    if (dfbvideosink->layer_mode == LAYER_MODE_ADMINISTRATIVE)
#endif
      dfbvideosink->layer->EnableCursor (dfbvideosink->layer, FALSE);
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
    dfbvideosink->layer->Release (dfbvideosink->layer);
    dfbvideosink->layer = NULL;
  }

  if (dfbvideosink->dfb) {
    dfbvideosink->dfb->Release (dfbvideosink->dfb);
    dfbvideosink->dfb = NULL;
  }

  dfbvideosink->setup = FALSE;
}

static DFBSurfacePixelFormat
gst_dfbvideosink_get_format_from_caps (GstCaps * caps)
{
  GstStructure *structure;
  DFBSurfacePixelFormat pixel_format = DSPF_UNKNOWN;
Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
1102 1103
  const gchar *str;
  GstVideoFormat format;
1104 1105 1106 1107

  g_return_val_if_fail (GST_IS_CAPS (caps), DSPF_UNKNOWN);

  structure = gst_caps_get_structure (caps, 0);
Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
1108 1109 1110 1111 1112
  str = gst_structure_get_string (structure, "format");
  if (str == NULL) {
    GST_WARNING ("failed grabbing fourcc from caps %" GST_PTR_FORMAT, caps);
    return DSPF_UNKNOWN;
  }
1113

Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
  format = gst_video_format_from_string (str);
  switch (format) {
    case GST_VIDEO_FORMAT_RGB16:
      pixel_format = DSPF_RGB16;
      break;
    case GST_VIDEO_FORMAT_RGB:
      pixel_format = DSPF_RGB24;
      break;
    case GST_VIDEO_FORMAT_xRGB:
      pixel_format = DSPF_RGB32;
      break;
    case GST_VIDEO_FORMAT_ARGB:
      pixel_format = DSPF_ARGB;
      break;
    case GST_VIDEO_FORMAT_I420:
      pixel_format = DSPF_I420;
      break;
    case GST_VIDEO_FORMAT_YV12:
      pixel_format = DSPF_YV12;
      break;
    case GST_VIDEO_FORMAT_YUY2:
      pixel_format = DSPF_YUY2;
      break;
    case GST_VIDEO_FORMAT_UYVY:
      pixel_format = DSPF_UYVY;
      break;
    case GST_VIDEO_FORMAT_NV12:
      pixel_format = DSPF_NV12;
      break;
    default:
      GST_WARNING ("unhandled pixel format %s", str);
      return DSPF_UNKNOWN;
1146 1147 1148 1149 1150 1151 1152 1153
  }

  return pixel_format;
}

static GstCaps *
gst_dfbvideosink_get_caps_from_format (DFBSurfacePixelFormat format)
{
Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
1154
  const char *fourcc;
1155 1156 1157 1158 1159

  g_return_val_if_fail (format != DSPF_UNKNOWN, NULL);

  switch (format) {
    case DSPF_RGB16:
Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
1160
      fourcc = gst_video_format_to_string (GST_VIDEO_FORMAT_RGB16);
1161 1162
      break;
    case DSPF_RGB24:
Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
1163
      fourcc = gst_video_format_to_string (GST_VIDEO_FORMAT_RGB);
1164 1165
      break;
    case DSPF_RGB32:
Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
1166
      fourcc = gst_video_format_to_string (GST_VIDEO_FORMAT_xRGB);
1167 1168
      break;
    case DSPF_ARGB:
Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
1169
      fourcc = gst_video_format_to_string (GST_VIDEO_FORMAT_ARGB);
1170 1171
      break;
    case DSPF_YUY2:
Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
1172
      fourcc = gst_video_format_to_string (GST_VIDEO_FORMAT_YUY2);
1173 1174
      break;
    case DSPF_UYVY:
Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
1175
      fourcc = gst_video_format_to_string (GST_VIDEO_FORMAT_UYVY);
1176 1177
      break;
    case DSPF_I420:
Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
1178
      fourcc = gst_video_format_to_string (GST_VIDEO_FORMAT_I420);
1179 1180
      break;
    case DSPF_YV12:
Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
1181 1182 1183 1184
      fourcc = gst_video_format_to_string (GST_VIDEO_FORMAT_YV12);
      break;
    case DSPF_NV12:
      fourcc = gst_video_format_to_string (GST_VIDEO_FORMAT_NV12);
1185 1186 1187 1188
      break;
    default:
      GST_WARNING ("unknown pixel format %s",
          gst_dfbvideosink_get_format_name (format));
Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
1189
      return NULL;
1190 1191
  }

Kazunori Kobayashi's avatar
Kazunori Kobayashi committed
1192 1193
  return gst_caps_new_simple ("video/x-raw", "format", G_TYPE_STRING, fourcc,
      NULL);
1194 1195 1196 1197
}

static gboolean
gst_dfbvideosink_can_blit_from_format (GstDfbVideoSink * dfbvideosink,
1198
    DFBSurfacePixelFormat format, gboolean accelerated)
1199 1200 1201 1202 1203 1204
{
  gboolean res = FALSE;
  DFBResult ret;
  IDirectFBSurface *surface = NULL;
  DFBSurfaceDescription s_dsc;
  DFBAccelerationMask mask;
1205
  DFBDisplayLayerConfig dlc, prev_dlc;
1206