tizen 2.0 init
[framework/multimedia/gst-plugins-good0.10.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  *
24  * Parses .au files mostly originating from sun os based computers.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include "gstauparse.h"
35 #include <gst/audio/audio.h>
36
37 GST_DEBUG_CATEGORY_STATIC (auparse_debug);
38 #define GST_CAT_DEFAULT (auparse_debug)
39
40 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
41     GST_PAD_SINK,
42     GST_PAD_ALWAYS,
43     GST_STATIC_CAPS ("audio/x-au")
44     );
45
46 #define GST_AU_PARSE_ALAW_PAD_TEMPLATE_CAPS \
47     "audio/x-alaw, "                        \
48     "rate = (int) [ 8000, 192000 ], "       \
49     "channels = (int) [ 1, 2 ]"
50
51 #define GST_AU_PARSE_MULAW_PAD_TEMPLATE_CAPS \
52     "audio/x-mulaw, "                        \
53     "rate = (int) [ 8000, 192000 ], "        \
54     "channels = (int) [ 1, 2 ]"
55
56 /* Nothing to decode those ADPCM streams for now */
57 #define GST_AU_PARSE_ADPCM_PAD_TEMPLATE_CAPS \
58     "audio/x-adpcm, "                        \
59     "layout = (string) { g721, g722, g723_3, g723_5 }"
60
61 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
62     GST_PAD_SRC,
63     GST_PAD_SOMETIMES,
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));
69
70
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);
81 static gboolean gst_au_parse_src_convert (GstAuParse * auparse,
82     GstFormat src_format, gint64 srcval, GstFormat dest_format,
83     gint64 * destval);
84
85 GST_BOILERPLATE (GstAuParse, gst_au_parse, GstElement, GST_TYPE_ELEMENT);
86
87 static void
88 gst_au_parse_base_init (gpointer g_class)
89 {
90   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
91
92   gst_element_class_add_static_pad_template (element_class,
93       &sink_template);
94   gst_element_class_add_static_pad_template (element_class, &src_template);
95   gst_element_class_set_details_simple (element_class, "AU audio demuxer",
96       "Codec/Demuxer/Audio",
97       "Parse an .au file into raw audio",
98       "Erik Walthinsen <omega@cse.ogi.edu>");
99
100   GST_DEBUG_CATEGORY_INIT (auparse_debug, "auparse", 0, ".au parser");
101 }
102
103 static void
104 gst_au_parse_class_init (GstAuParseClass * klass)
105 {
106   GObjectClass *gobject_class;
107   GstElementClass *gstelement_class;
108
109   gobject_class = (GObjectClass *) klass;
110   gstelement_class = (GstElementClass *) klass;
111
112   gobject_class->dispose = gst_au_parse_dispose;
113
114   gstelement_class->change_state =
115       GST_DEBUG_FUNCPTR (gst_au_parse_change_state);
116 }
117
118 static void
119 gst_au_parse_init (GstAuParse * auparse, GstAuParseClass * klass)
120 {
121   auparse->sinkpad = gst_pad_new_from_static_template (&sink_template, "sink");
122   gst_pad_set_chain_function (auparse->sinkpad,
123       GST_DEBUG_FUNCPTR (gst_au_parse_chain));
124   gst_pad_set_event_function (auparse->sinkpad,
125       GST_DEBUG_FUNCPTR (gst_au_parse_sink_event));
126   gst_element_add_pad (GST_ELEMENT (auparse), auparse->sinkpad);
127
128   auparse->srcpad = NULL;
129   auparse->adapter = gst_adapter_new ();
130   gst_au_parse_reset (auparse);
131 }
132
133 static void
134 gst_au_parse_dispose (GObject * object)
135 {
136   GstAuParse *au = GST_AU_PARSE (object);
137
138   if (au->adapter != NULL) {
139     g_object_unref (au->adapter);
140     au->adapter = NULL;
141   }
142   G_OBJECT_CLASS (parent_class)->dispose (object);
143 }
144
145 static void
146 gst_au_parse_reset (GstAuParse * auparse)
147 {
148   gst_au_parse_remove_srcpad (auparse);
149
150   auparse->offset = 0;
151   auparse->buffer_offset = 0;
152   auparse->encoding = 0;
153   auparse->samplerate = 0;
154   auparse->channels = 0;
155
156   gst_adapter_clear (auparse->adapter);
157
158   /* gst_segment_init (&auparse->segment, GST_FORMAT_TIME); */
159 }
160
161 static gboolean
162 gst_au_parse_add_srcpad (GstAuParse * auparse, GstCaps * new_caps)
163 {
164   if (auparse->src_caps && gst_caps_is_equal (new_caps, auparse->src_caps)) {
165     GST_LOG_OBJECT (auparse, "same caps, nothing to do");
166     return TRUE;
167   }
168
169   gst_caps_replace (&auparse->src_caps, new_caps);
170   if (auparse->srcpad != NULL) {
171     GST_DEBUG_OBJECT (auparse, "Changing src pad caps to %" GST_PTR_FORMAT,
172         auparse->src_caps);
173     gst_pad_set_caps (auparse->srcpad, auparse->src_caps);
174   }
175
176   if (auparse->srcpad == NULL) {
177     auparse->srcpad = gst_pad_new_from_static_template (&src_template, "src");
178     g_return_val_if_fail (auparse->srcpad != NULL, FALSE);
179
180 #if 0
181     gst_pad_set_query_type_function (auparse->srcpad,
182         GST_DEBUG_FUNCPTR (gst_au_parse_src_get_query_types));
183 #endif
184     gst_pad_set_query_function (auparse->srcpad,
185         GST_DEBUG_FUNCPTR (gst_au_parse_src_query));
186     gst_pad_set_event_function (auparse->srcpad,
187         GST_DEBUG_FUNCPTR (gst_au_parse_src_event));
188
189     gst_pad_use_fixed_caps (auparse->srcpad);
190     gst_pad_set_active (auparse->srcpad, TRUE);
191
192     if (auparse->src_caps)
193       gst_pad_set_caps (auparse->srcpad, auparse->src_caps);
194
195     GST_DEBUG_OBJECT (auparse, "Adding src pad with caps %" GST_PTR_FORMAT,
196         auparse->src_caps);
197
198     gst_object_ref (auparse->srcpad);
199     if (!gst_element_add_pad (GST_ELEMENT (auparse), auparse->srcpad))
200       return FALSE;
201     gst_element_no_more_pads (GST_ELEMENT (auparse));
202   }
203
204   return TRUE;
205 }
206
207 static gboolean
208 gst_au_parse_remove_srcpad (GstAuParse * auparse)
209 {
210   gboolean res = TRUE;
211
212   if (auparse->srcpad != NULL) {
213     GST_DEBUG_OBJECT (auparse, "Removing src pad");
214     res = gst_element_remove_pad (GST_ELEMENT (auparse), auparse->srcpad);
215     g_return_val_if_fail (res != FALSE, FALSE);
216     gst_object_unref (auparse->srcpad);
217     auparse->srcpad = NULL;
218   }
219
220   return res;
221 }
222
223 static GstFlowReturn
224 gst_au_parse_parse_header (GstAuParse * auparse)
225 {
226   GstCaps *tempcaps;
227   guint32 size;
228   guint8 *head;
229   gchar layout[7] = { 0, };
230   gint law = 0, depth = 0, ieee = 0;
231
232   head = (guint8 *) gst_adapter_peek (auparse->adapter, 24);
233   g_assert (head != NULL);
234
235   GST_DEBUG_OBJECT (auparse, "[%c%c%c%c]", head[0], head[1], head[2], head[3]);
236
237   switch (GST_READ_UINT32_BE (head)) {
238       /* normal format is big endian (au is a Sparc format) */
239     case 0x2e736e64:{          /* ".snd" */
240       auparse->endianness = G_BIG_ENDIAN;
241       break;
242     }
243       /* and of course, someone had to invent a little endian
244        * version.  Used by DEC systems. */
245     case 0x646e732e:           /* dns.                          */
246     case 0x0064732e:{          /* other source say it is "dns." */
247       auparse->endianness = G_LITTLE_ENDIAN;
248       break;
249     }
250     default:{
251       goto unknown_header;
252     }
253   }
254
255   auparse->offset = GST_READ_UINT32_BE (head + 4);
256   /* Do not trust size, could be set to -1 : unknown
257    * otherwise: filesize = size + auparse->offset
258    */
259   size = GST_READ_UINT32_BE (head + 8);
260   auparse->encoding = GST_READ_UINT32_BE (head + 12);
261   auparse->samplerate = GST_READ_UINT32_BE (head + 16);
262   auparse->channels = GST_READ_UINT32_BE (head + 20);
263
264   if (auparse->samplerate < 8000 || auparse->samplerate > 192000)
265     goto unsupported_sample_rate;
266
267   if (auparse->channels < 1 || auparse->channels > 2)
268     goto unsupported_number_of_channels;
269
270   GST_DEBUG_OBJECT (auparse, "offset %" G_GINT64_FORMAT ", size %u, "
271       "encoding %u, frequency %u, channels %u", auparse->offset, size,
272       auparse->encoding, auparse->samplerate, auparse->channels);
273
274   /* Docs:
275    * http://www.opengroup.org/public/pubs/external/auformat.html
276    * http://astronomy.swin.edu.au/~pbourke/dataformats/au/
277    * Solaris headers : /usr/include/audio/au.h
278    * libsndfile : src/au.c
279    *
280    * Samples :
281    * http://www.tsp.ece.mcgill.ca/MMSP/Documents/AudioFormats/AU/Samples.html
282    */
283
284   switch (auparse->encoding) {
285     case 1:                    /* 8-bit ISDN mu-law G.711 */
286       law = 1;
287       depth = 8;
288       break;
289     case 27:                   /* 8-bit ISDN  A-law G.711 */
290       law = 2;
291       depth = 8;
292       break;
293
294     case 2:                    /*  8-bit linear PCM */
295       depth = 8;
296       break;
297     case 3:                    /* 16-bit linear PCM */
298       depth = 16;
299       break;
300     case 4:                    /* 24-bit linear PCM */
301       depth = 24;
302       break;
303     case 5:                    /* 32-bit linear PCM */
304       depth = 32;
305       break;
306
307     case 6:                    /* 32-bit IEEE floating point */
308       ieee = 1;
309       depth = 32;
310       break;
311     case 7:                    /* 64-bit IEEE floating point */
312       ieee = 1;
313       depth = 64;
314       break;
315
316     case 23:                   /* 4-bit CCITT G.721   ADPCM 32kbps -> modplug/libsndfile (compressed 8-bit mu-law) */
317       strcpy (layout, "g721");
318       break;
319     case 24:                   /* 8-bit CCITT G.722   ADPCM        -> rtp */
320       strcpy (layout, "g722");
321       break;
322     case 25:                   /* 3-bit CCITT G.723.3 ADPCM 24kbps -> rtp/xine/modplug/libsndfile */
323       strcpy (layout, "g723_3");
324       break;
325     case 26:                   /* 5-bit CCITT G.723.5 ADPCM 40kbps -> rtp/xine/modplug/libsndfile */
326       strcpy (layout, "g723_5");
327       break;
328
329     case 8:                    /* Fragmented sample data */
330     case 9:                    /* AU_ENCODING_NESTED */
331
332     case 10:                   /* DSP program */
333     case 11:                   /* DSP  8-bit fixed point */
334     case 12:                   /* DSP 16-bit fixed point */
335     case 13:                   /* DSP 24-bit fixed point */
336     case 14:                   /* DSP 32-bit fixed point */
337
338     case 16:                   /* AU_ENCODING_DISPLAY : non-audio display data */
339     case 17:                   /* AU_ENCODING_MULAW_SQUELCH */
340
341     case 18:                   /* 16-bit linear with emphasis */
342     case 19:                   /* 16-bit linear compressed (NeXT) */
343     case 20:                   /* 16-bit linear with emphasis and compression */
344
345     case 21:                   /* Music kit DSP commands */
346     case 22:                   /* Music kit DSP commands samples */
347
348     default:
349       goto unknown_format;
350   }
351
352   if (law) {
353     tempcaps =
354         gst_caps_new_simple ((law == 1) ? "audio/x-mulaw" : "audio/x-alaw",
355         "rate", G_TYPE_INT, auparse->samplerate,
356         "channels", G_TYPE_INT, auparse->channels, NULL);
357     auparse->sample_size = auparse->channels;
358   } else if (ieee) {
359     tempcaps = gst_caps_new_simple ("audio/x-raw-float",
360         "rate", G_TYPE_INT, auparse->samplerate,
361         "channels", G_TYPE_INT, auparse->channels,
362         "endianness", G_TYPE_INT, auparse->endianness,
363         "width", G_TYPE_INT, depth, NULL);
364     auparse->sample_size = auparse->channels * depth / 8;
365   } else if (layout[0]) {
366     tempcaps = gst_caps_new_simple ("audio/x-adpcm",
367         "layout", G_TYPE_STRING, layout, NULL);
368     auparse->sample_size = 0;
369   } else {
370     tempcaps = gst_caps_new_simple ("audio/x-raw-int",
371         "rate", G_TYPE_INT, auparse->samplerate,
372         "channels", G_TYPE_INT, auparse->channels,
373         "endianness", G_TYPE_INT, auparse->endianness,
374         "depth", G_TYPE_INT, depth, "width", G_TYPE_INT, depth,
375         /* FIXME: signed TRUE even for 8-bit PCM? */
376         "signed", G_TYPE_BOOLEAN, TRUE, NULL);
377     auparse->sample_size = auparse->channels * depth / 8;
378   }
379
380   GST_DEBUG_OBJECT (auparse, "sample_size=%d", auparse->sample_size);
381
382   if (!gst_au_parse_add_srcpad (auparse, tempcaps))
383     goto add_pad_failed;
384
385   GST_DEBUG_OBJECT (auparse, "offset=%" G_GINT64_FORMAT, auparse->offset);
386   gst_adapter_flush (auparse->adapter, auparse->offset);
387
388   gst_caps_unref (tempcaps);
389   return GST_FLOW_OK;
390
391   /* ERRORS */
392 unknown_header:
393   {
394     GST_ELEMENT_ERROR (auparse, STREAM, WRONG_TYPE, (NULL), (NULL));
395     return GST_FLOW_ERROR;
396   }
397 unsupported_sample_rate:
398   {
399     GST_ELEMENT_ERROR (auparse, STREAM, FORMAT, (NULL),
400         ("Unsupported samplerate: %u", auparse->samplerate));
401     return GST_FLOW_ERROR;
402   }
403 unsupported_number_of_channels:
404   {
405     GST_ELEMENT_ERROR (auparse, STREAM, FORMAT, (NULL),
406         ("Unsupported number of channels: %u", auparse->channels));
407     return GST_FLOW_ERROR;
408   }
409 unknown_format:
410   {
411     GST_ELEMENT_ERROR (auparse, STREAM, FORMAT, (NULL),
412         ("Unsupported encoding: %u", auparse->encoding));
413     return GST_FLOW_ERROR;
414   }
415 add_pad_failed:
416   {
417     GST_ELEMENT_ERROR (auparse, STREAM, FAILED, (NULL),
418         ("Failed to add srcpad"));
419     gst_caps_unref (tempcaps);
420     return GST_FLOW_ERROR;
421   }
422 }
423
424 #define AU_HEADER_SIZE 24
425
426 static GstFlowReturn
427 gst_au_parse_chain (GstPad * pad, GstBuffer * buf)
428 {
429   GstFlowReturn ret = GST_FLOW_OK;
430   GstAuParse *auparse;
431   gint avail, sendnow = 0;
432   gint64 timestamp;
433   gint64 duration;
434   gint64 offset;
435
436   auparse = GST_AU_PARSE (gst_pad_get_parent (pad));
437
438   GST_LOG_OBJECT (auparse, "got buffer of size %u", GST_BUFFER_SIZE (buf));
439
440   gst_adapter_push (auparse->adapter, buf);
441   buf = NULL;
442
443   /* if we haven't seen any data yet... */
444   if (auparse->srcpad == NULL) {
445     if (gst_adapter_available (auparse->adapter) < AU_HEADER_SIZE) {
446       GST_DEBUG_OBJECT (auparse, "need more data to parse header");
447       ret = GST_FLOW_OK;
448       goto out;
449     }
450
451     ret = gst_au_parse_parse_header (auparse);
452     if (ret != GST_FLOW_OK)
453       goto out;
454
455     gst_pad_push_event (auparse->srcpad,
456         gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_TIME,
457             0, GST_CLOCK_TIME_NONE, 0));
458   }
459
460   avail = gst_adapter_available (auparse->adapter);
461
462   if (auparse->sample_size > 0) {
463     /* Ensure we push a buffer that's a multiple of the frame size downstream */
464     sendnow = avail - (avail % auparse->sample_size);
465   } else {
466     /* It's something non-trivial (such as ADPCM), we don't understand it, so
467      * just push downstream and assume it will know what to do with it */
468     sendnow = avail;
469   }
470
471   if (sendnow > 0) {
472     GstBuffer *outbuf;
473     const guint8 *data;
474     gint64 pos;
475
476     ret = gst_pad_alloc_buffer_and_set_caps (auparse->srcpad,
477         auparse->buffer_offset, sendnow, GST_PAD_CAPS (auparse->srcpad),
478         &outbuf);
479
480     if (ret != GST_FLOW_OK) {
481       GST_DEBUG_OBJECT (auparse, "pad alloc flow: %s", gst_flow_get_name (ret));
482       goto out;
483     }
484
485     data = gst_adapter_peek (auparse->adapter, sendnow);
486     memcpy (GST_BUFFER_DATA (outbuf), data, sendnow);
487     gst_adapter_flush (auparse->adapter, sendnow);
488
489     pos = auparse->buffer_offset - auparse->offset;
490     pos = MAX (pos, 0);
491
492     if (auparse->sample_size > 0 && auparse->samplerate > 0) {
493       gst_au_parse_src_convert (auparse, GST_FORMAT_BYTES, pos,
494           GST_FORMAT_DEFAULT, &offset);
495       gst_au_parse_src_convert (auparse, GST_FORMAT_BYTES, pos,
496           GST_FORMAT_TIME, &timestamp);
497       gst_au_parse_src_convert (auparse, GST_FORMAT_BYTES,
498           sendnow, GST_FORMAT_TIME, &duration);
499
500       GST_BUFFER_OFFSET (outbuf) = offset;
501       GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
502       GST_BUFFER_DURATION (outbuf) = duration;
503     }
504
505     auparse->buffer_offset += sendnow;
506
507     ret = gst_pad_push (auparse->srcpad, outbuf);
508   }
509
510 out:
511
512   gst_object_unref (auparse);
513   return ret;
514 }
515
516 static gboolean
517 gst_au_parse_src_convert (GstAuParse * auparse, GstFormat src_format,
518     gint64 srcval, GstFormat dest_format, gint64 * destval)
519 {
520   gboolean ret = TRUE;
521   guint samplesize, rate;
522
523   if (dest_format == src_format) {
524     *destval = srcval;
525     return TRUE;
526   }
527
528   GST_OBJECT_LOCK (auparse);
529   samplesize = auparse->sample_size;
530   rate = auparse->samplerate;
531   GST_OBJECT_UNLOCK (auparse);
532
533   if (samplesize == 0 || rate == 0) {
534     GST_LOG_OBJECT (auparse, "cannot convert, sample_size or rate unknown");
535     return FALSE;
536   }
537
538   switch (src_format) {
539     case GST_FORMAT_BYTES:
540       srcval /= samplesize;
541       /* fallthrough */
542     case GST_FORMAT_DEFAULT:{
543       switch (dest_format) {
544         case GST_FORMAT_DEFAULT:
545           *destval = srcval;
546           break;
547         case GST_FORMAT_BYTES:
548           *destval = srcval * samplesize;
549           break;
550         case GST_FORMAT_TIME:
551           *destval = gst_util_uint64_scale_int (srcval, GST_SECOND, rate);
552           break;
553         default:
554           ret = FALSE;
555           break;
556       }
557       break;
558     }
559     case GST_FORMAT_TIME:{
560       switch (dest_format) {
561         case GST_FORMAT_BYTES:
562           *destval = samplesize *
563               gst_util_uint64_scale_int (srcval, rate, GST_SECOND);
564           break;
565         case GST_FORMAT_DEFAULT:
566           *destval = gst_util_uint64_scale_int (srcval, rate, GST_SECOND);
567           break;
568         default:
569           ret = FALSE;
570           break;
571       }
572       break;
573     }
574     default:{
575       ret = FALSE;
576       break;
577     }
578   }
579
580   if (!ret) {
581     GST_DEBUG_OBJECT (auparse, "could not convert from %s to %s format",
582         gst_format_get_name (src_format), gst_format_get_name (dest_format));
583   }
584
585   return ret;
586 }
587
588 static gboolean
589 gst_au_parse_src_query (GstPad * pad, GstQuery * query)
590 {
591   GstAuParse *auparse;
592   gboolean ret = FALSE;
593
594   auparse = GST_AU_PARSE (gst_pad_get_parent (pad));
595
596   switch (GST_QUERY_TYPE (query)) {
597     case GST_QUERY_DURATION:{
598       GstFormat bformat = GST_FORMAT_BYTES;
599       GstFormat format;
600       gint64 len, val;
601
602       gst_query_parse_duration (query, &format, NULL);
603       if (!gst_pad_query_peer_duration (auparse->sinkpad, &bformat, &len)) {
604         GST_DEBUG_OBJECT (auparse, "failed to query upstream length");
605         break;
606       }
607       GST_OBJECT_LOCK (auparse);
608       len -= auparse->offset;
609       GST_OBJECT_UNLOCK (auparse);
610
611       ret = gst_au_parse_src_convert (auparse, bformat, len, format, &val);
612
613       if (ret) {
614         gst_query_set_duration (query, format, val);
615       }
616       break;
617     }
618     case GST_QUERY_POSITION:{
619       GstFormat bformat = GST_FORMAT_BYTES;
620       GstFormat format;
621       gint64 pos, val;
622
623       gst_query_parse_position (query, &format, NULL);
624       if (!gst_pad_query_peer_position (auparse->sinkpad, &bformat, &pos)) {
625         GST_DEBUG_OBJECT (auparse, "failed to query upstream position");
626         break;
627       }
628       GST_OBJECT_LOCK (auparse);
629       pos -= auparse->offset;
630       GST_OBJECT_UNLOCK (auparse);
631
632       ret = gst_au_parse_src_convert (auparse, GST_FORMAT_BYTES, pos,
633           format, &val);
634
635       if (ret) {
636         gst_query_set_position (query, format, val);
637       }
638       break;
639     }
640     case GST_QUERY_SEEKING:{
641       GstFormat format;
642
643       gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
644       /* FIXME: query duration in 'format'
645          gst_query_set_seeking (query, format, TRUE, 0, duration);
646        */
647       gst_query_set_seeking (query, format, TRUE, 0, GST_CLOCK_TIME_NONE);
648       ret = TRUE;
649       break;
650     }
651     default:
652       ret = gst_pad_query_default (pad, query);
653       break;
654   }
655
656   gst_object_unref (auparse);
657   return ret;
658 }
659
660 static gboolean
661 gst_au_parse_handle_seek (GstAuParse * auparse, GstEvent * event)
662 {
663   GstSeekType start_type, stop_type;
664   GstSeekFlags flags;
665   GstFormat format;
666   gdouble rate;
667   gint64 start, stop;
668   gboolean res;
669
670   gst_event_parse_seek (event, &rate, &format, &flags, &start_type, &start,
671       &stop_type, &stop);
672
673   if (format != GST_FORMAT_TIME) {
674     GST_DEBUG_OBJECT (auparse, "only support seeks in TIME format");
675     return FALSE;
676   }
677
678   res = gst_au_parse_src_convert (auparse, GST_FORMAT_TIME, start,
679       GST_FORMAT_BYTES, &start);
680
681   if (stop > 0) {
682     res = gst_au_parse_src_convert (auparse, GST_FORMAT_TIME, stop,
683         GST_FORMAT_BYTES, &stop);
684   }
685
686   GST_INFO_OBJECT (auparse,
687       "seeking: %" G_GINT64_FORMAT " ... %" G_GINT64_FORMAT, start, stop);
688
689   event = gst_event_new_seek (rate, GST_FORMAT_BYTES, flags, start_type, start,
690       stop_type, stop);
691   res = gst_pad_push_event (auparse->sinkpad, event);
692   return res;
693 }
694
695 static gboolean
696 gst_au_parse_sink_event (GstPad * pad, GstEvent * event)
697 {
698   GstAuParse *auparse;
699   gboolean ret = TRUE;
700
701   auparse = GST_AU_PARSE (gst_pad_get_parent (pad));
702
703   switch (GST_EVENT_TYPE (event)) {
704     case GST_EVENT_NEWSEGMENT:
705     {
706       GstFormat format;
707       gdouble rate, arate;
708       gint64 start, stop, time, offset = 0;
709       gboolean update;
710       GstSegment segment;
711       GstEvent *new_event = NULL;
712
713       gst_segment_init (&segment, GST_FORMAT_UNDEFINED);
714       gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
715           &start, &stop, &time);
716       gst_segment_set_newsegment_full (&segment, update, rate, arate, format,
717           start, stop, time);
718
719       if (auparse->sample_size > 0) {
720         if (start > 0) {
721           offset = start;
722           start -= auparse->offset;
723           start = MAX (start, 0);
724         }
725         if (stop > 0) {
726           stop -= auparse->offset;
727           stop = MAX (stop, 0);
728         }
729         gst_au_parse_src_convert (auparse, GST_FORMAT_BYTES, start,
730             GST_FORMAT_TIME, &start);
731         gst_au_parse_src_convert (auparse, GST_FORMAT_BYTES, stop,
732             GST_FORMAT_TIME, &stop);
733       }
734
735       if (auparse->srcpad) {
736         GST_INFO_OBJECT (auparse,
737             "new segment: %" GST_TIME_FORMAT " ... %" GST_TIME_FORMAT,
738             GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
739
740         new_event = gst_event_new_new_segment_full (update, rate, arate,
741             GST_FORMAT_TIME, start, stop, start);
742
743         ret = gst_pad_push_event (auparse->srcpad, new_event);
744       }
745
746       auparse->buffer_offset = offset;
747
748       gst_event_unref (event);
749       break;
750     }
751     case GST_EVENT_EOS:
752       if (!auparse->srcpad) {
753         GST_ELEMENT_ERROR (auparse, STREAM, WRONG_TYPE,
754             ("No valid input found before end of stream"), (NULL));
755       }
756       /* fall-through */
757     default:
758       ret = gst_pad_event_default (pad, event);
759       break;
760   }
761
762   gst_object_unref (auparse);
763   return ret;
764 }
765
766 static gboolean
767 gst_au_parse_src_event (GstPad * pad, GstEvent * event)
768 {
769   GstAuParse *auparse;
770   gboolean ret;
771
772   auparse = GST_AU_PARSE (gst_pad_get_parent (pad));
773
774   switch (GST_EVENT_TYPE (event)) {
775     case GST_EVENT_SEEK:
776       ret = gst_au_parse_handle_seek (auparse, event);
777       break;
778     default:
779       ret = gst_pad_event_default (pad, event);
780       break;
781   }
782
783   gst_object_unref (auparse);
784   return ret;
785 }
786
787 static GstStateChangeReturn
788 gst_au_parse_change_state (GstElement * element, GstStateChange transition)
789 {
790   GstAuParse *auparse = GST_AU_PARSE (element);
791   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
792
793   ret = parent_class->change_state (element, transition);
794   if (ret == GST_STATE_CHANGE_FAILURE)
795     return ret;
796
797   switch (transition) {
798     case GST_STATE_CHANGE_PAUSED_TO_READY:
799       gst_au_parse_reset (auparse);
800     default:
801       break;
802   }
803
804   return ret;
805 }
806
807 static gboolean
808 plugin_init (GstPlugin * plugin)
809 {
810   if (!gst_element_register (plugin, "auparse", GST_RANK_SECONDARY,
811           GST_TYPE_AU_PARSE)) {
812     return FALSE;
813   }
814
815   return TRUE;
816 }
817
818 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
819     GST_VERSION_MINOR,
820     "auparse",
821     "parses au streams", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,
822     GST_PACKAGE_ORIGIN)