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 "config_components.h"
22
23 #include "libavutil/avstring.h"
24 #include "libavutil/imgutils.h"
25 #include "libavutil/opt.h"
26 #include "libavutil/parseutils.h"
27 #include "libavutil/pixdesc.h"
28
29 #include "avfilter.h"
30 #include "drawutils.h"
31 #include "formats.h"
32 #include "internal.h"
33 #include "framesync.h"
34 #include "video.h"
35
36 typedef struct StackItem {
37 int x[4], y[4];
38 int linesize[4];
39 int height[4];
40 } StackItem;
41
42 typedef struct StackContext {
43 const AVClass *class;
44 const AVPixFmtDescriptor *desc;
45 int nb_inputs;
46 char *layout;
47 int shortest;
48 int is_vertical;
49 int is_horizontal;
50 int nb_planes;
51 int nb_grid_columns;
52 int nb_grid_rows;
53 uint8_t fillcolor[4];
54 char *fillcolor_str;
55 int fillcolor_enable;
56
57 FFDrawContext draw;
58 FFDrawColor color;
59
60 StackItem *items;
61 AVFrame **frames;
62 FFFrameSync fs;
63 } StackContext;
64
query_formats(AVFilterContext * ctx)65 static int query_formats(AVFilterContext *ctx)
66 {
67 StackContext *s = ctx->priv;
68 int reject_flags = AV_PIX_FMT_FLAG_BITSTREAM |
69 AV_PIX_FMT_FLAG_HWACCEL |
70 AV_PIX_FMT_FLAG_PAL;
71
72 if (s->fillcolor_enable) {
73 return ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
74 }
75
76 return ff_set_common_formats(ctx, ff_formats_pixdesc_filter(0, reject_flags));
77 }
78
init(AVFilterContext * ctx)79 static av_cold int init(AVFilterContext *ctx)
80 {
81 StackContext *s = ctx->priv;
82 int i, ret;
83
84 if (!strcmp(ctx->filter->name, "vstack"))
85 s->is_vertical = 1;
86
87 if (!strcmp(ctx->filter->name, "hstack"))
88 s->is_horizontal = 1;
89
90 if (!strcmp(ctx->filter->name, "xstack")) {
91 int is_grid;
92 if (strcmp(s->fillcolor_str, "none") &&
93 av_parse_color(s->fillcolor, s->fillcolor_str, -1, ctx) >= 0) {
94 s->fillcolor_enable = 1;
95 } else {
96 s->fillcolor_enable = 0;
97 }
98 is_grid = s->nb_grid_rows && s->nb_grid_columns;
99 if (s->layout && is_grid) {
100 av_log(ctx, AV_LOG_ERROR, "Both layout and grid were specified. Only one is allowed.\n");
101 return AVERROR(EINVAL);
102 }
103 if (!s->layout && !is_grid) {
104 if (s->nb_inputs == 2) {
105 s->nb_grid_rows = 1;
106 s->nb_grid_columns = 2;
107 is_grid = 1;
108 } else {
109 av_log(ctx, AV_LOG_ERROR, "No layout or grid specified.\n");
110 return AVERROR(EINVAL);
111 }
112 }
113
114 if (is_grid)
115 s->nb_inputs = s->nb_grid_rows * s->nb_grid_columns;
116 }
117
118 s->frames = av_calloc(s->nb_inputs, sizeof(*s->frames));
119 if (!s->frames)
120 return AVERROR(ENOMEM);
121
122 s->items = av_calloc(s->nb_inputs, sizeof(*s->items));
123 if (!s->items)
124 return AVERROR(ENOMEM);
125
126 for (i = 0; i < s->nb_inputs; i++) {
127 AVFilterPad pad = { 0 };
128
129 pad.type = AVMEDIA_TYPE_VIDEO;
130 pad.name = av_asprintf("input%d", i);
131 if (!pad.name)
132 return AVERROR(ENOMEM);
133
134 if ((ret = ff_append_inpad_free_name(ctx, &pad)) < 0)
135 return ret;
136 }
137
138 return 0;
139 }
140
process_slice(AVFilterContext * ctx,void * arg,int job,int nb_jobs)141 static int process_slice(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
142 {
143 StackContext *s = ctx->priv;
144 AVFrame *out = arg;
145 AVFrame **in = s->frames;
146 const int start = (s->nb_inputs * job ) / nb_jobs;
147 const int end = (s->nb_inputs * (job+1)) / nb_jobs;
148
149 for (int i = start; i < end; i++) {
150 StackItem *item = &s->items[i];
151
152 for (int p = 0; p < s->nb_planes; p++) {
153 av_image_copy_plane(out->data[p] + out->linesize[p] * item->y[p] + item->x[p],
154 out->linesize[p],
155 in[i]->data[p],
156 in[i]->linesize[p],
157 item->linesize[p], item->height[p]);
158 }
159 }
160
161 return 0;
162 }
163
process_frame(FFFrameSync * fs)164 static int process_frame(FFFrameSync *fs)
165 {
166 AVFilterContext *ctx = fs->parent;
167 AVFilterLink *outlink = ctx->outputs[0];
168 StackContext *s = fs->opaque;
169 AVFrame **in = s->frames;
170 AVFrame *out;
171 int i, ret;
172
173 for (i = 0; i < s->nb_inputs; i++) {
174 if ((ret = ff_framesync_get_frame(&s->fs, i, &in[i], 0)) < 0)
175 return ret;
176 }
177
178 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
179 if (!out)
180 return AVERROR(ENOMEM);
181 out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
182 out->sample_aspect_ratio = outlink->sample_aspect_ratio;
183
184 if (s->fillcolor_enable)
185 ff_fill_rectangle(&s->draw, &s->color, out->data, out->linesize,
186 0, 0, outlink->w, outlink->h);
187
188 ff_filter_execute(ctx, process_slice, out, NULL,
189 FFMIN(s->nb_inputs, ff_filter_get_nb_threads(ctx)));
190
191 return ff_filter_frame(outlink, out);
192 }
193
config_output(AVFilterLink * outlink)194 static int config_output(AVFilterLink *outlink)
195 {
196 AVFilterContext *ctx = outlink->src;
197 StackContext *s = ctx->priv;
198 AVRational frame_rate = ctx->inputs[0]->frame_rate;
199 AVRational sar = ctx->inputs[0]->sample_aspect_ratio;
200 int height = ctx->inputs[0]->h;
201 int width = ctx->inputs[0]->w;
202 FFFrameSyncIn *in;
203 int i, ret;
204
205 s->desc = av_pix_fmt_desc_get(outlink->format);
206 if (!s->desc)
207 return AVERROR_BUG;
208
209 if (s->is_vertical) {
210 for (i = 0; i < s->nb_inputs; i++) {
211 AVFilterLink *inlink = ctx->inputs[i];
212 StackItem *item = &s->items[i];
213
214 if (ctx->inputs[i]->w != width) {
215 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);
216 return AVERROR(EINVAL);
217 }
218
219 if ((ret = av_image_fill_linesizes(item->linesize, inlink->format, inlink->w)) < 0) {
220 return ret;
221 }
222
223 item->height[1] = item->height[2] = AV_CEIL_RSHIFT(inlink->h, s->desc->log2_chroma_h);
224 item->height[0] = item->height[3] = inlink->h;
225
226 if (i) {
227 item->y[1] = item->y[2] = AV_CEIL_RSHIFT(height, s->desc->log2_chroma_h);
228 item->y[0] = item->y[3] = height;
229
230 height += ctx->inputs[i]->h;
231 }
232 }
233 } else if (s->is_horizontal) {
234 for (i = 0; i < s->nb_inputs; i++) {
235 AVFilterLink *inlink = ctx->inputs[i];
236 StackItem *item = &s->items[i];
237
238 if (ctx->inputs[i]->h != height) {
239 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);
240 return AVERROR(EINVAL);
241 }
242
243 if ((ret = av_image_fill_linesizes(item->linesize, inlink->format, inlink->w)) < 0) {
244 return ret;
245 }
246
247 item->height[1] = item->height[2] = AV_CEIL_RSHIFT(inlink->h, s->desc->log2_chroma_h);
248 item->height[0] = item->height[3] = inlink->h;
249
250 if (i) {
251 if ((ret = av_image_fill_linesizes(item->x, inlink->format, width)) < 0) {
252 return ret;
253 }
254
255 width += ctx->inputs[i]->w;
256 }
257 }
258 } else if (s->nb_grid_rows && s->nb_grid_columns) {
259 int inw = 0, inh = 0;
260 int k = 0;
261 int row_height;
262 height = 0;
263 width = 0;
264 for (i = 0; i < s->nb_grid_rows; i++, inh += row_height) {
265 row_height = ctx->inputs[i * s->nb_grid_columns]->h;
266 inw = 0;
267 for (int j = 0; j < s->nb_grid_columns; j++, k++) {
268 AVFilterLink *inlink = ctx->inputs[k];
269 StackItem *item = &s->items[k];
270
271 if (ctx->inputs[k]->h != row_height) {
272 av_log(ctx, AV_LOG_ERROR, "Input %d height %d does not match current row's height %d.\n",
273 k, ctx->inputs[k]->h, row_height);
274 return AVERROR(EINVAL);
275 }
276
277 if ((ret = av_image_fill_linesizes(item->linesize, inlink->format, inlink->w)) < 0) {
278 return ret;
279 }
280
281 item->height[1] = item->height[2] = AV_CEIL_RSHIFT(inlink->h, s->desc->log2_chroma_h);
282 item->height[0] = item->height[3] = inlink->h;
283
284 if ((ret = av_image_fill_linesizes(item->x, inlink->format, inw)) < 0) {
285 return ret;
286 }
287
288 item->y[1] = item->y[2] = AV_CEIL_RSHIFT(inh, s->desc->log2_chroma_h);
289 item->y[0] = item->y[3] = inh;
290 inw += ctx->inputs[k]->w;
291 }
292 height += row_height;
293 if (!i)
294 width = inw;
295 if (i && width != inw) {
296 av_log(ctx, AV_LOG_ERROR, "Row %d width %d does not match previous row width %d.\n", i, inw, width);
297 return AVERROR(EINVAL);
298 }
299 }
300 } else {
301 char *arg, *p = s->layout, *saveptr = NULL;
302 char *arg2, *p2, *saveptr2 = NULL;
303 char *arg3, *p3, *saveptr3 = NULL;
304 int inw, inh, size;
305
306 if (s->fillcolor_enable) {
307 ff_draw_init(&s->draw, ctx->inputs[0]->format, 0);
308 ff_draw_color(&s->draw, &s->color, s->fillcolor);
309 }
310
311 for (i = 0; i < s->nb_inputs; i++) {
312 AVFilterLink *inlink = ctx->inputs[i];
313 StackItem *item = &s->items[i];
314
315 if (!(arg = av_strtok(p, "|", &saveptr)))
316 return AVERROR(EINVAL);
317
318 p = NULL;
319
320 if ((ret = av_image_fill_linesizes(item->linesize, inlink->format, inlink->w)) < 0) {
321 return ret;
322 }
323
324 item->height[1] = item->height[2] = AV_CEIL_RSHIFT(inlink->h, s->desc->log2_chroma_h);
325 item->height[0] = item->height[3] = inlink->h;
326
327 p2 = arg;
328 inw = inh = 0;
329
330 for (int j = 0; j < 2; j++) {
331 if (!(arg2 = av_strtok(p2, "_", &saveptr2)))
332 return AVERROR(EINVAL);
333
334 p2 = NULL;
335 p3 = arg2;
336 while ((arg3 = av_strtok(p3, "+", &saveptr3))) {
337 p3 = NULL;
338 if (sscanf(arg3, "w%d", &size) == 1) {
339 if (size == i || size < 0 || size >= s->nb_inputs)
340 return AVERROR(EINVAL);
341
342 if (!j)
343 inw += ctx->inputs[size]->w;
344 else
345 inh += ctx->inputs[size]->w;
346 } else if (sscanf(arg3, "h%d", &size) == 1) {
347 if (size == i || size < 0 || size >= s->nb_inputs)
348 return AVERROR(EINVAL);
349
350 if (!j)
351 inw += ctx->inputs[size]->h;
352 else
353 inh += ctx->inputs[size]->h;
354 } else if (sscanf(arg3, "%d", &size) == 1) {
355 if (size < 0)
356 return AVERROR(EINVAL);
357
358 if (!j)
359 inw += size;
360 else
361 inh += size;
362 } else {
363 return AVERROR(EINVAL);
364 }
365 }
366 }
367
368 if ((ret = av_image_fill_linesizes(item->x, inlink->format, inw)) < 0) {
369 return ret;
370 }
371
372 item->y[1] = item->y[2] = AV_CEIL_RSHIFT(inh, s->desc->log2_chroma_h);
373 item->y[0] = item->y[3] = inh;
374
375 width = FFMAX(width, inlink->w + inw);
376 height = FFMAX(height, inlink->h + inh);
377 }
378 }
379
380 s->nb_planes = av_pix_fmt_count_planes(outlink->format);
381
382 outlink->w = width;
383 outlink->h = height;
384 outlink->frame_rate = frame_rate;
385 outlink->sample_aspect_ratio = sar;
386
387 for (i = 1; i < s->nb_inputs; i++) {
388 AVFilterLink *inlink = ctx->inputs[i];
389 if (outlink->frame_rate.num != inlink->frame_rate.num ||
390 outlink->frame_rate.den != inlink->frame_rate.den) {
391 av_log(ctx, AV_LOG_VERBOSE,
392 "Video inputs have different frame rates, output will be VFR\n");
393 outlink->frame_rate = av_make_q(1, 0);
394 break;
395 }
396 }
397
398 if ((ret = ff_framesync_init(&s->fs, ctx, s->nb_inputs)) < 0)
399 return ret;
400
401 in = s->fs.in;
402 s->fs.opaque = s;
403 s->fs.on_event = process_frame;
404
405 for (i = 0; i < s->nb_inputs; i++) {
406 AVFilterLink *inlink = ctx->inputs[i];
407
408 in[i].time_base = inlink->time_base;
409 in[i].sync = 1;
410 in[i].before = EXT_STOP;
411 in[i].after = s->shortest ? EXT_STOP : EXT_INFINITY;
412 }
413
414 ret = ff_framesync_configure(&s->fs);
415 outlink->time_base = s->fs.time_base;
416
417 return ret;
418 }
419
uninit(AVFilterContext * ctx)420 static av_cold void uninit(AVFilterContext *ctx)
421 {
422 StackContext *s = ctx->priv;
423
424 ff_framesync_uninit(&s->fs);
425 av_freep(&s->frames);
426 av_freep(&s->items);
427 }
428
activate(AVFilterContext * ctx)429 static int activate(AVFilterContext *ctx)
430 {
431 StackContext *s = ctx->priv;
432 return ff_framesync_activate(&s->fs);
433 }
434
435 #define OFFSET(x) offsetof(StackContext, x)
436 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
437 static const AVOption stack_options[] = {
438 { "inputs", "set number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64=2}, 2, INT_MAX, .flags = FLAGS },
439 { "shortest", "force termination when the shortest input terminates", OFFSET(shortest), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, .flags = FLAGS },
440 { NULL },
441 };
442
443 AVFILTER_DEFINE_CLASS_EXT(stack, "(h|v)stack", stack_options);
444
445 static const AVFilterPad outputs[] = {
446 {
447 .name = "default",
448 .type = AVMEDIA_TYPE_VIDEO,
449 .config_props = config_output,
450 },
451 };
452
453 #if CONFIG_HSTACK_FILTER
454
455 const AVFilter ff_vf_hstack = {
456 .name = "hstack",
457 .description = NULL_IF_CONFIG_SMALL("Stack video inputs horizontally."),
458 .priv_class = &stack_class,
459 .priv_size = sizeof(StackContext),
460 FILTER_OUTPUTS(outputs),
461 FILTER_QUERY_FUNC(query_formats),
462 .init = init,
463 .uninit = uninit,
464 .activate = activate,
465 .flags = AVFILTER_FLAG_DYNAMIC_INPUTS | AVFILTER_FLAG_SLICE_THREADS,
466 };
467
468 #endif /* CONFIG_HSTACK_FILTER */
469
470 #if CONFIG_VSTACK_FILTER
471
472 const AVFilter ff_vf_vstack = {
473 .name = "vstack",
474 .description = NULL_IF_CONFIG_SMALL("Stack video inputs vertically."),
475 .priv_class = &stack_class,
476 .priv_size = sizeof(StackContext),
477 FILTER_OUTPUTS(outputs),
478 FILTER_QUERY_FUNC(query_formats),
479 .init = init,
480 .uninit = uninit,
481 .activate = activate,
482 .flags = AVFILTER_FLAG_DYNAMIC_INPUTS | AVFILTER_FLAG_SLICE_THREADS,
483 };
484
485 #endif /* CONFIG_VSTACK_FILTER */
486
487 #if CONFIG_XSTACK_FILTER
488
489 static const AVOption xstack_options[] = {
490 { "inputs", "set number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64=2}, 2, INT_MAX, .flags = FLAGS },
491 { "layout", "set custom layout", OFFSET(layout), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, .flags = FLAGS },
492 { "grid", "set fixed size grid layout", OFFSET(nb_grid_columns), AV_OPT_TYPE_IMAGE_SIZE, {.str=NULL}, 0, 0, .flags = FLAGS },
493 { "shortest", "force termination when the shortest input terminates", OFFSET(shortest), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, .flags = FLAGS },
494 { "fill", "set the color for unused pixels", OFFSET(fillcolor_str), AV_OPT_TYPE_STRING, {.str = "none"}, .flags = FLAGS },
495 { NULL },
496 };
497
498 AVFILTER_DEFINE_CLASS(xstack);
499
500 const AVFilter ff_vf_xstack = {
501 .name = "xstack",
502 .description = NULL_IF_CONFIG_SMALL("Stack video inputs into custom layout."),
503 .priv_size = sizeof(StackContext),
504 .priv_class = &xstack_class,
505 FILTER_OUTPUTS(outputs),
506 FILTER_QUERY_FUNC(query_formats),
507 .init = init,
508 .uninit = uninit,
509 .activate = activate,
510 .flags = AVFILTER_FLAG_DYNAMIC_INPUTS | AVFILTER_FLAG_SLICE_THREADS,
511 };
512
513 #endif /* CONFIG_XSTACK_FILTER */
514