LCOV - code coverage report
Current view: top level - libavfilter - vf_neighbor.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 145 0.0 %
Date: 2018-05-20 11:54:08 Functions: 0 12 0.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2012-2013 Oka Motofumi (chikuzen.mo at gmail dot com)
       3             :  * Copyright (c) 2015 Paul B Mahol
       4             :  *
       5             :  * This file is part of FFmpeg.
       6             :  *
       7             :  * FFmpeg is free software; you can redistribute it and/or
       8             :  * modify it under the terms of the GNU Lesser General Public
       9             :  * License as published by the Free Software Foundation; either
      10             :  * version 2.1 of the License, or (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 GNU
      15             :  * Lesser General Public License for more details.
      16             :  *
      17             :  * You should have received a copy of the GNU Lesser General Public
      18             :  * License along with FFmpeg; if not, write to the Free Software
      19             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      20             :  */
      21             : 
      22             : #include "libavutil/imgutils.h"
      23             : #include "libavutil/intreadwrite.h"
      24             : #include "libavutil/pixdesc.h"
      25             : #include "libavutil/opt.h"
      26             : #include "avfilter.h"
      27             : #include "formats.h"
      28             : #include "internal.h"
      29             : #include "video.h"
      30             : 
      31             : typedef struct ThreadData {
      32             :     AVFrame *in, *out;
      33             : } ThreadData;
      34             : 
      35             : typedef struct NContext {
      36             :     const AVClass *class;
      37             :     int planeheight[4];
      38             :     int planewidth[4];
      39             :     int nb_planes;
      40             :     int threshold[4];
      41             :     int coordinates;
      42             : 
      43             :     int depth;
      44             :     int bpc;
      45             : 
      46             :     void (*filter)(uint8_t *dst, const uint8_t *p1, int width,
      47             :                    int threshold, const uint8_t *coordinates[], int coord);
      48             : } NContext;
      49             : 
      50           0 : static int query_formats(AVFilterContext *ctx)
      51             : {
      52             :     static const enum AVPixelFormat pix_fmts[] = {
      53             :         AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
      54             :         AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
      55             :         AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
      56             :         AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
      57             :         AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
      58             :         AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
      59             :         AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
      60             :         AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
      61             :         AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
      62             :         AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
      63             :         AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
      64             :         AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
      65             :         AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
      66             :         AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
      67             :         AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
      68             :         AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
      69             :         AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY16,
      70             :         AV_PIX_FMT_NONE
      71             :     };
      72             : 
      73           0 :     return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
      74             : }
      75             : 
      76           0 : static void erosion(uint8_t *dst, const uint8_t *p1, int width,
      77             :                     int threshold, const uint8_t *coordinates[], int coord)
      78             : {
      79             :     int x, i;
      80             : 
      81           0 :     for (x = 0; x < width; x++) {
      82           0 :         int min = p1[x];
      83           0 :         int limit = FFMAX(min - threshold, 0);
      84             : 
      85           0 :         for (i = 0; i < 8; i++) {
      86           0 :             if (coord & (1 << i)) {
      87           0 :                 min = FFMIN(min, *(coordinates[i] + x));
      88             :             }
      89           0 :             min = FFMAX(min, limit);
      90             :         }
      91             : 
      92           0 :         dst[x] = min;
      93             :     }
      94           0 : }
      95             : 
      96           0 : static void erosion16(uint8_t *dstp, const uint8_t *p1, int width,
      97             :                       int threshold, const uint8_t *coordinates[], int coord)
      98             : {
      99           0 :     uint16_t *dst = (uint16_t *)dstp;
     100             :     int x, i;
     101             : 
     102           0 :     for (x = 0; x < width; x++) {
     103           0 :         int min = AV_RN16A(&p1[2 * x]);
     104           0 :         int limit = FFMAX(min - threshold, 0);
     105             : 
     106           0 :         for (i = 0; i < 8; i++) {
     107           0 :             if (coord & (1 << i)) {
     108           0 :                 min = FFMIN(min, AV_RN16A(coordinates[i] + x * 2));
     109             :             }
     110           0 :             min = FFMAX(min, limit);
     111             :         }
     112             : 
     113           0 :         dst[x] = min;
     114             :     }
     115           0 : }
     116             : 
     117           0 : static void dilation(uint8_t *dst, const uint8_t *p1, int width,
     118             :                      int threshold, const uint8_t *coordinates[], int coord)
     119             : {
     120             :     int x, i;
     121             : 
     122           0 :     for (x = 0; x < width; x++) {
     123           0 :         int max = p1[x];
     124           0 :         int limit = FFMIN(max + threshold, 255);
     125             : 
     126           0 :         for (i = 0; i < 8; i++) {
     127           0 :             if (coord & (1 << i)) {
     128           0 :                 max = FFMAX(max, *(coordinates[i] + x));
     129             :             }
     130           0 :             max = FFMIN(max, limit);
     131             :         }
     132             : 
     133           0 :         dst[x] = max;
     134             :     }
     135           0 : }
     136             : 
     137           0 : static void dilation16(uint8_t *dstp, const uint8_t *p1, int width,
     138             :                        int threshold, const uint8_t *coordinates[], int coord)
     139             : {
     140           0 :     uint16_t *dst = (uint16_t *)dstp;
     141             :     int x, i;
     142             : 
     143           0 :     for (x = 0; x < width; x++) {
     144           0 :         int max = AV_RN16A(&p1[x * 2]);
     145           0 :         int limit = FFMIN(max + threshold, 255);
     146             : 
     147           0 :         for (i = 0; i < 8; i++) {
     148           0 :             if (coord & (1 << i)) {
     149           0 :                 max = FFMAX(max, AV_RN16A(coordinates[i] + x * 2));
     150             :             }
     151           0 :             max = FFMIN(max, limit);
     152             :         }
     153             : 
     154           0 :         dst[x] = max;
     155             :     }
     156           0 : }
     157             : 
     158           0 : static void deflate(uint8_t *dst, const uint8_t *p1, int width,
     159             :                     int threshold, const uint8_t *coordinates[], int coord)
     160             : {
     161             :     int x, i;
     162             : 
     163           0 :     for (x = 0; x < width; x++) {
     164           0 :         int sum = 0;
     165           0 :         int limit = FFMAX(p1[x] - threshold, 0);
     166             : 
     167           0 :         for (i = 0; i < 8; sum += *(coordinates[i++] + x));
     168             : 
     169           0 :         dst[x] = FFMAX(FFMIN(sum / 8, p1[x]), limit);
     170             :     }
     171           0 : }
     172             : 
     173           0 : static void deflate16(uint8_t *dstp, const uint8_t *p1, int width,
     174             :                       int threshold, const uint8_t *coordinates[], int coord)
     175             : {
     176           0 :     uint16_t *dst = (uint16_t *)dstp;
     177             :     int x, i;
     178             : 
     179           0 :     for (x = 0; x < width; x++) {
     180           0 :         int sum = 0;
     181           0 :         int limit = FFMAX(AV_RN16A(&p1[2 * x]) - threshold, 0);
     182             : 
     183           0 :         for (i = 0; i < 8; sum += AV_RN16A(coordinates[i++] + x * 2));
     184             : 
     185           0 :         dst[x] = FFMAX(FFMIN(sum / 8, p1[x]), limit);
     186             :     }
     187           0 : }
     188             : 
     189           0 : static void inflate(uint8_t *dst, const uint8_t *p1, int width,
     190             :                     int threshold, const uint8_t *coordinates[], int coord)
     191             : {
     192             :     int x, i;
     193             : 
     194           0 :     for (x = 0; x < width; x++) {
     195           0 :         int sum = 0;
     196           0 :         int limit = FFMIN(p1[x] + threshold, 255);
     197             : 
     198           0 :         for (i = 0; i < 8; sum += *(coordinates[i++] + x));
     199             : 
     200           0 :         dst[x] = FFMIN(FFMAX(sum / 8, p1[x]), limit);
     201             :     }
     202           0 : }
     203             : 
     204           0 : static void inflate16(uint8_t *dstp, const uint8_t *p1, int width,
     205             :                       int threshold, const uint8_t *coordinates[], int coord)
     206             : {
     207           0 :     uint16_t *dst = (uint16_t *)dstp;
     208             :     int x, i;
     209             : 
     210           0 :     for (x = 0; x < width; x++) {
     211           0 :         int sum = 0;
     212           0 :         int limit = FFMIN(AV_RN16A(&p1[2 * x]) + threshold, 255);
     213             : 
     214           0 :         for (i = 0; i < 8; sum += AV_RN16A(coordinates[i++] + x * 2));
     215             : 
     216           0 :         dst[x] = FFMIN(FFMAX(sum / 8, p1[x]), limit);
     217             :     }
     218           0 : }
     219             : 
     220           0 : static int config_input(AVFilterLink *inlink)
     221             : {
     222           0 :     AVFilterContext *ctx = inlink->dst;
     223           0 :     NContext *s = ctx->priv;
     224           0 :     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
     225             : 
     226           0 :     s->depth = desc->comp[0].depth;
     227           0 :     s->bpc = (s->depth + 7) / 8;
     228             : 
     229           0 :     s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
     230           0 :     s->planewidth[0] = s->planewidth[3] = inlink->w;
     231           0 :     s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
     232           0 :     s->planeheight[0] = s->planeheight[3] = inlink->h;
     233             : 
     234           0 :     s->nb_planes = av_pix_fmt_count_planes(inlink->format);
     235             : 
     236           0 :     if (!strcmp(ctx->filter->name, "erosion"))
     237           0 :         s->filter = s->depth > 8 ? erosion16 : erosion;
     238           0 :     else if (!strcmp(ctx->filter->name, "dilation"))
     239           0 :         s->filter = s->depth > 8 ? dilation16 : dilation;
     240           0 :     else if (!strcmp(ctx->filter->name, "deflate"))
     241           0 :         s->filter = s->depth > 8 ? deflate16 : deflate;
     242           0 :     else if (!strcmp(ctx->filter->name, "inflate"))
     243           0 :         s->filter = s->depth > 8 ? inflate16 : inflate;
     244             : 
     245           0 :     return 0;
     246             : }
     247             : 
     248           0 : static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
     249             : {
     250           0 :     NContext *s = ctx->priv;
     251           0 :     ThreadData *td = arg;
     252           0 :     AVFrame *out = td->out;
     253           0 :     AVFrame *in = td->in;
     254             :     int plane, y;
     255             : 
     256           0 :     for (plane = 0; plane < s->nb_planes; plane++) {
     257           0 :         const int bpc = s->bpc;
     258           0 :         const int threshold = s->threshold[plane];
     259           0 :         const int stride = in->linesize[plane];
     260           0 :         const int dstride = out->linesize[plane];
     261           0 :         const int height = s->planeheight[plane];
     262           0 :         const int width  = s->planewidth[plane];
     263           0 :         const int slice_start = (height * jobnr) / nb_jobs;
     264           0 :         const int slice_end = (height * (jobnr+1)) / nb_jobs;
     265           0 :         const uint8_t *src = (const uint8_t *)in->data[plane] + slice_start * stride;
     266           0 :         uint8_t *dst = out->data[plane] + slice_start * dstride;
     267             : 
     268           0 :         if (!threshold) {
     269           0 :             av_image_copy_plane(dst, dstride, src, stride, width * bpc, slice_end - slice_start);
     270           0 :             continue;
     271             :         }
     272             : 
     273           0 :         for (y = slice_start; y < slice_end; y++) {
     274           0 :             const int nh = y > 0;
     275           0 :             const int ph = y < height - 1;
     276           0 :             const uint8_t *coordinates[] = { src - nh * stride, src + 1 * bpc - nh * stride, src + 2 * bpc - nh * stride,
     277           0 :                                              src,                                            src + 2 * bpc,
     278           0 :                                              src + ph * stride, src + 1 * bpc + ph * stride, src + 2 * bpc + ph * stride};
     279             : 
     280           0 :             const uint8_t *coordinateslb[] = { src + 1 * bpc - nh * stride, src - nh * stride, src + 1 * bpc - nh * stride,
     281           0 :                                                src + 1 * bpc,                                  src + 1 * bpc,
     282           0 :                                                src + 1 * bpc + ph * stride, src + ph * stride, src + 1 * bpc + ph * stride};
     283             : 
     284           0 :             const uint8_t *coordinatesrb[] = { src + (width - 2) * bpc - nh * stride, src + (width - 1) * bpc - nh * stride, src + (width - 2) * bpc - nh * stride,
     285           0 :                                                src + (width - 2) * bpc,                                                      src + (width - 2) * bpc,
     286           0 :                                                src + (width - 2) * bpc + ph * stride, src + (width - 1) * bpc + ph * stride, src + (width - 2) * bpc + ph * stride};
     287             : 
     288           0 :             s->filter(dst,                     src,                     1,         threshold, coordinateslb, s->coordinates);
     289           0 :             s->filter(dst          + 1  * bpc, src          + 1  * bpc, width - 2, threshold, coordinates,   s->coordinates);
     290           0 :             s->filter(dst + (width - 1) * bpc, src + (width - 1) * bpc, 1,         threshold, coordinatesrb, s->coordinates);
     291             : 
     292           0 :             src += stride;
     293           0 :             dst += dstride;
     294             :         }
     295             :     }
     296             : 
     297           0 :     return 0;
     298             : }
     299             : 
     300           0 : static int filter_frame(AVFilterLink *inlink, AVFrame *in)
     301             : {
     302           0 :     AVFilterContext *ctx = inlink->dst;
     303           0 :     AVFilterLink *outlink = ctx->outputs[0];
     304           0 :     NContext *s = ctx->priv;
     305             :     ThreadData td;
     306             :     AVFrame *out;
     307             : 
     308           0 :     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
     309           0 :     if (!out) {
     310           0 :         av_frame_free(&in);
     311           0 :         return AVERROR(ENOMEM);
     312             :     }
     313           0 :     av_frame_copy_props(out, in);
     314             : 
     315           0 :     td.in = in;
     316           0 :     td.out = out;
     317           0 :     ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(s->planeheight[1], ff_filter_get_nb_threads(ctx)));
     318             : 
     319           0 :     av_frame_free(&in);
     320           0 :     return ff_filter_frame(outlink, out);
     321             : }
     322             : 
     323             : static const AVFilterPad neighbor_inputs[] = {
     324             :     {
     325             :         .name         = "default",
     326             :         .type         = AVMEDIA_TYPE_VIDEO,
     327             :         .filter_frame = filter_frame,
     328             :         .config_props = config_input,
     329             :     },
     330             :     { NULL }
     331             : };
     332             : 
     333             : static const AVFilterPad neighbor_outputs[] = {
     334             :     {
     335             :         .name = "default",
     336             :         .type = AVMEDIA_TYPE_VIDEO,
     337             :     },
     338             :     { NULL }
     339             : };
     340             : 
     341             : #define OFFSET(x) offsetof(NContext, x)
     342             : #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
     343             : 
     344             : #define DEFINE_NEIGHBOR_FILTER(name_, description_)          \
     345             : AVFILTER_DEFINE_CLASS(name_);                                \
     346             :                                                              \
     347             : AVFilter ff_vf_##name_ = {                                   \
     348             :     .name          = #name_,                                 \
     349             :     .description   = NULL_IF_CONFIG_SMALL(description_),     \
     350             :     .priv_size     = sizeof(NContext),                       \
     351             :     .priv_class    = &name_##_class,                         \
     352             :     .query_formats = query_formats,                          \
     353             :     .inputs        = neighbor_inputs,                        \
     354             :     .outputs       = neighbor_outputs,                       \
     355             :     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC| \
     356             :                      AVFILTER_FLAG_SLICE_THREADS,            \
     357             : }
     358             : 
     359             : #if CONFIG_EROSION_FILTER
     360             : 
     361             : static const AVOption erosion_options[] = {
     362             :     { "threshold0",  "set threshold for 1st plane",   OFFSET(threshold[0]),   AV_OPT_TYPE_INT, {.i64=65535}, 0, 65535, FLAGS },
     363             :     { "threshold1",  "set threshold for 2nd plane",   OFFSET(threshold[1]),   AV_OPT_TYPE_INT, {.i64=65535}, 0, 65535, FLAGS },
     364             :     { "threshold2",  "set threshold for 3rd plane",   OFFSET(threshold[2]),   AV_OPT_TYPE_INT, {.i64=65535}, 0, 65535, FLAGS },
     365             :     { "threshold3",  "set threshold for 4th plane",   OFFSET(threshold[3]),   AV_OPT_TYPE_INT, {.i64=65535}, 0, 65535, FLAGS },
     366             :     { "coordinates", "set coordinates",               OFFSET(coordinates),    AV_OPT_TYPE_INT, {.i64=255},   0, 255,   FLAGS },
     367             :     { NULL }
     368             : };
     369             : 
     370             : DEFINE_NEIGHBOR_FILTER(erosion, "Apply erosion effect.");
     371             : 
     372             : #endif /* CONFIG_EROSION_FILTER */
     373             : 
     374             : #if CONFIG_DILATION_FILTER
     375             : 
     376             : static const AVOption dilation_options[] = {
     377             :     { "threshold0",  "set threshold for 1st plane",   OFFSET(threshold[0]),   AV_OPT_TYPE_INT, {.i64=65535}, 0, 65535, FLAGS },
     378             :     { "threshold1",  "set threshold for 2nd plane",   OFFSET(threshold[1]),   AV_OPT_TYPE_INT, {.i64=65535}, 0, 65535, FLAGS },
     379             :     { "threshold2",  "set threshold for 3rd plane",   OFFSET(threshold[2]),   AV_OPT_TYPE_INT, {.i64=65535}, 0, 65535, FLAGS },
     380             :     { "threshold3",  "set threshold for 4th plane",   OFFSET(threshold[3]),   AV_OPT_TYPE_INT, {.i64=65535}, 0, 65535, FLAGS },
     381             :     { "coordinates", "set coordinates",               OFFSET(coordinates),    AV_OPT_TYPE_INT, {.i64=255},   0, 255,   FLAGS },
     382             :     { NULL }
     383             : };
     384             : 
     385             : DEFINE_NEIGHBOR_FILTER(dilation, "Apply dilation effect.");
     386             : 
     387             : #endif /* CONFIG_DILATION_FILTER */
     388             : 
     389             : #if CONFIG_DEFLATE_FILTER
     390             : 
     391             : static const AVOption deflate_options[] = {
     392             :     { "threshold0", "set threshold for 1st plane",   OFFSET(threshold[0]),   AV_OPT_TYPE_INT, {.i64=65535}, 0, 65535, FLAGS },
     393             :     { "threshold1", "set threshold for 2nd plane",   OFFSET(threshold[1]),   AV_OPT_TYPE_INT, {.i64=65535}, 0, 65535, FLAGS },
     394             :     { "threshold2", "set threshold for 3rd plane",   OFFSET(threshold[2]),   AV_OPT_TYPE_INT, {.i64=65535}, 0, 65535, FLAGS },
     395             :     { "threshold3", "set threshold for 4th plane",   OFFSET(threshold[3]),   AV_OPT_TYPE_INT, {.i64=65535}, 0, 65535, FLAGS },
     396             :     { NULL }
     397             : };
     398             : 
     399             : DEFINE_NEIGHBOR_FILTER(deflate, "Apply deflate effect.");
     400             : 
     401             : #endif /* CONFIG_DEFLATE_FILTER */
     402             : 
     403             : #if CONFIG_INFLATE_FILTER
     404             : 
     405             : static const AVOption inflate_options[] = {
     406             :     { "threshold0", "set threshold for 1st plane",   OFFSET(threshold[0]),   AV_OPT_TYPE_INT, {.i64=65535}, 0, 65535, FLAGS },
     407             :     { "threshold1", "set threshold for 2nd plane",   OFFSET(threshold[1]),   AV_OPT_TYPE_INT, {.i64=65535}, 0, 65535, FLAGS },
     408             :     { "threshold2", "set threshold for 3rd plane",   OFFSET(threshold[2]),   AV_OPT_TYPE_INT, {.i64=65535}, 0, 65535, FLAGS },
     409             :     { "threshold3", "set threshold for 4th plane",   OFFSET(threshold[3]),   AV_OPT_TYPE_INT, {.i64=65535}, 0, 65535, FLAGS },
     410             :     { NULL }
     411             : };
     412             : 
     413             : DEFINE_NEIGHBOR_FILTER(inflate, "Apply inflate effect.");
     414             : 
     415             : #endif /* CONFIG_INFLATE_FILTER */

Generated by: LCOV version 1.13