Commit 9e5cdcbefa for ffmpeg
commit 9e5cdcbefa40c620970f5aa708328ba75f757209
Author: Niklas Haas <git@haasn.dev>
Date: Fri Oct 9 17:04:30 2026 +0200
fftools/ffmpeg_filter: split -filter_buffered_frames per media type
This allows callers to (optionally) set a specific limit for audio and
video frames, separately.
Since these are not really stream options, I had to add a new parsing logic
to handle the :a and :v suffixes, but I don't think it's too ugly.
As an aside, also fixes a memleak if setting the option somehow fails.
Signed-off-by: Niklas Haas <git@haasn.dev>
diff --git a/doc/ffmpeg.texi b/doc/ffmpeg.texi
index d31678f8d8..bff7e31c7c 100644
--- a/doc/ffmpeg.texi
+++ b/doc/ffmpeg.texi
@@ -1447,7 +1447,7 @@ 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.
-@item -filter_buffered_frames @var{nb_frames} (@emph{global})
+@item -filter_buffered_frames[:@var{media_type}] @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
@@ -1456,6 +1456,11 @@ 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.
+The optional suffix @var{media_type} may be used to set a different limit
+for audio and video frames, using the suffixes @code{a} and @code{v},
+respectively. If the suffix is omitted, the limit applies to both audio and
+video frames, separately.
+
@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 f51867435d..fbf44bf65a 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -770,7 +770,7 @@ extern float max_error_rate;
extern char *filter_nbthreads;
extern int filter_complex_nbthreads;
-extern int filter_buffered_frames;
+extern int filter_buffered_frames[AVMEDIA_TYPE_NB];
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 c52371e16f..32ff664855 100644
--- a/fftools/ffmpeg_filter.c
+++ b/fftools/ffmpeg_filter.c
@@ -2231,10 +2231,13 @@ 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);
+ for (int type = 0; type < AVMEDIA_TYPE_NB; type++) {
+ const int limit = filter_buffered_frames[type];
+ if (limit <= 0)
+ continue;
+ ret = avfilter_graph_set_max_buffered_frames(fgt->graph, type, limit);
if (ret < 0)
- return ret;
+ goto fail;
}
hw_device = hw_device_for_filter();
diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c
index 265bfba35f..603e9dfbaa 100644
--- a/fftools/ffmpeg_opt.c
+++ b/fftools/ffmpeg_opt.c
@@ -72,7 +72,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 filter_buffered_frames[AVMEDIA_TYPE_NB] = {0};
int vstats_version = 2;
int print_graphs = 0;
char *print_graphs_file = NULL;
@@ -467,6 +467,37 @@ static int opt_filter_threads(void *optctx, const char *opt, const char *arg)
return 0;
}
+static int opt_filter_buffered_frames(void *optctx, const char *opt, const char *arg)
+{
+ enum AVMediaType type;
+ const char *suffix = strchr(opt, ':');
+ suffix = suffix ? suffix + 1 : NULL;
+ if (!suffix)
+ type = AVMEDIA_TYPE_UNKNOWN;
+ else if (!strcmp(suffix, "a"))
+ type = AVMEDIA_TYPE_AUDIO;
+ else if (!strcmp(suffix, "v"))
+ type = AVMEDIA_TYPE_VIDEO;
+ else {
+ av_log(NULL, AV_LOG_ERROR, "Invalid media type '%s' for option '%s', "
+ "expected 'a' or 'v'.\n", suffix, opt);
+ return AVERROR(EINVAL);
+ }
+
+ double num;
+ int ret = parse_number(opt, arg, OPT_TYPE_INT, 0, INT_MAX, &num);
+ if (ret < 0)
+ return ret;
+
+ if (type != AVMEDIA_TYPE_UNKNOWN) {
+ filter_buffered_frames[type] = num;
+ } else {
+ for (int i = 0; i < AVMEDIA_TYPE_NB; i++)
+ filter_buffered_frames[i] = num;
+ }
+ return 0;
+}
+
static int opt_abort_on(void *optctx, const char *opt, const char *arg)
{
static const AVOption opts[] = {
@@ -1746,9 +1777,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" },
+ { "filter_buffered_frames", OPT_TYPE_FUNC, OPT_FUNC_ARG | OPT_EXPERT,
+ { .func_arg = opt_filter_buffered_frames },
+ "maximum number of buffered audio/video frames in a filter graph", "nb_frames" },
{ "reinit_filter", OPT_TYPE_INT, OPT_PERSTREAM | OPT_INPUT | OPT_EXPERT,
{ .off = OFFSET(reinit_filters) },
"reinit filtergraph on input parameter changes", "" },