2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
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.
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.
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.
19 /* Element-Checklist-Version: 5 */
21 /* 2001/04/03 - Updated parseau to use caps nego
22 * Zaheer Abbas Merali <zaheerabbas at merali dot org>
31 #include "gstauparse.h"
32 #include <gst/audio/audio.h>
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>");
41 static GstStaticPadTemplate gst_auparse_sink_template =
42 GST_STATIC_PAD_TEMPLATE ("sink",
45 GST_STATIC_CAPS ("audio/x-au")
48 static GstStaticPadTemplate gst_auparse_src_template =
49 GST_STATIC_PAD_TEMPLATE ("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 */
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]" "; "
62 "rate = (int) [ 8000, 192000 ], "
63 "channels = (int) [ 1, 2 ]" "; "
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 }")
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);
80 static GstFlowReturn gst_auparse_chain (GstPad * pad, GstBuffer * buf);
82 static GstStateChangeReturn gst_auparse_change_state (GstElement * element,
83 GstStateChange transition);
85 static GstElementClass *parent_class = NULL;
88 /*static guint gst_auparse_signals[LAST_SIGNAL] = { 0 }; */
91 gst_auparse_get_type (void)
93 static GType auparse_type = 0;
96 static const GTypeInfo auparse_info = {
97 sizeof (GstAuParseClass),
98 gst_auparse_base_init,
100 (GClassInitFunc) gst_auparse_class_init,
105 (GInstanceInitFunc) gst_auparse_init,
109 g_type_register_static (GST_TYPE_ELEMENT, "GstAuParse", &auparse_info,
116 gst_auparse_base_init (gpointer g_class)
118 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
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);
129 gst_auparse_class_init (GstAuParseClass * klass)
131 GstElementClass *gstelement_class;
133 gstelement_class = (GstElementClass *) klass;
135 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
137 gstelement_class->change_state = gst_auparse_change_state;
141 gst_auparse_init (GstAuParse * auparse)
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);
149 auparse->srcpad = NULL;
150 #if 0 /* FIXME: spider */
151 gst_pad_new_from_template (gst_static_pad_template_get
152 (&gst_auparse_src_template), "src");
153 gst_element_add_pad (GST_ELEMENT (auparse), auparse->srcpad);
154 gst_pad_use_fixed_caps (auparse->srcpad);
159 auparse->encoding = 0;
160 auparse->frequency = 0;
161 auparse->channels = 0;
165 gst_auparse_chain (GstPad * pad, GstBuffer * buf)
172 gint law = 0, depth = 0, ieee = 0;
177 g_return_val_if_fail (pad != NULL, GST_FLOW_ERROR);
178 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
179 g_return_val_if_fail (buf != NULL, GST_FLOW_ERROR);
181 auparse = GST_AUPARSE (gst_pad_get_parent (pad));
183 GST_DEBUG ("gst_auparse_chain: got buffer in '%s'",
184 gst_element_get_name (GST_ELEMENT (auparse)));
186 data = GST_BUFFER_DATA (buf);
187 size = GST_BUFFER_SIZE (buf);
189 /* if we haven't seen any data yet... */
190 if (auparse->size == 0) {
192 guint32 *head = (guint32 *) data;
194 /* normal format is big endian (au is a Sparc format) */
195 if (GST_READ_UINT32_BE (head) == 0x2e736e64) { /* ".snd" */
198 auparse->offset = GST_READ_UINT32_BE (head);
200 /* Do not trust size, could be set to -1 : unknown */
201 auparse->size = GST_READ_UINT32_BE (head);
203 auparse->encoding = GST_READ_UINT32_BE (head);
205 auparse->frequency = GST_READ_UINT32_BE (head);
207 auparse->channels = GST_READ_UINT32_BE (head);
210 /* and of course, someone had to invent a little endian
211 * version. Used by DEC systems. */
212 } else if (GST_READ_UINT32_LE (head) == 0x0064732E) { /* other source say it is "dns." */
215 auparse->offset = GST_READ_UINT32_LE (head);
217 /* Do not trust size, could be set to -1 : unknown */
218 auparse->size = GST_READ_UINT32_LE (head);
220 auparse->encoding = GST_READ_UINT32_LE (head);
222 auparse->frequency = GST_READ_UINT32_LE (head);
224 auparse->channels = GST_READ_UINT32_LE (head);
228 GST_ELEMENT_ERROR (auparse, STREAM, WRONG_TYPE, (NULL), (NULL));
229 return GST_FLOW_ERROR;
233 ("offset %ld, size %ld, encoding %ld, frequency %ld, channels %ld\n",
234 auparse->offset, auparse->size, auparse->encoding, auparse->frequency,
239 http://www.opengroup.org/public/pubs/external/auformat.html
240 http://astronomy.swin.edu.au/~pbourke/dataformats/au/
241 Solaris headers : /usr/include/audio/au.h
242 libsndfile : src/au.c
244 http://www.tsp.ece.mcgill.ca/MMSP/Documents/AudioFormats/AU/Samples.html
247 switch (auparse->encoding) {
249 case 1: /* 8-bit ISDN mu-law G.711 */
253 case 27: /* 8-bit ISDN A-law G.711 */
258 case 2: /* 8-bit linear PCM */
261 case 3: /* 16-bit linear PCM */
264 case 4: /* 24-bit linear PCM */
267 case 5: /* 32-bit linear PCM */
271 case 6: /* 32-bit IEEE floating point */
275 case 7: /* 64-bit IEEE floating point */
280 case 23: /* 4-bit CCITT G.721 ADPCM 32kbps -> modplug/libsndfile (compressed 8-bit mu-law) */
281 strcpy (layout, "g721");
283 case 24: /* 8-bit CCITT G.722 ADPCM -> rtp */
284 strcpy (layout, "g722");
286 case 25: /* 3-bit CCITT G.723.3 ADPCM 24kbps -> rtp/xine/modplug/libsndfile */
287 strcpy (layout, "g723_3");
289 case 26: /* 5-bit CCITT G.723.5 ADPCM 40kbps -> rtp/xine/modplug/libsndfile */
290 strcpy (layout, "g723_5");
293 case 8: /* Fragmented sample data */
294 case 9: /* AU_ENCODING_NESTED */
296 case 10: /* DSP program */
297 case 11: /* DSP 8-bit fixed point */
298 case 12: /* DSP 16-bit fixed point */
299 case 13: /* DSP 24-bit fixed point */
300 case 14: /* DSP 32-bit fixed point */
302 case 16: /* AU_ENCODING_DISPLAY : non-audio display data */
303 case 17: /* AU_ENCODING_MULAW_SQUELCH */
305 case 18: /* 16-bit linear with emphasis */
306 case 19: /* 16-bit linear compressed (NeXT) */
307 case 20: /* 16-bit linear with emphasis and compression */
309 case 21: /* Music kit DSP commands */
310 case 22: /* Music kit DSP commands samples */
313 GST_ELEMENT_ERROR (auparse, STREAM, FORMAT, (NULL), (NULL));
314 return GST_FLOW_ERROR;
318 gst_pad_new_from_template (gst_static_pad_template_get
319 (&gst_auparse_src_template), "src");
321 g_return_val_if_fail (auparse->srcpad != NULL, GST_FLOW_ERROR);
324 gst_pad_use_fixed_caps (auparse->srcpad);
328 gst_caps_new_simple ((law == 1) ? "audio/x-mulaw" : "audio/x-alaw",
329 "rate", G_TYPE_INT, auparse->frequency,
330 "channels", G_TYPE_INT, auparse->channels, NULL);
332 tempcaps = gst_caps_new_simple ("audio/x-raw-float",
333 "rate", G_TYPE_INT, auparse->frequency,
334 "channels", G_TYPE_INT, auparse->channels,
335 "endianness", G_TYPE_INT,
336 auparse->le ? G_LITTLE_ENDIAN : G_BIG_ENDIAN, "width", G_TYPE_INT,
337 depth, "buffer-frames", G_TYPE_INT, 0, NULL);
338 } else if (layout[0]) {
339 tempcaps = gst_caps_new_simple ("audio/x-adpcm",
340 "layout", G_TYPE_STRING, layout, NULL);
342 tempcaps = gst_caps_new_simple ("audio/x-raw-int",
343 "rate", G_TYPE_INT, auparse->frequency,
344 "channels", G_TYPE_INT, auparse->channels,
345 "endianness", G_TYPE_INT,
346 auparse->le ? G_LITTLE_ENDIAN : G_BIG_ENDIAN, "depth", G_TYPE_INT,
347 depth, "width", G_TYPE_INT, depth, "signed", G_TYPE_BOOLEAN, TRUE,
351 gst_pad_use_fixed_caps (auparse->srcpad);
352 gst_pad_set_active (auparse->srcpad, TRUE);
353 gst_pad_set_caps (auparse->srcpad, tempcaps);
354 gst_element_add_pad (GST_ELEMENT (auparse), auparse->srcpad);
356 if ((ret = gst_pad_alloc_buffer (auparse->srcpad, GST_BUFFER_OFFSET_NONE,
357 size - (auparse->offset),
358 GST_PAD_CAPS (auparse->srcpad), &newbuf)) != GST_FLOW_OK) {
359 printf ("failed gst_pad_alloc_buffer\n");
365 memcpy (GST_BUFFER_DATA (newbuf), data + (auparse->offset),
366 size - (auparse->offset));
367 GST_BUFFER_SIZE (newbuf) = size - (auparse->offset);
373 event = gst_event_new_newsegment (FALSE, 1.0, GST_FORMAT_DEFAULT,
374 0, GST_CLOCK_TIME_NONE, 0);
377 gst_pad_push_event (auparse->srcpad, event);
379 return gst_pad_push (auparse->srcpad, newbuf);
383 return gst_pad_push (auparse->srcpad, buf);
387 static GstStateChangeReturn
388 gst_auparse_change_state (GstElement * element, GstStateChange transition)
390 if (parent_class->change_state)
391 return parent_class->change_state (element, transition);
393 return GST_STATE_CHANGE_SUCCESS;
397 plugin_init (GstPlugin * plugin)
399 if (!gst_element_register (plugin, "auparse", GST_RANK_SECONDARY,
407 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
410 "parses au streams", plugin_init, VERSION, "LGPL", GST_PACKAGE, GST_ORIGIN)