Remove the data parameter from draw_slice(). It's unneeded and often more
[libav.git] / libavfilter / avfilter.c
CommitLineData
4dbbcdee
VS
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
e0752603 22#include <stdarg.h>
4dbbcdee
VS
23#include <stdlib.h>
24#include <string.h>
25#include <stdio.h>
26
27#include "avfilter.h"
d72a138e 28#include "allfilters.h"
4dbbcdee
VS
29
30/** list of registered filters, sorted by name */
31static int filter_count = 0;
32static AVFilter **filters = NULL;
33
efb36bfc 34AVFilterPicRef *avfilter_ref_pic(AVFilterPicRef *ref, int pmask)
4dbbcdee
VS
35{
36 AVFilterPicRef *ret = av_malloc(sizeof(AVFilterPicRef));
37 memcpy(ret, ref, sizeof(AVFilterPicRef));
efb36bfc 38 ret->perms &= pmask;
4dbbcdee
VS
39 ret->pic->refcount ++;
40 return ret;
41}
42
43void avfilter_unref_pic(AVFilterPicRef *ref)
44{
45 if(-- ref->pic->refcount == 0)
46 ref->pic->free(ref->pic);
47 av_free(ref);
48}
49
24618441
VS
50void avfilter_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
51 AVFilterPad **pads, AVFilterLink ***links,
52 AVFilterPad *newpad)
53{
54 unsigned i;
55
56 idx = FFMIN(idx, *count);
57
58 *pads = av_realloc(*pads, sizeof(AVFilterPad) * (*count + 1));
59 *links = av_realloc(*links, sizeof(AVFilterLink*) * (*count + 1));
60 memmove(*pads +idx+1, *pads +idx, sizeof(AVFilterPad) * (*count-idx));
61 memmove(*links+idx+1, *links+idx, sizeof(AVFilterLink*) * (*count-idx));
62 memcpy(*pads+idx, newpad, sizeof(AVFilterPad));
63 (*links)[idx] = NULL;
64
65 (*count) ++;
66 for(i = idx+1; i < *count; i ++)
67 if(*links[i])
68 (*(unsigned *)((uint8_t *)(*links[i]) + padidx_off)) ++;
69}
70
4dbbcdee
VS
71int avfilter_link(AVFilterContext *src, unsigned srcpad,
72 AVFilterContext *dst, unsigned dstpad)
73{
74 AVFilterLink *link;
75
7d0e1392
VS
76 if(src->output_count <= srcpad || dst->input_count <= dstpad ||
77 src->outputs[srcpad] || dst->inputs[dstpad])
4dbbcdee
VS
78 return -1;
79
80 src->outputs[srcpad] =
81 dst->inputs[dstpad] = link = av_malloc(sizeof(AVFilterLink));
82
1653c11f
VS
83 link->src = src;
84 link->dst = dst;
85 link->srcpad = srcpad;
86 link->dstpad = dstpad;
4dbbcdee 87 link->cur_pic = NULL;
01942f1d
VS
88 link->format = -1;
89
90 return 0;
91}
92
93int avfilter_config_link(AVFilterLink *link)
94{
95 int *fmts[2], i, j;
96 int (*config_link)(AVFilterLink *);
97
98 if(!link)
99 return 0;
4dbbcdee 100
e0752603 101 /* find a format both filters support - TODO: auto-insert conversion filter */
998a7aa3 102 link->format = -1;
01942f1d
VS
103 if(link->src->output_pads[link->srcpad].query_formats)
104 fmts[0] = link->src->output_pads[link->srcpad].query_formats(link);
102fb0e3 105 else
78b0c0bb 106 fmts[0] = avfilter_default_query_output_formats(link);
01942f1d 107 fmts[1] = link->dst->input_pads[link->dstpad].query_formats(link);
e0752603
VS
108 for(i = 0; fmts[0][i] != -1; i ++)
109 for(j = 0; fmts[1][j] != -1; j ++)
110 if(fmts[0][i] == fmts[1][j]) {
111 link->format = fmts[0][i];
112 goto format_done;
113 }
114
115format_done:
116 av_free(fmts[0]);
117 av_free(fmts[1]);
01942f1d 118 if(link->format == -1)
e0752603 119 return -1;
e0752603 120
01942f1d
VS
121 if(!(config_link = link->src->output_pads[link->srcpad].config_props))
122 config_link = avfilter_default_config_output_link;
123 if(config_link(link))
124 return -1;
102fb0e3 125
01942f1d
VS
126 if(!(config_link = link->dst->input_pads[link->dstpad].config_props))
127 config_link = avfilter_default_config_input_link;
128 if(config_link(link))
129 return -1;
102fb0e3 130
4dbbcdee
VS
131 return 0;
132}
133
134AVFilterPicRef *avfilter_get_video_buffer(AVFilterLink *link, int perms)
135{
136 AVFilterPicRef *ret = NULL;
137
c5ef7d7b
VS
138 if(link->dst->input_pads[link->dstpad].get_video_buffer)
139 ret = link->dst->input_pads[link->dstpad].get_video_buffer(link, perms);
4dbbcdee
VS
140
141 if(!ret)
142 ret = avfilter_default_get_video_buffer(link, perms);
143
144 return ret;
145}
146
3306f8bd 147int avfilter_request_frame(AVFilterLink *link)
4dbbcdee 148{
c5ef7d7b 149 const AVFilterPad *pad = &link->src->output_pads[link->srcpad];
9586ba3a
VS
150
151 if(pad->request_frame)
3306f8bd 152 return pad->request_frame(link);
9586ba3a 153 else if(link->src->inputs[0])
3306f8bd
VS
154 return avfilter_request_frame(link->src->inputs[0]);
155 else return -1;
4dbbcdee
VS
156}
157
158/* XXX: should we do the duplicating of the picture ref here, instead of
159 * forcing the source filter to do it? */
160void avfilter_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
161{
162 void (*start_frame)(AVFilterLink *, AVFilterPicRef *);
163
c5ef7d7b 164 start_frame = link->dst->input_pads[link->dstpad].start_frame;
4dbbcdee
VS
165 if(!start_frame)
166 start_frame = avfilter_default_start_frame;
167
168 start_frame(link, picref);
169}
170
171void avfilter_end_frame(AVFilterLink *link)
172{
173 void (*end_frame)(AVFilterLink *);
174
c5ef7d7b 175 end_frame = link->dst->input_pads[link->dstpad].end_frame;
4dbbcdee
VS
176 if(!end_frame)
177 end_frame = avfilter_default_end_frame;
178
179 end_frame(link);
180}
181
ef35d13a 182void avfilter_draw_slice(AVFilterLink *link, int y, int h)
4dbbcdee 183{
c5ef7d7b 184 if(!link->dst->input_pads[link->dstpad].draw_slice)
7d0e1392
VS
185 return;
186
ef35d13a 187 link->dst->input_pads[link->dstpad].draw_slice(link, y, h);
4dbbcdee
VS
188}
189
190static int filter_cmp(const void *aa, const void *bb)
191{
192 const AVFilter *a = *(const AVFilter **)aa, *b = *(const AVFilter **)bb;
193 return strcmp(a->name, b->name);
194}
195
196AVFilter *avfilter_get_by_name(char *name)
197{
198 AVFilter key = { .name = name, };
199 AVFilter *key2 = &key;
200 AVFilter **ret;
201
202 ret = bsearch(&key2, filters, filter_count, sizeof(AVFilter **), filter_cmp);
203 if(ret)
204 return *ret;
205 return NULL;
206}
207
208/* FIXME: insert in order, rather than insert at end + resort */
209void avfilter_register(AVFilter *filter)
210{
211 filters = av_realloc(filters, sizeof(AVFilter*) * (filter_count+1));
212 filters[filter_count] = filter;
213 qsort(filters, ++filter_count, sizeof(AVFilter **), filter_cmp);
214}
215
216void avfilter_init(void)
217{
d72a138e 218 avfilter_register(&vsrc_dummy);
eaf7eb66 219 avfilter_register(&vsrc_ppm);
d72a138e 220 avfilter_register(&vf_crop);
49d915cb 221 avfilter_register(&vf_fps);
d4169dd3 222 avfilter_register(&vf_graph);
f9285377
VS
223 avfilter_register(&vf_graphdesc);
224 avfilter_register(&vf_graphfile);
ef712a3b 225 avfilter_register(&vf_overlay);
d72a138e 226 avfilter_register(&vf_passthrough);
72e3037a 227 avfilter_register(&vf_rgb2bgr);
ce356b09 228 avfilter_register(&vf_slicify);
e2fcb3cb 229 avfilter_register(&vf_vflip);
4dbbcdee
VS
230}
231
232void avfilter_uninit(void)
233{
234 av_freep(&filters);
235 filter_count = 0;
236}
237
238static int pad_count(const AVFilterPad *pads)
239{
240 AVFilterPad *p = (AVFilterPad *) pads;
241 int count;
242
243 for(count = 0; p->name; count ++) p ++;
244 return count;
245}
246
247static const char *filter_name(void *p)
248{
249 AVFilterContext *filter = p;
250 return filter->filter->name;
251}
252
6ae82d1e 253AVFilterContext *avfilter_create(AVFilter *filter, char *inst_name)
4dbbcdee
VS
254{
255 AVFilterContext *ret = av_malloc(sizeof(AVFilterContext));
256
257 ret->av_class = av_mallocz(sizeof(AVClass));
258 ret->av_class->item_name = filter_name;
259 ret->filter = filter;
2350e69c 260 ret->name = inst_name ? av_strdup(inst_name) : NULL;
4dbbcdee
VS
261 ret->priv = av_mallocz(filter->priv_size);
262
7d0e1392 263 ret->input_count = pad_count(filter->inputs);
c5ef7d7b
VS
264 ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->input_count);
265 memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad)*ret->input_count);
7d0e1392 266 ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
c5ef7d7b 267
7d0e1392 268 ret->output_count = pad_count(filter->outputs);
c5ef7d7b
VS
269 ret->output_pads = av_malloc(sizeof(AVFilterPad) * ret->output_count);
270 memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad)*ret->output_count);
7d0e1392
VS
271 ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
272
4dbbcdee
VS
273 return ret;
274}
275
276void avfilter_destroy(AVFilterContext *filter)
277{
278 int i;
279
280 if(filter->filter->uninit)
281 filter->filter->uninit(filter);
282
c5ef7d7b 283 for(i = 0; i < filter->input_count; i ++) {
4dbbcdee
VS
284 if(filter->inputs[i])
285 filter->inputs[i]->src->outputs[filter->inputs[i]->srcpad] = NULL;
286 av_free(filter->inputs[i]);
287 }
c5ef7d7b 288 for(i = 0; i < filter->output_count; i ++) {
4dbbcdee
VS
289 if(filter->outputs[i])
290 filter->outputs[i]->dst->inputs[filter->outputs[i]->dstpad] = NULL;
291 av_free(filter->outputs[i]);
292 }
293
2350e69c 294 av_free(filter->name);
c5ef7d7b
VS
295 av_free(filter->input_pads);
296 av_free(filter->output_pads);
4dbbcdee
VS
297 av_free(filter->inputs);
298 av_free(filter->outputs);
299 av_free(filter->priv);
300 av_free(filter->av_class);
301 av_free(filter);
302}
303
6ae82d1e 304AVFilterContext *avfilter_create_by_name(char *name, char *inst_name)
4dbbcdee
VS
305{
306 AVFilter *filt;
307
308 if(!(filt = avfilter_get_by_name(name))) return NULL;
6ae82d1e 309 return avfilter_create(filt, inst_name);
4dbbcdee
VS
310}
311
a360f71e 312int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
4dbbcdee 313{
c6b0aa68 314 int ret;
4dbbcdee
VS
315
316 if(filter->filter->init)
a360f71e 317 if((ret = filter->filter->init(filter, args, opaque))) return ret;
4dbbcdee
VS
318 return 0;
319}
320
e0752603
VS
321int *avfilter_make_format_list(int len, ...)
322{
323 int *ret, i;
324 va_list vl;
325
326 ret = av_malloc(sizeof(int) * (len + 1));
327 va_start(vl, len);
328 for(i = 0; i < len; i ++)
329 ret[i] = va_arg(vl, int);
330 va_end(vl);
331 ret[len] = -1;
332
333 return ret;
334}
335