PR #26257 opened by Baptiste Coudurier (bcoudurier) URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26257 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26257.patch
The CAF muxer turned any difference between packet durations and the fixed frames per packet into remainder frames, so streams whose durations drift or use another time base got trimmed far beyond the last packet. AMR-NB copied with a 1/1024 time base wrote 39760 remainder frames for 160-frame packets. The remainder now comes from the last packet only, ignoring shortfalls within one input tick. Deliberately shortened last packets are kept. Adds fate-caf-amr_nb-timebase-remux, whose CAF matches the plain remux byte for byte. 🤖 Generated with [Claude Code](https://claude.com/claude-code) From 067d35cd599d68d5fe4f530498235800332fddb9 Mon Sep 17 00:00:00 2001 From: Baptiste Coudurier <[email protected]> Date: Sat, 21 Nov 2026 12:09:43 -0800 Subject: [PATCH] avformat/cafenc: take the remainder from the last packet only Duration differences across the stream became remainder frames, exceeding the packet size. Signed-off-by: Baptiste Coudurier <[email protected]> --- libavformat/cafenc.c | 14 +- tests/fate/caf.mak | 3 + tests/ref/fate/caf-amr_nb-timebase-remux | 295 +++++++++++++++++++++++ 3 files changed, 310 insertions(+), 2 deletions(-) create mode 100644 tests/ref/fate/caf-amr_nb-timebase-remux diff --git a/libavformat/cafenc.c b/libavformat/cafenc.c index 0634bdb4c14..0eb3e5974c9 100644 --- a/libavformat/cafenc.c +++ b/libavformat/cafenc.c @@ -36,6 +36,7 @@ typedef struct { int64_t data; int64_t total_duration; + int64_t last_duration; uint32_t frame_size; uint8_t *pakt_buf; @@ -272,6 +273,7 @@ static int caf_write_packet(AVFormatContext *s, AVPacket *pkt) caf->pakt_buf_size = pakt_buf - caf->pakt_buf; } caf->total_duration += pkt->duration; + caf->last_duration = pkt->duration; avio_write(s->pb, pkt->data, pkt->size); return 0; @@ -288,8 +290,16 @@ static int caf_write_trailer(AVFormatContext *s) 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; + unsigned remainder_frames = 0; + + /* Only the last packet can be shortened, by more than rounding. */ + if (caf->frame_size) { + AVRational sample_tb = { 1, par->sample_rate }; + int64_t last = av_rescale_q(caf->last_duration, st->time_base, sample_tb); + int64_t tick = av_rescale_q_rnd(1, st->time_base, sample_tb, AV_ROUND_UP); + if (last > 0 && last + tick <= caf->frame_size) + remainder_frames = caf->frame_size - last; + } avio_seek(pb, caf->data, SEEK_SET); avio_wb64(pb, file_size - caf->data - 8); diff --git a/tests/fate/caf.mak b/tests/fate/caf.mak index 4ee9c95a5c8..c0dd24e851d 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-$(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" +FATE_CAF_FFMPEG_FFPROBE-$(call REMUX, CAF, AMR_DEMUXER SETTS_BSF) += fate-caf-amr_nb-timebase-remux +fate-caf-amr_nb-timebase-remux: CMD = transcode amr $(TARGET_SAMPLES)/amrnb/4.75k.amr caf "-c copy -bsf:a setts=time_base=1/1024" "-c copy" "-show_entries stream=duration_ts" + 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_nb-timebase-remux b/tests/ref/fate/caf-amr_nb-timebase-remux new file mode 100644 index 00000000000..1623c525004 --- /dev/null +++ b/tests/ref/fate/caf-amr_nb-timebase-remux @@ -0,0 +1,295 @@ +a00bd18f70b66286e67d84f0df034a48 *tests/data/fate/caf-amr_nb-timebase-remux.caf +4145 tests/data/fate/caf-amr_nb-timebase-remux.caf +#extradata 0: 29, 0x6b3407d3 +#tb 0: 1/8000 +#media_type 0: audio +#codec_id 0: amr_nb +#sample_rate 0: 8000 +#channel_layout_name 0: mono +0, 0, 0, 160, 13, 0x2bf906f6 +0, 160, 160, 160, 13, 0x28bd0756 +0, 320, 320, 160, 13, 0x2b2706f7 +0, 480, 480, 160, 13, 0x370d0858 +0, 640, 640, 160, 13, 0x148a02de +0, 800, 800, 160, 13, 0x201104b7 +0, 960, 960, 160, 13, 0x2e690704 +0, 1120, 1120, 160, 13, 0x2f640726 +0, 1280, 1280, 160, 13, 0x31af0767 +0, 1440, 1440, 160, 13, 0x273a060f +0, 1600, 1600, 160, 13, 0x2dc40693 +0, 1760, 1760, 160, 13, 0x39a5080d +0, 1920, 1920, 160, 13, 0x2dc5060a +0, 2080, 2080, 160, 13, 0x26150628 +0, 2240, 2240, 160, 13, 0x25150626 +0, 2400, 2400, 160, 13, 0x2e330673 +0, 2560, 2560, 160, 13, 0x2a99068c +0, 2720, 2720, 160, 13, 0x31860652 +0, 2880, 2880, 160, 13, 0x2f830756 +0, 3040, 3040, 160, 13, 0x226c0515 +0, 3200, 3200, 160, 13, 0x302e073b +0, 3360, 3360, 160, 13, 0x2b8906c4 +0, 3520, 3520, 160, 13, 0x28e905a2 +0, 3680, 3680, 160, 13, 0x26cf0675 +0, 3840, 3840, 160, 13, 0x31e407d8 +0, 4000, 4000, 160, 13, 0x2ac8065b +0, 4160, 4160, 160, 13, 0x3aba08ae +0, 4320, 4320, 160, 13, 0x28b8063b +0, 4480, 4480, 160, 13, 0x2a8f05c2 +0, 4640, 4640, 160, 13, 0x239005d8 +0, 4800, 4800, 160, 13, 0x2c5f06c7 +0, 4960, 4960, 160, 13, 0x26180632 +0, 5120, 5120, 160, 13, 0x273805c7 +0, 5280, 5280, 160, 13, 0x24fc05c3 +0, 5440, 5440, 160, 13, 0x2e220717 +0, 5600, 5600, 160, 13, 0x37c10838 +0, 5760, 5760, 160, 13, 0x2f74073d +0, 5920, 5920, 160, 13, 0x369f0870 +0, 6080, 6080, 160, 13, 0x2dfd0739 +0, 6240, 6240, 160, 13, 0x2b09065c +0, 6400, 6400, 160, 13, 0x336a070c +0, 6560, 6560, 160, 13, 0x3a110883 +0, 6720, 6720, 160, 13, 0x339b06cd +0, 6880, 6880, 160, 13, 0x35a9070e +0, 7040, 7040, 160, 13, 0x307306fd +0, 7200, 7200, 160, 13, 0x32d1068b +0, 7360, 7360, 160, 13, 0x352107a1 +0, 7520, 7520, 160, 13, 0x29c305fd +0, 7680, 7680, 160, 13, 0x240804e3 +0, 7840, 7840, 160, 13, 0x294405d6 +0, 8000, 8000, 160, 13, 0x3314073b +0, 8160, 8160, 160, 13, 0x30af074b +0, 8320, 8320, 160, 13, 0x352407d2 +0, 8480, 8480, 160, 13, 0x313507ff +0, 8640, 8640, 160, 13, 0x2f9606b7 +0, 8800, 8800, 160, 13, 0x1d360439 +0, 8960, 8960, 160, 13, 0x254205ee +0, 9120, 9120, 160, 13, 0x2ad90608 +0, 9280, 9280, 160, 13, 0x2522053a +0, 9440, 9440, 160, 13, 0x39150838 +0, 9600, 9600, 160, 13, 0x221704a3 +0, 9760, 9760, 160, 13, 0x2f0c0696 +0, 9920, 9920, 160, 13, 0x3037066d +0, 10080, 10080, 160, 13, 0x2ae205b7 +0, 10240, 10240, 160, 13, 0x24b505d6 +0, 10400, 10400, 160, 13, 0x28930673 +0, 10560, 10560, 160, 13, 0x2f7406db +0, 10720, 10720, 160, 13, 0x33160774 +0, 10880, 10880, 160, 13, 0x381207b7 +0, 11040, 11040, 160, 13, 0x2f6d0662 +0, 11200, 11200, 160, 13, 0x3229072b +0, 11360, 11360, 160, 13, 0x2da106fc +0, 11520, 11520, 160, 13, 0x28ba062b +0, 11680, 11680, 160, 13, 0x317407ac +0, 11840, 11840, 160, 13, 0x224a04af +0, 12000, 12000, 160, 13, 0x336907b5 +0, 12160, 12160, 160, 13, 0x364c07a8 +0, 12320, 12320, 160, 13, 0x2de70679 +0, 12480, 12480, 160, 13, 0x2fee0727 +0, 12640, 12640, 160, 13, 0x35560775 +0, 12800, 12800, 160, 13, 0x318a0656 +0, 12960, 12960, 160, 13, 0x2d6e06ba +0, 13120, 13120, 160, 13, 0x326c06d6 +0, 13280, 13280, 160, 13, 0x2630058e +0, 13440, 13440, 160, 13, 0x2e300673 +0, 13600, 13600, 160, 13, 0x262d058a +0, 13760, 13760, 160, 13, 0x265e0515 +0, 13920, 13920, 160, 13, 0x2e6d0610 +0, 14080, 14080, 160, 13, 0x366b0721 +0, 14240, 14240, 160, 13, 0x2f8306bd +0, 14400, 14400, 160, 13, 0x37e307c4 +0, 14560, 14560, 160, 13, 0x2d7e05e2 +0, 14720, 14720, 160, 13, 0x266b057c +0, 14880, 14880, 160, 13, 0x2ddb063b +0, 15040, 15040, 160, 13, 0x31460716 +0, 15200, 15200, 160, 13, 0x247d05e3 +0, 15360, 15360, 160, 13, 0x25d10591 +0, 15520, 15520, 160, 13, 0x2b340600 +0, 15680, 15680, 160, 13, 0x32eb07e1 +0, 15840, 15840, 160, 13, 0x2c81064b +0, 16000, 16000, 160, 13, 0x2d480652 +0, 16160, 16160, 160, 13, 0x2a790640 +0, 16320, 16320, 160, 13, 0x30f707ac +0, 16480, 16480, 160, 13, 0x2c01073b +0, 16640, 16640, 160, 13, 0x3b3707c1 +0, 16800, 16800, 160, 13, 0x2e8c071f +0, 16960, 16960, 160, 13, 0x36650857 +0, 17120, 17120, 160, 13, 0x2b070630 +0, 17280, 17280, 160, 13, 0x2cc405aa +0, 17440, 17440, 160, 13, 0x3961084f +0, 17600, 17600, 160, 13, 0x369d0772 +0, 17760, 17760, 160, 13, 0x2a3705dd +0, 17920, 17920, 160, 13, 0x2fda0739 +0, 18080, 18080, 160, 13, 0x350b06df +0, 18240, 18240, 160, 13, 0x3856087e +0, 18400, 18400, 160, 13, 0x2f0a061e +0, 18560, 18560, 160, 13, 0x258805aa +0, 18720, 18720, 160, 13, 0x285c066e +0, 18880, 18880, 160, 13, 0x3e070912 +0, 19040, 19040, 160, 13, 0x3384078e +0, 19200, 19200, 160, 13, 0x2878069a +0, 19360, 19360, 160, 13, 0x2b2e0702 +0, 19520, 19520, 160, 13, 0x236b0596 +0, 19680, 19680, 160, 13, 0x2b570698 +0, 19840, 19840, 160, 13, 0x2267059c +0, 20000, 20000, 160, 13, 0x21590557 +0, 20160, 20160, 160, 13, 0x25e00697 +0, 20320, 20320, 160, 13, 0x1fd1050f +0, 20480, 20480, 160, 13, 0x31170760 +0, 20640, 20640, 160, 13, 0x2c65078e +0, 20800, 20800, 160, 13, 0x1f7805cc +0, 20960, 20960, 160, 13, 0x2af40656 +0, 21120, 21120, 160, 13, 0x27d405af +0, 21280, 21280, 160, 13, 0x324c0791 +0, 21440, 21440, 160, 13, 0x2ead06d9 +0, 21600, 21600, 160, 13, 0x27e10671 +0, 21760, 21760, 160, 13, 0x25430537 +0, 21920, 21920, 160, 13, 0x2c4b0661 +0, 22080, 22080, 160, 13, 0x2ba70624 +0, 22240, 22240, 160, 13, 0x2cd50656 +0, 22400, 22400, 160, 13, 0x2d8a064c +0, 22560, 22560, 160, 13, 0x300406e5 +0, 22720, 22720, 160, 13, 0x2ee505e4 +0, 22880, 22880, 160, 13, 0x2ada06ce +0, 23040, 23040, 160, 13, 0x2da10646 +0, 23200, 23200, 160, 13, 0x25f705e8 +0, 23360, 23360, 160, 13, 0x1de60454 +0, 23520, 23520, 160, 13, 0x2adf0628 +0, 23680, 23680, 160, 13, 0x28a30614 +0, 23840, 23840, 160, 13, 0x2c5b0710 +0, 24000, 24000, 160, 13, 0x2f0b0726 +0, 24160, 24160, 160, 13, 0x1c5404a3 +0, 24320, 24320, 160, 13, 0x28a50614 +0, 24480, 24480, 160, 13, 0x221204fb +0, 24640, 24640, 160, 13, 0x247a056e +0, 24800, 24800, 160, 13, 0x2c52070e +0, 24960, 24960, 160, 13, 0x271c05e7 +0, 25120, 25120, 160, 13, 0x3027071b +0, 25280, 25280, 160, 13, 0x2e060646 +0, 25440, 25440, 160, 13, 0x27810558 +0, 25600, 25600, 160, 13, 0x225c0524 +0, 25760, 25760, 160, 13, 0x264b0552 +0, 25920, 25920, 160, 13, 0x22da05e1 +0, 26080, 26080, 160, 13, 0x2a050720 +0, 26240, 26240, 160, 13, 0x23d604d3 +0, 26400, 26400, 160, 13, 0x2c5d0713 +0, 26560, 26560, 160, 13, 0x29fe05cf +0, 26720, 26720, 160, 13, 0x2a9806a6 +0, 26880, 26880, 160, 13, 0x22a104d7 +0, 27040, 27040, 160, 13, 0x2a2406d4 +0, 27200, 27200, 160, 13, 0x2aae06b8 +0, 27360, 27360, 160, 13, 0x2f770748 +0, 27520, 27520, 160, 13, 0x286b0620 +0, 27680, 27680, 160, 13, 0x29240658 +0, 27840, 27840, 160, 13, 0x32fa06f6 +0, 28000, 28000, 160, 13, 0x249c05cd +0, 28160, 28160, 160, 13, 0x2fa4071e +0, 28320, 28320, 160, 13, 0x1f910475 +0, 28480, 28480, 160, 13, 0x2b76065c +0, 28640, 28640, 160, 13, 0x30a50732 +0, 28800, 28800, 160, 13, 0x2841052b +0, 28960, 28960, 160, 13, 0x26ff05cd +0, 29120, 29120, 160, 13, 0x30ce06c9 +0, 29280, 29280, 160, 13, 0x24520510 +0, 29440, 29440, 160, 13, 0x2f93062b +0, 29600, 29600, 160, 13, 0x2bee072b +0, 29760, 29760, 160, 13, 0x2ee907ae +0, 29920, 29920, 160, 13, 0x34cb079d +0, 30080, 30080, 160, 13, 0x2f8f076f +0, 30240, 30240, 160, 13, 0x27b205d5 +0, 30400, 30400, 160, 13, 0x30c306c6 +0, 30560, 30560, 160, 13, 0x228105fb +0, 30720, 30720, 160, 13, 0x2b6606b8 +0, 30880, 30880, 160, 13, 0x2b8d0761 +0, 31040, 31040, 160, 13, 0x23a50588 +0, 31200, 31200, 160, 13, 0x21ed0519 +0, 31360, 31360, 160, 13, 0x230805a4 +0, 31520, 31520, 160, 13, 0x1f280487 +0, 31680, 31680, 160, 13, 0x29af05aa +0, 31840, 31840, 160, 13, 0x28680630 +0, 32000, 32000, 160, 13, 0x2760067f +0, 32160, 32160, 160, 13, 0x1a7e04e5 +0, 32320, 32320, 160, 13, 0x1e5004a1 +0, 32480, 32480, 160, 13, 0x278c0673 +0, 32640, 32640, 160, 13, 0x271205e6 +0, 32800, 32800, 160, 13, 0x2b0606cf +0, 32960, 32960, 160, 13, 0x25b405b0 +0, 33120, 33120, 160, 13, 0x2f9006cb +0, 33280, 33280, 160, 13, 0x2d68070d +0, 33440, 33440, 160, 13, 0x342706e7 +0, 33600, 33600, 160, 13, 0x224204fc +0, 33760, 33760, 160, 13, 0x27da0611 +0, 33920, 33920, 160, 13, 0x2fe107a3 +0, 34080, 34080, 160, 13, 0x31a20665 +0, 34240, 34240, 160, 13, 0x344e0687 +0, 34400, 34400, 160, 13, 0x2c890641 +0, 34560, 34560, 160, 13, 0x24060529 +0, 34720, 34720, 160, 13, 0x2c930602 +0, 34880, 34880, 160, 13, 0x2d2b0649 +0, 35040, 35040, 160, 13, 0x29d205eb +0, 35200, 35200, 160, 13, 0x22d704eb +0, 35360, 35360, 160, 13, 0x28770676 +0, 35520, 35520, 160, 13, 0x267705f7 +0, 35680, 35680, 160, 13, 0x2ce706da +0, 35840, 35840, 160, 13, 0x1d1d04a2 +0, 36000, 36000, 160, 13, 0x25f904a0 +0, 36160, 36160, 160, 13, 0x28640617 +0, 36320, 36320, 160, 13, 0x2fbf068f +0, 36480, 36480, 160, 13, 0x250205b6 +0, 36640, 36640, 160, 13, 0x290705f3 +0, 36800, 36800, 160, 13, 0x283005c4 +0, 36960, 36960, 160, 13, 0x27a20645 +0, 37120, 37120, 160, 13, 0x2eed0680 +0, 37280, 37280, 160, 13, 0x23690534 +0, 37440, 37440, 160, 13, 0x30a1072c +0, 37600, 37600, 160, 13, 0x29450590 +0, 37760, 37760, 160, 13, 0x233304ee +0, 37920, 37920, 160, 13, 0x269f05cb +0, 38080, 38080, 160, 13, 0x277a070b +0, 38240, 38240, 160, 13, 0x2aee061f +0, 38400, 38400, 160, 13, 0x28c60624 +0, 38560, 38560, 160, 13, 0x2c0206bf +0, 38720, 38720, 160, 13, 0x2d3b070e +0, 38880, 38880, 160, 13, 0x2fcd0728 +0, 39040, 39040, 160, 13, 0x28d60753 +0, 39200, 39200, 160, 13, 0x296d0617 +0, 39360, 39360, 160, 13, 0x2f7f07f1 +0, 39520, 39520, 160, 13, 0x2f7106c2 +0, 39680, 39680, 160, 13, 0x3295073a +0, 39840, 39840, 160, 13, 0x2a0b06fa +0, 40000, 40000, 160, 13, 0x2ed8068e +0, 40160, 40160, 160, 13, 0x2a5905ff +0, 40320, 40320, 160, 13, 0x215f0560 +0, 40480, 40480, 160, 13, 0x28720660 +0, 40640, 40640, 160, 13, 0x34fa07af +0, 40800, 40800, 160, 13, 0x3313072d +0, 40960, 40960, 160, 13, 0x34790729 +0, 41120, 41120, 160, 13, 0x2bb906f4 +0, 41280, 41280, 160, 13, 0x3ace07e3 +0, 41440, 41440, 160, 13, 0x265205d3 +0, 41600, 41600, 160, 13, 0x2e8b075c +0, 41760, 41760, 160, 13, 0x2d0c06cd +0, 41920, 41920, 160, 13, 0x2d5e06da +0, 42080, 42080, 160, 13, 0x307f069d +0, 42240, 42240, 160, 13, 0x2b8e068c +0, 42400, 42400, 160, 13, 0x2e5606a1 +0, 42560, 42560, 160, 13, 0x2fb5067b +0, 42720, 42720, 160, 13, 0x270f05cb +0, 42880, 42880, 160, 13, 0x22e40580 +0, 43040, 43040, 160, 13, 0x28c50635 +0, 43200, 43200, 160, 13, 0x2ac50633 +0, 43360, 43360, 160, 13, 0x200904ee +0, 43520, 43520, 160, 13, 0x2fe806a5 +0, 43680, 43680, 160, 13, 0x2f2b06ec +0, 43840, 43840, 160, 13, 0x2ebd0695 +0, 44000, 44000, 160, 13, 0x303a0720 +0, 44160, 44160, 160, 13, 0x23250524 +0, 44320, 44320, 160, 13, 0x1dd80404 +0, 44480, 44480, 160, 13, 0x26ab05f4 +0, 44640, 44640, 160, 13, 0x320606f9 +0, 44800, 44800, 160, 13, 0x26c2058f +0, 44960, 44960, 160, 13, 0x2caa065c +0, 45120, 45120, 160, 13, 0x2855063e +0, 45280, 45280, 160, 13, 0x30890656 +[STREAM] +duration_ts=45440 +[/STREAM] -- 2.54.1 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
