rework configure.ac; make asterisk rtp stuff compile on mingw
[platform/upstream/gst-plugins-good.git] / gst / auparse / gstauparse.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19 /* Element-Checklist-Version: 5 */
20
21 /* 2001/04/03 - Updated parseau to use caps nego
22  *              Zaheer Abbas Merali <zaheerabbas at merali dot org>
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include "gstauparse.h"
32 #include <gst/audio/audio.h>
33
34 /* elementfactory information */
35 static GstElementDetails gst_auparse_details =
36 GST_ELEMENT_DETAILS (".au parser",
37     "Codec/Demuxer/Audio",
38     "Parse an .au file into raw audio",
39     "Erik Walthinsen <omega@cse.ogi.edu>");
40
41 static GstStaticPadTemplate gst_auparse_sink_template =
42 GST_STATIC_PAD_TEMPLATE ("sink",
43     GST_PAD_SINK,
44     GST_PAD_ALWAYS,
45     GST_STATIC_CAPS ("audio/x-au")
46     );
47
48 static GstStaticPadTemplate gst_auparse_src_template =
49     GST_STATIC_PAD_TEMPLATE ("src",
50     GST_PAD_SRC,
51     GST_PAD_SOMETIMES,          /* FIXME: spider */
52     GST_STATIC_CAPS (GST_AUDIO_INT_PAD_TEMPLATE_CAPS "; "
53         /* we don't use GST_AUDIO_FLOAT_PAD_TEMPLATE_CAPS
54            because of min buffer-frames which is 1, not 0 */
55         "audio/x-raw-float, "
56         "rate = (int) [ 1, MAX ], "
57         "channels = (int) [ 1, MAX ], "
58         "endianness = (int) { LITTLE_ENDIAN , BIG_ENDIAN }, "
59         "width = (int) { 32, 64 }, "
60         "buffer-frames = (int) [ 0, MAX]" "; "
61         "audio/x-alaw, "
62         "rate = (int) [ 8000, 192000 ], "
63         "channels = (int) [ 1, 2 ]" "; "
64         "audio/x-mulaw, "
65         "rate = (int) [ 8000, 192000 ], " "channels = (int) [ 1, 2 ]" "; "
66         /* Nothing to decode those ADPCM streams for now */
67         "audio/x-adpcm, " "layout = (string) { g721, g722, g723_3, g723_5 }")
68     );
69
70 enum
71 {
72   ARG_0
73       /* FILL ME */
74 };
75
76 static void gst_auparse_base_init (gpointer g_class);
77 static void gst_auparse_class_init (GstAuParseClass * klass);
78 static void gst_auparse_init (GstAuParse * auparse);
79
80 static GstFlowReturn gst_auparse_chain (GstPad * pad, GstBuffer * buf);
81
82 static GstStateChangeReturn gst_auparse_change_state (GstElement * element,
83     GstStateChange transition);
84
85 static GstElementClass *parent_class = NULL;
86
87
88 /*static guint gst_auparse_signals[LAST_SIGNAL] = { 0 }; */
89
90 GType
91 gst_auparse_get_type (void)
92 {
93   static GType auparse_type = 0;
94
95   if (!auparse_type) {
96     static const GTypeInfo auparse_info = {
97       sizeof (GstAuParseClass),
98       gst_auparse_base_init,
99       NULL,
100       (GClassInitFunc) gst_auparse_class_init,
101       NULL,
102       NULL,
103       sizeof (GstAuParse),
104       0,
105       (GInstanceInitFunc) gst_auparse_init,
106     };
107
108     auparse_type =
109         g_type_register_static (GST_TYPE_ELEMENT, "GstAuParse", &auparse_info,
110         0);
111   }
112   return auparse_type;
113 }
114
115 static void
116 gst_auparse_base_init (gpointer g_class)
117 {
118   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
119
120   gst_element_class_add_pad_template (element_class,
121       gst_static_pad_template_get (&gst_auparse_sink_template));
122   gst_element_class_add_pad_template (element_class,
123       gst_static_pad_template_get (&gst_auparse_src_template));
124   gst_element_class_set_details (element_class, &gst_auparse_details);
125
126 }
127
128 static void
129 gst_auparse_class_init (GstAuParseClass * klass)
130 {
131   GstElementClass *gstelement_class;
132
133   gstelement_class = (GstElementClass *) klass;
134
135   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
136
137   gstelement_class->change_state = gst_auparse_change_state;
138 }
139
140 static void
141 gst_auparse_init (GstAuParse * auparse)
142 {
143   auparse->sinkpad =
144       gst_pad_new_from_template (gst_static_pad_template_get
145       (&gst_auparse_sink_template), "sink");
146   gst_element_add_pad (GST_ELEMENT (auparse), auparse->sinkpad);
147   gst_pad_set_chain_function (auparse->sinkpad, gst_auparse_chain);
148
149   auparse->srcpad = gst_pad_new_from_template (gst_static_pad_template_get
150       (&gst_auparse_src_template), "src");
151   gst_pad_use_fixed_caps (auparse->srcpad);
152   gst_element_add_pad (GST_ELEMENT (auparse), auparse->srcpad);
153
154   auparse->offset = 0;
155   auparse->size = 0;
156   auparse->encoding = 0;
157   auparse->frequency = 0;
158   auparse->channels = 0;
159 }
160
161 static GstFlowReturn
162 gst_auparse_chain (GstPad * pad, GstBuffer * buf)
163 {
164   GstFlowReturn ret;
165   GstAuParse *auparse;
166   guchar *data;
167   glong size;
168   GstCaps *tempcaps;
169   gint law = 0, depth = 0, ieee = 0;
170   gchar layout[7];
171
172   layout[0] = 0;
173
174   auparse = GST_AUPARSE (gst_pad_get_parent (pad));
175
176   GST_DEBUG ("gst_auparse_chain: got buffer in '%s'",
177       gst_element_get_name (GST_ELEMENT (auparse)));
178
179   data = GST_BUFFER_DATA (buf);
180   size = GST_BUFFER_SIZE (buf);
181
182   /* if we haven't seen any data yet... */
183   if (auparse->size == 0) {
184     GstBuffer *newbuf;
185     guint32 *head = (guint32 *) data;
186
187     /* normal format is big endian (au is a Sparc format) */
188     if (GST_READ_UINT32_BE (head) == 0x2e736e64) {      /* ".snd" */
189       head++;
190       auparse->le = 0;
191       auparse->offset = GST_READ_UINT32_BE (head);
192       head++;
193       /* Do not trust size, could be set to -1 : unknown */
194       auparse->size = GST_READ_UINT32_BE (head);
195       head++;
196       auparse->encoding = GST_READ_UINT32_BE (head);
197       head++;
198       auparse->frequency = GST_READ_UINT32_BE (head);
199       head++;
200       auparse->channels = GST_READ_UINT32_BE (head);
201       head++;
202
203       /* and of course, someone had to invent a little endian
204        * version.  Used by DEC systems. */
205     } else if (GST_READ_UINT32_LE (head) == 0x0064732E) {       /* other source say it is "dns." */
206       head++;
207       auparse->le = 1;
208       auparse->offset = GST_READ_UINT32_LE (head);
209       head++;
210       /* Do not trust size, could be set to -1 : unknown */
211       auparse->size = GST_READ_UINT32_LE (head);
212       head++;
213       auparse->encoding = GST_READ_UINT32_LE (head);
214       head++;
215       auparse->frequency = GST_READ_UINT32_LE (head);
216       head++;
217       auparse->channels = GST_READ_UINT32_LE (head);
218       head++;
219
220     } else {
221       GST_ELEMENT_ERROR (auparse, STREAM, WRONG_TYPE, (NULL), (NULL));
222       gst_buffer_unref (buf);
223       g_object_unref (auparse);
224       return GST_FLOW_ERROR;
225     }
226
227     GST_DEBUG
228         ("offset %ld, size %ld, encoding %ld, frequency %ld, channels %ld\n",
229         auparse->offset, auparse->size, auparse->encoding, auparse->frequency,
230         auparse->channels);
231
232 /*
233 Docs :
234         http://www.opengroup.org/public/pubs/external/auformat.html
235         http://astronomy.swin.edu.au/~pbourke/dataformats/au/
236         Solaris headers : /usr/include/audio/au.h
237         libsndfile : src/au.c
238 Samples :
239         http://www.tsp.ece.mcgill.ca/MMSP/Documents/AudioFormats/AU/Samples.html
240 */
241
242     switch (auparse->encoding) {
243
244       case 1:                  /* 8-bit ISDN mu-law G.711 */
245         law = 1;
246         depth = 8;
247         break;
248       case 27:                 /* 8-bit ISDN  A-law G.711 */
249         law = 2;
250         depth = 8;
251         break;
252
253       case 2:                  /*  8-bit linear PCM */
254         depth = 8;
255         break;
256       case 3:                  /* 16-bit linear PCM */
257         depth = 16;
258         break;
259       case 4:                  /* 24-bit linear PCM */
260         depth = 24;
261         break;
262       case 5:                  /* 32-bit linear PCM */
263         depth = 32;
264         break;
265
266       case 6:                  /* 32-bit IEEE floating point */
267         ieee = 1;
268         depth = 32;
269         break;
270       case 7:                  /* 64-bit IEEE floating point */
271         ieee = 1;
272         depth = 64;
273         break;
274
275       case 23:                 /* 4-bit CCITT G.721   ADPCM 32kbps -> modplug/libsndfile (compressed 8-bit mu-law) */
276         strcpy (layout, "g721");
277         break;
278       case 24:                 /* 8-bit CCITT G.722   ADPCM        -> rtp */
279         strcpy (layout, "g722");
280         break;
281       case 25:                 /* 3-bit CCITT G.723.3 ADPCM 24kbps -> rtp/xine/modplug/libsndfile */
282         strcpy (layout, "g723_3");
283         break;
284       case 26:                 /* 5-bit CCITT G.723.5 ADPCM 40kbps -> rtp/xine/modplug/libsndfile */
285         strcpy (layout, "g723_5");
286         break;
287
288       case 8:                  /* Fragmented sample data */
289       case 9:                  /* AU_ENCODING_NESTED */
290
291       case 10:                 /* DSP program */
292       case 11:                 /* DSP  8-bit fixed point */
293       case 12:                 /* DSP 16-bit fixed point */
294       case 13:                 /* DSP 24-bit fixed point */
295       case 14:                 /* DSP 32-bit fixed point */
296
297       case 16:                 /* AU_ENCODING_DISPLAY : non-audio display data */
298       case 17:                 /* AU_ENCODING_MULAW_SQUELCH */
299
300       case 18:                 /* 16-bit linear with emphasis */
301       case 19:                 /* 16-bit linear compressed (NeXT) */
302       case 20:                 /* 16-bit linear with emphasis and compression */
303
304       case 21:                 /* Music kit DSP commands */
305       case 22:                 /* Music kit DSP commands samples */
306
307       default:
308         GST_ELEMENT_ERROR (auparse, STREAM, FORMAT, (NULL), (NULL));
309         gst_buffer_unref (buf);
310         g_object_unref (auparse);
311         return GST_FLOW_ERROR;
312     }
313
314     if (law) {
315       tempcaps =
316           gst_caps_new_simple ((law == 1) ? "audio/x-mulaw" : "audio/x-alaw",
317           "rate", G_TYPE_INT, auparse->frequency,
318           "channels", G_TYPE_INT, auparse->channels, NULL);
319     } else if (ieee) {
320       tempcaps = gst_caps_new_simple ("audio/x-raw-float",
321           "rate", G_TYPE_INT, auparse->frequency,
322           "channels", G_TYPE_INT, auparse->channels,
323           "endianness", G_TYPE_INT,
324           auparse->le ? G_LITTLE_ENDIAN : G_BIG_ENDIAN, "width", G_TYPE_INT,
325           depth, "buffer-frames", G_TYPE_INT, 0, NULL);
326     } else if (layout[0]) {
327       tempcaps = gst_caps_new_simple ("audio/x-adpcm",
328           "layout", G_TYPE_STRING, layout, NULL);
329     } else {
330       tempcaps = gst_caps_new_simple ("audio/x-raw-int",
331           "rate", G_TYPE_INT, auparse->frequency,
332           "channels", G_TYPE_INT, auparse->channels,
333           "endianness", G_TYPE_INT,
334           auparse->le ? G_LITTLE_ENDIAN : G_BIG_ENDIAN, "depth", G_TYPE_INT,
335           depth, "width", G_TYPE_INT, depth, "signed", G_TYPE_BOOLEAN, TRUE,
336           NULL);
337     }
338
339     gst_pad_set_active (auparse->srcpad, TRUE);
340     gst_pad_set_caps (auparse->srcpad, tempcaps);
341
342     if ((ret = gst_pad_alloc_buffer (auparse->srcpad, GST_BUFFER_OFFSET_NONE,
343                 size - (auparse->offset),
344                 GST_PAD_CAPS (auparse->srcpad), &newbuf)) != GST_FLOW_OK) {
345       gst_buffer_unref (buf);
346       g_object_unref (auparse);
347       return ret;
348     }
349     ret = GST_FLOW_OK;
350
351
352     memcpy (GST_BUFFER_DATA (newbuf), data + (auparse->offset),
353         size - (auparse->offset));
354     GST_BUFFER_SIZE (newbuf) = size - (auparse->offset);
355
356     GstEvent *event;
357
358     event = NULL;
359
360     event = gst_event_new_newsegment (FALSE, 1.0, GST_FORMAT_DEFAULT,
361         0, GST_CLOCK_TIME_NONE, 0);
362
363
364     gst_pad_push_event (auparse->srcpad, event);
365
366     gst_buffer_unref (buf);
367     g_object_unref (auparse);
368     return gst_pad_push (auparse->srcpad, newbuf);
369
370   }
371
372   g_object_unref (auparse);
373   return gst_pad_push (auparse->srcpad, buf);
374
375 }
376
377 static GstStateChangeReturn
378 gst_auparse_change_state (GstElement * element, GstStateChange transition)
379 {
380   GstAuParse *auparse = GST_AUPARSE (element);
381   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
382
383   if (parent_class->change_state)
384     ret = parent_class->change_state (element, transition);
385
386   switch (transition) {
387     case GST_STATE_CHANGE_READY_TO_NULL:
388       auparse->offset = 0;
389       auparse->size = 0;
390       auparse->encoding = 0;
391       auparse->frequency = 0;
392       auparse->channels = 0;
393     default:
394       break;
395   }
396
397   return ret;
398 }
399
400 static gboolean
401 plugin_init (GstPlugin * plugin)
402 {
403   if (!gst_element_register (plugin, "auparse", GST_RANK_SECONDARY,
404           GST_TYPE_AUPARSE)) {
405     return FALSE;
406   }
407
408   return TRUE;
409 }
410
411 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
412     GST_VERSION_MINOR,
413     "auparse",
414     "parses au streams", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,
415     GST_PACKAGE_ORIGIN)