]> git.sesse.net Git - ffmpeg/blob - libavfilter/avfilter.c
When inserting a filter, don't lose any information we may already have
[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 int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
100                            unsigned in, unsigned out)
101 {
102     av_log(NULL, AV_LOG_INFO, "auto-inserting filter '%s'\n",
103             filt->filter->name);
104
105     link->dst->inputs[link->dstpad] = NULL;
106     if(avfilter_link(filt, out, link->dst, link->dstpad)) {
107         /* failed to link output filter to new filter */
108         link->dst->inputs[link->dstpad] = link;
109         return -1;
110     }
111
112     /* re-hookup the link to the new destination filter we inserted */
113     link->dst = filt;
114     link->dstpad = in;
115     filt->inputs[in] = link;
116
117     /* if any information on supported colorspaces already exists on the
118      * link, we need to preserve that */
119     if(link->out_formats) {
120         filt->outputs[out]->out_formats = link->out_formats;
121         link->out_formats = NULL;
122     }
123
124     return 0;
125 }
126
127 int avfilter_config_link(AVFilterLink *link)
128 {
129     int (*config_link)(AVFilterLink *);
130
131     if(!link)
132         return 0;
133
134     if(!(config_link = link_spad(link).config_props))
135         config_link  = avfilter_default_config_output_link;
136     if(config_link(link))
137             return -1;
138
139     if(!(config_link = link_dpad(link).config_props))
140         config_link  = avfilter_default_config_input_link;
141     if(config_link(link))
142             return -1;
143
144     return 0;
145 }
146
147 AVFilterPicRef *avfilter_get_video_buffer(AVFilterLink *link, int perms)
148 {
149     AVFilterPicRef *ret = NULL;
150
151     if(link_dpad(link).get_video_buffer)
152         ret = link_dpad(link).get_video_buffer(link, perms);
153
154     if(!ret)
155         ret = avfilter_default_get_video_buffer(link, perms);
156
157     return ret;
158 }
159
160 int avfilter_request_frame(AVFilterLink *link)
161 {
162     if(link_spad(link).request_frame)
163         return link_spad(link).request_frame(link);
164     else if(link->src->inputs[0])
165         return avfilter_request_frame(link->src->inputs[0]);
166     else return -1;
167 }
168
169 /* XXX: should we do the duplicating of the picture ref here, instead of
170  * forcing the source filter to do it? */
171 void avfilter_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
172 {
173     void (*start_frame)(AVFilterLink *, AVFilterPicRef *);
174
175     if(!(start_frame = link_dpad(link).start_frame))
176         start_frame = avfilter_default_start_frame;
177
178     /* prepare to copy the picture if it has insufficient permissions */
179     if((link_dpad(link).min_perms & picref->perms) != link_dpad(link).min_perms ||
180         link_dpad(link).rej_perms & picref->perms) {
181         av_log(link->dst, AV_LOG_INFO,
182                 "frame copy needed (have perms %x, need %x, reject %x)\n",
183                 picref->perms,
184                 link_dpad(link).min_perms, link_dpad(link).rej_perms);
185
186         link->cur_pic = avfilter_default_get_video_buffer(link, link_dpad(link).min_perms);
187         link->srcpic = picref;
188     }
189     else
190         link->cur_pic = picref;
191
192     start_frame(link, link->cur_pic);
193 }
194
195 void avfilter_end_frame(AVFilterLink *link)
196 {
197     void (*end_frame)(AVFilterLink *);
198
199     /* unreference the source picture if we're feeding the destination filter
200      * a copied version dues to permission issues */
201     if(link->srcpic) {
202         avfilter_unref_pic(link->srcpic);
203         link->srcpic = NULL;
204     }
205
206     if(!(end_frame = link_dpad(link).end_frame))
207         end_frame = avfilter_default_end_frame;
208
209     end_frame(link);
210 }
211
212 void avfilter_draw_slice(AVFilterLink *link, int y, int h)
213 {
214     uint8_t *src[4], *dst[4];
215     int i, j, hsub, vsub;
216
217     /* copy the slice if needed for permission reasons */
218     if(link->srcpic) {
219         avcodec_get_chroma_sub_sample(link->format, &hsub, &vsub);
220
221         src[0] = link->srcpic-> data[0] + y * link->srcpic-> linesize[0];
222         dst[0] = link->cur_pic->data[0] + y * link->cur_pic->linesize[0];
223         for(i = 1; i < 4; i ++) {
224             if(link->srcpic->data[i]) {
225                 src[i] = link->srcpic-> data[i] + (y >> vsub) * link->srcpic-> linesize[i];
226                 dst[i] = link->cur_pic->data[i] + (y >> vsub) * link->cur_pic->linesize[i];
227             } else
228                 src[i] = dst[i] = NULL;
229         }
230         for(j = 0; j < h; j ++) {
231             memcpy(dst[0], src[0], link->cur_pic->linesize[0]);
232             src[0] += link->srcpic ->linesize[0];
233             dst[0] += link->cur_pic->linesize[0];
234         }
235         for(i = 1; i < 4; i ++) {
236             if(!src[i]) continue;
237
238             for(j = 0; j < h >> vsub; j ++) {
239                 memcpy(dst[i], src[i], link->cur_pic->linesize[i]);
240                 src[i] += link->srcpic ->linesize[i];
241                 dst[i] += link->cur_pic->linesize[i];
242             }
243         }
244     }
245
246     if(!link_dpad(link).draw_slice)
247         return;
248
249     link_dpad(link).draw_slice(link, y, h);
250 }
251
252 AVFilter *avfilter_get_by_name(char *name)
253 {
254     struct FilterList *filt;
255
256     for(filt = filters; filt; filt = filt->next)
257         if(!strcmp(filt->filter->name, name))
258             return filt->filter;
259
260     return NULL;
261 }
262
263 void avfilter_register(AVFilter *filter)
264 {
265     struct FilterList *newfilt = av_malloc(sizeof(struct FilterList));
266
267     newfilt->filter = filter;
268     newfilt->next   = filters;
269     filters         = newfilt;
270 }
271
272 void avfilter_init(void)
273 {
274     avfilter_register(&avfilter_vf_crop);
275     avfilter_register(&avfilter_vf_fifo);
276     avfilter_register(&avfilter_vf_fps);
277     avfilter_register(&avfilter_vf_graph);
278     avfilter_register(&avfilter_vf_graphdesc);
279     avfilter_register(&avfilter_vf_graphfile);
280     avfilter_register(&avfilter_vf_negate);
281     avfilter_register(&avfilter_vf_overlay);
282     avfilter_register(&avfilter_vf_passthrough);
283     avfilter_register(&avfilter_vf_scale);
284     avfilter_register(&avfilter_vf_slicify);
285     avfilter_register(&avfilter_vf_split);
286     avfilter_register(&avfilter_vf_vflip);
287 }
288
289 void avfilter_uninit(void)
290 {
291     struct FilterList *tmp;
292
293     for(; filters; filters = tmp) {
294         tmp = filters->next;
295         av_free(filters);
296     }
297 }
298
299 static int pad_count(const AVFilterPad *pads)
300 {
301     AVFilterPad *p = (AVFilterPad *) pads;
302     int count;
303
304     for(count = 0; p->name; count ++) p ++;
305     return count;
306 }
307
308 static const char *filter_name(void *p)
309 {
310     AVFilterContext *filter = p;
311     return filter->filter->name;
312 }
313
314 AVFilterContext *avfilter_open(AVFilter *filter, char *inst_name)
315 {
316     AVFilterContext *ret;
317
318     if (!filter)
319         return 0;
320
321     ret = av_malloc(sizeof(AVFilterContext));
322
323     ret->av_class = av_mallocz(sizeof(AVClass));
324     ret->av_class->item_name = filter_name;
325     ret->filter   = filter;
326     ret->name     = inst_name ? av_strdup(inst_name) : NULL;
327     ret->priv     = av_mallocz(filter->priv_size);
328
329     ret->input_count  = pad_count(filter->inputs);
330     ret->input_pads   = av_malloc(sizeof(AVFilterPad) * ret->input_count);
331     memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad)*ret->input_count);
332     ret->inputs       = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
333
334     ret->output_count = pad_count(filter->outputs);
335     ret->output_pads  = av_malloc(sizeof(AVFilterPad) * ret->output_count);
336     memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad)*ret->output_count);
337     ret->outputs      = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
338
339     return ret;
340 }
341
342 void avfilter_destroy(AVFilterContext *filter)
343 {
344     int i;
345
346     if(filter->filter->uninit)
347         filter->filter->uninit(filter);
348
349     for(i = 0; i < filter->input_count; i ++) {
350         if(filter->inputs[i])
351             filter->inputs[i]->src->outputs[filter->inputs[i]->srcpad] = NULL;
352         av_freep(&filter->inputs[i]);
353     }
354     for(i = 0; i < filter->output_count; i ++) {
355         if(filter->outputs[i])
356             filter->outputs[i]->dst->inputs[filter->outputs[i]->dstpad] = NULL;
357         av_freep(&filter->outputs[i]);
358     }
359
360     av_freep(&filter->name);
361     av_freep(&filter->input_pads);
362     av_freep(&filter->output_pads);
363     av_freep(&filter->inputs);
364     av_freep(&filter->outputs);
365     av_freep(&filter->priv);
366     av_freep(&filter->av_class);
367     av_free(filter);
368 }
369
370 int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
371 {
372     int ret;
373
374     if(filter->filter->init)
375         if((ret = filter->filter->init(filter, args, opaque))) return ret;
376     return 0;
377 }
378