ext/audiofile/gstafparse.c: change class to Codec/Demuxer/Audio
[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 Merali <zaheer@grid9.net
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 "; "       /* 24-bit PCM is barely supported by gstreamer actually */
53         GST_AUDIO_FLOAT_PAD_TEMPLATE_CAPS "; "  /* 64-bit float is barely supported by gstreamer actually */
54         "audio/x-alaw, " "rate = (int) [ 8000, 192000 ], " "channels = (int) [ 1, 2 ]; " "audio/x-mulaw, " "rate = (int) [ 8000, 192000 ], " "channels = (int) [ 1, 2 ]"        /*"; "
55                                                                                                                                                                                    "audio/x-adpcm, "
56                                                                                                                                                                                    "layout = (string) { g721, g722, g723_3, g723_5 }" */ )
57     /* Nothing to decode those ADPCM streams for now */
58     );
59
60 /* AuParse signals and args */
61 enum
62 {
63   /* FILL ME */
64   LAST_SIGNAL
65 };
66
67 enum
68 {
69   ARG_0,
70   /* FILL ME */
71 };
72
73 static void gst_auparse_base_init (gpointer g_class);
74 static void gst_auparse_class_init (GstAuParseClass * klass);
75 static void gst_auparse_init (GstAuParse * auparse);
76
77 static void gst_auparse_chain (GstPad * pad, GstData * _data);
78
79 static GstElementStateReturn gst_auparse_change_state (GstElement * element);
80
81 static GstElementClass *parent_class = NULL;
82
83 /*static guint gst_auparse_signals[LAST_SIGNAL] = { 0 }; */
84
85 GType
86 gst_auparse_get_type (void)
87 {
88   static GType auparse_type = 0;
89
90   if (!auparse_type) {
91     static const GTypeInfo auparse_info = {
92       sizeof (GstAuParseClass),
93       gst_auparse_base_init,
94       NULL,
95       (GClassInitFunc) gst_auparse_class_init,
96       NULL,
97       NULL,
98       sizeof (GstAuParse),
99       0,
100       (GInstanceInitFunc) gst_auparse_init,
101     };
102
103     auparse_type =
104         g_type_register_static (GST_TYPE_ELEMENT, "GstAuParse", &auparse_info,
105         0);
106   }
107   return auparse_type;
108 }
109
110 static void
111 gst_auparse_base_init (gpointer g_class)
112 {
113   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
114
115   gst_element_class_add_pad_template (element_class,
116       gst_static_pad_template_get (&gst_auparse_sink_template));
117   gst_element_class_add_pad_template (element_class,
118       gst_static_pad_template_get (&gst_auparse_src_template));
119   gst_element_class_set_details (element_class, &gst_auparse_details);
120
121 }
122
123 static void
124 gst_auparse_class_init (GstAuParseClass * klass)
125 {
126   GstElementClass *gstelement_class;
127
128   gstelement_class = (GstElementClass *) klass;
129
130   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
131
132   gstelement_class->change_state = gst_auparse_change_state;
133 }
134
135 static void
136 gst_auparse_init (GstAuParse * auparse)
137 {
138   auparse->sinkpad =
139       gst_pad_new_from_template (gst_static_pad_template_get
140       (&gst_auparse_sink_template), "sink");
141   gst_element_add_pad (GST_ELEMENT (auparse), auparse->sinkpad);
142   gst_pad_set_chain_function (auparse->sinkpad, gst_auparse_chain);
143
144   auparse->srcpad = NULL;
145 #if 0                           /* FIXME: spider */
146   gst_pad_new_from_template (gst_static_pad_template_get
147       (&gst_auparse_src_template), "src");
148   gst_element_add_pad (GST_ELEMENT (auparse), auparse->srcpad);
149   gst_pad_use_explicit_caps (auparse->srcpad);
150 #endif
151
152   auparse->offset = 0;
153   auparse->size = 0;
154   auparse->encoding = 0;
155   auparse->frequency = 0;
156   auparse->channels = 0;
157 }
158
159 static void
160 gst_auparse_chain (GstPad * pad, GstData * _data)
161 {
162   GstBuffer *buf = GST_BUFFER (_data);
163   GstAuParse *auparse;
164   gchar *data;
165   glong size;
166   GstCaps *tempcaps;
167   gint law = 0, depth, ieee = 0;
168
169   g_return_if_fail (pad != NULL);
170   g_return_if_fail (GST_IS_PAD (pad));
171   g_return_if_fail (buf != NULL);
172
173   auparse = GST_AUPARSE (gst_pad_get_parent (pad));
174
175   GST_DEBUG ("gst_auparse_chain: got buffer in '%s'",
176       gst_element_get_name (GST_ELEMENT (auparse)));
177
178   data = GST_BUFFER_DATA (buf);
179   size = GST_BUFFER_SIZE (buf);
180
181   /* if we haven't seen any data yet... */
182   if (auparse->size == 0) {
183     GstBuffer *newbuf;
184     guint32 *head = (guint32 *) data;
185
186     /* normal format is big endian (au is a Sparc format) */
187     if (GUINT32_FROM_BE (*head) == 0x2e736e64) {        /* ".snd" */
188       head++;
189       auparse->le = 0;
190       auparse->offset = GUINT32_FROM_BE (*head);
191       head++;
192       auparse->size = GUINT32_FROM_BE (*head);  /* Do not trust size, could be set to -1 : unknown */
193       head++;
194       auparse->encoding = GUINT32_FROM_BE (*head);
195       head++;
196       auparse->frequency = GUINT32_FROM_BE (*head);
197       head++;
198       auparse->channels = GUINT32_FROM_BE (*head);
199       head++;
200
201       /* and of course, someone had to invent a little endian
202        * version.  Used by DEC systems. */
203     } else if (GUINT32_FROM_LE (*head) == 0x0064732E) { /* other source say it is "dns." */
204       head++;
205       auparse->le = 1;
206       auparse->offset = GUINT32_FROM_LE (*head);
207       head++;
208       auparse->size = GUINT32_FROM_LE (*head);  /* Do not trust size, could be set to -1 : unknown */
209       head++;
210       auparse->encoding = GUINT32_FROM_LE (*head);
211       head++;
212       auparse->frequency = GUINT32_FROM_LE (*head);
213       head++;
214       auparse->channels = GUINT32_FROM_LE (*head);
215       head++;
216
217     } else {
218       g_warning ("help, dunno what I'm looking at!\n");
219       gst_buffer_unref (buf);
220       return;
221     }
222
223     GST_DEBUG
224         ("offset %ld, size %ld, encoding %ld, frequency %ld, channels %ld",
225         auparse->offset, auparse->size, auparse->encoding, auparse->frequency,
226         auparse->channels);
227
228 /*
229 Docs :
230         http://www.opengroup.org/public/pubs/external/auformat.html
231         http://astronomy.swin.edu.au/~pbourke/dataformats/au/
232         Solaris headers : /usr/include/audio/au.h
233         libsndfile : src/au.c
234 Samples :
235         http://www.tsp.ece.mcgill.ca/MMSP/Documents/AudioFormats/AU/Samples.html
236 */
237
238     switch (auparse->encoding) {
239
240       case 1:                  /* 8-bit ISDN mu-law G.711 */
241         law = 1;
242         depth = 8;
243         break;
244       case 27:                 /* 8-bit ISDN  A-law G.711 */
245         law = 2;
246         depth = 8;
247         break;
248
249       case 2:                  /*  8-bit linear PCM */
250         depth = 8;
251         break;
252       case 3:                  /* 16-bit linear PCM */
253         depth = 16;
254         break;
255       case 4:                  /* 24-bit linear PCM */
256         depth = 24;
257         break;
258       case 5:                  /* 32-bit linear PCM */
259         depth = 32;
260         break;
261
262       case 6:                  /* 32-bit IEEE floating point */
263         ieee = 1;
264         depth = 32;
265         break;
266       case 7:                  /* 64-bit IEEE floating point */
267         ieee = 1;
268         depth = 64;
269         break;
270
271       case 8:                  /* Fragmented sample data */
272       case 9:                  /* AU_ENCODING_NESTED */
273
274       case 10:                 /* DSP program */
275       case 11:                 /* DSP  8-bit fixed point */
276       case 12:                 /* DSP 16-bit fixed point */
277       case 13:                 /* DSP 24-bit fixed point */
278       case 14:                 /* DSP 32-bit fixed point */
279
280       case 16:                 /* AU_ENCODING_DISPLAY : non-audio display data */
281       case 17:                 /* AU_ENCODING_MULAW_SQUELCH */
282
283       case 18:                 /* 16-bit linear with emphasis */
284       case 19:                 /* 16-bit linear compressed (NeXT) */
285       case 20:                 /* 16-bit linear with emphasis and compression */
286
287       case 21:                 /* Music kit DSP commands */
288       case 22:                 /* Music kit DSP commands samples */
289
290       case 23:                 /* 4-bit CCITT G.721   ADPCM 32kbps -> modplug/libsndfile (compressed 8-bit mu-law) */
291       case 24:                 /* 8-bit CCITT G.722   ADPCM        -> rtp */
292       case 25:                 /* 3-bit CCITT G.723.3 ADPCM 24kbps -> rtp/xine/modplug/libsndfile */
293       case 26:                 /* 5-bit CCITT G.723.5 ADPCM 40kbps -> rtp/xine/modplug/libsndfile */
294
295       default:
296         GST_ELEMENT_ERROR (auparse, STREAM, FORMAT, (NULL), (NULL));
297         gst_buffer_unref (buf);
298         return;
299     }
300
301     auparse->srcpad =
302         gst_pad_new_from_template (gst_static_pad_template_get
303         (&gst_auparse_src_template), "src");
304     gst_pad_use_explicit_caps (auparse->srcpad);
305
306     if (law) {
307       tempcaps =
308           gst_caps_new_simple ((law == 1) ? "audio/x-mulaw" : "audio/x-alaw",
309           "rate", G_TYPE_INT, auparse->frequency, "channels", G_TYPE_INT,
310           auparse->channels, NULL);
311     } else if (ieee) {
312       tempcaps = gst_caps_new_simple ("audio/x-raw-float",
313           "width", G_TYPE_INT, depth,
314           "endianness", G_TYPE_INT,
315           auparse->le ? G_LITTLE_ENDIAN : G_BIG_ENDIAN, NULL);
316 /*
317     } else if (layout) {
318       tempcaps = gst_caps_new_simple ("audio/x-adpcm",
319           "layout", G_TYPE_STRING, layout, NULL);
320 */
321     } else {
322       tempcaps = gst_caps_new_simple ("audio/x-raw-int",
323           "endianness", G_TYPE_INT,
324           auparse->le ? G_LITTLE_ENDIAN : G_BIG_ENDIAN, "rate", G_TYPE_INT,
325           auparse->frequency, "channels", G_TYPE_INT, auparse->channels,
326           "depth", G_TYPE_INT, depth, "width", G_TYPE_INT, depth, "signed",
327           G_TYPE_BOOLEAN, TRUE, NULL);
328     }
329
330     if (!gst_pad_set_explicit_caps (auparse->srcpad, tempcaps)) {
331       gst_buffer_unref (buf);
332       gst_object_unref (GST_OBJECT (auparse->srcpad));
333       auparse->srcpad = NULL;
334       return;
335     }
336
337     gst_element_add_pad (GST_ELEMENT (auparse), auparse->srcpad);
338
339     newbuf = gst_buffer_new ();
340     GST_BUFFER_DATA (newbuf) = (gpointer) malloc (size - (auparse->offset));
341     memcpy (GST_BUFFER_DATA (newbuf), data + (auparse->offset),
342         size - (auparse->offset));
343     GST_BUFFER_SIZE (newbuf) = size - (auparse->offset);
344
345     gst_buffer_unref (buf);
346
347     gst_pad_push (auparse->srcpad, GST_DATA (newbuf));
348     return;
349   }
350
351   gst_pad_push (auparse->srcpad, GST_DATA (buf));
352 }
353
354 static GstElementStateReturn
355 gst_auparse_change_state (GstElement * element)
356 {
357   GstAuParse *auparse = GST_AUPARSE (element);
358
359   switch (GST_STATE_TRANSITION (element)) {
360     case GST_STATE_PAUSED_TO_READY:
361       if (auparse->srcpad) {
362         gst_element_remove_pad (element, auparse->srcpad);
363         auparse->srcpad = NULL;
364       }
365       break;
366     default:
367       break;
368   }
369
370   if (parent_class->change_state)
371     return parent_class->change_state (element);
372
373   return GST_STATE_SUCCESS;
374 }
375
376 static gboolean
377 plugin_init (GstPlugin * plugin)
378 {
379   if (!gst_element_register (plugin, "auparse", GST_RANK_SECONDARY,
380           GST_TYPE_AUPARSE)) {
381     return FALSE;
382   }
383
384   return TRUE;
385 }
386
387 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
388     GST_VERSION_MINOR,
389     "auparse",
390     "parses au streams", plugin_init, VERSION, "GPL", GST_PACKAGE, GST_ORIGIN)