Statistics
| Branch: | Revision:

ffmpeg / libavfilter / defaults.c @ 610312e9

History | View | Annotate | Download (10.8 KB)

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