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
29
30 #define WHITESPACES " \n\t"
31
32 /**
33 * Link two filters together.
34 *
35 * @see avfilter_link()
36 */
39 void *log_ctx)
40 {
41 int ret;
44 "Cannot create the link %s:%d -> %s:%d\n",
46 return ret;
47 }
48
49 return 0;
50 }
51
52 /**
53 * Parse the name of a link, which has the format "[linkname]".
54 *
55 * @return a pointer (that need to be freed after use) to the name
56 * between parenthesis
57 */
59 {
62 (*buf)++;
63
65 if (!name)
67
68 if (!name[0]) {
70 "Bad (empty?) label found in the following: \"%s\".\n", start);
72 }
73
74 if (*(*buf)++ != ']') {
76 "Mismatched '[' found in the following: \"%s\".\n", start);
79 }
80
82 }
83
84 /**
85 * Create an instance of a filter, initialize and insert it in the
86 * filtergraph in *ctx.
87 *
88 * @param filt_ctx put here a filter context in case of successful creation and configuration, NULL otherwise.
89 * @param ctx the filtergraph context
90 * @param index an index which is supposed to be unique for each filter instance added to the filtergraph
91 * @param filt_name the name of the filter to create
92 * @param args the arguments provided to the filter during its initialization
93 * @param log_ctx the log context to use
94 * @return >= 0 in case of success, a negative AVERROR code otherwise
95 */
97 const char *filt_name,
const char *
args,
void *log_ctx)
98 {
100 char inst_name[30];
101 char *tmp_args =
NULL;
102 int ret;
103
104 snprintf(inst_name,
sizeof(inst_name),
"Parsed_%s_%d", filt_name, index);
105
107
108 if (!filt) {
110 "No such filter: '%s'\n", filt_name);
112 }
113
115 if (!*filt_ctx) {
117 "Error creating filter '%s'\n", filt_name);
119 }
120
121 if (!strcmp(filt_name, "scale") && (!args || !strstr(args, "flags")) &&
123 if (args) {
126 if (!tmp_args)
128 args = tmp_args;
129 } else
131 }
132
134 if (ret < 0) {
136 "Error initializing filter '%s'", filt_name);
137 if (args)
142 }
143
145 return ret;
146 }
147
148 /**
149 * Parse a string of the form FILTER_NAME[=PARAMS], and create a
150 * corresponding filter instance which is added to graph with
151 * create_filter().
152 *
153 * @param filt_ctx Pointer that is set to the created and configured filter
154 * context on success, set to NULL on failure.
155 * @param filt_ctx put here a pointer to the created filter context on
156 * success, NULL otherwise
157 * @param buf pointer to the buffer to parse, *buf will be updated to
158 * point to the char next after the parsed string
159 * @param index an index which is assigned to the created filter
160 * instance, and which is supposed to be unique for each filter
161 * instance added to the filtergraph
162 * @return >= 0 in case of success, a negative AVERROR code otherwise
163 */
165 int index,
void *log_ctx)
166 {
169 int ret;
170
171 if (**buf == '=') {
172 (*buf)++;
174 }
175
176 ret =
create_filter(filt_ctx, graph, index, name, opts, log_ctx);
179 return ret;
180 }
181
183 {
185 }
186
188 {
189 while (*inout) {
193 *inout = next;
194 }
195 }
196
198 {
200
201 while (*links && (!(*links)->name || strcmp((*links)->name, label)))
202 links = &((*links)->next);
203
204 ret = *links;
205
206 if (ret) {
209 }
210
211 return ret;
212 }
213
215 {
216 element->
next = *inouts;
217 *inouts = element;
218 }
219
221 {
222 while (*inouts && (*inouts)->
next)
223 inouts = &((*inouts)->next);
224
225 if (!*inouts)
226 *inouts = *element;
227 else
228 (*inouts)->
next = *element;
230 }
231
235 {
236 int pad, ret;
237
238 for (pad = 0; pad < filt_ctx->
nb_inputs; pad++) {
240
241 if (p) {
242 *curr_inputs = (*curr_inputs)->
next;
246
251 if (ret < 0)
252 return ret;
253 } else {
257 }
258 }
259
260 if (*curr_inputs) {
262 "Too many inputs specified for the \"%s\" filter.\n",
265 }
266
268 while (pad--) {
270 if (!currlinkn)
275 }
276
277 return 0;
278 }
279
282 {
284 int pad = 0;
285
286 while (**buf == '[') {
289
290 if (!name)
292
293 /* First check if the label is not in the open_outputs list */
295
296 if (match) {
298 } else {
299 /* Not in the list, so add it as an input */
303 }
306 }
307
309
311 pad++;
312 }
313
315 *curr_inputs = parsed_inputs;
316
317 return pad;
318 }
319
323 {
324 int ret, pad = 0;
325
326 while (**buf == '[') {
329
331
332 if (!name)
334
335 if (!input) {
337 "No output pad can be associated to link label '%s'.\n", name);
340 }
341 *curr_inputs = (*curr_inputs)->
next;
342
343 /* First check if the label is not in the open_inputs list */
345
346 if (match) {
350 return ret;
351 }
356 } else {
357 /* Not in the list, so add the first input as a open_output */
360 }
362 pad++;
363 }
364
365 return pad;
366 }
367
369 {
370 char *p = strchr(*buf, ';');
371
372 if (strncmp(*buf, "sws_flags=", 10))
373 return 0;
374
375 if (!p) {
378 }
379
380 *buf += 4; // keep the 'flags=' part
381
386
387 *buf = p + 1;
388 return 0;
389 }
390
394 {
395 int index = 0, ret = 0;
396 char chr = 0;
397
399
401
404
405 do {
408
409 if ((ret =
parse_inputs(&filters, &curr_inputs, &open_outputs, graph)) < 0)
411 if ((ret =
parse_filter(&filter, &filters, graph, index, graph)) < 0)
413
414
417
418 if ((ret =
parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
419 graph)) < 0)
421
423 chr = *filters++;
424
425 if (chr == ';' && curr_inputs)
427 index++;
428 } while (chr == ',' || chr == ';');
429
430 if (chr) {
432 "Unable to parse graph description substring: \"%s\"\n",
433 filters - 1);
436 }
437
439
440
441 *inputs = open_inputs;
442 *outputs = open_outputs;
443 return 0;
444
452
455
456 return ret;
457 }
458
462 {
463 int ret;
465
468
469 /* First input can be omitted if it is "[in]" */
470 if (inputs && !inputs->
name)
472 for (cur = inputs; cur; cur = cur->
next) {
475 "Not enough inputs specified for the \"%s\" filter.\n",
479 }
481 continue;
485 if (ret < 0)
487 }
488
489 /* Last output can be omitted if it is "[out]" */
490 if (outputs && !outputs->name)
492 for (cur = outputs; cur; cur = cur->
next) {
495 "Invalid filterchain containing an unlabelled output pad: \"%s\"\n",
496 filters);
499 }
501 continue;
505 if (ret < 0)
507 }
508
510 if (ret < 0) {
514 }
519 return ret;
520 }
521
524 void *log_ctx)
525 {
526 int index = 0, ret = 0;
527 char chr = 0;
528
532
535
536 do {
538 const char *filterchain =
filters;
540
541 if ((ret =
parse_inputs(&filters, &curr_inputs, &open_outputs, log_ctx)) < 0)
543
544 if ((ret =
parse_filter(&filter, &filters, graph, index, log_ctx)) < 0)
546
547 if (filter->
nb_inputs == 1 && !curr_inputs && !index) {
548 /* First input pad, assume it is "[in]" if not specified */
549 const char *tmp = "[in]";
550 if ((ret =
parse_inputs(&tmp, &curr_inputs, &open_outputs, log_ctx)) < 0)
552 }
553
556
557 if ((ret =
parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
558 log_ctx)) < 0)
560
562 chr = *filters++;
563
564 if (chr == ';' && curr_inputs) {
566 "Invalid filterchain containing an unlabelled output pad: \"%s\"\n",
567 filterchain);
570 }
571 index++;
572 } while (chr == ',' || chr == ';');
573
574 if (chr) {
576 "Unable to parse graph description substring: \"%s\"\n",
577 filters - 1);
580 }
581
582 if (curr_inputs) {
583 /* Last output pad, assume it is "[out]" if not specified */
584 const char *tmp = "[out]";
585 if ((ret =
parse_outputs(&tmp, &curr_inputs, &open_inputs, &open_outputs,
586 log_ctx)) < 0)
588 }
589
591 /* clear open_in/outputs only if not passed as parameters */
592 if (open_inputs_ptr) *open_inputs_ptr = open_inputs;
594 if (open_outputs_ptr) *open_outputs_ptr = open_outputs;
597
598 if (ret < 0) {
602 }
603 return ret;
604 }
AVFilterContext ** filters
void avfilter_free(AVFilterContext *filter)
Free a filter context.
Main libavfilter public API header.
memory handling functions
void avfilter_inout_free(AVFilterInOut **inout)
Free the supplied list of AVFilterInOut and set *inout to NULL.
struct AVFilterInOut * next
next input/input in the list, NULL if this is the last
char * scale_sws_opts
sws options to use for the auto-inserted scale filters
int avfilter_link(AVFilterContext *src, unsigned srcpad, AVFilterContext *dst, unsigned dstpad)
Link two filters together.
static av_cold int end(AVCodecContext *avctx)
static void filter(int16_t *output, ptrdiff_t out_stride, int16_t *low, ptrdiff_t low_stride, int16_t *high, ptrdiff_t high_stride, int len, uint8_t clip)
static void insert_inout(AVFilterInOut **inouts, AVFilterInOut *element)
int avfilter_graph_parse2(AVFilterGraph *graph, const char *filters, AVFilterInOut **inputs, AVFilterInOut **outputs)
Add a graph described by a string to a graph.
static int parse_inputs(const char **buf, AVFilterInOut **curr_inputs, AVFilterInOut **open_outputs, void *log_ctx)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static AVFilterInOut * extract_inout(const char *label, AVFilterInOut **links)
unsigned nb_outputs
number of output pads
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
char * av_get_token(const char **buf, const char *term)
Unescape the given string until a non escaped terminating char, and return the token corresponding to...
const AVFilter * avfilter_get_by_name(const char *name)
Get a filter definition matching the given name.
char * av_asprintf(const char *fmt,...)
unsigned nb_inputs
number of input pads
static const AVFilterPad outputs[]
AVFilterContext * filter_ctx
filter context associated to this input/output
int avfilter_init_str(AVFilterContext *filter, const char *args)
Initialize a filter with the supplied parameters.
A linked-list of the inputs/outputs of the filter chain.
static const AVFilterPad inputs[]
char * av_strdup(const char *s)
Duplicate the string s.
static int parse_sws_flags(const char **buf, AVFilterGraph *graph)
static void append_inout(AVFilterInOut **inouts, AVFilterInOut **element)
int avfilter_graph_parse(AVFilterGraph *graph, const char *filters, AVFilterInOut *open_inputs, AVFilterInOut *open_outputs, void *log_ctx)
Add a graph described by a string to a graph.
int pad_idx
index of the filt_ctx pad to use for linking
const char * name
Filter name.
static int parse_filter(AVFilterContext **filt_ctx, const char **buf, AVFilterGraph *graph, int index, void *log_ctx)
Parse a string of the form FILTER_NAME[=PARAMS], and create a corresponding filter instance which is ...
char * name
unique name for this input/output in the list
static const int8_t filt[NUMTAPS]
static int parse_outputs(const char **buf, AVFilterInOut **curr_inputs, AVFilterInOut **open_inputs, AVFilterInOut **open_outputs, void *log_ctx)
AVFilterInOut * avfilter_inout_alloc(void)
Allocate a single AVFilterInOut entry.
static int create_filter(AVFilterContext **filt_ctx, AVFilterGraph *ctx, int index, const char *filt_name, const char *args, void *log_ctx)
Create an instance of a filter, initialize and insert it in the filtergraph in *ctx.
static int link_filter(AVFilterContext *src, int srcpad, AVFilterContext *dst, int dstpad, void *log_ctx)
Link two filters together.
AVFilterContext * avfilter_graph_alloc_filter(AVFilterGraph *graph, const AVFilter *filter, const char *name)
Create a new filter instance in a filter graph.
static const struct PPFilter filters[]
static char * parse_link_name(const char **buf, void *log_ctx)
Parse the name of a link, which has the format "[linkname]".
static int link_filter_inouts(AVFilterContext *filt_ctx, AVFilterInOut **curr_inputs, AVFilterInOut **open_inputs, void *log_ctx)
int avfilter_graph_parse_ptr(AVFilterGraph *graph, const char *filters, AVFilterInOut **open_inputs_ptr, AVFilterInOut **open_outputs_ptr, void *log_ctx)
Add a graph described by a string to a graph.
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
const AVFilter * filter
the AVFilter of which this is an instance