Statistics
| Branch: | Revision:

ffmpeg / libavfilter / avfilter.c @ 5bc8ebd3

History | View | Annotate | Download (11 KB)

1
/*
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
#include "libavcodec/imgconvert.h"
23
#include "avfilter.h"
24

    
25
unsigned avfilter_version(void) {
26
    return LIBAVFILTER_VERSION_INT;
27
}
28

    
29
/** list of registered filters */
30
struct FilterList
31
{
32
    AVFilter *filter;
33
    struct FilterList *next;
34
} *filters = NULL;
35

    
36
/** helper macros to get the in/out pad on the dst/src filter */
37
#define link_dpad(link)     link->dst-> input_pads[link->dstpad]
38
#define link_spad(link)     link->src->output_pads[link->srcpad]
39

    
40
AVFilterPicRef *avfilter_ref_pic(AVFilterPicRef *ref, int pmask)
41
{
42
    AVFilterPicRef *ret = av_malloc(sizeof(AVFilterPicRef));
43
    *ret = *ref;
44
    ret->perms &= pmask;
45
    ret->pic->refcount ++;
46
    return ret;
47
}
48

    
49
void avfilter_unref_pic(AVFilterPicRef *ref)
50
{
51
    if(!(--ref->pic->refcount))
52
        ref->pic->free(ref->pic);
53
    av_free(ref);
54
}
55

    
56
void avfilter_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
57
                         AVFilterPad **pads, AVFilterLink ***links,
58
                         AVFilterPad *newpad)
59
{
60
    unsigned i;
61

    
62
    idx = FFMIN(idx, *count);
63

    
64
    *pads  = av_realloc(*pads,  sizeof(AVFilterPad)   * (*count + 1));
65
    *links = av_realloc(*links, sizeof(AVFilterLink*) * (*count + 1));
66
    memmove(*pads +idx+1, *pads +idx, sizeof(AVFilterPad)   * (*count-idx));
67
    memmove(*links+idx+1, *links+idx, sizeof(AVFilterLink*) * (*count-idx));
68
    memcpy(*pads+idx, newpad, sizeof(AVFilterPad));
69
    (*links)[idx] = NULL;
70

    
71
    (*count) ++;
72
    for(i = idx+1; i < *count; i ++)
73
        if(*links[i])
74
            (*(unsigned *)((uint8_t *) *links[i] + padidx_off)) ++;
75
}
76

    
77
int avfilter_link(AVFilterContext *src, unsigned srcpad,
78
                  AVFilterContext *dst, unsigned dstpad)
79
{
80
    AVFilterLink *link;
81

    
82
    if(src->output_count <= srcpad || dst->input_count <= dstpad ||
83
       src->outputs[srcpad]        || dst->inputs[dstpad])
84
        return -1;
85

    
86
    src->outputs[srcpad] =
87
    dst-> inputs[dstpad] = link = av_mallocz(sizeof(AVFilterLink));
88

    
89
    link->src     = src;
90
    link->dst     = dst;
91
    link->srcpad  = srcpad;
92
    link->dstpad  = dstpad;
93
    link->format  = PIX_FMT_NONE;
94

    
95
    return 0;
96
}
97

    
98
int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
99
                           unsigned in, unsigned out)
100
{
101
    av_log(link->dst, AV_LOG_INFO, "auto-inserting filter '%s'\n",
102
            filt->filter->name);
103

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

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

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

    
122
    return 0;
123
}
124

    
125
int avfilter_config_links(AVFilterContext *filter)
126
{
127
    int (*config_link)(AVFilterLink *);
128
    unsigned i;
129

    
130
    for(i = 0; i < filter->input_count; i ++) {
131
        AVFilterLink *link = filter->inputs[i];
132

    
133
        if(!link) continue;
134

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

    
144
            if(avfilter_config_links(link->src))
145
                return -1;
146

    
147
            if(!(config_link = link_spad(link).config_props))
148
                config_link  = avfilter_default_config_output_link;
149
            if(config_link(link))
150
                return -1;
151

    
152
            if((config_link = link_dpad(link).config_props))
153
                if(config_link(link))
154
                    return -1;
155

    
156
            link->init_state = AVLINK_INIT;
157
        }
158
    }
159

    
160
    return 0;
161
}
162

    
163
AVFilterPicRef *avfilter_get_video_buffer(AVFilterLink *link, int perms)
164
{
165
    AVFilterPicRef *ret = NULL;
166

    
167
    if(link_dpad(link).get_video_buffer)
168
        ret = link_dpad(link).get_video_buffer(link, perms);
169

    
170
    if(!ret)
171
        ret = avfilter_default_get_video_buffer(link, perms);
172

    
173
    return ret;
174
}
175

    
176
int avfilter_request_frame(AVFilterLink *link)
177
{
178
    if(link_spad(link).request_frame)
179
        return link_spad(link).request_frame(link);
180
    else if(link->src->inputs[0])
181
        return avfilter_request_frame(link->src->inputs[0]);
182
    else return -1;
183
}
184

    
185
int avfilter_poll_frame(AVFilterLink *link)
186
{
187
    int i, min=INT_MAX;
188

    
189
    if(link_spad(link).poll_frame)
190
        return link_spad(link).poll_frame(link);
191

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

    
198
    return min;
199
}
200

    
201
/* XXX: should we do the duplicating of the picture ref here, instead of
202
 * forcing the source filter to do it? */
203
void avfilter_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
204
{
205
    void (*start_frame)(AVFilterLink *, AVFilterPicRef *);
206
    AVFilterPad *dst = &link_dpad(link);
207

    
208
    if(!(start_frame = dst->start_frame))
209
        start_frame = avfilter_default_start_frame;
210

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

    
221
        link->cur_pic = avfilter_default_get_video_buffer(link, dst->min_perms);
222
        link->srcpic = picref;
223
        link->cur_pic->pts = link->srcpic->pts;
224
    }
225
    else
226
        link->cur_pic = picref;
227

    
228
    start_frame(link, link->cur_pic);
229
}
230

    
231
void avfilter_end_frame(AVFilterLink *link)
232
{
233
    void (*end_frame)(AVFilterLink *);
234

    
235
    if(!(end_frame = link_dpad(link).end_frame))
236
        end_frame = avfilter_default_end_frame;
237

    
238
    end_frame(link);
239

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

    
247
}
248

    
249
void avfilter_draw_slice(AVFilterLink *link, int y, int h)
250
{
251
    uint8_t *src[4], *dst[4];
252
    int i, j, hsub, vsub;
253

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

    
258
        for(i = 0; i < 4; i ++) {
259
            if(link->srcpic->data[i]) {
260
                src[i] = link->srcpic-> data[i] +
261
                    (y >> (i==0 ? 0 : vsub)) * link->srcpic-> linesize[i];
262
                dst[i] = link->cur_pic->data[i] +
263
                    (y >> (i==0 ? 0 : vsub)) * link->cur_pic->linesize[i];
264
            } else
265
                src[i] = dst[i] = NULL;
266
        }
267

    
268
        for(i = 0; i < 4; i ++) {
269
            int planew =
270
                ff_get_plane_bytewidth(link->format, link->cur_pic->w, i);
271

    
272
            if(!src[i]) continue;
273

    
274
            for(j = 0; j < h >> (i==0 ? 0 : vsub); j ++) {
275
                memcpy(dst[i], src[i], planew);
276
                src[i] += link->srcpic ->linesize[i];
277
                dst[i] += link->cur_pic->linesize[i];
278
            }
279
        }
280
    }
281

    
282
    if(link_dpad(link).draw_slice)
283
        link_dpad(link).draw_slice(link, y, h);
284
}
285

    
286
AVFilter *avfilter_get_by_name(const char *name)
287
{
288
    struct FilterList *filt;
289

    
290
    for(filt = filters; filt; filt = filt->next)
291
        if(!strcmp(filt->filter->name, name))
292
            return filt->filter;
293

    
294
    return NULL;
295
}
296

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

    
301
    newfilt->filter = filter;
302
    newfilt->next   = filters;
303
    filters         = newfilt;
304
}
305

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

    
310
    for(; filters; filters = tmp) {
311
        tmp = filters->next;
312
        av_free(filters);
313
    }
314
}
315

    
316
static int pad_count(const AVFilterPad *pads)
317
{
318
    int count;
319

    
320
    for(count = 0; pads->name; count ++) pads ++;
321
    return count;
322
}
323

    
324
static const char *filter_name(void *p)
325
{
326
    AVFilterContext *filter = p;
327
    return filter->filter->name;
328
}
329

    
330
static const AVClass avfilter_class = {
331
    "AVFilter",
332
    filter_name
333
};
334

    
335
AVFilterContext *avfilter_open(AVFilter *filter, const char *inst_name)
336
{
337
    AVFilterContext *ret;
338

    
339
    if (!filter)
340
        return 0;
341

    
342
    ret = av_malloc(sizeof(AVFilterContext));
343

    
344
    ret->av_class = &avfilter_class;
345
    ret->filter   = filter;
346
    ret->name     = inst_name ? av_strdup(inst_name) : NULL;
347
    ret->priv     = av_mallocz(filter->priv_size);
348

    
349
    ret->input_count  = pad_count(filter->inputs);
350
    ret->input_pads   = av_malloc(sizeof(AVFilterPad) * ret->input_count);
351
    memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad)*ret->input_count);
352
    ret->inputs       = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
353

    
354
    ret->output_count = pad_count(filter->outputs);
355
    ret->output_pads  = av_malloc(sizeof(AVFilterPad) * ret->output_count);
356
    memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad)*ret->output_count);
357
    ret->outputs      = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
358

    
359
    return ret;
360
}
361

    
362
void avfilter_destroy(AVFilterContext *filter)
363
{
364
    int i;
365

    
366
    if(filter->filter->uninit)
367
        filter->filter->uninit(filter);
368

    
369
    for(i = 0; i < filter->input_count; i ++) {
370
        if(filter->inputs[i])
371
            filter->inputs[i]->src->outputs[filter->inputs[i]->srcpad] = NULL;
372
        av_freep(&filter->inputs[i]);
373
    }
374
    for(i = 0; i < filter->output_count; i ++) {
375
        if(filter->outputs[i])
376
            filter->outputs[i]->dst->inputs[filter->outputs[i]->dstpad] = NULL;
377
        av_freep(&filter->outputs[i]);
378
    }
379

    
380
    av_freep(&filter->name);
381
    av_freep(&filter->input_pads);
382
    av_freep(&filter->output_pads);
383
    av_freep(&filter->inputs);
384
    av_freep(&filter->outputs);
385
    av_freep(&filter->priv);
386
    av_free(filter);
387
}
388

    
389
int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
390
{
391
    int ret=0;
392

    
393
    if(filter->filter->init)
394
        ret = filter->filter->init(filter, args, opaque);
395
    return ret;
396
}
397