2002-03-20 21:45:04 +00:00
|
|
|
/* GStreamer
|
2001-12-22 23:26:33 +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.
|
|
|
|
*/
|
|
|
|
|
2002-03-19 04:10:06 +00:00
|
|
|
/*#define GST_DEBUG_ENABLED */
|
2003-06-29 19:46:13 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
2001-12-22 23:26:33 +00:00
|
|
|
#include <gstmpegaudioparse.h>
|
|
|
|
|
|
|
|
|
|
|
|
/* elementfactory information */
|
|
|
|
static GstElementDetails mp3parse_details = {
|
2002-04-20 21:42:51 +00:00
|
|
|
"MPEG1 Audio Parser",
|
|
|
|
"Codec/Parser",
|
2002-09-18 19:02:52 +00:00
|
|
|
"LGPL",
|
2002-04-20 21:42:51 +00:00
|
|
|
"Parses and frames mpeg1 audio streams (levels 1-3), provides seek",
|
2001-12-22 23:26:33 +00:00
|
|
|
VERSION,
|
|
|
|
"Erik Walthinsen <omega@cse.ogi.edu>",
|
|
|
|
"(C) 1999",
|
|
|
|
};
|
|
|
|
|
2003-10-01 13:14:50 +00:00
|
|
|
static GstCaps * mp3_type_find (GstByteStream *bs, gpointer data);
|
|
|
|
|
|
|
|
static GstTypeDefinition mp3type_definition = {
|
|
|
|
"mp3_audio/mpeg",
|
|
|
|
"audio/mpeg",
|
|
|
|
".mp3 .mp2 .mp1 .mpga",
|
|
|
|
mp3_type_find,
|
|
|
|
};
|
|
|
|
|
2001-12-22 23:26:33 +00:00
|
|
|
static GstPadTemplate*
|
|
|
|
mp3_src_factory (void)
|
|
|
|
{
|
|
|
|
return
|
2002-04-11 20:42:26 +00:00
|
|
|
gst_pad_template_new (
|
2001-12-22 23:26:33 +00:00
|
|
|
"src",
|
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_ALWAYS,
|
|
|
|
gst_caps_new (
|
|
|
|
"mp3parse_src",
|
2003-07-29 19:57:11 +00:00
|
|
|
"audio/mpeg",
|
2001-12-22 23:26:33 +00:00
|
|
|
gst_props_new (
|
2003-07-29 19:57:11 +00:00
|
|
|
"mpegversion", GST_PROPS_INT (1),
|
2001-12-22 23:26:33 +00:00
|
|
|
"layer", GST_PROPS_INT_RANGE (1, 3),
|
2003-07-06 20:49:52 +00:00
|
|
|
"rate", GST_PROPS_INT_RANGE (8000, 48000),
|
|
|
|
"channels", GST_PROPS_INT_RANGE (1, 2),
|
|
|
|
NULL)),
|
2001-12-22 23:26:33 +00:00
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstPadTemplate*
|
|
|
|
mp3_sink_factory (void)
|
|
|
|
{
|
|
|
|
return
|
2002-04-11 20:42:26 +00:00
|
|
|
gst_pad_template_new (
|
2001-12-22 23:26:33 +00:00
|
|
|
"sink",
|
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_ALWAYS,
|
|
|
|
gst_caps_new (
|
|
|
|
"mp3parse_sink",
|
2003-07-29 19:57:11 +00:00
|
|
|
"audio/mpeg",
|
2001-12-22 23:26:33 +00:00
|
|
|
NULL),
|
|
|
|
NULL);
|
|
|
|
};
|
|
|
|
|
|
|
|
/* GstMPEGAudioParse signals and args */
|
|
|
|
enum {
|
|
|
|
/* FILL ME */
|
|
|
|
LAST_SIGNAL
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
ARG_0,
|
|
|
|
ARG_SKIP,
|
|
|
|
ARG_BIT_RATE,
|
|
|
|
/* FILL ME */
|
|
|
|
};
|
|
|
|
|
|
|
|
static GstPadTemplate *sink_temp, *src_temp;
|
|
|
|
|
|
|
|
static void gst_mp3parse_class_init (GstMPEGAudioParseClass *klass);
|
|
|
|
static void gst_mp3parse_init (GstMPEGAudioParse *mp3parse);
|
|
|
|
|
|
|
|
static void gst_mp3parse_chain (GstPad *pad,GstBuffer *buf);
|
|
|
|
static long bpf_from_header (GstMPEGAudioParse *parse, unsigned long header);
|
|
|
|
static int head_check (unsigned long head);
|
|
|
|
|
|
|
|
static void gst_mp3parse_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
|
|
|
|
static void gst_mp3parse_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
|
2003-07-06 20:49:52 +00:00
|
|
|
static GstElementStateReturn
|
|
|
|
gst_mp3parse_change_state (GstElement *element);
|
2001-12-22 23:26:33 +00:00
|
|
|
|
|
|
|
static GstElementClass *parent_class = NULL;
|
2002-03-19 04:10:06 +00:00
|
|
|
/*static guint gst_mp3parse_signals[LAST_SIGNAL] = { 0 }; */
|
2001-12-22 23:26:33 +00:00
|
|
|
|
|
|
|
GType
|
2001-12-23 06:56:55 +00:00
|
|
|
gst_mp3parse_get_type(void) {
|
2001-12-22 23:26:33 +00:00
|
|
|
static GType mp3parse_type = 0;
|
|
|
|
|
|
|
|
if (!mp3parse_type) {
|
|
|
|
static const GTypeInfo mp3parse_info = {
|
|
|
|
sizeof(GstMPEGAudioParseClass), NULL,
|
|
|
|
NULL,
|
|
|
|
(GClassInitFunc)gst_mp3parse_class_init,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
sizeof(GstMPEGAudioParse),
|
|
|
|
0,
|
|
|
|
(GInstanceInitFunc)gst_mp3parse_init,
|
|
|
|
};
|
2003-10-01 13:14:50 +00:00
|
|
|
mp3parse_type = g_type_register_static (GST_TYPE_ELEMENT,
|
|
|
|
"GstMPEGAudioParse",
|
|
|
|
&mp3parse_info, 0);
|
2001-12-22 23:26:33 +00:00
|
|
|
}
|
|
|
|
return mp3parse_type;
|
|
|
|
}
|
|
|
|
|
2003-10-01 13:14:50 +00:00
|
|
|
static guint mp3types_bitrates[2][3][16] =
|
|
|
|
{ { {0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448, },
|
|
|
|
{0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, },
|
|
|
|
{0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, } },
|
|
|
|
{ {0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256, },
|
|
|
|
{0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, },
|
|
|
|
{0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, } },
|
|
|
|
};
|
|
|
|
|
|
|
|
static guint mp3types_freqs[3][3] =
|
|
|
|
{ {44100, 48000, 32000},
|
|
|
|
{22050, 24000, 16000},
|
|
|
|
{11025, 12000, 8000}};
|
|
|
|
|
|
|
|
static inline guint
|
|
|
|
mp3_type_frame_length_from_header (guint32 header, guint *put_layer,
|
|
|
|
guint *put_channels, guint *put_bitrate,
|
|
|
|
guint *put_samplerate)
|
|
|
|
{
|
|
|
|
guint length;
|
|
|
|
gulong mode, samplerate, bitrate, layer, version, channels;
|
|
|
|
|
|
|
|
/* we don't need extension, copyright, original or
|
|
|
|
* emphasis for the frame length */
|
|
|
|
header >>= 6;
|
|
|
|
|
|
|
|
/* mode */
|
|
|
|
mode = header & 0x3;
|
|
|
|
header >>= 3;
|
|
|
|
|
|
|
|
/* padding */
|
|
|
|
length = header & 0x1;
|
|
|
|
header >>= 1;
|
|
|
|
|
|
|
|
/* sampling frequency */
|
|
|
|
samplerate = header & 0x3;
|
|
|
|
if (samplerate == 3)
|
|
|
|
return 0;
|
|
|
|
header >>= 2;
|
|
|
|
|
|
|
|
/* bitrate index */
|
|
|
|
bitrate = header & 0xF;
|
|
|
|
if (bitrate == 15 || bitrate == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* ignore error correction, too */
|
|
|
|
header >>= 5;
|
|
|
|
|
|
|
|
/* layer */
|
|
|
|
layer = 4 - (header & 0x3);
|
|
|
|
if (layer == 4)
|
|
|
|
return 0;
|
|
|
|
header >>= 2;
|
|
|
|
|
|
|
|
/* version */
|
|
|
|
version = header & 0x3;
|
|
|
|
if (version == 1)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* lookup */
|
|
|
|
channels = (mode == 3) ? 1 : 2;
|
|
|
|
bitrate = mp3types_bitrates[version == 3 ? 0 : 1][layer - 1][bitrate];
|
|
|
|
samplerate = mp3types_freqs[version > 0 ? version - 1 : 0][samplerate];
|
|
|
|
|
|
|
|
/* calculating */
|
|
|
|
if (layer == 1) {
|
|
|
|
length = ((12000 * bitrate / samplerate) + length) * 4;
|
|
|
|
} else {
|
|
|
|
length += ((layer == 3 && version == 0) ? 144000 : 72000) * bitrate / samplerate;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_DEBUG ("Calculated mp3 frame length of %u bytes", length);
|
|
|
|
GST_DEBUG ("samplerate = %lu - bitrate = %lu - layer = %lu - version = %lu"
|
|
|
|
" - channels = %lu",
|
|
|
|
samplerate, bitrate, layer, version, channels);
|
|
|
|
|
|
|
|
if (put_layer)
|
|
|
|
*put_layer = layer;
|
|
|
|
if (put_channels)
|
|
|
|
*put_channels = channels;
|
|
|
|
if (put_bitrate)
|
|
|
|
*put_bitrate = bitrate;
|
|
|
|
if (put_samplerate)
|
|
|
|
*put_samplerate = samplerate;
|
|
|
|
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The chance that random data is identified as a valid mp3 header is 63 / 2^18
|
|
|
|
* (0.024%) per try. This makes the function for calculating false positives
|
|
|
|
* 1 - (1 - ((63 / 2 ^18) ^ GST_MP3_TYPEFIND_MIN_HEADERS)) ^ buffersize)
|
|
|
|
* This has the following probabilities of false positives:
|
|
|
|
* bufsize MIN_HEADERS
|
|
|
|
* (bytes) 1 2 3 4
|
|
|
|
* 4096 62.6% 0.02% 0% 0%
|
|
|
|
* 16384 98% 0.09% 0% 0%
|
|
|
|
* 1 MiB 100% 5.88% 0% 0%
|
|
|
|
* 1 GiB 100% 100% 1.44% 0%
|
|
|
|
* 1 TiB 100% 100% 100% 0.35%
|
|
|
|
* This means that the current choice (3 headers by most of the time 4096 byte
|
|
|
|
* buffers is pretty safe for now.
|
|
|
|
*
|
|
|
|
* The max. size of each frame is 1440 bytes, which means that for N frames
|
|
|
|
* to be detected, we need 1440 * GST_MP3_TYPEFIND_MIN_HEADERS + 3 of data.
|
|
|
|
* Assuming we step into the stream right after the frame header, this
|
|
|
|
* means we need 1440 * (GST_MP3_TYPEFIND_MIN_HEADERS + 1) - 1 + 3 bytes
|
|
|
|
* of data (5762) to always detect any mp3.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* increase this value when this function finds too many false positives */
|
|
|
|
#define GST_MP3_TYPEFIND_MIN_HEADERS 3
|
|
|
|
#define GST_MP3_TYPEFIND_MIN_DATA (1440 * (GST_MP3_TYPEFIND_MIN_HEADERS + 1) - 1 + 3)
|
|
|
|
|
|
|
|
static GstCaps *
|
|
|
|
mp3_caps_create (guint layer, guint channels,
|
|
|
|
guint bitrate, guint samplerate)
|
|
|
|
{
|
|
|
|
GstCaps *new;
|
|
|
|
|
|
|
|
g_assert (layer);
|
|
|
|
g_assert (samplerate);
|
|
|
|
g_assert (bitrate);
|
|
|
|
g_assert (channels);
|
|
|
|
|
|
|
|
new = GST_CAPS_NEW ("mp3_type_find",
|
|
|
|
"audio/mpeg",
|
|
|
|
"mpegversion", GST_PROPS_INT (1),
|
|
|
|
"layer", GST_PROPS_INT (layer),
|
|
|
|
/*"bitrate", GST_PROPS_INT (bitrate),*/
|
|
|
|
"rate", GST_PROPS_INT (samplerate),
|
|
|
|
"channels", GST_PROPS_INT (channels));
|
|
|
|
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstCaps *
|
|
|
|
mp3_type_find (GstByteStream *bs, gpointer private)
|
|
|
|
{
|
|
|
|
GstBuffer *buf = NULL;
|
|
|
|
GstCaps *new = NULL;
|
|
|
|
guint8 *data;
|
|
|
|
guint size;
|
|
|
|
guint32 head;
|
|
|
|
guint layer = 0, bitrate = 0, samplerate = 0, channels = 0;
|
|
|
|
|
|
|
|
/* note that even if we don't get the requested size,
|
|
|
|
* it might still be a (very small) mp3 */
|
|
|
|
gst_bytestream_peek (bs, &buf, GST_MP3_TYPEFIND_MIN_DATA);
|
|
|
|
if (!buf) {
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
data = GST_BUFFER_DATA (buf);
|
|
|
|
size = GST_BUFFER_SIZE (buf);
|
|
|
|
|
|
|
|
while (size >= 4) {
|
|
|
|
head = GUINT32_FROM_BE(*((guint32 *)data));
|
|
|
|
if ((head & 0xffe00000) == 0xffe00000) {
|
|
|
|
guint length;
|
|
|
|
guint prev_layer = 0, prev_bitrate = 0,
|
|
|
|
prev_channels = 0, prev_samplerate = 0;
|
|
|
|
guint found = 0; /* number of valid headers found */
|
|
|
|
guint pos = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (!(length = mp3_type_frame_length_from_header (head, &layer,
|
|
|
|
&channels, &bitrate,
|
|
|
|
&samplerate))) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ((prev_layer && prev_layer != layer) || !layer ||
|
|
|
|
(prev_bitrate && prev_bitrate != bitrate) || !bitrate ||
|
|
|
|
(prev_samplerate && prev_samplerate != samplerate) || !samplerate ||
|
|
|
|
(prev_channels && prev_channels != channels) || !channels) {
|
|
|
|
/* this means an invalid property, or a change, which likely
|
|
|
|
* indicates that this is not a mp3 but just a random bytestream */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
prev_layer = layer;
|
|
|
|
prev_bitrate = bitrate;
|
|
|
|
prev_channels = channels;
|
|
|
|
prev_samplerate = samplerate;
|
|
|
|
pos += length;
|
|
|
|
if (++found >= GST_MP3_TYPEFIND_MIN_HEADERS) {
|
|
|
|
/* we're pretty sure that this is mp3 now */
|
|
|
|
new = mp3_caps_create (layer, channels, bitrate, samplerate);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* and now, find a new head */
|
|
|
|
head = GUINT32_FROM_BE(*((guint32 *) &(data[pos])));
|
|
|
|
if ((head & 0xffe00000) != 0xffe00000)
|
|
|
|
break;
|
|
|
|
} while (TRUE);
|
|
|
|
}
|
|
|
|
data++;
|
|
|
|
size--;
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
if (buf != NULL) {
|
|
|
|
gst_buffer_unref (buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
2001-12-22 23:26:33 +00:00
|
|
|
static void
|
|
|
|
gst_mp3parse_class_init (GstMPEGAudioParseClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
|
|
|
GstElementClass *gstelement_class;
|
|
|
|
|
|
|
|
gobject_class = (GObjectClass*)klass;
|
|
|
|
gstelement_class = (GstElementClass*)klass;
|
|
|
|
|
|
|
|
g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_SKIP,
|
|
|
|
g_param_spec_int("skip","skip","skip",
|
2002-03-19 04:10:06 +00:00
|
|
|
G_MININT,G_MAXINT,0,G_PARAM_READWRITE)); /* CHECKME */
|
2001-12-22 23:26:33 +00:00
|
|
|
g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_BIT_RATE,
|
2003-07-06 20:49:52 +00:00
|
|
|
g_param_spec_int("bitrate","Bitrate","Bit Rate",
|
2002-03-19 04:10:06 +00:00
|
|
|
G_MININT,G_MAXINT,0,G_PARAM_READABLE)); /* CHECKME */
|
2001-12-22 23:26:33 +00:00
|
|
|
|
|
|
|
parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
|
|
|
|
|
|
|
|
gobject_class->set_property = gst_mp3parse_set_property;
|
|
|
|
gobject_class->get_property = gst_mp3parse_get_property;
|
2003-07-06 20:49:52 +00:00
|
|
|
|
|
|
|
gstelement_class->change_state = gst_mp3parse_change_state;
|
2001-12-22 23:26:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_mp3parse_init (GstMPEGAudioParse *mp3parse)
|
|
|
|
{
|
|
|
|
mp3parse->sinkpad = gst_pad_new_from_template(sink_temp, "sink");
|
|
|
|
gst_element_add_pad(GST_ELEMENT(mp3parse),mp3parse->sinkpad);
|
|
|
|
|
|
|
|
gst_pad_set_chain_function(mp3parse->sinkpad,gst_mp3parse_chain);
|
2002-04-06 03:40:15 +00:00
|
|
|
gst_element_set_loop_function (GST_ELEMENT(mp3parse),NULL);
|
2001-12-22 23:26:33 +00:00
|
|
|
|
|
|
|
mp3parse->srcpad = gst_pad_new_from_template(src_temp, "src");
|
|
|
|
gst_element_add_pad(GST_ELEMENT(mp3parse),mp3parse->srcpad);
|
2002-03-19 04:10:06 +00:00
|
|
|
/*gst_pad_set_type_id(mp3parse->srcpad, mp3frametype); */
|
2001-12-22 23:26:33 +00:00
|
|
|
|
|
|
|
mp3parse->partialbuf = NULL;
|
|
|
|
mp3parse->skip = 0;
|
|
|
|
mp3parse->in_flush = FALSE;
|
2003-07-06 20:49:52 +00:00
|
|
|
|
|
|
|
mp3parse->rate = mp3parse->channels = mp3parse->layer = -1;
|
2001-12-22 23:26:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_mp3parse_chain (GstPad *pad, GstBuffer *buf)
|
|
|
|
{
|
|
|
|
GstMPEGAudioParse *mp3parse;
|
|
|
|
guchar *data;
|
|
|
|
glong size,offset = 0;
|
2003-04-07 18:43:25 +00:00
|
|
|
guint32 header;
|
2001-12-22 23:26:33 +00:00
|
|
|
int bpf;
|
|
|
|
GstBuffer *outbuf;
|
|
|
|
guint64 last_ts;
|
|
|
|
|
|
|
|
g_return_if_fail(pad != NULL);
|
|
|
|
g_return_if_fail(GST_IS_PAD(pad));
|
|
|
|
g_return_if_fail(buf != NULL);
|
2002-03-19 04:10:06 +00:00
|
|
|
/* g_return_if_fail(GST_IS_BUFFER(buf)); */
|
2001-12-22 23:26:33 +00:00
|
|
|
|
|
|
|
mp3parse = GST_MP3PARSE (gst_pad_get_parent (pad));
|
|
|
|
|
2003-06-29 19:46:13 +00:00
|
|
|
GST_DEBUG ("mp3parse: received buffer of %d bytes",GST_BUFFER_SIZE(buf));
|
2001-12-22 23:26:33 +00:00
|
|
|
|
|
|
|
last_ts = GST_BUFFER_TIMESTAMP(buf);
|
|
|
|
|
2001-12-28 20:24:41 +00:00
|
|
|
/* FIXME, do flush */
|
|
|
|
/*
|
2001-12-22 23:26:33 +00:00
|
|
|
if (mp3parse->partialbuf) {
|
|
|
|
gst_buffer_unref(mp3parse->partialbuf);
|
|
|
|
mp3parse->partialbuf = NULL;
|
|
|
|
}
|
|
|
|
mp3parse->in_flush = TRUE;
|
2001-12-28 20:24:41 +00:00
|
|
|
*/
|
2001-12-22 23:26:33 +00:00
|
|
|
|
2002-03-19 04:10:06 +00:00
|
|
|
/* if we have something left from the previous frame */
|
2001-12-22 23:26:33 +00:00
|
|
|
if (mp3parse->partialbuf) {
|
|
|
|
|
2002-07-08 19:43:32 +00:00
|
|
|
mp3parse->partialbuf = gst_buffer_merge(mp3parse->partialbuf, buf);
|
2002-03-19 04:10:06 +00:00
|
|
|
/* and the one we received.. */
|
2001-12-22 23:26:33 +00:00
|
|
|
gst_buffer_unref(buf);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mp3parse->partialbuf = buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
size = GST_BUFFER_SIZE(mp3parse->partialbuf);
|
|
|
|
data = GST_BUFFER_DATA(mp3parse->partialbuf);
|
|
|
|
|
2002-03-19 04:10:06 +00:00
|
|
|
/* while we still have bytes left -4 for the header */
|
2001-12-22 23:26:33 +00:00
|
|
|
while (offset < size-4) {
|
|
|
|
int skipped = 0;
|
|
|
|
|
2003-06-29 19:46:13 +00:00
|
|
|
GST_DEBUG ("mp3parse: offset %ld, size %ld ",offset, size);
|
2001-12-22 23:26:33 +00:00
|
|
|
|
2002-03-19 04:10:06 +00:00
|
|
|
/* search for a possible start byte */
|
2001-12-22 23:26:33 +00:00
|
|
|
for (;((data[offset] != 0xff) && (offset < size));offset++) skipped++;
|
|
|
|
if (skipped && !mp3parse->in_flush) {
|
2003-06-29 19:46:13 +00:00
|
|
|
GST_DEBUG ("mp3parse: **** now at %ld skipped %d bytes",offset,skipped);
|
2001-12-22 23:26:33 +00:00
|
|
|
}
|
2002-03-19 04:10:06 +00:00
|
|
|
/* construct the header word */
|
2003-04-07 18:43:25 +00:00
|
|
|
header = GUINT32_FROM_BE(*((guint32 *)(data+offset)));
|
2002-03-19 04:10:06 +00:00
|
|
|
/* if it's a valid header, go ahead and send off the frame */
|
2001-12-22 23:26:33 +00:00
|
|
|
if (head_check(header)) {
|
2002-03-19 04:10:06 +00:00
|
|
|
/* calculate the bpf of the frame */
|
2001-12-22 23:26:33 +00:00
|
|
|
bpf = bpf_from_header(mp3parse, header);
|
|
|
|
|
|
|
|
/********************************************************************************
|
|
|
|
* robust seek support
|
|
|
|
* - This performs additional frame validation if the in_flush flag is set
|
|
|
|
* (indicating a discontinuous stream).
|
|
|
|
* - The current frame header is not accepted as valid unless the NEXT frame
|
|
|
|
* header has the same values for most fields. This significantly increases
|
|
|
|
* the probability that we aren't processing random data.
|
|
|
|
* - It is not clear if this is sufficient for robust seeking of Layer III
|
|
|
|
* streams which utilize the concept of a "bit reservoir" by borrow bitrate
|
|
|
|
* from previous frames. In this case, seeking may be more complicated because
|
|
|
|
* the frames are not independently coded.
|
|
|
|
********************************************************************************/
|
|
|
|
if ( mp3parse->in_flush ) {
|
2003-04-07 18:43:25 +00:00
|
|
|
guint32 header2;
|
2001-12-22 23:26:33 +00:00
|
|
|
|
2002-03-19 04:10:06 +00:00
|
|
|
if ((size-offset)<(bpf+4)) { if (mp3parse->in_flush) break; } /* wait until we have the the entire current frame as well as the next frame header */
|
2001-12-22 23:26:33 +00:00
|
|
|
|
2003-04-07 18:43:25 +00:00
|
|
|
header2 = GUINT32_FROM_BE(*((guint32 *)(data+offset+bpf)));
|
2003-06-29 19:46:13 +00:00
|
|
|
GST_DEBUG ("mp3parse: header=%08X, header2=%08X, bpf=%d", (unsigned int)header, (unsigned int)header2, bpf );
|
2001-12-22 23:26:33 +00:00
|
|
|
|
2003-10-01 13:14:50 +00:00
|
|
|
/* mask the bits which are allowed to differ between frames */
|
|
|
|
#define HDRMASK ~((0xF << 12) /* bitrate */ | \
|
|
|
|
(0x1 << 9) /* padding */ | \
|
|
|
|
(0x3 << 4)) /*mode extension*/
|
2001-12-22 23:26:33 +00:00
|
|
|
|
2002-03-19 04:10:06 +00:00
|
|
|
if ( (header2&HDRMASK) != (header&HDRMASK) ) { /* require 2 matching headers in a row */
|
2003-06-29 19:46:13 +00:00
|
|
|
GST_DEBUG ("mp3parse: next header doesn't match (header=%08X, header2=%08X, bpf=%d)", (unsigned int)header, (unsigned int)header2, bpf );
|
2002-03-19 04:10:06 +00:00
|
|
|
offset++; /* This frame is invalid. Start looking for a valid frame at the next position in the stream */
|
2001-12-22 23:26:33 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2002-03-19 04:10:06 +00:00
|
|
|
/* if we don't have the whole frame... */
|
2001-12-22 23:26:33 +00:00
|
|
|
if ((size - offset) < bpf) {
|
2003-06-29 19:46:13 +00:00
|
|
|
GST_DEBUG ("mp3parse: partial buffer needed %ld < %d ",(size-offset), bpf);
|
2001-12-22 23:26:33 +00:00
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
|
|
|
|
outbuf = gst_buffer_create_sub(mp3parse->partialbuf,offset,bpf);
|
|
|
|
|
|
|
|
offset += bpf;
|
|
|
|
if (mp3parse->skip == 0) {
|
2003-06-29 19:46:13 +00:00
|
|
|
GST_DEBUG ("mp3parse: pushing buffer of %d bytes",GST_BUFFER_SIZE(outbuf));
|
2001-12-22 23:26:33 +00:00
|
|
|
if (mp3parse->in_flush) {
|
2001-12-28 20:24:41 +00:00
|
|
|
/* FIXME do some sort of flush event */
|
2001-12-22 23:26:33 +00:00
|
|
|
mp3parse->in_flush = FALSE;
|
|
|
|
}
|
|
|
|
GST_BUFFER_TIMESTAMP(outbuf) = last_ts;
|
2003-07-29 19:57:11 +00:00
|
|
|
GST_BUFFER_DURATION(outbuf) = 8 * (GST_SECOND/1000) * GST_BUFFER_SIZE(outbuf) / mp3parse->bit_rate;
|
2003-07-06 20:49:52 +00:00
|
|
|
|
|
|
|
if (GST_PAD_CAPS (mp3parse->srcpad) != NULL) {
|
|
|
|
gst_pad_push(mp3parse->srcpad,outbuf);
|
|
|
|
} else {
|
|
|
|
GST_DEBUG ("No capsnego yet, delaying buffer push");
|
|
|
|
gst_buffer_unref (outbuf);
|
|
|
|
}
|
2001-12-22 23:26:33 +00:00
|
|
|
}
|
|
|
|
else {
|
2003-06-29 19:46:13 +00:00
|
|
|
GST_DEBUG ("mp3parse: skipping buffer of %d bytes",GST_BUFFER_SIZE(outbuf));
|
2001-12-22 23:26:33 +00:00
|
|
|
gst_buffer_unref(outbuf);
|
|
|
|
mp3parse->skip--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
offset++;
|
2003-06-29 19:46:13 +00:00
|
|
|
if (!mp3parse->in_flush) GST_DEBUG ("mp3parse: *** wrong header, skipping byte (FIXME?)");
|
2001-12-22 23:26:33 +00:00
|
|
|
}
|
|
|
|
}
|
2002-03-19 04:10:06 +00:00
|
|
|
/* if we have processed this block and there are still */
|
|
|
|
/* bytes left not in a partial block, copy them over. */
|
2001-12-22 23:26:33 +00:00
|
|
|
if (size-offset > 0) {
|
|
|
|
glong remainder = (size - offset);
|
2003-06-29 19:46:13 +00:00
|
|
|
GST_DEBUG ("mp3parse: partial buffer needed %ld for trailing bytes",remainder);
|
2001-12-22 23:26:33 +00:00
|
|
|
|
|
|
|
outbuf = gst_buffer_create_sub(mp3parse->partialbuf,offset,remainder);
|
|
|
|
gst_buffer_unref(mp3parse->partialbuf);
|
|
|
|
mp3parse->partialbuf = outbuf;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
gst_buffer_unref(mp3parse->partialbuf);
|
|
|
|
mp3parse->partialbuf = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static long
|
|
|
|
bpf_from_header (GstMPEGAudioParse *parse, unsigned long header)
|
|
|
|
{
|
2003-10-01 13:14:50 +00:00
|
|
|
guint bitrate, layer, rate, channels, length;
|
2001-12-22 23:26:33 +00:00
|
|
|
|
2003-10-01 13:14:50 +00:00
|
|
|
if (!(length = mp3_type_frame_length_from_header (header, &layer,
|
|
|
|
&channels,
|
|
|
|
&bitrate, &rate))) {
|
|
|
|
return 0;
|
2001-12-22 23:26:33 +00:00
|
|
|
}
|
|
|
|
|
2003-07-06 20:49:52 +00:00
|
|
|
if (channels != parse->channels ||
|
|
|
|
rate != parse->rate ||
|
2003-10-01 13:14:50 +00:00
|
|
|
layer != parse->layer ||
|
|
|
|
bitrate != parse->bit_rate) {
|
|
|
|
GstCaps *caps = mp3_caps_create (layer, channels, bitrate, rate);
|
|
|
|
|
2003-07-06 20:49:52 +00:00
|
|
|
if (gst_pad_try_set_caps(parse->srcpad, caps) <= 0) {
|
2003-09-16 10:00:00 +00:00
|
|
|
gst_element_error (GST_ELEMENT (parse),
|
|
|
|
"mp3parse: failed to negotiate format with next element");
|
2003-07-06 20:49:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
parse->channels = channels;
|
|
|
|
parse->layer = layer;
|
|
|
|
parse->rate = rate;
|
2003-10-01 13:14:50 +00:00
|
|
|
parse->bit_rate = bitrate;
|
2003-07-06 20:49:52 +00:00
|
|
|
}
|
|
|
|
|
2003-10-01 13:14:50 +00:00
|
|
|
return length;
|
2001-12-22 23:26:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
head_check (unsigned long head)
|
|
|
|
{
|
2003-06-29 19:46:13 +00:00
|
|
|
GST_DEBUG ("checking mp3 header 0x%08lx",head);
|
2001-12-22 23:26:33 +00:00
|
|
|
/* if it's not a valid sync */
|
|
|
|
if ((head & 0xffe00000) != 0xffe00000) {
|
2003-06-29 19:46:13 +00:00
|
|
|
GST_DEBUG ("invalid sync");return FALSE; }
|
2001-12-22 23:26:33 +00:00
|
|
|
/* if it's an invalid MPEG version */
|
|
|
|
if (((head >> 19) & 3) == 0x1) {
|
2003-06-29 19:46:13 +00:00
|
|
|
GST_DEBUG ("invalid MPEG version");return FALSE; }
|
2001-12-22 23:26:33 +00:00
|
|
|
/* if it's an invalid layer */
|
|
|
|
if (!((head >> 17) & 3)) {
|
2003-06-29 19:46:13 +00:00
|
|
|
GST_DEBUG ("invalid layer");return FALSE; }
|
2001-12-22 23:26:33 +00:00
|
|
|
/* if it's an invalid bitrate */
|
|
|
|
if (((head >> 12) & 0xf) == 0x0) {
|
2003-06-29 19:46:13 +00:00
|
|
|
GST_DEBUG ("invalid bitrate");return FALSE; }
|
2001-12-22 23:26:33 +00:00
|
|
|
if (((head >> 12) & 0xf) == 0xf) {
|
2003-06-29 19:46:13 +00:00
|
|
|
GST_DEBUG ("invalid bitrate");return FALSE; }
|
2001-12-22 23:26:33 +00:00
|
|
|
/* if it's an invalid samplerate */
|
|
|
|
if (((head >> 10) & 0x3) == 0x3) {
|
2003-06-29 19:46:13 +00:00
|
|
|
GST_DEBUG ("invalid samplerate");return FALSE; }
|
2001-12-22 23:26:33 +00:00
|
|
|
if ((head & 0xffff0000) == 0xfffe0000) {
|
2003-06-29 19:46:13 +00:00
|
|
|
GST_DEBUG ("invalid sync");return FALSE; }
|
2001-12-22 23:26:33 +00:00
|
|
|
if (head & 0x00000002) {
|
2003-06-29 19:46:13 +00:00
|
|
|
GST_DEBUG ("invalid emphasis");return FALSE; }
|
2001-12-22 23:26:33 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_mp3parse_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GstMPEGAudioParse *src;
|
|
|
|
|
|
|
|
/* it's not null if we got it, but it might not be ours */
|
|
|
|
g_return_if_fail(GST_IS_MP3PARSE(object));
|
|
|
|
src = GST_MP3PARSE(object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case ARG_SKIP:
|
|
|
|
src->skip = g_value_get_int (value);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_mp3parse_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GstMPEGAudioParse *src;
|
|
|
|
|
|
|
|
/* it's not null if we got it, but it might not be ours */
|
|
|
|
g_return_if_fail(GST_IS_MP3PARSE(object));
|
|
|
|
src = GST_MP3PARSE(object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case ARG_SKIP:
|
|
|
|
g_value_set_int (value, src->skip);
|
|
|
|
break;
|
|
|
|
case ARG_BIT_RATE:
|
|
|
|
g_value_set_int (value, src->bit_rate * 1000);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-07-06 20:49:52 +00:00
|
|
|
static GstElementStateReturn
|
|
|
|
gst_mp3parse_change_state (GstElement *element)
|
|
|
|
{
|
|
|
|
GstMPEGAudioParse *src;
|
|
|
|
|
|
|
|
g_return_val_if_fail(GST_IS_MP3PARSE(element), GST_STATE_FAILURE);
|
|
|
|
src = GST_MP3PARSE(element);
|
|
|
|
|
|
|
|
switch (GST_STATE_TRANSITION (element)) {
|
|
|
|
case GST_STATE_PAUSED_TO_READY:
|
|
|
|
src->channels = -1; src->rate = -1; src->layer = -1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GST_ELEMENT_CLASS(parent_class)->change_state)
|
|
|
|
return GST_ELEMENT_CLASS(parent_class)->change_state(element);
|
|
|
|
|
|
|
|
return GST_STATE_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2001-12-22 23:26:33 +00:00
|
|
|
static gboolean
|
|
|
|
plugin_init (GModule *module, GstPlugin *plugin)
|
|
|
|
{
|
|
|
|
GstElementFactory *factory;
|
2003-10-01 13:14:50 +00:00
|
|
|
GstTypeFactory *type;
|
2001-12-22 23:26:33 +00:00
|
|
|
|
|
|
|
/* create an elementfactory for the mp3parse element */
|
2002-04-11 20:42:26 +00:00
|
|
|
factory = gst_element_factory_new ("mp3parse",
|
2001-12-22 23:26:33 +00:00
|
|
|
GST_TYPE_MP3PARSE,
|
|
|
|
&mp3parse_details);
|
|
|
|
g_return_val_if_fail (factory != NULL, FALSE);
|
|
|
|
|
|
|
|
sink_temp = mp3_sink_factory ();
|
2002-04-11 20:42:26 +00:00
|
|
|
gst_element_factory_add_pad_template (factory, sink_temp);
|
2001-12-22 23:26:33 +00:00
|
|
|
|
|
|
|
src_temp = mp3_src_factory ();
|
2002-04-11 20:42:26 +00:00
|
|
|
gst_element_factory_add_pad_template (factory, src_temp);
|
2001-12-22 23:26:33 +00:00
|
|
|
|
|
|
|
gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
|
|
|
|
|
2003-10-01 13:14:50 +00:00
|
|
|
/* type finding */
|
|
|
|
type = gst_type_factory_new (&mp3type_definition);
|
|
|
|
gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (type));
|
|
|
|
|
2001-12-22 23:26:33 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
GstPluginDesc plugin_desc = {
|
|
|
|
GST_VERSION_MAJOR,
|
|
|
|
GST_VERSION_MINOR,
|
|
|
|
"mp3parse",
|
|
|
|
plugin_init
|
|
|
|
};
|