Statistics
| Branch: | Revision:

ffmpeg / libavfilter / defaults.c @ 65d4cab5

History | View | Annotate | Download (6.04 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 bf4ce7a3 Vitor Sessak
#include "avfilter.h"
24
25
/* TODO: buffer pool.  see comment for avfilter_default_get_video_buffer() */
26 f0d77b20 S.N. Hemanth Meenakshisundaram
static void avfilter_default_free_buffer(AVFilterBuffer *ptr)
27 bf4ce7a3 Vitor Sessak
{
28 f0d77b20 S.N. Hemanth Meenakshisundaram
    av_free(ptr->data[0]);
29
    av_free(ptr);
30 bf4ce7a3 Vitor Sessak
}
31
32
/* TODO: set the buffer's priv member to a context structure for the whole
33
 * filter chain.  This will allow for a buffer pool instead of the constant
34
 * alloc & free cycle currently implemented. */
35 ecc8dada S.N. Hemanth Meenakshisundaram
AVFilterBufferRef *avfilter_default_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
36 bf4ce7a3 Vitor Sessak
{
37 f0d77b20 S.N. Hemanth Meenakshisundaram
    AVFilterBuffer *pic = av_mallocz(sizeof(AVFilterBuffer));
38 65d4cab5 Stefano Sabatini
    AVFilterBufferRef *ref = NULL;
39 790a03d7 Vitor Sessak
    int i, tempsize;
40 65d4cab5 Stefano Sabatini
    char *buf = NULL;
41
42
    if (!pic || !(ref = av_mallocz(sizeof(AVFilterBufferRef))))
43
        goto fail;
44 bf4ce7a3 Vitor Sessak
45 cc80caff S.N. Hemanth Meenakshisundaram
    ref->buf         = pic;
46
    ref->video       = av_mallocz(sizeof(AVFilterBufferRefVideoProps));
47
    ref->video->w    = w;
48
    ref->video->h    = h;
49 4f909565 Vitor Sessak
50
    /* make sure the buffer gets read permission or it's useless for output */
51
    ref->perms = perms | AV_PERM_READ;
52 bf4ce7a3 Vitor Sessak
53
    pic->refcount = 1;
54 d54e0948 S.N. Hemanth Meenakshisundaram
    ref->format   = link->format;
55 f0d77b20 S.N. Hemanth Meenakshisundaram
    pic->free     = avfilter_default_free_buffer;
56 cc80caff S.N. Hemanth Meenakshisundaram
    av_fill_image_linesizes(pic->linesize, ref->format, ref->video->w);
57 790a03d7 Vitor Sessak
58
    for (i=0; i<4;i++)
59 ef516f73 David Conrad
        pic->linesize[i] = FFALIGN(pic->linesize[i], 16);
60 790a03d7 Vitor Sessak
61 cc80caff S.N. Hemanth Meenakshisundaram
    tempsize = av_fill_image_pointers(pic->data, ref->format, ref->video->h, NULL, pic->linesize);
62 8ad802e6 Vitor Sessak
    buf = av_malloc(tempsize + 16); // +2 is needed for swscaler, +16 to be
63
                                    // SIMD-friendly
64 65d4cab5 Stefano Sabatini
    if (!buf)
65
        goto fail;
66 cc80caff S.N. Hemanth Meenakshisundaram
    av_fill_image_pointers(pic->data, ref->format, ref->video->h, buf, pic->linesize);
67 bf4ce7a3 Vitor Sessak
68 c1db7bff S.N. Hemanth Meenakshisundaram
    memcpy(ref->data,     pic->data,     sizeof(ref->data));
69
    memcpy(ref->linesize, pic->linesize, sizeof(ref->linesize));
70 bf4ce7a3 Vitor Sessak
71
    return ref;
72 65d4cab5 Stefano Sabatini
73
fail:
74
    av_free(buf);
75
    if (ref && ref->video)
76
        av_free(ref->video);
77
    av_free(ref);
78
    av_free(pic);
79
    return NULL;
80 bf4ce7a3 Vitor Sessak
}
81
82 ecc8dada S.N. Hemanth Meenakshisundaram
void avfilter_default_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
83 bf4ce7a3 Vitor Sessak
{
84
    AVFilterLink *out = NULL;
85
86
    if(link->dst->output_count)
87
        out = link->dst->outputs[0];
88
89
    if(out) {
90 5d4890d7 S.N. Hemanth Meenakshisundaram
        out->out_buf      = avfilter_get_video_buffer(out, AV_PERM_WRITE, out->w, out->h);
91
        avfilter_copy_buffer_ref_props(out->out_buf, picref);
92
        avfilter_start_frame(out, avfilter_ref_buffer(out->out_buf, ~0));
93 bf4ce7a3 Vitor Sessak
    }
94
}
95
96 a13a5437 Stefano Sabatini
void avfilter_default_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
97 b9609848 Stefano Sabatini
{
98
    AVFilterLink *out = NULL;
99
100
    if(link->dst->output_count)
101
        out = link->dst->outputs[0];
102
103
    if(out)
104 a13a5437 Stefano Sabatini
        avfilter_draw_slice(out, y, h, slice_dir);
105 b9609848 Stefano Sabatini
}
106
107 bf4ce7a3 Vitor Sessak
void avfilter_default_end_frame(AVFilterLink *link)
108
{
109
    AVFilterLink *out = NULL;
110
111
    if(link->dst->output_count)
112
        out = link->dst->outputs[0];
113
114 5d4890d7 S.N. Hemanth Meenakshisundaram
    avfilter_unref_buffer(link->cur_buf);
115
    link->cur_buf = NULL;
116 bf4ce7a3 Vitor Sessak
117
    if(out) {
118 5d4890d7 S.N. Hemanth Meenakshisundaram
        if(out->out_buf) {
119
            avfilter_unref_buffer(out->out_buf);
120
            out->out_buf = NULL;
121 552c0208 Vitor Sessak
        }
122 bf4ce7a3 Vitor Sessak
        avfilter_end_frame(out);
123
    }
124
}
125
126
/**
127
 * default config_link() implementation for output video links to simplify
128
 * the implementation of one input one output video filters */
129
int avfilter_default_config_output_link(AVFilterLink *link)
130
{
131
    if(link->src->input_count && link->src->inputs[0]) {
132
        link->w = link->src->inputs[0]->w;
133
        link->h = link->src->inputs[0]->h;
134
    } else {
135
        /* XXX: any non-simple filter which would cause this branch to be taken
136
         * really should implement its own config_props() for this link. */
137 d0e4eba5 Vitor Sessak
        return -1;
138 bf4ce7a3 Vitor Sessak
    }
139
140
    return 0;
141
}
142
143
/**
144 e363730c Vitor Sessak
 * A helper for query_formats() which sets all links to the same list of
145
 * formats. If there are no links hooked to this filter, the list of formats is
146
 * freed.
147
 *
148
 * FIXME: this will need changed for filters with a mix of pad types
149
 * (video + audio, etc)
150
 */
151
void avfilter_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
152 bf4ce7a3 Vitor Sessak
{
153 e363730c Vitor Sessak
    int count = 0, i;
154
155
    for(i = 0; i < ctx->input_count; i ++) {
156
        if(ctx->inputs[i]) {
157
            avfilter_formats_ref(formats, &ctx->inputs[i]->out_formats);
158
            count ++;
159
        }
160
    }
161
    for(i = 0; i < ctx->output_count; i ++) {
162
        if(ctx->outputs[i]) {
163
            avfilter_formats_ref(formats, &ctx->outputs[i]->in_formats);
164
            count ++;
165
        }
166
    }
167
168
    if(!count) {
169
        av_free(formats->formats);
170
        av_free(formats->refs);
171
        av_free(formats);
172
    }
173
}
174
175
int avfilter_default_query_formats(AVFilterContext *ctx)
176
{
177 bdab614b S.N. Hemanth Meenakshisundaram
    enum AVMediaType type = ctx->inputs [0] ? ctx->inputs [0]->type :
178
                            ctx->outputs[0] ? ctx->outputs[0]->type :
179
                            AVMEDIA_TYPE_VIDEO;
180
181
    avfilter_set_common_formats(ctx, avfilter_all_formats(type));
182 e363730c Vitor Sessak
    return 0;
183 bf4ce7a3 Vitor Sessak
}
184
185 ecc8dada S.N. Hemanth Meenakshisundaram
void avfilter_null_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
186 91d1c741 Bobby Bingham
{
187
    avfilter_start_frame(link->dst->outputs[0], picref);
188
}
189
190
void avfilter_null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
191
{
192
    avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
193
}
194
195
void avfilter_null_end_frame(AVFilterLink *link)
196
{
197
    avfilter_end_frame(link->dst->outputs[0]);
198
}
199
200 ecc8dada S.N. Hemanth Meenakshisundaram
AVFilterBufferRef *avfilter_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
201 91d1c741 Bobby Bingham
{
202
    return avfilter_get_video_buffer(link->dst->outputs[0], perms, w, h);
203
}