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 * 2008 Sebastian Dröge <slomo@circular-chaos.org>
8 * deinterleave.c: deinterleave samples
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Library General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Library General Public License for more details.
20 * You should have received a copy of the GNU Library General Public
21 * License along with this library; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301, USA.
27 * - handle changes in number of channels
28 * - handle changes in channel positions
29 * - better capsnego by using a buffer alloc function
30 * and passing downstream caps changes upstream there
34 * SECTION:element-deinterleave
35 * @see_also: interleave
37 * Splits one interleaved multichannel audio stream into many mono audio streams.
39 * This element handles all raw audio formats and supports changing the input caps as long as
40 * all downstream elements can handle the new caps and the number of channels and the channel
41 * positions stay the same. This restriction will be removed in later versions by adding or
42 * removing some source pads as required.
44 * In most cases a queue and an audioconvert element should be added after each source pad
45 * before further processing of the audio data.
48 * <title>Example launch line</title>
50 * gst-launch-1.0 filesrc location=/path/to/file.mp3 ! decodebin ! audioconvert ! "audio/x-raw,channels=2 ! deinterleave name=d d.src_0 ! queue ! audioconvert ! vorbisenc ! oggmux ! filesink location=channel1.ogg d.src_1 ! queue ! audioconvert ! vorbisenc ! oggmux ! filesink location=channel2.ogg
51 * ]| Decodes an MP3 file and encodes the left and right channel into separate
54 * gst-launch-1.0 filesrc location=file.mp3 ! decodebin ! audioconvert ! "audio/x-raw,channels=2" ! deinterleave name=d interleave name=i ! audioconvert ! wavenc ! filesink location=test.wav d.src_0 ! queue ! audioconvert ! i.sink_1 d.src_1 ! queue ! audioconvert ! i.sink_0
55 * ]| Decodes and deinterleaves a Stereo MP3 file into separate channels and
56 * then interleaves the channels again to a WAV file with the channel with the
67 #include "deinterleave.h"
69 GST_DEBUG_CATEGORY_STATIC (gst_deinterleave_debug);
70 #define GST_CAT_DEFAULT gst_deinterleave_debug
72 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src_%u",
75 GST_STATIC_CAPS ("audio/x-raw, "
76 "format = (string) " GST_AUDIO_FORMATS_ALL ", "
77 "rate = (int) [ 1, MAX ], "
78 "channels = (int) 1, layout = (string) {non-interleaved, interleaved}"));
80 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
83 GST_STATIC_CAPS ("audio/x-raw, "
84 "format = (string) " GST_AUDIO_FORMATS_ALL ", "
85 "rate = (int) [ 1, MAX ], "
86 "channels = (int) [ 1, MAX ], layout = (string) interleaved"));
88 #define MAKE_FUNC(type) \
89 static void deinterleave_##type (guint##type *out, guint##type *in, \
90 guint stride, guint nframes) \
94 for (i = 0; i < nframes; i++) { \
106 deinterleave_24 (guint8 * out, guint8 * in, guint stride, guint nframes)
110 for (i = 0; i < nframes; i++) {
117 #define gst_deinterleave_parent_class parent_class
118 G_DEFINE_TYPE (GstDeinterleave, gst_deinterleave, GST_TYPE_ELEMENT);
126 static GstFlowReturn gst_deinterleave_chain (GstPad * pad, GstObject * parent,
129 static gboolean gst_deinterleave_sink_setcaps (GstDeinterleave * self,
132 static GstStateChangeReturn
133 gst_deinterleave_change_state (GstElement * element, GstStateChange transition);
135 static gboolean gst_deinterleave_sink_event (GstPad * pad, GstObject * parent,
137 static gboolean gst_deinterleave_sink_query (GstPad * pad, GstObject * parent,
140 static gboolean gst_deinterleave_src_query (GstPad * pad, GstObject * parent,
143 static void gst_deinterleave_set_property (GObject * object,
144 guint prop_id, const GValue * value, GParamSpec * pspec);
145 static void gst_deinterleave_get_property (GObject * object,
146 guint prop_id, GValue * value, GParamSpec * pspec);
150 gst_deinterleave_finalize (GObject * obj)
152 GstDeinterleave *self = GST_DEINTERLEAVE (obj);
154 if (self->pending_events) {
155 g_list_foreach (self->pending_events, (GFunc) gst_mini_object_unref, NULL);
156 g_list_free (self->pending_events);
157 self->pending_events = NULL;
160 G_OBJECT_CLASS (parent_class)->finalize (obj);
164 gst_deinterleave_class_init (GstDeinterleaveClass * klass)
166 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
167 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
169 GST_DEBUG_CATEGORY_INIT (gst_deinterleave_debug, "deinterleave", 0,
170 "deinterleave element");
172 gst_element_class_set_static_metadata (gstelement_class,
173 "Audio deinterleaver", "Filter/Converter/Audio",
174 "Splits one interleaved multichannel audio stream into many mono audio streams",
175 "Andy Wingo <wingo at pobox.com>, " "Iain <iain@prettypeople.org>, "
176 "Sebastian Dröge <slomo@circular-chaos.org>");
178 gst_element_class_add_static_pad_template (gstelement_class, &sink_template);
179 gst_element_class_add_static_pad_template (gstelement_class, &src_template);
181 gstelement_class->change_state = gst_deinterleave_change_state;
183 gobject_class->finalize = gst_deinterleave_finalize;
184 gobject_class->set_property = gst_deinterleave_set_property;
185 gobject_class->get_property = gst_deinterleave_get_property;
188 * GstDeinterleave:keep-positions
190 * Keep positions: When enable the caps on the output buffers will
191 * contain the original channel positions. This can be used to correctly
192 * interleave the output again later but can also lead to unwanted effects
193 * if the output should be handled as Mono.
196 g_object_class_install_property (gobject_class, PROP_KEEP_POSITIONS,
197 g_param_spec_boolean ("keep-positions", "Keep positions",
198 "Keep the original channel positions on the output buffers",
199 FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
203 gst_deinterleave_init (GstDeinterleave * self)
205 self->keep_positions = FALSE;
207 gst_audio_info_init (&self->audio_info);
210 self->sink = gst_pad_new_from_static_template (&sink_template, "sink");
211 gst_pad_set_chain_function (self->sink,
212 GST_DEBUG_FUNCPTR (gst_deinterleave_chain));
213 gst_pad_set_event_function (self->sink,
214 GST_DEBUG_FUNCPTR (gst_deinterleave_sink_event));
215 gst_pad_set_query_function (self->sink,
216 GST_DEBUG_FUNCPTR (gst_deinterleave_sink_query));
217 gst_element_add_pad (GST_ELEMENT (self), self->sink);
224 } CopyStickyEventsData;
227 copy_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
229 CopyStickyEventsData *data = user_data;
231 if (GST_EVENT_TYPE (*event) >= GST_EVENT_CAPS && data->caps) {
232 gst_pad_set_caps (data->pad, data->caps);
236 if (GST_EVENT_TYPE (*event) != GST_EVENT_CAPS)
237 gst_pad_push_event (data->pad, gst_event_ref (*event));
243 gst_deinterleave_add_new_pads (GstDeinterleave * self, GstCaps * caps)
248 for (i = 0; i < GST_AUDIO_INFO_CHANNELS (&self->audio_info); i++) {
249 gchar *name = g_strdup_printf ("src_%u", i);
252 GstAudioFormat format = GST_AUDIO_INFO_FORMAT (&self->audio_info);
253 gint rate = GST_AUDIO_INFO_RATE (&self->audio_info);
254 GstAudioChannelPosition position = GST_AUDIO_CHANNEL_POSITION_MONO;
255 CopyStickyEventsData data;
257 /* Set channel position if we know it */
258 if (self->keep_positions)
259 position = GST_AUDIO_INFO_POSITION (&self->audio_info, i);
261 gst_audio_info_init (&info);
262 gst_audio_info_set_format (&info, format, rate, 1, &position);
264 srccaps = gst_audio_info_to_caps (&info);
266 pad = gst_pad_new_from_static_template (&src_template, name);
269 gst_pad_use_fixed_caps (pad);
270 gst_pad_set_query_function (pad,
271 GST_DEBUG_FUNCPTR (gst_deinterleave_src_query));
272 gst_pad_set_active (pad, TRUE);
276 gst_pad_sticky_events_foreach (self->sink, copy_sticky_events, &data);
278 gst_pad_set_caps (pad, data.caps);
279 gst_element_add_pad (GST_ELEMENT (self), pad);
280 self->srcpads = g_list_prepend (self->srcpads, gst_object_ref (pad));
282 gst_caps_unref (srccaps);
285 gst_element_no_more_pads (GST_ELEMENT (self));
286 self->srcpads = g_list_reverse (self->srcpads);
290 gst_deinterleave_set_pads_caps (GstDeinterleave * self, GstCaps * caps)
296 for (l = self->srcpads, i = 0; l; l = l->next, i++) {
297 GstPad *pad = GST_PAD (l->data);
301 if (!gst_audio_info_from_caps (&info, caps)) {
305 if (self->keep_positions)
306 GST_AUDIO_INFO_POSITION (&info, 0) =
307 GST_AUDIO_INFO_POSITION (&self->audio_info, i);
309 srccaps = gst_audio_info_to_caps (&info);
311 gst_pad_set_caps (pad, srccaps);
312 gst_caps_unref (srccaps);
318 gst_deinterleave_remove_pads (GstDeinterleave * self)
322 GST_INFO_OBJECT (self, "removing pads");
324 for (l = self->srcpads; l; l = l->next) {
325 GstPad *pad = GST_PAD (l->data);
327 gst_element_remove_pad (GST_ELEMENT_CAST (self), pad);
328 gst_object_unref (pad);
330 g_list_free (self->srcpads);
331 self->srcpads = NULL;
333 gst_caps_replace (&self->sinkcaps, NULL);
337 gst_deinterleave_set_process_function (GstDeinterleave * self)
339 switch (GST_AUDIO_INFO_WIDTH (&self->audio_info)) {
341 self->func = (GstDeinterleaveFunc) deinterleave_8;
344 self->func = (GstDeinterleaveFunc) deinterleave_16;
347 self->func = (GstDeinterleaveFunc) deinterleave_24;
350 self->func = (GstDeinterleaveFunc) deinterleave_32;
353 self->func = (GstDeinterleaveFunc) deinterleave_64;
362 gst_deinterleave_check_caps_change (GstDeinterleave * self,
363 GstAudioInfo * old_info, GstAudioInfo * new_info)
366 gboolean same_layout = TRUE;
367 gboolean was_unpositioned;
368 gboolean is_unpositioned;
372 new_channels = GST_AUDIO_INFO_CHANNELS (new_info);
373 old_channels = GST_AUDIO_INFO_CHANNELS (old_info);
375 if (GST_AUDIO_INFO_IS_UNPOSITIONED (new_info) || new_channels == 1)
376 is_unpositioned = TRUE;
378 is_unpositioned = FALSE;
380 if (GST_AUDIO_INFO_IS_UNPOSITIONED (old_info) || old_channels == 1)
381 was_unpositioned = TRUE;
383 was_unpositioned = FALSE;
385 /* We allow caps changes as long as the number of channels doesn't change
386 * and the channel positions stay the same. _getcaps() should've cared
387 * for this already but better be safe.
389 if (new_channels != old_channels)
390 goto cannot_change_caps;
392 /* Now check the channel positions. If we had no channel positions
393 * and get them or the other way around things have changed.
394 * If we had channel positions and get different ones things have
395 * changed too of course
397 if ((!was_unpositioned && is_unpositioned) || (was_unpositioned
398 && !is_unpositioned))
399 goto cannot_change_caps;
401 if (!is_unpositioned) {
402 if (GST_AUDIO_INFO_CHANNELS (old_info) !=
403 GST_AUDIO_INFO_CHANNELS (new_info))
404 goto cannot_change_caps;
405 for (i = 0; i < GST_AUDIO_INFO_CHANNELS (old_info); i++) {
406 if (new_info->position[i] != old_info->position[i]) {
412 goto cannot_change_caps;
422 gst_deinterleave_sink_setcaps (GstDeinterleave * self, GstCaps * caps)
427 GST_DEBUG_OBJECT (self, "got caps: %" GST_PTR_FORMAT, caps);
429 if (!gst_audio_info_from_caps (&self->audio_info, caps))
432 if (!gst_deinterleave_set_process_function (self))
433 goto unsupported_caps;
435 if (self->sinkcaps && !gst_caps_is_equal (caps, self->sinkcaps)) {
436 GstAudioInfo old_info;
438 gst_audio_info_init (&old_info);
439 if (!gst_audio_info_from_caps (&old_info, self->sinkcaps))
440 goto info_from_caps_failed;
442 if (gst_deinterleave_check_caps_change (self, &old_info, &self->audio_info)) {
443 if (!gst_deinterleave_set_process_function (self))
444 goto cannot_change_caps;
446 goto cannot_change_caps;
450 gst_caps_replace (&self->sinkcaps, caps);
452 /* Get srcpad caps */
453 srccaps = gst_caps_copy (caps);
454 s = gst_caps_get_structure (srccaps, 0);
455 gst_structure_set (s, "channels", G_TYPE_INT, 1, NULL);
456 gst_structure_remove_field (s, "channel-mask");
458 /* If we already have pads, update the caps otherwise
461 if (!gst_deinterleave_set_pads_caps (self, srccaps))
462 goto set_caps_failed;
464 gst_deinterleave_add_new_pads (self, srccaps);
467 gst_caps_unref (srccaps);
473 GST_WARNING_OBJECT (self, "caps change from %" GST_PTR_FORMAT
474 " to %" GST_PTR_FORMAT " not supported: channel number or channel "
475 "positions change", self->sinkcaps, caps);
480 GST_ERROR_OBJECT (self, "caps not supported: %" GST_PTR_FORMAT, caps);
485 GST_ERROR_OBJECT (self, "invalid caps");
490 GST_ERROR_OBJECT (self, "set_caps failed");
491 gst_caps_unref (srccaps);
494 info_from_caps_failed:
496 GST_ERROR_OBJECT (self, "coud not get info from caps");
502 __remove_channels (GstCaps * caps)
507 size = gst_caps_get_size (caps);
508 for (i = 0; i < size; i++) {
509 s = gst_caps_get_structure (caps, i);
510 gst_structure_remove_field (s, "channel-mask");
511 gst_structure_remove_field (s, "channels");
516 __set_channels (GstCaps * caps, gint channels)
521 size = gst_caps_get_size (caps);
522 for (i = 0; i < size; i++) {
523 s = gst_caps_get_structure (caps, i);
525 gst_structure_set (s, "channels", G_TYPE_INT, channels, NULL);
527 gst_structure_set (s, "channels", GST_TYPE_INT_RANGE, 1, G_MAXINT, NULL);
532 gst_deinterleave_sink_acceptcaps (GstPad * pad, GstObject * parent,
535 GstDeinterleave *self = GST_DEINTERLEAVE (parent);
536 GstCaps *templ_caps = gst_pad_get_pad_template_caps (pad);
539 ret = gst_caps_can_intersect (templ_caps, caps);
540 gst_caps_unref (templ_caps);
541 if (ret && self->sinkcaps) {
542 GstAudioInfo new_info;
544 gst_audio_info_init (&new_info);
545 if (!gst_audio_info_from_caps (&new_info, caps))
546 goto info_from_caps_failed;
548 gst_deinterleave_check_caps_change (self, &self->audio_info, &new_info);
553 info_from_caps_failed:
555 GST_ERROR_OBJECT (self, "coud not get info from caps");
561 gst_deinterleave_getcaps (GstPad * pad, GstObject * parent, GstCaps * filter)
563 GstDeinterleave *self = GST_DEINTERLEAVE (parent);
566 GstIteratorResult res;
567 GValue v = G_VALUE_INIT;
569 if (pad != self->sink) {
570 ret = gst_pad_get_current_caps (pad);
574 gst_caps_intersect_full (filter, ret, GST_CAPS_INTERSECT_FIRST);
575 gst_caps_unref (ret);
582 /* Intersect all of our pad template caps with the peer caps of the pad
583 * to get all formats that are possible up- and downstream.
585 * For the pad for which the caps are requested we don't remove the channel
586 * informations as they must be in the returned caps and incompatibilities
587 * will be detected here already
589 ret = gst_caps_new_any ();
590 it = gst_element_iterate_pads (GST_ELEMENT_CAST (self));
593 res = gst_iterator_next (it, &v);
595 case GST_ITERATOR_OK:{
596 GstPad *ourpad = GST_PAD (g_value_get_object (&v));
597 GstCaps *peercaps = NULL, *ourcaps;
598 GstCaps *templ_caps = gst_pad_get_pad_template_caps (ourpad);
600 ourcaps = gst_caps_copy (templ_caps);
601 gst_caps_unref (templ_caps);
604 if (GST_PAD_DIRECTION (pad) == GST_PAD_SINK)
605 __set_channels (ourcaps,
606 GST_AUDIO_INFO_CHANNELS (&self->audio_info));
608 __set_channels (ourcaps, 1);
610 __remove_channels (ourcaps);
611 /* Only ask for peer caps for other pads than pad
612 * as otherwise gst_pad_peer_get_caps() might call
613 * back into this function and deadlock
615 peercaps = gst_pad_peer_query_caps (ourpad, NULL);
616 peercaps = gst_caps_make_writable (peercaps);
619 /* If the peer exists and has caps add them to the intersection,
620 * otherwise assume that the peer accepts everything */
622 GstCaps *intersection;
623 GstCaps *oldret = ret;
625 __remove_channels (peercaps);
627 intersection = gst_caps_intersect (peercaps, ourcaps);
629 ret = gst_caps_intersect (ret, intersection);
630 gst_caps_unref (intersection);
631 gst_caps_unref (peercaps);
632 gst_caps_unref (oldret);
634 GstCaps *oldret = ret;
636 ret = gst_caps_intersect (ret, ourcaps);
637 gst_caps_unref (oldret);
639 gst_caps_unref (ourcaps);
643 case GST_ITERATOR_DONE:
645 case GST_ITERATOR_ERROR:
646 gst_caps_unref (ret);
647 ret = gst_caps_new_empty ();
649 case GST_ITERATOR_RESYNC:
650 gst_caps_unref (ret);
651 ret = gst_caps_new_any ();
652 gst_iterator_resync (it);
655 } while (res != GST_ITERATOR_DONE && res != GST_ITERATOR_ERROR);
657 gst_iterator_free (it);
662 aux = gst_caps_intersect_full (filter, ret, GST_CAPS_INTERSECT_FIRST);
663 gst_caps_unref (ret);
667 GST_DEBUG_OBJECT (pad, "Intersected caps to %" GST_PTR_FORMAT, ret);
673 gst_deinterleave_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
675 GstDeinterleave *self = GST_DEINTERLEAVE (parent);
678 GST_DEBUG ("Got %s event on pad %s:%s", GST_EVENT_TYPE_NAME (event),
679 GST_DEBUG_PAD_NAME (pad));
681 /* Send FLUSH_STOP, FLUSH_START and EOS immediately, no matter if
682 * we have src pads already or not. Queue all other events and
683 * push them after we have src pads
685 switch (GST_EVENT_TYPE (event)) {
686 case GST_EVENT_FLUSH_STOP:
687 case GST_EVENT_FLUSH_START:
689 ret = gst_pad_event_default (pad, parent, event);
695 gst_event_parse_caps (event, &caps);
696 ret = gst_deinterleave_sink_setcaps (self, caps);
697 gst_event_unref (event);
702 if (!self->srcpads && !GST_EVENT_IS_STICKY (event)) {
703 /* Sticky events are copied when creating a new pad */
704 GST_OBJECT_LOCK (self);
705 self->pending_events = g_list_append (self->pending_events, event);
706 GST_OBJECT_UNLOCK (self);
709 ret = gst_pad_event_default (pad, parent, event);
718 gst_deinterleave_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
722 switch (GST_QUERY_TYPE (query)) {
723 case GST_QUERY_CAPS:{
727 gst_query_parse_caps (query, &filter);
728 caps = gst_deinterleave_getcaps (pad, parent, filter);
729 gst_query_set_caps_result (query, caps);
730 gst_caps_unref (caps);
734 case GST_QUERY_ACCEPT_CAPS:{
738 gst_query_parse_accept_caps (query, &caps);
739 ret = gst_deinterleave_sink_acceptcaps (pad, parent, caps);
740 gst_query_set_accept_caps_result (query, ret);
745 res = gst_pad_query_default (pad, parent, query);
753 gst_deinterleave_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
755 GstDeinterleave *self = GST_DEINTERLEAVE (parent);
758 res = gst_pad_query_default (pad, parent, query);
760 if (res && GST_QUERY_TYPE (query) == GST_QUERY_DURATION) {
764 gst_query_parse_duration (query, &format, &dur);
766 /* Need to divide by the number of channels in byte format
767 * to get the correct value. All other formats should be fine
769 if (format == GST_FORMAT_BYTES && dur != -1)
770 gst_query_set_duration (query, format,
771 dur / GST_AUDIO_INFO_CHANNELS (&self->audio_info));
772 } else if (res && GST_QUERY_TYPE (query) == GST_QUERY_POSITION) {
776 gst_query_parse_position (query, &format, &pos);
778 /* Need to divide by the number of channels in byte format
779 * to get the correct value. All other formats should be fine
781 if (format == GST_FORMAT_BYTES && pos != -1)
782 gst_query_set_position (query, format,
783 pos / GST_AUDIO_INFO_CHANNELS (&self->audio_info));
784 } else if (res && GST_QUERY_TYPE (query) == GST_QUERY_CAPS) {
785 GstCaps *filter, *caps;
787 gst_query_parse_caps (query, &filter);
788 caps = gst_deinterleave_getcaps (pad, parent, filter);
789 gst_query_set_caps_result (query, caps);
790 gst_caps_unref (caps);
797 gst_deinterleave_set_property (GObject * object, guint prop_id,
798 const GValue * value, GParamSpec * pspec)
800 GstDeinterleave *self = GST_DEINTERLEAVE (object);
803 case PROP_KEEP_POSITIONS:
804 self->keep_positions = g_value_get_boolean (value);
807 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
813 gst_deinterleave_get_property (GObject * object, guint prop_id,
814 GValue * value, GParamSpec * pspec)
816 GstDeinterleave *self = GST_DEINTERLEAVE (object);
819 case PROP_KEEP_POSITIONS:
820 g_value_set_boolean (value, self->keep_positions);
823 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
829 gst_deinterleave_process (GstDeinterleave * self, GstBuffer * buf)
831 GstFlowReturn ret = GST_FLOW_OK;
832 guint channels = GST_AUDIO_INFO_CHANNELS (&self->audio_info);
833 guint pads_pushed = 0, buffers_allocated = 0;
835 gst_buffer_get_size (buf) / channels /
836 (GST_AUDIO_INFO_WIDTH (&self->audio_info) / 8);
837 guint bufsize = nframes * (GST_AUDIO_INFO_WIDTH (&self->audio_info) / 8);
840 GstBuffer **buffers_out = g_new0 (GstBuffer *, channels);
842 GstMapInfo read_info;
843 GList *pending_events, *l;
845 /* Send any pending events to all src pads */
846 GST_OBJECT_LOCK (self);
847 pending_events = self->pending_events;
848 self->pending_events = NULL;
849 GST_OBJECT_UNLOCK (self);
851 if (pending_events) {
854 GST_DEBUG_OBJECT (self, "Sending pending events to all src pads");
855 for (l = pending_events; l; l = l->next) {
857 for (srcs = self->srcpads; srcs != NULL; srcs = srcs->next)
858 gst_pad_push_event (GST_PAD (srcs->data), gst_event_ref (event));
859 gst_event_unref (event);
861 g_list_free (pending_events);
864 gst_buffer_map (buf, &read_info, GST_MAP_READ);
866 /* Allocate buffers */
867 for (srcs = self->srcpads, i = 0; srcs; srcs = srcs->next, i++) {
868 buffers_out[i] = gst_buffer_new_allocate (NULL, bufsize, NULL);
870 /* Make sure we got a correct buffer. The only other case we allow
871 * here is an unliked pad */
873 goto alloc_buffer_failed;
874 else if (buffers_out[i]
875 && gst_buffer_get_size (buffers_out[i]) != bufsize)
876 goto alloc_buffer_bad_size;
878 if (buffers_out[i]) {
879 gst_buffer_copy_into (buffers_out[i], buf, GST_BUFFER_COPY_METADATA, 0,
885 /* Return NOT_LINKED if no pad was linked */
886 if (!buffers_allocated) {
887 GST_WARNING_OBJECT (self,
888 "Couldn't allocate any buffers because no pad was linked");
889 ret = GST_FLOW_NOT_LINKED;
894 for (srcs = self->srcpads, i = 0; srcs; srcs = srcs->next, i++) {
895 GstPad *pad = (GstPad *) srcs->data;
896 GstMapInfo write_info;
898 in = (guint8 *) read_info.data;
899 in += i * (GST_AUDIO_INFO_WIDTH (&self->audio_info) / 8);
900 if (buffers_out[i]) {
901 gst_buffer_map (buffers_out[i], &write_info, GST_MAP_WRITE);
902 out = (guint8 *) write_info.data;
903 self->func (out, in, channels, nframes);
904 gst_buffer_unmap (buffers_out[i], &write_info);
906 ret = gst_pad_push (pad, buffers_out[i]);
907 buffers_out[i] = NULL;
908 if (ret == GST_FLOW_OK)
910 else if (ret == GST_FLOW_NOT_LINKED)
917 /* Return NOT_LINKED if no pad was linked */
919 ret = GST_FLOW_NOT_LINKED;
921 GST_DEBUG_OBJECT (self, "Pushed on %d pads", pads_pushed);
924 gst_buffer_unmap (buf, &read_info);
925 gst_buffer_unref (buf);
926 g_free (buffers_out);
931 GST_WARNING ("gst_pad_alloc_buffer() returned %s", gst_flow_get_name (ret));
935 alloc_buffer_bad_size:
937 GST_WARNING ("called alloc_buffer(), but didn't get requested bytes");
938 ret = GST_FLOW_NOT_NEGOTIATED;
943 GST_DEBUG ("push() failed, flow = %s", gst_flow_get_name (ret));
948 gst_buffer_unmap (buf, &read_info);
949 for (i = 0; i < channels; i++) {
951 gst_buffer_unref (buffers_out[i]);
953 gst_buffer_unref (buf);
954 g_free (buffers_out);
960 gst_deinterleave_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
962 GstDeinterleave *self = GST_DEINTERLEAVE (parent);
965 g_return_val_if_fail (self->func != NULL, GST_FLOW_NOT_NEGOTIATED);
966 g_return_val_if_fail (GST_AUDIO_INFO_WIDTH (&self->audio_info) > 0,
967 GST_FLOW_NOT_NEGOTIATED);
968 g_return_val_if_fail (GST_AUDIO_INFO_CHANNELS (&self->audio_info) > 0,
969 GST_FLOW_NOT_NEGOTIATED);
971 ret = gst_deinterleave_process (self, buffer);
973 if (ret != GST_FLOW_OK)
974 GST_DEBUG_OBJECT (self, "flow return: %s", gst_flow_get_name (ret));
979 static GstStateChangeReturn
980 gst_deinterleave_change_state (GstElement * element, GstStateChange transition)
982 GstStateChangeReturn ret;
983 GstDeinterleave *self = GST_DEINTERLEAVE (element);
985 switch (transition) {
986 case GST_STATE_CHANGE_NULL_TO_READY:
988 case GST_STATE_CHANGE_READY_TO_PAUSED:
989 gst_deinterleave_remove_pads (self);
993 if (self->pending_events) {
994 g_list_foreach (self->pending_events, (GFunc) gst_mini_object_unref,
996 g_list_free (self->pending_events);
997 self->pending_events = NULL;
1000 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1006 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1008 switch (transition) {
1009 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1011 case GST_STATE_CHANGE_PAUSED_TO_READY:
1012 gst_deinterleave_remove_pads (self);
1016 if (self->pending_events) {
1017 g_list_foreach (self->pending_events, (GFunc) gst_mini_object_unref,
1019 g_list_free (self->pending_events);
1020 self->pending_events = NULL;
1023 case GST_STATE_CHANGE_READY_TO_NULL: