diff options
Diffstat (limited to 'libavfilter/avfilter.c')
-rw-r--r-- | libavfilter/avfilter.c | 374 |
1 files changed, 325 insertions, 49 deletions
diff --git a/libavfilter/avfilter.c b/libavfilter/avfilter.c index 93302ccd23..4edd5be30f 100644 --- a/libavfilter/avfilter.c +++ b/libavfilter/avfilter.c @@ -2,25 +2,25 @@ * 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/avassert.h" +#include "libavutil/avstring.h" #include "libavutil/channel_layout.h" #include "libavutil/common.h" #include "libavutil/imgutils.h" @@ -32,21 +32,73 @@ #include "avfilter.h" #include "formats.h" #include "internal.h" -#include "video.h" +#include "audio.h" + +static int ff_filter_frame_framed(AVFilterLink *link, AVFilterBufferRef *frame); + +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, @@ -81,8 +133,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); } @@ -94,12 +147,35 @@ 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(AV_PIX_FMT_NONE == -1 && AV_SAMPLE_FMT_NONE == -1); + av_assert0(AV_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); +} + +int avfilter_link_get_channels(AVFilterLink *link) +{ + return link->channels; +} + +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) { @@ -127,6 +203,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); @@ -145,9 +222,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; @@ -175,26 +256,39 @@ 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 (inlink && !link->frame_rate.num && !link->frame_rate.den) + link->frame_rate = inlink->frame_rate; - if (link->src->nb_inputs) { + 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->time_base.num && !link->time_base.den) + link->time_base = inlink->time_base; + } + + if (!link->time_base.num && !link->time_base.den) + link->time_base = (AVRational) {1, link->sample_rate}; } if ((config_link = link->dstpad->config_props)) @@ -212,11 +306,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_get_pix_fmt_name(link->format), link->src ? link->src->filter->name : "", @@ -226,9 +320,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 : "", @@ -238,13 +332,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_frame_framed(link, pbuf); + return 0; + } + if (ret == AVERROR_EOF) + link->closed = 1; + return ret; } int ff_poll_frame(AVFilterLink *link) @@ -265,7 +370,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]; @@ -284,8 +410,21 @@ AVFilter *avfilter_get_by_name(const char *name) int avfilter_register(AVFilter *filter) { - if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB) - return -1; + int i; + + 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); + } + + for(i=0; filter->inputs && filter->inputs[i].name; i++) { + const AVFilterPad *input = &filter->inputs[i]; + av_assert0( !input->filter_frame + || (!input->start_frame && !input->end_frame)); + } registered_avfilters[next_registered_avfilter_idx++] = filter; return 0; @@ -313,19 +452,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; @@ -393,6 +567,9 @@ void avfilter_free(AVFilterContext *filter) int i; AVFilterLink *link; + if (!filter) + return; + if (filter->filter->uninit) filter->filter->uninit(filter); @@ -407,7 +584,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])) { @@ -420,7 +597,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); @@ -429,6 +606,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); } @@ -436,7 +616,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; } @@ -456,21 +638,31 @@ static int default_filter_frame(AVFilterLink *link, AVFilterBufferRef *frame) return ff_filter_frame(link->dst->outputs[0], frame); } -int ff_filter_frame(AVFilterLink *link, AVFilterBufferRef *frame) +static int ff_filter_frame_framed(AVFilterLink *link, AVFilterBufferRef *frame) { int (*filter_frame)(AVFilterLink *, AVFilterBufferRef *); + AVFilterPad *src = link->srcpad; AVFilterPad *dst = link->dstpad; AVFilterBufferRef *out; - int perms = frame->perms; + int perms, ret; + AVFilterCommand *cmd= link->dst->command_queue; + int64_t pts; - FF_DPRINTF_START(NULL, filter_frame); - ff_dlog_link(NULL, link, 1); + if (link->closed) { + avfilter_unref_buffer(frame); + return AVERROR_EOF; + } if (!(filter_frame = dst->filter_frame)) filter_frame = default_filter_frame; + av_assert1((frame->perms & src->min_perms) == src->min_perms); + frame->perms &= ~ src->rej_perms; + perms = frame->perms; + if (frame->linesize[0] < 0) perms |= AV_PERM_NEG_LINESIZES; + /* prepare to copy the frame if the buffer has insufficient permissions */ if ((dst->min_perms & perms) != dst->min_perms || dst->rej_perms & perms) { @@ -478,6 +670,7 @@ int ff_filter_frame(AVFilterLink *link, AVFilterBufferRef *frame) "Copying data in avfilter (have perms %x, need %x, reject %x)\n", perms, link->dstpad->min_perms, link->dstpad->rej_perms); + /* Maybe use ff_copy_buffer_ref instead? */ switch (link->type) { case AVMEDIA_TYPE_VIDEO: out = ff_get_video_buffer(link, dst->min_perms, @@ -513,5 +706,88 @@ int ff_filter_frame(AVFilterLink *link, AVFilterBufferRef *frame) } else out = frame; - return filter_frame(link, out); + while(cmd && cmd->time <= frame->pts * av_q2d(link->time_base)){ + av_log(link->dst, AV_LOG_DEBUG, + "Processing command time:%f command:%s arg:%s\n", + cmd->time, cmd->command, cmd->arg); + avfilter_process_command(link->dst, cmd->command, cmd->arg, 0, 0, cmd->flags); + ff_command_queue_pop(link->dst); + cmd= link->dst->command_queue; + } + + pts = out->pts; + ret = filter_frame(link, out); + ff_update_link_current_pts(link, pts); + return ret; +} + +static int ff_filter_frame_needs_framing(AVFilterLink *link, AVFilterBufferRef *frame) +{ + int insamples = frame->audio->nb_samples, inpos = 0, nb_samples; + AVFilterBufferRef *pbuf = link->partial_buf; + int nb_channels = frame->audio->channels; + int ret = 0; + + /* Handle framing (min_samples, max_samples) */ + while (insamples) { + if (!pbuf) { + AVRational samples_tb = { 1, link->sample_rate }; + int perms = link->dstpad->min_perms | AV_PERM_WRITE; + pbuf = ff_get_audio_buffer(link, perms, link->partial_buf_size); + if (!pbuf) { + av_log(link->dst, AV_LOG_WARNING, + "Samples dropped due to memory allocation failure.\n"); + return 0; + } + avfilter_copy_buffer_ref_props(pbuf, frame); + pbuf->pts = frame->pts + + av_rescale_q(inpos, samples_tb, link->time_base); + pbuf->audio->nb_samples = 0; + } + nb_samples = FFMIN(insamples, + link->partial_buf_size - pbuf->audio->nb_samples); + av_samples_copy(pbuf->extended_data, frame->extended_data, + pbuf->audio->nb_samples, inpos, + nb_samples, nb_channels, link->format); + inpos += nb_samples; + insamples -= nb_samples; + pbuf->audio->nb_samples += nb_samples; + if (pbuf->audio->nb_samples >= link->min_samples) { + ret = ff_filter_frame_framed(link, pbuf); + pbuf = NULL; + } + } + avfilter_unref_buffer(frame); + link->partial_buf = pbuf; + return ret; +} + +int ff_filter_frame(AVFilterLink *link, AVFilterBufferRef *frame) +{ + FF_TPRINTF_START(NULL, filter_frame); ff_tlog_link(NULL, link, 1); ff_tlog(NULL, " "); ff_tlog_ref(NULL, frame, 1); + + /* Consistency checks */ + if (link->type == AVMEDIA_TYPE_VIDEO) { + if (strcmp(link->dst->filter->name, "scale")) { + av_assert1(frame->format == link->format); + av_assert1(frame->video->w == link->w); + av_assert1(frame->video->h == link->h); + } + } else { + av_assert1(frame->format == link->format); + av_assert1(frame->audio->channels == link->channels); + av_assert1(frame->audio->channel_layout == link->channel_layout); + av_assert1(frame->audio->sample_rate == link->sample_rate); + } + + /* Go directly to actual filtering if possible */ + if (link->type == AVMEDIA_TYPE_AUDIO && + link->min_samples && + (link->partial_buf || + frame->audio->nb_samples < link->min_samples || + frame->audio->nb_samples > link->max_samples)) { + return ff_filter_frame_needs_framing(link, frame); + } else { + return ff_filter_frame_framed(link, frame); + } } |