2013-01-16 11:36:25 +00:00
|
|
|
/* GStreamer SBC audio decoder
|
2007-08-23 19:12:23 +00:00
|
|
|
* BlueZ - Bluetooth protocol stack for Linux
|
|
|
|
*
|
2010-01-02 01:08:17 +00:00
|
|
|
* Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
|
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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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
|
|
|
|
2008-12-23 04:25:50 +00:00
|
|
|
#include "gstsbcutil.h"
|
2007-08-23 19:12:23 +00:00
|
|
|
#include "gstsbcdec.h"
|
2013-01-16 11:36:25 +00:00
|
|
|
#include <gst/audio/audio.h>
|
2007-08-23 19:12:23 +00:00
|
|
|
|
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-08 09:19:39 +00:00
|
|
|
static void gst_sbc_dec_finalize (GObject * obj);
|
|
|
|
|
2013-01-16 11:36:25 +00:00
|
|
|
/* FIXME: port to GstAudioDecoder base class */
|
|
|
|
#define parent_class gst_sbc_dec_parent_class
|
|
|
|
G_DEFINE_TYPE (GstSbcDec, gst_sbc_dec, GST_TYPE_ELEMENT);
|
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,
|
|
|
|
GST_STATIC_CAPS ("audio/x-sbc"));
|
|
|
|
|
|
|
|
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
|
|
|
|
gst_sbc_dec_flush (GstSbcDec * dec, GstBuffer * outbuf,
|
|
|
|
gint outoffset, gint channels, gint rate)
|
|
|
|
{
|
|
|
|
GstClockTime outtime, duration;
|
|
|
|
|
|
|
|
/* we will reuse the same caps object */
|
2013-01-16 11:36:25 +00:00
|
|
|
if (dec->send_caps) {
|
2013-01-08 09:19:39 +00:00
|
|
|
GstCaps *caps;
|
|
|
|
|
2013-01-16 11:36:25 +00:00
|
|
|
caps = gst_caps_new_simple ("audio/x-raw",
|
|
|
|
"format", G_TYPE_STRING, GST_AUDIO_NE (S16),
|
|
|
|
"rate", G_TYPE_INT, rate, "channels", G_TYPE_INT, channels,
|
|
|
|
"layout", G_TYPE_STRING, "interleaved", NULL);
|
2013-01-08 09:19:39 +00:00
|
|
|
|
2013-01-16 11:36:25 +00:00
|
|
|
gst_pad_push_event (dec->srcpad, gst_event_new_caps (caps));
|
2013-01-08 09:19:39 +00:00
|
|
|
|
|
|
|
gst_caps_unref (caps);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* calculate duration */
|
|
|
|
outtime = GST_BUFFER_TIMESTAMP (outbuf);
|
|
|
|
if (dec->next_timestamp != (guint64) - 1 && outtime != (guint64) - 1) {
|
|
|
|
duration = dec->next_timestamp - outtime;
|
|
|
|
} else if (outtime != (guint64) - 1) {
|
|
|
|
/* otherwise calculate duration based on outbuf size */
|
|
|
|
duration = gst_util_uint64_scale_int (outoffset / (2 * channels),
|
|
|
|
GST_SECOND, rate) - outtime;
|
|
|
|
} else {
|
|
|
|
duration = GST_CLOCK_TIME_NONE;
|
|
|
|
}
|
|
|
|
GST_BUFFER_DURATION (outbuf) = duration;
|
2013-01-16 11:36:25 +00:00
|
|
|
gst_buffer_resize (outbuf, 0, outoffset);
|
2013-01-08 09:19:39 +00:00
|
|
|
|
|
|
|
return gst_pad_push (dec->srcpad, outbuf);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-08-25 17:03:03 +00:00
|
|
|
static GstFlowReturn
|
2013-01-16 11:36:25 +00:00
|
|
|
sbc_dec_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
|
2007-08-25 17:03:03 +00:00
|
|
|
{
|
2013-01-16 11:36:25 +00:00
|
|
|
GstSbcDec *dec = GST_SBC_DEC (parent);
|
2007-08-25 17:03:03 +00:00
|
|
|
GstFlowReturn res = GST_FLOW_OK;
|
2013-01-08 09:19:39 +00:00
|
|
|
const guint8 *indata;
|
|
|
|
guint insize;
|
2012-10-29 17:19:50 +00:00
|
|
|
GstClockTime timestamp;
|
2012-10-29 17:22:28 +00:00
|
|
|
gboolean discont;
|
2013-01-16 11:36:25 +00:00
|
|
|
GstMapInfo out_map;
|
2013-01-08 09:19:39 +00:00
|
|
|
GstBuffer *outbuf;
|
|
|
|
guint inoffset, outoffset;
|
|
|
|
gint rate, channels;
|
2007-08-25 17:03:03 +00:00
|
|
|
|
2012-10-29 17:22:28 +00:00
|
|
|
discont = GST_BUFFER_IS_DISCONT (buffer);
|
|
|
|
if (discont) {
|
2012-10-29 17:19:50 +00:00
|
|
|
/* reset previous buffer */
|
2013-01-08 09:19:39 +00:00
|
|
|
gst_adapter_clear (dec->adapter);
|
2012-10-29 17:19:50 +00:00
|
|
|
/* we need a new timestamp to lock onto */
|
|
|
|
dec->next_sample = -1;
|
|
|
|
}
|
|
|
|
|
2013-01-08 09:19:39 +00:00
|
|
|
gst_adapter_push (dec->adapter, buffer);
|
2007-08-25 17:03:03 +00:00
|
|
|
|
2012-10-29 17:19:50 +00:00
|
|
|
timestamp = GST_BUFFER_TIMESTAMP (buffer);
|
2013-01-08 09:19:39 +00:00
|
|
|
if (GST_CLOCK_TIME_IS_VALID (timestamp))
|
|
|
|
dec->next_timestamp = timestamp;
|
2012-10-29 17:19:50 +00:00
|
|
|
|
2013-01-08 09:19:39 +00:00
|
|
|
insize = gst_adapter_available (dec->adapter);
|
2013-01-16 11:36:25 +00:00
|
|
|
indata = gst_adapter_map (dec->adapter, insize);
|
2007-08-25 17:03:03 +00:00
|
|
|
|
2013-01-08 09:19:39 +00:00
|
|
|
inoffset = 0;
|
|
|
|
outbuf = NULL;
|
|
|
|
channels = rate = 0;
|
|
|
|
|
|
|
|
while (insize > 0) {
|
|
|
|
gint inconsumed, outlen;
|
|
|
|
gint outsize;
|
|
|
|
size_t outconsumed;
|
2007-08-25 17:03:03 +00:00
|
|
|
|
2013-01-08 09:19:39 +00:00
|
|
|
if (outbuf == NULL) {
|
2013-01-16 11:36:25 +00:00
|
|
|
outbuf = gst_buffer_new_and_alloc (BUF_SIZE);
|
2012-10-29 17:19:50 +00:00
|
|
|
|
2013-01-08 09:19:39 +00:00
|
|
|
if (discont) {
|
|
|
|
GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
|
|
|
|
discont = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_BUFFER_TIMESTAMP (outbuf) = dec->next_timestamp;
|
2013-01-16 11:36:25 +00:00
|
|
|
|
|
|
|
gst_buffer_map (outbuf, &out_map, GST_MAP_WRITE);
|
|
|
|
outsize = out_map.size;
|
2013-01-08 09:19:39 +00:00
|
|
|
outoffset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_INFO_OBJECT (dec, "inoffset %d/%d, outoffset %d/%d", inoffset,
|
|
|
|
insize, outoffset, outsize);
|
|
|
|
|
|
|
|
inconsumed = sbc_decode (&dec->sbc, indata + inoffset, insize,
|
2013-01-16 11:36:25 +00:00
|
|
|
out_map.data + outoffset, outsize, &outconsumed);
|
2013-01-08 09:19:39 +00:00
|
|
|
|
|
|
|
GST_INFO_OBJECT (dec, "consumed %d, produced %d", inconsumed, outconsumed);
|
|
|
|
|
|
|
|
if (inconsumed <= 0) {
|
|
|
|
guint frame_len = sbc_get_frame_length (&dec->sbc);
|
|
|
|
/* skip a frame */
|
|
|
|
if (insize > frame_len) {
|
|
|
|
insize -= frame_len;
|
|
|
|
inoffset += frame_len;
|
|
|
|
} else {
|
|
|
|
insize = 0;
|
|
|
|
}
|
2012-10-30 18:41:56 +00:00
|
|
|
continue;
|
|
|
|
}
|
2007-11-12 18:15:59 +00:00
|
|
|
|
2013-01-08 09:19:39 +00:00
|
|
|
inoffset += inconsumed;
|
|
|
|
if ((gint) insize > inconsumed)
|
|
|
|
insize -= inconsumed;
|
|
|
|
else
|
|
|
|
insize = 0;
|
|
|
|
outoffset += outconsumed;
|
|
|
|
outsize -= outconsumed;
|
|
|
|
|
2012-10-29 17:19:50 +00:00
|
|
|
rate = gst_sbc_parse_rate_from_sbc (dec->sbc.frequency);
|
|
|
|
channels = gst_sbc_get_channel_number (dec->sbc.mode);
|
|
|
|
|
2013-01-08 09:19:39 +00:00
|
|
|
/* calculate timestamp either from the incomming buffers or
|
|
|
|
* from our sample counter */
|
2012-10-29 17:19:50 +00:00
|
|
|
if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
|
|
|
|
/* lock onto timestamp when we have one */
|
|
|
|
dec->next_sample = gst_util_uint64_scale_int (timestamp,
|
|
|
|
rate, GST_SECOND);
|
2013-01-08 09:19:39 +00:00
|
|
|
timestamp = GST_CLOCK_TIME_NONE;
|
2012-10-29 17:19:50 +00:00
|
|
|
}
|
|
|
|
if (dec->next_sample != (guint64) - 1) {
|
2013-01-08 09:19:39 +00:00
|
|
|
/* calculate the next sample */
|
|
|
|
dec->next_sample += outconsumed / (2 * channels);
|
|
|
|
dec->next_timestamp = gst_util_uint64_scale_int (dec->next_sample,
|
2012-10-29 17:19:50 +00:00
|
|
|
GST_SECOND, rate);
|
|
|
|
}
|
2008-01-30 14:21:43 +00:00
|
|
|
|
2013-01-08 09:19:39 +00:00
|
|
|
/* check for space, push outbuf buffer */
|
|
|
|
outlen = sbc_get_codesize (&dec->sbc);
|
|
|
|
if (outsize < outlen) {
|
2013-01-16 11:36:25 +00:00
|
|
|
gst_buffer_unmap (outbuf, &out_map);
|
|
|
|
|
2013-01-08 09:19:39 +00:00
|
|
|
res = gst_sbc_dec_flush (dec, outbuf, outoffset, channels, rate);
|
2008-01-30 14:21:43 +00:00
|
|
|
|
2013-01-08 09:19:39 +00:00
|
|
|
outbuf = NULL;
|
2012-10-29 17:22:28 +00:00
|
|
|
|
2013-01-16 11:36:25 +00:00
|
|
|
if (res != GST_FLOW_OK)
|
|
|
|
goto done;
|
|
|
|
}
|
2007-08-25 17:03:03 +00:00
|
|
|
}
|
|
|
|
|
2013-01-16 11:36:25 +00:00
|
|
|
if (outbuf) {
|
|
|
|
gst_buffer_unmap (outbuf, &out_map);
|
|
|
|
|
2013-01-08 09:19:39 +00:00
|
|
|
res = gst_sbc_dec_flush (dec, outbuf, outoffset, channels, rate);
|
2013-01-16 11:36:25 +00:00
|
|
|
}
|
2007-08-25 17:03:03 +00:00
|
|
|
|
|
|
|
done:
|
2013-01-16 11:36:25 +00:00
|
|
|
|
|
|
|
gst_adapter_unmap (dec->adapter);
|
|
|
|
gst_adapter_flush (dec->adapter, inoffset);
|
2007-08-25 17:03:03 +00:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstStateChangeReturn
|
2013-01-08 09:19:39 +00:00
|
|
|
gst_sbc_dec_change_state (GstElement * element, GstStateChange transition)
|
2007-08-25 17:03:03 +00:00
|
|
|
{
|
2012-10-29 12:37:09 +00:00
|
|
|
GstStateChangeReturn result;
|
2007-08-25 17:03:03 +00:00
|
|
|
GstSbcDec *dec = GST_SBC_DEC (element);
|
|
|
|
|
|
|
|
switch (transition) {
|
|
|
|
case GST_STATE_CHANGE_READY_TO_PAUSED:
|
|
|
|
GST_DEBUG ("Setup subband codec");
|
|
|
|
sbc_init (&dec->sbc, 0);
|
2013-01-16 11:36:25 +00:00
|
|
|
dec->send_caps = TRUE;
|
2012-10-29 17:19:50 +00:00
|
|
|
dec->next_sample = -1;
|
2007-08-25 17:03:03 +00:00
|
|
|
break;
|
2012-10-29 12:37:09 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2007-08-25 17:03:03 +00:00
|
|
|
|
2013-01-16 11:36:25 +00:00
|
|
|
result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
|
2012-10-29 12:37:09 +00:00
|
|
|
|
|
|
|
switch (transition) {
|
2007-08-25 17:03:03 +00:00
|
|
|
case GST_STATE_CHANGE_PAUSED_TO_READY:
|
|
|
|
GST_DEBUG ("Finish subband codec");
|
2013-01-08 09:19:39 +00:00
|
|
|
gst_adapter_clear (dec->adapter);
|
2007-08-25 17:03:03 +00:00
|
|
|
sbc_finish (&dec->sbc);
|
2013-01-16 11:36:25 +00:00
|
|
|
dec->send_caps = TRUE;
|
2007-08-25 17:03:03 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-10-29 12:37:09 +00:00
|
|
|
return result;
|
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-01-16 11:36:25 +00:00
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
|
|
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
|
|
|
|
|
|
|
|
object_class->finalize = gst_sbc_dec_finalize;
|
|
|
|
|
|
|
|
element_class->change_state = GST_DEBUG_FUNCPTR (gst_sbc_dec_change_state);
|
2007-08-23 19:12:23 +00:00
|
|
|
|
2007-08-25 17:03:03 +00:00
|
|
|
gst_element_class_add_pad_template (element_class,
|
|
|
|
gst_static_pad_template_get (&sbc_dec_sink_factory));
|
|
|
|
|
|
|
|
gst_element_class_add_pad_template (element_class,
|
|
|
|
gst_static_pad_template_get (&sbc_dec_src_factory));
|
|
|
|
|
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-01-16 11:36:25 +00:00
|
|
|
gst_sbc_dec_init (GstSbcDec * self)
|
2007-08-23 19:12:23 +00:00
|
|
|
{
|
2007-08-25 17:03:03 +00:00
|
|
|
self->sinkpad =
|
|
|
|
gst_pad_new_from_static_template (&sbc_dec_sink_factory, "sink");
|
|
|
|
gst_pad_set_chain_function (self->sinkpad, GST_DEBUG_FUNCPTR (sbc_dec_chain));
|
|
|
|
gst_element_add_pad (GST_ELEMENT (self), self->sinkpad);
|
|
|
|
|
|
|
|
self->srcpad = gst_pad_new_from_static_template (&sbc_dec_src_factory, "src");
|
|
|
|
gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
|
2008-01-30 14:21:43 +00:00
|
|
|
|
2013-01-08 09:19:39 +00:00
|
|
|
self->adapter = gst_adapter_new ();
|
2013-01-16 11:36:25 +00:00
|
|
|
self->send_caps = TRUE;
|
2007-08-23 19:12:23 +00:00
|
|
|
}
|
2008-01-23 13:14:02 +00:00
|
|
|
|
2013-01-08 09:19:39 +00:00
|
|
|
static void
|
|
|
|
gst_sbc_dec_finalize (GObject * obj)
|
|
|
|
{
|
|
|
|
GstSbcDec *self = GST_SBC_DEC (obj);
|
|
|
|
|
|
|
|
g_object_unref (self->adapter);
|
2013-01-16 11:36:25 +00:00
|
|
|
self->adapter = NULL;
|
2013-01-08 09:19:39 +00:00
|
|
|
|
|
|
|
G_OBJECT_CLASS (parent_class)->finalize (obj);
|
2008-01-23 13:14:02 +00:00
|
|
|
}
|