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;
177 static void set_common_formats(AVFilterContext *ctx, AVFilterFormats *fmts,
178 enum AVMediaType type, int offin, int offout)
181 for (i = 0; i < ctx->input_count; i++)
182 if (ctx->inputs[i] && ctx->inputs[i]->type == type)
183 avfilter_formats_ref(fmts,
184 (AVFilterFormats **)((uint8_t *)ctx->inputs[i]+offout));
186 for (i = 0; i < ctx->output_count; i++)
187 if (ctx->outputs[i] && ctx->outputs[i]->type == type)
188 avfilter_formats_ref(fmts,
189 (AVFilterFormats **)((uint8_t *)ctx->outputs[i]+offin));
191 if (!fmts->refcount) {
192 av_free(fmts->formats);
198 void avfilter_set_common_pixel_formats(AVFilterContext *ctx, AVFilterFormats *formats)
200 set_common_formats(ctx, formats, AVMEDIA_TYPE_VIDEO,
201 offsetof(AVFilterLink, in_formats),
202 offsetof(AVFilterLink, out_formats));
205 void avfilter_set_common_sample_formats(AVFilterContext *ctx, AVFilterFormats *formats)
207 set_common_formats(ctx, formats, AVMEDIA_TYPE_AUDIO,
208 offsetof(AVFilterLink, in_formats),
209 offsetof(AVFilterLink, out_formats));
212 void avfilter_set_common_channel_layouts(AVFilterContext *ctx, AVFilterFormats *formats)
214 set_common_formats(ctx, formats, AVMEDIA_TYPE_AUDIO,
215 offsetof(AVFilterLink, in_chlayouts),
216 offsetof(AVFilterLink, out_chlayouts));
219 void avfilter_set_common_packing_formats(AVFilterContext *ctx, AVFilterFormats *formats)
221 set_common_formats(ctx, formats, AVMEDIA_TYPE_AUDIO,
222 offsetof(AVFilterLink, in_packing),
223 offsetof(AVFilterLink, out_packing));
226 int avfilter_default_query_formats(AVFilterContext *ctx)
228 avfilter_set_common_pixel_formats(ctx, avfilter_all_formats(AVMEDIA_TYPE_VIDEO));
229 avfilter_set_common_sample_formats(ctx, avfilter_all_formats(AVMEDIA_TYPE_AUDIO));
230 avfilter_set_common_channel_layouts(ctx, avfilter_all_channel_layouts());
231 avfilter_set_common_packing_formats(ctx, avfilter_all_packing_formats());
236 void avfilter_null_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
238 avfilter_start_frame(link->dst->outputs[0], picref);
241 void avfilter_null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
243 avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
246 void avfilter_null_end_frame(AVFilterLink *link)
248 avfilter_end_frame(link->dst->outputs[0]);
251 void avfilter_null_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
253 avfilter_filter_samples(link->dst->outputs[0], samplesref);
256 AVFilterBufferRef *avfilter_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
258 return avfilter_get_video_buffer(link->dst->outputs[0], perms, w, h);
261 AVFilterBufferRef *avfilter_null_get_audio_buffer(AVFilterLink *link, int perms,
262 enum AVSampleFormat sample_fmt, int size,
263 int64_t channel_layout, int packed)
265 return avfilter_get_audio_buffer(link->dst->outputs[0], perms, sample_fmt,
266 size, channel_layout, packed);