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.

597 lines
17KB

  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 <string.h>
  23. #include <stdio.h>
  24. #include "libavutil/avstring.h"
  25. #include "libavutil/mem.h"
  26. #include "avfilter.h"
  27. #define WHITESPACES " \n\t"
  28. /**
  29. * Link two filters together.
  30. *
  31. * @see avfilter_link()
  32. */
  33. static int link_filter(AVFilterContext *src, int srcpad,
  34. AVFilterContext *dst, int dstpad,
  35. void *log_ctx)
  36. {
  37. int ret;
  38. if ((ret = avfilter_link(src, srcpad, dst, dstpad))) {
  39. av_log(log_ctx, AV_LOG_ERROR,
  40. "Cannot create the link %s:%d -> %s:%d\n",
  41. src->filter->name, srcpad, dst->filter->name, dstpad);
  42. return ret;
  43. }
  44. return 0;
  45. }
  46. /**
  47. * Parse the name of a link, which has the format "[linkname]".
  48. *
  49. * @return a pointer (that need to be freed after use) to the name
  50. * between parenthesis
  51. */
  52. static char *parse_link_name(const char **buf, void *log_ctx)
  53. {
  54. const char *start = *buf;
  55. char *name;
  56. (*buf)++;
  57. name = av_get_token(buf, "]");
  58. if (!name[0]) {
  59. av_log(log_ctx, AV_LOG_ERROR,
  60. "Bad (empty?) label found in the following: \"%s\".\n", start);
  61. goto fail;
  62. }
  63. if (*(*buf)++ != ']') {
  64. av_log(log_ctx, AV_LOG_ERROR,
  65. "Mismatched '[' found in the following: \"%s\".\n", start);
  66. fail:
  67. av_freep(&name);
  68. }
  69. return name;
  70. }
  71. /**
  72. * Create an instance of a filter, initialize and insert it in the
  73. * filtergraph in *ctx.
  74. *
  75. * @param filt_ctx put here a filter context in case of successful creation and configuration, NULL otherwise.
  76. * @param ctx the filtergraph context
  77. * @param index an index which is supposed to be unique for each filter instance added to the filtergraph
  78. * @param filt_name the name of the filter to create
  79. * @param args the arguments provided to the filter during its initialization
  80. * @param log_ctx the log context to use
  81. * @return 0 in case of success, a negative AVERROR code otherwise
  82. */
  83. static int create_filter(AVFilterContext **filt_ctx, AVFilterGraph *ctx, int index,
  84. const char *filt_name, const char *args, void *log_ctx)
  85. {
  86. AVFilter *filt;
  87. char inst_name[30];
  88. char tmp_args[256];
  89. int ret;
  90. snprintf(inst_name, sizeof(inst_name), "Parsed_%s_%d", filt_name, index);
  91. filt = avfilter_get_by_name(filt_name);
  92. if (!filt) {
  93. av_log(log_ctx, AV_LOG_ERROR,
  94. "No such filter: '%s'\n", filt_name);
  95. return AVERROR(EINVAL);
  96. }
  97. *filt_ctx = avfilter_graph_alloc_filter(ctx, filt, inst_name);
  98. if (!*filt_ctx) {
  99. av_log(log_ctx, AV_LOG_ERROR,
  100. "Error creating filter '%s'\n", filt_name);
  101. return AVERROR(ENOMEM);
  102. }
  103. if (!strcmp(filt_name, "scale") && args && !strstr(args, "flags") &&
  104. ctx->scale_sws_opts) {
  105. snprintf(tmp_args, sizeof(tmp_args), "%s:%s",
  106. args, ctx->scale_sws_opts);
  107. args = tmp_args;
  108. }
  109. ret = avfilter_init_str(*filt_ctx, args);
  110. if (ret < 0) {
  111. av_log(log_ctx, AV_LOG_ERROR,
  112. "Error initializing filter '%s' with args '%s'\n", filt_name, args);
  113. return ret;
  114. }
  115. return 0;
  116. }
  117. /**
  118. * Parse a string of the form FILTER_NAME[=PARAMS], and create a
  119. * corresponding filter instance which is added to graph with
  120. * create_filter().
  121. *
  122. * @param filt_ctx Pointer that is set to the created and configured filter
  123. * context on success, set to NULL on failure.
  124. * @param filt_ctx put here a pointer to the created filter context on
  125. * success, NULL otherwise
  126. * @param buf pointer to the buffer to parse, *buf will be updated to
  127. * point to the char next after the parsed string
  128. * @param index an index which is assigned to the created filter
  129. * instance, and which is supposed to be unique for each filter
  130. * instance added to the filtergraph
  131. * @return 0 in case of success, a negative AVERROR code otherwise
  132. */
  133. static int parse_filter(AVFilterContext **filt_ctx, const char **buf, AVFilterGraph *graph,
  134. int index, void *log_ctx)
  135. {
  136. char *opts = NULL;
  137. char *name = av_get_token(buf, "=,;[\n");
  138. int ret;
  139. if (**buf == '=') {
  140. (*buf)++;
  141. opts = av_get_token(buf, "[],;\n");
  142. }
  143. ret = create_filter(filt_ctx, graph, index, name, opts, log_ctx);
  144. av_free(name);
  145. av_free(opts);
  146. return ret;
  147. }
  148. AVFilterInOut *avfilter_inout_alloc(void)
  149. {
  150. return av_mallocz(sizeof(AVFilterInOut));
  151. }
  152. void avfilter_inout_free(AVFilterInOut **inout)
  153. {
  154. while (*inout) {
  155. AVFilterInOut *next = (*inout)->next;
  156. av_freep(&(*inout)->name);
  157. av_freep(inout);
  158. *inout = next;
  159. }
  160. }
  161. static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
  162. {
  163. AVFilterInOut *ret;
  164. while (*links && (!(*links)->name || strcmp((*links)->name, label)))
  165. links = &((*links)->next);
  166. ret = *links;
  167. if (ret) {
  168. *links = ret->next;
  169. ret->next = NULL;
  170. }
  171. return ret;
  172. }
  173. static void insert_inout(AVFilterInOut **inouts, AVFilterInOut *element)
  174. {
  175. element->next = *inouts;
  176. *inouts = element;
  177. }
  178. static void append_inout(AVFilterInOut **inouts, AVFilterInOut **element)
  179. {
  180. while (*inouts && (*inouts)->next)
  181. inouts = &((*inouts)->next);
  182. if (!*inouts)
  183. *inouts = *element;
  184. else
  185. (*inouts)->next = *element;
  186. *element = NULL;
  187. }
  188. static int link_filter_inouts(AVFilterContext *filt_ctx,
  189. AVFilterInOut **curr_inputs,
  190. AVFilterInOut **open_inputs, void *log_ctx)
  191. {
  192. int pad, ret;
  193. for (pad = 0; pad < filt_ctx->nb_inputs; pad++) {
  194. AVFilterInOut *p = *curr_inputs;
  195. if (p) {
  196. *curr_inputs = (*curr_inputs)->next;
  197. p->next = NULL;
  198. } else if (!(p = av_mallocz(sizeof(*p))))
  199. return AVERROR(ENOMEM);
  200. if (p->filter_ctx) {
  201. ret = link_filter(p->filter_ctx, p->pad_idx, filt_ctx, pad, log_ctx);
  202. av_free(p->name);
  203. av_free(p);
  204. if (ret < 0)
  205. return ret;
  206. } else {
  207. p->filter_ctx = filt_ctx;
  208. p->pad_idx = pad;
  209. append_inout(open_inputs, &p);
  210. }
  211. }
  212. if (*curr_inputs) {
  213. av_log(log_ctx, AV_LOG_ERROR,
  214. "Too many inputs specified for the \"%s\" filter.\n",
  215. filt_ctx->filter->name);
  216. return AVERROR(EINVAL);
  217. }
  218. pad = filt_ctx->nb_outputs;
  219. while (pad--) {
  220. AVFilterInOut *currlinkn = av_mallocz(sizeof(AVFilterInOut));
  221. if (!currlinkn)
  222. return AVERROR(ENOMEM);
  223. currlinkn->filter_ctx = filt_ctx;
  224. currlinkn->pad_idx = pad;
  225. insert_inout(curr_inputs, currlinkn);
  226. }
  227. return 0;
  228. }
  229. static int parse_inputs(const char **buf, AVFilterInOut **curr_inputs,
  230. AVFilterInOut **open_outputs, void *log_ctx)
  231. {
  232. AVFilterInOut *parsed_inputs = NULL;
  233. int pad = 0;
  234. while (**buf == '[') {
  235. char *name = parse_link_name(buf, log_ctx);
  236. AVFilterInOut *match;
  237. if (!name)
  238. return AVERROR(EINVAL);
  239. /* First check if the label is not in the open_outputs list */
  240. match = extract_inout(name, open_outputs);
  241. if (match) {
  242. av_free(name);
  243. } else {
  244. /* Not in the list, so add it as an input */
  245. if (!(match = av_mallocz(sizeof(AVFilterInOut)))) {
  246. av_free(name);
  247. return AVERROR(ENOMEM);
  248. }
  249. match->name = name;
  250. match->pad_idx = pad;
  251. }
  252. append_inout(&parsed_inputs, &match);
  253. *buf += strspn(*buf, WHITESPACES);
  254. pad++;
  255. }
  256. append_inout(&parsed_inputs, curr_inputs);
  257. *curr_inputs = parsed_inputs;
  258. return pad;
  259. }
  260. static int parse_outputs(const char **buf, AVFilterInOut **curr_inputs,
  261. AVFilterInOut **open_inputs,
  262. AVFilterInOut **open_outputs, void *log_ctx)
  263. {
  264. int ret, pad = 0;
  265. while (**buf == '[') {
  266. char *name = parse_link_name(buf, log_ctx);
  267. AVFilterInOut *match;
  268. AVFilterInOut *input = *curr_inputs;
  269. if (!name)
  270. return AVERROR(EINVAL);
  271. if (!input) {
  272. av_log(log_ctx, AV_LOG_ERROR,
  273. "No output pad can be associated to link label '%s'.\n", name);
  274. av_free(name);
  275. return AVERROR(EINVAL);
  276. }
  277. *curr_inputs = (*curr_inputs)->next;
  278. /* First check if the label is not in the open_inputs list */
  279. match = extract_inout(name, open_inputs);
  280. if (match) {
  281. if ((ret = link_filter(input->filter_ctx, input->pad_idx,
  282. match->filter_ctx, match->pad_idx, log_ctx)) < 0) {
  283. av_free(name);
  284. return ret;
  285. }
  286. av_free(match->name);
  287. av_free(name);
  288. av_free(match);
  289. av_free(input);
  290. } else {
  291. /* Not in the list, so add the first input as a open_output */
  292. input->name = name;
  293. insert_inout(open_outputs, input);
  294. }
  295. *buf += strspn(*buf, WHITESPACES);
  296. pad++;
  297. }
  298. return pad;
  299. }
  300. static int parse_sws_flags(const char **buf, AVFilterGraph *graph)
  301. {
  302. char *p = strchr(*buf, ';');
  303. if (strncmp(*buf, "sws_flags=", 10))
  304. return 0;
  305. if (!p) {
  306. av_log(graph, AV_LOG_ERROR, "sws_flags not terminated with ';'.\n");
  307. return AVERROR(EINVAL);
  308. }
  309. *buf += 4; // keep the 'flags=' part
  310. av_freep(&graph->scale_sws_opts);
  311. if (!(graph->scale_sws_opts = av_mallocz(p - *buf + 1)))
  312. return AVERROR(ENOMEM);
  313. av_strlcpy(graph->scale_sws_opts, *buf, p - *buf + 1);
  314. *buf = p + 1;
  315. return 0;
  316. }
  317. int avfilter_graph_parse2(AVFilterGraph *graph, const char *filters,
  318. AVFilterInOut **inputs,
  319. AVFilterInOut **outputs)
  320. {
  321. int index = 0, ret = 0;
  322. char chr = 0;
  323. AVFilterInOut *curr_inputs = NULL, *open_inputs = NULL, *open_outputs = NULL;
  324. filters += strspn(filters, WHITESPACES);
  325. if ((ret = parse_sws_flags(&filters, graph)) < 0)
  326. goto fail;
  327. do {
  328. AVFilterContext *filter;
  329. filters += strspn(filters, WHITESPACES);
  330. if ((ret = parse_inputs(&filters, &curr_inputs, &open_outputs, graph)) < 0)
  331. goto end;
  332. if ((ret = parse_filter(&filter, &filters, graph, index, graph)) < 0)
  333. goto end;
  334. if ((ret = link_filter_inouts(filter, &curr_inputs, &open_inputs, graph)) < 0)
  335. goto end;
  336. if ((ret = parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
  337. graph)) < 0)
  338. goto end;
  339. filters += strspn(filters, WHITESPACES);
  340. chr = *filters++;
  341. if (chr == ';' && curr_inputs)
  342. append_inout(&open_outputs, &curr_inputs);
  343. index++;
  344. } while (chr == ',' || chr == ';');
  345. if (chr) {
  346. av_log(graph, AV_LOG_ERROR,
  347. "Unable to parse graph description substring: \"%s\"\n",
  348. filters - 1);
  349. ret = AVERROR(EINVAL);
  350. goto end;
  351. }
  352. append_inout(&open_outputs, &curr_inputs);
  353. *inputs = open_inputs;
  354. *outputs = open_outputs;
  355. return 0;
  356. fail:end:
  357. while (graph->nb_filters)
  358. avfilter_free(graph->filters[0]);
  359. av_freep(&graph->filters);
  360. avfilter_inout_free(&open_inputs);
  361. avfilter_inout_free(&open_outputs);
  362. avfilter_inout_free(&curr_inputs);
  363. *inputs = NULL;
  364. *outputs = NULL;
  365. return ret;
  366. }
  367. int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
  368. AVFilterInOut **open_inputs_ptr, AVFilterInOut **open_outputs_ptr,
  369. void *log_ctx)
  370. {
  371. #if 0
  372. int ret;
  373. AVFilterInOut *open_inputs = open_inputs_ptr ? *open_inputs_ptr : NULL;
  374. AVFilterInOut *open_outputs = open_outputs_ptr ? *open_outputs_ptr : NULL;
  375. AVFilterInOut *cur, *match, *inputs = NULL, *outputs = NULL;
  376. if ((ret = avfilter_graph_parse2(graph, filters, &inputs, &outputs)) < 0)
  377. goto fail;
  378. /* First input can be omitted if it is "[in]" */
  379. if (inputs && !inputs->name)
  380. inputs->name = av_strdup("in");
  381. for (cur = inputs; cur; cur = cur->next) {
  382. if (!cur->name) {
  383. av_log(log_ctx, AV_LOG_ERROR,
  384. "Not enough inputs specified for the \"%s\" filter.\n",
  385. cur->filter_ctx->filter->name);
  386. ret = AVERROR(EINVAL);
  387. goto fail;
  388. }
  389. if (!(match = extract_inout(cur->name, &open_outputs)))
  390. continue;
  391. ret = avfilter_link(match->filter_ctx, match->pad_idx,
  392. cur->filter_ctx, cur->pad_idx);
  393. avfilter_inout_free(&match);
  394. if (ret < 0)
  395. goto fail;
  396. }
  397. /* Last output can be omitted if it is "[out]" */
  398. if (outputs && !outputs->name)
  399. outputs->name = av_strdup("out");
  400. for (cur = outputs; cur; cur = cur->next) {
  401. if (!cur->name) {
  402. av_log(log_ctx, AV_LOG_ERROR,
  403. "Invalid filterchain containing an unlabelled output pad: \"%s\"\n",
  404. filters);
  405. ret = AVERROR(EINVAL);
  406. goto fail;
  407. }
  408. if (!(match = extract_inout(cur->name, &open_inputs)))
  409. continue;
  410. ret = avfilter_link(cur->filter_ctx, cur->pad_idx,
  411. match->filter_ctx, match->pad_idx);
  412. avfilter_inout_free(&match);
  413. if (ret < 0)
  414. goto fail;
  415. }
  416. fail:
  417. if (ret < 0) {
  418. while (graph->nb_filters)
  419. avfilter_free(graph->filters[0]);
  420. av_freep(&graph->filters);
  421. }
  422. avfilter_inout_free(&inputs);
  423. avfilter_inout_free(&outputs);
  424. /* clear open_in/outputs only if not passed as parameters */
  425. if (open_inputs_ptr) *open_inputs_ptr = open_inputs;
  426. else avfilter_inout_free(&open_inputs);
  427. if (open_outputs_ptr) *open_outputs_ptr = open_outputs;
  428. else avfilter_inout_free(&open_outputs);
  429. return ret;
  430. }
  431. #else
  432. int index = 0, ret = 0;
  433. char chr = 0;
  434. AVFilterInOut *curr_inputs = NULL;
  435. AVFilterInOut *open_inputs = open_inputs_ptr ? *open_inputs_ptr : NULL;
  436. AVFilterInOut *open_outputs = open_outputs_ptr ? *open_outputs_ptr : NULL;
  437. if ((ret = parse_sws_flags(&filters, graph)) < 0)
  438. goto end;
  439. do {
  440. AVFilterContext *filter;
  441. const char *filterchain = filters;
  442. filters += strspn(filters, WHITESPACES);
  443. if ((ret = parse_inputs(&filters, &curr_inputs, &open_outputs, log_ctx)) < 0)
  444. goto end;
  445. if ((ret = parse_filter(&filter, &filters, graph, index, log_ctx)) < 0)
  446. goto end;
  447. if (filter->input_count == 1 && !curr_inputs && !index) {
  448. /* First input pad, assume it is "[in]" if not specified */
  449. const char *tmp = "[in]";
  450. if ((ret = parse_inputs(&tmp, &curr_inputs, &open_outputs, log_ctx)) < 0)
  451. goto end;
  452. }
  453. if ((ret = link_filter_inouts(filter, &curr_inputs, &open_inputs, log_ctx)) < 0)
  454. goto end;
  455. if ((ret = parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
  456. log_ctx)) < 0)
  457. goto end;
  458. filters += strspn(filters, WHITESPACES);
  459. chr = *filters++;
  460. if (chr == ';' && curr_inputs) {
  461. av_log(log_ctx, AV_LOG_ERROR,
  462. "Invalid filterchain containing an unlabelled output pad: \"%s\"\n",
  463. filterchain);
  464. ret = AVERROR(EINVAL);
  465. goto end;
  466. }
  467. index++;
  468. } while (chr == ',' || chr == ';');
  469. if (chr) {
  470. av_log(log_ctx, AV_LOG_ERROR,
  471. "Unable to parse graph description substring: \"%s\"\n",
  472. filters - 1);
  473. ret = AVERROR(EINVAL);
  474. goto end;
  475. }
  476. if (curr_inputs) {
  477. /* Last output pad, assume it is "[out]" if not specified */
  478. const char *tmp = "[out]";
  479. if ((ret = parse_outputs(&tmp, &curr_inputs, &open_inputs, &open_outputs,
  480. log_ctx)) < 0)
  481. goto end;
  482. }
  483. end:
  484. /* clear open_in/outputs only if not passed as parameters */
  485. if (open_inputs_ptr) *open_inputs_ptr = open_inputs;
  486. else avfilter_inout_free(&open_inputs);
  487. if (open_outputs_ptr) *open_outputs_ptr = open_outputs;
  488. else avfilter_inout_free(&open_outputs);
  489. avfilter_inout_free(&curr_inputs);
  490. if (ret < 0) {
  491. while (graph->nb_filters)
  492. avfilter_free(graph->filters[0]);
  493. av_freep(&graph->filters);
  494. }
  495. return ret;
  496. }
  497. #endif