This is an automated email from the git hooks/post-receive script. Git pushed a commit to branch master in repository ffmpeg.
commit bb504183bcbd1756aacb401db89430538c8d9966 Author: Baptiste Coudurier <[email protected]> AuthorDate: Sun Nov 15 23:37:07 2026 -0800 Commit: Baptiste Coudurier <[email protected]> CommitDate: Fri Nov 27 00:34:00 2026 +0000 avformat/caf: add AMR-WB Map sawb, write 320 frames per packet and a kuki, and require one frame per channel. Count sample frames from whole packets and the trim. Co-Authored-By: Claude Opus 5.5 <[email protected]> Assisted-by: Fairy Signed-off-by: Baptiste Coudurier <[email protected]> --- libavformat/caf.c | 1 + libavformat/cafenc.c | 116 +++++++++++++++++++---- tests/fate/caf.mak | 12 +++ tests/ref/fate/caf-amr_wb-rounded-duration-remux | 11 +++ tests/ref/fate/caf-amr_wb-skip-samples-remux | 11 +++ 5 files changed, 135 insertions(+), 16 deletions(-) diff --git a/libavformat/caf.c b/libavformat/caf.c index a61c39fae5..1c8f6cbfd1 100644 --- a/libavformat/caf.c +++ b/libavformat/caf.c @@ -40,6 +40,7 @@ const AVCodecTag ff_codec_caf_tags[] = { { AV_CODEC_ID_ADPCM_MS, MKTAG('m','s', 0, 2 ) }, { AV_CODEC_ID_ALAC, MKTAG('a','l','a','c') }, { AV_CODEC_ID_AMR_NB, MKTAG('s','a','m','r') }, + { AV_CODEC_ID_AMR_WB, MKTAG('s','a','w','b') }, /* FIXME: use DV demuxer, as done in MOV */ /*{ AV_CODEC_ID_DVAUDIO, MKTAG('v','d','v','a') },*/ /*{ AV_CODEC_ID_DVAUDIO, MKTAG('d','v','c','a') },*/ diff --git a/libavformat/cafenc.c b/libavformat/cafenc.c index 0634bdb4c1..ec1155e9ea 100644 --- a/libavformat/cafenc.c +++ b/libavformat/cafenc.c @@ -25,8 +25,10 @@ #include "caf.h" #include "isom.h" #include "avio_internal.h" +#include "internal.h" #include "mux.h" #include "libavutil/intfloat.h" +#include "libavutil/intreadwrite.h" #include "libavutil/dict.h" #include "libavutil/mem.h" #include "libavcodec/opus/tab.h" @@ -37,6 +39,8 @@ typedef struct { int64_t data; int64_t total_duration; uint32_t frame_size; + uint32_t priming; + int64_t end_discard; uint8_t *pakt_buf; unsigned pakt_buf_size; @@ -90,6 +94,7 @@ static uint32_t samples_per_packet(const AVCodecParameters *par) { case AV_CODEC_ID_ILBC: case AV_CODEC_ID_QCELP: return 160; + case AV_CODEC_ID_AMR_WB: case AV_CODEC_ID_GSM_MS: return 320; case AV_CODEC_ID_MP1: @@ -117,6 +122,7 @@ static uint32_t samples_per_packet(const AVCodecParameters *par) { 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); @@ -137,6 +143,11 @@ static int caf_write_init(struct AVFormatContext *s) return AVERROR_INVALIDDATA; } + caf->end_discard = -1; + /* AMR-WB trim and packet duration are both measured in sample frames. */ + if (par->codec_id == AV_CODEC_ID_AMR_WB) + avpriv_set_pts_info(s->streams[0], 64, 1, 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) @@ -197,17 +208,13 @@ static int caf_write_header(AVFormatContext *s) avio_wb64(pb, 12 + par->extradata_size); avio_write(pb, "\0\0\0\14frmaalac", 12); avio_write(pb, par->extradata, par->extradata_size); - } else if (par->codec_id == AV_CODEC_ID_AMR_NB) { + } else if (par->codec_id == AV_CODEC_ID_AMR_NB || + par->codec_id == AV_CODEC_ID_AMR_WB) { ffio_wfourcc(pb, "kuki"); - avio_wb64(pb, 29); - avio_write(pb, "\0\0\0\14frmasamr", 12); - avio_wb32(pb, 0x11); /* size */ - avio_write(pb, "samrFFMP", 8); - avio_w8(pb, 0); /* decoder version */ - - 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 */ + avio_wb64(pb, 12 + FF_AMR_CONFIG_ATOM_SIZE); + avio_write(pb, "\0\0\0\14frma", 8); + avio_wl32(pb, codec_tag); + ff_amr_write_config_atom(pb, par->codec_id, 1); } 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); @@ -252,8 +259,47 @@ static int caf_write_packet(AVFormatContext *s, AVPacket *pkt) { CAFContext *caf = s->priv_data; AVStream *const st = s->streams[0]; + const AVCodecParameters *par = st->codecpar; + int64_t duration = pkt->duration; + int64_t end_discard = -1; + uint32_t start_skip = 0; + + if (par->codec_id == AV_CODEC_ID_AMR_WB) { + const AVPacketSideData *sd; + int ret = ff_amr_check_frame_block(par->codec_id, pkt->data, pkt->size, + par->ch_layout.nb_channels); + if (ret < 0) { + av_log(s, AV_LOG_ERROR, "AMR packets must contain one complete frame per channel\n"); + return ret; + } + if (par->block_align && pkt->size != par->block_align) { + av_log(s, AV_LOG_ERROR, "AMR packet size does not match the block alignment\n"); + return AVERROR_INVALIDDATA; + } + + sd = av_packet_side_data_get(pkt->side_data, pkt->side_data_elems, + AV_PKT_DATA_SKIP_SAMPLES); + if (sd && sd->size >= 10) { + end_discard = AV_RL32(sd->data + 4); + if (end_discard >= caf->frame_size) { + av_log(s, AV_LOG_ERROR, "AMR end discard must leave samples in the packet\n"); + return AVERROR_INVALIDDATA; + } + start_skip = AV_RL32(sd->data); + } - if (!st->codecpar->block_align || !caf->frame_size) { + /* Only the final AMR frame block can carry tail padding. */ + duration = caf->frame_size; + if (end_discard >= 0) { + duration -= end_discard; + } else if (pkt->duration > 0 && pkt->duration < caf->frame_size) { + /* Without an explicit discard count, duration is only an estimate: + * stream-copy rescaling may already have rounded the endpoint. */ + duration = pkt->duration; + } + } + + if (!par->block_align || !caf->frame_size) { #if SIZE_MAX - 10 < UINT_MAX if (caf->pakt_buf_size > SIZE_MAX - 10) return AVERROR(ERANGE); @@ -265,13 +311,25 @@ 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 (!par->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; + + if (par->codec_id == AV_CODEC_ID_AMR_WB) { + if (caf->end_discard > 0) + av_log(s, AV_LOG_WARNING, "Ignoring the end discard of a packet before the last one\n"); + if (!st->nb_frames) + caf->priming = start_skip; + else if (start_skip) + av_log(s, AV_LOG_WARNING, "Ignoring the start skip of a packet after the first one\n"); + caf->total_duration = st->nb_frames * caf->frame_size + duration; + caf->end_discard = end_discard; + } else { + caf->total_duration += duration; + } avio_write(s->pb, pkt->data, pkt->size); return 0; @@ -283,6 +341,32 @@ static int caf_write_trailer(AVFormatContext *s) AVIOContext *pb = s->pb; AVStream *st = s->streams[0]; AVCodecParameters *par = st->codecpar; + uint32_t priming = par->initial_padding; + + if (par->codec_id == AV_CODEC_ID_AMR_WB) { + int64_t frames = st->nb_frames * caf->frame_size; + + /* Skip-samples side data may carry only the leading trim. */ + if (caf->end_discard <= 0 && par->trailing_padding) { + if (par->trailing_padding < 0 || par->trailing_padding >= caf->frame_size || + par->trailing_padding > frames) { + av_log(s, AV_LOG_ERROR, "AMR end discard must leave samples in the packet\n"); + return AVERROR(EINVAL); + } + caf->total_duration = frames - par->trailing_padding; + } + if (caf->priming) + priming = caf->priming; + if (priming > INT32_MAX || (priming && priming >= caf->total_duration)) { + av_log(s, AV_LOG_ERROR, "AMR priming must fit in the packet table and leave valid samples\n"); + return AVERROR(EINVAL); + } + if (!(pb->seekable & AVIO_SEEKABLE_NORMAL) && + (priming || caf->total_duration < frames)) { + av_log(s, AV_LOG_ERROR, "AMR padding requires a seekable output\n"); + return AVERROR(EINVAL); + } + } if (pb->seekable & AVIO_SEEKABLE_NORMAL) { int64_t file_size = avio_tell(pb); @@ -294,8 +378,8 @@ static int caf_write_trailer(AVFormatContext *s) 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; + if (!par->block_align || !caf->frame_size || priming || remainder_frames) { + valid_frames -= priming; valid_frames -= remainder_frames; avio_seek(pb, file_size, SEEK_SET); @@ -303,7 +387,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); ///< 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 e24d859c0e..789bfa037d 100644 --- a/tests/fate/caf.mak +++ b/tests/fate/caf.mak @@ -21,6 +21,18 @@ fate-caf-alac-remux: CMD = transcode m4a $(TARGET_SAMPLES)/lossless-audio/inside 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" +# Rounding each packet duration from a 999 Hz clock must not drop the last packet. +FATE_CAF_FFMPEG_FFPROBE-$(call REMUX, CAF, MOV_DEMUXER SETTS_BSF) += fate-caf-amr_wb-rounded-duration-remux +fate-caf-amr_wb-rounded-duration-remux: CMD = transcode mov $(TARGET_SAMPLES)/amrwb/seed-23k85.awb caf \ + "-c copy -bsf:a setts=time_base=1/999:prescale=1:duration=NEXT_PTS-PTS -frames:a 1000" \ + "-c copy -frames:a 2" "-count_packets -show_entries stream=duration_ts,nb_read_packets -of compact" \ + "" "" "-stream_loop 1" + +# Two complete coded packets with 7 samples of priming and 11 of end discard. +FATE_CAF_FFMPEG_FFPROBE-$(call REMUX, CAF, MOV_DEMUXER TRIM_BSF) += fate-caf-amr_wb-skip-samples-remux +fate-caf-amr_wb-skip-samples-remux: CMD = transcode mov $(TARGET_SAMPLES)/amrwb/seed-23k85.awb caf \ + "-c copy -bsf:a trim=start=7:end=629" "-c copy" "-show_entries stream=initial_padding,duration_ts -of compact" + FATE_CAF_FFMPEG-$(call REMUX, CAF, MOV_DEMUXER) += fate-caf-qdm2-remux fate-caf-qdm2-remux: CMD = transcode mov $(TARGET_SAMPLES)/qt-surge-suite/surge-2-16-B-QDM2.mov caf "-c copy" "-c copy -t 0.2" diff --git a/tests/ref/fate/caf-amr_wb-rounded-duration-remux b/tests/ref/fate/caf-amr_wb-rounded-duration-remux new file mode 100644 index 0000000000..7f9b068e25 --- /dev/null +++ b/tests/ref/fate/caf-amr_wb-rounded-duration-remux @@ -0,0 +1,11 @@ +7b3ed16616540ac21ca5b480e0aa543f *tests/data/fate/caf-amr_wb-rounded-duration-remux.caf +62169 tests/data/fate/caf-amr_wb-rounded-duration-remux.caf +#extradata 0: 29, 0x6ada07cf +#tb 0: 1/16000 +#media_type 0: audio +#codec_id 0: amr_wb +#sample_rate 0: 16000 +#channel_layout_name 0: mono +0, 0, 0, 320, 61, 0xc8891fe9 +0, 320, 320, 320, 61, 0xc2562027 +stream|duration_ts=320000|nb_read_packets=1000 diff --git a/tests/ref/fate/caf-amr_wb-skip-samples-remux b/tests/ref/fate/caf-amr_wb-skip-samples-remux new file mode 100644 index 0000000000..cb30a46e12 --- /dev/null +++ b/tests/ref/fate/caf-amr_wb-skip-samples-remux @@ -0,0 +1,11 @@ +2a5149355add8cfa6aef36da665d62dc *tests/data/fate/caf-amr_wb-skip-samples-remux.caf +293 tests/data/fate/caf-amr_wb-skip-samples-remux.caf +#extradata 0: 29, 0x6ada07cf +#tb 0: 1/16000 +#media_type 0: audio +#codec_id 0: amr_wb +#sample_rate 0: 16000 +#channel_layout_name 0: mono +0, -7, -7, 320, 61, 0xc8891fe9, S=1, Skip Samples, 10, 0x00460007 +0, 313, 313, 309, 61, 0xc2562027, S=1, Skip Samples, 10, 0x0042000b +stream|initial_padding=7|duration_ts=622 -- To stop receiving notification emails like this one, please contact [email protected]. _______________________________________________ ffmpeg-cvslog mailing list -- [email protected] To unsubscribe send an email to [email protected]
