Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

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