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