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>
32 #include "gstauparse.h"
33 #include <gst/audio/audio.h>
35 /* elementfactory information */
36 static GstElementDetails gst_au_parse_details =
37 GST_ELEMENT_DETAILS (".au parser",
38 "Codec/Demuxer/Audio",
39 "Parse an .au file into raw audio",
40 "Erik Walthinsen <omega@cse.ogi.edu>");
42 static GstStaticPadTemplate gst_au_parse_sink_template =
43 GST_STATIC_PAD_TEMPLATE ("sink",
46 GST_STATIC_CAPS ("audio/x-au")
49 #define GST_AU_PARSE_ALAW_PAD_TEMPLATE_CAPS \
51 "rate = (int) [ 8000, 192000 ], " \
52 "channels = (int) [ 1, 2 ]"
54 #define GST_AU_PARSE_MULAW_PAD_TEMPLATE_CAPS \
56 "rate = (int) [ 8000, 192000 ], " \
57 "channels = (int) [ 1, 2 ]"
59 /* Nothing to decode those ADPCM streams for now */
60 #define GST_AU_PARSE_ADPCM_PAD_TEMPLATE_CAPS \
62 "layout = (string) { g721, g722, g723_3, g723_5 }"
64 static GstStaticPadTemplate gst_au_parse_src_template =
65 GST_STATIC_PAD_TEMPLATE ("src",
68 GST_STATIC_CAPS (GST_AUDIO_INT_PAD_TEMPLATE_CAPS "; "
69 GST_AUDIO_FLOAT_PAD_TEMPLATE_CAPS ";"
70 GST_AU_PARSE_ALAW_PAD_TEMPLATE_CAPS ";"
71 GST_AU_PARSE_MULAW_PAD_TEMPLATE_CAPS ";"
72 GST_AU_PARSE_ADPCM_PAD_TEMPLATE_CAPS));
75 static void gst_au_parse_dispose (GObject * object);
76 static GstFlowReturn gst_au_parse_chain (GstPad * pad, GstBuffer * buf);
77 static GstStateChangeReturn gst_au_parse_change_state (GstElement * element,
78 GstStateChange transition);
80 GST_BOILERPLATE (GstAuParse, gst_au_parse, GstElement, GST_TYPE_ELEMENT)
82 static void gst_au_parse_base_init (gpointer g_class)
84 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
86 gst_element_class_add_pad_template (element_class,
87 gst_static_pad_template_get (&gst_au_parse_sink_template));
88 gst_element_class_add_pad_template (element_class,
89 gst_static_pad_template_get (&gst_au_parse_src_template));
90 gst_element_class_set_details (element_class, &gst_au_parse_details);
95 gst_au_parse_class_init (GstAuParseClass * klass)
97 GObjectClass *gobject_class;
98 GstElementClass *gstelement_class;
100 gobject_class = (GObjectClass *) klass;
101 gstelement_class = (GstElementClass *) klass;
103 parent_class = g_type_class_peek_parent (klass);
105 gobject_class->dispose = gst_au_parse_dispose;
107 gstelement_class->change_state =
108 GST_DEBUG_FUNCPTR (gst_au_parse_change_state);
112 gst_au_parse_init (GstAuParse * auparse, GstAuParseClass * klass)
115 gst_pad_new_from_static_template (&gst_au_parse_sink_template, "sink");
116 gst_element_add_pad (GST_ELEMENT (auparse), auparse->sinkpad);
117 gst_pad_set_chain_function (auparse->sinkpad, gst_au_parse_chain);
120 gst_pad_new_from_static_template (&gst_au_parse_src_template, "src");
121 gst_pad_use_fixed_caps (auparse->srcpad);
122 gst_element_add_pad (GST_ELEMENT (auparse), auparse->srcpad);
125 auparse->buffer_offset = 0;
126 auparse->adapter = gst_adapter_new ();
128 auparse->encoding = 0;
129 auparse->frequency = 0;
130 auparse->channels = 0;
134 gst_au_parse_dispose (GObject * object)
136 GstAuParse *au = GST_AU_PARSE (object);
138 if (au->adapter != NULL) {
139 g_object_unref (au->adapter);
142 G_OBJECT_CLASS (parent_class)->dispose (object);
146 gst_au_parse_chain (GstPad * pad, GstBuffer * buf)
153 gint law = 0, depth = 0, ieee = 0;
160 auparse = GST_AU_PARSE (gst_pad_get_parent (pad));
162 data = GST_BUFFER_DATA (buf);
163 size = GST_BUFFER_SIZE (buf);
165 GST_DEBUG_OBJECT (auparse, "got buffer of size %ld", size);
167 /* if we haven't seen any data yet... */
168 if (auparse->size == 0) {
169 guint32 *head = (guint32 *) data;
171 /* normal format is big endian (au is a Sparc format) */
172 if (GST_READ_UINT32_BE (head) == 0x2e736e64) { /* ".snd" */
175 auparse->offset = GST_READ_UINT32_BE (head);
177 /* Do not trust size, could be set to -1 : unknown */
178 auparse->size = GST_READ_UINT32_BE (head);
180 auparse->encoding = GST_READ_UINT32_BE (head);
182 auparse->frequency = GST_READ_UINT32_BE (head);
184 auparse->channels = GST_READ_UINT32_BE (head);
187 /* and of course, someone had to invent a little endian
188 * version. Used by DEC systems. */
189 } else if (GST_READ_UINT32_LE (head) == 0x0064732E) { /* other source say it is "dns." */
192 auparse->offset = GST_READ_UINT32_LE (head);
194 /* Do not trust size, could be set to -1 : unknown */
195 auparse->size = GST_READ_UINT32_LE (head);
197 auparse->encoding = GST_READ_UINT32_LE (head);
199 auparse->frequency = GST_READ_UINT32_LE (head);
201 auparse->channels = GST_READ_UINT32_LE (head);
205 GST_ELEMENT_ERROR (auparse, STREAM, WRONG_TYPE, (NULL), (NULL));
206 gst_buffer_unref (buf);
207 gst_object_unref (auparse);
208 return GST_FLOW_ERROR;
212 ("offset %ld, size %ld, encoding %ld, frequency %ld, channels %ld\n",
213 auparse->offset, auparse->size, auparse->encoding, auparse->frequency,
218 http://www.opengroup.org/public/pubs/external/auformat.html
219 http://astronomy.swin.edu.au/~pbourke/dataformats/au/
220 Solaris headers : /usr/include/audio/au.h
221 libsndfile : src/au.c
223 http://www.tsp.ece.mcgill.ca/MMSP/Documents/AudioFormats/AU/Samples.html
226 switch (auparse->encoding) {
228 case 1: /* 8-bit ISDN mu-law G.711 */
232 case 27: /* 8-bit ISDN A-law G.711 */
237 case 2: /* 8-bit linear PCM */
240 case 3: /* 16-bit linear PCM */
243 case 4: /* 24-bit linear PCM */
246 case 5: /* 32-bit linear PCM */
250 case 6: /* 32-bit IEEE floating point */
254 case 7: /* 64-bit IEEE floating point */
259 case 23: /* 4-bit CCITT G.721 ADPCM 32kbps -> modplug/libsndfile (compressed 8-bit mu-law) */
260 strcpy (layout, "g721");
262 case 24: /* 8-bit CCITT G.722 ADPCM -> rtp */
263 strcpy (layout, "g722");
265 case 25: /* 3-bit CCITT G.723.3 ADPCM 24kbps -> rtp/xine/modplug/libsndfile */
266 strcpy (layout, "g723_3");
268 case 26: /* 5-bit CCITT G.723.5 ADPCM 40kbps -> rtp/xine/modplug/libsndfile */
269 strcpy (layout, "g723_5");
272 case 8: /* Fragmented sample data */
273 case 9: /* AU_ENCODING_NESTED */
275 case 10: /* DSP program */
276 case 11: /* DSP 8-bit fixed point */
277 case 12: /* DSP 16-bit fixed point */
278 case 13: /* DSP 24-bit fixed point */
279 case 14: /* DSP 32-bit fixed point */
281 case 16: /* AU_ENCODING_DISPLAY : non-audio display data */
282 case 17: /* AU_ENCODING_MULAW_SQUELCH */
284 case 18: /* 16-bit linear with emphasis */
285 case 19: /* 16-bit linear compressed (NeXT) */
286 case 20: /* 16-bit linear with emphasis and compression */
288 case 21: /* Music kit DSP commands */
289 case 22: /* Music kit DSP commands samples */
292 GST_ELEMENT_ERROR (auparse, STREAM, FORMAT, (NULL), (NULL));
293 gst_buffer_unref (buf);
294 gst_object_unref (auparse);
295 return GST_FLOW_ERROR;
300 gst_caps_new_simple ((law == 1) ? "audio/x-mulaw" : "audio/x-alaw",
301 "rate", G_TYPE_INT, auparse->frequency,
302 "channels", G_TYPE_INT, auparse->channels, NULL);
303 auparse->sample_size = auparse->channels;
305 tempcaps = gst_caps_new_simple ("audio/x-raw-float",
306 "rate", G_TYPE_INT, auparse->frequency,
307 "channels", G_TYPE_INT, auparse->channels,
308 "endianness", G_TYPE_INT,
309 auparse->le ? G_LITTLE_ENDIAN : G_BIG_ENDIAN,
310 "width", G_TYPE_INT, depth, NULL);
311 auparse->sample_size = auparse->channels * depth / 8;
312 } else if (layout[0]) {
313 tempcaps = gst_caps_new_simple ("audio/x-adpcm",
314 "layout", G_TYPE_STRING, layout, NULL);
315 auparse->sample_size = 0;
317 tempcaps = gst_caps_new_simple ("audio/x-raw-int",
318 "rate", G_TYPE_INT, auparse->frequency,
319 "channels", G_TYPE_INT, auparse->channels,
320 "endianness", G_TYPE_INT,
321 auparse->le ? G_LITTLE_ENDIAN : G_BIG_ENDIAN, "depth", G_TYPE_INT,
322 depth, "width", G_TYPE_INT, depth, "signed", G_TYPE_BOOLEAN, TRUE,
324 auparse->sample_size = auparse->channels * depth / 8;
327 gst_pad_use_fixed_caps (auparse->srcpad);
328 gst_pad_set_caps (auparse->srcpad, tempcaps);
329 gst_pad_set_active (auparse->srcpad, TRUE);
331 event = gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_DEFAULT,
332 0, GST_CLOCK_TIME_NONE, 0);
334 gst_pad_push_event (auparse->srcpad, event);
336 databuf = gst_buffer_create_sub (buf, auparse->offset,
337 size - auparse->offset);
339 gst_buffer_unref (buf);
346 if (auparse->sample_size) {
349 gst_adapter_push (auparse->adapter, databuf);
351 /* Ensure we push a buffer that's a multiple of the frame size downstream */
352 avail = gst_adapter_available (auparse->adapter);
353 avail -= avail % auparse->sample_size;
356 const guint8 *data = gst_adapter_peek (auparse->adapter, avail);
360 gst_pad_alloc_buffer_and_set_caps (auparse->srcpad,
361 auparse->buffer_offset, avail, GST_PAD_CAPS (auparse->srcpad),
362 &newbuf)) == GST_FLOW_OK) {
364 memcpy (GST_BUFFER_DATA (newbuf), data, avail);
365 gst_adapter_flush (auparse->adapter, avail);
367 auparse->buffer_offset += avail;
369 ret = gst_pad_push (auparse->srcpad, newbuf);
375 /* It's something non-trivial (such as ADPCM), we don't understand it, so
376 * just push downstream and assume this will know what to do with it */
377 gst_buffer_set_caps (databuf, GST_PAD_CAPS (auparse->srcpad));
378 ret = gst_pad_push (auparse->srcpad, databuf);
381 gst_object_unref (auparse);
386 static GstStateChangeReturn
387 gst_au_parse_change_state (GstElement * element, GstStateChange transition)
389 GstAuParse *auparse = GST_AU_PARSE (element);
390 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
392 ret = parent_class->change_state (element, transition);
393 if (ret == GST_STATE_CHANGE_FAILURE)
396 switch (transition) {
397 case GST_STATE_CHANGE_READY_TO_NULL:
398 gst_adapter_clear (auparse->adapter);
399 auparse->buffer_offset = 0;
402 auparse->encoding = 0;
403 auparse->frequency = 0;
404 auparse->channels = 0;
413 plugin_init (GstPlugin * plugin)
415 if (!gst_element_register (plugin, "auparse", GST_RANK_SECONDARY,
416 GST_TYPE_AU_PARSE)) {
423 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
426 "parses au streams", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,