]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_tile.c
Merge commit '7ebe7e8e7a76c0ce302f4f583ef0d14220031214'
[ffmpeg] / libavfilter / vf_tile.c
1 /*
2  * Copyright (c) 2012 Nicolas George
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 /**
22  * @file
23  * tile video filter
24  */
25
26 #include "libavutil/imgutils.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/pixdesc.h"
29 #include "avfilter.h"
30 #include "drawutils.h"
31 #include "formats.h"
32 #include "video.h"
33 #include "internal.h"
34
35 typedef struct TileContext {
36     const AVClass *class;
37     unsigned w, h;
38     unsigned margin;
39     unsigned padding;
40     unsigned current;
41     unsigned nb_frames;
42     FFDrawContext draw;
43     FFDrawColor blank;
44     AVFrame *out_ref;
45     uint8_t rgba_color[4];
46 } TileContext;
47
48 #define OFFSET(x) offsetof(TileContext, x)
49 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
50
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 },
61     { NULL }
62 };
63
64 AVFILTER_DEFINE_CLASS(tile);
65
66 static av_cold int init(AVFilterContext *ctx)
67 {
68     TileContext *tile = ctx->priv;
69
70     if (tile->w > UINT_MAX / tile->h) {
71         av_log(ctx, AV_LOG_ERROR, "Tile size %ux%u is insane.\n",
72                tile->w, tile->h);
73         return AVERROR(EINVAL);
74     }
75
76     if (tile->padding) {
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);
82         }
83     }
84
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);
91     }
92
93     return 0;
94 }
95
96 static int query_formats(AVFilterContext *ctx)
97 {
98     return ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
99 }
100
101 static int config_props(AVFilterLink *outlink)
102 {
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;
108
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",
111                tile->w, inlink->w);
112         return AVERROR(EINVAL);
113     }
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",
116                tile->h, inlink->h);
117         return AVERROR(EINVAL);
118     }
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);
126
127     return 0;
128 }
129
130 static void get_current_tile_pos(AVFilterContext *ctx, unsigned *x, unsigned *y)
131 {
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;
136
137     *x = tile->margin + (inlink->w + tile->padding) * tx;
138     *y = tile->margin + (inlink->h + tile->padding) * ty;
139 }
140
141 static void draw_blank_frame(AVFilterContext *ctx, AVFrame *out_buf)
142 {
143     TileContext *tile    = ctx->priv;
144     AVFilterLink *inlink = ctx->inputs[0];
145     unsigned x0, y0;
146
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);
151     tile->current++;
152 }
153
154 static int end_last_frame(AVFilterContext *ctx)
155 {
156     TileContext *tile     = ctx->priv;
157     AVFilterLink *outlink = ctx->outputs[0];
158     AVFrame *out_buf = tile->out_ref;
159     int ret;
160
161     while (tile->current < tile->nb_frames)
162         draw_blank_frame(ctx, out_buf);
163     ret = ff_filter_frame(outlink, out_buf);
164     tile->current = 0;
165     return ret;
166 }
167
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). */
171
172 static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
173 {
174     AVFilterContext *ctx  = inlink->dst;
175     TileContext *tile     = ctx->priv;
176     AVFilterLink *outlink = ctx->outputs[0];
177     unsigned x0, y0;
178
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);
184         }
185         av_frame_copy_props(tile->out_ref, picref);
186         tile->out_ref->width  = outlink->w;
187         tile->out_ref->height = outlink->h;
188
189         /* fill surface once for margin/padding */
190         if (tile->margin || tile->padding)
191             ff_fill_rectangle(&tile->draw, &tile->blank,
192                               tile->out_ref->data,
193                               tile->out_ref->linesize,
194                               0, 0, outlink->w, outlink->h);
195     }
196
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);
202
203     av_frame_free(&picref);
204     if (++tile->current == tile->nb_frames)
205         return end_last_frame(ctx);
206
207     return 0;
208 }
209
210 static int request_frame(AVFilterLink *outlink)
211 {
212     AVFilterContext *ctx = outlink->src;
213     TileContext *tile    = ctx->priv;
214     AVFilterLink *inlink = ctx->inputs[0];
215     int r;
216
217     r = ff_request_frame(inlink);
218     if (r == AVERROR_EOF && tile->current)
219         r = end_last_frame(ctx);
220     return r;
221 }
222
223 static const AVFilterPad tile_inputs[] = {
224     {
225         .name         = "default",
226         .type         = AVMEDIA_TYPE_VIDEO,
227         .filter_frame = filter_frame,
228     },
229     { NULL }
230 };
231
232 static const AVFilterPad tile_outputs[] = {
233     {
234         .name          = "default",
235         .type          = AVMEDIA_TYPE_VIDEO,
236         .config_props  = config_props,
237         .request_frame = request_frame,
238     },
239     { NULL }
240 };
241
242 AVFilter ff_vf_tile = {
243     .name          = "tile",
244     .description   = NULL_IF_CONFIG_SMALL("Tile several successive frames together."),
245     .init          = init,
246     .query_formats = query_formats,
247     .priv_size     = sizeof(TileContext),
248     .inputs        = tile_inputs,
249     .outputs       = tile_outputs,
250     .priv_class    = &tile_class,
251 };