Statistics
| Branch: | Revision:

ffmpeg / libavfilter / avfiltergraph.c @ 4359288c

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