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

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2011 Pascal Getreuer
       3             :  * Copyright (c) 2016 Paul B Mahol
       4             :  *
       5             :  * Redistribution and use in source and binary forms, with or without modification,
       6             :  * are permitted provided that the following conditions are met:
       7             :  *
       8             :  *  * Redistributions of source code must retain the above copyright
       9             :  *    notice, this list of conditions and the following disclaimer.
      10             :  *  * Redistributions in binary form must reproduce the above
      11             :  *    copyright notice, this list of conditions and the following
      12             :  *    disclaimer in the documentation and/or other materials provided
      13             :  *    with the distribution.
      14             :  *
      15             :  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
      16             :  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
      17             :  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
      18             :  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
      19             :  * HOLDER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
      20             :  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
      21             :  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
      22             :  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
      23             :  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
      24             :  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
      25             :  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      26             :  */
      27             : 
      28             : #include "libavutil/imgutils.h"
      29             : #include "libavutil/opt.h"
      30             : #include "libavutil/pixdesc.h"
      31             : #include "avfilter.h"
      32             : #include "formats.h"
      33             : #include "internal.h"
      34             : #include "video.h"
      35             : 
      36             : typedef struct GBlurContext {
      37             :     const AVClass *class;
      38             : 
      39             :     float sigma;
      40             :     float sigmaV;
      41             :     int steps;
      42             :     int planes;
      43             : 
      44             :     int depth;
      45             :     int planewidth[4];
      46             :     int planeheight[4];
      47             :     float *buffer;
      48             :     float boundaryscale;
      49             :     float boundaryscaleV;
      50             :     float postscale;
      51             :     float postscaleV;
      52             :     float nu;
      53             :     float nuV;
      54             :     int nb_planes;
      55             : } GBlurContext;
      56             : 
      57             : #define OFFSET(x) offsetof(GBlurContext, x)
      58             : #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
      59             : 
      60             : static const AVOption gblur_options[] = {
      61             :     { "sigma",  "set sigma",            OFFSET(sigma),  AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0.0, 1024, FLAGS },
      62             :     { "steps",  "set number of steps",  OFFSET(steps),  AV_OPT_TYPE_INT,   {.i64=1},     1,    6, FLAGS },
      63             :     { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT,   {.i64=0xF},   0,  0xF, FLAGS },
      64             :     { "sigmaV", "set vertical sigma",   OFFSET(sigmaV), AV_OPT_TYPE_FLOAT, {.dbl=-1},   -1, 1024, FLAGS },
      65             :     { NULL }
      66             : };
      67             : 
      68             : AVFILTER_DEFINE_CLASS(gblur);
      69             : 
      70             : typedef struct ThreadData {
      71             :     int height;
      72             :     int width;
      73             : } ThreadData;
      74             : 
      75           0 : static int filter_horizontally(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
      76             : {
      77           0 :     GBlurContext *s = ctx->priv;
      78           0 :     ThreadData *td = arg;
      79           0 :     const int height = td->height;
      80           0 :     const int width = td->width;
      81           0 :     const int slice_start = (height *  jobnr   ) / nb_jobs;
      82           0 :     const int slice_end   = (height * (jobnr+1)) / nb_jobs;
      83           0 :     const float boundaryscale = s->boundaryscale;
      84           0 :     const int steps = s->steps;
      85           0 :     const float nu = s->nu;
      86           0 :     float *buffer = s->buffer;
      87             :     int y, x, step;
      88             :     float *ptr;
      89             : 
      90             :     /* Filter horizontally along each row */
      91           0 :     for (y = slice_start; y < slice_end; y++) {
      92           0 :         for (step = 0; step < steps; step++) {
      93           0 :             ptr = buffer + width * y;
      94           0 :             ptr[0] *= boundaryscale;
      95             : 
      96             :             /* Filter rightwards */
      97           0 :             for (x = 1; x < width; x++)
      98           0 :                 ptr[x] += nu * ptr[x - 1];
      99             : 
     100           0 :             ptr[x = width - 1] *= boundaryscale;
     101             : 
     102             :             /* Filter leftwards */
     103           0 :             for (; x > 0; x--)
     104           0 :                 ptr[x - 1] += nu * ptr[x];
     105             :         }
     106             :     }
     107             : 
     108           0 :     return 0;
     109             : }
     110             : 
     111           0 : static int filter_vertically(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
     112             : {
     113           0 :     GBlurContext *s = ctx->priv;
     114           0 :     ThreadData *td = arg;
     115           0 :     const int height = td->height;
     116           0 :     const int width = td->width;
     117           0 :     const int slice_start = (width *  jobnr   ) / nb_jobs;
     118           0 :     const int slice_end   = (width * (jobnr+1)) / nb_jobs;
     119           0 :     const float boundaryscale = s->boundaryscaleV;
     120           0 :     const int numpixels = width * height;
     121           0 :     const int steps = s->steps;
     122           0 :     const float nu = s->nuV;
     123           0 :     float *buffer = s->buffer;
     124             :     int i, x, step;
     125             :     float *ptr;
     126             : 
     127             :     /* Filter vertically along each column */
     128           0 :     for (x = slice_start; x < slice_end; x++) {
     129           0 :         for (step = 0; step < steps; step++) {
     130           0 :             ptr = buffer + x;
     131           0 :             ptr[0] *= boundaryscale;
     132             : 
     133             :             /* Filter downwards */
     134           0 :             for (i = width; i < numpixels; i += width)
     135           0 :                 ptr[i] += nu * ptr[i - width];
     136             : 
     137           0 :             ptr[i = numpixels - width] *= boundaryscale;
     138             : 
     139             :             /* Filter upwards */
     140           0 :             for (; i > 0; i -= width)
     141           0 :                 ptr[i - width] += nu * ptr[i];
     142             :         }
     143             :     }
     144             : 
     145           0 :     return 0;
     146             : }
     147             : 
     148             : 
     149           0 : static int filter_postscale(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
     150             : {
     151           0 :     GBlurContext *s = ctx->priv;
     152           0 :     ThreadData *td = arg;
     153           0 :     const int height = td->height;
     154           0 :     const int width = td->width;
     155           0 :     const int64_t numpixels = width * (int64_t)height;
     156           0 :     const unsigned slice_start = (numpixels *  jobnr   ) / nb_jobs;
     157           0 :     const unsigned slice_end   = (numpixels * (jobnr+1)) / nb_jobs;
     158           0 :     const float postscale = s->postscale * s->postscaleV;
     159           0 :     float *buffer = s->buffer;
     160             :     unsigned i;
     161             : 
     162           0 :     for (i = slice_start; i < slice_end; i++)
     163           0 :         buffer[i] *= postscale;
     164             : 
     165           0 :     return 0;
     166             : }
     167             : 
     168           0 : static void gaussianiir2d(AVFilterContext *ctx, int plane)
     169             : {
     170           0 :     GBlurContext *s = ctx->priv;
     171           0 :     const int width = s->planewidth[plane];
     172           0 :     const int height = s->planeheight[plane];
     173           0 :     const int nb_threads = ff_filter_get_nb_threads(ctx);
     174             :     ThreadData td;
     175             : 
     176           0 :     if (s->sigma <= 0 || s->steps < 0)
     177           0 :         return;
     178             : 
     179           0 :     td.width = width;
     180           0 :     td.height = height;
     181           0 :     ctx->internal->execute(ctx, filter_horizontally, &td, NULL, FFMIN(height, nb_threads));
     182           0 :     ctx->internal->execute(ctx, filter_vertically, &td, NULL, FFMIN(width, nb_threads));
     183           0 :     ctx->internal->execute(ctx, filter_postscale, &td, NULL, FFMIN(width * height, nb_threads));
     184             : }
     185             : 
     186           0 : static int query_formats(AVFilterContext *ctx)
     187             : {
     188             :     static const enum AVPixelFormat pix_fmts[] = {
     189             :         AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
     190             :         AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
     191             :         AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
     192             :         AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
     193             :         AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
     194             :         AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
     195             :         AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
     196             :         AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
     197             :         AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
     198             :         AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
     199             :         AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
     200             :         AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
     201             :         AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
     202             :         AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
     203             :         AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
     204             :         AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
     205             :         AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY16,
     206             :         AV_PIX_FMT_NONE
     207             :     };
     208             : 
     209           0 :     return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
     210             : }
     211             : 
     212           0 : static int config_input(AVFilterLink *inlink)
     213             : {
     214           0 :     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
     215           0 :     GBlurContext *s = inlink->dst->priv;
     216             : 
     217           0 :     s->depth = desc->comp[0].depth;
     218           0 :     s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
     219           0 :     s->planewidth[0] = s->planewidth[3] = inlink->w;
     220           0 :     s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
     221           0 :     s->planeheight[0] = s->planeheight[3] = inlink->h;
     222             : 
     223           0 :     s->nb_planes = av_pix_fmt_count_planes(inlink->format);
     224             : 
     225           0 :     s->buffer = av_malloc_array(inlink->w, inlink->h * sizeof(*s->buffer));
     226           0 :     if (!s->buffer)
     227           0 :         return AVERROR(ENOMEM);
     228             : 
     229           0 :     if (s->sigmaV < 0) {
     230           0 :         s->sigmaV = s->sigma;
     231             :     }
     232             : 
     233           0 :     return 0;
     234             : }
     235             : 
     236           0 : static void set_params(float sigma, int steps, float *postscale, float *boundaryscale, float *nu)
     237             : {
     238             :     double dnu, lambda;
     239             : 
     240           0 :     lambda = (sigma * sigma) / (2.0 * steps);
     241           0 :     dnu = (1.0 + 2.0 * lambda - sqrt(1.0 + 4.0 * lambda)) / (2.0 * lambda);
     242           0 :     *postscale = pow(dnu / lambda, steps);
     243           0 :     *boundaryscale = 1.0 / (1.0 - dnu);
     244           0 :     *nu = (float)dnu;
     245           0 : }
     246             : 
     247           0 : static int filter_frame(AVFilterLink *inlink, AVFrame *in)
     248             : {
     249           0 :     AVFilterContext *ctx = inlink->dst;
     250           0 :     GBlurContext *s = ctx->priv;
     251           0 :     AVFilterLink *outlink = ctx->outputs[0];
     252             :     AVFrame *out;
     253             :     int plane;
     254             : 
     255           0 :     set_params(s->sigma,  s->steps, &s->postscale,  &s->boundaryscale,  &s->nu);
     256           0 :     set_params(s->sigmaV, s->steps, &s->postscaleV, &s->boundaryscaleV, &s->nuV);
     257             : 
     258           0 :     if (av_frame_is_writable(in)) {
     259           0 :         out = in;
     260             :     } else {
     261           0 :         out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
     262           0 :         if (!out) {
     263           0 :             av_frame_free(&in);
     264           0 :             return AVERROR(ENOMEM);
     265             :         }
     266           0 :         av_frame_copy_props(out, in);
     267             :     }
     268             : 
     269           0 :     for (plane = 0; plane < s->nb_planes; plane++) {
     270           0 :         const int height = s->planeheight[plane];
     271           0 :         const int width = s->planewidth[plane];
     272           0 :         float *bptr = s->buffer;
     273           0 :         const uint8_t *src = in->data[plane];
     274           0 :         const uint16_t *src16 = (const uint16_t *)in->data[plane];
     275           0 :         uint8_t *dst = out->data[plane];
     276           0 :         uint16_t *dst16 = (uint16_t *)out->data[plane];
     277             :         int y, x;
     278             : 
     279           0 :         if (!s->sigma || !(s->planes & (1 << plane))) {
     280           0 :             if (out != in)
     281           0 :                 av_image_copy_plane(out->data[plane], out->linesize[plane],
     282           0 :                                     in->data[plane], in->linesize[plane],
     283           0 :                                     width * ((s->depth + 7) / 8), height);
     284           0 :             continue;
     285             :         }
     286             : 
     287           0 :         if (s->depth == 8) {
     288           0 :             for (y = 0; y < height; y++) {
     289           0 :                 for (x = 0; x < width; x++) {
     290           0 :                     bptr[x] = src[x];
     291             :                 }
     292           0 :                 bptr += width;
     293           0 :                 src += in->linesize[plane];
     294             :             }
     295             :         } else {
     296           0 :             for (y = 0; y < height; y++) {
     297           0 :                 for (x = 0; x < width; x++) {
     298           0 :                     bptr[x] = src16[x];
     299             :                 }
     300           0 :                 bptr += width;
     301           0 :                 src16 += in->linesize[plane] / 2;
     302             :             }
     303             :         }
     304             : 
     305           0 :         gaussianiir2d(ctx, plane);
     306             : 
     307           0 :         bptr = s->buffer;
     308           0 :         if (s->depth == 8) {
     309           0 :             for (y = 0; y < height; y++) {
     310           0 :                 for (x = 0; x < width; x++) {
     311           0 :                     dst[x] = bptr[x];
     312             :                 }
     313           0 :                 bptr += width;
     314           0 :                 dst += out->linesize[plane];
     315             :             }
     316             :         } else {
     317           0 :             for (y = 0; y < height; y++) {
     318           0 :                 for (x = 0; x < width; x++) {
     319           0 :                     dst16[x] = bptr[x];
     320             :                 }
     321           0 :                 bptr += width;
     322           0 :                 dst16 += out->linesize[plane] / 2;
     323             :             }
     324             :         }
     325             :     }
     326             : 
     327           0 :     if (out != in)
     328           0 :         av_frame_free(&in);
     329           0 :     return ff_filter_frame(outlink, out);
     330             : }
     331             : 
     332           0 : static av_cold void uninit(AVFilterContext *ctx)
     333             : {
     334           0 :     GBlurContext *s = ctx->priv;
     335             : 
     336           0 :     av_freep(&s->buffer);
     337           0 : }
     338             : 
     339             : static const AVFilterPad gblur_inputs[] = {
     340             :     {
     341             :         .name         = "default",
     342             :         .type         = AVMEDIA_TYPE_VIDEO,
     343             :         .config_props = config_input,
     344             :         .filter_frame = filter_frame,
     345             :     },
     346             :     { NULL }
     347             : };
     348             : 
     349             : static const AVFilterPad gblur_outputs[] = {
     350             :     {
     351             :         .name = "default",
     352             :         .type = AVMEDIA_TYPE_VIDEO,
     353             :     },
     354             :     { NULL }
     355             : };
     356             : 
     357             : AVFilter ff_vf_gblur = {
     358             :     .name          = "gblur",
     359             :     .description   = NULL_IF_CONFIG_SMALL("Apply Gaussian Blur filter."),
     360             :     .priv_size     = sizeof(GBlurContext),
     361             :     .priv_class    = &gblur_class,
     362             :     .uninit        = uninit,
     363             :     .query_formats = query_formats,
     364             :     .inputs        = gblur_inputs,
     365             :     .outputs       = gblur_outputs,
     366             :     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
     367             : };

Generated by: LCOV version 1.13