Add a slice_dir parameter to avfilter_draw_slice().
[libav.git] / 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 /* #define DEBUG */
23
24 #include "libavcodec/imgconvert.h"
25 #include "avfilter.h"
26
27 unsigned avfilter_version(void) {
28 return LIBAVFILTER_VERSION_INT;
29 }
30
31 const char * avfilter_configuration(void)
32 {
33 return FFMPEG_CONFIGURATION;
34 }
35
36 const char * avfilter_license(void)
37 {
38 #define LICENSE_PREFIX "libavfilter license: "
39 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
40 }
41
42 /** helper macros to get the in/out pad on the dst/src filter */
43 #define link_dpad(link) link->dst-> input_pads[link->dstpad]
44 #define link_spad(link) link->src->output_pads[link->srcpad]
45
46 AVFilterPicRef *avfilter_ref_pic(AVFilterPicRef *ref, int pmask)
47 {
48 AVFilterPicRef *ret = av_malloc(sizeof(AVFilterPicRef));
49 *ret = *ref;
50 ret->perms &= pmask;
51 ret->pic->refcount ++;
52 return ret;
53 }
54
55 void avfilter_unref_pic(AVFilterPicRef *ref)
56 {
57 if(!(--ref->pic->refcount))
58 ref->pic->free(ref->pic);
59 av_free(ref);
60 }
61
62 void avfilter_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
63 AVFilterPad **pads, AVFilterLink ***links,
64 AVFilterPad *newpad)
65 {
66 unsigned i;
67
68 idx = FFMIN(idx, *count);
69
70 *pads = av_realloc(*pads, sizeof(AVFilterPad) * (*count + 1));
71 *links = av_realloc(*links, sizeof(AVFilterLink*) * (*count + 1));
72 memmove(*pads +idx+1, *pads +idx, sizeof(AVFilterPad) * (*count-idx));
73 memmove(*links+idx+1, *links+idx, sizeof(AVFilterLink*) * (*count-idx));
74 memcpy(*pads+idx, newpad, sizeof(AVFilterPad));
75 (*links)[idx] = NULL;
76
77 (*count) ++;
78 for(i = idx+1; i < *count; i ++)
79 if(*links[i])
80 (*(unsigned *)((uint8_t *) *links[i] + padidx_off)) ++;
81 }
82
83 int avfilter_link(AVFilterContext *src, unsigned srcpad,
84 AVFilterContext *dst, unsigned dstpad)
85 {
86 AVFilterLink *link;
87
88 if(src->output_count <= srcpad || dst->input_count <= dstpad ||
89 src->outputs[srcpad] || dst->inputs[dstpad])
90 return -1;
91
92 src->outputs[srcpad] =
93 dst-> inputs[dstpad] = link = av_mallocz(sizeof(AVFilterLink));
94
95 link->src = src;
96 link->dst = dst;
97 link->srcpad = srcpad;
98 link->dstpad = dstpad;
99 link->format = PIX_FMT_NONE;
100
101 return 0;
102 }
103
104 int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
105 unsigned in, unsigned out)
106 {
107 av_log(link->dst, AV_LOG_INFO, "auto-inserting filter '%s'\n",
108 filt->filter->name);
109
110 link->dst->inputs[link->dstpad] = NULL;
111 if(avfilter_link(filt, out, link->dst, link->dstpad)) {
112 /* failed to link output filter to new filter */
113 link->dst->inputs[link->dstpad] = link;
114 return -1;
115 }
116
117 /* re-hookup the link to the new destination filter we inserted */
118 link->dst = filt;
119 link->dstpad = in;
120 filt->inputs[in] = link;
121
122 /* if any information on supported colorspaces already exists on the
123 * link, we need to preserve that */
124 if(link->out_formats)
125 avfilter_formats_changeref(&link->out_formats,
126 &filt->outputs[out]->out_formats);
127
128 return 0;
129 }
130
131 int avfilter_config_links(AVFilterContext *filter)
132 {
133 int (*config_link)(AVFilterLink *);
134 unsigned i;
135
136 for(i = 0; i < filter->input_count; i ++) {
137 AVFilterLink *link = filter->inputs[i];
138
139 if(!link) continue;
140
141 switch(link->init_state) {
142 case AVLINK_INIT:
143 continue;
144 case AVLINK_STARTINIT:
145 av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
146 return 0;
147 case AVLINK_UNINIT:
148 link->init_state = AVLINK_STARTINIT;
149
150 if(avfilter_config_links(link->src))
151 return -1;
152
153 if(!(config_link = link_spad(link).config_props))
154 config_link = avfilter_default_config_output_link;
155 if(config_link(link))
156 return -1;
157
158 if((config_link = link_dpad(link).config_props))
159 if(config_link(link))
160 return -1;
161
162 link->init_state = AVLINK_INIT;
163 }
164 }
165
166 return 0;
167 }
168
169 static void dprintf_picref(void *ctx, AVFilterPicRef *picref, int end)
170 {
171 dprintf(ctx,
172 "picref[%p data[%p, %p, %p, %p] linesize[%d, %d, %d, %d] pts:%"PRId64" s:%dx%d]%s",
173 picref,
174 picref->data [0], picref->data [1], picref->data [2], picref->data [3],
175 picref->linesize[0], picref->linesize[1], picref->linesize[2], picref->linesize[3],
176 picref->pts, picref->w, picref->h,
177 end ? "\n" : "");
178 }
179
180 static void dprintf_link(void *ctx, AVFilterLink *link, int end)
181 {
182 dprintf(ctx,
183 "link[%p s:%dx%d fmt:%-16s %-16s->%-16s]%s",
184 link, link->w, link->h,
185 avcodec_get_pix_fmt_name(link->format),
186 link->src ? link->src->filter->name : "",
187 link->dst ? link->dst->filter->name : "",
188 end ? "\n" : "");
189 }
190
191 #define DPRINTF_START(ctx, func) dprintf(NULL, "%-16s: ", #func)
192
193 AVFilterPicRef *avfilter_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
194 {
195 AVFilterPicRef *ret = NULL;
196
197 DPRINTF_START(NULL, get_video_buffer); dprintf_link(NULL, link, 0); dprintf(NULL, " perms:%d w:%d h:%d\n", perms, w, h);
198
199 if(link_dpad(link).get_video_buffer)
200 ret = link_dpad(link).get_video_buffer(link, perms, w, h);
201
202 if(!ret)
203 ret = avfilter_default_get_video_buffer(link, perms, w, h);
204
205 DPRINTF_START(NULL, get_video_buffer); dprintf_link(NULL, link, 0); dprintf(NULL, " returning "); dprintf_picref(NULL, ret, 1);
206
207 return ret;
208 }
209
210 int avfilter_request_frame(AVFilterLink *link)
211 {
212 DPRINTF_START(NULL, request_frame); dprintf_link(NULL, link, 1);
213
214 if(link_spad(link).request_frame)
215 return link_spad(link).request_frame(link);
216 else if(link->src->inputs[0])
217 return avfilter_request_frame(link->src->inputs[0]);
218 else return -1;
219 }
220
221 int avfilter_poll_frame(AVFilterLink *link)
222 {
223 int i, min=INT_MAX;
224
225 if(link_spad(link).poll_frame)
226 return link_spad(link).poll_frame(link);
227
228 for (i=0; i<link->src->input_count; i++) {
229 int val;
230 if(!link->src->inputs[i])
231 return -1;
232 val = avfilter_poll_frame(link->src->inputs[i]);
233 min = FFMIN(min, val);
234 }
235
236 return min;
237 }
238
239 /* XXX: should we do the duplicating of the picture ref here, instead of
240 * forcing the source filter to do it? */
241 void avfilter_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
242 {
243 void (*start_frame)(AVFilterLink *, AVFilterPicRef *);
244 AVFilterPad *dst = &link_dpad(link);
245
246 DPRINTF_START(NULL, start_frame); dprintf_link(NULL, link, 0); dprintf(NULL, " "); dprintf_picref(NULL, picref, 1);
247
248 if(!(start_frame = dst->start_frame))
249 start_frame = avfilter_default_start_frame;
250
251 /* prepare to copy the picture if it has insufficient permissions */
252 if((dst->min_perms & picref->perms) != dst->min_perms ||
253 dst->rej_perms & picref->perms) {
254 /*
255 av_log(link->dst, AV_LOG_INFO,
256 "frame copy needed (have perms %x, need %x, reject %x)\n",
257 picref->perms,
258 link_dpad(link).min_perms, link_dpad(link).rej_perms);
259 */
260
261 link->cur_pic = avfilter_default_get_video_buffer(link, dst->min_perms, link->w, link->h);
262 link->srcpic = picref;
263 link->cur_pic->pts = link->srcpic->pts;
264 link->cur_pic->pixel_aspect = link->srcpic->pixel_aspect;
265 }
266 else
267 link->cur_pic = picref;
268
269 start_frame(link, link->cur_pic);
270 }
271
272 void avfilter_end_frame(AVFilterLink *link)
273 {
274 void (*end_frame)(AVFilterLink *);
275
276 if(!(end_frame = link_dpad(link).end_frame))
277 end_frame = avfilter_default_end_frame;
278
279 end_frame(link);
280
281 /* unreference the source picture if we're feeding the destination filter
282 * a copied version dues to permission issues */
283 if(link->srcpic) {
284 avfilter_unref_pic(link->srcpic);
285 link->srcpic = NULL;
286 }
287
288 }
289
290 void avfilter_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
291 {
292 uint8_t *src[4], *dst[4];
293 int i, j, hsub, vsub;
294 void (*draw_slice)(AVFilterLink *, int, int, int);
295
296 DPRINTF_START(NULL, draw_slice); dprintf_link(NULL, link, 0); dprintf(NULL, " y:%d h:%d dir:%d\n", y, h, slice_dir);
297
298 /* copy the slice if needed for permission reasons */
299 if(link->srcpic) {
300 avcodec_get_chroma_sub_sample(link->format, &hsub, &vsub);
301
302 for(i = 0; i < 4; i ++) {
303 if(link->srcpic->data[i]) {
304 src[i] = link->srcpic-> data[i] +
305 (y >> (i==0 ? 0 : vsub)) * link->srcpic-> linesize[i];
306 dst[i] = link->cur_pic->data[i] +
307 (y >> (i==0 ? 0 : vsub)) * link->cur_pic->linesize[i];
308 } else
309 src[i] = dst[i] = NULL;
310 }
311
312 for(i = 0; i < 4; i ++) {
313 int planew =
314 ff_get_plane_bytewidth(link->format, link->cur_pic->w, i);
315
316 if(!src[i]) continue;
317
318 for(j = 0; j < h >> (i==0 ? 0 : vsub); j ++) {
319 memcpy(dst[i], src[i], planew);
320 src[i] += link->srcpic ->linesize[i];
321 dst[i] += link->cur_pic->linesize[i];
322 }
323 }
324 }
325
326 if(!(draw_slice = link_dpad(link).draw_slice))
327 draw_slice = avfilter_default_draw_slice;
328 draw_slice(link, y, h, slice_dir);
329 }
330
331 #define MAX_REGISTERED_AVFILTERS_NB 64
332
333 static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1];
334
335 static int next_registered_avfilter_idx = 0;
336
337 AVFilter *avfilter_get_by_name(const char *name)
338 {
339 int i;
340
341 for (i = 0; registered_avfilters[i]; i++)
342 if (!strcmp(registered_avfilters[i]->name, name))
343 return registered_avfilters[i];
344
345 return NULL;
346 }
347
348 int avfilter_register(AVFilter *filter)
349 {
350 if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB)
351 return -1;
352
353 registered_avfilters[next_registered_avfilter_idx++] = filter;
354 return 0;
355 }
356
357 AVFilter **av_filter_next(AVFilter **filter)
358 {
359 return filter ? ++filter : &registered_avfilters[0];
360 }
361
362 void avfilter_uninit(void)
363 {
364 memset(registered_avfilters, 0, sizeof(registered_avfilters));
365 next_registered_avfilter_idx = 0;
366 }
367
368 static int pad_count(const AVFilterPad *pads)
369 {
370 int count;
371
372 for(count = 0; pads->name; count ++) pads ++;
373 return count;
374 }
375
376 static const char *filter_name(void *p)
377 {
378 AVFilterContext *filter = p;
379 return filter->filter->name;
380 }
381
382 static const AVClass avfilter_class = {
383 "AVFilter",
384 filter_name
385 };
386
387 AVFilterContext *avfilter_open(AVFilter *filter, const char *inst_name)
388 {
389 AVFilterContext *ret;
390
391 if (!filter)
392 return 0;
393
394 ret = av_mallocz(sizeof(AVFilterContext));
395
396 ret->av_class = &avfilter_class;
397 ret->filter = filter;
398 ret->name = inst_name ? av_strdup(inst_name) : NULL;
399 ret->priv = av_mallocz(filter->priv_size);
400
401 ret->input_count = pad_count(filter->inputs);
402 if (ret->input_count) {
403 ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->input_count);
404 memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->input_count);
405 ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
406 }
407
408 ret->output_count = pad_count(filter->outputs);
409 if (ret->output_count) {
410 ret->output_pads = av_malloc(sizeof(AVFilterPad) * ret->output_count);
411 memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->output_count);
412 ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
413 }
414
415 return ret;
416 }
417
418 void avfilter_destroy(AVFilterContext *filter)
419 {
420 int i;
421
422 if(filter->filter->uninit)
423 filter->filter->uninit(filter);
424
425 for(i = 0; i < filter->input_count; i ++) {
426 if(filter->inputs[i])
427 filter->inputs[i]->src->outputs[filter->inputs[i]->srcpad] = NULL;
428 av_freep(&filter->inputs[i]);
429 }
430 for(i = 0; i < filter->output_count; i ++) {
431 if(filter->outputs[i])
432 filter->outputs[i]->dst->inputs[filter->outputs[i]->dstpad] = NULL;
433 av_freep(&filter->outputs[i]);
434 }
435
436 av_freep(&filter->name);
437 av_freep(&filter->input_pads);
438 av_freep(&filter->output_pads);
439 av_freep(&filter->inputs);
440 av_freep(&filter->outputs);
441 av_freep(&filter->priv);
442 av_free(filter);
443 }
444
445 int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
446 {
447 int ret=0;
448
449 if(filter->filter->init)
450 ret = filter->filter->init(filter, args, opaque);
451 return ret;
452 }
453