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