Actually fail when colorspaces don't match
[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
34/* TODO: buffer pool. see comment for avfilter_default_get_video_buffer() */
35void avfilter_default_free_video_buffer(AVFilterPic *pic)
36{
37 avpicture_free((AVPicture *) pic);
38 av_free(pic);
39}
40
41/* TODO: set the buffer's priv member to a context structure for the whole
42 * filter chain. This will allow for a buffer pool instead of the constant
43 * alloc & free cycle currently implemented. */
44AVFilterPicRef *avfilter_default_get_video_buffer(AVFilterLink *link, int perms)
45{
46 AVFilterPic *pic = av_mallocz(sizeof(AVFilterPic));
47 AVFilterPicRef *ref = av_mallocz(sizeof(AVFilterPicRef));
48
49 ref->pic = pic;
50 ref->w = link->w;
51 ref->h = link->h;
52 ref->perms = perms;
53
54 pic->refcount = 1;
55 pic->format = link->format;
56 pic->free = avfilter_default_free_video_buffer;
57 avpicture_alloc((AVPicture *)pic, pic->format, ref->w, ref->h);
58
92beffdc 59 memcpy(ref->data, pic->data, sizeof(pic->data));
a4ca7389 60 memcpy(ref->linesize, pic->linesize, sizeof(pic->linesize));
4dbbcdee
VS
61
62 return ref;
63}
64
65void avfilter_default_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
66{
3628fbe0
VS
67 AVFilterLink *out = link->dst->outputs[0];
68
4dbbcdee 69 link->cur_pic = picref;
3628fbe0
VS
70
71 if(out) {
72 out->outpic = avfilter_get_video_buffer(out, AV_PERM_WRITE);
73 avfilter_start_frame(out, avfilter_ref_pic(out->outpic, ~0));
74 }
4dbbcdee
VS
75}
76
77void avfilter_default_end_frame(AVFilterLink *link)
78{
3628fbe0
VS
79 AVFilterLink *out = link->dst->outputs[0];
80
4dbbcdee
VS
81 avfilter_unref_pic(link->cur_pic);
82 link->cur_pic = NULL;
3628fbe0
VS
83
84 if(out) {
85 avfilter_unref_pic(out->outpic);
86 out->outpic = NULL;
87 avfilter_end_frame(out);
88 }
4dbbcdee
VS
89}
90
efb36bfc 91AVFilterPicRef *avfilter_ref_pic(AVFilterPicRef *ref, int pmask)
4dbbcdee
VS
92{
93 AVFilterPicRef *ret = av_malloc(sizeof(AVFilterPicRef));
94 memcpy(ret, ref, sizeof(AVFilterPicRef));
efb36bfc 95 ret->perms &= pmask;
4dbbcdee
VS
96 ret->pic->refcount ++;
97 return ret;
98}
99
100void avfilter_unref_pic(AVFilterPicRef *ref)
101{
102 if(-- ref->pic->refcount == 0)
103 ref->pic->free(ref->pic);
104 av_free(ref);
105}
106
102fb0e3
VS
107/**
108 * default config_link() implementation for output video links to simplify
109 * the implementation of one input one output video filters */
110static int default_config_output_link(AVFilterLink *link)
111{
112 link->w = link->src->inputs[0]->w;
113 link->h = link->src->inputs[0]->h;
114
115 return 0;
116}
117
118/**
119 * default query_formats() implementation for output video links to simplify
120 * the implementation of one input one output video filters */
121static int *default_query_output_formats(AVFilterLink *link)
122{
123 return avfilter_make_format_list(1, link->src->inputs[0]->format);
124}
125
4dbbcdee
VS
126int avfilter_link(AVFilterContext *src, unsigned srcpad,
127 AVFilterContext *dst, unsigned dstpad)
128{
129 AVFilterLink *link;
e0752603 130 int *fmts[2], i, j;
4dbbcdee
VS
131
132 if(src->outputs[srcpad] || dst->inputs[dstpad])
133 return -1;
134
135 src->outputs[srcpad] =
136 dst->inputs[dstpad] = link = av_malloc(sizeof(AVFilterLink));
137
138 link->src = src;
139 link->dst = dst;
140 link->srcpad = srcpad;
141 link->dstpad = dstpad;
142 link->cur_pic = NULL;
143
e0752603 144 /* find a format both filters support - TODO: auto-insert conversion filter */
998a7aa3 145 link->format = -1;
102fb0e3
VS
146 if(src->filter->outputs[srcpad].query_formats)
147 fmts[0] = src->filter->outputs[srcpad].query_formats(link);
148 else
149 fmts[0] = default_query_output_formats(link);
ba6b9035 150 fmts[1] = dst->filter-> inputs[dstpad].query_formats(link);
e0752603
VS
151 for(i = 0; fmts[0][i] != -1; i ++)
152 for(j = 0; fmts[1][j] != -1; j ++)
153 if(fmts[0][i] == fmts[1][j]) {
154 link->format = fmts[0][i];
155 goto format_done;
156 }
157
158format_done:
159 av_free(fmts[0]);
160 av_free(fmts[1]);
161 if(link->format == -1) {
162 /* failed to find a format. fail at creating the link */
163 av_free(link);
164 src->outputs[srcpad] = NULL;
165 dst->inputs[dstpad] = NULL;
166 return -1;
167 }
168
102fb0e3
VS
169 if (src->filter->outputs[srcpad].config_props)
170 src->filter->outputs[srcpad].config_props(link);
171 else
172 default_config_output_link(link);
173
174 if (dst->filter->inputs[dstpad].config_props)
175 dst->filter->inputs[dstpad].config_props(link);
176
4dbbcdee
VS
177 return 0;
178}
179
180AVFilterPicRef *avfilter_get_video_buffer(AVFilterLink *link, int perms)
181{
182 AVFilterPicRef *ret = NULL;
183
184 if(link->dst->filter->inputs[link->dstpad].get_video_buffer)
185 ret = link->dst->filter->inputs[link->dstpad].get_video_buffer(link, perms);
186
187 if(!ret)
188 ret = avfilter_default_get_video_buffer(link, perms);
189
190 return ret;
191}
192
193void avfilter_request_frame(AVFilterLink *link)
194{
9586ba3a
VS
195 const AVFilterPad *pad = &link->src->filter->outputs[link->srcpad];
196
197 if(pad->request_frame)
198 pad->request_frame(link);
199 else if(link->src->inputs[0])
200 avfilter_request_frame(link->src->inputs[0]);
4dbbcdee
VS
201}
202
203/* XXX: should we do the duplicating of the picture ref here, instead of
204 * forcing the source filter to do it? */
205void avfilter_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
206{
207 void (*start_frame)(AVFilterLink *, AVFilterPicRef *);
208
209 start_frame = link->dst->filter->inputs[link->dstpad].start_frame;
210 if(!start_frame)
211 start_frame = avfilter_default_start_frame;
212
213 start_frame(link, picref);
214}
215
216void avfilter_end_frame(AVFilterLink *link)
217{
218 void (*end_frame)(AVFilterLink *);
219
220 end_frame = link->dst->filter->inputs[link->dstpad].end_frame;
221 if(!end_frame)
222 end_frame = avfilter_default_end_frame;
223
224 end_frame(link);
225}
226
227void avfilter_draw_slice(AVFilterLink *link, uint8_t *data[4], int y, int h)
228{
229 link->dst->filter->inputs[link->dstpad].draw_slice(link, data, y, h);
230}
231
232static int filter_cmp(const void *aa, const void *bb)
233{
234 const AVFilter *a = *(const AVFilter **)aa, *b = *(const AVFilter **)bb;
235 return strcmp(a->name, b->name);
236}
237
238AVFilter *avfilter_get_by_name(char *name)
239{
240 AVFilter key = { .name = name, };
241 AVFilter *key2 = &key;
242 AVFilter **ret;
243
244 ret = bsearch(&key2, filters, filter_count, sizeof(AVFilter **), filter_cmp);
245 if(ret)
246 return *ret;
247 return NULL;
248}
249
250/* FIXME: insert in order, rather than insert at end + resort */
251void avfilter_register(AVFilter *filter)
252{
253 filters = av_realloc(filters, sizeof(AVFilter*) * (filter_count+1));
254 filters[filter_count] = filter;
255 qsort(filters, ++filter_count, sizeof(AVFilter **), filter_cmp);
256}
257
258void avfilter_init(void)
259{
d72a138e 260 avfilter_register(&vsrc_dummy);
eaf7eb66 261 avfilter_register(&vsrc_ppm);
d72a138e
VS
262 avfilter_register(&vf_crop);
263 avfilter_register(&vf_passthrough);
72e3037a 264 avfilter_register(&vf_rgb2bgr);
ce356b09 265 avfilter_register(&vf_slicify);
d72a138e 266 avfilter_register(&vo_sdl);
4dbbcdee
VS
267}
268
269void avfilter_uninit(void)
270{
271 av_freep(&filters);
272 filter_count = 0;
273}
274
275static int pad_count(const AVFilterPad *pads)
276{
277 AVFilterPad *p = (AVFilterPad *) pads;
278 int count;
279
280 for(count = 0; p->name; count ++) p ++;
281 return count;
282}
283
284static const char *filter_name(void *p)
285{
286 AVFilterContext *filter = p;
287 return filter->filter->name;
288}
289
290AVFilterContext *avfilter_create(AVFilter *filter)
291{
292 AVFilterContext *ret = av_malloc(sizeof(AVFilterContext));
293
294 ret->av_class = av_mallocz(sizeof(AVClass));
295 ret->av_class->item_name = filter_name;
296 ret->filter = filter;
297 ret->inputs = av_mallocz(sizeof(AVFilterLink*) * pad_count(filter->inputs));
298 ret->outputs = av_mallocz(sizeof(AVFilterLink*) * pad_count(filter->outputs));
299 ret->priv = av_mallocz(filter->priv_size);
300
301 return ret;
302}
303
304void avfilter_destroy(AVFilterContext *filter)
305{
306 int i;
307
308 if(filter->filter->uninit)
309 filter->filter->uninit(filter);
310
311 for(i = 0; i < pad_count(filter->filter->inputs); i ++) {
312 if(filter->inputs[i])
313 filter->inputs[i]->src->outputs[filter->inputs[i]->srcpad] = NULL;
314 av_free(filter->inputs[i]);
315 }
316 for(i = 0; i < pad_count(filter->filter->outputs); i ++) {
317 if(filter->outputs[i])
318 filter->outputs[i]->dst->inputs[filter->outputs[i]->dstpad] = NULL;
319 av_free(filter->outputs[i]);
320 }
321
322 av_free(filter->inputs);
323 av_free(filter->outputs);
324 av_free(filter->priv);
325 av_free(filter->av_class);
326 av_free(filter);
327}
328
329AVFilterContext *avfilter_create_by_name(char *name)
330{
331 AVFilter *filt;
332
333 if(!(filt = avfilter_get_by_name(name))) return NULL;
334 return avfilter_create(filt);
335}
336
89e64908 337int avfilter_init_filter(AVFilterContext *filter, const char *args)
4dbbcdee
VS
338{
339 int ret, i;
340
341 if(filter->filter->init)
89e64908 342 if((ret = filter->filter->init(filter, args))) return ret;
4dbbcdee
VS
343 return 0;
344}
345
e0752603
VS
346int *avfilter_make_format_list(int len, ...)
347{
348 int *ret, i;
349 va_list vl;
350
351 ret = av_malloc(sizeof(int) * (len + 1));
352 va_start(vl, len);
353 for(i = 0; i < len; i ++)
354 ret[i] = va_arg(vl, int);
355 va_end(vl);
356 ret[len] = -1;
357
358 return ret;
359}
360