PageRenderTime 28ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/release/src/router/ffmpeg/libavfilter/graphparser.c

https://gitlab.com/envieidoc/tomato
C | 357 lines | 256 code | 66 blank | 35 comment | 46 complexity | 98e70cd3148ea4d0afd4146007005f96 MD5 | raw file
  1. /*
  2. * filter graph parser
  3. * copyright (c) 2008 Vitor Sessak
  4. * copyright (c) 2007 Bobby Bingham
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include <ctype.h>
  23. #include <string.h>
  24. #include "graphparser.h"
  25. #include "avfilter.h"
  26. #include "avfiltergraph.h"
  27. #include "parseutils.h"
  28. #define WHITESPACES " \n\t"
  29. static int link_filter(AVFilterContext *src, int srcpad,
  30. AVFilterContext *dst, int dstpad,
  31. AVClass *log_ctx)
  32. {
  33. if(avfilter_link(src, srcpad, dst, dstpad)) {
  34. av_log(log_ctx, AV_LOG_ERROR,
  35. "cannot create the link %s:%d -> %s:%d\n",
  36. src->filter->name, srcpad, dst->filter->name, dstpad);
  37. return -1;
  38. }
  39. return 0;
  40. }
  41. /**
  42. * Parse "[linkname]"
  43. * @param name a pointer (that need to be free'd after use) to the name between
  44. * parenthesis
  45. */
  46. static char *parse_link_name(const char **buf, AVClass *log_ctx)
  47. {
  48. const char *start = *buf;
  49. char *name;
  50. (*buf)++;
  51. name = av_get_token(buf, "]");
  52. if(!name[0]) {
  53. av_log(log_ctx, AV_LOG_ERROR,
  54. "Bad (empty?) label found in the following: \"%s\".\n", start);
  55. goto fail;
  56. }
  57. if(*(*buf)++ != ']') {
  58. av_log(log_ctx, AV_LOG_ERROR,
  59. "Mismatched '[' found in the following: \"%s\".\n", start);
  60. fail:
  61. av_freep(&name);
  62. }
  63. return name;
  64. }
  65. static AVFilterContext *create_filter(AVFilterGraph *ctx, int index,
  66. const char *filt_name, const char *args,
  67. AVClass *log_ctx)
  68. {
  69. AVFilterContext *filt_ctx;
  70. AVFilter *filt;
  71. char inst_name[30];
  72. snprintf(inst_name, sizeof(inst_name), "Parsed filter %d", index);
  73. filt = avfilter_get_by_name(filt_name);
  74. if(!filt) {
  75. av_log(log_ctx, AV_LOG_ERROR,
  76. "no such filter: '%s'\n", filt_name);
  77. return NULL;
  78. }
  79. filt_ctx = avfilter_open(filt, inst_name);
  80. if(!filt_ctx) {
  81. av_log(log_ctx, AV_LOG_ERROR,
  82. "error creating filter '%s'\n", filt_name);
  83. return NULL;
  84. }
  85. if(avfilter_graph_add_filter(ctx, filt_ctx) < 0) {
  86. avfilter_destroy(filt_ctx);
  87. return NULL;
  88. }
  89. if(avfilter_init_filter(filt_ctx, args, NULL)) {
  90. av_log(log_ctx, AV_LOG_ERROR,
  91. "error initializing filter '%s' with args '%s'\n", filt_name, args);
  92. return NULL;
  93. }
  94. return filt_ctx;
  95. }
  96. /**
  97. * Parse "filter=params"
  98. */
  99. static AVFilterContext *parse_filter(const char **buf, AVFilterGraph *graph,
  100. int index, AVClass *log_ctx)
  101. {
  102. char *opts = NULL;
  103. char *name = av_get_token(buf, "=,;[\n");
  104. AVFilterContext *ret;
  105. if(**buf == '=') {
  106. (*buf)++;
  107. opts = av_get_token(buf, "[],;\n");
  108. }
  109. ret = create_filter(graph, index, name, opts, log_ctx);
  110. av_free(name);
  111. av_free(opts);
  112. return ret;
  113. }
  114. static void free_inout(AVFilterInOut *head)
  115. {
  116. while(head) {
  117. AVFilterInOut *next = head->next;
  118. av_free(head->name);
  119. av_free(head);
  120. head = next;
  121. }
  122. }
  123. static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
  124. {
  125. AVFilterInOut *ret;
  126. while(*links && strcmp((*links)->name, label))
  127. links = &((*links)->next);
  128. ret = *links;
  129. if(ret)
  130. *links = ret->next;
  131. return ret;
  132. }
  133. static void insert_inout(AVFilterInOut **inouts, AVFilterInOut *element)
  134. {
  135. element->next = *inouts;
  136. *inouts = element;
  137. }
  138. static int link_filter_inouts(AVFilterContext *filter,
  139. AVFilterInOut **curr_inputs,
  140. AVFilterInOut **open_inputs, AVClass *log_ctx)
  141. {
  142. int pad = filter->input_count;
  143. while(pad--) {
  144. AVFilterInOut *p = *curr_inputs;
  145. if(!p) {
  146. av_log(log_ctx, AV_LOG_ERROR,
  147. "Not enough inputs specified for the \"%s\" filter.\n",
  148. filter->filter->name);
  149. return -1;
  150. }
  151. *curr_inputs = (*curr_inputs)->next;
  152. if(p->filter) {
  153. if(link_filter(p->filter, p->pad_idx, filter, pad, log_ctx))
  154. return -1;
  155. av_free(p->name);
  156. av_free(p);
  157. } else {
  158. p->filter = filter;
  159. p->pad_idx = pad;
  160. insert_inout(open_inputs, p);
  161. }
  162. }
  163. if(*curr_inputs) {
  164. av_log(log_ctx, AV_LOG_ERROR,
  165. "Too many inputs specified for the \"%s\" filter.\n",
  166. filter->filter->name);
  167. return -1;
  168. }
  169. pad = filter->output_count;
  170. while(pad--) {
  171. AVFilterInOut *currlinkn = av_mallocz(sizeof(AVFilterInOut));
  172. currlinkn->filter = filter;
  173. currlinkn->pad_idx = pad;
  174. insert_inout(curr_inputs, currlinkn);
  175. }
  176. return 0;
  177. }
  178. static int parse_inputs(const char **buf, AVFilterInOut **curr_inputs,
  179. AVFilterInOut **open_outputs, AVClass *log_ctx)
  180. {
  181. int pad = 0;
  182. while(**buf == '[') {
  183. char *name = parse_link_name(buf, log_ctx);
  184. AVFilterInOut *match;
  185. if(!name)
  186. return -1;
  187. /* First check if the label is not in the open_outputs list */
  188. match = extract_inout(name, open_outputs);
  189. if(match) {
  190. av_free(name);
  191. } else {
  192. /* Not in the list, so add it as an input */
  193. match = av_mallocz(sizeof(AVFilterInOut));
  194. match->name = name;
  195. match->pad_idx = pad;
  196. }
  197. insert_inout(curr_inputs, match);
  198. *buf += strspn(*buf, WHITESPACES);
  199. pad++;
  200. }
  201. return pad;
  202. }
  203. static int parse_outputs(const char **buf, AVFilterInOut **curr_inputs,
  204. AVFilterInOut **open_inputs,
  205. AVFilterInOut **open_outputs, AVClass *log_ctx)
  206. {
  207. int pad = 0;
  208. while(**buf == '[') {
  209. char *name = parse_link_name(buf, log_ctx);
  210. AVFilterInOut *match;
  211. AVFilterInOut *input = *curr_inputs;
  212. *curr_inputs = (*curr_inputs)->next;
  213. if(!name)
  214. return -1;
  215. /* First check if the label is not in the open_inputs list */
  216. match = extract_inout(name, open_inputs);
  217. if(match) {
  218. if(link_filter(input->filter, input->pad_idx,
  219. match->filter, match->pad_idx, log_ctx) < 0)
  220. return -1;
  221. av_free(match->name);
  222. av_free(name);
  223. av_free(match);
  224. av_free(input);
  225. } else {
  226. /* Not in the list, so add the first input as a open_output */
  227. input->name = name;
  228. insert_inout(open_outputs, input);
  229. }
  230. *buf += strspn(*buf, WHITESPACES);
  231. pad++;
  232. }
  233. return pad;
  234. }
  235. int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
  236. AVFilterInOut *open_inputs,
  237. AVFilterInOut *open_outputs, AVClass *log_ctx)
  238. {
  239. int index = 0;
  240. char chr = 0;
  241. AVFilterInOut *curr_inputs = NULL;
  242. do {
  243. AVFilterContext *filter;
  244. filters += strspn(filters, WHITESPACES);
  245. if(parse_inputs(&filters, &curr_inputs, &open_outputs, log_ctx) < 0)
  246. goto fail;
  247. filter = parse_filter(&filters, graph, index, log_ctx);
  248. if(!filter)
  249. goto fail;
  250. if(filter->input_count == 1 && !curr_inputs && !index) {
  251. /* First input can be omitted if it is "[in]" */
  252. const char *tmp = "[in]";
  253. if(parse_inputs(&tmp, &curr_inputs, &open_outputs, log_ctx) < 0)
  254. goto fail;
  255. }
  256. if(link_filter_inouts(filter, &curr_inputs, &open_inputs, log_ctx) < 0)
  257. goto fail;
  258. if(parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
  259. log_ctx) < 0)
  260. goto fail;
  261. filters += strspn(filters, WHITESPACES);
  262. chr = *filters++;
  263. if(chr == ';' && curr_inputs) {
  264. av_log(log_ctx, AV_LOG_ERROR,
  265. "Could not find a output to link when parsing \"%s\"\n",
  266. filters - 1);
  267. goto fail;
  268. }
  269. index++;
  270. } while(chr == ',' || chr == ';');
  271. if (chr) {
  272. av_log(log_ctx, AV_LOG_ERROR,
  273. "Unable to parse graph description substring: \"%s\"\n",
  274. filters - 1);
  275. goto fail;
  276. }
  277. if(open_inputs && !strcmp(open_inputs->name, "out") && curr_inputs) {
  278. /* Last output can be omitted if it is "[out]" */
  279. const char *tmp = "[out]";
  280. if(parse_outputs(&tmp, &curr_inputs, &open_inputs,
  281. &open_outputs, log_ctx) < 0)
  282. goto fail;
  283. }
  284. return 0;
  285. fail:
  286. avfilter_graph_destroy(graph);
  287. free_inout(open_inputs);
  288. free_inout(open_outputs);
  289. free_inout(curr_inputs);
  290. return -1;
  291. }