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;
112 return ff_start_frame(outlink, outlink->out_buf);
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)
134 TileContext *tile = ctx->priv;
135 AVFilterLink *inlink = ctx->inputs[0];
136 AVFilterLink *outlink = ctx->outputs[0];
137 unsigned x0 = inlink->w * (tile->current % tile->w);
138 unsigned y0 = inlink->h * (tile->current / tile->w);
140 ff_fill_rectangle(&tile->draw, &tile->blank,
141 outlink->out_buf->data, outlink->out_buf->linesize,
142 x0, y0, inlink->w, inlink->h);
145 static void end_last_frame(AVFilterContext *ctx)
147 TileContext *tile = ctx->priv;
148 AVFilterLink *outlink = ctx->outputs[0];
150 while (tile->current < tile->w * tile->h)
151 draw_blank_frame(ctx);
152 ff_draw_slice(outlink, 0, outlink->out_buf->video->h, 1);
153 ff_end_frame(outlink);
157 static int end_frame(AVFilterLink *inlink)
159 AVFilterContext *ctx = inlink->dst;
160 TileContext *tile = ctx->priv;
162 avfilter_unref_bufferp(&inlink->cur_buf);
163 if (++tile->current == tile->w * tile->h)
168 static int request_frame(AVFilterLink *outlink)
170 AVFilterContext *ctx = outlink->src;
171 TileContext *tile = ctx->priv;
172 AVFilterLink *inlink = ctx->inputs[0];
176 r = ff_request_frame(inlink);
178 if (r == AVERROR_EOF && tile->current)
184 if (!tile->current) /* done */
191 AVFilter avfilter_vf_tile = {
193 .description = NULL_IF_CONFIG_SMALL("Tile several successive frames together."),
195 .query_formats = query_formats,
196 .priv_size = sizeof(TileContext),
197 .inputs = (const AVFilterPad[]) {
199 .type = AVMEDIA_TYPE_VIDEO,
200 .start_frame = start_frame,
201 .draw_slice = draw_slice,
202 .end_frame = end_frame,
203 .min_perms = AV_PERM_READ, },
206 .outputs = (const AVFilterPad[]) {
208 .type = AVMEDIA_TYPE_VIDEO,
209 .config_props = config_props,
210 .request_frame = request_frame },