LCOV - code coverage report
Current view: top level - libavfilter - vf_maskedmerge.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 117 0.0 %
Date: 2017-12-17 23:02:56 Functions: 0 8 0.0 %

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

Generated by: LCOV version 1.13