]> git.sesse.net Git - ffmpeg/blob - libavfilter/avfiltergraph.c
Merge remote-tracking branch 'luzero/pulse'
[ffmpeg] / libavfilter / avfiltergraph.c
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 "libavutil/audioconvert.h"
27 #include "avfilter.h"
28 #include "avfiltergraph.h"
29 #include "internal.h"
30
31 AVFilterGraph *avfilter_graph_alloc(void)
32 {
33     return av_mallocz(sizeof(AVFilterGraph));
34 }
35
36 void avfilter_graph_free(AVFilterGraph **graph)
37 {
38     if (!*graph)
39         return;
40     for (; (*graph)->filter_count > 0; (*graph)->filter_count--)
41         avfilter_free((*graph)->filters[(*graph)->filter_count - 1]);
42     av_freep(&(*graph)->scale_sws_opts);
43     av_freep(&(*graph)->filters);
44     av_freep(graph);
45 }
46
47 int avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
48 {
49     AVFilterContext **filters = av_realloc(graph->filters,
50                                            sizeof(AVFilterContext*) * (graph->filter_count+1));
51     if (!filters)
52         return AVERROR(ENOMEM);
53
54     graph->filters = filters;
55     graph->filters[graph->filter_count++] = filter;
56
57     return 0;
58 }
59
60 int avfilter_graph_create_filter(AVFilterContext **filt_ctx, AVFilter *filt,
61                                  const char *name, const char *args, void *opaque,
62                                  AVFilterGraph *graph_ctx)
63 {
64     int ret;
65
66     if ((ret = avfilter_open(filt_ctx, filt, name)) < 0)
67         goto fail;
68     if ((ret = avfilter_init_filter(*filt_ctx, args, opaque)) < 0)
69         goto fail;
70     if ((ret = avfilter_graph_add_filter(graph_ctx, *filt_ctx)) < 0)
71         goto fail;
72     return 0;
73
74 fail:
75     if (*filt_ctx)
76         avfilter_free(*filt_ctx);
77     *filt_ctx = NULL;
78     return ret;
79 }
80
81 int ff_avfilter_graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
82 {
83     AVFilterContext *filt;
84     int i, j;
85
86     for (i = 0; i < graph->filter_count; i++) {
87         filt = graph->filters[i];
88
89         for (j = 0; j < filt->input_count; j++) {
90             if (!filt->inputs[j] || !filt->inputs[j]->src) {
91                 av_log(log_ctx, AV_LOG_ERROR,
92                        "Input pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any source\n",
93                        filt->input_pads[j].name, filt->name, filt->filter->name);
94                 return AVERROR(EINVAL);
95             }
96         }
97
98         for (j = 0; j < filt->output_count; j++) {
99             if (!filt->outputs[j] || !filt->outputs[j]->dst) {
100                 av_log(log_ctx, AV_LOG_ERROR,
101                        "Output pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any destination\n",
102                        filt->output_pads[j].name, filt->name, filt->filter->name);
103                 return AVERROR(EINVAL);
104             }
105         }
106     }
107
108     return 0;
109 }
110
111 int ff_avfilter_graph_config_links(AVFilterGraph *graph, AVClass *log_ctx)
112 {
113     AVFilterContext *filt;
114     int i, ret;
115
116     for (i=0; i < graph->filter_count; i++) {
117         filt = graph->filters[i];
118
119         if (!filt->output_count) {
120             if ((ret = avfilter_config_links(filt)))
121                 return ret;
122         }
123     }
124
125     return 0;
126 }
127
128 AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
129 {
130     int i;
131
132     for (i = 0; i < graph->filter_count; i++)
133         if (graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
134             return graph->filters[i];
135
136     return NULL;
137 }
138
139 static int insert_conv_filter(AVFilterGraph *graph, AVFilterLink *link,
140                               const char *filt_name, const char *filt_args)
141 {
142     static int auto_count = 0, ret;
143     char inst_name[32];
144     AVFilterContext *filt_ctx;
145
146     snprintf(inst_name, sizeof(inst_name), "auto-inserted %s %d",
147             filt_name, auto_count++);
148
149     if ((ret = avfilter_graph_create_filter(&filt_ctx,
150                                             avfilter_get_by_name(filt_name),
151                                             inst_name, filt_args, NULL, graph)) < 0)
152         return ret;
153     if ((ret = avfilter_insert_filter(link, filt_ctx, 0, 0)) < 0)
154         return ret;
155
156     filt_ctx->filter->query_formats(filt_ctx);
157
158     if ( ((link = filt_ctx-> inputs[0]) &&
159            !avfilter_merge_formats(link->in_formats, link->out_formats)) ||
160          ((link = filt_ctx->outputs[0]) &&
161            !avfilter_merge_formats(link->in_formats, link->out_formats))
162        ) {
163         av_log(NULL, AV_LOG_ERROR,
164                "Impossible to convert between the formats supported by the filter "
165                "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
166         return AVERROR(EINVAL);
167     }
168
169     if (link->type == AVMEDIA_TYPE_AUDIO &&
170          (((link = filt_ctx-> inputs[0]) &&
171            (!avfilter_merge_formats(link->in_chlayouts, link->out_chlayouts) ||
172             !avfilter_merge_formats(link->in_packing,   link->out_packing))) ||
173          ((link = filt_ctx->outputs[0]) &&
174            (!avfilter_merge_formats(link->in_chlayouts, link->out_chlayouts) ||
175             !avfilter_merge_formats(link->in_packing,   link->out_packing))))
176        ) {
177         av_log(NULL, AV_LOG_ERROR,
178                "Impossible to convert between the channel layouts/packing formats supported by the filter "
179                "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
180         return AVERROR(EINVAL);
181     }
182
183     return 0;
184 }
185
186 static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
187 {
188     int i, j, ret;
189     char filt_args[128];
190
191     /* ask all the sub-filters for their supported media formats */
192     for (i = 0; i < graph->filter_count; i++) {
193         if (graph->filters[i]->filter->query_formats)
194             graph->filters[i]->filter->query_formats(graph->filters[i]);
195         else
196             avfilter_default_query_formats(graph->filters[i]);
197     }
198
199     /* go through and merge as many format lists as possible */
200     for (i = 0; i < graph->filter_count; i++) {
201         AVFilterContext *filter = graph->filters[i];
202
203         for (j = 0; j < filter->input_count; j++) {
204             AVFilterLink *link = filter->inputs[j];
205             if (!link) continue;
206
207             if (!link->in_formats || !link->out_formats)
208                 return AVERROR(EINVAL);
209
210             if (link->type == AVMEDIA_TYPE_VIDEO &&
211                 !avfilter_merge_formats(link->in_formats, link->out_formats)) {
212
213                 /* couldn't merge format lists, auto-insert scale filter */
214                 snprintf(filt_args, sizeof(filt_args), "0:0:%s",
215                          graph->scale_sws_opts);
216                 if (ret = insert_conv_filter(graph, link, "scale", filt_args))
217                     return ret;
218             }
219             else if (link->type == AVMEDIA_TYPE_AUDIO) {
220                 if (!link->in_chlayouts || !link->out_chlayouts ||
221                     !link->in_packing   || !link->out_packing)
222                     return AVERROR(EINVAL);
223
224                 if (!avfilter_merge_formats(link->in_formats,   link->out_formats)   ||
225                     !avfilter_merge_formats(link->in_chlayouts, link->out_chlayouts) ||
226                     !avfilter_merge_formats(link->in_packing,   link->out_packing))
227                     if (ret = insert_conv_filter(graph, link, "aconvert", NULL))
228                        return ret;
229             }
230         }
231     }
232
233     return 0;
234 }
235
236 static void pick_format(AVFilterLink *link)
237 {
238     if (!link || !link->in_formats)
239         return;
240
241     link->in_formats->format_count = 1;
242     link->format = link->in_formats->formats[0];
243     avfilter_formats_unref(&link->in_formats);
244     avfilter_formats_unref(&link->out_formats);
245
246     if (link->type == AVMEDIA_TYPE_AUDIO) {
247         link->in_chlayouts->format_count = 1;
248         link->channel_layout = link->in_chlayouts->formats[0];
249         avfilter_formats_unref(&link->in_chlayouts);
250         avfilter_formats_unref(&link->out_chlayouts);
251
252         link->in_packing->format_count = 1;
253         link->planar = link->in_packing->formats[0] == AVFILTER_PLANAR;
254         avfilter_formats_unref(&link->in_packing);
255         avfilter_formats_unref(&link->out_packing);
256     }
257 }
258
259 static void pick_formats(AVFilterGraph *graph)
260 {
261     int i, j;
262
263     for (i = 0; i < graph->filter_count; i++) {
264         AVFilterContext *filter = graph->filters[i];
265
266         for (j = 0; j < filter->input_count; j++)
267             pick_format(filter->inputs[j]);
268         for (j = 0; j < filter->output_count; j++)
269             pick_format(filter->outputs[j]);
270     }
271 }
272
273 int ff_avfilter_graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
274 {
275     int ret;
276
277     /* find supported formats from sub-filters, and merge along links */
278     if ((ret = query_formats(graph, log_ctx)) < 0)
279         return ret;
280
281     /* Once everything is merged, it's possible that we'll still have
282      * multiple valid media format choices. We pick the first one. */
283     pick_formats(graph);
284
285     return 0;
286 }
287
288 int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
289 {
290     int ret;
291
292     if ((ret = ff_avfilter_graph_check_validity(graphctx, log_ctx)))
293         return ret;
294     if ((ret = ff_avfilter_graph_config_formats(graphctx, log_ctx)))
295         return ret;
296     if ((ret = ff_avfilter_graph_config_links(graphctx, log_ctx)))
297         return ret;
298
299     return 0;
300 }
301
302 int avfilter_graph_send_command(AVFilterGraph *graph, const char *target, const char *cmd, const char *arg, char *res, int res_len, int flags)
303 {
304     int i, r = AVERROR(ENOSYS);
305
306     if(!graph)
307         return r;
308
309     if((flags & AVFILTER_CMD_FLAG_ONE) && !(flags & AVFILTER_CMD_FLAG_FAST)) {
310         r=avfilter_graph_send_command(graph, target, cmd, arg, res, res_len, flags | AVFILTER_CMD_FLAG_FAST);
311         if(r != AVERROR(ENOSYS))
312             return r;
313     }
314
315     if(res_len && res)
316         res[0]= 0;
317
318     for (i = 0; i < graph->filter_count; i++) {
319         AVFilterContext *filter = graph->filters[i];
320         if(!strcmp(target, "all") || (filter->name && !strcmp(target, filter->name)) || !strcmp(target, filter->filter->name)){
321             r = avfilter_process_command(filter, cmd, arg, res, res_len, flags);
322             if(r != AVERROR(ENOSYS)) {
323                 if((flags & AVFILTER_CMD_FLAG_ONE) || r<0)
324                     return r;
325             }
326         }
327     }
328
329     return r;
330 }
331
332 int avfilter_graph_queue_command(AVFilterGraph *graph, const char *target, const char *command, const char *arg, int flags, double ts)
333 {
334     int i;
335
336     if(!graph)
337         return 0;
338
339     for (i = 0; i < graph->filter_count; i++) {
340         AVFilterContext *filter = graph->filters[i];
341         if(filter && (!strcmp(target, "all") || !strcmp(target, filter->name) || !strcmp(target, filter->filter->name))){
342             AVFilterCommand **que = &filter->command_queue, *next;
343             while(*que && (*que)->time <= ts)
344                 que = &(*que)->next;
345             next= *que;
346             *que= av_mallocz(sizeof(AVFilterCommand));
347             (*que)->command = av_strdup(command);
348             (*que)->arg     = av_strdup(arg);
349             (*que)->time    = ts;
350             (*que)->flags   = flags;
351             (*que)->next    = next;
352             if(flags & AVFILTER_CMD_FLAG_ONE)
353                 return 0;
354         }
355     }
356
357     return 0;
358 }