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