Skip to content

audio: ffmpeg_dec: add MP3 and AAC-LC encoder mode (libshine, vo-aacenc) and capture topologies - #11414

Draft
lgirdwood wants to merge 4 commits into
thesofproject:mainfrom
lgirdwood:topic/ffmpeg-encoders
Draft

lgirdwood wants to merge 4 commits into
thesofproject:mainfrom
lgirdwood:topic/ffmpeg-encoders

Conversation

@lgirdwood

Copy link
Copy Markdown
Member

Summary

This PR (Phase 3 of the FFmpeg integration series, following #11308, #11334, and #11390) adds fixed-point audio encode support (CONFIG_FFMPEG_DEC_ENCODE_MODE) and compressed capture topologies to the ffmpeg_dec module:

  1. west: manifest: add shine and vo-aacenc projects

    • Adds the SOF forks of shine (thesofproject/shine, sof-hifi branch ab594a0) and vo-aacenc (thesofproject/vo-aacenc, sof-hifi branch c823d30) under modules/audio/shine and modules/audio/vo-aacenc.
  2. audio: base_fw: advertise FFmpeg MP3 and AAC compress capture encode capabilities

    • Advertises SND_AUDIOCODEC_MP3 (guarded by !CONFIG_CADENCE_CODEC_MP3_ENC) and SND_AUDIOCODEC_AAC for SOF_IPC_STREAM_CAPTURE in IPC4_SOF_CODEC_INFO when CONFIG_FFMPEG_ENC_MP3 / CONFIG_FFMPEG_ENC_AAC are enabled.
  3. audio: ffmpeg_dec: add MP3 and AAC-LC encoder mode (libshine, vo-aacenc)

    • Implements src/audio/ffmpeg_dec/ffmpeg_dec-encode.c driving fixed-point MP3 encoding (libshine, 1152-sample frames) and fixed-point AAC-LC ADTS encoding (vo-aacenc, 1024-sample frames) over the DP sink/source .process interface.
    • Selects the encoder codec and bitrate from IPC4 struct snd_codec parameters when provided, rejecting unsupported/disabled codec IDs with -ENOTSUP, and falls back to Kconfig defaults (CONFIG_FFMPEG_ENC_DEFAULT_AAC / CONFIG_FFMPEG_ENC_DEFAULT_MP3) when no IPC codec ID is specified.
    • Cross-builds libshine (shine_ext) and vo-aacenc (vo_aacenc_ext) in ffmpeg.cmake for both built-in (=y) and LLEXT (=m) builds.
  4. topology2: add compressed audio capture pipelines for FFmpeg encoders

    • Adds ffmpeg_enc.conf, compr-capture-ffmpeg.conf, and mixout-capture.conf to tap the HDA analog capture BE via a standalone timer-scheduled mixout into the DP ffmpeg_enc pipeline (PCM ID 51, pipeline ID 91, mixout pipeline ID 93).
    • Adds sof-hda-generic-ffmpeg-mp3-enc-compr and sof-hda-generic-ffmpeg-aac-enc-compr development topology targets.

Add the SOF forks of libshine (fixed-point MP3 encoder) and vo-aacenc
(VisualOn fixed-point AAC-LC encoder) under modules/audio/shine and
modules/audio/vo-aacenc, pinned to their sof-hifi branches on
thesofproject.

shine (sof-hifi):
ab594a0 shine: add Xtensa HiFi accelerated multipliers and optimize quantize loop
a531d18 shine: work around clang/Xtensa backend miscompile in rate-control loop
b917fc4 shine: precompute init tables to avoid runtime pow/sqrt on the DSP

vo-aacenc (sof-hifi):
c823d30 xtensa: optimize basic operators, band energy, and autocorrelation

Signed-off-by: Liam Girdwood <liam.r.girdwood@linux.intel.com>
…capabilities

Advertise SND_AUDIOCODEC_MP3 (guarded by !CONFIG_CADENCE_CODEC_MP3_ENC)
and SND_AUDIOCODEC_AAC for SOF_IPC_STREAM_CAPTURE in IPC4_SOF_CODEC_INFO
when CONFIG_FFMPEG_ENC_MP3 or CONFIG_FFMPEG_ENC_AAC is enabled.

Signed-off-by: Liam Girdwood <liam.r.girdwood@linux.intel.com>
Add encoder mode (CONFIG_FFMPEG_DEC_ENCODE_MODE) to the ffmpeg_dec
module, supporting:
- Fixed-point MP3 encoding via libshine (CONFIG_FFMPEG_ENC_MP3, 1152-sample
  MPEG-1 Layer III frames).
- Fixed-point AAC-LC encoding via vo-aacenc (CONFIG_FFMPEG_ENC_AAC,
  1024-sample ADTS frames).

Key details:
- Driven via the DP sink/source .process interface with lazy encoder
  opening on the DP thread.
- Selects the encoder codec and bit_rate from IPC4 snd_codec parameters
  when provided, rejecting unsupported or disabled codec IDs with
  -ENOTSUP, and falling back to Kconfig default (FFMPEG_ENC_DEFAULT_AAC
  or FFMPEG_ENC_DEFAULT_MP3) when no IPC codec ID is specified.
- Cross-builds libshine and vo-aacenc archives in ffmpeg.cmake for both
  built-in (=y) and LLEXT (=m) builds.

Signed-off-by: Liam Girdwood <liam.r.girdwood@linux.intel.com>
Add compressed audio capture (encode) topology support for the FFmpeg
encoder widget (ffmpeg_enc):
- include/components/ffmpeg_enc.conf: encoder widget class sharing the
  ffmpeg_dec UUID with DAPM widget type 'encoder'.
- include/pipelines/cavs/compr-capture-ffmpeg.conf: DP encoder + aif_out
  host-copier capture pipeline.
- include/pipelines/cavs/mixout-capture.conf: standalone timer-scheduled
  mixout pipeline for cross-pipeline LL->DP capture fan-out.
- platform/intel/compr-capture-ffmpeg.conf & compr-default.conf:
  COMPR_FFMPEG_ENC capture PCM (id 51, pipeline id 91, mixout pipeline id
  93) and per-instance stack/heap sizing (64 KiB stack, 256 KiB heap).
- cavs-mixin-mixout-hda.conf & development/tplg-targets.cmake: mixin
  capture fan-out when COMPR_FFMPEG_ENC is enabled, plus
  sof-hda-generic-ffmpeg-mp3-enc-compr and
  sof-hda-generic-ffmpeg-aac-enc-compr development topology targets.

Signed-off-by: Liam Girdwood <liam.r.girdwood@linux.intel.com>

@lgirdwood lgirdwood left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets also split the media format encoding logic into separate files like we do with decode.

Comment on lines +179 to +190
#if defined(CONFIG_FFMPEG_ENC_DEFAULT_AAC) || \
(defined(CONFIG_FFMPEG_ENC_AAC) && !defined(CONFIG_FFMPEG_ENC_MP3))
e->type = FFMPEG_ENC_TYPE_AAC;
cd->codec = FFMPEG_DEC_CODEC_AAC;
#elif defined(CONFIG_FFMPEG_ENC_MP3)
e->type = FFMPEG_ENC_TYPE_MP3;
cd->codec = FFMPEG_DEC_CODEC_MP3;
#else
e->type = FFMPEG_ENC_TYPE_NONE;
cd->codec = FFMPEG_DEC_CODEC_NONE;
#endif
e->bit_rate = FFMPEG_ENC_DEFAULT_BITRATE;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The encoding type will be set at runtime not at build time. This mean we will have single binaries that can encode multiple formats

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unresolved critical encoder defects can cause out-of-bounds reads, exhaust the MP3 capture heap, and carry prior-recording audio into restarted AAC streams.

8 open issues
What changed in this PR

This PR extends SOF’s FFmpeg module with a build-time encoder mode for MP3 and AAC-LC, adding compressed HDA capture alongside the existing decoder infrastructure.

Changes:

  • Adds fixed-point encoding through pinned libshine and vo-aacenc dependencies.
  • Introduces encoder configuration, processing callbacks, and capture capability reporting.
  • Adds HDA capture routing, reusable topology classes, and development targets.
File Description
west.yml Pins libshine and vo-aacenc dependencies.
tools/​topology/​topology2/​sof-hda-generic.conf Includes capture pipeline classes.
tools/​topology/​topology2/​platform/​intel/​compr-default.conf Defines encoder IDs and resource budgets.
tools/​topology/​topology2/​platform/​intel/​compr-capture-ffmpeg.conf Instantiates compressed capture and routing.
tools/​topology/​topology2/​include/​pipelines/​cavs/​mixout-gain-host-copier-capture.conf Removes the class-level gain-to-copier route.
tools/​topology/​topology2/​include/​pipelines/​cavs/​mixout-capture.conf Adds a standalone timer-scheduled mixout.
tools/​topology/​topology2/​include/​pipelines/​cavs/​compr-capture-ffmpeg.conf Defines the encoder-to-host capture pipeline.
tools/​topology/​topology2/​include/​components/​ffmpeg_enc.conf Defines the encoder widget using the existing module UUID.
tools/​topology/​topology2/​development/​tplg-targets.cmake Adds MP3 and AAC capture targets.
tools/​topology/​topology2/​cavs-mixin-mixout-hda.conf Adds conditional mixer-based capture fan-out.
src/​kernel/​audio/​base_fw.c Advertises MP3 and AAC capture capabilities.
src/​audio/​ffmpeg_dec/​llext/​CMakeLists.txt Adds encoder library build dependencies.
src/​audio/​ffmpeg_dec/​Kconfig Adds encoder selections and build mode.
src/​audio/​ffmpeg_dec/​ffmpeg.cmake Cross-builds the encoder libraries.
src/​audio/​ffmpeg_dec/​ffmpeg_dec.h Declares encoder callbacks and shared copy helpers.
src/​audio/​ffmpeg_dec/​ffmpeg_dec.c Selects encoder callbacks and shares buffer helpers.
src/​audio/​ffmpeg_dec/​ffmpeg_dec-encode.c Implements PCM conversion and compressed encoding.
src/​audio/​ffmpeg_dec/​CMakeLists.txt Integrates encoder sources, headers, and libraries.

Review effort: Balanced

Comment on lines +319 to +321
cd->out_channels = source_get_channels(src);
cd->out_rate = source_get_rate(src);
e->frame_bytes = source_get_frame_bytes(src);
Comment on lines +327 to +328
AACENC_PARAM params = { 0 };

# 48 KiB LL->DP input ring (3x 16 KiB) + ~32 KiB staging (pcm_in, out_stage,
# pcm_s16 / AVFrame) + ~48 KiB libshine / vo-aacenc state fits comfortably
# within 256 KiB.
COMPR_FFMPEG_ENC_HEAP 262144
Comment on lines +400 to +403
add_custom_command(
OUTPUT "${SHINE_INSTALL}/lib/libshine.a" "${CMAKE_CURRENT_BINARY_DIR}/shine_cfgstub.o"
COMMAND sh "${CMAKE_CURRENT_BINARY_DIR}/shine-build.sh"
VERBATIM)
Comment on lines +487 to +490
add_custom_command(
OUTPUT "${VO_AACENC_INSTALL}/lib/libvo-aacenc.a"
COMMAND sh "${CMAKE_CURRENT_BINARY_DIR}/vo-aacenc-build.sh"
VERBATIM)
Comment on lines +201 to +204
case SND_AUDIOCODEC_AAC:
e->type = FFMPEG_ENC_TYPE_AAC;
cd->codec = FFMPEG_DEC_CODEC_AAC;
break;
#endif /* !CONFIG_FFMPEG_DEC_ENCODE_MODE */

/* This defines the module operations */
#if CONFIG_FFMPEG_DEC_ENCODE_MODE
Comment on lines +136 to +143
#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.
*/
codec_info.items[codec_info.count++] =
SET_CODEC_INFO_ITEM(SND_AUDIOCODEC_MP3, SOF_IPC_STREAM_CAPTURE);
#endif
#if defined(CONFIG_FFMPEG_ENC_AAC)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants