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