1593 lines · 49.3 KB · C
RawDownload
/* * filter layer * Copyright (c) 2007 Bobby Bingham * * This file is part of FFmpeg. * * 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. * * 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 FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include "libavutil/avassert.h"#include "libavutil/avstring.h"#include "libavutil/buffer.h"#include "libavutil/channel_layout.h"#include "libavutil/common.h"#include "libavutil/eval.h"#include "libavutil/frame.h"#include "libavutil/hwcontext.h"#include "libavutil/internal.h"#include "libavutil/opt.h"#include "libavutil/pixdesc.h"#include "libavutil/rational.h"#include "libavutil/samplefmt.h"#include "libavutil/thread.h" #define FF_INTERNAL_FIELDS 1#include "framequeue.h" #include "audio.h"#include "avfilter.h"#include "filters.h"#include "formats.h"#include "framepool.h"#include "internal.h"#include "version.h" #include "libavutil/ffversion.h"const char av_filter_ffversion[] = "FFmpeg version " FFMPEG_VERSION; static void tlog_ref(void *ctx, AVFrame *ref, int end){    ff_tlog(ctx,            "ref[%p buf:%p data:%p linesize[%d, %d, %d, %d] pts:%"PRId64" pos:%"PRId64,            ref, ref->buf, ref->data[0],            ref->linesize[0], ref->linesize[1], ref->linesize[2], ref->linesize[3],            ref->pts, ref->pkt_pos);     if (ref->width) {        ff_tlog(ctx, " a:%d/%d s:%dx%d i:%c iskey:%d type:%c",                ref->sample_aspect_ratio.num, ref->sample_aspect_ratio.den,                ref->width, ref->height,                !ref->interlaced_frame     ? 'P' :         /* Progressive  */                ref->top_field_first ? 'T' : 'B',    /* Top / Bottom */                ref->key_frame,                av_get_picture_type_char(ref->pict_type));    }    if (ref->nb_samples) {        ff_tlog(ctx, " cl:%"PRId64"d n:%d r:%d",                ref->channel_layout,                ref->nb_samples,                ref->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 FFMPEG_CONFIGURATION;} const char *avfilter_license(void){#define LICENSE_PREFIX "libavfilter license: "    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);} /** * Append a new pad. * * @param count  Pointer to the number of pads in the list * @param pads   Pointer to the pointer to the beginning of the list of pads * @param links  Pointer to the pointer to the beginning of the list of links * @param newpad The new pad to add. A copy is made when adding. * @return >= 0 in case of success, a negative AVERROR code on error */static int append_pad(unsigned *count, AVFilterPad **pads,                      AVFilterLink ***links, AVFilterPad *newpad){    AVFilterLink **newlinks;    AVFilterPad *newpads;    unsigned idx = *count;     newpads  = av_realloc_array(*pads,  idx + 1, sizeof(*newpads));    newlinks = av_realloc_array(*links, idx + 1, sizeof(*newlinks));    if (newpads)        *pads  = newpads;    if (newlinks)        *links = newlinks;    if (!newpads || !newlinks) {        if (newpad->flags & AVFILTERPAD_FLAG_FREE_NAME)            av_freep(&newpad->name);        return AVERROR(ENOMEM);    }     memcpy(*pads + idx, newpad, sizeof(AVFilterPad));    (*links)[idx] = NULL;     (*count)++;     return 0;} int ff_append_inpad(AVFilterContext *f, AVFilterPad *p){    return append_pad(&f->nb_inputs, &f->input_pads, &f->inputs, p);} int ff_append_inpad_free_name(AVFilterContext *f, AVFilterPad *p){    p->flags |= AVFILTERPAD_FLAG_FREE_NAME;    return ff_append_inpad(f, p);} int ff_append_outpad(AVFilterContext *f, AVFilterPad *p){    return append_pad(&f->nb_outputs, &f->output_pads, &f->outputs, p);} int ff_append_outpad_free_name(AVFilterContext *f, AVFilterPad *p){    p->flags |= AVFILTERPAD_FLAG_FREE_NAME;    return ff_append_outpad(f, p);} int avfilter_link(AVFilterContext *src, unsigned srcpad,                  AVFilterContext *dst, unsigned dstpad){    AVFilterLink *link;     av_assert0(src->graph);    av_assert0(dst->graph);    av_assert0(src->graph == dst->graph);     if (src->nb_outputs <= srcpad || dst->nb_inputs <= dstpad ||        src->outputs[srcpad]      || dst->inputs[dstpad])        return AVERROR(EINVAL);     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 (%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);    }     link = av_mallocz(sizeof(*link));    if (!link)        return AVERROR(ENOMEM);     src->outputs[srcpad] = dst->inputs[dstpad] = link;     link->src     = src;    link->dst     = dst;    link->srcpad  = &src->output_pads[srcpad];    link->dstpad  = &dst->input_pads[dstpad];    link->type    = src->output_pads[srcpad].type;    av_assert0(AV_PIX_FMT_NONE == -1 && AV_SAMPLE_FMT_NONE == -1);    link->format  = -1;    ff_framequeue_init(&link->fifo, &src->graph->internal->frame_queues);     return 0;} void avfilter_link_free(AVFilterLink **link){    if (!*link)        return;     ff_framequeue_free(&(*link)->fifo);    ff_frame_pool_uninit((FFFramePool**)&(*link)->frame_pool);    av_channel_layout_uninit(&(*link)->ch_layout);     av_freep(link);} void ff_filter_set_ready(AVFilterContext *filter, unsigned priority){    filter->ready = FFMAX(filter->ready, priority);} /** * Clear frame_blocked_in on all outputs. * This is necessary whenever something changes on input. */static void filter_unblock(AVFilterContext *filter){    unsigned i;     for (i = 0; i < filter->nb_outputs; i++)        filter->outputs[i]->frame_blocked_in = 0;}  void ff_avfilter_link_set_in_status(AVFilterLink *link, int status, int64_t pts){    if (link->status_in == status)        return;    av_assert0(!link->status_in);    link->status_in = status;    link->status_in_pts = pts;    link->frame_wanted_out = 0;    link->frame_blocked_in = 0;    filter_unblock(link->dst);    ff_filter_set_ready(link->dst, 200);} void ff_avfilter_link_set_out_status(AVFilterLink *link, int status, int64_t pts){    av_assert0(!link->frame_wanted_out);    av_assert0(!link->status_out);    link->status_out = status;    if (pts != AV_NOPTS_VALUE)        ff_update_link_current_pts(link, pts);    filter_unblock(link->dst);    ff_filter_set_ready(link->src, 200);} int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,                           unsigned filt_srcpad_idx, unsigned filt_dstpad_idx){    int ret;    unsigned dstpad_idx = link->dstpad - link->dst->input_pads;     av_log(link->dst, AV_LOG_VERBOSE, "auto-inserting filter '%s' "           "between the filter '%s' and the filter '%s'\n",           filt->name, link->src->name, link->dst->name);     link->dst->inputs[dstpad_idx] = NULL;    if ((ret = avfilter_link(filt, filt_dstpad_idx, link->dst, dstpad_idx)) < 0) {        /* failed to link output filter to new filter */        link->dst->inputs[dstpad_idx] = link;        return ret;    }     /* re-hookup the link to the new destination filter we inserted */    link->dst                     = filt;    link->dstpad                  = &filt->input_pads[filt_srcpad_idx];    filt->inputs[filt_srcpad_idx] = link;     /* if any information on supported media formats already exists on the     * link, we need to preserve that */    if (link->outcfg.formats)        ff_formats_changeref(&link->outcfg.formats,                             &filt->outputs[filt_dstpad_idx]->outcfg.formats);    if (link->outcfg.samplerates)        ff_formats_changeref(&link->outcfg.samplerates,                             &filt->outputs[filt_dstpad_idx]->outcfg.samplerates);    if (link->outcfg.channel_layouts)        ff_channel_layouts_changeref(&link->outcfg.channel_layouts,                                     &filt->outputs[filt_dstpad_idx]->outcfg.channel_layouts);     return 0;} int avfilter_config_links(AVFilterContext *filter){    int (*config_link)(AVFilterLink *);    unsigned i;    int ret;     for (i = 0; i < filter->nb_inputs; i ++) {        AVFilterLink *link = filter->inputs[i];        AVFilterLink *inlink;         if (!link) continue;        if (!link->src || !link->dst) {            av_log(filter, AV_LOG_ERROR,                   "Not all input and output are properly linked (%d).\n", i);            return AVERROR(EINVAL);        }         inlink = link->src->nb_inputs ? link->src->inputs[0] : NULL;        link->current_pts =        link->current_pts_us = AV_NOPTS_VALUE;         switch (link->init_state) {        case AVLINK_INIT:            continue;        case AVLINK_STARTINIT:            av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");            return 0;        case AVLINK_UNINIT:            link->init_state = AVLINK_STARTINIT;             if ((ret = avfilter_config_links(link->src)) < 0)                return ret;             if (!(config_link = link->srcpad->config_props)) {                if (link->src->nb_inputs != 1) {                    av_log(link->src, AV_LOG_ERROR, "Source filters and filters "                                                    "with more than one input "                                                    "must set config_props() "                                                    "callbacks on all outputs\n");                    return AVERROR(EINVAL);                }            } else if ((ret = config_link(link)) < 0) {                av_log(link->src, AV_LOG_ERROR,                       "Failed to configure output pad on %s\n",                       link->src->name);                return ret;            }             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->sample_aspect_ratio.num && !link->sample_aspect_ratio.den)                    link->sample_aspect_ratio = inlink ?                        inlink->sample_aspect_ratio : (AVRational){1,1};                 if (inlink) {                    if (!link->frame_rate.num && !link->frame_rate.den)                        link->frame_rate = inlink->frame_rate;                    if (!link->w)                        link->w = inlink->w;                    if (!link->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 (link->src->nb_inputs && link->src->inputs[0]->hw_frames_ctx &&                !(link->src->filter->flags_internal & FF_FILTER_FLAG_HWFRAME_AWARE)) {                av_assert0(!link->hw_frames_ctx &&                           "should not be set by non-hwframe-aware filter");                link->hw_frames_ctx = av_buffer_ref(link->src->inputs[0]->hw_frames_ctx);                if (!link->hw_frames_ctx)                    return AVERROR(ENOMEM);            }             if ((config_link = link->dstpad->config_props))                if ((ret = config_link(link)) < 0) {                    av_log(link->dst, AV_LOG_ERROR,                           "Failed to configure input pad on %s\n",                           link->dst->name);                    return ret;                }             link->init_state = AVLINK_INIT;        }    }     return 0;} void ff_tlog_link(void *ctx, AVFilterLink *link, int end){    if (link->type == AVMEDIA_TYPE_VIDEO) {        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 : "",                link->dst ? link->dst->filter->name : "",                end ? "\n" : "");    } else {        char buf[128];        av_channel_layout_describe(&link->ch_layout, buf, sizeof(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 : "",                end ? "\n" : "");    }} int ff_request_frame(AVFilterLink *link){    FF_TPRINTF_START(NULL, request_frame); ff_tlog_link(NULL, link, 1);     av_assert1(!link->dst->filter->activate);    if (link->status_out)        return link->status_out;    if (link->status_in) {        if (ff_framequeue_queued_frames(&link->fifo)) {            av_assert1(!link->frame_wanted_out);            av_assert1(link->dst->ready >= 300);            return 0;        } else {            /* Acknowledge status change. Filters using ff_request_frame() will               handle the change automatically. Filters can also check the               status directly but none do yet. */            ff_avfilter_link_set_out_status(link, link->status_in, link->status_in_pts);            return link->status_out;        }    }    link->frame_wanted_out = 1;    ff_filter_set_ready(link->src, 100);    return 0;} static int64_t guess_status_pts(AVFilterContext *ctx, int status, AVRational link_time_base){    unsigned i;    int64_t r = INT64_MAX;     for (i = 0; i < ctx->nb_inputs; i++)        if (ctx->inputs[i]->status_out == status)            r = FFMIN(r, av_rescale_q(ctx->inputs[i]->current_pts, ctx->inputs[i]->time_base, link_time_base));    if (r < INT64_MAX)        return r;    av_log(ctx, AV_LOG_WARNING, "EOF timestamp not reliable\n");    for (i = 0; i < ctx->nb_inputs; i++)        r = FFMIN(r, av_rescale_q(ctx->inputs[i]->status_in_pts, ctx->inputs[i]->time_base, link_time_base));    if (r < INT64_MAX)        return r;    return AV_NOPTS_VALUE;} static int ff_request_frame_to_filter(AVFilterLink *link){    int ret = -1;     FF_TPRINTF_START(NULL, request_frame_to_filter); ff_tlog_link(NULL, link, 1);    /* Assume the filter is blocked, let the method clear it if not */    link->frame_blocked_in = 1;    if (link->srcpad->request_frame)        ret = link->srcpad->request_frame(link);    else if (link->src->inputs[0])        ret = ff_request_frame(link->src->inputs[0]);    if (ret < 0) {        if (ret != AVERROR(EAGAIN) && ret != link->status_in)            ff_avfilter_link_set_in_status(link, ret, guess_status_pts(link->src, ret, link->time_base));        if (ret == AVERROR_EOF)            ret = 0;    }    return ret;} static const char *const var_names[] = {    "t",    "n",    "pos",    "w",    "h",    NULL}; enum {    VAR_T,    VAR_N,    VAR_POS,    VAR_W,    VAR_H,    VAR_VARS_NB

Lines 1–500 of 1593.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t