]> git.sesse.net Git - ffmpeg/blob - libavfilter/avfilter.c
Provide a default for request_frame() which does the right thing for simple
[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     AVFilterLink *out = link->dst->outputs[0];
68
69     link->cur_pic = picref;
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     }
75 }
76
77 void avfilter_default_end_frame(AVFilterLink *link)
78 {
79     AVFilterLink *out = link->dst->outputs[0];
80
81     avfilter_unref_pic(link->cur_pic);
82     link->cur_pic = NULL;
83
84     if(out) {
85         avfilter_unref_pic(out->outpic);
86         out->outpic = NULL;
87         avfilter_end_frame(out);
88     }
89 }
90
91 AVFilterPicRef *avfilter_ref_pic(AVFilterPicRef *ref, int pmask)
92 {
93     AVFilterPicRef *ret = av_malloc(sizeof(AVFilterPicRef));
94     memcpy(ret, ref, sizeof(AVFilterPicRef));
95     ret->perms &= pmask;
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
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
126 int avfilter_link(AVFilterContext *src, unsigned srcpad,
127                   AVFilterContext *dst, unsigned dstpad)
128 {
129     AVFilterLink *link;
130     int *fmts[2], i, j;
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
144     /* find a format both filters support - TODO: auto-insert conversion filter */
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);
149     fmts[1] = dst->filter-> inputs[dstpad].query_formats(link);
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
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
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     const AVFilterPad *pad = &link->src->filter->outputs[link->srcpad];
195
196     if(pad->request_frame)
197         pad->request_frame(link);
198     else if(link->src->inputs[0])
199         avfilter_request_frame(link->src->inputs[0]);
200 }
201
202 /* XXX: should we do the duplicating of the picture ref here, instead of
203  * forcing the source filter to do it? */
204 void avfilter_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
205 {
206     void (*start_frame)(AVFilterLink *, AVFilterPicRef *);
207
208     start_frame = link->dst->filter->inputs[link->dstpad].start_frame;
209     if(!start_frame)
210         start_frame = avfilter_default_start_frame;
211
212     start_frame(link, picref);
213 }
214
215 void avfilter_end_frame(AVFilterLink *link)
216 {
217     void (*end_frame)(AVFilterLink *);
218
219     end_frame = link->dst->filter->inputs[link->dstpad].end_frame;
220     if(!end_frame)
221         end_frame = avfilter_default_end_frame;
222
223     end_frame(link);
224 }
225
226 void avfilter_draw_slice(AVFilterLink *link, uint8_t *data[4], int y, int h)
227 {
228     link->dst->filter->inputs[link->dstpad].draw_slice(link, data, y, h);
229 }
230
231 static int filter_cmp(const void *aa, const void *bb)
232 {
233     const AVFilter *a = *(const AVFilter **)aa, *b = *(const AVFilter **)bb;
234     return strcmp(a->name, b->name);
235 }
236
237 AVFilter *avfilter_get_by_name(char *name)
238 {
239     AVFilter key = { .name = name, };
240     AVFilter *key2 = &key;
241     AVFilter **ret;
242
243     ret = bsearch(&key2, filters, filter_count, sizeof(AVFilter **), filter_cmp);
244     if(ret)
245         return *ret;
246     return NULL;
247 }
248
249 /* FIXME: insert in order, rather than insert at end + resort */
250 void avfilter_register(AVFilter *filter)
251 {
252     filters = av_realloc(filters, sizeof(AVFilter*) * (filter_count+1));
253     filters[filter_count] = filter;
254     qsort(filters, ++filter_count, sizeof(AVFilter **), filter_cmp);
255 }
256
257 void avfilter_init(void)
258 {
259     avfilter_register(&vsrc_dummy);
260     avfilter_register(&vsrc_ppm);
261     avfilter_register(&vf_crop);
262     avfilter_register(&vf_passthrough);
263     avfilter_register(&vf_rgb2bgr);
264     avfilter_register(&vf_slicify);
265     avfilter_register(&vo_sdl);
266 }
267
268 void avfilter_uninit(void)
269 {
270     av_freep(&filters);
271     filter_count = 0;
272 }
273
274 static int pad_count(const AVFilterPad *pads)
275 {
276     AVFilterPad *p = (AVFilterPad *) pads;
277     int count;
278
279     for(count = 0; p->name; count ++) p ++;
280     return count;
281 }
282
283 static const char *filter_name(void *p)
284 {
285     AVFilterContext *filter = p;
286     return filter->filter->name;
287 }
288
289 AVFilterContext *avfilter_create(AVFilter *filter)
290 {
291     AVFilterContext *ret = av_malloc(sizeof(AVFilterContext));
292
293     ret->av_class = av_mallocz(sizeof(AVClass));
294     ret->av_class->item_name = filter_name;
295     ret->filter   = filter;
296     ret->inputs   = av_mallocz(sizeof(AVFilterLink*) * pad_count(filter->inputs));
297     ret->outputs  = av_mallocz(sizeof(AVFilterLink*) * pad_count(filter->outputs));
298     ret->priv     = av_mallocz(filter->priv_size);
299
300     return ret;
301 }
302
303 void avfilter_destroy(AVFilterContext *filter)
304 {
305     int i;
306
307     if(filter->filter->uninit)
308         filter->filter->uninit(filter);
309
310     for(i = 0; i < pad_count(filter->filter->inputs); i ++) {
311         if(filter->inputs[i])
312             filter->inputs[i]->src->outputs[filter->inputs[i]->srcpad] = NULL;
313         av_free(filter->inputs[i]);
314     }
315     for(i = 0; i < pad_count(filter->filter->outputs); i ++) {
316         if(filter->outputs[i])
317             filter->outputs[i]->dst->inputs[filter->outputs[i]->dstpad] = NULL;
318         av_free(filter->outputs[i]);
319     }
320
321     av_free(filter->inputs);
322     av_free(filter->outputs);
323     av_free(filter->priv);
324     av_free(filter->av_class);
325     av_free(filter);
326 }
327
328 AVFilterContext *avfilter_create_by_name(char *name)
329 {
330     AVFilter *filt;
331
332     if(!(filt = avfilter_get_by_name(name))) return NULL;
333     return avfilter_create(filt);
334 }
335
336 int avfilter_init_filter(AVFilterContext *filter, const char *args)
337 {
338     int ret, i;
339
340     if(filter->filter->init)
341         if((ret = filter->filter->init(filter, args))) return ret;
342     return 0;
343 }
344
345 int *avfilter_make_format_list(int len, ...)
346 {
347     int *ret, i;
348     va_list vl;
349
350     ret = av_malloc(sizeof(int) * (len + 1));
351     va_start(vl, len);
352     for(i = 0; i < len; i ++)
353         ret[i] = va_arg(vl, int);
354     va_end(vl);
355     ret[len] = -1;
356
357     return ret;
358 }
359