[MOVED FROM BAD 30/68] gst_element_class_set_details => gst_element_class_set_details...
[platform/upstream/gstreamer.git] / gst / colorspace / gstcolorspace.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 #include "gstcolorspace.h"
24 #include <gst/gst.h>
25 #include <gst/video/video.h>
26
27 #include "yuv2rgb.h"
28
29
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)},
36 };
37
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},
45 };
46
47 static GstStaticPadTemplate gst_colorspace_sink_template =
48 GST_STATIC_PAD_TEMPLATE ("sink",
49     GST_PAD_SINK,
50     GST_PAD_ALWAYS,
51     GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("{ I420, YV12 }"))
52     );
53
54 static GstStaticPadTemplate gst_colorspace_src_template =
55     GST_STATIC_PAD_TEMPLATE ("src",
56     GST_PAD_SRC,
57     GST_PAD_ALWAYS,
58     GST_STATIC_CAPS (GST_VIDEO_CAPS_xRGB "; "
59         GST_VIDEO_CAPS_RGB "; " GST_VIDEO_CAPS_RGB_16)
60     );
61
62 /* Stereo signals and args */
63 enum
64 {
65   /* FILL ME */
66   LAST_SIGNAL
67 };
68
69 enum
70 {
71   ARG_0,
72   ARG_SOURCE,
73   ARG_DEST
74 };
75
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);
79
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);
84
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);
90
91
92 static GstElementClass *parent_class = NULL;
93
94 /*static guint gst_colorspace_signals[LAST_SIGNAL] = { 0 }; */
95
96 #if 0
97 static gboolean
98 colorspace_setup_converter (GstColorspace * space, GstCaps * from_caps,
99     GstCaps * to_caps)
100 {
101   guint32 from_space, to_space;
102   GstStructure *from_struct;
103   GstStructure *to_struct;
104
105   g_return_val_if_fail (to_caps != NULL, FALSE);
106   g_return_val_if_fail (from_caps != NULL, FALSE);
107
108   from_struct = gst_caps_get_structure (from_caps, 0);
109   to_struct = gst_caps_get_structure (to_caps, 0);
110
111   from_space = GST_MAKE_FOURCC ('R', 'G', 'B', ' ');
112   gst_structure_get_fourcc (from_struct, "format", &from_space);
113
114   to_space = GST_MAKE_FOURCC ('R', 'G', 'B', ' ');
115   gst_structure_get_fourcc (to_struct, "format", &to_space);
116
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);
121
122   switch (from_space) {
123     case GST_MAKE_FOURCC ('R', 'G', 'B', ' '):
124     {
125       gint from_bpp;
126
127       gst_structure_get_int (from_struct, "bpp", &from_bpp);
128
129       switch (to_space) {
130         case GST_MAKE_FOURCC ('R', 'G', 'B', ' '):
131 #ifdef HAVE_HERMES
132         {
133           gint to_bpp;
134
135           gst_structure_get_int (to_struct, "bpp", &to_bpp);
136
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);
140           space->source.a = 0;
141           space->srcbpp = space->source.bits = from_bpp;
142           space->source.indexed = 0;
143           space->source.has_colorkey = 0;
144
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);
149
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);
153           space->dest.a = 0;
154           space->destbpp = space->dest.bits = to_bpp;
155           space->dest.indexed = 0;
156           space->dest.has_colorkey = 0;
157
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);
162
163           if (!Hermes_ConverterRequest (space->h_handle, &space->source,
164                   &space->dest)) {
165             g_warning ("Hermes: could not get converter\n");
166             return FALSE;
167           }
168           GST_INFO ("converter set up");
169           space->type = GST_COLORSPACE_HERMES;
170           return TRUE;
171         }
172 #else
173           g_warning ("colorspace: compiled without hermes!");
174           return FALSE;
175 #endif
176         case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
177           if (from_bpp == 32) {
178             space->type = GST_COLORSPACE_RGB32_YV12;
179             space->destbpp = 12;
180             return TRUE;
181           }
182         case GST_MAKE_FOURCC ('I', '4', '2', '0'):
183           if (from_bpp == 32) {
184             space->type = GST_COLORSPACE_RGB32_I420;
185             space->destbpp = 12;
186             return TRUE;
187           }
188         case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
189           GST_INFO ("colorspace: RGB to YUV with bpp %d not implemented!!",
190               from_bpp);
191           return FALSE;
192       }
193       break;
194     }
195     case GST_MAKE_FOURCC ('I', '4', '2', '0'):
196       switch (to_space) {
197         case GST_MAKE_FOURCC ('R', 'G', 'B', ' '):
198           GST_INFO ("colorspace: YUV to RGB");
199
200           gst_structure_get_int (to_struct, "bpp", &space->destbpp);
201           space->converter =
202               gst_colorspace_yuv2rgb_get_converter (from_caps, to_caps);
203           space->type = GST_COLORSPACE_YUV_RGB;
204           return TRUE;
205         case GST_MAKE_FOURCC ('I', '4', '2', '0'):
206           space->type = GST_COLORSPACE_NONE;
207           space->destbpp = 12;
208           return TRUE;
209         case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
210           space->type = GST_COLORSPACE_420_SWAP;
211           space->destbpp = 12;
212           return TRUE;
213
214       }
215       break;
216     case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
217       switch (to_space) {
218         case GST_MAKE_FOURCC ('I', '4', '2', '0'):
219           space->type = GST_COLORSPACE_YUY2_I420;
220           space->destbpp = 12;
221           return TRUE;
222         case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
223           space->type = GST_COLORSPACE_NONE;
224           space->destbpp = 16;
225           return TRUE;
226         case GST_MAKE_FOURCC ('R', 'G', 'B', ' '):
227           GST_INFO ("colorspace: YUY2 to RGB not implemented!!");
228           return FALSE;
229       }
230       break;
231     case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
232       switch (to_space) {
233         case GST_MAKE_FOURCC ('R', 'G', 'B', ' '):
234           GST_INFO ("colorspace: YV12 to RGB");
235
236           gst_structure_get_int (to_struct, "bpp", &space->destbpp);
237           space->converter =
238               gst_colorspace_yuv2rgb_get_converter (from_caps, to_caps);
239           space->type = GST_COLORSPACE_YUV_RGB;
240           return TRUE;
241         case GST_MAKE_FOURCC ('I', '4', '2', '0'):
242           space->type = GST_COLORSPACE_420_SWAP;
243           space->destbpp = 12;
244           return TRUE;
245         case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
246           space->type = GST_COLORSPACE_NONE;
247           space->destbpp = 12;
248           return TRUE;
249       }
250       break;
251   }
252   return FALSE;
253 }
254 #endif
255
256 static GstCaps *
257 gst_colorspace_caps_remove_format_info (GstCaps * caps, const char *media_type)
258 {
259   int i;
260   GstStructure *structure;
261
262   for (i = 0; i < gst_caps_get_size (caps); i++) {
263     structure = gst_caps_get_structure (caps, i);
264
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");
273   }
274
275   gst_caps_do_simplify (caps);
276   return caps;
277 }
278
279 static GstCaps *
280 gst_colorspace_getcaps (GstPad * pad)
281 {
282   GstColorspace *space;
283   GstPad *otherpad;
284   GstCaps *othercaps;
285   GstCaps *caps;
286
287   space = GST_COLORSPACE (gst_pad_get_parent (pad));
288
289   otherpad = (pad == space->srcpad) ? space->sinkpad : space->srcpad;
290
291   othercaps = gst_pad_get_allowed_caps (otherpad);
292
293   othercaps = gst_colorspace_caps_remove_format_info (othercaps,
294       (pad == space->srcpad) ? "video/x-raw-rgb" : "video/x-raw-yuv");
295
296   caps = gst_caps_intersect (othercaps, gst_pad_get_pad_template_caps (pad));
297   gst_caps_free (othercaps);
298
299   return caps;
300 }
301
302 static GstColorSpaceFormatType
303 gst_colorspace_get_format (const GstCaps * caps)
304 {
305   int i;
306
307   for (i = 0; i < G_N_ELEMENTS (gst_colorspace_formats); i++) {
308     GstCaps *icaps;
309     GstCaps *fcaps;
310
311     fcaps =
312         gst_caps_copy (gst_static_caps_get (&gst_colorspace_formats[i].caps));
313
314     icaps = gst_caps_intersect (caps, fcaps);
315     if (!gst_caps_is_empty (icaps)) {
316       gst_caps_free (icaps);
317       return i;
318     }
319     gst_caps_free (icaps);
320   }
321
322   g_assert_not_reached ();
323   return -1;
324 }
325
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)
329
330 static int
331 gst_colorspace_format_get_size (GstColorSpaceFormatType index, int width,
332     int height)
333 {
334   int size;
335
336   switch (index) {
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;
342       return size;
343       break;
344     case GST_COLORSPACE_RGB32:
345       return width * height * 4;
346       break;
347     case GST_COLORSPACE_RGB24:
348       return ROUND_UP_4 (width * 3) * height;
349       break;
350     case GST_COLORSPACE_RGB16:
351       return ROUND_UP_4 (width * 2) * height;
352       break;
353   }
354
355   g_assert_not_reached ();
356   return 0;
357 }
358
359 static int
360 gst_colorspace_get_converter (GstColorSpaceFormatType from,
361     GstColorSpaceFormatType to)
362 {
363   int i;
364
365   for (i = 0; i < G_N_ELEMENTS (gst_colorspace_converters); i++) {
366     GstColorspaceConverter *converter = gst_colorspace_converters + i;
367
368     if (from == converter->from && to == converter->to) {
369       return i;
370     }
371   }
372   g_assert_not_reached ();
373   return -1;
374 }
375
376 static GstPadLinkReturn
377 gst_colorspace_link (GstPad * pad, const GstCaps * caps)
378 {
379   GstColorspace *space;
380   GstPad *otherpad;
381   GstStructure *structure;
382   GstPadLinkReturn link_ret;
383   int width, height;
384   double fps;
385   int format_index;
386
387   space = GST_COLORSPACE (gst_pad_get_parent (pad));
388   otherpad = (pad == space->sinkpad) ? space->srcpad : space->sinkpad;
389
390   link_ret = gst_pad_try_set_caps (otherpad, caps);
391   if (link_ret == GST_PAD_LINK_OK) {
392     return link_ret;
393   }
394
395   structure = gst_caps_get_structure (caps, 0);
396
397   format_index = gst_colorspace_get_format (caps);
398   g_print ("format index is %d\n", format_index);
399
400   gst_structure_get_int (structure, "width", &width);
401   gst_structure_get_int (structure, "height", &height);
402   gst_structure_get_double (structure, "framerate", &fps);
403
404   GST_INFO ("size: %dx%d", space->width, space->height);
405
406   if (gst_pad_is_negotiated (otherpad)) {
407     GstCaps *othercaps;
408
409     othercaps = gst_caps_copy (gst_pad_get_negotiated_caps (otherpad));
410
411     gst_caps_set_simple (othercaps,
412         "width", G_TYPE_INT, width,
413         "height", G_TYPE_INT, height, "framerate", G_TYPE_DOUBLE, fps, NULL);
414
415     link_ret = gst_pad_try_set_caps (otherpad, othercaps);
416     if (link_ret != GST_PAD_LINK_OK) {
417       return link_ret;
418     }
419   }
420
421   if (pad == space->srcpad) {
422     space->src_format_index = format_index;
423   } else {
424     space->sink_format_index = format_index;
425   }
426
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);
431
432     g_print ("using index %d\n", space->converter_index);
433
434     space->sink_size = gst_colorspace_format_get_size (space->sink_format_index,
435         width, height);
436     space->src_size = gst_colorspace_format_get_size (space->src_format_index,
437         width, height);
438     space->width = width;
439     space->height = height;
440     space->fps = fps;
441   }
442 #if 0
443   if (gst_pad_is_negotiated (otherpad)) {
444     g_warning ("could not get converter\n");
445     return GST_PAD_LINK_REFUSED;
446   }
447 #endif
448
449   return GST_PAD_LINK_OK;
450 }
451
452 GType
453 gst_colorspace_get_type (void)
454 {
455   static GType colorspace_type = 0;
456
457   if (!colorspace_type) {
458     static const GTypeInfo colorspace_info = {
459       sizeof (GstColorspaceClass),
460       gst_colorspace_base_init,
461       NULL,
462       (GClassInitFunc) gst_colorspace_class_init,
463       NULL,
464       NULL,
465       sizeof (GstColorspace),
466       0,
467       (GInstanceInitFunc) gst_colorspace_init,
468     };
469
470     colorspace_type =
471         g_type_register_static (GST_TYPE_ELEMENT, "GstColorspace",
472         &colorspace_info, 0);
473   }
474   return colorspace_type;
475 }
476
477 static void
478 gst_colorspace_base_init (gpointer g_class)
479 {
480   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
481
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));
486
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>");
490 }
491
492 static void
493 gst_colorspace_class_init (GstColorspaceClass * klass)
494 {
495   GObjectClass *gobject_class;
496   GstElementClass *gstelement_class;
497
498   gobject_class = (GObjectClass *) klass;
499   gstelement_class = (GstElementClass *) klass;
500
501   parent_class = g_type_class_peek_parent (klass);
502
503   gobject_class->set_property = gst_colorspace_set_property;
504   gobject_class->get_property = gst_colorspace_get_property;
505
506   gstelement_class->change_state = gst_colorspace_change_state;
507
508   gst_colorspace_table_init (NULL);
509 }
510
511 static void
512 gst_colorspace_init (GstColorspace * space)
513 {
514   space->sinkpad =
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);
520
521   space->srcpad =
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);
525 }
526
527 static void
528 gst_colorspace_chain (GstPad * pad, GstData * _data)
529 {
530   GstBuffer *buf = GST_BUFFER (_data);
531   GstColorspace *space;
532   GstBuffer *outbuf = NULL;
533   GstColorspaceConverter *converter;
534
535   g_return_if_fail (pad != NULL);
536   g_return_if_fail (GST_IS_PAD (pad));
537   g_return_if_fail (buf != NULL);
538
539   space = GST_COLORSPACE (gst_pad_get_parent (pad));
540
541   g_return_if_fail (space != NULL);
542   g_return_if_fail (GST_IS_COLORSPACE (space));
543
544   if (GST_BUFFER_SIZE (buf) < space->sink_size) {
545     g_critical ("input size is smaller than expected");
546   }
547
548   outbuf =
549       gst_pad_alloc_buffer_and_set_caps (space->srcpad, GST_BUFFER_OFFSET_NONE,
550       space->src_size);
551
552   converter = gst_colorspace_converters + space->converter_index;
553   converter->convert (space, GST_BUFFER_DATA (outbuf), GST_BUFFER_DATA (buf));
554
555   GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buf);
556   GST_BUFFER_DURATION (outbuf) = GST_BUFFER_DURATION (buf);
557
558   gst_buffer_unref (buf);
559   gst_pad_push (space->srcpad, GST_DATA (outbuf));
560 }
561
562 static GstStateChangeReturn
563 gst_colorspace_change_state (GstElement * element, GstStateChange transition)
564 {
565   GstColorspace *space;
566
567   space = GST_COLORSPACE (element);
568
569   switch (transition) {
570     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
571       break;
572     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
573       break;
574     case GST_STATE_CHANGE_PAUSED_TO_READY:
575       break;
576   }
577
578   return parent_class->change_state (element, transition);
579 }
580
581 static void
582 gst_colorspace_set_property (GObject * object, guint prop_id,
583     const GValue * value, GParamSpec * pspec)
584 {
585   GstColorspace *space;
586
587   g_return_if_fail (GST_IS_COLORSPACE (object));
588   space = GST_COLORSPACE (object);
589
590   switch (prop_id) {
591     default:
592       break;
593   }
594 }
595
596 static void
597 gst_colorspace_get_property (GObject * object, guint prop_id, GValue * value,
598     GParamSpec * pspec)
599 {
600   GstColorspace *space;
601
602   g_return_if_fail (GST_IS_COLORSPACE (object));
603   space = GST_COLORSPACE (object);
604
605   switch (prop_id) {
606     default:
607       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
608       break;
609   }
610 }
611
612 static gboolean
613 plugin_init (GstPlugin * plugin)
614 {
615   if (!gst_element_register (plugin, "colorspace", GST_RANK_PRIMARY - 1,
616           GST_TYPE_COLORSPACE))
617     return FALSE;
618
619   return TRUE;
620 }
621
622 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
623     GST_VERSION_MINOR,
624     "yuvtorgbcolorspace",
625     "YUV to RGB colorspace converter",
626     plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)