Statistics
| Branch: | Revision:

ffmpeg / libavfilter / avfilter.c @ 998a7aa3

History | View | Annotate | Download (9.64 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
/** list of registered filters, sorted by name */
31
static int filter_count = 0;
32
static AVFilter **filters = NULL;
33
34
/* TODO: buffer pool.  see comment for avfilter_default_get_video_buffer() */
35
void avfilter_default_free_video_buffer(AVFilterPic *pic)
36
{
37
    avpicture_free((AVPicture *) pic);
38
    av_free(pic);
39
}
40
41
/* TODO: set the buffer's priv member to a context structure for the whole
42
 * filter chain.  This will allow for a buffer pool instead of the constant
43
 * alloc & free cycle currently implemented. */
44
AVFilterPicRef *avfilter_default_get_video_buffer(AVFilterLink *link, int perms)
45
{
46
    AVFilterPic *pic = av_mallocz(sizeof(AVFilterPic));
47
    AVFilterPicRef *ref = av_mallocz(sizeof(AVFilterPicRef));
48
49
    ref->pic = pic;
50
    ref->w = link->w;
51
    ref->h = link->h;
52
    ref->perms = perms;
53
54
    pic->refcount = 1;
55
    pic->format = link->format;
56
    pic->free = avfilter_default_free_video_buffer;
57
    avpicture_alloc((AVPicture *)pic, pic->format, ref->w, ref->h);
58
59 92beffdc Vitor Sessak
    memcpy(ref->data,     pic->data,     sizeof(pic->data));
60 a4ca7389 Vitor Sessak
    memcpy(ref->linesize, pic->linesize, sizeof(pic->linesize));
61 4dbbcdee Vitor Sessak
62
    return ref;
63
}
64
65
void avfilter_default_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
66
{
67 3628fbe0 Vitor Sessak
    AVFilterLink *out = link->dst->outputs[0];
68
69 4dbbcdee Vitor Sessak
    link->cur_pic = picref;
70 3628fbe0 Vitor Sessak
71
    if(out) {
72
        out->outpic  = avfilter_get_video_buffer(out, AV_PERM_WRITE);
73
        avfilter_start_frame(out, avfilter_ref_pic(out->outpic, ~0));
74
    }
75 4dbbcdee Vitor Sessak
}
76
77
void avfilter_default_end_frame(AVFilterLink *link)
78
{
79 3628fbe0 Vitor Sessak
    AVFilterLink *out = link->dst->outputs[0];
80
81 4dbbcdee Vitor Sessak
    avfilter_unref_pic(link->cur_pic);
82
    link->cur_pic = NULL;
83 3628fbe0 Vitor Sessak
84
    if(out) {
85
        avfilter_unref_pic(out->outpic);
86
        out->outpic = NULL;
87
        avfilter_end_frame(out);
88
    }
89 4dbbcdee Vitor Sessak
}
90
91 efb36bfc Vitor Sessak
AVFilterPicRef *avfilter_ref_pic(AVFilterPicRef *ref, int pmask)
92 4dbbcdee Vitor Sessak
{
93
    AVFilterPicRef *ret = av_malloc(sizeof(AVFilterPicRef));
94
    memcpy(ret, ref, sizeof(AVFilterPicRef));
95 efb36bfc Vitor Sessak
    ret->perms &= pmask;
96 4dbbcdee Vitor Sessak
    ret->pic->refcount ++;
97
    return ret;
98
}
99
100
void avfilter_unref_pic(AVFilterPicRef *ref)
101
{
102
    if(-- ref->pic->refcount == 0)
103
        ref->pic->free(ref->pic);
104
    av_free(ref);
105
}
106
107 102fb0e3 Vitor Sessak
/**
108
 * default config_link() implementation for output video links to simplify
109
 * the implementation of one input one output video filters */
110
static int default_config_output_link(AVFilterLink *link)
111
{
112
    link->w = link->src->inputs[0]->w;
113
    link->h = link->src->inputs[0]->h;
114
115
    return 0;
116
}
117
118
/**
119
 * default query_formats() implementation for output video links to simplify
120
 * the implementation of one input one output video filters */
121
static int *default_query_output_formats(AVFilterLink *link)
122
{
123
    return avfilter_make_format_list(1, link->src->inputs[0]->format);
124
}
125
126 4dbbcdee Vitor Sessak
int avfilter_link(AVFilterContext *src, unsigned srcpad,
127
                  AVFilterContext *dst, unsigned dstpad)
128
{
129
    AVFilterLink *link;
130 e0752603 Vitor Sessak
    int *fmts[2], i, j;
131 4dbbcdee Vitor Sessak
132
    if(src->outputs[srcpad] || dst->inputs[dstpad])
133
        return -1;
134
135
    src->outputs[srcpad] =
136
    dst->inputs[dstpad]  = link = av_malloc(sizeof(AVFilterLink));
137
138
    link->src = src;
139
    link->dst = dst;
140
    link->srcpad = srcpad;
141
    link->dstpad = dstpad;
142
    link->cur_pic = NULL;
143
144 e0752603 Vitor Sessak
    /* find a format both filters support - TODO: auto-insert conversion filter */
145 998a7aa3 Vitor Sessak
    link->format = -1;
146 102fb0e3 Vitor Sessak
    if(src->filter->outputs[srcpad].query_formats)
147
        fmts[0] = src->filter->outputs[srcpad].query_formats(link);
148
    else
149
        fmts[0] = default_query_output_formats(link);
150 ba6b9035 Vitor Sessak
    fmts[1] = dst->filter-> inputs[dstpad].query_formats(link);
151 e0752603 Vitor Sessak
    for(i = 0; fmts[0][i] != -1; i ++)
152
        for(j = 0; fmts[1][j] != -1; j ++)
153
            if(fmts[0][i] == fmts[1][j]) {
154
                link->format = fmts[0][i];
155
                goto format_done;
156
            }
157
158
format_done:
159
    av_free(fmts[0]);
160
    av_free(fmts[1]);
161
    if(link->format == -1) {
162
        /* failed to find a format.  fail at creating the link */
163
        av_free(link);
164
        src->outputs[srcpad] = NULL;
165
        dst->inputs[dstpad]  = NULL;
166
        return -1;
167
    }
168
169 102fb0e3 Vitor Sessak
    if (src->filter->outputs[srcpad].config_props)
170
        src->filter->outputs[srcpad].config_props(link);
171
    else
172
        default_config_output_link(link);
173
174
    if (dst->filter->inputs[dstpad].config_props)
175
        dst->filter->inputs[dstpad].config_props(link);
176
177 4dbbcdee Vitor Sessak
    return 0;
178
}
179
180
AVFilterPicRef *avfilter_get_video_buffer(AVFilterLink *link, int perms)
181
{
182
    AVFilterPicRef *ret = NULL;
183
184
    if(link->dst->filter->inputs[link->dstpad].get_video_buffer)
185
        ret = link->dst->filter->inputs[link->dstpad].get_video_buffer(link, perms);
186
187
    if(!ret)
188
        ret = avfilter_default_get_video_buffer(link, perms);
189
190
    return ret;
191
}
192
193
void avfilter_request_frame(AVFilterLink *link)
194
{
195 9586ba3a Vitor Sessak
    const AVFilterPad *pad = &link->src->filter->outputs[link->srcpad];
196
197
    if(pad->request_frame)
198
        pad->request_frame(link);
199
    else if(link->src->inputs[0])
200
        avfilter_request_frame(link->src->inputs[0]);
201 4dbbcdee Vitor Sessak
}
202
203
/* XXX: should we do the duplicating of the picture ref here, instead of
204
 * forcing the source filter to do it? */
205
void avfilter_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
206
{
207
    void (*start_frame)(AVFilterLink *, AVFilterPicRef *);
208
209
    start_frame = link->dst->filter->inputs[link->dstpad].start_frame;
210
    if(!start_frame)
211
        start_frame = avfilter_default_start_frame;
212
213
    start_frame(link, picref);
214
}
215
216
void avfilter_end_frame(AVFilterLink *link)
217
{
218
    void (*end_frame)(AVFilterLink *);
219
220
    end_frame = link->dst->filter->inputs[link->dstpad].end_frame;
221
    if(!end_frame)
222
        end_frame = avfilter_default_end_frame;
223
224
    end_frame(link);
225
}
226
227
void avfilter_draw_slice(AVFilterLink *link, uint8_t *data[4], int y, int h)
228
{
229
    link->dst->filter->inputs[link->dstpad].draw_slice(link, data, y, h);
230
}
231
232
static int filter_cmp(const void *aa, const void *bb)
233
{
234
    const AVFilter *a = *(const AVFilter **)aa, *b = *(const AVFilter **)bb;
235
    return strcmp(a->name, b->name);
236
}
237
238
AVFilter *avfilter_get_by_name(char *name)
239
{
240
    AVFilter key = { .name = name, };
241
    AVFilter *key2 = &key;
242
    AVFilter **ret;
243
244
    ret = bsearch(&key2, filters, filter_count, sizeof(AVFilter **), filter_cmp);
245
    if(ret)
246
        return *ret;
247
    return NULL;
248
}
249
250
/* FIXME: insert in order, rather than insert at end + resort */
251
void avfilter_register(AVFilter *filter)
252
{
253
    filters = av_realloc(filters, sizeof(AVFilter*) * (filter_count+1));
254
    filters[filter_count] = filter;
255
    qsort(filters, ++filter_count, sizeof(AVFilter **), filter_cmp);
256
}
257
258
void avfilter_init(void)
259
{
260 d72a138e Vitor Sessak
    avfilter_register(&vsrc_dummy);
261 eaf7eb66 Vitor Sessak
    avfilter_register(&vsrc_ppm);
262 d72a138e Vitor Sessak
    avfilter_register(&vf_crop);
263
    avfilter_register(&vf_passthrough);
264 72e3037a Vitor Sessak
    avfilter_register(&vf_rgb2bgr);
265 ce356b09 Vitor Sessak
    avfilter_register(&vf_slicify);
266 d72a138e Vitor Sessak
    avfilter_register(&vo_sdl);
267 4dbbcdee Vitor Sessak
}
268
269
void avfilter_uninit(void)
270
{
271
    av_freep(&filters);
272
    filter_count = 0;
273
}
274
275
static int pad_count(const AVFilterPad *pads)
276
{
277
    AVFilterPad *p = (AVFilterPad *) pads;
278
    int count;
279
280
    for(count = 0; p->name; count ++) p ++;
281
    return count;
282
}
283
284
static const char *filter_name(void *p)
285
{
286
    AVFilterContext *filter = p;
287
    return filter->filter->name;
288
}
289
290
AVFilterContext *avfilter_create(AVFilter *filter)
291
{
292
    AVFilterContext *ret = av_malloc(sizeof(AVFilterContext));
293
294
    ret->av_class = av_mallocz(sizeof(AVClass));
295
    ret->av_class->item_name = filter_name;
296
    ret->filter   = filter;
297
    ret->inputs   = av_mallocz(sizeof(AVFilterLink*) * pad_count(filter->inputs));
298
    ret->outputs  = av_mallocz(sizeof(AVFilterLink*) * pad_count(filter->outputs));
299
    ret->priv     = av_mallocz(filter->priv_size);
300
301
    return ret;
302
}
303
304
void avfilter_destroy(AVFilterContext *filter)
305
{
306
    int i;
307
308
    if(filter->filter->uninit)
309
        filter->filter->uninit(filter);
310
311
    for(i = 0; i < pad_count(filter->filter->inputs); i ++) {
312
        if(filter->inputs[i])
313
            filter->inputs[i]->src->outputs[filter->inputs[i]->srcpad] = NULL;
314
        av_free(filter->inputs[i]);
315
    }
316
    for(i = 0; i < pad_count(filter->filter->outputs); i ++) {
317
        if(filter->outputs[i])
318
            filter->outputs[i]->dst->inputs[filter->outputs[i]->dstpad] = NULL;
319
        av_free(filter->outputs[i]);
320
    }
321
322
    av_free(filter->inputs);
323
    av_free(filter->outputs);
324
    av_free(filter->priv);
325
    av_free(filter->av_class);
326
    av_free(filter);
327
}
328
329
AVFilterContext *avfilter_create_by_name(char *name)
330
{
331
    AVFilter *filt;
332
333
    if(!(filt = avfilter_get_by_name(name))) return NULL;
334
    return avfilter_create(filt);
335
}
336
337 89e64908 Vitor Sessak
int avfilter_init_filter(AVFilterContext *filter, const char *args)
338 4dbbcdee Vitor Sessak
{
339
    int ret, i;
340
341
    if(filter->filter->init)
342 89e64908 Vitor Sessak
        if((ret = filter->filter->init(filter, args))) return ret;
343 4dbbcdee Vitor Sessak
    return 0;
344
}
345
346 e0752603 Vitor Sessak
int *avfilter_make_format_list(int len, ...)
347
{
348
    int *ret, i;
349
    va_list vl;
350
351
    ret = av_malloc(sizeof(int) * (len + 1));
352
    va_start(vl, len);
353
    for(i = 0; i < len; i ++)
354
        ret[i] = va_arg(vl, int);
355
    va_end(vl);
356
    ret[len] = -1;
357
358
    return ret;
359
}