Skip to content

Commit bc5d8d3

Browse files
committed
Simplify lookup for A2DP channel mode and sampling
1 parent 54c06ce commit bc5d8d3

File tree

10 files changed

+276
-272
lines changed

10 files changed

+276
-272
lines changed

src/a2dp-aac.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -456,9 +456,10 @@ void *a2dp_aac_dec_thread(struct ba_transport_pcm *t_pcm) {
456456
static const struct a2dp_channel_mode a2dp_aac_channels[] = {
457457
{ A2DP_CHM_MONO, 1, AAC_CHANNELS_1 },
458458
{ A2DP_CHM_STEREO, 2, AAC_CHANNELS_2 },
459+
{ 0 },
459460
};
460461

461-
static const struct a2dp_sampling_freq a2dp_aac_samplings[] = {
462+
static const struct a2dp_sampling a2dp_aac_samplings[] = {
462463
{ 8000, AAC_SAMPLING_FREQ_8000 },
463464
{ 11025, AAC_SAMPLING_FREQ_11025 },
464465
{ 12000, AAC_SAMPLING_FREQ_12000 },
@@ -471,17 +472,24 @@ static const struct a2dp_sampling_freq a2dp_aac_samplings[] = {
471472
{ 64000, AAC_SAMPLING_FREQ_64000 },
472473
{ 88200, AAC_SAMPLING_FREQ_88200 },
473474
{ 96000, AAC_SAMPLING_FREQ_96000 },
475+
{ 0 },
474476
};
475477

476478
static int a2dp_aac_transport_init(struct ba_transport *t) {
477479

478-
const struct a2dp_codec *codec = t->a2dp.codec;
480+
const struct a2dp_channel_mode *chm;
481+
if ((chm = a2dp_channel_mode_lookup(a2dp_aac_channels,
482+
t->a2dp.configuration.aac.channels)) == NULL)
483+
return -1;
484+
485+
const struct a2dp_sampling *sampling;
486+
if ((sampling = a2dp_sampling_lookup(a2dp_aac_samplings,
487+
AAC_GET_FREQUENCY(t->a2dp.configuration.aac))) == NULL)
488+
return -1;
479489

480490
t->a2dp.pcm.format = BA_TRANSPORT_PCM_FORMAT_S16_2LE;
481-
t->a2dp.pcm.channels = a2dp_codec_lookup_channels(codec,
482-
t->a2dp.configuration.aac.channels, false);
483-
t->a2dp.pcm.sampling = a2dp_codec_lookup_frequency(codec,
484-
AAC_GET_FREQUENCY(t->a2dp.configuration.aac), false);
491+
t->a2dp.pcm.channels = chm->channels;
492+
t->a2dp.pcm.sampling = sampling->frequency;
485493

486494
return 0;
487495
}
@@ -546,9 +554,7 @@ struct a2dp_codec a2dp_aac_source = {
546554
},
547555
.capabilities_size = sizeof(a2dp_aac_t),
548556
.channels[0] = a2dp_aac_channels,
549-
.channels_size[0] = ARRAYSIZE(a2dp_aac_channels),
550557
.samplings[0] = a2dp_aac_samplings,
551-
.samplings_size[0] = ARRAYSIZE(a2dp_aac_samplings),
552558
.init = a2dp_aac_source_init,
553559
.transport_init = a2dp_aac_transport_init,
554560
.transport_start = a2dp_aac_source_transport_start,
@@ -607,9 +613,7 @@ struct a2dp_codec a2dp_aac_sink = {
607613
},
608614
.capabilities_size = sizeof(a2dp_aac_t),
609615
.channels[0] = a2dp_aac_channels,
610-
.channels_size[0] = ARRAYSIZE(a2dp_aac_channels),
611616
.samplings[0] = a2dp_aac_samplings,
612-
.samplings_size[0] = ARRAYSIZE(a2dp_aac_samplings),
613617
.init = a2dp_aac_sink_init,
614618
.transport_init = a2dp_aac_transport_init,
615619
.transport_start = a2dp_aac_sink_transport_start,

src/a2dp-aptx-hd.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -271,24 +271,32 @@ void *a2dp_aptx_hd_dec_thread(struct ba_transport_pcm *t_pcm) {
271271

272272
static const struct a2dp_channel_mode a2dp_aptx_hd_channels[] = {
273273
{ A2DP_CHM_STEREO, 2, APTX_CHANNEL_MODE_STEREO },
274+
{ 0 },
274275
};
275276

276-
static const struct a2dp_sampling_freq a2dp_aptx_hd_samplings[] = {
277+
static const struct a2dp_sampling a2dp_aptx_hd_samplings[] = {
277278
{ 16000, APTX_SAMPLING_FREQ_16000 },
278279
{ 32000, APTX_SAMPLING_FREQ_32000 },
279280
{ 44100, APTX_SAMPLING_FREQ_44100 },
280281
{ 48000, APTX_SAMPLING_FREQ_48000 },
282+
{ 0 },
281283
};
282284

283285
static int a2dp_aptx_hd_transport_init(struct ba_transport *t) {
284286

285-
const struct a2dp_codec *codec = t->a2dp.codec;
287+
const struct a2dp_channel_mode *chm;
288+
if ((chm = a2dp_channel_mode_lookup(a2dp_aptx_hd_channels,
289+
t->a2dp.configuration.aptx_hd.aptx.channel_mode)) == NULL)
290+
return -1;
291+
292+
const struct a2dp_sampling *sampling;
293+
if ((sampling = a2dp_sampling_lookup(a2dp_aptx_hd_samplings,
294+
t->a2dp.configuration.aptx_hd.aptx.frequency)) == NULL)
295+
return -1;
286296

287297
t->a2dp.pcm.format = BA_TRANSPORT_PCM_FORMAT_S24_4LE;
288-
t->a2dp.pcm.channels = a2dp_codec_lookup_channels(codec,
289-
t->a2dp.configuration.aptx_hd.aptx.channel_mode, false);
290-
t->a2dp.pcm.sampling = a2dp_codec_lookup_frequency(codec,
291-
t->a2dp.configuration.aptx_hd.aptx.frequency, false);
298+
t->a2dp.pcm.channels = chm->channels;
299+
t->a2dp.pcm.sampling = sampling->frequency;
292300

293301
return 0;
294302
}
@@ -323,9 +331,7 @@ struct a2dp_codec a2dp_aptx_hd_source = {
323331
},
324332
.capabilities_size = sizeof(a2dp_aptx_hd_t),
325333
.channels[0] = a2dp_aptx_hd_channels,
326-
.channels_size[0] = ARRAYSIZE(a2dp_aptx_hd_channels),
327334
.samplings[0] = a2dp_aptx_hd_samplings,
328-
.samplings_size[0] = ARRAYSIZE(a2dp_aptx_hd_samplings),
329335
.init = a2dp_aptx_hd_source_init,
330336
.transport_init = a2dp_aptx_hd_transport_init,
331337
.transport_start = a2dp_aptx_hd_source_transport_start,
@@ -355,9 +361,7 @@ struct a2dp_codec a2dp_aptx_hd_sink = {
355361
},
356362
.capabilities_size = sizeof(a2dp_aptx_hd_t),
357363
.channels[0] = a2dp_aptx_hd_channels,
358-
.channels_size[0] = ARRAYSIZE(a2dp_aptx_hd_channels),
359364
.samplings[0] = a2dp_aptx_hd_samplings,
360-
.samplings_size[0] = ARRAYSIZE(a2dp_aptx_hd_samplings),
361365
.transport_init = a2dp_aptx_hd_transport_init,
362366
.transport_start = a2dp_aptx_hd_sink_transport_start,
363367
};

src/a2dp-aptx.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -234,24 +234,32 @@ void *a2dp_aptx_dec_thread(struct ba_transport_pcm *t_pcm) {
234234

235235
static const struct a2dp_channel_mode a2dp_aptx_channels[] = {
236236
{ A2DP_CHM_STEREO, 2, APTX_CHANNEL_MODE_STEREO },
237+
{ 0 },
237238
};
238239

239-
static const struct a2dp_sampling_freq a2dp_aptx_samplings[] = {
240+
static const struct a2dp_sampling a2dp_aptx_samplings[] = {
240241
{ 16000, APTX_SAMPLING_FREQ_16000 },
241242
{ 32000, APTX_SAMPLING_FREQ_32000 },
242243
{ 44100, APTX_SAMPLING_FREQ_44100 },
243244
{ 48000, APTX_SAMPLING_FREQ_48000 },
245+
{ 0 },
244246
};
245247

246248
static int a2dp_aptx_transport_init(struct ba_transport *t) {
247249

248-
const struct a2dp_codec *codec = t->a2dp.codec;
250+
const struct a2dp_channel_mode *chm;
251+
if ((chm = a2dp_channel_mode_lookup(a2dp_aptx_channels,
252+
t->a2dp.configuration.aptx.channel_mode)) == NULL)
253+
return -1;
254+
255+
const struct a2dp_sampling *sampling;
256+
if ((sampling = a2dp_sampling_lookup(a2dp_aptx_samplings,
257+
t->a2dp.configuration.aptx.frequency)) == NULL)
258+
return -1;
249259

250260
t->a2dp.pcm.format = BA_TRANSPORT_PCM_FORMAT_S16_2LE;
251-
t->a2dp.pcm.channels = a2dp_codec_lookup_channels(codec,
252-
t->a2dp.configuration.aptx.channel_mode, false);
253-
t->a2dp.pcm.sampling = a2dp_codec_lookup_frequency(codec,
254-
t->a2dp.configuration.aptx.frequency, false);
261+
t->a2dp.pcm.channels = chm->channels;
262+
t->a2dp.pcm.sampling = sampling->frequency;
255263

256264
return 0;
257265
}
@@ -286,9 +294,7 @@ struct a2dp_codec a2dp_aptx_source = {
286294
},
287295
.capabilities_size = sizeof(a2dp_aptx_t),
288296
.channels[0] = a2dp_aptx_channels,
289-
.channels_size[0] = ARRAYSIZE(a2dp_aptx_channels),
290297
.samplings[0] = a2dp_aptx_samplings,
291-
.samplings_size[0] = ARRAYSIZE(a2dp_aptx_samplings),
292298
.init = a2dp_aptx_source_init,
293299
.transport_init = a2dp_aptx_transport_init,
294300
.transport_start = a2dp_aptx_source_transport_start,
@@ -318,9 +324,7 @@ struct a2dp_codec a2dp_aptx_sink = {
318324
},
319325
.capabilities_size = sizeof(a2dp_aptx_t),
320326
.channels[0] = a2dp_aptx_channels,
321-
.channels_size[0] = ARRAYSIZE(a2dp_aptx_channels),
322327
.samplings[0] = a2dp_aptx_samplings,
323-
.samplings_size[0] = ARRAYSIZE(a2dp_aptx_samplings),
324328
.transport_init = a2dp_aptx_transport_init,
325329
.transport_start = a2dp_aptx_sink_transport_start,
326330
};

src/a2dp-faststream.c

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -240,32 +240,43 @@ void *a2dp_faststream_dec_thread(struct ba_transport_pcm *t_pcm) {
240240
return NULL;
241241
}
242242

243-
static const struct a2dp_sampling_freq a2dp_faststream_samplings_music[] = {
243+
static const struct a2dp_sampling a2dp_faststream_samplings_music[] = {
244244
{ 44100, FASTSTREAM_SAMPLING_FREQ_MUSIC_44100 },
245245
{ 48000, FASTSTREAM_SAMPLING_FREQ_MUSIC_48000 },
246+
{ 0 },
246247
};
247248

248-
static const struct a2dp_sampling_freq a2dp_faststream_samplings_voice[] = {
249+
static const struct a2dp_sampling a2dp_faststream_samplings_voice[] = {
249250
{ 16000, FASTSTREAM_SAMPLING_FREQ_VOICE_16000 },
251+
{ 0 },
250252
};
251253

252254
static int a2dp_faststream_transport_init(struct ba_transport *t) {
253255

254-
const struct a2dp_codec *codec = t->a2dp.codec;
256+
if (t->a2dp.configuration.faststream.direction & FASTSTREAM_DIRECTION_MUSIC) {
255257

256-
t->a2dp.pcm.format = BA_TRANSPORT_PCM_FORMAT_S16_2LE;
257-
t->a2dp.pcm_bc.format = BA_TRANSPORT_PCM_FORMAT_S16_2LE;
258+
const struct a2dp_sampling *sampling;
259+
if ((sampling = a2dp_sampling_lookup(a2dp_faststream_samplings_music,
260+
t->a2dp.configuration.faststream.frequency_music)) == NULL)
261+
return -1;
258262

259-
if (t->a2dp.configuration.faststream.direction & FASTSTREAM_DIRECTION_MUSIC) {
263+
t->a2dp.pcm.format = BA_TRANSPORT_PCM_FORMAT_S16_2LE;
264+
t->a2dp.pcm.sampling = sampling->frequency;
260265
t->a2dp.pcm.channels = 2;
261-
t->a2dp.pcm.sampling = a2dp_codec_lookup_frequency(codec,
262-
t->a2dp.configuration.faststream.frequency_music, false);
266+
263267
}
264268

265269
if (t->a2dp.configuration.faststream.direction & FASTSTREAM_DIRECTION_VOICE) {
270+
271+
const struct a2dp_sampling *sampling;
272+
if ((sampling = a2dp_sampling_lookup(a2dp_faststream_samplings_voice,
273+
t->a2dp.configuration.faststream.frequency_voice)) == NULL)
274+
return -1;
275+
276+
t->a2dp.pcm_bc.format = BA_TRANSPORT_PCM_FORMAT_S16_2LE;
277+
t->a2dp.pcm_bc.sampling = sampling->frequency;
266278
t->a2dp.pcm_bc.channels = 1;
267-
t->a2dp.pcm_bc.sampling = a2dp_codec_lookup_frequency(codec,
268-
t->a2dp.configuration.faststream.frequency_voice, true);
279+
269280
}
270281

271282
return 0;
@@ -308,9 +319,7 @@ struct a2dp_codec a2dp_faststream_source = {
308319
},
309320
.capabilities_size = sizeof(a2dp_faststream_t),
310321
.samplings[0] = a2dp_faststream_samplings_music,
311-
.samplings_size[0] = ARRAYSIZE(a2dp_faststream_samplings_music),
312322
.samplings[1] = a2dp_faststream_samplings_voice,
313-
.samplings_size[1] = ARRAYSIZE(a2dp_faststream_samplings_voice),
314323
.init = a2dp_faststream_source_init,
315324
.transport_init = a2dp_faststream_transport_init,
316325
.transport_start = a2dp_faststream_source_transport_start,
@@ -345,9 +354,7 @@ struct a2dp_codec a2dp_faststream_sink = {
345354
},
346355
.capabilities_size = sizeof(a2dp_faststream_t),
347356
.samplings[0] = a2dp_faststream_samplings_music,
348-
.samplings_size[0] = ARRAYSIZE(a2dp_faststream_samplings_music),
349357
.samplings[1] = a2dp_faststream_samplings_voice,
350-
.samplings_size[1] = ARRAYSIZE(a2dp_faststream_samplings_voice),
351358
.transport_init = a2dp_faststream_transport_init,
352359
.transport_start = a2dp_faststream_sink_transport_start,
353360
};

src/a2dp-lc3plus.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -538,22 +538,30 @@ void *a2dp_lc3plus_dec_thread(struct ba_transport_pcm *t_pcm) {
538538
static const struct a2dp_channel_mode a2dp_lc3plus_channels[] = {
539539
{ A2DP_CHM_MONO, 1, LC3PLUS_CHANNELS_1 },
540540
{ A2DP_CHM_STEREO, 2, LC3PLUS_CHANNELS_2 },
541+
{ 0 },
541542
};
542543

543-
static const struct a2dp_sampling_freq a2dp_lc3plus_samplings[] = {
544+
static const struct a2dp_sampling a2dp_lc3plus_samplings[] = {
544545
{ 48000, LC3PLUS_SAMPLING_FREQ_48000 },
545546
{ 96000, LC3PLUS_SAMPLING_FREQ_96000 },
547+
{ 0 },
546548
};
547549

548550
static int a2dp_lc3plus_transport_init(struct ba_transport *t) {
549551

550-
const struct a2dp_codec *codec = t->a2dp.codec;
552+
const struct a2dp_channel_mode *chm;
553+
if ((chm = a2dp_channel_mode_lookup(a2dp_lc3plus_channels,
554+
t->a2dp.configuration.lc3plus.channels)) == NULL)
555+
return -1;
556+
557+
const struct a2dp_sampling *sampling;
558+
if ((sampling = a2dp_sampling_lookup(a2dp_lc3plus_samplings,
559+
LC3PLUS_GET_FREQUENCY(t->a2dp.configuration.lc3plus))) == NULL)
560+
return -1;
551561

552562
t->a2dp.pcm.format = BA_TRANSPORT_PCM_FORMAT_S24_4LE;
553-
t->a2dp.pcm.channels = a2dp_codec_lookup_channels(codec,
554-
t->a2dp.configuration.lc3plus.channels, false);
555-
t->a2dp.pcm.sampling = a2dp_codec_lookup_frequency(codec,
556-
LC3PLUS_GET_FREQUENCY(t->a2dp.configuration.lc3plus), false);
563+
t->a2dp.pcm.channels = chm->channels;
564+
t->a2dp.pcm.sampling = sampling->frequency;
557565

558566
return 0;
559567
}
@@ -589,9 +597,7 @@ struct a2dp_codec a2dp_lc3plus_source = {
589597
},
590598
.capabilities_size = sizeof(a2dp_lc3plus_t),
591599
.channels[0] = a2dp_lc3plus_channels,
592-
.channels_size[0] = ARRAYSIZE(a2dp_lc3plus_channels),
593600
.samplings[0] = a2dp_lc3plus_samplings,
594-
.samplings_size[0] = ARRAYSIZE(a2dp_lc3plus_samplings),
595601
.init = a2dp_lc3plus_source_init,
596602
.transport_init = a2dp_lc3plus_transport_init,
597603
.transport_start = a2dp_lc3plus_source_transport_start,
@@ -620,9 +626,7 @@ struct a2dp_codec a2dp_lc3plus_sink = {
620626
},
621627
.capabilities_size = sizeof(a2dp_lc3plus_t),
622628
.channels[0] = a2dp_lc3plus_channels,
623-
.channels_size[0] = ARRAYSIZE(a2dp_lc3plus_channels),
624629
.samplings[0] = a2dp_lc3plus_samplings,
625-
.samplings_size[0] = ARRAYSIZE(a2dp_lc3plus_samplings),
626630
.transport_init = a2dp_lc3plus_transport_init,
627631
.transport_start = a2dp_lc3plus_sink_transport_start,
628632
};

src/a2dp-ldac.c

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -326,27 +326,34 @@ static const struct a2dp_channel_mode a2dp_ldac_channels[] = {
326326
{ A2DP_CHM_MONO, 1, LDAC_CHANNEL_MODE_MONO },
327327
{ A2DP_CHM_DUAL_CHANNEL, 2, LDAC_CHANNEL_MODE_DUAL },
328328
{ A2DP_CHM_STEREO, 2, LDAC_CHANNEL_MODE_STEREO },
329+
{ 0 },
329330
};
330331

331-
static const struct a2dp_sampling_freq a2dp_ldac_samplings[] = {
332+
static const struct a2dp_sampling a2dp_ldac_samplings[] = {
332333
{ 44100, LDAC_SAMPLING_FREQ_44100 },
333334
{ 48000, LDAC_SAMPLING_FREQ_48000 },
334335
{ 88200, LDAC_SAMPLING_FREQ_88200 },
335336
{ 96000, LDAC_SAMPLING_FREQ_96000 },
337+
{ 0 },
336338
};
337339

338340
static int a2dp_ldac_transport_init(struct ba_transport *t) {
339341

340-
const struct a2dp_codec *codec = t->a2dp.codec;
342+
const struct a2dp_channel_mode *chm;
343+
if ((chm = a2dp_channel_mode_lookup(a2dp_ldac_channels,
344+
t->a2dp.configuration.ldac.channel_mode)) == NULL)
345+
return -1;
346+
347+
const struct a2dp_sampling *sampling;
348+
if ((sampling = a2dp_sampling_lookup(a2dp_ldac_samplings,
349+
t->a2dp.configuration.ldac.frequency)) == NULL)
350+
return -1;
341351

342352
/* LDAC library internally for encoding uses 31-bit integers or
343353
* floats, so the best choice for PCM sample is signed 32-bit. */
344354
t->a2dp.pcm.format = BA_TRANSPORT_PCM_FORMAT_S32_4LE;
345-
346-
t->a2dp.pcm.channels = a2dp_codec_lookup_channels(codec,
347-
t->a2dp.configuration.ldac.channel_mode, false);
348-
t->a2dp.pcm.sampling = a2dp_codec_lookup_frequency(codec,
349-
t->a2dp.configuration.ldac.frequency, false);
355+
t->a2dp.pcm.channels = chm->channels;
356+
t->a2dp.pcm.sampling = sampling->frequency;
350357

351358
return 0;
352359
}
@@ -383,9 +390,7 @@ struct a2dp_codec a2dp_ldac_source = {
383390
},
384391
.capabilities_size = sizeof(a2dp_ldac_t),
385392
.channels[0] = a2dp_ldac_channels,
386-
.channels_size[0] = ARRAYSIZE(a2dp_ldac_channels),
387393
.samplings[0] = a2dp_ldac_samplings,
388-
.samplings_size[0] = ARRAYSIZE(a2dp_ldac_samplings),
389394
.init = a2dp_ldac_source_init,
390395
.transport_init = a2dp_ldac_transport_init,
391396
.transport_start = a2dp_ldac_source_transport_start,
@@ -417,9 +422,7 @@ struct a2dp_codec a2dp_ldac_sink = {
417422
},
418423
.capabilities_size = sizeof(a2dp_ldac_t),
419424
.channels[0] = a2dp_ldac_channels,
420-
.channels_size[0] = ARRAYSIZE(a2dp_ldac_channels),
421425
.samplings[0] = a2dp_ldac_samplings,
422-
.samplings_size[0] = ARRAYSIZE(a2dp_ldac_samplings),
423426
.transport_init = a2dp_ldac_transport_init,
424427
.transport_start = a2dp_ldac_sink_transport_start,
425428
};

0 commit comments

Comments
 (0)