]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_vignette.c
lavfi/vignette: make sure a sane sar is set.
[ffmpeg] / libavfilter / vf_vignette.c
1 /*
2  * Copyright (c) 2013 Clément Bœsch
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "libavutil/opt.h"
22 #include "libavutil/eval.h"
23 #include "libavutil/avassert.h"
24 #include "libavutil/pixdesc.h"
25 #include "avfilter.h"
26 #include "formats.h"
27 #include "internal.h"
28 #include "video.h"
29
30 static const char *const var_names[] = {
31     "w",    // stream width
32     "h",    // stream height
33     "n",    // frame count
34     "pts",  // presentation timestamp expressed in AV_TIME_BASE units
35     "r",    // frame rate
36     "t",    // timestamp expressed in seconds
37     "tb",   // timebase
38     NULL
39 };
40
41 enum var_name {
42     VAR_W,
43     VAR_H,
44     VAR_N,
45     VAR_PTS,
46     VAR_R,
47     VAR_T,
48     VAR_TB,
49     VAR_NB
50 };
51
52 typedef struct {
53     const AVClass *class;
54     const AVPixFmtDescriptor *desc;
55     int backward;
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);
59     DEF_EXPR_FIELDS(x0);
60     DEF_EXPR_FIELDS(y0);
61     double var_values[VAR_NB];
62     float *fmap;
63     int fmap_linesize;
64     double dmax;
65     float xscale, yscale;
66     uint32_t dither;
67     int do_dither;
68 } VignetteContext;
69
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 },
84     { NULL }
85 };
86
87 AVFILTER_DEFINE_CLASS(vignette);
88
89 static av_cold int init(AVFilterContext *ctx)
90 {
91     VignetteContext *s = ctx->priv;
92
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);                \
96     if (ret < 0) {                                                          \
97         av_log(ctx, AV_LOG_ERROR, "Unable to parse expression for '"        \
98                AV_STRINGIFY(name) "'\n");                                   \
99         return ret;                                                         \
100     }                                                                       \
101 } while (0)
102
103     PARSE_EXPR(angle);
104     PARSE_EXPR(x0);
105     PARSE_EXPR(y0);
106     return 0;
107 }
108
109 static av_cold void uninit(AVFilterContext *ctx)
110 {
111     VignetteContext *s = ctx->priv;
112     av_freep(&s->fmap);
113     av_expr_free(s->angle_pexpr);
114     av_expr_free(s->x0_pexpr);
115     av_expr_free(s->y0_pexpr);
116 }
117
118 static int query_formats(AVFilterContext *ctx)
119 {
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,
125         AV_PIX_FMT_GRAY8,
126         AV_PIX_FMT_NONE
127     };
128     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
129     return 0;
130 }
131
132 static double get_natural_factor(const VignetteContext *s, int x, int y)
133 {
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;
137     if (dnorm > 1) {
138         return 0;
139     } else {
140         const double c = cos(s->angle * dnorm);
141         return (c*c)*(c*c); // do not remove braces, it helps compilers
142     }
143 }
144
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))
147
148 static void update_context(VignetteContext *s, AVFilterLink *inlink, AVFrame *frame)
149 {
150     int x, y;
151     float *dst = s->fmap;
152     int dst_linesize = s->fmap_linesize;
153
154     if (frame) {
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);
158     } else {
159         s->var_values[VAR_N]   = 0;
160         s->var_values[VAR_T]   = NAN;
161         s->var_values[VAR_PTS] = NAN;
162     }
163
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);
167
168     if (s->backward) {
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);
172             dst += dst_linesize;
173         }
174     } else {
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);
178             dst += dst_linesize;
179         }
180     }
181 }
182
183 static inline double get_dither_value(VignetteContext *s)
184 {
185     double dv = 0;
186     if (s->do_dither) {
187         dv = s->dither / (double)(1LL<<32);
188         s->dither = s->dither * 1664525 + 1013904223;
189     }
190     return dv;
191 }
192
193 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
194 {
195     unsigned x, y;
196     AVFilterContext *ctx = inlink->dst;
197     VignetteContext *s = ctx->priv;
198     AVFilterLink *outlink = inlink->dst->outputs[0];
199     AVFrame *out;
200
201     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
202     if (!out) {
203         av_frame_free(&in);
204         return AVERROR(ENOMEM);
205     }
206     av_frame_copy_props(out, in);
207
208     if (s->eval_mode == EVAL_MODE_FRAME)
209         update_context(s, inlink, in);
210
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;
218
219         for (y = 0; y < inlink->h; y++) {
220             uint8_t       *dstp = dst;
221             const uint8_t *srcp = src;
222
223             for (x = 0; x < inlink->w; x++, dstp += 3, srcp += 3) {
224                 const float f = fmap[x];
225
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));
229             }
230             dst += dst_linesize;
231             src += src_linesize;
232             fmap += fmap_linesize;
233         }
234     } else {
235         int plane;
236
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);
249
250             for (y = 0; y < h; y++) {
251                 uint8_t *dstp = dst;
252                 const uint8_t *srcp = src;
253
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);
258                 }
259                 dst += dst_linesize;
260                 src += src_linesize;
261                 fmap += fmap_linesize << vsub;
262             }
263         }
264     }
265
266     return ff_filter_frame(outlink, out);
267 }
268
269 static int config_props(AVFilterLink *inlink)
270 {
271     VignetteContext *s = inlink->dst->priv;
272     AVRational sar = inlink->sample_aspect_ratio;
273
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);
280
281     if (!sar.num || !sar.den)
282         sar.num = sar.den = 1;
283     if (sar.num > sar.den) {
284         s->xscale = av_q2d(sar);
285         s->yscale = 1;
286         s->dmax = hypot(inlink->w / 2., s->yscale * inlink->h / 2.);
287     } else {
288         s->yscale = av_q2d(sar);
289         s->xscale = 1;
290         s->dmax = hypot(s->xscale * inlink->w / 2., inlink->h / 2.);
291     }
292
293     s->fmap_linesize = FFALIGN(inlink->w, 32);
294     s->fmap = av_malloc(s->fmap_linesize * inlink->h * sizeof(*s->fmap));
295     if (!s->fmap)
296         return AVERROR(ENOMEM);
297
298     if (s->eval_mode == EVAL_MODE_INIT)
299         update_context(s, inlink, NULL);
300
301     return 0;
302 }
303
304 static const AVFilterPad vignette_inputs[] = {
305     {
306         .name         = "default",
307         .type         = AVMEDIA_TYPE_VIDEO,
308         .filter_frame = filter_frame,
309         .config_props = config_props,
310     },
311     { NULL }
312 };
313
314 static const AVFilterPad vignette_outputs[] = {
315      {
316          .name = "default",
317          .type = AVMEDIA_TYPE_VIDEO,
318      },
319      { NULL }
320 };
321
322 AVFilter avfilter_vf_vignette = {
323     .name          = "vignette",
324     .description   = NULL_IF_CONFIG_SMALL("Make or reverse a vignette effect."),
325     .priv_size     = sizeof(VignetteContext),
326     .init          = init,
327     .uninit        = uninit,
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,
333 };