2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
4 * Copyright (C) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
5 * Copyright (C) 2010 David Schleef <ds@schleef.org>
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.
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.
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.
24 * SECTION:element-colorspace
26 * Convert video frames between a great variety of colorspace formats.
29 * <title>Example launch line</title>
31 * gst-launch -v videotestsrc ! video/x-raw-yuv,format=\(fourcc\)YUY2 ! colorspace ! ximagesink
40 #include "gstcolorspace.h"
41 #include <gst/video/video.h>
43 GST_DEBUG_CATEGORY (colorspace_debug);
44 #define GST_CAT_DEFAULT colorspace_debug
45 GST_DEBUG_CATEGORY (colorspace_performance);
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")";"
71 static GstStaticPadTemplate gst_csp_src_template =
72 GST_STATIC_PAD_TEMPLATE ("src",
75 GST_STATIC_CAPS (CSP_VIDEO_CAPS)
78 static GstStaticPadTemplate gst_csp_sink_template =
79 GST_STATIC_PAD_TEMPLATE ("sink",
82 GST_STATIC_CAPS (CSP_VIDEO_CAPS)
85 GType gst_csp_get_type (void);
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);
94 static GQuark _QRAWRGB; /* "video/x-raw-rgb" */
95 static GQuark _QRAWYUV; /* "video/x-raw-yuv" */
96 static GQuark _QALPHAMASK; /* "alpha_mask" */
98 /* copies the given caps */
100 gst_csp_caps_remove_format_info (GstCaps * caps)
102 GstStructure *yuvst, *rgbst, *grayst;
104 /* We know there's only one structure since we're given simple caps */
105 caps = gst_caps_copy (caps);
107 yuvst = gst_caps_get_structure (caps, 0);
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);
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);
118 grayst = gst_structure_copy (rgbst);
119 gst_structure_set_name (grayst, "video/x-raw-gray");
121 gst_caps_append_structure (caps, rgbst);
122 gst_caps_append_structure (caps, grayst);
129 gst_csp_structure_is_alpha (GstStructure * s)
133 name = gst_structure_get_name_id (s);
135 if (name == _QRAWRGB) {
136 return gst_structure_id_has_field (s, _QALPHAMASK);
137 } else if (name == _QRAWYUV) {
140 if (!gst_structure_get_fourcc (s, "format", &fourcc))
143 return (fourcc == GST_MAKE_FOURCC ('A', 'Y', 'U', 'V'));
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. */
153 gst_csp_transform_caps (GstBaseTransform * btrans,
154 GstPadDirection direction, GstCaps * caps)
160 GstCaps *alpha, *non_alpha;
162 template = gst_static_pad_template_get_caps (&gst_csp_src_template);
163 result = gst_caps_copy (caps);
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);
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 ();
176 while ((s = gst_caps_steal_structure (tmp, 0))) {
177 if (gst_csp_structure_is_alpha (s))
178 gst_caps_append_structure (alpha, s);
180 gst_caps_append_structure (non_alpha, s);
183 s = gst_caps_get_structure (caps, 0);
184 gst_caps_unref (tmp);
186 if (gst_csp_structure_is_alpha (s)) {
187 gst_caps_append (alpha, non_alpha);
190 gst_caps_append (non_alpha, alpha);
194 gst_caps_append (result, tmp);
196 GST_DEBUG_OBJECT (btrans, "transformed %" GST_PTR_FORMAT " into %"
197 GST_PTR_FORMAT, caps, result);
203 gst_csp_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
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;
218 space = GST_CSP (btrans);
222 ret = gst_video_format_parse_caps (incaps, &in_format, &in_width, &in_height);
224 goto no_width_height;
226 ret = gst_video_parse_caps_framerate (incaps, &in_fps_n, &in_fps_d);
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,
239 gst_video_format_parse_caps (outcaps, &out_format, &out_width,
242 goto no_width_height;
244 ret = gst_video_parse_caps_framerate (outcaps, &out_fps_n, &out_fps_d);
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,
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;
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;
264 /* if present, these must match too */
265 if (have_in_interlaced && have_out_interlaced &&
266 in_interlaced != out_interlaced)
267 goto format_mismatch;
269 space->width = in_width;
270 space->height = in_height;
271 space->interlaced = in_interlaced;
274 /* palette, only for from data */
275 /* FIXME add palette handling */
277 GST_DEBUG ("reconfigured %d %d", space->from_format, space->to_format);
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;
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;
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;
305 GST_BOILERPLATE (GstCsp, gst_csp, GstVideoFilter, GST_TYPE_VIDEO_FILTER);
308 gst_csp_base_init (gpointer klass)
310 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
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));
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>");
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");
328 gst_csp_finalize (GObject * obj)
330 GstCsp *space = GST_CSP (obj);
333 g_free (space->palette);
335 G_OBJECT_CLASS (parent_class)->finalize (obj);
339 gst_csp_class_init (GstCspClass * klass)
341 GObjectClass *gobject_class = (GObjectClass *) klass;
342 GstBaseTransformClass *gstbasetransform_class =
343 (GstBaseTransformClass *) klass;
345 gobject_class->finalize = gst_csp_finalize;
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);
354 gstbasetransform_class->passthrough_on_same_caps = TRUE;
358 gst_csp_init (GstCsp * space, GstCspClass * klass)
360 space->from_format = GST_VIDEO_FORMAT_UNKNOWN;
361 space->to_format = GST_VIDEO_FORMAT_UNKNOWN;
362 space->palette = NULL;
366 gst_csp_get_unit_size (GstBaseTransform * btrans, GstCaps * caps, guint * size)
369 GstVideoFormat format;
374 ret = gst_video_format_parse_caps (caps, &format, &width, &height);
376 *size = gst_video_format_get_size (format, width, height);
383 gst_csp_transform (GstBaseTransform * btrans, GstBuffer * inbuf,
388 space = GST_CSP (btrans);
390 GST_DEBUG ("from %d -> to %d", space->from_format, space->to_format);
392 if (G_UNLIKELY (space->from_format == GST_VIDEO_FORMAT_UNKNOWN ||
393 space->to_format == GST_VIDEO_FORMAT_UNKNOWN))
397 /* baseclass copies timestamps */
398 GST_DEBUG ("from %d -> to %d done", space->from_format, space->to_format);
405 GST_ELEMENT_ERROR (space, CORE, NOT_IMPLEMENTED, (NULL),
406 ("attempting to convert colorspaces between unknown formats"));
407 return GST_FLOW_NOT_NEGOTIATED;
412 GST_ELEMENT_ERROR (space, CORE, NOT_IMPLEMENTED, (NULL),
413 ("cannot convert between formats"));
414 return GST_FLOW_NOT_SUPPORTED;
420 plugin_init (GstPlugin * plugin)
422 GST_DEBUG_CATEGORY_INIT (colorspace_debug, "colorspace", 0,
423 "Colorspace Converter");
424 GST_DEBUG_CATEGORY_GET (colorspace_performance, "GST_PERFORMANCE");
426 return gst_element_register (plugin, "colorspace",
427 GST_RANK_NONE, GST_TYPE_CSP);
430 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
432 "colorspace", "Colorspace conversion", plugin_init, VERSION, "LGPL", "", "")