libavfilter/vf_geq.c
8eecbaf5
 /*
  * Copyright (C) 2006 Michael Niedermayer <michaelni@gmx.at>
493ebbd7
  * Copyright (C) 2012 Clément Bœsch <u pkh me>
8eecbaf5
  *
  * This file is part of FFmpeg.
  *
d5e7f010
  * 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.
8eecbaf5
  *
  * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
d5e7f010
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
8eecbaf5
  *
d5e7f010
  * 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
8eecbaf5
  */
 
 /**
  * @file
  * Generic equation change filter
  * Originally written by Michael Niedermayer for the MPlayer project, and
  * ported by Clément Bœsch for FFmpeg.
  */
 
a056636c
 #include "libavutil/avassert.h"
8eecbaf5
 #include "libavutil/avstring.h"
 #include "libavutil/eval.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "internal.h"
 
c044ac20
 #define MAX_NB_THREADS 32
47fd73ac
 #define NB_PLANES 4
 
8a0d45a9
 enum InterpolationMethods {
     INTERP_NEAREST,
     INTERP_BILINEAR,
     NB_INTERP
 };
 
206b25f9
 static const char *const var_names[] = {   "X",   "Y",   "W",   "H",   "N",   "SW",   "SH",   "T",        NULL };
 enum                                   { VAR_X, VAR_Y, VAR_W, VAR_H, VAR_N, VAR_SW, VAR_SH, VAR_T, VAR_VARS_NB };
 
ed93ed5e
 typedef struct GEQContext {
8eecbaf5
     const AVClass *class;
c044ac20
     AVExpr *e[NB_PLANES][MAX_NB_THREADS]; ///< expressions for each plane and thread
8f04220d
     char *expr_str[4+3];        ///< expression strings for each plane
a05a44e2
     AVFrame *picref;            ///< current input buffer
ac6b0bba
     uint8_t *dst;               ///< reference pointer to the 8bits output
     uint16_t *dst16;            ///< reference pointer to the 16bits output
     double values[VAR_VARS_NB]; ///< expression values
8eecbaf5
     int hsub, vsub;             ///< chroma subsampling
eac93932
     int planes;                 ///< number of planes
8a0d45a9
     int interpolation;
8f04220d
     int is_rgb;
ecc16d89
     int bps;
5c0d1f78
 
     double *pixel_sums[NB_PLANES];
     int needs_sum[NB_PLANES];
8eecbaf5
 } GEQContext;
 
a8d98377
 enum { Y = 0, U, V, A, G, B, R };
 
8eecbaf5
 #define OFFSET(x) offsetof(GEQContext, x)
 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
 
 static const AVOption geq_options[] = {
d64cbd4f
     { "lum_expr",   "set luminance expression",   OFFSET(expr_str[Y]), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
     { "lum",        "set luminance expression",   OFFSET(expr_str[Y]), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
     { "cb_expr",    "set chroma blue expression", OFFSET(expr_str[U]), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
     { "cb",         "set chroma blue expression", OFFSET(expr_str[U]), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
     { "cr_expr",    "set chroma red expression",  OFFSET(expr_str[V]), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
     { "cr",         "set chroma red expression",  OFFSET(expr_str[V]), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
     { "alpha_expr", "set alpha expression",       OFFSET(expr_str[A]), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
     { "a",          "set alpha expression",       OFFSET(expr_str[A]), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
     { "red_expr",   "set red expression",         OFFSET(expr_str[R]), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
     { "r",          "set red expression",         OFFSET(expr_str[R]), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
     { "green_expr", "set green expression",       OFFSET(expr_str[G]), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
     { "g",          "set green expression",       OFFSET(expr_str[G]), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
     { "blue_expr",  "set blue expression",        OFFSET(expr_str[B]), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
     { "b",          "set blue expression",        OFFSET(expr_str[B]), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
8a0d45a9
     { "interpolation","set interpolation method", OFFSET(interpolation), AV_OPT_TYPE_INT, {.i64=INTERP_BILINEAR}, 0, NB_INTERP-1, FLAGS, "interp" },
     { "i",          "set interpolation method",   OFFSET(interpolation), AV_OPT_TYPE_INT, {.i64=INTERP_BILINEAR}, 0, NB_INTERP-1, FLAGS, "interp" },
     { "nearest",    "nearest interpolation",      0,                   AV_OPT_TYPE_CONST, {.i64=INTERP_NEAREST},  0, 0, FLAGS, "interp" },
     { "n",          "nearest interpolation",      0,                   AV_OPT_TYPE_CONST, {.i64=INTERP_NEAREST},  0, 0, FLAGS, "interp" },
     { "bilinear",   "bilinear interpolation",     0,                   AV_OPT_TYPE_CONST, {.i64=INTERP_BILINEAR}, 0, 0, FLAGS, "interp" },
     { "b",          "bilinear interpolation",     0,                   AV_OPT_TYPE_CONST, {.i64=INTERP_BILINEAR}, 0, 0, FLAGS, "interp" },
8eecbaf5
     {NULL},
 };
 
 AVFILTER_DEFINE_CLASS(geq);
 
 static inline double getpix(void *priv, double x, double y, int plane)
 {
     int xi, yi;
     GEQContext *geq = priv;
a05a44e2
     AVFrame *picref = geq->picref;
8eecbaf5
     const uint8_t *src = picref->data[plane];
ecc16d89
     int linesize = picref->linesize[plane];
21f94684
     const int w = (plane == 1 || plane == 2) ? AV_CEIL_RSHIFT(picref->width,  geq->hsub) : picref->width;
     const int h = (plane == 1 || plane == 2) ? AV_CEIL_RSHIFT(picref->height, geq->vsub) : picref->height;
eac93932
 
     if (!src)
         return 0;
8eecbaf5
 
8a0d45a9
     if (geq->interpolation == INTERP_BILINEAR) {
036fff7e
         xi = x = av_clipd(x, 0, w - 2);
         yi = y = av_clipd(y, 0, h - 2);
8eecbaf5
 
036fff7e
         x -= xi;
         y -= yi;
8eecbaf5
 
036fff7e
         if (geq->bps > 8) {
             const uint16_t *src16 = (const uint16_t*)src;
             linesize /= 2;
ecc16d89
 
036fff7e
             return (1-y)*((1-x)*src16[xi +  yi    * linesize] + x*src16[xi + 1 +  yi    * linesize])
                   +   y *((1-x)*src16[xi + (yi+1) * linesize] + x*src16[xi + 1 + (yi+1) * linesize]);
         } else {
             return (1-y)*((1-x)*src[xi +  yi    * linesize] + x*src[xi + 1 +  yi    * linesize])
                   +   y *((1-x)*src[xi + (yi+1) * linesize] + x*src[xi + 1 + (yi+1) * linesize]);
         }
8a0d45a9
     } else {
         xi = av_clipd(x, 0, w - 1);
         yi = av_clipd(y, 0, h - 1);
 
         if (geq->bps > 8) {
             const uint16_t *src16 = (const uint16_t*)src;
             linesize /= 2;
 
             return src16[xi + yi * linesize];
         } else {
             return src[xi + yi * linesize];
         }
     }
8eecbaf5
 }
 
5c0d1f78
 static int calculate_sums(GEQContext *geq, int plane, int w, int h)
 {
     int xi, yi;
     AVFrame *picref = geq->picref;
     const uint8_t *src = picref->data[plane];
     int linesize = picref->linesize[plane];
 
     if (!geq->pixel_sums[plane])
         geq->pixel_sums[plane] = av_malloc_array(w, h * sizeof (*geq->pixel_sums[plane]));
     if (!geq->pixel_sums[plane])
         return AVERROR(ENOMEM);
     if (geq->bps > 8)
         linesize /= 2;
     for (yi = 0; yi < h; yi ++) {
         if (geq->bps > 8) {
             const uint16_t *src16 = (const uint16_t*)src;
             double linesum = 0;
 
             for (xi = 0; xi < w; xi ++) {
                 linesum += src16[xi + yi * linesize];
                 geq->pixel_sums[plane][xi + yi * w] = linesum;
             }
         } else {
             double linesum = 0;
 
             for (xi = 0; xi < w; xi ++) {
                 linesum += src[xi + yi * linesize];
                 geq->pixel_sums[plane][xi + yi * w] = linesum;
             }
         }
         if (yi)
             for (xi = 0; xi < w; xi ++) {
                 geq->pixel_sums[plane][xi + yi * w] += geq->pixel_sums[plane][xi + yi * w - w];
             }
     }
     return 0;
 }
 
 static inline double getpix_integrate_internal(GEQContext *geq, int x, int y, int plane, int w, int h)
 {
     if (x > w - 1) {
         double boundary =   getpix_integrate_internal(geq, w - 1, y, plane, w, h);
         return 2*boundary - getpix_integrate_internal(geq, 2*(w - 1) - x, y, plane, w, h);
     } else if (y > h - 1) {
         double boundary =   getpix_integrate_internal(geq, x, h - 1, plane, w, h);
         return 2*boundary - getpix_integrate_internal(geq, x, 2*(h - 1) - y, plane, w, h);
     } else if (x < 0) {
         if (x == -1) return 0;
         return - getpix_integrate_internal(geq, -x-2, y, plane, w, h);
     } else if (y < 0) {
         if (y == -1) return 0;
         return - getpix_integrate_internal(geq, x, -y-2, plane, w, h);
     }
 
     return geq->pixel_sums[plane][x + y * w];
 }
 
 static inline double getpix_integrate(void *priv, double x, double y, int plane) {
     GEQContext *geq = priv;
     AVFrame *picref = geq->picref;
     const uint8_t *src = picref->data[plane];
     const int w = (plane == 1 || plane == 2) ? AV_CEIL_RSHIFT(picref->width,  geq->hsub) : picref->width;
     const int h = (plane == 1 || plane == 2) ? AV_CEIL_RSHIFT(picref->height, geq->vsub) : picref->height;
 
     if (!src)
         return 0;
 
     return getpix_integrate_internal(geq, lrint(av_clipd(x, -w, 2*w)), lrint(av_clipd(y, -h, 2*h)), plane, w, h);
 }
 
8eecbaf5
 //TODO: cubic interpolate
 //TODO: keep the last few frames
 static double lum(void *priv, double x, double y) { return getpix(priv, x, y, 0); }
 static double  cb(void *priv, double x, double y) { return getpix(priv, x, y, 1); }
 static double  cr(void *priv, double x, double y) { return getpix(priv, x, y, 2); }
eac93932
 static double alpha(void *priv, double x, double y) { return getpix(priv, x, y, 3); }
8eecbaf5
 
5c0d1f78
 static double   lumsum(void *priv, double x, double y) { return getpix_integrate(priv, x, y, 0); }
 static double    cbsum(void *priv, double x, double y) { return getpix_integrate(priv, x, y, 1); }
 static double    crsub(void *priv, double x, double y) { return getpix_integrate(priv, x, y, 2); }
 static double alphasum(void *priv, double x, double y) { return getpix_integrate(priv, x, y, 3); }
 
fd6228e6
 static av_cold int geq_init(AVFilterContext *ctx)
8eecbaf5
 {
     GEQContext *geq = ctx->priv;
     int plane, ret = 0;
 
a8d98377
     if (!geq->expr_str[Y] && !geq->expr_str[G] && !geq->expr_str[B] && !geq->expr_str[R]) {
8f04220d
         av_log(ctx, AV_LOG_ERROR, "A luminance or RGB expression is mandatory\n");
         ret = AVERROR(EINVAL);
         goto end;
     }
a8d98377
     geq->is_rgb = !geq->expr_str[Y];
8f04220d
 
a8d98377
     if ((geq->expr_str[Y] || geq->expr_str[U] || geq->expr_str[V]) && (geq->expr_str[G] || geq->expr_str[B] || geq->expr_str[R])) {
8f04220d
         av_log(ctx, AV_LOG_ERROR, "Either YCbCr or RGB but not both must be specified\n");
8eecbaf5
         ret = AVERROR(EINVAL);
         goto end;
     }
 
a8d98377
     if (!geq->expr_str[U] && !geq->expr_str[V]) {
8eecbaf5
         /* No chroma at all: fallback on luma */
a8d98377
         geq->expr_str[U] = av_strdup(geq->expr_str[Y]);
         geq->expr_str[V] = av_strdup(geq->expr_str[Y]);
8eecbaf5
     } else {
         /* One chroma unspecified, fallback on the other */
a8d98377
         if (!geq->expr_str[U]) geq->expr_str[U] = av_strdup(geq->expr_str[V]);
         if (!geq->expr_str[V]) geq->expr_str[V] = av_strdup(geq->expr_str[U]);
8eecbaf5
     }
 
ecc16d89
     if (!geq->expr_str[A]) {
         char bps_string[8];
         snprintf(bps_string, sizeof(bps_string), "%d", (1<<geq->bps) - 1);
         geq->expr_str[A] = av_strdup(bps_string);
     }
a8d98377
     if (!geq->expr_str[G])
         geq->expr_str[G] = av_strdup("g(X,Y)");
     if (!geq->expr_str[B])
         geq->expr_str[B] = av_strdup("b(X,Y)");
     if (!geq->expr_str[R])
         geq->expr_str[R] = av_strdup("r(X,Y)");
8f04220d
 
     if (geq->is_rgb ?
a8d98377
             (!geq->expr_str[G] || !geq->expr_str[B] || !geq->expr_str[R])
8f04220d
                     :
a8d98377
             (!geq->expr_str[U] || !geq->expr_str[V] || !geq->expr_str[A])) {
8eecbaf5
         ret = AVERROR(ENOMEM);
         goto end;
     }
 
47fd73ac
     for (plane = 0; plane < NB_PLANES; plane++) {
5c0d1f78
         static double (*p[])(void *, double, double) = {
             lum   , cb   , cr   , alpha   ,
             lumsum, cbsum, crsub, alphasum,
         };
         static const char *const func2_yuv_names[]    = {
             "lum"   , "cb"   , "cr"   , "alpha"   , "p",
             "lumsum", "cbsum", "crsum", "alphasum", "psum",
             NULL };
         static const char *const func2_rgb_names[]    = {
             "g"   , "b"   , "r"   , "alpha"   , "p",
             "gsum", "bsum", "rsum", "alphasum", "psum",
             NULL };
8f04220d
         const char *const *func2_names       = geq->is_rgb ? func2_rgb_names : func2_yuv_names;
5c0d1f78
         double (*func2[])(void *, double, double) = {
             lum   , cb   , cr   , alpha   , p[plane],
             lumsum, cbsum, crsub, alphasum, p[plane + 4],
             NULL };
         int counter[10] = {0};
8eecbaf5
 
c044ac20
         for (int i = 0; i < MAX_NB_THREADS; i++) {
             ret = av_expr_parse(&geq->e[plane][i], geq->expr_str[plane < 3 && geq->is_rgb ? plane+4 : plane], var_names,
                                 NULL, NULL, func2_names, func2, 0, ctx);
             if (ret < 0)
                 goto end;
         }
5c0d1f78
 
c044ac20
         av_expr_count_func(geq->e[plane][0], counter, FF_ARRAY_ELEMS(counter), 2);
5c0d1f78
         geq->needs_sum[plane] = counter[5] + counter[6] + counter[7] + counter[8] + counter[9];
8eecbaf5
     }
 
 end:
     return ret;
 }
 
 static int geq_query_formats(AVFilterContext *ctx)
 {
8f04220d
     GEQContext *geq = ctx->priv;
0f16dfda
     static const enum AVPixelFormat yuv_pix_fmts[] = {
8eecbaf5
         AV_PIX_FMT_YUV444P,  AV_PIX_FMT_YUV422P,  AV_PIX_FMT_YUV420P,
         AV_PIX_FMT_YUV411P,  AV_PIX_FMT_YUV410P,  AV_PIX_FMT_YUV440P,
eac93932
         AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
         AV_PIX_FMT_GRAY8,
ecc16d89
         AV_PIX_FMT_YUV444P9,  AV_PIX_FMT_YUV422P9,  AV_PIX_FMT_YUV420P9,
         AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA420P9,
         AV_PIX_FMT_YUV444P10,  AV_PIX_FMT_YUV422P10,  AV_PIX_FMT_YUV420P10,
         AV_PIX_FMT_YUV440P10,
         AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA420P10,
bd6c57d5
         AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10,
ecc16d89
         AV_PIX_FMT_YUV444P12,  AV_PIX_FMT_YUV422P12,  AV_PIX_FMT_YUV420P12,
bd6c57d5
         AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14,
ecc16d89
         AV_PIX_FMT_YUV444P14,  AV_PIX_FMT_YUV422P14,  AV_PIX_FMT_YUV420P14,
         AV_PIX_FMT_YUV444P16,  AV_PIX_FMT_YUV422P16,  AV_PIX_FMT_YUV420P16,
         AV_PIX_FMT_YUVA444P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA420P16,
         AV_PIX_FMT_GRAY16,
8eecbaf5
         AV_PIX_FMT_NONE
     };
0f16dfda
     static const enum AVPixelFormat rgb_pix_fmts[] = {
1f2baec7
         AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
ecc16d89
         AV_PIX_FMT_GBRP9,
         AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRAP10,
         AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12,
         AV_PIX_FMT_GBRP14,
         AV_PIX_FMT_GBRP16, AV_PIX_FMT_GBRAP16,
8f04220d
         AV_PIX_FMT_NONE
     };
a0854c08
     AVFilterFormats *fmts_list;
 
8f04220d
     if (geq->is_rgb) {
a0854c08
         fmts_list = ff_make_format_list(rgb_pix_fmts);
8f04220d
     } else
a0854c08
         fmts_list = ff_make_format_list(yuv_pix_fmts);
     if (!fmts_list)
         return AVERROR(ENOMEM);
     return ff_set_common_formats(ctx, fmts_list);
8eecbaf5
 }
 
 static int geq_config_props(AVFilterLink *inlink)
 {
     GEQContext *geq = inlink->dst->priv;
     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
 
a056636c
     av_assert0(desc);
 
8eecbaf5
     geq->hsub = desc->log2_chroma_w;
     geq->vsub = desc->log2_chroma_h;
ac6b0bba
     geq->bps = desc->comp[0].depth;
eac93932
     geq->planes = desc->nb_components;
ac6b0bba
     return 0;
 }
 
 typedef struct ThreadData {
     int height;
     int width;
     int plane;
     int linesize;
 } ThreadData;
 
 static int slice_geq_filter(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
 {
     GEQContext *geq = ctx->priv;
     ThreadData *td = arg;
     const int height = td->height;
     const int width = td->width;
     const int plane = td->plane;
     const int linesize = td->linesize;
     const int slice_start = (height *  jobnr) / nb_jobs;
     const int slice_end = (height * (jobnr+1)) / nb_jobs;
     int x, y;
 
     double values[VAR_VARS_NB];
     values[VAR_W] = geq->values[VAR_W];
     values[VAR_H] = geq->values[VAR_H];
     values[VAR_N] = geq->values[VAR_N];
     values[VAR_SW] = geq->values[VAR_SW];
     values[VAR_SH] = geq->values[VAR_SH];
     values[VAR_T] = geq->values[VAR_T];
 
     if (geq->bps == 8) {
47d5d0cc
         uint8_t *ptr = geq->dst + linesize * slice_start;
ac6b0bba
         for (y = slice_start; y < slice_end; y++) {
             values[VAR_Y] = y;
 
             for (x = 0; x < width; x++) {
                 values[VAR_X] = x;
c044ac20
                 ptr[x] = av_expr_eval(geq->e[plane][jobnr], values, geq);
ac6b0bba
             }
             ptr += linesize;
         }
47d5d0cc
     } else {
         uint16_t *ptr16 = geq->dst16 + (linesize/2) * slice_start;
ac6b0bba
         for (y = slice_start; y < slice_end; y++) {
             values[VAR_Y] = y;
             for (x = 0; x < width; x++) {
                 values[VAR_X] = x;
c044ac20
                 ptr16[x] = av_expr_eval(geq->e[plane][jobnr], values, geq);
ac6b0bba
             }
47d5d0cc
             ptr16 += linesize/2;
ac6b0bba
         }
     }
ecc16d89
 
8eecbaf5
     return 0;
 }
 
a05a44e2
 static int geq_filter_frame(AVFilterLink *inlink, AVFrame *in)
8eecbaf5
 {
5d170b09
     int plane;
ac6b0bba
     AVFilterContext *ctx = inlink->dst;
c044ac20
     const int nb_threads = FFMIN(MAX_NB_THREADS, ff_filter_get_nb_threads(ctx));
ac6b0bba
     GEQContext *geq = ctx->priv;
8eecbaf5
     AVFilterLink *outlink = inlink->dst->outputs[0];
a05a44e2
     AVFrame *out;
ac6b0bba
 
     geq->values[VAR_N] = inlink->frame_count_out,
     geq->values[VAR_T] = in->pts == AV_NOPTS_VALUE ? NAN : in->pts * av_q2d(inlink->time_base),
8eecbaf5
 
5d170b09
     geq->picref = in;
a05a44e2
     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
5d170b09
     if (!out) {
a05a44e2
         av_frame_free(&in);
5d170b09
         return AVERROR(ENOMEM);
     }
a05a44e2
     av_frame_copy_props(out, in);
8eecbaf5
 
eac93932
     for (plane = 0; plane < geq->planes && out->data[plane]; plane++) {
e425047a
         const int width = (plane == 1 || plane == 2) ? AV_CEIL_RSHIFT(inlink->w, geq->hsub) : inlink->w;
         const int height = (plane == 1 || plane == 2) ? AV_CEIL_RSHIFT(inlink->h, geq->vsub) : inlink->h;
ac6b0bba
         const int linesize = out->linesize[plane];
         ThreadData td;
8eecbaf5
 
ac6b0bba
         geq->dst = out->data[plane];
         geq->dst16 = (uint16_t*)out->data[plane];
8eecbaf5
 
e425047a
         geq->values[VAR_W]  = width;
         geq->values[VAR_H]  = height;
         geq->values[VAR_SW] = width / (double)inlink->w;
         geq->values[VAR_SH] = height / (double)inlink->h;
ac6b0bba
 
e425047a
         td.width = width;
         td.height = height;
ac6b0bba
         td.plane = plane;
         td.linesize = linesize;
 
5c0d1f78
         if (geq->needs_sum[plane])
             calculate_sums(geq, plane, width, height);
 
e425047a
         ctx->internal->execute(ctx, slice_geq_filter, &td, NULL, FFMIN(height, nb_threads));
8eecbaf5
     }
 
a05a44e2
     av_frame_free(&geq->picref);
5d170b09
     return ff_filter_frame(outlink, out);
8eecbaf5
 }
 
 static av_cold void geq_uninit(AVFilterContext *ctx)
 {
     int i;
     GEQContext *geq = ctx->priv;
 
c044ac20
     for (i = 0; i < NB_PLANES; i++)
         for (int j = 0; j < MAX_NB_THREADS; j++)
             av_expr_free(geq->e[i][j]);
5c0d1f78
     for (i = 0; i < NB_PLANES; i++)
         av_freep(&geq->pixel_sums);
8eecbaf5
 }
 
 static const AVFilterPad geq_inputs[] = {
     {
         .name         = "default",
         .type         = AVMEDIA_TYPE_VIDEO,
         .config_props = geq_config_props,
5d170b09
         .filter_frame = geq_filter_frame,
8eecbaf5
     },
     { NULL }
 };
 
 static const AVFilterPad geq_outputs[] = {
     {
         .name = "default",
         .type = AVMEDIA_TYPE_VIDEO,
     },
     { NULL }
 };
 
325f6e0a
 AVFilter ff_vf_geq = {
8eecbaf5
     .name          = "geq",
     .description   = NULL_IF_CONFIG_SMALL("Apply generic equation to each pixel."),
     .priv_size     = sizeof(GEQContext),
     .init          = geq_init,
     .uninit        = geq_uninit,
     .query_formats = geq_query_formats,
     .inputs        = geq_inputs,
     .outputs       = geq_outputs,
     .priv_class    = &geq_class,
ac6b0bba
     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
8eecbaf5
 };