Revert last change as we don't want plugins-good to depend on plugins-base CVS now.
[platform/upstream/gst-plugins-good.git] / gst / auparse / gstauparse.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  * Copyright (C) <2006> Tim-Philipp Müller <tim centricular net>
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 /**
22  * SECTION:element-auparse
23  * @short_description: .au file parser
24  * 
25  * <refsect2>
26  * <para>
27  * Parses .au files.
28  * </para>
29  * </refsect2>
30  */
31
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35
36 #include <stdlib.h>
37 #include <string.h>
38
39 #include "gstauparse.h"
40 #include <gst/audio/audio.h>
41
42 GST_DEBUG_CATEGORY_STATIC (auparse_debug);
43 #define GST_CAT_DEFAULT (auparse_debug)
44
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>");
50
51 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
52     GST_PAD_SINK,
53     GST_PAD_ALWAYS,
54     GST_STATIC_CAPS ("audio/x-au")
55     );
56
57 #define GST_AU_PARSE_ALAW_PAD_TEMPLATE_CAPS \
58     "audio/x-alaw, "                        \
59     "rate = (int) [ 8000, 192000 ], "       \
60     "channels = (int) [ 1, 2 ]"
61
62 #define GST_AU_PARSE_MULAW_PAD_TEMPLATE_CAPS \
63     "audio/x-mulaw, "                        \
64     "rate = (int) [ 8000, 192000 ], "        \
65     "channels = (int) [ 1, 2 ]"
66
67 /* Nothing to decode those ADPCM streams for now */
68 #define GST_AU_PARSE_ADPCM_PAD_TEMPLATE_CAPS \
69     "audio/x-adpcm, "                        \
70     "layout = (string) { g721, g722, g723_3, g723_5 }"
71
72 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
73     GST_PAD_SRC,
74     GST_PAD_SOMETIMES,
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));
80
81
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);
92
93 GST_BOILERPLATE (GstAuParse, gst_au_parse, GstElement, GST_TYPE_ELEMENT);
94
95 static void
96 gst_au_parse_base_init (gpointer g_class)
97 {
98   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
99
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);
105
106   GST_DEBUG_CATEGORY_INIT (auparse_debug, "auparse", 0, ".au parser");
107 }
108
109 static void
110 gst_au_parse_class_init (GstAuParseClass * klass)
111 {
112   GObjectClass *gobject_class;
113   GstElementClass *gstelement_class;
114
115   gobject_class = (GObjectClass *) klass;
116   gstelement_class = (GstElementClass *) klass;
117
118   gobject_class->dispose = gst_au_parse_dispose;
119
120   gstelement_class->change_state =
121       GST_DEBUG_FUNCPTR (gst_au_parse_change_state);
122 }
123
124 static void
125 gst_au_parse_init (GstAuParse * auparse, GstAuParseClass * klass)
126 {
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);
133
134   auparse->srcpad = NULL;
135   auparse->adapter = gst_adapter_new ();
136   gst_au_parse_reset (auparse);
137 }
138
139 static void
140 gst_au_parse_dispose (GObject * object)
141 {
142   GstAuParse *au = GST_AU_PARSE (object);
143
144   if (au->adapter != NULL) {
145     g_object_unref (au->adapter);
146     au->adapter = NULL;
147   }
148   G_OBJECT_CLASS (parent_class)->dispose (object);
149 }
150
151 static void
152 gst_au_parse_reset (GstAuParse * auparse)
153 {
154   gst_au_parse_remove_srcpad (auparse);
155
156   auparse->offset = 0;
157   auparse->buffer_offset = 0;
158   auparse->encoding = 0;
159   auparse->samplerate = 0;
160   auparse->channels = 0;
161   auparse->float_swap = 0;
162
163   gst_adapter_clear (auparse->adapter);
164
165   /* gst_segment_init (&auparse->segment, GST_FORMAT_TIME); */
166 }
167
168 static gboolean
169 gst_au_parse_add_srcpad (GstAuParse * auparse, GstCaps * new_caps)
170 {
171   GstPad *srcpad = NULL;
172
173   if (auparse->src_caps && gst_caps_is_equal (new_caps, auparse->src_caps)) {
174     GST_LOG_OBJECT (auparse, "same caps, nothing to do");
175     return TRUE;
176   }
177
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,
181         auparse->src_caps);
182     gst_pad_set_caps (auparse->srcpad, auparse->src_caps);
183   }
184
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);
189
190 #if 0
191     gst_pad_set_query_type_function (auparse->srcpad,
192         GST_DEBUG_FUNCPTR (gst_au_parse_src_get_query_types));
193 #endif
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));
198
199     gst_pad_use_fixed_caps (auparse->srcpad);
200     gst_pad_set_active (auparse->srcpad, TRUE);
201
202     if (auparse->src_caps)
203       gst_pad_set_caps (auparse->srcpad, auparse->src_caps);
204
205     GST_DEBUG_OBJECT (auparse, "Adding src pad with caps %" GST_PTR_FORMAT,
206         auparse->src_caps);
207
208     gst_object_ref (auparse->srcpad);
209     if (!gst_element_add_pad (GST_ELEMENT (auparse), auparse->srcpad))
210       return FALSE;
211     gst_element_no_more_pads (GST_ELEMENT (auparse));
212   }
213
214   return TRUE;
215 }
216
217 static gboolean
218 gst_au_parse_remove_srcpad (GstAuParse * auparse)
219 {
220   gboolean res = TRUE;
221
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;
228   }
229
230   return res;
231 }
232
233 static GstFlowReturn
234 gst_au_parse_parse_header (GstAuParse * auparse)
235 {
236   GstCaps *tempcaps;
237   guint32 size;
238   guint8 *head;
239   gchar layout[7] = { 0, };
240   gint law = 0, depth = 0, ieee = 0;
241
242   head = (guint8 *) gst_adapter_peek (auparse->adapter, 24);
243   g_assert (head != NULL);
244
245   GST_DEBUG_OBJECT (auparse, "[%c%c%c%c]", head[0], head[1], head[2], head[3]);
246
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;
251       break;
252     }
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;
258       break;
259     }
260     default:{
261       goto unknown_header;
262     }
263   }
264
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);
271
272   GST_DEBUG_OBJECT (auparse, "offset %" G_GINT64_FORMAT ", size %u, "
273       "encoding %u, frequency %u, channels %u", auparse->offset, size,
274       auparse->encoding, auparse->samplerate, auparse->channels);
275
276   /* Docs:
277    * http://www.opengroup.org/public/pubs/external/auformat.html
278    * http://astronomy.swin.edu.au/~pbourke/dataformats/au/
279    * Solaris headers : /usr/include/audio/au.h
280    * libsndfile : src/au.c
281    *
282    * Samples :
283    * http://www.tsp.ece.mcgill.ca/MMSP/Documents/AudioFormats/AU/Samples.html
284    */
285
286   auparse->float_swap = 0;
287
288   switch (auparse->encoding) {
289     case 1:                    /* 8-bit ISDN mu-law G.711 */
290       law = 1;
291       depth = 8;
292       break;
293     case 27:                   /* 8-bit ISDN  A-law G.711 */
294       law = 2;
295       depth = 8;
296       break;
297
298     case 2:                    /*  8-bit linear PCM */
299       depth = 8;
300       break;
301     case 3:                    /* 16-bit linear PCM */
302       depth = 16;
303       break;
304     case 4:                    /* 24-bit linear PCM */
305       depth = 24;
306       break;
307     case 5:                    /* 32-bit linear PCM */
308       depth = 32;
309       break;
310
311     case 6:                    /* 32-bit IEEE floating point */
312       ieee = 1;
313       depth = 32;
314       break;
315     case 7:                    /* 64-bit IEEE floating point */
316       ieee = 1;
317       depth = 64;
318       break;
319
320     case 23:                   /* 4-bit CCITT G.721   ADPCM 32kbps -> modplug/libsndfile (compressed 8-bit mu-law) */
321       strcpy (layout, "g721");
322       break;
323     case 24:                   /* 8-bit CCITT G.722   ADPCM        -> rtp */
324       strcpy (layout, "g722");
325       break;
326     case 25:                   /* 3-bit CCITT G.723.3 ADPCM 24kbps -> rtp/xine/modplug/libsndfile */
327       strcpy (layout, "g723_3");
328       break;
329     case 26:                   /* 5-bit CCITT G.723.5 ADPCM 40kbps -> rtp/xine/modplug/libsndfile */
330       strcpy (layout, "g723_5");
331       break;
332
333     case 8:                    /* Fragmented sample data */
334     case 9:                    /* AU_ENCODING_NESTED */
335
336     case 10:                   /* DSP program */
337     case 11:                   /* DSP  8-bit fixed point */
338     case 12:                   /* DSP 16-bit fixed point */
339     case 13:                   /* DSP 24-bit fixed point */
340     case 14:                   /* DSP 32-bit fixed point */
341
342     case 16:                   /* AU_ENCODING_DISPLAY : non-audio display data */
343     case 17:                   /* AU_ENCODING_MULAW_SQUELCH */
344
345     case 18:                   /* 16-bit linear with emphasis */
346     case 19:                   /* 16-bit linear compressed (NeXT) */
347     case 20:                   /* 16-bit linear with emphasis and compression */
348
349     case 21:                   /* Music kit DSP commands */
350     case 22:                   /* Music kit DSP commands samples */
351
352     default:
353       goto unknown_format;
354   }
355
356   if (law) {
357     tempcaps =
358         gst_caps_new_simple ((law == 1) ? "audio/x-mulaw" : "audio/x-alaw",
359         "rate", G_TYPE_INT, auparse->samplerate,
360         "channels", G_TYPE_INT, auparse->channels, NULL);
361     auparse->sample_size = auparse->channels;
362   } else if (ieee) {
363     tempcaps = gst_caps_new_simple ("audio/x-raw-float",
364         "rate", G_TYPE_INT, auparse->samplerate,
365         "channels", G_TYPE_INT, auparse->channels,
366         "endianness", G_TYPE_INT, G_BYTE_ORDER,
367         "width", G_TYPE_INT, depth, NULL);
368     auparse->sample_size = auparse->channels * depth / 8;
369     if (auparse->endianness != G_BYTE_ORDER) {
370       GST_DEBUG_OBJECT (auparse, "need to swap float byte order ourselves!");
371       auparse->float_swap = depth;
372     }
373   } else if (layout[0]) {
374     tempcaps = gst_caps_new_simple ("audio/x-adpcm",
375         "layout", G_TYPE_STRING, layout, NULL);
376     auparse->sample_size = 0;
377   } else {
378     tempcaps = gst_caps_new_simple ("audio/x-raw-int",
379         "rate", G_TYPE_INT, auparse->samplerate,
380         "channels", G_TYPE_INT, auparse->channels,
381         "endianness", G_TYPE_INT, auparse->endianness,
382         "depth", G_TYPE_INT, depth, "width", G_TYPE_INT, depth,
383         /* FIXME: signed TRUE even for 8-bit PCM? */
384         "signed", G_TYPE_BOOLEAN, TRUE, NULL);
385     auparse->sample_size = auparse->channels * depth / 8;
386   }
387
388   GST_DEBUG_OBJECT (auparse, "sample_size=%d", auparse->sample_size);
389
390   if (!gst_au_parse_add_srcpad (auparse, tempcaps))
391     goto add_pad_failed;
392
393   GST_DEBUG_OBJECT (auparse, "offset=%" G_GINT64_FORMAT, auparse->offset);
394   gst_adapter_flush (auparse->adapter, auparse->offset);
395
396   gst_caps_unref (tempcaps);
397   return GST_FLOW_OK;
398
399   /* ERRORS */
400 unknown_header:
401   {
402     GST_ELEMENT_ERROR (auparse, STREAM, WRONG_TYPE, (NULL), (NULL));
403     return GST_FLOW_ERROR;
404   }
405 unknown_format:
406   {
407     GST_ELEMENT_ERROR (auparse, STREAM, FORMAT, (NULL), (NULL));
408     return GST_FLOW_ERROR;
409   }
410 add_pad_failed:
411   {
412     GST_ELEMENT_ERROR (auparse, STREAM, FAILED, (NULL),
413         ("Failed to add srcpad"));
414     gst_caps_unref (tempcaps);
415     return GST_FLOW_ERROR;
416   }
417 }
418
419 #define AU_HEADER_SIZE 24
420
421 static GstFlowReturn
422 gst_au_parse_chain (GstPad * pad, GstBuffer * buf)
423 {
424   GstFlowReturn ret = GST_FLOW_OK;
425   GstAuParse *auparse;
426   gint avail, sendnow = 0;
427
428   auparse = GST_AU_PARSE (gst_pad_get_parent (pad));
429
430   GST_LOG_OBJECT (auparse, "got buffer of size %u", GST_BUFFER_SIZE (buf));
431
432   gst_adapter_push (auparse->adapter, buf);
433   buf = NULL;
434
435   /* if we haven't seen any data yet... */
436   if (auparse->srcpad == NULL) {
437     if (gst_adapter_available (auparse->adapter) < AU_HEADER_SIZE) {
438       GST_DEBUG_OBJECT (auparse, "need more data to parse header");
439       ret = GST_FLOW_OK;
440       goto out;
441     }
442
443     ret = gst_au_parse_parse_header (auparse);
444     if (ret != GST_FLOW_OK)
445       goto out;
446
447     gst_pad_push_event (auparse->srcpad,
448         gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_DEFAULT,
449             0, GST_CLOCK_TIME_NONE, 0));
450   }
451
452   avail = gst_adapter_available (auparse->adapter);
453
454   if (auparse->sample_size > 0) {
455     /* Ensure we push a buffer that's a multiple of the frame size downstream */
456     sendnow = avail - (avail % auparse->sample_size);
457   } else {
458     /* It's something non-trivial (such as ADPCM), we don't understand it, so
459      * just push downstream and assume it will know what to do with it */
460     sendnow = avail;
461   }
462
463   if (sendnow > 0) {
464     GstBuffer *outbuf;
465     const guint8 *data;
466
467     ret = gst_pad_alloc_buffer_and_set_caps (auparse->srcpad,
468         auparse->buffer_offset, sendnow, GST_PAD_CAPS (auparse->srcpad),
469         &outbuf);
470
471     if (ret != GST_FLOW_OK) {
472       GST_DEBUG_OBJECT (auparse, "pad alloc flow: %s", gst_flow_get_name (ret));
473       goto out;
474     }
475
476     data = gst_adapter_peek (auparse->adapter, sendnow);
477
478     /* audioconvert only handles floats in native endianness ... */
479     switch (auparse->float_swap) {
480       case 32:{
481         guint32 *indata = (guint32 *) data;
482         guint32 *outdata = (guint32 *) GST_BUFFER_DATA (outbuf);
483         gint i;
484
485         for (i = 0; i < (sendnow / sizeof (guint32)); ++i) {
486           outdata[i] = GUINT32_SWAP_LE_BE (indata[i]);
487         }
488         break;
489       }
490       case 64:{
491         guint64 *indata = (guint64 *) data;
492         guint64 *outdata = (guint64 *) GST_BUFFER_DATA (outbuf);
493         gint i;
494
495         for (i = 0; i < (sendnow / sizeof (guint64)); ++i) {
496           outdata[i] = GUINT64_SWAP_LE_BE (indata[i]);
497         }
498         break;
499       }
500       default:{
501         memcpy (GST_BUFFER_DATA (outbuf), data, sendnow);
502         break;
503       }
504     }
505
506     gst_adapter_flush (auparse->adapter, sendnow);
507
508     auparse->buffer_offset += sendnow;
509
510     ret = gst_pad_push (auparse->srcpad, outbuf);
511   }
512
513 out:
514
515   gst_object_unref (auparse);
516   return ret;
517 }
518
519 static gboolean
520 gst_au_parse_src_convert (GstAuParse * auparse, GstFormat src_format,
521     gint64 srcval, GstFormat dest_format, gint64 * destval)
522 {
523   gboolean ret = TRUE;
524   gint64 offset;
525   guint samplesize, rate;
526
527   if (dest_format == src_format) {
528     *destval = srcval;
529     return TRUE;
530   }
531
532   GST_OBJECT_LOCK (auparse);
533   samplesize = auparse->sample_size;
534   offset = auparse->offset;
535   rate = auparse->samplerate;
536   GST_OBJECT_UNLOCK (auparse);
537
538   if (samplesize == 0 || rate == 0) {
539     GST_LOG_OBJECT (auparse, "cannot convert, sample_size or rate unknown");
540     return FALSE;
541   }
542
543   switch (src_format) {
544     case GST_FORMAT_BYTES:
545       srcval /= samplesize;
546       /* fallthrough */
547     case GST_FORMAT_DEFAULT:{
548       switch (dest_format) {
549         case GST_FORMAT_BYTES:
550           *destval = srcval * samplesize;
551           break;
552         case GST_FORMAT_TIME:
553           *destval = gst_util_uint64_scale_int (srcval, GST_SECOND, rate);
554           break;
555         default:
556           ret = FALSE;
557           break;
558       }
559       break;
560     }
561     case GST_FORMAT_TIME:{
562       switch (dest_format) {
563         case GST_FORMAT_BYTES:
564           *destval =
565               gst_util_uint64_scale_int (srcval, rate * samplesize, GST_SECOND);
566           break;
567         case GST_FORMAT_DEFAULT:
568           *destval = gst_util_uint64_scale_int (srcval, rate, GST_SECOND);
569           break;
570         default:
571           ret = FALSE;
572           break;
573       }
574       break;
575     }
576     default:{
577       ret = FALSE;
578       break;
579     }
580   }
581
582   if (!ret) {
583     GST_DEBUG_OBJECT (auparse, "could not convert from %s to %s format",
584         gst_format_get_name (src_format), gst_format_get_name (dest_format));
585   }
586
587   return ret;
588 }
589
590 static gboolean
591 gst_au_parse_src_query (GstPad * pad, GstQuery * query)
592 {
593   GstAuParse *auparse;
594   gboolean ret = FALSE;
595
596   auparse = GST_AU_PARSE (gst_pad_get_parent (pad));
597
598   switch (GST_QUERY_TYPE (query)) {
599     case GST_QUERY_DURATION:{
600       GstFormat bformat = GST_FORMAT_BYTES;
601       GstFormat format;
602       gint64 len, val;
603
604       gst_query_parse_duration (query, &format, NULL);
605       if (!gst_pad_query_peer_duration (auparse->sinkpad, &bformat, &len)) {
606         GST_DEBUG_OBJECT (auparse, "failed to query upstream length");
607         break;
608       }
609       GST_OBJECT_LOCK (auparse);
610       len -= auparse->offset;
611       GST_OBJECT_UNLOCK (auparse);
612
613       ret = gst_au_parse_src_convert (auparse, GST_FORMAT_BYTES, len,
614           format, &val);
615
616       if (ret) {
617         gst_query_set_duration (query, format, val);
618       }
619       break;
620     }
621     case GST_QUERY_POSITION:{
622       GstFormat bformat = GST_FORMAT_BYTES;
623       GstFormat format;
624       gint64 pos, val;
625
626       gst_query_parse_position (query, &format, NULL);
627       if (!gst_pad_query_peer_position (auparse->sinkpad, &bformat, &pos)) {
628         GST_DEBUG_OBJECT (auparse, "failed to query upstream position");
629         break;
630       }
631       GST_OBJECT_LOCK (auparse);
632       pos -= auparse->offset;
633       GST_OBJECT_UNLOCK (auparse);
634
635       ret = gst_au_parse_src_convert (auparse, GST_FORMAT_BYTES, pos,
636           format, &val);
637
638       if (ret) {
639         gst_query_set_position (query, format, val);
640       }
641       break;
642     }
643     default:
644       ret = gst_pad_query_default (pad, query);
645       break;
646   }
647
648   gst_object_unref (auparse);
649   return ret;
650 }
651
652 static gboolean
653 gst_au_parse_handle_seek (GstAuParse * auparse, GstEvent * event)
654 {
655   GstSeekType start_type, stop_type;
656   GstSeekFlags flags;
657   GstFormat format;
658   gdouble rate;
659   gint64 start, stop;
660
661   gst_event_parse_seek (event, &rate, &format, &flags, &start_type, &start,
662       &stop_type, &stop);
663
664   if (format != GST_FORMAT_TIME) {
665     GST_DEBUG_OBJECT (auparse, "only support seeks in TIME format");
666     return FALSE;
667   }
668
669   /* FIXME: implement seeking */
670   return FALSE;
671 }
672
673 static gboolean
674 gst_au_parse_sink_event (GstPad * pad, GstEvent * event)
675 {
676   GstAuParse *auparse;
677   gboolean ret;
678
679   auparse = GST_AU_PARSE (gst_pad_get_parent (pad));
680
681   switch (GST_EVENT_TYPE (event)) {
682     default:
683       ret = gst_pad_event_default (pad, event);
684       break;
685   }
686
687   gst_object_unref (auparse);
688   return ret;
689 }
690
691 static gboolean
692 gst_au_parse_src_event (GstPad * pad, GstEvent * event)
693 {
694   GstAuParse *auparse;
695   gboolean ret;
696
697   auparse = GST_AU_PARSE (gst_pad_get_parent (pad));
698
699   switch (GST_EVENT_TYPE (event)) {
700     case GST_EVENT_SEEK:
701       ret = gst_au_parse_handle_seek (auparse, event);
702       break;
703     default:
704       ret = gst_pad_event_default (pad, event);
705       break;
706   }
707
708   gst_object_unref (auparse);
709   return ret;
710 }
711
712 static GstStateChangeReturn
713 gst_au_parse_change_state (GstElement * element, GstStateChange transition)
714 {
715   GstAuParse *auparse = GST_AU_PARSE (element);
716   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
717
718   ret = parent_class->change_state (element, transition);
719   if (ret == GST_STATE_CHANGE_FAILURE)
720     return ret;
721
722   switch (transition) {
723     case GST_STATE_CHANGE_PAUSED_TO_READY:
724       gst_au_parse_reset (auparse);
725     default:
726       break;
727   }
728
729   return ret;
730 }
731
732 static gboolean
733 plugin_init (GstPlugin * plugin)
734 {
735   if (!gst_element_register (plugin, "auparse", GST_RANK_SECONDARY,
736           GST_TYPE_AU_PARSE)) {
737     return FALSE;
738   }
739
740   return TRUE;
741 }
742
743 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
744     GST_VERSION_MINOR,
745     "auparse",
746     "parses au streams", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,
747     GST_PACKAGE_ORIGIN)