]> git.sesse.net Git - ffmpeg/blob - libavfilter/defaults.c
lavfi: remove some audio-related function from public API.
[ffmpeg] / libavfilter / defaults.c
1 /*
2  * Filter layer - default implementations
3  * Copyright (c) 2007 Bobby Bingham
4  *
5  * This file is part of Libav.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "libavutil/audioconvert.h"
23 #include "libavutil/imgutils.h"
24 #include "libavutil/samplefmt.h"
25
26 #include "audio.h"
27 #include "avfilter.h"
28 #include "internal.h"
29
30 /* TODO: buffer pool.  see comment for avfilter_default_get_video_buffer() */
31 void ff_avfilter_default_free_buffer(AVFilterBuffer *ptr)
32 {
33     if (ptr->extended_data != ptr->data)
34         av_freep(&ptr->extended_data);
35     av_free(ptr->data[0]);
36     av_free(ptr);
37 }
38
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)
43 {
44     int linesize[4];
45     uint8_t *data[4];
46     AVFilterBufferRef *picref = NULL;
47
48     // +2 is needed for swscaler, +16 to be SIMD-friendly
49     if (av_image_alloc(data, linesize, w, h, link->format, 16) < 0)
50         return NULL;
51
52     picref = avfilter_get_video_buffer_ref_from_arrays(data, linesize,
53                                                        perms, w, h, link->format);
54     if (!picref) {
55         av_free(data[0]);
56         return NULL;
57     }
58
59     return picref;
60 }
61
62 AVFilterBufferRef *ff_default_get_audio_buffer(AVFilterLink *link, int perms,
63                                                int nb_samples)
64 {
65     AVFilterBufferRef *samplesref = NULL;
66     uint8_t **data;
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;
70     int linesize;
71
72     if (!(data = av_mallocz(sizeof(*data) * planes)))
73         goto fail;
74
75     if (av_samples_alloc(data, &linesize, nb_channels, nb_samples, link->format, 0) < 0)
76         goto fail;
77
78     samplesref = avfilter_get_audio_buffer_ref_from_arrays(data, linesize, perms,
79                                                            nb_samples, link->format,
80                                                            link->channel_layout);
81     if (!samplesref)
82         goto fail;
83
84     av_freep(&data);
85
86 fail:
87     if (data)
88         av_freep(&data[0]);
89     av_freep(&data);
90     return samplesref;
91 }
92
93 void avfilter_default_start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
94 {
95     AVFilterLink *outlink = NULL;
96
97     if (inlink->dst->output_count)
98         outlink = inlink->dst->outputs[0];
99
100     if (outlink) {
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));
104     }
105 }
106
107 void avfilter_default_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
108 {
109     AVFilterLink *outlink = NULL;
110
111     if (inlink->dst->output_count)
112         outlink = inlink->dst->outputs[0];
113
114     if (outlink)
115         avfilter_draw_slice(outlink, y, h, slice_dir);
116 }
117
118 void avfilter_default_end_frame(AVFilterLink *inlink)
119 {
120     AVFilterLink *outlink = NULL;
121
122     if (inlink->dst->output_count)
123         outlink = inlink->dst->outputs[0];
124
125     avfilter_unref_buffer(inlink->cur_buf);
126     inlink->cur_buf = NULL;
127
128     if (outlink) {
129         if (outlink->out_buf) {
130             avfilter_unref_buffer(outlink->out_buf);
131             outlink->out_buf = NULL;
132         }
133         avfilter_end_frame(outlink);
134     }
135 }
136
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)
139 {
140     AVFilterLink *outlink = NULL;
141
142     if (inlink->dst->output_count)
143         outlink = inlink->dst->outputs[0];
144
145     if (outlink) {
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;
153     }
154     avfilter_unref_buffer(samplesref);
155     inlink->cur_buf = NULL;
156 }
157
158 /**
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)
162 {
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;
171         }
172     } else {
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. */
175         return -1;
176     }
177
178     return 0;
179 }
180
181 /**
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
184  * freed.
185  *
186  * FIXME: this will need changed for filters with a mix of pad types
187  * (video + audio, etc)
188  */
189 void avfilter_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
190 {
191     int count = 0, i;
192
193     for (i = 0; i < ctx->input_count; i++) {
194         if (ctx->inputs[i]) {
195             avfilter_formats_ref(formats, &ctx->inputs[i]->out_formats);
196             count++;
197         }
198     }
199     for (i = 0; i < ctx->output_count; i++) {
200         if (ctx->outputs[i]) {
201             avfilter_formats_ref(formats, &ctx->outputs[i]->in_formats);
202             count++;
203         }
204     }
205
206     if (!count) {
207         av_free(formats->formats);
208         av_free(formats->refs);
209         av_free(formats);
210     }
211 }
212
213 int avfilter_default_query_formats(AVFilterContext *ctx)
214 {
215     enum AVMediaType type = ctx->inputs  && ctx->inputs [0] ? ctx->inputs [0]->type :
216                             ctx->outputs && ctx->outputs[0] ? ctx->outputs[0]->type :
217                             AVMEDIA_TYPE_VIDEO;
218
219     avfilter_set_common_formats(ctx, avfilter_all_formats(type));
220     return 0;
221 }
222
223 void avfilter_null_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
224 {
225     avfilter_start_frame(link->dst->outputs[0], picref);
226 }
227
228 void avfilter_null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
229 {
230     avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
231 }
232
233 void avfilter_null_end_frame(AVFilterLink *link)
234 {
235     avfilter_end_frame(link->dst->outputs[0]);
236 }
237
238 void ff_null_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
239 {
240     ff_filter_samples(link->dst->outputs[0], samplesref);
241 }
242
243 AVFilterBufferRef *avfilter_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
244 {
245     return avfilter_get_video_buffer(link->dst->outputs[0], perms, w, h);
246 }
247
248 AVFilterBufferRef *ff_null_get_audio_buffer(AVFilterLink *link, int perms,
249                                             int nb_samples)
250 {
251     return ff_get_audio_buffer(link->dst->outputs[0], perms, nb_samples);
252 }