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