LCOV - code coverage report
Current view: top level - libavfilter - vf_smartblur.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 103 0.0 %
Date: 2017-12-18 13:19:42 Functions: 0 7 0.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
       3             :  * Copyright (c) 2012 Jeremy Tran
       4             :  *
       5             :  * This file is part of FFmpeg.
       6             :  *
       7             :  * FFmpeg is free software; you can redistribute it and/or modify
       8             :  * it under the terms of the GNU General Public License as published by
       9             :  * the Free Software Foundation; either version 2 of the License, or
      10             :  * (at your option) any later version.
      11             :  *
      12             :  * FFmpeg is distributed in the hope that it will be useful,
      13             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15             :  * GNU General Public License for more details.
      16             :  *
      17             :  * You should have received a copy of the GNU General Public License along
      18             :  * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
      19             :  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
      20             :  */
      21             : 
      22             : /**
      23             :  * @file
      24             :  * Apply a smartblur filter to the input video
      25             :  * Ported from MPlayer libmpcodecs/vf_smartblur.c by Michael Niedermayer.
      26             :  */
      27             : 
      28             : #include "libavutil/opt.h"
      29             : #include "libavutil/pixdesc.h"
      30             : #include "libswscale/swscale.h"
      31             : 
      32             : #include "avfilter.h"
      33             : #include "formats.h"
      34             : #include "internal.h"
      35             : 
      36             : #define RADIUS_MIN 0.1
      37             : #define RADIUS_MAX 5.0
      38             : 
      39             : #define STRENGTH_MIN -1.0
      40             : #define STRENGTH_MAX 1.0
      41             : 
      42             : #define THRESHOLD_MIN -30
      43             : #define THRESHOLD_MAX 30
      44             : 
      45             : typedef struct FilterParam {
      46             :     float              radius;
      47             :     float              strength;
      48             :     int                threshold;
      49             :     float              quality;
      50             :     struct SwsContext *filter_context;
      51             : } FilterParam;
      52             : 
      53             : typedef struct SmartblurContext {
      54             :     const AVClass *class;
      55             :     FilterParam  luma;
      56             :     FilterParam  chroma;
      57             :     int          hsub;
      58             :     int          vsub;
      59             :     unsigned int sws_flags;
      60             : } SmartblurContext;
      61             : 
      62             : #define OFFSET(x) offsetof(SmartblurContext, x)
      63             : #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
      64             : 
      65             : static const AVOption smartblur_options[] = {
      66             :     { "luma_radius",    "set luma radius",    OFFSET(luma.radius),    AV_OPT_TYPE_FLOAT, {.dbl=1.0}, RADIUS_MIN, RADIUS_MAX, .flags=FLAGS },
      67             :     { "lr"         ,    "set luma radius",    OFFSET(luma.radius),    AV_OPT_TYPE_FLOAT, {.dbl=1.0}, RADIUS_MIN, RADIUS_MAX, .flags=FLAGS },
      68             :     { "luma_strength",  "set luma strength",  OFFSET(luma.strength),  AV_OPT_TYPE_FLOAT, {.dbl=1.0}, STRENGTH_MIN, STRENGTH_MAX, .flags=FLAGS },
      69             :     { "ls",             "set luma strength",  OFFSET(luma.strength),  AV_OPT_TYPE_FLOAT, {.dbl=1.0}, STRENGTH_MIN, STRENGTH_MAX, .flags=FLAGS },
      70             :     { "luma_threshold", "set luma threshold", OFFSET(luma.threshold), AV_OPT_TYPE_INT,   {.i64=0}, THRESHOLD_MIN, THRESHOLD_MAX, .flags=FLAGS },
      71             :     { "lt",             "set luma threshold", OFFSET(luma.threshold), AV_OPT_TYPE_INT,   {.i64=0}, THRESHOLD_MIN, THRESHOLD_MAX, .flags=FLAGS },
      72             : 
      73             :     { "chroma_radius",    "set chroma radius",    OFFSET(chroma.radius),    AV_OPT_TYPE_FLOAT, {.dbl=RADIUS_MIN-1},   RADIUS_MIN-1, RADIUS_MAX, .flags=FLAGS },
      74             :     { "cr",               "set chroma radius",    OFFSET(chroma.radius),    AV_OPT_TYPE_FLOAT, {.dbl=RADIUS_MIN-1},   RADIUS_MIN-1, RADIUS_MAX, .flags=FLAGS },
      75             :     { "chroma_strength",  "set chroma strength",  OFFSET(chroma.strength),  AV_OPT_TYPE_FLOAT, {.dbl=STRENGTH_MIN-1}, STRENGTH_MIN-1, STRENGTH_MAX, .flags=FLAGS },
      76             :     { "cs",               "set chroma strength",  OFFSET(chroma.strength),  AV_OPT_TYPE_FLOAT, {.dbl=STRENGTH_MIN-1}, STRENGTH_MIN-1, STRENGTH_MAX, .flags=FLAGS },
      77             :     { "chroma_threshold", "set chroma threshold", OFFSET(chroma.threshold), AV_OPT_TYPE_INT,   {.i64=THRESHOLD_MIN-1}, THRESHOLD_MIN-1, THRESHOLD_MAX, .flags=FLAGS },
      78             :     { "ct",               "set chroma threshold", OFFSET(chroma.threshold), AV_OPT_TYPE_INT,   {.i64=THRESHOLD_MIN-1}, THRESHOLD_MIN-1, THRESHOLD_MAX, .flags=FLAGS },
      79             : 
      80             :     { NULL }
      81             : };
      82             : 
      83             : AVFILTER_DEFINE_CLASS(smartblur);
      84             : 
      85           0 : static av_cold int init(AVFilterContext *ctx)
      86             : {
      87           0 :     SmartblurContext *s = ctx->priv;
      88             : 
      89             :     /* make chroma default to luma values, if not explicitly set */
      90           0 :     if (s->chroma.radius < RADIUS_MIN)
      91           0 :         s->chroma.radius = s->luma.radius;
      92           0 :     if (s->chroma.strength < STRENGTH_MIN)
      93           0 :         s->chroma.strength  = s->luma.strength;
      94           0 :     if (s->chroma.threshold < THRESHOLD_MIN)
      95           0 :         s->chroma.threshold = s->luma.threshold;
      96             : 
      97           0 :     s->luma.quality = s->chroma.quality = 3.0;
      98           0 :     s->sws_flags = SWS_BICUBIC;
      99             : 
     100           0 :     av_log(ctx, AV_LOG_VERBOSE,
     101             :            "luma_radius:%f luma_strength:%f luma_threshold:%d "
     102             :            "chroma_radius:%f chroma_strength:%f chroma_threshold:%d\n",
     103           0 :            s->luma.radius, s->luma.strength, s->luma.threshold,
     104           0 :            s->chroma.radius, s->chroma.strength, s->chroma.threshold);
     105             : 
     106           0 :     return 0;
     107             : }
     108             : 
     109           0 : static av_cold void uninit(AVFilterContext *ctx)
     110             : {
     111           0 :     SmartblurContext *s = ctx->priv;
     112             : 
     113           0 :     sws_freeContext(s->luma.filter_context);
     114           0 :     sws_freeContext(s->chroma.filter_context);
     115           0 : }
     116             : 
     117           0 : static int query_formats(AVFilterContext *ctx)
     118             : {
     119             :     static const enum AVPixelFormat pix_fmts[] = {
     120             :         AV_PIX_FMT_YUV444P,      AV_PIX_FMT_YUV422P,
     121             :         AV_PIX_FMT_YUV420P,      AV_PIX_FMT_YUV411P,
     122             :         AV_PIX_FMT_YUV410P,      AV_PIX_FMT_YUV440P,
     123             :         AV_PIX_FMT_GRAY8,
     124             :         AV_PIX_FMT_NONE
     125             :     };
     126             : 
     127           0 :     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
     128           0 :     if (!fmts_list)
     129           0 :         return AVERROR(ENOMEM);
     130           0 :     return ff_set_common_formats(ctx, fmts_list);
     131             : }
     132             : 
     133           0 : static int alloc_sws_context(FilterParam *f, int width, int height, unsigned int flags)
     134             : {
     135             :     SwsVector *vec;
     136             :     SwsFilter sws_filter;
     137             : 
     138           0 :     vec = sws_getGaussianVec(f->radius, f->quality);
     139             : 
     140           0 :     if (!vec)
     141           0 :         return AVERROR(EINVAL);
     142             : 
     143           0 :     sws_scaleVec(vec, f->strength);
     144           0 :     vec->coeff[vec->length / 2] += 1.0 - f->strength;
     145           0 :     sws_filter.lumH = sws_filter.lumV = vec;
     146           0 :     sws_filter.chrH = sws_filter.chrV = NULL;
     147           0 :     f->filter_context = sws_getCachedContext(NULL,
     148             :                                              width, height, AV_PIX_FMT_GRAY8,
     149             :                                              width, height, AV_PIX_FMT_GRAY8,
     150             :                                              flags, &sws_filter, NULL, NULL);
     151             : 
     152           0 :     sws_freeVec(vec);
     153             : 
     154           0 :     if (!f->filter_context)
     155           0 :         return AVERROR(EINVAL);
     156             : 
     157           0 :     return 0;
     158             : }
     159             : 
     160           0 : static int config_props(AVFilterLink *inlink)
     161             : {
     162           0 :     SmartblurContext *s = inlink->dst->priv;
     163           0 :     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
     164             : 
     165           0 :     s->hsub = desc->log2_chroma_w;
     166           0 :     s->vsub = desc->log2_chroma_h;
     167             : 
     168           0 :     alloc_sws_context(&s->luma, inlink->w, inlink->h, s->sws_flags);
     169           0 :     alloc_sws_context(&s->chroma,
     170           0 :                       AV_CEIL_RSHIFT(inlink->w, s->hsub),
     171           0 :                       AV_CEIL_RSHIFT(inlink->h, s->vsub),
     172             :                       s->sws_flags);
     173             : 
     174           0 :     return 0;
     175             : }
     176             : 
     177           0 : static void blur(uint8_t       *dst, const int dst_linesize,
     178             :                  const uint8_t *src, const int src_linesize,
     179             :                  const int w, const int h, const int threshold,
     180             :                  struct SwsContext *filter_context)
     181             : {
     182             :     int x, y;
     183             :     int orig, filtered;
     184             :     int diff;
     185             :     /* Declare arrays of 4 to get aligned data */
     186           0 :     const uint8_t* const src_array[4] = {src};
     187           0 :     uint8_t *dst_array[4]             = {dst};
     188           0 :     int src_linesize_array[4] = {src_linesize};
     189           0 :     int dst_linesize_array[4] = {dst_linesize};
     190             : 
     191           0 :     sws_scale(filter_context, src_array, src_linesize_array,
     192             :               0, h, dst_array, dst_linesize_array);
     193             : 
     194           0 :     if (threshold > 0) {
     195           0 :         for (y = 0; y < h; ++y) {
     196           0 :             for (x = 0; x < w; ++x) {
     197           0 :                 orig     = src[x + y * src_linesize];
     198           0 :                 filtered = dst[x + y * dst_linesize];
     199           0 :                 diff     = orig - filtered;
     200             : 
     201           0 :                 if (diff > 0) {
     202           0 :                     if (diff > 2 * threshold)
     203           0 :                         dst[x + y * dst_linesize] = orig;
     204           0 :                     else if (diff > threshold)
     205             :                         /* add 'diff' and subtract 'threshold' from 'filtered' */
     206           0 :                         dst[x + y * dst_linesize] = orig - threshold;
     207             :                 } else {
     208           0 :                     if (-diff > 2 * threshold)
     209           0 :                         dst[x + y * dst_linesize] = orig;
     210           0 :                     else if (-diff > threshold)
     211             :                         /* add 'diff' and 'threshold' to 'filtered' */
     212           0 :                         dst[x + y * dst_linesize] = orig + threshold;
     213             :                 }
     214             :             }
     215             :         }
     216           0 :     } else if (threshold < 0) {
     217           0 :         for (y = 0; y < h; ++y) {
     218           0 :             for (x = 0; x < w; ++x) {
     219           0 :                 orig     = src[x + y * src_linesize];
     220           0 :                 filtered = dst[x + y * dst_linesize];
     221           0 :                 diff     = orig - filtered;
     222             : 
     223           0 :                 if (diff > 0) {
     224           0 :                     if (diff <= -threshold)
     225           0 :                         dst[x + y * dst_linesize] = orig;
     226           0 :                     else if (diff <= -2 * threshold)
     227             :                         /* subtract 'diff' and 'threshold' from 'orig' */
     228           0 :                         dst[x + y * dst_linesize] = filtered - threshold;
     229             :                 } else {
     230           0 :                     if (diff >= threshold)
     231           0 :                         dst[x + y * dst_linesize] = orig;
     232           0 :                     else if (diff >= 2 * threshold)
     233             :                         /* add 'threshold' and subtract 'diff' from 'orig' */
     234           0 :                         dst[x + y * dst_linesize] = filtered + threshold;
     235             :                 }
     236             :             }
     237             :         }
     238             :     }
     239           0 : }
     240             : 
     241           0 : static int filter_frame(AVFilterLink *inlink, AVFrame *inpic)
     242             : {
     243           0 :     SmartblurContext  *s  = inlink->dst->priv;
     244           0 :     AVFilterLink *outlink     = inlink->dst->outputs[0];
     245             :     AVFrame *outpic;
     246           0 :     int cw = AV_CEIL_RSHIFT(inlink->w, s->hsub);
     247           0 :     int ch = AV_CEIL_RSHIFT(inlink->h, s->vsub);
     248             : 
     249           0 :     outpic = ff_get_video_buffer(outlink, outlink->w, outlink->h);
     250           0 :     if (!outpic) {
     251           0 :         av_frame_free(&inpic);
     252           0 :         return AVERROR(ENOMEM);
     253             :     }
     254           0 :     av_frame_copy_props(outpic, inpic);
     255             : 
     256           0 :     blur(outpic->data[0], outpic->linesize[0],
     257           0 :          inpic->data[0],  inpic->linesize[0],
     258             :          inlink->w, inlink->h, s->luma.threshold,
     259             :          s->luma.filter_context);
     260             : 
     261           0 :     if (inpic->data[2]) {
     262           0 :         blur(outpic->data[1], outpic->linesize[1],
     263           0 :              inpic->data[1],  inpic->linesize[1],
     264             :              cw, ch, s->chroma.threshold,
     265             :              s->chroma.filter_context);
     266           0 :         blur(outpic->data[2], outpic->linesize[2],
     267           0 :              inpic->data[2],  inpic->linesize[2],
     268             :              cw, ch, s->chroma.threshold,
     269             :              s->chroma.filter_context);
     270             :     }
     271             : 
     272           0 :     av_frame_free(&inpic);
     273           0 :     return ff_filter_frame(outlink, outpic);
     274             : }
     275             : 
     276             : static const AVFilterPad smartblur_inputs[] = {
     277             :     {
     278             :         .name         = "default",
     279             :         .type         = AVMEDIA_TYPE_VIDEO,
     280             :         .filter_frame = filter_frame,
     281             :         .config_props = config_props,
     282             :     },
     283             :     { NULL }
     284             : };
     285             : 
     286             : static const AVFilterPad smartblur_outputs[] = {
     287             :     {
     288             :         .name = "default",
     289             :         .type = AVMEDIA_TYPE_VIDEO,
     290             :     },
     291             :     { NULL }
     292             : };
     293             : 
     294             : AVFilter ff_vf_smartblur = {
     295             :     .name          = "smartblur",
     296             :     .description   = NULL_IF_CONFIG_SMALL("Blur the input video without impacting the outlines."),
     297             :     .priv_size     = sizeof(SmartblurContext),
     298             :     .init          = init,
     299             :     .uninit        = uninit,
     300             :     .query_formats = query_formats,
     301             :     .inputs        = smartblur_inputs,
     302             :     .outputs       = smartblur_outputs,
     303             :     .priv_class    = &smartblur_class,
     304             :     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
     305             : };

Generated by: LCOV version 1.13