dfbvideosink.c 74.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* GStreamer DirectFB plugin
 * Copyright (C) 2005 Julien MOUTTE <julien@moutte.net>
 *
 * 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
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

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
/**
 * 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 
48
 *   #GstDfbVideoSink:surface provided by the
49 50 51 52 53 54
 *   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
55
 *   #GstDfbVideoSink:surface provided by the 
56
 *   application, that means it won't handle navigation events and won't resize
57
 *   the #GstDfbVideoSink:surface to fit video
58 59
 *   frames geometry. Application has to implement the necessary code to grab
 *   informations about the negotiated geometry and resize there
60
 *   #GstDfbVideoSink:surface accordingly.
61 62 63 64 65 66 67
 *   </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
68 69 70
 * or the application provided external #GstDfbVideoSink:surface.
 *
 * <refsect2>
71 72 73 74
 * <title>Example application</title>
 * <para>
 * <include xmlns="http://www.w3.org/2003/XInclude" href="element-dfb-example.xml" />
 * </para>
75 76
 * </refsect2>
 * <refsect2>
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
77
 * <title>Example pipelines</title>
78
 * |[
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
79
 * gst-launch -v videotestsrc ! dfbvideosink hue=20000 saturation=40000 brightness=25000
80
 * ]| test the colorbalance interface implementation in dfbvideosink
81 82 83
 * </refsect2>
 */

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

/* Our interfaces */
#include <gst/interfaces/navigation.h>
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
90
#include <gst/interfaces/colorbalance.h>
91 92 93 94

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

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

98
/* Debugging category */
99
GST_DEBUG_CATEGORY_STATIC (dfbvideosink_debug);
100 101 102 103 104 105 106 107
#define GST_CAT_DEFAULT dfbvideosink_debug

/* Default template */
static GstStaticPadTemplate gst_dfbvideosink_sink_template_factory =
    GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("video/x-raw-rgb, "
108
        "framerate = (fraction) [ 0, MAX ], "
109 110 111
        "width = (int) [ 1, MAX ], "
        "height = (int) [ 1, MAX ]; "
        "video/x-raw-yuv, "
112
        "framerate = (fraction) [ 0, MAX ], "
113 114 115 116 117 118 119
        "width = (int) [ 1, MAX ], " "height = (int) [ 1, MAX ]")
    );

/* Signals and args */
enum
{
  ARG_0,
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
120 121 122 123 124
  ARG_SURFACE,
  ARG_CONTRAST,
  ARG_BRIGHTNESS,
  ARG_HUE,
  ARG_SATURATION,
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
125
  ARG_PIXEL_ASPECT_RATIO,
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
126
  ARG_VSYNC
127 128 129 130 131
};

static void gst_dfbvideosink_bufferpool_clear (GstDfbVideoSink * dfbvideosink);
static DFBSurfacePixelFormat gst_dfbvideosink_get_format_from_caps (GstCaps *
    caps);
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
132 133
static void gst_dfbvideosink_update_colorbalance (GstDfbVideoSink *
    dfbvideosink);
134
static void gst_dfbvideosink_surface_destroy (GstDfbVideoSink * dfbvideosink,
Wim Taymans's avatar
Wim Taymans committed
135
    GstBuffer * surface);
136 137 138

static GstVideoSinkClass *parent_class = NULL;

139
#ifndef GST_DISABLE_GST_DEBUG
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 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
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";
  }
}
188
#endif /* GST_DISABLE_GST_DEBUG */
189

Wim Taymans's avatar
Wim Taymans committed
190 191 192 193 194 195 196 197
static void
gst_dfbsurface_dispose (GstBuffer * surface)
{
  GstDfbVideoSink *dfbvideosink = NULL;
  GstMetaDfbSurface *meta;

  g_return_if_fail (surface != NULL);

Wim Taymans's avatar
Wim Taymans committed
198
  meta = GST_META_DFBSURFACE_GET (surface);
Wim Taymans's avatar
Wim Taymans committed
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

  dfbvideosink = meta->dfbvideosink;
  if (!dfbvideosink) {
    GST_WARNING_OBJECT (surface, "no sink found");
    goto beach;
  }

  /* If our geometry changed we can't reuse that image. */
  if ((meta->width != dfbvideosink->video_width) ||
      (meta->height != dfbvideosink->video_height) ||
      (meta->pixel_format != dfbvideosink->pixel_format)) {
    GST_DEBUG_OBJECT (dfbvideosink, "destroy surface %p as its size changed "
        "%dx%d vs current %dx%d", surface, meta->width, meta->height,
        dfbvideosink->video_width, dfbvideosink->video_height);
    gst_dfbvideosink_surface_destroy (dfbvideosink, surface);
  } else {
    /* In that case we can reuse the image and add it to our image pool. */
    GST_DEBUG_OBJECT (dfbvideosink, "recycling surface %p in pool", surface);
    /* need to increment the refcount again to recycle */
    gst_buffer_ref (surface);
    g_mutex_lock (dfbvideosink->pool_lock);
    dfbvideosink->buffer_pool = g_slist_prepend (dfbvideosink->buffer_pool,
        surface);
    g_mutex_unlock (dfbvideosink->pool_lock);
  }

beach:
  return;
}

229
/* Creates miniobject and our internal surface */
Wim Taymans's avatar
Wim Taymans committed
230
static GstBuffer *
231 232 233
gst_dfbvideosink_surface_create (GstDfbVideoSink * dfbvideosink, GstCaps * caps,
    size_t size)
{
Wim Taymans's avatar
Wim Taymans committed
234 235
  GstBuffer *surface = NULL;
  GstMetaDfbSurface *meta = NULL;
236 237 238 239 240
  GstStructure *structure = NULL;
  DFBResult ret;
  DFBSurfaceDescription s_dsc;
  gpointer data;
  gint pitch;
241
  gboolean succeeded = FALSE;
242 243 244

  g_return_val_if_fail (GST_IS_DFBVIDEOSINK (dfbvideosink), NULL);

Wim Taymans's avatar
Wim Taymans committed
245 246 247
  surface = gst_buffer_new ();
  GST_MINI_OBJECT_CAST (surface)->dispose =
      (GstMiniObjectDisposeFunction) gst_dfbsurface_dispose;
Wim Taymans's avatar
Wim Taymans committed
248
  meta = GST_META_DFBSURFACE_ADD (surface);
249

250
  /* Keep a ref to our sink */
Wim Taymans's avatar
Wim Taymans committed
251
  meta->dfbvideosink = gst_object_ref (dfbvideosink);
252
  /* Surface is not locked yet */
Wim Taymans's avatar
Wim Taymans committed
253
  meta->locked = FALSE;
254

255 256
  structure = gst_caps_get_structure (caps, 0);

Wim Taymans's avatar
Wim Taymans committed
257 258
  if (!gst_structure_get_int (structure, "width", &meta->width) ||
      !gst_structure_get_int (structure, "height", &meta->height)) {
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
259 260
    GST_WARNING_OBJECT (dfbvideosink, "failed getting geometry from caps %"
        GST_PTR_FORMAT, caps);
261
    goto fallback;
262 263
  }

264
  /* Pixel format from caps */
Wim Taymans's avatar
Wim Taymans committed
265 266
  meta->pixel_format = gst_dfbvideosink_get_format_from_caps (caps);
  if (meta->pixel_format == DSPF_UNKNOWN) {
267 268
    goto fallback;
  }
269

270 271 272 273
  if (!dfbvideosink->dfb) {
    GST_DEBUG_OBJECT (dfbvideosink, "no DirectFB context to create a surface");
    goto fallback;
  }
274

275 276
  /* Creating an internal surface which will be used as GstBuffer, we used
     the detected pixel format and video dimensions */
277

278 279
  s_dsc.flags =
      DSDESC_PIXELFORMAT | DSDESC_WIDTH | DSDESC_HEIGHT /*| DSDESC_CAPS */ ;
280

Wim Taymans's avatar
Wim Taymans committed
281 282 283
  s_dsc.pixelformat = meta->pixel_format;
  s_dsc.width = meta->width;
  s_dsc.height = meta->height;
284 285 286
  /*s_dsc.caps = DSCAPS_VIDEOONLY; */

  ret = dfbvideosink->dfb->CreateSurface (dfbvideosink->dfb, &s_dsc,
Wim Taymans's avatar
Wim Taymans committed
287
      &meta->surface);
288 289
  if (ret != DFB_OK) {
    GST_WARNING_OBJECT (dfbvideosink, "failed creating a DirectFB surface");
Wim Taymans's avatar
Wim Taymans committed
290
    meta->surface = NULL;
291
    goto fallback;
292 293
  }

294
  /* Clearing surface */
Wim Taymans's avatar
Wim Taymans committed
295
  meta->surface->Clear (meta->surface, 0x00, 0x00, 0x00, 0xFF);
296 297

  /* Locking the surface to acquire the memory pointer */
Wim Taymans's avatar
Wim Taymans committed
298 299
  meta->surface->Lock (meta->surface, DSLF_WRITE, &data, &pitch);
  meta->locked = TRUE;
300
  GST_BUFFER_DATA (surface) = data;
Wim Taymans's avatar
Wim Taymans committed
301
  GST_BUFFER_SIZE (surface) = pitch * meta->height;
302 303 304 305 306 307

  /* Be carefull here. If size is different from the surface size
     (pitch * height), we can't use that surface through buffer alloc system
     or we are going to run into serious stride issues */
  if (GST_BUFFER_SIZE (surface) != size) {
    GST_WARNING_OBJECT (dfbvideosink, "DirectFB surface size (%dx%d=%d) "
Wim Taymans's avatar
Wim Taymans committed
308
        "differs from GStreamer requested size %u", pitch, meta->height,
309
        GST_BUFFER_SIZE (surface), (guint) size);
310 311 312 313
    goto fallback;
  }

  GST_DEBUG_OBJECT (dfbvideosink, "creating a %dx%d surface (%p) with %s "
Wim Taymans's avatar
Wim Taymans committed
314 315
      "pixel format, line pitch %d", meta->width, meta->height, surface,
      gst_dfbvideosink_get_format_name (meta->pixel_format), pitch);
316 317 318 319 320 321 322 323 324

  succeeded = TRUE;

  goto beach;

fallback:

  /* We allocate a standard buffer ourselves to store it in our buffer pool,
     this is an optimisation for memory allocation */
Wim Taymans's avatar
Wim Taymans committed
325 326
  GST_BUFFER_MALLOCDATA (surface) = g_malloc (size);
  GST_BUFFER_DATA (surface) = GST_BUFFER_MALLOCDATA (surface);
327
  GST_BUFFER_SIZE (surface) = size;
Wim Taymans's avatar
Wim Taymans committed
328 329 330 331 332

  if (meta->surface) {
    if (meta->locked) {
      meta->surface->Unlock (meta->surface);
      meta->locked = FALSE;
333
    }
Wim Taymans's avatar
Wim Taymans committed
334 335
    meta->surface->Release (meta->surface);
    meta->surface = NULL;
336
  }
337 338
  GST_DEBUG_OBJECT (dfbvideosink, "allocating a buffer (%p) of %u bytes",
      surface, (guint) size);
339 340

  succeeded = TRUE;
341 342

beach:
343 344 345 346
  if (!succeeded) {
    gst_dfbvideosink_surface_destroy (dfbvideosink, surface);
    surface = NULL;
  }
347 348 349 350 351 352 353
  return surface;
}

/* We are called from the finalize method of miniobject, the object will be
 * destroyed so we just have to clean our internal stuff */
static void
gst_dfbvideosink_surface_destroy (GstDfbVideoSink * dfbvideosink,
Wim Taymans's avatar
Wim Taymans committed
354
    GstBuffer * surface)
355
{
Wim Taymans's avatar
Wim Taymans committed
356 357
  GstMetaDfbSurface *meta;

358 359
  g_return_if_fail (GST_IS_DFBVIDEOSINK (dfbvideosink));

Wim Taymans's avatar
Wim Taymans committed
360
  meta = GST_META_DFBSURFACE_GET (surface);
Wim Taymans's avatar
Wim Taymans committed
361

362
  /* Release our internal surface */
Wim Taymans's avatar
Wim Taymans committed
363 364 365 366
  if (meta->surface) {
    if (meta->locked) {
      meta->surface->Unlock (meta->surface);
      meta->locked = FALSE;
367
    }
Wim Taymans's avatar
Wim Taymans committed
368 369
    meta->surface->Release (meta->surface);
    meta->surface = NULL;
370 371
  }

Wim Taymans's avatar
Wim Taymans committed
372
  if (meta->dfbvideosink) {
373
    /* Release the ref to our sink */
Wim Taymans's avatar
Wim Taymans committed
374
    meta->dfbvideosink = NULL;
375
    gst_object_unref (dfbvideosink);
376 377 378 379 380 381 382 383 384 385
  }
}

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

  while (dfbvideosink->running) {
    /* Wait for an event with a 50 ms timeout */
Wim Taymans's avatar
Wim Taymans committed
386 387
    dfbvideosink->event_buffer->WaitForEventWithTimeout (dfbvideosink->
        event_buffer, 0, 50);
388 389 390 391 392 393 394

    /* 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
395
      GST_DEBUG_OBJECT (dfbvideosink, "we have an event");
396 397 398 399

      ret = dfbvideosink->event_buffer->GetEvent (dfbvideosink->event_buffer,
          &event);
      if (ret != DFB_OK) {      /* Error */
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
400 401
        GST_WARNING_OBJECT (dfbvideosink, "failed when getting event from "
            "event buffer");
402 403 404 405 406 407 408 409 410 411 412
      } 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
413 414
              GST_DEBUG_OBJECT (dfbvideosink, "key press event %c !",
                  event.input.key_symbol);
415 416 417 418 419 420 421 422
              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
423 424
          GST_DEBUG_OBJECT (dfbvideosink, "button %d pressed at %dx%d",
              event.input.button, x, y);
425 426 427 428 429 430 431 432

          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
433 434
          GST_DEBUG_OBJECT (dfbvideosink, "button %d released at %dx%d",
              event.input.button, x, y);
435 436 437 438

          gst_navigation_send_mouse_event (GST_NAVIGATION (dfbvideosink),
              "mouse-button-release", event.input.button, x, y);
        } else if (event.input.type == DIET_AXISMOTION) {
439 440 441 442 443
          gint x, y;

          dfbvideosink->layer->GetCursorPosition (dfbvideosink->layer, &x, &y);
          gst_navigation_send_mouse_event (GST_NAVIGATION (dfbvideosink),
              "mouse-move", 0, x, y);
444
        } else {
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
445 446
          GST_WARNING_OBJECT (dfbvideosink, "unhandled event type %d",
              event.input.type);
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
        }
      }
    }
  }
  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);

468 469
  GST_DEBUG_OBJECT (dfbvideosink, "inspecting display layer %d with name: %s",
      id, desc.name);
470 471

  if ((desc.type & DLTF_VIDEO) && (desc.caps & DLCAPS_SURFACE)) {
472 473
    GST_DEBUG_OBJECT (dfbvideosink,
        "this layer can handle live video and has a surface");
474 475
  } else {
    if (desc.caps & DLCAPS_SURFACE) {
476 477
      GST_DEBUG_OBJECT (dfbvideosink,
          "this layer can not handle live video but has a surface");
478
    } else {
479
      GST_DEBUG_OBJECT (dfbvideosink, "no we can't use that layer, really...");
480 481 482 483 484 485
      goto beach;
    }
  }

  ret = dfbvideosink->dfb->GetDisplayLayer (dfbvideosink->dfb, id, &layer);
  if (ret != DFB_OK) {
486 487
    GST_WARNING_OBJECT (dfbvideosink, "failed getting display layer %s",
        desc.name);
488 489 490 491 492
    goto beach;
  }

  ret = layer->GetConfiguration (layer, &dlc);
  if (ret != DFB_OK) {
493 494
    GST_WARNING_OBJECT (dfbvideosink,
        "failed getting display layer configuration");
495 496 497 498
    goto beach;
  }

  if ((dlc.flags & DLCONF_BUFFERMODE) && (dlc.buffermode & DLBM_FRONTONLY)) {
499
    GST_DEBUG_OBJECT (dfbvideosink, "no backbuffer");
500 501
  }
  if ((dlc.flags & DLCONF_BUFFERMODE) && (dlc.buffermode & DLBM_BACKVIDEO)) {
502
    GST_DEBUG_OBJECT (dfbvideosink, "backbuffer is in video memory");
503 504 505
    backbuffer = TRUE;
  }
  if ((dlc.flags & DLCONF_BUFFERMODE) && (dlc.buffermode & DLBM_BACKSYSTEM)) {
506
    GST_DEBUG_OBJECT (dfbvideosink, "backbuffer is in system memory");
507 508 509
    backbuffer = TRUE;
  }
  if ((dlc.flags & DLCONF_BUFFERMODE) && (dlc.buffermode & DLBM_TRIPLE)) {
510
    GST_DEBUG_OBJECT (dfbvideosink, "triple buffering");
511 512 513
    backbuffer = TRUE;
  }

514 515 516 517 518 519 520 521 522
  /* 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);
  }
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540

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
541 542
  GST_DEBUG_OBJECT (dfbvideosink, "adding video mode %dx%d at %d bpp", width,
      height, bpp);
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
  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
573 574
  GST_DEBUG_OBJECT (dfbvideosink, "detected input device %s from vendor %s",
      desc.name, desc.vendor);
575 576 577 578

  /* 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
579 580
    GST_WARNING_OBJECT (dfbvideosink, "failed when getting input device id %d",
        id);
581 582 583 584 585
    goto beach;
  }

  ret = device->AttachEventBuffer (device, dfbvideosink->event_buffer);
  if (ret != DFB_OK) {
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
586 587
    GST_WARNING_OBJECT (dfbvideosink, "failed when attaching input device "
        "%d to our event buffer", id);
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
  }

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;
608 609
  dfbvideosink->fps_d = 0;
  dfbvideosink->fps_n = 0;
610 611 612 613 614 615 616
  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
617 618
    GST_DEBUG_OBJECT (dfbvideosink, "no external surface, taking over "
        "DirectFB fullscreen");
619 620
    if (!dfbvideosink->dfb) {
      DFBGraphicsDeviceDescription hw_caps;
Benjamin Otte's avatar
Benjamin Otte committed
621
      char *argv[] = { (char *) "-", (char *) "--dfb:quiet", NULL };
622 623
      int argc = 2;
      char **args;
624

Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
625
      GST_DEBUG_OBJECT (dfbvideosink, "initializing DirectFB");
626

627 628
      args = argv;
      ret = DirectFBInit (&argc, &args);
629 630

      if (ret != DFB_OK) {
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
631
        GST_WARNING_OBJECT (dfbvideosink, "DirectFB initialization failed");
632 633 634 635 636 637
        goto beach;
      }

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

      if (ret != DFB_OK) {
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
638 639
        GST_WARNING_OBJECT (dfbvideosink, "failed creating the DirectFB "
            "main object");
640 641 642
        goto beach;
      }

643
      /* Get Hardware capabilities */
644 645 646 647
      ret = dfbvideosink->dfb->GetDeviceDescription (dfbvideosink->dfb,
          &hw_caps);

      if (ret != DFB_OK) {
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
648 649
        GST_WARNING_OBJECT (dfbvideosink, "failed grabbing the hardware "
            "capabilities");
650 651 652
        goto beach;
      }

Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
653 654 655
      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);
656 657

      if (hw_caps.acceleration_mask & DFXL_BLIT) {
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
658
        GST_DEBUG_OBJECT (dfbvideosink, "Blit is accelerated");
659 660
      }
      if (hw_caps.acceleration_mask & DFXL_STRETCHBLIT) {
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
661
        GST_DEBUG_OBJECT (dfbvideosink, "StretchBlit is accelerated");
662 663
        dfbvideosink->hw_scaling = TRUE;
      } else {
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
664
        GST_DEBUG_OBJECT (dfbvideosink, "StretchBlit is not accelerated");
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
        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 */
      dfbvideosink->event_thread = g_thread_create (
          (GThreadFunc) gst_dfbvideosink_event_thread,
          dfbvideosink, TRUE, NULL);
    }
    if (!dfbvideosink->layer) {
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
690 691 692
      GList *channels_list = NULL;
      DFBDisplayLayerDescription dl_desc;

693 694 695 696
      /* 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
697
        GST_WARNING_OBJECT (dfbvideosink, "failed getting display layer");
698 699 700 701 702 703 704
        goto beach;
      }

      ret = dfbvideosink->layer->SetCooperativeLevel (dfbvideosink->layer,
          DLSCL_EXCLUSIVE);

      if (ret != DFB_OK) {
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
705 706
        GST_WARNING_OBJECT (dfbvideosink, "failed setting display layer to "
            "fullscreen mode");
707 708 709
        goto beach;
      }

Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
710 711 712 713
      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
714
        channels_list = g_list_append (channels_list, (char *) "BRIGHTNESS");
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
715 716
      }
      if (dl_desc.caps & DLCAPS_CONTRAST) {
Benjamin Otte's avatar
Benjamin Otte committed
717
        channels_list = g_list_append (channels_list, (char *) "CONTRAST");
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
718 719
      }
      if (dl_desc.caps & DLCAPS_HUE) {
Benjamin Otte's avatar
Benjamin Otte committed
720
        channels_list = g_list_append (channels_list, (char *) "HUE");
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
721 722
      }
      if (dl_desc.caps & DLCAPS_SATURATION) {
Benjamin Otte's avatar
Benjamin Otte committed
723
        channels_list = g_list_append (channels_list, (char *) "SATURATION");
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
724 725 726 727 728 729 730 731 732 733
      }

      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",
734
              (const char *) walk->data);
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790

          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);
      }

791 792 793 794 795
      dfbvideosink->layer->SetBackgroundColor (dfbvideosink->layer,
          0x00, 0x00, 0x00, 0xFF);

      dfbvideosink->layer->EnableCursor (dfbvideosink->layer, TRUE);

Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
796
      GST_DEBUG_OBJECT (dfbvideosink, "getting primary surface");
797 798
      dfbvideosink->layer->GetSurface (dfbvideosink->layer,
          &dfbvideosink->primary);
799 800 801

      dfbvideosink->primary->SetBlittingFlags (dfbvideosink->primary,
          DSBLIT_NOFX);
802 803 804 805 806 807 808
    }

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

Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
809 810
    GST_DEBUG_OBJECT (dfbvideosink, "getting pixel format from foreign "
        "surface %p", dfbvideosink->ext_surface);
811 812 813 814 815 816 817 818 819 820 821
    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
822 823
    GST_DEBUG_OBJECT (dfbvideosink, "external surface is %dx%d and uses %s "
        "pixel format", dfbvideosink->out_width, dfbvideosink->out_height,
824 825 826 827 828 829 830 831 832 833 834 835 836 837
        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
838
  GST_DEBUG_OBJECT (dfbvideosink, "cleaning up DirectFB environment");
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861

  /* 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
862 863 864 865 866 867 868 869 870 871 872 873 874
  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;
  }

875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 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
  if (dfbvideosink->buffer_pool) {
    gst_dfbvideosink_bufferpool_clear (dfbvideosink);
  }

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

  if (dfbvideosink->layer) {
    dfbvideosink->layer->EnableCursor (dfbvideosink->layer, FALSE);
    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;
  gboolean ret;
  DFBSurfacePixelFormat pixel_format = DSPF_UNKNOWN;

  g_return_val_if_fail (GST_IS_CAPS (caps), DSPF_UNKNOWN);

  structure = gst_caps_get_structure (caps, 0);

  if (gst_structure_has_name (structure, "video/x-raw-rgb")) {
    gint bpp, depth;

    ret = gst_structure_get_int (structure, "bpp", &bpp);
    ret &= gst_structure_get_int (structure, "depth", &depth);

    if (!ret) {
      goto beach;
    }

    switch (bpp) {
      case 16:
        pixel_format = DSPF_RGB16;
        break;
      case 24:
        pixel_format = DSPF_RGB24;
        break;
      case 32:
        if (depth == 24) {
          pixel_format = DSPF_RGB32;
        } else if (depth == 32) {
          pixel_format = DSPF_ARGB;
        } else {
          goto beach;
        }
        break;
      default:
        GST_WARNING ("unhandled RGB format, bpp %d, depth %d", bpp, depth);
        goto beach;
    }
  } else if (gst_structure_has_name (structure, "video/x-raw-yuv")) {
    guint32 fourcc;

    ret = gst_structure_get_fourcc (structure, "format", &fourcc);

    if (!ret) {
      GST_WARNING ("failed grabbing fourcc from caps %" GST_PTR_FORMAT, caps);
      goto beach;
    }

    switch (fourcc) {
      case GST_MAKE_FOURCC ('I', '4', '2', '0'):
        pixel_format = DSPF_I420;
        break;
      case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
        pixel_format = DSPF_YV12;
        break;
      case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
        pixel_format = DSPF_YUY2;
        break;
      case GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y'):
        pixel_format = DSPF_UYVY;
        break;
      default:
963
        GST_WARNING ("unhandled YUV format %" GST_FOURCC_FORMAT,
964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
            GST_FOURCC_ARGS (fourcc));
        goto beach;
    }
  } else {
    GST_WARNING ("unknown caps name received %" GST_PTR_FORMAT, caps);
    goto beach;
  }

beach:
  return pixel_format;
}

static GstCaps *
gst_dfbvideosink_get_caps_from_format (DFBSurfacePixelFormat format)
{
  GstCaps *caps = NULL;
  gboolean is_rgb = FALSE, is_yuv = FALSE;
  gint bpp, depth;
  guint32 fourcc;

  g_return_val_if_fail (format != DSPF_UNKNOWN, NULL);

  switch (format) {
    case DSPF_RGB16:
      is_rgb = TRUE;
      bpp = 16;
      depth = 16;
      break;
    case DSPF_RGB24:
      is_rgb = TRUE;
      bpp = 24;
      depth = 24;
      break;
    case DSPF_RGB32:
      is_rgb = TRUE;
      bpp = 32;
      depth = 24;
      break;
    case DSPF_ARGB:
      is_rgb = TRUE;
      bpp = 32;
      depth = 32;
      break;
    case DSPF_YUY2:
      is_yuv = TRUE;
      fourcc = GST_MAKE_FOURCC ('Y', 'U', 'Y', '2');
      break;
    case DSPF_UYVY:
      is_yuv = TRUE;
      fourcc = GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y');
      break;
    case DSPF_I420:
      is_yuv = TRUE;
      fourcc = GST_MAKE_FOURCC ('I', '4', '2', '0');
      break;
    case DSPF_YV12:
      is_yuv = TRUE;
      fourcc = GST_MAKE_FOURCC ('Y', 'V', '1', '2');
      break;
    default:
      GST_WARNING ("unknown pixel format %s",
          gst_dfbvideosink_get_format_name (format));
      goto beach;
  }

  if (is_rgb) {
    caps = gst_caps_new_simple ("video/x-raw-rgb",
        "bpp", G_TYPE_INT, bpp, "depth", G_TYPE_INT, depth, NULL);
  } else if (is_yuv) {
    caps = gst_caps_new_simple ("video/x-raw-yuv",
        "format", GST_TYPE_FOURCC, fourcc, NULL);
  } else {
    GST_WARNING ("neither rgb nor yuv, something strange here");
  }

beach:
  return caps;
}

static gboolean
gst_dfbvideosink_can_blit_from_format (GstDfbVideoSink * dfbvideosink,
1045
    DFBSurfacePixelFormat format, gboolean accelerated)
1046 1047 1048 1049 1050 1051
{
  gboolean res = FALSE;
  DFBResult ret;
  IDirectFBSurface *surface = NULL;
  DFBSurfaceDescription s_dsc;
  DFBAccelerationMask mask;
1052
  DFBDisplayLayerConfig dlc, prev_dlc;
1053 1054 1055 1056 1057 1058

  g_return_val_if_fail (GST_IS_DFBVIDEOSINK (dfbvideosink), FALSE);

  /* Create a surface of desired format */
  s_dsc.flags = DSDESC_PIXELFORMAT | DSDESC_WIDTH | DSDESC_HEIGHT;
  s_dsc.pixelformat = format;
1059 1060
  s_dsc.width = 10;
  s_dsc.height = 10;
1061 1062 1063

  ret = dfbvideosink->dfb->CreateSurface (dfbvideosink->dfb, &s_dsc, &surface);
  if (ret != DFB_OK) {
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
1064
    GST_WARNING_OBJECT (dfbvideosink, "failed creating surface with format %s",
1065 1066 1067 1068
        gst_dfbvideosink_get_format_name (format));
    goto beach;
  }

1069 1070 1071
  /* Backup layer configuration */
  ret = dfbvideosink->layer->GetConfiguration (dfbvideosink->layer, &prev_dlc);
  if (ret != DFB_OK) {
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
1072 1073
    GST_WARNING_OBJECT (dfbvideosink, "failed when getting current layer "
        "configuration");
1074 1075 1076
    goto beach;
  }

1077 1078 1079 1080 1081 1082 1083
  /* Test configuration of the layer to this pixel format */
  dlc.flags = DLCONF_PIXELFORMAT;
  dlc.pixelformat = format;

  ret = dfbvideosink->layer->TestConfiguration (dfbvideosink->layer, &dlc,
      NULL);
  if (ret != DFB_OK) {
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
1084 1085
    GST_DEBUG_OBJECT (dfbvideosink, "our layer refuses to operate in pixel "
        "format %s", gst_dfbvideosink_get_format_name (format));
1086 1087 1088 1089 1090
    goto beach;
  }

  ret = dfbvideosink->layer->SetConfiguration (dfbvideosink->layer, &dlc);
  if (ret != DFB_OK) {
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
1091 1092
    GST_WARNING_OBJECT (dfbvideosink, "our layer refuses to operate in pixel "
        "format, though this format was successfully tested earlied %s",
1093 1094 1095 1096 1097 1098 1099
        gst_dfbvideosink_get_format_name (format));
    goto beach;
  }

  ret = dfbvideosink->primary->GetAccelerationMask (dfbvideosink->primary,
      surface, &mask);
  if (ret != DFB_OK) {
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
1100
    GST_WARNING_OBJECT (dfbvideosink, "failed getting acceleration mask");
1101 1102 1103 1104
    goto beach;
  }

  /* Blitting from this format to our primary is accelerated */
1105
  if ((mask & DFXL_BLIT) && accelerated) {
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
1106 1107
    GST_DEBUG_OBJECT (dfbvideosink, "blitting from format %s to our primary "
        "is accelerated", gst_dfbvideosink_get_format_name (format));
1108
    res = TRUE;
1109
  } else if (!accelerated) {
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
1110 1111
    GST_DEBUG_OBJECT (dfbvideosink, "blitting from format %s to our primary "
        "is not accelerated", gst_dfbvideosink_get_format_name (format));
1112
    res = TRUE;
1113 1114 1115 1116 1117
  }

  /* Restore original layer configuration */
  ret = dfbvideosink->layer->SetConfiguration (dfbvideosink->layer, &prev_dlc);
  if (ret != DFB_OK) {
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
1118 1119
    GST_WARNING_OBJECT (dfbvideosink, "failed when restoring layer "
        "configuration");
1120
    goto beach;
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
  }

beach:
  if (surface) {
    surface->Release (surface);
  }
  return res;
}

static gboolean
gst_dfbvideosink_get_best_vmode (GstDfbVideoSink * dfbvideosink, gint v_width,
    gint v_height, GstDfbVMode * best_vmode)
{
  GSList *walk = NULL;
  gboolean ret = FALSE;
  gint width, height, bpp;
  GstDfbVMode *vmode = NULL;

  g_return_val_if_fail (GST_IS_DFBVIDEOSINK (dfbvideosink), FALSE);

1141 1142 1143 1144
  if (!dfbvideosink->vmodes) {
    goto beach;
  }

1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
  walk = dfbvideosink->vmodes;

  vmode = (GstDfbVMode *) walk->data;

  /* First mode */
  width = vmode->width;
  height = vmode->height;
  bpp = vmode->bpp;

  while (walk) {
    gint wgap, hgap, best_wgap, best_hgap;

    vmode = (GstDfbVMode *) walk->data;

    /* What are the gaps */
    wgap = abs (vmode->width - v_width);
    hgap = abs (vmode->height - v_height);
    best_wgap = abs (width - v_width);
    best_hgap = abs (height - v_height);

    /* If this mode is better we ll use that */
    if (wgap + hgap < best_wgap + best_hgap) {
      width = vmode->width;
      height = vmode->height;
      bpp = vmode->bpp;
    }

    walk = g_slist_next (walk);
  }

Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
1175 1176
  GST_DEBUG_OBJECT (dfbvideosink, "found video mode %dx%d for input at %dx%d",
      width, height, v_width, v_height);
1177 1178 1179 1180 1181 1182 1183

  best_vmode->width = width;
  best_vmode->height = height;
  best_vmode->bpp = bpp;

  ret = TRUE;

1184
beach:
1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
  return ret;
}

static GstCaps *
gst_dfbvideosink_getcaps (GstBaseSink * bsink)
{
  GstDfbVideoSink *dfbvideosink;
  GstCaps *caps = NULL;
  gint i;

  dfbvideosink = GST_DFBVIDEOSINK (bsink);

  if (!dfbvideosink->setup) {
    caps = gst_caps_copy (gst_pad_get_pad_template_caps (GST_VIDEO_SINK_PAD
            (dfbvideosink)));
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
1200
    GST_DEBUG_OBJECT (dfbvideosink, "getcaps called and we are not setup yet, "
1201 1202 1203
        "returning template %" GST_PTR_FORMAT, caps);
    goto beach;
  } else {
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
1204 1205
    GST_DEBUG_OBJECT (dfbvideosink, "getcaps called, checking our internal "
        "format");
1206 1207 1208 1209 1210 1211
    if (dfbvideosink->ext_surface) {
      /* We are not rendering to our own surface, returning this surface's
       *  pixel format */
      caps = gst_dfbvideosink_get_caps_from_format (dfbvideosink->pixel_format);
    } else {
      /* Try some formats */
1212
      gboolean accelerated = TRUE;
1213 1214
      caps = gst_caps_new_empty ();

1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
      do {
        if (gst_dfbvideosink_can_blit_from_format (dfbvideosink, DSPF_RGB16,
                accelerated)) {
          gst_caps_append (caps,
              gst_dfbvideosink_get_caps_from_format (DSPF_RGB16));
        }
        if (gst_dfbvideosink_can_blit_from_format (dfbvideosink, DSPF_RGB24,
                accelerated)) {
          gst_caps_append (caps,
              gst_dfbvideosink_get_caps_from_format (DSPF_RGB24));
        }
        if (gst_dfbvideosink_can_blit_from_format (dfbvideosink, DSPF_RGB32,
                accelerated)) {
          gst_caps_append (caps,
              gst_dfbvideosink_get_caps_from_format (DSPF_RGB32));
        }
        if (gst_dfbvideosink_can_blit_from_format (dfbvideosink, DSPF_ARGB,
                accelerated)) {
          gst_caps_append (caps,
              gst_dfbvideosink_get_caps_from_format (DSPF_ARGB));
        }
        if (gst_dfbvideosink_can_blit_from_format (dfbvideosink, DSPF_YUY2,
                accelerated)) {
          gst_caps_append (caps,
              gst_dfbvideosink_get_caps_from_format (DSPF_YUY2));
        }
        if (gst_dfbvideosink_can_blit_from_format (dfbvideosink, DSPF_UYVY,
                accelerated)) {
          gst_caps_append (caps,
              gst_dfbvideosink_get_caps_from_format (DSPF_UYVY));
        }
        if (gst_dfbvideosink_can_blit_from_format (dfbvideosink, DSPF_I420,
                accelerated)) {
          gst_caps_append (caps,
              gst_dfbvideosink_get_caps_from_format (DSPF_I420));
        }
        if (gst_dfbvideosink_can_blit_from_format (dfbvideosink, DSPF_YV12,
                accelerated)) {
          gst_caps_append (caps,
              gst_dfbvideosink_get_caps_from_format (DSPF_YV12));
        }
        accelerated = !accelerated;
      } while (accelerated == FALSE);
1258 1259 1260 1261 1262 1263 1264
    }
  }

  for (i = 0; i < gst_caps_get_size (caps); i++) {
    GstStructure *structure = gst_caps_get_structure (caps, i);

    gst_structure_set (structure,
1265 1266
        "width", GST_TYPE_INT_RANGE, 1, G_MAXINT,
        "height", GST_TYPE_INT_RANGE, 1, G_MAXINT,
1267
        "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
1268 1269 1270 1271 1272 1273 1274 1275 1276

    if (!dfbvideosink->hw_scaling && dfbvideosink->par) {
      int nom, den;

      nom = gst_value_get_fraction_numerator (dfbvideosink->par);
      den = gst_value_get_fraction_denominator (dfbvideosink->par);
      gst_structure_set (structure, "pixel-aspect-ratio",
          GST_TYPE_FRACTION, nom, den, NULL);
    }
1277 1278
  }

Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
1279
  GST_DEBUG_OBJECT (dfbvideosink, "returning our caps %" GST_PTR_FORMAT, caps);
1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291

beach:
  return caps;
}

static gboolean
gst_dfbvideosink_setcaps (GstBaseSink * bsink, GstCaps * caps)
{
  GstDfbVideoSink *dfbvideosink;
  GstStructure *structure;
  gboolean res, result = FALSE;
  gint video_width, video_height;
1292
  const GValue *framerate;
1293 1294 1295 1296 1297 1298 1299
  DFBSurfacePixelFormat pixel_format = DSPF_UNKNOWN;

  dfbvideosink = GST_DFBVIDEOSINK (bsink);

  structure = gst_caps_get_structure (caps, 0);
  res = gst_structure_get_int (structure, "width", &video_width);
  res &= gst_structure_get_int (structure, "height", &video_height);
1300 1301
  framerate = gst_structure_get_value (structure, "framerate");
  res &= (framerate != NULL);
1302 1303 1304 1305
  if (!res) {
    goto beach;
  }

1306 1307 1308
  dfbvideosink->fps_n = gst_value_get_fraction_numerator (framerate);
  dfbvideosink->fps_d = gst_value_get_fraction_denominator (framerate);

1309 1310
  pixel_format = gst_dfbvideosink_get_format_from_caps (caps);

Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
1311 1312
  GST_DEBUG_OBJECT (dfbvideosink, "setcaps called with %" GST_PTR_FORMAT, caps);
  GST_DEBUG_OBJECT (dfbvideosink, "our format is: %dx%d %s video at %d/%d fps",
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
1313 1314 1315
      video_width, video_height,
      gst_dfbvideosink_get_format_name (pixel_format), dfbvideosink->fps_n,
      dfbvideosink->fps_d);
1316

Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394
  if (dfbvideosink->hw_scaling && dfbvideosink->par) {
    gint video_par_n, video_par_d;      /* video's PAR */
    gint display_par_n, display_par_d;  /* display's PAR */
    gint num, den;
    GValue display_ratio = { 0, };      /* display w/h ratio */
    const GValue *caps_par;

    /* get aspect ratio from caps if it's present, and
     * convert video width and height to a display width and height
     * using wd / hd = wv / hv * PARv / PARd
     * the ratio wd / hd will be stored in display_ratio */
    g_value_init (&display_ratio, GST_TYPE_FRACTION);

    /* get video's PAR */
    caps_par = gst_structure_get_value (structure, "pixel-aspect-ratio");
    if (caps_par) {
      video_par_n = gst_value_get_fraction_numerator (caps_par);
      video_par_d = gst_value_get_fraction_denominator (caps_par);
    } else {
      video_par_n = 1;
      video_par_d = 1;
    }
    /* get display's PAR */
    if (dfbvideosink->par) {
      display_par_n = gst_value_get_fraction_numerator (dfbvideosink->par);
      display_par_d = gst_value_get_fraction_denominator (dfbvideosink->par);
    } else {
      display_par_n = 1;
      display_par_d = 1;
    }

    gst_value_set_fraction (&display_ratio,
        video_width * video_par_n * display_par_d,
        video_height * video_par_d * display_par_n);

    num = gst_value_get_fraction_numerator (&display_ratio);
    den = gst_value_get_fraction_denominator (&display_ratio);
    GST_DEBUG_OBJECT (dfbvideosink,
        "video width/height: %dx%d, calculated display ratio: %d/%d",
        video_width, video_height, num, den);

    /* now find a width x height that respects this display ratio.
     * prefer those that have one of w/h the same as the incoming video
     * using wd / hd = num / den */

    /* start with same height, because of interlaced video */
    /* check hd / den is an integer scale factor, and scale wd with the PAR */
    if (video_height % den == 0) {
      GST_DEBUG_OBJECT (dfbvideosink, "keeping video height");
      GST_VIDEO_SINK_WIDTH (dfbvideosink) = video_height * num / den;
      GST_VIDEO_SINK_HEIGHT (dfbvideosink) = video_height;
    } else if (video_width % num == 0) {
      GST_DEBUG_OBJECT (dfbvideosink, "keeping video width");
      GST_VIDEO_SINK_WIDTH (dfbvideosink) = video_width;
      GST_VIDEO_SINK_HEIGHT (dfbvideosink) = video_width * den / num;
    } else {
      GST_DEBUG_OBJECT (dfbvideosink, "approximating while keeping height");
      GST_VIDEO_SINK_WIDTH (dfbvideosink) = video_height * num / den;
      GST_VIDEO_SINK_HEIGHT (dfbvideosink) = video_height;
    }
    GST_DEBUG_OBJECT (dfbvideosink, "scaling to %dx%d",
        GST_VIDEO_SINK_WIDTH (dfbvideosink),
        GST_VIDEO_SINK_HEIGHT (dfbvideosink));
  } else {
    if (dfbvideosink->par) {
      const GValue *par;

      par = gst_structure_get_value (structure, "pixel-aspect-ratio");
      if (par) {
        if (gst_value_compare (par, dfbvideosink->par) != GST_VALUE_EQUAL) {
          goto wrong_aspect;
        }
      }
    }
    GST_VIDEO_SINK_WIDTH (dfbvideosink) = video_width;
    GST_VIDEO_SINK_HEIGHT (dfbvideosink) = video_height;
  }

1395 1396 1397 1398 1399
  /* Try to adapt the video mode to the video geometry */
  if (dfbvideosink->dfb) {
    DFBResult ret;
    GstDfbVMode vmode;

Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
1400 1401
    GST_DEBUG_OBJECT (dfbvideosink, "trying to adapt the video mode to video "
        "geometry");
1402 1403

    /* Set video mode and layer configuration appropriately */
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
1404 1405 1406
    if (gst_dfbvideosink_get_best_vmode (dfbvideosink,
            GST_VIDEO_SINK_WIDTH (dfbvideosink),
            GST_VIDEO_SINK_HEIGHT (dfbvideosink), &vmode)) {
1407 1408 1409 1410 1411 1412 1413
      DFBDisplayLayerConfig lc;
      gint width, height, bpp;

      width = vmode.width;
      height = vmode.height;
      bpp = vmode.bpp;

Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
1414 1415
      GST_DEBUG_OBJECT (dfbvideosink, "setting video mode to %dx%d at %d bpp",
          width, height, bpp);
1416 1417 1418 1419

      ret = dfbvideosink->dfb->SetVideoMode (dfbvideosink->dfb, width,
          height, bpp);
      if (ret != DFB_OK) {
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
1420 1421
        GST_WARNING_OBJECT (dfbvideosink, "failed setting video mode %dx%d "
            "at %d bpp", width, height, bpp);
1422 1423
      }

Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
1424
      lc.flags = DLCONF_PIXELFORMAT;
1425 1426 1427 1428
      lc.pixelformat = pixel_format;

      ret = dfbvideosink->layer->SetConfiguration (dfbvideosink->layer, &lc);
      if (ret != DFB_OK) {
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
1429 1430
        GST_WARNING_OBJECT (dfbvideosink, "failed setting layer pixelformat "
            "to %s", gst_dfbvideosink_get_format_name (pixel_format));
1431
      } else {
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
1432 1433 1434 1435
        dfbvideosink->layer->GetConfiguration (dfbvideosink->layer, &lc);
        dfbvideosink->out_width = lc.width;
        dfbvideosink->out_height = lc.height;
        dfbvideosink->pixel_format = lc.pixelformat;
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
1436
        GST_DEBUG_OBJECT (dfbvideosink, "layer %d now configured to %dx%d %s",
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
1437 1438
            dfbvideosink->layer_id, lc.width, lc.height,
            gst_dfbvideosink_get_format_name (lc.pixelformat));
1439 1440 1441 1442 1443
      }
    }
  }

  if (pixel_format != dfbvideosink->pixel_format) {
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
1444 1445
    GST_WARNING_OBJECT (dfbvideosink, "setcaps sent us a different pixel "
        "format %s", gst_dfbvideosink_get_format_name (pixel_format));
1446 1447 1448 1449 1450 1451 1452 1453 1454 1455
    goto beach;
  }

  dfbvideosink->video_width = video_width;
  dfbvideosink->video_height = video_height;

  result = TRUE;

beach:
  return result;
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
1456 1457 1458 1459 1460 1461 1462

/* ERRORS */
wrong_aspect:
  {
    GST_INFO_OBJECT (dfbvideosink, "pixel aspect ratio does not match");
    return FALSE;
  }
1463 1464 1465 1466 1467 1468
}

static GstStateChangeReturn
gst_dfbvideosink_change_state (GstElement * element, GstStateChange transition)
{
  GstDfbVideoSink *dfbvideosink;
1469
  GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1470 1471 1472 1473 1474 1475 1476

  dfbvideosink = GST_DFBVIDEOSINK (element);

  switch (transition) {
    case GST_STATE_CHANGE_NULL_TO_READY:
      dfbvideosink->running = TRUE;
      if (!dfbvideosink->setup) {
1477 1478 1479
        if (!gst_dfbvideosink_setup (dfbvideosink)) {
          GST_DEBUG_OBJECT (dfbvideosink, "setup failed when changing state "
              "from NULL to READY");
1480 1481
          GST_ELEMENT_ERROR (dfbvideosink, RESOURCE, OPEN_WRITE,
              (NULL), ("Failed initializing DirectFB system"));
1482 1483
          return GST_STATE_CHANGE_FAILURE;
        }
1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498
      }
      break;
    case GST_STATE_CHANGE_READY_TO_PAUSED:
      /* Blank surface if we have one */
      if (dfbvideosink->ext_surface) {
        dfbvideosink->ext_surface->Clear (dfbvideosink->ext_surface,
            0x00, 0x00, 0x00, 0xFF);
      }
      if (dfbvideosink->primary) {
        dfbvideosink->primary->Clear (dfbvideosink->primary, 0x00, 0x00,
            0x00, 0xFF);
      }
      break;
    case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
      break;
1499 1500 1501 1502 1503 1504 1505 1506 1507
    default:
      break;
  }

  ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
  if (ret == GST_STATE_CHANGE_FAILURE)
    return ret;

  switch (transition) {
1508 1509 1510
    case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
      break;
    case GST_STATE_CHANGE_PAUSED_TO_READY:
1511 1512
      dfbvideosink->fps_d = 0;
      dfbvideosink->fps_n = 0;
1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525
      dfbvideosink->video_width = 0;
      dfbvideosink->video_height = 0;

      if (dfbvideosink->buffer_pool) {
        gst_dfbvideosink_bufferpool_clear (dfbvideosink);
      }
      break;
    case GST_STATE_CHANGE_READY_TO_NULL:
      dfbvideosink->running = FALSE;
      if (dfbvideosink->setup) {
        gst_dfbvideosink_cleanup (dfbvideosink);
      }
      break;
1526 1527
    default:
      break;
1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545
  }

  return ret;
}

static void
gst_dfbvideosink_get_times (GstBaseSink * bsink, GstBuffer * buf,
    GstClockTime * start, GstClockTime * end)
{
  GstDfbVideoSink *dfbvideosink;

  dfbvideosink = GST_DFBVIDEOSINK (bsink);

  if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
    *start = GST_BUFFER_TIMESTAMP (buf);
    if (GST_BUFFER_DURATION_IS_VALID (buf)) {
      *end = *start + GST_BUFFER_DURATION (buf);
    } else {
1546 1547 1548
      if (dfbvideosink->fps_n > 0) {
        *end =
            *start + (GST_SECOND * dfbvideosink->fps_d) / dfbvideosink->fps_n;
1549 1550 1551 1552 1553 1554 1555 1556
      }
    }
  }
}

static GstFlowReturn
gst_dfbvideosink_show_frame (GstBaseSink * bsink, GstBuffer * buf)
{
1557
  GstDfbVideoSink *dfbvideosink = NULL;
1558
  DFBResult res;
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
1559
  GstVideoRectangle dst, src, result;
1560
  GstFlowReturn ret = GST_FLOW_OK;
1561
  gboolean mem_cpy = TRUE;
Wim Taymans's avatar
Wim Taymans committed
1562
  GstMetaDfbSurface *meta;
1563 1564 1565 1566 1567 1568 1569 1570

  dfbvideosink = GST_DFBVIDEOSINK (bsink);

  if (!dfbvideosink->setup) {
    ret = GST_FLOW_UNEXPECTED;
    goto beach;
  }

Wim Taymans's avatar
Wim Taymans committed
1571
  meta = GST_META_DFBSURFACE_GET (buf);
1572

Wim Taymans's avatar
Wim Taymans committed
1573 1574
  /* Is that a buffer we allocated ourselves ? */
  if (meta != NULL) {
1575
    /* Does it have a surface ? */
Wim Taymans's avatar
Wim Taymans committed
1576
    if (meta->surface) {
1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591
      mem_cpy = FALSE;
      GST_DEBUG_OBJECT (dfbvideosink, "we have a buffer (%p) we allocated "
          "ourselves and it has a surface, no memcpy then", buf);
    } else {
      /* No surface, that's a malloc */
      GST_DEBUG_OBJECT (dfbvideosink, "we have a buffer (%p) we allocated "
          "ourselves but it does not hold a surface", buf);
    }
  } else {
    /* Not our baby */
    GST_DEBUG_OBJECT (dfbvideosink, "we have a buffer (%p) we did not allocate",
        buf);
  }

  if (mem_cpy) {
1592
    IDirectFBSurface *dest = NULL, *surface = NULL;
1593
    guint8 *data;
1594
    gint dest_pitch, src_pitch, line;
Julien Moutte Moutte's avatar
Julien Moutte Moutte committed
1595
    GstStructure *structure;
1596

1597 1598 1599 1600
    /* As we are not blitting no acceleration is possible. If the surface is
     * too small we do clipping, if it's too big we center. Theoretically as 
     * we are using buffer_alloc, there's a chance that we have been able to 
     * do reverse caps negotiation */
1601

1602 1603
    if (dfbvideosink->ext_surface) {
      surface = dfbvideosink->ext_surface;
Julien Moutte Moutte's avatar