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 av_assert0(LIBAVFILTER_VERSION_MICRO >= 100);
35 return LIBAVFILTER_VERSION_INT;
38 const char *avfilter_configuration(void)
40 return FFMPEG_CONFIGURATION;
43 const char *avfilter_license(void)
45 #define LICENSE_PREFIX "libavfilter license: "
46 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
49 static void command_queue_pop(AVFilterContext *filter)
51 AVFilterCommand *c= filter->command_queue;
53 av_freep(&c->command);
54 filter->command_queue= c->next;
58 AVFilterBufferRef *avfilter_ref_buffer(AVFilterBufferRef *ref, int pmask)
60 AVFilterBufferRef *ret = av_malloc(sizeof(AVFilterBufferRef));
64 if (ref->type == AVMEDIA_TYPE_VIDEO) {
65 ret->video = av_malloc(sizeof(AVFilterBufferRefVideoProps));
70 *ret->video = *ref->video;
71 } else if (ref->type == AVMEDIA_TYPE_AUDIO) {
72 ret->audio = av_malloc(sizeof(AVFilterBufferRefAudioProps));
77 *ret->audio = *ref->audio;
80 ret->buf->refcount ++;
84 static void free_pool(AVFilterPool *pool)
88 av_assert0(pool->refcount > 0);
90 for (i = 0; i < POOL_SIZE; i++) {
92 AVFilterBufferRef *picref = pool->pic[i];
93 /* free buffer: picrefs stored in the pool are not
94 * supposed to contain a free callback */
95 av_assert0(!picref->buf->refcount);
96 av_freep(&picref->buf->data[0]);
97 av_freep(&picref->buf);
99 av_freep(&picref->audio);
100 av_freep(&picref->video);
101 av_freep(&pool->pic[i]);
107 if (!--pool->refcount) {
108 av_assert0(!pool->count);
113 static void store_in_pool(AVFilterBufferRef *ref)
116 AVFilterPool *pool= ref->buf->priv;
118 av_assert0(ref->buf->data[0]);
119 av_assert0(pool->refcount>0);
121 if (pool->count == POOL_SIZE) {
122 AVFilterBufferRef *ref1 = pool->pic[0];
123 av_freep(&ref1->video);
124 av_freep(&ref1->audio);
125 av_freep(&ref1->buf->data[0]);
126 av_freep(&ref1->buf);
128 memmove(&pool->pic[0], &pool->pic[1], sizeof(void*)*(POOL_SIZE-1));
130 pool->pic[POOL_SIZE-1] = NULL;
133 for (i = 0; i < POOL_SIZE; i++) {
140 if (pool->draining) {
146 void avfilter_unref_buffer(AVFilterBufferRef *ref)
150 av_assert0(ref->buf->refcount > 0);
151 if (!(--ref->buf->refcount)) {
152 if (!ref->buf->free) {
156 ref->buf->free(ref->buf);
158 av_freep(&ref->video);
159 av_freep(&ref->audio);
163 void avfilter_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
164 AVFilterPad **pads, AVFilterLink ***links,
169 idx = FFMIN(idx, *count);
171 *pads = av_realloc(*pads, sizeof(AVFilterPad) * (*count + 1));
172 *links = av_realloc(*links, sizeof(AVFilterLink*) * (*count + 1));
173 memmove(*pads +idx+1, *pads +idx, sizeof(AVFilterPad) * (*count-idx));
174 memmove(*links+idx+1, *links+idx, sizeof(AVFilterLink*) * (*count-idx));
175 memcpy(*pads+idx, newpad, sizeof(AVFilterPad));
176 (*links)[idx] = NULL;
179 for (i = idx+1; i < *count; i++)
181 (*(unsigned *)((uint8_t *) *links[i] + padidx_off))++;
184 int avfilter_link(AVFilterContext *src, unsigned srcpad,
185 AVFilterContext *dst, unsigned dstpad)
189 if (src->output_count <= srcpad || dst->input_count <= dstpad ||
190 src->outputs[srcpad] || dst->inputs[dstpad])
193 if (src->output_pads[srcpad].type != dst->input_pads[dstpad].type) {
194 av_log(src, AV_LOG_ERROR,
195 "Media type mismatch between the '%s' filter output pad %d and the '%s' filter input pad %d\n",
196 src->name, srcpad, dst->name, dstpad);
197 return AVERROR(EINVAL);
200 src->outputs[srcpad] =
201 dst-> inputs[dstpad] = link = av_mallocz(sizeof(AVFilterLink));
205 link->srcpad = &src->output_pads[srcpad];
206 link->dstpad = &dst->input_pads[dstpad];
207 link->type = src->output_pads[srcpad].type;
208 assert(PIX_FMT_NONE == -1 && AV_SAMPLE_FMT_NONE == -1);
214 void avfilter_link_free(AVFilterLink **link)
220 free_pool((*link)->pool);
225 int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
226 unsigned filt_srcpad_idx, unsigned filt_dstpad_idx)
229 unsigned dstpad_idx = link->dstpad - link->dst->input_pads;
231 av_log(link->dst, AV_LOG_INFO, "auto-inserting filter '%s' "
232 "between the filter '%s' and the filter '%s'\n",
233 filt->name, link->src->name, link->dst->name);
235 link->dst->inputs[dstpad_idx] = NULL;
236 if ((ret = avfilter_link(filt, filt_dstpad_idx, link->dst, dstpad_idx)) < 0) {
237 /* failed to link output filter to new filter */
238 link->dst->inputs[dstpad_idx] = link;
242 /* re-hookup the link to the new destination filter we inserted */
244 link->dstpad = &filt->input_pads[filt_srcpad_idx];
245 filt->inputs[filt_srcpad_idx] = link;
247 /* if any information on supported media formats already exists on the
248 * link, we need to preserve that */
249 if (link->out_formats)
250 avfilter_formats_changeref(&link->out_formats,
251 &filt->outputs[filt_dstpad_idx]->out_formats);
252 if (link->out_chlayouts)
253 avfilter_formats_changeref(&link->out_chlayouts,
254 &filt->outputs[filt_dstpad_idx]->out_chlayouts);
255 if (link->out_packing)
256 avfilter_formats_changeref(&link->out_packing,
257 &filt->outputs[filt_dstpad_idx]->out_packing);
262 int avfilter_config_links(AVFilterContext *filter)
264 int (*config_link)(AVFilterLink *);
268 for (i = 0; i < filter->input_count; i ++) {
269 AVFilterLink *link = filter->inputs[i];
270 AVFilterLink *inlink = link->src->input_count ?
271 link->src->inputs[0] : NULL;
275 switch (link->init_state) {
278 case AVLINK_STARTINIT:
279 av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
282 link->init_state = AVLINK_STARTINIT;
284 if ((ret = avfilter_config_links(link->src)) < 0)
287 if (!(config_link = link->srcpad->config_props)) {
288 if (link->src->input_count != 1) {
289 av_log(link->src, AV_LOG_ERROR, "Source filters and filters "
290 "with more than one input "
291 "must set config_props() "
292 "callbacks on all outputs\n");
293 return AVERROR(EINVAL);
295 } else if ((ret = config_link(link)) < 0)
298 switch (link->type) {
299 case AVMEDIA_TYPE_VIDEO:
300 if (!link->time_base.num && !link->time_base.den)
301 link->time_base = inlink ? inlink->time_base : AV_TIME_BASE_Q;
303 if (!link->sample_aspect_ratio.num && !link->sample_aspect_ratio.den)
304 link->sample_aspect_ratio = inlink ?
305 inlink->sample_aspect_ratio : (AVRational){1,1};
312 } else if (!link->w || !link->h) {
313 av_log(link->src, AV_LOG_ERROR,
314 "Video source filters must set their output link's "
315 "width and height\n");
316 return AVERROR(EINVAL);
320 case AVMEDIA_TYPE_AUDIO:
322 if (!link->sample_rate)
323 link->sample_rate = inlink->sample_rate;
324 if (!link->time_base.num && !link->time_base.den)
325 link->time_base = inlink->time_base;
326 } else if (!link->sample_rate) {
327 av_log(link->src, AV_LOG_ERROR,
328 "Audio source filters must set their output link's "
330 return AVERROR(EINVAL);
333 if (!link->time_base.num && !link->time_base.den)
334 link->time_base = (AVRational) {1, link->sample_rate};
337 if ((config_link = link->dstpad->config_props))
338 if ((ret = config_link(link)) < 0)
341 link->init_state = AVLINK_INIT;
348 static char *ff_get_ref_perms_string(char *buf, size_t buf_size, int perms)
350 snprintf(buf, buf_size, "%s%s%s%s%s%s",
351 perms & AV_PERM_READ ? "r" : "",
352 perms & AV_PERM_WRITE ? "w" : "",
353 perms & AV_PERM_PRESERVE ? "p" : "",
354 perms & AV_PERM_REUSE ? "u" : "",
355 perms & AV_PERM_REUSE2 ? "U" : "",
356 perms & AV_PERM_NEG_LINESIZES ? "n" : "");
360 static void ff_dlog_ref(void *ctx, AVFilterBufferRef *ref, int end)
362 av_unused char buf[16];
364 "ref[%p buf:%p refcount:%d perms:%s data:%p linesize[%d, %d, %d, %d] pts:%"PRId64" pos:%"PRId64,
365 ref, ref->buf, ref->buf->refcount, ff_get_ref_perms_string(buf, sizeof(buf), ref->perms), ref->data[0],
366 ref->linesize[0], ref->linesize[1], ref->linesize[2], ref->linesize[3],
370 av_dlog(ctx, " a:%d/%d s:%dx%d i:%c iskey:%d type:%c",
371 ref->video->sample_aspect_ratio.num, ref->video->sample_aspect_ratio.den,
372 ref->video->w, ref->video->h,
373 !ref->video->interlaced ? 'P' : /* Progressive */
374 ref->video->top_field_first ? 'T' : 'B', /* Top / Bottom */
375 ref->video->key_frame,
376 av_get_picture_type_char(ref->video->pict_type));
379 av_dlog(ctx, " cl:%"PRId64"d n:%d r:%d p:%d",
380 ref->audio->channel_layout,
381 ref->audio->nb_samples,
382 ref->audio->sample_rate,
386 av_dlog(ctx, "]%s", end ? "\n" : "");
389 static void ff_dlog_link(void *ctx, AVFilterLink *link, int end)
391 if (link->type == AVMEDIA_TYPE_VIDEO) {
393 "link[%p s:%dx%d fmt:%s %s->%s]%s",
394 link, link->w, link->h,
395 av_pix_fmt_descriptors[link->format].name,
396 link->src ? link->src->filter->name : "",
397 link->dst ? link->dst->filter->name : "",
401 av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout);
404 "link[%p r:%d cl:%s fmt:%s %s->%s]%s",
405 link, (int)link->sample_rate, buf,
406 av_get_sample_fmt_name(link->format),
407 link->src ? link->src->filter->name : "",
408 link->dst ? link->dst->filter->name : "",
413 #define FF_DPRINTF_START(ctx, func) av_dlog(NULL, "%-16s: ", #func)
415 AVFilterBufferRef *avfilter_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
417 AVFilterBufferRef *ret = NULL;
419 av_unused char buf[16];
420 FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0);
421 av_dlog(NULL, " perms:%s w:%d h:%d\n", ff_get_ref_perms_string(buf, sizeof(buf), perms), w, h);
423 if (link->dstpad->get_video_buffer)
424 ret = link->dstpad->get_video_buffer(link, perms, w, h);
427 ret = avfilter_default_get_video_buffer(link, perms, w, h);
430 ret->type = AVMEDIA_TYPE_VIDEO;
432 FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " returning "); ff_dlog_ref(NULL, ret, 1);
438 avfilter_get_video_buffer_ref_from_arrays(uint8_t * const data[4], const int linesize[4], int perms,
439 int w, int h, enum PixelFormat format)
441 AVFilterBuffer *pic = av_mallocz(sizeof(AVFilterBuffer));
442 AVFilterBufferRef *picref = av_mallocz(sizeof(AVFilterBufferRef));
448 picref->buf->free = ff_avfilter_default_free_buffer;
449 if (!(picref->video = av_mallocz(sizeof(AVFilterBufferRefVideoProps))))
452 pic->w = picref->video->w = w;
453 pic->h = picref->video->h = h;
455 /* make sure the buffer gets read permission or it's useless for output */
456 picref->perms = perms | AV_PERM_READ;
459 picref->type = AVMEDIA_TYPE_VIDEO;
460 pic->format = picref->format = format;
462 memcpy(pic->data, data, 4*sizeof(data[0]));
463 memcpy(pic->linesize, linesize, 4*sizeof(linesize[0]));
464 memcpy(picref->data, pic->data, sizeof(picref->data));
465 memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
470 if (picref && picref->video)
471 av_free(picref->video);
477 AVFilterBufferRef *avfilter_get_audio_buffer(AVFilterLink *link,
478 int perms, int nb_samples)
480 AVFilterBufferRef *ret = NULL;
482 if (link->dstpad->get_audio_buffer)
483 ret = link->dstpad->get_audio_buffer(link, perms, nb_samples);
486 ret = avfilter_default_get_audio_buffer(link, perms, nb_samples);
489 ret->type = AVMEDIA_TYPE_AUDIO;
495 avfilter_get_audio_buffer_ref_from_arrays(uint8_t *data[8], int linesize[8], int perms,
496 int nb_samples, enum AVSampleFormat sample_fmt,
497 uint64_t channel_layout, int planar)
499 AVFilterBuffer *samples = av_mallocz(sizeof(AVFilterBuffer));
500 AVFilterBufferRef *samplesref = av_mallocz(sizeof(AVFilterBufferRef));
502 if (!samples || !samplesref)
505 samplesref->buf = samples;
506 samplesref->buf->free = ff_avfilter_default_free_buffer;
507 if (!(samplesref->audio = av_mallocz(sizeof(AVFilterBufferRefAudioProps))))
510 samplesref->audio->nb_samples = nb_samples;
511 samplesref->audio->channel_layout = channel_layout;
512 samplesref->audio->planar = planar;
514 /* make sure the buffer gets read permission or it's useless for output */
515 samplesref->perms = perms | AV_PERM_READ;
517 samples->refcount = 1;
518 samplesref->type = AVMEDIA_TYPE_AUDIO;
519 samplesref->format = sample_fmt;
521 memcpy(samples->data, data, sizeof(samples->data));
522 memcpy(samples->linesize, linesize, sizeof(samples->linesize));
523 memcpy(samplesref->data, data, sizeof(samplesref->data));
524 memcpy(samplesref->linesize, linesize, sizeof(samplesref->linesize));
529 if (samplesref && samplesref->audio)
530 av_freep(&samplesref->audio);
531 av_freep(&samplesref);
536 int avfilter_request_frame(AVFilterLink *link)
538 FF_DPRINTF_START(NULL, request_frame); ff_dlog_link(NULL, link, 1);
540 if (link->srcpad->request_frame)
541 return link->srcpad->request_frame(link);
542 else if (link->src->inputs[0])
543 return avfilter_request_frame(link->src->inputs[0]);
547 int avfilter_poll_frame(AVFilterLink *link)
549 int i, min = INT_MAX;
551 if (link->srcpad->poll_frame)
552 return link->srcpad->poll_frame(link);
554 for (i = 0; i < link->src->input_count; i++) {
556 if (!link->src->inputs[i])
558 val = avfilter_poll_frame(link->src->inputs[i]);
559 min = FFMIN(min, val);
565 /* XXX: should we do the duplicating of the picture ref here, instead of
566 * forcing the source filter to do it? */
567 void avfilter_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
569 void (*start_frame)(AVFilterLink *, AVFilterBufferRef *);
570 AVFilterPad *dst = link->dstpad;
571 int perms = picref->perms;
572 AVFilterCommand *cmd= link->dst->command_queue;
574 FF_DPRINTF_START(NULL, start_frame); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " "); ff_dlog_ref(NULL, picref, 1);
576 if (!(start_frame = dst->start_frame))
577 start_frame = avfilter_default_start_frame;
579 if (picref->linesize[0] < 0)
580 perms |= AV_PERM_NEG_LINESIZES;
581 /* prepare to copy the picture if it has insufficient permissions */
582 if ((dst->min_perms & perms) != dst->min_perms || dst->rej_perms & perms) {
583 av_log(link->dst, AV_LOG_DEBUG,
584 "frame copy needed (have perms %x, need %x, reject %x)\n",
586 link->dstpad->min_perms, link->dstpad->rej_perms);
588 link->cur_buf = avfilter_get_video_buffer(link, dst->min_perms, link->w, link->h);
589 link->src_buf = picref;
590 avfilter_copy_buffer_ref_props(link->cur_buf, link->src_buf);
593 link->cur_buf = picref;
595 while(cmd && cmd->time <= picref->pts * av_q2d(link->time_base)){
596 av_log(link->dst, AV_LOG_DEBUG,
597 "Processing command time:%f command:%s arg:%s\n",
598 cmd->time, cmd->command, cmd->arg);
599 avfilter_process_command(link->dst, cmd->command, cmd->arg, 0, 0, cmd->flags);
600 command_queue_pop(link->dst);
601 cmd= link->dst->command_queue;
604 start_frame(link, link->cur_buf);
607 void avfilter_end_frame(AVFilterLink *link)
609 void (*end_frame)(AVFilterLink *);
611 if (!(end_frame = link->dstpad->end_frame))
612 end_frame = avfilter_default_end_frame;
616 /* unreference the source picture if we're feeding the destination filter
617 * a copied version dues to permission issues */
619 avfilter_unref_buffer(link->src_buf);
620 link->src_buf = NULL;
624 void avfilter_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
626 uint8_t *src[4], *dst[4];
628 void (*draw_slice)(AVFilterLink *, int, int, int);
630 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);
632 /* copy the slice if needed for permission reasons */
634 vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
636 for (i = 0; i < 4; i++) {
637 if (link->src_buf->data[i]) {
638 src[i] = link->src_buf-> data[i] +
639 (y >> (i==1 || i==2 ? vsub : 0)) * link->src_buf-> linesize[i];
640 dst[i] = link->cur_buf->data[i] +
641 (y >> (i==1 || i==2 ? vsub : 0)) * link->cur_buf->linesize[i];
643 src[i] = dst[i] = NULL;
646 for (i = 0; i < 4; i++) {
648 av_image_get_linesize(link->format, link->cur_buf->video->w, i);
650 if (!src[i]) continue;
652 for (j = 0; j < h >> (i==1 || i==2 ? vsub : 0); j++) {
653 memcpy(dst[i], src[i], planew);
654 src[i] += link->src_buf->linesize[i];
655 dst[i] += link->cur_buf->linesize[i];
660 if (!(draw_slice = link->dstpad->draw_slice))
661 draw_slice = avfilter_default_draw_slice;
662 draw_slice(link, y, h, slice_dir);
665 int avfilter_process_command(AVFilterContext *filter, const char *cmd, const char *arg, char *res, int res_len, int flags)
667 if(!strcmp(cmd, "ping")){
668 av_strlcatf(res, res_len, "pong from:%s %s\n", filter->filter->name, filter->name);
670 }else if(filter->filter->process_command) {
671 return filter->filter->process_command(filter, cmd, arg, res, res_len, flags);
673 return AVERROR(ENOSYS);
676 void avfilter_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
678 void (*filter_samples)(AVFilterLink *, AVFilterBufferRef *);
679 AVFilterPad *dst = link->dstpad;
682 FF_DPRINTF_START(NULL, filter_samples); ff_dlog_link(NULL, link, 1);
684 if (!(filter_samples = dst->filter_samples))
685 filter_samples = avfilter_default_filter_samples;
687 /* prepare to copy the samples if the buffer has insufficient permissions */
688 if ((dst->min_perms & samplesref->perms) != dst->min_perms ||
689 dst->rej_perms & samplesref->perms) {
691 av_log(link->dst, AV_LOG_DEBUG,
692 "Copying audio data in avfilter (have perms %x, need %x, reject %x)\n",
693 samplesref->perms, link->dstpad->min_perms, link->dstpad->rej_perms);
695 link->cur_buf = avfilter_default_get_audio_buffer(link, dst->min_perms,
696 samplesref->audio->nb_samples);
697 link->cur_buf->pts = samplesref->pts;
698 link->cur_buf->audio->sample_rate = samplesref->audio->sample_rate;
700 /* Copy actual data into new samples buffer */
701 for (i = 0; samplesref->data[i] && i < 8; i++)
702 memcpy(link->cur_buf->data[i], samplesref->data[i], samplesref->linesize[0]);
704 avfilter_unref_buffer(samplesref);
706 link->cur_buf = samplesref;
708 filter_samples(link, link->cur_buf);
711 #define MAX_REGISTERED_AVFILTERS_NB 128
713 static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1];
715 static int next_registered_avfilter_idx = 0;
717 AVFilter *avfilter_get_by_name(const char *name)
721 for (i = 0; registered_avfilters[i]; i++)
722 if (!strcmp(registered_avfilters[i]->name, name))
723 return registered_avfilters[i];
728 int avfilter_register(AVFilter *filter)
730 if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB) {
731 av_log(NULL, AV_LOG_ERROR,
732 "Maximum number of registered filters %d reached, "
733 "impossible to register filter with name '%s'\n",
734 MAX_REGISTERED_AVFILTERS_NB, filter->name);
735 return AVERROR(ENOMEM);
738 registered_avfilters[next_registered_avfilter_idx++] = filter;
742 AVFilter **av_filter_next(AVFilter **filter)
744 return filter ? ++filter : ®istered_avfilters[0];
747 void avfilter_uninit(void)
749 memset(registered_avfilters, 0, sizeof(registered_avfilters));
750 next_registered_avfilter_idx = 0;
753 static int pad_count(const AVFilterPad *pads)
757 for(count = 0; pads->name; count ++) pads ++;
761 static const char *filter_name(void *p)
763 AVFilterContext *filter = p;
764 return filter->filter->name;
767 static const AVClass avfilter_class = {
771 LIBAVUTIL_VERSION_INT,
774 int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
776 AVFilterContext *ret;
780 return AVERROR(EINVAL);
782 ret = av_mallocz(sizeof(AVFilterContext));
784 return AVERROR(ENOMEM);
786 ret->av_class = &avfilter_class;
787 ret->filter = filter;
788 ret->name = inst_name ? av_strdup(inst_name) : NULL;
789 if (filter->priv_size) {
790 ret->priv = av_mallocz(filter->priv_size);
795 ret->input_count = pad_count(filter->inputs);
796 if (ret->input_count) {
797 ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->input_count);
798 if (!ret->input_pads)
800 memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->input_count);
801 ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
806 ret->output_count = pad_count(filter->outputs);
807 if (ret->output_count) {
808 ret->output_pads = av_malloc(sizeof(AVFilterPad) * ret->output_count);
809 if (!ret->output_pads)
811 memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->output_count);
812 ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
821 av_freep(&ret->inputs);
822 av_freep(&ret->input_pads);
823 ret->input_count = 0;
824 av_freep(&ret->outputs);
825 av_freep(&ret->output_pads);
826 ret->output_count = 0;
827 av_freep(&ret->priv);
829 return AVERROR(ENOMEM);
832 void avfilter_free(AVFilterContext *filter)
837 if (filter->filter->uninit)
838 filter->filter->uninit(filter);
840 for (i = 0; i < filter->input_count; i++) {
841 if ((link = filter->inputs[i])) {
843 link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
844 avfilter_formats_unref(&link->in_formats);
845 avfilter_formats_unref(&link->out_formats);
847 avfilter_link_free(&link);
849 for (i = 0; i < filter->output_count; i++) {
850 if ((link = filter->outputs[i])) {
852 link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
853 avfilter_formats_unref(&link->in_formats);
854 avfilter_formats_unref(&link->out_formats);
856 avfilter_link_free(&link);
859 av_freep(&filter->name);
860 av_freep(&filter->input_pads);
861 av_freep(&filter->output_pads);
862 av_freep(&filter->inputs);
863 av_freep(&filter->outputs);
864 av_freep(&filter->priv);
865 while(filter->command_queue){
866 command_queue_pop(filter);
871 int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
875 if (filter->filter->init)
876 ret = filter->filter->init(filter, args, opaque);