3 * copyright (c) 2008 Vitor Sessak
4 * copyright (c) 2007 Bobby Bingham
6 * This file is part of FFmpeg.
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.
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.
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
27 #include "avfiltergraph.h"
29 void avfilter_graph_destroy(AVFilterGraph
*graph
)
31 for(; graph
->filter_count
> 0; graph
->filter_count
--)
32 avfilter_destroy(graph
->filters
[graph
->filter_count
- 1]);
33 av_freep(&graph
->scale_sws_opts
);
34 av_freep(&graph
->filters
);
37 int avfilter_graph_add_filter(AVFilterGraph
*graph
, AVFilterContext
*filter
)
39 graph
->filters
= av_realloc(graph
->filters
,
40 sizeof(AVFilterContext
*) * ++graph
->filter_count
);
45 graph
->filters
[graph
->filter_count
- 1] = filter
;
50 int avfilter_graph_check_validity(AVFilterGraph
*graph
, AVClass
*log_ctx
)
52 AVFilterContext
*filt
;
55 for (i
=0; i
< graph
->filter_count
; i
++) {
56 filt
= graph
->filters
[i
];
58 for (j
= 0; j
< filt
->input_count
; j
++) {
59 if (!filt
->inputs
[j
] || !filt
->inputs
[j
]->src
) {
60 av_log(log_ctx
, AV_LOG_ERROR
,
61 "Input pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any source\n",
62 filt
->input_pads
[j
].name
, filt
->name
, filt
->filter
->name
);
67 for (j
= 0; j
< filt
->output_count
; j
++) {
68 if (!filt
->outputs
[j
] || !filt
->outputs
[j
]->dst
) {
69 av_log(log_ctx
, AV_LOG_ERROR
,
70 "Output pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any destination\n",
71 filt
->output_pads
[j
].name
, filt
->name
, filt
->filter
->name
);
80 int avfilter_graph_config_links(AVFilterGraph
*graph
, AVClass
*log_ctx
)
82 AVFilterContext
*filt
;
85 for (i
=0; i
< graph
->filter_count
; i
++) {
86 filt
= graph
->filters
[i
];
88 if (!filt
->output_count
) {
89 if ((ret
= avfilter_config_links(filt
)))
97 AVFilterContext
*avfilter_graph_get_filter(AVFilterGraph
*graph
, char *name
)
101 for(i
= 0; i
< graph
->filter_count
; i
++)
102 if(graph
->filters
[i
]->name
&& !strcmp(name
, graph
->filters
[i
]->name
))
103 return graph
->filters
[i
];
108 static int query_formats(AVFilterGraph
*graph
)
111 int scaler_count
= 0;
114 /* ask all the sub-filters for their supported colorspaces */
115 for(i
= 0; i
< graph
->filter_count
; i
++) {
116 if(graph
->filters
[i
]->filter
->query_formats
)
117 graph
->filters
[i
]->filter
->query_formats(graph
->filters
[i
]);
119 avfilter_default_query_formats(graph
->filters
[i
]);
122 /* go through and merge as many format lists as possible */
123 for(i
= 0; i
< graph
->filter_count
; i
++) {
124 AVFilterContext
*filter
= graph
->filters
[i
];
126 for(j
= 0; j
< filter
->input_count
; j
++) {
127 AVFilterLink
*link
= filter
->inputs
[j
];
128 if(link
&& link
->in_formats
!= link
->out_formats
) {
129 if(!avfilter_merge_formats(link
->in_formats
,
130 link
->out_formats
)) {
131 AVFilterContext
*scale
;
132 char scale_args
[256];
133 /* couldn't merge format lists. auto-insert scale filter */
134 snprintf(inst_name
, sizeof(inst_name
), "auto-inserted scaler %d",
137 avfilter_open(avfilter_get_by_name("scale"),inst_name
);
139 snprintf(scale_args
, sizeof(scale_args
), "0:0:%s", graph
->scale_sws_opts
);
140 if(!scale
|| scale
->filter
->init(scale
, scale_args
, NULL
) ||
141 avfilter_insert_filter(link
, scale
, 0, 0)) {
142 avfilter_destroy(scale
);
146 if (avfilter_graph_add_filter(graph
, scale
) < 0)
149 scale
->filter
->query_formats(scale
);
150 if(!avfilter_merge_formats(scale
-> inputs
[0]->in_formats
,
151 scale
-> inputs
[0]->out_formats
)||
152 !avfilter_merge_formats(scale
->outputs
[0]->in_formats
,
153 scale
->outputs
[0]->out_formats
))
163 static void pick_format(AVFilterLink
*link
)
165 if(!link
|| !link
->in_formats
)
168 link
->in_formats
->format_count
= 1;
169 link
->format
= link
->in_formats
->formats
[0];
171 avfilter_formats_unref(&link
->in_formats
);
172 avfilter_formats_unref(&link
->out_formats
);
175 static void pick_formats(AVFilterGraph
*graph
)
179 for(i
= 0; i
< graph
->filter_count
; i
++) {
180 AVFilterContext
*filter
= graph
->filters
[i
];
182 for(j
= 0; j
< filter
->input_count
; j
++)
183 pick_format(filter
->inputs
[j
]);
184 for(j
= 0; j
< filter
->output_count
; j
++)
185 pick_format(filter
->outputs
[j
]);
189 int avfilter_graph_config_formats(AVFilterGraph
*graph
)
191 /* find supported formats from sub-filters, and merge along links */
192 if(query_formats(graph
))
195 /* Once everything is merged, it's possible that we'll still have
196 * multiple valid colorspace choices. We pick the first one. */