2 * Copyright (c) 2013 Clément Bœsch
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "libavutil/opt.h"
22 #include "libavutil/eval.h"
23 #include "libavutil/avassert.h"
24 #include "libavutil/pixdesc.h"
30 static const char *const var_names[] = {
34 "pts", // presentation timestamp expressed in AV_TIME_BASE units
36 "t", // timestamp expressed in seconds
54 const AVPixFmtDescriptor *desc;
56 enum EvalMode { EVAL_MODE_INIT, EVAL_MODE_FRAME, EVAL_MODE_NB } eval_mode;
57 #define DEF_EXPR_FIELDS(name) AVExpr *name##_pexpr; char *name##_expr; double name;
58 DEF_EXPR_FIELDS(angle);
61 double var_values[VAR_NB];
70 #define OFFSET(x) offsetof(VignetteContext, x)
71 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
72 static const AVOption vignette_options[] = {
73 { "angle", "set lens angle", OFFSET(angle_expr), AV_OPT_TYPE_STRING, {.str="PI/5"}, .flags = FLAGS },
74 { "a", "set lens angle", OFFSET(angle_expr), AV_OPT_TYPE_STRING, {.str="PI/5"}, .flags = FLAGS },
75 { "x0", "set circle center position on x-axis", OFFSET(x0_expr), AV_OPT_TYPE_STRING, {.str="w/2"}, .flags = FLAGS },
76 { "y0", "set circle center position on y-axis", OFFSET(y0_expr), AV_OPT_TYPE_STRING, {.str="h/2"}, .flags = FLAGS },
77 { "mode", "set forward/backward mode", OFFSET(backward), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, FLAGS, "mode" },
78 { "forward", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 0}, INT_MIN, INT_MAX, FLAGS, "mode"},
79 { "backward", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 1}, INT_MIN, INT_MAX, FLAGS, "mode"},
80 { "eval", "specify when to evaluate expressions", OFFSET(eval_mode), AV_OPT_TYPE_INT, {.i64 = EVAL_MODE_INIT}, 0, EVAL_MODE_NB-1, FLAGS, "eval" },
81 { "init", "eval expressions once during initialization", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_INIT}, .flags = FLAGS, .unit = "eval" },
82 { "frame", "eval expressions for each frame", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_FRAME}, .flags = FLAGS, .unit = "eval" },
83 { "dither", "set dithering", OFFSET(do_dither), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, FLAGS },
87 AVFILTER_DEFINE_CLASS(vignette);
89 static av_cold int init(AVFilterContext *ctx)
91 VignetteContext *s = ctx->priv;
93 #define PARSE_EXPR(name) do { \
94 int ret = av_expr_parse(&s->name##_pexpr, s->name##_expr, var_names, \
95 NULL, NULL, NULL, NULL, 0, ctx); \
97 av_log(ctx, AV_LOG_ERROR, "Unable to parse expression for '" \
98 AV_STRINGIFY(name) "'\n"); \
109 static av_cold void uninit(AVFilterContext *ctx)
111 VignetteContext *s = ctx->priv;
113 av_expr_free(s->angle_pexpr);
114 av_expr_free(s->x0_pexpr);
115 av_expr_free(s->y0_pexpr);
118 static int query_formats(AVFilterContext *ctx)
120 static const enum AVPixelFormat pix_fmts[] = {
121 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P,
122 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P,
123 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
124 AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
128 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
132 static double get_natural_factor(const VignetteContext *s, int x, int y)
134 const int xx = (x - s->x0) * s->xscale;
135 const int yy = (y - s->y0) * s->yscale;
136 const double dnorm = hypot(xx, yy) / s->dmax;
140 const double c = cos(s->angle * dnorm);
141 return (c*c)*(c*c); // do not remove braces, it helps compilers
145 #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
146 #define TS2T(ts, tb) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts) * av_q2d(tb))
148 static void update_context(VignetteContext *s, AVFilterLink *inlink, AVFrame *frame)
151 float *dst = s->fmap;
152 int dst_linesize = s->fmap_linesize;
155 s->var_values[VAR_N] = inlink->frame_count;
156 s->var_values[VAR_T] = TS2T(frame->pts, inlink->time_base);
157 s->var_values[VAR_PTS] = TS2D(frame->pts);
159 s->var_values[VAR_N] = 0;
160 s->var_values[VAR_T] = NAN;
161 s->var_values[VAR_PTS] = NAN;
164 s->angle = av_clipf(av_expr_eval(s->angle_pexpr, s->var_values, NULL), 0, M_PI_2);
165 s->x0 = av_expr_eval(s->x0_pexpr, s->var_values, NULL);
166 s->y0 = av_expr_eval(s->y0_pexpr, s->var_values, NULL);
169 for (y = 0; y < inlink->h; y++) {
170 for (x = 0; x < inlink->w; x++)
171 dst[x] = 1. / get_natural_factor(s, x, y);
175 for (y = 0; y < inlink->h; y++) {
176 for (x = 0; x < inlink->w; x++)
177 dst[x] = get_natural_factor(s, x, y);
183 static inline double get_dither_value(VignetteContext *s)
187 dv = s->dither / (double)(1LL<<32);
188 s->dither = s->dither * 1664525 + 1013904223;
193 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
196 AVFilterContext *ctx = inlink->dst;
197 VignetteContext *s = ctx->priv;
198 AVFilterLink *outlink = inlink->dst->outputs[0];
201 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
204 return AVERROR(ENOMEM);
206 av_frame_copy_props(out, in);
208 if (s->eval_mode == EVAL_MODE_FRAME)
209 update_context(s, inlink, in);
211 if (s->desc->flags & PIX_FMT_RGB) {
212 uint8_t *dst = out->data[0];
213 const uint8_t *src = in ->data[0];
214 const float *fmap = s->fmap;
215 const int dst_linesize = out->linesize[0];
216 const int src_linesize = in ->linesize[0];
217 const int fmap_linesize = s->fmap_linesize;
219 for (y = 0; y < inlink->h; y++) {
221 const uint8_t *srcp = src;
223 for (x = 0; x < inlink->w; x++, dstp += 3, srcp += 3) {
224 const float f = fmap[x];
226 dstp[0] = av_clip_uint8(srcp[0] * f + get_dither_value(s));
227 dstp[1] = av_clip_uint8(srcp[1] * f + get_dither_value(s));
228 dstp[2] = av_clip_uint8(srcp[2] * f + get_dither_value(s));
232 fmap += fmap_linesize;
237 for (plane = 0; plane < 4 && in->data[plane]; plane++) {
238 uint8_t *dst = out->data[plane];
239 const uint8_t *src = in ->data[plane];
240 const float *fmap = s->fmap;
241 const int dst_linesize = out->linesize[plane];
242 const int src_linesize = in ->linesize[plane];
243 const int fmap_linesize = s->fmap_linesize;
244 const int chroma = plane == 1 || plane == 2;
245 const int hsub = chroma ? s->desc->log2_chroma_w : 0;
246 const int vsub = chroma ? s->desc->log2_chroma_h : 0;
247 const int w = FF_CEIL_RSHIFT(inlink->w, hsub);
248 const int h = FF_CEIL_RSHIFT(inlink->h, vsub);
250 for (y = 0; y < h; y++) {
252 const uint8_t *srcp = src;
254 for (x = 0; x < w; x++) {
255 const double dv = get_dither_value(s);
256 if (chroma) *dstp++ = av_clip_uint8(fmap[x << hsub] * (*srcp++ - 127) + 127 + dv);
257 else *dstp++ = av_clip_uint8(fmap[x ] * *srcp++ + dv);
261 fmap += fmap_linesize << vsub;
266 return ff_filter_frame(outlink, out);
269 static int config_props(AVFilterLink *inlink)
271 VignetteContext *s = inlink->dst->priv;
272 AVRational sar = inlink->sample_aspect_ratio;
274 s->desc = av_pix_fmt_desc_get(inlink->format);
275 s->var_values[VAR_W] = inlink->w;
276 s->var_values[VAR_H] = inlink->h;
277 s->var_values[VAR_TB] = av_q2d(inlink->time_base);
278 s->var_values[VAR_R] = inlink->frame_rate.num == 0 || inlink->frame_rate.den == 0 ?
279 NAN : av_q2d(inlink->frame_rate);
281 if (!sar.num || !sar.den)
282 sar.num = sar.den = 1;
283 if (sar.num > sar.den) {
284 s->xscale = av_q2d(sar);
286 s->dmax = hypot(inlink->w / 2., s->yscale * inlink->h / 2.);
288 s->yscale = av_q2d(sar);
290 s->dmax = hypot(s->xscale * inlink->w / 2., inlink->h / 2.);
293 s->fmap_linesize = FFALIGN(inlink->w, 32);
294 s->fmap = av_malloc(s->fmap_linesize * inlink->h * sizeof(*s->fmap));
296 return AVERROR(ENOMEM);
298 if (s->eval_mode == EVAL_MODE_INIT)
299 update_context(s, inlink, NULL);
304 static const AVFilterPad vignette_inputs[] = {
307 .type = AVMEDIA_TYPE_VIDEO,
308 .filter_frame = filter_frame,
309 .config_props = config_props,
314 static const AVFilterPad vignette_outputs[] = {
317 .type = AVMEDIA_TYPE_VIDEO,
322 AVFilter avfilter_vf_vignette = {
324 .description = NULL_IF_CONFIG_SMALL("Make or reverse a vignette effect."),
325 .priv_size = sizeof(VignetteContext),
328 .query_formats = query_formats,
329 .inputs = vignette_inputs,
330 .outputs = vignette_outputs,
331 .priv_class = &vignette_class,
332 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,