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,
86 AVFilterBufferRef *samplesref = NULL;
89 int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
91 /* Calculate total buffer size, round to multiple of 16 to be SIMD friendly */
92 if (av_samples_alloc(data, linesize,
93 nb_channels, nb_samples, link->format,
94 link->planar, 16) < 0)
98 avfilter_get_audio_buffer_ref_from_arrays(data, linesize, perms,
99 nb_samples, link->format,
100 link->channel_layout, link->planar);
109 void avfilter_default_start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
111 AVFilterLink *outlink = NULL;
113 if (inlink->dst->output_count)
114 outlink = inlink->dst->outputs[0];
117 outlink->out_buf = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
118 avfilter_copy_buffer_ref_props(outlink->out_buf, picref);
119 avfilter_start_frame(outlink, avfilter_ref_buffer(outlink->out_buf, ~0));
123 void avfilter_default_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
125 AVFilterLink *outlink = NULL;
127 if (inlink->dst->output_count)
128 outlink = inlink->dst->outputs[0];
131 avfilter_draw_slice(outlink, y, h, slice_dir);
134 void avfilter_default_end_frame(AVFilterLink *inlink)
136 AVFilterLink *outlink = NULL;
138 if (inlink->dst->output_count)
139 outlink = inlink->dst->outputs[0];
141 avfilter_unref_buffer(inlink->cur_buf);
142 inlink->cur_buf = NULL;
145 if (outlink->out_buf) {
146 avfilter_unref_buffer(outlink->out_buf);
147 outlink->out_buf = NULL;
149 avfilter_end_frame(outlink);
153 /* FIXME: samplesref is same as link->cur_buf. Need to consider removing the redundant parameter. */
154 void avfilter_default_filter_samples(AVFilterLink *inlink, AVFilterBufferRef *samplesref)
156 AVFilterLink *outlink = NULL;
158 if (inlink->dst->output_count)
159 outlink = inlink->dst->outputs[0];
162 outlink->out_buf = avfilter_default_get_audio_buffer(inlink, AV_PERM_WRITE,
163 samplesref->audio->nb_samples);
164 outlink->out_buf->pts = samplesref->pts;
165 outlink->out_buf->audio->sample_rate = samplesref->audio->sample_rate;
166 avfilter_filter_samples(outlink, avfilter_ref_buffer(outlink->out_buf, ~0));
167 avfilter_unref_buffer(outlink->out_buf);
168 outlink->out_buf = NULL;
170 avfilter_unref_buffer(samplesref);
171 inlink->cur_buf = NULL;
174 static void set_common_formats(AVFilterContext *ctx, AVFilterFormats *fmts,
175 enum AVMediaType type, int offin, int offout)
178 for (i = 0; i < ctx->input_count; i++)
179 if (ctx->inputs[i] && ctx->inputs[i]->type == type)
180 avfilter_formats_ref(fmts,
181 (AVFilterFormats **)((uint8_t *)ctx->inputs[i]+offout));
183 for (i = 0; i < ctx->output_count; i++)
184 if (ctx->outputs[i] && ctx->outputs[i]->type == type)
185 avfilter_formats_ref(fmts,
186 (AVFilterFormats **)((uint8_t *)ctx->outputs[i]+offin));
188 if (!fmts->refcount) {
189 av_free(fmts->formats);
195 void avfilter_set_common_pixel_formats(AVFilterContext *ctx, AVFilterFormats *formats)
197 set_common_formats(ctx, formats, AVMEDIA_TYPE_VIDEO,
198 offsetof(AVFilterLink, in_formats),
199 offsetof(AVFilterLink, out_formats));
202 void avfilter_set_common_sample_formats(AVFilterContext *ctx, AVFilterFormats *formats)
204 set_common_formats(ctx, formats, AVMEDIA_TYPE_AUDIO,
205 offsetof(AVFilterLink, in_formats),
206 offsetof(AVFilterLink, out_formats));
209 void avfilter_set_common_channel_layouts(AVFilterContext *ctx, AVFilterFormats *formats)
211 set_common_formats(ctx, formats, AVMEDIA_TYPE_AUDIO,
212 offsetof(AVFilterLink, in_chlayouts),
213 offsetof(AVFilterLink, out_chlayouts));
216 void avfilter_set_common_packing_formats(AVFilterContext *ctx, AVFilterFormats *formats)
218 set_common_formats(ctx, formats, AVMEDIA_TYPE_AUDIO,
219 offsetof(AVFilterLink, in_packing),
220 offsetof(AVFilterLink, out_packing));
223 int avfilter_default_query_formats(AVFilterContext *ctx)
225 avfilter_set_common_pixel_formats(ctx, avfilter_all_formats(AVMEDIA_TYPE_VIDEO));
226 avfilter_set_common_sample_formats(ctx, avfilter_all_formats(AVMEDIA_TYPE_AUDIO));
227 avfilter_set_common_channel_layouts(ctx, avfilter_all_channel_layouts());
228 avfilter_set_common_packing_formats(ctx, avfilter_all_packing_formats());
233 void avfilter_null_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
235 avfilter_start_frame(link->dst->outputs[0], picref);
238 void avfilter_null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
240 avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
243 void avfilter_null_end_frame(AVFilterLink *link)
245 avfilter_end_frame(link->dst->outputs[0]);
248 void avfilter_null_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
250 avfilter_filter_samples(link->dst->outputs[0], samplesref);
253 AVFilterBufferRef *avfilter_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
255 return avfilter_get_video_buffer(link->dst->outputs[0], perms, w, h);
258 AVFilterBufferRef *avfilter_null_get_audio_buffer(AVFilterLink *link, int perms,
261 return avfilter_get_audio_buffer(link->dst->outputs[0], perms, nb_samples);