LCOV - code coverage report
Current view: top level - libavfilter - vf_tile.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 83 118 70.3 %
Date: 2017-12-12 11:08:38 Functions: 8 9 88.9 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2012 Nicolas George
       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             :  * tile video filter
      24             :  */
      25             : 
      26             : #include "libavutil/imgutils.h"
      27             : #include "libavutil/opt.h"
      28             : #include "libavutil/pixdesc.h"
      29             : #include "avfilter.h"
      30             : #include "drawutils.h"
      31             : #include "formats.h"
      32             : #include "video.h"
      33             : #include "internal.h"
      34             : 
      35             : typedef struct TileContext {
      36             :     const AVClass *class;
      37             :     unsigned w, h;
      38             :     unsigned margin;
      39             :     unsigned padding;
      40             :     unsigned overlap;
      41             :     unsigned init_padding;
      42             :     unsigned current;
      43             :     unsigned nb_frames;
      44             :     FFDrawContext draw;
      45             :     FFDrawColor blank;
      46             :     AVFrame *out_ref;
      47             :     AVFrame *prev_out_ref;
      48             :     uint8_t rgba_color[4];
      49             : } TileContext;
      50             : 
      51             : #define OFFSET(x) offsetof(TileContext, x)
      52             : #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
      53             : 
      54             : static const AVOption tile_options[] = {
      55             :     { "layout", "set grid size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE,
      56             :         {.str = "6x5"}, 0, 0, FLAGS },
      57             :     { "nb_frames", "set maximum number of frame to render", OFFSET(nb_frames),
      58             :         AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS },
      59             :     { "margin",  "set outer border margin in pixels",    OFFSET(margin),
      60             :         AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1024, FLAGS },
      61             :     { "padding", "set inner border thickness in pixels", OFFSET(padding),
      62             :         AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1024, FLAGS },
      63             :     { "color",   "set the color of the unused area", OFFSET(rgba_color), AV_OPT_TYPE_COLOR, {.str = "black"}, .flags = FLAGS },
      64             :     { "overlap", "set how many frames to overlap for each render", OFFSET(overlap),
      65             :         AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS },
      66             :     { "init_padding", " set how many frames to initially pad", OFFSET(init_padding),
      67             :         AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS },
      68             :     { NULL }
      69             : };
      70             : 
      71             : AVFILTER_DEFINE_CLASS(tile);
      72             : 
      73           1 : static av_cold int init(AVFilterContext *ctx)
      74             : {
      75           1 :     TileContext *tile = ctx->priv;
      76             : 
      77           1 :     if (tile->w > UINT_MAX / tile->h) {
      78           0 :         av_log(ctx, AV_LOG_ERROR, "Tile size %ux%u is insane.\n",
      79             :                tile->w, tile->h);
      80           0 :         return AVERROR(EINVAL);
      81             :     }
      82             : 
      83           1 :     if (tile->padding) {
      84           2 :         if ((tile->w - 1 > (UINT32_MAX - 2 * tile->margin) / tile->padding) ||
      85           1 :             (tile->h - 1 > (UINT32_MAX - 2 * tile->margin) / tile->padding)) {
      86           0 :             av_log(ctx, AV_LOG_ERROR, "Combination of Tile size %ux%u, padding %d and margin %d overflows.\n",
      87             :                    tile->w, tile->h, tile->padding, tile->margin);
      88           0 :             return AVERROR(EINVAL);
      89             :         }
      90             :     }
      91             : 
      92           1 :     if (tile->nb_frames == 0) {
      93           0 :         tile->nb_frames = tile->w * tile->h;
      94           1 :     } else if (tile->nb_frames > tile->w * tile->h) {
      95           0 :         av_log(ctx, AV_LOG_ERROR, "nb_frames must be less than or equal to %dx%d=%d\n",
      96           0 :                tile->w, tile->h, tile->w * tile->h);
      97           0 :         return AVERROR(EINVAL);
      98             :     }
      99             : 
     100           1 :     if (tile->overlap >= tile->nb_frames) {
     101           0 :         av_log(ctx, AV_LOG_WARNING, "overlap must be less than %d\n", tile->nb_frames);
     102           0 :         tile->overlap = tile->nb_frames - 1;
     103             :     }
     104             : 
     105           1 :     if (tile->init_padding >= tile->nb_frames) {
     106           0 :         av_log(ctx, AV_LOG_WARNING, "init_padding must be less than %d\n", tile->nb_frames);
     107             :     } else {
     108           1 :         tile->current = tile->init_padding;
     109             :     }
     110             : 
     111           1 :     return 0;
     112             : }
     113             : 
     114           1 : static int query_formats(AVFilterContext *ctx)
     115             : {
     116           1 :     return ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
     117             : }
     118             : 
     119           1 : static int config_props(AVFilterLink *outlink)
     120             : {
     121           1 :     AVFilterContext *ctx = outlink->src;
     122           1 :     TileContext *tile    = ctx->priv;
     123           1 :     AVFilterLink *inlink = ctx->inputs[0];
     124           1 :     const unsigned total_margin_w = (tile->w - 1) * tile->padding + 2*tile->margin;
     125           1 :     const unsigned total_margin_h = (tile->h - 1) * tile->padding + 2*tile->margin;
     126             : 
     127           1 :     if (inlink->w > (INT_MAX - total_margin_w) / tile->w) {
     128           0 :         av_log(ctx, AV_LOG_ERROR, "Total width %ux%u is too much.\n",
     129             :                tile->w, inlink->w);
     130           0 :         return AVERROR(EINVAL);
     131             :     }
     132           1 :     if (inlink->h > (INT_MAX - total_margin_h) / tile->h) {
     133           0 :         av_log(ctx, AV_LOG_ERROR, "Total height %ux%u is too much.\n",
     134             :                tile->h, inlink->h);
     135           0 :         return AVERROR(EINVAL);
     136             :     }
     137           1 :     outlink->w = tile->w * inlink->w + total_margin_w;
     138           1 :     outlink->h = tile->h * inlink->h + total_margin_h;
     139           1 :     outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
     140           1 :     outlink->frame_rate = av_mul_q(inlink->frame_rate,
     141           1 :                                    av_make_q(1, tile->nb_frames - tile->overlap));
     142           1 :     ff_draw_init(&tile->draw, inlink->format, 0);
     143           1 :     ff_draw_color(&tile->draw, &tile->blank, tile->rgba_color);
     144             : 
     145           1 :     return 0;
     146             : }
     147             : 
     148          25 : static void get_tile_pos(AVFilterContext *ctx, unsigned *x, unsigned *y, unsigned current)
     149             : {
     150          25 :     TileContext *tile    = ctx->priv;
     151          25 :     AVFilterLink *inlink = ctx->inputs[0];
     152          25 :     const unsigned tx = current % tile->w;
     153          25 :     const unsigned ty = current / tile->w;
     154             : 
     155          25 :     *x = tile->margin + (inlink->w + tile->padding) * tx;
     156          25 :     *y = tile->margin + (inlink->h + tile->padding) * ty;
     157          25 : }
     158             : 
     159           0 : static void draw_blank_frame(AVFilterContext *ctx, AVFrame *out_buf)
     160             : {
     161           0 :     TileContext *tile    = ctx->priv;
     162           0 :     AVFilterLink *inlink = ctx->inputs[0];
     163             :     unsigned x0, y0;
     164             : 
     165           0 :     get_tile_pos(ctx, &x0, &y0, tile->current);
     166           0 :     ff_fill_rectangle(&tile->draw, &tile->blank,
     167           0 :                       out_buf->data, out_buf->linesize,
     168             :                       x0, y0, inlink->w, inlink->h);
     169           0 :     tile->current++;
     170           0 : }
     171             : 
     172           5 : static int end_last_frame(AVFilterContext *ctx)
     173             : {
     174           5 :     TileContext *tile     = ctx->priv;
     175           5 :     AVFilterLink *outlink = ctx->outputs[0];
     176           5 :     AVFrame *out_buf = tile->out_ref;
     177             :     int ret;
     178             : 
     179          10 :     while (tile->current < tile->nb_frames)
     180           0 :         draw_blank_frame(ctx, out_buf);
     181           5 :     tile->current = tile->overlap;
     182           5 :     if (tile->current) {
     183           0 :         av_frame_free(&tile->prev_out_ref);
     184           0 :         tile->prev_out_ref = av_frame_clone(out_buf);
     185             :     }
     186           5 :     ret = ff_filter_frame(outlink, out_buf);
     187           5 :     tile->out_ref = NULL;
     188           5 :     return ret;
     189             : }
     190             : 
     191             : /* Note: direct rendering is not possible since there is no guarantee that
     192             :  * buffers are fed to filter_frame in the order they were obtained from
     193             :  * get_buffer (think B-frames). */
     194             : 
     195          25 : static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
     196             : {
     197          25 :     AVFilterContext *ctx  = inlink->dst;
     198          25 :     TileContext *tile     = ctx->priv;
     199          25 :     AVFilterLink *outlink = ctx->outputs[0];
     200             :     unsigned x0, y0;
     201             : 
     202          25 :     if (!tile->out_ref) {
     203           5 :         tile->out_ref = ff_get_video_buffer(outlink, outlink->w, outlink->h);
     204           5 :         if (!tile->out_ref) {
     205           0 :             av_frame_free(&picref);
     206           0 :             return AVERROR(ENOMEM);
     207             :         }
     208           5 :         av_frame_copy_props(tile->out_ref, picref);
     209           5 :         tile->out_ref->width  = outlink->w;
     210           5 :         tile->out_ref->height = outlink->h;
     211             : 
     212             :         /* fill surface once for margin/padding */
     213           5 :         if (tile->margin || tile->padding || tile->init_padding)
     214          10 :             ff_fill_rectangle(&tile->draw, &tile->blank,
     215           5 :                               tile->out_ref->data,
     216           5 :                               tile->out_ref->linesize,
     217             :                               0, 0, outlink->w, outlink->h);
     218           5 :         tile->init_padding = 0;
     219             :     }
     220             : 
     221          25 :     if (tile->prev_out_ref) {
     222             :         unsigned x1, y1, i;
     223             : 
     224           0 :         for (i = tile->nb_frames - tile->overlap; i < tile->nb_frames; i++) {
     225           0 :             get_tile_pos(ctx, &x1, &y1, i);
     226           0 :             get_tile_pos(ctx, &x0, &y0, i - (tile->nb_frames - tile->overlap));
     227           0 :             ff_copy_rectangle2(&tile->draw,
     228           0 :                                tile->out_ref->data, tile->out_ref->linesize,
     229           0 :                                tile->prev_out_ref->data, tile->prev_out_ref->linesize,
     230             :                                x0, y0, x1, y1, inlink->w, inlink->h);
     231             : 
     232             :         }
     233             :     }
     234             : 
     235          25 :     get_tile_pos(ctx, &x0, &y0, tile->current);
     236          50 :     ff_copy_rectangle2(&tile->draw,
     237          25 :                        tile->out_ref->data, tile->out_ref->linesize,
     238          25 :                        picref->data, picref->linesize,
     239             :                        x0, y0, 0, 0, inlink->w, inlink->h);
     240             : 
     241          25 :     av_frame_free(&picref);
     242          25 :     if (++tile->current == tile->nb_frames)
     243           5 :         return end_last_frame(ctx);
     244             : 
     245          20 :     return 0;
     246             : }
     247             : 
     248          25 : static int request_frame(AVFilterLink *outlink)
     249             : {
     250          25 :     AVFilterContext *ctx = outlink->src;
     251          25 :     TileContext *tile    = ctx->priv;
     252          25 :     AVFilterLink *inlink = ctx->inputs[0];
     253             :     int r;
     254             : 
     255          25 :     r = ff_request_frame(inlink);
     256          25 :     if (r == AVERROR_EOF && tile->current && tile->out_ref)
     257           0 :         r = end_last_frame(ctx);
     258          25 :     return r;
     259             : }
     260             : 
     261           1 : static av_cold void uninit(AVFilterContext *ctx)
     262             : {
     263           1 :     TileContext *tile = ctx->priv;
     264             : 
     265           1 :     av_frame_free(&tile->prev_out_ref);
     266           1 : }
     267             : 
     268             : static const AVFilterPad tile_inputs[] = {
     269             :     {
     270             :         .name         = "default",
     271             :         .type         = AVMEDIA_TYPE_VIDEO,
     272             :         .filter_frame = filter_frame,
     273             :     },
     274             :     { NULL }
     275             : };
     276             : 
     277             : static const AVFilterPad tile_outputs[] = {
     278             :     {
     279             :         .name          = "default",
     280             :         .type          = AVMEDIA_TYPE_VIDEO,
     281             :         .config_props  = config_props,
     282             :         .request_frame = request_frame,
     283             :     },
     284             :     { NULL }
     285             : };
     286             : 
     287             : AVFilter ff_vf_tile = {
     288             :     .name          = "tile",
     289             :     .description   = NULL_IF_CONFIG_SMALL("Tile several successive frames together."),
     290             :     .init          = init,
     291             :     .uninit        = uninit,
     292             :     .query_formats = query_formats,
     293             :     .priv_size     = sizeof(TileContext),
     294             :     .inputs        = tile_inputs,
     295             :     .outputs       = tile_outputs,
     296             :     .priv_class    = &tile_class,
     297             : };

Generated by: LCOV version 1.13