LCOV - code coverage report
Current view: top level - libavfilter - vf_floodfill.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 197 0.0 %
Date: 2017-12-14 19:11:59 Functions: 0 23 0.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2017 Paul B Mahol
       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 "libavutil/opt.h"
      22             : #include "libavutil/imgutils.h"
      23             : #include "libavutil/intreadwrite.h"
      24             : #include "avfilter.h"
      25             : #include "formats.h"
      26             : #include "internal.h"
      27             : #include "video.h"
      28             : 
      29             : typedef struct Points {
      30             :     uint16_t x, y;
      31             : } Points;
      32             : 
      33             : typedef struct FloodfillContext {
      34             :     const AVClass *class;
      35             : 
      36             :     int x, y;
      37             :     int s0, s1, s2, s3;
      38             :     int d0, d1, d2, d3;
      39             : 
      40             :     int back, front;
      41             :     Points *points;
      42             : 
      43             :     int (*is_same)(AVFrame *frame, int x, int y,
      44             :                    unsigned s0, unsigned s1, unsigned s2, unsigned s3);
      45             :     void (*set_pixel)(AVFrame *frame, int x, int y,
      46             :                       unsigned d0, unsigned d1, unsigned d2, unsigned d3);
      47             :     void (*pick_pixel)(AVFrame *frame, int x, int y,
      48             :                        int *s0, int *s1, int *s2, int *s3);
      49             : } FloodfillContext;
      50             : 
      51           0 : static int is_inside(int x, int y, int w, int h)
      52             : {
      53           0 :     if (x >= 0 && x < w && y >= 0 && y < h)
      54           0 :         return 1;
      55           0 :     return 0;
      56             : }
      57             : 
      58           0 : static int is_same4(AVFrame *frame, int x, int y,
      59             :                     unsigned s0, unsigned s1, unsigned s2, unsigned s3)
      60             : {
      61           0 :     unsigned c0 = frame->data[0][y * frame->linesize[0] + x];
      62           0 :     unsigned c1 = frame->data[1][y * frame->linesize[1] + x];
      63           0 :     unsigned c2 = frame->data[2][y * frame->linesize[2] + x];
      64           0 :     unsigned c3 = frame->data[3][y * frame->linesize[3] + x];
      65             : 
      66           0 :     if (s0 == c0 && s1 == c1 && s2 == c2 && s3 == c3)
      67           0 :         return 1;
      68           0 :     return 0;
      69             : }
      70             : 
      71           0 : static int is_same4_16(AVFrame *frame, int x, int y,
      72             :                        unsigned s0, unsigned s1, unsigned s2, unsigned s3)
      73             : {
      74           0 :     unsigned c0 = AV_RN16(frame->data[0] + y * frame->linesize[0] + 2 * x);
      75           0 :     unsigned c1 = AV_RN16(frame->data[1] + y * frame->linesize[1] + 2 * x);
      76           0 :     unsigned c2 = AV_RN16(frame->data[2] + y * frame->linesize[2] + 2 * x);
      77           0 :     unsigned c3 = AV_RN16(frame->data[3] + y * frame->linesize[3] + 2 * x);
      78             : 
      79           0 :     if (s0 == c0 && s1 == c1 && s2 == c2 && s3 == c3)
      80           0 :         return 1;
      81           0 :     return 0;
      82             : }
      83             : 
      84           0 : static int is_same3(AVFrame *frame, int x, int y,
      85             :                     unsigned s0, unsigned s1, unsigned s2, unsigned s3)
      86             : {
      87           0 :     unsigned c0 = frame->data[0][y * frame->linesize[0] + x];
      88           0 :     unsigned c1 = frame->data[1][y * frame->linesize[1] + x];
      89           0 :     unsigned c2 = frame->data[2][y * frame->linesize[2] + x];
      90             : 
      91           0 :     if (s0 == c0 && s1 == c1 && s2 == c2)
      92           0 :         return 1;
      93           0 :     return 0;
      94             : }
      95             : 
      96           0 : static int is_same3_16(AVFrame *frame, int x, int y,
      97             :                        unsigned s0, unsigned s1, unsigned s2, unsigned s3)
      98             : {
      99           0 :     unsigned c0 = AV_RN16(frame->data[0] + y * frame->linesize[0] + 2 * x);
     100           0 :     unsigned c1 = AV_RN16(frame->data[1] + y * frame->linesize[1] + 2 * x);
     101           0 :     unsigned c2 = AV_RN16(frame->data[2] + y * frame->linesize[2] + 2 * x);
     102             : 
     103           0 :     if (s0 == c0 && s1 == c1 && s2 == c2)
     104           0 :         return 1;
     105           0 :     return 0;
     106             : }
     107             : 
     108           0 : static int is_same1(AVFrame *frame, int x, int y,
     109             :                     unsigned s0, unsigned s1, unsigned s2, unsigned s3)
     110             : {
     111           0 :     unsigned c0 = frame->data[0][y * frame->linesize[0] + x];
     112             : 
     113           0 :     if (s0 == c0)
     114           0 :         return 1;
     115           0 :     return 0;
     116             : }
     117             : 
     118           0 : static int is_same1_16(AVFrame *frame, int x, int y,
     119             :                        unsigned s0, unsigned s1, unsigned s2, unsigned s3)
     120             : {
     121           0 :     unsigned c0 = AV_RN16(frame->data[0] + y * frame->linesize[0] + 2 * x);
     122             : 
     123           0 :     if (s0 == c0)
     124           0 :         return 1;
     125           0 :     return 0;
     126             : }
     127             : 
     128           0 : static void set_pixel1(AVFrame *frame, int x, int y,
     129             :                        unsigned d0, unsigned d1, unsigned d2, unsigned d3)
     130             : {
     131           0 :     frame->data[0][y * frame->linesize[0] + x] = d0;
     132           0 : }
     133             : 
     134           0 : static void set_pixel1_16(AVFrame *frame, int x, int y,
     135             :                           unsigned d0, unsigned d1, unsigned d2, unsigned d3)
     136             : {
     137           0 :     AV_WN16(frame->data[0] + y * frame->linesize[0] + 2 * x, d0);
     138           0 : }
     139             : 
     140           0 : static void set_pixel3(AVFrame *frame, int x, int y,
     141             :                        unsigned d0, unsigned d1, unsigned d2, unsigned d3)
     142             : {
     143           0 :     frame->data[0][y * frame->linesize[0] + x] = d0;
     144           0 :     frame->data[1][y * frame->linesize[1] + x] = d1;
     145           0 :     frame->data[2][y * frame->linesize[2] + x] = d2;
     146           0 : }
     147             : 
     148           0 : static void set_pixel3_16(AVFrame *frame, int x, int y,
     149             :                           unsigned d0, unsigned d1, unsigned d2, unsigned d3)
     150             : {
     151           0 :     AV_WN16(frame->data[0] + y * frame->linesize[0] + 2 * x, d0);
     152           0 :     AV_WN16(frame->data[1] + y * frame->linesize[1] + 2 * x, d1);
     153           0 :     AV_WN16(frame->data[2] + y * frame->linesize[2] + 2 * x, d2);
     154           0 : }
     155             : 
     156           0 : static void set_pixel4(AVFrame *frame, int x, int y,
     157             :                        unsigned d0, unsigned d1, unsigned d2, unsigned d3)
     158             : {
     159           0 :     frame->data[0][y * frame->linesize[0] + x] = d0;
     160           0 :     frame->data[1][y * frame->linesize[1] + x] = d1;
     161           0 :     frame->data[2][y * frame->linesize[2] + x] = d2;
     162           0 :     frame->data[3][y * frame->linesize[3] + x] = d3;
     163           0 : }
     164             : 
     165           0 : static void set_pixel4_16(AVFrame *frame, int x, int y,
     166             :                           unsigned d0, unsigned d1, unsigned d2, unsigned d3)
     167             : {
     168           0 :     AV_WN16(frame->data[0] + y * frame->linesize[0] + 2 * x, d0);
     169           0 :     AV_WN16(frame->data[1] + y * frame->linesize[1] + 2 * x, d1);
     170           0 :     AV_WN16(frame->data[2] + y * frame->linesize[2] + 2 * x, d2);
     171           0 :     AV_WN16(frame->data[3] + y * frame->linesize[3] + 2 * x, d3);
     172           0 : }
     173             : 
     174           0 : static void pick_pixel1(AVFrame *frame, int x, int y,
     175             :                         int *s0, int *s1, int *s2, int *s3)
     176             : {
     177           0 :     if (*s0 < 0)
     178           0 :         *s0 = frame->data[0][y * frame->linesize[0] + x];
     179           0 : }
     180             : 
     181           0 : static void pick_pixel1_16(AVFrame *frame, int x, int y,
     182             :                            int *s0, int *s1, int *s2, int *s3)
     183             : {
     184           0 :     if (*s0 < 0)
     185           0 :         *s0 = AV_RN16(frame->data[0] + y * frame->linesize[0] + 2 * x);
     186           0 : }
     187             : 
     188           0 : static void pick_pixel3(AVFrame *frame, int x, int y,
     189             :                         int *s0, int *s1, int *s2, int *s3)
     190             : {
     191           0 :     if (*s0 < 0)
     192           0 :         *s0 = frame->data[0][y * frame->linesize[0] + x];
     193           0 :     if (*s1 < 0)
     194           0 :         *s1 = frame->data[1][y * frame->linesize[1] + x];
     195           0 :     if (*s2 < 0)
     196           0 :         *s2 = frame->data[2][y * frame->linesize[2] + x];
     197           0 : }
     198             : 
     199           0 : static void pick_pixel3_16(AVFrame *frame, int x, int y,
     200             :                            int *s0, int *s1, int *s2, int *s3)
     201             : {
     202           0 :     if (*s0 < 0)
     203           0 :         *s0 = AV_RN16(frame->data[0] + y * frame->linesize[0] + 2 * x);
     204           0 :     if (*s1 < 0)
     205           0 :         *s1 = AV_RN16(frame->data[1] + y * frame->linesize[1] + 2 * x);
     206           0 :     if (*s2 < 0)
     207           0 :         *s2 = AV_RN16(frame->data[2] + y * frame->linesize[2] + 2 * x);
     208           0 : }
     209             : 
     210           0 : static void pick_pixel4(AVFrame *frame, int x, int y,
     211             :                         int *s0, int *s1, int *s2, int *s3)
     212             : {
     213           0 :     if (*s0 < 0)
     214           0 :         *s0 = frame->data[0][y * frame->linesize[0] + x];
     215           0 :     if (*s1 < 0)
     216           0 :         *s1 = frame->data[1][y * frame->linesize[1] + x];
     217           0 :     if (*s2 < 0)
     218           0 :         *s2 = frame->data[2][y * frame->linesize[2] + x];
     219           0 :     if (*s3 < 0)
     220           0 :         *s3 = frame->data[3][y * frame->linesize[3] + x];
     221           0 : }
     222             : 
     223           0 : static void pick_pixel4_16(AVFrame *frame, int x, int y,
     224             :                            int *s0, int *s1, int *s2, int *s3)
     225             : {
     226           0 :     if (*s0 < 0)
     227           0 :         *s0 = AV_RN16(frame->data[0] + y * frame->linesize[0] + 2 * x);
     228           0 :     if (*s1 < 0)
     229           0 :         *s1 = AV_RN16(frame->data[1] + y * frame->linesize[1] + 2 * x);
     230           0 :     if (*s2 < 0)
     231           0 :         *s2 = AV_RN16(frame->data[2] + y * frame->linesize[2] + 2 * x);
     232           0 :     if (*s3 < 0)
     233           0 :         *s3 = AV_RN16(frame->data[3] + y * frame->linesize[3] + 2 * x);
     234           0 : }
     235             : 
     236           0 : static int config_input(AVFilterLink *inlink)
     237             : {
     238           0 :     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
     239           0 :     AVFilterContext *ctx = inlink->dst;
     240           0 :     FloodfillContext *s = ctx->priv;
     241           0 :     int nb_planes = av_pix_fmt_count_planes(inlink->format);
     242             :     int depth;
     243             : 
     244           0 :     depth = desc->comp[0].depth;
     245           0 :     if (depth == 8) {
     246           0 :         switch (nb_planes) {
     247           0 :         case 1: s->set_pixel  = set_pixel1;
     248           0 :                 s->is_same    = is_same1;
     249           0 :                 s->pick_pixel = pick_pixel1; break;
     250           0 :         case 3: s->set_pixel  = set_pixel3;
     251           0 :                 s->is_same    = is_same3;
     252           0 :                 s->pick_pixel = pick_pixel3; break;
     253           0 :         case 4: s->set_pixel  = set_pixel4;
     254           0 :                 s->is_same    = is_same4;
     255           0 :                 s->pick_pixel = pick_pixel4; break;
     256             :        }
     257             :     } else {
     258           0 :         switch (nb_planes) {
     259           0 :         case 1: s->set_pixel  = set_pixel1_16;
     260           0 :                 s->is_same    = is_same1_16;
     261           0 :                 s->pick_pixel = pick_pixel1_16; break;
     262           0 :         case 3: s->set_pixel  = set_pixel3_16;
     263           0 :                 s->is_same    = is_same3_16;
     264           0 :                 s->pick_pixel = pick_pixel3_16; break;
     265           0 :         case 4: s->set_pixel  = set_pixel4_16;
     266           0 :                 s->is_same    = is_same4_16;
     267           0 :                 s->pick_pixel = pick_pixel4_16; break;
     268             :        }
     269             :     }
     270             : 
     271           0 :     s->front = s->back = 0;
     272           0 :     s->points = av_calloc(inlink->w * inlink->h, 4 * sizeof(Points));
     273           0 :     if (!s->points)
     274           0 :         return AVERROR(ENOMEM);
     275             : 
     276           0 :     return 0;
     277             : }
     278             : 
     279           0 : static int filter_frame(AVFilterLink *link, AVFrame *frame)
     280             : {
     281           0 :     AVFilterContext *ctx = link->dst;
     282           0 :     FloodfillContext *s = ctx->priv;
     283           0 :     const unsigned d0 = s->d0;
     284           0 :     const unsigned d1 = s->d1;
     285           0 :     const unsigned d2 = s->d2;
     286           0 :     const unsigned d3 = s->d3;
     287           0 :     int s0 = s->s0;
     288           0 :     int s1 = s->s1;
     289           0 :     int s2 = s->s2;
     290           0 :     int s3 = s->s3;
     291           0 :     const int w = frame->width;
     292           0 :     const int h = frame->height;
     293             :     int ret;
     294             : 
     295           0 :     if (ret = av_frame_make_writable(frame))
     296           0 :         return ret;
     297             : 
     298           0 :     if (is_inside(s->x, s->y, w, h)) {
     299           0 :         s->pick_pixel(frame, s->x, s->y, &s0, &s1, &s2, &s3);
     300             : 
     301           0 :         if (s->is_same(frame, s->x, s->y, s0, s1, s2, s3)) {
     302           0 :             s->points[s->front].x = s->x;
     303           0 :             s->points[s->front].y = s->y;
     304           0 :             s->front++;
     305             :         }
     306             : 
     307           0 :         while (s->front > s->back) {
     308             :             int x, y;
     309             : 
     310           0 :             s->front--;
     311           0 :             x = s->points[s->front].x;
     312           0 :             y = s->points[s->front].y;
     313             : 
     314           0 :             if (s->is_same(frame, x, y, s0, s1, s2, s3)) {
     315           0 :                 s->set_pixel(frame, x, y, d0, d1, d2, d3);
     316             : 
     317           0 :                 if (is_inside(x + 1, y, w, h)) {
     318           0 :                     s->points[s->front]  .x = x + 1;
     319           0 :                     s->points[s->front++].y = y;
     320             :                 }
     321             : 
     322           0 :                 if (is_inside(x - 1, y, w, h)) {
     323           0 :                     s->points[s->front]  .x = x - 1;
     324           0 :                     s->points[s->front++].y = y;
     325             :                 }
     326             : 
     327           0 :                 if (is_inside(x, y + 1, w, h)) {
     328           0 :                     s->points[s->front]  .x = x;
     329           0 :                     s->points[s->front++].y = y + 1;
     330             :                 }
     331             : 
     332           0 :                 if (is_inside(x, y - 1, w, h)) {
     333           0 :                     s->points[s->front]  .x = x;
     334           0 :                     s->points[s->front++].y = y - 1;
     335             :                 }
     336             :             }
     337             :         }
     338             :     }
     339             : 
     340           0 :     return ff_filter_frame(ctx->outputs[0], frame);
     341             : }
     342             : 
     343           0 : static av_cold int query_formats(AVFilterContext *ctx)
     344             : {
     345             :     static const enum AVPixelFormat pixel_fmts[] = {
     346             :         AV_PIX_FMT_GRAY8,
     347             :         AV_PIX_FMT_YUV444P,
     348             :         AV_PIX_FMT_YUVA444P,
     349             :         AV_PIX_FMT_GBRP,
     350             :         AV_PIX_FMT_GBRP9,
     351             :         AV_PIX_FMT_GBRP10,
     352             :         AV_PIX_FMT_GBRAP10,
     353             :         AV_PIX_FMT_GBRP12,
     354             :         AV_PIX_FMT_GBRAP12,
     355             :         AV_PIX_FMT_GBRP14,
     356             :         AV_PIX_FMT_GBRP16,
     357             :         AV_PIX_FMT_GBRAP16,
     358             :         AV_PIX_FMT_GBRAP,
     359             :         AV_PIX_FMT_YUV444P9,
     360             :         AV_PIX_FMT_YUVA444P9,
     361             :         AV_PIX_FMT_YUV444P10,
     362             :         AV_PIX_FMT_YUVA444P10,
     363             :         AV_PIX_FMT_YUV444P12,
     364             :         AV_PIX_FMT_YUV444P14,
     365             :         AV_PIX_FMT_GRAY16,
     366             :         AV_PIX_FMT_YUV444P16,
     367             :         AV_PIX_FMT_YUVA444P16,
     368             :         AV_PIX_FMT_NONE
     369             :     };
     370             :     AVFilterFormats *formats;
     371             : 
     372           0 :     formats = ff_make_format_list(pixel_fmts);
     373           0 :     if (!formats)
     374           0 :         return AVERROR(ENOMEM);
     375             : 
     376           0 :     return ff_set_common_formats(ctx, formats);
     377             : }
     378             : 
     379           0 : static av_cold void uninit(AVFilterContext *ctx)
     380             : {
     381           0 :     FloodfillContext *s = ctx->priv;
     382             : 
     383           0 :     av_freep(&s->points);
     384           0 : }
     385             : 
     386             : static const AVFilterPad floodfill_inputs[] = {
     387             :     {
     388             :         .name         = "default",
     389             :         .type         = AVMEDIA_TYPE_VIDEO,
     390             :         .filter_frame = filter_frame,
     391             :         .config_props = config_input,
     392             :     },
     393             :     { NULL }
     394             : };
     395             : 
     396             : static const AVFilterPad floodfill_outputs[] = {
     397             :     {
     398             :         .name = "default",
     399             :         .type = AVMEDIA_TYPE_VIDEO,
     400             :     },
     401             :     { NULL }
     402             : };
     403             : 
     404             : #define OFFSET(x) offsetof(FloodfillContext, x)
     405             : #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
     406             : 
     407             : static const AVOption floodfill_options[] = {
     408             :     { "x",  "set pixel x coordinate",             OFFSET(x),  AV_OPT_TYPE_INT, {.i64=0}, 0, UINT16_MAX, FLAGS },
     409             :     { "y",  "set pixel y coordinate",             OFFSET(y),  AV_OPT_TYPE_INT, {.i64=0}, 0, UINT16_MAX, FLAGS },
     410             :     { "s0", "set source #0 component value",      OFFSET(s0), AV_OPT_TYPE_INT, {.i64=0},-1, UINT16_MAX, FLAGS },
     411             :     { "s1", "set source #1 component value",      OFFSET(s1), AV_OPT_TYPE_INT, {.i64=0},-1, UINT16_MAX, FLAGS },
     412             :     { "s2", "set source #2 component value",      OFFSET(s2), AV_OPT_TYPE_INT, {.i64=0},-1, UINT16_MAX, FLAGS },
     413             :     { "s3", "set source #3 component value",      OFFSET(s3), AV_OPT_TYPE_INT, {.i64=0},-1, UINT16_MAX, FLAGS },
     414             :     { "d0", "set destination #0 component value", OFFSET(d0), AV_OPT_TYPE_INT, {.i64=0}, 0, UINT16_MAX, FLAGS },
     415             :     { "d1", "set destination #1 component value", OFFSET(d1), AV_OPT_TYPE_INT, {.i64=0}, 0, UINT16_MAX, FLAGS },
     416             :     { "d2", "set destination #2 component value", OFFSET(d2), AV_OPT_TYPE_INT, {.i64=0}, 0, UINT16_MAX, FLAGS },
     417             :     { "d3", "set destination #3 component value", OFFSET(d3), AV_OPT_TYPE_INT, {.i64=0}, 0, UINT16_MAX, FLAGS },
     418             :     { NULL }
     419             : };
     420             : 
     421             : AVFILTER_DEFINE_CLASS(floodfill);
     422             : 
     423             : AVFilter ff_vf_floodfill = {
     424             :     .name          = "floodfill",
     425             :     .description   = NULL_IF_CONFIG_SMALL("Fill area with same color with another color."),
     426             :     .priv_size     = sizeof(FloodfillContext),
     427             :     .priv_class    = &floodfill_class,
     428             :     .query_formats = query_formats,
     429             :     .uninit        = uninit,
     430             :     .inputs        = floodfill_inputs,
     431             :     .outputs       = floodfill_outputs,
     432             :     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
     433             : };

Generated by: LCOV version 1.13