[MOVED FROM BAD 62/68] colorspace: Add support for r210
[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 static void gst_csp_dispose (GObject * object);
101
102 static gboolean gst_csp_set_caps (GstBaseTransform * btrans,
103     GstCaps * incaps, GstCaps * outcaps);
104 static gboolean gst_csp_get_unit_size (GstBaseTransform * btrans,
105     GstCaps * caps, guint * size);
106 static GstFlowReturn gst_csp_transform (GstBaseTransform * btrans,
107     GstBuffer * inbuf, GstBuffer * outbuf);
108
109 static GQuark _QRAWRGB;         /* "video/x-raw-rgb" */
110 static GQuark _QRAWYUV;         /* "video/x-raw-yuv" */
111 static GQuark _QALPHAMASK;      /* "alpha_mask" */
112
113
114 static GType
115 dither_method_get_type (void)
116 {
117   static GType gtype = 0;
118
119   if (gtype == 0) {
120     static const GEnumValue values[] = {
121       {DITHER_NONE, "No dithering (default)", "none"},
122       {DITHER_VERTERR, "Vertical error propogation", "verterr"},
123       {DITHER_HALFTONE, "Half-tone", "halftone"},
124       {0, NULL, NULL}
125     };
126
127     gtype = g_enum_register_static ("GstColorspaceDitherMethod", values);
128   }
129   return gtype;
130 }
131
132 /* copies the given caps */
133 static GstCaps *
134 gst_csp_caps_remove_format_info (GstCaps * caps)
135 {
136   GstStructure *yuvst, *rgbst, *grayst;
137
138   /* We know there's only one structure since we're given simple caps */
139   caps = gst_caps_copy (caps);
140
141   yuvst = gst_caps_get_structure (caps, 0);
142
143   gst_structure_set_name (yuvst, "video/x-raw-yuv");
144   gst_structure_remove_fields (yuvst, "format", "endianness", "depth",
145       "bpp", "red_mask", "green_mask", "blue_mask", "alpha_mask",
146       "palette_data", "color-matrix", NULL);
147
148   rgbst = gst_structure_copy (yuvst);
149   gst_structure_set_name (rgbst, "video/x-raw-rgb");
150   gst_structure_remove_fields (rgbst, "color-matrix", "chroma-site", NULL);
151
152   grayst = gst_structure_copy (rgbst);
153   gst_structure_set_name (grayst, "video/x-raw-gray");
154
155   gst_caps_append_structure (caps, rgbst);
156   gst_caps_append_structure (caps, grayst);
157
158   return caps;
159 }
160
161
162 static gboolean
163 gst_csp_structure_is_alpha (GstStructure * s)
164 {
165   GQuark name;
166
167   name = gst_structure_get_name_id (s);
168
169   if (name == _QRAWRGB) {
170     return gst_structure_id_has_field (s, _QALPHAMASK);
171   } else if (name == _QRAWYUV) {
172     guint32 fourcc;
173
174     if (!gst_structure_get_fourcc (s, "format", &fourcc))
175       return FALSE;
176
177     return (fourcc == GST_MAKE_FOURCC ('A', 'Y', 'U', 'V'));
178   }
179
180   return FALSE;
181 }
182
183 /* The caps can be transformed into any other caps with format info removed.
184  * However, we should prefer passthrough, so if passthrough is possible,
185  * put it first in the list. */
186 static GstCaps *
187 gst_csp_transform_caps (GstBaseTransform * btrans,
188     GstPadDirection direction, GstCaps * caps)
189 {
190   GstCaps *template;
191   GstCaps *tmp, *tmp2;
192   GstCaps *result;
193   GstStructure *s;
194   GstCaps *alpha, *non_alpha;
195
196   template = gst_static_pad_template_get_caps (&gst_csp_src_template);
197   result = gst_caps_copy (caps);
198
199   /* Get all possible caps that we can transform to */
200   tmp = gst_csp_caps_remove_format_info (caps);
201   tmp2 = gst_caps_intersect (tmp, template);
202   gst_caps_unref (tmp);
203   tmp = tmp2;
204
205   /* Now move alpha formats to the beginning if caps is an alpha format
206    * or at the end if caps is no alpha format */
207   alpha = gst_caps_new_empty ();
208   non_alpha = gst_caps_new_empty ();
209
210   while ((s = gst_caps_steal_structure (tmp, 0))) {
211     if (gst_csp_structure_is_alpha (s))
212       gst_caps_append_structure (alpha, s);
213     else
214       gst_caps_append_structure (non_alpha, s);
215   }
216
217   s = gst_caps_get_structure (caps, 0);
218   gst_caps_unref (tmp);
219
220   if (gst_csp_structure_is_alpha (s)) {
221     gst_caps_append (alpha, non_alpha);
222     tmp = alpha;
223   } else {
224     gst_caps_append (non_alpha, alpha);
225     tmp = non_alpha;
226   }
227
228   gst_caps_append (result, tmp);
229
230   GST_DEBUG_OBJECT (btrans, "transformed %" GST_PTR_FORMAT " into %"
231       GST_PTR_FORMAT, caps, result);
232
233   return result;
234 }
235
236 static gboolean
237 gst_csp_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
238     GstCaps * outcaps)
239 {
240   GstCsp *space;
241   GstVideoFormat in_format;
242   GstVideoFormat out_format;
243   gint in_height, in_width;
244   gint out_height, out_width;
245   gint in_fps_n, in_fps_d, in_par_n, in_par_d;
246   gint out_fps_n, out_fps_d, out_par_n, out_par_d;
247   gboolean have_in_par, have_out_par;
248   gboolean have_in_interlaced, have_out_interlaced;
249   gboolean in_interlaced, out_interlaced;
250   gboolean ret;
251   ColorSpaceColorSpec in_spec, out_spec;
252
253   space = GST_CSP (btrans);
254
255   if (space->convert) {
256     colorspace_convert_free (space->convert);
257   }
258
259   /* input caps */
260
261   ret = gst_video_format_parse_caps (incaps, &in_format, &in_width, &in_height);
262   if (!ret)
263     goto no_width_height;
264
265   ret = gst_video_parse_caps_framerate (incaps, &in_fps_n, &in_fps_d);
266   if (!ret)
267     goto no_framerate;
268
269   have_in_par = gst_video_parse_caps_pixel_aspect_ratio (incaps,
270       &in_par_n, &in_par_d);
271   have_in_interlaced = gst_video_format_parse_caps_interlaced (incaps,
272       &in_interlaced);
273
274   if (gst_video_format_is_rgb (in_format)) {
275     in_spec = COLOR_SPEC_RGB;
276   } else if (gst_video_format_is_yuv (in_format)) {
277     const gchar *matrix = gst_video_parse_caps_color_matrix (incaps);
278
279     if (matrix && g_str_equal (matrix, "hdtv"))
280       in_spec = COLOR_SPEC_YUV_BT709;
281     else
282       in_spec = COLOR_SPEC_YUV_BT470_6;
283   } else {
284     in_spec = COLOR_SPEC_GRAY;
285   }
286
287   /* output caps */
288
289   ret =
290       gst_video_format_parse_caps (outcaps, &out_format, &out_width,
291       &out_height);
292   if (!ret)
293     goto no_width_height;
294
295   ret = gst_video_parse_caps_framerate (outcaps, &out_fps_n, &out_fps_d);
296   if (!ret)
297     goto no_framerate;
298
299   have_out_par = gst_video_parse_caps_pixel_aspect_ratio (outcaps,
300       &out_par_n, &out_par_d);
301   have_out_interlaced = gst_video_format_parse_caps_interlaced (incaps,
302       &out_interlaced);
303
304   if (gst_video_format_is_rgb (out_format)) {
305     out_spec = COLOR_SPEC_RGB;
306   } else if (gst_video_format_is_yuv (out_format)) {
307     const gchar *matrix = gst_video_parse_caps_color_matrix (outcaps);
308
309     if (matrix && g_str_equal (matrix, "hdtv"))
310       out_spec = COLOR_SPEC_YUV_BT709;
311     else
312       out_spec = COLOR_SPEC_YUV_BT470_6;
313   } else {
314     out_spec = COLOR_SPEC_GRAY;
315   }
316
317   /* these must match */
318   if (in_width != out_width || in_height != out_height ||
319       in_fps_n != out_fps_n || in_fps_d != out_fps_d)
320     goto format_mismatch;
321
322   /* if present, these must match too */
323   if (have_in_par && have_out_par &&
324       (in_par_n != out_par_n || in_par_d != out_par_d))
325     goto format_mismatch;
326
327   /* if present, these must match too */
328   if (have_in_interlaced && have_out_interlaced &&
329       in_interlaced != out_interlaced)
330     goto format_mismatch;
331
332   space->from_format = in_format;
333   space->from_spec = in_spec;
334   space->to_format = out_format;
335   space->to_spec = out_spec;
336   space->width = in_width;
337   space->height = in_height;
338   space->interlaced = in_interlaced;
339
340   space->convert = colorspace_convert_new (out_format, out_spec, in_format,
341       in_spec, in_width, in_height);
342   if (space->convert) {
343     colorspace_convert_set_interlaced (space->convert, in_interlaced);
344   }
345   /* palette, only for from data */
346   if (space->from_format == GST_VIDEO_FORMAT_RGB8_PALETTED &&
347       space->to_format == GST_VIDEO_FORMAT_RGB8_PALETTED) {
348     goto format_mismatch;
349   } else if (space->from_format == GST_VIDEO_FORMAT_RGB8_PALETTED) {
350     GstBuffer *palette;
351
352     palette = gst_video_parse_caps_palette (incaps);
353
354     if (!palette || GST_BUFFER_SIZE (palette) < 256 * 4) {
355       if (palette)
356         gst_buffer_unref (palette);
357       goto invalid_palette;
358     }
359     colorspace_convert_set_palette (space->convert,
360         (const guint32 *) GST_BUFFER_DATA (palette));
361     gst_buffer_unref (palette);
362   } else if (space->to_format == GST_VIDEO_FORMAT_RGB8_PALETTED) {
363     const guint32 *palette;
364     GstBuffer *p_buf;
365
366     palette = colorspace_convert_get_palette (space->convert);
367     p_buf = gst_buffer_new_and_alloc (256 * 4);
368     memcpy (GST_BUFFER_DATA (p_buf), palette, 256 * 4);
369     gst_caps_set_simple (outcaps, "palette_data", GST_TYPE_BUFFER, p_buf, NULL);
370     gst_buffer_unref (p_buf);
371   }
372
373   GST_DEBUG ("reconfigured %d %d", space->from_format, space->to_format);
374
375   return TRUE;
376
377   /* ERRORS */
378 no_width_height:
379   {
380     GST_ERROR_OBJECT (space, "did not specify width or height");
381     space->from_format = GST_VIDEO_FORMAT_UNKNOWN;
382     space->to_format = GST_VIDEO_FORMAT_UNKNOWN;
383     return FALSE;
384   }
385 no_framerate:
386   {
387     GST_ERROR_OBJECT (space, "did not specify framerate");
388     space->from_format = GST_VIDEO_FORMAT_UNKNOWN;
389     space->to_format = GST_VIDEO_FORMAT_UNKNOWN;
390     return FALSE;
391   }
392 format_mismatch:
393   {
394     GST_ERROR_OBJECT (space, "input and output formats do not match");
395     space->from_format = GST_VIDEO_FORMAT_UNKNOWN;
396     space->to_format = GST_VIDEO_FORMAT_UNKNOWN;
397     return FALSE;
398   }
399 invalid_palette:
400   {
401     GST_ERROR_OBJECT (space, "invalid palette");
402     space->from_format = GST_VIDEO_FORMAT_UNKNOWN;
403     space->to_format = GST_VIDEO_FORMAT_UNKNOWN;
404     return FALSE;
405   }
406 }
407
408 GST_BOILERPLATE (GstCsp, gst_csp, GstVideoFilter, GST_TYPE_VIDEO_FILTER);
409
410 static void
411 gst_csp_base_init (gpointer klass)
412 {
413   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
414
415   gst_element_class_add_pad_template (element_class,
416       gst_static_pad_template_get (&gst_csp_src_template));
417   gst_element_class_add_pad_template (element_class,
418       gst_static_pad_template_get (&gst_csp_sink_template));
419
420   gst_element_class_set_details_simple (element_class,
421       " Colorspace converter", "Filter/Converter/Video",
422       "Converts video from one colorspace to another",
423       "GStreamer maintainers <gstreamer-devel@lists.sourceforge.net>");
424
425   _QRAWRGB = g_quark_from_string ("video/x-raw-rgb");
426   _QRAWYUV = g_quark_from_string ("video/x-raw-yuv");
427   _QALPHAMASK = g_quark_from_string ("alpha_mask");
428 }
429
430 void
431 gst_csp_dispose (GObject * object)
432 {
433   GstCsp *csp;
434
435   g_return_if_fail (GST_IS_CSP (object));
436   csp = GST_CSP (object);
437
438   /* clean up as possible.  may be called multiple times */
439
440   G_OBJECT_CLASS (parent_class)->dispose (object);
441 }
442
443 static void
444 gst_csp_finalize (GObject * obj)
445 {
446   GstCsp *space = GST_CSP (obj);
447
448   if (space->convert) {
449     colorspace_convert_free (space->convert);
450   }
451
452   G_OBJECT_CLASS (parent_class)->finalize (obj);
453
454 }
455
456 static void
457 gst_csp_class_init (GstCspClass * klass)
458 {
459   GObjectClass *gobject_class = (GObjectClass *) klass;
460   GstBaseTransformClass *gstbasetransform_class =
461       (GstBaseTransformClass *) klass;
462
463   gobject_class->set_property = gst_csp_set_property;
464   gobject_class->get_property = gst_csp_get_property;
465   gobject_class->dispose = gst_csp_dispose;
466   gobject_class->finalize = gst_csp_finalize;
467
468   gstbasetransform_class->transform_caps =
469       GST_DEBUG_FUNCPTR (gst_csp_transform_caps);
470   gstbasetransform_class->set_caps = GST_DEBUG_FUNCPTR (gst_csp_set_caps);
471   gstbasetransform_class->get_unit_size =
472       GST_DEBUG_FUNCPTR (gst_csp_get_unit_size);
473   gstbasetransform_class->transform = GST_DEBUG_FUNCPTR (gst_csp_transform);
474
475   gstbasetransform_class->passthrough_on_same_caps = TRUE;
476
477   g_object_class_install_property (gobject_class, PROP_DITHER,
478       g_param_spec_enum ("dither", "Dither", "Apply dithering while converting",
479           dither_method_get_type (), DITHER_NONE,
480           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
481
482 }
483
484 static void
485 gst_csp_init (GstCsp * space, GstCspClass * klass)
486 {
487   space->from_format = GST_VIDEO_FORMAT_UNKNOWN;
488   space->to_format = GST_VIDEO_FORMAT_UNKNOWN;
489 }
490
491 void
492 gst_csp_set_property (GObject * object, guint property_id,
493     const GValue * value, GParamSpec * pspec)
494 {
495   GstCsp *csp;
496
497   g_return_if_fail (GST_IS_CSP (object));
498   csp = GST_CSP (object);
499
500   switch (property_id) {
501     case PROP_DITHER:
502       csp->dither = g_value_get_enum (value);
503       break;
504     default:
505       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
506       break;
507   }
508 }
509
510 void
511 gst_csp_get_property (GObject * object, guint property_id,
512     GValue * value, GParamSpec * pspec)
513 {
514   GstCsp *csp;
515
516   g_return_if_fail (GST_IS_CSP (object));
517   csp = GST_CSP (object);
518
519   switch (property_id) {
520     case PROP_DITHER:
521       g_value_set_enum (value, csp->dither);
522       break;
523     default:
524       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
525       break;
526   }
527 }
528
529 static gboolean
530 gst_csp_get_unit_size (GstBaseTransform * btrans, GstCaps * caps, guint * size)
531 {
532   gboolean ret = TRUE;
533   GstVideoFormat format;
534   gint width, height;
535
536   g_assert (size);
537
538   ret = gst_video_format_parse_caps (caps, &format, &width, &height);
539   if (ret) {
540     *size = gst_video_format_get_size (format, width, height);
541   }
542
543   return ret;
544 }
545
546 static GstFlowReturn
547 gst_csp_transform (GstBaseTransform * btrans, GstBuffer * inbuf,
548     GstBuffer * outbuf)
549 {
550   GstCsp *space;
551
552   space = GST_CSP (btrans);
553
554   GST_DEBUG ("from %d -> to %d", space->from_format, space->to_format);
555
556   if (G_UNLIKELY (space->from_format == GST_VIDEO_FORMAT_UNKNOWN ||
557           space->to_format == GST_VIDEO_FORMAT_UNKNOWN))
558     goto unknown_format;
559
560   if (space->dither) {
561     colorspace_convert_set_dither (space->convert, 1);
562   } else {
563     colorspace_convert_set_dither (space->convert, 0);
564   }
565
566   colorspace_convert_convert (space->convert, GST_BUFFER_DATA (outbuf),
567       GST_BUFFER_DATA (inbuf));
568
569   /* baseclass copies timestamps */
570   GST_DEBUG ("from %d -> to %d done", space->from_format, space->to_format);
571
572   return GST_FLOW_OK;
573
574   /* ERRORS */
575 unknown_format:
576   {
577     GST_ELEMENT_ERROR (space, CORE, NOT_IMPLEMENTED, (NULL),
578         ("attempting to convert colorspaces between unknown formats"));
579     return GST_FLOW_NOT_NEGOTIATED;
580   }
581 #if 0
582 not_supported:
583   {
584     GST_ELEMENT_ERROR (space, CORE, NOT_IMPLEMENTED, (NULL),
585         ("cannot convert between formats"));
586     return GST_FLOW_NOT_SUPPORTED;
587   }
588 #endif
589 }
590
591 static gboolean
592 plugin_init (GstPlugin * plugin)
593 {
594   GST_DEBUG_CATEGORY_INIT (colorspace_debug, "colorspace", 0,
595       "Colorspace Converter");
596   GST_DEBUG_CATEGORY_GET (colorspace_performance, "GST_PERFORMANCE");
597
598   return gst_element_register (plugin, "colorspace",
599       GST_RANK_NONE, GST_TYPE_CSP);
600 }
601
602 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
603     GST_VERSION_MINOR,
604     "colorspace", "Colorspace conversion", plugin_init, VERSION, "LGPL", "", "")