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