]> git.sesse.net Git - ffmpeg/blob - libavfilter/avfilter.c
lavfi: move buffer management function to a separate file.
[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/pixdesc.h"
25 #include "libavutil/rational.h"
26 #include "libavutil/audioconvert.h"
27
28 #include "avfilter.h"
29 #include "formats.h"
30 #include "internal.h"
31
32 unsigned avfilter_version(void) {
33     return LIBAVFILTER_VERSION_INT;
34 }
35
36 const char *avfilter_configuration(void)
37 {
38     return LIBAV_CONFIGURATION;
39 }
40
41 const char *avfilter_license(void)
42 {
43 #define LICENSE_PREFIX "libavfilter license: "
44     return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
45 }
46
47 void avfilter_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
48                          AVFilterPad **pads, AVFilterLink ***links,
49                          AVFilterPad *newpad)
50 {
51     unsigned i;
52
53     idx = FFMIN(idx, *count);
54
55     *pads  = av_realloc(*pads,  sizeof(AVFilterPad)   * (*count + 1));
56     *links = av_realloc(*links, sizeof(AVFilterLink*) * (*count + 1));
57     memmove(*pads +idx+1, *pads +idx, sizeof(AVFilterPad)   * (*count-idx));
58     memmove(*links+idx+1, *links+idx, sizeof(AVFilterLink*) * (*count-idx));
59     memcpy(*pads+idx, newpad, sizeof(AVFilterPad));
60     (*links)[idx] = NULL;
61
62     (*count)++;
63     for (i = idx+1; i < *count; i++)
64         if (*links[i])
65             (*(unsigned *)((uint8_t *) *links[i] + padidx_off))++;
66 }
67
68 int avfilter_link(AVFilterContext *src, unsigned srcpad,
69                   AVFilterContext *dst, unsigned dstpad)
70 {
71     AVFilterLink *link;
72
73     if (src->output_count <= srcpad || dst->input_count <= dstpad ||
74         src->outputs[srcpad]        || dst->inputs[dstpad])
75         return -1;
76
77     if (src->output_pads[srcpad].type != dst->input_pads[dstpad].type) {
78         av_log(src, AV_LOG_ERROR,
79                "Media type mismatch between the '%s' filter output pad %d and the '%s' filter input pad %d\n",
80                src->name, srcpad, dst->name, dstpad);
81         return AVERROR(EINVAL);
82     }
83
84     src->outputs[srcpad] =
85     dst-> inputs[dstpad] = link = av_mallocz(sizeof(AVFilterLink));
86
87     link->src     = src;
88     link->dst     = dst;
89     link->srcpad  = &src->output_pads[srcpad];
90     link->dstpad  = &dst->input_pads[dstpad];
91     link->type    = src->output_pads[srcpad].type;
92     assert(PIX_FMT_NONE == -1 && AV_SAMPLE_FMT_NONE == -1);
93     link->format  = -1;
94
95     return 0;
96 }
97
98 int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
99                            unsigned filt_srcpad_idx, unsigned filt_dstpad_idx)
100 {
101     int ret;
102     unsigned dstpad_idx = link->dstpad - link->dst->input_pads;
103
104     av_log(link->dst, AV_LOG_INFO, "auto-inserting filter '%s' "
105            "between the filter '%s' and the filter '%s'\n",
106            filt->name, link->src->name, link->dst->name);
107
108     link->dst->inputs[dstpad_idx] = NULL;
109     if ((ret = avfilter_link(filt, filt_dstpad_idx, link->dst, dstpad_idx)) < 0) {
110         /* failed to link output filter to new filter */
111         link->dst->inputs[dstpad_idx] = link;
112         return ret;
113     }
114
115     /* re-hookup the link to the new destination filter we inserted */
116     link->dst = filt;
117     link->dstpad = &filt->input_pads[filt_srcpad_idx];
118     filt->inputs[filt_srcpad_idx] = link;
119
120     /* if any information on supported media formats already exists on the
121      * link, we need to preserve that */
122     if (link->out_formats)
123         avfilter_formats_changeref(&link->out_formats,
124                                    &filt->outputs[filt_dstpad_idx]->out_formats);
125     if (link->out_samplerates)
126         avfilter_formats_changeref(&link->out_samplerates,
127                                    &filt->outputs[filt_dstpad_idx]->out_samplerates);
128     if (link->out_channel_layouts)
129         ff_channel_layouts_changeref(&link->out_channel_layouts,
130                                      &filt->outputs[filt_dstpad_idx]->out_channel_layouts);
131
132     return 0;
133 }
134
135 int avfilter_config_links(AVFilterContext *filter)
136 {
137     int (*config_link)(AVFilterLink *);
138     unsigned i;
139     int ret;
140
141     for (i = 0; i < filter->input_count; i ++) {
142         AVFilterLink *link = filter->inputs[i];
143
144         if (!link) continue;
145
146         switch (link->init_state) {
147         case AVLINK_INIT:
148             continue;
149         case AVLINK_STARTINIT:
150             av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
151             return 0;
152         case AVLINK_UNINIT:
153             link->init_state = AVLINK_STARTINIT;
154
155             if ((ret = avfilter_config_links(link->src)) < 0)
156                 return ret;
157
158             if (!(config_link = link->srcpad->config_props))
159                 config_link  = avfilter_default_config_output_link;
160             if ((ret = config_link(link)) < 0)
161                 return ret;
162
163             if (link->time_base.num == 0 && link->time_base.den == 0)
164                 link->time_base = link->src && link->src->input_count ?
165                     link->src->inputs[0]->time_base : AV_TIME_BASE_Q;
166
167             if (link->sample_aspect_ratio.num == 0 && link->sample_aspect_ratio.den == 0)
168                 link->sample_aspect_ratio = link->src->input_count ?
169                     link->src->inputs[0]->sample_aspect_ratio : (AVRational){1,1};
170
171             if ((config_link = link->dstpad->config_props))
172                 if ((ret = config_link(link)) < 0)
173                     return ret;
174
175             link->init_state = AVLINK_INIT;
176         }
177     }
178
179     return 0;
180 }
181
182 #ifdef DEBUG
183 static char *ff_get_ref_perms_string(char *buf, size_t buf_size, int perms)
184 {
185     snprintf(buf, buf_size, "%s%s%s%s%s%s",
186              perms & AV_PERM_READ      ? "r" : "",
187              perms & AV_PERM_WRITE     ? "w" : "",
188              perms & AV_PERM_PRESERVE  ? "p" : "",
189              perms & AV_PERM_REUSE     ? "u" : "",
190              perms & AV_PERM_REUSE2    ? "U" : "",
191              perms & AV_PERM_NEG_LINESIZES ? "n" : "");
192     return buf;
193 }
194 #endif
195
196 void ff_dlog_link(void *ctx, AVFilterLink *link, int end)
197 {
198     if (link->type == AVMEDIA_TYPE_VIDEO) {
199         av_dlog(ctx,
200                 "link[%p s:%dx%d fmt:%-16s %-16s->%-16s]%s",
201                 link, link->w, link->h,
202                 av_pix_fmt_descriptors[link->format].name,
203                 link->src ? link->src->filter->name : "",
204                 link->dst ? link->dst->filter->name : "",
205                 end ? "\n" : "");
206     } else {
207         char buf[128];
208         av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout);
209
210         av_dlog(ctx,
211                 "link[%p r:%"PRId64" cl:%s fmt:%-16s %-16s->%-16s]%s",
212                 link, link->sample_rate, buf,
213                 av_get_sample_fmt_name(link->format),
214                 link->src ? link->src->filter->name : "",
215                 link->dst ? link->dst->filter->name : "",
216                 end ? "\n" : "");
217     }
218 }
219
220 int avfilter_request_frame(AVFilterLink *link)
221 {
222     FF_DPRINTF_START(NULL, request_frame); ff_dlog_link(NULL, link, 1);
223
224     if (link->srcpad->request_frame)
225         return link->srcpad->request_frame(link);
226     else if (link->src->inputs[0])
227         return avfilter_request_frame(link->src->inputs[0]);
228     else return -1;
229 }
230
231 int avfilter_poll_frame(AVFilterLink *link)
232 {
233     int i, min = INT_MAX;
234
235     if (link->srcpad->poll_frame)
236         return link->srcpad->poll_frame(link);
237
238     for (i = 0; i < link->src->input_count; i++) {
239         int val;
240         if (!link->src->inputs[i])
241             return -1;
242         val = avfilter_poll_frame(link->src->inputs[i]);
243         min = FFMIN(min, val);
244     }
245
246     return min;
247 }
248
249 #define MAX_REGISTERED_AVFILTERS_NB 64
250
251 static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1];
252
253 static int next_registered_avfilter_idx = 0;
254
255 AVFilter *avfilter_get_by_name(const char *name)
256 {
257     int i;
258
259     for (i = 0; registered_avfilters[i]; i++)
260         if (!strcmp(registered_avfilters[i]->name, name))
261             return registered_avfilters[i];
262
263     return NULL;
264 }
265
266 int avfilter_register(AVFilter *filter)
267 {
268     if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB)
269         return -1;
270
271     registered_avfilters[next_registered_avfilter_idx++] = filter;
272     return 0;
273 }
274
275 AVFilter **av_filter_next(AVFilter **filter)
276 {
277     return filter ? ++filter : &registered_avfilters[0];
278 }
279
280 void avfilter_uninit(void)
281 {
282     memset(registered_avfilters, 0, sizeof(registered_avfilters));
283     next_registered_avfilter_idx = 0;
284 }
285
286 static int pad_count(const AVFilterPad *pads)
287 {
288     int count;
289
290     for(count = 0; pads->name; count ++) pads ++;
291     return count;
292 }
293
294 static const char *filter_name(void *p)
295 {
296     AVFilterContext *filter = p;
297     return filter->filter->name;
298 }
299
300 static const AVClass avfilter_class = {
301     "AVFilter",
302     filter_name,
303     NULL,
304     LIBAVUTIL_VERSION_INT,
305 };
306
307 int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
308 {
309     AVFilterContext *ret;
310     *filter_ctx = NULL;
311
312     if (!filter)
313         return AVERROR(EINVAL);
314
315     ret = av_mallocz(sizeof(AVFilterContext));
316     if (!ret)
317         return AVERROR(ENOMEM);
318
319     ret->av_class = &avfilter_class;
320     ret->filter   = filter;
321     ret->name     = inst_name ? av_strdup(inst_name) : NULL;
322     if (filter->priv_size) {
323         ret->priv     = av_mallocz(filter->priv_size);
324         if (!ret->priv)
325             goto err;
326     }
327
328     ret->input_count  = pad_count(filter->inputs);
329     if (ret->input_count) {
330         ret->input_pads   = av_malloc(sizeof(AVFilterPad) * ret->input_count);
331         if (!ret->input_pads)
332             goto err;
333         memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->input_count);
334         ret->inputs       = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
335         if (!ret->inputs)
336             goto err;
337     }
338
339     ret->output_count = pad_count(filter->outputs);
340     if (ret->output_count) {
341         ret->output_pads  = av_malloc(sizeof(AVFilterPad) * ret->output_count);
342         if (!ret->output_pads)
343             goto err;
344         memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->output_count);
345         ret->outputs      = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
346         if (!ret->outputs)
347             goto err;
348     }
349
350     *filter_ctx = ret;
351     return 0;
352
353 err:
354     av_freep(&ret->inputs);
355     av_freep(&ret->input_pads);
356     ret->input_count = 0;
357     av_freep(&ret->outputs);
358     av_freep(&ret->output_pads);
359     ret->output_count = 0;
360     av_freep(&ret->priv);
361     av_free(ret);
362     return AVERROR(ENOMEM);
363 }
364
365 void avfilter_free(AVFilterContext *filter)
366 {
367     int i;
368     AVFilterLink *link;
369
370     if (filter->filter->uninit)
371         filter->filter->uninit(filter);
372
373     for (i = 0; i < filter->input_count; i++) {
374         if ((link = filter->inputs[i])) {
375             if (link->src)
376                 link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
377             avfilter_formats_unref(&link->in_formats);
378             avfilter_formats_unref(&link->out_formats);
379             avfilter_formats_unref(&link->in_samplerates);
380             avfilter_formats_unref(&link->out_samplerates);
381             ff_channel_layouts_unref(&link->in_channel_layouts);
382             ff_channel_layouts_unref(&link->out_channel_layouts);
383         }
384         av_freep(&link);
385     }
386     for (i = 0; i < filter->output_count; i++) {
387         if ((link = filter->outputs[i])) {
388             if (link->dst)
389                 link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
390             avfilter_formats_unref(&link->in_formats);
391             avfilter_formats_unref(&link->out_formats);
392             avfilter_formats_unref(&link->in_samplerates);
393             avfilter_formats_unref(&link->out_samplerates);
394             ff_channel_layouts_unref(&link->in_channel_layouts);
395             ff_channel_layouts_unref(&link->out_channel_layouts);
396         }
397         av_freep(&link);
398     }
399
400     av_freep(&filter->name);
401     av_freep(&filter->input_pads);
402     av_freep(&filter->output_pads);
403     av_freep(&filter->inputs);
404     av_freep(&filter->outputs);
405     av_freep(&filter->priv);
406     av_free(filter);
407 }
408
409 int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
410 {
411     int ret=0;
412
413     if (filter->filter->init)
414         ret = filter->filter->init(filter, args, opaque);
415     return ret;
416 }