gst/: Printf format fixes (#476128).
[platform/upstream/gst-plugins-good.git] / gst / interleave / deinterleave.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *                    2005 Wim Taymans <wim@fluendo.com>
5  *                    2007 Andy Wingo <wingo at pobox.com>
6  *
7  * deinterleave.c: deinterleave samples, based on interleave.c
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public
20  * License along with this library; if not, write to the
21  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22  * Boston, MA 02111-1307, USA.
23  */
24
25
26 #ifdef HAVE_CONFIG_H
27 #  include "config.h"
28 #endif
29
30 #include <gst/gst.h>
31
32
33 #define GST_TYPE_DEINTERLEAVE            (gst_deinterleave_get_type())
34 #define GST_DEINTERLEAVE(obj)            (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_DEINTERLEAVE,GstDeinterleave))
35 #define GST_DEINTERLEAVE_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_DEINTERLEAVE,GstDeinterleaveClass))
36 #define GST_DEINTERLEAVE_GET_CLASS(obj) \
37         (G_TYPE_INSTANCE_GET_CLASS ((obj),GST_TYPE_DEINTERLEAVE,GstDeinterleaveClass))
38 #define GST_IS_DEINTERLEAVE(obj)         (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_DEINTERLEAVE))
39 #define GST_IS_DEINTERLEAVE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_DEINTERLEAVE))
40
41 typedef struct _GstDeinterleave GstDeinterleave;
42 typedef struct _GstDeinterleaveClass GstDeinterleaveClass;
43
44
45 struct _GstDeinterleave
46 {
47   GstElement element;
48
49   /*< private > */
50   GList *srcpads;
51   GstCaps *sinkcaps;
52   gint channels;
53
54   GstPad *sink;
55 };
56
57 struct _GstDeinterleaveClass
58 {
59   GstElementClass parent_class;
60 };
61
62
63 GST_DEBUG_CATEGORY_STATIC (gst_deinterleave_debug);
64 #define GST_CAT_DEFAULT gst_deinterleave_debug
65
66
67 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src%d",
68     GST_PAD_SRC,
69     GST_PAD_SOMETIMES,
70     GST_STATIC_CAPS ("audio/x-raw-float, "
71         "rate = (int) [ 1, MAX ], "
72         "channels = (int) 1, "
73         "endianness = (int) BYTE_ORDER, " "width = (int) 32")
74     );
75 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
76     GST_PAD_SINK,
77     GST_PAD_ALWAYS,
78     GST_STATIC_CAPS ("audio/x-raw-float, "
79         "rate = (int) [ 1, MAX ], "
80         "channels = (int) [ 1, MAX ], "
81         "endianness = (int) BYTE_ORDER, " "width = (int) 32")
82     );
83
84
85 GST_BOILERPLATE (GstDeinterleave, gst_deinterleave, GstElement,
86     GST_TYPE_ELEMENT);
87
88
89 static GstFlowReturn gst_deinterleave_chain (GstPad * pad, GstBuffer * buffer);
90 static gboolean gst_deinterleave_sink_setcaps (GstPad * pad, GstCaps * caps);
91 static gboolean gst_deinterleave_sink_activate_push (GstPad * pad,
92     gboolean active);
93
94
95 static const GstElementDetails details =
96 GST_ELEMENT_DETAILS ("Audio deinterleaver",
97     "Filter/Converter/Audio",
98     "Splits one interleaved multichannel audio stream into many mono audio streams",
99     "Andy Wingo <wingo at pobox.com>, " "Iain <iain@prettypeople.org>");
100
101 static void
102 gst_deinterleave_base_init (gpointer g_class)
103 {
104   GST_DEBUG_CATEGORY_INIT (gst_deinterleave_debug, "interleave", 0,
105       "interleave element");
106
107   gst_element_class_set_details (g_class, &details);
108
109   gst_element_class_add_pad_template (g_class,
110       gst_static_pad_template_get (&sink_template));
111   gst_element_class_add_pad_template (g_class,
112       gst_static_pad_template_get (&src_template));
113 }
114
115 static void
116 gst_deinterleave_class_init (GstDeinterleaveClass * klass)
117 {
118   /* pass */
119 }
120
121 static void
122 gst_deinterleave_init (GstDeinterleave * self, GstDeinterleaveClass * klass)
123 {
124   self->sink = gst_pad_new_from_static_template (&sink_template, "sink");
125
126   gst_pad_set_chain_function (self->sink,
127       GST_DEBUG_FUNCPTR (gst_deinterleave_chain));
128   gst_pad_set_setcaps_function (self->sink,
129       GST_DEBUG_FUNCPTR (gst_deinterleave_sink_setcaps));
130   gst_pad_set_activatepush_function (self->sink,
131       GST_DEBUG_FUNCPTR (gst_deinterleave_sink_activate_push));
132
133   gst_element_add_pad (GST_ELEMENT (self), self->sink);
134 }
135
136 static void
137 gst_deinterleave_add_new_pads (GstDeinterleave * self, GstCaps * caps)
138 {
139   GstPad *pad;
140   guint i;
141
142   for (i = 0; i < self->channels; i++) {
143     gchar *name = g_strdup_printf ("src%d", i);
144
145     pad = gst_pad_new_from_static_template (&src_template, name);
146     g_free (name);
147     gst_pad_set_caps (pad, caps);
148     gst_pad_use_fixed_caps (pad);
149     gst_pad_set_active (pad, TRUE);
150     gst_element_add_pad (GST_ELEMENT (self), pad);
151     self->srcpads = g_list_prepend (self->srcpads, gst_object_ref (pad));
152   }
153
154   gst_element_no_more_pads (GST_ELEMENT (self));
155   self->srcpads = g_list_reverse (self->srcpads);
156 }
157
158 static void
159 gst_deinterleave_remove_pads (GstDeinterleave * self)
160 {
161   GList *l;
162
163   GST_INFO_OBJECT (self, "removing pads");
164
165   for (l = self->srcpads; l; l = l->next) {
166     GstPad *pad = GST_PAD (l->data);
167
168     gst_element_remove_pad (GST_ELEMENT_CAST (self), pad);
169     gst_object_unref (pad);
170   }
171   g_list_free (self->srcpads);
172   self->srcpads = NULL;
173
174   gst_pad_set_caps (self->sink, NULL);
175   gst_caps_replace (&self->sinkcaps, NULL);
176 }
177
178 static gboolean
179 gst_deinterleave_sink_setcaps (GstPad * pad, GstCaps * caps)
180 {
181   GstDeinterleave *self;
182
183   self = GST_DEINTERLEAVE (gst_pad_get_parent (pad));
184
185   if (self->sinkcaps && !gst_caps_is_equal (caps, self->sinkcaps)) {
186     goto cannot_change_caps_dog;
187   } else {
188     GST_DEBUG_OBJECT (self, "got caps: %" GST_PTR_FORMAT, caps);
189     gst_caps_replace (&self->sinkcaps, caps);
190   }
191
192   {
193     GstCaps *srccaps;
194     GstStructure *s;
195
196     srccaps = gst_caps_copy (caps);
197     s = gst_caps_get_structure (srccaps, 0);
198     if (!gst_structure_get_int (s, "channels", &self->channels))
199       goto no_channels;
200     gst_structure_set (s, "channels", G_TYPE_INT, 1, NULL);
201     gst_structure_remove_field (s, "channel-positions");
202     gst_deinterleave_add_new_pads (self, srccaps);
203     gst_caps_unref (srccaps);
204   }
205
206   gst_object_unref (self);
207
208   return TRUE;
209
210 cannot_change_caps_dog:
211   {
212     gst_object_unref (self);
213     return FALSE;
214   }
215 no_channels:
216   {
217     g_warning ("yarr, shiver me timbers");
218     gst_object_unref (self);
219     return FALSE;
220   }
221 }
222
223 static GstFlowReturn
224 gst_deinterleave_process (GstDeinterleave * self, GstBuffer * buf)
225 {
226   GstFlowReturn ret = GST_FLOW_OK;      /* initialized to silence a warning */
227   GList *srcs;
228   guint bufsize, i, j, channels, pads_pushed, nframes;
229   GstBuffer **buffers_out;
230   gfloat *in, *out;
231
232   channels = self->channels;
233   buffers_out = g_alloca (sizeof (GstBuffer *) * channels);
234   nframes = GST_BUFFER_SIZE (buf) / channels / sizeof (gfloat);
235   bufsize = nframes * sizeof (gfloat);
236   pads_pushed = 0;
237
238   for (i = 0; i < channels; i++)
239     buffers_out[i] = NULL;
240
241   for (srcs = self->srcpads, i = 0; srcs; srcs = srcs->next, i++) {
242     GstPad *pad = (GstPad *) srcs->data;
243
244     buffers_out[i] = NULL;
245     ret = gst_pad_alloc_buffer (pad, -1, bufsize, GST_PAD_CAPS (pad),
246         &buffers_out[i]);
247
248     if (ret != GST_FLOW_OK && ret != GST_FLOW_NOT_LINKED)
249       goto alloc_buffer_failed;
250     if (buffers_out[i] && GST_BUFFER_SIZE (buffers_out[i]) != bufsize)
251       goto alloc_buffer_bad_size;
252
253     if (buffers_out[i])
254       gst_buffer_copy_metadata (buffers_out[i], buf,
255           GST_BUFFER_COPY_TIMESTAMPS);
256   }
257
258   /* do the thing */
259   for (srcs = self->srcpads, i = 0; srcs; srcs = srcs->next, i++) {
260     GstPad *pad = (GstPad *) srcs->data;
261
262     in = (gfloat *) GST_BUFFER_DATA (buf);
263     in += i;                    /* gfloat * arith */
264     if (buffers_out[i]) {
265       out = (gfloat *) GST_BUFFER_DATA (buffers_out[i]);
266       for (j = 0; j < nframes; j++)
267         out[j] = in[j * channels];
268
269       ret = gst_pad_push (pad, buffers_out[i]);
270       buffers_out[i] = NULL;
271       if (ret == GST_FLOW_OK)
272         pads_pushed++;
273       else if (ret == GST_FLOW_NOT_LINKED)
274         ret = GST_FLOW_OK;
275       else
276         goto push_failed;
277     }
278   }
279
280   if (!pads_pushed)
281     ret = GST_FLOW_NOT_LINKED;
282
283   gst_buffer_unref (buf);
284   return ret;
285
286 alloc_buffer_failed:
287   {
288     GST_WARNING ("gst_pad_alloc_buffer() returned %s", gst_flow_get_name (ret));
289     goto clean_buffers;
290
291   }
292 alloc_buffer_bad_size:
293   {
294     GST_WARNING ("called alloc_buffer(), but didn't get requested bytes");
295     ret = GST_FLOW_NOT_NEGOTIATED;
296     goto clean_buffers;
297   }
298 push_failed:
299   {
300     GST_DEBUG ("push() failed, flow = %s", gst_flow_get_name (ret));
301     goto clean_buffers;
302   }
303 clean_buffers:
304   {
305     for (i = 0; i < channels; i++) {
306       if (buffers_out[i])
307         gst_buffer_unref (buffers_out[i]);
308     }
309     gst_buffer_unref (buf);
310     return ret;
311   }
312 }
313
314 static GstFlowReturn
315 gst_deinterleave_chain (GstPad * pad, GstBuffer * buffer)
316 {
317   GstDeinterleave *self;
318   GstFlowReturn ret;
319
320   self = GST_DEINTERLEAVE (GST_PAD_PARENT (pad));
321
322   ret = gst_deinterleave_process (self, buffer);
323
324   if (ret != GST_FLOW_OK)
325     GST_DEBUG_OBJECT (self, "flow: %s", gst_flow_get_name (ret));
326
327   return ret;
328 }
329
330 static gboolean
331 gst_deinterleave_sink_activate_push (GstPad * pad, gboolean active)
332 {
333   GstDeinterleave *self;
334
335   self = GST_DEINTERLEAVE (gst_pad_get_parent (pad));
336
337   if (!active)
338     gst_deinterleave_remove_pads (self);
339
340   gst_object_unref (self);
341
342   return TRUE;
343 }