OSDN Git Service

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