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