various: fix pad template ref leaks
[platform/upstream/gstreamer.git] / gst / bayer / gstbayer2rgb.c
1 /* 
2  * GStreamer
3  * Copyright (C) 2007 David Schleef <ds@schleef.org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * March 2008
21  * Logic enhanced by William Brack <wbrack@mmm.com.hk>
22  */
23
24 /**
25  * SECTION:element-bayer2rgb
26  *
27  * Decodes raw camera bayer (fourcc BA81) to RGB.
28  */
29
30 /*
31  * In order to guard against my advancing maturity, some extra detailed
32  * information about the logic of the decode is included here.  Much of
33  * this was inspired by a technical paper from siliconimaging.com, which
34  * in turn was based upon an article from IEEE,
35  * T. Sakamoto, C. Nakanishi and T. Hase,
36  * “Software pixel interpolation for digital still cameras suitable for
37  *  a 32-bit MCU,”
38  * IEEE Trans. Consumer Electronics, vol. 44, no. 4, November 1998.
39  *
40  * The code assumes a Bayer matrix of the type produced by the fourcc
41  * BA81 (v4l2 format SBGGR8) of width w and height h which looks like:
42  *       0 1 2 3  w-2 w-1
43  *
44  *   0   B G B G ....B G
45  *   1   G R G R ....G R
46  *   2   B G B G ....B G
47  *       ...............
48  * h-2   B G B G ....B G
49  * h-1   G R G R ....G R
50  *
51  * We expand this matrix, producing a separate {r, g, b} triple for each
52  * of the individual elements.  The algorithm for doing this expansion is
53  * as follows.
54  *
55  * We are designing for speed of transformation, at a slight expense of code.
56  * First, we calculate the appropriate triples for the four corners, the
57  * remainder of the top and bottom rows, and the left and right columns.
58  * The reason for this is that those elements are transformed slightly
59  * differently than all of the remainder of the matrix. Finally, we transform
60  * all of the remainder.
61  *
62  * The transformation into the "appropriate triples" is based upon the
63  * "nearest neighbor" principal, with some additional complexity for the
64  * calculation of the "green" element, where an "adaptive" pairing is used.
65  *
66  * For purposes of documentation and indentification, each element of the
67  * original array can be put into one of four classes:
68  *   R   A red element
69  *   B   A blue element
70  *   GR  A green element which is followed by a red one
71  *   GB  A green element which is followed by a blue one
72  */
73
74 #ifdef HAVE_CONFIG_H
75 #include "config.h"
76 #endif
77
78 #include <gst/gst.h>
79 #include <gst/base/gstbasetransform.h>
80 #include <gst/video/video.h>
81 #include <string.h>
82 #include <stdlib.h>
83 #include <_stdint.h>
84 #include "gstbayerorc.h"
85
86 #define GST_CAT_DEFAULT gst_bayer2rgb_debug
87 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
88
89 enum
90 {
91   GST_BAYER_2_RGB_FORMAT_BGGR = 0,
92   GST_BAYER_2_RGB_FORMAT_GBRG,
93   GST_BAYER_2_RGB_FORMAT_GRBG,
94   GST_BAYER_2_RGB_FORMAT_RGGB
95 };
96
97
98 #define GST_TYPE_BAYER2RGB            (gst_bayer2rgb_get_type())
99 #define GST_BAYER2RGB(obj)            (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_BAYER2RGB,GstBayer2RGB))
100 #define GST_IS_BAYER2RGB(obj)         (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_BAYER2RGB))
101 #define GST_BAYER2RGB_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST((klass) ,GST_TYPE_BAYER2RGB,GstBayer2RGBClass))
102 #define GST_IS_BAYER2RGB_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass) ,GST_TYPE_BAYER2RGB))
103 #define GST_BAYER2RGB_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS((obj) ,GST_TYPE_BAYER2RGB,GstBayer2RGBClass))
104 typedef struct _GstBayer2RGB GstBayer2RGB;
105 typedef struct _GstBayer2RGBClass GstBayer2RGBClass;
106
107 typedef void (*GstBayer2RGBProcessFunc) (GstBayer2RGB *, guint8 *, guint);
108
109 struct _GstBayer2RGB
110 {
111   GstBaseTransform basetransform;
112
113   /* < private > */
114   int width;
115   int height;
116   int stride;
117   int pixsize;                  /* bytes per pixel */
118   int r_off;                    /* offset for red */
119   int g_off;                    /* offset for green */
120   int b_off;                    /* offset for blue */
121   int format;
122 };
123
124 struct _GstBayer2RGBClass
125 {
126   GstBaseTransformClass parent;
127 };
128
129 #define SRC_CAPS                                 \
130   GST_VIDEO_CAPS_RGBx ";"                        \
131   GST_VIDEO_CAPS_xRGB ";"                        \
132   GST_VIDEO_CAPS_BGRx ";"                        \
133   GST_VIDEO_CAPS_xBGR ";"                        \
134   GST_VIDEO_CAPS_RGBA ";"                        \
135   GST_VIDEO_CAPS_ARGB ";"                        \
136   GST_VIDEO_CAPS_BGRA ";"                        \
137   GST_VIDEO_CAPS_ABGR
138
139 #define SINK_CAPS "video/x-raw-bayer,format=(string){bggr,grbg,gbrg,rggb}," \
140   "width=(int)[1,MAX],height=(int)[1,MAX],framerate=(fraction)[0/1,MAX]"
141
142 enum
143 {
144   PROP_0
145 };
146
147 #define DEBUG_INIT(bla) \
148   GST_DEBUG_CATEGORY_INIT (gst_bayer2rgb_debug, "bayer2rgb", 0, "bayer2rgb element");
149
150 GType gst_bayer2rgb_get_type (void);
151 GST_BOILERPLATE_FULL (GstBayer2RGB, gst_bayer2rgb, GstBaseTransform,
152     GST_TYPE_BASE_TRANSFORM, DEBUG_INIT);
153
154 static void gst_bayer2rgb_set_property (GObject * object, guint prop_id,
155     const GValue * value, GParamSpec * pspec);
156 static void gst_bayer2rgb_get_property (GObject * object, guint prop_id,
157     GValue * value, GParamSpec * pspec);
158
159 static gboolean gst_bayer2rgb_set_caps (GstBaseTransform * filter,
160     GstCaps * incaps, GstCaps * outcaps);
161 static GstFlowReturn gst_bayer2rgb_transform (GstBaseTransform * base,
162     GstBuffer * inbuf, GstBuffer * outbuf);
163 static void gst_bayer2rgb_reset (GstBayer2RGB * filter);
164 static GstCaps *gst_bayer2rgb_transform_caps (GstBaseTransform * base,
165     GstPadDirection direction, GstCaps * caps);
166 static gboolean gst_bayer2rgb_get_unit_size (GstBaseTransform * base,
167     GstCaps * caps, guint * size);
168
169
170 static void
171 gst_bayer2rgb_base_init (gpointer klass)
172 {
173   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
174   GstPadTemplate *pad_template;
175
176   gst_element_class_set_details_simple (element_class,
177       "Bayer to RGB decoder for cameras", "Filter/Converter/Video",
178       "Converts video/x-raw-bayer to video/x-raw-rgb",
179       "William Brack <wbrack@mmm.com.hk>");
180
181   pad_template =
182       gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
183       gst_caps_from_string (SRC_CAPS));
184   gst_element_class_add_pad_template (element_class, pad_template);
185   gst_object_unref (pad_template);
186   pad_template =
187       gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
188       gst_caps_from_string (SINK_CAPS));
189   gst_element_class_add_pad_template (element_class, pad_template);
190   gst_object_unref (pad_template);
191 }
192
193 static void
194 gst_bayer2rgb_class_init (GstBayer2RGBClass * klass)
195 {
196   GObjectClass *gobject_class;
197
198   gobject_class = (GObjectClass *) klass;
199   gobject_class->set_property = gst_bayer2rgb_set_property;
200   gobject_class->get_property = gst_bayer2rgb_get_property;
201
202   GST_BASE_TRANSFORM_CLASS (klass)->transform_caps =
203       GST_DEBUG_FUNCPTR (gst_bayer2rgb_transform_caps);
204   GST_BASE_TRANSFORM_CLASS (klass)->get_unit_size =
205       GST_DEBUG_FUNCPTR (gst_bayer2rgb_get_unit_size);
206   GST_BASE_TRANSFORM_CLASS (klass)->set_caps =
207       GST_DEBUG_FUNCPTR (gst_bayer2rgb_set_caps);
208   GST_BASE_TRANSFORM_CLASS (klass)->transform =
209       GST_DEBUG_FUNCPTR (gst_bayer2rgb_transform);
210 }
211
212 static void
213 gst_bayer2rgb_init (GstBayer2RGB * filter, GstBayer2RGBClass * klass)
214 {
215   gst_bayer2rgb_reset (filter);
216   gst_base_transform_set_in_place (GST_BASE_TRANSFORM (filter), TRUE);
217 }
218
219 /* No properties are implemented, so only a warning is produced */
220 static void
221 gst_bayer2rgb_set_property (GObject * object, guint prop_id,
222     const GValue * value, GParamSpec * pspec)
223 {
224
225   switch (prop_id) {
226     default:
227       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
228       break;
229   }
230 }
231
232 static void
233 gst_bayer2rgb_get_property (GObject * object, guint prop_id,
234     GValue * value, GParamSpec * pspec)
235 {
236
237   switch (prop_id) {
238     default:
239       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
240       break;
241   }
242 }
243
244 /* Routine to convert colormask value into relative byte offset */
245 static int
246 get_pix_offset (int mask, int bpp)
247 {
248   int bpp32 = (bpp / 8) - 3;
249
250   switch (mask) {
251     case 255:
252       return 2 + bpp32;
253     case 65280:
254       return 1 + bpp32;
255     case 16711680:
256       return 0 + bpp32;
257     case -16777216:
258       return 0;
259     default:
260       GST_ERROR ("Invalid color mask 0x%08x", mask);
261       return -1;
262   }
263 }
264
265 static gboolean
266 gst_bayer2rgb_set_caps (GstBaseTransform * base, GstCaps * incaps,
267     GstCaps * outcaps)
268 {
269   GstBayer2RGB *bayer2rgb = GST_BAYER2RGB (base);
270   GstStructure *structure;
271   int val, bpp;
272   const char *format;
273
274   GST_DEBUG ("in caps %" GST_PTR_FORMAT " out caps %" GST_PTR_FORMAT, incaps,
275       outcaps);
276
277   structure = gst_caps_get_structure (incaps, 0);
278
279   gst_structure_get_int (structure, "width", &bayer2rgb->width);
280   gst_structure_get_int (structure, "height", &bayer2rgb->height);
281   bayer2rgb->stride = GST_ROUND_UP_4 (bayer2rgb->width);
282
283   format = gst_structure_get_string (structure, "format");
284   if (g_str_equal (format, "bggr")) {
285     bayer2rgb->format = GST_BAYER_2_RGB_FORMAT_BGGR;
286   } else if (g_str_equal (format, "gbrg")) {
287     bayer2rgb->format = GST_BAYER_2_RGB_FORMAT_GBRG;
288   } else if (g_str_equal (format, "grbg")) {
289     bayer2rgb->format = GST_BAYER_2_RGB_FORMAT_GRBG;
290   } else if (g_str_equal (format, "rggb")) {
291     bayer2rgb->format = GST_BAYER_2_RGB_FORMAT_RGGB;
292   } else {
293     return FALSE;
294   }
295
296   /* To cater for different RGB formats, we need to set params for later */
297   structure = gst_caps_get_structure (outcaps, 0);
298   gst_structure_get_int (structure, "bpp", &bpp);
299   bayer2rgb->pixsize = bpp / 8;
300   gst_structure_get_int (structure, "red_mask", &val);
301   bayer2rgb->r_off = get_pix_offset (val, bpp);
302   gst_structure_get_int (structure, "green_mask", &val);
303   bayer2rgb->g_off = get_pix_offset (val, bpp);
304   gst_structure_get_int (structure, "blue_mask", &val);
305   bayer2rgb->b_off = get_pix_offset (val, bpp);
306
307   return TRUE;
308 }
309
310 static void
311 gst_bayer2rgb_reset (GstBayer2RGB * filter)
312 {
313   filter->width = 0;
314   filter->height = 0;
315   filter->stride = 0;
316   filter->pixsize = 0;
317   filter->r_off = 0;
318   filter->g_off = 0;
319   filter->b_off = 0;
320 }
321
322 static GstCaps *
323 gst_bayer2rgb_transform_caps (GstBaseTransform * base,
324     GstPadDirection direction, GstCaps * caps)
325 {
326   GstStructure *structure;
327   GstCaps *newcaps;
328   GstStructure *newstruct;
329
330   GST_DEBUG_OBJECT (caps, "transforming caps (from)");
331
332   structure = gst_caps_get_structure (caps, 0);
333
334   if (direction == GST_PAD_SRC) {
335     newcaps = gst_caps_from_string ("video/x-raw-bayer,"
336         "format=(string){bggr,grbg,gbrg,rggb}");
337   } else {
338     newcaps = gst_caps_new_simple ("video/x-raw-rgb", NULL);
339   }
340   newstruct = gst_caps_get_structure (newcaps, 0);
341
342   gst_structure_set_value (newstruct, "width",
343       gst_structure_get_value (structure, "width"));
344   gst_structure_set_value (newstruct, "height",
345       gst_structure_get_value (structure, "height"));
346   gst_structure_set_value (newstruct, "framerate",
347       gst_structure_get_value (structure, "framerate"));
348
349   GST_DEBUG_OBJECT (newcaps, "transforming caps (into)");
350
351   return newcaps;
352 }
353
354 static gboolean
355 gst_bayer2rgb_get_unit_size (GstBaseTransform * base, GstCaps * caps,
356     guint * size)
357 {
358   GstStructure *structure;
359   int width;
360   int height;
361   int pixsize;
362   const char *name;
363
364   structure = gst_caps_get_structure (caps, 0);
365
366   if (gst_structure_get_int (structure, "width", &width) &&
367       gst_structure_get_int (structure, "height", &height)) {
368     name = gst_structure_get_name (structure);
369     /* Our name must be either video/x-raw-bayer video/x-raw-rgb */
370     if (strcmp (name, "video/x-raw-rgb")) {
371       *size = GST_ROUND_UP_4 (width) * height;
372       return TRUE;
373     } else {
374       /* For output, calculate according to format */
375       if (gst_structure_get_int (structure, "bpp", &pixsize)) {
376         *size = width * height * (pixsize / 8);
377         return TRUE;
378       }
379     }
380
381   }
382   GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
383       ("Incomplete caps, some required field missing"));
384   return FALSE;
385 }
386
387 static void
388 gst_bayer2rgb_split_and_upsample_horiz (guint8 * dest0, guint8 * dest1,
389     const guint8 * src, int n)
390 {
391   int i;
392
393   dest0[0] = src[0];
394   dest1[0] = src[1];
395   dest0[1] = (src[0] + src[2] + 1) >> 1;
396   dest1[1] = src[1];
397
398 #if defined(__i386__) || defined(__amd64__)
399   gst_bayer_horiz_upsample_unaligned (dest0 + 2, dest1 + 2, src + 1,
400       (n - 4) >> 1);
401 #else
402   gst_bayer_horiz_upsample (dest0 + 2, dest1 + 2, src + 2, (n - 4) >> 1);
403 #endif
404
405   for (i = n - 2; i < n; i++) {
406     if ((i & 1) == 0) {
407       dest0[i] = src[i];
408       dest1[i] = src[i - 1];
409     } else {
410       dest0[i] = src[i - 1];
411       dest1[i] = src[i];
412     }
413   }
414 }
415
416 typedef void (*process_func) (guint8 * d0, const guint8 * s0, const guint8 * s1,
417     const guint8 * s2, const guint8 * s3, const guint8 * s4, const guint8 * s5,
418     int n);
419
420 static void
421 gst_bayer2rgb_process (GstBayer2RGB * bayer2rgb, uint8_t * dest,
422     int dest_stride, uint8_t * src, int src_stride)
423 {
424   int j;
425   guint8 *tmp;
426   process_func merge[2] = { NULL, NULL };
427   int r_off, g_off, b_off;
428
429   /* We exploit some symmetry in the functions here.  The base functions
430    * are all named for the BGGR arrangement.  For RGGB, we swap the
431    * red offset and blue offset in the output.  For GRBG, we swap the
432    * order of the merge functions.  For GBRG, do both. */
433   r_off = bayer2rgb->r_off;
434   g_off = bayer2rgb->g_off;
435   b_off = bayer2rgb->b_off;
436   if (bayer2rgb->format == GST_BAYER_2_RGB_FORMAT_RGGB ||
437       bayer2rgb->format == GST_BAYER_2_RGB_FORMAT_GBRG) {
438     r_off = bayer2rgb->b_off;
439     b_off = bayer2rgb->r_off;
440   }
441
442   if (r_off == 2 && g_off == 1 && b_off == 0) {
443     merge[0] = gst_bayer_merge_bg_bgra;
444     merge[1] = gst_bayer_merge_gr_bgra;
445   } else if (r_off == 3 && g_off == 2 && b_off == 1) {
446     merge[0] = gst_bayer_merge_bg_abgr;
447     merge[1] = gst_bayer_merge_gr_abgr;
448   } else if (r_off == 1 && g_off == 2 && b_off == 3) {
449     merge[0] = gst_bayer_merge_bg_argb;
450     merge[1] = gst_bayer_merge_gr_argb;
451   } else if (r_off == 0 && g_off == 1 && b_off == 2) {
452     merge[0] = gst_bayer_merge_bg_rgba;
453     merge[1] = gst_bayer_merge_gr_rgba;
454   }
455   if (bayer2rgb->format == GST_BAYER_2_RGB_FORMAT_GRBG ||
456       bayer2rgb->format == GST_BAYER_2_RGB_FORMAT_GBRG) {
457     process_func tmp = merge[0];
458     merge[0] = merge[1];
459     merge[1] = tmp;
460   }
461
462   tmp = g_malloc (2 * 4 * bayer2rgb->width);
463 #define LINE(x) (tmp + ((x)&7) * bayer2rgb->width)
464
465   gst_bayer2rgb_split_and_upsample_horiz (LINE (3 * 2 + 0), LINE (3 * 2 + 1),
466       src + 1 * src_stride, bayer2rgb->width);
467   j = 0;
468   gst_bayer2rgb_split_and_upsample_horiz (LINE (j * 2 + 0), LINE (j * 2 + 1),
469       src + j * src_stride, bayer2rgb->width);
470
471   for (j = 0; j < bayer2rgb->height; j++) {
472     if (j < bayer2rgb->height - 1) {
473       gst_bayer2rgb_split_and_upsample_horiz (LINE ((j + 1) * 2 + 0),
474           LINE ((j + 1) * 2 + 1), src + (j + 1) * src_stride, bayer2rgb->width);
475     }
476
477     merge[j & 1] (dest + j * dest_stride,
478         LINE (j * 2 - 2), LINE (j * 2 - 1),
479         LINE (j * 2 + 0), LINE (j * 2 + 1),
480         LINE (j * 2 + 2), LINE (j * 2 + 3), bayer2rgb->width >> 1);
481   }
482
483   g_free (tmp);
484 }
485
486
487
488
489 static GstFlowReturn
490 gst_bayer2rgb_transform (GstBaseTransform * base, GstBuffer * inbuf,
491     GstBuffer * outbuf)
492 {
493   GstBayer2RGB *filter = GST_BAYER2RGB (base);
494   uint8_t *input, *output;
495
496   /*
497    * We need to lock our filter params to prevent changing
498    * caps in the middle of a transformation (nice way to get
499    * segfaults)
500    */
501   GST_OBJECT_LOCK (filter);
502
503   GST_DEBUG ("transforming buffer");
504   input = (uint8_t *) GST_BUFFER_DATA (inbuf);
505   output = (uint8_t *) GST_BUFFER_DATA (outbuf);
506   gst_bayer2rgb_process (filter, output, filter->width * 4,
507       input, filter->width);
508
509   GST_OBJECT_UNLOCK (filter);
510   return GST_FLOW_OK;
511 }