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