From d733a37ae09e412bdc1cf5cf5d49d5b289af6469 Mon Sep 17 00:00:00 2001 From: Liam Girdwood Date: Sat, 28 Nov 2026 17:05:14 +0000 Subject: [PATCH 1/9] lib_manager: llext_manager: detach cold partitions before mapping shdr Loading or unloading DP LLEXT modules whose .cold/.coldrodata segments are detached in DRAM can share cold partition metadata across LL and DP memory domains. Detach cold partitions before section mapping/unloading and switch partition removal error logs to Zephyr LOG_ERR(). Signed-off-by: Liam Girdwood --- src/kernel/library_manager/llext_manager.c | 72 ++++++++++++++-------- 1 file changed, 48 insertions(+), 24 deletions(-) diff --git a/src/kernel/library_manager/llext_manager.c b/src/kernel/library_manager/llext_manager.c index 57c103bfed21..67969afeff64 100644 --- a/src/kernel/library_manager/llext_manager.c +++ b/src/kernel/library_manager/llext_manager.c @@ -205,6 +205,42 @@ static int llext_manager_rm_partition(struct k_mem_domain *domain, tr_dbg(&lib_manager_tr, "remove %#zx @ %lx partition", part.size, part.start); return k_mem_domain_remove_partition(domain, &part); } + +/** + * Detach .cold and .coldrodata partitions from @domain and clear their metadata. + */ +static int llext_manager_rm_cold_partitions(struct lib_manager_module *mctx, + struct k_mem_domain *domain) +{ + int err, ret = 0; + + if (mctx->segment[LIB_MANAGER_COLD].addr) { + ret = llext_manager_rm_partition(domain, + mctx->segment[LIB_MANAGER_COLD].addr, + mctx->segment[LIB_MANAGER_COLD].size, + K_MEM_PARTITION_P_RX_U_RX | XTENSA_MMU_CACHED_WB); + if (ret < 0) + LOG_ERR("failed to remove .cold memory partition: %d", ret); + mctx->segment[LIB_MANAGER_COLD].addr = 0; + mctx->segment[LIB_MANAGER_COLD].size = 0; + } + + if (mctx->segment[LIB_MANAGER_COLDRODATA].addr) { + err = llext_manager_rm_partition(domain, + mctx->segment[LIB_MANAGER_COLDRODATA].addr, + mctx->segment[LIB_MANAGER_COLDRODATA].size, + K_MEM_PARTITION_P_RO_U_RO | XTENSA_MMU_CACHED_WB); + if (err < 0) { + LOG_ERR("failed to remove .coldrodata memory partition: %d", err); + if (!ret) + ret = err; + } + mctx->segment[LIB_MANAGER_COLDRODATA].addr = 0; + mctx->segment[LIB_MANAGER_COLDRODATA].size = 0; + } + + return ret; +} #endif /* CONFIG_USERSPACE */ static void llext_manager_unmap_detached_sections(struct llext_loader *ldr, @@ -395,6 +431,15 @@ __cold static int llext_manager_unload_module(struct lib_manager_module *mctx) assert_can_be_cold(); #ifdef CONFIG_SOF_USERSPACE_LL + /* + * Detach .cold/.coldrodata partitions before mapping shdr: in small + * LLEXT ELF buffers, section headers can share a 4 KiB DRAM page with + * .cold/.coldrodata, and reset_region() restores boot-time kernel access. + * Keep .text/.rodata/.data mapped in the domain until after + * llext_manager_align_unmap() so cache writeback does not fault. + */ + llext_manager_rm_cold_partitions(mctx, zephyr_ll_mem_domain()); + unsigned int sect_cnt = llext_section_count(ext); size_t total = sect_cnt * sizeof(elf_shdr_t); const elf_shdr_t *shdr; @@ -1195,30 +1240,9 @@ static int llext_manager_rm_mod_domain(struct lib_manager_module *mctx, struct k } } - if (mctx->segment[LIB_MANAGER_COLD].addr) { - err = llext_manager_rm_partition(domain, - mctx->segment[LIB_MANAGER_COLD].addr, - mctx->segment[LIB_MANAGER_COLD].size, - K_MEM_PARTITION_P_RX_U_RX | XTENSA_MMU_CACHED_WB); - if (err < 0) { - tr_err(&lib_manager_tr, "failed to remove .cold memory partition: %d", err); - if (!ret) - ret = err; - } - } - - if (mctx->segment[LIB_MANAGER_COLDRODATA].addr) { - err = llext_manager_rm_partition(domain, - mctx->segment[LIB_MANAGER_COLDRODATA].addr, - mctx->segment[LIB_MANAGER_COLDRODATA].size, - K_MEM_PARTITION_P_RO_U_RO | XTENSA_MMU_CACHED_WB); - if (err < 0) { - tr_err(&lib_manager_tr, - "failed to remove .coldrodata memory partition: %d", err); - if (!ret) - ret = err; - } - } + err = llext_manager_rm_cold_partitions(mctx, domain); + if (err < 0 && !ret) + ret = err; return ret; } From 0fec69c63bb80cf83d8481002fa803479eeab04d Mon Sep 17 00:00:00 2001 From: Liam Girdwood Date: Sat, 28 Nov 2026 17:05:14 +0000 Subject: [PATCH 2/9] zephyr: vregion: flush interim heap metadata for SMP coherency DP codec modules execute on secondary cores (e.g., FFmpeg on core 1) while IPC setup and teardown on core 0 access the module's vregion interim heap. Explicitly flush D-cache over k_heap metadata updates so all cores observe coherent interim heap state under SMP. Signed-off-by: Liam Girdwood --- zephyr/lib/vregion.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/zephyr/lib/vregion.c b/zephyr/lib/vregion.c index 7239c7644a68..a52788f84a0a 100644 --- a/zephyr/lib/vregion.c +++ b/zephyr/lib/vregion.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -481,6 +482,11 @@ static void interim_heap_init(struct vregion *vr) vr->interim.heap.heap.init_bytes = interim_size; k_heap_init(&vr->interim.heap, interim_base, interim_size); + /* + * Ensure the heap metadata written into interim_base on the IPC core + * is visible to DP worker cores running on other CPUs without L1 cache coherency. + */ + sys_cache_data_flush_range(interim_base, interim_size); vr->type = VREGION_MEM_TYPE_INTERIM; /* Update lifetime heap with the interim heap usage. */ From 41a9116d6a1a07dd80324b84880d04f305bfdc23 Mon Sep 17 00:00:00 2001 From: Liam Girdwood Date: Sat, 28 Nov 2026 18:10:43 +0000 Subject: [PATCH 3/9] ipc4: base_fw: add runtime codec registry, query, and library unload Remove build-time per-codec #ifdef checks from get_codec_info() in base_fw.c and replace them with a pre-allocated static slot table (sized by CONFIG_SOF_IPC4_CODEC_INFO_MAX_ITEMS) updated 1-by-1 via sof_codec_info_add() / sof_codec_info_add_items() / sof_codec_info_remove() under a spinlock to avoid heap fragmentation across LLEXT load/unload cycles. Add IPC4_SOF_INFO_GET (37) and IPC4_SOF_LIBRARY_UNLOAD (38) along with IPC4_SOF_CAP_RUNTIME_SOF_INFO and IPC4_SOF_CAP_LIBRARY_UNLOAD under IPC4_SOF_CAPS so hosts can query updated codecs after library load or unload and purge idle dynamic libraries. Keep IPC4_FW_CONFIG emitting IPC4_SOF_CODEC_INFO and IPC4_SOF_CAPS for backwards compatibility. Signed-off-by: Liam Girdwood --- src/audio/Kconfig | 9 + src/include/ipc4/base_fw.h | 14 ++ src/include/sof/audio/codec_info.h | 72 ++++++ src/kernel/audio/base_fw.c | 388 ++++++++++++++++------------- 4 files changed, 311 insertions(+), 172 deletions(-) create mode 100644 src/include/sof/audio/codec_info.h diff --git a/src/audio/Kconfig b/src/audio/Kconfig index 4061b5c2d6f2..2e567c1f1b04 100644 --- a/src/audio/Kconfig +++ b/src/audio/Kconfig @@ -9,6 +9,15 @@ config COMP_BASEFW_IPC4 help Select for BASEFW component +config SOF_IPC4_CODEC_INFO_MAX_ITEMS + int "Maximum runtime codec capability items in BaseFW" + default 64 + range 16 256 + depends on COMP_BASEFW_IPC4 + help + Maximum number of codec capability entries stored in the BaseFW + static runtime codec registry for IPC4_FW_SOF_INFO queries. + config IPC4_BASE_FW_INTEL bool "BASEFW component Intel vendor extensions" depends on COMP_BASEFW_IPC4 diff --git a/src/include/ipc4/base_fw.h b/src/include/ipc4/base_fw.h index 6583365e755a..b513798886fa 100644 --- a/src/include/ipc4/base_fw.h +++ b/src/include/ipc4/base_fw.h @@ -190,6 +190,12 @@ enum ipc4_basefw_params { /** Set policy mask for microphone privacy in firmware-managed mode. */ IPC4_SET_MIC_PRIVACY_FW_MANAGED_POLICY_MASK = 36, + + /** Discover SOF specific runtime capabilities/information in TLV format via LARGE_CONFIG_GET. */ + IPC4_SOF_INFO_GET = 37, + + /** Unload an idle dynamic library by ID via LARGE_CONFIG_SET. */ + IPC4_SOF_LIBRARY_UNLOAD = 38, }; /** @@ -289,6 +295,8 @@ enum ipc4_fw_config_params { enum ipc4_fw_sof_info_params { /** Decoder/encoder codec information. */ IPC4_SOF_CODEC_INFO = 0, + /** Bitmask of runtime SOF info capabilities (IPC4_SOF_CAP_*). */ + IPC4_SOF_CAPS = 1, /** Total number of SOF config parameters. */ IPC4_SOF_CFG_PARAMS_COUNT, @@ -296,6 +304,12 @@ enum ipc4_fw_sof_info_params { IPC4_MAX_SOF_CFG_PARAM = IPC4_SOF_CFG_PARAMS_COUNT - 1, }; +/** BaseFW supports runtime query via IPC4_SOF_INFO_GET. */ +#define IPC4_SOF_CAP_RUNTIME_SOF_INFO (1U << 0) + +/** BaseFW supports unloading idle dynamic libraries via IPC4_SOF_LIBRARY_UNLOAD. */ +#define IPC4_SOF_CAP_LIBRARY_UNLOAD (1U << 1) + /** * \brief Hardware configuration parameter TLV IDs returned by IPC4_HW_CONFIG_GET. * \ingroup ipc4_base_fw_api diff --git a/src/include/sof/audio/codec_info.h b/src/include/sof/audio/codec_info.h new file mode 100644 index 000000000000..8d581dd4dc16 --- /dev/null +++ b/src/include/sof/audio/codec_info.h @@ -0,0 +1,72 @@ +/* SPDX-License-Identifier: BSD-3-Clause */ +/* + * Copyright(c) 2026 Intel Corporation. All rights reserved. + */ + +#ifndef __SOF_AUDIO_CODEC_INFO_H__ +#define __SOF_AUDIO_CODEC_INFO_H__ + +#include +#include +#include + +/** + * \brief Encodes codec and direction information into a single 32-bit value. + * \param codec Codec type (bits 0-7, e.g. SND_AUDIOCODEC_*) + * \param dir Stream direction (bits 8-11, e.g. SOF_IPC_STREAM_*) + * \return Encoded 32-bit codec capability item + */ +#define SOF_CODEC_INFO_ITEM(codec, dir) \ + (((uint32_t)(codec) & 0xffU) | (((uint32_t)(dir) & 0xfU) << 8)) + +/** ELF section name holding LLEXT module codec capability items. */ +#define SOF_CODEC_INFO_SECTION ".sof_codec_info" + +#if CONFIG_COMP_BASEFW_IPC4 && !CONFIG_LIBRARY +/** + * \brief Add a single runtime codec capability item for a module or library. + * \param[in] owner Opaque owner token (e.g. module interface or library module ctx). + * \param[in] item Encoded SOF_CODEC_INFO_ITEM(codec, dir) value. + * \return 0 on success, negative error code on failure. + */ +int sof_codec_info_add(const void *owner, uint32_t item); + +/** + * \brief Remove runtime codec capability item(s) for an owner token. + * \param[in] owner Opaque owner token previously passed to sof_codec_info_add(). + * \param[in] item Encoded item to remove, or 0 to remove all items for @p owner. + */ +void sof_codec_info_remove(const void *owner, uint32_t item); + +/** + * \brief Add multiple runtime codec capability items for a module or library. + * \param[in] owner Opaque owner token (e.g. module interface or library module ctx). + * \param[in] items Array of encoded SOF_CODEC_INFO_ITEM(codec, dir) values. + * \param[in] count Number of entries in @p items. + * \return 0 on success, negative error code on failure. + */ +int sof_codec_info_add_items(const void *owner, const uint32_t *items, size_t count); +#else +static inline int sof_codec_info_add(const void *owner, uint32_t item) +{ + (void)owner; + (void)item; + return 0; +} + +static inline void sof_codec_info_remove(const void *owner, uint32_t item) +{ + (void)owner; + (void)item; +} + +static inline int sof_codec_info_add_items(const void *owner, const uint32_t *items, size_t count) +{ + (void)owner; + (void)items; + (void)count; + return 0; +} +#endif + +#endif /* __SOF_AUDIO_CODEC_INFO_H__ */ diff --git a/src/kernel/audio/base_fw.c b/src/kernel/audio/base_fw.c index 099a9172bc7a..e8c170f43290 100644 --- a/src/kernel/audio/base_fw.c +++ b/src/kernel/audio/base_fw.c @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -15,7 +16,6 @@ #include #include #include -#include #include #include #include @@ -24,8 +24,11 @@ #include #include #include +#include #include #include +#include +#include #include #include #include @@ -78,185 +81,144 @@ __cold static uint32_t get_host_buffer_size(void) struct sof_ipc4_codec_info_data { uint32_t count; - uint32_t items[32]; + uint32_t items[CONFIG_SOF_IPC4_CODEC_INFO_MAX_ITEMS]; } __packed __aligned(4); -/** - * Encodes codec and direction information into a single 32-bit value. - * @param codec Codec type (bits 0-7) - * @param dir Stream direction (bits 8-11) - * @return Encoded 32-bit value - */ -#define SET_CODEC_INFO_ITEM(codec, dir) (((codec) & 0xff) | (((dir) & 0xf) << 8)) +/* Static owner-tagged slot table to avoid runtime heap churn on LLEXT reload. */ +struct sof_codec_info_slot { + const void *owner; + uint32_t item; +}; + +static struct sof_codec_info_slot codec_info_slots[CONFIG_SOF_IPC4_CODEC_INFO_MAX_ITEMS]; +static struct k_spinlock codec_info_lock; -static inline __maybe_unused void -add_codec_info_item(struct sof_ipc4_codec_info_data *info, - uint32_t codec, uint32_t dir) +/* Append a unique non-zero codec capability item into the IPC4 response buffer. */ +static inline void add_codec_info_item(struct sof_ipc4_codec_info_data *info, + uint32_t item) { - if (info->count < ARRAY_SIZE(info->items)) { - info->items[info->count++] = SET_CODEC_INFO_ITEM(codec, dir); + uint32_t i; + + if (!item) + return; + + /* Deduplicate identical codec/direction pairs reported by multiple owners. */ + for (i = 0; i < info->count; i++) { + if (info->items[i] == item) + return; } + + if (info->count < ARRAY_SIZE(info->items)) + info->items[info->count++] = item; } -static void get_codec_info(struct sof_tlv **tuple) +/* Register one encoded codec capability item under an opaque owner token. */ +__cold int sof_codec_info_add(const void *owner, uint32_t item) { - struct sof_ipc4_codec_info_data codec_info = { 0 }; + k_spinlock_key_t key; + int free_idx = -1; + uint32_t i; -#ifdef CONFIG_CADENCE_CODEC_AAC_DEC - add_codec_info_item(&codec_info, SND_AUDIOCODEC_AAC, - SOF_IPC_STREAM_PLAYBACK); -#endif -#ifdef CONFIG_CADENCE_CODEC_MP3_DEC - add_codec_info_item(&codec_info, SND_AUDIOCODEC_MP3, - SOF_IPC_STREAM_PLAYBACK); -#endif -#ifdef CONFIG_CADENCE_CODEC_MP3_ENC - add_codec_info_item(&codec_info, SND_AUDIOCODEC_MP3, - SOF_IPC_STREAM_CAPTURE); -#endif -#ifdef CONFIG_CADENCE_CODEC_VORBIS_DEC - add_codec_info_item(&codec_info, SND_AUDIOCODEC_VORBIS, - SOF_IPC_STREAM_PLAYBACK); -#endif -#ifdef CONFIG_COMP_MFCC - add_codec_info_item(&codec_info, SND_AUDIOCODEC_BESPOKE, - SOF_IPC_STREAM_CAPTURE); -#endif -#if defined(CONFIG_SOF_COMPRESS_CODEC_PCM_DEC) || defined(CONFIG_FFMPEG_FILTER) - add_codec_info_item(&codec_info, SND_AUDIOCODEC_PCM, - SOF_IPC_STREAM_PLAYBACK); -#endif -#ifdef CONFIG_FFMPEG_DEC_FLAC - /* FLAC decode is provided by the ffmpeg_dec (libavcodec) module. */ - add_codec_info_item(&codec_info, SND_AUDIOCODEC_FLAC, - SOF_IPC_STREAM_PLAYBACK); -#endif -#ifdef CONFIG_FFMPEG_DEC_ALAC - /* ALAC decode is provided by the ffmpeg_dec (libavcodec) module. */ - add_codec_info_item(&codec_info, SND_AUDIOCODEC_ALAC, - SOF_IPC_STREAM_PLAYBACK); -#endif -#if defined(CONFIG_FFMPEG_DEC_AAC) && !defined(CONFIG_CADENCE_CODEC_AAC_DEC) - /* AAC-LC decode is provided by the ffmpeg_dec (libavcodec) module (guarded so - * it is not advertised twice when the Cadence AAC decoder is also present). - */ - add_codec_info_item(&codec_info, SND_AUDIOCODEC_AAC, - SOF_IPC_STREAM_PLAYBACK); -#endif -#if defined(CONFIG_FFMPEG_DEC_MP3) && !defined(CONFIG_CADENCE_CODEC_MP3_DEC) - /* MP3 decode is provided by the ffmpeg_dec (libavcodec) module (guarded so - * it is not advertised twice when the Cadence MP3 decoder is also present). - */ - add_codec_info_item(&codec_info, SND_AUDIOCODEC_MP3, - SOF_IPC_STREAM_PLAYBACK); -#endif -#if defined(CONFIG_FFMPEG_ENC_MP3) && !defined(CONFIG_CADENCE_CODEC_MP3_ENC) - /* MP3 encode (compress capture) via ffmpeg_dec (libshine), guarded so it is - * not advertised twice when the Cadence MP3 encoder is also present. - */ - add_codec_info_item(&codec_info, SND_AUDIOCODEC_MP3, - SOF_IPC_STREAM_CAPTURE); -#endif -#if defined(CONFIG_FFMPEG_ENC_AAC) - /* AAC-LC encode (compress capture) via ffmpeg_dec (vo-aacenc). */ - add_codec_info_item(&codec_info, SND_AUDIOCODEC_AAC, - SOF_IPC_STREAM_CAPTURE); -#endif -#if defined(CONFIG_FFMPEG_ENC_FLAC) - /* FLAC encode (compress capture) via ffmpeg_enc (native libavcodec). */ - add_codec_info_item(&codec_info, SND_AUDIOCODEC_FLAC, - SOF_IPC_STREAM_CAPTURE); -#endif -#if defined(CONFIG_FFMPEG_ENC_ALAC) - /* ALAC encode (compress capture) via ffmpeg_enc (native libavcodec). */ - add_codec_info_item(&codec_info, SND_AUDIOCODEC_ALAC, - SOF_IPC_STREAM_CAPTURE); -#endif -#if defined(CONFIG_FFMPEG_DEC_SBC) - /* Bluetooth SBC / mSBC decode via ffmpeg_dec (native libavcodec). */ - add_codec_info_item(&codec_info, SND_AUDIOCODEC_SBC, - SOF_IPC_STREAM_PLAYBACK); -#endif -#if defined(CONFIG_FFMPEG_ENC_SBC) - /* Bluetooth SBC / mSBC encode via ffmpeg_enc (native libavcodec). */ - add_codec_info_item(&codec_info, SND_AUDIOCODEC_SBC, - SOF_IPC_STREAM_CAPTURE); -#endif -#if defined(CONFIG_FFMPEG_DEC_G722) - /* ITU-T G.722 wideband speech decode via ffmpeg_dec (native libavcodec). */ - add_codec_info_item(&codec_info, SND_AUDIOCODEC_G722, - SOF_IPC_STREAM_PLAYBACK); -#endif -#if defined(CONFIG_FFMPEG_ENC_G722) - /* ITU-T G.722 wideband speech encode via ffmpeg_enc (native libavcodec). */ - add_codec_info_item(&codec_info, SND_AUDIOCODEC_G722, - SOF_IPC_STREAM_CAPTURE); -#endif -#if defined(CONFIG_FFMPEG_DEC_APTX) - /* Bluetooth aptX decode via ffmpeg_dec (native libavcodec). */ - add_codec_info_item(&codec_info, SND_AUDIOCODEC_APTX, - SOF_IPC_STREAM_PLAYBACK); -#endif -#if defined(CONFIG_FFMPEG_ENC_APTX) - /* Bluetooth aptX encode via ffmpeg_enc (native libavcodec). */ - add_codec_info_item(&codec_info, SND_AUDIOCODEC_APTX, - SOF_IPC_STREAM_CAPTURE); -#endif -#if defined(CONFIG_FFMPEG_DEC_APTX_HD) - /* Bluetooth aptX HD decode via ffmpeg_dec (native libavcodec). */ - add_codec_info_item(&codec_info, SND_AUDIOCODEC_APTX_HD, SOF_IPC_STREAM_PLAYBACK); -#endif -#if defined(CONFIG_FFMPEG_ENC_APTX_HD) - /* Bluetooth aptX HD encode via ffmpeg_enc (native libavcodec). */ - add_codec_info_item(&codec_info, SND_AUDIOCODEC_APTX_HD, SOF_IPC_STREAM_CAPTURE); -#endif -#if defined(CONFIG_FFMPEG_DEC_G726) - /* ITU-T G.726 ADPCM decode via ffmpeg_dec (native libavcodec). */ - add_codec_info_item(&codec_info, SND_AUDIOCODEC_G726, SOF_IPC_STREAM_PLAYBACK); -#endif -#if defined(CONFIG_FFMPEG_ENC_G726) - /* ITU-T G.726 ADPCM encode via ffmpeg_enc (native libavcodec). */ - add_codec_info_item(&codec_info, SND_AUDIOCODEC_G726, SOF_IPC_STREAM_CAPTURE); -#endif -#if defined(CONFIG_FFMPEG_DEC_ALAW) - /* ITU-T G.711 A-law decode via ffmpeg_dec (native libavcodec). */ - add_codec_info_item(&codec_info, SND_AUDIOCODEC_ALAW, SOF_IPC_STREAM_PLAYBACK); -#endif -#if defined(CONFIG_FFMPEG_ENC_ALAW) - /* ITU-T G.711 A-law encode via ffmpeg_enc (native libavcodec). */ - add_codec_info_item(&codec_info, SND_AUDIOCODEC_ALAW, SOF_IPC_STREAM_CAPTURE); -#endif -#if defined(CONFIG_FFMPEG_DEC_MULAW) - /* ITU-T G.711 mu-law decode via ffmpeg_dec (native libavcodec). */ - add_codec_info_item(&codec_info, SND_AUDIOCODEC_MULAW, SOF_IPC_STREAM_PLAYBACK); -#endif -#if defined(CONFIG_FFMPEG_ENC_MULAW) - /* ITU-T G.711 mu-law encode via ffmpeg_enc (native libavcodec). */ - add_codec_info_item(&codec_info, SND_AUDIOCODEC_MULAW, SOF_IPC_STREAM_CAPTURE); -#endif -#if (defined(CONFIG_FFMPEG_ENC_SBC) || defined(CONFIG_FFMPEG_ENC_G722) || \ - defined(CONFIG_FFMPEG_ENC_APTX) || defined(CONFIG_FFMPEG_ENC_APTX_HD) || \ - defined(CONFIG_FFMPEG_ENC_G726) || defined(CONFIG_FFMPEG_ENC_ALAW) || \ - defined(CONFIG_FFMPEG_ENC_MULAW)) && !defined(CONFIG_COMP_MFCC) - /* Also advertise BESPOKE capture for kernels with older SND_AUDIOCODEC_MAX. */ - add_codec_info_item(&codec_info, SND_AUDIOCODEC_BESPOKE, SOF_IPC_STREAM_CAPTURE); -#endif -#ifdef CONFIG_FFMPEG_DEC_OPUS - /* Opus decode is provided by the ffmpeg_dec (libavcodec) module. - * Advertise OPUS_RAW and BESPOKE. - */ - add_codec_info_item(&codec_info, SND_AUDIOCODEC_OPUS_RAW, SOF_IPC_STREAM_PLAYBACK); -#endif -#if defined(CONFIG_FFMPEG_DEC_OPUS) || defined(CONFIG_FFMPEG_DEC_SBC) || \ - defined(CONFIG_FFMPEG_DEC_G722) || defined(CONFIG_FFMPEG_DEC_APTX) || \ - defined(CONFIG_FFMPEG_DEC_APTX_HD) || defined(CONFIG_FFMPEG_DEC_G726) || \ - defined(CONFIG_FFMPEG_DEC_ALAW) || defined(CONFIG_FFMPEG_DEC_MULAW) - add_codec_info_item(&codec_info, SND_AUDIOCODEC_BESPOKE, SOF_IPC_STREAM_PLAYBACK); -#endif + assert_can_be_cold(); + + if (!owner || !item) + return -EINVAL; + + /* Scan atomically for duplicates while remembering the first free slot. */ + key = k_spin_lock(&codec_info_lock); + for (i = 0; i < ARRAY_SIZE(codec_info_slots); i++) { + if (codec_info_slots[i].owner == owner && codec_info_slots[i].item == item) { + k_spin_unlock(&codec_info_lock, key); + return 0; + } + if (!codec_info_slots[i].owner && free_idx < 0) + free_idx = i; + } + + /* Reject additions once the static slot table is exhausted. */ + if (free_idx < 0) { + k_spin_unlock(&codec_info_lock, key); + return -ENOSPC; + } + + /* Commit the new owner/item entry inside the lock. */ + codec_info_slots[free_idx].owner = owner; + codec_info_slots[free_idx].item = item; + k_spin_unlock(&codec_info_lock, key); + + return 0; +} + +/* Register an array of encoded codec capability items for a module or library. */ +__cold int sof_codec_info_add_items(const void *owner, const uint32_t *items, size_t count) +{ + size_t i; + + assert_can_be_cold(); + + /* Reject invalid item arrays before mutating the slot table. */ + if (!items) { + LOG_ERR("sof_codec_info_add_items: NULL items for owner %p", owner); + return -EINVAL; + } - if (!codec_info.count) + /* Insert each item individually so duplicates and bounds are checked uniformly. */ + for (i = 0; i < count; i++) { + int ret = sof_codec_info_add(owner, items[i]); + + if (ret < 0) { + LOG_ERR("sof_codec_info_add(%#x) failed for owner %p: %d", + items[i], owner, ret); + return ret; + } + } + + return 0; +} + +/* Remove one item (or all items if @p item is 0) belonging to @p owner. */ +__cold void sof_codec_info_remove(const void *owner, uint32_t item) +{ + k_spinlock_key_t key; + uint32_t i; + + assert_can_be_cold(); + + if (!owner) + return; + + /* Clear matching slots under lock so readers never see partial entries. */ + key = k_spin_lock(&codec_info_lock); + for (i = 0; i < ARRAY_SIZE(codec_info_slots); i++) { + if (codec_info_slots[i].owner == owner && + (!item || codec_info_slots[i].item == item)) { + codec_info_slots[i].owner = NULL; + codec_info_slots[i].item = 0; + } + } + k_spin_unlock(&codec_info_lock, key); +} + +/* Populate the IPC4_SOF_CODEC_INFO TLV tuple from active registry slots. */ +static void get_codec_info(struct sof_tlv **tuple, bool include_empty) +{ + struct sof_ipc4_codec_info_data codec_info = { 0 }; + k_spinlock_key_t key; + uint32_t i; + + /* Snapshot all unique non-zero codec items under the registry spinlock. */ + key = k_spin_lock(&codec_info_lock); + for (i = 0; i < ARRAY_SIZE(codec_info_slots); i++) { + if (codec_info_slots[i].owner) + add_codec_info_item(&codec_info, codec_info_slots[i].item); + } + k_spin_unlock(&codec_info_lock, key); + + /* Omit empty codec lists in IPC4_FW_CONFIG for backwards compatibility. */ + if (!codec_info.count && !include_empty) return; + /* Serialize count plus active items into the next TLV slot. */ tlv_value_set(*tuple, IPC4_SOF_CODEC_INFO, sizeof(codec_info.count) + sizeof(codec_info.items[0]) * codec_info.count, &codec_info); @@ -265,16 +227,36 @@ static void get_codec_info(struct sof_tlv **tuple) #define SOF_CONFIG_MEMBER_SIZE(struct_name) (sizeof(struct sof_tlv) + \ sizeof(struct struct_name)) -#define SOF_CONFIG_SIZE_MAX (SOF_CONFIG_MEMBER_SIZE(sof_ipc4_codec_info_data)) +#define SOF_CONFIG_SIZE_MAX (SOF_CONFIG_MEMBER_SIZE(sof_ipc4_codec_info_data) + \ + sizeof(struct sof_tlv) + sizeof(uint32_t)) + +/* Serialize nested IPC4_FW_SOF_INFO TLVs (codec list + SOF capability mask). */ +static uint32_t build_sof_info_tlvs(char *buf, bool include_empty_codec_info) +{ + struct sof_tlv *sof_config_tuple = (struct sof_tlv *)buf; + uint32_t caps = IPC4_SOF_CAP_RUNTIME_SOF_INFO; + /* Advertise dynamic library unload when library manager is compiled in. */ +#ifdef CONFIG_LIBRARY_MANAGER + caps |= IPC4_SOF_CAP_LIBRARY_UNLOAD; +#endif + + /* Emit active codec capabilities, then append the BaseFW SOF capability bits. */ + get_codec_info(&sof_config_tuple, include_empty_codec_info); + + tlv_value_uint32_set(sof_config_tuple, IPC4_SOF_CAPS, caps); + sof_config_tuple = tlv_next(sof_config_tuple); + + return (uint32_t)((char *)sof_config_tuple - buf); +} + +/* Append IPC4_FW_SOF_INFO TLV to IPC4_FW_CONFIG response. */ static void base_fw_sof_config(struct sof_tlv **tuple) { char sof_config_data[SOF_CONFIG_SIZE_MAX] = { 0 }; - struct sof_tlv *sof_config_tuple = (struct sof_tlv *)sof_config_data; uint32_t sof_config_size; - get_codec_info(&sof_config_tuple); - sof_config_size = (uint32_t)((char *)sof_config_tuple - sof_config_data); + sof_config_size = build_sof_info_tlvs(sof_config_data, false); if (sof_config_size == 0) return; @@ -283,6 +265,30 @@ static void base_fw_sof_config(struct sof_tlv **tuple) *tuple = tlv_next(*tuple); } +/* Handle IPC4_SOF_INFO_GET query with live codec and SOF capability TLVs. */ +__cold static int basefw_sof_info_get(uint32_t *data_offset, char *data) +{ + char sof_config_data[SOF_CONFIG_SIZE_MAX] = { 0 }; + uint32_t sof_config_size; + int ret; + + assert_can_be_cold(); + + /* Build nested TLV payload and verify caller buffer capacity. */ + sof_config_size = build_sof_info_tlvs(sof_config_data, true); + if (*data_offset < sof_config_size) + return IPC4_INVALID_CONFIG_DATA_LEN; + + /* Copy TLV payload to IPC response buffer and report exact byte length. */ + ret = memcpy_s(data, *data_offset, sof_config_data, sof_config_size); + if (ret < 0) + return IPC4_OUT_OF_MEMORY; + + *data_offset = sof_config_size; + + return IPC4_SUCCESS; +} + __cold static int basefw_config(uint32_t *data_offset, char *data) { uint16_t version[4] = {SOF_MAJOR, SOF_MINOR, SOF_MICRO, SOF_BUILD}; @@ -877,6 +883,8 @@ __cold static int basefw_get_large_config(struct comp_dev *dev, uint32_t param_i return io_global_perf_state_get(data_offset, data); case IPC4_IO_GLOBAL_PERF_DATA: return io_global_perf_data_get(data_offset, data); + case IPC4_SOF_INFO_GET: + return basefw_sof_info_get(data_offset, data); /* TODO: add more support */ case IPC4_DSP_RESOURCE_STATE: @@ -971,6 +979,40 @@ __cold static int basefw_dma_control(bool first_block, bool last_block, uint32_t return IPC4_SUCCESS; } +/* Handle IPC4_SOF_LIBRARY_UNLOAD requests to free an idle external library. */ +__cold static int basefw_library_unload(bool first_block, bool last_block, + uint32_t data_offset, const char *data) +{ + uint32_t lib_id; + int ret; + + assert_can_be_cold(); + + /* Library unload uses a single small payload carrying the 32-bit library ID. */ + if (!first_block || !last_block || data_offset < sizeof(lib_id) || !data) { + LOG_ERR("Invalid library unload params: first %d last %d size %u data %p", + first_block, last_block, data_offset, data); + return IPC4_ERROR_INVALID_PARAM; + } + + /* Ask library manager to unload the library and map errno values to IPC4 status. */ + lib_id = *(const uint32_t *)data; + ret = lib_manager_unload_library(lib_id); + if (!ret) + return IPC4_SUCCESS; + + LOG_ERR("Failed to unload library id %u: %d", lib_id, ret); + + switch (ret) { + case -EBUSY: + return IPC4_BUSY; + case -ENOTSUP: + return IPC4_UNAVAILABLE; + default: + return IPC4_ERROR_INVALID_PARAM; + } +} + __cold static int basefw_set_large_config(struct comp_dev *dev, uint32_t param_id, bool first_block, bool last_block, uint32_t data_offset, const char *data) @@ -998,6 +1040,8 @@ __cold static int basefw_set_large_config(struct comp_dev *dev, uint32_t param_i case IPC4_RESOURCE_ALLOCATION_REQUEST: return basefw_resource_allocation_request(first_block, last_block, data_offset, data); + case IPC4_SOF_LIBRARY_UNLOAD: + return basefw_library_unload(first_block, last_block, data_offset, data); default: break; } From 95b8c357e23e83b7caa52629b94445e7ebfcb716 Mon Sep 17 00:00:00 2001 From: Liam Girdwood Date: Sat, 28 Nov 2026 18:10:43 +0000 Subject: [PATCH 4/9] lib_manager: llext_manager: support LLEXT codec info and library unload Parse the optional ".sof_codec_info" ELF section when loading or DRAM-restoring an LLEXT library and register each codec item 1-by-1 with base_fw. Add lib_manager_unload_library() and llext_manager_remove_library() to reject unsupported library types (-ENOTSUP), free retained LLEXT inspection data before unloading, and unregister the library's codec entries, using Zephyr LOG_* APIs and documenting public helpers with Doxygen. Signed-off-by: Liam Girdwood --- src/include/sof/lib_manager.h | 20 ++ src/kernel/include/sof/kernel/llext_manager.h | 37 ++++ src/kernel/library_manager/lib_manager.c | 51 +++++ src/kernel/library_manager/llext_manager.c | 201 ++++++++++++++++++ .../library_manager/llext_manager_dram.c | 9 + 5 files changed, 318 insertions(+) diff --git a/src/include/sof/lib_manager.h b/src/include/sof/lib_manager.h index 9c05ac340835..72d32a41a21c 100644 --- a/src/include/sof/lib_manager.h +++ b/src/include/sof/lib_manager.h @@ -222,6 +222,26 @@ void lib_manager_get_instance_bss_address(uint32_t instance_id, */ int lib_manager_load_library(uint32_t dma_id, uint32_t lib_id, uint32_t type); +/** + * \brief Unload a previously loaded external library. + * + * \param[in] lib_id Library ID to unload (1..LIB_MANAGER_MAX_LIBS - 1). + * + * Unregisters codec capabilities, unloads any cached LLEXT contexts, and frees + * library storage when no module instance is active. + * + * \return 0 on success, negative error code on failure (-EBUSY if in use). + */ +#ifdef CONFIG_LIBRARY_MANAGER +int lib_manager_unload_library(uint32_t lib_id); +#else +static inline int lib_manager_unload_library(uint32_t lib_id) +{ + (void)lib_id; + return -ENOTSUP; +} +#endif + struct userspace_context; /* * \brief Allocate the module and start the agent if needed diff --git a/src/kernel/include/sof/kernel/llext_manager.h b/src/kernel/include/sof/kernel/llext_manager.h index 525fa50b1506..98c929822cf8 100644 --- a/src/kernel/include/sof/kernel/llext_manager.h +++ b/src/kernel/include/sof/kernel/llext_manager.h @@ -28,8 +28,44 @@ uintptr_t llext_manager_allocate_module(const struct comp_ipc_config *ipc_config int llext_manager_free_module(const uint32_t component_id); +/** + * \brief Add and register an external LLEXT library with the runtime loader. + * + * \param[in] module_id Packed library ID returned by the library manager. + * + * \return 0 on success, negative error code on failure. + */ int llext_manager_add_library(uint32_t module_id); +/** + * \brief Unload an idle LLEXT library and release its virtual memory/metadata. + * + * \param[in] module_id Packed library ID returned by the library manager. + * + * \return 0 on success, -EBUSY if any module or dependent is still active, + * or negative error code on failure. + */ +int llext_manager_remove_library(uint32_t module_id); + +struct lib_manager_mod_ctx; +struct sof_man_fw_desc; + +/** + * \brief Scan loaded LLEXT ELF modules for ".sof_codec_info" and register items. + * + * \param[in] ctx Library module context array for the loaded library. + * \param[in] desc Manifest header describing the library image layout in memory. + */ +void llext_manager_register_lib_codecs(const struct lib_manager_mod_ctx *ctx, + const struct sof_man_fw_desc *desc); + +/** + * \brief Remove all runtime codec capability items owned by an LLEXT library. + * + * \param[in] ctx Library module context array for the library being unloaded. + */ +void llext_manager_unregister_lib_codecs(const struct lib_manager_mod_ctx *ctx); + int llext_manager_add_domain(const uint32_t component_id, struct k_mem_domain *domain); int llext_manager_rm_domain(const uint32_t component_id, struct k_mem_domain *domain); @@ -39,6 +75,7 @@ bool comp_is_llext(struct comp_dev *comp); #define llext_manager_allocate_module(ipc_config, ipc_specific_config) 0 #define llext_manager_free_module(component_id) 0 #define llext_manager_add_library(module_id) 0 +#define llext_manager_remove_library(module_id) 0 #define llext_manager_add_domain(component_id, domain) 0 #define comp_is_llext(comp) false #endif diff --git a/src/kernel/library_manager/lib_manager.c b/src/kernel/library_manager/lib_manager.c index 49a004c5ee23..fa3f1f3f5a54 100644 --- a/src/kernel/library_manager/lib_manager.c +++ b/src/kernel/library_manager/lib_manager.c @@ -1388,3 +1388,54 @@ __cold int lib_manager_load_library(uint32_t dma_id, uint32_t lib_id, uint32_t t return ret; } + +/* Unload an idle LLEXT library, releasing its ELF image and descriptor slot. */ +__cold int lib_manager_unload_library(uint32_t lib_id) +{ + struct ext_library *_ext_lib = ext_lib_get(); + struct lib_manager_mod_ctx *ctx; + uint32_t module_id; + int ret; + + assert_can_be_cold(); + + /* Validate library ID bounds and ensure library table is initialized. */ + if (lib_id == 0 || lib_id >= LIB_MANAGER_MAX_LIBS || !_ext_lib) { + LOG_ERR("Invalid library id %u (ext_lib %p)", lib_id, _ext_lib); + return -EINVAL; + } + + /* Ensure the requested library slot is currently populated. */ + ctx = _ext_lib->desc[lib_id]; + if (!ctx) { + LOG_ERR("Library id %u is not loaded", lib_id); + return -ENOENT; + } + + /* Only LLEXT libraries can be safely unloaded at runtime. */ + module_id = LIB_MANAGER_PACK_LIB_ID(lib_id); + if (ctx->load_type == SOF_MAN_MOD_TYPE_LLEXT || + ctx->load_type == SOF_MAN_MOD_TYPE_LLEXT_AUX) { + ret = llext_manager_remove_library(module_id); + if (ret < 0) { + LOG_ERR("Failed to remove LLEXT library id %u (mod %#x): %d", + lib_id, module_id, ret); + return ret; + } + } else { + LOG_ERR("Unsupported load type %u for unloading library id %u", + ctx->load_type, lib_id); + return -ENOTSUP; + } + + /* Free the cached library binary buffer and descriptor entry. */ + if (ctx->base_addr) + rfree(ctx->base_addr); + + rfree(ctx); + _ext_lib->desc[lib_id] = NULL; + + LOG_INF("unloaded library id: %u", lib_id); + + return 0; +} diff --git a/src/kernel/library_manager/llext_manager.c b/src/kernel/library_manager/llext_manager.c index 67969afeff64..49dd315200a0 100644 --- a/src/kernel/library_manager/llext_manager.c +++ b/src/kernel/library_manager/llext_manager.c @@ -10,6 +10,7 @@ */ #include +#include #include #include #include @@ -1334,6 +1335,151 @@ __cold int llext_manager_free_module(const uint32_t component_id) return llext_manager_unload_module(mctx); } +/* Locate a named non-empty PROGBITS/data section within an in-memory ELF image. */ +__cold static const void *llext_manager_elf_find_section(const uint8_t *elf, size_t elf_size, + const char *name, size_t *sec_size) +{ + const elf_ehdr_t *ehdr; + const elf_shdr_t *shdrs; + const elf_shdr_t *shstr; + const char *strtab; + unsigned int i; + + assert_can_be_cold(); + + /* Validate ELF header magic before inspecting section headers. */ + if (!elf || elf_size < sizeof(*ehdr)) + return NULL; + + ehdr = (const elf_ehdr_t *)elf; + if (ehdr->e_ident[0] != 0x7f || ehdr->e_ident[1] != 'E' || + ehdr->e_ident[2] != 'L' || ehdr->e_ident[3] != 'F') + return NULL; + + /* Bounds-check the section header table against the module ELF buffer. */ + if (ehdr->e_shentsize != sizeof(*shdrs) || !ehdr->e_shnum) + return NULL; + + if (ehdr->e_shoff > elf_size || + ehdr->e_shnum > (elf_size - ehdr->e_shoff) / sizeof(*shdrs)) + return NULL; + + if (ehdr->e_shstrndx >= ehdr->e_shnum) + return NULL; + + /* Bounds-check the section header string table. */ + shdrs = (const elf_shdr_t *)(elf + ehdr->e_shoff); + shstr = &shdrs[ehdr->e_shstrndx]; + if (shstr->sh_offset > elf_size || shstr->sh_size > elf_size - shstr->sh_offset) + return NULL; + + strtab = (const char *)(elf + shstr->sh_offset); + + /* Scan section names safely and return the matching section data pointer. */ + for (i = 0; i < ehdr->e_shnum; i++) { + const char *sec_name; + size_t max_len; + + if (shdrs[i].sh_name >= shstr->sh_size) + continue; + + sec_name = strtab + shdrs[i].sh_name; + max_len = shstr->sh_size - shdrs[i].sh_name; + if (strnlen(sec_name, max_len) == max_len) + continue; + + if (strcmp(sec_name, name) != 0) + continue; + + if (shdrs[i].sh_type == SHT_NOBITS || !shdrs[i].sh_size) + return NULL; + + if (shdrs[i].sh_offset > elf_size || + shdrs[i].sh_size > elf_size - shdrs[i].sh_offset) + return NULL; + + *sec_size = shdrs[i].sh_size; + return elf + shdrs[i].sh_offset; + } + + return NULL; +} + +/* Parse ".sof_codec_info" sections from all modules in a library and register items. */ +__cold void llext_manager_register_lib_codecs(const struct lib_manager_mod_ctx *ctx, + const struct sof_man_fw_desc *desc) +{ + const struct sof_man_module *mod_array; + size_t lib_size; + unsigned int k; + + assert_can_be_cold(); + + if (!ctx || !ctx->mod || !desc) + return; + + /* Compute total manifest image size to guard module ELF offset calculations. */ + mod_array = (const struct sof_man_module *)((const uint8_t *)desc + + SOF_MAN_MODULE_OFFSET(0)); + if (size_mul_overflow(desc->header.preload_page_count, PAGE_SZ, &lib_size)) + return; + + /* Extract and register each module's embedded codec capability table. */ + for (k = 0; k < ctx->n_mod; k++) { + const struct lib_manager_module *mctx = &ctx->mod[k]; + uint32_t entry_index = mctx->start_idx; + uint32_t next_idx = entry_index + mctx->n_mod; + size_t mod_offset; + size_t mod_size; + const void *sec; + size_t sec_size = 0; + int ret; + + if (entry_index >= desc->header.num_module_entries) + continue; + + /* Compute this module's packed ELF span inside the library image. */ + mod_offset = mod_array[entry_index].segment[LIB_MANAGER_TEXT].file_offset; + if (mod_offset >= lib_size) + continue; + + if (next_idx >= desc->header.num_module_entries) + mod_size = lib_size - mod_offset; + else + mod_size = ALIGN_UP(mod_array[next_idx].segment[LIB_MANAGER_TEXT].file_offset - + mod_offset, PAGE_SZ); + + if (mod_size > lib_size - mod_offset) + continue; + + /* Look up ".sof_codec_info" inside this module's ELF image. */ + sec = llext_manager_elf_find_section((const uint8_t *)desc - + SOF_MAN_ELF_TEXT_OFFSET + mod_offset, + mod_size, SOF_CODEC_INFO_SECTION, &sec_size); + if (!sec || sec_size < sizeof(uint32_t) || (sec_size % sizeof(uint32_t)) != 0) + continue; + + /* Register encoded 32-bit codec items keyed by the module context pointer. */ + ret = sof_codec_info_add_items(mctx, sec, sec_size / sizeof(uint32_t)); + if (ret < 0) + LOG_WRN("failed to register codec info for mod %u: %d", k, ret); + } +} + +/* Remove all runtime codec entries belonging to any module in @p ctx. */ +__cold void llext_manager_unregister_lib_codecs(const struct lib_manager_mod_ctx *ctx) +{ + unsigned int k; + + assert_can_be_cold(); + + if (!ctx || !ctx->mod) + return; + + for (k = 0; k < ctx->n_mod; k++) + sof_codec_info_remove(&ctx->mod[k], 0); +} + /* An auxiliary library has been loaded, need to read in its exported symbols */ __cold int llext_manager_add_library(uint32_t module_id) { @@ -1371,6 +1517,61 @@ __cold int llext_manager_add_library(uint32_t module_id) } ctx->load_type = SOF_MAN_MOD_TYPE_LLEXT; + llext_manager_register_lib_codecs(ctx, desc); + + return 0; +} + +/* Unload all idle LLEXT modules in @p module_id and free their metadata. */ +__cold int llext_manager_remove_library(uint32_t module_id) +{ + struct lib_manager_mod_ctx *const ctx = lib_manager_get_mod_ctx(module_id); + unsigned int k; + + assert_can_be_cold(); + + if (!ctx || !ctx->mod) + return -ENOENT; + + /* Refuse unloading if any module is still mapped or referenced by dependents. */ + for (k = 0; k < ctx->n_mod; k++) { + const struct lib_manager_module *mctx = &ctx->mod[k]; + + if (mctx->mapped || mctx->n_dependent || + (mctx->llext && mctx->llext->use_count > 1)) + return -EBUSY; + } + + /* Remove runtime codec capabilities before freeing module descriptors. */ + llext_manager_unregister_lib_codecs(ctx); + + /* Release retained ELF inspection data, LLEXT instances, and loaders. */ + for (k = 0; k < ctx->n_mod; k++) { + struct lib_manager_module *mctx = &ctx->mod[k]; + + if (mctx->ebl && mctx->llext) { + int ret = llext_free_inspection_data(&mctx->ebl->loader, mctx->llext); + + if (ret < 0) + LOG_WRN("llext_free_inspection_data(%u) failed: %d", k, ret); + } + if (mctx->llext) { + int ret = llext_unload(&mctx->llext); + + if (ret < 0) + LOG_WRN("llext_unload(%u) failed: %d", k, ret); + mctx->llext = NULL; + } + if (mctx->ebl) { + rfree(mctx->ebl); + mctx->ebl = NULL; + } + } + + rfree(ctx->mod); + ctx->mod = NULL; + ctx->n_mod = 0; + return 0; } diff --git a/src/kernel/library_manager/llext_manager_dram.c b/src/kernel/library_manager/llext_manager_dram.c index 1b5b9e2b2f49..84e715ee1119 100644 --- a/src/kernel/library_manager/llext_manager_dram.c +++ b/src/kernel/library_manager/llext_manager_dram.c @@ -6,6 +6,7 @@ #include #include +#include #include #include @@ -287,6 +288,12 @@ int llext_manager_restore_from_dram(void) } _ext_lib->desc[i] = ctx; + if (ctx->base_addr) { + const struct sof_man_fw_desc *desc = (const struct sof_man_fw_desc *) + ((const uint8_t *)ctx->base_addr + SOF_MAN_ELF_TEXT_OFFSET); + + llext_manager_register_lib_codecs(ctx, desc); + } } if (lib_manager_dram.n_llext) { @@ -338,6 +345,8 @@ int llext_manager_restore_from_dram(void) struct lib_manager_module *mod = ctx->mod; + llext_manager_unregister_lib_codecs(ctx); + if (!mod) continue; From c3d106d206277f9879f719ba8b22a10c8eb41491 Mon Sep 17 00:00:00 2001 From: Liam Girdwood Date: Sat, 28 Nov 2026 18:10:43 +0000 Subject: [PATCH 5/9] audio: codecs: advertise per-module codec capabilities at init and in LLEXT Move codec capability declarations out of base_fw.c into ffmpeg_dec, mfcc, and cadence_ipc4: embed ".sof_codec_info" tables in LLEXT builds and register built-in codec items 1-by-1 during SOF_MODULE_INIT via sof_codec_info_add_items(). Mark the init wrappers __maybe_unused for host library builds where SOF_MODULE_INIT is a no-op. Signed-off-by: Liam Girdwood --- src/audio/ffmpeg_dec/ffmpeg_dec.c | 76 ++++++++++++++++++- src/audio/mfcc/mfcc.c | 20 ++++- .../module_adapter/module/cadence_ipc4.c | 37 ++++++++- 3 files changed, 130 insertions(+), 3 deletions(-) diff --git a/src/audio/ffmpeg_dec/ffmpeg_dec.c b/src/audio/ffmpeg_dec/ffmpeg_dec.c index d551ffdffadd..6eac55ee34b8 100644 --- a/src/audio/ffmpeg_dec/ffmpeg_dec.c +++ b/src/audio/ffmpeg_dec/ffmpeg_dec.c @@ -9,6 +9,7 @@ // ffmpeg_dec-stub.c and ffmpeg_dec-ffmpeg.c). The input is a compressed audio // elementary stream (raw data), the output is interleaved PCM. +#include #include #include #include @@ -1049,6 +1050,71 @@ static const struct module_interface ffmpeg_dec_interface = { .free = ffmpeg_dec_free }; +static const uint32_t ffmpeg_dec_codec_info_items[] +#if CONFIG_COMP_FFMPEG_DEC_MODULE + __section(SOF_CODEC_INFO_SECTION) __used +#endif + = { +#if defined(CONFIG_FFMPEG_FILTER) + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_PCM, SOF_IPC_STREAM_PLAYBACK), +#endif +#if defined(CONFIG_FFMPEG_DEC_FLAC) || defined(CONFIG_COMP_FFMPEG_DEC_STUB) + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_FLAC, SOF_IPC_STREAM_PLAYBACK), +#endif +#if defined(CONFIG_FFMPEG_DEC_ALAC) || defined(CONFIG_COMP_FFMPEG_DEC_STUB) + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_ALAC, SOF_IPC_STREAM_PLAYBACK), +#endif +#if defined(CONFIG_FFMPEG_DEC_AAC) || defined(CONFIG_COMP_FFMPEG_DEC_STUB) + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_AAC, SOF_IPC_STREAM_PLAYBACK), +#endif +#if defined(CONFIG_FFMPEG_DEC_MP3) || defined(CONFIG_COMP_FFMPEG_DEC_STUB) + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_MP3, SOF_IPC_STREAM_PLAYBACK), +#endif +#if defined(CONFIG_FFMPEG_ENC_MP3) + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_MP3, SOF_IPC_STREAM_CAPTURE), +#endif +#if defined(CONFIG_FFMPEG_ENC_AAC) + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_AAC, SOF_IPC_STREAM_CAPTURE), +#endif +#if defined(CONFIG_FFMPEG_ENC_FLAC) + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_FLAC, SOF_IPC_STREAM_CAPTURE), +#endif +#if defined(CONFIG_FFMPEG_ENC_ALAC) + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_ALAC, SOF_IPC_STREAM_CAPTURE), +#endif +#if defined(CONFIG_FFMPEG_DEC_SBC) || defined(CONFIG_COMP_FFMPEG_DEC_STUB) + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_SBC, SOF_IPC_STREAM_PLAYBACK), +#endif +#if defined(CONFIG_FFMPEG_ENC_SBC) + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_SBC, SOF_IPC_STREAM_CAPTURE), +#endif +#if defined(CONFIG_FFMPEG_DEC_G722) || defined(CONFIG_COMP_FFMPEG_DEC_STUB) + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_G722, SOF_IPC_STREAM_PLAYBACK), +#endif +#if defined(CONFIG_FFMPEG_ENC_G722) + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_G722, SOF_IPC_STREAM_CAPTURE), +#endif +#if defined(CONFIG_FFMPEG_DEC_APTX) || defined(CONFIG_COMP_FFMPEG_DEC_STUB) + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_APTX, SOF_IPC_STREAM_PLAYBACK), +#endif +#if defined(CONFIG_FFMPEG_ENC_APTX) + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_APTX, SOF_IPC_STREAM_CAPTURE), +#endif +#if defined(CONFIG_FFMPEG_ENC_SBC) || defined(CONFIG_FFMPEG_ENC_G722) || \ + defined(CONFIG_FFMPEG_ENC_APTX) + /* Also advertise BESPOKE capture for kernels with older SND_AUDIOCODEC_MAX. */ + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_BESPOKE, SOF_IPC_STREAM_CAPTURE), +#endif +#if defined(CONFIG_FFMPEG_DEC_OPUS) || defined(CONFIG_COMP_FFMPEG_DEC_STUB) + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_OPUS_RAW, SOF_IPC_STREAM_PLAYBACK), +#endif +#if defined(CONFIG_FFMPEG_DEC_OPUS) || defined(CONFIG_FFMPEG_DEC_SBC) || \ + defined(CONFIG_FFMPEG_DEC_G722) || defined(CONFIG_FFMPEG_DEC_APTX) || \ + defined(CONFIG_COMP_FFMPEG_DEC_STUB) + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_BESPOKE, SOF_IPC_STREAM_PLAYBACK), +#endif +}; + /* If COMP_FFMPEG_DEC is =m in Kconfig this is built as a loadable LLEXT module. */ #if CONFIG_COMP_FFMPEG_DEC_MODULE @@ -1067,6 +1133,14 @@ SOF_LLEXT_BUILDINFO; /* Only used for the module adapter trace context, soon to be deprecated */ DECLARE_TR_CTX(ffmpeg_dec_tr, SOF_UUID(ffmpeg_dec_uuid), LOG_LEVEL_INFO); DECLARE_MODULE_ADAPTER(ffmpeg_dec_interface, ffmpeg_dec_uuid, ffmpeg_dec_tr); -SOF_MODULE_INIT(ffmpeg_dec, sys_comp_module_ffmpeg_dec_interface_init); + +static __maybe_unused void sys_comp_ffmpeg_dec_init(void) +{ + sys_comp_module_ffmpeg_dec_interface_init(); + sof_codec_info_add_items(&ffmpeg_dec_interface, ffmpeg_dec_codec_info_items, + ARRAY_SIZE(ffmpeg_dec_codec_info_items)); +} + +SOF_MODULE_INIT(ffmpeg_dec, sys_comp_ffmpeg_dec_init); #endif diff --git a/src/audio/mfcc/mfcc.c b/src/audio/mfcc/mfcc.c index 945c77c6d613..f23630c10251 100644 --- a/src/audio/mfcc/mfcc.c +++ b/src/audio/mfcc/mfcc.c @@ -4,6 +4,7 @@ // // Author: Seppo Ingalsuo +#include #include #include #include @@ -21,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -321,6 +323,14 @@ static const struct module_interface mfcc_interface = { .reset = mfcc_reset, }; +static const uint32_t mfcc_codec_info_items[] +#if CONFIG_COMP_MFCC_MODULE + __section(SOF_CODEC_INFO_SECTION) __used +#endif + = { + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_BESPOKE, SOF_IPC_STREAM_CAPTURE), +}; + #if CONFIG_COMP_MFCC_MODULE /* modular: llext dynamic link */ @@ -338,6 +348,14 @@ SOF_LLEXT_BUILDINFO; /* unused with Zephyr, generates no output */ DECLARE_TR_CTX(mfcc_tr, SOF_UUID(mfcc_uuid), LOG_LEVEL_INFO); DECLARE_MODULE_ADAPTER(mfcc_interface, mfcc_uuid, mfcc_tr); -SOF_MODULE_INIT(mfcc, sys_comp_module_mfcc_interface_init); + +static __maybe_unused void sys_comp_mfcc_init(void) +{ + sys_comp_module_mfcc_interface_init(); + sof_codec_info_add_items(&mfcc_interface, mfcc_codec_info_items, + ARRAY_SIZE(mfcc_codec_info_items)); +} + +SOF_MODULE_INIT(mfcc, sys_comp_mfcc_init); #endif diff --git a/src/audio/module_adapter/module/cadence_ipc4.c b/src/audio/module_adapter/module/cadence_ipc4.c index 3a4489e24dcf..612385c849aa 100644 --- a/src/audio/module_adapter/module/cadence_ipc4.c +++ b/src/audio/module_adapter/module/cadence_ipc4.c @@ -3,6 +3,7 @@ // Copyright(c) 2025-2026 Intel Corporation. All rights reserved. // +#include #include #include #include @@ -644,4 +645,38 @@ static const struct module_interface cadence_codec_interface = { }; DECLARE_MODULE_ADAPTER(cadence_codec_interface, cadence_codec_uuid, cadence_codec_tr); -SOF_MODULE_INIT(cadence_codec, sys_comp_module_cadence_codec_interface_init); + +#if defined(CONFIG_CADENCE_CODEC_AAC_DEC) || defined(CONFIG_CADENCE_CODEC_MP3_DEC) || \ + defined(CONFIG_CADENCE_CODEC_MP3_ENC) || defined(CONFIG_CADENCE_CODEC_VORBIS_DEC) || \ + defined(CONFIG_SOF_COMPRESS_CODEC_PCM_DEC) +static const uint32_t cadence_codec_info_items[] = { +#ifdef CONFIG_CADENCE_CODEC_AAC_DEC + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_AAC, SOF_IPC_STREAM_PLAYBACK), +#endif +#ifdef CONFIG_CADENCE_CODEC_MP3_DEC + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_MP3, SOF_IPC_STREAM_PLAYBACK), +#endif +#ifdef CONFIG_CADENCE_CODEC_MP3_ENC + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_MP3, SOF_IPC_STREAM_CAPTURE), +#endif +#ifdef CONFIG_CADENCE_CODEC_VORBIS_DEC + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_VORBIS, SOF_IPC_STREAM_PLAYBACK), +#endif +#ifdef CONFIG_SOF_COMPRESS_CODEC_PCM_DEC + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_PCM, SOF_IPC_STREAM_PLAYBACK), +#endif +}; +#endif + +static __maybe_unused void sys_comp_cadence_codec_init(void) +{ + sys_comp_module_cadence_codec_interface_init(); +#if defined(CONFIG_CADENCE_CODEC_AAC_DEC) || defined(CONFIG_CADENCE_CODEC_MP3_DEC) || \ + defined(CONFIG_CADENCE_CODEC_MP3_ENC) || defined(CONFIG_CADENCE_CODEC_VORBIS_DEC) || \ + defined(CONFIG_SOF_COMPRESS_CODEC_PCM_DEC) + sof_codec_info_add_items(&cadence_codec_interface, cadence_codec_info_items, + ARRAY_SIZE(cadence_codec_info_items)); +#endif +} + +SOF_MODULE_INIT(cadence_codec, sys_comp_cadence_codec_init); From 4a90bc49ae4afb4d8474802c5b6373a7a5cecb2a Mon Sep 17 00:00:00 2001 From: Liam Girdwood Date: Sat, 28 Nov 2026 18:10:43 +0000 Subject: [PATCH 6/9] zephyr, llext: support standalone FFmpeg LLEXT builds Prepare Zephyr LLEXT build rules for standalone per-codec modules: - Add LINK_OPTIONS support to sof_llext_build() and ensure all LLEXT modules depend on uuid_reg_h when generated headers are enabled. - Derive SOF_BASE in sof_llext_build() from CMAKE_CURRENT_FUNCTION_LIST_DIR/.. so out-of-tree APPLICATION_SOURCE_DIR builds always use SOF's scripts. - Add SOF_LLEXT_RETAIN (__attribute__((__retain__))) on .mod_buildinfo so -ffunction-sections/-fdata-sections and --gc-sections preserve module metadata sections. - Add DEPENDS on uuid-registry.txt for uuid_reg_h regeneration. - Default CONFIG_LLEXT_HEAP_SIZE=32 for relocatable FFmpeg LLEXT metadata. Signed-off-by: Liam Girdwood --- app/Kconfig | 6 ++++++ scripts/cmake/uuid-registry.cmake | 3 ++- src/include/module/module/llext.h | 19 ++++++++++++++++++- zephyr/CMakeLists.txt | 19 +++++++++++++++++-- 4 files changed, 43 insertions(+), 4 deletions(-) diff --git a/app/Kconfig b/app/Kconfig index 339a16bed3e8..ea9ea5fa1da5 100644 --- a/app/Kconfig +++ b/app/Kconfig @@ -1,6 +1,12 @@ config SCHED_CPU_MASK_PIN_ONLY default y if SMP +# Zephyr's LLEXT loader allocates ELF section/symbol metadata from this static +# .llext_heap during llext_load(), independent of the DP module vregion heap +# sized by topology (heap_bytes_requirement). Relocatable FFmpeg LLEXTs need 32 KB. +config LLEXT_HEAP_SIZE + default 32 if COMP_FFMPEG_DEC = m && LLEXT_TYPE_ELF_RELOCATABLE + # Allow compiler to determine whether to generate FLIX instructions. choice COMPILER_CODEGEN_VLIW default COMPILER_CODEGEN_VLIW_AUTO if "$(ZEPHYR_TOOLCHAIN_VARIANT)" = "xt-clang" diff --git a/scripts/cmake/uuid-registry.cmake b/scripts/cmake/uuid-registry.cmake index 0d3b287a7d62..41661e83d2a5 100644 --- a/scripts/cmake/uuid-registry.cmake +++ b/scripts/cmake/uuid-registry.cmake @@ -30,7 +30,8 @@ add_custom_command( COMMAND ${PYTHON_EXECUTABLE} ${TOPDIR}/scripts/gen-uuid-reg.py ${TOPDIR}/uuid-registry.txt - ${UUID_REG_H}) + ${UUID_REG_H} + DEPENDS ${TOPDIR}/uuid-registry.txt) add_custom_target(uuid_reg_h DEPENDS ${UUID_REG_H}) diff --git a/src/include/module/module/llext.h b/src/include/module/module/llext.h index 4f1208f33d5e..7b1ecf7f61dd 100644 --- a/src/include/module/module/llext.h +++ b/src/include/module/module/llext.h @@ -41,8 +41,25 @@ } \ } +/* + * Keep metadata sections such as .module and .mod_buildinfo alive when modules + * are compiled with -ffunction-sections/-fdata-sections and linked with + * --gc-sections, as nothing in the LLEXT references them directly. + */ +#ifndef SOF_LLEXT_RETAIN +#ifdef __has_attribute +#if __has_attribute(__retain__) +#define SOF_LLEXT_RETAIN __attribute__((__retain__)) +#endif +#endif +#ifndef SOF_LLEXT_RETAIN +#define SOF_LLEXT_RETAIN +#endif +#endif + #define SOF_LLEXT_BUILDINFO \ -static const struct sof_module_api_build_info buildinfo __section(".mod_buildinfo") __used = { \ +static const struct sof_module_api_build_info buildinfo __section(".mod_buildinfo") __used \ + SOF_LLEXT_RETAIN = { \ .format = SOF_MODULE_API_BUILD_INFO_FORMAT, \ .api_version_number.full = SOF_MODULE_API_CURRENT_VERSION, \ } diff --git a/zephyr/CMakeLists.txt b/zephyr/CMakeLists.txt index 41db7e5f2b5e..095856795841 100644 --- a/zephyr/CMakeLists.txt +++ b/zephyr/CMakeLists.txt @@ -41,11 +41,12 @@ previously added LLEXT module for the establishment of a dependency chain." # Build an LLEXT module. Provice a module name, a list of sources and an address # of the .text section as arguments. function(sof_llext_build module) - set(multi_args SOURCES INCLUDES CFLAGS LIBS LIBS_PATH) + set(options) + set(multi_args SOURCES INCLUDES CFLAGS LIBS LIBS_PATH LINK_OPTIONS) set(single_args LIB) cmake_parse_arguments(PARSE_ARGV 1 SOF_LLEXT "${options}" "${single_args}" "${multi_args}") - cmake_path(SET SOF_BASE NORMALIZE ${APPLICATION_SOURCE_DIR}/..) + cmake_path(SET SOF_BASE NORMALIZE ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/..) add_llext_target(${module} OUTPUT ${PROJECT_BINARY_DIR}/${module}_llext/${module}.llext @@ -107,6 +108,11 @@ function(sof_llext_build module) set(EXTRA_LINKER_PARAMS ${EXTRA_LINKER_PARAMS} -L${path}) endforeach() + foreach(opt ${SOF_LLEXT_LINK_OPTIONS}) + llext_link_options(${module} ${opt}) + set(EXTRA_LINKER_PARAMS ${EXTRA_LINKER_PARAMS} ${opt}) + endforeach() + get_target_property(proc_in_file ${module} lib_output) get_target_property(proc_out_file ${module} pkg_input) get_target_property(proc_pkg_file ${module} pkg_output) @@ -117,6 +123,7 @@ function(sof_llext_build module) add_dependencies(${module}_llext_proc ${last_target}) endif() set_property(GLOBAL PROPERTY SOF_LLEXT_LAST_TARGET ${module}) + set_property(GLOBAL APPEND PROPERTY SOF_LLEXT_ALL_TARGETS ${module}) add_llext_command(TARGET ${module} POST_BUILD @@ -702,4 +709,12 @@ endif() sof_append_relative_path_definitions(modules_sof) sof_append_relative_path_definitions(sof_kernel) +# Ensure all LLEXT module objects wait for generated uuid-registry.h before compiling. +get_property(_all_llexts GLOBAL PROPERTY SOF_LLEXT_ALL_TARGETS) +if(_all_llexts) + foreach(_mod IN LISTS _all_llexts) + add_dependencies(${_mod}_llext_lib uuid_reg_h) + endforeach() +endif() + endif() # CONFIG_SOF_FULL_ZEPHYR_APPLICATION From 69c13365aad3b19793cf5b126b09fb88e9c8d3b0 Mon Sep 17 00:00:00 2001 From: Liam Girdwood Date: Sat, 28 Nov 2026 18:10:43 +0000 Subject: [PATCH 7/9] audio: ffmpeg_dec: split codecs and filters into standalone LLEXTs Split monolithic ffmpeg_dec LLEXT into 25 per-codec standalone LLEXT modules (12 decoders, 11 encoders, 2 audio filters) with dedicated UUIDs in uuid-registry.txt, each compiled with -fvisibility=hidden, -ffunction-sections, and -fdata-sections and linked against per-module shim registration libraries so unused codecs and parsers are stripped. Signed-off-by: Liam Girdwood --- src/audio/ffmpeg_dec/CMakeLists.txt | 7 +- src/audio/ffmpeg_dec/ffmpeg-encode.c | 47 +++ src/audio/ffmpeg_dec/ffmpeg-filter.c | 71 +++- src/audio/ffmpeg_dec/ffmpeg.cmake | 46 ++- src/audio/ffmpeg_dec/ffmpeg_common.c | 181 +++++++++ src/audio/ffmpeg_dec/ffmpeg_dec-shims.c | 11 +- src/audio/ffmpeg_dec/ffmpeg_dec.c | 279 +++++--------- src/audio/ffmpeg_dec/ffmpeg_dec.h | 11 + src/audio/ffmpeg_dec/ffmpeg_mod.toml.in | 20 + src/audio/ffmpeg_dec/llext/CMakeLists.txt | 434 ++++++++++++++++------ uuid-registry.txt | 25 ++ 11 files changed, 811 insertions(+), 321 deletions(-) create mode 100644 src/audio/ffmpeg_dec/ffmpeg_common.c create mode 100644 src/audio/ffmpeg_dec/ffmpeg_mod.toml.in diff --git a/src/audio/ffmpeg_dec/CMakeLists.txt b/src/audio/ffmpeg_dec/CMakeLists.txt index a598e942d5fa..5130c563e680 100644 --- a/src/audio/ffmpeg_dec/CMakeLists.txt +++ b/src/audio/ffmpeg_dec/CMakeLists.txt @@ -2,9 +2,11 @@ if(CONFIG_COMP_FFMPEG_DEC STREQUAL "m" AND DEFINED CONFIG_LLEXT) add_subdirectory(llext ${PROJECT_BINARY_DIR}/ffmpeg_dec_llext) - add_dependencies(app ffmpeg_dec) + if(FFMPEG_LLEXT_TARGETS) + add_dependencies(app ${FFMPEG_LLEXT_TARGETS}) + endif() else() - add_local_sources(sof ffmpeg_dec.c) + add_local_sources(sof ffmpeg_common.c ffmpeg_dec.c) if(CONFIG_FFMPEG_DEC_FLAC OR CONFIG_COMP_FFMPEG_DEC_STUB) add_local_sources(sof ffmpeg_dec-flac.c) @@ -88,6 +90,7 @@ else() # must target THAT scope via TARGET_DIRECTORY, else CMake silently # ignores it and the glue keeps hardware FP0. set(_ffmpeg_dec_sources + ${CMAKE_CURRENT_SOURCE_DIR}/ffmpeg_common.c ${CMAKE_CURRENT_SOURCE_DIR}/ffmpeg_dec.c ${CMAKE_CURRENT_SOURCE_DIR}/ffmpeg_dec-ffmpeg.c ${CMAKE_CURRENT_SOURCE_DIR}/ffmpeg_dec-convert.c diff --git a/src/audio/ffmpeg_dec/ffmpeg-encode.c b/src/audio/ffmpeg_dec/ffmpeg-encode.c index 495b23734c4f..489d76138e8d 100644 --- a/src/audio/ffmpeg_dec/ffmpeg-encode.c +++ b/src/audio/ffmpeg_dec/ffmpeg-encode.c @@ -11,6 +11,7 @@ // domain, so a DP-scheduled codec (needed to keep the heavy codec init and // per-frame encode off the LL tick) must use the sink/source .process interface. +#include #include #include #include @@ -19,7 +20,11 @@ #include #include "ffmpeg_dec.h" +#ifdef FFMPEG_MOD_ROLE_ENC +LOG_MODULE_REGISTER(ffmpeg_dec, CONFIG_SOF_LOG_LEVEL); +#else LOG_MODULE_DECLARE(ffmpeg_dec, CONFIG_SOF_LOG_LEVEL); +#endif #define FFMPEG_ENC_DEFAULT_BITRATE 128000 @@ -35,6 +40,15 @@ LOG_MODULE_DECLARE(ffmpeg_dec, CONFIG_SOF_LOG_LEVEL); */ #define FFMPEG_ENC_PCM_STAGE 16384 +#ifdef FFMPEG_MOD_ROLE_ENC +#ifndef FFMPEG_MOD_BESPOKE +#define FFMPEG_MOD_BESPOKE 0 +#endif +extern const struct ffmpeg_enc_codec_ops FFMPEG_MOD_CODEC_OPS; +static const struct ffmpeg_enc_codec_ops * const ffmpeg_enc_codecs[] = { + &FFMPEG_MOD_CODEC_OPS, +}; +#else static const struct ffmpeg_enc_codec_ops * const ffmpeg_enc_codecs[] = { #if defined(CONFIG_FFMPEG_ENC_MP3) &ffmpeg_enc_mp3_ops, @@ -70,6 +84,7 @@ static const struct ffmpeg_enc_codec_ops * const ffmpeg_enc_codecs[] = { &ffmpeg_enc_mulaw_ops, #endif }; +#endif /* FFMPEG_MOD_ROLE_ENC */ static const struct ffmpeg_enc_codec_ops * ffmpeg_enc_find_codec_ops_by_snd_id(const struct snd_codec *codec_params) @@ -528,3 +543,35 @@ int ffmpeg_enc_mod_free(struct processing_module *mod) mod->priv.private = NULL; return 0; } + +#ifdef FFMPEG_MOD_ROLE_ENC +#include +#include +#include + +static const struct module_interface ffmpeg_enc_mod_interface = { + .init = ffmpeg_enc_mod_init, + .prepare = ffmpeg_enc_mod_prepare, + .process = ffmpeg_enc_mod_process, + .is_ready_to_process = ffmpeg_enc_is_ready_to_process, + .reset = ffmpeg_enc_mod_reset, + .free = ffmpeg_enc_mod_free, +}; + +static const uint32_t ffmpeg_enc_codec_info_items[] + __section(SOF_CODEC_INFO_SECTION) __used FFMPEG_SECTION_RETAIN = { + SOF_CODEC_INFO_ITEM(FFMPEG_MOD_SND_CODEC_ID, SOF_IPC_STREAM_CAPTURE), +#if FFMPEG_MOD_BESPOKE + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_BESPOKE, SOF_IPC_STREAM_CAPTURE), +#endif +}; + +#define FFMPEG_REG_UUID(sym) SOF_REG_UUID(sym) + +static const struct sof_man_module_manifest mod_manifest + __section(".module") __used FFMPEG_SECTION_RETAIN = + SOF_LLEXT_MODULE_MANIFEST(FFMPEG_MOD_MANIFEST_NAME, &ffmpeg_enc_mod_interface, 1, + FFMPEG_REG_UUID(FFMPEG_MOD_UUID_SYM), 40); + +SOF_LLEXT_BUILDINFO; +#endif /* FFMPEG_MOD_ROLE_ENC */ diff --git a/src/audio/ffmpeg_dec/ffmpeg-filter.c b/src/audio/ffmpeg_dec/ffmpeg-filter.c index 87469ac928b0..e241abcaf252 100644 --- a/src/audio/ffmpeg_dec/ffmpeg-filter.c +++ b/src/audio/ffmpeg_dec/ffmpeg-filter.c @@ -13,6 +13,7 @@ // Built when CONFIG_FFMPEG_FILTER is enabled (selected by any // CONFIG_FFMPEG_FILTER_* option), which pulls libavfilter into the module. +#include #include #include #include @@ -32,7 +33,11 @@ #include #include +#ifdef FFMPEG_MOD_ROLE_AF +LOG_MODULE_REGISTER(ffmpeg_dec, CONFIG_SOF_LOG_LEVEL); +#else LOG_MODULE_DECLARE(ffmpeg_dec, CONFIG_SOF_LOG_LEVEL); +#endif #define FFMPEG_AF_MAX_NAME 32 #define FFMPEG_AF_MAX_ARGS 128 @@ -52,7 +57,8 @@ struct ffmpeg_af_filter_desc { }; static const struct ffmpeg_af_filter_desc ffmpeg_af_filters[] = { -#if defined(CONFIG_FFMPEG_FILTER_AFFTDN) +#if (!defined(FFMPEG_MOD_ROLE_AF) && defined(CONFIG_FFMPEG_FILTER_AFFTDN)) || \ + defined(FFMPEG_AF_SINGLE_AFFTDN) { .id = FFMPEG_AF_FILTER_AFFTDN, .name = "afftdn", @@ -60,7 +66,8 @@ static const struct ffmpeg_af_filter_desc ffmpeg_af_filters[] = { .sample_fmt = AV_SAMPLE_FMT_FLTP, }, #endif -#if defined(CONFIG_FFMPEG_FILTER_ALIMITER) +#if (!defined(FFMPEG_MOD_ROLE_AF) && defined(CONFIG_FFMPEG_FILTER_ALIMITER)) || \ + defined(FFMPEG_AF_SINGLE_ALIMITER) { .id = FFMPEG_AF_FILTER_ALIMITER, .name = "alimiter", @@ -943,3 +950,63 @@ int ffmpeg_af_mod_free(struct processing_module *mod) md->private = NULL; return 0; } + +#ifdef FFMPEG_MOD_ROLE_AF +#include +#include +#include + +__cold static int +ffmpeg_af_set_config_ext(struct processing_module *mod, uint32_t config_id, + enum module_cfg_fragment_position pos, uint32_t data_offset_size, + const uint8_t *fragment, size_t fragment_size, uint8_t *response, + size_t response_size) +{ + struct comp_dev *dev = mod->dev; + struct module_config *config = &mod->priv.cfg; + int ret; + + assert_can_be_cold(); + + ret = module_set_configuration(mod, config_id, pos, data_offset_size, fragment, + fragment_size, response, response_size); + if (ret < 0) { + comp_err(dev, "module_set_configuration failed %d", ret); + return ret; + } + + if (pos != MODULE_CFG_FRAGMENT_LAST && pos != MODULE_CFG_FRAGMENT_SINGLE) + return 0; + + if (!config->size || !config->data) { + comp_warn(dev, "empty filter config"); + return 0; + } + + return ffmpeg_af_mod_set_config(mod, config->data, config->size); +} + +static const struct module_interface ffmpeg_af_mod_interface = { + .init = ffmpeg_af_mod_init, + .prepare = ffmpeg_af_mod_prepare, + .process = ffmpeg_af_mod_process, + .is_ready_to_process = ffmpeg_af_is_ready_to_process, + .set_configuration = ffmpeg_af_set_config_ext, + .reset = ffmpeg_af_mod_reset, + .free = ffmpeg_af_mod_free, +}; + +static const uint32_t ffmpeg_af_codec_info_items[] + __section(SOF_CODEC_INFO_SECTION) __used FFMPEG_SECTION_RETAIN = { + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_PCM, SOF_IPC_STREAM_PLAYBACK), +}; + +#define FFMPEG_REG_UUID(sym) SOF_REG_UUID(sym) + +static const struct sof_man_module_manifest mod_manifest + __section(".module") __used FFMPEG_SECTION_RETAIN = + SOF_LLEXT_MODULE_MANIFEST(FFMPEG_MOD_MANIFEST_NAME, &ffmpeg_af_mod_interface, 1, + FFMPEG_REG_UUID(FFMPEG_MOD_UUID_SYM), 40); + +SOF_LLEXT_BUILDINFO; +#endif /* FFMPEG_MOD_ROLE_AF */ diff --git a/src/audio/ffmpeg_dec/ffmpeg.cmake b/src/audio/ffmpeg_dec/ffmpeg.cmake index f06c95456fde..49f69c89e6c0 100644 --- a/src/audio/ffmpeg_dec/ffmpeg.cmake +++ b/src/audio/ffmpeg_dec/ffmpeg.cmake @@ -133,8 +133,18 @@ if(CONFIG_FFMPEG_BUILD_AVFILTER) endif() # --- 3. Derive the cross toolchain + per-compiler cross flags --- +if(CONFIG_LLEXT_BUILD_PIC OR NOT CONFIG_COMP_FFMPEG_DEC STREQUAL "m") + set(_ff_pic_cflag "-fPIC") + set(_ff_pic_cfg "--enable-pic") +else() + set(_ff_pic_cflag "-fno-pic -fno-pie") + set(_ff_pic_cfg "--disable-pic") +endif() # GCC 14 (Zephyr SDK) promotes these to errors; FFmpeg 7.x trips them. -set(_ff_extra_cflags "-fPIC -mlongcalls -mtext-section-literals -Wno-error=incompatible-pointer-types -Wno-error=implicit-function-declaration") +set(_ff_extra_cflags "${_ff_pic_cflag} -mlongcalls -mtext-section-literals -Wno-error=incompatible-pointer-types -Wno-error=implicit-function-declaration") +if(NOT CONFIG_FFMPEG_DEC_COLD_SPLIT) + set(_ff_extra_cflags "${_ff_extra_cflags} -ffunction-sections -fdata-sections -fvisibility=hidden") +endif() # SOF decodes AAC-LC only. Tell the AAC decoder to skip the per-element SBR/PS # storage (~437 KiB of a ~554 KiB ExtChannelElement); LC never exercises SBR, @@ -487,13 +497,13 @@ export PATH=${_tc_dir}:\$PATH CC=\"${_ff_cc}\" mkdir -p ${CMAKE_CURRENT_BINARY_DIR}/shine-obj for f in ${SHINE_SRC}/src/lib/*.c; do - \$CC -O2 -fPIC -mlongcalls -mtext-section-literals -DSHINE_HAVE_BSWAP_H \\ + \$CC -O2 ${_ff_pic_cflag} -ffunction-sections -fdata-sections -fvisibility=hidden -mlongcalls -mtext-section-literals -DSHINE_HAVE_BSWAP_H \\ -I${SHINE_SRC}/src/lib -c \"\$f\" \\ -o ${CMAKE_CURRENT_BINARY_DIR}/shine-obj/\$(basename \"\$f\").o done ${_ff_cross_prefix}ar rcs ${SHINE_INSTALL}/lib/libshine.a ${CMAKE_CURRENT_BINARY_DIR}/shine-obj/*.o cp ${SHINE_SRC}/src/lib/layer3.h ${SHINE_INSTALL}/include/shine/layer3.h -\$CC -fPIC -c ${CMAKE_CURRENT_BINARY_DIR}/shine_cfgstub.c \\ +\$CC ${_ff_pic_cflag} -c ${CMAKE_CURRENT_BINARY_DIR}/shine_cfgstub.c \\ -o ${CMAKE_CURRENT_BINARY_DIR}/shine_cfgstub.o ") add_custom_command( @@ -583,7 +593,7 @@ export PATH=${_tc_dir}:\$PATH CC=\"${_ff_cc}\" mkdir -p ${CMAKE_CURRENT_BINARY_DIR}/vo-aacenc-obj for f in ${_vo_aacenc_srclist}; do - \$CC -O2 -DNDEBUG -fPIC -mlongcalls -mtext-section-literals -D__unused= \\ + \$CC -O2 -DNDEBUG ${_ff_pic_cflag} -ffunction-sections -fdata-sections -fvisibility=hidden -mlongcalls -mtext-section-literals -D__unused= \\ -I${VO_AACENC_SRC}/aacenc/inc \\ -I${VO_AACENC_SRC}/aacenc/basic_op \\ -I${VO_AACENC_SRC}/common/include \\ @@ -606,17 +616,40 @@ cp ${VO_AACENC_SRC}/common/include/*.h ${VO_AACENC_INSTALL}/include/vo-aacenc/ endif() # --- 4. Configure + make + install (out-of-tree; source must be clean) --- +set(FFMPEG_EXT_BUILD_DIR "${CMAKE_CURRENT_BINARY_DIR}/ffmpeg-build" CACHE INTERNAL "ffmpeg_ext build dir") +set(FFMPEG_CC_FF "${_ff_cc_ff}" CACHE INTERNAL "ffmpeg C compiler command") +set(FFMPEG_EXTRA_CFLAGS "${_ff_extra_cflags}" CACHE INTERNAL "ffmpeg extra cflags") +set(FFMPEG_CROSS_PREFIX "${_ff_cross_prefix}" CACHE INTERNAL "ffmpeg cross prefix") + set(_ff_byproducts ${FFMPEG_INSTALL_DIR}/lib/libavcodec.a + ${FFMPEG_INSTALL_DIR}/lib/libavcodec_noco.a ${FFMPEG_INSTALL_DIR}/lib/libavutil.a ${FFMPEG_INSTALL_DIR}/lib/libswresample.a) +set(_ff_noco_cmds + COMMAND ${CMAKE_COMMAND} -E copy + ${FFMPEG_INSTALL_DIR}/lib/libavcodec.a + ${FFMPEG_INSTALL_DIR}/lib/libavcodec_noco.a + COMMAND ${_ff_cross_prefix}ar d + ${FFMPEG_INSTALL_DIR}/lib/libavcodec_noco.a + allcodecs.o parsers.o) if(CONFIG_FFMPEG_BUILD_AVFILTER) - list(APPEND _ff_byproducts ${FFMPEG_INSTALL_DIR}/lib/libavfilter.a) + list(APPEND _ff_byproducts + ${FFMPEG_INSTALL_DIR}/lib/libavfilter.a + ${FFMPEG_INSTALL_DIR}/lib/libavfilter_noco.a) + list(APPEND _ff_noco_cmds + COMMAND ${CMAKE_COMMAND} -E copy + ${FFMPEG_INSTALL_DIR}/lib/libavfilter.a + ${FFMPEG_INSTALL_DIR}/lib/libavfilter_noco.a + COMMAND ${_ff_cross_prefix}ar d + ${FFMPEG_INSTALL_DIR}/lib/libavfilter_noco.a + allfilters.o) endif() ExternalProject_Add(ffmpeg_ext DEPENDS ${_ff_shine_dep} ${_ff_vo_aacenc_dep} SOURCE_DIR "${SOF_FFMPEG_SRC_DIR}" + BINARY_DIR "${FFMPEG_EXT_BUILD_DIR}" BUILD_IN_SOURCE 0 CONFIGURE_COMMAND ${CMAKE_COMMAND} -E env ${_ff_cfg_env} @@ -639,13 +672,14 @@ ExternalProject_Add(ffmpeg_ext ${_ff_dec_cfg} ${_ff_enc_cfg} ${_ff_shine_cfg} - --enable-small --enable-pic --enable-hardcoded-tables + --enable-small ${_ff_pic_cfg} --enable-hardcoded-tables "--extra-cflags=${_ff_extra_cflags}" ${_ff_flacenc_prep} BUILD_COMMAND ${CMAKE_COMMAND} -E env "PATH=${_tc_dir}:$ENV{PATH}" make -j 8 INSTALL_COMMAND make install ${_ff_cold_rename} + ${_ff_noco_cmds} BUILD_BYPRODUCTS ${_ff_byproducts} ) diff --git a/src/audio/ffmpeg_dec/ffmpeg_common.c b/src/audio/ffmpeg_dec/ffmpeg_common.c new file mode 100644 index 000000000000..669c1cd16fb1 --- /dev/null +++ b/src/audio/ffmpeg_dec/ffmpeg_common.c @@ -0,0 +1,181 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2026 Intel Corporation. +// +// Shared buffer, extradata, ID3v2, and IPC4 EOS helpers across FFmpeg +// decoder, encoder, and audio-filter modules. + +#include +#include +#include +#include +#include +#include +#include "ffmpeg_dec.h" + +#if CONFIG_IPC_MAJOR_4 +#include +#include +#include +#include +#endif + +LOG_MODULE_DECLARE(ffmpeg_dec, CONFIG_SOF_LOG_LEVEL); + +static inline size_t ffmpeg_common_min(size_t a, size_t b) +{ + return a < b ? a : b; +} + +#if CONFIG_IPC_MAJOR_4 +__cold struct ipc_msg *ffmpeg_dec_eos_notification_init(struct processing_module *mod) +{ + struct comp_ipc_config *ipc_config = &mod->dev->ipc_config; + struct sof_ipc4_notify_module_data *msg_data; + union ipc4_notification_header primary; + struct ipc_msg *msg; + + primary.dat = 0; + primary.r.notif_type = SOF_IPC4_MODULE_NOTIFICATION; + primary.r.type = SOF_IPC4_GLB_NOTIFICATION; + primary.r.rsp = SOF_IPC4_MESSAGE_DIR_MSG_REQUEST; + primary.r.msg_tgt = SOF_IPC4_MESSAGE_TARGET_FW_GEN_MSG; + + msg = mod_ipc_msg_w_ext_init(mod, primary.dat, 0, sizeof(*msg_data)); + if (!msg) + return NULL; + + msg_data = (struct sof_ipc4_notify_module_data *)msg->tx_data; + msg_data->instance_id = IPC4_INST_ID(ipc_config->id); + msg_data->module_id = IPC4_MOD_ID(ipc_config->id); + msg_data->event_id = SOF_IPC4_NOTIFY_MODULE_EVENTID_COMPR_MAGIC_VAL; + msg_data->event_data_size = 0; + + return msg; +} + +void ffmpeg_dec_signal_eos(struct processing_module *mod, struct sof_sink *sink) +{ + struct ffmpeg_dec_comp_data *cd = module_get_private_data(mod); + + if (cd->eos_sent || !cd->eos_msg) + return; + + ipc_msg_send(cd->eos_msg, NULL, false); + cd->eos_sent = true; + audio_buffer_set_eos(sof_audio_buffer_from_sink(sink)); + comp_info(mod->dev, "EOS: compress drain complete"); +} +#endif /* CONFIG_IPC_MAJOR_4 */ + +int ffmpeg_dec_store_extradata(struct processing_module *mod, + const uint8_t *data, size_t size) +{ + struct ffmpeg_dec_comp_data *cd = module_get_private_data(mod); + struct comp_dev *dev = mod->dev; + + if (!size) + return 0; + + if (size > FFMPEG_DEC_MAX_EXTRADATA) { + comp_err(dev, "extradata too large: %zu", size); + return -EINVAL; + } + + memcpy_s(cd->extradata, sizeof(cd->extradata), data, size); + cd->extradata_size = size; + comp_info(dev, "stored %zu bytes of codec extradata", size); + return 0; +} + +void ffmpeg_dec_copy_from_circular(void *dst, const void *src, + const void *buf_start, size_t buf_size, + size_t bytes) +{ + size_t to_end = (size_t)((const uint8_t *)buf_start + buf_size - + (const uint8_t *)src); + + if (to_end >= bytes) { + memcpy_s(dst, bytes, src, bytes); + return; + } + memcpy_s(dst, to_end, src, to_end); + memcpy_s((uint8_t *)dst + to_end, bytes - to_end, + buf_start, bytes - to_end); +} + +void ffmpeg_dec_copy_to_circular(void *dst, void *buf_start, + size_t buf_size, const void *src, + size_t bytes) +{ + size_t to_end = (size_t)((uint8_t *)buf_start + buf_size - + (uint8_t *)dst); + + if (to_end >= bytes) { + memcpy_s(dst, bytes, src, bytes); + return; + } + memcpy_s(dst, to_end, src, to_end); + memcpy_s(buf_start, bytes - to_end, + (const uint8_t *)src + to_end, bytes - to_end); +} + +int ffmpeg_dec_skip_id3v2(struct processing_module *mod, struct sof_source *src) +{ + struct ffmpeg_dec_comp_data *cd = module_get_private_data(mod); + struct comp_dev *dev = mod->dev; + const void *sp, *sstart; + size_t sbytes, avail, req; + int ret; + + /* Drain any pending skip bytes from a previous chunk */ + if (cd->skip_bytes > 0) { + avail = source_get_data_available(src); + if (!avail) + return -ENODATA; + size_t to_skip = ffmpeg_common_min(avail, cd->skip_bytes); + + source_drop_data(src, to_skip); + cd->skip_bytes -= to_skip; + if (cd->skip_bytes > 0) + return -ENODATA; + } + + avail = source_get_data_available(src); + if (avail < 4) + return -ENODATA; + + req = ffmpeg_common_min(avail, cd->in_buf_size); + ret = source_get_data(src, req, &sp, &sstart, &sbytes); + if (ret) + return ret; + ffmpeg_dec_copy_from_circular(cd->in_buf, sp, sstart, sbytes, req); + source_release_data(src, 0); + + /* ID3v2 tag: 10-byte header with 28-bit syncsafe length */ + if (memcmp(cd->in_buf, "ID3", 3) == 0) { + uint32_t tag; + size_t total_tag; + + if (req < 10) + return -ENODATA; + + tag = ((uint32_t)(cd->in_buf[6] & 0x7f) << 21) | + ((uint32_t)(cd->in_buf[7] & 0x7f) << 14) | + ((uint32_t)(cd->in_buf[8] & 0x7f) << 7) | + (uint32_t)(cd->in_buf[9] & 0x7f); + total_tag = 10 + tag + ((cd->in_buf[5] & 0x10) ? 10 : 0); + comp_info(dev, "ID3v2 tag detected: %zu bytes", total_tag); + + if (avail >= total_tag) { + source_drop_data(src, total_tag); + return 1; + } + + source_drop_data(src, avail); + cd->skip_bytes = total_tag - avail; + return -ENODATA; + } + + return 0; +} diff --git a/src/audio/ffmpeg_dec/ffmpeg_dec-shims.c b/src/audio/ffmpeg_dec/ffmpeg_dec-shims.c index 11770a2f031c..426ebe6099ef 100644 --- a/src/audio/ffmpeg_dec/ffmpeg_dec-shims.c +++ b/src/audio/ffmpeg_dec/ffmpeg_dec-shims.c @@ -405,11 +405,18 @@ unsigned long strtoul(const char *nptr, char **endptr, int base) /* * Double-precision libm is provided by the real newlib math archive * (libnewlib_m.a, see llext/CMakeLists.txt). newlib math functions report - * domain/range errors through *__errno(). + * domain/range errors through *__errno(), and picolibc inline + * accesses *z_errno_wrap() from libavutil objects. */ +static int llext_errno; + int *__errno(void) { - static int llext_errno; + return &llext_errno; +} + +int *z_errno_wrap(void) +{ return &llext_errno; } diff --git a/src/audio/ffmpeg_dec/ffmpeg_dec.c b/src/audio/ffmpeg_dec/ffmpeg_dec.c index 6eac55ee34b8..074db501398e 100644 --- a/src/audio/ffmpeg_dec/ffmpeg_dec.c +++ b/src/audio/ffmpeg_dec/ffmpeg_dec.c @@ -27,195 +27,35 @@ #include #endif +#if !CONFIG_COMP_FFMPEG_DEC_MODULE /* UUID identifies the component. Registered in uuid-registry.txt. */ SOF_DEFINE_REG_UUID(ffmpeg_dec); +#endif /* Creates logging data for the component */ LOG_MODULE_REGISTER(ffmpeg_dec, CONFIG_SOF_LOG_LEVEL); -#if CONFIG_IPC_MAJOR_4 -/* - * Build the module notification the driver expects to unblock compress_drain(). - * The kernel's compress EOS handler (sof_ipc4_compr_drain_done) matches on the - * COMPR "magic" event id, so use the same template the Cadence decoder does. - */ -__cold struct ipc_msg *ffmpeg_dec_eos_notification_init(struct processing_module *mod) -{ - struct comp_ipc_config *ipc_config = &mod->dev->ipc_config; - struct sof_ipc4_notify_module_data *msg_data; - union ipc4_notification_header primary; - struct ipc_msg *msg; - - primary.dat = 0; - primary.r.notif_type = SOF_IPC4_MODULE_NOTIFICATION; - primary.r.type = SOF_IPC4_GLB_NOTIFICATION; - primary.r.rsp = SOF_IPC4_MESSAGE_DIR_MSG_REQUEST; - primary.r.msg_tgt = SOF_IPC4_MESSAGE_TARGET_FW_GEN_MSG; - - msg = mod_ipc_msg_w_ext_init(mod, primary.dat, 0, sizeof(*msg_data)); - if (!msg) - return NULL; - - msg_data = (struct sof_ipc4_notify_module_data *)msg->tx_data; - msg_data->instance_id = IPC4_INST_ID(ipc_config->id); - msg_data->module_id = IPC4_MOD_ID(ipc_config->id); - msg_data->event_id = SOF_IPC4_NOTIFY_MODULE_EVENTID_COMPR_MAGIC_VAL; - msg_data->event_data_size = 0; - - return msg; -} - -/* - * Report that the compress drain is complete: send the EOS notification (which - * unblocks the host's compress_drain()) exactly once, and mark the sink so the - * end-of-stream state propagates downstream to the DAI. - */ -void ffmpeg_dec_signal_eos(struct processing_module *mod, struct sof_sink *sink) -{ - struct ffmpeg_dec_comp_data *cd = module_get_private_data(mod); - - if (cd->eos_sent || !cd->eos_msg) - return; - - ipc_msg_send(cd->eos_msg, NULL, false); - cd->eos_sent = true; - audio_buffer_set_eos(sof_audio_buffer_from_sink(sink)); - comp_info(mod->dev, "EOS: compress drain complete"); -} -#endif /* CONFIG_IPC_MAJOR_4 */ - -/* Decoder-mode ops (compressed -> PCM). */ -int ffmpeg_dec_store_extradata(struct processing_module *mod, - const uint8_t *data, size_t size) -{ - struct ffmpeg_dec_comp_data *cd = module_get_private_data(mod); - struct comp_dev *dev = mod->dev; - - if (!size) - return 0; - - if (size > FFMPEG_DEC_MAX_EXTRADATA) { - comp_err(dev, "extradata too large: %zu", size); - return -EINVAL; - } - - memcpy_s(cd->extradata, sizeof(cd->extradata), data, size); - cd->extradata_size = size; - comp_info(dev, "stored %zu bytes of codec extradata", size); - return 0; -} +#if defined(CONFIG_FFMPEG_ENC) && !defined(FFMPEG_MOD_ROLE_DEC) +#define FFMPEG_DEC_ENABLE_ENC 1 +#endif +#if defined(CONFIG_FFMPEG_FILTER) && !defined(FFMPEG_MOD_ROLE_DEC) +#define FFMPEG_DEC_ENABLE_FILTER 1 +#endif static inline size_t ffmpeg_dec_min(size_t a, size_t b) { return a < b ? a : b; } -/* Copy @bytes out of a circular source buffer (starting at @src) into the - * linear @dst, wrapping at the buffer end if necessary. - */ -void ffmpeg_dec_copy_from_circular(void *dst, const void *src, - const void *buf_start, size_t buf_size, - size_t bytes) -{ - size_t to_end = (size_t)((const uint8_t *)buf_start + buf_size - - (const uint8_t *)src); - - if (to_end >= bytes) { - memcpy_s(dst, bytes, src, bytes); - return; - } - memcpy_s(dst, to_end, src, to_end); - memcpy_s((uint8_t *)dst + to_end, bytes - to_end, - buf_start, bytes - to_end); -} - -/* Copy @bytes from the linear @src into a circular sink buffer (starting at - * @dst), wrapping at the buffer end if necessary. - */ -void ffmpeg_dec_copy_to_circular(void *dst, void *buf_start, - size_t buf_size, const void *src, - size_t bytes) -{ - size_t to_end = (size_t)((uint8_t *)buf_start + buf_size - - (uint8_t *)dst); - - if (to_end >= bytes) { - memcpy_s(dst, bytes, src, bytes); - return; - } - memcpy_s(dst, to_end, src, to_end); - memcpy_s(buf_start, bytes - to_end, - (const uint8_t *)src + to_end, bytes - to_end); -} - -/* - * Peek at the start of the stream and skip a leading ID3v2 tag if present. - * Returns: - * 1 if an ID3v2 tag was completely skipped (caller should re-check), - * 0 if the stream does not start with an ID3v2 tag (cd->in_buf holds the - * peeked header bytes), - * -ENODATA while waiting for enough bytes to inspect or finish skipping. - */ -int ffmpeg_dec_skip_id3v2(struct processing_module *mod, struct sof_source *src) -{ - struct ffmpeg_dec_comp_data *cd = module_get_private_data(mod); - struct comp_dev *dev = mod->dev; - const void *sp, *sstart; - size_t sbytes, avail, req; - int ret; - - /* Drain any pending skip bytes from a previous chunk */ - if (cd->skip_bytes > 0) { - avail = source_get_data_available(src); - if (!avail) - return -ENODATA; - size_t to_skip = ffmpeg_dec_min(avail, cd->skip_bytes); - - source_drop_data(src, to_skip); - cd->skip_bytes -= to_skip; - if (cd->skip_bytes > 0) - return -ENODATA; - } - - avail = source_get_data_available(src); - if (avail < 4) - return -ENODATA; - - req = ffmpeg_dec_min(avail, cd->in_buf_size); - ret = source_get_data(src, req, &sp, &sstart, &sbytes); - if (ret) - return ret; - ffmpeg_dec_copy_from_circular(cd->in_buf, sp, sstart, sbytes, req); - source_release_data(src, 0); - - /* ID3v2 tag: 10-byte header with 28-bit syncsafe length */ - if (memcmp(cd->in_buf, "ID3", 3) == 0) { - uint32_t tag; - size_t total_tag; - - if (req < 10) - return -ENODATA; - - tag = ((uint32_t)(cd->in_buf[6] & 0x7f) << 21) | - ((uint32_t)(cd->in_buf[7] & 0x7f) << 14) | - ((uint32_t)(cd->in_buf[8] & 0x7f) << 7) | - (uint32_t)(cd->in_buf[9] & 0x7f); - total_tag = 10 + tag + ((cd->in_buf[5] & 0x10) ? 10 : 0); - comp_info(dev, "ID3v2 tag detected: %zu bytes", total_tag); - - if (avail >= total_tag) { - source_drop_data(src, total_tag); - return 1; - } - - source_drop_data(src, avail); - cd->skip_bytes = total_tag - avail; - return -ENODATA; - } - - return 0; -} - +#ifdef FFMPEG_MOD_ROLE_DEC +#ifndef FFMPEG_MOD_BESPOKE +#define FFMPEG_MOD_BESPOKE 0 +#endif +extern const struct ffmpeg_dec_codec_ops FFMPEG_MOD_CODEC_OPS; +static const struct ffmpeg_dec_codec_ops * const ffmpeg_dec_codecs[] = { + &FFMPEG_MOD_CODEC_OPS, +}; +#else static const struct ffmpeg_dec_codec_ops * const ffmpeg_dec_codecs[] = { #if defined(CONFIG_FFMPEG_DEC_FLAC) || defined(CONFIG_COMP_FFMPEG_DEC_STUB) &ffmpeg_dec_flac_ops, @@ -263,6 +103,7 @@ static const struct ffmpeg_dec_codec_ops *ffmpeg_dec_find_codec_ops(enum ffmpeg_ } return NULL; } +#endif /* FFMPEG_MOD_ROLE_DEC */ #if CONFIG_IPC_MAJOR_4 static const struct ffmpeg_dec_codec_ops * @@ -333,7 +174,7 @@ __cold static int ffmpeg_dec_init(struct processing_module *mod) } if (direction == SOF_IPC_STREAM_CAPTURE) { -#if defined(CONFIG_FFMPEG_ENC) +#ifdef FFMPEG_DEC_ENABLE_ENC return ffmpeg_enc_mod_init(mod); #else comp_err(dev, "capture (encode) requested but no encoders enabled"); @@ -345,14 +186,14 @@ __cold static int ffmpeg_dec_init(struct processing_module *mod) return -EINVAL; } if (codec_params->id == SND_AUDIOCODEC_PCM) { -#if defined(CONFIG_FFMPEG_FILTER) +#ifdef FFMPEG_DEC_ENABLE_FILTER return ffmpeg_af_mod_init(mod); #else comp_err(dev, "PCM filter requested but no filters enabled"); return -ENOTSUP; #endif } -#if defined(CONFIG_FFMPEG_FILTER) +#ifdef FFMPEG_DEC_ENABLE_FILTER } else { return ffmpeg_af_mod_init(mod); #endif @@ -365,8 +206,13 @@ __cold static int ffmpeg_dec_init(struct processing_module *mod) md->private = cd; cd->backend = &ffmpeg_dec_backend; +#ifdef FFMPEG_MOD_ROLE_DEC + cd->codec_ops = &FFMPEG_MOD_CODEC_OPS; + cd->codec = cd->codec_ops->id; +#else cd->codec = FFMPEG_DEC_DEFAULT_CODEC; cd->codec_ops = ffmpeg_dec_find_codec_ops(cd->codec); +#endif #if CONFIG_IPC_MAJOR_4 if (mod->priv.cfg.ext_data && @@ -481,12 +327,12 @@ static int ffmpeg_dec_prepare(struct processing_module *mod, comp_dbg(dev, "entry"); -#if defined(CONFIG_FFMPEG_ENC) +#ifdef FFMPEG_DEC_ENABLE_ENC if (cd && cd->is_encoder) return ffmpeg_enc_mod_prepare(mod, sources, num_of_sources, sinks, num_of_sinks); #endif -#if defined(CONFIG_FFMPEG_FILTER) +#ifdef FFMPEG_DEC_ENABLE_FILTER if (cd && cd->is_filter) return ffmpeg_af_mod_prepare(mod, sources, num_of_sources, sinks, num_of_sinks); @@ -614,12 +460,12 @@ static bool ffmpeg_dec_is_ready_to_process(struct processing_module *mod, { struct ffmpeg_dec_comp_data *cd = module_get_private_data(mod); -#if defined(CONFIG_FFMPEG_ENC) +#ifdef FFMPEG_DEC_ENABLE_ENC if (cd && cd->is_encoder) return ffmpeg_enc_is_ready_to_process(mod, sources, num_of_sources, sinks, num_of_sinks); #endif -#if defined(CONFIG_FFMPEG_FILTER) +#ifdef FFMPEG_DEC_ENABLE_FILTER if (cd && cd->is_filter) return ffmpeg_af_is_ready_to_process(mod, sources, num_of_sources, sinks, num_of_sinks); @@ -687,12 +533,12 @@ static int ffmpeg_dec_process(struct processing_module *mod, size_t consumed = 0, produced = 0; int ret; -#if defined(CONFIG_FFMPEG_ENC) +#ifdef FFMPEG_DEC_ENABLE_ENC if (cd && cd->is_encoder) return ffmpeg_enc_mod_process(mod, sources, num_of_sources, sinks, num_of_sinks); #endif -#if defined(CONFIG_FFMPEG_FILTER) +#ifdef FFMPEG_DEC_ENABLE_FILTER if (cd && cd->is_filter) return ffmpeg_af_mod_process(mod, sources, num_of_sources, sinks, num_of_sinks); @@ -944,7 +790,7 @@ ffmpeg_dec_set_config(struct processing_module *mod, uint32_t config_id, return 0; } -#if defined(CONFIG_FFMPEG_FILTER) +#ifdef FFMPEG_DEC_ENABLE_FILTER { struct ffmpeg_dec_comp_data *cd = module_get_private_data(mod); @@ -970,11 +816,11 @@ static int ffmpeg_dec_reset(struct processing_module *mod) comp_dbg(mod->dev, "entry"); -#if defined(CONFIG_FFMPEG_ENC) +#ifdef FFMPEG_DEC_ENABLE_ENC if (cd && cd->is_encoder) return ffmpeg_enc_mod_reset(mod); #endif -#if defined(CONFIG_FFMPEG_FILTER) +#ifdef FFMPEG_DEC_ENABLE_FILTER if (cd && cd->is_filter) return ffmpeg_af_mod_reset(mod); #endif @@ -1013,11 +859,11 @@ __cold static int ffmpeg_dec_free(struct processing_module *mod) assert_can_be_cold(); comp_dbg(mod->dev, "entry"); -#if defined(CONFIG_FFMPEG_ENC) +#ifdef FFMPEG_DEC_ENABLE_ENC if (cd && cd->is_encoder) return ffmpeg_enc_mod_free(mod); #endif -#if defined(CONFIG_FFMPEG_FILTER) +#ifdef FFMPEG_DEC_ENABLE_FILTER if (cd && cd->is_filter) return ffmpeg_af_mod_free(mod); #endif @@ -1052,9 +898,16 @@ static const struct module_interface ffmpeg_dec_interface = { static const uint32_t ffmpeg_dec_codec_info_items[] #if CONFIG_COMP_FFMPEG_DEC_MODULE - __section(SOF_CODEC_INFO_SECTION) __used + __section(SOF_CODEC_INFO_SECTION) __used FFMPEG_SECTION_RETAIN #endif = { +#ifdef FFMPEG_MOD_ROLE_DEC + SOF_CODEC_INFO_ITEM(FFMPEG_MOD_SND_CODEC_ID, SOF_IPC_STREAM_PLAYBACK), +#if FFMPEG_MOD_BESPOKE + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_BESPOKE, SOF_IPC_STREAM_PLAYBACK), +#endif +#else +/* Built-in monolithic / stub fallback when not built as standalone per-codec LLEXT. */ #if defined(CONFIG_FFMPEG_FILTER) SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_PCM, SOF_IPC_STREAM_PLAYBACK), #endif @@ -1100,8 +953,34 @@ static const uint32_t ffmpeg_dec_codec_info_items[] #if defined(CONFIG_FFMPEG_ENC_APTX) SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_APTX, SOF_IPC_STREAM_CAPTURE), #endif +#if defined(CONFIG_FFMPEG_DEC_APTX_HD) || defined(CONFIG_COMP_FFMPEG_DEC_STUB) + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_APTX_HD, SOF_IPC_STREAM_PLAYBACK), +#endif +#if defined(CONFIG_FFMPEG_ENC_APTX_HD) + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_APTX_HD, SOF_IPC_STREAM_CAPTURE), +#endif +#if defined(CONFIG_FFMPEG_DEC_G726) || defined(CONFIG_COMP_FFMPEG_DEC_STUB) + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_G726, SOF_IPC_STREAM_PLAYBACK), +#endif +#if defined(CONFIG_FFMPEG_ENC_G726) + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_G726, SOF_IPC_STREAM_CAPTURE), +#endif +#if defined(CONFIG_FFMPEG_DEC_ALAW) || defined(CONFIG_COMP_FFMPEG_DEC_STUB) + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_ALAW, SOF_IPC_STREAM_PLAYBACK), +#endif +#if defined(CONFIG_FFMPEG_ENC_ALAW) + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_ALAW, SOF_IPC_STREAM_CAPTURE), +#endif +#if defined(CONFIG_FFMPEG_DEC_MULAW) || defined(CONFIG_COMP_FFMPEG_DEC_STUB) + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_MULAW, SOF_IPC_STREAM_PLAYBACK), +#endif +#if defined(CONFIG_FFMPEG_ENC_MULAW) + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_MULAW, SOF_IPC_STREAM_CAPTURE), +#endif #if defined(CONFIG_FFMPEG_ENC_SBC) || defined(CONFIG_FFMPEG_ENC_G722) || \ - defined(CONFIG_FFMPEG_ENC_APTX) + defined(CONFIG_FFMPEG_ENC_APTX) || defined(CONFIG_FFMPEG_ENC_APTX_HD) || \ + defined(CONFIG_FFMPEG_ENC_G726) || defined(CONFIG_FFMPEG_ENC_ALAW) || \ + defined(CONFIG_FFMPEG_ENC_MULAW) /* Also advertise BESPOKE capture for kernels with older SND_AUDIOCODEC_MAX. */ SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_BESPOKE, SOF_IPC_STREAM_CAPTURE), #endif @@ -1110,9 +989,12 @@ static const uint32_t ffmpeg_dec_codec_info_items[] #endif #if defined(CONFIG_FFMPEG_DEC_OPUS) || defined(CONFIG_FFMPEG_DEC_SBC) || \ defined(CONFIG_FFMPEG_DEC_G722) || defined(CONFIG_FFMPEG_DEC_APTX) || \ + defined(CONFIG_FFMPEG_DEC_APTX_HD) || defined(CONFIG_FFMPEG_DEC_G726) || \ + defined(CONFIG_FFMPEG_DEC_ALAW) || defined(CONFIG_FFMPEG_DEC_MULAW) || \ defined(CONFIG_COMP_FFMPEG_DEC_STUB) SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_BESPOKE, SOF_IPC_STREAM_PLAYBACK), #endif +#endif /* FFMPEG_MOD_ROLE_DEC */ }; /* If COMP_FFMPEG_DEC is =m in Kconfig this is built as a loadable LLEXT module. */ @@ -1122,9 +1004,18 @@ static const uint32_t ffmpeg_dec_codec_info_items[] #include #include -static const struct sof_man_module_manifest mod_manifest __section(".module") __used = +#ifdef FFMPEG_MOD_ROLE_DEC +#define FFMPEG_REG_UUID(sym) SOF_REG_UUID(sym) +static const struct sof_man_module_manifest mod_manifest + __section(".module") __used FFMPEG_SECTION_RETAIN = + SOF_LLEXT_MODULE_MANIFEST(FFMPEG_MOD_MANIFEST_NAME, &ffmpeg_dec_interface, 1, + FFMPEG_REG_UUID(FFMPEG_MOD_UUID_SYM), 40); +#else +static const struct sof_man_module_manifest mod_manifest + __section(".module") __used FFMPEG_SECTION_RETAIN = SOF_LLEXT_MODULE_MANIFEST("FFMPGDEC", &ffmpeg_dec_interface, 1, SOF_REG_UUID(ffmpeg_dec), 40); +#endif SOF_LLEXT_BUILDINFO; diff --git a/src/audio/ffmpeg_dec/ffmpeg_dec.h b/src/audio/ffmpeg_dec/ffmpeg_dec.h index 6864f71fff05..8f9c570a557b 100644 --- a/src/audio/ffmpeg_dec/ffmpeg_dec.h +++ b/src/audio/ffmpeg_dec/ffmpeg_dec.h @@ -420,4 +420,15 @@ void ffmpeg_dec_libc_bind(struct processing_module *mod); size_t ffmpeg_dec_heap_free(void); size_t ffmpeg_dec_heap_lastfail(void); /*DBG*/ +#ifndef FFMPEG_SECTION_RETAIN +#ifdef __has_attribute +#if __has_attribute(__retain__) +#define FFMPEG_SECTION_RETAIN __attribute__((__retain__)) +#endif +#endif +#ifndef FFMPEG_SECTION_RETAIN +#define FFMPEG_SECTION_RETAIN +#endif +#endif + #endif /* __SOF_AUDIO_FFMPEG_DEC_H__ */ diff --git a/src/audio/ffmpeg_dec/ffmpeg_mod.toml.in b/src/audio/ffmpeg_dec/ffmpeg_mod.toml.in new file mode 100644 index 000000000000..7bc0dfe8663b --- /dev/null +++ b/src/audio/ffmpeg_dec/ffmpeg_mod.toml.in @@ -0,0 +1,20 @@ +#include + + REM # FFmpeg individual audio module config (@FFMPEG_MOD_NAME@) + [[module.entry]] + name = "@FFMPEG_MOD_MANIFEST_NAME@" + uuid = @FFMPEG_MOD_UUID_MACRO@ + affinity_mask = "0x1" + instance_count = "40" + domain_types = "0" + load_type = "2" + module_type = "9" + auto_start = "0" + sched_caps = [1, 0x00008000] + pin = [0, 0, 0xfeef, 0xf, 0xf, 0x45ff, 1, 0, 0xfeef, 0xf, 0xf, 0x1ff] + mod_cfg = [0, 0, 0, 0, 8192, 5000000, @FFMPEG_MOD_IBS@, @FFMPEG_MOD_OBS@, 0, 5000, 0] + + index = __COUNTER__ + +[module] +count = __COUNTER__ diff --git a/src/audio/ffmpeg_dec/llext/CMakeLists.txt b/src/audio/ffmpeg_dec/llext/CMakeLists.txt index 8d43c1ea8db6..3f7814ff216b 100644 --- a/src/audio/ffmpeg_dec/llext/CMakeLists.txt +++ b/src/audio/ffmpeg_dec/llext/CMakeLists.txt @@ -2,9 +2,10 @@ # SPDX-License-Identifier: Apache-2.0 if(CONFIG_COMP_FFMPEG_DEC_STUB) - # Dependency-free build: SOF glue + passthrough stub backend, no libavcodec. + # Dependency-free build: SOF glue + passthrough stub backend, standalone LLEXT. sof_llext_build("ffmpeg_dec" - SOURCES ../ffmpeg_dec.c + SOURCES ../ffmpeg_common.c + ../ffmpeg_dec.c ../ffmpeg_dec-flac.c ../ffmpeg_dec-mp3.c ../ffmpeg_dec-aac.c @@ -16,145 +17,348 @@ if(CONFIG_COMP_FFMPEG_DEC_STUB) ../ffmpeg_dec-g726.c ../ffmpeg_dec-g711.c ../ffmpeg_dec-stub.c - LIB openmodules ) + set(FFMPEG_LLEXT_TARGETS ffmpeg_dec PARENT_SCOPE) + return() +endif() + +# Real backends: cross-build FFmpeg, libshine, vo-aacenc, and newlib_m once, +# then link each enabled decoder, encoder, and filter as its own standalone +# .llext module (no LIB openmodules packing) with per-module registration tables +# so unused codecs are never pulled into the module. +set(_ff_root_dir "${CMAKE_CURRENT_LIST_DIR}/..") +set(_ff_llext_dir "${CMAKE_CURRENT_LIST_DIR}") +include("${_ff_root_dir}/ffmpeg.cmake") + +set(_ff_common_cflags "") +if(CONFIG_FFMPEG_DEC_COLD_SPLIT) + list(APPEND _ff_common_cflags -mlongcalls) else() - # Real decoder: cross-build the FFmpeg archive (Kconfig-selected decoders) - # and link it. libc/libm are NOT bundled (non-PIC, and would clash) — the - # module provides the needed libc symbols itself (shims/alloc), resolves the - # rest from the SOF core, and supplies fast single-precision float math - # (fastmathf.c) when a float codec (AAC/Opus) is selected. - include(${CMAKE_CURRENT_LIST_DIR}/../ffmpeg.cmake) + list(APPEND _ff_common_cflags -ffunction-sections -fdata-sections -fvisibility=hidden) +endif() +# Disable scalar hardware FP0 on Clang targets (CONFIG_FPU=n) so float +# conversions and strtod/strtof emit soft-float libcalls instead of +# faulting FP0 instructions. +if(CMAKE_C_COMPILER_ID MATCHES "Clang") + list(APPEND _ff_common_cflags "SHELL:-Xclang -target-feature -Xclang -fp") +endif() - set(ffmpeg_dec_srcs - ../ffmpeg_dec.c - ../ffmpeg_dec-ffmpeg.c - ../ffmpeg_dec-convert.c - ../ffmpeg-convert-hifi.c - ../ffmpeg_dec-shims.c - ../ffmpeg_dec-alloc.c) - if(CONFIG_FFMPEG_DEC_FLAC) - list(APPEND ffmpeg_dec_srcs ../ffmpeg_dec-flac.c) - endif() - if(CONFIG_FFMPEG_DEC_MP3) - list(APPEND ffmpeg_dec_srcs ../ffmpeg_dec-mp3.c) - endif() - if(CONFIG_FFMPEG_DEC_AAC) - list(APPEND ffmpeg_dec_srcs ../ffmpeg_dec-aac.c) - endif() - if(CONFIG_FFMPEG_DEC_OPUS) - list(APPEND ffmpeg_dec_srcs ../ffmpeg_dec-opus.c) - endif() - if(CONFIG_FFMPEG_DEC_ALAC) - list(APPEND ffmpeg_dec_srcs ../ffmpeg_dec-alac.c) - endif() - if(CONFIG_FFMPEG_DEC_SBC) - list(APPEND ffmpeg_dec_srcs ../ffmpeg_dec-sbc.c) - endif() - if(CONFIG_FFMPEG_DEC_G722) - list(APPEND ffmpeg_dec_srcs ../ffmpeg_dec-g722.c) - endif() - if(CONFIG_FFMPEG_DEC_APTX OR CONFIG_FFMPEG_DEC_APTX_HD) - list(APPEND ffmpeg_dec_srcs ../ffmpeg_dec-aptx.c) - endif() - if(CONFIG_FFMPEG_DEC_G726) - list(APPEND ffmpeg_dec_srcs ../ffmpeg_dec-g726.c) - endif() - if(CONFIG_FFMPEG_DEC_ALAW OR CONFIG_FFMPEG_DEC_MULAW) - list(APPEND ffmpeg_dec_srcs ../ffmpeg_dec-g711.c) +get_filename_component(_ff_libm_dir "${FFMPEG_TARGET_LIBM}" DIRECTORY) + +set(_ff_all_targets "") + +# Helper: generate per-module config header, codec/parser/filter registration +# lists + archive, llext.toml.h and CMakeLists.txt, and register sof_llext_build(). +function(_ffmpeg_add_llext_module mod_name) + set(options USE_FASTMATH USE_AVFILTER USE_SHINE USE_VOAACENC) + set(one_val ROLE MANIFEST_NAME UUID_SYM IBS OBS CODEC_OPS SND_ID BESPOKE AF_DEFINE) + set(multi_val SOURCES CODEC_SYMS PARSER_SYMS FILTER_SYMS EXTRA_DEPS) + cmake_parse_arguments(ARG "${options}" "${one_val}" "${multi_val}" ${ARGN}) + + set(_mod_src_dir "${CMAKE_CURRENT_BINARY_DIR}/mods/${mod_name}") + file(MAKE_DIRECTORY "${_mod_src_dir}/libavcodec" "${_mod_src_dir}/libavfilter") + + # 1. Generate TOML header from template + string(TOUPPER "${ARG_UUID_SYM}" _uuid_upper) + set(FFMPEG_MOD_NAME "${mod_name}") + set(FFMPEG_MOD_MANIFEST_NAME "${ARG_MANIFEST_NAME}") + set(FFMPEG_MOD_UUID_MACRO "UUIDREG_STR_${_uuid_upper}") + set(FFMPEG_MOD_IBS "${ARG_IBS}") + set(FFMPEG_MOD_OBS "${ARG_OBS}") + configure_file("${_ff_root_dir}/ffmpeg_mod.toml.in" + "${_mod_src_dir}/llext.toml.h" @ONLY) + + # 2. Generate per-module macro header (included via -imacros to avoid shell quoting) + set(_cfg_h "#ifndef _FFMPEG_MOD_CFG_H_\n#define _FFMPEG_MOD_CFG_H_\n") + if(ARG_ROLE STREQUAL "dec") + set(_cfg_h "${_cfg_h}#define FFMPEG_MOD_ROLE_DEC 1\n") + set(_cfg_h "${_cfg_h}#define FFMPEG_MOD_CODEC_OPS ${ARG_CODEC_OPS}\n") + set(_cfg_h "${_cfg_h}#define FFMPEG_MOD_SND_CODEC_ID ${ARG_SND_ID}\n") + set(_cfg_h "${_cfg_h}#define FFMPEG_MOD_BESPOKE ${ARG_BESPOKE}\n") + elseif(ARG_ROLE STREQUAL "enc") + set(_cfg_h "${_cfg_h}#define FFMPEG_MOD_ROLE_ENC 1\n") + set(_cfg_h "${_cfg_h}#define FFMPEG_MOD_CODEC_OPS ${ARG_CODEC_OPS}\n") + set(_cfg_h "${_cfg_h}#define FFMPEG_MOD_SND_CODEC_ID ${ARG_SND_ID}\n") + set(_cfg_h "${_cfg_h}#define FFMPEG_MOD_BESPOKE ${ARG_BESPOKE}\n") + elseif(ARG_ROLE STREQUAL "af") + set(_cfg_h "${_cfg_h}#define FFMPEG_MOD_ROLE_AF 1\n") + set(_cfg_h "${_cfg_h}#define ${ARG_AF_DEFINE} 1\n") endif() - # fastmathf provides the single-precision libm the codecs need AND sofm_log2f - # used by the shims' log10(); always built for the real (non-stub) backend. - list(APPEND ffmpeg_dec_srcs ../fastmathf.c) + set(_cfg_h "${_cfg_h}#define FFMPEG_MOD_MANIFEST_NAME \"${ARG_MANIFEST_NAME}\"\n") + set(_cfg_h "${_cfg_h}#define FFMPEG_MOD_UUID_SYM ${ARG_UUID_SYM}\n") + set(_cfg_h "${_cfg_h}#endif\n") + file(WRITE "${_mod_src_dir}/ffmpeg_mod_cfg.h" "${_cfg_h}") - # avfilter graph backend (PCM effects, e.g. afftdn noise reduction). Pulls - # libavfilter into the link; it must precede the libs it depends on. - set(ffmpeg_dec_libs avcodec swresample avutil) - set(ffmpeg_dec_libpaths "${FFMPEG_INSTALL_DIR}/lib") - if(CONFIG_FFMPEG_FILTER) - list(APPEND ffmpeg_dec_srcs ../ffmpeg-filter.c) + # 3. Generate per-module registration lists and static archive (lib_reg.a) + set(_reg_lib "") + set(_reg_dep "") + set(_need_reg FALSE) + set(_reg_cmds "") + set(_reg_objs "") + + if(ARG_CODEC_SYMS OR ARG_PARSER_SYMS) + set(_need_reg TRUE) + set(_c_decls "") + set(_c_list "") + foreach(_cs IN LISTS ARG_CODEC_SYMS) + set(_c_decls "${_c_decls}extern const FFCodec ${_cs};\n") + set(_c_list "${_c_list} &${_cs},\n") + endforeach() + set(_p_decls "") + set(_p_list "") + foreach(_ps IN LISTS ARG_PARSER_SYMS) + set(_p_decls "${_p_decls}extern const AVCodecParser ${_ps};\n") + set(_p_list "${_p_list} &${_ps},\n") + endforeach() + file(WRITE "${_mod_src_dir}/libavcodec/codec_list.c" + "${_c_decls}static const FFCodec * const codec_list[] = {\n${_c_list} NULL };\n") + file(WRITE "${_mod_src_dir}/libavcodec/parser_list.c" + "${_p_decls}static const AVCodecParser * const parser_list[] = {\n${_p_list} NULL };\n") + set(_reg_cmds "${_reg_cmds} +\$CC \$CFLAGS -c \"${SOF_FFMPEG_SRC_DIR}/libavcodec/allcodecs.c\" -o \"${_mod_src_dir}/allcodecs.o\" +\$CC \$CFLAGS -c \"${SOF_FFMPEG_SRC_DIR}/libavcodec/parsers.c\" -o \"${_mod_src_dir}/parsers.o\"") + set(_reg_objs "${_reg_objs} \"${_mod_src_dir}/allcodecs.o\" \"${_mod_src_dir}/parsers.o\"") endif() - if(CONFIG_FFMPEG_BUILD_AVFILTER) - set(ffmpeg_dec_libs avfilter ${ffmpeg_dec_libs}) + + if(ARG_USE_AVFILTER) + set(_need_reg TRUE) + set(_f_decls "") + set(_f_list "") + foreach(_fs IN LISTS ARG_FILTER_SYMS) + set(_f_decls "${_f_decls}extern const AVFilter ${_fs};\n") + set(_f_list "${_f_list} &${_fs},\n") + endforeach() + file(WRITE "${_mod_src_dir}/libavfilter/filter_list.c" + "${_f_decls}static const AVFilter * const filter_list[] = {\n${_f_list} NULL };\n") + set(_reg_cmds "${_reg_cmds} +\$CC \$CFLAGS -c \"${SOF_FFMPEG_SRC_DIR}/libavfilter/allfilters.c\" -o \"${_mod_src_dir}/allfilters.o\"") + set(_reg_objs "${_reg_objs} \"${_mod_src_dir}/allfilters.o\"") endif() - # Encode support: PCM->compressed backend + libshine / vo-aacenc archives - # (linked AFTER libavcodec, which references libshine). - if(CONFIG_FFMPEG_ENC) - list(APPEND ffmpeg_dec_srcs ../ffmpeg-encode.c) + if(_need_reg) + set(_reg_archive "${_mod_src_dir}/lib${mod_name}_reg.a") + file(WRITE "${_mod_src_dir}/build_reg.sh" +"#!/bin/sh +set -e +CC=\"${FFMPEG_CC_FF}\" +CFLAGS=\"-Os ${FFMPEG_EXTRA_CFLAGS} -DHAVE_AV_CONFIG_H -I${_mod_src_dir} -I${FFMPEG_EXT_BUILD_DIR} -I${SOF_FFMPEG_SRC_DIR}\" +${_reg_cmds} +rm -f \"${_reg_archive}\" +${FFMPEG_CROSS_PREFIX}ar rcs \"${_reg_archive}\" ${_reg_objs} +") + add_custom_command( + OUTPUT "${_reg_archive}" + COMMAND sh "${_mod_src_dir}/build_reg.sh" + DEPENDS ffmpeg_ext "${_mod_src_dir}/build_reg.sh" + VERBATIM) + add_custom_target(${mod_name}_reg DEPENDS "${_reg_archive}") + set(_reg_lib "${mod_name}_reg") + set(_reg_dep "${mod_name}_reg") endif() - if(CONFIG_FFMPEG_ENC_MP3) - list(APPEND ffmpeg_dec_srcs ../ffmpeg-enc-mp3.c) - list(APPEND ffmpeg_dec_libs shine) - list(APPEND ffmpeg_dec_libpaths "${SHINE_INSTALL}/lib") + + # 4. Build SOURCES, INCLUDES, LIBS, LIBS_PATH, CFLAGS lists + set(_srcs + "${_ff_root_dir}/ffmpeg_common.c" + "${_ff_root_dir}/ffmpeg_dec-convert.c" + "${_ff_root_dir}/ffmpeg-convert-hifi.c" + "${_ff_root_dir}/ffmpeg_dec-shims.c" + "${_ff_root_dir}/ffmpeg_dec-alloc.c" + ${ARG_SOURCES}) + if(ARG_USE_FASTMATH) + list(APPEND _srcs "${_ff_root_dir}/fastmathf.c") endif() - if(CONFIG_FFMPEG_ENC_AAC) - list(APPEND ffmpeg_dec_srcs ../ffmpeg-enc-aac.c) - list(APPEND ffmpeg_dec_libs vo-aacenc) - list(APPEND ffmpeg_dec_libpaths "${VO_AACENC_INSTALL}/lib") + + set(_incs "${FFMPEG_INSTALL_DIR}/include" "${_ff_root_dir}") + set(_libpaths "${_mod_src_dir}" "${FFMPEG_INSTALL_DIR}/lib") + set(_libs "") + + if(_reg_lib) + list(APPEND _libs "${_reg_lib}") endif() - if(CONFIG_FFMPEG_ENC_FLAC) - list(APPEND ffmpeg_dec_srcs ../ffmpeg-enc-flac.c) + if(ARG_USE_AVFILTER) + list(APPEND _libs avfilter_noco) endif() - if(CONFIG_FFMPEG_ENC_ALAC) - list(APPEND ffmpeg_dec_srcs ../ffmpeg-enc-alac.c) + if(ARG_CODEC_SYMS OR ARG_PARSER_SYMS) + list(APPEND _libs avcodec_noco) endif() - if(CONFIG_FFMPEG_ENC_SBC) - list(APPEND ffmpeg_dec_srcs ../ffmpeg-enc-sbc.c) + # Repeat _reg_lib (and avcodec_noco when parsers are present) because + # archive members in avcodec_noco (parser.o -> av_parser_iterate) + # pull parsers.o, which in turn references ff_*_parser in avcodec_noco. + if(_reg_lib) + list(APPEND _libs "${_reg_lib}") endif() - if(CONFIG_FFMPEG_ENC_G722) - list(APPEND ffmpeg_dec_srcs ../ffmpeg-enc-g722.c) + if(ARG_PARSER_SYMS) + list(APPEND _libs avcodec_noco) endif() - if(CONFIG_FFMPEG_ENC_APTX OR CONFIG_FFMPEG_ENC_APTX_HD) - list(APPEND ffmpeg_dec_srcs ../ffmpeg-enc-aptx.c) + if(ARG_USE_SHINE) + list(APPEND _libs shine) + list(APPEND _libpaths "${SHINE_INSTALL}/lib") endif() - if(CONFIG_FFMPEG_ENC_G726) - list(APPEND ffmpeg_dec_srcs ../ffmpeg-enc-g726.c) + if(ARG_USE_VOAACENC) + list(APPEND _libs vo-aacenc) + list(APPEND _incs "${VO_AACENC_INSTALL}/include") + list(APPEND _libpaths "${VO_AACENC_INSTALL}/lib") endif() - if(CONFIG_FFMPEG_ENC_ALAW OR CONFIG_FFMPEG_ENC_MULAW) - list(APPEND ffmpeg_dec_srcs ../ffmpeg-enc-g711.c) + if(ARG_ROLE STREQUAL "dec" OR ARG_USE_AVFILTER) + list(APPEND _libs swresample) endif() + list(APPEND _libs avutil newlib_m) + list(APPEND _libpaths "${_ff_libm_dir}") - # Real libm (newlib double math) LAST so libav*/filter references resolve against it - get_filename_component(_ff_libm_dir "${FFMPEG_TARGET_LIBM}" DIRECTORY) - list(APPEND ffmpeg_dec_libpaths "${_ff_libm_dir}") - list(APPEND ffmpeg_dec_libs newlib_m) + set(_cflags ${_ff_common_cflags} "SHELL:-imacros ${_mod_src_dir}/ffmpeg_mod_cfg.h") + set(_deps ffmpeg_ext newlib_m ${_reg_dep} ${ARG_EXTRA_DEPS}) - # Cold split: module code must also use -mlongcalls to reach FFmpeg code - # that now lives in the far (DRAM) .cold region. - set(ffmpeg_dec_cflags "") + set(_srcs_str "") + foreach(_s IN LISTS _srcs) + set(_srcs_str "${_srcs_str}\n\t\"${_s}\"") + endforeach() + set(_incs_str "") + foreach(_i IN LISTS _incs) + set(_incs_str "${_incs_str}\n\t\"${_i}\"") + endforeach() + set(_libpaths_str "") + foreach(_p IN LISTS _libpaths) + set(_libpaths_str "${_libpaths_str}\n\t\"${_p}\"") + endforeach() + set(_libs_str "") + foreach(_l IN LISTS _libs) + set(_libs_str "${_libs_str} ${_l}") + endforeach() + set(_cflags_str "") + foreach(_c IN LISTS _cflags) + set(_cflags_str "${_cflags_str} \"${_c}\"") + endforeach() + set(_deps_str "") + foreach(_d IN LISTS _deps) + set(_deps_str "${_deps_str} ${_d}") + endforeach() + + set(_link_opts_list "") + if(NOT CONFIG_LLEXT_TYPE_ELF_RELOCATABLE) + list(APPEND _link_opts_list "\"-Wl,--exclude-libs,ALL\"" "\"-Wl,--gc-sections\"") + endif() if(CONFIG_FFMPEG_DEC_COLD_SPLIT) - list(APPEND ffmpeg_dec_cflags -mlongcalls) + list(APPEND _link_opts_list "\"-Wl,-T,${_ff_llext_dir}/ffmpeg_cold.ld\"") endif() - # Disable scalar hardware FP0 on Clang targets (CONFIG_FPU=n) so float - # conversions and strtod/strtof emit soft-float libcalls instead of - # faulting FP0 instructions. - if(CMAKE_C_COMPILER_ID MATCHES "Clang") - list(APPEND ffmpeg_dec_cflags "SHELL:-Xclang -target-feature -Xclang -fp") + set(_link_opts_str "") + if(_link_opts_list) + string(JOIN " " _link_opts_joined ${_link_opts_list}) + set(_link_opts_str "\n\tLINK_OPTIONS ${_link_opts_joined}") endif() - set(ffmpeg_dec_includes "${FFMPEG_INSTALL_DIR}/include") - if(CONFIG_FFMPEG_ENC_AAC) - list(APPEND ffmpeg_dec_includes "${VO_AACENC_INSTALL}/include") - endif() + file(WRITE "${_mod_src_dir}/CMakeLists.txt" +"# Auto-generated per-module LLEXT build for ${mod_name} +sof_llext_build(\"${mod_name}\" + SOURCES ${_srcs_str} + INCLUDES ${_incs_str} + LIBS_PATH ${_libpaths_str} + LIBS ${_libs_str} + CFLAGS ${_cflags_str}${_link_opts_str} +) +add_dependencies(${mod_name}_llext_lib ${_deps_str}) +add_dependencies(${mod_name} ${_deps_str}) +") - sof_llext_build("ffmpeg_dec" - SOURCES ${ffmpeg_dec_srcs} - INCLUDES ${ffmpeg_dec_includes} - LIBS_PATH ${ffmpeg_dec_libpaths} - LIBS ${ffmpeg_dec_libs} - CFLAGS ${ffmpeg_dec_cflags} - LIB openmodules - ) + add_subdirectory("${_mod_src_dir}" "${PROJECT_BINARY_DIR}/${mod_name}_llext") + list(APPEND _ff_all_targets "${mod_name}") + set(_ff_all_targets "${_ff_all_targets}" PARENT_SCOPE) +endfunction() - # The archives must be built before the module compiles/links against them. - add_dependencies(ffmpeg_dec_llext_lib ffmpeg_ext newlib_m ${_ff_shine_dep} ${_ff_vo_aacenc_dep}) - add_dependencies(ffmpeg_dec ffmpeg_ext newlib_m ${_ff_shine_dep} ${_ff_vo_aacenc_dep}) +# --- Decoders (IBS=4096, OBS=16384) --- +macro(_ffmpeg_add_decoder kconf mod_name manifest_name src_file ops_sym snd_id bespoke ff_dec_syms ff_par_syms use_fastmath) + if(${kconf}) + set(_fm_flag "") + if(${use_fastmath}) + set(_fm_flag USE_FASTMATH) + endif() + _ffmpeg_add_llext_module("${mod_name}" + ROLE dec + MANIFEST_NAME "${manifest_name}" + UUID_SYM "${mod_name}" + IBS 4096 OBS 16384 + CODEC_OPS "${ops_sym}" + SND_ID "${snd_id}" + BESPOKE "${bespoke}" + SOURCES "${_ff_root_dir}/ffmpeg_dec.c" + "${_ff_root_dir}/ffmpeg_dec-ffmpeg.c" + "${_ff_root_dir}/${src_file}" + CODEC_SYMS ${ff_dec_syms} + PARSER_SYMS ${ff_par_syms} + ${_fm_flag} + ) + endif() +endmacro() - # Cold split: place the (renamed) FFmpeg .cold code so the loader relocates - # it to DRAM. See ffmpeg_cold.ld for why INSERT AFTER .bss. - if(CONFIG_FFMPEG_DEC_COLD_SPLIT) - llext_link_options(ffmpeg_dec - "-Wl,-T,${CMAKE_CURRENT_LIST_DIR}/ffmpeg_cold.ld") +_ffmpeg_add_decoder(CONFIG_FFMPEG_DEC_FLAC ffmpeg_dec_flac FFDECFLC ffmpeg_dec-flac.c ffmpeg_dec_flac_ops SND_AUDIOCODEC_FLAC 0 "ff_flac_decoder" "ff_flac_parser" FALSE) +_ffmpeg_add_decoder(CONFIG_FFMPEG_DEC_AAC ffmpeg_dec_aac FFDECAAC ffmpeg_dec-aac.c ffmpeg_dec_aac_ops SND_AUDIOCODEC_AAC 0 "ff_aac_fixed_decoder" "ff_aac_parser;ff_aac_latm_parser" TRUE) +_ffmpeg_add_decoder(CONFIG_FFMPEG_DEC_OPUS ffmpeg_dec_opus FFDECOPS ffmpeg_dec-opus.c ffmpeg_dec_opus_ops SND_AUDIOCODEC_OPUS_RAW 1 "ff_opus_decoder" "ff_opus_parser" TRUE) +_ffmpeg_add_decoder(CONFIG_FFMPEG_DEC_MP3 ffmpeg_dec_mp3 FFDECMP3 ffmpeg_dec-mp3.c ffmpeg_dec_mp3_ops SND_AUDIOCODEC_MP3 0 "ff_mp3_decoder" "ff_mpegaudio_parser" TRUE) +_ffmpeg_add_decoder(CONFIG_FFMPEG_DEC_ALAC ffmpeg_dec_alac FFDECALC ffmpeg_dec-alac.c ffmpeg_dec_alac_ops SND_AUDIOCODEC_ALAC 0 "ff_alac_decoder" "" FALSE) +_ffmpeg_add_decoder(CONFIG_FFMPEG_DEC_SBC ffmpeg_dec_sbc FFDECSBC ffmpeg_dec-sbc.c ffmpeg_dec_sbc_ops SND_AUDIOCODEC_SBC 1 "ff_sbc_decoder" "ff_sbc_parser" FALSE) +_ffmpeg_add_decoder(CONFIG_FFMPEG_DEC_G722 ffmpeg_dec_g722 FFDEC722 ffmpeg_dec-g722.c ffmpeg_dec_g722_ops SND_AUDIOCODEC_G722 1 "ff_adpcm_g722_decoder" "" FALSE) +_ffmpeg_add_decoder(CONFIG_FFMPEG_DEC_APTX ffmpeg_dec_aptx FFDECAPX ffmpeg_dec-aptx.c ffmpeg_dec_aptx_ops SND_AUDIOCODEC_APTX 1 "ff_aptx_decoder" "" FALSE) +_ffmpeg_add_decoder(CONFIG_FFMPEG_DEC_APTX_HD ffmpeg_dec_aptx_hd FFDECAHD ffmpeg_dec-aptx.c ffmpeg_dec_aptx_hd_ops SND_AUDIOCODEC_APTX_HD 1 "ff_aptx_hd_decoder" "" FALSE) +_ffmpeg_add_decoder(CONFIG_FFMPEG_DEC_G726 ffmpeg_dec_g726 FFDEC726 ffmpeg_dec-g726.c ffmpeg_dec_g726_ops SND_AUDIOCODEC_G726 1 "ff_adpcm_g726_decoder;ff_adpcm_g726le_decoder" "" FALSE) +_ffmpeg_add_decoder(CONFIG_FFMPEG_DEC_ALAW ffmpeg_dec_alaw FFDECALW ffmpeg_dec-g711.c ffmpeg_dec_alaw_ops SND_AUDIOCODEC_ALAW 1 "ff_pcm_alaw_decoder" "" FALSE) +_ffmpeg_add_decoder(CONFIG_FFMPEG_DEC_MULAW ffmpeg_dec_mulaw FFDECULW ffmpeg_dec-g711.c ffmpeg_dec_mulaw_ops SND_AUDIOCODEC_MULAW 1 "ff_pcm_mulaw_decoder" "" FALSE) + +# --- Encoders (IBS=16384, OBS=4096) --- +macro(_ffmpeg_add_encoder kconf mod_name manifest_name src_file ops_sym snd_id bespoke ff_enc_syms) + if(${kconf}) + _ffmpeg_add_llext_module("${mod_name}" + ROLE enc + MANIFEST_NAME "${manifest_name}" + UUID_SYM "${mod_name}" + IBS 16384 OBS 4096 + CODEC_OPS "${ops_sym}" + SND_ID "${snd_id}" + BESPOKE "${bespoke}" + SOURCES "${_ff_root_dir}/ffmpeg-encode.c" + "${_ff_root_dir}/${src_file}" + CODEC_SYMS ${ff_enc_syms} + ${ARGN} + ) endif() +endmacro() + +_ffmpeg_add_encoder(CONFIG_FFMPEG_ENC_MP3 ffmpeg_enc_mp3 FFENCMP3 ffmpeg-enc-mp3.c ffmpeg_enc_mp3_ops SND_AUDIOCODEC_MP3 0 "ff_libshine_encoder" USE_SHINE EXTRA_DEPS shine_ext) +_ffmpeg_add_encoder(CONFIG_FFMPEG_ENC_AAC ffmpeg_enc_aac FFENCAAC ffmpeg-enc-aac.c ffmpeg_enc_aac_ops SND_AUDIOCODEC_AAC 0 "" USE_VOAACENC EXTRA_DEPS vo_aacenc_ext) +_ffmpeg_add_encoder(CONFIG_FFMPEG_ENC_FLAC ffmpeg_enc_flac FFENCFLC ffmpeg-enc-flac.c ffmpeg_enc_flac_ops SND_AUDIOCODEC_FLAC 0 "ff_flac_encoder") +_ffmpeg_add_encoder(CONFIG_FFMPEG_ENC_ALAC ffmpeg_enc_alac FFENCALC ffmpeg-enc-alac.c ffmpeg_enc_alac_ops SND_AUDIOCODEC_ALAC 0 "ff_alac_encoder") +_ffmpeg_add_encoder(CONFIG_FFMPEG_ENC_SBC ffmpeg_enc_sbc FFENCSBC ffmpeg-enc-sbc.c ffmpeg_enc_sbc_ops SND_AUDIOCODEC_SBC 1 "ff_sbc_encoder") +_ffmpeg_add_encoder(CONFIG_FFMPEG_ENC_G722 ffmpeg_enc_g722 FFENC722 ffmpeg-enc-g722.c ffmpeg_enc_g722_ops SND_AUDIOCODEC_G722 1 "ff_adpcm_g722_encoder") +_ffmpeg_add_encoder(CONFIG_FFMPEG_ENC_APTX ffmpeg_enc_aptx FFENCAPX ffmpeg-enc-aptx.c ffmpeg_enc_aptx_ops SND_AUDIOCODEC_APTX 1 "ff_aptx_encoder") +_ffmpeg_add_encoder(CONFIG_FFMPEG_ENC_APTX_HD ffmpeg_enc_aptx_hd FFENCAHD ffmpeg-enc-aptx.c ffmpeg_enc_aptx_hd_ops SND_AUDIOCODEC_APTX_HD 1 "ff_aptx_hd_encoder") +_ffmpeg_add_encoder(CONFIG_FFMPEG_ENC_G726 ffmpeg_enc_g726 FFENC726 ffmpeg-enc-g726.c ffmpeg_enc_g726_ops SND_AUDIOCODEC_G726 1 "ff_adpcm_g726_encoder;ff_adpcm_g726le_encoder") +_ffmpeg_add_encoder(CONFIG_FFMPEG_ENC_ALAW ffmpeg_enc_alaw FFENCALW ffmpeg-enc-g711.c ffmpeg_enc_alaw_ops SND_AUDIOCODEC_ALAW 1 "ff_pcm_alaw_encoder") +_ffmpeg_add_encoder(CONFIG_FFMPEG_ENC_MULAW ffmpeg_enc_mulaw FFENCULW ffmpeg-enc-g711.c ffmpeg_enc_mulaw_ops SND_AUDIOCODEC_MULAW 1 "ff_pcm_mulaw_encoder") + +# --- Audio Filters (IBS=16384, OBS=16384) --- +if(CONFIG_FFMPEG_FILTER_AFFTDN) + _ffmpeg_add_llext_module(ffmpeg_af_afftdn + ROLE af + MANIFEST_NAME "FFAFDN" + UUID_SYM ffmpeg_af_afftdn + IBS 16384 OBS 16384 + AF_DEFINE FFMPEG_AF_SINGLE_AFFTDN + SOURCES "${_ff_root_dir}/ffmpeg-filter.c" + FILTER_SYMS ff_asrc_abuffer ff_asink_abuffer ff_af_aformat ff_af_afftdn + USE_AVFILTER + USE_FASTMATH + ) +endif() +if(CONFIG_FFMPEG_FILTER_ALIMITER) + _ffmpeg_add_llext_module(ffmpeg_af_alimiter + ROLE af + MANIFEST_NAME "FFALIM" + UUID_SYM ffmpeg_af_alimiter + IBS 16384 OBS 16384 + AF_DEFINE FFMPEG_AF_SINGLE_ALIMITER + SOURCES "${_ff_root_dir}/ffmpeg-filter.c" + FILTER_SYMS ff_asrc_abuffer ff_asink_abuffer ff_af_aformat ff_af_alimiter + USE_AVFILTER + USE_FASTMATH + ) endif() + +set(FFMPEG_LLEXT_TARGETS ${_ff_all_targets} PARENT_SCOPE) diff --git a/uuid-registry.txt b/uuid-registry.txt index ca201b2df1fe..982da93b4c6d 100644 --- a/uuid-registry.txt +++ b/uuid-registry.txt @@ -82,6 +82,31 @@ f6d15ad3-b122-458c-ae9b0ab0b5867aa0 dummy_dma 5150c0e6-27f9-4ec8-8351c705b642d12f eq_iir 889f6dcd-ddcd-4e05-aa5b0d39f8bca961 esai 1207af73-f9c0-4f65-92b49d45178fc9a8 ffmpeg_dec +3e76cf8f-3391-42c5-9ff25bcd5b117139 ffmpeg_dec_flac +5631ac1b-23a9-45e4-87a363d0b024aff2 ffmpeg_dec_aac +3ffe5e59-9ffb-4fc2-b7ae861a32e2098a ffmpeg_dec_opus +0dc39c33-411f-4b54-88a75b2d0749b6c1 ffmpeg_dec_mp3 +14586f35-70c6-4229-aec1d74e9e82c089 ffmpeg_dec_alac +b0dfdccc-4c0d-4ac1-9d4f451724f9b892 ffmpeg_dec_sbc +e35d347a-224d-44a1-9e61974e17254b64 ffmpeg_dec_g722 +f64b1cfe-fe1e-4ea7-9dc5bf4c2ae8d218 ffmpeg_dec_aptx +7e65fb20-bd9e-478c-8c093b700f3e0b52 ffmpeg_dec_aptx_hd +d19ecb7c-add8-4129-a10b42b72c697541 ffmpeg_dec_g726 +d06c27a8-0536-4009-a20dc037278e6d2b ffmpeg_dec_alaw +baeb50e3-5bc9-403f-85e146651495ac25 ffmpeg_dec_mulaw +987d75dd-2db0-4cca-b2eab3e97636fe00 ffmpeg_enc_mp3 +b85435cd-6914-4dfd-8646c83873d0af2a ffmpeg_enc_aac +f3d30b8a-f33c-413d-a4609e1ceb378a51 ffmpeg_enc_flac +b0057b6f-b0c2-418c-88b83b50ba508bce ffmpeg_enc_alac +2a1536c8-b836-4b44-9164a658276b50fd ffmpeg_enc_sbc +f71fed97-dacb-435e-9ffb6a784f12fb84 ffmpeg_enc_g722 +1ae50809-4ab4-4425-b0c46e0769d1dd3b ffmpeg_enc_aptx +252ad4bd-98f2-41be-985ca46aac2f36e8 ffmpeg_enc_aptx_hd +e9aa820a-6923-4fea-aceae09e95442c63 ffmpeg_enc_g726 +c5213065-db1d-4a66-9796eb2057a0c4c0 ffmpeg_enc_alaw +1f280ffc-27bf-48d6-b0f75a6b80633b09 ffmpeg_enc_mulaw +87b7f2ee-5a71-48c7-b91db85d9a1aed9d ffmpeg_af_afftdn +511b2960-20b1-4663-aa5781dc7bf92bb9 ffmpeg_af_alimiter bfc7488c-75aa-4ce8-9dbed8da08a698c2 file 93446e12-1864-4e04-afe03b1d778ffb79 fir 61bca9a8-18d0-4a18-8e7b2639219804b7 gain From c511393d88dae46b999126d280a70fc7f4d2a774 Mon Sep 17 00:00:00 2001 From: Liam Girdwood Date: Sat, 28 Nov 2026 18:10:43 +0000 Subject: [PATCH 8/9] topology2: wire compressed FFmpeg pipelines to standalone module UUIDs Route COMPR_FFMPEG (decoder) and COMPR_FFMPEG_ENC (encoder) pipelines to the per-codec standalone module UUIDs when COMPR_FFMPEG_LLEXT is enabled (default), keeping legacy monolithic UUID fallback when disabled, and pass COMPR_FFMPEG_ENC_CODEC for per-codec encoder topologies. Signed-off-by: Liam Girdwood --- .../topology2/development/tplg-targets.cmake | 14 +++++++------- .../platform/intel/compr-capture-ffmpeg.conf | 17 +++++++++++++++++ .../platform/intel/compr-default.conf | 2 ++ .../topology2/platform/intel/compr-ffmpeg.conf | 18 ++++++++++++++++++ 4 files changed, 44 insertions(+), 7 deletions(-) diff --git a/tools/topology/topology2/development/tplg-targets.cmake b/tools/topology/topology2/development/tplg-targets.cmake index 07f91662df7e..ef11b70aae61 100644 --- a/tools/topology/topology2/development/tplg-targets.cmake +++ b/tools/topology/topology2/development/tplg-targets.cmake @@ -237,13 +237,13 @@ DEEPBUFFER_FW_DMA_MS=false,HDA_ANALOG_CAPTURE_RATE=96000,HDA_ANALOG_PLAYBACK_RAT # by ffmpeg_enc. COMPR_FFMPEG_ENC # gates compr-capture-ffmpeg.conf, which taps the HDA analog capture BE, so it # may be built alongside or independently of the decode COMPR_FFMPEG path above. -"sof-hda-generic\;sof-hda-generic-ffmpeg-mp3-enc-compr\;HDA_CONFIG=mix,COMPR_FFMPEG_ENC=true" -"sof-hda-generic\;sof-hda-generic-ffmpeg-aac-enc-compr\;HDA_CONFIG=mix,COMPR_FFMPEG_ENC=true" -"sof-hda-generic\;sof-hda-generic-ffmpeg-flac-enc-compr\;HDA_CONFIG=mix,COMPR_FFMPEG_ENC=true" -"sof-hda-generic\;sof-hda-generic-ffmpeg-alac-enc-compr\;HDA_CONFIG=mix,COMPR_FFMPEG_ENC=true" -"sof-hda-generic\;sof-hda-generic-ffmpeg-sbc-enc-compr\;HDA_CONFIG=mix,COMPR_FFMPEG_ENC=true" -"sof-hda-generic\;sof-hda-generic-ffmpeg-g722-enc-compr\;HDA_CONFIG=mix,COMPR_FFMPEG_ENC=true" -"sof-hda-generic\;sof-hda-generic-ffmpeg-aptx-enc-compr\;HDA_CONFIG=mix,COMPR_FFMPEG_ENC=true" +"sof-hda-generic\;sof-hda-generic-ffmpeg-mp3-enc-compr\;HDA_CONFIG=mix,COMPR_FFMPEG_ENC=true,COMPR_FFMPEG_ENC_CODEC=mp3" +"sof-hda-generic\;sof-hda-generic-ffmpeg-aac-enc-compr\;HDA_CONFIG=mix,COMPR_FFMPEG_ENC=true,COMPR_FFMPEG_ENC_CODEC=aac" +"sof-hda-generic\;sof-hda-generic-ffmpeg-flac-enc-compr\;HDA_CONFIG=mix,COMPR_FFMPEG_ENC=true,COMPR_FFMPEG_ENC_CODEC=flac" +"sof-hda-generic\;sof-hda-generic-ffmpeg-alac-enc-compr\;HDA_CONFIG=mix,COMPR_FFMPEG_ENC=true,COMPR_FFMPEG_ENC_CODEC=alac" +"sof-hda-generic\;sof-hda-generic-ffmpeg-sbc-enc-compr\;HDA_CONFIG=mix,COMPR_FFMPEG_ENC=true,COMPR_FFMPEG_ENC_CODEC=sbc" +"sof-hda-generic\;sof-hda-generic-ffmpeg-g722-enc-compr\;HDA_CONFIG=mix,COMPR_FFMPEG_ENC=true,COMPR_FFMPEG_ENC_CODEC=g722" +"sof-hda-generic\;sof-hda-generic-ffmpeg-aptx-enc-compr\;HDA_CONFIG=mix,COMPR_FFMPEG_ENC=true,COMPR_FFMPEG_ENC_CODEC=aptx" # SDW + DMIC + HDMI, with 16 kHz DMIC1 "cavs-sdw\;sof-mtl-sdw-cs42l42-l0-max98363-l2-4ch-48k-16k\;PLATFORM=mtl,\ diff --git a/tools/topology/topology2/platform/intel/compr-capture-ffmpeg.conf b/tools/topology/topology2/platform/intel/compr-capture-ffmpeg.conf index f6801e6b4c9d..8c5ec78a07c9 100644 --- a/tools/topology/topology2/platform/intel/compr-capture-ffmpeg.conf +++ b/tools/topology/topology2/platform/intel/compr-capture-ffmpeg.conf @@ -69,6 +69,23 @@ IncludeByKey.COMPR_FFMPEG_ENC { # 21.33 ms) must accumulate a full frame before emitting a # packet, so the DP ring supplies headroom a 2 ms LL buffer # lacks. + IncludeByKey.COMPR_FFMPEG_LLEXT { + "true" { + IncludeByKey.COMPR_FFMPEG_ENC_CODEC { + "mp3" { uuid "dd:75:7d:98:b0:2d:ca:4c:b2:ea:b3:e9:76:36:fe:00" } + "aac" { uuid "cd:35:54:b8:14:69:fd:4d:86:46:c8:38:73:d0:af:2a" } + "flac" { uuid "8a:0b:d3:f3:3c:f3:3d:41:a4:60:9e:1c:eb:37:8a:51" } + "alac" { uuid "6f:7b:05:b0:c2:b0:8c:41:88:b8:3b:50:ba:50:8b:ce" } + "sbc" { uuid "c8:36:15:2a:36:b8:44:4b:91:64:a6:58:27:6b:50:fd" } + "g722" { uuid "97:ed:1f:f7:cb:da:5e:43:9f:fb:6a:78:4f:12:fb:84" } + "aptx" { uuid "09:08:e5:1a:b4:4a:25:44:b0:c4:6e:07:69:d1:dd:3b" } + "aptx_hd" { uuid "bd:d4:2a:25:f2:98:be:41:98:5c:a4:6a:ac:2f:36:e8" } + "g726" { uuid "0a:82:aa:e9:23:69:ea:4f:ac:ea:e0:9e:95:44:2c:63" } + "alaw" { uuid "65:30:21:c5:1d:db:66:4a:97:96:eb:20:57:a0:c4:c0" } + "mulaw" { uuid "fc:0f:28:1f:bf:27:d6:48:b0:f7:5a:6b:80:63:3b:09" } + } + } + } core_id $COMPR_ENCODER_CORE_ID stack_bytes_requirement $COMPR_FFMPEG_ENC_STACK heap_bytes_requirement $COMPR_FFMPEG_ENC_HEAP diff --git a/tools/topology/topology2/platform/intel/compr-default.conf b/tools/topology/topology2/platform/intel/compr-default.conf index 73df2ebc339a..bf727e5160db 100644 --- a/tools/topology/topology2/platform/intel/compr-default.conf +++ b/tools/topology/topology2/platform/intel/compr-default.conf @@ -20,6 +20,7 @@ Define { # decoder widget (ffmpeg_dec) instead of the Cadence decoder. Enable # instead of COMPRESSED_1 (they share the COMPR_* pcm/pipeline defines). COMPR_FFMPEG false + COMPR_FFMPEG_LLEXT true COMPR_FFMPEG_DEC_STACK 65536 # Modular heap requirement (mod_alloc): up to 192 KiB DP output ring + # 64 KiB PCM staging buffer + 4 KiB input bounce buffer + 4 KiB @@ -36,6 +37,7 @@ Define { # HDA-specific PCM name are set where the BE is defined # (cavs-mixin-mixout-hda.conf). COMPR_FFMPEG_ENC false + COMPR_FFMPEG_ENC_CODEC 'mp3' COMPR_ENC_PCM_NAME 'Compress Capture' COMPR_ENC_PCM_ID 51 COMPR_ENC_PIPELINE_ID 91 diff --git a/tools/topology/topology2/platform/intel/compr-ffmpeg.conf b/tools/topology/topology2/platform/intel/compr-ffmpeg.conf index cc80c07f00b9..53faaf4ebec7 100644 --- a/tools/topology/topology2/platform/intel/compr-ffmpeg.conf +++ b/tools/topology/topology2/platform/intel/compr-ffmpeg.conf @@ -35,6 +35,24 @@ IncludeByKey.COMPR_FFMPEG { # (after container header stripping) and per-frame decoding. stack_bytes_requirement $COMPR_FFMPEG_DEC_STACK heap_bytes_requirement $COMPR_FFMPEG_DEC_HEAP + IncludeByKey.COMPR_FFMPEG_LLEXT { + "true" { + IncludeByKey.COMPR_FFMPEG_CODEC { + "flac" { uuid "8f:cf:76:3e:91:33:c5:42:9f:f2:5b:cd:5b:11:71:39" } + "aac" { uuid "1b:ac:31:56:a9:23:e4:45:87:a3:63:d0:b0:24:af:f2" } + "opus" { uuid "59:5e:fe:3f:fb:9f:c2:4f:b7:ae:86:1a:32:e2:09:8a" } + "mp3" { uuid "33:9c:c3:0d:1f:41:54:4b:88:a7:5b:2d:07:49:b6:c1" } + "alac" { uuid "35:6f:58:14:c6:70:29:42:ae:c1:d7:4e:9e:82:c0:89" } + "sbc" { uuid "cc:dc:df:b0:0d:4c:c1:4a:9d:4f:45:17:24:f9:b8:92" } + "g722" { uuid "7a:34:5d:e3:4d:22:a1:44:9e:61:97:4e:17:25:4b:64" } + "aptx" { uuid "fe:1c:4b:f6:1e:fe:a7:4e:9d:c5:bf:4c:2a:e8:d2:18" } + "aptx_hd" { uuid "20:fb:65:7e:9e:bd:8c:47:8c:09:3b:70:0f:3e:0b:52" } + "g726" { uuid "7c:cb:9e:d1:d8:ad:29:41:a1:0b:42:b7:2c:69:75:41" } + "alaw" { uuid "a8:27:6c:d0:36:05:09:40:a2:0d:c0:37:27:8e:6d:2b" } + "mulaw" { uuid "e3:50:eb:ba:c9:5b:3f:40:85:e1:46:65:14:95:ac:25" } + } + } + } # Optional codec setup control: extradata may be delivered via # ALSA mixer bytes control; FLAC STREAMINFO is also extracted # dynamically from the incoming stream by ffmpeg_dec. From cac7540e63b8d411c146f359a3e3f97844e2f01d Mon Sep 17 00:00:00 2001 From: Liam Girdwood Date: Sat, 28 Nov 2026 18:10:43 +0000 Subject: [PATCH 9/9] lib_manager: llext_manager: fix unload, vpage release, and heap perms Repeated load/unload cycles of large FFmpeg LLEXTs via IPC4_SOF_LIBRARY_UNLOAD exhausted virtual memory pages and panicked on subsequent reloads when freed detached cold pages kept read-only MMU permissions or lost segment addresses before both domains tore down. Keep .cold/.coldrodata addresses until teardown completes in both LL and DP domains, release vpage reservations in llext_manager_remove_library(), and restore SYS_MM_MEM_PERM_RW on non-empty detached ELF pages during unmap/rollback so returning them to l3_heap leaves writable heap pages cleanly reusable. Signed-off-by: Liam Girdwood --- src/kernel/library_manager/llext_manager.c | 56 ++++++++++++++++++---- 1 file changed, 46 insertions(+), 10 deletions(-) diff --git a/src/kernel/library_manager/llext_manager.c b/src/kernel/library_manager/llext_manager.c index 49dd315200a0..18d090b330b0 100644 --- a/src/kernel/library_manager/llext_manager.c +++ b/src/kernel/library_manager/llext_manager.c @@ -81,17 +81,33 @@ static int llext_manager_align_unmap(void __sparse_cache *vma, size_t size) return sys_mm_drv_unmap_region(aligned_vma, ALIGN_UP(pre_pad_size + size, PAGE_SZ)); } +/* + * Update MMU permissions for detached .cold/.coldrodata pages backed by l3_heap. + * Used both to tighten loaded pages to RX/RO and to restore K_MEM_PERM_RW when + * unmapping or rolling back so freeing back to l3_heap never panics later loads. + */ static void llext_manager_detached_update_flags(void __sparse_cache *vma, size_t size, uint32_t flags) { #ifdef CONFIG_MMU size_t pre_pad_size = (uintptr_t)vma & (PAGE_SZ - 1); void *aligned_vma = (__sparse_force uint8_t *)vma - pre_pad_size; + /* Always preserve write-back cacheability on DRAM-backed heap pages. */ + uint32_t k_flags = K_MEM_CACHE_WB; + + /* Skip empty segments so arch_mem_map() is not called with zero size. */ + if (!size) + return; + + if (flags & SYS_MM_MEM_PERM_RW) + k_flags |= K_MEM_PERM_RW; + if (flags & SYS_MM_MEM_PERM_EXEC) + k_flags |= K_MEM_PERM_EXEC; /* Use cached virtual address */ uintptr_t va = POINTER_TO_UINT(sys_cache_cached_ptr_get(aligned_vma)); - arch_mem_map(aligned_vma, va, ALIGN_UP(pre_pad_size + size, PAGE_SZ), flags); + arch_mem_map(aligned_vma, va, ALIGN_UP(pre_pad_size + size, PAGE_SZ), k_flags); #endif } @@ -208,7 +224,8 @@ static int llext_manager_rm_partition(struct k_mem_domain *domain, } /** - * Detach .cold and .coldrodata partitions from @domain and clear their metadata. + * Detach .cold and .coldrodata partitions from @domain without clearing + * segment metadata, since a DP module may belong to both DP and LL domains. */ static int llext_manager_rm_cold_partitions(struct lib_manager_module *mctx, struct k_mem_domain *domain) @@ -222,8 +239,6 @@ static int llext_manager_rm_cold_partitions(struct lib_manager_module *mctx, K_MEM_PARTITION_P_RX_U_RX | XTENSA_MMU_CACHED_WB); if (ret < 0) LOG_ERR("failed to remove .cold memory partition: %d", ret); - mctx->segment[LIB_MANAGER_COLD].addr = 0; - mctx->segment[LIB_MANAGER_COLD].size = 0; } if (mctx->segment[LIB_MANAGER_COLDRODATA].addr) { @@ -236,12 +251,18 @@ static int llext_manager_rm_cold_partitions(struct lib_manager_module *mctx, if (!ret) ret = err; } - mctx->segment[LIB_MANAGER_COLDRODATA].addr = 0; - mctx->segment[LIB_MANAGER_COLDRODATA].size = 0; } return ret; } + +static void llext_manager_clear_cold_partitions(struct lib_manager_module *mctx) +{ + mctx->segment[LIB_MANAGER_COLD].addr = 0; + mctx->segment[LIB_MANAGER_COLD].size = 0; + mctx->segment[LIB_MANAGER_COLDRODATA].addr = 0; + mctx->segment[LIB_MANAGER_COLDRODATA].size = 0; +} #endif /* CONFIG_USERSPACE */ static void llext_manager_unmap_detached_sections(struct llext_loader *ldr, @@ -263,12 +284,12 @@ static void llext_manager_unmap_detached_sections(struct llext_loader *ldr, if (s_region != region) continue; - /* unmap detached sections (will be outside requested VMA area) */ + /* restore writable heap permissions for detached sections */ if ((uintptr_t)shdr->sh_addr < (uintptr_t)vma || (uintptr_t)shdr->sh_addr >= (uintptr_t)vma + size) llext_manager_detached_update_flags((__sparse_force void __sparse_cache *) llext_peek(ldr, shdr->sh_offset), - shdr->sh_size, 0); + shdr->sh_size, SYS_MM_MEM_PERM_RW); } #endif /* CONFIG_MMU */ } @@ -397,13 +418,19 @@ __cold static int llext_manager_load_module(struct lib_manager_module *mctx) return 0; #ifdef CONFIG_SOF_USERSPACE_LL e_data: + llext_manager_unmap_detached_sections(ldr, ext, LLEXT_MEM_DATA, + va_base_data, data_size); if (data_size) llext_manager_align_unmap(va_base_data, data_size); #endif e_rodata: + llext_manager_unmap_detached_sections(ldr, ext, LLEXT_MEM_RODATA, + va_base_rodata, rodata_size); if (rodata_size) llext_manager_align_unmap(va_base_rodata, rodata_size); e_text: + llext_manager_unmap_detached_sections(ldr, ext, LLEXT_MEM_TEXT, + va_base_text, text_size); llext_manager_align_unmap(va_base_text, text_size); return ret; @@ -440,6 +467,7 @@ __cold static int llext_manager_unload_module(struct lib_manager_module *mctx) * llext_manager_align_unmap() so cache writeback does not fault. */ llext_manager_rm_cold_partitions(mctx, zephyr_ll_mem_domain()); + llext_manager_clear_cold_partitions(mctx); unsigned int sect_cnt = llext_section_count(ext); size_t total = sect_cnt * sizeof(elf_shdr_t); @@ -480,6 +508,9 @@ __cold static int llext_manager_unload_module(struct lib_manager_module *mctx) #ifdef CONFIG_SOF_USERSPACE_LL llext_manager_rm_mod_domain(mctx, zephyr_ll_mem_domain()); #endif +#ifdef CONFIG_USERSPACE + llext_manager_clear_cold_partitions(mctx); +#endif return err; } @@ -1522,7 +1553,7 @@ __cold int llext_manager_add_library(uint32_t module_id) return 0; } -/* Unload all idle LLEXT modules in @p module_id and free their metadata. */ +/* Unload all idle LLEXT modules in @p module_id and free their vpages/metadata. */ __cold int llext_manager_remove_library(uint32_t module_id) { struct lib_manager_mod_ctx *const ctx = lib_manager_get_mod_ctx(module_id); @@ -1545,7 +1576,7 @@ __cold int llext_manager_remove_library(uint32_t module_id) /* Remove runtime codec capabilities before freeing module descriptors. */ llext_manager_unregister_lib_codecs(ctx); - /* Release retained ELF inspection data, LLEXT instances, and loaders. */ + /* Release retained ELF inspection data, LLEXT instances, vpages, and loaders. */ for (k = 0; k < ctx->n_mod; k++) { struct lib_manager_module *mctx = &ctx->mod[k]; @@ -1562,6 +1593,11 @@ __cold int llext_manager_remove_library(uint32_t module_id) LOG_WRN("llext_unload(%u) failed: %d", k, ret); mctx->llext = NULL; } + if (mctx->vma.size) { + vpage_release((void *)mctx->vma.addr); + mctx->vma.addr = 0; + mctx->vma.size = 0; + } if (mctx->ebl) { rfree(mctx->ebl); mctx->ebl = NULL;