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);
79 static void gst_auparse_dispose (GObject * object);
81 static GstFlowReturn gst_auparse_chain (GstPad * pad, GstBuffer * buf);
83 static GstStateChangeReturn gst_auparse_change_state (GstElement * element,
84 GstStateChange transition);
86 static GstElementClass *parent_class = NULL;
89 /*static guint gst_auparse_signals[LAST_SIGNAL] = { 0 }; */
92 gst_auparse_get_type (void)
94 static GType auparse_type = 0;
97 static const GTypeInfo auparse_info = {
98 sizeof (GstAuParseClass),
99 gst_auparse_base_init,
101 (GClassInitFunc) gst_auparse_class_init,
106 (GInstanceInitFunc) gst_auparse_init,
110 g_type_register_static (GST_TYPE_ELEMENT, "GstAuParse", &auparse_info,
117 gst_auparse_base_init (gpointer g_class)
119 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
121 gst_element_class_add_pad_template (element_class,
122 gst_static_pad_template_get (&gst_auparse_sink_template));
123 gst_element_class_add_pad_template (element_class,
124 gst_static_pad_template_get (&gst_auparse_src_template));
125 gst_element_class_set_details (element_class, &gst_auparse_details);
130 gst_auparse_class_init (GstAuParseClass * klass)
132 GObjectClass *gobject_class;
133 GstElementClass *gstelement_class;
135 gobject_class = (GObjectClass *) klass;
136 gstelement_class = (GstElementClass *) klass;
138 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
140 gobject_class->dispose = gst_auparse_dispose;
142 gstelement_class->change_state = gst_auparse_change_state;
146 gst_auparse_init (GstAuParse * auparse)
149 gst_pad_new_from_template (gst_static_pad_template_get
150 (&gst_auparse_sink_template), "sink");
151 gst_element_add_pad (GST_ELEMENT (auparse), auparse->sinkpad);
152 gst_pad_set_chain_function (auparse->sinkpad, gst_auparse_chain);
154 auparse->srcpad = gst_pad_new_from_template (gst_static_pad_template_get
155 (&gst_auparse_src_template), "src");
156 gst_pad_use_fixed_caps (auparse->srcpad);
157 gst_element_add_pad (GST_ELEMENT (auparse), auparse->srcpad);
160 auparse->buffer_offset = 0;
161 auparse->adapter = gst_adapter_new ();
163 auparse->encoding = 0;
164 auparse->frequency = 0;
165 auparse->channels = 0;
169 gst_auparse_dispose (GObject * object)
171 GstAuParse *au = GST_AUPARSE (object);
173 if (au->adapter != NULL) {
174 gst_object_unref (au->adapter);
177 G_OBJECT_CLASS (parent_class)->dispose (object);
181 gst_auparse_chain (GstPad * pad, GstBuffer * buf)
188 gint law = 0, depth = 0, ieee = 0;
195 auparse = GST_AUPARSE (gst_pad_get_parent (pad));
197 GST_DEBUG ("gst_auparse_chain: got buffer in '%s'",
198 gst_element_get_name (GST_ELEMENT (auparse)));
200 data = GST_BUFFER_DATA (buf);
201 size = GST_BUFFER_SIZE (buf);
203 /* if we haven't seen any data yet... */
204 if (auparse->size == 0) {
205 guint32 *head = (guint32 *) data;
207 /* normal format is big endian (au is a Sparc format) */
208 if (GST_READ_UINT32_BE (head) == 0x2e736e64) { /* ".snd" */
211 auparse->offset = GST_READ_UINT32_BE (head);
213 /* Do not trust size, could be set to -1 : unknown */
214 auparse->size = GST_READ_UINT32_BE (head);
216 auparse->encoding = GST_READ_UINT32_BE (head);
218 auparse->frequency = GST_READ_UINT32_BE (head);
220 auparse->channels = GST_READ_UINT32_BE (head);
223 /* and of course, someone had to invent a little endian
224 * version. Used by DEC systems. */
225 } else if (GST_READ_UINT32_LE (head) == 0x0064732E) { /* other source say it is "dns." */
228 auparse->offset = GST_READ_UINT32_LE (head);
230 /* Do not trust size, could be set to -1 : unknown */
231 auparse->size = GST_READ_UINT32_LE (head);
233 auparse->encoding = GST_READ_UINT32_LE (head);
235 auparse->frequency = GST_READ_UINT32_LE (head);
237 auparse->channels = GST_READ_UINT32_LE (head);
241 GST_ELEMENT_ERROR (auparse, STREAM, WRONG_TYPE, (NULL), (NULL));
242 gst_buffer_unref (buf);
243 g_object_unref (auparse);
244 return GST_FLOW_ERROR;
248 ("offset %ld, size %ld, encoding %ld, frequency %ld, channels %ld\n",
249 auparse->offset, auparse->size, auparse->encoding, auparse->frequency,
254 http://www.opengroup.org/public/pubs/external/auformat.html
255 http://astronomy.swin.edu.au/~pbourke/dataformats/au/
256 Solaris headers : /usr/include/audio/au.h
257 libsndfile : src/au.c
259 http://www.tsp.ece.mcgill.ca/MMSP/Documents/AudioFormats/AU/Samples.html
262 switch (auparse->encoding) {
264 case 1: /* 8-bit ISDN mu-law G.711 */
268 case 27: /* 8-bit ISDN A-law G.711 */
273 case 2: /* 8-bit linear PCM */
276 case 3: /* 16-bit linear PCM */
279 case 4: /* 24-bit linear PCM */
282 case 5: /* 32-bit linear PCM */
286 case 6: /* 32-bit IEEE floating point */
290 case 7: /* 64-bit IEEE floating point */
295 case 23: /* 4-bit CCITT G.721 ADPCM 32kbps -> modplug/libsndfile (compressed 8-bit mu-law) */
296 strcpy (layout, "g721");
298 case 24: /* 8-bit CCITT G.722 ADPCM -> rtp */
299 strcpy (layout, "g722");
301 case 25: /* 3-bit CCITT G.723.3 ADPCM 24kbps -> rtp/xine/modplug/libsndfile */
302 strcpy (layout, "g723_3");
304 case 26: /* 5-bit CCITT G.723.5 ADPCM 40kbps -> rtp/xine/modplug/libsndfile */
305 strcpy (layout, "g723_5");
308 case 8: /* Fragmented sample data */
309 case 9: /* AU_ENCODING_NESTED */
311 case 10: /* DSP program */
312 case 11: /* DSP 8-bit fixed point */
313 case 12: /* DSP 16-bit fixed point */
314 case 13: /* DSP 24-bit fixed point */
315 case 14: /* DSP 32-bit fixed point */
317 case 16: /* AU_ENCODING_DISPLAY : non-audio display data */
318 case 17: /* AU_ENCODING_MULAW_SQUELCH */
320 case 18: /* 16-bit linear with emphasis */
321 case 19: /* 16-bit linear compressed (NeXT) */
322 case 20: /* 16-bit linear with emphasis and compression */
324 case 21: /* Music kit DSP commands */
325 case 22: /* Music kit DSP commands samples */
328 GST_ELEMENT_ERROR (auparse, STREAM, FORMAT, (NULL), (NULL));
329 gst_buffer_unref (buf);
330 g_object_unref (auparse);
331 return GST_FLOW_ERROR;
336 gst_caps_new_simple ((law == 1) ? "audio/x-mulaw" : "audio/x-alaw",
337 "rate", G_TYPE_INT, auparse->frequency,
338 "channels", G_TYPE_INT, auparse->channels, NULL);
339 auparse->sample_size = auparse->channels;
341 tempcaps = gst_caps_new_simple ("audio/x-raw-float",
342 "rate", G_TYPE_INT, auparse->frequency,
343 "channels", G_TYPE_INT, auparse->channels,
344 "endianness", G_TYPE_INT,
345 auparse->le ? G_LITTLE_ENDIAN : G_BIG_ENDIAN,
346 "width", G_TYPE_INT, depth, NULL);
347 auparse->sample_size = auparse->channels * depth / 8;
348 } else if (layout[0]) {
349 tempcaps = gst_caps_new_simple ("audio/x-adpcm",
350 "layout", G_TYPE_STRING, layout, NULL);
351 auparse->sample_size = 0;
353 tempcaps = gst_caps_new_simple ("audio/x-raw-int",
354 "rate", G_TYPE_INT, auparse->frequency,
355 "channels", G_TYPE_INT, auparse->channels,
356 "endianness", G_TYPE_INT,
357 auparse->le ? G_LITTLE_ENDIAN : G_BIG_ENDIAN, "depth", G_TYPE_INT,
358 depth, "width", G_TYPE_INT, depth, "signed", G_TYPE_BOOLEAN, TRUE,
360 auparse->sample_size = auparse->channels * depth / 8;
363 gst_pad_set_active (auparse->srcpad, TRUE);
364 gst_pad_set_caps (auparse->srcpad, tempcaps);
366 event = gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_DEFAULT,
367 0, GST_CLOCK_TIME_NONE, 0);
369 gst_pad_push_event (auparse->srcpad, event);
371 subbuf = gst_buffer_create_sub (buf, auparse->offset,
372 size - auparse->offset);
374 gst_buffer_unref (buf);
376 gst_adapter_push (auparse->adapter, subbuf);
378 gst_adapter_push (auparse->adapter, buf);
381 if (auparse->sample_size) {
382 /* Ensure we push a buffer that's a multiple of the frame size downstream */
383 int avail = gst_adapter_available (auparse->adapter);
385 avail -= avail % auparse->sample_size;
388 const guint8 *data = gst_adapter_peek (auparse->adapter, avail);
392 gst_pad_alloc_buffer_and_set_caps (auparse->srcpad,
393 auparse->buffer_offset, avail, GST_PAD_CAPS (auparse->srcpad),
394 &newbuf)) == GST_FLOW_OK) {
396 memcpy (GST_BUFFER_DATA (newbuf), data, avail);
397 gst_adapter_flush (auparse->adapter, avail);
399 auparse->buffer_offset += avail;
401 ret = gst_pad_push (auparse->srcpad, newbuf);
406 /* It's something non-trivial (such as ADPCM), we don't understand it, so
407 * just push downstream and assume this will know what to do with it */
408 ret = gst_pad_push (auparse->srcpad, buf);
411 g_object_unref (auparse);
416 static GstStateChangeReturn
417 gst_auparse_change_state (GstElement * element, GstStateChange transition)
419 GstAuParse *auparse = GST_AUPARSE (element);
420 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
422 if (parent_class->change_state)
423 ret = parent_class->change_state (element, transition);
425 switch (transition) {
426 case GST_STATE_CHANGE_READY_TO_NULL:
427 gst_adapter_clear (auparse->adapter);
428 auparse->buffer_offset = 0;
431 auparse->encoding = 0;
432 auparse->frequency = 0;
433 auparse->channels = 0;
442 plugin_init (GstPlugin * plugin)
444 if (!gst_element_register (plugin, "auparse", GST_RANK_SECONDARY,
452 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
455 "parses au streams", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,