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/opt.h"
27 #include "libavutil/pixdesc.h"
29 #include "drawutils.h"
45 #define REASONABLE_SIZE 1024
47 #define OFFSET(x) offsetof(TileContext, x)
48 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
50 static const AVOption tile_options[] = {
51 { "layout", "set grid size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE,
52 {.str = "6x5"}, 0, 0, FLAGS },
53 { "margin", "set outer border margin in pixels", OFFSET(margin),
54 AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1024, FLAGS },
55 { "padding", "set inner border thickness in pixels", OFFSET(padding),
56 AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1024, 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 },
62 AVFILTER_DEFINE_CLASS(tile);
64 static av_cold int init(AVFilterContext *ctx, const char *args)
66 TileContext *tile = ctx->priv;
67 static const char *shorthand[] = { "layout", "nb_frames", "margin", "padding", NULL };
70 tile->class = &tile_class;
71 av_opt_set_defaults(tile);
73 if ((ret = av_opt_set_from_string(tile, args, shorthand, "=", ":")) < 0)
76 if (tile->w > REASONABLE_SIZE || tile->h > REASONABLE_SIZE) {
77 av_log(ctx, AV_LOG_ERROR, "Tile size %ux%u is insane.\n",
79 return AVERROR(EINVAL);
82 if (tile->nb_frames == 0) {
83 tile->nb_frames = tile->w * tile->h;
84 } else if (tile->nb_frames > tile->w * tile->h) {
85 av_log(ctx, AV_LOG_ERROR, "nb_frames must be less than or equal to %dx%d=%d\n",
86 tile->w, tile->h, tile->w * tile->h);
87 return AVERROR(EINVAL);
93 static int query_formats(AVFilterContext *ctx)
95 ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
99 static int config_props(AVFilterLink *outlink)
101 AVFilterContext *ctx = outlink->src;
102 TileContext *tile = ctx->priv;
103 AVFilterLink *inlink = ctx->inputs[0];
104 const unsigned total_margin_w = (tile->w - 1) * tile->padding + 2*tile->margin;
105 const unsigned total_margin_h = (tile->h - 1) * tile->padding + 2*tile->margin;
107 if (inlink->w > (INT_MAX - total_margin_w) / tile->w) {
108 av_log(ctx, AV_LOG_ERROR, "Total width %ux%u is too much.\n",
110 return AVERROR(EINVAL);
112 if (inlink->h > (INT_MAX - total_margin_h) / tile->h) {
113 av_log(ctx, AV_LOG_ERROR, "Total height %ux%u is too much.\n",
115 return AVERROR(EINVAL);
117 outlink->w = tile->w * inlink->w + total_margin_w;
118 outlink->h = tile->h * inlink->h + total_margin_h;
119 outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
120 outlink->frame_rate = av_mul_q(inlink->frame_rate,
121 (AVRational){ 1, tile->nb_frames });
122 ff_draw_init(&tile->draw, inlink->format, 0);
123 /* TODO make the color an option, or find an unified way of choosing it */
124 ff_draw_color(&tile->draw, &tile->blank, (uint8_t[]){ 0, 0, 0, -1 });
129 /* Note: direct rendering is not possible since there is no guarantee that
130 * buffers are fed to start_frame in the order they were obtained from
131 * get_buffer (think B-frames). */
133 static int start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
135 AVFilterContext *ctx = inlink->dst;
136 TileContext *tile = ctx->priv;
137 AVFilterLink *outlink = ctx->outputs[0];
141 outlink->out_buf = ff_get_video_buffer(outlink, AV_PERM_WRITE,
142 outlink->w, outlink->h);
143 avfilter_copy_buffer_ref_props(outlink->out_buf, picref);
144 outlink->out_buf->video->w = outlink->w;
145 outlink->out_buf->video->h = outlink->h;
147 /* fill surface once for margin/padding */
148 if (tile->margin || tile->padding)
149 ff_fill_rectangle(&tile->draw, &tile->blank,
150 outlink->out_buf->data, outlink->out_buf->linesize,
151 0, 0, outlink->w, outlink->h);
155 static void get_current_tile_pos(AVFilterContext *ctx, unsigned *x, unsigned *y)
157 TileContext *tile = ctx->priv;
158 AVFilterLink *inlink = ctx->inputs[0];
159 const unsigned tx = tile->current % tile->w;
160 const unsigned ty = tile->current / tile->w;
162 *x = tile->margin + (inlink->w + tile->padding) * tx;
163 *y = tile->margin + (inlink->h + tile->padding) * ty;
166 static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
168 AVFilterContext *ctx = inlink->dst;
169 TileContext *tile = ctx->priv;
170 AVFilterLink *outlink = ctx->outputs[0];
173 get_current_tile_pos(ctx, &x0, &y0);
174 ff_copy_rectangle2(&tile->draw,
175 outlink->out_buf->data, outlink->out_buf->linesize,
176 inlink ->cur_buf->data, inlink ->cur_buf->linesize,
177 x0, y0 + y, 0, y, inlink->cur_buf->video->w, h);
178 /* TODO if tile->w == 1 && slice_dir is always 1, we could draw_slice
183 static void draw_blank_frame(AVFilterContext *ctx, AVFilterBufferRef *out_buf)
185 TileContext *tile = ctx->priv;
186 AVFilterLink *inlink = ctx->inputs[0];
189 get_current_tile_pos(ctx, &x0, &y0);
190 ff_fill_rectangle(&tile->draw, &tile->blank,
191 out_buf->data, out_buf->linesize,
192 x0, y0, inlink->w, inlink->h);
195 static void end_last_frame(AVFilterContext *ctx)
197 TileContext *tile = ctx->priv;
198 AVFilterLink *outlink = ctx->outputs[0];
199 AVFilterBufferRef *out_buf = outlink->out_buf;
201 outlink->out_buf = NULL;
202 ff_start_frame(outlink, out_buf);
203 while (tile->current < tile->nb_frames)
204 draw_blank_frame(ctx, out_buf);
205 ff_draw_slice(outlink, 0, out_buf->video->h, 1);
206 ff_end_frame(outlink);
210 static int end_frame(AVFilterLink *inlink)
212 AVFilterContext *ctx = inlink->dst;
213 TileContext *tile = ctx->priv;
215 avfilter_unref_bufferp(&inlink->cur_buf);
216 if (++tile->current == tile->nb_frames)
221 static int request_frame(AVFilterLink *outlink)
223 AVFilterContext *ctx = outlink->src;
224 TileContext *tile = ctx->priv;
225 AVFilterLink *inlink = ctx->inputs[0];
229 r = ff_request_frame(inlink);
231 if (r == AVERROR_EOF && tile->current)
237 if (!tile->current) /* done */
244 AVFilter avfilter_vf_tile = {
246 .description = NULL_IF_CONFIG_SMALL("Tile several successive frames together."),
248 .query_formats = query_formats,
249 .priv_size = sizeof(TileContext),
250 .inputs = (const AVFilterPad[]) {
252 .type = AVMEDIA_TYPE_VIDEO,
253 .start_frame = start_frame,
254 .draw_slice = draw_slice,
255 .end_frame = end_frame,
256 .min_perms = AV_PERM_READ, },
259 .outputs = (const AVFilterPad[]) {
261 .type = AVMEDIA_TYPE_VIDEO,
262 .config_props = config_props,
263 .request_frame = request_frame },
266 .priv_class = &tile_class,