Statistics
| Branch: | Revision:

ffmpeg / libavfilter / avfilter.c @ f9033975

History | View | Annotate | Download (12.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 d21cbbff Vitor Sessak
static int common_format(int *fmts0, int *fmts1)
100
{
101
    int i, j;
102
103
    for(i = 0; fmts0[i] != -1; i ++)
104
        for(j = 0; fmts1[j] != -1; j ++)
105
            if(fmts0[i] == fmts1[j])
106
                return fmts0[i];
107
108
    return -1;
109
}
110
111 01942f1d Vitor Sessak
int avfilter_config_link(AVFilterLink *link)
112
{
113 d21cbbff Vitor Sessak
    int *fmts[3] = {NULL,NULL,NULL};
114 01942f1d Vitor Sessak
    int (*config_link)(AVFilterLink *);
115 e675dccd Vitor Sessak
    int *(*query_formats)(AVFilterLink *link);
116 01942f1d Vitor Sessak
117 d21cbbff Vitor Sessak
    AVFilterContext *scale;
118
    AVFilterLink *link2 = NULL;
119
120 01942f1d Vitor Sessak
    if(!link)
121
        return 0;
122 4dbbcdee Vitor Sessak
123 d21cbbff Vitor Sessak
    /* find a format both filters support */
124 e675dccd Vitor Sessak
    if(!(query_formats = link_spad(link).query_formats))
125
        query_formats = avfilter_default_query_output_formats;
126
    fmts[0] = query_formats(link);
127 6fb0648d Vitor Sessak
    fmts[1] = link_dpad(link).query_formats(link);
128 d21cbbff Vitor Sessak
    if((link->format = common_format(fmts[0], fmts[1])) == -1) {
129
        /* no common format found.  insert scale filter to convert */
130
        if(!(scale = avfilter_open(&avfilter_vf_scale, NULL)))
131
            goto format_done;
132
        if(scale->filter->init(scale, NULL, NULL)) {
133
            avfilter_destroy(scale);
134
            goto format_done;
135
        }
136
137
        link->dst->inputs[link->dstpad] = NULL;
138
        if(avfilter_link(scale, 0, link->dst, link->dstpad)) {
139
            link->dst->inputs[link->dstpad] = link;
140
            goto format_done;
141
        }
142
        link2 = scale->outputs[0];
143
        link->dst    = scale;
144
        link->dstpad = 0;
145
        scale->inputs[0] = link;
146
147
        /* now try again to find working colorspaces.
148
         * XXX: is it safe to assume that the scale filter always supports the
149
         * same input and output colorspaces? */
150
        fmts[2] = scale->input_pads[0].query_formats(link);
151
        link->format  = common_format(fmts[0], fmts[2]);
152
        link2->format = common_format(fmts[1], fmts[2]);
153
    }
154 e0752603 Vitor Sessak
155
format_done:
156
    av_free(fmts[0]);
157
    av_free(fmts[1]);
158 d21cbbff Vitor Sessak
    av_free(fmts[2]);
159
    if(link->format == -1 || (link2 && link2->format == -1)) {
160
        if(link2) {
161
            link->dst    = link2->dst;
162
            link->dstpad = link2->dstpad;
163
            link->dst->inputs[link->dstpad] = link;
164
            link->format = -1;
165
            avfilter_destroy(scale);
166
            av_free(link2);
167
        }
168 e0752603 Vitor Sessak
        return -1;
169 d21cbbff Vitor Sessak
    }
170 e0752603 Vitor Sessak
171 6fb0648d Vitor Sessak
    if(!(config_link = link_spad(link).config_props))
172 01942f1d Vitor Sessak
        config_link  = avfilter_default_config_output_link;
173
    if(config_link(link))
174
            return -1;
175 102fb0e3 Vitor Sessak
176 6fb0648d Vitor Sessak
    if(!(config_link = link_dpad(link).config_props))
177 01942f1d Vitor Sessak
        config_link  = avfilter_default_config_input_link;
178
    if(config_link(link))
179
            return -1;
180 102fb0e3 Vitor Sessak
181 d21cbbff Vitor Sessak
    if(link2) {
182
        if(!(config_link = link_spad(link2).config_props))
183
            config_link  = avfilter_default_config_output_link;
184
        if(config_link(link2))
185
                return -1;
186
187
        if(!(config_link = link_dpad(link2).config_props))
188
            config_link  = avfilter_default_config_input_link;
189
        if(config_link(link2))
190
                return -1;
191
    }
192
193 4dbbcdee Vitor Sessak
    return 0;
194
}
195
196
AVFilterPicRef *avfilter_get_video_buffer(AVFilterLink *link, int perms)
197
{
198
    AVFilterPicRef *ret = NULL;
199
200 6fb0648d Vitor Sessak
    if(link_dpad(link).get_video_buffer)
201
        ret = link_dpad(link).get_video_buffer(link, perms);
202 4dbbcdee Vitor Sessak
203
    if(!ret)
204
        ret = avfilter_default_get_video_buffer(link, perms);
205
206
    return ret;
207
}
208
209 3306f8bd Vitor Sessak
int avfilter_request_frame(AVFilterLink *link)
210 4dbbcdee Vitor Sessak
{
211 6fb0648d Vitor Sessak
    if(link_spad(link).request_frame)
212
        return link_spad(link).request_frame(link);
213 9586ba3a Vitor Sessak
    else if(link->src->inputs[0])
214 3306f8bd Vitor Sessak
        return avfilter_request_frame(link->src->inputs[0]);
215
    else return -1;
216 4dbbcdee Vitor Sessak
}
217
218
/* XXX: should we do the duplicating of the picture ref here, instead of
219
 * forcing the source filter to do it? */
220
void avfilter_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
221
{
222
    void (*start_frame)(AVFilterLink *, AVFilterPicRef *);
223
224 e675dccd Vitor Sessak
    if(!(start_frame = link_dpad(link).start_frame))
225 4dbbcdee Vitor Sessak
        start_frame = avfilter_default_start_frame;
226
227 c949d867 Vitor Sessak
    /* prepare to copy the picture if it has insufficient permissions */
228 269b9847 Vitor Sessak
    if((link_dpad(link).min_perms & picref->perms) != link_dpad(link).min_perms ||
229
        link_dpad(link).rej_perms & picref->perms) {
230 c949d867 Vitor Sessak
        av_log(link->dst, AV_LOG_INFO,
231
                "frame copy needed (have perms %x, need %x, reject %x)\n",
232 269b9847 Vitor Sessak
                picref->perms,
233 c949d867 Vitor Sessak
                link_dpad(link).min_perms, link_dpad(link).rej_perms);
234
235
        link->cur_pic = avfilter_default_get_video_buffer(link, link_dpad(link).min_perms);
236
        link->srcpic = picref;
237
    }
238
    else
239
        link->cur_pic = picref;
240
241
    start_frame(link, link->cur_pic);
242 4dbbcdee Vitor Sessak
}
243
244
void avfilter_end_frame(AVFilterLink *link)
245
{
246
    void (*end_frame)(AVFilterLink *);
247
248 c949d867 Vitor Sessak
    /* unreference the source picture if we're feeding the destination filter
249
     * a copied version dues to permission issues */
250
    if(link->srcpic) {
251
        avfilter_unref_pic(link->srcpic);
252
        link->srcpic = NULL;
253
    }
254
255 e675dccd Vitor Sessak
    if(!(end_frame = link_dpad(link).end_frame))
256 4dbbcdee Vitor Sessak
        end_frame = avfilter_default_end_frame;
257
258
    end_frame(link);
259
}
260
261 ef35d13a Vitor Sessak
void avfilter_draw_slice(AVFilterLink *link, int y, int h)
262 4dbbcdee Vitor Sessak
{
263 c949d867 Vitor Sessak
    uint8_t *src[4], *dst[4];
264
    int i, j, hsub, vsub;
265
266
    /* copy the slice if needed for permission reasons */
267
    if(link->srcpic) {
268
        avcodec_get_chroma_sub_sample(link->format, &hsub, &vsub);
269
270
        src[0] = link->srcpic-> data[0] + y * link->srcpic-> linesize[0];
271
        dst[0] = link->cur_pic->data[0] + y * link->cur_pic->linesize[0];
272
        for(i = 1; i < 4; i ++) {
273
            if(link->srcpic->data[i]) {
274
                src[i] = link->srcpic-> data[i] + (y >> vsub) * link->srcpic-> linesize[i];
275
                dst[i] = link->cur_pic->data[i] + (y >> vsub) * link->cur_pic->linesize[i];
276
            } else
277
                src[i] = dst[i] = NULL;
278
        }
279
        for(j = 0; j < h; j ++) {
280
            memcpy(dst[0], src[0], link->cur_pic->linesize[0]);
281
            src[0] += link->srcpic ->linesize[0];
282
            dst[0] += link->cur_pic->linesize[0];
283
        }
284
        for(i = 1; i < 4; i ++) {
285
            if(!src[i]) continue;
286
287
            for(j = 0; j < h >> vsub; j ++) {
288
                memcpy(dst[i], src[i], link->cur_pic->linesize[i]);
289
                src[i] += link->srcpic ->linesize[i];
290
                dst[i] += link->cur_pic->linesize[i];
291
            }
292
        }
293
    }
294
295 6fb0648d Vitor Sessak
    if(!link_dpad(link).draw_slice)
296 7d0e1392 Vitor Sessak
        return;
297
298 6fb0648d Vitor Sessak
    link_dpad(link).draw_slice(link, y, h);
299 4dbbcdee Vitor Sessak
}
300
301
AVFilter *avfilter_get_by_name(char *name)
302
{
303 6d8c67a7 Vitor Sessak
    struct FilterList *filt;
304
305
    for(filt = filters; filt; filt = filt->next)
306
        if(!strcmp(filt->filter->name, name))
307
            return filt->filter;
308 4dbbcdee Vitor Sessak
309
    return NULL;
310
}
311
312
/* FIXME: insert in order, rather than insert at end + resort */
313
void avfilter_register(AVFilter *filter)
314
{
315 6d8c67a7 Vitor Sessak
    struct FilterList *newfilt = av_malloc(sizeof(struct FilterList));
316
317
    newfilt->filter = filter;
318
    newfilt->next   = filters;
319
    filters         = newfilt;
320 4dbbcdee Vitor Sessak
}
321
322
void avfilter_init(void)
323
{
324 7bce27a3 Vitor Sessak
    avfilter_register(&avfilter_vsrc_dummy);
325
    avfilter_register(&avfilter_vsrc_ppm);
326
    avfilter_register(&avfilter_vf_crop);
327
    avfilter_register(&avfilter_vf_fifo);
328
    avfilter_register(&avfilter_vf_fps);
329
    avfilter_register(&avfilter_vf_graph);
330
    avfilter_register(&avfilter_vf_graphdesc);
331
    avfilter_register(&avfilter_vf_graphfile);
332 f9033975 Vitor Sessak
    avfilter_register(&avfilter_vf_negate);
333 7bce27a3 Vitor Sessak
    avfilter_register(&avfilter_vf_overlay);
334
    avfilter_register(&avfilter_vf_passthrough);
335
    avfilter_register(&avfilter_vf_rgb2bgr);
336 2a553ef5 Vitor Sessak
    avfilter_register(&avfilter_vf_scale);
337 7bce27a3 Vitor Sessak
    avfilter_register(&avfilter_vf_slicify);
338
    avfilter_register(&avfilter_vf_split);
339
    avfilter_register(&avfilter_vf_vflip);
340 4dbbcdee Vitor Sessak
}
341
342
void avfilter_uninit(void)
343
{
344 6d8c67a7 Vitor Sessak
    struct FilterList *tmp;
345
346
    for(; filters; filters = tmp) {
347
        tmp = filters->next;
348
        av_free(filters);
349
    }
350 4dbbcdee Vitor Sessak
}
351
352
static int pad_count(const AVFilterPad *pads)
353
{
354
    AVFilterPad *p = (AVFilterPad *) pads;
355
    int count;
356
357
    for(count = 0; p->name; count ++) p ++;
358
    return count;
359
}
360
361
static const char *filter_name(void *p)
362
{
363
    AVFilterContext *filter = p;
364
    return filter->filter->name;
365
}
366
367 44b73497 Vitor Sessak
AVFilterContext *avfilter_open(AVFilter *filter, char *inst_name)
368 4dbbcdee Vitor Sessak
{
369
    AVFilterContext *ret = av_malloc(sizeof(AVFilterContext));
370
371
    ret->av_class = av_mallocz(sizeof(AVClass));
372
    ret->av_class->item_name = filter_name;
373
    ret->filter   = filter;
374 2350e69c Vitor Sessak
    ret->name     = inst_name ? av_strdup(inst_name) : NULL;
375 4dbbcdee Vitor Sessak
    ret->priv     = av_mallocz(filter->priv_size);
376
377 7d0e1392 Vitor Sessak
    ret->input_count  = pad_count(filter->inputs);
378 c5ef7d7b Vitor Sessak
    ret->input_pads   = av_malloc(sizeof(AVFilterPad) * ret->input_count);
379
    memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad)*ret->input_count);
380 7d0e1392 Vitor Sessak
    ret->inputs       = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
381 c5ef7d7b Vitor Sessak
382 7d0e1392 Vitor Sessak
    ret->output_count = pad_count(filter->outputs);
383 c5ef7d7b Vitor Sessak
    ret->output_pads  = av_malloc(sizeof(AVFilterPad) * ret->output_count);
384
    memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad)*ret->output_count);
385 7d0e1392 Vitor Sessak
    ret->outputs      = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
386
387 4dbbcdee Vitor Sessak
    return ret;
388
}
389
390
void avfilter_destroy(AVFilterContext *filter)
391
{
392
    int i;
393
394
    if(filter->filter->uninit)
395
        filter->filter->uninit(filter);
396
397 c5ef7d7b Vitor Sessak
    for(i = 0; i < filter->input_count; i ++) {
398 4dbbcdee Vitor Sessak
        if(filter->inputs[i])
399
            filter->inputs[i]->src->outputs[filter->inputs[i]->srcpad] = NULL;
400 f4cb4462 Vitor Sessak
        av_freep(&filter->inputs[i]);
401 4dbbcdee Vitor Sessak
    }
402 c5ef7d7b Vitor Sessak
    for(i = 0; i < filter->output_count; i ++) {
403 4dbbcdee Vitor Sessak
        if(filter->outputs[i])
404
            filter->outputs[i]->dst->inputs[filter->outputs[i]->dstpad] = NULL;
405 f4cb4462 Vitor Sessak
        av_freep(&filter->outputs[i]);
406 4dbbcdee Vitor Sessak
    }
407
408 f4cb4462 Vitor Sessak
    av_freep(&filter->name);
409
    av_freep(&filter->input_pads);
410
    av_freep(&filter->output_pads);
411
    av_freep(&filter->inputs);
412
    av_freep(&filter->outputs);
413
    av_freep(&filter->priv);
414
    av_freep(&filter->av_class);
415 4dbbcdee Vitor Sessak
    av_free(filter);
416
}
417
418 a360f71e Vitor Sessak
int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
419 4dbbcdee Vitor Sessak
{
420 c6b0aa68 Vitor Sessak
    int ret;
421 4dbbcdee Vitor Sessak
422
    if(filter->filter->init)
423 a360f71e Vitor Sessak
        if((ret = filter->filter->init(filter, args, opaque))) return ret;
424 4dbbcdee Vitor Sessak
    return 0;
425
}
426
427 e0752603 Vitor Sessak
int *avfilter_make_format_list(int len, ...)
428
{
429
    int *ret, i;
430
    va_list vl;
431
432
    ret = av_malloc(sizeof(int) * (len + 1));
433
    va_start(vl, len);
434
    for(i = 0; i < len; i ++)
435
        ret[i] = va_arg(vl, int);
436
    va_end(vl);
437
    ret[len] = -1;
438
439
    return ret;
440
}