• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2006 Michael Niedermayer <michaelni@gmx.at>
3  * Copyright (C) 2012 Clément Bœsch <u pkh me>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * Generic equation change filter
25  * Originally written by Michael Niedermayer for the MPlayer project, and
26  * ported by Clément Bœsch for FFmpeg.
27  */
28 
29 #include "libavutil/avassert.h"
30 #include "libavutil/avstring.h"
31 #include "libavutil/eval.h"
32 #include "libavutil/opt.h"
33 #include "libavutil/pixdesc.h"
34 #include "internal.h"
35 
36 #define MAX_NB_THREADS 32
37 #define NB_PLANES 4
38 
39 enum InterpolationMethods {
40     INTERP_NEAREST,
41     INTERP_BILINEAR,
42     NB_INTERP
43 };
44 
45 static const char *const var_names[] = {   "X",   "Y",   "W",   "H",   "N",   "SW",   "SH",   "T",        NULL };
46 enum                                   { VAR_X, VAR_Y, VAR_W, VAR_H, VAR_N, VAR_SW, VAR_SH, VAR_T, VAR_VARS_NB };
47 
48 typedef struct GEQContext {
49     const AVClass *class;
50     AVExpr *e[NB_PLANES][MAX_NB_THREADS]; ///< expressions for each plane and thread
51     char *expr_str[4+3];        ///< expression strings for each plane
52     AVFrame *picref;            ///< current input buffer
53     uint8_t *dst;               ///< reference pointer to the 8bits output
54     uint16_t *dst16;            ///< reference pointer to the 16bits output
55     float *dst32;               ///< reference pointer to the 32bits output
56     double values[VAR_VARS_NB]; ///< expression values
57     int hsub, vsub;             ///< chroma subsampling
58     int planes;                 ///< number of planes
59     int interpolation;
60     int is_rgb;
61     int bps;
62 
63     double *pixel_sums[NB_PLANES];
64     int needs_sum[NB_PLANES];
65 } GEQContext;
66 
67 enum { Y = 0, U, V, A, G, B, R };
68 
69 #define OFFSET(x) offsetof(GEQContext, x)
70 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
71 
72 static const AVOption geq_options[] = {
73     { "lum_expr",   "set luminance expression",   OFFSET(expr_str[Y]), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
74     { "lum",        "set luminance expression",   OFFSET(expr_str[Y]), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
75     { "cb_expr",    "set chroma blue expression", OFFSET(expr_str[U]), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
76     { "cb",         "set chroma blue expression", OFFSET(expr_str[U]), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
77     { "cr_expr",    "set chroma red expression",  OFFSET(expr_str[V]), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
78     { "cr",         "set chroma red expression",  OFFSET(expr_str[V]), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
79     { "alpha_expr", "set alpha expression",       OFFSET(expr_str[A]), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
80     { "a",          "set alpha expression",       OFFSET(expr_str[A]), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
81     { "red_expr",   "set red expression",         OFFSET(expr_str[R]), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
82     { "r",          "set red expression",         OFFSET(expr_str[R]), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
83     { "green_expr", "set green expression",       OFFSET(expr_str[G]), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
84     { "g",          "set green expression",       OFFSET(expr_str[G]), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
85     { "blue_expr",  "set blue expression",        OFFSET(expr_str[B]), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
86     { "b",          "set blue expression",        OFFSET(expr_str[B]), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
87     { "interpolation","set interpolation method", OFFSET(interpolation), AV_OPT_TYPE_INT, {.i64=INTERP_BILINEAR}, 0, NB_INTERP-1, FLAGS, "interp" },
88     { "i",          "set interpolation method",   OFFSET(interpolation), AV_OPT_TYPE_INT, {.i64=INTERP_BILINEAR}, 0, NB_INTERP-1, FLAGS, "interp" },
89     { "nearest",    "nearest interpolation",      0,                   AV_OPT_TYPE_CONST, {.i64=INTERP_NEAREST},  0, 0, FLAGS, "interp" },
90     { "n",          "nearest interpolation",      0,                   AV_OPT_TYPE_CONST, {.i64=INTERP_NEAREST},  0, 0, FLAGS, "interp" },
91     { "bilinear",   "bilinear interpolation",     0,                   AV_OPT_TYPE_CONST, {.i64=INTERP_BILINEAR}, 0, 0, FLAGS, "interp" },
92     { "b",          "bilinear interpolation",     0,                   AV_OPT_TYPE_CONST, {.i64=INTERP_BILINEAR}, 0, 0, FLAGS, "interp" },
93     {NULL},
94 };
95 
96 AVFILTER_DEFINE_CLASS(geq);
97 
getpix(void * priv,double x,double y,int plane)98 static inline double getpix(void *priv, double x, double y, int plane)
99 {
100     int xi, yi;
101     GEQContext *geq = priv;
102     AVFrame *picref = geq->picref;
103     const uint8_t *src = picref->data[plane];
104     int linesize = picref->linesize[plane];
105     const int w = (plane == 1 || plane == 2) ? AV_CEIL_RSHIFT(picref->width,  geq->hsub) : picref->width;
106     const int h = (plane == 1 || plane == 2) ? AV_CEIL_RSHIFT(picref->height, geq->vsub) : picref->height;
107 
108     if (!src)
109         return 0;
110 
111     if (geq->interpolation == INTERP_BILINEAR) {
112         xi = x = av_clipd(x, 0, w - 2);
113         yi = y = av_clipd(y, 0, h - 2);
114 
115         x -= xi;
116         y -= yi;
117 
118         if (geq->bps > 8 && geq->bps <= 16) {
119             const uint16_t *src16 = (const uint16_t*)src;
120             linesize /= 2;
121 
122             return (1-y)*((1-x)*src16[xi +  yi    * linesize] + x*src16[xi + 1 +  yi    * linesize])
123                   +   y *((1-x)*src16[xi + (yi+1) * linesize] + x*src16[xi + 1 + (yi+1) * linesize]);
124         } else if (geq->bps == 32) {
125             const float *src32 = (const float*)src;
126             linesize /= 4;
127 
128             return (1-y)*((1-x)*src32[xi +  yi    * linesize] + x*src32[xi + 1 +  yi    * linesize])
129                   +   y *((1-x)*src32[xi + (yi+1) * linesize] + x*src32[xi + 1 + (yi+1) * linesize]);
130         } else if (geq->bps == 8) {
131             return (1-y)*((1-x)*src[xi +  yi    * linesize] + x*src[xi + 1 +  yi    * linesize])
132                   +   y *((1-x)*src[xi + (yi+1) * linesize] + x*src[xi + 1 + (yi+1) * linesize]);
133         }
134     } else {
135         xi = av_clipd(x, 0, w - 1);
136         yi = av_clipd(y, 0, h - 1);
137 
138         if (geq->bps > 8 && geq->bps <= 16) {
139             const uint16_t *src16 = (const uint16_t*)src;
140             linesize /= 2;
141 
142             return src16[xi + yi * linesize];
143         } else if (geq->bps == 32) {
144             const float *src32 = (const float*)src;
145             linesize /= 4;
146 
147             return src32[xi + yi * linesize];
148         } else if (geq->bps == 8) {
149             return src[xi + yi * linesize];
150         }
151     }
152 
153     return 0;
154 }
155 
calculate_sums(GEQContext * geq,int plane,int w,int h)156 static int calculate_sums(GEQContext *geq, int plane, int w, int h)
157 {
158     int xi, yi;
159     AVFrame *picref = geq->picref;
160     const uint8_t *src = picref->data[plane];
161     int linesize = picref->linesize[plane];
162 
163     if (!geq->pixel_sums[plane])
164         geq->pixel_sums[plane] = av_malloc_array(w, h * sizeof (*geq->pixel_sums[plane]));
165     if (!geq->pixel_sums[plane])
166         return AVERROR(ENOMEM);
167     if (geq->bps == 32)
168         linesize /= 4;
169     else if (geq->bps > 8 && geq->bps <= 16)
170         linesize /= 2;
171     for (yi = 0; yi < h; yi ++) {
172         if (geq->bps > 8 && geq->bps <= 16) {
173             const uint16_t *src16 = (const uint16_t*)src;
174             double linesum = 0;
175 
176             for (xi = 0; xi < w; xi ++) {
177                 linesum += src16[xi + yi * linesize];
178                 geq->pixel_sums[plane][xi + yi * w] = linesum;
179             }
180         } else if (geq->bps == 8) {
181             double linesum = 0;
182 
183             for (xi = 0; xi < w; xi ++) {
184                 linesum += src[xi + yi * linesize];
185                 geq->pixel_sums[plane][xi + yi * w] = linesum;
186             }
187         } else if (geq->bps == 32) {
188             const float *src32 = (const float*)src;
189             double linesum = 0;
190 
191             for (xi = 0; xi < w; xi ++) {
192                 linesum += src32[xi + yi * linesize];
193                 geq->pixel_sums[plane][xi + yi * w] = linesum;
194             }
195         }
196         if (yi)
197             for (xi = 0; xi < w; xi ++) {
198                 geq->pixel_sums[plane][xi + yi * w] += geq->pixel_sums[plane][xi + yi * w - w];
199             }
200     }
201     return 0;
202 }
203 
getpix_integrate_internal(GEQContext * geq,int x,int y,int plane,int w,int h)204 static inline double getpix_integrate_internal(GEQContext *geq, int x, int y, int plane, int w, int h)
205 {
206     if (x > w - 1) {
207         double boundary =   getpix_integrate_internal(geq, w - 1, y, plane, w, h);
208         return 2*boundary - getpix_integrate_internal(geq, 2*(w - 1) - x, y, plane, w, h);
209     } else if (y > h - 1) {
210         double boundary =   getpix_integrate_internal(geq, x, h - 1, plane, w, h);
211         return 2*boundary - getpix_integrate_internal(geq, x, 2*(h - 1) - y, plane, w, h);
212     } else if (x < 0) {
213         if (x == -1) return 0;
214         return - getpix_integrate_internal(geq, -x-2, y, plane, w, h);
215     } else if (y < 0) {
216         if (y == -1) return 0;
217         return - getpix_integrate_internal(geq, x, -y-2, plane, w, h);
218     }
219 
220     return geq->pixel_sums[plane][x + y * w];
221 }
222 
getpix_integrate(void * priv,double x,double y,int plane)223 static inline double getpix_integrate(void *priv, double x, double y, int plane) {
224     GEQContext *geq = priv;
225     AVFrame *picref = geq->picref;
226     const uint8_t *src = picref->data[plane];
227     const int w = (plane == 1 || plane == 2) ? AV_CEIL_RSHIFT(picref->width,  geq->hsub) : picref->width;
228     const int h = (plane == 1 || plane == 2) ? AV_CEIL_RSHIFT(picref->height, geq->vsub) : picref->height;
229 
230     if (!src)
231         return 0;
232 
233     return getpix_integrate_internal(geq, lrint(av_clipd(x, -w, 2*w)), lrint(av_clipd(y, -h, 2*h)), plane, w, h);
234 }
235 
236 //TODO: cubic interpolate
237 //TODO: keep the last few frames
lum(void * priv,double x,double y)238 static double lum(void *priv, double x, double y) { return getpix(priv, x, y, 0); }
cb(void * priv,double x,double y)239 static double  cb(void *priv, double x, double y) { return getpix(priv, x, y, 1); }
cr(void * priv,double x,double y)240 static double  cr(void *priv, double x, double y) { return getpix(priv, x, y, 2); }
alpha(void * priv,double x,double y)241 static double alpha(void *priv, double x, double y) { return getpix(priv, x, y, 3); }
242 
lumsum(void * priv,double x,double y)243 static double   lumsum(void *priv, double x, double y) { return getpix_integrate(priv, x, y, 0); }
cbsum(void * priv,double x,double y)244 static double    cbsum(void *priv, double x, double y) { return getpix_integrate(priv, x, y, 1); }
crsub(void * priv,double x,double y)245 static double    crsub(void *priv, double x, double y) { return getpix_integrate(priv, x, y, 2); }
alphasum(void * priv,double x,double y)246 static double alphasum(void *priv, double x, double y) { return getpix_integrate(priv, x, y, 3); }
247 
geq_init(AVFilterContext * ctx)248 static av_cold int geq_init(AVFilterContext *ctx)
249 {
250     GEQContext *geq = ctx->priv;
251     int plane, ret = 0;
252 
253     if (!geq->expr_str[Y] && !geq->expr_str[G] && !geq->expr_str[B] && !geq->expr_str[R]) {
254         av_log(ctx, AV_LOG_ERROR, "A luminance or RGB expression is mandatory\n");
255         ret = AVERROR(EINVAL);
256         goto end;
257     }
258     geq->is_rgb = !geq->expr_str[Y];
259 
260     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])) {
261         av_log(ctx, AV_LOG_ERROR, "Either YCbCr or RGB but not both must be specified\n");
262         ret = AVERROR(EINVAL);
263         goto end;
264     }
265 
266     if (!geq->expr_str[U] && !geq->expr_str[V]) {
267         /* No chroma at all: fallback on luma */
268         geq->expr_str[U] = av_strdup(geq->expr_str[Y]);
269         geq->expr_str[V] = av_strdup(geq->expr_str[Y]);
270     } else {
271         /* One chroma unspecified, fallback on the other */
272         if (!geq->expr_str[U]) geq->expr_str[U] = av_strdup(geq->expr_str[V]);
273         if (!geq->expr_str[V]) geq->expr_str[V] = av_strdup(geq->expr_str[U]);
274     }
275 
276     if (!geq->expr_str[A] && geq->bps != 32) {
277         geq->expr_str[A] = av_asprintf("%d", (1<<geq->bps) - 1);
278     } else if (!geq->expr_str[A]) {
279         geq->expr_str[A] = av_asprintf("%f", 1.f);
280     }
281     if (!geq->expr_str[G])
282         geq->expr_str[G] = av_strdup("g(X,Y)");
283     if (!geq->expr_str[B])
284         geq->expr_str[B] = av_strdup("b(X,Y)");
285     if (!geq->expr_str[R])
286         geq->expr_str[R] = av_strdup("r(X,Y)");
287 
288     if (geq->is_rgb ?
289             (!geq->expr_str[G] || !geq->expr_str[B] || !geq->expr_str[R])
290                     :
291             (!geq->expr_str[U] || !geq->expr_str[V] || !geq->expr_str[A])) {
292         ret = AVERROR(ENOMEM);
293         goto end;
294     }
295 
296     for (plane = 0; plane < NB_PLANES; plane++) {
297         static double (*const p[])(void *, double, double) = {
298             lum   , cb   , cr   , alpha   ,
299             lumsum, cbsum, crsub, alphasum,
300         };
301         static const char *const func2_yuv_names[]    = {
302             "lum"   , "cb"   , "cr"   , "alpha"   , "p",
303             "lumsum", "cbsum", "crsum", "alphasum", "psum",
304             NULL };
305         static const char *const func2_rgb_names[]    = {
306             "g"   , "b"   , "r"   , "alpha"   , "p",
307             "gsum", "bsum", "rsum", "alphasum", "psum",
308             NULL };
309         const char *const *func2_names       = geq->is_rgb ? func2_rgb_names : func2_yuv_names;
310         double (*const func2[])(void *, double, double) = {
311             lum   , cb   , cr   , alpha   , p[plane],
312             lumsum, cbsum, crsub, alphasum, p[plane + 4],
313             NULL };
314         int counter[10] = {0};
315 
316         for (int i = 0; i < MAX_NB_THREADS; i++) {
317             ret = av_expr_parse(&geq->e[plane][i], geq->expr_str[plane < 3 && geq->is_rgb ? plane+4 : plane], var_names,
318                                 NULL, NULL, func2_names, func2, 0, ctx);
319             if (ret < 0)
320                 goto end;
321         }
322 
323         av_expr_count_func(geq->e[plane][0], counter, FF_ARRAY_ELEMS(counter), 2);
324         geq->needs_sum[plane] = counter[5] + counter[6] + counter[7] + counter[8] + counter[9];
325     }
326 
327 end:
328     return ret;
329 }
330 
geq_query_formats(AVFilterContext * ctx)331 static int geq_query_formats(AVFilterContext *ctx)
332 {
333     GEQContext *geq = ctx->priv;
334     static const enum AVPixelFormat yuv_pix_fmts[] = {
335         AV_PIX_FMT_YUV444P,  AV_PIX_FMT_YUV422P,  AV_PIX_FMT_YUV420P,
336         AV_PIX_FMT_YUV411P,  AV_PIX_FMT_YUV410P,  AV_PIX_FMT_YUV440P,
337         AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
338         AV_PIX_FMT_GRAY8,
339         AV_PIX_FMT_YUV444P9,  AV_PIX_FMT_YUV422P9,  AV_PIX_FMT_YUV420P9,
340         AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA420P9,
341         AV_PIX_FMT_YUV444P10,  AV_PIX_FMT_YUV422P10,  AV_PIX_FMT_YUV420P10,
342         AV_PIX_FMT_YUV440P10,
343         AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA420P10,
344         AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10,
345         AV_PIX_FMT_YUV444P12,  AV_PIX_FMT_YUV422P12,  AV_PIX_FMT_YUV420P12,
346         AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14,
347         AV_PIX_FMT_YUV444P14,  AV_PIX_FMT_YUV422P14,  AV_PIX_FMT_YUV420P14,
348         AV_PIX_FMT_YUV444P16,  AV_PIX_FMT_YUV422P16,  AV_PIX_FMT_YUV420P16,
349         AV_PIX_FMT_YUVA444P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA420P16,
350         AV_PIX_FMT_GRAY16,
351         AV_PIX_FMT_GRAYF32,
352         AV_PIX_FMT_NONE
353     };
354     static const enum AVPixelFormat rgb_pix_fmts[] = {
355         AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
356         AV_PIX_FMT_GBRP9,
357         AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRAP10,
358         AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12,
359         AV_PIX_FMT_GBRP14,
360         AV_PIX_FMT_GBRP16, AV_PIX_FMT_GBRAP16,
361         AV_PIX_FMT_GBRPF32, AV_PIX_FMT_GBRAPF32,
362         AV_PIX_FMT_NONE
363     };
364     const enum AVPixelFormat *pix_fmts = geq->is_rgb ? rgb_pix_fmts : yuv_pix_fmts;
365 
366     return ff_set_common_formats_from_list(ctx, pix_fmts);
367 }
368 
geq_config_props(AVFilterLink * inlink)369 static int geq_config_props(AVFilterLink *inlink)
370 {
371     GEQContext *geq = inlink->dst->priv;
372     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
373 
374     av_assert0(desc);
375 
376     geq->hsub = desc->log2_chroma_w;
377     geq->vsub = desc->log2_chroma_h;
378     geq->bps = desc->comp[0].depth;
379     geq->planes = desc->nb_components;
380     return 0;
381 }
382 
383 typedef struct ThreadData {
384     int height;
385     int width;
386     int plane;
387     int linesize;
388 } ThreadData;
389 
slice_geq_filter(AVFilterContext * ctx,void * arg,int jobnr,int nb_jobs)390 static int slice_geq_filter(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
391 {
392     GEQContext *geq = ctx->priv;
393     ThreadData *td = arg;
394     const int height = td->height;
395     const int width = td->width;
396     const int plane = td->plane;
397     const int linesize = td->linesize;
398     const int slice_start = (height *  jobnr) / nb_jobs;
399     const int slice_end = (height * (jobnr+1)) / nb_jobs;
400     int x, y;
401 
402     double values[VAR_VARS_NB];
403     values[VAR_W] = geq->values[VAR_W];
404     values[VAR_H] = geq->values[VAR_H];
405     values[VAR_N] = geq->values[VAR_N];
406     values[VAR_SW] = geq->values[VAR_SW];
407     values[VAR_SH] = geq->values[VAR_SH];
408     values[VAR_T] = geq->values[VAR_T];
409 
410     if (geq->bps == 8) {
411         uint8_t *ptr = geq->dst + linesize * slice_start;
412         for (y = slice_start; y < slice_end; y++) {
413             values[VAR_Y] = y;
414 
415             for (x = 0; x < width; x++) {
416                 values[VAR_X] = x;
417                 ptr[x] = av_expr_eval(geq->e[plane][jobnr], values, geq);
418             }
419             ptr += linesize;
420         }
421     } else if (geq->bps <= 16) {
422         uint16_t *ptr16 = geq->dst16 + (linesize/2) * slice_start;
423         for (y = slice_start; y < slice_end; y++) {
424             values[VAR_Y] = y;
425             for (x = 0; x < width; x++) {
426                 values[VAR_X] = x;
427                 ptr16[x] = av_expr_eval(geq->e[plane][jobnr], values, geq);
428             }
429             ptr16 += linesize/2;
430         }
431     } else {
432         float *ptr32 = geq->dst32 + (linesize/4) * slice_start;
433         for (y = slice_start; y < slice_end; y++) {
434             values[VAR_Y] = y;
435             for (x = 0; x < width; x++) {
436                 values[VAR_X] = x;
437                 ptr32[x] = av_expr_eval(geq->e[plane][jobnr], values, geq);
438             }
439             ptr32 += linesize/4;
440         }
441     }
442 
443     return 0;
444 }
445 
geq_filter_frame(AVFilterLink * inlink,AVFrame * in)446 static int geq_filter_frame(AVFilterLink *inlink, AVFrame *in)
447 {
448     int plane;
449     AVFilterContext *ctx = inlink->dst;
450     const int nb_threads = FFMIN(MAX_NB_THREADS, ff_filter_get_nb_threads(ctx));
451     GEQContext *geq = ctx->priv;
452     AVFilterLink *outlink = inlink->dst->outputs[0];
453     AVFrame *out;
454 
455     geq->values[VAR_N] = inlink->frame_count_out,
456     geq->values[VAR_T] = in->pts == AV_NOPTS_VALUE ? NAN : in->pts * av_q2d(inlink->time_base),
457 
458     geq->picref = in;
459     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
460     if (!out) {
461         av_frame_free(&in);
462         return AVERROR(ENOMEM);
463     }
464     av_frame_copy_props(out, in);
465 
466     for (plane = 0; plane < geq->planes && out->data[plane]; plane++) {
467         const int width = (plane == 1 || plane == 2) ? AV_CEIL_RSHIFT(inlink->w, geq->hsub) : inlink->w;
468         const int height = (plane == 1 || plane == 2) ? AV_CEIL_RSHIFT(inlink->h, geq->vsub) : inlink->h;
469         const int linesize = out->linesize[plane];
470         ThreadData td;
471 
472         geq->dst = out->data[plane];
473         geq->dst16 = (uint16_t*)out->data[plane];
474         geq->dst32 = (float*)out->data[plane];
475 
476         geq->values[VAR_W]  = width;
477         geq->values[VAR_H]  = height;
478         geq->values[VAR_SW] = width / (double)inlink->w;
479         geq->values[VAR_SH] = height / (double)inlink->h;
480 
481         td.width = width;
482         td.height = height;
483         td.plane = plane;
484         td.linesize = linesize;
485 
486         if (geq->needs_sum[plane])
487             calculate_sums(geq, plane, width, height);
488 
489         ff_filter_execute(ctx, slice_geq_filter, &td,
490                           NULL, FFMIN(height, nb_threads));
491     }
492 
493     av_frame_free(&geq->picref);
494     return ff_filter_frame(outlink, out);
495 }
496 
geq_uninit(AVFilterContext * ctx)497 static av_cold void geq_uninit(AVFilterContext *ctx)
498 {
499     int i;
500     GEQContext *geq = ctx->priv;
501 
502     for (i = 0; i < NB_PLANES; i++)
503         for (int j = 0; j < MAX_NB_THREADS; j++)
504             av_expr_free(geq->e[i][j]);
505     for (i = 0; i < NB_PLANES; i++)
506         av_freep(&geq->pixel_sums);
507 }
508 
509 static const AVFilterPad geq_inputs[] = {
510     {
511         .name         = "default",
512         .type         = AVMEDIA_TYPE_VIDEO,
513         .config_props = geq_config_props,
514         .filter_frame = geq_filter_frame,
515     },
516 };
517 
518 static const AVFilterPad geq_outputs[] = {
519     {
520         .name = "default",
521         .type = AVMEDIA_TYPE_VIDEO,
522     },
523 };
524 
525 const AVFilter ff_vf_geq = {
526     .name          = "geq",
527     .description   = NULL_IF_CONFIG_SMALL("Apply generic equation to each pixel."),
528     .priv_size     = sizeof(GEQContext),
529     .init          = geq_init,
530     .uninit        = geq_uninit,
531     FILTER_INPUTS(geq_inputs),
532     FILTER_OUTPUTS(geq_outputs),
533     FILTER_QUERY_FUNC(geq_query_formats),
534     .priv_class    = &geq_class,
535     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
536 };
537