2 * Filter layer - default implementations
3 * Copyright (c) 2007 Bobby Bingham
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavutil/audioconvert.h"
23 #include "libavutil/imgutils.h"
24 #include "libavutil/samplefmt.h"
28 void ff_avfilter_default_free_buffer(AVFilterBuffer *ptr)
30 av_free(ptr->data[0]);
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)
42 AVFilterBufferRef *picref = NULL;
43 AVFilterPool *pool = link->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;
54 picref->perms = perms | AV_PERM_READ;
55 picref->format = link->format;
57 memcpy(picref->data, pic->data, sizeof(picref->data));
58 memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
63 pool = link->pool = av_mallocz(sizeof(AVFilterPool));
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)
69 picref = avfilter_get_video_buffer_ref_from_arrays(data, linesize,
70 perms, w, h, link->format);
75 memset(data[0], 128, i);
77 picref->buf->priv = pool;
78 picref->buf->free = NULL;
83 AVFilterBufferRef *avfilter_default_get_audio_buffer(AVFilterLink *link, int perms,
84 enum AVSampleFormat sample_fmt, int nb_samples,
85 int64_t channel_layout, int planar)
87 AVFilterBufferRef *samplesref = NULL;
90 int nb_channels = av_get_channel_layout_nb_channels(channel_layout);
92 /* Calculate total buffer size, round to multiple of 16 to be SIMD friendly */
93 if (av_samples_alloc(data, linesize,
94 nb_channels, nb_samples, sample_fmt,
99 avfilter_get_audio_buffer_ref_from_arrays(data, linesize, perms,
100 nb_samples, sample_fmt,
101 channel_layout, planar);
110 void avfilter_default_start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
112 AVFilterLink *outlink = NULL;
114 if (inlink->dst->output_count)
115 outlink = inlink->dst->outputs[0];
118 outlink->out_buf = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
119 avfilter_copy_buffer_ref_props(outlink->out_buf, picref);
120 avfilter_start_frame(outlink, avfilter_ref_buffer(outlink->out_buf, ~0));
124 void avfilter_default_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
126 AVFilterLink *outlink = NULL;
128 if (inlink->dst->output_count)
129 outlink = inlink->dst->outputs[0];
132 avfilter_draw_slice(outlink, y, h, slice_dir);
135 void avfilter_default_end_frame(AVFilterLink *inlink)
137 AVFilterLink *outlink = NULL;
139 if (inlink->dst->output_count)
140 outlink = inlink->dst->outputs[0];
142 avfilter_unref_buffer(inlink->cur_buf);
143 inlink->cur_buf = NULL;
146 if (outlink->out_buf) {
147 avfilter_unref_buffer(outlink->out_buf);
148 outlink->out_buf = NULL;
150 avfilter_end_frame(outlink);
154 /* FIXME: samplesref is same as link->cur_buf. Need to consider removing the redundant parameter. */
155 void avfilter_default_filter_samples(AVFilterLink *inlink, AVFilterBufferRef *samplesref)
157 AVFilterLink *outlink = NULL;
159 if (inlink->dst->output_count)
160 outlink = inlink->dst->outputs[0];
163 outlink->out_buf = avfilter_default_get_audio_buffer(inlink, AV_PERM_WRITE, samplesref->format,
164 samplesref->audio->nb_samples,
165 samplesref->audio->channel_layout,
166 samplesref->audio->planar);
167 outlink->out_buf->pts = samplesref->pts;
168 outlink->out_buf->audio->sample_rate = samplesref->audio->sample_rate;
169 avfilter_filter_samples(outlink, avfilter_ref_buffer(outlink->out_buf, ~0));
170 avfilter_unref_buffer(outlink->out_buf);
171 outlink->out_buf = NULL;
173 avfilter_unref_buffer(samplesref);
174 inlink->cur_buf = NULL;
178 * default config_link() implementation for output video links to simplify
179 * the implementation of one input one output video filters */
180 int avfilter_default_config_output_link(AVFilterLink *link)
182 if (link->src->input_count && link->src->inputs[0]) {
183 if (link->type == AVMEDIA_TYPE_VIDEO) {
184 link->w = link->src->inputs[0]->w;
185 link->h = link->src->inputs[0]->h;
186 link->time_base = link->src->inputs[0]->time_base;
187 } else if (link->type == AVMEDIA_TYPE_AUDIO) {
188 link->channel_layout = link->src->inputs[0]->channel_layout;
189 link->sample_rate = link->src->inputs[0]->sample_rate;
192 /* XXX: any non-simple filter which would cause this branch to be taken
193 * really should implement its own config_props() for this link. */
200 static void set_common_formats(AVFilterContext *ctx, AVFilterFormats *fmts,
201 enum AVMediaType type, int offin, int offout)
204 for (i = 0; i < ctx->input_count; i++)
205 if (ctx->inputs[i] && ctx->inputs[i]->type == type)
206 avfilter_formats_ref(fmts,
207 (AVFilterFormats**)((void*)ctx->inputs[i]+offout));
209 for (i = 0; i < ctx->output_count; i++)
210 if (ctx->outputs[i] && ctx->outputs[i]->type == type)
211 avfilter_formats_ref(fmts,
212 (AVFilterFormats**)((void*)ctx->outputs[i]+offin));
214 if (!fmts->refcount) {
215 av_free(fmts->formats);
221 void avfilter_set_common_pixel_formats(AVFilterContext *ctx, AVFilterFormats *formats)
223 set_common_formats(ctx, formats, AVMEDIA_TYPE_VIDEO,
224 offsetof(AVFilterLink, in_formats),
225 offsetof(AVFilterLink, out_formats));
228 void avfilter_set_common_sample_formats(AVFilterContext *ctx, AVFilterFormats *formats)
230 set_common_formats(ctx, formats, AVMEDIA_TYPE_AUDIO,
231 offsetof(AVFilterLink, in_formats),
232 offsetof(AVFilterLink, out_formats));
235 void avfilter_set_common_channel_layouts(AVFilterContext *ctx, AVFilterFormats *formats)
237 set_common_formats(ctx, formats, AVMEDIA_TYPE_AUDIO,
238 offsetof(AVFilterLink, in_chlayouts),
239 offsetof(AVFilterLink, out_chlayouts));
242 int avfilter_default_query_formats(AVFilterContext *ctx)
244 avfilter_set_common_pixel_formats(ctx, avfilter_all_formats(AVMEDIA_TYPE_VIDEO));
245 avfilter_set_common_sample_formats(ctx, avfilter_all_formats(AVMEDIA_TYPE_AUDIO));
246 avfilter_set_common_channel_layouts(ctx, avfilter_all_channel_layouts());
251 void avfilter_null_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
253 avfilter_start_frame(link->dst->outputs[0], picref);
256 void avfilter_null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
258 avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
261 void avfilter_null_end_frame(AVFilterLink *link)
263 avfilter_end_frame(link->dst->outputs[0]);
266 void avfilter_null_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
268 avfilter_filter_samples(link->dst->outputs[0], samplesref);
271 AVFilterBufferRef *avfilter_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
273 return avfilter_get_video_buffer(link->dst->outputs[0], perms, w, h);
276 AVFilterBufferRef *avfilter_null_get_audio_buffer(AVFilterLink *link, int perms,
277 enum AVSampleFormat sample_fmt, int size,
278 int64_t channel_layout, int packed)
280 return avfilter_get_audio_buffer(link->dst->outputs[0], perms, sample_fmt,
281 size, channel_layout, packed);