FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_stack.c
Date: 2024-04-19 17:50:32
Exec Total Coverage
Lines: 117 232 50.4%
Functions: 7 7 100.0%
Branches: 48 150 32.0%

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