Skip to content

Commit

Permalink
Simplify lookup for A2DP channel mode and sampling
Browse files Browse the repository at this point in the history
  • Loading branch information
arkq committed Jan 5, 2024
1 parent 54c06ce commit bc5d8d3
Show file tree
Hide file tree
Showing 10 changed files with 276 additions and 272 deletions.
24 changes: 14 additions & 10 deletions src/a2dp-aac.c
Original file line number Diff line number Diff line change
Expand Up @@ -456,9 +456,10 @@ void *a2dp_aac_dec_thread(struct ba_transport_pcm *t_pcm) {
static const struct a2dp_channel_mode a2dp_aac_channels[] = {
{ A2DP_CHM_MONO, 1, AAC_CHANNELS_1 },
{ A2DP_CHM_STEREO, 2, AAC_CHANNELS_2 },
{ 0 },
};

static const struct a2dp_sampling_freq a2dp_aac_samplings[] = {
static const struct a2dp_sampling a2dp_aac_samplings[] = {
{ 8000, AAC_SAMPLING_FREQ_8000 },
{ 11025, AAC_SAMPLING_FREQ_11025 },
{ 12000, AAC_SAMPLING_FREQ_12000 },
Expand All @@ -471,17 +472,24 @@ static const struct a2dp_sampling_freq a2dp_aac_samplings[] = {
{ 64000, AAC_SAMPLING_FREQ_64000 },
{ 88200, AAC_SAMPLING_FREQ_88200 },
{ 96000, AAC_SAMPLING_FREQ_96000 },
{ 0 },
};

static int a2dp_aac_transport_init(struct ba_transport *t) {

const struct a2dp_codec *codec = t->a2dp.codec;
const struct a2dp_channel_mode *chm;
if ((chm = a2dp_channel_mode_lookup(a2dp_aac_channels,
t->a2dp.configuration.aac.channels)) == NULL)
return -1;

const struct a2dp_sampling *sampling;
if ((sampling = a2dp_sampling_lookup(a2dp_aac_samplings,
AAC_GET_FREQUENCY(t->a2dp.configuration.aac))) == NULL)
return -1;

t->a2dp.pcm.format = BA_TRANSPORT_PCM_FORMAT_S16_2LE;
t->a2dp.pcm.channels = a2dp_codec_lookup_channels(codec,
t->a2dp.configuration.aac.channels, false);
t->a2dp.pcm.sampling = a2dp_codec_lookup_frequency(codec,
AAC_GET_FREQUENCY(t->a2dp.configuration.aac), false);
t->a2dp.pcm.channels = chm->channels;
t->a2dp.pcm.sampling = sampling->frequency;

return 0;
}
Expand Down Expand Up @@ -546,9 +554,7 @@ struct a2dp_codec a2dp_aac_source = {
},
.capabilities_size = sizeof(a2dp_aac_t),
.channels[0] = a2dp_aac_channels,
.channels_size[0] = ARRAYSIZE(a2dp_aac_channels),
.samplings[0] = a2dp_aac_samplings,
.samplings_size[0] = ARRAYSIZE(a2dp_aac_samplings),
.init = a2dp_aac_source_init,
.transport_init = a2dp_aac_transport_init,
.transport_start = a2dp_aac_source_transport_start,
Expand Down Expand Up @@ -607,9 +613,7 @@ struct a2dp_codec a2dp_aac_sink = {
},
.capabilities_size = sizeof(a2dp_aac_t),
.channels[0] = a2dp_aac_channels,
.channels_size[0] = ARRAYSIZE(a2dp_aac_channels),
.samplings[0] = a2dp_aac_samplings,
.samplings_size[0] = ARRAYSIZE(a2dp_aac_samplings),
.init = a2dp_aac_sink_init,
.transport_init = a2dp_aac_transport_init,
.transport_start = a2dp_aac_sink_transport_start,
Expand Down
24 changes: 14 additions & 10 deletions src/a2dp-aptx-hd.c
Original file line number Diff line number Diff line change
Expand Up @@ -271,24 +271,32 @@ void *a2dp_aptx_hd_dec_thread(struct ba_transport_pcm *t_pcm) {

static const struct a2dp_channel_mode a2dp_aptx_hd_channels[] = {
{ A2DP_CHM_STEREO, 2, APTX_CHANNEL_MODE_STEREO },
{ 0 },
};

static const struct a2dp_sampling_freq a2dp_aptx_hd_samplings[] = {
static const struct a2dp_sampling a2dp_aptx_hd_samplings[] = {
{ 16000, APTX_SAMPLING_FREQ_16000 },
{ 32000, APTX_SAMPLING_FREQ_32000 },
{ 44100, APTX_SAMPLING_FREQ_44100 },
{ 48000, APTX_SAMPLING_FREQ_48000 },
{ 0 },
};

static int a2dp_aptx_hd_transport_init(struct ba_transport *t) {

const struct a2dp_codec *codec = t->a2dp.codec;
const struct a2dp_channel_mode *chm;
if ((chm = a2dp_channel_mode_lookup(a2dp_aptx_hd_channels,
t->a2dp.configuration.aptx_hd.aptx.channel_mode)) == NULL)
return -1;

const struct a2dp_sampling *sampling;
if ((sampling = a2dp_sampling_lookup(a2dp_aptx_hd_samplings,
t->a2dp.configuration.aptx_hd.aptx.frequency)) == NULL)
return -1;

t->a2dp.pcm.format = BA_TRANSPORT_PCM_FORMAT_S24_4LE;
t->a2dp.pcm.channels = a2dp_codec_lookup_channels(codec,
t->a2dp.configuration.aptx_hd.aptx.channel_mode, false);
t->a2dp.pcm.sampling = a2dp_codec_lookup_frequency(codec,
t->a2dp.configuration.aptx_hd.aptx.frequency, false);
t->a2dp.pcm.channels = chm->channels;
t->a2dp.pcm.sampling = sampling->frequency;

return 0;
}
Expand Down Expand Up @@ -323,9 +331,7 @@ struct a2dp_codec a2dp_aptx_hd_source = {
},
.capabilities_size = sizeof(a2dp_aptx_hd_t),
.channels[0] = a2dp_aptx_hd_channels,
.channels_size[0] = ARRAYSIZE(a2dp_aptx_hd_channels),
.samplings[0] = a2dp_aptx_hd_samplings,
.samplings_size[0] = ARRAYSIZE(a2dp_aptx_hd_samplings),
.init = a2dp_aptx_hd_source_init,
.transport_init = a2dp_aptx_hd_transport_init,
.transport_start = a2dp_aptx_hd_source_transport_start,
Expand Down Expand Up @@ -355,9 +361,7 @@ struct a2dp_codec a2dp_aptx_hd_sink = {
},
.capabilities_size = sizeof(a2dp_aptx_hd_t),
.channels[0] = a2dp_aptx_hd_channels,
.channels_size[0] = ARRAYSIZE(a2dp_aptx_hd_channels),
.samplings[0] = a2dp_aptx_hd_samplings,
.samplings_size[0] = ARRAYSIZE(a2dp_aptx_hd_samplings),
.transport_init = a2dp_aptx_hd_transport_init,
.transport_start = a2dp_aptx_hd_sink_transport_start,
};
Expand Down
24 changes: 14 additions & 10 deletions src/a2dp-aptx.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,24 +234,32 @@ void *a2dp_aptx_dec_thread(struct ba_transport_pcm *t_pcm) {

static const struct a2dp_channel_mode a2dp_aptx_channels[] = {
{ A2DP_CHM_STEREO, 2, APTX_CHANNEL_MODE_STEREO },
{ 0 },
};

static const struct a2dp_sampling_freq a2dp_aptx_samplings[] = {
static const struct a2dp_sampling a2dp_aptx_samplings[] = {
{ 16000, APTX_SAMPLING_FREQ_16000 },
{ 32000, APTX_SAMPLING_FREQ_32000 },
{ 44100, APTX_SAMPLING_FREQ_44100 },
{ 48000, APTX_SAMPLING_FREQ_48000 },
{ 0 },
};

static int a2dp_aptx_transport_init(struct ba_transport *t) {

const struct a2dp_codec *codec = t->a2dp.codec;
const struct a2dp_channel_mode *chm;
if ((chm = a2dp_channel_mode_lookup(a2dp_aptx_channels,
t->a2dp.configuration.aptx.channel_mode)) == NULL)
return -1;

const struct a2dp_sampling *sampling;
if ((sampling = a2dp_sampling_lookup(a2dp_aptx_samplings,
t->a2dp.configuration.aptx.frequency)) == NULL)
return -1;

t->a2dp.pcm.format = BA_TRANSPORT_PCM_FORMAT_S16_2LE;
t->a2dp.pcm.channels = a2dp_codec_lookup_channels(codec,
t->a2dp.configuration.aptx.channel_mode, false);
t->a2dp.pcm.sampling = a2dp_codec_lookup_frequency(codec,
t->a2dp.configuration.aptx.frequency, false);
t->a2dp.pcm.channels = chm->channels;
t->a2dp.pcm.sampling = sampling->frequency;

return 0;
}
Expand Down Expand Up @@ -286,9 +294,7 @@ struct a2dp_codec a2dp_aptx_source = {
},
.capabilities_size = sizeof(a2dp_aptx_t),
.channels[0] = a2dp_aptx_channels,
.channels_size[0] = ARRAYSIZE(a2dp_aptx_channels),
.samplings[0] = a2dp_aptx_samplings,
.samplings_size[0] = ARRAYSIZE(a2dp_aptx_samplings),
.init = a2dp_aptx_source_init,
.transport_init = a2dp_aptx_transport_init,
.transport_start = a2dp_aptx_source_transport_start,
Expand Down Expand Up @@ -318,9 +324,7 @@ struct a2dp_codec a2dp_aptx_sink = {
},
.capabilities_size = sizeof(a2dp_aptx_t),
.channels[0] = a2dp_aptx_channels,
.channels_size[0] = ARRAYSIZE(a2dp_aptx_channels),
.samplings[0] = a2dp_aptx_samplings,
.samplings_size[0] = ARRAYSIZE(a2dp_aptx_samplings),
.transport_init = a2dp_aptx_transport_init,
.transport_start = a2dp_aptx_sink_transport_start,
};
Expand Down
35 changes: 21 additions & 14 deletions src/a2dp-faststream.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,32 +240,43 @@ void *a2dp_faststream_dec_thread(struct ba_transport_pcm *t_pcm) {
return NULL;
}

static const struct a2dp_sampling_freq a2dp_faststream_samplings_music[] = {
static const struct a2dp_sampling a2dp_faststream_samplings_music[] = {
{ 44100, FASTSTREAM_SAMPLING_FREQ_MUSIC_44100 },
{ 48000, FASTSTREAM_SAMPLING_FREQ_MUSIC_48000 },
{ 0 },
};

static const struct a2dp_sampling_freq a2dp_faststream_samplings_voice[] = {
static const struct a2dp_sampling a2dp_faststream_samplings_voice[] = {
{ 16000, FASTSTREAM_SAMPLING_FREQ_VOICE_16000 },
{ 0 },
};

static int a2dp_faststream_transport_init(struct ba_transport *t) {

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

t->a2dp.pcm.format = BA_TRANSPORT_PCM_FORMAT_S16_2LE;
t->a2dp.pcm_bc.format = BA_TRANSPORT_PCM_FORMAT_S16_2LE;
const struct a2dp_sampling *sampling;
if ((sampling = a2dp_sampling_lookup(a2dp_faststream_samplings_music,
t->a2dp.configuration.faststream.frequency_music)) == NULL)
return -1;

if (t->a2dp.configuration.faststream.direction & FASTSTREAM_DIRECTION_MUSIC) {
t->a2dp.pcm.format = BA_TRANSPORT_PCM_FORMAT_S16_2LE;
t->a2dp.pcm.sampling = sampling->frequency;
t->a2dp.pcm.channels = 2;
t->a2dp.pcm.sampling = a2dp_codec_lookup_frequency(codec,
t->a2dp.configuration.faststream.frequency_music, false);

}

if (t->a2dp.configuration.faststream.direction & FASTSTREAM_DIRECTION_VOICE) {

const struct a2dp_sampling *sampling;
if ((sampling = a2dp_sampling_lookup(a2dp_faststream_samplings_voice,
t->a2dp.configuration.faststream.frequency_voice)) == NULL)
return -1;

t->a2dp.pcm_bc.format = BA_TRANSPORT_PCM_FORMAT_S16_2LE;
t->a2dp.pcm_bc.sampling = sampling->frequency;
t->a2dp.pcm_bc.channels = 1;
t->a2dp.pcm_bc.sampling = a2dp_codec_lookup_frequency(codec,
t->a2dp.configuration.faststream.frequency_voice, true);

}

return 0;
Expand Down Expand Up @@ -308,9 +319,7 @@ struct a2dp_codec a2dp_faststream_source = {
},
.capabilities_size = sizeof(a2dp_faststream_t),
.samplings[0] = a2dp_faststream_samplings_music,
.samplings_size[0] = ARRAYSIZE(a2dp_faststream_samplings_music),
.samplings[1] = a2dp_faststream_samplings_voice,
.samplings_size[1] = ARRAYSIZE(a2dp_faststream_samplings_voice),
.init = a2dp_faststream_source_init,
.transport_init = a2dp_faststream_transport_init,
.transport_start = a2dp_faststream_source_transport_start,
Expand Down Expand Up @@ -345,9 +354,7 @@ struct a2dp_codec a2dp_faststream_sink = {
},
.capabilities_size = sizeof(a2dp_faststream_t),
.samplings[0] = a2dp_faststream_samplings_music,
.samplings_size[0] = ARRAYSIZE(a2dp_faststream_samplings_music),
.samplings[1] = a2dp_faststream_samplings_voice,
.samplings_size[1] = ARRAYSIZE(a2dp_faststream_samplings_voice),
.transport_init = a2dp_faststream_transport_init,
.transport_start = a2dp_faststream_sink_transport_start,
};
24 changes: 14 additions & 10 deletions src/a2dp-lc3plus.c
Original file line number Diff line number Diff line change
Expand Up @@ -538,22 +538,30 @@ void *a2dp_lc3plus_dec_thread(struct ba_transport_pcm *t_pcm) {
static const struct a2dp_channel_mode a2dp_lc3plus_channels[] = {
{ A2DP_CHM_MONO, 1, LC3PLUS_CHANNELS_1 },
{ A2DP_CHM_STEREO, 2, LC3PLUS_CHANNELS_2 },
{ 0 },
};

static const struct a2dp_sampling_freq a2dp_lc3plus_samplings[] = {
static const struct a2dp_sampling a2dp_lc3plus_samplings[] = {
{ 48000, LC3PLUS_SAMPLING_FREQ_48000 },
{ 96000, LC3PLUS_SAMPLING_FREQ_96000 },
{ 0 },
};

static int a2dp_lc3plus_transport_init(struct ba_transport *t) {

const struct a2dp_codec *codec = t->a2dp.codec;
const struct a2dp_channel_mode *chm;
if ((chm = a2dp_channel_mode_lookup(a2dp_lc3plus_channels,
t->a2dp.configuration.lc3plus.channels)) == NULL)
return -1;

const struct a2dp_sampling *sampling;
if ((sampling = a2dp_sampling_lookup(a2dp_lc3plus_samplings,
LC3PLUS_GET_FREQUENCY(t->a2dp.configuration.lc3plus))) == NULL)
return -1;

t->a2dp.pcm.format = BA_TRANSPORT_PCM_FORMAT_S24_4LE;
t->a2dp.pcm.channels = a2dp_codec_lookup_channels(codec,
t->a2dp.configuration.lc3plus.channels, false);
t->a2dp.pcm.sampling = a2dp_codec_lookup_frequency(codec,
LC3PLUS_GET_FREQUENCY(t->a2dp.configuration.lc3plus), false);
t->a2dp.pcm.channels = chm->channels;
t->a2dp.pcm.sampling = sampling->frequency;

return 0;
}
Expand Down Expand Up @@ -589,9 +597,7 @@ struct a2dp_codec a2dp_lc3plus_source = {
},
.capabilities_size = sizeof(a2dp_lc3plus_t),
.channels[0] = a2dp_lc3plus_channels,
.channels_size[0] = ARRAYSIZE(a2dp_lc3plus_channels),
.samplings[0] = a2dp_lc3plus_samplings,
.samplings_size[0] = ARRAYSIZE(a2dp_lc3plus_samplings),
.init = a2dp_lc3plus_source_init,
.transport_init = a2dp_lc3plus_transport_init,
.transport_start = a2dp_lc3plus_source_transport_start,
Expand Down Expand Up @@ -620,9 +626,7 @@ struct a2dp_codec a2dp_lc3plus_sink = {
},
.capabilities_size = sizeof(a2dp_lc3plus_t),
.channels[0] = a2dp_lc3plus_channels,
.channels_size[0] = ARRAYSIZE(a2dp_lc3plus_channels),
.samplings[0] = a2dp_lc3plus_samplings,
.samplings_size[0] = ARRAYSIZE(a2dp_lc3plus_samplings),
.transport_init = a2dp_lc3plus_transport_init,
.transport_start = a2dp_lc3plus_sink_transport_start,
};
25 changes: 14 additions & 11 deletions src/a2dp-ldac.c
Original file line number Diff line number Diff line change
Expand Up @@ -326,27 +326,34 @@ static const struct a2dp_channel_mode a2dp_ldac_channels[] = {
{ A2DP_CHM_MONO, 1, LDAC_CHANNEL_MODE_MONO },
{ A2DP_CHM_DUAL_CHANNEL, 2, LDAC_CHANNEL_MODE_DUAL },
{ A2DP_CHM_STEREO, 2, LDAC_CHANNEL_MODE_STEREO },
{ 0 },
};

static const struct a2dp_sampling_freq a2dp_ldac_samplings[] = {
static const struct a2dp_sampling a2dp_ldac_samplings[] = {
{ 44100, LDAC_SAMPLING_FREQ_44100 },
{ 48000, LDAC_SAMPLING_FREQ_48000 },
{ 88200, LDAC_SAMPLING_FREQ_88200 },
{ 96000, LDAC_SAMPLING_FREQ_96000 },
{ 0 },
};

static int a2dp_ldac_transport_init(struct ba_transport *t) {

const struct a2dp_codec *codec = t->a2dp.codec;
const struct a2dp_channel_mode *chm;
if ((chm = a2dp_channel_mode_lookup(a2dp_ldac_channels,
t->a2dp.configuration.ldac.channel_mode)) == NULL)
return -1;

const struct a2dp_sampling *sampling;
if ((sampling = a2dp_sampling_lookup(a2dp_ldac_samplings,
t->a2dp.configuration.ldac.frequency)) == NULL)
return -1;

/* LDAC library internally for encoding uses 31-bit integers or
* floats, so the best choice for PCM sample is signed 32-bit. */
t->a2dp.pcm.format = BA_TRANSPORT_PCM_FORMAT_S32_4LE;

t->a2dp.pcm.channels = a2dp_codec_lookup_channels(codec,
t->a2dp.configuration.ldac.channel_mode, false);
t->a2dp.pcm.sampling = a2dp_codec_lookup_frequency(codec,
t->a2dp.configuration.ldac.frequency, false);
t->a2dp.pcm.channels = chm->channels;
t->a2dp.pcm.sampling = sampling->frequency;

return 0;
}
Expand Down Expand Up @@ -383,9 +390,7 @@ struct a2dp_codec a2dp_ldac_source = {
},
.capabilities_size = sizeof(a2dp_ldac_t),
.channels[0] = a2dp_ldac_channels,
.channels_size[0] = ARRAYSIZE(a2dp_ldac_channels),
.samplings[0] = a2dp_ldac_samplings,
.samplings_size[0] = ARRAYSIZE(a2dp_ldac_samplings),
.init = a2dp_ldac_source_init,
.transport_init = a2dp_ldac_transport_init,
.transport_start = a2dp_ldac_source_transport_start,
Expand Down Expand Up @@ -417,9 +422,7 @@ struct a2dp_codec a2dp_ldac_sink = {
},
.capabilities_size = sizeof(a2dp_ldac_t),
.channels[0] = a2dp_ldac_channels,
.channels_size[0] = ARRAYSIZE(a2dp_ldac_channels),
.samplings[0] = a2dp_ldac_samplings,
.samplings_size[0] = ARRAYSIZE(a2dp_ldac_samplings),
.transport_init = a2dp_ldac_transport_init,
.transport_start = a2dp_ldac_sink_transport_start,
};
Expand Down
Loading

0 comments on commit bc5d8d3

Please sign in to comment.