1 /* Spa A2DP SBC codec
2  *
3  * Copyright © 2020 Wim Taymans
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
24 
25 #include <unistd.h>
26 #include <stddef.h>
27 #include <errno.h>
28 #include <arpa/inet.h>
29 
30 #include <spa/param/audio/format.h>
31 #include <spa/utils/string.h>
32 
33 #include <sbc/sbc.h>
34 
35 #include "rtp.h"
36 #include "a2dp-codecs.h"
37 
38 #define MAX_FRAME_COUNT 16
39 
40 struct impl {
41 	sbc_t sbc;
42 
43 	struct rtp_header *header;
44 	struct rtp_payload *payload;
45 
46 	size_t mtu;
47 	int codesize;
48 	int max_frames;
49 
50 	int min_bitpool;
51 	int max_bitpool;
52 };
53 
codec_fill_caps(const struct a2dp_codec * codec,uint32_t flags,uint8_t caps[A2DP_MAX_CAPS_SIZE])54 static int codec_fill_caps(const struct a2dp_codec *codec, uint32_t flags,
55 		uint8_t caps[A2DP_MAX_CAPS_SIZE])
56 {
57 	static const a2dp_sbc_t a2dp_sbc = {
58 		.frequency =
59 			SBC_SAMPLING_FREQ_16000 |
60 			SBC_SAMPLING_FREQ_32000 |
61 			SBC_SAMPLING_FREQ_44100 |
62 			SBC_SAMPLING_FREQ_48000,
63 		.channel_mode =
64 			SBC_CHANNEL_MODE_MONO |
65 			SBC_CHANNEL_MODE_DUAL_CHANNEL |
66 			SBC_CHANNEL_MODE_STEREO |
67 			SBC_CHANNEL_MODE_JOINT_STEREO,
68 		.block_length =
69 			SBC_BLOCK_LENGTH_4 |
70 			SBC_BLOCK_LENGTH_8 |
71 			SBC_BLOCK_LENGTH_12 |
72 			SBC_BLOCK_LENGTH_16,
73 		.subbands =
74 			SBC_SUBBANDS_4 |
75 			SBC_SUBBANDS_8,
76 		.allocation_method =
77 			SBC_ALLOCATION_SNR |
78 			SBC_ALLOCATION_LOUDNESS,
79 		.min_bitpool = SBC_MIN_BITPOOL,
80 		.max_bitpool = SBC_MAX_BITPOOL,
81 	};
82 
83 	memcpy(caps, &a2dp_sbc, sizeof(a2dp_sbc));
84 	return sizeof(a2dp_sbc);
85 }
86 
default_bitpool(uint8_t freq,uint8_t mode,bool xq)87 static uint8_t default_bitpool(uint8_t freq, uint8_t mode, bool xq)
88 {
89 	/* A2DP spec v1.2 states that all SNK implementation shall handle bitrates
90 	 * of up to 512 kbps (~ bitpool = 86 stereo, or 2x43 dual channel at 44.1KHz
91 	 * or ~ bitpool = 78 stereo, or 2x39 dual channel at 48KHz). */
92 	switch (freq) {
93 	case SBC_SAMPLING_FREQ_16000:
94         case SBC_SAMPLING_FREQ_32000:
95 		return 64;
96 
97 	case SBC_SAMPLING_FREQ_44100:
98 		switch (mode) {
99 		case SBC_CHANNEL_MODE_MONO:
100 		case SBC_CHANNEL_MODE_DUAL_CHANNEL:
101 			return xq ? 43 : 32;
102 
103 		case SBC_CHANNEL_MODE_STEREO:
104 		case SBC_CHANNEL_MODE_JOINT_STEREO:
105 			return xq ? 86 : 64;
106 		}
107 		return xq ? 86 : 64;
108 	case SBC_SAMPLING_FREQ_48000:
109 		switch (mode) {
110 		case SBC_CHANNEL_MODE_MONO:
111 		case SBC_CHANNEL_MODE_DUAL_CHANNEL:
112 			return xq ? 39 : 29;
113 
114 		case SBC_CHANNEL_MODE_STEREO:
115 		case SBC_CHANNEL_MODE_JOINT_STEREO:
116 			return xq ? 78 : 58;
117 		}
118 		return xq ? 78 : 58;
119 	}
120 	return xq ? 86 : 64;
121 }
122 
123 
124 static const struct a2dp_codec_config
125 sbc_frequencies[] = {
126 	{ SBC_SAMPLING_FREQ_48000, 48000, 3 },
127 	{ SBC_SAMPLING_FREQ_44100, 44100, 2 },
128 	{ SBC_SAMPLING_FREQ_32000, 32000, 1 },
129 	{ SBC_SAMPLING_FREQ_16000, 16000, 0 },
130 };
131 
132 static const struct a2dp_codec_config
133 sbc_xq_frequencies[] = {
134 	{ SBC_SAMPLING_FREQ_44100, 44100, 1 },
135 	{ SBC_SAMPLING_FREQ_48000, 48000, 0 },
136 };
137 
138 static const struct a2dp_codec_config
139 sbc_channel_modes[] = {
140 	{ SBC_CHANNEL_MODE_JOINT_STEREO, 2, 3 },
141 	{ SBC_CHANNEL_MODE_STEREO,       2, 2 },
142 	{ SBC_CHANNEL_MODE_DUAL_CHANNEL, 2, 1 },
143 	{ SBC_CHANNEL_MODE_MONO,         1, 0 },
144 };
145 
146 static const struct a2dp_codec_config
147 sbc_xq_channel_modes[] = {
148 	{ SBC_CHANNEL_MODE_DUAL_CHANNEL, 2, 2 },
149 	{ SBC_CHANNEL_MODE_JOINT_STEREO, 2, 1 },
150 	{ SBC_CHANNEL_MODE_STEREO,       2, 0 },
151 };
152 
codec_select_config(const struct a2dp_codec * codec,uint32_t flags,const void * caps,size_t caps_size,const struct a2dp_codec_audio_info * info,const struct spa_dict * settings,uint8_t config[A2DP_MAX_CAPS_SIZE])153 static int codec_select_config(const struct a2dp_codec *codec, uint32_t flags,
154 		const void *caps, size_t caps_size,
155 		const struct a2dp_codec_audio_info *info,
156 		const struct spa_dict *settings, uint8_t config[A2DP_MAX_CAPS_SIZE])
157 {
158 	a2dp_sbc_t conf;
159 	int bitpool, i;
160 	size_t n;
161 	const struct a2dp_codec_config *configs;
162 	bool xq = false;
163 
164 
165 	if (caps_size < sizeof(conf))
166 		return -EINVAL;
167 
168 	xq = (spa_streq(codec->name, "sbc_xq"));
169 
170 	memcpy(&conf, caps, sizeof(conf));
171 
172 	if (xq) {
173 		configs = sbc_xq_frequencies;
174 		n = SPA_N_ELEMENTS(sbc_xq_frequencies);
175 	} else {
176 		configs = sbc_frequencies;
177 		n = SPA_N_ELEMENTS(sbc_frequencies);
178 	}
179 	if ((i = a2dp_codec_select_config(configs, n, conf.frequency,
180 					  info ? info->rate : A2DP_CODEC_DEFAULT_RATE
181 					  )) < 0)
182 		return -ENOTSUP;
183 	conf.frequency = configs[i].config;
184 
185 	if (xq) {
186 		configs = sbc_xq_channel_modes;
187 		n = SPA_N_ELEMENTS(sbc_xq_channel_modes);
188 	} else {
189 		configs = sbc_channel_modes;
190 		n = SPA_N_ELEMENTS(sbc_channel_modes);
191 	}
192 	if ((i = a2dp_codec_select_config(configs, n, conf.channel_mode,
193 					  info ? info->channels : A2DP_CODEC_DEFAULT_CHANNELS
194 					  )) < 0)
195 		return -ENOTSUP;
196 	conf.channel_mode = configs[i].config;
197 
198 	if (conf.block_length & SBC_BLOCK_LENGTH_16)
199 		conf.block_length = SBC_BLOCK_LENGTH_16;
200 	else if (conf.block_length & SBC_BLOCK_LENGTH_12)
201 		conf.block_length = SBC_BLOCK_LENGTH_12;
202 	else if (conf.block_length & SBC_BLOCK_LENGTH_8)
203 		conf.block_length = SBC_BLOCK_LENGTH_8;
204 	else if (conf.block_length & SBC_BLOCK_LENGTH_4)
205 		conf.block_length = SBC_BLOCK_LENGTH_4;
206 	else
207 		return -ENOTSUP;
208 
209 	if (conf.subbands & SBC_SUBBANDS_8)
210 		conf.subbands = SBC_SUBBANDS_8;
211 	else if (conf.subbands & SBC_SUBBANDS_4)
212 		conf.subbands = SBC_SUBBANDS_4;
213 	else
214 		return -ENOTSUP;
215 
216 	if (conf.allocation_method & SBC_ALLOCATION_LOUDNESS)
217 		conf.allocation_method = SBC_ALLOCATION_LOUDNESS;
218 	else if (conf.allocation_method & SBC_ALLOCATION_SNR)
219 		conf.allocation_method = SBC_ALLOCATION_SNR;
220 	else
221 		return -ENOTSUP;
222 
223 	bitpool = default_bitpool(conf.frequency, conf.channel_mode, xq);
224 
225 	conf.min_bitpool = SPA_MAX(SBC_MIN_BITPOOL, conf.min_bitpool);
226 	conf.max_bitpool = SPA_MIN(bitpool, conf.max_bitpool);
227 	memcpy(config, &conf, sizeof(conf));
228 
229 	return sizeof(conf);
230 }
231 
codec_caps_preference_cmp(const struct a2dp_codec * codec,const void * caps1,size_t caps1_size,const void * caps2,size_t caps2_size,const struct a2dp_codec_audio_info * info)232 static int codec_caps_preference_cmp(const struct a2dp_codec *codec, const void *caps1, size_t caps1_size,
233 		const void *caps2, size_t caps2_size, const struct a2dp_codec_audio_info *info)
234 {
235 	a2dp_sbc_t conf1, conf2;
236 	a2dp_sbc_t *conf;
237 	int res1, res2;
238 	int a, b;
239 	bool xq = (spa_streq(codec->name, "sbc_xq"));
240 
241 	/* Order selected configurations by preference */
242 	res1 = codec->select_config(codec, 0, caps1, caps1_size, info, NULL, (uint8_t *)&conf1);
243 	res2 = codec->select_config(codec, 0, caps2, caps2_size, info , NULL, (uint8_t *)&conf2);
244 
245 #define PREFER_EXPR(expr)			\
246 		do {				\
247 			conf = &conf1; 		\
248 			a = (expr);		\
249 			conf = &conf2;		\
250 			b = (expr);		\
251 			if (a != b)		\
252 				return b - a;	\
253 		} while (0)
254 
255 #define PREFER_BOOL(expr)	PREFER_EXPR((expr) ? 1 : 0)
256 
257 	/* Prefer valid */
258 	a = (res1 > 0 && (size_t)res1 == sizeof(a2dp_sbc_t)) ? 1 : 0;
259 	b = (res2 > 0 && (size_t)res2 == sizeof(a2dp_sbc_t)) ? 1 : 0;
260 	if (!a || !b)
261 		return b - a;
262 
263 	PREFER_BOOL(conf->frequency & (SBC_SAMPLING_FREQ_48000 | SBC_SAMPLING_FREQ_44100));
264 
265 	if (xq)
266 		PREFER_BOOL(conf->channel_mode & SBC_CHANNEL_MODE_DUAL_CHANNEL);
267 	else
268 		PREFER_BOOL(conf->channel_mode & SBC_CHANNEL_MODE_JOINT_STEREO);
269 
270 	PREFER_EXPR(conf->max_bitpool);
271 
272 	return 0;
273 
274 #undef PREFER_EXPR
275 #undef PREFER_BOOL
276 }
277 
codec_validate_config(const struct a2dp_codec * codec,uint32_t flags,const void * caps,size_t caps_size,struct spa_audio_info * info)278 static int codec_validate_config(const struct a2dp_codec *codec, uint32_t flags,
279 			const void *caps, size_t caps_size,
280 			struct spa_audio_info *info)
281 {
282 	const a2dp_sbc_t *conf;
283 
284 	if (caps == NULL || caps_size < sizeof(*conf))
285 		return -EINVAL;
286 
287 	conf = caps;
288 
289 	spa_zero(*info);
290 	info->media_type = SPA_MEDIA_TYPE_audio;
291 	info->media_subtype = SPA_MEDIA_SUBTYPE_raw;
292 	info->info.raw.format = SPA_AUDIO_FORMAT_S16;
293 
294 	switch (conf->frequency) {
295 	case SBC_SAMPLING_FREQ_16000:
296 		info->info.raw.rate = 16000;
297 		break;
298 	case SBC_SAMPLING_FREQ_32000:
299 		info->info.raw.rate = 32000;
300 		break;
301 	case SBC_SAMPLING_FREQ_44100:
302 		info->info.raw.rate = 44100;
303 		break;
304 	case SBC_SAMPLING_FREQ_48000:
305 		info->info.raw.rate = 48000;
306 		break;
307 	default:
308 		return -EINVAL;
309         }
310 
311 	switch (conf->channel_mode) {
312 	case SBC_CHANNEL_MODE_MONO:
313 		info->info.raw.channels = 1;
314 		info->info.raw.position[0] = SPA_AUDIO_CHANNEL_MONO;
315                 break;
316 	case SBC_CHANNEL_MODE_DUAL_CHANNEL:
317 	case SBC_CHANNEL_MODE_STEREO:
318 	case SBC_CHANNEL_MODE_JOINT_STEREO:
319 		info->info.raw.channels = 2;
320 		info->info.raw.position[0] = SPA_AUDIO_CHANNEL_FL;
321 		info->info.raw.position[1] = SPA_AUDIO_CHANNEL_FR;
322                 break;
323 	default:
324 		return -EINVAL;
325         }
326 
327 	switch (conf->subbands) {
328 	case SBC_SUBBANDS_4:
329 	case SBC_SUBBANDS_8:
330 		break;
331 	default:
332 		return -EINVAL;
333 	}
334 
335 	switch (conf->block_length) {
336 	case SBC_BLOCK_LENGTH_4:
337 	case SBC_BLOCK_LENGTH_8:
338 	case SBC_BLOCK_LENGTH_12:
339 	case SBC_BLOCK_LENGTH_16:
340 		break;
341 	default:
342 		return -EINVAL;
343 	}
344 	return 0;
345 }
346 
codec_set_bitpool(struct impl * this,int bitpool)347 static int codec_set_bitpool(struct impl *this, int bitpool)
348 {
349 	size_t rtp_size = sizeof(struct rtp_header) + sizeof(struct rtp_payload);
350 
351 	this->sbc.bitpool = SPA_CLAMP(bitpool, this->min_bitpool, this->max_bitpool);
352 	this->codesize = sbc_get_codesize(&this->sbc);
353 	this->max_frames = (this->mtu - rtp_size) / sbc_get_frame_length(&this->sbc);
354 	if (this->max_frames > 15)
355 		this->max_frames = 15;
356 	return this->sbc.bitpool;
357 }
358 
codec_enum_config(const struct a2dp_codec * codec,const void * caps,size_t caps_size,uint32_t id,uint32_t idx,struct spa_pod_builder * b,struct spa_pod ** param)359 static int codec_enum_config(const struct a2dp_codec *codec,
360 		const void *caps, size_t caps_size, uint32_t id, uint32_t idx,
361 		struct spa_pod_builder *b, struct spa_pod **param)
362 {
363 	a2dp_sbc_t conf;
364         struct spa_pod_frame f[2];
365 	struct spa_pod_choice *choice;
366 	uint32_t i = 0;
367 	uint32_t position[SPA_AUDIO_MAX_CHANNELS];
368 
369 	if (caps_size < sizeof(conf))
370 		return -EINVAL;
371 
372 	memcpy(&conf, caps, sizeof(conf));
373 
374 	if (idx > 0)
375 		return 0;
376 
377 	spa_pod_builder_push_object(b, &f[0], SPA_TYPE_OBJECT_Format, id);
378 	spa_pod_builder_add(b,
379 			SPA_FORMAT_mediaType,      SPA_POD_Id(SPA_MEDIA_TYPE_audio),
380 			SPA_FORMAT_mediaSubtype,   SPA_POD_Id(SPA_MEDIA_SUBTYPE_raw),
381 			SPA_FORMAT_AUDIO_format,   SPA_POD_Id(SPA_AUDIO_FORMAT_S16),
382 			0);
383 	spa_pod_builder_prop(b, SPA_FORMAT_AUDIO_rate, 0);
384 
385 	spa_pod_builder_push_choice(b, &f[1], SPA_CHOICE_None, 0);
386 	choice = (struct spa_pod_choice*)spa_pod_builder_frame(b, &f[1]);
387 	i = 0;
388 	if (conf.frequency & SBC_SAMPLING_FREQ_48000) {
389 		if (i++ == 0)
390 			spa_pod_builder_int(b, 48000);
391 		spa_pod_builder_int(b, 48000);
392 	}
393 	if (conf.frequency & SBC_SAMPLING_FREQ_44100) {
394 		if (i++ == 0)
395 			spa_pod_builder_int(b, 44100);
396 		spa_pod_builder_int(b, 44100);
397 	}
398 	if (conf.frequency & SBC_SAMPLING_FREQ_32000) {
399 		if (i++ == 0)
400 			spa_pod_builder_int(b, 32000);
401 		spa_pod_builder_int(b, 32000);
402 	}
403 	if (conf.frequency & SBC_SAMPLING_FREQ_16000) {
404 		if (i++ == 0)
405 			spa_pod_builder_int(b, 16000);
406 		spa_pod_builder_int(b, 16000);
407 	}
408 	if (i > 1)
409 		choice->body.type = SPA_CHOICE_Enum;
410 	spa_pod_builder_pop(b, &f[1]);
411 
412 	if (conf.channel_mode & SBC_CHANNEL_MODE_MONO &&
413 	    conf.channel_mode & (SBC_CHANNEL_MODE_JOINT_STEREO |
414 		    SBC_CHANNEL_MODE_STEREO | SBC_CHANNEL_MODE_DUAL_CHANNEL)) {
415 		spa_pod_builder_add(b,
416 				SPA_FORMAT_AUDIO_channels, SPA_POD_CHOICE_RANGE_Int(2, 1, 2),
417 				0);
418 	} else if (conf.channel_mode & SBC_CHANNEL_MODE_MONO) {
419 		position[0] = SPA_AUDIO_CHANNEL_MONO;
420 		spa_pod_builder_add(b,
421 				SPA_FORMAT_AUDIO_channels, SPA_POD_Int(1),
422 				SPA_FORMAT_AUDIO_position, SPA_POD_Array(sizeof(uint32_t),
423 					SPA_TYPE_Id, 1, position),
424 				0);
425 	} else {
426 		position[0] = SPA_AUDIO_CHANNEL_FL;
427 		position[1] = SPA_AUDIO_CHANNEL_FR;
428 		spa_pod_builder_add(b,
429 				SPA_FORMAT_AUDIO_channels, SPA_POD_Int(2),
430 				SPA_FORMAT_AUDIO_position, SPA_POD_Array(sizeof(uint32_t),
431 					SPA_TYPE_Id, 2, position),
432 				0);
433 	}
434 	*param = spa_pod_builder_pop(b, &f[0]);
435 	return *param == NULL ? -EIO : 1;
436 }
437 
codec_reduce_bitpool(void * data)438 static int codec_reduce_bitpool(void *data)
439 {
440 	struct impl *this = data;
441 	return codec_set_bitpool(this, this->sbc.bitpool - 2);
442 }
443 
codec_increase_bitpool(void * data)444 static int codec_increase_bitpool(void *data)
445 {
446 	struct impl *this = data;
447 	return codec_set_bitpool(this, this->sbc.bitpool + 1);
448 }
449 
codec_get_block_size(void * data)450 static int codec_get_block_size(void *data)
451 {
452 	struct impl *this = data;
453 	return this->codesize;
454 }
455 
codec_init(const struct a2dp_codec * codec,uint32_t flags,void * config,size_t config_len,const struct spa_audio_info * info,void * props,size_t mtu)456 static void *codec_init(const struct a2dp_codec *codec, uint32_t flags,
457 		void *config, size_t config_len, const struct spa_audio_info *info,
458 		void *props, size_t mtu)
459 {
460 	struct impl *this;
461 	a2dp_sbc_t *conf = config;
462 	int res;
463 
464 	this = calloc(1, sizeof(struct impl));
465 	if (this == NULL) {
466 		res = -errno;
467 		goto error;
468 	}
469 
470 	sbc_init(&this->sbc, 0);
471 	this->sbc.endian = SBC_LE;
472 	this->mtu = mtu;
473 
474 	if (info->media_type != SPA_MEDIA_TYPE_audio ||
475 	    info->media_subtype != SPA_MEDIA_SUBTYPE_raw ||
476 	    info->info.raw.format != SPA_AUDIO_FORMAT_S16) {
477 		res = -EINVAL;
478 		goto error;
479 	}
480 
481 	switch (conf->frequency) {
482 	case SBC_SAMPLING_FREQ_16000:
483 		this->sbc.frequency = SBC_FREQ_16000;
484 		break;
485 	case SBC_SAMPLING_FREQ_32000:
486 		this->sbc.frequency = SBC_FREQ_32000;
487 		break;
488 	case SBC_SAMPLING_FREQ_44100:
489 		this->sbc.frequency = SBC_FREQ_44100;
490 		break;
491 	case SBC_SAMPLING_FREQ_48000:
492 		this->sbc.frequency = SBC_FREQ_48000;
493 		break;
494 	default:
495 		res = -EINVAL;
496                 goto error;
497         }
498 
499 	switch (conf->channel_mode) {
500 	case SBC_CHANNEL_MODE_MONO:
501 		this->sbc.mode = SBC_MODE_MONO;
502                 break;
503 	case SBC_CHANNEL_MODE_DUAL_CHANNEL:
504 		this->sbc.mode = SBC_MODE_DUAL_CHANNEL;
505 		break;
506 	case SBC_CHANNEL_MODE_STEREO:
507 		this->sbc.mode = SBC_MODE_STEREO;
508 		break;
509 	case SBC_CHANNEL_MODE_JOINT_STEREO:
510 		this->sbc.mode = SBC_MODE_JOINT_STEREO;
511                 break;
512 	default:
513 		res = -EINVAL;
514                 goto error;
515         }
516 
517 	switch (conf->subbands) {
518 	case SBC_SUBBANDS_4:
519 		this->sbc.subbands = SBC_SB_4;
520 		break;
521 	case SBC_SUBBANDS_8:
522 		this->sbc.subbands = SBC_SB_8;
523 		break;
524 	default:
525 		res = -EINVAL;
526 		goto error;
527 	}
528 
529 	if (conf->allocation_method & SBC_ALLOCATION_LOUDNESS)
530 		this->sbc.allocation = SBC_AM_LOUDNESS;
531 	else
532 		this->sbc.allocation = SBC_AM_SNR;
533 
534 	switch (conf->block_length) {
535 	case SBC_BLOCK_LENGTH_4:
536 		this->sbc.blocks = SBC_BLK_4;
537 		break;
538 	case SBC_BLOCK_LENGTH_8:
539 		this->sbc.blocks = SBC_BLK_8;
540 		break;
541 	case SBC_BLOCK_LENGTH_12:
542 		this->sbc.blocks = SBC_BLK_12;
543 		break;
544 	case SBC_BLOCK_LENGTH_16:
545 		this->sbc.blocks = SBC_BLK_16;
546 		break;
547 	default:
548 		res = -EINVAL;
549 		goto error;
550 	}
551 
552 	this->min_bitpool = SPA_MAX(conf->min_bitpool, 12);
553 	this->max_bitpool = conf->max_bitpool;
554 
555 	codec_set_bitpool(this, conf->max_bitpool);
556 
557 	return this;
558 error:
559 	errno = -res;
560 	return NULL;
561 }
562 
codec_deinit(void * data)563 static void codec_deinit(void *data)
564 {
565 	struct impl *this = data;
566 	sbc_finish(&this->sbc);
567 	free(this);
568 }
569 
codec_abr_process(void * data,size_t unsent)570 static int codec_abr_process (void *data, size_t unsent)
571 {
572 	return -ENOTSUP;
573 }
574 
codec_start_encode(void * data,void * dst,size_t dst_size,uint16_t seqnum,uint32_t timestamp)575 static int codec_start_encode (void *data,
576 		void *dst, size_t dst_size, uint16_t seqnum, uint32_t timestamp)
577 {
578 	struct impl *this = data;
579 
580 	this->header = (struct rtp_header *)dst;
581 	this->payload = SPA_PTROFF(dst, sizeof(struct rtp_header), struct rtp_payload);
582 	memset(this->header, 0, sizeof(struct rtp_header)+sizeof(struct rtp_payload));
583 
584 	this->payload->frame_count = 0;
585 	this->header->v = 2;
586 	this->header->pt = 96;
587 	this->header->sequence_number = htons(seqnum);
588 	this->header->timestamp = htonl(timestamp);
589 	this->header->ssrc = htonl(1);
590 	return sizeof(struct rtp_header) + sizeof(struct rtp_payload);
591 }
592 
codec_encode(void * data,const void * src,size_t src_size,void * dst,size_t dst_size,size_t * dst_out,int * need_flush)593 static int codec_encode(void *data,
594 		const void *src, size_t src_size,
595 		void *dst, size_t dst_size,
596 		size_t *dst_out, int *need_flush)
597 {
598 	struct impl *this = data;
599 	int res;
600 
601 	res = sbc_encode(&this->sbc, src, src_size,
602 			dst, dst_size, (ssize_t*)dst_out);
603 	if (SPA_UNLIKELY(res < 0))
604 		return -EINVAL;
605 	spa_assert(res == this->codesize);
606 
607 	this->payload->frame_count += res / this->codesize;
608 	*need_flush = this->payload->frame_count >= this->max_frames;
609 	return res;
610 }
611 
codec_start_decode(void * data,const void * src,size_t src_size,uint16_t * seqnum,uint32_t * timestamp)612 static int codec_start_decode (void *data,
613 		const void *src, size_t src_size, uint16_t *seqnum, uint32_t *timestamp)
614 {
615 	const struct rtp_header *header = src;
616 	size_t header_size = sizeof(struct rtp_header) + sizeof(struct rtp_payload);
617 
618 	spa_return_val_if_fail (src_size > header_size, -EINVAL);
619 
620 	if (seqnum)
621 		*seqnum = ntohs(header->sequence_number);
622 	if (timestamp)
623 		*timestamp = ntohl(header->timestamp);
624 	return header_size;
625 }
626 
codec_decode(void * data,const void * src,size_t src_size,void * dst,size_t dst_size,size_t * dst_out)627 static int codec_decode(void *data,
628 		const void *src, size_t src_size,
629 		void *dst, size_t dst_size,
630 		size_t *dst_out)
631 {
632 	struct impl *this = data;
633 	int res;
634 
635 	res = sbc_decode(&this->sbc, src, src_size,
636 			dst, dst_size, dst_out);
637 
638 	return res;
639 }
640 
641 const struct a2dp_codec a2dp_codec_sbc = {
642 	.id = SPA_BLUETOOTH_AUDIO_CODEC_SBC,
643 	.codec_id = A2DP_CODEC_SBC,
644 	.name = "sbc",
645 	.description = "SBC",
646 	.fill_caps = codec_fill_caps,
647 	.select_config = codec_select_config,
648 	.enum_config = codec_enum_config,
649 	.validate_config = codec_validate_config,
650 	.caps_preference_cmp = codec_caps_preference_cmp,
651 	.init = codec_init,
652 	.deinit = codec_deinit,
653 	.get_block_size = codec_get_block_size,
654 	.abr_process = codec_abr_process,
655 	.start_encode = codec_start_encode,
656 	.encode = codec_encode,
657 	.start_decode = codec_start_decode,
658 	.decode = codec_decode,
659 	.reduce_bitpool = codec_reduce_bitpool,
660 	.increase_bitpool = codec_increase_bitpool,
661 };
662 
663 const struct a2dp_codec a2dp_codec_sbc_xq = {
664 	.id = SPA_BLUETOOTH_AUDIO_CODEC_SBC_XQ,
665 	.codec_id = A2DP_CODEC_SBC,
666 	.name = "sbc_xq",
667 	.description = "SBC-XQ",
668 	.fill_caps = codec_fill_caps,
669 	.select_config = codec_select_config,
670 	.enum_config = codec_enum_config,
671 	.validate_config = codec_validate_config,
672 	.caps_preference_cmp = codec_caps_preference_cmp,
673 	.init = codec_init,
674 	.deinit = codec_deinit,
675 	.get_block_size = codec_get_block_size,
676 	.abr_process = codec_abr_process,
677 	.start_encode = codec_start_encode,
678 	.encode = codec_encode,
679 	.start_decode = codec_start_decode,
680 	.decode = codec_decode,
681 	.reduce_bitpool = codec_reduce_bitpool,
682 	.increase_bitpool = codec_increase_bitpool,
683 };
684 
685 A2DP_CODEC_EXPORT_DEF(
686 	"sbc",
687 	&a2dp_codec_sbc,
688 	&a2dp_codec_sbc_xq
689 );
690