]> git.sesse.net Git - ffmpeg/blob - libavfilter/avfilter.c
Allow filters to set the requirements on permissions for incoming buffers.
[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 /** helper macros to get the in/out pad on the dst/src filter */
35 #define link_dpad(link)     link->dst-> input_pads[link->dstpad]
36 #define link_spad(link)     link->src->output_pads[link->srcpad]
37
38 AVFilterPicRef *avfilter_ref_pic(AVFilterPicRef *ref, int pmask)
39 {
40     AVFilterPicRef *ret = av_malloc(sizeof(AVFilterPicRef));
41     memcpy(ret, ref, sizeof(AVFilterPicRef));
42     ret->perms &= pmask;
43     ret->pic->refcount ++;
44     return ret;
45 }
46
47 void avfilter_unref_pic(AVFilterPicRef *ref)
48 {
49     if(-- ref->pic->refcount == 0)
50         ref->pic->free(ref->pic);
51     av_free(ref);
52 }
53
54 void avfilter_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
55                          AVFilterPad **pads, AVFilterLink ***links,
56                          AVFilterPad *newpad)
57 {
58     unsigned i;
59
60     idx = FFMIN(idx, *count);
61
62     *pads  = av_realloc(*pads,  sizeof(AVFilterPad)   * (*count + 1));
63     *links = av_realloc(*links, sizeof(AVFilterLink*) * (*count + 1));
64     memmove(*pads +idx+1, *pads +idx, sizeof(AVFilterPad)   * (*count-idx));
65     memmove(*links+idx+1, *links+idx, sizeof(AVFilterLink*) * (*count-idx));
66     memcpy(*pads+idx, newpad, sizeof(AVFilterPad));
67     (*links)[idx] = NULL;
68
69     (*count) ++;
70     for(i = idx+1; i < *count; i ++)
71         if(*links[i])
72             (*(unsigned *)((uint8_t *)(*links[i]) + padidx_off)) ++;
73 }
74
75 int avfilter_link(AVFilterContext *src, unsigned srcpad,
76                   AVFilterContext *dst, unsigned dstpad)
77 {
78     AVFilterLink *link;
79
80     if(src->output_count <= srcpad || dst->input_count <= dstpad ||
81        src->outputs[srcpad]        || dst->inputs[dstpad])
82         return -1;
83
84     src->outputs[srcpad] =
85     dst->inputs[dstpad]  = link = av_mallocz(sizeof(AVFilterLink));
86
87     link->src     = src;
88     link->dst     = dst;
89     link->srcpad  = srcpad;
90     link->dstpad  = dstpad;
91     link->format  = -1;
92
93     return 0;
94 }
95
96 int avfilter_config_link(AVFilterLink *link)
97 {
98     int *fmts[2], i, j;
99     int (*config_link)(AVFilterLink *);
100
101     if(!link)
102         return 0;
103
104     /* find a format both filters support - TODO: auto-insert conversion filter */
105     link->format = -1;
106     if(link_spad(link).query_formats)
107         fmts[0] = link_spad(link).query_formats(link);
108     else
109         fmts[0] = avfilter_default_query_output_formats(link);
110     fmts[1] = link_dpad(link).query_formats(link);
111     for(i = 0; fmts[0][i] != -1; i ++)
112         for(j = 0; fmts[1][j] != -1; j ++)
113             if(fmts[0][i] == fmts[1][j]) {
114                 link->format = fmts[0][i];
115                 goto format_done;
116             }
117
118 format_done:
119     av_free(fmts[0]);
120     av_free(fmts[1]);
121     if(link->format == -1)
122         return -1;
123
124     if(!(config_link = link_spad(link).config_props))
125         config_link  = avfilter_default_config_output_link;
126     if(config_link(link))
127             return -1;
128
129     if(!(config_link = link_dpad(link).config_props))
130         config_link  = avfilter_default_config_input_link;
131     if(config_link(link))
132             return -1;
133
134     return 0;
135 }
136
137 AVFilterPicRef *avfilter_get_video_buffer(AVFilterLink *link, int perms)
138 {
139     AVFilterPicRef *ret = NULL;
140
141     if(link_dpad(link).get_video_buffer)
142         ret = link_dpad(link).get_video_buffer(link, perms);
143
144     if(!ret)
145         ret = avfilter_default_get_video_buffer(link, perms);
146
147     return ret;
148 }
149
150 int avfilter_request_frame(AVFilterLink *link)
151 {
152     if(link_spad(link).request_frame)
153         return link_spad(link).request_frame(link);
154     else if(link->src->inputs[0])
155         return avfilter_request_frame(link->src->inputs[0]);
156     else return -1;
157 }
158
159 /* XXX: should we do the duplicating of the picture ref here, instead of
160  * forcing the source filter to do it? */
161 void avfilter_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
162 {
163     void (*start_frame)(AVFilterLink *, AVFilterPicRef *);
164
165     start_frame = link_dpad(link).start_frame;
166     if(!start_frame)
167         start_frame = avfilter_default_start_frame;
168
169     /* prepare to copy the picture if it has insufficient permissions */
170     if((link_dpad(link).min_perms & picref->perms) != link_dpad(link).min_perms ||
171         link_dpad(link).rej_perms & picref->perms) {
172         av_log(link->dst, AV_LOG_INFO,
173                 "frame copy needed (have perms %x, need %x, reject %x)\n",
174                 picref->perms,
175                 link_dpad(link).min_perms, link_dpad(link).rej_perms);
176
177         link->cur_pic = avfilter_default_get_video_buffer(link, link_dpad(link).min_perms);
178         link->srcpic = picref;
179     }
180     else
181         link->cur_pic = picref;
182
183     start_frame(link, link->cur_pic);
184 }
185
186 void avfilter_end_frame(AVFilterLink *link)
187 {
188     void (*end_frame)(AVFilterLink *);
189
190     /* unreference the source picture if we're feeding the destination filter
191      * a copied version dues to permission issues */
192     if(link->srcpic) {
193         avfilter_unref_pic(link->srcpic);
194         link->srcpic = NULL;
195     }
196
197     end_frame = link_dpad(link).end_frame;
198     if(!end_frame)
199         end_frame = avfilter_default_end_frame;
200
201     end_frame(link);
202 }
203
204 void avfilter_draw_slice(AVFilterLink *link, int y, int h)
205 {
206     uint8_t *src[4], *dst[4];
207     int i, j, hsub, vsub;
208
209     /* copy the slice if needed for permission reasons */
210     if(link->srcpic) {
211         avcodec_get_chroma_sub_sample(link->format, &hsub, &vsub);
212
213         src[0] = link->srcpic-> data[0] + y * link->srcpic-> linesize[0];
214         dst[0] = link->cur_pic->data[0] + y * link->cur_pic->linesize[0];
215         for(i = 1; i < 4; i ++) {
216             if(link->srcpic->data[i]) {
217                 src[i] = link->srcpic-> data[i] + (y >> vsub) * link->srcpic-> linesize[i];
218                 dst[i] = link->cur_pic->data[i] + (y >> vsub) * link->cur_pic->linesize[i];
219             } else
220                 src[i] = dst[i] = NULL;
221         }
222         for(j = 0; j < h; j ++) {
223             memcpy(dst[0], src[0], link->cur_pic->linesize[0]);
224             src[0] += link->srcpic ->linesize[0];
225             dst[0] += link->cur_pic->linesize[0];
226         }
227         for(i = 1; i < 4; i ++) {
228             if(!src[i]) continue;
229
230             for(j = 0; j < h >> vsub; j ++) {
231                 memcpy(dst[i], src[i], link->cur_pic->linesize[i]);
232                 src[i] += link->srcpic ->linesize[i];
233                 dst[i] += link->cur_pic->linesize[i];
234             }
235         }
236     }
237
238     if(!link_dpad(link).draw_slice)
239         return;
240
241     link_dpad(link).draw_slice(link, y, h);
242 }
243
244 static int filter_cmp(const void *aa, const void *bb)
245 {
246     const AVFilter *a = *(const AVFilter **)aa, *b = *(const AVFilter **)bb;
247     return strcmp(a->name, b->name);
248 }
249
250 AVFilter *avfilter_get_by_name(char *name)
251 {
252     AVFilter key = { .name = name, };
253     AVFilter *key2 = &key;
254     AVFilter **ret;
255
256     ret = bsearch(&key2, filters, filter_count, sizeof(AVFilter **), filter_cmp);
257     if(ret)
258         return *ret;
259     return NULL;
260 }
261
262 /* FIXME: insert in order, rather than insert at end + resort */
263 void avfilter_register(AVFilter *filter)
264 {
265     filters = av_realloc(filters, sizeof(AVFilter*) * (filter_count+1));
266     filters[filter_count] = filter;
267     qsort(filters, ++filter_count, sizeof(AVFilter **), filter_cmp);
268 }
269
270 void avfilter_init(void)
271 {
272     avfilter_register(&vsrc_dummy);
273     avfilter_register(&vsrc_ppm);
274     avfilter_register(&vf_crop);
275     avfilter_register(&vf_fps);
276     avfilter_register(&vf_graph);
277     avfilter_register(&vf_graphdesc);
278     avfilter_register(&vf_graphfile);
279     avfilter_register(&vf_overlay);
280     avfilter_register(&vf_passthrough);
281     avfilter_register(&vf_rgb2bgr);
282     avfilter_register(&vf_slicify);
283     avfilter_register(&vf_vflip);
284 }
285
286 void avfilter_uninit(void)
287 {
288     av_freep(&filters);
289     filter_count = 0;
290 }
291
292 static int pad_count(const AVFilterPad *pads)
293 {
294     AVFilterPad *p = (AVFilterPad *) pads;
295     int count;
296
297     for(count = 0; p->name; count ++) p ++;
298     return count;
299 }
300
301 static const char *filter_name(void *p)
302 {
303     AVFilterContext *filter = p;
304     return filter->filter->name;
305 }
306
307 AVFilterContext *avfilter_create(AVFilter *filter, char *inst_name)
308 {
309     AVFilterContext *ret = av_malloc(sizeof(AVFilterContext));
310
311     ret->av_class = av_mallocz(sizeof(AVClass));
312     ret->av_class->item_name = filter_name;
313     ret->filter   = filter;
314     ret->name     = inst_name ? av_strdup(inst_name) : NULL;
315     ret->priv     = av_mallocz(filter->priv_size);
316
317     ret->input_count  = pad_count(filter->inputs);
318     ret->input_pads   = av_malloc(sizeof(AVFilterPad) * ret->input_count);
319     memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad)*ret->input_count);
320     ret->inputs       = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
321
322     ret->output_count = pad_count(filter->outputs);
323     ret->output_pads  = av_malloc(sizeof(AVFilterPad) * ret->output_count);
324     memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad)*ret->output_count);
325     ret->outputs      = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
326
327     return ret;
328 }
329
330 void avfilter_destroy(AVFilterContext *filter)
331 {
332     int i;
333
334     if(filter->filter->uninit)
335         filter->filter->uninit(filter);
336
337     for(i = 0; i < filter->input_count; i ++) {
338         if(filter->inputs[i])
339             filter->inputs[i]->src->outputs[filter->inputs[i]->srcpad] = NULL;
340         av_free(filter->inputs[i]);
341     }
342     for(i = 0; i < filter->output_count; i ++) {
343         if(filter->outputs[i])
344             filter->outputs[i]->dst->inputs[filter->outputs[i]->dstpad] = NULL;
345         av_free(filter->outputs[i]);
346     }
347
348     av_free(filter->name);
349     av_free(filter->input_pads);
350     av_free(filter->output_pads);
351     av_free(filter->inputs);
352     av_free(filter->outputs);
353     av_free(filter->priv);
354     av_free(filter->av_class);
355     av_free(filter);
356 }
357
358 AVFilterContext *avfilter_create_by_name(char *name, char *inst_name)
359 {
360     AVFilter *filt;
361
362     if(!(filt = avfilter_get_by_name(name))) return NULL;
363     return avfilter_create(filt, inst_name);
364 }
365
366 int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
367 {
368     int ret;
369
370     if(filter->filter->init)
371         if((ret = filter->filter->init(filter, args, opaque))) return ret;
372     return 0;
373 }
374
375 int *avfilter_make_format_list(int len, ...)
376 {
377     int *ret, i;
378     va_list vl;
379
380     ret = av_malloc(sizeof(int) * (len + 1));
381     va_start(vl, len);
382     for(i = 0; i < len; i ++)
383         ret[i] = va_arg(vl, int);
384     va_end(vl);
385     ret[len] = -1;
386
387     return ret;
388 }
389