81a55617ffa7df35a063c2b83330f93b2a87cd9f
[platform/upstream/gstreamer.git] / gst / colorspace / gstcolorspace.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  * This file:
4  * Copyright (C) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
5  * Copyright (C) 2010 David Schleef <ds@schleef.org>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 /**
24  * SECTION:element-colorspace
25  *
26  * Convert video frames between a great variety of colorspace formats.
27  *
28  * <refsect2>
29  * <title>Example launch line</title>
30  * |[
31  * gst-launch -v videotestsrc ! video/x-raw-yuv,format=\(fourcc\)YUY2 ! colorspace ! ximagesink
32  * ]|
33  * </refsect2>
34  */
35
36 #ifdef HAVE_CONFIG_H
37 #  include "config.h"
38 #endif
39
40 #include "gstcolorspace.h"
41 #include <gst/video/video.h>
42
43 #include <string.h>
44
45 GST_DEBUG_CATEGORY (colorspace_debug);
46 #define GST_CAT_DEFAULT colorspace_debug
47 GST_DEBUG_CATEGORY (colorspace_performance);
48
49 enum
50 {
51   PROP_0,
52   PROP_DITHER
53 };
54
55 #define CSP_VIDEO_CAPS                                          \
56   "video/x-raw-yuv, width = "GST_VIDEO_SIZE_RANGE" , "                  \
57   "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   GST_VIDEO_CAPS_RGB";"                                                 \
60   GST_VIDEO_CAPS_BGR";"                                                 \
61   GST_VIDEO_CAPS_RGBx";"                                                \
62   GST_VIDEO_CAPS_xRGB";"                                                \
63   GST_VIDEO_CAPS_BGRx";"                                                \
64   GST_VIDEO_CAPS_xBGR";"                                                \
65   GST_VIDEO_CAPS_RGBA";"                                                \
66   GST_VIDEO_CAPS_ARGB";"                                                \
67   GST_VIDEO_CAPS_BGRA";"                                                \
68   GST_VIDEO_CAPS_ABGR";"                                                \
69   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   GST_VIDEO_CAPS_GRAY8";"                                               \
75   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_csp_src_template =
81 GST_STATIC_PAD_TEMPLATE ("src",
82     GST_PAD_SRC,
83     GST_PAD_ALWAYS,
84     GST_STATIC_CAPS (CSP_VIDEO_CAPS)
85     );
86
87 static GstStaticPadTemplate gst_csp_sink_template =
88 GST_STATIC_PAD_TEMPLATE ("sink",
89     GST_PAD_SINK,
90     GST_PAD_ALWAYS,
91     GST_STATIC_CAPS (CSP_VIDEO_CAPS)
92     );
93
94 GType gst_csp_get_type (void);
95
96 static void gst_csp_set_property (GObject * object,
97     guint property_id, const GValue * value, GParamSpec * pspec);
98 static void gst_csp_get_property (GObject * object,
99     guint property_id, GValue * value, GParamSpec * pspec);
100
101 static gboolean gst_csp_set_caps (GstBaseTransform * btrans,
102     GstCaps * incaps, GstCaps * outcaps);
103 static gboolean gst_csp_get_unit_size (GstBaseTransform * btrans,
104     GstCaps * caps, gsize * size);
105 static GstFlowReturn gst_csp_transform (GstBaseTransform * btrans,
106     GstBuffer * inbuf, GstBuffer * outbuf);
107
108 static GQuark _QRAWRGB;         /* "video/x-raw-rgb" */
109 static GQuark _QRAWYUV;         /* "video/x-raw-yuv" */
110 static GQuark _QALPHAMASK;      /* "alpha_mask" */
111
112
113 static GType
114 dither_method_get_type (void)
115 {
116   static GType gtype = 0;
117
118   if (gtype == 0) {
119     static const GEnumValue values[] = {
120       {DITHER_NONE, "No dithering (default)", "none"},
121       {DITHER_VERTERR, "Vertical error propogation", "verterr"},
122       {DITHER_HALFTONE, "Half-tone", "halftone"},
123       {0, NULL, NULL}
124     };
125
126     gtype = g_enum_register_static ("GstColorspaceDitherMethod", values);
127   }
128   return gtype;
129 }
130
131 /* copies the given caps */
132 static GstCaps *
133 gst_csp_caps_remove_format_info (GstCaps * caps)
134 {
135   GstStructure *yuvst, *rgbst, *grayst;
136
137   /* We know there's only one structure since we're given simple caps */
138   caps = gst_caps_copy (caps);
139
140   yuvst = gst_caps_get_structure (caps, 0);
141
142   gst_structure_set_name (yuvst, "video/x-raw-yuv");
143   gst_structure_remove_fields (yuvst, "format", "endianness", "depth",
144       "bpp", "red_mask", "green_mask", "blue_mask", "alpha_mask",
145       "palette_data", "color-matrix", NULL);
146
147   rgbst = gst_structure_copy (yuvst);
148   gst_structure_set_name (rgbst, "video/x-raw-rgb");
149   gst_structure_remove_fields (rgbst, "color-matrix", "chroma-site", NULL);
150
151   grayst = gst_structure_copy (rgbst);
152   gst_structure_set_name (grayst, "video/x-raw-gray");
153
154   gst_caps_append_structure (caps, rgbst);
155   gst_caps_append_structure (caps, grayst);
156
157   return caps;
158 }
159
160
161 static gboolean
162 gst_csp_structure_is_alpha (GstStructure * s)
163 {
164   GQuark name;
165
166   name = gst_structure_get_name_id (s);
167
168   if (name == _QRAWRGB) {
169     return gst_structure_id_has_field (s, _QALPHAMASK);
170   } else if (name == _QRAWYUV) {
171     guint32 fourcc;
172
173     if (!gst_structure_get_fourcc (s, "format", &fourcc))
174       return FALSE;
175
176     return (fourcc == GST_MAKE_FOURCC ('A', 'Y', 'U', 'V'));
177   }
178
179   return FALSE;
180 }
181
182 /* The caps can be transformed into any other caps with format info removed.
183  * However, we should prefer passthrough, so if passthrough is possible,
184  * put it first in the list. */
185 static GstCaps *
186 gst_csp_transform_caps (GstBaseTransform * btrans,
187     GstPadDirection direction, GstCaps * caps, GstCaps * filter)
188 {
189   GstCaps *template;
190   GstCaps *tmp, *tmp2;
191   GstCaps *result;
192   GstStructure *s;
193   GstCaps *alpha, *non_alpha;
194
195   template = gst_static_pad_template_get_caps (&gst_csp_src_template);
196   result = gst_caps_copy (caps);
197
198   /* Get all possible caps that we can transform to */
199   tmp = gst_csp_caps_remove_format_info (caps);
200   tmp2 = gst_caps_intersect (tmp, template);
201   gst_caps_unref (tmp);
202   tmp = tmp2;
203
204   /* Now move alpha formats to the beginning if caps is an alpha format
205    * or at the end if caps is no alpha format */
206   alpha = gst_caps_new_empty ();
207   non_alpha = gst_caps_new_empty ();
208
209   while ((s = gst_caps_steal_structure (tmp, 0))) {
210     if (gst_csp_structure_is_alpha (s))
211       gst_caps_append_structure (alpha, s);
212     else
213       gst_caps_append_structure (non_alpha, s);
214   }
215
216   s = gst_caps_get_structure (caps, 0);
217   gst_caps_unref (tmp);
218
219   if (gst_csp_structure_is_alpha (s)) {
220     gst_caps_append (alpha, non_alpha);
221     tmp = alpha;
222   } else {
223     gst_caps_append (non_alpha, alpha);
224     tmp = non_alpha;
225   }
226
227   gst_caps_append (result, tmp);
228
229   GST_DEBUG_OBJECT (btrans, "transformed %" GST_PTR_FORMAT " into %"
230       GST_PTR_FORMAT, caps, result);
231
232   return result;
233 }
234
235 static gboolean
236 gst_csp_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
237     GstCaps * outcaps)
238 {
239   GstCsp *space;
240   GstVideoFormat in_format;
241   GstVideoFormat out_format;
242   gint in_height, in_width;
243   gint out_height, out_width;
244   gint in_fps_n, in_fps_d, in_par_n, in_par_d;
245   gint out_fps_n, out_fps_d, out_par_n, out_par_d;
246   gboolean have_in_par, have_out_par;
247   gboolean have_in_interlaced, have_out_interlaced;
248   gboolean in_interlaced, out_interlaced;
249   gboolean ret;
250   ColorSpaceColorSpec in_spec, out_spec;
251
252   space = GST_CSP (btrans);
253
254   if (space->convert) {
255     colorspace_convert_free (space->convert);
256   }
257
258   /* input caps */
259
260   ret = gst_video_format_parse_caps (incaps, &in_format, &in_width, &in_height);
261   if (!ret)
262     goto no_width_height;
263
264   ret = gst_video_parse_caps_framerate (incaps, &in_fps_n, &in_fps_d);
265   if (!ret)
266     goto no_framerate;
267
268   have_in_par = gst_video_parse_caps_pixel_aspect_ratio (incaps,
269       &in_par_n, &in_par_d);
270   have_in_interlaced = gst_video_format_parse_caps_interlaced (incaps,
271       &in_interlaced);
272
273   if (gst_video_format_is_rgb (in_format)) {
274     in_spec = COLOR_SPEC_RGB;
275   } else if (gst_video_format_is_yuv (in_format)) {
276     const gchar *matrix = gst_video_parse_caps_color_matrix (incaps);
277
278     if (matrix && g_str_equal (matrix, "hdtv"))
279       in_spec = COLOR_SPEC_YUV_BT709;
280     else
281       in_spec = COLOR_SPEC_YUV_BT470_6;
282   } else {
283     in_spec = COLOR_SPEC_GRAY;
284   }
285
286   /* output caps */
287
288   ret =
289       gst_video_format_parse_caps (outcaps, &out_format, &out_width,
290       &out_height);
291   if (!ret)
292     goto no_width_height;
293
294   ret = gst_video_parse_caps_framerate (outcaps, &out_fps_n, &out_fps_d);
295   if (!ret)
296     goto no_framerate;
297
298   have_out_par = gst_video_parse_caps_pixel_aspect_ratio (outcaps,
299       &out_par_n, &out_par_d);
300   have_out_interlaced = gst_video_format_parse_caps_interlaced (incaps,
301       &out_interlaced);
302
303   if (gst_video_format_is_rgb (out_format)) {
304     out_spec = COLOR_SPEC_RGB;
305   } else if (gst_video_format_is_yuv (out_format)) {
306     const gchar *matrix = gst_video_parse_caps_color_matrix (outcaps);
307
308     if (matrix && g_str_equal (matrix, "hdtv"))
309       out_spec = COLOR_SPEC_YUV_BT709;
310     else
311       out_spec = COLOR_SPEC_YUV_BT470_6;
312   } else {
313     out_spec = COLOR_SPEC_GRAY;
314   }
315
316   /* these must match */
317   if (in_width != out_width || in_height != out_height ||
318       in_fps_n != out_fps_n || in_fps_d != out_fps_d)
319     goto format_mismatch;
320
321   /* if present, these must match too */
322   if (have_in_par && have_out_par &&
323       (in_par_n != out_par_n || in_par_d != out_par_d))
324     goto format_mismatch;
325
326   /* if present, these must match too */
327   if (have_in_interlaced && have_out_interlaced &&
328       in_interlaced != out_interlaced)
329     goto format_mismatch;
330
331   space->from_format = in_format;
332   space->from_spec = in_spec;
333   space->to_format = out_format;
334   space->to_spec = out_spec;
335   space->width = in_width;
336   space->height = in_height;
337   space->interlaced = in_interlaced;
338
339   space->convert = colorspace_convert_new (out_format, out_spec, in_format,
340       in_spec, in_width, in_height);
341   if (space->convert) {
342     colorspace_convert_set_interlaced (space->convert, in_interlaced);
343   }
344   /* palette, only for from data */
345   if (space->from_format == GST_VIDEO_FORMAT_RGB8_PALETTED &&
346       space->to_format == GST_VIDEO_FORMAT_RGB8_PALETTED) {
347     goto format_mismatch;
348   } else if (space->from_format == GST_VIDEO_FORMAT_RGB8_PALETTED) {
349     GstBuffer *palette;
350     guint32 *data;
351
352     palette = gst_video_parse_caps_palette (incaps);
353
354     if (!palette || gst_buffer_get_size (palette) < 256 * 4) {
355       if (palette)
356         gst_buffer_unref (palette);
357       goto invalid_palette;
358     }
359
360     data = gst_buffer_map (palette, NULL, NULL, GST_MAP_READ);
361     colorspace_convert_set_palette (space->convert, data);
362     gst_buffer_unmap (palette, data, -1);
363
364     gst_buffer_unref (palette);
365   } else if (space->to_format == GST_VIDEO_FORMAT_RGB8_PALETTED) {
366     const guint32 *palette;
367     GstBuffer *p_buf;
368
369     palette = colorspace_convert_get_palette (space->convert);
370
371     p_buf = gst_buffer_new_and_alloc (256 * 4);
372     gst_buffer_fill (p_buf, 0, palette, 256 * 4);
373     gst_caps_set_simple (outcaps, "palette_data", GST_TYPE_BUFFER, p_buf, NULL);
374     gst_buffer_unref (p_buf);
375   }
376
377   GST_DEBUG ("reconfigured %d %d", space->from_format, space->to_format);
378
379   return TRUE;
380
381   /* ERRORS */
382 no_width_height:
383   {
384     GST_ERROR_OBJECT (space, "did not specify width or height");
385     space->from_format = GST_VIDEO_FORMAT_UNKNOWN;
386     space->to_format = GST_VIDEO_FORMAT_UNKNOWN;
387     return FALSE;
388   }
389 no_framerate:
390   {
391     GST_ERROR_OBJECT (space, "did not specify framerate");
392     space->from_format = GST_VIDEO_FORMAT_UNKNOWN;
393     space->to_format = GST_VIDEO_FORMAT_UNKNOWN;
394     return FALSE;
395   }
396 format_mismatch:
397   {
398     GST_ERROR_OBJECT (space, "input and output formats do not match");
399     space->from_format = GST_VIDEO_FORMAT_UNKNOWN;
400     space->to_format = GST_VIDEO_FORMAT_UNKNOWN;
401     return FALSE;
402   }
403 invalid_palette:
404   {
405     GST_ERROR_OBJECT (space, "invalid palette");
406     space->from_format = GST_VIDEO_FORMAT_UNKNOWN;
407     space->to_format = GST_VIDEO_FORMAT_UNKNOWN;
408     return FALSE;
409   }
410 }
411
412 #define gst_csp_parent_class parent_class
413 G_DEFINE_TYPE (GstCsp, gst_csp, GST_TYPE_VIDEO_FILTER);
414
415 static void
416 gst_csp_finalize (GObject * obj)
417 {
418   GstCsp *space = GST_CSP (obj);
419
420   if (space->convert) {
421     colorspace_convert_free (space->convert);
422   }
423
424   G_OBJECT_CLASS (parent_class)->finalize (obj);
425 }
426
427 static void
428 gst_csp_class_init (GstCspClass * klass)
429 {
430   GObjectClass *gobject_class = (GObjectClass *) klass;
431   GstElementClass *gstelement_class = (GstElementClass *) klass;
432   GstBaseTransformClass *gstbasetransform_class =
433       (GstBaseTransformClass *) klass;
434
435   gobject_class->set_property = gst_csp_set_property;
436   gobject_class->get_property = gst_csp_get_property;
437   gobject_class->finalize = gst_csp_finalize;
438
439   gst_element_class_add_pad_template (gstelement_class,
440       gst_static_pad_template_get (&gst_csp_src_template));
441   gst_element_class_add_pad_template (gstelement_class,
442       gst_static_pad_template_get (&gst_csp_sink_template));
443
444   gst_element_class_set_details_simple (gstelement_class,
445       " Colorspace converter", "Filter/Converter/Video",
446       "Converts video from one colorspace to another",
447       "GStreamer maintainers <gstreamer-devel@lists.sourceforge.net>");
448
449   _QRAWRGB = g_quark_from_string ("video/x-raw-rgb");
450   _QRAWYUV = g_quark_from_string ("video/x-raw-yuv");
451   _QALPHAMASK = g_quark_from_string ("alpha_mask");
452
453   gstbasetransform_class->transform_caps =
454       GST_DEBUG_FUNCPTR (gst_csp_transform_caps);
455   gstbasetransform_class->set_caps = GST_DEBUG_FUNCPTR (gst_csp_set_caps);
456   gstbasetransform_class->get_unit_size =
457       GST_DEBUG_FUNCPTR (gst_csp_get_unit_size);
458   gstbasetransform_class->transform = GST_DEBUG_FUNCPTR (gst_csp_transform);
459
460   gstbasetransform_class->passthrough_on_same_caps = TRUE;
461
462   g_object_class_install_property (gobject_class, PROP_DITHER,
463       g_param_spec_enum ("dither", "Dither", "Apply dithering while converting",
464           dither_method_get_type (), DITHER_NONE,
465           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
466
467 }
468
469 static void
470 gst_csp_init (GstCsp * space)
471 {
472   space->from_format = GST_VIDEO_FORMAT_UNKNOWN;
473   space->to_format = GST_VIDEO_FORMAT_UNKNOWN;
474 }
475
476 void
477 gst_csp_set_property (GObject * object, guint property_id,
478     const GValue * value, GParamSpec * pspec)
479 {
480   GstCsp *csp;
481
482   g_return_if_fail (GST_IS_CSP (object));
483   csp = GST_CSP (object);
484
485   switch (property_id) {
486     case PROP_DITHER:
487       csp->dither = g_value_get_enum (value);
488       break;
489     default:
490       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
491       break;
492   }
493 }
494
495 void
496 gst_csp_get_property (GObject * object, guint property_id,
497     GValue * value, GParamSpec * pspec)
498 {
499   GstCsp *csp;
500
501   g_return_if_fail (GST_IS_CSP (object));
502   csp = GST_CSP (object);
503
504   switch (property_id) {
505     case PROP_DITHER:
506       g_value_set_enum (value, csp->dither);
507       break;
508     default:
509       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
510       break;
511   }
512 }
513
514 static gboolean
515 gst_csp_get_unit_size (GstBaseTransform * btrans, GstCaps * caps, gsize * size)
516 {
517   gboolean ret = TRUE;
518   GstVideoFormat format;
519   gint width, height;
520
521   g_assert (size);
522
523   ret = gst_video_format_parse_caps (caps, &format, &width, &height);
524   if (ret) {
525     *size = gst_video_format_get_size (format, width, height);
526   }
527
528   return ret;
529 }
530
531 static GstFlowReturn
532 gst_csp_transform (GstBaseTransform * btrans, GstBuffer * inbuf,
533     GstBuffer * outbuf)
534 {
535   GstCsp *space;
536   guint8 *indata, *outdata;
537   gsize insize, outsize;
538
539   space = GST_CSP (btrans);
540
541   GST_DEBUG ("from %d -> to %d", space->from_format, space->to_format);
542
543   if (G_UNLIKELY (space->from_format == GST_VIDEO_FORMAT_UNKNOWN ||
544           space->to_format == GST_VIDEO_FORMAT_UNKNOWN))
545     goto unknown_format;
546
547   colorspace_convert_set_dither (space->convert, space->dither);
548
549   indata = gst_buffer_map (inbuf, &insize, NULL, GST_MAP_READ);
550   outdata = gst_buffer_map (outbuf, &outsize, NULL, GST_MAP_WRITE);
551
552   colorspace_convert_convert (space->convert, outdata, indata);
553
554   gst_buffer_unmap (outbuf, outdata, outsize);
555   gst_buffer_unmap (inbuf, indata, insize);
556
557   /* baseclass copies timestamps */
558   GST_DEBUG ("from %d -> to %d done", space->from_format, space->to_format);
559
560   return GST_FLOW_OK;
561
562   /* ERRORS */
563 unknown_format:
564   {
565     GST_ELEMENT_ERROR (space, CORE, NOT_IMPLEMENTED, (NULL),
566         ("attempting to convert colorspaces between unknown formats"));
567     return GST_FLOW_NOT_NEGOTIATED;
568   }
569 #if 0
570 not_supported:
571   {
572     GST_ELEMENT_ERROR (space, CORE, NOT_IMPLEMENTED, (NULL),
573         ("cannot convert between formats"));
574     return GST_FLOW_NOT_SUPPORTED;
575   }
576 #endif
577 }
578
579 static gboolean
580 plugin_init (GstPlugin * plugin)
581 {
582   GST_DEBUG_CATEGORY_INIT (colorspace_debug, "colorspace", 0,
583       "Colorspace Converter");
584   GST_DEBUG_CATEGORY_GET (colorspace_performance, "GST_PERFORMANCE");
585
586   return gst_element_register (plugin, "colorspace",
587       GST_RANK_NONE, GST_TYPE_CSP);
588 }
589
590 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
591     GST_VERSION_MINOR,
592     "colorspace", "Colorspace conversion", plugin_init, VERSION, "LGPL", "", "")