2 * Copyright (C) 2006 Michael Niedermayer <michaelni@gmx.at>
3 * Copyright (C) 2012 Clément Bœsch <u pkh me>
5 * This file is part of FFmpeg.
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.
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.
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
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.
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"
38 enum InterpolationMethods {
44 static const char *const var_names[] = { "X", "Y", "W", "H", "N", "SW", "SH", "T", NULL };
45 enum { VAR_X, VAR_Y, VAR_W, VAR_H, VAR_N, VAR_SW, VAR_SH, VAR_T, VAR_VARS_NB };
47 typedef struct GEQContext {
49 AVExpr *e[NB_PLANES]; ///< expressions for each plane
50 char *expr_str[4+3]; ///< expression strings for each plane
51 AVFrame *picref; ///< current input buffer
52 uint8_t *dst; ///< reference pointer to the 8bits output
53 uint16_t *dst16; ///< reference pointer to the 16bits output
54 double values[VAR_VARS_NB]; ///< expression values
55 int hsub, vsub; ///< chroma subsampling
56 int planes; ///< number of planes
61 double *pixel_sums[NB_PLANES];
62 int needs_sum[NB_PLANES];
65 enum { Y = 0, U, V, A, G, B, R };
67 #define OFFSET(x) offsetof(GEQContext, x)
68 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
70 static const AVOption geq_options[] = {
71 { "lum_expr", "set luminance expression", OFFSET(expr_str[Y]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
72 { "lum", "set luminance expression", OFFSET(expr_str[Y]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
73 { "cb_expr", "set chroma blue expression", OFFSET(expr_str[U]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
74 { "cb", "set chroma blue expression", OFFSET(expr_str[U]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
75 { "cr_expr", "set chroma red expression", OFFSET(expr_str[V]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
76 { "cr", "set chroma red expression", OFFSET(expr_str[V]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
77 { "alpha_expr", "set alpha expression", OFFSET(expr_str[A]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
78 { "a", "set alpha expression", OFFSET(expr_str[A]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
79 { "red_expr", "set red expression", OFFSET(expr_str[R]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
80 { "r", "set red expression", OFFSET(expr_str[R]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
81 { "green_expr", "set green expression", OFFSET(expr_str[G]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
82 { "g", "set green expression", OFFSET(expr_str[G]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
83 { "blue_expr", "set blue expression", OFFSET(expr_str[B]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
84 { "b", "set blue expression", OFFSET(expr_str[B]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
85 { "interpolation","set interpolation method", OFFSET(interpolation), AV_OPT_TYPE_INT, {.i64=INTERP_BILINEAR}, 0, NB_INTERP-1, FLAGS, "interp" },
86 { "i", "set interpolation method", OFFSET(interpolation), AV_OPT_TYPE_INT, {.i64=INTERP_BILINEAR}, 0, NB_INTERP-1, FLAGS, "interp" },
87 { "nearest", "nearest interpolation", 0, AV_OPT_TYPE_CONST, {.i64=INTERP_NEAREST}, 0, 0, FLAGS, "interp" },
88 { "n", "nearest interpolation", 0, AV_OPT_TYPE_CONST, {.i64=INTERP_NEAREST}, 0, 0, FLAGS, "interp" },
89 { "bilinear", "bilinear interpolation", 0, AV_OPT_TYPE_CONST, {.i64=INTERP_BILINEAR}, 0, 0, FLAGS, "interp" },
90 { "b", "bilinear interpolation", 0, AV_OPT_TYPE_CONST, {.i64=INTERP_BILINEAR}, 0, 0, FLAGS, "interp" },
94 AVFILTER_DEFINE_CLASS(geq);
96 static inline double getpix(void *priv, double x, double y, int plane)
99 GEQContext *geq = priv;
100 AVFrame *picref = geq->picref;
101 const uint8_t *src = picref->data[plane];
102 int linesize = picref->linesize[plane];
103 const int w = (plane == 1 || plane == 2) ? AV_CEIL_RSHIFT(picref->width, geq->hsub) : picref->width;
104 const int h = (plane == 1 || plane == 2) ? AV_CEIL_RSHIFT(picref->height, geq->vsub) : picref->height;
109 if (geq->interpolation == INTERP_BILINEAR) {
110 xi = x = av_clipd(x, 0, w - 2);
111 yi = y = av_clipd(y, 0, h - 2);
117 const uint16_t *src16 = (const uint16_t*)src;
120 return (1-y)*((1-x)*src16[xi + yi * linesize] + x*src16[xi + 1 + yi * linesize])
121 + y *((1-x)*src16[xi + (yi+1) * linesize] + x*src16[xi + 1 + (yi+1) * linesize]);
123 return (1-y)*((1-x)*src[xi + yi * linesize] + x*src[xi + 1 + yi * linesize])
124 + y *((1-x)*src[xi + (yi+1) * linesize] + x*src[xi + 1 + (yi+1) * linesize]);
127 xi = av_clipd(x, 0, w - 1);
128 yi = av_clipd(y, 0, h - 1);
131 const uint16_t *src16 = (const uint16_t*)src;
134 return src16[xi + yi * linesize];
136 return src[xi + yi * linesize];
141 static int calculate_sums(GEQContext *geq, int plane, int w, int h)
144 AVFrame *picref = geq->picref;
145 const uint8_t *src = picref->data[plane];
146 int linesize = picref->linesize[plane];
148 if (!geq->pixel_sums[plane])
149 geq->pixel_sums[plane] = av_malloc_array(w, h * sizeof (*geq->pixel_sums[plane]));
150 if (!geq->pixel_sums[plane])
151 return AVERROR(ENOMEM);
154 for (yi = 0; yi < h; yi ++) {
156 const uint16_t *src16 = (const uint16_t*)src;
159 for (xi = 0; xi < w; xi ++) {
160 linesum += src16[xi + yi * linesize];
161 geq->pixel_sums[plane][xi + yi * w] = linesum;
166 for (xi = 0; xi < w; xi ++) {
167 linesum += src[xi + yi * linesize];
168 geq->pixel_sums[plane][xi + yi * w] = linesum;
172 for (xi = 0; xi < w; xi ++) {
173 geq->pixel_sums[plane][xi + yi * w] += geq->pixel_sums[plane][xi + yi * w - w];
179 static inline double getpix_integrate_internal(GEQContext *geq, int x, int y, int plane, int w, int h)
182 double boundary = getpix_integrate_internal(geq, w - 1, y, plane, w, h);
183 return 2*boundary - getpix_integrate_internal(geq, 2*(w - 1) - x, y, plane, w, h);
184 } else if (y > h - 1) {
185 double boundary = getpix_integrate_internal(geq, x, h - 1, plane, w, h);
186 return 2*boundary - getpix_integrate_internal(geq, x, 2*(h - 1) - y, plane, w, h);
188 if (x == -1) return 0;
189 return - getpix_integrate_internal(geq, -x-2, y, plane, w, h);
191 if (y == -1) return 0;
192 return - getpix_integrate_internal(geq, x, -y-2, plane, w, h);
195 return geq->pixel_sums[plane][x + y * w];
198 static inline double getpix_integrate(void *priv, double x, double y, int plane) {
199 GEQContext *geq = priv;
200 AVFrame *picref = geq->picref;
201 const uint8_t *src = picref->data[plane];
202 const int w = (plane == 1 || plane == 2) ? AV_CEIL_RSHIFT(picref->width, geq->hsub) : picref->width;
203 const int h = (plane == 1 || plane == 2) ? AV_CEIL_RSHIFT(picref->height, geq->vsub) : picref->height;
208 return getpix_integrate_internal(geq, lrint(av_clipd(x, -w, 2*w)), lrint(av_clipd(y, -h, 2*h)), plane, w, h);
211 //TODO: cubic interpolate
212 //TODO: keep the last few frames
213 static double lum(void *priv, double x, double y) { return getpix(priv, x, y, 0); }
214 static double cb(void *priv, double x, double y) { return getpix(priv, x, y, 1); }
215 static double cr(void *priv, double x, double y) { return getpix(priv, x, y, 2); }
216 static double alpha(void *priv, double x, double y) { return getpix(priv, x, y, 3); }
218 static double lumsum(void *priv, double x, double y) { return getpix_integrate(priv, x, y, 0); }
219 static double cbsum(void *priv, double x, double y) { return getpix_integrate(priv, x, y, 1); }
220 static double crsub(void *priv, double x, double y) { return getpix_integrate(priv, x, y, 2); }
221 static double alphasum(void *priv, double x, double y) { return getpix_integrate(priv, x, y, 3); }
223 static av_cold int geq_init(AVFilterContext *ctx)
225 GEQContext *geq = ctx->priv;
228 if (!geq->expr_str[Y] && !geq->expr_str[G] && !geq->expr_str[B] && !geq->expr_str[R]) {
229 av_log(ctx, AV_LOG_ERROR, "A luminance or RGB expression is mandatory\n");
230 ret = AVERROR(EINVAL);
233 geq->is_rgb = !geq->expr_str[Y];
235 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])) {
236 av_log(ctx, AV_LOG_ERROR, "Either YCbCr or RGB but not both must be specified\n");
237 ret = AVERROR(EINVAL);
241 if (!geq->expr_str[U] && !geq->expr_str[V]) {
242 /* No chroma at all: fallback on luma */
243 geq->expr_str[U] = av_strdup(geq->expr_str[Y]);
244 geq->expr_str[V] = av_strdup(geq->expr_str[Y]);
246 /* One chroma unspecified, fallback on the other */
247 if (!geq->expr_str[U]) geq->expr_str[U] = av_strdup(geq->expr_str[V]);
248 if (!geq->expr_str[V]) geq->expr_str[V] = av_strdup(geq->expr_str[U]);
251 if (!geq->expr_str[A]) {
253 snprintf(bps_string, sizeof(bps_string), "%d", (1<<geq->bps) - 1);
254 geq->expr_str[A] = av_strdup(bps_string);
256 if (!geq->expr_str[G])
257 geq->expr_str[G] = av_strdup("g(X,Y)");
258 if (!geq->expr_str[B])
259 geq->expr_str[B] = av_strdup("b(X,Y)");
260 if (!geq->expr_str[R])
261 geq->expr_str[R] = av_strdup("r(X,Y)");
264 (!geq->expr_str[G] || !geq->expr_str[B] || !geq->expr_str[R])
266 (!geq->expr_str[U] || !geq->expr_str[V] || !geq->expr_str[A])) {
267 ret = AVERROR(ENOMEM);
271 for (plane = 0; plane < NB_PLANES; plane++) {
272 static double (*p[])(void *, double, double) = {
273 lum , cb , cr , alpha ,
274 lumsum, cbsum, crsub, alphasum,
276 static const char *const func2_yuv_names[] = {
277 "lum" , "cb" , "cr" , "alpha" , "p",
278 "lumsum", "cbsum", "crsum", "alphasum", "psum",
280 static const char *const func2_rgb_names[] = {
281 "g" , "b" , "r" , "alpha" , "p",
282 "gsum", "bsum", "rsum", "alphasum", "psum",
284 const char *const *func2_names = geq->is_rgb ? func2_rgb_names : func2_yuv_names;
285 double (*func2[])(void *, double, double) = {
286 lum , cb , cr , alpha , p[plane],
287 lumsum, cbsum, crsub, alphasum, p[plane + 4],
289 int counter[10] = {0};
291 ret = av_expr_parse(&geq->e[plane], geq->expr_str[plane < 3 && geq->is_rgb ? plane+4 : plane], var_names,
292 NULL, NULL, func2_names, func2, 0, ctx);
296 av_expr_count_func(geq->e[plane], counter, FF_ARRAY_ELEMS(counter), 2);
297 geq->needs_sum[plane] = counter[5] + counter[6] + counter[7] + counter[8] + counter[9];
304 static int geq_query_formats(AVFilterContext *ctx)
306 GEQContext *geq = ctx->priv;
307 static const enum AVPixelFormat yuv_pix_fmts[] = {
308 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
309 AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
310 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
312 AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV420P9,
313 AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA420P9,
314 AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV420P10,
315 AV_PIX_FMT_YUV440P10,
316 AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA420P10,
317 AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10,
318 AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12,
319 AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14,
320 AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14,
321 AV_PIX_FMT_YUV444P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV420P16,
322 AV_PIX_FMT_YUVA444P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA420P16,
326 static const enum AVPixelFormat rgb_pix_fmts[] = {
327 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
329 AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRAP10,
330 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12,
332 AV_PIX_FMT_GBRP16, AV_PIX_FMT_GBRAP16,
335 AVFilterFormats *fmts_list;
338 fmts_list = ff_make_format_list(rgb_pix_fmts);
340 fmts_list = ff_make_format_list(yuv_pix_fmts);
342 return AVERROR(ENOMEM);
343 return ff_set_common_formats(ctx, fmts_list);
346 static int geq_config_props(AVFilterLink *inlink)
348 GEQContext *geq = inlink->dst->priv;
349 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
353 geq->hsub = desc->log2_chroma_w;
354 geq->vsub = desc->log2_chroma_h;
355 geq->bps = desc->comp[0].depth;
356 geq->planes = desc->nb_components;
360 typedef struct ThreadData {
367 static int slice_geq_filter(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
369 GEQContext *geq = ctx->priv;
370 ThreadData *td = arg;
371 const int height = td->height;
372 const int width = td->width;
373 const int plane = td->plane;
374 const int linesize = td->linesize;
375 const int slice_start = (height * jobnr) / nb_jobs;
376 const int slice_end = (height * (jobnr+1)) / nb_jobs;
379 double values[VAR_VARS_NB];
380 values[VAR_W] = geq->values[VAR_W];
381 values[VAR_H] = geq->values[VAR_H];
382 values[VAR_N] = geq->values[VAR_N];
383 values[VAR_SW] = geq->values[VAR_SW];
384 values[VAR_SH] = geq->values[VAR_SH];
385 values[VAR_T] = geq->values[VAR_T];
388 uint8_t *ptr = geq->dst + linesize * slice_start;
389 for (y = slice_start; y < slice_end; y++) {
392 for (x = 0; x < width; x++) {
394 ptr[x] = av_expr_eval(geq->e[plane], values, geq);
399 uint16_t *ptr16 = geq->dst16 + (linesize/2) * slice_start;
400 for (y = slice_start; y < slice_end; y++) {
402 for (x = 0; x < width; x++) {
404 ptr16[x] = av_expr_eval(geq->e[plane], values, geq);
413 static int geq_filter_frame(AVFilterLink *inlink, AVFrame *in)
416 AVFilterContext *ctx = inlink->dst;
417 const int nb_threads = ff_filter_get_nb_threads(ctx);
418 GEQContext *geq = ctx->priv;
419 AVFilterLink *outlink = inlink->dst->outputs[0];
422 geq->values[VAR_N] = inlink->frame_count_out,
423 geq->values[VAR_T] = in->pts == AV_NOPTS_VALUE ? NAN : in->pts * av_q2d(inlink->time_base),
426 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
429 return AVERROR(ENOMEM);
431 av_frame_copy_props(out, in);
433 for (plane = 0; plane < geq->planes && out->data[plane]; plane++) {
434 const int width = (plane == 1 || plane == 2) ? AV_CEIL_RSHIFT(inlink->w, geq->hsub) : inlink->w;
435 const int height = (plane == 1 || plane == 2) ? AV_CEIL_RSHIFT(inlink->h, geq->vsub) : inlink->h;
436 const int linesize = out->linesize[plane];
439 geq->dst = out->data[plane];
440 geq->dst16 = (uint16_t*)out->data[plane];
442 geq->values[VAR_W] = width;
443 geq->values[VAR_H] = height;
444 geq->values[VAR_SW] = width / (double)inlink->w;
445 geq->values[VAR_SH] = height / (double)inlink->h;
450 td.linesize = linesize;
452 if (geq->needs_sum[plane])
453 calculate_sums(geq, plane, width, height);
455 ctx->internal->execute(ctx, slice_geq_filter, &td, NULL, FFMIN(height, nb_threads));
458 av_frame_free(&geq->picref);
459 return ff_filter_frame(outlink, out);
462 static av_cold void geq_uninit(AVFilterContext *ctx)
465 GEQContext *geq = ctx->priv;
467 for (i = 0; i < FF_ARRAY_ELEMS(geq->e); i++)
468 av_expr_free(geq->e[i]);
469 for (i = 0; i < NB_PLANES; i++)
470 av_freep(&geq->pixel_sums);
473 static const AVFilterPad geq_inputs[] = {
476 .type = AVMEDIA_TYPE_VIDEO,
477 .config_props = geq_config_props,
478 .filter_frame = geq_filter_frame,
483 static const AVFilterPad geq_outputs[] = {
486 .type = AVMEDIA_TYPE_VIDEO,
491 AVFilter ff_vf_geq = {
493 .description = NULL_IF_CONFIG_SMALL("Apply generic equation to each pixel."),
494 .priv_size = sizeof(GEQContext),
496 .uninit = geq_uninit,
497 .query_formats = geq_query_formats,
498 .inputs = geq_inputs,
499 .outputs = geq_outputs,
500 .priv_class = &geq_class,
501 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,