Commit 22a5fc1f68 for ffmpeg
commit 22a5fc1f6893efab8b82d01a76233e96edbb38e0
Author: Baptiste Coudurier <baptiste.coudurier@gmail.com>
Date: Sat Nov 21 12:06:45 2026 -0800
fftools/sync_queue: keep the cut audio packet in the muxing queue
With a streamcopied stream, the packet queue dropped the last audio
packet cut by the frame queue, ending a fraction of a sample late.
Round its cutoff only for encoded audio without a bitstream filter, and
only when a tick is no longer than a sample, allowing packet-end
timestamps up to half a sample past the cutoff.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Baptiste Coudurier <baptiste.coudurier@gmail.com>
diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index a6991afd87..8c67908e54 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -671,6 +671,12 @@ int of_stream_init(OutputFile *of, OutputStream *ost,
ost->st->time_base);
}
+ /* Bitstream filters can change packet timing. A finer output time base
+ * can hide the loss of precision at an earlier stage. */
+ if (mux->have_sq_enc && ms->sq_idx_mux >= 0 && !ms->bsf_ctx && enc_ctx &&
+ ost->type == AVMEDIA_TYPE_AUDIO && !enc_ctx->frame_size)
+ sq_cut_upstream(mux->sq_mux, ms->sq_idx_mux, enc_ctx->sample_rate);
+
if (ms->sch_idx >= 0)
return sch_mux_stream_ready(mux->sch, of->index, ms->sch_idx);
diff --git a/fftools/ffmpeg_mux.h b/fftools/ffmpeg_mux.h
index 3d6686da02..60d876e6de 100644
--- a/fftools/ffmpeg_mux.h
+++ b/fftools/ffmpeg_mux.h
@@ -117,6 +117,7 @@ typedef struct Muxer {
atomic_int_least64_t last_filesize;
int header_written;
+ int have_sq_enc;
SyncQueue *sq_mux;
AVPacket *sq_pkt;
} Muxer;
diff --git a/fftools/ffmpeg_mux_init.c b/fftools/ffmpeg_mux_init.c
index 00d33bb03b..812fc6cda2 100644
--- a/fftools/ffmpeg_mux_init.c
+++ b/fftools/ffmpeg_mux_init.c
@@ -2258,6 +2258,7 @@ static int setup_sync_queues(Muxer *mux, AVFormatContext *oc,
sq_idx = sch_add_sq_enc(mux->sch, buf_size_us, mux);
if (sq_idx < 0)
return sq_idx;
+ mux->have_sq_enc = 1;
for (int i = 0; i < oc->nb_streams; i++) {
OutputStream *ost = of->streams[i];
diff --git a/fftools/sync_queue.c b/fftools/sync_queue.c
index 20fa822e18..ad6dcdea59 100644
--- a/fftools/sync_queue.c
+++ b/fftools/sync_queue.c
@@ -85,6 +85,8 @@ typedef struct SyncQueueStream {
uint64_t samples_sent;
uint64_t frames_max;
int frame_samples;
+ /* sample rate for audio that can be cut by a frame queue, or zero */
+ int cut_sample_rate;
} SyncQueueStream;
struct SyncQueue {
@@ -549,6 +551,13 @@ static int receive_for_stream(SyncQueue *sq, unsigned int stream_idx,
av_rescale_q(peek.f->pts, st->tb, sample_tb);
cut = FFMIN(cut, peek.f->nb_samples);
}
+
+ /* Allow the nearest tick for cut audio if that tick is no longer
+ * than a sample. A coarse packet time base can otherwise admit a
+ * whole packet that the frame queue released without cutting. */
+ if (cmp > 0 && st_head->finished && st->cut_sample_rate &&
+ av_cmp_q(st->tb, (AVRational){ 1, st->cut_sample_rate }) <= 0)
+ cmp = ts > av_rescale_q(st_head->head_ts, st_head->tb, st->tb);
}
/* We can release frames that do not end after the queue head.
@@ -666,6 +675,14 @@ void sq_limit_frames(SyncQueue *sq, unsigned int stream_idx, uint64_t frames)
finish_stream(sq, stream_idx);
}
+void sq_cut_upstream(SyncQueue *sq, unsigned int stream_idx, int sample_rate)
+{
+ av_assert0(sq->type == SYNC_QUEUE_PACKETS);
+ av_assert0(stream_idx < sq->nb_streams);
+ av_assert0(sample_rate > 0);
+ sq->streams[stream_idx].cut_sample_rate = sample_rate;
+}
+
void sq_frame_samples(SyncQueue *sq, unsigned int stream_idx,
int frame_samples)
{
diff --git a/fftools/sync_queue.h b/fftools/sync_queue.h
index dc5acfd499..9ce51c9787 100644
--- a/fftools/sync_queue.h
+++ b/fftools/sync_queue.h
@@ -71,6 +71,14 @@ int sq_add_stream(SyncQueue *sq, int limiting);
void sq_limit_frames(SyncQueue *sq, unsigned int stream_idx,
uint64_t max_frames);
+/**
+ * Allow nearest-tick cutoff for packets from an audio stream that can be cut
+ * by a frame queue, with no intervening bitstream filter. The packet time
+ * base must be at least as fine as 1/sample_rate. Can only be used with
+ * SYNC_QUEUE_PACKETS queues.
+ */
+void sq_cut_upstream(SyncQueue *sq, unsigned int stream_idx, int sample_rate);
+
/**
* Set a constant output audio frame size, in samples. Can only be used with
* SYNC_QUEUE_FRAMES queues and audio streams.
diff --git a/tests/fate/ffmpeg.mak b/tests/fate/ffmpeg.mak
index dca806bb98..96252410a6 100644
--- a/tests/fate/ffmpeg.mak
+++ b/tests/fate/ffmpeg.mak
@@ -158,6 +158,18 @@ fate-ffmpeg-frames-cut-audio-round: CMD = framecrc \
-f lavfi -i "color=s=16x16:r=30000/1001:d=2[out0];sine=r=48000:samples_per_frame=1602:d=2[out1]" \
-map 0:v -map 0:a -frames:v 1
+# the cut audio ending at 14715, after 14714.7, passes the muxing sync queue
+FATE_FFMPEG-$(call ALLYES, LAVFI_INDEV COLOR_FILTER SINE_FILTER \
+ AFORMAT_FILTER ARESAMPLE_FILTER \
+ WRAPPED_AVFRAME_DECODER \
+ PCM_S16$(if $(filter yes,$(HAVE_BIGENDIAN)),BE,LE)_DECODER \
+ RAWVIDEO_ENCODER PCM_S16LE_ENCODER FRAMECRC_MUXER \
+ PIPE_PROTOCOL) \
+ += fate-ffmpeg-frames-cut-audio-copy
+fate-ffmpeg-frames-cut-audio-copy: CMD = framecrc \
+ -f lavfi -i "color=s=16x16:r=30000/1001:d=2[out0];sine=r=44100:samples_per_frame=4410:d=2[out1];sine=r=44100:samples_per_frame=4410:d=2,aformat=u8[out2]" \
+ -map 0:v -map 0:a:0 -map 0:a:1 -c:a:1 copy -frames:v 10
+
# test interleaving video with a sparse subtitle stream
FATE_SAMPLES_FFMPEG-$(call FRAMECRC, MATROSKA,, COLOR_FILTER VOBSUB_DEMUXER \
RAWVIDEO_ENCODER MATROSKA_MUXER) += fate-shortest-sub
diff --git a/tests/ref/fate/ffmpeg-frames-cut-audio-copy b/tests/ref/fate/ffmpeg-frames-cut-audio-copy
new file mode 100644
index 0000000000..70cb6be104
--- /dev/null
+++ b/tests/ref/fate/ffmpeg-frames-cut-audio-copy
@@ -0,0 +1,32 @@
+#tb 0: 1001/30000
+#media_type 0: video
+#codec_id 0: rawvideo
+#dimensions 0: 16x16
+#sar 0: 1/1
+#tb 1: 1/44100
+#media_type 1: audio
+#codec_id 1: pcm_s16le
+#sample_rate 1: 44100
+#channel_layout_name 1: mono
+#tb 2: 1/44100
+#media_type 2: audio
+#codec_id 2: pcm_u8
+#sample_rate 2: 44100
+#channel_layout_name 2: mono
+0, 0, 0, 1, 384, 0x29e05000
+1, 0, 0, 4410, 8820, 0x9e493079
+2, 0, 0, 4410, 4410, 0xe1fd94dc
+0, 1, 1, 1, 384, 0x29e05000
+0, 2, 2, 1, 384, 0x29e05000
+1, 4410, 4410, 4410, 8820, 0x9e493079
+2, 4410, 4410, 4410, 4410, 0xe1fd94dc
+0, 3, 3, 1, 384, 0x29e05000
+0, 4, 4, 1, 384, 0x29e05000
+0, 5, 5, 1, 384, 0x29e05000
+1, 8820, 8820, 4410, 8820, 0x9e493079
+2, 8820, 8820, 4410, 4410, 0xe1fd94dc
+0, 6, 6, 1, 384, 0x29e05000
+0, 7, 7, 1, 384, 0x29e05000
+0, 8, 8, 1, 384, 0x29e05000
+1, 13230, 13230, 1485, 2970, 0xd8a1bd39
+0, 9, 9, 1, 384, 0x29e05000