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