2 * Copyright (c) 2015 Paul B. Mahol
4 * This file is part of FFmpeg.
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.
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.
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
21 #include "libavutil/avstring.h"
22 #include "libavutil/imgutils.h"
23 #include "libavutil/opt.h"
24 #include "libavutil/pixdesc.h"
29 #include "framesync.h"
32 typedef struct StackItem {
38 typedef struct StackContext {
40 const AVPixFmtDescriptor *desc;
53 static int query_formats(AVFilterContext *ctx)
55 AVFilterFormats *pix_fmts = NULL;
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)
67 return ff_set_common_formats(ctx, pix_fmts);
70 static av_cold int init(AVFilterContext *ctx)
72 StackContext *s = ctx->priv;
75 if (!strcmp(ctx->filter->name, "vstack"))
78 if (!strcmp(ctx->filter->name, "hstack"))
81 s->frames = av_calloc(s->nb_inputs, sizeof(*s->frames));
83 return AVERROR(ENOMEM);
85 s->items = av_calloc(s->nb_inputs, sizeof(*s->items));
87 return AVERROR(ENOMEM);
89 if (!strcmp(ctx->filter->name, "xstack")) {
91 if (s->nb_inputs == 2) {
92 s->layout = av_strdup("0_0|w0_0");
94 return AVERROR(ENOMEM);
96 av_log(ctx, AV_LOG_ERROR, "No layout specified.\n");
97 return AVERROR(EINVAL);
102 for (i = 0; i < s->nb_inputs; i++) {
103 AVFilterPad pad = { 0 };
105 pad.type = AVMEDIA_TYPE_VIDEO;
106 pad.name = av_asprintf("input%d", i);
108 return AVERROR(ENOMEM);
110 if ((ret = ff_insert_inpad(ctx, i, &pad)) < 0) {
119 static int process_slice(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
121 StackContext *s = ctx->priv;
123 AVFrame **in = s->frames;
124 const int start = (s->nb_inputs * job ) / nb_jobs;
125 const int end = (s->nb_inputs * (job+1)) / nb_jobs;
127 for (int i = start; i < end; i++) {
128 StackItem *item = &s->items[i];
130 for (int p = 0; p < s->nb_planes; p++) {
131 av_image_copy_plane(out->data[p] + out->linesize[p] * item->y[p] + item->x[p],
135 item->linesize[p], item->height[p]);
142 static int process_frame(FFFrameSync *fs)
144 AVFilterContext *ctx = fs->parent;
145 AVFilterLink *outlink = ctx->outputs[0];
146 StackContext *s = fs->opaque;
147 AVFrame **in = s->frames;
151 for (i = 0; i < s->nb_inputs; i++) {
152 if ((ret = ff_framesync_get_frame(&s->fs, i, &in[i], 0)) < 0)
156 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
158 return AVERROR(ENOMEM);
159 out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
160 out->sample_aspect_ratio = outlink->sample_aspect_ratio;
162 ctx->internal->execute(ctx, process_slice, out, NULL, FFMIN(s->nb_inputs, ff_filter_get_nb_threads(ctx)));
164 return ff_filter_frame(outlink, out);
167 static int config_output(AVFilterLink *outlink)
169 AVFilterContext *ctx = outlink->src;
170 StackContext *s = ctx->priv;
171 AVRational frame_rate = ctx->inputs[0]->frame_rate;
172 AVRational sar = ctx->inputs[0]->sample_aspect_ratio;
173 int height = ctx->inputs[0]->h;
174 int width = ctx->inputs[0]->w;
178 s->desc = av_pix_fmt_desc_get(outlink->format);
182 if (s->is_vertical) {
183 for (i = 0; i < s->nb_inputs; i++) {
184 AVFilterLink *inlink = ctx->inputs[i];
185 StackItem *item = &s->items[i];
187 if (ctx->inputs[i]->w != width) {
188 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);
189 return AVERROR(EINVAL);
192 if ((ret = av_image_fill_linesizes(item->linesize, inlink->format, inlink->w)) < 0) {
196 item->height[1] = item->height[2] = AV_CEIL_RSHIFT(inlink->h, s->desc->log2_chroma_h);
197 item->height[0] = item->height[3] = inlink->h;
200 item->y[1] = item->y[2] = AV_CEIL_RSHIFT(height, s->desc->log2_chroma_h);
201 item->y[0] = item->y[3] = height;
203 height += ctx->inputs[i]->h;
206 } else if (s->is_horizontal) {
207 for (i = 0; i < s->nb_inputs; i++) {
208 AVFilterLink *inlink = ctx->inputs[i];
209 StackItem *item = &s->items[i];
211 if (ctx->inputs[i]->h != height) {
212 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);
213 return AVERROR(EINVAL);
216 if ((ret = av_image_fill_linesizes(item->linesize, inlink->format, inlink->w)) < 0) {
220 item->height[1] = item->height[2] = AV_CEIL_RSHIFT(inlink->h, s->desc->log2_chroma_h);
221 item->height[0] = item->height[3] = inlink->h;
224 if ((ret = av_image_fill_linesizes(item->x, inlink->format, width)) < 0) {
228 width += ctx->inputs[i]->w;
232 char *arg, *p = s->layout, *saveptr = NULL;
233 char *arg2, *p2, *saveptr2 = NULL;
234 char *arg3, *p3, *saveptr3 = NULL;
237 for (i = 0; i < s->nb_inputs; i++) {
238 AVFilterLink *inlink = ctx->inputs[i];
239 StackItem *item = &s->items[i];
241 if (!(arg = av_strtok(p, "|", &saveptr)))
242 return AVERROR(EINVAL);
246 if ((ret = av_image_fill_linesizes(item->linesize, inlink->format, inlink->w)) < 0) {
250 item->height[1] = item->height[2] = AV_CEIL_RSHIFT(inlink->h, s->desc->log2_chroma_h);
251 item->height[0] = item->height[3] = inlink->h;
256 for (int j = 0; j < 2; j++) {
257 if (!(arg2 = av_strtok(p2, "_", &saveptr2)))
258 return AVERROR(EINVAL);
262 while ((arg3 = av_strtok(p3, "+", &saveptr3))) {
264 if (sscanf(arg3, "w%d", &size) == 1) {
265 if (size == i || size < 0 || size >= s->nb_inputs)
266 return AVERROR(EINVAL);
269 inw += ctx->inputs[size]->w;
271 inh += ctx->inputs[size]->w;
272 } else if (sscanf(arg3, "h%d", &size) == 1) {
273 if (size == i || size < 0 || size >= s->nb_inputs)
274 return AVERROR(EINVAL);
277 inw += ctx->inputs[size]->h;
279 inh += ctx->inputs[size]->h;
280 } else if (sscanf(arg3, "%d", &size) == 1) {
282 return AVERROR(EINVAL);
289 return AVERROR(EINVAL);
294 if ((ret = av_image_fill_linesizes(item->x, inlink->format, inw)) < 0) {
298 item->y[1] = item->y[2] = AV_CEIL_RSHIFT(inh, s->desc->log2_chroma_h);
299 item->y[0] = item->y[3] = inh;
301 width = FFMAX(width, inlink->w + inw);
302 height = FFMAX(height, inlink->h + inh);
306 s->nb_planes = av_pix_fmt_count_planes(outlink->format);
310 outlink->frame_rate = frame_rate;
311 outlink->sample_aspect_ratio = sar;
313 for (i = 1; i < s->nb_inputs; i++) {
314 AVFilterLink *inlink = ctx->inputs[i];
315 if (outlink->frame_rate.num != inlink->frame_rate.num ||
316 outlink->frame_rate.den != inlink->frame_rate.den) {
317 av_log(ctx, AV_LOG_VERBOSE,
318 "Video inputs have different frame rates, output will be VFR\n");
319 outlink->frame_rate = av_make_q(1, 0);
324 if ((ret = ff_framesync_init(&s->fs, ctx, s->nb_inputs)) < 0)
329 s->fs.on_event = process_frame;
331 for (i = 0; i < s->nb_inputs; i++) {
332 AVFilterLink *inlink = ctx->inputs[i];
334 in[i].time_base = inlink->time_base;
336 in[i].before = EXT_STOP;
337 in[i].after = s->shortest ? EXT_STOP : EXT_INFINITY;
340 ret = ff_framesync_configure(&s->fs);
341 outlink->time_base = s->fs.time_base;
346 static av_cold void uninit(AVFilterContext *ctx)
348 StackContext *s = ctx->priv;
351 ff_framesync_uninit(&s->fs);
352 av_freep(&s->frames);
355 for (i = 0; i < ctx->nb_inputs; i++)
356 av_freep(&ctx->input_pads[i].name);
359 static int activate(AVFilterContext *ctx)
361 StackContext *s = ctx->priv;
362 return ff_framesync_activate(&s->fs);
365 #define OFFSET(x) offsetof(StackContext, x)
366 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
367 static const AVOption stack_options[] = {
368 { "inputs", "set number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64=2}, 2, INT_MAX, .flags = FLAGS },
369 { "shortest", "force termination when the shortest input terminates", OFFSET(shortest), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, .flags = FLAGS },
373 static const AVFilterPad outputs[] = {
376 .type = AVMEDIA_TYPE_VIDEO,
377 .config_props = config_output,
382 #if CONFIG_HSTACK_FILTER
384 #define hstack_options stack_options
385 AVFILTER_DEFINE_CLASS(hstack);
387 AVFilter ff_vf_hstack = {
389 .description = NULL_IF_CONFIG_SMALL("Stack video inputs horizontally."),
390 .priv_size = sizeof(StackContext),
391 .priv_class = &hstack_class,
392 .query_formats = query_formats,
396 .activate = activate,
397 .flags = AVFILTER_FLAG_DYNAMIC_INPUTS | AVFILTER_FLAG_SLICE_THREADS,
400 #endif /* CONFIG_HSTACK_FILTER */
402 #if CONFIG_VSTACK_FILTER
404 #define vstack_options stack_options
405 AVFILTER_DEFINE_CLASS(vstack);
407 AVFilter ff_vf_vstack = {
409 .description = NULL_IF_CONFIG_SMALL("Stack video inputs vertically."),
410 .priv_size = sizeof(StackContext),
411 .priv_class = &vstack_class,
412 .query_formats = query_formats,
416 .activate = activate,
417 .flags = AVFILTER_FLAG_DYNAMIC_INPUTS | AVFILTER_FLAG_SLICE_THREADS,
420 #endif /* CONFIG_VSTACK_FILTER */
422 #if CONFIG_XSTACK_FILTER
424 static const AVOption xstack_options[] = {
425 { "inputs", "set number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64=2}, 2, INT_MAX, .flags = FLAGS },
426 { "layout", "set custom layout", OFFSET(layout), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, .flags = FLAGS },
427 { "shortest", "force termination when the shortest input terminates", OFFSET(shortest), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, .flags = FLAGS },
431 AVFILTER_DEFINE_CLASS(xstack);
433 AVFilter ff_vf_xstack = {
435 .description = NULL_IF_CONFIG_SMALL("Stack video inputs into custom layout."),
436 .priv_size = sizeof(StackContext),
437 .priv_class = &xstack_class,
438 .query_formats = query_formats,
442 .activate = activate,
443 .flags = AVFILTER_FLAG_DYNAMIC_INPUTS | AVFILTER_FLAG_SLICE_THREADS,
446 #endif /* CONFIG_XSTACK_FILTER */