2013-01-16 11:36:25 +00:00
|
|
|
/* GStreamer SBC audio decoder
|
2007-08-23 19:12:23 +00:00
|
|
|
*
|
2010-01-02 01:08:17 +00:00
|
|
|
* Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
|
2013-03-26 13:55:32 +00:00
|
|
|
* Copyright (C) 2013 Tim-Philipp Müller <tim centricular net>
|
2007-08-23 19:12:23 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser 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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2013-03-26 13:55:32 +00:00
|
|
|
/**
|
|
|
|
* SECTION:element-sbdec
|
|
|
|
*
|
|
|
|
* This element decodes a Bluetooth SBC audio streams to raw integer PCM audio
|
|
|
|
*
|
|
|
|
* <refsect2>
|
|
|
|
* <title>Example pipelines</title>
|
|
|
|
* |[
|
|
|
|
* gst-launch-1.0 -v filesrc location=audio.sbc ! sbcparse ! sbcdec ! audioconvert ! audioresample ! autoaudiosink
|
|
|
|
* ]| Decode a raw SBC file.
|
|
|
|
* </refsect2>
|
|
|
|
*/
|
|
|
|
|
2007-08-23 19:12:23 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2007-08-25 17:03:03 +00:00
|
|
|
#include <string.h>
|
2007-08-23 19:12:23 +00:00
|
|
|
|
|
|
|
#include "gstsbcdec.h"
|
|
|
|
|
2013-01-16 11:36:25 +00:00
|
|
|
/* FIXME: where does this come from? how is it derived? */
|
2013-01-08 09:19:39 +00:00
|
|
|
#define BUF_SIZE 8192
|
|
|
|
|
2007-08-23 19:12:23 +00:00
|
|
|
GST_DEBUG_CATEGORY_STATIC (sbc_dec_debug);
|
|
|
|
#define GST_CAT_DEFAULT sbc_dec_debug
|
|
|
|
|
2013-01-16 11:36:25 +00:00
|
|
|
#define parent_class gst_sbc_dec_parent_class
|
2013-03-26 13:55:32 +00:00
|
|
|
G_DEFINE_TYPE (GstSbcDec, gst_sbc_dec, GST_TYPE_AUDIO_DECODER);
|
2007-08-23 19:12:23 +00:00
|
|
|
|
2007-08-25 17:03:03 +00:00
|
|
|
static GstStaticPadTemplate sbc_dec_sink_factory =
|
|
|
|
GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
|
2013-03-26 13:55:32 +00:00
|
|
|
GST_STATIC_CAPS ("audio/x-sbc, channels = (int) [ 1, 2 ], "
|
|
|
|
"rate = (int) { 16000, 32000, 44100, 48000 }, "
|
|
|
|
"parsed = (boolean) true"));
|
2007-08-25 17:03:03 +00:00
|
|
|
|
|
|
|
static GstStaticPadTemplate sbc_dec_src_factory =
|
|
|
|
GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
|
2013-01-16 11:36:25 +00:00
|
|
|
GST_STATIC_CAPS ("audio/x-raw, format=" GST_AUDIO_NE (S16) ", "
|
2007-08-25 17:37:05 +00:00
|
|
|
"rate = (int) { 16000, 32000, 44100, 48000 }, "
|
2013-01-16 11:36:25 +00:00
|
|
|
"channels = (int) [ 1, 2 ], layout=interleaved"));
|
2007-08-25 17:03:03 +00:00
|
|
|
|
2013-01-08 09:19:39 +00:00
|
|
|
static GstFlowReturn
|
2013-03-26 13:55:32 +00:00
|
|
|
gst_sbc_dec_handle_frame (GstAudioDecoder * audio_dec, GstBuffer * buf)
|
2007-08-25 17:03:03 +00:00
|
|
|
{
|
2013-03-26 13:55:32 +00:00
|
|
|
GstSbcDec *dec = GST_SBC_DEC (audio_dec);
|
|
|
|
GstBuffer *outbuf = NULL;
|
2013-01-16 11:36:25 +00:00
|
|
|
GstMapInfo out_map;
|
2013-03-26 13:55:32 +00:00
|
|
|
GstMapInfo in_map;
|
|
|
|
gsize output_size;
|
|
|
|
guint num_frames, i;
|
2007-08-25 17:03:03 +00:00
|
|
|
|
2013-03-26 13:55:32 +00:00
|
|
|
/* no fancy draining */
|
|
|
|
if (G_UNLIKELY (buf == NULL))
|
|
|
|
return GST_FLOW_OK;
|
2012-10-29 17:19:50 +00:00
|
|
|
|
2013-03-26 13:55:32 +00:00
|
|
|
gst_buffer_map (buf, &in_map, GST_MAP_READ);
|
2013-01-08 09:19:39 +00:00
|
|
|
|
2013-03-26 13:55:32 +00:00
|
|
|
if (G_UNLIKELY (in_map.size == 0))
|
|
|
|
goto done;
|
2007-08-25 17:03:03 +00:00
|
|
|
|
2013-03-26 13:55:32 +00:00
|
|
|
/* we assume all frames are of the same size, this is implied by the
|
|
|
|
* input caps applying to the whole input buffer, and the parser should
|
|
|
|
* also have made sure of that */
|
|
|
|
if (G_UNLIKELY (in_map.size % dec->frame_len != 0))
|
|
|
|
goto mixed_frames;
|
2012-10-29 17:19:50 +00:00
|
|
|
|
2013-03-26 13:55:32 +00:00
|
|
|
num_frames = in_map.size / dec->frame_len;
|
|
|
|
output_size = num_frames * dec->samples_per_frame * sizeof (gint16);
|
2013-01-08 09:19:39 +00:00
|
|
|
|
2013-03-26 13:55:32 +00:00
|
|
|
outbuf = gst_audio_decoder_allocate_output_buffer (audio_dec, output_size);
|
2013-01-16 11:36:25 +00:00
|
|
|
|
2013-03-26 13:55:32 +00:00
|
|
|
if (outbuf == NULL)
|
|
|
|
goto no_buffer;
|
2013-01-08 09:19:39 +00:00
|
|
|
|
2013-03-26 13:55:32 +00:00
|
|
|
gst_buffer_map (outbuf, &out_map, GST_MAP_WRITE);
|
2013-01-08 09:19:39 +00:00
|
|
|
|
2013-03-26 13:55:32 +00:00
|
|
|
for (i = 0; i < num_frames; ++i) {
|
|
|
|
gssize ret;
|
|
|
|
gsize written;
|
2013-01-08 09:19:39 +00:00
|
|
|
|
2013-03-26 13:55:32 +00:00
|
|
|
ret = sbc_decode (&dec->sbc, in_map.data + (i * dec->frame_len),
|
|
|
|
dec->frame_len, out_map.data + (i * dec->samples_per_frame * 2),
|
|
|
|
dec->samples_per_frame * 2, &written);
|
2013-01-08 09:19:39 +00:00
|
|
|
|
2013-03-26 13:55:32 +00:00
|
|
|
if (ret <= 0 || written != (dec->samples_per_frame * 2)) {
|
|
|
|
GST_WARNING_OBJECT (dec, "decoding error, ret = %" G_GSSIZE_FORMAT ", "
|
|
|
|
"written = %" G_GSSIZE_FORMAT, ret, written);
|
|
|
|
break;
|
2012-10-30 18:41:56 +00:00
|
|
|
}
|
2013-03-26 13:55:32 +00:00
|
|
|
}
|
2007-11-12 18:15:59 +00:00
|
|
|
|
2013-03-26 13:55:32 +00:00
|
|
|
gst_buffer_unmap (outbuf, &out_map);
|
2008-01-30 14:21:43 +00:00
|
|
|
|
2013-03-26 13:55:32 +00:00
|
|
|
if (i > 0)
|
|
|
|
gst_buffer_set_size (outbuf, i * dec->samples_per_frame * 2);
|
|
|
|
else
|
|
|
|
gst_buffer_replace (&outbuf, NULL);
|
2013-01-16 11:36:25 +00:00
|
|
|
|
2013-03-26 13:55:32 +00:00
|
|
|
done:
|
2008-01-30 14:21:43 +00:00
|
|
|
|
2013-03-26 13:55:32 +00:00
|
|
|
gst_buffer_unmap (buf, &in_map);
|
2012-10-29 17:22:28 +00:00
|
|
|
|
2013-03-26 13:55:32 +00:00
|
|
|
return gst_audio_decoder_finish_frame (audio_dec, outbuf, 1);
|
2007-08-25 17:03:03 +00:00
|
|
|
|
2013-03-26 13:55:32 +00:00
|
|
|
/* ERRORS */
|
|
|
|
mixed_frames:
|
|
|
|
{
|
|
|
|
GST_WARNING_OBJECT (dec, "inconsistent input data/frames, skipping");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
no_buffer:
|
|
|
|
{
|
|
|
|
GST_ERROR_OBJECT (dec, "could not allocate output buffer");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
2013-01-16 11:36:25 +00:00
|
|
|
|
2013-03-26 13:55:32 +00:00
|
|
|
static gboolean
|
|
|
|
gst_sbc_dec_set_format (GstAudioDecoder * audio_dec, GstCaps * caps)
|
|
|
|
{
|
|
|
|
GstSbcDec *dec = GST_SBC_DEC (audio_dec);
|
|
|
|
const gchar *channel_mode;
|
|
|
|
GstAudioInfo info;
|
|
|
|
GstStructure *s;
|
|
|
|
gint channels, rate, subbands, blocks, bitpool;
|
|
|
|
|
|
|
|
s = gst_caps_get_structure (caps, 0);
|
|
|
|
gst_structure_get_int (s, "channels", &channels);
|
|
|
|
gst_structure_get_int (s, "rate", &rate);
|
|
|
|
|
|
|
|
/* save input format */
|
|
|
|
channel_mode = gst_structure_get_string (s, "channel-mode");
|
|
|
|
if (channel_mode == NULL ||
|
|
|
|
!gst_structure_get_int (s, "subbands", &subbands) ||
|
|
|
|
!gst_structure_get_int (s, "blocks", &blocks) ||
|
|
|
|
!gst_structure_get_int (s, "bitpool", &bitpool))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (strcmp (channel_mode, "mono") == 0) {
|
2016-01-12 14:54:23 +00:00
|
|
|
dec->frame_len = 4 + (subbands * 1) / 2 + ((blocks * 1 * bitpool) + 7) / 8;
|
2013-03-26 13:55:32 +00:00
|
|
|
} else if (strcmp (channel_mode, "dual") == 0) {
|
2016-01-12 14:54:23 +00:00
|
|
|
dec->frame_len = 4 + (subbands * 2) / 2 + ((blocks * 2 * bitpool) + 7) / 8;
|
2013-03-26 13:55:32 +00:00
|
|
|
} else if (strcmp (channel_mode, "stereo") == 0) {
|
2016-01-12 14:54:23 +00:00
|
|
|
dec->frame_len = 4 + (subbands * 2) / 2 + ((blocks * bitpool) + 7) / 8;
|
2013-03-26 13:55:32 +00:00
|
|
|
} else if (strcmp (channel_mode, "joint") == 0) {
|
2016-03-04 06:50:26 +00:00
|
|
|
dec->frame_len =
|
|
|
|
4 + (subbands * 2) / 2 + ((subbands + blocks * bitpool) + 7) / 8;
|
2013-03-26 13:55:32 +00:00
|
|
|
} else {
|
|
|
|
return FALSE;
|
2013-01-16 11:36:25 +00:00
|
|
|
}
|
2007-08-25 17:03:03 +00:00
|
|
|
|
2013-03-26 13:55:32 +00:00
|
|
|
dec->samples_per_frame = channels * blocks * subbands;
|
2013-01-16 11:36:25 +00:00
|
|
|
|
2013-03-26 13:55:32 +00:00
|
|
|
GST_INFO_OBJECT (dec, "frame len: %" G_GSIZE_FORMAT ", samples per frame "
|
|
|
|
"%" G_GSIZE_FORMAT, dec->frame_len, dec->samples_per_frame);
|
2007-08-25 17:03:03 +00:00
|
|
|
|
2013-03-26 13:55:32 +00:00
|
|
|
/* set up output format */
|
|
|
|
gst_audio_info_init (&info);
|
|
|
|
gst_audio_info_set_format (&info, GST_AUDIO_FORMAT_S16, rate, channels, NULL);
|
|
|
|
gst_audio_decoder_set_output_format (audio_dec, &info);
|
|
|
|
|
|
|
|
return TRUE;
|
2007-08-25 17:03:03 +00:00
|
|
|
}
|
|
|
|
|
2013-03-26 13:55:32 +00:00
|
|
|
static gboolean
|
|
|
|
gst_sbc_dec_start (GstAudioDecoder * dec)
|
2007-08-25 17:03:03 +00:00
|
|
|
{
|
2013-03-26 13:55:32 +00:00
|
|
|
GstSbcDec *sbcdec = GST_SBC_DEC (dec);
|
2007-08-25 17:03:03 +00:00
|
|
|
|
2013-03-26 13:55:32 +00:00
|
|
|
GST_INFO_OBJECT (dec, "Setup subband codec");
|
|
|
|
sbc_init (&sbcdec->sbc, 0);
|
2012-10-29 12:37:09 +00:00
|
|
|
|
2013-03-26 13:55:32 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
2007-08-25 17:03:03 +00:00
|
|
|
|
2013-03-26 13:55:32 +00:00
|
|
|
static gboolean
|
|
|
|
gst_sbc_dec_stop (GstAudioDecoder * dec)
|
|
|
|
{
|
|
|
|
GstSbcDec *sbcdec = GST_SBC_DEC (dec);
|
2007-08-25 17:03:03 +00:00
|
|
|
|
2013-03-26 13:55:32 +00:00
|
|
|
GST_INFO_OBJECT (sbcdec, "Finish subband codec");
|
|
|
|
sbc_finish (&sbcdec->sbc);
|
|
|
|
sbcdec->samples_per_frame = 0;
|
|
|
|
sbcdec->frame_len = 0;
|
|
|
|
|
|
|
|
return TRUE;
|
2007-08-25 17:03:03 +00:00
|
|
|
}
|
|
|
|
|
2007-08-23 19:12:23 +00:00
|
|
|
static void
|
2013-01-16 11:36:25 +00:00
|
|
|
gst_sbc_dec_class_init (GstSbcDecClass * klass)
|
2007-08-23 19:12:23 +00:00
|
|
|
{
|
2013-03-26 13:55:32 +00:00
|
|
|
GstAudioDecoderClass *audio_decoder_class = (GstAudioDecoderClass *) klass;
|
|
|
|
GstElementClass *element_class = (GstElementClass *) klass;
|
2013-01-16 11:36:25 +00:00
|
|
|
|
2013-03-26 13:55:32 +00:00
|
|
|
audio_decoder_class->start = GST_DEBUG_FUNCPTR (gst_sbc_dec_start);
|
|
|
|
audio_decoder_class->stop = GST_DEBUG_FUNCPTR (gst_sbc_dec_stop);
|
|
|
|
audio_decoder_class->set_format = GST_DEBUG_FUNCPTR (gst_sbc_dec_set_format);
|
|
|
|
audio_decoder_class->handle_frame =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_sbc_dec_handle_frame);
|
2007-08-23 19:12:23 +00:00
|
|
|
|
2016-03-04 06:50:26 +00:00
|
|
|
gst_element_class_add_static_pad_template (element_class,
|
|
|
|
&sbc_dec_sink_factory);
|
|
|
|
gst_element_class_add_static_pad_template (element_class,
|
|
|
|
&sbc_dec_src_factory);
|
2007-08-25 17:03:03 +00:00
|
|
|
|
2013-01-16 11:36:25 +00:00
|
|
|
gst_element_class_set_static_metadata (element_class,
|
|
|
|
"Bluetooth SBC audio decoder", "Codec/Decoder/Audio",
|
|
|
|
"Decode an SBC audio stream", "Marcel Holtmann <marcel@holtmann.org>");
|
2007-08-25 17:03:03 +00:00
|
|
|
|
2007-08-23 19:12:23 +00:00
|
|
|
GST_DEBUG_CATEGORY_INIT (sbc_dec_debug, "sbcdec", 0, "SBC decoding element");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-03-26 13:55:32 +00:00
|
|
|
gst_sbc_dec_init (GstSbcDec * dec)
|
2013-01-08 09:19:39 +00:00
|
|
|
{
|
2013-12-05 11:05:30 +00:00
|
|
|
gst_audio_decoder_set_needs_format (GST_AUDIO_DECODER (dec), TRUE);
|
2015-08-15 15:58:40 +00:00
|
|
|
gst_audio_decoder_set_use_default_pad_acceptcaps (GST_AUDIO_DECODER_CAST
|
|
|
|
(dec), TRUE);
|
|
|
|
GST_PAD_SET_ACCEPT_TEMPLATE (GST_AUDIO_DECODER_SINK_PAD (dec));
|
2013-12-05 11:05:30 +00:00
|
|
|
|
2013-03-26 13:55:32 +00:00
|
|
|
dec->samples_per_frame = 0;
|
|
|
|
dec->frame_len = 0;
|
2008-01-23 13:14:02 +00:00
|
|
|
}
|