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