Skip to content

audio: ffmpeg_dec: add standalone Vorbis decoder and Opus encoder LLEXTs + Ogg Opus demux - #11561

Draft
lgirdwood wants to merge 2 commits into
thesofproject:mainfrom
lgirdwood:topic/ffmpeg-tier2
Draft

lgirdwood wants to merge 2 commits into
thesofproject:mainfrom
lgirdwood:topic/ffmpeg-tier2

Conversation

@lgirdwood

@lgirdwood lgirdwood commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

Summary

Adds standalone per-codec LLEXT support for Vorbis decoding and Opus encoding, along with shared incremental Ogg page/packet framing and Ogg Opus demuxing in ffmpeg_dec:

  • Shared Ogg Framing (ffmpeg_dec-ogg.c, ffmpeg_dec-ogg.h):
    • Incremental Ogg page/packet demuxer and Ogg page muxer linked only into ffmpeg_dec_opus, ffmpeg_dec_vorbis, and ffmpeg_enc_opus.
    • Added optional extract_packet, reset, and free hooks to struct ffmpeg_dec_codec_ops so non-Ogg standalone decoder LLEXTs do not reference Ogg symbols.
  • Ogg Opus Demux (ffmpeg_dec-opus.c):
    • Detects OggS streams, parses OpusHead/OpusTags headers, stages OpusHead as extradata, and extracts Ogg Opus packets via ffmpeg_dec_ogg_extract_packet(), while leaving raw/unframed Opus on the normal parser path.
  • Vorbis Decoder (CONFIG_FFMPEG_DEC_VORBIS, ffmpeg_dec_vorbis, UUID 1208cd52-7a82-4d71-b903-7346fbf64156):
    • Standalone USE_FASTMATH LLEXT with MAX_EXTRADATA 16384 (configurable FFMPEG_MOD_MAX_EXTRADATA) to hold Vorbis ID, canonical comment, and codebook setup headers, plus built-in heap bump (768 KB).
  • Opus Encoder (CONFIG_FFMPEG_ENC_OPUS, ffmpeg_enc_opus, UUID 857d55e7-8b6b-4e3d-a6fb-91b78c928607):
    • Standalone USE_FASTMATH LLEXT emitting Ogg-encapsulated Opus frames (OpusHead + OpusTags + Ogg audio pages, matching Kconfig) with supports_mono_downmix = true and libavcodec/opus/enc_psy.c mono/stereo fix.
  • CMake & Topology2:
    • Fixed _ffmpeg_add_decoder / _ffmpeg_add_encoder macro src_files list expansion in llext/CMakeLists.txt so multi-source LLEXTs link all per-codec sources cleanly.
    • Added vorbis decode and opus encode UUIDs and target definitions in compr-ffmpeg.conf, compr-capture-ffmpeg.conf, and tplg-targets.cmake (with 640 KiB compress heaps for Opus decode and Opus encode).

Commits

  • 61797a2f0 ffmpeg_dec: add standalone Vorbis decoder and Opus encoder LLEXTs + Ogg Opus demux
  • 9b1edd220 ffmpeg_dec: address PR 11561 review comments and size Opus topology heaps

Testing

  • PTL standalone LLEXT build (intel_adsp/ace30/ptl, CONFIG_COMP_FFMPEG_DEC=m, -DEXTRA_CFLAGS='-Werror', CONFIG_COMP_BT_CODEC=n locally due to absent liblc3): passed; verified .sof_codec_info and zero unresolved ffmpeg_dec_ogg/avcodec/av_ symbols in ffmpeg_dec_opus.llext (696K), ffmpeg_dec_vorbis.llext (602K), and ffmpeg_enc_opus.llext (592K), plus ffmpeg_dec_aac.llext still uses 4096-byte max extradata.
  • PTL stub LLEXT build (CONFIG_COMP_FFMPEG_DEC=m, CONFIG_COMP_FFMPEG_DEC_STUB=y, -DEXTRA_CFLAGS='-Werror'): passed (ffmpeg_dec.llext 26K).
  • PTL built-in build (CONFIG_COMP_FFMPEG_DEC=y, -DEXTRA_CFLAGS='-Werror'): passed.
  • Topology2 build: Built sof-hda-generic-ffmpeg-opus-compr.tplg, sof-hda-generic-ffmpeg-vorbis-compr.tplg, and sof-hda-generic-ffmpeg-opus-enc-compr.tplg (using host libasound with 24000 rate support).
  • Style & includes: zephyr/scripts/checkpatch.pl --no-tree --max-line-length=100 (0 errors, 0 warnings, 1845 lines checked) and scripts/check_kernel_includes.py --sources --strict (0 violations).
  • PTL hardware verification (Damsel Fly / intel_adsp/ace30/ptl):
    • Ogg Vorbis decode (sof-hda-generic-ffmpeg-vorbis-compr.tplg): back-to-back playback with pause/resume (48 kHz stereo followed by 44.1 kHz mono), verifying header extradata re-staging and clean compress EOS drain.
    • Ogg Opus decode (sof-hda-generic-ffmpeg-opus-compr.tplg): back-to-back playback (48 kHz stereo followed by 48 kHz mono), verifying OpusHead/OpusTags parsing and clean compress EOS drain.
    • Ogg Opus encode capture (sof-hda-generic-ffmpeg-opus-enc-compr.tplg): captured both 48 kHz stereo and 48 kHz mono-downmix S16_LE streams, and validated captured .opus containers on host via GStreamer oggdemux ! opusdec and Python Ogg CRC-32 (0 CRC errors, valid OpusHead/OpusTags, monotonic granule positions).

…gg Opus demux

Rebase Vorbis decode, Opus encode, and shared Ogg framing onto main's
per-codec standalone LLEXT architecture:

- Add shared incremental Ogg page/packet demuxer and Ogg page muxer
  (ffmpeg_dec-ogg.c/.h) linked only into the Opus and Vorbis decoder
  and Opus encoder LLEXTs via per-codec hooks (extract_packet/reset/free).
- Add Ogg Opus stream detection (OpusHead/OpusTags header stripping and
  extradata staging) to ffmpeg_dec-opus.c while keeping raw Opus working.
- Port Vorbis decoder (ffmpeg_dec_vorbis, UUID 1208cd52-7a82-4d71-b903-7346fbf64156)
  to standalone LLEXT with FFMPEG_MOD_MAX_EXTRADATA=16384 for Vorbis setup headers.
- Port Opus encoder (ffmpeg_enc_opus, UUID 857d55e7-8b6b-4e3d-a6fb-91b78c928607)
  to standalone LLEXT (CELT/PVQ + Ogg encapsulation) with FFmpeg enc_psy mono/stereo fix.
- Update Kconfig, built-in CMakeLists, LLEXT macro list handling, and
  topology2 compr configs/targets for Vorbis decode and Opus encode.

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.

we need to be more verbose with small inline comments for each large code block

Comment thread src/audio/ffmpeg_dec/ffmpeg_dec-opus.c Outdated
Comment on lines +34 to +57
if (!cd->ogg) {
avail = source_get_data_available(src);
if (avail < sizeof(magic)) {
return -ENODATA;
}

ret = source_get_data(src, sizeof(magic), &sp, &sstart, &sbytes);
if (ret < 0) {
return ret;
}
ffmpeg_dec_copy_from_circular(magic, sp, sstart, sbytes, sizeof(magic));
source_release_data(src, 0);

/* Non-Ogg stream (raw or MPEG-TS Opus): hand straight to parser. */
if (memcmp(magic, "OggS", 4) != 0) {
return 0;
}

cd->ogg = ffmpeg_dec_ogg_create(mod);
if (!cd->ogg) {
return -ENOMEM;
}
cd->ogg->active = true;
}

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.

Blocking bug on stream restart (reset() / prepare()): ffmpeg_dec_ogg_reset() sets ogg->active = false without freeing cd->ogg. On a second playback after reset(), if (!cd->ogg) is skipped, so:

  1. cd->ogg->active stays false, causing ffmpeg_dec_ogg_extract_packet() to return -EAGAIN on every decode call and feed raw OggS page headers into ff_opus_parser.
  2. If the next stream after reset is raw (non-Ogg) Opus, the "OggS" magic check is bypassed and raw Opus bytes are fed into the Ogg header parser.

Check !cd->ogg || !cd->ogg->active and reuse the existing allocation:

Suggested change
if (!cd->ogg) {
avail = source_get_data_available(src);
if (avail < sizeof(magic)) {
return -ENODATA;
}
ret = source_get_data(src, sizeof(magic), &sp, &sstart, &sbytes);
if (ret < 0) {
return ret;
}
ffmpeg_dec_copy_from_circular(magic, sp, sstart, sbytes, sizeof(magic));
source_release_data(src, 0);
/* Non-Ogg stream (raw or MPEG-TS Opus): hand straight to parser. */
if (memcmp(magic, "OggS", 4) != 0) {
return 0;
}
cd->ogg = ffmpeg_dec_ogg_create(mod);
if (!cd->ogg) {
return -ENOMEM;
}
cd->ogg->active = true;
}
if (!cd->ogg || !cd->ogg->active) {
avail = source_get_data_available(src);
if (avail < sizeof(magic)) {
return -ENODATA;
}
ret = source_get_data(src, sizeof(magic), &sp, &sstart, &sbytes);
if (ret < 0) {
return ret;
}
ffmpeg_dec_copy_from_circular(magic, sp, sstart, sbytes, sizeof(magic));
source_release_data(src, 0);
/* Non-Ogg stream (raw or MPEG-TS Opus): hand straight to parser. */
if (memcmp(magic, "OggS", 4) != 0) {
return 0;
}
if (!cd->ogg) {
cd->ogg = ffmpeg_dec_ogg_create(mod);
if (!cd->ogg) {
return -ENOMEM;
}
}
cd->ogg->active = true;
}

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.

Fixed in 9b1edd2: ffmpeg_dec-opus.c now checks if (!cd->ogg || !cd->ogg->active), reuses an existing cd->ogg across stream restarts instead of overwriting it, and clears cd->configured = false once OpusHead + OpusTags complete (with ffmpeg_dec_ff_configure() safely re-creating an already-open AVCodecContext). Verified back-to-back Ogg Opus stereo + mono playback on Damsel Fly.

Comment on lines +82 to +108
if (!cd->ogg) {
avail = source_get_data_available(src);
if (avail < sizeof(magic)) {
return -ENODATA;
}

ret = source_get_data(src, sizeof(magic), &sp, &sstart, &sbytes);
if (ret < 0) {
return ret;
}
ffmpeg_dec_copy_from_circular(magic, sp, sstart, sbytes, sizeof(magic));
source_release_data(src, 0);

if (memcmp(magic, "OggS", 4) != 0) {
if (cd->extradata_size > 0) {
return 0; /* raw Vorbis packets with preloaded extradata */
}
comp_err(dev, "expected OggS header for Vorbis stream");
return -EINVAL;
}

cd->ogg = ffmpeg_dec_ogg_create(mod);
if (!cd->ogg) {
return -ENOMEM;
}
cd->ogg->active = true;
}

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.

Blocking bug on stream restart (reset() / prepare()): Same issue as ffmpeg_dec-opus.c—after ffmpeg_dec_ogg_reset(), cd->ogg is still non-NULL with cd->ogg->active == false. Skipping this block leaves cd->ogg->active == false, so ffmpeg_dec_ogg_extract_packet() returns -EAGAIN and raw Ogg pages fall through to the unparsed decode path.

(Also note: because ffmpeg_dec_reset() leaves cd->configured == true, restarting with a different Vorbis file will parse the new setup header into cd->extradata without re-opening ff->avctx—so ff_vorbis_decoder will still hold the previous file's codebooks unless Vorbis resets/re-opens avctx on reset.)

Suggested change
if (!cd->ogg) {
avail = source_get_data_available(src);
if (avail < sizeof(magic)) {
return -ENODATA;
}
ret = source_get_data(src, sizeof(magic), &sp, &sstart, &sbytes);
if (ret < 0) {
return ret;
}
ffmpeg_dec_copy_from_circular(magic, sp, sstart, sbytes, sizeof(magic));
source_release_data(src, 0);
if (memcmp(magic, "OggS", 4) != 0) {
if (cd->extradata_size > 0) {
return 0; /* raw Vorbis packets with preloaded extradata */
}
comp_err(dev, "expected OggS header for Vorbis stream");
return -EINVAL;
}
cd->ogg = ffmpeg_dec_ogg_create(mod);
if (!cd->ogg) {
return -ENOMEM;
}
cd->ogg->active = true;
}
if (!cd->ogg || !cd->ogg->active) {
avail = source_get_data_available(src);
if (avail < sizeof(magic)) {
return -ENODATA;
}
ret = source_get_data(src, sizeof(magic), &sp, &sstart, &sbytes);
if (ret < 0) {
return ret;
}
ffmpeg_dec_copy_from_circular(magic, sp, sstart, sbytes, sizeof(magic));
source_release_data(src, 0);
if (memcmp(magic, "OggS", 4) != 0) {
if (cd->extradata_size > 0) {
return 0; /* raw Vorbis packets with preloaded extradata */
}
comp_err(dev, "expected OggS header for Vorbis stream");
return -EINVAL;
}
if (!cd->ogg) {
cd->ogg = ffmpeg_dec_ogg_create(mod);
if (!cd->ogg) {
return -ENOMEM;
}
}
cd->ogg->active = true;
}

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.

Fixed in 9b1edd2: ffmpeg_dec-vorbis.c now checks if (!cd->ogg || !cd->ogg->active) to reuse and re-arm an existing cd->ogg, sets cd->configured = false when packet 2 finishes, and ffmpeg_dec_ff_configure() in ffmpeg_dec-ffmpeg.c closes/frees any previously open ff->avctx and ff->pktbuf before re-opening with the new headers. Verified back-to-back 48 kHz stereo + 44.1 kHz mono Vorbis playback on Damsel Fly.

Comment on lines +136 to +165
cd->extradata_size = 3 + VORBIS_ID_HEADER_SIZE + VORBIS_CANON_COMMENT_SIZE;
cd->ogg->hdr_pkts_seen = 1;
} else if (cd->ogg->hdr_pkts_seen == 1) {
/*
* ff_vorbis_decoder ignores all comment fields; substituting the
* canonical 16-byte comment header protects extradata against
* oversized embedded album artwork in METADATA_BLOCK_PICTURE.
*/
if (pkt_len < 7 || memcmp(pkt, "\x03vorbis", 7) != 0) {
comp_err(dev, "invalid Vorbis comment header");
return -EINVAL;
}
cd->ogg->hdr_pkts_seen = 2;
} else {
size_t off = cd->extradata_size;

if (pkt_len < 7 || memcmp(pkt, "\x05vorbis", 7) != 0) {
comp_err(dev, "invalid Vorbis setup header");
return -EINVAL;
}
if (total_len != pkt_len || off + total_len > sizeof(cd->extradata)) {
comp_err(dev, "Vorbis setup header too large (%zu bytes, cap %zu)",
total_len, sizeof(cd->extradata) - off);
return -ENOMEM;
}
memcpy_s(cd->extradata + off, sizeof(cd->extradata) - off,
pkt, pkt_len);
cd->extradata_size = off + pkt_len;
cd->ogg->hdr_pkts_seen = 3;
cd->ogg->headers_done = true;

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.

If packet 0 (\x01vorbis) succeeds here (cd->extradata_size = 49) and packet 1 (\x03vorbis) or packet 2 (\x05vorbis) subsequently fails with -EINVAL/-ENOMEM, cd->extradata_size is left holding a truncated 49-byte header without codebooks. On a later retry with a non-Ogg stream, line 96 (if (cd->extradata_size > 0) return 0;) would treat that partial buffer as valid preloaded extradata.

Consider keeping off = 3 + VORBIS_ID_HEADER_SIZE + VORBIS_CANON_COMMENT_SIZE constant and only committing cd->extradata_size = off + pkt_len once packet 2 succeeds (or clearing cd->extradata_size = 0 on error).

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.

Fixed in 9b1edd2: ffmpeg_dec-vorbis.c now resets cd->extradata_size = 0 when Vorbis identification packet 0 arrives and commits cd->extradata_size = VORBIS_SETUP_OFFSET + pkt_len only after packet 2 succeeds cleanly.

Comment on lines +25 to +31
#if defined(FFMPEG_MOD_MAX_EXTRADATA)
#define FFMPEG_DEC_MAX_EXTRADATA FFMPEG_MOD_MAX_EXTRADATA
#elif defined(CONFIG_FFMPEG_DEC_VORBIS)
#define FFMPEG_DEC_MAX_EXTRADATA 16384
#else
#define FFMPEG_DEC_MAX_EXTRADATA 4096
#endif

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.

Footprint bug in multi-module LLEXT builds: When CONFIG_COMP_FFMPEG_DEC=m and CONFIG_FFMPEG_DEC_VORBIS=y, CONFIG_FFMPEG_DEC_VORBIS is defined in Zephyr's autoconf.h for every standalone LLEXT module. Since only ffmpeg_dec_vorbis passes MAX_EXTRADATA 16384, all other standalone decoders, encoders, and filters fall into #elif defined(CONFIG_FFMPEG_DEC_VORBIS) and bloat struct ffmpeg_dec_comp_data by +12 KiB—defeating FFMPEG_MOD_MAX_EXTRADATA.

Guard the Kconfig fallback so standalone role LLEXTs default to 4096 unless they explicitly pass FFMPEG_MOD_MAX_EXTRADATA:

Suggested change
#if defined(FFMPEG_MOD_MAX_EXTRADATA)
#define FFMPEG_DEC_MAX_EXTRADATA FFMPEG_MOD_MAX_EXTRADATA
#elif defined(CONFIG_FFMPEG_DEC_VORBIS)
#define FFMPEG_DEC_MAX_EXTRADATA 16384
#else
#define FFMPEG_DEC_MAX_EXTRADATA 4096
#endif
#if defined(FFMPEG_MOD_MAX_EXTRADATA)
#define FFMPEG_DEC_MAX_EXTRADATA FFMPEG_MOD_MAX_EXTRADATA
#elif defined(CONFIG_FFMPEG_DEC_VORBIS) && !defined(FFMPEG_MOD_ROLE_DEC) && \
!defined(FFMPEG_MOD_ROLE_ENC) && !defined(FFMPEG_MOD_ROLE_AF)
#define FFMPEG_DEC_MAX_EXTRADATA 16384
#else
#define FFMPEG_DEC_MAX_EXTRADATA 4096
#endif

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.

Fixed in 9b1edd2: guarded the 16384-byte Vorbis FFMPEG_DEC_MAX_EXTRADATA bump in ffmpeg_dec.h with !defined(FFMPEG_MOD_ROLE_DEC) && !defined(FFMPEG_MOD_ROLE_ENC) && !defined(FFMPEG_MOD_ROLE_AF) so non-Vorbis standalone LLEXTs keep the 4096-byte limit. Verified in the disassembled ELF that ffmpeg_dec_aac.llext checks 0x1000 while ffmpeg_dec_vorbis.llext checks 0x4000.

Comment on lines +342 to +349
if (pkt_len == 0) {
return 0;
}
memset(ff->pktbuf + pkt_len, 0, AV_INPUT_BUFFER_PADDING_SIZE);
ff->pkt->data = ff->pktbuf;
ff->pkt->size = (int)pkt_len;
ret = avcodec_send_packet(ff->avctx, ff->pkt);
goto drain_frames;

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.

Blocking interaction with ffmpeg_dec.c:734-735 during compress drain: Two normal cases return 0 with *produced == 0 here while input data still remains in src:

  1. pkt_len == 0 (partial Ogg page/packet split across a 4 KiB in_buf read).
  2. The very first Vorbis audio packet (avcodec_receive_frame() returns AVERROR(EAGAIN) because Vorbis MDCT overlap-add needs two packets before emitting PCM).

In ffmpeg_dec.c:734-735, if (host_eos && !cd->pcm_avail && (!produced || source_get_data_available(src) == 0)) will immediately flush the decoder and signal EOS as soon as !produced is true—even when source_get_data_available(src) > 0 (which happens on every buffer-resident file where the host issues DRAIN up front). That contradicts the comment on ffmpeg_dec.c:717-726 and will truncate Vorbis streams to 0 samples. Please fix line 735 in ffmpeg_dec.c to drop !produced ||.

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.

Fixed in 9b1edd2: dropped the !produced || condition from the EOS drain trigger in src/audio/ffmpeg_dec/ffmpeg_dec.c and updated the inline comment. Verified compress EOS drain on Damsel Fly for both Vorbis and Opus playback.

Comment on lines +174 to +184
ogg->nsegs = ogg->hdr[26];
ogg->lacing_pos = 0;
ogg->seg_idx = 0;
ogg->seg_rem = 0;
ogg->seg_active = false;
if (ogg->nsegs == 0) {
ogg->hdr_pos = 0;
break;
}
ogg->state = OGG_STATE_SEG_TABLE;
break;

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 Ogg continued_packet flag (ogg->hdr[5] & 0x01) is ignored here:

  • If !(ogg->hdr[5] & 0x01) while ogg->pkt_len > 0 (e.g. a previous page was truncated mid-packet), the new fresh packet will be concatenated onto the stale partial bytes instead of resetting ogg->pkt_len = 0; ogg->pkt_total_len = 0;.
  • Conversely, if (ogg->hdr[5] & 0x01) right after a sync-loss resync (ogg->pkt_len == 0), a broken continuation tail will be returned as a complete packet to the decoder.

Checking ogg->hdr[5] & 0x01 here would make the resync path safe against corrupting the first recovered packet.

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.

Fixed in 9b1edd2: added cont_drop to struct ffmpeg_dec_ogg_demux (cleared on reset/resync) and now inspect hdr[5] & 0x01 (continued_packet) in ffmpeg_dec_ogg_demux_packet(): continuation flags at resync drop the orphaned tail segment up to the first completing lacing byte < 255, and fresh pages (!(hdr[5] & 0x01)) reset any stale partial packet buffer.

Comment on lines +133 to +141
if (codec_params->format != SND_AUDIOSTREAMFORMAT_UNDEFINED &&
codec_params->format != SND_AUDIOSTREAMFORMAT_RAW &&
codec_params->format != 2 &&
codec_params->format != 6 &&
codec_params->format != 10) {
comp_err(dev, "unsupported Opus stream format 0x%x",
codec_params->format);
return -ENOTSUP;
}

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.

  1. Please add a brief comment explaining the magic values 2, 6, 10 (SNDRV_PCM_FORMAT_S16_LE / S24_LE / S32_LE passed by crecord -F), matching ffmpeg-enc-flac.c:35-39.
  2. Note that SND_AUDIOSTREAMFORMAT_RAW is accepted here and the PR description says this supports "raw and Ogg-encapsulated Opus", but ffmpeg_enc_opus_encode_frame() always wraps output in Ogg pages. Either gate Ogg framing on format or clarify the PR description to match Kconfig.

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.

Fixed in 9b1edd2: clarified in ffmpeg-enc-opus.c (and updated the PR description to match Kconfig) that ffmpeg_enc_opus emits Ogg-encapsulated Opus streams, and added a concise comment matching ffmpeg-enc-flac.c explaining why ffmpeg_enc_init() outputs 2-byte/6-byte/10-byte Ogg framing bytes when ready to flush OpusHead + OpusTags.

Comment thread src/audio/ffmpeg_dec/ffmpeg-enc-opus.c Outdated
Comment on lines +205 to +208
if (enc_ch < 1 || enc_ch > 2) {
comp_err(dev, "unsupported Opus channel count %u", enc_ch);
return -ENOTSUP;
}

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.

Bug with >2ch sources: Because ffmpeg_enc_opus_ops.supports_mono_downmix = true, ffmpeg-encode.c:390 accepts e->req_channels == 1 for any cd->out_channels (e.g. 4ch). Line 300 then only checks cd->out_channels == 2 && opus->enc_channels == 1, so a >2ch source falls into the else branch and passes opus->enc_channels (1) to ffmpeg_dec_pcm_to_frame(), misinterpreting multi-channel interleaved PCM as mono.

Validate cd->out_channels here as well:

Suggested change
if (enc_ch < 1 || enc_ch > 2) {
comp_err(dev, "unsupported Opus channel count %u", enc_ch);
return -ENOTSUP;
}
if (enc_ch < 1 || enc_ch > 2 || cd->out_channels < 1 || cd->out_channels > 2) {
comp_err(dev, "unsupported Opus channel count src %u enc %u",
cd->out_channels, enc_ch);
return -ENOTSUP;
}

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.

Fixed in 9b1edd2: ffmpeg-enc-opus.c now rejects cd->out_channels < 1 || cd->out_channels > 2 before allocating the Opus encoder or setting channel-count fields.

opus->avctx->bit_rate = enc_ch == 1 ?
OPUS_ENC_DEFAULT_MONO_BITRATE : OPUS_ENC_DEFAULT_STEREO_BITRATE;
}
opus->avctx->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL;

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.

Explicitly set thread_count = 1 for consistency with all other libavcodec codecs (ffmpeg_dec-ffmpeg.c:250, ffmpeg-enc-flac.c:116, ffmpeg-enc-sbc.c:137):

Suggested change
opus->avctx->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL;
opus->avctx->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL;
opus->avctx->thread_count = 1;

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.

Fixed in 9b1edd2: added opus->avctx->thread_count = 1; before avcodec_open2() in ffmpeg-enc-opus.c.

Comment on lines +265 to +271
page_bytes = ffmpeg_dec_ogg_write_page(opus->page_buf, opus->page_cap,
OPUS_ENC_OGG_SERIAL,
opus->page_seq++, flags, granule,
pkt, pkt_len);
if (page_bytes > 0) {
ffmpeg_enc_stage_packet(mod, e, opus->page_buf, (size_t)page_bytes);
}

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.

Only advance opus->page_seq when ffmpeg_dec_ogg_write_page() succeeds, and log a warning if a page write fails instead of silently dropping it (also check opus->avctx->extradata != NULL alongside extradata_size >= 19 at line 281):

Suggested change
page_bytes = ffmpeg_dec_ogg_write_page(opus->page_buf, opus->page_cap,
OPUS_ENC_OGG_SERIAL,
opus->page_seq++, flags, granule,
pkt, pkt_len);
if (page_bytes > 0) {
ffmpeg_enc_stage_packet(mod, e, opus->page_buf, (size_t)page_bytes);
}
page_bytes = ffmpeg_dec_ogg_write_page(opus->page_buf, opus->page_cap,
OPUS_ENC_OGG_SERIAL,
opus->page_seq, flags, granule,
pkt, pkt_len);
if (page_bytes > 0) {
opus->page_seq++;
ffmpeg_enc_stage_packet(mod, e, opus->page_buf, (size_t)page_bytes);
} else {
comp_warn(mod->dev, "Ogg page write failed (%d, pkt_len=%zu)",
page_bytes, pkt_len);
}

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.

Fixed in 9b1edd2: gated opus->page_seq++ on page_bytes > 0 with comp_warn() on flush/mux failure, and added a guard checking opus->avctx->extradata before avcodec_open2() returns success.

…eaps

Address review feedback on Ogg framing, stream restart state, build
isolation, and inline documentation, and size PTL compress topology heaps
verified on Damsel Fly:

- Guard the Vorbis 16 KiB FFMPEG_DEC_MAX_EXTRADATA override so non-Vorbis
  standalone LLEXTs remain at 4096 bytes.
- Re-create open AVCodecContext instances when cd->configured is cleared
  during mid-stream/back-to-back Ogg header restarts in
  ffmpeg_dec_ff_configure(), and allow Opus/Vorbis decoders to reuse and
  re-arm an inactive Ogg demuxer without leaking cd->ogg.
- Clear cd->extradata_size at Vorbis packet 0 and only commit the final
  extradata_size once Vorbis setup packet 2 is assembled.
- Handle Ogg header flag 0x01 (continued_packet) in the incremental Ogg
  demuxer by dropping orphan continuation segments after page resync and
  resetting stale partial packets when bit 0 is clear.
- Drop the redundant !produced check in the ffmpeg_dec EOS drain trigger so
  drained packets are forwarded promptly on EOS.
- Validate out_channels (1-2), pin thread_count = 1, gate Ogg page_seq
  increment on non-error flush/mux, and guard extradata before building
  OpusHead in ffmpeg-enc-opus.c.
- Add concise inline block comments across the Ogg demuxer, Vorbis/Opus
  decoders, FFmpeg backend configure path, and Opus encoder, and clarify
  Ogg-encapsulated Opus output (matching Kconfig).
- Set COMPR_FFMPEG_DEC_HEAP / COMPR_FFMPEG_ENC_HEAP to 655360 (640 KiB)
  in tplg-targets.cmake for sof-hda-generic-ffmpeg-opus-compr and
  sof-hda-generic-ffmpeg-opus-enc-compr based on PTL Ogg Opus playback and
  encode hardware testing.

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

Copy link
Copy Markdown
Member Author

Addressed all review comments in commit 9b1edd220:

  • Added concise inline block comments across ffmpeg_dec-ogg.c, ffmpeg_dec-vorbis.c, ffmpeg_dec-opus.c, ffmpeg_dec-ffmpeg.c, ffmpeg-enc-opus.c, and ffmpeg_dec.c, and clarified Ogg-encapsulated Opus output in ffmpeg-enc-opus.c and the PR description.
  • Fixed Ogg demuxer continued_packet handling, stream-restart cd->ogg reuse and AVCodecContext re-creation, Vorbis header staging atomicity, standalone LLEXT FFMPEG_DEC_MAX_EXTRADATA scoping, EOS drain trigger, and Opus encoder validation/error guards.
  • Sized COMPR_FFMPEG_DEC_HEAP / COMPR_FFMPEG_ENC_HEAP to 655360 in tplg-targets.cmake for Opus decode/encode topologies and verified Ogg Vorbis decode, Ogg Opus decode, and Ogg Opus stereo + mono capture on Damsel Fly (intel_adsp/ace30/ptl).

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 audio boundary bugs can stall short Opus/Vorbis streams, drop final packets, produce excess PCM, and truncate Opus captures without proper encoder draining or Ogg EOS signaling.

5 open issues
What changed in this PR

Adds standalone Vorbis decoding and Ogg Opus encoding to SOF’s FFmpeg audio modules, with shared Ogg framing and topology support.

Changes:

  • Adds incremental Ogg demuxing/muxing and Ogg Opus header handling.
  • Integrates Vorbis decoding and Opus encoding into codec selection, builds, and lifecycle hooks.
  • Registers codec UUIDs and topology targets, with adjusted heap and extradata limits.
File Description
uuid-registry.txt Registers Vorbis decoder and Opus encoder UUIDs.
tools/​topology/​topology2/​platform/​intel/​compr-ffmpeg.conf Maps Vorbis playback to its module UUID.
tools/​topology/​topology2/​platform/​intel/​compr-capture-ffmpeg.conf Maps Opus capture to its module UUID.
tools/​topology/​topology2/​development/​tplg-targets.cmake Adds codec targets and adjusts Opus heaps.
src/​audio/​ffmpeg_dec/​llext/​CMakeLists.txt Adds standalone modules and multi-source support.
src/​audio/​ffmpeg_dec/​Kconfig Exposes codec options and math dependencies.
src/​audio/​ffmpeg_dec/​ffmpeg.cmake Enables codecs and adjusts Opus psychoacoustic processing.
src/​audio/​ffmpeg_dec/​ffmpeg-encode.c Registers the Opus encoder.
src/​audio/​ffmpeg_dec/​ffmpeg-enc-opus.c Implements Ogg Opus encoding and mono downmixing.
src/​audio/​ffmpeg_dec/​ffmpeg_dec.h Adds codec hooks, Ogg state, and extradata sizing.
src/​audio/​ffmpeg_dec/​ffmpeg_dec.c Integrates codec lifecycle hooks and updates EOS handling.
src/​audio/​ffmpeg_dec/​ffmpeg_dec-vorbis.c Parses Vorbis headers and configures packet extraction.
src/​audio/​ffmpeg_dec/​ffmpeg_dec-opus.c Detects Ogg streams and parses Opus headers.
src/​audio/​ffmpeg_dec/​ffmpeg_dec-ogg.h Defines shared Ogg interfaces and state.
src/​audio/​ffmpeg_dec/​ffmpeg_dec-ogg.c Implements incremental packet extraction and page writing.
src/​audio/​ffmpeg_dec/​ffmpeg_dec-ffmpeg.c Integrates framed decoding and context recreation.
src/​audio/​ffmpeg_dec/​ffmpeg_dec-builtin-alloc.c Increases the built-in heap for Vorbis.
src/​audio/​ffmpeg_dec/​CMakeLists.txt Includes new codec and Ogg sources in builds.

Review effort: Balanced

Comment on lines +355 to +358
opus->granule_pos += dur;
opus_stage_ogg_packet(mod, e, opus, 0x00, opus->granule_pos,
opus->pkt->data,
(size_t)opus->pkt->size);
return 0;
}

while (pos < in_size) {
ogg->cont_drop = true;
}

ogg->nsegs = ogg->hdr[26];
Comment on lines +40 to +55
avail = source_get_data_available(src);
if (avail < sizeof(magic)) {
return -ENODATA;
}

ret = source_get_data(src, sizeof(magic), &sp, &sstart, &sbytes);
if (ret < 0) {
return ret;
}
ffmpeg_dec_copy_from_circular(magic, sp, sstart, sbytes, sizeof(magic));
source_release_data(src, 0);

/* Non-Ogg stream (raw or MPEG-TS Opus): hand straight to parser. */
if (memcmp(magic, "OggS", 4) != 0) {
return 0;
}
Comment on lines +90 to +108
avail = source_get_data_available(src);
if (avail < sizeof(magic)) {
return -ENODATA;
}

ret = source_get_data(src, sizeof(magic), &sp, &sstart, &sbytes);
if (ret < 0) {
return ret;
}
ffmpeg_dec_copy_from_circular(magic, sp, sstart, sbytes, sizeof(magic));
source_release_data(src, 0);

if (memcmp(magic, "OggS", 4) != 0) {
if (cd->extradata_size > 0) {
return 0; /* raw Vorbis packets with preloaded extradata */
}
comp_err(dev, "expected OggS header for Vorbis stream");
return -EINVAL;
}
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