]> git.sesse.net Git - ffmpeg/blob - libavfilter/avfiltergraph.c
avfiltergraph: improve pick_format()
[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 "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->in_formats->formats[0] = best;
273         }
274     }
275
276     link->in_formats->format_count = 1;
277     link->format = link->in_formats->formats[0];
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     int change;
341
342     do{
343         change = 0;
344         for (i = 0; i < graph->filter_count; i++) {
345             AVFilterContext *filter = graph->filters[i];
346             if (filter->input_count){
347                 for (j = 0; j < filter->input_count; j++){
348                     if(filter->inputs[j]->in_formats && filter->inputs[j]->in_formats->format_count == 1) {
349                         pick_format(filter->inputs[j], NULL);
350                         change = 1;
351                     }
352                 }
353             }
354             if (filter->output_count){
355                 for (j = 0; j < filter->output_count; j++){
356                     if(filter->outputs[j]->in_formats && filter->outputs[j]->in_formats->format_count == 1) {
357                         pick_format(filter->outputs[j], NULL);
358                         change = 1;
359                     }
360                 }
361             }
362             if (filter->input_count && filter->output_count && filter->inputs[0]->format>=0) {
363                 for (j = 0; j < filter->output_count; j++) {
364                     if(filter->outputs[j]->format<0) {
365                         pick_format(filter->outputs[j], filter->inputs[0]);
366                         change = 1;
367                     }
368                 }
369             }
370         }
371     }while(change);
372
373     for (i = 0; i < graph->filter_count; i++) {
374         AVFilterContext *filter = graph->filters[i];
375         if (1) {
376             for (j = 0; j < filter->input_count; j++)
377                 pick_format(filter->inputs[j], NULL);
378             for (j = 0; j < filter->output_count; j++)
379                 pick_format(filter->outputs[j], NULL);
380         }
381     }
382 }
383
384 int ff_avfilter_graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
385 {
386     int ret;
387
388     /* find supported formats from sub-filters, and merge along links */
389     if ((ret = query_formats(graph, log_ctx)) < 0)
390         return ret;
391
392     /* Once everything is merged, it's possible that we'll still have
393      * multiple valid media format choices. We try to minimize the amount
394      * of format conversion inside filters */
395     reduce_formats(graph);
396
397     pick_formats(graph);
398
399     return 0;
400 }
401
402 static int ff_avfilter_graph_config_pointers(AVFilterGraph *graph,
403                                              AVClass *log_ctx)
404 {
405     unsigned i, j;
406     int sink_links_count = 0, n = 0;
407     AVFilterContext *f;
408     AVFilterLink **sinks;
409
410     for (i = 0; i < graph->filter_count; i++) {
411         f = graph->filters[i];
412         for (j = 0; j < f->input_count; j++) {
413             f->inputs[j]->graph     = graph;
414             f->inputs[j]->age_index = -1;
415         }
416         for (j = 0; j < f->output_count; j++) {
417             f->outputs[j]->graph    = graph;
418             f->outputs[j]->age_index= -1;
419         }
420         if (!f->output_count) {
421             if (f->input_count > INT_MAX - sink_links_count)
422                 return AVERROR(EINVAL);
423             sink_links_count += f->input_count;
424         }
425     }
426     sinks = av_calloc(sink_links_count, sizeof(*sinks));
427     if (!sinks)
428         return AVERROR(ENOMEM);
429     for (i = 0; i < graph->filter_count; i++) {
430         f = graph->filters[i];
431         if (!f->output_count) {
432             for (j = 0; j < f->input_count; j++) {
433                 sinks[n] = f->inputs[j];
434                 f->inputs[j]->age_index = n++;
435             }
436         }
437     }
438     av_assert0(n == sink_links_count);
439     graph->sink_links       = sinks;
440     graph->sink_links_count = sink_links_count;
441     return 0;
442 }
443
444 int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
445 {
446     int ret;
447
448     if ((ret = ff_avfilter_graph_check_validity(graphctx, log_ctx)))
449         return ret;
450     if ((ret = ff_avfilter_graph_config_formats(graphctx, log_ctx)))
451         return ret;
452     if ((ret = ff_avfilter_graph_config_links(graphctx, log_ctx)))
453         return ret;
454     if ((ret = ff_avfilter_graph_config_pointers(graphctx, log_ctx)))
455         return ret;
456
457     return 0;
458 }
459
460 int avfilter_graph_send_command(AVFilterGraph *graph, const char *target, const char *cmd, const char *arg, char *res, int res_len, int flags)
461 {
462     int i, r = AVERROR(ENOSYS);
463
464     if(!graph)
465         return r;
466
467     if((flags & AVFILTER_CMD_FLAG_ONE) && !(flags & AVFILTER_CMD_FLAG_FAST)) {
468         r=avfilter_graph_send_command(graph, target, cmd, arg, res, res_len, flags | AVFILTER_CMD_FLAG_FAST);
469         if(r != AVERROR(ENOSYS))
470             return r;
471     }
472
473     if(res_len && res)
474         res[0]= 0;
475
476     for (i = 0; i < graph->filter_count; i++) {
477         AVFilterContext *filter = graph->filters[i];
478         if(!strcmp(target, "all") || (filter->name && !strcmp(target, filter->name)) || !strcmp(target, filter->filter->name)){
479             r = avfilter_process_command(filter, cmd, arg, res, res_len, flags);
480             if(r != AVERROR(ENOSYS)) {
481                 if((flags & AVFILTER_CMD_FLAG_ONE) || r<0)
482                     return r;
483             }
484         }
485     }
486
487     return r;
488 }
489
490 int avfilter_graph_queue_command(AVFilterGraph *graph, const char *target, const char *command, const char *arg, int flags, double ts)
491 {
492     int i;
493
494     if(!graph)
495         return 0;
496
497     for (i = 0; i < graph->filter_count; i++) {
498         AVFilterContext *filter = graph->filters[i];
499         if(filter && (!strcmp(target, "all") || !strcmp(target, filter->name) || !strcmp(target, filter->filter->name))){
500             AVFilterCommand **que = &filter->command_queue, *next;
501             while(*que && (*que)->time <= ts)
502                 que = &(*que)->next;
503             next= *que;
504             *que= av_mallocz(sizeof(AVFilterCommand));
505             (*que)->command = av_strdup(command);
506             (*que)->arg     = av_strdup(arg);
507             (*que)->time    = ts;
508             (*que)->flags   = flags;
509             (*que)->next    = next;
510             if(flags & AVFILTER_CMD_FLAG_ONE)
511                 return 0;
512         }
513     }
514
515     return 0;
516 }
517
518 static void heap_bubble_up(AVFilterGraph *graph,
519                            AVFilterLink *link, int index)
520 {
521     AVFilterLink **links = graph->sink_links;
522
523     while (index) {
524         int parent = (index - 1) >> 1;
525         if (links[parent]->current_pts >= link->current_pts)
526             break;
527         links[index] = links[parent];
528         links[index]->age_index = index;
529         index = parent;
530     }
531     links[index] = link;
532     link->age_index = index;
533 }
534
535 static void heap_bubble_down(AVFilterGraph *graph,
536                              AVFilterLink *link, int index)
537 {
538     AVFilterLink **links = graph->sink_links;
539
540     while (1) {
541         int child = 2 * index + 1;
542         if (child >= graph->sink_links_count)
543             break;
544         if (child + 1 < graph->sink_links_count &&
545             links[child + 1]->current_pts < links[child]->current_pts)
546             child++;
547         if (link->current_pts < links[child]->current_pts)
548             break;
549         links[index] = links[child];
550         links[index]->age_index = index;
551         index = child;
552     }
553     links[index] = link;
554     link->age_index = index;
555 }
556
557 void ff_avfilter_graph_update_heap(AVFilterGraph *graph, AVFilterLink *link)
558 {
559     heap_bubble_up  (graph, link, link->age_index);
560     heap_bubble_down(graph, link, link->age_index);
561 }
562
563
564 int avfilter_graph_request_oldest(AVFilterGraph *graph)
565 {
566     while (graph->sink_links_count) {
567         AVFilterLink *oldest = graph->sink_links[0];
568         int r = avfilter_request_frame(oldest);
569         if (r != AVERROR_EOF)
570             return r;
571         /* EOF: remove the link from the heap */
572         if (oldest->age_index < --graph->sink_links_count)
573             heap_bubble_down(graph, graph->sink_links[graph->sink_links_count],
574                              oldest->age_index);
575         oldest->age_index = -1;
576     }
577     return AVERROR_EOF;
578 }