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 modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (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
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
19 * 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"
36 static const char *const var_names[] = { "X", "Y", "W", "H", "N", "SW", "SH", "T", NULL };
37 enum { VAR_X, VAR_Y, VAR_W, VAR_H, VAR_N, VAR_SW, VAR_SH, VAR_T, VAR_VARS_NB };
39 typedef struct GEQContext {
41 AVExpr *e[4]; ///< expressions for each plane
42 char *expr_str[4+3]; ///< expression strings for each plane
43 AVFrame *picref; ///< current input buffer
44 uint8_t *dst; ///< reference pointer to the 8bits output
45 uint16_t *dst16; ///< reference pointer to the 16bits output
46 double values[VAR_VARS_NB]; ///< expression values
47 int hsub, vsub; ///< chroma subsampling
48 int planes; ///< number of planes
53 enum { Y = 0, U, V, A, G, B, R };
55 #define OFFSET(x) offsetof(GEQContext, x)
56 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
58 static const AVOption geq_options[] = {
59 { "lum_expr", "set luminance expression", OFFSET(expr_str[Y]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
60 { "lum", "set luminance expression", OFFSET(expr_str[Y]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
61 { "cb_expr", "set chroma blue expression", OFFSET(expr_str[U]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
62 { "cb", "set chroma blue expression", OFFSET(expr_str[U]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
63 { "cr_expr", "set chroma red expression", OFFSET(expr_str[V]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
64 { "cr", "set chroma red expression", OFFSET(expr_str[V]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
65 { "alpha_expr", "set alpha expression", OFFSET(expr_str[A]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
66 { "a", "set alpha expression", OFFSET(expr_str[A]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
67 { "red_expr", "set red expression", OFFSET(expr_str[R]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
68 { "r", "set red expression", OFFSET(expr_str[R]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
69 { "green_expr", "set green expression", OFFSET(expr_str[G]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
70 { "g", "set green expression", OFFSET(expr_str[G]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
71 { "blue_expr", "set blue expression", OFFSET(expr_str[B]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
72 { "b", "set blue expression", OFFSET(expr_str[B]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
76 AVFILTER_DEFINE_CLASS(geq);
78 static inline double getpix(void *priv, double x, double y, int plane)
81 GEQContext *geq = priv;
82 AVFrame *picref = geq->picref;
83 const uint8_t *src = picref->data[plane];
84 int linesize = picref->linesize[plane];
85 const int w = (plane == 1 || plane == 2) ? AV_CEIL_RSHIFT(picref->width, geq->hsub) : picref->width;
86 const int h = (plane == 1 || plane == 2) ? AV_CEIL_RSHIFT(picref->height, geq->vsub) : picref->height;
91 xi = x = av_clipf(x, 0, w - 2);
92 yi = y = av_clipf(y, 0, h - 2);
98 const uint16_t *src16 = (const uint16_t*)src;
101 return (1-y)*((1-x)*src16[xi + yi * linesize] + x*src16[xi + 1 + yi * linesize])
102 + y *((1-x)*src16[xi + (yi+1) * linesize] + x*src16[xi + 1 + (yi+1) * linesize]);
104 return (1-y)*((1-x)*src[xi + yi * linesize] + x*src[xi + 1 + yi * linesize])
105 + y *((1-x)*src[xi + (yi+1) * linesize] + x*src[xi + 1 + (yi+1) * linesize]);
109 //TODO: cubic interpolate
110 //TODO: keep the last few frames
111 static double lum(void *priv, double x, double y) { return getpix(priv, x, y, 0); }
112 static double cb(void *priv, double x, double y) { return getpix(priv, x, y, 1); }
113 static double cr(void *priv, double x, double y) { return getpix(priv, x, y, 2); }
114 static double alpha(void *priv, double x, double y) { return getpix(priv, x, y, 3); }
116 static av_cold int geq_init(AVFilterContext *ctx)
118 GEQContext *geq = ctx->priv;
121 if (!geq->expr_str[Y] && !geq->expr_str[G] && !geq->expr_str[B] && !geq->expr_str[R]) {
122 av_log(ctx, AV_LOG_ERROR, "A luminance or RGB expression is mandatory\n");
123 ret = AVERROR(EINVAL);
126 geq->is_rgb = !geq->expr_str[Y];
128 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])) {
129 av_log(ctx, AV_LOG_ERROR, "Either YCbCr or RGB but not both must be specified\n");
130 ret = AVERROR(EINVAL);
134 if (!geq->expr_str[U] && !geq->expr_str[V]) {
135 /* No chroma at all: fallback on luma */
136 geq->expr_str[U] = av_strdup(geq->expr_str[Y]);
137 geq->expr_str[V] = av_strdup(geq->expr_str[Y]);
139 /* One chroma unspecified, fallback on the other */
140 if (!geq->expr_str[U]) geq->expr_str[U] = av_strdup(geq->expr_str[V]);
141 if (!geq->expr_str[V]) geq->expr_str[V] = av_strdup(geq->expr_str[U]);
144 if (!geq->expr_str[A]) {
146 snprintf(bps_string, sizeof(bps_string), "%d", (1<<geq->bps) - 1);
147 geq->expr_str[A] = av_strdup(bps_string);
149 if (!geq->expr_str[G])
150 geq->expr_str[G] = av_strdup("g(X,Y)");
151 if (!geq->expr_str[B])
152 geq->expr_str[B] = av_strdup("b(X,Y)");
153 if (!geq->expr_str[R])
154 geq->expr_str[R] = av_strdup("r(X,Y)");
157 (!geq->expr_str[G] || !geq->expr_str[B] || !geq->expr_str[R])
159 (!geq->expr_str[U] || !geq->expr_str[V] || !geq->expr_str[A])) {
160 ret = AVERROR(ENOMEM);
164 for (plane = 0; plane < 4; plane++) {
165 static double (*p[])(void *, double, double) = { lum, cb, cr, alpha };
166 static const char *const func2_yuv_names[] = { "lum", "cb", "cr", "alpha", "p", NULL };
167 static const char *const func2_rgb_names[] = { "g", "b", "r", "alpha", "p", NULL };
168 const char *const *func2_names = geq->is_rgb ? func2_rgb_names : func2_yuv_names;
169 double (*func2[])(void *, double, double) = { lum, cb, cr, alpha, p[plane], NULL };
171 ret = av_expr_parse(&geq->e[plane], geq->expr_str[plane < 3 && geq->is_rgb ? plane+4 : plane], var_names,
172 NULL, NULL, func2_names, func2, 0, ctx);
181 static int geq_query_formats(AVFilterContext *ctx)
183 GEQContext *geq = ctx->priv;
184 static const enum AVPixelFormat yuv_pix_fmts[] = {
185 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
186 AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
187 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
189 AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV420P9,
190 AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA420P9,
191 AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV420P10,
192 AV_PIX_FMT_YUV440P10,
193 AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA420P10,
194 AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10,
195 AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12,
196 AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14,
197 AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14,
198 AV_PIX_FMT_YUV444P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV420P16,
199 AV_PIX_FMT_YUVA444P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA420P16,
203 static const enum AVPixelFormat rgb_pix_fmts[] = {
204 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
206 AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRAP10,
207 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12,
209 AV_PIX_FMT_GBRP16, AV_PIX_FMT_GBRAP16,
212 AVFilterFormats *fmts_list;
215 fmts_list = ff_make_format_list(rgb_pix_fmts);
217 fmts_list = ff_make_format_list(yuv_pix_fmts);
219 return AVERROR(ENOMEM);
220 return ff_set_common_formats(ctx, fmts_list);
223 static int geq_config_props(AVFilterLink *inlink)
225 GEQContext *geq = inlink->dst->priv;
226 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
230 geq->hsub = desc->log2_chroma_w;
231 geq->vsub = desc->log2_chroma_h;
232 geq->bps = desc->comp[0].depth;
233 geq->planes = desc->nb_components;
237 typedef struct ThreadData {
244 static int slice_geq_filter(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
246 GEQContext *geq = ctx->priv;
247 ThreadData *td = arg;
248 const int height = td->height;
249 const int width = td->width;
250 const int plane = td->plane;
251 const int linesize = td->linesize;
252 const int slice_start = (height * jobnr) / nb_jobs;
253 const int slice_end = (height * (jobnr+1)) / nb_jobs;
258 double values[VAR_VARS_NB];
259 values[VAR_W] = geq->values[VAR_W];
260 values[VAR_H] = geq->values[VAR_H];
261 values[VAR_N] = geq->values[VAR_N];
262 values[VAR_SW] = geq->values[VAR_SW];
263 values[VAR_SH] = geq->values[VAR_SH];
264 values[VAR_T] = geq->values[VAR_T];
267 for (y = slice_start; y < slice_end; y++) {
268 ptr = geq->dst + linesize * y;
271 for (x = 0; x < width; x++) {
273 ptr[x] = av_expr_eval(geq->e[plane], values, geq);
279 for (y = slice_start; y < slice_end; y++) {
280 ptr16 = geq->dst16 + (linesize/2) * y;
282 for (x = 0; x < width; x++) {
284 ptr16[x] = av_expr_eval(geq->e[plane], values, geq);
292 static int geq_filter_frame(AVFilterLink *inlink, AVFrame *in)
295 AVFilterContext *ctx = inlink->dst;
296 const int nb_threads = ff_filter_get_nb_threads(ctx);
297 GEQContext *geq = ctx->priv;
298 AVFilterLink *outlink = inlink->dst->outputs[0];
301 geq->values[VAR_N] = inlink->frame_count_out,
302 geq->values[VAR_T] = in->pts == AV_NOPTS_VALUE ? NAN : in->pts * av_q2d(inlink->time_base),
305 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
308 return AVERROR(ENOMEM);
310 av_frame_copy_props(out, in);
312 for (plane = 0; plane < geq->planes && out->data[plane]; plane++) {
313 const int width = (plane == 1 || plane == 2) ? AV_CEIL_RSHIFT(inlink->w, geq->hsub) : inlink->w;
314 const int height = (plane == 1 || plane == 2) ? AV_CEIL_RSHIFT(inlink->h, geq->vsub) : inlink->h;
315 const int linesize = out->linesize[plane];
318 geq->dst = out->data[plane];
319 geq->dst16 = (uint16_t*)out->data[plane];
321 geq->values[VAR_W] = width;
322 geq->values[VAR_H] = height;
323 geq->values[VAR_SW] = width / (double)inlink->w;
324 geq->values[VAR_SH] = height / (double)inlink->h;
329 td.linesize = linesize;
331 ctx->internal->execute(ctx, slice_geq_filter, &td, NULL, FFMIN(height, nb_threads));
334 av_frame_free(&geq->picref);
335 return ff_filter_frame(outlink, out);
338 static av_cold void geq_uninit(AVFilterContext *ctx)
341 GEQContext *geq = ctx->priv;
343 for (i = 0; i < FF_ARRAY_ELEMS(geq->e); i++)
344 av_expr_free(geq->e[i]);
347 static const AVFilterPad geq_inputs[] = {
350 .type = AVMEDIA_TYPE_VIDEO,
351 .config_props = geq_config_props,
352 .filter_frame = geq_filter_frame,
357 static const AVFilterPad geq_outputs[] = {
360 .type = AVMEDIA_TYPE_VIDEO,
365 AVFilter ff_vf_geq = {
367 .description = NULL_IF_CONFIG_SMALL("Apply generic equation to each pixel."),
368 .priv_size = sizeof(GEQContext),
370 .uninit = geq_uninit,
371 .query_formats = geq_query_formats,
372 .inputs = geq_inputs,
373 .outputs = geq_outputs,
374 .priv_class = &geq_class,
375 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,