Commited in SoC by Bobby Bingham on 2007-08-14 22:27:05 Originally committed as revision 12700 to svn://svn.ffmpeg.org/ffmpeg/trunk
Showing the changes to libavfilter/avfiltergraph.c (1 of 2 files). Show all
Unified · Split
| @@ -26,6 +26,8 @@ · 25 unmodified lines | |||
| 26 | #include "avfilter.h" | 26 | #include "avfilter.h" |
| 27 | #include "avfiltergraph.h" | 27 | #include "avfiltergraph.h" |
| 28 | 28 | ||
| 29 | #include "allfilters.h" | ||
| 30 | |||
| 29 | typedef struct AVFilterGraph { | 31 | typedef struct AVFilterGraph { |
| 30 | unsigned filter_count; | 32 | unsigned filter_count; |
| 31 | AVFilterContext **filters; | 33 | AVFilterContext **filters; |
| @@ -376,6 +378,7 @@ static void uninit(AVFilterContext *ctx) · 344 unmodified lines | |||
| 376 | av_freep(&graph->filters); | 378 | av_freep(&graph->filters); |
| 377 | } | 379 | } |
| 378 | 380 | ||
| 381 | /* TODO: insert in sorted order */ | ||
| 379 | void avfilter_graph_add_filter(AVFilterContext *graphctx, AVFilterContext *filter) | 382 | void avfilter_graph_add_filter(AVFilterContext *graphctx, AVFilterContext *filter) |
| 380 | { | 383 | { |
| 381 | GraphContext *graph = graphctx->priv; | 384 | GraphContext *graph = graphctx->priv; |
| @@ -385,15 +388,38 @@ void avfilter_graph_add_filter(AVFilterContext *graphctx, AVFilterContext *filte · 3 unmodified lines | |||
| 385 | graph->filters[graph->filter_count - 1] = filter; | 388 | graph->filters[graph->filter_count - 1] = filter; |
| 386 | } | 389 | } |
| 387 | 390 | ||
| 391 | /* search intelligently, once we insert in order */ | ||
| 392 | AVFilterContext *avfilter_graph_get_filter(AVFilterContext *ctx, char *name) | ||
| 393 | { | ||
| 394 | GraphContext *graph = ctx->priv; | ||
| 395 | int i; | ||
| 396 | |||
| 397 | if(!name) | ||
| 398 | return NULL; | ||
| 399 | |||
| 400 | for(i = 0; i < graph->filter_count; i ++) | ||
| 401 | if(graph->filters[i]->name && !strcmp(name, graph->filters[i]->name)) | ||
| 402 | return graph->filters[i]; | ||
| 403 | |||
| 404 | return NULL; | ||
| 405 | } | ||
| 406 | |||
| 388 | int avfilter_graph_config_links(AVFilterContext *graphctx) | 407 | int avfilter_graph_config_links(AVFilterContext *graphctx) |
| 389 | { | 408 | { |
| 390 | GraphContext *graph = graphctx->priv; | 409 | GraphContext *graph = graphctx->priv; |
| 391 | int i, j; | 410 | int i, j; |
| 392 | 411 | ||
| 393 | for(i = 0; i < graph->filter_count; i ++) { | 412 | for(i = 0; i < graph->filter_count; i ++) { |
| 394 | for(j = 0; j < graph->filters[i]->input_count; j ++) | 413 | for(j = 0; j < graph->filters[i]->input_count; j ++) { |
| 414 | /* ensure that graphs contained within graphs are configured */ | ||
| 415 | if((graph->filters[i]->filter == &vf_graph || | ||
| 416 | graph->filters[i]->filter == &vf_graphfile || | ||
| 417 | graph->filters[i]->filter == &vf_graphdesc) && | ||
| 418 | avfilter_graph_config_links(graph->filters[i])) | ||
| 419 | return -1; | ||
| 395 | if(avfilter_config_link(graph->filters[i]->inputs[j])) | 420 | if(avfilter_config_link(graph->filters[i]->inputs[j])) |
| 396 | return -1; | 421 | return -1; |
| 422 | } | ||
| 397 | } | 423 | } |
| 398 | 424 | ||
| 399 | return 0; | 425 | return 0; |
| @@ -542,3 +568,126 @@ AVFilter vf_graph = · 142 unmodified lines | |||
| 542 | .outputs = (AVFilterPad[]) {{ .name = NULL, }}, | 568 | .outputs = (AVFilterPad[]) {{ .name = NULL, }}, |
| 543 | }; | 569 | }; |
| 544 | 570 | ||
| 571 | static int graph_load_from_desc(AVFilterContext *ctx, AVFilterGraphDesc *desc) | ||
| 572 | { | ||
| 573 | AVFilterGraphDescFilter *curfilt; | ||
| 574 | AVFilterGraphDescLink *curlink; | ||
| 575 | AVFilterGraphDescExport *curpad; | ||
| 576 | AVFilterContext *filt, *filtb; | ||
| 577 | |||
| 578 | /* create all filters */ | ||
| 579 | for(curfilt = desc->filters; curfilt; curfilt = curfilt->next) { | ||
| 580 | if(!(filt = avfilter_create_by_name(curfilt->filter, curfilt->name))) { | ||
| 581 | av_log(ctx, AV_LOG_ERROR, "error creating filter\n"); | ||
| 582 | goto fail; | ||
| 583 | } | ||
| 584 | avfilter_graph_add_filter(ctx, filt); | ||
| 585 | if(avfilter_init_filter(filt, curfilt->args, NULL)) { | ||
| 586 | av_log(ctx, AV_LOG_ERROR, "error initializing filter\n"); | ||
| 587 | goto fail; | ||
| 588 | } | ||
| 589 | } | ||
| 590 | |||
| 591 | /* create all links */ | ||
| 592 | for(curlink = desc->links; curlink; curlink = curlink->next) { | ||
| 593 | if(!(filt = avfilter_graph_get_filter(ctx, curlink->src))) { | ||
| 594 | av_log(ctx, AV_LOG_ERROR, "link source does not exist in graph\n"); | ||
| 595 | goto fail; | ||
| 596 | } | ||
| 597 | if(!(filtb = avfilter_graph_get_filter(ctx, curlink->dst))) { | ||
| 598 | av_log(ctx, AV_LOG_ERROR, "link destination does not exist in graph\n"); | ||
| 599 | goto fail; | ||
| 600 | } | ||
| 601 | if(avfilter_link(filt, curlink->srcpad, filtb, curlink->dstpad)) { | ||
| 602 | av_log(ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n"); | ||
| 603 | goto fail; | ||
| 604 | } | ||
| 605 | } | ||
| 606 | |||
| 607 | /* export all input pads */ | ||
| 608 | for(curpad = desc->inputs; curpad; curpad = curpad->next) { | ||
| 609 | if(!(filt = avfilter_graph_get_filter(ctx, curpad->filter))) { | ||
| 610 | av_log(ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n"); | ||
| 611 | goto fail; | ||
| 612 | } | ||
| 613 | add_graph_input(ctx, filt, curpad->pad, curpad->name); | ||
| 614 | } | ||
| 615 | |||
| 616 | /* export all output pads */ | ||
| 617 | for(curpad = desc->outputs; curpad; curpad = curpad->next) { | ||
| 618 | if(!(filt = avfilter_graph_get_filter(ctx, curpad->filter))) { | ||
| 619 | av_log(ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n"); | ||
| 620 | goto fail; | ||
| 621 | } | ||
| 622 | add_graph_output(ctx, filt, curpad->pad, curpad->name); | ||
| 623 | } | ||
| 624 | |||
| 625 | return 0; | ||
| 626 | |||
| 627 | fail: | ||
| 628 | uninit(ctx); | ||
| 629 | return -1; | ||
| 630 | } | ||
| 631 | |||
| 632 | static int init_desc(AVFilterContext *ctx, const char *args, void *opaque) | ||
| 633 | { | ||
| 634 | GraphContext *gctx = ctx->priv; | ||
| 635 | |||
| 636 | if(!opaque) | ||
| 637 | return -1; | ||
| 638 | |||
| 639 | if(!(gctx->link_filter = avfilter_create(&vf_graph_dummy, NULL))) | ||
| 640 | return -1; | ||
| 641 | if(avfilter_init_filter(gctx->link_filter, NULL, ctx)) | ||
| 642 | goto fail; | ||
| 643 | |||
| 644 | return graph_load_from_desc(ctx, opaque); | ||
| 645 | |||
| 646 | fail: | ||
| 647 | avfilter_destroy(gctx->link_filter); | ||
| 648 | return -1; | ||
| 649 | } | ||
| 650 | |||
| 651 | AVFilter vf_graphdesc = | ||
| 652 | { | ||
| 653 | .name = "graph_desc", | ||
| 654 | .author = "Bobby Bingham", | ||
| 655 | |||
| 656 | .priv_size = sizeof(GraphContext), | ||
| 657 | |||
| 658 | .init = init_desc, | ||
| 659 | .uninit = uninit, | ||
| 660 | |||
| 661 | .inputs = (AVFilterPad[]) {{ .name = NULL, }}, | ||
| 662 | .outputs = (AVFilterPad[]) {{ .name = NULL, }}, | ||
| 663 | }; | ||
| 664 | |||
| 665 | static int init_file(AVFilterContext *ctx, const char *args, void *opaque) | ||
| 666 | { | ||
| 667 | AVFilterGraphDesc *desc; | ||
| 668 | int ret; | ||
| 669 | |||
| 670 | if(!args) | ||
| 671 | return -1; | ||
| 672 | if(!(desc = avfilter_graph_load_desc(args))) | ||
| 673 | return -1; | ||
| 674 | |||
| 675 | ret = init_desc(ctx, NULL, desc); | ||
| 676 | avfilter_graph_free_desc(desc); | ||
| 677 | return ret; | ||
| 678 | } | ||
| 679 | |||
| 680 | AVFilter vf_graphfile = | ||
| 681 | { | ||
| 682 | .name = "graph_file", | ||
| 683 | .author = "Bobby Bingham", | ||
| 684 | |||
| 685 | .priv_size = sizeof(GraphContext), | ||
| 686 | |||
| 687 | .init = init_file, | ||
| 688 | .uninit = uninit, | ||
| 689 | |||
| 690 | .inputs = (AVFilterPad[]) {{ .name = NULL, }}, | ||
| 691 | .outputs = (AVFilterPad[]) {{ .name = NULL, }}, | ||
| 692 | }; | ||
| 693 | |||