+ while (graph->nb_filters)
+ avfilter_free(graph->filters[0]);
+ av_freep(&graph->filters);
+ avfilter_inout_free(&open_inputs);
+ avfilter_inout_free(&open_outputs);
+ avfilter_inout_free(&curr_inputs);
+
+ *inputs = NULL;
+ *outputs = NULL;
+
+ return ret;
+}
+
+int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
+ AVFilterInOut *open_inputs,
+ AVFilterInOut *open_outputs, void *log_ctx)
+{
+ int ret;
+ AVFilterInOut *cur, *match, *inputs = NULL, *outputs = NULL;
+
+ if ((ret = avfilter_graph_parse2(graph, filters, &inputs, &outputs)) < 0)
+ goto fail;
+
+ /* First input can be omitted if it is "[in]" */
+ if (inputs && !inputs->name)
+ inputs->name = av_strdup("in");
+ for (cur = inputs; cur; cur = cur->next) {
+ if (!cur->name) {
+ av_log(log_ctx, AV_LOG_ERROR,
+ "Not enough inputs specified for the \"%s\" filter.\n",
+ cur->filter_ctx->filter->name);
+ ret = AVERROR(EINVAL);
+ goto fail;
+ }
+ if (!(match = extract_inout(cur->name, &open_outputs)))
+ continue;
+ ret = avfilter_link(match->filter_ctx, match->pad_idx,
+ cur->filter_ctx, cur->pad_idx);
+ avfilter_inout_free(&match);
+ if (ret < 0)
+ goto fail;
+ }
+
+ /* Last output can be omitted if it is "[out]" */
+ if (outputs && !outputs->name)
+ outputs->name = av_strdup("out");
+ for (cur = outputs; cur; cur = cur->next) {
+ if (!cur->name) {
+ av_log(log_ctx, AV_LOG_ERROR,
+ "Invalid filterchain containing an unlabelled output pad: \"%s\"\n",
+ filters);
+ ret = AVERROR(EINVAL);
+ goto fail;
+ }
+ if (!(match = extract_inout(cur->name, &open_inputs)))
+ continue;
+ ret = avfilter_link(cur->filter_ctx, cur->pad_idx,
+ match->filter_ctx, match->pad_idx);
+ avfilter_inout_free(&match);
+ if (ret < 0)
+ goto fail;
+ }
+
+ fail:
+ if (ret < 0) {
+ while (graph->nb_filters)
+ avfilter_free(graph->filters[0]);
+ av_freep(&graph->filters);
+ }
+ avfilter_inout_free(&inputs);
+ avfilter_inout_free(&outputs);
+ avfilter_inout_free(&open_inputs);
+ avfilter_inout_free(&open_outputs);
+ return ret;