LCOV - code coverage report
Current view: top level - libavfilter - vf_lumakey.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 70 0.0 %
Date: 2017-12-17 16:07:53 Functions: 0 5 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 "avfilter.h"
      24             : #include "formats.h"
      25             : #include "internal.h"
      26             : #include "video.h"
      27             : 
      28             : typedef struct LumakeyContext {
      29             :     const AVClass *class;
      30             : 
      31             :     int threshold;
      32             :     int tolerance;
      33             :     int softness;
      34             : 
      35             :     int white;
      36             :     int black;
      37             :     int max;
      38             : 
      39             :     int (*do_lumakey_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
      40             : } LumakeyContext;
      41             : 
      42           0 : static int do_lumakey_slice8(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
      43             : {
      44           0 :     LumakeyContext *s = ctx->priv;
      45           0 :     AVFrame *frame = arg;
      46           0 :     const int slice_start = (frame->height * jobnr) / nb_jobs;
      47           0 :     const int slice_end = (frame->height * (jobnr + 1)) / nb_jobs;
      48           0 :     uint8_t *alpha = frame->data[3] + slice_start * frame->linesize[3];
      49           0 :     const uint8_t *luma = frame->data[0] + slice_start * frame->linesize[0];
      50           0 :     const int so = s->softness;
      51           0 :     const int w = s->white;
      52           0 :     const int b = s->black;
      53             :     int x, y;
      54             : 
      55           0 :     for (y = slice_start; y < slice_end; y++) {
      56           0 :         for (x = 0; x < frame->width; x++) {
      57           0 :             if (luma[x] >= b && luma[x] <= w) {
      58           0 :                 alpha[x] = 0;
      59           0 :             } else if (luma[x] > b - so && luma[x] < w + so) {
      60           0 :                 if (luma[x] < b) {
      61           0 :                     alpha[x] = 255 - (luma[x] - b + so) * 255 / so;
      62             :                 } else {
      63           0 :                     alpha[x] = (luma[x] - w) * 255 / so;
      64             :                 }
      65             :             }
      66             :         }
      67           0 :         luma += frame->linesize[0];
      68           0 :         alpha += frame->linesize[3];
      69             :     }
      70             : 
      71           0 :     return 0;
      72             : }
      73             : 
      74           0 : static int do_lumakey_slice16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
      75             : {
      76           0 :     LumakeyContext *s = ctx->priv;
      77           0 :     AVFrame *frame = arg;
      78           0 :     const int slice_start = (frame->height * jobnr) / nb_jobs;
      79           0 :     const int slice_end = (frame->height * (jobnr + 1)) / nb_jobs;
      80           0 :     uint16_t *alpha = (uint16_t *)(frame->data[3] + slice_start * frame->linesize[3]);
      81           0 :     const uint16_t *luma = (const uint16_t *)(frame->data[0] + slice_start * frame->linesize[0]);
      82           0 :     const int so = s->softness;
      83           0 :     const int w = s->white;
      84           0 :     const int b = s->black;
      85           0 :     const int m = s->max;
      86             :     int x, y;
      87             : 
      88           0 :     for (y = slice_start; y < slice_end; y++) {
      89           0 :         for (x = 0; x < frame->width; x++) {
      90           0 :             if (luma[x] >= b && luma[x] <= w) {
      91           0 :                 alpha[x] = 0;
      92           0 :             } else if (luma[x] > b - so && luma[x] < w + so) {
      93           0 :                 if (luma[x] < b) {
      94           0 :                     alpha[x] = m - (luma[x] - b + so) * m / so;
      95             :                 } else {
      96           0 :                     alpha[x] = (luma[x] - w) * m / so;
      97             :                 }
      98             :             }
      99             :         }
     100           0 :         luma += frame->linesize[0] / 2;
     101           0 :         alpha += frame->linesize[3] / 2;
     102             :     }
     103             : 
     104           0 :     return 0;
     105             : }
     106             : 
     107           0 : static int config_input(AVFilterLink *inlink)
     108             : {
     109           0 :     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
     110           0 :     AVFilterContext *ctx = inlink->dst;
     111           0 :     LumakeyContext *s = ctx->priv;
     112             :     int depth;
     113             : 
     114           0 :     depth = desc->comp[0].depth;
     115           0 :     if (depth == 8) {
     116           0 :         s->white = av_clip_uint8(s->threshold + s->tolerance);
     117           0 :         s->black = av_clip_uint8(s->threshold - s->tolerance);
     118           0 :         s->do_lumakey_slice = do_lumakey_slice8;
     119             :     } else {
     120           0 :         s->max = (1 << depth) - 1;
     121           0 :         s->white = av_clip(s->threshold + s->tolerance, 0, s->max);
     122           0 :         s->black = av_clip(s->threshold - s->tolerance, 0, s->max);
     123           0 :         s->do_lumakey_slice = do_lumakey_slice16;
     124             :     }
     125             : 
     126           0 :     return 0;
     127             : }
     128             : 
     129           0 : static int filter_frame(AVFilterLink *link, AVFrame *frame)
     130             : {
     131           0 :     AVFilterContext *ctx = link->dst;
     132           0 :     LumakeyContext *s = ctx->priv;
     133             :     int ret;
     134             : 
     135           0 :     if (ret = av_frame_make_writable(frame))
     136           0 :         return ret;
     137             : 
     138           0 :     if (ret = ctx->internal->execute(ctx, s->do_lumakey_slice, frame, NULL, FFMIN(frame->height, ff_filter_get_nb_threads(ctx))))
     139           0 :         return ret;
     140             : 
     141           0 :     return ff_filter_frame(ctx->outputs[0], frame);
     142             : }
     143             : 
     144           0 : static av_cold int query_formats(AVFilterContext *ctx)
     145             : {
     146             :     static const enum AVPixelFormat pixel_fmts[] = {
     147             :         AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
     148             :         AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA420P9,
     149             :         AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA420P10,
     150             :         AV_PIX_FMT_YUVA444P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA420P16,
     151             :         AV_PIX_FMT_NONE
     152             :     };
     153             :     AVFilterFormats *formats;
     154             : 
     155           0 :     formats = ff_make_format_list(pixel_fmts);
     156           0 :     if (!formats)
     157           0 :         return AVERROR(ENOMEM);
     158             : 
     159           0 :     return ff_set_common_formats(ctx, formats);
     160             : }
     161             : 
     162             : static const AVFilterPad lumakey_inputs[] = {
     163             :     {
     164             :         .name         = "default",
     165             :         .type         = AVMEDIA_TYPE_VIDEO,
     166             :         .filter_frame = filter_frame,
     167             :         .config_props = config_input,
     168             :     },
     169             :     { NULL }
     170             : };
     171             : 
     172             : static const AVFilterPad lumakey_outputs[] = {
     173             :     {
     174             :         .name          = "default",
     175             :         .type          = AVMEDIA_TYPE_VIDEO,
     176             :     },
     177             :     { NULL }
     178             : };
     179             : 
     180             : #define OFFSET(x) offsetof(LumakeyContext, x)
     181             : #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
     182             : 
     183             : static const AVOption lumakey_options[] = {
     184             :     { "threshold", "set the threshold value", OFFSET(threshold), AV_OPT_TYPE_INT, {.i64=0}, 0, UINT16_MAX, FLAGS },
     185             :     { "tolerance", "set the tolerance value", OFFSET(tolerance), AV_OPT_TYPE_INT, {.i64=1}, 0, UINT16_MAX, FLAGS },
     186             :     { "softness",  "set the softness value",  OFFSET(softness),  AV_OPT_TYPE_INT, {.i64=0}, 0, UINT16_MAX, FLAGS },
     187             :     { NULL }
     188             : };
     189             : 
     190             : AVFILTER_DEFINE_CLASS(lumakey);
     191             : 
     192             : AVFilter ff_vf_lumakey = {
     193             :     .name          = "lumakey",
     194             :     .description   = NULL_IF_CONFIG_SMALL("Turns a certain luma into transparency."),
     195             :     .priv_size     = sizeof(LumakeyContext),
     196             :     .priv_class    = &lumakey_class,
     197             :     .query_formats = query_formats,
     198             :     .inputs        = lumakey_inputs,
     199             :     .outputs       = lumakey_outputs,
     200             :     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
     201             : };

Generated by: LCOV version 1.13