Statistics
| Branch: | Revision:

ffmpeg / libavfilter / avfiltergraph.c @ 037be76e

History | View | Annotate | Download (7.57 KB)

1
/*
2
 * filter graphs
3
 * Copyright (c) 2008 Vitor Sessak
4
 * 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
#include <ctype.h>
24
#include <string.h>
25

    
26
#include "avfilter.h"
27
#include "avfiltergraph.h"
28
#include "internal.h"
29

    
30
AVFilterGraph *avfilter_graph_alloc(void)
31
{
32
    return av_mallocz(sizeof(AVFilterGraph));
33
}
34

    
35
void avfilter_graph_free(AVFilterGraph *graph)
36
{
37
    for(; graph->filter_count > 0; graph->filter_count --)
38
        avfilter_free(graph->filters[graph->filter_count - 1]);
39
    av_freep(&graph->scale_sws_opts);
40
    av_freep(&graph->filters);
41
}
42

    
43
int avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
44
{
45
    AVFilterContext **filters = av_realloc(graph->filters,
46
                                           sizeof(AVFilterContext*) * (graph->filter_count+1));
47
    if (!filters)
48
        return AVERROR(ENOMEM);
49

    
50
    graph->filters = filters;
51
    graph->filters[graph->filter_count++] = filter;
52

    
53
    return 0;
54
}
55

    
56
int avfilter_graph_create_filter(AVFilterContext **filt_ctx, AVFilter *filt,
57
                                 const char *name, const char *args, void *opaque,
58
                                 AVFilterGraph *graph_ctx)
59
{
60
    int ret;
61

    
62
    if ((ret = avfilter_open(filt_ctx, filt, name)) < 0)
63
        goto fail;
64
    if ((ret = avfilter_init_filter(*filt_ctx, args, opaque)) < 0)
65
        goto fail;
66
    if ((ret = avfilter_graph_add_filter(graph_ctx, *filt_ctx)) < 0)
67
        goto fail;
68
    return 0;
69

    
70
fail:
71
    if (*filt_ctx)
72
        avfilter_free(*filt_ctx);
73
    *filt_ctx = NULL;
74
    return ret;
75
}
76

    
77
int ff_avfilter_graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
78
{
79
    AVFilterContext *filt;
80
    int i, j;
81

    
82
    for (i=0; i < graph->filter_count; i++) {
83
        filt = graph->filters[i];
84

    
85
        for (j = 0; j < filt->input_count; j++) {
86
            if (!filt->inputs[j] || !filt->inputs[j]->src) {
87
                av_log(log_ctx, AV_LOG_ERROR,
88
                       "Input pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any source\n",
89
                       filt->input_pads[j].name, filt->name, filt->filter->name);
90
                return -1;
91
            }
92
        }
93

    
94
        for (j = 0; j < filt->output_count; j++) {
95
            if (!filt->outputs[j] || !filt->outputs[j]->dst) {
96
                av_log(log_ctx, AV_LOG_ERROR,
97
                       "Output pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any destination\n",
98
                       filt->output_pads[j].name, filt->name, filt->filter->name);
99
                return -1;
100
            }
101
        }
102
    }
103

    
104
    return 0;
105
}
106

    
107
int ff_avfilter_graph_config_links(AVFilterGraph *graph, AVClass *log_ctx)
108
{
109
    AVFilterContext *filt;
110
    int i, ret;
111

    
112
    for (i=0; i < graph->filter_count; i++) {
113
        filt = graph->filters[i];
114

    
115
        if (!filt->output_count) {
116
            if ((ret = avfilter_config_links(filt)))
117
                return ret;
118
        }
119
    }
120

    
121
    return 0;
122
}
123

    
124
AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
125
{
126
    int i;
127

    
128
    for(i = 0; i < graph->filter_count; i ++)
129
        if(graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
130
            return graph->filters[i];
131

    
132
    return NULL;
133
}
134

    
135
static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
136
{
137
    int i, j, ret;
138
    int scaler_count = 0;
139
    char inst_name[30];
140

    
141
    /* ask all the sub-filters for their supported media formats */
142
    for(i = 0; i < graph->filter_count; i ++) {
143
        if(graph->filters[i]->filter->query_formats)
144
            graph->filters[i]->filter->query_formats(graph->filters[i]);
145
        else
146
            avfilter_default_query_formats(graph->filters[i]);
147
    }
148

    
149
    /* go through and merge as many format lists as possible */
150
    for(i = 0; i < graph->filter_count; i ++) {
151
        AVFilterContext *filter = graph->filters[i];
152

    
153
        for(j = 0; j < filter->input_count; j ++) {
154
            AVFilterLink *link = filter->inputs[j];
155
            if(link && link->in_formats != link->out_formats) {
156
                if(!avfilter_merge_formats(link->in_formats,
157
                                           link->out_formats)) {
158
                    AVFilterContext *scale;
159
                    char scale_args[256];
160
                    /* couldn't merge format lists. auto-insert scale filter */
161
                    snprintf(inst_name, sizeof(inst_name), "auto-inserted scaler %d",
162
                             scaler_count++);
163
                    snprintf(scale_args, sizeof(scale_args), "0:0:%s", graph->scale_sws_opts);
164
                    if ((ret = avfilter_graph_create_filter(&scale, avfilter_get_by_name("scale"),
165
                                                            inst_name, scale_args, NULL, graph)) < 0)
166
                        return ret;
167
                    if ((ret = avfilter_insert_filter(link, scale, 0, 0)) < 0)
168
                        return ret;
169

    
170
                    scale->filter->query_formats(scale);
171
                    if (((link = scale-> inputs[0]) &&
172
                         !avfilter_merge_formats(link->in_formats, link->out_formats)) ||
173
                        ((link = scale->outputs[0]) &&
174
                         !avfilter_merge_formats(link->in_formats, link->out_formats))) {
175
                        av_log(log_ctx, AV_LOG_ERROR,
176
                               "Impossible to convert between the formats supported by the filter "
177
                               "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
178
                        return -1;
179
                    }
180
                }
181
            }
182
        }
183
    }
184

    
185
    return 0;
186
}
187

    
188
static void pick_format(AVFilterLink *link)
189
{
190
    if(!link || !link->in_formats)
191
        return;
192

    
193
    link->in_formats->format_count = 1;
194
    link->format = link->in_formats->formats[0];
195

    
196
    avfilter_formats_unref(&link->in_formats);
197
    avfilter_formats_unref(&link->out_formats);
198
}
199

    
200
static void pick_formats(AVFilterGraph *graph)
201
{
202
    int i, j;
203

    
204
    for(i = 0; i < graph->filter_count; i ++) {
205
        AVFilterContext *filter = graph->filters[i];
206

    
207
        for(j = 0; j < filter->input_count; j ++)
208
            pick_format(filter->inputs[j]);
209
        for(j = 0; j < filter->output_count; j ++)
210
            pick_format(filter->outputs[j]);
211
    }
212
}
213

    
214
int ff_avfilter_graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
215
{
216
    /* find supported formats from sub-filters, and merge along links */
217
    if(query_formats(graph, log_ctx))
218
        return -1;
219

    
220
    /* Once everything is merged, it's possible that we'll still have
221
     * multiple valid media format choices. We pick the first one. */
222
    pick_formats(graph);
223

    
224
    return 0;
225
}
226

    
227
int avfilter_graph_config(AVFilterGraph *graphctx, AVClass *log_ctx)
228
{
229
    int ret;
230

    
231
    if ((ret = ff_avfilter_graph_check_validity(graphctx, log_ctx)))
232
        return ret;
233
    if ((ret = ff_avfilter_graph_config_formats(graphctx, log_ctx)))
234
        return ret;
235
    if ((ret = ff_avfilter_graph_config_links(graphctx, log_ctx)))
236
        return ret;
237

    
238
    return 0;
239
}