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