diff --git a/libavfilter/avfilter.c b/libavfilter/avfilter.c index 7032f2df28..c32ae1745a 100644 --- a/libavfilter/avfilter.c +++ b/libavfilter/avfilter.c @@ -430,6 +430,9 @@ void avfilter_free(AVFilterContext *filter) int i; AVFilterLink *link; + if (filter->graph) + ff_filter_graph_remove_filter(filter->graph, filter); + if (filter->filter->uninit) filter->filter->uninit(filter); diff --git a/libavfilter/avfilter.h b/libavfilter/avfilter.h index b37b74f2ce..ca2e7b7336 100644 --- a/libavfilter/avfilter.h +++ b/libavfilter/avfilter.h @@ -658,7 +658,8 @@ int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *in int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque); /** - * Free a filter context. + * Free a filter context. This will also remove the filter from its + * filtergraph's list of filters. * * @param filter the filter to free */ diff --git a/libavfilter/avfiltergraph.c b/libavfilter/avfiltergraph.c index 969d958541..5679ad98bc 100644 --- a/libavfilter/avfiltergraph.c +++ b/libavfilter/avfiltergraph.c @@ -46,12 +46,27 @@ AVFilterGraph *avfilter_graph_alloc(void) return ret; } +void ff_filter_graph_remove_filter(AVFilterGraph *graph, AVFilterContext *filter) +{ + int i; + for (i = 0; i < graph->nb_filters; i++) { + if (graph->filters[i] == filter) { + FFSWAP(AVFilterContext*, graph->filters[i], + graph->filters[graph->nb_filters - 1]); + graph->nb_filters--; + return; + } + } +} + void avfilter_graph_free(AVFilterGraph **graph) { if (!*graph) return; - for (; (*graph)->nb_filters > 0; (*graph)->nb_filters--) - avfilter_free((*graph)->filters[(*graph)->nb_filters - 1]); + + while ((*graph)->nb_filters) + avfilter_free((*graph)->filters[0]); + av_freep(&(*graph)->scale_sws_opts); av_freep(&(*graph)->resample_lavr_opts); av_freep(&(*graph)->filters); diff --git a/libavfilter/graphparser.c b/libavfilter/graphparser.c index 7fbe43b3d0..5738a12e06 100644 --- a/libavfilter/graphparser.c +++ b/libavfilter/graphparser.c @@ -430,8 +430,8 @@ int avfilter_graph_parse2(AVFilterGraph *graph, const char *filters, return 0; fail: - for (; graph->nb_filters > 0; graph->nb_filters--) - avfilter_free(graph->filters[graph->nb_filters - 1]); + while (graph->nb_filters) + avfilter_free(graph->filters[0]); av_freep(&graph->filters); avfilter_inout_free(&open_inputs); avfilter_inout_free(&open_outputs); @@ -495,8 +495,8 @@ int avfilter_graph_parse(AVFilterGraph *graph, const char *filters, fail: if (ret < 0) { - for (; graph->nb_filters > 0; graph->nb_filters--) - avfilter_free(graph->filters[graph->nb_filters - 1]); + while (graph->nb_filters) + avfilter_free(graph->filters[0]); av_freep(&graph->filters); } avfilter_inout_free(&inputs); diff --git a/libavfilter/internal.h b/libavfilter/internal.h index 5ff150aee4..8e8a13f084 100644 --- a/libavfilter/internal.h +++ b/libavfilter/internal.h @@ -206,4 +206,9 @@ int ff_filter_frame(AVFilterLink *link, AVFrame *frame); */ AVFilterContext *ff_filter_alloc(const AVFilter *filter, const char *inst_name); +/** + * Remove a filter from a graph; + */ +void ff_filter_graph_remove_filter(AVFilterGraph *graph, AVFilterContext *filter); + #endif /* AVFILTER_INTERNAL_H */