* [FFmpeg-devel] [PATCH 1/4] avfilter/framequeue: add support for limiting and tracking buffered frames in the queues
@ 2025-07-06 22:00 Marton Balint
2025-07-06 22:00 ` [FFmpeg-devel] [PATCH 2/4] avfilter/avfilter: add AVFilterGraph->max_buffered_frames to limit buffered frames Marton Balint
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Marton Balint @ 2025-07-06 22:00 UTC (permalink / raw)
To: ffmpeg-devel; +Cc: Marton Balint
Signed-off-by: Marton Balint <cus@passwd.hu>
---
libavfilter/framequeue.c | 7 +++++++
libavfilter/framequeue.h | 18 +++++++++++++++---
2 files changed, 22 insertions(+), 3 deletions(-)
diff --git a/libavfilter/framequeue.c b/libavfilter/framequeue.c
index 79255fe532..3363406ba7 100644
--- a/libavfilter/framequeue.c
+++ b/libavfilter/framequeue.c
@@ -30,6 +30,8 @@ static inline FFFrameBucket *bucket(FFFrameQueue *fq, size_t idx)
void ff_framequeue_global_init(FFFrameQueueGlobal *fqg)
{
+ fqg->max_queued = SIZE_MAX;
+ fqg->queued = 0;
}
static void check_consistency(FFFrameQueue *fq)
@@ -49,6 +51,7 @@ void ff_framequeue_init(FFFrameQueue *fq, FFFrameQueueGlobal *fqg)
{
fq->queue = &fq->first_bucket;
fq->allocated = 1;
+ fq->global = fqg;
}
void ff_framequeue_free(FFFrameQueue *fq)
@@ -66,6 +69,8 @@ int ff_framequeue_add(FFFrameQueue *fq, AVFrame *frame)
FFFrameBucket *b;
check_consistency(fq);
+ if (fq->global->queued >= fq->global->max_queued)
+ return AVERROR(ENOMEM);
if (fq->queued == fq->allocated) {
if (fq->allocated == 1) {
size_t na = 8;
@@ -89,6 +94,7 @@ int ff_framequeue_add(FFFrameQueue *fq, AVFrame *frame)
b = bucket(fq, fq->queued);
b->frame = frame;
fq->queued++;
+ fq->global->queued++;
fq->total_frames_head++;
fq->total_samples_head += frame->nb_samples;
check_consistency(fq);
@@ -103,6 +109,7 @@ AVFrame *ff_framequeue_take(FFFrameQueue *fq)
av_assert1(fq->queued);
b = bucket(fq, 0);
fq->queued--;
+ fq->global->queued--;
fq->tail++;
fq->tail &= fq->allocated - 1;
fq->total_frames_tail++;
diff --git a/libavfilter/framequeue.h b/libavfilter/framequeue.h
index c49d872e85..405a43c65d 100644
--- a/libavfilter/framequeue.h
+++ b/libavfilter/framequeue.h
@@ -40,11 +40,18 @@ typedef struct FFFrameBucket {
*
* This structure is intended to allow implementing global control of the
* frame queues, including memory consumption caps.
- *
- * It is currently empty.
*/
typedef struct FFFrameQueueGlobal {
- char dummy; /* C does not allow empty structs */
+
+ /**
+ * Maximum number of allowed frames in the queues combined.
+ */
+ size_t max_queued;
+
+ /**
+ * Total number of queued frames in the queues combined.
+ */
+ size_t queued;
} FFFrameQueueGlobal;
/**
@@ -52,6 +59,11 @@ typedef struct FFFrameQueueGlobal {
*/
typedef struct FFFrameQueue {
+ /**
+ * Pointer to the global frame queue struct holding statistics and limits
+ */
+ FFFrameQueueGlobal *global;
+
/**
* Array of allocated buckets, used as a circular buffer.
*/
--
2.43.0
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 4+ messages in thread
* [FFmpeg-devel] [PATCH 2/4] avfilter/avfilter: add AVFilterGraph->max_buffered_frames to limit buffered frames
2025-07-06 22:00 [FFmpeg-devel] [PATCH 1/4] avfilter/framequeue: add support for limiting and tracking buffered frames in the queues Marton Balint
@ 2025-07-06 22:00 ` Marton Balint
2025-07-06 22:00 ` [FFmpeg-devel] [PATCH 3/4] fftools/ffmpeg: add support for setting maximum buffered frames in a filtergraph Marton Balint
2025-07-06 22:00 ` [FFmpeg-devel] [PATCH 4/4] tests/fate: add fate test for excessive frame buffering when using filters Marton Balint
2 siblings, 0 replies; 4+ messages in thread
From: Marton Balint @ 2025-07-06 22:00 UTC (permalink / raw)
To: ffmpeg-devel; +Cc: Marton Balint
Signed-off-by: Marton Balint <cus@passwd.hu>
---
doc/APIchanges | 3 +++
libavfilter/avfilter.h | 8 ++++++++
libavfilter/avfiltergraph.c | 4 ++++
libavfilter/version.h | 2 +-
4 files changed, 16 insertions(+), 1 deletion(-)
diff --git a/doc/APIchanges b/doc/APIchanges
index 9c03e541fd..797879f769 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -2,6 +2,9 @@ The last version increases of all libraries were on 2025-03-28
API changes, most recent first:
+2025-07-xx - xxxxxxxxxd - lavfi 11.2.100 - avfilter.h
+ Add AVFilterGraph->max_buffered_frames.
+
2025-07-01 - 39d5a998bd - lavc 62.4.101 - packet.h
Add AV_PKT_DATA_3D_REFERENCE_DISPLAYS.
diff --git a/libavfilter/avfilter.h b/libavfilter/avfilter.h
index f85929dc5c..d2571b1404 100644
--- a/libavfilter/avfilter.h
+++ b/libavfilter/avfilter.h
@@ -629,6 +629,14 @@ typedef struct AVFilterGraph {
avfilter_execute_func *execute;
char *aresample_swr_opts; ///< swr options to use for the auto-inserted aresample filters, Access ONLY through AVOptions
+
+ /**
+ * Sets the maximum number of buffered frames in the filtergraph combined.
+ *
+ * Zero means no limit. This field must be set before calling
+ * avfilter_graph_config().
+ */
+ unsigned max_buffered_frames;
} AVFilterGraph;
/**
diff --git a/libavfilter/avfiltergraph.c b/libavfilter/avfiltergraph.c
index 38b89db22a..86da0b9975 100644
--- a/libavfilter/avfiltergraph.c
+++ b/libavfilter/avfiltergraph.c
@@ -56,6 +56,8 @@ static const AVOption filtergraph_options[] = {
AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, F|V },
{"aresample_swr_opts" , "default aresample filter options" , OFFSET(aresample_swr_opts) ,
AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, F|A },
+ {"max_buffered_frames" , "maximum number of buffered frames allowed", OFFSET(max_buffered_frames),
+ AV_OPT_TYPE_UINT, {.i64 = 0}, 0, UINT_MAX, F|V|A },
{ NULL },
};
@@ -1296,6 +1298,8 @@ int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
{
int ret;
+ if (graphctx->max_buffered_frames)
+ fffiltergraph(graphctx)->frame_queues.max_queued = graphctx->max_buffered_frames;
if ((ret = graph_check_validity(graphctx, log_ctx)))
return ret;
if ((ret = graph_config_formats(graphctx, log_ctx)))
diff --git a/libavfilter/version.h b/libavfilter/version.h
index 1e884d9b44..bece922c7f 100644
--- a/libavfilter/version.h
+++ b/libavfilter/version.h
@@ -31,7 +31,7 @@
#include "version_major.h"
-#define LIBAVFILTER_VERSION_MINOR 1
+#define LIBAVFILTER_VERSION_MINOR 2
#define LIBAVFILTER_VERSION_MICRO 100
--
2.43.0
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 4+ messages in thread
* [FFmpeg-devel] [PATCH 3/4] fftools/ffmpeg: add support for setting maximum buffered frames in a filtergraph
2025-07-06 22:00 [FFmpeg-devel] [PATCH 1/4] avfilter/framequeue: add support for limiting and tracking buffered frames in the queues Marton Balint
2025-07-06 22:00 ` [FFmpeg-devel] [PATCH 2/4] avfilter/avfilter: add AVFilterGraph->max_buffered_frames to limit buffered frames Marton Balint
@ 2025-07-06 22:00 ` Marton Balint
2025-07-06 22:00 ` [FFmpeg-devel] [PATCH 4/4] tests/fate: add fate test for excessive frame buffering when using filters Marton Balint
2 siblings, 0 replies; 4+ messages in thread
From: Marton Balint @ 2025-07-06 22:00 UTC (permalink / raw)
To: ffmpeg-devel; +Cc: Marton Balint
Signed-off-by: Marton Balint <cus@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.
+@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
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 4+ messages in thread
* [FFmpeg-devel] [PATCH 4/4] tests/fate: add fate test for excessive frame buffering when using filters
2025-07-06 22:00 [FFmpeg-devel] [PATCH 1/4] avfilter/framequeue: add support for limiting and tracking buffered frames in the queues Marton Balint
2025-07-06 22:00 ` [FFmpeg-devel] [PATCH 2/4] avfilter/avfilter: add AVFilterGraph->max_buffered_frames to limit buffered frames Marton Balint
2025-07-06 22:00 ` [FFmpeg-devel] [PATCH 3/4] fftools/ffmpeg: add support for setting maximum buffered frames in a filtergraph Marton Balint
@ 2025-07-06 22:00 ` Marton Balint
2 siblings, 0 replies; 4+ messages in thread
From: Marton Balint @ 2025-07-06 22:00 UTC (permalink / raw)
To: ffmpeg-devel; +Cc: Marton Balint
Based on the command line of ticket #10959.
Signed-off-by: Marton Balint <cus@passwd.hu>
---
tests/fate/filter-video.mak | 4 ++++
tests/filtergraphs/select-buffering | 1 +
tests/ref/fate/filter-select-buffering | 30 ++++++++++++++++++++++++++
3 files changed, 35 insertions(+)
create mode 100644 tests/filtergraphs/select-buffering
create mode 100644 tests/ref/fate/filter-select-buffering
diff --git a/tests/fate/filter-video.mak b/tests/fate/filter-video.mak
index 34cf7afa83..65d4cc6c14 100644
--- a/tests/fate/filter-video.mak
+++ b/tests/fate/filter-video.mak
@@ -328,6 +328,10 @@ fate-filter-select-alternate: CMD = framecrc -c:v pgmyuv -i $(SRC) -/filter $(TA
FATE_FILTER-$(call ALLYES, FFPROBE SELECT_FILTER SMPTEBARS_FILTER LAVFI_INDEV) += fate-filter-select-ffprobe
fate-filter-select-ffprobe: CMD = probe -print_format compact -show_entries packet=stream_index,pts,pts_time -f lavfi "smptebars=d=1,select=n=2:e=1[out0][out1]"
+FATE_FILTER-$(call FILTERFRAMECRC, SMPTEBARS SELECT, LAVFI_INDEV WRAPPED_AVFRAME_ENCODER NULL_MUXER) += fate-filter-select-buffering
+fate-filter-select-buffering: tests/data/filtergraphs/select-buffering
+fate-filter-select-buffering: CMD = framecrc -filter_buffered_frames 1 -f lavfi -i "smptebars=d=21" -/filter_complex $(TARGET_PATH)/tests/data/filtergraphs/select-buffering -map "[o1]" -f null none -map "[o2]" -f null none -map "[o3]"
+
FATE_FILTER_VSYNTH_PGMYUV-$(call ALLYES, SETPTS_FILTER SETTB_FILTER) += fate-filter-setpts
fate-filter-setpts: tests/data/filtergraphs/setpts
fate-filter-setpts: CMD = framecrc -c:v pgmyuv -i $(SRC) -/filter $(TARGET_PATH)/tests/data/filtergraphs/setpts
diff --git a/tests/filtergraphs/select-buffering b/tests/filtergraphs/select-buffering
new file mode 100644
index 0000000000..38b23db896
--- /dev/null
+++ b/tests/filtergraphs/select-buffering
@@ -0,0 +1 @@
+select=n=3:e='lt(t,10)+gte(t,10)*lt(t,20)*2+gte(t,20)*3'[o1][o2][o3]
diff --git a/tests/ref/fate/filter-select-buffering b/tests/ref/fate/filter-select-buffering
new file mode 100644
index 0000000000..b9834bf4da
--- /dev/null
+++ b/tests/ref/fate/filter-select-buffering
@@ -0,0 +1,30 @@
+#tb 0: 1/25
+#media_type 0: video
+#codec_id 0: rawvideo
+#dimensions 0: 320x240
+#sar 0: 1/1
+0, 500, 500, 1, 115200, 0x87d91dc5
+0, 501, 501, 1, 115200, 0x87d91dc5
+0, 502, 502, 1, 115200, 0x87d91dc5
+0, 503, 503, 1, 115200, 0x87d91dc5
+0, 504, 504, 1, 115200, 0x87d91dc5
+0, 505, 505, 1, 115200, 0x87d91dc5
+0, 506, 506, 1, 115200, 0x87d91dc5
+0, 507, 507, 1, 115200, 0x87d91dc5
+0, 508, 508, 1, 115200, 0x87d91dc5
+0, 509, 509, 1, 115200, 0x87d91dc5
+0, 510, 510, 1, 115200, 0x87d91dc5
+0, 511, 511, 1, 115200, 0x87d91dc5
+0, 512, 512, 1, 115200, 0x87d91dc5
+0, 513, 513, 1, 115200, 0x87d91dc5
+0, 514, 514, 1, 115200, 0x87d91dc5
+0, 515, 515, 1, 115200, 0x87d91dc5
+0, 516, 516, 1, 115200, 0x87d91dc5
+0, 517, 517, 1, 115200, 0x87d91dc5
+0, 518, 518, 1, 115200, 0x87d91dc5
+0, 519, 519, 1, 115200, 0x87d91dc5
+0, 520, 520, 1, 115200, 0x87d91dc5
+0, 521, 521, 1, 115200, 0x87d91dc5
+0, 522, 522, 1, 115200, 0x87d91dc5
+0, 523, 523, 1, 115200, 0x87d91dc5
+0, 524, 524, 1, 115200, 0x87d91dc5
--
2.43.0
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2025-07-06 22:01 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-07-06 22:00 [FFmpeg-devel] [PATCH 1/4] avfilter/framequeue: add support for limiting and tracking buffered frames in the queues Marton Balint
2025-07-06 22:00 ` [FFmpeg-devel] [PATCH 2/4] avfilter/avfilter: add AVFilterGraph->max_buffered_frames to limit buffered frames Marton Balint
2025-07-06 22:00 ` [FFmpeg-devel] [PATCH 3/4] fftools/ffmpeg: add support for setting maximum buffered frames in a filtergraph Marton Balint
2025-07-06 22:00 ` [FFmpeg-devel] [PATCH 4/4] tests/fate: add fate test for excessive frame buffering when using filters Marton Balint
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
This inbox may be cloned and mirrored by anyone:
git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git
# If you have public-inbox 1.1+ installed, you may
# initialize and index your mirror using the following commands:
public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
ffmpegdev@gitmailbox.com
public-inbox-index ffmpegdev
Example config snippet for mirrors.
AGPL code for this site: git clone https://public-inbox.org/public-inbox.git