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