]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_stack.c
avfilter/vf_xstack: set better error msg for
[ffmpeg] / libavfilter / vf_stack.c
1 /*
2  * Copyright (c) 2015 Paul B. Mahol
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 #include "libavutil/avstring.h"
22 #include "libavutil/imgutils.h"
23 #include "libavutil/opt.h"
24 #include "libavutil/pixdesc.h"
25
26 #include "avfilter.h"
27 #include "formats.h"
28 #include "internal.h"
29 #include "framesync.h"
30 #include "video.h"
31
32 typedef struct StackItem {
33     int x[4], y[4];
34     int linesize[4];
35     int height[4];
36 } StackItem;
37
38 typedef struct StackContext {
39     const AVClass *class;
40     const AVPixFmtDescriptor *desc;
41     int nb_inputs;
42     char *layout;
43     int shortest;
44     int is_vertical;
45     int is_horizontal;
46     int nb_planes;
47
48     StackItem *items;
49     AVFrame **frames;
50     FFFrameSync fs;
51 } StackContext;
52
53 static int query_formats(AVFilterContext *ctx)
54 {
55     AVFilterFormats *pix_fmts = NULL;
56     int fmt, ret;
57
58     for (fmt = 0; av_pix_fmt_desc_get(fmt); fmt++) {
59         const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
60         if (!(desc->flags & AV_PIX_FMT_FLAG_PAL ||
61               desc->flags & AV_PIX_FMT_FLAG_HWACCEL ||
62               desc->flags & AV_PIX_FMT_FLAG_BITSTREAM) &&
63             (ret = ff_add_format(&pix_fmts, fmt)) < 0)
64             return ret;
65     }
66
67     return ff_set_common_formats(ctx, pix_fmts);
68 }
69
70 static av_cold int init(AVFilterContext *ctx)
71 {
72     StackContext *s = ctx->priv;
73     int i, ret;
74
75     if (!strcmp(ctx->filter->name, "vstack"))
76         s->is_vertical = 1;
77
78     if (!strcmp(ctx->filter->name, "hstack"))
79         s->is_horizontal = 1;
80
81     s->frames = av_calloc(s->nb_inputs, sizeof(*s->frames));
82     if (!s->frames)
83         return AVERROR(ENOMEM);
84
85     if (!strcmp(ctx->filter->name, "xstack")) {
86         if (!s->layout) {
87             if (s->nb_inputs == 2)
88                 s->layout = "0_0|w0_0";
89             else {
90                 av_log(ctx, AV_LOG_ERROR, "No layout specified.\n");
91                 return AVERROR(EINVAL);
92             }
93         }
94
95         s->items = av_calloc(s->nb_inputs, sizeof(*s->items));
96         if (!s->items)
97             return AVERROR(ENOMEM);
98     }
99
100     for (i = 0; i < s->nb_inputs; i++) {
101         AVFilterPad pad = { 0 };
102
103         pad.type = AVMEDIA_TYPE_VIDEO;
104         pad.name = av_asprintf("input%d", i);
105         if (!pad.name)
106             return AVERROR(ENOMEM);
107
108         if ((ret = ff_insert_inpad(ctx, i, &pad)) < 0) {
109             av_freep(&pad.name);
110             return ret;
111         }
112     }
113
114     return 0;
115 }
116
117 static int process_frame(FFFrameSync *fs)
118 {
119     AVFilterContext *ctx = fs->parent;
120     AVFilterLink *outlink = ctx->outputs[0];
121     StackContext *s = fs->opaque;
122     AVFrame **in = s->frames;
123     AVFrame *out;
124     int i, p, ret, offset[4] = { 0 };
125
126     for (i = 0; i < s->nb_inputs; i++) {
127         if ((ret = ff_framesync_get_frame(&s->fs, i, &in[i], 0)) < 0)
128             return ret;
129     }
130
131     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
132     if (!out)
133         return AVERROR(ENOMEM);
134     out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
135     out->sample_aspect_ratio = outlink->sample_aspect_ratio;
136
137     for (i = 0; i < s->nb_inputs; i++) {
138         AVFilterLink *inlink = ctx->inputs[i];
139         int linesize[4];
140         int height[4];
141
142         if (s->is_horizontal || s->is_vertical) {
143             if ((ret = av_image_fill_linesizes(linesize, inlink->format, inlink->w)) < 0) {
144                 av_frame_free(&out);
145                 return ret;
146             }
147
148             height[1] = height[2] = AV_CEIL_RSHIFT(inlink->h, s->desc->log2_chroma_h);
149             height[0] = height[3] = inlink->h;
150         }
151
152         for (p = 0; p < s->nb_planes; p++) {
153             if (s->is_vertical) {
154                 av_image_copy_plane(out->data[p] + offset[p] * out->linesize[p],
155                                     out->linesize[p],
156                                     in[i]->data[p],
157                                     in[i]->linesize[p],
158                                     linesize[p], height[p]);
159                 offset[p] += height[p];
160             } else if (s->is_horizontal) {
161                 av_image_copy_plane(out->data[p] + offset[p],
162                                     out->linesize[p],
163                                     in[i]->data[p],
164                                     in[i]->linesize[p],
165                                     linesize[p], height[p]);
166                 offset[p] += linesize[p];
167             } else {
168                 StackItem *item = &s->items[i];
169
170                 av_image_copy_plane(out->data[p] + out->linesize[p] * item->y[p] + item->x[p],
171                                     out->linesize[p],
172                                     in[i]->data[p],
173                                     in[i]->linesize[p],
174                                     item->linesize[p], item->height[p]);
175             }
176         }
177     }
178
179     return ff_filter_frame(outlink, out);
180 }
181
182 static int config_output(AVFilterLink *outlink)
183 {
184     AVFilterContext *ctx = outlink->src;
185     StackContext *s = ctx->priv;
186     AVRational frame_rate = ctx->inputs[0]->frame_rate;
187     AVRational sar = ctx->inputs[0]->sample_aspect_ratio;
188     int height = ctx->inputs[0]->h;
189     int width = ctx->inputs[0]->w;
190     FFFrameSyncIn *in;
191     int i, ret;
192
193     s->desc = av_pix_fmt_desc_get(outlink->format);
194     if (!s->desc)
195         return AVERROR_BUG;
196
197     if (s->is_vertical) {
198         for (i = 1; i < s->nb_inputs; i++) {
199             if (ctx->inputs[i]->w != width) {
200                 av_log(ctx, AV_LOG_ERROR, "Input %d width %d does not match input %d width %d.\n", i, ctx->inputs[i]->w, 0, width);
201                 return AVERROR(EINVAL);
202             }
203             height += ctx->inputs[i]->h;
204         }
205     } else if (s->is_horizontal) {
206         for (i = 1; i < s->nb_inputs; i++) {
207             if (ctx->inputs[i]->h != height) {
208                 av_log(ctx, AV_LOG_ERROR, "Input %d height %d does not match input %d height %d.\n", i, ctx->inputs[i]->h, 0, height);
209                 return AVERROR(EINVAL);
210             }
211             width += ctx->inputs[i]->w;
212         }
213     } else {
214         char *arg, *p = s->layout, *saveptr = NULL;
215         char *arg2, *p2, *saveptr2 = NULL;
216         char *arg3, *p3, *saveptr3 = NULL;
217         int inw, inh, size;
218
219         for (i = 0; i < s->nb_inputs; i++) {
220             AVFilterLink *inlink = ctx->inputs[i];
221             StackItem *item = &s->items[i];
222
223             if (!(arg = av_strtok(p, "|", &saveptr)))
224                 return AVERROR(EINVAL);
225
226             p = NULL;
227
228             if ((ret = av_image_fill_linesizes(item->linesize, inlink->format, inlink->w)) < 0) {
229                 return ret;
230             }
231
232             item->height[1] = item->height[2] = AV_CEIL_RSHIFT(inlink->h, s->desc->log2_chroma_h);
233             item->height[0] = item->height[3] = inlink->h;
234
235             p2 = arg;
236             inw = inh = 0;
237
238             for (int j = 0; j < 2; j++) {
239                 if (!(arg2 = av_strtok(p2, "_", &saveptr2)))
240                     return AVERROR(EINVAL);
241
242                 p2 = NULL;
243                 p3 = arg2;
244                 while ((arg3 = av_strtok(p3, "+", &saveptr3))) {
245                     p3 = NULL;
246                     if (sscanf(arg3, "w%d", &size) == 1) {
247                         if (size == i || size < 0 || size >= s->nb_inputs)
248                             return AVERROR(EINVAL);
249
250                         if (!j)
251                             inw += ctx->inputs[size]->w;
252                         else
253                             inh += ctx->inputs[size]->w;
254                     } else if (sscanf(arg3, "h%d", &size) == 1) {
255                         if (size == i || size < 0 || size >= s->nb_inputs)
256                             return AVERROR(EINVAL);
257
258                         if (!j)
259                             inw += ctx->inputs[size]->h;
260                         else
261                             inh += ctx->inputs[size]->h;
262                     } else if (sscanf(arg3, "%d", &size) == 1) {
263                         if (size < 0)
264                             return AVERROR(EINVAL);
265
266                         if (!j)
267                             inw += size;
268                         else
269                             inh += size;
270                     } else {
271                         return AVERROR(EINVAL);
272                     }
273                 }
274             }
275
276             if ((ret = av_image_fill_linesizes(item->x, inlink->format, inw)) < 0) {
277                 return ret;
278             }
279
280             item->y[1] = item->y[2] = AV_CEIL_RSHIFT(inh, s->desc->log2_chroma_h);
281             item->y[0] = item->y[3] = inh;
282
283             width  = FFMAX(width,  inlink->w + inw);
284             height = FFMAX(height, inlink->h + inh);
285         }
286     }
287
288     s->nb_planes = av_pix_fmt_count_planes(outlink->format);
289
290     outlink->w          = width;
291     outlink->h          = height;
292     outlink->frame_rate = frame_rate;
293     outlink->sample_aspect_ratio = sar;
294
295     if ((ret = ff_framesync_init(&s->fs, ctx, s->nb_inputs)) < 0)
296         return ret;
297
298     in = s->fs.in;
299     s->fs.opaque = s;
300     s->fs.on_event = process_frame;
301
302     for (i = 0; i < s->nb_inputs; i++) {
303         AVFilterLink *inlink = ctx->inputs[i];
304
305         in[i].time_base = inlink->time_base;
306         in[i].sync   = 1;
307         in[i].before = EXT_STOP;
308         in[i].after  = s->shortest ? EXT_STOP : EXT_INFINITY;
309     }
310
311     ret = ff_framesync_configure(&s->fs);
312     outlink->time_base = s->fs.time_base;
313
314     return ret;
315 }
316
317 static av_cold void uninit(AVFilterContext *ctx)
318 {
319     StackContext *s = ctx->priv;
320     int i;
321
322     ff_framesync_uninit(&s->fs);
323     av_freep(&s->frames);
324     av_freep(&s->items);
325
326     for (i = 0; i < ctx->nb_inputs; i++)
327         av_freep(&ctx->input_pads[i].name);
328 }
329
330 static int activate(AVFilterContext *ctx)
331 {
332     StackContext *s = ctx->priv;
333     return ff_framesync_activate(&s->fs);
334 }
335
336 #define OFFSET(x) offsetof(StackContext, x)
337 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
338 static const AVOption stack_options[] = {
339     { "inputs", "set number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64=2}, 2, INT_MAX, .flags = FLAGS },
340     { "shortest", "force termination when the shortest input terminates", OFFSET(shortest), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, .flags = FLAGS },
341     { NULL },
342 };
343
344 static const AVFilterPad outputs[] = {
345     {
346         .name          = "default",
347         .type          = AVMEDIA_TYPE_VIDEO,
348         .config_props  = config_output,
349     },
350     { NULL }
351 };
352
353 #if CONFIG_HSTACK_FILTER
354
355 #define hstack_options stack_options
356 AVFILTER_DEFINE_CLASS(hstack);
357
358 AVFilter ff_vf_hstack = {
359     .name          = "hstack",
360     .description   = NULL_IF_CONFIG_SMALL("Stack video inputs horizontally."),
361     .priv_size     = sizeof(StackContext),
362     .priv_class    = &hstack_class,
363     .query_formats = query_formats,
364     .outputs       = outputs,
365     .init          = init,
366     .uninit        = uninit,
367     .activate      = activate,
368     .flags         = AVFILTER_FLAG_DYNAMIC_INPUTS,
369 };
370
371 #endif /* CONFIG_HSTACK_FILTER */
372
373 #if CONFIG_VSTACK_FILTER
374
375 #define vstack_options stack_options
376 AVFILTER_DEFINE_CLASS(vstack);
377
378 AVFilter ff_vf_vstack = {
379     .name          = "vstack",
380     .description   = NULL_IF_CONFIG_SMALL("Stack video inputs vertically."),
381     .priv_size     = sizeof(StackContext),
382     .priv_class    = &vstack_class,
383     .query_formats = query_formats,
384     .outputs       = outputs,
385     .init          = init,
386     .uninit        = uninit,
387     .activate      = activate,
388     .flags         = AVFILTER_FLAG_DYNAMIC_INPUTS,
389 };
390
391 #endif /* CONFIG_VSTACK_FILTER */
392
393 #if CONFIG_XSTACK_FILTER
394
395 static const AVOption xstack_options[] = {
396     { "inputs", "set number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64=2}, 2, INT_MAX, .flags = FLAGS },
397     { "layout", "set custom layout", OFFSET(layout), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, .flags = FLAGS },
398     { "shortest", "force termination when the shortest input terminates", OFFSET(shortest), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, .flags = FLAGS },
399     { NULL },
400 };
401
402 AVFILTER_DEFINE_CLASS(xstack);
403
404 AVFilter ff_vf_xstack = {
405     .name          = "xstack",
406     .description   = NULL_IF_CONFIG_SMALL("Stack video inputs into custom layout."),
407     .priv_size     = sizeof(StackContext),
408     .priv_class    = &xstack_class,
409     .query_formats = query_formats,
410     .outputs       = outputs,
411     .init          = init,
412     .uninit        = uninit,
413     .activate      = activate,
414     .flags         = AVFILTER_FLAG_DYNAMIC_INPUTS,
415 };
416
417 #endif /* CONFIG_XSTACK_FILTER */