AVAssetExportSession does not convert channel order for 5.1/7.1 PCM → AAC

Summary

Exporting a multichannel PCM QuickTime movie with an AVAssetExportSession
preset (as avconvert does) writes the AAC samples in input order while
declaring the MPEG-4 channel order. For 5.1 and 7.1 the source order differs from
the codec order, so every channel is assigned to the wrong speaker and the
channel that lands in the LFE slot is removed by the low-pass filter, i.e. its
content is destroyed.

The layout information is available: AVAssetReader reports the source layout
(bitmap tag 0x10000), and AVAssetWriter with
AVChannelLayoutKey = kAudioChannelLayoutTag_AAC_5_1 performs the conversion
correctly. The export-session preset path does not.

Reproduction

avconvert uses the export session, so it reproduces the defect:

# 5.1: H.264 + PCM, layout 5.1 (L R C LFE Ls Rs), tones 311/521/733/113/1013/1301 Hz
afinfo src_51_h264_pcm.mov
avconvert --source src_51_h264_pcm.mov --output out_51.mp4 \
          --preset PresetHighestQuality --replace
afinfo out_51.mp4          # declares 5.1 (C L R Ls Rs LFE)

# 7.1: 8-channel PCM, layout 7.1 (L R C LFE Ls Rs Lsd Rsd)
avconvert --source src_71_pcm.mov --output out_71.mp4 \
          --preset PresetHighestQuality --replace

A Swift program that calls AVAssetExportSession with
AVAssetExportPresetHighestQuality directly produces a byte-identical 5.1
output (59,789 B) and the same decoded PCM (MD5 match) as avconvert.

Measured result

Decoding the 5.1 output (ffmpeg returns channels in SMPTE order):

output speakertone foundshould be
L521 Hz311 Hz
R733 Hz521 Hz
C311 Hz733 Hz
LFEnear silence (−77 dBFS)113 Hz
Ls113 Hz1013 Hz
Rs1013 Hz1301 Hz

The 1301 Hz tone is absent from every channel. The channel that lands in the LFE
slot is filtered away; note that the LFE tone itself (113 Hz) is still present in
its original slot 3, so this is not "LFE replacing another channel" — it is
one content channel being destroyed and the rest rotated.

For 7.1 the same happens: the 1901 Hz tone is absent from all eight channels and
the slot that received it is near silence (−93.7 dBFS, 3 LSB).

Root-cause isolation (measured with Swift)
experimentresult
AVAssetExportSession + AVAssetExportPresetHighestQualityidentical to avconvert (byte-identical 5.1 output, same PCM MD5) → defect is in the export session
AVAssetWriter + AVChannelLayoutKey = kAudioChannelLayoutTag_AAC_5_1correct: C,L,R,Ls,Rs,LFE = 733,311,521,1013,1301,113
AVAssetWriter + AAC_7_1correct: C=733, L=311, R=521, Ls=1013, Rs=1301, LFE=113; Lc/Rc silent; Lsd/Rsd discarded (AAC 7.1 has no such speakers)
samples relabelled as MPEG_5_1_A before exportoutput unchanged → the source layout tag is ignored
bitmap layout vs equivalent tag (MPEG_5_1_A / MPEG_7_1_C)byte-identical output → not a bitmap-parsing issue
Quad 4.0 (source order equals AAC order)correct, no defect
3.0 (L R C → C L R)permuted only (311,521,733); no LFE slot to destroy
ProRes + 5.1 PCM with a video trackreproduces identically

AVAssetReader prints layout tag = 0x10000 (UseChannelBitmap) for the
source, so the layout is known to AVFoundation; it is the export session that
does not use it for channel mapping.

What the correct behaviour is

For 5.1, the six speakers must keep their own content (C/L/R/Ls/Rs/LFE
remapped according to the MPEG-4 order). For 7.1, AAC_7_1 has no Lsd/Rsd
speakers: the correct conversion keeps C/L/R/Ls/Rs/LFE, leaves Lc/Rc silent and
discards Lsd/Rsd — it must not place either of them into the LFE slot.

Scope

The defect appears whenever the source channel order differs from the codec's
AAC order — which is the case for 5.1 and 7.1, the two common surround layouts.
Quad is unaffected (orders coincide) and 3.0 is permuted without losing a
channel. Presets such as PresetAppleM4A downmix to stereo and are out of scope.

Additional evidence

ffmpeg warns when decoding the 7.1 output:

Assuming an incorrectly encoded 7.1 channel layout instead of a spec-compliant
7.1(wide) layout

i.e. the file is not even spec-compliant as a 7.1 AAC stream.

Impact

Any application that exports multichannel audio through an
AVAssetExportSession preset can silently destroy one channel and rotate the
rest. We have verified avconvert and the export session API directly; GUI
applications that use the same path (QuickTime Player exports, Compressor,
Final Cut) are not verified by us and are not claimed here.

Environment
  • macOS 26.5 (arm64), system AVFoundation
  • Input produced by FFmpeg 9.0.2 (pcm_s24le, 6 or 8 channels)
Reproduce Code
python3 -c "
import numpy as np, wave
freqs=[311,521,733,113,1013,1301]; r=48000; t=np.arange(r*3)/r
d=np.stack([0.5*np.sin(2*np.pi*f*t) for f in freqs],1)
pcm=(np.clip(d,-1,1)*32767).astype('<i2')
w=wave.open('src_51.wav','wb'); w.setnchannels(6); w.setsampwidth(2); w.setframerate(r)
w.writeframes(pcm.tobytes())"
ffmpeg -i src_51.wav -c:a pcm_s24le -f mov src_51.mov
avconvert --source src_51.mov --output out.mp4 --preset PresetHighestQuality --replace
afinfo src_51.mov out.mp4