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

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

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

43 44
#include <string.h>

45 46 47
GST_DEBUG_CATEGORY (videoconvert_debug);
#define GST_CAT_DEFAULT videoconvert_debug
GST_DEBUG_CATEGORY (videoconvert_performance);
48

49 50 51 52 53 54
enum
{
  PROP_0,
  PROP_DITHER
};

55 56 57
#define CSP_VIDEO_CAPS						\
  "video/x-raw-yuv, width = "GST_VIDEO_SIZE_RANGE" , "			\
  "height="GST_VIDEO_SIZE_RANGE",framerate="GST_VIDEO_FPS_RANGE","	\
58
  "format= (fourcc) { I420 , NV12 , NV21 , YV12 , YUY2 , Y42B , Y444 , YUV9 , YVU9 , Y41B , Y800 , Y8 , GREY , Y16 , UYVY , YVYU , IYU1 , v308 , AYUV, v210, v216, A420, AY64 } ;" \
59 60 61 62 63 64 65 66 67 68 69
  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";"						\
70
  GST_VIDEO_CAPS_BGR_16";"						\
71
  GST_VIDEO_CAPS_RGB_15";"						\
72
  GST_VIDEO_CAPS_BGR_15";"						\
73
  GST_VIDEO_CAPS_RGB8_PALETTED "; "                                     \
74 75
  GST_VIDEO_CAPS_GRAY8";"						\
  GST_VIDEO_CAPS_GRAY16("BIG_ENDIAN")";"				\
76
  GST_VIDEO_CAPS_GRAY16("LITTLE_ENDIAN")";"                             \
77
  GST_VIDEO_CAPS_r210";"                                                \
78
  GST_VIDEO_CAPS_ARGB_64
79

80
static GstStaticPadTemplate gst_video_convert_src_template =
81 82 83 84 85
GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS (CSP_VIDEO_CAPS)
    );
86

87
static GstStaticPadTemplate gst_video_convert_sink_template =
88
GST_STATIC_PAD_TEMPLATE ("sink",
89 90
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
91
    GST_STATIC_CAPS (CSP_VIDEO_CAPS)
92
    );
93

94
GType gst_video_convert_get_type (void);
95

96
static void gst_video_convert_set_property (GObject * object,
97
    guint property_id, const GValue * value, GParamSpec * pspec);
98
static void gst_video_convert_get_property (GObject * object,
99 100
    guint property_id, GValue * value, GParamSpec * pspec);

101
static gboolean gst_video_convert_set_caps (GstBaseTransform * btrans,
102
    GstCaps * incaps, GstCaps * outcaps);
103
static gboolean gst_video_convert_get_unit_size (GstBaseTransform * btrans,
Wim Taymans's avatar
Wim Taymans committed
104
    GstCaps * caps, gsize * size);
105
static GstFlowReturn gst_video_convert_transform (GstBaseTransform * btrans,
106
    GstBuffer * inbuf, GstBuffer * outbuf);
107

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

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130

static GType
dither_method_get_type (void)
{
  static GType gtype = 0;

  if (gtype == 0) {
    static const GEnumValue values[] = {
      {DITHER_NONE, "No dithering (default)", "none"},
      {DITHER_VERTERR, "Vertical error propogation", "verterr"},
      {DITHER_HALFTONE, "Half-tone", "halftone"},
      {0, NULL, NULL}
    };

    gtype = g_enum_register_static ("GstColorspaceDitherMethod", values);
  }
  return gtype;
}

131 132
/* copies the given caps */
static GstCaps *
133
gst_video_convert_caps_remove_format_info (GstCaps * caps)
134
{
135
  GstStructure *yuvst, *rgbst, *grayst;
Wim Taymans's avatar
Wim Taymans committed
136 137
  gint i, n;
  GstCaps *res;
138

Wim Taymans's avatar
Wim Taymans committed
139
  res = gst_caps_new_empty ();
140

Wim Taymans's avatar
Wim Taymans committed
141 142 143
  n = gst_caps_get_size (caps);
  for (i = 0; i < n; i++) {
    yuvst = gst_caps_get_structure (caps, i);
144

Wim Taymans's avatar
Wim Taymans committed
145 146 147 148
    /* If this is already expressed by the existing caps
     * skip this structure */
    if (i > 0 && gst_caps_is_subset_structure (res, yuvst))
      continue;
149

Wim Taymans's avatar
Wim Taymans committed
150 151 152 153 154
    yuvst = gst_structure_copy (yuvst);
    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",
        "palette_data", NULL);
155

Wim Taymans's avatar
Wim Taymans committed
156 157 158
    rgbst = gst_structure_copy (yuvst);
    gst_structure_set_name (rgbst, "video/x-raw-rgb");
    gst_structure_remove_fields (rgbst, "color-matrix", "chroma-site", NULL);
159

Wim Taymans's avatar
Wim Taymans committed
160 161
    grayst = gst_structure_copy (rgbst);
    gst_structure_set_name (grayst, "video/x-raw-gray");
162

Wim Taymans's avatar
Wim Taymans committed
163 164 165
    gst_caps_append_structure (res, yuvst);
    gst_caps_append_structure (res, rgbst);
    gst_caps_append_structure (res, grayst);
166 167
  }

Wim Taymans's avatar
Wim Taymans committed
168
  return res;
169 170
}

171 172 173
/* 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. */
174
static GstCaps *
175
gst_video_convert_transform_caps (GstBaseTransform * btrans,
Wim Taymans's avatar
Wim Taymans committed
176
    GstPadDirection direction, GstCaps * caps, GstCaps * filter)
177
{
178 179 180 181
  GstCaps *template;
  GstCaps *tmp, *tmp2;
  GstCaps *result;

182
  template = gst_static_pad_template_get_caps (&gst_video_convert_src_template);
183 184 185
  result = gst_caps_copy (caps);

  /* Get all possible caps that we can transform to */
186
  tmp = gst_video_convert_caps_remove_format_info (caps);
187

Wim Taymans's avatar
Wim Taymans committed
188 189 190 191
  if (filter) {
    tmp2 = gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
    gst_caps_unref (tmp);
    tmp = tmp2;
192
  }
193

Wim Taymans's avatar
Wim Taymans committed
194
  result = tmp;
195

196 197
  GST_DEBUG_OBJECT (btrans, "transformed %" GST_PTR_FORMAT " into %"
      GST_PTR_FORMAT, caps, result);
198

199
  return result;
200 201
}

202
static gboolean
203
gst_video_convert_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
204
    GstCaps * outcaps)
205
{
206
  GstVideoConvert *space;
207 208 209 210 211 212 213 214 215 216
  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;
217
  ColorSpaceColorSpec in_spec, out_spec;
218

219
  space = GST_VIDEO_CONVERT_CAST (btrans);
220

221
  if (space->convert) {
222
    videoconvert_convert_free (space->convert);
223 224
  }

225
  /* input caps */
226

227 228 229
  ret = gst_video_format_parse_caps (incaps, &in_format, &in_width, &in_height);
  if (!ret)
    goto no_width_height;
230

231 232 233
  ret = gst_video_parse_caps_framerate (incaps, &in_fps_n, &in_fps_d);
  if (!ret)
    goto no_framerate;
234

235 236 237 238
  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);
239

240 241 242 243 244 245 246 247 248 249 250 251
  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;
  }
252

253
  /* output caps */
254

255 256 257 258 259
  ret =
      gst_video_format_parse_caps (outcaps, &out_format, &out_width,
      &out_height);
  if (!ret)
    goto no_width_height;
260

261 262 263
  ret = gst_video_parse_caps_framerate (outcaps, &out_fps_n, &out_fps_d);
  if (!ret)
    goto no_framerate;
264

265 266 267 268
  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);
269

270 271 272 273 274 275 276 277 278 279 280 281
  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;
  }
282

283 284 285 286
  /* 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;
287

288 289 290 291
  /* 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;
292

293 294 295 296
  /* if present, these must match too */
  if (have_in_interlaced && have_out_interlaced &&
      in_interlaced != out_interlaced)
    goto format_mismatch;
297

298
  space->from_format = in_format;
299
  space->from_spec = in_spec;
300
  space->to_format = out_format;
301
  space->to_spec = out_spec;
302 303 304
  space->width = in_width;
  space->height = in_height;
  space->interlaced = in_interlaced;
305

306
  space->convert = videoconvert_convert_new (out_format, out_spec, in_format,
307
      in_spec, in_width, in_height);
308
  if (space->convert) {
309
    videoconvert_convert_set_interlaced (space->convert, in_interlaced);
310
  }
311
  /* palette, only for from data */
312 313 314 315 316
  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;
Wim Taymans's avatar
Wim Taymans committed
317
    guint32 *data;
318 319 320

    palette = gst_video_parse_caps_palette (incaps);

Wim Taymans's avatar
Wim Taymans committed
321
    if (!palette || gst_buffer_get_size (palette) < 256 * 4) {
322 323 324 325
      if (palette)
        gst_buffer_unref (palette);
      goto invalid_palette;
    }
Wim Taymans's avatar
Wim Taymans committed
326 327

    data = gst_buffer_map (palette, NULL, NULL, GST_MAP_READ);
328
    videoconvert_convert_set_palette (space->convert, data);
Wim Taymans's avatar
Wim Taymans committed
329 330
    gst_buffer_unmap (palette, data, -1);

331 332 333 334 335
    gst_buffer_unref (palette);
  } else if (space->to_format == GST_VIDEO_FORMAT_RGB8_PALETTED) {
    const guint32 *palette;
    GstBuffer *p_buf;

336
    palette = videoconvert_convert_get_palette (space->convert);
Wim Taymans's avatar
Wim Taymans committed
337

338
    p_buf = gst_buffer_new_and_alloc (256 * 4);
Wim Taymans's avatar
Wim Taymans committed
339
    gst_buffer_fill (p_buf, 0, palette, 256 * 4);
340 341 342
    gst_caps_set_simple (outcaps, "palette_data", GST_TYPE_BUFFER, p_buf, NULL);
    gst_buffer_unref (p_buf);
  }
343

344
  GST_DEBUG ("reconfigured %d %d", space->from_format, space->to_format);
345

346
  return TRUE;
347

348 349 350
  /* ERRORS */
no_width_height:
  {
351
    GST_ERROR_OBJECT (space, "did not specify width or height");
352 353 354
    space->from_format = GST_VIDEO_FORMAT_UNKNOWN;
    space->to_format = GST_VIDEO_FORMAT_UNKNOWN;
    return FALSE;
355
  }
356 357
no_framerate:
  {
358
    GST_ERROR_OBJECT (space, "did not specify framerate");
359 360 361
    space->from_format = GST_VIDEO_FORMAT_UNKNOWN;
    space->to_format = GST_VIDEO_FORMAT_UNKNOWN;
    return FALSE;
362
  }
363 364
format_mismatch:
  {
365
    GST_ERROR_OBJECT (space, "input and output formats do not match");
366 367 368
    space->from_format = GST_VIDEO_FORMAT_UNKNOWN;
    space->to_format = GST_VIDEO_FORMAT_UNKNOWN;
    return FALSE;
369
  }
370 371 372 373 374 375 376
invalid_palette:
  {
    GST_ERROR_OBJECT (space, "invalid palette");
    space->from_format = GST_VIDEO_FORMAT_UNKNOWN;
    space->to_format = GST_VIDEO_FORMAT_UNKNOWN;
    return FALSE;
  }
377 378
}

379 380
#define gst_video_convert_parent_class parent_class
G_DEFINE_TYPE (GstVideoConvert, gst_video_convert, GST_TYPE_VIDEO_FILTER);
381

382
static void
383
gst_video_convert_finalize (GObject * obj)
384
{
385
  GstVideoConvert *space = GST_VIDEO_CONVERT (obj);
386 387

  if (space->convert) {
388
    videoconvert_convert_free (space->convert);
389 390
  }

391 392 393 394
  G_OBJECT_CLASS (parent_class)->finalize (obj);
}

static void
395
gst_video_convert_class_init (GstVideoConvertClass * klass)
396 397
{
  GObjectClass *gobject_class = (GObjectClass *) klass;
Wim Taymans's avatar
Wim Taymans committed
398
  GstElementClass *gstelement_class = (GstElementClass *) klass;
399 400
  GstBaseTransformClass *gstbasetransform_class =
      (GstBaseTransformClass *) klass;
401

402 403 404
  gobject_class->set_property = gst_video_convert_set_property;
  gobject_class->get_property = gst_video_convert_get_property;
  gobject_class->finalize = gst_video_convert_finalize;
405

Wim Taymans's avatar
Wim Taymans committed
406
  gst_element_class_add_pad_template (gstelement_class,
407
      gst_static_pad_template_get (&gst_video_convert_src_template));
Wim Taymans's avatar
Wim Taymans committed
408
  gst_element_class_add_pad_template (gstelement_class,
409
      gst_static_pad_template_get (&gst_video_convert_sink_template));
Wim Taymans's avatar
Wim Taymans committed
410 411 412 413 414 415 416 417 418 419

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

  _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");

420
  gstbasetransform_class->transform_caps =
421 422 423
      GST_DEBUG_FUNCPTR (gst_video_convert_transform_caps);
  gstbasetransform_class->set_caps =
      GST_DEBUG_FUNCPTR (gst_video_convert_set_caps);
424
  gstbasetransform_class->get_unit_size =
425 426 427
      GST_DEBUG_FUNCPTR (gst_video_convert_get_unit_size);
  gstbasetransform_class->transform =
      GST_DEBUG_FUNCPTR (gst_video_convert_transform);
428

429
  gstbasetransform_class->passthrough_on_same_caps = TRUE;
430 431 432 433 434

  g_object_class_install_property (gobject_class, PROP_DITHER,
      g_param_spec_enum ("dither", "Dither", "Apply dithering while converting",
          dither_method_get_type (), DITHER_NONE,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
435 436 437
}

static void
438
gst_video_convert_init (GstVideoConvert * space)
439
{
440 441
  space->from_format = GST_VIDEO_FORMAT_UNKNOWN;
  space->to_format = GST_VIDEO_FORMAT_UNKNOWN;
442 443
}

444
void
445
gst_video_convert_set_property (GObject * object, guint property_id,
446 447
    const GValue * value, GParamSpec * pspec)
{
448
  GstVideoConvert *csp;
449

450
  csp = GST_VIDEO_CONVERT (object);
451 452 453 454 455 456 457 458 459 460 461 462

  switch (property_id) {
    case PROP_DITHER:
      csp->dither = g_value_get_enum (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
  }
}

void
463
gst_video_convert_get_property (GObject * object, guint property_id,
464 465
    GValue * value, GParamSpec * pspec)
{
466
  GstVideoConvert *csp;
467

468
  csp = GST_VIDEO_CONVERT (object);
469 470 471 472 473 474 475 476 477 478 479

  switch (property_id) {
    case PROP_DITHER:
      g_value_set_enum (value, csp->dither);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
  }
}

480
static gboolean
481 482
gst_video_convert_get_unit_size (GstBaseTransform * btrans, GstCaps * caps,
    gsize * size)
483
{
484 485 486
  gboolean ret = TRUE;
  GstVideoFormat format;
  gint width, height;
487

488
  g_assert (size);
489

490 491 492
  ret = gst_video_format_parse_caps (caps, &format, &width, &height);
  if (ret) {
    *size = gst_video_format_get_size (format, width, height);
493 494
  }

495
  return ret;
496 497
}

498
static GstFlowReturn
499
gst_video_convert_transform (GstBaseTransform * btrans, GstBuffer * inbuf,
500
    GstBuffer * outbuf)
501
{
502
  GstVideoConvert *space;
Wim Taymans's avatar
Wim Taymans committed
503 504
  guint8 *indata, *outdata;
  gsize insize, outsize;
505

506
  space = GST_VIDEO_CONVERT_CAST (btrans);
507

508
  GST_DEBUG ("from %d -> to %d", space->from_format, space->to_format);
509

510 511 512
  if (G_UNLIKELY (space->from_format == GST_VIDEO_FORMAT_UNKNOWN ||
          space->to_format == GST_VIDEO_FORMAT_UNKNOWN))
    goto unknown_format;
513

514
  videoconvert_convert_set_dither (space->convert, space->dither);
515

Wim Taymans's avatar
Wim Taymans committed
516 517 518
  indata = gst_buffer_map (inbuf, &insize, NULL, GST_MAP_READ);
  outdata = gst_buffer_map (outbuf, &outsize, NULL, GST_MAP_WRITE);

519
  videoconvert_convert_convert (space->convert, outdata, indata);
Wim Taymans's avatar
Wim Taymans committed
520 521 522

  gst_buffer_unmap (outbuf, outdata, outsize);
  gst_buffer_unmap (inbuf, indata, insize);
523

524 525
  /* baseclass copies timestamps */
  GST_DEBUG ("from %d -> to %d done", space->from_format, space->to_format);
526

527
  return GST_FLOW_OK;
528

529 530 531 532 533 534
  /* ERRORS */
unknown_format:
  {
    GST_ELEMENT_ERROR (space, CORE, NOT_IMPLEMENTED, (NULL),
        ("attempting to convert colorspaces between unknown formats"));
    return GST_FLOW_NOT_NEGOTIATED;
535
  }
536 537 538 539 540 541 542 543
#if 0
not_supported:
  {
    GST_ELEMENT_ERROR (space, CORE, NOT_IMPLEMENTED, (NULL),
        ("cannot convert between formats"));
    return GST_FLOW_NOT_SUPPORTED;
  }
#endif
544 545 546
}

static gboolean
547
plugin_init (GstPlugin * plugin)
548
{
549
  GST_DEBUG_CATEGORY_INIT (videoconvert_debug, "videoconvert", 0,
550
      "Colorspace Converter");
551
  GST_DEBUG_CATEGORY_GET (videoconvert_performance, "GST_PERFORMANCE");
552

553 554
  return gst_element_register (plugin, "videoconvert",
      GST_RANK_NONE, GST_TYPE_VIDEO_CONVERT);
555 556
}

557 558
GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
    GST_VERSION_MINOR,
559 560
    "videoconvert", "Colorspace conversion", plugin_init, VERSION, "LGPL", "",
    "")