[MOVED FROM BAD 31/68] colorspace: Revive element
[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 GST_DEBUG_CATEGORY (colorspace_debug);
44 #define GST_CAT_DEFAULT colorspace_debug
45 GST_DEBUG_CATEGORY (colorspace_performance);
46
47 #define CSP_VIDEO_CAPS                                          \
48   "video/x-raw-yuv, width = "GST_VIDEO_SIZE_RANGE" , "                  \
49   "height="GST_VIDEO_SIZE_RANGE",framerate="GST_VIDEO_FPS_RANGE","      \
50   "format= (fourcc) { I420 , NV12 , NV21 , YV12 , YUY2 , Y42B , Y444 , YUV9 , YVU9 , Y41B , Y800 , Y8 , GREY , Y16 , UYVY , YVYU , IYU1 , v308 , AYUV } ;" \
51   GST_VIDEO_CAPS_RGB";"                                                 \
52   GST_VIDEO_CAPS_BGR";"                                                 \
53   GST_VIDEO_CAPS_RGBx";"                                                \
54   GST_VIDEO_CAPS_xRGB";"                                                \
55   GST_VIDEO_CAPS_BGRx";"                                                \
56   GST_VIDEO_CAPS_xBGR";"                                                \
57   GST_VIDEO_CAPS_RGBA";"                                                \
58   GST_VIDEO_CAPS_ARGB";"                                                \
59   GST_VIDEO_CAPS_BGRA";"                                                \
60   GST_VIDEO_CAPS_ABGR";"                                                \
61   GST_VIDEO_CAPS_RGB_16";"                                              \
62   GST_VIDEO_CAPS_RGB_15";"                                              \
63   "video/x-raw-rgb, bpp = (int)8, depth = (int)8, "                     \
64       "width = "GST_VIDEO_SIZE_RANGE" , "                               \
65       "height = " GST_VIDEO_SIZE_RANGE ", "                             \
66       "framerate = "GST_VIDEO_FPS_RANGE ";"                             \
67   GST_VIDEO_CAPS_GRAY8";"                                               \
68   GST_VIDEO_CAPS_GRAY16("BIG_ENDIAN")";"                                \
69   GST_VIDEO_CAPS_GRAY16("LITTLE_ENDIAN")";"
70
71 static GstStaticPadTemplate gst_csp_src_template =
72 GST_STATIC_PAD_TEMPLATE ("src",
73     GST_PAD_SRC,
74     GST_PAD_ALWAYS,
75     GST_STATIC_CAPS (CSP_VIDEO_CAPS)
76     );
77
78 static GstStaticPadTemplate gst_csp_sink_template =
79 GST_STATIC_PAD_TEMPLATE ("sink",
80     GST_PAD_SINK,
81     GST_PAD_ALWAYS,
82     GST_STATIC_CAPS (CSP_VIDEO_CAPS)
83     );
84
85 GType gst_csp_get_type (void);
86
87 static gboolean gst_csp_set_caps (GstBaseTransform * btrans,
88     GstCaps * incaps, GstCaps * outcaps);
89 static gboolean gst_csp_get_unit_size (GstBaseTransform * btrans,
90     GstCaps * caps, guint * size);
91 static GstFlowReturn gst_csp_transform (GstBaseTransform * btrans,
92     GstBuffer * inbuf, GstBuffer * outbuf);
93
94 static GQuark _QRAWRGB;         /* "video/x-raw-rgb" */
95 static GQuark _QRAWYUV;         /* "video/x-raw-yuv" */
96 static GQuark _QALPHAMASK;      /* "alpha_mask" */
97
98 /* copies the given caps */
99 static GstCaps *
100 gst_csp_caps_remove_format_info (GstCaps * caps)
101 {
102   GstStructure *yuvst, *rgbst, *grayst;
103
104   /* We know there's only one structure since we're given simple caps */
105   caps = gst_caps_copy (caps);
106
107   yuvst = gst_caps_get_structure (caps, 0);
108
109   gst_structure_set_name (yuvst, "video/x-raw-yuv");
110   gst_structure_remove_fields (yuvst, "format", "endianness", "depth",
111       "bpp", "red_mask", "green_mask", "blue_mask", "alpha_mask",
112       "palette_data", NULL);
113
114   rgbst = gst_structure_copy (yuvst);
115   gst_structure_set_name (rgbst, "video/x-raw-rgb");
116   gst_structure_remove_fields (rgbst, "color-matrix", "chroma-site", NULL);
117
118   grayst = gst_structure_copy (rgbst);
119   gst_structure_set_name (grayst, "video/x-raw-gray");
120
121   gst_caps_append_structure (caps, rgbst);
122   gst_caps_append_structure (caps, grayst);
123
124   return caps;
125 }
126
127
128 static gboolean
129 gst_csp_structure_is_alpha (GstStructure * s)
130 {
131   GQuark name;
132
133   name = gst_structure_get_name_id (s);
134
135   if (name == _QRAWRGB) {
136     return gst_structure_id_has_field (s, _QALPHAMASK);
137   } else if (name == _QRAWYUV) {
138     guint32 fourcc;
139
140     if (!gst_structure_get_fourcc (s, "format", &fourcc))
141       return FALSE;
142
143     return (fourcc == GST_MAKE_FOURCC ('A', 'Y', 'U', 'V'));
144   }
145
146   return FALSE;
147 }
148
149 /* The caps can be transformed into any other caps with format info removed.
150  * However, we should prefer passthrough, so if passthrough is possible,
151  * put it first in the list. */
152 static GstCaps *
153 gst_csp_transform_caps (GstBaseTransform * btrans,
154     GstPadDirection direction, GstCaps * caps)
155 {
156   GstCaps *template;
157   GstCaps *tmp, *tmp2;
158   GstCaps *result;
159   GstStructure *s;
160   GstCaps *alpha, *non_alpha;
161
162   template = gst_static_pad_template_get_caps (&gst_csp_src_template);
163   result = gst_caps_copy (caps);
164
165   /* Get all possible caps that we can transform to */
166   tmp = gst_csp_caps_remove_format_info (caps);
167   tmp2 = gst_caps_intersect (tmp, template);
168   gst_caps_unref (tmp);
169   tmp = tmp2;
170
171   /* Now move alpha formats to the beginning if caps is an alpha format
172    * or at the end if caps is no alpha format */
173   alpha = gst_caps_new_empty ();
174   non_alpha = gst_caps_new_empty ();
175
176   while ((s = gst_caps_steal_structure (tmp, 0))) {
177     if (gst_csp_structure_is_alpha (s))
178       gst_caps_append_structure (alpha, s);
179     else
180       gst_caps_append_structure (non_alpha, s);
181   }
182
183   s = gst_caps_get_structure (caps, 0);
184   gst_caps_unref (tmp);
185
186   if (gst_csp_structure_is_alpha (s)) {
187     gst_caps_append (alpha, non_alpha);
188     tmp = alpha;
189   } else {
190     gst_caps_append (non_alpha, alpha);
191     tmp = non_alpha;
192   }
193
194   gst_caps_append (result, tmp);
195
196   GST_DEBUG_OBJECT (btrans, "transformed %" GST_PTR_FORMAT " into %"
197       GST_PTR_FORMAT, caps, result);
198
199   return result;
200 }
201
202 static gboolean
203 gst_csp_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
204     GstCaps * outcaps)
205 {
206   GstCsp *space;
207   GstVideoFormat in_format;
208   GstVideoFormat out_format;
209   gint in_height, in_width;
210   gint out_height, out_width;
211   gint in_fps_n, in_fps_d, in_par_n, in_par_d;
212   gint out_fps_n, out_fps_d, out_par_n, out_par_d;
213   gboolean have_in_par, have_out_par;
214   gboolean have_in_interlaced, have_out_interlaced;
215   gboolean in_interlaced, out_interlaced;
216   gboolean ret;
217
218   space = GST_CSP (btrans);
219
220   /* input caps */
221
222   ret = gst_video_format_parse_caps (incaps, &in_format, &in_width, &in_height);
223   if (!ret)
224     goto no_width_height;
225
226   ret = gst_video_parse_caps_framerate (incaps, &in_fps_n, &in_fps_d);
227   if (!ret)
228     goto no_framerate;
229
230   have_in_par = gst_video_parse_caps_pixel_aspect_ratio (incaps,
231       &in_par_n, &in_par_d);
232   have_in_interlaced = gst_video_format_parse_caps_interlaced (incaps,
233       &in_interlaced);
234
235
236   /* output caps */
237
238   ret =
239       gst_video_format_parse_caps (outcaps, &out_format, &out_width,
240       &out_height);
241   if (!ret)
242     goto no_width_height;
243
244   ret = gst_video_parse_caps_framerate (outcaps, &out_fps_n, &out_fps_d);
245   if (!ret)
246     goto no_framerate;
247
248   have_out_par = gst_video_parse_caps_pixel_aspect_ratio (outcaps,
249       &out_par_n, &out_par_d);
250   have_out_interlaced = gst_video_format_parse_caps_interlaced (incaps,
251       &out_interlaced);
252
253
254   /* these must match */
255   if (in_width != out_width || in_height != out_height ||
256       in_fps_n != out_fps_n || in_fps_d != out_fps_d)
257     goto format_mismatch;
258
259   /* if present, these must match too */
260   if (have_in_par && have_out_par &&
261       (in_par_n != out_par_n || in_par_d != out_par_d))
262     goto format_mismatch;
263
264   /* if present, these must match too */
265   if (have_in_interlaced && have_out_interlaced &&
266       in_interlaced != out_interlaced)
267     goto format_mismatch;
268
269   space->width = in_width;
270   space->height = in_height;
271   space->interlaced = in_interlaced;
272
273
274   /* palette, only for from data */
275   /* FIXME add palette handling */
276
277   GST_DEBUG ("reconfigured %d %d", space->from_format, space->to_format);
278
279   return TRUE;
280
281   /* ERRORS */
282 no_width_height:
283   {
284     GST_DEBUG_OBJECT (space, "did not specify width or height");
285     space->from_format = GST_VIDEO_FORMAT_UNKNOWN;
286     space->to_format = GST_VIDEO_FORMAT_UNKNOWN;
287     return FALSE;
288   }
289 no_framerate:
290   {
291     GST_DEBUG_OBJECT (space, "did not specify framerate");
292     space->from_format = GST_VIDEO_FORMAT_UNKNOWN;
293     space->to_format = GST_VIDEO_FORMAT_UNKNOWN;
294     return FALSE;
295   }
296 format_mismatch:
297   {
298     GST_DEBUG_OBJECT (space, "input and output formats do not match");
299     space->from_format = GST_VIDEO_FORMAT_UNKNOWN;
300     space->to_format = GST_VIDEO_FORMAT_UNKNOWN;
301     return FALSE;
302   }
303 }
304
305 GST_BOILERPLATE (GstCsp, gst_csp, GstVideoFilter, GST_TYPE_VIDEO_FILTER);
306
307 static void
308 gst_csp_base_init (gpointer klass)
309 {
310   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
311
312   gst_element_class_add_pad_template (element_class,
313       gst_static_pad_template_get (&gst_csp_src_template));
314   gst_element_class_add_pad_template (element_class,
315       gst_static_pad_template_get (&gst_csp_sink_template));
316
317   gst_element_class_set_details_simple (element_class,
318       " Colorspace converter", "Filter/Converter/Video",
319       "Converts video from one colorspace to another",
320       "GStreamer maintainers <gstreamer-devel@lists.sourceforge.net>");
321
322   _QRAWRGB = g_quark_from_string ("video/x-raw-rgb");
323   _QRAWYUV = g_quark_from_string ("video/x-raw-yuv");
324   _QALPHAMASK = g_quark_from_string ("alpha_mask");
325 }
326
327 static void
328 gst_csp_finalize (GObject * obj)
329 {
330   GstCsp *space = GST_CSP (obj);
331
332   if (space->palette)
333     g_free (space->palette);
334
335   G_OBJECT_CLASS (parent_class)->finalize (obj);
336 }
337
338 static void
339 gst_csp_class_init (GstCspClass * klass)
340 {
341   GObjectClass *gobject_class = (GObjectClass *) klass;
342   GstBaseTransformClass *gstbasetransform_class =
343       (GstBaseTransformClass *) klass;
344
345   gobject_class->finalize = gst_csp_finalize;
346
347   gstbasetransform_class->transform_caps =
348       GST_DEBUG_FUNCPTR (gst_csp_transform_caps);
349   gstbasetransform_class->set_caps = GST_DEBUG_FUNCPTR (gst_csp_set_caps);
350   gstbasetransform_class->get_unit_size =
351       GST_DEBUG_FUNCPTR (gst_csp_get_unit_size);
352   gstbasetransform_class->transform = GST_DEBUG_FUNCPTR (gst_csp_transform);
353
354   gstbasetransform_class->passthrough_on_same_caps = TRUE;
355 }
356
357 static void
358 gst_csp_init (GstCsp * space, GstCspClass * klass)
359 {
360   space->from_format = GST_VIDEO_FORMAT_UNKNOWN;
361   space->to_format = GST_VIDEO_FORMAT_UNKNOWN;
362   space->palette = NULL;
363 }
364
365 static gboolean
366 gst_csp_get_unit_size (GstBaseTransform * btrans, GstCaps * caps, guint * size)
367 {
368   gboolean ret = TRUE;
369   GstVideoFormat format;
370   gint width, height;
371
372   g_assert (size);
373
374   ret = gst_video_format_parse_caps (caps, &format, &width, &height);
375   if (ret) {
376     *size = gst_video_format_get_size (format, width, height);
377   }
378
379   return ret;
380 }
381
382 static GstFlowReturn
383 gst_csp_transform (GstBaseTransform * btrans, GstBuffer * inbuf,
384     GstBuffer * outbuf)
385 {
386   GstCsp *space;
387
388   space = GST_CSP (btrans);
389
390   GST_DEBUG ("from %d -> to %d", space->from_format, space->to_format);
391
392   if (G_UNLIKELY (space->from_format == GST_VIDEO_FORMAT_UNKNOWN ||
393           space->to_format == GST_VIDEO_FORMAT_UNKNOWN))
394     goto unknown_format;
395
396
397   /* baseclass copies timestamps */
398   GST_DEBUG ("from %d -> to %d done", space->from_format, space->to_format);
399
400   return GST_FLOW_OK;
401
402   /* ERRORS */
403 unknown_format:
404   {
405     GST_ELEMENT_ERROR (space, CORE, NOT_IMPLEMENTED, (NULL),
406         ("attempting to convert colorspaces between unknown formats"));
407     return GST_FLOW_NOT_NEGOTIATED;
408   }
409 #if 0
410 not_supported:
411   {
412     GST_ELEMENT_ERROR (space, CORE, NOT_IMPLEMENTED, (NULL),
413         ("cannot convert between formats"));
414     return GST_FLOW_NOT_SUPPORTED;
415   }
416 #endif
417 }
418
419 static gboolean
420 plugin_init (GstPlugin * plugin)
421 {
422   GST_DEBUG_CATEGORY_INIT (colorspace_debug, "colorspace", 0,
423       "Colorspace Converter");
424   GST_DEBUG_CATEGORY_GET (colorspace_performance, "GST_PERFORMANCE");
425
426   return gst_element_register (plugin, "colorspace",
427       GST_RANK_NONE, GST_TYPE_CSP);
428 }
429
430 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
431     GST_VERSION_MINOR,
432     "colorspace", "Colorspace conversion", plugin_init, VERSION, "LGPL", "", "")