gstcolorspace.c 14.8 KB
Newer Older
1 2
/* GStreamer
 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 4 5
 * This file:
 * Copyright (C) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
 * Copyright (C) 2010 David Schleef <ds@schleef.org>
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * 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.
 */

23 24 25 26 27 28 29 30 31 32 33 34 35
/**
 * SECTION:element-colorspace
 *
 * Convert video frames between a great variety of colorspace formats.
 *
 * <refsect2>
 * <title>Example launch line</title>
 * |[
 * gst-launch -v videotestsrc ! video/x-raw-yuv,format=\(fourcc\)YUY2 ! colorspace ! ximagesink
 * ]|
 * </refsect2>
 */

36
#ifdef HAVE_CONFIG_H
37
#  include "config.h"
38
#endif
39

40
#include "gstcolorspace.h"
41 42
#include <gst/video/video.h>

43 44 45
/* For GST_CHECK_PLUGINS_BASE_VERSION() */
#include <gst/pbutils/pbutils.h>

46 47
#include <string.h>

48 49 50 51
GST_DEBUG_CATEGORY (colorspace_debug);
#define GST_CAT_DEFAULT colorspace_debug
GST_DEBUG_CATEGORY (colorspace_performance);

52
#if GST_CHECK_PLUGINS_BASE_VERSION(0, 10, 32)
53
#define VIDEO_CAPS_RGB8_PALETTED \
54
  GST_VIDEO_CAPS_RGB8_PALETTED "; "
55 56
#else
#define VIDEO_CAPS_RGB8_PALETTED        /* no-op */
57 58
#endif

59 60 61
#define CSP_VIDEO_CAPS						\
  "video/x-raw-yuv, width = "GST_VIDEO_SIZE_RANGE" , "			\
  "height="GST_VIDEO_SIZE_RANGE",framerate="GST_VIDEO_FPS_RANGE","	\
62
  "format= (fourcc) { I420 , NV12 , NV21 , YV12 , YUY2 , Y42B , Y444 , YUV9 , YVU9 , Y41B , Y800 , Y8 , GREY , Y16 , UYVY , YVYU , IYU1 , v308 , AYUV, v210, A420 } ;" \
63 64 65 66 67 68 69 70 71 72 73
  GST_VIDEO_CAPS_RGB";"							\
  GST_VIDEO_CAPS_BGR";"							\
  GST_VIDEO_CAPS_RGBx";"						\
  GST_VIDEO_CAPS_xRGB";"						\
  GST_VIDEO_CAPS_BGRx";"						\
  GST_VIDEO_CAPS_xBGR";"						\
  GST_VIDEO_CAPS_RGBA";"						\
  GST_VIDEO_CAPS_ARGB";"						\
  GST_VIDEO_CAPS_BGRA";"						\
  GST_VIDEO_CAPS_ABGR";"						\
  GST_VIDEO_CAPS_RGB_16";"						\
74
  GST_VIDEO_CAPS_BGR_16";"						\
75
  GST_VIDEO_CAPS_RGB_15";"						\
76
  GST_VIDEO_CAPS_BGR_15";"						\
77
  VIDEO_CAPS_RGB8_PALETTED                                              \
78 79 80 81 82 83 84 85 86 87
  GST_VIDEO_CAPS_GRAY8";"						\
  GST_VIDEO_CAPS_GRAY16("BIG_ENDIAN")";"				\
  GST_VIDEO_CAPS_GRAY16("LITTLE_ENDIAN")";"

static GstStaticPadTemplate gst_csp_src_template =
GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS (CSP_VIDEO_CAPS)
    );
88

89
static GstStaticPadTemplate gst_csp_sink_template =
90
GST_STATIC_PAD_TEMPLATE ("sink",
91 92
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
93
    GST_STATIC_CAPS (CSP_VIDEO_CAPS)
94
    );
95

96
GType gst_csp_get_type (void);
97

98 99 100 101 102 103
static gboolean gst_csp_set_caps (GstBaseTransform * btrans,
    GstCaps * incaps, GstCaps * outcaps);
static gboolean gst_csp_get_unit_size (GstBaseTransform * btrans,
    GstCaps * caps, guint * size);
static GstFlowReturn gst_csp_transform (GstBaseTransform * btrans,
    GstBuffer * inbuf, GstBuffer * outbuf);
104

105 106 107 108 109 110 111
static GQuark _QRAWRGB;         /* "video/x-raw-rgb" */
static GQuark _QRAWYUV;         /* "video/x-raw-yuv" */
static GQuark _QALPHAMASK;      /* "alpha_mask" */

/* copies the given caps */
static GstCaps *
gst_csp_caps_remove_format_info (GstCaps * caps)
112
{
113
  GstStructure *yuvst, *rgbst, *grayst;
114

115 116
  /* We know there's only one structure since we're given simple caps */
  caps = gst_caps_copy (caps);
117

118
  yuvst = gst_caps_get_structure (caps, 0);
119

120 121 122
  gst_structure_set_name (yuvst, "video/x-raw-yuv");
  gst_structure_remove_fields (yuvst, "format", "endianness", "depth",
      "bpp", "red_mask", "green_mask", "blue_mask", "alpha_mask",
123
      "palette_data", "color-matrix", NULL);
124

125 126 127
  rgbst = gst_structure_copy (yuvst);
  gst_structure_set_name (rgbst, "video/x-raw-rgb");
  gst_structure_remove_fields (rgbst, "color-matrix", "chroma-site", NULL);
128

129 130
  grayst = gst_structure_copy (rgbst);
  gst_structure_set_name (grayst, "video/x-raw-gray");
131

132 133
  gst_caps_append_structure (caps, rgbst);
  gst_caps_append_structure (caps, grayst);
134

135
  return caps;
136 137
}

138 139 140

static gboolean
gst_csp_structure_is_alpha (GstStructure * s)
141
{
142 143 144 145 146 147 148 149 150 151 152 153 154
  GQuark name;

  name = gst_structure_get_name_id (s);

  if (name == _QRAWRGB) {
    return gst_structure_id_has_field (s, _QALPHAMASK);
  } else if (name == _QRAWYUV) {
    guint32 fourcc;

    if (!gst_structure_get_fourcc (s, "format", &fourcc))
      return FALSE;

    return (fourcc == GST_MAKE_FOURCC ('A', 'Y', 'U', 'V'));
155 156
  }

157
  return FALSE;
158 159
}

160 161 162
/* The caps can be transformed into any other caps with format info removed.
 * However, we should prefer passthrough, so if passthrough is possible,
 * put it first in the list. */
163
static GstCaps *
164 165
gst_csp_transform_caps (GstBaseTransform * btrans,
    GstPadDirection direction, GstCaps * caps)
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
  GstCaps *template;
  GstCaps *tmp, *tmp2;
  GstCaps *result;
  GstStructure *s;
  GstCaps *alpha, *non_alpha;

  template = gst_static_pad_template_get_caps (&gst_csp_src_template);
  result = gst_caps_copy (caps);

  /* Get all possible caps that we can transform to */
  tmp = gst_csp_caps_remove_format_info (caps);
  tmp2 = gst_caps_intersect (tmp, template);
  gst_caps_unref (tmp);
  tmp = tmp2;

  /* Now move alpha formats to the beginning if caps is an alpha format
   * or at the end if caps is no alpha format */
  alpha = gst_caps_new_empty ();
  non_alpha = gst_caps_new_empty ();

  while ((s = gst_caps_steal_structure (tmp, 0))) {
    if (gst_csp_structure_is_alpha (s))
      gst_caps_append_structure (alpha, s);
    else
      gst_caps_append_structure (non_alpha, s);
  }
193

194 195
  s = gst_caps_get_structure (caps, 0);
  gst_caps_unref (tmp);
196

197 198 199 200 201 202 203
  if (gst_csp_structure_is_alpha (s)) {
    gst_caps_append (alpha, non_alpha);
    tmp = alpha;
  } else {
    gst_caps_append (non_alpha, alpha);
    tmp = non_alpha;
  }
204

205
  gst_caps_append (result, tmp);
206

207 208
  GST_DEBUG_OBJECT (btrans, "transformed %" GST_PTR_FORMAT " into %"
      GST_PTR_FORMAT, caps, result);
209

210
  return result;
211 212
}

213 214 215
static gboolean
gst_csp_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
    GstCaps * outcaps)
216
{
217 218 219 220 221 222 223 224 225 226 227
  GstCsp *space;
  GstVideoFormat in_format;
  GstVideoFormat out_format;
  gint in_height, in_width;
  gint out_height, out_width;
  gint in_fps_n, in_fps_d, in_par_n, in_par_d;
  gint out_fps_n, out_fps_d, out_par_n, out_par_d;
  gboolean have_in_par, have_out_par;
  gboolean have_in_interlaced, have_out_interlaced;
  gboolean in_interlaced, out_interlaced;
  gboolean ret;
228
  ColorSpaceColorSpec in_spec, out_spec;
229

230
  space = GST_CSP (btrans);
231

232
  /* input caps */
233

234 235 236
  ret = gst_video_format_parse_caps (incaps, &in_format, &in_width, &in_height);
  if (!ret)
    goto no_width_height;
237

238 239 240
  ret = gst_video_parse_caps_framerate (incaps, &in_fps_n, &in_fps_d);
  if (!ret)
    goto no_framerate;
241

242 243 244 245
  have_in_par = gst_video_parse_caps_pixel_aspect_ratio (incaps,
      &in_par_n, &in_par_d);
  have_in_interlaced = gst_video_format_parse_caps_interlaced (incaps,
      &in_interlaced);
246

247 248 249 250 251 252 253 254 255 256 257 258
  if (gst_video_format_is_rgb (in_format)) {
    in_spec = COLOR_SPEC_RGB;
  } else if (gst_video_format_is_yuv (in_format)) {
    const gchar *matrix = gst_video_parse_caps_color_matrix (incaps);

    if (matrix && g_str_equal (matrix, "hdtv"))
      in_spec = COLOR_SPEC_YUV_BT709;
    else
      in_spec = COLOR_SPEC_YUV_BT470_6;
  } else {
    in_spec = COLOR_SPEC_GRAY;
  }
259

260
  /* output caps */
261

262 263 264 265 266
  ret =
      gst_video_format_parse_caps (outcaps, &out_format, &out_width,
      &out_height);
  if (!ret)
    goto no_width_height;
267

268 269 270
  ret = gst_video_parse_caps_framerate (outcaps, &out_fps_n, &out_fps_d);
  if (!ret)
    goto no_framerate;
271

272 273 274 275
  have_out_par = gst_video_parse_caps_pixel_aspect_ratio (outcaps,
      &out_par_n, &out_par_d);
  have_out_interlaced = gst_video_format_parse_caps_interlaced (incaps,
      &out_interlaced);
276

277 278 279 280 281 282 283 284 285 286 287 288
  if (gst_video_format_is_rgb (out_format)) {
    out_spec = COLOR_SPEC_RGB;
  } else if (gst_video_format_is_yuv (out_format)) {
    const gchar *matrix = gst_video_parse_caps_color_matrix (outcaps);

    if (matrix && g_str_equal (matrix, "hdtv"))
      out_spec = COLOR_SPEC_YUV_BT709;
    else
      out_spec = COLOR_SPEC_YUV_BT470_6;
  } else {
    out_spec = COLOR_SPEC_GRAY;
  }
289

290 291 292 293
  /* these must match */
  if (in_width != out_width || in_height != out_height ||
      in_fps_n != out_fps_n || in_fps_d != out_fps_d)
    goto format_mismatch;
294

295 296 297 298
  /* if present, these must match too */
  if (have_in_par && have_out_par &&
      (in_par_n != out_par_n || in_par_d != out_par_d))
    goto format_mismatch;
299

300 301 302 303
  /* if present, these must match too */
  if (have_in_interlaced && have_out_interlaced &&
      in_interlaced != out_interlaced)
    goto format_mismatch;
304

305
  space->from_format = in_format;
306
  space->from_spec = in_spec;
307
  space->to_format = out_format;
308
  space->to_spec = out_spec;
309 310 311
  space->width = in_width;
  space->height = in_height;
  space->interlaced = in_interlaced;
312

313 314
  space->convert = colorspace_convert_new (out_format, out_spec, in_format,
      in_spec, in_width, in_height);
315 316 317
  if (space->convert) {
    colorspace_convert_set_interlaced (space->convert, in_interlaced);
  }
318
#if GST_CHECK_PLUGINS_BASE_VERSION(0, 10, 32)
319
  /* palette, only for from data */
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
  if (space->from_format == GST_VIDEO_FORMAT_RGB8_PALETTED &&
      space->to_format == GST_VIDEO_FORMAT_RGB8_PALETTED) {
    goto format_mismatch;
  } else if (space->from_format == GST_VIDEO_FORMAT_RGB8_PALETTED) {
    GstBuffer *palette;

    palette = gst_video_parse_caps_palette (incaps);

    if (!palette || GST_BUFFER_SIZE (palette) < 256 * 4) {
      if (palette)
        gst_buffer_unref (palette);
      goto invalid_palette;
    }
    colorspace_convert_set_palette (space->convert,
        (const guint32 *) GST_BUFFER_DATA (palette));
    gst_buffer_unref (palette);
  } else if (space->to_format == GST_VIDEO_FORMAT_RGB8_PALETTED) {
    const guint32 *palette;
    GstBuffer *p_buf;

    palette = colorspace_convert_get_palette (space->convert);
    p_buf = gst_buffer_new_and_alloc (256 * 4);
    memcpy (GST_BUFFER_DATA (p_buf), palette, 256 * 4);
    gst_caps_set_simple (outcaps, "palette_data", GST_TYPE_BUFFER, p_buf, NULL);
    gst_buffer_unref (p_buf);
  }
346
#endif
347

348
  GST_DEBUG ("reconfigured %d %d", space->from_format, space->to_format);
349

350
  return TRUE;
351

352 353 354
  /* ERRORS */
no_width_height:
  {
355
    GST_ERROR_OBJECT (space, "did not specify width or height");
356 357 358
    space->from_format = GST_VIDEO_FORMAT_UNKNOWN;
    space->to_format = GST_VIDEO_FORMAT_UNKNOWN;
    return FALSE;
359
  }
360 361
no_framerate:
  {
362
    GST_ERROR_OBJECT (space, "did not specify framerate");
363 364 365
    space->from_format = GST_VIDEO_FORMAT_UNKNOWN;
    space->to_format = GST_VIDEO_FORMAT_UNKNOWN;
    return FALSE;
366
  }
367 368
format_mismatch:
  {
369
    GST_ERROR_OBJECT (space, "input and output formats do not match");
370 371 372
    space->from_format = GST_VIDEO_FORMAT_UNKNOWN;
    space->to_format = GST_VIDEO_FORMAT_UNKNOWN;
    return FALSE;
373
  }
374
#if GST_CHECK_PLUGINS_BASE_VERSION(0, 10, 32)
375 376 377 378 379 380 381 382
invalid_palette:
  {
    GST_ERROR_OBJECT (space, "invalid palette");
    space->from_format = GST_VIDEO_FORMAT_UNKNOWN;
    space->to_format = GST_VIDEO_FORMAT_UNKNOWN;
    return FALSE;
  }
#endif
383 384
}

385
GST_BOILERPLATE (GstCsp, gst_csp, GstVideoFilter, GST_TYPE_VIDEO_FILTER);
386 387

static void
388
gst_csp_base_init (gpointer klass)
389
{
390
  GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
391

392
  gst_element_class_add_pad_template (element_class,
393
      gst_static_pad_template_get (&gst_csp_src_template));
394
  gst_element_class_add_pad_template (element_class,
395 396 397 398 399 400
      gst_static_pad_template_get (&gst_csp_sink_template));

  gst_element_class_set_details_simple (element_class,
      " Colorspace converter", "Filter/Converter/Video",
      "Converts video from one colorspace to another",
      "GStreamer maintainers <gstreamer-devel@lists.sourceforge.net>");
401

402 403 404
  _QRAWRGB = g_quark_from_string ("video/x-raw-rgb");
  _QRAWYUV = g_quark_from_string ("video/x-raw-yuv");
  _QALPHAMASK = g_quark_from_string ("alpha_mask");
405
}
406

407
static void
408
gst_csp_finalize (GObject * obj)
409
{
410 411 412 413 414 415 416 417 418
  G_OBJECT_CLASS (parent_class)->finalize (obj);
}

static void
gst_csp_class_init (GstCspClass * klass)
{
  GObjectClass *gobject_class = (GObjectClass *) klass;
  GstBaseTransformClass *gstbasetransform_class =
      (GstBaseTransformClass *) klass;
419

420
  gobject_class->finalize = gst_csp_finalize;
421

422 423 424 425 426 427
  gstbasetransform_class->transform_caps =
      GST_DEBUG_FUNCPTR (gst_csp_transform_caps);
  gstbasetransform_class->set_caps = GST_DEBUG_FUNCPTR (gst_csp_set_caps);
  gstbasetransform_class->get_unit_size =
      GST_DEBUG_FUNCPTR (gst_csp_get_unit_size);
  gstbasetransform_class->transform = GST_DEBUG_FUNCPTR (gst_csp_transform);
428

429
  gstbasetransform_class->passthrough_on_same_caps = TRUE;
430 431 432
}

static void
433
gst_csp_init (GstCsp * space, GstCspClass * klass)
434
{
435 436
  space->from_format = GST_VIDEO_FORMAT_UNKNOWN;
  space->to_format = GST_VIDEO_FORMAT_UNKNOWN;
437 438
}

439 440
static gboolean
gst_csp_get_unit_size (GstBaseTransform * btrans, GstCaps * caps, guint * size)
441
{
442 443 444
  gboolean ret = TRUE;
  GstVideoFormat format;
  gint width, height;
445

446
  g_assert (size);
447

448 449 450
  ret = gst_video_format_parse_caps (caps, &format, &width, &height);
  if (ret) {
    *size = gst_video_format_get_size (format, width, height);
451 452
  }

453
  return ret;
454 455
}

456 457 458
static GstFlowReturn
gst_csp_transform (GstBaseTransform * btrans, GstBuffer * inbuf,
    GstBuffer * outbuf)
459
{
460
  GstCsp *space;
461

462
  space = GST_CSP (btrans);
463

464
  GST_DEBUG ("from %d -> to %d", space->from_format, space->to_format);
465

466 467 468
  if (G_UNLIKELY (space->from_format == GST_VIDEO_FORMAT_UNKNOWN ||
          space->to_format == GST_VIDEO_FORMAT_UNKNOWN))
    goto unknown_format;
469

470 471
  colorspace_convert_convert (space->convert, GST_BUFFER_DATA (outbuf),
      GST_BUFFER_DATA (inbuf));
472

473 474
  /* baseclass copies timestamps */
  GST_DEBUG ("from %d -> to %d done", space->from_format, space->to_format);
475

476
  return GST_FLOW_OK;
477

478 479 480 481 482 483
  /* ERRORS */
unknown_format:
  {
    GST_ELEMENT_ERROR (space, CORE, NOT_IMPLEMENTED, (NULL),
        ("attempting to convert colorspaces between unknown formats"));
    return GST_FLOW_NOT_NEGOTIATED;
484
  }
485 486 487 488 489 490 491 492
#if 0
not_supported:
  {
    GST_ELEMENT_ERROR (space, CORE, NOT_IMPLEMENTED, (NULL),
        ("cannot convert between formats"));
    return GST_FLOW_NOT_SUPPORTED;
  }
#endif
493 494 495
}

static gboolean
496
plugin_init (GstPlugin * plugin)
497
{
498 499 500
  GST_DEBUG_CATEGORY_INIT (colorspace_debug, "colorspace", 0,
      "Colorspace Converter");
  GST_DEBUG_CATEGORY_GET (colorspace_performance, "GST_PERFORMANCE");
501

502 503
  return gst_element_register (plugin, "colorspace",
      GST_RANK_NONE, GST_TYPE_CSP);
504 505
}

506 507
GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
    GST_VERSION_MINOR,
508
    "colorspace", "Colorspace conversion", plugin_init, VERSION, "LGPL", "", "")