2002-03-20 21:45:03 +00:00
|
|
|
/* GStreamer
|
2001-12-20 22:10:54 +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.
|
|
|
|
*/
|
|
|
|
|
2008-06-13 06:57:21 +00:00
|
|
|
/**
|
|
|
|
* SECTION:element-mad
|
|
|
|
* @see_also: lame
|
|
|
|
*
|
|
|
|
* MP3 audio decoder.
|
|
|
|
*
|
|
|
|
* <refsect2>
|
|
|
|
* <title>Example pipelines</title>
|
|
|
|
* |[
|
2011-08-16 19:12:06 +00:00
|
|
|
* gst-launch filesrc location=music.mp3 ! mad ! audioconvert ! audioresample ! autoaudiosink
|
2008-06-13 06:57:21 +00:00
|
|
|
* ]| Decode the mp3 file and play
|
|
|
|
* </refsect2>
|
|
|
|
*/
|
|
|
|
|
2003-06-29 19:46:09 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
2003-07-25 02:04:01 +00:00
|
|
|
|
2009-10-02 15:27:11 +00:00
|
|
|
#include <stdlib.h>
|
2001-12-20 22:10:54 +00:00
|
|
|
#include <string.h>
|
2003-11-24 04:08:47 +00:00
|
|
|
#include "gstmad.h"
|
2007-07-13 15:46:13 +00:00
|
|
|
#include <gst/audio/audio.h>
|
2002-02-03 20:10:04 +00:00
|
|
|
|
2004-03-14 22:34:30 +00:00
|
|
|
enum
|
|
|
|
{
|
2001-12-20 22:10:54 +00:00
|
|
|
ARG_0,
|
2002-11-02 17:40:16 +00:00
|
|
|
ARG_HALF,
|
2006-06-17 19:35:41 +00:00
|
|
|
ARG_IGNORE_CRC
|
2001-12-20 22:10:54 +00:00
|
|
|
};
|
|
|
|
|
2004-04-05 11:38:04 +00:00
|
|
|
GST_DEBUG_CATEGORY_STATIC (mad_debug);
|
|
|
|
#define GST_CAT_DEFAULT mad_debug
|
|
|
|
|
2003-12-22 01:47:08 +00:00
|
|
|
static GstStaticPadTemplate mad_src_template_factory =
|
2004-03-14 22:34:30 +00:00
|
|
|
GST_STATIC_PAD_TEMPLATE ("src",
|
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_ALWAYS,
|
2011-08-16 19:12:06 +00:00
|
|
|
GST_STATIC_CAPS ("audio/x-raw-int, "
|
|
|
|
"endianness = (int) " G_STRINGIFY (G_BYTE_ORDER) ", "
|
|
|
|
"signed = (boolean) true, "
|
|
|
|
"width = (int) 32, "
|
|
|
|
"depth = (int) 32, "
|
2004-06-14 10:58:27 +00:00
|
|
|
"rate = (int) { 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000 }, "
|
|
|
|
"channels = (int) [ 1, 2 ]")
|
2004-03-14 22:34:30 +00:00
|
|
|
);
|
2001-12-20 22:10:54 +00:00
|
|
|
|
2004-06-14 10:58:27 +00:00
|
|
|
/* FIXME: make three caps, for mpegversion 1, 2 and 2.5 */
|
2003-12-22 01:47:08 +00:00
|
|
|
static GstStaticPadTemplate mad_sink_template_factory =
|
2004-03-14 22:34:30 +00:00
|
|
|
GST_STATIC_PAD_TEMPLATE ("sink",
|
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_ALWAYS,
|
|
|
|
GST_STATIC_CAPS ("audio/mpeg, "
|
2004-06-14 10:58:27 +00:00
|
|
|
"mpegversion = (int) 1, "
|
|
|
|
"layer = (int) [ 1, 3 ], "
|
|
|
|
"rate = (int) { 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000 }, "
|
|
|
|
"channels = (int) [ 1, 2 ]")
|
2004-03-14 22:34:30 +00:00
|
|
|
);
|
2001-12-20 22:10:54 +00:00
|
|
|
|
2011-08-16 19:12:06 +00:00
|
|
|
|
2011-11-13 00:29:20 +00:00
|
|
|
static gboolean gst_mad_start (GstAudioDecoder * dec);
|
|
|
|
static gboolean gst_mad_stop (GstAudioDecoder * dec);
|
|
|
|
static gboolean gst_mad_parse (GstAudioDecoder * dec, GstAdapter * adapter,
|
2011-08-16 19:12:06 +00:00
|
|
|
gint * offset, gint * length);
|
2011-11-13 00:29:20 +00:00
|
|
|
static GstFlowReturn gst_mad_handle_frame (GstAudioDecoder * dec,
|
2011-08-16 19:12:06 +00:00
|
|
|
GstBuffer * buffer);
|
2011-11-13 00:29:20 +00:00
|
|
|
static void gst_mad_flush (GstAudioDecoder * dec, gboolean hard);
|
2004-03-14 22:34:30 +00:00
|
|
|
|
|
|
|
static void gst_mad_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec);
|
|
|
|
static void gst_mad_get_property (GObject * object, guint prop_id,
|
|
|
|
GValue * value, GParamSpec * pspec);
|
2001-12-20 22:10:54 +00:00
|
|
|
|
2011-11-13 00:29:20 +00:00
|
|
|
GST_BOILERPLATE (GstMad, gst_mad, GstAudioDecoder, GST_TYPE_AUDIO_DECODER);
|
2002-07-24 21:36:39 +00:00
|
|
|
|
2011-08-16 19:12:06 +00:00
|
|
|
static void
|
|
|
|
gst_mad_base_init (gpointer g_class)
|
2004-03-14 22:34:30 +00:00
|
|
|
{
|
2011-08-16 19:12:06 +00:00
|
|
|
GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
|
2002-07-24 21:36:39 +00:00
|
|
|
|
2011-08-16 19:12:06 +00:00
|
|
|
gst_element_class_add_pad_template (element_class,
|
|
|
|
gst_static_pad_template_get (&mad_sink_template_factory));
|
|
|
|
gst_element_class_add_pad_template (element_class,
|
|
|
|
gst_static_pad_template_get (&mad_src_template_factory));
|
|
|
|
gst_element_class_set_details_simple (element_class, "mad mp3 decoder",
|
|
|
|
"Codec/Decoder/Audio",
|
|
|
|
"Uses mad code to decode mp3 streams", "Wim Taymans <wim@fluendo.com>");
|
2002-07-24 21:36:39 +00:00
|
|
|
}
|
2002-02-06 18:10:35 +00:00
|
|
|
|
2001-12-20 22:10:54 +00:00
|
|
|
static void
|
2004-03-14 22:34:30 +00:00
|
|
|
gst_mad_class_init (GstMadClass * klass)
|
2001-12-20 22:10:54 +00:00
|
|
|
{
|
2011-08-16 19:12:06 +00:00
|
|
|
GObjectClass *gobject_class = (GObjectClass *) klass;
|
2011-11-13 00:29:20 +00:00
|
|
|
GstAudioDecoderClass *base_class = (GstAudioDecoderClass *) klass;
|
2001-12-20 22:10:54 +00:00
|
|
|
|
2011-08-16 19:12:06 +00:00
|
|
|
parent_class = g_type_class_peek_parent (klass);
|
|
|
|
|
|
|
|
base_class->start = GST_DEBUG_FUNCPTR (gst_mad_start);
|
|
|
|
base_class->stop = GST_DEBUG_FUNCPTR (gst_mad_stop);
|
|
|
|
base_class->parse = GST_DEBUG_FUNCPTR (gst_mad_parse);
|
|
|
|
base_class->handle_frame = GST_DEBUG_FUNCPTR (gst_mad_handle_frame);
|
|
|
|
base_class->flush = GST_DEBUG_FUNCPTR (gst_mad_flush);
|
2001-12-20 22:10:54 +00:00
|
|
|
|
2002-02-06 18:10:35 +00:00
|
|
|
gobject_class->set_property = gst_mad_set_property;
|
|
|
|
gobject_class->get_property = gst_mad_get_property;
|
2001-12-28 01:59:26 +00:00
|
|
|
|
2002-02-06 18:10:35 +00:00
|
|
|
/* init properties */
|
|
|
|
/* currently, string representations are used, we might want to change that */
|
2003-01-08 16:11:48 +00:00
|
|
|
/* FIXME: descriptions need to be more technical,
|
|
|
|
* default values and ranges need to be selected right */
|
2002-11-02 17:40:16 +00:00
|
|
|
g_object_class_install_property (gobject_class, ARG_HALF,
|
2004-03-14 22:34:30 +00:00
|
|
|
g_param_spec_boolean ("half", "Half", "Generate PCM at 1/2 sample rate",
|
2010-10-19 07:06:33 +00:00
|
|
|
FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
2002-11-02 17:40:16 +00:00
|
|
|
g_object_class_install_property (gobject_class, ARG_IGNORE_CRC,
|
2010-10-19 09:20:40 +00:00
|
|
|
g_param_spec_boolean ("ignore-crc", "Ignore CRC", "Ignore CRC errors",
|
2010-10-19 07:06:33 +00:00
|
|
|
TRUE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
2001-12-20 22:10:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-08-16 19:12:06 +00:00
|
|
|
gst_mad_init (GstMad * mad, GstMadClass * klass)
|
2001-12-20 22:10:54 +00:00
|
|
|
{
|
2011-11-13 00:29:20 +00:00
|
|
|
GstAudioDecoder *dec;
|
2011-08-16 19:12:06 +00:00
|
|
|
|
2011-11-13 00:29:20 +00:00
|
|
|
dec = GST_AUDIO_DECODER (mad);
|
|
|
|
gst_audio_decoder_set_tolerance (dec, 20 * GST_MSECOND);
|
2002-05-26 21:59:22 +00:00
|
|
|
|
2002-11-02 17:40:16 +00:00
|
|
|
mad->half = FALSE;
|
2004-05-16 20:50:13 +00:00
|
|
|
mad->ignore_crc = TRUE;
|
2002-05-26 21:59:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2011-11-13 00:29:20 +00:00
|
|
|
gst_mad_start (GstAudioDecoder * dec)
|
2002-05-26 21:59:22 +00:00
|
|
|
{
|
2011-08-16 19:12:06 +00:00
|
|
|
GstMad *mad = GST_MAD (dec);
|
|
|
|
guint options = 0;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (dec, "start");
|
|
|
|
mad_stream_init (&mad->stream);
|
|
|
|
mad_frame_init (&mad->frame);
|
|
|
|
mad_synth_init (&mad->synth);
|
|
|
|
mad->rate = 0;
|
|
|
|
mad->channels = 0;
|
|
|
|
mad->caps_set = FALSE;
|
|
|
|
mad->frame.header.samplerate = 0;
|
|
|
|
if (mad->ignore_crc)
|
|
|
|
options |= MAD_OPTION_IGNORECRC;
|
|
|
|
if (mad->half)
|
|
|
|
options |= MAD_OPTION_HALFSAMPLERATE;
|
|
|
|
mad_stream_options (&mad->stream, options);
|
|
|
|
mad->header.mode = -1;
|
|
|
|
mad->header.emphasis = -1;
|
2003-01-20 19:38:37 +00:00
|
|
|
|
2011-08-16 19:12:06 +00:00
|
|
|
/* call upon legacy upstream byte support (e.g. seeking) */
|
2011-11-13 00:29:20 +00:00
|
|
|
gst_audio_decoder_set_byte_time (dec, TRUE);
|
Various event updates and cleanups.
Original commit message from CVS:
* ext/amrnb/amrnbparse.c: (gst_amrnbparse_event),
(gst_amrnbparse_loop):
* ext/dv/gstdvdec.c: (gst_dvdec_handle_sink_event),
(gst_dvdec_handle_src_event), (gst_dvdec_decode_frame):
* ext/mad/gstid3tag.c: (gst_id3_tag_src_event),
(gst_id3_tag_sink_event), (gst_id3_tag_chain):
* ext/mad/gstmad.c: (gst_mad_src_query), (index_seek),
(normal_seek), (gst_mad_sink_event), (gst_mad_chain):
* ext/mpeg2dec/gstmpeg2dec.c:
* ext/shout2/gstshout2.c: (gst_shout2send_event):
* ext/sidplay/gstsiddec.cc:
* gst/avi/gstavidemux.c: (gst_avi_demux_handle_src_event),
(gst_avi_demux_send_event), (gst_avi_demux_stream_header),
(gst_avi_demux_handle_seek), (gst_avi_demux_process_next_entry):
* gst/goom/gstgoom.c: (gst_goom_event):
* gst/realmedia/rmdemux.c: (gst_rmdemux_sink_event),
(gst_rmdemux_chain), (gst_rmdemux_send_event),
(gst_rmdemux_add_stream):
* gst/wavparse/gstwavparse.c: (gst_wavparse_handle_seek),
(gst_wavparse_stream_headers), (gst_wavparse_stream_data),
(gst_wavparse_loop), (gst_wavparse_srcpad_event):
Various event updates and cleanups.
2005-07-27 18:37:25 +00:00
|
|
|
|
2011-08-16 19:12:06 +00:00
|
|
|
return TRUE;
|
2003-01-20 19:38:37 +00:00
|
|
|
}
|
|
|
|
|
2002-03-30 17:06:26 +00:00
|
|
|
static gboolean
|
2011-11-13 00:29:20 +00:00
|
|
|
gst_mad_stop (GstAudioDecoder * dec)
|
2002-03-30 17:06:26 +00:00
|
|
|
{
|
2011-08-16 19:12:06 +00:00
|
|
|
GstMad *mad = GST_MAD (dec);
|
2003-01-08 16:11:48 +00:00
|
|
|
|
2011-08-16 19:12:06 +00:00
|
|
|
GST_DEBUG_OBJECT (dec, "stop");
|
|
|
|
mad_synth_finish (&mad->synth);
|
|
|
|
mad_frame_finish (&mad->frame);
|
|
|
|
mad_stream_finish (&mad->stream);
|
2002-03-30 17:06:26 +00:00
|
|
|
|
2011-08-16 19:12:06 +00:00
|
|
|
return TRUE;
|
2002-03-30 17:06:26 +00:00
|
|
|
}
|
|
|
|
|
2006-01-31 22:03:30 +00:00
|
|
|
static inline gint32
|
2001-12-20 22:10:54 +00:00
|
|
|
scale (mad_fixed_t sample)
|
|
|
|
{
|
2006-01-31 22:03:30 +00:00
|
|
|
#if MAD_F_FRACBITS < 28
|
2001-12-20 22:10:54 +00:00
|
|
|
/* round */
|
2006-01-31 22:03:30 +00:00
|
|
|
sample += (1L << (28 - MAD_F_FRACBITS - 1));
|
|
|
|
#endif
|
2001-12-20 22:10:54 +00:00
|
|
|
|
|
|
|
/* clip */
|
|
|
|
if (sample >= MAD_F_ONE)
|
|
|
|
sample = MAD_F_ONE - 1;
|
|
|
|
else if (sample < -MAD_F_ONE)
|
|
|
|
sample = -MAD_F_ONE;
|
|
|
|
|
2006-01-31 22:03:30 +00:00
|
|
|
#if MAD_F_FRACBITS < 28
|
2001-12-20 22:10:54 +00:00
|
|
|
/* quantize */
|
2006-01-31 22:03:30 +00:00
|
|
|
sample >>= (28 - MAD_F_FRACBITS);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* convert from 29 bits to 32 bits */
|
|
|
|
return (gint32) (sample << 3);
|
2001-12-20 22:10:54 +00:00
|
|
|
}
|
|
|
|
|
2004-04-05 12:28:57 +00:00
|
|
|
/* internal function to check if the header has changed and thus the
|
|
|
|
* caps need to be reset. Only call during normal mode, not resyncing */
|
2005-05-06 11:04:30 +00:00
|
|
|
static void
|
2004-04-05 12:28:57 +00:00
|
|
|
gst_mad_check_caps_reset (GstMad * mad)
|
|
|
|
{
|
|
|
|
guint nchannels;
|
2011-08-16 19:12:06 +00:00
|
|
|
guint rate;
|
2004-04-05 12:28:57 +00:00
|
|
|
|
|
|
|
nchannels = MAD_NCHANNELS (&mad->frame.header);
|
|
|
|
|
|
|
|
#if MAD_VERSION_MINOR <= 12
|
|
|
|
rate = mad->header.sfreq;
|
|
|
|
#else
|
|
|
|
rate = mad->frame.header.samplerate;
|
|
|
|
#endif
|
|
|
|
|
2004-04-07 15:48:40 +00:00
|
|
|
/* rate and channels are not supposed to change in a continuous stream,
|
|
|
|
* so check this first before doing anything */
|
|
|
|
|
|
|
|
/* only set caps if they weren't already set for this continuous stream */
|
|
|
|
if (mad->channels != nchannels || mad->rate != rate) {
|
2011-08-16 19:12:06 +00:00
|
|
|
GstCaps *caps;
|
|
|
|
|
2004-04-07 15:48:40 +00:00
|
|
|
if (mad->caps_set) {
|
2011-08-16 19:12:06 +00:00
|
|
|
GST_DEBUG_OBJECT (mad, "Header changed from %d Hz/%d ch to %d Hz/%d ch, "
|
|
|
|
"failed sync after seek ?", mad->rate, mad->channels, rate,
|
|
|
|
nchannels);
|
ext/mad/gstmad.c: Allow for mp3 rate/channels changes. However, only very conservatively. Reason that we *have* to en...
Original commit message from CVS:
* ext/mad/gstmad.c: (gst_mad_check_caps_reset),
(gst_mad_change_state):
Allow for mp3 rate/channels changes. However, only very
conservatively. Reason that we *have* to enable this is smiply
because the mad find_sync() function is not good enough, it will
regularly sync on random data as valid frames and therefore make
us provide random caps as *final* caps of the stream. The best fix
I could think of is to simply require several of the same stream
changes in a row before we change caps.
The actual testcase that works now is #
* ext/ogg/Makefile.am:
* ext/ogg/gstogg.c: (plugin_init):
* ext/ogg/gstogmparse.c:
OGM support (video only for now; I need an audio sample file).
* gst/asfdemux/gstasfdemux.c: (gst_asf_demux_base_init),
(gst_asf_demux_process_stream), (gst_asf_demux_video_caps),
(gst_asf_demux_add_video_stream):
WMV extradata.
* gst/playback/gstplaybasebin.c: (unknown_type):
Don't error out on single unknown-types after all. It's wrong.
If we found type of video and audio but not of a subtitle stream,
it will still error out (which is unwanted). Will find a better fix
later on.
* gst/typefind/gsttypefindfunctions.c: (ogmvideo_type_find),
(ogmaudio_type_find), (plugin_init):
OGM support.
2004-09-20 12:40:40 +00:00
|
|
|
/* we're conservative on stream changes. However, our *initial* caps
|
|
|
|
* might have been wrong as well - mad ain't perfect in syncing. So,
|
|
|
|
* we count caps changes and change if we pass a limit treshold (3). */
|
|
|
|
if (nchannels != mad->pending_channels || rate != mad->pending_rate) {
|
|
|
|
mad->times_pending = 0;
|
|
|
|
mad->pending_channels = nchannels;
|
|
|
|
mad->pending_rate = rate;
|
|
|
|
}
|
|
|
|
if (++mad->times_pending < 3)
|
2005-05-06 11:04:30 +00:00
|
|
|
return;
|
2004-04-07 15:48:40 +00:00
|
|
|
}
|
2004-04-05 12:28:57 +00:00
|
|
|
|
2005-05-06 11:04:30 +00:00
|
|
|
if (mad->stream.options & MAD_OPTION_HALFSAMPLERATE)
|
|
|
|
rate >>= 1;
|
|
|
|
|
2004-04-05 12:28:57 +00:00
|
|
|
/* we set the caps even when the pad is not connected so they
|
|
|
|
* can be gotten for streaminfo */
|
2011-08-16 19:12:06 +00:00
|
|
|
caps = gst_caps_new_simple ("audio/x-raw-int",
|
|
|
|
"endianness", G_TYPE_INT, G_BYTE_ORDER,
|
|
|
|
"signed", G_TYPE_BOOLEAN, TRUE,
|
|
|
|
"width", G_TYPE_INT, 32,
|
|
|
|
"depth", G_TYPE_INT, 32,
|
2004-04-05 12:28:57 +00:00
|
|
|
"rate", G_TYPE_INT, rate, "channels", G_TYPE_INT, nchannels, NULL);
|
|
|
|
|
2011-11-13 00:29:20 +00:00
|
|
|
gst_pad_set_caps (GST_AUDIO_DECODER_SRC_PAD (mad), caps);
|
2005-11-15 22:46:23 +00:00
|
|
|
gst_caps_unref (caps);
|
|
|
|
|
2011-08-16 19:12:06 +00:00
|
|
|
mad->caps_set = TRUE;
|
2005-05-06 11:04:30 +00:00
|
|
|
mad->channels = nchannels;
|
|
|
|
mad->rate = rate;
|
2004-04-05 12:28:57 +00:00
|
|
|
}
|
|
|
|
}
|
2004-03-05 23:32:36 +00:00
|
|
|
|
2010-09-29 19:24:23 +00:00
|
|
|
static GstFlowReturn
|
2011-11-13 00:29:20 +00:00
|
|
|
gst_mad_parse (GstAudioDecoder * dec, GstAdapter * adapter,
|
2011-08-16 19:12:06 +00:00
|
|
|
gint * _offset, gint * len)
|
2001-12-20 22:10:54 +00:00
|
|
|
{
|
|
|
|
GstMad *mad;
|
2011-08-16 19:12:06 +00:00
|
|
|
GstFlowReturn ret = GST_FLOW_UNEXPECTED;
|
|
|
|
gint av, size, offset, prev_offset, consumed = 0;
|
|
|
|
const guint8 *data;
|
|
|
|
|
|
|
|
mad = GST_MAD (dec);
|
|
|
|
|
|
|
|
/* we basically let mad library do parsing,
|
|
|
|
* and translate that back to baseclass.
|
|
|
|
* if a frame is found (and also decoded), subsequent handle_frame
|
|
|
|
* only needs to synthesize it */
|
|
|
|
|
|
|
|
prev_offset = -1;
|
|
|
|
offset = 0;
|
|
|
|
av = gst_adapter_available (adapter);
|
|
|
|
while (offset < av) {
|
|
|
|
size = MIN (MAD_BUFFER_MDLEN * 3, av - offset);
|
|
|
|
data = gst_adapter_peek (adapter, av);
|
|
|
|
|
|
|
|
/* check for mad asking too much */
|
|
|
|
if (offset == prev_offset) {
|
|
|
|
if (G_UNLIKELY (offset + size < av)) {
|
|
|
|
/* mad should not do this, so really fatal */
|
|
|
|
GST_ELEMENT_ERROR (mad, STREAM, DECODE, (NULL),
|
|
|
|
("mad claims to need more data than %u bytes", size));
|
|
|
|
ret = GST_FLOW_ERROR;
|
|
|
|
goto exit;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2004-11-05 03:33:26 +00:00
|
|
|
}
|
2002-12-11 21:57:39 +00:00
|
|
|
|
2011-08-16 19:12:06 +00:00
|
|
|
/* only feed that much to mad at a time */
|
|
|
|
mad_stream_buffer (&mad->stream, data + offset, size);
|
|
|
|
prev_offset = offset;
|
2001-12-28 01:59:26 +00:00
|
|
|
|
2011-08-16 19:12:06 +00:00
|
|
|
while (offset - prev_offset < size) {
|
|
|
|
consumed = 0;
|
2001-12-28 01:59:26 +00:00
|
|
|
|
2011-08-16 19:12:06 +00:00
|
|
|
GST_LOG_OBJECT (mad, "decoding the header now");
|
2005-05-06 11:04:30 +00:00
|
|
|
if (mad_header_decode (&mad->frame.header, &mad->stream) == -1) {
|
2006-08-16 10:34:57 +00:00
|
|
|
if (mad->stream.error == MAD_ERROR_BUFLEN) {
|
2011-08-16 19:12:06 +00:00
|
|
|
GST_LOG_OBJECT (mad,
|
|
|
|
"not enough data in tempbuffer (%d), breaking to get more", size);
|
2006-08-16 10:34:57 +00:00
|
|
|
break;
|
|
|
|
} else {
|
2011-08-16 19:12:06 +00:00
|
|
|
GST_WARNING_OBJECT (mad, "mad_header_decode had an error: %s",
|
2006-08-16 10:34:57 +00:00
|
|
|
mad_stream_errorstr (&mad->stream));
|
|
|
|
}
|
2005-05-06 11:04:30 +00:00
|
|
|
}
|
|
|
|
|
2011-08-16 19:12:06 +00:00
|
|
|
GST_LOG_OBJECT (mad, "parsing and decoding one frame now");
|
2001-12-28 01:59:26 +00:00
|
|
|
if (mad_frame_decode (&mad->frame, &mad->stream) == -1) {
|
2011-08-16 19:12:06 +00:00
|
|
|
GST_LOG_OBJECT (mad, "got error %d", mad->stream.error);
|
2004-10-13 14:21:02 +00:00
|
|
|
|
2004-03-15 19:32:25 +00:00
|
|
|
/* not enough data, need to wait for next buffer? */
|
|
|
|
if (mad->stream.error == MAD_ERROR_BUFLEN) {
|
2011-08-16 19:12:06 +00:00
|
|
|
if (mad->stream.next_frame == data) {
|
|
|
|
GST_LOG_OBJECT (mad,
|
|
|
|
"not enough data in tempbuffer (%d), breaking to get more",
|
|
|
|
size);
|
2004-11-05 03:33:26 +00:00
|
|
|
break;
|
|
|
|
} else {
|
2011-08-16 19:12:06 +00:00
|
|
|
GST_LOG_OBJECT (mad, "sync error, flushing unneeded data");
|
|
|
|
goto flush;
|
2004-11-05 03:33:26 +00:00
|
|
|
}
|
2009-02-19 19:08:10 +00:00
|
|
|
} else if (mad->stream.error == MAD_ERROR_BADDATAPTR) {
|
|
|
|
/* Flush data */
|
2011-08-16 19:12:06 +00:00
|
|
|
goto flush;
|
|
|
|
} else {
|
|
|
|
GST_WARNING_OBJECT (mad, "mad_frame_decode had an error: %s",
|
|
|
|
mad_stream_errorstr (&mad->stream));
|
|
|
|
if (!MAD_RECOVERABLE (mad->stream.error)) {
|
|
|
|
/* well, all may be well enough bytes later on ... */
|
2011-11-13 00:29:20 +00:00
|
|
|
GST_AUDIO_DECODER_ERROR (mad, 1, STREAM, DECODE, (NULL),
|
2011-08-16 19:12:06 +00:00
|
|
|
("mad error: %s", mad_stream_errorstr (&mad->stream)), ret);
|
|
|
|
/* so make sure we really move along ... */
|
|
|
|
if (!offset)
|
|
|
|
offset++;
|
|
|
|
goto exit;
|
|
|
|
} else {
|
|
|
|
const guint8 *before_sync, *after_sync;
|
|
|
|
|
|
|
|
mad_frame_mute (&mad->frame);
|
|
|
|
mad_synth_mute (&mad->synth);
|
|
|
|
before_sync = mad->stream.ptr.byte;
|
|
|
|
if (mad_stream_sync (&mad->stream) != 0)
|
|
|
|
GST_WARNING_OBJECT (mad, "mad_stream_sync failed");
|
|
|
|
after_sync = mad->stream.ptr.byte;
|
|
|
|
/* a succesful resync should make us drop bytes as consumed, so
|
|
|
|
* calculate from the byte pointers before and after resync */
|
|
|
|
consumed = after_sync - before_sync;
|
|
|
|
GST_DEBUG_OBJECT (mad, "resynchronization consumes %d bytes",
|
|
|
|
consumed);
|
|
|
|
GST_DEBUG_OBJECT (mad, "synced to data: 0x%0x 0x%0x",
|
|
|
|
*mad->stream.ptr.byte, *(mad->stream.ptr.byte + 1));
|
|
|
|
|
|
|
|
mad_stream_sync (&mad->stream);
|
|
|
|
/* recoverable errors pass */
|
|
|
|
goto flush;
|
2007-03-27 12:02:49 +00:00
|
|
|
}
|
|
|
|
}
|
2011-08-16 19:12:06 +00:00
|
|
|
} else {
|
|
|
|
/* decoding ok; found frame */
|
|
|
|
ret = GST_FLOW_OK;
|
2003-01-20 19:38:37 +00:00
|
|
|
}
|
2011-08-16 19:12:06 +00:00
|
|
|
flush:
|
|
|
|
if (consumed == 0) {
|
|
|
|
consumed = mad->stream.next_frame - (data + offset);
|
|
|
|
g_assert (consumed >= 0);
|
2003-01-20 19:38:37 +00:00
|
|
|
}
|
|
|
|
|
2011-08-16 19:12:06 +00:00
|
|
|
if (ret == GST_FLOW_OK)
|
|
|
|
goto exit;
|
2008-10-08 13:59:57 +00:00
|
|
|
|
2011-08-16 19:12:06 +00:00
|
|
|
offset += consumed;
|
|
|
|
}
|
|
|
|
}
|
2002-11-02 17:40:16 +00:00
|
|
|
|
2011-08-16 19:12:06 +00:00
|
|
|
exit:
|
|
|
|
*_offset = offset;
|
|
|
|
*len = consumed;
|
2002-05-26 21:59:22 +00:00
|
|
|
|
2011-08-16 19:12:06 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2002-09-18 22:04:32 +00:00
|
|
|
|
2011-08-16 19:12:06 +00:00
|
|
|
static GstFlowReturn
|
2011-11-13 00:29:20 +00:00
|
|
|
gst_mad_handle_frame (GstAudioDecoder * dec, GstBuffer * buffer)
|
2011-08-16 19:12:06 +00:00
|
|
|
{
|
|
|
|
GstMad *mad;
|
|
|
|
GstFlowReturn ret = GST_FLOW_UNEXPECTED;
|
|
|
|
GstBuffer *outbuffer;
|
|
|
|
guint nsamples;
|
|
|
|
gint32 *outdata;
|
|
|
|
mad_fixed_t const *left_ch, *right_ch;
|
|
|
|
|
|
|
|
mad = GST_MAD (dec);
|
|
|
|
|
|
|
|
/* no fancy draining */
|
|
|
|
if (G_UNLIKELY (!buffer))
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
|
|
|
|
/* _parse prepared a frame */
|
|
|
|
nsamples = MAD_NSBSAMPLES (&mad->frame.header) *
|
|
|
|
(mad->stream.options & MAD_OPTION_HALFSAMPLERATE ? 16 : 32);
|
|
|
|
GST_LOG_OBJECT (mad, "mad frame with %d samples", nsamples);
|
|
|
|
|
|
|
|
/* arrange for initial caps before pushing data,
|
|
|
|
* and update later on if needed */
|
|
|
|
gst_mad_check_caps_reset (mad);
|
|
|
|
|
|
|
|
// TODO more generic check and size check ?
|
2011-11-13 00:29:20 +00:00
|
|
|
ret = gst_pad_alloc_buffer_and_set_caps (GST_AUDIO_DECODER_SRC_PAD (dec),
|
2011-08-16 19:12:06 +00:00
|
|
|
0, nsamples * mad->channels * 4,
|
2011-11-13 00:29:20 +00:00
|
|
|
GST_PAD_CAPS (GST_AUDIO_DECODER_SRC_PAD (dec)), &outbuffer);
|
2011-08-16 19:12:06 +00:00
|
|
|
if (ret != GST_FLOW_OK) {
|
|
|
|
/* Head for the exit, dropping samples as we go */
|
|
|
|
GST_LOG_OBJECT (dec,
|
|
|
|
"Skipping frame synthesis due to pad_alloc return value");
|
2011-11-13 00:29:20 +00:00
|
|
|
gst_audio_decoder_finish_frame (dec, NULL, 1);
|
2011-08-16 19:12:06 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
mad_synth_frame (&mad->synth, &mad->frame);
|
|
|
|
left_ch = mad->synth.pcm.samples[0];
|
|
|
|
right_ch = mad->synth.pcm.samples[1];
|
|
|
|
|
|
|
|
outdata = (gint32 *) GST_BUFFER_DATA (outbuffer);
|
|
|
|
|
|
|
|
/* output sample(s) in 16-bit signed native-endian PCM */
|
|
|
|
if (mad->channels == 1) {
|
|
|
|
gint count = nsamples;
|
|
|
|
|
|
|
|
while (count--) {
|
|
|
|
*outdata++ = scale (*left_ch++) & 0xffffffff;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
gint count = nsamples;
|
2002-06-09 14:30:16 +00:00
|
|
|
|
2011-08-16 19:12:06 +00:00
|
|
|
while (count--) {
|
|
|
|
*outdata++ = scale (*left_ch++) & 0xffffffff;
|
|
|
|
*outdata++ = scale (*right_ch++) & 0xffffffff;
|
2001-12-28 01:59:26 +00:00
|
|
|
}
|
2001-12-20 22:10:54 +00:00
|
|
|
}
|
|
|
|
|
2011-11-13 00:29:20 +00:00
|
|
|
ret = gst_audio_decoder_finish_frame (dec, outbuffer, 1);
|
2011-08-16 19:12:06 +00:00
|
|
|
exit:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-11-13 00:29:20 +00:00
|
|
|
gst_mad_flush (GstAudioDecoder * dec, gboolean hard)
|
2011-08-16 19:12:06 +00:00
|
|
|
{
|
|
|
|
GstMad *mad;
|
2005-05-06 11:04:30 +00:00
|
|
|
|
2011-08-16 19:12:06 +00:00
|
|
|
mad = GST_MAD (dec);
|
|
|
|
if (hard) {
|
|
|
|
mad_frame_mute (&mad->frame);
|
|
|
|
mad_synth_mute (&mad->synth);
|
|
|
|
}
|
2001-12-20 22:10:54 +00:00
|
|
|
}
|
|
|
|
|
2011-08-16 19:12:06 +00:00
|
|
|
static void
|
|
|
|
gst_mad_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec)
|
2001-12-20 22:10:54 +00:00
|
|
|
{
|
|
|
|
GstMad *mad;
|
|
|
|
|
2011-08-16 19:12:06 +00:00
|
|
|
mad = GST_MAD (object);
|
2001-12-20 22:10:54 +00:00
|
|
|
|
2011-08-16 19:12:06 +00:00
|
|
|
switch (prop_id) {
|
|
|
|
case ARG_HALF:
|
|
|
|
mad->half = g_value_get_boolean (value);
|
2001-12-28 01:59:26 +00:00
|
|
|
break;
|
2011-08-16 19:12:06 +00:00
|
|
|
case ARG_IGNORE_CRC:
|
|
|
|
mad->ignore_crc = g_value_get_boolean (value);
|
2001-12-28 01:59:26 +00:00
|
|
|
break;
|
2005-05-25 12:58:23 +00:00
|
|
|
default:
|
2011-08-16 19:12:06 +00:00
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
2005-05-25 12:58:23 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-08-16 19:12:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_mad_get_property (GObject * object, guint prop_id,
|
|
|
|
GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstMad *mad;
|
2005-05-25 12:58:23 +00:00
|
|
|
|
2011-08-16 19:12:06 +00:00
|
|
|
mad = GST_MAD (object);
|
2005-05-25 12:58:23 +00:00
|
|
|
|
2011-08-16 19:12:06 +00:00
|
|
|
switch (prop_id) {
|
|
|
|
case ARG_HALF:
|
|
|
|
g_value_set_boolean (value, mad->half);
|
2001-12-28 01:59:26 +00:00
|
|
|
break;
|
2011-08-16 19:12:06 +00:00
|
|
|
case ARG_IGNORE_CRC:
|
|
|
|
g_value_set_boolean (value, mad->ignore_crc);
|
2001-12-28 01:59:26 +00:00
|
|
|
break;
|
2005-05-25 12:58:23 +00:00
|
|
|
default:
|
2011-08-16 19:12:06 +00:00
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
2005-05-25 12:58:23 +00:00
|
|
|
break;
|
2001-12-28 01:59:26 +00:00
|
|
|
}
|
|
|
|
}
|
2009-06-05 19:53:57 +00:00
|
|
|
|
2011-08-16 19:12:06 +00:00
|
|
|
/* plugin initialisation */
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_mad_register (GstPlugin * plugin)
|
2009-06-05 19:53:57 +00:00
|
|
|
{
|
2011-11-13 00:29:20 +00:00
|
|
|
GST_DEBUG_CATEGORY_INIT (mad_debug, "mad", 0, "mad mp3 decoding");
|
2009-06-05 19:53:57 +00:00
|
|
|
|
2011-11-13 00:29:20 +00:00
|
|
|
return gst_element_register (plugin, "mad", GST_RANK_NONE,
|
2009-06-05 19:53:57 +00:00
|
|
|
gst_mad_get_type ());
|
|
|
|
}
|