3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 #include "gstpragma.h"
31 #include "gstsbcutil.h"
32 #include "gstsbcdec.h"
34 GST_DEBUG_CATEGORY_STATIC(sbc_dec_debug);
35 #define GST_CAT_DEFAULT sbc_dec_debug
37 GST_BOILERPLATE(GstSbcDec, gst_sbc_dec, GstElement, GST_TYPE_ELEMENT);
39 static const GstElementDetails sbc_dec_details =
40 GST_ELEMENT_DETAILS("Bluetooth SBC decoder",
41 "Codec/Decoder/Audio",
42 "Decode a SBC audio stream",
43 "Marcel Holtmann <marcel@holtmann.org>");
45 static GstStaticPadTemplate sbc_dec_sink_factory =
46 GST_STATIC_PAD_TEMPLATE("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
47 GST_STATIC_CAPS("audio/x-sbc"));
49 static GstStaticPadTemplate sbc_dec_src_factory =
50 GST_STATIC_PAD_TEMPLATE("src", GST_PAD_SRC, GST_PAD_ALWAYS,
51 GST_STATIC_CAPS("audio/x-raw-int, "
52 "rate = (int) { 16000, 32000, 44100, 48000 }, "
53 "channels = (int) [ 1, 2 ], "
54 "endianness = (int) BYTE_ORDER, "
55 "signed = (boolean) true, "
59 static GstFlowReturn sbc_dec_chain(GstPad *pad, GstBuffer *buffer)
61 GstSbcDec *dec = GST_SBC_DEC(gst_pad_get_parent(pad));
62 GstFlowReturn res = GST_FLOW_OK;
63 guint size, codesize, offset = 0;
66 codesize = sbc_get_codesize(&dec->sbc);
69 GstBuffer *temp = buffer;
70 buffer = gst_buffer_span(dec->buffer, 0, buffer,
71 GST_BUFFER_SIZE(dec->buffer) + GST_BUFFER_SIZE(buffer));
72 gst_buffer_unref(temp);
73 gst_buffer_unref(dec->buffer);
77 data = GST_BUFFER_DATA(buffer);
78 size = GST_BUFFER_SIZE(buffer);
80 while (offset < size) {
82 GstPadTemplate *template;
86 res = gst_pad_alloc_buffer_and_set_caps(dec->srcpad,
87 GST_BUFFER_OFFSET_NONE,
88 codesize, NULL, &output);
90 if (res != GST_FLOW_OK)
93 consumed = sbc_decode(&dec->sbc, data + offset, size - offset,
94 GST_BUFFER_DATA(output), codesize,
99 /* we will reuse the same caps object */
100 if (dec->outcaps == NULL) {
101 caps = gst_caps_new_simple("audio/x-raw-int",
103 gst_sbc_parse_rate_from_sbc(
105 "channels", G_TYPE_INT,
106 gst_sbc_get_channel_number(
110 template = gst_static_pad_template_get(&sbc_dec_src_factory);
112 dec->outcaps = gst_caps_intersect(caps,
113 gst_pad_template_get_caps(template));
115 gst_caps_unref(caps);
118 gst_buffer_set_caps(output, dec->outcaps);
120 /* FIXME get a real timestamp */
121 GST_BUFFER_TIMESTAMP(output) = GST_CLOCK_TIME_NONE;
123 res = gst_pad_push(dec->srcpad, output);
124 if (res != GST_FLOW_OK)
131 dec->buffer = gst_buffer_create_sub(buffer,
132 offset, size - offset);
135 gst_buffer_unref(buffer);
136 gst_object_unref(dec);
141 static GstStateChangeReturn sbc_dec_change_state(GstElement *element,
142 GstStateChange transition)
144 GstSbcDec *dec = GST_SBC_DEC(element);
146 switch (transition) {
147 case GST_STATE_CHANGE_READY_TO_PAUSED:
148 GST_DEBUG("Setup subband codec");
150 gst_buffer_unref(dec->buffer);
153 sbc_init(&dec->sbc, 0);
157 case GST_STATE_CHANGE_PAUSED_TO_READY:
158 GST_DEBUG("Finish subband codec");
160 gst_buffer_unref(dec->buffer);
163 sbc_finish(&dec->sbc);
165 gst_caps_unref(dec->outcaps);
174 return parent_class->change_state(element, transition);
177 static void gst_sbc_dec_base_init(gpointer g_class)
179 GstElementClass *element_class = GST_ELEMENT_CLASS(g_class);
181 gst_element_class_add_pad_template(element_class,
182 gst_static_pad_template_get(&sbc_dec_sink_factory));
184 gst_element_class_add_pad_template(element_class,
185 gst_static_pad_template_get(&sbc_dec_src_factory));
187 gst_element_class_set_details(element_class, &sbc_dec_details);
190 static void gst_sbc_dec_class_init(GstSbcDecClass *klass)
192 GstElementClass *element_class = GST_ELEMENT_CLASS(klass);
194 parent_class = g_type_class_peek_parent(klass);
196 element_class->change_state = GST_DEBUG_FUNCPTR(sbc_dec_change_state);
198 GST_DEBUG_CATEGORY_INIT(sbc_dec_debug, "sbcdec", 0,
199 "SBC decoding element");
202 static void gst_sbc_dec_init(GstSbcDec *self, GstSbcDecClass *klass)
204 self->sinkpad = gst_pad_new_from_static_template(
205 &sbc_dec_sink_factory, "sink");
206 gst_pad_set_chain_function(self->sinkpad, GST_DEBUG_FUNCPTR(
208 gst_element_add_pad(GST_ELEMENT(self), self->sinkpad);
210 self->srcpad = gst_pad_new_from_static_template(
211 &sbc_dec_src_factory, "src");
212 gst_element_add_pad(GST_ELEMENT(self), self->srcpad);
214 self->outcaps = NULL;
217 gboolean gst_sbc_dec_plugin_init(GstPlugin *plugin)
219 return gst_element_register(plugin, "sbcdec", GST_RANK_PRIMARY,