diff --git a/src/audio/ffmpeg_dec/CMakeLists.txt b/src/audio/ffmpeg_dec/CMakeLists.txt index 916eaafba70b..a598e942d5fa 100644 --- a/src/audio/ffmpeg_dec/CMakeLists.txt +++ b/src/audio/ffmpeg_dec/CMakeLists.txt @@ -27,9 +27,15 @@ else() if(CONFIG_FFMPEG_DEC_G722 OR CONFIG_COMP_FFMPEG_DEC_STUB) add_local_sources(sof ffmpeg_dec-g722.c) endif() - if(CONFIG_FFMPEG_DEC_APTX OR CONFIG_COMP_FFMPEG_DEC_STUB) + if(CONFIG_FFMPEG_DEC_APTX OR CONFIG_FFMPEG_DEC_APTX_HD OR CONFIG_COMP_FFMPEG_DEC_STUB) add_local_sources(sof ffmpeg_dec-aptx.c) endif() + if(CONFIG_FFMPEG_DEC_G726 OR CONFIG_COMP_FFMPEG_DEC_STUB) + add_local_sources(sof ffmpeg_dec-g726.c) + endif() + if(CONFIG_FFMPEG_DEC_ALAW OR CONFIG_FFMPEG_DEC_MULAW OR CONFIG_COMP_FFMPEG_DEC_STUB) + add_local_sources(sof ffmpeg_dec-g711.c) + endif() if(CONFIG_COMP_FFMPEG_DEC_STUB) add_local_sources(sof ffmpeg_dec-stub.c) @@ -58,9 +64,15 @@ else() if(CONFIG_FFMPEG_ENC_G722) add_local_sources(sof ffmpeg-enc-g722.c) endif() - if(CONFIG_FFMPEG_ENC_APTX) + if(CONFIG_FFMPEG_ENC_APTX OR CONFIG_FFMPEG_ENC_APTX_HD) add_local_sources(sof ffmpeg-enc-aptx.c) endif() + if(CONFIG_FFMPEG_ENC_G726) + add_local_sources(sof ffmpeg-enc-g726.c) + endif() + if(CONFIG_FFMPEG_ENC_ALAW OR CONFIG_FFMPEG_ENC_MULAW) + add_local_sources(sof ffmpeg-enc-g711.c) + endif() if(CONFIG_FFMPEG_FILTER) add_local_sources(sof ffmpeg-filter.c) endif() @@ -102,9 +114,15 @@ else() if(CONFIG_FFMPEG_ENC_G722) list(APPEND _ffmpeg_dec_sources ${CMAKE_CURRENT_SOURCE_DIR}/ffmpeg-enc-g722.c) endif() - if(CONFIG_FFMPEG_ENC_APTX) + if(CONFIG_FFMPEG_ENC_APTX OR CONFIG_FFMPEG_ENC_APTX_HD) list(APPEND _ffmpeg_dec_sources ${CMAKE_CURRENT_SOURCE_DIR}/ffmpeg-enc-aptx.c) endif() + if(CONFIG_FFMPEG_ENC_G726) + list(APPEND _ffmpeg_dec_sources ${CMAKE_CURRENT_SOURCE_DIR}/ffmpeg-enc-g726.c) + endif() + if(CONFIG_FFMPEG_ENC_ALAW OR CONFIG_FFMPEG_ENC_MULAW) + list(APPEND _ffmpeg_dec_sources ${CMAKE_CURRENT_SOURCE_DIR}/ffmpeg-enc-g711.c) + endif() if(CONFIG_FFMPEG_FILTER) list(APPEND _ffmpeg_dec_sources ${CMAKE_CURRENT_SOURCE_DIR}/ffmpeg-filter.c) endif() diff --git a/src/audio/ffmpeg_dec/Kconfig b/src/audio/ffmpeg_dec/Kconfig index c4ddbc4cd95e..e62fa91adae4 100644 --- a/src/audio/ffmpeg_dec/Kconfig +++ b/src/audio/ffmpeg_dec/Kconfig @@ -83,6 +83,36 @@ config FFMPEG_DEC_APTX --enable-decoder=aptx in the libavcodec build for compress playback streams requesting SND_AUDIOCODEC_APTX. +config FFMPEG_DEC_APTX_HD + bool "Bluetooth aptX HD decoder" + help + Bluetooth aptX HD 24-bit audio decoder. Pure integer 4-band QMF + + ADPCM decoder with no floating-point math dependency. Enables + --enable-decoder=aptx_hd in the libavcodec build for compress + playback streams requesting SND_AUDIOCODEC_APTX_HD. + +config FFMPEG_DEC_G726 + bool "ITU-T G.726 ADPCM decoder" + help + ITU-T G.726 ADPCM decoder (16/24/32/40 kbps, big- and little-endian + packing via profile=1). Pure integer ADPCM decoder with no + floating-point math dependency. Enables + --enable-decoder=adpcm_g726,adpcm_g726le in the libavcodec build. + +config FFMPEG_DEC_ALAW + bool "ITU-T G.711 A-law decoder" + help + ITU-T G.711 A-law companded PCM decoder. Pure integer table lookup + with no floating-point math dependency. Enables + --enable-decoder=pcm_alaw in the libavcodec build. + +config FFMPEG_DEC_MULAW + bool "ITU-T G.711 mu-law decoder" + help + ITU-T G.711 mu-law companded PCM decoder. Pure integer table lookup + with no floating-point math dependency. Enables + --enable-decoder=pcm_mulaw in the libavcodec build. + comment "FFmpeg encoders to build (footprint scales with selection)" config FFMPEG_ENC_MP3 @@ -144,11 +174,41 @@ config FFMPEG_ENC_APTX dependency. Selected at runtime for compress capture streams requesting SND_AUDIOCODEC_APTX. +config FFMPEG_ENC_APTX_HD + bool "Bluetooth aptX HD encoder (native libavcodec)" + help + Bluetooth aptX HD 24-bit audio encoder via native libavcodec aptx_hd + encoder. Pure integer 4-band QMF + ADPCM encoder with no + floating-point math dependency. Selected at runtime for compress + capture streams requesting SND_AUDIOCODEC_APTX_HD. + +config FFMPEG_ENC_G726 + bool "ITU-T G.726 ADPCM encoder (native libavcodec)" + help + ITU-T G.726 ADPCM encoder (16/24/32/40 kbps, big- and little-endian + packing via profile=1) via native libavcodec adpcm_g726 / + adpcm_g726le encoders. Pure integer ADPCM with no floating-point math + dependency. + +config FFMPEG_ENC_ALAW + bool "ITU-T G.711 A-law encoder (native libavcodec)" + help + ITU-T G.711 A-law companded PCM encoder via native libavcodec + pcm_alaw encoder. Pure integer lookup with no floating-point math + dependency. + +config FFMPEG_ENC_MULAW + bool "ITU-T G.711 mu-law encoder (native libavcodec)" + help + ITU-T G.711 mu-law companded PCM encoder via native libavcodec + pcm_mulaw encoder. Pure integer lookup with no floating-point math + dependency. + # Selected automatically when any encoder is enabled. Gates compilation of the # runtime encode path (ffmpeg-encode.c). config FFMPEG_ENC bool - default y if FFMPEG_ENC_MP3 || FFMPEG_ENC_AAC || FFMPEG_ENC_FLAC || FFMPEG_ENC_ALAC || FFMPEG_ENC_SBC || FFMPEG_ENC_G722 || FFMPEG_ENC_APTX + default y if FFMPEG_ENC_MP3 || FFMPEG_ENC_AAC || FFMPEG_ENC_FLAC || FFMPEG_ENC_ALAC || FFMPEG_ENC_SBC || FFMPEG_ENC_G722 || FFMPEG_ENC_APTX || FFMPEG_ENC_APTX_HD || FFMPEG_ENC_G726 || FFMPEG_ENC_ALAW || FFMPEG_ENC_MULAW comment "FFmpeg audio filters to build (libavfilter)" depends on IPC_MAJOR_4 diff --git a/src/audio/ffmpeg_dec/ffmpeg-enc-aptx.c b/src/audio/ffmpeg_dec/ffmpeg-enc-aptx.c index f7625d9aeec9..0fb61a5ee6a0 100644 --- a/src/audio/ffmpeg_dec/ffmpeg-enc-aptx.c +++ b/src/audio/ffmpeg_dec/ffmpeg-enc-aptx.c @@ -2,7 +2,7 @@ // // Copyright(c) 2026 Intel Corporation. // -// Bluetooth aptX per-codec encoder implementation for ffmpeg_dec. +/* Bluetooth aptX / aptX HD per-codec encoder implementation for ffmpeg_dec. */ #include #include @@ -46,14 +46,17 @@ static int ffmpeg_enc_aptx_init(struct processing_module *mod, struct ffmpeg_enc { struct comp_dev *dev = mod->dev; struct ffmpeg_enc_aptx_data *aptx; + enum AVCodecID codec_id; aptx = mod_zalloc(mod, sizeof(*aptx)); if (!aptx) return -ENOMEM; - aptx->codec = avcodec_find_encoder(AV_CODEC_ID_APTX); + codec_id = (e->codec_ops->id == FFMPEG_DEC_CODEC_APTX_HD) ? + AV_CODEC_ID_APTX_HD : AV_CODEC_ID_APTX; + aptx->codec = avcodec_find_encoder(codec_id); if (!aptx->codec) { - comp_err(dev, "libavcodec aptX encoder not found"); + comp_err(dev, "libavcodec %s encoder not found", e->codec_ops->name); mod_free(mod, aptx); return -ENODEV; } @@ -188,6 +191,7 @@ static void ffmpeg_enc_aptx_free(struct processing_module *mod, struct ffmpeg_en e->codec_priv = NULL; } +#if defined(CONFIG_FFMPEG_ENC_APTX) const struct ffmpeg_enc_codec_ops ffmpeg_enc_aptx_ops = { .id = FFMPEG_DEC_CODEC_APTX, .snd_codec_id = SND_AUDIOCODEC_APTX, @@ -199,3 +203,18 @@ const struct ffmpeg_enc_codec_ops ffmpeg_enc_aptx_ops = { .reset = ffmpeg_enc_aptx_reset, .free = ffmpeg_enc_aptx_free, }; +#endif + +#if defined(CONFIG_FFMPEG_ENC_APTX_HD) +const struct ffmpeg_enc_codec_ops ffmpeg_enc_aptx_hd_ops = { + .id = FFMPEG_DEC_CODEC_APTX_HD, + .snd_codec_id = SND_AUDIOCODEC_APTX_HD, + .name = "aptx_hd", + .validate_params = ffmpeg_enc_aptx_validate_params, + .init = ffmpeg_enc_aptx_init, + .configure = ffmpeg_enc_aptx_configure, + .encode_frame = ffmpeg_enc_aptx_encode_frame, + .reset = ffmpeg_enc_aptx_reset, + .free = ffmpeg_enc_aptx_free, +}; +#endif diff --git a/src/audio/ffmpeg_dec/ffmpeg-enc-g711.c b/src/audio/ffmpeg_dec/ffmpeg-enc-g711.c new file mode 100644 index 000000000000..ab2cb5001018 --- /dev/null +++ b/src/audio/ffmpeg_dec/ffmpeg-enc-g711.c @@ -0,0 +1,286 @@ +// SPDX-License-Identifier: BSD-3-Clause +/* + * Copyright(c) 2026 Intel Corporation. + * + * ITU-T G.711 (A-law and mu-law) encoder implementation for ffmpeg_dec. + */ + +#include +#include +#include +#include +#include +#include +#include +#include "ffmpeg_dec.h" +#include "ffmpeg_dec-convert.h" + +LOG_MODULE_DECLARE(ffmpeg_dec, CONFIG_SOF_LOG_LEVEL); + +#define G711_ENC_FRAME_SAMPLES 160 + +struct ffmpeg_enc_g711_data { + const AVCodec *codec; + AVCodecContext *avctx; + AVPacket *pkt; + AVFrame *frame; + uint32_t enc_rate; + uint32_t enc_ch; + int decimate; +}; + +static int ffmpeg_enc_g711_validate_params(struct processing_module *mod, + const struct snd_codec *codec_params) +{ + struct comp_dev *dev = mod->dev; + + if (codec_params->format != SND_AUDIOSTREAMFORMAT_UNDEFINED && + codec_params->format != SND_AUDIOSTREAMFORMAT_RAW && + codec_params->format != 2 && + codec_params->format != 10) { + comp_err(dev, "unsupported G.711 stream format 0x%x", + codec_params->format); + return -ENOTSUP; + } + + return 0; +} + +static int ffmpeg_enc_g711_init(struct processing_module *mod, struct ffmpeg_enc_data *e) +{ + struct comp_dev *dev = mod->dev; + struct ffmpeg_enc_g711_data *g711; + enum AVCodecID codec_id; + + g711 = mod_zalloc(mod, sizeof(*g711)); + if (!g711) { + return -ENOMEM; + } + + codec_id = (e->codec_ops->id == FFMPEG_DEC_CODEC_ALAW) ? + AV_CODEC_ID_PCM_ALAW : AV_CODEC_ID_PCM_MULAW; + g711->codec = avcodec_find_encoder(codec_id); + if (!g711->codec) { + comp_err(dev, "libavcodec %s encoder not found", e->codec_ops->name); + mod_free(mod, g711); + return -ENODEV; + } + + g711->avctx = avcodec_alloc_context3(g711->codec); + g711->pkt = av_packet_alloc(); + g711->frame = av_frame_alloc(); + if (!g711->avctx || !g711->pkt || !g711->frame) { + if (g711->frame) { + av_frame_free(&g711->frame); + } + if (g711->pkt) { + av_packet_free(&g711->pkt); + } + if (g711->avctx) { + avcodec_free_context(&g711->avctx); + } + mod_free(mod, g711); + return -ENOMEM; + } + + e->codec_priv = g711; + return 0; +} + +static int ffmpeg_enc_g711_configure(struct processing_module *mod, struct ffmpeg_enc_data *e) +{ + struct ffmpeg_dec_comp_data *cd = module_get_private_data(mod); + struct ffmpeg_enc_g711_data *g711 = e->codec_priv; + struct comp_dev *dev = mod->dev; + uint32_t enc_rate = e->req_rate ? e->req_rate : cd->out_rate; + uint32_t enc_ch = e->req_channels ? e->req_channels : cd->out_channels; + int ret; + + if (enc_ch < 1 || enc_ch > 2 || + (enc_ch != 1 && enc_ch != cd->out_channels)) { + comp_err(dev, "G.711 cannot convert %u ch source to %u ch", + cd->out_channels, enc_ch); + return -EINVAL; + } + + if (enc_rate == cd->out_rate) { + g711->decimate = 1; + } else if (cd->out_rate == 48000 && enc_rate == 16000) { + g711->decimate = 3; + } else if (cd->out_rate == 48000 && enc_rate == 8000) { + g711->decimate = 6; + } else { + comp_err(dev, "G.711 cannot convert %u Hz source to %u Hz", + cd->out_rate, enc_rate); + return -EINVAL; + } + + if (g711->decimate > 1 && enc_ch != 1) { + comp_err(dev, "G.711 decimation %d:1 only supports mono (got %u ch)", + g711->decimate, enc_ch); + return -EINVAL; + } + + if (avcodec_is_open(g711->avctx)) { + avcodec_free_context(&g711->avctx); + g711->avctx = avcodec_alloc_context3(g711->codec); + if (!g711->avctx) { + return -ENOMEM; + } + } + + g711->enc_rate = enc_rate; + g711->enc_ch = enc_ch; + g711->avctx->sample_rate = enc_rate; + av_channel_layout_default(&g711->avctx->ch_layout, enc_ch); + g711->avctx->sample_fmt = AV_SAMPLE_FMT_S16; + g711->avctx->thread_count = 1; + + ret = avcodec_open2(g711->avctx, g711->codec, NULL); + if (ret < 0) { + comp_err(dev, "avcodec_open2 (%s encoder) failed %d", + e->codec_ops->name, ret); + return -EIO; + } + + e->nb = G711_ENC_FRAME_SAMPLES * g711->decimate; + return 0; +} + +static int ffmpeg_enc_g711_encode_frame(struct processing_module *mod, struct ffmpeg_enc_data *e) +{ + struct ffmpeg_dec_comp_data *cd = module_get_private_data(mod); + struct ffmpeg_enc_g711_data *g711 = e->codec_priv; + struct comp_dev *dev = mod->dev; + int ret; + + g711->frame->nb_samples = G711_ENC_FRAME_SAMPLES; + g711->frame->format = g711->avctx->sample_fmt; + g711->frame->sample_rate = g711->enc_rate; + av_channel_layout_copy(&g711->frame->ch_layout, &g711->avctx->ch_layout); + if (av_frame_get_buffer(g711->frame, 0) < 0) { + return -ENOMEM; + } + + if (g711->decimate == 6) { + ffmpeg_dec_pcm_decimate_6to1_s16_mono(e->pcm_in, + (int16_t *)g711->frame->data[0], + e->src_fmt, cd->out_channels, + G711_ENC_FRAME_SAMPLES, + e->decim_hist); + } else if (g711->decimate == 3) { + ffmpeg_dec_pcm_decimate_3to1_s16_mono(e->pcm_in, + (int16_t *)g711->frame->data[0], + e->src_fmt, cd->out_channels, + G711_ENC_FRAME_SAMPLES, + e->decim_hist); + } else { + ret = ffmpeg_dec_pcm_to_frame(e->pcm_in, g711->frame, + e->src_fmt, cd->out_channels, + G711_ENC_FRAME_SAMPLES); + if (ret < 0) { + av_frame_unref(g711->frame); + return ret; + } + } + + ret = avcodec_send_frame(g711->avctx, g711->frame); + av_frame_unref(g711->frame); + if (ret < 0) { + comp_err(dev, "avcodec_send_frame (%s) failed %d", + e->codec_ops->name, ret); + return -EIO; + } + + while (ret >= 0) { + ret = avcodec_receive_packet(g711->avctx, g711->pkt); + if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) { + break; + } + if (ret < 0) { + comp_err(dev, "avcodec_receive_packet (%s) failed %d", + e->codec_ops->name, ret); + return -EIO; + } + if (g711->pkt->size > 0) { + ffmpeg_enc_stage_packet(mod, e, g711->pkt->data, + (size_t)g711->pkt->size); + } + av_packet_unref(g711->pkt); + ret = 0; + } + + return 0; +} + +static void ffmpeg_enc_g711_reset(struct processing_module *mod, struct ffmpeg_enc_data *e) +{ + struct ffmpeg_enc_g711_data *g711 = e->codec_priv; + + (void)mod; + if (!g711) { + return; + } + + if (g711->frame) { + av_frame_unref(g711->frame); + } + if (g711->pkt) { + av_packet_unref(g711->pkt); + } +} + +static void ffmpeg_enc_g711_free(struct processing_module *mod, struct ffmpeg_enc_data *e) +{ + struct ffmpeg_enc_g711_data *g711 = e->codec_priv; + + if (!g711) { + return; + } + + if (g711->frame) { + av_frame_free(&g711->frame); + } + if (g711->pkt) { + av_packet_free(&g711->pkt); + } + if (g711->avctx) { + avcodec_free_context(&g711->avctx); + } + mod_free(mod, g711); + e->codec_priv = NULL; +} + +#if defined(CONFIG_FFMPEG_ENC_ALAW) +const struct ffmpeg_enc_codec_ops ffmpeg_enc_alaw_ops = { + .id = FFMPEG_DEC_CODEC_ALAW, + .snd_codec_id = SND_AUDIOCODEC_ALAW, + .name = "alaw", + .supports_mono_downmix = true, + .supports_decim_3to1 = true, + .supports_decim_6to1 = true, + .validate_params = ffmpeg_enc_g711_validate_params, + .init = ffmpeg_enc_g711_init, + .configure = ffmpeg_enc_g711_configure, + .encode_frame = ffmpeg_enc_g711_encode_frame, + .reset = ffmpeg_enc_g711_reset, + .free = ffmpeg_enc_g711_free, +}; +#endif + +#if defined(CONFIG_FFMPEG_ENC_MULAW) +const struct ffmpeg_enc_codec_ops ffmpeg_enc_mulaw_ops = { + .id = FFMPEG_DEC_CODEC_MULAW, + .snd_codec_id = SND_AUDIOCODEC_MULAW, + .name = "mulaw", + .supports_mono_downmix = true, + .supports_decim_3to1 = true, + .supports_decim_6to1 = true, + .validate_params = ffmpeg_enc_g711_validate_params, + .init = ffmpeg_enc_g711_init, + .configure = ffmpeg_enc_g711_configure, + .encode_frame = ffmpeg_enc_g711_encode_frame, + .reset = ffmpeg_enc_g711_reset, + .free = ffmpeg_enc_g711_free, +}; +#endif diff --git a/src/audio/ffmpeg_dec/ffmpeg-enc-g722.c b/src/audio/ffmpeg_dec/ffmpeg-enc-g722.c index a3b59186f1a6..1d12c25e7100 100644 --- a/src/audio/ffmpeg_dec/ffmpeg-enc-g722.c +++ b/src/audio/ffmpeg_dec/ffmpeg-enc-g722.c @@ -229,6 +229,8 @@ const struct ffmpeg_enc_codec_ops ffmpeg_enc_g722_ops = { .id = FFMPEG_DEC_CODEC_G722, .snd_codec_id = SND_AUDIOCODEC_G722, .name = "g722", + .supports_mono_downmix = true, + .supports_decim_3to1 = true, .validate_params = ffmpeg_enc_g722_validate_params, .init = ffmpeg_enc_g722_init, .configure = ffmpeg_enc_g722_configure, diff --git a/src/audio/ffmpeg_dec/ffmpeg-enc-g726.c b/src/audio/ffmpeg_dec/ffmpeg-enc-g726.c new file mode 100644 index 000000000000..489d30f2e795 --- /dev/null +++ b/src/audio/ffmpeg_dec/ffmpeg-enc-g726.c @@ -0,0 +1,286 @@ +// SPDX-License-Identifier: BSD-3-Clause +/* + * Copyright(c) 2026 Intel Corporation. + * + * ITU-T G.726 / G.726LE ADPCM per-codec encoder implementation for ffmpeg_dec. + */ + +#include +#include +#include +#include +#include +#include +#include +#include "ffmpeg_dec.h" +#include "ffmpeg_dec-convert.h" + +LOG_MODULE_DECLARE(ffmpeg_dec, CONFIG_SOF_LOG_LEVEL); + +#define G726_DEFAULT_CODE_SIZE 4 +/* Multiple of 8 so 2/3/4/5-bit codewords align to whole byte boundaries */ +#define G726_ENC_FRAME_SAMPLES 160 + +struct ffmpeg_enc_g726_data { + const AVCodec *codec; + AVCodecContext *avctx; + AVPacket *pkt; + AVFrame *frame; + uint32_t enc_rate; + int decimate; +}; + +static int ffmpeg_enc_g726_validate_params(struct processing_module *mod, + const struct snd_codec *codec_params) +{ + struct comp_dev *dev = mod->dev; + uint32_t req_ch = codec_params->ch_in ? + codec_params->ch_in : codec_params->ch_out; + + if (req_ch > 1) { + comp_err(dev, "G.726 encoder only supports mono (got %u ch)", req_ch); + return -EINVAL; + } + + if (codec_params->format != SND_AUDIOSTREAMFORMAT_UNDEFINED && + codec_params->format != SND_AUDIOSTREAMFORMAT_RAW && + codec_params->format != 2 && + codec_params->format != 10) { + comp_err(dev, "unsupported G.726 stream format 0x%x", + codec_params->format); + return -ENOTSUP; + } + + if (codec_params->profile > 1) { + comp_err(dev, "unsupported G.726 profile %u", + codec_params->profile); + return -ENOTSUP; + } + + return 0; +} + +static int ffmpeg_enc_g726_init(struct processing_module *mod, struct ffmpeg_enc_data *e) +{ + struct comp_dev *dev = mod->dev; + struct ffmpeg_enc_g726_data *g726; + enum AVCodecID codec_id; + + g726 = mod_zalloc(mod, sizeof(*g726)); + if (!g726) { + return -ENOMEM; + } + + codec_id = (e->req_profile == 1) ? + AV_CODEC_ID_ADPCM_G726LE : AV_CODEC_ID_ADPCM_G726; + g726->codec = avcodec_find_encoder(codec_id); + if (!g726->codec) { + comp_err(dev, "libavcodec G.726 encoder (profile %u) not found", + e->req_profile); + mod_free(mod, g726); + return -ENODEV; + } + + g726->avctx = avcodec_alloc_context3(g726->codec); + g726->pkt = av_packet_alloc(); + g726->frame = av_frame_alloc(); + if (!g726->avctx || !g726->pkt || !g726->frame) { + if (g726->frame) { + av_frame_free(&g726->frame); + } + if (g726->pkt) { + av_packet_free(&g726->pkt); + } + if (g726->avctx) { + avcodec_free_context(&g726->avctx); + } + mod_free(mod, g726); + return -ENOMEM; + } + + e->codec_priv = g726; + return 0; +} + +static int ffmpeg_enc_g726_configure(struct processing_module *mod, struct ffmpeg_enc_data *e) +{ + struct ffmpeg_dec_comp_data *cd = module_get_private_data(mod); + struct ffmpeg_enc_g726_data *g726 = e->codec_priv; + struct comp_dev *dev = mod->dev; + uint32_t enc_rate = e->req_rate ? e->req_rate : cd->out_rate; + uint32_t cs; + int ret; + + if (e->req_channels > 1) { + comp_err(dev, "G.726 encoder only supports mono (got %u ch)", + e->req_channels); + return -EINVAL; + } + + if (enc_rate == cd->out_rate) { + g726->decimate = 1; + } else if (cd->out_rate == 48000 && enc_rate == 16000) { + g726->decimate = 3; + } else if (cd->out_rate == 48000 && enc_rate == 8000) { + g726->decimate = 6; + } else { + comp_err(dev, "G.726 cannot convert %u Hz source to %u Hz", + cd->out_rate, enc_rate); + return -EINVAL; + } + + if (!e->req_bit_rate) { + cs = G726_DEFAULT_CODE_SIZE; + } else { + cs = (e->req_bit_rate + enc_rate / 2) / enc_rate; + if (cs < 2 || cs > 5 || cs * enc_rate != e->req_bit_rate) { + comp_err(dev, "unsupported G.726 encoder bit_rate %u at %u Hz", + e->req_bit_rate, enc_rate); + return -EINVAL; + } + } + + if (avcodec_is_open(g726->avctx)) { + avcodec_free_context(&g726->avctx); + g726->avctx = avcodec_alloc_context3(g726->codec); + if (!g726->avctx) { + return -ENOMEM; + } + } + + g726->enc_rate = enc_rate; + g726->avctx->strict_std_compliance = FF_COMPLIANCE_UNOFFICIAL; + g726->avctx->sample_rate = enc_rate; + av_channel_layout_default(&g726->avctx->ch_layout, 1); + g726->avctx->sample_fmt = AV_SAMPLE_FMT_S16; + g726->avctx->bit_rate = (int64_t)cs * enc_rate; + g726->avctx->thread_count = 1; + + ret = avcodec_open2(g726->avctx, g726->codec, NULL); + if (ret < 0) { + comp_err(dev, "avcodec_open2 (G.726 encoder) failed %d", ret); + return -EIO; + } + + /* Override large default frame_size so streaming packets stay within SOF module heap */ + g726->avctx->frame_size = G726_ENC_FRAME_SAMPLES; + e->nb = G726_ENC_FRAME_SAMPLES * g726->decimate; + return 0; +} + +static int ffmpeg_enc_g726_encode_frame(struct processing_module *mod, struct ffmpeg_enc_data *e) +{ + struct ffmpeg_dec_comp_data *cd = module_get_private_data(mod); + struct ffmpeg_enc_g726_data *g726 = e->codec_priv; + struct comp_dev *dev = mod->dev; + int ret; + + g726->frame->nb_samples = G726_ENC_FRAME_SAMPLES; + g726->frame->format = g726->avctx->sample_fmt; + g726->frame->sample_rate = g726->enc_rate; + av_channel_layout_copy(&g726->frame->ch_layout, &g726->avctx->ch_layout); + if (av_frame_get_buffer(g726->frame, 0) < 0) { + return -ENOMEM; + } + + if (g726->decimate == 6) { + ffmpeg_dec_pcm_decimate_6to1_s16_mono(e->pcm_in, + (int16_t *)g726->frame->data[0], + e->src_fmt, cd->out_channels, + G726_ENC_FRAME_SAMPLES, + e->decim_hist); + } else if (g726->decimate == 3) { + ffmpeg_dec_pcm_decimate_3to1_s16_mono(e->pcm_in, + (int16_t *)g726->frame->data[0], + e->src_fmt, cd->out_channels, + G726_ENC_FRAME_SAMPLES, + e->decim_hist); + } else { + ret = ffmpeg_dec_pcm_to_frame(e->pcm_in, g726->frame, + e->src_fmt, cd->out_channels, + G726_ENC_FRAME_SAMPLES); + if (ret < 0) { + av_frame_unref(g726->frame); + return ret; + } + } + + ret = avcodec_send_frame(g726->avctx, g726->frame); + av_frame_unref(g726->frame); + if (ret < 0) { + comp_err(dev, "avcodec_send_frame (G.726) failed %d", ret); + return -EIO; + } + + while (ret >= 0) { + ret = avcodec_receive_packet(g726->avctx, g726->pkt); + if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) { + break; + } + if (ret < 0) { + comp_err(dev, "avcodec_receive_packet (G.726) failed %d", ret); + return -EIO; + } + if (g726->pkt->size > 0) { + ffmpeg_enc_stage_packet(mod, e, g726->pkt->data, + (size_t)g726->pkt->size); + } + av_packet_unref(g726->pkt); + ret = 0; + } + + return 0; +} + +static void ffmpeg_enc_g726_reset(struct processing_module *mod, struct ffmpeg_enc_data *e) +{ + struct ffmpeg_enc_g726_data *g726 = e->codec_priv; + + (void)mod; + if (!g726) { + return; + } + + if (g726->frame) { + av_frame_unref(g726->frame); + } + if (g726->pkt) { + av_packet_unref(g726->pkt); + } +} + +static void ffmpeg_enc_g726_free(struct processing_module *mod, struct ffmpeg_enc_data *e) +{ + struct ffmpeg_enc_g726_data *g726 = e->codec_priv; + + if (!g726) { + return; + } + + if (g726->frame) { + av_frame_free(&g726->frame); + } + if (g726->pkt) { + av_packet_free(&g726->pkt); + } + if (g726->avctx) { + avcodec_free_context(&g726->avctx); + } + mod_free(mod, g726); + e->codec_priv = NULL; +} + +const struct ffmpeg_enc_codec_ops ffmpeg_enc_g726_ops = { + .id = FFMPEG_DEC_CODEC_G726, + .snd_codec_id = SND_AUDIOCODEC_G726, + .name = "g726", + .supports_mono_downmix = true, + .supports_decim_3to1 = true, + .supports_decim_6to1 = true, + .validate_params = ffmpeg_enc_g726_validate_params, + .init = ffmpeg_enc_g726_init, + .configure = ffmpeg_enc_g726_configure, + .encode_frame = ffmpeg_enc_g726_encode_frame, + .reset = ffmpeg_enc_g726_reset, + .free = ffmpeg_enc_g726_free, +}; diff --git a/src/audio/ffmpeg_dec/ffmpeg-enc-sbc.c b/src/audio/ffmpeg_dec/ffmpeg-enc-sbc.c index 1e67b77765b8..1db3550c7203 100644 --- a/src/audio/ffmpeg_dec/ffmpeg-enc-sbc.c +++ b/src/audio/ffmpeg_dec/ffmpeg-enc-sbc.c @@ -241,6 +241,8 @@ const struct ffmpeg_enc_codec_ops ffmpeg_enc_sbc_ops = { .id = FFMPEG_DEC_CODEC_SBC, .snd_codec_id = SND_AUDIOCODEC_SBC, .name = "sbc", + .supports_mono_downmix = true, + .supports_decim_3to1 = true, .validate_params = ffmpeg_enc_sbc_validate_params, .init = ffmpeg_enc_sbc_init, .configure = ffmpeg_enc_sbc_configure, diff --git a/src/audio/ffmpeg_dec/ffmpeg-encode.c b/src/audio/ffmpeg_dec/ffmpeg-encode.c index 3e2fec68ccf9..495b23734c4f 100644 --- a/src/audio/ffmpeg_dec/ffmpeg-encode.c +++ b/src/audio/ffmpeg_dec/ffmpeg-encode.c @@ -57,6 +57,18 @@ static const struct ffmpeg_enc_codec_ops * const ffmpeg_enc_codecs[] = { #if defined(CONFIG_FFMPEG_ENC_APTX) &ffmpeg_enc_aptx_ops, #endif +#if defined(CONFIG_FFMPEG_ENC_APTX_HD) + &ffmpeg_enc_aptx_hd_ops, +#endif +#if defined(CONFIG_FFMPEG_ENC_G726) + &ffmpeg_enc_g726_ops, +#endif +#if defined(CONFIG_FFMPEG_ENC_ALAW) + &ffmpeg_enc_alaw_ops, +#endif +#if defined(CONFIG_FFMPEG_ENC_MULAW) + &ffmpeg_enc_mulaw_ops, +#endif }; static const struct ffmpeg_enc_codec_ops * @@ -352,14 +364,14 @@ static int ffmpeg_enc_configure(struct processing_module *mod, struct sof_source return -EINVAL; } - if ((e->req_rate && e->req_rate != cd->out_rate && - !(e->req_rate == 16000 && cd->out_rate == 48000 && - (cd->codec == FFMPEG_DEC_CODEC_G722 || - (cd->codec == FFMPEG_DEC_CODEC_SBC && e->req_profile == 1)))) || - (e->req_channels && e->req_channels != cd->out_channels && - !(e->req_channels == 1 && - (cd->codec == FFMPEG_DEC_CODEC_G722 || - cd->codec == FFMPEG_DEC_CODEC_SBC)))) { + bool rate_ok = (!e->req_rate || e->req_rate == cd->out_rate || + (cd->out_rate == 48000 && + ((e->req_rate == 16000 && e->codec_ops->supports_decim_3to1) || + (e->req_rate == 8000 && e->codec_ops->supports_decim_6to1)))); + bool ch_ok = (!e->req_channels || e->req_channels == cd->out_channels || + (e->req_channels == 1 && e->codec_ops->supports_mono_downmix)); + + if (!rate_ok || !ch_ok) { comp_err(dev, "ffmpeg_enc: requested %u Hz/%u ch does not match source %u Hz/%u ch", e->req_rate, e->req_channels, cd->out_rate, cd->out_channels); return -EINVAL; diff --git a/src/audio/ffmpeg_dec/ffmpeg.cmake b/src/audio/ffmpeg_dec/ffmpeg.cmake index b558d95b5ebd..f06c95456fde 100644 --- a/src/audio/ffmpeg_dec/ffmpeg.cmake +++ b/src/audio/ffmpeg_dec/ffmpeg.cmake @@ -56,6 +56,18 @@ endif() if(CONFIG_FFMPEG_DEC_APTX) list(APPEND _ff_decoders aptx) endif() +if(CONFIG_FFMPEG_DEC_APTX_HD) + list(APPEND _ff_decoders aptx_hd) +endif() +if(CONFIG_FFMPEG_DEC_G726) + list(APPEND _ff_decoders adpcm_g726 adpcm_g726le) +endif() +if(CONFIG_FFMPEG_DEC_ALAW) + list(APPEND _ff_decoders pcm_alaw) +endif() +if(CONFIG_FFMPEG_DEC_MULAW) + list(APPEND _ff_decoders pcm_mulaw) +endif() if(CONFIG_FFMPEG_ENC_FLAC) list(APPEND _ff_encoders flac) endif() @@ -71,6 +83,18 @@ endif() if(CONFIG_FFMPEG_ENC_APTX) list(APPEND _ff_encoders aptx) endif() +if(CONFIG_FFMPEG_ENC_APTX_HD) + list(APPEND _ff_encoders aptx_hd) +endif() +if(CONFIG_FFMPEG_ENC_G726) + list(APPEND _ff_encoders adpcm_g726 adpcm_g726le) +endif() +if(CONFIG_FFMPEG_ENC_ALAW) + list(APPEND _ff_encoders pcm_alaw) +endif() +if(CONFIG_FFMPEG_ENC_MULAW) + list(APPEND _ff_encoders pcm_mulaw) +endif() # At least one decoder, encoder, or filter must be selected. if(NOT _ff_decoders AND NOT _ff_encoders AND NOT CONFIG_FFMPEG_ENC_MP3 AND NOT CONFIG_FFMPEG_ENC_AAC AND NOT CONFIG_FFMPEG_BUILD_AVFILTER) message(FATAL_ERROR "ffmpeg_dec: no decoder, encoder, or filter selected (Kconfig FFMPEG_DEC_*/FFMPEG_ENC_*/FFMPEG_FILTER_*)") diff --git a/src/audio/ffmpeg_dec/ffmpeg_dec-aptx.c b/src/audio/ffmpeg_dec/ffmpeg_dec-aptx.c index 2afd06f56e38..78be4261df87 100644 --- a/src/audio/ffmpeg_dec/ffmpeg_dec-aptx.c +++ b/src/audio/ffmpeg_dec/ffmpeg_dec-aptx.c @@ -2,7 +2,7 @@ // // Copyright(c) 2026 Intel Corporation. // -// Bluetooth aptX per-codec packet alignment and frame sizing for ffmpeg_dec. +/* Bluetooth aptX / aptX HD per-codec packet alignment and frame sizing for ffmpeg_dec. */ #include #include @@ -14,6 +14,7 @@ #define APTX_DEC_MAX_SAMPLES 1024 #define APTX_BLOCK_ALIGN 4 +#define APTX_HD_BLOCK_ALIGN 6 #define APTX_DEFAULT_CHANNELS 2 #define APTX_DEFAULT_RATE 48000 @@ -24,17 +25,19 @@ static uint32_t ffmpeg_dec_aptx_max_frame_samples(const struct ffmpeg_dec_comp_d } /* - * Each 4-byte aptX block (two 16-bit codewords, L+R) decodes to 4 stereo PCM - * samples. Clamp each packet to 1024 bytes and align down to a multiple of 4 - * bytes so aptx_decode_frame never sees a partial codeword. + * Each aptX block (4 bytes) or aptX HD block (6 bytes) decodes to 4 stereo PCM + * samples. Clamp each packet to 1024 bytes and align down to a whole block + * (1024 bytes for aptX, 1020 bytes for aptX HD) so the decoder never sees a + * partial codeword. */ static size_t ffmpeg_dec_aptx_limit_packet_size(const struct ffmpeg_dec_comp_data *cd, size_t pkt_len) { + size_t align = (cd->codec == FFMPEG_DEC_CODEC_APTX_HD) ? + APTX_HD_BLOCK_ALIGN : APTX_BLOCK_ALIGN; size_t clamped = pkt_len > APTX_DEC_MAX_SAMPLES ? APTX_DEC_MAX_SAMPLES : pkt_len; - (void)cd; - return clamped & ~(size_t)(APTX_BLOCK_ALIGN - 1); + return clamped - (clamped % align); } #if !defined(CONFIG_COMP_FFMPEG_DEC_STUB) @@ -52,6 +55,7 @@ static int ffmpeg_dec_aptx_configure_avctx(struct processing_module *mod, void * } #endif +#if defined(CONFIG_FFMPEG_DEC_APTX) || defined(CONFIG_COMP_FFMPEG_DEC_STUB) const struct ffmpeg_dec_codec_ops ffmpeg_dec_aptx_ops = { .id = FFMPEG_DEC_CODEC_APTX, .snd_codec_id = SND_AUDIOCODEC_APTX, @@ -63,3 +67,18 @@ const struct ffmpeg_dec_codec_ops ffmpeg_dec_aptx_ops = { #endif .limit_packet_size = ffmpeg_dec_aptx_limit_packet_size, }; +#endif + +#if defined(CONFIG_FFMPEG_DEC_APTX_HD) || defined(CONFIG_COMP_FFMPEG_DEC_STUB) +const struct ffmpeg_dec_codec_ops ffmpeg_dec_aptx_hd_ops = { + .id = FFMPEG_DEC_CODEC_APTX_HD, + .snd_codec_id = SND_AUDIOCODEC_APTX_HD, + .name = "aptx_hd", + .decoder_name = "aptx_hd", + .max_frame_samples = ffmpeg_dec_aptx_max_frame_samples, +#if !defined(CONFIG_COMP_FFMPEG_DEC_STUB) + .configure_avctx = ffmpeg_dec_aptx_configure_avctx, +#endif + .limit_packet_size = ffmpeg_dec_aptx_limit_packet_size, +}; +#endif diff --git a/src/audio/ffmpeg_dec/ffmpeg_dec-convert.c b/src/audio/ffmpeg_dec/ffmpeg_dec-convert.c index ba697f3fb496..b1d0df81730d 100644 --- a/src/audio/ffmpeg_dec/ffmpeg_dec-convert.c +++ b/src/audio/ffmpeg_dec/ffmpeg_dec-convert.c @@ -446,50 +446,101 @@ static inline int16_t pcm_frame_to_s16_mono(const uint8_t *in, int idx, return (int16_t)Q_SHIFT((int32_t)(acc / src_ch), 31, 15); } +static inline int16_t decim_3to1_step(const uint8_t *in, int idx3, int src_fmt, + int src_ch, int16_t hist[FFMPEG_DECIM_3TO1_HIST_LEN]) +{ + int16_t x0 = pcm_frame_to_s16_mono(in, 3 * idx3 + 0, src_fmt, src_ch); + int16_t x1 = pcm_frame_to_s16_mono(in, 3 * idx3 + 1, src_fmt, src_ch); + int16_t x2 = pcm_frame_to_s16_mono(in, 3 * idx3 + 2, src_fmt, src_ch); + /* 13-sample window: hist[0..11] followed by x0, x1, x2 (shifted by 3) */ + int32_t s0 = hist[2]; + int32_t s2 = hist[4]; + int32_t s3 = hist[5]; + int32_t s4 = hist[6]; + int32_t s5 = hist[7]; + int32_t s6 = hist[8]; + int32_t s7 = hist[9]; + int32_t s8 = hist[10]; + int32_t s9 = hist[11]; + int32_t s10 = x0; + int32_t s12 = x2; + int32_t acc; + int k; + + /* + * 13-tap symmetric linear-phase FIR low-pass filter + * h = [-208, 0, 1096, 2272, 4344, 5840, 6080, 5840, 4344, 2272, 1096, 0, -208] + * Unity DC gain (32768), exact notch at 12 kHz, -30 dB at 16 kHz. + */ + acc = -208 * (s0 + s12) + + 1096 * (s2 + s10) + + 2272 * (s3 + s9) + + 4344 * (s4 + s8) + + 5840 * (s5 + s7) + + 6080 * s6; + acc = (acc + 16384) >> 15; + + for (k = 0; k < 9; k++) { + hist[k] = hist[k + 3]; + } + hist[9] = x0; + hist[10] = x1; + hist[11] = x2; + + return sat_int16(acc); +} + void ffmpeg_dec_pcm_decimate_3to1_s16_mono(const uint8_t *in, int16_t *dst, int src_fmt, int src_ch, int nb_out_samples, int16_t hist[FFMPEG_DECIM_3TO1_HIST_LEN]) { - int i, k; + int i; for (i = 0; i < nb_out_samples; i++) { - int16_t x0 = pcm_frame_to_s16_mono(in, 3 * i + 0, src_fmt, src_ch); - int16_t x1 = pcm_frame_to_s16_mono(in, 3 * i + 1, src_fmt, src_ch); - int16_t x2 = pcm_frame_to_s16_mono(in, 3 * i + 2, src_fmt, src_ch); - /* 13-sample window: hist[0..11] followed by x0, x1, x2 (shifted by 3) */ - int32_t s0 = hist[2]; - int32_t s2 = hist[4]; - int32_t s3 = hist[5]; - int32_t s4 = hist[6]; - int32_t s5 = hist[7]; - int32_t s6 = hist[8]; - int32_t s7 = hist[9]; - int32_t s8 = hist[10]; - int32_t s9 = hist[11]; - int32_t s10 = x0; - int32_t s12 = x2; + dst[i] = decim_3to1_step(in, i, src_fmt, src_ch, hist); + } +} + +void ffmpeg_dec_pcm_decimate_6to1_s16_mono(const uint8_t *in, int16_t *dst, + int src_fmt, int src_ch, + int nb_out_samples, + int16_t hist[FFMPEG_DECIM_6TO1_HIST_LEN]) +{ + /* + * Second stage (16->8 kHz): 29-tap symmetric Kaiser-windowed linear-phase + * low-pass FIR (fc = 0.225 * 16 kHz = 3.6 kHz, beta = 5.2, unity Q15 gain + * 32768). Combined with decim_3to1_step(): -22.9 dB at 4 kHz Nyquist, + * -72.6 dB at 5 kHz, >63 dB stopband rejection above 4.5 kHz. + */ + static const int32_t h[15] = { + 19, -24, -96, 27, 266, 62, -543, -364, + 899, 1086, -1263, -2781, 1537, 10184, 14750, + }; + int16_t *hist2 = &hist[FFMPEG_DECIM_3TO1_HIST_LEN]; + int i; + + for (i = 0; i < nb_out_samples; i++) { + int16_t y0 = decim_3to1_step(in, 2 * i + 0, src_fmt, src_ch, hist); + int16_t y1 = decim_3to1_step(in, 2 * i + 1, src_fmt, src_ch, hist); int32_t acc; + int k; - /* - * 13-tap symmetric linear-phase FIR low-pass filter - * h = [-208, 0, 1096, 2272, 4344, 5840, 6080, 5840, 4344, 2272, 1096, 0, -208] - * Unity DC gain (32768), exact notch at 12 kHz, -30 dB at 16 kHz. - */ - acc = -208 * (s0 + s12) + - 1096 * (s2 + s10) + - 2272 * (s3 + s9) + - 4344 * (s4 + s8) + - 5840 * (s5 + s7) + - 6080 * s6; + acc = h[0] * ((int32_t)hist2[0] + (int32_t)y1) + + h[1] * ((int32_t)hist2[1] + (int32_t)y0) + + h[14] * (int32_t)hist2[14]; + for (k = 2; k < 14; k++) { + acc += h[k] * ((int32_t)hist2[k] + (int32_t)hist2[28 - k]); + } acc = (acc + 16384) >> 15; - dst[i] = sat_int16(acc); - for (k = 0; k < 9; k++) - hist[k] = hist[k + 3]; - hist[9] = x0; - hist[10] = x1; - hist[11] = x2; + for (k = 0; k < FFMPEG_DECIM_2TO1_HIST_LEN - 2; k++) { + hist2[k] = hist2[k + 2]; + } + hist2[FFMPEG_DECIM_2TO1_HIST_LEN - 2] = y0; + hist2[FFMPEG_DECIM_2TO1_HIST_LEN - 1] = y1; + + dst[i] = sat_int16(acc); } } diff --git a/src/audio/ffmpeg_dec/ffmpeg_dec-convert.h b/src/audio/ffmpeg_dec/ffmpeg_dec-convert.h index 4a3dd7d3f047..9f94080efb1d 100644 --- a/src/audio/ffmpeg_dec/ffmpeg_dec-convert.h +++ b/src/audio/ffmpeg_dec/ffmpeg_dec-convert.h @@ -28,7 +28,15 @@ int ffmpeg_dec_convert_frame(const AVFrame *frame, uint8_t *out, size_t out_size, int sink_fmt, int channels); +#ifndef FFMPEG_DECIM_3TO1_HIST_LEN #define FFMPEG_DECIM_3TO1_HIST_LEN 12 +#endif +#ifndef FFMPEG_DECIM_2TO1_HIST_LEN +#define FFMPEG_DECIM_2TO1_HIST_LEN 27 +#endif +#ifndef FFMPEG_DECIM_6TO1_HIST_LEN +#define FFMPEG_DECIM_6TO1_HIST_LEN (FFMPEG_DECIM_3TO1_HIST_LEN + FFMPEG_DECIM_2TO1_HIST_LEN) +#endif /** * ffmpeg_dec_pcm_to_frame - Convert interleaved SOF PCM into an AVFrame. @@ -57,6 +65,20 @@ void ffmpeg_dec_pcm_decimate_3to1_s16_mono(const uint8_t *in, int16_t *dst, int nb_out_samples, int16_t hist[FFMPEG_DECIM_3TO1_HIST_LEN]); +/** + * ffmpeg_dec_pcm_decimate_6to1_s16_mono - Anti-aliased 6:1 (48->8 kHz) mono S16 decimator. + * @in: Source interleaved 48 kHz PCM buffer (6 * @nb_out_samples frames). + * @dst: Destination 8 kHz mono S16 buffer (@nb_out_samples samples). + * @src_fmt: SOF valid sample format (SOF_IPC_FRAME_S16_LE / S24_4LE / S32_LE). + * @src_ch: Input channel count. + * @nb_out_samples: Number of 8 kHz output samples to produce. + * @hist: Stateful 39-sample two-stage FIR delay line preserved across frames. + */ +void ffmpeg_dec_pcm_decimate_6to1_s16_mono(const uint8_t *in, int16_t *dst, + int src_fmt, int src_ch, + int nb_out_samples, + int16_t hist[FFMPEG_DECIM_6TO1_HIST_LEN]); + /* HiFi SIMD helpers implemented in ffmpeg-convert-hifi.c */ int ffmpeg_hifi_fltp_to_s32(const float *const *src, int32_t *dst, int channels, int nb_samples); diff --git a/src/audio/ffmpeg_dec/ffmpeg_dec-ffmpeg.c b/src/audio/ffmpeg_dec/ffmpeg_dec-ffmpeg.c index 15a3f0c2b63c..69c6db82868d 100644 --- a/src/audio/ffmpeg_dec/ffmpeg_dec-ffmpeg.c +++ b/src/audio/ffmpeg_dec/ffmpeg_dec-ffmpeg.c @@ -90,9 +90,9 @@ struct ffmpeg_dec_ffmpeg_data { }; /* Map the SOF codec enum to a libavcodec decoder id. */ -static enum AVCodecID ffmpeg_dec_av_codec_id(enum ffmpeg_dec_codec codec) +static enum AVCodecID ffmpeg_dec_av_codec_id(const struct ffmpeg_dec_comp_data *cd) { - switch (codec) { + switch (cd->codec) { #if defined(CONFIG_FFMPEG_DEC_FLAC) case FFMPEG_DEC_CODEC_FLAC: return AV_CODEC_ID_FLAC; @@ -124,6 +124,23 @@ static enum AVCodecID ffmpeg_dec_av_codec_id(enum ffmpeg_dec_codec codec) #if defined(CONFIG_FFMPEG_DEC_APTX) case FFMPEG_DEC_CODEC_APTX: return AV_CODEC_ID_APTX; +#endif +#if defined(CONFIG_FFMPEG_DEC_APTX_HD) + case FFMPEG_DEC_CODEC_APTX_HD: + return AV_CODEC_ID_APTX_HD; +#endif +#if defined(CONFIG_FFMPEG_DEC_G726) + case FFMPEG_DEC_CODEC_G726: + return (cd->req_profile == 1) ? AV_CODEC_ID_ADPCM_G726LE : + AV_CODEC_ID_ADPCM_G726; +#endif +#if defined(CONFIG_FFMPEG_DEC_ALAW) + case FFMPEG_DEC_CODEC_ALAW: + return AV_CODEC_ID_PCM_ALAW; +#endif +#if defined(CONFIG_FFMPEG_DEC_MULAW) + case FFMPEG_DEC_CODEC_MULAW: + return AV_CODEC_ID_PCM_MULAW; #endif default: return AV_CODEC_ID_NONE; @@ -148,9 +165,11 @@ static int ffmpeg_dec_ff_init(struct processing_module *mod) if (!ff) return -ENOMEM; - id = ffmpeg_dec_av_codec_id(cd->codec); - if (cd->codec_ops && cd->codec_ops->decoder_name) + id = ffmpeg_dec_av_codec_id(cd); + if (cd->codec_ops && cd->codec_ops->decoder_name && + cd->codec != FFMPEG_DEC_CODEC_G726) { ff->codec = avcodec_find_decoder_by_name(cd->codec_ops->decoder_name); + } if (!ff->codec) ff->codec = avcodec_find_decoder(id); if (!ff->codec) { @@ -372,7 +391,8 @@ static int ffmpeg_dec_ff_decode(struct processing_module *mod, } if (raw_len >= cap || (cd->codec_ops && cd->codec_ops->limit_packet_size)) { - comp_err(dev, "unparsed packet reassembly buffer full or invalid block"); + comp_err(dev, "unparsed decode failed %d (raw=%zu pkt=%zu prev=%zu)", + ret, raw_len, pkt_len, prev_len); ff->pktbuf_len = 0; return -EIO; } diff --git a/src/audio/ffmpeg_dec/ffmpeg_dec-g711.c b/src/audio/ffmpeg_dec/ffmpeg_dec-g711.c new file mode 100644 index 000000000000..4d6c1c0093d0 --- /dev/null +++ b/src/audio/ffmpeg_dec/ffmpeg_dec-g711.c @@ -0,0 +1,86 @@ +// SPDX-License-Identifier: BSD-3-Clause +/* + * Copyright(c) 2026 Intel Corporation. + * + * ITU-T G.711 (A-law and mu-law) per-codec configuration for ffmpeg_dec. + */ + +#include +#include +#include +#if !defined(CONFIG_COMP_FFMPEG_DEC_STUB) +#include +#include +#endif +#include "ffmpeg_dec.h" + +LOG_MODULE_DECLARE(ffmpeg_dec, CONFIG_SOF_LOG_LEVEL); + +#define G711_DEFAULT_RATE 8000 +#define G711_DEC_MAX_SAMPLES 1024 + +static uint32_t ffmpeg_dec_g711_max_frame_samples(const struct ffmpeg_dec_comp_data *cd) +{ + (void)cd; + return G711_DEC_MAX_SAMPLES; +} + +static size_t ffmpeg_dec_g711_limit_packet_size(const struct ffmpeg_dec_comp_data *cd, + size_t pkt_len) +{ + uint32_t ch = cd->req_channels ? cd->req_channels : + (cd->out_channels ? cd->out_channels : 1); + size_t max_bytes = G711_DEC_MAX_SAMPLES * (size_t)ch; + size_t clamped = pkt_len > max_bytes ? max_bytes : pkt_len; + + return clamped - (clamped % (size_t)ch); +} + +#if !defined(CONFIG_COMP_FFMPEG_DEC_STUB) +static int ffmpeg_dec_g711_configure_avctx(struct processing_module *mod, void *ctx) +{ + struct ffmpeg_dec_comp_data *cd = module_get_private_data(mod); + AVCodecContext *avctx = ctx; + uint32_t ch = cd->req_channels ? cd->req_channels : + (cd->out_channels ? cd->out_channels : 1); + uint32_t rate = cd->req_rate ? cd->req_rate : + (cd->out_rate ? cd->out_rate : G711_DEFAULT_RATE); + + if (ch < 1 || ch > 2) { + comp_err(mod->dev, "unsupported G.711 channels %u", ch); + return -EINVAL; + } + + avctx->sample_rate = rate; + av_channel_layout_default(&avctx->ch_layout, ch); + return 0; +} +#endif + +#if defined(CONFIG_FFMPEG_DEC_ALAW) || defined(CONFIG_COMP_FFMPEG_DEC_STUB) +const struct ffmpeg_dec_codec_ops ffmpeg_dec_alaw_ops = { + .id = FFMPEG_DEC_CODEC_ALAW, + .snd_codec_id = SND_AUDIOCODEC_ALAW, + .name = "alaw", + .decoder_name = "pcm_alaw", + .max_frame_samples = ffmpeg_dec_g711_max_frame_samples, +#if !defined(CONFIG_COMP_FFMPEG_DEC_STUB) + .configure_avctx = ffmpeg_dec_g711_configure_avctx, +#endif + .limit_packet_size = ffmpeg_dec_g711_limit_packet_size, +}; +#endif + +#if defined(CONFIG_FFMPEG_DEC_MULAW) || defined(CONFIG_COMP_FFMPEG_DEC_STUB) +const struct ffmpeg_dec_codec_ops ffmpeg_dec_mulaw_ops = { + .id = FFMPEG_DEC_CODEC_MULAW, + .snd_codec_id = SND_AUDIOCODEC_MULAW, + .name = "mulaw", + .decoder_name = "pcm_mulaw", + .max_frame_samples = ffmpeg_dec_g711_max_frame_samples, +#if !defined(CONFIG_COMP_FFMPEG_DEC_STUB) + .configure_avctx = ffmpeg_dec_g711_configure_avctx, +#endif + .limit_packet_size = ffmpeg_dec_g711_limit_packet_size, +}; +#endif diff --git a/src/audio/ffmpeg_dec/ffmpeg_dec-g726.c b/src/audio/ffmpeg_dec/ffmpeg_dec-g726.c new file mode 100644 index 000000000000..e959f5d7455d --- /dev/null +++ b/src/audio/ffmpeg_dec/ffmpeg_dec-g726.c @@ -0,0 +1,134 @@ +// SPDX-License-Identifier: BSD-3-Clause +/* + * Copyright(c) 2026 Intel Corporation. + * + * ITU-T G.726 ADPCM per-codec packet sizing and configuration for ffmpeg_dec. + */ + +#include +#include +#include +#if !defined(CONFIG_COMP_FFMPEG_DEC_STUB) +#include +#include +#endif +#include "ffmpeg_dec.h" + +LOG_MODULE_DECLARE(ffmpeg_dec, CONFIG_SOF_LOG_LEVEL); + +#define G726_DEFAULT_RATE 8000 +#define G726_DEFAULT_CODE_SIZE 4 +#define G726_DEC_MAX_SAMPLES 1024 + +static int ffmpeg_dec_g726_code_size(const struct ffmpeg_dec_comp_data *cd, uint32_t rate) +{ + uint32_t cs; + + if (!cd->req_bit_rate) { + return G726_DEFAULT_CODE_SIZE; + } + + cs = (cd->req_bit_rate + rate / 2) / rate; + if (cs < 2 || cs > 5 || cs * rate != cd->req_bit_rate) { + return -EINVAL; + } + + return (int)cs; +} + +static int ffmpeg_dec_g726_validate_params(struct processing_module *mod, + const struct snd_codec *codec_params) +{ + uint32_t req_ch = codec_params->ch_in ? + codec_params->ch_in : codec_params->ch_out; + + if (codec_params->profile > 1) { + comp_err(mod->dev, "unsupported G.726 profile %u", codec_params->profile); + return -EINVAL; + } + + if (req_ch > 1) { + comp_err(mod->dev, "G.726 decoder only supports mono (got %u ch)", req_ch); + return -EINVAL; + } + + return 0; +} + +static uint32_t ffmpeg_dec_g726_max_frame_samples(const struct ffmpeg_dec_comp_data *cd) +{ + (void)cd; + return G726_DEC_MAX_SAMPLES; +} + +/* + * Align unparsed G.726 chunks to whole byte-aligned code blocks (1 byte for + * 2/4-bit codes, 3 bytes for 3-bit codes, 5 bytes for 5-bit codes) and cap + * packet length so decoded output stays within G726_DEC_MAX_SAMPLES. + */ +static size_t ffmpeg_dec_g726_limit_packet_size(const struct ffmpeg_dec_comp_data *cd, + size_t pkt_len) +{ + uint32_t rate = cd->req_rate ? cd->req_rate : + (cd->out_rate ? cd->out_rate : G726_DEFAULT_RATE); + int cs = ffmpeg_dec_g726_code_size(cd, rate); + size_t align; + size_t max_bytes; + size_t clamped; + + if (cs < 2) { + cs = G726_DEFAULT_CODE_SIZE; + } + + align = (cs == 3 || cs == 5) ? (size_t)cs : 1; + max_bytes = (G726_DEC_MAX_SAMPLES * (size_t)cs) / 8; + clamped = pkt_len > max_bytes ? max_bytes : pkt_len; + + return clamped - (clamped % align); +} + +#if !defined(CONFIG_COMP_FFMPEG_DEC_STUB) +static int ffmpeg_dec_g726_configure_avctx(struct processing_module *mod, void *ctx) +{ + struct ffmpeg_dec_comp_data *cd = module_get_private_data(mod); + AVCodecContext *avctx = ctx; + uint32_t rate = cd->req_rate ? cd->req_rate : + (cd->out_rate ? cd->out_rate : G726_DEFAULT_RATE); + int cs; + + if (cd->req_profile > 1) { + comp_err(mod->dev, "unsupported G.726 profile %u", cd->req_profile); + return -EINVAL; + } + + if (cd->req_channels > 1) { + comp_err(mod->dev, "G.726 decoder only supports mono (got %u ch)", + cd->req_channels); + return -EINVAL; + } + + cs = ffmpeg_dec_g726_code_size(cd, rate); + if (cs < 0) { + comp_err(mod->dev, "unsupported G.726 bitrate %u at %u Hz", + cd->req_bit_rate, rate); + return cs; + } + + avctx->sample_rate = rate; + av_channel_layout_default(&avctx->ch_layout, 1); + avctx->bits_per_coded_sample = cs; + return 0; +} +#endif + +const struct ffmpeg_dec_codec_ops ffmpeg_dec_g726_ops = { + .id = FFMPEG_DEC_CODEC_G726, + .snd_codec_id = SND_AUDIOCODEC_G726, + .name = "g726", + .validate_params = ffmpeg_dec_g726_validate_params, + .max_frame_samples = ffmpeg_dec_g726_max_frame_samples, +#if !defined(CONFIG_COMP_FFMPEG_DEC_STUB) + .configure_avctx = ffmpeg_dec_g726_configure_avctx, +#endif + .limit_packet_size = ffmpeg_dec_g726_limit_packet_size, +}; diff --git a/src/audio/ffmpeg_dec/ffmpeg_dec.c b/src/audio/ffmpeg_dec/ffmpeg_dec.c index 81eaab628441..d551ffdffadd 100644 --- a/src/audio/ffmpeg_dec/ffmpeg_dec.c +++ b/src/audio/ffmpeg_dec/ffmpeg_dec.c @@ -240,6 +240,18 @@ static const struct ffmpeg_dec_codec_ops * const ffmpeg_dec_codecs[] = { #if defined(CONFIG_FFMPEG_DEC_APTX) || defined(CONFIG_COMP_FFMPEG_DEC_STUB) &ffmpeg_dec_aptx_ops, #endif +#if defined(CONFIG_FFMPEG_DEC_APTX_HD) || defined(CONFIG_COMP_FFMPEG_DEC_STUB) + &ffmpeg_dec_aptx_hd_ops, +#endif +#if defined(CONFIG_FFMPEG_DEC_G726) || defined(CONFIG_COMP_FFMPEG_DEC_STUB) + &ffmpeg_dec_g726_ops, +#endif +#if defined(CONFIG_FFMPEG_DEC_ALAW) || defined(CONFIG_COMP_FFMPEG_DEC_STUB) + &ffmpeg_dec_alaw_ops, +#endif +#if defined(CONFIG_FFMPEG_DEC_MULAW) || defined(CONFIG_COMP_FFMPEG_DEC_STUB) + &ffmpeg_dec_mulaw_ops, +#endif }; static const struct ffmpeg_dec_codec_ops *ffmpeg_dec_find_codec_ops(enum ffmpeg_dec_codec id) @@ -367,11 +379,23 @@ __cold static int ffmpeg_dec_init(struct processing_module *mod) comp_err(dev, "unsupported or disabled snd_codec id 0x%x", codec_params->id); mod_free(mod, cd); + md->private = NULL; return -ENOTSUP; } + if (ops->validate_params) { + ret = ops->validate_params(mod, codec_params); + if (ret) { + mod_free(mod, cd); + md->private = NULL; + return ret; + } + } cd->codec_ops = ops; cd->codec = ops->id; + cd->req_rate = codec_params->sample_rate; + cd->req_channels = codec_params->ch_in ? codec_params->ch_in : codec_params->ch_out; cd->req_bit_rate = codec_params->bit_rate; + cd->req_profile = codec_params->profile; comp_info(dev, "codec selected from IPC: %s (%d, snd_codec id 0x%x)", cd->codec_ops->name, cd->codec, codec_params->id); } @@ -477,6 +501,15 @@ static int ffmpeg_dec_prepare(struct processing_module *mod, } sink = sinks[0]; + /* + * Compressed inputs have no fixed frame-size alignment for EOS zero-fill: + * clear min_available so comp_check_eos() does not inject 0x00 silence + * blocks into unframed bitstreams (G.711/G.726/aptX) after host EOS. + */ + if (num_of_sources > 0 && sources[0]) { + source_set_min_available(sources[0], 0); + } + /* Cache the PCM format the decoder must produce for the pipeline. */ cd->out_rate = sink_get_rate(sink); cd->out_channels = sink_get_channels(sink); @@ -536,6 +569,9 @@ static int ffmpeg_dec_prepare(struct processing_module *mod, cd->unparsed_skip_to_eof = false; cd->draining = false; cd->drained = false; +#if CONFIG_IPC_MAJOR_4 + cd->eos_sent = false; +#endif return 0; } @@ -591,13 +627,35 @@ static bool ffmpeg_dec_is_ready_to_process(struct processing_module *mod, if (num_of_sources < 1 || num_of_sinks < 1) return false; +#if CONFIG_IPC_MAJOR_4 + /* Do not pull trailing EOS-flush silence blocks once compress drain completed. */ + if (cd->eos_sent) { + return false; + } +#endif + if (sink_get_free_size(sinks[0]) == 0) return false; if (cd->pcm_avail) return true; - return source_get_data_available(sources[0]) > 0; + if (source_get_data_available(sources[0]) > 0) { + return true; + } + +#if CONFIG_IPC_MAJOR_4 + { + enum sof_audio_buffer_state src_state = source_get_state(sources[0]); + + if (src_state == AUDIOBUF_STATE_END_OF_STREAM || + src_state == AUDIOBUF_STATE_END_OF_STREAM_FLUSH) { + return true; + } + } +#endif + + return false; } /** @@ -654,6 +712,13 @@ static int ffmpeg_dec_process(struct processing_module *mod, return 0; } +#if CONFIG_IPC_MAJOR_4 + if (cd->eos_sent) { + source_drop_data(src, source_get_data_available(src)); + return 0; + } +#endif + /* One-time: strip the container header so the parser sees frames. */ if (!cd->hdr_done) { if (cd->codec_ops && cd->codec_ops->skip_container) { @@ -764,6 +829,22 @@ static int ffmpeg_dec_process(struct processing_module *mod, if (ret) { if (direct_sink) sink_commit_buffer(sink, 0); + if (host_eos) { + bool all_zero = true; + + for (size_t i = 0; i < req; i++) { + if (cd->in_buf[i] != 0) { + all_zero = false; + break; + } + } + if (all_zero) { + source_drop_data(src, source_get_data_available(src)); + cd->drained = true; + ffmpeg_dec_signal_eos(mod, sink); + return 0; + } + } comp_err(dev, "decode failed %d", ret); return ret; } @@ -797,7 +878,8 @@ static int ffmpeg_dec_process(struct processing_module *mod, * produced no PCM and no staged PCM remains, enter draining mode to * flush the parser and codec before signaling EOS. */ - if (host_eos && !produced && !cd->pcm_avail) { + if (host_eos && !cd->pcm_avail && + (!produced || source_get_data_available(src) == 0)) { if (!cd->drained) { size_t c_consumed = 0; size_t c_flushed = 0; diff --git a/src/audio/ffmpeg_dec/ffmpeg_dec.h b/src/audio/ffmpeg_dec/ffmpeg_dec.h index e7b3a022bb65..6864f71fff05 100644 --- a/src/audio/ffmpeg_dec/ffmpeg_dec.h +++ b/src/audio/ffmpeg_dec/ffmpeg_dec.h @@ -52,6 +52,10 @@ enum ffmpeg_dec_codec { FFMPEG_DEC_CODEC_SBC, FFMPEG_DEC_CODEC_G722, FFMPEG_DEC_CODEC_APTX, + FFMPEG_DEC_CODEC_APTX_HD, + FFMPEG_DEC_CODEC_G726, + FFMPEG_DEC_CODEC_ALAW, + FFMPEG_DEC_CODEC_MULAW, }; /* Default codec for a new instance, picked from the Kconfig selection until the @@ -73,6 +77,14 @@ enum ffmpeg_dec_codec { #define FFMPEG_DEC_DEFAULT_CODEC FFMPEG_DEC_CODEC_G722 #elif defined(CONFIG_FFMPEG_DEC_APTX) #define FFMPEG_DEC_DEFAULT_CODEC FFMPEG_DEC_CODEC_APTX +#elif defined(CONFIG_FFMPEG_DEC_APTX_HD) +#define FFMPEG_DEC_DEFAULT_CODEC FFMPEG_DEC_CODEC_APTX_HD +#elif defined(CONFIG_FFMPEG_DEC_G726) +#define FFMPEG_DEC_DEFAULT_CODEC FFMPEG_DEC_CODEC_G726 +#elif defined(CONFIG_FFMPEG_DEC_ALAW) +#define FFMPEG_DEC_DEFAULT_CODEC FFMPEG_DEC_CODEC_ALAW +#elif defined(CONFIG_FFMPEG_DEC_MULAW) +#define FFMPEG_DEC_DEFAULT_CODEC FFMPEG_DEC_CODEC_MULAW #else #define FFMPEG_DEC_DEFAULT_CODEC FFMPEG_DEC_CODEC_NONE #endif @@ -137,6 +149,9 @@ struct ffmpeg_enc_codec_ops { enum ffmpeg_dec_codec id; uint32_t snd_codec_id; const char *name; + bool supports_mono_downmix; + bool supports_decim_3to1; + bool supports_decim_6to1; int (*validate_params)(struct processing_module *mod, const struct snd_codec *codec_params); int (*init)(struct processing_module *mod, struct ffmpeg_enc_data *e); @@ -146,6 +161,10 @@ struct ffmpeg_enc_codec_ops { void (*free)(struct processing_module *mod, struct ffmpeg_enc_data *e); }; +#define FFMPEG_DECIM_3TO1_HIST_LEN 12 +#define FFMPEG_DECIM_2TO1_HIST_LEN 27 +#define FFMPEG_DECIM_6TO1_HIST_LEN (FFMPEG_DECIM_3TO1_HIST_LEN + FFMPEG_DECIM_2TO1_HIST_LEN) + /** * struct ffmpeg_enc_data - Common encoder state across all encode codecs. * @codec_ops: Selected per-codec encoder operations. @@ -174,7 +193,7 @@ struct ffmpeg_enc_data { uint32_t req_channels; uint32_t req_ch_mode; uint32_t req_profile; - int16_t decim_hist[12]; + int16_t decim_hist[FFMPEG_DECIM_6TO1_HIST_LEN]; int src_fmt; int frame_bytes; int nb; @@ -194,6 +213,10 @@ extern const struct ffmpeg_enc_codec_ops ffmpeg_enc_alac_ops; extern const struct ffmpeg_enc_codec_ops ffmpeg_enc_sbc_ops; extern const struct ffmpeg_enc_codec_ops ffmpeg_enc_g722_ops; extern const struct ffmpeg_enc_codec_ops ffmpeg_enc_aptx_ops; +extern const struct ffmpeg_enc_codec_ops ffmpeg_enc_aptx_hd_ops; +extern const struct ffmpeg_enc_codec_ops ffmpeg_enc_g726_ops; +extern const struct ffmpeg_enc_codec_ops ffmpeg_enc_alaw_ops; +extern const struct ffmpeg_enc_codec_ops ffmpeg_enc_mulaw_ops; void ffmpeg_enc_pcm_to_s16_interleaved(const uint8_t *pcm_in, int16_t *dst, size_t total_samples, int src_fmt); @@ -227,6 +250,8 @@ struct ffmpeg_dec_codec_ops { uint32_t snd_codec_id; const char *name; const char *decoder_name; + int (*validate_params)(struct processing_module *mod, + const struct snd_codec *codec_params); uint32_t (*max_frame_samples)(const struct ffmpeg_dec_comp_data *cd); int (*skip_container)(struct processing_module *mod, struct sof_source *src); void (*prepare_extradata)(struct processing_module *mod); @@ -244,6 +269,10 @@ extern const struct ffmpeg_dec_codec_ops ffmpeg_dec_alac_ops; extern const struct ffmpeg_dec_codec_ops ffmpeg_dec_sbc_ops; extern const struct ffmpeg_dec_codec_ops ffmpeg_dec_g722_ops; extern const struct ffmpeg_dec_codec_ops ffmpeg_dec_aptx_ops; +extern const struct ffmpeg_dec_codec_ops ffmpeg_dec_aptx_hd_ops; +extern const struct ffmpeg_dec_codec_ops ffmpeg_dec_g726_ops; +extern const struct ffmpeg_dec_codec_ops ffmpeg_dec_alaw_ops; +extern const struct ffmpeg_dec_codec_ops ffmpeg_dec_mulaw_ops; /** * struct ffmpeg_dec_backend - decode backend operations. @@ -308,7 +337,10 @@ struct ffmpeg_dec_comp_data { size_t extradata_size; uint32_t out_rate; uint32_t out_channels; + uint32_t req_rate; + uint32_t req_channels; uint32_t req_bit_rate; + uint32_t req_profile; int out_frame_fmt; uint32_t out_frame_bytes; bool is_encoder; diff --git a/src/audio/ffmpeg_dec/llext/CMakeLists.txt b/src/audio/ffmpeg_dec/llext/CMakeLists.txt index 06d659fa404d..8d43c1ea8db6 100644 --- a/src/audio/ffmpeg_dec/llext/CMakeLists.txt +++ b/src/audio/ffmpeg_dec/llext/CMakeLists.txt @@ -13,6 +13,8 @@ if(CONFIG_COMP_FFMPEG_DEC_STUB) ../ffmpeg_dec-sbc.c ../ffmpeg_dec-g722.c ../ffmpeg_dec-aptx.c + ../ffmpeg_dec-g726.c + ../ffmpeg_dec-g711.c ../ffmpeg_dec-stub.c LIB openmodules ) @@ -52,9 +54,15 @@ else() if(CONFIG_FFMPEG_DEC_G722) list(APPEND ffmpeg_dec_srcs ../ffmpeg_dec-g722.c) endif() - if(CONFIG_FFMPEG_DEC_APTX) + 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) + 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) @@ -97,9 +105,15 @@ else() if(CONFIG_FFMPEG_ENC_G722) list(APPEND ffmpeg_dec_srcs ../ffmpeg-enc-g722.c) endif() - if(CONFIG_FFMPEG_ENC_APTX) + if(CONFIG_FFMPEG_ENC_APTX OR CONFIG_FFMPEG_ENC_APTX_HD) list(APPEND ffmpeg_dec_srcs ../ffmpeg-enc-aptx.c) endif() + if(CONFIG_FFMPEG_ENC_G726) + list(APPEND ffmpeg_dec_srcs ../ffmpeg-enc-g726.c) + endif() + if(CONFIG_FFMPEG_ENC_ALAW OR CONFIG_FFMPEG_ENC_MULAW) + list(APPEND ffmpeg_dec_srcs ../ffmpeg-enc-g711.c) + endif() # Real libm (newlib double math) LAST so libav*/filter references resolve against it get_filename_component(_ff_libm_dir "${FFMPEG_TARGET_LIBM}" DIRECTORY) diff --git a/src/audio/source_api_helper.c b/src/audio/source_api_helper.c index a6dc56249ee3..d5c6f27b73c3 100644 --- a/src/audio/source_api_helper.c +++ b/src/audio/source_api_helper.c @@ -105,3 +105,4 @@ void source_set_min_available(struct sof_source *source, size_t min_available) { source->min_available = min_available; } +EXPORT_SYMBOL(source_set_min_available); diff --git a/src/include/ipc/compress_params.h b/src/include/ipc/compress_params.h index 77c98d95b87f..a3a86a0eace7 100644 --- a/src/include/ipc/compress_params.h +++ b/src/include/ipc/compress_params.h @@ -81,7 +81,11 @@ #define SND_AUDIOCODEC_SBC ((uint32_t) 0x00000012) #define SND_AUDIOCODEC_G722 ((uint32_t) 0x00000013) #define SND_AUDIOCODEC_APTX ((uint32_t) 0x00000014) -#define SND_AUDIOCODEC_MAX SND_AUDIOCODEC_APTX +#define SND_AUDIOCODEC_APTX_HD ((uint32_t) 0x00000015) +#define SND_AUDIOCODEC_G726 ((uint32_t) 0x00000016) +#define SND_AUDIOCODEC_ALAW ((uint32_t) 0x00000017) +#define SND_AUDIOCODEC_MULAW ((uint32_t) 0x00000018) +#define SND_AUDIOCODEC_MAX SND_AUDIOCODEC_MULAW /* * Profile and modes are listed with bit masks. This allows for a diff --git a/src/kernel/audio/base_fw.c b/src/kernel/audio/base_fw.c index 880306a4a03b..1ec4cf790fe2 100644 --- a/src/kernel/audio/base_fw.c +++ b/src/kernel/audio/base_fw.c @@ -86,6 +86,14 @@ struct sof_ipc4_codec_info_data { */ #define SET_CODEC_INFO_ITEM(codec, dir) (((codec) & 0xff) | (((dir) & 0xf) << 8)) +static inline void add_codec_info_item(struct sof_ipc4_codec_info_data *info, + uint32_t codec, uint32_t dir) +{ + if (info->count < ARRAY_SIZE(info->items)) { + info->items[info->count++] = SET_CODEC_INFO_ITEM(codec, dir); + } +} + static void get_codec_info(struct sof_tlv **tuple) { struct sof_ipc4_codec_info_data codec_info = { 0 }; @@ -190,23 +198,56 @@ static void get_codec_info(struct sof_tlv **tuple) codec_info.items[codec_info.count++] = SET_CODEC_INFO_ITEM(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_COMP_MFCC) + 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. */ - codec_info.items[codec_info.count++] = - SET_CODEC_INFO_ITEM(SND_AUDIOCODEC_BESPOKE, SOF_IPC_STREAM_CAPTURE); + 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. */ - codec_info.items[codec_info.count++] = - SET_CODEC_INFO_ITEM(SND_AUDIOCODEC_OPUS_RAW, SOF_IPC_STREAM_PLAYBACK); + 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) - codec_info.items[codec_info.count++] = - SET_CODEC_INFO_ITEM(SND_AUDIOCODEC_BESPOKE, SOF_IPC_STREAM_PLAYBACK); + 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 if (!codec_info.count)