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
29 #include "gstsbcutil.h"
32 * Selects one rate from a list of possible rates
33 * TODO - use a better approach to this (it is selecting the last element)
35 gint gst_sbc_select_rate_from_list(const GValue *value)
37 guint size = gst_value_list_get_size(value);
38 return g_value_get_int(gst_value_list_get_value(value, size-1));
42 * Selects one number of channels option from a range of possible numbers
43 * TODO - use a better approach to this (it is selecting the maximum value)
45 gint gst_sbc_select_channels_from_range(const GValue *value)
47 return gst_value_get_int_range_max(value);
51 * Selects one number of blocks from a list of possible blocks
52 * TODO - use a better approach to this (it is selecting the last element)
54 gint gst_sbc_select_blocks_from_list(const GValue *value)
56 guint size = gst_value_list_get_size(value);
57 return g_value_get_int(gst_value_list_get_value(value, size-1));
61 * Selects one number of subbands from a list
62 * TODO - use a better approach to this (it is selecting the last element)
64 gint gst_sbc_select_subbands_from_list(const GValue *value)
66 guint size = gst_value_list_get_size(value);
67 return g_value_get_int(gst_value_list_get_value(value, size-1));
71 * Selects one bitpool option from a range
72 * TODO - use a better approach to this (it is selecting the maximum value)
74 gint gst_sbc_select_bitpool_from_range(const GValue *value)
76 return gst_value_get_int_range_max(value);
80 * Selects one allocation mode from the ones on the list
81 * TODO - use a better approach
83 const gchar *gst_sbc_get_allocation_from_list(const GValue *value)
85 guint size = gst_value_list_get_size(value);
86 return g_value_get_string(gst_value_list_get_value(value, size-1));
90 * Selects one mode from the ones on the list
92 const gchar *gst_sbc_get_mode_from_list(const GValue *list, gint channels)
97 gboolean joint, stereo, dual, mono;
98 guint size = gst_value_list_get_size(list);
100 joint = stereo = dual = mono = FALSE;
102 for (i = 0; i < size; i++) {
103 value = gst_value_list_get_value(list, i);
104 aux = g_value_get_string(value);
105 if (strcmp("joint", aux) == 0)
107 else if (strcmp("stereo", aux) == 0)
109 else if (strcmp("dual", aux) == 0)
111 else if (strcmp("mono", aux) == 0)
115 if (channels == 1 && mono)
117 else if (channels == 2) {
129 gint gst_sbc_parse_rate_from_sbc(gint frequency)
145 gint gst_sbc_parse_rate_to_sbc(gint rate)
149 return SBC_FREQ_16000;
151 return SBC_FREQ_32000;
153 return SBC_FREQ_44100;
155 return SBC_FREQ_48000;
161 gint gst_sbc_get_channel_number(gint mode)
164 case SBC_MODE_JOINT_STEREO:
165 case SBC_MODE_STEREO:
166 case SBC_MODE_DUAL_CHANNEL:
175 gint gst_sbc_parse_subbands_from_sbc(gint subbands)
187 gint gst_sbc_parse_subbands_to_sbc(gint subbands)
199 gint gst_sbc_parse_blocks_from_sbc(gint blocks)
215 gint gst_sbc_parse_blocks_to_sbc(gint blocks)
231 const gchar *gst_sbc_parse_mode_from_sbc(gint mode)
236 case SBC_MODE_DUAL_CHANNEL:
238 case SBC_MODE_STEREO:
240 case SBC_MODE_JOINT_STEREO:
248 gint gst_sbc_parse_mode_to_sbc(const gchar *mode)
250 if (g_ascii_strcasecmp(mode, "joint") == 0)
251 return SBC_MODE_JOINT_STEREO;
252 else if (g_ascii_strcasecmp(mode, "stereo") == 0)
253 return SBC_MODE_STEREO;
254 else if (g_ascii_strcasecmp(mode, "dual") == 0)
255 return SBC_MODE_DUAL_CHANNEL;
256 else if (g_ascii_strcasecmp(mode, "mono") == 0)
257 return SBC_MODE_MONO;
258 else if (g_ascii_strcasecmp(mode, "auto") == 0)
259 return SBC_MODE_JOINT_STEREO;
264 const gchar *gst_sbc_parse_allocation_from_sbc(gint alloc)
267 case SBC_AM_LOUDNESS:
278 gint gst_sbc_parse_allocation_to_sbc(const gchar *allocation)
280 if (g_ascii_strcasecmp(allocation, "loudness") == 0)
281 return SBC_AM_LOUDNESS;
282 else if (g_ascii_strcasecmp(allocation, "snr") == 0)
285 return SBC_AM_LOUDNESS;
288 GstCaps *gst_sbc_parse_caps_from_sbc(sbc_t *sbc)
291 const gchar *mode_str;
292 const gchar *allocation_str;
294 mode_str = gst_sbc_parse_mode_from_sbc(sbc->mode);
295 allocation_str = gst_sbc_parse_allocation_from_sbc(sbc->allocation);
296 caps = gst_caps_new_simple("audio/x-sbc",
298 gst_sbc_parse_rate_from_sbc(sbc->frequency),
299 "channels", G_TYPE_INT,
300 gst_sbc_get_channel_number(sbc->mode),
301 "mode", G_TYPE_STRING, mode_str,
302 "subbands", G_TYPE_INT,
303 gst_sbc_parse_subbands_from_sbc(sbc->subbands),
304 "blocks", G_TYPE_INT,
305 gst_sbc_parse_blocks_from_sbc(sbc->blocks),
306 "allocation", G_TYPE_STRING, allocation_str,
307 "bitpool", G_TYPE_INT, sbc->bitpool,
314 * Given a GstCaps, this will return a fixed GstCaps on successful conversion.
315 * If an error occurs, it will return NULL and error_message will contain the
318 * error_message must be passed NULL, if an error occurs, the caller has the
319 * ownership of the error_message, it must be freed after use.
321 GstCaps *gst_sbc_util_caps_fixate(GstCaps *caps, gchar **error_message)
324 GstStructure *structure;
326 gboolean error = FALSE;
327 gint temp, rate, channels, blocks, subbands, bitpool;
328 const gchar *allocation = NULL;
329 const gchar *mode = NULL;
331 g_assert(*error_message == NULL);
333 structure = gst_caps_get_structure(caps, 0);
335 if (!gst_structure_has_field(structure, "rate")) {
337 *error_message = g_strdup("no rate");
340 value = gst_structure_get_value(structure, "rate");
341 if (GST_VALUE_HOLDS_LIST(value))
342 temp = gst_sbc_select_rate_from_list(value);
344 temp = g_value_get_int(value);
348 if (!gst_structure_has_field(structure, "channels")) {
350 *error_message = g_strdup("no channels");
353 value = gst_structure_get_value(structure, "channels");
354 if (GST_VALUE_HOLDS_INT_RANGE(value))
355 temp = gst_sbc_select_channels_from_range(value);
357 temp = g_value_get_int(value);
361 if (!gst_structure_has_field(structure, "blocks")) {
363 *error_message = g_strdup("no blocks.");
366 value = gst_structure_get_value(structure, "blocks");
367 if (GST_VALUE_HOLDS_LIST(value))
368 temp = gst_sbc_select_blocks_from_list(value);
370 temp = g_value_get_int(value);
374 if (!gst_structure_has_field(structure, "subbands")) {
376 *error_message = g_strdup("no subbands");
379 value = gst_structure_get_value(structure, "subbands");
380 if (GST_VALUE_HOLDS_LIST(value))
381 temp = gst_sbc_select_subbands_from_list(value);
383 temp = g_value_get_int(value);
387 if (!gst_structure_has_field(structure, "bitpool")) {
389 *error_message = g_strdup("no bitpool");
392 value = gst_structure_get_value(structure, "bitpool");
393 if (GST_VALUE_HOLDS_INT_RANGE(value))
394 temp = gst_sbc_select_bitpool_from_range(value);
396 temp = g_value_get_int(value);
400 if (!gst_structure_has_field(structure, "allocation")) {
402 *error_message = g_strdup("no allocation");
405 value = gst_structure_get_value(structure, "allocation");
406 if (GST_VALUE_HOLDS_LIST(value))
407 allocation = gst_sbc_get_allocation_from_list(value);
409 allocation = g_value_get_string(value);
412 if (!gst_structure_has_field(structure, "mode")) {
414 *error_message = g_strdup("no mode");
417 value = gst_structure_get_value(structure, "mode");
418 if (GST_VALUE_HOLDS_LIST(value)) {
419 mode = gst_sbc_get_mode_from_list(value, channels);
421 mode = g_value_get_string(value);
424 /* perform validation
425 * if channels is 1, we must have channel mode = mono
426 * if channels is 2, we can't have channel mode = mono */
427 if ( (channels == 1 && (strcmp(mode, "mono") != 0) ) ||
428 ( channels == 2 && ( strcmp(mode, "mono") == 0))) {
429 *error_message = g_strdup_printf("Invalid combination of "
430 "channels (%d) and channel mode (%s)",
439 result = gst_caps_new_simple("audio/x-sbc",
440 "rate", G_TYPE_INT, rate,
441 "channels", G_TYPE_INT, channels,
442 "mode", G_TYPE_STRING, mode,
443 "blocks", G_TYPE_INT, blocks,
444 "subbands", G_TYPE_INT, subbands,
445 "allocation", G_TYPE_STRING, allocation,
446 "bitpool", G_TYPE_INT, bitpool,
453 * Sets the int field_value to the param "field" on the structure.
454 * value is used to do the operation, it must be a uninitialized (zero-filled)
455 * GValue, it will be left unitialized at the end of the function.
457 void gst_sbc_util_set_structure_int_param(GstStructure *structure,
458 const gchar *field, gint field_value,
461 value = g_value_init(value, G_TYPE_INT);
462 g_value_set_int(value, field_value);
463 gst_structure_set_value(structure, field, value);
464 g_value_unset(value);
468 * Sets the string field_value to the param "field" on the structure.
469 * value is used to do the operation, it must be a uninitialized (zero-filled)
470 * GValue, it will be left unitialized at the end of the function.
472 void gst_sbc_util_set_structure_string_param(GstStructure *structure,
473 const gchar *field, const gchar *field_value,
476 value = g_value_init(value, G_TYPE_STRING);
477 g_value_set_string(value, field_value);
478 gst_structure_set_value(structure, field, value);
479 g_value_unset(value);
482 gboolean gst_sbc_util_fill_sbc_params(sbc_t *sbc, GstCaps *caps)
484 GstStructure *structure;
485 gint rate, channels, subbands, blocks, bitpool;
487 const gchar *allocation;
489 g_assert(gst_caps_is_fixed(caps));
491 structure = gst_caps_get_structure(caps, 0);
493 if (!gst_structure_get_int(structure, "rate", &rate))
495 if (!gst_structure_get_int(structure, "channels", &channels))
497 if (!gst_structure_get_int(structure, "subbands", &subbands))
499 if (!gst_structure_get_int(structure, "blocks", &blocks))
501 if (!gst_structure_get_int(structure, "bitpool", &bitpool))
504 if (!(mode = gst_structure_get_string(structure, "mode")))
506 if (!(allocation = gst_structure_get_string(structure, "allocation")))
509 if (channels == 1 && strcmp(mode, "mono") != 0)
512 sbc->frequency = gst_sbc_parse_rate_to_sbc(rate);
513 sbc->blocks = gst_sbc_parse_blocks_to_sbc(blocks);
514 sbc->subbands = gst_sbc_parse_subbands_to_sbc(subbands);
515 sbc->bitpool = bitpool;
516 sbc->mode = gst_sbc_parse_mode_to_sbc(mode);
517 sbc->allocation = gst_sbc_parse_allocation_to_sbc(allocation);