3 * copyright (c) 2007 Bobby Bingham
5 * This file is part of FFmpeg.
7 * FFmpeg 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 * FFmpeg 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 FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 #include "allfilters.h"
30 /** list of registered filters, sorted by name */
31 static int filter_count
= 0;
32 static AVFilter
**filters
= NULL
;
34 /* TODO: buffer pool. see comment for avfilter_default_get_video_buffer() */
35 void avfilter_default_free_video_buffer(AVFilterPic
*pic
)
37 avpicture_free((AVPicture
*) pic
);
41 /* TODO: set the buffer's priv member to a context structure for the whole
42 * filter chain. This will allow for a buffer pool instead of the constant
43 * alloc & free cycle currently implemented. */
44 AVFilterPicRef
*avfilter_default_get_video_buffer(AVFilterLink
*link
, int perms
)
46 AVFilterPic
*pic
= av_mallocz(sizeof(AVFilterPic
));
47 AVFilterPicRef
*ref
= av_mallocz(sizeof(AVFilterPicRef
));
55 pic
->format
= link
->format
;
56 pic
->free
= avfilter_default_free_video_buffer
;
57 avpicture_alloc((AVPicture
*)pic
, pic
->format
, ref
->w
, ref
->h
);
59 memcpy(ref
->data
, pic
->data
, sizeof(pic
->data
));
60 memcpy(ref
->linesize
, pic
->linesize
, sizeof(pic
->linesize
));
65 void avfilter_default_start_frame(AVFilterLink
*link
, AVFilterPicRef
*picref
)
67 link
->cur_pic
= picref
;
70 void avfilter_default_end_frame(AVFilterLink
*link
)
72 avfilter_unref_pic(link
->cur_pic
);
76 AVFilterPicRef
*avfilter_ref_pic(AVFilterPicRef
*ref
)
78 AVFilterPicRef
*ret
= av_malloc(sizeof(AVFilterPicRef
));
79 memcpy(ret
, ref
, sizeof(AVFilterPicRef
));
80 ret
->pic
->refcount
++;
84 void avfilter_unref_pic(AVFilterPicRef
*ref
)
86 if(-- ref
->pic
->refcount
== 0)
87 ref
->pic
->free(ref
->pic
);
91 int avfilter_link(AVFilterContext
*src
, unsigned srcpad
,
92 AVFilterContext
*dst
, unsigned dstpad
)
97 if(src
->outputs
[srcpad
] || dst
->inputs
[dstpad
])
100 src
->outputs
[srcpad
] =
101 dst
->inputs
[dstpad
] = link
= av_malloc(sizeof(AVFilterLink
));
105 link
->srcpad
= srcpad
;
106 link
->dstpad
= dstpad
;
107 link
->cur_pic
= NULL
;
109 /* find a format both filters support - TODO: auto-insert conversion filter */
110 fmts
[0] = src
->filter
->outputs
[srcpad
].query_formats(link
);
111 fmts
[1] = dst
->filter
->inputs
[dstpad
].query_formats(link
);
112 for(i
= 0; fmts
[0][i
] != -1; i
++)
113 for(j
= 0; fmts
[1][j
] != -1; j
++)
114 if(fmts
[0][i
] == fmts
[1][j
]) {
115 link
->format
= fmts
[0][i
];
122 if(link
->format
== -1) {
123 /* failed to find a format. fail at creating the link */
125 src
->outputs
[srcpad
] = NULL
;
126 dst
->inputs
[dstpad
] = NULL
;
130 src
->filter
->outputs
[srcpad
].config_props(link
);
131 dst
->filter
->inputs
[dstpad
].config_props(link
);
135 AVFilterPicRef
*avfilter_get_video_buffer(AVFilterLink
*link
, int perms
)
137 AVFilterPicRef
*ret
= NULL
;
139 if(link
->dst
->filter
->inputs
[link
->dstpad
].get_video_buffer
)
140 ret
= link
->dst
->filter
->inputs
[link
->dstpad
].get_video_buffer(link
, perms
);
143 ret
= avfilter_default_get_video_buffer(link
, perms
);
148 void avfilter_request_frame(AVFilterLink
*link
)
150 link
->src
->filter
->outputs
[link
->srcpad
].request_frame(link
);
153 /* XXX: should we do the duplicating of the picture ref here, instead of
154 * forcing the source filter to do it? */
155 void avfilter_start_frame(AVFilterLink
*link
, AVFilterPicRef
*picref
)
157 void (*start_frame
)(AVFilterLink
*, AVFilterPicRef
*);
159 start_frame
= link
->dst
->filter
->inputs
[link
->dstpad
].start_frame
;
161 start_frame
= avfilter_default_start_frame
;
163 start_frame(link
, picref
);
166 void avfilter_end_frame(AVFilterLink
*link
)
168 void (*end_frame
)(AVFilterLink
*);
170 end_frame
= link
->dst
->filter
->inputs
[link
->dstpad
].end_frame
;
172 end_frame
= avfilter_default_end_frame
;
177 void avfilter_draw_slice(AVFilterLink
*link
, uint8_t *data
[4], int y
, int h
)
179 link
->dst
->filter
->inputs
[link
->dstpad
].draw_slice(link
, data
, y
, h
);
182 static int filter_cmp(const void *aa
, const void *bb
)
184 const AVFilter
*a
= *(const AVFilter
**)aa
, *b
= *(const AVFilter
**)bb
;
185 return strcmp(a
->name
, b
->name
);
188 AVFilter
*avfilter_get_by_name(char *name
)
190 AVFilter key
= { .name
= name
, };
191 AVFilter
*key2
= &key
;
194 ret
= bsearch(&key2
, filters
, filter_count
, sizeof(AVFilter
**), filter_cmp
);
200 /* FIXME: insert in order, rather than insert at end + resort */
201 void avfilter_register(AVFilter
*filter
)
203 filters
= av_realloc(filters
, sizeof(AVFilter
*) * (filter_count
+1));
204 filters
[filter_count
] = filter
;
205 qsort(filters
, ++filter_count
, sizeof(AVFilter
**), filter_cmp
);
208 void avfilter_init(void)
210 avfilter_register(&vsrc_dummy
);
211 avfilter_register(&vf_crop
);
212 avfilter_register(&vf_passthrough
);
213 avfilter_register(&vo_sdl
);
216 void avfilter_uninit(void)
222 static int pad_count(const AVFilterPad
*pads
)
224 AVFilterPad
*p
= (AVFilterPad
*) pads
;
227 for(count
= 0; p
->name
; count
++) p
++;
231 static const char *filter_name(void *p
)
233 AVFilterContext
*filter
= p
;
234 return filter
->filter
->name
;
237 AVFilterContext
*avfilter_create(AVFilter
*filter
)
239 AVFilterContext
*ret
= av_malloc(sizeof(AVFilterContext
));
241 ret
->av_class
= av_mallocz(sizeof(AVClass
));
242 ret
->av_class
->item_name
= filter_name
;
243 ret
->filter
= filter
;
244 ret
->inputs
= av_mallocz(sizeof(AVFilterLink
*) * pad_count(filter
->inputs
));
245 ret
->outputs
= av_mallocz(sizeof(AVFilterLink
*) * pad_count(filter
->outputs
));
246 ret
->priv
= av_mallocz(filter
->priv_size
);
251 void avfilter_destroy(AVFilterContext
*filter
)
255 if(filter
->filter
->uninit
)
256 filter
->filter
->uninit(filter
);
258 for(i
= 0; i
< pad_count(filter
->filter
->inputs
); i
++) {
259 if(filter
->inputs
[i
])
260 filter
->inputs
[i
]->src
->outputs
[filter
->inputs
[i
]->srcpad
] = NULL
;
261 av_free(filter
->inputs
[i
]);
263 for(i
= 0; i
< pad_count(filter
->filter
->outputs
); i
++) {
264 if(filter
->outputs
[i
])
265 filter
->outputs
[i
]->dst
->inputs
[filter
->outputs
[i
]->dstpad
] = NULL
;
266 av_free(filter
->outputs
[i
]);
269 av_free(filter
->inputs
);
270 av_free(filter
->outputs
);
271 av_free(filter
->priv
);
272 av_free(filter
->av_class
);
276 AVFilterContext
*avfilter_create_by_name(char *name
)
280 if(!(filt
= avfilter_get_by_name(name
))) return NULL
;
281 return avfilter_create(filt
);
284 int avfilter_init_filter(AVFilterContext
*filter
, const char *args
)
288 if(filter
->filter
->init
)
289 if((ret
= filter
->filter
->init(filter
, args
))) return ret
;
293 int *avfilter_make_format_list(int len
, ...)
298 ret
= av_malloc(sizeof(int) * (len
+ 1));
300 for(i
= 0; i
< len
; i
++)
301 ret
[i
] = va_arg(vl
, int);