[MOVED FROM BAD 28/68] Fix leaks.
[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 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>");
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 GstStateChangeReturn gst_colorspace_change_state (GstElement * element,
95     GstStateChange transition);
96
97
98 static GstElementClass *parent_class = NULL;
99
100 /*static guint gst_colorspace_signals[LAST_SIGNAL] = { 0 }; */
101
102 #if 0
103 static gboolean
104 colorspace_setup_converter (GstColorspace * space, GstCaps * from_caps,
105     GstCaps * to_caps)
106 {
107   guint32 from_space, to_space;
108   GstStructure *from_struct;
109   GstStructure *to_struct;
110
111   g_return_val_if_fail (to_caps != NULL, FALSE);
112   g_return_val_if_fail (from_caps != NULL, FALSE);
113
114   from_struct = gst_caps_get_structure (from_caps, 0);
115   to_struct = gst_caps_get_structure (to_caps, 0);
116
117   from_space = GST_MAKE_FOURCC ('R', 'G', 'B', ' ');
118   gst_structure_get_fourcc (from_struct, "format", &from_space);
119
120   to_space = GST_MAKE_FOURCC ('R', 'G', 'B', ' ');
121   gst_structure_get_fourcc (to_struct, "format", &to_space);
122
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);
127
128   switch (from_space) {
129     case GST_MAKE_FOURCC ('R', 'G', 'B', ' '):
130     {
131       gint from_bpp;
132
133       gst_structure_get_int (from_struct, "bpp", &from_bpp);
134
135       switch (to_space) {
136         case GST_MAKE_FOURCC ('R', 'G', 'B', ' '):
137 #ifdef HAVE_HERMES
138         {
139           gint to_bpp;
140
141           gst_structure_get_int (to_struct, "bpp", &to_bpp);
142
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);
146           space->source.a = 0;
147           space->srcbpp = space->source.bits = from_bpp;
148           space->source.indexed = 0;
149           space->source.has_colorkey = 0;
150
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);
155
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);
159           space->dest.a = 0;
160           space->destbpp = space->dest.bits = to_bpp;
161           space->dest.indexed = 0;
162           space->dest.has_colorkey = 0;
163
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);
168
169           if (!Hermes_ConverterRequest (space->h_handle, &space->source,
170                   &space->dest)) {
171             g_warning ("Hermes: could not get converter\n");
172             return FALSE;
173           }
174           GST_INFO ("converter set up");
175           space->type = GST_COLORSPACE_HERMES;
176           return TRUE;
177         }
178 #else
179           g_warning ("colorspace: compiled without hermes!");
180           return FALSE;
181 #endif
182         case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
183           if (from_bpp == 32) {
184             space->type = GST_COLORSPACE_RGB32_YV12;
185             space->destbpp = 12;
186             return TRUE;
187           }
188         case GST_MAKE_FOURCC ('I', '4', '2', '0'):
189           if (from_bpp == 32) {
190             space->type = GST_COLORSPACE_RGB32_I420;
191             space->destbpp = 12;
192             return TRUE;
193           }
194         case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
195           GST_INFO ("colorspace: RGB to YUV with bpp %d not implemented!!",
196               from_bpp);
197           return FALSE;
198       }
199       break;
200     }
201     case GST_MAKE_FOURCC ('I', '4', '2', '0'):
202       switch (to_space) {
203         case GST_MAKE_FOURCC ('R', 'G', 'B', ' '):
204           GST_INFO ("colorspace: YUV to RGB");
205
206           gst_structure_get_int (to_struct, "bpp", &space->destbpp);
207           space->converter =
208               gst_colorspace_yuv2rgb_get_converter (from_caps, to_caps);
209           space->type = GST_COLORSPACE_YUV_RGB;
210           return TRUE;
211         case GST_MAKE_FOURCC ('I', '4', '2', '0'):
212           space->type = GST_COLORSPACE_NONE;
213           space->destbpp = 12;
214           return TRUE;
215         case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
216           space->type = GST_COLORSPACE_420_SWAP;
217           space->destbpp = 12;
218           return TRUE;
219
220       }
221       break;
222     case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
223       switch (to_space) {
224         case GST_MAKE_FOURCC ('I', '4', '2', '0'):
225           space->type = GST_COLORSPACE_YUY2_I420;
226           space->destbpp = 12;
227           return TRUE;
228         case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
229           space->type = GST_COLORSPACE_NONE;
230           space->destbpp = 16;
231           return TRUE;
232         case GST_MAKE_FOURCC ('R', 'G', 'B', ' '):
233           GST_INFO ("colorspace: YUY2 to RGB not implemented!!");
234           return FALSE;
235       }
236       break;
237     case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
238       switch (to_space) {
239         case GST_MAKE_FOURCC ('R', 'G', 'B', ' '):
240           GST_INFO ("colorspace: YV12 to RGB");
241
242           gst_structure_get_int (to_struct, "bpp", &space->destbpp);
243           space->converter =
244               gst_colorspace_yuv2rgb_get_converter (from_caps, to_caps);
245           space->type = GST_COLORSPACE_YUV_RGB;
246           return TRUE;
247         case GST_MAKE_FOURCC ('I', '4', '2', '0'):
248           space->type = GST_COLORSPACE_420_SWAP;
249           space->destbpp = 12;
250           return TRUE;
251         case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
252           space->type = GST_COLORSPACE_NONE;
253           space->destbpp = 12;
254           return TRUE;
255       }
256       break;
257   }
258   return FALSE;
259 }
260 #endif
261
262 static GstCaps *
263 gst_colorspace_caps_remove_format_info (GstCaps * caps, const char *media_type)
264 {
265   int i;
266   GstStructure *structure;
267
268   for (i = 0; i < gst_caps_get_size (caps); i++) {
269     structure = gst_caps_get_structure (caps, i);
270
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");
279   }
280
281   gst_caps_do_simplify (caps);
282   return caps;
283 }
284
285 static GstCaps *
286 gst_colorspace_getcaps (GstPad * pad)
287 {
288   GstColorspace *space;
289   GstPad *otherpad;
290   GstCaps *othercaps;
291   GstCaps *caps;
292
293   space = GST_COLORSPACE (gst_pad_get_parent (pad));
294
295   otherpad = (pad == space->srcpad) ? space->sinkpad : space->srcpad;
296
297   othercaps = gst_pad_get_allowed_caps (otherpad);
298
299   othercaps = gst_colorspace_caps_remove_format_info (othercaps,
300       (pad == space->srcpad) ? "video/x-raw-rgb" : "video/x-raw-yuv");
301
302   caps = gst_caps_intersect (othercaps, gst_pad_get_pad_template_caps (pad));
303   gst_caps_free (othercaps);
304
305   return caps;
306 }
307
308 static GstColorSpaceFormatType
309 gst_colorspace_get_format (const GstCaps * caps)
310 {
311   int i;
312
313   for (i = 0; i < G_N_ELEMENTS (gst_colorspace_formats); i++) {
314     GstCaps *icaps;
315     GstCaps *fcaps;
316
317     fcaps =
318         gst_caps_copy (gst_static_caps_get (&gst_colorspace_formats[i].caps));
319
320     icaps = gst_caps_intersect (caps, fcaps);
321     if (!gst_caps_is_empty (icaps)) {
322       gst_caps_free (icaps);
323       return i;
324     }
325     gst_caps_free (icaps);
326   }
327
328   g_assert_not_reached ();
329   return -1;
330 }
331
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)
335
336 static int
337 gst_colorspace_format_get_size (GstColorSpaceFormatType index, int width,
338     int height)
339 {
340   int size;
341
342   switch (index) {
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;
348       return size;
349       break;
350     case GST_COLORSPACE_RGB32:
351       return width * height * 4;
352       break;
353     case GST_COLORSPACE_RGB24:
354       return ROUND_UP_4 (width * 3) * height;
355       break;
356     case GST_COLORSPACE_RGB16:
357       return ROUND_UP_4 (width * 2) * height;
358       break;
359   }
360
361   g_assert_not_reached ();
362   return 0;
363 }
364
365 static int
366 gst_colorspace_get_converter (GstColorSpaceFormatType from,
367     GstColorSpaceFormatType to)
368 {
369   int i;
370
371   for (i = 0; i < G_N_ELEMENTS (gst_colorspace_converters); i++) {
372     GstColorspaceConverter *converter = gst_colorspace_converters + i;
373
374     if (from == converter->from && to == converter->to) {
375       return i;
376     }
377   }
378   g_assert_not_reached ();
379   return -1;
380 }
381
382 static GstPadLinkReturn
383 gst_colorspace_link (GstPad * pad, const GstCaps * caps)
384 {
385   GstColorspace *space;
386   GstPad *otherpad;
387   GstStructure *structure;
388   GstPadLinkReturn link_ret;
389   int width, height;
390   double fps;
391   int format_index;
392
393   space = GST_COLORSPACE (gst_pad_get_parent (pad));
394   otherpad = (pad == space->sinkpad) ? space->srcpad : space->sinkpad;
395
396   link_ret = gst_pad_try_set_caps (otherpad, caps);
397   if (link_ret == GST_PAD_LINK_OK) {
398     return link_ret;
399   }
400
401   structure = gst_caps_get_structure (caps, 0);
402
403   format_index = gst_colorspace_get_format (caps);
404   g_print ("format index is %d\n", format_index);
405
406   gst_structure_get_int (structure, "width", &width);
407   gst_structure_get_int (structure, "height", &height);
408   gst_structure_get_double (structure, "framerate", &fps);
409
410   GST_INFO ("size: %dx%d", space->width, space->height);
411
412   if (gst_pad_is_negotiated (otherpad)) {
413     GstCaps *othercaps;
414
415     othercaps = gst_caps_copy (gst_pad_get_negotiated_caps (otherpad));
416
417     gst_caps_set_simple (othercaps,
418         "width", G_TYPE_INT, width,
419         "height", G_TYPE_INT, height, "framerate", G_TYPE_DOUBLE, fps, NULL);
420
421     link_ret = gst_pad_try_set_caps (otherpad, othercaps);
422     if (link_ret != GST_PAD_LINK_OK) {
423       return link_ret;
424     }
425   }
426
427   if (pad == space->srcpad) {
428     space->src_format_index = format_index;
429   } else {
430     space->sink_format_index = format_index;
431   }
432
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);
437
438     g_print ("using index %d\n", space->converter_index);
439
440     space->sink_size = gst_colorspace_format_get_size (space->sink_format_index,
441         width, height);
442     space->src_size = gst_colorspace_format_get_size (space->src_format_index,
443         width, height);
444     space->width = width;
445     space->height = height;
446     space->fps = fps;
447   }
448 #if 0
449   if (gst_pad_is_negotiated (otherpad)) {
450     g_warning ("could not get converter\n");
451     return GST_PAD_LINK_REFUSED;
452   }
453 #endif
454
455   return GST_PAD_LINK_OK;
456 }
457
458 GType
459 gst_colorspace_get_type (void)
460 {
461   static GType colorspace_type = 0;
462
463   if (!colorspace_type) {
464     static const GTypeInfo colorspace_info = {
465       sizeof (GstColorspaceClass),
466       gst_colorspace_base_init,
467       NULL,
468       (GClassInitFunc) gst_colorspace_class_init,
469       NULL,
470       NULL,
471       sizeof (GstColorspace),
472       0,
473       (GInstanceInitFunc) gst_colorspace_init,
474     };
475
476     colorspace_type =
477         g_type_register_static (GST_TYPE_ELEMENT, "GstColorspace",
478         &colorspace_info, 0);
479   }
480   return colorspace_type;
481 }
482
483 static void
484 gst_colorspace_base_init (gpointer g_class)
485 {
486   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
487
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));
492
493   gst_element_class_set_details (element_class, &colorspace_details);
494 }
495
496 static void
497 gst_colorspace_class_init (GstColorspaceClass * klass)
498 {
499   GObjectClass *gobject_class;
500   GstElementClass *gstelement_class;
501
502   gobject_class = (GObjectClass *) klass;
503   gstelement_class = (GstElementClass *) klass;
504
505   parent_class = g_type_class_peek_parent (klass);
506
507   gobject_class->set_property = gst_colorspace_set_property;
508   gobject_class->get_property = gst_colorspace_get_property;
509
510   gstelement_class->change_state = gst_colorspace_change_state;
511
512   gst_colorspace_table_init (NULL);
513 }
514
515 static void
516 gst_colorspace_init (GstColorspace * space)
517 {
518   space->sinkpad =
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);
524
525   space->srcpad =
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);
529 }
530
531 static void
532 gst_colorspace_chain (GstPad * pad, GstData * _data)
533 {
534   GstBuffer *buf = GST_BUFFER (_data);
535   GstColorspace *space;
536   GstBuffer *outbuf = NULL;
537   GstColorspaceConverter *converter;
538
539   g_return_if_fail (pad != NULL);
540   g_return_if_fail (GST_IS_PAD (pad));
541   g_return_if_fail (buf != NULL);
542
543   space = GST_COLORSPACE (gst_pad_get_parent (pad));
544
545   g_return_if_fail (space != NULL);
546   g_return_if_fail (GST_IS_COLORSPACE (space));
547
548   if (GST_BUFFER_SIZE (buf) < space->sink_size) {
549     g_critical ("input size is smaller than expected");
550   }
551
552   outbuf =
553       gst_pad_alloc_buffer_and_set_caps (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 GstStateChangeReturn
567 gst_colorspace_change_state (GstElement * element, GstStateChange transition)
568 {
569   GstColorspace *space;
570
571   space = GST_COLORSPACE (element);
572
573   switch (transition) {
574     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
575       break;
576     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
577       break;
578     case GST_STATE_CHANGE_PAUSED_TO_READY:
579       break;
580   }
581
582   return parent_class->change_state (element, transition);
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_NAME, GST_PACKAGE_ORIGIN)