2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
23 #include "gstcolorspace.h"
25 #include <gst/video/video.h>
30 static GstColorspaceFormat gst_colorspace_formats[] = {
31 {GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("I420"))},
32 {GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("YV12"))},
33 {GST_STATIC_CAPS (GST_VIDEO_CAPS_xRGB)},
34 {GST_STATIC_CAPS (GST_VIDEO_CAPS_RGB)},
35 {GST_STATIC_CAPS (GST_VIDEO_CAPS_RGB_16)},
38 static GstColorspaceConverter gst_colorspace_converters[] = {
39 {GST_COLORSPACE_I420, GST_COLORSPACE_RGB32, gst_colorspace_I420_to_rgb32},
40 {GST_COLORSPACE_YV12, GST_COLORSPACE_RGB32, gst_colorspace_YV12_to_rgb32},
41 {GST_COLORSPACE_I420, GST_COLORSPACE_RGB24, gst_colorspace_I420_to_rgb24},
42 {GST_COLORSPACE_YV12, GST_COLORSPACE_RGB24, gst_colorspace_YV12_to_rgb24},
43 {GST_COLORSPACE_I420, GST_COLORSPACE_RGB16, gst_colorspace_I420_to_rgb16},
44 {GST_COLORSPACE_YV12, GST_COLORSPACE_RGB16, gst_colorspace_YV12_to_rgb16},
47 static const GstElementDetails colorspace_details =
48 GST_ELEMENT_DETAILS ("Colorspace converter",
49 "Filter/Converter/Video",
50 "Converts video from YUV to RGB",
51 "Wim Taymans <wim.taymans@chello.be>");
53 static GstStaticPadTemplate gst_colorspace_sink_template =
54 GST_STATIC_PAD_TEMPLATE ("sink",
57 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("{ I420, YV12 }"))
60 static GstStaticPadTemplate gst_colorspace_src_template =
61 GST_STATIC_PAD_TEMPLATE ("src",
64 GST_STATIC_CAPS (GST_VIDEO_CAPS_xRGB "; "
65 GST_VIDEO_CAPS_RGB "; " GST_VIDEO_CAPS_RGB_16)
68 /* Stereo signals and args */
82 static void gst_colorspace_base_init (gpointer g_class);
83 static void gst_colorspace_class_init (GstColorspaceClass * klass);
84 static void gst_colorspace_init (GstColorspace * space);
86 static void gst_colorspace_set_property (GObject * object, guint prop_id,
87 const GValue * value, GParamSpec * pspec);
88 static void gst_colorspace_get_property (GObject * object, guint prop_id,
89 GValue * value, GParamSpec * pspec);
91 static GstPadLinkReturn
92 gst_colorspace_link (GstPad * pad, const GstCaps * caps);
93 static void gst_colorspace_chain (GstPad * pad, GstData * _data);
94 static GstStateChangeReturn gst_colorspace_change_state (GstElement * element,
95 GstStateChange transition);
98 static GstElementClass *parent_class = NULL;
100 /*static guint gst_colorspace_signals[LAST_SIGNAL] = { 0 }; */
104 colorspace_setup_converter (GstColorspace * space, GstCaps * from_caps,
107 guint32 from_space, to_space;
108 GstStructure *from_struct;
109 GstStructure *to_struct;
111 g_return_val_if_fail (to_caps != NULL, FALSE);
112 g_return_val_if_fail (from_caps != NULL, FALSE);
114 from_struct = gst_caps_get_structure (from_caps, 0);
115 to_struct = gst_caps_get_structure (to_caps, 0);
117 from_space = GST_MAKE_FOURCC ('R', 'G', 'B', ' ');
118 gst_structure_get_fourcc (from_struct, "format", &from_space);
120 to_space = GST_MAKE_FOURCC ('R', 'G', 'B', ' ');
121 gst_structure_get_fourcc (to_struct, "format", &to_space);
123 GST_INFO ("set up converter for " GST_FOURCC_FORMAT
124 " (%08x) to " GST_FOURCC_FORMAT " (%08x)",
125 GST_FOURCC_ARGS (from_space), from_space,
126 GST_FOURCC_ARGS (to_space), to_space);
128 switch (from_space) {
129 case GST_MAKE_FOURCC ('R', 'G', 'B', ' '):
133 gst_structure_get_int (from_struct, "bpp", &from_bpp);
136 case GST_MAKE_FOURCC ('R', 'G', 'B', ' '):
141 gst_structure_get_int (to_struct, "bpp", &to_bpp);
143 gst_structure_get_int (from_struct, "red_mask", &space->source.r);
144 gst_structure_get_int (from_struct, "green_mask", &space->source.g);
145 gst_structure_get_int (from_struct, "blue_mask", &space->source.b);
147 space->srcbpp = space->source.bits = from_bpp;
148 space->source.indexed = 0;
149 space->source.has_colorkey = 0;
151 GST_INFO ("source red mask %08x", space->source.r);
152 GST_INFO ("source green mask %08x", space->source.g);
153 GST_INFO ("source blue mask %08x", space->source.b);
154 GST_INFO ("source bpp %08x", space->srcbpp);
156 gst_structure_get_int (to_struct, "red_mask", &space->dest.r);
157 gst_structure_get_int (to_struct, "green_mask", &space->dest.g);
158 gst_structure_get_int (to_struct, "blue_mask", &space->dest.b);
160 space->destbpp = space->dest.bits = to_bpp;
161 space->dest.indexed = 0;
162 space->dest.has_colorkey = 0;
164 GST_INFO ("dest red mask %08x", space->dest.r);
165 GST_INFO ("dest green mask %08x", space->dest.g);
166 GST_INFO ("dest blue mask %08x", space->dest.b);
167 GST_INFO ("dest bpp %08x", space->destbpp);
169 if (!Hermes_ConverterRequest (space->h_handle, &space->source,
171 g_warning ("Hermes: could not get converter\n");
174 GST_INFO ("converter set up");
175 space->type = GST_COLORSPACE_HERMES;
179 g_warning ("colorspace: compiled without hermes!");
182 case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
183 if (from_bpp == 32) {
184 space->type = GST_COLORSPACE_RGB32_YV12;
188 case GST_MAKE_FOURCC ('I', '4', '2', '0'):
189 if (from_bpp == 32) {
190 space->type = GST_COLORSPACE_RGB32_I420;
194 case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
195 GST_INFO ("colorspace: RGB to YUV with bpp %d not implemented!!",
201 case GST_MAKE_FOURCC ('I', '4', '2', '0'):
203 case GST_MAKE_FOURCC ('R', 'G', 'B', ' '):
204 GST_INFO ("colorspace: YUV to RGB");
206 gst_structure_get_int (to_struct, "bpp", &space->destbpp);
208 gst_colorspace_yuv2rgb_get_converter (from_caps, to_caps);
209 space->type = GST_COLORSPACE_YUV_RGB;
211 case GST_MAKE_FOURCC ('I', '4', '2', '0'):
212 space->type = GST_COLORSPACE_NONE;
215 case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
216 space->type = GST_COLORSPACE_420_SWAP;
222 case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
224 case GST_MAKE_FOURCC ('I', '4', '2', '0'):
225 space->type = GST_COLORSPACE_YUY2_I420;
228 case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
229 space->type = GST_COLORSPACE_NONE;
232 case GST_MAKE_FOURCC ('R', 'G', 'B', ' '):
233 GST_INFO ("colorspace: YUY2 to RGB not implemented!!");
237 case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
239 case GST_MAKE_FOURCC ('R', 'G', 'B', ' '):
240 GST_INFO ("colorspace: YV12 to RGB");
242 gst_structure_get_int (to_struct, "bpp", &space->destbpp);
244 gst_colorspace_yuv2rgb_get_converter (from_caps, to_caps);
245 space->type = GST_COLORSPACE_YUV_RGB;
247 case GST_MAKE_FOURCC ('I', '4', '2', '0'):
248 space->type = GST_COLORSPACE_420_SWAP;
251 case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
252 space->type = GST_COLORSPACE_NONE;
263 gst_colorspace_caps_remove_format_info (GstCaps * caps, const char *media_type)
266 GstStructure *structure;
268 for (i = 0; i < gst_caps_get_size (caps); i++) {
269 structure = gst_caps_get_structure (caps, i);
271 gst_structure_set_name (structure, media_type);
272 gst_structure_remove_field (structure, "format");
273 gst_structure_remove_field (structure, "endianness");
274 gst_structure_remove_field (structure, "depth");
275 gst_structure_remove_field (structure, "bpp");
276 gst_structure_remove_field (structure, "red_mask");
277 gst_structure_remove_field (structure, "green_mask");
278 gst_structure_remove_field (structure, "blue_mask");
281 gst_caps_do_simplify (caps);
286 gst_colorspace_getcaps (GstPad * pad)
288 GstColorspace *space;
293 space = GST_COLORSPACE (gst_pad_get_parent (pad));
295 otherpad = (pad == space->srcpad) ? space->sinkpad : space->srcpad;
297 othercaps = gst_pad_get_allowed_caps (otherpad);
299 othercaps = gst_colorspace_caps_remove_format_info (othercaps,
300 (pad == space->srcpad) ? "video/x-raw-rgb" : "video/x-raw-yuv");
302 caps = gst_caps_intersect (othercaps, gst_pad_get_pad_template_caps (pad));
303 gst_caps_free (othercaps);
308 static GstColorSpaceFormatType
309 gst_colorspace_get_format (const GstCaps * caps)
313 for (i = 0; i < G_N_ELEMENTS (gst_colorspace_formats); i++) {
318 gst_caps_copy (gst_static_caps_get (&gst_colorspace_formats[i].caps));
320 icaps = gst_caps_intersect (caps, fcaps);
321 if (!gst_caps_is_empty (icaps)) {
322 gst_caps_free (icaps);
325 gst_caps_free (icaps);
328 g_assert_not_reached ();
332 #define ROUND_UP_2(x) (((x)+1)&~1)
333 #define ROUND_UP_4(x) (((x)+3)&~3)
334 #define ROUND_UP_8(x) (((x)+7)&~7)
337 gst_colorspace_format_get_size (GstColorSpaceFormatType index, int width,
343 case GST_COLORSPACE_I420:
344 case GST_COLORSPACE_YV12:
345 size = ROUND_UP_4 (width) * ROUND_UP_2 (height);
346 size += ROUND_UP_8 (width) / 2 * ROUND_UP_2 (height) / 2;
347 size += ROUND_UP_8 (width) / 2 * ROUND_UP_2 (height) / 2;
350 case GST_COLORSPACE_RGB32:
351 return width * height * 4;
353 case GST_COLORSPACE_RGB24:
354 return ROUND_UP_4 (width * 3) * height;
356 case GST_COLORSPACE_RGB16:
357 return ROUND_UP_4 (width * 2) * height;
361 g_assert_not_reached ();
366 gst_colorspace_get_converter (GstColorSpaceFormatType from,
367 GstColorSpaceFormatType to)
371 for (i = 0; i < G_N_ELEMENTS (gst_colorspace_converters); i++) {
372 GstColorspaceConverter *converter = gst_colorspace_converters + i;
374 if (from == converter->from && to == converter->to) {
378 g_assert_not_reached ();
382 static GstPadLinkReturn
383 gst_colorspace_link (GstPad * pad, const GstCaps * caps)
385 GstColorspace *space;
387 GstStructure *structure;
388 GstPadLinkReturn link_ret;
393 space = GST_COLORSPACE (gst_pad_get_parent (pad));
394 otherpad = (pad == space->sinkpad) ? space->srcpad : space->sinkpad;
396 link_ret = gst_pad_try_set_caps (otherpad, caps);
397 if (link_ret == GST_PAD_LINK_OK) {
401 structure = gst_caps_get_structure (caps, 0);
403 format_index = gst_colorspace_get_format (caps);
404 g_print ("format index is %d\n", format_index);
406 gst_structure_get_int (structure, "width", &width);
407 gst_structure_get_int (structure, "height", &height);
408 gst_structure_get_double (structure, "framerate", &fps);
410 GST_INFO ("size: %dx%d", space->width, space->height);
412 if (gst_pad_is_negotiated (otherpad)) {
415 othercaps = gst_caps_copy (gst_pad_get_negotiated_caps (otherpad));
417 gst_caps_set_simple (othercaps,
418 "width", G_TYPE_INT, width,
419 "height", G_TYPE_INT, height, "framerate", G_TYPE_DOUBLE, fps, NULL);
421 link_ret = gst_pad_try_set_caps (otherpad, othercaps);
422 if (link_ret != GST_PAD_LINK_OK) {
427 if (pad == space->srcpad) {
428 space->src_format_index = format_index;
430 space->sink_format_index = format_index;
433 if (gst_pad_is_negotiated (otherpad)) {
434 space->converter_index =
435 gst_colorspace_get_converter (space->sink_format_index,
436 space->src_format_index);
438 g_print ("using index %d\n", space->converter_index);
440 space->sink_size = gst_colorspace_format_get_size (space->sink_format_index,
442 space->src_size = gst_colorspace_format_get_size (space->src_format_index,
444 space->width = width;
445 space->height = height;
449 if (gst_pad_is_negotiated (otherpad)) {
450 g_warning ("could not get converter\n");
451 return GST_PAD_LINK_REFUSED;
455 return GST_PAD_LINK_OK;
459 gst_colorspace_get_type (void)
461 static GType colorspace_type = 0;
463 if (!colorspace_type) {
464 static const GTypeInfo colorspace_info = {
465 sizeof (GstColorspaceClass),
466 gst_colorspace_base_init,
468 (GClassInitFunc) gst_colorspace_class_init,
471 sizeof (GstColorspace),
473 (GInstanceInitFunc) gst_colorspace_init,
477 g_type_register_static (GST_TYPE_ELEMENT, "GstColorspace",
478 &colorspace_info, 0);
480 return colorspace_type;
484 gst_colorspace_base_init (gpointer g_class)
486 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
488 gst_element_class_add_pad_template (element_class,
489 gst_static_pad_template_get (&gst_colorspace_src_template));
490 gst_element_class_add_pad_template (element_class,
491 gst_static_pad_template_get (&gst_colorspace_sink_template));
493 gst_element_class_set_details (element_class, &colorspace_details);
497 gst_colorspace_class_init (GstColorspaceClass * klass)
499 GObjectClass *gobject_class;
500 GstElementClass *gstelement_class;
502 gobject_class = (GObjectClass *) klass;
503 gstelement_class = (GstElementClass *) klass;
505 parent_class = g_type_class_peek_parent (klass);
507 gobject_class->set_property = gst_colorspace_set_property;
508 gobject_class->get_property = gst_colorspace_get_property;
510 gstelement_class->change_state = gst_colorspace_change_state;
512 gst_colorspace_table_init (NULL);
516 gst_colorspace_init (GstColorspace * space)
519 gst_pad_new_from_static_template (&gst_colorspace_sink_template, "sink");
520 gst_pad_set_link_function (space->sinkpad, gst_colorspace_link);
521 gst_pad_set_getcaps_function (space->sinkpad, gst_colorspace_getcaps);
522 gst_pad_set_chain_function (space->sinkpad, gst_colorspace_chain);
523 gst_element_add_pad (GST_ELEMENT (space), space->sinkpad);
526 gst_pad_new_from_static_template (&gst_colorspace_src_template, "src");
527 gst_element_add_pad (GST_ELEMENT (space), space->srcpad);
528 gst_pad_set_link_function (space->srcpad, gst_colorspace_link);
532 gst_colorspace_chain (GstPad * pad, GstData * _data)
534 GstBuffer *buf = GST_BUFFER (_data);
535 GstColorspace *space;
536 GstBuffer *outbuf = NULL;
537 GstColorspaceConverter *converter;
539 g_return_if_fail (pad != NULL);
540 g_return_if_fail (GST_IS_PAD (pad));
541 g_return_if_fail (buf != NULL);
543 space = GST_COLORSPACE (gst_pad_get_parent (pad));
545 g_return_if_fail (space != NULL);
546 g_return_if_fail (GST_IS_COLORSPACE (space));
548 if (GST_BUFFER_SIZE (buf) < space->sink_size) {
549 g_critical ("input size is smaller than expected");
553 gst_pad_alloc_buffer_and_set_caps (space->srcpad, GST_BUFFER_OFFSET_NONE,
556 converter = gst_colorspace_converters + space->converter_index;
557 converter->convert (space, GST_BUFFER_DATA (outbuf), GST_BUFFER_DATA (buf));
559 GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buf);
560 GST_BUFFER_DURATION (outbuf) = GST_BUFFER_DURATION (buf);
562 gst_buffer_unref (buf);
563 gst_pad_push (space->srcpad, GST_DATA (outbuf));
566 static GstStateChangeReturn
567 gst_colorspace_change_state (GstElement * element, GstStateChange transition)
569 GstColorspace *space;
571 space = GST_COLORSPACE (element);
573 switch (transition) {
574 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
576 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
578 case GST_STATE_CHANGE_PAUSED_TO_READY:
582 return parent_class->change_state (element, transition);
586 gst_colorspace_set_property (GObject * object, guint prop_id,
587 const GValue * value, GParamSpec * pspec)
589 GstColorspace *space;
591 g_return_if_fail (GST_IS_COLORSPACE (object));
592 space = GST_COLORSPACE (object);
601 gst_colorspace_get_property (GObject * object, guint prop_id, GValue * value,
604 GstColorspace *space;
606 g_return_if_fail (GST_IS_COLORSPACE (object));
607 space = GST_COLORSPACE (object);
611 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
617 plugin_init (GstPlugin * plugin)
619 if (!gst_element_register (plugin, "colorspace", GST_RANK_PRIMARY - 1,
620 GST_TYPE_COLORSPACE))
626 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
628 "yuvtorgbcolorspace",
629 "YUV to RGB colorspace converter",
630 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)