LCOV - code coverage report
Current view: top level - libavfilter - vf_vignette.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 142 0.0 %
Date: 2017-12-18 06:23:41 Functions: 0 8 0.0 %

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

Generated by: LCOV version 1.13