diff --git a/meson_options.txt b/meson_options.txt index 7c8cc15ba..6a6e70889 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -133,6 +133,10 @@ option('bluez5-codec-ldac-dec', description: 'Enable LDAC Sony open source codec decoding', type: 'feature', value: 'auto') +option('bluez5-codec-lhdc', + description: 'Enable LHDC v5 open source codec implementation', + type: 'feature', + value: 'auto') option('bluez5-codec-aac', description: 'Enable Fraunhofer FDK AAC open source codec implementation', type: 'feature', diff --git a/spa/include/spa/param/bluetooth/audio.h b/spa/include/spa/param/bluetooth/audio.h index 34b45e297..c0c5a841a 100644 --- a/spa/include/spa/param/bluetooth/audio.h +++ b/spa/include/spa/param/bluetooth/audio.h @@ -25,6 +25,7 @@ enum spa_bluetooth_audio_codec { SPA_BLUETOOTH_AUDIO_CODEC_APTX, SPA_BLUETOOTH_AUDIO_CODEC_APTX_HD, SPA_BLUETOOTH_AUDIO_CODEC_LDAC, + SPA_BLUETOOTH_AUDIO_CODEC_LHDC_V5, SPA_BLUETOOTH_AUDIO_CODEC_APTX_LL, SPA_BLUETOOTH_AUDIO_CODEC_APTX_LL_DUPLEX, SPA_BLUETOOTH_AUDIO_CODEC_FASTSTREAM, diff --git a/spa/include/spa/param/bluetooth/type-info.h b/spa/include/spa/param/bluetooth/type-info.h index 7bf1da52c..d14367226 100644 --- a/spa/include/spa/param/bluetooth/type-info.h +++ b/spa/include/spa/param/bluetooth/type-info.h @@ -29,6 +29,7 @@ static const struct spa_type_info spa_type_bluetooth_audio_codec[] = { { SPA_BLUETOOTH_AUDIO_CODEC_APTX, SPA_TYPE_Int, SPA_TYPE_INFO_BLUETOOTH_AUDIO_CODEC_BASE "aptx", NULL }, { SPA_BLUETOOTH_AUDIO_CODEC_APTX_HD, SPA_TYPE_Int, SPA_TYPE_INFO_BLUETOOTH_AUDIO_CODEC_BASE "aptx_hd", NULL }, { SPA_BLUETOOTH_AUDIO_CODEC_LDAC, SPA_TYPE_Int, SPA_TYPE_INFO_BLUETOOTH_AUDIO_CODEC_BASE "ldac", NULL }, + { SPA_BLUETOOTH_AUDIO_CODEC_LHDC_V5, SPA_TYPE_Int, SPA_TYPE_INFO_BLUETOOTH_AUDIO_CODEC_BASE "lhdc_v5", NULL }, { SPA_BLUETOOTH_AUDIO_CODEC_APTX_LL, SPA_TYPE_Int, SPA_TYPE_INFO_BLUETOOTH_AUDIO_CODEC_BASE "aptx_ll", NULL }, { SPA_BLUETOOTH_AUDIO_CODEC_APTX_LL_DUPLEX, SPA_TYPE_Int, SPA_TYPE_INFO_BLUETOOTH_AUDIO_CODEC_BASE "aptx_ll_duplex", NULL }, { SPA_BLUETOOTH_AUDIO_CODEC_FASTSTREAM, SPA_TYPE_Int, SPA_TYPE_INFO_BLUETOOTH_AUDIO_CODEC_BASE "faststream", NULL }, diff --git a/spa/meson.build b/spa/meson.build index f8acaec6a..e81e017ce 100644 --- a/spa/meson.build +++ b/spa/meson.build @@ -94,6 +94,9 @@ if get_option('spa-plugins').allowed() endif summary({'LDAC DEC': ldac_dec_dep.found()}, bool_yn: true, section: 'Bluetooth audio codecs') + lhdc_dep = dependency('lhdcv5', required : get_option('bluez5-codec-lhdc')) + summary({'LHDC v5': lhdc_dep.found()}, bool_yn: true, section: 'Bluetooth audio codecs') + aptx_dep = dependency('libfreeaptx', required : get_option('bluez5-codec-aptx')) summary({'aptX': aptx_dep.found()}, bool_yn: true, section: 'Bluetooth audio codecs') fdk_aac_dep = dependency('fdk-aac', required : get_option('bluez5-codec-aac')) diff --git a/spa/plugins/bluez5/a2dp-codec-caps.h b/spa/plugins/bluez5/a2dp-codec-caps.h index b58e33624..e8077dabc 100644 --- a/spa/plugins/bluez5/a2dp-codec-caps.h +++ b/spa/plugins/bluez5/a2dp-codec-caps.h @@ -194,6 +194,17 @@ #define LDAC_SAMPLING_FREQ_176400 0x02 #define LDAC_SAMPLING_FREQ_192000 0x01 +#define LHDC_V5_VENDOR_ID 0x0000053a +#define LHDC_V5_CODEC_ID 0x4c35 + +#define LHDC_V5_SAMPLING_FREQ_48000 0x10 +#define LHDC_V5_BIT_FMT_16 0x04 +#define LHDC_V5_MIN_BITRATE_64K 0x00 +#define LHDC_V5_MAX_BITRATE_400K 0x10 +#define LHDC_V5_VERSION_1 0x01 +#define LHDC_V5_FRAME_LEN_5MS 0x10 +#define LHDC_V5_FEATURE_LL 0x40 + #define FASTSTREAM_VENDOR_ID 0x0000000a #define FASTSTREAM_CODEC_ID 0x0001 @@ -318,6 +329,15 @@ typedef struct { uint8_t channel_mode; } __attribute__ ((packed)) a2dp_ldac_t; +typedef struct { + a2dp_vendor_codec_t info; + uint8_t sampling_freq; + uint8_t bitrate_and_depth; + uint8_t frame_len_and_version; + uint8_t features; + uint8_t reserved; +} __attribute__ ((packed)) a2dp_lhdc_v5_t; + #if __BYTE_ORDER == __LITTLE_ENDIAN typedef struct { diff --git a/spa/plugins/bluez5/a2dp-codec-lhdc.c b/spa/plugins/bluez5/a2dp-codec-lhdc.c new file mode 100644 index 000000000..a6fe3d447 --- /dev/null +++ b/spa/plugins/bluez5/a2dp-codec-lhdc.c @@ -0,0 +1,322 @@ +/* Spa A2DP LHDC v5 codec */ +/* SPDX-License-Identifier: MIT */ + +#include +#include +#include +#include +#include + +#include +#include + +#include + +#include "rtp.h" +#include "media-codecs.h" + +#define LHDCV5_SAMPLE_RATE 48000 +#define LHDCV5_CHANNELS 2 +#define LHDCV5_BITS_PER_SAMPLE LHDCBT_SMPL_FMT_S16 +#define LHDCV5_INTERVAL_MS LHDC_ENC_INTERVAL_10MS +#define LHDCV5_MAX_PACKET_BYTES 504u + +static struct spa_log *log_; + +struct lhdc_media_payload { +#if __BYTE_ORDER == __BIG_ENDIAN + uint8_t frame_count:6; + uint8_t latency:2; +#else + uint8_t latency:2; + uint8_t frame_count:6; +#endif + uint8_t seq_number; +} __attribute__ ((packed)); + +struct impl { + HANDLE_LHDC_BT lhdc; + + struct rtp_header *rtp; + struct lhdc_media_payload *payload; + uint8_t media_seqnum; + + uint32_t block_samples; + uint32_t block_bytes; +}; + +static int codec_fill_caps(const struct media_codec *codec, uint32_t flags, + const struct spa_dict *settings, uint8_t caps[A2DP_MAX_CAPS_SIZE]) +{ + const a2dp_lhdc_v5_t conf = { + .info = codec->vendor, + .sampling_freq = LHDC_V5_SAMPLING_FREQ_48000, + .bitrate_and_depth = LHDC_V5_MIN_BITRATE_64K | + LHDC_V5_MAX_BITRATE_400K | LHDC_V5_BIT_FMT_16, + .frame_len_and_version = LHDC_V5_FRAME_LEN_5MS | LHDC_V5_VERSION_1, + .features = LHDC_V5_FEATURE_LL, + .reserved = 0, + }; + memcpy(caps, &conf, sizeof(conf)); + return sizeof(conf); +} + +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], + void **config_data) +{ + a2dp_lhdc_v5_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 ((conf.sampling_freq & LHDC_V5_SAMPLING_FREQ_48000) == 0) + return -ENOTSUP; + if ((conf.bitrate_and_depth & LHDC_V5_BIT_FMT_16) == 0) + return -ENOTSUP; + if ((conf.frame_len_and_version & LHDC_V5_FRAME_LEN_5MS) == 0 || + (conf.frame_len_and_version & LHDC_V5_VERSION_1) == 0) + return -ENOTSUP; + + conf.sampling_freq = LHDC_V5_SAMPLING_FREQ_48000; + conf.bitrate_and_depth = LHDC_V5_MIN_BITRATE_64K | + LHDC_V5_MAX_BITRATE_400K | LHDC_V5_BIT_FMT_16; + conf.frame_len_and_version = LHDC_V5_FRAME_LEN_5MS | LHDC_V5_VERSION_1; + conf.features &= LHDC_V5_FEATURE_LL; + conf.reserved = 0; + + memcpy(config, &conf, sizeof(conf)); + return sizeof(conf); +} + +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) +{ + struct spa_pod_frame f[1]; + uint32_t position[2] = { SPA_AUDIO_CHANNEL_FL, SPA_AUDIO_CHANNEL_FR }; + + if (idx > 0) + return 0; + if (caps_size < sizeof(a2dp_lhdc_v5_t)) + return -EINVAL; + + 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_S16), + SPA_FORMAT_AUDIO_rate, SPA_POD_Int(LHDCV5_SAMPLE_RATE), + SPA_FORMAT_AUDIO_channels, SPA_POD_Int(LHDCV5_CHANNELS), + SPA_FORMAT_AUDIO_position, SPA_POD_Array(sizeof(uint32_t), + SPA_TYPE_Id, 2, 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) +{ + uint8_t config[A2DP_MAX_CAPS_SIZE]; + struct media_codec_audio_info audio_info = { + .rate = info->info.raw.rate, + .channels = info->info.raw.channels, + }; + + if (codec_select_config(codec, flags, caps, caps_size, + &audio_info, NULL, config, NULL) < 0) + return -EINVAL; + + info->media_type = SPA_MEDIA_TYPE_audio; + info->media_subtype = SPA_MEDIA_SUBTYPE_raw; + info->info.raw.format = SPA_AUDIO_FORMAT_S16; + info->info.raw.rate = LHDCV5_SAMPLE_RATE; + info->info.raw.channels = LHDCV5_CHANNELS; + info->info.raw.position[0] = SPA_AUDIO_CHANNEL_FL; + info->info.raw.position[1] = SPA_AUDIO_CHANNEL_FR; + return 0; +} + +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) +{ + struct impl *this; + int32_t res; + + if (info->info.raw.format != SPA_AUDIO_FORMAT_S16 || + info->info.raw.rate != LHDCV5_SAMPLE_RATE || + info->info.raw.channels != LHDCV5_CHANNELS) { + errno = EINVAL; + return NULL; + } + + this = calloc(1, sizeof(struct impl)); + if (this == NULL) + return NULL; + + res = lhdcv5BT_get_handle(LHDC_VERSION_1, &this->lhdc); + if (res != LHDC_FRET_SUCCESS || this->lhdc == NULL) + goto error; + + res = lhdcv5BT_init_encoder(this->lhdc, LHDCV5_SAMPLE_RATE, + LHDCV5_BITS_PER_SAMPLE, LHDC_QUALITY_AUTO, mtu, + LHDCV5_INTERVAL_MS, 0); + if (res != LHDC_FRET_SUCCESS) + goto error; + + lhdcv5BT_set_min_bitrate(this->lhdc, LHDC_QUALITY_LOW0); + lhdcv5BT_set_max_bitrate(this->lhdc, LHDC_QUALITY_HIGH1); + + res = lhdcv5BT_get_block_Size(this->lhdc, &this->block_samples); + if (res != LHDC_FRET_SUCCESS || this->block_samples == 0) + goto error; + + this->block_bytes = this->block_samples * LHDCV5_CHANNELS * sizeof(int16_t); + return this; + +error: + if (this->lhdc) + lhdcv5BT_free_handle(this->lhdc); + free(this); + errno = EIO; + return NULL; +} + +static void codec_deinit(void *data) +{ + struct impl *this = data; + if (this->lhdc) + lhdcv5BT_free_handle(this->lhdc); + free(this); +} + +static int codec_get_block_size(void *data) +{ + struct impl *this = data; + return this->block_bytes; +} + +static int codec_abr_process(void *data, size_t unsent) +{ + struct impl *this = data; + uint32_t queue_len = (unsent + LHDCV5_MAX_PACKET_BYTES - 1) / LHDCV5_MAX_PACKET_BYTES; + int32_t res = lhdcv5BT_adjust_bitrate(this->lhdc, queue_len); + + return res == LHDC_FRET_SUCCESS ? 0 : -EIO; +} + +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 lhdc_media_payload); + + if (dst_size <= header_size) + return -EINVAL; + + this->rtp = dst; + this->payload = SPA_PTROFF(dst, sizeof(struct rtp_header), struct lhdc_media_payload); + memset(dst, 0, header_size); + + this->rtp->v = 2; + this->rtp->pt = 96; + this->rtp->sequence_number = htons(seqnum); + this->rtp->timestamp = htonl(timestamp); + this->rtp->ssrc = htonl(1); + + this->payload->latency = 0; + this->payload->frame_count = 0; + this->payload->seq_number = this->media_seqnum; + + return header_size; +} + +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; + uint32_t written = 0; + uint32_t frames = 0; + int32_t res; + + *dst_out = 0; + *need_flush = NEED_FLUSH_NO; + + if (src == NULL) + return 0; + if (src_size < this->block_bytes) + return 0; + if (dst_size > UINT32_MAX) + dst_size = UINT32_MAX; + + res = lhdcv5BT_encode(this->lhdc, (void *)src, this->block_bytes, + dst, (uint32_t)dst_size, &written, &frames); + if (res != LHDC_FRET_SUCCESS) + return -EIO; + + if (written == 0) + return this->block_bytes; + + this->payload->latency = 0; + this->payload->frame_count = frames; + this->payload->seq_number = this->media_seqnum++; + + *dst_out = written; + *need_flush = NEED_FLUSH_ALL; + return this->block_bytes; +} + +static void codec_get_delay(void *data, uint32_t *encoder, uint32_t *decoder) +{ + struct impl *this = data; + if (encoder) + *encoder = this->block_samples * 2; + if (decoder) + *decoder = 0; +} + +static void codec_set_log(struct spa_log *global_log) +{ + log_ = global_log; + spa_log_topic_init(log_, &codec_plugin_log_topic); +} + +const struct media_codec a2dp_codec_lhdc_v5 = { + .id = SPA_BLUETOOTH_AUDIO_CODEC_LHDC_V5, + .kind = MEDIA_CODEC_A2DP, + .codec_id = A2DP_CODEC_VENDOR, + .vendor = { .vendor_id = LHDC_V5_VENDOR_ID, + .codec_id = LHDC_V5_CODEC_ID }, + .name = "lhdc_v5", + .description = "LHDC v5", + .send_buf_size = 16 * 1024, + .fill_caps = codec_fill_caps, + .select_config = codec_select_config, + .enum_config = codec_enum_config, + .validate_config = codec_validate_config, + .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, + .get_delay = codec_get_delay, + .set_log = codec_set_log, +}; + +MEDIA_CODEC_EXPORT_DEF( + "lhdc", + &a2dp_codec_lhdc_v5 +); diff --git a/spa/plugins/bluez5/codec-loader.c b/spa/plugins/bluez5/codec-loader.c index 68e6af756..153306615 100644 --- a/spa/plugins/bluez5/codec-loader.c +++ b/spa/plugins/bluez5/codec-loader.c @@ -32,6 +32,7 @@ static int codec_order(const struct media_codec *c) { static const enum spa_bluetooth_audio_codec order[] = { SPA_BLUETOOTH_AUDIO_CODEC_LC3, + SPA_BLUETOOTH_AUDIO_CODEC_LHDC_V5, SPA_BLUETOOTH_AUDIO_CODEC_LDAC, SPA_BLUETOOTH_AUDIO_CODEC_APTX_HD, SPA_BLUETOOTH_AUDIO_CODEC_APTX, @@ -183,6 +184,7 @@ const struct media_codec * const *load_media_codecs(struct spa_plugin_loader *lo MEDIA_CODEC_FACTORY_LIB("aac"), MEDIA_CODEC_FACTORY_LIB("aptx"), MEDIA_CODEC_FACTORY_LIB("faststream"), + MEDIA_CODEC_FACTORY_LIB("lhdc"), MEDIA_CODEC_FACTORY_LIB("ldac"), MEDIA_CODEC_FACTORY_LIB("sbc"), MEDIA_CODEC_FACTORY_LIB("lc3plus"), diff --git a/spa/plugins/bluez5/meson.build b/spa/plugins/bluez5/meson.build index 01c5f3ac1..9c17bcbff 100644 --- a/spa/plugins/bluez5/meson.build +++ b/spa/plugins/bluez5/meson.build @@ -152,6 +152,16 @@ if ldac_dep.found() install_dir : spa_plugindir / 'bluez5') endif +if lhdc_dep.found() + bluez_codec_lhdc = shared_library('spa-codec-bluez5-lhdc', + [ 'a2dp-codec-lhdc.c', 'media-codecs.c' ], + include_directories : [ configinc ], + c_args : codec_args, + dependencies : [ spa_dep, lhdc_dep ], + install : true, + install_dir : spa_plugindir / 'bluez5') +endif + if get_option('bluez5-codec-lc3plus').allowed() and lc3plus_dep.found() bluez_codec_lc3plus = shared_library('spa-codec-bluez5-lc3plus', [ 'a2dp-codec-lc3plus.c', 'media-codecs.c' ],