PR #26024 opened by Baptiste Coudurier (bcoudurier) URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26024 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26024.patch
Apple players (AVFoundation, AudioToolbox, HLS) ignore MP2 and MP3 tracks in mp4 signalled as `mp4a` with objectTypeIndication 0x6B/0x69: no track at all, or CoreMedia error -16170 in HLS fMP4. - **MP2**: written as `mp4a` + OTI 0x40 + a generated AudioSpecificConfig with object type 33 (Layer-2, ISO/IEC 14496-3 subpart 9, one plain frame per access unit). Plays in AVPlayer, AVAssetReader, HLS fMP4 and `afinfo`. - **MP3**: written with a `.mp3` sample entry, as in mov. The MPEG-4 Audio route (object type 34) is not usable: its access units must be mp3_channel_elements (14496-3 9.4.2, the mp3on4 format), and Apple has no decoder for it anyway. `-tag:a mp4a` still writes OTI 0x6B. - **Demuxer**: object types 32/33 now read back as mp1/mp2 instead of mp3on4; 34 stays mp3on4. - MP2/MP3 extradata (e.g. from AVI/WAV) is no longer written as a `glbl` atom. Tested on macOS: AVAssetReader decode matches FFmpeg's (42-45 dB, the usual Apple vs FFmpeg decoder difference) for MP3 and for MP2 at 32/44.1/48 kHz, mono and stereo, progressive and HLS fMP4. Remux round trips are packet-identical. Full FATE passes; `hls-fmp4-big-init` now demuxes `mp2` instead of a mislabelled `mp3`, and `generic-tags-remux-mov` loses the esds. Depends on #26016 (first commit). 🤖 Generated with [Claude Code](https://claude.com/claude-code) From f46f0e0d99b15b3c3c049da526b6700bba82e6fb Mon Sep 17 00:00:00 2001 From: Baptiste Coudurier <[email protected]> Date: Thu, 12 Nov 2026 18:51:44 -0800 Subject: [PATCH 1/4] avformat/movenc: only write a DecoderSpecificInfo for codecs that define one MP3 extradata copied from AVI ended up there. Fixes: #26006 Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Baptiste Coudurier <[email protected]> --- libavformat/movenc.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/libavformat/movenc.c b/libavformat/movenc.c index f36072470f3..3c0adf5d38d 100644 --- a/libavformat/movenc.c +++ b/libavformat/movenc.c @@ -795,11 +795,27 @@ static struct mpeg4_bit_rate_values calculate_mpeg4_bit_rates(MOVTrack *track) return bit_rates; } +static int esds_has_decoder_specific_info(enum AVCodecID codec_id) +{ + switch (codec_id) { + case AV_CODEC_ID_AAC: + case AV_CODEC_ID_MP4ALS: + case AV_CODEC_ID_MPEG4: + case AV_CODEC_ID_VORBIS: + case AV_CODEC_ID_DVD_SUBTITLE: + return 1; + default: + return 0; + } +} + static int mov_write_esds_tag(AVIOContext *pb, MOVTrack *track) // Basic { struct mpeg4_bit_rate_values bit_rates = calculate_mpeg4_bit_rates(track); int64_t pos = avio_tell(pb); - int decoder_specific_info_len = track->extradata_size[track->last_stsd_index] ? + int write_dsi = track->extradata_size[track->last_stsd_index] && + esds_has_decoder_specific_info(track->par->codec_id); + int decoder_specific_info_len = write_dsi ? 5 + track->extradata_size[track->last_stsd_index] : 0; avio_wb32(pb, 0); // size @@ -835,7 +851,7 @@ static int mov_write_esds_tag(AVIOContext *pb, MOVTrack *track) // Basic avio_wb32(pb, bit_rates.max_bit_rate); // maxbitrate avio_wb32(pb, bit_rates.avg_bit_rate); - if (track->extradata_size[track->last_stsd_index]) { + if (write_dsi) { // DecoderSpecific info descriptor put_descr(pb, 0x05, track->extradata_size[track->last_stsd_index]); avio_write(pb, track->extradata[track->last_stsd_index], -- 2.54.0 From 4f9998bb6b7b8723edb3596eb97afcbd21ff7565 Mon Sep 17 00:00:00 2001 From: Baptiste Coudurier <[email protected]> Date: Thu, 12 Nov 2026 19:31:14 -0800 Subject: [PATCH 2/4] avformat/isom: demux MPEG-4 Audio Layer-1/2 object types as mp1/mp2 Their access units are plain frames (ISO/IEC 14496-3 9.4.1), unlike Layer-3. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Baptiste Coudurier <[email protected]> --- libavformat/isom.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/libavformat/isom.c b/libavformat/isom.c index 09be63aa193..46ae385e422 100644 --- a/libavformat/isom.c +++ b/libavformat/isom.c @@ -28,6 +28,7 @@ #include "libavcodec/mpeg4audio.h" #include "libavcodec/mpegaudiodata.h" #include "libavutil/channel_layout.h" +#include "libavutil/mem.h" /* http://www.mp4ra.org */ /* ordered by muxing preference */ @@ -322,9 +323,9 @@ void ff_mp4_parse_es_descr(AVIOContext *pb, int *es_id) static const AVCodecTag mp4_audio_types[] = { { AV_CODEC_ID_MP3ON4, AOT_PS }, /* old mp3on4 draft */ - { AV_CODEC_ID_MP3ON4, AOT_L1 }, /* layer 1 */ - { AV_CODEC_ID_MP3ON4, AOT_L2 }, /* layer 2 */ - { AV_CODEC_ID_MP3ON4, AOT_L3 }, /* layer 3 */ + { AV_CODEC_ID_MP1, AOT_L1 }, /* layer 1, one frame per AU */ + { AV_CODEC_ID_MP2, AOT_L2 }, /* layer 2, one frame per AU */ + { AV_CODEC_ID_MP3ON4, AOT_L3 }, /* layer 3, mp3_channel_elements */ { AV_CODEC_ID_MP4ALS, AOT_ALS }, /* MPEG-4 ALS */ { AV_CODEC_ID_NONE, AOT_NULL }, }; @@ -393,6 +394,13 @@ int ff_mp4_read_dec_config_descr(void *logctx, AVStream *st, AVIOContext *pb) if (!(st->codecpar->codec_id = ff_codec_get_id(mp4_audio_types, cfg.object_type))) st->codecpar->codec_id = AV_CODEC_ID_AAC; + /* Layer 1/2 access units are plain frames, the AudioSpecificConfig + * is not their extradata. */ + if (st->codecpar->codec_id == AV_CODEC_ID_MP1 || + st->codecpar->codec_id == AV_CODEC_ID_MP2) { + av_freep(&st->codecpar->extradata); + st->codecpar->extradata_size = 0; + } } } return 0; -- 2.54.0 From 999d0948d3d2f3a354a69d7f391d101b14bbdda7 Mon Sep 17 00:00:00 2001 From: Baptiste Coudurier <[email protected]> Date: Thu, 12 Nov 2026 23:47:05 -0800 Subject: [PATCH 3/4] avformat/movenc: write MP2 as MPEG-4 Audio object type 33 ISO/IEC 14496-3 Layer-2, with a generated AudioSpecificConfig; Apple players ignore OTI 0x69/0x6B. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Baptiste Coudurier <[email protected]> --- libavformat/Makefile | 3 +- libavformat/movenc.c | 55 +++++++++++++++++++++++++------- tests/fate/mov.mak | 5 +++ tests/ref/fate/hls-fmp4-big-init | 2 +- tests/ref/fate/mov-mp4-mp2-aot33 | 23 +++++++++++++ 5 files changed, 75 insertions(+), 13 deletions(-) create mode 100644 tests/ref/fate/mov-mp4-mp2-aot33 diff --git a/libavformat/Makefile b/libavformat/Makefile index 891d7f82d9a..d2e0721ccf3 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -757,7 +757,8 @@ SHLIBOBJS-$(CONFIG_JPEGXL_ANIM_DEMUXER) += jpegxl_parse.o SHLIBOBJS-$(CONFIG_MATROSKA_DEMUXER) += mpeg4audio_sample_rates.o SHLIBOBJS-$(CONFIG_MATROSKA_MUXER) += opus_frame_duration_tab.o SHLIBOBJS-$(CONFIG_MOV_DEMUXER) += ac3_channel_layout_tab.o -SHLIBOBJS-$(CONFIG_MOV_MUXER) += mpegaudiotabs.o +SHLIBOBJS-$(CONFIG_MOV_MUXER) += mpeg4audio_sample_rates.o \ + mpegaudiotabs.o SHLIBOBJS-$(CONFIG_MP3_MUXER) += mpegaudiotabs.o SHLIBOBJS-$(CONFIG_MXF_MUXER) += golomb_tab.o \ rangecoder_dec.o diff --git a/libavformat/movenc.c b/libavformat/movenc.c index 3c0adf5d38d..8a20f0427ad 100644 --- a/libavformat/movenc.c +++ b/libavformat/movenc.c @@ -43,6 +43,7 @@ #include "libavcodec/ac3_parser_internal.h" #include "libavcodec/dnxhddata.h" #include "libavcodec/flac.h" +#include "libavcodec/mpeg4audio.h" #include "libavcodec/mpegaudiodata.h" #include "libavcodec/get_bits.h" @@ -809,14 +810,46 @@ static int esds_has_decoder_specific_info(enum AVCodecID codec_id) } } +/* AudioSpecificConfig for Layer-2, ISO/IEC 14496-3 subpart 9 */ +static int mp2_audio_specific_config(uint8_t *buf, int size, MOVTrack *track) +{ + const AVCodecParameters *par = track->par; + int channels = par->ch_layout.nb_channels; + PutBitContext pbc; + int sfi; + + for (sfi = 0; sfi < 13; sfi++) + if (ff_mpeg4audio_sample_rates[sfi] == par->sample_rate) + break; + + init_put_bits(&pbc, buf, size); + put_bits(&pbc, 5, AOT_ESCAPE); + put_bits(&pbc, 6, AOT_L2 - 32); + put_bits(&pbc, 4, sfi < 13 ? sfi : 15); + if (sfi == 13) + put_bits(&pbc, 24, par->sample_rate); + put_bits(&pbc, 4, channels <= 2 ? channels : 0); + put_bits(&pbc, 1, 0); // MPEG_1_2_SpecificConfig.extension + flush_put_bits(&pbc); + return put_bytes_output(&pbc); +} + static int mov_write_esds_tag(AVIOContext *pb, MOVTrack *track) // Basic { struct mpeg4_bit_rate_values bit_rates = calculate_mpeg4_bit_rates(track); int64_t pos = avio_tell(pb); - int write_dsi = track->extradata_size[track->last_stsd_index] && - esds_has_decoder_specific_info(track->par->codec_id); - int decoder_specific_info_len = write_dsi ? - 5 + track->extradata_size[track->last_stsd_index] : 0; + const uint8_t *dsi = track->extradata[track->last_stsd_index]; + int dsi_size = track->extradata_size[track->last_stsd_index]; + uint8_t asc[6]; + int decoder_specific_info_len; + + if (track->par->codec_id == AV_CODEC_ID_MP2) { + dsi = asc; + dsi_size = mp2_audio_specific_config(asc, sizeof(asc), track); + } else if (!esds_has_decoder_specific_info(track->par->codec_id)) { + dsi_size = 0; + } + decoder_specific_info_len = dsi_size ? 5 + dsi_size : 0; avio_wb32(pb, 0); // size ffio_wfourcc(pb, "esds"); @@ -831,9 +864,10 @@ static int mov_write_esds_tag(AVIOContext *pb, MOVTrack *track) // Basic put_descr(pb, 0x04, 13 + decoder_specific_info_len); // Object type indication - if ((track->par->codec_id == AV_CODEC_ID_MP2 || - track->par->codec_id == AV_CODEC_ID_MP3) && - track->par->sample_rate > 24000) + if (track->par->codec_id == AV_CODEC_ID_MP2) + avio_w8(pb, 0x40); // 14496-3 Layer-2 + else if (track->par->codec_id == AV_CODEC_ID_MP3 && + track->par->sample_rate > 24000) avio_w8(pb, 0x6B); // 11172-3 else avio_w8(pb, ff_codec_get_tag(ff_mp4_obj_type, track->par->codec_id)); @@ -851,11 +885,10 @@ static int mov_write_esds_tag(AVIOContext *pb, MOVTrack *track) // Basic avio_wb32(pb, bit_rates.max_bit_rate); // maxbitrate avio_wb32(pb, bit_rates.avg_bit_rate); - if (write_dsi) { + if (dsi_size) { // DecoderSpecific info descriptor - put_descr(pb, 0x05, track->extradata_size[track->last_stsd_index]); - avio_write(pb, track->extradata[track->last_stsd_index], - track->extradata_size[track->last_stsd_index]); + put_descr(pb, 0x05, dsi_size); + avio_write(pb, dsi, dsi_size); } // SL descriptor diff --git a/tests/fate/mov.mak b/tests/fate/mov.mak index 01b1346cec5..611a7fb5b82 100644 --- a/tests/fate/mov.mak +++ b/tests/fate/mov.mak @@ -420,6 +420,11 @@ fate-mov-dfpwm-last-packet: CMD = transcode lavfi "sine=r=48000,atrim=end_sample fate-mov-vfr: CMP = oneline fate-mov-vfr: REF = 1558b4a9398d8635783c93f84eb5a60d +# MP2 is written as MPEG-4 Audio object type 33 (ISO/IEC 14496-3 subpart 9) +# and has to demux back as mp2, without the AudioSpecificConfig as extradata. +FATE_MOV_FFMPEG_FFPROBE-$(call TRANSCODE, MP2, MP4 MOV, LAVFI_INDEV SINE_FILTER ATRIM_FILTER ARESAMPLE_FILTER) += fate-mov-mp4-mp2-aot33 +fate-mov-mp4-mp2-aot33: CMD = transcode lavfi "sine=r=44100,atrim=end_sample=11520" mp4 "-auto_conversion_filters -c:a mp2" "-c copy" "-show_entries stream=codec_name,codec_tag_string,sample_rate,channels,extradata_size" + # The QuickTime user data list has to carry the composer atom the demuxer # already reads: without it a mov to mov remux dropped the tag. FATE_MOV_FFMPEG_FFPROBE-$(call TRANSCODE, PCM_S16LE, MOV, WAV_DEMUXER ARESAMPLE_FILTER) += fate-mov-composer-metadata diff --git a/tests/ref/fate/hls-fmp4-big-init b/tests/ref/fate/hls-fmp4-big-init index 07a81414c69..797bd1edf24 100644 --- a/tests/ref/fate/hls-fmp4-big-init +++ b/tests/ref/fate/hls-fmp4-big-init @@ -1,6 +1,6 @@ #tb 0: 1/16000 #media_type 0: audio -#codec_id 0: mp3 +#codec_id 0: mp2 #sample_rate 0: 16000 #channel_layout_name 0: mono 0, 0, 0, 1152, 72, 0x40951fab diff --git a/tests/ref/fate/mov-mp4-mp2-aot33 b/tests/ref/fate/mov-mp4-mp2-aot33 new file mode 100644 index 00000000000..3ef54862cf6 --- /dev/null +++ b/tests/ref/fate/mov-mp4-mp2-aot33 @@ -0,0 +1,23 @@ +bdd4d4de10c8b559c12a478d6fb12575 *tests/data/fate/mov-mp4-mp2-aot33.mp4 +13304 tests/data/fate/mov-mp4-mp2-aot33.mp4 +#tb 0: 1/44100 +#media_type 0: audio +#codec_id 0: mp2 +#sample_rate 0: 44100 +#channel_layout_name 0: mono +0, -481, -481, 1152, 1253, 0x5c89ebe2, S=1, Skip Samples, 10, 0x08d300e2 +0, 671, 671, 1152, 1254, 0x441e219e +0, 1823, 1823, 1152, 1254, 0xbc8ed1b7 +0, 2975, 2975, 1152, 1254, 0xcf6eda5f +0, 4127, 4127, 1152, 1254, 0x880ff8dc +0, 5279, 5279, 1152, 1254, 0x2827edb9 +0, 6431, 6431, 1152, 1254, 0xb3dfbaae +0, 7583, 7583, 1152, 1254, 0x0053dc95 +0, 8735, 8735, 1152, 1253, 0x37d4ed14 +0, 9887, 9887, 1152, 1254, 0xb106f757 +[STREAM] +codec_name=mp2 +codec_tag_string=mp4a +sample_rate=44100 +channels=1 +[/STREAM] -- 2.54.0 From f6d3526ee503f489632b60c21ce1d9f6bcfe182e Mon Sep 17 00:00:00 2001 From: Baptiste Coudurier <[email protected]> Date: Thu, 12 Nov 2026 23:47:16 -0800 Subject: [PATCH 4/4] avformat/movenc: write MP3 in mp4 with a .mp3 sample entry Apple players ignore mp4a with OTI 0x69/0x6B; -tag:a mp4a still writes it. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Baptiste Coudurier <[email protected]> --- libavformat/movenc.c | 5 +++- tests/fate/mov.mak | 4 ++++ tests/ref/fate/generic-tags-remux-mov | 4 ++-- tests/ref/fate/mov-mp4-mp3-remux | 33 +++++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 tests/ref/fate/mov-mp4-mp3-remux diff --git a/libavformat/movenc.c b/libavformat/movenc.c index 8a20f0427ad..18211ac832f 100644 --- a/libavformat/movenc.c +++ b/libavformat/movenc.c @@ -1649,7 +1649,9 @@ static int mov_write_audio_tag(AVFormatContext *s, AVIOContext *pb, MOVMuxContex if (ret < 0) return ret; ret = mov_write_pcmc_tag(s, pb, track); - } else if (track->extradata_size[track->last_stsd_index] > 0) + } else if (track->extradata_size[track->last_stsd_index] > 0 && + track->par->codec_id != AV_CODEC_ID_MP2 && + track->par->codec_id != AV_CODEC_ID_MP3) ret = mov_write_glbl_tag(pb, track); if (ret < 0) @@ -9551,6 +9553,7 @@ static const AVCodecTag codec_mp4_tags[] = { { AV_CODEC_ID_APPLE_APAC, MKTAG('a', 'p', 'a', 'c') }, { AV_CODEC_ID_ALAC, MKTAG('a', 'l', 'a', 'c') }, { AV_CODEC_ID_MP4ALS, MKTAG('m', 'p', '4', 'a') }, + { AV_CODEC_ID_MP3, MKTAG('.', 'm', 'p', '3') }, { AV_CODEC_ID_MP3, MKTAG('m', 'p', '4', 'a') }, { AV_CODEC_ID_MP2, MKTAG('m', 'p', '4', 'a') }, { AV_CODEC_ID_AC3, MKTAG('a', 'c', '-', '3') }, diff --git a/tests/fate/mov.mak b/tests/fate/mov.mak index 611a7fb5b82..cc34bb0118c 100644 --- a/tests/fate/mov.mak +++ b/tests/fate/mov.mak @@ -425,6 +425,10 @@ fate-mov-vfr: REF = 1558b4a9398d8635783c93f84eb5a60d FATE_MOV_FFMPEG_FFPROBE-$(call TRANSCODE, MP2, MP4 MOV, LAVFI_INDEV SINE_FILTER ATRIM_FILTER ARESAMPLE_FILTER) += fate-mov-mp4-mp2-aot33 fate-mov-mp4-mp2-aot33: CMD = transcode lavfi "sine=r=44100,atrim=end_sample=11520" mp4 "-auto_conversion_filters -c:a mp2" "-c copy" "-show_entries stream=codec_name,codec_tag_string,sample_rate,channels,extradata_size" +# MP3 is written with a .mp3 sample entry and no esds. +FATE_MOV_FFMPEG_FFPROBE_SAMPLES-$(call REMUX, MP4 MOV, MP3_DECODER) += fate-mov-mp4-mp3-remux +fate-mov-mp4-mp3-remux: CMD = transcode mov $(TARGET_SAMPLES)/mpegaudio/packed_maindata.mp3.mp4 mp4 "-c copy" "-c copy -t 0.5" "-show_entries stream=codec_name,codec_tag_string,sample_rate,channels,extradata_size" + # The QuickTime user data list has to carry the composer atom the demuxer # already reads: without it a mov to mov remux dropped the tag. FATE_MOV_FFMPEG_FFPROBE-$(call TRANSCODE, PCM_S16LE, MOV, WAV_DEMUXER ARESAMPLE_FILTER) += fate-mov-composer-metadata diff --git a/tests/ref/fate/generic-tags-remux-mov b/tests/ref/fate/generic-tags-remux-mov index 25bc866092f..9af5f10b58e 100644 --- a/tests/ref/fate/generic-tags-remux-mov +++ b/tests/ref/fate/generic-tags-remux-mov @@ -1,5 +1,5 @@ -f6544f83b6b59a90f6aa2b50c04b5776 *tests/data/fate/generic-tags-remux-mov.mp4 -18063 tests/data/fate/generic-tags-remux-mov.mp4 +6412d14ae7662c00ed055e347686d0b9 *tests/data/fate/generic-tags-remux-mov.mp4 +18019 tests/data/fate/generic-tags-remux-mov.mp4 #tb 0: 1/44100 #media_type 0: audio #codec_id 0: mp3 diff --git a/tests/ref/fate/mov-mp4-mp3-remux b/tests/ref/fate/mov-mp4-mp3-remux new file mode 100644 index 00000000000..0c11c57ff21 --- /dev/null +++ b/tests/ref/fate/mov-mp4-mp3-remux @@ -0,0 +1,33 @@ +4bb0bc8b2a53c159f945d8d1a0c37a0c *tests/data/fate/mov-mp4-mp3-remux.mp4 +144788 tests/data/fate/mov-mp4-mp3-remux.mp4 +#tb 0: 1/44100 +#media_type 0: audio +#codec_id 0: mp3 +#sample_rate 0: 44100 +#channel_layout_name 0: stereo +0, 0, 0, 1152, 36, 0x8e260589 +0, 1152, 1152, 1152, 36, 0x8e260589 +0, 2304, 2304, 1152, 36, 0x8e260589 +0, 3456, 3456, 1152, 36, 0x8e260589 +0, 4608, 4608, 1152, 36, 0x8e260589 +0, 5760, 5760, 1152, 36, 0x8e260589 +0, 6912, 6912, 1152, 69, 0xdb550f09 +0, 8064, 8064, 1152, 104, 0xbaeb1aa8 +0, 9216, 9216, 1152, 104, 0xbaeb1aa8 +0, 10368, 10368, 1152, 104, 0xbaeb1aa8 +0, 11520, 11520, 1152, 104, 0xbaeb1aa8 +0, 12672, 12672, 1152, 104, 0xbaeb1aa8 +0, 13824, 13824, 1152, 104, 0xbaeb1aa8 +0, 14976, 14976, 1152, 36, 0x8e260589 +0, 16128, 16128, 1152, 601, 0xc34f1dc9 +0, 17280, 17280, 1152, 716, 0x6c2665e5 +0, 18432, 18432, 1152, 672, 0xf9873f4b +0, 19584, 19584, 1152, 631, 0xd13333cc +0, 20736, 20736, 1152, 604, 0xc0bb13e5 +0, 21888, 21888, 1152, 642, 0x4e873319 +[STREAM] +codec_name=mp3 +codec_tag_string=.mp3 +sample_rate=44100 +channels=2 +[/STREAM] -- 2.54.0 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
