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