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