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 GstElementDetails colorspace_details =
48 GST_ELEMENT_DETAILS ("Colorspace converter",
49 "Filter/Converter/Video",
50 "Converts video from one colorspace to another",
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 GstElementStateReturn gst_colorspace_change_state (GstElement * element);
97 static GstElementClass *parent_class = NULL;
99 /*static guint gst_colorspace_signals[LAST_SIGNAL] = { 0 }; */
103 colorspace_setup_converter (GstColorspace * space, GstCaps * from_caps,
106 guint32 from_space, to_space;
107 GstStructure *from_struct;
108 GstStructure *to_struct;
110 g_return_val_if_fail (to_caps != NULL, FALSE);
111 g_return_val_if_fail (from_caps != NULL, FALSE);
113 from_struct = gst_caps_get_structure (from_caps, 0);
114 to_struct = gst_caps_get_structure (to_caps, 0);
116 from_space = GST_MAKE_FOURCC ('R', 'G', 'B', ' ');
117 gst_structure_get_fourcc (from_struct, "format", &from_space);
119 to_space = GST_MAKE_FOURCC ('R', 'G', 'B', ' ');
120 gst_structure_get_fourcc (to_struct, "format", &to_space);
122 GST_INFO ("set up converter for " GST_FOURCC_FORMAT
123 " (%08x) to " GST_FOURCC_FORMAT " (%08x)",
124 GST_FOURCC_ARGS (from_space), from_space,
125 GST_FOURCC_ARGS (to_space), to_space);
127 switch (from_space) {
128 case GST_MAKE_FOURCC ('R', 'G', 'B', ' '):
132 gst_structure_get_int (from_struct, "bpp", &from_bpp);
135 case GST_MAKE_FOURCC ('R', 'G', 'B', ' '):
140 gst_structure_get_int (to_struct, "bpp", &to_bpp);
142 gst_structure_get_int (from_struct, "red_mask", &space->source.r);
143 gst_structure_get_int (from_struct, "green_mask", &space->source.g);
144 gst_structure_get_int (from_struct, "blue_mask", &space->source.b);
146 space->srcbpp = space->source.bits = from_bpp;
147 space->source.indexed = 0;
148 space->source.has_colorkey = 0;
150 GST_INFO ("source red mask %08x", space->source.r);
151 GST_INFO ("source green mask %08x", space->source.g);
152 GST_INFO ("source blue mask %08x", space->source.b);
153 GST_INFO ("source bpp %08x", space->srcbpp);
155 gst_structure_get_int (to_struct, "red_mask", &space->dest.r);
156 gst_structure_get_int (to_struct, "green_mask", &space->dest.g);
157 gst_structure_get_int (to_struct, "blue_mask", &space->dest.b);
159 space->destbpp = space->dest.bits = to_bpp;
160 space->dest.indexed = 0;
161 space->dest.has_colorkey = 0;
163 GST_INFO ("dest red mask %08x", space->dest.r);
164 GST_INFO ("dest green mask %08x", space->dest.g);
165 GST_INFO ("dest blue mask %08x", space->dest.b);
166 GST_INFO ("dest bpp %08x", space->destbpp);
168 if (!Hermes_ConverterRequest (space->h_handle, &space->source,
170 g_warning ("Hermes: could not get converter\n");
173 GST_INFO ("converter set up");
174 space->type = GST_COLORSPACE_HERMES;
178 g_warning ("colorspace: compiled without hermes!");
181 case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
182 if (from_bpp == 32) {
183 space->type = GST_COLORSPACE_RGB32_YV12;
187 case GST_MAKE_FOURCC ('I', '4', '2', '0'):
188 if (from_bpp == 32) {
189 space->type = GST_COLORSPACE_RGB32_I420;
193 case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
194 GST_INFO ("colorspace: RGB to YUV with bpp %d not implemented!!",
200 case GST_MAKE_FOURCC ('I', '4', '2', '0'):
202 case GST_MAKE_FOURCC ('R', 'G', 'B', ' '):
203 GST_INFO ("colorspace: YUV to RGB");
205 gst_structure_get_int (to_struct, "bpp", &space->destbpp);
207 gst_colorspace_yuv2rgb_get_converter (from_caps, to_caps);
208 space->type = GST_COLORSPACE_YUV_RGB;
210 case GST_MAKE_FOURCC ('I', '4', '2', '0'):
211 space->type = GST_COLORSPACE_NONE;
214 case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
215 space->type = GST_COLORSPACE_420_SWAP;
221 case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
223 case GST_MAKE_FOURCC ('I', '4', '2', '0'):
224 space->type = GST_COLORSPACE_YUY2_I420;
227 case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
228 space->type = GST_COLORSPACE_NONE;
231 case GST_MAKE_FOURCC ('R', 'G', 'B', ' '):
232 GST_INFO ("colorspace: YUY2 to RGB not implemented!!");
236 case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
238 case GST_MAKE_FOURCC ('R', 'G', 'B', ' '):
239 GST_INFO ("colorspace: YV12 to RGB");
241 gst_structure_get_int (to_struct, "bpp", &space->destbpp);
243 gst_colorspace_yuv2rgb_get_converter (from_caps, to_caps);
244 space->type = GST_COLORSPACE_YUV_RGB;
246 case GST_MAKE_FOURCC ('I', '4', '2', '0'):
247 space->type = GST_COLORSPACE_420_SWAP;
250 case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
251 space->type = GST_COLORSPACE_NONE;
262 gst_colorspace_caps_remove_format_info (GstCaps * caps, const char *media_type)
265 GstStructure *structure;
267 for (i = 0; i < gst_caps_get_size (caps); i++) {
268 structure = gst_caps_get_structure (caps, i);
270 gst_structure_set_name (structure, media_type);
271 gst_structure_remove_field (structure, "format");
272 gst_structure_remove_field (structure, "endianness");
273 gst_structure_remove_field (structure, "depth");
274 gst_structure_remove_field (structure, "bpp");
275 gst_structure_remove_field (structure, "red_mask");
276 gst_structure_remove_field (structure, "green_mask");
277 gst_structure_remove_field (structure, "blue_mask");
280 gst_caps_do_simplify (caps);
285 gst_colorspace_getcaps (GstPad * pad)
287 GstColorspace *space;
292 space = GST_COLORSPACE (gst_pad_get_parent (pad));
294 otherpad = (pad == space->srcpad) ? space->sinkpad : space->srcpad;
296 othercaps = gst_pad_get_allowed_caps (otherpad);
298 othercaps = gst_colorspace_caps_remove_format_info (othercaps,
299 (pad == space->srcpad) ? "video/x-raw-rgb" : "video/x-raw-yuv");
301 caps = gst_caps_intersect (othercaps, gst_pad_get_pad_template_caps (pad));
302 gst_caps_free (othercaps);
307 static GstColorSpaceFormatType
308 gst_colorspace_get_format (const GstCaps * caps)
312 for (i = 0; i < G_N_ELEMENTS (gst_colorspace_formats); i++) {
317 gst_caps_copy (gst_static_caps_get (&gst_colorspace_formats[i].caps));
319 icaps = gst_caps_intersect (caps, fcaps);
320 if (!gst_caps_is_empty (icaps)) {
321 gst_caps_free (icaps);
324 gst_caps_free (icaps);
327 g_assert_not_reached ();
331 #define ROUND_UP_2(x) (((x)+1)&~1)
332 #define ROUND_UP_4(x) (((x)+3)&~3)
333 #define ROUND_UP_8(x) (((x)+7)&~7)
336 gst_colorspace_format_get_size (GstColorSpaceFormatType index, int width,
342 case GST_COLORSPACE_I420:
343 case GST_COLORSPACE_YV12:
344 size = ROUND_UP_4 (width) * ROUND_UP_2 (height);
345 size += ROUND_UP_8 (width) / 2 * ROUND_UP_2 (height) / 2;
346 size += ROUND_UP_8 (width) / 2 * ROUND_UP_2 (height) / 2;
349 case GST_COLORSPACE_RGB32:
350 return width * height * 4;
352 case GST_COLORSPACE_RGB24:
353 return ROUND_UP_4 (width * 3) * height;
355 case GST_COLORSPACE_RGB16:
356 return ROUND_UP_4 (width * 2) * height;
360 g_assert_not_reached ();
365 gst_colorspace_get_converter (GstColorSpaceFormatType from,
366 GstColorSpaceFormatType to)
370 for (i = 0; i < G_N_ELEMENTS (gst_colorspace_converters); i++) {
371 GstColorspaceConverter *converter = gst_colorspace_converters + i;
373 if (from == converter->from && to == converter->to) {
377 g_assert_not_reached ();
381 static GstPadLinkReturn
382 gst_colorspace_link (GstPad * pad, const GstCaps * caps)
384 GstColorspace *space;
386 GstStructure *structure;
387 GstPadLinkReturn link_ret;
392 space = GST_COLORSPACE (gst_pad_get_parent (pad));
393 otherpad = (pad == space->sinkpad) ? space->srcpad : space->sinkpad;
395 link_ret = gst_pad_try_set_caps (otherpad, caps);
396 if (link_ret == GST_PAD_LINK_OK) {
400 structure = gst_caps_get_structure (caps, 0);
402 format_index = gst_colorspace_get_format (caps);
403 g_print ("format index is %d\n", format_index);
405 gst_structure_get_int (structure, "width", &width);
406 gst_structure_get_int (structure, "height", &height);
407 gst_structure_get_double (structure, "framerate", &fps);
409 GST_INFO ("size: %dx%d", space->width, space->height);
411 if (gst_pad_is_negotiated (otherpad)) {
414 othercaps = gst_caps_copy (gst_pad_get_negotiated_caps (otherpad));
416 gst_caps_set_simple (othercaps,
417 "width", G_TYPE_INT, width,
418 "height", G_TYPE_INT, height, "framerate", G_TYPE_DOUBLE, fps, NULL);
420 link_ret = gst_pad_try_set_caps (otherpad, othercaps);
421 if (link_ret != GST_PAD_LINK_OK) {
426 if (pad == space->srcpad) {
427 space->src_format_index = format_index;
429 space->sink_format_index = format_index;
432 if (gst_pad_is_negotiated (otherpad)) {
433 space->converter_index =
434 gst_colorspace_get_converter (space->sink_format_index,
435 space->src_format_index);
437 g_print ("using index %d\n", space->converter_index);
439 space->sink_size = gst_colorspace_format_get_size (space->sink_format_index,
441 space->src_size = gst_colorspace_format_get_size (space->src_format_index,
443 space->width = width;
444 space->height = height;
448 if (gst_pad_is_negotiated (otherpad)) {
449 g_warning ("could not get converter\n");
450 return GST_PAD_LINK_REFUSED;
454 return GST_PAD_LINK_OK;
458 gst_colorspace_get_type (void)
460 static GType colorspace_type = 0;
462 if (!colorspace_type) {
463 static const GTypeInfo colorspace_info = {
464 sizeof (GstColorspaceClass),
465 gst_colorspace_base_init,
467 (GClassInitFunc) gst_colorspace_class_init,
470 sizeof (GstColorspace),
472 (GInstanceInitFunc) gst_colorspace_init,
476 g_type_register_static (GST_TYPE_ELEMENT, "GstColorspace",
477 &colorspace_info, 0);
479 return colorspace_type;
483 gst_colorspace_base_init (gpointer g_class)
485 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
487 gst_element_class_add_pad_template (element_class,
488 gst_static_pad_template_get (&gst_colorspace_src_template));
489 gst_element_class_add_pad_template (element_class,
490 gst_static_pad_template_get (&gst_colorspace_sink_template));
492 gst_element_class_set_details (element_class, &colorspace_details);
496 gst_colorspace_class_init (GstColorspaceClass * klass)
498 GObjectClass *gobject_class;
499 GstElementClass *gstelement_class;
501 gobject_class = (GObjectClass *) klass;
502 gstelement_class = (GstElementClass *) klass;
504 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
506 gobject_class->set_property = gst_colorspace_set_property;
507 gobject_class->get_property = gst_colorspace_get_property;
509 gstelement_class->change_state = gst_colorspace_change_state;
511 gst_colorspace_table_init (NULL);
515 gst_colorspace_init (GstColorspace * space)
518 gst_pad_new_from_template (gst_static_pad_template_get
519 (&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_template (gst_static_pad_template_get
527 (&gst_colorspace_src_template), "src");
528 gst_element_add_pad (GST_ELEMENT (space), space->srcpad);
529 gst_pad_set_link_function (space->srcpad, gst_colorspace_link);
533 gst_colorspace_chain (GstPad * pad, GstData * _data)
535 GstBuffer *buf = GST_BUFFER (_data);
536 GstColorspace *space;
537 GstBuffer *outbuf = NULL;
538 GstColorspaceConverter *converter;
540 g_return_if_fail (pad != NULL);
541 g_return_if_fail (GST_IS_PAD (pad));
542 g_return_if_fail (buf != NULL);
544 space = GST_COLORSPACE (gst_pad_get_parent (pad));
546 g_return_if_fail (space != NULL);
547 g_return_if_fail (GST_IS_COLORSPACE (space));
549 if (GST_BUFFER_SIZE (buf) < space->sink_size) {
550 g_critical ("input size is smaller than expected");
553 outbuf = gst_pad_alloc_buffer (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 GstElementStateReturn
567 gst_colorspace_change_state (GstElement * element)
569 GstColorspace *space;
571 space = GST_COLORSPACE (element);
573 switch (GST_STATE_TRANSITION (element)) {
574 case GST_STATE_PAUSED_TO_PLAYING:
576 case GST_STATE_PLAYING_TO_PAUSED:
578 case GST_STATE_PAUSED_TO_READY:
582 return parent_class->change_state (element);
586 gst_colorspace_set_property (GObject * object, guint prop_id,
587 const GValue * value, GParamSpec * pspec)
589 GstColorspace *space;
591 /* it's not null if we got it, but it might not be ours */
592 g_return_if_fail (GST_IS_COLORSPACE (object));
593 space = GST_COLORSPACE (object);
602 gst_colorspace_get_property (GObject * object, guint prop_id, GValue * value,
605 GstColorspace *space;
607 /* it's not null if we got it, but it might not be ours */
608 g_return_if_fail (GST_IS_COLORSPACE (object));
609 space = GST_COLORSPACE (object);
613 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
619 plugin_init (GstPlugin * plugin)
621 if (!gst_element_register (plugin, "colorspace", GST_RANK_PRIMARY - 1,
622 GST_TYPE_COLORSPACE))
628 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
631 "internal colorspace converter",
632 plugin_init, VERSION, "LGPL", GST_PACKAGE, GST_ORIGIN)