The main reason behind this change is adding floating point precision to 
scale/crop to the zoompan filter. However the current zoompan filter have a few 
things that don’t fit my use case:

- output aspect ratio is forced to be the same as the input aspect ratio
- output size can't be dynamic
- PTS is rewritten

I've tried updating the current zoompan filter but couldn't do something that 
wouldn't break things for the users.

Therefore I ended up creating this new filter.

Signed-off-by: Quentin Renard <contact-git...@asticode.com>
---
 Changelog                |   1 +
 doc/filters.texi         |  59 ++++++++++
 libavfilter/Makefile     |   1 +
 libavfilter/allfilters.c |   1 +
 libavfilter/vf_yazf.c    | 234 +++++++++++++++++++++++++++++++++++++++
 5 files changed, 296 insertions(+)
 create mode 100644 libavfilter/vf_yazf.c

diff --git a/Changelog b/Changelog
index 4217449438..632d0e03d1 100644
--- a/Changelog
+++ b/Changelog
@@ -18,6 +18,7 @@ version <next>:
 - APV encoding support through a libopenapv wrapper
 - VVC decoder supports all content of SCC (Screen Content Coding):
   IBC (Inter Block Copy), Palette Mode and ACT (Adaptive Color Transform
+- yazf filter
 
 
 version 7.1:
diff --git a/doc/filters.texi b/doc/filters.texi
index 63f55f5794..94b204b7f8 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -25986,6 +25986,65 @@ Set blur strength. Default value is 128.
 @subsection Commands
 This filter supports same @ref{commands} as options.
 
+@section yazf
+
+Apply Zoom & Pan effect with floating point precision, without rewriting PTS, 
with possible dynamic output size all the while respecting the output size 
aspect ratio ("yazf" means "yet another zoompan filter").
+
+This filter accepts the following options:
+
+@table @option
+@item z
+Set the zoom expression. Range is 1-10. Default is 1.
+
+@item x
+@item y
+Set the x and y expression. Default is 0.
+
+@item w
+@item h
+Set the output frame width and height expression. Default is 1.
+@end table
+
+Each expression can contain the following constants:
+
+@table @option
+@item in_w, iw
+Input width.
+
+@item in_h, ih
+Input height.
+
+@item n
+Input frame count.
+
+@item t
+The input timestamp expressed in seconds.
+@end table
+
+Additionally x and y expressions can contain the following constants:
+
+@table @option
+@item z
+Last calculated zoom from 'z' expression for current input frame.
+
+@item zw
+Last calculated zoom width for current input frame.
+
+@item zh
+Last calculated zoom height for current input frame.
+@end table
+
+@subsection Examples
+
+@itemize
+@item
+Zoom in 2x into center of picture for first 30 frames:
+@example
+yazf=x='(iw/2)-(zw/2)':y='(ih/2)-(zh/2)':z='min(2, 1+n*/30)':w=1080:h=1080"
+@end example
+
+@end itemize
+
 @section zoompan
 
 Apply Zoom & Pan effect.
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 7c6dfcf4bf..205704014d 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -582,6 +582,7 @@ OBJS-$(CONFIG_YADIF_VIDEOTOOLBOX_FILTER)     += 
vf_yadif_videotoolbox.o \
                                                 metal/utils.o \
                                                 yadif_common.o
 OBJS-$(CONFIG_YAEPBLUR_FILTER)               += vf_yaepblur.o
+OBJS-$(CONFIG_YAZF_FILTER)                   += vf_yazf.o perspective.o
 OBJS-$(CONFIG_ZMQ_FILTER)                    += f_zmq.o
 OBJS-$(CONFIG_ZOOMPAN_FILTER)                += vf_zoompan.o
 OBJS-$(CONFIG_ZSCALE_FILTER)                 += vf_zscale.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 3bc045b28f..5fc80303f5 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -544,6 +544,7 @@ extern const FFFilter ff_vf_yadif;
 extern const FFFilter ff_vf_yadif_cuda;
 extern const FFFilter ff_vf_yadif_videotoolbox;
 extern const FFFilter ff_vf_yaepblur;
+extern const FFFilter ff_vf_yazf;
 extern const FFFilter ff_vf_zmq;
 extern const FFFilter ff_vf_zoompan;
 extern const FFFilter ff_vf_zscale;
diff --git a/libavfilter/vf_yazf.c b/libavfilter/vf_yazf.c
new file mode 100644
index 0000000000..798ba1370a
--- /dev/null
+++ b/libavfilter/vf_yazf.c
@@ -0,0 +1,234 @@
+/*
+ * Copyright (c) 2025 Quentin Renard
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "libavutil/eval.h"
+#include "libavutil/opt.h"
+#include "perspective.h"
+#include "video.h"
+
+static const char *const var_names[] = {
+    "in_w",   "iw",
+    "in_h",   "ih",
+    "z",
+    "zw",
+    "zh",
+    "n",
+    "t",
+    NULL
+};
+
+enum var_name {
+    VAR_IN_W,   VAR_IW,
+    VAR_IN_H,   VAR_IH,
+    VAR_Z,
+    VAR_ZW,
+    VAR_ZH,
+    VAR_N,
+    VAR_T,
+    VARS_NB
+};
+
+typedef struct YAZFContext {
+    const AVClass *class;
+    char *x_expr_str, *y_expr_str, *w_expr_str, *h_expr_str, *zoom_expr_str;
+    AVExpr *x_expr, *y_expr, *w_expr, *h_expr, *zoom_expr;
+    double var_values[VARS_NB];
+    PerspectiveResampleContext *r;
+} YAZFContext;
+
+static av_cold int init(AVFilterContext *ctx)
+{
+    YAZFContext *s = ctx->priv;
+    int ret;
+
+    s->r = 
perspective_resample_context_alloc(PERSPECTIVE_RESAMPLE_INTERPOLATION_LINEAR, 
PERSPECTIVE_RESAMPLE_SENSE_SOURCE);
+    if (!s->r)
+        return AVERROR(ENOMEM);
+
+    ret = av_expr_parse(&s->x_expr, s->x_expr_str, var_names, NULL, NULL, 
NULL, NULL, 0, ctx);
+    if (ret < 0)
+        return ret;
+
+    ret = av_expr_parse(&s->y_expr, s->y_expr_str, var_names, NULL, NULL, 
NULL, NULL, 0, ctx);
+    if (ret < 0)
+        return ret;
+
+    ret = av_expr_parse(&s->w_expr, s->w_expr_str, var_names, NULL, NULL, 
NULL, NULL, 0, ctx);
+    if (ret < 0)
+        return ret;
+
+    ret = av_expr_parse(&s->h_expr, s->h_expr_str, var_names, NULL, NULL, 
NULL, NULL, 0, ctx);
+    if (ret < 0)
+        return ret;
+
+    ret = av_expr_parse(&s->zoom_expr, s->zoom_expr_str, var_names, NULL, 
NULL, NULL, NULL, 0, ctx);
+    if (ret < 0)
+        return ret;
+
+    return 0;
+}
+
+static int config_outlink(AVFilterLink *outlink)
+{
+    AVFilterContext *ctx = outlink->src;
+    AVFilterLink *inlink = ctx->inputs[0];
+    YAZFContext *s = ctx->priv;
+
+    s->var_values[VAR_IN_W] = s->var_values[VAR_IW] = inlink->w;
+    s->var_values[VAR_IN_H] = s->var_values[VAR_IH] = inlink->h;
+
+    outlink->w = FFMAX(av_expr_eval(s->w_expr, s->var_values, NULL), 1);
+    outlink->h = FFMAX(av_expr_eval(s->h_expr, s->var_values, NULL), 1);
+    return 0;
+}
+
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
+{
+    AVFilterContext *ctx = inlink->dst;
+    YAZFContext *s = ctx->priv;
+    AVFilterLink *outlink = ctx->outputs[0];
+    int ret;
+    AVFrame *out = NULL;
+    float zoom, a, crop_x, crop_y, crop_w, crop_h;
+    double ref[4][2];
+
+    inlink->w = in->width;
+    inlink->h = in->height;
+    s->var_values[VAR_N] = ff_filter_link(inlink)->frame_count_out;
+    s->var_values[VAR_T] = TS2T(in->pts, inlink->time_base);
+
+    if ((ret = config_outlink(outlink)) < 0)
+        goto err;
+
+    a = (float)outlink->w / (float)outlink->h;
+    
+    s->var_values[VAR_Z] = zoom = av_clipd(av_expr_eval(s->zoom_expr, 
s->var_values, NULL), 1, 10);
+    
+    crop_w = (float)inlink->w / zoom;
+    crop_h = crop_w / a;
+    if (crop_h > inlink->h) {
+        crop_h = inlink->h;
+        crop_w = crop_h * a;
+    }
+    s->var_values[VAR_ZW] = crop_w;
+    s->var_values[VAR_ZH] = crop_h;
+    
+    crop_x = av_clipd(av_expr_eval(s->x_expr, s->var_values, NULL), 0, 
FFMAX(inlink->w - crop_w, 0));
+    crop_y = av_clipd(av_expr_eval(s->y_expr, s->var_values, NULL), 0, 
FFMAX(inlink->h - crop_h, 0));
+
+    ref[0][0] = crop_x;
+    ref[0][1] = crop_y;
+    ref[1][0] = crop_x + crop_w;
+    ref[1][1] = crop_y;
+    ref[2][0] = crop_x;
+    ref[2][1] = crop_y + crop_h;
+    ref[3][0] = crop_x + crop_w;
+    ref[3][1] = crop_y + crop_h;
+
+    out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
+    if (!out) {
+        ret = AVERROR(ENOMEM);
+        goto err;
+    }
+
+    if ((ret = av_frame_copy_props(out, in)) < 0)
+        goto err;
+
+    if ((ret = perspective_resample_config_props(s->r, out->width, 
out->height, out->format, ref)) < 0) {
+        goto err;
+    }
+
+    perspective_resample(s->r, ctx, in, out);
+
+    av_frame_free(&in);
+    return ff_filter_frame(outlink, out);
+    
+err:
+    av_frame_free(&in);
+    av_frame_free(&out);
+    return ret;
+}
+
+static const enum AVPixelFormat pix_fmts[] = {
+    AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
+    AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
+    AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
+    AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUVJ420P,
+    AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ440P,
+    AV_PIX_FMT_YUVJ444P,
+    AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P,
+    AV_PIX_FMT_YUVA444P,
+    AV_PIX_FMT_NONE
+};
+
+static av_cold void uninit(AVFilterContext *ctx)
+{
+    YAZFContext *s = ctx->priv;
+
+    perspective_resample_context_free(&s->r);
+    av_expr_free(s->x_expr);
+    av_expr_free(s->y_expr);
+    av_expr_free(s->zoom_expr);
+    av_expr_free(s->w_expr);
+    av_expr_free(s->h_expr);
+}
+
+#define OFFSET(x) offsetof(YAZFContext, x)
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
+
+static const AVOption yazf_options[] = {
+    { "z", "set the zoom expression", OFFSET(zoom_expr_str), 
AV_OPT_TYPE_STRING, {.str = "1" }, .flags = FLAGS },
+    { "x", "set the zoom x expression", OFFSET(x_expr_str), 
AV_OPT_TYPE_STRING, {.str = "0" }, .flags = FLAGS },
+    { "y", "set the zoom y expression", OFFSET(y_expr_str), 
AV_OPT_TYPE_STRING, {.str = "0" }, .flags = FLAGS },
+    { "w", "set the output w expression", OFFSET(w_expr_str), 
AV_OPT_TYPE_STRING, {.str = "1" }, .flags = FLAGS },
+    { "h", "set the output h expression", OFFSET(h_expr_str), 
AV_OPT_TYPE_STRING, {.str = "1" }, .flags = FLAGS },
+    { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(yazf);
+
+static const AVFilterPad avfilter_vf_yazf_inputs[] = {
+    {
+        .name         = "default",
+        .type         = AVMEDIA_TYPE_VIDEO,
+        .filter_frame = filter_frame,
+    },
+};
+
+static const AVFilterPad avfilter_vf_yazf_outputs[] = {
+    {
+        .name         = "default",
+        .type         = AVMEDIA_TYPE_VIDEO,
+        .config_props = config_outlink,
+    },
+};
+
+const FFFilter ff_vf_yazf = {
+    .p.name          = "yazf",
+    .p.description   = NULL_IF_CONFIG_SMALL("Apply Zoom & Pan effect with 
floating point precision."),
+    .p.priv_class    = &yazf_class,
+    .p.flags         = AVFILTER_FLAG_SLICE_THREADS,
+    .init            = init,
+    .priv_size       = sizeof(YAZFContext),
+    .uninit          = uninit,
+    FILTER_INPUTS(avfilter_vf_yazf_inputs),
+    FILTER_OUTPUTS(avfilter_vf_yazf_outputs),
+    FILTER_PIXFMTS_ARRAY(pix_fmts),
+};
-- 
2.39.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-requ...@ffmpeg.org with subject "unsubscribe".

Reply via email to