[MOVED FROM BAD 32/68] colorspace: Add conversion code
[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, v210 } ;" \
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->from_format = in_format;
270   space->to_format = out_format;
271   space->width = in_width;
272   space->height = in_height;
273   space->interlaced = in_interlaced;
274
275   space->convert = colorspace_convert_new (out_format, in_format,
276       in_width, in_height);
277   if (space->convert) {
278     colorspace_convert_set_interlaced (space->convert, in_interlaced);
279   }
280
281   /* palette, only for from data */
282   /* FIXME add palette handling */
283 #if 0
284   colorspace_convert_set_palette (convert, palette);
285 #endif
286
287   GST_DEBUG ("reconfigured %d %d", space->from_format, space->to_format);
288
289   return TRUE;
290
291   /* ERRORS */
292 no_width_height:
293   {
294     GST_ERROR_OBJECT (space, "did not specify width or height");
295     space->from_format = GST_VIDEO_FORMAT_UNKNOWN;
296     space->to_format = GST_VIDEO_FORMAT_UNKNOWN;
297     return FALSE;
298   }
299 no_framerate:
300   {
301     GST_ERROR_OBJECT (space, "did not specify framerate");
302     space->from_format = GST_VIDEO_FORMAT_UNKNOWN;
303     space->to_format = GST_VIDEO_FORMAT_UNKNOWN;
304     return FALSE;
305   }
306 format_mismatch:
307   {
308     GST_ERROR_OBJECT (space, "input and output formats do not match");
309     space->from_format = GST_VIDEO_FORMAT_UNKNOWN;
310     space->to_format = GST_VIDEO_FORMAT_UNKNOWN;
311     return FALSE;
312   }
313 }
314
315 GST_BOILERPLATE (GstCsp, gst_csp, GstVideoFilter, GST_TYPE_VIDEO_FILTER);
316
317 static void
318 gst_csp_base_init (gpointer klass)
319 {
320   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
321
322   gst_element_class_add_pad_template (element_class,
323       gst_static_pad_template_get (&gst_csp_src_template));
324   gst_element_class_add_pad_template (element_class,
325       gst_static_pad_template_get (&gst_csp_sink_template));
326
327   gst_element_class_set_details_simple (element_class,
328       " Colorspace converter", "Filter/Converter/Video",
329       "Converts video from one colorspace to another",
330       "GStreamer maintainers <gstreamer-devel@lists.sourceforge.net>");
331
332   _QRAWRGB = g_quark_from_string ("video/x-raw-rgb");
333   _QRAWYUV = g_quark_from_string ("video/x-raw-yuv");
334   _QALPHAMASK = g_quark_from_string ("alpha_mask");
335 }
336
337 static void
338 gst_csp_finalize (GObject * obj)
339 {
340   GstCsp *space = GST_CSP (obj);
341
342   if (space->palette)
343     g_free (space->palette);
344
345   G_OBJECT_CLASS (parent_class)->finalize (obj);
346 }
347
348 static void
349 gst_csp_class_init (GstCspClass * klass)
350 {
351   GObjectClass *gobject_class = (GObjectClass *) klass;
352   GstBaseTransformClass *gstbasetransform_class =
353       (GstBaseTransformClass *) klass;
354
355   gobject_class->finalize = gst_csp_finalize;
356
357   gstbasetransform_class->transform_caps =
358       GST_DEBUG_FUNCPTR (gst_csp_transform_caps);
359   gstbasetransform_class->set_caps = GST_DEBUG_FUNCPTR (gst_csp_set_caps);
360   gstbasetransform_class->get_unit_size =
361       GST_DEBUG_FUNCPTR (gst_csp_get_unit_size);
362   gstbasetransform_class->transform = GST_DEBUG_FUNCPTR (gst_csp_transform);
363
364   gstbasetransform_class->passthrough_on_same_caps = TRUE;
365 }
366
367 static void
368 gst_csp_init (GstCsp * space, GstCspClass * klass)
369 {
370   space->from_format = GST_VIDEO_FORMAT_UNKNOWN;
371   space->to_format = GST_VIDEO_FORMAT_UNKNOWN;
372   space->palette = NULL;
373 }
374
375 static gboolean
376 gst_csp_get_unit_size (GstBaseTransform * btrans, GstCaps * caps, guint * size)
377 {
378   gboolean ret = TRUE;
379   GstVideoFormat format;
380   gint width, height;
381
382   g_assert (size);
383
384   ret = gst_video_format_parse_caps (caps, &format, &width, &height);
385   if (ret) {
386     *size = gst_video_format_get_size (format, width, height);
387   }
388
389   return ret;
390 }
391
392 static GstFlowReturn
393 gst_csp_transform (GstBaseTransform * btrans, GstBuffer * inbuf,
394     GstBuffer * outbuf)
395 {
396   GstCsp *space;
397
398   space = GST_CSP (btrans);
399
400   GST_DEBUG ("from %d -> to %d", space->from_format, space->to_format);
401
402   if (G_UNLIKELY (space->from_format == GST_VIDEO_FORMAT_UNKNOWN ||
403           space->to_format == GST_VIDEO_FORMAT_UNKNOWN))
404     goto unknown_format;
405
406   colorspace_convert_convert (space->convert, GST_BUFFER_DATA (outbuf),
407       GST_BUFFER_DATA (inbuf));
408
409   /* baseclass copies timestamps */
410   GST_DEBUG ("from %d -> to %d done", space->from_format, space->to_format);
411
412   return GST_FLOW_OK;
413
414   /* ERRORS */
415 unknown_format:
416   {
417     GST_ELEMENT_ERROR (space, CORE, NOT_IMPLEMENTED, (NULL),
418         ("attempting to convert colorspaces between unknown formats"));
419     return GST_FLOW_NOT_NEGOTIATED;
420   }
421 #if 0
422 not_supported:
423   {
424     GST_ELEMENT_ERROR (space, CORE, NOT_IMPLEMENTED, (NULL),
425         ("cannot convert between formats"));
426     return GST_FLOW_NOT_SUPPORTED;
427   }
428 #endif
429 }
430
431 static gboolean
432 plugin_init (GstPlugin * plugin)
433 {
434   GST_DEBUG_CATEGORY_INIT (colorspace_debug, "colorspace", 0,
435       "Colorspace Converter");
436   GST_DEBUG_CATEGORY_GET (colorspace_performance, "GST_PERFORMANCE");
437
438   return gst_element_register (plugin, "colorspace",
439       GST_RANK_NONE, GST_TYPE_CSP);
440 }
441
442 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
443     GST_VERSION_MINOR,
444     "colorspace", "Colorspace conversion", plugin_init, VERSION, "LGPL", "", "")