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_RAW_PAD_TEMPLATE_CAPS \
48 "format= (string) { S8, S16LE, S16BE, S24LE, S24BE, " \
49 "S32LE, S32BE, F32LE, F32BE, " \
51 "rate = (int) [ 8000, 192000 ], " \
52 "channels = (int) [ 1, 2 ]"
54 #define GST_AU_PARSE_ALAW_PAD_TEMPLATE_CAPS \
56 "rate = (int) [ 8000, 192000 ], " \
57 "channels = (int) [ 1, 2 ]"
59 #define GST_AU_PARSE_MULAW_PAD_TEMPLATE_CAPS \
61 "rate = (int) [ 8000, 192000 ], " \
62 "channels = (int) [ 1, 2 ]"
64 /* Nothing to decode those ADPCM streams for now */
65 #define GST_AU_PARSE_ADPCM_PAD_TEMPLATE_CAPS \
67 "layout = (string) { g721, g722, g723_3, g723_5 }"
69 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
72 GST_STATIC_CAPS (GST_AU_PARSE_RAW_PAD_TEMPLATE_CAPS "; "
73 GST_AU_PARSE_ALAW_PAD_TEMPLATE_CAPS ";"
74 GST_AU_PARSE_MULAW_PAD_TEMPLATE_CAPS ";"
75 GST_AU_PARSE_ADPCM_PAD_TEMPLATE_CAPS));
78 static void gst_au_parse_dispose (GObject * object);
79 static GstFlowReturn gst_au_parse_chain (GstPad * pad, GstBuffer * buf);
80 static GstStateChangeReturn gst_au_parse_change_state (GstElement * element,
81 GstStateChange transition);
82 static void gst_au_parse_reset (GstAuParse * auparse);
83 static gboolean gst_au_parse_src_query (GstPad * pad, GstQuery * query);
84 static gboolean gst_au_parse_src_event (GstPad * pad, GstEvent * event);
85 static gboolean gst_au_parse_sink_event (GstPad * pad, GstEvent * event);
86 static gboolean gst_au_parse_src_convert (GstAuParse * auparse,
87 GstFormat src_format, gint64 srcval, GstFormat dest_format,
90 #define gst_au_parse_parent_class parent_class
91 G_DEFINE_TYPE (GstAuParse, gst_au_parse, GST_TYPE_ELEMENT);
94 gst_au_parse_class_init (GstAuParseClass * klass)
96 GObjectClass *gobject_class;
97 GstElementClass *gstelement_class;
99 GST_DEBUG_CATEGORY_INIT (auparse_debug, "auparse", 0, ".au parser");
101 gobject_class = (GObjectClass *) klass;
102 gstelement_class = (GstElementClass *) klass;
104 gobject_class->dispose = gst_au_parse_dispose;
106 gstelement_class->change_state =
107 GST_DEBUG_FUNCPTR (gst_au_parse_change_state);
108 gst_element_class_add_pad_template (gstelement_class,
109 gst_static_pad_template_get (&sink_template));
110 gst_element_class_add_pad_template (gstelement_class,
111 gst_static_pad_template_get (&src_template));
112 gst_element_class_set_details_simple (gstelement_class,
114 "Codec/Demuxer/Audio",
115 "Parse an .au file into raw audio",
116 "Erik Walthinsen <omega@cse.ogi.edu>");
120 gst_au_parse_init (GstAuParse * auparse)
122 auparse->sinkpad = gst_pad_new_from_static_template (&sink_template, "sink");
123 gst_pad_set_chain_function (auparse->sinkpad,
124 GST_DEBUG_FUNCPTR (gst_au_parse_chain));
125 gst_pad_set_event_function (auparse->sinkpad,
126 GST_DEBUG_FUNCPTR (gst_au_parse_sink_event));
127 gst_element_add_pad (GST_ELEMENT (auparse), auparse->sinkpad);
129 auparse->srcpad = gst_pad_new_from_static_template (&src_template, "src");
130 gst_pad_set_query_function (auparse->srcpad,
131 GST_DEBUG_FUNCPTR (gst_au_parse_src_query));
132 gst_pad_set_event_function (auparse->srcpad,
133 GST_DEBUG_FUNCPTR (gst_au_parse_src_event));
134 gst_pad_use_fixed_caps (auparse->srcpad);
135 gst_element_add_pad (GST_ELEMENT (auparse), auparse->srcpad);
137 auparse->adapter = gst_adapter_new ();
138 gst_au_parse_reset (auparse);
142 gst_au_parse_dispose (GObject * object)
144 GstAuParse *au = GST_AU_PARSE (object);
146 if (au->adapter != NULL) {
147 g_object_unref (au->adapter);
150 G_OBJECT_CLASS (parent_class)->dispose (object);
154 gst_au_parse_reset (GstAuParse * auparse)
157 auparse->buffer_offset = 0;
158 auparse->encoding = 0;
159 auparse->samplerate = 0;
160 auparse->channels = 0;
162 gst_adapter_clear (auparse->adapter);
164 /* gst_segment_init (&auparse->segment, GST_FORMAT_TIME); */
168 gst_au_parse_negotiate_srcpad (GstAuParse * auparse, GstCaps * new_caps)
170 if (auparse->src_caps && gst_caps_is_equal (new_caps, auparse->src_caps)) {
171 GST_LOG_OBJECT (auparse, "same caps, nothing to do");
175 gst_caps_replace (&auparse->src_caps, new_caps);
176 GST_DEBUG_OBJECT (auparse, "Changing src pad caps to %" GST_PTR_FORMAT,
178 gst_pad_set_caps (auparse->srcpad, auparse->src_caps);
184 gst_au_parse_parse_header (GstAuParse * auparse)
189 gchar layout[7] = { 0, };
190 GstAudioFormat format = GST_AUDIO_FORMAT_UNKNOWN;
194 head = (guint8 *) gst_adapter_map (auparse->adapter, 24);
195 g_assert (head != NULL);
197 GST_DEBUG_OBJECT (auparse, "[%c%c%c%c]", head[0], head[1], head[2], head[3]);
199 switch (GST_READ_UINT32_BE (head)) {
200 /* normal format is big endian (au is a Sparc format) */
201 case 0x2e736e64:{ /* ".snd" */
202 endianness = G_BIG_ENDIAN;
205 /* and of course, someone had to invent a little endian
206 * version. Used by DEC systems. */
207 case 0x646e732e: /* dns. */
208 case 0x0064732e:{ /* other source say it is "dns." */
209 endianness = G_LITTLE_ENDIAN;
217 auparse->offset = GST_READ_UINT32_BE (head + 4);
218 /* Do not trust size, could be set to -1 : unknown
219 * otherwise: filesize = size + auparse->offset
221 size = GST_READ_UINT32_BE (head + 8);
222 auparse->encoding = GST_READ_UINT32_BE (head + 12);
223 auparse->samplerate = GST_READ_UINT32_BE (head + 16);
224 auparse->channels = GST_READ_UINT32_BE (head + 20);
226 if (auparse->samplerate < 8000 || auparse->samplerate > 192000)
227 goto unsupported_sample_rate;
229 if (auparse->channels < 1 || auparse->channels > 2)
230 goto unsupported_number_of_channels;
232 GST_DEBUG_OBJECT (auparse, "offset %" G_GINT64_FORMAT ", size %u, "
233 "encoding %u, frequency %u, channels %u", auparse->offset, size,
234 auparse->encoding, auparse->samplerate, auparse->channels);
237 * http://www.opengroup.org/public/pubs/external/auformat.html
238 * http://astronomy.swin.edu.au/~pbourke/dataformats/au/
239 * Solaris headers : /usr/include/audio/au.h
240 * libsndfile : src/au.c
243 * http://www.tsp.ece.mcgill.ca/MMSP/Documents/AudioFormats/AU/Samples.html
246 switch (auparse->encoding) {
247 case 1: /* 8-bit ISDN mu-law G.711 */
250 case 27: /* 8-bit ISDN A-law G.711 */
254 case 2: /* 8-bit linear PCM, FIXME signed? */
255 format = GST_AUDIO_FORMAT_S8;
256 auparse->sample_size = auparse->channels;
258 case 3: /* 16-bit linear PCM */
259 if (endianness == G_LITTLE_ENDIAN)
260 format = GST_AUDIO_FORMAT_S16LE;
262 format = GST_AUDIO_FORMAT_S16BE;
263 auparse->sample_size = auparse->channels * 2;
265 case 4: /* 24-bit linear PCM */
266 if (endianness == G_LITTLE_ENDIAN)
267 format = GST_AUDIO_FORMAT_S24LE;
269 format = GST_AUDIO_FORMAT_S24BE;
270 auparse->sample_size = auparse->channels * 3;
272 case 5: /* 32-bit linear PCM */
273 if (endianness == G_LITTLE_ENDIAN)
274 format = GST_AUDIO_FORMAT_S32LE;
276 format = GST_AUDIO_FORMAT_S32BE;
277 auparse->sample_size = auparse->channels * 4;
280 case 6: /* 32-bit IEEE floating point */
281 if (endianness == G_LITTLE_ENDIAN)
282 format = GST_AUDIO_FORMAT_F32LE;
284 format = GST_AUDIO_FORMAT_F32BE;
285 auparse->sample_size = auparse->channels * 4;
287 case 7: /* 64-bit IEEE floating point */
288 if (endianness == G_LITTLE_ENDIAN)
289 format = GST_AUDIO_FORMAT_F64LE;
291 format = GST_AUDIO_FORMAT_F64BE;
292 auparse->sample_size = auparse->channels * 8;
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 */
333 gst_caps_new_simple ((law == 1) ? "audio/x-mulaw" : "audio/x-alaw",
334 "rate", G_TYPE_INT, auparse->samplerate,
335 "channels", G_TYPE_INT, auparse->channels, NULL);
336 auparse->sample_size = auparse->channels;
337 } else if (format != GST_AUDIO_FORMAT_UNKNOWN) {
338 tempcaps = gst_caps_new_simple ("audio/x-raw",
339 "format", G_TYPE_STRING, gst_audio_format_to_string (format),
340 "rate", G_TYPE_INT, auparse->samplerate,
341 "channels", G_TYPE_INT, auparse->channels, NULL);
342 } else if (layout[0]) {
343 tempcaps = gst_caps_new_simple ("audio/x-adpcm",
344 "layout", G_TYPE_STRING, layout, NULL);
345 auparse->sample_size = 0;
349 GST_DEBUG_OBJECT (auparse, "sample_size=%d", auparse->sample_size);
351 gst_au_parse_negotiate_srcpad (auparse, tempcaps);
353 GST_DEBUG_OBJECT (auparse, "offset=%" G_GINT64_FORMAT, auparse->offset);
354 gst_adapter_unmap (auparse->adapter);
355 gst_adapter_flush (auparse->adapter, auparse->offset);
357 gst_caps_unref (tempcaps);
363 gst_adapter_unmap (auparse->adapter);
364 GST_ELEMENT_ERROR (auparse, STREAM, WRONG_TYPE, (NULL), (NULL));
365 return GST_FLOW_ERROR;
367 unsupported_sample_rate:
369 gst_adapter_unmap (auparse->adapter);
370 GST_ELEMENT_ERROR (auparse, STREAM, FORMAT, (NULL),
371 ("Unsupported samplerate: %u", auparse->samplerate));
372 return GST_FLOW_ERROR;
374 unsupported_number_of_channels:
376 gst_adapter_unmap (auparse->adapter);
377 GST_ELEMENT_ERROR (auparse, STREAM, FORMAT, (NULL),
378 ("Unsupported number of channels: %u", auparse->channels));
379 return GST_FLOW_ERROR;
383 gst_adapter_unmap (auparse->adapter);
384 GST_ELEMENT_ERROR (auparse, STREAM, FORMAT, (NULL),
385 ("Unsupported encoding: %u", auparse->encoding));
386 return GST_FLOW_ERROR;
390 #define AU_HEADER_SIZE 24
393 gst_au_parse_chain (GstPad * pad, GstBuffer * buf)
395 GstFlowReturn ret = GST_FLOW_OK;
397 gint avail, sendnow = 0;
403 auparse = GST_AU_PARSE (gst_pad_get_parent (pad));
405 GST_LOG_OBJECT (auparse, "got buffer of size %u", gst_buffer_get_size (buf));
407 gst_adapter_push (auparse->adapter, buf);
410 /* if we haven't seen any data yet... */
411 if (!gst_pad_has_current_caps (auparse->srcpad)) {
412 if (gst_adapter_available (auparse->adapter) < AU_HEADER_SIZE) {
413 GST_DEBUG_OBJECT (auparse, "need more data to parse header");
418 ret = gst_au_parse_parse_header (auparse);
419 if (ret != GST_FLOW_OK)
422 gst_segment_init (&segment, GST_FORMAT_TIME);
423 gst_pad_push_event (auparse->srcpad, gst_event_new_segment (&segment));
426 avail = gst_adapter_available (auparse->adapter);
428 if (auparse->sample_size > 0) {
429 /* Ensure we push a buffer that's a multiple of the frame size downstream */
430 sendnow = avail - (avail % auparse->sample_size);
432 /* It's something non-trivial (such as ADPCM), we don't understand it, so
433 * just push downstream and assume it will know what to do with it */
441 outbuf = gst_adapter_take_buffer (auparse->adapter, sendnow);
442 outbuf = gst_buffer_make_writable (outbuf);
444 pos = auparse->buffer_offset - auparse->offset;
447 if (auparse->sample_size > 0 && auparse->samplerate > 0) {
448 gst_au_parse_src_convert (auparse, GST_FORMAT_BYTES, pos,
449 GST_FORMAT_DEFAULT, &offset);
450 gst_au_parse_src_convert (auparse, GST_FORMAT_BYTES, pos,
451 GST_FORMAT_TIME, ×tamp);
452 gst_au_parse_src_convert (auparse, GST_FORMAT_BYTES,
453 sendnow, GST_FORMAT_TIME, &duration);
455 GST_BUFFER_OFFSET (outbuf) = offset;
456 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
457 GST_BUFFER_DURATION (outbuf) = duration;
460 auparse->buffer_offset += sendnow;
462 ret = gst_pad_push (auparse->srcpad, outbuf);
467 gst_object_unref (auparse);
472 gst_au_parse_src_convert (GstAuParse * auparse, GstFormat src_format,
473 gint64 srcval, GstFormat dest_format, gint64 * destval)
476 guint samplesize, rate;
478 if (dest_format == src_format) {
483 GST_OBJECT_LOCK (auparse);
484 samplesize = auparse->sample_size;
485 rate = auparse->samplerate;
486 GST_OBJECT_UNLOCK (auparse);
488 if (samplesize == 0 || rate == 0) {
489 GST_LOG_OBJECT (auparse, "cannot convert, sample_size or rate unknown");
493 switch (src_format) {
494 case GST_FORMAT_BYTES:
495 srcval /= samplesize;
497 case GST_FORMAT_DEFAULT:{
498 switch (dest_format) {
499 case GST_FORMAT_DEFAULT:
502 case GST_FORMAT_BYTES:
503 *destval = srcval * samplesize;
505 case GST_FORMAT_TIME:
506 *destval = gst_util_uint64_scale_int (srcval, GST_SECOND, rate);
514 case GST_FORMAT_TIME:{
515 switch (dest_format) {
516 case GST_FORMAT_BYTES:
517 *destval = samplesize *
518 gst_util_uint64_scale_int (srcval, rate, GST_SECOND);
520 case GST_FORMAT_DEFAULT:
521 *destval = gst_util_uint64_scale_int (srcval, rate, GST_SECOND);
536 GST_DEBUG_OBJECT (auparse, "could not convert from %s to %s format",
537 gst_format_get_name (src_format), gst_format_get_name (dest_format));
544 gst_au_parse_src_query (GstPad * pad, GstQuery * query)
547 gboolean ret = FALSE;
549 auparse = GST_AU_PARSE (gst_pad_get_parent (pad));
551 switch (GST_QUERY_TYPE (query)) {
552 case GST_QUERY_DURATION:{
556 gst_query_parse_duration (query, &format, NULL);
557 if (!gst_pad_peer_query_duration (auparse->sinkpad, GST_FORMAT_BYTES,
559 GST_DEBUG_OBJECT (auparse, "failed to query upstream length");
562 GST_OBJECT_LOCK (auparse);
563 len -= auparse->offset;
564 GST_OBJECT_UNLOCK (auparse);
567 gst_au_parse_src_convert (auparse, GST_FORMAT_BYTES, len, format,
571 gst_query_set_duration (query, format, val);
575 case GST_QUERY_POSITION:{
579 gst_query_parse_position (query, &format, NULL);
580 if (!gst_pad_peer_query_position (auparse->sinkpad, GST_FORMAT_BYTES,
582 GST_DEBUG_OBJECT (auparse, "failed to query upstream position");
585 GST_OBJECT_LOCK (auparse);
586 pos -= auparse->offset;
587 GST_OBJECT_UNLOCK (auparse);
589 ret = gst_au_parse_src_convert (auparse, GST_FORMAT_BYTES, pos,
593 gst_query_set_position (query, format, val);
597 case GST_QUERY_SEEKING:{
600 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
601 /* FIXME: query duration in 'format'
602 gst_query_set_seeking (query, format, TRUE, 0, duration);
604 gst_query_set_seeking (query, format, TRUE, 0, GST_CLOCK_TIME_NONE);
609 ret = gst_pad_query_default (pad, query);
613 gst_object_unref (auparse);
618 gst_au_parse_handle_seek (GstAuParse * auparse, GstEvent * event)
620 GstSeekType start_type, stop_type;
627 gst_event_parse_seek (event, &rate, &format, &flags, &start_type, &start,
630 if (format != GST_FORMAT_TIME) {
631 GST_DEBUG_OBJECT (auparse, "only support seeks in TIME format");
635 res = gst_au_parse_src_convert (auparse, GST_FORMAT_TIME, start,
636 GST_FORMAT_BYTES, &start);
639 res = gst_au_parse_src_convert (auparse, GST_FORMAT_TIME, stop,
640 GST_FORMAT_BYTES, &stop);
643 GST_INFO_OBJECT (auparse,
644 "seeking: %" G_GINT64_FORMAT " ... %" G_GINT64_FORMAT, start, stop);
646 event = gst_event_new_seek (rate, GST_FORMAT_BYTES, flags, start_type, start,
648 res = gst_pad_push_event (auparse->sinkpad, event);
653 gst_au_parse_sink_event (GstPad * pad, GstEvent * event)
658 auparse = GST_AU_PARSE (gst_pad_get_parent (pad));
660 switch (GST_EVENT_TYPE (event)) {
663 /* discard, we'll come up with proper src caps */
664 gst_event_unref (event);
667 case GST_EVENT_SEGMENT:
669 gint64 start, stop, offset = 0;
671 GstEvent *new_event = NULL;
673 /* some debug output */
674 gst_event_copy_segment (event, &segment);
675 GST_DEBUG_OBJECT (auparse, "received newsegment %" GST_SEGMENT_FORMAT,
678 start = segment.start;
680 if (auparse->sample_size > 0) {
683 start -= auparse->offset;
684 start = MAX (start, 0);
687 stop -= auparse->offset;
688 stop = MAX (stop, 0);
690 gst_au_parse_src_convert (auparse, GST_FORMAT_BYTES, start,
691 GST_FORMAT_TIME, &start);
692 gst_au_parse_src_convert (auparse, GST_FORMAT_BYTES, stop,
693 GST_FORMAT_TIME, &stop);
696 GST_INFO_OBJECT (auparse,
697 "new segment: %" GST_TIME_FORMAT " ... %" GST_TIME_FORMAT,
698 GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
700 gst_segment_init (&segment, GST_FORMAT_TIME);
701 segment.start = segment.time = start;
703 new_event = gst_event_new_segment (&segment);
705 ret = gst_pad_push_event (auparse->srcpad, new_event);
707 auparse->buffer_offset = offset;
709 gst_event_unref (event);
713 if (!auparse->srcpad) {
714 GST_ELEMENT_ERROR (auparse, STREAM, WRONG_TYPE,
715 ("No valid input found before end of stream"), (NULL));
719 ret = gst_pad_event_default (pad, event);
723 gst_object_unref (auparse);
728 gst_au_parse_src_event (GstPad * pad, GstEvent * event)
733 auparse = GST_AU_PARSE (gst_pad_get_parent (pad));
735 switch (GST_EVENT_TYPE (event)) {
737 ret = gst_au_parse_handle_seek (auparse, event);
740 ret = gst_pad_event_default (pad, event);
744 gst_object_unref (auparse);
748 static GstStateChangeReturn
749 gst_au_parse_change_state (GstElement * element, GstStateChange transition)
751 GstAuParse *auparse = GST_AU_PARSE (element);
752 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
754 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
755 if (ret == GST_STATE_CHANGE_FAILURE)
758 switch (transition) {
759 case GST_STATE_CHANGE_PAUSED_TO_READY:
760 gst_au_parse_reset (auparse);
769 plugin_init (GstPlugin * plugin)
771 if (!gst_element_register (plugin, "auparse", GST_RANK_SECONDARY,
772 GST_TYPE_AU_PARSE)) {
779 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
782 "parses au streams", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,