Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.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  *                    2008 Sebastian Dröge <slomo@circular-chaos.org>
7  *
8  * deinterleave.c: deinterleave samples
9  *
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.
14  *
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.
19  *
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., 59 Temple Place - Suite 330,
23  * Boston, MA 02111-1307, USA.
24  */
25
26 /* TODO: 
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
31  */
32
33 /**
34  * SECTION:element-deinterleave
35  * @see_also: interleave
36  *
37  * Splits one interleaved multichannel audio stream into many mono audio streams.
38  * 
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.
43  * 
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.
46  * 
47  * <refsect2>
48  * <title>Example launch line</title>
49  * |[
50  * gst-launch filesrc location=/path/to/file.mp3 ! decodebin ! audioconvert ! "audio/x-raw-int,channels=2 ! deinterleave name=d  d.src0 ! queue ! audioconvert ! vorbisenc ! oggmux ! filesink location=channel1.ogg  d.src1 ! queue ! audioconvert ! vorbisenc ! oggmux ! filesink location=channel2.ogg
51  * ]| Decodes an MP3 file and encodes the left and right channel into separate
52  * Ogg Vorbis files.
53  * |[
54  * gst-launch filesrc location=file.mp3 ! decodebin ! audioconvert ! "audio/x-raw-int,channels=2" ! deinterleave name=d  interleave name=i ! audioconvert ! wavenc ! filesink location=test.wav    d.src0 ! queue ! audioconvert ! i.sink1    d.src1 ! queue ! audioconvert ! i.sink0
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
57  * channels exchanged.
58  * </refsect2>
59  */
60
61 #ifdef HAVE_CONFIG_H
62 #  include "config.h"
63 #endif
64
65 #include <gst/gst.h>
66 #include <string.h>
67 #include "deinterleave.h"
68
69 GST_DEBUG_CATEGORY_STATIC (gst_deinterleave_debug);
70 #define GST_CAT_DEFAULT gst_deinterleave_debug
71
72 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src%d",
73     GST_PAD_SRC,
74     GST_PAD_SOMETIMES,
75     GST_STATIC_CAPS ("audio/x-raw-int, "
76         "rate = (int) [ 1, MAX ], "
77         "channels = (int) 1, "
78         "endianness = (int) { LITTLE_ENDIAN, BIG_ENDIAN }, "
79         "width = (int) { 8, 16, 24, 32 }, "
80         "depth = (int) [ 1, 32 ], "
81         "signed = (boolean) { true, false }; "
82         "audio/x-raw-float, "
83         "rate = (int) [ 1, MAX ], "
84         "channels = (int) 1, "
85         "endianness = (int) { LITTLE_ENDIAN , BIG_ENDIAN }, "
86         "width = (int) { 32, 64 }")
87     );
88
89 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
90     GST_PAD_SINK,
91     GST_PAD_ALWAYS,
92     GST_STATIC_CAPS ("audio/x-raw-int, "
93         "rate = (int) [ 1, MAX ], "
94         "channels = (int) [ 1, MAX ], "
95         "endianness = (int) { LITTLE_ENDIAN, BIG_ENDIAN }, "
96         "width = (int) { 8, 16, 24, 32 }, "
97         "depth = (int) [ 1, 32 ], "
98         "signed = (boolean) { true, false }; "
99         "audio/x-raw-float, "
100         "rate = (int) [ 1, MAX ], "
101         "channels = (int) [ 1, MAX ], "
102         "endianness = (int) { LITTLE_ENDIAN , BIG_ENDIAN }, "
103         "width = (int) { 32, 64 }")
104     );
105
106 #define MAKE_FUNC(type) \
107 static void deinterleave_##type (guint##type *out, guint##type *in, \
108     guint stride, guint nframes) \
109 { \
110   gint i; \
111   \
112   for (i = 0; i < nframes; i++) { \
113     out[i] = *in; \
114     in += stride; \
115   } \
116 }
117
118 MAKE_FUNC (8);
119 MAKE_FUNC (16);
120 MAKE_FUNC (32);
121 MAKE_FUNC (64);
122
123 static void
124 deinterleave_24 (guint8 * out, guint8 * in, guint stride, guint nframes)
125 {
126   gint i;
127
128   for (i = 0; i < nframes; i++) {
129     memcpy (out, in, 3);
130     out += 3;
131     in += stride * 3;
132   }
133 }
134
135 GST_BOILERPLATE (GstDeinterleave, gst_deinterleave, GstElement,
136     GST_TYPE_ELEMENT);
137
138 enum
139 {
140   PROP_0,
141   PROP_KEEP_POSITIONS
142 };
143
144 static GstFlowReturn gst_deinterleave_chain (GstPad * pad, GstBuffer * buffer);
145
146 static gboolean gst_deinterleave_sink_setcaps (GstPad * pad, GstCaps * caps);
147
148 static GstCaps *gst_deinterleave_sink_getcaps (GstPad * pad);
149
150 static gboolean gst_deinterleave_sink_activate_push (GstPad * pad,
151     gboolean active);
152 static gboolean gst_deinterleave_sink_event (GstPad * pad, GstEvent * event);
153
154 static gboolean gst_deinterleave_src_query (GstPad * pad, GstQuery * query);
155
156 static void gst_deinterleave_set_property (GObject * object,
157     guint prop_id, const GValue * value, GParamSpec * pspec);
158 static void gst_deinterleave_get_property (GObject * object,
159     guint prop_id, GValue * value, GParamSpec * pspec);
160
161
162 static void
163 gst_deinterleave_finalize (GObject * obj)
164 {
165   GstDeinterleave *self = GST_DEINTERLEAVE (obj);
166
167   if (self->pos) {
168     g_free (self->pos);
169     self->pos = NULL;
170   }
171
172   if (self->pending_events) {
173     g_list_foreach (self->pending_events, (GFunc) gst_mini_object_unref, NULL);
174     g_list_free (self->pending_events);
175     self->pending_events = NULL;
176   }
177
178   G_OBJECT_CLASS (parent_class)->finalize (obj);
179 }
180
181 static void
182 gst_deinterleave_base_init (gpointer g_class)
183 {
184   GstElementClass *gstelement_class = (GstElementClass *) g_class;
185
186   gst_element_class_set_details_simple (gstelement_class, "Audio deinterleaver",
187       "Filter/Converter/Audio",
188       "Splits one interleaved multichannel audio stream into many mono audio streams",
189       "Andy Wingo <wingo at pobox.com>, "
190       "Iain <iain@prettypeople.org>, "
191       "Sebastian Dröge <slomo@circular-chaos.org>");
192
193   gst_element_class_add_static_pad_template (gstelement_class,
194       &sink_template);
195   gst_element_class_add_static_pad_template (gstelement_class,
196       &src_template);
197 }
198
199 static void
200 gst_deinterleave_class_init (GstDeinterleaveClass * klass)
201 {
202   GObjectClass *gobject_class = (GObjectClass *) klass;
203
204   GST_DEBUG_CATEGORY_INIT (gst_deinterleave_debug, "deinterleave", 0,
205       "deinterleave element");
206
207   gobject_class->finalize = gst_deinterleave_finalize;
208   gobject_class->set_property = gst_deinterleave_set_property;
209   gobject_class->get_property = gst_deinterleave_get_property;
210
211   /**
212    * GstDeinterleave:keep-positions
213    * 
214    * Keep positions: When enable the caps on the output buffers will
215    * contain the original channel positions. This can be used to correctly
216    * interleave the output again later but can also lead to unwanted effects
217    * if the output should be handled as Mono.
218    *
219    */
220   g_object_class_install_property (gobject_class, PROP_KEEP_POSITIONS,
221       g_param_spec_boolean ("keep-positions", "Keep positions",
222           "Keep the original channel positions on the output buffers",
223           FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
224 }
225
226 static void
227 gst_deinterleave_init (GstDeinterleave * self, GstDeinterleaveClass * klass)
228 {
229   self->channels = 0;
230   self->pos = NULL;
231   self->keep_positions = FALSE;
232   self->width = 0;
233   self->func = NULL;
234
235   /* Add sink pad */
236   self->sink = gst_pad_new_from_static_template (&sink_template, "sink");
237   gst_pad_set_chain_function (self->sink,
238       GST_DEBUG_FUNCPTR (gst_deinterleave_chain));
239   gst_pad_set_setcaps_function (self->sink,
240       GST_DEBUG_FUNCPTR (gst_deinterleave_sink_setcaps));
241   gst_pad_set_getcaps_function (self->sink,
242       GST_DEBUG_FUNCPTR (gst_deinterleave_sink_getcaps));
243   gst_pad_set_activatepush_function (self->sink,
244       GST_DEBUG_FUNCPTR (gst_deinterleave_sink_activate_push));
245   gst_pad_set_event_function (self->sink,
246       GST_DEBUG_FUNCPTR (gst_deinterleave_sink_event));
247   gst_element_add_pad (GST_ELEMENT (self), self->sink);
248 }
249
250 static void
251 gst_deinterleave_add_new_pads (GstDeinterleave * self, GstCaps * caps)
252 {
253   GstPad *pad;
254
255   guint i;
256
257   for (i = 0; i < self->channels; i++) {
258     gchar *name = g_strdup_printf ("src%d", i);
259
260     GstCaps *srccaps;
261
262     GstStructure *s;
263
264     pad = gst_pad_new_from_static_template (&src_template, name);
265     g_free (name);
266
267     /* Set channel position if we know it */
268     if (self->keep_positions) {
269       GstAudioChannelPosition pos[1] = { GST_AUDIO_CHANNEL_POSITION_NONE };
270
271       srccaps = gst_caps_copy (caps);
272       s = gst_caps_get_structure (srccaps, 0);
273       if (self->pos)
274         gst_audio_set_channel_positions (s, &self->pos[i]);
275       else
276         gst_audio_set_channel_positions (s, pos);
277     } else {
278       srccaps = caps;
279     }
280
281     gst_pad_set_caps (pad, srccaps);
282     gst_pad_use_fixed_caps (pad);
283     gst_pad_set_query_function (pad,
284         GST_DEBUG_FUNCPTR (gst_deinterleave_src_query));
285     gst_pad_set_active (pad, TRUE);
286     gst_element_add_pad (GST_ELEMENT (self), pad);
287     self->srcpads = g_list_prepend (self->srcpads, gst_object_ref (pad));
288
289     if (self->keep_positions)
290       gst_caps_unref (srccaps);
291   }
292
293   gst_element_no_more_pads (GST_ELEMENT (self));
294   self->srcpads = g_list_reverse (self->srcpads);
295 }
296
297 static void
298 gst_deinterleave_set_pads_caps (GstDeinterleave * self, GstCaps * caps)
299 {
300   GList *l;
301
302   GstStructure *s;
303
304   gint i;
305
306   for (l = self->srcpads, i = 0; l; l = l->next, i++) {
307     GstPad *pad = GST_PAD (l->data);
308
309     GstCaps *srccaps;
310
311     /* Set channel position if we know it */
312     if (self->keep_positions) {
313       GstAudioChannelPosition pos[1] = { GST_AUDIO_CHANNEL_POSITION_NONE };
314
315       srccaps = gst_caps_copy (caps);
316       s = gst_caps_get_structure (srccaps, 0);
317       if (self->pos)
318         gst_audio_set_channel_positions (s, &self->pos[i]);
319       else
320         gst_audio_set_channel_positions (s, pos);
321     } else {
322       srccaps = caps;
323     }
324
325     gst_pad_set_caps (pad, srccaps);
326
327     if (self->keep_positions)
328       gst_caps_unref (srccaps);
329   }
330 }
331
332 static void
333 gst_deinterleave_remove_pads (GstDeinterleave * self)
334 {
335   GList *l;
336
337   GST_INFO_OBJECT (self, "removing pads");
338
339   for (l = self->srcpads; l; l = l->next) {
340     GstPad *pad = GST_PAD (l->data);
341
342     gst_element_remove_pad (GST_ELEMENT_CAST (self), pad);
343     gst_object_unref (pad);
344   }
345   g_list_free (self->srcpads);
346   self->srcpads = NULL;
347
348   gst_pad_set_caps (self->sink, NULL);
349   gst_caps_replace (&self->sinkcaps, NULL);
350 }
351
352 static gboolean
353 gst_deinterleave_set_process_function (GstDeinterleave * self, GstCaps * caps)
354 {
355   GstStructure *s;
356
357   s = gst_caps_get_structure (caps, 0);
358   if (!gst_structure_get_int (s, "width", &self->width))
359     return FALSE;
360
361   switch (self->width) {
362     case 8:
363       self->func = (GstDeinterleaveFunc) deinterleave_8;
364       break;
365     case 16:
366       self->func = (GstDeinterleaveFunc) deinterleave_16;
367       break;
368     case 24:
369       self->func = (GstDeinterleaveFunc) deinterleave_24;
370       break;
371     case 32:
372       self->func = (GstDeinterleaveFunc) deinterleave_32;
373       break;
374     case 64:
375       self->func = (GstDeinterleaveFunc) deinterleave_64;
376       break;
377     default:
378       return FALSE;
379   }
380   return TRUE;
381 }
382
383 static gboolean
384 gst_deinterleave_sink_setcaps (GstPad * pad, GstCaps * caps)
385 {
386   GstDeinterleave *self;
387
388   GstCaps *srccaps;
389
390   GstStructure *s;
391
392   self = GST_DEINTERLEAVE (gst_pad_get_parent (pad));
393
394   GST_DEBUG_OBJECT (self, "got caps: %" GST_PTR_FORMAT, caps);
395
396   if (self->sinkcaps && !gst_caps_is_equal (caps, self->sinkcaps)) {
397     gint new_channels, i;
398
399     GstAudioChannelPosition *pos;
400
401     gboolean same_layout = TRUE;
402
403     s = gst_caps_get_structure (caps, 0);
404
405     /* We allow caps changes as long as the number of channels doesn't change
406      * and the channel positions stay the same. _getcaps() should've cared
407      * for this already but better be safe.
408      */
409     if (!gst_structure_get_int (s, "channels", &new_channels) ||
410         new_channels != self->channels ||
411         !gst_deinterleave_set_process_function (self, caps))
412       goto cannot_change_caps;
413
414     /* Now check the channel positions. If we had no channel positions
415      * and get them or the other way around things have changed.
416      * If we had channel positions and get different ones things have
417      * changed too of course
418      */
419     pos = gst_audio_get_channel_positions (s);
420     if ((pos && !self->pos) || (!pos && self->pos))
421       goto cannot_change_caps;
422
423     if (pos) {
424       for (i = 0; i < self->channels; i++) {
425         if (self->pos[i] != pos[i]) {
426           same_layout = FALSE;
427           break;
428         }
429       }
430       g_free (pos);
431       if (!same_layout)
432         goto cannot_change_caps;
433     }
434
435   } else {
436     s = gst_caps_get_structure (caps, 0);
437
438     if (!gst_structure_get_int (s, "channels", &self->channels))
439       goto no_channels;
440
441     if (!gst_deinterleave_set_process_function (self, caps))
442       goto unsupported_caps;
443
444     self->pos = gst_audio_get_channel_positions (s);
445   }
446
447   gst_caps_replace (&self->sinkcaps, caps);
448
449   /* Get srcpad caps */
450   srccaps = gst_caps_copy (caps);
451   s = gst_caps_get_structure (srccaps, 0);
452   gst_structure_set (s, "channels", G_TYPE_INT, 1, NULL);
453   gst_structure_remove_field (s, "channel-positions");
454
455   /* If we already have pads, update the caps otherwise
456    * add new pads */
457   if (self->srcpads) {
458     gst_deinterleave_set_pads_caps (self, srccaps);
459   } else {
460     gst_deinterleave_add_new_pads (self, srccaps);
461   }
462
463   gst_caps_unref (srccaps);
464   gst_object_unref (self);
465
466   return TRUE;
467
468 cannot_change_caps:
469   {
470     GST_ERROR_OBJECT (self, "can't set new caps: %" GST_PTR_FORMAT, caps);
471     gst_object_unref (self);
472     return FALSE;
473   }
474 unsupported_caps:
475   {
476     GST_ERROR_OBJECT (self, "caps not supported: %" GST_PTR_FORMAT, caps);
477     gst_object_unref (self);
478     return FALSE;
479   }
480 no_channels:
481   {
482     GST_ERROR_OBJECT (self, "invalid caps");
483     gst_object_unref (self);
484     return FALSE;
485   }
486 }
487
488 static void
489 __remove_channels (GstCaps * caps)
490 {
491   GstStructure *s;
492
493   gint i, size;
494
495   size = gst_caps_get_size (caps);
496   for (i = 0; i < size; i++) {
497     s = gst_caps_get_structure (caps, i);
498     gst_structure_remove_field (s, "channel-positions");
499     gst_structure_remove_field (s, "channels");
500   }
501 }
502
503 static void
504 __set_channels (GstCaps * caps, gint channels)
505 {
506   GstStructure *s;
507
508   gint i, size;
509
510   size = gst_caps_get_size (caps);
511   for (i = 0; i < size; i++) {
512     s = gst_caps_get_structure (caps, i);
513     if (channels > 0)
514       gst_structure_set (s, "channels", G_TYPE_INT, channels, NULL);
515     else
516       gst_structure_set (s, "channels", GST_TYPE_INT_RANGE, 1, G_MAXINT, NULL);
517   }
518 }
519
520 static GstCaps *
521 gst_deinterleave_sink_getcaps (GstPad * pad)
522 {
523   GstDeinterleave *self = GST_DEINTERLEAVE (gst_pad_get_parent (pad));
524
525   GstCaps *ret;
526
527   GList *l;
528
529   GST_OBJECT_LOCK (self);
530   /* Intersect all of our pad template caps with the peer caps of the pad
531    * to get all formats that are possible up- and downstream.
532    *
533    * For the pad for which the caps are requested we don't remove the channel
534    * informations as they must be in the returned caps and incompatibilities
535    * will be detected here already
536    */
537   ret = gst_caps_new_any ();
538   for (l = GST_ELEMENT (self)->pads; l != NULL; l = l->next) {
539     GstPad *ourpad = GST_PAD (l->data);
540
541     GstCaps *peercaps = NULL, *ourcaps;
542
543     ourcaps = gst_caps_copy (gst_pad_get_pad_template_caps (ourpad));
544
545     if (pad == ourpad) {
546       if (GST_PAD_DIRECTION (pad) == GST_PAD_SINK)
547         __set_channels (ourcaps, self->channels);
548       else
549         __set_channels (ourcaps, 1);
550     } else {
551       __remove_channels (ourcaps);
552       /* Only ask for peer caps for other pads than pad
553        * as otherwise gst_pad_peer_get_caps() might call
554        * back into this function and deadlock
555        */
556       peercaps = gst_pad_peer_get_caps (ourpad);
557     }
558
559     /* If the peer exists and has caps add them to the intersection,
560      * otherwise assume that the peer accepts everything */
561     if (peercaps) {
562       GstCaps *intersection;
563
564       GstCaps *oldret = ret;
565
566       __remove_channels (peercaps);
567
568       intersection = gst_caps_intersect (peercaps, ourcaps);
569
570       ret = gst_caps_intersect (ret, intersection);
571       gst_caps_unref (intersection);
572       gst_caps_unref (peercaps);
573       gst_caps_unref (oldret);
574     } else {
575       GstCaps *oldret = ret;
576
577       ret = gst_caps_intersect (ret, ourcaps);
578       gst_caps_unref (oldret);
579     }
580     gst_caps_unref (ourcaps);
581   }
582   GST_OBJECT_UNLOCK (self);
583
584   gst_object_unref (self);
585
586   GST_DEBUG_OBJECT (pad, "Intersected caps to %" GST_PTR_FORMAT, ret);
587
588   return ret;
589 }
590
591 static gboolean
592 gst_deinterleave_sink_event (GstPad * pad, GstEvent * event)
593 {
594   GstDeinterleave *self = GST_DEINTERLEAVE (gst_pad_get_parent (pad));
595
596   gboolean ret;
597
598   GST_DEBUG ("Got %s event on pad %s:%s", GST_EVENT_TYPE_NAME (event),
599       GST_DEBUG_PAD_NAME (pad));
600
601   /* Send FLUSH_STOP, FLUSH_START and EOS immediately, no matter if
602    * we have src pads already or not. Queue all other events and
603    * push them after we have src pads
604    */
605   switch (GST_EVENT_TYPE (event)) {
606     case GST_EVENT_FLUSH_STOP:
607     case GST_EVENT_FLUSH_START:
608     case GST_EVENT_EOS:
609       ret = gst_pad_event_default (pad, event);
610       break;
611     default:
612       if (self->srcpads) {
613         ret = gst_pad_event_default (pad, event);
614       } else {
615         GST_OBJECT_LOCK (self);
616         self->pending_events = g_list_append (self->pending_events, event);
617         GST_OBJECT_UNLOCK (self);
618         ret = TRUE;
619       }
620       break;
621   }
622
623   gst_object_unref (self);
624
625   return ret;
626 }
627
628 static gboolean
629 gst_deinterleave_src_query (GstPad * pad, GstQuery * query)
630 {
631   GstDeinterleave *self = GST_DEINTERLEAVE (gst_pad_get_parent (pad));
632
633   gboolean res;
634
635   res = gst_pad_query_default (pad, query);
636
637   if (res && GST_QUERY_TYPE (query) == GST_QUERY_DURATION) {
638     GstFormat format;
639
640     gint64 dur;
641
642     gst_query_parse_duration (query, &format, &dur);
643
644     /* Need to divide by the number of channels in byte format
645      * to get the correct value. All other formats should be fine
646      */
647     if (format == GST_FORMAT_BYTES && dur != -1)
648       gst_query_set_duration (query, format, dur / self->channels);
649   } else if (res && GST_QUERY_TYPE (query) == GST_QUERY_POSITION) {
650     GstFormat format;
651
652     gint64 pos;
653
654     gst_query_parse_position (query, &format, &pos);
655
656     /* Need to divide by the number of channels in byte format
657      * to get the correct value. All other formats should be fine
658      */
659     if (format == GST_FORMAT_BYTES && pos != -1)
660       gst_query_set_position (query, format, pos / self->channels);
661   }
662
663   gst_object_unref (self);
664   return res;
665 }
666
667 static void
668 gst_deinterleave_set_property (GObject * object, guint prop_id,
669     const GValue * value, GParamSpec * pspec)
670 {
671   GstDeinterleave *self = GST_DEINTERLEAVE (object);
672
673   switch (prop_id) {
674     case PROP_KEEP_POSITIONS:
675       self->keep_positions = g_value_get_boolean (value);
676       break;
677     default:
678       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
679       break;
680   }
681 }
682
683 static void
684 gst_deinterleave_get_property (GObject * object, guint prop_id,
685     GValue * value, GParamSpec * pspec)
686 {
687   GstDeinterleave *self = GST_DEINTERLEAVE (object);
688
689   switch (prop_id) {
690     case PROP_KEEP_POSITIONS:
691       g_value_set_boolean (value, self->keep_positions);
692       break;
693     default:
694       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
695       break;
696   }
697 }
698
699 static GstFlowReturn
700 gst_deinterleave_process (GstDeinterleave * self, GstBuffer * buf)
701 {
702   GstFlowReturn ret = GST_FLOW_OK;
703
704   guint channels = self->channels;
705
706   guint pads_pushed = 0, buffers_allocated = 0;
707
708   guint nframes = GST_BUFFER_SIZE (buf) / channels / (self->width / 8);
709
710   guint bufsize = nframes * (self->width / 8);
711
712   guint i;
713
714   GList *srcs;
715
716   GstBuffer **buffers_out = g_new0 (GstBuffer *, channels);
717
718   guint8 *in, *out;
719
720   /* Send any pending events to all src pads */
721   GST_OBJECT_LOCK (self);
722   if (self->pending_events) {
723     GList *events;
724
725     GstEvent *event;
726
727     GST_DEBUG_OBJECT (self, "Sending pending events to all src pads");
728
729     for (events = self->pending_events; events != NULL; events = events->next) {
730       event = GST_EVENT (events->data);
731
732       for (srcs = self->srcpads; srcs != NULL; srcs = srcs->next)
733         gst_pad_push_event (GST_PAD (srcs->data), gst_event_ref (event));
734       gst_event_unref (event);
735     }
736
737     g_list_free (self->pending_events);
738     self->pending_events = NULL;
739   }
740   GST_OBJECT_UNLOCK (self);
741
742   /* Allocate buffers */
743   for (srcs = self->srcpads, i = 0; srcs; srcs = srcs->next, i++) {
744     GstPad *pad = (GstPad *) srcs->data;
745
746     buffers_out[i] = NULL;
747     ret =
748         gst_pad_alloc_buffer (pad, GST_BUFFER_OFFSET_NONE, bufsize,
749         GST_PAD_CAPS (pad), &buffers_out[i]);
750
751     /* Make sure we got a correct buffer. The only other case we allow
752      * here is an unliked pad */
753     if (ret != GST_FLOW_OK && ret != GST_FLOW_NOT_LINKED)
754       goto alloc_buffer_failed;
755     else if (buffers_out[i] && GST_BUFFER_SIZE (buffers_out[i]) != bufsize)
756       goto alloc_buffer_bad_size;
757     else if (buffers_out[i] &&
758         !gst_caps_is_equal (GST_BUFFER_CAPS (buffers_out[i]),
759             GST_PAD_CAPS (pad)))
760       goto invalid_caps;
761
762     if (buffers_out[i]) {
763       gst_buffer_copy_metadata (buffers_out[i], buf,
764           GST_BUFFER_COPY_TIMESTAMPS | GST_BUFFER_COPY_FLAGS);
765       buffers_allocated++;
766     }
767   }
768
769   /* Return NOT_LINKED if no pad was linked */
770   if (!buffers_allocated) {
771     GST_WARNING_OBJECT (self,
772         "Couldn't allocate any buffers because no pad was linked");
773     ret = GST_FLOW_NOT_LINKED;
774     goto done;
775   }
776
777   /* deinterleave */
778   for (srcs = self->srcpads, i = 0; srcs; srcs = srcs->next, i++) {
779     GstPad *pad = (GstPad *) srcs->data;
780
781     in = (guint8 *) GST_BUFFER_DATA (buf);
782     in += i * (self->width / 8);
783     if (buffers_out[i]) {
784       out = (guint8 *) GST_BUFFER_DATA (buffers_out[i]);
785
786       self->func (out, in, channels, nframes);
787
788       ret = gst_pad_push (pad, buffers_out[i]);
789       buffers_out[i] = NULL;
790       if (ret == GST_FLOW_OK)
791         pads_pushed++;
792       else if (ret == GST_FLOW_NOT_LINKED)
793         ret = GST_FLOW_OK;
794       else
795         goto push_failed;
796     }
797   }
798
799   /* Return NOT_LINKED if no pad was linked */
800   if (!pads_pushed)
801     ret = GST_FLOW_NOT_LINKED;
802
803 done:
804   gst_buffer_unref (buf);
805   g_free (buffers_out);
806   return ret;
807
808 alloc_buffer_failed:
809   {
810     GST_WARNING ("gst_pad_alloc_buffer() returned %s", gst_flow_get_name (ret));
811     goto clean_buffers;
812
813   }
814 alloc_buffer_bad_size:
815   {
816     GST_WARNING ("called alloc_buffer(), but didn't get requested bytes");
817     ret = GST_FLOW_NOT_NEGOTIATED;
818     goto clean_buffers;
819   }
820 invalid_caps:
821   {
822     GST_WARNING ("called alloc_buffer(), but didn't get requested caps");
823     ret = GST_FLOW_NOT_NEGOTIATED;
824     goto clean_buffers;
825   }
826 push_failed:
827   {
828     GST_DEBUG ("push() failed, flow = %s", gst_flow_get_name (ret));
829     goto clean_buffers;
830   }
831 clean_buffers:
832   {
833     for (i = 0; i < channels; i++) {
834       if (buffers_out[i])
835         gst_buffer_unref (buffers_out[i]);
836     }
837     gst_buffer_unref (buf);
838     g_free (buffers_out);
839     return ret;
840   }
841 }
842
843 static GstFlowReturn
844 gst_deinterleave_chain (GstPad * pad, GstBuffer * buffer)
845 {
846   GstDeinterleave *self = GST_DEINTERLEAVE (GST_PAD_PARENT (pad));
847
848   GstFlowReturn ret;
849
850   g_return_val_if_fail (self->func != NULL, GST_FLOW_NOT_NEGOTIATED);
851   g_return_val_if_fail (self->width > 0, GST_FLOW_NOT_NEGOTIATED);
852   g_return_val_if_fail (self->channels > 0, GST_FLOW_NOT_NEGOTIATED);
853
854   ret = gst_deinterleave_process (self, buffer);
855
856   if (ret != GST_FLOW_OK)
857     GST_DEBUG_OBJECT (self, "flow return: %s", gst_flow_get_name (ret));
858
859   return ret;
860 }
861
862 static gboolean
863 gst_deinterleave_sink_activate_push (GstPad * pad, gboolean active)
864 {
865   GstDeinterleave *self = GST_DEINTERLEAVE (gst_pad_get_parent (pad));
866
867   /* Reset everything when the pad is deactivated */
868   if (!active) {
869     gst_deinterleave_remove_pads (self);
870     if (self->pos) {
871       g_free (self->pos);
872       self->pos = NULL;
873     }
874     self->channels = 0;
875     self->width = 0;
876     self->func = NULL;
877
878     if (self->pending_events) {
879       g_list_foreach (self->pending_events, (GFunc) gst_mini_object_unref,
880           NULL);
881       g_list_free (self->pending_events);
882       self->pending_events = NULL;
883     }
884   }
885
886   gst_object_unref (self);
887
888   return TRUE;
889 }