Commit | Line | Data |
---|---|---|
4dbbcdee VS |
1 | /* |
2 | * Filter layer | |
3 | * copyright (c) 2007 Bobby Bingham | |
4 | * | |
5 | * This file is part of FFmpeg. | |
6 | * | |
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. | |
11 | * | |
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. | |
16 | * | |
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 | |
20 | */ | |
21 | ||
e0752603 | 22 | #include <stdarg.h> |
4dbbcdee VS |
23 | #include <stdlib.h> |
24 | #include <string.h> | |
25 | #include <stdio.h> | |
26 | ||
27 | #include "avfilter.h" | |
d72a138e | 28 | #include "allfilters.h" |
4dbbcdee VS |
29 | |
30 | /** list of registered filters, sorted by name */ | |
31 | static int filter_count = 0; | |
32 | static AVFilter **filters = NULL; | |
33 | ||
34 | /* TODO: buffer pool. see comment for avfilter_default_get_video_buffer() */ | |
35 | void avfilter_default_free_video_buffer(AVFilterPic *pic) | |
36 | { | |
37 | avpicture_free((AVPicture *) pic); | |
38 | av_free(pic); | |
39 | } | |
40 | ||
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) | |
45 | { | |
46 | AVFilterPic *pic = av_mallocz(sizeof(AVFilterPic)); | |
47 | AVFilterPicRef *ref = av_mallocz(sizeof(AVFilterPicRef)); | |
48 | ||
49 | ref->pic = pic; | |
50 | ref->w = link->w; | |
51 | ref->h = link->h; | |
52 | ref->perms = perms; | |
53 | ||
54 | pic->refcount = 1; | |
55 | pic->format = link->format; | |
56 | pic->free = avfilter_default_free_video_buffer; | |
57 | avpicture_alloc((AVPicture *)pic, pic->format, ref->w, ref->h); | |
58 | ||
92beffdc | 59 | memcpy(ref->data, pic->data, sizeof(pic->data)); |
a4ca7389 | 60 | memcpy(ref->linesize, pic->linesize, sizeof(pic->linesize)); |
4dbbcdee VS |
61 | |
62 | return ref; | |
63 | } | |
64 | ||
65 | void avfilter_default_start_frame(AVFilterLink *link, AVFilterPicRef *picref) | |
66 | { | |
3628fbe0 VS |
67 | AVFilterLink *out = link->dst->outputs[0]; |
68 | ||
4dbbcdee | 69 | link->cur_pic = picref; |
3628fbe0 VS |
70 | |
71 | if(out) { | |
72 | out->outpic = avfilter_get_video_buffer(out, AV_PERM_WRITE); | |
73 | avfilter_start_frame(out, avfilter_ref_pic(out->outpic, ~0)); | |
74 | } | |
4dbbcdee VS |
75 | } |
76 | ||
77 | void avfilter_default_end_frame(AVFilterLink *link) | |
78 | { | |
3628fbe0 VS |
79 | AVFilterLink *out = link->dst->outputs[0]; |
80 | ||
4dbbcdee VS |
81 | avfilter_unref_pic(link->cur_pic); |
82 | link->cur_pic = NULL; | |
3628fbe0 VS |
83 | |
84 | if(out) { | |
85 | avfilter_unref_pic(out->outpic); | |
86 | out->outpic = NULL; | |
87 | avfilter_end_frame(out); | |
88 | } | |
4dbbcdee VS |
89 | } |
90 | ||
efb36bfc | 91 | AVFilterPicRef *avfilter_ref_pic(AVFilterPicRef *ref, int pmask) |
4dbbcdee VS |
92 | { |
93 | AVFilterPicRef *ret = av_malloc(sizeof(AVFilterPicRef)); | |
94 | memcpy(ret, ref, sizeof(AVFilterPicRef)); | |
efb36bfc | 95 | ret->perms &= pmask; |
4dbbcdee VS |
96 | ret->pic->refcount ++; |
97 | return ret; | |
98 | } | |
99 | ||
100 | void avfilter_unref_pic(AVFilterPicRef *ref) | |
101 | { | |
102 | if(-- ref->pic->refcount == 0) | |
103 | ref->pic->free(ref->pic); | |
104 | av_free(ref); | |
105 | } | |
106 | ||
102fb0e3 VS |
107 | /** |
108 | * default config_link() implementation for output video links to simplify | |
109 | * the implementation of one input one output video filters */ | |
110 | static int default_config_output_link(AVFilterLink *link) | |
111 | { | |
112 | link->w = link->src->inputs[0]->w; | |
113 | link->h = link->src->inputs[0]->h; | |
114 | ||
115 | return 0; | |
116 | } | |
117 | ||
118 | /** | |
119 | * default query_formats() implementation for output video links to simplify | |
120 | * the implementation of one input one output video filters */ | |
121 | static int *default_query_output_formats(AVFilterLink *link) | |
122 | { | |
123 | return avfilter_make_format_list(1, link->src->inputs[0]->format); | |
124 | } | |
125 | ||
4dbbcdee VS |
126 | int avfilter_link(AVFilterContext *src, unsigned srcpad, |
127 | AVFilterContext *dst, unsigned dstpad) | |
128 | { | |
129 | AVFilterLink *link; | |
e0752603 | 130 | int *fmts[2], i, j; |
4dbbcdee VS |
131 | |
132 | if(src->outputs[srcpad] || dst->inputs[dstpad]) | |
133 | return -1; | |
134 | ||
135 | src->outputs[srcpad] = | |
136 | dst->inputs[dstpad] = link = av_malloc(sizeof(AVFilterLink)); | |
137 | ||
138 | link->src = src; | |
139 | link->dst = dst; | |
140 | link->srcpad = srcpad; | |
141 | link->dstpad = dstpad; | |
142 | link->cur_pic = NULL; | |
143 | ||
e0752603 | 144 | /* find a format both filters support - TODO: auto-insert conversion filter */ |
102fb0e3 VS |
145 | if(src->filter->outputs[srcpad].query_formats) |
146 | fmts[0] = src->filter->outputs[srcpad].query_formats(link); | |
147 | else | |
148 | fmts[0] = default_query_output_formats(link); | |
ba6b9035 | 149 | fmts[1] = dst->filter-> inputs[dstpad].query_formats(link); |
e0752603 VS |
150 | for(i = 0; fmts[0][i] != -1; i ++) |
151 | for(j = 0; fmts[1][j] != -1; j ++) | |
152 | if(fmts[0][i] == fmts[1][j]) { | |
153 | link->format = fmts[0][i]; | |
154 | goto format_done; | |
155 | } | |
156 | ||
157 | format_done: | |
158 | av_free(fmts[0]); | |
159 | av_free(fmts[1]); | |
160 | if(link->format == -1) { | |
161 | /* failed to find a format. fail at creating the link */ | |
162 | av_free(link); | |
163 | src->outputs[srcpad] = NULL; | |
164 | dst->inputs[dstpad] = NULL; | |
165 | return -1; | |
166 | } | |
167 | ||
102fb0e3 VS |
168 | if (src->filter->outputs[srcpad].config_props) |
169 | src->filter->outputs[srcpad].config_props(link); | |
170 | else | |
171 | default_config_output_link(link); | |
172 | ||
173 | if (dst->filter->inputs[dstpad].config_props) | |
174 | dst->filter->inputs[dstpad].config_props(link); | |
175 | ||
4dbbcdee VS |
176 | return 0; |
177 | } | |
178 | ||
179 | AVFilterPicRef *avfilter_get_video_buffer(AVFilterLink *link, int perms) | |
180 | { | |
181 | AVFilterPicRef *ret = NULL; | |
182 | ||
183 | if(link->dst->filter->inputs[link->dstpad].get_video_buffer) | |
184 | ret = link->dst->filter->inputs[link->dstpad].get_video_buffer(link, perms); | |
185 | ||
186 | if(!ret) | |
187 | ret = avfilter_default_get_video_buffer(link, perms); | |
188 | ||
189 | return ret; | |
190 | } | |
191 | ||
192 | void avfilter_request_frame(AVFilterLink *link) | |
193 | { | |
194 | link->src->filter->outputs[link->srcpad].request_frame(link); | |
195 | } | |
196 | ||
197 | /* XXX: should we do the duplicating of the picture ref here, instead of | |
198 | * forcing the source filter to do it? */ | |
199 | void avfilter_start_frame(AVFilterLink *link, AVFilterPicRef *picref) | |
200 | { | |
201 | void (*start_frame)(AVFilterLink *, AVFilterPicRef *); | |
202 | ||
203 | start_frame = link->dst->filter->inputs[link->dstpad].start_frame; | |
204 | if(!start_frame) | |
205 | start_frame = avfilter_default_start_frame; | |
206 | ||
207 | start_frame(link, picref); | |
208 | } | |
209 | ||
210 | void avfilter_end_frame(AVFilterLink *link) | |
211 | { | |
212 | void (*end_frame)(AVFilterLink *); | |
213 | ||
214 | end_frame = link->dst->filter->inputs[link->dstpad].end_frame; | |
215 | if(!end_frame) | |
216 | end_frame = avfilter_default_end_frame; | |
217 | ||
218 | end_frame(link); | |
219 | } | |
220 | ||
221 | void avfilter_draw_slice(AVFilterLink *link, uint8_t *data[4], int y, int h) | |
222 | { | |
223 | link->dst->filter->inputs[link->dstpad].draw_slice(link, data, y, h); | |
224 | } | |
225 | ||
226 | static int filter_cmp(const void *aa, const void *bb) | |
227 | { | |
228 | const AVFilter *a = *(const AVFilter **)aa, *b = *(const AVFilter **)bb; | |
229 | return strcmp(a->name, b->name); | |
230 | } | |
231 | ||
232 | AVFilter *avfilter_get_by_name(char *name) | |
233 | { | |
234 | AVFilter key = { .name = name, }; | |
235 | AVFilter *key2 = &key; | |
236 | AVFilter **ret; | |
237 | ||
238 | ret = bsearch(&key2, filters, filter_count, sizeof(AVFilter **), filter_cmp); | |
239 | if(ret) | |
240 | return *ret; | |
241 | return NULL; | |
242 | } | |
243 | ||
244 | /* FIXME: insert in order, rather than insert at end + resort */ | |
245 | void avfilter_register(AVFilter *filter) | |
246 | { | |
247 | filters = av_realloc(filters, sizeof(AVFilter*) * (filter_count+1)); | |
248 | filters[filter_count] = filter; | |
249 | qsort(filters, ++filter_count, sizeof(AVFilter **), filter_cmp); | |
250 | } | |
251 | ||
252 | void avfilter_init(void) | |
253 | { | |
d72a138e | 254 | avfilter_register(&vsrc_dummy); |
eaf7eb66 | 255 | avfilter_register(&vsrc_ppm); |
d72a138e VS |
256 | avfilter_register(&vf_crop); |
257 | avfilter_register(&vf_passthrough); | |
72e3037a | 258 | avfilter_register(&vf_rgb2bgr); |
ce356b09 | 259 | avfilter_register(&vf_slicify); |
d72a138e | 260 | avfilter_register(&vo_sdl); |
4dbbcdee VS |
261 | } |
262 | ||
263 | void avfilter_uninit(void) | |
264 | { | |
265 | av_freep(&filters); | |
266 | filter_count = 0; | |
267 | } | |
268 | ||
269 | static int pad_count(const AVFilterPad *pads) | |
270 | { | |
271 | AVFilterPad *p = (AVFilterPad *) pads; | |
272 | int count; | |
273 | ||
274 | for(count = 0; p->name; count ++) p ++; | |
275 | return count; | |
276 | } | |
277 | ||
278 | static const char *filter_name(void *p) | |
279 | { | |
280 | AVFilterContext *filter = p; | |
281 | return filter->filter->name; | |
282 | } | |
283 | ||
284 | AVFilterContext *avfilter_create(AVFilter *filter) | |
285 | { | |
286 | AVFilterContext *ret = av_malloc(sizeof(AVFilterContext)); | |
287 | ||
288 | ret->av_class = av_mallocz(sizeof(AVClass)); | |
289 | ret->av_class->item_name = filter_name; | |
290 | ret->filter = filter; | |
291 | ret->inputs = av_mallocz(sizeof(AVFilterLink*) * pad_count(filter->inputs)); | |
292 | ret->outputs = av_mallocz(sizeof(AVFilterLink*) * pad_count(filter->outputs)); | |
293 | ret->priv = av_mallocz(filter->priv_size); | |
294 | ||
295 | return ret; | |
296 | } | |
297 | ||
298 | void avfilter_destroy(AVFilterContext *filter) | |
299 | { | |
300 | int i; | |
301 | ||
302 | if(filter->filter->uninit) | |
303 | filter->filter->uninit(filter); | |
304 | ||
305 | for(i = 0; i < pad_count(filter->filter->inputs); i ++) { | |
306 | if(filter->inputs[i]) | |
307 | filter->inputs[i]->src->outputs[filter->inputs[i]->srcpad] = NULL; | |
308 | av_free(filter->inputs[i]); | |
309 | } | |
310 | for(i = 0; i < pad_count(filter->filter->outputs); i ++) { | |
311 | if(filter->outputs[i]) | |
312 | filter->outputs[i]->dst->inputs[filter->outputs[i]->dstpad] = NULL; | |
313 | av_free(filter->outputs[i]); | |
314 | } | |
315 | ||
316 | av_free(filter->inputs); | |
317 | av_free(filter->outputs); | |
318 | av_free(filter->priv); | |
319 | av_free(filter->av_class); | |
320 | av_free(filter); | |
321 | } | |
322 | ||
323 | AVFilterContext *avfilter_create_by_name(char *name) | |
324 | { | |
325 | AVFilter *filt; | |
326 | ||
327 | if(!(filt = avfilter_get_by_name(name))) return NULL; | |
328 | return avfilter_create(filt); | |
329 | } | |
330 | ||
89e64908 | 331 | int avfilter_init_filter(AVFilterContext *filter, const char *args) |
4dbbcdee VS |
332 | { |
333 | int ret, i; | |
334 | ||
335 | if(filter->filter->init) | |
89e64908 | 336 | if((ret = filter->filter->init(filter, args))) return ret; |
4dbbcdee VS |
337 | return 0; |
338 | } | |
339 | ||
e0752603 VS |
340 | int *avfilter_make_format_list(int len, ...) |
341 | { | |
342 | int *ret, i; | |
343 | va_list vl; | |
344 | ||
345 | ret = av_malloc(sizeof(int) * (len + 1)); | |
346 | va_start(vl, len); | |
347 | for(i = 0; i < len; i ++) | |
348 | ret[i] = va_arg(vl, int); | |
349 | va_end(vl); | |
350 | ret[len] = -1; | |
351 | ||
352 | return ret; | |
353 | } | |
354 |