1 /*  GStreamer SBC audio encoder
2  *  Copyright (C) 2004-2010  Marcel Holtmann <marcel@holtmann.org>
3  *  Copyright (C) 2013       Tim-Philipp Müller <tim centricular net>
4  *
5  *  This library is free software; you can redistribute it and/or
6  *  modify it under the terms of the GNU Lesser General Public
7  *  License as published by the Free Software Foundation; either
8  *  version 2.1 of the License, or (at your option) any later version.
9  *
10  *  This library is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  *  Lesser General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Lesser General Public
16  *  License along with this library; if not, write to the Free Software
17  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  */
20 
21 /**
22  * SECTION:element-sbenc
23  * @title: sbenc
24  *
25  * This element encodes raw integer PCM audio into a Bluetooth SBC audio.
26  *
27  * Encoding parameters such as blocks, subbands, bitpool, channel-mode, and
28  * allocation-mode can be set by adding a capsfilter element with appropriate
29  * filtercaps after the sbcenc encoder element.
30  *
31  * ## Example pipelines
32  * |[
33  * gst-launch-1.0 -v audiotestsrc ! sbcenc ! rtpsbcpay ! udpsink
34  * ]| Encode a sine wave into SBC, RTP payload it and send over the network using UDP
35  *
36  */
37 
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
41 
42 #include <string.h>
43 
44 #include "gstsbcenc.h"
45 
46 GST_DEBUG_CATEGORY_STATIC (sbc_enc_debug);
47 #define GST_CAT_DEFAULT sbc_enc_debug
48 
49 G_DEFINE_TYPE (GstSbcEnc, gst_sbc_enc, GST_TYPE_AUDIO_ENCODER);
50 
51 static GstStaticPadTemplate sbc_enc_sink_factory =
52 GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
53     GST_STATIC_CAPS ("audio/x-raw, format=" GST_AUDIO_NE (S16) ", "
54         "rate = (int) { 16000, 32000, 44100, 48000 }, "
55         "channels = (int) [ 1, 2 ]"));
56 
57 static GstStaticPadTemplate sbc_enc_src_factory =
58 GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
59     GST_STATIC_CAPS ("audio/x-sbc, "
60         "rate = (int) { 16000, 32000, 44100, 48000 }, "
61         "channels = (int) [ 1, 2 ], "
62         "channel-mode = (string) { mono, dual, stereo, joint }, "
63         "blocks = (int) { 4, 8, 12, 16 }, "
64         "subbands = (int) { 4, 8 }, "
65         "allocation-method = (string) { snr, loudness }, "
66         "bitpool = (int) [ 2, 64 ]"));
67 
68 
69 static gboolean gst_sbc_enc_start (GstAudioEncoder * enc);
70 static gboolean gst_sbc_enc_stop (GstAudioEncoder * enc);
71 static gboolean gst_sbc_enc_set_format (GstAudioEncoder * enc,
72     GstAudioInfo * info);
73 static GstFlowReturn gst_sbc_enc_handle_frame (GstAudioEncoder * enc,
74     GstBuffer * buffer);
75 
76 static gboolean
gst_sbc_enc_set_format(GstAudioEncoder * audio_enc,GstAudioInfo * info)77 gst_sbc_enc_set_format (GstAudioEncoder * audio_enc, GstAudioInfo * info)
78 {
79   const gchar *allocation_method, *channel_mode;
80   GstSbcEnc *enc = GST_SBC_ENC (audio_enc);
81   GstStructure *s;
82   GstCaps *caps, *filter_caps;
83   GstCaps *output_caps = NULL;
84   guint sampleframes_per_frame;
85 
86   enc->rate = GST_AUDIO_INFO_RATE (info);
87   enc->channels = GST_AUDIO_INFO_CHANNELS (info);
88 
89   /* negotiate output format based on downstream caps restrictions */
90   caps = gst_pad_get_allowed_caps (GST_AUDIO_ENCODER_SRC_PAD (enc));
91   if (caps == GST_CAPS_NONE || gst_caps_is_empty (caps))
92     goto failure;
93 
94   if (caps == NULL)
95     caps = gst_static_pad_template_get_caps (&sbc_enc_src_factory);
96 
97   /* fixate output caps */
98   filter_caps = gst_caps_new_simple ("audio/x-sbc", "rate", G_TYPE_INT,
99       enc->rate, "channels", G_TYPE_INT, enc->channels, NULL);
100   output_caps = gst_caps_intersect (caps, filter_caps);
101   gst_caps_unref (filter_caps);
102 
103   if (output_caps == NULL || gst_caps_is_empty (output_caps)) {
104     GST_WARNING_OBJECT (enc, "Couldn't negotiate output caps with input rate "
105         "%d and input channels %d and allowed output caps %" GST_PTR_FORMAT,
106         enc->rate, enc->channels, caps);
107     goto failure;
108   }
109 
110   gst_caps_unref (caps);
111   caps = NULL;
112 
113   GST_DEBUG_OBJECT (enc, "fixating caps %" GST_PTR_FORMAT, output_caps);
114   output_caps = gst_caps_truncate (output_caps);
115   s = gst_caps_get_structure (output_caps, 0);
116   if (enc->channels == 1)
117     gst_structure_fixate_field_string (s, "channel-mode", "mono");
118   else
119     gst_structure_fixate_field_string (s, "channel-mode", "joint");
120 
121   gst_structure_fixate_field_nearest_int (s, "bitpool", 64);
122   gst_structure_fixate_field_nearest_int (s, "blocks", 16);
123   gst_structure_fixate_field_nearest_int (s, "subbands", 8);
124   gst_structure_fixate_field_string (s, "allocation-method", "loudness");
125   s = NULL;
126 
127   /* in case there's anything else left to fixate */
128   output_caps = gst_caps_fixate (output_caps);
129   gst_caps_set_simple (output_caps, "parsed", G_TYPE_BOOLEAN, TRUE, NULL);
130 
131   GST_INFO_OBJECT (enc, "output caps %" GST_PTR_FORMAT, output_caps);
132 
133   /* let's see what we fixated to */
134   s = gst_caps_get_structure (output_caps, 0);
135   gst_structure_get_int (s, "blocks", &enc->blocks);
136   gst_structure_get_int (s, "subbands", &enc->subbands);
137   gst_structure_get_int (s, "bitpool", &enc->bitpool);
138   allocation_method = gst_structure_get_string (s, "allocation-method");
139   channel_mode = gst_structure_get_string (s, "channel-mode");
140 
141   /* We want channel-mode and channels coherent */
142   if (enc->channels == 1) {
143     if (g_strcmp0 (channel_mode, "mono") != 0) {
144       GST_ERROR_OBJECT (enc, "Can't have channel-mode '%s' for 1 channel",
145           channel_mode);
146       goto failure;
147     }
148   } else {
149     if (g_strcmp0 (channel_mode, "joint") != 0 &&
150         g_strcmp0 (channel_mode, "stereo") != 0 &&
151         g_strcmp0 (channel_mode, "dual") != 0) {
152       GST_ERROR_OBJECT (enc, "Can't have channel-mode '%s' for 2 channels",
153           channel_mode);
154       goto failure;
155     }
156   }
157 
158   /* we want to be handed all available samples in handle_frame, but always
159    * enough to encode a frame */
160   sampleframes_per_frame = enc->blocks * enc->subbands;
161   gst_audio_encoder_set_frame_samples_min (audio_enc, sampleframes_per_frame);
162   gst_audio_encoder_set_frame_samples_max (audio_enc, sampleframes_per_frame);
163   gst_audio_encoder_set_frame_max (audio_enc, 0);
164 
165   /* FIXME: what to do with left-over samples at the end? can we encode them? */
166   gst_audio_encoder_set_hard_min (audio_enc, TRUE);
167 
168   /* and configure encoder based on the output caps we negotiated */
169   if (enc->rate == 16000)
170     enc->sbc.frequency = SBC_FREQ_16000;
171   else if (enc->rate == 32000)
172     enc->sbc.frequency = SBC_FREQ_32000;
173   else if (enc->rate == 44100)
174     enc->sbc.frequency = SBC_FREQ_44100;
175   else if (enc->rate == 48000)
176     enc->sbc.frequency = SBC_FREQ_48000;
177   else
178     goto failure;
179 
180   if (enc->blocks == 4)
181     enc->sbc.blocks = SBC_BLK_4;
182   else if (enc->blocks == 8)
183     enc->sbc.blocks = SBC_BLK_8;
184   else if (enc->blocks == 12)
185     enc->sbc.blocks = SBC_BLK_12;
186   else if (enc->blocks == 16)
187     enc->sbc.blocks = SBC_BLK_16;
188   else
189     goto failure;
190 
191   enc->sbc.subbands = (enc->subbands == 4) ? SBC_SB_4 : SBC_SB_8;
192   enc->sbc.bitpool = enc->bitpool;
193 
194   if (channel_mode == NULL || allocation_method == NULL)
195     goto failure;
196 
197   if (strcmp (channel_mode, "joint") == 0)
198     enc->sbc.mode = SBC_MODE_JOINT_STEREO;
199   else if (strcmp (channel_mode, "stereo") == 0)
200     enc->sbc.mode = SBC_MODE_STEREO;
201   else if (strcmp (channel_mode, "dual") == 0)
202     enc->sbc.mode = SBC_MODE_DUAL_CHANNEL;
203   else if (strcmp (channel_mode, "mono") == 0)
204     enc->sbc.mode = SBC_MODE_MONO;
205   else if (strcmp (channel_mode, "auto") == 0)
206     enc->sbc.mode = SBC_MODE_JOINT_STEREO;
207   else
208     goto failure;
209 
210   if (strcmp (allocation_method, "loudness") == 0)
211     enc->sbc.allocation = SBC_AM_LOUDNESS;
212   else if (strcmp (allocation_method, "snr") == 0)
213     enc->sbc.allocation = SBC_AM_SNR;
214   else
215     goto failure;
216 
217   if (!gst_audio_encoder_set_output_format (audio_enc, output_caps))
218     goto failure;
219 
220   return gst_audio_encoder_negotiate (audio_enc);
221 
222 failure:
223   if (output_caps)
224     gst_caps_unref (output_caps);
225   if (caps)
226     gst_caps_unref (caps);
227   return FALSE;
228 }
229 
230 static GstFlowReturn
gst_sbc_enc_handle_frame(GstAudioEncoder * audio_enc,GstBuffer * buffer)231 gst_sbc_enc_handle_frame (GstAudioEncoder * audio_enc, GstBuffer * buffer)
232 {
233   GstSbcEnc *enc = GST_SBC_ENC (audio_enc);
234   GstMapInfo in_map, out_map;
235   GstBuffer *outbuf = NULL;
236   guint samples_per_frame, frames, i = 0;
237 
238   /* no fancy draining */
239   if (buffer == NULL)
240     return GST_FLOW_OK;
241 
242   if (G_UNLIKELY (enc->channels == 0 || enc->blocks == 0 || enc->subbands == 0))
243     return GST_FLOW_NOT_NEGOTIATED;
244 
245   samples_per_frame = enc->channels * enc->blocks * enc->subbands;
246 
247   if (!gst_buffer_map (buffer, &in_map, GST_MAP_READ))
248     goto map_failed;
249 
250   frames = in_map.size / (samples_per_frame * sizeof (gint16));
251 
252   GST_LOG_OBJECT (enc,
253       "encoding %" G_GSIZE_FORMAT " samples into %u SBC frames",
254       in_map.size / (enc->channels * sizeof (gint16)), frames);
255 
256   if (frames > 0) {
257     gsize frame_len;
258 
259     frame_len = sbc_get_frame_length (&enc->sbc);
260     outbuf = gst_audio_encoder_allocate_output_buffer (audio_enc,
261         frames * frame_len);
262 
263     if (outbuf == NULL)
264       goto no_buffer;
265 
266     gst_buffer_map (outbuf, &out_map, GST_MAP_WRITE);
267 
268     for (i = 0; i < frames; ++i) {
269       gssize ret, written = 0;
270 
271       ret = sbc_encode (&enc->sbc, in_map.data + (i * samples_per_frame * 2),
272           samples_per_frame * 2, out_map.data + (i * frame_len), frame_len,
273           &written);
274 
275       if (ret < 0 || written != frame_len) {
276         GST_WARNING_OBJECT (enc, "encoding error, ret = %" G_GSSIZE_FORMAT ", "
277             "written = %" G_GSSIZE_FORMAT, ret, written);
278         break;
279       }
280     }
281 
282     gst_buffer_unmap (outbuf, &out_map);
283 
284     if (i > 0)
285       gst_buffer_set_size (outbuf, i * frame_len);
286     else
287       gst_buffer_replace (&outbuf, NULL);
288   }
289 
290 done:
291 
292   gst_buffer_unmap (buffer, &in_map);
293 
294   return gst_audio_encoder_finish_frame (audio_enc, outbuf,
295       i * (samples_per_frame / enc->channels));
296 
297 /* ERRORS */
298 no_buffer:
299   {
300     GST_ERROR_OBJECT (enc, "could not allocate output buffer");
301     goto done;
302   }
303 map_failed:
304   {
305     GST_ERROR_OBJECT (enc, "could not map input buffer");
306     goto done;
307   }
308 }
309 
310 static gboolean
gst_sbc_enc_start(GstAudioEncoder * audio_enc)311 gst_sbc_enc_start (GstAudioEncoder * audio_enc)
312 {
313   GstSbcEnc *enc = GST_SBC_ENC (audio_enc);
314 
315   GST_INFO_OBJECT (enc, "Setup subband codec");
316   sbc_init (&enc->sbc, 0);
317 
318   return TRUE;
319 }
320 
321 static gboolean
gst_sbc_enc_stop(GstAudioEncoder * audio_enc)322 gst_sbc_enc_stop (GstAudioEncoder * audio_enc)
323 {
324   GstSbcEnc *enc = GST_SBC_ENC (audio_enc);
325 
326   GST_INFO_OBJECT (enc, "Finish subband codec");
327   sbc_finish (&enc->sbc);
328 
329   enc->subbands = 0;
330   enc->blocks = 0;
331   enc->rate = 0;
332   enc->channels = 0;
333   enc->bitpool = 0;
334 
335   return TRUE;
336 }
337 
338 static void
gst_sbc_enc_class_init(GstSbcEncClass * klass)339 gst_sbc_enc_class_init (GstSbcEncClass * klass)
340 {
341   GstAudioEncoderClass *encoder_class = GST_AUDIO_ENCODER_CLASS (klass);
342   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
343 
344   encoder_class->start = GST_DEBUG_FUNCPTR (gst_sbc_enc_start);
345   encoder_class->stop = GST_DEBUG_FUNCPTR (gst_sbc_enc_stop);
346   encoder_class->set_format = GST_DEBUG_FUNCPTR (gst_sbc_enc_set_format);
347   encoder_class->handle_frame = GST_DEBUG_FUNCPTR (gst_sbc_enc_handle_frame);
348 
349   gst_element_class_add_static_pad_template (element_class,
350       &sbc_enc_sink_factory);
351   gst_element_class_add_static_pad_template (element_class,
352       &sbc_enc_src_factory);
353 
354   gst_element_class_set_static_metadata (element_class,
355       "Bluetooth SBC audio encoder", "Codec/Encoder/Audio",
356       "Encode an SBC audio stream", "Marcel Holtmann <marcel@holtmann.org>");
357 
358   GST_DEBUG_CATEGORY_INIT (sbc_enc_debug, "sbcenc", 0, "SBC encoding element");
359 }
360 
361 static void
gst_sbc_enc_init(GstSbcEnc * self)362 gst_sbc_enc_init (GstSbcEnc * self)
363 {
364   GST_PAD_SET_ACCEPT_TEMPLATE (GST_AUDIO_ENCODER_SINK_PAD (self));
365   self->subbands = 0;
366   self->blocks = 0;
367   self->rate = 0;
368   self->channels = 0;
369   self->bitpool = 0;
370 }
371