[FFmpeg-devel] [PATCH 3/4] fftools/ffmpeg: add support for setting maximum buffered frames in a filtergraph
Marton Balint
cus at passwd.hu
Mon Jul 7 01:00:03 EEST 2025
Signed-off-by: Marton Balint <cus at passwd.hu>
---
doc/ffmpeg.texi | 9 +++++++++
fftools/ffmpeg.h | 1 +
fftools/ffmpeg_filter.c | 6 ++++++
fftools/ffmpeg_opt.c | 4 ++++
4 files changed, 20 insertions(+)
diff --git a/doc/ffmpeg.texi b/doc/ffmpeg.texi
index 35675b5309..6dea7ce7a0 100644
--- a/doc/ffmpeg.texi
+++ b/doc/ffmpeg.texi
@@ -1384,6 +1384,15 @@ Defines how many threads are used to process a filter pipeline. Each pipeline
will produce a thread pool with this many threads available for parallel processing.
The default is the number of available CPUs.
+ at item -filter_buffered_frames @var{nb_frames} (@emph{global})
+Defines the maximum number of buffered frames allowed in a filtergraph. Under
+normal circumstances, a filtergraph should not buffer more than a few frames,
+especially if frames are being fed to it and read from it in a balanced way
+(which is the intended behavior in ffmpeg). That said, this option allows you
+to limit the total number of frames buffered across all links in a filtergraph.
+If more frames are generated, filtering is aborted and an error is returned.
+The default value is 0, which means no limit.
+
@item -pre[:@var{stream_specifier}] @var{preset_name} (@emph{output,per-stream})
Specify the preset for matching stream(s).
diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index 7868f3d85f..53f71bc080 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -737,6 +737,7 @@ extern float max_error_rate;
extern char *filter_nbthreads;
extern int filter_complex_nbthreads;
+extern int filter_buffered_frames;
extern int vstats_version;
extern int print_graphs;
extern char *print_graphs_file;
diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c
index f6e496158c..d6f9c610d6 100644
--- a/fftools/ffmpeg_filter.c
+++ b/fftools/ffmpeg_filter.c
@@ -1949,6 +1949,12 @@ static int configure_filtergraph(FilterGraph *fg, FilterGraphThread *fgt)
fgt->graph->nb_threads = filter_complex_nbthreads;
}
+ if (filter_buffered_frames) {
+ ret = av_opt_set_int(fgt->graph, "max_buffered_frames", filter_buffered_frames, 0);
+ if (ret < 0)
+ return ret;
+ }
+
hw_device = hw_device_for_filter();
ret = graph_parse(fg, fgt->graph, graph_desc, &inputs, &outputs, hw_device);
diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c
index 3d1efe32f9..d714a1523a 100644
--- a/fftools/ffmpeg_opt.c
+++ b/fftools/ffmpeg_opt.c
@@ -75,6 +75,7 @@ int stdin_interaction = 1;
float max_error_rate = 2.0/3;
char *filter_nbthreads;
int filter_complex_nbthreads = 0;
+int filter_buffered_frames = 0;
int vstats_version = 2;
int print_graphs = 0;
char *print_graphs_file = NULL;
@@ -1714,6 +1715,9 @@ const OptionDef options[] = {
{ "filter_threads", OPT_TYPE_FUNC, OPT_FUNC_ARG | OPT_EXPERT,
{ .func_arg = opt_filter_threads },
"number of non-complex filter threads" },
+ { "filter_buffered_frames", OPT_TYPE_INT, OPT_EXPERT,
+ { &filter_buffered_frames },
+ "maximum number of buffered frames in a filter graph" },
#if FFMPEG_OPT_FILTER_SCRIPT
{ "filter_script", OPT_TYPE_STRING, OPT_PERSTREAM | OPT_EXPERT | OPT_OUTPUT,
{ .off = OFFSET(filter_scripts) },
--
2.43.0
More information about the ffmpeg-devel
mailing list