You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
3.4KB

  1. /*
  2. * Filter graphs
  3. * copyright (c) 2007 Bobby Bingham
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <string.h>
  22. #include <stddef.h>
  23. #include "avstring.h"
  24. #include "avfilter.h"
  25. #include "avfiltergraph.h"
  26. struct AVFilterGraph {
  27. unsigned filter_count;
  28. AVFilterContext **filters;
  29. };
  30. AVFilterGraph *avfilter_create_graph(void)
  31. {
  32. return av_mallocz(sizeof(AVFilterGraph));
  33. }
  34. static void destroy_graph_filters(AVFilterGraph *graph)
  35. {
  36. for(; graph->filter_count > 0; graph->filter_count --)
  37. avfilter_destroy(graph->filters[graph->filter_count - 1]);
  38. av_freep(&graph->filters);
  39. }
  40. void avfilter_destroy_graph(AVFilterGraph *graph)
  41. {
  42. destroy_graph_filters(graph);
  43. av_free(graph);
  44. }
  45. void avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
  46. {
  47. graph->filters = av_realloc(graph->filters,
  48. sizeof(AVFilterContext*) * ++graph->filter_count);
  49. graph->filters[graph->filter_count - 1] = filter;
  50. }
  51. static AVFilterContext *create_filter_with_args(const char *filt)
  52. {
  53. AVFilterContext *ret;
  54. char *filter = av_strdup(filt); /* copy - don't mangle the input string */
  55. char *name, *args;
  56. name = filter;
  57. if((args = strchr(filter, '='))) {
  58. /* ensure we at least have a name */
  59. if(args == filter)
  60. goto fail;
  61. *args ++ = 0;
  62. }
  63. av_log(NULL, AV_LOG_INFO, "creating filter \"%s\" with args \"%s\"\n",
  64. name, args ? args : "(none)");
  65. if((ret = avfilter_create_by_name(name, NULL))) {
  66. if(avfilter_init_filter(ret, args)) {
  67. av_log(NULL, AV_LOG_ERROR, "error initializing filter!\n");
  68. avfilter_destroy(ret);
  69. goto fail;
  70. }
  71. } else av_log(NULL, AV_LOG_ERROR, "error creating filter!\n");
  72. return ret;
  73. fail:
  74. av_free(filter);
  75. return NULL;
  76. }
  77. int avfilter_graph_load_chain(AVFilterGraph *graph,
  78. unsigned count, char **filter_list,
  79. AVFilterContext **first, AVFilterContext **last)
  80. {
  81. unsigned i;
  82. AVFilterContext *filters[2] = {NULL,NULL};
  83. for(i = 0; i < count; i ++) {
  84. if(!(filters[1] = create_filter_with_args(filter_list[i])))
  85. goto fail;
  86. if(i == 0) {
  87. if(first) *first = filters[1];
  88. } else {
  89. if(avfilter_link(filters[0], 0, filters[1], 0)) {
  90. av_log(NULL, AV_LOG_ERROR, "error linking filters!\n");
  91. goto fail;
  92. }
  93. }
  94. avfilter_graph_add_filter(graph, filters[1]);
  95. filters[0] = filters[1];
  96. }
  97. if(last) *last = filters[1];
  98. return 0;
  99. fail:
  100. destroy_graph_filters(graph);
  101. if(first) *first = NULL;
  102. if(last) *last = NULL;
  103. return -1;
  104. }