PR #26034 opened by Baptiste Coudurier (bcoudurier) URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26034 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26034.patch
Fixes #23138 for CAF, and the AC-3 stream copy regression in MOV since e6c2c8703732. - mov_chan: for AC-3, AAC and ALAC, match the layout tag on the set of channels, since their parsers and encoders export native order layouts. A back pair matches the surround pair. - cafenc: write the chan chunk from the same codec tables as movenc (FLAC uses the WAVE ones), instead of the WAVE order for every codec. - cafenc: write the dac3 box as the AC-3 magic cookie, taken from the first frame. Without it, Apple's decoder guesses the channel mode from the channel count (2.1 and quad decoded wrong). Checked by decoding with Apple's tools, one tone per channel: 185 encode and stream copy cases (AC-3, E-AC-3, ALAC, AAC, FLAC, PCM, MP2 in CAF and MOV). 162 now have every channel on the right speaker, against 128 on master, with no new failures. The rest fail on master too: AC-3 3.0(back), 3.1 and 4.1 also decode wrong from raw .ac3 files, and FLAC CAF and AAC 2.1 do not open. For all AC-3 layouts, the desc and chan chunks are byte-identical to `afconvert -f caff -d 0`. Its dac3 only differs in bit_rate_code, which afconvert sets to half the frame rate except at 192 kb/s; this follows frmsizcod as TS 102 366 F.4.2.7 requires. 🤖 Generated with [Claude Code](https://claude.com/claude-code) From 97f6654faed7966f6e9ee455ae4ee56a852eda4c Mon Sep 17 00:00:00 2001 From: Baptiste Coudurier <[email protected]> Date: Fri, 13 Nov 2026 13:33:42 -0800 Subject: [PATCH 1/3] avformat/mov_chan: match the channel set for codecs with their own order Their parsers and encoders export native order layouts. Fixes a regression since e6c2c8703732. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Baptiste Coudurier <[email protected]> --- libavformat/mov_chan.c | 46 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/libavformat/mov_chan.c b/libavformat/mov_chan.c index 8274e62f3d7..7d7d246a97d 100644 --- a/libavformat/mov_chan.c +++ b/libavformat/mov_chan.c @@ -473,6 +473,37 @@ static int is_layout_valid_for_tag(const AVChannelLayout *ch_layout, uint32_t ta return 0; } +/** + * Get the set of channels of a layout tag. + * + * @return the channel mask, or 0 if it cannot be expressed as one + */ +static uint64_t layout_tag_mask(uint32_t tag) +{ + const struct MovChannelLayoutMap *layout_map = find_layout_map(tag, mov_ch_layout_map); + uint64_t mask = 0; + + if (!layout_map) + return 0; + + for (int i = 0; i < (tag & 0xFFFF); i++) { + enum AVChannel id = layout_map[i].id; + if (id < 0 || id >= 64 || mask & (1ULL << id)) + return 0; + mask |= 1ULL << id; + } + return mask; +} + +static uint32_t find_layout_tag_by_mask(const enum MovChannelLayoutTag *layouts, + uint64_t mask) +{ + for (int i = 0; layouts[i] != 0; i++) + if (layout_tag_mask(layouts[i]) == mask) + return layouts[i]; + return 0; +} + int ff_mov_get_channel_layout_tag(const AVCodecParameters *par, uint32_t *layout, uint32_t *bitmap, @@ -497,6 +528,21 @@ int ff_mov_get_channel_layout_tag(const AVCodecParameters *par, break; tag = layouts[i]; + + /* Parsers and encoders of codecs with their own channel order export + * a native order layout: only the set of channels is meaningful, the + * bitstream defines the order. */ + if (!tag && layouts != mov_ch_layouts_wav && + par->ch_layout.order == AV_CHANNEL_ORDER_NATIVE) { + const uint64_t back = AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT; + const uint64_t side = AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT; + uint64_t mask = par->ch_layout.u.mask; + + tag = find_layout_tag_by_mask(layouts, mask); + /* the tags map the surround pair to side channels */ + if (!tag && (mask & back) == back && !(mask & side)) + tag = find_layout_tag_by_mask(layouts, mask ^ back ^ side); + } } *layout = tag; -- 2.54.0 From ca5340f64fe361a3ef12b82b20ae69497d22c5aa Mon Sep 17 00:00:00 2001 From: Baptiste Coudurier <[email protected]> Date: Fri, 13 Nov 2026 13:33:42 -0800 Subject: [PATCH 2/3] avformat/cafenc: write the channel layout for the codec's channel order AC-3 and ALAC were tagged with the WAVE order. Fixes #23138. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Baptiste Coudurier <[email protected]> --- libavformat/Makefile | 2 +- libavformat/cafenc.c | 48 ++++++++++++++++++++++++++++++++++++------ libavformat/mov_chan.c | 1 + 3 files changed, 44 insertions(+), 7 deletions(-) diff --git a/libavformat/Makefile b/libavformat/Makefile index 891d7f82d9a..d2515adac19 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -176,7 +176,7 @@ OBJS-$(CONFIG_BFSTM_DEMUXER) += brstm.o OBJS-$(CONFIG_BRSTM_DEMUXER) += brstm.o OBJS-$(CONFIG_C93_DEMUXER) += c93.o voc_packet.o voc.o OBJS-$(CONFIG_CAF_DEMUXER) += cafdec.o caf.o mov_chan.o mov_esds.o -OBJS-$(CONFIG_CAF_MUXER) += cafenc.o caf.o riff.o +OBJS-$(CONFIG_CAF_MUXER) += cafenc.o caf.o mov_chan.o riff.o OBJS-$(CONFIG_CAVSVIDEO_DEMUXER) += cavsvideodec.o rawdec.o OBJS-$(CONFIG_CAVSVIDEO_MUXER) += rawenc.o OBJS-$(CONFIG_CDG_DEMUXER) += cdg.o diff --git a/libavformat/cafenc.c b/libavformat/cafenc.c index b7e7d1c56fd..21a78dfee22 100644 --- a/libavformat/cafenc.c +++ b/libavformat/cafenc.c @@ -23,7 +23,7 @@ #include "avformat.h" #include "caf.h" -#include "isom.h" +#include "mov_chan.h" #include "avio_internal.h" #include "mux.h" #include "libavutil/intfloat.h" @@ -144,6 +144,44 @@ static int caf_write_init(struct AVFormatContext *s) return 0; } +static int caf_write_chan(AVIOContext *pb, const AVCodecParameters *par) +{ + uint32_t layout_tag, bitmap, *channel_desc; + int num_desc, ret; + + ret = ff_mov_get_channel_layout_tag(par, &layout_tag, &bitmap, &channel_desc); + if (ret == AVERROR(ENOSYS)) + return 0; + if (ret < 0) + return ret; + + /* The bitstream defines the order, which no tag describes. */ + if (layout_tag == MOV_CH_LAYOUT_UNKNOWN) { + int channels = par->ch_layout.nb_channels; + layout_tag = channels == 1 ? MOV_CH_LAYOUT_MONO : + channels == 2 ? MOV_CH_LAYOUT_STEREO : + MOV_CH_LAYOUT_UNKNOWN | channels; + } + + num_desc = layout_tag ? 0 : par->ch_layout.nb_channels; + + ffio_wfourcc(pb, "chan"); + avio_wb64(pb, 12 + 20 * num_desc); + avio_wb32(pb, layout_tag); //< mChannelLayoutTag + avio_wb32(pb, bitmap); //< mChannelBitmap + avio_wb32(pb, num_desc); //< mNumberChannelDescriptions + for (int i = 0; i < num_desc; i++) { + avio_wb32(pb, channel_desc[i]); //< mChannelLabel + avio_wb32(pb, 0); //< mChannelFlags + avio_wb32(pb, 0); //< mCoordinates[0] + avio_wb32(pb, 0); //< mCoordinates[1] + avio_wb32(pb, 0); //< mCoordinates[2] + } + av_free(channel_desc); + + return 0; +} + static int caf_write_header(AVFormatContext *s) { AVIOContext *pb = s->pb; @@ -152,6 +190,7 @@ static int caf_write_header(AVFormatContext *s) const AVDictionaryEntry *t = NULL; unsigned int codec_tag = ff_codec_get_tag(ff_codec_caf_tags, par->codec_id); int64_t chunk_size = 0; + int ret; int sample_rate = par->sample_rate; if (!par->block_align && !(pb->seekable & AVIO_SEEKABLE_NORMAL)) { @@ -185,11 +224,8 @@ static int caf_write_header(AVFormatContext *s) avio_wb32(pb, par->ch_layout.nb_channels); //< mChannelsPerFrame avio_wb32(pb, av_get_bits_per_sample(par->codec_id)); //< mBitsPerChannel - if (par->ch_layout.order == AV_CHANNEL_ORDER_NATIVE) { - ffio_wfourcc(pb, "chan"); - avio_wb64(pb, 12); - ff_mov_write_chan(pb, par->ch_layout.u.mask); - } + if ((ret = caf_write_chan(pb, par)) < 0) + return ret; if (par->codec_id == AV_CODEC_ID_ALAC) { ffio_wfourcc(pb, "kuki"); diff --git a/libavformat/mov_chan.c b/libavformat/mov_chan.c index 7d7d246a97d..6a36e3b33f3 100644 --- a/libavformat/mov_chan.c +++ b/libavformat/mov_chan.c @@ -338,6 +338,7 @@ static const struct { { AV_CODEC_ID_AC3, mov_ch_layouts_ac3 }, { AV_CODEC_ID_ADPCM_IMA_QT, mov_ch_layouts_wav }, { AV_CODEC_ID_ALAC, mov_ch_layouts_alac }, + { AV_CODEC_ID_FLAC, mov_ch_layouts_wav }, { AV_CODEC_ID_MACE3, mov_ch_layouts_wav }, { AV_CODEC_ID_MACE6, mov_ch_layouts_wav }, { AV_CODEC_ID_PCM_ALAW, mov_ch_layouts_wav }, -- 2.54.0 From 447818493cb9dcf7b281046342c9c0529034f83a Mon Sep 17 00:00:00 2001 From: Baptiste Coudurier <[email protected]> Date: Fri, 13 Nov 2026 13:47:56 -0800 Subject: [PATCH 3/3] avformat/cafenc: write the AC-3 magic cookie Without dac3, the channel mode is guessed from the channel count. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Baptiste Coudurier <[email protected]> --- configure | 2 +- libavformat/cafenc.c | 91 ++++++++++++++++++++++++++++++------ tests/fate/caf.mak | 3 ++ tests/ref/fate/caf-ac3-remux | 18 +++++++ 4 files changed, 98 insertions(+), 16 deletions(-) create mode 100644 tests/ref/fate/caf-ac3-remux diff --git a/configure b/configure index 69793448886..39bb1288cea 100755 --- a/configure +++ b/configure @@ -3900,7 +3900,7 @@ avi_demuxer_select="riffdec" avi_muxer_select="riffenc" avif_muxer_select="mov_muxer" caf_demuxer_select="iso_media" -caf_muxer_select="iso_media" +caf_muxer_select="ac3_parser iso_media" dash_muxer_select="mp4_muxer" dash_demuxer_deps="libxml2" daud_muxer_select="pcm_rechunk_bsf" diff --git a/libavformat/cafenc.c b/libavformat/cafenc.c index 21a78dfee22..9472b04e9e9 100644 --- a/libavformat/cafenc.c +++ b/libavformat/cafenc.c @@ -29,6 +29,8 @@ #include "libavutil/intfloat.h" #include "libavutil/dict.h" #include "libavutil/mem.h" +#include "libavcodec/ac3_parser_internal.h" +#include "libavcodec/put_bits.h" #define FRAME_SIZE_OFFSET 40 @@ -182,7 +184,42 @@ static int caf_write_chan(AVIOContext *pb, const AVCodecParameters *par) return 0; } -static int caf_write_header(AVFormatContext *s) +static int caf_write_dac3(AVFormatContext *s, AVIOContext *pb, const AVPacket *pkt) +{ + AC3HeaderInfo *hdr = NULL; + PutBitContext pbc; + uint8_t buf[3]; + int ret = avpriv_ac3_parse_header(&hdr, pkt->data, pkt->size); + + if (ret < 0 || hdr->ac3_bit_rate_code < 0) { + av_free(hdr); + if (ret == AVERROR(ENOMEM)) + return ret; + av_log(s, AV_LOG_WARNING, "Invalid first AC-3 frame, not writing dac3\n"); + return 0; + } + + init_put_bits(&pbc, buf, sizeof(buf)); + put_bits(&pbc, 2, hdr->sr_code); + put_bits(&pbc, 5, hdr->bitstream_id); + put_bits(&pbc, 3, hdr->bitstream_mode); + put_bits(&pbc, 3, hdr->channel_mode); + put_bits(&pbc, 1, hdr->lfe_on); + put_bits(&pbc, 5, hdr->ac3_bit_rate_code); + put_bits(&pbc, 5, 0); // reserved + flush_put_bits(&pbc); + av_free(hdr); + + ffio_wfourcc(pb, "kuki"); + avio_wb64(pb, 11); + avio_wb32(pb, 11); + ffio_wfourcc(pb, "dac3"); + avio_write(pb, buf, sizeof(buf)); + + return 0; +} + +static int caf_write_chunks(AVFormatContext *s, const AVPacket *pkt) { AVIOContext *pb = s->pb; AVCodecParameters *par = s->streams[0]->codecpar; @@ -193,20 +230,6 @@ static int caf_write_header(AVFormatContext *s) int ret; int sample_rate = par->sample_rate; - if (!par->block_align && !(pb->seekable & AVIO_SEEKABLE_NORMAL)) { - av_log(s, AV_LOG_ERROR, "Muxing variable packet size not supported on non seekable output\n"); - return AVERROR_INVALIDDATA; - } - - caf->frame_size = par->frame_size; - if (par->codec_id != AV_CODEC_ID_MP3 || caf->frame_size != 576) - caf->frame_size = samples_per_packet(par); - - if (!caf->frame_size && !(pb->seekable & AVIO_SEEKABLE_NORMAL)) { - av_log(s, AV_LOG_ERROR, "Muxing variable frame size not supported on non seekable output\n"); - return AVERROR_INVALIDDATA; - } - if (par->codec_id == AV_CODEC_ID_OPUS) sample_rate = 48000; @@ -243,6 +266,9 @@ static int caf_write_header(AVFormatContext *s) avio_wb16(pb, 0x81FF); /* Mode set (all modes for AMR_NB) */ avio_w8(pb, 0x00); /* Mode change period (no restriction) */ avio_w8(pb, 0x01); /* Frames per sample */ + } else if (par->codec_id == AV_CODEC_ID_AC3 && pkt) { + if ((ret = caf_write_dac3(s, pb, pkt)) < 0) + return ret; } else if (par->codec_id == AV_CODEC_ID_QDM2 || par->codec_id == AV_CODEC_ID_QDMC) { ffio_wfourcc(pb, "kuki"); avio_wb64(pb, par->extradata_size); @@ -272,6 +298,33 @@ static int caf_write_header(AVFormatContext *s) return 0; } +static int caf_write_header(AVFormatContext *s) +{ + AVIOContext *pb = s->pb; + AVCodecParameters *par = s->streams[0]->codecpar; + CAFContext *caf = s->priv_data; + + if (!par->block_align && !(pb->seekable & AVIO_SEEKABLE_NORMAL)) { + av_log(s, AV_LOG_ERROR, "Muxing variable packet size not supported on non seekable output\n"); + return AVERROR_INVALIDDATA; + } + + caf->frame_size = par->frame_size; + if (par->codec_id != AV_CODEC_ID_MP3 || caf->frame_size != 576) + caf->frame_size = samples_per_packet(par); + + if (!caf->frame_size && !(pb->seekable & AVIO_SEEKABLE_NORMAL)) { + av_log(s, AV_LOG_ERROR, "Muxing variable frame size not supported on non seekable output\n"); + return AVERROR_INVALIDDATA; + } + + /* The AC-3 magic cookie is taken from the first frame. */ + if (par->codec_id == AV_CODEC_ID_AC3) + return 0; + + return caf_write_chunks(s, NULL); +} + static uint8_t *put_variable_length_num(uint8_t *buf, uint32_t num) { for (int j = 4; j > 0; j--) { @@ -287,6 +340,10 @@ static int caf_write_packet(AVFormatContext *s, AVPacket *pkt) { CAFContext *caf = s->priv_data; AVStream *const st = s->streams[0]; + int ret; + + if (!caf->data && (ret = caf_write_chunks(s, pkt)) < 0) + return ret; if (!st->codecpar->block_align || !caf->frame_size) { #if SIZE_MAX - 10 < UINT_MAX @@ -318,6 +375,10 @@ static int caf_write_trailer(AVFormatContext *s) AVIOContext *pb = s->pb; AVStream *st = s->streams[0]; AVCodecParameters *par = st->codecpar; + int ret; + + if (!caf->data && (ret = caf_write_chunks(s, NULL)) < 0) + return ret; if (pb->seekable & AVIO_SEEKABLE_NORMAL) { int64_t file_size = avio_tell(pb); diff --git a/tests/fate/caf.mak b/tests/fate/caf.mak index 4ee9c95a5c8..802ee368d31 100644 --- a/tests/fate/caf.mak +++ b/tests/fate/caf.mak @@ -1,6 +1,9 @@ FATE_CAF_FFMPEG-$(call CRC, CAF) += fate-caf-demux fate-caf-demux: CMD = crc -i $(TARGET_SAMPLES)/caf/caf-pcm16.caf -c copy +FATE_CAF_FFMPEG_FFPROBE-$(call REMUX, CAF, AC3_DEMUXER) += fate-caf-ac3-remux +fate-caf-ac3-remux: CMD = transcode ac3 $(TARGET_SAMPLES)/ac3/millers_crossing_4.0.ac3 caf "-c copy" "-c copy -t 0.2" "-show_entries stream=channel_layout" + FATE_CAF_FFMPEG_FFPROBE-$(call REMUX, CAF, MOV_DEMUXER) += fate-caf-alac-remux fate-caf-alac-remux: CMD = transcode m4a $(TARGET_SAMPLES)/lossless-audio/inside.m4a caf "-map 0:a -c copy -metadata major_brand= " "-c copy -t 0.2" "-show_entries format_tags" diff --git a/tests/ref/fate/caf-ac3-remux b/tests/ref/fate/caf-ac3-remux new file mode 100644 index 00000000000..48c9fbbfb0e --- /dev/null +++ b/tests/ref/fate/caf-ac3-remux @@ -0,0 +1,18 @@ +182cc63e4c17c0837d8c946468e3dd6b *tests/data/fate/caf-ac3-remux.caf +95507 tests/data/fate/caf-ac3-remux.caf +#extradata 0: 11, 0x0957025f +#tb 0: 1/48000 +#media_type 0: audio +#codec_id 0: ac3 +#sample_rate 0: 48000 +#channel_layout_name 0: 4.0 +0, 0, 0, 1536, 1536, 0x1a9401a1 +0, 1536, 1536, 1536, 1536, 0x6768f3cf +0, 3072, 3072, 1536, 1536, 0xfdbde2da +0, 4608, 4608, 1536, 1536, 0x6773f7cd +0, 6144, 6144, 1536, 1536, 0x88e0fa80 +0, 7680, 7680, 1536, 1536, 0x94280408 +0, 9216, 9216, 1536, 1536, 0x4afffc71 +[STREAM] +channel_layout=4.0 +[/STREAM] -- 2.54.0 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
