2 * Copyright (c) 2012 Nicolas George
4 * This file is part of FFmpeg.
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.
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.
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
26 #include "libavutil/imgutils.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/pixdesc.h"
30 #include "drawutils.h"
35 typedef struct TileContext {
45 uint8_t rgba_color[4];
48 #define OFFSET(x) offsetof(TileContext, x)
49 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
51 static const AVOption tile_options[] = {
52 { "layout", "set grid size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE,
53 {.str = "6x5"}, 0, 0, FLAGS },
54 { "nb_frames", "set maximum number of frame to render", OFFSET(nb_frames),
55 AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS },
56 { "margin", "set outer border margin in pixels", OFFSET(margin),
57 AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1024, FLAGS },
58 { "padding", "set inner border thickness in pixels", OFFSET(padding),
59 AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1024, FLAGS },
60 { "color", "set the color of the unused area", OFFSET(rgba_color), AV_OPT_TYPE_COLOR, {.str = "black"}, .flags = FLAGS },
64 AVFILTER_DEFINE_CLASS(tile);
66 static av_cold int init(AVFilterContext *ctx)
68 TileContext *tile = ctx->priv;
70 if (tile->w > UINT_MAX / tile->h) {
71 av_log(ctx, AV_LOG_ERROR, "Tile size %ux%u is insane.\n",
73 return AVERROR(EINVAL);
77 if ((tile->w - 1 > (UINT32_MAX - 2 * tile->margin) / tile->padding) ||
78 (tile->h - 1 > (UINT32_MAX - 2 * tile->margin) / tile->padding)) {
79 av_log(ctx, AV_LOG_ERROR, "Combination of Tile size %ux%u, padding %d and margin %d overflows.\n",
80 tile->w, tile->h, tile->padding, tile->margin);
81 return AVERROR(EINVAL);
85 if (tile->nb_frames == 0) {
86 tile->nb_frames = tile->w * tile->h;
87 } else if (tile->nb_frames > tile->w * tile->h) {
88 av_log(ctx, AV_LOG_ERROR, "nb_frames must be less than or equal to %dx%d=%d\n",
89 tile->w, tile->h, tile->w * tile->h);
90 return AVERROR(EINVAL);
96 static int query_formats(AVFilterContext *ctx)
98 return ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
101 static int config_props(AVFilterLink *outlink)
103 AVFilterContext *ctx = outlink->src;
104 TileContext *tile = ctx->priv;
105 AVFilterLink *inlink = ctx->inputs[0];
106 const unsigned total_margin_w = (tile->w - 1) * tile->padding + 2*tile->margin;
107 const unsigned total_margin_h = (tile->h - 1) * tile->padding + 2*tile->margin;
109 if (inlink->w > (INT_MAX - total_margin_w) / tile->w) {
110 av_log(ctx, AV_LOG_ERROR, "Total width %ux%u is too much.\n",
112 return AVERROR(EINVAL);
114 if (inlink->h > (INT_MAX - total_margin_h) / tile->h) {
115 av_log(ctx, AV_LOG_ERROR, "Total height %ux%u is too much.\n",
117 return AVERROR(EINVAL);
119 outlink->w = tile->w * inlink->w + total_margin_w;
120 outlink->h = tile->h * inlink->h + total_margin_h;
121 outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
122 outlink->frame_rate = av_mul_q(inlink->frame_rate,
123 av_make_q(1, tile->nb_frames));
124 ff_draw_init(&tile->draw, inlink->format, 0);
125 ff_draw_color(&tile->draw, &tile->blank, tile->rgba_color);
130 static void get_current_tile_pos(AVFilterContext *ctx, unsigned *x, unsigned *y)
132 TileContext *tile = ctx->priv;
133 AVFilterLink *inlink = ctx->inputs[0];
134 const unsigned tx = tile->current % tile->w;
135 const unsigned ty = tile->current / tile->w;
137 *x = tile->margin + (inlink->w + tile->padding) * tx;
138 *y = tile->margin + (inlink->h + tile->padding) * ty;
141 static void draw_blank_frame(AVFilterContext *ctx, AVFrame *out_buf)
143 TileContext *tile = ctx->priv;
144 AVFilterLink *inlink = ctx->inputs[0];
147 get_current_tile_pos(ctx, &x0, &y0);
148 ff_fill_rectangle(&tile->draw, &tile->blank,
149 out_buf->data, out_buf->linesize,
150 x0, y0, inlink->w, inlink->h);
154 static int end_last_frame(AVFilterContext *ctx)
156 TileContext *tile = ctx->priv;
157 AVFilterLink *outlink = ctx->outputs[0];
158 AVFrame *out_buf = tile->out_ref;
161 while (tile->current < tile->nb_frames)
162 draw_blank_frame(ctx, out_buf);
163 ret = ff_filter_frame(outlink, out_buf);
168 /* Note: direct rendering is not possible since there is no guarantee that
169 * buffers are fed to filter_frame in the order they were obtained from
170 * get_buffer (think B-frames). */
172 static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
174 AVFilterContext *ctx = inlink->dst;
175 TileContext *tile = ctx->priv;
176 AVFilterLink *outlink = ctx->outputs[0];
179 if (!tile->current) {
180 tile->out_ref = ff_get_video_buffer(outlink, outlink->w, outlink->h);
181 if (!tile->out_ref) {
182 av_frame_free(&picref);
183 return AVERROR(ENOMEM);
185 av_frame_copy_props(tile->out_ref, picref);
186 tile->out_ref->width = outlink->w;
187 tile->out_ref->height = outlink->h;
189 /* fill surface once for margin/padding */
190 if (tile->margin || tile->padding)
191 ff_fill_rectangle(&tile->draw, &tile->blank,
193 tile->out_ref->linesize,
194 0, 0, outlink->w, outlink->h);
197 get_current_tile_pos(ctx, &x0, &y0);
198 ff_copy_rectangle2(&tile->draw,
199 tile->out_ref->data, tile->out_ref->linesize,
200 picref->data, picref->linesize,
201 x0, y0, 0, 0, inlink->w, inlink->h);
203 av_frame_free(&picref);
204 if (++tile->current == tile->nb_frames)
205 return end_last_frame(ctx);
210 static int request_frame(AVFilterLink *outlink)
212 AVFilterContext *ctx = outlink->src;
213 TileContext *tile = ctx->priv;
214 AVFilterLink *inlink = ctx->inputs[0];
217 r = ff_request_frame(inlink);
218 if (r == AVERROR_EOF && tile->current)
219 r = end_last_frame(ctx);
223 static const AVFilterPad tile_inputs[] = {
226 .type = AVMEDIA_TYPE_VIDEO,
227 .filter_frame = filter_frame,
232 static const AVFilterPad tile_outputs[] = {
235 .type = AVMEDIA_TYPE_VIDEO,
236 .config_props = config_props,
237 .request_frame = request_frame,
242 AVFilter ff_vf_tile = {
244 .description = NULL_IF_CONFIG_SMALL("Tile several successive frames together."),
246 .query_formats = query_formats,
247 .priv_size = sizeof(TileContext),
248 .inputs = tile_inputs,
249 .outputs = tile_outputs,
250 .priv_class = &tile_class,