LCOV - code coverage report
Current view: top level - libavfilter - vf_remap.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 173 0.0 %
Date: 2017-12-16 01:21:47 Functions: 0 10 0.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2016 Floris Sluiter
       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             :  * Pixel remap filter
      24             :  * This filter copies pixel by pixel a source frame to a target frame.
      25             :  * It remaps the pixels to a new x,y destination based on two files ymap/xmap.
      26             :  * Map files are passed as a parameter and are in PGM format (P2 or P5),
      27             :  * where the values are y(rows)/x(cols) coordinates of the source_frame.
      28             :  * The *target* frame dimension is based on mapfile dimensions: specified in the
      29             :  * header of the mapfile and reflected in the number of datavalues.
      30             :  * Dimensions of ymap and xmap must be equal. Datavalues must be positive or zero.
      31             :  * Any datavalue in the ymap or xmap which value is higher
      32             :  * then the *source* frame height or width is silently ignored, leaving a
      33             :  * blank/chromakey pixel. This can safely be used as a feature to create overlays.
      34             :  *
      35             :  * Algorithm digest:
      36             :  * Target_frame[y][x] = Source_frame[ ymap[y][x] ][ [xmap[y][x] ];
      37             :  */
      38             : 
      39             : #include "libavutil/imgutils.h"
      40             : #include "libavutil/pixdesc.h"
      41             : #include "libavutil/opt.h"
      42             : #include "avfilter.h"
      43             : #include "formats.h"
      44             : #include "framesync.h"
      45             : #include "internal.h"
      46             : #include "video.h"
      47             : 
      48             : typedef struct RemapContext {
      49             :     const AVClass *class;
      50             :     int nb_planes;
      51             :     int nb_components;
      52             :     int step;
      53             :     FFFrameSync fs;
      54             : 
      55             :     void (*remap)(struct RemapContext *s, const AVFrame *in,
      56             :                   const AVFrame *xin, const AVFrame *yin,
      57             :                   AVFrame *out);
      58             : } RemapContext;
      59             : 
      60             : #define OFFSET(x) offsetof(RemapContext, x)
      61             : #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
      62             : 
      63             : static const AVOption remap_options[] = {
      64             :     { NULL }
      65             : };
      66             : 
      67             : AVFILTER_DEFINE_CLASS(remap);
      68             : 
      69           0 : static int query_formats(AVFilterContext *ctx)
      70             : {
      71             :     static const enum AVPixelFormat pix_fmts[] = {
      72             :         AV_PIX_FMT_YUVA444P,
      73             :         AV_PIX_FMT_YUV444P,
      74             :         AV_PIX_FMT_YUVJ444P,
      75             :         AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
      76             :         AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR, AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
      77             :         AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
      78             :         AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV444P12,
      79             :         AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV444P16,
      80             :         AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P16,
      81             :         AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12,
      82             :         AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
      83             :         AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
      84             :         AV_PIX_FMT_RGB48, AV_PIX_FMT_BGR48,
      85             :         AV_PIX_FMT_RGBA64, AV_PIX_FMT_BGRA64,
      86             :         AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9,
      87             :         AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12,
      88             :         AV_PIX_FMT_GRAY16,
      89             :         AV_PIX_FMT_NONE
      90             :     };
      91             :     static const enum AVPixelFormat map_fmts[] = {
      92             :         AV_PIX_FMT_GRAY16,
      93             :         AV_PIX_FMT_NONE
      94             :     };
      95           0 :     AVFilterFormats *pix_formats = NULL, *map_formats = NULL;
      96             :     int ret;
      97             : 
      98           0 :     if (!(pix_formats = ff_make_format_list(pix_fmts)) ||
      99           0 :         !(map_formats = ff_make_format_list(map_fmts))) {
     100           0 :         ret = AVERROR(ENOMEM);
     101           0 :         goto fail;
     102             :     }
     103           0 :     if ((ret = ff_formats_ref(pix_formats, &ctx->inputs[0]->out_formats)) < 0 ||
     104           0 :         (ret = ff_formats_ref(map_formats, &ctx->inputs[1]->out_formats)) < 0 ||
     105           0 :         (ret = ff_formats_ref(map_formats, &ctx->inputs[2]->out_formats)) < 0 ||
     106           0 :         (ret = ff_formats_ref(pix_formats, &ctx->outputs[0]->in_formats)) < 0)
     107             :         goto fail;
     108           0 :     return 0;
     109           0 : fail:
     110           0 :     if (pix_formats)
     111           0 :         av_freep(&pix_formats->formats);
     112           0 :     av_freep(&pix_formats);
     113           0 :     if (map_formats)
     114           0 :         av_freep(&map_formats->formats);
     115           0 :     av_freep(&map_formats);
     116           0 :     return ret;
     117             : }
     118             : 
     119             : /**
     120             :  * remap_planar algorithm expects planes of same size
     121             :  * pixels are copied from source to target using :
     122             :  * Target_frame[y][x] = Source_frame[ ymap[y][x] ][ [xmap[y][x] ];
     123             :  */
     124           0 : static void remap_planar(RemapContext *s, const AVFrame *in,
     125             :                          const AVFrame *xin, const AVFrame *yin,
     126             :                          AVFrame *out)
     127             : {
     128           0 :     const int xlinesize = xin->linesize[0] / 2;
     129           0 :     const int ylinesize = yin->linesize[0] / 2;
     130             :     int x , y, plane;
     131             : 
     132           0 :     for (plane = 0; plane < s->nb_planes ; plane++) {
     133           0 :         uint8_t *dst         = out->data[plane];
     134           0 :         const int dlinesize  = out->linesize[plane];
     135           0 :         const uint8_t *src   = in->data[plane];
     136           0 :         const int slinesize  = in->linesize[plane];
     137           0 :         const uint16_t *xmap = (const uint16_t *)xin->data[0];
     138           0 :         const uint16_t *ymap = (const uint16_t *)yin->data[0];
     139             : 
     140           0 :         for (y = 0; y < out->height; y++) {
     141           0 :             for (x = 0; x < out->width; x++) {
     142           0 :                 if (ymap[x] < in->height && xmap[x] < in->width) {
     143           0 :                     dst[x] = src[ymap[x] * slinesize + xmap[x]];
     144             :                 } else {
     145           0 :                     dst[x] = 0;
     146             :                 }
     147             :             }
     148           0 :             dst  += dlinesize;
     149           0 :             xmap += xlinesize;
     150           0 :             ymap += ylinesize;
     151             :         }
     152             :     }
     153           0 : }
     154             : 
     155           0 : static void remap_planar16(RemapContext *s, const AVFrame *in,
     156             :                            const AVFrame *xin, const AVFrame *yin,
     157             :                            AVFrame *out)
     158             : {
     159           0 :     const int xlinesize = xin->linesize[0] / 2;
     160           0 :     const int ylinesize = yin->linesize[0] / 2;
     161             :     int x , y, plane;
     162             : 
     163           0 :     for (plane = 0; plane < s->nb_planes ; plane++) {
     164           0 :         uint16_t *dst        = (uint16_t *)out->data[plane];
     165           0 :         const int dlinesize  = out->linesize[plane] / 2;
     166           0 :         const uint16_t *src  = (const uint16_t *)in->data[plane];
     167           0 :         const int slinesize  = in->linesize[plane] / 2;
     168           0 :         const uint16_t *xmap = (const uint16_t *)xin->data[0];
     169           0 :         const uint16_t *ymap = (const uint16_t *)yin->data[0];
     170             : 
     171           0 :         for (y = 0; y < out->height; y++) {
     172           0 :             for (x = 0; x < out->width; x++) {
     173           0 :                 if (ymap[x] < in->height && xmap[x] < in->width) {
     174           0 :                     dst[x] = src[ymap[x] * slinesize + xmap[x]];
     175             :                 } else {
     176           0 :                     dst[x] = 0;
     177             :                 }
     178             :             }
     179           0 :             dst  += dlinesize;
     180           0 :             xmap += xlinesize;
     181           0 :             ymap += ylinesize;
     182             :         }
     183             :     }
     184           0 : }
     185             : 
     186             : /**
     187             :  * remap_packed algorithm expects pixels with both padded bits (step) and
     188             :  * number of components correctly set.
     189             :  * pixels are copied from source to target using :
     190             :  * Target_frame[y][x] = Source_frame[ ymap[y][x] ][ [xmap[y][x] ];
     191             :  */
     192           0 : static void remap_packed(RemapContext *s, const AVFrame *in,
     193             :                          const AVFrame *xin, const AVFrame *yin,
     194             :                          AVFrame *out)
     195             : {
     196           0 :     uint8_t *dst = out->data[0];
     197           0 :     const uint8_t *src  = in->data[0];
     198           0 :     const int dlinesize = out->linesize[0];
     199           0 :     const int slinesize = in->linesize[0];
     200           0 :     const int xlinesize = xin->linesize[0] / 2;
     201           0 :     const int ylinesize = yin->linesize[0] / 2;
     202           0 :     const uint16_t *xmap = (const uint16_t *)xin->data[0];
     203           0 :     const uint16_t *ymap = (const uint16_t *)yin->data[0];
     204           0 :     const int step = s->step;
     205             :     int c, x, y;
     206             : 
     207           0 :     for (y = 0; y < out->height; y++) {
     208           0 :         for (x = 0; x < out->width; x++) {
     209           0 :             for (c = 0; c < s->nb_components; c++) {
     210           0 :                 if (ymap[x] < in->height && xmap[x] < in->width) {
     211           0 :                     dst[x * step + c] = src[ymap[x] * slinesize + xmap[x] * step + c];
     212             :                 } else {
     213           0 :                     dst[x * step + c] = 0;
     214             :                 }
     215             :             }
     216             :         }
     217           0 :         dst  += dlinesize;
     218           0 :         xmap += xlinesize;
     219           0 :         ymap += ylinesize;
     220             :     }
     221           0 : }
     222             : 
     223           0 : static void remap_packed16(RemapContext *s, const AVFrame *in,
     224             :                            const AVFrame *xin, const AVFrame *yin,
     225             :                            AVFrame *out)
     226             : {
     227           0 :     uint16_t *dst = (uint16_t *)out->data[0];
     228           0 :     const uint16_t *src  = (const uint16_t *)in->data[0];
     229           0 :     const int dlinesize = out->linesize[0] / 2;
     230           0 :     const int slinesize = in->linesize[0] / 2;
     231           0 :     const int xlinesize = xin->linesize[0] / 2;
     232           0 :     const int ylinesize = yin->linesize[0] / 2;
     233           0 :     const uint16_t *xmap = (const uint16_t *)xin->data[0];
     234           0 :     const uint16_t *ymap = (const uint16_t *)yin->data[0];
     235           0 :     const int step = s->step / 2;
     236             :     int c, x, y;
     237             : 
     238           0 :     for (y = 0; y < out->height; y++) {
     239           0 :         for (x = 0; x < out->width; x++) {
     240           0 :             for (c = 0; c < s->nb_components; c++) {
     241           0 :                 if (ymap[x] < in->height && xmap[x] < in->width) {
     242           0 :                     dst[x * step + c] = src[ymap[x] * slinesize + xmap[x] * step + c];
     243             :                 } else {
     244           0 :                     dst[x * step + c] = 0;
     245             :                 }
     246             :             }
     247             :         }
     248           0 :         dst  += dlinesize;
     249           0 :         xmap += xlinesize;
     250           0 :         ymap += ylinesize;
     251             :     }
     252           0 : }
     253             : 
     254           0 : static int config_input(AVFilterLink *inlink)
     255             : {
     256           0 :     AVFilterContext *ctx = inlink->dst;
     257           0 :     RemapContext *s = ctx->priv;
     258           0 :     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
     259             : 
     260           0 :     s->nb_planes = av_pix_fmt_count_planes(inlink->format);
     261           0 :     s->nb_components = desc->nb_components;
     262             : 
     263           0 :     if (desc->comp[0].depth == 8) {
     264           0 :         if (s->nb_planes > 1 || s->nb_components == 1) {
     265           0 :             s->remap = remap_planar;
     266             :         } else {
     267           0 :             s->remap = remap_packed;
     268             :         }
     269             :     } else {
     270           0 :         if (s->nb_planes > 1 || s->nb_components == 1) {
     271           0 :             s->remap = remap_planar16;
     272             :         } else {
     273           0 :             s->remap = remap_packed16;
     274             :         }
     275             :     }
     276             : 
     277           0 :     s->step = av_get_padded_bits_per_pixel(desc) >> 3;
     278           0 :     return 0;
     279             : }
     280             : 
     281           0 : static int process_frame(FFFrameSync *fs)
     282             : {
     283           0 :     AVFilterContext *ctx = fs->parent;
     284           0 :     RemapContext *s = fs->opaque;
     285           0 :     AVFilterLink *outlink = ctx->outputs[0];
     286             :     AVFrame *out, *in, *xpic, *ypic;
     287             :     int ret;
     288             : 
     289           0 :     if ((ret = ff_framesync_get_frame(&s->fs, 0, &in,   0)) < 0 ||
     290           0 :         (ret = ff_framesync_get_frame(&s->fs, 1, &xpic, 0)) < 0 ||
     291           0 :         (ret = ff_framesync_get_frame(&s->fs, 2, &ypic, 0)) < 0)
     292           0 :         return ret;
     293             : 
     294           0 :     if (ctx->is_disabled) {
     295           0 :         out = av_frame_clone(in);
     296           0 :         if (!out)
     297           0 :             return AVERROR(ENOMEM);
     298             :     } else {
     299           0 :         out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
     300           0 :         if (!out)
     301           0 :             return AVERROR(ENOMEM);
     302           0 :         av_frame_copy_props(out, in);
     303             : 
     304           0 :         s->remap(s, in, xpic, ypic, out);
     305             :     }
     306           0 :     out->pts = av_rescale_q(in->pts, s->fs.time_base, outlink->time_base);
     307             : 
     308           0 :     return ff_filter_frame(outlink, out);
     309             : }
     310             : 
     311           0 : static int config_output(AVFilterLink *outlink)
     312             : {
     313           0 :     AVFilterContext *ctx = outlink->src;
     314           0 :     RemapContext *s = ctx->priv;
     315           0 :     AVFilterLink *srclink = ctx->inputs[0];
     316           0 :     AVFilterLink *xlink = ctx->inputs[1];
     317           0 :     AVFilterLink *ylink = ctx->inputs[2];
     318             :     FFFrameSyncIn *in;
     319             :     int ret;
     320             : 
     321           0 :     if (xlink->w != ylink->w || xlink->h != ylink->h) {
     322           0 :         av_log(ctx, AV_LOG_ERROR, "Second input link %s parameters "
     323             :                "(size %dx%d) do not match the corresponding "
     324             :                "third input link %s parameters (%dx%d)\n",
     325           0 :                ctx->input_pads[1].name, xlink->w, xlink->h,
     326           0 :                ctx->input_pads[2].name, ylink->w, ylink->h);
     327           0 :         return AVERROR(EINVAL);
     328             :     }
     329             : 
     330           0 :     outlink->w = xlink->w;
     331           0 :     outlink->h = xlink->h;
     332           0 :     outlink->time_base = srclink->time_base;
     333           0 :     outlink->sample_aspect_ratio = srclink->sample_aspect_ratio;
     334           0 :     outlink->frame_rate = srclink->frame_rate;
     335             : 
     336           0 :     ret = ff_framesync_init(&s->fs, ctx, 3);
     337           0 :     if (ret < 0)
     338           0 :         return ret;
     339             : 
     340           0 :     in = s->fs.in;
     341           0 :     in[0].time_base = srclink->time_base;
     342           0 :     in[1].time_base = xlink->time_base;
     343           0 :     in[2].time_base = ylink->time_base;
     344           0 :     in[0].sync   = 2;
     345           0 :     in[0].before = EXT_STOP;
     346           0 :     in[0].after  = EXT_STOP;
     347           0 :     in[1].sync   = 1;
     348           0 :     in[1].before = EXT_NULL;
     349           0 :     in[1].after  = EXT_INFINITY;
     350           0 :     in[2].sync   = 1;
     351           0 :     in[2].before = EXT_NULL;
     352           0 :     in[2].after  = EXT_INFINITY;
     353           0 :     s->fs.opaque   = s;
     354           0 :     s->fs.on_event = process_frame;
     355             : 
     356           0 :     return ff_framesync_configure(&s->fs);
     357             : }
     358             : 
     359           0 : static int activate(AVFilterContext *ctx)
     360             : {
     361           0 :     RemapContext *s = ctx->priv;
     362           0 :     return ff_framesync_activate(&s->fs);
     363             : }
     364             : 
     365             : 
     366           0 : static av_cold void uninit(AVFilterContext *ctx)
     367             : {
     368           0 :     RemapContext *s = ctx->priv;
     369             : 
     370           0 :     ff_framesync_uninit(&s->fs);
     371           0 : }
     372             : 
     373             : static const AVFilterPad remap_inputs[] = {
     374             :     {
     375             :         .name         = "source",
     376             :         .type         = AVMEDIA_TYPE_VIDEO,
     377             :         .config_props = config_input,
     378             :     },
     379             :     {
     380             :         .name         = "xmap",
     381             :         .type         = AVMEDIA_TYPE_VIDEO,
     382             :     },
     383             :     {
     384             :         .name         = "ymap",
     385             :         .type         = AVMEDIA_TYPE_VIDEO,
     386             :     },
     387             :     { NULL }
     388             : };
     389             : 
     390             : static const AVFilterPad remap_outputs[] = {
     391             :     {
     392             :         .name          = "default",
     393             :         .type          = AVMEDIA_TYPE_VIDEO,
     394             :         .config_props  = config_output,
     395             :     },
     396             :     { NULL }
     397             : };
     398             : 
     399             : AVFilter ff_vf_remap = {
     400             :     .name          = "remap",
     401             :     .description   = NULL_IF_CONFIG_SMALL("Remap pixels."),
     402             :     .priv_size     = sizeof(RemapContext),
     403             :     .uninit        = uninit,
     404             :     .query_formats = query_formats,
     405             :     .activate      = activate,
     406             :     .inputs        = remap_inputs,
     407             :     .outputs       = remap_outputs,
     408             :     .priv_class    = &remap_class,
     409             :     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
     410             : };

Generated by: LCOV version 1.13