gsthermescolorspace.c 19.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
/* GStreamer
 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
 *
 * 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.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <gst/gst.h>
#include <gst/video/video.h>
#include <Hermes/Hermes.h>


#define GST_TYPE_COLORSPACE \
  (gst_hermes_colorspace_get_type())
#define GST_HERMES_COLORSPACE(obj) \
  (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_COLORSPACE,GstHermesColorspace))
#define GST_HERMES_COLORSPACE_CLASS(klass) \
  (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_ULAW,GstHermesColorspace))
#define GST_IS_COLORSPACE(obj) \
  (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_COLORSPACE))
#define GST_IS_COLORSPACE_CLASS(obj) \
  (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_COLORSPACE))

typedef struct _GstHermesColorspace GstHermesColorspace;
typedef struct _GstHermesColorspaceClass GstHermesColorspaceClass;

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
42 43
typedef enum
{
44 45 46 47 48 49
  GST_HERMES_COLORSPACE_NONE,
  GST_HERMES_COLORSPACE_HERMES,
  GST_HERMES_COLORSPACE_YUV_RGB,
  GST_HERMES_COLORSPACE_YUY2_I420,
  GST_HERMES_COLORSPACE_RGB32_I420,
  GST_HERMES_COLORSPACE_RGB32_YV12,
50
  GST_HERMES_COLORSPACE_420_SWAP
51 52
}
GstColorSpaceConverterType;
53

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
54 55
struct _GstHermesColorspace
{
56 57
  GstElement element;

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
58
  GstPad *sinkpad, *srcpad;
59 60 61 62 63 64 65 66 67 68

  HermesHandle h_handle;
  HermesFormat sink_format;
  HermesFormat src_format;

  int src_format_index;
  int sink_format_index;

  int src_size;
  int sink_size;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
69

70 71 72 73 74 75 76 77
  int src_stride;
  int sink_stride;

  gint width, height;
  gdouble fps;
  gboolean passthru;
};

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
78 79
struct _GstHermesColorspaceClass
{
80 81 82
  GstElementClass parent_class;
};

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
83
GType gst_hermes_colorspace_get_type (void);
84

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
85 86
typedef struct _GstHermesColorspaceFormat
{
87 88
  GstStaticCaps caps;

89 90
}
GstHermesColorspaceFormat;
91 92

static GstHermesColorspaceFormat gst_hermes_colorspace_formats[] = {
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
93 94 95 96 97 98 99 100
  {GST_STATIC_CAPS (GST_VIDEO_CAPS_xRGB)},
  {GST_STATIC_CAPS (GST_VIDEO_CAPS_RGBx)},
  {GST_STATIC_CAPS (GST_VIDEO_CAPS_BGRx)},
  {GST_STATIC_CAPS (GST_VIDEO_CAPS_xBGR)},
  {GST_STATIC_CAPS (GST_VIDEO_CAPS_BGR)},
  {GST_STATIC_CAPS (GST_VIDEO_CAPS_RGB)},
  {GST_STATIC_CAPS (GST_VIDEO_CAPS_RGB_15)},
  {GST_STATIC_CAPS (GST_VIDEO_CAPS_RGB_16)},
101 102 103 104 105 106 107 108 109 110 111
};

static GstElementDetails colorspace_details = {
  "Colorspace converter",
  "Filter/Converter/Video",
  "Converts video from one colorspace to another using libhermes",
  "Wim Taymans <wim.taymans@chello.be>",
};


/* Stereo signals and args */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
112 113
enum
{
114 115 116 117
  /* FILL ME */
  LAST_SIGNAL
};

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
118 119
enum
{
120 121
  ARG_0,
  ARG_SOURCE,
122
  ARG_DEST
123 124
};

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
125 126 127
static void gst_hermes_colorspace_base_init (gpointer g_class);
static void gst_hermes_colorspace_class_init (GstHermesColorspaceClass * klass);
static void gst_hermes_colorspace_init (GstHermesColorspace * space);
128

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
129 130 131 132
static void gst_hermes_colorspace_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec);
static void gst_hermes_colorspace_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec);
133 134

static GstPadLinkReturn
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
135 136
gst_hermes_colorspace_link (GstPad * pad, const GstCaps * caps);
static void gst_hermes_colorspace_chain (GstPad * pad, GstData * _data);
137
static GstElementStateReturn
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
138
gst_hermes_colorspace_change_state (GstElement * element);
139 140 141


static GstElementClass *parent_class = NULL;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
142

143 144 145
/*static guint gst_hermes_colorspace_signals[LAST_SIGNAL] = { 0 }; */

#if 0
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
146 147 148
static gboolean
colorspace_setup_converter (GstHermesColorspace * space, GstCaps * from_caps,
    GstCaps * to_caps)
149 150 151 152 153 154 155 156 157 158 159
{
  guint32 from_space, to_space;
  GstStructure *from_struct;
  GstStructure *to_struct;

  g_return_val_if_fail (to_caps != NULL, FALSE);
  g_return_val_if_fail (from_caps != NULL, FALSE);

  from_struct = gst_caps_get_structure (from_caps, 0);
  to_struct = gst_caps_get_structure (to_caps, 0);

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
160
  from_space = GST_MAKE_FOURCC ('R', 'G', 'B', ' ');
161 162
  gst_structure_get_fourcc (from_struct, "format", &from_space);

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
163
  to_space = GST_MAKE_FOURCC ('R', 'G', 'B', ' ');
164 165
  gst_structure_get_fourcc (to_struct, "format", &to_space);

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
166 167 168 169
  GST_INFO ("set up converter for " GST_FOURCC_FORMAT
      " (%08x) to " GST_FOURCC_FORMAT " (%08x)",
      GST_FOURCC_ARGS (from_space), from_space,
      GST_FOURCC_ARGS (to_space), to_space);
170 171

  switch (from_space) {
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
172
    case GST_MAKE_FOURCC ('R', 'G', 'B', ' '):
173 174
    {
      gint from_bpp;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
175

176 177 178
      gst_structure_get_int (from_struct, "bpp", &from_bpp);

      switch (to_space) {
179
        case GST_MAKE_FOURCC ('R', 'G', 'B', ' '):
180
#ifdef HAVE_HERMES
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
        {
          gint to_bpp;

          gst_structure_get_int (to_struct, "bpp", &to_bpp);

          gst_structure_get_int (from_struct, "red_mask", &space->source.r);
          gst_structure_get_int (from_struct, "green_mask", &space->source.g);
          gst_structure_get_int (from_struct, "blue_mask", &space->source.b);
          space->source.a = 0;
          space->srcbpp = space->source.bits = from_bpp;
          space->source.indexed = 0;
          space->source.has_colorkey = 0;

          GST_INFO ("source red mask   %08x", space->source.r);
          GST_INFO ("source green mask %08x", space->source.g);
          GST_INFO ("source blue mask  %08x", space->source.b);
          GST_INFO ("source bpp        %08x", space->srcbpp);

          gst_structure_get_int (to_struct, "red_mask", &space->dest.r);
          gst_structure_get_int (to_struct, "green_mask", &space->dest.g);
          gst_structure_get_int (to_struct, "blue_mask", &space->dest.b);
          space->dest.a = 0;
          space->destbpp = space->dest.bits = to_bpp;
          space->dest.indexed = 0;
          space->dest.has_colorkey = 0;

          GST_INFO ("dest red mask   %08x", space->dest.r);
          GST_INFO ("dest green mask %08x", space->dest.g);
          GST_INFO ("dest blue mask  %08x", space->dest.b);
          GST_INFO ("dest bpp        %08x", space->destbpp);

          if (!Hermes_ConverterRequest (space->h_handle, &space->source,
                  &space->dest)) {
            g_warning ("Hermes: could not get converter\n");
            return FALSE;
          }
          GST_INFO ("converter set up");
          space->type = GST_HERMES_COLORSPACE_HERMES;
          return TRUE;
        }
221
#else
222 223
          g_warning ("colorspace: compiled without hermes!");
          return FALSE;
224
#endif
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
        case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
          if (from_bpp == 32) {
            space->type = GST_HERMES_COLORSPACE_RGB32_YV12;
            space->destbpp = 12;
            return TRUE;
          }
        case GST_MAKE_FOURCC ('I', '4', '2', '0'):
          if (from_bpp == 32) {
            space->type = GST_HERMES_COLORSPACE_RGB32_I420;
            space->destbpp = 12;
            return TRUE;
          }
        case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
          GST_INFO ("colorspace: RGB to YUV with bpp %d not implemented!!",
              from_bpp);
          return FALSE;
241 242 243
      }
      break;
    }
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
244
    case GST_MAKE_FOURCC ('I', '4', '2', '0'):
245
      switch (to_space) {
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
        case GST_MAKE_FOURCC ('R', 'G', 'B', ' '):
          GST_INFO ("colorspace: YUV to RGB");

          gst_structure_get_int (to_struct, "bpp", &space->destbpp);
          space->converter =
              gst_hermes_colorspace_yuv2rgb_get_converter (from_caps, to_caps);
          space->type = GST_HERMES_COLORSPACE_YUV_RGB;
          return TRUE;
        case GST_MAKE_FOURCC ('I', '4', '2', '0'):
          space->type = GST_HERMES_COLORSPACE_NONE;
          space->destbpp = 12;
          return TRUE;
        case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
          space->type = GST_HERMES_COLORSPACE_420_SWAP;
          space->destbpp = 12;
          return TRUE;
262 263 264

      }
      break;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
265
    case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
266
      switch (to_space) {
267 268 269 270 271 272 273 274 275 276 277
        case GST_MAKE_FOURCC ('I', '4', '2', '0'):
          space->type = GST_HERMES_COLORSPACE_YUY2_I420;
          space->destbpp = 12;
          return TRUE;
        case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
          space->type = GST_HERMES_COLORSPACE_NONE;
          space->destbpp = 16;
          return TRUE;
        case GST_MAKE_FOURCC ('R', 'G', 'B', ' '):
          GST_INFO ("colorspace: YUY2 to RGB not implemented!!");
          return FALSE;
278 279
      }
      break;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
280
    case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
281
      switch (to_space) {
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
        case GST_MAKE_FOURCC ('R', 'G', 'B', ' '):
          GST_INFO ("colorspace: YV12 to RGB");

          gst_structure_get_int (to_struct, "bpp", &space->destbpp);
          space->converter =
              gst_hermes_colorspace_yuv2rgb_get_converter (from_caps, to_caps);
          space->type = GST_HERMES_COLORSPACE_YUV_RGB;
          return TRUE;
        case GST_MAKE_FOURCC ('I', '4', '2', '0'):
          space->type = GST_HERMES_COLORSPACE_420_SWAP;
          space->destbpp = 12;
          return TRUE;
        case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
          space->type = GST_HERMES_COLORSPACE_NONE;
          space->destbpp = 12;
          return TRUE;
298 299 300 301 302 303 304 305
      }
      break;
  }
  return FALSE;
}
#endif

static GstCaps *
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
306
gst_hermes_colorspace_caps_remove_format_info (GstCaps * caps)
307 308 309 310
{
  int i;
  GstStructure *structure;

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
311
  for (i = 0; i < gst_caps_get_size (caps); i++) {
312 313 314 315 316 317 318 319 320 321 322
    structure = gst_caps_get_structure (caps, i);

    gst_structure_remove_field (structure, "format");
    gst_structure_remove_field (structure, "endianness");
    gst_structure_remove_field (structure, "depth");
    gst_structure_remove_field (structure, "bpp");
    gst_structure_remove_field (structure, "red_mask");
    gst_structure_remove_field (structure, "green_mask");
    gst_structure_remove_field (structure, "blue_mask");
  }

323
  gst_caps_do_simplify (caps);
324

325
  return caps;
326 327 328
}

static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
329 330
gst_hermes_colorspace_structure_to_hermes_format (HermesFormat * format,
    GstStructure * structure)
331
{
Ronald S. Bultje's avatar
Ronald S. Bultje committed
332 333 334
  gst_structure_get_int (structure, "red_mask", (gint *) & format->r);
  gst_structure_get_int (structure, "green_mask", (gint *) & format->g);
  gst_structure_get_int (structure, "blue_mask", (gint *) & format->b);
335
  format->a = 0;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
336
  gst_structure_get_int (structure, "bpp", &format->bits);
337 338 339 340
  format->indexed = 0;
  format->has_colorkey = 0;
}

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
341 342
static GstCaps *
gst_hermes_colorspace_getcaps (GstPad * pad)
343 344 345 346 347
{
  GstHermesColorspace *space;
  GstPad *otherpad;
  GstCaps *othercaps;
  GstCaps *caps;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
348

349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
  space = GST_HERMES_COLORSPACE (gst_pad_get_parent (pad));

  otherpad = (pad == space->srcpad) ? space->sinkpad : space->srcpad;

  othercaps = gst_pad_get_allowed_caps (otherpad);

  othercaps = gst_hermes_colorspace_caps_remove_format_info (othercaps);

  caps = gst_caps_intersect (othercaps, gst_pad_get_pad_template_caps (pad));
  gst_caps_free (othercaps);

  return caps;
}

static GstPadLinkReturn
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
364
gst_hermes_colorspace_link (GstPad * pad, const GstCaps * caps)
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
{
  GstHermesColorspace *space;
  GstPad *otherpad;
  GstStructure *structure;
  GstPadLinkReturn link_ret;
  int width, height;
  double fps;
  int i;

  space = GST_HERMES_COLORSPACE (gst_pad_get_parent (pad));
  otherpad = (pad == space->sinkpad) ? space->srcpad : space->sinkpad;

  link_ret = gst_pad_try_set_caps (otherpad, caps);
  if (link_ret == GST_PAD_LINK_OK) {
    space->passthru = TRUE;
    return link_ret;
  }

  structure = gst_caps_get_structure (caps, 0);

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
385
  for (i = 0; i < G_N_ELEMENTS (gst_hermes_colorspace_formats); i++) {
386 387
    GstCaps *icaps;
    GstCaps *fcaps;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
388 389

    fcaps =
390 391
        gst_caps_copy (gst_static_caps_get (&gst_hermes_colorspace_formats[i].
            caps));
392 393 394 395 396 397 398

    icaps = gst_caps_intersect (caps, fcaps);
    if (!gst_caps_is_empty (icaps)) {
      break;
    }
    gst_caps_free (icaps);
  }
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
399
  if (i == G_N_ELEMENTS (gst_hermes_colorspace_formats)) {
400 401 402 403 404 405 406 407
    g_assert_not_reached ();
    return GST_PAD_LINK_REFUSED;
  }

  gst_structure_get_int (structure, "width", &width);
  gst_structure_get_int (structure, "height", &height);
  gst_structure_get_double (structure, "framerate", &fps);

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
408
  GST_INFO ("size: %dx%d", space->width, space->height);
409 410 411

  if (gst_pad_is_negotiated (otherpad)) {
    GstCaps *othercaps;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
412

413 414 415
    othercaps = gst_caps_copy (gst_pad_get_negotiated_caps (otherpad));

    gst_caps_set_simple (othercaps,
416 417
        "width", G_TYPE_INT, width,
        "height", G_TYPE_INT, height, "framerate", G_TYPE_DOUBLE, fps, NULL);
418 419 420 421 422 423 424 425 426

    link_ret = gst_pad_try_set_caps (otherpad, othercaps);
    if (link_ret != GST_PAD_LINK_OK) {
      return link_ret;
    }
  }

  if (pad == space->srcpad) {
    space->src_format_index = i;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
427
    gst_hermes_colorspace_structure_to_hermes_format (&space->src_format,
428
        structure);
429 430
  } else {
    space->sink_format_index = i;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
431
    gst_hermes_colorspace_structure_to_hermes_format (&space->sink_format,
432
        structure);
433 434
  }

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
435 436
  space->sink_stride = width * (space->sink_format.bits / 8);
  space->src_stride = width * (space->src_format.bits / 8);
437 438 439 440 441 442 443 444
  space->sink_size = space->sink_stride * height;
  space->src_size = space->src_stride * height;
  space->width = width;
  space->height = height;
  space->fps = fps;

  if (gst_pad_is_negotiated (otherpad)) {
    if (!Hermes_ConverterRequest (space->h_handle, &space->sink_format,
445
            &space->src_format)) {
446 447 448
      g_warning ("Hermes: could not get converter\n");
      return GST_PAD_LINK_REFUSED;
    }
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
449
    g_print ("inited\n");
450 451 452 453 454 455 456 457 458 459 460 461
  }

  return GST_PAD_LINK_OK;
}

GType
gst_hermes_colorspace_get_type (void)
{
  static GType colorspace_type = 0;

  if (!colorspace_type) {
    static const GTypeInfo colorspace_info = {
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
462
      sizeof (GstHermesColorspaceClass),
463 464
      gst_hermes_colorspace_base_init,
      NULL,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
465
      (GClassInitFunc) gst_hermes_colorspace_class_init,
466 467
      NULL,
      NULL,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
468
      sizeof (GstHermesColorspace),
469
      0,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
470
      (GInstanceInitFunc) gst_hermes_colorspace_init,
471
    };
472

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
473
    colorspace_type =
474 475
        g_type_register_static (GST_TYPE_ELEMENT, "GstHermesColorspace",
        &colorspace_info, 0);
476 477 478 479
  }
  return colorspace_type;
}

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
480 481
static GstStaticPadTemplate gst_hermes_colorspace_src_pad_template =
    GST_STATIC_PAD_TEMPLATE ("src",
482 483
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
484
    GST_STATIC_CAPS (GST_VIDEO_CAPS_xRGB "; "
485 486 487 488 489 490
        GST_VIDEO_CAPS_RGBx "; "
        GST_VIDEO_CAPS_BGRx "; "
        GST_VIDEO_CAPS_xBGR "; "
        GST_VIDEO_CAPS_BGR "; "
        GST_VIDEO_CAPS_RGB "; "
        GST_VIDEO_CAPS_RGB_16 "; " GST_VIDEO_CAPS_RGB_15)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
491 492 493 494
    );

static GstStaticPadTemplate gst_hermes_colorspace_sink_pad_template =
    GST_STATIC_PAD_TEMPLATE ("sink",
495 496
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
497
    GST_STATIC_CAPS (GST_VIDEO_CAPS_xRGB "; "
498 499 500 501 502 503
        GST_VIDEO_CAPS_RGBx "; "
        GST_VIDEO_CAPS_BGRx "; "
        GST_VIDEO_CAPS_xBGR "; "
        GST_VIDEO_CAPS_BGR "; "
        GST_VIDEO_CAPS_RGB "; "
        GST_VIDEO_CAPS_RGB_16 "; " GST_VIDEO_CAPS_RGB_15)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
504
    );
505 506 507 508 509

static void
gst_hermes_colorspace_base_init (gpointer g_class)
{
  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
510

511 512 513
  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&gst_hermes_colorspace_src_pad_template));
  gst_element_class_add_pad_template (element_class,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
514
      gst_static_pad_template_get (&gst_hermes_colorspace_sink_pad_template));
515 516 517

  gst_element_class_set_details (element_class, &colorspace_details);
}
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
518

519
static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
520
gst_hermes_colorspace_class_init (GstHermesColorspaceClass * klass)
521 522 523 524
{
  GObjectClass *gobject_class;
  GstElementClass *gstelement_class;

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
525 526
  gobject_class = (GObjectClass *) klass;
  gstelement_class = (GstElementClass *) klass;
527

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
528
  parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
529 530 531 532 533 534 535 536

  gobject_class->set_property = gst_hermes_colorspace_set_property;
  gobject_class->get_property = gst_hermes_colorspace_get_property;

  gstelement_class->change_state = gst_hermes_colorspace_change_state;
}

static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
537
gst_hermes_colorspace_init (GstHermesColorspace * space)
538
{
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
539 540 541
  space->sinkpad =
      gst_pad_new_from_template (gst_static_pad_template_get
      (&gst_hermes_colorspace_sink_pad_template), "sink");
542 543
  gst_pad_set_link_function (space->sinkpad, gst_hermes_colorspace_link);
  gst_pad_set_getcaps_function (space->sinkpad, gst_hermes_colorspace_getcaps);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
544 545
  gst_pad_set_chain_function (space->sinkpad, gst_hermes_colorspace_chain);
  gst_element_add_pad (GST_ELEMENT (space), space->sinkpad);
546

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
547 548 549 550
  space->srcpad =
      gst_pad_new_from_template (gst_static_pad_template_get
      (&gst_hermes_colorspace_src_pad_template), "src");
  gst_element_add_pad (GST_ELEMENT (space), space->srcpad);
551 552 553 554 555 556 557
  gst_pad_set_link_function (space->srcpad, gst_hermes_colorspace_link);

  space->h_handle = Hermes_ConverterInstance (0);
  space->passthru = FALSE;
}

static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
558
gst_hermes_colorspace_chain (GstPad * pad, GstData * _data)
559 560 561 562 563 564 565 566 567 568
{
  GstBuffer *buf = GST_BUFFER (_data);
  GstHermesColorspace *space;
  GstBuffer *outbuf = NULL;

  g_return_if_fail (pad != NULL);
  g_return_if_fail (GST_IS_PAD (pad));
  g_return_if_fail (buf != NULL);

  space = GST_HERMES_COLORSPACE (gst_pad_get_parent (pad));
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
569

570 571 572 573 574 575 576 577 578 579 580 581 582 583
  g_return_if_fail (space != NULL);
  g_return_if_fail (GST_IS_COLORSPACE (space));

  if (space->passthru) {
    gst_pad_push (space->srcpad, _data);
    return;
  }

  if (GST_BUFFER_SIZE (buf) < space->sink_size) {
    g_critical ("input size is smaller than expected");
  }

  outbuf = gst_pad_alloc_buffer (space->srcpad, GST_BUFFER_OFFSET_NONE,
      space->src_size);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
584 585

  Hermes_ConverterCopy (space->h_handle,
586 587 588 589 590 591 592 593 594 595 596 597
      GST_BUFFER_DATA (buf), 0, 0, space->width, space->height,
      space->sink_stride, GST_BUFFER_DATA (outbuf), 0, 0,
      space->width, space->height, space->src_stride);

  GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buf);
  GST_BUFFER_DURATION (outbuf) = GST_BUFFER_DURATION (buf);

  gst_buffer_unref (buf);
  gst_pad_push (space->srcpad, GST_DATA (outbuf));
}

static GstElementStateReturn
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
598
gst_hermes_colorspace_change_state (GstElement * element)
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
{
  GstHermesColorspace *space;

  space = GST_HERMES_COLORSPACE (element);

  switch (GST_STATE_TRANSITION (element)) {
    case GST_STATE_PAUSED_TO_PLAYING:
      break;
    case GST_STATE_PLAYING_TO_PAUSED:
      break;
    case GST_STATE_PAUSED_TO_READY:
      break;
  }

  return parent_class->change_state (element);
}

static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
617 618
gst_hermes_colorspace_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec)
619 620 621 622
{
  GstHermesColorspace *space;

  /* it's not null if we got it, but it might not be ours */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
623 624
  g_return_if_fail (GST_IS_COLORSPACE (object));
  space = GST_HERMES_COLORSPACE (object);
625 626 627 628 629 630 631 632

  switch (prop_id) {
    default:
      break;
  }
}

static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
633 634
gst_hermes_colorspace_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec)
635 636 637 638
{
  GstHermesColorspace *space;

  /* it's not null if we got it, but it might not be ours */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
639 640
  g_return_if_fail (GST_IS_COLORSPACE (object));
  space = GST_HERMES_COLORSPACE (object);
641 642 643 644 645 646 647 648 649

  switch (prop_id) {
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static gboolean
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
650
plugin_init (GstPlugin * plugin)
651 652 653
{
  gint hermes_res;

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
654
  hermes_res = Hermes_Init ();
655 656
  g_return_val_if_fail (hermes_res != 0, FALSE);

657
  if (!gst_element_register (plugin, "hermescolorspace", GST_RANK_PRIMARY - 2,
658
          GST_TYPE_COLORSPACE))
659 660 661 662 663
    return FALSE;

  return TRUE;
}

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
664 665 666 667 668
GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
    GST_VERSION_MINOR,
    "hermescolorspace",
    "Hermes colorspace converter",
    plugin_init, VERSION, "LGPL", GST_PACKAGE, GST_ORIGIN)