diff options
Diffstat (limited to 'libavfilter/avfilter.c')
-rw-r--r-- | libavfilter/avfilter.c | 263 |
1 files changed, 220 insertions, 43 deletions
diff --git a/libavfilter/avfilter.c b/libavfilter/avfilter.c index 77b3633..3bac29d 100644 --- a/libavfilter/avfilter.c +++ b/libavfilter/avfilter.c @@ -2,47 +2,98 @@ * filter layer * Copyright (c) 2007 Bobby Bingham * - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * FFmpeg is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * - * Libav is distributed in the hope that it will be useful, + * FFmpeg is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -/* #define DEBUG */ - #include "libavutil/common.h" #include "libavutil/pixdesc.h" #include "libavutil/rational.h" #include "libavutil/audioconvert.h" +#include "libavutil/avassert.h" +#include "libavutil/avstring.h" #include "avfilter.h" #include "formats.h" #include "internal.h" +#include "audio.h" + +char *ff_get_ref_perms_string(char *buf, size_t buf_size, int perms) +{ + snprintf(buf, buf_size, "%s%s%s%s%s%s", + perms & AV_PERM_READ ? "r" : "", + perms & AV_PERM_WRITE ? "w" : "", + perms & AV_PERM_PRESERVE ? "p" : "", + perms & AV_PERM_REUSE ? "u" : "", + perms & AV_PERM_REUSE2 ? "U" : "", + perms & AV_PERM_NEG_LINESIZES ? "n" : ""); + return buf; +} + +void ff_tlog_ref(void *ctx, AVFilterBufferRef *ref, int end) +{ + av_unused char buf[16]; + ff_tlog(ctx, + "ref[%p buf:%p refcount:%d perms:%s data:%p linesize[%d, %d, %d, %d] pts:%"PRId64" pos:%"PRId64, + ref, ref->buf, ref->buf->refcount, ff_get_ref_perms_string(buf, sizeof(buf), ref->perms), ref->data[0], + ref->linesize[0], ref->linesize[1], ref->linesize[2], ref->linesize[3], + ref->pts, ref->pos); + + if (ref->video) { + ff_tlog(ctx, " a:%d/%d s:%dx%d i:%c iskey:%d type:%c", + ref->video->sample_aspect_ratio.num, ref->video->sample_aspect_ratio.den, + ref->video->w, ref->video->h, + !ref->video->interlaced ? 'P' : /* Progressive */ + ref->video->top_field_first ? 'T' : 'B', /* Top / Bottom */ + ref->video->key_frame, + av_get_picture_type_char(ref->video->pict_type)); + } + if (ref->audio) { + ff_tlog(ctx, " cl:%"PRId64"d n:%d r:%d", + ref->audio->channel_layout, + ref->audio->nb_samples, + ref->audio->sample_rate); + } + + ff_tlog(ctx, "]%s", end ? "\n" : ""); +} unsigned avfilter_version(void) { + av_assert0(LIBAVFILTER_VERSION_MICRO >= 100); return LIBAVFILTER_VERSION_INT; } const char *avfilter_configuration(void) { - return LIBAV_CONFIGURATION; + return FFMPEG_CONFIGURATION; } const char *avfilter_license(void) { #define LICENSE_PREFIX "libavfilter license: " - return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1; + return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1; +} + +void ff_command_queue_pop(AVFilterContext *filter) +{ + AVFilterCommand *c= filter->command_queue; + av_freep(&c->arg); + av_freep(&c->command); + filter->command_queue= c->next; + av_free(c); } void ff_insert_pad(unsigned idx, unsigned *count, size_t padidx_off, @@ -77,8 +128,9 @@ int avfilter_link(AVFilterContext *src, unsigned srcpad, if (src->output_pads[srcpad].type != dst->input_pads[dstpad].type) { av_log(src, AV_LOG_ERROR, - "Media type mismatch between the '%s' filter output pad %d and the '%s' filter input pad %d\n", - src->name, srcpad, dst->name, dstpad); + "Media type mismatch between the '%s' filter output pad %d (%s) and the '%s' filter input pad %d (%s)\n", + src->name, srcpad, (char *)av_x_if_null(av_get_media_type_string(src->output_pads[srcpad].type), "?"), + dst->name, dstpad, (char *)av_x_if_null(av_get_media_type_string(dst-> input_pads[dstpad].type), "?")); return AVERROR(EINVAL); } @@ -90,12 +142,30 @@ int avfilter_link(AVFilterContext *src, unsigned srcpad, link->srcpad = &src->output_pads[srcpad]; link->dstpad = &dst->input_pads[dstpad]; link->type = src->output_pads[srcpad].type; - assert(PIX_FMT_NONE == -1 && AV_SAMPLE_FMT_NONE == -1); + av_assert0(PIX_FMT_NONE == -1 && AV_SAMPLE_FMT_NONE == -1); link->format = -1; return 0; } +void avfilter_link_free(AVFilterLink **link) +{ + if (!*link) + return; + + if ((*link)->pool) + ff_free_pool((*link)->pool); + + avfilter_unref_bufferp(&(*link)->partial_buf); + + av_freep(link); +} + +void avfilter_link_set_closed(AVFilterLink *link, int closed) +{ + link->closed = closed; +} + int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt, unsigned filt_srcpad_idx, unsigned filt_dstpad_idx) { @@ -123,6 +193,7 @@ int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt, if (link->out_formats) ff_formats_changeref(&link->out_formats, &filt->outputs[filt_dstpad_idx]->out_formats); + if (link->out_samplerates) ff_formats_changeref(&link->out_samplerates, &filt->outputs[filt_dstpad_idx]->out_samplerates); @@ -141,9 +212,13 @@ int avfilter_config_links(AVFilterContext *filter) for (i = 0; i < filter->nb_inputs; i ++) { AVFilterLink *link = filter->inputs[i]; + AVFilterLink *inlink = link->src->nb_inputs ? + link->src->inputs[0] : NULL; if (!link) continue; + link->current_pts = AV_NOPTS_VALUE; + switch (link->init_state) { case AVLINK_INIT: continue; @@ -171,26 +246,48 @@ int avfilter_config_links(AVFilterContext *filter) return ret; } - if (link->time_base.num == 0 && link->time_base.den == 0) - link->time_base = link->src && link->src->nb_inputs ? - link->src->inputs[0]->time_base : AV_TIME_BASE_Q; + switch (link->type) { + case AVMEDIA_TYPE_VIDEO: + if (!link->time_base.num && !link->time_base.den) + link->time_base = inlink ? inlink->time_base : AV_TIME_BASE_Q; - if (link->type == AVMEDIA_TYPE_VIDEO) { if (!link->sample_aspect_ratio.num && !link->sample_aspect_ratio.den) - link->sample_aspect_ratio = link->src->nb_inputs ? - link->src->inputs[0]->sample_aspect_ratio : (AVRational){1,1}; + link->sample_aspect_ratio = inlink ? + inlink->sample_aspect_ratio : (AVRational){1,1}; - if (link->src->nb_inputs) { + if (inlink && !link->frame_rate.num && !link->frame_rate.den) + link->frame_rate = inlink->frame_rate; + + if (inlink) { if (!link->w) - link->w = link->src->inputs[0]->w; + link->w = inlink->w; if (!link->h) - link->h = link->src->inputs[0]->h; + link->h = inlink->h; } else if (!link->w || !link->h) { av_log(link->src, AV_LOG_ERROR, "Video source filters must set their output link's " "width and height\n"); return AVERROR(EINVAL); } + break; + + case AVMEDIA_TYPE_AUDIO: + if (inlink) { + if (!link->sample_rate) + link->sample_rate = inlink->sample_rate; + if (!link->time_base.num && !link->time_base.den) + link->time_base = inlink->time_base; + if (!link->channel_layout) + link->channel_layout = inlink->channel_layout; + } else if (!link->sample_rate) { + av_log(link->src, AV_LOG_ERROR, + "Audio source filters must set their output link's " + "sample_rate\n"); + return AVERROR(EINVAL); + } + + if (!link->time_base.num && !link->time_base.den) + link->time_base = (AVRational) {1, link->sample_rate}; } if ((config_link = link->dstpad->config_props)) @@ -208,11 +305,11 @@ int avfilter_config_links(AVFilterContext *filter) return 0; } -void ff_dlog_link(void *ctx, AVFilterLink *link, int end) +void ff_tlog_link(void *ctx, AVFilterLink *link, int end) { if (link->type == AVMEDIA_TYPE_VIDEO) { - av_dlog(ctx, - "link[%p s:%dx%d fmt:%-16s %-16s->%-16s]%s", + ff_tlog(ctx, + "link[%p s:%dx%d fmt:%s %s->%s]%s", link, link->w, link->h, av_pix_fmt_descriptors[link->format].name, link->src ? link->src->filter->name : "", @@ -222,9 +319,9 @@ void ff_dlog_link(void *ctx, AVFilterLink *link, int end) char buf[128]; av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout); - av_dlog(ctx, - "link[%p r:%d cl:%s fmt:%-16s %-16s->%-16s]%s", - link, link->sample_rate, buf, + ff_tlog(ctx, + "link[%p r:%d cl:%s fmt:%s %s->%s]%s", + link, (int)link->sample_rate, buf, av_get_sample_fmt_name(link->format), link->src ? link->src->filter->name : "", link->dst ? link->dst->filter->name : "", @@ -234,13 +331,24 @@ void ff_dlog_link(void *ctx, AVFilterLink *link, int end) int ff_request_frame(AVFilterLink *link) { - FF_DPRINTF_START(NULL, request_frame); ff_dlog_link(NULL, link, 1); + int ret = -1; + FF_TPRINTF_START(NULL, request_frame); ff_tlog_link(NULL, link, 1); + if (link->closed) + return AVERROR_EOF; if (link->srcpad->request_frame) - return link->srcpad->request_frame(link); + ret = link->srcpad->request_frame(link); else if (link->src->inputs[0]) - return ff_request_frame(link->src->inputs[0]); - else return -1; + ret = ff_request_frame(link->src->inputs[0]); + if (ret == AVERROR_EOF && link->partial_buf) { + AVFilterBufferRef *pbuf = link->partial_buf; + link->partial_buf = NULL; + ff_filter_samples_framed(link, pbuf); + return 0; + } + if (ret == AVERROR_EOF) + link->closed = 1; + return ret; } int ff_poll_frame(AVFilterLink *link) @@ -261,7 +369,28 @@ int ff_poll_frame(AVFilterLink *link) return min; } -#define MAX_REGISTERED_AVFILTERS_NB 64 +void ff_update_link_current_pts(AVFilterLink *link, int64_t pts) +{ + if (pts == AV_NOPTS_VALUE) + return; + link->current_pts = av_rescale_q(pts, link->time_base, AV_TIME_BASE_Q); + /* TODO use duration */ + if (link->graph && link->age_index >= 0) + ff_avfilter_graph_update_heap(link->graph, link); +} + +int avfilter_process_command(AVFilterContext *filter, const char *cmd, const char *arg, char *res, int res_len, int flags) +{ + if(!strcmp(cmd, "ping")){ + av_strlcatf(res, res_len, "pong from:%s %s\n", filter->filter->name, filter->name); + return 0; + }else if(filter->filter->process_command) { + return filter->filter->process_command(filter, cmd, arg, res, res_len, flags); + } + return AVERROR(ENOSYS); +} + +#define MAX_REGISTERED_AVFILTERS_NB 128 static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1]; @@ -280,8 +409,13 @@ AVFilter *avfilter_get_by_name(const char *name) int avfilter_register(AVFilter *filter) { - if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB) - return -1; + if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB) { + av_log(NULL, AV_LOG_ERROR, + "Maximum number of registered filters %d reached, " + "impossible to register filter with name '%s'\n", + MAX_REGISTERED_AVFILTERS_NB, filter->name); + return AVERROR(ENOMEM); + } registered_avfilters[next_registered_avfilter_idx++] = filter; return 0; @@ -306,19 +440,54 @@ static int pad_count(const AVFilterPad *pads) return count; } -static const char *filter_name(void *p) +static const char *default_filter_name(void *filter_ctx) { - AVFilterContext *filter = p; - return filter->filter->name; + AVFilterContext *ctx = filter_ctx; + return ctx->name ? ctx->name : ctx->filter->name; +} + +static void *filter_child_next(void *obj, void *prev) +{ + AVFilterContext *ctx = obj; + if (!prev && ctx->filter && ctx->filter->priv_class) + return ctx->priv; + return NULL; +} + +static const AVClass *filter_child_class_next(const AVClass *prev) +{ + AVFilter **filter_ptr = NULL; + + /* find the filter that corresponds to prev */ + while (prev && *(filter_ptr = av_filter_next(filter_ptr))) + if ((*filter_ptr)->priv_class == prev) + break; + + /* could not find filter corresponding to prev */ + if (prev && !(*filter_ptr)) + return NULL; + + /* find next filter with specific options */ + while (*(filter_ptr = av_filter_next(filter_ptr))) + if ((*filter_ptr)->priv_class) + return (*filter_ptr)->priv_class; + return NULL; } static const AVClass avfilter_class = { - "AVFilter", - filter_name, - NULL, - LIBAVUTIL_VERSION_INT, + .class_name = "AVFilter", + .item_name = default_filter_name, + .version = LIBAVUTIL_VERSION_INT, + .category = AV_CLASS_CATEGORY_FILTER, + .child_next = filter_child_next, + .child_class_next = filter_child_class_next, }; +const AVClass *avfilter_get_class(void) +{ + return &avfilter_class; +} + int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name) { AVFilterContext *ret; @@ -386,6 +555,9 @@ void avfilter_free(AVFilterContext *filter) int i; AVFilterLink *link; + if (!filter) + return; + if (filter->filter->uninit) filter->filter->uninit(filter); @@ -400,7 +572,7 @@ void avfilter_free(AVFilterContext *filter) ff_channel_layouts_unref(&link->in_channel_layouts); ff_channel_layouts_unref(&link->out_channel_layouts); } - av_freep(&link); + avfilter_link_free(&link); } for (i = 0; i < filter->nb_outputs; i++) { if ((link = filter->outputs[i])) { @@ -413,7 +585,7 @@ void avfilter_free(AVFilterContext *filter) ff_channel_layouts_unref(&link->in_channel_layouts); ff_channel_layouts_unref(&link->out_channel_layouts); } - av_freep(&link); + avfilter_link_free(&link); } av_freep(&filter->name); @@ -422,6 +594,9 @@ void avfilter_free(AVFilterContext *filter) av_freep(&filter->inputs); av_freep(&filter->outputs); av_freep(&filter->priv); + while(filter->command_queue){ + ff_command_queue_pop(filter); + } av_free(filter); } @@ -429,7 +604,9 @@ int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque { int ret=0; - if (filter->filter->init) + if (filter->filter->init_opaque) + ret = filter->filter->init_opaque(filter, args, opaque); + else if (filter->filter->init) ret = filter->filter->init(filter, args); return ret; } |