[FFmpeg-devel] [PATCH v4 1/1] avfilter/buffersink: Add video frame allocation callback

John Cox jc at kynesim.co.uk
Tue Jul 25 14:14:17 EEST 2023


Add a callback to enable user allocation of video frames on the final
stage of a filter chain.

Signed-off-by: John Cox <jc at kynesim.co.uk>
---
 libavfilter/buffersink.c | 38 ++++++++++++++++++++++++++++++
 libavfilter/buffersink.h | 51 ++++++++++++++++++++++++++++++++++++++++
 libavfilter/version.h    |  2 +-
 3 files changed, 90 insertions(+), 1 deletion(-)

diff --git a/libavfilter/buffersink.c b/libavfilter/buffersink.c
index 306c283f77..93f78f6db8 100644
--- a/libavfilter/buffersink.c
+++ b/libavfilter/buffersink.c
@@ -62,6 +62,12 @@ typedef struct BufferSinkContext {
     int sample_rates_size;
 
     AVFrame *peeked_frame;
+
+    union {
+        AVBuffersinkAllocVideoFrameFunc video;
+        AVBuffersinkAllocAudioFrameFunc audio;
+    } alloc_cb;
+    void *alloc_v;
 } BufferSinkContext;
 
 #define NB_ITEMS(list) (list ## _size / sizeof(*list))
@@ -154,6 +160,36 @@ int attribute_align_arg av_buffersink_get_samples(AVFilterContext *ctx,
     return get_frame_internal(ctx, frame, 0, nb_samples);
 }
 
+static AVFrame *alloc_audio_buffer(AVFilterLink *link, int nb_samples)
+{
+    AVFilterContext *const ctx = link->dst;
+    BufferSinkContext *const buf = ctx->priv;
+    return buf->alloc_cb.audio ? buf->alloc_cb.audio(ctx, buf->alloc_v, nb_samples) :
+                                 ff_default_get_audio_buffer(link, nb_samples);
+}
+
+void av_buffersink_set_alloc_audio_frame(AVFilterContext *ctx, AVBuffersinkAllocAudioFrameFunc cb, void *v)
+{
+    BufferSinkContext *const buf = ctx->priv;
+    buf->alloc_cb.audio = cb;
+    buf->alloc_v = v;
+}
+
+static AVFrame *alloc_video_buffer(AVFilterLink *link, int w, int h)
+{
+    AVFilterContext *const ctx = link->dst;
+    BufferSinkContext *const buf = ctx->priv;
+    return buf->alloc_cb.video ? buf->alloc_cb.video(ctx, buf->alloc_v, w, h) :
+                                 ff_default_get_video_buffer(link, w, h);
+}
+
+void av_buffersink_set_alloc_video_frame(AVFilterContext *ctx, AVBuffersinkAllocVideoFrameFunc cb, void *v)
+{
+    BufferSinkContext *const buf = ctx->priv;
+    buf->alloc_cb.video = cb;
+    buf->alloc_v = v;
+}
+
 static av_cold int common_init(AVFilterContext *ctx)
 {
     BufferSinkContext *buf = ctx->priv;
@@ -381,6 +417,7 @@ static const AVFilterPad avfilter_vsink_buffer_inputs[] = {
     {
         .name = "default",
         .type = AVMEDIA_TYPE_VIDEO,
+        .get_buffer = {.video = alloc_video_buffer},
     },
 };
 
@@ -400,6 +437,7 @@ static const AVFilterPad avfilter_asink_abuffer_inputs[] = {
     {
         .name = "default",
         .type = AVMEDIA_TYPE_AUDIO,
+        .get_buffer = {.audio = alloc_audio_buffer},
     },
 };
 
diff --git a/libavfilter/buffersink.h b/libavfilter/buffersink.h
index 64e08de53e..72ee755688 100644
--- a/libavfilter/buffersink.h
+++ b/libavfilter/buffersink.h
@@ -166,6 +166,57 @@ int av_buffersink_get_frame(AVFilterContext *ctx, AVFrame *frame);
  */
 int av_buffersink_get_samples(AVFilterContext *ctx, AVFrame *frame, int nb_samples);
 
+/**
+ * Callback from ::av_buffersink_set_alloc_audio_frame to
+ * allocate a frame 
+ *
+ * @param ctx pointer to a context of the abuffersink AVFilter.
+ * @param v opaque pointer passed to
+ *          ::av_buffersink_set_alloc_audio_frame
+ * @param nb_samples number of samples to allocate in the frame
+ *
+ * @retval AVFrame* The newly allocated frame
+ * @retval NULL     Error allocating the frame
+ */
+typedef AVFrame *(*AVBuffersinkAllocAudioFrameFunc)(AVFilterContext *ctx, void *v, int nb_samples);
+
+/**
+ * Set an audio frame allocation method for abuffersink
+ *
+ * @param ctx pointer to a context of the abuffersink AVFilter.
+ * @param cb Callback to the allocation function. If set to NULL
+ *           then the default avfilter allocation function will
+ *           be used.
+ * @param v  Opaque to pass to the allocation function
+ */
+void av_buffersink_set_alloc_audio_frame(AVFilterContext *ctx, AVBuffersinkAllocAudioFrameFunc cb, void *v);
+
+/**
+ * Callback from ::av_buffersink_set_alloc_video_frame to
+ * allocate a frame 
+ *  
+ * @param ctx pointer to a context of the buffersink AVFilter.
+ * @param v opaque pointer passed to 
+ *          ::av_buffersink_set_alloc_video_frame
+ * @param w width of frame to allocate 
+ * @param h height of frame to allocate
+ *  
+ * @retval AVFrame* The newly allocated frame
+ * @retval NULL     Error allocating the frame
+ */
+typedef AVFrame *(*AVBuffersinkAllocVideoFrameFunc)(AVFilterContext *ctx, void *v, int w, int h);
+
+/**
+ * Set a video frame allocation method for buffersink
+ *
+ * @param ctx pointer to a context of the buffersink AVFilter.
+ * @param cb Callback to the allocation function. If set to NULL 
+ *           then the default avfilter allocation function will
+ *           be used.
+ * @param v  Opaque to pass to the allocation function
+ */
+void av_buffersink_set_alloc_video_frame(AVFilterContext *ctx, AVBuffersinkAllocVideoFrameFunc cb, void *v);
+
 /**
  * @}
  */
diff --git a/libavfilter/version.h b/libavfilter/version.h
index 4a69d6be98..8f4a7a9cd3 100644
--- a/libavfilter/version.h
+++ b/libavfilter/version.h
@@ -31,7 +31,7 @@
 
 #include "version_major.h"
 
-#define LIBAVFILTER_VERSION_MINOR  10
+#define LIBAVFILTER_VERSION_MINOR  11
 #define LIBAVFILTER_VERSION_MICRO 100
 
 
-- 
2.39.2



More information about the ffmpeg-devel mailing list