remove copyright field from plugins
[platform/upstream/gst-plugins-good.git] / gst / wavparse / gstwavparse.c
1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: t; c-basic-offset: 2 -*- */
2 /* GStreamer
3  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
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 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 #include <string.h>
26
27 #include <gstwavparse.h>
28
29 static void             gst_wavparse_base_init          (gpointer g_class);
30 static void             gst_wavparse_class_init         (GstWavParseClass *klass);
31 static void             gst_wavparse_init               (GstWavParse *wavparse);
32
33 static GstElementStateReturn
34                         gst_wavparse_change_state       (GstElement *element);
35
36 static const GstFormat* gst_wavparse_get_formats        (GstPad *pad);
37 static const GstQueryType *
38                         gst_wavparse_get_query_types    (GstPad *pad);
39 static gboolean         gst_wavparse_pad_query          (GstPad *pad, 
40                                                          GstQueryType type,
41                                                          GstFormat *format, 
42                                                          gint64 *value);
43 static gboolean         gst_wavparse_pad_convert        (GstPad *pad,
44                                                          GstFormat src_format,
45                                                          gint64 src_value,
46                                                          GstFormat *dest_format,
47                                                          gint64 *dest_value);
48
49 static void             gst_wavparse_loop               (GstElement *element);
50 static const GstEventMask*
51                         gst_wavparse_get_event_masks    (GstPad *pad);
52 static gboolean         gst_wavparse_srcpad_event       (GstPad *pad, GstEvent *event);
53 static void             gst_wavparse_get_property       (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
54
55 /* elementfactory information */
56 static GstElementDetails gst_wavparse_details = GST_ELEMENT_DETAILS (
57   ".wav demuxer",
58   "Codec/Demuxer",
59   "Parse a .wav file into raw audio",
60   "Erik Walthinsen <omega@cse.ogi.edu>"
61 );
62
63 GST_PAD_TEMPLATE_FACTORY (sink_template_factory,
64   "wavparse_sink",
65   GST_PAD_SINK,
66   GST_PAD_ALWAYS,
67   GST_CAPS_NEW (
68     "wavparse_wav",
69     "audio/x-wav",
70     NULL
71   )
72 )
73
74 GST_PAD_TEMPLATE_FACTORY (src_template_factory,
75   "wavparse_src",
76   GST_PAD_SRC,
77   GST_PAD_ALWAYS,
78   GST_CAPS_NEW (
79     "wavparse_raw",
80     "audio/x-raw-int",
81        "endianness",       GST_PROPS_INT (G_LITTLE_ENDIAN),
82        "signed",           GST_PROPS_LIST (
83                                 GST_PROPS_BOOLEAN (FALSE),
84                                 GST_PROPS_BOOLEAN (TRUE)
85                            ),
86        "width",            GST_PROPS_LIST (
87                              GST_PROPS_INT (8),
88                              GST_PROPS_INT (16)
89                            ),
90        "depth",            GST_PROPS_LIST (
91                              GST_PROPS_INT (8),
92                              GST_PROPS_INT (16)
93                            ),
94        "rate",             GST_PROPS_INT_RANGE (8000, 48000),
95        "channels",         GST_PROPS_INT_RANGE (1, 2)
96   ),
97   GST_CAPS_NEW (
98     "wavparse_mpeg",
99     "audio/mpeg",
100       "rate",              GST_PROPS_INT_RANGE (8000, 48000),
101       "channels",          GST_PROPS_INT_RANGE (1, 2),
102       "layer",             GST_PROPS_INT_RANGE (1, 3)
103   ),
104   GST_CAPS_NEW (
105     "parsewav_law",
106     "audio/x-alaw",
107       "rate",              GST_PROPS_INT_RANGE (8000, 48000),
108       "channels",          GST_PROPS_INT_RANGE (1, 2)
109   ),
110   GST_CAPS_NEW (
111     "parsewav_law",
112     "audio/x-mulaw",
113       "rate",              GST_PROPS_INT_RANGE (8000, 48000),
114       "channels",          GST_PROPS_INT_RANGE (1, 2)
115   )
116 )
117
118 /* WavParse signals and args */
119 enum {
120   /* FILL ME */
121   LAST_SIGNAL
122 };
123
124 enum {
125   PROP_0,
126   PROP_METADATA
127 };
128
129 static GstElementClass *parent_class = NULL;
130 /*static guint gst_wavparse_signals[LAST_SIGNAL] = { 0 }; */
131
132 GType
133 gst_wavparse_get_type (void) 
134 {
135   static GType wavparse_type = 0;
136
137   if (!wavparse_type) {
138     static const GTypeInfo wavparse_info = {
139       sizeof(GstWavParseClass),
140       gst_wavparse_base_init,
141       NULL,
142       (GClassInitFunc) gst_wavparse_class_init,
143       NULL,
144       NULL,
145       sizeof(GstWavParse),
146       0,
147       (GInstanceInitFunc) gst_wavparse_init,
148     };
149     wavparse_type = g_type_register_static (GST_TYPE_ELEMENT, "GstWavParse", &wavparse_info, 0);
150   }
151   return wavparse_type;
152 }
153
154
155 static void
156 gst_wavparse_base_init (gpointer g_class) 
157 {
158   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
159   
160   gst_element_class_set_details (element_class, &gst_wavparse_details);
161
162   /* register src pads */
163   gst_element_class_add_pad_template (element_class, GST_PAD_TEMPLATE_GET (sink_template_factory));
164   gst_element_class_add_pad_template (element_class, GST_PAD_TEMPLATE_GET (src_template_factory));
165 }
166 static void
167 gst_wavparse_class_init (GstWavParseClass *klass) 
168 {
169   GstElementClass *gstelement_class;
170   GObjectClass *object_class;
171   
172   gstelement_class = (GstElementClass*) klass;
173   object_class = (GObjectClass *) klass;
174   
175   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
176
177   object_class->get_property = gst_wavparse_get_property;
178   gstelement_class->change_state = gst_wavparse_change_state;
179
180   g_object_class_install_property (object_class, PROP_METADATA,
181                                    g_param_spec_boxed ("metadata",
182                                                        "Metadata", "Metadata",
183                                                        GST_TYPE_CAPS,
184                                                        G_PARAM_READABLE));
185 }
186
187 static void 
188 gst_wavparse_init (GstWavParse *wavparse) 
189 {
190   GstProps *props;
191   
192   /* sink */
193   wavparse->sinkpad = gst_pad_new_from_template (GST_PAD_TEMPLATE_GET (sink_template_factory), "sink");
194   gst_element_add_pad (GST_ELEMENT (wavparse), wavparse->sinkpad);
195
196   gst_pad_set_formats_function (wavparse->sinkpad, gst_wavparse_get_formats);
197   gst_pad_set_convert_function (wavparse->sinkpad, gst_wavparse_pad_convert);
198   gst_pad_set_query_type_function (wavparse->sinkpad, 
199                                    gst_wavparse_get_query_types);
200   gst_pad_set_query_function (wavparse->sinkpad, gst_wavparse_pad_query);
201
202   /* source */
203   wavparse->srcpad = gst_pad_new_from_template (GST_PAD_TEMPLATE_GET (src_template_factory), "src");
204   gst_element_add_pad (GST_ELEMENT (wavparse), wavparse->srcpad);
205   gst_pad_set_formats_function (wavparse->srcpad, gst_wavparse_get_formats);
206   gst_pad_set_convert_function (wavparse->srcpad, gst_wavparse_pad_convert);
207   gst_pad_set_query_type_function (wavparse->srcpad,
208                                    gst_wavparse_get_query_types);
209   gst_pad_set_query_function (wavparse->srcpad, gst_wavparse_pad_query);
210   gst_pad_set_event_function (wavparse->srcpad, gst_wavparse_srcpad_event);
211   gst_pad_set_event_mask_function (wavparse->srcpad, gst_wavparse_get_event_masks);
212
213   gst_element_set_loop_function (GST_ELEMENT (wavparse), gst_wavparse_loop);
214
215   wavparse->state = GST_WAVPARSE_UNKNOWN;
216   wavparse->bps = 0;
217   wavparse->seek_pending = FALSE;
218   wavparse->seek_offset = 0;
219
220   props = gst_props_empty_new ();
221
222   /* Metadata is added later when we find it */
223   gst_caps_replace_sink (&wavparse->metadata,
224                          gst_caps_new ("wav_metadata",
225                                        "application/x-gst-metadata",
226                                        props));
227 }
228
229 static void
230 gst_wavparse_get_property (GObject *object,
231                            guint prop_id,
232                            GValue *value,
233                            GParamSpec *pspec)
234 {
235   GstWavParse *wavparse;
236
237   wavparse = GST_WAVPARSE (object);
238
239   switch (prop_id) {
240   case PROP_METADATA:
241     g_value_set_boxed (value, wavparse->metadata);
242     break;
243
244   default:
245     break;
246   }
247 }
248
249 static void
250 gst_wavparse_parse_adtl (GstWavParse *wavparse,
251                          int len)
252 {
253   guint32 got_bytes;
254   GstByteStream *bs = wavparse->bs;
255   gst_riff_chunk *temp_chunk, chunk;
256   guint8 *tempdata;
257   struct _gst_riff_labl labl, *temp_labl;
258   struct _gst_riff_ltxt ltxt, *temp_ltxt;
259   struct _gst_riff_note note, *temp_note;
260   char *label_name;
261   GstProps *props;
262   GstPropsEntry *entry;
263   GstCaps *new_caps;
264   GList *caps = NULL;
265
266   props = wavparse->metadata->properties;
267   
268   while (len > 0) {
269     got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, sizeof (gst_riff_chunk));
270     if (got_bytes != sizeof (gst_riff_chunk)) {
271       return;
272     }
273     temp_chunk = (gst_riff_chunk *) tempdata;
274     
275     chunk.id = GUINT32_FROM_LE (temp_chunk->id);
276     chunk.size = GUINT32_FROM_LE (temp_chunk->size);
277
278     if (chunk.size == 0) {
279       gst_bytestream_flush (bs, sizeof (gst_riff_chunk));
280       len -= sizeof (gst_riff_chunk);
281       continue;
282     }
283     
284     switch  (chunk.id) {
285     case GST_RIFF_adtl_labl:
286       got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, sizeof (struct _gst_riff_labl));
287       if (got_bytes != sizeof (struct _gst_riff_labl)) {
288         return;
289       }
290       
291       temp_labl = (struct _gst_riff_labl *) tempdata;
292       labl.id = GUINT32_FROM_LE (temp_labl->id);
293       labl.size = GUINT32_FROM_LE (temp_labl->size);
294       labl.identifier = GUINT32_FROM_LE (temp_labl->identifier);
295
296       gst_bytestream_flush (bs, sizeof (struct _gst_riff_labl));
297       len -= sizeof (struct _gst_riff_labl);
298       
299       got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, labl.size - 4);
300       if (got_bytes != labl.size - 4) {
301         return;
302       }
303
304       label_name = (char *) tempdata;
305       
306       gst_bytestream_flush (bs, ((labl.size - 4) + 1) & ~1);
307       len -= (( (labl.size - 4) + 1) & ~1);
308
309       new_caps = gst_caps_new ("label",
310                                                                                                                          "application/x-gst-metadata",
311                                                                                                                          gst_props_new (
312                                                                                                                                  "identifier", GST_PROPS_INT (labl.identifier),
313                                                                                                                                  "name", GST_PROPS_STRING (label_name),
314                                                                                                                                  NULL));
315       
316       if (gst_props_get (props, "labels", &caps, NULL)) {
317                                 caps = g_list_append (caps, new_caps);
318       } else {
319                                 caps = g_list_append (NULL, new_caps);
320                                 
321                                 entry = gst_props_entry_new ("labels", GST_PROPS_GLIST (caps));
322                                 gst_props_add_entry (props, entry);
323       }
324       
325       break;
326       
327     case GST_RIFF_adtl_ltxt:
328       got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, sizeof (struct _gst_riff_ltxt));
329       if (got_bytes != sizeof (struct _gst_riff_ltxt)) {
330         return;
331       }
332       
333       temp_ltxt = (struct _gst_riff_ltxt *) tempdata;
334       ltxt.id = GUINT32_FROM_LE (temp_ltxt->id);
335       ltxt.size = GUINT32_FROM_LE (temp_ltxt->size);
336       ltxt.identifier = GUINT32_FROM_LE (temp_ltxt->identifier);
337       ltxt.length = GUINT32_FROM_LE (temp_ltxt->length);
338       ltxt.purpose = GUINT32_FROM_LE (temp_ltxt->purpose);
339       ltxt.country = GUINT16_FROM_LE (temp_ltxt->country);
340       ltxt.language = GUINT16_FROM_LE (temp_ltxt->language);
341       ltxt.dialect = GUINT16_FROM_LE (temp_ltxt->dialect);
342       ltxt.codepage = GUINT16_FROM_LE (temp_ltxt->codepage);
343
344       gst_bytestream_flush (bs, sizeof (struct _gst_riff_ltxt));
345       len -= sizeof (struct _gst_riff_ltxt);
346
347                         if (ltxt.size - 20 > 0) {
348                                 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, ltxt.size - 20);
349                                 if (got_bytes != ltxt.size - 20) {
350                                         return;
351                                 }
352                                 
353                                 gst_bytestream_flush (bs, ((ltxt.size - 20) + 1) & ~1);
354                                 len -= (( (ltxt.size - 20) + 1) & ~1);
355
356                                 label_name = (char *) tempdata;
357                         } else {
358                                 label_name = "";
359                         }
360                         
361       new_caps = gst_caps_new ("ltxt",
362                                                                                                                          "application/x-gst-metadata",
363                                                                                                                          gst_props_new (
364                                                                                                                                  "identifier", GST_PROPS_INT (ltxt.identifier),
365                                                                                                                                  "name", GST_PROPS_STRING (label_name),
366                                                                                                                                  "length", GST_PROPS_INT (ltxt.length),
367                                                                                                                                  NULL));
368                         
369       if (gst_props_get (props, "ltxts", &caps, NULL)) {
370                                 caps = g_list_append (caps, new_caps);
371       } else {
372                                 caps = g_list_append (NULL, new_caps);
373                                 
374                                 entry = gst_props_entry_new ("ltxts", GST_PROPS_GLIST (caps));
375                                 gst_props_add_entry (props, entry);
376       }
377       
378       break;
379                         
380     case GST_RIFF_adtl_note:
381       got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, sizeof (struct _gst_riff_note));
382       if (got_bytes != sizeof (struct _gst_riff_note)) {
383                                 return;
384       }
385       
386       temp_note = (struct _gst_riff_note *) tempdata;
387       note.id = GUINT32_FROM_LE (temp_note->id);
388       note.size = GUINT32_FROM_LE (temp_note->size);
389       note.identifier = GUINT32_FROM_LE (temp_note->identifier);
390       
391       gst_bytestream_flush (bs, sizeof (struct _gst_riff_note));
392       len -= sizeof (struct _gst_riff_note);
393                         
394       got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, note.size - 4);
395       if (got_bytes != note.size - 4) {
396                                 return;
397       }
398                         
399       gst_bytestream_flush (bs, ((note.size - 4) + 1) & ~1);
400       len -= (( (note.size - 4) + 1) & ~1);
401                         
402       label_name = (char *) tempdata;
403       
404       new_caps = gst_caps_new ("note",
405                                                                                                                          "application/x-gst-metadata",
406                                                                                                                          gst_props_new (
407                                                                                                                                  "identifier", GST_PROPS_INT (note.identifier),
408                                                                                                                                  "name", GST_PROPS_STRING (label_name),
409                                                                                                                                  NULL));
410       
411       if (gst_props_get (props, "notes", &caps, NULL)) {
412                                 caps = g_list_append (caps, new_caps);
413       } else {
414                                 caps = g_list_append (NULL, new_caps);
415                                 
416                                 entry = gst_props_entry_new ("notes", GST_PROPS_GLIST (caps));
417                                 gst_props_add_entry (props, entry);
418       }
419                         
420       break;      
421       
422     default:
423       g_print ("Unknown chunk: " GST_FOURCC_FORMAT "\n", GST_FOURCC_ARGS(chunk.id));
424       return;
425     }
426   }
427
428   g_object_notify (G_OBJECT (wavparse), "metadata");
429 }
430
431 static void
432 gst_wavparse_parse_info (GstWavParse *wavparse,
433                                                                                                  int len)
434 {
435   gst_riff_chunk *temp_chunk, chunk;
436   GstByteStream *bs = wavparse->bs;
437   guint8 *tempdata;
438   guint32 got_bytes;
439   char *name, *type;
440   
441   while (len > 0) {
442     got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, sizeof (gst_riff_chunk));
443     temp_chunk = (gst_riff_chunk *) tempdata;
444     
445     chunk.id = GUINT32_FROM_LE (temp_chunk->id);
446     chunk.size = GUINT32_FROM_LE (temp_chunk->size);
447
448     gst_bytestream_flush (bs, sizeof (gst_riff_chunk));
449     if (got_bytes != sizeof (gst_riff_chunk)) {
450       return;
451     }
452                                                  
453     /* move our pointer on past the header */
454     len -= sizeof (gst_riff_chunk);
455     
456     if (chunk.size == 0) {
457       continue;
458     }
459     
460     got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, chunk.size);
461     name = (char *) tempdata;
462     if (got_bytes != chunk.size) {
463       return;
464     }
465     
466     /* move our pointer on past the data ... on an even boundary */
467                 gst_bytestream_flush (bs, (chunk.size + 1) & ~1);
468     len -= ((chunk.size + 1) & ~1);
469
470     /* We now have an info string in 'name' of type chunk.id
471        - find type */
472     switch (chunk.id) {
473     case GST_RIFF_INFO_IARL:
474       type = "Location";
475       break;
476       
477     case GST_RIFF_INFO_IART:
478       type = "Artist";
479       break;
480       
481     case GST_RIFF_INFO_ICMS:
482       type = "Commissioner";
483       break;
484       
485     case GST_RIFF_INFO_ICMT:
486       type = "Comment";
487       break;
488       
489     case GST_RIFF_INFO_ICOP:
490       type = "Copyright";
491       break;
492       
493     case GST_RIFF_INFO_ICRD:
494       type = "Creation Date";
495       break;
496       
497     case GST_RIFF_INFO_IENG:
498       type = "Engineer";
499       break;
500       
501     case GST_RIFF_INFO_IGNR:
502       type = "Genre";
503       break;
504       
505     case GST_RIFF_INFO_IKEY:
506       type = "Keywords";
507       break;
508       
509     case GST_RIFF_INFO_INAM:
510       type = "Title"; /* name */
511       break;
512       
513     case GST_RIFF_INFO_IPRD:
514       type = "Product";
515       break;
516       
517     case GST_RIFF_INFO_ISBJ:
518       type = "Subject";
519       break;
520       
521     case GST_RIFF_INFO_ISFT:
522       type = "Software";
523       break;
524       
525     case GST_RIFF_INFO_ITCH:
526       type = "Technician";
527       break;
528       
529     default:
530       g_print ("Unknown: %4.4s\n", (char *) &chunk.id);
531       type = NULL;
532       break;
533     }
534
535     if (type) {
536       GstPropsEntry *entry;
537
538       entry = gst_props_entry_new (type, GST_PROPS_STRING (name));
539       gst_props_add_entry (wavparse->metadata->properties, entry);
540     }
541   }
542
543   g_object_notify (G_OBJECT (wavparse), "metadata");
544 }
545
546 static void
547 gst_wavparse_parse_cues (GstWavParse *wavparse,
548                          int len)
549 {
550   guint32 got_bytes;
551   GstByteStream *bs = wavparse->bs;
552   struct _gst_riff_cue *temp_cue, cue;
553   struct _gst_riff_cuepoints *points;
554   guint8 *tempdata;
555   int i;
556   GList *cues = NULL;
557   GstPropsEntry *entry;
558
559   while (len > 0) {
560     int required;
561     
562     got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, sizeof (struct _gst_riff_cue));
563     temp_cue = (struct _gst_riff_cue *) tempdata;
564
565     /* fixup for our big endian friends */
566     cue.id = GUINT32_FROM_LE (temp_cue->id);
567     cue.size = GUINT32_FROM_LE (temp_cue->size);
568     cue.cuepoints = GUINT32_FROM_LE (temp_cue->cuepoints);
569
570     gst_bytestream_flush (bs, sizeof (struct _gst_riff_cue));
571     if (got_bytes != sizeof (struct _gst_riff_cue)) {
572       return;
573     }
574
575     len -= sizeof (struct _gst_riff_cue);
576
577     /* -4 because cue.size contains the cuepoints size
578        and we've already flushed that out of the system */
579     required = cue.size - 4;
580     got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, required);
581     gst_bytestream_flush (bs, ((required) + 1) & ~1);
582     if (got_bytes != required) {
583       return;
584     }
585
586     len -= ( ((cue.size - 4) + 1) & ~1);
587
588     /* now we have an array of struct _gst_riff_cuepoints in tempdata */
589     points = (struct _gst_riff_cuepoints *) tempdata;
590     
591     for (i = 0; i < cue.cuepoints; i++) {
592       GstCaps *caps;
593
594       caps = gst_caps_new ("cues",
595                                                                                                          "application/x-gst-metadata",
596                                                                                                          gst_props_new (
597                                                                                                                  "identifier", GST_PROPS_INT (points[i].identifier),
598                                                                                                                  "position", GST_PROPS_INT (points[i].offset),
599                                                                                                                  NULL));
600       cues = g_list_append (cues, caps);
601     }
602     
603     entry = gst_props_entry_new ("cues", GST_PROPS_GLIST (cues));
604     gst_props_add_entry (wavparse->metadata->properties, entry);
605   }
606   
607   g_object_notify (G_OBJECT (wavparse), "metadata");
608 }
609
610 static void
611 gst_wavparse_parse_fmt (GstWavParse *wavparse)
612 {
613   GstWavParseFormat *format;
614   GstCaps *caps = NULL;
615   guint8 *fmtdata;
616   GstByteStream *bs = wavparse->bs;
617   guint32 got_bytes;
618
619   got_bytes = gst_bytestream_peek_bytes (bs, &fmtdata, sizeof (GstWavParseFormat));
620   format = (GstWavParseFormat *) fmtdata;
621
622   if (got_bytes == sizeof (GstWavParseFormat)) {
623     gst_bytestream_flush (bs, got_bytes);
624     wavparse->bps = GUINT16_FROM_LE (format->wBlockAlign);
625     wavparse->rate = GUINT32_FROM_LE (format->dwSamplesPerSec);
626     wavparse->channels = GUINT16_FROM_LE (format->wChannels);
627     wavparse->width = GUINT16_FROM_LE (format->wBitsPerSample);
628     wavparse->format = GINT16_FROM_LE (format->wFormatTag);
629
630     /* set the caps on the src pad */
631     /* FIXME: handle all of the other formats as well */
632     switch (wavparse->format) {
633     case GST_RIFF_WAVE_FORMAT_ALAW:
634     case GST_RIFF_WAVE_FORMAT_MULAW: {
635       char *mime = (wavparse->format == GST_RIFF_WAVE_FORMAT_ALAW) ?
636                                 "audio/x-alaw" : "audio/x-mulaw";
637       if (wavparse->width != 8) {
638                                 g_warning ("Ignoring invalid width %d", wavparse->width);
639                                 return;
640       }
641                         
642       caps = GST_CAPS_NEW ("parsewav_src",
643                                                                                                          mime,
644                                                                                                          "rate", GST_PROPS_INT (wavparse->rate),
645                                                                                                          "channels", GST_PROPS_INT (wavparse->channels)
646                                 );
647     }
648                         
649     case GST_RIFF_WAVE_FORMAT_PCM:
650       caps = GST_CAPS_NEW ("parsewav_src",
651                                                                                                          "audio/x-raw-int",
652                                                                                                          "endianness", GST_PROPS_INT (G_LITTLE_ENDIAN),
653                                                                                                          "signed", GST_PROPS_BOOLEAN ((wavparse->width > 8) ? TRUE : FALSE),
654                                                                                                          "width", GST_PROPS_INT (wavparse->width),
655                                                                                                          "depth", GST_PROPS_INT (wavparse->width),
656                                                                                                          "rate", GST_PROPS_INT (wavparse->rate),
657                                                                                                          "channels", GST_PROPS_INT (wavparse->channels)
658                                 );
659       break;
660                         
661     case GST_RIFF_WAVE_FORMAT_MPEGL12:
662     case GST_RIFF_WAVE_FORMAT_MPEGL3: {
663       int layer = (wavparse->format == GST_RIFF_WAVE_FORMAT_MPEGL12) ? 2 : 3;
664                         
665       caps = GST_CAPS_NEW ("parsewav_src",
666                                                                                                          "audio/mpeg",
667                                                                                                          "layer", GST_PROPS_INT (layer),
668                                                                                                          "rate", GST_PROPS_INT (wavparse->rate),
669                                                                                                          "channels", GST_PROPS_INT (wavparse->channels)
670                                 );
671     }
672       break;
673                         
674     default:
675       gst_element_error (GST_ELEMENT (wavparse), "wavparse: format %d not handled", wavparse->format);
676       return;
677     }
678                 
679     if (gst_pad_try_set_caps (wavparse->srcpad, caps) <= 0) {
680       gst_element_error (GST_ELEMENT (wavparse), "Could not set caps");
681       return;
682     }
683                 
684     GST_DEBUG ("frequency %d, channels %d",
685                                                          wavparse->rate, wavparse->channels);
686   }
687 }
688
689 static gboolean
690 gst_wavparse_handle_sink_event (GstWavParse *wavparse)
691 {
692   guint32 remaining;
693   GstEvent *event;
694   GstEventType type;
695   gboolean res = TRUE;
696         
697   gst_bytestream_get_status (wavparse->bs, &remaining, &event);
698         
699   type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
700   GST_DEBUG ("wavparse: event %p %d", event, type);
701         
702   switch (type) {
703   case GST_EVENT_EOS:
704     gst_bytestream_flush (wavparse->bs, remaining);
705     gst_pad_event_default (wavparse->sinkpad, event);
706     res = FALSE;
707     goto done;
708
709   case GST_EVENT_FLUSH:
710     g_warning ("Wavparse: Flush event");
711     break;
712
713   default:
714     g_warning ("Wavparse: Unhandled event %d", type);
715     break;
716   }
717
718   gst_event_unref (event);
719
720  done:
721   return res;
722 }
723     
724 static void
725 gst_wavparse_loop (GstElement *element)
726 {
727   GstWavParse *wavparse;
728   gst_riff_riff chunk;
729   guint32 flush = 0;
730   guint32 got_bytes;
731   GstByteStream *bs;
732
733   wavparse = GST_WAVPARSE (element);
734
735   bs = wavparse->bs;
736
737   if (wavparse->seek_pending) {
738     GST_DEBUG ("wavparse: seek pending to %" G_GINT64_FORMAT " %08llx",
739                wavparse->seek_offset,
740                (unsigned long long) wavparse->seek_offset);
741     
742     if (!gst_bytestream_seek (bs, wavparse->seek_offset, GST_SEEK_METHOD_SET)) {
743       GST_INFO ("wavparse: Could not seek");
744     }
745                 
746     wavparse->seek_pending = FALSE;
747   }
748         
749   if (wavparse->state == GST_WAVPARSE_DATA) {
750     GstBuffer *buf;
751     int desired;
752     
753     /* This seems to want the whole chunk,
754        Will this screw up streaming?
755        Does anyone care about streaming wavs?
756        FIXME: Should we have a decent buffer size? */
757                 
758 #define MAX_BUFFER_SIZE 1024
759
760     if (wavparse->dataleft > 0) {
761       desired = MIN (wavparse->dataleft, MAX_BUFFER_SIZE);
762       got_bytes = gst_bytestream_peek (bs, &buf, desired);
763
764       if (got_bytes == 0) {
765         return;
766       }
767
768       wavparse->dataleft -= got_bytes;
769       wavparse->byteoffset += got_bytes;
770
771       gst_bytestream_flush (bs, got_bytes);
772                         
773       gst_pad_push (wavparse->srcpad, GST_DATA (buf));
774       return;
775     } else {
776       wavparse->state = GST_WAVPARSE_OTHER;
777     }
778   }
779
780   do {
781     gst_riff_riff *temp_chunk;
782     guint8 *tempdata;
783     guint32 skipsize;
784                 
785     /* read first two dwords to get chunktype and size */
786     while (TRUE) {
787       got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, sizeof (gst_riff_chunk));
788       temp_chunk = (gst_riff_riff *) tempdata;
789
790       if (got_bytes < sizeof (gst_riff_chunk)) {
791                                 if (!gst_wavparse_handle_sink_event (wavparse)) {
792                                         return;
793                                 }
794       } else {
795                                 break;
796       }
797     }
798                 
799     chunk.id = GUINT32_FROM_LE (temp_chunk->id);
800     chunk.size = GUINT32_FROM_LE (temp_chunk->size);
801
802     switch (chunk.id) {
803     case GST_RIFF_TAG_RIFF:
804     case GST_RIFF_TAG_LIST:
805       /* Read complete list chunk */
806       while (TRUE) {
807         got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, sizeof (gst_riff_list));
808         temp_chunk = (gst_riff_riff *) tempdata;
809         if (got_bytes < sizeof (gst_riff_list)) {
810           if (!gst_wavparse_handle_sink_event (wavparse)) {
811             return;
812           }
813         } else {
814           break;
815         }
816       }
817                         
818       chunk.type = GUINT32_FROM_LE (temp_chunk->type);
819       skipsize = sizeof (gst_riff_list);
820       break;
821
822     case GST_RIFF_TAG_cue:
823       skipsize = 0;
824       break;
825       
826     default:
827       skipsize = sizeof (gst_riff_chunk);
828       break;
829     }
830     gst_bytestream_flush (bs, skipsize);
831   } while (FALSE);
832
833   /* need to flush an even number of bytes at the end */
834   flush = (chunk.size + 1) & ~1;
835
836   switch (wavparse->state) {
837   case GST_WAVPARSE_START:
838     if (chunk.id != GST_RIFF_TAG_RIFF &&
839                                 chunk.type != GST_RIFF_RIFF_WAVE) {
840       gst_element_error (element, "This doesn't appear to be a WAV file %08x %08x", chunk.id, chunk.type);
841       return;
842     }
843                 
844     wavparse->state = GST_WAVPARSE_OTHER;
845     /* We are not going to flush lists */
846     flush = 0;
847     break;
848
849   case GST_WAVPARSE_OTHER:
850     GST_DEBUG ("riff tag: %4.4s %08x", (char *) &chunk.id, chunk.size);
851                 
852     switch (chunk.id) {
853     case GST_RIFF_TAG_data:
854       wavparse->state = GST_WAVPARSE_DATA;
855                         wavparse->dataleft = chunk.size;
856                         wavparse->byteoffset = 0;
857
858                         flush = 0;
859       break;
860       
861     case GST_RIFF_TAG_fmt:
862       gst_wavparse_parse_fmt (wavparse);
863       break;
864
865     case GST_RIFF_TAG_cue:
866       gst_wavparse_parse_cues (wavparse, chunk.size);
867       break;
868       
869     case GST_RIFF_TAG_LIST:
870       GST_DEBUG ("list type: %4.4s", (char *) &chunk.type);
871       switch (chunk.type) {
872       case GST_RIFF_LIST_INFO:
873         gst_wavparse_parse_info (wavparse, chunk.size - 4);
874         flush = 0;
875
876         break;
877                                 
878       case GST_RIFF_LIST_adtl:
879         gst_wavparse_parse_adtl (wavparse, chunk.size - 4);
880         flush = 0;
881         break;
882
883       default:
884         flush = 0;
885         break;
886       }
887       
888       flush = 0;
889       break;      
890     }
891                 
892   case GST_WAVPARSE_DATA:
893                 /* Should have been handled up there ^^^^ */
894                 flush = 0;
895     break;
896                 
897   default:
898     /* Unknown */
899     GST_DEBUG ("  *****  unknown chunkid %08x", chunk.id);
900     break;
901   }
902
903   if (flush > 0) {
904     gboolean res;
905
906     res = gst_bytestream_flush (bs, flush);
907     if (!res) {
908       guint32 remaining;
909       GstEvent *event;
910
911       gst_bytestream_get_status (bs, &remaining, &event);
912       gst_event_unref (event);
913     } 
914   }
915 }
916
917 /* convert and query stuff */
918 static const GstFormat *
919 gst_wavparse_get_formats (GstPad *pad)
920 {
921   static GstFormat formats[] = {
922     GST_FORMAT_TIME,
923     GST_FORMAT_BYTES,
924     GST_FORMAT_DEFAULT, /* a "frame", ie a set of samples per Hz */
925     0,
926     0
927   };
928   return formats;
929 }
930
931 static gboolean
932 gst_wavparse_pad_convert (GstPad *pad,
933                           GstFormat src_format, gint64 src_value,
934                           GstFormat *dest_format, gint64 *dest_value)
935 {
936   gint bytes_per_sample;
937   glong byterate;
938   GstWavParse *wavparse;
939
940   wavparse = GST_WAVPARSE (gst_pad_get_parent (pad));
941   
942   bytes_per_sample = wavparse->channels * wavparse->width / 8;
943   if (bytes_per_sample == 0) {
944     GST_DEBUG ("bytes_per_sample is 0, probably an mp3 - channels %d,  width %d\n",
945                   wavparse->channels, wavparse->width);
946     return FALSE;
947   }
948   byterate = (glong) (bytes_per_sample * wavparse->rate);
949   if (byterate == 0) {
950     g_warning ("byterate is 0, internal error\n");
951     return FALSE;
952   }
953   GST_DEBUG ("bytes per sample: %d\n", bytes_per_sample);
954
955   switch (src_format) {
956     case GST_FORMAT_BYTES:
957       if (*dest_format == GST_FORMAT_DEFAULT)
958         *dest_value = src_value / bytes_per_sample;
959       else if (*dest_format == GST_FORMAT_TIME)
960         *dest_value = src_value * GST_SECOND / byterate;
961       else
962         return FALSE;
963       break;
964     case GST_FORMAT_DEFAULT:
965       if (*dest_format == GST_FORMAT_BYTES)
966         *dest_value = src_value * bytes_per_sample;
967       else if (*dest_format == GST_FORMAT_TIME)
968         *dest_value = src_value * GST_SECOND / wavparse->rate;
969       else
970         return FALSE;
971       break;
972     case GST_FORMAT_TIME:
973       if (*dest_format == GST_FORMAT_BYTES)
974         *dest_value = src_value * byterate / GST_SECOND;
975       else if (*dest_format == GST_FORMAT_DEFAULT)
976         *dest_value = src_value * wavparse->rate / GST_SECOND;
977       else
978         return FALSE;
979
980       *dest_value = *dest_value & ~(bytes_per_sample - 1);
981       break;
982     default:
983       g_warning ("unhandled format for wavparse\n");
984       break;
985   }
986   return TRUE;
987 }
988       
989 static const GstQueryType *
990 gst_wavparse_get_query_types (GstPad *pad)
991 {
992   static const GstQueryType types[] = {
993     GST_QUERY_TOTAL,
994     GST_QUERY_POSITION,
995     0
996   };
997   return types;
998 }
999
1000 /* handle queries for location and length in requested format */
1001 static gboolean
1002 gst_wavparse_pad_query (GstPad *pad, GstQueryType type,
1003                         GstFormat *format, gint64 *value)
1004 {
1005   GstFormat peer_format = GST_FORMAT_BYTES;
1006   gint64 peer_value;
1007   GstWavParse *wavparse;
1008
1009   /* probe sink's peer pad, convert value, and that's it :) */
1010   /* FIXME: ideally we'd loop over possible formats of peer instead
1011    * of only using BYTE */
1012   wavparse = GST_WAVPARSE (gst_pad_get_parent (pad));
1013   if (!gst_pad_query (GST_PAD_PEER (wavparse->sinkpad), type, 
1014                       &peer_format, &peer_value)) {
1015     g_warning ("Could not query sink pad's peer\n");
1016     return FALSE;
1017   }
1018   if (!gst_pad_convert (wavparse->sinkpad, peer_format, peer_value,
1019                         format, value)) {
1020     g_warning ("Could not query sink pad's peer\n");
1021     return FALSE;
1022   }
1023   GST_DEBUG ("pad_query done, value %" G_GINT64_FORMAT "\n", *value);
1024   return TRUE;
1025 }
1026
1027 static const GstEventMask*
1028 gst_wavparse_get_event_masks (GstPad *pad)
1029
1030   static const GstEventMask gst_wavparse_src_event_masks[] = {
1031     { GST_EVENT_SEEK, GST_SEEK_METHOD_SET |
1032                       GST_SEEK_FLAG_FLUSH },
1033     { 0, }
1034   };
1035   return gst_wavparse_src_event_masks;
1036 }   
1037
1038 static gboolean
1039 gst_wavparse_srcpad_event (GstPad *pad, GstEvent *event)
1040 {
1041 #if 0
1042   GstWavParse *wavparse = GST_WAVPARSE (GST_PAD_PARENT (pad));
1043   gboolean res = FALSE;
1044
1045   GST_DEBUG ("event %d", GST_EVENT_TYPE (event));
1046
1047   switch (GST_EVENT_TYPE (event)) {
1048     case GST_EVENT_SEEK:
1049     {
1050       gint64 byteoffset;
1051       GstFormat format;
1052
1053       /* we can only seek when in the DATA state */
1054       if (wavparse->state != GST_WAVPARSE_DATA) {
1055         return FALSE;
1056       }
1057
1058       format = GST_FORMAT_BYTES;
1059       
1060       /* bring format to bytes for the peer element, 
1061        * FIXME be smarter here */
1062       res = gst_pad_convert (pad, 
1063                              GST_EVENT_SEEK_FORMAT (event),
1064                              GST_EVENT_SEEK_OFFSET (event),
1065                              &format,
1066                              &byteoffset);
1067       
1068       if (res) {
1069         /* ok, seek worked, update our state */
1070         wavparse->seek_offset = byteoffset;
1071         wavparse->seek_pending = TRUE;
1072         wavparse->need_discont = TRUE;
1073       }
1074       break;
1075     }
1076     default:
1077       break;
1078   }
1079
1080   gst_event_unref (event);
1081   return res;
1082 #else
1083   return FALSE;
1084 #endif
1085 }
1086
1087 static GstElementStateReturn
1088 gst_wavparse_change_state (GstElement *element)
1089 {
1090   GstWavParse *wavparse = GST_WAVPARSE (element);
1091
1092   switch (GST_STATE_TRANSITION (element)) {
1093     case GST_STATE_NULL_TO_READY:
1094       break;
1095     case GST_STATE_READY_TO_PAUSED:
1096       wavparse->bs = gst_bytestream_new (wavparse->sinkpad);
1097       wavparse->state = GST_WAVPARSE_START;
1098       break;
1099     case GST_STATE_PAUSED_TO_PLAYING:
1100       break;
1101     case GST_STATE_PLAYING_TO_PAUSED:
1102       break;
1103     case GST_STATE_PAUSED_TO_READY:
1104       gst_bytestream_destroy (wavparse->bs);
1105       wavparse->state = GST_WAVPARSE_UNKNOWN;
1106       wavparse->bps = 0;
1107       wavparse->seek_pending = FALSE;
1108       wavparse->seek_offset = 0;
1109       gst_caps_replace (&wavparse->metadata, NULL);
1110       
1111       break;
1112     case GST_STATE_READY_TO_NULL:
1113       break;
1114   }
1115
1116   if (GST_ELEMENT_CLASS (parent_class)->change_state)
1117     return GST_ELEMENT_CLASS (parent_class)->change_state (element);
1118
1119   return GST_STATE_SUCCESS;
1120 }
1121
1122 static gboolean
1123 plugin_init (GstPlugin *plugin)
1124 {
1125   if (!gst_library_load ("gstbytestream")) {
1126     return FALSE;
1127   }
1128
1129   return gst_element_register (plugin, "wavparse", GST_RANK_SECONDARY, GST_TYPE_WAVPARSE);
1130 }
1131
1132 GST_PLUGIN_DEFINE (
1133   GST_VERSION_MAJOR,
1134   GST_VERSION_MINOR,
1135   "wavparse",
1136   "Parse a .wav file into raw audio",
1137   plugin_init,
1138   VERSION,
1139   GST_LICENSE,
1140   GST_PACKAGE,
1141   GST_ORIGIN
1142 )