PR #25966 opened by Baptiste Coudurier (bcoudurier) URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25966 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25966.patch
Opus muxing in CAF was disabled in 3fc7e39eb8 because the content of the `kuki` chunk was unknown. This series figures it out from files written by `afconvert -f caff -d opus`, re-enables muxing, and adds demuxing of Apple's multichannel layout. - **cafenc: set the stream time base to the sample rate.** Packet durations went into the `pakt` chunk in the caller's time base. Remuxing MP2 from Matroska gave a file Core Audio reported as 0.012 s long instead of 1 s. - **cafenc: write a kuki chunk for Opus.** Apple's cookie is seven big-endian 32-bit fields: 2048 (`OPUS_APPLICATION_VOIP`), sample rate, frame size, -1000 (`OPUS_AUTO`), channels, 0, 0. The muxer now writes the same bytes. `afinfo`/`afconvert` read the output with the right priming, remainder and duration, and decode it in sync with the FFmpeg decoder. Adds `fate-caf-opus-remux`. - **cafdec: support multichannel Opus.** Apple's encoder accepts 1, 2 or 4 channels. With 4, each packet holds one mono stream per channel, each preceded by a 16-bit big-endian size. The demuxer exports mapping family 255 and repacks packets into RFC 7845 multistream framing. Output matches `afconvert`'s decode sample-aligned (78 dB SNR per channel at 48 kHz) and remuxes to Ogg bit-exactly. Multichannel muxing is not included: the muxer still rejects more than 2 channels. There is no FATE test for multichannel demuxing yet, since it needs a new sample in the suite. 🤖 Generated with [Claude Code](https://claude.com/claude-code) From ff2dea37e4587fef397b19576f5ce03a8637a18a Mon Sep 17 00:00:00 2001 From: Baptiste Coudurier <[email protected]> Date: Thu, 12 Nov 2026 00:58:47 -0800 Subject: [PATCH 1/3] avformat/cafenc: set the stream time base to the sample rate The muxer stores packet durations in the pakt chunk and uses them to compute the valid and remainder frame counts, all of which are in sample units, but never set the stream time base. Durations were thus written in whatever time base the caller used. Remuxing MP2 from Matroska (1/1000 time base) produced a file Core Audio reported as 0.012 seconds long instead of 1 second. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Baptiste Coudurier <[email protected]> --- libavformat/cafenc.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/libavformat/cafenc.c b/libavformat/cafenc.c index b7e7d1c56fd..f84765c83af 100644 --- a/libavformat/cafenc.c +++ b/libavformat/cafenc.c @@ -25,6 +25,7 @@ #include "caf.h" #include "isom.h" #include "avio_internal.h" +#include "internal.h" #include "mux.h" #include "libavutil/intfloat.h" #include "libavutil/dict.h" @@ -136,6 +137,9 @@ static int caf_write_init(struct AVFormatContext *s) return AVERROR_INVALIDDATA; } + avpriv_set_pts_info(s->streams[0], 64, 1, + par->codec_id == AV_CODEC_ID_OPUS ? 48000 : par->sample_rate); + // if either block_align or frame_size are 0, we need to check that the output // is seekable. Postpone reporting init as complete until caf_write_header() if (!par->block_align || !par->frame_size) -- 2.54.0 From c24f6e355a4c51029c7358f33de56f624e254d81 Mon Sep 17 00:00:00 2001 From: Baptiste Coudurier <[email protected]> Date: Thu, 12 Nov 2026 00:59:24 -0800 Subject: [PATCH 2/3] avformat/cafenc: write a kuki chunk for Opus and enable muxing it again Opus muxing was disabled in 3fc7e39eb8 because the content of the kuki chunk was unknown. Files written by afconvert carry a 28-byte cookie of seven big-endian 32-bit fields: 2048 (OPUS_APPLICATION_VOIP), the sample rate, the frame size, -1000 (OPUS_AUTO), the channel count, 0 and 0. Changing any field but the fourth and the last one makes Apple's decoder fail or change its output. Write the same cookie. afinfo and afconvert read the resulting files with the right priming, remainder and duration, and decode them in sync with the FFmpeg decoder. Signed-off-by: Baptiste Coudurier <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]> --- libavformat/cafdec.c | 7 +++++-- libavformat/cafenc.c | 12 +++++++++++- tests/fate/caf.mak | 3 +++ tests/ref/fate/caf-opus-remux | 24 ++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 tests/ref/fate/caf-opus-remux diff --git a/libavformat/cafdec.c b/libavformat/cafdec.c index fae4d7db5ec..38ea1e51cc9 100644 --- a/libavformat/cafdec.c +++ b/libavformat/cafdec.c @@ -229,8 +229,11 @@ static int read_kuki_chunk(AVFormatContext *s, int64_t size) if (!last) av_log(s, AV_LOG_WARNING, "non-STREAMINFO FLACMetadataBlock(s) ignored\n"); } else if (st->codecpar->codec_id == AV_CODEC_ID_OPUS) { - // The data layout for Opus is currently unknown, so we generate - // extradata using known sane values. Multichannel streams are not supported. + // The cookie written by Apple's encoder holds seven 32-bit fields + // (application, sample rate, frame size, bitrate, channels, 0, 0), none + // of which OpusHead needs, so generate extradata from the desc values. + // Multichannel streams are stored as one length-prefixed mono stream + // per channel and are not supported. if (st->codecpar->ch_layout.nb_channels > 2) { avpriv_request_sample(s, "multichannel Opus in CAF"); return AVERROR_PATCHWELCOME; diff --git a/libavformat/cafenc.c b/libavformat/cafenc.c index f84765c83af..34e25ad1847 100644 --- a/libavformat/cafenc.c +++ b/libavformat/cafenc.c @@ -122,7 +122,6 @@ static int caf_write_init(struct AVFormatContext *s) switch (par->codec_id) { case AV_CODEC_ID_AAC: - case AV_CODEC_ID_OPUS: av_log(s, AV_LOG_ERROR, "muxing codec currently unsupported\n"); return AVERROR_PATCHWELCOME; } @@ -215,6 +214,17 @@ static int caf_write_header(AVFormatContext *s) ffio_wfourcc(pb, "kuki"); avio_wb64(pb, par->extradata_size); avio_write(pb, par->extradata, par->extradata_size); + } else if (par->codec_id == AV_CODEC_ID_OPUS) { + /* Same layout and values as the cookie written by Apple's encoder. */ + ffio_wfourcc(pb, "kuki"); + avio_wb64(pb, 28); + avio_wb32(pb, 2048); /* OPUS_APPLICATION_VOIP */ + avio_wb32(pb, sample_rate); + avio_wb32(pb, caf->frame_size); + avio_wb32(pb, -1000); /* OPUS_AUTO */ + avio_wb32(pb, par->ch_layout.nb_channels); + avio_wb32(pb, 0); + avio_wb32(pb, 0); } ff_standardize_creation_time(s); diff --git a/tests/fate/caf.mak b/tests/fate/caf.mak index 4ee9c95a5c8..ff3ddc77dfb 100644 --- a/tests/fate/caf.mak +++ b/tests/fate/caf.mak @@ -18,6 +18,9 @@ FATE_CAF_FFMPEG-$(call REMUX, CAF, WAV_DEMUXER PCM_S24LE_DECODER \ += fate-caf-pcm_s24-remux fate-caf-pcm_s24-remux: CMD = transcode wav $(TARGET_SAMPLES)/audio-reference/divertimenti_2ch_96kHz_s24.wav caf "-c pcm_s24be" "-c copy -t 0.05" +FATE_CAF_FFMPEG_FFPROBE-$(call REMUX, CAF, MATROSKA_DEMUXER) += fate-caf-opus-remux +fate-caf-opus-remux: CMD = transcode matroska $(TARGET_SAMPLES)/mkv/codec_delay_opus.mkv caf "-c copy" "-c copy -t 0.2" "-show_entries stream=codec_name,channels,initial_padding" + FATE_CAF_FFMPEG-$(call REMUX, CAF, MOV_DEMUXER) += fate-caf-mace6-remux fate-caf-mace6-remux: CMD = transcode mov $(TARGET_SAMPLES)/qtrle/Animation-16Greys.mov caf "-map 0:a -c copy" "-c copy -t 0.003" diff --git a/tests/ref/fate/caf-opus-remux b/tests/ref/fate/caf-opus-remux new file mode 100644 index 00000000000..46f79dd806a --- /dev/null +++ b/tests/ref/fate/caf-opus-remux @@ -0,0 +1,24 @@ +6f7ca2944f2538ee9f7822aca1dbe3c3 *tests/data/fate/caf-opus-remux.caf +8867 tests/data/fate/caf-opus-remux.caf +#extradata 0: 19, 0x3a04048f +#tb 0: 1/48000 +#media_type 0: audio +#codec_id 0: opus +#sample_rate 0: 48000 +#channel_layout_name 0: mono +0, -312, -312, 960, 320, 0x58b9a88d, S=1, Skip Samples, 10, 0x02390039 +0, 648, 648, 960, 159, 0x6c9c4b4c +0, 1608, 1608, 960, 148, 0x0caf4b5d +0, 2568, 2568, 960, 139, 0xc5624226 +0, 3528, 3528, 960, 146, 0x633c4937 +0, 4488, 4488, 960, 153, 0x3d0b4f93 +0, 5448, 5448, 960, 158, 0xe5c55641 +0, 6408, 6408, 960, 156, 0xf2fd50ef +0, 7368, 7368, 960, 158, 0x93b15410 +0, 8328, 8328, 960, 157, 0xb6f74f5f +0, 9288, 9288, 960, 159, 0x9aff4957 +[STREAM] +codec_name=opus +channels=1 +initial_padding=312 +[/STREAM] -- 2.54.0 From cc6896ffb5ecf55c61c1661f31e27af440850984 Mon Sep 17 00:00:00 2001 From: Baptiste Coudurier <[email protected]> Date: Thu, 12 Nov 2026 01:03:21 -0800 Subject: [PATCH 3/3] avformat/cafdec: support multichannel Opus Apple's encoder accepts 1, 2 or 4 channels. With 4 channels, each packet holds one mono Opus stream per channel, each preceded by its size as a 16-bit big-endian value. Export channel mapping family 255 with one uncoupled stream per channel, and convert packets to the RFC 7845 multistream layout by dropping the size prefixes and giving all streams but the last one self-delimiting framing. Files written by afconvert now decode sample-aligned with afconvert's own output, with channels in order, and can be remuxed to Ogg. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Baptiste Coudurier <[email protected]> --- libavformat/cafdec.c | 183 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 175 insertions(+), 8 deletions(-) diff --git a/libavformat/cafdec.c b/libavformat/cafdec.c index 38ea1e51cc9..03816c216db 100644 --- a/libavformat/cafdec.c +++ b/libavformat/cafdec.c @@ -232,25 +232,34 @@ static int read_kuki_chunk(AVFormatContext *s, int64_t size) // The cookie written by Apple's encoder holds seven 32-bit fields // (application, sample rate, frame size, bitrate, channels, 0, 0), none // of which OpusHead needs, so generate extradata from the desc values. - // Multichannel streams are stored as one length-prefixed mono stream - // per channel and are not supported. - if (st->codecpar->ch_layout.nb_channels > 2) { - avpriv_request_sample(s, "multichannel Opus in CAF"); - return AVERROR_PATCHWELCOME; + // With more than two channels, each channel is coded as its own mono + // stream (see opus_repack_multistream()), which maps to channel + // mapping family 255 with one uncoupled stream per channel. + int channels = st->codecpar->ch_layout.nb_channels; + + if (channels < 1 || channels > 255) { + av_log(s, AV_LOG_ERROR, "invalid Opus channel count %d\n", channels); + return AVERROR_INVALIDDATA; } - ret = ff_alloc_extradata(st->codecpar, 19); + ret = ff_alloc_extradata(st->codecpar, 19 + (channels > 2) * (2 + channels)); if (ret < 0) return ret; AV_WB32A(st->codecpar->extradata, MKBETAG('O','p','u','s')); AV_WB32A(st->codecpar->extradata + 4, MKBETAG('H','e','a','d')); AV_WB8(st->codecpar->extradata + 8, 1); /* OpusHead version */ - AV_WB8(st->codecpar->extradata + 9, st->codecpar->ch_layout.nb_channels); + AV_WB8(st->codecpar->extradata + 9, channels); AV_WL16A(st->codecpar->extradata + 10, st->codecpar->initial_padding); AV_WL32A(st->codecpar->extradata + 12, st->codecpar->sample_rate); AV_WL16A(st->codecpar->extradata + 16, 0); - AV_WB8(st->codecpar->extradata + 18, 0); + AV_WB8(st->codecpar->extradata + 18, channels > 2 ? 255 : 0); + if (channels > 2) { + AV_WB8(st->codecpar->extradata + 19, channels); /* stream count */ + AV_WB8(st->codecpar->extradata + 20, 0); /* coupled stream count */ + for (int i = 0; i < channels; i++) + AV_WB8(st->codecpar->extradata + 21 + i, i); + } avio_skip(pb, size); } else if ((ret = ff_get_extradata(s, st->codecpar, pb, size)) < 0) { @@ -477,6 +486,154 @@ found_data: #define CAF_MAX_PKT_SIZE 4096 +static int opus_read_frame_length(const uint8_t *p, const uint8_t *end, int *len) +{ + if (end - p < 1) + return AVERROR_INVALIDDATA; + if (p[0] < 252) { + *len = p[0]; + return 1; + } + if (end - p < 2) + return AVERROR_INVALIDDATA; + *len = p[0] + 4 * p[1]; + return 2; +} + +/** + * Copy an Opus packet, converting it to the self-delimiting framing of + * RFC 6716 Appendix B by inserting the length of its last frame. + * + * @return number of bytes written to dst, at most size + 2, or a negative + * error code + */ +static int opus_self_delimit(uint8_t *dst, const uint8_t *src, int size) +{ + const uint8_t *end = src + size; + int pos = 1, last, len, ret; + + switch (src[0] & 3) { + case 0: + last = size - 1; + break; + case 1: + if (!(size & 1)) + return AVERROR_INVALIDDATA; + last = (size - 1) / 2; + break; + case 2: + ret = opus_read_frame_length(src + pos, end, &len); + if (ret < 0) + return ret; + pos += ret; + last = size - pos - len; + break; + default: { + int nb_frames, padding = 0, sum = 0; + + if (size < 2) + return AVERROR_INVALIDDATA; + nb_frames = src[1] & 0x3f; + pos = 2; + if (!nb_frames) + return AVERROR_INVALIDDATA; + if (src[1] & 0x40) { + int b; + do { + if (pos >= size) + return AVERROR_INVALIDDATA; + b = src[pos++]; + padding += b == 255 ? 254 : b; + } while (b == 255); + } + if (src[1] & 0x80) { + for (int i = 0; i < nb_frames - 1; i++) { + ret = opus_read_frame_length(src + pos, end, &len); + if (ret < 0) + return ret; + pos += ret; + sum += len; + } + last = size - pos - padding - sum; + } else { + if ((size - pos - padding) % nb_frames) + return AVERROR_INVALIDDATA; + last = (size - pos - padding) / nb_frames; + } + } + } + + if (last < 0 || last > 1275) + return AVERROR_INVALIDDATA; + + memcpy(dst, src, pos); + if (last < 252) { + dst[pos] = last; + len = 1; + } else { + dst[pos] = 252 + (last & 3); + dst[pos + 1] = (last - dst[pos]) >> 2; + len = 2; + } + memcpy(dst + pos + len, src + pos, size - pos); + + return size + len; +} + +/** + * Apple stores multichannel Opus as one mono stream per channel, each + * preceded by its size as a 16-bit big-endian value. Convert such packets + * to the multistream packet layout of RFC 7845 Section 5.1.1. + */ +static int opus_repack_multistream(AVPacket *pkt, int nb_streams) +{ + const uint8_t *p = pkt->data, *end = pkt->data + pkt->size; + AVBufferRef *buf; + uint8_t *out; + int ret; + + /* each stream loses its 2-byte size prefix and gains at most 2 bytes */ + buf = av_buffer_alloc(pkt->size + AV_INPUT_BUFFER_PADDING_SIZE); + if (!buf) + return AVERROR(ENOMEM); + out = buf->data; + + for (int i = 0; i < nb_streams; i++) { + int size; + + if (end - p < 2) + goto fail; + size = AV_RB16(p); + p += 2; + if (size < 1 || size > end - p) + goto fail; + + if (i < nb_streams - 1) { + ret = opus_self_delimit(out, p, size); + if (ret < 0) + goto fail; + } else { + memcpy(out, p, size); + ret = size; + } + out += ret; + p += size; + } + if (p != end) + goto fail; + + memset(out, 0, AV_INPUT_BUFFER_PADDING_SIZE); + av_buffer_unref(&pkt->buf); + pkt->buf = buf; + pkt->data = buf->data; + pkt->size = out - buf->data; + + return 0; +fail: + av_buffer_unref(&buf); + return AVERROR_INVALIDDATA; +} + static int read_packet(AVFormatContext *s, AVPacket *pkt) { AVIOContext *pb = s->pb; @@ -529,6 +686,16 @@ static int read_packet(AVFormatContext *s, AVPacket *pkt) if (res < 0) return res; + if (st->codecpar->codec_id == AV_CODEC_ID_OPUS && + st->codecpar->ch_layout.nb_channels > 2) { + res = opus_repack_multistream(pkt, st->codecpar->ch_layout.nb_channels); + if (res < 0) { + av_log(s, AV_LOG_ERROR, "invalid multichannel Opus packet\n"); + return res; + } + res = pkt->size; + } + if (!caf->packet_cnt) priming = st->codecpar->initial_padding; -- 2.54.0 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
