abcbee125fc26165e261cc71b4e9c1d49d3a777c
[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  * Copyright (C) <2006> Nokia Corporation, Stefan Kost <stefan.kost@nokia.com>.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 /**
23  * SECTION:element-wavparse
24  *
25  * Parse a .wav file into raw or compressed audio.
26  *
27  * Wavparse supports both push and pull mode operations, making it possible to
28  * stream from a network source.
29  *
30  * <refsect2>
31  * <title>Example launch line</title>
32  * |[
33  * gst-launch filesrc location=sine.wav ! wavparse ! audioconvert ! alsasink
34  * ]| Read a wav file and output to the soundcard using the ALSA element. The
35  * wav file is assumed to contain raw uncompressed samples.
36  * |[
37  * gst-launch gnomevfssrc location=http://www.example.org/sine.wav ! queue ! wavparse ! audioconvert ! alsasink
38  * ]| Stream data from a network url.
39  * </refsect2>
40  *
41  * Last reviewed on 2007-02-14 (0.10.6)
42  */
43
44 /*
45  * TODO:
46  * http://replaygain.hydrogenaudio.org/file_format_wav.html
47  */
48
49 #ifdef HAVE_CONFIG_H
50 #include "config.h"
51 #endif
52 #include <string.h>
53 #include <math.h>
54
55 #include "gstwavparse.h"
56 #include "gst/riff/riff-ids.h"
57 #include "gst/riff/riff-media.h"
58 #include <gst/gst-i18n-plugin.h>
59
60 GST_DEBUG_CATEGORY_STATIC (wavparse_debug);
61 #define GST_CAT_DEFAULT (wavparse_debug)
62
63 static void gst_wavparse_dispose (GObject * object);
64
65 static gboolean gst_wavparse_sink_activate (GstPad * sinkpad);
66 static gboolean gst_wavparse_sink_activate_pull (GstPad * sinkpad,
67     gboolean active);
68 static gboolean gst_wavparse_send_event (GstElement * element,
69     GstEvent * event);
70 static GstStateChangeReturn gst_wavparse_change_state (GstElement * element,
71     GstStateChange transition);
72
73 static const GstQueryType *gst_wavparse_get_query_types (GstPad * pad);
74 static gboolean gst_wavparse_pad_query (GstPad * pad, GstQuery * query);
75 static gboolean gst_wavparse_pad_convert (GstPad * pad,
76     GstFormat src_format,
77     gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
78
79 static GstFlowReturn gst_wavparse_chain (GstPad * pad, GstBuffer * buf);
80 static gboolean gst_wavparse_sink_event (GstPad * pad, GstEvent * event);
81 static void gst_wavparse_loop (GstPad * pad);
82 static gboolean gst_wavparse_srcpad_event (GstPad * pad, GstEvent * event);
83
84 static const GstElementDetails gst_wavparse_details =
85 GST_ELEMENT_DETAILS ("WAV audio demuxer",
86     "Codec/Demuxer/Audio",
87     "Parse a .wav file into raw audio",
88     "Erik Walthinsen <omega@cse.ogi.edu>");
89
90 static GstStaticPadTemplate sink_template_factory =
91 GST_STATIC_PAD_TEMPLATE ("wavparse_sink",
92     GST_PAD_SINK,
93     GST_PAD_ALWAYS,
94     GST_STATIC_CAPS ("audio/x-wav")
95     );
96
97 #define DEBUG_INIT(bla) \
98   GST_DEBUG_CATEGORY_INIT (wavparse_debug, "wavparse", 0, "WAV parser");
99
100 GST_BOILERPLATE_FULL (GstWavParse, gst_wavparse, GstElement,
101     GST_TYPE_ELEMENT, DEBUG_INIT);
102
103 static void
104 gst_wavparse_base_init (gpointer g_class)
105 {
106   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
107   GstPadTemplate *src_template;
108
109   /* register pads */
110   gst_element_class_add_pad_template (element_class,
111       gst_static_pad_template_get (&sink_template_factory));
112
113   src_template = gst_pad_template_new ("wavparse_src", GST_PAD_SRC,
114       GST_PAD_SOMETIMES, gst_riff_create_audio_template_caps ());
115   gst_element_class_add_pad_template (element_class, src_template);
116   gst_object_unref (src_template);
117
118   gst_element_class_set_details (element_class, &gst_wavparse_details);
119 }
120
121 static void
122 gst_wavparse_class_init (GstWavParseClass * klass)
123 {
124   GstElementClass *gstelement_class;
125   GObjectClass *object_class;
126
127   gstelement_class = (GstElementClass *) klass;
128   object_class = (GObjectClass *) klass;
129
130   parent_class = g_type_class_peek_parent (klass);
131
132   object_class->dispose = gst_wavparse_dispose;
133
134   gstelement_class->change_state = gst_wavparse_change_state;
135   gstelement_class->send_event = gst_wavparse_send_event;
136 }
137
138 static void
139 gst_wavparse_reset (GstWavParse * wav)
140 {
141   wav->state = GST_WAVPARSE_START;
142
143   /* These will all be set correctly in the fmt chunk */
144   wav->depth = 0;
145   wav->rate = 0;
146   wav->width = 0;
147   wav->channels = 0;
148   wav->blockalign = 0;
149   wav->bps = 0;
150   wav->fact = 0;
151   wav->offset = 0;
152   wav->end_offset = 0;
153   wav->dataleft = 0;
154   wav->datasize = 0;
155   wav->datastart = 0;
156   wav->duration = 0;
157   wav->got_fmt = FALSE;
158   wav->first = TRUE;
159
160   if (wav->seek_event)
161     gst_event_unref (wav->seek_event);
162   wav->seek_event = NULL;
163   if (wav->adapter) {
164     gst_adapter_clear (wav->adapter);
165     g_object_unref (wav->adapter);
166     wav->adapter = NULL;
167   }
168   if (wav->tags)
169     gst_tag_list_free (wav->tags);
170   wav->tags = NULL;
171 }
172
173 static void
174 gst_wavparse_dispose (GObject * object)
175 {
176   GstWavParse *wav = GST_WAVPARSE (object);
177
178   GST_DEBUG_OBJECT (wav, "WAV: Dispose");
179   gst_wavparse_reset (wav);
180
181   G_OBJECT_CLASS (parent_class)->dispose (object);
182 }
183
184 static void
185 gst_wavparse_init (GstWavParse * wavparse, GstWavParseClass * g_class)
186 {
187   gst_wavparse_reset (wavparse);
188
189   /* sink */
190   wavparse->sinkpad =
191       gst_pad_new_from_static_template (&sink_template_factory, "sink");
192   gst_pad_set_activate_function (wavparse->sinkpad,
193       GST_DEBUG_FUNCPTR (gst_wavparse_sink_activate));
194   gst_pad_set_activatepull_function (wavparse->sinkpad,
195       GST_DEBUG_FUNCPTR (gst_wavparse_sink_activate_pull));
196   gst_pad_set_chain_function (wavparse->sinkpad,
197       GST_DEBUG_FUNCPTR (gst_wavparse_chain));
198   gst_pad_set_event_function (wavparse->sinkpad,
199       GST_DEBUG_FUNCPTR (gst_wavparse_sink_event));
200   gst_element_add_pad (GST_ELEMENT_CAST (wavparse), wavparse->sinkpad);
201
202   /* src, will be created later */
203   wavparse->srcpad = NULL;
204 }
205
206 static void
207 gst_wavparse_destroy_sourcepad (GstWavParse * wavparse)
208 {
209   if (wavparse->srcpad) {
210     gst_element_remove_pad (GST_ELEMENT_CAST (wavparse), wavparse->srcpad);
211     wavparse->srcpad = NULL;
212   }
213 }
214
215 static void
216 gst_wavparse_create_sourcepad (GstWavParse * wavparse)
217 {
218   GstElementClass *klass = GST_ELEMENT_GET_CLASS (wavparse);
219   GstPadTemplate *src_template;
220
221   /* destroy previous one */
222   gst_wavparse_destroy_sourcepad (wavparse);
223
224   /* source */
225   src_template = gst_element_class_get_pad_template (klass, "wavparse_src");
226   wavparse->srcpad = gst_pad_new_from_template (src_template, "src");
227   gst_pad_use_fixed_caps (wavparse->srcpad);
228   gst_pad_set_query_type_function (wavparse->srcpad,
229       GST_DEBUG_FUNCPTR (gst_wavparse_get_query_types));
230   gst_pad_set_query_function (wavparse->srcpad,
231       GST_DEBUG_FUNCPTR (gst_wavparse_pad_query));
232   gst_pad_set_event_function (wavparse->srcpad,
233       GST_DEBUG_FUNCPTR (gst_wavparse_srcpad_event));
234
235   GST_DEBUG_OBJECT (wavparse, "srcpad created");
236 }
237
238 /* Compute (value * nom) % denom, avoiding overflow.  This can be used
239  * to perform ceiling or rounding division together with
240  * gst_util_uint64_scale[_int]. */
241 #define uint64_scale_modulo(val, nom, denom) \
242   ((val % denom) * (nom % denom) % denom)
243
244 /* Like gst_util_uint64_scale, but performs ceiling division. */
245 static guint64
246 uint64_ceiling_scale_int (guint64 val, gint num, gint denom)
247 {
248   guint64 result = gst_util_uint64_scale_int (val, num, denom);
249
250   if (uint64_scale_modulo (val, num, denom) == 0)
251     return result;
252   else
253     return result + 1;
254 }
255
256 /* Like gst_util_uint64_scale, but performs ceiling division. */
257 static guint64
258 uint64_ceiling_scale (guint64 val, guint64 num, guint64 denom)
259 {
260   guint64 result = gst_util_uint64_scale (val, num, denom);
261
262   if (uint64_scale_modulo (val, num, denom) == 0)
263     return result;
264   else
265     return result + 1;
266 }
267
268
269 /* FIXME: why is that not in use? */
270 #if 0
271 static void
272 gst_wavparse_parse_adtl (GstWavParse * wavparse, int len)
273 {
274   guint32 got_bytes;
275   GstByteStream *bs = wavparse->bs;
276   gst_riff_chunk *temp_chunk, chunk;
277   guint8 *tempdata;
278   struct _gst_riff_labl labl, *temp_labl;
279   struct _gst_riff_ltxt ltxt, *temp_ltxt;
280   struct _gst_riff_note note, *temp_note;
281   char *label_name;
282   GstProps *props;
283   GstPropsEntry *entry;
284   GstCaps *new_caps;
285   GList *caps = NULL;
286
287   props = wavparse->metadata->properties;
288
289   while (len > 0) {
290     got_bytes =
291         gst_bytestream_peek_bytes (bs, &tempdata, sizeof (gst_riff_chunk));
292     if (got_bytes != sizeof (gst_riff_chunk)) {
293       return;
294     }
295     temp_chunk = (gst_riff_chunk *) tempdata;
296
297     chunk.id = GUINT32_FROM_LE (temp_chunk->id);
298     chunk.size = GUINT32_FROM_LE (temp_chunk->size);
299
300     if (chunk.size == 0) {
301       gst_bytestream_flush (bs, sizeof (gst_riff_chunk));
302       len -= sizeof (gst_riff_chunk);
303       continue;
304     }
305
306     switch (chunk.id) {
307       case GST_RIFF_adtl_labl:
308         got_bytes =
309             gst_bytestream_peek_bytes (bs, &tempdata,
310             sizeof (struct _gst_riff_labl));
311         if (got_bytes != sizeof (struct _gst_riff_labl)) {
312           return;
313         }
314
315         temp_labl = (struct _gst_riff_labl *) tempdata;
316         labl.id = GUINT32_FROM_LE (temp_labl->id);
317         labl.size = GUINT32_FROM_LE (temp_labl->size);
318         labl.identifier = GUINT32_FROM_LE (temp_labl->identifier);
319
320         gst_bytestream_flush (bs, sizeof (struct _gst_riff_labl));
321         len -= sizeof (struct _gst_riff_labl);
322
323         got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, labl.size - 4);
324         if (got_bytes != labl.size - 4) {
325           return;
326         }
327
328         label_name = (char *) tempdata;
329
330         gst_bytestream_flush (bs, ((labl.size - 4) + 1) & ~1);
331         len -= (((labl.size - 4) + 1) & ~1);
332
333         new_caps = gst_caps_new ("label",
334             "application/x-gst-metadata",
335             gst_props_new ("identifier", G_TYPE_INT (labl.identifier),
336                 "name", G_TYPE_STRING (label_name), NULL));
337
338         if (gst_props_get (props, "labels", &caps, NULL)) {
339           caps = g_list_append (caps, new_caps);
340         } else {
341           caps = g_list_append (NULL, new_caps);
342
343           entry = gst_props_entry_new ("labels", GST_PROPS_GLIST (caps));
344           gst_props_add_entry (props, entry);
345         }
346
347         break;
348
349       case GST_RIFF_adtl_ltxt:
350         got_bytes =
351             gst_bytestream_peek_bytes (bs, &tempdata,
352             sizeof (struct _gst_riff_ltxt));
353         if (got_bytes != sizeof (struct _gst_riff_ltxt)) {
354           return;
355         }
356
357         temp_ltxt = (struct _gst_riff_ltxt *) tempdata;
358         ltxt.id = GUINT32_FROM_LE (temp_ltxt->id);
359         ltxt.size = GUINT32_FROM_LE (temp_ltxt->size);
360         ltxt.identifier = GUINT32_FROM_LE (temp_ltxt->identifier);
361         ltxt.length = GUINT32_FROM_LE (temp_ltxt->length);
362         ltxt.purpose = GUINT32_FROM_LE (temp_ltxt->purpose);
363         ltxt.country = GUINT16_FROM_LE (temp_ltxt->country);
364         ltxt.language = GUINT16_FROM_LE (temp_ltxt->language);
365         ltxt.dialect = GUINT16_FROM_LE (temp_ltxt->dialect);
366         ltxt.codepage = GUINT16_FROM_LE (temp_ltxt->codepage);
367
368         gst_bytestream_flush (bs, sizeof (struct _gst_riff_ltxt));
369         len -= sizeof (struct _gst_riff_ltxt);
370
371         if (ltxt.size - 20 > 0) {
372           got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, ltxt.size - 20);
373           if (got_bytes != ltxt.size - 20) {
374             return;
375           }
376
377           gst_bytestream_flush (bs, ((ltxt.size - 20) + 1) & ~1);
378           len -= (((ltxt.size - 20) + 1) & ~1);
379
380           label_name = (char *) tempdata;
381         } else {
382           label_name = "";
383         }
384
385         new_caps = gst_caps_new ("ltxt",
386             "application/x-gst-metadata",
387             gst_props_new ("identifier", G_TYPE_INT (ltxt.identifier),
388                 "name", G_TYPE_STRING (label_name),
389                 "length", G_TYPE_INT (ltxt.length), NULL));
390
391         if (gst_props_get (props, "ltxts", &caps, NULL)) {
392           caps = g_list_append (caps, new_caps);
393         } else {
394           caps = g_list_append (NULL, new_caps);
395
396           entry = gst_props_entry_new ("ltxts", GST_PROPS_GLIST (caps));
397           gst_props_add_entry (props, entry);
398         }
399
400         break;
401
402       case GST_RIFF_adtl_note:
403         got_bytes =
404             gst_bytestream_peek_bytes (bs, &tempdata,
405             sizeof (struct _gst_riff_note));
406         if (got_bytes != sizeof (struct _gst_riff_note)) {
407           return;
408         }
409
410         temp_note = (struct _gst_riff_note *) tempdata;
411         note.id = GUINT32_FROM_LE (temp_note->id);
412         note.size = GUINT32_FROM_LE (temp_note->size);
413         note.identifier = GUINT32_FROM_LE (temp_note->identifier);
414
415         gst_bytestream_flush (bs, sizeof (struct _gst_riff_note));
416         len -= sizeof (struct _gst_riff_note);
417
418         got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, note.size - 4);
419         if (got_bytes != note.size - 4) {
420           return;
421         }
422
423         gst_bytestream_flush (bs, ((note.size - 4) + 1) & ~1);
424         len -= (((note.size - 4) + 1) & ~1);
425
426         label_name = (char *) tempdata;
427
428         new_caps = gst_caps_new ("note",
429             "application/x-gst-metadata",
430             gst_props_new ("identifier", G_TYPE_INT (note.identifier),
431                 "name", G_TYPE_STRING (label_name), NULL));
432
433         if (gst_props_get (props, "notes", &caps, NULL)) {
434           caps = g_list_append (caps, new_caps);
435         } else {
436           caps = g_list_append (NULL, new_caps);
437
438           entry = gst_props_entry_new ("notes", GST_PROPS_GLIST (caps));
439           gst_props_add_entry (props, entry);
440         }
441
442         break;
443
444       default:
445         g_print ("Unknown chunk: %" GST_FOURCC_FORMAT "\n",
446             GST_FOURCC_ARGS (chunk.id));
447         return;
448     }
449   }
450
451   g_object_notify (G_OBJECT (wavparse), "metadata");
452 }
453
454 static void
455 gst_wavparse_parse_cues (GstWavParse * wavparse, int len)
456 {
457   guint32 got_bytes;
458   GstByteStream *bs = wavparse->bs;
459   struct _gst_riff_cue *temp_cue, cue;
460   struct _gst_riff_cuepoints *points;
461   guint8 *tempdata;
462   int i;
463   GList *cues = NULL;
464   GstPropsEntry *entry;
465
466   while (len > 0) {
467     int required;
468
469     got_bytes =
470         gst_bytestream_peek_bytes (bs, &tempdata,
471         sizeof (struct _gst_riff_cue));
472     temp_cue = (struct _gst_riff_cue *) tempdata;
473
474     /* fixup for our big endian friends */
475     cue.id = GUINT32_FROM_LE (temp_cue->id);
476     cue.size = GUINT32_FROM_LE (temp_cue->size);
477     cue.cuepoints = GUINT32_FROM_LE (temp_cue->cuepoints);
478
479     gst_bytestream_flush (bs, sizeof (struct _gst_riff_cue));
480     if (got_bytes != sizeof (struct _gst_riff_cue)) {
481       return;
482     }
483
484     len -= sizeof (struct _gst_riff_cue);
485
486     /* -4 because cue.size contains the cuepoints size
487        and we've already flushed that out of the system */
488     required = cue.size - 4;
489     got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, required);
490     gst_bytestream_flush (bs, ((required) + 1) & ~1);
491     if (got_bytes != required) {
492       return;
493     }
494
495     len -= (((cue.size - 4) + 1) & ~1);
496
497     /* now we have an array of struct _gst_riff_cuepoints in tempdata */
498     points = (struct _gst_riff_cuepoints *) tempdata;
499
500     for (i = 0; i < cue.cuepoints; i++) {
501       GstCaps *caps;
502
503       caps = gst_caps_new ("cues",
504           "application/x-gst-metadata",
505           gst_props_new ("identifier", G_TYPE_INT (points[i].identifier),
506               "position", G_TYPE_INT (points[i].offset), NULL));
507       cues = g_list_append (cues, caps);
508     }
509
510     entry = gst_props_entry_new ("cues", GST_PROPS_GLIST (cues));
511     gst_props_add_entry (wavparse->metadata->properties, entry);
512   }
513
514   g_object_notify (G_OBJECT (wavparse), "metadata");
515 }
516
517 /* Read 'fmt ' header */
518 static gboolean
519 gst_wavparse_fmt (GstWavParse * wav)
520 {
521   gst_riff_strf_auds *header = NULL;
522   GstCaps *caps;
523
524   if (!gst_riff_read_strf_auds (wav, &header))
525     goto no_fmt;
526
527   wav->format = header->format;
528   wav->rate = header->rate;
529   wav->channels = header->channels;
530   if (wav->channels == 0)
531     goto no_channels;
532
533   wav->blockalign = header->blockalign;
534   wav->width = (header->blockalign * 8) / header->channels;
535   wav->depth = header->size;
536   wav->bps = header->av_bps;
537   if (wav->bps <= 0)
538     goto no_bps;
539
540   /* Note: gst_riff_create_audio_caps might need to fix values in
541    * the header header depending on the format, so call it first */
542   caps = gst_riff_create_audio_caps (header->format, NULL, header, NULL);
543   g_free (header);
544
545   if (caps == NULL)
546     goto no_caps;
547
548   gst_wavparse_create_sourcepad (wav);
549   gst_pad_use_fixed_caps (wav->srcpad);
550   gst_pad_set_active (wav->srcpad, TRUE);
551   gst_pad_set_caps (wav->srcpad, caps);
552   gst_caps_free (caps);
553   gst_element_add_pad (GST_ELEMENT_CAST (wav), wav->srcpad);
554   gst_element_no_more_pads (GST_ELEMENT_CAST (wav));
555
556   GST_DEBUG ("frequency %d, channels %d", wav->rate, wav->channels);
557
558   return TRUE;
559
560   /* ERRORS */
561 no_fmt:
562   {
563     GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL),
564         ("No FMT tag found"));
565     return FALSE;
566   }
567 no_channels:
568   {
569     GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
570         ("Stream claims to contain zero channels - invalid data"));
571     g_free (header);
572     return FALSE;
573   }
574 no_bps:
575   {
576     GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
577         ("Stream claims to bitrate of <= zero - invalid data"));
578     g_free (header);
579     return FALSE;
580   }
581 no_caps:
582   {
583     GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL), (NULL));
584     return FALSE;
585   }
586 }
587
588 static gboolean
589 gst_wavparse_other (GstWavParse * wav)
590 {
591   guint32 tag, length;
592
593   if (!gst_riff_peek_head (wav, &tag, &length, NULL)) {
594     GST_WARNING_OBJECT (wav, "could not peek head");
595     return FALSE;
596   }
597   GST_DEBUG_OBJECT (wav, "got tag (%08x) %4.4s, length %d", tag,
598       (gchar *) & tag, length);
599
600   switch (tag) {
601     case GST_RIFF_TAG_LIST:
602       if (!(tag = gst_riff_peek_list (wav))) {
603         GST_WARNING_OBJECT (wav, "could not peek list");
604         return FALSE;
605       }
606
607       switch (tag) {
608         case GST_RIFF_LIST_INFO:
609           if (!gst_riff_read_list (wav, &tag) || !gst_riff_read_info (wav)) {
610             GST_WARNING_OBJECT (wav, "could not read list");
611             return FALSE;
612           }
613           break;
614
615         case GST_RIFF_LIST_adtl:
616           if (!gst_riff_read_skip (wav)) {
617             GST_WARNING_OBJECT (wav, "could not read skip");
618             return FALSE;
619           }
620           break;
621
622         default:
623           GST_DEBUG_OBJECT (wav, "skipping tag (%08x) %4.4s", tag,
624               (gchar *) & tag);
625           if (!gst_riff_read_skip (wav)) {
626             GST_WARNING_OBJECT (wav, "could not read skip");
627             return FALSE;
628           }
629           break;
630       }
631
632       break;
633
634     case GST_RIFF_TAG_data:
635       if (!gst_bytestream_flush (wav->bs, 8)) {
636         GST_WARNING_OBJECT (wav, "could not flush 8 bytes");
637         return FALSE;
638       }
639
640       GST_DEBUG_OBJECT (wav, "switching to data mode");
641       wav->state = GST_WAVPARSE_DATA;
642       wav->datastart = gst_bytestream_tell (wav->bs);
643       if (length == 0) {
644         guint64 file_length;
645
646         /* length is 0, data probably stretches to the end
647          * of file */
648         GST_DEBUG_OBJECT (wav, "length is 0 trying to find length");
649         /* get length of file */
650         file_length = gst_bytestream_length (wav->bs);
651         if (file_length == -1) {
652           GST_DEBUG_OBJECT (wav,
653               "could not get file length, assuming data to eof");
654           /* could not get length, assuming till eof */
655           length = G_MAXUINT32;
656         }
657         if (file_length > G_MAXUINT32) {
658           GST_DEBUG_OBJECT (wav, "file length %lld, clipping to 32 bits");
659           /* could not get length, assuming till eof */
660           length = G_MAXUINT32;
661         } else {
662           GST_DEBUG_OBJECT (wav, "file length %lld, datalength",
663               file_length, length);
664           /* substract offset of datastart from length */
665           length = file_length - wav->datastart;
666           GST_DEBUG_OBJECT (wav, "datalength %lld", length);
667         }
668       }
669       wav->datasize = (guint64) length;
670       GST_DEBUG_OBJECT (wav, "datasize = %ld", length)
671           break;
672
673     case GST_RIFF_TAG_cue:
674       if (!gst_riff_read_skip (wav)) {
675         GST_WARNING_OBJECT (wav, "could not read skip");
676         return FALSE;
677       }
678       break;
679
680     default:
681       GST_DEBUG_OBJECT (wav, "skipping tag (%08x) %4.4s", tag, (gchar *) & tag);
682       if (!gst_riff_read_skip (wav))
683         return FALSE;
684       break;
685   }
686
687   return TRUE;
688 }
689 #endif
690
691
692 static gboolean
693 gst_wavparse_parse_file_header (GstElement * element, GstBuffer * buf)
694 {
695   guint32 doctype;
696
697   if (!gst_riff_parse_file_header (element, buf, &doctype))
698     return FALSE;
699
700   if (doctype != GST_RIFF_RIFF_WAVE)
701     goto not_wav;
702
703   return TRUE;
704
705   /* ERRORS */
706 not_wav:
707   {
708     GST_ELEMENT_ERROR (element, STREAM, WRONG_TYPE, (NULL),
709         ("File is not a WAVE file: %" GST_FOURCC_FORMAT,
710             GST_FOURCC_ARGS (doctype)));
711     return FALSE;
712   }
713 }
714
715 static GstFlowReturn
716 gst_wavparse_stream_init (GstWavParse * wav)
717 {
718   GstFlowReturn res;
719   GstBuffer *buf = NULL;
720
721   if ((res = gst_pad_pull_range (wav->sinkpad,
722               wav->offset, 12, &buf)) != GST_FLOW_OK)
723     return res;
724   else if (!gst_wavparse_parse_file_header (GST_ELEMENT_CAST (wav), buf))
725     return GST_FLOW_ERROR;
726
727   wav->offset += 12;
728
729   return GST_FLOW_OK;
730 }
731
732 static gboolean
733 gst_wavparse_time_to_bytepos (GstWavParse * wav, gint64 ts, gint64 * bytepos)
734 {
735   /* -1 always maps to -1 */
736   if (ts == -1) {
737     *bytepos = -1;
738     return TRUE;
739   }
740
741   /* 0 always maps to 0 */
742   if (ts == 0) {
743     *bytepos = 0;
744     return TRUE;
745   }
746
747   if (wav->bps > 0) {
748     *bytepos = uint64_ceiling_scale (ts, (guint64) wav->bps, GST_SECOND);
749     return TRUE;
750   } else if (wav->fact) {
751     guint64 bps =
752         gst_util_uint64_scale_int (wav->datasize, wav->rate, wav->fact);
753     *bytepos = uint64_ceiling_scale (ts, bps, GST_SECOND);
754     return TRUE;
755   }
756
757   return FALSE;
758 }
759
760 /* This function is used to perform seeks on the element.
761  *
762  * It also works when event is NULL, in which case it will just
763  * start from the last configured segment. This technique is
764  * used when activating the element and to perform the seek in
765  * READY.
766  */
767 static gboolean
768 gst_wavparse_perform_seek (GstWavParse * wav, GstEvent * event)
769 {
770   gboolean res;
771   gdouble rate;
772   GstFormat format, bformat;
773   GstSeekFlags flags;
774   GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
775   gint64 cur, stop, upstream_size;
776   gboolean flush;
777   gboolean update;
778   GstSegment seeksegment = { 0, };
779   gint64 last_stop;
780
781   if (event) {
782     GST_DEBUG_OBJECT (wav, "doing seek with event");
783
784     gst_event_parse_seek (event, &rate, &format, &flags,
785         &cur_type, &cur, &stop_type, &stop);
786
787     /* no negative rates yet */
788     if (rate < 0.0)
789       goto negative_rate;
790
791     if (format != wav->segment.format) {
792       GST_INFO_OBJECT (wav, "converting seek-event from %s to %s",
793           gst_format_get_name (format),
794           gst_format_get_name (wav->segment.format));
795       res = TRUE;
796       if (cur_type != GST_SEEK_TYPE_NONE)
797         res =
798             gst_pad_query_convert (wav->srcpad, format, cur,
799             &wav->segment.format, &cur);
800       if (res && stop_type != GST_SEEK_TYPE_NONE)
801         res =
802             gst_pad_query_convert (wav->srcpad, format, stop,
803             &wav->segment.format, &stop);
804       if (!res)
805         goto no_format;
806
807       format = wav->segment.format;
808     }
809   } else {
810     GST_DEBUG_OBJECT (wav, "doing seek without event");
811     flags = 0;
812     rate = 1.0;
813     cur_type = GST_SEEK_TYPE_SET;
814     stop_type = GST_SEEK_TYPE_SET;
815   }
816
817   /* in push mode, we must delegate to upstream */
818   if (wav->streaming) {
819     gboolean res = FALSE;
820
821     /* if streaming not yet started; only prepare initial newsegment */
822     if (!event || wav->state != GST_WAVPARSE_DATA) {
823       if (wav->start_segment)
824         gst_event_unref (wav->start_segment);
825       wav->start_segment =
826           gst_event_new_new_segment (FALSE, wav->segment.rate,
827           wav->segment.format, wav->segment.last_stop, wav->segment.duration,
828           wav->segment.last_stop);
829       res = TRUE;
830     } else {
831       /* convert seek positions to byte positions in data sections */
832       if (format == GST_FORMAT_TIME) {
833         /* should not fail */
834         if (!gst_wavparse_time_to_bytepos (wav, cur, &cur))
835           goto no_position;
836         if (!gst_wavparse_time_to_bytepos (wav, stop, &stop))
837           goto no_position;
838       }
839       /* mind sample boundary and header */
840       if (cur >= 0) {
841         cur -= (cur % wav->bytes_per_sample);
842         cur += wav->datastart;
843       }
844       if (stop >= 0) {
845         stop -= (stop % wav->bytes_per_sample);
846         stop += wav->datastart;
847       }
848       GST_DEBUG_OBJECT (wav, "Pushing BYTE seek rate %g, "
849           "start %" G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT, rate, cur,
850           stop);
851       /* BYTE seek event */
852       event = gst_event_new_seek (rate, GST_FORMAT_BYTES, flags, cur_type, cur,
853           stop_type, stop);
854       res = gst_pad_push_event (wav->sinkpad, event);
855     }
856     return res;
857   }
858
859   /* get flush flag */
860   flush = flags & GST_SEEK_FLAG_FLUSH;
861
862   /* now we need to make sure the streaming thread is stopped. We do this by
863    * either sending a FLUSH_START event downstream which will cause the
864    * streaming thread to stop with a WRONG_STATE.
865    * For a non-flushing seek we simply pause the task, which will happen as soon
866    * as it completes one iteration (and thus might block when the sink is
867    * blocking in preroll). */
868   if (flush) {
869     if (wav->srcpad) {
870       GST_DEBUG_OBJECT (wav, "sending flush start");
871       gst_pad_push_event (wav->srcpad, gst_event_new_flush_start ());
872     }
873   } else {
874     gst_pad_pause_task (wav->sinkpad);
875   }
876
877   /* we should now be able to grab the streaming thread because we stopped it
878    * with the above flush/pause code */
879   GST_PAD_STREAM_LOCK (wav->sinkpad);
880
881   /* save current position */
882   last_stop = wav->segment.last_stop;
883
884   GST_DEBUG_OBJECT (wav, "stopped streaming at %" G_GINT64_FORMAT, last_stop);
885
886   /* copy segment, we need this because we still need the old
887    * segment when we close the current segment. */
888   memcpy (&seeksegment, &wav->segment, sizeof (GstSegment));
889
890   /* configure the seek parameters in the seeksegment. We will then have the
891    * right values in the segment to perform the seek */
892   if (event) {
893     GST_DEBUG_OBJECT (wav, "configuring seek");
894     gst_segment_set_seek (&seeksegment, rate, format, flags,
895         cur_type, cur, stop_type, stop, &update);
896   }
897
898   /* figure out the last position we need to play. If it's configured (stop !=
899    * -1), use that, else we play until the total duration of the file */
900   if ((stop = seeksegment.stop) == -1)
901     stop = seeksegment.duration;
902
903   GST_DEBUG_OBJECT (wav, "cur_type =%d", cur_type);
904   if ((cur_type != GST_SEEK_TYPE_NONE)) {
905     /* bring offset to bytes, if the bps is 0, we have the segment in BYTES and
906      * we can just copy the last_stop. If not, we use the bps to convert TIME to
907      * bytes. */
908     if (!gst_wavparse_time_to_bytepos (wav, seeksegment.last_stop,
909             (gint64 *) & wav->offset))
910       wav->offset = seeksegment.last_stop;
911     GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
912     wav->offset -= (wav->offset % wav->bytes_per_sample);
913     GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
914     wav->offset += wav->datastart;
915     GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
916   } else {
917     GST_LOG_OBJECT (wav, "continue from offset=%" G_GUINT64_FORMAT,
918         wav->offset);
919   }
920
921   if (stop_type != GST_SEEK_TYPE_NONE) {
922     if (!gst_wavparse_time_to_bytepos (wav, stop, (gint64 *) & wav->end_offset))
923       wav->end_offset = stop;
924     GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
925     wav->end_offset -= (wav->end_offset % wav->bytes_per_sample);
926     GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
927     wav->end_offset += wav->datastart;
928     GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
929   } else {
930     GST_LOG_OBJECT (wav, "continue to end_offset=%" G_GUINT64_FORMAT,
931         wav->end_offset);
932   }
933
934   /* make sure filesize is not exceeded due to rounding errors or so,
935    * same precaution as in _stream_headers */
936   bformat = GST_FORMAT_BYTES;
937   if (gst_pad_query_peer_duration (wav->sinkpad, &bformat, &upstream_size))
938     wav->end_offset = MIN (wav->end_offset, upstream_size);
939
940   /* this is the range of bytes we will use for playback */
941   wav->offset = MIN (wav->offset, wav->end_offset);
942   wav->dataleft = wav->end_offset - wav->offset;
943
944   GST_DEBUG_OBJECT (wav,
945       "seek: rate %lf, offset %" G_GUINT64_FORMAT ", end %" G_GUINT64_FORMAT
946       ", segment %" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT, rate, wav->offset,
947       wav->end_offset, GST_TIME_ARGS (seeksegment.start), GST_TIME_ARGS (stop));
948
949   /* prepare for streaming again */
950   if (wav->srcpad) {
951     if (flush) {
952       /* if we sent a FLUSH_START, we now send a FLUSH_STOP */
953       GST_DEBUG_OBJECT (wav, "sending flush stop");
954       gst_pad_push_event (wav->srcpad, gst_event_new_flush_stop ());
955     } else if (wav->segment_running) {
956       /* we are running the current segment and doing a non-flushing seek,
957        * close the segment first based on the previous last_stop. */
958       GST_DEBUG_OBJECT (wav, "closing running segment %" G_GINT64_FORMAT
959           " to %" G_GINT64_FORMAT, wav->segment.accum, wav->segment.last_stop);
960
961       /* queue the segment for sending in the stream thread */
962       if (wav->close_segment)
963         gst_event_unref (wav->close_segment);
964       wav->close_segment = gst_event_new_new_segment (TRUE,
965           wav->segment.rate, wav->segment.format,
966           wav->segment.accum, wav->segment.last_stop, wav->segment.accum);
967
968       /* keep track of our last_stop */
969       seeksegment.accum = wav->segment.last_stop;
970     }
971   }
972
973   /* now we did the seek and can activate the new segment values */
974   memcpy (&wav->segment, &seeksegment, sizeof (GstSegment));
975
976   /* if we're doing a segment seek, post a SEGMENT_START message */
977   if (wav->segment.flags & GST_SEEK_FLAG_SEGMENT) {
978     gst_element_post_message (GST_ELEMENT_CAST (wav),
979         gst_message_new_segment_start (GST_OBJECT_CAST (wav),
980             wav->segment.format, wav->segment.last_stop));
981   }
982
983   /* now create the newsegment */
984   GST_DEBUG_OBJECT (wav, "Creating newsegment from %" G_GINT64_FORMAT
985       " to %" G_GINT64_FORMAT, wav->segment.last_stop, stop);
986
987   /* store the newsegment event so it can be sent from the streaming thread. */
988   if (wav->start_segment)
989     gst_event_unref (wav->start_segment);
990   wav->start_segment =
991       gst_event_new_new_segment (FALSE, wav->segment.rate,
992       wav->segment.format, wav->segment.last_stop, stop,
993       wav->segment.last_stop);
994
995   /* mark discont if we are going to stream from another position. */
996   if (last_stop != wav->segment.last_stop) {
997     GST_DEBUG_OBJECT (wav, "mark DISCONT, we did a seek to another position");
998     wav->discont = TRUE;
999   }
1000
1001   /* and start the streaming task again */
1002   wav->segment_running = TRUE;
1003   if (!wav->streaming) {
1004     gst_pad_start_task (wav->sinkpad, (GstTaskFunction) gst_wavparse_loop,
1005         wav->sinkpad);
1006   }
1007
1008   GST_PAD_STREAM_UNLOCK (wav->sinkpad);
1009
1010   return TRUE;
1011
1012   /* ERRORS */
1013 negative_rate:
1014   {
1015     GST_DEBUG_OBJECT (wav, "negative playback rates are not supported yet.");
1016     return FALSE;
1017   }
1018 no_format:
1019   {
1020     GST_DEBUG_OBJECT (wav, "unsupported format given, seek aborted.");
1021     return FALSE;
1022   }
1023 no_position:
1024   {
1025     GST_DEBUG_OBJECT (wav,
1026         "Could not determine byte position for desired time");
1027     return FALSE;
1028   }
1029 }
1030
1031 /*
1032  * gst_wavparse_peek_chunk_info:
1033  * @wav Wavparse object
1034  * @tag holder for tag
1035  * @size holder for tag size
1036  *
1037  * Peek next chunk info (tag and size)
1038  *
1039  * Returns: %TRUE when the chunk info (header) is available
1040  */
1041 static gboolean
1042 gst_wavparse_peek_chunk_info (GstWavParse * wav, guint32 * tag, guint32 * size)
1043 {
1044   const guint8 *data = NULL;
1045
1046   if (gst_adapter_available (wav->adapter) < 8)
1047     return FALSE;
1048
1049   data = gst_adapter_peek (wav->adapter, 8);
1050   *tag = GST_READ_UINT32_LE (data);
1051   *size = GST_READ_UINT32_LE (data + 4);
1052
1053   GST_DEBUG ("Next chunk size is %d bytes, type %" GST_FOURCC_FORMAT, *size,
1054       GST_FOURCC_ARGS (*tag));
1055
1056   return TRUE;
1057 }
1058
1059 /*
1060  * gst_wavparse_peek_chunk:
1061  * @wav Wavparse object
1062  * @tag holder for tag
1063  * @size holder for tag size
1064  *
1065  * Peek enough data for one full chunk
1066  *
1067  * Returns: %TRUE when the full chunk is available
1068  */
1069 static gboolean
1070 gst_wavparse_peek_chunk (GstWavParse * wav, guint32 * tag, guint32 * size)
1071 {
1072   guint32 peek_size = 0;
1073   guint available;
1074
1075   if (!gst_wavparse_peek_chunk_info (wav, tag, size))
1076     return FALSE;
1077
1078   GST_DEBUG ("Need to peek chunk of %d bytes", *size);
1079   peek_size = (*size + 1) & ~1;
1080
1081   available = gst_adapter_available (wav->adapter);
1082   if (available >= (8 + peek_size)) {
1083     return TRUE;
1084   } else {
1085     GST_LOG ("but only %u bytes available now", available);
1086     return FALSE;
1087   }
1088 }
1089
1090 /*
1091  * gst_wavparse_calculate_duration:
1092  * @wav: wavparse object
1093  *
1094  * Calculate duration on demand and store in @wav. Prefer bps, but use fact as a
1095  * fallback.
1096  *
1097  * Returns: %TRUE if duration is available.
1098  */
1099 static gboolean
1100 gst_wavparse_calculate_duration (GstWavParse * wav)
1101 {
1102   if (wav->duration > 0)
1103     return TRUE;
1104
1105   if (wav->bps > 0) {
1106     GST_INFO_OBJECT (wav, "Got datasize %" G_GUINT64_FORMAT, wav->datasize);
1107     wav->duration =
1108         uint64_ceiling_scale (wav->datasize, GST_SECOND, (guint64) wav->bps);
1109     GST_INFO_OBJECT (wav, "Got duration (bps) %" GST_TIME_FORMAT,
1110         GST_TIME_ARGS (wav->duration));
1111     return TRUE;
1112   } else if (wav->fact) {
1113     wav->duration = uint64_ceiling_scale_int (GST_SECOND, wav->fact, wav->rate);
1114     GST_INFO_OBJECT (wav, "Got duration (fact) %" GST_TIME_FORMAT,
1115         GST_TIME_ARGS (wav->duration));
1116     return TRUE;
1117   }
1118   return FALSE;
1119 }
1120
1121 static void
1122 gst_waveparse_ignore_chunk (GstWavParse * wav, GstBuffer * buf, guint32 tag,
1123     guint32 size)
1124 {
1125   guint flush;
1126
1127   if (wav->streaming) {
1128     if (!gst_wavparse_peek_chunk (wav, &tag, &size))
1129       return;
1130   }
1131   GST_DEBUG_OBJECT (wav, "Ignoring tag %" GST_FOURCC_FORMAT,
1132       GST_FOURCC_ARGS (tag));
1133   flush = 8 + ((size + 1) & ~1);
1134   wav->offset += flush;
1135   if (wav->streaming) {
1136     gst_adapter_flush (wav->adapter, flush);
1137   } else {
1138     gst_buffer_unref (buf);
1139   }
1140 }
1141
1142 static GstFlowReturn
1143 gst_wavparse_stream_headers (GstWavParse * wav)
1144 {
1145   GstFlowReturn res;
1146   GstBuffer *buf;
1147   gst_riff_strf_auds *header = NULL;
1148   guint32 tag, size;
1149   gboolean gotdata = FALSE;
1150   GstCaps *caps;
1151   gchar *codec_name = NULL;
1152   GstEvent **event_p;
1153   GstFormat bformat;
1154   gint64 upstream_size = 0;
1155
1156   /* search for "_fmt" chunk, which should be first */
1157   while (!wav->got_fmt) {
1158     GstBuffer *extra;
1159
1160     /* The header starts with a 'fmt ' tag */
1161     if (wav->streaming) {
1162       if (!gst_wavparse_peek_chunk (wav, &tag, &size))
1163         return GST_FLOW_OK;
1164
1165       gst_adapter_flush (wav->adapter, 8);
1166       wav->offset += 8;
1167
1168       buf = gst_adapter_take_buffer (wav->adapter, size);
1169     } else {
1170       if ((res = gst_riff_read_chunk (GST_ELEMENT_CAST (wav), wav->sinkpad,
1171                   &wav->offset, &tag, &buf)) != GST_FLOW_OK)
1172         return res;
1173     }
1174
1175     if (tag == GST_RIFF_TAG_JUNK || tag == GST_RIFF_TAG_bext ||
1176         tag == GST_RIFF_TAG_BEXT || tag == GST_RIFF_TAG_LIST) {
1177       GST_DEBUG_OBJECT (wav, "skipping %" GST_FOURCC_FORMAT " chunk",
1178           GST_FOURCC_ARGS (tag));
1179       gst_buffer_unref (buf);
1180       buf = NULL;
1181       continue;
1182     }
1183
1184     if (tag != GST_RIFF_TAG_fmt)
1185       goto invalid_wav;
1186
1187     if (!(gst_riff_parse_strf_auds (GST_ELEMENT_CAST (wav), buf, &header,
1188                 &extra)))
1189       goto parse_header_error;
1190
1191     buf = NULL;                 /* parse_strf_auds() took ownership of buffer */
1192
1193     /* do sanity checks of header fields */
1194     if (header->channels == 0)
1195       goto no_channels;
1196     if (header->rate == 0)
1197       goto no_rate;
1198
1199     GST_DEBUG_OBJECT (wav, "creating the caps");
1200
1201     /* Note: gst_riff_create_audio_caps might need to fix values in
1202      * the header header depending on the format, so call it first */
1203     caps = gst_riff_create_audio_caps (header->format, NULL, header, extra,
1204         NULL, &codec_name);
1205
1206     if (extra)
1207       gst_buffer_unref (extra);
1208
1209     if (!caps)
1210       goto unknown_format;
1211
1212     /* do more sanity checks of header fields
1213      * (these can be sanitized by gst_riff_create_audio_caps()
1214      */
1215     wav->format = header->format;
1216     wav->rate = header->rate;
1217     wav->channels = header->channels;
1218     wav->blockalign = header->blockalign;
1219     wav->depth = header->size;
1220     wav->av_bps = header->av_bps;
1221     wav->vbr = FALSE;
1222
1223     g_free (header);
1224     header = NULL;
1225
1226     /* do format specific handling */
1227     switch (wav->format) {
1228       case GST_RIFF_WAVE_FORMAT_MPEGL12:
1229       case GST_RIFF_WAVE_FORMAT_MPEGL3:
1230       {
1231         /* Note: workaround for mp2/mp3 embedded in wav, that relies on the
1232          * bitrate inside the mpeg stream */
1233         GST_INFO ("resetting bps from %d to 0 for mp2/3", wav->av_bps);
1234         wav->bps = 0;
1235         break;
1236       }
1237       case GST_RIFF_WAVE_FORMAT_PCM:
1238         if (wav->blockalign > wav->channels * (guint) ceil (wav->depth / 8.0))
1239           goto invalid_blockalign;
1240         /* fall through */
1241       default:
1242         if (wav->av_bps > wav->blockalign * wav->rate)
1243           goto invalid_bps;
1244         /* use the configured bps */
1245         wav->bps = wav->av_bps;
1246         break;
1247     }
1248
1249     wav->width = (wav->blockalign * 8) / wav->channels;
1250     wav->bytes_per_sample = wav->channels * wav->width / 8;
1251
1252     if (wav->bytes_per_sample <= 0)
1253       goto no_bytes_per_sample;
1254
1255     GST_DEBUG_OBJECT (wav, "blockalign = %u", (guint) wav->blockalign);
1256     GST_DEBUG_OBJECT (wav, "width      = %u", (guint) wav->width);
1257     GST_DEBUG_OBJECT (wav, "depth      = %u", (guint) wav->depth);
1258     GST_DEBUG_OBJECT (wav, "av_bps     = %u", (guint) wav->av_bps);
1259     GST_DEBUG_OBJECT (wav, "frequency  = %u", (guint) wav->rate);
1260     GST_DEBUG_OBJECT (wav, "channels   = %u", (guint) wav->channels);
1261     GST_DEBUG_OBJECT (wav, "bytes_per_sample = %u", wav->bytes_per_sample);
1262
1263     /* bps can be 0 when we don't have a valid bitrate (mostly for compressed
1264      * formats). This will make the element output a BYTE format segment and
1265      * will not timestamp the outgoing buffers.
1266      */
1267     GST_DEBUG_OBJECT (wav, "bps        = %u", (guint) wav->bps);
1268
1269     GST_DEBUG_OBJECT (wav, "caps = %" GST_PTR_FORMAT, caps);
1270
1271     /* create pad later so we can sniff the first few bytes
1272      * of the real data and correct our caps if necessary */
1273     gst_caps_replace (&wav->caps, caps);
1274     gst_caps_replace (&caps, NULL);
1275
1276     wav->got_fmt = TRUE;
1277
1278     if (codec_name) {
1279       wav->tags = gst_tag_list_new ();
1280
1281       gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1282           GST_TAG_AUDIO_CODEC, codec_name, NULL);
1283
1284       g_free (codec_name);
1285       codec_name = NULL;
1286     }
1287
1288   }
1289
1290   bformat = GST_FORMAT_BYTES;
1291   gst_pad_query_peer_duration (wav->sinkpad, &bformat, &upstream_size);
1292   GST_DEBUG_OBJECT (wav, "upstream size %" G_GUINT64_FORMAT, upstream_size);
1293
1294   /* loop headers until we get data */
1295   while (!gotdata) {
1296     if (wav->streaming) {
1297       if (!gst_wavparse_peek_chunk_info (wav, &tag, &size))
1298         return GST_FLOW_OK;
1299     } else {
1300       if ((res =
1301               gst_pad_pull_range (wav->sinkpad, wav->offset, 8,
1302                   &buf)) != GST_FLOW_OK)
1303         goto header_read_error;
1304       tag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf));
1305       size = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 4);
1306     }
1307
1308     GST_INFO_OBJECT (wav,
1309         "Got TAG: %" GST_FOURCC_FORMAT ", offset %" G_GUINT64_FORMAT,
1310         GST_FOURCC_ARGS (tag), wav->offset);
1311
1312     /* wav is a st00pid format, we don't know for sure where data starts.
1313      * So we have to go bit by bit until we find the 'data' header
1314      */
1315     switch (tag) {
1316       case GST_RIFF_TAG_data:{
1317         GST_DEBUG_OBJECT (wav, "Got 'data' TAG, size : %d", size);
1318         if (wav->streaming) {
1319           gst_adapter_flush (wav->adapter, 8);
1320           gotdata = TRUE;
1321         } else {
1322           gst_buffer_unref (buf);
1323         }
1324         wav->offset += 8;
1325         wav->datastart = wav->offset;
1326         /* file might be truncated */
1327         if (upstream_size) {
1328           size = MIN (size, (upstream_size - wav->datastart));
1329         }
1330         wav->datasize = (guint64) size;
1331         wav->dataleft = (guint64) size;
1332         wav->end_offset = size + wav->datastart;
1333         if (!wav->streaming) {
1334           /* We will continue parsing tags 'till end */
1335           wav->offset += size;
1336         }
1337         GST_DEBUG_OBJECT (wav, "datasize = %d", size);
1338         break;
1339       }
1340       case GST_RIFF_TAG_fact:{
1341         if (wav->format != GST_RIFF_WAVE_FORMAT_MPEGL12 &&
1342             wav->format != GST_RIFF_WAVE_FORMAT_MPEGL3) {
1343           const guint data_size = 4;
1344
1345           /* number of samples (for compressed formats) */
1346           if (wav->streaming) {
1347             const guint8 *data = NULL;
1348
1349             if (gst_adapter_available (wav->adapter) < 8 + data_size) {
1350               return GST_FLOW_OK;
1351             }
1352             gst_adapter_flush (wav->adapter, 8);
1353             data = gst_adapter_peek (wav->adapter, data_size);
1354             wav->fact = GST_READ_UINT32_LE (data);
1355             gst_adapter_flush (wav->adapter, data_size);
1356           } else {
1357             gst_buffer_unref (buf);
1358             if ((res =
1359                     gst_pad_pull_range (wav->sinkpad, wav->offset + 8,
1360                         data_size, &buf)) != GST_FLOW_OK)
1361               goto header_read_error;
1362             wav->fact = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf));
1363             gst_buffer_unref (buf);
1364           }
1365           GST_DEBUG_OBJECT (wav, "have fact %u", wav->fact);
1366           wav->offset += 8 + data_size;
1367           break;
1368         } else {
1369           gst_waveparse_ignore_chunk (wav, buf, tag, size);
1370         }
1371         break;
1372       }
1373       case GST_RIFF_TAG_acid:{
1374         const gst_riff_acid *acid = NULL;
1375         const guint data_size = sizeof (gst_riff_acid);
1376
1377         if (wav->streaming) {
1378           if (gst_adapter_available (wav->adapter) < 8 + data_size) {
1379             return GST_FLOW_OK;
1380           }
1381           gst_adapter_flush (wav->adapter, 8);
1382           acid = (const gst_riff_acid *) gst_adapter_peek (wav->adapter,
1383               data_size);
1384         } else {
1385           gst_buffer_unref (buf);
1386           if ((res =
1387                   gst_pad_pull_range (wav->sinkpad, wav->offset + 8,
1388                       data_size, &buf)) != GST_FLOW_OK)
1389             goto header_read_error;
1390           acid = (const gst_riff_acid *) GST_BUFFER_DATA (buf);
1391         }
1392         GST_INFO_OBJECT (wav, "Have acid chunk");
1393         /* send data as tags */
1394         if (!wav->tags)
1395           wav->tags = gst_tag_list_new ();
1396         gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1397             GST_TAG_BEATS_PER_MINUTE, acid->tempo, NULL);
1398
1399         if (wav->streaming) {
1400           gst_adapter_flush (wav->adapter, data_size);
1401         } else {
1402           gst_buffer_unref (buf);
1403           wav->offset += 8 + data_size;
1404         }
1405         break;
1406       }
1407         /* FIXME: all list tags after data are ignored in streaming mode */
1408       case GST_RIFF_TAG_LIST:{
1409         guint32 ltag;
1410
1411         if (wav->streaming) {
1412           const guint8 *data = NULL;
1413
1414           if (gst_adapter_available (wav->adapter) < 12) {
1415             return GST_FLOW_OK;
1416           }
1417           data = gst_adapter_peek (wav->adapter, 12);
1418           ltag = GST_READ_UINT32_LE (data + 8);
1419         } else {
1420           gst_buffer_unref (buf);
1421           if ((res =
1422                   gst_pad_pull_range (wav->sinkpad, wav->offset, 12,
1423                       &buf)) != GST_FLOW_OK)
1424             goto header_read_error;
1425           ltag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 8);
1426         }
1427         switch (ltag) {
1428           case GST_RIFF_LIST_INFO:{
1429             const guint data_size = size - 4;
1430             GstTagList *new;
1431
1432             GST_INFO_OBJECT (wav, "Have LIST chunk INFO size %u", data_size);
1433             if (wav->streaming) {
1434               gst_adapter_flush (wav->adapter, 12);
1435               if (gst_adapter_available (wav->adapter) < data_size) {
1436                 return GST_FLOW_OK;
1437               }
1438               gst_buffer_unref (buf);
1439               if (data_size > 0)
1440                 buf = gst_adapter_take_buffer (wav->adapter, data_size);
1441             } else {
1442               wav->offset += 12;
1443               gst_buffer_unref (buf);
1444               if (data_size > 0) {
1445                 if ((res =
1446                         gst_pad_pull_range (wav->sinkpad, wav->offset,
1447                             data_size, &buf)) != GST_FLOW_OK)
1448                   goto header_read_error;
1449               }
1450             }
1451             if (data_size > 0) {
1452               /* parse tags */
1453               gst_riff_parse_info (GST_ELEMENT (wav), buf, &new);
1454               if (new) {
1455                 GstTagList *old = wav->tags;
1456                 wav->tags =
1457                     gst_tag_list_merge (old, new, GST_TAG_MERGE_REPLACE);
1458                 if (old)
1459                   gst_tag_list_free (old);
1460                 gst_tag_list_free (new);
1461               }
1462               if (wav->streaming) {
1463                 gst_adapter_flush (wav->adapter, data_size);
1464               } else {
1465                 gst_buffer_unref (buf);
1466                 wav->offset += data_size;
1467               }
1468             }
1469             break;
1470           }
1471           default:
1472             GST_INFO_OBJECT (wav, "Ignoring LIST chunk %" GST_FOURCC_FORMAT,
1473                 GST_FOURCC_ARGS (ltag));
1474             gst_waveparse_ignore_chunk (wav, buf, tag, size);
1475             break;
1476         }
1477         break;
1478       }
1479       default:
1480         gst_waveparse_ignore_chunk (wav, buf, tag, size);
1481     }
1482
1483     if (upstream_size && (wav->offset >= upstream_size)) {
1484       /* Now we are gone through the whole file */
1485       gotdata = TRUE;
1486     }
1487   }
1488
1489   GST_DEBUG_OBJECT (wav, "Finished parsing headers");
1490
1491   if (wav->bps <= 0 && wav->fact) {
1492 #if 0
1493     /* not a good idea, as for embedded mp2/mp3 we set bps to 0 earlier */
1494     wav->bps =
1495         (guint32) gst_util_uint64_scale ((guint64) wav->rate, wav->datasize,
1496         (guint64) wav->fact);
1497     GST_INFO_OBJECT (wav, "calculated bps : %d, enabling VBR", wav->bps);
1498 #endif
1499     wav->vbr = TRUE;
1500   }
1501
1502   if (gst_wavparse_calculate_duration (wav)) {
1503     gst_segment_init (&wav->segment, GST_FORMAT_TIME);
1504     gst_segment_set_duration (&wav->segment, GST_FORMAT_TIME, wav->duration);
1505   } else {
1506     /* no bitrate, let downstream peer do the math, we'll feed it bytes. */
1507     gst_segment_init (&wav->segment, GST_FORMAT_BYTES);
1508     gst_segment_set_duration (&wav->segment, GST_FORMAT_BYTES, wav->datasize);
1509   }
1510
1511   /* now we have all the info to perform a pending seek if any, if no
1512    * event, this will still do the right thing and it will also send
1513    * the right newsegment event downstream. */
1514   gst_wavparse_perform_seek (wav, wav->seek_event);
1515   /* remove pending event */
1516   event_p = &wav->seek_event;
1517   gst_event_replace (event_p, NULL);
1518
1519   /* we just started, we are discont */
1520   wav->discont = TRUE;
1521
1522   wav->state = GST_WAVPARSE_DATA;
1523
1524   return GST_FLOW_OK;
1525
1526   /* ERROR */
1527 invalid_wav:
1528   {
1529     GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL),
1530         ("Invalid WAV header (no fmt at start): %"
1531             GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
1532     g_free (codec_name);
1533     return GST_FLOW_ERROR;
1534   }
1535 parse_header_error:
1536   {
1537     GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL),
1538         ("Couldn't parse audio header"));
1539     g_free (codec_name);
1540     return GST_FLOW_ERROR;
1541   }
1542 no_channels:
1543   {
1544     GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1545         ("Stream claims to contain no channels - invalid data"));
1546     g_free (header);
1547     g_free (codec_name);
1548     return GST_FLOW_ERROR;
1549   }
1550 no_rate:
1551   {
1552     GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1553         ("Stream with sample_rate == 0 - invalid data"));
1554     g_free (header);
1555     g_free (codec_name);
1556     return GST_FLOW_ERROR;
1557   }
1558 invalid_blockalign:
1559   {
1560     GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1561         ("Stream claims blockalign = %u, which is more than %u - invalid data",
1562             wav->blockalign, wav->channels * (guint) ceil (wav->depth / 8.0)));
1563     g_free (codec_name);
1564     return GST_FLOW_ERROR;
1565   }
1566 invalid_bps:
1567   {
1568     GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1569         ("Stream claims av_bsp = %u, which is more than %u - invalid data",
1570             wav->av_bps, wav->blockalign * wav->rate));
1571     g_free (codec_name);
1572     return GST_FLOW_ERROR;
1573   }
1574 no_bytes_per_sample:
1575   {
1576     GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1577         ("Could not caluclate bytes per sample - invalid data"));
1578     g_free (codec_name);
1579     return GST_FLOW_ERROR;
1580   }
1581 unknown_format:
1582   {
1583     GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL),
1584         ("No caps found for format 0x%x, %d channels, %d Hz",
1585             wav->format, wav->channels, wav->rate));
1586     g_free (header);
1587     g_free (codec_name);
1588     return GST_FLOW_ERROR;
1589   }
1590 header_read_error:
1591   {
1592     GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL),
1593         ("Couldn't read in header %d (%s)", res, gst_flow_get_name (res)));
1594     g_free (codec_name);
1595     return GST_FLOW_ERROR;
1596   }
1597 }
1598
1599 /*
1600  * Read WAV file tag when streaming
1601  */
1602 static GstFlowReturn
1603 gst_wavparse_parse_stream_init (GstWavParse * wav)
1604 {
1605   if (gst_adapter_available (wav->adapter) >= 12) {
1606     GstBuffer *tmp;
1607
1608     /* _take flushes the data */
1609     tmp = gst_adapter_take_buffer (wav->adapter, 12);
1610
1611     GST_DEBUG ("Parsing wav header");
1612     if (!gst_wavparse_parse_file_header (GST_ELEMENT_CAST (wav), tmp))
1613       return GST_FLOW_ERROR;
1614
1615     wav->offset += 12;
1616     /* Go to next state */
1617     wav->state = GST_WAVPARSE_HEADER;
1618   }
1619   return GST_FLOW_OK;
1620 }
1621
1622 /* handle an event sent directly to the element.
1623  *
1624  * This event can be sent either in the READY state or the
1625  * >READY state. The only event of interest really is the seek
1626  * event.
1627  *
1628  * In the READY state we can only store the event and try to
1629  * respect it when going to PAUSED. We assume we are in the
1630  * READY state when our parsing state != GST_WAVPARSE_DATA.
1631  *
1632  * When we are steaming, we can simply perform the seek right
1633  * away.
1634  */
1635 static gboolean
1636 gst_wavparse_send_event (GstElement * element, GstEvent * event)
1637 {
1638   GstWavParse *wav = GST_WAVPARSE (element);
1639   gboolean res = FALSE;
1640   GstEvent **event_p;
1641
1642   GST_DEBUG_OBJECT (wav, "received event %s", GST_EVENT_TYPE_NAME (event));
1643
1644   switch (GST_EVENT_TYPE (event)) {
1645     case GST_EVENT_SEEK:
1646       if (wav->state == GST_WAVPARSE_DATA) {
1647         /* we can handle the seek directly when streaming data */
1648         res = gst_wavparse_perform_seek (wav, event);
1649       } else {
1650         GST_DEBUG_OBJECT (wav, "queuing seek for later");
1651
1652         event_p = &wav->seek_event;
1653         gst_event_replace (event_p, event);
1654
1655         /* we always return true */
1656         res = TRUE;
1657       }
1658       break;
1659     default:
1660       break;
1661   }
1662   gst_event_unref (event);
1663   return res;
1664 }
1665
1666 static void
1667 gst_wavparse_add_src_pad (GstWavParse * wav, GstBuffer * buf)
1668 {
1669   GstStructure *s;
1670   const guint8 dts_marker[] = { 0xFF, 0x1F, 0x00, 0xE8, 0xF1, 0x07 };
1671
1672   GST_DEBUG_OBJECT (wav, "adding src pad");
1673
1674   if (wav->caps) {
1675     s = gst_caps_get_structure (wav->caps, 0);
1676     if (s && gst_structure_has_name (s, "audio/x-raw-int") && buf &&
1677         GST_BUFFER_SIZE (buf) > 6 &&
1678         memcmp (GST_BUFFER_DATA (buf), dts_marker, 6) == 0) {
1679
1680       GST_WARNING_OBJECT (wav, "Found DTS marker in file marked as raw PCM");
1681       gst_caps_unref (wav->caps);
1682       wav->caps = gst_caps_from_string ("audio/x-dts");
1683
1684       gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1685           GST_TAG_AUDIO_CODEC, "dts", NULL);
1686     }
1687   }
1688
1689   gst_wavparse_create_sourcepad (wav);
1690   gst_pad_set_active (wav->srcpad, TRUE);
1691   gst_pad_set_caps (wav->srcpad, wav->caps);
1692   gst_caps_replace (&wav->caps, NULL);
1693
1694   gst_element_add_pad (GST_ELEMENT_CAST (wav), wav->srcpad);
1695   gst_element_no_more_pads (GST_ELEMENT_CAST (wav));
1696
1697   if (wav->close_segment) {
1698     GST_DEBUG_OBJECT (wav, "Send close segment event on newpad");
1699     gst_pad_push_event (wav->srcpad, wav->close_segment);
1700     wav->close_segment = NULL;
1701   }
1702   if (wav->start_segment) {
1703     GST_DEBUG_OBJECT (wav, "Send start segment event on newpad");
1704     gst_pad_push_event (wav->srcpad, wav->start_segment);
1705     wav->start_segment = NULL;
1706   }
1707
1708   if (wav->tags) {
1709     gst_element_found_tags_for_pad (GST_ELEMENT_CAST (wav), wav->srcpad,
1710         wav->tags);
1711     wav->tags = NULL;
1712   }
1713 }
1714
1715 #define MAX_BUFFER_SIZE 4096
1716
1717 static GstFlowReturn
1718 gst_wavparse_stream_data (GstWavParse * wav)
1719 {
1720   GstBuffer *buf = NULL;
1721   GstFlowReturn res = GST_FLOW_OK;
1722   guint64 desired, obtained;
1723   GstClockTime timestamp, next_timestamp, duration;
1724   guint64 pos, nextpos;
1725
1726 iterate_adapter:
1727   GST_LOG_OBJECT (wav,
1728       "offset: %" G_GINT64_FORMAT " , end: %" G_GINT64_FORMAT " , dataleft: %"
1729       G_GINT64_FORMAT, wav->offset, wav->end_offset, wav->dataleft);
1730
1731   /* Get the next n bytes and output them */
1732   if (wav->dataleft == 0 || wav->dataleft < wav->blockalign)
1733     goto found_eos;
1734
1735   /* scale the amount of data by the segment rate so we get equal
1736    * amounts of data regardless of the playback rate */
1737   desired =
1738       MIN (gst_guint64_to_gdouble (wav->dataleft),
1739       MAX_BUFFER_SIZE * wav->segment.abs_rate);
1740
1741   if (desired >= wav->blockalign && wav->blockalign > 0)
1742     desired -= (desired % wav->blockalign);
1743
1744   GST_LOG_OBJECT (wav, "Fetching %" G_GINT64_FORMAT " bytes of data "
1745       "from the sinkpad", desired);
1746
1747   if (wav->streaming) {
1748     guint avail = gst_adapter_available (wav->adapter);
1749     guint extra;
1750
1751     /* flush some bytes if evil upstream sends segment that starts
1752      * before data or does is not send sample aligned segment */
1753     if (G_LIKELY (wav->offset >= wav->datastart)) {
1754       extra = (wav->offset - wav->datastart) % wav->bytes_per_sample;
1755     } else {
1756       extra = wav->datastart - wav->offset;
1757     }
1758
1759     if (G_UNLIKELY (extra)) {
1760       extra = wav->bytes_per_sample - extra;
1761       if (extra <= avail) {
1762         GST_DEBUG_OBJECT (wav, "flushing %d bytes to sample boundary", extra);
1763         gst_adapter_flush (wav->adapter, extra);
1764         wav->offset += extra;
1765         wav->dataleft -= extra;
1766         goto iterate_adapter;
1767       } else {
1768         GST_DEBUG_OBJECT (wav, "flushing %d bytes", avail);
1769         gst_adapter_clear (wav->adapter);
1770         wav->offset += avail;
1771         wav->dataleft -= avail;
1772         return GST_FLOW_OK;
1773       }
1774     }
1775
1776     if (avail < desired) {
1777       GST_LOG_OBJECT (wav, "Got only %d bytes of data from the sinkpad", avail);
1778       return GST_FLOW_OK;
1779     }
1780
1781     buf = gst_adapter_take_buffer (wav->adapter, desired);
1782   } else {
1783     if ((res = gst_pad_pull_range (wav->sinkpad, wav->offset,
1784                 desired, &buf)) != GST_FLOW_OK)
1785       goto pull_error;
1786   }
1787
1788   /* first chunk of data? create the source pad. We do this only here so
1789    * we can detect broken .wav files with dts disguised as raw PCM (sigh) */
1790   if (G_UNLIKELY (wav->first)) {
1791     wav->first = FALSE;
1792     /* this will also push the segment events */
1793     gst_wavparse_add_src_pad (wav, buf);
1794   } else {
1795     /* If we have a pending close/start segment, send it now. */
1796     if (G_UNLIKELY (wav->close_segment != NULL)) {
1797       gst_pad_push_event (wav->srcpad, wav->close_segment);
1798       wav->close_segment = NULL;
1799     }
1800     if (G_UNLIKELY (wav->start_segment != NULL)) {
1801       gst_pad_push_event (wav->srcpad, wav->start_segment);
1802       wav->start_segment = NULL;
1803     }
1804   }
1805
1806   obtained = GST_BUFFER_SIZE (buf);
1807
1808   /* our positions in bytes */
1809   pos = wav->offset - wav->datastart;
1810   nextpos = pos + obtained;
1811
1812   /* update offsets, does not overflow. */
1813   GST_BUFFER_OFFSET (buf) = pos / wav->bytes_per_sample;
1814   GST_BUFFER_OFFSET_END (buf) = nextpos / wav->bytes_per_sample;
1815
1816   if (wav->bps > 0) {
1817     /* and timestamps if we have a bitrate, be careful for overflows */
1818     timestamp = uint64_ceiling_scale (pos, GST_SECOND, (guint64) wav->bps);
1819     next_timestamp =
1820         uint64_ceiling_scale (nextpos, GST_SECOND, (guint64) wav->bps);
1821     duration = next_timestamp - timestamp;
1822
1823     /* update current running segment position */
1824     gst_segment_set_last_stop (&wav->segment, GST_FORMAT_TIME, next_timestamp);
1825   } else if (wav->fact) {
1826     guint64 bps =
1827         gst_util_uint64_scale_int (wav->datasize, wav->rate, wav->fact);
1828     /* and timestamps if we have a bitrate, be careful for overflows */
1829     timestamp = uint64_ceiling_scale (pos, GST_SECOND, bps);
1830     next_timestamp = uint64_ceiling_scale (nextpos, GST_SECOND, bps);
1831     duration = next_timestamp - timestamp;
1832   } else {
1833     /* no bitrate, all we know is that the first sample has timestamp 0, all
1834      * other positions and durations have unknown timestamp. */
1835     if (pos == 0)
1836       timestamp = 0;
1837     else
1838       timestamp = GST_CLOCK_TIME_NONE;
1839     duration = GST_CLOCK_TIME_NONE;
1840     /* update current running segment position with byte offset */
1841     gst_segment_set_last_stop (&wav->segment, GST_FORMAT_BYTES, nextpos);
1842   }
1843   if ((pos > 0) && wav->vbr) {
1844     /* don't set timestamps for VBR files if it's not the first buffer */
1845     timestamp = GST_CLOCK_TIME_NONE;
1846     duration = GST_CLOCK_TIME_NONE;
1847   }
1848   if (wav->discont) {
1849     GST_DEBUG_OBJECT (wav, "marking DISCONT");
1850     GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
1851     wav->discont = FALSE;
1852   }
1853
1854   GST_BUFFER_TIMESTAMP (buf) = timestamp;
1855   GST_BUFFER_DURATION (buf) = duration;
1856
1857   /* don't forget to set the caps on the buffer */
1858   gst_buffer_set_caps (buf, GST_PAD_CAPS (wav->srcpad));
1859
1860   GST_LOG_OBJECT (wav,
1861       "Got buffer. timestamp:%" GST_TIME_FORMAT " , duration:%" GST_TIME_FORMAT
1862       ", size:%u", GST_TIME_ARGS (timestamp), GST_TIME_ARGS (duration),
1863       GST_BUFFER_SIZE (buf));
1864
1865   if ((res = gst_pad_push (wav->srcpad, buf)) != GST_FLOW_OK)
1866     goto push_error;
1867
1868   if (obtained < wav->dataleft) {
1869     wav->offset += obtained;
1870     wav->dataleft -= obtained;
1871   } else {
1872     wav->offset += wav->dataleft;
1873     wav->dataleft = 0;
1874   }
1875
1876   /* Iterate until need more data, so adapter size won't grow */
1877   if (wav->streaming) {
1878     GST_LOG_OBJECT (wav,
1879         "offset: %" G_GINT64_FORMAT " , end: %" G_GINT64_FORMAT, wav->offset,
1880         wav->end_offset);
1881     goto iterate_adapter;
1882   }
1883   return res;
1884
1885   /* ERROR */
1886 found_eos:
1887   {
1888     GST_DEBUG_OBJECT (wav, "found EOS");
1889     return GST_FLOW_UNEXPECTED;
1890   }
1891 pull_error:
1892   {
1893     /* check if we got EOS */
1894     if (res == GST_FLOW_UNEXPECTED)
1895       goto found_eos;
1896
1897     GST_WARNING_OBJECT (wav,
1898         "Error getting %" G_GINT64_FORMAT " bytes from the "
1899         "sinkpad (dataleft = %" G_GINT64_FORMAT ")", desired, wav->dataleft);
1900     return res;
1901   }
1902 push_error:
1903   {
1904     GST_INFO_OBJECT (wav,
1905         "Error pushing on srcpad %s:%s, reason %s, is linked? = %d",
1906         GST_DEBUG_PAD_NAME (wav->srcpad), gst_flow_get_name (res),
1907         gst_pad_is_linked (wav->srcpad));
1908     return res;
1909   }
1910 }
1911
1912 static void
1913 gst_wavparse_loop (GstPad * pad)
1914 {
1915   GstFlowReturn ret;
1916   GstWavParse *wav = GST_WAVPARSE (GST_PAD_PARENT (pad));
1917
1918   GST_LOG_OBJECT (wav, "process data");
1919
1920   switch (wav->state) {
1921     case GST_WAVPARSE_START:
1922       GST_INFO_OBJECT (wav, "GST_WAVPARSE_START");
1923       if ((ret = gst_wavparse_stream_init (wav)) != GST_FLOW_OK)
1924         goto pause;
1925
1926       wav->state = GST_WAVPARSE_HEADER;
1927       /* fall-through */
1928
1929     case GST_WAVPARSE_HEADER:
1930       GST_INFO_OBJECT (wav, "GST_WAVPARSE_HEADER");
1931       if ((ret = gst_wavparse_stream_headers (wav)) != GST_FLOW_OK)
1932         goto pause;
1933
1934       wav->state = GST_WAVPARSE_DATA;
1935       GST_INFO_OBJECT (wav, "GST_WAVPARSE_DATA");
1936       /* fall-through */
1937
1938     case GST_WAVPARSE_DATA:
1939       if ((ret = gst_wavparse_stream_data (wav)) != GST_FLOW_OK)
1940         goto pause;
1941       break;
1942     default:
1943       g_assert_not_reached ();
1944   }
1945   return;
1946
1947   /* ERRORS */
1948 pause:
1949   {
1950     const gchar *reason = gst_flow_get_name (ret);
1951
1952     GST_DEBUG_OBJECT (wav, "pausing task, reason %s", reason);
1953     wav->segment_running = FALSE;
1954     gst_pad_pause_task (pad);
1955
1956     if (GST_FLOW_IS_FATAL (ret) || ret == GST_FLOW_NOT_LINKED) {
1957       if (ret == GST_FLOW_UNEXPECTED) {
1958         /* add pad before we perform EOS */
1959         if (G_UNLIKELY (wav->first)) {
1960           wav->first = FALSE;
1961           gst_wavparse_add_src_pad (wav, NULL);
1962         }
1963         /* perform EOS logic */
1964         if (wav->segment.flags & GST_SEEK_FLAG_SEGMENT) {
1965           GstClockTime stop;
1966
1967           if ((stop = wav->segment.stop) == -1)
1968             stop = wav->segment.duration;
1969
1970           gst_element_post_message (GST_ELEMENT_CAST (wav),
1971               gst_message_new_segment_done (GST_OBJECT_CAST (wav),
1972                   wav->segment.format, stop));
1973         } else {
1974           if (wav->srcpad != NULL)
1975             gst_pad_push_event (wav->srcpad, gst_event_new_eos ());
1976         }
1977       } else {
1978         /* for fatal errors we post an error message, post the error
1979          * first so the app knows about the error first. */
1980         GST_ELEMENT_ERROR (wav, STREAM, FAILED,
1981             (_("Internal data flow error.")),
1982             ("streaming task paused, reason %s (%d)", reason, ret));
1983         if (wav->srcpad != NULL)
1984           gst_pad_push_event (wav->srcpad, gst_event_new_eos ());
1985       }
1986     }
1987     return;
1988   }
1989 }
1990
1991 static GstFlowReturn
1992 gst_wavparse_chain (GstPad * pad, GstBuffer * buf)
1993 {
1994   GstFlowReturn ret;
1995   GstWavParse *wav = GST_WAVPARSE (GST_PAD_PARENT (pad));
1996
1997   GST_LOG_OBJECT (wav, "adapter_push %u bytes", GST_BUFFER_SIZE (buf));
1998
1999   gst_adapter_push (wav->adapter, buf);
2000
2001   switch (wav->state) {
2002     case GST_WAVPARSE_START:
2003       GST_INFO_OBJECT (wav, "GST_WAVPARSE_START");
2004       if ((ret = gst_wavparse_parse_stream_init (wav)) != GST_FLOW_OK)
2005         goto done;
2006
2007       if (wav->state != GST_WAVPARSE_HEADER)
2008         break;
2009
2010       /* otherwise fall-through */
2011     case GST_WAVPARSE_HEADER:
2012       GST_INFO_OBJECT (wav, "GST_WAVPARSE_HEADER");
2013       if ((ret = gst_wavparse_stream_headers (wav)) != GST_FLOW_OK)
2014         goto done;
2015
2016       if (!wav->got_fmt || wav->datastart == 0)
2017         break;
2018
2019       wav->state = GST_WAVPARSE_DATA;
2020       GST_INFO_OBJECT (wav, "GST_WAVPARSE_DATA");
2021
2022       /* fall-through */
2023     case GST_WAVPARSE_DATA:
2024       if (buf && GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DISCONT))
2025         wav->discont = TRUE;
2026       if ((ret = gst_wavparse_stream_data (wav)) != GST_FLOW_OK)
2027         goto done;
2028       break;
2029     default:
2030       g_return_val_if_reached (GST_FLOW_ERROR);
2031   }
2032 done:
2033   return ret;
2034 }
2035
2036 static GstFlowReturn
2037 gst_wavparse_flush_data (GstWavParse * wav)
2038 {
2039   GstFlowReturn ret = GST_FLOW_OK;
2040   guint av;
2041
2042   if ((av = gst_adapter_available (wav->adapter)) > 0) {
2043     wav->dataleft = av;
2044     wav->end_offset = wav->offset + av;
2045     ret = gst_wavparse_stream_data (wav);
2046   }
2047
2048   return ret;
2049 }
2050
2051 static gboolean
2052 gst_wavparse_sink_event (GstPad * pad, GstEvent * event)
2053 {
2054   GstWavParse *wav = GST_WAVPARSE (GST_PAD_PARENT (pad));
2055   gboolean ret = TRUE;
2056
2057   GST_LOG_OBJECT (wav, "handling %s event", GST_EVENT_TYPE_NAME (event));
2058
2059   switch (GST_EVENT_TYPE (event)) {
2060     case GST_EVENT_NEWSEGMENT:
2061     {
2062       GstFormat format;
2063       gdouble rate, arate;
2064       gint64 start, stop, time, offset = 0, end_offset = -1;
2065       gboolean update;
2066       GstSegment segment;
2067
2068       /* some debug output */
2069       gst_segment_init (&segment, GST_FORMAT_UNDEFINED);
2070       gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
2071           &start, &stop, &time);
2072       gst_segment_set_newsegment_full (&segment, update, rate, arate, format,
2073           start, stop, time);
2074       GST_DEBUG_OBJECT (wav,
2075           "received format %d newsegment %" GST_SEGMENT_FORMAT, format,
2076           &segment);
2077
2078       if (wav->state != GST_WAVPARSE_DATA) {
2079         GST_DEBUG_OBJECT (wav, "still starting, eating event");
2080         goto exit;
2081       }
2082
2083       /* now we are either committed to TIME or BYTE format,
2084        * and we only expect a BYTE segment, e.g. following a seek */
2085       if (format == GST_FORMAT_BYTES) {
2086         if (start > 0) {
2087           offset = start;
2088           start -= wav->datastart;
2089           start = MAX (start, 0);
2090         }
2091         if (stop > 0) {
2092           end_offset = stop;
2093           stop -= wav->datastart;
2094           stop = MAX (stop, 0);
2095         }
2096         if (wav->segment.format == GST_FORMAT_TIME) {
2097           guint64 bps = wav->bps;
2098
2099           /* operating in format TIME, so we can convert */
2100           if (!bps && wav->fact)
2101             bps =
2102                 gst_util_uint64_scale_int (wav->datasize, wav->rate, wav->fact);
2103           if (bps) {
2104             if (start >= 0)
2105               start =
2106                   uint64_ceiling_scale (start, GST_SECOND, (guint64) wav->bps);
2107             if (stop >= 0)
2108               stop =
2109                   uint64_ceiling_scale (stop, GST_SECOND, (guint64) wav->bps);
2110           }
2111         }
2112       } else {
2113         GST_DEBUG_OBJECT (wav, "unsupported segment format, ignoring");
2114         goto exit;
2115       }
2116
2117       /* accept upstream's notion of segment and distribute along */
2118       gst_segment_set_newsegment_full (&wav->segment, update, rate, arate,
2119           wav->segment.format, start, stop, start);
2120       /* also store the newsegment event for the streaming thread */
2121       if (wav->start_segment)
2122         gst_event_unref (wav->start_segment);
2123       wav->start_segment =
2124           gst_event_new_new_segment_full (update, rate, arate,
2125           wav->segment.format, start, stop, start);
2126       GST_DEBUG_OBJECT (wav, "Pushing newseg update %d, rate %g, "
2127           "applied rate %g, format %d, start %" G_GINT64_FORMAT ", "
2128           "stop %" G_GINT64_FORMAT, update, rate, arate, wav->segment.format,
2129           start, stop);
2130
2131       /* stream leftover data in current segment */
2132       gst_wavparse_flush_data (wav);
2133       /* and set up streaming thread for next one */
2134       wav->offset = offset;
2135       wav->end_offset = end_offset;
2136       if (wav->end_offset > 0) {
2137         wav->dataleft = wav->end_offset - wav->offset;
2138       } else {
2139         /* infinity; upstream will EOS when done */
2140         wav->dataleft = G_MAXUINT64;
2141       }
2142     exit:
2143       gst_event_unref (event);
2144       break;
2145     }
2146     case GST_EVENT_EOS:
2147       /* stream leftover data in current segment */
2148       gst_wavparse_flush_data (wav);
2149       /* fall-through */
2150     case GST_EVENT_FLUSH_STOP:
2151       gst_adapter_clear (wav->adapter);
2152       wav->discont = TRUE;
2153       /* fall-through */
2154     default:
2155       ret = gst_pad_event_default (wav->sinkpad, event);
2156       break;
2157   }
2158
2159   return ret;
2160 }
2161
2162 #if 0
2163 /* convert and query stuff */
2164 static const GstFormat *
2165 gst_wavparse_get_formats (GstPad * pad)
2166 {
2167   static GstFormat formats[] = {
2168     GST_FORMAT_TIME,
2169     GST_FORMAT_BYTES,
2170     GST_FORMAT_DEFAULT,         /* a "frame", ie a set of samples per Hz */
2171     0
2172   };
2173
2174   return formats;
2175 }
2176 #endif
2177
2178 static gboolean
2179 gst_wavparse_pad_convert (GstPad * pad,
2180     GstFormat src_format, gint64 src_value,
2181     GstFormat * dest_format, gint64 * dest_value)
2182 {
2183   GstWavParse *wavparse;
2184   gboolean res = TRUE;
2185
2186   wavparse = GST_WAVPARSE (GST_PAD_PARENT (pad));
2187
2188   if (*dest_format == src_format) {
2189     *dest_value = src_value;
2190     return TRUE;
2191   }
2192
2193   if ((wavparse->bps == 0) && !wavparse->fact)
2194     goto no_bps_fact;
2195
2196   GST_INFO_OBJECT (wavparse, "converting value from %s to %s",
2197       gst_format_get_name (src_format), gst_format_get_name (*dest_format));
2198
2199   switch (src_format) {
2200     case GST_FORMAT_BYTES:
2201       switch (*dest_format) {
2202         case GST_FORMAT_DEFAULT:
2203           *dest_value = src_value / wavparse->bytes_per_sample;
2204           /* make sure we end up on a sample boundary */
2205           *dest_value -= *dest_value % wavparse->bytes_per_sample;
2206           break;
2207         case GST_FORMAT_TIME:
2208           /* src_value + datastart = offset */
2209           GST_INFO_OBJECT (wavparse,
2210               "src=%" G_GINT64_FORMAT ", offset=%" G_GINT64_FORMAT, src_value,
2211               wavparse->offset);
2212           if (wavparse->bps > 0)
2213             *dest_value = uint64_ceiling_scale (src_value, GST_SECOND,
2214                 (guint64) wavparse->bps);
2215           else if (wavparse->fact) {
2216             guint64 bps = uint64_ceiling_scale_int (wavparse->datasize,
2217                 wavparse->rate, wavparse->fact);
2218
2219             *dest_value = uint64_ceiling_scale_int (src_value, GST_SECOND, bps);
2220           } else {
2221             res = FALSE;
2222           }
2223           break;
2224         default:
2225           res = FALSE;
2226           goto done;
2227       }
2228       break;
2229
2230     case GST_FORMAT_DEFAULT:
2231       switch (*dest_format) {
2232         case GST_FORMAT_BYTES:
2233           *dest_value = src_value * wavparse->bytes_per_sample;
2234           break;
2235         case GST_FORMAT_TIME:
2236           *dest_value = gst_util_uint64_scale (src_value, GST_SECOND,
2237               (guint64) wavparse->rate);
2238           break;
2239         default:
2240           res = FALSE;
2241           goto done;
2242       }
2243       break;
2244
2245     case GST_FORMAT_TIME:
2246       switch (*dest_format) {
2247         case GST_FORMAT_BYTES:
2248           if (wavparse->bps > 0)
2249             *dest_value = gst_util_uint64_scale (src_value,
2250                 (guint64) wavparse->bps, GST_SECOND);
2251           else {
2252             guint64 bps = gst_util_uint64_scale_int (wavparse->datasize,
2253                 wavparse->rate, wavparse->fact);
2254
2255             *dest_value = gst_util_uint64_scale (src_value, bps, GST_SECOND);
2256           }
2257           /* make sure we end up on a sample boundary */
2258           *dest_value -= *dest_value % wavparse->blockalign;
2259           break;
2260         case GST_FORMAT_DEFAULT:
2261           *dest_value = gst_util_uint64_scale (src_value,
2262               (guint64) wavparse->rate, GST_SECOND);
2263           break;
2264         default:
2265           res = FALSE;
2266           goto done;
2267       }
2268       break;
2269
2270     default:
2271       res = FALSE;
2272       goto done;
2273   }
2274
2275 done:
2276   return res;
2277
2278   /* ERRORS */
2279 no_bps_fact:
2280   {
2281     GST_DEBUG_OBJECT (wavparse, "bps 0 or no fact chunk, cannot convert");
2282     res = FALSE;
2283     goto done;
2284   }
2285 }
2286
2287 static const GstQueryType *
2288 gst_wavparse_get_query_types (GstPad * pad)
2289 {
2290   static const GstQueryType types[] = {
2291     GST_QUERY_POSITION,
2292     GST_QUERY_DURATION,
2293     GST_QUERY_CONVERT,
2294     GST_QUERY_SEEKING,
2295     0
2296   };
2297
2298   return types;
2299 }
2300
2301 /* handle queries for location and length in requested format */
2302 static gboolean
2303 gst_wavparse_pad_query (GstPad * pad, GstQuery * query)
2304 {
2305   gboolean res = TRUE;
2306   GstWavParse *wav = GST_WAVPARSE (gst_pad_get_parent (pad));
2307
2308   /* only if we know */
2309   if (wav->state != GST_WAVPARSE_DATA) {
2310     gst_object_unref (wav);
2311     return FALSE;
2312   }
2313
2314   GST_LOG_OBJECT (pad, "%s query", GST_QUERY_TYPE_NAME (query));
2315
2316   switch (GST_QUERY_TYPE (query)) {
2317     case GST_QUERY_POSITION:
2318     {
2319       gint64 curb;
2320       gint64 cur;
2321       GstFormat format;
2322
2323       /* this is not very precise, as we have pushed severla buffer upstream for prerolling */
2324       curb = wav->offset - wav->datastart;
2325       gst_query_parse_position (query, &format, NULL);
2326       GST_INFO_OBJECT (wav, "pos query at %" G_GINT64_FORMAT, curb);
2327
2328       switch (format) {
2329         case GST_FORMAT_TIME:
2330           res = gst_wavparse_pad_convert (pad, GST_FORMAT_BYTES, curb,
2331               &format, &cur);
2332           break;
2333         default:
2334           format = GST_FORMAT_BYTES;
2335           cur = curb;
2336           break;
2337       }
2338       if (res)
2339         gst_query_set_position (query, format, cur);
2340       break;
2341     }
2342     case GST_QUERY_DURATION:
2343     {
2344       gint64 duration = 0;
2345       GstFormat format;
2346
2347       gst_query_parse_duration (query, &format, NULL);
2348
2349       switch (format) {
2350         case GST_FORMAT_TIME:{
2351           if ((res = gst_wavparse_calculate_duration (wav))) {
2352             duration = wav->duration;
2353           }
2354           break;
2355         }
2356         default:
2357           format = GST_FORMAT_BYTES;
2358           duration = wav->datasize;
2359           break;
2360       }
2361       gst_query_set_duration (query, format, duration);
2362       break;
2363     }
2364     case GST_QUERY_CONVERT:
2365     {
2366       gint64 srcvalue, dstvalue;
2367       GstFormat srcformat, dstformat;
2368
2369       gst_query_parse_convert (query, &srcformat, &srcvalue,
2370           &dstformat, &dstvalue);
2371       res = gst_wavparse_pad_convert (pad, srcformat, srcvalue,
2372           &dstformat, &dstvalue);
2373       if (res)
2374         gst_query_set_convert (query, srcformat, srcvalue, dstformat, dstvalue);
2375       break;
2376     }
2377     case GST_QUERY_SEEKING:{
2378       GstFormat fmt;
2379       gboolean seekable = FALSE;
2380
2381       gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
2382       if (fmt == wav->segment.format) {
2383         if (wav->streaming) {
2384           GstQuery *q;
2385
2386           q = gst_query_new_seeking (GST_FORMAT_BYTES);
2387           if ((res = gst_pad_peer_query (wav->sinkpad, q))) {
2388             gst_query_parse_seeking (q, &fmt, &seekable, NULL, NULL);
2389             GST_LOG_OBJECT (wav, "upstream BYTE seekable %d", seekable);
2390           }
2391           gst_query_unref (q);
2392         } else {
2393           GST_LOG_OBJECT (wav, "looping => seekable");
2394           seekable = TRUE;
2395           res = TRUE;
2396         }
2397       } else if (fmt == GST_FORMAT_TIME) {
2398         res = TRUE;
2399       }
2400       if (res) {
2401         gst_query_set_seeking (query, fmt, seekable, 0, wav->segment.duration);
2402       }
2403       break;
2404     }
2405     default:
2406       res = gst_pad_query_default (pad, query);
2407       break;
2408   }
2409   gst_object_unref (wav);
2410   return res;
2411 }
2412
2413 static gboolean
2414 gst_wavparse_srcpad_event (GstPad * pad, GstEvent * event)
2415 {
2416   GstWavParse *wavparse = GST_WAVPARSE (gst_pad_get_parent (pad));
2417   gboolean res = FALSE;
2418
2419   GST_DEBUG_OBJECT (wavparse, "%s event", GST_EVENT_TYPE_NAME (event));
2420
2421   switch (GST_EVENT_TYPE (event)) {
2422     case GST_EVENT_SEEK:
2423       /* can only handle events when we are in the data state */
2424       if (wavparse->state == GST_WAVPARSE_DATA) {
2425         res = gst_wavparse_perform_seek (wavparse, event);
2426       }
2427       gst_event_unref (event);
2428       break;
2429     default:
2430       res = gst_pad_push_event (wavparse->sinkpad, event);
2431       break;
2432   }
2433   gst_object_unref (wavparse);
2434   return res;
2435 }
2436
2437 static gboolean
2438 gst_wavparse_sink_activate (GstPad * sinkpad)
2439 {
2440   GstWavParse *wav = GST_WAVPARSE (gst_pad_get_parent (sinkpad));
2441   gboolean res;
2442
2443   if (wav->adapter) {
2444     gst_adapter_clear (wav->adapter);
2445     g_object_unref (wav->adapter);
2446     wav->adapter = NULL;
2447   }
2448
2449   if (gst_pad_check_pull_range (sinkpad)) {
2450     GST_DEBUG ("going to pull mode");
2451     wav->streaming = FALSE;
2452     res = gst_pad_activate_pull (sinkpad, TRUE);
2453   } else {
2454     GST_DEBUG ("going to push (streaming) mode");
2455     wav->streaming = TRUE;
2456     wav->adapter = gst_adapter_new ();
2457     res = gst_pad_activate_push (sinkpad, TRUE);
2458   }
2459   gst_object_unref (wav);
2460   return res;
2461 }
2462
2463
2464 static gboolean
2465 gst_wavparse_sink_activate_pull (GstPad * sinkpad, gboolean active)
2466 {
2467   GstWavParse *wav = GST_WAVPARSE (GST_OBJECT_PARENT (sinkpad));
2468
2469   if (active) {
2470     /* if we have a scheduler we can start the task */
2471     wav->segment_running = TRUE;
2472     return gst_pad_start_task (sinkpad, (GstTaskFunction) gst_wavparse_loop,
2473         sinkpad);
2474   } else {
2475     wav->segment_running = FALSE;
2476     return gst_pad_stop_task (sinkpad);
2477   }
2478 };
2479
2480 static GstStateChangeReturn
2481 gst_wavparse_change_state (GstElement * element, GstStateChange transition)
2482 {
2483   GstStateChangeReturn ret;
2484   GstWavParse *wav = GST_WAVPARSE (element);
2485
2486   switch (transition) {
2487     case GST_STATE_CHANGE_NULL_TO_READY:
2488       break;
2489     case GST_STATE_CHANGE_READY_TO_PAUSED:
2490       gst_wavparse_reset (wav);
2491       break;
2492     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
2493       break;
2494     default:
2495       break;
2496   }
2497
2498   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2499
2500   switch (transition) {
2501     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2502       break;
2503     case GST_STATE_CHANGE_PAUSED_TO_READY:
2504       gst_wavparse_destroy_sourcepad (wav);
2505       gst_wavparse_reset (wav);
2506       break;
2507     case GST_STATE_CHANGE_READY_TO_NULL:
2508       break;
2509     default:
2510       break;
2511   }
2512   return ret;
2513 }
2514
2515 static gboolean
2516 plugin_init (GstPlugin * plugin)
2517 {
2518   gst_riff_init ();
2519
2520   return gst_element_register (plugin, "wavparse", GST_RANK_PRIMARY,
2521       GST_TYPE_WAVPARSE);
2522 }
2523
2524 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
2525     GST_VERSION_MINOR,
2526     "wavparse",
2527     "Parse a .wav file into raw audio",
2528     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)