3 * Copyright (c) 2007 Bobby Bingham
5 * This file is part of FFmpeg.
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.
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.
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
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 "libavutil/avstring.h"
33 unsigned avfilter_version(void) {
34 return LIBAVFILTER_VERSION_INT;
37 const char *avfilter_configuration(void)
39 return FFMPEG_CONFIGURATION;
42 const char *avfilter_license(void)
44 #define LICENSE_PREFIX "libavfilter license: "
45 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
48 static void command_queue_pop(AVFilterContext *filter)
50 AVFilterCommand *c= filter->command_queue;
52 av_freep(&c->command);
53 filter->command_queue= c->next;
57 AVFilterBufferRef *avfilter_ref_buffer(AVFilterBufferRef *ref, int pmask)
59 AVFilterBufferRef *ret = av_malloc(sizeof(AVFilterBufferRef));
63 if (ref->type == AVMEDIA_TYPE_VIDEO) {
64 ret->video = av_malloc(sizeof(AVFilterBufferRefVideoProps));
69 *ret->video = *ref->video;
70 } else if (ref->type == AVMEDIA_TYPE_AUDIO) {
71 ret->audio = av_malloc(sizeof(AVFilterBufferRefAudioProps));
76 *ret->audio = *ref->audio;
79 ret->buf->refcount ++;
83 static void store_in_pool(AVFilterBufferRef *ref)
86 AVFilterPool *pool= ref->buf->priv;
88 av_assert0(ref->buf->data[0]);
90 if (pool->count == POOL_SIZE) {
91 AVFilterBufferRef *ref1 = pool->pic[0];
92 av_freep(&ref1->video);
93 av_freep(&ref1->audio);
94 av_freep(&ref1->buf->data[0]);
97 memmove(&pool->pic[0], &pool->pic[1], sizeof(void*)*(POOL_SIZE-1));
99 pool->pic[POOL_SIZE-1] = NULL;
102 for (i = 0; i < POOL_SIZE; i++) {
111 void avfilter_unref_buffer(AVFilterBufferRef *ref)
115 if (!(--ref->buf->refcount)) {
116 if (!ref->buf->free) {
120 ref->buf->free(ref->buf);
122 av_freep(&ref->video);
123 av_freep(&ref->audio);
127 void avfilter_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
128 AVFilterPad **pads, AVFilterLink ***links,
133 idx = FFMIN(idx, *count);
135 *pads = av_realloc(*pads, sizeof(AVFilterPad) * (*count + 1));
136 *links = av_realloc(*links, sizeof(AVFilterLink*) * (*count + 1));
137 memmove(*pads +idx+1, *pads +idx, sizeof(AVFilterPad) * (*count-idx));
138 memmove(*links+idx+1, *links+idx, sizeof(AVFilterLink*) * (*count-idx));
139 memcpy(*pads+idx, newpad, sizeof(AVFilterPad));
140 (*links)[idx] = NULL;
143 for (i = idx+1; i < *count; i++)
145 (*(unsigned *)((uint8_t *) *links[i] + padidx_off))++;
148 int avfilter_link(AVFilterContext *src, unsigned srcpad,
149 AVFilterContext *dst, unsigned dstpad)
153 if (src->output_count <= srcpad || dst->input_count <= dstpad ||
154 src->outputs[srcpad] || dst->inputs[dstpad])
157 if (src->output_pads[srcpad].type != dst->input_pads[dstpad].type) {
158 av_log(src, AV_LOG_ERROR,
159 "Media type mismatch between the '%s' filter output pad %d and the '%s' filter input pad %d\n",
160 src->name, srcpad, dst->name, dstpad);
161 return AVERROR(EINVAL);
164 src->outputs[srcpad] =
165 dst-> inputs[dstpad] = link = av_mallocz(sizeof(AVFilterLink));
169 link->srcpad = &src->output_pads[srcpad];
170 link->dstpad = &dst->input_pads[dstpad];
171 link->type = src->output_pads[srcpad].type;
172 assert(PIX_FMT_NONE == -1 && AV_SAMPLE_FMT_NONE == -1);
178 void avfilter_link_free(AVFilterLink **link)
185 for (i = 0; i < POOL_SIZE; i++) {
186 if ((*link)->pool->pic[i]) {
187 AVFilterBufferRef *picref = (*link)->pool->pic[i];
188 /* free buffer: picrefs stored in the pool are not
189 * supposed to contain a free callback */
190 av_freep(&picref->buf->data[0]);
191 av_freep(&picref->buf);
193 av_freep(&picref->audio);
194 av_freep(&picref->video);
195 av_freep(&(*link)->pool->pic[i]);
198 (*link)->pool->count = 0;
199 // av_freep(&(*link)->pool);
204 int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
205 unsigned filt_srcpad_idx, unsigned filt_dstpad_idx)
208 unsigned dstpad_idx = link->dstpad - link->dst->input_pads;
210 av_log(link->dst, AV_LOG_INFO, "auto-inserting filter '%s' "
211 "between the filter '%s' and the filter '%s'\n",
212 filt->name, link->src->name, link->dst->name);
214 link->dst->inputs[dstpad_idx] = NULL;
215 if ((ret = avfilter_link(filt, filt_dstpad_idx, link->dst, dstpad_idx)) < 0) {
216 /* failed to link output filter to new filter */
217 link->dst->inputs[dstpad_idx] = link;
221 /* re-hookup the link to the new destination filter we inserted */
223 link->dstpad = &filt->input_pads[filt_srcpad_idx];
224 filt->inputs[filt_srcpad_idx] = link;
226 /* if any information on supported media formats already exists on the
227 * link, we need to preserve that */
228 if (link->out_formats)
229 avfilter_formats_changeref(&link->out_formats,
230 &filt->outputs[filt_dstpad_idx]->out_formats);
231 if (link->out_chlayouts)
232 avfilter_formats_changeref(&link->out_chlayouts,
233 &filt->outputs[filt_dstpad_idx]->out_chlayouts);
234 if (link->out_packing)
235 avfilter_formats_changeref(&link->out_packing,
236 &filt->outputs[filt_dstpad_idx]->out_packing);
241 int avfilter_config_links(AVFilterContext *filter)
243 int (*config_link)(AVFilterLink *);
247 for (i = 0; i < filter->input_count; i ++) {
248 AVFilterLink *link = filter->inputs[i];
249 AVFilterLink *inlink = link->src->input_count ?
250 link->src->inputs[0] : NULL;
254 switch (link->init_state) {
257 case AVLINK_STARTINIT:
258 av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
261 link->init_state = AVLINK_STARTINIT;
263 if ((ret = avfilter_config_links(link->src)) < 0)
266 if (!(config_link = link->srcpad->config_props)) {
267 if (link->src->input_count != 1) {
268 av_log(link->src, AV_LOG_ERROR, "Source filters and filters "
269 "with more than one input "
270 "must set config_props() "
271 "callbacks on all outputs\n");
272 return AVERROR(EINVAL);
274 } else if ((ret = config_link(link)) < 0)
277 switch (link->type) {
278 case AVMEDIA_TYPE_VIDEO:
279 if (!link->time_base.num && !link->time_base.den)
280 link->time_base = inlink ? inlink->time_base : AV_TIME_BASE_Q;
282 if (!link->sample_aspect_ratio.num && !link->sample_aspect_ratio.den)
283 link->sample_aspect_ratio = inlink ?
284 inlink->sample_aspect_ratio : (AVRational){1,1};
291 } else if (!link->w || !link->h) {
292 av_log(link->src, AV_LOG_ERROR,
293 "Video source filters must set their output link's "
294 "width and height\n");
295 return AVERROR(EINVAL);
299 case AVMEDIA_TYPE_AUDIO:
301 if (!link->sample_rate)
302 link->sample_rate = inlink->sample_rate;
303 if (!link->time_base.num && !link->time_base.den)
304 link->time_base = inlink->time_base;
305 } else if (!link->sample_rate) {
306 av_log(link->src, AV_LOG_ERROR,
307 "Audio source filters must set their output link's "
309 return AVERROR(EINVAL);
312 if (!link->time_base.num && !link->time_base.den)
313 link->time_base = (AVRational) {1, link->sample_rate};
316 if ((config_link = link->dstpad->config_props))
317 if ((ret = config_link(link)) < 0)
320 link->init_state = AVLINK_INIT;
327 static char *ff_get_ref_perms_string(char *buf, size_t buf_size, int perms)
329 snprintf(buf, buf_size, "%s%s%s%s%s%s",
330 perms & AV_PERM_READ ? "r" : "",
331 perms & AV_PERM_WRITE ? "w" : "",
332 perms & AV_PERM_PRESERVE ? "p" : "",
333 perms & AV_PERM_REUSE ? "u" : "",
334 perms & AV_PERM_REUSE2 ? "U" : "",
335 perms & AV_PERM_NEG_LINESIZES ? "n" : "");
339 static void ff_dlog_ref(void *ctx, AVFilterBufferRef *ref, int end)
341 av_unused char buf[16];
343 "ref[%p buf:%p refcount:%d perms:%s data:%p linesize[%d, %d, %d, %d] pts:%"PRId64" pos:%"PRId64,
344 ref, ref->buf, ref->buf->refcount, ff_get_ref_perms_string(buf, sizeof(buf), ref->perms), ref->data[0],
345 ref->linesize[0], ref->linesize[1], ref->linesize[2], ref->linesize[3],
349 av_dlog(ctx, " a:%d/%d s:%dx%d i:%c iskey:%d type:%c",
350 ref->video->sample_aspect_ratio.num, ref->video->sample_aspect_ratio.den,
351 ref->video->w, ref->video->h,
352 !ref->video->interlaced ? 'P' : /* Progressive */
353 ref->video->top_field_first ? 'T' : 'B', /* Top / Bottom */
354 ref->video->key_frame,
355 av_get_picture_type_char(ref->video->pict_type));
358 av_dlog(ctx, " cl:%"PRId64"d n:%d r:%d p:%d",
359 ref->audio->channel_layout,
360 ref->audio->nb_samples,
361 ref->audio->sample_rate,
365 av_dlog(ctx, "]%s", end ? "\n" : "");
368 static void ff_dlog_link(void *ctx, AVFilterLink *link, int end)
370 if (link->type == AVMEDIA_TYPE_VIDEO) {
372 "link[%p s:%dx%d fmt:%-16s %-16s->%-16s]%s",
373 link, link->w, link->h,
374 av_pix_fmt_descriptors[link->format].name,
375 link->src ? link->src->filter->name : "",
376 link->dst ? link->dst->filter->name : "",
380 av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout);
383 "link[%p r:%d cl:%s fmt:%-16s %-16s->%-16s]%s",
384 link, (int)link->sample_rate, buf,
385 av_get_sample_fmt_name(link->format),
386 link->src ? link->src->filter->name : "",
387 link->dst ? link->dst->filter->name : "",
392 #define FF_DPRINTF_START(ctx, func) av_dlog(NULL, "%-16s: ", #func)
394 AVFilterBufferRef *avfilter_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
396 AVFilterBufferRef *ret = NULL;
398 av_unused char buf[16];
399 FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0);
400 av_dlog(NULL, " perms:%s w:%d h:%d\n", ff_get_ref_perms_string(buf, sizeof(buf), perms), w, h);
402 if (link->dstpad->get_video_buffer)
403 ret = link->dstpad->get_video_buffer(link, perms, w, h);
406 ret = avfilter_default_get_video_buffer(link, perms, w, h);
409 ret->type = AVMEDIA_TYPE_VIDEO;
411 FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " returning "); ff_dlog_ref(NULL, ret, 1);
417 avfilter_get_video_buffer_ref_from_arrays(uint8_t * const data[4], const int linesize[4], int perms,
418 int w, int h, enum PixelFormat format)
420 AVFilterBuffer *pic = av_mallocz(sizeof(AVFilterBuffer));
421 AVFilterBufferRef *picref = av_mallocz(sizeof(AVFilterBufferRef));
427 picref->buf->free = ff_avfilter_default_free_buffer;
428 if (!(picref->video = av_mallocz(sizeof(AVFilterBufferRefVideoProps))))
431 pic->w = picref->video->w = w;
432 pic->h = picref->video->h = h;
434 /* make sure the buffer gets read permission or it's useless for output */
435 picref->perms = perms | AV_PERM_READ;
438 picref->type = AVMEDIA_TYPE_VIDEO;
439 pic->format = picref->format = format;
441 memcpy(pic->data, data, 4*sizeof(data[0]));
442 memcpy(pic->linesize, linesize, 4*sizeof(linesize[0]));
443 memcpy(picref->data, pic->data, sizeof(picref->data));
444 memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
449 if (picref && picref->video)
450 av_free(picref->video);
456 AVFilterBufferRef *avfilter_get_audio_buffer(AVFilterLink *link,
457 int perms, int nb_samples)
459 AVFilterBufferRef *ret = NULL;
461 if (link->dstpad->get_audio_buffer)
462 ret = link->dstpad->get_audio_buffer(link, perms, nb_samples);
465 ret = avfilter_default_get_audio_buffer(link, perms, nb_samples);
468 ret->type = AVMEDIA_TYPE_AUDIO;
474 avfilter_get_audio_buffer_ref_from_arrays(uint8_t *data[8], int linesize[8], int perms,
475 int nb_samples, enum AVSampleFormat sample_fmt,
476 uint64_t channel_layout, int planar)
478 AVFilterBuffer *samples = av_mallocz(sizeof(AVFilterBuffer));
479 AVFilterBufferRef *samplesref = av_mallocz(sizeof(AVFilterBufferRef));
481 if (!samples || !samplesref)
484 samplesref->buf = samples;
485 samplesref->buf->free = ff_avfilter_default_free_buffer;
486 if (!(samplesref->audio = av_mallocz(sizeof(AVFilterBufferRefAudioProps))))
489 samplesref->audio->nb_samples = nb_samples;
490 samplesref->audio->channel_layout = channel_layout;
491 samplesref->audio->planar = planar;
493 /* make sure the buffer gets read permission or it's useless for output */
494 samplesref->perms = perms | AV_PERM_READ;
496 samples->refcount = 1;
497 samplesref->type = AVMEDIA_TYPE_AUDIO;
498 samplesref->format = sample_fmt;
500 memcpy(samples->data, data, sizeof(samples->data));
501 memcpy(samples->linesize, linesize, sizeof(samples->linesize));
502 memcpy(samplesref->data, data, sizeof(samplesref->data));
503 memcpy(samplesref->linesize, linesize, sizeof(samplesref->linesize));
508 if (samplesref && samplesref->audio)
509 av_freep(&samplesref->audio);
510 av_freep(&samplesref);
515 int avfilter_request_frame(AVFilterLink *link)
517 FF_DPRINTF_START(NULL, request_frame); ff_dlog_link(NULL, link, 1);
519 if (link->srcpad->request_frame)
520 return link->srcpad->request_frame(link);
521 else if (link->src->inputs[0])
522 return avfilter_request_frame(link->src->inputs[0]);
526 int avfilter_poll_frame(AVFilterLink *link)
528 int i, min = INT_MAX;
530 if (link->srcpad->poll_frame)
531 return link->srcpad->poll_frame(link);
533 for (i = 0; i < link->src->input_count; i++) {
535 if (!link->src->inputs[i])
537 val = avfilter_poll_frame(link->src->inputs[i]);
538 min = FFMIN(min, val);
544 /* XXX: should we do the duplicating of the picture ref here, instead of
545 * forcing the source filter to do it? */
546 void avfilter_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
548 void (*start_frame)(AVFilterLink *, AVFilterBufferRef *);
549 AVFilterPad *dst = link->dstpad;
550 int perms = picref->perms;
551 AVFilterCommand *cmd= link->dst->command_queue;
553 FF_DPRINTF_START(NULL, start_frame); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " "); ff_dlog_ref(NULL, picref, 1);
555 if (!(start_frame = dst->start_frame))
556 start_frame = avfilter_default_start_frame;
558 if (picref->linesize[0] < 0)
559 perms |= AV_PERM_NEG_LINESIZES;
560 /* prepare to copy the picture if it has insufficient permissions */
561 if ((dst->min_perms & perms) != dst->min_perms || dst->rej_perms & perms) {
562 av_log(link->dst, AV_LOG_DEBUG,
563 "frame copy needed (have perms %x, need %x, reject %x)\n",
565 link->dstpad->min_perms, link->dstpad->rej_perms);
567 link->cur_buf = avfilter_get_video_buffer(link, dst->min_perms, link->w, link->h);
568 link->src_buf = picref;
569 avfilter_copy_buffer_ref_props(link->cur_buf, link->src_buf);
572 link->cur_buf = picref;
574 while(cmd && cmd->time <= picref->pts * av_q2d(link->time_base)){
575 av_log(link->dst, AV_LOG_DEBUG,
576 "Processing command time:%f command:%s arg:%s\n",
577 cmd->time, cmd->command, cmd->arg);
578 avfilter_process_command(link->dst, cmd->command, cmd->arg, 0, 0, cmd->flags);
579 command_queue_pop(link->dst);
580 cmd= link->dst->command_queue;
583 start_frame(link, link->cur_buf);
586 void avfilter_end_frame(AVFilterLink *link)
588 void (*end_frame)(AVFilterLink *);
590 if (!(end_frame = link->dstpad->end_frame))
591 end_frame = avfilter_default_end_frame;
595 /* unreference the source picture if we're feeding the destination filter
596 * a copied version dues to permission issues */
598 avfilter_unref_buffer(link->src_buf);
599 link->src_buf = NULL;
603 void avfilter_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
605 uint8_t *src[4], *dst[4];
607 void (*draw_slice)(AVFilterLink *, int, int, int);
609 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);
611 /* copy the slice if needed for permission reasons */
613 vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
615 for (i = 0; i < 4; i++) {
616 if (link->src_buf->data[i]) {
617 src[i] = link->src_buf-> data[i] +
618 (y >> (i==1 || i==2 ? vsub : 0)) * link->src_buf-> linesize[i];
619 dst[i] = link->cur_buf->data[i] +
620 (y >> (i==1 || i==2 ? vsub : 0)) * link->cur_buf->linesize[i];
622 src[i] = dst[i] = NULL;
625 for (i = 0; i < 4; i++) {
627 av_image_get_linesize(link->format, link->cur_buf->video->w, i);
629 if (!src[i]) continue;
631 for (j = 0; j < h >> (i==1 || i==2 ? vsub : 0); j++) {
632 memcpy(dst[i], src[i], planew);
633 src[i] += link->src_buf->linesize[i];
634 dst[i] += link->cur_buf->linesize[i];
639 if (!(draw_slice = link->dstpad->draw_slice))
640 draw_slice = avfilter_default_draw_slice;
641 draw_slice(link, y, h, slice_dir);
644 int avfilter_process_command(AVFilterContext *filter, const char *cmd, const char *arg, char *res, int res_len, int flags)
646 if(!strcmp(cmd, "ping")){
647 av_strlcatf(res, res_len, "pong from:%s %s\n", filter->filter->name, filter->name);
649 }else if(filter->filter->process_command) {
650 return filter->filter->process_command(filter, cmd, arg, res, res_len, flags);
652 return AVERROR(ENOSYS);
655 void avfilter_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
657 void (*filter_samples)(AVFilterLink *, AVFilterBufferRef *);
658 AVFilterPad *dst = link->dstpad;
661 FF_DPRINTF_START(NULL, filter_samples); ff_dlog_link(NULL, link, 1);
663 if (!(filter_samples = dst->filter_samples))
664 filter_samples = avfilter_default_filter_samples;
666 /* prepare to copy the samples if the buffer has insufficient permissions */
667 if ((dst->min_perms & samplesref->perms) != dst->min_perms ||
668 dst->rej_perms & samplesref->perms) {
670 av_log(link->dst, AV_LOG_DEBUG,
671 "Copying audio data in avfilter (have perms %x, need %x, reject %x)\n",
672 samplesref->perms, link->dstpad->min_perms, link->dstpad->rej_perms);
674 link->cur_buf = avfilter_default_get_audio_buffer(link, dst->min_perms,
675 samplesref->audio->nb_samples);
676 link->cur_buf->pts = samplesref->pts;
677 link->cur_buf->audio->sample_rate = samplesref->audio->sample_rate;
679 /* Copy actual data into new samples buffer */
680 for (i = 0; samplesref->data[i]; i++)
681 memcpy(link->cur_buf->data[i], samplesref->data[i], samplesref->linesize[0]);
683 avfilter_unref_buffer(samplesref);
685 link->cur_buf = samplesref;
687 filter_samples(link, link->cur_buf);
690 #define MAX_REGISTERED_AVFILTERS_NB 128
692 static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1];
694 static int next_registered_avfilter_idx = 0;
696 AVFilter *avfilter_get_by_name(const char *name)
700 for (i = 0; registered_avfilters[i]; i++)
701 if (!strcmp(registered_avfilters[i]->name, name))
702 return registered_avfilters[i];
707 int avfilter_register(AVFilter *filter)
709 if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB) {
710 av_log(NULL, AV_LOG_ERROR,
711 "Maximum number of registered filters %d reached, "
712 "impossible to register filter with name '%s'\n",
713 MAX_REGISTERED_AVFILTERS_NB, filter->name);
714 return AVERROR(ENOMEM);
717 registered_avfilters[next_registered_avfilter_idx++] = filter;
721 AVFilter **av_filter_next(AVFilter **filter)
723 return filter ? ++filter : ®istered_avfilters[0];
726 void avfilter_uninit(void)
728 memset(registered_avfilters, 0, sizeof(registered_avfilters));
729 next_registered_avfilter_idx = 0;
732 static int pad_count(const AVFilterPad *pads)
736 for(count = 0; pads->name; count ++) pads ++;
740 static const char *filter_name(void *p)
742 AVFilterContext *filter = p;
743 return filter->filter->name;
746 static const AVClass avfilter_class = {
750 LIBAVUTIL_VERSION_INT,
753 int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
755 AVFilterContext *ret;
759 return AVERROR(EINVAL);
761 ret = av_mallocz(sizeof(AVFilterContext));
763 return AVERROR(ENOMEM);
765 ret->av_class = &avfilter_class;
766 ret->filter = filter;
767 ret->name = inst_name ? av_strdup(inst_name) : NULL;
768 if (filter->priv_size) {
769 ret->priv = av_mallocz(filter->priv_size);
774 ret->input_count = pad_count(filter->inputs);
775 if (ret->input_count) {
776 ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->input_count);
777 if (!ret->input_pads)
779 memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->input_count);
780 ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
785 ret->output_count = pad_count(filter->outputs);
786 if (ret->output_count) {
787 ret->output_pads = av_malloc(sizeof(AVFilterPad) * ret->output_count);
788 if (!ret->output_pads)
790 memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->output_count);
791 ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
800 av_freep(&ret->inputs);
801 av_freep(&ret->input_pads);
802 ret->input_count = 0;
803 av_freep(&ret->outputs);
804 av_freep(&ret->output_pads);
805 ret->output_count = 0;
806 av_freep(&ret->priv);
808 return AVERROR(ENOMEM);
811 void avfilter_free(AVFilterContext *filter)
816 if (filter->filter->uninit)
817 filter->filter->uninit(filter);
819 for (i = 0; i < filter->input_count; i++) {
820 if ((link = filter->inputs[i])) {
822 link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
823 avfilter_formats_unref(&link->in_formats);
824 avfilter_formats_unref(&link->out_formats);
826 avfilter_link_free(&link);
828 for (i = 0; i < filter->output_count; i++) {
829 if ((link = filter->outputs[i])) {
831 link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
832 avfilter_formats_unref(&link->in_formats);
833 avfilter_formats_unref(&link->out_formats);
835 avfilter_link_free(&link);
838 av_freep(&filter->name);
839 av_freep(&filter->input_pads);
840 av_freep(&filter->output_pads);
841 av_freep(&filter->inputs);
842 av_freep(&filter->outputs);
843 av_freep(&filter->priv);
844 while(filter->command_queue){
845 command_queue_pop(filter);
850 int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
854 if (filter->filter->init)
855 ret = filter->filter->init(filter, args, opaque);