2 * Copyright (C) 2006 Michael Niedermayer <michaelni@gmx.at>
3 * Copyright (C) 2012 Clément Bœsch <ubitux@gmail.com>
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/avstring.h"
30 #include "libavutil/eval.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/pixdesc.h"
37 AVExpr *e[3]; ///< expressions for each plane
38 char *expr_str[3]; ///< expression strings for each plane
39 int framenum; ///< frame counter
40 AVFilterBufferRef *picref; ///< current input buffer
41 int hsub, vsub; ///< chroma subsampling
44 #define OFFSET(x) offsetof(GEQContext, x)
45 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
47 static const AVOption geq_options[] = {
48 { "lum_expr", "set luminance expression", OFFSET(expr_str), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
49 { "cb_expr", "set chroma blue expression", OFFSET(expr_str) + sizeof(char*), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
50 { "cr_expr", "set chroma red expression", OFFSET(expr_str) + 2*sizeof(char*), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
54 AVFILTER_DEFINE_CLASS(geq);
56 static inline double getpix(void *priv, double x, double y, int plane)
59 GEQContext *geq = priv;
60 AVFilterBufferRef *picref = geq->picref;
61 const uint8_t *src = picref->data[plane];
62 const int linesize = picref->linesize[plane];
63 const int w = picref->video->w >> (plane ? geq->hsub : 0);
64 const int h = picref->video->h >> (plane ? geq->vsub : 0);
66 xi = x = av_clipf(x, 0, w - 2);
67 yi = y = av_clipf(y, 0, h - 2);
72 return (1-y)*((1-x)*src[xi + yi * linesize] + x*src[xi + 1 + yi * linesize])
73 + y *((1-x)*src[xi + (yi+1) * linesize] + x*src[xi + 1 + (yi+1) * linesize]);
76 //TODO: cubic interpolate
77 //TODO: keep the last few frames
78 static double lum(void *priv, double x, double y) { return getpix(priv, x, y, 0); }
79 static double cb(void *priv, double x, double y) { return getpix(priv, x, y, 1); }
80 static double cr(void *priv, double x, double y) { return getpix(priv, x, y, 2); }
82 static const char *const var_names[] = { "X", "Y", "W", "H", "N", "SW", "SH", "T", NULL };
83 enum { VAR_X, VAR_Y, VAR_W, VAR_H, VAR_N, VAR_SW, VAR_SH, VAR_T, VAR_VARS_NB };
85 static av_cold int geq_init(AVFilterContext *ctx, const char *args)
87 GEQContext *geq = ctx->priv;
89 static const char *shorthand[] = { "lum_expr", "cb_expr", "cr_expr", NULL };
91 geq->class = &geq_class;
92 av_opt_set_defaults(geq);
94 if ((ret = av_opt_set_from_string(geq, args, shorthand, "=", ":")) < 0)
97 if (!geq->expr_str[0]) {
98 av_log(ctx, AV_LOG_ERROR, "Luminance expression is mandatory\n");
99 ret = AVERROR(EINVAL);
103 if (!geq->expr_str[1] && !geq->expr_str[2]) {
104 /* No chroma at all: fallback on luma */
105 geq->expr_str[1] = av_strdup(geq->expr_str[0]);
106 geq->expr_str[2] = av_strdup(geq->expr_str[0]);
108 /* One chroma unspecified, fallback on the other */
109 if (!geq->expr_str[1]) geq->expr_str[1] = av_strdup(geq->expr_str[2]);
110 if (!geq->expr_str[2]) geq->expr_str[2] = av_strdup(geq->expr_str[1]);
113 if (!geq->expr_str[1] || !geq->expr_str[2]) {
114 ret = AVERROR(ENOMEM);
118 for (plane = 0; plane < 3; plane++) {
119 static double (*p[])(void *, double, double) = { lum, cb, cr };
120 static const char *const func2_names[] = { "lum", "cb", "cr", "p", NULL };
121 double (*func2[])(void *, double, double) = { lum, cb, cr, p[plane], NULL };
123 ret = av_expr_parse(&geq->e[plane], geq->expr_str[plane], var_names,
124 NULL, NULL, func2_names, func2, 0, ctx);
133 static int geq_query_formats(AVFilterContext *ctx)
135 static const enum PixelFormat pix_fmts[] = {
136 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
137 AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
141 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
145 static int geq_config_props(AVFilterLink *inlink)
147 GEQContext *geq = inlink->dst->priv;
148 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
150 geq->hsub = desc->log2_chroma_w;
151 geq->vsub = desc->log2_chroma_h;
155 static int geq_filter_frame(AVFilterLink *inlink, AVFilterBufferRef *in)
158 GEQContext *geq = inlink->dst->priv;
159 AVFilterLink *outlink = inlink->dst->outputs[0];
160 AVFilterBufferRef *out;
161 double values[VAR_VARS_NB] = {
162 [VAR_N] = geq->framenum++,
163 [VAR_T] = in->pts == AV_NOPTS_VALUE ? NAN : in->pts * av_q2d(inlink->time_base),
167 out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
169 avfilter_unref_bufferp(&in);
170 return AVERROR(ENOMEM);
172 avfilter_copy_buffer_ref_props(out, in);
174 for (plane = 0; plane < 3; plane++) {
176 uint8_t *dst = out->data[plane];
177 const int linesize = out->linesize[plane];
178 const int w = inlink->w >> (plane ? geq->hsub : 0);
179 const int h = inlink->h >> (plane ? geq->vsub : 0);
183 values[VAR_SW] = w / (double)inlink->w;
184 values[VAR_SH] = h / (double)inlink->h;
186 for (y = 0; y < h; y++) {
188 for (x = 0; x < w; x++) {
190 dst[x] = av_expr_eval(geq->e[plane], values, geq);
196 avfilter_unref_bufferp(&geq->picref);
197 return ff_filter_frame(outlink, out);
200 static av_cold void geq_uninit(AVFilterContext *ctx)
203 GEQContext *geq = ctx->priv;
205 for (i = 0; i < FF_ARRAY_ELEMS(geq->e); i++)
206 av_expr_free(geq->e[i]);
210 static const AVFilterPad geq_inputs[] = {
213 .type = AVMEDIA_TYPE_VIDEO,
214 .config_props = geq_config_props,
215 .filter_frame = geq_filter_frame,
216 .min_perms = AV_PERM_READ,
221 static const AVFilterPad geq_outputs[] = {
224 .type = AVMEDIA_TYPE_VIDEO,
229 AVFilter avfilter_vf_geq = {
231 .description = NULL_IF_CONFIG_SMALL("Apply generic equation to each pixel."),
232 .priv_size = sizeof(GEQContext),
234 .uninit = geq_uninit,
235 .query_formats = geq_query_formats,
236 .inputs = geq_inputs,
237 .outputs = geq_outputs,
238 .priv_class = &geq_class,