avfilter.c 10.9 KB
Newer Older
V
Vitor Sessak 已提交
1
/*
2
 * filter layer
V
Vitor Sessak 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 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
 */

22
#include "libavcodec/imgconvert.h"
V
Vitor Sessak 已提交
23 24
#include "avfilter.h"

25 26 27 28 29 30
/** list of registered filters */
struct FilterList
{
    AVFilter *filter;
    struct FilterList *next;
} *filters = NULL;
V
Vitor Sessak 已提交
31

V
Vitor Sessak 已提交
32 33 34 35
/** helper macros to get the in/out pad on the dst/src filter */
#define link_dpad(link)     link->dst-> input_pads[link->dstpad]
#define link_spad(link)     link->src->output_pads[link->srcpad]

36
AVFilterPicRef *avfilter_ref_pic(AVFilterPicRef *ref, int pmask)
V
Vitor Sessak 已提交
37 38
{
    AVFilterPicRef *ret = av_malloc(sizeof(AVFilterPicRef));
V
Vitor Sessak 已提交
39
    *ret = *ref;
40
    ret->perms &= pmask;
V
Vitor Sessak 已提交
41 42 43 44 45 46
    ret->pic->refcount ++;
    return ret;
}

void avfilter_unref_pic(AVFilterPicRef *ref)
{
V
Vitor Sessak 已提交
47
    if(!(--ref->pic->refcount))
V
Vitor Sessak 已提交
48 49 50 51
        ref->pic->free(ref->pic);
    av_free(ref);
}

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
void avfilter_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
                         AVFilterPad **pads, AVFilterLink ***links,
                         AVFilterPad *newpad)
{
    unsigned i;

    idx = FFMIN(idx, *count);

    *pads  = av_realloc(*pads,  sizeof(AVFilterPad)   * (*count + 1));
    *links = av_realloc(*links, sizeof(AVFilterLink*) * (*count + 1));
    memmove(*pads +idx+1, *pads +idx, sizeof(AVFilterPad)   * (*count-idx));
    memmove(*links+idx+1, *links+idx, sizeof(AVFilterLink*) * (*count-idx));
    memcpy(*pads+idx, newpad, sizeof(AVFilterPad));
    (*links)[idx] = NULL;

    (*count) ++;
    for(i = idx+1; i < *count; i ++)
        if(*links[i])
V
Vitor Sessak 已提交
70
            (*(unsigned *)((uint8_t *) *links[i] + padidx_off)) ++;
71 72
}

V
Vitor Sessak 已提交
73 74 75 76 77
int avfilter_link(AVFilterContext *src, unsigned srcpad,
                  AVFilterContext *dst, unsigned dstpad)
{
    AVFilterLink *link;

V
Vitor Sessak 已提交
78 79
    if(src->output_count <= srcpad || dst->input_count <= dstpad ||
       src->outputs[srcpad]        || dst->inputs[dstpad])
V
Vitor Sessak 已提交
80 81 82
        return -1;

    src->outputs[srcpad] =
V
Vitor Sessak 已提交
83
    dst-> inputs[dstpad] = link = av_mallocz(sizeof(AVFilterLink));
V
Vitor Sessak 已提交
84

V
Vitor Sessak 已提交
85 86 87 88
    link->src     = src;
    link->dst     = dst;
    link->srcpad  = srcpad;
    link->dstpad  = dstpad;
89 90 91 92 93
    link->format  = -1;

    return 0;
}

94 95 96
int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
                           unsigned in, unsigned out)
{
V
Vitor Sessak 已提交
97
    av_log(link->dst, AV_LOG_INFO, "auto-inserting filter '%s'\n",
98 99 100 101 102 103 104 105 106 107 108 109 110 111
            filt->filter->name);

    link->dst->inputs[link->dstpad] = NULL;
    if(avfilter_link(filt, out, link->dst, link->dstpad)) {
        /* failed to link output filter to new filter */
        link->dst->inputs[link->dstpad] = link;
        return -1;
    }

    /* re-hookup the link to the new destination filter we inserted */
    link->dst = filt;
    link->dstpad = in;
    filt->inputs[in] = link;

112 113
    /* if any information on supported colorspaces already exists on the
     * link, we need to preserve that */
114
    if(link->out_formats)
115 116
        avfilter_formats_changeref(&link->out_formats,
                                   &filt->outputs[out]->out_formats);
117

118 119 120
    return 0;
}

121
int avfilter_config_links(AVFilterContext *filter)
122 123
{
    int (*config_link)(AVFilterLink *);
124 125 126
    unsigned i;

    for(i = 0; i < filter->input_count; i ++) {
V
Vitor Sessak 已提交
127
        AVFilterLink *link = filter->inputs[i];
128

V
Vitor Sessak 已提交
129
        if(!link) continue;
130 131 132 133 134

        switch(link->init_state) {
        case AVLINK_INIT:
            continue;
        case AVLINK_STARTINIT:
135 136
            av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
            return 0;
137 138 139 140 141
        case AVLINK_UNINIT:
            link->init_state = AVLINK_STARTINIT;

            if(avfilter_config_links(link->src))
                return -1;
V
Vitor Sessak 已提交
142

V
Vitor Sessak 已提交
143 144 145 146
            if(!(config_link = link_spad(link).config_props))
                config_link  = avfilter_default_config_output_link;
            if(config_link(link))
                return -1;
147

V
Vitor Sessak 已提交
148
            if((config_link = link_dpad(link).config_props))
V
Vitor Sessak 已提交
149 150
                if(config_link(link))
                    return -1;
151

152 153 154 155
            link->init_state = AVLINK_INIT;
        }
    }

V
Vitor Sessak 已提交
156 157 158 159 160 161 162
    return 0;
}

AVFilterPicRef *avfilter_get_video_buffer(AVFilterLink *link, int perms)
{
    AVFilterPicRef *ret = NULL;

V
Vitor Sessak 已提交
163 164
    if(link_dpad(link).get_video_buffer)
        ret = link_dpad(link).get_video_buffer(link, perms);
V
Vitor Sessak 已提交
165 166 167 168 169 170 171

    if(!ret)
        ret = avfilter_default_get_video_buffer(link, perms);

    return ret;
}

172
int avfilter_request_frame(AVFilterLink *link)
V
Vitor Sessak 已提交
173
{
V
Vitor Sessak 已提交
174 175
    if(link_spad(link).request_frame)
        return link_spad(link).request_frame(link);
176
    else if(link->src->inputs[0])
177 178
        return avfilter_request_frame(link->src->inputs[0]);
    else return -1;
V
Vitor Sessak 已提交
179 180
}

181 182 183 184 185 186
int avfilter_poll_frame(AVFilterLink *link)
{
    int i, min=INT_MAX;

    if(link_spad(link).poll_frame)
        return link_spad(link).poll_frame(link);
V
Vitor Sessak 已提交
187 188 189 190 191 192

    for (i=0; i<link->src->input_count; i++) {
        if(!link->src->inputs[i])
            return -1;
        min = FFMIN(min, avfilter_poll_frame(link->src->inputs[i]));
    }
193 194 195 196

    return min;
}

V
Vitor Sessak 已提交
197 198 199 200 201
/* XXX: should we do the duplicating of the picture ref here, instead of
 * forcing the source filter to do it? */
void avfilter_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
{
    void (*start_frame)(AVFilterLink *, AVFilterPicRef *);
202
    AVFilterPad *dst = &link_dpad(link);
V
Vitor Sessak 已提交
203

204
    if(!(start_frame = dst->start_frame))
V
Vitor Sessak 已提交
205 206
        start_frame = avfilter_default_start_frame;

207
    /* prepare to copy the picture if it has insufficient permissions */
208 209
    if((dst->min_perms & picref->perms) != dst->min_perms ||
        dst->rej_perms & picref->perms) {
210
        /*
211 212
        av_log(link->dst, AV_LOG_INFO,
                "frame copy needed (have perms %x, need %x, reject %x)\n",
213
                picref->perms,
214
                link_dpad(link).min_perms, link_dpad(link).rej_perms);
215
        */
216

217
        link->cur_pic = avfilter_default_get_video_buffer(link, dst->min_perms);
218
        link->srcpic = picref;
219
        link->cur_pic->pts = link->srcpic->pts;
220 221 222 223 224
    }
    else
        link->cur_pic = picref;

    start_frame(link, link->cur_pic);
V
Vitor Sessak 已提交
225 226 227 228 229 230
}

void avfilter_end_frame(AVFilterLink *link)
{
    void (*end_frame)(AVFilterLink *);

231 232 233 234 235
    if(!(end_frame = link_dpad(link).end_frame))
        end_frame = avfilter_default_end_frame;

    end_frame(link);

236 237 238 239 240 241 242
    /* unreference the source picture if we're feeding the destination filter
     * a copied version dues to permission issues */
    if(link->srcpic) {
        avfilter_unref_pic(link->srcpic);
        link->srcpic = NULL;
    }

V
Vitor Sessak 已提交
243 244
}

245
void avfilter_draw_slice(AVFilterLink *link, int y, int h)
V
Vitor Sessak 已提交
246
{
247 248 249 250 251 252 253
    uint8_t *src[4], *dst[4];
    int i, j, hsub, vsub;

    /* copy the slice if needed for permission reasons */
    if(link->srcpic) {
        avcodec_get_chroma_sub_sample(link->format, &hsub, &vsub);

V
Vitor Sessak 已提交
254
        for(i = 0; i < 4; i ++) {
255
            if(link->srcpic->data[i]) {
V
Vitor Sessak 已提交
256 257 258 259
                src[i] = link->srcpic-> data[i] +
                    (y >> (i==0 ? 0 : vsub)) * link->srcpic-> linesize[i];
                dst[i] = link->cur_pic->data[i] +
                    (y >> (i==0 ? 0 : vsub)) * link->cur_pic->linesize[i];
260 261 262
            } else
                src[i] = dst[i] = NULL;
        }
V
Vitor Sessak 已提交
263 264

        for(i = 0; i < 4; i ++) {
V
Vitor Sessak 已提交
265 266 267
            int planew =
                ff_get_plane_bytewidth(link->format, link->cur_pic->w, i);

268 269
            if(!src[i]) continue;

V
Vitor Sessak 已提交
270
            for(j = 0; j < h >> (i==0 ? 0 : vsub); j ++) {
V
Vitor Sessak 已提交
271
                memcpy(dst[i], src[i], planew);
272 273 274 275 276 277
                src[i] += link->srcpic ->linesize[i];
                dst[i] += link->cur_pic->linesize[i];
            }
        }
    }

V
Vitor Sessak 已提交
278 279
    if(link_dpad(link).draw_slice)
        link_dpad(link).draw_slice(link, y, h);
V
Vitor Sessak 已提交
280 281
}

V
Vitor Sessak 已提交
282
AVFilter *avfilter_get_by_name(const char *name)
V
Vitor Sessak 已提交
283
{
284 285 286 287 288
    struct FilterList *filt;

    for(filt = filters; filt; filt = filt->next)
        if(!strcmp(filt->filter->name, name))
            return filt->filter;
V
Vitor Sessak 已提交
289 290 291 292 293 294

    return NULL;
}

void avfilter_register(AVFilter *filter)
{
295 296 297 298 299
    struct FilterList *newfilt = av_malloc(sizeof(struct FilterList));

    newfilt->filter = filter;
    newfilt->next   = filters;
    filters         = newfilt;
V
Vitor Sessak 已提交
300 301 302 303
}

void avfilter_uninit(void)
{
304 305 306 307 308 309
    struct FilterList *tmp;

    for(; filters; filters = tmp) {
        tmp = filters->next;
        av_free(filters);
    }
V
Vitor Sessak 已提交
310 311 312 313 314 315
}

static int pad_count(const AVFilterPad *pads)
{
    int count;

V
Vitor Sessak 已提交
316
    for(count = 0; pads->name; count ++) pads ++;
V
Vitor Sessak 已提交
317 318 319 320 321 322 323 324 325
    return count;
}

static const char *filter_name(void *p)
{
    AVFilterContext *filter = p;
    return filter->filter->name;
}

M
Måns Rullgård 已提交
326 327 328 329 330
static const AVClass avfilter_class = {
    "AVFilter",
    filter_name
};

V
Vitor Sessak 已提交
331
AVFilterContext *avfilter_open(AVFilter *filter, const char *inst_name)
V
Vitor Sessak 已提交
332
{
V
Vitor Sessak 已提交
333 334 335 336 337 338
    AVFilterContext *ret;

    if (!filter)
        return 0;

    ret = av_malloc(sizeof(AVFilterContext));
V
Vitor Sessak 已提交
339

M
Måns Rullgård 已提交
340
    ret->av_class = &avfilter_class;
V
Vitor Sessak 已提交
341
    ret->filter   = filter;
342
    ret->name     = inst_name ? av_strdup(inst_name) : NULL;
V
Vitor Sessak 已提交
343 344
    ret->priv     = av_mallocz(filter->priv_size);

V
Vitor Sessak 已提交
345
    ret->input_count  = pad_count(filter->inputs);
346 347
    ret->input_pads   = av_malloc(sizeof(AVFilterPad) * ret->input_count);
    memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad)*ret->input_count);
V
Vitor Sessak 已提交
348
    ret->inputs       = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
349

V
Vitor Sessak 已提交
350
    ret->output_count = pad_count(filter->outputs);
351 352
    ret->output_pads  = av_malloc(sizeof(AVFilterPad) * ret->output_count);
    memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad)*ret->output_count);
V
Vitor Sessak 已提交
353 354
    ret->outputs      = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);

V
Vitor Sessak 已提交
355 356 357 358 359 360 361 362 363 364
    return ret;
}

void avfilter_destroy(AVFilterContext *filter)
{
    int i;

    if(filter->filter->uninit)
        filter->filter->uninit(filter);

365
    for(i = 0; i < filter->input_count; i ++) {
V
Vitor Sessak 已提交
366 367
        if(filter->inputs[i])
            filter->inputs[i]->src->outputs[filter->inputs[i]->srcpad] = NULL;
V
Vitor Sessak 已提交
368
        av_freep(&filter->inputs[i]);
V
Vitor Sessak 已提交
369
    }
370
    for(i = 0; i < filter->output_count; i ++) {
V
Vitor Sessak 已提交
371 372
        if(filter->outputs[i])
            filter->outputs[i]->dst->inputs[filter->outputs[i]->dstpad] = NULL;
V
Vitor Sessak 已提交
373
        av_freep(&filter->outputs[i]);
V
Vitor Sessak 已提交
374 375
    }

V
Vitor Sessak 已提交
376 377 378 379 380 381
    av_freep(&filter->name);
    av_freep(&filter->input_pads);
    av_freep(&filter->output_pads);
    av_freep(&filter->inputs);
    av_freep(&filter->outputs);
    av_freep(&filter->priv);
V
Vitor Sessak 已提交
382 383 384
    av_free(filter);
}

385
int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
V
Vitor Sessak 已提交
386
{
V
Vitor Sessak 已提交
387
    int ret=0;
V
Vitor Sessak 已提交
388 389

    if(filter->filter->init)
V
Vitor Sessak 已提交
390 391
        ret = filter->filter->init(filter, args, opaque);
    return ret;
V
Vitor Sessak 已提交
392 393
}