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"
37 #define REASONABLE_SIZE 1024
39 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
41 TileContext *tile = ctx->priv;
47 r = sscanf(args, "%ux%u%c", &tile->w, &tile->h, &dummy);
48 if (r != 2 || !tile->w || !tile->h)
49 return AVERROR(EINVAL);
50 if (tile->w > REASONABLE_SIZE || tile->h > REASONABLE_SIZE) {
51 av_log(ctx, AV_LOG_ERROR, "Tile size %ux%u is insane.\n",
53 return AVERROR(EINVAL);
58 static int query_formats(AVFilterContext *ctx)
60 avfilter_set_common_pixel_formats(ctx, ff_draw_supported_pixel_formats(0));
64 static int config_props(AVFilterLink *outlink)
66 AVFilterContext *ctx = outlink->src;
67 TileContext *tile = ctx->priv;
68 AVFilterLink *inlink = ctx->inputs[0];
70 if (inlink->w > INT_MAX / tile->w) {
71 av_log(ctx, AV_LOG_ERROR, "Total width %ux%u is too much.\n",
73 return AVERROR(EINVAL);
75 if (inlink->h > INT_MAX / tile->h) {
76 av_log(ctx, AV_LOG_ERROR, "Total height %ux%u is too much.\n",
78 return AVERROR(EINVAL);
80 outlink->w = tile->w * inlink->w;
81 outlink->h = tile->h * inlink->h;
82 outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
83 ff_draw_init(&tile->draw, inlink->format, 0);
84 /* TODO make the color an option, or find an unified way of choosing it */
85 ff_draw_color(&tile->draw, &tile->blank, (uint8_t[]){ 0, 0, 0, -1 });
90 /* Note: direct rendering is not possible since there is no guarantee that
91 * buffers are fed to start_frame in the order they were obtained from
92 * get_buffer (think B-frames). */
94 static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
96 AVFilterContext *ctx = inlink->dst;
97 TileContext *tile = ctx->priv;
98 AVFilterLink *outlink = ctx->outputs[0];
102 outlink->out_buf = avfilter_get_video_buffer(outlink, AV_PERM_WRITE,
103 outlink->w, outlink->h);
104 avfilter_copy_buffer_ref_props(outlink->out_buf, picref);
105 outlink->out_buf->video->w = outlink->w;
106 outlink->out_buf->video->h = outlink->h;
107 avfilter_start_frame(outlink, outlink->out_buf);
110 static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
112 AVFilterContext *ctx = inlink->dst;
113 TileContext *tile = ctx->priv;
114 AVFilterLink *outlink = ctx->outputs[0];
115 unsigned x0 = inlink->w * (tile->current % tile->w);
116 unsigned y0 = inlink->h * (tile->current / tile->w);
118 ff_copy_rectangle2(&tile->draw,
119 outlink->out_buf->data, outlink->out_buf->linesize,
120 inlink ->cur_buf->data, inlink ->cur_buf->linesize,
121 x0, y0 + y, 0, y, inlink->cur_buf->video->w, h);
122 /* TODO if tile->w == 1 && slice_dir is always 1, we could draw_slice
126 static void draw_blank_frame(AVFilterContext *ctx)
128 TileContext *tile = ctx->priv;
129 AVFilterLink *inlink = ctx->inputs[0];
130 AVFilterLink *outlink = ctx->outputs[0];
131 unsigned x0 = inlink->w * (tile->current % tile->w);
132 unsigned y0 = inlink->h * (tile->current / tile->w);
134 ff_fill_rectangle(&tile->draw, &tile->blank,
135 outlink->out_buf->data, outlink->out_buf->linesize,
136 x0, y0, inlink->w, inlink->h);
139 static void end_last_frame(AVFilterContext *ctx)
141 TileContext *tile = ctx->priv;
142 AVFilterLink *outlink = ctx->outputs[0];
144 while (tile->current < tile->w * tile->h)
145 draw_blank_frame(ctx);
146 avfilter_draw_slice(outlink, 0, outlink->out_buf->video->h, 1);
147 avfilter_end_frame(outlink);
151 static void end_frame(AVFilterLink *inlink)
153 AVFilterContext *ctx = inlink->dst;
154 TileContext *tile = ctx->priv;
156 avfilter_unref_buffer(inlink->cur_buf);
157 if (++tile->current == tile->w * tile->h)
161 static int request_frame(AVFilterLink *outlink)
163 AVFilterContext *ctx = outlink->src;
164 TileContext *tile = ctx->priv;
165 AVFilterLink *inlink = ctx->inputs[0];
169 r = avfilter_request_frame(inlink);
171 if (r == AVERROR_EOF && tile->current)
177 if (!tile->current) /* done */
184 AVFilter avfilter_vf_tile = {
186 .description = NULL_IF_CONFIG_SMALL("Tile several successive frames together."),
188 .query_formats = query_formats,
189 .priv_size = sizeof(TileContext),
190 .inputs = (const AVFilterPad[]) {
192 .type = AVMEDIA_TYPE_VIDEO,
193 .start_frame = start_frame,
194 .draw_slice = draw_slice,
195 .end_frame = end_frame,
196 .min_perms = AV_PERM_READ, },
199 .outputs = (const AVFilterPad[]) {
201 .type = AVMEDIA_TYPE_VIDEO,
202 .config_props = config_props,
203 .request_frame = request_frame },