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 GstStaticPadTemplate gst_colorspace_sink_template =
48 GST_STATIC_PAD_TEMPLATE ("sink",
51 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("{ I420, YV12 }"))
54 static GstStaticPadTemplate gst_colorspace_src_template =
55 GST_STATIC_PAD_TEMPLATE ("src",
58 GST_STATIC_CAPS (GST_VIDEO_CAPS_xRGB "; "
59 GST_VIDEO_CAPS_RGB "; " GST_VIDEO_CAPS_RGB_16)
62 /* Stereo signals and args */
76 static void gst_colorspace_base_init (gpointer g_class);
77 static void gst_colorspace_class_init (GstColorspaceClass * klass);
78 static void gst_colorspace_init (GstColorspace * space);
80 static void gst_colorspace_set_property (GObject * object, guint prop_id,
81 const GValue * value, GParamSpec * pspec);
82 static void gst_colorspace_get_property (GObject * object, guint prop_id,
83 GValue * value, GParamSpec * pspec);
85 static GstPadLinkReturn
86 gst_colorspace_link (GstPad * pad, const GstCaps * caps);
87 static void gst_colorspace_chain (GstPad * pad, GstData * _data);
88 static GstStateChangeReturn gst_colorspace_change_state (GstElement * element,
89 GstStateChange transition);
92 static GstElementClass *parent_class = NULL;
94 /*static guint gst_colorspace_signals[LAST_SIGNAL] = { 0 }; */
98 colorspace_setup_converter (GstColorspace * space, GstCaps * from_caps,
101 guint32 from_space, to_space;
102 GstStructure *from_struct;
103 GstStructure *to_struct;
105 g_return_val_if_fail (to_caps != NULL, FALSE);
106 g_return_val_if_fail (from_caps != NULL, FALSE);
108 from_struct = gst_caps_get_structure (from_caps, 0);
109 to_struct = gst_caps_get_structure (to_caps, 0);
111 from_space = GST_MAKE_FOURCC ('R', 'G', 'B', ' ');
112 gst_structure_get_fourcc (from_struct, "format", &from_space);
114 to_space = GST_MAKE_FOURCC ('R', 'G', 'B', ' ');
115 gst_structure_get_fourcc (to_struct, "format", &to_space);
117 GST_INFO ("set up converter for " GST_FOURCC_FORMAT
118 " (%08x) to " GST_FOURCC_FORMAT " (%08x)",
119 GST_FOURCC_ARGS (from_space), from_space,
120 GST_FOURCC_ARGS (to_space), to_space);
122 switch (from_space) {
123 case GST_MAKE_FOURCC ('R', 'G', 'B', ' '):
127 gst_structure_get_int (from_struct, "bpp", &from_bpp);
130 case GST_MAKE_FOURCC ('R', 'G', 'B', ' '):
135 gst_structure_get_int (to_struct, "bpp", &to_bpp);
137 gst_structure_get_int (from_struct, "red_mask", &space->source.r);
138 gst_structure_get_int (from_struct, "green_mask", &space->source.g);
139 gst_structure_get_int (from_struct, "blue_mask", &space->source.b);
141 space->srcbpp = space->source.bits = from_bpp;
142 space->source.indexed = 0;
143 space->source.has_colorkey = 0;
145 GST_INFO ("source red mask %08x", space->source.r);
146 GST_INFO ("source green mask %08x", space->source.g);
147 GST_INFO ("source blue mask %08x", space->source.b);
148 GST_INFO ("source bpp %08x", space->srcbpp);
150 gst_structure_get_int (to_struct, "red_mask", &space->dest.r);
151 gst_structure_get_int (to_struct, "green_mask", &space->dest.g);
152 gst_structure_get_int (to_struct, "blue_mask", &space->dest.b);
154 space->destbpp = space->dest.bits = to_bpp;
155 space->dest.indexed = 0;
156 space->dest.has_colorkey = 0;
158 GST_INFO ("dest red mask %08x", space->dest.r);
159 GST_INFO ("dest green mask %08x", space->dest.g);
160 GST_INFO ("dest blue mask %08x", space->dest.b);
161 GST_INFO ("dest bpp %08x", space->destbpp);
163 if (!Hermes_ConverterRequest (space->h_handle, &space->source,
165 g_warning ("Hermes: could not get converter\n");
168 GST_INFO ("converter set up");
169 space->type = GST_COLORSPACE_HERMES;
173 g_warning ("colorspace: compiled without hermes!");
176 case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
177 if (from_bpp == 32) {
178 space->type = GST_COLORSPACE_RGB32_YV12;
182 case GST_MAKE_FOURCC ('I', '4', '2', '0'):
183 if (from_bpp == 32) {
184 space->type = GST_COLORSPACE_RGB32_I420;
188 case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
189 GST_INFO ("colorspace: RGB to YUV with bpp %d not implemented!!",
195 case GST_MAKE_FOURCC ('I', '4', '2', '0'):
197 case GST_MAKE_FOURCC ('R', 'G', 'B', ' '):
198 GST_INFO ("colorspace: YUV to RGB");
200 gst_structure_get_int (to_struct, "bpp", &space->destbpp);
202 gst_colorspace_yuv2rgb_get_converter (from_caps, to_caps);
203 space->type = GST_COLORSPACE_YUV_RGB;
205 case GST_MAKE_FOURCC ('I', '4', '2', '0'):
206 space->type = GST_COLORSPACE_NONE;
209 case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
210 space->type = GST_COLORSPACE_420_SWAP;
216 case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
218 case GST_MAKE_FOURCC ('I', '4', '2', '0'):
219 space->type = GST_COLORSPACE_YUY2_I420;
222 case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
223 space->type = GST_COLORSPACE_NONE;
226 case GST_MAKE_FOURCC ('R', 'G', 'B', ' '):
227 GST_INFO ("colorspace: YUY2 to RGB not implemented!!");
231 case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
233 case GST_MAKE_FOURCC ('R', 'G', 'B', ' '):
234 GST_INFO ("colorspace: YV12 to RGB");
236 gst_structure_get_int (to_struct, "bpp", &space->destbpp);
238 gst_colorspace_yuv2rgb_get_converter (from_caps, to_caps);
239 space->type = GST_COLORSPACE_YUV_RGB;
241 case GST_MAKE_FOURCC ('I', '4', '2', '0'):
242 space->type = GST_COLORSPACE_420_SWAP;
245 case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
246 space->type = GST_COLORSPACE_NONE;
257 gst_colorspace_caps_remove_format_info (GstCaps * caps, const char *media_type)
260 GstStructure *structure;
262 for (i = 0; i < gst_caps_get_size (caps); i++) {
263 structure = gst_caps_get_structure (caps, i);
265 gst_structure_set_name (structure, media_type);
266 gst_structure_remove_field (structure, "format");
267 gst_structure_remove_field (structure, "endianness");
268 gst_structure_remove_field (structure, "depth");
269 gst_structure_remove_field (structure, "bpp");
270 gst_structure_remove_field (structure, "red_mask");
271 gst_structure_remove_field (structure, "green_mask");
272 gst_structure_remove_field (structure, "blue_mask");
275 gst_caps_do_simplify (caps);
280 gst_colorspace_getcaps (GstPad * pad)
282 GstColorspace *space;
287 space = GST_COLORSPACE (gst_pad_get_parent (pad));
289 otherpad = (pad == space->srcpad) ? space->sinkpad : space->srcpad;
291 othercaps = gst_pad_get_allowed_caps (otherpad);
293 othercaps = gst_colorspace_caps_remove_format_info (othercaps,
294 (pad == space->srcpad) ? "video/x-raw-rgb" : "video/x-raw-yuv");
296 caps = gst_caps_intersect (othercaps, gst_pad_get_pad_template_caps (pad));
297 gst_caps_free (othercaps);
302 static GstColorSpaceFormatType
303 gst_colorspace_get_format (const GstCaps * caps)
307 for (i = 0; i < G_N_ELEMENTS (gst_colorspace_formats); i++) {
312 gst_caps_copy (gst_static_caps_get (&gst_colorspace_formats[i].caps));
314 icaps = gst_caps_intersect (caps, fcaps);
315 if (!gst_caps_is_empty (icaps)) {
316 gst_caps_free (icaps);
319 gst_caps_free (icaps);
322 g_assert_not_reached ();
326 #define ROUND_UP_2(x) (((x)+1)&~1)
327 #define ROUND_UP_4(x) (((x)+3)&~3)
328 #define ROUND_UP_8(x) (((x)+7)&~7)
331 gst_colorspace_format_get_size (GstColorSpaceFormatType index, int width,
337 case GST_COLORSPACE_I420:
338 case GST_COLORSPACE_YV12:
339 size = ROUND_UP_4 (width) * ROUND_UP_2 (height);
340 size += ROUND_UP_8 (width) / 2 * ROUND_UP_2 (height) / 2;
341 size += ROUND_UP_8 (width) / 2 * ROUND_UP_2 (height) / 2;
344 case GST_COLORSPACE_RGB32:
345 return width * height * 4;
347 case GST_COLORSPACE_RGB24:
348 return ROUND_UP_4 (width * 3) * height;
350 case GST_COLORSPACE_RGB16:
351 return ROUND_UP_4 (width * 2) * height;
355 g_assert_not_reached ();
360 gst_colorspace_get_converter (GstColorSpaceFormatType from,
361 GstColorSpaceFormatType to)
365 for (i = 0; i < G_N_ELEMENTS (gst_colorspace_converters); i++) {
366 GstColorspaceConverter *converter = gst_colorspace_converters + i;
368 if (from == converter->from && to == converter->to) {
372 g_assert_not_reached ();
376 static GstPadLinkReturn
377 gst_colorspace_link (GstPad * pad, const GstCaps * caps)
379 GstColorspace *space;
381 GstStructure *structure;
382 GstPadLinkReturn link_ret;
387 space = GST_COLORSPACE (gst_pad_get_parent (pad));
388 otherpad = (pad == space->sinkpad) ? space->srcpad : space->sinkpad;
390 link_ret = gst_pad_try_set_caps (otherpad, caps);
391 if (link_ret == GST_PAD_LINK_OK) {
395 structure = gst_caps_get_structure (caps, 0);
397 format_index = gst_colorspace_get_format (caps);
398 g_print ("format index is %d\n", format_index);
400 gst_structure_get_int (structure, "width", &width);
401 gst_structure_get_int (structure, "height", &height);
402 gst_structure_get_double (structure, "framerate", &fps);
404 GST_INFO ("size: %dx%d", space->width, space->height);
406 if (gst_pad_is_negotiated (otherpad)) {
409 othercaps = gst_caps_copy (gst_pad_get_negotiated_caps (otherpad));
411 gst_caps_set_simple (othercaps,
412 "width", G_TYPE_INT, width,
413 "height", G_TYPE_INT, height, "framerate", G_TYPE_DOUBLE, fps, NULL);
415 link_ret = gst_pad_try_set_caps (otherpad, othercaps);
416 if (link_ret != GST_PAD_LINK_OK) {
421 if (pad == space->srcpad) {
422 space->src_format_index = format_index;
424 space->sink_format_index = format_index;
427 if (gst_pad_is_negotiated (otherpad)) {
428 space->converter_index =
429 gst_colorspace_get_converter (space->sink_format_index,
430 space->src_format_index);
432 g_print ("using index %d\n", space->converter_index);
434 space->sink_size = gst_colorspace_format_get_size (space->sink_format_index,
436 space->src_size = gst_colorspace_format_get_size (space->src_format_index,
438 space->width = width;
439 space->height = height;
443 if (gst_pad_is_negotiated (otherpad)) {
444 g_warning ("could not get converter\n");
445 return GST_PAD_LINK_REFUSED;
449 return GST_PAD_LINK_OK;
453 gst_colorspace_get_type (void)
455 static GType colorspace_type = 0;
457 if (!colorspace_type) {
458 static const GTypeInfo colorspace_info = {
459 sizeof (GstColorspaceClass),
460 gst_colorspace_base_init,
462 (GClassInitFunc) gst_colorspace_class_init,
465 sizeof (GstColorspace),
467 (GInstanceInitFunc) gst_colorspace_init,
471 g_type_register_static (GST_TYPE_ELEMENT, "GstColorspace",
472 &colorspace_info, 0);
474 return colorspace_type;
478 gst_colorspace_base_init (gpointer g_class)
480 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
482 gst_element_class_add_pad_template (element_class,
483 gst_static_pad_template_get (&gst_colorspace_src_template));
484 gst_element_class_add_pad_template (element_class,
485 gst_static_pad_template_get (&gst_colorspace_sink_template));
487 gst_element_class_set_details_simple (element_class, "Colorspace converter",
488 "Filter/Converter/Video",
489 "Converts video from YUV to RGB", "Wim Taymans <wim.taymans@chello.be>");
493 gst_colorspace_class_init (GstColorspaceClass * klass)
495 GObjectClass *gobject_class;
496 GstElementClass *gstelement_class;
498 gobject_class = (GObjectClass *) klass;
499 gstelement_class = (GstElementClass *) klass;
501 parent_class = g_type_class_peek_parent (klass);
503 gobject_class->set_property = gst_colorspace_set_property;
504 gobject_class->get_property = gst_colorspace_get_property;
506 gstelement_class->change_state = gst_colorspace_change_state;
508 gst_colorspace_table_init (NULL);
512 gst_colorspace_init (GstColorspace * space)
515 gst_pad_new_from_static_template (&gst_colorspace_sink_template, "sink");
516 gst_pad_set_link_function (space->sinkpad, gst_colorspace_link);
517 gst_pad_set_getcaps_function (space->sinkpad, gst_colorspace_getcaps);
518 gst_pad_set_chain_function (space->sinkpad, gst_colorspace_chain);
519 gst_element_add_pad (GST_ELEMENT (space), space->sinkpad);
522 gst_pad_new_from_static_template (&gst_colorspace_src_template, "src");
523 gst_element_add_pad (GST_ELEMENT (space), space->srcpad);
524 gst_pad_set_link_function (space->srcpad, gst_colorspace_link);
528 gst_colorspace_chain (GstPad * pad, GstData * _data)
530 GstBuffer *buf = GST_BUFFER (_data);
531 GstColorspace *space;
532 GstBuffer *outbuf = NULL;
533 GstColorspaceConverter *converter;
535 g_return_if_fail (pad != NULL);
536 g_return_if_fail (GST_IS_PAD (pad));
537 g_return_if_fail (buf != NULL);
539 space = GST_COLORSPACE (gst_pad_get_parent (pad));
541 g_return_if_fail (space != NULL);
542 g_return_if_fail (GST_IS_COLORSPACE (space));
544 if (GST_BUFFER_SIZE (buf) < space->sink_size) {
545 g_critical ("input size is smaller than expected");
549 gst_pad_alloc_buffer_and_set_caps (space->srcpad, GST_BUFFER_OFFSET_NONE,
552 converter = gst_colorspace_converters + space->converter_index;
553 converter->convert (space, GST_BUFFER_DATA (outbuf), GST_BUFFER_DATA (buf));
555 GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buf);
556 GST_BUFFER_DURATION (outbuf) = GST_BUFFER_DURATION (buf);
558 gst_buffer_unref (buf);
559 gst_pad_push (space->srcpad, GST_DATA (outbuf));
562 static GstStateChangeReturn
563 gst_colorspace_change_state (GstElement * element, GstStateChange transition)
565 GstColorspace *space;
567 space = GST_COLORSPACE (element);
569 switch (transition) {
570 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
572 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
574 case GST_STATE_CHANGE_PAUSED_TO_READY:
578 return parent_class->change_state (element, transition);
582 gst_colorspace_set_property (GObject * object, guint prop_id,
583 const GValue * value, GParamSpec * pspec)
585 GstColorspace *space;
587 g_return_if_fail (GST_IS_COLORSPACE (object));
588 space = GST_COLORSPACE (object);
597 gst_colorspace_get_property (GObject * object, guint prop_id, GValue * value,
600 GstColorspace *space;
602 g_return_if_fail (GST_IS_COLORSPACE (object));
603 space = GST_COLORSPACE (object);
607 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
613 plugin_init (GstPlugin * plugin)
615 if (!gst_element_register (plugin, "colorspace", GST_RANK_PRIMARY - 1,
616 GST_TYPE_COLORSPACE))
622 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
624 "yuvtorgbcolorspace",
625 "YUV to RGB colorspace converter",
626 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)