From 4ced2a0ea46e3b6aa26f4efd4cc8532cc41882c1 Mon Sep 17 00:00:00 2001 From: DBeidachazi <269502169@qq.com> Date: Mon, 29 Jun 2026 17:25:21 +0800 Subject: [PATCH 1/3] bluez5: add LHDC v5 codec using liblhdcv5 --- meson_options.txt | 4 + spa/include/spa/param/bluetooth/audio.h | 1 + spa/include/spa/param/bluetooth/type-info.h | 1 + spa/meson.build | 3 + spa/plugins/bluez5/a2dp-codec-caps.h | 20 ++ spa/plugins/bluez5/a2dp-codec-lhdc.c | 322 ++++++++++++++++++++ spa/plugins/bluez5/codec-loader.c | 2 + spa/plugins/bluez5/meson.build | 10 + 8 files changed, 363 insertions(+) create mode 100644 spa/plugins/bluez5/a2dp-codec-lhdc.c 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' ], From 276167a82baa716c66f7cec326d4020e4b31758f Mon Sep 17 00:00:00 2001 From: DBeidachazi <269502169@qq.com> Date: Thu, 2 Jul 2026 14:01:24 +0800 Subject: [PATCH 2/3] bluez5: improve LHDC v5 codec handling Tighten LHDC v5 config validation and keep the codec encoder-only: do not add unsupported decode, delay query, or PipeWire-side ABR behavior. Advertise the LHDC v5 sample rates and S16/S24 formats supported by liblhdcv5, parse the P7 bitrate range from peer capabilities, and initialize liblhdcv5 with the selected min/max bitrate indices. Keep fixed quality choices limited to the currently negotiable local max. --- spa/plugins/bluez5/a2dp-codec-caps.h | 16 + spa/plugins/bluez5/a2dp-codec-lhdc.c | 624 +++++++++++++++++++++++---- 2 files changed, 558 insertions(+), 82 deletions(-) diff --git a/spa/plugins/bluez5/a2dp-codec-caps.h b/spa/plugins/bluez5/a2dp-codec-caps.h index e8077dabc..2f7b13bf8 100644 --- a/spa/plugins/bluez5/a2dp-codec-caps.h +++ b/spa/plugins/bluez5/a2dp-codec-caps.h @@ -197,10 +197,26 @@ #define LHDC_V5_VENDOR_ID 0x0000053a #define LHDC_V5_CODEC_ID 0x4c35 +#define LHDC_V5_SAMPLING_FREQ_44100 0x20 #define LHDC_V5_SAMPLING_FREQ_48000 0x10 +#define LHDC_V5_SAMPLING_FREQ_96000 0x04 +#define LHDC_V5_SAMPLING_FREQ_192000 0x01 +#define LHDC_V5_BIT_FMT_24 0x02 #define LHDC_V5_BIT_FMT_16 0x04 + +/* LHDC v5 P7 bitrate_and_depth byte: bit[7:6] min bitrate, + * bit[5:4] max bitrate, bit[2:0] bit depth. + */ +#define LHDC_V5_MIN_BITRATE_MASK 0xc0 #define LHDC_V5_MIN_BITRATE_64K 0x00 +#define LHDC_V5_MIN_BITRATE_160K 0x40 +#define LHDC_V5_MIN_BITRATE_256K 0x80 +#define LHDC_V5_MIN_BITRATE_400K 0xc0 +#define LHDC_V5_MAX_BITRATE_MASK 0x30 +#define LHDC_V5_MAX_BITRATE_1000K 0x00 #define LHDC_V5_MAX_BITRATE_400K 0x10 +#define LHDC_V5_MAX_BITRATE_500K 0x20 +#define LHDC_V5_MAX_BITRATE_900K 0x30 #define LHDC_V5_VERSION_1 0x01 #define LHDC_V5_FRAME_LEN_5MS 0x10 #define LHDC_V5_FEATURE_LL 0x40 diff --git a/spa/plugins/bluez5/a2dp-codec-lhdc.c b/spa/plugins/bluez5/a2dp-codec-lhdc.c index a6fe3d447..8e8ea525b 100644 --- a/spa/plugins/bluez5/a2dp-codec-lhdc.c +++ b/spa/plugins/bluez5/a2dp-codec-lhdc.c @@ -7,7 +7,10 @@ #include #include +#include #include +#include +#include #include #include @@ -15,14 +18,59 @@ #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 +#define LHDCV5_FRAME_CONFIG (LHDC_V5_FRAME_LEN_5MS | LHDC_V5_VERSION_1) +#define LHDCV5_LOCAL_MIN_BITRATE LHDC_V5_MIN_BITRATE_64K +#define LHDCV5_LOCAL_MAX_BITRATE LHDC_V5_MAX_BITRATE_400K +#define LHDCV5_BITRATE_CONFIG (LHDCV5_LOCAL_MIN_BITRATE | LHDCV5_LOCAL_MAX_BITRATE) +#define LHDCV5_FORMAT_CONFIG (LHDC_V5_BIT_FMT_16 | LHDC_V5_BIT_FMT_24) +#define LHDCV5_MAX_FRAME_COUNT 0x3f static struct spa_log *log_; +static const struct media_codec_config lhdc_frequencies[] = { + { LHDC_V5_SAMPLING_FREQ_44100, 44100, 3 }, + { LHDC_V5_SAMPLING_FREQ_48000, 48000, 2 }, + { LHDC_V5_SAMPLING_FREQ_96000, 96000, 1 }, + { LHDC_V5_SAMPLING_FREQ_192000, 192000, 0 }, +}; + +struct lhdc_format_config { + uint32_t config; + enum spa_audio_format format; + uint32_t bits_per_sample; + uint32_t sample_size; +}; + +static const struct lhdc_format_config lhdc_formats[] = { + { LHDC_V5_BIT_FMT_24, SPA_AUDIO_FORMAT_S24, LHDCBT_SMPL_FMT_S24, 3 }, + { LHDC_V5_BIT_FMT_16, SPA_AUDIO_FORMAT_S16, LHDCBT_SMPL_FMT_S16, sizeof(int16_t) }, +}; + +struct lhdc_bitrate_config { + uint32_t config; + uint32_t bitrate; +}; + +static const struct lhdc_bitrate_config lhdc_min_bitrates[] = { + { LHDC_V5_MIN_BITRATE_64K, 64 }, + { LHDC_V5_MIN_BITRATE_160K, 160 }, + { LHDC_V5_MIN_BITRATE_256K, 256 }, + { LHDC_V5_MIN_BITRATE_400K, 400 }, +}; + +static const struct lhdc_bitrate_config lhdc_max_bitrates[] = { + { LHDC_V5_MAX_BITRATE_400K, 400 }, + { LHDC_V5_MAX_BITRATE_500K, 500 }, + { LHDC_V5_MAX_BITRATE_900K, 900 }, + { LHDC_V5_MAX_BITRATE_1000K, 1000 }, +}; + +struct props { + uint32_t quality; +}; + struct lhdc_media_payload { #if __BYTE_ORDER == __BIG_ENDIAN uint8_t frame_count:6; @@ -41,19 +89,176 @@ struct impl { struct lhdc_media_payload *payload; uint8_t media_seqnum; + uint32_t quality; + uint32_t rate; + uint32_t bits_per_sample; + uint32_t sample_size; + uint32_t mtu; + uint32_t min_bitrate_index; + uint32_t max_bitrate_index; + uint32_t block_samples; uint32_t block_bytes; }; +static const struct lhdc_format_config *select_format_config(uint32_t config) +{ + size_t i; + + for (i = 0; i < SPA_N_ELEMENTS(lhdc_formats); i++) + if (config & lhdc_formats[i].config) + return &lhdc_formats[i]; + return NULL; +} + +static const struct lhdc_format_config *get_format_config(uint32_t config) +{ + uint32_t format = config & LHDCV5_FORMAT_CONFIG; + size_t i; + + for (i = 0; i < SPA_N_ELEMENTS(lhdc_formats); i++) + if (format == lhdc_formats[i].config) + return &lhdc_formats[i]; + return NULL; +} + +static const struct lhdc_bitrate_config *get_bitrate_config( + const struct lhdc_bitrate_config configs[], size_t n, uint32_t config) +{ + size_t i; + + for (i = 0; i < n; i++) + if (config == configs[i].config) + return &configs[i]; + return NULL; +} + +static const struct lhdc_bitrate_config *get_min_bitrate_config(uint32_t config) +{ + return get_bitrate_config(lhdc_min_bitrates, SPA_N_ELEMENTS(lhdc_min_bitrates), + config & LHDC_V5_MIN_BITRATE_MASK); +} + +static const struct lhdc_bitrate_config *get_max_bitrate_config(uint32_t config) +{ + return get_bitrate_config(lhdc_max_bitrates, SPA_N_ELEMENTS(lhdc_max_bitrates), + config & LHDC_V5_MAX_BITRATE_MASK); +} + +static const struct lhdc_bitrate_config *find_min_bitrate_config(uint32_t bitrate) +{ + size_t i; + + for (i = 0; i < SPA_N_ELEMENTS(lhdc_min_bitrates); i++) + if (lhdc_min_bitrates[i].bitrate >= bitrate) + return &lhdc_min_bitrates[i]; + return NULL; +} + +static const struct lhdc_bitrate_config *find_max_bitrate_config(uint32_t bitrate) +{ + const struct lhdc_bitrate_config *res = NULL; + size_t i; + + for (i = 0; i < SPA_N_ELEMENTS(lhdc_max_bitrates); i++) + if (lhdc_max_bitrates[i].bitrate <= bitrate) + res = &lhdc_max_bitrates[i]; + return res; +} + +static int select_bitrate_config(uint32_t peer_config, uint8_t *bitrate_config) +{ + const struct lhdc_bitrate_config *peer_min, *peer_max, *local_min, *local_max; + const struct lhdc_bitrate_config *min, *max; + uint32_t min_bitrate, max_bitrate; + + peer_min = get_min_bitrate_config(peer_config); + peer_max = get_max_bitrate_config(peer_config); + local_min = get_min_bitrate_config(LHDCV5_LOCAL_MIN_BITRATE); + local_max = get_max_bitrate_config(LHDCV5_LOCAL_MAX_BITRATE); + if (peer_min == NULL || peer_max == NULL || local_min == NULL || local_max == NULL) + return -EINVAL; + + min_bitrate = peer_min->bitrate > local_min->bitrate ? peer_min->bitrate : local_min->bitrate; + max_bitrate = peer_max->bitrate < local_max->bitrate ? peer_max->bitrate : local_max->bitrate; + if (min_bitrate > max_bitrate) + return -ENOTSUP; + + min = find_min_bitrate_config(min_bitrate); + max = find_max_bitrate_config(max_bitrate); + if (min == NULL || max == NULL || min->bitrate > max->bitrate) + return -ENOTSUP; + + *bitrate_config = min->config | max->config; + return 0; +} + +static bool get_bitrate_indices(uint32_t config, uint32_t rate, + uint32_t *min_bitrate_index, uint32_t *max_bitrate_index) +{ + switch (config & LHDC_V5_MIN_BITRATE_MASK) { + case LHDC_V5_MIN_BITRATE_64K: + *min_bitrate_index = LHDC_QUALITY_LOW0; + break; + case LHDC_V5_MIN_BITRATE_160K: + *min_bitrate_index = LHDC_QUALITY_LOW1; + break; + case LHDC_V5_MIN_BITRATE_256K: + /* Pick the first liblhdcv5 bitrate table index >= 256k: + * 44.1 kHz maps LOW3 to 240k and LOW4 to 320k. + */ + *min_bitrate_index = rate == 44100 ? LHDC_QUALITY_LOW4 : LHDC_QUALITY_LOW3; + break; + case LHDC_V5_MIN_BITRATE_400K: + *min_bitrate_index = LHDC_QUALITY_LOW; + break; + default: + return false; + } + + switch (config & LHDC_V5_MAX_BITRATE_MASK) { + case LHDC_V5_MAX_BITRATE_400K: + *max_bitrate_index = LHDC_QUALITY_LOW; + break; + case LHDC_V5_MAX_BITRATE_500K: + *max_bitrate_index = LHDC_QUALITY_MID; + break; + case LHDC_V5_MAX_BITRATE_900K: + *max_bitrate_index = LHDC_QUALITY_HIGH; + break; + case LHDC_V5_MAX_BITRATE_1000K: + *max_bitrate_index = LHDC_QUALITY_HIGH1; + break; + default: + return false; + } + + return *min_bitrate_index <= *max_bitrate_index; +} + +static uint32_t clamp_quality(uint32_t quality, + uint32_t min_bitrate_index, uint32_t max_bitrate_index) +{ + if (quality == LHDC_QUALITY_AUTO) + return quality; + if (quality < min_bitrate_index) + return min_bitrate_index; + if (quality > max_bitrate_index) + return max_bitrate_index; + return quality; +} + 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, + .sampling_freq = LHDC_V5_SAMPLING_FREQ_44100 | + LHDC_V5_SAMPLING_FREQ_48000 | + LHDC_V5_SAMPLING_FREQ_96000 | + LHDC_V5_SAMPLING_FREQ_192000, + .bitrate_and_depth = LHDCV5_BITRATE_CONFIG | LHDCV5_FORMAT_CONFIG, + .frame_len_and_version = LHDCV5_FRAME_CONFIG, .features = LHDC_V5_FEATURE_LL, .reserved = 0, }; @@ -68,6 +273,9 @@ static int codec_select_config(const struct media_codec *codec, uint32_t flags, void **config_data) { a2dp_lhdc_v5_t conf; + int i; + const struct lhdc_format_config *format; + uint8_t bitrate_config; if (caps_size < sizeof(conf)) return -EINVAL; @@ -78,18 +286,25 @@ static int codec_select_config(const struct media_codec *codec, uint32_t flags, codec->vendor.codec_id != conf.info.codec_id) return -ENOTSUP; - if ((conf.sampling_freq & LHDC_V5_SAMPLING_FREQ_48000) == 0) + i = media_codec_select_config(lhdc_frequencies, + SPA_N_ELEMENTS(lhdc_frequencies), conf.sampling_freq, + info ? info->rate : A2DP_CODEC_DEFAULT_RATE); + if (i < 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) + conf.sampling_freq = lhdc_frequencies[i].config; + + format = select_format_config(conf.bitrate_and_depth); + if (format == NULL) 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; + if (select_bitrate_config(conf.bitrate_and_depth, &bitrate_config) < 0) + return -ENOTSUP; + + if ((conf.frame_len_and_version & LHDCV5_FRAME_CONFIG) != LHDCV5_FRAME_CONFIG) + return -ENOTSUP; + + conf.bitrate_and_depth = bitrate_config | format->config; + conf.frame_len_and_version = LHDCV5_FRAME_CONFIG; conf.features &= LHDC_V5_FEATURE_LL; conf.reserved = 0; @@ -97,53 +312,228 @@ static int codec_select_config(const struct media_codec *codec, uint32_t flags, return sizeof(conf); } +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_lhdc_v5_t *conf = caps; + const struct lhdc_format_config *format; + uint32_t min_bitrate_index, max_bitrate_index; + int rate; + + if (caps == NULL || caps_size < sizeof(*conf)) + return -EINVAL; + + if (codec->vendor.vendor_id != conf->info.vendor_id || + codec->vendor.codec_id != conf->info.codec_id) + return -EINVAL; + + rate = media_codec_get_config(lhdc_frequencies, + SPA_N_ELEMENTS(lhdc_frequencies), conf->sampling_freq); + if (rate < 0) + return -EINVAL; + + format = get_format_config(conf->bitrate_and_depth); + if (format == NULL) + return -EINVAL; + + if ((conf->frame_len_and_version & LHDCV5_FRAME_CONFIG) != LHDCV5_FRAME_CONFIG) + return -EINVAL; + + if (!get_bitrate_indices(conf->bitrate_and_depth, rate, + &min_bitrate_index, &max_bitrate_index)) + return -EINVAL; + + spa_zero(*info); + info->media_type = SPA_MEDIA_TYPE_audio; + info->media_subtype = SPA_MEDIA_SUBTYPE_raw; + info->info.raw.format = format->format; + info->info.raw.rate = 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 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_audio_info info; struct spa_pod_frame f[1]; - uint32_t position[2] = { SPA_AUDIO_CHANNEL_FL, SPA_AUDIO_CHANNEL_FR }; + int res; + + if ((res = codec_validate_config(codec, flags, caps, caps_size, &info)) < 0) + return res; 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_mediaType, SPA_POD_Id(info.media_type), + SPA_FORMAT_mediaSubtype, SPA_POD_Id(info.media_subtype), + SPA_FORMAT_AUDIO_format, SPA_POD_Id(info.info.raw.format), + SPA_FORMAT_AUDIO_rate, SPA_POD_Int(info.info.raw.rate), + SPA_FORMAT_AUDIO_channels, SPA_POD_Int(info.info.raw.channels), SPA_FORMAT_AUDIO_position, SPA_POD_Array(sizeof(uint32_t), - SPA_TYPE_Id, 2, position), + SPA_TYPE_Id, info.info.raw.channels, info.info.raw.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) +static uint32_t string_to_quality(const char *quality) { - 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 (spa_streq(quality, "400k")) + return LHDC_QUALITY_LOW; + return LHDC_QUALITY_AUTO; +} - if (codec_select_config(codec, flags, caps, caps_size, - &audio_info, NULL, config, NULL) < 0) - return -EINVAL; +static bool is_valid_quality(uint32_t quality) +{ + /* Keep the fixed choices in sync with LHDCV5_LOCAL_MAX_BITRATE. */ + return quality == LHDC_QUALITY_AUTO || quality == LHDC_QUALITY_LOW; +} - 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; +static void *codec_init_props(const struct media_codec *codec, uint32_t flags, + const struct spa_dict *settings) +{ + struct props *p = calloc(1, sizeof(struct props)); + const char *str; + + if (p == NULL) + return NULL; + + if (settings == NULL || (str = spa_dict_lookup(settings, "bluez5.a2dp.lhdc.quality")) == NULL) + str = "auto"; + + p->quality = string_to_quality(str); + return p; +} + +static void codec_clear_props(void *props) +{ + free(props); +} + +static int codec_enum_props(void *props, const struct spa_dict *settings, uint32_t id, uint32_t idx, + struct spa_pod_builder *b, struct spa_pod **param) +{ + struct props *p = props; + struct spa_pod_frame f[2]; + + switch (id) { + case SPA_PARAM_PropInfo: + switch (idx) { + case 0: + spa_pod_builder_push_object(b, &f[0], SPA_TYPE_OBJECT_PropInfo, id); + spa_pod_builder_prop(b, SPA_PROP_INFO_id, 0); + spa_pod_builder_id(b, SPA_PROP_quality); + spa_pod_builder_prop(b, SPA_PROP_INFO_description, 0); + spa_pod_builder_string(b, "LHDC v5 quality"); + + spa_pod_builder_prop(b, SPA_PROP_INFO_type, 0); + spa_pod_builder_push_choice(b, &f[1], SPA_CHOICE_Enum, 0); + spa_pod_builder_int(b, p->quality); + spa_pod_builder_int(b, LHDC_QUALITY_AUTO); + spa_pod_builder_int(b, LHDC_QUALITY_LOW); + spa_pod_builder_pop(b, &f[1]); + + spa_pod_builder_prop(b, SPA_PROP_INFO_labels, 0); + spa_pod_builder_push_struct(b, &f[1]); + spa_pod_builder_int(b, LHDC_QUALITY_AUTO); + spa_pod_builder_string(b, "auto"); + spa_pod_builder_int(b, LHDC_QUALITY_LOW); + spa_pod_builder_string(b, "400k"); + spa_pod_builder_pop(b, &f[1]); + + *param = spa_pod_builder_pop(b, &f[0]); + break; + default: + return 0; + } + break; + case SPA_PARAM_Props: + switch (idx) { + case 0: + *param = spa_pod_builder_add_object(b, + SPA_TYPE_OBJECT_Props, id, + SPA_PROP_quality, SPA_POD_Int(p->quality)); + break; + default: + return 0; + } + break; + default: + return -ENOENT; + } + return 1; +} + +static int codec_set_props(void *props, const struct spa_pod *param) +{ + struct props *p = props; + const uint32_t prev_quality = p->quality; + int quality = p->quality; + + if (param == NULL) { + p->quality = LHDC_QUALITY_AUTO; + } else { + spa_pod_parse_object(param, + SPA_TYPE_OBJECT_Props, NULL, + SPA_PROP_quality, SPA_POD_OPT_Int(&quality)); + if (is_valid_quality(quality)) + p->quality = quality; + } + + return prev_quality != p->quality; +} + +static int init_lhdc_handle(HANDLE_LHDC_BT *handle, uint32_t rate, + uint32_t bits_per_sample, uint32_t quality, uint32_t mtu, + uint32_t min_bitrate_index, uint32_t max_bitrate_index, + uint32_t *block_samples) +{ + HANDLE_LHDC_BT lhdc = NULL; + int32_t res; + + res = lhdcv5BT_get_handle(LHDC_VERSION_1, &lhdc); + if (res != LHDC_FRET_SUCCESS || lhdc == NULL) { + spa_log_error(log_, "LHDC v5 get_handle failed: %d", res); + return -EIO; + } + + res = lhdcv5BT_init_encoder(lhdc, rate, bits_per_sample, quality, mtu, + LHDCV5_INTERVAL_MS, 0); + if (res != LHDC_FRET_SUCCESS) { + spa_log_error(log_, "LHDC v5 encoder initialization failed: %d", res); + goto error; + } + + res = lhdcv5BT_set_min_bitrate(lhdc, min_bitrate_index); + if (res != LHDC_FRET_SUCCESS) { + spa_log_error(log_, "LHDC v5 set min bitrate failed: %d", res); + goto error; + } + + res = lhdcv5BT_set_max_bitrate(lhdc, max_bitrate_index); + if (res != LHDC_FRET_SUCCESS) { + spa_log_error(log_, "LHDC v5 set max bitrate failed: %d", res); + goto error; + } + + res = lhdcv5BT_get_block_Size(lhdc, block_samples); + if (res != LHDC_FRET_SUCCESS || *block_samples == 0) { + spa_log_error(log_, "LHDC v5 get block size failed: %d", res); + goto error; + } + + *handle = lhdc; return 0; + +error: + lhdcv5BT_free_handle(lhdc); + return -EIO; } static void *codec_init(const struct media_codec *codec, uint32_t flags, @@ -151,37 +541,72 @@ static void *codec_init(const struct media_codec *codec, uint32_t flags, void *props, size_t mtu) { struct impl *this; - int32_t res; + struct spa_audio_info config_info; + const a2dp_lhdc_v5_t *conf = config; + const struct lhdc_format_config *format; + const struct props *p = props; + uint32_t rate; + uint32_t min_bitrate_index, max_bitrate_index; + uint32_t quality = p != NULL ? p->quality : LHDC_QUALITY_AUTO; + int res; - if (info->info.raw.format != SPA_AUDIO_FORMAT_S16 || - info->info.raw.rate != LHDCV5_SAMPLE_RATE || - info->info.raw.channels != LHDCV5_CHANNELS) { + if (codec_validate_config(codec, flags, config, config_len, &config_info) < 0) { errno = EINVAL; return NULL; } + format = get_format_config(conf->bitrate_and_depth); + if (format == NULL) { + errno = EINVAL; + return NULL; + } + + if (info->media_type != SPA_MEDIA_TYPE_audio || + info->media_subtype != SPA_MEDIA_SUBTYPE_raw || + info->info.raw.format != config_info.info.raw.format || + info->info.raw.rate != config_info.info.raw.rate || + info->info.raw.channels != config_info.info.raw.channels) { + spa_log_error(log_, "LHDC v5 invalid audio format"); + errno = EINVAL; + return NULL; + } + if (mtu > UINT32_MAX) { + spa_log_error(log_, "LHDC v5 MTU too large: %zu", mtu); + errno = EINVAL; + return NULL; + } + + rate = config_info.info.raw.rate; + if (!get_bitrate_indices(conf->bitrate_and_depth, rate, + &min_bitrate_index, &max_bitrate_index)) { + errno = EINVAL; + return NULL; + } + quality = clamp_quality(quality, min_bitrate_index, max_bitrate_index); + 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) + this->quality = quality; + this->rate = rate; + this->bits_per_sample = format->bits_per_sample; + this->sample_size = format->sample_size; + this->mtu = (uint32_t)mtu; + this->min_bitrate_index = min_bitrate_index; + this->max_bitrate_index = max_bitrate_index; + + res = init_lhdc_handle(&this->lhdc, this->rate, this->bits_per_sample, + this->quality, this->mtu, this->min_bitrate_index, + this->max_bitrate_index, &this->block_samples); + if (res < 0) 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) + if (this->block_samples > UINT32_MAX / LHDCV5_CHANNELS / this->sample_size) { + spa_log_error(log_, "LHDC v5 block size overflow"); 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); + } + this->block_bytes = this->block_samples * LHDCV5_CHANNELS * format->sample_size; return this; error: @@ -192,6 +617,50 @@ error: return NULL; } +static int codec_update_props(void *data, void *props) +{ + struct impl *this = data; + const struct props *p = props; + HANDLE_LHDC_BT lhdc = NULL; + uint32_t block_samples = 0; + uint32_t block_bytes; + uint32_t quality; + int res; + + if (p == NULL) + return 0; + + quality = clamp_quality(p->quality, this->min_bitrate_index, + this->max_bitrate_index); + if (quality == this->quality) + return 0; + + res = init_lhdc_handle(&lhdc, this->rate, this->bits_per_sample, + quality, this->mtu, this->min_bitrate_index, + this->max_bitrate_index, + &block_samples); + if (res < 0) + return res; + + if (block_samples > UINT32_MAX / LHDCV5_CHANNELS / this->sample_size) { + spa_log_error(log_, "LHDC v5 block size overflow"); + lhdcv5BT_free_handle(lhdc); + return -EOVERFLOW; + } + + block_bytes = block_samples * LHDCV5_CHANNELS * this->sample_size; + if (block_bytes != this->block_bytes) { + spa_log_error(log_, "LHDC v5 block size changed during props update"); + lhdcv5BT_free_handle(lhdc); + return -EINVAL; + } + + lhdcv5BT_free_handle(this->lhdc); + this->lhdc = lhdc; + this->quality = quality; + return 0; +} + static void codec_deinit(void *data) { struct impl *this = data; @@ -206,15 +675,6 @@ static int codec_get_block_size(void *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) { @@ -263,11 +723,17 @@ static int codec_encode(void *data, res = lhdcv5BT_encode(this->lhdc, (void *)src, this->block_bytes, dst, (uint32_t)dst_size, &written, &frames); - if (res != LHDC_FRET_SUCCESS) + if (res != LHDC_FRET_SUCCESS) { + spa_log_error(log_, "LHDC v5 encode failed: %d", res); return -EIO; + } if (written == 0) return this->block_bytes; + if (written > dst_size) + return -EIO; + if (frames == 0 || frames > LHDCV5_MAX_FRAME_COUNT) + return -EIO; this->payload->latency = 0; this->payload->frame_count = frames; @@ -278,15 +744,6 @@ static int codec_encode(void *data, 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; @@ -306,13 +763,16 @@ const struct media_codec a2dp_codec_lhdc_v5 = { .select_config = codec_select_config, .enum_config = codec_enum_config, .validate_config = codec_validate_config, + .init_props = codec_init_props, + .enum_props = codec_enum_props, + .set_props = codec_set_props, + .clear_props = codec_clear_props, .init = codec_init, .deinit = codec_deinit, + .update_props = codec_update_props, .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, }; From 23cc0740195dd51db7b221c0af366a646187d773 Mon Sep 17 00:00:00 2001 From: DBeidachazi <269502169@qq.com> Date: Fri, 3 Jul 2026 18:07:21 +0800 Subject: [PATCH 3/3] ci: disable LHDC in all-options jobs --- .gitlab-ci.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 03015ecd1..1f0e785f9 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -358,6 +358,7 @@ build_on_debian: -D auto_features=enabled -D session-managers=[] -D bluez5-backend-native-mm=enabled + -D bluez5-codec-lhdc=disabled -D bluez5-codec-lc3plus=disabled -D bluez5-codec-ldac=disabled -D bluez5-codec-ldac-dec=disabled @@ -530,6 +531,7 @@ build_all: MESON_OPTIONS: >- -Dauto_features=enabled -Dbluez5-codec-aptx=disabled + -Dbluez5-codec-lhdc=disabled -Dbluez5-codec-lc3plus=disabled -Dbluez5-codec-ldac-dec=disabled -Droc=disabled