1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
|
/*
*
* BlueZ - Bluetooth protocol stack for Linux
*
* Copyright (C) 2004-2007 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 "gstsbcenc.h"
#include "gstsbcutil.h"
#define SBC_ENC_DEFAULT_MODE CFG_MODE_AUTO
#define SBC_ENC_DEFAULT_BLOCKS 16
#define SBC_ENC_DEFAULT_SUB_BANDS 8
#define SBC_ENC_DEFAULT_BITPOOL 53
#define SBC_ENC_DEFAULT_ALLOCATION CFG_ALLOCATION_AUTO
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[] = {
{ 0, "Auto", "auto" },
{ 1, "Mono", "mono" },
{ 2, "Dual Channel", "dual" },
{ 3, "Stereo", "stereo" },
{ 4, "Joint Stereo", "joint" },
{ -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[] = {
{ CFG_ALLOCATION_AUTO, "Auto", "auto" },
{ CFG_ALLOCATION_LOUDNESS, "Loudness", "loudness" },
{ CFG_ALLOCATION_SNR, "SNR", "snr" },
{ -1, NULL, NULL}
};
if (!sbc_allocation_type)
sbc_allocation_type = g_enum_register_static(
"GstSbcAllocation", sbc_allocations);
return sbc_allocation_type;
}
enum {
PROP_0,
PROP_MODE,
PROP_ALLOCATION,
PROP_BLOCKS,
PROP_SUBBANDS
};
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) LITTLE_ENDIAN, "
"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) [ 2, 64 ]"));
static GstCaps* sbc_enc_generate_srcpad_caps(GstSbcEnc *enc, GstCaps *caps)
{
gint rate;
gint channels;
GstCaps* src_caps;
GstStructure *structure;
const gchar *mode;
const gchar *allocation;
structure = gst_caps_get_structure(caps, 0);
if (!gst_structure_get_int (structure, "rate", &rate))
return NULL;
if (!gst_structure_get_int (structure, "channels", &channels))
return NULL;
enc->sbc.rate = rate;
enc->sbc.channels = channels;
if (enc->mode == CFG_MODE_AUTO)
enc->mode = CFG_MODE_JOINT_STEREO;
if (enc->mode == CFG_MODE_MONO || enc->mode == CFG_MODE_JOINT_STEREO)
enc->sbc.joint = 1;
enc->sbc.blocks = enc->blocks;
enc->sbc.subbands = enc->subbands;
if (enc->allocation == 0)
enc->sbc.allocation = CFG_ALLOCATION_LOUDNESS;
else
enc->sbc.allocation = enc->allocation;
enc->sbc.bitpool = SBC_ENC_DEFAULT_BITPOOL;
mode = gst_sbc_get_mode_string(enc->sbc.joint);
allocation = gst_sbc_get_allocation_string(enc->sbc.allocation);
src_caps = gst_caps_new_simple("audio/x-sbc",
"rate", G_TYPE_INT, enc->sbc.rate,
"channels", G_TYPE_INT, enc->sbc.channels,
"mode", G_TYPE_STRING, mode,
"subbands", G_TYPE_INT, enc->sbc.subbands,
"blocks", G_TYPE_INT, enc->sbc.blocks,
"allocation", G_TYPE_STRING, allocation,
"bitpool", G_TYPE_INT, enc->sbc.bitpool,
NULL);
return src_caps;
}
static gboolean sbc_enc_sink_setcaps (GstPad * pad, GstCaps * caps)
{
GstSbcEnc *enc;
GstStructure *structure;
GstCaps *othercaps;
enc = GST_SBC_ENC(GST_PAD_PARENT (pad));
structure = gst_caps_get_structure(caps, 0);
othercaps = sbc_enc_generate_srcpad_caps(enc, caps);
if (othercaps == NULL)
goto error;
gst_pad_set_caps (enc->srcpad, othercaps);
gst_caps_unref(othercaps);
return TRUE;
error:
GST_ERROR_OBJECT (enc, "invalid input caps");
return FALSE;
}
gboolean gst_sbc_enc_fill_sbc_params(GstSbcEnc *enc, 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;
sbc_finish(&enc->sbc);
sbc_init(&enc->sbc, 0);
enc->sbc.rate = rate;
enc->sbc.channels = channels;
enc->blocks = blocks;
enc->sbc.subbands = subbands;
enc->sbc.bitpool = bitpool;
enc->mode = gst_sbc_get_mode_int(mode);
enc->allocation = gst_sbc_get_allocation_mode_int(allocation);
return TRUE;
}
static gboolean gst_sbc_enc_change_caps(GstSbcEnc *enc, GstCaps *caps)
{
GST_INFO_OBJECT(enc, "Changing srcpad caps (renegotiation)");
if (!gst_pad_accept_caps(enc->srcpad, caps)) {
GST_WARNING_OBJECT(enc, "Src pad refused caps");
return FALSE;
}
if (!gst_sbc_enc_fill_sbc_params(enc, caps)) {
GST_ERROR_OBJECT(enc, "couldn't get sbc parameters from caps");
return FALSE;
}
return TRUE;
}
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;
gint codesize, frame_len;
codesize = sbc_get_codesize(&enc->sbc);
frame_len = sbc_get_frame_length(&enc->sbc);
gst_adapter_push(adapter, buffer);
while (gst_adapter_available(adapter) >= codesize && res == GST_FLOW_OK) {
GstBuffer *output;
GstCaps *caps;
const guint8 *data;
int consumed;
caps = GST_PAD_CAPS(enc->srcpad);
res = gst_pad_alloc_buffer_and_set_caps(enc->srcpad,
GST_BUFFER_OFFSET_NONE,
frame_len, caps, &output);
data = gst_adapter_peek(adapter, codesize);
consumed = sbc_encode(&enc->sbc, (gpointer) data, codesize,
GST_BUFFER_DATA(output), frame_len,
NULL);
if (consumed <= 0) {
GST_ERROR ("comsumed < 0, codesize: %d", codesize);
break;
}
gst_adapter_flush(adapter, consumed);
if (res != GST_FLOW_OK)
goto done;
if (!gst_caps_is_equal(caps, GST_BUFFER_CAPS(output)))
if (!gst_sbc_enc_change_caps(enc,
GST_BUFFER_CAPS(output))) {
res = GST_FLOW_ERROR;
GST_ERROR_OBJECT(enc, "couldn't renegotiate caps");
goto done;
}
GST_BUFFER_TIMESTAMP(output) = GST_BUFFER_TIMESTAMP(buffer);
res = gst_pad_push(enc->srcpad, output);
if (res != GST_FLOW_OK) {
GST_ERROR_OBJECT(enc, "pad pushing failed");
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);
/* TODO - CAN ONLY BE CHANGED ON READY AND BELOW */
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:
/* TODO - verify consistency */
enc->blocks = g_value_get_int(value);
break;
case PROP_SUBBANDS:
/* TODO - verify consistency */
enc->subbands = 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_int(value, enc->blocks);
break;
case PROP_SUBBANDS:
g_value_set_int(value, enc->subbands);
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 mode", GST_TYPE_SBC_ALLOCATION,
SBC_ENC_DEFAULT_ALLOCATION, G_PARAM_READWRITE));
g_object_class_install_property(object_class, PROP_BLOCKS,
g_param_spec_int("blocks", "Blocks",
"Blocks", 0, G_MAXINT,
SBC_ENC_DEFAULT_BLOCKS, G_PARAM_READWRITE));
g_object_class_install_property(object_class, PROP_SUBBANDS,
g_param_spec_int("subbands", "Sub Bands",
"Sub Bands", 0, G_MAXINT,
SBC_ENC_DEFAULT_SUB_BANDS, 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_chain_function(self->sinkpad, GST_DEBUG_FUNCPTR(sbc_enc_chain));
gst_element_add_pad(GST_ELEMENT(self), self->srcpad);
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->adapter = gst_adapter_new();
}
|