Statistics
| Branch: | Revision:

ffmpeg / libavfilter / avfiltergraph.c @ d370e3e9

History | View | Annotate | Download (6.96 KB)

1 eccb6fb7 Vitor Sessak
/*
2 ec8398a3 Vitor Sessak
 * filter graphs
3 5c2ff9fd Vitor Sessak
 * copyright (c) 2008 Vitor Sessak
4 eccb6fb7 Vitor Sessak
 * copyright (c) 2007 Bobby Bingham
5
 *
6
 * This file is part of FFmpeg.
7
 *
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.
12
 *
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.
17
 *
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
21
 */
22
23 5c2ff9fd Vitor Sessak
#include <ctype.h>
24
#include <string.h>
25
26 eccb6fb7 Vitor Sessak
#include "avfilter.h"
27
#include "avfiltergraph.h"
28
29 d7dbe558 Stefano Sabatini
void avfilter_graph_destroy(AVFilterGraph *graph)
30 f2ee8083 Vitor Sessak
{
31 65a4bd9c Vitor Sessak
    for(; graph->filter_count > 0; graph->filter_count --)
32
        avfilter_destroy(graph->filters[graph->filter_count - 1]);
33 39c07b8e Stefano Sabatini
    av_freep(&graph->scale_sws_opts);
34 ffcaef34 Vitor Sessak
    av_freep(&graph->filters);
35
}
36
37 4e4a3e23 Vitor Sessak
int avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
38 ffcaef34 Vitor Sessak
{
39 3748b2b8 Stefano Sabatini
    AVFilterContext **filters = av_realloc(graph->filters,
40
                                           sizeof(AVFilterContext*) * (graph->filter_count+1));
41
    if (!filters)
42 22b7b23c Stefano Sabatini
        return AVERROR(ENOMEM);
43 4e4a3e23 Vitor Sessak
44 3748b2b8 Stefano Sabatini
    graph->filters = filters;
45
    graph->filters[graph->filter_count++] = filter;
46 4e4a3e23 Vitor Sessak
47
    return 0;
48 eccb6fb7 Vitor Sessak
}
49
50 822005ed Stefano Sabatini
int avfilter_graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
51
{
52
    AVFilterContext *filt;
53
    int i, j;
54
55
    for (i=0; i < graph->filter_count; i++) {
56
        filt = graph->filters[i];
57
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);
63
                return -1;
64
            }
65
        }
66
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);
72
                return -1;
73
            }
74
        }
75
    }
76
77
    return 0;
78
}
79
80 4ec42240 Stefano Sabatini
int avfilter_graph_config_links(AVFilterGraph *graph, AVClass *log_ctx)
81
{
82
    AVFilterContext *filt;
83
    int i, ret;
84
85
    for (i=0; i < graph->filter_count; i++) {
86
        filt = graph->filters[i];
87
88
        if (!filt->output_count) {
89
            if ((ret = avfilter_config_links(filt)))
90
                return ret;
91
        }
92
    }
93
94
    return 0;
95
}
96
97 9f39bb37 Vitor Sessak
AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
98 57d4a157 Vitor Sessak
{
99
    int i;
100
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];
104
105
    return NULL;
106
}
107
108 3fb8e77a Stefano Sabatini
static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
109 7b470878 Vitor Sessak
{
110
    int i, j;
111 a6f88fbc Vitor Sessak
    int scaler_count = 0;
112
    char inst_name[30];
113 7b470878 Vitor Sessak
114 bdab614b S.N. Hemanth Meenakshisundaram
    /* ask all the sub-filters for their supported media formats */
115 7b470878 Vitor Sessak
    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]);
118
        else
119
            avfilter_default_query_formats(graph->filters[i]);
120
    }
121
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];
125
126
        for(j = 0; j < filter->input_count; j ++) {
127 779cc07a Vitor Sessak
            AVFilterLink *link = filter->inputs[j];
128
            if(link && link->in_formats != link->out_formats) {
129 7b470878 Vitor Sessak
                if(!avfilter_merge_formats(link->in_formats,
130
                                           link->out_formats)) {
131 a6f88fbc Vitor Sessak
                    AVFilterContext *scale;
132 39c07b8e Stefano Sabatini
                    char scale_args[256];
133 7b470878 Vitor Sessak
                    /* couldn't merge format lists. auto-insert scale filter */
134 51bf654d Vitor Sessak
                    snprintf(inst_name, sizeof(inst_name), "auto-inserted scaler %d",
135 9ea97721 Stefano Sabatini
                             scaler_count++);
136 84c03869 Stefano Sabatini
                    avfilter_open(&scale, avfilter_get_by_name("scale"), inst_name);
137 7b470878 Vitor Sessak
138 39c07b8e Stefano Sabatini
                    snprintf(scale_args, sizeof(scale_args), "0:0:%s", graph->scale_sws_opts);
139
                    if(!scale || scale->filter->init(scale, scale_args, NULL) ||
140 779cc07a Vitor Sessak
                                 avfilter_insert_filter(link, scale, 0, 0)) {
141 7b470878 Vitor Sessak
                        avfilter_destroy(scale);
142
                        return -1;
143
                    }
144
145 4e4a3e23 Vitor Sessak
                    if (avfilter_graph_add_filter(graph, scale) < 0)
146
                        return -1;
147
148 7b470878 Vitor Sessak
                    scale->filter->query_formats(scale);
149 f37c934d Stefano Sabatini
                    if (((link = scale-> inputs[0]) &&
150
                         !avfilter_merge_formats(link->in_formats, link->out_formats)) ||
151
                        ((link = scale->outputs[0]) &&
152
                         !avfilter_merge_formats(link->in_formats, link->out_formats))) {
153
                        av_log(log_ctx, AV_LOG_ERROR,
154
                               "Impossible to convert between the formats supported by the filter "
155
                               "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
156 7b470878 Vitor Sessak
                        return -1;
157 f37c934d Stefano Sabatini
                    }
158 7b470878 Vitor Sessak
                }
159
            }
160
        }
161
    }
162
163
    return 0;
164
}
165
166
static void pick_format(AVFilterLink *link)
167
{
168
    if(!link || !link->in_formats)
169
        return;
170
171
    link->in_formats->format_count = 1;
172
    link->format = link->in_formats->formats[0];
173
174
    avfilter_formats_unref(&link->in_formats);
175
    avfilter_formats_unref(&link->out_formats);
176
}
177
178 9f39bb37 Vitor Sessak
static void pick_formats(AVFilterGraph *graph)
179 7b470878 Vitor Sessak
{
180
    int i, j;
181
182
    for(i = 0; i < graph->filter_count; i ++) {
183
        AVFilterContext *filter = graph->filters[i];
184
185
        for(j = 0; j < filter->input_count; j ++)
186
            pick_format(filter->inputs[j]);
187
        for(j = 0; j < filter->output_count; j ++)
188
            pick_format(filter->outputs[j]);
189
    }
190
}
191
192 3fb8e77a Stefano Sabatini
int avfilter_graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
193 7b470878 Vitor Sessak
{
194 ec8398a3 Vitor Sessak
    /* find supported formats from sub-filters, and merge along links */
195 3fb8e77a Stefano Sabatini
    if(query_formats(graph, log_ctx))
196 7b470878 Vitor Sessak
        return -1;
197
198
    /* Once everything is merged, it's possible that we'll still have
199 bdab614b S.N. Hemanth Meenakshisundaram
     * multiple valid media format choices. We pick the first one. */
200 7b470878 Vitor Sessak
    pick_formats(graph);
201
202
    return 0;
203
}
204
205 2a24df93 Stefano Sabatini
int avfilter_graph_config(AVFilterGraph *graphctx, AVClass *log_ctx)
206
{
207
    int ret;
208
209
    if ((ret = avfilter_graph_check_validity(graphctx, log_ctx)))
210
        return ret;
211
    if ((ret = avfilter_graph_config_formats(graphctx, log_ctx)))
212
        return ret;
213
    if ((ret = avfilter_graph_config_links(graphctx, log_ctx)))
214
        return ret;
215
216
    return 0;
217
}