2007-08-24 03:12:23 +08:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* BlueZ - Bluetooth protocol stack for Linux
|
|
|
|
*
|
2008-02-02 11:37:05 +08:00
|
|
|
* Copyright (C) 2004-2008 Marcel Holtmann <marcel@holtmann.org>
|
2007-08-24 03:12:23 +08: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-26 01:03:03 +08:00
|
|
|
#include <string.h>
|
2007-08-24 03:12:23 +08:00
|
|
|
|
|
|
|
#include "gstsbcdec.h"
|
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_STATIC(sbc_dec_debug);
|
|
|
|
#define GST_CAT_DEFAULT sbc_dec_debug
|
|
|
|
|
|
|
|
GST_BOILERPLATE(GstSbcDec, gst_sbc_dec, GstElement, GST_TYPE_ELEMENT);
|
|
|
|
|
|
|
|
static const GstElementDetails sbc_dec_details =
|
|
|
|
GST_ELEMENT_DETAILS("Bluetooth SBC decoder",
|
|
|
|
"Codec/Decoder/Audio",
|
|
|
|
"Decode a SBC audio stream",
|
|
|
|
"Marcel Holtmann <marcel@holtmann.org>");
|
|
|
|
|
2007-08-26 01:03:03 +08: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,
|
|
|
|
GST_STATIC_CAPS("audio/x-raw-int, "
|
2007-08-26 01:37:05 +08:00
|
|
|
"rate = (int) { 16000, 32000, 44100, 48000 }, "
|
2007-08-26 01:03:03 +08:00
|
|
|
"channels = (int) [ 1, 2 ], "
|
2007-08-27 22:10:00 +08:00
|
|
|
"endianness = (int) LITTLE_ENDIAN, "
|
2007-08-26 01:03:03 +08:00
|
|
|
"signed = (boolean) true, "
|
|
|
|
"width = (int) 16, "
|
|
|
|
"depth = (int) 16"));
|
|
|
|
|
|
|
|
static GstFlowReturn sbc_dec_chain(GstPad *pad, GstBuffer *buffer)
|
|
|
|
{
|
|
|
|
GstSbcDec *dec = GST_SBC_DEC(gst_pad_get_parent(pad));
|
|
|
|
GstFlowReturn res = GST_FLOW_OK;
|
2007-11-13 02:15:59 +08:00
|
|
|
guint size, codesize, offset = 0;
|
2007-08-26 01:03:03 +08:00
|
|
|
guint8 *data;
|
|
|
|
GstClockTime timestamp;
|
|
|
|
|
2007-11-13 02:15:59 +08:00
|
|
|
codesize = sbc_get_codesize(&dec->sbc);
|
2007-08-26 01:03:03 +08:00
|
|
|
timestamp = GST_BUFFER_TIMESTAMP(buffer);
|
|
|
|
|
|
|
|
if (dec->buffer) {
|
|
|
|
GstBuffer *temp = buffer;
|
|
|
|
buffer = gst_buffer_span(dec->buffer, 0, buffer,
|
|
|
|
GST_BUFFER_SIZE(dec->buffer) + GST_BUFFER_SIZE(buffer));
|
|
|
|
gst_buffer_unref(temp);
|
|
|
|
gst_buffer_unref(dec->buffer);
|
|
|
|
dec->buffer = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
data = GST_BUFFER_DATA(buffer);
|
|
|
|
size = GST_BUFFER_SIZE(buffer);
|
|
|
|
|
|
|
|
while (offset < size) {
|
|
|
|
GstBuffer *output;
|
|
|
|
GstPadTemplate *template;
|
2008-01-30 22:21:43 +08:00
|
|
|
GstCaps *caps;
|
2007-08-26 01:03:03 +08:00
|
|
|
int consumed;
|
|
|
|
|
|
|
|
res = gst_pad_alloc_buffer_and_set_caps(dec->srcpad,
|
|
|
|
GST_BUFFER_OFFSET_NONE,
|
2008-01-30 22:21:43 +08:00
|
|
|
codesize, NULL, &output);
|
2007-08-26 01:03:03 +08:00
|
|
|
|
|
|
|
if (res != GST_FLOW_OK)
|
|
|
|
goto done;
|
|
|
|
|
2007-11-13 02:15:59 +08:00
|
|
|
consumed = sbc_decode(&dec->sbc, data + offset, size - offset,
|
|
|
|
GST_BUFFER_DATA(output), codesize,
|
|
|
|
NULL);
|
|
|
|
if (consumed <= 0)
|
|
|
|
break;
|
|
|
|
|
2008-01-30 22:21:43 +08:00
|
|
|
/* we will reuse the same caps object */
|
|
|
|
if (dec->outcaps == NULL) {
|
|
|
|
caps = gst_caps_new_simple("audio/x-raw-int",
|
2008-02-20 03:49:24 +08:00
|
|
|
"rate", G_TYPE_INT,
|
|
|
|
gst_sbc_parse_rate_from_sbc(
|
|
|
|
dec->sbc.frequency),
|
|
|
|
"channels", G_TYPE_INT,
|
|
|
|
gst_sbc_get_channel_number(
|
|
|
|
dec->sbc.mode),
|
2008-01-30 22:21:43 +08:00
|
|
|
NULL);
|
|
|
|
|
|
|
|
template = gst_static_pad_template_get(&sbc_dec_src_factory);
|
|
|
|
|
|
|
|
dec->outcaps = gst_caps_intersect(caps,
|
|
|
|
gst_pad_template_get_caps(template));
|
|
|
|
|
|
|
|
gst_caps_unref(caps);
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_buffer_set_caps(output, dec->outcaps);
|
|
|
|
|
|
|
|
/* FIXME get a real timestamp */
|
|
|
|
GST_BUFFER_TIMESTAMP(output) = GST_CLOCK_TIME_NONE;
|
2007-08-26 01:03:03 +08:00
|
|
|
|
|
|
|
res = gst_pad_push(dec->srcpad, output);
|
|
|
|
if (res != GST_FLOW_OK)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
offset += consumed;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (offset < size)
|
|
|
|
dec->buffer = gst_buffer_create_sub(buffer,
|
|
|
|
offset, size - offset);
|
|
|
|
|
|
|
|
done:
|
|
|
|
gst_buffer_unref(buffer);
|
|
|
|
gst_object_unref(dec);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstStateChangeReturn sbc_dec_change_state(GstElement *element,
|
|
|
|
GstStateChange transition)
|
|
|
|
{
|
|
|
|
GstSbcDec *dec = GST_SBC_DEC(element);
|
|
|
|
|
|
|
|
switch (transition) {
|
|
|
|
case GST_STATE_CHANGE_READY_TO_PAUSED:
|
|
|
|
GST_DEBUG("Setup subband codec");
|
|
|
|
if (dec->buffer) {
|
|
|
|
gst_buffer_unref(dec->buffer);
|
|
|
|
dec->buffer = NULL;
|
|
|
|
}
|
|
|
|
sbc_init(&dec->sbc, 0);
|
2008-01-30 22:21:43 +08:00
|
|
|
dec->outcaps = NULL;
|
2007-08-26 01:03:03 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GST_STATE_CHANGE_PAUSED_TO_READY:
|
|
|
|
GST_DEBUG("Finish subband codec");
|
|
|
|
if (dec->buffer) {
|
|
|
|
gst_buffer_unref(dec->buffer);
|
|
|
|
dec->buffer = NULL;
|
|
|
|
}
|
|
|
|
sbc_finish(&dec->sbc);
|
2008-01-30 22:21:43 +08:00
|
|
|
if (dec->outcaps) {
|
|
|
|
gst_caps_unref(dec->outcaps);
|
|
|
|
dec->outcaps = NULL;
|
|
|
|
}
|
2007-08-26 01:03:03 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent_class->change_state(element, transition);
|
|
|
|
}
|
|
|
|
|
2007-08-24 03:12:23 +08:00
|
|
|
static void gst_sbc_dec_base_init(gpointer g_class)
|
|
|
|
{
|
|
|
|
GstElementClass *element_class = GST_ELEMENT_CLASS(g_class);
|
|
|
|
|
2007-08-26 01:03:03 +08: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));
|
|
|
|
|
2007-08-24 03:12:23 +08:00
|
|
|
gst_element_class_set_details(element_class, &sbc_dec_details);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gst_sbc_dec_class_init(GstSbcDecClass *klass)
|
|
|
|
{
|
2007-08-26 02:50:28 +08:00
|
|
|
GstElementClass *element_class = GST_ELEMENT_CLASS(klass);
|
2007-08-26 01:03:03 +08:00
|
|
|
|
2007-08-24 03:12:23 +08:00
|
|
|
parent_class = g_type_class_peek_parent(klass);
|
|
|
|
|
2007-08-26 02:50:28 +08:00
|
|
|
element_class->change_state = GST_DEBUG_FUNCPTR(sbc_dec_change_state);
|
2007-08-26 01:03:03 +08:00
|
|
|
|
2007-08-24 03:12:23 +08:00
|
|
|
GST_DEBUG_CATEGORY_INIT(sbc_dec_debug, "sbcdec", 0,
|
|
|
|
"SBC decoding element");
|
|
|
|
}
|
|
|
|
|
2007-08-26 01:03:03 +08:00
|
|
|
static void gst_sbc_dec_init(GstSbcDec *self, GstSbcDecClass *klass)
|
2007-08-24 03:12:23 +08:00
|
|
|
{
|
2008-01-23 21:14:02 +08: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));
|
2007-08-26 01:03:03 +08:00
|
|
|
gst_element_add_pad(GST_ELEMENT(self), self->sinkpad);
|
|
|
|
|
2008-01-23 21:14:02 +08:00
|
|
|
self->srcpad = gst_pad_new_from_static_template(
|
|
|
|
&sbc_dec_src_factory, "src");
|
2007-08-26 01:03:03 +08:00
|
|
|
gst_element_add_pad(GST_ELEMENT(self), self->srcpad);
|
2008-01-30 22:21:43 +08:00
|
|
|
|
|
|
|
self->outcaps = NULL;
|
2007-08-24 03:12:23 +08:00
|
|
|
}
|
2008-01-23 21:14:02 +08:00
|
|
|
|
|
|
|
gboolean gst_sbc_dec_plugin_init (GstPlugin * plugin)
|
|
|
|
{
|
|
|
|
return gst_element_register (plugin, "sbcdec",
|
|
|
|
GST_RANK_PRIMARY, GST_TYPE_SBC_DEC);
|
|
|
|
}
|
|
|
|
|
|
|
|
|