Statistics
| Branch: | Revision:

ffmpeg / libavfilter / avfilter.c @ 34017fd9

History | View | Annotate | Download (17.7 KB)

1 4dbbcdee Vitor Sessak
/*
2 16790dc3 Vitor Sessak
 * filter layer
3 4dbbcdee Vitor Sessak
 * 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 96da1c51 Stefano Sabatini
/* #define DEBUG */
23
24 ad2c9501 S.N. Hemanth Meenakshisundaram
#include "libavcodec/audioconvert.c"
25 b4b66456 Stefano Sabatini
#include "libavutil/pixdesc.h"
26 81c1ecab Stefano Sabatini
#include "libavcore/imgutils.h"
27 4dbbcdee Vitor Sessak
#include "avfilter.h"
28 9f0e31d2 Stefano Sabatini
#include "internal.h"
29 4dbbcdee Vitor Sessak
30 540f1c7b Stefano Sabatini
unsigned avfilter_version(void) {
31
    return LIBAVFILTER_VERSION_INT;
32
}
33
34 41600690 Stefano Sabatini
const char *avfilter_configuration(void)
35 c1736936 Diego Biurrun
{
36
    return FFMPEG_CONFIGURATION;
37
}
38
39 41600690 Stefano Sabatini
const char *avfilter_license(void)
40 c1736936 Diego Biurrun
{
41
#define LICENSE_PREFIX "libavfilter license: "
42
    return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
43
}
44
45 6fb0648d Vitor Sessak
/** helper macros to get the in/out pad on the dst/src filter */
46
#define link_dpad(link)     link->dst-> input_pads[link->dstpad]
47
#define link_spad(link)     link->src->output_pads[link->srcpad]
48
49 7fce481a S.N. Hemanth Meenakshisundaram
AVFilterBufferRef *avfilter_ref_buffer(AVFilterBufferRef *ref, int pmask)
50 4dbbcdee Vitor Sessak
{
51 ecc8dada S.N. Hemanth Meenakshisundaram
    AVFilterBufferRef *ret = av_malloc(sizeof(AVFilterBufferRef));
52 4f9ce3c4 Stefano Sabatini
    if (!ret)
53
        return NULL;
54 cdf2a332 Vitor Sessak
    *ret = *ref;
55 cc80caff S.N. Hemanth Meenakshisundaram
    if (ref->type == AVMEDIA_TYPE_VIDEO) {
56
        ret->video = av_malloc(sizeof(AVFilterBufferRefVideoProps));
57 4f9ce3c4 Stefano Sabatini
        if (!ret->video) {
58
            av_free(ret);
59
            return NULL;
60
        }
61 cc80caff S.N. Hemanth Meenakshisundaram
        *ret->video = *ref->video;
62 ad2c9501 S.N. Hemanth Meenakshisundaram
    } else if (ref->type == AVMEDIA_TYPE_AUDIO) {
63
        ret->audio = av_malloc(sizeof(AVFilterBufferRefAudioProps));
64
        if (!ret->audio) {
65
            av_free(ret);
66
            return NULL;
67
        }
68
        *ret->audio = *ref->audio;
69 cc80caff S.N. Hemanth Meenakshisundaram
    }
70 efb36bfc Vitor Sessak
    ret->perms &= pmask;
71 7fce481a S.N. Hemanth Meenakshisundaram
    ret->buf->refcount ++;
72 4dbbcdee Vitor Sessak
    return ret;
73
}
74
75 7fce481a S.N. Hemanth Meenakshisundaram
void avfilter_unref_buffer(AVFilterBufferRef *ref)
76 4dbbcdee Vitor Sessak
{
77 ca857431 Stefano Sabatini
    if (!(--ref->buf->refcount))
78 7fce481a S.N. Hemanth Meenakshisundaram
        ref->buf->free(ref->buf);
79 cc80caff S.N. Hemanth Meenakshisundaram
    av_free(ref->video);
80 ad2c9501 S.N. Hemanth Meenakshisundaram
    av_free(ref->audio);
81 4dbbcdee Vitor Sessak
    av_free(ref);
82
}
83
84 24618441 Vitor Sessak
void avfilter_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
85
                         AVFilterPad **pads, AVFilterLink ***links,
86
                         AVFilterPad *newpad)
87
{
88
    unsigned i;
89
90
    idx = FFMIN(idx, *count);
91
92
    *pads  = av_realloc(*pads,  sizeof(AVFilterPad)   * (*count + 1));
93
    *links = av_realloc(*links, sizeof(AVFilterLink*) * (*count + 1));
94
    memmove(*pads +idx+1, *pads +idx, sizeof(AVFilterPad)   * (*count-idx));
95
    memmove(*links+idx+1, *links+idx, sizeof(AVFilterLink*) * (*count-idx));
96
    memcpy(*pads+idx, newpad, sizeof(AVFilterPad));
97
    (*links)[idx] = NULL;
98
99 ca857431 Stefano Sabatini
    (*count)++;
100
    for (i = idx+1; i < *count; i++)
101
        if (*links[i])
102
            (*(unsigned *)((uint8_t *) *links[i] + padidx_off))++;
103 24618441 Vitor Sessak
}
104
105 4dbbcdee Vitor Sessak
int avfilter_link(AVFilterContext *src, unsigned srcpad,
106
                  AVFilterContext *dst, unsigned dstpad)
107
{
108
    AVFilterLink *link;
109
110 ca857431 Stefano Sabatini
    if (src->output_count <= srcpad || dst->input_count <= dstpad ||
111
        src->outputs[srcpad]        || dst->inputs[dstpad])
112 4dbbcdee Vitor Sessak
        return -1;
113
114
    src->outputs[srcpad] =
115 b3431ecd Vitor Sessak
    dst-> inputs[dstpad] = link = av_mallocz(sizeof(AVFilterLink));
116 4dbbcdee Vitor Sessak
117 1653c11f Vitor Sessak
    link->src     = src;
118
    link->dst     = dst;
119
    link->srcpad  = srcpad;
120
    link->dstpad  = dstpad;
121 bdab614b S.N. Hemanth Meenakshisundaram
    link->type    = src->output_pads[srcpad].type;
122
    assert(PIX_FMT_NONE == -1 && SAMPLE_FMT_NONE == -1);
123
    link->format  = -1;
124 01942f1d Vitor Sessak
125
    return 0;
126
}
127
128 52362e9d Vitor Sessak
int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
129
                           unsigned in, unsigned out)
130
{
131 08f8b51f Stefano Sabatini
    av_log(link->dst, AV_LOG_INFO, "auto-inserting filter '%s' "
132
           "between the filter '%s' and the filter '%s'\n",
133
           filt->name, link->src->name, link->dst->name);
134 52362e9d Vitor Sessak
135
    link->dst->inputs[link->dstpad] = NULL;
136 ca857431 Stefano Sabatini
    if (avfilter_link(filt, out, link->dst, link->dstpad)) {
137 52362e9d Vitor Sessak
        /* failed to link output filter to new filter */
138
        link->dst->inputs[link->dstpad] = link;
139
        return -1;
140
    }
141
142
    /* re-hookup the link to the new destination filter we inserted */
143
    link->dst = filt;
144
    link->dstpad = in;
145
    filt->inputs[in] = link;
146
147 bdab614b S.N. Hemanth Meenakshisundaram
    /* if any information on supported media formats already exists on the
148 07d0bba5 Vitor Sessak
     * link, we need to preserve that */
149 ca857431 Stefano Sabatini
    if (link->out_formats)
150 e3065c8b Vitor Sessak
        avfilter_formats_changeref(&link->out_formats,
151
                                   &filt->outputs[out]->out_formats);
152 07d0bba5 Vitor Sessak
153 52362e9d Vitor Sessak
    return 0;
154
}
155
156 69818105 Vitor Sessak
int avfilter_config_links(AVFilterContext *filter)
157 01942f1d Vitor Sessak
{
158
    int (*config_link)(AVFilterLink *);
159 69818105 Vitor Sessak
    unsigned i;
160
161 ca857431 Stefano Sabatini
    for (i = 0; i < filter->input_count; i ++) {
162 e079d22e Vitor Sessak
        AVFilterLink *link = filter->inputs[i];
163 d21cbbff Vitor Sessak
164 ca857431 Stefano Sabatini
        if (!link) continue;
165 69818105 Vitor Sessak
166 ca857431 Stefano Sabatini
        switch (link->init_state) {
167 69818105 Vitor Sessak
        case AVLINK_INIT:
168
            continue;
169
        case AVLINK_STARTINIT:
170 7a9fd2a0 Vitor Sessak
            av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
171
            return 0;
172 69818105 Vitor Sessak
        case AVLINK_UNINIT:
173
            link->init_state = AVLINK_STARTINIT;
174
175 ca857431 Stefano Sabatini
            if (avfilter_config_links(link->src))
176 69818105 Vitor Sessak
                return -1;
177 4dbbcdee Vitor Sessak
178 ca857431 Stefano Sabatini
            if (!(config_link = link_spad(link).config_props))
179 2fd46877 Vitor Sessak
                config_link  = avfilter_default_config_output_link;
180 ca857431 Stefano Sabatini
            if (config_link(link))
181 2fd46877 Vitor Sessak
                return -1;
182 102fb0e3 Vitor Sessak
183 ca857431 Stefano Sabatini
            if ((config_link = link_dpad(link).config_props))
184
                if (config_link(link))
185 a5b5d95a Vitor Sessak
                    return -1;
186 102fb0e3 Vitor Sessak
187 69818105 Vitor Sessak
            link->init_state = AVLINK_INIT;
188
        }
189
    }
190
191 4dbbcdee Vitor Sessak
    return 0;
192
}
193
194 eedca998 Stefano Sabatini
char *ff_get_ref_perms_string(char *buf, size_t buf_size, int perms)
195
{
196
    snprintf(buf, buf_size, "%s%s%s%s%s",
197
             perms & AV_PERM_READ      ? "r" : "",
198
             perms & AV_PERM_WRITE     ? "w" : "",
199
             perms & AV_PERM_PRESERVE  ? "p" : "",
200
             perms & AV_PERM_REUSE     ? "r" : "",
201
             perms & AV_PERM_REUSE2    ? "R" : "");
202
    return buf;
203
}
204
205 b5fbb31e Stefano Sabatini
void ff_dprintf_ref(void *ctx, AVFilterBufferRef *ref, int end)
206 96da1c51 Stefano Sabatini
{
207 eedca998 Stefano Sabatini
    av_unused char buf[16];
208 96da1c51 Stefano Sabatini
    dprintf(ctx,
209 eedca998 Stefano Sabatini
            "ref[%p buf:%p refcount:%d perms:%s data:%p linesize[%d, %d, %d, %d] pts:%"PRId64" pos:%"PRId64,
210
            ref, ref->buf, ref->buf->refcount, ff_get_ref_perms_string(buf, sizeof(buf), ref->perms), ref->data[0],
211 b5fbb31e Stefano Sabatini
            ref->linesize[0], ref->linesize[1], ref->linesize[2], ref->linesize[3],
212
            ref->pts, ref->pos);
213 382ecd35 Stefano Sabatini
214 b5fbb31e Stefano Sabatini
    if (ref->video) {
215 801ab980 Stefano Sabatini
        dprintf(ctx, " a:%d/%d s:%dx%d i:%c",
216 b5fbb31e Stefano Sabatini
                ref->video->pixel_aspect.num, ref->video->pixel_aspect.den,
217
                ref->video->w, ref->video->h,
218
                !ref->video->interlaced     ? 'P' :         /* Progressive  */
219
                ref->video->top_field_first ? 'T' : 'B');   /* Top / Bottom */
220 382ecd35 Stefano Sabatini
    }
221 014df3a2 Stefano Sabatini
    if (ref->audio) {
222
        dprintf(ctx, " cl:%"PRId64"d sn:%d s:%d sr:%d p:%d",
223
                ref->audio->channel_layout,
224
                ref->audio->samples_nb,
225
                ref->audio->size,
226
                ref->audio->sample_rate,
227
                ref->audio->planar);
228
    }
229
230 382ecd35 Stefano Sabatini
    dprintf(ctx, "]%s", end ? "\n" : "");
231 96da1c51 Stefano Sabatini
}
232
233 9f0e31d2 Stefano Sabatini
void ff_dprintf_link(void *ctx, AVFilterLink *link, int end)
234 96da1c51 Stefano Sabatini
{
235
    dprintf(ctx,
236 8fdb01c1 Stefano Sabatini
            "link[%p s:%dx%d fmt:%-16s %-16s->%-16s]%s",
237 96da1c51 Stefano Sabatini
            link, link->w, link->h,
238 b4b66456 Stefano Sabatini
            av_pix_fmt_descriptors[link->format].name,
239 96da1c51 Stefano Sabatini
            link->src ? link->src->filter->name : "",
240
            link->dst ? link->dst->filter->name : "",
241
            end ? "\n" : "");
242
}
243
244 ecc8dada S.N. Hemanth Meenakshisundaram
AVFilterBufferRef *avfilter_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
245 4dbbcdee Vitor Sessak
{
246 ecc8dada S.N. Hemanth Meenakshisundaram
    AVFilterBufferRef *ret = NULL;
247 4dbbcdee Vitor Sessak
248 eedca998 Stefano Sabatini
    av_unused char buf[16];
249
    FF_DPRINTF_START(NULL, get_video_buffer); ff_dprintf_link(NULL, link, 0);
250
    dprintf(NULL, " perms:%s w:%d h:%d\n", ff_get_ref_perms_string(buf, sizeof(buf), perms), w, h);
251 96da1c51 Stefano Sabatini
252 ca857431 Stefano Sabatini
    if (link_dpad(link).get_video_buffer)
253 0eb4ff9e Stefano Sabatini
        ret = link_dpad(link).get_video_buffer(link, perms, w, h);
254
255 ca857431 Stefano Sabatini
    if (!ret)
256 0eb4ff9e Stefano Sabatini
        ret = avfilter_default_get_video_buffer(link, perms, w, h);
257 4dbbcdee Vitor Sessak
258 382ac60e S.N. Hemanth Meenakshisundaram
    if (ret)
259
        ret->type = AVMEDIA_TYPE_VIDEO;
260
261 b5fbb31e Stefano Sabatini
    FF_DPRINTF_START(NULL, get_video_buffer); ff_dprintf_link(NULL, link, 0); dprintf(NULL, " returning "); ff_dprintf_ref(NULL, ret, 1);
262 96da1c51 Stefano Sabatini
263 4dbbcdee Vitor Sessak
    return ret;
264
}
265
266 ad2c9501 S.N. Hemanth Meenakshisundaram
AVFilterBufferRef *avfilter_get_audio_buffer(AVFilterLink *link, int perms,
267
                                             enum SampleFormat sample_fmt, int size,
268
                                             int64_t channel_layout, int planar)
269
{
270
    AVFilterBufferRef *ret = NULL;
271
272
    if (link_dpad(link).get_audio_buffer)
273
        ret = link_dpad(link).get_audio_buffer(link, perms, sample_fmt, size, channel_layout, planar);
274
275
    if (!ret)
276
        ret = avfilter_default_get_audio_buffer(link, perms, sample_fmt, size, channel_layout, planar);
277
278
    if (ret)
279
        ret->type = AVMEDIA_TYPE_AUDIO;
280
281
    return ret;
282
}
283
284 3306f8bd Vitor Sessak
int avfilter_request_frame(AVFilterLink *link)
285 4dbbcdee Vitor Sessak
{
286 9f0e31d2 Stefano Sabatini
    FF_DPRINTF_START(NULL, request_frame); ff_dprintf_link(NULL, link, 1);
287 96da1c51 Stefano Sabatini
288 ca857431 Stefano Sabatini
    if (link_spad(link).request_frame)
289 6fb0648d Vitor Sessak
        return link_spad(link).request_frame(link);
290 ca857431 Stefano Sabatini
    else if (link->src->inputs[0])
291 3306f8bd Vitor Sessak
        return avfilter_request_frame(link->src->inputs[0]);
292
    else return -1;
293 4dbbcdee Vitor Sessak
}
294
295 7b02c484 Vitor Sessak
int avfilter_poll_frame(AVFilterLink *link)
296
{
297 ca857431 Stefano Sabatini
    int i, min = INT_MAX;
298 7b02c484 Vitor Sessak
299 ca857431 Stefano Sabatini
    if (link_spad(link).poll_frame)
300 7b02c484 Vitor Sessak
        return link_spad(link).poll_frame(link);
301 c245ddf2 Vitor Sessak
302 ca857431 Stefano Sabatini
    for (i = 0; i < link->src->input_count; i++) {
303 9dd08b4e Vitor Sessak
        int val;
304 ca857431 Stefano Sabatini
        if (!link->src->inputs[i])
305 c245ddf2 Vitor Sessak
            return -1;
306 9dd08b4e Vitor Sessak
        val = avfilter_poll_frame(link->src->inputs[i]);
307
        min = FFMIN(min, val);
308 c245ddf2 Vitor Sessak
    }
309 7b02c484 Vitor Sessak
310
    return min;
311
}
312
313 4dbbcdee Vitor Sessak
/* XXX: should we do the duplicating of the picture ref here, instead of
314
 * forcing the source filter to do it? */
315 ecc8dada S.N. Hemanth Meenakshisundaram
void avfilter_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
316 4dbbcdee Vitor Sessak
{
317 ecc8dada S.N. Hemanth Meenakshisundaram
    void (*start_frame)(AVFilterLink *, AVFilterBufferRef *);
318 ac8a116e Vitor Sessak
    AVFilterPad *dst = &link_dpad(link);
319 4dbbcdee Vitor Sessak
320 b5fbb31e Stefano Sabatini
    FF_DPRINTF_START(NULL, start_frame); ff_dprintf_link(NULL, link, 0); dprintf(NULL, " "); ff_dprintf_ref(NULL, picref, 1);
321 96da1c51 Stefano Sabatini
322 ca857431 Stefano Sabatini
    if (!(start_frame = dst->start_frame))
323 4dbbcdee Vitor Sessak
        start_frame = avfilter_default_start_frame;
324
325 c949d867 Vitor Sessak
    /* prepare to copy the picture if it has insufficient permissions */
326 ca857431 Stefano Sabatini
    if ((dst->min_perms & picref->perms) != dst->min_perms ||
327
         dst->rej_perms & picref->perms) {
328 dc46661a Stefano Sabatini
        av_log(link->dst, AV_LOG_DEBUG,
329 c949d867 Vitor Sessak
                "frame copy needed (have perms %x, need %x, reject %x)\n",
330 269b9847 Vitor Sessak
                picref->perms,
331 c949d867 Vitor Sessak
                link_dpad(link).min_perms, link_dpad(link).rej_perms);
332
333 5a9dd3de Stefano Sabatini
        link->cur_buf = avfilter_get_video_buffer(link, dst->min_perms, link->w, link->h);
334 5d4890d7 S.N. Hemanth Meenakshisundaram
        link->src_buf = picref;
335
        avfilter_copy_buffer_ref_props(link->cur_buf, link->src_buf);
336 c949d867 Vitor Sessak
    }
337
    else
338 5d4890d7 S.N. Hemanth Meenakshisundaram
        link->cur_buf = picref;
339 c949d867 Vitor Sessak
340 5d4890d7 S.N. Hemanth Meenakshisundaram
    start_frame(link, link->cur_buf);
341 4dbbcdee Vitor Sessak
}
342
343
void avfilter_end_frame(AVFilterLink *link)
344
{
345
    void (*end_frame)(AVFilterLink *);
346
347 ca857431 Stefano Sabatini
    if (!(end_frame = link_dpad(link).end_frame))
348 35db407e Vitor Sessak
        end_frame = avfilter_default_end_frame;
349
350
    end_frame(link);
351
352 c949d867 Vitor Sessak
    /* unreference the source picture if we're feeding the destination filter
353
     * a copied version dues to permission issues */
354 ca857431 Stefano Sabatini
    if (link->src_buf) {
355 5d4890d7 S.N. Hemanth Meenakshisundaram
        avfilter_unref_buffer(link->src_buf);
356
        link->src_buf = NULL;
357 c949d867 Vitor Sessak
    }
358 4dbbcdee Vitor Sessak
}
359
360 a13a5437 Stefano Sabatini
void avfilter_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
361 4dbbcdee Vitor Sessak
{
362 c949d867 Vitor Sessak
    uint8_t *src[4], *dst[4];
363 2bb96256 Bobby Bingham
    int i, j, vsub;
364 a13a5437 Stefano Sabatini
    void (*draw_slice)(AVFilterLink *, int, int, int);
365 c949d867 Vitor Sessak
366 9f0e31d2 Stefano Sabatini
    FF_DPRINTF_START(NULL, draw_slice); ff_dprintf_link(NULL, link, 0); dprintf(NULL, " y:%d h:%d dir:%d\n", y, h, slice_dir);
367 96da1c51 Stefano Sabatini
368 c949d867 Vitor Sessak
    /* copy the slice if needed for permission reasons */
369 ca857431 Stefano Sabatini
    if (link->src_buf) {
370 b4b66456 Stefano Sabatini
        vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
371 c949d867 Vitor Sessak
372 ca857431 Stefano Sabatini
        for (i = 0; i < 4; i++) {
373
            if (link->src_buf->data[i]) {
374 5d4890d7 S.N. Hemanth Meenakshisundaram
                src[i] = link->src_buf-> data[i] +
375
                    (y >> (i==0 ? 0 : vsub)) * link->src_buf-> linesize[i];
376
                dst[i] = link->cur_buf->data[i] +
377
                    (y >> (i==0 ? 0 : vsub)) * link->cur_buf->linesize[i];
378 c949d867 Vitor Sessak
            } else
379
                src[i] = dst[i] = NULL;
380
        }
381 19dc7104 Vitor Sessak
382 ca857431 Stefano Sabatini
        for (i = 0; i < 4; i++) {
383 805d0232 Vitor Sessak
            int planew =
384 e16f217c Stefano Sabatini
                av_image_get_linesize(link->format, link->cur_buf->video->w, i);
385 805d0232 Vitor Sessak
386 ca857431 Stefano Sabatini
            if (!src[i]) continue;
387 c949d867 Vitor Sessak
388 ca857431 Stefano Sabatini
            for (j = 0; j < h >> (i==0 ? 0 : vsub); j++) {
389 805d0232 Vitor Sessak
                memcpy(dst[i], src[i], planew);
390 ca857431 Stefano Sabatini
                src[i] += link->src_buf->linesize[i];
391 5d4890d7 S.N. Hemanth Meenakshisundaram
                dst[i] += link->cur_buf->linesize[i];
392 c949d867 Vitor Sessak
            }
393
        }
394
    }
395
396 ca857431 Stefano Sabatini
    if (!(draw_slice = link_dpad(link).draw_slice))
397 b9609848 Stefano Sabatini
        draw_slice = avfilter_default_draw_slice;
398 a13a5437 Stefano Sabatini
    draw_slice(link, y, h, slice_dir);
399 4dbbcdee Vitor Sessak
}
400
401 ad2c9501 S.N. Hemanth Meenakshisundaram
void avfilter_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
402
{
403
    void (*filter_samples)(AVFilterLink *, AVFilterBufferRef *);
404
    AVFilterPad *dst = &link_dpad(link);
405
406
    if (!(filter_samples = dst->filter_samples))
407
        filter_samples = avfilter_default_filter_samples;
408
409
    /* prepare to copy the samples if the buffer has insufficient permissions */
410
    if ((dst->min_perms & samplesref->perms) != dst->min_perms ||
411
        dst->rej_perms & samplesref->perms) {
412
413
        av_log(link->dst, AV_LOG_DEBUG,
414
               "Copying audio data in avfilter (have perms %x, need %x, reject %x)\n",
415
               samplesref->perms, link_dpad(link).min_perms, link_dpad(link).rej_perms);
416
417
        link->cur_buf = avfilter_default_get_audio_buffer(link, dst->min_perms,
418
                                                          samplesref->format,
419
                                                          samplesref->audio->size,
420
                                                          samplesref->audio->channel_layout,
421
                                                          samplesref->audio->planar);
422
        link->cur_buf->pts                = samplesref->pts;
423
        link->cur_buf->audio->sample_rate = samplesref->audio->sample_rate;
424
425
        /* Copy actual data into new samples buffer */
426
        memcpy(link->cur_buf->data[0], samplesref->data[0], samplesref->audio->size);
427
428
        avfilter_unref_buffer(samplesref);
429
    } else
430
        link->cur_buf = samplesref;
431
432
    filter_samples(link, link->cur_buf);
433
}
434
435 86a60fa1 Stefano Sabatini
#define MAX_REGISTERED_AVFILTERS_NB 64
436
437
static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1];
438
439
static int next_registered_avfilter_idx = 0;
440 3555d2e8 Stefano Sabatini
441 05decb00 Vitor Sessak
AVFilter *avfilter_get_by_name(const char *name)
442 4dbbcdee Vitor Sessak
{
443 86a60fa1 Stefano Sabatini
    int i;
444 6d8c67a7 Vitor Sessak
445 86a60fa1 Stefano Sabatini
    for (i = 0; registered_avfilters[i]; i++)
446
        if (!strcmp(registered_avfilters[i]->name, name))
447
            return registered_avfilters[i];
448 4dbbcdee Vitor Sessak
449
    return NULL;
450
}
451
452 86a60fa1 Stefano Sabatini
int avfilter_register(AVFilter *filter)
453 4dbbcdee Vitor Sessak
{
454 86a60fa1 Stefano Sabatini
    if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB)
455
        return -1;
456 6d8c67a7 Vitor Sessak
457 86a60fa1 Stefano Sabatini
    registered_avfilters[next_registered_avfilter_idx++] = filter;
458
    return 0;
459 4dbbcdee Vitor Sessak
}
460
461 1433c4ab Stefano Sabatini
AVFilter **av_filter_next(AVFilter **filter)
462
{
463
    return filter ? ++filter : &registered_avfilters[0];
464
}
465
466 4dbbcdee Vitor Sessak
void avfilter_uninit(void)
467
{
468 86a60fa1 Stefano Sabatini
    memset(registered_avfilters, 0, sizeof(registered_avfilters));
469
    next_registered_avfilter_idx = 0;
470 4dbbcdee Vitor Sessak
}
471
472
static int pad_count(const AVFilterPad *pads)
473
{
474
    int count;
475
476 05decb00 Vitor Sessak
    for(count = 0; pads->name; count ++) pads ++;
477 4dbbcdee Vitor Sessak
    return count;
478
}
479
480
static const char *filter_name(void *p)
481
{
482
    AVFilterContext *filter = p;
483
    return filter->filter->name;
484
}
485
486 b049ad50 Måns Rullgård
static const AVClass avfilter_class = {
487
    "AVFilter",
488 2308b6c1 Michael Niedermayer
    filter_name,
489
    NULL,
490
    LIBAVUTIL_VERSION_INT,
491 b049ad50 Måns Rullgård
};
492
493 84c03869 Stefano Sabatini
int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
494 4dbbcdee Vitor Sessak
{
495 8f618f4c Vitor Sessak
    AVFilterContext *ret;
496 84c03869 Stefano Sabatini
    *filter_ctx = NULL;
497 8f618f4c Vitor Sessak
498
    if (!filter)
499 84c03869 Stefano Sabatini
        return AVERROR(EINVAL);
500 8f618f4c Vitor Sessak
501 689a5f49 Stefano Sabatini
    ret = av_mallocz(sizeof(AVFilterContext));
502 4dbbcdee Vitor Sessak
503 b049ad50 Måns Rullgård
    ret->av_class = &avfilter_class;
504 4dbbcdee Vitor Sessak
    ret->filter   = filter;
505 2350e69c Vitor Sessak
    ret->name     = inst_name ? av_strdup(inst_name) : NULL;
506 4dbbcdee Vitor Sessak
    ret->priv     = av_mallocz(filter->priv_size);
507
508 7d0e1392 Vitor Sessak
    ret->input_count  = pad_count(filter->inputs);
509 689a5f49 Stefano Sabatini
    if (ret->input_count) {
510 90993b38 Stefano Sabatini
        ret->input_pads   = av_malloc(sizeof(AVFilterPad) * ret->input_count);
511
        memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->input_count);
512
        ret->inputs       = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
513 689a5f49 Stefano Sabatini
    }
514 c5ef7d7b Vitor Sessak
515 7d0e1392 Vitor Sessak
    ret->output_count = pad_count(filter->outputs);
516 689a5f49 Stefano Sabatini
    if (ret->output_count) {
517 90993b38 Stefano Sabatini
        ret->output_pads  = av_malloc(sizeof(AVFilterPad) * ret->output_count);
518
        memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->output_count);
519
        ret->outputs      = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
520 689a5f49 Stefano Sabatini
    }
521 7d0e1392 Vitor Sessak
522 84c03869 Stefano Sabatini
    *filter_ctx = ret;
523
    return 0;
524 4dbbcdee Vitor Sessak
}
525
526
void avfilter_destroy(AVFilterContext *filter)
527
{
528
    int i;
529
530 ca857431 Stefano Sabatini
    if (filter->filter->uninit)
531 4dbbcdee Vitor Sessak
        filter->filter->uninit(filter);
532
533 ca857431 Stefano Sabatini
    for (i = 0; i < filter->input_count; i++) {
534
        if (filter->inputs[i]) {
535 0bb7408e Måns Rullgård
            if (filter->inputs[i]->src)
536 5a6fbf4c Måns Rullgård
                filter->inputs[i]->src->outputs[filter->inputs[i]->srcpad] = NULL;
537 f3b90d25 Måns Rullgård
            avfilter_formats_unref(&filter->inputs[i]->in_formats);
538
            avfilter_formats_unref(&filter->inputs[i]->out_formats);
539
        }
540 f4cb4462 Vitor Sessak
        av_freep(&filter->inputs[i]);
541 4dbbcdee Vitor Sessak
    }
542 ca857431 Stefano Sabatini
    for (i = 0; i < filter->output_count; i++) {
543
        if (filter->outputs[i]) {
544 0bb7408e Måns Rullgård
            if (filter->outputs[i]->dst)
545 5a6fbf4c Måns Rullgård
                filter->outputs[i]->dst->inputs[filter->outputs[i]->dstpad] = NULL;
546 f3b90d25 Måns Rullgård
            avfilter_formats_unref(&filter->outputs[i]->in_formats);
547
            avfilter_formats_unref(&filter->outputs[i]->out_formats);
548
        }
549 f4cb4462 Vitor Sessak
        av_freep(&filter->outputs[i]);
550 4dbbcdee Vitor Sessak
    }
551
552 f4cb4462 Vitor Sessak
    av_freep(&filter->name);
553
    av_freep(&filter->input_pads);
554
    av_freep(&filter->output_pads);
555
    av_freep(&filter->inputs);
556
    av_freep(&filter->outputs);
557
    av_freep(&filter->priv);
558 4dbbcdee Vitor Sessak
    av_free(filter);
559
}
560
561 a360f71e Vitor Sessak
int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
562 4dbbcdee Vitor Sessak
{
563 e079d22e Vitor Sessak
    int ret=0;
564 4dbbcdee Vitor Sessak
565 ca857431 Stefano Sabatini
    if (filter->filter->init)
566 e079d22e Vitor Sessak
        ret = filter->filter->init(filter, args, opaque);
567
    return ret;
568 4dbbcdee Vitor Sessak
}