Track the permissions that have been given out to each picture.
[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
a4af86c8
VS
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 */
43static 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
60int avfilter_get_pic_perms(AVFilterPicRef *pic, AVFilterContext *filter)
4dbbcdee 61{
a4af86c8
VS
62 return get_perms(pic->pic, filter)->perms;
63}
64
65void avfilter_add_pic_perms(AVFilterPicRef *pic, AVFilterContext *filter,
66 int perms)
67{
68 get_perms(pic->pic, filter)->perms |= perms;
69}
70
71AVFilterPicRef *avfilter_ref_pic(AVFilterPicRef *ref, AVFilterContext *filter,
72 int pmask)
73{
74 AVFilterPicPerms *pic_perms;
4dbbcdee
VS
75 AVFilterPicRef *ret = av_malloc(sizeof(AVFilterPicRef));
76 memcpy(ret, ref, sizeof(AVFilterPicRef));
efb36bfc 77 ret->perms &= pmask;
4dbbcdee 78 ret->pic->refcount ++;
a4af86c8
VS
79
80 if(filter) {
81 pic_perms = get_perms(ref->pic, filter);
82 pic_perms->perms |= ret->perms;
83 }
84
4dbbcdee
VS
85 return ret;
86}
87
88void avfilter_unref_pic(AVFilterPicRef *ref)
89{
a4af86c8
VS
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 }
4dbbcdee 97 ref->pic->free(ref->pic);
a4af86c8 98 }
4dbbcdee
VS
99 av_free(ref);
100}
101
24618441
VS
102void 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
4dbbcdee
VS
123int avfilter_link(AVFilterContext *src, unsigned srcpad,
124 AVFilterContext *dst, unsigned dstpad)
125{
126 AVFilterLink *link;
127
7d0e1392
VS
128 if(src->output_count <= srcpad || dst->input_count <= dstpad ||
129 src->outputs[srcpad] || dst->inputs[dstpad])
4dbbcdee
VS
130 return -1;
131
132 src->outputs[srcpad] =
c949d867 133 dst->inputs[dstpad] = link = av_mallocz(sizeof(AVFilterLink));
4dbbcdee 134
1653c11f
VS
135 link->src = src;
136 link->dst = dst;
137 link->srcpad = srcpad;
138 link->dstpad = dstpad;
01942f1d
VS
139 link->format = -1;
140
141 return 0;
142}
143
144int avfilter_config_link(AVFilterLink *link)
145{
146 int *fmts[2], i, j;
147 int (*config_link)(AVFilterLink *);
148
149 if(!link)
150 return 0;
4dbbcdee 151
e0752603 152 /* find a format both filters support - TODO: auto-insert conversion filter */
998a7aa3 153 link->format = -1;
6fb0648d
VS
154 if(link_spad(link).query_formats)
155 fmts[0] = link_spad(link).query_formats(link);
102fb0e3 156 else
78b0c0bb 157 fmts[0] = avfilter_default_query_output_formats(link);
6fb0648d 158 fmts[1] = link_dpad(link).query_formats(link);
e0752603
VS
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
166format_done:
167 av_free(fmts[0]);
168 av_free(fmts[1]);
01942f1d 169 if(link->format == -1)
e0752603 170 return -1;
e0752603 171
6fb0648d 172 if(!(config_link = link_spad(link).config_props))
01942f1d
VS
173 config_link = avfilter_default_config_output_link;
174 if(config_link(link))
175 return -1;
102fb0e3 176
6fb0648d 177 if(!(config_link = link_dpad(link).config_props))
01942f1d
VS
178 config_link = avfilter_default_config_input_link;
179 if(config_link(link))
180 return -1;
102fb0e3 181
4dbbcdee
VS
182 return 0;
183}
184
185AVFilterPicRef *avfilter_get_video_buffer(AVFilterLink *link, int perms)
186{
187 AVFilterPicRef *ret = NULL;
188
6fb0648d
VS
189 if(link_dpad(link).get_video_buffer)
190 ret = link_dpad(link).get_video_buffer(link, perms);
4dbbcdee
VS
191
192 if(!ret)
193 ret = avfilter_default_get_video_buffer(link, perms);
194
195 return ret;
196}
197
3306f8bd 198int avfilter_request_frame(AVFilterLink *link)
4dbbcdee 199{
6fb0648d
VS
200 if(link_spad(link).request_frame)
201 return link_spad(link).request_frame(link);
9586ba3a 202 else if(link->src->inputs[0])
3306f8bd
VS
203 return avfilter_request_frame(link->src->inputs[0]);
204 else return -1;
4dbbcdee
VS
205}
206
207/* XXX: should we do the duplicating of the picture ref here, instead of
208 * forcing the source filter to do it? */
209void avfilter_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
210{
a4af86c8 211 int perms = get_perms(picref->pic, link->dst)->perms;
4dbbcdee
VS
212 void (*start_frame)(AVFilterLink *, AVFilterPicRef *);
213
6fb0648d 214 start_frame = link_dpad(link).start_frame;
4dbbcdee
VS
215 if(!start_frame)
216 start_frame = avfilter_default_start_frame;
217
c949d867 218 /* prepare to copy the picture if it has insufficient permissions */
a4af86c8
VS
219 if((link_dpad(link).min_perms & perms) != link_dpad(link).min_perms ||
220 link_dpad(link).rej_perms & perms) {
c949d867
VS
221 av_log(link->dst, AV_LOG_INFO,
222 "frame copy needed (have perms %x, need %x, reject %x)\n",
a4af86c8 223 perms,
c949d867
VS
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);
4dbbcdee
VS
233}
234
235void avfilter_end_frame(AVFilterLink *link)
236{
237 void (*end_frame)(AVFilterLink *);
238
c949d867
VS
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
6fb0648d 246 end_frame = link_dpad(link).end_frame;
4dbbcdee
VS
247 if(!end_frame)
248 end_frame = avfilter_default_end_frame;
249
250 end_frame(link);
251}
252
ef35d13a 253void avfilter_draw_slice(AVFilterLink *link, int y, int h)
4dbbcdee 254{
c949d867
VS
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
6fb0648d 287 if(!link_dpad(link).draw_slice)
7d0e1392
VS
288 return;
289
6fb0648d 290 link_dpad(link).draw_slice(link, y, h);
4dbbcdee
VS
291}
292
293static 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
299AVFilter *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 */
312void 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
319void avfilter_init(void)
320{
d72a138e 321 avfilter_register(&vsrc_dummy);
eaf7eb66 322 avfilter_register(&vsrc_ppm);
d72a138e 323 avfilter_register(&vf_crop);
49d915cb 324 avfilter_register(&vf_fps);
d4169dd3 325 avfilter_register(&vf_graph);
f9285377
VS
326 avfilter_register(&vf_graphdesc);
327 avfilter_register(&vf_graphfile);
ef712a3b 328 avfilter_register(&vf_overlay);
d72a138e 329 avfilter_register(&vf_passthrough);
72e3037a 330 avfilter_register(&vf_rgb2bgr);
ce356b09 331 avfilter_register(&vf_slicify);
e2fcb3cb 332 avfilter_register(&vf_vflip);
4dbbcdee
VS
333}
334
335void avfilter_uninit(void)
336{
337 av_freep(&filters);
338 filter_count = 0;
339}
340
341static 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
350static const char *filter_name(void *p)
351{
352 AVFilterContext *filter = p;
353 return filter->filter->name;
354}
355
6ae82d1e 356AVFilterContext *avfilter_create(AVFilter *filter, char *inst_name)
4dbbcdee
VS
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;
2350e69c 363 ret->name = inst_name ? av_strdup(inst_name) : NULL;
4dbbcdee
VS
364 ret->priv = av_mallocz(filter->priv_size);
365
7d0e1392 366 ret->input_count = pad_count(filter->inputs);
c5ef7d7b
VS
367 ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->input_count);
368 memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad)*ret->input_count);
7d0e1392 369 ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
c5ef7d7b 370
7d0e1392 371 ret->output_count = pad_count(filter->outputs);
c5ef7d7b
VS
372 ret->output_pads = av_malloc(sizeof(AVFilterPad) * ret->output_count);
373 memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad)*ret->output_count);
7d0e1392
VS
374 ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
375
4dbbcdee
VS
376 return ret;
377}
378
379void avfilter_destroy(AVFilterContext *filter)
380{
381 int i;
382
383 if(filter->filter->uninit)
384 filter->filter->uninit(filter);
385
c5ef7d7b 386 for(i = 0; i < filter->input_count; i ++) {
4dbbcdee
VS
387 if(filter->inputs[i])
388 filter->inputs[i]->src->outputs[filter->inputs[i]->srcpad] = NULL;
389 av_free(filter->inputs[i]);
390 }
c5ef7d7b 391 for(i = 0; i < filter->output_count; i ++) {
4dbbcdee
VS
392 if(filter->outputs[i])
393 filter->outputs[i]->dst->inputs[filter->outputs[i]->dstpad] = NULL;
394 av_free(filter->outputs[i]);
395 }
396
2350e69c 397 av_free(filter->name);
c5ef7d7b
VS
398 av_free(filter->input_pads);
399 av_free(filter->output_pads);
4dbbcdee
VS
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
6ae82d1e 407AVFilterContext *avfilter_create_by_name(char *name, char *inst_name)
4dbbcdee
VS
408{
409 AVFilter *filt;
410
411 if(!(filt = avfilter_get_by_name(name))) return NULL;
6ae82d1e 412 return avfilter_create(filt, inst_name);
4dbbcdee
VS
413}
414
a360f71e 415int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
4dbbcdee 416{
c6b0aa68 417 int ret;
4dbbcdee
VS
418
419 if(filter->filter->init)
a360f71e 420 if((ret = filter->filter->init(filter, args, opaque))) return ret;
4dbbcdee
VS
421 return 0;
422}
423
e0752603
VS
424int *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