blob: 84b515709210f893e7cf7b068c3dcc221e01c11d [file] [log] [blame]
Tim-Philipp Müllerfecddde2013-01-16 11:36:25 +00001/* GStreamer SBC audio decoder
Marcel Holtmann974757b2007-08-23 19:12:23 +00002 *
Marcel Holtmann1ddbdc32010-01-01 17:08:17 -08003 * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
Tim-Philipp Müllerca9daee2013-03-26 13:55:32 +00004 * Copyright (C) 2013 Tim-Philipp Müller <tim centricular net>
Marcel Holtmann974757b2007-08-23 19:12:23 +00005 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 */
21
Tim-Philipp Müllerca9daee2013-03-26 13:55:32 +000022/**
23 * SECTION:element-sbdec
Thibault Saunier78022a62017-03-08 15:01:13 -030024 * @title: sbdec
Tim-Philipp Müllerca9daee2013-03-26 13:55:32 +000025 *
26 * This element decodes a Bluetooth SBC audio streams to raw integer PCM audio
27 *
Thibault Saunier78022a62017-03-08 15:01:13 -030028 * ## Example pipelines
Tim-Philipp Müllerca9daee2013-03-26 13:55:32 +000029 * |[
30 * gst-launch-1.0 -v filesrc location=audio.sbc ! sbcparse ! sbcdec ! audioconvert ! audioresample ! autoaudiosink
31 * ]| Decode a raw SBC file.
Thibault Saunier78022a62017-03-08 15:01:13 -030032 *
Tim-Philipp Müllerca9daee2013-03-26 13:55:32 +000033 */
34
Marcel Holtmann974757b2007-08-23 19:12:23 +000035#ifdef HAVE_CONFIG_H
36#include <config.h>
37#endif
38
Marcel Holtmann36f952b2007-08-25 17:03:03 +000039#include <string.h>
Marcel Holtmann974757b2007-08-23 19:12:23 +000040
41#include "gstsbcdec.h"
42
Tim-Philipp Müllerfecddde2013-01-16 11:36:25 +000043/* FIXME: where does this come from? how is it derived? */
Wim Taymans91f4b152013-01-08 10:19:39 +010044#define BUF_SIZE 8192
45
Marcel Holtmann974757b2007-08-23 19:12:23 +000046GST_DEBUG_CATEGORY_STATIC (sbc_dec_debug);
47#define GST_CAT_DEFAULT sbc_dec_debug
48
Tim-Philipp Müllerfecddde2013-01-16 11:36:25 +000049#define parent_class gst_sbc_dec_parent_class
Tim-Philipp Müllerca9daee2013-03-26 13:55:32 +000050G_DEFINE_TYPE (GstSbcDec, gst_sbc_dec, GST_TYPE_AUDIO_DECODER);
Marcel Holtmann974757b2007-08-23 19:12:23 +000051
Marcel Holtmann36f952b2007-08-25 17:03:03 +000052static GstStaticPadTemplate sbc_dec_sink_factory =
53GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
Tim-Philipp Müllerca9daee2013-03-26 13:55:32 +000054 GST_STATIC_CAPS ("audio/x-sbc, channels = (int) [ 1, 2 ], "
55 "rate = (int) { 16000, 32000, 44100, 48000 }, "
56 "parsed = (boolean) true"));
Marcel Holtmann36f952b2007-08-25 17:03:03 +000057
58static GstStaticPadTemplate sbc_dec_src_factory =
59GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
Tim-Philipp Müllerfecddde2013-01-16 11:36:25 +000060 GST_STATIC_CAPS ("audio/x-raw, format=" GST_AUDIO_NE (S16) ", "
Marcel Holtmann7a231ad2007-08-25 17:37:05 +000061 "rate = (int) { 16000, 32000, 44100, 48000 }, "
Tim-Philipp Müllerfecddde2013-01-16 11:36:25 +000062 "channels = (int) [ 1, 2 ], layout=interleaved"));
Marcel Holtmann36f952b2007-08-25 17:03:03 +000063
64static GstFlowReturn
Tim-Philipp Müllerca9daee2013-03-26 13:55:32 +000065gst_sbc_dec_handle_frame (GstAudioDecoder * audio_dec, GstBuffer * buf)
Wim Taymans91f4b152013-01-08 10:19:39 +010066{
Tim-Philipp Müllerca9daee2013-03-26 13:55:32 +000067 GstSbcDec *dec = GST_SBC_DEC (audio_dec);
68 GstBuffer *outbuf = NULL;
Tim-Philipp Müllerfecddde2013-01-16 11:36:25 +000069 GstMapInfo out_map;
Tim-Philipp Müllerca9daee2013-03-26 13:55:32 +000070 GstMapInfo in_map;
71 gsize output_size;
72 guint num_frames, i;
Marcel Holtmann36f952b2007-08-25 17:03:03 +000073
Tim-Philipp Müllerca9daee2013-03-26 13:55:32 +000074 /* no fancy draining */
75 if (G_UNLIKELY (buf == NULL))
76 return GST_FLOW_OK;
Wim Taymansbeaeeaf2012-10-29 17:19:50 +000077
Tim-Philipp Müllerca9daee2013-03-26 13:55:32 +000078 gst_buffer_map (buf, &in_map, GST_MAP_READ);
Wim Taymans91f4b152013-01-08 10:19:39 +010079
Tim-Philipp Müllerca9daee2013-03-26 13:55:32 +000080 if (G_UNLIKELY (in_map.size == 0))
81 goto done;
Wim Taymansbeaeeaf2012-10-29 17:19:50 +000082
Tim-Philipp Müllerca9daee2013-03-26 13:55:32 +000083 /* we assume all frames are of the same size, this is implied by the
84 * input caps applying to the whole input buffer, and the parser should
85 * also have made sure of that */
86 if (G_UNLIKELY (in_map.size % dec->frame_len != 0))
87 goto mixed_frames;
Marcel Holtmann36f952b2007-08-25 17:03:03 +000088
Tim-Philipp Müllerca9daee2013-03-26 13:55:32 +000089 num_frames = in_map.size / dec->frame_len;
90 output_size = num_frames * dec->samples_per_frame * sizeof (gint16);
Marcel Holtmann36f952b2007-08-25 17:03:03 +000091
Tim-Philipp Müllerca9daee2013-03-26 13:55:32 +000092 outbuf = gst_audio_decoder_allocate_output_buffer (audio_dec, output_size);
Wim Taymansbeaeeaf2012-10-29 17:19:50 +000093
Tim-Philipp Müllerca9daee2013-03-26 13:55:32 +000094 if (outbuf == NULL)
95 goto no_buffer;
Wim Taymans91f4b152013-01-08 10:19:39 +010096
Tim-Philipp Müllerca9daee2013-03-26 13:55:32 +000097 gst_buffer_map (outbuf, &out_map, GST_MAP_WRITE);
Tim-Philipp Müllerfecddde2013-01-16 11:36:25 +000098
Tim-Philipp Müllerca9daee2013-03-26 13:55:32 +000099 for (i = 0; i < num_frames; ++i) {
100 gssize ret;
101 gsize written;
Wim Taymans91f4b152013-01-08 10:19:39 +0100102
Tim-Philipp Müllerca9daee2013-03-26 13:55:32 +0000103 ret = sbc_decode (&dec->sbc, in_map.data + (i * dec->frame_len),
104 dec->frame_len, out_map.data + (i * dec->samples_per_frame * 2),
105 dec->samples_per_frame * 2, &written);
Wim Taymans91f4b152013-01-08 10:19:39 +0100106
Tim-Philipp Müllerca9daee2013-03-26 13:55:32 +0000107 if (ret <= 0 || written != (dec->samples_per_frame * 2)) {
108 GST_WARNING_OBJECT (dec, "decoding error, ret = %" G_GSSIZE_FORMAT ", "
109 "written = %" G_GSSIZE_FORMAT, ret, written);
110 break;
Tim-Philipp Müllerfecddde2013-01-16 11:36:25 +0000111 }
Marcel Holtmann36f952b2007-08-25 17:03:03 +0000112 }
113
Tim-Philipp Müllerca9daee2013-03-26 13:55:32 +0000114 gst_buffer_unmap (outbuf, &out_map);
Marcel Holtmann36f952b2007-08-25 17:03:03 +0000115
Tim-Philipp Müllerca9daee2013-03-26 13:55:32 +0000116 if (i > 0)
117 gst_buffer_set_size (outbuf, i * dec->samples_per_frame * 2);
118 else
119 gst_buffer_replace (&outbuf, NULL);
Tim-Philipp Müllerfecddde2013-01-16 11:36:25 +0000120
Marcel Holtmann36f952b2007-08-25 17:03:03 +0000121done:
Tim-Philipp Müllerfecddde2013-01-16 11:36:25 +0000122
Tim-Philipp Müllerca9daee2013-03-26 13:55:32 +0000123 gst_buffer_unmap (buf, &in_map);
Marcel Holtmann36f952b2007-08-25 17:03:03 +0000124
Tim-Philipp Müllerca9daee2013-03-26 13:55:32 +0000125 return gst_audio_decoder_finish_frame (audio_dec, outbuf, 1);
126
127/* ERRORS */
128mixed_frames:
129 {
130 GST_WARNING_OBJECT (dec, "inconsistent input data/frames, skipping");
131 goto done;
132 }
133no_buffer:
134 {
135 GST_ERROR_OBJECT (dec, "could not allocate output buffer");
136 goto done;
137 }
Marcel Holtmann36f952b2007-08-25 17:03:03 +0000138}
139
Tim-Philipp Müllerca9daee2013-03-26 13:55:32 +0000140static gboolean
141gst_sbc_dec_set_format (GstAudioDecoder * audio_dec, GstCaps * caps)
Marcel Holtmann36f952b2007-08-25 17:03:03 +0000142{
Tim-Philipp Müllerca9daee2013-03-26 13:55:32 +0000143 GstSbcDec *dec = GST_SBC_DEC (audio_dec);
144 const gchar *channel_mode;
145 GstAudioInfo info;
146 GstStructure *s;
147 gint channels, rate, subbands, blocks, bitpool;
Marcel Holtmann36f952b2007-08-25 17:03:03 +0000148
Tim-Philipp Müllerca9daee2013-03-26 13:55:32 +0000149 s = gst_caps_get_structure (caps, 0);
150 gst_structure_get_int (s, "channels", &channels);
151 gst_structure_get_int (s, "rate", &rate);
152
153 /* save input format */
154 channel_mode = gst_structure_get_string (s, "channel-mode");
155 if (channel_mode == NULL ||
156 !gst_structure_get_int (s, "subbands", &subbands) ||
157 !gst_structure_get_int (s, "blocks", &blocks) ||
158 !gst_structure_get_int (s, "bitpool", &bitpool))
159 return FALSE;
160
161 if (strcmp (channel_mode, "mono") == 0) {
Tim Sheridan95a14fd2016-01-12 14:54:23 +0000162 dec->frame_len = 4 + (subbands * 1) / 2 + ((blocks * 1 * bitpool) + 7) / 8;
Tim-Philipp Müllerca9daee2013-03-26 13:55:32 +0000163 } else if (strcmp (channel_mode, "dual") == 0) {
Tim Sheridan95a14fd2016-01-12 14:54:23 +0000164 dec->frame_len = 4 + (subbands * 2) / 2 + ((blocks * 2 * bitpool) + 7) / 8;
Tim-Philipp Müllerca9daee2013-03-26 13:55:32 +0000165 } else if (strcmp (channel_mode, "stereo") == 0) {
Tim Sheridan95a14fd2016-01-12 14:54:23 +0000166 dec->frame_len = 4 + (subbands * 2) / 2 + ((blocks * bitpool) + 7) / 8;
Tim-Philipp Müllerca9daee2013-03-26 13:55:32 +0000167 } else if (strcmp (channel_mode, "joint") == 0) {
Vineeth TM8cdfb132016-03-04 15:50:26 +0900168 dec->frame_len =
169 4 + (subbands * 2) / 2 + ((subbands + blocks * bitpool) + 7) / 8;
Tim-Philipp Müllerca9daee2013-03-26 13:55:32 +0000170 } else {
171 return FALSE;
Wim Taymans9581e702012-10-29 12:37:09 +0000172 }
Marcel Holtmann36f952b2007-08-25 17:03:03 +0000173
Tim-Philipp Müllerca9daee2013-03-26 13:55:32 +0000174 dec->samples_per_frame = channels * blocks * subbands;
Wim Taymans9581e702012-10-29 12:37:09 +0000175
Tim-Philipp Müllerca9daee2013-03-26 13:55:32 +0000176 GST_INFO_OBJECT (dec, "frame len: %" G_GSIZE_FORMAT ", samples per frame "
177 "%" G_GSIZE_FORMAT, dec->frame_len, dec->samples_per_frame);
Marcel Holtmann36f952b2007-08-25 17:03:03 +0000178
Tim-Philipp Müllerca9daee2013-03-26 13:55:32 +0000179 /* set up output format */
180 gst_audio_info_init (&info);
181 gst_audio_info_set_format (&info, GST_AUDIO_FORMAT_S16, rate, channels, NULL);
182 gst_audio_decoder_set_output_format (audio_dec, &info);
Marcel Holtmann36f952b2007-08-25 17:03:03 +0000183
Tim-Philipp Müllerca9daee2013-03-26 13:55:32 +0000184 return TRUE;
185}
186
187static gboolean
188gst_sbc_dec_start (GstAudioDecoder * dec)
189{
190 GstSbcDec *sbcdec = GST_SBC_DEC (dec);
191
192 GST_INFO_OBJECT (dec, "Setup subband codec");
193 sbc_init (&sbcdec->sbc, 0);
194
195 return TRUE;
196}
197
198static gboolean
199gst_sbc_dec_stop (GstAudioDecoder * dec)
200{
201 GstSbcDec *sbcdec = GST_SBC_DEC (dec);
202
203 GST_INFO_OBJECT (sbcdec, "Finish subband codec");
204 sbc_finish (&sbcdec->sbc);
205 sbcdec->samples_per_frame = 0;
206 sbcdec->frame_len = 0;
207
208 return TRUE;
Marcel Holtmann36f952b2007-08-25 17:03:03 +0000209}
210
Marcel Holtmann974757b2007-08-23 19:12:23 +0000211static void
Tim-Philipp Müllerfecddde2013-01-16 11:36:25 +0000212gst_sbc_dec_class_init (GstSbcDecClass * klass)
Marcel Holtmann974757b2007-08-23 19:12:23 +0000213{
Tim-Philipp Müllerca9daee2013-03-26 13:55:32 +0000214 GstAudioDecoderClass *audio_decoder_class = (GstAudioDecoderClass *) klass;
215 GstElementClass *element_class = (GstElementClass *) klass;
Tim-Philipp Müllerfecddde2013-01-16 11:36:25 +0000216
Tim-Philipp Müllerca9daee2013-03-26 13:55:32 +0000217 audio_decoder_class->start = GST_DEBUG_FUNCPTR (gst_sbc_dec_start);
218 audio_decoder_class->stop = GST_DEBUG_FUNCPTR (gst_sbc_dec_stop);
219 audio_decoder_class->set_format = GST_DEBUG_FUNCPTR (gst_sbc_dec_set_format);
220 audio_decoder_class->handle_frame =
221 GST_DEBUG_FUNCPTR (gst_sbc_dec_handle_frame);
Marcel Holtmann974757b2007-08-23 19:12:23 +0000222
Vineeth TM8cdfb132016-03-04 15:50:26 +0900223 gst_element_class_add_static_pad_template (element_class,
224 &sbc_dec_sink_factory);
225 gst_element_class_add_static_pad_template (element_class,
226 &sbc_dec_src_factory);
Marcel Holtmann36f952b2007-08-25 17:03:03 +0000227
Tim-Philipp Müllerfecddde2013-01-16 11:36:25 +0000228 gst_element_class_set_static_metadata (element_class,
229 "Bluetooth SBC audio decoder", "Codec/Decoder/Audio",
230 "Decode an SBC audio stream", "Marcel Holtmann <marcel@holtmann.org>");
Marcel Holtmann36f952b2007-08-25 17:03:03 +0000231
Marcel Holtmann974757b2007-08-23 19:12:23 +0000232 GST_DEBUG_CATEGORY_INIT (sbc_dec_debug, "sbcdec", 0, "SBC decoding element");
233}
234
235static void
Tim-Philipp Müllerca9daee2013-03-26 13:55:32 +0000236gst_sbc_dec_init (GstSbcDec * dec)
Marcel Holtmann974757b2007-08-23 19:12:23 +0000237{
Sebastian Drögec635cb22013-12-05 12:05:30 +0100238 gst_audio_decoder_set_needs_format (GST_AUDIO_DECODER (dec), TRUE);
Thiago Santos56b822f2015-08-15 12:58:40 -0300239 gst_audio_decoder_set_use_default_pad_acceptcaps (GST_AUDIO_DECODER_CAST
240 (dec), TRUE);
241 GST_PAD_SET_ACCEPT_TEMPLATE (GST_AUDIO_DECODER_SINK_PAD (dec));
Sebastian Drögec635cb22013-12-05 12:05:30 +0100242
Tim-Philipp Müllerca9daee2013-03-26 13:55:32 +0000243 dec->samples_per_frame = 0;
244 dec->frame_len = 0;
Luiz Augusto von Dentz76c77b12008-01-23 13:14:02 +0000245}