]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_tile.c
lavf/http: Remove superfluous parenthesis.
[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     AVFrame *out_ref;
44     uint8_t rgba_color[4];
45 } TileContext;
46
47 #define REASONABLE_SIZE 1024
48
49 #define OFFSET(x) offsetof(TileContext, x)
50 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
51
52 static const AVOption tile_options[] = {
53     { "layout", "set grid size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE,
54         {.str = "6x5"}, 0, 0, FLAGS },
55     { "nb_frames", "set maximum number of frame to render", OFFSET(nb_frames),
56         AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS },
57     { "margin",  "set outer border margin in pixels",    OFFSET(margin),
58         AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1024, FLAGS },
59     { "padding", "set inner border thickness in pixels", OFFSET(padding),
60         AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1024, FLAGS },
61     { "color",   "set the color of the unused area", OFFSET(rgba_color), AV_OPT_TYPE_COLOR, {.str = "black"}, .flags = FLAGS },
62     { NULL }
63 };
64
65 AVFILTER_DEFINE_CLASS(tile);
66
67 static av_cold int init(AVFilterContext *ctx)
68 {
69     TileContext *tile = ctx->priv;
70
71     if (tile->w > REASONABLE_SIZE || tile->h > REASONABLE_SIZE) {
72         av_log(ctx, AV_LOG_ERROR, "Tile size %ux%u is insane.\n",
73                tile->w, tile->h);
74         return AVERROR(EINVAL);
75     }
76
77     if (tile->nb_frames == 0) {
78         tile->nb_frames = tile->w * tile->h;
79     } else if (tile->nb_frames > tile->w * tile->h) {
80         av_log(ctx, AV_LOG_ERROR, "nb_frames must be less than or equal to %dx%d=%d\n",
81                tile->w, tile->h, tile->w * tile->h);
82         return AVERROR(EINVAL);
83     }
84
85     return 0;
86 }
87
88 static int query_formats(AVFilterContext *ctx)
89 {
90     return ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
91 }
92
93 static int config_props(AVFilterLink *outlink)
94 {
95     AVFilterContext *ctx = outlink->src;
96     TileContext *tile    = ctx->priv;
97     AVFilterLink *inlink = ctx->inputs[0];
98     const unsigned total_margin_w = (tile->w - 1) * tile->padding + 2*tile->margin;
99     const unsigned total_margin_h = (tile->h - 1) * tile->padding + 2*tile->margin;
100
101     if (inlink->w > (INT_MAX - total_margin_w) / tile->w) {
102         av_log(ctx, AV_LOG_ERROR, "Total width %ux%u is too much.\n",
103                tile->w, inlink->w);
104         return AVERROR(EINVAL);
105     }
106     if (inlink->h > (INT_MAX - total_margin_h) / tile->h) {
107         av_log(ctx, AV_LOG_ERROR, "Total height %ux%u is too much.\n",
108                tile->h, inlink->h);
109         return AVERROR(EINVAL);
110     }
111     outlink->w = tile->w * inlink->w + total_margin_w;
112     outlink->h = tile->h * inlink->h + total_margin_h;
113     outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
114     outlink->frame_rate = av_mul_q(inlink->frame_rate,
115                                    av_make_q(1, tile->nb_frames));
116     ff_draw_init(&tile->draw, inlink->format, 0);
117     ff_draw_color(&tile->draw, &tile->blank, tile->rgba_color);
118
119     outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
120
121     return 0;
122 }
123
124 static void get_current_tile_pos(AVFilterContext *ctx, unsigned *x, unsigned *y)
125 {
126     TileContext *tile    = ctx->priv;
127     AVFilterLink *inlink = ctx->inputs[0];
128     const unsigned tx = tile->current % tile->w;
129     const unsigned ty = tile->current / tile->w;
130
131     *x = tile->margin + (inlink->w + tile->padding) * tx;
132     *y = tile->margin + (inlink->h + tile->padding) * ty;
133 }
134
135 static void draw_blank_frame(AVFilterContext *ctx, AVFrame *out_buf)
136 {
137     TileContext *tile    = ctx->priv;
138     AVFilterLink *inlink = ctx->inputs[0];
139     unsigned x0, y0;
140
141     get_current_tile_pos(ctx, &x0, &y0);
142     ff_fill_rectangle(&tile->draw, &tile->blank,
143                       out_buf->data, out_buf->linesize,
144                       x0, y0, inlink->w, inlink->h);
145     tile->current++;
146 }
147 static int end_last_frame(AVFilterContext *ctx)
148 {
149     TileContext *tile     = ctx->priv;
150     AVFilterLink *outlink = ctx->outputs[0];
151     AVFrame *out_buf = tile->out_ref;
152     int ret;
153
154     while (tile->current < tile->nb_frames)
155         draw_blank_frame(ctx, out_buf);
156     ret = ff_filter_frame(outlink, out_buf);
157     tile->current = 0;
158     return ret;
159 }
160
161 /* Note: direct rendering is not possible since there is no guarantee that
162  * buffers are fed to filter_frame in the order they were obtained from
163  * get_buffer (think B-frames). */
164
165 static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
166 {
167     AVFilterContext *ctx  = inlink->dst;
168     TileContext *tile     = ctx->priv;
169     AVFilterLink *outlink = ctx->outputs[0];
170     unsigned x0, y0;
171
172     if (!tile->current) {
173         tile->out_ref = ff_get_video_buffer(outlink, outlink->w, outlink->h);
174         if (!tile->out_ref) {
175             av_frame_free(&picref);
176             return AVERROR(ENOMEM);
177         }
178         av_frame_copy_props(tile->out_ref, picref);
179         tile->out_ref->width  = outlink->w;
180         tile->out_ref->height = outlink->h;
181
182         /* fill surface once for margin/padding */
183         if (tile->margin || tile->padding)
184             ff_fill_rectangle(&tile->draw, &tile->blank,
185                               tile->out_ref->data,
186                               tile->out_ref->linesize,
187                               0, 0, outlink->w, outlink->h);
188     }
189
190     get_current_tile_pos(ctx, &x0, &y0);
191     ff_copy_rectangle2(&tile->draw,
192                        tile->out_ref->data, tile->out_ref->linesize,
193                        picref->data, picref->linesize,
194                        x0, y0, 0, 0, inlink->w, inlink->h);
195
196     av_frame_free(&picref);
197     if (++tile->current == tile->nb_frames)
198         return end_last_frame(ctx);
199
200     return 0;
201 }
202
203 static int request_frame(AVFilterLink *outlink)
204 {
205     AVFilterContext *ctx = outlink->src;
206     TileContext *tile    = ctx->priv;
207     AVFilterLink *inlink = ctx->inputs[0];
208     int r;
209
210     r = ff_request_frame(inlink);
211     if (r == AVERROR_EOF && tile->current)
212         r = end_last_frame(ctx);
213     return r;
214 }
215
216 static const AVFilterPad tile_inputs[] = {
217     {
218         .name         = "default",
219         .type         = AVMEDIA_TYPE_VIDEO,
220         .filter_frame = filter_frame,
221     },
222     { NULL }
223 };
224
225 static const AVFilterPad tile_outputs[] = {
226     {
227         .name          = "default",
228         .type          = AVMEDIA_TYPE_VIDEO,
229         .config_props  = config_props,
230         .request_frame = request_frame,
231     },
232     { NULL }
233 };
234
235 AVFilter ff_vf_tile = {
236     .name          = "tile",
237     .description   = NULL_IF_CONFIG_SMALL("Tile several successive frames together."),
238     .init          = init,
239     .query_formats = query_formats,
240     .priv_size     = sizeof(TileContext),
241     .inputs        = tile_inputs,
242     .outputs       = tile_outputs,
243     .priv_class    = &tile_class,
244 };