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 <float.h> /* DBL_MAX */
23 #include "libavutil/opt.h"
24 #include "libavutil/eval.h"
25 #include "libavutil/avassert.h"
26 #include "libavutil/pixdesc.h"
32 static const char *const var_names[] = {
36 "pts", // presentation timestamp expressed in AV_TIME_BASE units
38 "t", // timestamp expressed in seconds
60 typedef struct VignetteContext {
62 const AVPixFmtDescriptor *desc;
64 int eval_mode; ///< EvalMode
65 #define DEF_EXPR_FIELDS(name) AVExpr *name##_pexpr; char *name##_expr; double name
66 DEF_EXPR_FIELDS(angle);
69 double var_values[VAR_NB];
80 #define OFFSET(x) offsetof(VignetteContext, x)
81 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
82 static const AVOption vignette_options[] = {
83 { "angle", "set lens angle", OFFSET(angle_expr), AV_OPT_TYPE_STRING, {.str="PI/5"}, .flags = FLAGS },
84 { "a", "set lens angle", OFFSET(angle_expr), AV_OPT_TYPE_STRING, {.str="PI/5"}, .flags = FLAGS },
85 { "x0", "set circle center position on x-axis", OFFSET(x0_expr), AV_OPT_TYPE_STRING, {.str="w/2"}, .flags = FLAGS },
86 { "y0", "set circle center position on y-axis", OFFSET(y0_expr), AV_OPT_TYPE_STRING, {.str="h/2"}, .flags = FLAGS },
87 { "mode", "set forward/backward mode", OFFSET(backward), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, FLAGS, "mode" },
88 { "forward", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 0}, INT_MIN, INT_MAX, FLAGS, "mode"},
89 { "backward", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 1}, INT_MIN, INT_MAX, FLAGS, "mode"},
90 { "eval", "specify when to evaluate expressions", OFFSET(eval_mode), AV_OPT_TYPE_INT, {.i64 = EVAL_MODE_INIT}, 0, EVAL_MODE_NB-1, FLAGS, "eval" },
91 { "init", "eval expressions once during initialization", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_INIT}, .flags = FLAGS, .unit = "eval" },
92 { "frame", "eval expressions for each frame", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_FRAME}, .flags = FLAGS, .unit = "eval" },
93 { "dither", "set dithering", OFFSET(do_dither), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, FLAGS },
94 { "aspect", "set aspect ratio", OFFSET(aspect), AV_OPT_TYPE_RATIONAL, {.dbl = 1}, 0, DBL_MAX, .flags = FLAGS },
98 AVFILTER_DEFINE_CLASS(vignette);
100 static av_cold int init(AVFilterContext *ctx)
102 VignetteContext *s = ctx->priv;
104 #define PARSE_EXPR(name) do { \
105 int ret = av_expr_parse(&s->name##_pexpr, s->name##_expr, var_names, \
106 NULL, NULL, NULL, NULL, 0, ctx); \
108 av_log(ctx, AV_LOG_ERROR, "Unable to parse expression for '" \
109 AV_STRINGIFY(name) "'\n"); \
120 static av_cold void uninit(AVFilterContext *ctx)
122 VignetteContext *s = ctx->priv;
124 av_expr_free(s->angle_pexpr);
125 av_expr_free(s->x0_pexpr);
126 av_expr_free(s->y0_pexpr);
129 static int query_formats(AVFilterContext *ctx)
131 static const enum AVPixelFormat pix_fmts[] = {
132 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P,
133 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P,
134 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
135 AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
139 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
141 return AVERROR(ENOMEM);
142 return ff_set_common_formats(ctx, fmts_list);
145 static double get_natural_factor(const VignetteContext *s, int x, int y)
147 const int xx = (x - s->x0) * s->xscale;
148 const int yy = (y - s->y0) * s->yscale;
149 const double dnorm = hypot(xx, yy) / s->dmax;
153 const double c = cos(s->angle * dnorm);
154 return (c*c)*(c*c); // do not remove braces, it helps compilers
158 static void update_context(VignetteContext *s, AVFilterLink *inlink, AVFrame *frame)
161 float *dst = s->fmap;
162 int dst_linesize = s->fmap_linesize;
165 s->var_values[VAR_N] = inlink->frame_count_out;
166 s->var_values[VAR_T] = TS2T(frame->pts, inlink->time_base);
167 s->var_values[VAR_PTS] = TS2D(frame->pts);
169 s->var_values[VAR_N] = NAN;
170 s->var_values[VAR_T] = NAN;
171 s->var_values[VAR_PTS] = NAN;
174 s->angle = av_expr_eval(s->angle_pexpr, s->var_values, NULL);
175 s->x0 = av_expr_eval(s->x0_pexpr, s->var_values, NULL);
176 s->y0 = av_expr_eval(s->y0_pexpr, s->var_values, NULL);
178 if (isnan(s->x0) || isnan(s->y0) || isnan(s->angle))
179 s->eval_mode = EVAL_MODE_FRAME;
181 s->angle = av_clipf(s->angle, 0, M_PI_2);
184 for (y = 0; y < inlink->h; y++) {
185 for (x = 0; x < inlink->w; x++)
186 dst[x] = 1. / get_natural_factor(s, x, y);
190 for (y = 0; y < inlink->h; y++) {
191 for (x = 0; x < inlink->w; x++)
192 dst[x] = get_natural_factor(s, x, y);
198 static inline double get_dither_value(VignetteContext *s)
202 dv = s->dither / (double)(1LL<<32);
203 s->dither = s->dither * 1664525 + 1013904223;
208 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
210 unsigned x, y, direct = 0;
211 AVFilterContext *ctx = inlink->dst;
212 VignetteContext *s = ctx->priv;
213 AVFilterLink *outlink = ctx->outputs[0];
216 if (av_frame_is_writable(in)) {
220 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
223 return AVERROR(ENOMEM);
225 av_frame_copy_props(out, in);
228 if (s->eval_mode == EVAL_MODE_FRAME)
229 update_context(s, inlink, in);
231 if (s->desc->flags & AV_PIX_FMT_FLAG_RGB) {
232 uint8_t *dst = out->data[0];
233 const uint8_t *src = in ->data[0];
234 const float *fmap = s->fmap;
235 const int dst_linesize = out->linesize[0];
236 const int src_linesize = in ->linesize[0];
237 const int fmap_linesize = s->fmap_linesize;
239 for (y = 0; y < inlink->h; y++) {
241 const uint8_t *srcp = src;
243 for (x = 0; x < inlink->w; x++, dstp += 3, srcp += 3) {
244 const float f = fmap[x];
246 dstp[0] = av_clip_uint8(srcp[0] * f + get_dither_value(s));
247 dstp[1] = av_clip_uint8(srcp[1] * f + get_dither_value(s));
248 dstp[2] = av_clip_uint8(srcp[2] * f + get_dither_value(s));
252 fmap += fmap_linesize;
257 for (plane = 0; plane < 4 && in->data[plane] && in->linesize[plane]; plane++) {
258 uint8_t *dst = out->data[plane];
259 const uint8_t *src = in ->data[plane];
260 const float *fmap = s->fmap;
261 const int dst_linesize = out->linesize[plane];
262 const int src_linesize = in ->linesize[plane];
263 const int fmap_linesize = s->fmap_linesize;
264 const int chroma = plane == 1 || plane == 2;
265 const int hsub = chroma ? s->desc->log2_chroma_w : 0;
266 const int vsub = chroma ? s->desc->log2_chroma_h : 0;
267 const int w = AV_CEIL_RSHIFT(inlink->w, hsub);
268 const int h = AV_CEIL_RSHIFT(inlink->h, vsub);
270 for (y = 0; y < h; y++) {
272 const uint8_t *srcp = src;
274 for (x = 0; x < w; x++) {
275 const double dv = get_dither_value(s);
276 if (chroma) *dstp++ = av_clip_uint8(fmap[x << hsub] * (*srcp++ - 127) + 127 + dv);
277 else *dstp++ = av_clip_uint8(fmap[x ] * *srcp++ + dv);
281 fmap += fmap_linesize << vsub;
288 return ff_filter_frame(outlink, out);
291 static int config_props(AVFilterLink *inlink)
293 VignetteContext *s = inlink->dst->priv;
294 AVRational sar = inlink->sample_aspect_ratio;
296 s->desc = av_pix_fmt_desc_get(inlink->format);
297 s->var_values[VAR_W] = inlink->w;
298 s->var_values[VAR_H] = inlink->h;
299 s->var_values[VAR_TB] = av_q2d(inlink->time_base);
300 s->var_values[VAR_R] = inlink->frame_rate.num == 0 || inlink->frame_rate.den == 0 ?
301 NAN : av_q2d(inlink->frame_rate);
303 if (!sar.num || !sar.den)
304 sar.num = sar.den = 1;
305 if (sar.num > sar.den) {
306 s->xscale = av_q2d(av_div_q(sar, s->aspect));
309 s->yscale = av_q2d(av_div_q(s->aspect, sar));
312 s->dmax = hypot(inlink->w / 2., inlink->h / 2.);
313 av_log(s, AV_LOG_DEBUG, "xscale=%f yscale=%f dmax=%f\n",
314 s->xscale, s->yscale, s->dmax);
316 s->fmap_linesize = FFALIGN(inlink->w, 32);
317 s->fmap = av_malloc_array(s->fmap_linesize, inlink->h * sizeof(*s->fmap));
319 return AVERROR(ENOMEM);
321 if (s->eval_mode == EVAL_MODE_INIT)
322 update_context(s, inlink, NULL);
327 static const AVFilterPad vignette_inputs[] = {
330 .type = AVMEDIA_TYPE_VIDEO,
331 .filter_frame = filter_frame,
332 .config_props = config_props,
337 static const AVFilterPad vignette_outputs[] = {
340 .type = AVMEDIA_TYPE_VIDEO,
345 const AVFilter ff_vf_vignette = {
347 .description = NULL_IF_CONFIG_SMALL("Make or reverse a vignette effect."),
348 .priv_size = sizeof(VignetteContext),
351 .query_formats = query_formats,
352 .inputs = vignette_inputs,
353 .outputs = vignette_outputs,
354 .priv_class = &vignette_class,
355 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,