ef597ddf7f73d32ea68c0e6688a40a0a4d4a73fc
[ffmpeg.git] / 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 "libavutil/avassert.h"
28 #include "libavutil/pixdesc.h"
29 #include "avfilter.h"
30 #include "avfiltergraph.h"
31 #include "internal.h"
32
33 #include "libavutil/log.h"
34
35 static const AVClass filtergraph_class = {
36     .class_name = "AVFilterGraph",
37     .item_name  = av_default_item_name,
38     .version    = LIBAVUTIL_VERSION_INT,
39 };
40
41 AVFilterGraph *avfilter_graph_alloc(void)
42 {
43     AVFilterGraph *ret = av_mallocz(sizeof(AVFilterGraph));
44     if (!ret)
45         return NULL;
46 #if FF_API_GRAPH_AVCLASS
47     ret->av_class = &filtergraph_class;
48 #endif
49     return ret;
50 }
51
52 void avfilter_graph_free(AVFilterGraph **graph)
53 {
54     if (!*graph)
55         return;
56     for (; (*graph)->filter_count > 0; (*graph)->filter_count--)
57         avfilter_free((*graph)->filters[(*graph)->filter_count - 1]);
58     av_freep(&(*graph)->scale_sws_opts);
59     av_freep(&(*graph)->filters);
60     av_freep(graph);
61 }
62
63 int avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
64 {
65     AVFilterContext **filters = av_realloc(graph->filters,
66                                            sizeof(AVFilterContext*) * (graph->filter_count+1));
67     if (!filters)
68         return AVERROR(ENOMEM);
69
70     graph->filters = filters;
71     graph->filters[graph->filter_count++] = filter;
72
73     return 0;
74 }
75
76 int avfilter_graph_create_filter(AVFilterContext **filt_ctx, AVFilter *filt,
77                                  const char *name, const char *args, void *opaque,
78                                  AVFilterGraph *graph_ctx)
79 {
80     int ret;
81
82     if ((ret = avfilter_open(filt_ctx, filt, name)) < 0)
83         goto fail;
84     if ((ret = avfilter_init_filter(*filt_ctx, args, opaque)) < 0)
85         goto fail;
86     if ((ret = avfilter_graph_add_filter(graph_ctx, *filt_ctx)) < 0)
87         goto fail;
88     return 0;
89
90 fail:
91     if (*filt_ctx)
92         avfilter_free(*filt_ctx);
93     *filt_ctx = NULL;
94     return ret;
95 }
96
97 int ff_avfilter_graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
98 {
99     AVFilterContext *filt;
100     int i, j;
101
102     for (i = 0; i < graph->filter_count; i++) {
103         filt = graph->filters[i];
104
105         for (j = 0; j < filt->input_count; j++) {
106             if (!filt->inputs[j] || !filt->inputs[j]->src) {
107                 av_log(log_ctx, AV_LOG_ERROR,
108                        "Input pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any source\n",
109                        filt->input_pads[j].name, filt->name, filt->filter->name);
110                 return AVERROR(EINVAL);
111             }
112         }
113
114         for (j = 0; j < filt->output_count; j++) {
115             if (!filt->outputs[j] || !filt->outputs[j]->dst) {
116                 av_log(log_ctx, AV_LOG_ERROR,
117                        "Output pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any destination\n",
118                        filt->output_pads[j].name, filt->name, filt->filter->name);
119                 return AVERROR(EINVAL);
120             }
121         }
122     }
123
124     return 0;
125 }
126
127 int ff_avfilter_graph_config_links(AVFilterGraph *graph, AVClass *log_ctx)
128 {
129     AVFilterContext *filt;
130     int i, ret;
131
132     for (i=0; i < graph->filter_count; i++) {
133         filt = graph->filters[i];
134
135         if (!filt->output_count) {
136             if ((ret = avfilter_config_links(filt)))
137                 return ret;
138         }
139     }
140
141     return 0;
142 }
143
144 AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
145 {
146     int i;
147
148     for (i = 0; i < graph->filter_count; i++)
149         if (graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
150             return graph->filters[i];
151
152     return NULL;
153 }
154
155 static int insert_conv_filter(AVFilterGraph *graph, AVFilterLink *link,
156                               const char *filt_name, const char *filt_args)
157 {
158     static int auto_count = 0, ret;
159     char inst_name[32];
160     AVFilterContext *filt_ctx;
161
162     snprintf(inst_name, sizeof(inst_name), "auto-inserted %s %d",
163             filt_name, auto_count++);
164
165     if ((ret = avfilter_graph_create_filter(&filt_ctx,
166                                             avfilter_get_by_name(filt_name),
167                                             inst_name, filt_args, NULL, graph)) < 0)
168         return ret;
169     if ((ret = avfilter_insert_filter(link, filt_ctx, 0, 0)) < 0)
170         return ret;
171
172     filt_ctx->filter->query_formats(filt_ctx);
173
174     if ( ((link = filt_ctx-> inputs[0]) &&
175            !avfilter_merge_formats(link->in_formats, link->out_formats)) ||
176          ((link = filt_ctx->outputs[0]) &&
177            !avfilter_merge_formats(link->in_formats, link->out_formats))
178        ) {
179         av_log(NULL, AV_LOG_ERROR,
180                "Impossible to convert between the formats supported by the filter "
181                "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
182         return AVERROR(EINVAL);
183     }
184
185     if (link->type == AVMEDIA_TYPE_AUDIO &&
186          (((link = filt_ctx-> inputs[0]) &&
187            (!avfilter_merge_formats(link->in_chlayouts, link->out_chlayouts) ||
188             !avfilter_merge_formats(link->in_packing,   link->out_packing))) ||
189          ((link = filt_ctx->outputs[0]) &&
190            (!avfilter_merge_formats(link->in_chlayouts, link->out_chlayouts) ||
191             !avfilter_merge_formats(link->in_packing,   link->out_packing))))
192        ) {
193         av_log(NULL, AV_LOG_ERROR,
194                "Impossible to convert between the channel layouts/packing formats supported by the filter "
195                "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
196         return AVERROR(EINVAL);
197     }
198
199     return 0;
200 }
201
202 static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
203 {
204     int i, j, ret;
205     char filt_args[128];
206     AVFilterFormats *formats, *chlayouts, *packing;
207
208     /* ask all the sub-filters for their supported media formats */
209     for (i = 0; i < graph->filter_count; i++) {
210         if (graph->filters[i]->filter->query_formats)
211             graph->filters[i]->filter->query_formats(graph->filters[i]);
212         else
213             avfilter_default_query_formats(graph->filters[i]);
214     }
215
216     /* go through and merge as many format lists as possible */
217     for (i = 0; i < graph->filter_count; i++) {
218         AVFilterContext *filter = graph->filters[i];
219
220         for (j = 0; j < filter->input_count; j++) {
221             AVFilterLink *link = filter->inputs[j];
222             if (!link) continue;
223
224             if (!link->in_formats || !link->out_formats)
225                 return AVERROR(EINVAL);
226
227             if (link->type == AVMEDIA_TYPE_VIDEO &&
228                 !avfilter_merge_formats(link->in_formats, link->out_formats)) {
229
230                 /* couldn't merge format lists, auto-insert scale filter */
231                 snprintf(filt_args, sizeof(filt_args), "0:0:%s",
232                          graph->scale_sws_opts);
233                 if (ret = insert_conv_filter(graph, link, "scale", filt_args))
234                     return ret;
235             }
236             else if (link->type == AVMEDIA_TYPE_AUDIO) {
237                 if (!link->in_chlayouts || !link->out_chlayouts ||
238                     !link->in_packing   || !link->out_packing)
239                     return AVERROR(EINVAL);
240
241                 /* Merge all three list before checking: that way, in all
242                  * three categories, aconvert will use a common format
243                  * whenever possible. */
244                 formats   = avfilter_merge_formats(link->in_formats,   link->out_formats);
245                 chlayouts = avfilter_merge_formats(link->in_chlayouts, link->out_chlayouts);
246                 packing   = avfilter_merge_formats(link->in_packing,   link->out_packing);
247                 if (!formats || !chlayouts || !packing)
248                     if (ret = insert_conv_filter(graph, link, "aconvert", NULL))
249                        return ret;
250             }
251         }
252     }
253
254     return 0;
255 }
256
257 static void pick_format(AVFilterLink *link, AVFilterLink *ref)
258 {
259     if (!link || !link->in_formats)
260         return;
261
262     if (link->type == AVMEDIA_TYPE_VIDEO) {
263         if(ref && ref->type == AVMEDIA_TYPE_VIDEO){
264             int has_alpha= av_pix_fmt_descriptors[ref->format].nb_components % 2 == 0;
265             enum PixelFormat best= PIX_FMT_NONE;
266             int i;
267             for (i=0; i<link->in_formats->format_count; i++) {
268                 enum PixelFormat p = link->in_formats->formats[i];
269                 best= avcodec_find_best_pix_fmt2(best, p, ref->format, has_alpha, NULL);
270             }
271             link->format = best;
272         }else
273             link->format = link->in_formats->formats[0];
274     }
275
276     link->in_formats->format_count = 1;
277     avfilter_formats_unref(&link->in_formats);
278     avfilter_formats_unref(&link->out_formats);
279
280     if (link->type == AVMEDIA_TYPE_AUDIO) {
281         link->in_chlayouts->format_count = 1;
282         link->channel_layout = link->in_chlayouts->formats[0];
283         avfilter_formats_unref(&link->in_chlayouts);
284         avfilter_formats_unref(&link->out_chlayouts);
285
286         link->in_packing->format_count = 1;
287         link->planar = link->in_packing->formats[0] == AVFILTER_PLANAR;
288         avfilter_formats_unref(&link->in_packing);
289         avfilter_formats_unref(&link->out_packing);
290     }
291 }
292
293 static int reduce_formats_on_filter(AVFilterContext *filter)
294 {
295     int i, j, k, ret = 0;
296
297     for (i = 0; i < filter->input_count; i++) {
298         AVFilterLink *link = filter->inputs[i];
299         int         format = link->out_formats->formats[0];
300
301         if (link->out_formats->format_count != 1)
302             continue;
303
304         for (j = 0; j < filter->output_count; j++) {
305             AVFilterLink *out_link = filter->outputs[j];
306             AVFilterFormats  *fmts = out_link->in_formats;
307
308             if (link->type != out_link->type ||
309                 out_link->in_formats->format_count == 1)
310                 continue;
311
312             for (k = 0; k < out_link->in_formats->format_count; k++)
313                 if (fmts->formats[k] == format) {
314                     fmts->formats[0]   = format;
315                     fmts->format_count = 1;
316                     ret = 1;
317                     break;
318                 }
319         }
320     }
321     return ret;
322 }
323
324 static void reduce_formats(AVFilterGraph *graph)
325 {
326     int i, reduced;
327
328     do {
329         reduced = 0;
330
331         for (i = 0; i < graph->filter_count; i++)
332             reduced |= reduce_formats_on_filter(graph->filters[i]);
333     } while (reduced);
334 }
335
336 static void pick_formats(AVFilterGraph *graph)
337 {
338     int i, j;
339
340     for (i = 0; i < graph->filter_count; i++) {
341         AVFilterContext *filter = graph->filters[i];
342         if (filter->input_count && filter->output_count) {
343             for (j = 0; j < filter->input_count; j++)
344                 pick_format(filter->inputs[j], NULL);
345             for (j = 0; j < filter->output_count; j++)
346                 pick_format(filter->outputs[j], filter->inputs[0]);
347         }
348     }
349     for (i = 0; i < graph->filter_count; i++) {
350         AVFilterContext *filter = graph->filters[i];
351         if (!(filter->input_count && filter->output_count)) {
352             for (j = 0; j < filter->input_count; j++)
353                 pick_format(filter->inputs[j], NULL);
354             for (j = 0; j < filter->output_count; j++)
355                 pick_format(filter->outputs[j], NULL);
356         }
357     }
358 }
359
360 int ff_avfilter_graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
361 {
362     int ret;
363
364     /* find supported formats from sub-filters, and merge along links */
365     if ((ret = query_formats(graph, log_ctx)) < 0)
366         return ret;
367
368     /* Once everything is merged, it's possible that we'll still have
369      * multiple valid media format choices. We try to minimize the amount
370      * of format conversion inside filters */
371     reduce_formats(graph);
372
373     pick_formats(graph);
374
375     return 0;
376 }
377
378 static int ff_avfilter_graph_config_pointers(AVFilterGraph *graph,
379                                              AVClass *log_ctx)
380 {
381     unsigned i, j;
382     int sink_links_count = 0, n = 0;
383     AVFilterContext *f;
384     AVFilterLink **sinks;
385
386     for (i = 0; i < graph->filter_count; i++) {
387         f = graph->filters[i];
388         for (j = 0; j < f->input_count; j++) {
389             f->inputs[j]->graph     = graph;
390             f->inputs[j]->age_index = -1;
391         }
392         for (j = 0; j < f->output_count; j++) {
393             f->outputs[j]->graph    = graph;
394             f->outputs[j]->age_index= -1;
395         }
396         if (!f->output_count) {
397             if (f->input_count > INT_MAX - sink_links_count)
398                 return AVERROR(EINVAL);
399             sink_links_count += f->input_count;
400         }
401     }
402     sinks = av_calloc(sink_links_count, sizeof(*sinks));
403     if (!sinks)
404         return AVERROR(ENOMEM);
405     for (i = 0; i < graph->filter_count; i++) {
406         f = graph->filters[i];
407         if (!f->output_count) {
408             for (j = 0; j < f->input_count; j++) {
409                 sinks[n] = f->inputs[j];
410                 f->inputs[j]->age_index = n++;
411             }
412         }
413     }
414     av_assert0(n == sink_links_count);
415     graph->sink_links       = sinks;
416     graph->sink_links_count = sink_links_count;
417     return 0;
418 }
419
420 int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
421 {
422     int ret;
423
424     if ((ret = ff_avfilter_graph_check_validity(graphctx, log_ctx)))
425         return ret;
426     if ((ret = ff_avfilter_graph_config_formats(graphctx, log_ctx)))
427         return ret;
428     if ((ret = ff_avfilter_graph_config_links(graphctx, log_ctx)))
429         return ret;
430     if ((ret = ff_avfilter_graph_config_pointers(graphctx, log_ctx)))
431         return ret;
432
433     return 0;
434 }
435
436 int avfilter_graph_send_command(AVFilterGraph *graph, const char *target, const char *cmd, const char *arg, char *res, int res_len, int flags)
437 {
438     int i, r = AVERROR(ENOSYS);
439
440     if(!graph)
441         return r;
442
443     if((flags & AVFILTER_CMD_FLAG_ONE) && !(flags & AVFILTER_CMD_FLAG_FAST)) {
444         r=avfilter_graph_send_command(graph, target, cmd, arg, res, res_len, flags | AVFILTER_CMD_FLAG_FAST);
445         if(r != AVERROR(ENOSYS))
446             return r;
447     }
448
449     if(res_len && res)
450         res[0]= 0;
451
452     for (i = 0; i < graph->filter_count; i++) {
453         AVFilterContext *filter = graph->filters[i];
454         if(!strcmp(target, "all") || (filter->name && !strcmp(target, filter->name)) || !strcmp(target, filter->filter->name)){
455             r = avfilter_process_command(filter, cmd, arg, res, res_len, flags);
456             if(r != AVERROR(ENOSYS)) {
457                 if((flags & AVFILTER_CMD_FLAG_ONE) || r<0)
458                     return r;
459             }
460         }
461     }
462
463     return r;
464 }
465
466 int avfilter_graph_queue_command(AVFilterGraph *graph, const char *target, const char *command, const char *arg, int flags, double ts)
467 {
468     int i;
469
470     if(!graph)
471         return 0;
472
473     for (i = 0; i < graph->filter_count; i++) {
474         AVFilterContext *filter = graph->filters[i];
475         if(filter && (!strcmp(target, "all") || !strcmp(target, filter->name) || !strcmp(target, filter->filter->name))){
476             AVFilterCommand **que = &filter->command_queue, *next;
477             while(*que && (*que)->time <= ts)
478                 que = &(*que)->next;
479             next= *que;
480             *que= av_mallocz(sizeof(AVFilterCommand));
481             (*que)->command = av_strdup(command);
482             (*que)->arg     = av_strdup(arg);
483             (*que)->time    = ts;
484             (*que)->flags   = flags;
485             (*que)->next    = next;
486             if(flags & AVFILTER_CMD_FLAG_ONE)
487                 return 0;
488         }
489     }
490
491     return 0;
492 }
493
494 static void heap_bubble_up(AVFilterGraph *graph,
495                            AVFilterLink *link, int index)
496 {
497     AVFilterLink **links = graph->sink_links;
498
499     while (index) {
500         int parent = (index - 1) >> 1;
501         if (links[parent]->current_pts >= link->current_pts)
502             break;
503         links[index] = links[parent];
504         links[index]->age_index = index;
505         index = parent;
506     }
507     links[index] = link;
508     link->age_index = index;
509 }
510
511 static void heap_bubble_down(AVFilterGraph *graph,
512                              AVFilterLink *link, int index)
513 {
514     AVFilterLink **links = graph->sink_links;
515
516     while (1) {
517         int child = 2 * index + 1;
518         if (child >= graph->sink_links_count)
519             break;
520         if (child + 1 < graph->sink_links_count &&
521             links[child + 1]->current_pts < links[child]->current_pts)
522             child++;
523         if (link->current_pts < links[child]->current_pts)
524             break;
525         links[index] = links[child];
526         links[index]->age_index = index;
527         index = child;
528     }
529     links[index] = link;
530     link->age_index = index;
531 }
532
533 void ff_avfilter_graph_update_heap(AVFilterGraph *graph, AVFilterLink *link)
534 {
535     heap_bubble_up  (graph, link, link->age_index);
536     heap_bubble_down(graph, link, link->age_index);
537 }
538
539
540 int avfilter_graph_request_oldest(AVFilterGraph *graph)
541 {
542     while (graph->sink_links_count) {
543         AVFilterLink *oldest = graph->sink_links[0];
544         int r = avfilter_request_frame(oldest);
545         if (r != AVERROR_EOF)
546             return r;
547         /* EOF: remove the link from the heap */
548         if (oldest->age_index < --graph->sink_links_count)
549             heap_bubble_down(graph, graph->sink_links[graph->sink_links_count],
550                              oldest->age_index);
551         oldest->age_index = -1;
552     }
553     return AVERROR_EOF;
554 }