[FFmpeg-devel] [WIP] [PATCH 1/2] lavfi/framesync: support filters with multiple outputs
Nicolas George
george at nsup.org
Mon Jun 26 23:09:31 EEST 2023
The filters will have to provide the logic to set the status
and check for a wanted frame.
Signed-off-by: Nicolas George <george at nsup.org>
---
libavfilter/framesync.c | 46 ++++++++++++++++++++++++++++++-----------
libavfilter/framesync.h | 28 +++++++++++++++++++++++++
2 files changed, 62 insertions(+), 12 deletions(-)
Untested yet.
diff --git a/libavfilter/framesync.c b/libavfilter/framesync.c
index c748262ba6..0923e8c22b 100644
--- a/libavfilter/framesync.c
+++ b/libavfilter/framesync.c
@@ -75,6 +75,10 @@ enum {
static int consume_from_fifos(FFFrameSync *fs);
+static void default_on_eof(FFFrameSync *fs);
+
+static int default_on_miss(FFFrameSync *fs);
+
void ff_framesync_preinit(FFFrameSync *fs)
{
if (fs->class)
@@ -83,28 +87,36 @@ void ff_framesync_preinit(FFFrameSync *fs)
av_opt_set_defaults(fs);
}
-int ff_framesync_init(FFFrameSync *fs, AVFilterContext *parent, unsigned nb_in)
+int ff_framesync_postinit(FFFrameSync *fs)
{
- /* For filters with several outputs, we will not be able to assume which
- output is relevant for ff_outlink_frame_wanted() and
- ff_outlink_set_status(). To be designed when needed. */
- av_assert0(parent->nb_outputs == 1);
-
- ff_framesync_preinit(fs);
- fs->parent = parent;
- fs->nb_in = nb_in;
+ if (fs->parent->nb_outputs == 1) {
+ if (!fs->on_eof)
+ fs->on_eof = default_on_eof;
+ if (!fs->on_miss)
+ fs->on_miss = default_on_miss;
+ }
+ av_assert0(fs->on_eof);
+ av_assert0(fs->on_miss);
- fs->in = av_calloc(nb_in, sizeof(*fs->in));
+ fs->in = av_calloc(fs->nb_in, sizeof(*fs->in));
if (!fs->in)
return AVERROR(ENOMEM);
return 0;
}
+int ff_framesync_init(FFFrameSync *fs, AVFilterContext *parent, unsigned nb_in)
+{
+ ff_framesync_preinit(fs);
+ fs->parent = parent;
+ fs->nb_in = nb_in;
+ return ff_framesync_postinit(fs);
+}
+
static void framesync_eof(FFFrameSync *fs)
{
fs->eof = 1;
fs->frame_ready = 0;
- ff_outlink_set_status(fs->parent->outputs[0], AVERROR_EOF, AV_NOPTS_VALUE);
+ fs->on_eof(fs);
}
static void framesync_sync_level_update(FFFrameSync *fs)
@@ -342,7 +354,7 @@ static int consume_from_fifos(FFFrameSync *fs)
}
}
if (nb_miss) {
- if (nb_miss == nb_active && !ff_outlink_frame_wanted(ctx->outputs[0]))
+ if (nb_miss == nb_active && !fs->on_miss(fs))
return FFERROR_NOT_READY;
for (i = 0; i < fs->nb_in; i++)
if (!fs->in[i].have_next && fs->in[i].state != STATE_EOF)
@@ -422,3 +434,13 @@ int ff_framesync_dualinput_get_writable(FFFrameSync *fs, AVFrame **f0, AVFrame *
}
return 0;
}
+
+static void default_on_eof(FFFrameSync *fs)
+{
+ ff_outlink_set_status(fs->parent->outputs[0], AVERROR_EOF, AV_NOPTS_VALUE);
+}
+
+static int default_on_miss(FFFrameSync *fs)
+{
+ return ff_outlink_frame_wanted(fs->parent->outputs[0]);
+}
diff --git a/libavfilter/framesync.h b/libavfilter/framesync.h
index 233f50a0eb..1957396c7f 100644
--- a/libavfilter/framesync.h
+++ b/libavfilter/framesync.h
@@ -193,6 +193,19 @@ typedef struct FFFrameSync {
*/
int (*on_event)(struct FFFrameSync *fs);
+ /**
+ * Callback called when EOF is reached; can be NULL
+ * The default is to ff_outlink_set_status() on the single output.
+ */
+ void (*on_eof)(struct FFFrameSync *fs);
+
+ /**
+ * Callback called when no input can be produced to decide if input must
+ * be requested
+ * The default is ff_outlink_frame_wanted() on the singe output.
+ */
+ int (*on_miss)(struct FFFrameSync *fs);
+
/**
* Opaque pointer, not used by the API
*/
@@ -240,6 +253,21 @@ typedef struct FFFrameSync {
*/
void ff_framesync_preinit(FFFrameSync *fs);
+/**
+ * Finish the initialization of a frame sync structure
+ *
+ * Use it in combination with ff_framesync_preinit() and set fields and
+ * options in between.
+ *
+ * ff_framesync_preinit(fs);
+ * fs->parent = parent;
+ * fs->nb_in = nb_in;
+ * fs->field = value;
+ * ret = ff_framesync_postinit(fs);
+ * if (ret < 0) ...
+ */
+int ff_framesync_postinit(FFFrameSync *fs);
+
/**
* Initialize a frame sync structure.
*
--
2.39.2
More information about the ffmpeg-devel
mailing list