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>
7 * deinterleave.c: deinterleave samples, based on interleave.c
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.
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.
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.
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))
41 typedef struct _GstDeinterleave GstDeinterleave;
42 typedef struct _GstDeinterleaveClass GstDeinterleaveClass;
45 struct _GstDeinterleave
57 struct _GstDeinterleaveClass
59 GstElementClass parent_class;
63 GST_DEBUG_CATEGORY_STATIC (gst_deinterleave_debug);
64 #define GST_CAT_DEFAULT gst_deinterleave_debug
67 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src%d",
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")
75 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
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")
85 GST_BOILERPLATE (GstDeinterleave, gst_deinterleave, GstElement,
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,
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>");
102 gst_deinterleave_base_init (gpointer g_class)
104 GST_DEBUG_CATEGORY_INIT (gst_deinterleave_debug, "interleave", 0,
105 "interleave element");
107 gst_element_class_set_details (g_class, &details);
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));
116 gst_deinterleave_class_init (GstDeinterleaveClass * klass)
122 gst_deinterleave_init (GstDeinterleave * self, GstDeinterleaveClass * klass)
124 self->sink = gst_pad_new_from_static_template (&sink_template, "sink");
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));
133 gst_element_add_pad (GST_ELEMENT (self), self->sink);
137 gst_deinterleave_add_new_pads (GstDeinterleave * self, GstCaps * caps)
142 for (i = 0; i < self->channels; i++) {
143 gchar *name = g_strdup_printf ("src%d", i);
145 pad = gst_pad_new_from_static_template (&src_template, 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));
154 gst_element_no_more_pads (GST_ELEMENT (self));
155 self->srcpads = g_list_reverse (self->srcpads);
159 gst_deinterleave_remove_pads (GstDeinterleave * self)
163 GST_INFO_OBJECT (self, "removing pads");
165 for (l = self->srcpads; l; l = l->next) {
166 GstPad *pad = GST_PAD (l->data);
168 gst_element_remove_pad (GST_ELEMENT_CAST (self), pad);
169 gst_object_unref (pad);
171 g_list_free (self->srcpads);
172 self->srcpads = NULL;
174 gst_pad_set_caps (self->sink, NULL);
175 gst_caps_replace (&self->sinkcaps, NULL);
179 gst_deinterleave_sink_setcaps (GstPad * pad, GstCaps * caps)
181 GstDeinterleave *self;
183 self = GST_DEINTERLEAVE (gst_pad_get_parent (pad));
185 if (self->sinkcaps && !gst_caps_is_equal (caps, self->sinkcaps)) {
186 goto cannot_change_caps_dog;
188 GST_DEBUG_OBJECT (self, "got caps: %" GST_PTR_FORMAT, caps);
189 gst_caps_replace (&self->sinkcaps, caps);
196 srccaps = gst_caps_copy (caps);
197 s = gst_caps_get_structure (srccaps, 0);
198 if (!gst_structure_get_int (s, "channels", &self->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);
206 gst_object_unref (self);
210 cannot_change_caps_dog:
212 gst_object_unref (self);
217 g_warning ("yarr, shiver me timbers");
218 gst_object_unref (self);
224 gst_deinterleave_process (GstDeinterleave * self, GstBuffer * buf)
226 GstFlowReturn ret = GST_FLOW_OK; /* initialized to silence a warning */
228 guint bufsize, i, j, channels, pads_pushed, nframes;
229 GstBuffer **buffers_out;
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);
238 for (i = 0; i < channels; i++)
239 buffers_out[i] = NULL;
241 for (srcs = self->srcpads, i = 0; srcs; srcs = srcs->next, i++) {
242 GstPad *pad = (GstPad *) srcs->data;
244 buffers_out[i] = NULL;
245 ret = gst_pad_alloc_buffer (pad, -1, bufsize, GST_PAD_CAPS (pad),
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;
254 gst_buffer_copy_metadata (buffers_out[i], buf,
255 GST_BUFFER_COPY_TIMESTAMPS);
259 for (srcs = self->srcpads, i = 0; srcs; srcs = srcs->next, i++) {
260 GstPad *pad = (GstPad *) srcs->data;
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];
269 ret = gst_pad_push (pad, buffers_out[i]);
270 buffers_out[i] = NULL;
271 if (ret == GST_FLOW_OK)
273 else if (ret == GST_FLOW_NOT_LINKED)
281 ret = GST_FLOW_NOT_LINKED;
283 gst_buffer_unref (buf);
288 GST_WARNING ("gst_pad_alloc_buffer() returned %s", gst_flow_get_name (ret));
292 alloc_buffer_bad_size:
294 GST_WARNING ("called alloc_buffer(), but didn't get requested bytes");
295 ret = GST_FLOW_NOT_NEGOTIATED;
300 GST_DEBUG ("push() failed, flow = %s", gst_flow_get_name (ret));
305 for (i = 0; i < channels; i++) {
307 gst_buffer_unref (buffers_out[i]);
309 gst_buffer_unref (buf);
315 gst_deinterleave_chain (GstPad * pad, GstBuffer * buffer)
317 GstDeinterleave *self;
320 self = GST_DEINTERLEAVE (GST_PAD_PARENT (pad));
322 ret = gst_deinterleave_process (self, buffer);
324 if (ret != GST_FLOW_OK)
325 GST_DEBUG_OBJECT (self, "flow: %s", gst_flow_get_name (ret));
331 gst_deinterleave_sink_activate_push (GstPad * pad, gboolean active)
333 GstDeinterleave *self;
335 self = GST_DEINTERLEAVE (gst_pad_get_parent (pad));
338 gst_deinterleave_remove_pads (self);
340 gst_object_unref (self);