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