gstreamer/ext/speex/gstspeexdec.c

579 lines
16 KiB
C
Raw Normal View History

/* GStreamer
* Copyright (C) 2004 Wim Taymans <wim@fluendo.com>
* Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
*
* 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., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
/**
* SECTION:element-speexdec
* @see_also: speexenc, oggdemux
*
* This element decodes a Speex stream to raw integer audio.
* <ulink url="http://www.speex.org/">Speex</ulink> is a royalty-free
* audio codec maintained by the <ulink url="http://www.xiph.org/">Xiph.org
* Foundation</ulink>.
*
* <refsect2>
* <title>Example pipelines</title>
* |[
* gst-launch-1.0 -v filesrc location=speex.ogg ! oggdemux ! speexdec ! audioconvert ! audioresample ! alsasink
* ]| Decode an Ogg/Speex file. To create an Ogg/Speex file refer to the
* documentation of speexenc.
* </refsect2>
*
* Last reviewed on 2006-04-05 (0.10.2)
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "gstspeexdec.h"
2010-02-12 13:34:38 +00:00
#include <stdlib.h>
#include <string.h>
#include <gst/tag/tag.h>
#include <gst/audio/audio.h>
GST_DEBUG_CATEGORY_STATIC (speexdec_debug);
#define GST_CAT_DEFAULT speexdec_debug
#define DEFAULT_ENH TRUE
enum
{
ARG_0,
ARG_ENH
};
#define FORMAT_STR GST_AUDIO_NE(S16)
static GstStaticPadTemplate speex_dec_src_factory =
GST_STATIC_PAD_TEMPLATE ("src",
GST_PAD_SRC,
GST_PAD_ALWAYS,
GST_STATIC_CAPS ("audio/x-raw, "
"format = (string) " FORMAT_STR ", "
"layout = (string) interleaved, "
"rate = (int) [ 6000, 48000 ], " "channels = (int) [ 1, 2 ]")
);
static GstStaticPadTemplate speex_dec_sink_factory =
GST_STATIC_PAD_TEMPLATE ("sink",
GST_PAD_SINK,
GST_PAD_ALWAYS,
GST_STATIC_CAPS ("audio/x-speex")
);
2011-07-06 13:57:23 +00:00
#define gst_speex_dec_parent_class parent_class
G_DEFINE_TYPE (GstSpeexDec, gst_speex_dec, GST_TYPE_AUDIO_DECODER);
2011-09-29 15:21:22 +00:00
static gboolean gst_speex_dec_start (GstAudioDecoder * dec);
static gboolean gst_speex_dec_stop (GstAudioDecoder * dec);
static gboolean gst_speex_dec_set_format (GstAudioDecoder * bdec,
GstCaps * caps);
static GstFlowReturn gst_speex_dec_handle_frame (GstAudioDecoder * dec,
GstBuffer * buffer);
static void gst_speex_dec_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec);
static void gst_speex_dec_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec);
static void
gst_speex_dec_class_init (GstSpeexDecClass * klass)
{
GObjectClass *gobject_class;
GstElementClass *gstelement_class;
2011-09-29 15:21:22 +00:00
GstAudioDecoderClass *base_class;
gobject_class = (GObjectClass *) klass;
gstelement_class = (GstElementClass *) klass;
2011-09-29 15:21:22 +00:00
base_class = (GstAudioDecoderClass *) klass;
gobject_class->set_property = gst_speex_dec_set_property;
gobject_class->get_property = gst_speex_dec_get_property;
2011-09-29 15:21:22 +00:00
base_class->start = GST_DEBUG_FUNCPTR (gst_speex_dec_start);
base_class->stop = GST_DEBUG_FUNCPTR (gst_speex_dec_stop);
base_class->set_format = GST_DEBUG_FUNCPTR (gst_speex_dec_set_format);
base_class->handle_frame = GST_DEBUG_FUNCPTR (gst_speex_dec_handle_frame);
g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_ENH,
g_param_spec_boolean ("enh", "Enh", "Enable perceptual enhancement",
DEFAULT_ENH, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2011-07-06 13:57:23 +00:00
gst_element_class_add_pad_template (gstelement_class,
gst_static_pad_template_get (&speex_dec_src_factory));
gst_element_class_add_pad_template (gstelement_class,
gst_static_pad_template_get (&speex_dec_sink_factory));
gst_element_class_set_static_metadata (gstelement_class,
"Speex audio decoder", "Codec/Decoder/Audio",
2011-07-06 13:57:23 +00:00
"decode speex streams to audio", "Wim Taymans <wim@fluendo.com>");
GST_DEBUG_CATEGORY_INIT (speexdec_debug, "speexdec", 0,
"speex decoding element");
}
static void
gst_speex_dec_reset (GstSpeexDec * dec)
{
dec->packetno = 0;
dec->frame_size = 0;
dec->frame_duration = 0;
dec->mode = NULL;
free (dec->header);
dec->header = NULL;
speex_bits_destroy (&dec->bits);
gst_buffer_replace (&dec->streamheader, NULL);
gst_buffer_replace (&dec->vorbiscomment, NULL);
if (dec->stereo) {
speex_stereo_state_destroy (dec->stereo);
dec->stereo = NULL;
}
if (dec->state) {
speex_decoder_destroy (dec->state);
dec->state = NULL;
}
}
static void
2011-07-06 13:57:23 +00:00
gst_speex_dec_init (GstSpeexDec * dec)
{
gst_audio_decoder_set_needs_format (GST_AUDIO_DECODER (dec), TRUE);
dec->enh = DEFAULT_ENH;
gst_speex_dec_reset (dec);
}
static gboolean
2011-09-29 15:21:22 +00:00
gst_speex_dec_start (GstAudioDecoder * dec)
{
2011-09-29 15:21:22 +00:00
GstSpeexDec *sd = GST_SPEEX_DEC (dec);
2011-09-29 15:21:22 +00:00
GST_DEBUG_OBJECT (dec, "start");
gst_speex_dec_reset (sd);
2011-09-29 15:21:22 +00:00
/* we know about concealment */
gst_audio_decoder_set_plc_aware (dec, TRUE);
2011-09-29 15:21:22 +00:00
return TRUE;
}
static gboolean
2011-09-29 15:21:22 +00:00
gst_speex_dec_stop (GstAudioDecoder * dec)
{
2011-09-29 15:21:22 +00:00
GstSpeexDec *sd = GST_SPEEX_DEC (dec);
2011-09-29 15:21:22 +00:00
GST_DEBUG_OBJECT (dec, "stop");
gst_speex_dec_reset (sd);
2011-09-29 15:21:22 +00:00
return TRUE;
}
static GstFlowReturn
2011-09-29 15:21:22 +00:00
gst_speex_dec_parse_header (GstSpeexDec * dec, GstBuffer * buf)
{
2012-01-24 13:38:58 +00:00
GstMapInfo map;
GstAudioInfo info;
static const GstAudioChannelPosition chan_pos[2][2] = {
{GST_AUDIO_CHANNEL_POSITION_MONO},
{GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT}
};
/* get the header */
2012-01-24 13:38:58 +00:00
gst_buffer_map (buf, &map, GST_MAP_READ);
dec->header = speex_packet_to_header ((gchar *) map.data, map.size);
gst_buffer_unmap (buf, &map);
if (!dec->header)
goto no_header;
if (dec->header->mode >= SPEEX_NB_MODES || dec->header->mode < 0)
goto mode_too_old;
dec->mode = speex_lib_get_mode (dec->header->mode);
/* initialize the decoder */
dec->state = speex_decoder_init (dec->mode);
if (!dec->state)
goto init_failed;
speex_decoder_ctl (dec->state, SPEEX_SET_ENH, &dec->enh);
speex_decoder_ctl (dec->state, SPEEX_GET_FRAME_SIZE, &dec->frame_size);
if (dec->header->nb_channels != 1) {
dec->stereo = speex_stereo_state_init ();
dec->callback.callback_id = SPEEX_INBAND_STEREO;
dec->callback.func = speex_std_stereo_request_handler;
dec->callback.data = dec->stereo;
speex_decoder_ctl (dec->state, SPEEX_SET_HANDLER, &dec->callback);
}
speex_decoder_ctl (dec->state, SPEEX_SET_SAMPLING_RATE, &dec->header->rate);
dec->frame_duration = gst_util_uint64_scale_int (dec->frame_size,
GST_SECOND, dec->header->rate);
speex_bits_init (&dec->bits);
/* set caps */
gst_audio_info_init (&info);
gst_audio_info_set_format (&info,
GST_AUDIO_FORMAT_S16,
dec->header->rate,
dec->header->nb_channels, chan_pos[dec->header->nb_channels - 1]);
if (!gst_audio_decoder_set_output_format (GST_AUDIO_DECODER (dec), &info))
goto nego_failed;
return GST_FLOW_OK;
/* ERRORS */
no_header:
{
GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
(NULL), ("couldn't read header"));
return GST_FLOW_ERROR;
}
mode_too_old:
{
GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
(NULL),
("Mode number %d does not (yet/any longer) exist in this version",
dec->header->mode));
return GST_FLOW_ERROR;
}
init_failed:
{
GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
(NULL), ("couldn't initialize decoder"));
return GST_FLOW_ERROR;
}
nego_failed:
{
GST_ELEMENT_ERROR (GST_ELEMENT (dec), STREAM, DECODE,
(NULL), ("couldn't negotiate format"));
return GST_FLOW_NOT_NEGOTIATED;
}
}
static GstFlowReturn
2011-09-29 15:21:22 +00:00
gst_speex_dec_parse_comments (GstSpeexDec * dec, GstBuffer * buf)
{
GstTagList *list;
gchar *ver, *encoder = NULL;
list = gst_tag_list_from_vorbiscomment_buffer (buf, NULL, 0, &encoder);
if (!list) {
GST_WARNING_OBJECT (dec, "couldn't decode comments");
list = gst_tag_list_new_empty ();
}
if (encoder) {
gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
GST_TAG_ENCODER, encoder, NULL);
}
gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
GST_TAG_AUDIO_CODEC, "Speex", NULL);
ver = g_strndup (dec->header->speex_version, SPEEX_HEADER_VERSION_LENGTH);
g_strstrip (ver);
if (ver != NULL && *ver != '\0') {
gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
GST_TAG_ENCODER_VERSION, ver, NULL);
}
if (dec->header->bitrate > 0) {
gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
GST_TAG_BITRATE, (guint) dec->header->bitrate, NULL);
}
GST_INFO_OBJECT (dec, "tags: %" GST_PTR_FORMAT, list);
gst_audio_decoder_merge_tags (GST_AUDIO_DECODER (dec), list,
GST_TAG_MERGE_REPLACE);
gst_tag_list_unref (list);
g_free (encoder);
g_free (ver);
return GST_FLOW_OK;
}
2011-09-29 15:21:22 +00:00
static gboolean
gst_speex_dec_set_format (GstAudioDecoder * bdec, GstCaps * caps)
{
GstSpeexDec *dec = GST_SPEEX_DEC (bdec);
gboolean ret = TRUE;
GstStructure *s;
const GValue *streamheader;
s = gst_caps_get_structure (caps, 0);
if ((streamheader = gst_structure_get_value (s, "streamheader")) &&
G_VALUE_HOLDS (streamheader, GST_TYPE_ARRAY) &&
gst_value_array_get_size (streamheader) >= 2) {
const GValue *header, *vorbiscomment;
GstBuffer *buf;
GstFlowReturn res = GST_FLOW_OK;
header = gst_value_array_get_value (streamheader, 0);
if (header && G_VALUE_HOLDS (header, GST_TYPE_BUFFER)) {
buf = gst_value_get_buffer (header);
res = gst_speex_dec_parse_header (dec, buf);
if (res != GST_FLOW_OK)
goto done;
gst_buffer_replace (&dec->streamheader, buf);
}
vorbiscomment = gst_value_array_get_value (streamheader, 1);
if (vorbiscomment && G_VALUE_HOLDS (vorbiscomment, GST_TYPE_BUFFER)) {
buf = gst_value_get_buffer (vorbiscomment);
res = gst_speex_dec_parse_comments (dec, buf);
if (res != GST_FLOW_OK)
goto done;
gst_buffer_replace (&dec->vorbiscomment, buf);
}
}
done:
return ret;
}
static GstFlowReturn
2011-09-29 15:21:22 +00:00
gst_speex_dec_parse_data (GstSpeexDec * dec, GstBuffer * buf)
{
GstFlowReturn res = GST_FLOW_OK;
gint i, fpp;
SpeexBits *bits;
2012-01-24 13:38:58 +00:00
GstMapInfo map;
2010-02-01 21:36:02 +00:00
if (!dec->frame_duration)
goto not_negotiated;
if (G_LIKELY (gst_buffer_get_size (buf))) {
/* send data to the bitstream */
2012-01-24 13:38:58 +00:00
gst_buffer_map (buf, &map, GST_MAP_READ);
speex_bits_read_from (&dec->bits, (gchar *) map.data, map.size);
gst_buffer_unmap (buf, &map);
fpp = dec->header->frames_per_packet;
bits = &dec->bits;
2011-11-22 01:40:39 +00:00
GST_DEBUG_OBJECT (dec, "received buffer of size %" G_GSIZE_FORMAT
2012-01-24 13:38:58 +00:00
", fpp %d, %d bits", map.size, fpp, speex_bits_remaining (bits));
} else {
2011-09-29 15:21:22 +00:00
/* FIXME ? actually consider how much concealment is needed */
/* concealment data, pass NULL as the bits parameters */
GST_DEBUG_OBJECT (dec, "creating concealment data");
fpp = dec->header->frames_per_packet;
bits = NULL;
}
/* now decode each frame, catering for unknown number of them (e.g. rtp) */
for (i = 0; i < fpp; i++) {
GstBuffer *outbuf;
gboolean corrupted = FALSE;
gint ret;
GST_LOG_OBJECT (dec, "decoding frame %d/%d, %d bits remaining", i, fpp,
bits ? speex_bits_remaining (bits) : -1);
2011-07-06 13:57:23 +00:00
#if 0
2011-09-29 15:21:22 +00:00
res =
gst_pad_alloc_buffer_and_set_caps (GST_AUDIO_DECODER_SRC_PAD (dec),
GST_BUFFER_OFFSET_NONE, dec->frame_size * dec->header->nb_channels * 2,
2011-09-29 15:21:22 +00:00
GST_PAD_CAPS (GST_AUDIO_DECODER_SRC_PAD (dec)), &outbuf);
if (res != GST_FLOW_OK) {
GST_DEBUG_OBJECT (dec, "buf alloc flow: %s", gst_flow_get_name (res));
return res;
}
2011-07-06 13:57:23 +00:00
#endif
/* FIXME, we can use a bufferpool because we have fixed size buffers. We
* could also use an allocator */
outbuf =
gst_buffer_new_allocate (NULL,
2012-03-15 12:36:17 +00:00
dec->frame_size * dec->header->nb_channels * 2, NULL);
2012-01-24 13:38:58 +00:00
gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
ret = speex_decode_int (dec->state, bits, (spx_int16_t *) map.data);
2011-07-06 13:57:23 +00:00
if (ret == -1) {
/* uh? end of stream */
if (fpp == 0 && speex_bits_remaining (bits) < 8) {
/* if we did not know how many frames to expect, then we get this
at the end if there are leftover bits to pad to the next byte */
2011-09-29 15:21:22 +00:00
GST_DEBUG_OBJECT (dec, "Discarding leftover bits");
} else {
GST_WARNING_OBJECT (dec, "Unexpected end of stream found");
}
corrupted = TRUE;
} else if (ret == -2) {
GST_WARNING_OBJECT (dec, "Decoding error: corrupted stream?");
corrupted = TRUE;
}
if (bits && speex_bits_remaining (bits) < 0) {
GST_WARNING_OBJECT (dec, "Decoding overflow: corrupted stream?");
corrupted = TRUE;
}
if (dec->header->nb_channels == 2)
2012-01-24 13:38:58 +00:00
speex_decode_stereo_int ((spx_int16_t *) map.data, dec->frame_size,
dec->stereo);
gst_buffer_unmap (outbuf, &map);
if (!corrupted) {
res = gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (dec), outbuf, 1);
} else {
res = gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (dec), NULL, 1);
gst_buffer_unref (outbuf);
}
if (res != GST_FLOW_OK) {
GST_DEBUG_OBJECT (dec, "flow: %s", gst_flow_get_name (res));
break;
}
}
return res;
2010-02-01 21:36:02 +00:00
/* ERRORS */
not_negotiated:
{
GST_ELEMENT_ERROR (dec, CORE, NEGOTIATION, (NULL),
("decoder not initialized"));
return GST_FLOW_NOT_NEGOTIATED;
}
}
2011-07-06 13:57:23 +00:00
static gboolean
memcmp_buffers (GstBuffer * buf1, GstBuffer * buf2)
{
2012-01-24 13:38:58 +00:00
GstMapInfo map;
2011-07-06 13:57:23 +00:00
gsize size1, size2;
gboolean res;
size1 = gst_buffer_get_size (buf1);
size2 = gst_buffer_get_size (buf2);
if (size1 != size2)
return FALSE;
2012-01-24 13:38:58 +00:00
gst_buffer_map (buf1, &map, GST_MAP_READ);
res = gst_buffer_memcmp (buf2, 0, map.data, map.size) == 0;
gst_buffer_unmap (buf1, &map);
2011-07-06 13:57:23 +00:00
return res;
}
static GstFlowReturn
2011-09-29 15:21:22 +00:00
gst_speex_dec_handle_frame (GstAudioDecoder * bdec, GstBuffer * buf)
{
GstFlowReturn res;
GstSpeexDec *dec;
2011-09-29 15:21:22 +00:00
/* no fancy draining */
if (G_UNLIKELY (!buf))
return GST_FLOW_OK;
dec = GST_SPEEX_DEC (bdec);
/* If we have the streamheader and vorbiscomment from the caps already
* ignore them here */
if (dec->streamheader && dec->vorbiscomment) {
2011-07-06 13:57:23 +00:00
if (memcmp_buffers (dec->streamheader, buf)) {
2011-09-29 15:21:22 +00:00
GST_DEBUG_OBJECT (dec, "found streamheader");
gst_audio_decoder_finish_frame (bdec, NULL, 1);
res = GST_FLOW_OK;
2011-07-06 13:57:23 +00:00
} else if (memcmp_buffers (dec->vorbiscomment, buf)) {
2011-09-29 15:21:22 +00:00
GST_DEBUG_OBJECT (dec, "found vorbiscomments");
gst_audio_decoder_finish_frame (bdec, NULL, 1);
res = GST_FLOW_OK;
} else {
2011-09-29 15:21:22 +00:00
res = gst_speex_dec_parse_data (dec, buf);
}
} else {
/* Otherwise fall back to packet counting and assume that the
* first two packets are the headers. */
switch (dec->packetno) {
case 0:
2011-09-29 15:21:22 +00:00
GST_DEBUG_OBJECT (dec, "counted streamheader");
res = gst_speex_dec_parse_header (dec, buf);
gst_audio_decoder_finish_frame (bdec, NULL, 1);
break;
case 1:
2011-09-29 15:21:22 +00:00
GST_DEBUG_OBJECT (dec, "counted vorbiscomments");
res = gst_speex_dec_parse_comments (dec, buf);
gst_audio_decoder_finish_frame (bdec, NULL, 1);
break;
default:
2011-09-29 15:21:22 +00:00
{
res = gst_speex_dec_parse_data (dec, buf);
break;
2011-09-29 15:21:22 +00:00
}
2010-02-01 21:36:02 +00:00
}
}
dec->packetno++;
close #333784 unref the result of gst_pad_get_parent() by: Christophe Fergeau. Original commit message from CVS: * ext/cairo/gsttextoverlay.c: (gst_text_overlay_setcaps): * ext/esd/esdmon.c: (gst_esdmon_get): * ext/flac/gstflactag.c: (gst_flac_tag_chain): * ext/gdk_pixbuf/gstgdkpixbuf.c: (gst_gdk_pixbuf_sink_setcaps), (gst_gdk_pixbuf_sink_getcaps): * ext/jpeg/gstjpegenc.c: (gst_jpegenc_getcaps), (gst_jpegenc_setcaps): * ext/jpeg/gstsmokedec.c: (gst_smokedec_chain): * ext/jpeg/gstsmokeenc.c: (gst_smokeenc_getcaps), (gst_smokeenc_setcaps): * ext/libmng/gstmngdec.c: (gst_mngdec_sinklink), (gst_mngdec_src_getcaps): * ext/libmng/gstmngenc.c: (gst_mngenc_sinklink), (gst_mngenc_chain): * ext/libpng/gstpngenc.c: (gst_pngenc_setcaps): * ext/mikmod/gstmikmod.c: (gst_mikmod_srclink): * ext/speex/gstspeexdec.c: (speex_dec_convert), (speex_dec_src_event), (speex_dec_chain): * gst/avi/gstavimux.c: (gst_avimux_vidsinkconnect), (gst_avimux_audsinkconnect), (gst_avimux_handle_event): * gst/debug/negotiation.c: (gst_negotiation_getcaps), (gst_negotiation_pad_link), (gst_negotiation_chain): * gst/flx/gstflxdec.c: (gst_flxdec_src_query_handler), (gst_flxdec_chain): * gst/interleave/deinterleave.c: (deinterleave_sink_link), (deinterleave_chain): * gst/law/mulaw-encode.c: (mulawenc_setcaps): * gst/median/gstmedian.c: (gst_median_link): * gst/monoscope/gstmonoscope.c: (gst_monoscope_srcconnect), (gst_monoscope_chain): * gst/rtp/gstrtpL16pay.c: (gst_rtpL16pay_sinkconnect): * gst/wavenc/gstwavenc.c: (gst_wavenc_sink_setcaps): * sys/osxaudio/gstosxaudiosink.c: (gst_osxaudiosink_chain): * sys/osxaudio/gstosxaudiosrc.c: (gst_osxaudiosrc_get): close #333784 unref the result of gst_pad_get_parent() by: Christophe Fergeau.
2006-03-13 15:49:08 +00:00
return res;
}
static void
gst_speex_dec_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec)
{
GstSpeexDec *speexdec;
speexdec = GST_SPEEX_DEC (object);
switch (prop_id) {
case ARG_ENH:
g_value_set_boolean (value, speexdec->enh);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gst_speex_dec_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec)
{
GstSpeexDec *speexdec;
speexdec = GST_SPEEX_DEC (object);
switch (prop_id) {
case ARG_ENH:
speexdec->enh = g_value_get_boolean (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}