1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: t; c-basic-offset: 2 -*- */
3 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
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.
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.
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.
27 #include <gstwavparse.h>
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);
33 static GstElementStateReturn
34 gst_wavparse_change_state (GstElement *element);
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,
43 static gboolean gst_wavparse_pad_convert (GstPad *pad,
46 GstFormat *dest_format,
49 static void gst_wavparse_loop (GstElement *element);
50 static const GstEventMask*
51 gst_wavparse_get_event_masks (GstPad *pad);
52 static gboolean gst_wavparse_srcpad_event (GstPad *pad, GstEvent *event);
53 static void gst_wavparse_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
55 /* elementfactory information */
56 static GstElementDetails gst_wavparse_details = GST_ELEMENT_DETAILS (
59 "Parse a .wav file into raw audio",
60 "Erik Walthinsen <omega@cse.ogi.edu>"
63 GST_PAD_TEMPLATE_FACTORY (sink_template_factory,
74 GST_PAD_TEMPLATE_FACTORY (src_template_factory,
81 "endianness", GST_PROPS_INT (G_LITTLE_ENDIAN),
82 "signed", GST_PROPS_LIST (
83 GST_PROPS_BOOLEAN (FALSE),
84 GST_PROPS_BOOLEAN (TRUE)
86 "width", GST_PROPS_LIST (
90 "depth", GST_PROPS_LIST (
94 "rate", GST_PROPS_INT_RANGE (8000, 48000),
95 "channels", GST_PROPS_INT_RANGE (1, 2)
100 "rate", GST_PROPS_INT_RANGE (8000, 48000),
101 "channels", GST_PROPS_INT_RANGE (1, 2),
102 "layer", GST_PROPS_INT_RANGE (1, 3)
107 "rate", GST_PROPS_INT_RANGE (8000, 48000),
108 "channels", GST_PROPS_INT_RANGE (1, 2)
113 "rate", GST_PROPS_INT_RANGE (8000, 48000),
114 "channels", GST_PROPS_INT_RANGE (1, 2)
118 /* WavParse signals and args */
129 static GstElementClass *parent_class = NULL;
130 /*static guint gst_wavparse_signals[LAST_SIGNAL] = { 0 }; */
133 gst_wavparse_get_type (void)
135 static GType wavparse_type = 0;
137 if (!wavparse_type) {
138 static const GTypeInfo wavparse_info = {
139 sizeof(GstWavParseClass),
140 gst_wavparse_base_init,
142 (GClassInitFunc) gst_wavparse_class_init,
147 (GInstanceInitFunc) gst_wavparse_init,
149 wavparse_type = g_type_register_static (GST_TYPE_ELEMENT, "GstWavParse", &wavparse_info, 0);
151 return wavparse_type;
156 gst_wavparse_base_init (gpointer g_class)
158 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
160 gst_element_class_set_details (element_class, &gst_wavparse_details);
162 /* register src pads */
163 gst_element_class_add_pad_template (element_class, GST_PAD_TEMPLATE_GET (sink_template_factory));
164 gst_element_class_add_pad_template (element_class, GST_PAD_TEMPLATE_GET (src_template_factory));
167 gst_wavparse_class_init (GstWavParseClass *klass)
169 GstElementClass *gstelement_class;
170 GObjectClass *object_class;
172 gstelement_class = (GstElementClass*) klass;
173 object_class = (GObjectClass *) klass;
175 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
177 object_class->get_property = gst_wavparse_get_property;
178 gstelement_class->change_state = gst_wavparse_change_state;
180 g_object_class_install_property (object_class, PROP_METADATA,
181 g_param_spec_boxed ("metadata",
182 "Metadata", "Metadata",
188 gst_wavparse_init (GstWavParse *wavparse)
193 wavparse->sinkpad = gst_pad_new_from_template (GST_PAD_TEMPLATE_GET (sink_template_factory), "sink");
194 gst_element_add_pad (GST_ELEMENT (wavparse), wavparse->sinkpad);
196 gst_pad_set_formats_function (wavparse->sinkpad, gst_wavparse_get_formats);
197 gst_pad_set_convert_function (wavparse->sinkpad, gst_wavparse_pad_convert);
198 gst_pad_set_query_type_function (wavparse->sinkpad,
199 gst_wavparse_get_query_types);
200 gst_pad_set_query_function (wavparse->sinkpad, gst_wavparse_pad_query);
203 wavparse->srcpad = gst_pad_new_from_template (GST_PAD_TEMPLATE_GET (src_template_factory), "src");
204 gst_element_add_pad (GST_ELEMENT (wavparse), wavparse->srcpad);
205 gst_pad_set_formats_function (wavparse->srcpad, gst_wavparse_get_formats);
206 gst_pad_set_convert_function (wavparse->srcpad, gst_wavparse_pad_convert);
207 gst_pad_set_query_type_function (wavparse->srcpad,
208 gst_wavparse_get_query_types);
209 gst_pad_set_query_function (wavparse->srcpad, gst_wavparse_pad_query);
210 gst_pad_set_event_function (wavparse->srcpad, gst_wavparse_srcpad_event);
211 gst_pad_set_event_mask_function (wavparse->srcpad, gst_wavparse_get_event_masks);
213 gst_element_set_loop_function (GST_ELEMENT (wavparse), gst_wavparse_loop);
215 wavparse->state = GST_WAVPARSE_UNKNOWN;
217 wavparse->seek_pending = FALSE;
218 wavparse->seek_offset = 0;
220 props = gst_props_empty_new ();
222 /* Metadata is added later when we find it */
223 gst_caps_replace_sink (&wavparse->metadata,
224 gst_caps_new ("wav_metadata",
225 "application/x-gst-metadata",
230 gst_wavparse_get_property (GObject *object,
235 GstWavParse *wavparse;
237 wavparse = GST_WAVPARSE (object);
241 g_value_set_boxed (value, wavparse->metadata);
250 gst_wavparse_parse_adtl (GstWavParse *wavparse,
254 GstByteStream *bs = wavparse->bs;
255 gst_riff_chunk *temp_chunk, chunk;
257 struct _gst_riff_labl labl, *temp_labl;
258 struct _gst_riff_ltxt ltxt, *temp_ltxt;
259 struct _gst_riff_note note, *temp_note;
262 GstPropsEntry *entry;
266 props = wavparse->metadata->properties;
269 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, sizeof (gst_riff_chunk));
270 if (got_bytes != sizeof (gst_riff_chunk)) {
273 temp_chunk = (gst_riff_chunk *) tempdata;
275 chunk.id = GUINT32_FROM_LE (temp_chunk->id);
276 chunk.size = GUINT32_FROM_LE (temp_chunk->size);
278 if (chunk.size == 0) {
279 gst_bytestream_flush (bs, sizeof (gst_riff_chunk));
280 len -= sizeof (gst_riff_chunk);
285 case GST_RIFF_adtl_labl:
286 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, sizeof (struct _gst_riff_labl));
287 if (got_bytes != sizeof (struct _gst_riff_labl)) {
291 temp_labl = (struct _gst_riff_labl *) tempdata;
292 labl.id = GUINT32_FROM_LE (temp_labl->id);
293 labl.size = GUINT32_FROM_LE (temp_labl->size);
294 labl.identifier = GUINT32_FROM_LE (temp_labl->identifier);
296 gst_bytestream_flush (bs, sizeof (struct _gst_riff_labl));
297 len -= sizeof (struct _gst_riff_labl);
299 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, labl.size - 4);
300 if (got_bytes != labl.size - 4) {
304 label_name = (char *) tempdata;
306 gst_bytestream_flush (bs, ((labl.size - 4) + 1) & ~1);
307 len -= (( (labl.size - 4) + 1) & ~1);
309 new_caps = gst_caps_new ("label",
310 "application/x-gst-metadata",
312 "identifier", GST_PROPS_INT (labl.identifier),
313 "name", GST_PROPS_STRING (label_name),
316 if (gst_props_get (props, "labels", &caps, NULL)) {
317 caps = g_list_append (caps, new_caps);
319 caps = g_list_append (NULL, new_caps);
321 entry = gst_props_entry_new ("labels", GST_PROPS_GLIST (caps));
322 gst_props_add_entry (props, entry);
327 case GST_RIFF_adtl_ltxt:
328 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, sizeof (struct _gst_riff_ltxt));
329 if (got_bytes != sizeof (struct _gst_riff_ltxt)) {
333 temp_ltxt = (struct _gst_riff_ltxt *) tempdata;
334 ltxt.id = GUINT32_FROM_LE (temp_ltxt->id);
335 ltxt.size = GUINT32_FROM_LE (temp_ltxt->size);
336 ltxt.identifier = GUINT32_FROM_LE (temp_ltxt->identifier);
337 ltxt.length = GUINT32_FROM_LE (temp_ltxt->length);
338 ltxt.purpose = GUINT32_FROM_LE (temp_ltxt->purpose);
339 ltxt.country = GUINT16_FROM_LE (temp_ltxt->country);
340 ltxt.language = GUINT16_FROM_LE (temp_ltxt->language);
341 ltxt.dialect = GUINT16_FROM_LE (temp_ltxt->dialect);
342 ltxt.codepage = GUINT16_FROM_LE (temp_ltxt->codepage);
344 gst_bytestream_flush (bs, sizeof (struct _gst_riff_ltxt));
345 len -= sizeof (struct _gst_riff_ltxt);
347 if (ltxt.size - 20 > 0) {
348 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, ltxt.size - 20);
349 if (got_bytes != ltxt.size - 20) {
353 gst_bytestream_flush (bs, ((ltxt.size - 20) + 1) & ~1);
354 len -= (( (ltxt.size - 20) + 1) & ~1);
356 label_name = (char *) tempdata;
361 new_caps = gst_caps_new ("ltxt",
362 "application/x-gst-metadata",
364 "identifier", GST_PROPS_INT (ltxt.identifier),
365 "name", GST_PROPS_STRING (label_name),
366 "length", GST_PROPS_INT (ltxt.length),
369 if (gst_props_get (props, "ltxts", &caps, NULL)) {
370 caps = g_list_append (caps, new_caps);
372 caps = g_list_append (NULL, new_caps);
374 entry = gst_props_entry_new ("ltxts", GST_PROPS_GLIST (caps));
375 gst_props_add_entry (props, entry);
380 case GST_RIFF_adtl_note:
381 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, sizeof (struct _gst_riff_note));
382 if (got_bytes != sizeof (struct _gst_riff_note)) {
386 temp_note = (struct _gst_riff_note *) tempdata;
387 note.id = GUINT32_FROM_LE (temp_note->id);
388 note.size = GUINT32_FROM_LE (temp_note->size);
389 note.identifier = GUINT32_FROM_LE (temp_note->identifier);
391 gst_bytestream_flush (bs, sizeof (struct _gst_riff_note));
392 len -= sizeof (struct _gst_riff_note);
394 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, note.size - 4);
395 if (got_bytes != note.size - 4) {
399 gst_bytestream_flush (bs, ((note.size - 4) + 1) & ~1);
400 len -= (( (note.size - 4) + 1) & ~1);
402 label_name = (char *) tempdata;
404 new_caps = gst_caps_new ("note",
405 "application/x-gst-metadata",
407 "identifier", GST_PROPS_INT (note.identifier),
408 "name", GST_PROPS_STRING (label_name),
411 if (gst_props_get (props, "notes", &caps, NULL)) {
412 caps = g_list_append (caps, new_caps);
414 caps = g_list_append (NULL, new_caps);
416 entry = gst_props_entry_new ("notes", GST_PROPS_GLIST (caps));
417 gst_props_add_entry (props, entry);
423 g_print ("Unknown chunk: " GST_FOURCC_FORMAT "\n", GST_FOURCC_ARGS(chunk.id));
428 g_object_notify (G_OBJECT (wavparse), "metadata");
432 gst_wavparse_parse_info (GstWavParse *wavparse,
435 gst_riff_chunk *temp_chunk, chunk;
436 GstByteStream *bs = wavparse->bs;
442 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, sizeof (gst_riff_chunk));
443 temp_chunk = (gst_riff_chunk *) tempdata;
445 chunk.id = GUINT32_FROM_LE (temp_chunk->id);
446 chunk.size = GUINT32_FROM_LE (temp_chunk->size);
448 gst_bytestream_flush (bs, sizeof (gst_riff_chunk));
449 if (got_bytes != sizeof (gst_riff_chunk)) {
453 /* move our pointer on past the header */
454 len -= sizeof (gst_riff_chunk);
456 if (chunk.size == 0) {
460 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, chunk.size);
461 name = (char *) tempdata;
462 if (got_bytes != chunk.size) {
466 /* move our pointer on past the data ... on an even boundary */
467 gst_bytestream_flush (bs, (chunk.size + 1) & ~1);
468 len -= ((chunk.size + 1) & ~1);
470 /* We now have an info string in 'name' of type chunk.id
473 case GST_RIFF_INFO_IARL:
477 case GST_RIFF_INFO_IART:
481 case GST_RIFF_INFO_ICMS:
482 type = "Commissioner";
485 case GST_RIFF_INFO_ICMT:
489 case GST_RIFF_INFO_ICOP:
493 case GST_RIFF_INFO_ICRD:
494 type = "Creation Date";
497 case GST_RIFF_INFO_IENG:
501 case GST_RIFF_INFO_IGNR:
505 case GST_RIFF_INFO_IKEY:
509 case GST_RIFF_INFO_INAM:
510 type = "Title"; /* name */
513 case GST_RIFF_INFO_IPRD:
517 case GST_RIFF_INFO_ISBJ:
521 case GST_RIFF_INFO_ISFT:
525 case GST_RIFF_INFO_ITCH:
530 g_print ("Unknown: %4.4s\n", (char *) &chunk.id);
536 GstPropsEntry *entry;
538 entry = gst_props_entry_new (type, GST_PROPS_STRING (name));
539 gst_props_add_entry (wavparse->metadata->properties, entry);
543 g_object_notify (G_OBJECT (wavparse), "metadata");
547 gst_wavparse_parse_cues (GstWavParse *wavparse,
551 GstByteStream *bs = wavparse->bs;
552 struct _gst_riff_cue *temp_cue, cue;
553 struct _gst_riff_cuepoints *points;
557 GstPropsEntry *entry;
562 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, sizeof (struct _gst_riff_cue));
563 temp_cue = (struct _gst_riff_cue *) tempdata;
565 /* fixup for our big endian friends */
566 cue.id = GUINT32_FROM_LE (temp_cue->id);
567 cue.size = GUINT32_FROM_LE (temp_cue->size);
568 cue.cuepoints = GUINT32_FROM_LE (temp_cue->cuepoints);
570 gst_bytestream_flush (bs, sizeof (struct _gst_riff_cue));
571 if (got_bytes != sizeof (struct _gst_riff_cue)) {
575 len -= sizeof (struct _gst_riff_cue);
577 /* -4 because cue.size contains the cuepoints size
578 and we've already flushed that out of the system */
579 required = cue.size - 4;
580 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, required);
581 gst_bytestream_flush (bs, ((required) + 1) & ~1);
582 if (got_bytes != required) {
586 len -= ( ((cue.size - 4) + 1) & ~1);
588 /* now we have an array of struct _gst_riff_cuepoints in tempdata */
589 points = (struct _gst_riff_cuepoints *) tempdata;
591 for (i = 0; i < cue.cuepoints; i++) {
594 caps = gst_caps_new ("cues",
595 "application/x-gst-metadata",
597 "identifier", GST_PROPS_INT (points[i].identifier),
598 "position", GST_PROPS_INT (points[i].offset),
600 cues = g_list_append (cues, caps);
603 entry = gst_props_entry_new ("cues", GST_PROPS_GLIST (cues));
604 gst_props_add_entry (wavparse->metadata->properties, entry);
607 g_object_notify (G_OBJECT (wavparse), "metadata");
611 gst_wavparse_parse_fmt (GstWavParse *wavparse)
613 GstWavParseFormat *format;
614 GstCaps *caps = NULL;
616 GstByteStream *bs = wavparse->bs;
619 got_bytes = gst_bytestream_peek_bytes (bs, &fmtdata, sizeof (GstWavParseFormat));
620 format = (GstWavParseFormat *) fmtdata;
622 if (got_bytes == sizeof (GstWavParseFormat)) {
623 gst_bytestream_flush (bs, got_bytes);
624 wavparse->bps = GUINT16_FROM_LE (format->wBlockAlign);
625 wavparse->rate = GUINT32_FROM_LE (format->dwSamplesPerSec);
626 wavparse->channels = GUINT16_FROM_LE (format->wChannels);
627 wavparse->width = GUINT16_FROM_LE (format->wBitsPerSample);
628 wavparse->format = GINT16_FROM_LE (format->wFormatTag);
630 /* set the caps on the src pad */
631 /* FIXME: handle all of the other formats as well */
632 switch (wavparse->format) {
633 case GST_RIFF_WAVE_FORMAT_ALAW:
634 case GST_RIFF_WAVE_FORMAT_MULAW: {
635 char *mime = (wavparse->format == GST_RIFF_WAVE_FORMAT_ALAW) ?
636 "audio/x-alaw" : "audio/x-mulaw";
637 if (wavparse->width != 8) {
638 g_warning ("Ignoring invalid width %d", wavparse->width);
642 caps = GST_CAPS_NEW ("parsewav_src",
644 "rate", GST_PROPS_INT (wavparse->rate),
645 "channels", GST_PROPS_INT (wavparse->channels)
649 case GST_RIFF_WAVE_FORMAT_PCM:
650 caps = GST_CAPS_NEW ("parsewav_src",
652 "endianness", GST_PROPS_INT (G_LITTLE_ENDIAN),
653 "signed", GST_PROPS_BOOLEAN ((wavparse->width > 8) ? TRUE : FALSE),
654 "width", GST_PROPS_INT (wavparse->width),
655 "depth", GST_PROPS_INT (wavparse->width),
656 "rate", GST_PROPS_INT (wavparse->rate),
657 "channels", GST_PROPS_INT (wavparse->channels)
661 case GST_RIFF_WAVE_FORMAT_MPEGL12:
662 case GST_RIFF_WAVE_FORMAT_MPEGL3: {
663 int layer = (wavparse->format == GST_RIFF_WAVE_FORMAT_MPEGL12) ? 2 : 3;
665 caps = GST_CAPS_NEW ("parsewav_src",
667 "layer", GST_PROPS_INT (layer),
668 "rate", GST_PROPS_INT (wavparse->rate),
669 "channels", GST_PROPS_INT (wavparse->channels)
675 gst_element_error (GST_ELEMENT (wavparse), "wavparse: format %d not handled", wavparse->format);
679 if (gst_pad_try_set_caps (wavparse->srcpad, caps) <= 0) {
680 gst_element_error (GST_ELEMENT (wavparse), "Could not set caps");
684 GST_DEBUG ("frequency %d, channels %d",
685 wavparse->rate, wavparse->channels);
690 gst_wavparse_handle_sink_event (GstWavParse *wavparse)
697 gst_bytestream_get_status (wavparse->bs, &remaining, &event);
699 type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
700 GST_DEBUG ("wavparse: event %p %d", event, type);
704 gst_bytestream_flush (wavparse->bs, remaining);
705 gst_pad_event_default (wavparse->sinkpad, event);
709 case GST_EVENT_FLUSH:
710 g_warning ("Wavparse: Flush event");
714 g_warning ("Wavparse: Unhandled event %d", type);
718 gst_event_unref (event);
725 gst_wavparse_loop (GstElement *element)
727 GstWavParse *wavparse;
733 wavparse = GST_WAVPARSE (element);
737 if (wavparse->seek_pending) {
738 GST_DEBUG ("wavparse: seek pending to %" G_GINT64_FORMAT " %08llx",
739 wavparse->seek_offset,
740 (unsigned long long) wavparse->seek_offset);
742 if (!gst_bytestream_seek (bs, wavparse->seek_offset, GST_SEEK_METHOD_SET)) {
743 GST_INFO ("wavparse: Could not seek");
746 wavparse->seek_pending = FALSE;
749 if (wavparse->state == GST_WAVPARSE_DATA) {
753 /* This seems to want the whole chunk,
754 Will this screw up streaming?
755 Does anyone care about streaming wavs?
756 FIXME: Should we have a decent buffer size? */
758 #define MAX_BUFFER_SIZE 1024
760 if (wavparse->dataleft > 0) {
761 desired = MIN (wavparse->dataleft, MAX_BUFFER_SIZE);
762 got_bytes = gst_bytestream_peek (bs, &buf, desired);
764 if (got_bytes == 0) {
768 wavparse->dataleft -= got_bytes;
769 wavparse->byteoffset += got_bytes;
771 gst_bytestream_flush (bs, got_bytes);
773 gst_pad_push (wavparse->srcpad, GST_DATA (buf));
776 wavparse->state = GST_WAVPARSE_OTHER;
781 gst_riff_riff *temp_chunk;
785 /* read first two dwords to get chunktype and size */
787 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, sizeof (gst_riff_chunk));
788 temp_chunk = (gst_riff_riff *) tempdata;
790 if (got_bytes < sizeof (gst_riff_chunk)) {
791 if (!gst_wavparse_handle_sink_event (wavparse)) {
799 chunk.id = GUINT32_FROM_LE (temp_chunk->id);
800 chunk.size = GUINT32_FROM_LE (temp_chunk->size);
803 case GST_RIFF_TAG_RIFF:
804 case GST_RIFF_TAG_LIST:
805 /* Read complete list chunk */
807 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, sizeof (gst_riff_list));
808 temp_chunk = (gst_riff_riff *) tempdata;
809 if (got_bytes < sizeof (gst_riff_list)) {
810 if (!gst_wavparse_handle_sink_event (wavparse)) {
818 chunk.type = GUINT32_FROM_LE (temp_chunk->type);
819 skipsize = sizeof (gst_riff_list);
822 case GST_RIFF_TAG_cue:
827 skipsize = sizeof (gst_riff_chunk);
830 gst_bytestream_flush (bs, skipsize);
833 /* need to flush an even number of bytes at the end */
834 flush = (chunk.size + 1) & ~1;
836 switch (wavparse->state) {
837 case GST_WAVPARSE_START:
838 if (chunk.id != GST_RIFF_TAG_RIFF &&
839 chunk.type != GST_RIFF_RIFF_WAVE) {
840 gst_element_error (element, "This doesn't appear to be a WAV file %08x %08x", chunk.id, chunk.type);
844 wavparse->state = GST_WAVPARSE_OTHER;
845 /* We are not going to flush lists */
849 case GST_WAVPARSE_OTHER:
850 GST_DEBUG ("riff tag: %4.4s %08x", (char *) &chunk.id, chunk.size);
853 case GST_RIFF_TAG_data:
854 wavparse->state = GST_WAVPARSE_DATA;
855 wavparse->dataleft = chunk.size;
856 wavparse->byteoffset = 0;
861 case GST_RIFF_TAG_fmt:
862 gst_wavparse_parse_fmt (wavparse);
865 case GST_RIFF_TAG_cue:
866 gst_wavparse_parse_cues (wavparse, chunk.size);
869 case GST_RIFF_TAG_LIST:
870 GST_DEBUG ("list type: %4.4s", (char *) &chunk.type);
871 switch (chunk.type) {
872 case GST_RIFF_LIST_INFO:
873 gst_wavparse_parse_info (wavparse, chunk.size - 4);
878 case GST_RIFF_LIST_adtl:
879 gst_wavparse_parse_adtl (wavparse, chunk.size - 4);
892 case GST_WAVPARSE_DATA:
893 /* Should have been handled up there ^^^^ */
899 GST_DEBUG (" ***** unknown chunkid %08x", chunk.id);
906 res = gst_bytestream_flush (bs, flush);
911 gst_bytestream_get_status (bs, &remaining, &event);
912 gst_event_unref (event);
917 /* convert and query stuff */
918 static const GstFormat *
919 gst_wavparse_get_formats (GstPad *pad)
921 static GstFormat formats[] = {
924 GST_FORMAT_DEFAULT, /* a "frame", ie a set of samples per Hz */
932 gst_wavparse_pad_convert (GstPad *pad,
933 GstFormat src_format, gint64 src_value,
934 GstFormat *dest_format, gint64 *dest_value)
936 gint bytes_per_sample;
938 GstWavParse *wavparse;
940 wavparse = GST_WAVPARSE (gst_pad_get_parent (pad));
942 bytes_per_sample = wavparse->channels * wavparse->width / 8;
943 if (bytes_per_sample == 0) {
944 GST_DEBUG ("bytes_per_sample is 0, probably an mp3 - channels %d, width %d\n",
945 wavparse->channels, wavparse->width);
948 byterate = (glong) (bytes_per_sample * wavparse->rate);
950 g_warning ("byterate is 0, internal error\n");
953 GST_DEBUG ("bytes per sample: %d\n", bytes_per_sample);
955 switch (src_format) {
956 case GST_FORMAT_BYTES:
957 if (*dest_format == GST_FORMAT_DEFAULT)
958 *dest_value = src_value / bytes_per_sample;
959 else if (*dest_format == GST_FORMAT_TIME)
960 *dest_value = src_value * GST_SECOND / byterate;
964 case GST_FORMAT_DEFAULT:
965 if (*dest_format == GST_FORMAT_BYTES)
966 *dest_value = src_value * bytes_per_sample;
967 else if (*dest_format == GST_FORMAT_TIME)
968 *dest_value = src_value * GST_SECOND / wavparse->rate;
972 case GST_FORMAT_TIME:
973 if (*dest_format == GST_FORMAT_BYTES)
974 *dest_value = src_value * byterate / GST_SECOND;
975 else if (*dest_format == GST_FORMAT_DEFAULT)
976 *dest_value = src_value * wavparse->rate / GST_SECOND;
980 *dest_value = *dest_value & ~(bytes_per_sample - 1);
983 g_warning ("unhandled format for wavparse\n");
989 static const GstQueryType *
990 gst_wavparse_get_query_types (GstPad *pad)
992 static const GstQueryType types[] = {
1000 /* handle queries for location and length in requested format */
1002 gst_wavparse_pad_query (GstPad *pad, GstQueryType type,
1003 GstFormat *format, gint64 *value)
1005 GstFormat peer_format = GST_FORMAT_BYTES;
1007 GstWavParse *wavparse;
1009 /* probe sink's peer pad, convert value, and that's it :) */
1010 /* FIXME: ideally we'd loop over possible formats of peer instead
1011 * of only using BYTE */
1012 wavparse = GST_WAVPARSE (gst_pad_get_parent (pad));
1013 if (!gst_pad_query (GST_PAD_PEER (wavparse->sinkpad), type,
1014 &peer_format, &peer_value)) {
1015 g_warning ("Could not query sink pad's peer\n");
1018 if (!gst_pad_convert (wavparse->sinkpad, peer_format, peer_value,
1020 g_warning ("Could not query sink pad's peer\n");
1023 GST_DEBUG ("pad_query done, value %" G_GINT64_FORMAT "\n", *value);
1027 static const GstEventMask*
1028 gst_wavparse_get_event_masks (GstPad *pad)
1030 static const GstEventMask gst_wavparse_src_event_masks[] = {
1031 { GST_EVENT_SEEK, GST_SEEK_METHOD_SET |
1032 GST_SEEK_FLAG_FLUSH },
1035 return gst_wavparse_src_event_masks;
1039 gst_wavparse_srcpad_event (GstPad *pad, GstEvent *event)
1042 GstWavParse *wavparse = GST_WAVPARSE (GST_PAD_PARENT (pad));
1043 gboolean res = FALSE;
1045 GST_DEBUG ("event %d", GST_EVENT_TYPE (event));
1047 switch (GST_EVENT_TYPE (event)) {
1048 case GST_EVENT_SEEK:
1053 /* we can only seek when in the DATA state */
1054 if (wavparse->state != GST_WAVPARSE_DATA) {
1058 format = GST_FORMAT_BYTES;
1060 /* bring format to bytes for the peer element,
1061 * FIXME be smarter here */
1062 res = gst_pad_convert (pad,
1063 GST_EVENT_SEEK_FORMAT (event),
1064 GST_EVENT_SEEK_OFFSET (event),
1069 /* ok, seek worked, update our state */
1070 wavparse->seek_offset = byteoffset;
1071 wavparse->seek_pending = TRUE;
1072 wavparse->need_discont = TRUE;
1080 gst_event_unref (event);
1087 static GstElementStateReturn
1088 gst_wavparse_change_state (GstElement *element)
1090 GstWavParse *wavparse = GST_WAVPARSE (element);
1092 switch (GST_STATE_TRANSITION (element)) {
1093 case GST_STATE_NULL_TO_READY:
1095 case GST_STATE_READY_TO_PAUSED:
1096 wavparse->bs = gst_bytestream_new (wavparse->sinkpad);
1097 wavparse->state = GST_WAVPARSE_START;
1099 case GST_STATE_PAUSED_TO_PLAYING:
1101 case GST_STATE_PLAYING_TO_PAUSED:
1103 case GST_STATE_PAUSED_TO_READY:
1104 gst_bytestream_destroy (wavparse->bs);
1105 wavparse->state = GST_WAVPARSE_UNKNOWN;
1107 wavparse->seek_pending = FALSE;
1108 wavparse->seek_offset = 0;
1109 gst_caps_replace (&wavparse->metadata, NULL);
1112 case GST_STATE_READY_TO_NULL:
1116 if (GST_ELEMENT_CLASS (parent_class)->change_state)
1117 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
1119 return GST_STATE_SUCCESS;
1123 plugin_init (GstPlugin *plugin)
1125 if (!gst_library_load ("gstbytestream")) {
1129 return gst_element_register (plugin, "wavparse", GST_RANK_SECONDARY, GST_TYPE_WAVPARSE);
1136 "Parse a .wav file into raw audio",