]> git.sesse.net Git - ffmpeg/blob - libavfilter/defaults.c
Merge remote-tracking branch 'qatar/master'
[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) {
46         for (i = 0; i < POOL_SIZE; i++) {
47             picref = pool->pic[i];
48             if (picref && picref->buf->format == link->format && picref->buf->w == w && picref->buf->h == h) {
49                 AVFilterBuffer *pic = picref->buf;
50                 pool->pic[i] = NULL;
51                 pool->count--;
52                 picref->video->w = w;
53                 picref->video->h = h;
54                 picref->perms = perms | AV_PERM_READ;
55                 picref->format = link->format;
56                 pic->refcount = 1;
57                 memcpy(picref->data,     pic->data,     sizeof(picref->data));
58                 memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
59                 return picref;
60             }
61         }
62     } else
63         pool = link->pool = av_mallocz(sizeof(AVFilterPool));
64
65     // align: +2 is needed for swscaler, +16 to be SIMD-friendly
66     if ((i = av_image_alloc(data, linesize, w, h, link->format, 16)) < 0)
67         return NULL;
68
69     picref = avfilter_get_video_buffer_ref_from_arrays(data, linesize,
70                                                        perms, w, h, link->format);
71     if (!picref) {
72         av_free(data[0]);
73         return NULL;
74     }
75     memset(data[0], 128, i);
76
77     picref->buf->priv = pool;
78     picref->buf->free = NULL;
79
80     return picref;
81 }
82
83 AVFilterBufferRef *avfilter_default_get_audio_buffer(AVFilterLink *link, int perms,
84                                                      enum AVSampleFormat 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       = ff_avfilter_default_free_buffer;
111
112     sample_size = av_get_bits_per_sample_fmt(sample_fmt) >>3;
113     chans_nb = av_get_channel_layout_nb_channels(channel_layout);
114
115     per_channel_size = size/chans_nb;
116     ref->audio->nb_samples = 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     if (ref)
155         av_free(ref->audio);
156     av_free(ref);
157     av_free(samples);
158     return NULL;
159 }
160
161 void avfilter_default_start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
162 {
163     AVFilterLink *outlink = NULL;
164
165     if (inlink->dst->output_count)
166         outlink = inlink->dst->outputs[0];
167
168     if (outlink) {
169         outlink->out_buf = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
170         avfilter_copy_buffer_ref_props(outlink->out_buf, picref);
171         avfilter_start_frame(outlink, avfilter_ref_buffer(outlink->out_buf, ~0));
172     }
173 }
174
175 void avfilter_default_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
176 {
177     AVFilterLink *outlink = NULL;
178
179     if (inlink->dst->output_count)
180         outlink = inlink->dst->outputs[0];
181
182     if (outlink)
183         avfilter_draw_slice(outlink, y, h, slice_dir);
184 }
185
186 void avfilter_default_end_frame(AVFilterLink *inlink)
187 {
188     AVFilterLink *outlink = NULL;
189
190     if (inlink->dst->output_count)
191         outlink = inlink->dst->outputs[0];
192
193     avfilter_unref_buffer(inlink->cur_buf);
194     inlink->cur_buf = NULL;
195
196     if (outlink) {
197         if (outlink->out_buf) {
198             avfilter_unref_buffer(outlink->out_buf);
199             outlink->out_buf = NULL;
200         }
201         avfilter_end_frame(outlink);
202     }
203 }
204
205 /* FIXME: samplesref is same as link->cur_buf. Need to consider removing the redundant parameter. */
206 void avfilter_default_filter_samples(AVFilterLink *inlink, AVFilterBufferRef *samplesref)
207 {
208     AVFilterLink *outlink = NULL;
209
210     if (inlink->dst->output_count)
211         outlink = inlink->dst->outputs[0];
212
213     if (outlink) {
214         outlink->out_buf = avfilter_default_get_audio_buffer(inlink, AV_PERM_WRITE, samplesref->format,
215                                                              samplesref->audio->size,
216                                                              samplesref->audio->channel_layout,
217                                                              samplesref->audio->planar);
218         outlink->out_buf->pts                = samplesref->pts;
219         outlink->out_buf->audio->sample_rate = samplesref->audio->sample_rate;
220         avfilter_filter_samples(outlink, avfilter_ref_buffer(outlink->out_buf, ~0));
221         avfilter_unref_buffer(outlink->out_buf);
222         outlink->out_buf = NULL;
223     }
224     avfilter_unref_buffer(samplesref);
225     inlink->cur_buf = NULL;
226 }
227
228 /**
229  * default config_link() implementation for output video links to simplify
230  * the implementation of one input one output video filters */
231 int avfilter_default_config_output_link(AVFilterLink *link)
232 {
233     if (link->src->input_count && link->src->inputs[0]) {
234         if (link->type == AVMEDIA_TYPE_VIDEO) {
235             link->w = link->src->inputs[0]->w;
236             link->h = link->src->inputs[0]->h;
237             link->time_base = link->src->inputs[0]->time_base;
238         } else if (link->type == AVMEDIA_TYPE_AUDIO) {
239             link->channel_layout = link->src->inputs[0]->channel_layout;
240             link->sample_rate    = link->src->inputs[0]->sample_rate;
241         }
242     } else {
243         /* XXX: any non-simple filter which would cause this branch to be taken
244          * really should implement its own config_props() for this link. */
245         return -1;
246     }
247
248     return 0;
249 }
250
251 /**
252  * A helper for query_formats() which sets all links to the same list of
253  * formats. If there are no links hooked to this filter, the list of formats is
254  * freed.
255  *
256  * FIXME: this will need changed for filters with a mix of pad types
257  * (video + audio, etc)
258  */
259 void avfilter_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
260 {
261     int count = 0, i;
262
263     for (i = 0; i < ctx->input_count; i++) {
264         if (ctx->inputs[i]) {
265             avfilter_formats_ref(formats, &ctx->inputs[i]->out_formats);
266             count++;
267         }
268     }
269     for (i = 0; i < ctx->output_count; i++) {
270         if (ctx->outputs[i]) {
271             avfilter_formats_ref(formats, &ctx->outputs[i]->in_formats);
272             count++;
273         }
274     }
275
276     if (!count) {
277         av_free(formats->formats);
278         av_free(formats->refs);
279         av_free(formats);
280     }
281 }
282
283 int avfilter_default_query_formats(AVFilterContext *ctx)
284 {
285     enum AVMediaType type = ctx->inputs  && ctx->inputs [0] ? ctx->inputs [0]->type :
286                             ctx->outputs && ctx->outputs[0] ? ctx->outputs[0]->type :
287                             AVMEDIA_TYPE_VIDEO;
288
289     avfilter_set_common_formats(ctx, avfilter_all_formats(type));
290     return 0;
291 }
292
293 void avfilter_null_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
294 {
295     avfilter_start_frame(link->dst->outputs[0], picref);
296 }
297
298 void avfilter_null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
299 {
300     avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
301 }
302
303 void avfilter_null_end_frame(AVFilterLink *link)
304 {
305     avfilter_end_frame(link->dst->outputs[0]);
306 }
307
308 void avfilter_null_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
309 {
310     avfilter_filter_samples(link->dst->outputs[0], samplesref);
311 }
312
313 AVFilterBufferRef *avfilter_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
314 {
315     return avfilter_get_video_buffer(link->dst->outputs[0], perms, w, h);
316 }
317
318 AVFilterBufferRef *avfilter_null_get_audio_buffer(AVFilterLink *link, int perms,
319                                                   enum AVSampleFormat sample_fmt, int size,
320                                                   int64_t channel_layout, int packed)
321 {
322     return avfilter_get_audio_buffer(link->dst->outputs[0], perms, sample_fmt,
323                                      size, channel_layout, packed);
324 }
325