FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_vignette.c
Date: 2024-04-26 14:42:52
Exec Total Coverage
Lines: 0 137 0.0%
Functions: 0 7 0.0%
Branches: 0 80 0.0%

Line Branch Exec Source
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 <float.h> /* DBL_MAX */
22
23 #include "libavutil/mem.h"
24 #include "libavutil/opt.h"
25 #include "libavutil/eval.h"
26 #include "libavutil/pixdesc.h"
27 #include "avfilter.h"
28 #include "internal.h"
29 #include "video.h"
30
31 static const char *const var_names[] = {
32 "w", // stream width
33 "h", // stream height
34 "n", // frame count
35 "pts", // presentation timestamp expressed in AV_TIME_BASE units
36 "r", // frame rate
37 "t", // timestamp expressed in seconds
38 "tb", // timebase
39 NULL
40 };
41
42 enum var_name {
43 VAR_W,
44 VAR_H,
45 VAR_N,
46 VAR_PTS,
47 VAR_R,
48 VAR_T,
49 VAR_TB,
50 VAR_NB
51 };
52
53 enum EvalMode {
54 EVAL_MODE_INIT,
55 EVAL_MODE_FRAME,
56 EVAL_MODE_NB
57 };
58
59 typedef struct VignetteContext {
60 const AVClass *class;
61 const AVPixFmtDescriptor *desc;
62 int backward;
63 int eval_mode; ///< EvalMode
64 #define DEF_EXPR_FIELDS(name) AVExpr *name##_pexpr; char *name##_expr; double name
65 DEF_EXPR_FIELDS(angle);
66 DEF_EXPR_FIELDS(x0);
67 DEF_EXPR_FIELDS(y0);
68 double var_values[VAR_NB];
69 float *fmap;
70 int fmap_linesize;
71 double dmax;
72 float xscale, yscale;
73 uint32_t dither;
74 int do_dither;
75 AVRational aspect;
76 AVRational scale;
77 } VignetteContext;
78
79 #define OFFSET(x) offsetof(VignetteContext, x)
80 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
81 static const AVOption vignette_options[] = {
82 { "angle", "set lens angle", OFFSET(angle_expr), AV_OPT_TYPE_STRING, {.str="PI/5"}, .flags = FLAGS },
83 { "a", "set lens angle", OFFSET(angle_expr), AV_OPT_TYPE_STRING, {.str="PI/5"}, .flags = FLAGS },
84 { "x0", "set circle center position on x-axis", OFFSET(x0_expr), AV_OPT_TYPE_STRING, {.str="w/2"}, .flags = FLAGS },
85 { "y0", "set circle center position on y-axis", OFFSET(y0_expr), AV_OPT_TYPE_STRING, {.str="h/2"}, .flags = FLAGS },
86 { "mode", "set forward/backward mode", OFFSET(backward), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, FLAGS, .unit = "mode" },
87 { "forward", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 0}, INT_MIN, INT_MAX, FLAGS, .unit = "mode"},
88 { "backward", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 1}, INT_MIN, INT_MAX, FLAGS, .unit = "mode"},
89 { "eval", "specify when to evaluate expressions", OFFSET(eval_mode), AV_OPT_TYPE_INT, {.i64 = EVAL_MODE_INIT}, 0, EVAL_MODE_NB-1, FLAGS, .unit = "eval" },
90 { "init", "eval expressions once during initialization", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_INIT}, .flags = FLAGS, .unit = "eval" },
91 { "frame", "eval expressions for each frame", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_FRAME}, .flags = FLAGS, .unit = "eval" },
92 { "dither", "set dithering", OFFSET(do_dither), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, FLAGS },
93 { "aspect", "set aspect ratio", OFFSET(aspect), AV_OPT_TYPE_RATIONAL, {.dbl = 1}, 0, DBL_MAX, .flags = FLAGS },
94 { NULL }
95 };
96
97 AVFILTER_DEFINE_CLASS(vignette);
98
99 static av_cold int init(AVFilterContext *ctx)
100 {
101 VignetteContext *s = ctx->priv;
102
103 #define PARSE_EXPR(name) do { \
104 int ret = av_expr_parse(&s->name##_pexpr, s->name##_expr, var_names, \
105 NULL, NULL, NULL, NULL, 0, ctx); \
106 if (ret < 0) { \
107 av_log(ctx, AV_LOG_ERROR, "Unable to parse expression for '" \
108 AV_STRINGIFY(name) "'\n"); \
109 return ret; \
110 } \
111 } while (0)
112
113 PARSE_EXPR(angle);
114 PARSE_EXPR(x0);
115 PARSE_EXPR(y0);
116 return 0;
117 }
118
119 static av_cold void uninit(AVFilterContext *ctx)
120 {
121 VignetteContext *s = ctx->priv;
122 av_freep(&s->fmap);
123 av_expr_free(s->angle_pexpr);
124 av_expr_free(s->x0_pexpr);
125 av_expr_free(s->y0_pexpr);
126 }
127
128 static const enum AVPixelFormat pix_fmts[] = {
129 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P,
130 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P,
131 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
132 AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
133 AV_PIX_FMT_GRAY8,
134 AV_PIX_FMT_NONE
135 };
136
137 static double get_natural_factor(const VignetteContext *s, int x, int y)
138 {
139 const int xx = (x - s->x0) * s->xscale;
140 const int yy = (y - s->y0) * s->yscale;
141 const double dnorm = hypot(xx, yy) / s->dmax;
142 if (dnorm > 1) {
143 return 0;
144 } else {
145 const double c = cos(s->angle * dnorm);
146 return (c*c)*(c*c); // do not remove braces, it helps compilers
147 }
148 }
149
150 static void update_context(VignetteContext *s, AVFilterLink *inlink, AVFrame *frame)
151 {
152 int x, y;
153 float *dst = s->fmap;
154 int dst_linesize = s->fmap_linesize;
155
156 if (frame) {
157 s->var_values[VAR_N] = inlink->frame_count_out;
158 s->var_values[VAR_T] = TS2T(frame->pts, inlink->time_base);
159 s->var_values[VAR_PTS] = TS2D(frame->pts);
160 } else {
161 s->var_values[VAR_N] = NAN;
162 s->var_values[VAR_T] = NAN;
163 s->var_values[VAR_PTS] = NAN;
164 }
165
166 s->angle = av_expr_eval(s->angle_pexpr, s->var_values, NULL);
167 s->x0 = av_expr_eval(s->x0_pexpr, s->var_values, NULL);
168 s->y0 = av_expr_eval(s->y0_pexpr, s->var_values, NULL);
169
170 if (isnan(s->x0) || isnan(s->y0) || isnan(s->angle))
171 s->eval_mode = EVAL_MODE_FRAME;
172
173 s->angle = av_clipf(s->angle, 0, M_PI_2);
174
175 if (s->backward) {
176 for (y = 0; y < inlink->h; y++) {
177 for (x = 0; x < inlink->w; x++)
178 dst[x] = 1. / get_natural_factor(s, x, y);
179 dst += dst_linesize;
180 }
181 } else {
182 for (y = 0; y < inlink->h; y++) {
183 for (x = 0; x < inlink->w; x++)
184 dst[x] = get_natural_factor(s, x, y);
185 dst += dst_linesize;
186 }
187 }
188 }
189
190 static inline double get_dither_value(VignetteContext *s)
191 {
192 double dv = 0;
193 if (s->do_dither) {
194 dv = s->dither / (double)(1LL<<32);
195 s->dither = s->dither * 1664525 + 1013904223;
196 }
197 return dv;
198 }
199
200 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
201 {
202 unsigned x, y, direct = 0;
203 AVFilterContext *ctx = inlink->dst;
204 VignetteContext *s = ctx->priv;
205 AVFilterLink *outlink = ctx->outputs[0];
206 AVFrame *out;
207
208 if (av_frame_is_writable(in)) {
209 direct = 1;
210 out = in;
211 } else {
212 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
213 if (!out) {
214 av_frame_free(&in);
215 return AVERROR(ENOMEM);
216 }
217 av_frame_copy_props(out, in);
218 }
219
220 if (s->eval_mode == EVAL_MODE_FRAME)
221 update_context(s, inlink, in);
222
223 if (s->desc->flags & AV_PIX_FMT_FLAG_RGB) {
224 uint8_t *dst = out->data[0];
225 const uint8_t *src = in ->data[0];
226 const float *fmap = s->fmap;
227 const int dst_linesize = out->linesize[0];
228 const int src_linesize = in ->linesize[0];
229 const int fmap_linesize = s->fmap_linesize;
230
231 for (y = 0; y < inlink->h; y++) {
232 uint8_t *dstp = dst;
233 const uint8_t *srcp = src;
234
235 for (x = 0; x < inlink->w; x++, dstp += 3, srcp += 3) {
236 const float f = fmap[x];
237
238 dstp[0] = av_clip_uint8(srcp[0] * f + get_dither_value(s));
239 dstp[1] = av_clip_uint8(srcp[1] * f + get_dither_value(s));
240 dstp[2] = av_clip_uint8(srcp[2] * f + get_dither_value(s));
241 }
242 dst += dst_linesize;
243 src += src_linesize;
244 fmap += fmap_linesize;
245 }
246 } else {
247 int plane;
248
249 for (plane = 0; plane < 4 && in->data[plane] && in->linesize[plane]; plane++) {
250 uint8_t *dst = out->data[plane];
251 const uint8_t *src = in ->data[plane];
252 const float *fmap = s->fmap;
253 const int dst_linesize = out->linesize[plane];
254 const int src_linesize = in ->linesize[plane];
255 const int fmap_linesize = s->fmap_linesize;
256 const int chroma = plane == 1 || plane == 2;
257 const int hsub = chroma ? s->desc->log2_chroma_w : 0;
258 const int vsub = chroma ? s->desc->log2_chroma_h : 0;
259 const int w = AV_CEIL_RSHIFT(inlink->w, hsub);
260 const int h = AV_CEIL_RSHIFT(inlink->h, vsub);
261
262 for (y = 0; y < h; y++) {
263 uint8_t *dstp = dst;
264 const uint8_t *srcp = src;
265
266 for (x = 0; x < w; x++) {
267 const double dv = get_dither_value(s);
268 if (chroma) *dstp++ = av_clip_uint8(fmap[x << hsub] * (*srcp++ - 127) + 127 + dv);
269 else *dstp++ = av_clip_uint8(fmap[x ] * *srcp++ + dv);
270 }
271 dst += dst_linesize;
272 src += src_linesize;
273 fmap += fmap_linesize << vsub;
274 }
275 }
276 }
277
278 if (!direct)
279 av_frame_free(&in);
280 return ff_filter_frame(outlink, out);
281 }
282
283 static int config_props(AVFilterLink *inlink)
284 {
285 VignetteContext *s = inlink->dst->priv;
286 AVRational sar = inlink->sample_aspect_ratio;
287
288 s->desc = av_pix_fmt_desc_get(inlink->format);
289 s->var_values[VAR_W] = inlink->w;
290 s->var_values[VAR_H] = inlink->h;
291 s->var_values[VAR_TB] = av_q2d(inlink->time_base);
292 s->var_values[VAR_R] = inlink->frame_rate.num == 0 || inlink->frame_rate.den == 0 ?
293 NAN : av_q2d(inlink->frame_rate);
294
295 if (!sar.num || !sar.den)
296 sar.num = sar.den = 1;
297 if (sar.num > sar.den) {
298 s->xscale = av_q2d(av_div_q(sar, s->aspect));
299 s->yscale = 1;
300 } else {
301 s->yscale = av_q2d(av_div_q(s->aspect, sar));
302 s->xscale = 1;
303 }
304 s->dmax = hypot(inlink->w / 2., inlink->h / 2.);
305 av_log(s, AV_LOG_DEBUG, "xscale=%f yscale=%f dmax=%f\n",
306 s->xscale, s->yscale, s->dmax);
307
308 s->fmap_linesize = FFALIGN(inlink->w, 32);
309 s->fmap = av_malloc_array(s->fmap_linesize, inlink->h * sizeof(*s->fmap));
310 if (!s->fmap)
311 return AVERROR(ENOMEM);
312
313 if (s->eval_mode == EVAL_MODE_INIT)
314 update_context(s, inlink, NULL);
315
316 return 0;
317 }
318
319 static const AVFilterPad vignette_inputs[] = {
320 {
321 .name = "default",
322 .type = AVMEDIA_TYPE_VIDEO,
323 .filter_frame = filter_frame,
324 .config_props = config_props,
325 },
326 };
327
328 const AVFilter ff_vf_vignette = {
329 .name = "vignette",
330 .description = NULL_IF_CONFIG_SMALL("Make or reverse a vignette effect."),
331 .priv_size = sizeof(VignetteContext),
332 .init = init,
333 .uninit = uninit,
334 FILTER_INPUTS(vignette_inputs),
335 FILTER_OUTPUTS(ff_video_default_filterpad),
336 FILTER_PIXFMTS_ARRAY(pix_fmts),
337 .priv_class = &vignette_class,
338 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
339 };
340