2001-12-22 23:27:31 +00:00
|
|
|
/* Gnome-Streamer
|
|
|
|
* Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <gstwavparse.h>
|
|
|
|
|
2002-01-30 10:20:58 +00:00
|
|
|
static void gst_wavparse_class_init (GstWavParseClass *klass);
|
|
|
|
static void gst_wavparse_init (GstWavParse *wavparse);
|
2001-12-22 23:27:31 +00:00
|
|
|
|
|
|
|
static GstCaps* wav_typefind (GstBuffer *buf, gpointer private);
|
|
|
|
|
2002-01-30 10:20:58 +00:00
|
|
|
static void gst_wavparse_chain (GstPad *pad, GstBuffer *buf);
|
2001-12-22 23:27:31 +00:00
|
|
|
|
|
|
|
/* elementfactory information */
|
2002-01-30 10:20:58 +00:00
|
|
|
static GstElementDetails gst_wavparse_details = {
|
2001-12-22 23:27:31 +00:00
|
|
|
".wav parser",
|
|
|
|
"Parser/Audio",
|
|
|
|
"Parse a .wav file into raw audio",
|
|
|
|
VERSION,
|
|
|
|
"Erik Walthinsen <omega@cse.ogi.edu>",
|
|
|
|
"(C) 1999",
|
|
|
|
};
|
|
|
|
|
|
|
|
GST_PADTEMPLATE_FACTORY (sink_template_factory,
|
2002-01-30 10:20:58 +00:00
|
|
|
"wavparse_sink",
|
2001-12-22 23:27:31 +00:00
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_ALWAYS,
|
|
|
|
GST_CAPS_NEW (
|
2002-01-30 10:20:58 +00:00
|
|
|
"wavparse_wav",
|
2001-12-22 23:27:31 +00:00
|
|
|
"audio/wav",
|
|
|
|
NULL
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
GST_PADTEMPLATE_FACTORY (src_template_factory,
|
2002-01-30 10:20:58 +00:00
|
|
|
"wavparse_src",
|
2001-12-22 23:27:31 +00:00
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_ALWAYS,
|
|
|
|
GST_CAPS_NEW (
|
2002-01-30 10:20:58 +00:00
|
|
|
"wavparse_raw",
|
2001-12-22 23:27:31 +00:00
|
|
|
"audio/raw",
|
|
|
|
"format", GST_PROPS_STRING ("int"),
|
|
|
|
"law", GST_PROPS_INT (0),
|
|
|
|
"endianness", GST_PROPS_INT (G_BYTE_ORDER),
|
|
|
|
"signed", GST_PROPS_BOOLEAN (TRUE),
|
|
|
|
"width", GST_PROPS_LIST (
|
|
|
|
GST_PROPS_INT (8),
|
|
|
|
GST_PROPS_INT (16)
|
|
|
|
),
|
|
|
|
"depth", GST_PROPS_LIST (
|
|
|
|
GST_PROPS_INT (8),
|
|
|
|
GST_PROPS_INT (16)
|
|
|
|
),
|
|
|
|
"rate", GST_PROPS_INT_RANGE (8000, 48000),
|
|
|
|
"channels", GST_PROPS_INT_RANGE (1, 2)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
/* typefactory for 'wav' */
|
|
|
|
static GstTypeDefinition
|
|
|
|
wavdefinition =
|
|
|
|
{
|
2002-01-30 10:20:58 +00:00
|
|
|
"wavparse_audio/wav",
|
2001-12-22 23:27:31 +00:00
|
|
|
"audio/wav",
|
|
|
|
".wav",
|
|
|
|
wav_typefind,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2002-01-30 10:20:58 +00:00
|
|
|
/* WavParse signals and args */
|
2001-12-22 23:27:31 +00:00
|
|
|
enum {
|
|
|
|
/* FILL ME */
|
|
|
|
LAST_SIGNAL
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
ARG_0,
|
|
|
|
/* FILL ME */
|
|
|
|
};
|
|
|
|
|
|
|
|
static GstElementClass *parent_class = NULL;
|
2002-01-30 10:20:58 +00:00
|
|
|
//static guint gst_wavparse_signals[LAST_SIGNAL] = { 0 };
|
2001-12-22 23:27:31 +00:00
|
|
|
|
|
|
|
GType
|
2002-01-30 10:20:58 +00:00
|
|
|
gst_wavparse_get_type (void)
|
2001-12-22 23:27:31 +00:00
|
|
|
{
|
2002-01-30 10:20:58 +00:00
|
|
|
static GType wavparse_type = 0;
|
2001-12-22 23:27:31 +00:00
|
|
|
|
2002-01-30 10:20:58 +00:00
|
|
|
if (!wavparse_type) {
|
|
|
|
static const GTypeInfo wavparse_info = {
|
|
|
|
sizeof(GstWavParseClass), NULL,
|
2001-12-22 23:27:31 +00:00
|
|
|
NULL,
|
2002-01-30 10:20:58 +00:00
|
|
|
(GClassInitFunc) gst_wavparse_class_init,
|
2001-12-22 23:27:31 +00:00
|
|
|
NULL,
|
|
|
|
NULL,
|
2002-01-30 10:20:58 +00:00
|
|
|
sizeof(GstWavParse),
|
2001-12-22 23:27:31 +00:00
|
|
|
0,
|
2002-01-30 10:20:58 +00:00
|
|
|
(GInstanceInitFunc) gst_wavparse_init,
|
2001-12-22 23:27:31 +00:00
|
|
|
};
|
2002-01-30 10:20:58 +00:00
|
|
|
wavparse_type = g_type_register_static (GST_TYPE_ELEMENT, "GstWavParse", &wavparse_info, 0);
|
2001-12-22 23:27:31 +00:00
|
|
|
}
|
2002-01-30 10:20:58 +00:00
|
|
|
return wavparse_type;
|
2001-12-22 23:27:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2002-01-30 10:20:58 +00:00
|
|
|
gst_wavparse_class_init (GstWavParseClass *klass)
|
2001-12-22 23:27:31 +00:00
|
|
|
{
|
|
|
|
GstElementClass *gstelement_class;
|
|
|
|
|
|
|
|
gstelement_class = (GstElementClass*) klass;
|
|
|
|
|
|
|
|
parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2002-01-30 10:20:58 +00:00
|
|
|
gst_wavparse_init (GstWavParse *wavparse)
|
2001-12-22 23:27:31 +00:00
|
|
|
{
|
2002-01-30 10:20:58 +00:00
|
|
|
wavparse->sinkpad = gst_pad_new_from_template (GST_PADTEMPLATE_GET (sink_template_factory), "sink");
|
|
|
|
gst_element_add_pad (GST_ELEMENT (wavparse), wavparse->sinkpad);
|
|
|
|
gst_pad_set_chain_function (wavparse->sinkpad, gst_wavparse_chain);
|
2001-12-22 23:27:31 +00:00
|
|
|
|
2002-01-30 10:20:58 +00:00
|
|
|
wavparse->srcpad = gst_pad_new_from_template (GST_PADTEMPLATE_GET (src_template_factory), "src");
|
|
|
|
gst_element_add_pad (GST_ELEMENT (wavparse), wavparse->srcpad);
|
2001-12-22 23:27:31 +00:00
|
|
|
|
2002-01-30 10:20:58 +00:00
|
|
|
wavparse->riff = NULL;
|
2001-12-22 23:27:31 +00:00
|
|
|
|
2002-01-30 10:20:58 +00:00
|
|
|
wavparse->state = GST_WAVPARSE_UNKNOWN;
|
|
|
|
wavparse->riff = NULL;
|
|
|
|
wavparse->riff_nextlikely = 0;
|
|
|
|
wavparse->size = 0;
|
|
|
|
wavparse->bps = 0;
|
2001-12-22 23:27:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GstCaps*
|
|
|
|
wav_typefind (GstBuffer *buf, gpointer private)
|
|
|
|
{
|
|
|
|
gchar *data = GST_BUFFER_DATA (buf);
|
|
|
|
|
|
|
|
if (strncmp (&data[0], "RIFF", 4)) return NULL;
|
|
|
|
if (strncmp (&data[8], "WAVE", 4)) return NULL;
|
|
|
|
|
|
|
|
return gst_caps_new ("wav_typefind", "audio/wav", NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2002-01-30 10:20:58 +00:00
|
|
|
gst_wavparse_chain (GstPad *pad, GstBuffer *buf)
|
2001-12-22 23:27:31 +00:00
|
|
|
{
|
2002-01-30 10:20:58 +00:00
|
|
|
GstWavParse *wavparse;
|
2001-12-22 23:27:31 +00:00
|
|
|
gboolean buffer_riffed = FALSE; /* so we don't parse twice */
|
|
|
|
gchar *data;
|
|
|
|
gulong size;
|
|
|
|
|
|
|
|
g_return_if_fail (pad != NULL);
|
|
|
|
g_return_if_fail (GST_IS_PAD (pad));
|
|
|
|
g_return_if_fail (buf != NULL);
|
|
|
|
g_return_if_fail (GST_BUFFER_DATA (buf) != NULL);
|
|
|
|
|
2002-01-30 10:20:58 +00:00
|
|
|
wavparse = GST_WAVPARSE (gst_pad_get_parent (pad));
|
|
|
|
GST_DEBUG (0, "gst_wavparse_chain: got buffer in '%s'\n",
|
|
|
|
gst_object_get_name (GST_OBJECT (wavparse)));
|
2001-12-22 23:27:31 +00:00
|
|
|
data = (guchar *) GST_BUFFER_DATA (buf);
|
|
|
|
size = GST_BUFFER_SIZE (buf);
|
|
|
|
|
|
|
|
/* walk through the states in priority order */
|
|
|
|
/* we're in the data region */
|
2002-01-30 10:20:58 +00:00
|
|
|
if (wavparse->state == GST_WAVPARSE_DATA) {
|
2001-12-22 23:27:31 +00:00
|
|
|
/* if we're expected to see a new chunk in this buffer */
|
2002-01-30 10:20:58 +00:00
|
|
|
if ((wavparse->riff_nextlikely - GST_BUFFER_OFFSET (buf)) < GST_BUFFER_SIZE (buf)) {
|
2001-12-22 23:27:31 +00:00
|
|
|
|
2002-01-30 10:20:58 +00:00
|
|
|
GST_BUFFER_SIZE (buf) = wavparse->riff_nextlikely - GST_BUFFER_OFFSET (buf);
|
2001-12-22 23:27:31 +00:00
|
|
|
|
2002-01-30 10:20:58 +00:00
|
|
|
wavparse->state = GST_WAVPARSE_OTHER;
|
2001-12-22 23:27:31 +00:00
|
|
|
/* I suppose we could signal an EOF at this point, but that may be
|
|
|
|
premature. We've stopped data flow, that's the main thing. */
|
|
|
|
}
|
2002-01-30 10:20:58 +00:00
|
|
|
gst_pad_push (wavparse->srcpad, buf);
|
2001-12-22 23:27:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-01-30 10:20:58 +00:00
|
|
|
if (wavparse->state == GST_WAVPARSE_OTHER) {
|
2001-12-22 23:27:31 +00:00
|
|
|
GST_DEBUG (0, "we're in unknown territory here, not passing on\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* here we deal with parsing out the primary state */
|
|
|
|
/* these are sequenced such that in the normal case each (RIFF/WAVE,
|
|
|
|
fmt, data) will fire in sequence, as they should */
|
|
|
|
|
|
|
|
/* we're in null state now, look for the RIFF header, start parsing */
|
2002-01-30 10:20:58 +00:00
|
|
|
if (wavparse->state == GST_WAVPARSE_UNKNOWN) {
|
2001-12-22 23:27:31 +00:00
|
|
|
gint retval;
|
|
|
|
|
2002-01-30 10:20:58 +00:00
|
|
|
GST_DEBUG (0, "GstWavParse: checking for RIFF format\n");
|
2001-12-22 23:27:31 +00:00
|
|
|
|
|
|
|
/* create a new RIFF parser */
|
2002-01-30 10:20:58 +00:00
|
|
|
wavparse->riff = gst_riff_new ();
|
2001-12-22 23:27:31 +00:00
|
|
|
|
|
|
|
/* give it the current buffer to start parsing */
|
2002-01-30 10:20:58 +00:00
|
|
|
retval = gst_riff_next_buffer (wavparse->riff, buf, 0);
|
2001-12-22 23:27:31 +00:00
|
|
|
buffer_riffed = TRUE;
|
|
|
|
if (retval < 0) {
|
|
|
|
GST_DEBUG (0, "sorry, isn't RIFF\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* this has to be a file of form WAVE for us to deal with it */
|
2002-01-30 10:20:58 +00:00
|
|
|
if (wavparse->riff->form != gst_riff_fourcc_to_id ("WAVE")) {
|
2001-12-22 23:27:31 +00:00
|
|
|
GST_DEBUG (0, "sorry, isn't WAVE\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* at this point we're waiting for the 'fmt ' chunk */
|
2002-01-30 10:20:58 +00:00
|
|
|
wavparse->state = GST_WAVPARSE_CHUNK_FMT;
|
2001-12-22 23:27:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* we're now looking for the 'fmt ' chunk to get the audio info */
|
2002-01-30 10:20:58 +00:00
|
|
|
if (wavparse->state == GST_WAVPARSE_CHUNK_FMT) {
|
2001-12-22 23:27:31 +00:00
|
|
|
GstRiffChunk *fmt;
|
2002-01-30 10:20:58 +00:00
|
|
|
GstWavParseFormat *format;
|
2001-12-22 23:27:31 +00:00
|
|
|
|
2002-01-30 10:20:58 +00:00
|
|
|
GST_DEBUG (0, "GstWavParse: looking for fmt chunk\n");
|
2001-12-22 23:27:31 +00:00
|
|
|
|
|
|
|
/* there's a good possibility we may not have parsed this buffer */
|
|
|
|
if (buffer_riffed == FALSE) {
|
2002-01-30 10:20:58 +00:00
|
|
|
gst_riff_next_buffer (wavparse->riff, buf, GST_BUFFER_OFFSET (buf));
|
2001-12-22 23:27:31 +00:00
|
|
|
buffer_riffed = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* see if the fmt chunk is available yet */
|
2002-01-30 10:20:58 +00:00
|
|
|
fmt = gst_riff_get_chunk (wavparse->riff, "fmt ");
|
2001-12-22 23:27:31 +00:00
|
|
|
|
|
|
|
/* if we've got something, deal with it */
|
|
|
|
if (fmt != NULL) {
|
2002-02-02 13:58:48 +00:00
|
|
|
GstCaps *caps;
|
|
|
|
|
2001-12-22 23:27:31 +00:00
|
|
|
|
|
|
|
/* we can gather format information now */
|
2002-01-30 10:20:58 +00:00
|
|
|
format = (GstWavParseFormat *)((guchar *) GST_BUFFER_DATA (buf) + fmt->offset);
|
2001-12-22 23:27:31 +00:00
|
|
|
|
|
|
|
/* set the caps on the src pad */
|
2002-02-02 13:58:48 +00:00
|
|
|
caps = GST_CAPS_NEW (
|
|
|
|
"parsewav_src",
|
2002-01-13 22:27:25 +00:00
|
|
|
"audio/raw",
|
|
|
|
"format", GST_PROPS_STRING ("int"),
|
|
|
|
"law", GST_PROPS_INT (0), //FIXME
|
|
|
|
"endianness", GST_PROPS_INT (G_BYTE_ORDER),
|
|
|
|
"signed", GST_PROPS_BOOLEAN (TRUE), //FIXME
|
|
|
|
"width", GST_PROPS_INT (format->wBitsPerSample),
|
|
|
|
"depth", GST_PROPS_INT (format->wBitsPerSample),
|
|
|
|
"rate", GST_PROPS_INT (format->dwSamplesPerSec),
|
|
|
|
"channels", GST_PROPS_INT (format->wChannels)
|
2002-02-02 13:58:48 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
gst_pad_try_set_caps (wavparse->srcpad, caps);
|
2001-12-22 23:27:31 +00:00
|
|
|
|
2002-01-30 10:20:58 +00:00
|
|
|
wavparse->bps = format->wBlockAlign;
|
2001-12-22 23:27:31 +00:00
|
|
|
GST_DEBUG (0, "frequency %d, channels %d\n",
|
|
|
|
format->dwSamplesPerSec, format->wChannels);
|
|
|
|
|
|
|
|
/* we're now looking for the data chunk */
|
2002-01-30 10:20:58 +00:00
|
|
|
wavparse->state = GST_WAVPARSE_CHUNK_DATA;
|
2001-12-22 23:27:31 +00:00
|
|
|
} else {
|
|
|
|
/* otherwise we just sort of give up for this buffer */
|
|
|
|
gst_buffer_unref (buf);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now we look for the data chunk */
|
2002-01-30 10:20:58 +00:00
|
|
|
if (wavparse->state == GST_WAVPARSE_CHUNK_DATA) {
|
2001-12-22 23:27:31 +00:00
|
|
|
GstBuffer *newbuf;
|
|
|
|
GstRiffChunk *datachunk;
|
|
|
|
|
2002-01-30 10:20:58 +00:00
|
|
|
GST_DEBUG (0, "GstWavParse: looking for data chunk\n");
|
2001-12-22 23:27:31 +00:00
|
|
|
|
|
|
|
/* again, we might need to parse the buffer */
|
|
|
|
if (buffer_riffed == FALSE) {
|
2002-01-30 10:20:58 +00:00
|
|
|
gst_riff_next_buffer (wavparse->riff, buf, GST_BUFFER_OFFSET (buf));
|
2001-12-22 23:27:31 +00:00
|
|
|
buffer_riffed = TRUE;
|
|
|
|
}
|
|
|
|
|
2002-01-30 10:20:58 +00:00
|
|
|
datachunk = gst_riff_get_chunk (wavparse->riff, "data");
|
2001-12-22 23:27:31 +00:00
|
|
|
|
|
|
|
if (datachunk != NULL) {
|
|
|
|
gulong subsize;
|
|
|
|
|
|
|
|
GST_DEBUG (0, "data begins at %ld\n", datachunk->offset);
|
|
|
|
|
|
|
|
/* at this point we can ACK that we have data */
|
2002-01-30 10:20:58 +00:00
|
|
|
wavparse->state = GST_WAVPARSE_DATA;
|
2001-12-22 23:27:31 +00:00
|
|
|
|
|
|
|
/* now we construct a new buffer for the remainder */
|
|
|
|
subsize = size - datachunk->offset;
|
|
|
|
GST_DEBUG (0, "sending last %ld bytes along as audio\n", subsize);
|
|
|
|
|
|
|
|
newbuf = gst_buffer_new ();
|
|
|
|
GST_BUFFER_DATA (newbuf) = g_malloc (subsize);
|
|
|
|
GST_BUFFER_SIZE (newbuf) = subsize;
|
|
|
|
|
|
|
|
memcpy (GST_BUFFER_DATA (newbuf), GST_BUFFER_DATA (buf) + datachunk->offset, subsize);
|
|
|
|
|
|
|
|
gst_buffer_unref (buf);
|
|
|
|
|
2002-01-30 10:20:58 +00:00
|
|
|
gst_pad_push (wavparse->srcpad, newbuf);
|
2001-12-22 23:27:31 +00:00
|
|
|
|
|
|
|
/* now we're ready to go, the next buffer should start data */
|
2002-01-30 10:20:58 +00:00
|
|
|
wavparse->state = GST_WAVPARSE_DATA;
|
2001-12-22 23:27:31 +00:00
|
|
|
|
|
|
|
/* however, we may be expecting another chunk at some point */
|
2002-01-30 10:20:58 +00:00
|
|
|
wavparse->riff_nextlikely = gst_riff_get_nextlikely (wavparse->riff);
|
2001-12-22 23:27:31 +00:00
|
|
|
} else {
|
|
|
|
/* otherwise we just sort of give up for this buffer */
|
|
|
|
gst_buffer_unref (buf);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
plugin_init (GModule *module, GstPlugin *plugin)
|
|
|
|
{
|
|
|
|
GstElementFactory *factory;
|
|
|
|
GstTypeFactory *type;
|
|
|
|
|
2002-01-30 10:20:58 +00:00
|
|
|
/* create an elementfactory for the wavparse element */
|
|
|
|
factory = gst_elementfactory_new ("wavparse", GST_TYPE_WAVPARSE,
|
|
|
|
&gst_wavparse_details);
|
2001-12-22 23:27:31 +00:00
|
|
|
g_return_val_if_fail(factory != NULL, FALSE);
|
|
|
|
|
|
|
|
/* register src pads */
|
|
|
|
gst_elementfactory_add_padtemplate (factory, GST_PADTEMPLATE_GET (sink_template_factory));
|
|
|
|
gst_elementfactory_add_padtemplate (factory, GST_PADTEMPLATE_GET (src_template_factory));
|
|
|
|
|
|
|
|
gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
|
|
|
|
|
|
|
|
type = gst_typefactory_new (&wavdefinition);
|
|
|
|
gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (type));
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
GstPluginDesc plugin_desc = {
|
|
|
|
GST_VERSION_MAJOR,
|
|
|
|
GST_VERSION_MINOR,
|
2002-01-30 10:20:58 +00:00
|
|
|
"wavparse",
|
2001-12-22 23:27:31 +00:00
|
|
|
plugin_init
|
|
|
|
};
|