Statistics
| Branch: | Revision:

ffmpeg / libavfilter / avfilter.c @ b62966c0

History | View | Annotate | Download (11.8 KB)

1 4dbbcdee Vitor Sessak
/*
2
 * Filter layer
3
 * copyright (c) 2007 Bobby Bingham
4
 *
5
 * This file is part of FFmpeg.
6
 *
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.
11
 *
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.
16
 *
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
20
 */
21
22 e0752603 Vitor Sessak
#include <stdarg.h>
23 4dbbcdee Vitor Sessak
#include <stdlib.h>
24
#include <string.h>
25
#include <stdio.h>
26
27
#include "avfilter.h"
28 d72a138e Vitor Sessak
#include "allfilters.h"
29 4dbbcdee Vitor Sessak
30 6d8c67a7 Vitor Sessak
/** list of registered filters */
31
struct FilterList
32
{
33
    AVFilter *filter;
34
    struct FilterList *next;
35
} *filters = NULL;
36 4dbbcdee Vitor Sessak
37 6fb0648d Vitor Sessak
/** helper macros to get the in/out pad on the dst/src filter */
38
#define link_dpad(link)     link->dst-> input_pads[link->dstpad]
39
#define link_spad(link)     link->src->output_pads[link->srcpad]
40
41 269b9847 Vitor Sessak
AVFilterPicRef *avfilter_ref_pic(AVFilterPicRef *ref, int pmask)
42 4dbbcdee Vitor Sessak
{
43
    AVFilterPicRef *ret = av_malloc(sizeof(AVFilterPicRef));
44 cdf2a332 Vitor Sessak
    *ret = *ref;
45 efb36bfc Vitor Sessak
    ret->perms &= pmask;
46 4dbbcdee Vitor Sessak
    ret->pic->refcount ++;
47
    return ret;
48
}
49
50
void avfilter_unref_pic(AVFilterPicRef *ref)
51
{
52 269b9847 Vitor Sessak
    if(-- ref->pic->refcount == 0)
53 4dbbcdee Vitor Sessak
        ref->pic->free(ref->pic);
54
    av_free(ref);
55
}
56
57 24618441 Vitor Sessak
void avfilter_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
58
                         AVFilterPad **pads, AVFilterLink ***links,
59
                         AVFilterPad *newpad)
60
{
61
    unsigned i;
62
63
    idx = FFMIN(idx, *count);
64
65
    *pads  = av_realloc(*pads,  sizeof(AVFilterPad)   * (*count + 1));
66
    *links = av_realloc(*links, sizeof(AVFilterLink*) * (*count + 1));
67
    memmove(*pads +idx+1, *pads +idx, sizeof(AVFilterPad)   * (*count-idx));
68
    memmove(*links+idx+1, *links+idx, sizeof(AVFilterLink*) * (*count-idx));
69
    memcpy(*pads+idx, newpad, sizeof(AVFilterPad));
70
    (*links)[idx] = NULL;
71
72
    (*count) ++;
73
    for(i = idx+1; i < *count; i ++)
74
        if(*links[i])
75
            (*(unsigned *)((uint8_t *)(*links[i]) + padidx_off)) ++;
76
}
77
78 4dbbcdee Vitor Sessak
int avfilter_link(AVFilterContext *src, unsigned srcpad,
79
                  AVFilterContext *dst, unsigned dstpad)
80
{
81
    AVFilterLink *link;
82
83 7d0e1392 Vitor Sessak
    if(src->output_count <= srcpad || dst->input_count <= dstpad ||
84
       src->outputs[srcpad]        || dst->inputs[dstpad])
85 4dbbcdee Vitor Sessak
        return -1;
86
87
    src->outputs[srcpad] =
88 c949d867 Vitor Sessak
    dst->inputs[dstpad]  = link = av_mallocz(sizeof(AVFilterLink));
89 4dbbcdee Vitor Sessak
90 1653c11f Vitor Sessak
    link->src     = src;
91
    link->dst     = dst;
92
    link->srcpad  = srcpad;
93
    link->dstpad  = dstpad;
94 01942f1d Vitor Sessak
    link->format  = -1;
95
96
    return 0;
97
}
98
99 52362e9d Vitor Sessak
int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
100
                           unsigned in, unsigned out)
101
{
102 e3065c8b Vitor Sessak
    AVFilterFormats *formats;
103
104 52362e9d Vitor Sessak
    av_log(NULL, AV_LOG_INFO, "auto-inserting filter '%s'\n",
105
            filt->filter->name);
106
107
    link->dst->inputs[link->dstpad] = NULL;
108
    if(avfilter_link(filt, out, link->dst, link->dstpad)) {
109
        /* failed to link output filter to new filter */
110
        link->dst->inputs[link->dstpad] = link;
111
        return -1;
112
    }
113
114
    /* re-hookup the link to the new destination filter we inserted */
115
    link->dst = filt;
116
    link->dstpad = in;
117
    filt->inputs[in] = link;
118
119 07d0bba5 Vitor Sessak
    /* if any information on supported colorspaces already exists on the
120
     * link, we need to preserve that */
121 e3065c8b Vitor Sessak
    if((formats = link->out_formats))
122
        avfilter_formats_changeref(&link->out_formats,
123
                                   &filt->outputs[out]->out_formats);
124 07d0bba5 Vitor Sessak
125 52362e9d Vitor Sessak
    return 0;
126
}
127
128 69818105 Vitor Sessak
int avfilter_config_links(AVFilterContext *filter)
129 01942f1d Vitor Sessak
{
130
    int (*config_link)(AVFilterLink *);
131 69818105 Vitor Sessak
    unsigned i;
132
133
    for(i = 0; i < filter->input_count; i ++) {
134
        AVFilterLink *link;
135 d21cbbff Vitor Sessak
136 69818105 Vitor Sessak
        if(!(link = filter->inputs[i])) continue;
137
138
        switch(link->init_state) {
139
        case AVLINK_INIT:
140
            continue;
141
        case AVLINK_STARTINIT:
142
            av_log(filter, AV_LOG_ERROR, "circular filter chain detected\n");
143
            return -1;
144
        case AVLINK_UNINIT:
145
            link->init_state = AVLINK_STARTINIT;
146
147
            if(avfilter_config_links(link->src))
148
                return -1;
149 4dbbcdee Vitor Sessak
150 2fd46877 Vitor Sessak
            if(!(config_link = link_spad(link).config_props))
151
                config_link  = avfilter_default_config_output_link;
152
            if(config_link(link))
153
                return -1;
154 102fb0e3 Vitor Sessak
155 2fd46877 Vitor Sessak
            if((config_link = link_dpad(link).config_props))
156
            if(config_link(link))
157
                return -1;
158 102fb0e3 Vitor Sessak
159 69818105 Vitor Sessak
            link->init_state = AVLINK_INIT;
160
        }
161
    }
162
163 4dbbcdee Vitor Sessak
    return 0;
164
}
165
166
AVFilterPicRef *avfilter_get_video_buffer(AVFilterLink *link, int perms)
167
{
168
    AVFilterPicRef *ret = NULL;
169
170 6fb0648d Vitor Sessak
    if(link_dpad(link).get_video_buffer)
171
        ret = link_dpad(link).get_video_buffer(link, perms);
172 4dbbcdee Vitor Sessak
173
    if(!ret)
174
        ret = avfilter_default_get_video_buffer(link, perms);
175
176
    return ret;
177
}
178
179 3306f8bd Vitor Sessak
int avfilter_request_frame(AVFilterLink *link)
180 4dbbcdee Vitor Sessak
{
181 6fb0648d Vitor Sessak
    if(link_spad(link).request_frame)
182
        return link_spad(link).request_frame(link);
183 9586ba3a Vitor Sessak
    else if(link->src->inputs[0])
184 3306f8bd Vitor Sessak
        return avfilter_request_frame(link->src->inputs[0]);
185
    else return -1;
186 4dbbcdee Vitor Sessak
}
187
188
/* XXX: should we do the duplicating of the picture ref here, instead of
189
 * forcing the source filter to do it? */
190
void avfilter_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
191
{
192
    void (*start_frame)(AVFilterLink *, AVFilterPicRef *);
193
194 e675dccd Vitor Sessak
    if(!(start_frame = link_dpad(link).start_frame))
195 4dbbcdee Vitor Sessak
        start_frame = avfilter_default_start_frame;
196
197 c949d867 Vitor Sessak
    /* prepare to copy the picture if it has insufficient permissions */
198 269b9847 Vitor Sessak
    if((link_dpad(link).min_perms & picref->perms) != link_dpad(link).min_perms ||
199
        link_dpad(link).rej_perms & picref->perms) {
200 c949d867 Vitor Sessak
        av_log(link->dst, AV_LOG_INFO,
201
                "frame copy needed (have perms %x, need %x, reject %x)\n",
202 269b9847 Vitor Sessak
                picref->perms,
203 c949d867 Vitor Sessak
                link_dpad(link).min_perms, link_dpad(link).rej_perms);
204
205
        link->cur_pic = avfilter_default_get_video_buffer(link, link_dpad(link).min_perms);
206
        link->srcpic = picref;
207
    }
208
    else
209
        link->cur_pic = picref;
210
211
    start_frame(link, link->cur_pic);
212 4dbbcdee Vitor Sessak
}
213
214
void avfilter_end_frame(AVFilterLink *link)
215
{
216
    void (*end_frame)(AVFilterLink *);
217
218 c949d867 Vitor Sessak
    /* unreference the source picture if we're feeding the destination filter
219
     * a copied version dues to permission issues */
220
    if(link->srcpic) {
221
        avfilter_unref_pic(link->srcpic);
222
        link->srcpic = NULL;
223
    }
224
225 e675dccd Vitor Sessak
    if(!(end_frame = link_dpad(link).end_frame))
226 4dbbcdee Vitor Sessak
        end_frame = avfilter_default_end_frame;
227
228
    end_frame(link);
229
}
230
231 ef35d13a Vitor Sessak
void avfilter_draw_slice(AVFilterLink *link, int y, int h)
232 4dbbcdee Vitor Sessak
{
233 c949d867 Vitor Sessak
    uint8_t *src[4], *dst[4];
234
    int i, j, hsub, vsub;
235
236
    /* copy the slice if needed for permission reasons */
237
    if(link->srcpic) {
238
        avcodec_get_chroma_sub_sample(link->format, &hsub, &vsub);
239
240
        src[0] = link->srcpic-> data[0] + y * link->srcpic-> linesize[0];
241
        dst[0] = link->cur_pic->data[0] + y * link->cur_pic->linesize[0];
242
        for(i = 1; i < 4; i ++) {
243
            if(link->srcpic->data[i]) {
244
                src[i] = link->srcpic-> data[i] + (y >> vsub) * link->srcpic-> linesize[i];
245
                dst[i] = link->cur_pic->data[i] + (y >> vsub) * link->cur_pic->linesize[i];
246
            } else
247
                src[i] = dst[i] = NULL;
248
        }
249
        for(j = 0; j < h; j ++) {
250
            memcpy(dst[0], src[0], link->cur_pic->linesize[0]);
251
            src[0] += link->srcpic ->linesize[0];
252
            dst[0] += link->cur_pic->linesize[0];
253
        }
254
        for(i = 1; i < 4; i ++) {
255
            if(!src[i]) continue;
256
257
            for(j = 0; j < h >> vsub; j ++) {
258
                memcpy(dst[i], src[i], link->cur_pic->linesize[i]);
259
                src[i] += link->srcpic ->linesize[i];
260
                dst[i] += link->cur_pic->linesize[i];
261
            }
262
        }
263
    }
264
265 6fb0648d Vitor Sessak
    if(!link_dpad(link).draw_slice)
266 7d0e1392 Vitor Sessak
        return;
267
268 6fb0648d Vitor Sessak
    link_dpad(link).draw_slice(link, y, h);
269 4dbbcdee Vitor Sessak
}
270
271
AVFilter *avfilter_get_by_name(char *name)
272
{
273 6d8c67a7 Vitor Sessak
    struct FilterList *filt;
274
275
    for(filt = filters; filt; filt = filt->next)
276
        if(!strcmp(filt->filter->name, name))
277
            return filt->filter;
278 4dbbcdee Vitor Sessak
279
    return NULL;
280
}
281
282
void avfilter_register(AVFilter *filter)
283
{
284 6d8c67a7 Vitor Sessak
    struct FilterList *newfilt = av_malloc(sizeof(struct FilterList));
285
286
    newfilt->filter = filter;
287
    newfilt->next   = filters;
288
    filters         = newfilt;
289 4dbbcdee Vitor Sessak
}
290
291
void avfilter_init(void)
292
{
293 7bce27a3 Vitor Sessak
    avfilter_register(&avfilter_vf_crop);
294
    avfilter_register(&avfilter_vf_fifo);
295 e946c25a Vitor Sessak
    avfilter_register(&avfilter_vf_format);
296 7bce27a3 Vitor Sessak
    avfilter_register(&avfilter_vf_fps);
297
    avfilter_register(&avfilter_vf_graph);
298
    avfilter_register(&avfilter_vf_graphdesc);
299
    avfilter_register(&avfilter_vf_graphfile);
300 c35a0473 Vitor Sessak
    avfilter_register(&avfilter_vf_hflip);
301 f9033975 Vitor Sessak
    avfilter_register(&avfilter_vf_negate);
302 cc0bb5a1 Vitor Sessak
    avfilter_register(&avfilter_vf_noformat);
303 7bce27a3 Vitor Sessak
    avfilter_register(&avfilter_vf_overlay);
304 0970ec6a Vitor Sessak
    avfilter_register(&avfilter_vf_rotate);
305 2a553ef5 Vitor Sessak
    avfilter_register(&avfilter_vf_scale);
306 0970ec6a Vitor Sessak
    avfilter_register(&avfilter_vf_setpts);
307 7bce27a3 Vitor Sessak
    avfilter_register(&avfilter_vf_slicify);
308
    avfilter_register(&avfilter_vf_split);
309 be696990 Vitor Sessak
    avfilter_register(&avfilter_vf_transpose);
310 7bce27a3 Vitor Sessak
    avfilter_register(&avfilter_vf_vflip);
311 b62966c0 Vitor Sessak
#ifdef CONFIG_AVFILTER_LAVF
312
    avfilter_register(&avfilter_vsrc_movie);
313
#endif //CONFIG_AVFILTER_LAVF
314 4dbbcdee Vitor Sessak
}
315
316
void avfilter_uninit(void)
317
{
318 6d8c67a7 Vitor Sessak
    struct FilterList *tmp;
319
320
    for(; filters; filters = tmp) {
321
        tmp = filters->next;
322
        av_free(filters);
323
    }
324 4dbbcdee Vitor Sessak
}
325
326
static int pad_count(const AVFilterPad *pads)
327
{
328
    AVFilterPad *p = (AVFilterPad *) pads;
329
    int count;
330
331
    for(count = 0; p->name; count ++) p ++;
332
    return count;
333
}
334
335
static const char *filter_name(void *p)
336
{
337
    AVFilterContext *filter = p;
338
    return filter->filter->name;
339
}
340
341 44b73497 Vitor Sessak
AVFilterContext *avfilter_open(AVFilter *filter, char *inst_name)
342 4dbbcdee Vitor Sessak
{
343 8f618f4c Vitor Sessak
    AVFilterContext *ret;
344
345
    if (!filter)
346
        return 0;
347
348
    ret = av_malloc(sizeof(AVFilterContext));
349 4dbbcdee Vitor Sessak
350
    ret->av_class = av_mallocz(sizeof(AVClass));
351
    ret->av_class->item_name = filter_name;
352
    ret->filter   = filter;
353 2350e69c Vitor Sessak
    ret->name     = inst_name ? av_strdup(inst_name) : NULL;
354 4dbbcdee Vitor Sessak
    ret->priv     = av_mallocz(filter->priv_size);
355
356 7d0e1392 Vitor Sessak
    ret->input_count  = pad_count(filter->inputs);
357 c5ef7d7b Vitor Sessak
    ret->input_pads   = av_malloc(sizeof(AVFilterPad) * ret->input_count);
358
    memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad)*ret->input_count);
359 7d0e1392 Vitor Sessak
    ret->inputs       = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
360 c5ef7d7b Vitor Sessak
361 7d0e1392 Vitor Sessak
    ret->output_count = pad_count(filter->outputs);
362 c5ef7d7b Vitor Sessak
    ret->output_pads  = av_malloc(sizeof(AVFilterPad) * ret->output_count);
363
    memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad)*ret->output_count);
364 7d0e1392 Vitor Sessak
    ret->outputs      = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
365
366 4dbbcdee Vitor Sessak
    return ret;
367
}
368
369
void avfilter_destroy(AVFilterContext *filter)
370
{
371
    int i;
372
373
    if(filter->filter->uninit)
374
        filter->filter->uninit(filter);
375
376 c5ef7d7b Vitor Sessak
    for(i = 0; i < filter->input_count; i ++) {
377 4dbbcdee Vitor Sessak
        if(filter->inputs[i])
378
            filter->inputs[i]->src->outputs[filter->inputs[i]->srcpad] = NULL;
379 f4cb4462 Vitor Sessak
        av_freep(&filter->inputs[i]);
380 4dbbcdee Vitor Sessak
    }
381 c5ef7d7b Vitor Sessak
    for(i = 0; i < filter->output_count; i ++) {
382 4dbbcdee Vitor Sessak
        if(filter->outputs[i])
383
            filter->outputs[i]->dst->inputs[filter->outputs[i]->dstpad] = NULL;
384 f4cb4462 Vitor Sessak
        av_freep(&filter->outputs[i]);
385 4dbbcdee Vitor Sessak
    }
386
387 f4cb4462 Vitor Sessak
    av_freep(&filter->name);
388
    av_freep(&filter->input_pads);
389
    av_freep(&filter->output_pads);
390
    av_freep(&filter->inputs);
391
    av_freep(&filter->outputs);
392
    av_freep(&filter->priv);
393
    av_freep(&filter->av_class);
394 4dbbcdee Vitor Sessak
    av_free(filter);
395
}
396
397 a360f71e Vitor Sessak
int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
398 4dbbcdee Vitor Sessak
{
399 c6b0aa68 Vitor Sessak
    int ret;
400 4dbbcdee Vitor Sessak
401
    if(filter->filter->init)
402 a360f71e Vitor Sessak
        if((ret = filter->filter->init(filter, args, opaque))) return ret;
403 4dbbcdee Vitor Sessak
    return 0;
404
}