LCOV - code coverage report
Current view: top level - libavfilter - vf_mergeplanes.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 113 141 80.1 %
Date: 2017-12-10 21:22:29 Functions: 6 6 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2013 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/avassert.h"
      22             : #include "libavutil/avstring.h"
      23             : #include "libavutil/imgutils.h"
      24             : #include "libavutil/opt.h"
      25             : #include "libavutil/pixdesc.h"
      26             : #include "avfilter.h"
      27             : #include "internal.h"
      28             : #include "framesync.h"
      29             : 
      30             : typedef struct InputParam {
      31             :     int depth[4];
      32             :     int nb_planes;
      33             :     int planewidth[4];
      34             :     int planeheight[4];
      35             : } InputParam;
      36             : 
      37             : typedef struct MergePlanesContext {
      38             :     const AVClass *class;
      39             :     int64_t mapping;
      40             :     const enum AVPixelFormat out_fmt;
      41             :     int nb_inputs;
      42             :     int nb_planes;
      43             :     int planewidth[4];
      44             :     int planeheight[4];
      45             :     int map[4][2];
      46             :     const AVPixFmtDescriptor *outdesc;
      47             : 
      48             :     FFFrameSync fs;
      49             : } MergePlanesContext;
      50             : 
      51             : #define OFFSET(x) offsetof(MergePlanesContext, x)
      52             : #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
      53             : static const AVOption mergeplanes_options[] = {
      54             :     { "mapping", "set input to output plane mapping", OFFSET(mapping), AV_OPT_TYPE_INT, {.i64=0}, 0, 0x33333333, FLAGS },
      55             :     { "format", "set output pixel format", OFFSET(out_fmt), AV_OPT_TYPE_PIXEL_FMT, {.i64=AV_PIX_FMT_YUVA444P}, 0, INT_MAX, .flags=FLAGS },
      56             :     { NULL }
      57             : };
      58             : 
      59             : AVFILTER_DEFINE_CLASS(mergeplanes);
      60             : 
      61           2 : static av_cold int init(AVFilterContext *ctx)
      62             : {
      63           2 :     MergePlanesContext *s = ctx->priv;
      64           2 :     int64_t m = s->mapping;
      65             :     int i, ret;
      66             : 
      67           2 :     s->outdesc = av_pix_fmt_desc_get(s->out_fmt);
      68           4 :     if (!(s->outdesc->flags & AV_PIX_FMT_FLAG_PLANAR) ||
      69           2 :         s->outdesc->nb_components < 2) {
      70           0 :         av_log(ctx, AV_LOG_ERROR, "Only planar formats with more than one component are supported.\n");
      71           0 :         return AVERROR(EINVAL);
      72             :     }
      73           2 :     s->nb_planes = av_pix_fmt_count_planes(s->out_fmt);
      74             : 
      75           8 :     for (i = s->nb_planes - 1; i >= 0; i--) {
      76           6 :         s->map[i][0] = m & 0xf;
      77           6 :         m >>= 4;
      78           6 :         s->map[i][1] = m & 0xf;
      79           6 :         m >>= 4;
      80             : 
      81           6 :         if (s->map[i][0] > 3 || s->map[i][1] > 3) {
      82           0 :             av_log(ctx, AV_LOG_ERROR, "Mapping with out of range input and/or plane number.\n");
      83           0 :             return AVERROR(EINVAL);
      84             :         }
      85             : 
      86           6 :         s->nb_inputs = FFMAX(s->nb_inputs, s->map[i][1] + 1);
      87             :     }
      88             : 
      89           2 :     av_assert0(s->nb_inputs && s->nb_inputs <= 4);
      90             : 
      91           8 :     for (i = 0; i < s->nb_inputs; i++) {
      92           2 :         AVFilterPad pad = { 0 };
      93             : 
      94           2 :         pad.type = AVMEDIA_TYPE_VIDEO;
      95           2 :         pad.name = av_asprintf("in%d", i);
      96           2 :         if (!pad.name)
      97           0 :             return AVERROR(ENOMEM);
      98             : 
      99           2 :         if ((ret = ff_insert_inpad(ctx, i, &pad)) < 0){
     100           0 :             av_freep(&pad.name);
     101           0 :             return ret;
     102             :         }
     103             :     }
     104             : 
     105           2 :     return 0;
     106             : }
     107             : 
     108           1 : static int query_formats(AVFilterContext *ctx)
     109             : {
     110           1 :     MergePlanesContext *s = ctx->priv;
     111           1 :     AVFilterFormats *formats = NULL;
     112             :     int i, ret;
     113             : 
     114           1 :     s->outdesc = av_pix_fmt_desc_get(s->out_fmt);
     115         184 :     for (i = 0; av_pix_fmt_desc_get(i); i++) {
     116         183 :         const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(i);
     117         219 :         if (desc->comp[0].depth == s->outdesc->comp[0].depth &&
     118          72 :             (desc->comp[0].depth <= 8 || (desc->flags & AV_PIX_FMT_FLAG_BE) == (s->outdesc->flags & AV_PIX_FMT_FLAG_BE)) &&
     119          54 :             av_pix_fmt_count_planes(i) == desc->nb_components &&
     120          18 :             (ret = ff_add_format(&formats, i)) < 0)
     121           0 :                 return ret;
     122             :     }
     123             : 
     124           2 :     for (i = 0; i < s->nb_inputs; i++)
     125           1 :         if ((ret = ff_formats_ref(formats, &ctx->inputs[i]->out_formats)) < 0)
     126           0 :             return ret;
     127             : 
     128           1 :     formats = NULL;
     129           2 :     if ((ret = ff_add_format(&formats, s->out_fmt)) < 0 ||
     130           1 :         (ret = ff_formats_ref(formats, &ctx->outputs[0]->in_formats)) < 0)
     131           0 :         return ret;
     132             : 
     133           1 :     return 0;
     134             : }
     135             : 
     136          50 : static int process_frame(FFFrameSync *fs)
     137             : {
     138          50 :     AVFilterContext *ctx = fs->parent;
     139          50 :     AVFilterLink *outlink = ctx->outputs[0];
     140          50 :     MergePlanesContext *s = fs->opaque;
     141          50 :     AVFrame *in[4] = { NULL };
     142             :     AVFrame *out;
     143             :     int i, ret;
     144             : 
     145         100 :     for (i = 0; i < s->nb_inputs; i++) {
     146          50 :         if ((ret = ff_framesync_get_frame(&s->fs, i, &in[i], 0)) < 0)
     147           0 :             return ret;
     148             :     }
     149             : 
     150          50 :     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
     151          50 :     if (!out)
     152           0 :         return AVERROR(ENOMEM);
     153          50 :     out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
     154             : 
     155         200 :     for (i = 0; i < s->nb_planes; i++) {
     156         150 :         const int input = s->map[i][1];
     157         150 :         const int plane = s->map[i][0];
     158             : 
     159         450 :         av_image_copy_plane(out->data[i], out->linesize[i],
     160         300 :                             in[input]->data[plane], in[input]->linesize[plane],
     161             :                             s->planewidth[i], s->planeheight[i]);
     162             :     }
     163             : 
     164          50 :     return ff_filter_frame(outlink, out);
     165             : }
     166             : 
     167           1 : static int config_output(AVFilterLink *outlink)
     168             : {
     169           1 :     AVFilterContext *ctx = outlink->src;
     170           1 :     MergePlanesContext *s = ctx->priv;
     171             :     InputParam inputsp[4];
     172             :     FFFrameSyncIn *in;
     173             :     int i, ret;
     174             : 
     175           1 :     if ((ret = ff_framesync_init(&s->fs, ctx, s->nb_inputs)) < 0)
     176           0 :         return ret;
     177             : 
     178           1 :     in = s->fs.in;
     179           1 :     s->fs.opaque = s;
     180           1 :     s->fs.on_event = process_frame;
     181             : 
     182           1 :     outlink->w = ctx->inputs[0]->w;
     183           1 :     outlink->h = ctx->inputs[0]->h;
     184           1 :     outlink->time_base = ctx->inputs[0]->time_base;
     185           1 :     outlink->frame_rate = ctx->inputs[0]->frame_rate;
     186           1 :     outlink->sample_aspect_ratio = ctx->inputs[0]->sample_aspect_ratio;
     187             : 
     188           1 :     s->planewidth[1]  =
     189           1 :     s->planewidth[2]  = AV_CEIL_RSHIFT(((s->outdesc->comp[1].depth > 8) + 1) * outlink->w, s->outdesc->log2_chroma_w);
     190           1 :     s->planewidth[0]  =
     191           1 :     s->planewidth[3]  = ((s->outdesc->comp[0].depth > 8) + 1) * outlink->w;
     192           1 :     s->planeheight[1] =
     193           1 :     s->planeheight[2] = AV_CEIL_RSHIFT(outlink->h, s->outdesc->log2_chroma_h);
     194           1 :     s->planeheight[0] =
     195           1 :     s->planeheight[3] = outlink->h;
     196             : 
     197           2 :     for (i = 0; i < s->nb_inputs; i++) {
     198           1 :         InputParam *inputp = &inputsp[i];
     199           1 :         AVFilterLink *inlink = ctx->inputs[i];
     200           1 :         const AVPixFmtDescriptor *indesc = av_pix_fmt_desc_get(inlink->format);
     201             :         int j;
     202             : 
     203           2 :         if (outlink->sample_aspect_ratio.num != inlink->sample_aspect_ratio.num ||
     204           1 :             outlink->sample_aspect_ratio.den != inlink->sample_aspect_ratio.den) {
     205           0 :             av_log(ctx, AV_LOG_ERROR, "input #%d link %s SAR %d:%d "
     206             :                                       "does not match output link %s SAR %d:%d\n",
     207           0 :                                       i, ctx->input_pads[i].name,
     208             :                                       inlink->sample_aspect_ratio.num,
     209             :                                       inlink->sample_aspect_ratio.den,
     210           0 :                                       ctx->output_pads[0].name,
     211             :                                       outlink->sample_aspect_ratio.num,
     212             :                                       outlink->sample_aspect_ratio.den);
     213           0 :             return AVERROR(EINVAL);
     214             :         }
     215             : 
     216           1 :         inputp->planewidth[1]  =
     217           1 :         inputp->planewidth[2]  = AV_CEIL_RSHIFT(((indesc->comp[1].depth > 8) + 1) * inlink->w, indesc->log2_chroma_w);
     218           1 :         inputp->planewidth[0]  =
     219           1 :         inputp->planewidth[3]  = ((indesc->comp[0].depth > 8) + 1) * inlink->w;
     220           1 :         inputp->planeheight[1] =
     221           1 :         inputp->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, indesc->log2_chroma_h);
     222           1 :         inputp->planeheight[0] =
     223           1 :         inputp->planeheight[3] = inlink->h;
     224           1 :         inputp->nb_planes = av_pix_fmt_count_planes(inlink->format);
     225             : 
     226           4 :         for (j = 0; j < inputp->nb_planes; j++)
     227           3 :             inputp->depth[j] = indesc->comp[j].depth;
     228             : 
     229           1 :         in[i].time_base = inlink->time_base;
     230           1 :         in[i].sync   = 1;
     231           1 :         in[i].before = EXT_STOP;
     232           1 :         in[i].after  = EXT_STOP;
     233             :     }
     234             : 
     235           4 :     for (i = 0; i < s->nb_planes; i++) {
     236           3 :         const int input = s->map[i][1];
     237           3 :         const int plane = s->map[i][0];
     238           3 :         InputParam *inputp = &inputsp[input];
     239             : 
     240           3 :         if (plane + 1 > inputp->nb_planes) {
     241           0 :             av_log(ctx, AV_LOG_ERROR, "input %d does not have %d plane\n",
     242             :                                       input, plane);
     243           0 :             goto fail;
     244             :         }
     245           3 :         if (s->outdesc->comp[i].depth != inputp->depth[plane]) {
     246           0 :             av_log(ctx, AV_LOG_ERROR, "output plane %d depth %d does not "
     247             :                                       "match input %d plane %d depth %d\n",
     248           0 :                                       i, s->outdesc->comp[i].depth,
     249             :                                       input, plane, inputp->depth[plane]);
     250           0 :             goto fail;
     251             :         }
     252           3 :         if (s->planewidth[i] != inputp->planewidth[plane]) {
     253           0 :             av_log(ctx, AV_LOG_ERROR, "output plane %d width %d does not "
     254             :                                       "match input %d plane %d width %d\n",
     255             :                                       i, s->planewidth[i],
     256             :                                       input, plane, inputp->planewidth[plane]);
     257           0 :             goto fail;
     258             :         }
     259           3 :         if (s->planeheight[i] != inputp->planeheight[plane]) {
     260           0 :             av_log(ctx, AV_LOG_ERROR, "output plane %d height %d does not "
     261             :                                       "match input %d plane %d height %d\n",
     262             :                                       i, s->planeheight[i],
     263             :                                       input, plane, inputp->planeheight[plane]);
     264           0 :             goto fail;
     265             :         }
     266             :     }
     267             : 
     268           1 :     return ff_framesync_configure(&s->fs);
     269           0 : fail:
     270           0 :     return AVERROR(EINVAL);
     271             : }
     272             : 
     273         101 : static int activate(AVFilterContext *ctx)
     274             : {
     275         101 :     MergePlanesContext *s = ctx->priv;
     276         101 :     return ff_framesync_activate(&s->fs);
     277             : }
     278             : 
     279           2 : static av_cold void uninit(AVFilterContext *ctx)
     280             : {
     281           2 :     MergePlanesContext *s = ctx->priv;
     282             :     int i;
     283             : 
     284           2 :     ff_framesync_uninit(&s->fs);
     285             : 
     286           4 :     for (i = 0; i < ctx->nb_inputs; i++)
     287           2 :         av_freep(&ctx->input_pads[i].name);
     288           2 : }
     289             : 
     290             : static const AVFilterPad mergeplanes_outputs[] = {
     291             :     {
     292             :         .name          = "default",
     293             :         .type          = AVMEDIA_TYPE_VIDEO,
     294             :         .config_props  = config_output,
     295             :     },
     296             :     { NULL }
     297             : };
     298             : 
     299             : AVFilter ff_vf_mergeplanes = {
     300             :     .name          = "mergeplanes",
     301             :     .description   = NULL_IF_CONFIG_SMALL("Merge planes."),
     302             :     .priv_size     = sizeof(MergePlanesContext),
     303             :     .priv_class    = &mergeplanes_class,
     304             :     .init          = init,
     305             :     .uninit        = uninit,
     306             :     .query_formats = query_formats,
     307             :     .activate      = activate,
     308             :     .inputs        = NULL,
     309             :     .outputs       = mergeplanes_outputs,
     310             :     .flags         = AVFILTER_FLAG_DYNAMIC_INPUTS,
     311             : };

Generated by: LCOV version 1.13