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 audio demuxer",
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_pad_set_chain_function (auparse->sinkpad, gst_au_parse_chain);
117 gst_element_add_pad (GST_ELEMENT (auparse), auparse->sinkpad);
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;
159 auparse = GST_AU_PARSE (gst_pad_get_parent (pad));
161 data = GST_BUFFER_DATA (buf);
162 size = GST_BUFFER_SIZE (buf);
164 GST_DEBUG_OBJECT (auparse, "got buffer of size %ld", size);
166 /* if we haven't seen any data yet... */
167 if (auparse->size == 0) {
168 guint32 *head = (guint32 *) data;
170 /* FIXME, check if we have enough data (with adapter?) instead of
172 /* normal format is big endian (au is a Sparc format) */
173 if (GST_READ_UINT32_BE (head) == 0x2e736e64) { /* ".snd" */
176 auparse->offset = GST_READ_UINT32_BE (head);
178 /* Do not trust size, could be set to -1 : unknown */
179 auparse->size = GST_READ_UINT32_BE (head);
181 auparse->encoding = GST_READ_UINT32_BE (head);
183 auparse->frequency = GST_READ_UINT32_BE (head);
185 auparse->channels = GST_READ_UINT32_BE (head);
188 /* and of course, someone had to invent a little endian
189 * version. Used by DEC systems. */
190 } else if (GST_READ_UINT32_LE (head) == 0x0064732E) { /* other source say it is "dns." */
193 auparse->offset = GST_READ_UINT32_LE (head);
195 /* Do not trust size, could be set to -1 : unknown */
196 auparse->size = GST_READ_UINT32_LE (head);
198 auparse->encoding = GST_READ_UINT32_LE (head);
200 auparse->frequency = GST_READ_UINT32_LE (head);
202 auparse->channels = GST_READ_UINT32_LE (head);
210 ("offset %ld, size %ld, encoding %ld, frequency %ld, channels %ld\n",
211 auparse->offset, auparse->size, auparse->encoding, auparse->frequency,
216 http://www.opengroup.org/public/pubs/external/auformat.html
217 http://astronomy.swin.edu.au/~pbourke/dataformats/au/
218 Solaris headers : /usr/include/audio/au.h
219 libsndfile : src/au.c
221 http://www.tsp.ece.mcgill.ca/MMSP/Documents/AudioFormats/AU/Samples.html
224 switch (auparse->encoding) {
226 case 1: /* 8-bit ISDN mu-law G.711 */
230 case 27: /* 8-bit ISDN A-law G.711 */
235 case 2: /* 8-bit linear PCM */
238 case 3: /* 16-bit linear PCM */
241 case 4: /* 24-bit linear PCM */
244 case 5: /* 32-bit linear PCM */
248 case 6: /* 32-bit IEEE floating point */
252 case 7: /* 64-bit IEEE floating point */
257 case 23: /* 4-bit CCITT G.721 ADPCM 32kbps -> modplug/libsndfile (compressed 8-bit mu-law) */
258 strcpy (layout, "g721");
260 case 24: /* 8-bit CCITT G.722 ADPCM -> rtp */
261 strcpy (layout, "g722");
263 case 25: /* 3-bit CCITT G.723.3 ADPCM 24kbps -> rtp/xine/modplug/libsndfile */
264 strcpy (layout, "g723_3");
266 case 26: /* 5-bit CCITT G.723.5 ADPCM 40kbps -> rtp/xine/modplug/libsndfile */
267 strcpy (layout, "g723_5");
270 case 8: /* Fragmented sample data */
271 case 9: /* AU_ENCODING_NESTED */
273 case 10: /* DSP program */
274 case 11: /* DSP 8-bit fixed point */
275 case 12: /* DSP 16-bit fixed point */
276 case 13: /* DSP 24-bit fixed point */
277 case 14: /* DSP 32-bit fixed point */
279 case 16: /* AU_ENCODING_DISPLAY : non-audio display data */
280 case 17: /* AU_ENCODING_MULAW_SQUELCH */
282 case 18: /* 16-bit linear with emphasis */
283 case 19: /* 16-bit linear compressed (NeXT) */
284 case 20: /* 16-bit linear with emphasis and compression */
286 case 21: /* Music kit DSP commands */
287 case 22: /* Music kit DSP commands samples */
295 gst_caps_new_simple ((law == 1) ? "audio/x-mulaw" : "audio/x-alaw",
296 "rate", G_TYPE_INT, auparse->frequency,
297 "channels", G_TYPE_INT, auparse->channels, NULL);
298 auparse->sample_size = auparse->channels;
300 tempcaps = gst_caps_new_simple ("audio/x-raw-float",
301 "rate", G_TYPE_INT, auparse->frequency,
302 "channels", G_TYPE_INT, auparse->channels,
303 "endianness", G_TYPE_INT,
304 auparse->le ? G_LITTLE_ENDIAN : G_BIG_ENDIAN,
305 "width", G_TYPE_INT, depth, NULL);
306 auparse->sample_size = auparse->channels * depth / 8;
307 } else if (layout[0]) {
308 tempcaps = gst_caps_new_simple ("audio/x-adpcm",
309 "layout", G_TYPE_STRING, layout, NULL);
310 auparse->sample_size = 0;
312 tempcaps = gst_caps_new_simple ("audio/x-raw-int",
313 "rate", G_TYPE_INT, auparse->frequency,
314 "channels", G_TYPE_INT, auparse->channels,
315 "endianness", G_TYPE_INT,
316 auparse->le ? G_LITTLE_ENDIAN : G_BIG_ENDIAN, "depth", G_TYPE_INT,
317 depth, "width", G_TYPE_INT, depth, "signed", G_TYPE_BOOLEAN, TRUE,
319 auparse->sample_size = auparse->channels * depth / 8;
322 gst_pad_set_caps (auparse->srcpad, tempcaps);
328 event = gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_DEFAULT,
329 0, GST_CLOCK_TIME_NONE, 0);
331 gst_pad_push_event (auparse->srcpad, event);
335 databuf = gst_buffer_create_sub (buf, auparse->offset,
336 size - auparse->offset);
338 gst_buffer_unref (buf);
345 if (auparse->sample_size) {
348 gst_adapter_push (auparse->adapter, databuf);
350 /* Ensure we push a buffer that's a multiple of the frame size downstream */
351 avail = gst_adapter_available (auparse->adapter);
352 avail -= avail % auparse->sample_size;
355 const guint8 *data = gst_adapter_peek (auparse->adapter, avail);
359 gst_pad_alloc_buffer_and_set_caps (auparse->srcpad,
360 auparse->buffer_offset, avail, GST_PAD_CAPS (auparse->srcpad),
361 &newbuf)) == GST_FLOW_OK) {
363 memcpy (GST_BUFFER_DATA (newbuf), data, avail);
364 gst_adapter_flush (auparse->adapter, avail);
366 auparse->buffer_offset += avail;
368 ret = gst_pad_push (auparse->srcpad, newbuf);
374 /* It's something non-trivial (such as ADPCM), we don't understand it, so
375 * just push downstream and assume this will know what to do with it */
376 gst_buffer_set_caps (databuf, GST_PAD_CAPS (auparse->srcpad));
377 ret = gst_pad_push (auparse->srcpad, databuf);
380 gst_object_unref (auparse);
387 GST_ELEMENT_ERROR (auparse, STREAM, WRONG_TYPE, (NULL), (NULL));
388 gst_buffer_unref (buf);
389 gst_object_unref (auparse);
390 return GST_FLOW_ERROR;
394 GST_ELEMENT_ERROR (auparse, STREAM, FORMAT, (NULL), (NULL));
395 gst_buffer_unref (buf);
396 gst_object_unref (auparse);
397 return GST_FLOW_ERROR;
401 static GstStateChangeReturn
402 gst_au_parse_change_state (GstElement * element, GstStateChange transition)
404 GstAuParse *auparse = GST_AU_PARSE (element);
405 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
407 ret = parent_class->change_state (element, transition);
408 if (ret == GST_STATE_CHANGE_FAILURE)
411 switch (transition) {
412 case GST_STATE_CHANGE_READY_TO_NULL:
413 gst_adapter_clear (auparse->adapter);
414 auparse->buffer_offset = 0;
417 auparse->encoding = 0;
418 auparse->frequency = 0;
419 auparse->channels = 0;
428 plugin_init (GstPlugin * plugin)
430 if (!gst_element_register (plugin, "auparse", GST_RANK_SECONDARY,
431 GST_TYPE_AU_PARSE)) {
438 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
441 "parses au streams", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,