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/pixdesc.h"
28 #include "drawutils.h"
40 #define REASONABLE_SIZE 1024
42 static av_cold int init(AVFilterContext *ctx, const char *args)
44 TileContext *tile = ctx->priv;
50 r = sscanf(args, "%ux%u%c", &tile->w, &tile->h, &dummy);
51 if (r != 2 || !tile->w || !tile->h)
52 return AVERROR(EINVAL);
53 if (tile->w > REASONABLE_SIZE || tile->h > REASONABLE_SIZE) {
54 av_log(ctx, AV_LOG_ERROR, "Tile size %ux%u is insane.\n",
56 return AVERROR(EINVAL);
61 static int query_formats(AVFilterContext *ctx)
63 ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
67 static int config_props(AVFilterLink *outlink)
69 AVFilterContext *ctx = outlink->src;
70 TileContext *tile = ctx->priv;
71 AVFilterLink *inlink = ctx->inputs[0];
73 if (inlink->w > INT_MAX / tile->w) {
74 av_log(ctx, AV_LOG_ERROR, "Total width %ux%u is too much.\n",
76 return AVERROR(EINVAL);
78 if (inlink->h > INT_MAX / tile->h) {
79 av_log(ctx, AV_LOG_ERROR, "Total height %ux%u is too much.\n",
81 return AVERROR(EINVAL);
83 outlink->w = tile->w * inlink->w;
84 outlink->h = tile->h * inlink->h;
85 outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
86 outlink->frame_rate = av_mul_q(inlink->frame_rate,
87 (AVRational){ 1, tile->w * tile->h });
88 ff_draw_init(&tile->draw, inlink->format, 0);
89 /* TODO make the color an option, or find an unified way of choosing it */
90 ff_draw_color(&tile->draw, &tile->blank, (uint8_t[]){ 0, 0, 0, -1 });
95 /* Note: direct rendering is not possible since there is no guarantee that
96 * buffers are fed to start_frame in the order they were obtained from
97 * get_buffer (think B-frames). */
99 static int start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
101 AVFilterContext *ctx = inlink->dst;
102 TileContext *tile = ctx->priv;
103 AVFilterLink *outlink = ctx->outputs[0];
107 outlink->out_buf = ff_get_video_buffer(outlink, AV_PERM_WRITE,
108 outlink->w, outlink->h);
109 avfilter_copy_buffer_ref_props(outlink->out_buf, picref);
110 outlink->out_buf->video->w = outlink->w;
111 outlink->out_buf->video->h = outlink->h;
115 static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
117 AVFilterContext *ctx = inlink->dst;
118 TileContext *tile = ctx->priv;
119 AVFilterLink *outlink = ctx->outputs[0];
120 unsigned x0 = inlink->w * (tile->current % tile->w);
121 unsigned y0 = inlink->h * (tile->current / tile->w);
123 ff_copy_rectangle2(&tile->draw,
124 outlink->out_buf->data, outlink->out_buf->linesize,
125 inlink ->cur_buf->data, inlink ->cur_buf->linesize,
126 x0, y0 + y, 0, y, inlink->cur_buf->video->w, h);
127 /* TODO if tile->w == 1 && slice_dir is always 1, we could draw_slice
132 static void draw_blank_frame(AVFilterContext *ctx, AVFilterBufferRef *out_buf)
134 TileContext *tile = ctx->priv;
135 AVFilterLink *inlink = ctx->inputs[0];
136 unsigned x0 = inlink->w * (tile->current % tile->w);
137 unsigned y0 = inlink->h * (tile->current / tile->w);
139 ff_fill_rectangle(&tile->draw, &tile->blank,
140 out_buf->data, out_buf->linesize,
141 x0, y0, inlink->w, inlink->h);
144 static void end_last_frame(AVFilterContext *ctx)
146 TileContext *tile = ctx->priv;
147 AVFilterLink *outlink = ctx->outputs[0];
148 AVFilterBufferRef *out_buf = outlink->out_buf;
150 outlink->out_buf = NULL;
151 ff_start_frame(outlink, out_buf);
152 while (tile->current < tile->w * tile->h)
153 draw_blank_frame(ctx, out_buf);
154 ff_draw_slice(outlink, 0, out_buf->video->h, 1);
155 ff_end_frame(outlink);
159 static int end_frame(AVFilterLink *inlink)
161 AVFilterContext *ctx = inlink->dst;
162 TileContext *tile = ctx->priv;
164 avfilter_unref_bufferp(&inlink->cur_buf);
165 if (++tile->current == tile->w * tile->h)
170 static int request_frame(AVFilterLink *outlink)
172 AVFilterContext *ctx = outlink->src;
173 TileContext *tile = ctx->priv;
174 AVFilterLink *inlink = ctx->inputs[0];
178 r = ff_request_frame(inlink);
180 if (r == AVERROR_EOF && tile->current)
186 if (!tile->current) /* done */
193 AVFilter avfilter_vf_tile = {
195 .description = NULL_IF_CONFIG_SMALL("Tile several successive frames together."),
197 .query_formats = query_formats,
198 .priv_size = sizeof(TileContext),
199 .inputs = (const AVFilterPad[]) {
201 .type = AVMEDIA_TYPE_VIDEO,
202 .start_frame = start_frame,
203 .draw_slice = draw_slice,
204 .end_frame = end_frame,
205 .min_perms = AV_PERM_READ, },
208 .outputs = (const AVFilterPad[]) {
210 .type = AVMEDIA_TYPE_VIDEO,
211 .config_props = config_props,
212 .request_frame = request_frame },