]> git.sesse.net Git - ffmpeg/blob - libavfilter/avfilter.c
Merge remote-tracking branch 'qatar/master'
[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 "libavutil/pixdesc.h"
25 #include "libavutil/rational.h"
26 #include "libavutil/audioconvert.h"
27 #include "libavutil/imgutils.h"
28 #include "libavutil/avassert.h"
29 #include "avfilter.h"
30 #include "internal.h"
31
32 unsigned avfilter_version(void) {
33     return LIBAVFILTER_VERSION_INT;
34 }
35
36 const char *avfilter_configuration(void)
37 {
38     return FFMPEG_CONFIGURATION;
39 }
40
41 const char *avfilter_license(void)
42 {
43 #define LICENSE_PREFIX "libavfilter license: "
44     return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
45 }
46
47 AVFilterBufferRef *avfilter_ref_buffer(AVFilterBufferRef *ref, int pmask)
48 {
49     AVFilterBufferRef *ret = av_malloc(sizeof(AVFilterBufferRef));
50     if (!ret)
51         return NULL;
52     *ret = *ref;
53     if (ref->type == AVMEDIA_TYPE_VIDEO) {
54         ret->video = av_malloc(sizeof(AVFilterBufferRefVideoProps));
55         if (!ret->video) {
56             av_free(ret);
57             return NULL;
58         }
59         *ret->video = *ref->video;
60     } else if (ref->type == AVMEDIA_TYPE_AUDIO) {
61         ret->audio = av_malloc(sizeof(AVFilterBufferRefAudioProps));
62         if (!ret->audio) {
63             av_free(ret);
64             return NULL;
65         }
66         *ret->audio = *ref->audio;
67     }
68     ret->perms &= pmask;
69     ret->buf->refcount ++;
70     return ret;
71 }
72
73 static void store_in_pool(AVFilterBufferRef *ref)
74 {
75     int i;
76     AVFilterPool *pool= ref->buf->priv;
77
78     av_assert0(ref->buf->data[0]);
79
80     if (pool->count == POOL_SIZE) {
81         AVFilterBufferRef *ref1 = pool->pic[0];
82         av_freep(&ref1->video);
83         av_freep(&ref1->audio);
84         av_freep(&ref1->buf->data[0]);
85         av_freep(&ref1->buf);
86         av_free(ref1);
87         memmove(&pool->pic[0], &pool->pic[1], sizeof(void*)*(POOL_SIZE-1));
88         pool->count--;
89         pool->pic[POOL_SIZE-1] = NULL;
90     }
91
92     for (i = 0; i < POOL_SIZE; i++) {
93         if (!pool->pic[i]) {
94             pool->pic[i] = ref;
95             pool->count++;
96             break;
97         }
98     }
99 }
100
101 void avfilter_unref_buffer(AVFilterBufferRef *ref)
102 {
103     if (!ref)
104         return;
105     if (!(--ref->buf->refcount)) {
106         if (!ref->buf->free) {
107             store_in_pool(ref);
108             return;
109         }
110         ref->buf->free(ref->buf);
111     }
112     av_freep(&ref->video);
113     av_freep(&ref->audio);
114     av_free(ref);
115 }
116
117 void avfilter_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
118                          AVFilterPad **pads, AVFilterLink ***links,
119                          AVFilterPad *newpad)
120 {
121     unsigned i;
122
123     idx = FFMIN(idx, *count);
124
125     *pads  = av_realloc(*pads,  sizeof(AVFilterPad)   * (*count + 1));
126     *links = av_realloc(*links, sizeof(AVFilterLink*) * (*count + 1));
127     memmove(*pads +idx+1, *pads +idx, sizeof(AVFilterPad)   * (*count-idx));
128     memmove(*links+idx+1, *links+idx, sizeof(AVFilterLink*) * (*count-idx));
129     memcpy(*pads+idx, newpad, sizeof(AVFilterPad));
130     (*links)[idx] = NULL;
131
132     (*count)++;
133     for (i = idx+1; i < *count; i++)
134         if (*links[i])
135             (*(unsigned *)((uint8_t *) *links[i] + padidx_off))++;
136 }
137
138 int avfilter_link(AVFilterContext *src, unsigned srcpad,
139                   AVFilterContext *dst, unsigned dstpad)
140 {
141     AVFilterLink *link;
142
143     if (src->output_count <= srcpad || dst->input_count <= dstpad ||
144         src->outputs[srcpad]        || dst->inputs[dstpad])
145         return -1;
146
147     if (src->output_pads[srcpad].type != dst->input_pads[dstpad].type) {
148         av_log(src, AV_LOG_ERROR,
149                "Media type mismatch between the '%s' filter output pad %d and the '%s' filter input pad %d\n",
150                src->name, srcpad, dst->name, dstpad);
151         return AVERROR(EINVAL);
152     }
153
154     src->outputs[srcpad] =
155     dst-> inputs[dstpad] = link = av_mallocz(sizeof(AVFilterLink));
156
157     link->src     = src;
158     link->dst     = dst;
159     link->srcpad  = &src->output_pads[srcpad];
160     link->dstpad  = &dst->input_pads[dstpad];
161     link->type    = src->output_pads[srcpad].type;
162     assert(PIX_FMT_NONE == -1 && AV_SAMPLE_FMT_NONE == -1);
163     link->format  = -1;
164
165     return 0;
166 }
167
168 void avfilter_link_free(AVFilterLink **link)
169 {
170     if (!*link)
171         return;
172
173     if ((*link)->pool) {
174         int i;
175         for (i = 0; i < POOL_SIZE; i++) {
176             if ((*link)->pool->pic[i]) {
177                 AVFilterBufferRef *picref = (*link)->pool->pic[i];
178                 /* free buffer: picrefs stored in the pool are not
179                  * supposed to contain a free callback */
180                 av_freep(&picref->buf->data[0]);
181                 av_freep(&picref->buf);
182
183                 av_freep(&picref->audio);
184                 av_freep(&picref->video);
185                 av_freep(&picref);
186             }
187         }
188         av_freep(&(*link)->pool);
189     }
190     av_freep(link);
191 }
192
193 int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
194                            unsigned filt_srcpad_idx, unsigned filt_dstpad_idx)
195 {
196     int ret;
197     unsigned dstpad_idx = link->dstpad - link->dst->input_pads;
198
199     av_log(link->dst, AV_LOG_INFO, "auto-inserting filter '%s' "
200            "between the filter '%s' and the filter '%s'\n",
201            filt->name, link->src->name, link->dst->name);
202
203     link->dst->inputs[dstpad_idx] = NULL;
204     if ((ret = avfilter_link(filt, filt_dstpad_idx, link->dst, dstpad_idx)) < 0) {
205         /* failed to link output filter to new filter */
206         link->dst->inputs[dstpad_idx] = link;
207         return ret;
208     }
209
210     /* re-hookup the link to the new destination filter we inserted */
211     link->dst = filt;
212     link->dstpad = &filt->input_pads[filt_srcpad_idx];
213     filt->inputs[filt_srcpad_idx] = link;
214
215     /* if any information on supported media formats already exists on the
216      * link, we need to preserve that */
217     if (link->out_formats)
218         avfilter_formats_changeref(&link->out_formats,
219                                    &filt->outputs[filt_dstpad_idx]->out_formats);
220
221     return 0;
222 }
223
224 int avfilter_config_links(AVFilterContext *filter)
225 {
226     int (*config_link)(AVFilterLink *);
227     unsigned i;
228     int ret;
229
230     for (i = 0; i < filter->input_count; i ++) {
231         AVFilterLink *link = filter->inputs[i];
232
233         if (!link) continue;
234
235         switch (link->init_state) {
236         case AVLINK_INIT:
237             continue;
238         case AVLINK_STARTINIT:
239             av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
240             return 0;
241         case AVLINK_UNINIT:
242             link->init_state = AVLINK_STARTINIT;
243
244             if ((ret = avfilter_config_links(link->src)) < 0)
245                 return ret;
246
247             if (!(config_link = link->srcpad->config_props))
248                 config_link  = avfilter_default_config_output_link;
249             if ((ret = config_link(link)) < 0)
250                 return ret;
251
252             if (link->time_base.num == 0 && link->time_base.den == 0)
253                 link->time_base = link->src && link->src->input_count ?
254                     link->src->inputs[0]->time_base : AV_TIME_BASE_Q;
255
256             if (link->sample_aspect_ratio.num == 0 && link->sample_aspect_ratio.den == 0)
257                 link->sample_aspect_ratio = link->src->input_count ?
258                     link->src->inputs[0]->sample_aspect_ratio : (AVRational){1,1};
259
260             if (link->sample_rate == 0 && link->src && link->src->input_count)
261                 link->sample_rate = link->src->inputs[0]->sample_rate;
262
263             if (link->channel_layout == 0 && link->src && link->src->input_count)
264                 link->channel_layout = link->src->inputs[0]->channel_layout;
265
266             if ((config_link = link->dstpad->config_props))
267                 if ((ret = config_link(link)) < 0)
268                     return ret;
269
270             link->init_state = AVLINK_INIT;
271         }
272     }
273
274     return 0;
275 }
276
277 static char *ff_get_ref_perms_string(char *buf, size_t buf_size, int perms)
278 {
279     snprintf(buf, buf_size, "%s%s%s%s%s%s",
280              perms & AV_PERM_READ      ? "r" : "",
281              perms & AV_PERM_WRITE     ? "w" : "",
282              perms & AV_PERM_PRESERVE  ? "p" : "",
283              perms & AV_PERM_REUSE     ? "u" : "",
284              perms & AV_PERM_REUSE2    ? "U" : "",
285              perms & AV_PERM_NEG_LINESIZES ? "n" : "");
286     return buf;
287 }
288
289 static void ff_dlog_ref(void *ctx, AVFilterBufferRef *ref, int end)
290 {
291     av_unused char buf[16];
292     av_dlog(ctx,
293             "ref[%p buf:%p refcount:%d perms:%s data:%p linesize[%d, %d, %d, %d] pts:%"PRId64" pos:%"PRId64,
294             ref, ref->buf, ref->buf->refcount, ff_get_ref_perms_string(buf, sizeof(buf), ref->perms), ref->data[0],
295             ref->linesize[0], ref->linesize[1], ref->linesize[2], ref->linesize[3],
296             ref->pts, ref->pos);
297
298     if (ref->video) {
299         av_dlog(ctx, " a:%d/%d s:%dx%d i:%c iskey:%d type:%c",
300                 ref->video->sample_aspect_ratio.num, ref->video->sample_aspect_ratio.den,
301                 ref->video->w, ref->video->h,
302                 !ref->video->interlaced     ? 'P' :         /* Progressive  */
303                 ref->video->top_field_first ? 'T' : 'B',    /* Top / Bottom */
304                 ref->video->key_frame,
305                 av_get_picture_type_char(ref->video->pict_type));
306     }
307     if (ref->audio) {
308         av_dlog(ctx, " cl:%"PRId64"d n:%d r:%d p:%d",
309                 ref->audio->channel_layout,
310                 ref->audio->nb_samples,
311                 ref->audio->sample_rate,
312                 ref->audio->planar);
313     }
314
315     av_dlog(ctx, "]%s", end ? "\n" : "");
316 }
317
318 static void ff_dlog_link(void *ctx, AVFilterLink *link, int end)
319 {
320     if (link->type == AVMEDIA_TYPE_VIDEO) {
321         av_dlog(ctx,
322                 "link[%p s:%dx%d fmt:%-16s %-16s->%-16s]%s",
323                 link, link->w, link->h,
324                 av_pix_fmt_descriptors[link->format].name,
325                 link->src ? link->src->filter->name : "",
326                 link->dst ? link->dst->filter->name : "",
327                 end ? "\n" : "");
328     } else {
329         char buf[128];
330         av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout);
331
332         av_dlog(ctx,
333                 "link[%p r:%"PRId64" cl:%s fmt:%-16s %-16s->%-16s]%s",
334                 link, link->sample_rate, buf,
335                 av_get_sample_fmt_name(link->format),
336                 link->src ? link->src->filter->name : "",
337                 link->dst ? link->dst->filter->name : "",
338                 end ? "\n" : "");
339     }
340 }
341
342 #define FF_DPRINTF_START(ctx, func) av_dlog(NULL, "%-16s: ", #func)
343
344 AVFilterBufferRef *avfilter_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
345 {
346     AVFilterBufferRef *ret = NULL;
347
348     av_unused char buf[16];
349     FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0);
350     av_dlog(NULL, " perms:%s w:%d h:%d\n", ff_get_ref_perms_string(buf, sizeof(buf), perms), w, h);
351
352     if (link->dstpad->get_video_buffer)
353         ret = link->dstpad->get_video_buffer(link, perms, w, h);
354
355     if (!ret)
356         ret = avfilter_default_get_video_buffer(link, perms, w, h);
357
358     if (ret)
359         ret->type = AVMEDIA_TYPE_VIDEO;
360
361     FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " returning "); ff_dlog_ref(NULL, ret, 1);
362
363     return ret;
364 }
365
366 AVFilterBufferRef *
367 avfilter_get_video_buffer_ref_from_arrays(uint8_t * const data[4], const int linesize[4], int perms,
368                                           int w, int h, enum PixelFormat format)
369 {
370     AVFilterBuffer *pic = av_mallocz(sizeof(AVFilterBuffer));
371     AVFilterBufferRef *picref = av_mallocz(sizeof(AVFilterBufferRef));
372
373     if (!pic || !picref)
374         goto fail;
375
376     picref->buf = pic;
377     picref->buf->free = ff_avfilter_default_free_buffer;
378     if (!(picref->video = av_mallocz(sizeof(AVFilterBufferRefVideoProps))))
379         goto fail;
380
381     pic->w = picref->video->w = w;
382     pic->h = picref->video->h = h;
383
384     /* make sure the buffer gets read permission or it's useless for output */
385     picref->perms = perms | AV_PERM_READ;
386
387     pic->refcount = 1;
388     picref->type = AVMEDIA_TYPE_VIDEO;
389     pic->format = picref->format = format;
390
391     memcpy(pic->data,        data,          sizeof(pic->data));
392     memcpy(pic->linesize,    linesize,      sizeof(pic->linesize));
393     memcpy(picref->data,     pic->data,     sizeof(picref->data));
394     memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
395
396     return picref;
397
398 fail:
399     if (picref && picref->video)
400         av_free(picref->video);
401     av_free(picref);
402     av_free(pic);
403     return NULL;
404 }
405
406 AVFilterBufferRef *avfilter_get_audio_buffer(AVFilterLink *link, int perms,
407                                              enum AVSampleFormat sample_fmt, int nb_samples,
408                                              int64_t channel_layout, int planar)
409 {
410     AVFilterBufferRef *ret = NULL;
411
412     if (link->dstpad->get_audio_buffer)
413         ret = link->dstpad->get_audio_buffer(link, perms, sample_fmt, nb_samples, channel_layout, planar);
414
415     if (!ret)
416         ret = avfilter_default_get_audio_buffer(link, perms, sample_fmt, nb_samples, channel_layout, planar);
417
418     if (ret)
419         ret->type = AVMEDIA_TYPE_AUDIO;
420
421     return ret;
422 }
423
424 AVFilterBufferRef *
425 avfilter_get_audio_buffer_ref_from_arrays(uint8_t *data[8], int linesize[8], int perms,
426                                           int nb_samples, enum AVSampleFormat sample_fmt,
427                                           int64_t channel_layout, int planar)
428 {
429     AVFilterBuffer *samples = av_mallocz(sizeof(AVFilterBuffer));
430     AVFilterBufferRef *samplesref = av_mallocz(sizeof(AVFilterBufferRef));
431
432     if (!samples || !samplesref)
433         goto fail;
434
435     samplesref->buf = samples;
436     samplesref->buf->free = ff_avfilter_default_free_buffer;
437     if (!(samplesref->audio = av_mallocz(sizeof(AVFilterBufferRefAudioProps))))
438         goto fail;
439
440     samplesref->audio->nb_samples     = nb_samples;
441     samplesref->audio->channel_layout = channel_layout;
442     samplesref->audio->planar         = planar;
443
444     /* make sure the buffer gets read permission or it's useless for output */
445     samplesref->perms = perms | AV_PERM_READ;
446
447     samples->refcount = 1;
448     samplesref->type = AVMEDIA_TYPE_AUDIO;
449     samplesref->format = sample_fmt;
450
451     memcpy(samples->data,        data,     sizeof(samples->data));
452     memcpy(samples->linesize,    linesize, sizeof(samples->linesize));
453     memcpy(samplesref->data,     data,     sizeof(samplesref->data));
454     memcpy(samplesref->linesize, linesize, sizeof(samplesref->linesize));
455
456     return samplesref;
457
458 fail:
459     if (samplesref && samplesref->audio)
460         av_freep(&samplesref->audio);
461     av_freep(&samplesref);
462     av_freep(&samples);
463     return NULL;
464 }
465
466 int avfilter_request_frame(AVFilterLink *link)
467 {
468     FF_DPRINTF_START(NULL, request_frame); ff_dlog_link(NULL, link, 1);
469
470     if (link->srcpad->request_frame)
471         return link->srcpad->request_frame(link);
472     else if (link->src->inputs[0])
473         return avfilter_request_frame(link->src->inputs[0]);
474     else return -1;
475 }
476
477 int avfilter_poll_frame(AVFilterLink *link)
478 {
479     int i, min = INT_MAX;
480
481     if (link->srcpad->poll_frame)
482         return link->srcpad->poll_frame(link);
483
484     for (i = 0; i < link->src->input_count; i++) {
485         int val;
486         if (!link->src->inputs[i])
487             return -1;
488         val = avfilter_poll_frame(link->src->inputs[i]);
489         min = FFMIN(min, val);
490     }
491
492     return min;
493 }
494
495 /* XXX: should we do the duplicating of the picture ref here, instead of
496  * forcing the source filter to do it? */
497 void avfilter_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
498 {
499     void (*start_frame)(AVFilterLink *, AVFilterBufferRef *);
500     AVFilterPad *dst = link->dstpad;
501     int perms = picref->perms;
502
503     FF_DPRINTF_START(NULL, start_frame); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " "); ff_dlog_ref(NULL, picref, 1);
504
505     if (!(start_frame = dst->start_frame))
506         start_frame = avfilter_default_start_frame;
507
508     if (picref->linesize[0] < 0)
509         perms |= AV_PERM_NEG_LINESIZES;
510     /* prepare to copy the picture if it has insufficient permissions */
511     if ((dst->min_perms & perms) != dst->min_perms || dst->rej_perms & perms) {
512         av_log(link->dst, AV_LOG_DEBUG,
513                 "frame copy needed (have perms %x, need %x, reject %x)\n",
514                 picref->perms,
515                 link->dstpad->min_perms, link->dstpad->rej_perms);
516
517         link->cur_buf = avfilter_get_video_buffer(link, dst->min_perms, link->w, link->h);
518         link->src_buf = picref;
519         avfilter_copy_buffer_ref_props(link->cur_buf, link->src_buf);
520     }
521     else
522         link->cur_buf = picref;
523
524     start_frame(link, link->cur_buf);
525 }
526
527 void avfilter_end_frame(AVFilterLink *link)
528 {
529     void (*end_frame)(AVFilterLink *);
530
531     if (!(end_frame = link->dstpad->end_frame))
532         end_frame = avfilter_default_end_frame;
533
534     end_frame(link);
535
536     /* unreference the source picture if we're feeding the destination filter
537      * a copied version dues to permission issues */
538     if (link->src_buf) {
539         avfilter_unref_buffer(link->src_buf);
540         link->src_buf = NULL;
541     }
542 }
543
544 void avfilter_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
545 {
546     uint8_t *src[4], *dst[4];
547     int i, j, vsub;
548     void (*draw_slice)(AVFilterLink *, int, int, int);
549
550     FF_DPRINTF_START(NULL, draw_slice); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " y:%d h:%d dir:%d\n", y, h, slice_dir);
551
552     /* copy the slice if needed for permission reasons */
553     if (link->src_buf) {
554         vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
555
556         for (i = 0; i < 4; i++) {
557             if (link->src_buf->data[i]) {
558                 src[i] = link->src_buf-> data[i] +
559                     (y >> (i==1 || i==2 ? vsub : 0)) * link->src_buf-> linesize[i];
560                 dst[i] = link->cur_buf->data[i] +
561                     (y >> (i==1 || i==2 ? vsub : 0)) * link->cur_buf->linesize[i];
562             } else
563                 src[i] = dst[i] = NULL;
564         }
565
566         for (i = 0; i < 4; i++) {
567             int planew =
568                 av_image_get_linesize(link->format, link->cur_buf->video->w, i);
569
570             if (!src[i]) continue;
571
572             for (j = 0; j < h >> (i==1 || i==2 ? vsub : 0); j++) {
573                 memcpy(dst[i], src[i], planew);
574                 src[i] += link->src_buf->linesize[i];
575                 dst[i] += link->cur_buf->linesize[i];
576             }
577         }
578     }
579
580     if (!(draw_slice = link->dstpad->draw_slice))
581         draw_slice = avfilter_default_draw_slice;
582     draw_slice(link, y, h, slice_dir);
583 }
584
585 void avfilter_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
586 {
587     void (*filter_samples)(AVFilterLink *, AVFilterBufferRef *);
588     AVFilterPad *dst = link->dstpad;
589     int i;
590
591     FF_DPRINTF_START(NULL, filter_samples); ff_dlog_link(NULL, link, 1);
592
593     if (!(filter_samples = dst->filter_samples))
594         filter_samples = avfilter_default_filter_samples;
595
596     /* prepare to copy the samples if the buffer has insufficient permissions */
597     if ((dst->min_perms & samplesref->perms) != dst->min_perms ||
598         dst->rej_perms & samplesref->perms) {
599
600         av_log(link->dst, AV_LOG_DEBUG,
601                "Copying audio data in avfilter (have perms %x, need %x, reject %x)\n",
602                samplesref->perms, link->dstpad->min_perms, link->dstpad->rej_perms);
603
604         link->cur_buf = avfilter_default_get_audio_buffer(link, dst->min_perms,
605                                                           samplesref->format,
606                                                           samplesref->audio->nb_samples,
607                                                           samplesref->audio->channel_layout,
608                                                           samplesref->audio->planar);
609         link->cur_buf->pts                = samplesref->pts;
610         link->cur_buf->audio->sample_rate = samplesref->audio->sample_rate;
611
612         /* Copy actual data into new samples buffer */
613         for (i = 0; samplesref->data[i]; i++)
614             memcpy(link->cur_buf->data[i], samplesref->data[i], samplesref->linesize[0]);
615
616         avfilter_unref_buffer(samplesref);
617     } else
618         link->cur_buf = samplesref;
619
620     filter_samples(link, link->cur_buf);
621 }
622
623 #define MAX_REGISTERED_AVFILTERS_NB 64
624
625 static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1];
626
627 static int next_registered_avfilter_idx = 0;
628
629 AVFilter *avfilter_get_by_name(const char *name)
630 {
631     int i;
632
633     for (i = 0; registered_avfilters[i]; i++)
634         if (!strcmp(registered_avfilters[i]->name, name))
635             return registered_avfilters[i];
636
637     return NULL;
638 }
639
640 int avfilter_register(AVFilter *filter)
641 {
642     if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB)
643         return -1;
644
645     registered_avfilters[next_registered_avfilter_idx++] = filter;
646     return 0;
647 }
648
649 AVFilter **av_filter_next(AVFilter **filter)
650 {
651     return filter ? ++filter : &registered_avfilters[0];
652 }
653
654 void avfilter_uninit(void)
655 {
656     memset(registered_avfilters, 0, sizeof(registered_avfilters));
657     next_registered_avfilter_idx = 0;
658 }
659
660 static int pad_count(const AVFilterPad *pads)
661 {
662     int count;
663
664     for(count = 0; pads->name; count ++) pads ++;
665     return count;
666 }
667
668 static const char *filter_name(void *p)
669 {
670     AVFilterContext *filter = p;
671     return filter->filter->name;
672 }
673
674 static const AVClass avfilter_class = {
675     "AVFilter",
676     filter_name,
677     NULL,
678     LIBAVUTIL_VERSION_INT,
679 };
680
681 int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
682 {
683     AVFilterContext *ret;
684     *filter_ctx = NULL;
685
686     if (!filter)
687         return AVERROR(EINVAL);
688
689     ret = av_mallocz(sizeof(AVFilterContext));
690     if (!ret)
691         return AVERROR(ENOMEM);
692
693     ret->av_class = &avfilter_class;
694     ret->filter   = filter;
695     ret->name     = inst_name ? av_strdup(inst_name) : NULL;
696     if (filter->priv_size) {
697         ret->priv     = av_mallocz(filter->priv_size);
698         if (!ret->priv)
699             goto err;
700     }
701
702     ret->input_count  = pad_count(filter->inputs);
703     if (ret->input_count) {
704         ret->input_pads   = av_malloc(sizeof(AVFilterPad) * ret->input_count);
705         if (!ret->input_pads)
706             goto err;
707         memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->input_count);
708         ret->inputs       = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
709         if (!ret->inputs)
710             goto err;
711     }
712
713     ret->output_count = pad_count(filter->outputs);
714     if (ret->output_count) {
715         ret->output_pads  = av_malloc(sizeof(AVFilterPad) * ret->output_count);
716         if (!ret->output_pads)
717             goto err;
718         memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->output_count);
719         ret->outputs      = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
720         if (!ret->outputs)
721             goto err;
722     }
723
724     *filter_ctx = ret;
725     return 0;
726
727 err:
728     av_freep(&ret->inputs);
729     av_freep(&ret->input_pads);
730     ret->input_count = 0;
731     av_freep(&ret->outputs);
732     av_freep(&ret->output_pads);
733     ret->output_count = 0;
734     av_freep(&ret->priv);
735     av_free(ret);
736     return AVERROR(ENOMEM);
737 }
738
739 void avfilter_free(AVFilterContext *filter)
740 {
741     int i;
742     AVFilterLink *link;
743
744     if (filter->filter->uninit)
745         filter->filter->uninit(filter);
746
747     for (i = 0; i < filter->input_count; i++) {
748         if ((link = filter->inputs[i])) {
749             if (link->src)
750                 link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
751             avfilter_formats_unref(&link->in_formats);
752             avfilter_formats_unref(&link->out_formats);
753         }
754         avfilter_link_free(&link);
755     }
756     for (i = 0; i < filter->output_count; i++) {
757         if ((link = filter->outputs[i])) {
758             if (link->dst)
759                 link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
760             avfilter_formats_unref(&link->in_formats);
761             avfilter_formats_unref(&link->out_formats);
762         }
763         avfilter_link_free(&link);
764     }
765
766     av_freep(&filter->name);
767     av_freep(&filter->input_pads);
768     av_freep(&filter->output_pads);
769     av_freep(&filter->inputs);
770     av_freep(&filter->outputs);
771     av_freep(&filter->priv);
772     av_free(filter);
773 }
774
775 int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
776 {
777     int ret=0;
778
779     if (filter->filter->init)
780         ret = filter->filter->init(filter, args, opaque);
781     return ret;
782 }
783