PR #26248 opened by rafaelcl292
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26248
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26248.patch

9a13d22118 lowered the default packet thread queue size from 8 to 2. That 
default also applies to the muxer queue, which with streamcopy is fed directly 
by the demuxer thread. With room for only 2 packets per stream, the demuxer and 
muxer threads wake each other up for almost every packet, and for inputs with 
many small packets that handoff dominates the runtime (about 60% of cycles in a 
perf profile, mostly scheduler wakeups and context switches).

This gives muxer queues their own default and keeps it at 8. Decoder packet 
queues and frame queues are unchanged, and `-thread_queue_size` still overrides 
it.

Streamcopy, 2 vCPU Neoverse-N1 VM, pinned to 1 core / unpinned (wall time):

| | before | after |
|---|---|---|
| 240k packet mkv -> mkv | 689 / 586 ms | 354 / 452 ms |
| 10 min h264+aac mp4 -> ts | 271 / 397 ms | 203 / 352 ms |
| aac decode -> pcm (control) | 837 ms | 816 ms (noise) |

On x86 (i7-10750H, WSL2) the first case uses 27% / 73% less CPU time. Output is 
bitexact for queue sizes 2, 8, 32 and 128, and FATE passes.

Larger queues help a bit further (32 packets: 272 / 460 ms in the first case), 
but 8 keeps the previous footprint.

cc @haasn



>From 4fabb939e8b977d5f4129838b548b43a5b4316af Mon Sep 17 00:00:00 2001
From: Rafael Leventhal <[email protected]>
Date: Sat, 21 Nov 2026 10:56:39 -0300
Subject: [PATCH] fftools/ffmpeg_sched: use a separate default queue size for
 muxers

9a13d22118 lowered the default packet thread queue size from 8 to 2.
That default is also used for the muxer queue, which with streamcopy is
fed directly by the demuxer thread. With room for only 2 packets per
stream, the demuxer and muxer threads wake each other up for almost every
packet, which dominates the runtime for inputs with many small packets.

Give muxer queues their own default and keep it at 8. Decoder packet
queues are unaffected, and -thread_queue_size still overrides it.

Streamcopy on a 2 vCPU Neoverse-N1 VM, pinned to 1 core / unpinned:
    240k packet mkv -> mkv:      689 /  586 ms ->  354 /  452 ms
    10 min h264+aac mp4 -> ts:   271 /  397 ms ->  203 /  352 ms
On x86 (i7-10750H, WSL2) the first case uses 27% / 73% less CPU time.
Output is bitexact for any queue size.

Regressed-by: 9a13d22118 ("fftools/ffmpeg_sched: drop packet thread queue size")
Assisted-by: Claude
Signed-off-by: Rafael Leventhal <[email protected]>
---
 doc/ffmpeg.texi        | 2 +-
 fftools/ffmpeg_sched.c | 2 +-
 fftools/ffmpeg_sched.h | 9 +++++++--
 3 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/doc/ffmpeg.texi b/doc/ffmpeg.texi
index bff7e31c7c4..48baebe5b94 100644
--- a/doc/ffmpeg.texi
+++ b/doc/ffmpeg.texi
@@ -2854,7 +2854,7 @@ not start from timestamp 0, such as transport streams.
 
 @item -thread_queue_size @var{size} (@emph{output})
 This option specifies the maximum number of packets per stream that may be
-queued to each muxing thread.
+queued to each muxing thread. Default is 8.
 
 @item -sdp_file @var{file} (@emph{global})
 Print sdp information for an output stream to @var{file}.
diff --git a/fftools/ffmpeg_sched.c b/fftools/ffmpeg_sched.c
index fe64eb6557a..960e7325877 100644
--- a/fftools/ffmpeg_sched.c
+++ b/fftools/ffmpeg_sched.c
@@ -684,7 +684,7 @@ int sch_add_mux(Scheduler *sch, SchThreadFunc func, int 
(*init)(void *),
     mux             = &sch->mux[idx];
     mux->class      = &sch_mux_class;
     mux->init       = init;
-    mux->queue_size = thread_queue_size;
+    mux->queue_size = thread_queue_size ? thread_queue_size : 
DEFAULT_MUX_THREAD_QUEUE_SIZE;
 
     task_init(sch, &mux->task, SCH_NODE_TYPE_MUX, idx, func, arg);
 
diff --git a/fftools/ffmpeg_sched.h b/fftools/ffmpeg_sched.h
index 26f8fb6d945..4f4058abad0 100644
--- a/fftools/ffmpeg_sched.h
+++ b/fftools/ffmpeg_sched.h
@@ -251,11 +251,16 @@ int sch_add_mux(Scheduler *sch, SchThreadFunc func, int 
(*init)(void *),
                 void *ctx, int sdp_auto, unsigned thread_queue_size);
 
 /**
- * Default size of a packet thread queue.  For muxing this can be overridden by
- * the thread_queue_size option as passed to a call to sch_add_mux().
+ * Default size of a packet thread queue.
  */
 #define DEFAULT_PACKET_THREAD_QUEUE_SIZE 2
 
+/**
+ * Default size of a muxer thread queue.  This can be overridden by the
+ * thread_queue_size option as passed to a call to sch_add_mux().
+ */
+#define DEFAULT_MUX_THREAD_QUEUE_SIZE 8
+
 /**
  * Default size of a frame thread queue.
  */
-- 
2.54.1

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to