Merge SBC decoder and encoder from bluez

https://bugzilla.gnome.org/show_bug.cgi?id=690582
This commit is contained in:
Tim-Philipp Müller 2013-03-27 22:24:03 +00:00
commit 89bbcdea39
6 changed files with 1688 additions and 0 deletions

328
ext/sbc/gstsbcdec.c Normal file
View file

@ -0,0 +1,328 @@
/*
*
* BlueZ - Bluetooth protocol stack for Linux
*
* Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
*
*
* 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
#include <string.h>
#include "gstpragma.h"
#include "gstsbcutil.h"
#include "gstsbcdec.h"
#define BUF_SIZE 8192
GST_DEBUG_CATEGORY_STATIC (sbc_dec_debug);
#define GST_CAT_DEFAULT sbc_dec_debug
static void gst_sbc_dec_finalize (GObject * obj);
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>");
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, "
"rate = (int) { 16000, 32000, 44100, 48000 }, "
"channels = (int) [ 1, 2 ], "
"endianness = (int) BYTE_ORDER, "
"signed = (boolean) true, " "width = (int) 16, " "depth = (int) 16"));
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 */
if (dec->outcaps == NULL) {
GstCaps *caps;
GstPadTemplate *template;
caps = gst_caps_new_simple ("audio/x-raw-int",
"rate", G_TYPE_INT, rate, "channels", G_TYPE_INT, channels, 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_object_unref (template);
}
gst_buffer_set_caps (outbuf, dec->outcaps);
/* 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;
GST_BUFFER_SIZE (outbuf) = outoffset;
return gst_pad_push (dec->srcpad, outbuf);
}
static GstFlowReturn
sbc_dec_chain (GstPad * pad, GstBuffer * buffer)
{
GstSbcDec *dec = GST_SBC_DEC (gst_pad_get_parent (pad));
GstFlowReturn res = GST_FLOW_OK;
const guint8 *indata;
guint insize;
GstClockTime timestamp;
gboolean discont;
GstBuffer *outbuf;
guint8 *outdata;
guint inoffset, outoffset;
gint rate, channels;
discont = GST_BUFFER_IS_DISCONT (buffer);
if (discont) {
/* reset previous buffer */
gst_adapter_clear (dec->adapter);
/* we need a new timestamp to lock onto */
dec->next_sample = -1;
}
gst_adapter_push (dec->adapter, buffer);
timestamp = GST_BUFFER_TIMESTAMP (buffer);
if (GST_CLOCK_TIME_IS_VALID (timestamp))
dec->next_timestamp = timestamp;
insize = gst_adapter_available (dec->adapter);
indata = gst_adapter_peek (dec->adapter, insize);
inoffset = 0;
outbuf = NULL;
channels = rate = 0;
while (insize > 0) {
gint inconsumed, outlen;
gint outsize;
size_t outconsumed;
if (outbuf == NULL) {
res = gst_pad_alloc_buffer_and_set_caps (dec->srcpad,
GST_BUFFER_OFFSET_NONE, BUF_SIZE, NULL, &outbuf);
if (res != GST_FLOW_OK)
goto done;
if (discont) {
GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
discont = FALSE;
}
GST_BUFFER_TIMESTAMP (outbuf) = dec->next_timestamp;
outdata = GST_BUFFER_DATA (outbuf);
outsize = GST_BUFFER_SIZE (outbuf);
outoffset = 0;
}
GST_INFO_OBJECT (dec, "inoffset %d/%d, outoffset %d/%d", inoffset,
insize, outoffset, outsize);
inconsumed = sbc_decode (&dec->sbc, indata + inoffset, insize,
outdata + outoffset, outsize, &outconsumed);
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;
}
continue;
}
inoffset += inconsumed;
if ((gint) insize > inconsumed)
insize -= inconsumed;
else
insize = 0;
outoffset += outconsumed;
outsize -= outconsumed;
rate = gst_sbc_parse_rate_from_sbc (dec->sbc.frequency);
channels = gst_sbc_get_channel_number (dec->sbc.mode);
/* calculate timestamp either from the incomming buffers or
* from our sample counter */
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);
timestamp = GST_CLOCK_TIME_NONE;
}
if (dec->next_sample != (guint64) - 1) {
/* calculate the next sample */
dec->next_sample += outconsumed / (2 * channels);
dec->next_timestamp = gst_util_uint64_scale_int (dec->next_sample,
GST_SECOND, rate);
}
/* check for space, push outbuf buffer */
outlen = sbc_get_codesize (&dec->sbc);
if (outsize < outlen) {
res = gst_sbc_dec_flush (dec, outbuf, outoffset, channels, rate);
if (res != GST_FLOW_OK)
goto done;
outbuf = NULL;
}
}
if (outbuf)
res = gst_sbc_dec_flush (dec, outbuf, outoffset, channels, rate);
gst_adapter_flush (dec->adapter, inoffset);
done:
gst_object_unref (dec);
return res;
}
static GstStateChangeReturn
gst_sbc_dec_change_state (GstElement * element, GstStateChange transition)
{
GstStateChangeReturn result;
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);
dec->outcaps = NULL;
dec->next_sample = -1;
break;
default:
break;
}
result = parent_class->change_state (element, transition);
switch (transition) {
case GST_STATE_CHANGE_PAUSED_TO_READY:
GST_DEBUG ("Finish subband codec");
gst_adapter_clear (dec->adapter);
sbc_finish (&dec->sbc);
if (dec->outcaps) {
gst_caps_unref (dec->outcaps);
dec->outcaps = NULL;
}
break;
default:
break;
}
return result;
}
static void
gst_sbc_dec_base_init (gpointer g_class)
{
GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
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));
gst_element_class_set_details (element_class, &sbc_dec_details);
}
static void
gst_sbc_dec_class_init (GstSbcDecClass * klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
parent_class = g_type_class_peek_parent (klass);
object_class->finalize = GST_DEBUG_FUNCPTR (gst_sbc_dec_finalize);
element_class->change_state = GST_DEBUG_FUNCPTR (gst_sbc_dec_change_state);
GST_DEBUG_CATEGORY_INIT (sbc_dec_debug, "sbcdec", 0, "SBC decoding element");
}
static void
gst_sbc_dec_init (GstSbcDec * self, GstSbcDecClass * klass)
{
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);
self->adapter = gst_adapter_new ();
self->outcaps = NULL;
}
static void
gst_sbc_dec_finalize (GObject * obj)
{
GstSbcDec *self = GST_SBC_DEC (obj);
g_object_unref (self->adapter);
G_OBJECT_CLASS (parent_class)->finalize (obj);
}
gboolean
gst_sbc_dec_plugin_init (GstPlugin * plugin)
{
return gst_element_register (plugin, "sbcdec", GST_RANK_PRIMARY,
GST_TYPE_SBC_DEC);
}

69
ext/sbc/gstsbcdec.h Normal file
View file

@ -0,0 +1,69 @@
/*
*
* BlueZ - Bluetooth protocol stack for Linux
*
* Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
*
*
* 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
*
*/
#include <gst/gst.h>
#include <gst/base/gstadapter.h>
#include "sbc.h"
G_BEGIN_DECLS
#define GST_TYPE_SBC_DEC \
(gst_sbc_dec_get_type())
#define GST_SBC_DEC(obj) \
(G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_SBC_DEC,GstSbcDec))
#define GST_SBC_DEC_CLASS(klass) \
(G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_SBC_DEC,GstSbcDecClass))
#define GST_IS_SBC_DEC(obj) \
(G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_SBC_DEC))
#define GST_IS_SBC_DEC_CLASS(obj) \
(G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_SBC_DEC))
typedef struct _GstSbcDec GstSbcDec;
typedef struct _GstSbcDecClass GstSbcDecClass;
struct _GstSbcDec {
GstElement element;
GstPad *sinkpad;
GstPad *srcpad;
GstAdapter *adapter;
/* caps for outgoing buffers */
GstCaps *outcaps;
sbc_t sbc;
guint64 next_sample;
guint64 next_timestamp;
};
struct _GstSbcDecClass {
GstElementClass parent_class;
};
GType gst_sbc_dec_get_type(void);
gboolean gst_sbc_dec_plugin_init(GstPlugin *plugin);
G_END_DECLS

604
ext/sbc/gstsbcenc.c Normal file
View file

@ -0,0 +1,604 @@
/*
*
* BlueZ - Bluetooth protocol stack for Linux
*
* Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
*
*
* 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
#include <string.h>
#include "gstpragma.h"
#include "gstsbcutil.h"
#include "gstsbcenc.h"
#define SBC_ENC_DEFAULT_MODE SBC_MODE_AUTO
#define SBC_ENC_DEFAULT_BLOCKS 0
#define SBC_ENC_DEFAULT_SUB_BANDS 0
#define SBC_ENC_DEFAULT_ALLOCATION SBC_AM_AUTO
#define SBC_ENC_DEFAULT_RATE 0
#define SBC_ENC_DEFAULT_CHANNELS 0
#define SBC_ENC_BITPOOL_AUTO 1
#define SBC_ENC_BITPOOL_MIN 2
#define SBC_ENC_BITPOOL_MIN_STR "2"
#define SBC_ENC_BITPOOL_MAX 64
#define SBC_ENC_BITPOOL_MAX_STR "64"
GST_DEBUG_CATEGORY_STATIC (sbc_enc_debug);
#define GST_CAT_DEFAULT sbc_enc_debug
#define GST_TYPE_SBC_MODE (gst_sbc_mode_get_type())
static GType
gst_sbc_mode_get_type (void)
{
static GType sbc_mode_type = 0;
static GEnumValue sbc_modes[] = {
{SBC_MODE_MONO, "Mono", "mono"},
{SBC_MODE_DUAL_CHANNEL, "Dual Channel", "dual"},
{SBC_MODE_STEREO, "Stereo", "stereo"},
{SBC_MODE_JOINT_STEREO, "Joint Stereo", "joint"},
{SBC_MODE_AUTO, "Auto", "auto"},
{-1, NULL, NULL}
};
if (!sbc_mode_type)
sbc_mode_type = g_enum_register_static ("GstSbcMode", sbc_modes);
return sbc_mode_type;
}
#define GST_TYPE_SBC_ALLOCATION (gst_sbc_allocation_get_type())
static GType
gst_sbc_allocation_get_type (void)
{
static GType sbc_allocation_type = 0;
static GEnumValue sbc_allocations[] = {
{SBC_AM_LOUDNESS, "Loudness", "loudness"},
{SBC_AM_SNR, "SNR", "snr"},
{SBC_AM_AUTO, "Auto", "auto"},
{-1, NULL, NULL}
};
if (!sbc_allocation_type)
sbc_allocation_type =
g_enum_register_static ("GstSbcAllocation", sbc_allocations);
return sbc_allocation_type;
}
#define GST_TYPE_SBC_BLOCKS (gst_sbc_blocks_get_type())
static GType
gst_sbc_blocks_get_type (void)
{
static GType sbc_blocks_type = 0;
static GEnumValue sbc_blocks[] = {
{0, "Auto", "auto"},
{4, "4", "4"},
{8, "8", "8"},
{12, "12", "12"},
{16, "16", "16"},
{-1, NULL, NULL}
};
if (!sbc_blocks_type)
sbc_blocks_type = g_enum_register_static ("GstSbcBlocks", sbc_blocks);
return sbc_blocks_type;
}
#define GST_TYPE_SBC_SUBBANDS (gst_sbc_subbands_get_type())
static GType
gst_sbc_subbands_get_type (void)
{
static GType sbc_subbands_type = 0;
static GEnumValue sbc_subbands[] = {
{0, "Auto", "auto"},
{4, "4 subbands", "4"},
{8, "8 subbands", "8"},
{-1, NULL, NULL}
};
if (!sbc_subbands_type)
sbc_subbands_type = g_enum_register_static ("GstSbcSubbands", sbc_subbands);
return sbc_subbands_type;
}
enum
{
PROP_0,
PROP_MODE,
PROP_ALLOCATION,
PROP_BLOCKS,
PROP_SUBBANDS,
PROP_BITPOOL
};
GST_BOILERPLATE (GstSbcEnc, gst_sbc_enc, GstElement, GST_TYPE_ELEMENT);
static const GstElementDetails sbc_enc_details =
GST_ELEMENT_DETAILS ("Bluetooth SBC encoder",
"Codec/Encoder/Audio",
"Encode a SBC audio stream",
"Marcel Holtmann <marcel@holtmann.org>");
static GstStaticPadTemplate sbc_enc_sink_factory =
GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
GST_STATIC_CAPS ("audio/x-raw-int, "
"rate = (int) { 16000, 32000, 44100, 48000 }, "
"channels = (int) [ 1, 2 ], "
"endianness = (int) BYTE_ORDER, "
"signed = (boolean) true, " "width = (int) 16, " "depth = (int) 16"));
static GstStaticPadTemplate sbc_enc_src_factory =
GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
GST_STATIC_CAPS ("audio/x-sbc, "
"rate = (int) { 16000, 32000, 44100, 48000 }, "
"channels = (int) [ 1, 2 ], "
"mode = (string) { \"mono\", \"dual\", \"stereo\", \"joint\" }, "
"blocks = (int) { 4, 8, 12, 16 }, "
"subbands = (int) { 4, 8 }, "
"allocation = (string) { \"snr\", \"loudness\" }, "
"bitpool = (int) [ " SBC_ENC_BITPOOL_MIN_STR
", " SBC_ENC_BITPOOL_MAX_STR " ]"));
gboolean gst_sbc_enc_fill_sbc_params (GstSbcEnc * enc, GstCaps * caps);
static GstCaps *
sbc_enc_generate_srcpad_caps (GstSbcEnc * enc)
{
GstCaps *src_caps;
GstStructure *structure;
GEnumValue *enum_value;
GEnumClass *enum_class;
GValue *value;
src_caps = gst_caps_copy (gst_pad_get_pad_template_caps (enc->srcpad));
structure = gst_caps_get_structure (src_caps, 0);
value = g_new0 (GValue, 1);
if (enc->rate != 0)
gst_sbc_util_set_structure_int_param (structure, "rate", enc->rate, value);
if (enc->channels != 0)
gst_sbc_util_set_structure_int_param (structure, "channels",
enc->channels, value);
if (enc->subbands != 0)
gst_sbc_util_set_structure_int_param (structure, "subbands",
enc->subbands, value);
if (enc->blocks != 0)
gst_sbc_util_set_structure_int_param (structure, "blocks",
enc->blocks, value);
if (enc->bitpool != SBC_ENC_BITPOOL_AUTO)
gst_sbc_util_set_structure_int_param (structure, "bitpool",
enc->bitpool, value);
if (enc->mode != SBC_ENC_DEFAULT_MODE) {
enum_class = g_type_class_ref (GST_TYPE_SBC_MODE);
enum_value = g_enum_get_value (enum_class, enc->mode);
gst_sbc_util_set_structure_string_param (structure, "mode",
enum_value->value_nick, value);
g_type_class_unref (enum_class);
}
if (enc->allocation != SBC_AM_AUTO) {
enum_class = g_type_class_ref (GST_TYPE_SBC_ALLOCATION);
enum_value = g_enum_get_value (enum_class, enc->allocation);
gst_sbc_util_set_structure_string_param (structure, "allocation",
enum_value->value_nick, value);
g_type_class_unref (enum_class);
}
g_free (value);
return src_caps;
}
static GstCaps *
sbc_enc_src_getcaps (GstPad * pad)
{
GstSbcEnc *enc;
enc = GST_SBC_ENC (GST_PAD_PARENT (pad));
return sbc_enc_generate_srcpad_caps (enc);
}
static gboolean
sbc_enc_src_setcaps (GstPad * pad, GstCaps * caps)
{
GstSbcEnc *enc = GST_SBC_ENC (GST_PAD_PARENT (pad));
GST_LOG_OBJECT (enc, "setting srcpad caps");
return gst_sbc_enc_fill_sbc_params (enc, caps);
}
static GstCaps *
sbc_enc_src_caps_fixate (GstSbcEnc * enc, GstCaps * caps)
{
gchar *error_message = NULL;
GstCaps *result;
result = gst_sbc_util_caps_fixate (caps, &error_message);
if (!result) {
GST_WARNING_OBJECT (enc, "Invalid input caps caused parsing "
"error: %s", error_message);
g_free (error_message);
return NULL;
}
return result;
}
static GstCaps *
sbc_enc_get_fixed_srcpad_caps (GstSbcEnc * enc)
{
GstCaps *caps;
gboolean res = TRUE;
GstCaps *result_caps = NULL;
caps = gst_pad_get_allowed_caps (enc->srcpad);
if (caps == NULL)
caps = sbc_enc_src_getcaps (enc->srcpad);
if (caps == GST_CAPS_NONE || gst_caps_is_empty (caps)) {
res = FALSE;
goto done;
}
result_caps = sbc_enc_src_caps_fixate (enc, caps);
done:
gst_caps_unref (caps);
if (!res)
return NULL;
return result_caps;
}
static gboolean
sbc_enc_sink_setcaps (GstPad * pad, GstCaps * caps)
{
GstSbcEnc *enc;
GstStructure *structure;
GstCaps *src_caps;
gint rate, channels;
gboolean res;
enc = GST_SBC_ENC (GST_PAD_PARENT (pad));
structure = gst_caps_get_structure (caps, 0);
if (!gst_structure_get_int (structure, "rate", &rate))
return FALSE;
if (!gst_structure_get_int (structure, "channels", &channels))
return FALSE;
enc->rate = rate;
enc->channels = channels;
src_caps = sbc_enc_get_fixed_srcpad_caps (enc);
if (!src_caps)
return FALSE;
res = gst_pad_set_caps (enc->srcpad, src_caps);
gst_caps_unref (src_caps);
return res;
}
gboolean
gst_sbc_enc_fill_sbc_params (GstSbcEnc * enc, GstCaps * caps)
{
if (!gst_caps_is_fixed (caps)) {
GST_DEBUG_OBJECT (enc, "didn't receive fixed caps, " "returning false");
return FALSE;
}
if (!gst_sbc_util_fill_sbc_params (&enc->sbc, caps))
return FALSE;
if (enc->rate != 0 && gst_sbc_parse_rate_from_sbc (enc->sbc.frequency)
!= enc->rate)
goto fail;
if (enc->channels != 0 && gst_sbc_get_channel_number (enc->sbc.mode)
!= enc->channels)
goto fail;
if (enc->blocks != 0 && gst_sbc_parse_blocks_from_sbc (enc->sbc.blocks)
!= enc->blocks)
goto fail;
if (enc->subbands != 0
&& gst_sbc_parse_subbands_from_sbc (enc->sbc.subbands) != enc->subbands)
goto fail;
if (enc->mode != SBC_ENC_DEFAULT_MODE && enc->sbc.mode != enc->mode)
goto fail;
if (enc->allocation != SBC_AM_AUTO && enc->sbc.allocation != enc->allocation)
goto fail;
if (enc->bitpool != SBC_ENC_BITPOOL_AUTO && enc->sbc.bitpool != enc->bitpool)
goto fail;
enc->codesize = sbc_get_codesize (&enc->sbc);
enc->frame_length = sbc_get_frame_length (&enc->sbc);
enc->frame_duration = sbc_get_frame_duration (&enc->sbc) * 1000;
GST_DEBUG_OBJECT (enc, "codesize: %d, frame_length: %d, frame_duration:"
" %d", enc->codesize, enc->frame_length, enc->frame_duration);
return TRUE;
fail:
memset (&enc->sbc, 0, sizeof (sbc_t));
return FALSE;
}
static GstFlowReturn
sbc_enc_chain (GstPad * pad, GstBuffer * buffer)
{
GstSbcEnc *enc = GST_SBC_ENC (gst_pad_get_parent (pad));
GstAdapter *adapter = enc->adapter;
GstFlowReturn res = GST_FLOW_OK;
gst_adapter_push (adapter, buffer);
while (gst_adapter_available (adapter) >= enc->codesize && res == GST_FLOW_OK) {
GstBuffer *output;
GstCaps *caps;
const guint8 *data;
gint consumed;
caps = GST_PAD_CAPS (enc->srcpad);
res = gst_pad_alloc_buffer_and_set_caps (enc->srcpad,
GST_BUFFER_OFFSET_NONE, enc->frame_length, caps, &output);
if (res != GST_FLOW_OK)
goto done;
data = gst_adapter_peek (adapter, enc->codesize);
consumed = sbc_encode (&enc->sbc, (gpointer) data,
enc->codesize,
GST_BUFFER_DATA (output), GST_BUFFER_SIZE (output), NULL);
if (consumed <= 0) {
GST_DEBUG_OBJECT (enc, "comsumed < 0, codesize: %d", enc->codesize);
break;
}
gst_adapter_flush (adapter, consumed);
GST_BUFFER_TIMESTAMP (output) = GST_BUFFER_TIMESTAMP (buffer);
/* we have only 1 frame */
GST_BUFFER_DURATION (output) = enc->frame_duration;
res = gst_pad_push (enc->srcpad, output);
if (res != GST_FLOW_OK)
goto done;
}
done:
gst_object_unref (enc);
return res;
}
static GstStateChangeReturn
sbc_enc_change_state (GstElement * element, GstStateChange transition)
{
GstSbcEnc *enc = GST_SBC_ENC (element);
switch (transition) {
case GST_STATE_CHANGE_READY_TO_PAUSED:
GST_DEBUG ("Setup subband codec");
sbc_init (&enc->sbc, 0);
break;
case GST_STATE_CHANGE_PAUSED_TO_READY:
GST_DEBUG ("Finish subband codec");
sbc_finish (&enc->sbc);
break;
default:
break;
}
return parent_class->change_state (element, transition);
}
static void
gst_sbc_enc_dispose (GObject * object)
{
GstSbcEnc *enc = GST_SBC_ENC (object);
if (enc->adapter != NULL)
g_object_unref (G_OBJECT (enc->adapter));
enc->adapter = NULL;
}
static void
gst_sbc_enc_base_init (gpointer g_class)
{
GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
gst_element_class_add_pad_template (element_class,
gst_static_pad_template_get (&sbc_enc_sink_factory));
gst_element_class_add_pad_template (element_class,
gst_static_pad_template_get (&sbc_enc_src_factory));
gst_element_class_set_details (element_class, &sbc_enc_details);
}
static void
gst_sbc_enc_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec)
{
GstSbcEnc *enc = GST_SBC_ENC (object);
/* changes to those properties will only happen on the next caps
* negotiation */
switch (prop_id) {
case PROP_MODE:
enc->mode = g_value_get_enum (value);
break;
case PROP_ALLOCATION:
enc->allocation = g_value_get_enum (value);
break;
case PROP_BLOCKS:
enc->blocks = g_value_get_enum (value);
break;
case PROP_SUBBANDS:
enc->subbands = g_value_get_enum (value);
break;
case PROP_BITPOOL:
enc->bitpool = g_value_get_int (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gst_sbc_enc_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec)
{
GstSbcEnc *enc = GST_SBC_ENC (object);
switch (prop_id) {
case PROP_MODE:
g_value_set_enum (value, enc->mode);
break;
case PROP_ALLOCATION:
g_value_set_enum (value, enc->allocation);
break;
case PROP_BLOCKS:
g_value_set_enum (value, enc->blocks);
break;
case PROP_SUBBANDS:
g_value_set_enum (value, enc->subbands);
break;
case PROP_BITPOOL:
g_value_set_int (value, enc->bitpool);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gst_sbc_enc_class_init (GstSbcEncClass * klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
parent_class = g_type_class_peek_parent (klass);
object_class->set_property = GST_DEBUG_FUNCPTR (gst_sbc_enc_set_property);
object_class->get_property = GST_DEBUG_FUNCPTR (gst_sbc_enc_get_property);
object_class->dispose = GST_DEBUG_FUNCPTR (gst_sbc_enc_dispose);
element_class->change_state = GST_DEBUG_FUNCPTR (sbc_enc_change_state);
g_object_class_install_property (object_class, PROP_MODE,
g_param_spec_enum ("mode", "Mode",
"Encoding mode", GST_TYPE_SBC_MODE,
SBC_ENC_DEFAULT_MODE, G_PARAM_READWRITE));
g_object_class_install_property (object_class, PROP_ALLOCATION,
g_param_spec_enum ("allocation", "Allocation",
"Allocation method", GST_TYPE_SBC_ALLOCATION,
SBC_ENC_DEFAULT_ALLOCATION, G_PARAM_READWRITE));
g_object_class_install_property (object_class, PROP_BLOCKS,
g_param_spec_enum ("blocks", "Blocks",
"Blocks", GST_TYPE_SBC_BLOCKS,
SBC_ENC_DEFAULT_BLOCKS, G_PARAM_READWRITE));
g_object_class_install_property (object_class, PROP_SUBBANDS,
g_param_spec_enum ("subbands", "Sub bands",
"Number of sub bands", GST_TYPE_SBC_SUBBANDS,
SBC_ENC_DEFAULT_SUB_BANDS, G_PARAM_READWRITE));
g_object_class_install_property (object_class, PROP_BITPOOL,
g_param_spec_int ("bitpool", "Bitpool",
"Bitpool (use 1 for automatic selection)",
SBC_ENC_BITPOOL_AUTO, SBC_ENC_BITPOOL_MAX,
SBC_ENC_BITPOOL_AUTO, G_PARAM_READWRITE));
GST_DEBUG_CATEGORY_INIT (sbc_enc_debug, "sbcenc", 0, "SBC encoding element");
}
static void
gst_sbc_enc_init (GstSbcEnc * self, GstSbcEncClass * klass)
{
self->sinkpad =
gst_pad_new_from_static_template (&sbc_enc_sink_factory, "sink");
gst_pad_set_setcaps_function (self->sinkpad,
GST_DEBUG_FUNCPTR (sbc_enc_sink_setcaps));
gst_element_add_pad (GST_ELEMENT (self), self->sinkpad);
self->srcpad = gst_pad_new_from_static_template (&sbc_enc_src_factory, "src");
gst_pad_set_getcaps_function (self->srcpad,
GST_DEBUG_FUNCPTR (sbc_enc_src_getcaps));
gst_pad_set_setcaps_function (self->srcpad,
GST_DEBUG_FUNCPTR (sbc_enc_src_setcaps));
gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
gst_pad_set_chain_function (self->sinkpad, GST_DEBUG_FUNCPTR (sbc_enc_chain));
self->subbands = SBC_ENC_DEFAULT_SUB_BANDS;
self->blocks = SBC_ENC_DEFAULT_BLOCKS;
self->mode = SBC_ENC_DEFAULT_MODE;
self->allocation = SBC_ENC_DEFAULT_ALLOCATION;
self->rate = SBC_ENC_DEFAULT_RATE;
self->channels = SBC_ENC_DEFAULT_CHANNELS;
self->bitpool = SBC_ENC_BITPOOL_AUTO;
self->frame_length = 0;
self->frame_duration = 0;
self->adapter = gst_adapter_new ();
}
gboolean
gst_sbc_enc_plugin_init (GstPlugin * plugin)
{
return gst_element_register (plugin, "sbcenc",
GST_RANK_NONE, GST_TYPE_SBC_ENC);
}

75
ext/sbc/gstsbcenc.h Normal file
View file

@ -0,0 +1,75 @@
/*
*
* BlueZ - Bluetooth protocol stack for Linux
*
* Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
*
*
* 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
*
*/
#include <gst/gst.h>
#include <gst/base/gstadapter.h>
#include "sbc.h"
G_BEGIN_DECLS
#define GST_TYPE_SBC_ENC \
(gst_sbc_enc_get_type())
#define GST_SBC_ENC(obj) \
(G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_SBC_ENC,GstSbcEnc))
#define GST_SBC_ENC_CLASS(klass) \
(G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_SBC_ENC,GstSbcEncClass))
#define GST_IS_SBC_ENC(obj) \
(G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_SBC_ENC))
#define GST_IS_SBC_ENC_CLASS(obj) \
(G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_SBC_ENC))
typedef struct _GstSbcEnc GstSbcEnc;
typedef struct _GstSbcEncClass GstSbcEncClass;
struct _GstSbcEnc {
GstElement element;
GstPad *sinkpad;
GstPad *srcpad;
GstAdapter *adapter;
gint rate;
gint channels;
gint mode;
gint blocks;
gint allocation;
gint subbands;
gint bitpool;
guint codesize;
gint frame_length;
gint frame_duration;
sbc_t sbc;
};
struct _GstSbcEncClass {
GstElementClass parent_class;
};
GType gst_sbc_enc_get_type(void);
gboolean gst_sbc_enc_plugin_init(GstPlugin *plugin);
G_END_DECLS

538
ext/sbc/gstsbcutil.c Normal file
View file

@ -0,0 +1,538 @@
/*
*
* BlueZ - Bluetooth protocol stack for Linux
*
* Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
*
*
* 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
#include <math.h>
#include "gstsbcutil.h"
/*
* Selects one rate from a list of possible rates
* TODO - use a better approach to this (it is selecting the last element)
*/
gint
gst_sbc_select_rate_from_list (const GValue * value)
{
guint size = gst_value_list_get_size (value);
return g_value_get_int (gst_value_list_get_value (value, size - 1));
}
/*
* Selects one number of channels option from a range of possible numbers
* TODO - use a better approach to this (it is selecting the maximum value)
*/
gint
gst_sbc_select_channels_from_range (const GValue * value)
{
return gst_value_get_int_range_max (value);
}
/*
* Selects one number of blocks from a list of possible blocks
* TODO - use a better approach to this (it is selecting the last element)
*/
gint
gst_sbc_select_blocks_from_list (const GValue * value)
{
guint size = gst_value_list_get_size (value);
return g_value_get_int (gst_value_list_get_value (value, size - 1));
}
/*
* Selects one number of subbands from a list
* TODO - use a better approach to this (it is selecting the last element)
*/
gint
gst_sbc_select_subbands_from_list (const GValue * value)
{
guint size = gst_value_list_get_size (value);
return g_value_get_int (gst_value_list_get_value (value, size - 1));
}
/*
* Selects one bitpool option from a range
* TODO - use a better approach to this (it is selecting the maximum value)
*/
gint
gst_sbc_select_bitpool_from_range (const GValue * value)
{
return gst_value_get_int_range_max (value);
}
/*
* Selects one allocation mode from the ones on the list
* TODO - use a better approach
*/
const gchar *
gst_sbc_get_allocation_from_list (const GValue * value)
{
guint size = gst_value_list_get_size (value);
return g_value_get_string (gst_value_list_get_value (value, size - 1));
}
/*
* Selects one mode from the ones on the list
*/
const gchar *
gst_sbc_get_mode_from_list (const GValue * list, gint channels)
{
unsigned int i;
const GValue *value;
const gchar *aux;
gboolean joint, stereo, dual, mono;
guint size = gst_value_list_get_size (list);
joint = stereo = dual = mono = FALSE;
for (i = 0; i < size; i++) {
value = gst_value_list_get_value (list, i);
aux = g_value_get_string (value);
if (strcmp ("joint", aux) == 0)
joint = TRUE;
else if (strcmp ("stereo", aux) == 0)
stereo = TRUE;
else if (strcmp ("dual", aux) == 0)
dual = TRUE;
else if (strcmp ("mono", aux) == 0)
mono = TRUE;
}
if (channels == 1 && mono)
return "mono";
else if (channels == 2) {
if (joint)
return "joint";
else if (stereo)
return "stereo";
else if (dual)
return "dual";
}
return NULL;
}
gint
gst_sbc_parse_rate_from_sbc (gint frequency)
{
switch (frequency) {
case SBC_FREQ_16000:
return 16000;
case SBC_FREQ_32000:
return 32000;
case SBC_FREQ_44100:
return 44100;
case SBC_FREQ_48000:
return 48000;
default:
return 0;
}
}
gint
gst_sbc_parse_rate_to_sbc (gint rate)
{
switch (rate) {
case 16000:
return SBC_FREQ_16000;
case 32000:
return SBC_FREQ_32000;
case 44100:
return SBC_FREQ_44100;
case 48000:
return SBC_FREQ_48000;
default:
return -1;
}
}
gint
gst_sbc_get_channel_number (gint mode)
{
switch (mode) {
case SBC_MODE_JOINT_STEREO:
case SBC_MODE_STEREO:
case SBC_MODE_DUAL_CHANNEL:
return 2;
case SBC_MODE_MONO:
return 1;
default:
return 0;
}
}
gint
gst_sbc_parse_subbands_from_sbc (gint subbands)
{
switch (subbands) {
case SBC_SB_4:
return 4;
case SBC_SB_8:
return 8;
default:
return 0;
}
}
gint
gst_sbc_parse_subbands_to_sbc (gint subbands)
{
switch (subbands) {
case 4:
return SBC_SB_4;
case 8:
return SBC_SB_8;
default:
return -1;
}
}
gint
gst_sbc_parse_blocks_from_sbc (gint blocks)
{
switch (blocks) {
case SBC_BLK_4:
return 4;
case SBC_BLK_8:
return 8;
case SBC_BLK_12:
return 12;
case SBC_BLK_16:
return 16;
default:
return 0;
}
}
gint
gst_sbc_parse_blocks_to_sbc (gint blocks)
{
switch (blocks) {
case 4:
return SBC_BLK_4;
case 8:
return SBC_BLK_8;
case 12:
return SBC_BLK_12;
case 16:
return SBC_BLK_16;
default:
return -1;
}
}
const gchar *
gst_sbc_parse_mode_from_sbc (gint mode)
{
switch (mode) {
case SBC_MODE_MONO:
return "mono";
case SBC_MODE_DUAL_CHANNEL:
return "dual";
case SBC_MODE_STEREO:
return "stereo";
case SBC_MODE_JOINT_STEREO:
case SBC_MODE_AUTO:
return "joint";
default:
return NULL;
}
}
gint
gst_sbc_parse_mode_to_sbc (const gchar * mode)
{
if (g_ascii_strcasecmp (mode, "joint") == 0)
return SBC_MODE_JOINT_STEREO;
else if (g_ascii_strcasecmp (mode, "stereo") == 0)
return SBC_MODE_STEREO;
else if (g_ascii_strcasecmp (mode, "dual") == 0)
return SBC_MODE_DUAL_CHANNEL;
else if (g_ascii_strcasecmp (mode, "mono") == 0)
return SBC_MODE_MONO;
else if (g_ascii_strcasecmp (mode, "auto") == 0)
return SBC_MODE_JOINT_STEREO;
else
return -1;
}
const gchar *
gst_sbc_parse_allocation_from_sbc (gint alloc)
{
switch (alloc) {
case SBC_AM_LOUDNESS:
return "loudness";
case SBC_AM_SNR:
return "snr";
case SBC_AM_AUTO:
return "loudness";
default:
return NULL;
}
}
gint
gst_sbc_parse_allocation_to_sbc (const gchar * allocation)
{
if (g_ascii_strcasecmp (allocation, "loudness") == 0)
return SBC_AM_LOUDNESS;
else if (g_ascii_strcasecmp (allocation, "snr") == 0)
return SBC_AM_SNR;
else
return SBC_AM_LOUDNESS;
}
GstCaps *
gst_sbc_parse_caps_from_sbc (sbc_t * sbc)
{
GstCaps *caps;
const gchar *mode_str;
const gchar *allocation_str;
mode_str = gst_sbc_parse_mode_from_sbc (sbc->mode);
allocation_str = gst_sbc_parse_allocation_from_sbc (sbc->allocation);
caps = gst_caps_new_simple ("audio/x-sbc",
"rate", G_TYPE_INT,
gst_sbc_parse_rate_from_sbc (sbc->frequency),
"channels", G_TYPE_INT,
gst_sbc_get_channel_number (sbc->mode),
"mode", G_TYPE_STRING, mode_str,
"subbands", G_TYPE_INT,
gst_sbc_parse_subbands_from_sbc (sbc->subbands),
"blocks", G_TYPE_INT,
gst_sbc_parse_blocks_from_sbc (sbc->blocks),
"allocation", G_TYPE_STRING, allocation_str,
"bitpool", G_TYPE_INT, sbc->bitpool, NULL);
return caps;
}
/*
* Given a GstCaps, this will return a fixed GstCaps on successful conversion.
* If an error occurs, it will return NULL and error_message will contain the
* error message.
*
* error_message must be passed NULL, if an error occurs, the caller has the
* ownership of the error_message, it must be freed after use.
*/
GstCaps *
gst_sbc_util_caps_fixate (GstCaps * caps, gchar ** error_message)
{
GstCaps *result;
GstStructure *structure;
const GValue *value;
gboolean error = FALSE;
gint temp, rate, channels, blocks, subbands, bitpool;
const gchar *allocation = NULL;
const gchar *mode = NULL;
g_assert (*error_message == NULL);
structure = gst_caps_get_structure (caps, 0);
if (!gst_structure_has_field (structure, "rate")) {
error = TRUE;
*error_message = g_strdup ("no rate");
goto error;
} else {
value = gst_structure_get_value (structure, "rate");
if (GST_VALUE_HOLDS_LIST (value))
temp = gst_sbc_select_rate_from_list (value);
else
temp = g_value_get_int (value);
rate = temp;
}
if (!gst_structure_has_field (structure, "channels")) {
error = TRUE;
*error_message = g_strdup ("no channels");
goto error;
} else {
value = gst_structure_get_value (structure, "channels");
if (GST_VALUE_HOLDS_INT_RANGE (value))
temp = gst_sbc_select_channels_from_range (value);
else
temp = g_value_get_int (value);
channels = temp;
}
if (!gst_structure_has_field (structure, "blocks")) {
error = TRUE;
*error_message = g_strdup ("no blocks.");
goto error;
} else {
value = gst_structure_get_value (structure, "blocks");
if (GST_VALUE_HOLDS_LIST (value))
temp = gst_sbc_select_blocks_from_list (value);
else
temp = g_value_get_int (value);
blocks = temp;
}
if (!gst_structure_has_field (structure, "subbands")) {
error = TRUE;
*error_message = g_strdup ("no subbands");
goto error;
} else {
value = gst_structure_get_value (structure, "subbands");
if (GST_VALUE_HOLDS_LIST (value))
temp = gst_sbc_select_subbands_from_list (value);
else
temp = g_value_get_int (value);
subbands = temp;
}
if (!gst_structure_has_field (structure, "bitpool")) {
error = TRUE;
*error_message = g_strdup ("no bitpool");
goto error;
} else {
value = gst_structure_get_value (structure, "bitpool");
if (GST_VALUE_HOLDS_INT_RANGE (value))
temp = gst_sbc_select_bitpool_from_range (value);
else
temp = g_value_get_int (value);
bitpool = temp;
}
if (!gst_structure_has_field (structure, "allocation")) {
error = TRUE;
*error_message = g_strdup ("no allocation");
goto error;
} else {
value = gst_structure_get_value (structure, "allocation");
if (GST_VALUE_HOLDS_LIST (value))
allocation = gst_sbc_get_allocation_from_list (value);
else
allocation = g_value_get_string (value);
}
if (!gst_structure_has_field (structure, "mode")) {
error = TRUE;
*error_message = g_strdup ("no mode");
goto error;
} else {
value = gst_structure_get_value (structure, "mode");
if (GST_VALUE_HOLDS_LIST (value)) {
mode = gst_sbc_get_mode_from_list (value, channels);
} else
mode = g_value_get_string (value);
}
/* perform validation
* if channels is 1, we must have channel mode = mono
* if channels is 2, we can't have channel mode = mono */
if ((channels == 1 && (strcmp (mode, "mono") != 0)) ||
(channels == 2 && (strcmp (mode, "mono") == 0))) {
*error_message = g_strdup_printf ("Invalid combination of "
"channels (%d) and channel mode (%s)", channels, mode);
error = TRUE;
}
error:
if (error)
return NULL;
result = gst_caps_new_simple ("audio/x-sbc",
"rate", G_TYPE_INT, rate,
"channels", G_TYPE_INT, channels,
"mode", G_TYPE_STRING, mode,
"blocks", G_TYPE_INT, blocks,
"subbands", G_TYPE_INT, subbands,
"allocation", G_TYPE_STRING, allocation,
"bitpool", G_TYPE_INT, bitpool, NULL);
return result;
}
/**
* Sets the int field_value to the param "field" on the structure.
* value is used to do the operation, it must be a uninitialized (zero-filled)
* GValue, it will be left unitialized at the end of the function.
*/
void
gst_sbc_util_set_structure_int_param (GstStructure * structure,
const gchar * field, gint field_value, GValue * value)
{
value = g_value_init (value, G_TYPE_INT);
g_value_set_int (value, field_value);
gst_structure_set_value (structure, field, value);
g_value_unset (value);
}
/**
* Sets the string field_value to the param "field" on the structure.
* value is used to do the operation, it must be a uninitialized (zero-filled)
* GValue, it will be left unitialized at the end of the function.
*/
void
gst_sbc_util_set_structure_string_param (GstStructure * structure,
const gchar * field, const gchar * field_value, GValue * value)
{
value = g_value_init (value, G_TYPE_STRING);
g_value_set_string (value, field_value);
gst_structure_set_value (structure, field, value);
g_value_unset (value);
}
gboolean
gst_sbc_util_fill_sbc_params (sbc_t * sbc, GstCaps * caps)
{
GstStructure *structure;
gint rate, channels, subbands, blocks, bitpool;
const gchar *mode;
const gchar *allocation;
g_assert (gst_caps_is_fixed (caps));
structure = gst_caps_get_structure (caps, 0);
if (!gst_structure_get_int (structure, "rate", &rate))
return FALSE;
if (!gst_structure_get_int (structure, "channels", &channels))
return FALSE;
if (!gst_structure_get_int (structure, "subbands", &subbands))
return FALSE;
if (!gst_structure_get_int (structure, "blocks", &blocks))
return FALSE;
if (!gst_structure_get_int (structure, "bitpool", &bitpool))
return FALSE;
if (!(mode = gst_structure_get_string (structure, "mode")))
return FALSE;
if (!(allocation = gst_structure_get_string (structure, "allocation")))
return FALSE;
if (channels == 1 && strcmp (mode, "mono") != 0)
return FALSE;
sbc->frequency = gst_sbc_parse_rate_to_sbc (rate);
sbc->blocks = gst_sbc_parse_blocks_to_sbc (blocks);
sbc->subbands = gst_sbc_parse_subbands_to_sbc (subbands);
sbc->bitpool = bitpool;
sbc->mode = gst_sbc_parse_mode_to_sbc (mode);
sbc->allocation = gst_sbc_parse_allocation_to_sbc (allocation);
return TRUE;
}

74
ext/sbc/gstsbcutil.h Normal file
View file

@ -0,0 +1,74 @@
/*
*
* BlueZ - Bluetooth protocol stack for Linux
*
* Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
*
*
* 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
*
*/
#include <gst/gst.h>
#include "sbc.h"
#include <string.h>
#define SBC_AM_AUTO 0x02
#define SBC_MODE_AUTO 0x04
gint gst_sbc_select_rate_from_list(const GValue *value);
gint gst_sbc_select_channels_from_range(const GValue *value);
gint gst_sbc_select_blocks_from_list(const GValue *value);
gint gst_sbc_select_subbands_from_list(const GValue *value);
gint gst_sbc_select_bitpool_from_range(const GValue *value);
const gchar *gst_sbc_get_allocation_from_list(const GValue *value);
const gchar *gst_sbc_get_mode_from_list(const GValue *value, gint channels);
gint gst_sbc_get_channel_number(gint mode);
gint gst_sbc_parse_rate_from_sbc(gint frequency);
gint gst_sbc_parse_rate_to_sbc(gint rate);
gint gst_sbc_parse_subbands_from_sbc(gint subbands);
gint gst_sbc_parse_subbands_to_sbc(gint subbands);
gint gst_sbc_parse_blocks_from_sbc(gint blocks);
gint gst_sbc_parse_blocks_to_sbc(gint blocks);
const gchar *gst_sbc_parse_mode_from_sbc(gint mode);
gint gst_sbc_parse_mode_to_sbc(const gchar *mode);
const gchar *gst_sbc_parse_allocation_from_sbc(gint alloc);
gint gst_sbc_parse_allocation_to_sbc(const gchar *allocation);
GstCaps *gst_sbc_parse_caps_from_sbc(sbc_t *sbc);
GstCaps *gst_sbc_util_caps_fixate(GstCaps *caps, gchar **error_message);
void gst_sbc_util_set_structure_int_param(GstStructure *structure,
const gchar *field, gint field_value,
GValue *value);
void gst_sbc_util_set_structure_string_param(GstStructure *structure,
const gchar *field, const gchar *field_value,
GValue *value);
gboolean gst_sbc_util_fill_sbc_params(sbc_t *sbc, GstCaps *caps);