Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: Niklas Haas <ffmpeg@haasn.xyz>
To: ffmpeg-devel@ffmpeg.org
Cc: Niklas Haas <git@haasn.dev>
Subject: [FFmpeg-devel] [PATCH 4/6] lavfi/vf_libplacebo: switch to activate()
Date: Wed, 10 May 2023 15:55:07 +0200
Message-ID: <20230510135509.4074-4-ffmpeg@haasn.xyz> (raw)
In-Reply-To: <20230510135509.4074-1-ffmpeg@haasn.xyz>

From: Niklas Haas <git@haasn.dev>

To present compatibility with the current behavior, we keep track of a
FIFO of exact frame timestamps that we want to output to the user. In
practice, this is essentially equivalent to the current filter_frame()
code, but this design allows us to scale to more complicated use cases
in the future - for example, insertion of intermediate frames
(deinterlacing, frame doubling, conversion to fixed fps, ...)
---
 libavfilter/vf_libplacebo.c | 113 +++++++++++++++++++++++++-----------
 1 file changed, 80 insertions(+), 33 deletions(-)

diff --git a/libavfilter/vf_libplacebo.c b/libavfilter/vf_libplacebo.c
index afb4e6a914..d8128351c8 100644
--- a/libavfilter/vf_libplacebo.c
+++ b/libavfilter/vf_libplacebo.c
@@ -18,10 +18,12 @@
 
 #include "libavutil/avassert.h"
 #include "libavutil/eval.h"
+#include "libavutil/fifo.h"
 #include "libavutil/file.h"
 #include "libavutil/opt.h"
 #include "libavutil/parseutils.h"
 #include "internal.h"
+#include "filters.h"
 #include "vulkan_filter.h"
 #include "scale_eval.h"
 
@@ -127,6 +129,9 @@ typedef struct LibplaceboContext {
     pl_queue queue;
     pl_tex tex[4];
 
+    /* filter state */
+    AVFifo *out_pts; ///< timestamps of wanted output frames
+
     /* settings */
     char *out_format_string;
     enum AVPixelFormat out_format;
@@ -458,6 +463,9 @@ static int libplacebo_init(AVFilterContext *avctx)
     RET(av_expr_parse(&s->pos_h_pexpr, s->pos_h_expr, var_names,
                       NULL, NULL, NULL, NULL, 0, s));
 
+    /* Initialize dynamic filter state */
+    s->out_pts = av_fifo_alloc2(1, sizeof(int64_t), AV_FIFO_FLAG_AUTO_GROW);
+
     /* Note: s->vulkan etc. are initialized later, when hwctx is available */
     return 0;
 
@@ -563,6 +571,7 @@ static void libplacebo_uninit(AVFilterContext *avctx)
     av_expr_free(s->pos_y_pexpr);
     av_expr_free(s->pos_w_pexpr);
     av_expr_free(s->pos_h_pexpr);
+    av_fifo_freep2(&s->out_pts);
 }
 
 static int libplacebo_process_command(AVFilterContext *ctx, const char *cmd,
@@ -760,47 +769,85 @@ static void discard_frame(const struct pl_source_frame *src)
     av_frame_free(&avframe);
 }
 
-static int filter_frame(AVFilterLink *link, AVFrame *in)
+static int libplacebo_activate(AVFilterContext *ctx)
 {
-    AVFilterContext *ctx = link->dst;
+    int ret, status;
     LibplaceboContext *s = ctx->priv;
+    AVFilterLink *inlink = ctx->inputs[0];
     AVFilterLink *outlink = ctx->outputs[0];
-    enum pl_queue_status status;
-    struct pl_frame_mix mix;
+    AVFrame *in;
+    int64_t pts;
 
+    FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
     pl_log_level_update(s->log, get_log_level());
 
-    /* Push input frame */
-    in->opaque = s;
-    pl_queue_push(s->queue, &(struct pl_source_frame) {
-        .pts         = in->pts * av_q2d(link->time_base),
-        .duration    = in->duration * av_q2d(link->time_base),
-        .first_field = pl_field_from_avframe(in),
-        .frame_data  = in,
-        .map         = map_frame,
-        .unmap       = unmap_frame,
-        .discard     = discard_frame,
-    });
-
-    /* Immediately return an output frame for the same PTS */
-    av_assert1(!av_cmp_q(link->time_base, outlink->time_base));
-    status = pl_queue_update(s->queue, &mix, pl_queue_params(
-        .pts            = in->pts * av_q2d(outlink->time_base),
-        .radius         = pl_frame_mix_radius(&s->params),
-        .vsync_duration = av_q2d(av_inv_q(outlink->frame_rate)),
-    ));
+    while ((ret = ff_inlink_consume_frame(inlink, &in)) > 0) {
+        in->opaque = s;
+        pl_queue_push(s->queue, &(struct pl_source_frame) {
+            .pts         = in->pts * av_q2d(inlink->time_base),
+            .duration    = in->duration * av_q2d(inlink->time_base),
+            .first_field = pl_field_from_avframe(in),
+            .frame_data  = in,
+            .map         = map_frame,
+            .unmap       = unmap_frame,
+            .discard     = discard_frame,
+        });
+
+        /* Internally queue an output frame for the same PTS */
+        av_assert1(!av_cmp_q(link->time_base, outlink->time_base));
+        av_fifo_write(s->out_pts, &in->pts, 1);
+    }
 
-    switch (status) {
-    case PL_QUEUE_MORE: // TODO: switch to activate() and handle properly
-    case PL_QUEUE_OK:
-        return output_frame_mix(ctx, &mix, in->pts);
-    case PL_QUEUE_EOF:
-        return 0;
-    case PL_QUEUE_ERR:
-        return AVERROR_EXTERNAL;
+    if (ret < 0)
+        return ret;
+
+    if (ff_inlink_acknowledge_status(inlink, &status, &pts)) {
+        pts = av_rescale_q_rnd(pts, inlink->time_base, outlink->time_base,
+                               AV_ROUND_UP);
+        if (status == AVERROR_EOF) {
+            /* Signal EOF to pl_queue, and enqueue this output frame to
+             * make sure we see PL_QUEUE_EOF returned eventually */
+            pl_queue_push(s->queue, NULL);
+            av_fifo_write(s->out_pts, &pts, 1);
+        } else {
+            ff_outlink_set_status(outlink, status, pts);
+            return 0;
+        }
+    }
+
+    if (ff_outlink_frame_wanted(outlink)) {
+        struct pl_frame_mix mix;
+        enum pl_queue_status ret;
+
+        if (av_fifo_peek(s->out_pts, &pts, 1, 0) < 0) {
+            ff_inlink_request_frame(inlink);
+            return 0;
+        }
+
+        ret = pl_queue_update(s->queue, &mix, pl_queue_params(
+            .pts            = pts * av_q2d(outlink->time_base),
+            .radius         = pl_frame_mix_radius(&s->params),
+            .vsync_duration = av_q2d(av_inv_q(outlink->frame_rate)),
+        ));
+
+        switch (ret) {
+        case PL_QUEUE_MORE:
+            ff_inlink_request_frame(inlink);
+            return 0;
+        case PL_QUEUE_OK:
+            av_fifo_drain2(s->out_pts, 1);
+            return output_frame_mix(ctx, &mix, pts);
+        case PL_QUEUE_EOF:
+            ff_outlink_set_status(outlink, AVERROR_EOF, pts);
+            return 0;
+        case PL_QUEUE_ERR:
+            return AVERROR_EXTERNAL;
+        }
+
+        return AVERROR_BUG;
     }
 
-    return AVERROR_BUG;
+    return FFERROR_NOT_READY;
 }
 
 static int libplacebo_query_format(AVFilterContext *ctx)
@@ -1142,7 +1189,6 @@ static const AVFilterPad libplacebo_inputs[] = {
     {
         .name         = "default",
         .type         = AVMEDIA_TYPE_VIDEO,
-        .filter_frame = &filter_frame,
         .config_props = &libplacebo_config_input,
     },
 };
@@ -1161,6 +1207,7 @@ const AVFilter ff_vf_libplacebo = {
     .priv_size      = sizeof(LibplaceboContext),
     .init           = &libplacebo_init,
     .uninit         = &libplacebo_uninit,
+    .activate       = &libplacebo_activate,
     .process_command = &libplacebo_process_command,
     FILTER_INPUTS(libplacebo_inputs),
     FILTER_OUTPUTS(libplacebo_outputs),
-- 
2.40.1

_______________________________________________
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".

  parent reply	other threads:[~2023-05-10 13:55 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-10 13:55 [FFmpeg-devel] [PATCH 1/6] lavfi/vf_libplacebo: update render params on demand Niklas Haas
2023-05-10 13:55 ` [FFmpeg-devel] [PATCH 2/6] lavfi/vf_libplacebo: split and refactor logic Niklas Haas
2023-05-10 13:55 ` [FFmpeg-devel] [PATCH 3/6] lavfi/vf_libplacebo: switch to pl_queue-based design Niklas Haas
2023-05-10 13:55 ` Niklas Haas [this message]
2023-05-10 13:55 ` [FFmpeg-devel] [PATCH 5/6] lavfi/vf_libplacebo: allow fps conversion Niklas Haas
2023-05-10 13:55 ` [FFmpeg-devel] [PATCH 6/6] lavfi/vf_libplacebo: add frame_mixer option Niklas Haas
2023-05-14  9:22 ` [FFmpeg-devel] [PATCH 1/6] lavfi/vf_libplacebo: update render params on demand Niklas Haas

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230510135509.4074-4-ffmpeg@haasn.xyz \
    --to=ffmpeg@haasn.xyz \
    --cc=ffmpeg-devel@ffmpeg.org \
    --cc=git@haasn.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

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