gst/interleave/deinterleave.c (gst_deinterleave_add_new_pads): Use fixed caps on...
[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   GstCaps *sinkcaps;
50   gint channels;
51
52   GstPad *sink;
53 };
54
55 struct _GstDeinterleaveClass
56 {
57   GstElementClass parent_class;
58 };
59
60
61 GST_DEBUG_CATEGORY_STATIC (gst_deinterleave_debug);
62 #define GST_CAT_DEFAULT gst_deinterleave_debug
63
64
65 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src%d",
66     GST_PAD_SRC,
67     GST_PAD_SOMETIMES,
68     GST_STATIC_CAPS ("audio/x-raw-float, "
69         "rate = (int) [ 1, MAX ], "
70         "channels = (int) 1, "
71         "endianness = (int) BYTE_ORDER, " "width = (int) 32")
72     );
73 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
74     GST_PAD_SINK,
75     GST_PAD_ALWAYS,
76     GST_STATIC_CAPS ("audio/x-raw-float, "
77         "rate = (int) [ 1, MAX ], "
78         "channels = (int) [ 1, MAX ], "
79         "endianness = (int) BYTE_ORDER, " "width = (int) 32")
80     );
81
82
83 GST_BOILERPLATE (GstDeinterleave, gst_deinterleave, GstElement,
84     GST_TYPE_ELEMENT);
85
86
87 static GstFlowReturn gst_deinterleave_chain (GstPad * pad, GstBuffer * buffer);
88 static gboolean gst_deinterleave_sink_setcaps (GstPad * pad, GstCaps * caps);
89 static gboolean gst_deinterleave_sink_activate_push (GstPad * pad,
90     gboolean active);
91
92
93 static const GstElementDetails details =
94 GST_ELEMENT_DETAILS ("Audio deinterleaver",
95     "Filter/Converter/Audio",
96     "Splits one interleaved multichannel audio stream into many mono audio streams",
97     "Andy Wingo <wingo at pobox.com>, " "Iain <iain@prettypeople.org>");
98
99 static void
100 gst_deinterleave_base_init (gpointer g_class)
101 {
102   GST_DEBUG_CATEGORY_INIT (gst_deinterleave_debug, "interleave", 0,
103       "interleave element");
104
105   gst_element_class_set_details (g_class, &details);
106
107   gst_element_class_add_pad_template (g_class,
108       gst_static_pad_template_get (&sink_template));
109   gst_element_class_add_pad_template (g_class,
110       gst_static_pad_template_get (&src_template));
111 }
112
113 static void
114 gst_deinterleave_class_init (GstDeinterleaveClass * klass)
115 {
116   /* pass */
117 }
118
119 static void
120 gst_deinterleave_init (GstDeinterleave * self, GstDeinterleaveClass * klass)
121 {
122   self->sink = gst_pad_new_from_static_template (&sink_template, "sink");
123
124   gst_pad_set_chain_function (self->sink,
125       GST_DEBUG_FUNCPTR (gst_deinterleave_chain));
126   gst_pad_set_setcaps_function (self->sink,
127       GST_DEBUG_FUNCPTR (gst_deinterleave_sink_setcaps));
128   gst_pad_set_activatepush_function (self->sink,
129       GST_DEBUG_FUNCPTR (gst_deinterleave_sink_activate_push));
130
131   gst_element_add_pad (GST_ELEMENT (self), self->sink);
132 }
133
134 static void
135 gst_deinterleave_add_new_pads (GstDeinterleave * self, GstCaps * caps)
136 {
137   GstPad *pad;
138   guint i;
139
140   for (i = 0; i < self->channels; i++) {
141     gchar *name = g_strdup_printf ("src%d", i);
142
143     pad = gst_pad_new_from_static_template (&src_template, name);
144     g_free (name);
145     gst_pad_set_caps (pad, caps);
146     gst_pad_use_fixed_caps (pad);
147     GST_PAD_UNSET_FLUSHING (pad);
148     gst_element_add_pad (GST_ELEMENT (self), pad);
149   }
150
151   gst_element_no_more_pads (GST_ELEMENT (self));
152 }
153
154 static void
155 gst_deinterleave_remove_pads (GstDeinterleave * self)
156 {
157   GstElement *elem;
158   GList *srcs, *l;
159
160   elem = GST_ELEMENT (self);
161
162   GST_INFO_OBJECT (self, "remove_pads()");
163
164   srcs = g_list_copy (elem->srcpads);
165
166   for (l = srcs; l; l = l->next)
167     gst_element_remove_pad (elem, GST_PAD (l->data));
168
169   gst_pad_set_caps (self->sink, NULL);
170   gst_caps_replace (&self->sinkcaps, NULL);
171
172   g_list_free (srcs);
173 }
174
175 static gboolean
176 gst_deinterleave_sink_setcaps (GstPad * pad, GstCaps * caps)
177 {
178   GstDeinterleave *self;
179
180   self = GST_DEINTERLEAVE (gst_pad_get_parent (pad));
181
182   if (self->sinkcaps && !gst_caps_is_equal (caps, self->sinkcaps)) {
183     goto cannot_change_caps_dog;
184   } else {
185     GST_DEBUG_OBJECT (self, "got caps: %" GST_PTR_FORMAT, caps);
186     gst_caps_replace (&self->sinkcaps, caps);
187   }
188
189   {
190     GstCaps *srccaps;
191     GstStructure *s;
192
193     srccaps = gst_caps_copy (caps);
194     s = gst_caps_get_structure (srccaps, 0);
195     if (!gst_structure_get_int (s, "channels", &self->channels))
196       goto no_channels;
197     gst_structure_set (s, "channels", G_TYPE_INT, 1, NULL);
198     gst_deinterleave_add_new_pads (self, srccaps);
199     gst_caps_unref (srccaps);
200   }
201
202   gst_object_unref (self);
203
204   return TRUE;
205
206 cannot_change_caps_dog:
207   {
208     gst_object_unref (self);
209     return FALSE;
210   }
211 no_channels:
212   {
213     g_warning ("yarr, shiver me timbers");
214     gst_object_unref (self);
215     return FALSE;
216   }
217 }
218
219 static GstFlowReturn
220 gst_deinterleave_process (GstDeinterleave * self, GstBuffer * buf)
221 {
222   GstFlowReturn ret;
223   GstElement *elem;
224   GList *srcs;
225   guint bufsize, i, j, channels, pads_pushed, nframes;
226   GstBuffer **buffers_out;
227   gfloat *in, *out;
228
229   elem = GST_ELEMENT (self);
230
231   channels = self->channels;
232   buffers_out = g_alloca (sizeof (GstBuffer *) * channels);
233   nframes = GST_BUFFER_SIZE (buf) / channels / sizeof (gfloat);
234   bufsize = nframes * sizeof (gfloat);
235   pads_pushed = 0;
236
237   for (i = 0; i < channels; i++)
238     buffers_out[i] = NULL;
239
240   for (srcs = elem->srcpads, i = 0; srcs; srcs = srcs->next, i++) {
241     GstPad *pad = (GstPad *) srcs->data;
242
243     buffers_out[i] = NULL;
244     ret = gst_pad_alloc_buffer (pad, -1, bufsize, GST_PAD_CAPS (pad),
245         &buffers_out[i]);
246
247     if (ret != GST_FLOW_OK && ret != GST_FLOW_NOT_LINKED)
248       goto alloc_buffer_failed;
249     if (buffers_out[i] && GST_BUFFER_SIZE (buffers_out[i]) != bufsize)
250       goto alloc_buffer_bad_size;
251
252     if (buffers_out[i]) {
253       GST_BUFFER_TIMESTAMP (buffers_out[i]) = GST_BUFFER_TIMESTAMP (buf);
254       GST_BUFFER_OFFSET (buffers_out[i]) = GST_BUFFER_OFFSET (buf);
255       GST_BUFFER_DURATION (buffers_out[i]) = GST_BUFFER_DURATION (buf);
256     }
257   }
258
259   /* do the thing */
260   for (srcs = elem->srcpads, i = 0; srcs; srcs = srcs->next, i++) {
261     GstPad *pad = (GstPad *) srcs->data;
262
263     in = (gfloat *) GST_BUFFER_DATA (buf);
264     in += i;                    /* gfloat * arith */
265     if (buffers_out[i]) {
266       out = (gfloat *) GST_BUFFER_DATA (buffers_out[i]);
267       for (j = 0; j < nframes; j++)
268         out[j] = in[j * channels];
269
270       ret = gst_pad_push (pad, buffers_out[i]);
271       buffers_out[i] = NULL;
272       if (ret == GST_FLOW_OK)
273         pads_pushed++;
274       else if (ret == GST_FLOW_NOT_LINKED)
275         ret = GST_FLOW_OK;
276       else
277         goto push_failed;
278     }
279   }
280
281   if (!pads_pushed)
282     ret = GST_FLOW_NOT_LINKED;
283
284   return ret;
285
286 alloc_buffer_failed:
287   {
288     GST_WARNING ("gst_pad_alloc_buffer() returned %d", 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");
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     return ret;
309   }
310 }
311
312 static GstFlowReturn
313 gst_deinterleave_chain (GstPad * pad, GstBuffer * buffer)
314 {
315   GstDeinterleave *self;
316   GstFlowReturn ret;
317
318   self = GST_DEINTERLEAVE (gst_pad_get_parent (pad));
319
320   ret = gst_deinterleave_process (self, buffer);
321
322   if (ret != GST_FLOW_OK)
323     GST_WARNING_OBJECT (self, "process failed");
324
325   gst_object_unref (self);
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 }