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
24 * Parses .au files mostly originating from sun os based computers.
34 #include "gstauparse.h"
35 #include <gst/audio/audio.h>
37 GST_DEBUG_CATEGORY_STATIC (auparse_debug);
38 #define GST_CAT_DEFAULT (auparse_debug)
40 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
43 GST_STATIC_CAPS ("audio/x-au")
46 #define GST_AU_PARSE_ALAW_PAD_TEMPLATE_CAPS \
48 "rate = (int) [ 8000, 192000 ], " \
49 "channels = (int) [ 1, 2 ]"
51 #define GST_AU_PARSE_MULAW_PAD_TEMPLATE_CAPS \
53 "rate = (int) [ 8000, 192000 ], " \
54 "channels = (int) [ 1, 2 ]"
56 /* Nothing to decode those ADPCM streams for now */
57 #define GST_AU_PARSE_ADPCM_PAD_TEMPLATE_CAPS \
59 "layout = (string) { g721, g722, g723_3, g723_5 }"
61 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
64 GST_STATIC_CAPS (GST_AUDIO_INT_PAD_TEMPLATE_CAPS "; "
65 GST_AUDIO_FLOAT_PAD_TEMPLATE_CAPS ";"
66 GST_AU_PARSE_ALAW_PAD_TEMPLATE_CAPS ";"
67 GST_AU_PARSE_MULAW_PAD_TEMPLATE_CAPS ";"
68 GST_AU_PARSE_ADPCM_PAD_TEMPLATE_CAPS));
71 static void gst_au_parse_dispose (GObject * object);
72 static GstFlowReturn gst_au_parse_chain (GstPad * pad, GstBuffer * buf);
73 static GstStateChangeReturn gst_au_parse_change_state (GstElement * element,
74 GstStateChange transition);
75 static void gst_au_parse_reset (GstAuParse * auparse);
76 static gboolean gst_au_parse_remove_srcpad (GstAuParse * auparse);
77 static gboolean gst_au_parse_add_srcpad (GstAuParse * auparse, GstCaps * caps);
78 static gboolean gst_au_parse_src_query (GstPad * pad, GstQuery * query);
79 static gboolean gst_au_parse_src_event (GstPad * pad, GstEvent * event);
80 static gboolean gst_au_parse_sink_event (GstPad * pad, GstEvent * event);
82 GST_BOILERPLATE (GstAuParse, gst_au_parse, GstElement, GST_TYPE_ELEMENT);
85 gst_au_parse_base_init (gpointer g_class)
87 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
89 gst_element_class_add_pad_template (element_class,
90 gst_static_pad_template_get (&sink_template));
91 gst_element_class_add_pad_template (element_class,
92 gst_static_pad_template_get (&src_template));
93 gst_element_class_set_details_simple (element_class, "AU audio demuxer",
94 "Codec/Demuxer/Audio",
95 "Parse an .au file into raw audio",
96 "Erik Walthinsen <omega@cse.ogi.edu>");
98 GST_DEBUG_CATEGORY_INIT (auparse_debug, "auparse", 0, ".au parser");
102 gst_au_parse_class_init (GstAuParseClass * klass)
104 GObjectClass *gobject_class;
105 GstElementClass *gstelement_class;
107 gobject_class = (GObjectClass *) klass;
108 gstelement_class = (GstElementClass *) klass;
110 gobject_class->dispose = gst_au_parse_dispose;
112 gstelement_class->change_state =
113 GST_DEBUG_FUNCPTR (gst_au_parse_change_state);
117 gst_au_parse_init (GstAuParse * auparse, GstAuParseClass * klass)
119 auparse->sinkpad = gst_pad_new_from_static_template (&sink_template, "sink");
120 gst_pad_set_chain_function (auparse->sinkpad,
121 GST_DEBUG_FUNCPTR (gst_au_parse_chain));
122 gst_pad_set_event_function (auparse->sinkpad,
123 GST_DEBUG_FUNCPTR (gst_au_parse_sink_event));
124 gst_element_add_pad (GST_ELEMENT (auparse), auparse->sinkpad);
126 auparse->srcpad = NULL;
127 auparse->adapter = gst_adapter_new ();
128 gst_au_parse_reset (auparse);
132 gst_au_parse_dispose (GObject * object)
134 GstAuParse *au = GST_AU_PARSE (object);
136 if (au->adapter != NULL) {
137 g_object_unref (au->adapter);
140 G_OBJECT_CLASS (parent_class)->dispose (object);
144 gst_au_parse_reset (GstAuParse * auparse)
146 gst_au_parse_remove_srcpad (auparse);
149 auparse->buffer_offset = 0;
150 auparse->encoding = 0;
151 auparse->samplerate = 0;
152 auparse->channels = 0;
154 gst_adapter_clear (auparse->adapter);
156 /* gst_segment_init (&auparse->segment, GST_FORMAT_TIME); */
160 gst_au_parse_add_srcpad (GstAuParse * auparse, GstCaps * new_caps)
162 if (auparse->src_caps && gst_caps_is_equal (new_caps, auparse->src_caps)) {
163 GST_LOG_OBJECT (auparse, "same caps, nothing to do");
167 gst_caps_replace (&auparse->src_caps, new_caps);
168 if (auparse->srcpad != NULL) {
169 GST_DEBUG_OBJECT (auparse, "Changing src pad caps to %" GST_PTR_FORMAT,
171 gst_pad_set_caps (auparse->srcpad, auparse->src_caps);
174 if (auparse->srcpad == NULL) {
175 auparse->srcpad = gst_pad_new_from_static_template (&src_template, "src");
176 g_return_val_if_fail (auparse->srcpad != NULL, FALSE);
179 gst_pad_set_query_type_function (auparse->srcpad,
180 GST_DEBUG_FUNCPTR (gst_au_parse_src_get_query_types));
182 gst_pad_set_query_function (auparse->srcpad,
183 GST_DEBUG_FUNCPTR (gst_au_parse_src_query));
184 gst_pad_set_event_function (auparse->srcpad,
185 GST_DEBUG_FUNCPTR (gst_au_parse_src_event));
187 gst_pad_use_fixed_caps (auparse->srcpad);
188 gst_pad_set_active (auparse->srcpad, TRUE);
190 if (auparse->src_caps)
191 gst_pad_set_caps (auparse->srcpad, auparse->src_caps);
193 GST_DEBUG_OBJECT (auparse, "Adding src pad with caps %" GST_PTR_FORMAT,
196 gst_object_ref (auparse->srcpad);
197 if (!gst_element_add_pad (GST_ELEMENT (auparse), auparse->srcpad))
199 gst_element_no_more_pads (GST_ELEMENT (auparse));
206 gst_au_parse_remove_srcpad (GstAuParse * auparse)
210 if (auparse->srcpad != NULL) {
211 GST_DEBUG_OBJECT (auparse, "Removing src pad");
212 res = gst_element_remove_pad (GST_ELEMENT (auparse), auparse->srcpad);
213 g_return_val_if_fail (res != FALSE, FALSE);
214 gst_object_unref (auparse->srcpad);
215 auparse->srcpad = NULL;
222 gst_au_parse_parse_header (GstAuParse * auparse)
227 gchar layout[7] = { 0, };
228 gint law = 0, depth = 0, ieee = 0;
230 head = (guint8 *) gst_adapter_peek (auparse->adapter, 24);
231 g_assert (head != NULL);
233 GST_DEBUG_OBJECT (auparse, "[%c%c%c%c]", head[0], head[1], head[2], head[3]);
235 switch (GST_READ_UINT32_BE (head)) {
236 /* normal format is big endian (au is a Sparc format) */
237 case 0x2e736e64:{ /* ".snd" */
238 auparse->endianness = G_BIG_ENDIAN;
241 /* and of course, someone had to invent a little endian
242 * version. Used by DEC systems. */
243 case 0x646e732e: /* dns. */
244 case 0x0064732e:{ /* other source say it is "dns." */
245 auparse->endianness = G_LITTLE_ENDIAN;
253 auparse->offset = GST_READ_UINT32_BE (head + 4);
254 /* Do not trust size, could be set to -1 : unknown */
255 size = GST_READ_UINT32_BE (head + 8);
256 auparse->encoding = GST_READ_UINT32_BE (head + 12);
257 auparse->samplerate = GST_READ_UINT32_BE (head + 16);
258 auparse->channels = GST_READ_UINT32_BE (head + 20);
260 if (auparse->samplerate < 8000 || auparse->samplerate > 192000)
261 goto unsupported_sample_rate;
263 if (auparse->channels < 1 || auparse->channels > 2)
264 goto unsupported_number_of_channels;
266 GST_DEBUG_OBJECT (auparse, "offset %" G_GINT64_FORMAT ", size %u, "
267 "encoding %u, frequency %u, channels %u", auparse->offset, size,
268 auparse->encoding, auparse->samplerate, auparse->channels);
271 * http://www.opengroup.org/public/pubs/external/auformat.html
272 * http://astronomy.swin.edu.au/~pbourke/dataformats/au/
273 * Solaris headers : /usr/include/audio/au.h
274 * libsndfile : src/au.c
277 * http://www.tsp.ece.mcgill.ca/MMSP/Documents/AudioFormats/AU/Samples.html
280 switch (auparse->encoding) {
281 case 1: /* 8-bit ISDN mu-law G.711 */
285 case 27: /* 8-bit ISDN A-law G.711 */
290 case 2: /* 8-bit linear PCM */
293 case 3: /* 16-bit linear PCM */
296 case 4: /* 24-bit linear PCM */
299 case 5: /* 32-bit linear PCM */
303 case 6: /* 32-bit IEEE floating point */
307 case 7: /* 64-bit IEEE floating point */
312 case 23: /* 4-bit CCITT G.721 ADPCM 32kbps -> modplug/libsndfile (compressed 8-bit mu-law) */
313 strcpy (layout, "g721");
315 case 24: /* 8-bit CCITT G.722 ADPCM -> rtp */
316 strcpy (layout, "g722");
318 case 25: /* 3-bit CCITT G.723.3 ADPCM 24kbps -> rtp/xine/modplug/libsndfile */
319 strcpy (layout, "g723_3");
321 case 26: /* 5-bit CCITT G.723.5 ADPCM 40kbps -> rtp/xine/modplug/libsndfile */
322 strcpy (layout, "g723_5");
325 case 8: /* Fragmented sample data */
326 case 9: /* AU_ENCODING_NESTED */
328 case 10: /* DSP program */
329 case 11: /* DSP 8-bit fixed point */
330 case 12: /* DSP 16-bit fixed point */
331 case 13: /* DSP 24-bit fixed point */
332 case 14: /* DSP 32-bit fixed point */
334 case 16: /* AU_ENCODING_DISPLAY : non-audio display data */
335 case 17: /* AU_ENCODING_MULAW_SQUELCH */
337 case 18: /* 16-bit linear with emphasis */
338 case 19: /* 16-bit linear compressed (NeXT) */
339 case 20: /* 16-bit linear with emphasis and compression */
341 case 21: /* Music kit DSP commands */
342 case 22: /* Music kit DSP commands samples */
350 gst_caps_new_simple ((law == 1) ? "audio/x-mulaw" : "audio/x-alaw",
351 "rate", G_TYPE_INT, auparse->samplerate,
352 "channels", G_TYPE_INT, auparse->channels, NULL);
353 auparse->sample_size = auparse->channels;
355 tempcaps = gst_caps_new_simple ("audio/x-raw-float",
356 "rate", G_TYPE_INT, auparse->samplerate,
357 "channels", G_TYPE_INT, auparse->channels,
358 "endianness", G_TYPE_INT, auparse->endianness,
359 "width", G_TYPE_INT, depth, NULL);
360 auparse->sample_size = auparse->channels * depth / 8;
361 } else if (layout[0]) {
362 tempcaps = gst_caps_new_simple ("audio/x-adpcm",
363 "layout", G_TYPE_STRING, layout, NULL);
364 auparse->sample_size = 0;
366 tempcaps = gst_caps_new_simple ("audio/x-raw-int",
367 "rate", G_TYPE_INT, auparse->samplerate,
368 "channels", G_TYPE_INT, auparse->channels,
369 "endianness", G_TYPE_INT, auparse->endianness,
370 "depth", G_TYPE_INT, depth, "width", G_TYPE_INT, depth,
371 /* FIXME: signed TRUE even for 8-bit PCM? */
372 "signed", G_TYPE_BOOLEAN, TRUE, NULL);
373 auparse->sample_size = auparse->channels * depth / 8;
376 GST_DEBUG_OBJECT (auparse, "sample_size=%d", auparse->sample_size);
378 if (!gst_au_parse_add_srcpad (auparse, tempcaps))
381 GST_DEBUG_OBJECT (auparse, "offset=%" G_GINT64_FORMAT, auparse->offset);
382 gst_adapter_flush (auparse->adapter, auparse->offset);
384 gst_caps_unref (tempcaps);
390 GST_ELEMENT_ERROR (auparse, STREAM, WRONG_TYPE, (NULL), (NULL));
391 return GST_FLOW_ERROR;
393 unsupported_sample_rate:
395 GST_ELEMENT_ERROR (auparse, STREAM, FORMAT, (NULL),
396 ("Unsupported samplerate: %u", auparse->samplerate));
397 return GST_FLOW_ERROR;
399 unsupported_number_of_channels:
401 GST_ELEMENT_ERROR (auparse, STREAM, FORMAT, (NULL),
402 ("Unsupported number of channels: %u", auparse->channels));
403 return GST_FLOW_ERROR;
407 GST_ELEMENT_ERROR (auparse, STREAM, FORMAT, (NULL),
408 ("Unsupported encoding: %u", auparse->encoding));
409 return GST_FLOW_ERROR;
413 GST_ELEMENT_ERROR (auparse, STREAM, FAILED, (NULL),
414 ("Failed to add srcpad"));
415 gst_caps_unref (tempcaps);
416 return GST_FLOW_ERROR;
420 #define AU_HEADER_SIZE 24
423 gst_au_parse_chain (GstPad * pad, GstBuffer * buf)
425 GstFlowReturn ret = GST_FLOW_OK;
427 gint avail, sendnow = 0;
429 auparse = GST_AU_PARSE (gst_pad_get_parent (pad));
431 GST_LOG_OBJECT (auparse, "got buffer of size %u", GST_BUFFER_SIZE (buf));
433 gst_adapter_push (auparse->adapter, buf);
436 /* if we haven't seen any data yet... */
437 if (auparse->srcpad == NULL) {
438 if (gst_adapter_available (auparse->adapter) < AU_HEADER_SIZE) {
439 GST_DEBUG_OBJECT (auparse, "need more data to parse header");
444 ret = gst_au_parse_parse_header (auparse);
445 if (ret != GST_FLOW_OK)
448 gst_pad_push_event (auparse->srcpad,
449 gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_DEFAULT,
450 0, GST_CLOCK_TIME_NONE, 0));
453 avail = gst_adapter_available (auparse->adapter);
455 if (auparse->sample_size > 0) {
456 /* Ensure we push a buffer that's a multiple of the frame size downstream */
457 sendnow = avail - (avail % auparse->sample_size);
459 /* It's something non-trivial (such as ADPCM), we don't understand it, so
460 * just push downstream and assume it will know what to do with it */
468 ret = gst_pad_alloc_buffer_and_set_caps (auparse->srcpad,
469 auparse->buffer_offset, sendnow, GST_PAD_CAPS (auparse->srcpad),
472 if (ret != GST_FLOW_OK) {
473 GST_DEBUG_OBJECT (auparse, "pad alloc flow: %s", gst_flow_get_name (ret));
477 data = gst_adapter_peek (auparse->adapter, sendnow);
478 memcpy (GST_BUFFER_DATA (outbuf), data, sendnow);
479 gst_adapter_flush (auparse->adapter, sendnow);
481 auparse->buffer_offset += sendnow;
483 ret = gst_pad_push (auparse->srcpad, outbuf);
488 gst_object_unref (auparse);
493 gst_au_parse_src_convert (GstAuParse * auparse, GstFormat src_format,
494 gint64 srcval, GstFormat dest_format, gint64 * destval)
497 guint samplesize, rate;
499 if (dest_format == src_format) {
504 GST_OBJECT_LOCK (auparse);
505 samplesize = auparse->sample_size;
506 rate = auparse->samplerate;
507 GST_OBJECT_UNLOCK (auparse);
509 if (samplesize == 0 || rate == 0) {
510 GST_LOG_OBJECT (auparse, "cannot convert, sample_size or rate unknown");
514 switch (src_format) {
515 case GST_FORMAT_BYTES:
516 srcval /= samplesize;
518 case GST_FORMAT_DEFAULT:{
519 switch (dest_format) {
520 case GST_FORMAT_BYTES:
521 *destval = srcval * samplesize;
523 case GST_FORMAT_TIME:
524 *destval = gst_util_uint64_scale_int (srcval, GST_SECOND, rate);
532 case GST_FORMAT_TIME:{
533 switch (dest_format) {
534 case GST_FORMAT_BYTES:
536 gst_util_uint64_scale_int (srcval, rate * samplesize, GST_SECOND);
538 case GST_FORMAT_DEFAULT:
539 *destval = gst_util_uint64_scale_int (srcval, rate, GST_SECOND);
554 GST_DEBUG_OBJECT (auparse, "could not convert from %s to %s format",
555 gst_format_get_name (src_format), gst_format_get_name (dest_format));
562 gst_au_parse_src_query (GstPad * pad, GstQuery * query)
565 gboolean ret = FALSE;
567 auparse = GST_AU_PARSE (gst_pad_get_parent (pad));
569 switch (GST_QUERY_TYPE (query)) {
570 case GST_QUERY_DURATION:{
571 GstFormat bformat = GST_FORMAT_BYTES;
575 gst_query_parse_duration (query, &format, NULL);
576 if (!gst_pad_query_peer_duration (auparse->sinkpad, &bformat, &len)) {
577 GST_DEBUG_OBJECT (auparse, "failed to query upstream length");
580 GST_OBJECT_LOCK (auparse);
581 len -= auparse->offset;
582 GST_OBJECT_UNLOCK (auparse);
584 ret = gst_au_parse_src_convert (auparse, GST_FORMAT_BYTES, len,
588 gst_query_set_duration (query, format, val);
592 case GST_QUERY_POSITION:{
593 GstFormat bformat = GST_FORMAT_BYTES;
597 gst_query_parse_position (query, &format, NULL);
598 if (!gst_pad_query_peer_position (auparse->sinkpad, &bformat, &pos)) {
599 GST_DEBUG_OBJECT (auparse, "failed to query upstream position");
602 GST_OBJECT_LOCK (auparse);
603 pos -= auparse->offset;
604 GST_OBJECT_UNLOCK (auparse);
606 ret = gst_au_parse_src_convert (auparse, GST_FORMAT_BYTES, pos,
610 gst_query_set_position (query, format, val);
615 ret = gst_pad_query_default (pad, query);
619 gst_object_unref (auparse);
624 gst_au_parse_handle_seek (GstAuParse * auparse, GstEvent * event)
626 GstSeekType start_type, stop_type;
632 gst_event_parse_seek (event, &rate, &format, &flags, &start_type, &start,
635 if (format != GST_FORMAT_TIME) {
636 GST_DEBUG_OBJECT (auparse, "only support seeks in TIME format");
640 /* FIXME: implement seeking */
645 gst_au_parse_sink_event (GstPad * pad, GstEvent * event)
650 auparse = GST_AU_PARSE (gst_pad_get_parent (pad));
652 switch (GST_EVENT_TYPE (event)) {
654 ret = gst_pad_event_default (pad, event);
658 gst_object_unref (auparse);
663 gst_au_parse_src_event (GstPad * pad, GstEvent * event)
668 auparse = GST_AU_PARSE (gst_pad_get_parent (pad));
670 switch (GST_EVENT_TYPE (event)) {
672 ret = gst_au_parse_handle_seek (auparse, event);
675 ret = gst_pad_event_default (pad, event);
679 gst_object_unref (auparse);
683 static GstStateChangeReturn
684 gst_au_parse_change_state (GstElement * element, GstStateChange transition)
686 GstAuParse *auparse = GST_AU_PARSE (element);
687 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
689 ret = parent_class->change_state (element, transition);
690 if (ret == GST_STATE_CHANGE_FAILURE)
693 switch (transition) {
694 case GST_STATE_CHANGE_PAUSED_TO_READY:
695 gst_au_parse_reset (auparse);
704 plugin_init (GstPlugin * plugin)
706 if (!gst_element_register (plugin, "auparse", GST_RANK_SECONDARY,
707 GST_TYPE_AU_PARSE)) {
714 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
717 "parses au streams", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,