| 1 |
/* GStreamer SBC audio parser |
| 2 |
* Copyright (C) 2012 Collabora Ltd. <[email protected]> |
| 3 |
* |
| 4 |
* This library is free software; you can redistribute it and/or |
| 5 |
* modify it under the terms of the GNU Library General Public |
| 6 |
* License as published by the Free Software Foundation; either |
| 7 |
* version 2 of the License, or (at your option) any later version. |
| 8 |
* |
| 9 |
* This library is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 |
* Library General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU Library General Public |
| 15 |
* License along with this library; if not, write to the |
| 16 |
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, |
| 17 |
* Boston, MA 02110-1301, USA. |
| 18 |
*/ |
| 19 |
|
| 20 |
#ifdef HAVE_CONFIG_H |
| 21 |
#include "config.h" |
| 22 |
#endif |
| 23 |
|
| 24 |
/** |
| 25 |
* SECTION:element-sbcparse |
| 26 |
* @title: sbcparse |
| 27 |
* @see_also: sbcdec, sbcenc |
| 28 |
* |
| 29 |
* The sbcparse element will parse a bluetooth SBC audio stream into |
| 30 |
* frames and timestamp them properly. |
| 31 |
* |
| 32 |
* Since: 1.2.0 |
| 33 |
*/ |
| 34 |
|
| 35 |
#ifdef HAVE_CONFIG_H |
| 36 |
#include "config.h" |
| 37 |
#endif |
| 38 |
|
| 39 |
#include "gstaudioparserselements.h" |
| 40 |
#include "gstsbcparse.h" |
| 41 |
|
| 42 |
#include <string.h> |
| 43 |
#include <gst/tag/tag.h> |
| 44 |
#include <gst/audio/audio.h> |
| 45 |
#include <gst/base/base.h> |
| 46 |
#include <gst/pbutils/pbutils.h> |
| 47 |
|
| 48 |
#define SBC_SYNCBYTE 0x9C |
| 49 |
|
| 50 |
GST_DEBUG_CATEGORY_STATIC (sbcparse_debug); |
| 51 |
#define GST_CAT_DEFAULT sbcparse_debug |
| 52 |
|
| 53 |
static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src", |
| 54 |
GST_PAD_SRC, |
| 55 |
GST_PAD_ALWAYS, |
| 56 |
GST_STATIC_CAPS ("audio/x-sbc, parsed = (boolean) true, " |
| 57 |
"channels = (int) [ 1, 2 ], " |
| 58 |
"rate = (int) { 16000, 32000, 44100, 48000 }") |
| 59 |
); |
| 60 |
|
| 61 |
static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink", |
| 62 |
GST_PAD_SINK, |
| 63 |
GST_PAD_ALWAYS, |
| 64 |
GST_STATIC_CAPS ("audio/x-sbc") |
| 65 |
); |
| 66 |
|
| 67 |
static gboolean gst_sbc_parse_start (GstBaseParse * parse); |
| 68 |
static gboolean gst_sbc_parse_stop (GstBaseParse * parse); |
| 69 |
static GstFlowReturn gst_sbc_parse_handle_frame (GstBaseParse * parse, |
| 70 |
GstBaseParseFrame * frame, gint * skipsize); |
| 71 |
static GstFlowReturn gst_sbc_parse_pre_push_frame (GstBaseParse * parse, |
| 72 |
GstBaseParseFrame * frame); |
| 73 |
static GstCaps *gst_sbc_parse_get_sink_caps (GstBaseParse * parse, |
| 74 |
GstCaps * filter); |
| 75 |
|
| 76 |
static guint8 gst_sbc_calculate_crc8 (const guint8 * data, gint bits_crc); |
| 77 |
static gsize gst_sbc_calc_framelen (guint subbands, GstSbcChannelMode ch_mode, |
| 78 |
guint blocks, guint bitpool); |
| 79 |
static gsize gst_sbc_parse_header (const guint8 * data, gsize len, guint * rate, |
| 80 |
guint * n_blocks, GstSbcChannelMode * ch_mode, |
| 81 |
GstSbcAllocationMethod * alloc_method, guint * n_subbands, guint * bitpool); |
| 82 |
|
| 83 |
#define parent_class gst_sbc_parse_parent_class |
| 84 |
G_DEFINE_TYPE (GstSbcParse, gst_sbc_parse, GST_TYPE_BASE_PARSE); |
| 85 |
GST_ELEMENT_REGISTER_DEFINE (sbcparse, "sbcparse", |
| 86 |
GST_RANK_PRIMARY + 1, GST_TYPE_SBC_PARSE); |
| 87 |
|
| 88 |
static void |
| 89 |
gst_sbc_parse_class_init (GstSbcParseClass * klass) |
| 90 |
{ |
| 91 |
GstBaseParseClass *baseparse_class = GST_BASE_PARSE_CLASS (klass); |
| 92 |
GstElementClass *element_class = GST_ELEMENT_CLASS (klass); |
| 93 |
|
| 94 |
GST_DEBUG_CATEGORY_INIT (sbcparse_debug, "sbcparse", 0, "SBC audio parser"); |
| 95 |
|
| 96 |
baseparse_class->start = GST_DEBUG_FUNCPTR (gst_sbc_parse_start); |
| 97 |
baseparse_class->stop = GST_DEBUG_FUNCPTR (gst_sbc_parse_stop); |
| 98 |
baseparse_class->pre_push_frame = |
| 99 |
GST_DEBUG_FUNCPTR (gst_sbc_parse_pre_push_frame); |
| 100 |
baseparse_class->handle_frame = |
| 101 |
GST_DEBUG_FUNCPTR (gst_sbc_parse_handle_frame); |
| 102 |
baseparse_class->get_sink_caps = |
| 103 |
GST_DEBUG_FUNCPTR (gst_sbc_parse_get_sink_caps); |
| 104 |
|
| 105 |
gst_element_class_add_static_pad_template (element_class, &src_factory); |
| 106 |
gst_element_class_add_static_pad_template (element_class, &sink_factory); |
| 107 |
|
| 108 |
gst_element_class_set_static_metadata (element_class, "SBC audio parser", |
| 109 |
"Codec/Parser/Audio", "Parses an SBC bluetooth audio stream", |
| 110 |
"Tim-Philipp Müller <[email protected]>"); |
| 111 |
} |
| 112 |
|
| 113 |
static void |
| 114 |
gst_sbc_parse_reset (GstSbcParse * sbcparse) |
| 115 |
{ |
| 116 |
sbcparse->alloc_method = GST_SBC_ALLOCATION_METHOD_INVALID; |
| 117 |
sbcparse->ch_mode = GST_SBC_CHANNEL_MODE_INVALID; |
| 118 |
sbcparse->rate = -1; |
| 119 |
sbcparse->n_blocks = -1; |
| 120 |
sbcparse->n_subbands = -1; |
| 121 |
sbcparse->bitpool = -1; |
| 122 |
sbcparse->sent_codec_tag = FALSE; |
| 123 |
} |
| 124 |
|
| 125 |
static void |
| 126 |
gst_sbc_parse_init (GstSbcParse * sbcparse) |
| 127 |
{ |
| 128 |
gst_sbc_parse_reset (sbcparse); |
| 129 |
GST_PAD_SET_ACCEPT_INTERSECT (GST_BASE_PARSE_SINK_PAD (sbcparse)); |
| 130 |
GST_PAD_SET_ACCEPT_TEMPLATE (GST_BASE_PARSE_SINK_PAD (sbcparse)); |
| 131 |
} |
| 132 |
|
| 133 |
static gboolean |
| 134 |
gst_sbc_parse_start (GstBaseParse * parse) |
| 135 |
{ |
| 136 |
gst_base_parse_set_min_frame_size (parse, |
| 137 |
gst_sbc_calc_framelen (4, GST_SBC_CHANNEL_MODE_MONO, 4, 2)); |
| 138 |
|
| 139 |
gst_base_parse_set_has_timing_info (parse, FALSE); |
| 140 |
|
| 141 |
gst_base_parse_set_syncable (parse, TRUE); |
| 142 |
|
| 143 |
return TRUE; |
| 144 |
} |
| 145 |
|
| 146 |
static gboolean |
| 147 |
gst_sbc_parse_stop (GstBaseParse * parse) |
| 148 |
{ |
| 149 |
gst_sbc_parse_reset (GST_SBC_PARSE (parse)); |
| 150 |
return TRUE; |
| 151 |
} |
| 152 |
|
| 153 |
static const gchar * |
| 154 |
gst_sbc_channel_mode_get_name (GstSbcChannelMode ch_mode) |
| 155 |
{ |
| 156 |
switch (ch_mode) { |
| 157 |
case GST_SBC_CHANNEL_MODE_MONO: |
| 158 |
return "mono"; |
| 159 |
case GST_SBC_CHANNEL_MODE_DUAL: |
| 160 |
return "dual"; |
| 161 |
case GST_SBC_CHANNEL_MODE_STEREO: |
| 162 |
return "stereo"; |
| 163 |
case GST_SBC_CHANNEL_MODE_JOINT_STEREO: |
| 164 |
return "joint"; |
| 165 |
default: |
| 166 |
break; |
| 167 |
} |
| 168 |
return "invalid"; |
| 169 |
} |
| 170 |
|
| 171 |
static const gchar * |
| 172 |
gst_sbc_allocation_method_get_name (GstSbcAllocationMethod alloc_method) |
| 173 |
{ |
| 174 |
switch (alloc_method) { |
| 175 |
case GST_SBC_ALLOCATION_METHOD_SNR: |
| 176 |
return "snr"; |
| 177 |
case GST_SBC_ALLOCATION_METHOD_LOUDNESS: |
| 178 |
return "loudness"; |
| 179 |
default: |
| 180 |
break; |
| 181 |
} |
| 182 |
return "invalid"; |
| 183 |
} |
| 184 |
|
| 185 |
static GstFlowReturn |
| 186 |
gst_sbc_parse_handle_frame (GstBaseParse * parse, GstBaseParseFrame * frame, |
| 187 |
gint * skipsize) |
| 188 |
{ |
| 189 |
GstSbcParse *sbcparse = GST_SBC_PARSE (parse); |
| 190 |
GstSbcAllocationMethod alloc_method = GST_SBC_ALLOCATION_METHOD_INVALID; |
| 191 |
GstSbcChannelMode ch_mode = GST_SBC_CHANNEL_MODE_INVALID; |
| 192 |
GstMapInfo map; |
| 193 |
guint rate = 0, n_blocks = 0, n_subbands = 0, bitpool = 0; |
| 194 |
gsize frame_len, next_len; |
| 195 |
gint i, max_frames; |
| 196 |
|
| 197 |
gst_buffer_map (frame->buffer, &map, GST_MAP_READ); |
| 198 |
|
| 199 |
frame_len = |
| 200 |
gst_sbc_parse_header (map.data, map.size, &rate, &n_blocks, &ch_mode, |
| 201 |
&alloc_method, &n_subbands, &bitpool); |
| 202 |
|
| 203 |
GST_LOG_OBJECT (parse, "frame_len: %u", (guint) frame_len); |
| 204 |
|
| 205 |
if (frame_len == 0) |
| 206 |
goto resync; |
| 207 |
|
| 208 |
if (sbcparse->alloc_method != alloc_method |
| 209 |
|| sbcparse->ch_mode != ch_mode |
| 210 |
|| sbcparse->rate != rate |
| 211 |
|| sbcparse->n_blocks != n_blocks |
| 212 |
|| sbcparse->n_subbands != n_subbands || sbcparse->bitpool != bitpool) { |
| 213 |
guint avg_bitrate; |
| 214 |
GstCaps *caps; |
| 215 |
|
| 216 |
/* FIXME: do all of these need to be in the caps? */ |
| 217 |
caps = gst_caps_new_simple ("audio/x-sbc", "rate", G_TYPE_INT, rate, |
| 218 |
"channels", G_TYPE_INT, (ch_mode == GST_SBC_CHANNEL_MODE_MONO) ? 1 : 2, |
| 219 |
"channel-mode", G_TYPE_STRING, gst_sbc_channel_mode_get_name (ch_mode), |
| 220 |
"blocks", G_TYPE_INT, n_blocks, "subbands", G_TYPE_INT, n_subbands, |
| 221 |
"allocation-method", G_TYPE_STRING, |
| 222 |
gst_sbc_allocation_method_get_name (alloc_method), |
| 223 |
"bitpool", G_TYPE_INT, bitpool, "parsed", G_TYPE_BOOLEAN, TRUE, NULL); |
| 224 |
|
| 225 |
GST_INFO_OBJECT (sbcparse, "caps changed to %" GST_PTR_FORMAT, caps); |
| 226 |
|
| 227 |
gst_pad_push_event (GST_BASE_PARSE_SRC_PAD (sbcparse), |
| 228 |
gst_event_new_caps (caps)); |
| 229 |
|
| 230 |
avg_bitrate = (8 * frame_len * rate) / (n_subbands * n_blocks); |
| 231 |
gst_base_parse_set_average_bitrate (parse, avg_bitrate); |
| 232 |
|
| 233 |
gst_base_parse_set_frame_rate (parse, rate, n_subbands * n_blocks, 0, 0); |
| 234 |
|
| 235 |
sbcparse->alloc_method = alloc_method; |
| 236 |
sbcparse->ch_mode = ch_mode; |
| 237 |
sbcparse->rate = rate; |
| 238 |
sbcparse->n_blocks = n_blocks; |
| 239 |
sbcparse->n_subbands = n_subbands; |
| 240 |
sbcparse->bitpool = bitpool; |
| 241 |
|
| 242 |
gst_caps_unref (caps); |
| 243 |
} |
| 244 |
|
| 245 |
if (frame_len > map.size) |
| 246 |
goto need_more_data; |
| 247 |
|
| 248 |
GST_BUFFER_OFFSET (frame->buffer) = GST_BUFFER_OFFSET_NONE; |
| 249 |
GST_BUFFER_OFFSET_END (frame->buffer) = GST_BUFFER_OFFSET_NONE; |
| 250 |
|
| 251 |
/* completely arbitrary limit, we only process data we already have, |
| 252 |
* so we aren't introducing latency here */ |
| 253 |
max_frames = MIN (map.size / frame_len, n_blocks * n_subbands * 5); |
| 254 |
GST_LOG_OBJECT (sbcparse, "parsing up to %d frames", max_frames); |
| 255 |
|
| 256 |
for (i = 1; i < max_frames; ++i) { |
| 257 |
next_len = |
| 258 |
gst_sbc_parse_header (map.data + (i * frame_len), |
| 259 |
map.size - (i * frame_len), &rate, &n_blocks, &ch_mode, &alloc_method, |
| 260 |
&n_subbands, &bitpool); |
| 261 |
|
| 262 |
if (next_len != frame_len || sbcparse->alloc_method != alloc_method || |
| 263 |
sbcparse->ch_mode != ch_mode || sbcparse->rate != rate || |
| 264 |
sbcparse->n_blocks != n_blocks || sbcparse->n_subbands != n_subbands || |
| 265 |
sbcparse->bitpool != bitpool) { |
| 266 |
break; |
| 267 |
} |
| 268 |
} |
| 269 |
GST_LOG_OBJECT (sbcparse, "packing %d SBC frames into next output buffer", i); |
| 270 |
|
| 271 |
/* Note: local n_subbands and n_blocks variables might be tainted if we |
| 272 |
* bailed out of the loop above because of a header configuration mismatch */ |
| 273 |
gst_base_parse_set_frame_rate (parse, rate, |
| 274 |
sbcparse->n_subbands * sbcparse->n_blocks * i, 0, 0); |
| 275 |
|
| 276 |
gst_buffer_unmap (frame->buffer, &map); |
| 277 |
return gst_base_parse_finish_frame (parse, frame, i * frame_len); |
| 278 |
|
| 279 |
resync: |
| 280 |
{ |
| 281 |
const guint8 *possible_sync; |
| 282 |
|
| 283 |
GST_DEBUG_OBJECT (parse, "no sync, resyncing"); |
| 284 |
|
| 285 |
possible_sync = memchr (map.data, SBC_SYNCBYTE, map.size); |
| 286 |
|
| 287 |
if (possible_sync != NULL) |
| 288 |
*skipsize = (gint) (possible_sync - map.data); |
| 289 |
else |
| 290 |
*skipsize = map.size; |
| 291 |
|
| 292 |
gst_buffer_unmap (frame->buffer, &map); |
| 293 |
|
| 294 |
/* we could optimise things here by looping over the data and checking |
| 295 |
* whether the sync is good or not instead of handing control back to |
| 296 |
* the base class just to be called again */ |
| 297 |
return GST_FLOW_OK; |
| 298 |
} |
| 299 |
need_more_data: |
| 300 |
{ |
| 301 |
GST_LOG_OBJECT (parse, |
| 302 |
"need %" G_GSIZE_FORMAT " bytes, but only have %" G_GSIZE_FORMAT, |
| 303 |
frame_len, map.size); |
| 304 |
gst_base_parse_set_min_frame_size (parse, frame_len); |
| 305 |
gst_buffer_unmap (frame->buffer, &map); |
| 306 |
return GST_FLOW_OK; |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
static void |
| 311 |
remove_fields (GstCaps * caps) |
| 312 |
{ |
| 313 |
guint i, n; |
| 314 |
|
| 315 |
n = gst_caps_get_size (caps); |
| 316 |
for (i = 0; i < n; i++) { |
| 317 |
GstStructure *s = gst_caps_get_structure (caps, i); |
| 318 |
|
| 319 |
gst_structure_remove_field (s, "parsed"); |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
static GstCaps * |
| 324 |
gst_sbc_parse_get_sink_caps (GstBaseParse * parse, GstCaps * filter) |
| 325 |
{ |
| 326 |
GstCaps *peercaps, *templ; |
| 327 |
GstCaps *res; |
| 328 |
|
| 329 |
templ = gst_pad_get_pad_template_caps (GST_BASE_PARSE_SINK_PAD (parse)); |
| 330 |
if (filter) { |
| 331 |
GstCaps *fcopy = gst_caps_copy (filter); |
| 332 |
/* Remove the fields we convert */ |
| 333 |
remove_fields (fcopy); |
| 334 |
peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), fcopy); |
| 335 |
gst_caps_unref (fcopy); |
| 336 |
} else |
| 337 |
peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), NULL); |
| 338 |
|
| 339 |
if (peercaps) { |
| 340 |
/* Remove the parsed field */ |
| 341 |
peercaps = gst_caps_make_writable (peercaps); |
| 342 |
remove_fields (peercaps); |
| 343 |
|
| 344 |
res = gst_caps_intersect_full (peercaps, templ, GST_CAPS_INTERSECT_FIRST); |
| 345 |
gst_caps_unref (peercaps); |
| 346 |
gst_caps_unref (templ); |
| 347 |
} else { |
| 348 |
res = templ; |
| 349 |
} |
| 350 |
|
| 351 |
if (filter) { |
| 352 |
GstCaps *intersection; |
| 353 |
|
| 354 |
intersection = |
| 355 |
gst_caps_intersect_full (filter, res, GST_CAPS_INTERSECT_FIRST); |
| 356 |
gst_caps_unref (res); |
| 357 |
res = intersection; |
| 358 |
} |
| 359 |
|
| 360 |
return res; |
| 361 |
} |
| 362 |
|
| 363 |
static const guint8 crc_table[256] = { |
| 364 |
0x00, 0x1D, 0x3A, 0x27, 0x74, 0x69, 0x4E, 0x53, |
| 365 |
0xE8, 0xF5, 0xD2, 0xCF, 0x9C, 0x81, 0xA6, 0xBB, |
| 366 |
0xCD, 0xD0, 0xF7, 0xEA, 0xB9, 0xA4, 0x83, 0x9E, |
| 367 |
0x25, 0x38, 0x1F, 0x02, 0x51, 0x4C, 0x6B, 0x76, |
| 368 |
0x87, 0x9A, 0xBD, 0xA0, 0xF3, 0xEE, 0xC9, 0xD4, |
| 369 |
0x6F, 0x72, 0x55, 0x48, 0x1B, 0x06, 0x21, 0x3C, |
| 370 |
0x4A, 0x57, 0x70, 0x6D, 0x3E, 0x23, 0x04, 0x19, |
| 371 |
0xA2, 0xBF, 0x98, 0x85, 0xD6, 0xCB, 0xEC, 0xF1, |
| 372 |
0x13, 0x0E, 0x29, 0x34, 0x67, 0x7A, 0x5D, 0x40, |
| 373 |
0xFB, 0xE6, 0xC1, 0xDC, 0x8F, 0x92, 0xB5, 0xA8, |
| 374 |
0xDE, 0xC3, 0xE4, 0xF9, 0xAA, 0xB7, 0x90, 0x8D, |
| 375 |
0x36, 0x2B, 0x0C, 0x11, 0x42, 0x5F, 0x78, 0x65, |
| 376 |
0x94, 0x89, 0xAE, 0xB3, 0xE0, 0xFD, 0xDA, 0xC7, |
| 377 |
0x7C, 0x61, 0x46, 0x5B, 0x08, 0x15, 0x32, 0x2F, |
| 378 |
0x59, 0x44, 0x63, 0x7E, 0x2D, 0x30, 0x17, 0x0A, |
| 379 |
0xB1, 0xAC, 0x8B, 0x96, 0xC5, 0xD8, 0xFF, 0xE2, |
| 380 |
0x26, 0x3B, 0x1C, 0x01, 0x52, 0x4F, 0x68, 0x75, |
| 381 |
0xCE, 0xD3, 0xF4, 0xE9, 0xBA, 0xA7, 0x80, 0x9D, |
| 382 |
0xEB, 0xF6, 0xD1, 0xCC, 0x9F, 0x82, 0xA5, 0xB8, |
| 383 |
0x03, 0x1E, 0x39, 0x24, 0x77, 0x6A, 0x4D, 0x50, |
| 384 |
0xA1, 0xBC, 0x9B, 0x86, 0xD5, 0xC8, 0xEF, 0xF2, |
| 385 |
0x49, 0x54, 0x73, 0x6E, 0x3D, 0x20, 0x07, 0x1A, |
| 386 |
0x6C, 0x71, 0x56, 0x4B, 0x18, 0x05, 0x22, 0x3F, |
| 387 |
0x84, 0x99, 0xBE, 0xA3, 0xF0, 0xED, 0xCA, 0xD7, |
| 388 |
0x35, 0x28, 0x0F, 0x12, 0x41, 0x5C, 0x7B, 0x66, |
| 389 |
0xDD, 0xC0, 0xE7, 0xFA, 0xA9, 0xB4, 0x93, 0x8E, |
| 390 |
0xF8, 0xE5, 0xC2, 0xDF, 0x8C, 0x91, 0xB6, 0xAB, |
| 391 |
0x10, 0x0D, 0x2A, 0x37, 0x64, 0x79, 0x5E, 0x43, |
| 392 |
0xB2, 0xAF, 0x88, 0x95, 0xC6, 0xDB, 0xFC, 0xE1, |
| 393 |
0x5A, 0x47, 0x60, 0x7D, 0x2E, 0x33, 0x14, 0x09, |
| 394 |
0x7F, 0x62, 0x45, 0x58, 0x0B, 0x16, 0x31, 0x2C, |
| 395 |
0x97, 0x8A, 0xAD, 0xB0, 0xE3, 0xFE, 0xD9, 0xC4 |
| 396 |
}; |
| 397 |
|
| 398 |
static guint8 |
| 399 |
gst_sbc_calculate_crc8 (const guint8 * data, gint crc_bits) |
| 400 |
{ |
| 401 |
guint8 crc = 0x0f; |
| 402 |
guint8 octet; |
| 403 |
|
| 404 |
while (crc_bits >= 8) { |
| 405 |
crc = crc_table[crc ^ *data]; |
| 406 |
crc_bits -= 8; |
| 407 |
++data; |
| 408 |
} |
| 409 |
|
| 410 |
octet = *data; |
| 411 |
while (crc_bits > 0) { |
| 412 |
gchar bit = ((octet ^ crc) & 0x80) >> 7; |
| 413 |
|
| 414 |
crc = ((crc & 0x7f) << 1) ^ (bit ? 0x1d : 0); |
| 415 |
|
| 416 |
octet = octet << 1; |
| 417 |
--crc_bits; |
| 418 |
} |
| 419 |
|
| 420 |
return crc; |
| 421 |
} |
| 422 |
|
| 423 |
static gsize |
| 424 |
gst_sbc_calc_framelen (guint subbands, GstSbcChannelMode ch_mode, |
| 425 |
guint blocks, guint bitpool) |
| 426 |
{ |
| 427 |
switch (ch_mode) { |
| 428 |
case GST_SBC_CHANNEL_MODE_MONO: |
| 429 |
return 4 + (subbands * 1) / 2 + ((blocks * 1 * bitpool) + 7) / 8; |
| 430 |
case GST_SBC_CHANNEL_MODE_DUAL: |
| 431 |
return 4 + (subbands * 2) / 2 + ((blocks * 2 * bitpool) + 7) / 8; |
| 432 |
case GST_SBC_CHANNEL_MODE_STEREO: |
| 433 |
return 4 + (subbands * 2) / 2 + ((blocks * bitpool) + 7) / 8; |
| 434 |
case GST_SBC_CHANNEL_MODE_JOINT_STEREO: |
| 435 |
return 4 + (subbands * 2) / 2 + ((subbands + blocks * bitpool) + 7) / 8; |
| 436 |
default: |
| 437 |
break; |
| 438 |
} |
| 439 |
|
| 440 |
g_return_val_if_reached (0); |
| 441 |
} |
| 442 |
|
| 443 |
static gsize |
| 444 |
gst_sbc_parse_header (const guint8 * data, gsize len, guint * rate, |
| 445 |
guint * n_blocks, GstSbcChannelMode * ch_mode, |
| 446 |
GstSbcAllocationMethod * alloc_method, guint * n_subbands, guint * bitpool) |
| 447 |
{ |
| 448 |
static const guint16 sbc_rates[4] = { 16000, 32000, 44100, 48000 }; |
| 449 |
static const guint8 sbc_blocks[4] = { 4, 8, 12, 16 }; |
| 450 |
guint8 crc_data[2 + 1 + 8], crc_bits, i; |
| 451 |
|
| 452 |
if (len < 7) |
| 453 |
return 0; |
| 454 |
GST_MEMDUMP ("header", data, 7); |
| 455 |
|
| 456 |
if (data[0] != SBC_SYNCBYTE) |
| 457 |
return 0; |
| 458 |
|
| 459 |
*rate = sbc_rates[(data[1] >> 6) & 0x03]; |
| 460 |
*n_blocks = sbc_blocks[(data[1] >> 4) & 0x03]; |
| 461 |
*ch_mode = (GstSbcChannelMode) ((data[1] >> 2) & 0x03); |
| 462 |
*alloc_method = (data[1] >> 1) & 0x01; |
| 463 |
*n_subbands = (data[1] & 0x01) ? 8 : 4; |
| 464 |
*bitpool = data[2]; |
| 465 |
|
| 466 |
GST_TRACE ("rate=%u, n_blocks=%u, ch_mode=%u, alloc_method=%u, " |
| 467 |
"n_subbands=%u, bitpool=%u", *rate, *n_blocks, *ch_mode, *alloc_method, |
| 468 |
*n_subbands, *bitpool); |
| 469 |
|
| 470 |
if (*bitpool < 2) |
| 471 |
return 0; |
| 472 |
|
| 473 |
/* check CRC */ |
| 474 |
crc_data[0] = data[1]; |
| 475 |
crc_data[1] = data[2]; |
| 476 |
crc_bits = 16; |
| 477 |
|
| 478 |
/* joint flags and RFA */ |
| 479 |
if (*ch_mode == GST_SBC_CHANNEL_MODE_JOINT_STEREO) |
| 480 |
crc_bits += *n_subbands; |
| 481 |
|
| 482 |
/* scale factors */ |
| 483 |
if (*ch_mode == GST_SBC_CHANNEL_MODE_MONO) |
| 484 |
crc_bits += *n_subbands * 1 * 4; |
| 485 |
else |
| 486 |
crc_bits += *n_subbands * 2 * 4; |
| 487 |
|
| 488 |
if (len < 1 + (crc_bits + 7) / 8 + 1) { |
| 489 |
GST_LOG ("not enough header data available for CRC check, got %" |
| 490 |
G_GSIZE_FORMAT ", expected %u", len, 1 + (crc_bits + 7) / 8 + 1); |
| 491 |
return 0; |
| 492 |
} |
| 493 |
|
| 494 |
for (i = 16; i < crc_bits; i += 8) { |
| 495 |
crc_data[i / 8] = data[1 + (i / 8) + 1]; |
| 496 |
} |
| 497 |
|
| 498 |
if (i > crc_bits) { |
| 499 |
crc_data[(i / 8) - 1] &= 0xF0; |
| 500 |
} |
| 501 |
|
| 502 |
GST_MEMDUMP ("crc bytes", crc_data, GST_ROUND_UP_8 (crc_bits) / 8); |
| 503 |
if (gst_sbc_calculate_crc8 (crc_data, crc_bits) != data[3]) { |
| 504 |
GST_LOG ("header CRC check failed, bits=%u, got 0x%02x, expected 0x%02x", |
| 505 |
crc_bits, gst_sbc_calculate_crc8 (crc_data, crc_bits), data[3]); |
| 506 |
return 0; |
| 507 |
} |
| 508 |
|
| 509 |
return gst_sbc_calc_framelen (*n_subbands, *ch_mode, *n_blocks, *bitpool); |
| 510 |
} |
| 511 |
|
| 512 |
static GstFlowReturn |
| 513 |
gst_sbc_parse_pre_push_frame (GstBaseParse * parse, GstBaseParseFrame * frame) |
| 514 |
{ |
| 515 |
GstSbcParse *sbcparse = GST_SBC_PARSE (parse); |
| 516 |
|
| 517 |
if (!sbcparse->sent_codec_tag) { |
| 518 |
GstTagList *taglist; |
| 519 |
GstCaps *caps; |
| 520 |
|
| 521 |
/* codec tag */ |
| 522 |
caps = gst_pad_get_current_caps (GST_BASE_PARSE_SRC_PAD (parse)); |
| 523 |
if (G_UNLIKELY (caps == NULL)) { |
| 524 |
if (GST_PAD_IS_FLUSHING (GST_BASE_PARSE_SRC_PAD (parse))) { |
| 525 |
GST_INFO_OBJECT (parse, "Src pad is flushing"); |
| 526 |
return GST_FLOW_FLUSHING; |
| 527 |
} else { |
| 528 |
GST_INFO_OBJECT (parse, "Src pad is not negotiated!"); |
| 529 |
return GST_FLOW_NOT_NEGOTIATED; |
| 530 |
} |
| 531 |
} |
| 532 |
|
| 533 |
taglist = gst_tag_list_new_empty (); |
| 534 |
gst_pb_utils_add_codec_description_to_tag_list (taglist, |
| 535 |
GST_TAG_AUDIO_CODEC, caps); |
| 536 |
gst_caps_unref (caps); |
| 537 |
|
| 538 |
gst_base_parse_merge_tags (parse, taglist, GST_TAG_MERGE_REPLACE); |
| 539 |
gst_tag_list_unref (taglist); |
| 540 |
|
| 541 |
/* also signals the end of first-frame processing */ |
| 542 |
sbcparse->sent_codec_tag = TRUE; |
| 543 |
} |
| 544 |
|
| 545 |
frame->flags |= GST_BASE_PARSE_FRAME_FLAG_CLIP; |
| 546 |
|
| 547 |
return GST_FLOW_OK; |
| 548 |
} |