Statistics
| Branch: | Revision:

ffmpeg / libavfilter / defaults.c @ 5d6e4c16

History | View | Annotate | Download (10.8 KB)

1
/*
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
#include "libavcore/imgutils.h"
23
#include "libavcore/samplefmt.h"
24
#include "libavcodec/audioconvert.h"
25
#include "avfilter.h"
26

    
27
/* TODO: buffer pool.  see comment for avfilter_default_get_video_buffer() */
28
static void avfilter_default_free_buffer(AVFilterBuffer *ptr)
29
{
30
    av_free(ptr->data[0]);
31
    av_free(ptr);
32
}
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
AVFilterBufferRef *avfilter_default_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
38
{
39
    AVFilterBuffer *pic = av_mallocz(sizeof(AVFilterBuffer));
40
    AVFilterBufferRef *ref = NULL;
41
    int i, tempsize;
42
    char *buf = NULL;
43

    
44
    if (!pic || !(ref = av_mallocz(sizeof(AVFilterBufferRef))))
45
        goto fail;
46

    
47
    ref->buf         = pic;
48
    ref->video       = av_mallocz(sizeof(AVFilterBufferRefVideoProps));
49
    ref->video->w    = w;
50
    ref->video->h    = h;
51

    
52
    /* make sure the buffer gets read permission or it's useless for output */
53
    ref->perms = perms | AV_PERM_READ;
54

    
55
    pic->refcount = 1;
56
    ref->format   = link->format;
57
    pic->free     = avfilter_default_free_buffer;
58
    av_image_fill_linesizes(pic->linesize, ref->format, ref->video->w);
59

    
60
    for (i = 0; i < 4; i++)
61
        pic->linesize[i] = FFALIGN(pic->linesize[i], 16);
62

    
63
    tempsize = av_image_fill_pointers(pic->data, ref->format, ref->video->h, NULL, pic->linesize);
64
    buf = av_malloc(tempsize + 16); // +2 is needed for swscaler, +16 to be
65
                                    // SIMD-friendly
66
    if (!buf)
67
        goto fail;
68
    av_image_fill_pointers(pic->data, ref->format, ref->video->h, buf, pic->linesize);
69

    
70
    memcpy(ref->data,     pic->data,     sizeof(ref->data));
71
    memcpy(ref->linesize, pic->linesize, sizeof(ref->linesize));
72

    
73
    return ref;
74

    
75
fail:
76
    av_free(buf);
77
    if (ref && ref->video)
78
        av_free(ref->video);
79
    av_free(ref);
80
    av_free(pic);
81
    return NULL;
82
}
83

    
84
AVFilterBufferRef *avfilter_default_get_audio_buffer(AVFilterLink *link, int perms,
85
                                                     enum AVSampleFormat sample_fmt, int size,
86
                                                     int64_t channel_layout, int planar)
87
{
88
    AVFilterBuffer *samples = av_mallocz(sizeof(AVFilterBuffer));
89
    AVFilterBufferRef *ref = NULL;
90
    int i, sample_size, chans_nb, bufsize, per_channel_size, step_size = 0;
91
    char *buf;
92

    
93
    if (!samples || !(ref = av_mallocz(sizeof(AVFilterBufferRef))))
94
        goto fail;
95

    
96
    ref->buf                   = samples;
97
    ref->format                = sample_fmt;
98

    
99
    ref->audio = av_mallocz(sizeof(AVFilterBufferRefAudioProps));
100
    if (!ref->audio)
101
        goto fail;
102

    
103
    ref->audio->channel_layout = channel_layout;
104
    ref->audio->size           = size;
105
    ref->audio->planar         = planar;
106

    
107
    /* make sure the buffer gets read permission or it's useless for output */
108
    ref->perms = perms | AV_PERM_READ;
109

    
110
    samples->refcount   = 1;
111
    samples->free       = avfilter_default_free_buffer;
112

    
113
    sample_size = av_get_bits_per_sample_fmt(sample_fmt) >>3;
114
    chans_nb = avcodec_channel_layout_num_channels(channel_layout);
115

    
116
    per_channel_size = size/chans_nb;
117
    ref->audio->samples_nb = per_channel_size/sample_size;
118

    
119
    /* Set the number of bytes to traverse to reach next sample of a particular channel:
120
     * For planar, this is simply the sample size.
121
     * For packed, this is the number of samples * sample_size.
122
     */
123
    for (i = 0; i < chans_nb; i++)
124
        samples->linesize[i] = planar > 0 ? per_channel_size : sample_size;
125
    memset(&samples->linesize[chans_nb], 0, (8-chans_nb) * sizeof(samples->linesize[0]));
126

    
127
    /* Calculate total buffer size, round to multiple of 16 to be SIMD friendly */
128
    bufsize = (size + 15)&~15;
129
    buf = av_malloc(bufsize);
130
    if (!buf)
131
        goto fail;
132

    
133
    /* For planar, set the start point of each channel's data within the buffer
134
     * For packed, set the start point of the entire buffer only
135
     */
136
    samples->data[0] = buf;
137
    if (buf && planar) {
138
        for (i = 1; i < chans_nb; i++) {
139
            step_size += per_channel_size;
140
            samples->data[i] = buf + step_size;
141
        }
142
    } else {
143
        for (i = 1; i < chans_nb; i++)
144
            samples->data[i] = buf;
145
    }
146

    
147
    memset(&samples->data[chans_nb], 0, (8-chans_nb) * sizeof(samples->data[0]));
148

    
149
    memcpy(ref->data,     samples->data,     sizeof(ref->data));
150
    memcpy(ref->linesize, samples->linesize, sizeof(ref->linesize));
151

    
152
    return ref;
153

    
154
fail:
155
    av_free(buf);
156
    if (ref && ref->audio)
157
        av_free(ref->audio);
158
    av_free(ref);
159
    av_free(samples);
160
    return NULL;
161
}
162

    
163
void avfilter_default_start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
164
{
165
    AVFilterLink *outlink = NULL;
166

    
167
    if (inlink->dst->output_count)
168
        outlink = inlink->dst->outputs[0];
169

    
170
    if (outlink) {
171
        outlink->out_buf = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
172
        avfilter_copy_buffer_ref_props(outlink->out_buf, picref);
173
        avfilter_start_frame(outlink, avfilter_ref_buffer(outlink->out_buf, ~0));
174
    }
175
}
176

    
177
void avfilter_default_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
178
{
179
    AVFilterLink *outlink = NULL;
180

    
181
    if (inlink->dst->output_count)
182
        outlink = inlink->dst->outputs[0];
183

    
184
    if (outlink)
185
        avfilter_draw_slice(outlink, y, h, slice_dir);
186
}
187

    
188
void avfilter_default_end_frame(AVFilterLink *inlink)
189
{
190
    AVFilterLink *outlink = NULL;
191

    
192
    if (inlink->dst->output_count)
193
        outlink = inlink->dst->outputs[0];
194

    
195
    avfilter_unref_buffer(inlink->cur_buf);
196
    inlink->cur_buf = NULL;
197

    
198
    if (outlink) {
199
        if (outlink->out_buf) {
200
            avfilter_unref_buffer(outlink->out_buf);
201
            outlink->out_buf = NULL;
202
        }
203
        avfilter_end_frame(outlink);
204
    }
205
}
206

    
207
/* FIXME: samplesref is same as link->cur_buf. Need to consider removing the redundant parameter. */
208
void avfilter_default_filter_samples(AVFilterLink *inlink, AVFilterBufferRef *samplesref)
209
{
210
    AVFilterLink *outlink = NULL;
211

    
212
    if (inlink->dst->output_count)
213
        outlink = inlink->dst->outputs[0];
214

    
215
    if (outlink) {
216
        outlink->out_buf = avfilter_default_get_audio_buffer(inlink, AV_PERM_WRITE, samplesref->format,
217
                                                             samplesref->audio->size,
218
                                                             samplesref->audio->channel_layout,
219
                                                             samplesref->audio->planar);
220
        outlink->out_buf->pts                = samplesref->pts;
221
        outlink->out_buf->audio->sample_rate = samplesref->audio->sample_rate;
222
        avfilter_filter_samples(outlink, avfilter_ref_buffer(outlink->out_buf, ~0));
223
        avfilter_unref_buffer(outlink->out_buf);
224
        outlink->out_buf = NULL;
225
    }
226
    avfilter_unref_buffer(samplesref);
227
    inlink->cur_buf = NULL;
228
}
229

    
230
/**
231
 * default config_link() implementation for output video links to simplify
232
 * the implementation of one input one output video filters */
233
int avfilter_default_config_output_link(AVFilterLink *link)
234
{
235
    if (link->src->input_count && link->src->inputs[0]) {
236
        if (link->type == AVMEDIA_TYPE_VIDEO) {
237
            link->w = link->src->inputs[0]->w;
238
            link->h = link->src->inputs[0]->h;
239
            link->time_base = link->src->inputs[0]->time_base;
240
        } else if (link->type == AVMEDIA_TYPE_AUDIO) {
241
            link->channel_layout = link->src->inputs[0]->channel_layout;
242
            link->sample_rate    = link->src->inputs[0]->sample_rate;
243
        }
244
    } else {
245
        /* XXX: any non-simple filter which would cause this branch to be taken
246
         * really should implement its own config_props() for this link. */
247
        return -1;
248
    }
249

    
250
    return 0;
251
}
252

    
253
/**
254
 * A helper for query_formats() which sets all links to the same list of
255
 * formats. If there are no links hooked to this filter, the list of formats is
256
 * freed.
257
 *
258
 * FIXME: this will need changed for filters with a mix of pad types
259
 * (video + audio, etc)
260
 */
261
void avfilter_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
262
{
263
    int count = 0, i;
264

    
265
    for (i = 0; i < ctx->input_count; i++) {
266
        if (ctx->inputs[i]) {
267
            avfilter_formats_ref(formats, &ctx->inputs[i]->out_formats);
268
            count++;
269
        }
270
    }
271
    for (i = 0; i < ctx->output_count; i++) {
272
        if (ctx->outputs[i]) {
273
            avfilter_formats_ref(formats, &ctx->outputs[i]->in_formats);
274
            count++;
275
        }
276
    }
277

    
278
    if (!count) {
279
        av_free(formats->formats);
280
        av_free(formats->refs);
281
        av_free(formats);
282
    }
283
}
284

    
285
int avfilter_default_query_formats(AVFilterContext *ctx)
286
{
287
    enum AVMediaType type = ctx->inputs  && ctx->inputs [0] ? ctx->inputs [0]->type :
288
                            ctx->outputs && ctx->outputs[0] ? ctx->outputs[0]->type :
289
                            AVMEDIA_TYPE_VIDEO;
290

    
291
    avfilter_set_common_formats(ctx, avfilter_all_formats(type));
292
    return 0;
293
}
294

    
295
void avfilter_null_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
296
{
297
    avfilter_start_frame(link->dst->outputs[0], picref);
298
}
299

    
300
void avfilter_null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
301
{
302
    avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
303
}
304

    
305
void avfilter_null_end_frame(AVFilterLink *link)
306
{
307
    avfilter_end_frame(link->dst->outputs[0]);
308
}
309

    
310
void avfilter_null_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
311
{
312
    avfilter_filter_samples(link->dst->outputs[0], samplesref);
313
}
314

    
315
AVFilterBufferRef *avfilter_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
316
{
317
    return avfilter_get_video_buffer(link->dst->outputs[0], perms, w, h);
318
}
319

    
320
AVFilterBufferRef *avfilter_null_get_audio_buffer(AVFilterLink *link, int perms,
321
                                                  enum AVSampleFormat sample_fmt, int size,
322
                                                  int64_t channel_layout, int packed)
323
{
324
    return avfilter_get_audio_buffer(link->dst->outputs[0], perms, sample_fmt,
325
                                     size, channel_layout, packed);
326
}
327