00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <ctype.h>
00024 #include <string.h>
00025
00026 #include "libavutil/avstring.h"
00027 #include "avfilter.h"
00028 #include "avfiltergraph.h"
00029
00030 #define WHITESPACES " \n\t"
00031
00037 static int link_filter(AVFilterContext *src, int srcpad,
00038 AVFilterContext *dst, int dstpad,
00039 void *log_ctx)
00040 {
00041 int ret;
00042 if ((ret = avfilter_link(src, srcpad, dst, dstpad))) {
00043 av_log(log_ctx, AV_LOG_ERROR,
00044 "Cannot create the link %s:%d -> %s:%d\n",
00045 src->filter->name, srcpad, dst->filter->name, dstpad);
00046 return ret;
00047 }
00048
00049 return 0;
00050 }
00051
00058 static char *parse_link_name(const char **buf, void *log_ctx)
00059 {
00060 const char *start = *buf;
00061 char *name;
00062 (*buf)++;
00063
00064 name = av_get_token(buf, "]");
00065
00066 if (!name[0]) {
00067 av_log(log_ctx, AV_LOG_ERROR,
00068 "Bad (empty?) label found in the following: \"%s\".\n", start);
00069 goto fail;
00070 }
00071
00072 if (*(*buf)++ != ']') {
00073 av_log(log_ctx, AV_LOG_ERROR,
00074 "Mismatched '[' found in the following: \"%s\".\n", start);
00075 fail:
00076 av_freep(&name);
00077 }
00078
00079 return name;
00080 }
00081
00094 static int create_filter(AVFilterContext **filt_ctx, AVFilterGraph *ctx, int index,
00095 const char *filt_name, const char *args, void *log_ctx)
00096 {
00097 AVFilter *filt;
00098 char inst_name[30];
00099 char tmp_args[256];
00100 int ret;
00101
00102 snprintf(inst_name, sizeof(inst_name), "Parsed_%s_%d", filt_name, index);
00103
00104 filt = avfilter_get_by_name(filt_name);
00105
00106 if (!filt) {
00107 av_log(log_ctx, AV_LOG_ERROR,
00108 "No such filter: '%s'\n", filt_name);
00109 return AVERROR(EINVAL);
00110 }
00111
00112 ret = avfilter_open(filt_ctx, filt, inst_name);
00113 if (!*filt_ctx) {
00114 av_log(log_ctx, AV_LOG_ERROR,
00115 "Error creating filter '%s'\n", filt_name);
00116 return ret;
00117 }
00118
00119 if ((ret = avfilter_graph_add_filter(ctx, *filt_ctx)) < 0) {
00120 avfilter_free(*filt_ctx);
00121 return ret;
00122 }
00123
00124 if (!strcmp(filt_name, "scale") && args && !strstr(args, "flags")) {
00125 snprintf(tmp_args, sizeof(tmp_args), "%s:%s",
00126 args, ctx->scale_sws_opts);
00127 args = tmp_args;
00128 }
00129
00130 if ((ret = avfilter_init_filter(*filt_ctx, args, NULL)) < 0) {
00131 av_log(log_ctx, AV_LOG_ERROR,
00132 "Error initializing filter '%s' with args '%s'\n", filt_name, args);
00133 return ret;
00134 }
00135
00136 return 0;
00137 }
00138
00155 static int parse_filter(AVFilterContext **filt_ctx, const char **buf, AVFilterGraph *graph,
00156 int index, void *log_ctx)
00157 {
00158 char *opts = NULL;
00159 char *name = av_get_token(buf, "=,;[\n");
00160 int ret;
00161
00162 if (**buf == '=') {
00163 (*buf)++;
00164 opts = av_get_token(buf, "[],;\n");
00165 }
00166
00167 ret = create_filter(filt_ctx, graph, index, name, opts, log_ctx);
00168 av_free(name);
00169 av_free(opts);
00170 return ret;
00171 }
00172
00173 AVFilterInOut *avfilter_inout_alloc(void)
00174 {
00175 return av_mallocz(sizeof(AVFilterInOut));
00176 }
00177
00178 void avfilter_inout_free(AVFilterInOut **inout)
00179 {
00180 while (*inout) {
00181 AVFilterInOut *next = (*inout)->next;
00182 av_freep(&(*inout)->name);
00183 av_freep(inout);
00184 *inout = next;
00185 }
00186 }
00187
00188 static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
00189 {
00190 AVFilterInOut *ret;
00191
00192 while (*links && (!(*links)->name || strcmp((*links)->name, label)))
00193 links = &((*links)->next);
00194
00195 ret = *links;
00196
00197 if (ret) {
00198 *links = ret->next;
00199 ret->next = NULL;
00200 }
00201
00202 return ret;
00203 }
00204
00205 static void insert_inout(AVFilterInOut **inouts, AVFilterInOut *element)
00206 {
00207 element->next = *inouts;
00208 *inouts = element;
00209 }
00210
00211 static void append_inout(AVFilterInOut **inouts, AVFilterInOut **element)
00212 {
00213 while (*inouts && (*inouts)->next)
00214 inouts = &((*inouts)->next);
00215
00216 if (!*inouts)
00217 *inouts = *element;
00218 else
00219 (*inouts)->next = *element;
00220 *element = NULL;
00221 }
00222
00223 static int link_filter_inouts(AVFilterContext *filt_ctx,
00224 AVFilterInOut **curr_inputs,
00225 AVFilterInOut **open_inputs, void *log_ctx)
00226 {
00227 int pad, ret;
00228
00229 for (pad = 0; pad < filt_ctx->input_count; pad++) {
00230 AVFilterInOut *p = *curr_inputs;
00231
00232 if (p) {
00233 *curr_inputs = (*curr_inputs)->next;
00234 p->next = NULL;
00235 } else if (!(p = av_mallocz(sizeof(*p))))
00236 return AVERROR(ENOMEM);
00237
00238 if (p->filter_ctx) {
00239 if ((ret = link_filter(p->filter_ctx, p->pad_idx, filt_ctx, pad, log_ctx)) < 0)
00240 return ret;
00241 av_free(p->name);
00242 av_free(p);
00243 } else {
00244 p->filter_ctx = filt_ctx;
00245 p->pad_idx = pad;
00246 append_inout(open_inputs, &p);
00247 }
00248 }
00249
00250 if (*curr_inputs) {
00251 av_log(log_ctx, AV_LOG_ERROR,
00252 "Too many inputs specified for the \"%s\" filter.\n",
00253 filt_ctx->filter->name);
00254 return AVERROR(EINVAL);
00255 }
00256
00257 pad = filt_ctx->output_count;
00258 while (pad--) {
00259 AVFilterInOut *currlinkn = av_mallocz(sizeof(AVFilterInOut));
00260 if (!currlinkn)
00261 return AVERROR(ENOMEM);
00262 currlinkn->filter_ctx = filt_ctx;
00263 currlinkn->pad_idx = pad;
00264 insert_inout(curr_inputs, currlinkn);
00265 }
00266
00267 return 0;
00268 }
00269
00270 static int parse_inputs(const char **buf, AVFilterInOut **curr_inputs,
00271 AVFilterInOut **open_outputs, void *log_ctx)
00272 {
00273 AVFilterInOut *parsed_inputs = NULL;
00274 int pad = 0;
00275
00276 while (**buf == '[') {
00277 char *name = parse_link_name(buf, log_ctx);
00278 AVFilterInOut *match;
00279
00280 if (!name)
00281 return AVERROR(EINVAL);
00282
00283
00284 match = extract_inout(name, open_outputs);
00285
00286 if (match) {
00287 av_free(name);
00288 } else {
00289
00290 if (!(match = av_mallocz(sizeof(AVFilterInOut))))
00291 return AVERROR(ENOMEM);
00292 match->name = name;
00293 match->pad_idx = pad;
00294 }
00295
00296 append_inout(&parsed_inputs, &match);
00297
00298 *buf += strspn(*buf, WHITESPACES);
00299 pad++;
00300 }
00301
00302 append_inout(&parsed_inputs, curr_inputs);
00303 *curr_inputs = parsed_inputs;
00304
00305 return pad;
00306 }
00307
00308 static int parse_outputs(const char **buf, AVFilterInOut **curr_inputs,
00309 AVFilterInOut **open_inputs,
00310 AVFilterInOut **open_outputs, void *log_ctx)
00311 {
00312 int ret, pad = 0;
00313
00314 while (**buf == '[') {
00315 char *name = parse_link_name(buf, log_ctx);
00316 AVFilterInOut *match;
00317
00318 AVFilterInOut *input = *curr_inputs;
00319 if (!input) {
00320 av_log(log_ctx, AV_LOG_ERROR,
00321 "No output pad can be associated to link label '%s'.\n",
00322 name);
00323 return AVERROR(EINVAL);
00324 }
00325 *curr_inputs = (*curr_inputs)->next;
00326
00327 if (!name)
00328 return AVERROR(EINVAL);
00329
00330
00331 match = extract_inout(name, open_inputs);
00332
00333 if (match) {
00334 if ((ret = link_filter(input->filter_ctx, input->pad_idx,
00335 match->filter_ctx, match->pad_idx, log_ctx)) < 0)
00336 return ret;
00337 av_free(match->name);
00338 av_free(name);
00339 av_free(match);
00340 av_free(input);
00341 } else {
00342
00343 input->name = name;
00344 insert_inout(open_outputs, input);
00345 }
00346 *buf += strspn(*buf, WHITESPACES);
00347 pad++;
00348 }
00349
00350 return pad;
00351 }
00352
00353 #if FF_API_GRAPH_AVCLASS
00354 #define log_ctx graph
00355 #else
00356 #define log_ctx NULL
00357 #endif
00358
00359 static int parse_sws_flags(const char **buf, AVFilterGraph *graph)
00360 {
00361 char *p = strchr(*buf, ';');
00362
00363 if (strncmp(*buf, "sws_flags=", 10))
00364 return 0;
00365
00366 if (!p) {
00367 av_log(log_ctx, AV_LOG_ERROR, "sws_flags not terminated with ';'.\n");
00368 return AVERROR(EINVAL);
00369 }
00370
00371 *buf += 4;
00372
00373 av_freep(&graph->scale_sws_opts);
00374 if (!(graph->scale_sws_opts = av_mallocz(p - *buf + 1)))
00375 return AVERROR(ENOMEM);
00376 av_strlcpy(graph->scale_sws_opts, *buf, p - *buf + 1);
00377
00378 *buf = p + 1;
00379 return 0;
00380 }
00381
00382 int avfilter_graph_parse2(AVFilterGraph *graph, const char *filters,
00383 AVFilterInOut **inputs,
00384 AVFilterInOut **outputs)
00385 {
00386 int index = 0, ret = 0;
00387 char chr = 0;
00388
00389 AVFilterInOut *curr_inputs = NULL, *open_inputs = NULL, *open_outputs = NULL;
00390
00391 filters += strspn(filters, WHITESPACES);
00392
00393 if ((ret = parse_sws_flags(&filters, graph)) < 0)
00394 goto fail;
00395
00396 do {
00397 AVFilterContext *filter;
00398 filters += strspn(filters, WHITESPACES);
00399
00400 if ((ret = parse_inputs(&filters, &curr_inputs, &open_outputs, log_ctx)) < 0)
00401 goto end;
00402
00403 if ((ret = parse_filter(&filter, &filters, graph, index, log_ctx)) < 0)
00404 goto end;
00405
00406 if ((ret = link_filter_inouts(filter, &curr_inputs, &open_inputs, log_ctx)) < 0)
00407 goto end;
00408
00409 if ((ret = parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
00410 log_ctx)) < 0)
00411 goto end;
00412
00413 filters += strspn(filters, WHITESPACES);
00414 chr = *filters++;
00415
00416 if (chr == ';' && curr_inputs)
00417 append_inout(&open_outputs, &curr_inputs);
00418 index++;
00419 } while (chr == ',' || chr == ';');
00420
00421 if (chr) {
00422 av_log(log_ctx, AV_LOG_ERROR,
00423 "Unable to parse graph description substring: \"%s\"\n",
00424 filters - 1);
00425 ret = AVERROR(EINVAL);
00426 goto end;
00427 }
00428
00429 append_inout(&open_outputs, &curr_inputs);
00430
00431
00432 *inputs = open_inputs;
00433 *outputs = open_outputs;
00434 return 0;
00435
00436 fail:end:
00437 for (; graph->filter_count > 0; graph->filter_count--)
00438 avfilter_free(graph->filters[graph->filter_count - 1]);
00439 av_freep(&graph->filters);
00440 avfilter_inout_free(&open_inputs);
00441 avfilter_inout_free(&open_outputs);
00442 avfilter_inout_free(&curr_inputs);
00443
00444 *inputs = NULL;
00445 *outputs = NULL;
00446
00447 return ret;
00448 }
00449 #undef log_ctx
00450
00451 int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
00452 AVFilterInOut **open_inputs_ptr, AVFilterInOut **open_outputs_ptr,
00453 void *log_ctx)
00454 {
00455 #if 0
00456 int ret;
00457 AVFilterInOut *open_inputs = open_inputs_ptr ? *open_inputs_ptr : NULL;
00458 AVFilterInOut *open_outputs = open_outputs_ptr ? *open_outputs_ptr : NULL;
00459 AVFilterInOut *cur, *match, *inputs = NULL, *outputs = NULL;
00460
00461 if ((ret = avfilter_graph_parse2(graph, filters, &inputs, &outputs)) < 0)
00462 goto fail;
00463
00464
00465 if (inputs && !inputs->name)
00466 inputs->name = av_strdup("in");
00467 for (cur = inputs; cur; cur = cur->next) {
00468 if (!cur->name) {
00469 av_log(log_ctx, AV_LOG_ERROR,
00470 "Not enough inputs specified for the \"%s\" filter.\n",
00471 cur->filter_ctx->filter->name);
00472 ret = AVERROR(EINVAL);
00473 goto fail;
00474 }
00475 if (!(match = extract_inout(cur->name, &open_outputs)))
00476 continue;
00477 ret = avfilter_link(match->filter_ctx, match->pad_idx,
00478 cur->filter_ctx, cur->pad_idx);
00479 avfilter_inout_free(&match);
00480 if (ret < 0)
00481 goto fail;
00482 }
00483
00484
00485 if (outputs && !outputs->name)
00486 outputs->name = av_strdup("out");
00487 for (cur = outputs; cur; cur = cur->next) {
00488 if (!cur->name) {
00489 av_log(log_ctx, AV_LOG_ERROR,
00490 "Invalid filterchain containing an unlabelled output pad: \"%s\"\n",
00491 filters);
00492 ret = AVERROR(EINVAL);
00493 goto fail;
00494 }
00495 if (!(match = extract_inout(cur->name, &open_inputs)))
00496 continue;
00497 ret = avfilter_link(cur->filter_ctx, cur->pad_idx,
00498 match->filter_ctx, match->pad_idx);
00499 avfilter_inout_free(&match);
00500 if (ret < 0)
00501 goto fail;
00502 }
00503
00504 fail:
00505 if (ret < 0) {
00506 for (; graph->filter_count > 0; graph->filter_count--)
00507 avfilter_free(graph->filters[graph->filter_count - 1]);
00508 av_freep(&graph->filters);
00509 }
00510 avfilter_inout_free(&inputs);
00511 avfilter_inout_free(&outputs);
00512
00513 if (open_inputs_ptr) *open_inputs_ptr = open_inputs;
00514 else avfilter_inout_free(&open_inputs);
00515 if (open_outputs_ptr) *open_outputs_ptr = open_outputs;
00516 else avfilter_inout_free(&open_outputs);
00517 return ret;
00518 }
00519 #else
00520 int index = 0, ret = 0;
00521 char chr = 0;
00522
00523 AVFilterInOut *curr_inputs = NULL;
00524 AVFilterInOut *open_inputs = open_inputs_ptr ? *open_inputs_ptr : NULL;
00525 AVFilterInOut *open_outputs = open_outputs_ptr ? *open_outputs_ptr : NULL;
00526
00527 do {
00528 AVFilterContext *filter;
00529 const char *filterchain = filters;
00530 filters += strspn(filters, WHITESPACES);
00531
00532 if ((ret = parse_inputs(&filters, &curr_inputs, &open_outputs, log_ctx)) < 0)
00533 goto end;
00534
00535 if ((ret = parse_filter(&filter, &filters, graph, index, log_ctx)) < 0)
00536 goto end;
00537
00538 if (filter->input_count == 1 && !curr_inputs && !index) {
00539
00540 const char *tmp = "[in]";
00541 if ((ret = parse_inputs(&tmp, &curr_inputs, &open_outputs, log_ctx)) < 0)
00542 goto end;
00543 }
00544
00545 if ((ret = link_filter_inouts(filter, &curr_inputs, &open_inputs, log_ctx)) < 0)
00546 goto end;
00547
00548 if ((ret = parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
00549 log_ctx)) < 0)
00550 goto end;
00551
00552 filters += strspn(filters, WHITESPACES);
00553 chr = *filters++;
00554
00555 if (chr == ';' && curr_inputs) {
00556 av_log(log_ctx, AV_LOG_ERROR,
00557 "Invalid filterchain containing an unlabelled output pad: \"%s\"\n",
00558 filterchain);
00559 ret = AVERROR(EINVAL);
00560 goto end;
00561 }
00562 index++;
00563 } while (chr == ',' || chr == ';');
00564
00565 if (chr) {
00566 av_log(log_ctx, AV_LOG_ERROR,
00567 "Unable to parse graph description substring: \"%s\"\n",
00568 filters - 1);
00569 ret = AVERROR(EINVAL);
00570 goto end;
00571 }
00572
00573 if (curr_inputs) {
00574
00575 const char *tmp = "[out]";
00576 if ((ret = parse_outputs(&tmp, &curr_inputs, &open_inputs, &open_outputs,
00577 log_ctx)) < 0)
00578 goto end;
00579 }
00580
00581 end:
00582
00583 if (open_inputs_ptr) *open_inputs_ptr = open_inputs;
00584 else avfilter_inout_free(&open_inputs);
00585 if (open_outputs_ptr) *open_outputs_ptr = open_outputs;
00586 else avfilter_inout_free(&open_outputs);
00587 avfilter_inout_free(&curr_inputs);
00588
00589 if (ret < 0) {
00590 for (; graph->filter_count > 0; graph->filter_count--)
00591 avfilter_free(graph->filters[graph->filter_count - 1]);
00592 av_freep(&graph->filters);
00593 }
00594 return ret;
00595 }
00596
00597 #endif