]> 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 int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
169                            unsigned filt_srcpad_idx, unsigned filt_dstpad_idx)
170 {
171     int ret;
172     unsigned dstpad_idx = link->dstpad - link->dst->input_pads;
173
174     av_log(link->dst, AV_LOG_INFO, "auto-inserting filter '%s' "
175            "between the filter '%s' and the filter '%s'\n",
176            filt->name, link->src->name, link->dst->name);
177
178     link->dst->inputs[dstpad_idx] = NULL;
179     if ((ret = avfilter_link(filt, filt_dstpad_idx, link->dst, dstpad_idx)) < 0) {
180         /* failed to link output filter to new filter */
181         link->dst->inputs[dstpad_idx] = link;
182         return ret;
183     }
184
185     /* re-hookup the link to the new destination filter we inserted */
186     link->dst = filt;
187     link->dstpad = &filt->input_pads[filt_srcpad_idx];
188     filt->inputs[filt_srcpad_idx] = link;
189
190     /* if any information on supported media formats already exists on the
191      * link, we need to preserve that */
192     if (link->out_formats)
193         avfilter_formats_changeref(&link->out_formats,
194                                    &filt->outputs[filt_dstpad_idx]->out_formats);
195
196     return 0;
197 }
198
199 int avfilter_config_links(AVFilterContext *filter)
200 {
201     int (*config_link)(AVFilterLink *);
202     unsigned i;
203     int ret;
204
205     for (i = 0; i < filter->input_count; i ++) {
206         AVFilterLink *link = filter->inputs[i];
207
208         if (!link) continue;
209
210         switch (link->init_state) {
211         case AVLINK_INIT:
212             continue;
213         case AVLINK_STARTINIT:
214             av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
215             return 0;
216         case AVLINK_UNINIT:
217             link->init_state = AVLINK_STARTINIT;
218
219             if ((ret = avfilter_config_links(link->src)) < 0)
220                 return ret;
221
222             if (!(config_link = link->srcpad->config_props))
223                 config_link  = avfilter_default_config_output_link;
224             if ((ret = config_link(link)) < 0)
225                 return ret;
226
227             if (link->time_base.num == 0 && link->time_base.den == 0)
228                 link->time_base = link->src && link->src->input_count ?
229                     link->src->inputs[0]->time_base : AV_TIME_BASE_Q;
230
231             if (link->sample_aspect_ratio.num == 0 && link->sample_aspect_ratio.den == 0)
232                 link->sample_aspect_ratio = link->src->input_count ?
233                     link->src->inputs[0]->sample_aspect_ratio : (AVRational){1,1};
234
235             if (link->sample_rate == 0 && link->src && link->src->input_count)
236                 link->sample_rate = link->src->inputs[0]->sample_rate;
237
238             if (link->channel_layout == 0 && link->src && link->src->input_count)
239                 link->channel_layout = link->src->inputs[0]->channel_layout;
240
241             if ((config_link = link->dstpad->config_props))
242                 if ((ret = config_link(link)) < 0)
243                     return ret;
244
245             link->init_state = AVLINK_INIT;
246         }
247     }
248
249     return 0;
250 }
251
252 static char *ff_get_ref_perms_string(char *buf, size_t buf_size, int perms)
253 {
254     snprintf(buf, buf_size, "%s%s%s%s%s%s",
255              perms & AV_PERM_READ      ? "r" : "",
256              perms & AV_PERM_WRITE     ? "w" : "",
257              perms & AV_PERM_PRESERVE  ? "p" : "",
258              perms & AV_PERM_REUSE     ? "u" : "",
259              perms & AV_PERM_REUSE2    ? "U" : "",
260              perms & AV_PERM_NEG_LINESIZES ? "n" : "");
261     return buf;
262 }
263
264 static void ff_dlog_ref(void *ctx, AVFilterBufferRef *ref, int end)
265 {
266     av_unused char buf[16];
267     av_dlog(ctx,
268             "ref[%p buf:%p refcount:%d perms:%s data:%p linesize[%d, %d, %d, %d] pts:%"PRId64" pos:%"PRId64,
269             ref, ref->buf, ref->buf->refcount, ff_get_ref_perms_string(buf, sizeof(buf), ref->perms), ref->data[0],
270             ref->linesize[0], ref->linesize[1], ref->linesize[2], ref->linesize[3],
271             ref->pts, ref->pos);
272
273     if (ref->video) {
274         av_dlog(ctx, " a:%d/%d s:%dx%d i:%c iskey:%d type:%c",
275                 ref->video->sample_aspect_ratio.num, ref->video->sample_aspect_ratio.den,
276                 ref->video->w, ref->video->h,
277                 !ref->video->interlaced     ? 'P' :         /* Progressive  */
278                 ref->video->top_field_first ? 'T' : 'B',    /* Top / Bottom */
279                 ref->video->key_frame,
280                 av_get_picture_type_char(ref->video->pict_type));
281     }
282     if (ref->audio) {
283         av_dlog(ctx, " cl:%"PRId64"d sn:%d s:%d sr:%d p:%d",
284                 ref->audio->channel_layout,
285                 ref->audio->nb_samples,
286                 ref->audio->size,
287                 ref->audio->sample_rate,
288                 ref->audio->planar);
289     }
290
291     av_dlog(ctx, "]%s", end ? "\n" : "");
292 }
293
294 static void ff_dlog_link(void *ctx, AVFilterLink *link, int end)
295 {
296     if (link->type == AVMEDIA_TYPE_VIDEO) {
297         av_dlog(ctx,
298                 "link[%p s:%dx%d fmt:%-16s %-16s->%-16s]%s",
299                 link, link->w, link->h,
300                 av_pix_fmt_descriptors[link->format].name,
301                 link->src ? link->src->filter->name : "",
302                 link->dst ? link->dst->filter->name : "",
303                 end ? "\n" : "");
304     } else {
305         char buf[128];
306         av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout);
307
308         av_dlog(ctx,
309                 "link[%p r:%"PRId64" cl:%s fmt:%-16s %-16s->%-16s]%s",
310                 link, link->sample_rate, buf,
311                 av_get_sample_fmt_name(link->format),
312                 link->src ? link->src->filter->name : "",
313                 link->dst ? link->dst->filter->name : "",
314                 end ? "\n" : "");
315     }
316 }
317
318 #define FF_DPRINTF_START(ctx, func) av_dlog(NULL, "%-16s: ", #func)
319
320 AVFilterBufferRef *avfilter_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
321 {
322     AVFilterBufferRef *ret = NULL;
323
324     av_unused char buf[16];
325     FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0);
326     av_dlog(NULL, " perms:%s w:%d h:%d\n", ff_get_ref_perms_string(buf, sizeof(buf), perms), w, h);
327
328     if (link->dstpad->get_video_buffer)
329         ret = link->dstpad->get_video_buffer(link, perms, w, h);
330
331     if (!ret)
332         ret = avfilter_default_get_video_buffer(link, perms, w, h);
333
334     if (ret)
335         ret->type = AVMEDIA_TYPE_VIDEO;
336
337     FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " returning "); ff_dlog_ref(NULL, ret, 1);
338
339     return ret;
340 }
341
342 AVFilterBufferRef *
343 avfilter_get_video_buffer_ref_from_arrays(uint8_t *data[4], int linesize[4], int perms,
344                                           int w, int h, enum PixelFormat format)
345 {
346     AVFilterBuffer *pic = av_mallocz(sizeof(AVFilterBuffer));
347     AVFilterBufferRef *picref = av_mallocz(sizeof(AVFilterBufferRef));
348
349     if (!pic || !picref)
350         goto fail;
351
352     picref->buf = pic;
353     picref->buf->free = ff_avfilter_default_free_buffer;
354     if (!(picref->video = av_mallocz(sizeof(AVFilterBufferRefVideoProps))))
355         goto fail;
356
357     pic->w = picref->video->w = w;
358     pic->h = picref->video->h = h;
359
360     /* make sure the buffer gets read permission or it's useless for output */
361     picref->perms = perms | AV_PERM_READ;
362
363     pic->refcount = 1;
364     picref->type = AVMEDIA_TYPE_VIDEO;
365     pic->format = picref->format = format;
366
367     memcpy(pic->data,        data,          sizeof(pic->data));
368     memcpy(pic->linesize,    linesize,      sizeof(pic->linesize));
369     memcpy(picref->data,     pic->data,     sizeof(picref->data));
370     memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
371
372     return picref;
373
374 fail:
375     if (picref && picref->video)
376         av_free(picref->video);
377     av_free(picref);
378     av_free(pic);
379     return NULL;
380 }
381
382 AVFilterBufferRef *avfilter_get_audio_buffer(AVFilterLink *link, int perms,
383                                              enum AVSampleFormat sample_fmt, int size,
384                                              int64_t channel_layout, int planar)
385 {
386     AVFilterBufferRef *ret = NULL;
387
388     if (link->dstpad->get_audio_buffer)
389         ret = link->dstpad->get_audio_buffer(link, perms, sample_fmt, size, channel_layout, planar);
390
391     if (!ret)
392         ret = avfilter_default_get_audio_buffer(link, perms, sample_fmt, size, channel_layout, planar);
393
394     if (ret)
395         ret->type = AVMEDIA_TYPE_AUDIO;
396
397     return ret;
398 }
399
400 int avfilter_request_frame(AVFilterLink *link)
401 {
402     FF_DPRINTF_START(NULL, request_frame); ff_dlog_link(NULL, link, 1);
403
404     if (link->srcpad->request_frame)
405         return link->srcpad->request_frame(link);
406     else if (link->src->inputs[0])
407         return avfilter_request_frame(link->src->inputs[0]);
408     else return -1;
409 }
410
411 int avfilter_poll_frame(AVFilterLink *link)
412 {
413     int i, min = INT_MAX;
414
415     if (link->srcpad->poll_frame)
416         return link->srcpad->poll_frame(link);
417
418     for (i = 0; i < link->src->input_count; i++) {
419         int val;
420         if (!link->src->inputs[i])
421             return -1;
422         val = avfilter_poll_frame(link->src->inputs[i]);
423         min = FFMIN(min, val);
424     }
425
426     return min;
427 }
428
429 /* XXX: should we do the duplicating of the picture ref here, instead of
430  * forcing the source filter to do it? */
431 void avfilter_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
432 {
433     void (*start_frame)(AVFilterLink *, AVFilterBufferRef *);
434     AVFilterPad *dst = link->dstpad;
435     int perms = picref->perms;
436
437     FF_DPRINTF_START(NULL, start_frame); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " "); ff_dlog_ref(NULL, picref, 1);
438
439     if (!(start_frame = dst->start_frame))
440         start_frame = avfilter_default_start_frame;
441
442     if (picref->linesize[0] < 0)
443         perms |= AV_PERM_NEG_LINESIZES;
444     /* prepare to copy the picture if it has insufficient permissions */
445     if ((dst->min_perms & perms) != dst->min_perms || dst->rej_perms & perms) {
446         av_log(link->dst, AV_LOG_DEBUG,
447                 "frame copy needed (have perms %x, need %x, reject %x)\n",
448                 picref->perms,
449                 link->dstpad->min_perms, link->dstpad->rej_perms);
450
451         link->cur_buf = avfilter_get_video_buffer(link, dst->min_perms, link->w, link->h);
452         link->src_buf = picref;
453         avfilter_copy_buffer_ref_props(link->cur_buf, link->src_buf);
454     }
455     else
456         link->cur_buf = picref;
457
458     start_frame(link, link->cur_buf);
459 }
460
461 void avfilter_end_frame(AVFilterLink *link)
462 {
463     void (*end_frame)(AVFilterLink *);
464
465     if (!(end_frame = link->dstpad->end_frame))
466         end_frame = avfilter_default_end_frame;
467
468     end_frame(link);
469
470     /* unreference the source picture if we're feeding the destination filter
471      * a copied version dues to permission issues */
472     if (link->src_buf) {
473         avfilter_unref_buffer(link->src_buf);
474         link->src_buf = NULL;
475     }
476 }
477
478 void avfilter_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
479 {
480     uint8_t *src[4], *dst[4];
481     int i, j, vsub;
482     void (*draw_slice)(AVFilterLink *, int, int, int);
483
484     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);
485
486     /* copy the slice if needed for permission reasons */
487     if (link->src_buf) {
488         vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
489
490         for (i = 0; i < 4; i++) {
491             if (link->src_buf->data[i]) {
492                 src[i] = link->src_buf-> data[i] +
493                     (y >> (i==1 || i==2 ? vsub : 0)) * link->src_buf-> linesize[i];
494                 dst[i] = link->cur_buf->data[i] +
495                     (y >> (i==1 || i==2 ? vsub : 0)) * link->cur_buf->linesize[i];
496             } else
497                 src[i] = dst[i] = NULL;
498         }
499
500         for (i = 0; i < 4; i++) {
501             int planew =
502                 av_image_get_linesize(link->format, link->cur_buf->video->w, i);
503
504             if (!src[i]) continue;
505
506             for (j = 0; j < h >> (i==1 || i==2 ? vsub : 0); j++) {
507                 memcpy(dst[i], src[i], planew);
508                 src[i] += link->src_buf->linesize[i];
509                 dst[i] += link->cur_buf->linesize[i];
510             }
511         }
512     }
513
514     if (!(draw_slice = link->dstpad->draw_slice))
515         draw_slice = avfilter_default_draw_slice;
516     draw_slice(link, y, h, slice_dir);
517 }
518
519 void avfilter_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
520 {
521     void (*filter_samples)(AVFilterLink *, AVFilterBufferRef *);
522     AVFilterPad *dst = link->dstpad;
523
524     FF_DPRINTF_START(NULL, filter_samples); ff_dlog_link(NULL, link, 1);
525
526     if (!(filter_samples = dst->filter_samples))
527         filter_samples = avfilter_default_filter_samples;
528
529     /* prepare to copy the samples if the buffer has insufficient permissions */
530     if ((dst->min_perms & samplesref->perms) != dst->min_perms ||
531         dst->rej_perms & samplesref->perms) {
532
533         av_log(link->dst, AV_LOG_DEBUG,
534                "Copying audio data in avfilter (have perms %x, need %x, reject %x)\n",
535                samplesref->perms, link->dstpad->min_perms, link->dstpad->rej_perms);
536
537         link->cur_buf = avfilter_default_get_audio_buffer(link, dst->min_perms,
538                                                           samplesref->format,
539                                                           samplesref->audio->size,
540                                                           samplesref->audio->channel_layout,
541                                                           samplesref->audio->planar);
542         link->cur_buf->pts                = samplesref->pts;
543         link->cur_buf->audio->sample_rate = samplesref->audio->sample_rate;
544
545         /* Copy actual data into new samples buffer */
546         memcpy(link->cur_buf->data[0], samplesref->data[0], samplesref->audio->size);
547
548         avfilter_unref_buffer(samplesref);
549     } else
550         link->cur_buf = samplesref;
551
552     filter_samples(link, link->cur_buf);
553 }
554
555 #define MAX_REGISTERED_AVFILTERS_NB 64
556
557 static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1];
558
559 static int next_registered_avfilter_idx = 0;
560
561 AVFilter *avfilter_get_by_name(const char *name)
562 {
563     int i;
564
565     for (i = 0; registered_avfilters[i]; i++)
566         if (!strcmp(registered_avfilters[i]->name, name))
567             return registered_avfilters[i];
568
569     return NULL;
570 }
571
572 int avfilter_register(AVFilter *filter)
573 {
574     if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB)
575         return -1;
576
577     registered_avfilters[next_registered_avfilter_idx++] = filter;
578     return 0;
579 }
580
581 AVFilter **av_filter_next(AVFilter **filter)
582 {
583     return filter ? ++filter : &registered_avfilters[0];
584 }
585
586 void avfilter_uninit(void)
587 {
588     memset(registered_avfilters, 0, sizeof(registered_avfilters));
589     next_registered_avfilter_idx = 0;
590 }
591
592 static int pad_count(const AVFilterPad *pads)
593 {
594     int count;
595
596     for(count = 0; pads->name; count ++) pads ++;
597     return count;
598 }
599
600 static const char *filter_name(void *p)
601 {
602     AVFilterContext *filter = p;
603     return filter->filter->name;
604 }
605
606 static const AVClass avfilter_class = {
607     "AVFilter",
608     filter_name,
609     NULL,
610     LIBAVUTIL_VERSION_INT,
611 };
612
613 int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
614 {
615     AVFilterContext *ret;
616     *filter_ctx = NULL;
617
618     if (!filter)
619         return AVERROR(EINVAL);
620
621     ret = av_mallocz(sizeof(AVFilterContext));
622     if (!ret)
623         return AVERROR(ENOMEM);
624
625     ret->av_class = &avfilter_class;
626     ret->filter   = filter;
627     ret->name     = inst_name ? av_strdup(inst_name) : NULL;
628     if (filter->priv_size) {
629         ret->priv     = av_mallocz(filter->priv_size);
630         if (!ret->priv)
631             goto err;
632     }
633
634     ret->input_count  = pad_count(filter->inputs);
635     if (ret->input_count) {
636         ret->input_pads   = av_malloc(sizeof(AVFilterPad) * ret->input_count);
637         if (!ret->input_pads)
638             goto err;
639         memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->input_count);
640         ret->inputs       = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
641         if (!ret->inputs)
642             goto err;
643     }
644
645     ret->output_count = pad_count(filter->outputs);
646     if (ret->output_count) {
647         ret->output_pads  = av_malloc(sizeof(AVFilterPad) * ret->output_count);
648         if (!ret->output_pads)
649             goto err;
650         memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->output_count);
651         ret->outputs      = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
652         if (!ret->outputs)
653             goto err;
654     }
655
656     *filter_ctx = ret;
657     return 0;
658
659 err:
660     av_freep(&ret->inputs);
661     av_freep(&ret->input_pads);
662     ret->input_count = 0;
663     av_freep(&ret->outputs);
664     av_freep(&ret->output_pads);
665     ret->output_count = 0;
666     av_freep(&ret->priv);
667     av_free(ret);
668     return AVERROR(ENOMEM);
669 }
670
671 void avfilter_free(AVFilterContext *filter)
672 {
673     int i;
674     AVFilterLink *link;
675
676     if (filter->filter->uninit)
677         filter->filter->uninit(filter);
678
679     for (i = 0; i < filter->input_count; i++) {
680         if ((link = filter->inputs[i])) {
681             if (link->src)
682                 link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
683             avfilter_formats_unref(&link->in_formats);
684             avfilter_formats_unref(&link->out_formats);
685         }
686         av_freep(&link);
687     }
688     for (i = 0; i < filter->output_count; i++) {
689         if ((link = filter->outputs[i])) {
690             if (link->dst)
691                 link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
692             avfilter_formats_unref(&link->in_formats);
693             avfilter_formats_unref(&link->out_formats);
694         }
695         av_freep(&link);
696     }
697
698     av_freep(&filter->name);
699     av_freep(&filter->input_pads);
700     av_freep(&filter->output_pads);
701     av_freep(&filter->inputs);
702     av_freep(&filter->outputs);
703     av_freep(&filter->priv);
704     av_free(filter);
705 }
706
707 int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
708 {
709     int ret=0;
710
711     if (filter->filter->init)
712         ret = filter->filter->init(filter, args, opaque);
713     return ret;
714 }
715