3 * Copyright (c) 2007 Bobby Bingham
5 * This file is part of Libav.
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.
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.
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
24 #include "libavutil/pixdesc.h"
25 #include "libavutil/rational.h"
26 #include "libavutil/audioconvert.h"
32 unsigned avfilter_version(void) {
33 return LIBAVFILTER_VERSION_INT;
36 const char *avfilter_configuration(void)
38 return LIBAV_CONFIGURATION;
41 const char *avfilter_license(void)
43 #define LICENSE_PREFIX "libavfilter license: "
44 return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
47 void avfilter_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
48 AVFilterPad **pads, AVFilterLink ***links,
53 idx = FFMIN(idx, *count);
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));
63 for (i = idx+1; i < *count; i++)
65 (*(unsigned *)((uint8_t *) *links[i] + padidx_off))++;
68 int avfilter_link(AVFilterContext *src, unsigned srcpad,
69 AVFilterContext *dst, unsigned dstpad)
73 if (src->output_count <= srcpad || dst->input_count <= dstpad ||
74 src->outputs[srcpad] || dst->inputs[dstpad])
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);
84 src->outputs[srcpad] =
85 dst-> inputs[dstpad] = link = av_mallocz(sizeof(AVFilterLink));
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);
98 int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
99 unsigned filt_srcpad_idx, unsigned filt_dstpad_idx)
102 unsigned dstpad_idx = link->dstpad - link->dst->input_pads;
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);
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;
115 /* re-hookup the link to the new destination filter we inserted */
117 link->dstpad = &filt->input_pads[filt_srcpad_idx];
118 filt->inputs[filt_srcpad_idx] = link;
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);
135 int avfilter_config_links(AVFilterContext *filter)
137 int (*config_link)(AVFilterLink *);
141 for (i = 0; i < filter->input_count; i ++) {
142 AVFilterLink *link = filter->inputs[i];
146 switch (link->init_state) {
149 case AVLINK_STARTINIT:
150 av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
153 link->init_state = AVLINK_STARTINIT;
155 if ((ret = avfilter_config_links(link->src)) < 0)
158 if (!(config_link = link->srcpad->config_props))
159 config_link = avfilter_default_config_output_link;
160 if ((ret = config_link(link)) < 0)
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;
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};
171 if ((config_link = link->dstpad->config_props))
172 if ((ret = config_link(link)) < 0)
175 link->init_state = AVLINK_INIT;
183 static char *ff_get_ref_perms_string(char *buf, size_t buf_size, int perms)
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" : "");
196 void ff_dlog_link(void *ctx, AVFilterLink *link, int end)
198 if (link->type == AVMEDIA_TYPE_VIDEO) {
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 : "",
208 av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout);
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 : "",
220 int avfilter_request_frame(AVFilterLink *link)
222 FF_DPRINTF_START(NULL, request_frame); ff_dlog_link(NULL, link, 1);
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]);
231 int avfilter_poll_frame(AVFilterLink *link)
233 int i, min = INT_MAX;
235 if (link->srcpad->poll_frame)
236 return link->srcpad->poll_frame(link);
238 for (i = 0; i < link->src->input_count; i++) {
240 if (!link->src->inputs[i])
242 val = avfilter_poll_frame(link->src->inputs[i]);
243 min = FFMIN(min, val);
249 #define MAX_REGISTERED_AVFILTERS_NB 64
251 static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1];
253 static int next_registered_avfilter_idx = 0;
255 AVFilter *avfilter_get_by_name(const char *name)
259 for (i = 0; registered_avfilters[i]; i++)
260 if (!strcmp(registered_avfilters[i]->name, name))
261 return registered_avfilters[i];
266 int avfilter_register(AVFilter *filter)
268 if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB)
271 registered_avfilters[next_registered_avfilter_idx++] = filter;
275 AVFilter **av_filter_next(AVFilter **filter)
277 return filter ? ++filter : ®istered_avfilters[0];
280 void avfilter_uninit(void)
282 memset(registered_avfilters, 0, sizeof(registered_avfilters));
283 next_registered_avfilter_idx = 0;
286 static int pad_count(const AVFilterPad *pads)
290 for(count = 0; pads->name; count ++) pads ++;
294 static const char *filter_name(void *p)
296 AVFilterContext *filter = p;
297 return filter->filter->name;
300 static const AVClass avfilter_class = {
304 LIBAVUTIL_VERSION_INT,
307 int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
309 AVFilterContext *ret;
313 return AVERROR(EINVAL);
315 ret = av_mallocz(sizeof(AVFilterContext));
317 return AVERROR(ENOMEM);
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);
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)
333 memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->input_count);
334 ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
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)
344 memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->output_count);
345 ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
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);
362 return AVERROR(ENOMEM);
365 void avfilter_free(AVFilterContext *filter)
370 if (filter->filter->uninit)
371 filter->filter->uninit(filter);
373 for (i = 0; i < filter->input_count; i++) {
374 if ((link = filter->inputs[i])) {
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);
386 for (i = 0; i < filter->output_count; i++) {
387 if ((link = filter->outputs[i])) {
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);
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);
409 int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
413 if (filter->filter->init)
414 ret = filter->filter->init(filter, args, opaque);