]> git.sesse.net Git - ffmpeg/blob - libavfilter/avfilter.c
lavfi: replace empty input/output lists with null pointers
[ffmpeg] / libavfilter / avfilter.c
1 /*
2  * filter layer
3  * Copyright (c) 2007 Bobby Bingham
4  *
5  * This file is part of Libav.
6  *
7  * Libav is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * Libav is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /* #define DEBUG */
23
24 #include "libavutil/common.h"
25 #include "libavutil/pixdesc.h"
26 #include "libavutil/rational.h"
27 #include "libavutil/audioconvert.h"
28
29 #include "avfilter.h"
30 #include "formats.h"
31 #include "internal.h"
32
33 unsigned avfilter_version(void) {
34     return LIBAVFILTER_VERSION_INT;
35 }
36
37 const char *avfilter_configuration(void)
38 {
39     return LIBAV_CONFIGURATION;
40 }
41
42 const char *avfilter_license(void)
43 {
44 #define LICENSE_PREFIX "libavfilter license: "
45     return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
46 }
47
48 void ff_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
49                    AVFilterPad **pads, AVFilterLink ***links,
50                    AVFilterPad *newpad)
51 {
52     unsigned i;
53
54     idx = FFMIN(idx, *count);
55
56     *pads  = av_realloc(*pads,  sizeof(AVFilterPad)   * (*count + 1));
57     *links = av_realloc(*links, sizeof(AVFilterLink*) * (*count + 1));
58     memmove(*pads +idx+1, *pads +idx, sizeof(AVFilterPad)   * (*count-idx));
59     memmove(*links+idx+1, *links+idx, sizeof(AVFilterLink*) * (*count-idx));
60     memcpy(*pads+idx, newpad, sizeof(AVFilterPad));
61     (*links)[idx] = NULL;
62
63     (*count)++;
64     for (i = idx+1; i < *count; i++)
65         if (*links[i])
66             (*(unsigned *)((uint8_t *) *links[i] + padidx_off))++;
67 }
68
69 int avfilter_link(AVFilterContext *src, unsigned srcpad,
70                   AVFilterContext *dst, unsigned dstpad)
71 {
72     AVFilterLink *link;
73
74     if (src->nb_outputs <= srcpad || dst->nb_inputs <= dstpad ||
75         src->outputs[srcpad]      || dst->inputs[dstpad])
76         return -1;
77
78     if (src->output_pads[srcpad].type != dst->input_pads[dstpad].type) {
79         av_log(src, AV_LOG_ERROR,
80                "Media type mismatch between the '%s' filter output pad %d and the '%s' filter input pad %d\n",
81                src->name, srcpad, dst->name, dstpad);
82         return AVERROR(EINVAL);
83     }
84
85     src->outputs[srcpad] =
86     dst-> inputs[dstpad] = link = av_mallocz(sizeof(AVFilterLink));
87
88     link->src     = src;
89     link->dst     = dst;
90     link->srcpad  = &src->output_pads[srcpad];
91     link->dstpad  = &dst->input_pads[dstpad];
92     link->type    = src->output_pads[srcpad].type;
93     assert(PIX_FMT_NONE == -1 && AV_SAMPLE_FMT_NONE == -1);
94     link->format  = -1;
95
96     return 0;
97 }
98
99 int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
100                            unsigned filt_srcpad_idx, unsigned filt_dstpad_idx)
101 {
102     int ret;
103     unsigned dstpad_idx = link->dstpad - link->dst->input_pads;
104
105     av_log(link->dst, AV_LOG_VERBOSE, "auto-inserting filter '%s' "
106            "between the filter '%s' and the filter '%s'\n",
107            filt->name, link->src->name, link->dst->name);
108
109     link->dst->inputs[dstpad_idx] = NULL;
110     if ((ret = avfilter_link(filt, filt_dstpad_idx, link->dst, dstpad_idx)) < 0) {
111         /* failed to link output filter to new filter */
112         link->dst->inputs[dstpad_idx] = link;
113         return ret;
114     }
115
116     /* re-hookup the link to the new destination filter we inserted */
117     link->dst = filt;
118     link->dstpad = &filt->input_pads[filt_srcpad_idx];
119     filt->inputs[filt_srcpad_idx] = link;
120
121     /* if any information on supported media formats already exists on the
122      * link, we need to preserve that */
123     if (link->out_formats)
124         ff_formats_changeref(&link->out_formats,
125                                    &filt->outputs[filt_dstpad_idx]->out_formats);
126     if (link->out_samplerates)
127         ff_formats_changeref(&link->out_samplerates,
128                                    &filt->outputs[filt_dstpad_idx]->out_samplerates);
129     if (link->out_channel_layouts)
130         ff_channel_layouts_changeref(&link->out_channel_layouts,
131                                      &filt->outputs[filt_dstpad_idx]->out_channel_layouts);
132
133     return 0;
134 }
135
136 int avfilter_config_links(AVFilterContext *filter)
137 {
138     int (*config_link)(AVFilterLink *);
139     unsigned i;
140     int ret;
141
142     for (i = 0; i < filter->nb_inputs; i ++) {
143         AVFilterLink *link = filter->inputs[i];
144
145         if (!link) continue;
146
147         switch (link->init_state) {
148         case AVLINK_INIT:
149             continue;
150         case AVLINK_STARTINIT:
151             av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
152             return 0;
153         case AVLINK_UNINIT:
154             link->init_state = AVLINK_STARTINIT;
155
156             if ((ret = avfilter_config_links(link->src)) < 0)
157                 return ret;
158
159             if (!(config_link = link->srcpad->config_props)) {
160                 if (link->src->nb_inputs != 1) {
161                     av_log(link->src, AV_LOG_ERROR, "Source filters and filters "
162                                                     "with more than one input "
163                                                     "must set config_props() "
164                                                     "callbacks on all outputs\n");
165                     return AVERROR(EINVAL);
166                 }
167             } else if ((ret = config_link(link)) < 0) {
168                 av_log(link->src, AV_LOG_ERROR,
169                        "Failed to configure output pad on %s\n",
170                        link->src->name);
171                 return ret;
172             }
173
174             if (link->time_base.num == 0 && link->time_base.den == 0)
175                 link->time_base = link->src && link->src->nb_inputs ?
176                     link->src->inputs[0]->time_base : AV_TIME_BASE_Q;
177
178             if (link->type == AVMEDIA_TYPE_VIDEO) {
179                 if (!link->sample_aspect_ratio.num && !link->sample_aspect_ratio.den)
180                     link->sample_aspect_ratio = link->src->nb_inputs ?
181                         link->src->inputs[0]->sample_aspect_ratio : (AVRational){1,1};
182
183                 if (link->src->nb_inputs) {
184                     if (!link->w)
185                         link->w = link->src->inputs[0]->w;
186                     if (!link->h)
187                         link->h = link->src->inputs[0]->h;
188                 } else if (!link->w || !link->h) {
189                     av_log(link->src, AV_LOG_ERROR,
190                            "Video source filters must set their output link's "
191                            "width and height\n");
192                     return AVERROR(EINVAL);
193                 }
194             }
195
196             if ((config_link = link->dstpad->config_props))
197                 if ((ret = config_link(link)) < 0) {
198                     av_log(link->src, AV_LOG_ERROR,
199                            "Failed to configure input pad on %s\n",
200                            link->dst->name);
201                     return ret;
202                 }
203
204             link->init_state = AVLINK_INIT;
205         }
206     }
207
208     return 0;
209 }
210
211 void ff_dlog_link(void *ctx, AVFilterLink *link, int end)
212 {
213     if (link->type == AVMEDIA_TYPE_VIDEO) {
214         av_dlog(ctx,
215                 "link[%p s:%dx%d fmt:%-16s %-16s->%-16s]%s",
216                 link, link->w, link->h,
217                 av_pix_fmt_descriptors[link->format].name,
218                 link->src ? link->src->filter->name : "",
219                 link->dst ? link->dst->filter->name : "",
220                 end ? "\n" : "");
221     } else {
222         char buf[128];
223         av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout);
224
225         av_dlog(ctx,
226                 "link[%p r:%d cl:%s fmt:%-16s %-16s->%-16s]%s",
227                 link, link->sample_rate, buf,
228                 av_get_sample_fmt_name(link->format),
229                 link->src ? link->src->filter->name : "",
230                 link->dst ? link->dst->filter->name : "",
231                 end ? "\n" : "");
232     }
233 }
234
235 int ff_request_frame(AVFilterLink *link)
236 {
237     FF_DPRINTF_START(NULL, request_frame); ff_dlog_link(NULL, link, 1);
238
239     if (link->srcpad->request_frame)
240         return link->srcpad->request_frame(link);
241     else if (link->src->inputs[0])
242         return ff_request_frame(link->src->inputs[0]);
243     else return -1;
244 }
245
246 int ff_poll_frame(AVFilterLink *link)
247 {
248     int i, min = INT_MAX;
249
250     if (link->srcpad->poll_frame)
251         return link->srcpad->poll_frame(link);
252
253     for (i = 0; i < link->src->nb_inputs; i++) {
254         int val;
255         if (!link->src->inputs[i])
256             return -1;
257         val = ff_poll_frame(link->src->inputs[i]);
258         min = FFMIN(min, val);
259     }
260
261     return min;
262 }
263
264 #define MAX_REGISTERED_AVFILTERS_NB 64
265
266 static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1];
267
268 static int next_registered_avfilter_idx = 0;
269
270 AVFilter *avfilter_get_by_name(const char *name)
271 {
272     int i;
273
274     for (i = 0; registered_avfilters[i]; i++)
275         if (!strcmp(registered_avfilters[i]->name, name))
276             return registered_avfilters[i];
277
278     return NULL;
279 }
280
281 int avfilter_register(AVFilter *filter)
282 {
283     if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB)
284         return -1;
285
286     registered_avfilters[next_registered_avfilter_idx++] = filter;
287     return 0;
288 }
289
290 AVFilter **av_filter_next(AVFilter **filter)
291 {
292     return filter ? ++filter : &registered_avfilters[0];
293 }
294
295 void avfilter_uninit(void)
296 {
297     memset(registered_avfilters, 0, sizeof(registered_avfilters));
298     next_registered_avfilter_idx = 0;
299 }
300
301 static int pad_count(const AVFilterPad *pads)
302 {
303     int count;
304
305     if (!pads)
306         return 0;
307
308     for(count = 0; pads->name; count ++) pads ++;
309     return count;
310 }
311
312 static const char *filter_name(void *p)
313 {
314     AVFilterContext *filter = p;
315     return filter->filter->name;
316 }
317
318 static const AVClass avfilter_class = {
319     "AVFilter",
320     filter_name,
321     NULL,
322     LIBAVUTIL_VERSION_INT,
323 };
324
325 int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
326 {
327     AVFilterContext *ret;
328     *filter_ctx = NULL;
329
330     if (!filter)
331         return AVERROR(EINVAL);
332
333     ret = av_mallocz(sizeof(AVFilterContext));
334     if (!ret)
335         return AVERROR(ENOMEM);
336
337     ret->av_class = &avfilter_class;
338     ret->filter   = filter;
339     ret->name     = inst_name ? av_strdup(inst_name) : NULL;
340     if (filter->priv_size) {
341         ret->priv     = av_mallocz(filter->priv_size);
342         if (!ret->priv)
343             goto err;
344     }
345
346     ret->nb_inputs = pad_count(filter->inputs);
347     if (ret->nb_inputs ) {
348         ret->input_pads   = av_malloc(sizeof(AVFilterPad) * ret->nb_inputs);
349         if (!ret->input_pads)
350             goto err;
351         memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->nb_inputs);
352         ret->inputs       = av_mallocz(sizeof(AVFilterLink*) * ret->nb_inputs);
353         if (!ret->inputs)
354             goto err;
355     }
356
357     ret->nb_outputs = pad_count(filter->outputs);
358     if (ret->nb_outputs) {
359         ret->output_pads  = av_malloc(sizeof(AVFilterPad) * ret->nb_outputs);
360         if (!ret->output_pads)
361             goto err;
362         memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->nb_outputs);
363         ret->outputs      = av_mallocz(sizeof(AVFilterLink*) * ret->nb_outputs);
364         if (!ret->outputs)
365             goto err;
366     }
367 #if FF_API_FOO_COUNT
368     ret->output_count = ret->nb_outputs;
369     ret->input_count  = ret->nb_inputs;
370 #endif
371
372     *filter_ctx = ret;
373     return 0;
374
375 err:
376     av_freep(&ret->inputs);
377     av_freep(&ret->input_pads);
378     ret->nb_inputs = 0;
379     av_freep(&ret->outputs);
380     av_freep(&ret->output_pads);
381     ret->nb_outputs = 0;
382     av_freep(&ret->priv);
383     av_free(ret);
384     return AVERROR(ENOMEM);
385 }
386
387 void avfilter_free(AVFilterContext *filter)
388 {
389     int i;
390     AVFilterLink *link;
391
392     if (filter->filter->uninit)
393         filter->filter->uninit(filter);
394
395     for (i = 0; i < filter->nb_inputs; i++) {
396         if ((link = filter->inputs[i])) {
397             if (link->src)
398                 link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
399             ff_formats_unref(&link->in_formats);
400             ff_formats_unref(&link->out_formats);
401             ff_formats_unref(&link->in_samplerates);
402             ff_formats_unref(&link->out_samplerates);
403             ff_channel_layouts_unref(&link->in_channel_layouts);
404             ff_channel_layouts_unref(&link->out_channel_layouts);
405         }
406         av_freep(&link);
407     }
408     for (i = 0; i < filter->nb_outputs; i++) {
409         if ((link = filter->outputs[i])) {
410             if (link->dst)
411                 link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
412             ff_formats_unref(&link->in_formats);
413             ff_formats_unref(&link->out_formats);
414             ff_formats_unref(&link->in_samplerates);
415             ff_formats_unref(&link->out_samplerates);
416             ff_channel_layouts_unref(&link->in_channel_layouts);
417             ff_channel_layouts_unref(&link->out_channel_layouts);
418         }
419         av_freep(&link);
420     }
421
422     av_freep(&filter->name);
423     av_freep(&filter->input_pads);
424     av_freep(&filter->output_pads);
425     av_freep(&filter->inputs);
426     av_freep(&filter->outputs);
427     av_freep(&filter->priv);
428     av_free(filter);
429 }
430
431 int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
432 {
433     int ret=0;
434
435     if (filter->filter->init)
436         ret = filter->filter->init(filter, args);
437     return ret;
438 }
439
440 const char *avfilter_pad_get_name(AVFilterPad *pads, int pad_idx)
441 {
442     return pads[pad_idx].name;
443 }
444
445 enum AVMediaType avfilter_pad_get_type(AVFilterPad *pads, int pad_idx)
446 {
447     return pads[pad_idx].type;
448 }