00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <ctype.h>
00024 #include <string.h>
00025
00026 #include "avfilter.h"
00027 #include "avfiltergraph.h"
00028 #include "internal.h"
00029
00030 AVFilterGraph *avfilter_graph_alloc(void)
00031 {
00032 return av_mallocz(sizeof(AVFilterGraph));
00033 }
00034
00035 void avfilter_graph_free(AVFilterGraph *graph)
00036 {
00037 if (!graph)
00038 return;
00039 for (; graph->filter_count > 0; graph->filter_count --)
00040 avfilter_free(graph->filters[graph->filter_count - 1]);
00041 av_freep(&graph->scale_sws_opts);
00042 av_freep(&graph->filters);
00043 }
00044
00045 int avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
00046 {
00047 AVFilterContext **filters = av_realloc(graph->filters,
00048 sizeof(AVFilterContext*) * (graph->filter_count+1));
00049 if (!filters)
00050 return AVERROR(ENOMEM);
00051
00052 graph->filters = filters;
00053 graph->filters[graph->filter_count++] = filter;
00054
00055 return 0;
00056 }
00057
00058 int avfilter_graph_create_filter(AVFilterContext **filt_ctx, AVFilter *filt,
00059 const char *name, const char *args, void *opaque,
00060 AVFilterGraph *graph_ctx)
00061 {
00062 int ret;
00063
00064 if ((ret = avfilter_open(filt_ctx, filt, name)) < 0)
00065 goto fail;
00066 if ((ret = avfilter_init_filter(*filt_ctx, args, opaque)) < 0)
00067 goto fail;
00068 if ((ret = avfilter_graph_add_filter(graph_ctx, *filt_ctx)) < 0)
00069 goto fail;
00070 return 0;
00071
00072 fail:
00073 if (*filt_ctx)
00074 avfilter_free(*filt_ctx);
00075 *filt_ctx = NULL;
00076 return ret;
00077 }
00078
00079 int ff_avfilter_graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
00080 {
00081 AVFilterContext *filt;
00082 int i, j;
00083
00084 for (i = 0; i < graph->filter_count; i++) {
00085 filt = graph->filters[i];
00086
00087 for (j = 0; j < filt->input_count; j++) {
00088 if (!filt->inputs[j] || !filt->inputs[j]->src) {
00089 av_log(log_ctx, AV_LOG_ERROR,
00090 "Input pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any source\n",
00091 filt->input_pads[j].name, filt->name, filt->filter->name);
00092 return -1;
00093 }
00094 }
00095
00096 for (j = 0; j < filt->output_count; j++) {
00097 if (!filt->outputs[j] || !filt->outputs[j]->dst) {
00098 av_log(log_ctx, AV_LOG_ERROR,
00099 "Output pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any destination\n",
00100 filt->output_pads[j].name, filt->name, filt->filter->name);
00101 return -1;
00102 }
00103 }
00104 }
00105
00106 return 0;
00107 }
00108
00109 int ff_avfilter_graph_config_links(AVFilterGraph *graph, AVClass *log_ctx)
00110 {
00111 AVFilterContext *filt;
00112 int i, ret;
00113
00114 for (i=0; i < graph->filter_count; i++) {
00115 filt = graph->filters[i];
00116
00117 if (!filt->output_count) {
00118 if ((ret = avfilter_config_links(filt)))
00119 return ret;
00120 }
00121 }
00122
00123 return 0;
00124 }
00125
00126 AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
00127 {
00128 int i;
00129
00130 for (i = 0; i < graph->filter_count; i++)
00131 if (graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
00132 return graph->filters[i];
00133
00134 return NULL;
00135 }
00136
00137 static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
00138 {
00139 int i, j, ret;
00140 int scaler_count = 0;
00141 char inst_name[30];
00142
00143
00144 for (i = 0; i < graph->filter_count; i++) {
00145 if (graph->filters[i]->filter->query_formats)
00146 graph->filters[i]->filter->query_formats(graph->filters[i]);
00147 else
00148 avfilter_default_query_formats(graph->filters[i]);
00149 }
00150
00151
00152 for (i = 0; i < graph->filter_count; i++) {
00153 AVFilterContext *filter = graph->filters[i];
00154
00155 for (j = 0; j < filter->input_count; j++) {
00156 AVFilterLink *link = filter->inputs[j];
00157 if (link && link->in_formats != link->out_formats) {
00158 if (!avfilter_merge_formats(link->in_formats,
00159 link->out_formats)) {
00160 AVFilterContext *scale;
00161 char scale_args[256];
00162
00163 snprintf(inst_name, sizeof(inst_name), "auto-inserted scaler %d",
00164 scaler_count++);
00165 snprintf(scale_args, sizeof(scale_args), "0:0:%s", graph->scale_sws_opts);
00166 if ((ret = avfilter_graph_create_filter(&scale, avfilter_get_by_name("scale"),
00167 inst_name, scale_args, NULL, graph)) < 0)
00168 return ret;
00169 if ((ret = avfilter_insert_filter(link, scale, 0, 0)) < 0)
00170 return ret;
00171
00172 scale->filter->query_formats(scale);
00173 if (((link = scale-> inputs[0]) &&
00174 !avfilter_merge_formats(link->in_formats, link->out_formats)) ||
00175 ((link = scale->outputs[0]) &&
00176 !avfilter_merge_formats(link->in_formats, link->out_formats))) {
00177 av_log(log_ctx, AV_LOG_ERROR,
00178 "Impossible to convert between the formats supported by the filter "
00179 "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
00180 return -1;
00181 }
00182 }
00183 }
00184 }
00185 }
00186
00187 return 0;
00188 }
00189
00190 static void pick_format(AVFilterLink *link)
00191 {
00192 if (!link || !link->in_formats)
00193 return;
00194
00195 link->in_formats->format_count = 1;
00196 link->format = link->in_formats->formats[0];
00197
00198 avfilter_formats_unref(&link->in_formats);
00199 avfilter_formats_unref(&link->out_formats);
00200 }
00201
00202 static void pick_formats(AVFilterGraph *graph)
00203 {
00204 int i, j;
00205
00206 for (i = 0; i < graph->filter_count; i++) {
00207 AVFilterContext *filter = graph->filters[i];
00208
00209 for (j = 0; j < filter->input_count; j++)
00210 pick_format(filter->inputs[j]);
00211 for (j = 0; j < filter->output_count; j++)
00212 pick_format(filter->outputs[j]);
00213 }
00214 }
00215
00216 int ff_avfilter_graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
00217 {
00218
00219 if (query_formats(graph, log_ctx))
00220 return -1;
00221
00222
00223
00224 pick_formats(graph);
00225
00226 return 0;
00227 }
00228
00229 int avfilter_graph_config(AVFilterGraph *graphctx, AVClass *log_ctx)
00230 {
00231 int ret;
00232
00233 if ((ret = ff_avfilter_graph_check_validity(graphctx, log_ctx)))
00234 return ret;
00235 if ((ret = ff_avfilter_graph_config_formats(graphctx, log_ctx)))
00236 return ret;
00237 if ((ret = ff_avfilter_graph_config_links(graphctx, log_ctx)))
00238 return ret;
00239
00240 return 0;
00241 }