Statistics
| Branch: | Revision:

ffmpeg / libavfilter / avfiltergraph.c @ a242ac36

History | View | Annotate | Download (7.61 KB)

1 eccb6fb7 Vitor Sessak
/*
2 ec8398a3 Vitor Sessak
 * filter graphs
3 3fa77bde Stefano Sabatini
 * Copyright (c) 2008 Vitor Sessak
4
 * Copyright (c) 2007 Bobby Bingham
5 eccb6fb7 Vitor Sessak
 *
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 4fcbb2af Stefano Sabatini
#include "internal.h"
29 eccb6fb7 Vitor Sessak
30 e15aeea6 Stefano Sabatini
AVFilterGraph *avfilter_graph_alloc(void)
31
{
32
    return av_mallocz(sizeof(AVFilterGraph));
33
}
34
35 97dd1e4a Stefano Sabatini
void avfilter_graph_free(AVFilterGraph *graph)
36 f2ee8083 Vitor Sessak
{
37 caeb0c30 Stefano Sabatini
    if (!graph)
38
        return;
39 b59dd1ea Stefano Sabatini
    for (; graph->filter_count > 0; graph->filter_count --)
40 24de0edb Stefano Sabatini
        avfilter_free(graph->filters[graph->filter_count - 1]);
41 39c07b8e Stefano Sabatini
    av_freep(&graph->scale_sws_opts);
42 ffcaef34 Vitor Sessak
    av_freep(&graph->filters);
43
}
44
45 4e4a3e23 Vitor Sessak
int avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
46 ffcaef34 Vitor Sessak
{
47 3748b2b8 Stefano Sabatini
    AVFilterContext **filters = av_realloc(graph->filters,
48
                                           sizeof(AVFilterContext*) * (graph->filter_count+1));
49
    if (!filters)
50 22b7b23c Stefano Sabatini
        return AVERROR(ENOMEM);
51 4e4a3e23 Vitor Sessak
52 3748b2b8 Stefano Sabatini
    graph->filters = filters;
53
    graph->filters[graph->filter_count++] = filter;
54 4e4a3e23 Vitor Sessak
55
    return 0;
56 eccb6fb7 Vitor Sessak
}
57
58 037be76e Stefano Sabatini
int avfilter_graph_create_filter(AVFilterContext **filt_ctx, AVFilter *filt,
59
                                 const char *name, const char *args, void *opaque,
60
                                 AVFilterGraph *graph_ctx)
61
{
62
    int ret;
63
64
    if ((ret = avfilter_open(filt_ctx, filt, name)) < 0)
65
        goto fail;
66
    if ((ret = avfilter_init_filter(*filt_ctx, args, opaque)) < 0)
67
        goto fail;
68
    if ((ret = avfilter_graph_add_filter(graph_ctx, *filt_ctx)) < 0)
69
        goto fail;
70
    return 0;
71
72
fail:
73
    if (*filt_ctx)
74
        avfilter_free(*filt_ctx);
75
    *filt_ctx = NULL;
76
    return ret;
77
}
78
79 4fcbb2af Stefano Sabatini
int ff_avfilter_graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
80 822005ed Stefano Sabatini
{
81
    AVFilterContext *filt;
82
    int i, j;
83
84 b59dd1ea Stefano Sabatini
    for (i = 0; i < graph->filter_count; i++) {
85 822005ed Stefano Sabatini
        filt = graph->filters[i];
86
87
        for (j = 0; j < filt->input_count; j++) {
88
            if (!filt->inputs[j] || !filt->inputs[j]->src) {
89
                av_log(log_ctx, AV_LOG_ERROR,
90
                       "Input pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any source\n",
91
                       filt->input_pads[j].name, filt->name, filt->filter->name);
92
                return -1;
93
            }
94
        }
95
96
        for (j = 0; j < filt->output_count; j++) {
97
            if (!filt->outputs[j] || !filt->outputs[j]->dst) {
98
                av_log(log_ctx, AV_LOG_ERROR,
99
                       "Output pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any destination\n",
100
                       filt->output_pads[j].name, filt->name, filt->filter->name);
101
                return -1;
102
            }
103
        }
104
    }
105
106
    return 0;
107
}
108
109 4fcbb2af Stefano Sabatini
int ff_avfilter_graph_config_links(AVFilterGraph *graph, AVClass *log_ctx)
110 4ec42240 Stefano Sabatini
{
111
    AVFilterContext *filt;
112
    int i, ret;
113
114
    for (i=0; i < graph->filter_count; i++) {
115
        filt = graph->filters[i];
116
117
        if (!filt->output_count) {
118
            if ((ret = avfilter_config_links(filt)))
119
                return ret;
120
        }
121
    }
122
123
    return 0;
124
}
125
126 9f39bb37 Vitor Sessak
AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
127 57d4a157 Vitor Sessak
{
128
    int i;
129
130 b59dd1ea Stefano Sabatini
    for (i = 0; i < graph->filter_count; i++)
131
        if (graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
132 57d4a157 Vitor Sessak
            return graph->filters[i];
133
134
    return NULL;
135
}
136
137 3fb8e77a Stefano Sabatini
static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
138 7b470878 Vitor Sessak
{
139 037be76e Stefano Sabatini
    int i, j, ret;
140 a6f88fbc Vitor Sessak
    int scaler_count = 0;
141
    char inst_name[30];
142 7b470878 Vitor Sessak
143 bdab614b S.N. Hemanth Meenakshisundaram
    /* ask all the sub-filters for their supported media formats */
144 b59dd1ea Stefano Sabatini
    for (i = 0; i < graph->filter_count; i++) {
145
        if (graph->filters[i]->filter->query_formats)
146 7b470878 Vitor Sessak
            graph->filters[i]->filter->query_formats(graph->filters[i]);
147
        else
148
            avfilter_default_query_formats(graph->filters[i]);
149
    }
150
151
    /* go through and merge as many format lists as possible */
152 b59dd1ea Stefano Sabatini
    for (i = 0; i < graph->filter_count; i++) {
153 7b470878 Vitor Sessak
        AVFilterContext *filter = graph->filters[i];
154
155 b59dd1ea Stefano Sabatini
        for (j = 0; j < filter->input_count; j++) {
156 779cc07a Vitor Sessak
            AVFilterLink *link = filter->inputs[j];
157 b59dd1ea Stefano Sabatini
            if (link && link->in_formats != link->out_formats) {
158
                if (!avfilter_merge_formats(link->in_formats,
159
                                            link->out_formats)) {
160 a6f88fbc Vitor Sessak
                    AVFilterContext *scale;
161 39c07b8e Stefano Sabatini
                    char scale_args[256];
162 7b470878 Vitor Sessak
                    /* couldn't merge format lists. auto-insert scale filter */
163 51bf654d Vitor Sessak
                    snprintf(inst_name, sizeof(inst_name), "auto-inserted scaler %d",
164 9ea97721 Stefano Sabatini
                             scaler_count++);
165 39c07b8e Stefano Sabatini
                    snprintf(scale_args, sizeof(scale_args), "0:0:%s", graph->scale_sws_opts);
166 037be76e Stefano Sabatini
                    if ((ret = avfilter_graph_create_filter(&scale, avfilter_get_by_name("scale"),
167
                                                            inst_name, scale_args, NULL, graph)) < 0)
168
                        return ret;
169
                    if ((ret = avfilter_insert_filter(link, scale, 0, 0)) < 0)
170
                        return ret;
171 4e4a3e23 Vitor Sessak
172 7b470878 Vitor Sessak
                    scale->filter->query_formats(scale);
173 f37c934d Stefano Sabatini
                    if (((link = scale-> inputs[0]) &&
174
                         !avfilter_merge_formats(link->in_formats, link->out_formats)) ||
175
                        ((link = scale->outputs[0]) &&
176
                         !avfilter_merge_formats(link->in_formats, link->out_formats))) {
177
                        av_log(log_ctx, AV_LOG_ERROR,
178
                               "Impossible to convert between the formats supported by the filter "
179
                               "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
180 7b470878 Vitor Sessak
                        return -1;
181 f37c934d Stefano Sabatini
                    }
182 7b470878 Vitor Sessak
                }
183
            }
184
        }
185
    }
186
187
    return 0;
188
}
189
190
static void pick_format(AVFilterLink *link)
191
{
192 b59dd1ea Stefano Sabatini
    if (!link || !link->in_formats)
193 7b470878 Vitor Sessak
        return;
194
195
    link->in_formats->format_count = 1;
196
    link->format = link->in_formats->formats[0];
197
198
    avfilter_formats_unref(&link->in_formats);
199
    avfilter_formats_unref(&link->out_formats);
200
}
201
202 9f39bb37 Vitor Sessak
static void pick_formats(AVFilterGraph *graph)
203 7b470878 Vitor Sessak
{
204
    int i, j;
205
206 b59dd1ea Stefano Sabatini
    for (i = 0; i < graph->filter_count; i++) {
207 7b470878 Vitor Sessak
        AVFilterContext *filter = graph->filters[i];
208
209 b59dd1ea Stefano Sabatini
        for (j = 0; j < filter->input_count; j++)
210 7b470878 Vitor Sessak
            pick_format(filter->inputs[j]);
211 b59dd1ea Stefano Sabatini
        for (j = 0; j < filter->output_count; j++)
212 7b470878 Vitor Sessak
            pick_format(filter->outputs[j]);
213
    }
214
}
215
216 4fcbb2af Stefano Sabatini
int ff_avfilter_graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
217 7b470878 Vitor Sessak
{
218 ec8398a3 Vitor Sessak
    /* find supported formats from sub-filters, and merge along links */
219 b59dd1ea Stefano Sabatini
    if (query_formats(graph, log_ctx))
220 7b470878 Vitor Sessak
        return -1;
221
222
    /* Once everything is merged, it's possible that we'll still have
223 bdab614b S.N. Hemanth Meenakshisundaram
     * multiple valid media format choices. We pick the first one. */
224 7b470878 Vitor Sessak
    pick_formats(graph);
225
226
    return 0;
227
}
228
229 2a24df93 Stefano Sabatini
int avfilter_graph_config(AVFilterGraph *graphctx, AVClass *log_ctx)
230
{
231
    int ret;
232
233 4fcbb2af Stefano Sabatini
    if ((ret = ff_avfilter_graph_check_validity(graphctx, log_ctx)))
234 2a24df93 Stefano Sabatini
        return ret;
235 4fcbb2af Stefano Sabatini
    if ((ret = ff_avfilter_graph_config_formats(graphctx, log_ctx)))
236 2a24df93 Stefano Sabatini
        return ret;
237 4fcbb2af Stefano Sabatini
    if ((ret = ff_avfilter_graph_config_links(graphctx, log_ctx)))
238 2a24df93 Stefano Sabatini
        return ret;
239
240
    return 0;
241
}