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.

197 lines
5.1KB

  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. typedef struct AVFilterGraph {
  27. unsigned filter_count;
  28. AVFilterContext **filters;
  29. } GraphContext;
  30. static void uninit(AVFilterContext *ctx)
  31. {
  32. GraphContext *graph = ctx->priv;
  33. for(; graph->filter_count > 0; graph->filter_count --)
  34. avfilter_destroy(graph->filters[graph->filter_count - 1]);
  35. av_freep(&graph->filters);
  36. }
  37. void avfilter_graph_add_filter(AVFilterContext *graphctx, AVFilterContext *filter)
  38. {
  39. GraphContext *graph = graphctx->priv;
  40. graph->filters = av_realloc(graph->filters,
  41. sizeof(AVFilterContext*) * ++graph->filter_count);
  42. graph->filters[graph->filter_count - 1] = filter;
  43. }
  44. int avfilter_graph_config_links(AVFilterContext *graphctx)
  45. {
  46. GraphContext *graph = graphctx->priv;
  47. int i, j;
  48. for(i = 0; i < graph->filter_count; i ++) {
  49. for(j = 0; j < graph->filters[i]->input_count; j ++)
  50. if(avfilter_config_link(graph->filters[i]->inputs[j]))
  51. return -1;
  52. }
  53. return 0;
  54. }
  55. static AVFilterContext *create_filter_with_args(const char *filt, void *opaque)
  56. {
  57. AVFilterContext *ret;
  58. char *filter = av_strdup(filt); /* copy - don't mangle the input string */
  59. char *name, *args;
  60. name = filter;
  61. if((args = strchr(filter, '='))) {
  62. /* ensure we at least have a name */
  63. if(args == filter)
  64. goto fail;
  65. *args ++ = 0;
  66. }
  67. av_log(NULL, AV_LOG_INFO, "creating filter \"%s\" with args \"%s\"\n",
  68. name, args ? args : "(none)");
  69. if((ret = avfilter_create_by_name(name, NULL))) {
  70. if(avfilter_init_filter(ret, args, opaque)) {
  71. av_log(NULL, AV_LOG_ERROR, "error initializing filter!\n");
  72. avfilter_destroy(ret);
  73. goto fail;
  74. }
  75. } else av_log(NULL, AV_LOG_ERROR, "error creating filter!\n");
  76. av_free(filter);
  77. return ret;
  78. fail:
  79. av_free(filter);
  80. return NULL;
  81. }
  82. static int graph_load_chain(AVFilterContext *graphctx,
  83. unsigned count, char **filter_list, void **opaque,
  84. AVFilterContext **first, AVFilterContext **last)
  85. {
  86. unsigned i;
  87. AVFilterContext *filters[2] = {NULL,NULL};
  88. for(i = 0; i < count; i ++) {
  89. void *op;
  90. if(opaque) op = opaque[i];
  91. else op = NULL;
  92. if(!(filters[1] = create_filter_with_args(filter_list[i], op)))
  93. goto fail;
  94. if(i == 0) {
  95. if(first) *first = filters[1];
  96. } else {
  97. if(avfilter_link(filters[0], 0, filters[1], 0)) {
  98. av_log(NULL, AV_LOG_ERROR, "error linking filters!\n");
  99. goto fail;
  100. }
  101. }
  102. avfilter_graph_add_filter(graphctx, filters[1]);
  103. filters[0] = filters[1];
  104. }
  105. if(last) *last = filters[1];
  106. return 0;
  107. fail:
  108. uninit(graphctx);
  109. if(first) *first = NULL;
  110. if(last) *last = NULL;
  111. return -1;
  112. }
  113. static int graph_load_chain_from_string(AVFilterContext *ctx, const char *str,
  114. AVFilterContext **first,
  115. AVFilterContext **last)
  116. {
  117. int count, ret = 0;
  118. char **strings;
  119. char *filt;
  120. strings = av_malloc(sizeof(char *));
  121. strings[0] = av_strdup(str);
  122. filt = strchr(strings[0], ',');
  123. for(count = 1; filt; count ++) {
  124. if(filt == strings[count-1]) {
  125. ret = -1;
  126. goto done;
  127. }
  128. strings = av_realloc(strings, sizeof(char *) * (count+1));
  129. strings[count] = filt + 1;
  130. *filt = '\0';
  131. filt = strchr(strings[count], ',');
  132. }
  133. ret = graph_load_chain(ctx, count, strings, NULL, first, last);
  134. done:
  135. av_free(strings[0]);
  136. av_free(strings);
  137. return ret;
  138. }
  139. static int init(AVFilterContext *ctx, const char *args, void *opaque)
  140. {
  141. AVFilterContext **filters = opaque;
  142. if(!args)
  143. return 0;
  144. if(!opaque)
  145. return -1;
  146. return graph_load_chain_from_string(ctx, args, filters, filters + 1);
  147. }
  148. AVFilter vf_graph =
  149. {
  150. .name = "graph",
  151. .author = "Bobby Bingham",
  152. .priv_size = sizeof(GraphContext),
  153. .init = init,
  154. .uninit = uninit,
  155. .inputs = (AVFilterPad[]) {{ .name = NULL, }},
  156. .outputs = (AVFilterPad[]) {{ .name = NULL, }},
  157. };