FFmpeg
Loading...
Searching...
No Matches
vf_vignette.c
Go to the documentation of this file.
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 "filters.h"
29#include "video.h"
30
31static 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
52
58
59typedef struct VignetteContext {
60 const AVClass *class;
63 int eval_mode; ///< EvalMode
64#define DEF_EXPR_FIELDS(name) AVExpr *name##_pexpr; char *name##_expr; double name
69 float *fmap;
71 double dmax;
72 float xscale, yscale;
73 uint32_t dither;
78
79#define OFFSET(x) offsetof(VignetteContext, x)
80#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
81static 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
98
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
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
136
137static 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
151{
152 FilterLink *inl = ff_filter_link(inlink);
153 int x, y;
154 float *dst = s->fmap;
155 int dst_linesize = s->fmap_linesize;
156
157 if (frame) {
158 s->var_values[VAR_N] = inl->frame_count_out;
159 s->var_values[VAR_T] = TS2T(frame->pts, inlink->time_base);
160 s->var_values[VAR_PTS] = TS2D(frame->pts);
161 } else {
162 s->var_values[VAR_N] = NAN;
163 s->var_values[VAR_T] = NAN;
164 s->var_values[VAR_PTS] = NAN;
165 }
166
167 s->angle = av_expr_eval(s->angle_pexpr, s->var_values, NULL);
168 s->x0 = av_expr_eval(s->x0_pexpr, s->var_values, NULL);
169 s->y0 = av_expr_eval(s->y0_pexpr, s->var_values, NULL);
170
171 if (isnan(s->x0) || isnan(s->y0) || isnan(s->angle))
172 s->eval_mode = EVAL_MODE_FRAME;
173
174 s->angle = av_clipf(s->angle, 0, M_PI_2);
175
176 if (s->backward) {
177 for (y = 0; y < inlink->h; y++) {
178 for (x = 0; x < inlink->w; x++)
179 dst[x] = 1. / get_natural_factor(s, x, y);
180 dst += dst_linesize;
181 }
182 } else {
183 for (y = 0; y < inlink->h; y++) {
184 for (x = 0; x < inlink->w; x++)
185 dst[x] = get_natural_factor(s, x, y);
186 dst += dst_linesize;
187 }
188 }
189}
190
191static inline double get_dither_value(VignetteContext *s)
192{
193 double dv = 0;
194 if (s->do_dither) {
195 dv = s->dither / (double)(1LL<<32);
196 s->dither = s->dither * 1664525 + 1013904223;
197 }
198 return dv;
199}
200
201static int filter_frame(AVFilterLink *inlink, AVFrame *in)
202{
203 unsigned x, y, direct = 0;
204 AVFilterContext *ctx = inlink->dst;
205 VignetteContext *s = ctx->priv;
206 AVFilterLink *outlink = ctx->outputs[0];
207 AVFrame *out;
208
209 if (av_frame_is_writable(in)) {
210 direct = 1;
211 out = in;
212 } else {
213 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
214 if (!out) {
215 av_frame_free(&in);
216 return AVERROR(ENOMEM);
217 }
219 }
220
221 if (s->eval_mode == EVAL_MODE_FRAME)
222 update_context(s, inlink, in);
223
224 if (s->desc->flags & AV_PIX_FMT_FLAG_RGB) {
225 uint8_t *dst = out->data[0];
226 const uint8_t *src = in ->data[0];
227 const float *fmap = s->fmap;
228 const int dst_linesize = out->linesize[0];
229 const int src_linesize = in ->linesize[0];
230 const int fmap_linesize = s->fmap_linesize;
231
232 for (y = 0; y < inlink->h; y++) {
233 uint8_t *dstp = dst;
234 const uint8_t *srcp = src;
235
236 for (x = 0; x < inlink->w; x++, dstp += 3, srcp += 3) {
237 const float f = fmap[x];
238
239 dstp[0] = av_clip_uint8(srcp[0] * f + get_dither_value(s));
240 dstp[1] = av_clip_uint8(srcp[1] * f + get_dither_value(s));
241 dstp[2] = av_clip_uint8(srcp[2] * f + get_dither_value(s));
242 }
243 dst += dst_linesize;
244 src += src_linesize;
245 fmap += fmap_linesize;
246 }
247 } else {
248 int plane;
249
250 for (plane = 0; plane < 4 && in->data[plane] && in->linesize[plane]; plane++) {
251 uint8_t *dst = out->data[plane];
252 const uint8_t *src = in ->data[plane];
253 const float *fmap = s->fmap;
254 const int dst_linesize = out->linesize[plane];
255 const int src_linesize = in ->linesize[plane];
256 const int fmap_linesize = s->fmap_linesize;
257 const int chroma = plane == 1 || plane == 2;
258 const int hsub = chroma ? s->desc->log2_chroma_w : 0;
259 const int vsub = chroma ? s->desc->log2_chroma_h : 0;
260 const int w = AV_CEIL_RSHIFT(inlink->w, hsub);
261 const int h = AV_CEIL_RSHIFT(inlink->h, vsub);
262
263 for (y = 0; y < h; y++) {
264 uint8_t *dstp = dst;
265 const uint8_t *srcp = src;
266
267 for (x = 0; x < w; x++) {
268 const double dv = get_dither_value(s);
269 if (chroma) *dstp++ = av_clip_uint8(fmap[x << hsub] * (*srcp++ - 127) + 127 + dv);
270 else *dstp++ = av_clip_uint8(fmap[x ] * *srcp++ + dv);
271 }
272 dst += dst_linesize;
273 src += src_linesize;
274 fmap += fmap_linesize << vsub;
275 }
276 }
277 }
278
279 if (!direct)
280 av_frame_free(&in);
281 return ff_filter_frame(outlink, out);
282}
283
284static int config_props(AVFilterLink *inlink)
285{
286 AVFilterContext * ctx = inlink->dst;
287 VignetteContext *s = ctx->priv;
288 FilterLink *l = ff_filter_link(inlink);
289 AVRational sar = inlink->sample_aspect_ratio;
290
291 s->desc = av_pix_fmt_desc_get(inlink->format);
292 s->var_values[VAR_W] = inlink->w;
293 s->var_values[VAR_H] = inlink->h;
294 s->var_values[VAR_TB] = av_q2d(inlink->time_base);
295 s->var_values[VAR_R] = l->frame_rate.num == 0 || l->frame_rate.den == 0 ?
296 NAN : av_q2d(l->frame_rate);
297
298 if (!sar.num || !sar.den)
299 sar.num = sar.den = 1;
300 if (sar.num > sar.den) {
301 s->xscale = av_q2d(av_div_q(sar, s->aspect));
302 s->yscale = 1;
303 } else {
304 s->yscale = av_q2d(av_div_q(s->aspect, sar));
305 s->xscale = 1;
306 }
307 s->dmax = hypot(inlink->w / 2., inlink->h / 2.);
308 av_log(ctx, AV_LOG_DEBUG, "xscale=%f yscale=%f dmax=%f\n",
309 s->xscale, s->yscale, s->dmax);
310
311 s->fmap_linesize = FFALIGN(inlink->w, 32);
312 s->fmap = av_malloc_array(s->fmap_linesize, inlink->h * sizeof(*s->fmap));
313 if (!s->fmap)
314 return AVERROR(ENOMEM);
315
316 if (s->eval_mode == EVAL_MODE_INIT)
317 update_context(s, inlink, NULL);
318
319 return 0;
320}
321
322static const AVFilterPad vignette_inputs[] = {
323 {
324 .name = "default",
325 .type = AVMEDIA_TYPE_VIDEO,
326 .filter_frame = filter_frame,
327 .config_props = config_props,
328 },
329};
330
332 .p.name = "vignette",
333 .p.description = NULL_IF_CONFIG_SMALL("Make or reverse a vignette effect."),
334 .p.priv_class = &vignette_class,
336 .priv_size = sizeof(VignetteContext),
337 .init = init,
338 .uninit = uninit,
342};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
@ VAR_T
Definition aeval.c:53
@ VAR_NB
EvalMode
Definition af_volume.h:39
@ EVAL_MODE_NB
Definition af_volume.h:42
@ EVAL_MODE_FRAME
Definition af_volume.h:41
const FFFilter ff_vf_vignette
@ VAR_H
Definition avfilter.c:565
@ VAR_W
Definition avfilter.c:564
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
Main libavfilter public API header.
#define f(width, name)
Definition cbs_vp8.c:236
#define s(width, name)
Definition cbs_vp9.c:198
#define FLAGS
Definition cmdutils.c:598
#define AV_CEIL_RSHIFT(a, b)
Definition common.h:60
#define av_clip_uint8
Definition common.h:106
#define av_clipf
Definition common.h:145
#define NULL
Definition coverity.c:32
static AVFrame * frame
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
Definition dolby_e.c:1067
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
void av_expr_free(AVExpr *e)
Free a parsed expression previously created with av_expr_parse().
Definition eval.c:368
double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
Evaluate a previously parsed expression.
Definition eval.c:824
simple arithmetic expression evaluator
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_RATIONAL
Underlying C type is AVRational.
Definition opt.h:279
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
Definition opt.h:275
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
Definition avfilter.h:196
#define AVERROR(e)
Definition error.h:45
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition frame.c:535
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:599
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition rational.h:104
AVRational av_div_q(AVRational b, AVRational c)
Divide one rational by another.
Definition rational.c:88
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
static av_cold void uninit(AVBitStreamFilterContext *ctx)
#define TS2D(ts)
Definition filters.h:482
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#define FILTER_PIXFMTS_ARRAY(array)
Definition filters.h:244
#define TS2T(ts, tb)
Definition filters.h:483
static FilterLink * ff_filter_link(AVFilterLink *link)
Definition filters.h:199
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
static enum AVPixelFormat pix_fmts[]
Definition libkvazaar.c:296
#define isnan(x)
Definition libm.h:342
static av_const double hypot(double x, double y)
Definition libm.h:368
uint8_t w
Definition llvidencdsp.c:39
#define FFALIGN(x, a)
Definition macros.h:78
#define NAN
#define M_PI_2
Definition mathematics.h:73
Memory handling functions.
var_name
Definition noise.c:46
@ VAR_PTS
Definition noise.c:49
@ VAR_TB
Definition noise.c:48
@ VAR_N
Definition noise.c:47
static const char *const var_names[]
Definition noise.c:30
AVOptions.
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3460
#define AV_PIX_FMT_FLAG_RGB
The pixel format contains RGB-like data (as opposed to YUV/grayscale).
Definition pixdesc.h:136
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition pixfmt.h:75
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition pixfmt.h:73
@ AV_PIX_FMT_YUV440P
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition pixfmt.h:106
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition pixfmt.h:77
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition pixfmt.h:81
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition pixfmt.h:79
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition pixfmt.h:80
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition pixfmt.h:78
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition pixfmt.h:76
static int config_props(AVBitStreamFilterLink *link)
Definition source.c:181
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:493
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition frame.h:517
AVOption.
Definition opt.h:428
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition pixdesc.h:69
Rational number (pair of numerator and denominator).
Definition rational.h:58
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
const AVPixFmtDescriptor * desc
Definition vf_vignette.c:61
uint32_t dither
Definition vf_vignette.c:73
DEF_EXPR_FIELDS(angle)
AVRational scale
Definition vf_vignette.c:76
AVRational aspect
Definition vf_vignette.c:75
double var_values[VAR_NB]
Definition vf_vignette.c:68
int eval_mode
EvalMode.
Definition vf_vignette.c:63
#define av_malloc_array(a, b)
#define av_freep(p)
#define av_log(a,...)
#define src
Definition vp8dsp.c:248
static FILE * out
Definition movenc.c:55
static AVFormatContext * ctx
Definition movenc.c:49
@ VAR_R
Definition vf_eq.h:40
@ EVAL_MODE_INIT
Definition vf_fftfilt.c:41
static void hsub(htype *dst, const htype *src, int bins)
Definition vf_median.c:74
@ VAR_NB
Definition vf_vignette.c:50
static double get_dither_value(VignetteContext *s)
static const AVFilterPad vignette_inputs[]
static double get_natural_factor(const VignetteContext *s, int x, int y)
static void update_context(VignetteContext *s, AVFilterLink *inlink, AVFrame *frame)
static const AVOption vignette_options[]
Definition vf_vignette.c:81
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
static int config_props(AVFilterLink *inlink)
static av_cold void uninit(AVFilterContext *ctx)
#define OFFSET(x)
Definition vf_vignette.c:79
#define PARSE_EXPR(name)
static av_always_inline void chroma(WaveformContext *s, AVFrame *in, AVFrame *out, int component, int intensity, int offset_y, int offset_x, int column, int mirror, int jobnr, int nb_jobs)
const AVFilterPad ff_video_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_VIDEO.
Definition video.c:37
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition video.c:89
static double c[64]