Statistics
| Branch: | Revision:

ffmpeg / libavfilter / defaults.c @ master

History | View | Annotate | Download (10.7 KB)

1 bf4ce7a3 Vitor Sessak
/*
2
 * Filter layer - default implementations
3 3fa77bde Stefano Sabatini
 * Copyright (c) 2007 Bobby Bingham
4 bf4ce7a3 Vitor Sessak
 *
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 7ffe76e5 Reinhard Tartler
#include "libavutil/audioconvert.h"
23
#include "libavutil/imgutils.h"
24
#include "libavutil/samplefmt.h"
25 bf4ce7a3 Vitor Sessak
#include "avfilter.h"
26 4723bc28 Stefano Sabatini
#include "internal.h"
27 bf4ce7a3 Vitor Sessak
28 4723bc28 Stefano Sabatini
void ff_avfilter_default_free_buffer(AVFilterBuffer *ptr)
29 bf4ce7a3 Vitor Sessak
{
30 f0d77b20 S.N. Hemanth Meenakshisundaram
    av_free(ptr->data[0]);
31
    av_free(ptr);
32 bf4ce7a3 Vitor Sessak
}
33
34
/* TODO: set the buffer's priv member to a context structure for the whole
35
 * filter chain.  This will allow for a buffer pool instead of the constant
36
 * alloc & free cycle currently implemented. */
37 ecc8dada S.N. Hemanth Meenakshisundaram
AVFilterBufferRef *avfilter_default_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
38 bf4ce7a3 Vitor Sessak
{
39 4da12e3b Stefano Sabatini
    int linesize[4];
40 d38c340f Stefano Sabatini
    uint8_t *data[4];
41 39f66edb Michael Niedermayer
    int i;
42 d38c340f Stefano Sabatini
    AVFilterBufferRef *picref = NULL;
43 39f66edb Michael Niedermayer
    AVFilterPool *pool= link->pool;
44
45
    if(pool) for(i=0; i<POOL_SIZE; i++){
46
        picref= pool->pic[i];
47
        if(picref && picref->buf->format == link->format && picref->buf->w == w && picref->buf->h == h){
48
            AVFilterBuffer *pic= picref->buf;
49
            pool->pic[i]= NULL;
50
            pool->count--;
51
            picref->video->w = w;
52
            picref->video->h = h;
53
            picref->perms = perms | AV_PERM_READ;
54
            picref->format= link->format;
55
            pic->refcount = 1;
56
            memcpy(picref->data,     pic->data,     sizeof(picref->data));
57
            memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
58
            return picref;
59
        }
60 c34cb130 Michael Niedermayer
    }else
61
        pool = link->pool = av_mallocz(sizeof(AVFilterPool));
62 65d4cab5 Stefano Sabatini
63 4da12e3b Stefano Sabatini
    // +2 is needed for swscaler, +16 to be SIMD-friendly
64 21d84246 Michael Niedermayer
    if ((i=av_image_alloc(data, linesize, w, h, link->format, 16)) < 0)
65 d38c340f Stefano Sabatini
        return NULL;
66 bf4ce7a3 Vitor Sessak
67 d38c340f Stefano Sabatini
    picref = avfilter_get_video_buffer_ref_from_arrays(data, linesize,
68
                                                       perms, w, h, link->format);
69
    if (!picref) {
70 4da12e3b Stefano Sabatini
        av_free(data[0]);
71 d38c340f Stefano Sabatini
        return NULL;
72
    }
73 21d84246 Michael Niedermayer
    memset(data[0], 128, i);
74
75 c34cb130 Michael Niedermayer
    picref->buf->priv= pool;
76 39f66edb Michael Niedermayer
    picref->buf->free= NULL;
77 65d4cab5 Stefano Sabatini
78 d38c340f Stefano Sabatini
    return picref;
79 bf4ce7a3 Vitor Sessak
}
80
81 ad2c9501 S.N. Hemanth Meenakshisundaram
AVFilterBufferRef *avfilter_default_get_audio_buffer(AVFilterLink *link, int perms,
82 5d6e4c16 Stefano Sabatini
                                                     enum AVSampleFormat sample_fmt, int size,
83 ad2c9501 S.N. Hemanth Meenakshisundaram
                                                     int64_t channel_layout, int planar)
84
{
85
    AVFilterBuffer *samples = av_mallocz(sizeof(AVFilterBuffer));
86
    AVFilterBufferRef *ref = NULL;
87
    int i, sample_size, chans_nb, bufsize, per_channel_size, step_size = 0;
88
    char *buf;
89
90
    if (!samples || !(ref = av_mallocz(sizeof(AVFilterBufferRef))))
91
        goto fail;
92
93
    ref->buf                   = samples;
94
    ref->format                = sample_fmt;
95
96
    ref->audio = av_mallocz(sizeof(AVFilterBufferRefAudioProps));
97
    if (!ref->audio)
98
        goto fail;
99
100
    ref->audio->channel_layout = channel_layout;
101
    ref->audio->size           = size;
102
    ref->audio->planar         = planar;
103
104
    /* make sure the buffer gets read permission or it's useless for output */
105
    ref->perms = perms | AV_PERM_READ;
106
107
    samples->refcount   = 1;
108 4723bc28 Stefano Sabatini
    samples->free       = ff_avfilter_default_free_buffer;
109 ad2c9501 S.N. Hemanth Meenakshisundaram
110 ba7d6e79 Stefano Sabatini
    sample_size = av_get_bits_per_sample_fmt(sample_fmt) >>3;
111 63e8d976 Stefano Sabatini
    chans_nb = av_get_channel_layout_nb_channels(channel_layout);
112 ad2c9501 S.N. Hemanth Meenakshisundaram
113
    per_channel_size = size/chans_nb;
114 a242ac36 Stefano Sabatini
    ref->audio->nb_samples = per_channel_size/sample_size;
115 ad2c9501 S.N. Hemanth Meenakshisundaram
116
    /* Set the number of bytes to traverse to reach next sample of a particular channel:
117
     * For planar, this is simply the sample size.
118
     * For packed, this is the number of samples * sample_size.
119
     */
120
    for (i = 0; i < chans_nb; i++)
121
        samples->linesize[i] = planar > 0 ? per_channel_size : sample_size;
122
    memset(&samples->linesize[chans_nb], 0, (8-chans_nb) * sizeof(samples->linesize[0]));
123
124
    /* Calculate total buffer size, round to multiple of 16 to be SIMD friendly */
125
    bufsize = (size + 15)&~15;
126
    buf = av_malloc(bufsize);
127
    if (!buf)
128
        goto fail;
129
130
    /* For planar, set the start point of each channel's data within the buffer
131
     * For packed, set the start point of the entire buffer only
132
     */
133
    samples->data[0] = buf;
134
    if (buf && planar) {
135
        for (i = 1; i < chans_nb; i++) {
136
            step_size += per_channel_size;
137
            samples->data[i] = buf + step_size;
138
        }
139
    } else {
140
        for (i = 1; i < chans_nb; i++)
141
            samples->data[i] = buf;
142
    }
143
144
    memset(&samples->data[chans_nb], 0, (8-chans_nb) * sizeof(samples->data[0]));
145
146
    memcpy(ref->data,     samples->data,     sizeof(ref->data));
147
    memcpy(ref->linesize, samples->linesize, sizeof(ref->linesize));
148
149
    return ref;
150
151
fail:
152 bffad70c Clément Bœsch
    if (ref)
153 ad2c9501 S.N. Hemanth Meenakshisundaram
        av_free(ref->audio);
154
    av_free(ref);
155
    av_free(samples);
156
    return NULL;
157
}
158
159 f167a1a4 Stefano Sabatini
void avfilter_default_start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
160 bf4ce7a3 Vitor Sessak
{
161 f167a1a4 Stefano Sabatini
    AVFilterLink *outlink = NULL;
162 bf4ce7a3 Vitor Sessak
163 f167a1a4 Stefano Sabatini
    if (inlink->dst->output_count)
164
        outlink = inlink->dst->outputs[0];
165 bf4ce7a3 Vitor Sessak
166 f167a1a4 Stefano Sabatini
    if (outlink) {
167
        outlink->out_buf = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
168
        avfilter_copy_buffer_ref_props(outlink->out_buf, picref);
169
        avfilter_start_frame(outlink, avfilter_ref_buffer(outlink->out_buf, ~0));
170 bf4ce7a3 Vitor Sessak
    }
171
}
172
173 f167a1a4 Stefano Sabatini
void avfilter_default_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
174 b9609848 Stefano Sabatini
{
175 f167a1a4 Stefano Sabatini
    AVFilterLink *outlink = NULL;
176 b9609848 Stefano Sabatini
177 f167a1a4 Stefano Sabatini
    if (inlink->dst->output_count)
178
        outlink = inlink->dst->outputs[0];
179 b9609848 Stefano Sabatini
180 f167a1a4 Stefano Sabatini
    if (outlink)
181
        avfilter_draw_slice(outlink, y, h, slice_dir);
182 b9609848 Stefano Sabatini
}
183
184 f167a1a4 Stefano Sabatini
void avfilter_default_end_frame(AVFilterLink *inlink)
185 bf4ce7a3 Vitor Sessak
{
186 f167a1a4 Stefano Sabatini
    AVFilterLink *outlink = NULL;
187 bf4ce7a3 Vitor Sessak
188 f167a1a4 Stefano Sabatini
    if (inlink->dst->output_count)
189
        outlink = inlink->dst->outputs[0];
190 bf4ce7a3 Vitor Sessak
191 f167a1a4 Stefano Sabatini
    avfilter_unref_buffer(inlink->cur_buf);
192
    inlink->cur_buf = NULL;
193 bf4ce7a3 Vitor Sessak
194 f167a1a4 Stefano Sabatini
    if (outlink) {
195
        if (outlink->out_buf) {
196
            avfilter_unref_buffer(outlink->out_buf);
197
            outlink->out_buf = NULL;
198 552c0208 Vitor Sessak
        }
199 f167a1a4 Stefano Sabatini
        avfilter_end_frame(outlink);
200 bf4ce7a3 Vitor Sessak
    }
201
}
202
203 ad2c9501 S.N. Hemanth Meenakshisundaram
/* FIXME: samplesref is same as link->cur_buf. Need to consider removing the redundant parameter. */
204 f167a1a4 Stefano Sabatini
void avfilter_default_filter_samples(AVFilterLink *inlink, AVFilterBufferRef *samplesref)
205 ad2c9501 S.N. Hemanth Meenakshisundaram
{
206
    AVFilterLink *outlink = NULL;
207
208 f167a1a4 Stefano Sabatini
    if (inlink->dst->output_count)
209
        outlink = inlink->dst->outputs[0];
210 ad2c9501 S.N. Hemanth Meenakshisundaram
211
    if (outlink) {
212 f167a1a4 Stefano Sabatini
        outlink->out_buf = avfilter_default_get_audio_buffer(inlink, AV_PERM_WRITE, samplesref->format,
213 ad2c9501 S.N. Hemanth Meenakshisundaram
                                                             samplesref->audio->size,
214
                                                             samplesref->audio->channel_layout,
215
                                                             samplesref->audio->planar);
216
        outlink->out_buf->pts                = samplesref->pts;
217
        outlink->out_buf->audio->sample_rate = samplesref->audio->sample_rate;
218
        avfilter_filter_samples(outlink, avfilter_ref_buffer(outlink->out_buf, ~0));
219
        avfilter_unref_buffer(outlink->out_buf);
220
        outlink->out_buf = NULL;
221
    }
222
    avfilter_unref_buffer(samplesref);
223 f167a1a4 Stefano Sabatini
    inlink->cur_buf = NULL;
224 ad2c9501 S.N. Hemanth Meenakshisundaram
}
225
226 bf4ce7a3 Vitor Sessak
/**
227
 * default config_link() implementation for output video links to simplify
228
 * the implementation of one input one output video filters */
229
int avfilter_default_config_output_link(AVFilterLink *link)
230
{
231 371cbff8 Stefano Sabatini
    if (link->src->input_count && link->src->inputs[0]) {
232 ad2c9501 S.N. Hemanth Meenakshisundaram
        if (link->type == AVMEDIA_TYPE_VIDEO) {
233
            link->w = link->src->inputs[0]->w;
234
            link->h = link->src->inputs[0]->h;
235 867ae7aa Stefano Sabatini
            link->time_base = link->src->inputs[0]->time_base;
236 ad2c9501 S.N. Hemanth Meenakshisundaram
        } else if (link->type == AVMEDIA_TYPE_AUDIO) {
237
            link->channel_layout = link->src->inputs[0]->channel_layout;
238
            link->sample_rate    = link->src->inputs[0]->sample_rate;
239
        }
240 bf4ce7a3 Vitor Sessak
    } else {
241
        /* XXX: any non-simple filter which would cause this branch to be taken
242
         * really should implement its own config_props() for this link. */
243 d0e4eba5 Vitor Sessak
        return -1;
244 bf4ce7a3 Vitor Sessak
    }
245
246
    return 0;
247
}
248
249
/**
250 e363730c Vitor Sessak
 * A helper for query_formats() which sets all links to the same list of
251
 * formats. If there are no links hooked to this filter, the list of formats is
252
 * freed.
253
 *
254
 * FIXME: this will need changed for filters with a mix of pad types
255
 * (video + audio, etc)
256
 */
257
void avfilter_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
258 bf4ce7a3 Vitor Sessak
{
259 e363730c Vitor Sessak
    int count = 0, i;
260
261 371cbff8 Stefano Sabatini
    for (i = 0; i < ctx->input_count; i++) {
262
        if (ctx->inputs[i]) {
263 e363730c Vitor Sessak
            avfilter_formats_ref(formats, &ctx->inputs[i]->out_formats);
264 371cbff8 Stefano Sabatini
            count++;
265 e363730c Vitor Sessak
        }
266
    }
267 371cbff8 Stefano Sabatini
    for (i = 0; i < ctx->output_count; i++) {
268
        if (ctx->outputs[i]) {
269 e363730c Vitor Sessak
            avfilter_formats_ref(formats, &ctx->outputs[i]->in_formats);
270 371cbff8 Stefano Sabatini
            count++;
271 e363730c Vitor Sessak
        }
272
    }
273
274 371cbff8 Stefano Sabatini
    if (!count) {
275 e363730c Vitor Sessak
        av_free(formats->formats);
276
        av_free(formats->refs);
277
        av_free(formats);
278
    }
279
}
280
281
int avfilter_default_query_formats(AVFilterContext *ctx)
282
{
283 12011922 Stefano Sabatini
    enum AVMediaType type = ctx->inputs  && ctx->inputs [0] ? ctx->inputs [0]->type :
284
                            ctx->outputs && ctx->outputs[0] ? ctx->outputs[0]->type :
285 bdab614b S.N. Hemanth Meenakshisundaram
                            AVMEDIA_TYPE_VIDEO;
286
287
    avfilter_set_common_formats(ctx, avfilter_all_formats(type));
288 e363730c Vitor Sessak
    return 0;
289 bf4ce7a3 Vitor Sessak
}
290
291 ecc8dada S.N. Hemanth Meenakshisundaram
void avfilter_null_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
292 91d1c741 Bobby Bingham
{
293
    avfilter_start_frame(link->dst->outputs[0], picref);
294
}
295
296
void avfilter_null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
297
{
298
    avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
299
}
300
301
void avfilter_null_end_frame(AVFilterLink *link)
302
{
303
    avfilter_end_frame(link->dst->outputs[0]);
304
}
305
306 ad2c9501 S.N. Hemanth Meenakshisundaram
void avfilter_null_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
307
{
308
    avfilter_filter_samples(link->dst->outputs[0], samplesref);
309
}
310
311 ecc8dada S.N. Hemanth Meenakshisundaram
AVFilterBufferRef *avfilter_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
312 91d1c741 Bobby Bingham
{
313
    return avfilter_get_video_buffer(link->dst->outputs[0], perms, w, h);
314
}
315
316 ad2c9501 S.N. Hemanth Meenakshisundaram
AVFilterBufferRef *avfilter_null_get_audio_buffer(AVFilterLink *link, int perms,
317 5d6e4c16 Stefano Sabatini
                                                  enum AVSampleFormat sample_fmt, int size,
318 ad2c9501 S.N. Hemanth Meenakshisundaram
                                                  int64_t channel_layout, int packed)
319
{
320
    return avfilter_get_audio_buffer(link->dst->outputs[0], perms, sample_fmt,
321
                                     size, channel_layout, packed);
322
}