OSDN Git Service

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