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