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 "mpegversion", GST_PROPS_INT (1),
101 "rate", GST_PROPS_INT_RANGE (8000, 48000),
102 "channels", GST_PROPS_INT_RANGE (1, 2),
103 "layer", GST_PROPS_INT_RANGE (1, 3)
108 "rate", GST_PROPS_INT_RANGE (8000, 48000),
109 "channels", GST_PROPS_INT_RANGE (1, 2)
114 "rate", GST_PROPS_INT_RANGE (8000, 48000),
115 "channels", GST_PROPS_INT_RANGE (1, 2)
119 /* WavParse signals and args */
130 static GstElementClass *parent_class = NULL;
131 /*static guint gst_wavparse_signals[LAST_SIGNAL] = { 0 }; */
134 gst_wavparse_get_type (void)
136 static GType wavparse_type = 0;
138 if (!wavparse_type) {
139 static const GTypeInfo wavparse_info = {
140 sizeof(GstWavParseClass),
141 gst_wavparse_base_init,
143 (GClassInitFunc) gst_wavparse_class_init,
148 (GInstanceInitFunc) gst_wavparse_init,
150 wavparse_type = g_type_register_static (GST_TYPE_ELEMENT, "GstWavParse", &wavparse_info, 0);
152 return wavparse_type;
157 gst_wavparse_base_init (gpointer g_class)
159 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
161 gst_element_class_set_details (element_class, &gst_wavparse_details);
163 /* register src pads */
164 gst_element_class_add_pad_template (element_class, GST_PAD_TEMPLATE_GET (sink_template_factory));
165 gst_element_class_add_pad_template (element_class, GST_PAD_TEMPLATE_GET (src_template_factory));
168 gst_wavparse_class_init (GstWavParseClass *klass)
170 GstElementClass *gstelement_class;
171 GObjectClass *object_class;
173 gstelement_class = (GstElementClass*) klass;
174 object_class = (GObjectClass *) klass;
176 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
178 object_class->get_property = gst_wavparse_get_property;
179 gstelement_class->change_state = gst_wavparse_change_state;
181 g_object_class_install_property (object_class, PROP_METADATA,
182 g_param_spec_boxed ("metadata",
183 "Metadata", "Metadata",
189 gst_wavparse_init (GstWavParse *wavparse)
194 wavparse->sinkpad = gst_pad_new_from_template (GST_PAD_TEMPLATE_GET (sink_template_factory), "sink");
195 gst_element_add_pad (GST_ELEMENT (wavparse), wavparse->sinkpad);
197 gst_pad_set_formats_function (wavparse->sinkpad, gst_wavparse_get_formats);
198 gst_pad_set_convert_function (wavparse->sinkpad, gst_wavparse_pad_convert);
199 gst_pad_set_query_type_function (wavparse->sinkpad,
200 gst_wavparse_get_query_types);
201 gst_pad_set_query_function (wavparse->sinkpad, gst_wavparse_pad_query);
204 wavparse->srcpad = gst_pad_new_from_template (GST_PAD_TEMPLATE_GET (src_template_factory), "src");
205 gst_element_add_pad (GST_ELEMENT (wavparse), wavparse->srcpad);
206 gst_pad_set_formats_function (wavparse->srcpad, gst_wavparse_get_formats);
207 gst_pad_set_convert_function (wavparse->srcpad, gst_wavparse_pad_convert);
208 gst_pad_set_query_type_function (wavparse->srcpad,
209 gst_wavparse_get_query_types);
210 gst_pad_set_query_function (wavparse->srcpad, gst_wavparse_pad_query);
211 gst_pad_set_event_function (wavparse->srcpad, gst_wavparse_srcpad_event);
212 gst_pad_set_event_mask_function (wavparse->srcpad, gst_wavparse_get_event_masks);
214 gst_element_set_loop_function (GST_ELEMENT (wavparse), gst_wavparse_loop);
216 wavparse->state = GST_WAVPARSE_UNKNOWN;
218 wavparse->seek_pending = FALSE;
219 wavparse->seek_offset = 0;
221 props = gst_props_empty_new ();
223 /* Metadata is added later when we find it */
224 gst_caps_replace_sink (&wavparse->metadata,
225 gst_caps_new ("wav_metadata",
226 "application/x-gst-metadata",
231 gst_wavparse_get_property (GObject *object,
236 GstWavParse *wavparse;
238 wavparse = GST_WAVPARSE (object);
242 g_value_set_boxed (value, wavparse->metadata);
251 gst_wavparse_parse_adtl (GstWavParse *wavparse,
255 GstByteStream *bs = wavparse->bs;
256 gst_riff_chunk *temp_chunk, chunk;
258 struct _gst_riff_labl labl, *temp_labl;
259 struct _gst_riff_ltxt ltxt, *temp_ltxt;
260 struct _gst_riff_note note, *temp_note;
263 GstPropsEntry *entry;
267 props = wavparse->metadata->properties;
270 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, sizeof (gst_riff_chunk));
271 if (got_bytes != sizeof (gst_riff_chunk)) {
274 temp_chunk = (gst_riff_chunk *) tempdata;
276 chunk.id = GUINT32_FROM_LE (temp_chunk->id);
277 chunk.size = GUINT32_FROM_LE (temp_chunk->size);
279 if (chunk.size == 0) {
280 gst_bytestream_flush (bs, sizeof (gst_riff_chunk));
281 len -= sizeof (gst_riff_chunk);
286 case GST_RIFF_adtl_labl:
287 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, sizeof (struct _gst_riff_labl));
288 if (got_bytes != sizeof (struct _gst_riff_labl)) {
292 temp_labl = (struct _gst_riff_labl *) tempdata;
293 labl.id = GUINT32_FROM_LE (temp_labl->id);
294 labl.size = GUINT32_FROM_LE (temp_labl->size);
295 labl.identifier = GUINT32_FROM_LE (temp_labl->identifier);
297 gst_bytestream_flush (bs, sizeof (struct _gst_riff_labl));
298 len -= sizeof (struct _gst_riff_labl);
300 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, labl.size - 4);
301 if (got_bytes != labl.size - 4) {
305 label_name = (char *) tempdata;
307 gst_bytestream_flush (bs, ((labl.size - 4) + 1) & ~1);
308 len -= (( (labl.size - 4) + 1) & ~1);
310 new_caps = gst_caps_new ("label",
311 "application/x-gst-metadata",
313 "identifier", GST_PROPS_INT (labl.identifier),
314 "name", GST_PROPS_STRING (label_name),
317 if (gst_props_get (props, "labels", &caps, NULL)) {
318 caps = g_list_append (caps, new_caps);
320 caps = g_list_append (NULL, new_caps);
322 entry = gst_props_entry_new ("labels", GST_PROPS_GLIST (caps));
323 gst_props_add_entry (props, entry);
328 case GST_RIFF_adtl_ltxt:
329 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, sizeof (struct _gst_riff_ltxt));
330 if (got_bytes != sizeof (struct _gst_riff_ltxt)) {
334 temp_ltxt = (struct _gst_riff_ltxt *) tempdata;
335 ltxt.id = GUINT32_FROM_LE (temp_ltxt->id);
336 ltxt.size = GUINT32_FROM_LE (temp_ltxt->size);
337 ltxt.identifier = GUINT32_FROM_LE (temp_ltxt->identifier);
338 ltxt.length = GUINT32_FROM_LE (temp_ltxt->length);
339 ltxt.purpose = GUINT32_FROM_LE (temp_ltxt->purpose);
340 ltxt.country = GUINT16_FROM_LE (temp_ltxt->country);
341 ltxt.language = GUINT16_FROM_LE (temp_ltxt->language);
342 ltxt.dialect = GUINT16_FROM_LE (temp_ltxt->dialect);
343 ltxt.codepage = GUINT16_FROM_LE (temp_ltxt->codepage);
345 gst_bytestream_flush (bs, sizeof (struct _gst_riff_ltxt));
346 len -= sizeof (struct _gst_riff_ltxt);
348 if (ltxt.size - 20 > 0) {
349 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, ltxt.size - 20);
350 if (got_bytes != ltxt.size - 20) {
354 gst_bytestream_flush (bs, ((ltxt.size - 20) + 1) & ~1);
355 len -= (( (ltxt.size - 20) + 1) & ~1);
357 label_name = (char *) tempdata;
362 new_caps = gst_caps_new ("ltxt",
363 "application/x-gst-metadata",
365 "identifier", GST_PROPS_INT (ltxt.identifier),
366 "name", GST_PROPS_STRING (label_name),
367 "length", GST_PROPS_INT (ltxt.length),
370 if (gst_props_get (props, "ltxts", &caps, NULL)) {
371 caps = g_list_append (caps, new_caps);
373 caps = g_list_append (NULL, new_caps);
375 entry = gst_props_entry_new ("ltxts", GST_PROPS_GLIST (caps));
376 gst_props_add_entry (props, entry);
381 case GST_RIFF_adtl_note:
382 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, sizeof (struct _gst_riff_note));
383 if (got_bytes != sizeof (struct _gst_riff_note)) {
387 temp_note = (struct _gst_riff_note *) tempdata;
388 note.id = GUINT32_FROM_LE (temp_note->id);
389 note.size = GUINT32_FROM_LE (temp_note->size);
390 note.identifier = GUINT32_FROM_LE (temp_note->identifier);
392 gst_bytestream_flush (bs, sizeof (struct _gst_riff_note));
393 len -= sizeof (struct _gst_riff_note);
395 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, note.size - 4);
396 if (got_bytes != note.size - 4) {
400 gst_bytestream_flush (bs, ((note.size - 4) + 1) & ~1);
401 len -= (( (note.size - 4) + 1) & ~1);
403 label_name = (char *) tempdata;
405 new_caps = gst_caps_new ("note",
406 "application/x-gst-metadata",
408 "identifier", GST_PROPS_INT (note.identifier),
409 "name", GST_PROPS_STRING (label_name),
412 if (gst_props_get (props, "notes", &caps, NULL)) {
413 caps = g_list_append (caps, new_caps);
415 caps = g_list_append (NULL, new_caps);
417 entry = gst_props_entry_new ("notes", GST_PROPS_GLIST (caps));
418 gst_props_add_entry (props, entry);
424 g_print ("Unknown chunk: " GST_FOURCC_FORMAT "\n", GST_FOURCC_ARGS(chunk.id));
429 g_object_notify (G_OBJECT (wavparse), "metadata");
433 gst_wavparse_parse_info (GstWavParse *wavparse,
436 gst_riff_chunk *temp_chunk, chunk;
437 GstByteStream *bs = wavparse->bs;
443 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, sizeof (gst_riff_chunk));
444 temp_chunk = (gst_riff_chunk *) tempdata;
446 chunk.id = GUINT32_FROM_LE (temp_chunk->id);
447 chunk.size = GUINT32_FROM_LE (temp_chunk->size);
449 gst_bytestream_flush (bs, sizeof (gst_riff_chunk));
450 if (got_bytes != sizeof (gst_riff_chunk)) {
454 /* move our pointer on past the header */
455 len -= sizeof (gst_riff_chunk);
457 if (chunk.size == 0) {
461 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, chunk.size);
462 name = (char *) tempdata;
463 if (got_bytes != chunk.size) {
467 /* move our pointer on past the data ... on an even boundary */
468 gst_bytestream_flush (bs, (chunk.size + 1) & ~1);
469 len -= ((chunk.size + 1) & ~1);
471 /* We now have an info string in 'name' of type chunk.id
474 case GST_RIFF_INFO_IARL:
478 case GST_RIFF_INFO_IART:
482 case GST_RIFF_INFO_ICMS:
483 type = "Commissioner";
486 case GST_RIFF_INFO_ICMT:
490 case GST_RIFF_INFO_ICOP:
494 case GST_RIFF_INFO_ICRD:
495 type = "Creation Date";
498 case GST_RIFF_INFO_IENG:
502 case GST_RIFF_INFO_IGNR:
506 case GST_RIFF_INFO_IKEY:
510 case GST_RIFF_INFO_INAM:
511 type = "Title"; /* name */
514 case GST_RIFF_INFO_IPRD:
518 case GST_RIFF_INFO_ISBJ:
522 case GST_RIFF_INFO_ISFT:
526 case GST_RIFF_INFO_ITCH:
531 g_print ("Unknown: %4.4s\n", (char *) &chunk.id);
537 GstPropsEntry *entry;
539 entry = gst_props_entry_new (type, GST_PROPS_STRING (name));
540 gst_props_add_entry (wavparse->metadata->properties, entry);
544 g_object_notify (G_OBJECT (wavparse), "metadata");
548 gst_wavparse_parse_cues (GstWavParse *wavparse,
552 GstByteStream *bs = wavparse->bs;
553 struct _gst_riff_cue *temp_cue, cue;
554 struct _gst_riff_cuepoints *points;
558 GstPropsEntry *entry;
563 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, sizeof (struct _gst_riff_cue));
564 temp_cue = (struct _gst_riff_cue *) tempdata;
566 /* fixup for our big endian friends */
567 cue.id = GUINT32_FROM_LE (temp_cue->id);
568 cue.size = GUINT32_FROM_LE (temp_cue->size);
569 cue.cuepoints = GUINT32_FROM_LE (temp_cue->cuepoints);
571 gst_bytestream_flush (bs, sizeof (struct _gst_riff_cue));
572 if (got_bytes != sizeof (struct _gst_riff_cue)) {
576 len -= sizeof (struct _gst_riff_cue);
578 /* -4 because cue.size contains the cuepoints size
579 and we've already flushed that out of the system */
580 required = cue.size - 4;
581 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, required);
582 gst_bytestream_flush (bs, ((required) + 1) & ~1);
583 if (got_bytes != required) {
587 len -= ( ((cue.size - 4) + 1) & ~1);
589 /* now we have an array of struct _gst_riff_cuepoints in tempdata */
590 points = (struct _gst_riff_cuepoints *) tempdata;
592 for (i = 0; i < cue.cuepoints; i++) {
595 caps = gst_caps_new ("cues",
596 "application/x-gst-metadata",
598 "identifier", GST_PROPS_INT (points[i].identifier),
599 "position", GST_PROPS_INT (points[i].offset),
601 cues = g_list_append (cues, caps);
604 entry = gst_props_entry_new ("cues", GST_PROPS_GLIST (cues));
605 gst_props_add_entry (wavparse->metadata->properties, entry);
608 g_object_notify (G_OBJECT (wavparse), "metadata");
612 gst_wavparse_parse_fmt (GstWavParse *wavparse)
614 GstWavParseFormat *format;
615 GstCaps *caps = NULL;
617 GstByteStream *bs = wavparse->bs;
620 got_bytes = gst_bytestream_peek_bytes (bs, &fmtdata, sizeof (GstWavParseFormat));
621 format = (GstWavParseFormat *) fmtdata;
623 if (got_bytes == sizeof (GstWavParseFormat)) {
624 gst_bytestream_flush (bs, got_bytes);
625 wavparse->bps = GUINT16_FROM_LE (format->wBlockAlign);
626 wavparse->rate = GUINT32_FROM_LE (format->dwSamplesPerSec);
627 wavparse->channels = GUINT16_FROM_LE (format->wChannels);
628 wavparse->width = GUINT16_FROM_LE (format->wBitsPerSample);
629 wavparse->format = GINT16_FROM_LE (format->wFormatTag);
631 /* set the caps on the src pad */
632 /* FIXME: handle all of the other formats as well */
633 switch (wavparse->format) {
634 case GST_RIFF_WAVE_FORMAT_ALAW:
635 case GST_RIFF_WAVE_FORMAT_MULAW: {
636 char *mime = (wavparse->format == GST_RIFF_WAVE_FORMAT_ALAW) ?
637 "audio/x-alaw" : "audio/x-mulaw";
638 if (wavparse->width != 8) {
639 g_warning ("Ignoring invalid width %d", wavparse->width);
643 caps = GST_CAPS_NEW ("parsewav_src",
645 "rate", GST_PROPS_INT (wavparse->rate),
646 "channels", GST_PROPS_INT (wavparse->channels)
650 case GST_RIFF_WAVE_FORMAT_PCM:
651 caps = GST_CAPS_NEW ("parsewav_src",
653 "endianness", GST_PROPS_INT (G_LITTLE_ENDIAN),
654 "signed", GST_PROPS_BOOLEAN ((wavparse->width > 8) ? TRUE : FALSE),
655 "width", GST_PROPS_INT (wavparse->width),
656 "depth", GST_PROPS_INT (wavparse->width),
657 "rate", GST_PROPS_INT (wavparse->rate),
658 "channels", GST_PROPS_INT (wavparse->channels)
662 case GST_RIFF_WAVE_FORMAT_MPEGL12:
663 case GST_RIFF_WAVE_FORMAT_MPEGL3: {
664 int layer = (wavparse->format == GST_RIFF_WAVE_FORMAT_MPEGL12) ? 2 : 3;
666 caps = GST_CAPS_NEW ("parsewav_src",
668 "mpegversion", GST_PROPS_INT (1),
669 "layer", GST_PROPS_INT (layer),
670 "rate", GST_PROPS_INT (wavparse->rate),
671 "channels", GST_PROPS_INT (wavparse->channels)
677 gst_element_error (GST_ELEMENT (wavparse), "wavparse: format %d not handled", wavparse->format);
681 if (gst_pad_try_set_caps (wavparse->srcpad, caps) <= 0) {
682 gst_element_error (GST_ELEMENT (wavparse), "Could not set caps");
686 GST_DEBUG ("frequency %d, channels %d",
687 wavparse->rate, wavparse->channels);
692 gst_wavparse_handle_sink_event (GstWavParse *wavparse)
699 gst_bytestream_get_status (wavparse->bs, &remaining, &event);
701 type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
702 GST_DEBUG ("wavparse: event %p %d", event, type);
706 gst_bytestream_flush (wavparse->bs, remaining);
707 gst_pad_event_default (wavparse->sinkpad, event);
711 case GST_EVENT_FLUSH:
712 g_warning ("Wavparse: Flush event");
716 g_warning ("Wavparse: Unhandled event %d", type);
720 gst_event_unref (event);
727 gst_wavparse_loop (GstElement *element)
729 GstWavParse *wavparse;
735 wavparse = GST_WAVPARSE (element);
739 if (wavparse->seek_pending) {
740 GST_DEBUG ("wavparse: seek pending to %" G_GINT64_FORMAT " %08llx",
741 wavparse->seek_offset,
742 (unsigned long long) wavparse->seek_offset);
744 if (!gst_bytestream_seek (bs, wavparse->seek_offset, GST_SEEK_METHOD_SET)) {
745 GST_INFO ("wavparse: Could not seek");
748 wavparse->seek_pending = FALSE;
751 if (wavparse->state == GST_WAVPARSE_DATA) {
755 /* This seems to want the whole chunk,
756 Will this screw up streaming?
757 Does anyone care about streaming wavs?
758 FIXME: Should we have a decent buffer size? */
760 #define MAX_BUFFER_SIZE 1024
762 if (wavparse->dataleft > 0) {
763 desired = MIN (wavparse->dataleft, MAX_BUFFER_SIZE);
764 got_bytes = gst_bytestream_peek (bs, &buf, desired);
766 if (got_bytes == 0) {
770 wavparse->dataleft -= got_bytes;
771 wavparse->byteoffset += got_bytes;
773 gst_bytestream_flush (bs, got_bytes);
775 gst_pad_push (wavparse->srcpad, GST_DATA (buf));
778 wavparse->state = GST_WAVPARSE_OTHER;
783 gst_riff_riff *temp_chunk;
787 /* read first two dwords to get chunktype and size */
789 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, sizeof (gst_riff_chunk));
790 temp_chunk = (gst_riff_riff *) tempdata;
792 if (got_bytes < sizeof (gst_riff_chunk)) {
793 if (!gst_wavparse_handle_sink_event (wavparse)) {
801 chunk.id = GUINT32_FROM_LE (temp_chunk->id);
802 chunk.size = GUINT32_FROM_LE (temp_chunk->size);
805 case GST_RIFF_TAG_RIFF:
806 case GST_RIFF_TAG_LIST:
807 /* Read complete list chunk */
809 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, sizeof (gst_riff_list));
810 temp_chunk = (gst_riff_riff *) tempdata;
811 if (got_bytes < sizeof (gst_riff_list)) {
812 if (!gst_wavparse_handle_sink_event (wavparse)) {
820 chunk.type = GUINT32_FROM_LE (temp_chunk->type);
821 skipsize = sizeof (gst_riff_list);
824 case GST_RIFF_TAG_cue:
829 skipsize = sizeof (gst_riff_chunk);
832 gst_bytestream_flush (bs, skipsize);
835 /* need to flush an even number of bytes at the end */
836 flush = (chunk.size + 1) & ~1;
838 switch (wavparse->state) {
839 case GST_WAVPARSE_START:
840 if (chunk.id != GST_RIFF_TAG_RIFF &&
841 chunk.type != GST_RIFF_RIFF_WAVE) {
842 gst_element_error (element, "This doesn't appear to be a WAV file %08x %08x", chunk.id, chunk.type);
846 wavparse->state = GST_WAVPARSE_OTHER;
847 /* We are not going to flush lists */
851 case GST_WAVPARSE_OTHER:
852 GST_DEBUG ("riff tag: %4.4s %08x", (char *) &chunk.id, chunk.size);
855 case GST_RIFF_TAG_data:
856 wavparse->state = GST_WAVPARSE_DATA;
857 wavparse->dataleft = chunk.size;
858 wavparse->byteoffset = 0;
863 case GST_RIFF_TAG_fmt:
864 gst_wavparse_parse_fmt (wavparse);
867 case GST_RIFF_TAG_cue:
868 gst_wavparse_parse_cues (wavparse, chunk.size);
871 case GST_RIFF_TAG_LIST:
872 GST_DEBUG ("list type: %4.4s", (char *) &chunk.type);
873 switch (chunk.type) {
874 case GST_RIFF_LIST_INFO:
875 gst_wavparse_parse_info (wavparse, chunk.size - 4);
880 case GST_RIFF_LIST_adtl:
881 gst_wavparse_parse_adtl (wavparse, chunk.size - 4);
894 case GST_WAVPARSE_DATA:
895 /* Should have been handled up there ^^^^ */
901 GST_DEBUG (" ***** unknown chunkid %08x", chunk.id);
908 res = gst_bytestream_flush (bs, flush);
913 gst_bytestream_get_status (bs, &remaining, &event);
914 gst_event_unref (event);
919 /* convert and query stuff */
920 static const GstFormat *
921 gst_wavparse_get_formats (GstPad *pad)
923 static GstFormat formats[] = {
926 GST_FORMAT_DEFAULT, /* a "frame", ie a set of samples per Hz */
934 gst_wavparse_pad_convert (GstPad *pad,
935 GstFormat src_format, gint64 src_value,
936 GstFormat *dest_format, gint64 *dest_value)
938 gint bytes_per_sample;
940 GstWavParse *wavparse;
942 wavparse = GST_WAVPARSE (gst_pad_get_parent (pad));
944 bytes_per_sample = wavparse->channels * wavparse->width / 8;
945 if (bytes_per_sample == 0) {
946 GST_DEBUG ("bytes_per_sample is 0, probably an mp3 - channels %d, width %d\n",
947 wavparse->channels, wavparse->width);
950 byterate = (glong) (bytes_per_sample * wavparse->rate);
952 g_warning ("byterate is 0, internal error\n");
955 GST_DEBUG ("bytes per sample: %d\n", bytes_per_sample);
957 switch (src_format) {
958 case GST_FORMAT_BYTES:
959 if (*dest_format == GST_FORMAT_DEFAULT)
960 *dest_value = src_value / bytes_per_sample;
961 else if (*dest_format == GST_FORMAT_TIME)
962 *dest_value = src_value * GST_SECOND / byterate;
966 case GST_FORMAT_DEFAULT:
967 if (*dest_format == GST_FORMAT_BYTES)
968 *dest_value = src_value * bytes_per_sample;
969 else if (*dest_format == GST_FORMAT_TIME)
970 *dest_value = src_value * GST_SECOND / wavparse->rate;
974 case GST_FORMAT_TIME:
975 if (*dest_format == GST_FORMAT_BYTES)
976 *dest_value = src_value * byterate / GST_SECOND;
977 else if (*dest_format == GST_FORMAT_DEFAULT)
978 *dest_value = src_value * wavparse->rate / GST_SECOND;
982 *dest_value = *dest_value & ~(bytes_per_sample - 1);
985 g_warning ("unhandled format for wavparse\n");
991 static const GstQueryType *
992 gst_wavparse_get_query_types (GstPad *pad)
994 static const GstQueryType types[] = {
1002 /* handle queries for location and length in requested format */
1004 gst_wavparse_pad_query (GstPad *pad, GstQueryType type,
1005 GstFormat *format, gint64 *value)
1007 GstFormat peer_format = GST_FORMAT_BYTES;
1009 GstWavParse *wavparse;
1011 /* probe sink's peer pad, convert value, and that's it :) */
1012 /* FIXME: ideally we'd loop over possible formats of peer instead
1013 * of only using BYTE */
1014 wavparse = GST_WAVPARSE (gst_pad_get_parent (pad));
1015 if (!gst_pad_query (GST_PAD_PEER (wavparse->sinkpad), type,
1016 &peer_format, &peer_value)) {
1017 g_warning ("Could not query sink pad's peer\n");
1020 if (!gst_pad_convert (wavparse->sinkpad, peer_format, peer_value,
1022 g_warning ("Could not query sink pad's peer\n");
1025 GST_DEBUG ("pad_query done, value %" G_GINT64_FORMAT "\n", *value);
1029 static const GstEventMask*
1030 gst_wavparse_get_event_masks (GstPad *pad)
1032 static const GstEventMask gst_wavparse_src_event_masks[] = {
1033 { GST_EVENT_SEEK, GST_SEEK_METHOD_SET |
1034 GST_SEEK_FLAG_FLUSH },
1037 return gst_wavparse_src_event_masks;
1041 gst_wavparse_srcpad_event (GstPad *pad, GstEvent *event)
1044 GstWavParse *wavparse = GST_WAVPARSE (GST_PAD_PARENT (pad));
1045 gboolean res = FALSE;
1047 GST_DEBUG ("event %d", GST_EVENT_TYPE (event));
1049 switch (GST_EVENT_TYPE (event)) {
1050 case GST_EVENT_SEEK:
1055 /* we can only seek when in the DATA state */
1056 if (wavparse->state != GST_WAVPARSE_DATA) {
1060 format = GST_FORMAT_BYTES;
1062 /* bring format to bytes for the peer element,
1063 * FIXME be smarter here */
1064 res = gst_pad_convert (pad,
1065 GST_EVENT_SEEK_FORMAT (event),
1066 GST_EVENT_SEEK_OFFSET (event),
1071 /* ok, seek worked, update our state */
1072 wavparse->seek_offset = byteoffset;
1073 wavparse->seek_pending = TRUE;
1074 wavparse->need_discont = TRUE;
1082 gst_event_unref (event);
1089 static GstElementStateReturn
1090 gst_wavparse_change_state (GstElement *element)
1092 GstWavParse *wavparse = GST_WAVPARSE (element);
1094 switch (GST_STATE_TRANSITION (element)) {
1095 case GST_STATE_NULL_TO_READY:
1097 case GST_STATE_READY_TO_PAUSED:
1098 wavparse->bs = gst_bytestream_new (wavparse->sinkpad);
1099 wavparse->state = GST_WAVPARSE_START;
1101 case GST_STATE_PAUSED_TO_PLAYING:
1103 case GST_STATE_PLAYING_TO_PAUSED:
1105 case GST_STATE_PAUSED_TO_READY:
1106 gst_bytestream_destroy (wavparse->bs);
1107 wavparse->state = GST_WAVPARSE_UNKNOWN;
1109 wavparse->seek_pending = FALSE;
1110 wavparse->seek_offset = 0;
1111 gst_caps_replace (&wavparse->metadata, NULL);
1114 case GST_STATE_READY_TO_NULL:
1118 if (GST_ELEMENT_CLASS (parent_class)->change_state)
1119 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
1121 return GST_STATE_SUCCESS;
1125 plugin_init (GstPlugin *plugin)
1127 if (!gst_library_load ("gstbytestream")) {
1131 return gst_element_register (plugin, "wavparse", GST_RANK_SECONDARY, GST_TYPE_WAVPARSE);
1138 "Parse a .wav file into raw audio",