libavfilter/vf_tile.c
3493390d
 /*
  * Copyright (c) 2012 Nicolas George
  *
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
  * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 /**
  * @file
  * tile video filter
  */
 
1f24b33d
 #include "libavutil/imgutils.h"
aa5a0290
 #include "libavutil/opt.h"
3493390d
 #include "libavutil/pixdesc.h"
 #include "avfilter.h"
 #include "drawutils.h"
c8428a0d
 #include "formats.h"
 #include "video.h"
1c600888
 #include "internal.h"
3493390d
 
ed93ed5e
 typedef struct TileContext {
aa5a0290
     const AVClass *class;
3493390d
     unsigned w, h;
ecfaff35
     unsigned margin;
     unsigned padding;
7a060867
     unsigned overlap;
3493390d
     unsigned current;
4a90e712
     unsigned nb_frames;
3493390d
     FFDrawContext draw;
     FFDrawColor blank;
a05a44e2
     AVFrame *out_ref;
7a060867
     AVFrame *prev_out_ref;
ef6718a5
     uint8_t rgba_color[4];
3493390d
 } TileContext;
 
aa5a0290
 #define OFFSET(x) offsetof(TileContext, x)
 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
 
 static const AVOption tile_options[] = {
     { "layout", "set grid size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE,
         {.str = "6x5"}, 0, 0, FLAGS },
64a10313
     { "nb_frames", "set maximum number of frame to render", OFFSET(nb_frames),
         AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS },
ecfaff35
     { "margin",  "set outer border margin in pixels",    OFFSET(margin),
         AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1024, FLAGS },
     { "padding", "set inner border thickness in pixels", OFFSET(padding),
         AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1024, FLAGS },
ef6718a5
     { "color",   "set the color of the unused area", OFFSET(rgba_color), AV_OPT_TYPE_COLOR, {.str = "black"}, .flags = FLAGS },
7a060867
     { "overlap", "set how many frames to overlap for each render", OFFSET(overlap),
         AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS },
b211607b
     { NULL }
aa5a0290
 };
 
 AVFILTER_DEFINE_CLASS(tile);
 
fd6228e6
 static av_cold int init(AVFilterContext *ctx)
3493390d
 {
     TileContext *tile = ctx->priv;
 
1f24b33d
     if (tile->w > UINT_MAX / tile->h) {
3493390d
         av_log(ctx, AV_LOG_ERROR, "Tile size %ux%u is insane.\n",
                tile->w, tile->h);
         return AVERROR(EINVAL);
     }
4a90e712
 
1f24b33d
     if (tile->padding) {
         if ((tile->w - 1 > (UINT32_MAX - 2 * tile->margin) / tile->padding) ||
             (tile->h - 1 > (UINT32_MAX - 2 * tile->margin) / tile->padding)) {
             av_log(ctx, AV_LOG_ERROR, "Combination of Tile size %ux%u, padding %d and margin %d overflows.\n",
                    tile->w, tile->h, tile->padding, tile->margin);
             return AVERROR(EINVAL);
         }
     }
 
4a90e712
     if (tile->nb_frames == 0) {
         tile->nb_frames = tile->w * tile->h;
     } else if (tile->nb_frames > tile->w * tile->h) {
         av_log(ctx, AV_LOG_ERROR, "nb_frames must be less than or equal to %dx%d=%d\n",
                tile->w, tile->h, tile->w * tile->h);
         return AVERROR(EINVAL);
     }
 
7a060867
     if (tile->overlap >= tile->nb_frames) {
         av_log(ctx, AV_LOG_WARNING, "overlap must be less than %d\n", tile->nb_frames);
         tile->overlap = tile->nb_frames - 1;
     }
 
3493390d
     return 0;
 }
 
 static int query_formats(AVFilterContext *ctx)
 {
a0854c08
     return ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
3493390d
 }
 
 static int config_props(AVFilterLink *outlink)
 {
     AVFilterContext *ctx = outlink->src;
ceee4407
     TileContext *tile    = ctx->priv;
3493390d
     AVFilterLink *inlink = ctx->inputs[0];
ecfaff35
     const unsigned total_margin_w = (tile->w - 1) * tile->padding + 2*tile->margin;
     const unsigned total_margin_h = (tile->h - 1) * tile->padding + 2*tile->margin;
3493390d
 
ecfaff35
     if (inlink->w > (INT_MAX - total_margin_w) / tile->w) {
3493390d
         av_log(ctx, AV_LOG_ERROR, "Total width %ux%u is too much.\n",
                tile->w, inlink->w);
         return AVERROR(EINVAL);
     }
ecfaff35
     if (inlink->h > (INT_MAX - total_margin_h) / tile->h) {
3493390d
         av_log(ctx, AV_LOG_ERROR, "Total height %ux%u is too much.\n",
                tile->h, inlink->h);
         return AVERROR(EINVAL);
     }
ecfaff35
     outlink->w = tile->w * inlink->w + total_margin_w;
     outlink->h = tile->h * inlink->h + total_margin_h;
3493390d
     outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
7b993c4a
     outlink->frame_rate = av_mul_q(inlink->frame_rate,
7a060867
                                    av_make_q(1, tile->nb_frames - tile->overlap));
3493390d
     ff_draw_init(&tile->draw, inlink->format, 0);
ef6718a5
     ff_draw_color(&tile->draw, &tile->blank, tile->rgba_color);
3493390d
 
     return 0;
 }
 
7a060867
 static void get_tile_pos(AVFilterContext *ctx, unsigned *x, unsigned *y, unsigned current)
ecfaff35
 {
     TileContext *tile    = ctx->priv;
     AVFilterLink *inlink = ctx->inputs[0];
7a060867
     const unsigned tx = current % tile->w;
     const unsigned ty = current / tile->w;
ecfaff35
 
     *x = tile->margin + (inlink->w + tile->padding) * tx;
     *y = tile->margin + (inlink->h + tile->padding) * ty;
 }
 
a05a44e2
 static void draw_blank_frame(AVFilterContext *ctx, AVFrame *out_buf)
3493390d
 {
     TileContext *tile    = ctx->priv;
ceee4407
     AVFilterLink *inlink = ctx->inputs[0];
ecfaff35
     unsigned x0, y0;
3493390d
 
7a060867
     get_tile_pos(ctx, &x0, &y0, tile->current);
3493390d
     ff_fill_rectangle(&tile->draw, &tile->blank,
f952b23e
                       out_buf->data, out_buf->linesize,
3493390d
                       x0, y0, inlink->w, inlink->h);
     tile->current++;
 }
1f24b33d
 
24cb1f97
 static int end_last_frame(AVFilterContext *ctx)
3493390d
 {
ceee4407
     TileContext *tile     = ctx->priv;
3493390d
     AVFilterLink *outlink = ctx->outputs[0];
a05a44e2
     AVFrame *out_buf = tile->out_ref;
24cb1f97
     int ret;
3493390d
 
4a90e712
     while (tile->current < tile->nb_frames)
f952b23e
         draw_blank_frame(ctx, out_buf);
7a060867
     tile->current = tile->overlap;
     if (tile->current) {
         av_frame_free(&tile->prev_out_ref);
         tile->prev_out_ref = av_frame_clone(out_buf);
     }
24cb1f97
     ret = ff_filter_frame(outlink, out_buf);
7a060867
     tile->out_ref = NULL;
24cb1f97
     return ret;
3493390d
 }
 
6f3d2fb1
 /* Note: direct rendering is not possible since there is no guarantee that
  * buffers are fed to filter_frame in the order they were obtained from
  * get_buffer (think B-frames). */
 
a05a44e2
 static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
3493390d
 {
     AVFilterContext *ctx  = inlink->dst;
ceee4407
     TileContext *tile     = ctx->priv;
6f3d2fb1
     AVFilterLink *outlink = ctx->outputs[0];
     unsigned x0, y0;
 
7a060867
     if (!tile->out_ref) {
a05a44e2
         tile->out_ref = ff_get_video_buffer(outlink, outlink->w, outlink->h);
8fbf940e
         if (!tile->out_ref) {
             av_frame_free(&picref);
6f3d2fb1
             return AVERROR(ENOMEM);
8fbf940e
         }
a05a44e2
         av_frame_copy_props(tile->out_ref, picref);
         tile->out_ref->width  = outlink->w;
         tile->out_ref->height = outlink->h;
6f3d2fb1
 
         /* fill surface once for margin/padding */
         if (tile->margin || tile->padding)
             ff_fill_rectangle(&tile->draw, &tile->blank,
4cd724da
                               tile->out_ref->data,
                               tile->out_ref->linesize,
6f3d2fb1
                               0, 0, outlink->w, outlink->h);
     }
 
7a060867
     if (tile->prev_out_ref) {
         unsigned x1, y1, i;
 
         for (i = tile->nb_frames - tile->overlap; i < tile->nb_frames; i++) {
             get_tile_pos(ctx, &x1, &y1, i);
             get_tile_pos(ctx, &x0, &y0, i - (tile->nb_frames - tile->overlap));
             ff_copy_rectangle2(&tile->draw,
                                tile->out_ref->data, tile->out_ref->linesize,
                                tile->prev_out_ref->data, tile->prev_out_ref->linesize,
                                x0, y0, x1, y1, inlink->w, inlink->h);
 
         }
     }
 
     get_tile_pos(ctx, &x0, &y0, tile->current);
6f3d2fb1
     ff_copy_rectangle2(&tile->draw,
4cd724da
                        tile->out_ref->data, tile->out_ref->linesize,
                        picref->data, picref->linesize,
6f3d2fb1
                        x0, y0, 0, 0, inlink->w, inlink->h);
3493390d
 
a05a44e2
     av_frame_free(&picref);
4a90e712
     if (++tile->current == tile->nb_frames)
24cb1f97
         return end_last_frame(ctx);
6f3d2fb1
 
88beb2df
     return 0;
3493390d
 }
 
 static int request_frame(AVFilterLink *outlink)
 {
     AVFilterContext *ctx = outlink->src;
ceee4407
     TileContext *tile    = ctx->priv;
3493390d
     AVFilterLink *inlink = ctx->inputs[0];
     int r;
 
77fa554b
     r = ff_request_frame(inlink);
7a060867
     if (r == AVERROR_EOF && tile->current && tile->out_ref)
77fa554b
         r = end_last_frame(ctx);
24cb1f97
     return r;
3493390d
 }
 
7a060867
 static av_cold void uninit(AVFilterContext *ctx)
 {
     TileContext *tile = ctx->priv;
 
     av_frame_free(&tile->prev_out_ref);
 }
 
2d9d4440
 static const AVFilterPad tile_inputs[] = {
     {
3b316247
         .name         = "default",
         .type         = AVMEDIA_TYPE_VIDEO,
         .filter_frame = filter_frame,
2d9d4440
     },
     { NULL }
 };
 
 static const AVFilterPad tile_outputs[] = {
     {
         .name          = "default",
         .type          = AVMEDIA_TYPE_VIDEO,
         .config_props  = config_props,
         .request_frame = request_frame,
     },
     { NULL }
 };
3493390d
 
325f6e0a
 AVFilter ff_vf_tile = {
3493390d
     .name          = "tile",
     .description   = NULL_IF_CONFIG_SMALL("Tile several successive frames together."),
     .init          = init,
7a060867
     .uninit        = uninit,
3493390d
     .query_formats = query_formats,
     .priv_size     = sizeof(TileContext),
2d9d4440
     .inputs        = tile_inputs,
     .outputs       = tile_outputs,
     .priv_class    = &tile_class,
3493390d
 };