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