adder: fix getcaps function
[platform/upstream/gstreamer.git] / gst / adder / gstadder.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2001 Thomas <thomas@apestaart.org>
4  *               2005,2006 Wim Taymans <wim@fluendo.com>
5  *
6  * adder.c: Adder element, N in, one out, samples are added
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23 /**
24  * SECTION:element-adder
25  *
26  * The adder allows to mix several streams into one by adding the data.
27  * Mixed data is clamped to the min/max values of the data format.
28  *
29  * The adder currently mixes all data received on the sinkpads as soon as
30  * possible without trying to synchronize the streams.
31  *
32  * <refsect2>
33  * <title>Example launch line</title>
34  * |[
35  * gst-launch audiotestsrc freq=100 ! adder name=mix ! audioconvert ! alsasink audiotestsrc freq=500 ! mix.
36  * ]| This pipeline produces two sine waves mixed together.
37  * </refsect2>
38  *
39  * Last reviewed on 2006-05-09 (0.10.7)
40  */
41 /* Element-Checklist-Version: 5 */
42
43 #ifdef HAVE_CONFIG_H
44 #include "config.h"
45 #endif
46 #include "gstadder.h"
47 #include <gst/audio/audio.h>
48 #include <string.h>             /* strcmp */
49 #include "gstadderorc.h"
50
51 /* highest positive/lowest negative x-bit value we can use for clamping */
52 #define MAX_INT_32  ((gint32) (0x7fffffff))
53 #define MAX_INT_16  ((gint16) (0x7fff))
54 #define MAX_INT_8   ((gint8)  (0x7f))
55 #define MAX_UINT_32 ((guint32)(0xffffffff))
56 #define MAX_UINT_16 ((guint16)(0xffff))
57 #define MAX_UINT_8  ((guint8) (0xff))
58
59 #define MIN_INT_32  ((gint32) (0x80000000))
60 #define MIN_INT_16  ((gint16) (0x8000))
61 #define MIN_INT_8   ((gint8)  (0x80))
62 #define MIN_UINT_32 ((guint32)(0x00000000))
63 #define MIN_UINT_16 ((guint16)(0x0000))
64 #define MIN_UINT_8  ((guint8) (0x00))
65
66 enum
67 {
68   PROP_0,
69   PROP_FILTER_CAPS
70 };
71
72 #define GST_CAT_DEFAULT gst_adder_debug
73 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
74
75 /* elementfactory information */
76
77 #define CAPS \
78   "audio/x-raw-int, " \
79   "rate = (int) [ 1, MAX ], " \
80   "channels = (int) [ 1, MAX ], " \
81   "endianness = (int) BYTE_ORDER, " \
82   "width = (int) 32, " \
83   "depth = (int) 32, " \
84   "signed = (boolean) { true, false } ;" \
85   "audio/x-raw-int, " \
86   "rate = (int) [ 1, MAX ], " \
87   "channels = (int) [ 1, MAX ], " \
88   "endianness = (int) BYTE_ORDER, " \
89   "width = (int) 16, " \
90   "depth = (int) 16, " \
91   "signed = (boolean) { true, false } ;" \
92   "audio/x-raw-int, " \
93   "rate = (int) [ 1, MAX ], " \
94   "channels = (int) [ 1, MAX ], " \
95   "endianness = (int) BYTE_ORDER, " \
96   "width = (int) 8, " \
97   "depth = (int) 8, " \
98   "signed = (boolean) { true, false } ;" \
99   "audio/x-raw-float, " \
100   "rate = (int) [ 1, MAX ], " \
101   "channels = (int) [ 1, MAX ], " \
102   "endianness = (int) BYTE_ORDER, " \
103   "width = (int) { 32, 64 }"
104
105 static GstStaticPadTemplate gst_adder_src_template =
106 GST_STATIC_PAD_TEMPLATE ("src",
107     GST_PAD_SRC,
108     GST_PAD_ALWAYS,
109     GST_STATIC_CAPS (CAPS)
110     );
111
112 static GstStaticPadTemplate gst_adder_sink_template =
113 GST_STATIC_PAD_TEMPLATE ("sink%d",
114     GST_PAD_SINK,
115     GST_PAD_REQUEST,
116     GST_STATIC_CAPS (CAPS)
117     );
118
119 #define gst_adder_parent_class parent_class
120 G_DEFINE_TYPE (GstAdder, gst_adder, GST_TYPE_ELEMENT);
121
122 static void gst_adder_dispose (GObject * object);
123 static void gst_adder_set_property (GObject * object, guint prop_id,
124     const GValue * value, GParamSpec * pspec);
125 static void gst_adder_get_property (GObject * object, guint prop_id,
126     GValue * value, GParamSpec * pspec);
127
128 static gboolean gst_adder_setcaps (GstPad * pad, GstCaps * caps);
129 static gboolean gst_adder_query (GstPad * pad, GstQuery * query);
130 static gboolean gst_adder_src_event (GstPad * pad, GstEvent * event);
131 static gboolean gst_adder_sink_event (GstPad * pad, GstEvent * event);
132
133 static GstPad *gst_adder_request_new_pad (GstElement * element,
134     GstPadTemplate * temp, const gchar * unused);
135 static void gst_adder_release_pad (GstElement * element, GstPad * pad);
136
137 static GstStateChangeReturn gst_adder_change_state (GstElement * element,
138     GstStateChange transition);
139
140 static GstBuffer *gst_adder_do_clip (GstCollectPads * pads,
141     GstCollectData * data, GstBuffer * buffer, gpointer user_data);
142 static GstFlowReturn gst_adder_collected (GstCollectPads * pads,
143     gpointer user_data);
144
145 /* non-clipping versions (for float) */
146 #define MAKE_FUNC_NC(name,type)                                 \
147 static void name (type *out, type *in, gint samples) {          \
148   gint i;                                                       \
149   for (i = 0; i < samples; i++)                                 \
150     out[i] += in[i];                                            \
151 }
152
153 /* *INDENT-OFF* */
154 MAKE_FUNC_NC (add_float64, gdouble)
155 /* *INDENT-ON* */
156
157 /* we can only accept caps that we and downstream can handle.
158  * if we have filtercaps set, use those to constrain the target caps.
159  */
160 static GstCaps *
161 gst_adder_sink_getcaps (GstPad * pad)
162 {
163   GstAdder *adder;
164   GstCaps *result, *peercaps, *sinkcaps, *filter_caps;
165
166   adder = GST_ADDER (GST_PAD_PARENT (pad));
167
168   GST_OBJECT_LOCK (adder);
169   /* take filter */
170   if ((filter_caps = adder->filter_caps))
171     gst_caps_ref (filter_caps);
172   GST_OBJECT_UNLOCK (adder);
173
174   /* get the downstream possible caps */
175   peercaps = gst_pad_peer_get_caps (adder->srcpad);
176
177   /* get the allowed caps on this sinkpad */
178   sinkcaps = gst_pad_get_current_caps (pad);
179   if (sinkcaps == NULL) {
180     sinkcaps = (GstCaps *) gst_pad_get_pad_template_caps (pad);
181     if (sinkcaps)
182       gst_caps_ref (sinkcaps);
183     else
184       sinkcaps = gst_caps_new_any ();
185   }
186
187   if (peercaps) {
188     /* restrict with filter-caps if any */
189     if (filter_caps) {
190       GST_DEBUG_OBJECT (adder, "filtering peer caps");
191       result = gst_caps_intersect (peercaps, filter_caps);
192       gst_caps_unref (peercaps);
193       peercaps = result;
194     }
195     /* if the peer has caps, intersect */
196     GST_DEBUG_OBJECT (adder, "intersecting peer and template caps");
197     result = gst_caps_intersect (peercaps, sinkcaps);
198     gst_caps_unref (peercaps);
199     gst_caps_unref (sinkcaps);
200   } else {
201     /* the peer has no caps (or there is no peer), just use the allowed caps
202      * of this sinkpad. */
203     /* restrict with filter-caps if any */
204     if (filter_caps) {
205       GST_DEBUG_OBJECT (adder, "no peer caps, using filtered sinkcaps");
206       result = gst_caps_intersect (sinkcaps, filter_caps);
207       gst_caps_unref (sinkcaps);
208     } else {
209       GST_DEBUG_OBJECT (adder, "no peer caps, using sinkcaps");
210       result = sinkcaps;
211     }
212   }
213
214   if (filter_caps)
215     gst_caps_unref (filter_caps);
216
217   GST_LOG_OBJECT (adder, "getting caps on pad %p,%s to %" GST_PTR_FORMAT, pad,
218       GST_PAD_NAME (pad), result);
219
220   return result;
221 }
222
223 /* the first caps we receive on any of the sinkpads will define the caps for all
224  * the other sinkpads because we can only mix streams with the same caps.
225  */
226 static gboolean
227 gst_adder_setcaps (GstPad * pad, GstCaps * caps)
228 {
229   GstAdder *adder;
230   GList *pads;
231   GstStructure *structure;
232   const char *media_type;
233
234   adder = GST_ADDER (GST_PAD_PARENT (pad));
235
236   GST_LOG_OBJECT (adder, "setting caps on pad %p,%s to %" GST_PTR_FORMAT, pad,
237       GST_PAD_NAME (pad), caps);
238
239   /* FIXME, see if the other pads can accept the format. Also lock the
240    * format on the other pads to this new format. */
241   GST_OBJECT_LOCK (adder);
242   pads = GST_ELEMENT (adder)->pads;
243   while (pads) {
244     GstPad *otherpad = GST_PAD (pads->data);
245
246     if (otherpad != pad) {
247       gst_caps_replace (&GST_PAD_CAPS (otherpad), caps);
248     }
249     pads = g_list_next (pads);
250   }
251   GST_OBJECT_UNLOCK (adder);
252
253   /* parse caps now */
254   structure = gst_caps_get_structure (caps, 0);
255   media_type = gst_structure_get_name (structure);
256   if (strcmp (media_type, "audio/x-raw-int") == 0) {
257     adder->format = GST_ADDER_FORMAT_INT;
258     gst_structure_get_int (structure, "width", &adder->width);
259     gst_structure_get_int (structure, "depth", &adder->depth);
260     gst_structure_get_int (structure, "endianness", &adder->endianness);
261     gst_structure_get_boolean (structure, "signed", &adder->is_signed);
262
263     GST_INFO_OBJECT (pad, "parse_caps sets adder to format int, %d bit",
264         adder->width);
265
266     if (adder->endianness != G_BYTE_ORDER)
267       goto not_supported;
268
269     switch (adder->width) {
270       case 8:
271         adder->func = (adder->is_signed ?
272             (GstAdderFunction) add_int8 : (GstAdderFunction) add_uint8);
273         adder->sample_size = 1;
274         break;
275       case 16:
276         adder->func = (adder->is_signed ?
277             (GstAdderFunction) add_int16 : (GstAdderFunction) add_uint16);
278         adder->sample_size = 2;
279         break;
280       case 32:
281         adder->func = (adder->is_signed ?
282             (GstAdderFunction) add_int32 : (GstAdderFunction) add_uint32);
283         adder->sample_size = 4;
284         break;
285       default:
286         goto not_supported;
287     }
288   } else if (strcmp (media_type, "audio/x-raw-float") == 0) {
289     adder->format = GST_ADDER_FORMAT_FLOAT;
290     gst_structure_get_int (structure, "width", &adder->width);
291     gst_structure_get_int (structure, "endianness", &adder->endianness);
292
293     GST_INFO_OBJECT (pad, "parse_caps sets adder to format float, %d bit",
294         adder->width);
295
296     if (adder->endianness != G_BYTE_ORDER)
297       goto not_supported;
298
299     switch (adder->width) {
300       case 32:
301         adder->func = (GstAdderFunction) add_float32;
302         adder->sample_size = 4;
303         break;
304       case 64:
305         adder->func = (GstAdderFunction) add_float64;
306         adder->sample_size = 8;
307         break;
308       default:
309         goto not_supported;
310     }
311   } else {
312     goto not_supported;
313   }
314
315   gst_structure_get_int (structure, "channels", &adder->channels);
316   gst_structure_get_int (structure, "rate", &adder->rate);
317   /* precalc bps */
318   adder->bps = (adder->width / 8) * adder->channels;
319
320   return TRUE;
321
322   /* ERRORS */
323 not_supported:
324   {
325     GST_DEBUG_OBJECT (adder, "unsupported format set as caps");
326     return FALSE;
327   }
328 }
329
330 /* FIXME, the duration query should reflect how long you will produce
331  * data, that is the amount of stream time until you will emit EOS.
332  *
333  * For synchronized mixing this is always the max of all the durations
334  * of upstream since we emit EOS when all of them finished.
335  *
336  * We don't do synchronized mixing so this really depends on where the
337  * streams where punched in and what their relative offsets are against
338  * eachother which we can get from the first timestamps we see.
339  *
340  * When we add a new stream (or remove a stream) the duration might
341  * also become invalid again and we need to post a new DURATION
342  * message to notify this fact to the parent.
343  * For now we take the max of all the upstream elements so the simple
344  * cases work at least somewhat.
345  */
346 static gboolean
347 gst_adder_query_duration (GstAdder * adder, GstQuery * query)
348 {
349   gint64 max;
350   gboolean res;
351   GstFormat format;
352   GstIterator *it;
353   gboolean done;
354   GValue item = { 0, };
355
356   /* parse format */
357   gst_query_parse_duration (query, &format, NULL);
358
359   max = -1;
360   res = TRUE;
361   done = FALSE;
362
363   it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (adder));
364   while (!done) {
365     GstIteratorResult ires;
366
367     ires = gst_iterator_next (it, &item);
368     switch (ires) {
369       case GST_ITERATOR_DONE:
370         done = TRUE;
371         break;
372       case GST_ITERATOR_OK:
373       {
374         GstPad *pad = g_value_get_object (&item);
375         gint64 duration;
376
377         /* ask sink peer for duration */
378         res &= gst_pad_query_peer_duration (pad, &format, &duration);
379         /* take max from all valid return values */
380         if (res) {
381           /* valid unknown length, stop searching */
382           if (duration == -1) {
383             max = duration;
384             done = TRUE;
385           }
386           /* else see if bigger than current max */
387           else if (duration > max)
388             max = duration;
389         }
390         g_value_reset (&item);
391         break;
392       }
393       case GST_ITERATOR_RESYNC:
394         max = -1;
395         res = TRUE;
396         gst_iterator_resync (it);
397         break;
398       default:
399         res = FALSE;
400         done = TRUE;
401         break;
402     }
403   }
404   g_value_unset (&item);
405   gst_iterator_free (it);
406
407   if (res) {
408     /* and store the max */
409     GST_DEBUG_OBJECT (adder, "Total duration in format %s: %"
410         GST_TIME_FORMAT, gst_format_get_name (format), GST_TIME_ARGS (max));
411     gst_query_set_duration (query, format, max);
412   }
413
414   return res;
415 }
416
417 static gboolean
418 gst_adder_query_latency (GstAdder * adder, GstQuery * query)
419 {
420   GstClockTime min, max;
421   gboolean live;
422   gboolean res;
423   GstIterator *it;
424   gboolean done;
425   GValue item = { 0, };
426
427   res = TRUE;
428   done = FALSE;
429
430   live = FALSE;
431   min = 0;
432   max = GST_CLOCK_TIME_NONE;
433
434   /* Take maximum of all latency values */
435   it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (adder));
436   while (!done) {
437     GstIteratorResult ires;
438
439     ires = gst_iterator_next (it, &item);
440     switch (ires) {
441       case GST_ITERATOR_DONE:
442         done = TRUE;
443         break;
444       case GST_ITERATOR_OK:
445       {
446         GstPad *pad = g_value_get_object (&item);
447         GstQuery *peerquery;
448         GstClockTime min_cur, max_cur;
449         gboolean live_cur;
450
451         peerquery = gst_query_new_latency ();
452
453         /* Ask peer for latency */
454         res &= gst_pad_peer_query (pad, peerquery);
455
456         /* take max from all valid return values */
457         if (res) {
458           gst_query_parse_latency (peerquery, &live_cur, &min_cur, &max_cur);
459
460           if (min_cur > min)
461             min = min_cur;
462
463           if (max_cur != GST_CLOCK_TIME_NONE &&
464               ((max != GST_CLOCK_TIME_NONE && max_cur > max) ||
465                   (max == GST_CLOCK_TIME_NONE)))
466             max = max_cur;
467
468           live = live || live_cur;
469         }
470
471         gst_query_unref (peerquery);
472         g_value_reset (&item);
473         break;
474       }
475       case GST_ITERATOR_RESYNC:
476         live = FALSE;
477         min = 0;
478         max = GST_CLOCK_TIME_NONE;
479         res = TRUE;
480         gst_iterator_resync (it);
481         break;
482       default:
483         res = FALSE;
484         done = TRUE;
485         break;
486     }
487   }
488   g_value_unset (&item);
489   gst_iterator_free (it);
490
491   if (res) {
492     /* store the results */
493     GST_DEBUG_OBJECT (adder, "Calculated total latency: live %s, min %"
494         GST_TIME_FORMAT ", max %" GST_TIME_FORMAT,
495         (live ? "yes" : "no"), GST_TIME_ARGS (min), GST_TIME_ARGS (max));
496     gst_query_set_latency (query, live, min, max);
497   }
498
499   return res;
500 }
501
502 static gboolean
503 gst_adder_query (GstPad * pad, GstQuery * query)
504 {
505   GstAdder *adder = GST_ADDER (gst_pad_get_parent (pad));
506   gboolean res = FALSE;
507
508   switch (GST_QUERY_TYPE (query)) {
509     case GST_QUERY_POSITION:
510     {
511       GstFormat format;
512
513       gst_query_parse_position (query, &format, NULL);
514
515       switch (format) {
516         case GST_FORMAT_TIME:
517           /* FIXME, bring to stream time, might be tricky */
518           gst_query_set_position (query, format, adder->timestamp);
519           res = TRUE;
520           break;
521         case GST_FORMAT_DEFAULT:
522           gst_query_set_position (query, format, adder->offset);
523           res = TRUE;
524           break;
525         default:
526           break;
527       }
528       break;
529     }
530     case GST_QUERY_DURATION:
531       res = gst_adder_query_duration (adder, query);
532       break;
533     case GST_QUERY_LATENCY:
534       res = gst_adder_query_latency (adder, query);
535       break;
536     default:
537       /* FIXME, needs a custom query handler because we have multiple
538        * sinkpads */
539       res = gst_pad_query_default (pad, query);
540       break;
541   }
542
543   gst_object_unref (adder);
544   return res;
545 }
546
547 typedef struct
548 {
549   GstEvent *event;
550   gboolean flush;
551 } EventData;
552
553 static gboolean
554 forward_event_func (const GValue * val, GValue * ret, EventData * data)
555 {
556   GstPad *pad = g_value_get_object (val);
557   GstEvent *event = data->event;
558
559   gst_event_ref (event);
560   GST_LOG_OBJECT (pad, "About to send event %s", GST_EVENT_TYPE_NAME (event));
561   if (!gst_pad_push_event (pad, event)) {
562     GST_WARNING_OBJECT (pad, "Sending event  %p (%s) failed.",
563         event, GST_EVENT_TYPE_NAME (event));
564     /* quick hack to unflush the pads, ideally we need a way to just unflush
565      * this single collect pad */
566     if (data->flush)
567       gst_pad_send_event (pad, gst_event_new_flush_stop ());
568   } else {
569     g_value_set_boolean (ret, TRUE);
570     GST_LOG_OBJECT (pad, "Sent event  %p (%s).",
571         event, GST_EVENT_TYPE_NAME (event));
572   }
573
574   /* continue on other pads, even if one failed */
575   return TRUE;
576 }
577
578 /* forwards the event to all sinkpads, takes ownership of the
579  * event
580  *
581  * Returns: TRUE if the event could be forwarded on all
582  * sinkpads.
583  */
584 static gboolean
585 forward_event (GstAdder * adder, GstEvent * event, gboolean flush)
586 {
587   gboolean ret;
588   GstIterator *it;
589   GstIteratorResult ires;
590   GValue vret = { 0 };
591   EventData data;
592
593   GST_LOG_OBJECT (adder, "Forwarding event %p (%s)", event,
594       GST_EVENT_TYPE_NAME (event));
595
596   data.event = event;
597   data.flush = flush;
598
599   g_value_init (&vret, G_TYPE_BOOLEAN);
600   g_value_set_boolean (&vret, FALSE);
601   it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (adder));
602   while (TRUE) {
603     ires =
604         gst_iterator_fold (it, (GstIteratorFoldFunction) forward_event_func,
605         &vret, &data);
606     switch (ires) {
607       case GST_ITERATOR_RESYNC:
608         GST_WARNING ("resync");
609         gst_iterator_resync (it);
610         g_value_set_boolean (&vret, TRUE);
611         break;
612       case GST_ITERATOR_OK:
613       case GST_ITERATOR_DONE:
614         ret = g_value_get_boolean (&vret);
615         goto done;
616       default:
617         ret = FALSE;
618         goto done;
619     }
620   }
621 done:
622   gst_iterator_free (it);
623   GST_LOG_OBJECT (adder, "Forwarded event %p (%s), ret=%d", event,
624       GST_EVENT_TYPE_NAME (event), ret);
625   gst_event_unref (event);
626
627   return ret;
628 }
629
630 static gboolean
631 gst_adder_src_event (GstPad * pad, GstEvent * event)
632 {
633   GstAdder *adder;
634   gboolean result;
635
636   adder = GST_ADDER (gst_pad_get_parent (pad));
637
638   switch (GST_EVENT_TYPE (event)) {
639     case GST_EVENT_SEEK:
640     {
641       GstSeekFlags flags;
642       GstSeekType curtype, endtype;
643       gint64 cur, end;
644       gboolean flush;
645
646       /* parse the seek parameters */
647       gst_event_parse_seek (event, &adder->segment_rate, NULL, &flags, &curtype,
648           &cur, &endtype, &end);
649
650       if ((curtype != GST_SEEK_TYPE_NONE) && (curtype != GST_SEEK_TYPE_SET)) {
651         result = FALSE;
652         GST_DEBUG_OBJECT (adder,
653             "seeking failed, unhandled seek type for start: %d", curtype);
654         goto done;
655       }
656       if ((endtype != GST_SEEK_TYPE_NONE) && (endtype != GST_SEEK_TYPE_SET)) {
657         result = FALSE;
658         GST_DEBUG_OBJECT (adder,
659             "seeking failed, unhandled seek type for end: %d", endtype);
660         goto done;
661       }
662
663       flush = (flags & GST_SEEK_FLAG_FLUSH) == GST_SEEK_FLAG_FLUSH;
664
665       /* check if we are flushing */
666       if (flush) {
667         /* make sure we accept nothing anymore and return WRONG_STATE */
668         gst_collect_pads_set_flushing (adder->collect, TRUE);
669
670         /* flushing seek, start flush downstream, the flush will be done
671          * when all pads received a FLUSH_STOP. */
672         gst_pad_push_event (adder->srcpad, gst_event_new_flush_start ());
673
674         /* We can't send FLUSH_STOP here since upstream could start pushing data
675          * after we unlock adder->collect.
676          * We set flush_stop_pending to TRUE instead and send FLUSH_STOP after
677          * forwarding the seek upstream or from gst_adder_collected,
678          * whichever happens first.
679          */
680         adder->flush_stop_pending = TRUE;
681       }
682       GST_DEBUG_OBJECT (adder, "handling seek event: %" GST_PTR_FORMAT, event);
683
684       /* now wait for the collected to be finished and mark a new
685        * segment. After we have the lock, no collect function is running and no
686        * new collect function will be called for as long as we're flushing. */
687       GST_OBJECT_LOCK (adder->collect);
688       if (curtype == GST_SEEK_TYPE_SET)
689         adder->segment_start = cur;
690       else
691         adder->segment_start = 0;
692       if (endtype == GST_SEEK_TYPE_SET)
693         adder->segment_end = end;
694       else
695         adder->segment_end = GST_CLOCK_TIME_NONE;
696       /* make sure we push a new segment, to inform about new basetime
697        * see FIXME in gst_adder_collected() */
698       adder->segment_pending = TRUE;
699       if (flush) {
700         /* Yes, we need to call _set_flushing again *WHEN* the streaming threads
701          * have stopped so that the cookie gets properly updated. */
702         gst_collect_pads_set_flushing (adder->collect, TRUE);
703       }
704       GST_OBJECT_UNLOCK (adder->collect);
705       GST_DEBUG_OBJECT (adder, "forwarding seek event: %" GST_PTR_FORMAT,
706           event);
707
708       result = forward_event (adder, event, flush);
709       if (!result) {
710         /* seek failed. maybe source is a live source. */
711         GST_DEBUG_OBJECT (adder, "seeking failed");
712       }
713       if (g_atomic_int_compare_and_exchange (&adder->flush_stop_pending,
714               TRUE, FALSE)) {
715         GST_DEBUG_OBJECT (adder, "pending flush stop");
716         gst_pad_push_event (adder->srcpad, gst_event_new_flush_stop ());
717       }
718       break;
719     }
720     case GST_EVENT_QOS:
721       /* QoS might be tricky */
722       result = FALSE;
723       break;
724     case GST_EVENT_NAVIGATION:
725       /* navigation is rather pointless. */
726       result = FALSE;
727       break;
728     default:
729       /* just forward the rest for now */
730       GST_DEBUG_OBJECT (adder, "forward unhandled event: %s",
731           GST_EVENT_TYPE_NAME (event));
732       result = forward_event (adder, event, FALSE);
733       break;
734   }
735
736 done:
737   gst_object_unref (adder);
738
739   return result;
740 }
741
742 static gboolean
743 gst_adder_sink_event (GstPad * pad, GstEvent * event)
744 {
745   GstAdder *adder;
746   gboolean ret = TRUE;
747
748   adder = GST_ADDER (gst_pad_get_parent (pad));
749
750   GST_DEBUG ("Got %s event on pad %s:%s", GST_EVENT_TYPE_NAME (event),
751       GST_DEBUG_PAD_NAME (pad));
752
753   switch (GST_EVENT_TYPE (event)) {
754     case GST_EVENT_FLUSH_STOP:
755       /* we received a flush-stop. The collect_event function will push the
756        * event past our element. We simply forward all flush-stop events, even
757        * when no flush-stop was pending, this is required because collectpads
758        * does not provide an API to handle-but-not-forward the flush-stop.
759        * We unset the pending flush-stop flag so that we don't send anymore
760        * flush-stop from the collect function later.
761        */
762       GST_OBJECT_LOCK (adder->collect);
763       adder->segment_pending = TRUE;
764       adder->flush_stop_pending = FALSE;
765       /* Clear pending tags */
766       if (adder->pending_events) {
767         g_list_foreach (adder->pending_events, (GFunc) gst_event_unref, NULL);
768         g_list_free (adder->pending_events);
769         adder->pending_events = NULL;
770       }
771       GST_OBJECT_UNLOCK (adder->collect);
772       break;
773     case GST_EVENT_TAG:
774       GST_OBJECT_LOCK (adder->collect);
775       /* collect tags here so we can push them out when we collect data */
776       adder->pending_events = g_list_append (adder->pending_events, event);
777       GST_OBJECT_UNLOCK (adder->collect);
778       goto beach;
779     default:
780       break;
781   }
782
783   /* now GstCollectPads can take care of the rest, e.g. EOS */
784   ret = adder->collect_event (pad, event);
785
786 beach:
787   gst_object_unref (adder);
788   return ret;
789 }
790
791 static void
792 gst_adder_class_init (GstAdderClass * klass)
793 {
794   GObjectClass *gobject_class = (GObjectClass *) klass;
795   GstElementClass *gstelement_class = (GstElementClass *) klass;
796
797   gobject_class->set_property = gst_adder_set_property;
798   gobject_class->get_property = gst_adder_get_property;
799   gobject_class->dispose = gst_adder_dispose;
800
801   /**
802    * GstAdder:caps:
803    *
804    * Since: 0.10.24
805    */
806   g_object_class_install_property (gobject_class, PROP_FILTER_CAPS,
807       g_param_spec_boxed ("caps", "Target caps",
808           "Set target format for mixing (NULL means ANY). "
809           "Setting this property takes a reference to the supplied GstCaps "
810           "object.", GST_TYPE_CAPS,
811           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
812
813   gst_element_class_add_pad_template (gstelement_class,
814       gst_static_pad_template_get (&gst_adder_src_template));
815   gst_element_class_add_pad_template (gstelement_class,
816       gst_static_pad_template_get (&gst_adder_sink_template));
817   gst_element_class_set_details_simple (gstelement_class, "Adder",
818       "Generic/Audio",
819       "Add N audio channels together",
820       "Thomas Vander Stichele <thomas at apestaart dot org>");
821
822   gstelement_class->request_new_pad =
823       GST_DEBUG_FUNCPTR (gst_adder_request_new_pad);
824   gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_adder_release_pad);
825   gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_adder_change_state);
826 }
827
828 static void
829 gst_adder_init (GstAdder * adder)
830 {
831   GstPadTemplate *template;
832
833   template = gst_static_pad_template_get (&gst_adder_src_template);
834   adder->srcpad = gst_pad_new_from_template (template, "src");
835   gst_object_unref (template);
836
837   gst_pad_set_getcaps_function (adder->srcpad,
838       GST_DEBUG_FUNCPTR (gst_pad_proxy_getcaps));
839   gst_pad_set_setcaps_function (adder->srcpad,
840       GST_DEBUG_FUNCPTR (gst_adder_setcaps));
841   gst_pad_set_query_function (adder->srcpad,
842       GST_DEBUG_FUNCPTR (gst_adder_query));
843   gst_pad_set_event_function (adder->srcpad,
844       GST_DEBUG_FUNCPTR (gst_adder_src_event));
845   gst_element_add_pad (GST_ELEMENT (adder), adder->srcpad);
846
847   adder->format = GST_ADDER_FORMAT_UNSET;
848   adder->padcount = 0;
849   adder->func = NULL;
850
851   adder->filter_caps = NULL;
852
853   /* keep track of the sinkpads requested */
854   adder->collect = gst_collect_pads_new ();
855   gst_collect_pads_set_function (adder->collect,
856       GST_DEBUG_FUNCPTR (gst_adder_collected), adder);
857   gst_collect_pads_set_clip_function (adder->collect,
858       GST_DEBUG_FUNCPTR (gst_adder_do_clip), adder);
859 }
860
861 static void
862 gst_adder_dispose (GObject * object)
863 {
864   GstAdder *adder = GST_ADDER (object);
865
866   if (adder->collect) {
867     gst_object_unref (adder->collect);
868     adder->collect = NULL;
869   }
870   gst_caps_replace (&adder->filter_caps, NULL);
871   if (adder->pending_events) {
872     g_list_foreach (adder->pending_events, (GFunc) gst_event_unref, NULL);
873     g_list_free (adder->pending_events);
874     adder->pending_events = NULL;
875   }
876
877   G_OBJECT_CLASS (parent_class)->dispose (object);
878 }
879
880 static void
881 gst_adder_set_property (GObject * object, guint prop_id,
882     const GValue * value, GParamSpec * pspec)
883 {
884   GstAdder *adder = GST_ADDER (object);
885
886   switch (prop_id) {
887     case PROP_FILTER_CAPS:{
888       GstCaps *new_caps = NULL;
889       GstCaps *old_caps;
890       const GstCaps *new_caps_val = gst_value_get_caps (value);
891
892       if (new_caps_val != NULL) {
893         new_caps = (GstCaps *) new_caps_val;
894         gst_caps_ref (new_caps);
895       }
896
897       GST_OBJECT_LOCK (adder);
898       old_caps = adder->filter_caps;
899       adder->filter_caps = new_caps;
900       GST_OBJECT_UNLOCK (adder);
901
902       if (old_caps)
903         gst_caps_unref (old_caps);
904
905       GST_DEBUG_OBJECT (adder, "set new caps %" GST_PTR_FORMAT, new_caps);
906       break;
907     }
908     default:
909       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
910       break;
911   }
912 }
913
914 static void
915 gst_adder_get_property (GObject * object, guint prop_id, GValue * value,
916     GParamSpec * pspec)
917 {
918   GstAdder *adder = GST_ADDER (object);
919
920   switch (prop_id) {
921     case PROP_FILTER_CAPS:
922       GST_OBJECT_LOCK (adder);
923       gst_value_set_caps (value, adder->filter_caps);
924       GST_OBJECT_UNLOCK (adder);
925       break;
926     default:
927       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
928       break;
929   }
930 }
931
932
933 static GstPad *
934 gst_adder_request_new_pad (GstElement * element, GstPadTemplate * templ,
935     const gchar * unused)
936 {
937   gchar *name;
938   GstAdder *adder;
939   GstPad *newpad;
940   gint padcount;
941
942   if (templ->direction != GST_PAD_SINK)
943     goto not_sink;
944
945   adder = GST_ADDER (element);
946
947   /* increment pad counter */
948   padcount = g_atomic_int_exchange_and_add (&adder->padcount, 1);
949
950   name = g_strdup_printf ("sink%d", padcount);
951   newpad = gst_pad_new_from_template (templ, name);
952   GST_DEBUG_OBJECT (adder, "request new pad %s", name);
953   g_free (name);
954
955   gst_pad_set_getcaps_function (newpad,
956       GST_DEBUG_FUNCPTR (gst_adder_sink_getcaps));
957   gst_pad_set_setcaps_function (newpad, GST_DEBUG_FUNCPTR (gst_adder_setcaps));
958   gst_collect_pads_add_pad (adder->collect, newpad, sizeof (GstCollectData));
959
960   /* FIXME: hacked way to override/extend the event function of
961    * GstCollectPads; because it sets its own event function giving the
962    * element no access to events */
963   adder->collect_event = (GstPadEventFunction) GST_PAD_EVENTFUNC (newpad);
964   gst_pad_set_event_function (newpad, GST_DEBUG_FUNCPTR (gst_adder_sink_event));
965
966   /* takes ownership of the pad */
967   if (!gst_element_add_pad (GST_ELEMENT (adder), newpad))
968     goto could_not_add;
969
970   return newpad;
971
972   /* errors */
973 not_sink:
974   {
975     g_warning ("gstadder: request new pad that is not a SINK pad\n");
976     return NULL;
977   }
978 could_not_add:
979   {
980     GST_DEBUG_OBJECT (adder, "could not add pad");
981     gst_collect_pads_remove_pad (adder->collect, newpad);
982     gst_object_unref (newpad);
983     return NULL;
984   }
985 }
986
987 static void
988 gst_adder_release_pad (GstElement * element, GstPad * pad)
989 {
990   GstAdder *adder;
991
992   adder = GST_ADDER (element);
993
994   GST_DEBUG_OBJECT (adder, "release pad %s:%s", GST_DEBUG_PAD_NAME (pad));
995
996   gst_collect_pads_remove_pad (adder->collect, pad);
997   gst_element_remove_pad (element, pad);
998 }
999
1000 static GstBuffer *
1001 gst_adder_do_clip (GstCollectPads * pads, GstCollectData * data,
1002     GstBuffer * buffer, gpointer user_data)
1003 {
1004   GstAdder *adder = GST_ADDER (user_data);
1005
1006   buffer = gst_audio_buffer_clip (buffer, &data->segment, adder->rate,
1007       adder->bps);
1008
1009   return buffer;
1010 }
1011
1012 static GstFlowReturn
1013 gst_adder_collected (GstCollectPads * pads, gpointer user_data)
1014 {
1015   /*
1016    * combine streams by adding data values
1017    * basic algorithm :
1018    * - this function is called when all pads have a buffer
1019    * - get available bytes on all pads.
1020    * - repeat for each input pad :
1021    *   - read available bytes, copy or add to target buffer
1022    *   - if there's an EOS event, remove the input channel
1023    * - push out the output buffer
1024    *
1025    * todo:
1026    * - would be nice to have a mixing mode, where instead of adding we mix
1027    *   - for float we could downscale after collect loop
1028    *   - for int we need to downscale each input to avoid clipping or
1029    *     mix into a temp (float) buffer and scale afterwards as well
1030    */
1031   GstAdder *adder;
1032   GSList *collected, *next = NULL;
1033   GstFlowReturn ret;
1034   GstBuffer *outbuf = NULL, *gapbuf = NULL;
1035   gpointer outdata = NULL;
1036   guint outsize;
1037   gint64 next_offset;
1038   gint64 next_timestamp;
1039
1040   adder = GST_ADDER (user_data);
1041
1042   /* this is fatal */
1043   if (G_UNLIKELY (adder->func == NULL))
1044     goto not_negotiated;
1045
1046   if (g_atomic_int_compare_and_exchange (&adder->flush_stop_pending,
1047           TRUE, FALSE)) {
1048     GST_DEBUG_OBJECT (adder, "pending flush stop");
1049     gst_pad_push_event (adder->srcpad, gst_event_new_flush_stop ());
1050   }
1051
1052   /* get available bytes for reading, this can be 0 which could mean empty
1053    * buffers or EOS, which we will catch when we loop over the pads. */
1054   outsize = gst_collect_pads_available (pads);
1055   /* can only happen when no pads to collect or all EOS */
1056   if (outsize == 0)
1057     goto eos;
1058
1059   GST_LOG_OBJECT (adder,
1060       "starting to cycle through channels, %d bytes available (bps = %d)",
1061       outsize, adder->bps);
1062
1063   for (collected = pads->data; collected; collected = next) {
1064     GstCollectData *collect_data;
1065     GstBuffer *inbuf;
1066     gboolean is_gap;
1067
1068     /* take next to see if this is the last collectdata */
1069     next = g_slist_next (collected);
1070
1071     collect_data = (GstCollectData *) collected->data;
1072
1073     /* get a buffer of size bytes, if we get a buffer, it is at least outsize
1074      * bytes big. */
1075     inbuf = gst_collect_pads_take_buffer (pads, collect_data, outsize);
1076     /* NULL means EOS or an empty buffer so we still need to flush in
1077      * case of an empty buffer. */
1078     if (inbuf == NULL) {
1079       GST_LOG_OBJECT (adder, "channel %p: no bytes available", collect_data);
1080       continue;
1081     }
1082
1083     is_gap = GST_BUFFER_FLAG_IS_SET (inbuf, GST_BUFFER_FLAG_GAP);
1084
1085     /* Try to make an output buffer */
1086     if (outbuf == NULL) {
1087       /* if this is a gap buffer but we have some more pads to check, skip it.
1088        * If we are at the last buffer, take it, regardless if it is a GAP
1089        * buffer or not. */
1090       if (is_gap && next) {
1091         GST_DEBUG_OBJECT (adder, "skipping, non-last GAP buffer");
1092         /* we keep the GAP buffer, if we don't have anymore buffers (all pads
1093          * EOS, we can use this one as the output buffer. */
1094         if (gapbuf == NULL)
1095           gapbuf = inbuf;
1096         else
1097           gst_buffer_unref (inbuf);
1098         continue;
1099       }
1100
1101       GST_LOG_OBJECT (adder, "channel %p: preparing output buffer of %d bytes",
1102           collect_data, outsize);
1103       /* make data and metadata writable, can simply return the inbuf when we
1104        * are the only one referencing this buffer. If this is the last (and
1105        * only) GAP buffer, it will automatically copy the GAP flag. */
1106       outbuf = gst_buffer_make_writable (inbuf);
1107       gst_buffer_set_caps (outbuf, GST_PAD_CAPS (adder->srcpad));
1108
1109       outdata = gst_buffer_map (outbuf, NULL, NULL, GST_MAP_WRITE);
1110     } else {
1111       if (!is_gap) {
1112         /* we had a previous output buffer, mix this non-GAP buffer */
1113         guint8 *indata;
1114         gsize insize;
1115
1116         indata = gst_buffer_map (inbuf, &insize, NULL, GST_MAP_READ);
1117
1118         /* all buffers should have outsize, there are no short buffers because we
1119          * asked for the max size above */
1120         g_assert (insize == outsize);
1121
1122         GST_LOG_OBJECT (adder, "channel %p: mixing %d bytes from data %p",
1123             collect_data, insize, indata);
1124
1125         /* further buffers, need to add them */
1126         adder->func ((gpointer) outdata, (gpointer) indata,
1127             insize / adder->sample_size);
1128         gst_buffer_unmap (inbuf, indata, insize);
1129       } else {
1130         /* skip gap buffer */
1131         GST_LOG_OBJECT (adder, "channel %p: skipping GAP buffer", collect_data);
1132       }
1133       gst_buffer_unref (inbuf);
1134     }
1135   }
1136   if (outbuf)
1137     gst_buffer_unmap (outbuf, outdata, outsize);
1138
1139   if (outbuf == NULL) {
1140     /* no output buffer, reuse one of the GAP buffers then if we have one */
1141     if (gapbuf) {
1142       GST_LOG_OBJECT (adder, "reusing GAP buffer %p", gapbuf);
1143       outbuf = gapbuf;
1144     } else
1145       /* assume EOS otherwise, this should not happen, really */
1146       goto eos;
1147   } else if (gapbuf)
1148     /* we had an output buffer, unref the gapbuffer we kept */
1149     gst_buffer_unref (gapbuf);
1150
1151   if (adder->segment_pending) {
1152     GstEvent *event;
1153
1154     /* FIXME, use rate/applied_rate as set on all sinkpads.
1155      * - currently we just set rate as received from last seek-event
1156      *
1157      * When seeking we set the start and stop positions as given in the seek
1158      * event. We also adjust offset & timestamp acordingly.
1159      * This basically ignores all newsegments sent by upstream.
1160      */
1161     event = gst_event_new_new_segment_full (FALSE, adder->segment_rate,
1162         1.0, GST_FORMAT_TIME, adder->segment_start, adder->segment_end,
1163         adder->segment_start);
1164     if (adder->segment_rate > 0.0) {
1165       adder->timestamp = adder->segment_start;
1166     } else {
1167       adder->timestamp = adder->segment_end;
1168     }
1169     adder->offset = gst_util_uint64_scale (adder->timestamp,
1170         adder->rate, GST_SECOND);
1171     GST_INFO_OBJECT (adder, "seg_start %" G_GUINT64_FORMAT ", seg_end %"
1172         G_GUINT64_FORMAT, adder->segment_start, adder->segment_end);
1173     GST_INFO_OBJECT (adder, "timestamp %" G_GINT64_FORMAT ",new offset %"
1174         G_GINT64_FORMAT, adder->timestamp, adder->offset);
1175
1176     if (event) {
1177       if (!gst_pad_push_event (adder->srcpad, event)) {
1178         GST_WARNING_OBJECT (adder->srcpad, "Sending event  %p (%s) failed.",
1179             event, GST_EVENT_TYPE_NAME (event));
1180       }
1181       adder->segment_pending = FALSE;
1182     } else {
1183       GST_WARNING_OBJECT (adder->srcpad, "Creating new segment event for "
1184           "start:%" G_GINT64_FORMAT "  end:%" G_GINT64_FORMAT " failed",
1185           adder->segment_start, adder->segment_end);
1186     }
1187   }
1188
1189   if (G_UNLIKELY (adder->pending_events)) {
1190     GList *tmp = adder->pending_events;
1191
1192     while (tmp) {
1193       GstEvent *ev = (GstEvent *) tmp->data;
1194
1195       gst_pad_push_event (adder->srcpad, ev);
1196       tmp = g_list_next (tmp);
1197     }
1198     g_list_free (adder->pending_events);
1199     adder->pending_events = NULL;
1200   }
1201
1202   /* for the next timestamp, use the sample counter, which will
1203    * never accumulate rounding errors */
1204   if (adder->segment_rate > 0.0) {
1205     next_offset = adder->offset + outsize / adder->bps;
1206   } else {
1207     next_offset = adder->offset - outsize / adder->bps;
1208   }
1209   next_timestamp = gst_util_uint64_scale (next_offset, GST_SECOND, adder->rate);
1210
1211
1212   /* set timestamps on the output buffer */
1213   if (adder->segment_rate > 0.0) {
1214     GST_BUFFER_TIMESTAMP (outbuf) = adder->timestamp;
1215     GST_BUFFER_OFFSET (outbuf) = adder->offset;
1216     GST_BUFFER_OFFSET_END (outbuf) = next_offset;
1217     GST_BUFFER_DURATION (outbuf) = next_timestamp - adder->timestamp;
1218   } else {
1219     GST_BUFFER_TIMESTAMP (outbuf) = next_timestamp;
1220     GST_BUFFER_OFFSET (outbuf) = next_offset;
1221     GST_BUFFER_OFFSET_END (outbuf) = adder->offset;
1222     GST_BUFFER_DURATION (outbuf) = adder->timestamp - next_timestamp;
1223   }
1224
1225   adder->offset = next_offset;
1226   adder->timestamp = next_timestamp;
1227
1228   /* send it out */
1229   GST_LOG_OBJECT (adder, "pushing outbuf %p, timestamp %" GST_TIME_FORMAT
1230       " offset %" G_GINT64_FORMAT, outbuf,
1231       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
1232       GST_BUFFER_OFFSET (outbuf));
1233   ret = gst_pad_push (adder->srcpad, outbuf);
1234
1235   GST_LOG_OBJECT (adder, "pushed outbuf, result = %s", gst_flow_get_name (ret));
1236
1237   return ret;
1238
1239   /* ERRORS */
1240 not_negotiated:
1241   {
1242     GST_ELEMENT_ERROR (adder, STREAM, FORMAT, (NULL),
1243         ("Unknown data received, not negotiated"));
1244     return GST_FLOW_NOT_NEGOTIATED;
1245   }
1246 eos:
1247   {
1248     GST_DEBUG_OBJECT (adder, "no data available, must be EOS");
1249     gst_pad_push_event (adder->srcpad, gst_event_new_eos ());
1250     return GST_FLOW_UNEXPECTED;
1251   }
1252 }
1253
1254 static GstStateChangeReturn
1255 gst_adder_change_state (GstElement * element, GstStateChange transition)
1256 {
1257   GstAdder *adder;
1258   GstStateChangeReturn ret;
1259
1260   adder = GST_ADDER (element);
1261
1262   switch (transition) {
1263     case GST_STATE_CHANGE_NULL_TO_READY:
1264       break;
1265     case GST_STATE_CHANGE_READY_TO_PAUSED:
1266       adder->timestamp = 0;
1267       adder->offset = 0;
1268       adder->flush_stop_pending = FALSE;
1269       adder->segment_pending = TRUE;
1270       adder->segment_start = 0;
1271       adder->segment_end = GST_CLOCK_TIME_NONE;
1272       adder->segment_rate = 1.0;
1273       gst_segment_init (&adder->segment, GST_FORMAT_UNDEFINED);
1274       gst_collect_pads_start (adder->collect);
1275       break;
1276     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1277       break;
1278     case GST_STATE_CHANGE_PAUSED_TO_READY:
1279       /* need to unblock the collectpads before calling the
1280        * parent change_state so that streaming can finish */
1281       gst_collect_pads_stop (adder->collect);
1282       break;
1283     default:
1284       break;
1285   }
1286
1287   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1288
1289   switch (transition) {
1290     default:
1291       break;
1292   }
1293
1294   return ret;
1295 }
1296
1297
1298 static gboolean
1299 plugin_init (GstPlugin * plugin)
1300 {
1301   GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "adder", 0,
1302       "audio channel mixing element");
1303
1304   gst_adder_orc_init ();
1305
1306   if (!gst_element_register (plugin, "adder", GST_RANK_NONE, GST_TYPE_ADDER)) {
1307     return FALSE;
1308   }
1309
1310   return TRUE;
1311 }
1312
1313 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1314     GST_VERSION_MINOR,
1315     "adder",
1316     "Adds multiple streams",
1317     plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)