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 ff_formats_changeref(&link->out_formats,
124 &filt->outputs[filt_dstpad_idx]->out_formats);
125 if (link->out_samplerates)
126 ff_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 if (link->src->input_count != 1) {
160 av_log(link->src, AV_LOG_ERROR, "Source filters and filters "
161 "with more than one input "
162 "must set config_props() "
163 "callbacks on all outputs\n");
164 return AVERROR(EINVAL);
166 } else if ((ret = config_link(link)) < 0)
169 if (link->time_base.num == 0 && link->time_base.den == 0)
170 link->time_base = link->src && link->src->input_count ?
171 link->src->inputs[0]->time_base : AV_TIME_BASE_Q;
173 if (link->type == AVMEDIA_TYPE_VIDEO) {
174 if (!link->sample_aspect_ratio.num && !link->sample_aspect_ratio.den)
175 link->sample_aspect_ratio = link->src->input_count ?
176 link->src->inputs[0]->sample_aspect_ratio : (AVRational){1,1};
178 if (link->src->input_count) {
180 link->w = link->src->inputs[0]->w;
182 link->h = link->src->inputs[0]->h;
183 } else if (!link->w || !link->h) {
184 av_log(link->src, AV_LOG_ERROR,
185 "Video source filters must set their output link's "
186 "width and height\n");
187 return AVERROR(EINVAL);
191 if ((config_link = link->dstpad->config_props))
192 if ((ret = config_link(link)) < 0)
195 link->init_state = AVLINK_INIT;
202 void ff_dlog_link(void *ctx, AVFilterLink *link, int end)
204 if (link->type == AVMEDIA_TYPE_VIDEO) {
206 "link[%p s:%dx%d fmt:%-16s %-16s->%-16s]%s",
207 link, link->w, link->h,
208 av_pix_fmt_descriptors[link->format].name,
209 link->src ? link->src->filter->name : "",
210 link->dst ? link->dst->filter->name : "",
214 av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout);
217 "link[%p r:%"PRId64" cl:%s fmt:%-16s %-16s->%-16s]%s",
218 link, link->sample_rate, buf,
219 av_get_sample_fmt_name(link->format),
220 link->src ? link->src->filter->name : "",
221 link->dst ? link->dst->filter->name : "",
226 int avfilter_request_frame(AVFilterLink *link)
228 FF_DPRINTF_START(NULL, request_frame); ff_dlog_link(NULL, link, 1);
230 if (link->srcpad->request_frame)
231 return link->srcpad->request_frame(link);
232 else if (link->src->inputs[0])
233 return avfilter_request_frame(link->src->inputs[0]);
237 int avfilter_poll_frame(AVFilterLink *link)
239 int i, min = INT_MAX;
241 if (link->srcpad->poll_frame)
242 return link->srcpad->poll_frame(link);
244 for (i = 0; i < link->src->input_count; i++) {
246 if (!link->src->inputs[i])
248 val = avfilter_poll_frame(link->src->inputs[i]);
249 min = FFMIN(min, val);
255 #define MAX_REGISTERED_AVFILTERS_NB 64
257 static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1];
259 static int next_registered_avfilter_idx = 0;
261 AVFilter *avfilter_get_by_name(const char *name)
265 for (i = 0; registered_avfilters[i]; i++)
266 if (!strcmp(registered_avfilters[i]->name, name))
267 return registered_avfilters[i];
272 int avfilter_register(AVFilter *filter)
274 if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB)
277 registered_avfilters[next_registered_avfilter_idx++] = filter;
281 AVFilter **av_filter_next(AVFilter **filter)
283 return filter ? ++filter : ®istered_avfilters[0];
286 void avfilter_uninit(void)
288 memset(registered_avfilters, 0, sizeof(registered_avfilters));
289 next_registered_avfilter_idx = 0;
292 static int pad_count(const AVFilterPad *pads)
296 for(count = 0; pads->name; count ++) pads ++;
300 static const char *filter_name(void *p)
302 AVFilterContext *filter = p;
303 return filter->filter->name;
306 static const AVClass avfilter_class = {
310 LIBAVUTIL_VERSION_INT,
313 int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
315 AVFilterContext *ret;
319 return AVERROR(EINVAL);
321 ret = av_mallocz(sizeof(AVFilterContext));
323 return AVERROR(ENOMEM);
325 ret->av_class = &avfilter_class;
326 ret->filter = filter;
327 ret->name = inst_name ? av_strdup(inst_name) : NULL;
328 if (filter->priv_size) {
329 ret->priv = av_mallocz(filter->priv_size);
334 ret->input_count = pad_count(filter->inputs);
335 if (ret->input_count) {
336 ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->input_count);
337 if (!ret->input_pads)
339 memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->input_count);
340 ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
345 ret->output_count = pad_count(filter->outputs);
346 if (ret->output_count) {
347 ret->output_pads = av_malloc(sizeof(AVFilterPad) * ret->output_count);
348 if (!ret->output_pads)
350 memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->output_count);
351 ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
360 av_freep(&ret->inputs);
361 av_freep(&ret->input_pads);
362 ret->input_count = 0;
363 av_freep(&ret->outputs);
364 av_freep(&ret->output_pads);
365 ret->output_count = 0;
366 av_freep(&ret->priv);
368 return AVERROR(ENOMEM);
371 void avfilter_free(AVFilterContext *filter)
376 if (filter->filter->uninit)
377 filter->filter->uninit(filter);
379 for (i = 0; i < filter->input_count; i++) {
380 if ((link = filter->inputs[i])) {
382 link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
383 ff_formats_unref(&link->in_formats);
384 ff_formats_unref(&link->out_formats);
385 ff_formats_unref(&link->in_samplerates);
386 ff_formats_unref(&link->out_samplerates);
387 ff_channel_layouts_unref(&link->in_channel_layouts);
388 ff_channel_layouts_unref(&link->out_channel_layouts);
392 for (i = 0; i < filter->output_count; i++) {
393 if ((link = filter->outputs[i])) {
395 link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
396 ff_formats_unref(&link->in_formats);
397 ff_formats_unref(&link->out_formats);
398 ff_formats_unref(&link->in_samplerates);
399 ff_formats_unref(&link->out_samplerates);
400 ff_channel_layouts_unref(&link->in_channel_layouts);
401 ff_channel_layouts_unref(&link->out_channel_layouts);
406 av_freep(&filter->name);
407 av_freep(&filter->input_pads);
408 av_freep(&filter->output_pads);
409 av_freep(&filter->inputs);
410 av_freep(&filter->outputs);
411 av_freep(&filter->priv);
415 int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
419 if (filter->filter->init)
420 ret = filter->filter->init(filter, args, opaque);
424 #if FF_API_DEFAULT_CONFIG_OUTPUT_LINK
425 int avfilter_default_config_output_link(AVFilterLink *link)