2012-02-23 16:32:27 +03:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2017-10-10 21:18:03 +02:00
|
|
|
#include "libavutil/imgutils.h"
|
2012-11-12 00:36:26 +03:00
|
|
|
#include "libavutil/opt.h"
|
2012-02-23 16:32:27 +03:00
|
|
|
#include "libavutil/pixdesc.h"
|
|
|
|
#include "avfilter.h"
|
|
|
|
#include "drawutils.h"
|
2012-06-10 11:40:33 +03:00
|
|
|
#include "formats.h"
|
|
|
|
#include "video.h"
|
2012-06-24 03:09:53 +03:00
|
|
|
#include "internal.h"
|
2012-02-23 16:32:27 +03:00
|
|
|
|
2017-05-12 20:00:49 +02:00
|
|
|
typedef struct TileContext {
|
2012-11-12 00:36:26 +03:00
|
|
|
const AVClass *class;
|
2012-02-23 16:32:27 +03:00
|
|
|
unsigned w, h;
|
2012-11-12 00:37:20 +03:00
|
|
|
unsigned margin;
|
|
|
|
unsigned padding;
|
2017-11-07 20:41:06 +02:00
|
|
|
unsigned overlap;
|
2017-11-17 22:33:37 +02:00
|
|
|
unsigned init_padding;
|
2012-02-23 16:32:27 +03:00
|
|
|
unsigned current;
|
2012-11-09 01:12:43 +03:00
|
|
|
unsigned nb_frames;
|
2012-02-23 16:32:27 +03:00
|
|
|
FFDrawContext draw;
|
|
|
|
FFDrawColor blank;
|
2013-03-10 03:30:30 +03:00
|
|
|
AVFrame *out_ref;
|
2017-11-07 20:41:06 +02:00
|
|
|
AVFrame *prev_out_ref;
|
2013-08-15 00:51:27 +03:00
|
|
|
uint8_t rgba_color[4];
|
2012-02-23 16:32:27 +03:00
|
|
|
} TileContext;
|
|
|
|
|
2012-11-12 00:36:26 +03:00
|
|
|
#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 },
|
2013-04-11 00:15:06 +03:00
|
|
|
{ "nb_frames", "set maximum number of frame to render", OFFSET(nb_frames),
|
|
|
|
AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS },
|
2012-11-12 00:37:20 +03:00
|
|
|
{ "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 },
|
2013-08-15 00:51:27 +03:00
|
|
|
{ "color", "set the color of the unused area", OFFSET(rgba_color), AV_OPT_TYPE_COLOR, {.str = "black"}, .flags = FLAGS },
|
2017-11-07 20:41:06 +02:00
|
|
|
{ "overlap", "set how many frames to overlap for each render", OFFSET(overlap),
|
|
|
|
AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS },
|
2021-02-05 13:31:01 +02:00
|
|
|
{ "init_padding", "set how many frames to initially pad", OFFSET(init_padding),
|
2017-11-17 22:33:37 +02:00
|
|
|
AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS },
|
2013-09-07 15:13:50 +03:00
|
|
|
{ NULL }
|
2012-11-12 00:36:26 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
AVFILTER_DEFINE_CLASS(tile);
|
|
|
|
|
2013-04-12 12:13:33 +03:00
|
|
|
static av_cold int init(AVFilterContext *ctx)
|
2012-02-23 16:32:27 +03:00
|
|
|
{
|
|
|
|
TileContext *tile = ctx->priv;
|
|
|
|
|
2017-10-10 21:18:03 +02:00
|
|
|
if (tile->w > UINT_MAX / tile->h) {
|
2012-02-23 16:32:27 +03:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "Tile size %ux%u is insane.\n",
|
|
|
|
tile->w, tile->h);
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
2012-11-09 01:12:43 +03:00
|
|
|
|
2017-10-10 21:18:03 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-09 01:12:43 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-11-07 20:41:06 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-11-17 22:33:37 +02:00
|
|
|
if (tile->init_padding >= tile->nb_frames) {
|
|
|
|
av_log(ctx, AV_LOG_WARNING, "init_padding must be less than %d\n", tile->nb_frames);
|
|
|
|
} else {
|
|
|
|
tile->current = tile->init_padding;
|
|
|
|
}
|
|
|
|
|
2012-02-23 16:32:27 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int query_formats(AVFilterContext *ctx)
|
|
|
|
{
|
2015-04-03 19:55:18 +02:00
|
|
|
return ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
|
2012-02-23 16:32:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int config_props(AVFilterLink *outlink)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = outlink->src;
|
2012-12-26 23:32:38 +03:00
|
|
|
TileContext *tile = ctx->priv;
|
2012-02-23 16:32:27 +03:00
|
|
|
AVFilterLink *inlink = ctx->inputs[0];
|
2012-11-12 00:37:20 +03:00
|
|
|
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;
|
2012-02-23 16:32:27 +03:00
|
|
|
|
2012-11-12 00:37:20 +03:00
|
|
|
if (inlink->w > (INT_MAX - total_margin_w) / tile->w) {
|
2012-02-23 16:32:27 +03:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "Total width %ux%u is too much.\n",
|
|
|
|
tile->w, inlink->w);
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
2012-11-12 00:37:20 +03:00
|
|
|
if (inlink->h > (INT_MAX - total_margin_h) / tile->h) {
|
2012-02-23 16:32:27 +03:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "Total height %ux%u is too much.\n",
|
|
|
|
tile->h, inlink->h);
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
2012-11-12 00:37:20 +03:00
|
|
|
outlink->w = tile->w * inlink->w + total_margin_w;
|
|
|
|
outlink->h = tile->h * inlink->h + total_margin_h;
|
2012-02-23 16:32:27 +03:00
|
|
|
outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
|
2012-06-10 11:35:17 +03:00
|
|
|
outlink->frame_rate = av_mul_q(inlink->frame_rate,
|
2017-11-07 20:41:06 +02:00
|
|
|
av_make_q(1, tile->nb_frames - tile->overlap));
|
2012-02-23 16:32:27 +03:00
|
|
|
ff_draw_init(&tile->draw, inlink->format, 0);
|
2013-08-15 00:51:27 +03:00
|
|
|
ff_draw_color(&tile->draw, &tile->blank, tile->rgba_color);
|
2012-02-23 16:32:27 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-11-07 20:41:06 +02:00
|
|
|
static void get_tile_pos(AVFilterContext *ctx, unsigned *x, unsigned *y, unsigned current)
|
2012-11-12 00:37:20 +03:00
|
|
|
{
|
|
|
|
TileContext *tile = ctx->priv;
|
|
|
|
AVFilterLink *inlink = ctx->inputs[0];
|
2017-11-07 20:41:06 +02:00
|
|
|
const unsigned tx = current % tile->w;
|
|
|
|
const unsigned ty = current / tile->w;
|
2012-11-12 00:37:20 +03:00
|
|
|
|
|
|
|
*x = tile->margin + (inlink->w + tile->padding) * tx;
|
|
|
|
*y = tile->margin + (inlink->h + tile->padding) * ty;
|
|
|
|
}
|
|
|
|
|
2013-03-10 03:30:30 +03:00
|
|
|
static void draw_blank_frame(AVFilterContext *ctx, AVFrame *out_buf)
|
2012-02-23 16:32:27 +03:00
|
|
|
{
|
|
|
|
TileContext *tile = ctx->priv;
|
2012-12-26 23:32:38 +03:00
|
|
|
AVFilterLink *inlink = ctx->inputs[0];
|
2012-11-12 00:37:20 +03:00
|
|
|
unsigned x0, y0;
|
2012-02-23 16:32:27 +03:00
|
|
|
|
2017-11-07 20:41:06 +02:00
|
|
|
get_tile_pos(ctx, &x0, &y0, tile->current);
|
2012-02-23 16:32:27 +03:00
|
|
|
ff_fill_rectangle(&tile->draw, &tile->blank,
|
2012-08-01 13:23:48 +03:00
|
|
|
out_buf->data, out_buf->linesize,
|
2012-02-23 16:32:27 +03:00
|
|
|
x0, y0, inlink->w, inlink->h);
|
|
|
|
tile->current++;
|
|
|
|
}
|
2017-10-10 21:18:03 +02:00
|
|
|
|
2012-11-29 19:13:06 +03:00
|
|
|
static int end_last_frame(AVFilterContext *ctx)
|
2012-02-23 16:32:27 +03:00
|
|
|
{
|
2012-12-26 23:32:38 +03:00
|
|
|
TileContext *tile = ctx->priv;
|
2012-02-23 16:32:27 +03:00
|
|
|
AVFilterLink *outlink = ctx->outputs[0];
|
2013-03-10 03:30:30 +03:00
|
|
|
AVFrame *out_buf = tile->out_ref;
|
2012-11-29 19:13:06 +03:00
|
|
|
int ret;
|
2012-02-23 16:32:27 +03:00
|
|
|
|
2012-11-09 01:12:43 +03:00
|
|
|
while (tile->current < tile->nb_frames)
|
2012-08-01 13:23:48 +03:00
|
|
|
draw_blank_frame(ctx, out_buf);
|
2017-11-07 20:41:06 +02:00
|
|
|
tile->current = tile->overlap;
|
|
|
|
if (tile->current) {
|
|
|
|
av_frame_free(&tile->prev_out_ref);
|
|
|
|
tile->prev_out_ref = av_frame_clone(out_buf);
|
|
|
|
}
|
2012-11-29 19:13:06 +03:00
|
|
|
ret = ff_filter_frame(outlink, out_buf);
|
2017-11-07 20:41:06 +02:00
|
|
|
tile->out_ref = NULL;
|
2012-11-29 19:13:06 +03:00
|
|
|
return ret;
|
2012-02-23 16:32:27 +03:00
|
|
|
}
|
|
|
|
|
2012-11-29 19:08:30 +03:00
|
|
|
/* 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). */
|
|
|
|
|
2013-03-10 03:30:30 +03:00
|
|
|
static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
|
2012-02-23 16:32:27 +03:00
|
|
|
{
|
|
|
|
AVFilterContext *ctx = inlink->dst;
|
2012-12-26 23:32:38 +03:00
|
|
|
TileContext *tile = ctx->priv;
|
2012-11-29 19:08:30 +03:00
|
|
|
AVFilterLink *outlink = ctx->outputs[0];
|
|
|
|
unsigned x0, y0;
|
|
|
|
|
2017-11-07 20:41:06 +02:00
|
|
|
if (!tile->out_ref) {
|
2013-03-10 03:30:30 +03:00
|
|
|
tile->out_ref = ff_get_video_buffer(outlink, outlink->w, outlink->h);
|
2013-08-22 23:31:22 +03:00
|
|
|
if (!tile->out_ref) {
|
|
|
|
av_frame_free(&picref);
|
2012-11-29 19:08:30 +03:00
|
|
|
return AVERROR(ENOMEM);
|
2013-08-22 23:31:22 +03:00
|
|
|
}
|
2013-03-10 03:30:30 +03:00
|
|
|
av_frame_copy_props(tile->out_ref, picref);
|
|
|
|
tile->out_ref->width = outlink->w;
|
|
|
|
tile->out_ref->height = outlink->h;
|
2012-11-29 19:08:30 +03:00
|
|
|
|
|
|
|
/* fill surface once for margin/padding */
|
2017-11-17 22:33:37 +02:00
|
|
|
if (tile->margin || tile->padding || tile->init_padding)
|
2012-11-29 19:08:30 +03:00
|
|
|
ff_fill_rectangle(&tile->draw, &tile->blank,
|
2012-12-26 22:19:30 +03:00
|
|
|
tile->out_ref->data,
|
|
|
|
tile->out_ref->linesize,
|
2012-11-29 19:08:30 +03:00
|
|
|
0, 0, outlink->w, outlink->h);
|
2017-11-17 22:33:37 +02:00
|
|
|
tile->init_padding = 0;
|
2012-11-29 19:08:30 +03:00
|
|
|
}
|
|
|
|
|
2017-11-07 20:41:06 +02:00
|
|
|
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);
|
2012-11-29 19:08:30 +03:00
|
|
|
ff_copy_rectangle2(&tile->draw,
|
2012-12-26 22:19:30 +03:00
|
|
|
tile->out_ref->data, tile->out_ref->linesize,
|
|
|
|
picref->data, picref->linesize,
|
2012-11-29 19:08:30 +03:00
|
|
|
x0, y0, 0, 0, inlink->w, inlink->h);
|
2012-02-23 16:32:27 +03:00
|
|
|
|
2013-03-10 03:30:30 +03:00
|
|
|
av_frame_free(&picref);
|
2012-11-09 01:12:43 +03:00
|
|
|
if (++tile->current == tile->nb_frames)
|
2012-11-29 19:13:06 +03:00
|
|
|
return end_last_frame(ctx);
|
2012-11-29 19:08:30 +03:00
|
|
|
|
2012-07-22 23:57:02 +03:00
|
|
|
return 0;
|
2012-02-23 16:32:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int request_frame(AVFilterLink *outlink)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = outlink->src;
|
2012-12-26 23:32:38 +03:00
|
|
|
TileContext *tile = ctx->priv;
|
2012-02-23 16:32:27 +03:00
|
|
|
AVFilterLink *inlink = ctx->inputs[0];
|
|
|
|
int r;
|
|
|
|
|
2013-03-31 20:19:35 +03:00
|
|
|
r = ff_request_frame(inlink);
|
2017-11-07 20:41:06 +02:00
|
|
|
if (r == AVERROR_EOF && tile->current && tile->out_ref)
|
2013-03-31 20:19:35 +03:00
|
|
|
r = end_last_frame(ctx);
|
2012-11-29 19:13:06 +03:00
|
|
|
return r;
|
2012-02-23 16:32:27 +03:00
|
|
|
}
|
|
|
|
|
2017-11-07 20:41:06 +02:00
|
|
|
static av_cold void uninit(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
TileContext *tile = ctx->priv;
|
|
|
|
|
2019-10-19 19:24:52 +02:00
|
|
|
av_frame_free(&tile->out_ref);
|
2017-11-07 20:41:06 +02:00
|
|
|
av_frame_free(&tile->prev_out_ref);
|
|
|
|
}
|
|
|
|
|
2012-11-28 22:01:59 +03:00
|
|
|
static const AVFilterPad tile_inputs[] = {
|
|
|
|
{
|
2012-11-29 19:10:05 +03:00
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.filter_frame = filter_frame,
|
2012-11-28 22:01:59 +03:00
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad tile_outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.config_props = config_props,
|
|
|
|
.request_frame = request_frame,
|
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
2012-02-23 16:32:27 +03:00
|
|
|
|
2013-10-29 13:50:56 +03:00
|
|
|
AVFilter ff_vf_tile = {
|
2012-02-23 16:32:27 +03:00
|
|
|
.name = "tile",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Tile several successive frames together."),
|
|
|
|
.init = init,
|
2017-11-07 20:41:06 +02:00
|
|
|
.uninit = uninit,
|
2012-02-23 16:32:27 +03:00
|
|
|
.query_formats = query_formats,
|
|
|
|
.priv_size = sizeof(TileContext),
|
2012-11-28 22:01:59 +03:00
|
|
|
.inputs = tile_inputs,
|
|
|
|
.outputs = tile_outputs,
|
|
|
|
.priv_class = &tile_class,
|
2012-02-23 16:32:27 +03:00
|
|
|
};
|