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