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