Tim-Philipp Müller | fecddde | 2013-01-16 11:36:25 +0000 | [diff] [blame] | 1 | /* GStreamer SBC audio decoder |
Marcel Holtmann | 974757b | 2007-08-23 19:12:23 +0000 | [diff] [blame] | 2 | * |
Marcel Holtmann | 1ddbdc3 | 2010-01-01 17:08:17 -0800 | [diff] [blame] | 3 | * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org> |
Tim-Philipp Müller | ca9daee | 2013-03-26 13:55:32 +0000 | [diff] [blame] | 4 | * Copyright (C) 2013 Tim-Philipp Müller <tim centricular net> |
Marcel Holtmann | 974757b | 2007-08-23 19:12:23 +0000 | [diff] [blame] | 5 | * |
| 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üller | ca9daee | 2013-03-26 13:55:32 +0000 | [diff] [blame] | 22 | /** |
| 23 | * SECTION:element-sbdec |
Thibault Saunier | 78022a6 | 2017-03-08 15:01:13 -0300 | [diff] [blame] | 24 | * @title: sbdec |
Tim-Philipp Müller | ca9daee | 2013-03-26 13:55:32 +0000 | [diff] [blame] | 25 | * |
| 26 | * This element decodes a Bluetooth SBC audio streams to raw integer PCM audio |
| 27 | * |
Thibault Saunier | 78022a6 | 2017-03-08 15:01:13 -0300 | [diff] [blame] | 28 | * ## Example pipelines |
Tim-Philipp Müller | ca9daee | 2013-03-26 13:55:32 +0000 | [diff] [blame] | 29 | * |[ |
| 30 | * gst-launch-1.0 -v filesrc location=audio.sbc ! sbcparse ! sbcdec ! audioconvert ! audioresample ! autoaudiosink |
| 31 | * ]| Decode a raw SBC file. |
Thibault Saunier | 78022a6 | 2017-03-08 15:01:13 -0300 | [diff] [blame] | 32 | * |
Tim-Philipp Müller | ca9daee | 2013-03-26 13:55:32 +0000 | [diff] [blame] | 33 | */ |
| 34 | |
Marcel Holtmann | 974757b | 2007-08-23 19:12:23 +0000 | [diff] [blame] | 35 | #ifdef HAVE_CONFIG_H |
| 36 | #include <config.h> |
| 37 | #endif |
| 38 | |
Marcel Holtmann | 36f952b | 2007-08-25 17:03:03 +0000 | [diff] [blame] | 39 | #include <string.h> |
Marcel Holtmann | 974757b | 2007-08-23 19:12:23 +0000 | [diff] [blame] | 40 | |
| 41 | #include "gstsbcdec.h" |
| 42 | |
Tim-Philipp Müller | fecddde | 2013-01-16 11:36:25 +0000 | [diff] [blame] | 43 | /* FIXME: where does this come from? how is it derived? */ |
Wim Taymans | 91f4b15 | 2013-01-08 10:19:39 +0100 | [diff] [blame] | 44 | #define BUF_SIZE 8192 |
| 45 | |
Marcel Holtmann | 974757b | 2007-08-23 19:12:23 +0000 | [diff] [blame] | 46 | GST_DEBUG_CATEGORY_STATIC (sbc_dec_debug); |
| 47 | #define GST_CAT_DEFAULT sbc_dec_debug |
| 48 | |
Tim-Philipp Müller | fecddde | 2013-01-16 11:36:25 +0000 | [diff] [blame] | 49 | #define parent_class gst_sbc_dec_parent_class |
Tim-Philipp Müller | ca9daee | 2013-03-26 13:55:32 +0000 | [diff] [blame] | 50 | G_DEFINE_TYPE (GstSbcDec, gst_sbc_dec, GST_TYPE_AUDIO_DECODER); |
Marcel Holtmann | 974757b | 2007-08-23 19:12:23 +0000 | [diff] [blame] | 51 | |
Marcel Holtmann | 36f952b | 2007-08-25 17:03:03 +0000 | [diff] [blame] | 52 | static GstStaticPadTemplate sbc_dec_sink_factory = |
| 53 | GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, |
Tim-Philipp Müller | ca9daee | 2013-03-26 13:55:32 +0000 | [diff] [blame] | 54 | GST_STATIC_CAPS ("audio/x-sbc, channels = (int) [ 1, 2 ], " |
| 55 | "rate = (int) { 16000, 32000, 44100, 48000 }, " |
| 56 | "parsed = (boolean) true")); |
Marcel Holtmann | 36f952b | 2007-08-25 17:03:03 +0000 | [diff] [blame] | 57 | |
| 58 | static GstStaticPadTemplate sbc_dec_src_factory = |
| 59 | GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS, |
Tim-Philipp Müller | fecddde | 2013-01-16 11:36:25 +0000 | [diff] [blame] | 60 | GST_STATIC_CAPS ("audio/x-raw, format=" GST_AUDIO_NE (S16) ", " |
Marcel Holtmann | 7a231ad | 2007-08-25 17:37:05 +0000 | [diff] [blame] | 61 | "rate = (int) { 16000, 32000, 44100, 48000 }, " |
Tim-Philipp Müller | fecddde | 2013-01-16 11:36:25 +0000 | [diff] [blame] | 62 | "channels = (int) [ 1, 2 ], layout=interleaved")); |
Marcel Holtmann | 36f952b | 2007-08-25 17:03:03 +0000 | [diff] [blame] | 63 | |
| 64 | static GstFlowReturn |
Tim-Philipp Müller | ca9daee | 2013-03-26 13:55:32 +0000 | [diff] [blame] | 65 | gst_sbc_dec_handle_frame (GstAudioDecoder * audio_dec, GstBuffer * buf) |
Wim Taymans | 91f4b15 | 2013-01-08 10:19:39 +0100 | [diff] [blame] | 66 | { |
Tim-Philipp Müller | ca9daee | 2013-03-26 13:55:32 +0000 | [diff] [blame] | 67 | GstSbcDec *dec = GST_SBC_DEC (audio_dec); |
| 68 | GstBuffer *outbuf = NULL; |
Tim-Philipp Müller | fecddde | 2013-01-16 11:36:25 +0000 | [diff] [blame] | 69 | GstMapInfo out_map; |
Tim-Philipp Müller | ca9daee | 2013-03-26 13:55:32 +0000 | [diff] [blame] | 70 | GstMapInfo in_map; |
| 71 | gsize output_size; |
| 72 | guint num_frames, i; |
Marcel Holtmann | 36f952b | 2007-08-25 17:03:03 +0000 | [diff] [blame] | 73 | |
Tim-Philipp Müller | ca9daee | 2013-03-26 13:55:32 +0000 | [diff] [blame] | 74 | /* no fancy draining */ |
| 75 | if (G_UNLIKELY (buf == NULL)) |
| 76 | return GST_FLOW_OK; |
Wim Taymans | beaeeaf | 2012-10-29 17:19:50 +0000 | [diff] [blame] | 77 | |
Tim-Philipp Müller | ca9daee | 2013-03-26 13:55:32 +0000 | [diff] [blame] | 78 | gst_buffer_map (buf, &in_map, GST_MAP_READ); |
Wim Taymans | 91f4b15 | 2013-01-08 10:19:39 +0100 | [diff] [blame] | 79 | |
Tim-Philipp Müller | ca9daee | 2013-03-26 13:55:32 +0000 | [diff] [blame] | 80 | if (G_UNLIKELY (in_map.size == 0)) |
| 81 | goto done; |
Wim Taymans | beaeeaf | 2012-10-29 17:19:50 +0000 | [diff] [blame] | 82 | |
Tim-Philipp Müller | ca9daee | 2013-03-26 13:55:32 +0000 | [diff] [blame] | 83 | /* 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 Holtmann | 36f952b | 2007-08-25 17:03:03 +0000 | [diff] [blame] | 88 | |
Tim-Philipp Müller | ca9daee | 2013-03-26 13:55:32 +0000 | [diff] [blame] | 89 | num_frames = in_map.size / dec->frame_len; |
| 90 | output_size = num_frames * dec->samples_per_frame * sizeof (gint16); |
Marcel Holtmann | 36f952b | 2007-08-25 17:03:03 +0000 | [diff] [blame] | 91 | |
Tim-Philipp Müller | ca9daee | 2013-03-26 13:55:32 +0000 | [diff] [blame] | 92 | outbuf = gst_audio_decoder_allocate_output_buffer (audio_dec, output_size); |
Wim Taymans | beaeeaf | 2012-10-29 17:19:50 +0000 | [diff] [blame] | 93 | |
Tim-Philipp Müller | ca9daee | 2013-03-26 13:55:32 +0000 | [diff] [blame] | 94 | if (outbuf == NULL) |
| 95 | goto no_buffer; |
Wim Taymans | 91f4b15 | 2013-01-08 10:19:39 +0100 | [diff] [blame] | 96 | |
Tim-Philipp Müller | ca9daee | 2013-03-26 13:55:32 +0000 | [diff] [blame] | 97 | gst_buffer_map (outbuf, &out_map, GST_MAP_WRITE); |
Tim-Philipp Müller | fecddde | 2013-01-16 11:36:25 +0000 | [diff] [blame] | 98 | |
Tim-Philipp Müller | ca9daee | 2013-03-26 13:55:32 +0000 | [diff] [blame] | 99 | for (i = 0; i < num_frames; ++i) { |
| 100 | gssize ret; |
| 101 | gsize written; |
Wim Taymans | 91f4b15 | 2013-01-08 10:19:39 +0100 | [diff] [blame] | 102 | |
Tim-Philipp Müller | ca9daee | 2013-03-26 13:55:32 +0000 | [diff] [blame] | 103 | 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 Taymans | 91f4b15 | 2013-01-08 10:19:39 +0100 | [diff] [blame] | 106 | |
Tim-Philipp Müller | ca9daee | 2013-03-26 13:55:32 +0000 | [diff] [blame] | 107 | 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üller | fecddde | 2013-01-16 11:36:25 +0000 | [diff] [blame] | 111 | } |
Marcel Holtmann | 36f952b | 2007-08-25 17:03:03 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Tim-Philipp Müller | ca9daee | 2013-03-26 13:55:32 +0000 | [diff] [blame] | 114 | gst_buffer_unmap (outbuf, &out_map); |
Marcel Holtmann | 36f952b | 2007-08-25 17:03:03 +0000 | [diff] [blame] | 115 | |
Tim-Philipp Müller | ca9daee | 2013-03-26 13:55:32 +0000 | [diff] [blame] | 116 | 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üller | fecddde | 2013-01-16 11:36:25 +0000 | [diff] [blame] | 120 | |
Marcel Holtmann | 36f952b | 2007-08-25 17:03:03 +0000 | [diff] [blame] | 121 | done: |
Tim-Philipp Müller | fecddde | 2013-01-16 11:36:25 +0000 | [diff] [blame] | 122 | |
Tim-Philipp Müller | ca9daee | 2013-03-26 13:55:32 +0000 | [diff] [blame] | 123 | gst_buffer_unmap (buf, &in_map); |
Marcel Holtmann | 36f952b | 2007-08-25 17:03:03 +0000 | [diff] [blame] | 124 | |
Tim-Philipp Müller | ca9daee | 2013-03-26 13:55:32 +0000 | [diff] [blame] | 125 | return gst_audio_decoder_finish_frame (audio_dec, outbuf, 1); |
| 126 | |
| 127 | /* ERRORS */ |
| 128 | mixed_frames: |
| 129 | { |
| 130 | GST_WARNING_OBJECT (dec, "inconsistent input data/frames, skipping"); |
| 131 | goto done; |
| 132 | } |
| 133 | no_buffer: |
| 134 | { |
| 135 | GST_ERROR_OBJECT (dec, "could not allocate output buffer"); |
| 136 | goto done; |
| 137 | } |
Marcel Holtmann | 36f952b | 2007-08-25 17:03:03 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Tim-Philipp Müller | ca9daee | 2013-03-26 13:55:32 +0000 | [diff] [blame] | 140 | static gboolean |
| 141 | gst_sbc_dec_set_format (GstAudioDecoder * audio_dec, GstCaps * caps) |
Marcel Holtmann | 36f952b | 2007-08-25 17:03:03 +0000 | [diff] [blame] | 142 | { |
Tim-Philipp Müller | ca9daee | 2013-03-26 13:55:32 +0000 | [diff] [blame] | 143 | 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 Holtmann | 36f952b | 2007-08-25 17:03:03 +0000 | [diff] [blame] | 148 | |
Tim-Philipp Müller | ca9daee | 2013-03-26 13:55:32 +0000 | [diff] [blame] | 149 | 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 Sheridan | 95a14fd | 2016-01-12 14:54:23 +0000 | [diff] [blame] | 162 | dec->frame_len = 4 + (subbands * 1) / 2 + ((blocks * 1 * bitpool) + 7) / 8; |
Tim-Philipp Müller | ca9daee | 2013-03-26 13:55:32 +0000 | [diff] [blame] | 163 | } else if (strcmp (channel_mode, "dual") == 0) { |
Tim Sheridan | 95a14fd | 2016-01-12 14:54:23 +0000 | [diff] [blame] | 164 | dec->frame_len = 4 + (subbands * 2) / 2 + ((blocks * 2 * bitpool) + 7) / 8; |
Tim-Philipp Müller | ca9daee | 2013-03-26 13:55:32 +0000 | [diff] [blame] | 165 | } else if (strcmp (channel_mode, "stereo") == 0) { |
Tim Sheridan | 95a14fd | 2016-01-12 14:54:23 +0000 | [diff] [blame] | 166 | dec->frame_len = 4 + (subbands * 2) / 2 + ((blocks * bitpool) + 7) / 8; |
Tim-Philipp Müller | ca9daee | 2013-03-26 13:55:32 +0000 | [diff] [blame] | 167 | } else if (strcmp (channel_mode, "joint") == 0) { |
Vineeth TM | 8cdfb13 | 2016-03-04 15:50:26 +0900 | [diff] [blame] | 168 | dec->frame_len = |
| 169 | 4 + (subbands * 2) / 2 + ((subbands + blocks * bitpool) + 7) / 8; |
Tim-Philipp Müller | ca9daee | 2013-03-26 13:55:32 +0000 | [diff] [blame] | 170 | } else { |
| 171 | return FALSE; |
Wim Taymans | 9581e70 | 2012-10-29 12:37:09 +0000 | [diff] [blame] | 172 | } |
Marcel Holtmann | 36f952b | 2007-08-25 17:03:03 +0000 | [diff] [blame] | 173 | |
Tim-Philipp Müller | ca9daee | 2013-03-26 13:55:32 +0000 | [diff] [blame] | 174 | dec->samples_per_frame = channels * blocks * subbands; |
Wim Taymans | 9581e70 | 2012-10-29 12:37:09 +0000 | [diff] [blame] | 175 | |
Tim-Philipp Müller | ca9daee | 2013-03-26 13:55:32 +0000 | [diff] [blame] | 176 | GST_INFO_OBJECT (dec, "frame len: %" G_GSIZE_FORMAT ", samples per frame " |
| 177 | "%" G_GSIZE_FORMAT, dec->frame_len, dec->samples_per_frame); |
Marcel Holtmann | 36f952b | 2007-08-25 17:03:03 +0000 | [diff] [blame] | 178 | |
Tim-Philipp Müller | ca9daee | 2013-03-26 13:55:32 +0000 | [diff] [blame] | 179 | /* 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 Holtmann | 36f952b | 2007-08-25 17:03:03 +0000 | [diff] [blame] | 183 | |
Tim-Philipp Müller | ca9daee | 2013-03-26 13:55:32 +0000 | [diff] [blame] | 184 | return TRUE; |
| 185 | } |
| 186 | |
| 187 | static gboolean |
| 188 | gst_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 | |
| 198 | static gboolean |
| 199 | gst_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 Holtmann | 36f952b | 2007-08-25 17:03:03 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Marcel Holtmann | 974757b | 2007-08-23 19:12:23 +0000 | [diff] [blame] | 211 | static void |
Tim-Philipp Müller | fecddde | 2013-01-16 11:36:25 +0000 | [diff] [blame] | 212 | gst_sbc_dec_class_init (GstSbcDecClass * klass) |
Marcel Holtmann | 974757b | 2007-08-23 19:12:23 +0000 | [diff] [blame] | 213 | { |
Tim-Philipp Müller | ca9daee | 2013-03-26 13:55:32 +0000 | [diff] [blame] | 214 | GstAudioDecoderClass *audio_decoder_class = (GstAudioDecoderClass *) klass; |
| 215 | GstElementClass *element_class = (GstElementClass *) klass; |
Tim-Philipp Müller | fecddde | 2013-01-16 11:36:25 +0000 | [diff] [blame] | 216 | |
Tim-Philipp Müller | ca9daee | 2013-03-26 13:55:32 +0000 | [diff] [blame] | 217 | 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 Holtmann | 974757b | 2007-08-23 19:12:23 +0000 | [diff] [blame] | 222 | |
Vineeth TM | 8cdfb13 | 2016-03-04 15:50:26 +0900 | [diff] [blame] | 223 | 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 Holtmann | 36f952b | 2007-08-25 17:03:03 +0000 | [diff] [blame] | 227 | |
Tim-Philipp Müller | fecddde | 2013-01-16 11:36:25 +0000 | [diff] [blame] | 228 | 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 Holtmann | 36f952b | 2007-08-25 17:03:03 +0000 | [diff] [blame] | 231 | |
Marcel Holtmann | 974757b | 2007-08-23 19:12:23 +0000 | [diff] [blame] | 232 | GST_DEBUG_CATEGORY_INIT (sbc_dec_debug, "sbcdec", 0, "SBC decoding element"); |
| 233 | } |
| 234 | |
| 235 | static void |
Tim-Philipp Müller | ca9daee | 2013-03-26 13:55:32 +0000 | [diff] [blame] | 236 | gst_sbc_dec_init (GstSbcDec * dec) |
Marcel Holtmann | 974757b | 2007-08-23 19:12:23 +0000 | [diff] [blame] | 237 | { |
Sebastian Dröge | c635cb2 | 2013-12-05 12:05:30 +0100 | [diff] [blame] | 238 | gst_audio_decoder_set_needs_format (GST_AUDIO_DECODER (dec), TRUE); |
Thiago Santos | 56b822f | 2015-08-15 12:58:40 -0300 | [diff] [blame] | 239 | 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öge | c635cb2 | 2013-12-05 12:05:30 +0100 | [diff] [blame] | 242 | |
Tim-Philipp Müller | ca9daee | 2013-03-26 13:55:32 +0000 | [diff] [blame] | 243 | dec->samples_per_frame = 0; |
| 244 | dec->frame_len = 0; |
Luiz Augusto von Dentz | 76c77b1 | 2008-01-23 13:14:02 +0000 | [diff] [blame] | 245 | } |