]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_tile.c
Merge remote-tracking branch 'qatar/master'
[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/opt.h"
27 #include "libavutil/pixdesc.h"
28 #include "avfilter.h"
29 #include "drawutils.h"
30 #include "formats.h"
31 #include "video.h"
32 #include "internal.h"
33
34 typedef struct {
35     const AVClass *class;
36     unsigned w, h;
37     unsigned margin;
38     unsigned padding;
39     unsigned current;
40     unsigned nb_frames;
41     FFDrawContext draw;
42     FFDrawColor blank;
43 } TileContext;
44
45 #define REASONABLE_SIZE 1024
46
47 #define OFFSET(x) offsetof(TileContext, x)
48 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
49
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 },
59     {NULL},
60 };
61
62 AVFILTER_DEFINE_CLASS(tile);
63
64 static av_cold int init(AVFilterContext *ctx, const char *args)
65 {
66     TileContext *tile = ctx->priv;
67     static const char *shorthand[] = { "layout", "nb_frames", "margin", "padding", NULL };
68     int ret;
69
70     tile->class = &tile_class;
71     av_opt_set_defaults(tile);
72
73     if ((ret = av_opt_set_from_string(tile, args, shorthand, "=", ":")) < 0)
74         return ret;
75
76     if (tile->w > REASONABLE_SIZE || tile->h > REASONABLE_SIZE) {
77         av_log(ctx, AV_LOG_ERROR, "Tile size %ux%u is insane.\n",
78                tile->w, tile->h);
79         return AVERROR(EINVAL);
80     }
81
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);
88     }
89
90     return 0;
91 }
92
93 static int query_formats(AVFilterContext *ctx)
94 {
95     ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
96     return 0;
97 }
98
99 static int config_props(AVFilterLink *outlink)
100 {
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;
106
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",
109                tile->w, inlink->w);
110         return AVERROR(EINVAL);
111     }
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",
114                tile->h, inlink->h);
115         return AVERROR(EINVAL);
116     }
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 });
125
126     return 0;
127 }
128
129 static void get_current_tile_pos(AVFilterContext *ctx, unsigned *x, unsigned *y)
130 {
131     TileContext *tile    = ctx->priv;
132     AVFilterLink *inlink = ctx->inputs[0];
133     const unsigned tx = tile->current % tile->w;
134     const unsigned ty = tile->current / tile->w;
135
136     *x = tile->margin + (inlink->w + tile->padding) * tx;
137     *y = tile->margin + (inlink->h + tile->padding) * ty;
138 }
139
140 static void draw_blank_frame(AVFilterContext *ctx, AVFilterBufferRef *out_buf)
141 {
142     TileContext *tile    = ctx->priv;
143     AVFilterLink *inlink  = ctx->inputs[0];
144     unsigned x0, y0;
145
146     get_current_tile_pos(ctx, &x0, &y0);
147     ff_fill_rectangle(&tile->draw, &tile->blank,
148                       out_buf->data, out_buf->linesize,
149                       x0, y0, inlink->w, inlink->h);
150     tile->current++;
151 }
152 static int end_last_frame(AVFilterContext *ctx)
153 {
154     TileContext *tile    = ctx->priv;
155     AVFilterLink *outlink = ctx->outputs[0];
156     AVFilterBufferRef *out_buf = outlink->out_buf;
157     int ret;
158
159     outlink->out_buf = NULL;
160     while (tile->current < tile->nb_frames)
161         draw_blank_frame(ctx, out_buf);
162     ret = ff_filter_frame(outlink, out_buf);
163     tile->current = 0;
164     return ret;
165 }
166
167 /* Note: direct rendering is not possible since there is no guarantee that
168  * buffers are fed to filter_frame in the order they were obtained from
169  * get_buffer (think B-frames). */
170
171 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
172 {
173     AVFilterContext *ctx  = inlink->dst;
174     TileContext *tile    = ctx->priv;
175     AVFilterLink *outlink = ctx->outputs[0];
176     unsigned x0, y0;
177
178     if (!tile->current) {
179         outlink->out_buf = ff_get_video_buffer(outlink, AV_PERM_WRITE,
180                                                outlink->w, outlink->h);
181         if (!outlink->out_buf)
182             return AVERROR(ENOMEM);
183         avfilter_copy_buffer_ref_props(outlink->out_buf, picref);
184         outlink->out_buf->video->w = outlink->w;
185         outlink->out_buf->video->h = outlink->h;
186
187         /* fill surface once for margin/padding */
188         if (tile->margin || tile->padding)
189             ff_fill_rectangle(&tile->draw, &tile->blank,
190                               outlink->out_buf->data,
191                               outlink->out_buf->linesize,
192                               0, 0, outlink->w, outlink->h);
193     }
194
195     get_current_tile_pos(ctx, &x0, &y0);
196     ff_copy_rectangle2(&tile->draw,
197                        outlink->out_buf->data, outlink->out_buf->linesize,
198                        inlink ->cur_buf->data, inlink ->cur_buf->linesize,
199                        x0, y0, 0, 0, inlink->w, inlink->h);
200
201     avfilter_unref_bufferp(&inlink->cur_buf);
202     if (++tile->current == tile->nb_frames)
203         return end_last_frame(ctx);
204
205     return 0;
206 }
207
208 static int request_frame(AVFilterLink *outlink)
209 {
210     AVFilterContext *ctx = outlink->src;
211     TileContext *tile   = ctx->priv;
212     AVFilterLink *inlink = ctx->inputs[0];
213     int r;
214
215     while (1) {
216         r = ff_request_frame(inlink);
217         if (r < 0) {
218             if (r == AVERROR_EOF && tile->current)
219                 r = end_last_frame(ctx);
220             break;
221         }
222         if (!tile->current) /* done */
223             break;
224     }
225     return r;
226 }
227
228 static const AVFilterPad tile_inputs[] = {
229     {
230         .name         = "default",
231         .type         = AVMEDIA_TYPE_VIDEO,
232         .filter_frame = filter_frame,
233         .min_perms    = AV_PERM_READ,
234     },
235     { NULL }
236 };
237
238 static const AVFilterPad tile_outputs[] = {
239     {
240         .name          = "default",
241         .type          = AVMEDIA_TYPE_VIDEO,
242         .config_props  = config_props,
243         .request_frame = request_frame,
244     },
245     { NULL }
246 };
247
248 AVFilter avfilter_vf_tile = {
249     .name          = "tile",
250     .description   = NULL_IF_CONFIG_SMALL("Tile several successive frames together."),
251     .init          = init,
252     .query_formats = query_formats,
253     .priv_size     = sizeof(TileContext),
254     .inputs        = tile_inputs,
255     .outputs       = tile_outputs,
256     .priv_class    = &tile_class,
257 };