Repository navigation
Conversation
…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
left a comment
There was a problem hiding this comment.
we need to be more verbose with small inline comments for each large code block
| 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; | ||
| } |
There was a problem hiding this comment.
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:
cd->ogg->activestaysfalse, causingffmpeg_dec_ogg_extract_packet()to return-EAGAINon every decode call and feed rawOggSpage headers intoff_opus_parser.- 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:
| 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; | |
| } |
There was a problem hiding this comment.
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.
| 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; | ||
| } |
There was a problem hiding this comment.
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.)
| 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; | |
| } |
There was a problem hiding this comment.
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.
| 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; |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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.
| #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 |
There was a problem hiding this comment.
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:
| #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 |
There was a problem hiding this comment.
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.
| 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; |
There was a problem hiding this comment.
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:
pkt_len == 0(partial Ogg page/packet split across a 4 KiBin_bufread).- The very first Vorbis audio packet (
avcodec_receive_frame()returnsAVERROR(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 ||.
There was a problem hiding this comment.
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.
| 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; |
There was a problem hiding this comment.
The Ogg continued_packet flag (ogg->hdr[5] & 0x01) is ignored here:
- If
!(ogg->hdr[5] & 0x01)whileogg->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 resettingogg->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.
There was a problem hiding this comment.
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.
| 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; | ||
| } |
There was a problem hiding this comment.
- Please add a brief comment explaining the magic values
2,6,10(SNDRV_PCM_FORMAT_S16_LE/S24_LE/S32_LEpassed bycrecord -F), matchingffmpeg-enc-flac.c:35-39. - Note that
SND_AUDIOSTREAMFORMAT_RAWis accepted here and the PR description says this supports "raw and Ogg-encapsulated Opus", butffmpeg_enc_opus_encode_frame()always wraps output in Ogg pages. Either gate Ogg framing onformator clarify the PR description to matchKconfig.
There was a problem hiding this comment.
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.
| if (enc_ch < 1 || enc_ch > 2) { | ||
| comp_err(dev, "unsupported Opus channel count %u", enc_ch); | ||
| return -ENOTSUP; | ||
| } |
There was a problem hiding this comment.
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:
| 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; | |
| } |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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):
| opus->avctx->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL; | |
| opus->avctx->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL; | |
| opus->avctx->thread_count = 1; |
There was a problem hiding this comment.
Fixed in 9b1edd2: added opus->avctx->thread_count = 1; before avcodec_open2() in ffmpeg-enc-opus.c.
| 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); | ||
| } |
There was a problem hiding this comment.
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):
| 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); | |
| } |
There was a problem hiding this comment.
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>
|
Addressed all review comments in commit
|
There was a problem hiding this comment.
🟡 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
Capture teardown drops delayed Opus packets and omits the EOS page · New Zero-length continuation laces can drop the final packet at EOF · New Final-page granule metadata is lost, causing excess decoded audio · New Short raw Opus packets are blocked by the four-byte probe · New Short raw Vorbis packets with extradata are blocked by the probe · New
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
| 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]; |
| 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; | ||
| } |
| 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; | ||
| } |

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:ffmpeg_dec-ogg.c,ffmpeg_dec-ogg.h):ffmpeg_dec_opus,ffmpeg_dec_vorbis, andffmpeg_enc_opus.extract_packet,reset, andfreehooks tostruct ffmpeg_dec_codec_opsso non-Ogg standalone decoder LLEXTs do not reference Ogg symbols.ffmpeg_dec-opus.c):OggSstreams, parsesOpusHead/OpusTagsheaders, stagesOpusHeadas extradata, and extracts Ogg Opus packets viaffmpeg_dec_ogg_extract_packet(), while leaving raw/unframed Opus on the normal parser path.CONFIG_FFMPEG_DEC_VORBIS,ffmpeg_dec_vorbis, UUID1208cd52-7a82-4d71-b903-7346fbf64156):USE_FASTMATHLLEXT withMAX_EXTRADATA 16384(configurableFFMPEG_MOD_MAX_EXTRADATA) to hold Vorbis ID, canonical comment, and codebook setup headers, plus built-in heap bump (768 KB).CONFIG_FFMPEG_ENC_OPUS,ffmpeg_enc_opus, UUID857d55e7-8b6b-4e3d-a6fb-91b78c928607):USE_FASTMATHLLEXT emitting Ogg-encapsulated Opus frames (OpusHead+OpusTags+ Ogg audio pages, matchingKconfig) withsupports_mono_downmix = trueandlibavcodec/opus/enc_psy.cmono/stereo fix._ffmpeg_add_decoder/_ffmpeg_add_encodermacrosrc_fileslist expansion inllext/CMakeLists.txtso multi-source LLEXTs link all per-codec sources cleanly.vorbisdecode andopusencode UUIDs and target definitions incompr-ffmpeg.conf,compr-capture-ffmpeg.conf, andtplg-targets.cmake(with 640 KiB compress heaps for Opus decode and Opus encode).Commits
61797a2f0ffmpeg_dec: add standalone Vorbis decoder and Opus encoder LLEXTs + Ogg Opus demux9b1edd220ffmpeg_dec: address PR 11561 review comments and size Opus topology heapsTesting
intel_adsp/ace30/ptl,CONFIG_COMP_FFMPEG_DEC=m,-DEXTRA_CFLAGS='-Werror',CONFIG_COMP_BT_CODEC=nlocally due to absentliblc3): passed; verified.sof_codec_infoand zero unresolvedffmpeg_dec_ogg/avcodec/av_symbols inffmpeg_dec_opus.llext(696K),ffmpeg_dec_vorbis.llext(602K), andffmpeg_enc_opus.llext(592K), plusffmpeg_dec_aac.llextstill uses 4096-byte max extradata.CONFIG_COMP_FFMPEG_DEC=m,CONFIG_COMP_FFMPEG_DEC_STUB=y,-DEXTRA_CFLAGS='-Werror'): passed (ffmpeg_dec.llext26K).CONFIG_COMP_FFMPEG_DEC=y,-DEXTRA_CFLAGS='-Werror'): passed.sof-hda-generic-ffmpeg-opus-compr.tplg,sof-hda-generic-ffmpeg-vorbis-compr.tplg, andsof-hda-generic-ffmpeg-opus-enc-compr.tplg(using hostlibasoundwith24000rate support).zephyr/scripts/checkpatch.pl --no-tree --max-line-length=100(0 errors, 0 warnings, 1845 lines checked) andscripts/check_kernel_includes.py --sources --strict(0 violations).intel_adsp/ace30/ptl):sof-hda-generic-ffmpeg-vorbis-compr.tplg): back-to-back playback with pause/resume (48 kHzstereo followed by44.1 kHzmono), verifying header extradata re-staging and clean compress EOS drain.sof-hda-generic-ffmpeg-opus-compr.tplg): back-to-back playback (48 kHzstereo followed by48 kHzmono), verifyingOpusHead/OpusTagsparsing and clean compress EOS drain.sof-hda-generic-ffmpeg-opus-enc-compr.tplg): captured both48 kHzstereo and48 kHzmono-downmix S16_LE streams, and validated captured.opuscontainers on host via GStreameroggdemux ! opusdecand Python Ogg CRC-32 (0CRC errors, validOpusHead/OpusTags, monotonic granule positions).