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.rg>
8 * interleave.c: interleave samples, mostly based on adder.
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 caps changes
28 * - handle more queries/events
32 * SECTION:element-interleave
33 * @see_also: deinterleave
35 * Merges separate mono inputs into one interleaved stream.
37 * This element handles all raw floating point sample formats and all signed integer sample formats. The first
38 * caps on one of the sinkpads will set the caps of the output so usually an audioconvert element should be
39 * placed before every sinkpad of interleave.
41 * It's possible to change the number of channels while the pipeline is running by adding or removing
42 * some of the request pads but this will change the caps of the output buffers. Changing the input
43 * caps is _not_ supported yet.
45 * The channel number of every sinkpad in the out can be retrieved from the "channel" property of the pad.
48 * <title>Example launch line</title>
50 * 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
51 * ]| Decodes and deinterleaves a Stereo MP3 file into separate channels and
52 * then interleaves the channels again to a WAV file with the channels
55 * gst-launch-1.0 interleave name=i ! audioconvert ! wavenc ! filesink location=file.wav filesrc location=file1.wav ! decodebin ! audioconvert ! "audio/x-raw,channels=1,channel-mask=(bitmask)0x1" ! queue ! i.sink_0 filesrc location=file2.wav ! decodebin ! audioconvert ! "audio/x-raw,channels=1,channel-mask=(bitmask)0x2" ! queue ! i.sink_1
56 * ]| Interleaves two Mono WAV files to a single Stereo WAV file. Having
57 * channel-masks defined in the sink pads ensures a sane mapping of the mono
58 * streams into the stereo stream. NOTE: the proper way to map channels in
59 * code is by using the channel-positions property of the interleave element.
63 /* FIXME 0.11: suppress warnings for deprecated API such as GValueArray
64 * with newer GLib versions (>= 2.31.0) */
65 #define GLIB_DISABLE_DEPRECATION_WARNINGS
73 #include "interleave.h"
75 #include <gst/audio/audio.h>
76 #include <gst/audio/audio-enumtypes.h>
78 GST_DEBUG_CATEGORY_STATIC (gst_interleave_debug);
79 #define GST_CAT_DEFAULT gst_interleave_debug
81 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink_%u",
84 GST_STATIC_CAPS ("audio/x-raw, "
85 "rate = (int) [ 1, MAX ], "
86 "channels = (int) 1, "
87 "format = (string) " GST_AUDIO_FORMATS_ALL ", "
88 "layout = (string) {non-interleaved, interleaved}")
91 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
94 GST_STATIC_CAPS ("audio/x-raw, "
95 "rate = (int) [ 1, MAX ], "
96 "channels = (int) [ 1, MAX ], "
97 "format = (string) " GST_AUDIO_FORMATS_ALL ", "
98 "layout = (string) interleaved")
101 #define MAKE_FUNC(type) \
102 static void interleave_##type (guint##type *out, guint##type *in, \
103 guint stride, guint nframes) \
107 for (i = 0; i < nframes; i++) { \
119 interleave_24 (guint8 * out, guint8 * in, guint stride, guint nframes)
123 for (i = 0; i < nframes; i++) {
142 static void gst_interleave_pad_class_init (GstPadClass * klass);
144 #define GST_TYPE_INTERLEAVE_PAD (gst_interleave_pad_get_type())
145 #define GST_INTERLEAVE_PAD(pad) (G_TYPE_CHECK_INSTANCE_CAST((pad),GST_TYPE_INTERLEAVE_PAD,GstInterleavePad))
146 #define GST_INTERLEAVE_PAD_CAST(pad) ((GstInterleavePad *) pad)
147 #define GST_IS_INTERLEAVE_PAD(pad) (G_TYPE_CHECK_INSTANCE_TYPE((pad),GST_TYPE_INTERLEAVE_PAD))
149 gst_interleave_pad_get_type (void)
151 static GType type = 0;
153 if (G_UNLIKELY (type == 0)) {
154 type = g_type_register_static_simple (GST_TYPE_PAD,
155 g_intern_static_string ("GstInterleavePad"), sizeof (GstPadClass),
156 (GClassInitFunc) gst_interleave_pad_class_init,
157 sizeof (GstInterleavePad), NULL, 0);
163 gst_interleave_pad_get_property (GObject * object,
164 guint prop_id, GValue * value, GParamSpec * pspec)
166 GstInterleavePad *self = GST_INTERLEAVE_PAD (object);
169 case PROP_PAD_CHANNEL:
170 g_value_set_uint (value, self->channel);
173 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
179 gst_interleave_pad_class_init (GstPadClass * klass)
181 GObjectClass *gobject_class = (GObjectClass *) klass;
183 gobject_class->get_property = gst_interleave_pad_get_property;
185 g_object_class_install_property (gobject_class,
187 g_param_spec_uint ("channel",
189 "Number of the channel of this pad in the output", 0, G_MAXUINT, 0,
190 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
193 #define gst_interleave_parent_class parent_class
194 G_DEFINE_TYPE (GstInterleave, gst_interleave, GST_TYPE_ELEMENT);
199 PROP_CHANNEL_POSITIONS,
200 PROP_CHANNEL_POSITIONS_FROM_INPUT
203 static void gst_interleave_set_property (GObject * object,
204 guint prop_id, const GValue * value, GParamSpec * pspec);
205 static void gst_interleave_get_property (GObject * object,
206 guint prop_id, GValue * value, GParamSpec * pspec);
208 static GstPad *gst_interleave_request_new_pad (GstElement * element,
209 GstPadTemplate * templ, const gchar * name, const GstCaps * caps);
210 static void gst_interleave_release_pad (GstElement * element, GstPad * pad);
212 static GstStateChangeReturn gst_interleave_change_state (GstElement * element,
213 GstStateChange transition);
215 static gboolean gst_interleave_src_query (GstPad * pad, GstObject * parent,
218 static gboolean gst_interleave_src_event (GstPad * pad, GstObject * parent,
221 static gboolean gst_interleave_sink_event (GstCollectPads * pads,
222 GstCollectData * data, GstEvent * event, gpointer user_data);
223 static gboolean gst_interleave_sink_query (GstCollectPads * pads,
224 GstCollectData * data, GstQuery * query, gpointer user_data);
226 static gboolean gst_interleave_sink_setcaps (GstInterleave * self,
227 GstPad * pad, const GstCaps * caps, const GstAudioInfo * info);
229 static GstCaps *gst_interleave_sink_getcaps (GstPad * pad, GstInterleave * self,
232 static GstFlowReturn gst_interleave_collected (GstCollectPads * pads,
233 GstInterleave * self);
236 gst_interleave_finalize (GObject * object)
238 GstInterleave *self = GST_INTERLEAVE (object);
241 gst_object_unref (self->collect);
242 self->collect = NULL;
245 if (self->channel_positions
246 && self->channel_positions != self->input_channel_positions) {
247 g_value_array_free (self->channel_positions);
248 self->channel_positions = NULL;
251 if (self->input_channel_positions) {
252 g_value_array_free (self->input_channel_positions);
253 self->input_channel_positions = NULL;
256 gst_caps_replace (&self->sinkcaps, NULL);
258 G_OBJECT_CLASS (parent_class)->finalize (object);
262 compare_positions (gconstpointer a, gconstpointer b, gpointer user_data)
264 const gint i = *(const gint *) a;
265 const gint j = *(const gint *) b;
266 const gint *pos = (const gint *) user_data;
270 else if (pos[i] > pos[j])
277 gst_interleave_channel_positions_to_mask (GValueArray * positions,
278 gint default_ordering_map[64], guint64 * mask)
282 GstAudioChannelPosition *pos;
285 channels = positions->n_values;
286 pos = g_new (GstAudioChannelPosition, channels);
288 for (i = 0; i < channels; i++) {
291 val = g_value_array_get_nth (positions, i);
292 pos[i] = g_value_get_enum (val);
295 /* sort the default ordering map according to the position order */
296 for (i = 0; i < channels; i++) {
297 default_ordering_map[i] = i;
299 g_qsort_with_data (default_ordering_map, channels,
300 sizeof (*default_ordering_map), compare_positions, pos);
302 ret = gst_audio_channel_positions_to_mask (pos, channels, FALSE, mask);
309 gst_interleave_set_channel_positions (GstInterleave * self, GstStructure * s)
311 if (self->channels <= 64 &&
312 self->channel_positions != NULL &&
313 self->channels == self->channel_positions->n_values) {
314 if (!gst_interleave_channel_positions_to_mask (self->channel_positions,
315 self->default_channels_ordering_map, &self->channel_mask)) {
316 GST_WARNING_OBJECT (self, "Invalid channel positions, using NONE");
317 self->channel_mask = 0;
320 self->channel_mask = 0;
321 if (self->channels <= 64) {
322 GST_WARNING_OBJECT (self, "Using NONE channel positions");
325 gst_structure_set (s, "channel-mask", GST_TYPE_BITMASK, self->channel_mask,
330 gst_interleave_send_stream_start (GstInterleave * self)
332 GST_OBJECT_LOCK (self);
333 if (self->send_stream_start) {
336 self->send_stream_start = FALSE;
337 GST_OBJECT_UNLOCK (self);
339 /* stream-start (FIXME: create id based on input ids) */
340 g_snprintf (s_id, sizeof (s_id), "interleave-%08x", g_random_int ());
341 gst_pad_push_event (self->src, gst_event_new_stream_start (s_id));
343 GST_OBJECT_UNLOCK (self);
348 gst_interleave_class_init (GstInterleaveClass * klass)
350 GstElementClass *gstelement_class;
351 GObjectClass *gobject_class;
353 gobject_class = G_OBJECT_CLASS (klass);
354 gstelement_class = GST_ELEMENT_CLASS (klass);
356 GST_DEBUG_CATEGORY_INIT (gst_interleave_debug, "interleave", 0,
357 "interleave element");
359 gst_element_class_set_static_metadata (gstelement_class, "Audio interleaver",
360 "Filter/Converter/Audio",
361 "Folds many mono channels into one interleaved audio stream",
362 "Andy Wingo <wingo at pobox.com>, "
363 "Sebastian Dröge <slomo@circular-chaos.org>");
365 gst_element_class_add_static_pad_template (gstelement_class, &sink_template);
366 gst_element_class_add_static_pad_template (gstelement_class, &src_template);
368 /* Reference GstInterleavePad class to have the type registered from
369 * a threadsafe context
371 g_type_class_ref (GST_TYPE_INTERLEAVE_PAD);
373 gobject_class->finalize = gst_interleave_finalize;
374 gobject_class->set_property = gst_interleave_set_property;
375 gobject_class->get_property = gst_interleave_get_property;
378 * GstInterleave:channel-positions
380 * Channel positions: This property controls the channel positions
381 * that are used on the src caps. The number of elements should be
382 * the same as the number of sink pads and the array should contain
383 * a valid list of channel positions. The n-th element of the array
384 * is the position of the n-th sink pad.
386 * These channel positions will only be used if they're valid and the
387 * number of elements is the same as the number of channels. If this
388 * is not given a NONE layout will be used.
391 g_object_class_install_property (gobject_class, PROP_CHANNEL_POSITIONS,
392 g_param_spec_value_array ("channel-positions", "Channel positions",
393 "Channel positions used on the output",
394 g_param_spec_enum ("channel-position", "Channel position",
395 "Channel position of the n-th input",
396 GST_TYPE_AUDIO_CHANNEL_POSITION,
397 GST_AUDIO_CHANNEL_POSITION_NONE,
398 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS),
399 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
402 * GstInterleave:channel-positions-from-input
404 * Channel positions from input: If this property is set to %TRUE the channel
405 * positions will be taken from the input caps if valid channel positions for
406 * the output can be constructed from them. If this is set to %TRUE setting the
407 * channel-positions property overwrites this property again.
410 g_object_class_install_property (gobject_class,
411 PROP_CHANNEL_POSITIONS_FROM_INPUT,
412 g_param_spec_boolean ("channel-positions-from-input",
413 "Channel positions from input",
414 "Take channel positions from the input", TRUE,
415 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
417 gstelement_class->request_new_pad =
418 GST_DEBUG_FUNCPTR (gst_interleave_request_new_pad);
419 gstelement_class->release_pad =
420 GST_DEBUG_FUNCPTR (gst_interleave_release_pad);
421 gstelement_class->change_state =
422 GST_DEBUG_FUNCPTR (gst_interleave_change_state);
426 gst_interleave_init (GstInterleave * self)
428 self->src = gst_pad_new_from_static_template (&src_template, "src");
430 gst_pad_set_query_function (self->src,
431 GST_DEBUG_FUNCPTR (gst_interleave_src_query));
432 gst_pad_set_event_function (self->src,
433 GST_DEBUG_FUNCPTR (gst_interleave_src_event));
435 gst_element_add_pad (GST_ELEMENT (self), self->src);
437 self->collect = gst_collect_pads_new ();
438 gst_collect_pads_set_function (self->collect,
439 (GstCollectPadsFunction) gst_interleave_collected, self);
441 self->input_channel_positions = g_value_array_new (0);
442 self->channel_positions_from_input = TRUE;
443 self->channel_positions = self->input_channel_positions;
447 gst_interleave_set_property (GObject * object, guint prop_id,
448 const GValue * value, GParamSpec * pspec)
450 GstInterleave *self = GST_INTERLEAVE (object);
453 case PROP_CHANNEL_POSITIONS:
454 if (self->channel_positions &&
455 self->channel_positions != self->input_channel_positions)
456 g_value_array_free (self->channel_positions);
458 self->channel_positions = g_value_dup_boxed (value);
459 self->channel_positions_from_input = FALSE;
461 case PROP_CHANNEL_POSITIONS_FROM_INPUT:
462 self->channel_positions_from_input = g_value_get_boolean (value);
464 if (self->channel_positions_from_input) {
465 if (self->channel_positions &&
466 self->channel_positions != self->input_channel_positions)
467 g_value_array_free (self->channel_positions);
468 self->channel_positions = self->input_channel_positions;
472 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
478 gst_interleave_get_property (GObject * object, guint prop_id,
479 GValue * value, GParamSpec * pspec)
481 GstInterleave *self = GST_INTERLEAVE (object);
484 case PROP_CHANNEL_POSITIONS:
485 g_value_set_boxed (value, self->channel_positions);
487 case PROP_CHANNEL_POSITIONS_FROM_INPUT:
488 g_value_set_boolean (value, self->channel_positions_from_input);
491 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
497 gst_interleave_request_new_pad (GstElement * element, GstPadTemplate * templ,
498 const gchar * req_name, const GstCaps * caps)
500 GstInterleave *self = GST_INTERLEAVE (element);
503 gint channel, padnumber;
506 if (templ->direction != GST_PAD_SINK)
509 padnumber = g_atomic_int_add (&self->padcounter, 1);
511 channel = g_atomic_int_add (&self->channels, 1);
512 if (!self->channel_positions_from_input)
515 pad_name = g_strdup_printf ("sink_%u", padnumber);
516 new_pad = GST_PAD_CAST (g_object_new (GST_TYPE_INTERLEAVE_PAD,
517 "name", pad_name, "direction", templ->direction,
518 "template", templ, NULL));
519 GST_INTERLEAVE_PAD_CAST (new_pad)->channel = channel;
520 GST_DEBUG_OBJECT (self, "requested new pad %s", pad_name);
523 gst_pad_use_fixed_caps (new_pad);
525 gst_collect_pads_add_pad (self->collect, new_pad, sizeof (GstCollectData),
528 gst_collect_pads_set_event_function (self->collect,
529 (GstCollectPadsEventFunction)
530 GST_DEBUG_FUNCPTR (gst_interleave_sink_event), self);
532 gst_collect_pads_set_query_function (self->collect,
533 (GstCollectPadsQueryFunction)
534 GST_DEBUG_FUNCPTR (gst_interleave_sink_query), self);
536 if (!gst_element_add_pad (element, new_pad))
539 g_value_init (&val, GST_TYPE_AUDIO_CHANNEL_POSITION);
540 g_value_set_enum (&val, GST_AUDIO_CHANNEL_POSITION_NONE);
541 self->input_channel_positions =
542 g_value_array_append (self->input_channel_positions, &val);
543 g_value_unset (&val);
545 /* Update the src caps if we already have them */
546 if (self->sinkcaps) {
550 /* Take lock to make sure processing finishes first */
551 GST_OBJECT_LOCK (self->collect);
553 srccaps = gst_caps_copy (self->sinkcaps);
554 s = gst_caps_get_structure (srccaps, 0);
556 gst_structure_set (s, "channels", G_TYPE_INT, self->channels, NULL);
557 gst_interleave_set_channel_positions (self, s);
559 gst_interleave_send_stream_start (self);
560 gst_pad_set_caps (self->src, srccaps);
561 gst_caps_unref (srccaps);
563 GST_OBJECT_UNLOCK (self->collect);
571 g_warning ("interleave: requested new pad that is not a SINK pad\n");
576 GST_DEBUG_OBJECT (self, "could not add pad %s", GST_PAD_NAME (new_pad));
577 gst_collect_pads_remove_pad (self->collect, new_pad);
578 gst_object_unref (new_pad);
584 gst_interleave_release_pad (GstElement * element, GstPad * pad)
586 GstInterleave *self = GST_INTERLEAVE (element);
588 GstAudioChannelPosition position;
590 g_return_if_fail (GST_IS_INTERLEAVE_PAD (pad));
592 /* Take lock to make sure we're not changing this when processing buffers */
593 GST_OBJECT_LOCK (self->collect);
595 g_atomic_int_add (&self->channels, -1);
597 if (gst_pad_has_current_caps (pad))
598 g_atomic_int_add (&self->configured_sinkpads_counter, -1);
600 position = GST_INTERLEAVE_PAD_CAST (pad)->channel;
601 g_value_array_remove (self->input_channel_positions, position);
603 /* Update channel numbers */
604 GST_OBJECT_LOCK (self);
605 for (l = GST_ELEMENT_CAST (self)->sinkpads; l != NULL; l = l->next) {
606 GstInterleavePad *ipad = GST_INTERLEAVE_PAD (l->data);
608 if (GST_INTERLEAVE_PAD_CAST (pad)->channel < ipad->channel)
611 GST_OBJECT_UNLOCK (self);
613 /* Update the src caps if we already have them */
614 if (self->sinkcaps) {
615 if (self->channels > 0) {
619 srccaps = gst_caps_copy (self->sinkcaps);
620 s = gst_caps_get_structure (srccaps, 0);
622 gst_structure_set (s, "channels", G_TYPE_INT, self->channels, NULL);
623 gst_interleave_set_channel_positions (self, s);
625 gst_interleave_send_stream_start (self);
626 gst_pad_set_caps (self->src, srccaps);
627 gst_caps_unref (srccaps);
629 gst_caps_replace (&self->sinkcaps, NULL);
633 GST_OBJECT_UNLOCK (self->collect);
635 gst_collect_pads_remove_pad (self->collect, pad);
636 gst_element_remove_pad (element, pad);
639 static GstStateChangeReturn
640 gst_interleave_change_state (GstElement * element, GstStateChange transition)
643 GstStateChangeReturn ret;
645 self = GST_INTERLEAVE (element);
647 switch (transition) {
648 case GST_STATE_CHANGE_NULL_TO_READY:
650 case GST_STATE_CHANGE_READY_TO_PAUSED:
653 gst_event_replace (&self->pending_segment, NULL);
654 self->send_stream_start = TRUE;
655 gst_collect_pads_start (self->collect);
657 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
663 /* Stop before calling the parent's state change function as
664 * GstCollectPads might take locks and we would deadlock in that
667 if (transition == GST_STATE_CHANGE_PAUSED_TO_READY)
668 gst_collect_pads_stop (self->collect);
670 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
672 switch (transition) {
673 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
675 case GST_STATE_CHANGE_PAUSED_TO_READY:
676 gst_caps_replace (&self->sinkcaps, NULL);
677 gst_event_replace (&self->pending_segment, NULL);
679 case GST_STATE_CHANGE_READY_TO_NULL:
689 __remove_channels (GstCaps * caps)
694 size = gst_caps_get_size (caps);
695 for (i = 0; i < size; i++) {
696 s = gst_caps_get_structure (caps, i);
697 gst_structure_remove_field (s, "channel-mask");
698 gst_structure_remove_field (s, "channels");
703 __set_channels (GstCaps * caps, gint channels)
708 size = gst_caps_get_size (caps);
709 for (i = 0; i < size; i++) {
710 s = gst_caps_get_structure (caps, i);
712 gst_structure_set (s, "channels", G_TYPE_INT, channels, NULL);
714 gst_structure_set (s, "channels", GST_TYPE_INT_RANGE, 1, G_MAXINT, NULL);
718 /* we can only accept caps that we and downstream can handle. */
720 gst_interleave_sink_getcaps (GstPad * pad, GstInterleave * self,
723 GstCaps *result, *peercaps, *sinkcaps;
725 GST_OBJECT_LOCK (self);
727 /* If we already have caps on one of the sink pads return them */
728 if (self->sinkcaps) {
729 result = gst_caps_copy (self->sinkcaps);
731 /* get the downstream possible caps */
732 peercaps = gst_pad_peer_query_caps (self->src, NULL);
734 /* get the allowed caps on this sinkpad */
735 sinkcaps = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
736 __remove_channels (sinkcaps);
738 peercaps = gst_caps_make_writable (peercaps);
739 __remove_channels (peercaps);
740 /* if the peer has caps, intersect */
741 GST_DEBUG_OBJECT (pad, "intersecting peer and template caps");
742 result = gst_caps_intersect (peercaps, sinkcaps);
743 gst_caps_unref (peercaps);
744 gst_caps_unref (sinkcaps);
746 /* the peer has no caps (or there is no peer), just use the allowed caps
747 * of this sinkpad. */
748 GST_DEBUG_OBJECT (pad, "no peer caps, using sinkcaps");
751 __set_channels (result, 1);
754 GST_OBJECT_UNLOCK (self);
756 if (filter != NULL) {
757 GstCaps *caps = result;
759 GST_LOG_OBJECT (pad, "intersecting filter caps %" GST_PTR_FORMAT " with "
760 "preliminary result %" GST_PTR_FORMAT, filter, caps);
762 result = gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
763 gst_caps_unref (caps);
766 GST_DEBUG_OBJECT (pad, "Returning caps %" GST_PTR_FORMAT, result);
772 gst_interleave_set_process_function (GstInterleave * self)
774 switch (self->width) {
776 self->func = (GstInterleaveFunc) interleave_8;
779 self->func = (GstInterleaveFunc) interleave_16;
782 self->func = (GstInterleaveFunc) interleave_24;
785 self->func = (GstInterleaveFunc) interleave_32;
788 self->func = (GstInterleaveFunc) interleave_64;
791 g_assert_not_reached ();
797 gst_interleave_sink_setcaps (GstInterleave * self, GstPad * pad,
798 const GstCaps * caps, const GstAudioInfo * info)
800 g_return_val_if_fail (GST_IS_INTERLEAVE_PAD (pad), FALSE);
802 /* TODO: handle caps changes */
803 if (self->sinkcaps && !gst_caps_is_subset (caps, self->sinkcaps)) {
804 goto cannot_change_caps;
810 self->width = GST_AUDIO_INFO_WIDTH (info);
811 self->rate = GST_AUDIO_INFO_RATE (info);
813 gst_interleave_set_process_function (self);
815 srccaps = gst_caps_copy (caps);
816 s = gst_caps_get_structure (srccaps, 0);
818 gst_structure_remove_field (s, "channel-mask");
820 gst_structure_set (s, "channels", G_TYPE_INT, self->channels, "layout",
821 G_TYPE_STRING, "interleaved", NULL);
822 gst_interleave_set_channel_positions (self, s);
824 gst_interleave_send_stream_start (self);
825 res = gst_pad_set_caps (self->src, srccaps);
826 gst_caps_unref (srccaps);
829 goto src_did_not_accept;
832 if (!self->sinkcaps) {
833 GstCaps *sinkcaps = gst_caps_copy (caps);
834 GstStructure *s = gst_caps_get_structure (sinkcaps, 0);
836 gst_structure_remove_field (s, "channel-mask");
838 GST_DEBUG_OBJECT (self, "setting sinkcaps %" GST_PTR_FORMAT, sinkcaps);
840 gst_caps_replace (&self->sinkcaps, sinkcaps);
842 gst_caps_unref (sinkcaps);
849 GST_WARNING_OBJECT (self, "caps of %" GST_PTR_FORMAT " already set, can't "
850 "change", self->sinkcaps);
855 GST_WARNING_OBJECT (self, "src did not accept setcaps()");
861 gst_interleave_sink_event (GstCollectPads * pads, GstCollectData * data,
862 GstEvent * event, gpointer user_data)
864 GstInterleave *self = GST_INTERLEAVE (user_data);
867 GST_DEBUG ("Got %s event on pad %s:%s", GST_EVENT_TYPE_NAME (event),
868 GST_DEBUG_PAD_NAME (data->pad));
870 switch (GST_EVENT_TYPE (event)) {
871 case GST_EVENT_FLUSH_STOP:
872 GST_OBJECT_LOCK (self);
873 gst_event_replace (&self->pending_segment, NULL);
874 GST_OBJECT_UNLOCK (self);
876 case GST_EVENT_SEGMENT:
878 GST_OBJECT_LOCK (self);
879 gst_event_replace (&self->pending_segment, event);
880 GST_OBJECT_UNLOCK (self);
890 gst_event_parse_caps (event, &caps);
892 if (!gst_audio_info_from_caps (&info, caps)) {
893 GST_WARNING_OBJECT (self, "invalid sink caps");
894 gst_event_unref (event);
900 if (self->channel_positions_from_input
901 && GST_AUDIO_INFO_CHANNELS (&info) == 1) {
902 channel = GST_INTERLEAVE_PAD_CAST (data->pad)->channel;
903 val = g_value_array_get_nth (self->input_channel_positions, channel);
904 g_value_set_enum (val, GST_AUDIO_INFO_POSITION (&info, 0));
907 if (!gst_pad_has_current_caps (data->pad))
908 g_atomic_int_add (&self->configured_sinkpads_counter, 1);
910 /* Last caps that are set on a sink pad are used as output caps */
911 if (g_atomic_int_get (&self->configured_sinkpads_counter) ==
913 ret = gst_interleave_sink_setcaps (self, data->pad, caps, &info);
914 gst_event_unref (event);
920 GST_FIXME_OBJECT (self, "FIXME: merge tags and send after stream-start");
926 /* now GstCollectPads can take care of the rest, e.g. EOS */
928 return gst_collect_pads_event_default (pads, data, event, FALSE);
934 gst_interleave_sink_query (GstCollectPads * pads,
935 GstCollectData * data, GstQuery * query, gpointer user_data)
937 GstInterleave *self = GST_INTERLEAVE (user_data);
940 GST_DEBUG ("Got %s query on pad %s:%s", GST_QUERY_TYPE_NAME (query),
941 GST_DEBUG_PAD_NAME (data->pad));
943 switch (GST_QUERY_TYPE (query)) {
946 GstCaps *filter, *caps;
948 gst_query_parse_caps (query, &filter);
949 caps = gst_interleave_sink_getcaps (data->pad, self, filter);
950 gst_query_set_caps_result (query, caps);
951 gst_caps_unref (caps);
956 ret = gst_collect_pads_query_default (pads, data, query, FALSE);
964 gst_interleave_src_query_duration (GstInterleave * self, GstQuery * query)
973 gst_query_parse_duration (query, &format, NULL);
979 /* Take maximum of all durations */
980 it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (self));
982 GstIteratorResult ires;
984 GValue item = { 0, };
986 ires = gst_iterator_next (it, &item);
988 case GST_ITERATOR_DONE:
991 case GST_ITERATOR_OK:
993 GstPad *pad = GST_PAD_CAST (g_value_dup_object (&item));
997 /* ask sink peer for duration */
998 res &= gst_pad_peer_query_duration (pad, format, &duration);
999 /* take max from all valid return values */
1001 /* valid unknown length, stop searching */
1002 if (duration == -1) {
1006 /* else see if bigger than current max */
1007 else if (duration > max)
1010 gst_object_unref (pad);
1011 g_value_unset (&item);
1014 case GST_ITERATOR_RESYNC:
1017 gst_iterator_resync (it);
1025 gst_iterator_free (it);
1028 /* If in bytes format we have to multiply with the number of channels
1029 * to get the correct results. All other formats should be fine */
1030 if (format == GST_FORMAT_BYTES && max != -1)
1031 max *= self->channels;
1033 /* and store the max */
1034 GST_DEBUG_OBJECT (self, "Total duration in format %s: %"
1035 GST_TIME_FORMAT, gst_format_get_name (format), GST_TIME_ARGS (max));
1036 gst_query_set_duration (query, format, max);
1043 gst_interleave_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
1045 GstInterleave *self = GST_INTERLEAVE (parent);
1046 gboolean res = FALSE;
1048 switch (GST_QUERY_TYPE (query)) {
1049 case GST_QUERY_POSITION:
1053 gst_query_parse_position (query, &format, NULL);
1056 case GST_FORMAT_TIME:
1057 /* FIXME, bring to stream time, might be tricky */
1058 gst_query_set_position (query, format, self->timestamp);
1061 case GST_FORMAT_BYTES:
1062 gst_query_set_position (query, format,
1063 self->offset * self->channels * self->width);
1066 case GST_FORMAT_DEFAULT:
1067 gst_query_set_position (query, format, self->offset);
1075 case GST_QUERY_DURATION:
1076 res = gst_interleave_src_query_duration (self, query);
1079 /* FIXME, needs a custom query handler because we have multiple
1081 res = gst_pad_query_default (pad, parent, query);
1089 forward_event_func (const GValue * item, GValue * ret, GstEvent * event)
1091 GstPad *pad = GST_PAD_CAST (g_value_dup_object (item));
1092 gst_event_ref (event);
1093 GST_LOG_OBJECT (pad, "About to send event %s", GST_EVENT_TYPE_NAME (event));
1094 if (!gst_pad_push_event (pad, event)) {
1095 g_value_set_boolean (ret, FALSE);
1096 GST_WARNING_OBJECT (pad, "Sending event %p (%s) failed.",
1097 event, GST_EVENT_TYPE_NAME (event));
1099 GST_LOG_OBJECT (pad, "Sent event %p (%s).",
1100 event, GST_EVENT_TYPE_NAME (event));
1102 gst_object_unref (pad);
1107 forward_event (GstInterleave * self, GstEvent * event)
1110 GValue vret = { 0 };
1112 GST_LOG_OBJECT (self, "Forwarding event %p (%s)", event,
1113 GST_EVENT_TYPE_NAME (event));
1115 g_value_init (&vret, G_TYPE_BOOLEAN);
1116 g_value_set_boolean (&vret, TRUE);
1117 it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (self));
1118 gst_iterator_fold (it, (GstIteratorFoldFunction) forward_event_func, &vret,
1120 gst_iterator_free (it);
1121 gst_event_unref (event);
1123 return g_value_get_boolean (&vret);
1128 gst_interleave_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
1130 GstInterleave *self = GST_INTERLEAVE (parent);
1133 switch (GST_EVENT_TYPE (event)) {
1135 /* QoS might be tricky */
1138 case GST_EVENT_SEEK:
1142 gst_event_parse_seek (event, NULL, NULL, &flags, NULL, NULL, NULL, NULL);
1144 /* check if we are flushing */
1145 if (flags & GST_SEEK_FLAG_FLUSH) {
1146 /* make sure we accept nothing anymore and return WRONG_STATE */
1147 gst_collect_pads_set_flushing (self->collect, TRUE);
1149 /* flushing seek, start flush downstream, the flush will be done
1150 * when all pads received a FLUSH_STOP. */
1151 gst_pad_push_event (self->src, gst_event_new_flush_start ());
1153 result = forward_event (self, event);
1156 case GST_EVENT_NAVIGATION:
1157 /* navigation is rather pointless. */
1161 /* just forward the rest for now */
1162 result = forward_event (self, event);
1169 static GstFlowReturn
1170 gst_interleave_collected (GstCollectPads * pads, GstInterleave * self)
1173 GstBuffer *outbuf = NULL;
1174 GstFlowReturn ret = GST_FLOW_OK;
1177 guint ncollected = 0;
1178 gboolean empty = TRUE;
1179 gint width = self->width / 8;
1180 GstMapInfo write_info;
1181 GstClockTime timestamp = -1;
1183 size = gst_collect_pads_available (pads);
1187 g_return_val_if_fail (self->func != NULL, GST_FLOW_NOT_NEGOTIATED);
1188 g_return_val_if_fail (self->width > 0, GST_FLOW_NOT_NEGOTIATED);
1189 g_return_val_if_fail (self->channels > 0, GST_FLOW_NOT_NEGOTIATED);
1190 g_return_val_if_fail (self->rate > 0, GST_FLOW_NOT_NEGOTIATED);
1192 g_return_val_if_fail (size % width == 0, GST_FLOW_ERROR);
1194 GST_DEBUG_OBJECT (self, "Starting to collect %u bytes from %d channels", size,
1197 nsamples = size / width;
1199 outbuf = gst_buffer_new_allocate (NULL, size * self->channels, NULL);
1201 if (outbuf == NULL || gst_buffer_get_size (outbuf) < size * self->channels) {
1202 gst_buffer_unref (outbuf);
1203 return GST_FLOW_NOT_NEGOTIATED;
1206 gst_buffer_map (outbuf, &write_info, GST_MAP_WRITE);
1207 memset (write_info.data, 0, size * self->channels);
1209 for (collected = pads->data; collected != NULL; collected = collected->next) {
1210 GstCollectData *cdata;
1213 GstMapInfo input_info;
1216 cdata = (GstCollectData *) collected->data;
1218 inbuf = gst_collect_pads_take_buffer (pads, cdata, size);
1219 if (inbuf == NULL) {
1220 GST_DEBUG_OBJECT (cdata->pad, "No buffer available");
1225 if (timestamp == -1)
1226 timestamp = GST_BUFFER_TIMESTAMP (inbuf);
1228 if (GST_BUFFER_FLAG_IS_SET (inbuf, GST_BUFFER_FLAG_GAP))
1232 channel = GST_INTERLEAVE_PAD_CAST (cdata->pad)->channel;
1233 if (self->channels <= 64 && self->channel_mask) {
1234 channel = self->default_channels_ordering_map[channel];
1236 outdata = write_info.data + width * channel;
1238 gst_buffer_map (inbuf, &input_info, GST_MAP_READ);
1239 self->func (outdata, input_info.data, self->channels, nsamples);
1240 gst_buffer_unmap (inbuf, &input_info);
1244 gst_buffer_unref (inbuf);
1247 if (ncollected == 0) {
1248 gst_buffer_unmap (outbuf, &write_info);
1252 GST_OBJECT_LOCK (self);
1253 if (self->pending_segment) {
1257 event = self->pending_segment;
1258 self->pending_segment = NULL;
1259 GST_OBJECT_UNLOCK (self);
1261 /* convert the input segment to time now */
1262 gst_event_copy_segment (event, &segment);
1264 if (segment.format != GST_FORMAT_TIME) {
1265 gst_event_unref (event);
1267 /* not time, convert */
1268 switch (segment.format) {
1269 case GST_FORMAT_BYTES:
1270 segment.start *= width;
1271 if (segment.stop != -1)
1272 segment.stop *= width;
1273 if (segment.position != -1)
1274 segment.position *= width;
1275 /* fallthrough for the samples case */
1276 case GST_FORMAT_DEFAULT:
1278 gst_util_uint64_scale_int (segment.start, GST_SECOND, self->rate);
1279 if (segment.stop != -1)
1281 gst_util_uint64_scale_int (segment.stop, GST_SECOND,
1283 if (segment.position != -1)
1285 gst_util_uint64_scale_int (segment.position, GST_SECOND,
1289 GST_WARNING ("can't convert segment values");
1292 segment.position = 0;
1295 event = gst_event_new_segment (&segment);
1297 gst_pad_push_event (self->src, event);
1299 GST_OBJECT_LOCK (self);
1301 GST_OBJECT_UNLOCK (self);
1303 if (timestamp != -1) {
1304 self->offset = gst_util_uint64_scale_int (timestamp, self->rate,
1306 self->timestamp = timestamp;
1309 GST_BUFFER_TIMESTAMP (outbuf) = self->timestamp;
1310 GST_BUFFER_OFFSET (outbuf) = self->offset;
1312 self->offset += nsamples;
1313 self->timestamp = gst_util_uint64_scale_int (self->offset,
1314 GST_SECOND, self->rate);
1316 GST_BUFFER_DURATION (outbuf) =
1317 self->timestamp - GST_BUFFER_TIMESTAMP (outbuf);
1320 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_GAP);
1322 gst_buffer_unmap (outbuf, &write_info);
1324 GST_LOG_OBJECT (self, "pushing outbuf, timestamp %" GST_TIME_FORMAT,
1325 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)));
1326 ret = gst_pad_push (self->src, outbuf);
1332 GST_DEBUG_OBJECT (self, "no data available, must be EOS");
1334 gst_buffer_unref (outbuf);
1335 gst_pad_push_event (self->src, gst_event_new_eos ());
1336 return GST_FLOW_EOS;