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