LCOV - code coverage report
Current view: top level - libavfilter - vf_threshold.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 33 137 24.1 %
Date: 2017-12-16 01:21:47 Functions: 3 9 33.3 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2016 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             : /**
      22             :  * @file
      23             :  * threshold video filter
      24             :  */
      25             : 
      26             : #include "libavutil/imgutils.h"
      27             : #include "libavutil/internal.h"
      28             : #include "libavutil/opt.h"
      29             : #include "libavutil/pixdesc.h"
      30             : #include "avfilter.h"
      31             : #include "framesync.h"
      32             : #include "internal.h"
      33             : #include "video.h"
      34             : #include "threshold.h"
      35             : 
      36             : #define OFFSET(x) offsetof(ThresholdContext, x)
      37             : #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
      38             : 
      39             : static const AVOption threshold_options[] = {
      40             :     { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT,  {.i64=15}, 0, 15, FLAGS},
      41             :     { NULL }
      42             : };
      43             : 
      44             : AVFILTER_DEFINE_CLASS(threshold);
      45             : 
      46           0 : static int query_formats(AVFilterContext *ctx)
      47             : {
      48             :     static const enum AVPixelFormat pix_fmts[] = {
      49             :         AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
      50             :         AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
      51             :         AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
      52             :         AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
      53             :         AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
      54             :         AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
      55             :         AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
      56             :         AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
      57             :         AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
      58             :         AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
      59             :         AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
      60             :         AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
      61             :         AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
      62             :         AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12 , AV_PIX_FMT_GBRAP16,
      63             :         AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10,
      64             :         AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY16,
      65             :         AV_PIX_FMT_NONE
      66             :     };
      67             : 
      68           0 :     return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
      69             : }
      70             : 
      71           0 : static int process_frame(FFFrameSync *fs)
      72             : {
      73           0 :     AVFilterContext *ctx = fs->parent;
      74           0 :     ThresholdContext *s = fs->opaque;
      75           0 :     AVFilterLink *outlink = ctx->outputs[0];
      76             :     AVFrame *out, *in, *threshold, *min, *max;
      77             :     int ret;
      78             : 
      79           0 :     if ((ret = ff_framesync_get_frame(&s->fs, 0, &in,        0)) < 0 ||
      80           0 :         (ret = ff_framesync_get_frame(&s->fs, 1, &threshold, 0)) < 0 ||
      81           0 :         (ret = ff_framesync_get_frame(&s->fs, 2, &min,       0)) < 0 ||
      82           0 :         (ret = ff_framesync_get_frame(&s->fs, 3, &max,       0)) < 0)
      83           0 :         return ret;
      84             : 
      85           0 :     if (ctx->is_disabled) {
      86           0 :         out = av_frame_clone(in);
      87           0 :         if (!out)
      88           0 :             return AVERROR(ENOMEM);
      89             :     } else {
      90             :         int p;
      91             : 
      92           0 :         out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
      93           0 :         if (!out)
      94           0 :             return AVERROR(ENOMEM);
      95           0 :         av_frame_copy_props(out, in);
      96             : 
      97           0 :         for (p = 0; p < s->nb_planes; p++) {
      98           0 :             if (!(s->planes & (1 << p))) {
      99           0 :                 av_image_copy_plane(out->data[p], out->linesize[p],
     100           0 :                                     in->data[p], in->linesize[p],
     101           0 :                                     s->width[p] * s->bpc,
     102             :                                     s->height[p]);
     103           0 :                 continue;
     104             :             }
     105           0 :             s->threshold(in->data[p], threshold->data[p],
     106           0 :                          min->data[p], max->data[p],
     107             :                          out->data[p],
     108           0 :                          in->linesize[p], threshold->linesize[p],
     109           0 :                          min->linesize[p], max->linesize[p],
     110           0 :                          out->linesize[p],
     111             :                          s->width[p], s->height[p]);
     112             :         }
     113             :     }
     114             : 
     115           0 :     out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
     116             : 
     117           0 :     return ff_filter_frame(outlink, out);
     118             : }
     119             : 
     120           3 : static void threshold8(const uint8_t *in, const uint8_t *threshold,
     121             :                        const uint8_t *min, const uint8_t *max,
     122             :                        uint8_t *out,
     123             :                        ptrdiff_t ilinesize, ptrdiff_t tlinesize,
     124             :                        ptrdiff_t flinesize, ptrdiff_t slinesize,
     125             :                        ptrdiff_t olinesize,
     126             :                        int w, int h)
     127             : {
     128             :     int x, y;
     129             : 
     130           6 :     for (y = 0; y < h; y++) {
     131         771 :         for (x = 0; x < w; x++) {
     132         768 :             out[x] = in[x] < threshold[x] ? min[x] : max[x];
     133             :         }
     134             : 
     135           3 :         in        += ilinesize;
     136           3 :         threshold += tlinesize;
     137           3 :         min       += flinesize;
     138           3 :         max       += slinesize;
     139           3 :         out       += olinesize;
     140             :     }
     141           3 : }
     142             : 
     143           3 : static void threshold16(const uint8_t *iin, const uint8_t *tthreshold,
     144             :                         const uint8_t *ffirst, const uint8_t *ssecond,
     145             :                         uint8_t *oout,
     146             :                         ptrdiff_t ilinesize, ptrdiff_t tlinesize,
     147             :                         ptrdiff_t flinesize, ptrdiff_t slinesize,
     148             :                         ptrdiff_t olinesize,
     149             :                         int w, int h)
     150             : {
     151           3 :     const uint16_t *in = (const uint16_t *)iin;
     152           3 :     const uint16_t *threshold = (const uint16_t *)tthreshold;
     153           3 :     const uint16_t *min = (const uint16_t *)ffirst;
     154           3 :     const uint16_t *max = (const uint16_t *)ssecond;
     155           3 :     uint16_t *out = (uint16_t *)oout;
     156             :     int x, y;
     157             : 
     158           6 :     for (y = 0; y < h; y++) {
     159         387 :         for (x = 0; x < w; x++) {
     160         384 :             out[x] = in[x] < threshold[x] ? min[x] : max[x];
     161             :         }
     162             : 
     163           3 :         in        += ilinesize / 2;
     164           3 :         threshold += tlinesize / 2;
     165           3 :         min       += flinesize / 2;
     166           3 :         max       += slinesize / 2;
     167           3 :         out       += olinesize / 2;
     168             :     }
     169           3 : }
     170             : 
     171           0 : static int config_input(AVFilterLink *inlink)
     172             : {
     173           0 :     AVFilterContext *ctx = inlink->dst;
     174           0 :     ThresholdContext *s = ctx->priv;
     175           0 :     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
     176             :     int vsub, hsub;
     177             : 
     178           0 :     s->nb_planes = av_pix_fmt_count_planes(inlink->format);
     179             : 
     180           0 :     hsub = desc->log2_chroma_w;
     181           0 :     vsub = desc->log2_chroma_h;
     182           0 :     s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
     183           0 :     s->height[0] = s->height[3] = inlink->h;
     184           0 :     s->width[1]  = s->width[2]  = AV_CEIL_RSHIFT(inlink->w, hsub);
     185           0 :     s->width[0]  = s->width[3]  = inlink->w;
     186           0 :     s->depth = desc->comp[0].depth;
     187             : 
     188           0 :     ff_threshold_init(s);
     189             : 
     190           0 :     return 0;
     191             : }
     192             : 
     193          26 : void ff_threshold_init(ThresholdContext *s)
     194             : {
     195          26 :     if (s->depth == 8) {
     196          13 :         s->threshold = threshold8;
     197          13 :         s->bpc = 1;
     198             :     } else {
     199          13 :         s->threshold = threshold16;
     200          13 :         s->bpc = 2;
     201             :     }
     202             : 
     203             :     if (ARCH_X86)
     204          26 :         ff_threshold_init_x86(s);
     205          26 : }
     206             : 
     207           0 : static int config_output(AVFilterLink *outlink)
     208             : {
     209           0 :     AVFilterContext *ctx = outlink->src;
     210           0 :     ThresholdContext *s = ctx->priv;
     211           0 :     AVFilterLink *base = ctx->inputs[0];
     212           0 :     AVFilterLink *threshold = ctx->inputs[1];
     213           0 :     AVFilterLink *min = ctx->inputs[2];
     214           0 :     AVFilterLink *max = ctx->inputs[3];
     215             :     FFFrameSyncIn *in;
     216             :     int ret;
     217             : 
     218           0 :     if (base->format != threshold->format ||
     219           0 :         base->format != min->format ||
     220           0 :         base->format != max->format) {
     221           0 :         av_log(ctx, AV_LOG_ERROR, "inputs must be of same pixel format\n");
     222           0 :         return AVERROR(EINVAL);
     223             :     }
     224           0 :     if (base->w                       != threshold->w ||
     225           0 :         base->h                       != threshold->h ||
     226           0 :         base->w                       != min->w ||
     227           0 :         base->h                       != min->h ||
     228           0 :         base->w                       != max->w ||
     229           0 :         base->h                       != max->h) {
     230           0 :         av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
     231             :                "(size %dx%d) do not match the corresponding "
     232             :                "second input link %s parameters (%dx%d) "
     233             :                "and/or third input link %s parameters (%dx%d) "
     234             :                "and/or fourth input link %s parameters (%dx%d)\n",
     235           0 :                ctx->input_pads[0].name, base->w, base->h,
     236           0 :                ctx->input_pads[1].name, threshold->w, threshold->h,
     237           0 :                ctx->input_pads[2].name, min->w, min->h,
     238           0 :                ctx->input_pads[3].name, max->w, max->h);
     239           0 :         return AVERROR(EINVAL);
     240             :     }
     241             : 
     242           0 :     outlink->w = base->w;
     243           0 :     outlink->h = base->h;
     244           0 :     outlink->time_base = base->time_base;
     245           0 :     outlink->sample_aspect_ratio = base->sample_aspect_ratio;
     246           0 :     outlink->frame_rate = base->frame_rate;
     247             : 
     248           0 :     if ((ret = ff_framesync_init(&s->fs, ctx, 4)) < 0)
     249           0 :         return ret;
     250             : 
     251           0 :     in = s->fs.in;
     252           0 :     in[0].time_base = base->time_base;
     253           0 :     in[1].time_base = threshold->time_base;
     254           0 :     in[2].time_base = min->time_base;
     255           0 :     in[3].time_base = max->time_base;
     256           0 :     in[0].sync   = 1;
     257           0 :     in[0].before = EXT_STOP;
     258           0 :     in[0].after  = EXT_STOP;
     259           0 :     in[1].sync   = 1;
     260           0 :     in[1].before = EXT_STOP;
     261           0 :     in[1].after  = EXT_STOP;
     262           0 :     in[2].sync   = 1;
     263           0 :     in[2].before = EXT_STOP;
     264           0 :     in[2].after  = EXT_STOP;
     265           0 :     in[3].sync   = 1;
     266           0 :     in[3].before = EXT_STOP;
     267           0 :     in[3].after  = EXT_STOP;
     268           0 :     s->fs.opaque   = s;
     269           0 :     s->fs.on_event = process_frame;
     270             : 
     271           0 :     return ff_framesync_configure(&s->fs);
     272             : }
     273             : 
     274           0 : static int activate(AVFilterContext *ctx)
     275             : {
     276           0 :     ThresholdContext *s = ctx->priv;
     277           0 :     return ff_framesync_activate(&s->fs);
     278             : }
     279             : 
     280           0 : static av_cold void uninit(AVFilterContext *ctx)
     281             : {
     282           0 :     ThresholdContext *s = ctx->priv;
     283             : 
     284           0 :     ff_framesync_uninit(&s->fs);
     285           0 : }
     286             : 
     287             : static const AVFilterPad inputs[] = {
     288             :     {
     289             :         .name         = "default",
     290             :         .type         = AVMEDIA_TYPE_VIDEO,
     291             :         .config_props = config_input,
     292             :     },
     293             :     {
     294             :         .name         = "threshold",
     295             :         .type         = AVMEDIA_TYPE_VIDEO,
     296             :     },
     297             :     {
     298             :         .name         = "min",
     299             :         .type         = AVMEDIA_TYPE_VIDEO,
     300             :     },
     301             :     {
     302             :         .name         = "max",
     303             :         .type         = AVMEDIA_TYPE_VIDEO,
     304             :     },
     305             :     { NULL }
     306             : };
     307             : 
     308             : static const AVFilterPad outputs[] = {
     309             :     {
     310             :         .name          = "default",
     311             :         .type          = AVMEDIA_TYPE_VIDEO,
     312             :         .config_props  = config_output,
     313             :     },
     314             :     { NULL }
     315             : };
     316             : 
     317             : AVFilter ff_vf_threshold = {
     318             :     .name          = "threshold",
     319             :     .description   = NULL_IF_CONFIG_SMALL("Threshold first video stream using other video streams."),
     320             :     .priv_size     = sizeof(ThresholdContext),
     321             :     .priv_class    = &threshold_class,
     322             :     .uninit        = uninit,
     323             :     .query_formats = query_formats,
     324             :     .activate      = activate,
     325             :     .inputs        = inputs,
     326             :     .outputs       = outputs,
     327             :     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
     328             : };

Generated by: LCOV version 1.13