2 * Filter layer - default implementations
3 * Copyright (c) 2007 Bobby Bingham
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; 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"
30 /* TODO: buffer pool. see comment for avfilter_default_get_video_buffer() */
31 void ff_avfilter_default_free_buffer(AVFilterBuffer *ptr)
33 if (ptr->extended_data != ptr->data)
34 av_freep(&ptr->extended_data);
35 av_free(ptr->data[0]);
39 /* TODO: set the buffer's priv member to a context structure for the whole
40 * filter chain. This will allow for a buffer pool instead of the constant
41 * alloc & free cycle currently implemented. */
42 AVFilterBufferRef *avfilter_default_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
46 AVFilterBufferRef *picref = NULL;
48 // +2 is needed for swscaler, +16 to be SIMD-friendly
49 if (av_image_alloc(data, linesize, w, h, link->format, 16) < 0)
52 picref = avfilter_get_video_buffer_ref_from_arrays(data, linesize,
53 perms, w, h, link->format);
62 AVFilterBufferRef *ff_default_get_audio_buffer(AVFilterLink *link, int perms,
65 AVFilterBufferRef *samplesref = NULL;
67 int planar = av_sample_fmt_is_planar(link->format);
68 int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
69 int planes = planar ? nb_channels : 1;
72 if (!(data = av_mallocz(sizeof(*data) * planes)))
75 if (av_samples_alloc(data, &linesize, nb_channels, nb_samples, link->format, 0) < 0)
78 samplesref = avfilter_get_audio_buffer_ref_from_arrays(data, linesize, perms,
79 nb_samples, link->format,
80 link->channel_layout);
93 void avfilter_default_start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
95 AVFilterLink *outlink = NULL;
97 if (inlink->dst->output_count)
98 outlink = inlink->dst->outputs[0];
101 outlink->out_buf = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
102 avfilter_copy_buffer_ref_props(outlink->out_buf, picref);
103 avfilter_start_frame(outlink, avfilter_ref_buffer(outlink->out_buf, ~0));
107 void avfilter_default_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
109 AVFilterLink *outlink = NULL;
111 if (inlink->dst->output_count)
112 outlink = inlink->dst->outputs[0];
115 avfilter_draw_slice(outlink, y, h, slice_dir);
118 void avfilter_default_end_frame(AVFilterLink *inlink)
120 AVFilterLink *outlink = NULL;
122 if (inlink->dst->output_count)
123 outlink = inlink->dst->outputs[0];
125 avfilter_unref_buffer(inlink->cur_buf);
126 inlink->cur_buf = NULL;
129 if (outlink->out_buf) {
130 avfilter_unref_buffer(outlink->out_buf);
131 outlink->out_buf = NULL;
133 avfilter_end_frame(outlink);
137 /* FIXME: samplesref is same as link->cur_buf. Need to consider removing the redundant parameter. */
138 void ff_default_filter_samples(AVFilterLink *inlink, AVFilterBufferRef *samplesref)
140 AVFilterLink *outlink = NULL;
142 if (inlink->dst->output_count)
143 outlink = inlink->dst->outputs[0];
146 outlink->out_buf = ff_default_get_audio_buffer(inlink, AV_PERM_WRITE,
147 samplesref->audio->nb_samples);
148 outlink->out_buf->pts = samplesref->pts;
149 outlink->out_buf->audio->sample_rate = samplesref->audio->sample_rate;
150 ff_filter_samples(outlink, avfilter_ref_buffer(outlink->out_buf, ~0));
151 avfilter_unref_buffer(outlink->out_buf);
152 outlink->out_buf = NULL;
154 avfilter_unref_buffer(samplesref);
155 inlink->cur_buf = NULL;
159 * default config_link() implementation for output video links to simplify
160 * the implementation of one input one output video filters */
161 int avfilter_default_config_output_link(AVFilterLink *link)
163 if (link->src->input_count && link->src->inputs[0]) {
164 if (link->type == AVMEDIA_TYPE_VIDEO) {
165 link->w = link->src->inputs[0]->w;
166 link->h = link->src->inputs[0]->h;
167 link->time_base = link->src->inputs[0]->time_base;
168 } else if (link->type == AVMEDIA_TYPE_AUDIO) {
169 link->channel_layout = link->src->inputs[0]->channel_layout;
170 link->sample_rate = link->src->inputs[0]->sample_rate;
173 /* XXX: any non-simple filter which would cause this branch to be taken
174 * really should implement its own config_props() for this link. */
182 * A helper for query_formats() which sets all links to the same list of
183 * formats. If there are no links hooked to this filter, the list of formats is
186 * FIXME: this will need changed for filters with a mix of pad types
187 * (video + audio, etc)
189 void avfilter_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
193 for (i = 0; i < ctx->input_count; i++) {
194 if (ctx->inputs[i]) {
195 avfilter_formats_ref(formats, &ctx->inputs[i]->out_formats);
199 for (i = 0; i < ctx->output_count; i++) {
200 if (ctx->outputs[i]) {
201 avfilter_formats_ref(formats, &ctx->outputs[i]->in_formats);
207 av_free(formats->formats);
208 av_free(formats->refs);
213 int avfilter_default_query_formats(AVFilterContext *ctx)
215 enum AVMediaType type = ctx->inputs && ctx->inputs [0] ? ctx->inputs [0]->type :
216 ctx->outputs && ctx->outputs[0] ? ctx->outputs[0]->type :
219 avfilter_set_common_formats(ctx, avfilter_all_formats(type));
223 void avfilter_null_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
225 avfilter_start_frame(link->dst->outputs[0], picref);
228 void avfilter_null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
230 avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
233 void avfilter_null_end_frame(AVFilterLink *link)
235 avfilter_end_frame(link->dst->outputs[0]);
238 void ff_null_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
240 ff_filter_samples(link->dst->outputs[0], samplesref);
243 AVFilterBufferRef *avfilter_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
245 return avfilter_get_video_buffer(link->dst->outputs[0], perms, w, h);
248 AVFilterBufferRef *ff_null_get_audio_buffer(AVFilterLink *link, int perms,
251 return ff_get_audio_buffer(link->dst->outputs[0], perms, nb_samples);