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