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/internal.h"
27#include "libavutil/pixdesc.h"
28#include "avfilter.h"
29#include "filters.h"
30#include "video.h"
31#include "wavelet97.h"
32
33static const char *const var_names[] = {
34 "w", // stream width
35 "h", // stream height
36 "n", // frame count
37 "pts", // presentation timestamp expressed in AV_TIME_BASE units
38 "r", // frame rate
39 "t", // timestamp expressed in seconds
40 "tb", // timebase
41 NULL
42};
43
54
60
66
67typedef struct VignetteContext {
68 const AVClass *class;
70 int mode; ///< Mode
71 int eval_mode; ///< EvalMode
72 double noise;
74 double sqcoeff[2][5];
75 float *wplanes;
77#define DEF_EXPR_FIELDS(name) AVExpr *name##_pexpr; char *name##_expr; double name
82 float *fmap;
84 double dmax;
85 float xscale, yscale;
86 uint32_t dither;
91
92#define OFFSET(x) offsetof(VignetteContext, x)
93#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
94static const AVOption vignette_options[] = {
95 { "angle", "set lens angle", OFFSET(angle_expr), AV_OPT_TYPE_STRING, {.str="PI/5"}, .flags = FLAGS },
96 { "a", "set lens angle", OFFSET(angle_expr), AV_OPT_TYPE_STRING, {.str="PI/5"}, .flags = FLAGS },
97 { "x0", "set circle center position on x-axis", OFFSET(x0_expr), AV_OPT_TYPE_STRING, {.str="w/2"}, .flags = FLAGS },
98 { "y0", "set circle center position on y-axis", OFFSET(y0_expr), AV_OPT_TYPE_STRING, {.str="h/2"}, .flags = FLAGS },
99 { "mode", "set forward/backward mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64 = MODE_FORWARD}, 0, MODE_WAVELET, FLAGS, .unit = "mode" },
100 { "forward", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = MODE_FORWARD}, INT_MIN, INT_MAX, FLAGS, .unit = "mode"},
101 { "backward", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = MODE_BACKWARD}, INT_MIN, INT_MAX, FLAGS, .unit = "mode"},
102 { "wavelet", "backward with noise suppression in the wavelet domain", 0, AV_OPT_TYPE_CONST, {.i64 = MODE_WAVELET}, INT_MIN, INT_MAX, FLAGS, .unit = "mode"},
103 { "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" },
104 { "init", "eval expressions once during initialization", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_INIT}, .flags = FLAGS, .unit = "eval" },
105 { "frame", "eval expressions for each frame", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_FRAME}, .flags = FLAGS, .unit = "eval" },
106 { "dither", "set dithering", OFFSET(do_dither), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, FLAGS },
107 { "aspect", "set aspect ratio", OFFSET(aspect), AV_OPT_TYPE_RATIONAL, {.dbl = 1}, 0, DBL_MAX, .flags = FLAGS },
108 { "noise", "set input noise standard deviation for wavelet mode", OFFSET(noise), AV_OPT_TYPE_DOUBLE, {.dbl = 0.5}, 0.01, 255, FLAGS },
109 { "decimated", "use a decimated wavelet transform, faster but lower quality", OFFSET(decimated), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS },
110 { NULL }
111};
112
114
116{
117 VignetteContext *s = ctx->priv;
118
119#define PARSE_EXPR(name) do { \
120 int ret = av_expr_parse(&s->name##_pexpr, s->name##_expr, var_names, \
121 NULL, NULL, NULL, NULL, 0, ctx); \
122 if (ret < 0) { \
123 av_log(ctx, AV_LOG_ERROR, "Unable to parse expression for '" \
124 AV_STRINGIFY(name) "'\n"); \
125 return ret; \
126 } \
127} while (0)
128
129 PARSE_EXPR(angle);
130 PARSE_EXPR(x0);
131 PARSE_EXPR(y0);
132
133 // squared taps propagate the noise variance as if the samples were uncorrelated
134 for (int i = 0; i < 2; i++)
135 for (int j = 0; j < 5; j++)
136 s->sqcoeff[i][j] = ff_wavelet97_coeff[i][j] * ff_wavelet97_coeff[i][j];
137 return 0;
138}
139
141{
142 VignetteContext *s = ctx->priv;
143 av_freep(&s->fmap);
144 av_freep(&s->wplanes);
145 av_expr_free(s->angle_pexpr);
146 av_expr_free(s->x0_pexpr);
147 av_expr_free(s->y0_pexpr);
148}
149
158
159static double get_natural_factor(const VignetteContext *s, int x, int y)
160{
161 const int xx = (x - s->x0) * s->xscale;
162 const int yy = (y - s->y0) * s->yscale;
163 const double dnorm = hypot(xx, yy) / s->dmax;
164 if (dnorm > 1) {
165 return 0;
166 } else {
167 const double c = cos(s->angle * dnorm);
168 return (c*c)*(c*c); // do not remove braces, it helps compilers
169 }
170}
171
173{
174 FilterLink *inl = ff_filter_link(inlink);
175 int x, y;
176 float *dst = s->fmap;
177 int dst_linesize = s->fmap_linesize;
178
179 if (frame) {
180 s->var_values[VAR_N] = inl->frame_count_out;
181 s->var_values[VAR_T] = TS2T(frame->pts, inlink->time_base);
182 s->var_values[VAR_PTS] = TS2D(frame->pts);
183 } else {
184 s->var_values[VAR_N] = NAN;
185 s->var_values[VAR_T] = NAN;
186 s->var_values[VAR_PTS] = NAN;
187 }
188
189 s->angle = av_expr_eval(s->angle_pexpr, s->var_values, NULL);
190 s->x0 = av_expr_eval(s->x0_pexpr, s->var_values, NULL);
191 s->y0 = av_expr_eval(s->y0_pexpr, s->var_values, NULL);
192
193 if (isnan(s->x0) || isnan(s->y0) || isnan(s->angle))
194 s->eval_mode = EVAL_MODE_FRAME;
195
196 s->angle = av_clipf(s->angle, 0, M_PI_2);
197
198 if (s->mode == MODE_BACKWARD) {
199 for (y = 0; y < inlink->h; y++) {
200 for (x = 0; x < inlink->w; x++) {
201 const double f = get_natural_factor(s, x, y);
202 /* f is 0 beyond dmax and can be tiny for an angle close to
203 * PI/2: bound the gain to keep the scaled samples in the
204 * range of int, 256 already saturates any non-zero 8-bit
205 * sample */
206 dst[x] = f > 1. / 256 ? 1. / f : 256;
207 }
208 dst += dst_linesize;
209 }
210 } else {
211 for (y = 0; y < inlink->h; y++) {
212 for (x = 0; x < inlink->w; x++)
213 dst[x] = get_natural_factor(s, x, y);
214 dst += dst_linesize;
215 }
216 }
217}
218
219static inline double get_dither_value(VignetteContext *s)
220{
221 double dv = 0;
222 if (s->do_dither) {
223 dv = s->dither / (double)(1LL<<32);
224 s->dither = s->dither * 1664525 + 1013904223;
225 }
226 return dv;
227}
228
229static int wavelet_levels(int w, int h)
230{
231 return FFMAX(av_log2(FFMIN(w, h)) - 2, 0);
232}
233
234#define BOX_RADIUS 3
235
245
246/**
247 * Samples of a plane processed by a pass, sample x of line y is at
248 * y * ystride + x * xstride, x < w, y < h.
249 */
250typedef struct Lattice {
252} Lattice;
253
255{
256 return (Lattice){ l.ystride, l.xstride, l.h, l.w };
257}
258
259/**
260 * One operation on the lines of a lattice, run in parallel over slices of lines
261 */
262typedef struct WaveletPass {
265 float *dst[2];
266 const float *src[3];
267 const double (*coeff)[5];
268 int step; ///< filter dilation
270
271static void box_lines(const WaveletPass *p, int line0, int line1)
272{
273 const Lattice l = p->lat;
274
275 for (int y = line0; y < line1; y++) {
276 for (int x = 0; x < l.w; x++) {
277 double sum = 0;
278
279 for (int k = -BOX_RADIUS; k <= BOX_RADIUS; k++)
280 sum += p->src[0][y * l.ystride + avpriv_mirror(x + k * p->step, l.w - 1) * l.xstride];
281 p->dst[0][y * l.ystride + x * l.xstride] = sum / (2 * BOX_RADIUS + 1);
282 }
283 }
284}
285
286static void energy_lines(const WaveletPass *p, int line0, int line1)
287{
288 const Lattice l = p->lat;
289
290 for (int y = line0; y < line1; y++) {
291 for (int x = 0; x < l.w; x++) {
292 const int i = y * l.ystride + x * l.xstride;
293 const float band = p->src[0][i], var = p->src[1][i];
294
295 p->dst[0][i] = band * band / var;
296 p->dst[1][i] = 1 / var;
297 }
298 }
299}
300
301static void gain_lines(const WaveletPass *p, int line0, int line1)
302{
303 const Lattice l = p->lat;
304 // energy a box must have to count as signal: noise + 3 standard errors, assuming independent coefficients
305 const double threshold = 1 + 3 * sqrt(2.0 / ((2 * BOX_RADIUS + 1) * (2 * BOX_RADIUS + 1)));
306
307 for (int y = line0; y < line1; y++) {
308 for (int x = 0; x < l.w; x++) {
309 const int i = y * l.ystride + x * l.xstride;
310 const float signal = FFMAX(p->src[0][i] - threshold, 0) / p->src[1][i];
311
312 p->dst[0][i] *= signal / (signal + p->src[2][i]);
313 }
314 }
315}
316
317static int run_pass_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
318{
319 const WaveletPass *p = arg;
320 const Lattice l = p->lat;
321 const int line0 = l.h * jobnr / nb_jobs;
322 const int line1 = l.h * (jobnr + 1) / nb_jobs;
323
324 switch (p->type) {
325 case PASS_DECOMPOSE:
326 ff_wavelet97_decompose_lines(p->dst[0], p->dst[1], p->src[0], p->coeff,
327 l.xstride, l.ystride, p->step, l.w, line0, line1);
328 break;
329 case PASS_COMPOSE:
330 ff_wavelet97_compose_lines(p->dst[0], p->src[0], p->src[1],
331 l.xstride, l.ystride, p->step, l.w, line0, line1);
332 break;
334 ff_wavelet97_decompose_decimated_lines(p->dst[0], p->src[0], p->coeff,
335 l.xstride, l.ystride, l.w, line0, line1);
336 break;
338 ff_wavelet97_compose_decimated_lines(p->dst[0], p->src[0],
339 l.xstride, l.ystride, l.w, line0, line1);
340 break;
341 case PASS_BOX: box_lines(p, line0, line1); break;
342 case PASS_ENERGY: energy_lines(p, line0, line1); break;
343 case PASS_GAIN: gain_lines(p, line0, line1); break;
344 }
345 return 0;
346}
347
349{
350 if (p->lat.h > 0)
353}
354
355static void decompose(AVFilterContext *ctx, float *dst[4], const float *src, float *temp[2],
356 const double coeff[2][5], int step, Lattice lat)
357{
358 WaveletPass p = { PASS_DECOMPOSE, lat, { temp[0], temp[1] }, { src }, coeff, step };
359
360 run_pass(ctx, &p);
361 p.lat = transpose(lat);
362 p.src[0] = temp[0]; p.dst[0] = dst[0]; p.dst[1] = dst[1];
363 run_pass(ctx, &p);
364 p.src[0] = temp[1]; p.dst[0] = dst[2]; p.dst[1] = dst[3];
365 run_pass(ctx, &p);
366}
367
368static void compose(AVFilterContext *ctx, float *dst, float *const src[4], float *temp[2],
369 int step, Lattice lat)
370{
371 WaveletPass p = { PASS_COMPOSE, transpose(lat), { temp[0] }, { src[0], src[1] }, NULL, step };
372
373 run_pass(ctx, &p);
374 p.dst[0] = temp[1]; p.src[0] = src[2]; p.src[1] = src[3];
375 run_pass(ctx, &p);
376 p.lat = lat;
377 p.dst[0] = dst; p.src[0] = temp[0]; p.src[1] = temp[1];
378 run_pass(ctx, &p);
379}
380
381static void decompose_decimated(AVFilterContext *ctx, float *plane, float *temp,
382 const double coeff[2][5], Lattice lat)
383{
384 WaveletPass p = { PASS_DECOMPOSE_DECIMATED, lat, { temp }, { plane }, coeff };
385
386 run_pass(ctx, &p);
387 p.lat = transpose(lat);
388 p.dst[0] = plane; p.src[0] = temp;
389 run_pass(ctx, &p);
390}
391
392static void compose_decimated(AVFilterContext *ctx, float *plane, float *temp, Lattice lat)
393{
394 WaveletPass p = { PASS_COMPOSE_DECIMATED, transpose(lat), { temp }, { plane } };
395
396 run_pass(ctx, &p);
397 p.lat = lat;
398 p.dst[0] = plane; p.src[0] = temp;
399 run_pass(ctx, &p);
400}
401
402static void box_filter(AVFilterContext *ctx, float *plane, float *temp, int step, Lattice lat)
403{
404 WaveletPass p = { PASS_BOX, lat, { temp }, { plane }, NULL, step };
405
406 run_pass(ctx, &p);
407 p.lat = transpose(lat);
408 p.dst[0] = plane; p.src[0] = temp;
409 run_pass(ctx, &p);
410}
411
412/**
413 * Wiener shrinkage of a detail band toward 0, the signal variance is estimated
414 * from a box of coefficients, weighted by their precision.
415 */
416static void shrink_band(AVFilterContext *ctx, float *band, const float *var, float *temp[3],
417 int step, Lattice lat)
418{
419 WaveletPass p = { PASS_ENERGY, lat, { temp[0], temp[1] }, { band, var } };
420
421 run_pass(ctx, &p);
422 box_filter(ctx, temp[0], temp[2], step, lat);
423 box_filter(ctx, temp[1], temp[2], step, lat);
424 p.type = PASS_GAIN;
425 p.dst[0] = band; p.src[0] = temp[0]; p.src[1] = temp[1]; p.src[2] = var;
426 run_pass(ctx, &p);
427}
428
429static void transform_undecimated(AVFilterContext *ctx, float *cur, float *var, int levels, Lattice lat)
430{
431 VignetteContext *s = ctx->priv;
432 const size_t plane_size = s->wplane_size;
433 float *next = var + plane_size;
434 float *temp[2] = { var + 2 * plane_size, var + 3 * plane_size };
435 float *vbands[4] = { var + 4 * plane_size, var + 5 * plane_size,
436 var + 6 * plane_size, var + 7 * plane_size };
437 float *hbands = var + 8 * plane_size;
438
439 for (int i = 0; i < levels; i++) {
440 float *bands[4] = { next, hbands + 3 * i * plane_size,
441 hbands + (3 * i + 1) * plane_size, hbands + (3 * i + 2) * plane_size };
442 float *scratch[3] = { temp[0], temp[1], cur };
443
444 decompose(ctx, bands, cur, temp, ff_wavelet97_coeff, 1 << i, lat);
445 decompose(ctx, vbands, var, temp, s->sqcoeff, 1 << i, lat);
446 for (int j = 1; j < 4; j++)
447 shrink_band(ctx, bands[j], vbands[j], scratch, 1 << i, lat);
448 FFSWAP(float *, cur, next);
449 FFSWAP(float *, var, vbands[0]);
450 }
451
452 for (int i = levels - 1; i >= 0; i--) {
453 float *bands[4] = { cur, hbands + 3 * i * plane_size,
454 hbands + (3 * i + 1) * plane_size, hbands + (3 * i + 2) * plane_size };
455
456 compose(ctx, next, bands, temp, 1 << i, lat);
457 FFSWAP(float *, cur, next);
458 }
459}
460
461/**
462 * Lattice of the lowpass coefficients of the given level of a decimated transform
463 */
465{
466 return (Lattice){ lat.xstride << level, lat.ystride << level,
468}
469
470static void transform_decimated(AVFilterContext *ctx, float *cur, float *var, int levels, Lattice lat)
471{
472 VignetteContext *s = ctx->priv;
473 const size_t plane_size = s->wplane_size;
474 float *temp[3] = { var + plane_size, var + 2 * plane_size, var + 3 * plane_size };
475
476 for (int i = 0; i < levels; i++) {
477 const Lattice level = level_lattice(lat, i);
478
480 decompose_decimated(ctx, var, temp[0], s->sqcoeff, level);
481 for (int j = 1; j < 4; j++) {
482 const int offset = (j & 1) * level.xstride + (j >> 1) * level.ystride;
483 const Lattice band = { 2 * level.xstride, 2 * level.ystride,
484 (level.w + !(j & 1)) / 2, (level.h + !(j >> 1)) / 2 };
485 float *scratch[3] = { temp[0] + offset, temp[1] + offset, temp[2] + offset };
486
487 shrink_band(ctx, cur + offset, var + offset, scratch, 1, band);
488 }
489 }
490
491 for (int i = levels - 1; i >= 0; i--)
492 compose_decimated(ctx, cur, temp[0], level_lattice(lat, i));
493}
494
496 uint8_t *dst, int dst_linesize,
497 const uint8_t *src, int src_linesize, int pixel_step,
498 int w, int h, int hsub, int vsub, int bias)
499{
500 VignetteContext *s = ctx->priv;
501 const int linesize = s->fmap_linesize;
502 const Lattice lat = { 1, linesize, w, h };
503 float *cur = s->wplanes;
504 float *var = cur + s->wplane_size;
505
506 for (int y = 0; y < h; y++) {
507 for (int x = 0; x < w; x++) {
508 const double a = FFMAX(s->fmap[(y << vsub) * linesize + (x << hsub)], 1e-6);
509
510 cur[y * linesize + x] = av_clipd((src[y * src_linesize + x * pixel_step] - bias) / a,
511 -bias, 255 - bias);
512 var[y * linesize + x] = s->noise * s->noise / (a * a);
513 }
514 }
515
516 if (s->decimated)
517 transform_decimated(ctx, cur, var, wavelet_levels(w, h), lat);
518 else
519 transform_undecimated(ctx, cur, var, wavelet_levels(w, h), lat);
520
521 for (int y = 0; y < h; y++)
522 for (int x = 0; x < w; x++)
523 dst[y * dst_linesize + x * pixel_step] =
524 av_clip_uint8(cur[y * linesize + x] + bias + get_dither_value(s));
525}
526
528{
529 VignetteContext *s = ctx->priv;
530
531 if (s->desc->flags & AV_PIX_FMT_FLAG_RGB) {
532 for (int c = 0; c < 3; c++)
533 filter_plane_wavelet(ctx, out->data[0] + c, out->linesize[0],
534 in->data[0] + c, in->linesize[0], 3,
535 in->width, in->height, 0, 0, 0);
536 return;
537 }
538
539 for (int plane = 0; plane < 4 && in->data[plane] && in->linesize[plane]; plane++) {
540 const int chroma = plane == 1 || plane == 2;
541 const int hsub = chroma ? s->desc->log2_chroma_w : 0;
542 const int vsub = chroma ? s->desc->log2_chroma_h : 0;
543
544 filter_plane_wavelet(ctx, out->data[plane], out->linesize[plane],
545 in->data[plane], in->linesize[plane], 1,
547 hsub, vsub, chroma ? 127 : 0);
548 }
549}
550
551static int filter_frame(AVFilterLink *inlink, AVFrame *in)
552{
553 unsigned x, y, direct = 0;
554 AVFilterContext *ctx = inlink->dst;
555 VignetteContext *s = ctx->priv;
556 AVFilterLink *outlink = ctx->outputs[0];
557 AVFrame *out;
558
559 if (av_frame_is_writable(in)) {
560 direct = 1;
561 out = in;
562 } else {
563 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
564 if (!out) {
565 av_frame_free(&in);
566 return AVERROR(ENOMEM);
567 }
569 }
570
571 if (s->eval_mode == EVAL_MODE_FRAME)
572 update_context(s, inlink, in);
573
574 if (s->mode == MODE_WAVELET) {
576 } else if (s->desc->flags & AV_PIX_FMT_FLAG_RGB) {
577 uint8_t *dst = out->data[0];
578 const uint8_t *src = in ->data[0];
579 const float *fmap = s->fmap;
580 const int dst_linesize = out->linesize[0];
581 const int src_linesize = in ->linesize[0];
582 const int fmap_linesize = s->fmap_linesize;
583
584 for (y = 0; y < inlink->h; y++) {
585 uint8_t *dstp = dst;
586 const uint8_t *srcp = src;
587
588 for (x = 0; x < inlink->w; x++, dstp += 3, srcp += 3) {
589 const float f = fmap[x];
590
591 dstp[0] = av_clip_uint8(srcp[0] * f + get_dither_value(s));
592 dstp[1] = av_clip_uint8(srcp[1] * f + get_dither_value(s));
593 dstp[2] = av_clip_uint8(srcp[2] * f + get_dither_value(s));
594 }
595 dst += dst_linesize;
596 src += src_linesize;
597 fmap += fmap_linesize;
598 }
599 } else {
600 int plane;
601
602 for (plane = 0; plane < 4 && in->data[plane] && in->linesize[plane]; plane++) {
603 uint8_t *dst = out->data[plane];
604 const uint8_t *src = in ->data[plane];
605 const float *fmap = s->fmap;
606 const int dst_linesize = out->linesize[plane];
607 const int src_linesize = in ->linesize[plane];
608 const int fmap_linesize = s->fmap_linesize;
609 const int chroma = plane == 1 || plane == 2;
610 const int hsub = chroma ? s->desc->log2_chroma_w : 0;
611 const int vsub = chroma ? s->desc->log2_chroma_h : 0;
612 const int w = AV_CEIL_RSHIFT(inlink->w, hsub);
613 const int h = AV_CEIL_RSHIFT(inlink->h, vsub);
614
615 for (y = 0; y < h; y++) {
616 uint8_t *dstp = dst;
617 const uint8_t *srcp = src;
618
619 for (x = 0; x < w; x++) {
620 const double dv = get_dither_value(s);
621 if (chroma) *dstp++ = av_clip_uint8(fmap[x << hsub] * (*srcp++ - 127) + 127 + dv);
622 else *dstp++ = av_clip_uint8(fmap[x ] * *srcp++ + dv);
623 }
624 dst += dst_linesize;
625 src += src_linesize;
626 fmap += fmap_linesize << vsub;
627 }
628 }
629 }
630
631 if (!direct)
632 av_frame_free(&in);
633 return ff_filter_frame(outlink, out);
634}
635
636static int config_props(AVFilterLink *inlink)
637{
638 AVFilterContext * ctx = inlink->dst;
639 VignetteContext *s = ctx->priv;
640 FilterLink *l = ff_filter_link(inlink);
641 AVRational sar = inlink->sample_aspect_ratio;
642
643 s->desc = av_pix_fmt_desc_get(inlink->format);
644 s->var_values[VAR_W] = inlink->w;
645 s->var_values[VAR_H] = inlink->h;
646 s->var_values[VAR_TB] = av_q2d(inlink->time_base);
647 s->var_values[VAR_R] = l->frame_rate.num == 0 || l->frame_rate.den == 0 ?
648 NAN : av_q2d(l->frame_rate);
649
650 if (!sar.num || !sar.den)
651 sar.num = sar.den = 1;
652 if (sar.num > sar.den) {
653 s->xscale = av_q2d(av_div_q(sar, s->aspect));
654 s->yscale = 1;
655 } else {
656 s->yscale = av_q2d(av_div_q(s->aspect, sar));
657 s->xscale = 1;
658 }
659 s->dmax = hypot(inlink->w / 2., inlink->h / 2.);
660 av_log(ctx, AV_LOG_DEBUG, "xscale=%f yscale=%f dmax=%f\n",
661 s->xscale, s->yscale, s->dmax);
662
663 s->fmap_linesize = FFALIGN(inlink->w, 32);
664 s->fmap = av_malloc_array(s->fmap_linesize, inlink->h * sizeof(*s->fmap));
665 if (!s->fmap)
666 return AVERROR(ENOMEM);
667
668 if (s->mode == MODE_WAVELET) {
669 s->wplane_size = (size_t)s->fmap_linesize * inlink->h;
670 s->wplanes = av_malloc_array(s->wplane_size, (s->decimated ? 5 : 9 + 3 * wavelet_levels(inlink->w, inlink->h)) * sizeof(*s->wplanes));
671 if (!s->wplanes)
672 return AVERROR(ENOMEM);
673 }
674
675 if (s->eval_mode == EVAL_MODE_INIT)
676 update_context(s, inlink, NULL);
677
678 return 0;
679}
680
681static const AVFilterPad vignette_inputs[] = {
682 {
683 .name = "default",
684 .type = AVMEDIA_TYPE_VIDEO,
685 .filter_frame = filter_frame,
686 .config_props = config_props,
687 },
688};
689
691 .p.name = "vignette",
692 .p.description = NULL_IF_CONFIG_SMALL("Make or reverse a vignette effect."),
693 .p.priv_class = &vignette_class,
695 .priv_size = sizeof(VignetteContext),
696 .init = init,
697 .uninit = uninit,
701};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:97
@ VAR_T
Definition aeval.c:53
@ VAR_NB
static const float bands[]
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
Mode
Frame type (Table 1a in 3GPP TS 26.101)
Definition amrnbdata.h:39
static FILE * out
static AVFormatContext * ctx
@ VAR_H
Definition avfilter.c:574
@ VAR_W
Definition avfilter.c:573
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1079
int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition avfilter.c:1721
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition avfilter.c:857
Main libavfilter public API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define f(width, name)
Definition cbs_vp8.c:236
#define s(width, name)
Definition cbs_vp9.c:198
#define FLAGS
Definition cmdutils.c:596
#define av_clipd
Definition common.h:148
#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:613
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_DOUBLE
Underlying C type is double.
Definition opt.h:266
@ 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:202
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition avfilter.h:172
#define AVERROR(e)
Definition error.h:45
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition frame.c:538
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:65
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:602
#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
int a
#define av_log2
Definition intmath.h:84
static av_cold void uninit(AVBitStreamFilterContext *ctx)
unsigned offset
Definition libaomenc.c:763
const char * arg
Definition jacosubdec.c:66
#define transpose(x)
#define TS2D(ts)
Definition filters.h:497
#define FILTER_INPUTS(array)
Definition filters.h:279
#define FILTER_OUTPUTS(array)
Definition filters.h:280
#define FILTER_PIXFMTS_ARRAY(array)
Definition filters.h:259
#define TS2T(ts, tb)
Definition filters.h:498
static FilterLink * ff_filter_link(AVFilterLink *link)
Definition filters.h:199
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:493
#define av_cold
Definition attributes.h:117
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:97
static av_always_inline av_const int avpriv_mirror(int x, int w)
Definition internal.h:143
static enum AVPixelFormat pix_fmts[]
Definition libkvazaar.c:296
uint8_t w
Definition llvidencdsp.c:39
#define FFSWAP(type, a, b)
Definition macros.h:52
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
#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 int noise(AVBSFContext *ctx, AVPacket *pkt)
Definition noise.c:124
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:3484
#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:279
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:479
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:500
int width
Definition frame.h:551
int height
Definition frame.h:551
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:524
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
Samples of a plane processed by a pass, sample x of line y is at y * ystride + x * xstride,...
int ystride
int xstride
const AVPixFmtDescriptor * desc
Definition vf_vignette.c:69
uint32_t dither
Definition vf_vignette.c:86
DEF_EXPR_FIELDS(angle)
AVRational scale
Definition vf_vignette.c:89
AVRational aspect
Definition vf_vignette.c:88
double var_values[VAR_NB]
Definition vf_vignette.c:81
double sqcoeff[2][5]
Definition vf_vignette.c:74
int eval_mode
EvalMode.
Definition vf_vignette.c:71
One operation on the lines of a lattice, run in parallel over slices of lines.
const float * src[3]
enum PassType type
int step
filter dilation
Lattice lat
const double(* coeff)[5]
float * dst[2]
Definition swscale.c:85
uint8_t level
Definition svq3.c:208
#define av_malloc_array(a, b)
#define av_freep(p)
#define av_log(a,...)
#define src
Definition vp8dsp.c:248
@ VAR_R
Definition vf_eq.h:40
@ EVAL_MODE_INIT
Definition vf_fftfilt.c:41
else temp
Definition vf_mcdeint.c:275
static void hsub(htype *dst, const htype *src, int bins)
Definition vf_median.c:74
@ VAR_NB
Definition vf_vignette.c:52
static int run_pass_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
static void run_pass(AVFilterContext *ctx, WaveletPass *p)
static void filter_plane_wavelet(AVFilterContext *ctx, uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize, int pixel_step, int w, int h, int hsub, int vsub, int bias)
static void transform_undecimated(AVFilterContext *ctx, float *cur, float *var, int levels, Lattice lat)
@ MODE_BACKWARD
Definition vf_vignette.c:63
@ MODE_WAVELET
Definition vf_vignette.c:64
@ MODE_FORWARD
Definition vf_vignette.c:62
static void energy_lines(const WaveletPass *p, int line0, int line1)
static double get_dither_value(VignetteContext *s)
static void transform_decimated(AVFilterContext *ctx, float *cur, float *var, int levels, Lattice lat)
static int wavelet_levels(int w, int h)
static const AVFilterPad vignette_inputs[]
static void shrink_band(AVFilterContext *ctx, float *band, const float *var, float *temp[3], int step, Lattice lat)
Wiener shrinkage of a detail band toward 0, the signal variance is estimated from a box of coefficien...
static double get_natural_factor(const VignetteContext *s, int x, int y)
static void update_context(VignetteContext *s, AVFilterLink *inlink, AVFrame *frame)
static void compose(AVFilterContext *ctx, float *dst, float *const src[4], float *temp[2], int step, Lattice lat)
static const AVOption vignette_options[]
Definition vf_vignette.c:94
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
static void box_filter(AVFilterContext *ctx, float *plane, float *temp, int step, Lattice lat)
static void gain_lines(const WaveletPass *p, int line0, int line1)
static void box_lines(const WaveletPass *p, int line0, int line1)
static void decompose(AVFilterContext *ctx, float *dst[4], const float *src, float *temp[2], const double coeff[2][5], int step, Lattice lat)
#define BOX_RADIUS
static Lattice level_lattice(Lattice lat, int level)
Lattice of the lowpass coefficients of the given level of a decimated transform.
static int config_props(AVFilterLink *inlink)
static av_cold void uninit(AVFilterContext *ctx)
PassType
@ PASS_COMPOSE
@ PASS_GAIN
@ PASS_BOX
@ PASS_COMPOSE_DECIMATED
@ PASS_DECOMPOSE_DECIMATED
@ PASS_DECOMPOSE
@ PASS_ENERGY
static void filter_frame_wavelet(AVFilterContext *ctx, AVFrame *out, const AVFrame *in)
#define OFFSET(x)
Definition vf_vignette.c:92
#define PARSE_EXPR(name)
static void decompose_decimated(AVFilterContext *ctx, float *plane, float *temp, const double coeff[2][5], Lattice lat)
static void compose_decimated(AVFilterContext *ctx, float *plane, float *temp, Lattice lat)
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:120
static int bias(int x, int c)
Definition vqcdec.c:115
static double c[64]
void ff_wavelet97_decompose_decimated_lines(float *dst, const float *src, const double coeff[2][5], int xstride, int ystride, int len, int line0, int line1)
Decimated analysis of lines line0..line1-1 along one direction.
Definition wavelet97.c:147
void ff_wavelet97_decompose_lines(float *dst_l, float *dst_h, const float *src, const double coeff[2][5], int xlinesize, int ylinesize, int step, int w, int line0, int line1)
Undecimated analysis of lines line0..line1-1 along one direction.
Definition wavelet97.c:120
void ff_wavelet97_compose_lines(float *dst, const float *src_l, const float *src_h, int xlinesize, int ylinesize, int step, int w, int line0, int line1)
Inverse of ff_wavelet97_decompose_lines() with ff_wavelet97_coeff.
Definition wavelet97.c:134
const double ff_wavelet97_coeff[2][5]
Undecimated 9/7 wavelet transform.
Definition wavelet97.c:26
void ff_wavelet97_compose_decimated_lines(float *dst, const float *src, int xstride, int ystride, int len, int line0, int line1)
Inverse of ff_wavelet97_decompose_decimated_lines() with ff_wavelet97_coeff.
Definition wavelet97.c:156