summaryrefslogtreecommitdiffstats
path: root/spa/plugins/bluez5/a2dp-codec-lc3plus.c
blob: 289662442b9da247025e461c42b48ad3bbdef713 (plain)
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
/* Spa A2DP LC3plus HR codec
 *
 * Copyright © 2020 Wim Taymans
 * Copyright © 2022 Pauli Virtanen
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#include <unistd.h>
#include <stddef.h>
#include <errno.h>
#include <arpa/inet.h>
#if __BYTE_ORDER != __LITTLE_ENDIAN
#include <byteswap.h>
#endif

#include <spa/param/audio/format.h>
#include <spa/param/audio/format-utils.h>

#ifdef HAVE_LC3PLUS_H
#include <lc3plus.h>
#else
#include <lc3.h>
#endif

#include "rtp.h"
#include "media-codecs.h"

#define BITRATE_MIN 96000
#define BITRATE_MAX 512000
#define BITRATE_DEFAULT 160000

struct dec_data {
	int frame_size;
	int fragment_size;
	int fragment_count;
	uint8_t fragment[LC3PLUS_MAX_BYTES];
};

struct enc_data {
	struct rtp_header *header;
	struct rtp_payload *payload;

	int samples;
	int codesize;

	int packet_size;
	int fragment_size;
	int fragment_count;
	void *fragment;

	int bitrate;
	int next_bitrate;
};

struct impl {
	LC3PLUS_Enc *enc;
	LC3PLUS_Dec *dec;

	int mtu;
	int samplerate;
	int channels;
	int frame_dms;
	int bitrate;

	struct dec_data d;
	struct enc_data e;

	int32_t buf[2][LC3PLUS_MAX_SAMPLES];
};

static int codec_fill_caps(const struct media_codec *codec, uint32_t flags,
		uint8_t caps[A2DP_MAX_CAPS_SIZE])
{
	const a2dp_lc3plus_hr_t a2dp_lc3plus_hr = {
		.info = codec->vendor,
		LC3PLUS_HR_INIT_FRAME_DURATION(LC3PLUS_HR_FRAME_DURATION_10MS
				| LC3PLUS_HR_FRAME_DURATION_5MS
				| LC3PLUS_HR_FRAME_DURATION_2_5MS)
		.channels = LC3PLUS_HR_CHANNELS_1 | LC3PLUS_HR_CHANNELS_2,
		LC3PLUS_HR_INIT_FREQUENCY(LC3PLUS_HR_SAMPLING_FREQ_48000
				| (lc3plus_samplerate_supported(96000) ? LC3PLUS_HR_SAMPLING_FREQ_96000 : 0))
	};
	memcpy(caps, &a2dp_lc3plus_hr, sizeof(a2dp_lc3plus_hr));
	return sizeof(a2dp_lc3plus_hr);
}

static int codec_select_config(const struct media_codec *codec, uint32_t flags,
		const void *caps, size_t caps_size,
		const struct media_codec_audio_info *info,
		const struct spa_dict *settings, uint8_t config[A2DP_MAX_CAPS_SIZE])
{
	a2dp_lc3plus_hr_t conf;

	if (caps_size < sizeof(conf))
		return -EINVAL;

	memcpy(&conf, caps, sizeof(conf));

	if (codec->vendor.vendor_id != conf.info.vendor_id ||
	    codec->vendor.codec_id != conf.info.codec_id)
		return -ENOTSUP;

	if ((LC3PLUS_HR_GET_FREQUENCY(conf) & LC3PLUS_HR_SAMPLING_FREQ_48000)
			&& lc3plus_samplerate_supported(48000))
		LC3PLUS_HR_SET_FREQUENCY(conf, LC3PLUS_HR_SAMPLING_FREQ_48000);
	else if ((LC3PLUS_HR_GET_FREQUENCY(conf) & LC3PLUS_HR_SAMPLING_FREQ_96000)
			&& lc3plus_samplerate_supported(96000))
		LC3PLUS_HR_SET_FREQUENCY(conf, LC3PLUS_HR_SAMPLING_FREQ_96000);
	else
		return -ENOTSUP;

	if ((conf.channels & LC3PLUS_HR_CHANNELS_2) &&
			lc3plus_channels_supported(2))
		conf.channels = LC3PLUS_HR_CHANNELS_2;
	else if ((conf.channels & LC3PLUS_HR_CHANNELS_1) &&
			lc3plus_channels_supported(1))
		conf.channels = LC3PLUS_HR_CHANNELS_1;
	else
		return -ENOTSUP;

	if (LC3PLUS_HR_GET_FRAME_DURATION(conf) & LC3PLUS_HR_FRAME_DURATION_10MS)
		LC3PLUS_HR_SET_FRAME_DURATION(conf, LC3PLUS_HR_FRAME_DURATION_10MS);
	else if (LC3PLUS_HR_GET_FRAME_DURATION(conf) & LC3PLUS_HR_FRAME_DURATION_5MS)
		LC3PLUS_HR_SET_FRAME_DURATION(conf, LC3PLUS_HR_FRAME_DURATION_5MS);
	else if (LC3PLUS_HR_GET_FRAME_DURATION(conf) & LC3PLUS_HR_FRAME_DURATION_2_5MS)
		LC3PLUS_HR_SET_FRAME_DURATION(conf, LC3PLUS_HR_FRAME_DURATION_2_5MS);
	else
		return -ENOTSUP;

	memcpy(config, &conf, sizeof(conf));

	return sizeof(conf);
}

static int codec_caps_preference_cmp(const struct media_codec *codec, uint32_t flags, const void *caps1, size_t caps1_size,
		const void *caps2, size_t caps2_size, const struct media_codec_audio_info *info, const struct spa_dict *global_settings)
{
	a2dp_lc3plus_hr_t conf1, conf2;
	a2dp_lc3plus_hr_t *conf;
	int res1, res2;
	int a, b;

	/* Order selected configurations by preference */
	res1 = codec->select_config(codec, 0, caps1, caps1_size, info, NULL, (uint8_t *)&conf1);
	res2 = codec->select_config(codec, 0, caps2, caps2_size, info , NULL, (uint8_t *)&conf2);

#define PREFER_EXPR(expr)			\
		do {				\
			conf = &conf1; 		\
			a = (expr);		\
			conf = &conf2;		\
			b = (expr);		\
			if (a != b)		\
				return b - a;	\
		} while (0)

#define PREFER_BOOL(expr)	PREFER_EXPR((expr) ? 1 : 0)

	/* Prefer valid */
	a = (res1 > 0 && (size_t)res1 == sizeof(a2dp_lc3plus_hr_t)) ? 1 : 0;
	b = (res2 > 0 && (size_t)res2 == sizeof(a2dp_lc3plus_hr_t)) ? 1 : 0;
	if (!a || !b)
		return b - a;

	PREFER_BOOL(conf->channels & LC3PLUS_HR_CHANNELS_2);
	PREFER_BOOL(LC3PLUS_HR_GET_FREQUENCY(*conf) & (LC3PLUS_HR_SAMPLING_FREQ_48000 | LC3PLUS_HR_SAMPLING_FREQ_96000));
	PREFER_BOOL(LC3PLUS_HR_GET_FREQUENCY(*conf) & LC3PLUS_HR_SAMPLING_FREQ_48000);

	return 0;

#undef PREFER_EXPR
#undef PREFER_BOOL
}

static int codec_enum_config(const struct media_codec *codec, uint32_t flags,
		const void *caps, size_t caps_size, uint32_t id, uint32_t idx,
		struct spa_pod_builder *b, struct spa_pod **param)
{
	a2dp_lc3plus_hr_t conf;
	struct spa_pod_frame f[2];
	struct spa_pod_choice *choice;
	uint32_t position[SPA_AUDIO_MAX_CHANNELS];
	uint32_t i = 0;

	if (caps_size < sizeof(conf))
		return -EINVAL;

	memcpy(&conf, caps, sizeof(conf));

	if (idx > 0)
		return 0;

	spa_pod_builder_push_object(b, &f[0], SPA_TYPE_OBJECT_Format, id);
	spa_pod_builder_add(b,
			SPA_FORMAT_mediaType,      SPA_POD_Id(SPA_MEDIA_TYPE_audio),
			SPA_FORMAT_mediaSubtype,   SPA_POD_Id(SPA_MEDIA_SUBTYPE_raw),
			SPA_FORMAT_AUDIO_format,   SPA_POD_Id(SPA_AUDIO_FORMAT_S24_32),
			0);
	spa_pod_builder_prop(b, SPA_FORMAT_AUDIO_rate, 0);

	spa_pod_builder_push_choice(b, &f[1], SPA_CHOICE_None, 0);
	choice = (struct spa_pod_choice*)spa_pod_builder_frame(b, &f[1]);
	i = 0;
	if ((LC3PLUS_HR_GET_FREQUENCY(conf) & LC3PLUS_HR_SAMPLING_FREQ_96000) &&
			lc3plus_samplerate_supported(96000)) {
		if (i++ == 0)
			spa_pod_builder_int(b, 96000);
		spa_pod_builder_int(b, 96000);
	}
	if ((LC3PLUS_HR_GET_FREQUENCY(conf) & LC3PLUS_HR_SAMPLING_FREQ_48000) &&
			lc3plus_samplerate_supported(48000)) {
		if (i++ == 0)
			spa_pod_builder_int(b, 48000);
		spa_pod_builder_int(b, 48000);
	}
	if (i == 0)
		return -EINVAL;
	if (i > 1)
		choice->body.type = SPA_CHOICE_Enum;
	spa_pod_builder_pop(b, &f[1]);

	if ((conf.channels & (LC3PLUS_HR_CHANNELS_2 | LC3PLUS_HR_CHANNELS_1)) &&
			lc3plus_channels_supported(2) && lc3plus_channels_supported(1)) {
		spa_pod_builder_add(b,
				SPA_FORMAT_AUDIO_channels, SPA_POD_CHOICE_RANGE_Int(2, 1, 2),
				0);
	} else if ((conf.channels & LC3PLUS_HR_CHANNELS_2) && lc3plus_channels_supported(2)) {
		position[0] = SPA_AUDIO_CHANNEL_FL;
		position[1] = SPA_AUDIO_CHANNEL_FR;
		spa_pod_builder_add(b,
				SPA_FORMAT_AUDIO_channels, SPA_POD_Int(2),
				SPA_FORMAT_AUDIO_position, SPA_POD_Array(sizeof(uint32_t),
					SPA_TYPE_Id, 2, position),
				0);
	} else if ((conf.channels & LC3PLUS_HR_CHANNELS_1) && lc3plus_channels_supported(1)) {
		position[0] = SPA_AUDIO_CHANNEL_MONO;
		spa_pod_builder_add(b,
				SPA_FORMAT_AUDIO_channels, SPA_POD_Int(1),
				SPA_FORMAT_AUDIO_position, SPA_POD_Array(sizeof(uint32_t),
					SPA_TYPE_Id, 1, position),
				0);
	}

	*param = spa_pod_builder_pop(b, &f[0]);
	return *param == NULL ? -EIO : 1;
}

static int codec_validate_config(const struct media_codec *codec, uint32_t flags,
			const void *caps, size_t caps_size,
			struct spa_audio_info *info)
{
	const a2dp_lc3plus_hr_t *conf;

	if (caps == NULL || caps_size < sizeof(*conf))
		return -EINVAL;

	conf = caps;

	spa_zero(*info);
	info->media_type = SPA_MEDIA_TYPE_audio;
	info->media_subtype = SPA_MEDIA_SUBTYPE_raw;
	info->info.raw.format = SPA_AUDIO_FORMAT_S24_32;

	switch (LC3PLUS_HR_GET_FREQUENCY(*conf)) {
	case LC3PLUS_HR_SAMPLING_FREQ_96000:
		if (!lc3plus_samplerate_supported(96000))
			return -EINVAL;
		info->info.raw.rate = 96000;
		break;
	case LC3PLUS_HR_SAMPLING_FREQ_48000:
		if (!lc3plus_samplerate_supported(48000))
			return -EINVAL;
		info->info.raw.rate = 48000;
		break;
	default:
		return -EINVAL;
        }

	switch (conf->channels) {
	case LC3PLUS_HR_CHANNELS_2:
		if (!lc3plus_channels_supported(2))
			return -EINVAL;
		info->info.raw.channels = 2;
		info->info.raw.position[0] = SPA_AUDIO_CHANNEL_FL;
		info->info.raw.position[1] = SPA_AUDIO_CHANNEL_FR;
		break;
	case LC3PLUS_HR_CHANNELS_1:
		if (!lc3plus_channels_supported(1))
			return -EINVAL;
		info->info.raw.channels = 1;
		info->info.raw.position[0] = SPA_AUDIO_CHANNEL_MONO;
                break;
	default:
		return -EINVAL;
        }

	switch (LC3PLUS_HR_GET_FRAME_DURATION(*conf)) {
	case LC3PLUS_HR_FRAME_DURATION_10MS:
	case LC3PLUS_HR_FRAME_DURATION_5MS:
	case LC3PLUS_HR_FRAME_DURATION_2_5MS:
		break;
	default:
		return -EINVAL;
	}

	return 0;
}

static size_t ceildiv(size_t v, size_t divisor)
{
	if (v % divisor == 0)
		return v / divisor;
	else
		return v / divisor + 1;
}

static bool check_mtu_vs_frame_dms(struct impl *this)
{
	/* Only 10ms frames can be fragmented (max 0xf fragments);
	 * others must fit in single MTU */
	size_t header_size = sizeof(struct rtp_header) + sizeof(struct rtp_payload);
	size_t max_fragments = (this->frame_dms == 100) ? 0xf : 1;
	size_t payload_size = lc3plus_enc_get_num_bytes(this->enc);
	return (size_t)this->mtu >= header_size + ceildiv(payload_size, max_fragments);
}

static void *codec_init(const struct media_codec *codec, uint32_t flags,
		void *config, size_t config_len, const struct spa_audio_info *info,
		void *props, size_t mtu)
{
	a2dp_lc3plus_hr_t *conf = config;
	struct impl *this = NULL;
	struct spa_audio_info config_info;
	int size;
	int res;

	if (info->media_type != SPA_MEDIA_TYPE_audio ||
	    info->media_subtype != SPA_MEDIA_SUBTYPE_raw ||
	    info->info.raw.format != SPA_AUDIO_FORMAT_S24_32) {
		res = -EINVAL;
		goto error;
	}

	if ((this = calloc(1, sizeof(struct impl))) == NULL)
		goto error_errno;

	if ((res = codec_validate_config(codec, flags, config, config_len, &config_info)) < 0)
		goto error;

	this->mtu = mtu;
	this->samplerate = config_info.info.raw.rate;
	this->channels = config_info.info.raw.channels;
	this->bitrate = BITRATE_DEFAULT * this->channels;

	switch (LC3PLUS_HR_GET_FRAME_DURATION(*conf)) {
	case LC3PLUS_HR_FRAME_DURATION_10MS:
		this->frame_dms = 100;
		break;
	case LC3PLUS_HR_FRAME_DURATION_5MS:
		this->frame_dms = 50;
		break;
	case LC3PLUS_HR_FRAME_DURATION_2_5MS:
		this->frame_dms = 25;
		break;
	default:
		res = -EINVAL;
		goto error;
	}

	if ((size = lc3plus_enc_get_size(this->samplerate, this->channels)) == 0) {
		res = -EIO;
		goto error;
	}
	if ((this->enc = calloc(1, size)) == NULL)
		goto error_errno;
	if (lc3plus_enc_init(this->enc, this->samplerate, this->channels) != LC3PLUS_OK) {
		res = -EINVAL;
		goto error;
	}
	if (lc3plus_enc_set_frame_ms(this->enc, this->frame_dms/10.0f) != LC3PLUS_OK) {
		res = -EINVAL;
		goto error;
	}
	if (lc3plus_enc_set_hrmode(this->enc, 1) != LC3PLUS_OK) {
		res = -EINVAL;
		goto error;
	}
	while (1) {
		/* Find a valid bitrate */
		if (lc3plus_enc_set_bitrate(this->enc, this->bitrate) != LC3PLUS_OK) {
			res = -EINVAL;
			goto error;
		}
		if (check_mtu_vs_frame_dms(this))
			break;
		this->bitrate = this->bitrate * 3/4;
	}

	if ((size = lc3plus_dec_get_size(this->samplerate, this->channels)) == 0) {
		res = -EINVAL;
		goto error;
	}
	if ((this->dec = calloc(1, size)) == NULL)
		goto error_errno;
	if (lc3plus_dec_init(this->dec, this->samplerate, this->channels, LC3PLUS_PLC_ADVANCED) != LC3PLUS_OK) {
		res = -EINVAL;
		goto error;
	}
	if (lc3plus_dec_set_frame_ms(this->dec, this->frame_dms/10.0f) != LC3PLUS_OK) {
		res = -EINVAL;
		goto error;
	}
	if (lc3plus_dec_set_hrmode(this->dec, 1) != LC3PLUS_OK) {
		res = -EINVAL;
		goto error;
	}

	this->e.samples = lc3plus_enc_get_input_samples(this->enc);
	this->e.codesize = this->e.samples * this->channels * sizeof(int32_t);

	spa_assert(this->e.samples <= LC3PLUS_MAX_SAMPLES);

	this->e.bitrate = this->bitrate;
	this->e.next_bitrate = this->bitrate;

	return this;

error_errno:
	res = -errno;
	goto error;

error:
	if (this && this->enc)
		lc3plus_enc_free_memory(this->enc);
	if (this && this->dec)
		lc3plus_dec_free_memory(this->dec);
	free(this);
	errno = -res;
	return NULL;
}

static void codec_deinit(void *data)
{
	struct impl *this = data;
	lc3plus_enc_free_memory(this->enc);
	lc3plus_dec_free_memory(this->dec);
	free(this);
}

static int codec_get_block_size(void *data)
{
	struct impl *this = data;
	return this->e.codesize;
}

static int codec_abr_process (void *data, size_t unsent)
{
	return -ENOTSUP;
}

static int codec_update_bitrate(struct impl *this)
{
	this->e.next_bitrate = SPA_CLAMP(this->e.next_bitrate,
			BITRATE_MIN * this->channels, BITRATE_MAX * this->channels);

	if (this->e.next_bitrate == this->e.bitrate)
		return 0;

	this->e.bitrate = this->e.next_bitrate;

	if (lc3plus_enc_set_bitrate(this->enc, this->e.bitrate) != LC3PLUS_OK ||
			!check_mtu_vs_frame_dms(this)) {
		lc3plus_enc_set_bitrate(this->enc, this->bitrate);
		return -EINVAL;
	}

	this->bitrate = this->e.bitrate;

	return 0;
}

static int codec_start_encode (void *data,
		void *dst, size_t dst_size, uint16_t seqnum, uint32_t timestamp)
{
	struct impl *this = data;
	size_t header_size = sizeof(struct rtp_header) + sizeof(struct rtp_payload);

	if (dst_size <= header_size)
		return -EINVAL;

	codec_update_bitrate(this);

	this->e.header = (struct rtp_header *)dst;
	this->e.payload = SPA_PTROFF(dst, sizeof(struct rtp_header), struct rtp_payload);
	memset(dst, 0, header_size);

	this->e.payload->frame_count = 0;
	this->e.header->v = 2;
	this->e.header->pt = 96;
	this->e.header->sequence_number = htons(seqnum);
	this->e.header->timestamp = htonl(timestamp);
	this->e.header->ssrc = htonl(1);

	this->e.packet_size = header_size;
	return this->e.packet_size;
}

static void deinterleave_32_c2(int32_t * SPA_RESTRICT * SPA_RESTRICT dst, const int32_t * SPA_RESTRICT src, size_t n_samples)
{
	/* We'll trust the compiler to optimize this */
	const size_t n_channels = 2;
	size_t i, j;
	for (j = 0; j < n_samples; ++j)
		for (i = 0; i < n_channels; ++i)
			dst[i][j] = *src++;
}

static void interleave_32_c2(int32_t * SPA_RESTRICT dst, const int32_t * SPA_RESTRICT * SPA_RESTRICT src, size_t n_samples)
{
	const size_t n_channels = 2;
	size_t i, j;
	for (j = 0; j < n_samples; ++j)
		for (i = 0; i < n_channels; ++i)
			*dst++ = src[i][j];
}

static int codec_encode(void *data,
		const void *src, size_t src_size,
		void *dst, size_t dst_size,
		size_t *dst_out, int *need_flush)
{
	struct impl *this = data;
	int frame_bytes;
	LC3PLUS_Error res;
	int size, processed;
	int header_size = sizeof(struct rtp_header) + sizeof(struct rtp_payload);
	int32_t *inputs[2];

	if (src == NULL) {
		/* Produce fragment packets.
		 *
		 * We assume the caller gives the same buffer here as in the previous
		 * calls to encode(), without changes in the buffer content.
		 */
		if (this->e.fragment == NULL ||
				this->e.fragment_count <= 1 ||
				this->e.fragment < dst ||
				SPA_PTROFF(this->e.fragment, this->e.fragment_size, void) > SPA_PTROFF(dst, dst_size, void)) {
			this->e.fragment = NULL;
			return -EINVAL;
		}

		size = SPA_MIN(this->mtu - header_size, this->e.fragment_size);
		memmove(dst, this->e.fragment, size);
		*dst_out = size;

		this->e.payload->is_fragmented = 1;
		this->e.payload->frame_count = --this->e.fragment_count;
		this->e.payload->is_last_fragment = (this->e.fragment_count == 1);

		if (this->e.fragment_size > size && this->e.fragment_count > 1) {
			this->e.fragment = SPA_PTROFF(this->e.fragment, size, void);
			this->e.fragment_size -= size;
			*need_flush = NEED_FLUSH_FRAGMENT;
		} else {
			this->e.fragment = NULL;
			*need_flush = NEED_FLUSH_ALL;
		}
		return 0;
	}

	frame_bytes = lc3plus_enc_get_num_bytes(this->enc);
	processed = 0;

	if (src_size < (size_t)this->e.codesize)
		goto done;
	if (dst_size < (size_t)frame_bytes)
		goto done;
	if (this->e.payload->frame_count > 0 &&
			this->e.packet_size + frame_bytes > this->mtu)
		goto done;

	if (this->channels == 1) {
		inputs[0] = (int32_t *)src;
		res = lc3plus_enc24(this->enc, inputs, dst, &size);
	} else {
		inputs[0] = this->buf[0];
		inputs[1] = this->buf[1];
		deinterleave_32_c2(inputs, src, this->e.samples);
		res = lc3plus_enc24(this->enc, inputs, dst, &size);
	}
	if (SPA_UNLIKELY(res != LC3PLUS_OK))
		return -EINVAL;
	*dst_out = size;

	processed += this->e.codesize;
	this->e.packet_size += size;
	this->e.payload->frame_count++;

done:
	if (this->e.payload->frame_count == 0)
		return processed;
	if (this->e.payload->frame_count < 0xf &&
			this->frame_dms * (this->e.payload->frame_count + 1) < 200 &&
			this->e.packet_size + frame_bytes <= this->mtu)
		return processed;  /* add another frame */

	if (this->e.packet_size > this->mtu) {
		/* Fragment packet */
		spa_assert(this->e.payload->frame_count == 1);
		spa_assert(this->frame_dms == 100);

		this->e.fragment_count = ceildiv(this->e.packet_size - header_size,
				this->mtu - header_size);

		this->e.payload->is_fragmented = 1;
		this->e.payload->is_first_fragment = 1;
		this->e.payload->frame_count = this->e.fragment_count;

		this->e.fragment_size = this->e.packet_size - this->mtu;
		this->e.fragment = SPA_PTROFF(dst, *dst_out - this->e.fragment_size, void);
		*need_flush = NEED_FLUSH_FRAGMENT;

		/*
		 * We keep the rest of the encoded frame in the same buffer, and rely
		 * that the caller won't overwrite it before the next call to encode()
		 */
		*dst_out = SPA_PTRDIFF(this->e.fragment, dst);
	} else {
		*need_flush = NEED_FLUSH_ALL;
	}

	return processed;
}

static SPA_UNUSED int codec_start_decode (void *data,
		const void *src, size_t src_size, uint16_t *seqnum, uint32_t *timestamp)
{
	struct impl *this = data;
	const struct rtp_header *header = src;
	const struct rtp_payload *payload = SPA_PTROFF(src, sizeof(struct rtp_header), void);
	size_t header_size = sizeof(struct rtp_header) + sizeof(struct rtp_payload);

	spa_return_val_if_fail (src_size > header_size, -EINVAL);

	if (seqnum)
		*seqnum = ntohs(header->sequence_number);
	if (timestamp)
		*timestamp = ntohl(header->timestamp);

	if (payload->is_fragmented) {
		if (payload->is_first_fragment) {
			this->d.fragment_size = 0;
		} else if (payload->frame_count + 1 != this->d.fragment_count ||
				(payload->frame_count == 1 && !payload->is_last_fragment)){
			/* Fragments not in right order: drop packet */
			return -EINVAL;
		}
		this->d.fragment_count = payload->frame_count;
		this->d.frame_size = src_size - header_size;
	} else {
		if (payload->frame_count <= 0)
			return -EINVAL;
		this->d.fragment_count = 0;
		this->d.frame_size = (src_size - header_size) / payload->frame_count;
		if (this->d.frame_size <= 0)
			return -EINVAL;
	}

	return header_size;
}

static SPA_UNUSED int codec_decode(void *data,
		const void *src, size_t src_size,
		void *dst, size_t dst_size,
		size_t *dst_out)
{
	struct impl *this = data;
	LC3PLUS_Error res;
	int32_t *outputs[2];
	int consumed;
	int samples;

	if (this->d.fragment_count > 0) {
		/* Fragmented frame */
		size_t avail;
		avail = SPA_MIN(sizeof(this->d.fragment) - this->d.fragment_size, src_size);
		memcpy(SPA_PTROFF(this->d.fragment, this->d.fragment_size, void), src, avail);

		this->d.fragment_size += avail;
		consumed = src_size;

		if (this->d.fragment_count > 1) {
			/* More fragments to come */
			*dst_out = 0;
			return consumed;
		}

		src = this->d.fragment;
		src_size = this->d.fragment_size;

		this->d.fragment_count = 0;
		this->d.fragment_size = 0;
	} else {
		src_size = SPA_MIN((size_t)this->d.frame_size, src_size);
		consumed = src_size;
	}

	samples = lc3plus_dec_get_output_samples(this->dec);
	*dst_out = samples * this->channels * sizeof(int32_t);
	if (dst_size < *dst_out)
		return -EINVAL;

	if (this->channels == 1) {
		outputs[0] = (int32_t *)dst;
		res = lc3plus_dec24(this->dec, (void *)src, src_size, outputs, 0);
	} else {
		outputs[0] = this->buf[0];
		outputs[1] = this->buf[1];
		res = lc3plus_dec24(this->dec, (void *)src, src_size, outputs, 0);
		interleave_32_c2(dst, (const int32_t**)outputs, samples);
	}
	if (SPA_UNLIKELY(res != LC3PLUS_OK && res != LC3PLUS_DECODE_ERROR))
		return -EINVAL;

	return consumed;
}

static int codec_reduce_bitpool(void *data)
{
	struct impl *this = data;
	this->e.next_bitrate = SPA_CLAMP(this->bitrate * 3 / 4,
			BITRATE_MIN * this->channels, BITRATE_MAX * this->channels);
	return this->e.next_bitrate;
}

static int codec_increase_bitpool(void *data)
{
	struct impl *this = data;
	this->e.next_bitrate = SPA_CLAMP(this->bitrate * 5 / 4,
			BITRATE_MIN * this->channels, BITRATE_MAX * this->channels);
	return this->e.next_bitrate;
}

const struct media_codec a2dp_codec_lc3plus_hr = {
	.id = SPA_BLUETOOTH_AUDIO_CODEC_LC3PLUS_HR,
	.name = "lc3plus_hr",
	.codec_id = A2DP_CODEC_VENDOR,
	.vendor = { .vendor_id = LC3PLUS_HR_VENDOR_ID,
		.codec_id = LC3PLUS_HR_CODEC_ID },
	.description = "LC3plus HR",
	.fill_caps = codec_fill_caps,
	.select_config = codec_select_config,
	.enum_config = codec_enum_config,
	.validate_config = codec_validate_config,
	.caps_preference_cmp = codec_caps_preference_cmp,
	.init = codec_init,
	.deinit = codec_deinit,
	.get_block_size = codec_get_block_size,
	.abr_process = codec_abr_process,
	.start_encode = codec_start_encode,
	.encode = codec_encode,
	.start_decode = codec_start_decode,
	.decode = codec_decode,
	.reduce_bitpool = codec_reduce_bitpool,
	.increase_bitpool = codec_increase_bitpool
};

MEDIA_CODEC_EXPORT_DEF(
	"lc3plus",
	&a2dp_codec_lc3plus_hr
);