LCOV - code coverage report
Current view: top level - libavfilter - vf_sab.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 121 0.0 %
Date: 2017-12-15 18:13:28 Functions: 0 8 0.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
       3             :  *
       4             :  * This file is part of FFmpeg.
       5             :  *
       6             :  * FFmpeg is free software; you can redistribute it and/or modify
       7             :  * it under the terms of the GNU General Public License as published by
       8             :  * the Free Software Foundation; either version 2 of the License, or
       9             :  * (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
      14             :  * GNU General Public License for more details.
      15             :  *
      16             :  * You should have received a copy of the GNU General Public License along
      17             :  * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
      18             :  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
      19             :  */
      20             : 
      21             : /**
      22             :  * @file
      23             :  * Shape Adaptive Blur filter, ported from MPlayer libmpcodecs/vf_sab.c
      24             :  */
      25             : 
      26             : #include "libavutil/opt.h"
      27             : #include "libavutil/pixdesc.h"
      28             : #include "libswscale/swscale.h"
      29             : 
      30             : #include "avfilter.h"
      31             : #include "formats.h"
      32             : #include "internal.h"
      33             : 
      34             : typedef struct FilterParam {
      35             :     float radius;
      36             :     float pre_filter_radius;
      37             :     float strength;
      38             :     float quality;
      39             :     struct SwsContext *pre_filter_context;
      40             :     uint8_t *pre_filter_buf;
      41             :     int pre_filter_linesize;
      42             :     int dist_width;
      43             :     int dist_linesize;
      44             :     int *dist_coeff;
      45             : #define COLOR_DIFF_COEFF_SIZE 512
      46             :     int color_diff_coeff[COLOR_DIFF_COEFF_SIZE];
      47             : } FilterParam;
      48             : 
      49             : typedef struct SabContext {
      50             :     const AVClass *class;
      51             :     FilterParam  luma;
      52             :     FilterParam  chroma;
      53             :     int          hsub;
      54             :     int          vsub;
      55             :     unsigned int sws_flags;
      56             : } SabContext;
      57             : 
      58           0 : static int query_formats(AVFilterContext *ctx)
      59             : {
      60             :     static const enum AVPixelFormat pix_fmts[] = {
      61             :         AV_PIX_FMT_YUV420P,
      62             :         AV_PIX_FMT_YUV410P,
      63             :         AV_PIX_FMT_YUV444P,
      64             :         AV_PIX_FMT_YUV422P,
      65             :         AV_PIX_FMT_YUV411P,
      66             :         AV_PIX_FMT_NONE
      67             :     };
      68           0 :     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
      69           0 :     if (!fmts_list)
      70           0 :         return AVERROR(ENOMEM);
      71           0 :     return ff_set_common_formats(ctx, fmts_list);
      72             : }
      73             : 
      74             : #define RADIUS_MIN 0.1
      75             : #define RADIUS_MAX 4.0
      76             : 
      77             : #define PRE_FILTER_RADIUS_MIN 0.1
      78             : #define PRE_FILTER_RADIUS_MAX 2.0
      79             : 
      80             : #define STRENGTH_MIN 0.1
      81             : #define STRENGTH_MAX 100.0
      82             : 
      83             : #define OFFSET(x) offsetof(SabContext, x)
      84             : #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
      85             : 
      86             : static const AVOption sab_options[] = {
      87             :     { "luma_radius",            "set luma radius", OFFSET(luma.radius), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, RADIUS_MIN, RADIUS_MAX, .flags=FLAGS },
      88             :     { "lr"         ,            "set luma radius", OFFSET(luma.radius), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, RADIUS_MIN, RADIUS_MAX, .flags=FLAGS },
      89             :     { "luma_pre_filter_radius", "set luma pre-filter radius", OFFSET(luma.pre_filter_radius), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, PRE_FILTER_RADIUS_MIN, PRE_FILTER_RADIUS_MAX, .flags=FLAGS },
      90             :     { "lpfr",                   "set luma pre-filter radius", OFFSET(luma.pre_filter_radius), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, PRE_FILTER_RADIUS_MIN, PRE_FILTER_RADIUS_MAX, .flags=FLAGS },
      91             :     { "luma_strength",          "set luma strength", OFFSET(luma.strength), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, STRENGTH_MIN, STRENGTH_MAX, .flags=FLAGS },
      92             :     { "ls",                     "set luma strength", OFFSET(luma.strength), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, STRENGTH_MIN, STRENGTH_MAX, .flags=FLAGS },
      93             : 
      94             :     { "chroma_radius",            "set chroma radius", OFFSET(chroma.radius), AV_OPT_TYPE_FLOAT, {.dbl=RADIUS_MIN-1}, RADIUS_MIN-1, RADIUS_MAX, .flags=FLAGS },
      95             :     { "cr",                       "set chroma radius", OFFSET(chroma.radius), AV_OPT_TYPE_FLOAT, {.dbl=RADIUS_MIN-1}, RADIUS_MIN-1, RADIUS_MAX, .flags=FLAGS },
      96             :     { "chroma_pre_filter_radius", "set chroma pre-filter radius",  OFFSET(chroma.pre_filter_radius), AV_OPT_TYPE_FLOAT, {.dbl=PRE_FILTER_RADIUS_MIN-1},
      97             :                                   PRE_FILTER_RADIUS_MIN-1, PRE_FILTER_RADIUS_MAX, .flags=FLAGS },
      98             :     { "cpfr",                     "set chroma pre-filter radius",  OFFSET(chroma.pre_filter_radius), AV_OPT_TYPE_FLOAT, {.dbl=PRE_FILTER_RADIUS_MIN-1},
      99             :                                   PRE_FILTER_RADIUS_MIN-1, PRE_FILTER_RADIUS_MAX, .flags=FLAGS },
     100             :     { "chroma_strength",          "set chroma strength", OFFSET(chroma.strength), AV_OPT_TYPE_FLOAT, {.dbl=STRENGTH_MIN-1}, STRENGTH_MIN-1, STRENGTH_MAX, .flags=FLAGS },
     101             :     { "cs",                       "set chroma strength", OFFSET(chroma.strength), AV_OPT_TYPE_FLOAT, {.dbl=STRENGTH_MIN-1}, STRENGTH_MIN-1, STRENGTH_MAX, .flags=FLAGS },
     102             : 
     103             :     { NULL }
     104             : };
     105             : 
     106             : AVFILTER_DEFINE_CLASS(sab);
     107             : 
     108           0 : static av_cold int init(AVFilterContext *ctx)
     109             : {
     110           0 :     SabContext *s = ctx->priv;
     111             : 
     112             :     /* make chroma default to luma values, if not explicitly set */
     113           0 :     if (s->chroma.radius < RADIUS_MIN)
     114           0 :         s->chroma.radius = s->luma.radius;
     115           0 :     if (s->chroma.pre_filter_radius < PRE_FILTER_RADIUS_MIN)
     116           0 :         s->chroma.pre_filter_radius = s->luma.pre_filter_radius;
     117           0 :     if (s->chroma.strength < STRENGTH_MIN)
     118           0 :         s->chroma.strength = s->luma.strength;
     119             : 
     120           0 :     s->luma.quality = s->chroma.quality = 3.0;
     121           0 :     s->sws_flags = SWS_POINT;
     122             : 
     123           0 :     av_log(ctx, AV_LOG_VERBOSE,
     124             :            "luma_radius:%f luma_pre_filter_radius::%f luma_strength:%f "
     125             :            "chroma_radius:%f chroma_pre_filter_radius:%f chroma_strength:%f\n",
     126           0 :            s->luma  .radius, s->luma  .pre_filter_radius, s->luma  .strength,
     127           0 :            s->chroma.radius, s->chroma.pre_filter_radius, s->chroma.strength);
     128           0 :     return 0;
     129             : }
     130             : 
     131           0 : static void close_filter_param(FilterParam *f)
     132             : {
     133           0 :     if (f->pre_filter_context) {
     134           0 :         sws_freeContext(f->pre_filter_context);
     135           0 :         f->pre_filter_context = NULL;
     136             :     }
     137           0 :     av_freep(&f->pre_filter_buf);
     138           0 :     av_freep(&f->dist_coeff);
     139           0 : }
     140             : 
     141           0 : static av_cold void uninit(AVFilterContext *ctx)
     142             : {
     143           0 :     SabContext *s = ctx->priv;
     144             : 
     145           0 :     close_filter_param(&s->luma);
     146           0 :     close_filter_param(&s->chroma);
     147           0 : }
     148             : 
     149           0 : static int open_filter_param(FilterParam *f, int width, int height, unsigned int sws_flags)
     150             : {
     151             :     SwsVector *vec;
     152             :     SwsFilter sws_f;
     153             :     int i, x, y;
     154           0 :     int linesize = FFALIGN(width, 8);
     155             : 
     156           0 :     f->pre_filter_buf = av_malloc(linesize * height);
     157           0 :     if (!f->pre_filter_buf)
     158           0 :         return AVERROR(ENOMEM);
     159             : 
     160           0 :     f->pre_filter_linesize = linesize;
     161           0 :     vec = sws_getGaussianVec(f->pre_filter_radius, f->quality);
     162           0 :     sws_f.lumH = sws_f.lumV = vec;
     163           0 :     sws_f.chrH = sws_f.chrV = NULL;
     164           0 :     f->pre_filter_context = sws_getContext(width, height, AV_PIX_FMT_GRAY8,
     165             :                                            width, height, AV_PIX_FMT_GRAY8,
     166             :                                            sws_flags, &sws_f, NULL, NULL);
     167           0 :     sws_freeVec(vec);
     168             : 
     169           0 :     vec = sws_getGaussianVec(f->strength, 5.0);
     170           0 :     for (i = 0; i < COLOR_DIFF_COEFF_SIZE; i++) {
     171             :         double d;
     172           0 :         int index = i-COLOR_DIFF_COEFF_SIZE/2 + vec->length/2;
     173             : 
     174           0 :         if (index < 0 || index >= vec->length) d = 0.0;
     175           0 :         else                                   d = vec->coeff[index];
     176             : 
     177           0 :         f->color_diff_coeff[i] = (int)(d/vec->coeff[vec->length/2]*(1<<12) + 0.5);
     178             :     }
     179           0 :     sws_freeVec(vec);
     180             : 
     181           0 :     vec = sws_getGaussianVec(f->radius, f->quality);
     182           0 :     f->dist_width    = vec->length;
     183           0 :     f->dist_linesize = FFALIGN(vec->length, 8);
     184           0 :     f->dist_coeff    = av_malloc_array(f->dist_width, f->dist_linesize * sizeof(*f->dist_coeff));
     185           0 :     if (!f->dist_coeff) {
     186           0 :         sws_freeVec(vec);
     187           0 :         return AVERROR(ENOMEM);
     188             :     }
     189             : 
     190           0 :     for (y = 0; y < vec->length; y++) {
     191           0 :         for (x = 0; x < vec->length; x++) {
     192           0 :             double d = vec->coeff[x] * vec->coeff[y];
     193           0 :             f->dist_coeff[x + y*f->dist_linesize] = (int)(d*(1<<10) + 0.5);
     194             :         }
     195             :     }
     196           0 :     sws_freeVec(vec);
     197             : 
     198           0 :     return 0;
     199             : }
     200             : 
     201           0 : static int config_props(AVFilterLink *inlink)
     202             : {
     203           0 :     SabContext *s = inlink->dst->priv;
     204           0 :     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
     205             :     int ret;
     206             : 
     207           0 :     s->hsub = desc->log2_chroma_w;
     208           0 :     s->vsub = desc->log2_chroma_h;
     209             : 
     210           0 :     close_filter_param(&s->luma);
     211           0 :     ret = open_filter_param(&s->luma, inlink->w, inlink->h, s->sws_flags);
     212           0 :     if (ret < 0)
     213           0 :         return ret;
     214             : 
     215           0 :     close_filter_param(&s->chroma);
     216           0 :     ret = open_filter_param(&s->chroma,
     217           0 :                             AV_CEIL_RSHIFT(inlink->w, s->hsub),
     218           0 :                             AV_CEIL_RSHIFT(inlink->h, s->vsub), s->sws_flags);
     219           0 :     return ret;
     220             : }
     221             : 
     222             : #define NB_PLANES 4
     223             : 
     224           0 : static void blur(uint8_t       *dst, const int dst_linesize,
     225             :                  const uint8_t *src, const int src_linesize,
     226             :                  const int w, const int h, FilterParam *fp)
     227             : {
     228             :     int x, y;
     229           0 :     FilterParam f = *fp;
     230           0 :     const int radius = f.dist_width/2;
     231             : 
     232           0 :     const uint8_t * const src2[NB_PLANES] = { src };
     233           0 :     int          src2_linesize[NB_PLANES] = { src_linesize };
     234           0 :     uint8_t     *dst2[NB_PLANES] = { f.pre_filter_buf };
     235           0 :     int dst2_linesize[NB_PLANES] = { f.pre_filter_linesize };
     236             : 
     237           0 :     sws_scale(f.pre_filter_context, src2, src2_linesize, 0, h, dst2, dst2_linesize);
     238             : 
     239             : #define UPDATE_FACTOR do {                                              \
     240             :         int factor;                                                     \
     241             :         factor = f.color_diff_coeff[COLOR_DIFF_COEFF_SIZE/2 + pre_val - \
     242             :                  f.pre_filter_buf[ix + iy*f.pre_filter_linesize]] * f.dist_coeff[dx + dy*f.dist_linesize]; \
     243             :         sum += src[ix + iy*src_linesize] * factor;                      \
     244             :         div += factor;                                                  \
     245             :     } while (0)
     246             : 
     247           0 :     for (y = 0; y < h; y++) {
     248           0 :         for (x = 0; x < w; x++) {
     249           0 :             int sum = 0;
     250           0 :             int div = 0;
     251             :             int dy;
     252           0 :             const int pre_val = f.pre_filter_buf[x + y*f.pre_filter_linesize];
     253           0 :             if (x >= radius && x < w - radius) {
     254           0 :                 for (dy = 0; dy < radius*2 + 1; dy++) {
     255             :                     int dx;
     256           0 :                     int iy = y+dy - radius;
     257           0 :                     iy = avpriv_mirror(iy, h-1);
     258             : 
     259           0 :                     for (dx = 0; dx < radius*2 + 1; dx++) {
     260           0 :                         const int ix = x+dx - radius;
     261           0 :                         UPDATE_FACTOR;
     262             :                     }
     263             :                 }
     264             :             } else {
     265           0 :                 for (dy = 0; dy < radius*2+1; dy++) {
     266             :                     int dx;
     267           0 :                     int iy = y+dy - radius;
     268           0 :                     iy = avpriv_mirror(iy, h-1);
     269             : 
     270           0 :                     for (dx = 0; dx < radius*2 + 1; dx++) {
     271           0 :                         int ix = x+dx - radius;
     272           0 :                         ix = avpriv_mirror(ix, w-1);
     273           0 :                         UPDATE_FACTOR;
     274             :                     }
     275             :                 }
     276             :             }
     277           0 :             dst[x + y*dst_linesize] = (sum + div/2) / div;
     278             :         }
     279             :     }
     280           0 : }
     281             : 
     282           0 : static int filter_frame(AVFilterLink *inlink, AVFrame *inpic)
     283             : {
     284           0 :     SabContext  *s = inlink->dst->priv;
     285           0 :     AVFilterLink *outlink = inlink->dst->outputs[0];
     286             :     AVFrame *outpic;
     287             : 
     288           0 :     outpic = ff_get_video_buffer(outlink, outlink->w, outlink->h);
     289           0 :     if (!outpic) {
     290           0 :         av_frame_free(&inpic);
     291           0 :         return AVERROR(ENOMEM);
     292             :     }
     293           0 :     av_frame_copy_props(outpic, inpic);
     294             : 
     295           0 :     blur(outpic->data[0], outpic->linesize[0], inpic->data[0],  inpic->linesize[0],
     296             :          inlink->w, inlink->h, &s->luma);
     297           0 :     if (inpic->data[2]) {
     298           0 :         int cw = AV_CEIL_RSHIFT(inlink->w, s->hsub);
     299           0 :         int ch = AV_CEIL_RSHIFT(inlink->h, s->vsub);
     300           0 :         blur(outpic->data[1], outpic->linesize[1], inpic->data[1], inpic->linesize[1], cw, ch, &s->chroma);
     301           0 :         blur(outpic->data[2], outpic->linesize[2], inpic->data[2], inpic->linesize[2], cw, ch, &s->chroma);
     302             :     }
     303             : 
     304           0 :     av_frame_free(&inpic);
     305           0 :     return ff_filter_frame(outlink, outpic);
     306             : }
     307             : 
     308             : static const AVFilterPad sab_inputs[] = {
     309             :     {
     310             :         .name         = "default",
     311             :         .type         = AVMEDIA_TYPE_VIDEO,
     312             :         .filter_frame = filter_frame,
     313             :         .config_props = config_props,
     314             :     },
     315             :     { NULL }
     316             : };
     317             : 
     318             : static const AVFilterPad sab_outputs[] = {
     319             :     {
     320             :         .name = "default",
     321             :         .type = AVMEDIA_TYPE_VIDEO,
     322             :     },
     323             :     { NULL }
     324             : };
     325             : 
     326             : AVFilter ff_vf_sab = {
     327             :     .name          = "sab",
     328             :     .description   = NULL_IF_CONFIG_SMALL("Apply shape adaptive blur."),
     329             :     .priv_size     = sizeof(SabContext),
     330             :     .init          = init,
     331             :     .uninit        = uninit,
     332             :     .query_formats = query_formats,
     333             :     .inputs        = sab_inputs,
     334             :     .outputs       = sab_outputs,
     335             :     .priv_class    = &sab_class,
     336             :     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
     337             : };

Generated by: LCOV version 1.13