PR #25985 opened by Baptiste Coudurier (bcoudurier) URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25985 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25985.patch
Adds AAC muxing to the CAF muxer, written the way Apple's tools write it, and fixes the CAF demuxer's handling of HE-AAC and of long remainders. **avformat/isom: share the MPEG-4 descriptor writer** Moves movenc's `put_descr()` to isom as `ff_mp4_write_descr()`, for use by the CAF muxer. **avformat/cafenc: support AAC** - `desc` describes the core layer (core sample rate, 1024 or 960 frames per packet), as Apple does. - The magic cookie is an ES_Descriptor with 4-byte descriptor lengths, as Apple writes it. - HE-AAC gets an `ldsc` chunk with the `aach` (and `aacp`) layers when the AudioSpecificConfig signals SBR (and PS) explicitly; Apple decoders need it to apply SBR/PS, and reject it for implicit signaling. - `chan` is only written for mono and stereo, since Apple decoders apply it to the decoded channels. - Priming and remainder: correct for encoder output, MP4/MKV stream copy, and remuxes after a seek; clamped for streams shorter than their priming. - AAC packets are always variable sized, as AVI and WAV give AAC a `block_align`. - AAC without extradata (ADTS) is rejected. **avformat/cafdec: handle a remainder longer than the last packet** Apple's HE-AAC files have a remainder (1370) longer than one packet (1024): the last packet got a negative duration. The remainder is now discarded from every packet it overlaps. A packet table with no valid frames is accepted. **avformat/cafdec: fix HE-AAC timing** The time base followed the decoded (SBR) rate while timestamps counted core frames, so HE-AAC files lasted half their length, and priming/remainder were applied in core frames. The `desc` rate is now the time base and priming/remainder are scaled to decoded samples. Testing: - LC, HE-AAC and HE-AACv2 M4A files (made with afconvert) remuxed to CAF: afinfo reports the same valid/priming/remainder frames as the source, and afconvert and FFmpeg decode the same audio as from the M4A. - Apple-made HE-AAC/HE-AACv2 CAF files now decode in FFmpeg to the same audio as the same streams in M4A. - `-c:a aac` output in CAF decodes sample-exact in afconvert and FFmpeg. - New FATE tests: caf-aac-remux, caf-aac-seek-remux, caf-aac-implicit-ps-remux, caf-aac-priming-only, caf-aac-he-remux. fate-caf, fate-preroll, fate-mov and fate-source pass on every commit. This touches the same lines of `caf_write_init()` as the Opus series in FFmpeg/FFmpeg#25966; whichever lands second needs a small rebase. 🤖 Generated with [Claude Code](https://claude.com/claude-code) From 52d1be3dcdeacc80584f6121c315eab98ef6c0a3 Mon Sep 17 00:00:00 2001 From: Baptiste Coudurier <[email protected]> Date: Thu, 12 Nov 2026 11:32:45 -0800 Subject: [PATCH 1/4] avformat/isom: share the MPEG-4 descriptor writer Move put_descr() from movenc to isom as ff_mp4_write_descr(), next to the descriptor readers, so that other muxers can write ES_Descriptors. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Baptiste Coudurier <[email protected]> --- libavformat/isom.c | 8 ++++++++ libavformat/isom.h | 1 + libavformat/movenc.c | 19 +++++-------------- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/libavformat/isom.c b/libavformat/isom.c index 09be63aa193..be0c1c755d3 100644 --- a/libavformat/isom.c +++ b/libavformat/isom.c @@ -282,6 +282,14 @@ int ff_mov_lang_to_iso639(unsigned code, char to[4]) return 1; } +void ff_mp4_write_descr(AVIOContext *pb, int tag, unsigned size) +{ + avio_w8(pb, tag); + for (int i = 3; i > 0; i--) + avio_w8(pb, (size >> (7 * i)) | 0x80); + avio_w8(pb, size & 0x7F); +} + int ff_mp4_read_descr_len(AVIOContext *pb) { int len = 0; diff --git a/libavformat/isom.h b/libavformat/isom.h index 6230218e37f..8d6b7dbe24f 100644 --- a/libavformat/isom.h +++ b/libavformat/isom.h @@ -403,6 +403,7 @@ typedef struct MOVContext { unsigned heif_icc_profile_items; } MOVContext; +void ff_mp4_write_descr(AVIOContext *pb, int tag, unsigned size); int ff_mp4_read_descr_len(AVIOContext *pb); int ff_mp4_read_descr(void *logctx, AVIOContext *pb, int *tag); int ff_mp4_read_dec_config_descr(void *logctx, AVStream *st, AVIOContext *pb); diff --git a/libavformat/movenc.c b/libavformat/movenc.c index f36072470f3..12751fbe429 100644 --- a/libavformat/movenc.c +++ b/libavformat/movenc.c @@ -724,15 +724,6 @@ static int mov_write_enda_tag_be(AVIOContext *pb) return 10; } -static void put_descr(AVIOContext *pb, int tag, unsigned int size) -{ - int i = 3; - avio_w8(pb, tag); - for (; i > 0; i--) - avio_w8(pb, (size >> (7 * i)) | 0x80); - avio_w8(pb, size & 0x7F); -} - static unsigned compute_avg_bitrate(MOVTrack *track) { uint64_t size = 0; @@ -807,12 +798,12 @@ static int mov_write_esds_tag(AVIOContext *pb, MOVTrack *track) // Basic avio_wb32(pb, 0); // Version // ES descriptor - put_descr(pb, 0x03, 3 + 5+13 + decoder_specific_info_len + 5+1); + ff_mp4_write_descr(pb, 0x03, 3 + 5+13 + decoder_specific_info_len + 5+1); avio_wb16(pb, track->track_id); avio_w8(pb, 0x00); // flags (= no flags) // DecoderConfig descriptor - put_descr(pb, 0x04, 13 + decoder_specific_info_len); + ff_mp4_write_descr(pb, 0x04, 13 + decoder_specific_info_len); // Object type indication if ((track->par->codec_id == AV_CODEC_ID_MP2 || @@ -837,13 +828,13 @@ static int mov_write_esds_tag(AVIOContext *pb, MOVTrack *track) // Basic if (track->extradata_size[track->last_stsd_index]) { // DecoderSpecific info descriptor - put_descr(pb, 0x05, track->extradata_size[track->last_stsd_index]); + ff_mp4_write_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]); } // SL descriptor - put_descr(pb, 0x06, 1); + ff_mp4_write_descr(pb, 0x06, 1); avio_w8(pb, 0x02); return update_size(pb, pos); } @@ -4597,7 +4588,7 @@ static int mov_write_iods_tag(AVIOContext *pb, MOVMuxContext *mov) avio_wb32(pb, 0x0); /* size */ ffio_wfourcc(pb, "iods"); avio_wb32(pb, 0); /* version & flags */ - put_descr(pb, 0x10, 7); + ff_mp4_write_descr(pb, 0x10, 7); avio_wb16(pb, 0x004f); avio_w8(pb, 0xff); avio_w8(pb, 0xff); -- 2.54.0 From d46a87f38b6b438e2531ea29b656af1cb722bf04 Mon Sep 17 00:00:00 2001 From: Baptiste Coudurier <[email protected]> Date: Thu, 12 Nov 2026 10:54:34 -0800 Subject: [PATCH 2/4] avformat/cafenc: support AAC Write AAC the way Apple's tools do: the 'desc' chunk describes the core layer (core sample rate and frames per packet), the magic cookie is an ES_Descriptor with 4-byte descriptor lengths, and HE-AAC gets an 'ldsc' chunk listing the 'aach' (and, when signaled, 'aacp') layers, which Apple decoders need to apply SBR and PS. AAC packets are always written as variable sized, as AVI and WAV give AAC a block_align. The 'ldsc' chunk is only written when the AudioSpecificConfig signals SBR explicitly: Apple decoders fail on layers they cannot find there, and play implicit SBR and PS streams as their core layer, as they do in MP4. The 'chan' chunk is only written for mono and stereo: Apple decoders apply it to the decoded audio, while the bitstream defines the order of more channels. Packet durations are rescaled from the stream time base, and the trailing discard padding of a packet now counts against its full duration, so that both encoder output and stream copy from MP4 get the right mRemainderFrames. mPrimingFrames comes from the first packet: its skip samples, or else the part of the initial padding still before timestamp zero, which is only the retained preroll after a seek. Remuxing LC, HE-AAC and HE-AACv2 MP4 files gives files that afinfo and afconvert read with the same valid, priming and remainder frames and the same decoded audio as the MP4 source. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Baptiste Coudurier <[email protected]> --- Changelog | 1 + configure | 2 +- libavformat/cafenc.c | 262 ++++++++++++++++++++--- tests/fate/caf.mak | 9 + tests/ref/fate/caf-aac-implicit-ps-remux | 15 ++ tests/ref/fate/caf-aac-remux | 117 ++++++++++ tests/ref/fate/caf-aac-seek-remux | 13 ++ 7 files changed, 394 insertions(+), 25 deletions(-) create mode 100644 tests/ref/fate/caf-aac-implicit-ps-remux create mode 100644 tests/ref/fate/caf-aac-remux create mode 100644 tests/ref/fate/caf-aac-seek-remux diff --git a/Changelog b/Changelog index bce79b181f7..51b7c200dfa 100644 --- a/Changelog +++ b/Changelog @@ -33,6 +33,7 @@ version 9.1: - side_data bitstream filter - JPEG Multi-Picture Format demuxer - No longer disabling GCC autovectorization on PowerPC, RISC-V, MIPS and LoongArch +- AAC muxing in CAF version 9.0: diff --git a/configure b/configure index 69793448886..8dfad18e92f 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="iso_media mpeg4audio" 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 b7e7d1c56fd..903bff34779 100644 --- a/libavformat/cafenc.c +++ b/libavformat/cafenc.c @@ -24,11 +24,14 @@ #include "avformat.h" #include "caf.h" #include "isom.h" +#include "mov_chan.h" #include "avio_internal.h" #include "mux.h" #include "libavutil/intfloat.h" +#include "libavutil/intreadwrite.h" #include "libavutil/dict.h" #include "libavutil/mem.h" +#include "libavcodec/mpeg4audio.h" #define FRAME_SIZE_OFFSET 40 @@ -36,6 +39,14 @@ typedef struct { int64_t data; int64_t total_duration; uint32_t frame_size; + int block_align; ///< bytes per packet, 0 if variable + + /* AAC */ + int sample_rate; ///< core sample rate, without SBR + int sbr_sample_rate; ///< 0 if SBR is not known to be present + int core_channels; ///< channels without PS + int ps; + unsigned priming_frames; ///< core frames before the first sample uint8_t *pakt_buf; unsigned pakt_buf_size; @@ -114,13 +125,160 @@ static uint32_t samples_per_packet(const AVCodecParameters *par) { } } +/* CAFAudioFormat, as in the 'desc' chunk and the 'ldsc' items */ +static void write_audio_description(AVIOContext *pb, int sample_rate, uint32_t format_id, + uint32_t flags, int bytes_per_packet, + int frames_per_packet, int channels, + int bits_per_channel) +{ + avio_wb64(pb, av_double2int(sample_rate)); //< mSampleRate + avio_wl32(pb, format_id); //< mFormatID + avio_wb32(pb, flags); //< mFormatFlags + avio_wb32(pb, bytes_per_packet); //< mBytesPerPacket + avio_wb32(pb, frames_per_packet); //< mFramesPerPacket + avio_wb32(pb, channels); //< mChannelsPerFrame + avio_wb32(pb, bits_per_channel); //< mBitsPerChannel +} + +static int aac_init(AVFormatContext *s) +{ + CAFContext *caf = s->priv_data; + AVCodecParameters *par = s->streams[0]->codecpar; + MPEG4AudioConfig cfg; + int ret; + + if (!par->extradata_size) { + av_log(s, AV_LOG_ERROR, "AAC without extradata (e.g. ADTS) is not supported\n"); + return AVERROR_PATCHWELCOME; + } + ret = avpriv_mpeg4audio_get_config2(&cfg, par->extradata, + par->extradata_size, 1, s); + if (ret < 0) + return ret; + + switch (cfg.object_type) { + case AOT_AAC_MAIN: + case AOT_AAC_LC: + case AOT_AAC_SSR: + case AOT_AAC_LTP: + break; + default: + av_log(s, AV_LOG_ERROR, "AAC object type %d is not supported\n", + cfg.object_type); + return AVERROR_PATCHWELCOME; + } + + /* The CAF description and packet table use core samples. */ + caf->sample_rate = cfg.sample_rate; + caf->frame_size = cfg.frame_length; + if (par->sample_rate != cfg.sample_rate && par->sample_rate != 2 * cfg.sample_rate) { + av_log(s, AV_LOG_ERROR, "Sample rate %d does not match the " + "AudioSpecificConfig (%d)\n", par->sample_rate, cfg.sample_rate); + return AVERROR(EINVAL); + } + + /* Without PS, and unless a program_config_element defines them, core + * channels are the decoded ones. */ + caf->core_channels = cfg.chan_config ? cfg.channels : par->ch_layout.nb_channels; + /* Apple decoders fail on 'ldsc' layers for SBR or PS that the + * AudioSpecificConfig only signals implicitly: describe those streams + * as their core layer, as Apple decoders play them. */ + caf->ps = cfg.ps == 1; + if (cfg.sbr == 1 && cfg.ext_sample_rate > 0) + caf->sbr_sample_rate = cfg.ext_sample_rate; + + return 0; +} + +/* The bitstream defines the order of more than two channels. */ +static uint32_t aac_layout_tag(int channels) +{ + return channels == 1 ? MOV_CH_LAYOUT_MONO : + channels == 2 ? MOV_CH_LAYOUT_STEREO : + MOV_CH_LAYOUT_UNKNOWN | channels; +} + +static void aac_write_layer(AVIOContext *pb, int sample_rate, uint32_t format_id, + int frames_per_packet, int channels) +{ + write_audio_description(pb, sample_rate, format_id, 0, 0, + frames_per_packet, channels, 0); + avio_wb32(pb, aac_layout_tag(channels)); //< mChannelLayoutTag +} + +/* Layered descriptions of HE-AAC, from the highest layer down to the core + * layer, which is also the one in the 'desc' chunk. Without them, Apple + * decoders ignore SBR and PS. */ +static void aac_write_ldsc(AVFormatContext *s) +{ + CAFContext *caf = s->priv_data; + AVIOContext *pb = s->pb; + int frames = av_rescale(caf->frame_size, caf->sbr_sample_rate, caf->sample_rate); + + ffio_wfourcc(pb, "ldsc"); + avio_wb64(pb, 36 * (2 + caf->ps)); + if (caf->ps) + aac_write_layer(pb, caf->sbr_sample_rate, MKTAG('a','a','c','p'), + frames, 2); + aac_write_layer(pb, caf->sbr_sample_rate, MKTAG('a','a','c','h'), + frames, caf->core_channels); + aac_write_layer(pb, caf->sample_rate, MKTAG('a','a','c',' '), + caf->frame_size, caf->core_channels); +} + +/* The magic cookie is an ES_Descriptor, as written by Apple: without the esds + * version and flags, and with 4-byte descriptor lengths, which lets readers + * that skip 4 bytes of version and flags first still find the descriptors. */ +static void aac_write_kuki(AVFormatContext *s) +{ + AVIOContext *pb = s->pb; + AVCodecParameters *par = s->streams[0]->codecpar; + const AVPacketSideData *sd = av_packet_side_data_get(par->coded_side_data, + par->nb_coded_side_data, + AV_PKT_DATA_CPB_PROPERTIES); + const AVCPBProperties *props = sd ? (const AVCPBProperties *)sd->data : NULL; + int64_t max_bitrate = FFMAX(par->bit_rate, props ? props->max_bitrate : 0); + int dsi_len = par->extradata_size; + int config_len = 13 + 5 + dsi_len; + int es_len = 3 + 5 + config_len + 5 + 1; + + ffio_wfourcc(pb, "kuki"); + avio_wb64(pb, 5 + es_len); + + ff_mp4_write_descr(pb, 0x03, es_len); // ES_Descriptor + avio_wb16(pb, 0); // ES_ID + avio_w8(pb, 0); // flags + + ff_mp4_write_descr(pb, 0x04, config_len); // DecoderConfigDescriptor + avio_w8(pb, 0x40); // objectTypeIndication: MPEG-4 audio + avio_w8(pb, 0x15); // streamType: audio + avio_wb24(pb, props ? FFMIN(props->buffer_size / 8, 0xFFFFFF) : 0); // bufferSizeDB + avio_wb32(pb, FFMIN(max_bitrate, UINT32_MAX)); // maxBitrate + avio_wb32(pb, FFMIN(par->bit_rate, UINT32_MAX)); // avgBitrate + + ff_mp4_write_descr(pb, 0x05, dsi_len); // DecoderSpecificInfo + avio_write(pb, par->extradata, dsi_len); + + ff_mp4_write_descr(pb, 0x06, 1); // SLConfigDescriptor + avio_w8(pb, 0x02); // predefined: MP4 +} + static int caf_write_init(struct AVFormatContext *s) { + CAFContext *caf = s->priv_data; AVCodecParameters *par = s->streams[0]->codecpar; unsigned int codec_tag = ff_codec_get_tag(ff_codec_caf_tags, par->codec_id); + int ret; + + caf->sample_rate = par->sample_rate; + /* AAC packets are variable sized, whatever the source container says. */ + caf->block_align = par->codec_id == AV_CODEC_ID_AAC ? 0 : par->block_align; switch (par->codec_id) { case AV_CODEC_ID_AAC: + if ((ret = aac_init(s)) < 0) + return ret; + break; case AV_CODEC_ID_OPUS: av_log(s, AV_LOG_ERROR, "muxing codec currently unsupported\n"); return AVERROR_PATCHWELCOME; @@ -138,7 +296,7 @@ static int caf_write_init(struct AVFormatContext *s) // 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) + if (!caf->block_align || !par->frame_size) return 1; return 0; @@ -152,16 +310,20 @@ 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 sample_rate = par->sample_rate; + int sample_rate = caf->sample_rate; + int channels = par->codec_id == AV_CODEC_ID_AAC ? caf->core_channels + : par->ch_layout.nb_channels; - if (!par->block_align && !(pb->seekable & AVIO_SEEKABLE_NORMAL)) { + if (!caf->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 (par->codec_id != AV_CODEC_ID_AAC) { + 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"); @@ -177,15 +339,20 @@ static int caf_write_header(AVFormatContext *s) ffio_wfourcc(pb, "desc"); //< Audio Description chunk avio_wb64(pb, 32); //< mChunkSize - avio_wb64(pb, av_double2int(sample_rate)); //< mSampleRate - avio_wl32(pb, codec_tag); //< mFormatID - avio_wb32(pb, codec_flags(par->codec_id)); //< mFormatFlags - avio_wb32(pb, par->block_align); //< mBytesPerPacket - avio_wb32(pb, caf->frame_size); //< mFramesPerPacket - avio_wb32(pb, par->ch_layout.nb_channels); //< mChannelsPerFrame - avio_wb32(pb, av_get_bits_per_sample(par->codec_id)); //< mBitsPerChannel + write_audio_description(pb, sample_rate, codec_tag, codec_flags(par->codec_id), + caf->block_align, caf->frame_size, channels, + av_get_bits_per_sample(par->codec_id)); - if (par->ch_layout.order == AV_CHANNEL_ORDER_NATIVE) { + if (par->codec_id == AV_CODEC_ID_AAC) { + /* Apple decoders apply the 'chan' layout to the decoded channels. */ + if (channels <= 2 && channels == par->ch_layout.nb_channels) { + ffio_wfourcc(pb, "chan"); + avio_wb64(pb, 12); + avio_wb32(pb, aac_layout_tag(channels)); //< mChannelLayoutTag + avio_wb32(pb, 0); //< mChannelBitmap + avio_wb32(pb, 0); //< mNumberChannelDescriptions + } + } else 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); @@ -211,6 +378,10 @@ 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_AAC) { + aac_write_kuki(s); + if (caf->sbr_sample_rate) + aac_write_ldsc(s); } ff_standardize_creation_time(s); @@ -251,8 +422,11 @@ static int caf_write_packet(AVFormatContext *s, AVPacket *pkt) { CAFContext *caf = s->priv_data; AVStream *const st = s->streams[0]; + int64_t duration = pkt->duration; + const uint8_t *skip; + size_t skip_size; - if (!st->codecpar->block_align || !caf->frame_size) { + if (!caf->block_align || !caf->frame_size) { #if SIZE_MAX - 10 < UINT_MAX if (caf->pakt_buf_size > SIZE_MAX - 10) return AVERROR(ERANGE); @@ -264,13 +438,45 @@ static int caf_write_packet(AVFormatContext *s, AVPacket *pkt) caf->pakt_buf = pakt_buf; pakt_buf += caf->pakt_buf_size; - if (!st->codecpar->block_align) + if (!caf->block_align) pakt_buf = put_variable_length_num(pakt_buf, pkt->size); if (!caf->frame_size) pakt_buf = put_variable_length_num(pakt_buf, pkt->duration); caf->pakt_buf_size = pakt_buf - caf->pakt_buf; } - caf->total_duration += pkt->duration; + + skip = av_packet_get_side_data(pkt, AV_PKT_DATA_SKIP_SAMPLES, &skip_size); + if (!st->nb_frames && st->codecpar->codec_id == AV_CODEC_ID_AAC) { + unsigned priming = st->codecpar->initial_padding; + + if (skip && skip_size >= 10 && AV_RL32(skip)) + priming = AV_RL32(skip); + else if (priming && pkt->pts != AV_NOPTS_VALUE) { + /* After a seek only the retained preroll precedes timestamp zero. */ + AVRational sample_tb = { 1, st->codecpar->sample_rate }; + int64_t preroll, offset; + + if (ff_get_muxer_ts_offset(s, 0, &offset) < 0) + offset = 0; + preroll = av_sat_sub64(offset, pkt->pts); + if (av_compare_ts(preroll, st->time_base, priming, sample_tb) < 0) + priming = av_rescale_q(FFMAX(preroll, 0), st->time_base, sample_tb); + } + caf->priming_frames = av_rescale(priming, caf->sample_rate, + st->codecpar->sample_rate); + } + + /* Encoders shorten the duration of the last packet, demuxers may not: + * count the discarded samples from the full packet duration. */ + if (skip && skip_size >= 10 && caf->frame_size) { + duration = FFMAX(duration, av_rescale_q(caf->frame_size, + (AVRational){ 1, caf->sample_rate }, + st->time_base)); + duration -= av_rescale_q(AV_RL32(skip + 4), + (AVRational){ 1, st->codecpar->sample_rate }, + st->time_base); + } + caf->total_duration += duration; avio_write(s->pb, pkt->data, pkt->size); return 0; @@ -285,16 +491,24 @@ static int caf_write_trailer(AVFormatContext *s) if (pb->seekable & AVIO_SEEKABLE_NORMAL) { int64_t file_size = avio_tell(pb); - int64_t packets = (!par->block_align || !caf->frame_size) ? st->nb_frames : 0; - int64_t valid_frames = caf->frame_size ? st->nb_frames * caf->frame_size : caf->total_duration; - unsigned remainder_frames = valid_frames > caf->total_duration - ? valid_frames - caf->total_duration : 0; + int64_t packets = (!caf->block_align || !caf->frame_size) ? st->nb_frames : 0; + int64_t total_duration = av_rescale_q(caf->total_duration, st->time_base, + (AVRational){ 1, caf->sample_rate }); + int64_t valid_frames = caf->frame_size ? st->nb_frames * caf->frame_size : total_duration; + unsigned remainder_frames = valid_frames > total_duration + ? valid_frames - total_duration : 0; + unsigned priming_frames = par->codec_id == AV_CODEC_ID_AAC ? caf->priming_frames + : par->initial_padding; avio_seek(pb, caf->data, SEEK_SET); avio_wb64(pb, file_size - caf->data - 8); - if (!par->block_align || !caf->frame_size || par->initial_padding || remainder_frames) { - valid_frames -= par->initial_padding; + /* A short stream may end during priming. */ + priming_frames = FFMIN(priming_frames, valid_frames); + remainder_frames = FFMIN(remainder_frames, valid_frames - priming_frames); + + if (!caf->block_align || !caf->frame_size || priming_frames || remainder_frames) { + valid_frames -= priming_frames; valid_frames -= remainder_frames; avio_seek(pb, file_size, SEEK_SET); @@ -302,7 +516,7 @@ static int caf_write_trailer(AVFormatContext *s) avio_wb64(pb, 24ULL + caf->pakt_buf_size); // size avio_wb64(pb, packets); ///< mNumberPackets avio_wb64(pb, valid_frames); ///< mNumberValidFrames - avio_wb32(pb, par->initial_padding); ///< mPrimingFrames + avio_wb32(pb, priming_frames); ///< mPrimingFrames avio_wb32(pb, remainder_frames); ///< mRemainderFrames avio_write(pb, caf->pakt_buf, caf->pakt_buf_size); } diff --git a/tests/fate/caf.mak b/tests/fate/caf.mak index 4ee9c95a5c8..e9b485505ee 100644 --- a/tests/fate/caf.mak +++ b/tests/fate/caf.mak @@ -4,6 +4,15 @@ fate-caf-demux: CMD = crc -i $(TARGET_SAMPLES)/caf/caf-pcm16.caf -c copy 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" +FATE_CAF_FFMPEG_FFPROBE-$(call REMUX, CAF, MOV_DEMUXER) += fate-caf-aac-remux +fate-caf-aac-remux: CMD = transcode mov $(TARGET_SAMPLES)/gapless/102400samples_qt-lc-aac.m4a caf "-c copy" "-c copy" "-show_entries stream=sample_rate,channels,initial_padding,duration_ts" + +FATE_CAF_FFMPEG_FFPROBE-$(call REMUX, CAF, MOV_DEMUXER) += fate-caf-aac-seek-remux +fate-caf-aac-seek-remux: CMD = transcode mov $(TARGET_SAMPLES)/gapless/102400samples_qt-lc-aac.m4a caf "-c copy" "-c copy -frames:a 1" "-show_entries stream=initial_padding,duration_ts" "" "" "-ss 1" + +FATE_CAF_FFMPEG_FFPROBE-$(call REMUX, CAF, MOV_DEMUXER AAC_DECODER) += fate-caf-aac-implicit-ps-remux +fate-caf-aac-implicit-ps-remux: CMD = transcode mov $(TARGET_SAMPLES)/aac/CT_DecoderCheck/sbr_i-ps_i.mp4 caf "-map 0:a -c copy -frames:a 3" "-c copy" "-show_entries stream=sample_rate,channels" + FATE_CAF_FFMPEG-$(call REMUX, CAF, AMR_DEMUXER) += fate-caf-amr_nb-remux fate-caf-amr_nb-remux: CMD = transcode amr $(TARGET_SAMPLES)/amrnb/4.75k.amr caf "-c copy" "-c copy -t 0.2" diff --git a/tests/ref/fate/caf-aac-implicit-ps-remux b/tests/ref/fate/caf-aac-implicit-ps-remux new file mode 100644 index 00000000000..af731f9b612 --- /dev/null +++ b/tests/ref/fate/caf-aac-implicit-ps-remux @@ -0,0 +1,15 @@ +cb468143a0f79c0e1edb01bffd47e3a7 *tests/data/fate/caf-aac-implicit-ps-remux.caf +819 tests/data/fate/caf-aac-implicit-ps-remux.caf +#extradata 0: 2, 0x00ae009b +#tb 0: 1/22050 +#media_type 0: audio +#codec_id 0: aac +#sample_rate 0: 44100 +#channel_layout_name 0: stereo +0, 0, 0, 1024, 186, 0x53860507 +0, 1024, 1024, 1024, 186, 0x183d0328 +0, 2048, 2048, 1024, 186, 0x432004ee +[STREAM] +sample_rate=44100 +channels=2 +[/STREAM] diff --git a/tests/ref/fate/caf-aac-remux b/tests/ref/fate/caf-aac-remux new file mode 100644 index 00000000000..aec59ce1e81 --- /dev/null +++ b/tests/ref/fate/caf-aac-remux @@ -0,0 +1,117 @@ +2aa5c96470deddb5a5579f17ef4ffff5 *tests/data/fate/caf-aac-remux.caf +6092 tests/data/fate/caf-aac-remux.caf +#extradata 0: 2, 0x00340022 +#tb 0: 1/44100 +#media_type 0: audio +#codec_id 0: aac +#sample_rate 0: 44100 +#channel_layout_name 0: stereo +0, -2112, -2112, 1024, 6, 0x031e0108, S=1, Skip Samples, 10, 0x02c80048 +0, -1088, -1088, 1024, 251, 0xc1e981d1 +0, -64, -64, 1024, 168, 0x801a4dd7 +0, 960, 960, 1024, 89, 0x73cb2580 +0, 1984, 1984, 1024, 27, 0x69ac08a1 +0, 3008, 3008, 1024, 27, 0x642c08bb +0, 4032, 4032, 1024, 27, 0x6bbb090b +0, 5056, 5056, 1024, 26, 0x625a08e2 +0, 6080, 6080, 1024, 27, 0x71ad0994 +0, 7104, 7104, 1024, 27, 0x69ac08a1 +0, 8128, 8128, 1024, 27, 0x642c08bb +0, 9152, 9152, 1024, 27, 0x6bbb090b +0, 10176, 10176, 1024, 26, 0x625a08e2 +0, 11200, 11200, 1024, 27, 0x71ad0994 +0, 12224, 12224, 1024, 27, 0x69ac08a1 +0, 13248, 13248, 1024, 27, 0x642c08bb +0, 14272, 14272, 1024, 27, 0x6bbb090b +0, 15296, 15296, 1024, 26, 0x625a08e2 +0, 16320, 16320, 1024, 27, 0x71ad0994 +0, 17344, 17344, 1024, 27, 0x69ac08a1 +0, 18368, 18368, 1024, 27, 0x642c08bb +0, 19392, 19392, 1024, 27, 0x6bbb090b +0, 20416, 20416, 1024, 26, 0x625a08e2 +0, 21440, 21440, 1024, 27, 0x71ad0994 +0, 22464, 22464, 1024, 27, 0x69ac08a1 +0, 23488, 23488, 1024, 28, 0x622e070c +0, 24512, 24512, 1024, 27, 0x6bbb090b +0, 25536, 25536, 1024, 27, 0x72f709ca +0, 26560, 26560, 1024, 28, 0x7b0209a5 +0, 27584, 27584, 1024, 28, 0x7ca30a99 +0, 28608, 28608, 1024, 28, 0x622e070c +0, 29632, 29632, 1024, 27, 0x6bbb090b +0, 30656, 30656, 1024, 27, 0x72f709ca +0, 31680, 31680, 1024, 28, 0x7b0209a5 +0, 32704, 32704, 1024, 28, 0x7ca30a99 +0, 33728, 33728, 1024, 28, 0x622e070c +0, 34752, 34752, 1024, 27, 0x6bbb090b +0, 35776, 35776, 1024, 27, 0x5b38070c +0, 36800, 36800, 1024, 28, 0x7be209ca +0, 37824, 37824, 1024, 28, 0x7f1f0af4 +0, 38848, 38848, 1024, 28, 0x6c6a085c +0, 39872, 39872, 1024, 28, 0x7cf40a3b +0, 40896, 40896, 1024, 27, 0x5b38070c +0, 41920, 41920, 1024, 28, 0x7be209ca +0, 42944, 42944, 1024, 28, 0x7f1f0af4 +0, 43968, 43968, 1024, 28, 0x6c6a085c +0, 44992, 44992, 1024, 28, 0x7cf40a3b +0, 46016, 46016, 1024, 27, 0x5b38070c +0, 47040, 47040, 1024, 28, 0x7be209ca +0, 48064, 48064, 1024, 30, 0x929c0b49 +0, 49088, 49088, 1024, 30, 0x861a09de +0, 50112, 50112, 1024, 30, 0x803c08dd +0, 51136, 51136, 1024, 29, 0x7b1509bc +0, 52160, 52160, 1024, 30, 0x965f0ad1 +0, 53184, 53184, 1024, 30, 0x929c0b49 +0, 54208, 54208, 1024, 30, 0x861a09de +0, 55232, 55232, 1024, 30, 0x803c08dd +0, 56256, 56256, 1024, 29, 0x7b1509bc +0, 57280, 57280, 1024, 30, 0x965f0ad1 +0, 58304, 58304, 1024, 30, 0x929c0b49 +0, 59328, 59328, 1024, 30, 0x861a09de +0, 60352, 60352, 1024, 30, 0x803c08dd +0, 61376, 61376, 1024, 39, 0x47cc1199 +0, 62400, 62400, 1024, 42, 0x8e4813fd +0, 63424, 63424, 1024, 41, 0x5f9d11fa +0, 64448, 64448, 1024, 41, 0x499111b0 +0, 65472, 65472, 1024, 41, 0x579410fc +0, 66496, 66496, 1024, 39, 0x47cc1199 +0, 67520, 67520, 1024, 42, 0x8e4813fd +0, 68544, 68544, 1024, 41, 0x5f9d11fa +0, 69568, 69568, 1024, 41, 0x499111b0 +0, 70592, 70592, 1024, 41, 0x579410fc +0, 71616, 71616, 1024, 39, 0x47cc1199 +0, 72640, 72640, 1024, 42, 0x8e4813fd +0, 73664, 73664, 1024, 41, 0x5f9d11fa +0, 74688, 74688, 1024, 41, 0x499111b0 +0, 75712, 75712, 1024, 41, 0x579410fc +0, 76736, 76736, 1024, 39, 0x47cc1199 +0, 77760, 77760, 1024, 42, 0x8e4813fd +0, 78784, 78784, 1024, 41, 0x5f9d11fa +0, 79808, 79808, 1024, 41, 0x499111b0 +0, 80832, 80832, 1024, 41, 0x579410fc +0, 81856, 81856, 1024, 39, 0x47cc1199 +0, 82880, 82880, 1024, 42, 0x8e4813fd +0, 83904, 83904, 1024, 41, 0x5f9d11fa +0, 84928, 84928, 1024, 41, 0x499111b0 +0, 85952, 85952, 1024, 41, 0x579410fc +0, 86976, 86976, 1024, 39, 0x47cc1199 +0, 88000, 88000, 1024, 42, 0x8e4813fd +0, 89024, 89024, 1024, 41, 0x5f9d11fa +0, 90048, 90048, 1024, 41, 0x499111b0 +0, 91072, 91072, 1024, 41, 0x579410fc +0, 92096, 92096, 1024, 39, 0x47cc1199 +0, 93120, 93120, 1024, 42, 0x8e4813fd +0, 94144, 94144, 1024, 41, 0x5f9d11fa +0, 95168, 95168, 1024, 41, 0x499111b0 +0, 96192, 96192, 1024, 41, 0x579410fc +0, 97216, 97216, 1024, 39, 0x47cc1199 +0, 98240, 98240, 1024, 42, 0x8e4813fd +0, 99264, 99264, 1024, 41, 0x5f9d11fa +0, 100288, 100288, 1024, 41, 0x499111b0 +0, 101312, 101312, 1024, 1371, 0x0623964c +0, 102336, 102336, 64, 573, 0xd09f2055, S=1, Skip Samples, 10, 0x048f00c3 +[STREAM] +sample_rate=44100 +channels=2 +initial_padding=2112 +duration_ts=102400 +[/STREAM] diff --git a/tests/ref/fate/caf-aac-seek-remux b/tests/ref/fate/caf-aac-seek-remux new file mode 100644 index 00000000000..18f9bfb42f0 --- /dev/null +++ b/tests/ref/fate/caf-aac-seek-remux @@ -0,0 +1,13 @@ +0b1d75a934863f72848b1a5da3ee2320 *tests/data/fate/caf-aac-seek-remux.caf +4415 tests/data/fate/caf-aac-seek-remux.caf +#extradata 0: 2, 0x00340022 +#tb 0: 1/44100 +#media_type 0: audio +#codec_id 0: aac +#sample_rate 0: 44100 +#channel_layout_name 0: stereo +0, -132, -132, 1024, 28, 0x6c6a085c, S=1, Skip Samples, 10, 0x05280084 +[STREAM] +initial_padding=132 +duration_ts=58300 +[/STREAM] -- 2.54.0 From ff1ef3ed8111805b6a92d01743963b94d975feac Mon Sep 17 00:00:00 2001 From: Baptiste Coudurier <[email protected]> Date: Thu, 12 Nov 2026 10:57:58 -0800 Subject: [PATCH 3/4] avformat/cafdec: handle a remainder longer than the last packet mRemainderFrames can exceed the frames of the last packet: Apple's HE-AAC files have 1370 remainder frames for 1024 frames per packet. The last packet then got a negative duration and the decoder kept the padding. Discard the remainder from every packet it overlaps instead. Also accept a packet table with no valid frames, as for a stream that ends during priming, instead of reporting it as missing. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Baptiste Coudurier <[email protected]> --- libavformat/cafdec.c | 24 ++++++++++++------------ tests/fate/caf.mak | 3 +++ tests/ref/fate/caf-aac-priming-only | 13 +++++++++++++ 3 files changed, 28 insertions(+), 12 deletions(-) create mode 100644 tests/ref/fate/caf-aac-priming-only diff --git a/libavformat/cafdec.c b/libavformat/cafdec.c index fae4d7db5ec..4d354f11878 100644 --- a/libavformat/cafdec.c +++ b/libavformat/cafdec.c @@ -445,7 +445,11 @@ found_data: if (caf->bytes_per_packet > 0 && caf->frames_per_packet > 0) { if (caf->data_size > 0 && caf->data_size / caf->bytes_per_packet < INT64_MAX / caf->frames_per_packet) st->nb_frames = (caf->data_size / caf->bytes_per_packet) * caf->frames_per_packet; - } else if (ffstream(st)->nb_index_entries && st->duration > 0) { + } else if (!ffstream(st)->nb_index_entries) { + av_log(s, AV_LOG_ERROR, "Missing packet table. It is required when " + "block size or frame size are variable.\n"); + return AVERROR_INVALIDDATA; + } else if (st->duration > 0) { if (st->codecpar->sample_rate && caf->data_size / st->duration > INT64_MAX / st->codecpar->sample_rate / 8) { av_log(s, AV_LOG_ERROR, "Overflow during bit rate calculation %d * 8 * %"PRId64"\n", st->codecpar->sample_rate, caf->data_size / st->duration); @@ -453,10 +457,6 @@ found_data: } st->codecpar->bit_rate = st->codecpar->sample_rate * 8LL * (caf->data_size / st->duration); - } else { - av_log(s, AV_LOG_ERROR, "Missing packet table. It is required when " - "block size or frame size are variable.\n"); - return AVERROR_INVALIDDATA; } avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate); @@ -509,19 +509,19 @@ static int read_packet(AVFormatContext *s, AVPacket *pkt) pkt_frames = sti->index_entries[caf->packet_cnt + 1].timestamp - sti->index_entries[caf->packet_cnt].timestamp; } else if (caf->packet_cnt == sti->nb_index_entries - 1) { pkt_size = caf->num_bytes - sti->index_entries[caf->packet_cnt].pos; - pkt_frames = st->duration - sti->index_entries[caf->packet_cnt].timestamp; - remainder = caf->remainder; + pkt_frames = st->duration + caf->remainder - sti->index_entries[caf->packet_cnt].timestamp; } else { return AVERROR_INVALIDDATA; } - } else if (caf->packet_cnt + 1 == caf->num_packets) { - pkt_frames -= caf->remainder; - remainder = caf->remainder; } - if (pkt_size == 0 || pkt_frames == 0 || pkt_size > left) + if (pkt_size <= 0 || pkt_frames <= 0 || pkt_size > left) return AVERROR_INVALIDDATA; + /* The remainder can span more than the last packet. */ + if (caf->remainder && caf->frame_cnt + pkt_frames > st->duration) + remainder = FFMIN(caf->frame_cnt + pkt_frames - st->duration, pkt_frames); + res = av_get_packet(pb, pkt, pkt_size); if (res < 0) return res; @@ -540,7 +540,7 @@ static int read_packet(AVFormatContext *s, AVPacket *pkt) AV_WL32A(side_data + 4, remainder); } - pkt->duration = pkt_frames; + pkt->duration = pkt_frames - remainder; pkt->size = res; pkt->stream_index = 0; pkt->dts = pkt->pts = caf->frame_cnt; diff --git a/tests/fate/caf.mak b/tests/fate/caf.mak index e9b485505ee..bb78c95efc8 100644 --- a/tests/fate/caf.mak +++ b/tests/fate/caf.mak @@ -7,6 +7,9 @@ fate-caf-alac-remux: CMD = transcode m4a $(TARGET_SAMPLES)/lossless-audio/inside FATE_CAF_FFMPEG_FFPROBE-$(call REMUX, CAF, MOV_DEMUXER) += fate-caf-aac-remux fate-caf-aac-remux: CMD = transcode mov $(TARGET_SAMPLES)/gapless/102400samples_qt-lc-aac.m4a caf "-c copy" "-c copy" "-show_entries stream=sample_rate,channels,initial_padding,duration_ts" +FATE_CAF_FFMPEG_FFPROBE-$(call REMUX, CAF, MOV_DEMUXER) += fate-caf-aac-priming-only +fate-caf-aac-priming-only: CMD = transcode mov $(TARGET_SAMPLES)/gapless/102400samples_qt-lc-aac.m4a caf "-c copy -frames:a 1" "-c copy" "-show_entries stream=initial_padding,duration_ts" + FATE_CAF_FFMPEG_FFPROBE-$(call REMUX, CAF, MOV_DEMUXER) += fate-caf-aac-seek-remux fate-caf-aac-seek-remux: CMD = transcode mov $(TARGET_SAMPLES)/gapless/102400samples_qt-lc-aac.m4a caf "-c copy" "-c copy -frames:a 1" "-show_entries stream=initial_padding,duration_ts" "" "" "-ss 1" diff --git a/tests/ref/fate/caf-aac-priming-only b/tests/ref/fate/caf-aac-priming-only new file mode 100644 index 00000000000..6a4535d18b8 --- /dev/null +++ b/tests/ref/fate/caf-aac-priming-only @@ -0,0 +1,13 @@ +1fc2988b1ea97f781f8262b97450ea14 *tests/data/fate/caf-aac-priming-only.caf +328 tests/data/fate/caf-aac-priming-only.caf +#extradata 0: 2, 0x00340022 +#tb 0: 1/44100 +#media_type 0: audio +#codec_id 0: aac +#sample_rate 0: 44100 +#channel_layout_name 0: stereo +0, -1024, -1024, 1024, 6, 0x031e0108, S=1, Skip Samples, 10, 0x00240004 +[STREAM] +initial_padding=1024 +duration_ts=0 +[/STREAM] -- 2.54.0 From 262d7327be126f13cd90b066fcc7b50dac647d6b Mon Sep 17 00:00:00 2001 From: Baptiste Coudurier <[email protected]> Date: Thu, 12 Nov 2026 10:58:38 -0800 Subject: [PATCH 4/4] avformat/cafdec: fix HE-AAC timing The 'desc' chunk of HE-AAC describes the core layer, at half the decoded sample rate, and the higher layers are listed in the 'ldsc' chunk. The demuxer exported the decoded sample rate from the magic cookie as the time base while counting core frames, so HE-AAC files lasted half their length, and priming and remainder were applied in core frames to decoded samples. Keep the 'desc' sample rate as the time base, and scale the exported priming and remainder to the decoded sample rate, which the magic cookie gives as Apple's HE-AAC files signal SBR explicitly. Apple's HE-AAC and HE-AACv2 CAF files now decode to the same audio as the same streams in MP4. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Baptiste Coudurier <[email protected]> --- libavformat/cafdec.c | 29 +++++++++++++++++++++-------- tests/fate/caf.mak | 3 +++ tests/ref/fate/caf-aac-he-remux | 19 +++++++++++++++++++ 3 files changed, 43 insertions(+), 8 deletions(-) create mode 100644 tests/ref/fate/caf-aac-he-remux diff --git a/libavformat/cafdec.c b/libavformat/cafdec.c index 4d354f11878..8c7bd295d9e 100644 --- a/libavformat/cafdec.c +++ b/libavformat/cafdec.c @@ -54,7 +54,11 @@ typedef struct CafContext { int64_t data_start; ///< data start position, in bytes int64_t data_size; ///< raw data size, in bytes + unsigned priming; ///< frames to discard from the first packet unsigned remainder; ///< frames to discard from the last packet + + int sample_rate; ///< sample rate of the 'desc' chunk, also the time base + int frame_scale; ///< decoded samples per described frame } CafContext; static int probe(const AVProbeData *p) @@ -86,6 +90,7 @@ static int read_desc_chunk(AVFormatContext *s) /* parse format description */ st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; st->codecpar->sample_rate = av_clipd(av_int2double(avio_rb64(pb)), 0, INT_MAX); + caf->sample_rate = st->codecpar->sample_rate; st->codecpar->codec_tag = avio_rl32(pb); flags = avio_rb32(pb); caf->bytes_per_packet = avio_rb32(pb); @@ -281,6 +286,7 @@ static int read_pakt_chunk(AVFormatContext *s, int64_t size) return AVERROR_INVALIDDATA; caf->frame_cnt = -(int64_t)priming; + caf->priming = priming; st->codecpar->initial_padding = priming; st->nb_frames += priming; st->nb_frames += caf->remainder; @@ -442,6 +448,13 @@ static int read_header(AVFormatContext *s) return AVERROR_INVALIDDATA; found_data: + /* For HE-AAC, the 'desc' chunk describes the core layer, at half the + * sample rate the magic cookie signals: timestamps count core frames, + * while priming and remainder are exported in decoded samples. */ + caf->frame_scale = caf->sample_rate > 0 && + st->codecpar->sample_rate == 2 * caf->sample_rate ? 2 : 1; + st->codecpar->initial_padding = caf->priming * caf->frame_scale; + if (caf->bytes_per_packet > 0 && caf->frames_per_packet > 0) { if (caf->data_size > 0 && caf->data_size / caf->bytes_per_packet < INT64_MAX / caf->frames_per_packet) st->nb_frames = (caf->data_size / caf->bytes_per_packet) * caf->frames_per_packet; @@ -450,16 +463,16 @@ found_data: "block size or frame size are variable.\n"); return AVERROR_INVALIDDATA; } else if (st->duration > 0) { - if (st->codecpar->sample_rate && caf->data_size / st->duration > INT64_MAX / st->codecpar->sample_rate / 8) { + if (caf->sample_rate && caf->data_size / st->duration > INT64_MAX / caf->sample_rate / 8) { av_log(s, AV_LOG_ERROR, "Overflow during bit rate calculation %d * 8 * %"PRId64"\n", - st->codecpar->sample_rate, caf->data_size / st->duration); + caf->sample_rate, caf->data_size / st->duration); return AVERROR_INVALIDDATA; } - st->codecpar->bit_rate = st->codecpar->sample_rate * 8LL * + st->codecpar->bit_rate = caf->sample_rate * 8LL * (caf->data_size / st->duration); } - avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate); + avpriv_set_pts_info(st, 64, 1, caf->sample_rate); st->start_time = 0; /* position the stream at the start of data */ @@ -527,7 +540,7 @@ static int read_packet(AVFormatContext *s, AVPacket *pkt) return res; if (!caf->packet_cnt) - priming = st->codecpar->initial_padding; + priming = caf->priming; if (priming > 0 || remainder > 0) { uint8_t* side_data = av_packet_new_side_data(pkt, @@ -536,8 +549,8 @@ static int read_packet(AVFormatContext *s, AVPacket *pkt) if (!side_data) return AVERROR(ENOMEM); - AV_WL32A(side_data, priming); - AV_WL32A(side_data + 4, remainder); + AV_WL32A(side_data, priming * caf->frame_scale); + AV_WL32A(side_data + 4, remainder * caf->frame_scale); } pkt->duration = pkt_frames - remainder; @@ -567,7 +580,7 @@ static int read_seek(AVFormatContext *s, int stream_index, if (caf->data_size > 0) pos = FFMIN(pos, caf->data_size); packet_cnt = pos / caf->bytes_per_packet; - frame_cnt = caf->frames_per_packet * packet_cnt - st->codecpar->initial_padding; + frame_cnt = caf->frames_per_packet * packet_cnt - caf->priming; } else if (sti->nb_index_entries) { packet_cnt = av_index_search_timestamp(st, timestamp, flags); if (packet_cnt < 0) diff --git a/tests/fate/caf.mak b/tests/fate/caf.mak index bb78c95efc8..f1db3aa9b98 100644 --- a/tests/fate/caf.mak +++ b/tests/fate/caf.mak @@ -16,6 +16,9 @@ fate-caf-aac-seek-remux: CMD = transcode mov $(TARGET_SAMPLES)/gapless/102400sam FATE_CAF_FFMPEG_FFPROBE-$(call REMUX, CAF, MOV_DEMUXER AAC_DECODER) += fate-caf-aac-implicit-ps-remux fate-caf-aac-implicit-ps-remux: CMD = transcode mov $(TARGET_SAMPLES)/aac/CT_DecoderCheck/sbr_i-ps_i.mp4 caf "-map 0:a -c copy -frames:a 3" "-c copy" "-show_entries stream=sample_rate,channels" +FATE_CAF_FFMPEG_FFPROBE-$(call REMUX, CAF, MOV_DEMUXER) += fate-caf-aac-he-remux +fate-caf-aac-he-remux: CMD = transcode mov $(TARGET_SAMPLES)/aac/al_sbr_ps_06_new.mp4 caf "-c copy" "-c copy -t 0.2" "-show_entries stream=sample_rate,channels,time_base,initial_padding,duration_ts" + FATE_CAF_FFMPEG-$(call REMUX, CAF, AMR_DEMUXER) += fate-caf-amr_nb-remux fate-caf-amr_nb-remux: CMD = transcode amr $(TARGET_SAMPLES)/amrnb/4.75k.amr caf "-c copy" "-c copy -t 0.2" diff --git a/tests/ref/fate/caf-aac-he-remux b/tests/ref/fate/caf-aac-he-remux new file mode 100644 index 00000000000..d050e7e5fe1 --- /dev/null +++ b/tests/ref/fate/caf-aac-he-remux @@ -0,0 +1,19 @@ +d5672259ae5a858680676ffb54675171 *tests/data/fate/caf-aac-he-remux.caf +54972 tests/data/fate/caf-aac-he-remux.caf +#extradata 0: 11, 0x04a60201 +#tb 0: 1/16000 +#media_type 0: audio +#codec_id 0: aac +#sample_rate 0: 32000 +#channel_layout_name 0: stereo +0, 0, 0, 1024, 257, 0xba1e03f3 +0, 1024, 1024, 1024, 256, 0x5c7d0392 +0, 2048, 2048, 1024, 256, 0xf6de73b5 +0, 3072, 3072, 1024, 256, 0xcd907792 +[STREAM] +sample_rate=32000 +channels=2 +initial_padding=0 +time_base=1/16000 +duration_ts=217088 +[/STREAM] -- 2.54.0 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
