2002-03-20 21:45:03 +00:00
|
|
|
/* GStreamer
|
2001-12-22 23:27:31 +00:00
|
|
|
* 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>
|
|
|
|
|
2003-03-01 18:34:38 +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
|
|
|
|
2003-03-01 18:34:38 +00:00
|
|
|
static GstCaps* wav_type_find (GstBuffer *buf, gpointer private);
|
2001-12-22 23:27:31 +00:00
|
|
|
|
2002-11-03 13:59:35 +00:00
|
|
|
static const GstFormat* gst_wavparse_get_formats (GstPad *pad);
|
2002-12-30 17:53:18 +00:00
|
|
|
static const GstQueryType *
|
2002-11-03 13:59:35 +00:00
|
|
|
gst_wavparse_get_query_types (GstPad *pad);
|
2003-03-01 18:34:38 +00:00
|
|
|
static gboolean gst_wavparse_pad_query (GstPad *pad,
|
|
|
|
GstQueryType type,
|
|
|
|
GstFormat *format,
|
|
|
|
gint64 *value);
|
|
|
|
static gboolean gst_wavparse_pad_convert (GstPad *pad,
|
|
|
|
GstFormat src_format,
|
|
|
|
gint64 src_value,
|
|
|
|
GstFormat *dest_format,
|
|
|
|
gint64 *dest_value);
|
|
|
|
static void gst_wavparse_chain (GstPad *pad, GstBuffer *buf);
|
|
|
|
|
|
|
|
static const GstEventMask*
|
|
|
|
gst_wavparse_get_event_masks (GstPad *pad);
|
|
|
|
static gboolean gst_wavparse_srcpad_event (GstPad *pad, GstEvent *event);
|
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",
|
2002-04-20 21:42:51 +00:00
|
|
|
"Codec/Parser",
|
2002-09-18 19:02:51 +00:00
|
|
|
"LGPL",
|
2001-12-22 23:27:31 +00:00
|
|
|
"Parse a .wav file into raw audio",
|
|
|
|
VERSION,
|
|
|
|
"Erik Walthinsen <omega@cse.ogi.edu>",
|
|
|
|
"(C) 1999",
|
|
|
|
};
|
|
|
|
|
2002-04-11 20:42:25 +00:00
|
|
|
GST_PAD_TEMPLATE_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 (
|
2003-01-14 14:01:49 +00:00
|
|
|
"wavparse_wav",
|
|
|
|
"audio/x-wav",
|
2001-12-22 23:27:31 +00:00
|
|
|
NULL
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2002-04-11 20:42:25 +00:00
|
|
|
GST_PAD_TEMPLATE_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 (
|
2003-01-14 14:01:49 +00:00
|
|
|
"wavparse_raw",
|
|
|
|
"audio/raw",
|
2001-12-22 23:27:31 +00:00
|
|
|
"format", GST_PROPS_STRING ("int"),
|
|
|
|
"law", GST_PROPS_INT (0),
|
2003-04-11 21:52:18 +00:00
|
|
|
"endianness", GST_PROPS_INT (G_LITTLE_ENDIAN),
|
2002-12-08 12:23:48 +00:00
|
|
|
"signed", GST_PROPS_LIST (
|
2003-01-14 14:01:49 +00:00
|
|
|
GST_PROPS_BOOLEAN (FALSE),
|
|
|
|
GST_PROPS_BOOLEAN (TRUE)
|
2002-12-08 12:23:48 +00:00
|
|
|
),
|
2001-12-22 23:27:31 +00:00
|
|
|
"width", GST_PROPS_LIST (
|
|
|
|
GST_PROPS_INT (8),
|
|
|
|
GST_PROPS_INT (16)
|
|
|
|
),
|
|
|
|
"depth", GST_PROPS_LIST (
|
|
|
|
GST_PROPS_INT (8),
|
|
|
|
GST_PROPS_INT (16)
|
|
|
|
),
|
2003-01-14 14:01:49 +00:00
|
|
|
"rate", GST_PROPS_INT_RANGE (8000, 48000),
|
2001-12-22 23:27:31 +00:00
|
|
|
"channels", GST_PROPS_INT_RANGE (1, 2)
|
2003-01-14 14:01:49 +00:00
|
|
|
),
|
|
|
|
GST_CAPS_NEW (
|
|
|
|
"wavparse_mp3",
|
|
|
|
"audio/x-mp3",
|
|
|
|
NULL
|
2003-04-11 21:52:18 +00:00
|
|
|
),
|
|
|
|
GST_CAPS_NEW (
|
|
|
|
"parsewav_law",
|
|
|
|
"audio/raw",
|
|
|
|
"format", GST_PROPS_STRING ("int"),
|
|
|
|
"law", GST_PROPS_INT_RANGE (1, 2),
|
|
|
|
"endianness", GST_PROPS_INT (G_LITTLE_ENDIAN),
|
|
|
|
"width", GST_PROPS_INT (8),
|
|
|
|
"depth", GST_PROPS_INT (8),
|
|
|
|
"rate", GST_PROPS_INT_RANGE (8000, 48000),
|
|
|
|
"channels", GST_PROPS_INT_RANGE (1, 2)
|
2001-12-22 23:27:31 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
/* typefactory for 'wav' */
|
2003-01-14 14:01:49 +00:00
|
|
|
static GstTypeDefinition
|
|
|
|
wavdefinition =
|
2001-12-22 23:27:31 +00:00
|
|
|
{
|
2002-04-26 12:44:20 +00:00
|
|
|
"wavparse_audio/x-wav",
|
|
|
|
"audio/x-wav",
|
2001-12-22 23:27:31 +00:00
|
|
|
".wav",
|
2002-04-11 20:42:25 +00:00
|
|
|
wav_type_find,
|
2001-12-22 23:27:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
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-03-19 04:10:05 +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-11-03 13:59:35 +00:00
|
|
|
/* sink */
|
2002-04-11 20:42:25 +00:00
|
|
|
wavparse->sinkpad = gst_pad_new_from_template (GST_PAD_TEMPLATE_GET (sink_template_factory), "sink");
|
2002-01-30 10:20:58 +00:00
|
|
|
gst_element_add_pad (GST_ELEMENT (wavparse), wavparse->sinkpad);
|
2001-12-22 23:27:31 +00:00
|
|
|
|
2002-11-03 13:59:35 +00:00
|
|
|
gst_pad_set_formats_function (wavparse->sinkpad, gst_wavparse_get_formats);
|
|
|
|
gst_pad_set_convert_function (wavparse->sinkpad, gst_wavparse_pad_convert);
|
|
|
|
gst_pad_set_query_type_function (wavparse->sinkpad,
|
|
|
|
gst_wavparse_get_query_types);
|
|
|
|
gst_pad_set_query_function (wavparse->sinkpad, gst_wavparse_pad_query);
|
|
|
|
|
|
|
|
/* source */
|
2002-04-11 20:42:25 +00:00
|
|
|
wavparse->srcpad = gst_pad_new_from_template (GST_PAD_TEMPLATE_GET (src_template_factory), "src");
|
2002-01-30 10:20:58 +00:00
|
|
|
gst_element_add_pad (GST_ELEMENT (wavparse), wavparse->srcpad);
|
2002-11-03 13:59:35 +00:00
|
|
|
gst_pad_set_formats_function (wavparse->srcpad, gst_wavparse_get_formats);
|
|
|
|
gst_pad_set_convert_function (wavparse->srcpad, gst_wavparse_pad_convert);
|
2003-01-14 14:01:49 +00:00
|
|
|
gst_pad_set_query_type_function (wavparse->srcpad,
|
2002-11-03 13:59:35 +00:00
|
|
|
gst_wavparse_get_query_types);
|
|
|
|
gst_pad_set_query_function (wavparse->srcpad, gst_wavparse_pad_query);
|
2003-03-01 18:34:38 +00:00
|
|
|
gst_pad_set_event_function (wavparse->srcpad, gst_wavparse_srcpad_event);
|
|
|
|
gst_pad_set_event_mask_function (wavparse->srcpad, gst_wavparse_get_event_masks);
|
2002-11-03 13:59:35 +00:00
|
|
|
|
|
|
|
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->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;
|
2003-03-01 18:34:38 +00:00
|
|
|
wavparse->offset = 0;
|
|
|
|
wavparse->need_discont = FALSE;
|
2001-12-22 23:27:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GstCaps*
|
2002-04-11 20:42:25 +00:00
|
|
|
wav_type_find (GstBuffer *buf, gpointer private)
|
2001-12-22 23:27:31 +00:00
|
|
|
{
|
|
|
|
gchar *data = GST_BUFFER_DATA (buf);
|
|
|
|
|
2003-04-11 21:52:18 +00:00
|
|
|
if (GST_BUFFER_SIZE (buf) < 12) return NULL;
|
2001-12-22 23:27:31 +00:00
|
|
|
if (strncmp (&data[0], "RIFF", 4)) return NULL;
|
|
|
|
if (strncmp (&data[8], "WAVE", 4)) return NULL;
|
|
|
|
|
2002-04-26 12:44:20 +00:00
|
|
|
return gst_caps_new ("wav_type_find", "audio/x-wav", NULL);
|
2001-12-22 23:27:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 */
|
|
|
|
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));
|
2002-03-24 22:07:03 +00:00
|
|
|
GST_DEBUG (0, "gst_wavparse_chain: got buffer in '%s'",
|
2002-01-30 10:20:58 +00:00
|
|
|
gst_object_get_name (GST_OBJECT (wavparse)));
|
2003-03-01 18:34:38 +00:00
|
|
|
|
2001-12-22 23:27:31 +00:00
|
|
|
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) {
|
2003-03-01 18:34:38 +00:00
|
|
|
GstFormat format;
|
|
|
|
guint64 maxsize;
|
|
|
|
|
|
|
|
/* we can't go beyond the max length */
|
|
|
|
maxsize = wavparse->riff_nextlikely - GST_BUFFER_OFFSET (buf);
|
|
|
|
|
2001-12-22 23:27:31 +00:00
|
|
|
/* if we're expected to see a new chunk in this buffer */
|
2003-03-01 18:34:38 +00:00
|
|
|
if (maxsize < size) {
|
|
|
|
GstBuffer *newbuf;
|
|
|
|
|
|
|
|
newbuf = gst_buffer_create_sub (buf, 0, maxsize);
|
|
|
|
gst_buffer_unref (buf);
|
|
|
|
buf = newbuf;
|
|
|
|
|
|
|
|
size = maxsize;
|
2003-01-14 14:01:49 +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. */
|
2003-01-14 14:01:49 +00:00
|
|
|
}
|
2002-05-30 04:43:38 +00:00
|
|
|
|
2003-03-01 18:34:38 +00:00
|
|
|
if (GST_PAD_IS_USABLE (wavparse->srcpad)) {
|
|
|
|
format = GST_FORMAT_TIME;
|
|
|
|
gst_pad_convert (wavparse->srcpad,
|
|
|
|
GST_FORMAT_BYTES,
|
|
|
|
wavparse->offset,
|
|
|
|
&format,
|
|
|
|
&GST_BUFFER_TIMESTAMP (buf));
|
|
|
|
|
|
|
|
if (wavparse->need_discont) {
|
2003-04-29 21:54:47 +00:00
|
|
|
if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
|
|
|
|
gst_pad_push (wavparse->srcpad,
|
|
|
|
GST_BUFFER (gst_event_new_discontinuous (FALSE,
|
|
|
|
GST_FORMAT_BYTES, wavparse->offset,
|
2003-03-01 18:34:38 +00:00
|
|
|
GST_FORMAT_TIME, GST_BUFFER_TIMESTAMP (buf),
|
|
|
|
NULL)));
|
2003-04-29 21:54:47 +00:00
|
|
|
} else {
|
|
|
|
gst_pad_push (wavparse->srcpad,
|
|
|
|
GST_BUFFER (gst_event_new_discontinuous (FALSE,
|
|
|
|
GST_FORMAT_BYTES, wavparse->offset,
|
|
|
|
NULL)));
|
|
|
|
}
|
2003-03-01 18:34:38 +00:00
|
|
|
wavparse->need_discont = FALSE;
|
|
|
|
}
|
2002-11-03 03:58:30 +00:00
|
|
|
gst_pad_push (wavparse->srcpad, buf);
|
2003-03-01 18:34:38 +00:00
|
|
|
}
|
2003-01-14 14:01:49 +00:00
|
|
|
else
|
|
|
|
gst_buffer_unref (buf);
|
2003-03-01 18:34:38 +00:00
|
|
|
|
|
|
|
wavparse->offset += size;
|
|
|
|
|
2001-12-22 23:27:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-01-30 10:20:58 +00:00
|
|
|
if (wavparse->state == GST_WAVPARSE_OTHER) {
|
2002-03-24 22:07:03 +00:00
|
|
|
GST_DEBUG (0, "we're in unknown territory here, not passing on");
|
2001-12-22 23:27:31 +00:00
|
|
|
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-03-24 22:07:03 +00:00
|
|
|
GST_DEBUG (0, "GstWavParse: checking for RIFF format");
|
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 ();
|
2003-01-14 14:01:49 +00:00
|
|
|
|
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) {
|
2002-03-24 22:07:03 +00:00
|
|
|
GST_DEBUG (0, "sorry, isn't RIFF");
|
2001-12-22 23:27:31 +00:00
|
|
|
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")) {
|
2002-03-24 22:07:03 +00:00
|
|
|
GST_DEBUG (0, "sorry, isn't WAVE");
|
2001-12-22 23:27:31 +00:00
|
|
|
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-03-24 22:07:03 +00:00
|
|
|
GST_DEBUG (0, "GstWavParse: looking for fmt chunk");
|
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) {
|
2003-01-14 14:01:49 +00:00
|
|
|
GstCaps *caps = NULL;
|
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
|
|
|
|
2002-12-08 12:23:48 +00:00
|
|
|
wavparse->bps = GUINT16_FROM_LE(format->wBlockAlign);
|
|
|
|
wavparse->rate = GUINT32_FROM_LE(format->dwSamplesPerSec);
|
|
|
|
wavparse->channels = GUINT16_FROM_LE(format->wChannels);
|
|
|
|
wavparse->width = GUINT16_FROM_LE(format->wBitsPerSample);
|
2003-01-14 14:01:49 +00:00
|
|
|
wavparse->format = GINT16_FROM_LE(format->wFormatTag);
|
|
|
|
|
2001-12-22 23:27:31 +00:00
|
|
|
/* set the caps on the src pad */
|
2003-01-14 14:01:49 +00:00
|
|
|
/* FIXME: handle all of the other formats as well */
|
|
|
|
switch (wavparse->format)
|
|
|
|
{
|
2003-04-11 21:52:18 +00:00
|
|
|
case GST_RIFF_WAVE_FORMAT_ALAW:
|
|
|
|
case GST_RIFF_WAVE_FORMAT_MULAW:
|
|
|
|
if (!(wavparse->width == 8)) {
|
|
|
|
gst_element_error (GST_ELEMENT (wavparse), "wavparse: invalid wave file");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
caps = GST_CAPS_NEW (
|
|
|
|
"parsewav_src",
|
|
|
|
"audio/raw",
|
|
|
|
"format", GST_PROPS_STRING ("int"),
|
|
|
|
"law", GST_PROPS_INT (wavparse->format == GST_RIFF_WAVE_FORMAT_ALAW ? 2 : 1),
|
|
|
|
"endianness", GST_PROPS_INT (G_LITTLE_ENDIAN),
|
|
|
|
"width", GST_PROPS_INT (8),
|
|
|
|
"depth", GST_PROPS_INT (8),
|
|
|
|
"rate", GST_PROPS_INT (wavparse->rate),
|
|
|
|
"channels", GST_PROPS_INT (wavparse->channels)
|
|
|
|
);
|
|
|
|
break;
|
2003-01-14 14:01:49 +00:00
|
|
|
case GST_RIFF_WAVE_FORMAT_PCM:
|
|
|
|
caps = GST_CAPS_NEW (
|
2002-02-02 13:58:48 +00:00
|
|
|
"parsewav_src",
|
2002-01-13 22:27:25 +00:00
|
|
|
"audio/raw",
|
|
|
|
"format", GST_PROPS_STRING ("int"),
|
2003-04-11 21:52:18 +00:00
|
|
|
"law", GST_PROPS_INT (0),
|
|
|
|
"endianness", GST_PROPS_INT (G_LITTLE_ENDIAN),
|
2003-01-14 14:01:49 +00:00
|
|
|
"signed", GST_PROPS_BOOLEAN ((wavparse->width > 8) ? TRUE : FALSE),
|
2002-12-08 12:23:48 +00:00
|
|
|
"width", GST_PROPS_INT (wavparse->width),
|
|
|
|
"depth", GST_PROPS_INT (wavparse->width),
|
|
|
|
"rate", GST_PROPS_INT (wavparse->rate),
|
|
|
|
"channels", GST_PROPS_INT (wavparse->channels)
|
2003-01-14 14:01:49 +00:00
|
|
|
);
|
|
|
|
break;
|
|
|
|
case GST_RIFF_WAVE_FORMAT_MPEGL12:
|
|
|
|
case GST_RIFF_WAVE_FORMAT_MPEGL3:
|
|
|
|
caps = GST_CAPS_NEW (
|
|
|
|
"parsewav_src",
|
|
|
|
"audio/x-mp3",
|
|
|
|
NULL
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
default:
|
2003-04-11 21:52:18 +00:00
|
|
|
gst_element_error (GST_ELEMENT (wavparse), "wavparse: format %d not handled", wavparse->format);
|
|
|
|
return;
|
2003-01-14 14:01:49 +00:00
|
|
|
}
|
2002-02-02 13:58:48 +00:00
|
|
|
|
2002-09-10 09:31:40 +00:00
|
|
|
if (gst_pad_try_set_caps (wavparse->srcpad, caps) <= 0) {
|
2002-05-30 04:43:38 +00:00
|
|
|
gst_element_error (GST_ELEMENT (wavparse), "Could not set caps");
|
|
|
|
return;
|
|
|
|
}
|
2001-12-22 23:27:31 +00:00
|
|
|
|
2002-03-24 22:07:03 +00:00
|
|
|
GST_DEBUG (0, "frequency %d, channels %d",
|
2003-01-14 14:01:49 +00:00
|
|
|
wavparse->rate, wavparse->channels);
|
2001-12-22 23:27:31 +00:00
|
|
|
|
|
|
|
/* 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-03-24 22:07:03 +00:00
|
|
|
GST_DEBUG (0, "GstWavParse: looking for data chunk");
|
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;
|
|
|
|
|
2002-03-24 22:07:03 +00:00
|
|
|
GST_DEBUG (0, "data begins at %ld", datachunk->offset);
|
2001-12-22 23:27:31 +00:00
|
|
|
|
2003-03-01 18:34:38 +00:00
|
|
|
wavparse->datastart = datachunk->offset;
|
|
|
|
|
2001-12-22 23:27:31 +00:00
|
|
|
/* 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;
|
2002-03-24 22:07:03 +00:00
|
|
|
GST_DEBUG (0, "sending last %ld bytes along as audio", subsize);
|
2003-01-14 14:01:49 +00:00
|
|
|
|
2003-03-01 18:34:38 +00:00
|
|
|
newbuf = gst_buffer_create_sub (buf, datachunk->offset, subsize);
|
2001-12-22 23:27:31 +00:00
|
|
|
gst_buffer_unref (buf);
|
|
|
|
|
2003-03-01 18:34:38 +00:00
|
|
|
GST_BUFFER_TIMESTAMP (newbuf) = 0;
|
|
|
|
|
2002-11-03 03:58:30 +00:00
|
|
|
if (GST_PAD_IS_USABLE (wavparse->srcpad))
|
|
|
|
gst_pad_push (wavparse->srcpad, newbuf);
|
2003-01-14 14:01:49 +00:00
|
|
|
else
|
|
|
|
gst_buffer_unref (newbuf);
|
2001-12-22 23:27:31 +00:00
|
|
|
|
2003-03-01 18:34:38 +00:00
|
|
|
wavparse->offset = subsize;
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-03 13:59:35 +00:00
|
|
|
/* convert and query stuff */
|
|
|
|
static const GstFormat *
|
|
|
|
gst_wavparse_get_formats (GstPad *pad)
|
|
|
|
{
|
|
|
|
static GstFormat formats[] = {
|
|
|
|
GST_FORMAT_TIME,
|
|
|
|
GST_FORMAT_BYTES,
|
|
|
|
GST_FORMAT_UNITS, /* a "frame", ie a set of samples per Hz */
|
|
|
|
0,
|
|
|
|
0
|
|
|
|
};
|
|
|
|
return formats;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_wavparse_pad_convert (GstPad *pad,
|
|
|
|
GstFormat src_format, gint64 src_value,
|
|
|
|
GstFormat *dest_format, gint64 *dest_value)
|
|
|
|
{
|
|
|
|
gint bytes_per_sample;
|
|
|
|
glong byterate;
|
|
|
|
GstWavParse *wavparse;
|
|
|
|
|
|
|
|
wavparse = GST_WAVPARSE (gst_pad_get_parent (pad));
|
2003-03-01 18:34:38 +00:00
|
|
|
/* FIXME default should be samples in this case IMO */
|
2002-11-03 13:59:35 +00:00
|
|
|
if (*dest_format == GST_FORMAT_DEFAULT)
|
|
|
|
*dest_format = GST_FORMAT_TIME;
|
|
|
|
|
|
|
|
bytes_per_sample = wavparse->channels * wavparse->width / 8;
|
|
|
|
if (bytes_per_sample == 0) {
|
2003-04-29 21:54:47 +00:00
|
|
|
GST_DEBUG (0, "bytes_per_sample is 0, probably an mp3 - channels %d, width %d\n",
|
|
|
|
wavparse->channels, wavparse->width);
|
2003-03-01 18:34:38 +00:00
|
|
|
return FALSE;
|
2002-11-03 13:59:35 +00:00
|
|
|
}
|
|
|
|
byterate = (glong) (bytes_per_sample * wavparse->rate);
|
|
|
|
if (byterate == 0) {
|
2003-03-01 18:34:38 +00:00
|
|
|
g_warning ("byterate is 0, internal error\n");
|
|
|
|
return FALSE;
|
2002-11-03 13:59:35 +00:00
|
|
|
}
|
2003-03-01 18:34:38 +00:00
|
|
|
GST_DEBUG (0, "bytes per sample: %d\n", bytes_per_sample);
|
2002-11-03 13:59:35 +00:00
|
|
|
|
|
|
|
switch (src_format) {
|
|
|
|
case GST_FORMAT_BYTES:
|
|
|
|
if (*dest_format == GST_FORMAT_UNITS)
|
|
|
|
*dest_value = src_value / bytes_per_sample;
|
|
|
|
else if (*dest_format == GST_FORMAT_TIME)
|
|
|
|
*dest_value = src_value * GST_SECOND / byterate;
|
|
|
|
else
|
|
|
|
return FALSE;
|
|
|
|
break;
|
|
|
|
case GST_FORMAT_UNITS:
|
|
|
|
if (*dest_format == GST_FORMAT_BYTES)
|
|
|
|
*dest_value = src_value * bytes_per_sample;
|
|
|
|
else if (*dest_format == GST_FORMAT_TIME)
|
|
|
|
*dest_value = src_value * GST_SECOND / wavparse->rate;
|
|
|
|
else
|
|
|
|
return FALSE;
|
|
|
|
break;
|
|
|
|
case GST_FORMAT_TIME:
|
|
|
|
if (*dest_format == GST_FORMAT_BYTES)
|
|
|
|
*dest_value = src_value * byterate / GST_SECOND;
|
|
|
|
else if (*dest_format == GST_FORMAT_UNITS)
|
2003-03-01 18:34:38 +00:00
|
|
|
*dest_value = src_value * wavparse->rate / GST_SECOND;
|
2002-11-03 13:59:35 +00:00
|
|
|
else
|
|
|
|
return FALSE;
|
2003-03-01 18:34:38 +00:00
|
|
|
|
|
|
|
*dest_value = *dest_value & ~(bytes_per_sample - 1);
|
2002-11-03 13:59:35 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_warning ("unhandled format for wavparse\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2002-12-30 17:53:18 +00:00
|
|
|
static const GstQueryType *
|
2002-11-03 13:59:35 +00:00
|
|
|
gst_wavparse_get_query_types (GstPad *pad)
|
|
|
|
{
|
2002-12-30 17:53:18 +00:00
|
|
|
static const GstQueryType types[] = {
|
|
|
|
GST_QUERY_TOTAL,
|
2003-01-14 21:10:53 +00:00
|
|
|
GST_QUERY_POSITION,
|
|
|
|
0
|
2002-11-03 13:59:35 +00:00
|
|
|
};
|
|
|
|
return types;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* handle queries for location and length in requested format */
|
|
|
|
static gboolean
|
2002-12-30 17:53:18 +00:00
|
|
|
gst_wavparse_pad_query (GstPad *pad, GstQueryType type,
|
2002-11-03 13:59:35 +00:00
|
|
|
GstFormat *format, gint64 *value)
|
|
|
|
{
|
|
|
|
GstFormat peer_format = GST_FORMAT_BYTES;
|
|
|
|
gint64 peer_value;
|
|
|
|
GstWavParse *wavparse;
|
|
|
|
|
|
|
|
/* probe sink's peer pad, convert value, and that's it :) */
|
|
|
|
/* FIXME: ideally we'd loop over possible formats of peer instead
|
|
|
|
* of only using BYTE */
|
|
|
|
wavparse = GST_WAVPARSE (gst_pad_get_parent (pad));
|
|
|
|
if (!gst_pad_query (GST_PAD_PEER (wavparse->sinkpad), type,
|
|
|
|
&peer_format, &peer_value)) {
|
|
|
|
g_warning ("Could not query sink pad's peer\n");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (!gst_pad_convert (wavparse->sinkpad, peer_format, peer_value,
|
|
|
|
format, value)) {
|
|
|
|
g_warning ("Could not query sink pad's peer\n");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2003-03-01 18:34:38 +00:00
|
|
|
GST_DEBUG (0, "pad_query done, value %" G_GINT64_FORMAT "\n", *value);
|
2002-11-03 13:59:35 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2003-03-01 18:34:38 +00:00
|
|
|
static const GstEventMask*
|
|
|
|
gst_wavparse_get_event_masks (GstPad *pad)
|
|
|
|
{
|
|
|
|
static const GstEventMask gst_wavparse_src_event_masks[] = {
|
|
|
|
{ GST_EVENT_SEEK, GST_SEEK_METHOD_SET |
|
|
|
|
GST_SEEK_FLAG_FLUSH },
|
|
|
|
{ 0, }
|
|
|
|
};
|
|
|
|
return gst_wavparse_src_event_masks;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_wavparse_srcpad_event (GstPad *pad, GstEvent *event)
|
|
|
|
{
|
|
|
|
GstWavParse *wavparse = GST_WAVPARSE (GST_PAD_PARENT (pad));
|
|
|
|
gboolean res = FALSE;
|
|
|
|
|
|
|
|
GST_DEBUG(0, "event %d", GST_EVENT_TYPE (event));
|
|
|
|
|
|
|
|
switch (GST_EVENT_TYPE (event)) {
|
|
|
|
case GST_EVENT_SEEK:
|
|
|
|
{
|
|
|
|
gint64 byteoffset;
|
|
|
|
GstFormat format;
|
|
|
|
|
|
|
|
/* we can only seek when in the DATA state */
|
|
|
|
if (wavparse->state != GST_WAVPARSE_DATA) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
format = GST_FORMAT_BYTES;
|
|
|
|
|
|
|
|
/* bring format to bytes for the peer element,
|
|
|
|
* FIXME be smarter here */
|
|
|
|
res = gst_pad_convert (pad,
|
|
|
|
GST_EVENT_SEEK_FORMAT (event),
|
|
|
|
GST_EVENT_SEEK_OFFSET (event),
|
|
|
|
&format,
|
|
|
|
&byteoffset);
|
|
|
|
|
|
|
|
if (res) {
|
|
|
|
GstEvent *seek;
|
|
|
|
|
|
|
|
/* seek to byteoffset + header length */
|
|
|
|
seek = gst_event_new_seek (
|
|
|
|
GST_FORMAT_BYTES |
|
|
|
|
(GST_EVENT_SEEK_TYPE (event) & ~GST_SEEK_FORMAT_MASK),
|
2003-04-29 21:54:47 +00:00
|
|
|
byteoffset + (GST_EVENT_SEEK_METHOD (event) == GST_SEEK_METHOD_END ? 0 : wavparse->datastart));
|
2003-03-01 18:34:38 +00:00
|
|
|
|
|
|
|
res = gst_pad_send_event (GST_PAD_PEER (wavparse->sinkpad), seek);
|
|
|
|
|
|
|
|
if (res) {
|
|
|
|
/* ok, seek worked, update our state */
|
|
|
|
wavparse->offset = byteoffset;
|
|
|
|
wavparse->need_discont = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_event_unref (event);
|
|
|
|
return res;
|
|
|
|
}
|
2001-12-22 23:27:31 +00:00
|
|
|
|
|
|
|
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 */
|
2002-04-11 20:42:25 +00:00
|
|
|
factory = gst_element_factory_new ("wavparse", GST_TYPE_WAVPARSE,
|
2002-01-30 10:20:58 +00:00
|
|
|
&gst_wavparse_details);
|
2001-12-22 23:27:31 +00:00
|
|
|
g_return_val_if_fail(factory != NULL, FALSE);
|
2002-05-31 08:24:31 +00:00
|
|
|
gst_element_factory_set_rank (factory, GST_ELEMENT_RANK_SECONDARY);
|
2001-12-22 23:27:31 +00:00
|
|
|
|
|
|
|
/* register src pads */
|
2002-04-11 20:42:25 +00:00
|
|
|
gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (sink_template_factory));
|
|
|
|
gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (src_template_factory));
|
2001-12-22 23:27:31 +00:00
|
|
|
|
|
|
|
gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
|
|
|
|
|
2002-04-11 20:42:25 +00:00
|
|
|
type = gst_type_factory_new (&wavdefinition);
|
2001-12-22 23:27:31 +00:00
|
|
|
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
|
|
|
|
};
|