2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (C) <2006> Tim-Philipp Müller <tim centricular net>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 * SECTION:element-auparse
23 * @short_description: .au file parser
39 #include "gstauparse.h"
40 #include <gst/audio/audio.h>
42 GST_DEBUG_CATEGORY_STATIC (auparse_debug);
43 #define GST_CAT_DEFAULT (auparse_debug)
45 static const GstElementDetails gst_au_parse_details =
46 GST_ELEMENT_DETAILS ("AU audio demuxer",
47 "Codec/Demuxer/Audio",
48 "Parse an .au file into raw audio",
49 "Erik Walthinsen <omega@cse.ogi.edu>");
51 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
54 GST_STATIC_CAPS ("audio/x-au")
57 #define GST_AU_PARSE_ALAW_PAD_TEMPLATE_CAPS \
59 "rate = (int) [ 8000, 192000 ], " \
60 "channels = (int) [ 1, 2 ]"
62 #define GST_AU_PARSE_MULAW_PAD_TEMPLATE_CAPS \
64 "rate = (int) [ 8000, 192000 ], " \
65 "channels = (int) [ 1, 2 ]"
67 /* Nothing to decode those ADPCM streams for now */
68 #define GST_AU_PARSE_ADPCM_PAD_TEMPLATE_CAPS \
70 "layout = (string) { g721, g722, g723_3, g723_5 }"
72 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
75 GST_STATIC_CAPS (GST_AUDIO_INT_PAD_TEMPLATE_CAPS "; "
76 GST_AUDIO_FLOAT_PAD_TEMPLATE_CAPS ";"
77 GST_AU_PARSE_ALAW_PAD_TEMPLATE_CAPS ";"
78 GST_AU_PARSE_MULAW_PAD_TEMPLATE_CAPS ";"
79 GST_AU_PARSE_ADPCM_PAD_TEMPLATE_CAPS));
82 static void gst_au_parse_dispose (GObject * object);
83 static GstFlowReturn gst_au_parse_chain (GstPad * pad, GstBuffer * buf);
84 static GstStateChangeReturn gst_au_parse_change_state (GstElement * element,
85 GstStateChange transition);
86 static void gst_au_parse_reset (GstAuParse * auparse);
87 static gboolean gst_au_parse_remove_srcpad (GstAuParse * auparse);
88 static gboolean gst_au_parse_add_srcpad (GstAuParse * auparse, GstCaps * caps);
89 static gboolean gst_au_parse_src_query (GstPad * pad, GstQuery * query);
90 static gboolean gst_au_parse_src_event (GstPad * pad, GstEvent * event);
91 static gboolean gst_au_parse_sink_event (GstPad * pad, GstEvent * event);
93 GST_BOILERPLATE (GstAuParse, gst_au_parse, GstElement, GST_TYPE_ELEMENT);
96 gst_au_parse_base_init (gpointer g_class)
98 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
100 gst_element_class_add_pad_template (element_class,
101 gst_static_pad_template_get (&sink_template));
102 gst_element_class_add_pad_template (element_class,
103 gst_static_pad_template_get (&src_template));
104 gst_element_class_set_details (element_class, &gst_au_parse_details);
106 GST_DEBUG_CATEGORY_INIT (auparse_debug, "auparse", 0, ".au parser");
110 gst_au_parse_class_init (GstAuParseClass * klass)
112 GObjectClass *gobject_class;
113 GstElementClass *gstelement_class;
115 gobject_class = (GObjectClass *) klass;
116 gstelement_class = (GstElementClass *) klass;
118 gobject_class->dispose = gst_au_parse_dispose;
120 gstelement_class->change_state =
121 GST_DEBUG_FUNCPTR (gst_au_parse_change_state);
125 gst_au_parse_init (GstAuParse * auparse, GstAuParseClass * klass)
127 auparse->sinkpad = gst_pad_new_from_static_template (&sink_template, "sink");
128 gst_pad_set_chain_function (auparse->sinkpad,
129 GST_DEBUG_FUNCPTR (gst_au_parse_chain));
130 gst_pad_set_event_function (auparse->sinkpad,
131 GST_DEBUG_FUNCPTR (gst_au_parse_sink_event));
132 gst_element_add_pad (GST_ELEMENT (auparse), auparse->sinkpad);
134 auparse->srcpad = NULL;
135 auparse->adapter = gst_adapter_new ();
136 gst_au_parse_reset (auparse);
140 gst_au_parse_dispose (GObject * object)
142 GstAuParse *au = GST_AU_PARSE (object);
144 if (au->adapter != NULL) {
145 g_object_unref (au->adapter);
148 G_OBJECT_CLASS (parent_class)->dispose (object);
152 gst_au_parse_reset (GstAuParse * auparse)
154 gst_au_parse_remove_srcpad (auparse);
157 auparse->buffer_offset = 0;
158 auparse->encoding = 0;
159 auparse->samplerate = 0;
160 auparse->channels = 0;
161 auparse->float_swap = 0;
163 gst_adapter_clear (auparse->adapter);
165 /* gst_segment_init (&auparse->segment, GST_FORMAT_TIME); */
169 gst_au_parse_add_srcpad (GstAuParse * auparse, GstCaps * new_caps)
171 GstPad *srcpad = NULL;
173 if (auparse->src_caps && gst_caps_is_equal (new_caps, auparse->src_caps)) {
174 GST_LOG_OBJECT (auparse, "same caps, nothing to do");
178 gst_caps_replace (&auparse->src_caps, new_caps);
179 if (auparse->srcpad != NULL) {
180 GST_DEBUG_OBJECT (auparse, "Changing src pad caps to %" GST_PTR_FORMAT,
182 gst_pad_set_caps (auparse->srcpad, auparse->src_caps);
185 if (auparse->srcpad == NULL) {
186 srcpad = auparse->srcpad =
187 gst_pad_new_from_static_template (&src_template, "src");
188 g_return_val_if_fail (auparse->srcpad != NULL, FALSE);
191 gst_pad_set_query_type_function (auparse->srcpad,
192 GST_DEBUG_FUNCPTR (gst_au_parse_src_get_query_types));
194 gst_pad_set_query_function (auparse->srcpad,
195 GST_DEBUG_FUNCPTR (gst_au_parse_src_query));
196 gst_pad_set_event_function (auparse->srcpad,
197 GST_DEBUG_FUNCPTR (gst_au_parse_src_event));
199 gst_pad_use_fixed_caps (auparse->srcpad);
200 gst_pad_set_active (auparse->srcpad, TRUE);
202 if (auparse->src_caps)
203 gst_pad_set_caps (auparse->srcpad, auparse->src_caps);
205 GST_DEBUG_OBJECT (auparse, "Adding src pad with caps %" GST_PTR_FORMAT,
208 gst_object_ref (auparse->srcpad);
209 if (!gst_element_add_pad (GST_ELEMENT (auparse), auparse->srcpad))
211 gst_element_no_more_pads (GST_ELEMENT (auparse));
218 gst_au_parse_remove_srcpad (GstAuParse * auparse)
222 if (auparse->srcpad != NULL) {
223 GST_DEBUG_OBJECT (auparse, "Removing src pad");
224 res = gst_element_remove_pad (GST_ELEMENT (auparse), auparse->srcpad);
225 g_return_val_if_fail (res != FALSE, FALSE);
226 gst_object_unref (auparse->srcpad);
227 auparse->srcpad = NULL;
234 gst_au_parse_parse_header (GstAuParse * auparse)
239 gchar layout[7] = { 0, };
240 gint law = 0, depth = 0, ieee = 0;
242 head = (guint8 *) gst_adapter_peek (auparse->adapter, 24);
243 g_assert (head != NULL);
245 GST_DEBUG_OBJECT (auparse, "[%c%c%c%c]", head[0], head[1], head[2], head[3]);
247 switch (GST_READ_UINT32_BE (head)) {
248 /* normal format is big endian (au is a Sparc format) */
249 case 0x2e736e64:{ /* ".snd" */
250 auparse->endianness = G_BIG_ENDIAN;
253 /* and of course, someone had to invent a little endian
254 * version. Used by DEC systems. */
255 case 0x646e732e: /* dns. */
256 case 0x0064732e:{ /* other source say it is "dns." */
257 auparse->endianness = G_LITTLE_ENDIAN;
265 auparse->offset = GST_READ_UINT32_BE (head + 4);
266 /* Do not trust size, could be set to -1 : unknown */
267 size = GST_READ_UINT32_BE (head + 8);
268 auparse->encoding = GST_READ_UINT32_BE (head + 12);
269 auparse->samplerate = GST_READ_UINT32_BE (head + 16);
270 auparse->channels = GST_READ_UINT32_BE (head + 20);
272 if (auparse->samplerate < 8000 || auparse->samplerate > 192000)
273 goto unsupported_sample_rate;
275 if (auparse->channels < 1 || auparse->channels > 2)
276 goto unsupported_number_of_channels;
278 GST_DEBUG_OBJECT (auparse, "offset %" G_GINT64_FORMAT ", size %u, "
279 "encoding %u, frequency %u, channels %u", auparse->offset, size,
280 auparse->encoding, auparse->samplerate, auparse->channels);
283 * http://www.opengroup.org/public/pubs/external/auformat.html
284 * http://astronomy.swin.edu.au/~pbourke/dataformats/au/
285 * Solaris headers : /usr/include/audio/au.h
286 * libsndfile : src/au.c
289 * http://www.tsp.ece.mcgill.ca/MMSP/Documents/AudioFormats/AU/Samples.html
292 auparse->float_swap = 0;
294 switch (auparse->encoding) {
295 case 1: /* 8-bit ISDN mu-law G.711 */
299 case 27: /* 8-bit ISDN A-law G.711 */
304 case 2: /* 8-bit linear PCM */
307 case 3: /* 16-bit linear PCM */
310 case 4: /* 24-bit linear PCM */
313 case 5: /* 32-bit linear PCM */
317 case 6: /* 32-bit IEEE floating point */
321 case 7: /* 64-bit IEEE floating point */
326 case 23: /* 4-bit CCITT G.721 ADPCM 32kbps -> modplug/libsndfile (compressed 8-bit mu-law) */
327 strcpy (layout, "g721");
329 case 24: /* 8-bit CCITT G.722 ADPCM -> rtp */
330 strcpy (layout, "g722");
332 case 25: /* 3-bit CCITT G.723.3 ADPCM 24kbps -> rtp/xine/modplug/libsndfile */
333 strcpy (layout, "g723_3");
335 case 26: /* 5-bit CCITT G.723.5 ADPCM 40kbps -> rtp/xine/modplug/libsndfile */
336 strcpy (layout, "g723_5");
339 case 8: /* Fragmented sample data */
340 case 9: /* AU_ENCODING_NESTED */
342 case 10: /* DSP program */
343 case 11: /* DSP 8-bit fixed point */
344 case 12: /* DSP 16-bit fixed point */
345 case 13: /* DSP 24-bit fixed point */
346 case 14: /* DSP 32-bit fixed point */
348 case 16: /* AU_ENCODING_DISPLAY : non-audio display data */
349 case 17: /* AU_ENCODING_MULAW_SQUELCH */
351 case 18: /* 16-bit linear with emphasis */
352 case 19: /* 16-bit linear compressed (NeXT) */
353 case 20: /* 16-bit linear with emphasis and compression */
355 case 21: /* Music kit DSP commands */
356 case 22: /* Music kit DSP commands samples */
364 gst_caps_new_simple ((law == 1) ? "audio/x-mulaw" : "audio/x-alaw",
365 "rate", G_TYPE_INT, auparse->samplerate,
366 "channels", G_TYPE_INT, auparse->channels, NULL);
367 auparse->sample_size = auparse->channels;
369 tempcaps = gst_caps_new_simple ("audio/x-raw-float",
370 "rate", G_TYPE_INT, auparse->samplerate,
371 "channels", G_TYPE_INT, auparse->channels,
372 "endianness", G_TYPE_INT, G_BYTE_ORDER,
373 "width", G_TYPE_INT, depth, NULL);
374 auparse->sample_size = auparse->channels * depth / 8;
375 if (auparse->endianness != G_BYTE_ORDER) {
376 GST_DEBUG_OBJECT (auparse, "need to swap float byte order ourselves!");
377 auparse->float_swap = depth;
379 } else if (layout[0]) {
380 tempcaps = gst_caps_new_simple ("audio/x-adpcm",
381 "layout", G_TYPE_STRING, layout, NULL);
382 auparse->sample_size = 0;
384 tempcaps = gst_caps_new_simple ("audio/x-raw-int",
385 "rate", G_TYPE_INT, auparse->samplerate,
386 "channels", G_TYPE_INT, auparse->channels,
387 "endianness", G_TYPE_INT, auparse->endianness,
388 "depth", G_TYPE_INT, depth, "width", G_TYPE_INT, depth,
389 /* FIXME: signed TRUE even for 8-bit PCM? */
390 "signed", G_TYPE_BOOLEAN, TRUE, NULL);
391 auparse->sample_size = auparse->channels * depth / 8;
394 GST_DEBUG_OBJECT (auparse, "sample_size=%d", auparse->sample_size);
396 if (!gst_au_parse_add_srcpad (auparse, tempcaps))
399 GST_DEBUG_OBJECT (auparse, "offset=%" G_GINT64_FORMAT, auparse->offset);
400 gst_adapter_flush (auparse->adapter, auparse->offset);
402 gst_caps_unref (tempcaps);
408 GST_ELEMENT_ERROR (auparse, STREAM, WRONG_TYPE, (NULL), (NULL));
409 return GST_FLOW_ERROR;
411 unsupported_sample_rate:
413 GST_ELEMENT_ERROR (auparse, STREAM, FORMAT, (NULL),
414 ("Unsupported samplerate: %u", auparse->samplerate));
415 return GST_FLOW_ERROR;
417 unsupported_number_of_channels:
419 GST_ELEMENT_ERROR (auparse, STREAM, FORMAT, (NULL),
420 ("Unsupported number of channels: %u", auparse->channels));
421 return GST_FLOW_ERROR;
425 GST_ELEMENT_ERROR (auparse, STREAM, FORMAT, (NULL),
426 ("Unsupported encoding: %u", auparse->encoding));
427 return GST_FLOW_ERROR;
431 GST_ELEMENT_ERROR (auparse, STREAM, FAILED, (NULL),
432 ("Failed to add srcpad"));
433 gst_caps_unref (tempcaps);
434 return GST_FLOW_ERROR;
438 #define AU_HEADER_SIZE 24
441 gst_au_parse_chain (GstPad * pad, GstBuffer * buf)
443 GstFlowReturn ret = GST_FLOW_OK;
445 gint avail, sendnow = 0;
447 auparse = GST_AU_PARSE (gst_pad_get_parent (pad));
449 GST_LOG_OBJECT (auparse, "got buffer of size %u", GST_BUFFER_SIZE (buf));
451 gst_adapter_push (auparse->adapter, buf);
454 /* if we haven't seen any data yet... */
455 if (auparse->srcpad == NULL) {
456 if (gst_adapter_available (auparse->adapter) < AU_HEADER_SIZE) {
457 GST_DEBUG_OBJECT (auparse, "need more data to parse header");
462 ret = gst_au_parse_parse_header (auparse);
463 if (ret != GST_FLOW_OK)
466 gst_pad_push_event (auparse->srcpad,
467 gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_DEFAULT,
468 0, GST_CLOCK_TIME_NONE, 0));
471 avail = gst_adapter_available (auparse->adapter);
473 if (auparse->sample_size > 0) {
474 /* Ensure we push a buffer that's a multiple of the frame size downstream */
475 sendnow = avail - (avail % auparse->sample_size);
477 /* It's something non-trivial (such as ADPCM), we don't understand it, so
478 * just push downstream and assume it will know what to do with it */
486 ret = gst_pad_alloc_buffer_and_set_caps (auparse->srcpad,
487 auparse->buffer_offset, sendnow, GST_PAD_CAPS (auparse->srcpad),
490 if (ret != GST_FLOW_OK) {
491 GST_DEBUG_OBJECT (auparse, "pad alloc flow: %s", gst_flow_get_name (ret));
495 data = gst_adapter_peek (auparse->adapter, sendnow);
497 /* audioconvert only handles floats in native endianness ... */
498 switch (auparse->float_swap) {
500 guint32 *indata = (guint32 *) data;
501 guint32 *outdata = (guint32 *) GST_BUFFER_DATA (outbuf);
504 for (i = 0; i < (sendnow / sizeof (guint32)); ++i) {
505 outdata[i] = GUINT32_SWAP_LE_BE (indata[i]);
510 guint64 *indata = (guint64 *) data;
511 guint64 *outdata = (guint64 *) GST_BUFFER_DATA (outbuf);
514 for (i = 0; i < (sendnow / sizeof (guint64)); ++i) {
515 outdata[i] = GUINT64_SWAP_LE_BE (indata[i]);
520 memcpy (GST_BUFFER_DATA (outbuf), data, sendnow);
525 gst_adapter_flush (auparse->adapter, sendnow);
527 auparse->buffer_offset += sendnow;
529 ret = gst_pad_push (auparse->srcpad, outbuf);
534 gst_object_unref (auparse);
539 gst_au_parse_src_convert (GstAuParse * auparse, GstFormat src_format,
540 gint64 srcval, GstFormat dest_format, gint64 * destval)
544 guint samplesize, rate;
546 if (dest_format == src_format) {
551 GST_OBJECT_LOCK (auparse);
552 samplesize = auparse->sample_size;
553 offset = auparse->offset;
554 rate = auparse->samplerate;
555 GST_OBJECT_UNLOCK (auparse);
557 if (samplesize == 0 || rate == 0) {
558 GST_LOG_OBJECT (auparse, "cannot convert, sample_size or rate unknown");
562 switch (src_format) {
563 case GST_FORMAT_BYTES:
564 srcval /= samplesize;
566 case GST_FORMAT_DEFAULT:{
567 switch (dest_format) {
568 case GST_FORMAT_BYTES:
569 *destval = srcval * samplesize;
571 case GST_FORMAT_TIME:
572 *destval = gst_util_uint64_scale_int (srcval, GST_SECOND, rate);
580 case GST_FORMAT_TIME:{
581 switch (dest_format) {
582 case GST_FORMAT_BYTES:
584 gst_util_uint64_scale_int (srcval, rate * samplesize, GST_SECOND);
586 case GST_FORMAT_DEFAULT:
587 *destval = gst_util_uint64_scale_int (srcval, rate, GST_SECOND);
602 GST_DEBUG_OBJECT (auparse, "could not convert from %s to %s format",
603 gst_format_get_name (src_format), gst_format_get_name (dest_format));
610 gst_au_parse_src_query (GstPad * pad, GstQuery * query)
613 gboolean ret = FALSE;
615 auparse = GST_AU_PARSE (gst_pad_get_parent (pad));
617 switch (GST_QUERY_TYPE (query)) {
618 case GST_QUERY_DURATION:{
619 GstFormat bformat = GST_FORMAT_BYTES;
623 gst_query_parse_duration (query, &format, NULL);
624 if (!gst_pad_query_peer_duration (auparse->sinkpad, &bformat, &len)) {
625 GST_DEBUG_OBJECT (auparse, "failed to query upstream length");
628 GST_OBJECT_LOCK (auparse);
629 len -= auparse->offset;
630 GST_OBJECT_UNLOCK (auparse);
632 ret = gst_au_parse_src_convert (auparse, GST_FORMAT_BYTES, len,
636 gst_query_set_duration (query, format, val);
640 case GST_QUERY_POSITION:{
641 GstFormat bformat = GST_FORMAT_BYTES;
645 gst_query_parse_position (query, &format, NULL);
646 if (!gst_pad_query_peer_position (auparse->sinkpad, &bformat, &pos)) {
647 GST_DEBUG_OBJECT (auparse, "failed to query upstream position");
650 GST_OBJECT_LOCK (auparse);
651 pos -= auparse->offset;
652 GST_OBJECT_UNLOCK (auparse);
654 ret = gst_au_parse_src_convert (auparse, GST_FORMAT_BYTES, pos,
658 gst_query_set_position (query, format, val);
663 ret = gst_pad_query_default (pad, query);
667 gst_object_unref (auparse);
672 gst_au_parse_handle_seek (GstAuParse * auparse, GstEvent * event)
674 GstSeekType start_type, stop_type;
680 gst_event_parse_seek (event, &rate, &format, &flags, &start_type, &start,
683 if (format != GST_FORMAT_TIME) {
684 GST_DEBUG_OBJECT (auparse, "only support seeks in TIME format");
688 /* FIXME: implement seeking */
693 gst_au_parse_sink_event (GstPad * pad, GstEvent * event)
698 auparse = GST_AU_PARSE (gst_pad_get_parent (pad));
700 switch (GST_EVENT_TYPE (event)) {
702 ret = gst_pad_event_default (pad, event);
706 gst_object_unref (auparse);
711 gst_au_parse_src_event (GstPad * pad, GstEvent * event)
716 auparse = GST_AU_PARSE (gst_pad_get_parent (pad));
718 switch (GST_EVENT_TYPE (event)) {
720 ret = gst_au_parse_handle_seek (auparse, event);
723 ret = gst_pad_event_default (pad, event);
727 gst_object_unref (auparse);
731 static GstStateChangeReturn
732 gst_au_parse_change_state (GstElement * element, GstStateChange transition)
734 GstAuParse *auparse = GST_AU_PARSE (element);
735 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
737 ret = parent_class->change_state (element, transition);
738 if (ret == GST_STATE_CHANGE_FAILURE)
741 switch (transition) {
742 case GST_STATE_CHANGE_PAUSED_TO_READY:
743 gst_au_parse_reset (auparse);
752 plugin_init (GstPlugin * plugin)
754 if (!gst_element_register (plugin, "auparse", GST_RANK_SECONDARY,
755 GST_TYPE_AU_PARSE)) {
762 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
765 "parses au streams", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,