FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/graphparser.c
Date: 2024-07-26 21:54:09
Exec Total Coverage
Lines: 346 585 59.1%
Functions: 23 28 82.1%
Branches: 198 362 54.7%

Line Branch Exec Source
1 /*
2 * filter graph parser
3 * Copyright (c) 2008 Vitor Sessak
4 * Copyright (c) 2007 Bobby Bingham
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include <string.h>
24 #include <stdio.h>
25
26 #include "libavutil/avstring.h"
27 #include "libavutil/dict.h"
28 #include "libavutil/mem.h"
29 #include "libavutil/opt.h"
30
31 #include "avfilter.h"
32 #include "avfilter_internal.h"
33 #include "internal.h"
34
35 #define WHITESPACES " \n\t\r"
36
37 /**
38 * Parse the name of a link, which has the format "[linkname]".
39 *
40 * @return a pointer (that need to be freed after use) to the name
41 * between parenthesis
42 */
43 691 static char *parse_link_name(const char **buf, void *log_ctx)
44 {
45 691 const char *start = *buf;
46 char *name;
47 691 (*buf)++;
48
49 691 name = av_get_token(buf, "]");
50
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 691 times.
691 if (!name)
51 return NULL;
52
53
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 691 times.
691 if (!name[0]) {
54 av_log(log_ctx, AV_LOG_ERROR,
55 "Bad (empty?) label found in the following: \"%s\".\n", start);
56 goto fail;
57 }
58
59
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 691 times.
691 if (**buf != ']') {
60 av_log(log_ctx, AV_LOG_ERROR,
61 "Mismatched '[' found in the following: \"%s\".\n", start);
62 fail:
63 av_freep(&name);
64 return NULL;
65 }
66 691 (*buf)++;
67
68 691 return name;
69 }
70
71 AVFilterInOut *avfilter_inout_alloc(void)
72 {
73 return av_mallocz(sizeof(AVFilterInOut));
74 }
75
76 25852 void avfilter_inout_free(AVFilterInOut **inout)
77 {
78
2/2
✓ Branch 0 taken 25853 times.
✓ Branch 1 taken 25852 times.
51705 while (*inout) {
79 25853 AVFilterInOut *next = (*inout)->next;
80 25853 av_freep(&(*inout)->name);
81 25853 av_freep(inout);
82 25853 *inout = next;
83 }
84 25852 }
85
86 45 static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
87 {
88 AVFilterInOut *ret;
89
90
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 45 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
45 while (*links && (!(*links)->name || strcmp((*links)->name, label)))
91 links = &((*links)->next);
92
93 45 ret = *links;
94
95
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45 times.
45 if (ret) {
96 *links = ret->next;
97 ret->next = NULL;
98 }
99
100 45 return ret;
101 }
102
103 25898 static void append_inout(AVFilterInOut **inouts, AVFilterInOut **element)
104 {
105
4/4
✓ Branch 0 taken 588 times.
✓ Branch 1 taken 25654 times.
✓ Branch 2 taken 344 times.
✓ Branch 3 taken 244 times.
26242 while (*inouts && (*inouts)->next)
106 344 inouts = &((*inouts)->next);
107
108
2/2
✓ Branch 0 taken 25654 times.
✓ Branch 1 taken 244 times.
25898 if (!*inouts)
109 25654 *inouts = *element;
110 else
111 244 (*inouts)->next = *element;
112 25898 *element = NULL;
113 25898 }
114
115 12883 static int parse_sws_flags(const char **buf, char **dst, void *log_ctx)
116 {
117 12883 char *p = strchr(*buf, ';');
118
119
2/2
✓ Branch 0 taken 12834 times.
✓ Branch 1 taken 49 times.
12883 if (strncmp(*buf, "sws_flags=", 10))
120 12834 return 0;
121
122
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
49 if (!p) {
123 av_log(log_ctx, AV_LOG_ERROR, "sws_flags not terminated with ';'.\n");
124 return AVERROR(EINVAL);
125 }
126
127 49 *buf += 4; // keep the 'flags=' part
128
129 49 av_freep(dst);
130
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 49 times.
49 if (!(*dst = av_mallocz(p - *buf + 1)))
131 return AVERROR(ENOMEM);
132 49 av_strlcpy(*dst, *buf, p - *buf + 1);
133
134 49 *buf = p + 1;
135 49 return 0;
136 }
137
138 int avfilter_graph_parse2(AVFilterGraph *graph, const char *filters,
139 AVFilterInOut **inputs,
140 AVFilterInOut **outputs)
141 {
142 AVFilterGraphSegment *seg;
143 int ret;
144
145 ret = avfilter_graph_segment_parse(graph, filters, 0, &seg);
146 if (ret < 0)
147 return ret;
148
149 ret = avfilter_graph_segment_apply(seg, 0, inputs, outputs);
150 avfilter_graph_segment_free(&seg);
151 if (ret < 0)
152 goto end;
153
154 return 0;
155
156 end:
157 while (graph->nb_filters)
158 avfilter_free(graph->filters[0]);
159 av_freep(&graph->filters);
160
161 return ret;
162 }
163
164 int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
165 AVFilterInOut *open_inputs,
166 AVFilterInOut *open_outputs, void *log_ctx)
167 {
168 int ret;
169 AVFilterInOut *cur, *match, *inputs = NULL, *outputs = NULL;
170
171 if ((ret = avfilter_graph_parse2(graph, filters, &inputs, &outputs)) < 0)
172 goto fail;
173
174 /* First input can be omitted if it is "[in]" */
175 if (inputs && !inputs->name)
176 inputs->name = av_strdup("in");
177 for (cur = inputs; cur; cur = cur->next) {
178 if (!cur->name) {
179 av_log(log_ctx, AV_LOG_ERROR,
180 "Not enough inputs specified for the \"%s\" filter.\n",
181 cur->filter_ctx->filter->name);
182 ret = AVERROR(EINVAL);
183 goto fail;
184 }
185 if (!(match = extract_inout(cur->name, &open_outputs)))
186 continue;
187 ret = avfilter_link(match->filter_ctx, match->pad_idx,
188 cur->filter_ctx, cur->pad_idx);
189 avfilter_inout_free(&match);
190 if (ret < 0)
191 goto fail;
192 }
193
194 /* Last output can be omitted if it is "[out]" */
195 if (outputs && !outputs->name)
196 outputs->name = av_strdup("out");
197 for (cur = outputs; cur; cur = cur->next) {
198 if (!cur->name) {
199 av_log(log_ctx, AV_LOG_ERROR,
200 "Invalid filterchain containing an unlabelled output pad: \"%s\"\n",
201 filters);
202 ret = AVERROR(EINVAL);
203 goto fail;
204 }
205 if (!(match = extract_inout(cur->name, &open_inputs)))
206 continue;
207 ret = avfilter_link(cur->filter_ctx, cur->pad_idx,
208 match->filter_ctx, match->pad_idx);
209 avfilter_inout_free(&match);
210 if (ret < 0)
211 goto fail;
212 }
213
214 fail:
215 if (ret < 0) {
216 while (graph->nb_filters)
217 avfilter_free(graph->filters[0]);
218 av_freep(&graph->filters);
219 }
220 avfilter_inout_free(&inputs);
221 avfilter_inout_free(&outputs);
222 avfilter_inout_free(&open_inputs);
223 avfilter_inout_free(&open_outputs);
224 return ret;
225 }
226
227 691 static void pad_params_free(AVFilterPadParams **pfpp)
228 {
229 691 AVFilterPadParams *fpp = *pfpp;
230
231
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 691 times.
691 if (!fpp)
232 return;
233
234 691 av_freep(&fpp->label);
235
236 691 av_freep(pfpp);
237 }
238
239 25526 static void filter_params_free(AVFilterParams **pp)
240 {
241 25526 AVFilterParams *p = *pp;
242
243
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25526 times.
25526 if (!p)
244 return;
245
246
2/2
✓ Branch 0 taken 346 times.
✓ Branch 1 taken 25526 times.
25872 for (unsigned i = 0; i < p->nb_inputs; i++)
247 346 pad_params_free(&p->inputs[i]);
248 25526 av_freep(&p->inputs);
249
250
2/2
✓ Branch 0 taken 345 times.
✓ Branch 1 taken 25526 times.
25871 for (unsigned i = 0; i < p->nb_outputs; i++)
251 345 pad_params_free(&p->outputs[i]);
252 25526 av_freep(&p->outputs);
253
254 25526 av_dict_free(&p->opts);
255
256 25526 av_freep(&p->filter_name);
257 25526 av_freep(&p->instance_name);
258
259 25526 av_freep(pp);
260 }
261
262 13123 static void chain_free(AVFilterChain **pch)
263 {
264 13123 AVFilterChain *ch = *pch;
265
266
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13123 times.
13123 if (!ch)
267 return;
268
269
2/2
✓ Branch 0 taken 25526 times.
✓ Branch 1 taken 13123 times.
38649 for (size_t i = 0; i < ch->nb_filters; i++)
270 25526 filter_params_free(&ch->filters[i]);
271 13123 av_freep(&ch->filters);
272
273 13123 av_freep(pch);
274 }
275
276 12926 void avfilter_graph_segment_free(AVFilterGraphSegment **pseg)
277 {
278 12926 AVFilterGraphSegment *seg = *pseg;
279
280
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 12883 times.
12926 if (!seg)
281 43 return;
282
283
2/2
✓ Branch 0 taken 13123 times.
✓ Branch 1 taken 12883 times.
26006 for (size_t i = 0; i < seg->nb_chains; i++)
284 13123 chain_free(&seg->chains[i]);
285 12883 av_freep(&seg->chains);
286
287 12883 av_freep(&seg->scale_sws_opts);
288
289 12883 av_freep(pseg);
290 }
291
292 51089 static int linklabels_parse(void *logctx, const char **linklabels,
293 AVFilterPadParams ***res, unsigned *nb_res)
294 {
295 51089 AVFilterPadParams **pp = NULL;
296 51089 int nb = 0;
297 int ret;
298
299
2/2
✓ Branch 0 taken 691 times.
✓ Branch 1 taken 51089 times.
51780 while (**linklabels == '[') {
300 char *label;
301 AVFilterPadParams *par;
302
303 691 label = parse_link_name(linklabels, logctx);
304
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 691 times.
691 if (!label) {
305 ret = AVERROR(EINVAL);
306 goto fail;
307 }
308
309 691 par = av_mallocz(sizeof(*par));
310
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 691 times.
691 if (!par) {
311 av_freep(&label);
312 ret = AVERROR(ENOMEM);
313 goto fail;
314 }
315
316 691 par->label = label;
317
318 691 ret = av_dynarray_add_nofree(&pp, &nb, par);
319
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 691 times.
691 if (ret < 0) {
320 pad_params_free(&par);
321 goto fail;
322 }
323
324 691 *linklabels += strspn(*linklabels, WHITESPACES);
325 }
326
327 51089 *res = pp;
328 51089 *nb_res = nb;
329
330 51089 return 0;
331 fail:
332 for (unsigned i = 0; i < nb; i++)
333 pad_params_free(&pp[i]);
334 av_freep(&pp);
335 return ret;
336 }
337
338 25526 static int filter_parse(void *logctx, const char **filter,
339 AVFilterParams **pp)
340 {
341 AVFilterParams *p;
342 char *inst_name;
343 int ret;
344
345 25526 p = av_mallocz(sizeof(*p));
346
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25526 times.
25526 if (!p)
347 return AVERROR(ENOMEM);
348
349 25526 ret = linklabels_parse(logctx, filter, &p->inputs, &p->nb_inputs);
350
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25526 times.
25526 if (ret < 0)
351 goto fail;
352
353 25526 p->filter_name = av_get_token(filter, "=,;[");
354
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25526 times.
25526 if (!p->filter_name) {
355 ret = AVERROR(ENOMEM);
356 goto fail;
357 }
358
359 25526 inst_name = strchr(p->filter_name, '@');
360
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25526 times.
25526 if (inst_name) {
361 *inst_name++ = 0;
362 p->instance_name = av_strdup(inst_name);
363 if (!p->instance_name) {
364 ret = AVERROR(ENOMEM);
365 goto fail;
366 }
367 }
368
369
2/2
✓ Branch 0 taken 13315 times.
✓ Branch 1 taken 12211 times.
25526 if (**filter == '=') {
370 13315 const AVFilter *f = avfilter_get_by_name(p->filter_name);
371 char *opts;
372
373 13315 (*filter)++;
374
375 13315 opts = av_get_token(filter, "[],;");
376
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13315 times.
13315 if (!opts) {
377 ret = AVERROR(ENOMEM);
378 goto fail;
379 }
380
381 13315 ret = ff_filter_opt_parse(logctx, f ? f->priv_class : NULL,
382
1/2
✓ Branch 0 taken 13315 times.
✗ Branch 1 not taken.
13315 &p->opts, opts);
383 13315 av_freep(&opts);
384
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13315 times.
13315 if (ret < 0)
385 goto fail;
386 }
387
388 25526 ret = linklabels_parse(logctx, filter, &p->outputs, &p->nb_outputs);
389
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25526 times.
25526 if (ret < 0)
390 goto fail;
391
392 25526 *filter += strspn(*filter, WHITESPACES);
393
394 25526 *pp = p;
395 25526 return 0;
396 fail:
397 av_log(logctx, AV_LOG_ERROR,
398 "Error parsing a filter description around: %s\n", *filter);
399 filter_params_free(&p);
400 return ret;
401 }
402
403 13123 static int chain_parse(void *logctx, const char **pchain,
404 AVFilterChain **pch)
405 {
406 13123 const char *chain = *pchain;
407 AVFilterChain *ch;
408 13123 int ret, nb_filters = 0;
409
410 13123 *pch = NULL;
411
412 13123 ch = av_mallocz(sizeof(*ch));
413
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13123 times.
13123 if (!ch)
414 return AVERROR(ENOMEM);
415
416
2/2
✓ Branch 0 taken 25526 times.
✓ Branch 1 taken 12869 times.
38395 while (*chain) {
417 AVFilterParams *p;
418 char chr;
419
420 25526 ret = filter_parse(logctx, &chain, &p);
421
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25526 times.
25526 if (ret < 0)
422 goto fail;
423
424 25526 ret = av_dynarray_add_nofree(&ch->filters, &nb_filters, p);
425
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25526 times.
25526 if (ret < 0) {
426 filter_params_free(&p);
427 goto fail;
428 }
429 25526 ch->nb_filters = nb_filters;
430
431 // a filter ends with one of: , ; end-of-string
432 25526 chr = *chain;
433
5/6
✓ Branch 0 taken 12657 times.
✓ Branch 1 taken 12869 times.
✓ Branch 2 taken 254 times.
✓ Branch 3 taken 12403 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 254 times.
25526 if (chr && chr != ',' && chr != ';') {
434 av_log(logctx, AV_LOG_ERROR,
435 "Trailing garbage after a filter: %s\n", chain);
436 ret = AVERROR(EINVAL);
437 goto fail;
438 }
439
440
2/2
✓ Branch 0 taken 12657 times.
✓ Branch 1 taken 12869 times.
25526 if (chr) {
441 12657 chain++;
442 12657 chain += strspn(chain, WHITESPACES);
443
444
2/2
✓ Branch 0 taken 254 times.
✓ Branch 1 taken 12403 times.
12657 if (chr == ';')
445 254 break;
446 }
447 }
448
449 13123 *pchain = chain;
450 13123 *pch = ch;
451
452 13123 return 0;
453 fail:
454 av_log(logctx, AV_LOG_ERROR,
455 "Error parsing filterchain '%s' around: %s\n", *pchain, chain);
456 chain_free(&ch);
457 return ret;
458 }
459
460 12883 int avfilter_graph_segment_parse(AVFilterGraph *graph, const char *graph_str,
461 int flags, AVFilterGraphSegment **pseg)
462 {
463 AVFilterGraphSegment *seg;
464 12883 int ret, nb_chains = 0;
465
466 12883 *pseg = NULL;
467
468
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12883 times.
12883 if (flags)
469 return AVERROR(ENOSYS);
470
471 12883 seg = av_mallocz(sizeof(*seg));
472
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12883 times.
12883 if (!seg)
473 return AVERROR(ENOMEM);
474
475 12883 seg->graph = graph;
476
477 12883 graph_str += strspn(graph_str, WHITESPACES);
478
479 12883 ret = parse_sws_flags(&graph_str, &seg->scale_sws_opts, graph);
480
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12883 times.
12883 if (ret < 0)
481 goto fail;
482
483 12883 graph_str += strspn(graph_str, WHITESPACES);
484
485
2/2
✓ Branch 0 taken 13123 times.
✓ Branch 1 taken 12883 times.
26006 while (*graph_str) {
486 AVFilterChain *ch;
487
488 13123 ret = chain_parse(graph, &graph_str, &ch);
489
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13123 times.
13123 if (ret < 0)
490 goto fail;
491
492 13123 ret = av_dynarray_add_nofree(&seg->chains, &nb_chains, ch);
493
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13123 times.
13123 if (ret < 0) {
494 chain_free(&ch);
495 goto fail;
496 }
497 13123 seg->nb_chains = nb_chains;
498
499 13123 graph_str += strspn(graph_str, WHITESPACES);
500 }
501
502
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12883 times.
12883 if (!seg->nb_chains) {
503 av_log(graph, AV_LOG_ERROR, "No filters specified in the graph description\n");
504 ret = AVERROR(EINVAL);
505 goto fail;
506 }
507
508 12883 *pseg = seg;
509
510 12883 return 0;
511 fail:
512 avfilter_graph_segment_free(&seg);
513 return ret;
514 }
515
516 25766 int avfilter_graph_segment_create_filters(AVFilterGraphSegment *seg, int flags)
517 {
518 25766 size_t idx = 0;
519
520
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25766 times.
25766 if (flags)
521 return AVERROR(ENOSYS);
522
523
2/2
✓ Branch 0 taken 98 times.
✓ Branch 1 taken 25668 times.
25766 if (seg->scale_sws_opts) {
524 98 av_freep(&seg->graph->scale_sws_opts);
525 98 seg->graph->scale_sws_opts = av_strdup(seg->scale_sws_opts);
526
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 98 times.
98 if (!seg->graph->scale_sws_opts)
527 return AVERROR(ENOMEM);
528 }
529
530
2/2
✓ Branch 0 taken 26246 times.
✓ Branch 1 taken 25766 times.
52012 for (size_t i = 0; i < seg->nb_chains; i++) {
531 26246 AVFilterChain *ch = seg->chains[i];
532
533
2/2
✓ Branch 0 taken 51052 times.
✓ Branch 1 taken 26246 times.
77298 for (size_t j = 0; j < ch->nb_filters; j++) {
534 51052 AVFilterParams *p = ch->filters[j];
535 51052 const AVFilter *f = avfilter_get_by_name(p->filter_name);
536 char name[64];
537
538 // skip already processed filters
539
3/4
✓ Branch 0 taken 25526 times.
✓ Branch 1 taken 25526 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 25526 times.
51052 if (p->filter || !p->filter_name)
540 25526 continue;
541
542
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25526 times.
25526 if (!f) {
543 av_log(seg->graph, AV_LOG_ERROR,
544 "No such filter: '%s'\n", p->filter_name);
545 return AVERROR_FILTER_NOT_FOUND;
546 }
547
548
1/2
✓ Branch 0 taken 25526 times.
✗ Branch 1 not taken.
25526 if (!p->instance_name)
549 25526 snprintf(name, sizeof(name), "Parsed_%s_%zu", f->name, idx);
550 else
551 snprintf(name, sizeof(name), "%s@%s", f->name, p->instance_name);
552
553 25526 p->filter = avfilter_graph_alloc_filter(seg->graph, f, name);
554
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25526 times.
25526 if (!p->filter)
555 return AVERROR(ENOMEM);
556
557
4/4
✓ Branch 0 taken 6041 times.
✓ Branch 1 taken 19485 times.
✓ Branch 2 taken 2685 times.
✓ Branch 3 taken 3356 times.
25526 if (!strcmp(f->name, "scale") && seg->graph->scale_sws_opts) {
558 2685 int ret = av_set_options_string(p->filter, seg->graph->scale_sws_opts,
559 "=", ":");
560
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2685 times.
2685 if (ret < 0) {
561 avfilter_free(p->filter);
562 p->filter = NULL;
563 return ret;
564 }
565 }
566
567 25526 av_freep(&p->filter_name);
568 25526 av_freep(&p->instance_name);
569
570 25526 idx++;
571 }
572 }
573
574 25766 return 0;
575 }
576
577 static int fail_creation_pending(AVFilterGraphSegment *seg, const char *fn,
578 const char *func)
579 {
580 av_log(seg->graph, AV_LOG_ERROR,
581 "A creation-pending filter '%s' present in the segment. All filters "
582 "must be created or disabled before calling %s().\n", fn, func);
583 return AVERROR(EINVAL);
584 }
585
586 12926 int avfilter_graph_segment_apply_opts(AVFilterGraphSegment *seg, int flags)
587 {
588 12926 int ret, leftover_opts = 0;
589
590
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12926 times.
12926 if (flags)
591 return AVERROR(ENOSYS);
592
593
2/2
✓ Branch 0 taken 13170 times.
✓ Branch 1 taken 12926 times.
26096 for (size_t i = 0; i < seg->nb_chains; i++) {
594 13170 AVFilterChain *ch = seg->chains[i];
595
596
2/2
✓ Branch 0 taken 25597 times.
✓ Branch 1 taken 13170 times.
38767 for (size_t j = 0; j < ch->nb_filters; j++) {
597 25597 AVFilterParams *p = ch->filters[j];
598
599
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25597 times.
25597 if (p->filter_name)
600 return fail_creation_pending(seg, p->filter_name, __func__);
601
3/4
✓ Branch 0 taken 25597 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 25532 times.
✓ Branch 3 taken 65 times.
25597 if (!p->filter || !p->opts)
602 25532 continue;
603
604 65 ret = av_opt_set_dict2(p->filter, &p->opts, AV_OPT_SEARCH_CHILDREN);
605
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
65 if (ret < 0)
606 return ret;
607
608
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 65 times.
65 if (av_dict_count(p->opts))
609 leftover_opts = 1;
610 }
611 }
612
613
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12926 times.
12926 return leftover_opts ? AVERROR_OPTION_NOT_FOUND : 0;
614 }
615
616 12926 int avfilter_graph_segment_init(AVFilterGraphSegment *seg, int flags)
617 {
618
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12926 times.
12926 if (flags)
619 return AVERROR(ENOSYS);
620
621
2/2
✓ Branch 0 taken 13170 times.
✓ Branch 1 taken 12926 times.
26096 for (size_t i = 0; i < seg->nb_chains; i++) {
622 13170 AVFilterChain *ch = seg->chains[i];
623
624
2/2
✓ Branch 0 taken 25597 times.
✓ Branch 1 taken 13170 times.
38767 for (size_t j = 0; j < ch->nb_filters; j++) {
625 25597 AVFilterParams *p = ch->filters[j];
626 int ret;
627
628
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25597 times.
25597 if (p->filter_name)
629 return fail_creation_pending(seg, p->filter_name, __func__);
630
3/4
✓ Branch 0 taken 25597 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 71 times.
✓ Branch 4 taken 25526 times.
25597 if (!p->filter || fffilterctx(p->filter)->initialized)
631 71 continue;
632
633 25526 ret = avfilter_init_dict(p->filter, NULL);
634
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25526 times.
25526 if (ret < 0)
635 return ret;
636 }
637 }
638
639 12926 return 0;
640 }
641
642 static unsigned
643 499 find_linklabel(AVFilterGraphSegment *seg, const char *label,
644 int output, size_t idx_chain, size_t idx_filter,
645 AVFilterParams **pp)
646 {
647
2/2
✓ Branch 0 taken 1339 times.
✓ Branch 1 taken 307 times.
1646 for (; idx_chain < seg->nb_chains; idx_chain++) {
648 1339 AVFilterChain *ch = seg->chains[idx_chain];
649
650
2/2
✓ Branch 0 taken 1403 times.
✓ Branch 1 taken 1147 times.
2550 for (; idx_filter < ch->nb_filters; idx_filter++) {
651 1403 AVFilterParams *p = ch->filters[idx_filter];
652
2/2
✓ Branch 0 taken 444 times.
✓ Branch 1 taken 959 times.
1403 AVFilterPadParams **io = output ? p->outputs : p->inputs;
653
2/2
✓ Branch 0 taken 444 times.
✓ Branch 1 taken 959 times.
1403 unsigned nb_io = output ? p->nb_outputs : p->nb_inputs;
654 AVFilterLink **l;
655 unsigned nb_l;
656
657
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1403 times.
1403 if (!p->filter)
658 continue;
659
660
2/2
✓ Branch 0 taken 444 times.
✓ Branch 1 taken 959 times.
1403 l = output ? p->filter->outputs : p->filter->inputs;
661
2/2
✓ Branch 0 taken 444 times.
✓ Branch 1 taken 959 times.
1403 nb_l = output ? p->filter->nb_outputs : p->filter->nb_inputs;
662
663
2/2
✓ Branch 0 taken 1167 times.
✓ Branch 1 taken 1211 times.
2378 for (unsigned i = 0; i < FFMIN(nb_io, nb_l); i++)
664
5/6
✓ Branch 0 taken 1046 times.
✓ Branch 1 taken 121 times.
✓ Branch 2 taken 1046 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 192 times.
✓ Branch 5 taken 854 times.
1167 if (!l[i] && io[i]->label && !strcmp(io[i]->label, label)) {
665 192 *pp = p;
666 192 return i;
667 }
668 }
669
670 1147 idx_filter = 0;
671 }
672
673 307 *pp = NULL;
674 307 return 0;
675 }
676
677 25853 static int inout_add(AVFilterInOut **inouts, AVFilterContext *f, unsigned pad_idx,
678 const char *label)
679 {
680 25853 AVFilterInOut *io = av_mallocz(sizeof(*io));
681
682
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25853 times.
25853 if (!io)
683 return AVERROR(ENOMEM);
684
685 25853 io->filter_ctx = f;
686 25853 io->pad_idx = pad_idx;
687
688
2/2
✓ Branch 0 taken 307 times.
✓ Branch 1 taken 25546 times.
25853 if (label) {
689 307 io->name = av_strdup(label);
690
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 307 times.
307 if (!io->name) {
691 avfilter_inout_free(&io);
692 return AVERROR(ENOMEM);
693 }
694 }
695
696 25853 append_inout(inouts, &io);
697
698 25853 return 0;
699 }
700
701 25526 static int link_inputs(AVFilterGraphSegment *seg, size_t idx_chain,
702 size_t idx_filter, AVFilterInOut **inputs)
703 {
704 25526 AVFilterChain *ch = seg->chains[idx_chain];
705 25526 AVFilterParams *p = ch->filters[idx_filter];
706 25526 AVFilterContext *f = p->filter;
707
708 int ret;
709
710
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25526 times.
25526 if (f->nb_inputs < p->nb_inputs) {
711 av_log(seg->graph, AV_LOG_ERROR,
712 "More input link labels specified for filter '%s' than "
713 "it has inputs: %u > %d\n", f->filter->name,
714 p->nb_inputs, f->nb_inputs);
715 return AVERROR(EINVAL);
716 }
717
718
2/2
✓ Branch 0 taken 25465 times.
✓ Branch 1 taken 25526 times.
50991 for (unsigned in = 0; in < f->nb_inputs; in++) {
719
2/2
✓ Branch 0 taken 346 times.
✓ Branch 1 taken 25119 times.
25465 const char *label = (in < p->nb_inputs) ? p->inputs[in]->label : NULL;
720
721 // skip already linked inputs
722
2/2
✓ Branch 0 taken 12597 times.
✓ Branch 1 taken 12868 times.
25465 if (f->inputs[in])
723 12597 continue;
724
725
2/2
✓ Branch 0 taken 154 times.
✓ Branch 1 taken 12714 times.
12868 if (label) {
726 154 AVFilterParams *po = NULL;
727 154 unsigned idx = find_linklabel(seg, label, 1, idx_chain, idx_filter, &po);
728
729
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 154 times.
154 if (po) {
730 ret = avfilter_link(po->filter, idx, f, in);
731 if (ret < 0)
732 return ret;
733
734 continue;
735 }
736 }
737
738 12868 ret = inout_add(inputs, f, in, label);
739
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12868 times.
12868 if (ret < 0)
740 return ret;
741 }
742
743 25526 return 0;
744 }
745
746 25526 static int link_outputs(AVFilterGraphSegment *seg, size_t idx_chain,
747 size_t idx_filter, AVFilterInOut **outputs)
748 {
749 25526 AVFilterChain *ch = seg->chains[idx_chain];
750 25526 AVFilterParams *p = ch->filters[idx_filter];
751 25526 AVFilterContext *f = p->filter;
752
753 int ret;
754
755
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25526 times.
25526 if (f->nb_outputs < p->nb_outputs) {
756 av_log(seg->graph, AV_LOG_ERROR,
757 "More output link labels specified for filter '%s' than "
758 "it has outputs: %u > %d\n", f->filter->name,
759 p->nb_outputs, f->nb_outputs);
760 return AVERROR(EINVAL);
761 }
762
2/2
✓ Branch 0 taken 25582 times.
✓ Branch 1 taken 25526 times.
51108 for (unsigned out = 0; out < f->nb_outputs; out++) {
763
2/2
✓ Branch 0 taken 345 times.
✓ Branch 1 taken 25237 times.
25582 char *label = (out < p->nb_outputs) ? p->outputs[out]->label : NULL;
764
765 // skip already linked outputs
766
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25582 times.
25582 if (f->outputs[out])
767 continue;
768
769
2/2
✓ Branch 0 taken 345 times.
✓ Branch 1 taken 25237 times.
25582 if (label) {
770 345 AVFilterParams *po = NULL;
771 345 unsigned idx = find_linklabel(seg, label, 0, idx_chain, idx_filter, &po);
772
773
2/2
✓ Branch 0 taken 192 times.
✓ Branch 1 taken 153 times.
345 if (po) {
774 192 ret = avfilter_link(f, out, po->filter, idx);
775
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 192 times.
192 if (ret < 0)
776 return ret;
777
778 192 continue;
779 }
780 }
781
782 // if this output is unlabeled, try linking it to an unlabeled
783 // input in the next non-disabled filter in the chain
784
3/4
✓ Branch 0 taken 12405 times.
✓ Branch 1 taken 12985 times.
✓ Branch 2 taken 12405 times.
✗ Branch 3 not taken.
25390 for (size_t i = idx_filter + 1; i < ch->nb_filters && !label; i++) {
785 12405 AVFilterParams *p_next = ch->filters[i];
786
787
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12405 times.
12405 if (!p_next->filter)
788 continue;
789
790
1/2
✓ Branch 0 taken 12409 times.
✗ Branch 1 not taken.
12409 for (unsigned in = 0; in < p_next->filter->nb_inputs; in++) {
791
2/2
✓ Branch 0 taken 12405 times.
✓ Branch 1 taken 4 times.
12409 if (!p_next->filter->inputs[in] &&
792
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 12405 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
12405 (in >= p_next->nb_inputs || !p_next->inputs[in]->label)) {
793 12405 ret = avfilter_link(f, out, p_next->filter, in);
794
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12405 times.
12405 if (ret < 0)
795 return ret;
796
797 12405 goto cont;
798 }
799 }
800 break;
801 }
802
803 12985 ret = inout_add(outputs, f, out, label);
804
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12985 times.
12985 if (ret < 0)
805 return ret;
806
807 25390 cont:;
808 }
809
810 25526 return 0;
811 }
812
813 12883 int avfilter_graph_segment_link(AVFilterGraphSegment *seg, int flags,
814 AVFilterInOut **inputs,
815 AVFilterInOut **outputs)
816 {
817 int ret;
818
819 12883 *inputs = NULL;
820 12883 *outputs = NULL;
821
822
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12883 times.
12883 if (flags)
823 return AVERROR(ENOSYS);
824
825
2/2
✓ Branch 0 taken 13123 times.
✓ Branch 1 taken 12883 times.
26006 for (size_t idx_chain = 0; idx_chain < seg->nb_chains; idx_chain++) {
826 13123 AVFilterChain *ch = seg->chains[idx_chain];
827
828
2/2
✓ Branch 0 taken 25526 times.
✓ Branch 1 taken 13123 times.
38649 for (size_t idx_filter = 0; idx_filter < ch->nb_filters; idx_filter++) {
829 25526 AVFilterParams *p = ch->filters[idx_filter];
830
831
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25526 times.
25526 if (p->filter_name) {
832 ret = fail_creation_pending(seg, p->filter_name, __func__);
833 goto fail;
834 }
835
836
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25526 times.
25526 if (!p->filter)
837 continue;
838
839 25526 ret = link_inputs(seg, idx_chain, idx_filter, inputs);
840
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25526 times.
25526 if (ret < 0)
841 goto fail;
842
843 25526 ret = link_outputs(seg, idx_chain, idx_filter, outputs);
844
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25526 times.
25526 if (ret < 0)
845 goto fail;
846 }
847 }
848 12883 return 0;
849 fail:
850 avfilter_inout_free(inputs);
851 avfilter_inout_free(outputs);
852 return ret;
853 }
854
855 // print an error message if some options were not found
856 static void log_unknown_opt(const AVFilterGraphSegment *seg)
857 {
858 for (size_t i = 0; i < seg->nb_chains; i++) {
859 const AVFilterChain *ch = seg->chains[i];
860
861 for (size_t j = 0; j < ch->nb_filters; j++) {
862 const AVFilterParams *p = ch->filters[j];
863 const AVDictionaryEntry *e;
864
865 if (!p->filter)
866 continue;
867
868 e = av_dict_iterate(p->opts, NULL);
869
870 if (e) {
871 av_log(p->filter, AV_LOG_ERROR,
872 "Could not set non-existent option '%s' to value '%s'\n",
873 e->key, e->value);
874 return;
875 }
876 }
877 }
878
879 }
880
881 12883 int avfilter_graph_segment_apply(AVFilterGraphSegment *seg, int flags,
882 AVFilterInOut **inputs,
883 AVFilterInOut **outputs)
884 {
885 int ret;
886
887
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12883 times.
12883 if (flags)
888 return AVERROR(ENOSYS);
889
890 12883 ret = avfilter_graph_segment_create_filters(seg, 0);
891
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12883 times.
12883 if (ret < 0) {
892 av_log(seg->graph, AV_LOG_ERROR, "Error creating filters\n");
893 return ret;
894 }
895
896 12883 ret = avfilter_graph_segment_apply_opts(seg, 0);
897
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12883 times.
12883 if (ret < 0) {
898 if (ret == AVERROR_OPTION_NOT_FOUND)
899 log_unknown_opt(seg);
900 av_log(seg->graph, AV_LOG_ERROR, "Error applying filter options\n");
901 return ret;
902 }
903
904 12883 ret = avfilter_graph_segment_init(seg, 0);
905
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12883 times.
12883 if (ret < 0) {
906 av_log(seg->graph, AV_LOG_ERROR, "Error initializing filters\n");
907 return ret;
908 }
909
910 12883 ret = avfilter_graph_segment_link(seg, 0, inputs, outputs);
911
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12883 times.
12883 if (ret < 0) {
912 av_log(seg->graph, AV_LOG_ERROR, "Error linking filters\n");
913 return ret;
914 }
915
916 12883 return 0;
917 }
918
919 43 int avfilter_graph_parse_ptr(AVFilterGraph *graph, const char *filters,
920 AVFilterInOut **open_inputs_ptr, AVFilterInOut **open_outputs_ptr,
921 void *log_ctx)
922 {
923
1/2
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
43 AVFilterInOut *user_inputs = open_inputs_ptr ? *open_inputs_ptr : NULL;
924
1/2
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
43 AVFilterInOut *user_outputs = open_outputs_ptr ? *open_outputs_ptr : NULL;
925
926 43 AVFilterInOut *inputs = NULL, *outputs = NULL;
927 43 AVFilterGraphSegment *seg = NULL;
928 AVFilterChain *ch;
929 AVFilterParams *p;
930 int ret;
931
932 43 ret = avfilter_graph_segment_parse(graph, filters, 0, &seg);
933
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 43 times.
43 if (ret < 0)
934 goto end;
935
936 43 ret = avfilter_graph_segment_create_filters(seg, 0);
937
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 43 times.
43 if (ret < 0)
938 goto end;
939
940 43 ret = avfilter_graph_segment_apply_opts(seg, 0);
941
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 43 times.
43 if (ret < 0) {
942 if (ret == AVERROR_OPTION_NOT_FOUND)
943 log_unknown_opt(seg);
944 goto end;
945 }
946
947 43 ret = avfilter_graph_segment_init(seg, 0);
948
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 43 times.
43 if (ret < 0)
949 goto end;
950
951 /* First input pad, assume it is "[in]" if not specified */
952 43 p = seg->chains[0]->filters[0];
953
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 43 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
43 if (p->filter->nb_inputs == 1 && !p->inputs) {
954 const char *tmp = "[in]";
955
956 ret = linklabels_parse(graph, &tmp, &p->inputs, &p->nb_inputs);
957 if (ret < 0)
958 goto end;
959 }
960
961 /* Last output pad, assume it is "[out]" if not specified */
962 43 ch = seg->chains[seg->nb_chains - 1];
963 43 p = ch->filters[ch->nb_filters - 1];
964
3/4
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 37 times.
✓ Branch 3 taken 6 times.
43 if (p->filter->nb_outputs == 1 && !p->outputs) {
965 37 const char *tmp = "[out]";
966
967 37 ret = linklabels_parse(graph, &tmp, &p->outputs, &p->nb_outputs);
968
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 if (ret < 0)
969 goto end;
970 }
971
972 43 ret = avfilter_graph_segment_apply(seg, 0, &inputs, &outputs);
973 43 avfilter_graph_segment_free(&seg);
974
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 43 times.
43 if (ret < 0)
975 goto end;
976
977 // process user-supplied inputs/outputs
978
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 43 times.
43 while (inputs) {
979 AVFilterInOut *cur, *match = NULL;
980
981 cur = inputs;
982 inputs = cur->next;
983 cur->next = NULL;
984
985 if (cur->name)
986 match = extract_inout(cur->name, &user_outputs);
987
988 if (match) {
989 ret = avfilter_link(match->filter_ctx, match->pad_idx,
990 cur->filter_ctx, cur->pad_idx);
991 avfilter_inout_free(&match);
992 avfilter_inout_free(&cur);
993 if (ret < 0)
994 goto end;
995 } else
996 append_inout(&user_inputs, &cur);
997 }
998
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 43 times.
88 while (outputs) {
999 45 AVFilterInOut *cur, *match = NULL;
1000
1001 45 cur = outputs;
1002 45 outputs = cur->next;
1003 45 cur->next = NULL;
1004
1005
1/2
✓ Branch 0 taken 45 times.
✗ Branch 1 not taken.
45 if (cur->name)
1006 45 match = extract_inout(cur->name, &user_inputs);
1007
1008
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45 times.
45 if (match) {
1009 ret = avfilter_link(cur->filter_ctx, cur->pad_idx,
1010 match->filter_ctx, match->pad_idx);
1011 avfilter_inout_free(&match);
1012 avfilter_inout_free(&cur);
1013 if (ret < 0)
1014 goto end;
1015 } else
1016 45 append_inout(&user_outputs, &cur);
1017 }
1018
1019 43 end:
1020 43 avfilter_graph_segment_free(&seg);
1021
1022
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 43 times.
43 if (ret < 0) {
1023 av_log(graph, AV_LOG_ERROR, "Error processing filtergraph: %s\n",
1024 av_err2str(ret));
1025
1026 while (graph->nb_filters)
1027 avfilter_free(graph->filters[0]);
1028 av_freep(&graph->filters);
1029 }
1030
1031 /* clear open_in/outputs only if not passed as parameters */
1032
1/2
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
43 if (open_inputs_ptr) *open_inputs_ptr = user_inputs;
1033 else avfilter_inout_free(&user_inputs);
1034
1/2
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
43 if (open_outputs_ptr) *open_outputs_ptr = user_outputs;
1035 else avfilter_inout_free(&user_outputs);
1036
1037 43 avfilter_inout_free(&inputs);
1038 43 avfilter_inout_free(&outputs);
1039
1040 43 return ret;
1041 }
1042